crypto_extra.h 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141
  1. /**
  2. * \file psa/crypto_extra.h
  3. *
  4. * \brief PSA cryptography module: Mbed TLS vendor extensions
  5. *
  6. * \note This file may not be included directly. Applications must
  7. * include psa/crypto.h.
  8. *
  9. * This file is reserved for vendor-specific definitions.
  10. */
  11. /*
  12. * Copyright The Mbed TLS Contributors
  13. * SPDX-License-Identifier: Apache-2.0
  14. *
  15. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  16. * not use this file except in compliance with the License.
  17. * You may obtain a copy of the License at
  18. *
  19. * http://www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing, software
  22. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  23. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. * See the License for the specific language governing permissions and
  25. * limitations under the License.
  26. */
  27. #ifndef PSA_CRYPTO_EXTRA_H
  28. #define PSA_CRYPTO_EXTRA_H
  29. #include "mbedtls/private_access.h"
  30. #include "mbedtls/platform_util.h"
  31. #include "crypto_types.h"
  32. #include "crypto_compat.h"
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /* UID for secure storage seed */
  37. #define PSA_CRYPTO_ITS_RANDOM_SEED_UID 0xFFFFFF52
  38. /* See mbedtls_config.h for definition */
  39. #if !defined(MBEDTLS_PSA_KEY_SLOT_COUNT)
  40. #define MBEDTLS_PSA_KEY_SLOT_COUNT 32
  41. #endif
  42. /** \addtogroup attributes
  43. * @{
  44. */
  45. /** \brief Declare the enrollment algorithm for a key.
  46. *
  47. * An operation on a key may indifferently use the algorithm set with
  48. * psa_set_key_algorithm() or with this function.
  49. *
  50. * \param[out] attributes The attribute structure to write to.
  51. * \param alg2 A second algorithm that the key may be used
  52. * for, in addition to the algorithm set with
  53. * psa_set_key_algorithm().
  54. *
  55. * \warning Setting an enrollment algorithm is not recommended, because
  56. * using the same key with different algorithms can allow some
  57. * attacks based on arithmetic relations between different
  58. * computations made with the same key, or can escalate harmless
  59. * side channels into exploitable ones. Use this function only
  60. * if it is necessary to support a protocol for which it has been
  61. * verified that the usage of the key with multiple algorithms
  62. * is safe.
  63. */
  64. static inline void psa_set_key_enrollment_algorithm(
  65. psa_key_attributes_t *attributes,
  66. psa_algorithm_t alg2)
  67. {
  68. attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2) = alg2;
  69. }
  70. /** Retrieve the enrollment algorithm policy from key attributes.
  71. *
  72. * \param[in] attributes The key attribute structure to query.
  73. *
  74. * \return The enrollment algorithm stored in the attribute structure.
  75. */
  76. static inline psa_algorithm_t psa_get_key_enrollment_algorithm(
  77. const psa_key_attributes_t *attributes)
  78. {
  79. return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2);
  80. }
  81. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  82. /** Retrieve the slot number where a key is stored.
  83. *
  84. * A slot number is only defined for keys that are stored in a secure
  85. * element.
  86. *
  87. * This information is only useful if the secure element is not entirely
  88. * managed through the PSA Cryptography API. It is up to the secure
  89. * element driver to decide how PSA slot numbers map to any other interface
  90. * that the secure element may have.
  91. *
  92. * \param[in] attributes The key attribute structure to query.
  93. * \param[out] slot_number On success, the slot number containing the key.
  94. *
  95. * \retval #PSA_SUCCESS
  96. * The key is located in a secure element, and \p *slot_number
  97. * indicates the slot number that contains it.
  98. * \retval #PSA_ERROR_NOT_PERMITTED
  99. * The caller is not permitted to query the slot number.
  100. * Mbed Crypto currently does not return this error.
  101. * \retval #PSA_ERROR_INVALID_ARGUMENT
  102. * The key is not located in a secure element.
  103. */
  104. psa_status_t psa_get_key_slot_number(
  105. const psa_key_attributes_t *attributes,
  106. psa_key_slot_number_t *slot_number);
  107. /** Choose the slot number where a key is stored.
  108. *
  109. * This function declares a slot number in the specified attribute
  110. * structure.
  111. *
  112. * A slot number is only meaningful for keys that are stored in a secure
  113. * element. It is up to the secure element driver to decide how PSA slot
  114. * numbers map to any other interface that the secure element may have.
  115. *
  116. * \note Setting a slot number in key attributes for a key creation can
  117. * cause the following errors when creating the key:
  118. * - #PSA_ERROR_NOT_SUPPORTED if the selected secure element does
  119. * not support choosing a specific slot number.
  120. * - #PSA_ERROR_NOT_PERMITTED if the caller is not permitted to
  121. * choose slot numbers in general or to choose this specific slot.
  122. * - #PSA_ERROR_INVALID_ARGUMENT if the chosen slot number is not
  123. * valid in general or not valid for this specific key.
  124. * - #PSA_ERROR_ALREADY_EXISTS if there is already a key in the
  125. * selected slot.
  126. *
  127. * \param[out] attributes The attribute structure to write to.
  128. * \param slot_number The slot number to set.
  129. */
  130. static inline void psa_set_key_slot_number(
  131. psa_key_attributes_t *attributes,
  132. psa_key_slot_number_t slot_number)
  133. {
  134. attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(flags) |= MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
  135. attributes->MBEDTLS_PRIVATE(slot_number) = slot_number;
  136. }
  137. /** Remove the slot number attribute from a key attribute structure.
  138. *
  139. * This function undoes the action of psa_set_key_slot_number().
  140. *
  141. * \param[out] attributes The attribute structure to write to.
  142. */
  143. static inline void psa_clear_key_slot_number(
  144. psa_key_attributes_t *attributes)
  145. {
  146. attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(flags) &=
  147. ~MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
  148. }
  149. /** Register a key that is already present in a secure element.
  150. *
  151. * The key must be located in a secure element designated by the
  152. * lifetime field in \p attributes, in the slot set with
  153. * psa_set_key_slot_number() in the attribute structure.
  154. * This function makes the key available through the key identifier
  155. * specified in \p attributes.
  156. *
  157. * \param[in] attributes The attributes of the existing key.
  158. *
  159. * \retval #PSA_SUCCESS
  160. * The key was successfully registered.
  161. * Note that depending on the design of the driver, this may or may
  162. * not guarantee that a key actually exists in the designated slot
  163. * and is compatible with the specified attributes.
  164. * \retval #PSA_ERROR_ALREADY_EXISTS
  165. * There is already a key with the identifier specified in
  166. * \p attributes.
  167. * \retval #PSA_ERROR_NOT_SUPPORTED
  168. * The secure element driver for the specified lifetime does not
  169. * support registering a key.
  170. * \retval #PSA_ERROR_INVALID_ARGUMENT
  171. * The identifier in \p attributes is invalid, namely the identifier is
  172. * not in the user range, or
  173. * \p attributes specifies a lifetime which is not located
  174. * in a secure element, or no slot number is specified in \p attributes,
  175. * or the specified slot number is not valid.
  176. * \retval #PSA_ERROR_NOT_PERMITTED
  177. * The caller is not authorized to register the specified key slot.
  178. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  179. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
  180. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  181. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  182. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  183. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  184. * \retval #PSA_ERROR_BAD_STATE
  185. * The library has not been previously initialized by psa_crypto_init().
  186. * It is implementation-dependent whether a failure to initialize
  187. * results in this error code.
  188. */
  189. psa_status_t mbedtls_psa_register_se_key(
  190. const psa_key_attributes_t *attributes);
  191. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  192. /**@}*/
  193. /**
  194. * \brief Library deinitialization.
  195. *
  196. * This function clears all data associated with the PSA layer,
  197. * including the whole key store.
  198. *
  199. * This is an Mbed TLS extension.
  200. */
  201. void mbedtls_psa_crypto_free(void);
  202. /** \brief Statistics about
  203. * resource consumption related to the PSA keystore.
  204. *
  205. * \note The content of this structure is not part of the stable API and ABI
  206. * of Mbed Crypto and may change arbitrarily from version to version.
  207. */
  208. typedef struct mbedtls_psa_stats_s {
  209. /** Number of slots containing key material for a volatile key. */
  210. size_t MBEDTLS_PRIVATE(volatile_slots);
  211. /** Number of slots containing key material for a key which is in
  212. * internal persistent storage. */
  213. size_t MBEDTLS_PRIVATE(persistent_slots);
  214. /** Number of slots containing a reference to a key in a
  215. * secure element. */
  216. size_t MBEDTLS_PRIVATE(external_slots);
  217. /** Number of slots which are occupied, but do not contain
  218. * key material yet. */
  219. size_t MBEDTLS_PRIVATE(half_filled_slots);
  220. /** Number of slots that contain cache data. */
  221. size_t MBEDTLS_PRIVATE(cache_slots);
  222. /** Number of slots that are not used for anything. */
  223. size_t MBEDTLS_PRIVATE(empty_slots);
  224. /** Number of slots that are locked. */
  225. size_t MBEDTLS_PRIVATE(locked_slots);
  226. /** Largest key id value among open keys in internal persistent storage. */
  227. psa_key_id_t MBEDTLS_PRIVATE(max_open_internal_key_id);
  228. /** Largest key id value among open keys in secure elements. */
  229. psa_key_id_t MBEDTLS_PRIVATE(max_open_external_key_id);
  230. } mbedtls_psa_stats_t;
  231. /** \brief Get statistics about
  232. * resource consumption related to the PSA keystore.
  233. *
  234. * \note When Mbed Crypto is built as part of a service, with isolation
  235. * between the application and the keystore, the service may or
  236. * may not expose this function.
  237. */
  238. void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats);
  239. /**
  240. * \brief Inject an initial entropy seed for the random generator into
  241. * secure storage.
  242. *
  243. * This function injects data to be used as a seed for the random generator
  244. * used by the PSA Crypto implementation. On devices that lack a trusted
  245. * entropy source (preferably a hardware random number generator),
  246. * the Mbed PSA Crypto implementation uses this value to seed its
  247. * random generator.
  248. *
  249. * On devices without a trusted entropy source, this function must be
  250. * called exactly once in the lifetime of the device. On devices with
  251. * a trusted entropy source, calling this function is optional.
  252. * In all cases, this function may only be called before calling any
  253. * other function in the PSA Crypto API, including psa_crypto_init().
  254. *
  255. * When this function returns successfully, it populates a file in
  256. * persistent storage. Once the file has been created, this function
  257. * can no longer succeed.
  258. *
  259. * If any error occurs, this function does not change the system state.
  260. * You can call this function again after correcting the reason for the
  261. * error if possible.
  262. *
  263. * \warning This function **can** fail! Callers MUST check the return status.
  264. *
  265. * \warning If you use this function, you should use it as part of a
  266. * factory provisioning process. The value of the injected seed
  267. * is critical to the security of the device. It must be
  268. * *secret*, *unpredictable* and (statistically) *unique per device*.
  269. * You should be generate it randomly using a cryptographically
  270. * secure random generator seeded from trusted entropy sources.
  271. * You should transmit it securely to the device and ensure
  272. * that its value is not leaked or stored anywhere beyond the
  273. * needs of transmitting it from the point of generation to
  274. * the call of this function, and erase all copies of the value
  275. * once this function returns.
  276. *
  277. * This is an Mbed TLS extension.
  278. *
  279. * \note This function is only available on the following platforms:
  280. * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled.
  281. * Note that you must provide compatible implementations of
  282. * mbedtls_nv_seed_read and mbedtls_nv_seed_write.
  283. * * In a client-server integration of PSA Cryptography, on the client side,
  284. * if the server supports this feature.
  285. * \param[in] seed Buffer containing the seed value to inject.
  286. * \param[in] seed_size Size of the \p seed buffer.
  287. * The size of the seed in bytes must be greater
  288. * or equal to both #MBEDTLS_ENTROPY_BLOCK_SIZE
  289. * and the value of \c MBEDTLS_ENTROPY_MIN_PLATFORM
  290. * in `library/entropy_poll.h` in the Mbed TLS source
  291. * code.
  292. * It must be less or equal to
  293. * #MBEDTLS_ENTROPY_MAX_SEED_SIZE.
  294. *
  295. * \retval #PSA_SUCCESS
  296. * The seed value was injected successfully. The random generator
  297. * of the PSA Crypto implementation is now ready for use.
  298. * You may now call psa_crypto_init() and use the PSA Crypto
  299. * implementation.
  300. * \retval #PSA_ERROR_INVALID_ARGUMENT
  301. * \p seed_size is out of range.
  302. * \retval #PSA_ERROR_STORAGE_FAILURE
  303. * There was a failure reading or writing from storage.
  304. * \retval #PSA_ERROR_NOT_PERMITTED
  305. * The library has already been initialized. It is no longer
  306. * possible to call this function.
  307. */
  308. psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
  309. size_t seed_size);
  310. /** \addtogroup crypto_types
  311. * @{
  312. */
  313. /** DSA public key.
  314. *
  315. * The import and export format is the
  316. * representation of the public key `y = g^x mod p` as a big-endian byte
  317. * string. The length of the byte string is the length of the base prime `p`
  318. * in bytes.
  319. */
  320. #define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t) 0x4002)
  321. /** DSA key pair (private and public key).
  322. *
  323. * The import and export format is the
  324. * representation of the private key `x` as a big-endian byte string. The
  325. * length of the byte string is the private key size in bytes (leading zeroes
  326. * are not stripped).
  327. *
  328. * Deterministic DSA key derivation with psa_generate_derived_key follows
  329. * FIPS 186-4 §B.1.2: interpret the byte string as integer
  330. * in big-endian order. Discard it if it is not in the range
  331. * [0, *N* - 2] where *N* is the boundary of the private key domain
  332. * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
  333. * or the order of the curve's base point for ECC).
  334. * Add 1 to the resulting integer and use this as the private key *x*.
  335. *
  336. */
  337. #define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t) 0x7002)
  338. /** Whether a key type is a DSA key (pair or public-only). */
  339. #define PSA_KEY_TYPE_IS_DSA(type) \
  340. (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
  341. #define PSA_ALG_DSA_BASE ((psa_algorithm_t) 0x06000400)
  342. /** DSA signature with hashing.
  343. *
  344. * This is the signature scheme defined by FIPS 186-4,
  345. * with a random per-message secret number (*k*).
  346. *
  347. * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
  348. * #PSA_ALG_IS_HASH(\p hash_alg) is true).
  349. * This includes #PSA_ALG_ANY_HASH
  350. * when specifying the algorithm in a usage policy.
  351. *
  352. * \return The corresponding DSA signature algorithm.
  353. * \return Unspecified if \p hash_alg is not a supported
  354. * hash algorithm.
  355. */
  356. #define PSA_ALG_DSA(hash_alg) \
  357. (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
  358. #define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t) 0x06000500)
  359. #define PSA_ALG_DSA_DETERMINISTIC_FLAG PSA_ALG_ECDSA_DETERMINISTIC_FLAG
  360. /** Deterministic DSA signature with hashing.
  361. *
  362. * This is the deterministic variant defined by RFC 6979 of
  363. * the signature scheme defined by FIPS 186-4.
  364. *
  365. * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
  366. * #PSA_ALG_IS_HASH(\p hash_alg) is true).
  367. * This includes #PSA_ALG_ANY_HASH
  368. * when specifying the algorithm in a usage policy.
  369. *
  370. * \return The corresponding DSA signature algorithm.
  371. * \return Unspecified if \p hash_alg is not a supported
  372. * hash algorithm.
  373. */
  374. #define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
  375. (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
  376. #define PSA_ALG_IS_DSA(alg) \
  377. (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
  378. PSA_ALG_DSA_BASE)
  379. #define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
  380. (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
  381. #define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
  382. (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
  383. #define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
  384. (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
  385. /* We need to expand the sample definition of this macro from
  386. * the API definition. */
  387. #undef PSA_ALG_IS_VENDOR_HASH_AND_SIGN
  388. #define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) \
  389. PSA_ALG_IS_DSA(alg)
  390. /**@}*/
  391. /** \addtogroup attributes
  392. * @{
  393. */
  394. /** Custom Diffie-Hellman group.
  395. *
  396. * For keys of type #PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or
  397. * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM), the group data comes
  398. * from domain parameters set by psa_set_key_domain_parameters().
  399. */
  400. #define PSA_DH_FAMILY_CUSTOM ((psa_dh_family_t) 0x7e)
  401. /** PAKE operation stages. */
  402. #define PSA_PAKE_OPERATION_STAGE_SETUP 0
  403. #define PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS 1
  404. #define PSA_PAKE_OPERATION_STAGE_COMPUTATION 2
  405. /**
  406. * \brief Set domain parameters for a key.
  407. *
  408. * Some key types require additional domain parameters in addition to
  409. * the key type identifier and the key size. Use this function instead
  410. * of psa_set_key_type() when you need to specify domain parameters.
  411. *
  412. * The format for the required domain parameters varies based on the key type.
  413. *
  414. * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEY_PAIR),
  415. * the domain parameter data consists of the public exponent,
  416. * represented as a big-endian integer with no leading zeros.
  417. * This information is used when generating an RSA key pair.
  418. * When importing a key, the public exponent is read from the imported
  419. * key data and the exponent recorded in the attribute structure is ignored.
  420. * As an exception, the public exponent 65537 is represented by an empty
  421. * byte string.
  422. * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEY_PAIR),
  423. * the `Dss-Params` format as defined by RFC 3279 §2.3.2.
  424. * ```
  425. * Dss-Params ::= SEQUENCE {
  426. * p INTEGER,
  427. * q INTEGER,
  428. * g INTEGER
  429. * }
  430. * ```
  431. * - For Diffie-Hellman key exchange keys
  432. * (#PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or
  433. * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM)), the
  434. * `DomainParameters` format as defined by RFC 3279 §2.3.3.
  435. * ```
  436. * DomainParameters ::= SEQUENCE {
  437. * p INTEGER, -- odd prime, p=jq +1
  438. * g INTEGER, -- generator, g
  439. * q INTEGER, -- factor of p-1
  440. * j INTEGER OPTIONAL, -- subgroup factor
  441. * validationParams ValidationParams OPTIONAL
  442. * }
  443. * ValidationParams ::= SEQUENCE {
  444. * seed BIT STRING,
  445. * pgenCounter INTEGER
  446. * }
  447. * ```
  448. *
  449. * \note This function may allocate memory or other resources.
  450. * Once you have called this function on an attribute structure,
  451. * you must call psa_reset_key_attributes() to free these resources.
  452. *
  453. * \note This is an experimental extension to the interface. It may change
  454. * in future versions of the library.
  455. *
  456. * \param[in,out] attributes Attribute structure where the specified domain
  457. * parameters will be stored.
  458. * If this function fails, the content of
  459. * \p attributes is not modified.
  460. * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
  461. * \param[in] data Buffer containing the key domain parameters.
  462. * The content of this buffer is interpreted
  463. * according to \p type as described above.
  464. * \param data_length Size of the \p data buffer in bytes.
  465. *
  466. * \retval #PSA_SUCCESS \emptydescription
  467. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  468. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  469. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  470. */
  471. psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
  472. psa_key_type_t type,
  473. const uint8_t *data,
  474. size_t data_length);
  475. /**
  476. * \brief Get domain parameters for a key.
  477. *
  478. * Get the domain parameters for a key with this function, if any. The format
  479. * of the domain parameters written to \p data is specified in the
  480. * documentation for psa_set_key_domain_parameters().
  481. *
  482. * \note This is an experimental extension to the interface. It may change
  483. * in future versions of the library.
  484. *
  485. * \param[in] attributes The key attribute structure to query.
  486. * \param[out] data On success, the key domain parameters.
  487. * \param data_size Size of the \p data buffer in bytes.
  488. * The buffer is guaranteed to be large
  489. * enough if its size in bytes is at least
  490. * the value given by
  491. * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
  492. * \param[out] data_length On success, the number of bytes
  493. * that make up the key domain parameters data.
  494. *
  495. * \retval #PSA_SUCCESS \emptydescription
  496. * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
  497. */
  498. psa_status_t psa_get_key_domain_parameters(
  499. const psa_key_attributes_t *attributes,
  500. uint8_t *data,
  501. size_t data_size,
  502. size_t *data_length);
  503. /** Safe output buffer size for psa_get_key_domain_parameters().
  504. *
  505. * This macro returns a compile-time constant if its arguments are
  506. * compile-time constants.
  507. *
  508. * \warning This function may call its arguments multiple times or
  509. * zero times, so you should not pass arguments that contain
  510. * side effects.
  511. *
  512. * \note This is an experimental extension to the interface. It may change
  513. * in future versions of the library.
  514. *
  515. * \param key_type A supported key type.
  516. * \param key_bits The size of the key in bits.
  517. *
  518. * \return If the parameters are valid and supported, return
  519. * a buffer size in bytes that guarantees that
  520. * psa_get_key_domain_parameters() will not fail with
  521. * #PSA_ERROR_BUFFER_TOO_SMALL.
  522. * If the parameters are a valid combination that is not supported
  523. * by the implementation, this macro shall return either a
  524. * sensible size or 0.
  525. * If the parameters are not valid, the
  526. * return value is unspecified.
  527. */
  528. #define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \
  529. (PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) : \
  530. PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
  531. PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
  532. 0)
  533. #define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
  534. (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/)
  535. #define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
  536. (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/)
  537. /**@}*/
  538. /** \defgroup psa_tls_helpers TLS helper functions
  539. * @{
  540. */
  541. #if defined(MBEDTLS_ECP_C)
  542. #include <mbedtls/ecp.h>
  543. /** Convert an ECC curve identifier from the Mbed TLS encoding to PSA.
  544. *
  545. * \note This function is provided solely for the convenience of
  546. * Mbed TLS and may be removed at any time without notice.
  547. *
  548. * \param grpid An Mbed TLS elliptic curve identifier
  549. * (`MBEDTLS_ECP_DP_xxx`).
  550. * \param[out] bits On success, the bit size of the curve.
  551. *
  552. * \return The corresponding PSA elliptic curve identifier
  553. * (`PSA_ECC_FAMILY_xxx`).
  554. * \return \c 0 on failure (\p grpid is not recognized).
  555. */
  556. static inline psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
  557. size_t *bits)
  558. {
  559. switch (grpid) {
  560. case MBEDTLS_ECP_DP_SECP192R1:
  561. *bits = 192;
  562. return PSA_ECC_FAMILY_SECP_R1;
  563. case MBEDTLS_ECP_DP_SECP224R1:
  564. *bits = 224;
  565. return PSA_ECC_FAMILY_SECP_R1;
  566. case MBEDTLS_ECP_DP_SECP256R1:
  567. *bits = 256;
  568. return PSA_ECC_FAMILY_SECP_R1;
  569. case MBEDTLS_ECP_DP_SECP384R1:
  570. *bits = 384;
  571. return PSA_ECC_FAMILY_SECP_R1;
  572. case MBEDTLS_ECP_DP_SECP521R1:
  573. *bits = 521;
  574. return PSA_ECC_FAMILY_SECP_R1;
  575. case MBEDTLS_ECP_DP_BP256R1:
  576. *bits = 256;
  577. return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
  578. case MBEDTLS_ECP_DP_BP384R1:
  579. *bits = 384;
  580. return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
  581. case MBEDTLS_ECP_DP_BP512R1:
  582. *bits = 512;
  583. return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
  584. case MBEDTLS_ECP_DP_CURVE25519:
  585. *bits = 255;
  586. return PSA_ECC_FAMILY_MONTGOMERY;
  587. case MBEDTLS_ECP_DP_SECP192K1:
  588. *bits = 192;
  589. return PSA_ECC_FAMILY_SECP_K1;
  590. case MBEDTLS_ECP_DP_SECP224K1:
  591. *bits = 224;
  592. return PSA_ECC_FAMILY_SECP_K1;
  593. case MBEDTLS_ECP_DP_SECP256K1:
  594. *bits = 256;
  595. return PSA_ECC_FAMILY_SECP_K1;
  596. case MBEDTLS_ECP_DP_CURVE448:
  597. *bits = 448;
  598. return PSA_ECC_FAMILY_MONTGOMERY;
  599. default:
  600. *bits = 0;
  601. return 0;
  602. }
  603. }
  604. /** Convert an ECC curve identifier from the PSA encoding to Mbed TLS.
  605. *
  606. * \note This function is provided solely for the convenience of
  607. * Mbed TLS and may be removed at any time without notice.
  608. *
  609. * \param curve A PSA elliptic curve identifier
  610. * (`PSA_ECC_FAMILY_xxx`).
  611. * \param bits The bit-length of a private key on \p curve.
  612. * \param bits_is_sloppy If true, \p bits may be the bit-length rounded up
  613. * to the nearest multiple of 8. This allows the caller
  614. * to infer the exact curve from the length of a key
  615. * which is supplied as a byte string.
  616. *
  617. * \return The corresponding Mbed TLS elliptic curve identifier
  618. * (`MBEDTLS_ECP_DP_xxx`).
  619. * \return #MBEDTLS_ECP_DP_NONE if \c curve is not recognized.
  620. * \return #MBEDTLS_ECP_DP_NONE if \p bits is not
  621. * correct for \p curve.
  622. */
  623. mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
  624. size_t bits,
  625. int bits_is_sloppy);
  626. #endif /* MBEDTLS_ECP_C */
  627. /**@}*/
  628. /** \defgroup psa_external_rng External random generator
  629. * @{
  630. */
  631. #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
  632. /** External random generator function, implemented by the platform.
  633. *
  634. * When the compile-time option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled,
  635. * this function replaces Mbed TLS's entropy and DRBG modules for all
  636. * random generation triggered via PSA crypto interfaces.
  637. *
  638. * \note This random generator must deliver random numbers with cryptographic
  639. * quality and high performance. It must supply unpredictable numbers
  640. * with a uniform distribution. The implementation of this function
  641. * is responsible for ensuring that the random generator is seeded
  642. * with sufficient entropy. If you have a hardware TRNG which is slow
  643. * or delivers non-uniform output, declare it as an entropy source
  644. * with mbedtls_entropy_add_source() instead of enabling this option.
  645. *
  646. * \param[in,out] context Pointer to the random generator context.
  647. * This is all-bits-zero on the first call
  648. * and preserved between successive calls.
  649. * \param[out] output Output buffer. On success, this buffer
  650. * contains random data with a uniform
  651. * distribution.
  652. * \param output_size The size of the \p output buffer in bytes.
  653. * \param[out] output_length On success, set this value to \p output_size.
  654. *
  655. * \retval #PSA_SUCCESS
  656. * Success. The output buffer contains \p output_size bytes of
  657. * cryptographic-quality random data, and \c *output_length is
  658. * set to \p output_size.
  659. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
  660. * The random generator requires extra entropy and there is no
  661. * way to obtain entropy under current environment conditions.
  662. * This error should not happen under normal circumstances since
  663. * this function is responsible for obtaining as much entropy as
  664. * it needs. However implementations of this function may return
  665. * #PSA_ERROR_INSUFFICIENT_ENTROPY if there is no way to obtain
  666. * entropy without blocking indefinitely.
  667. * \retval #PSA_ERROR_HARDWARE_FAILURE
  668. * A failure of the random generator hardware that isn't covered
  669. * by #PSA_ERROR_INSUFFICIENT_ENTROPY.
  670. */
  671. psa_status_t mbedtls_psa_external_get_random(
  672. mbedtls_psa_external_random_context_t *context,
  673. uint8_t *output, size_t output_size, size_t *output_length);
  674. #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  675. /**@}*/
  676. /** \defgroup psa_builtin_keys Built-in keys
  677. * @{
  678. */
  679. /** The minimum value for a key identifier that is built into the
  680. * implementation.
  681. *
  682. * The range of key identifiers from #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN
  683. * to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX within the range from
  684. * #PSA_KEY_ID_VENDOR_MIN and #PSA_KEY_ID_VENDOR_MAX and must not intersect
  685. * with any other set of implementation-chosen key identifiers.
  686. *
  687. * This value is part of the library's ABI since changing it would invalidate
  688. * the values of built-in key identifiers in applications.
  689. */
  690. #define MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ((psa_key_id_t) 0x7fff0000)
  691. /** The maximum value for a key identifier that is built into the
  692. * implementation.
  693. *
  694. * See #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN for more information.
  695. */
  696. #define MBEDTLS_PSA_KEY_ID_BUILTIN_MAX ((psa_key_id_t) 0x7fffefff)
  697. /** A slot number identifying a key in a driver.
  698. *
  699. * Values of this type are used to identify built-in keys.
  700. */
  701. typedef uint64_t psa_drv_slot_number_t;
  702. #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
  703. /** Test whether a key identifier belongs to the builtin key range.
  704. *
  705. * \param key_id Key identifier to test.
  706. *
  707. * \retval 1
  708. * The key identifier is a builtin key identifier.
  709. * \retval 0
  710. * The key identifier is not a builtin key identifier.
  711. */
  712. static inline int psa_key_id_is_builtin(psa_key_id_t key_id)
  713. {
  714. return (key_id >= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN) &&
  715. (key_id <= MBEDTLS_PSA_KEY_ID_BUILTIN_MAX);
  716. }
  717. /** Platform function to obtain the location and slot number of a built-in key.
  718. *
  719. * An application-specific implementation of this function must be provided if
  720. * #MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS is enabled. This would typically be provided
  721. * as part of a platform's system image.
  722. *
  723. * #MBEDTLS_SVC_KEY_ID_GET_KEY_ID(\p key_id) needs to be in the range from
  724. * #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX.
  725. *
  726. * In a multi-application configuration
  727. * (\c MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER is defined),
  728. * this function should check that #MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(\p key_id)
  729. * is allowed to use the given key.
  730. *
  731. * \param key_id The key ID for which to retrieve the
  732. * location and slot attributes.
  733. * \param[out] lifetime On success, the lifetime associated with the key
  734. * corresponding to \p key_id. Lifetime is a
  735. * combination of which driver contains the key,
  736. * and with what persistence level the key is
  737. * intended to be used. If the platform
  738. * implementation does not contain specific
  739. * information about the intended key persistence
  740. * level, the persistence level may be reported as
  741. * #PSA_KEY_PERSISTENCE_DEFAULT.
  742. * \param[out] slot_number On success, the slot number known to the driver
  743. * registered at the lifetime location reported
  744. * through \p lifetime which corresponds to the
  745. * requested built-in key.
  746. *
  747. * \retval #PSA_SUCCESS
  748. * The requested key identifier designates a built-in key.
  749. * In a multi-application configuration, the requested owner
  750. * is allowed to access it.
  751. * \retval #PSA_ERROR_DOES_NOT_EXIST
  752. * The requested key identifier is not a built-in key which is known
  753. * to this function. If a key exists in the key storage with this
  754. * identifier, the data from the storage will be used.
  755. * \return (any other error)
  756. * Any other error is propagated to the function that requested the key.
  757. * Common errors include:
  758. * - #PSA_ERROR_NOT_PERMITTED: the key exists but the requested owner
  759. * is not allowed to access it.
  760. */
  761. psa_status_t mbedtls_psa_platform_get_builtin_key(
  762. mbedtls_svc_key_id_t key_id,
  763. psa_key_lifetime_t *lifetime,
  764. psa_drv_slot_number_t *slot_number);
  765. #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
  766. /** @} */
  767. /** \addtogroup crypto_types
  768. * @{
  769. */
  770. #define PSA_ALG_CATEGORY_PAKE ((psa_algorithm_t) 0x0a000000)
  771. /** Whether the specified algorithm is a password-authenticated key exchange.
  772. *
  773. * \param alg An algorithm identifier (value of type #psa_algorithm_t).
  774. *
  775. * \return 1 if \p alg is a password-authenticated key exchange (PAKE)
  776. * algorithm, 0 otherwise.
  777. * This macro may return either 0 or 1 if \p alg is not a supported
  778. * algorithm identifier.
  779. */
  780. #define PSA_ALG_IS_PAKE(alg) \
  781. (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_PAKE)
  782. /** The Password-authenticated key exchange by juggling (J-PAKE) algorithm.
  783. *
  784. * This is J-PAKE as defined by RFC 8236, instantiated with the following
  785. * parameters:
  786. *
  787. * - The group can be either an elliptic curve or defined over a finite field.
  788. * - Schnorr NIZK proof as defined by RFC 8235 and using the same group as the
  789. * J-PAKE algorithm.
  790. * - A cryptographic hash function.
  791. *
  792. * To select these parameters and set up the cipher suite, call these functions
  793. * in any order:
  794. *
  795. * \code
  796. * psa_pake_cs_set_algorithm(cipher_suite, PSA_ALG_JPAKE);
  797. * psa_pake_cs_set_primitive(cipher_suite,
  798. * PSA_PAKE_PRIMITIVE(type, family, bits));
  799. * psa_pake_cs_set_hash(cipher_suite, hash);
  800. * \endcode
  801. *
  802. * For more information on how to set a specific curve or field, refer to the
  803. * documentation of the individual \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
  804. *
  805. * After initializing a J-PAKE operation, call
  806. *
  807. * \code
  808. * psa_pake_setup(operation, cipher_suite);
  809. * psa_pake_set_user(operation, ...);
  810. * psa_pake_set_peer(operation, ...);
  811. * psa_pake_set_password_key(operation, ...);
  812. * \endcode
  813. *
  814. * The password is provided as a key. This can be the password text itself,
  815. * in an agreed character encoding, or some value derived from the password
  816. * as required by a higher level protocol.
  817. *
  818. * (The implementation converts the key material to a number as described in
  819. * Section 2.3.8 of _SEC 1: Elliptic Curve Cryptography_
  820. * (https://www.secg.org/sec1-v2.pdf), before reducing it modulo \c q. Here
  821. * \c q is order of the group defined by the primitive set in the cipher suite.
  822. * The \c psa_pake_set_password_key() function returns an error if the result
  823. * of the reduction is 0.)
  824. *
  825. * The key exchange flow for J-PAKE is as follows:
  826. * -# To get the first round data that needs to be sent to the peer, call
  827. * \code
  828. * // Get g1
  829. * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
  830. * // Get the ZKP public key for x1
  831. * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
  832. * // Get the ZKP proof for x1
  833. * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
  834. * // Get g2
  835. * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
  836. * // Get the ZKP public key for x2
  837. * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
  838. * // Get the ZKP proof for x2
  839. * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
  840. * \endcode
  841. * -# To provide the first round data received from the peer to the operation,
  842. * call
  843. * \code
  844. * // Set g3
  845. * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
  846. * // Set the ZKP public key for x3
  847. * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
  848. * // Set the ZKP proof for x3
  849. * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
  850. * // Set g4
  851. * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
  852. * // Set the ZKP public key for x4
  853. * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
  854. * // Set the ZKP proof for x4
  855. * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
  856. * \endcode
  857. * -# To get the second round data that needs to be sent to the peer, call
  858. * \code
  859. * // Get A
  860. * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
  861. * // Get ZKP public key for x2*s
  862. * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
  863. * // Get ZKP proof for x2*s
  864. * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
  865. * \endcode
  866. * -# To provide the second round data received from the peer to the operation,
  867. * call
  868. * \code
  869. * // Set B
  870. * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
  871. * // Set ZKP public key for x4*s
  872. * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
  873. * // Set ZKP proof for x4*s
  874. * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
  875. * \endcode
  876. * -# To access the shared secret call
  877. * \code
  878. * // Get Ka=Kb=K
  879. * psa_pake_get_implicit_key()
  880. * \endcode
  881. *
  882. * For more information consult the documentation of the individual
  883. * \c PSA_PAKE_STEP_XXX constants.
  884. *
  885. * At this point there is a cryptographic guarantee that only the authenticated
  886. * party who used the same password is able to compute the key. But there is no
  887. * guarantee that the peer is the party it claims to be and was able to do so.
  888. *
  889. * That is, the authentication is only implicit (the peer is not authenticated
  890. * at this point, and no action should be taken that assume that they are - like
  891. * for example accessing restricted files).
  892. *
  893. * To make the authentication explicit there are various methods, see Section 5
  894. * of RFC 8236 for two examples.
  895. *
  896. */
  897. #define PSA_ALG_JPAKE ((psa_algorithm_t) 0x0a000100)
  898. /** @} */
  899. /** \defgroup pake Password-authenticated key exchange (PAKE)
  900. *
  901. * This is a proposed PAKE interface for the PSA Crypto API. It is not part of
  902. * the official PSA Crypto API yet.
  903. *
  904. * \note The content of this section is not part of the stable API and ABI
  905. * of Mbed Crypto and may change arbitrarily from version to version.
  906. * Same holds for the corresponding macros #PSA_ALG_CATEGORY_PAKE and
  907. * #PSA_ALG_JPAKE.
  908. * @{
  909. */
  910. /** \brief Encoding of the application role of PAKE
  911. *
  912. * Encodes the application's role in the algorithm is being executed. For more
  913. * information see the documentation of individual \c PSA_PAKE_ROLE_XXX
  914. * constants.
  915. */
  916. typedef uint8_t psa_pake_role_t;
  917. /** Encoding of input and output indicators for PAKE.
  918. *
  919. * Some PAKE algorithms need to exchange more data than just a single key share.
  920. * This type is for encoding additional input and output data for such
  921. * algorithms.
  922. */
  923. typedef uint8_t psa_pake_step_t;
  924. /** Encoding of the type of the PAKE's primitive.
  925. *
  926. * Values defined by this standard will never be in the range 0x80-0xff.
  927. * Vendors who define additional types must use an encoding in this range.
  928. *
  929. * For more information see the documentation of individual
  930. * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
  931. */
  932. typedef uint8_t psa_pake_primitive_type_t;
  933. /** \brief Encoding of the family of the primitive associated with the PAKE.
  934. *
  935. * For more information see the documentation of individual
  936. * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
  937. */
  938. typedef uint8_t psa_pake_family_t;
  939. /** \brief Encoding of the primitive associated with the PAKE.
  940. *
  941. * For more information see the documentation of the #PSA_PAKE_PRIMITIVE macro.
  942. */
  943. typedef uint32_t psa_pake_primitive_t;
  944. /** A value to indicate no role in a PAKE algorithm.
  945. * This value can be used in a call to psa_pake_set_role() for symmetric PAKE
  946. * algorithms which do not assign roles.
  947. */
  948. #define PSA_PAKE_ROLE_NONE ((psa_pake_role_t) 0x00)
  949. /** The first peer in a balanced PAKE.
  950. *
  951. * Although balanced PAKE algorithms are symmetric, some of them needs an
  952. * ordering of peers for the transcript calculations. If the algorithm does not
  953. * need this, both #PSA_PAKE_ROLE_FIRST and #PSA_PAKE_ROLE_SECOND are
  954. * accepted.
  955. */
  956. #define PSA_PAKE_ROLE_FIRST ((psa_pake_role_t) 0x01)
  957. /** The second peer in a balanced PAKE.
  958. *
  959. * Although balanced PAKE algorithms are symmetric, some of them needs an
  960. * ordering of peers for the transcript calculations. If the algorithm does not
  961. * need this, either #PSA_PAKE_ROLE_FIRST or #PSA_PAKE_ROLE_SECOND are
  962. * accepted.
  963. */
  964. #define PSA_PAKE_ROLE_SECOND ((psa_pake_role_t) 0x02)
  965. /** The client in an augmented PAKE.
  966. *
  967. * Augmented PAKE algorithms need to differentiate between client and server.
  968. */
  969. #define PSA_PAKE_ROLE_CLIENT ((psa_pake_role_t) 0x11)
  970. /** The server in an augmented PAKE.
  971. *
  972. * Augmented PAKE algorithms need to differentiate between client and server.
  973. */
  974. #define PSA_PAKE_ROLE_SERVER ((psa_pake_role_t) 0x12)
  975. /** The PAKE primitive type indicating the use of elliptic curves.
  976. *
  977. * The values of the \c family and \c bits fields of the cipher suite identify a
  978. * specific elliptic curve, using the same mapping that is used for ECC
  979. * (::psa_ecc_family_t) keys.
  980. *
  981. * (Here \c family means the value returned by psa_pake_cs_get_family() and
  982. * \c bits means the value returned by psa_pake_cs_get_bits().)
  983. *
  984. * Input and output during the operation can involve group elements and scalar
  985. * values:
  986. * -# The format for group elements is the same as for public keys on the
  987. * specific curve would be. For more information, consult the documentation of
  988. * psa_export_public_key().
  989. * -# The format for scalars is the same as for private keys on the specific
  990. * curve would be. For more information, consult the documentation of
  991. * psa_export_key().
  992. */
  993. #define PSA_PAKE_PRIMITIVE_TYPE_ECC ((psa_pake_primitive_type_t) 0x01)
  994. /** The PAKE primitive type indicating the use of Diffie-Hellman groups.
  995. *
  996. * The values of the \c family and \c bits fields of the cipher suite identify
  997. * a specific Diffie-Hellman group, using the same mapping that is used for
  998. * Diffie-Hellman (::psa_dh_family_t) keys.
  999. *
  1000. * (Here \c family means the value returned by psa_pake_cs_get_family() and
  1001. * \c bits means the value returned by psa_pake_cs_get_bits().)
  1002. *
  1003. * Input and output during the operation can involve group elements and scalar
  1004. * values:
  1005. * -# The format for group elements is the same as for public keys on the
  1006. * specific group would be. For more information, consult the documentation of
  1007. * psa_export_public_key().
  1008. * -# The format for scalars is the same as for private keys on the specific
  1009. * group would be. For more information, consult the documentation of
  1010. * psa_export_key().
  1011. */
  1012. #define PSA_PAKE_PRIMITIVE_TYPE_DH ((psa_pake_primitive_type_t) 0x02)
  1013. /** Construct a PAKE primitive from type, family and bit-size.
  1014. *
  1015. * \param pake_type The type of the primitive
  1016. * (value of type ::psa_pake_primitive_type_t).
  1017. * \param pake_family The family of the primitive
  1018. * (the type and interpretation of this parameter depends
  1019. * on \p type, for more information consult the
  1020. * documentation of individual ::psa_pake_primitive_type_t
  1021. * constants).
  1022. * \param pake_bits The bit-size of the primitive
  1023. * (Value of type \c size_t. The interpretation
  1024. * of this parameter depends on \p family, for more
  1025. * information consult the documentation of individual
  1026. * ::psa_pake_primitive_type_t constants).
  1027. *
  1028. * \return The constructed primitive value of type ::psa_pake_primitive_t.
  1029. * Return 0 if the requested primitive can't be encoded as
  1030. * ::psa_pake_primitive_t.
  1031. */
  1032. #define PSA_PAKE_PRIMITIVE(pake_type, pake_family, pake_bits) \
  1033. ((pake_bits & 0xFFFF) != pake_bits) ? 0 : \
  1034. ((psa_pake_primitive_t) (((pake_type) << 24 | \
  1035. (pake_family) << 16) | (pake_bits)))
  1036. /** The key share being sent to or received from the peer.
  1037. *
  1038. * The format for both input and output at this step is the same as for public
  1039. * keys on the group determined by the primitive (::psa_pake_primitive_t) would
  1040. * be.
  1041. *
  1042. * For more information on the format, consult the documentation of
  1043. * psa_export_public_key().
  1044. *
  1045. * For information regarding how the group is determined, consult the
  1046. * documentation #PSA_PAKE_PRIMITIVE.
  1047. */
  1048. #define PSA_PAKE_STEP_KEY_SHARE ((psa_pake_step_t) 0x01)
  1049. /** A Schnorr NIZKP public key.
  1050. *
  1051. * This is the ephemeral public key in the Schnorr Non-Interactive
  1052. * Zero-Knowledge Proof (the value denoted by the letter 'V' in RFC 8235).
  1053. *
  1054. * The format for both input and output at this step is the same as for public
  1055. * keys on the group determined by the primitive (::psa_pake_primitive_t) would
  1056. * be.
  1057. *
  1058. * For more information on the format, consult the documentation of
  1059. * psa_export_public_key().
  1060. *
  1061. * For information regarding how the group is determined, consult the
  1062. * documentation #PSA_PAKE_PRIMITIVE.
  1063. */
  1064. #define PSA_PAKE_STEP_ZK_PUBLIC ((psa_pake_step_t) 0x02)
  1065. /** A Schnorr NIZKP proof.
  1066. *
  1067. * This is the proof in the Schnorr Non-Interactive Zero-Knowledge Proof (the
  1068. * value denoted by the letter 'r' in RFC 8235).
  1069. *
  1070. * Both for input and output, the value at this step is an integer less than
  1071. * the order of the group selected in the cipher suite. The format depends on
  1072. * the group as well:
  1073. *
  1074. * - For Montgomery curves, the encoding is little endian.
  1075. * - For everything else the encoding is big endian (see Section 2.3.8 of
  1076. * _SEC 1: Elliptic Curve Cryptography_ at https://www.secg.org/sec1-v2.pdf).
  1077. *
  1078. * In both cases leading zeroes are allowed as long as the length in bytes does
  1079. * not exceed the byte length of the group order.
  1080. *
  1081. * For information regarding how the group is determined, consult the
  1082. * documentation #PSA_PAKE_PRIMITIVE.
  1083. */
  1084. #define PSA_PAKE_STEP_ZK_PROOF ((psa_pake_step_t) 0x03)
  1085. /** The type of the data structure for PAKE cipher suites.
  1086. *
  1087. * This is an implementation-defined \c struct. Applications should not
  1088. * make any assumptions about the content of this structure.
  1089. * Implementation details can change in future versions without notice.
  1090. */
  1091. typedef struct psa_pake_cipher_suite_s psa_pake_cipher_suite_t;
  1092. /** Return an initial value for a PAKE cipher suite object.
  1093. */
  1094. static psa_pake_cipher_suite_t psa_pake_cipher_suite_init(void);
  1095. /** Retrieve the PAKE algorithm from a PAKE cipher suite.
  1096. *
  1097. * \param[in] cipher_suite The cipher suite structure to query.
  1098. *
  1099. * \return The PAKE algorithm stored in the cipher suite structure.
  1100. */
  1101. static psa_algorithm_t psa_pake_cs_get_algorithm(
  1102. const psa_pake_cipher_suite_t *cipher_suite);
  1103. /** Declare the PAKE algorithm for the cipher suite.
  1104. *
  1105. * This function overwrites any PAKE algorithm
  1106. * previously set in \p cipher_suite.
  1107. *
  1108. * \param[out] cipher_suite The cipher suite structure to write to.
  1109. * \param algorithm The PAKE algorithm to write.
  1110. * (`PSA_ALG_XXX` values of type ::psa_algorithm_t
  1111. * such that #PSA_ALG_IS_PAKE(\c alg) is true.)
  1112. * If this is 0, the PAKE algorithm in
  1113. * \p cipher_suite becomes unspecified.
  1114. */
  1115. static void psa_pake_cs_set_algorithm(psa_pake_cipher_suite_t *cipher_suite,
  1116. psa_algorithm_t algorithm);
  1117. /** Retrieve the primitive from a PAKE cipher suite.
  1118. *
  1119. * \param[in] cipher_suite The cipher suite structure to query.
  1120. *
  1121. * \return The primitive stored in the cipher suite structure.
  1122. */
  1123. static psa_pake_primitive_t psa_pake_cs_get_primitive(
  1124. const psa_pake_cipher_suite_t *cipher_suite);
  1125. /** Declare the primitive for a PAKE cipher suite.
  1126. *
  1127. * This function overwrites any primitive previously set in \p cipher_suite.
  1128. *
  1129. * \param[out] cipher_suite The cipher suite structure to write to.
  1130. * \param primitive The primitive to write. If this is 0, the
  1131. * primitive type in \p cipher_suite becomes
  1132. * unspecified.
  1133. */
  1134. static void psa_pake_cs_set_primitive(psa_pake_cipher_suite_t *cipher_suite,
  1135. psa_pake_primitive_t primitive);
  1136. /** Retrieve the PAKE family from a PAKE cipher suite.
  1137. *
  1138. * \param[in] cipher_suite The cipher suite structure to query.
  1139. *
  1140. * \return The PAKE family stored in the cipher suite structure.
  1141. */
  1142. static psa_pake_family_t psa_pake_cs_get_family(
  1143. const psa_pake_cipher_suite_t *cipher_suite);
  1144. /** Retrieve the PAKE primitive bit-size from a PAKE cipher suite.
  1145. *
  1146. * \param[in] cipher_suite The cipher suite structure to query.
  1147. *
  1148. * \return The PAKE primitive bit-size stored in the cipher suite structure.
  1149. */
  1150. static uint16_t psa_pake_cs_get_bits(
  1151. const psa_pake_cipher_suite_t *cipher_suite);
  1152. /** Retrieve the hash algorithm from a PAKE cipher suite.
  1153. *
  1154. * \param[in] cipher_suite The cipher suite structure to query.
  1155. *
  1156. * \return The hash algorithm stored in the cipher suite structure. The return
  1157. * value is 0 if the PAKE is not parametrised by a hash algorithm or if
  1158. * the hash algorithm is not set.
  1159. */
  1160. static psa_algorithm_t psa_pake_cs_get_hash(
  1161. const psa_pake_cipher_suite_t *cipher_suite);
  1162. /** Declare the hash algorithm for a PAKE cipher suite.
  1163. *
  1164. * This function overwrites any hash algorithm
  1165. * previously set in \p cipher_suite.
  1166. *
  1167. * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
  1168. * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
  1169. * for more information.
  1170. *
  1171. * \param[out] cipher_suite The cipher suite structure to write to.
  1172. * \param hash The hash involved in the cipher suite.
  1173. * (`PSA_ALG_XXX` values of type ::psa_algorithm_t
  1174. * such that #PSA_ALG_IS_HASH(\c alg) is true.)
  1175. * If this is 0, the hash algorithm in
  1176. * \p cipher_suite becomes unspecified.
  1177. */
  1178. static void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
  1179. psa_algorithm_t hash);
  1180. /** The type of the state data structure for PAKE operations.
  1181. *
  1182. * Before calling any function on a PAKE operation object, the application
  1183. * must initialize it by any of the following means:
  1184. * - Set the structure to all-bits-zero, for example:
  1185. * \code
  1186. * psa_pake_operation_t operation;
  1187. * memset(&operation, 0, sizeof(operation));
  1188. * \endcode
  1189. * - Initialize the structure to logical zero values, for example:
  1190. * \code
  1191. * psa_pake_operation_t operation = {0};
  1192. * \endcode
  1193. * - Initialize the structure to the initializer #PSA_PAKE_OPERATION_INIT,
  1194. * for example:
  1195. * \code
  1196. * psa_pake_operation_t operation = PSA_PAKE_OPERATION_INIT;
  1197. * \endcode
  1198. * - Assign the result of the function psa_pake_operation_init()
  1199. * to the structure, for example:
  1200. * \code
  1201. * psa_pake_operation_t operation;
  1202. * operation = psa_pake_operation_init();
  1203. * \endcode
  1204. *
  1205. * This is an implementation-defined \c struct. Applications should not
  1206. * make any assumptions about the content of this structure.
  1207. * Implementation details can change in future versions without notice. */
  1208. typedef struct psa_pake_operation_s psa_pake_operation_t;
  1209. /** The type of input values for PAKE operations. */
  1210. typedef struct psa_crypto_driver_pake_inputs_s psa_crypto_driver_pake_inputs_t;
  1211. /** The type of computation stage for J-PAKE operations. */
  1212. typedef struct psa_jpake_computation_stage_s psa_jpake_computation_stage_t;
  1213. /** Return an initial value for a PAKE operation object.
  1214. */
  1215. static psa_pake_operation_t psa_pake_operation_init(void);
  1216. /** Get the length of the password in bytes from given inputs.
  1217. *
  1218. * \param[in] inputs Operation inputs.
  1219. * \param[out] password_len Password length.
  1220. *
  1221. * \retval #PSA_SUCCESS
  1222. * Success.
  1223. * \retval #PSA_ERROR_BAD_STATE
  1224. * Password hasn't been set yet.
  1225. */
  1226. psa_status_t psa_crypto_driver_pake_get_password_len(
  1227. const psa_crypto_driver_pake_inputs_t *inputs,
  1228. size_t *password_len);
  1229. /** Get the password from given inputs.
  1230. *
  1231. * \param[in] inputs Operation inputs.
  1232. * \param[out] buffer Return buffer for password.
  1233. * \param buffer_size Size of the return buffer in bytes.
  1234. * \param[out] buffer_length Actual size of the password in bytes.
  1235. *
  1236. * \retval #PSA_SUCCESS
  1237. * Success.
  1238. * \retval #PSA_ERROR_BAD_STATE
  1239. * Password hasn't been set yet.
  1240. */
  1241. psa_status_t psa_crypto_driver_pake_get_password(
  1242. const psa_crypto_driver_pake_inputs_t *inputs,
  1243. uint8_t *buffer, size_t buffer_size, size_t *buffer_length);
  1244. /** Get the role from given inputs.
  1245. *
  1246. * \param[in] inputs Operation inputs.
  1247. * \param[out] role Return buffer for role.
  1248. *
  1249. * \retval #PSA_SUCCESS
  1250. * Success.
  1251. * \retval #PSA_ERROR_BAD_STATE
  1252. * Role hasn't been set yet.
  1253. */
  1254. psa_status_t psa_crypto_driver_pake_get_role(
  1255. const psa_crypto_driver_pake_inputs_t *inputs,
  1256. psa_pake_role_t *role);
  1257. /** Get the length of the user id in bytes from given inputs.
  1258. *
  1259. * \param[in] inputs Operation inputs.
  1260. * \param[out] user_len User id length.
  1261. *
  1262. * \retval #PSA_SUCCESS
  1263. * Success.
  1264. * \retval #PSA_ERROR_BAD_STATE
  1265. * User id hasn't been set yet.
  1266. */
  1267. psa_status_t psa_crypto_driver_pake_get_user_len(
  1268. const psa_crypto_driver_pake_inputs_t *inputs,
  1269. size_t *user_len);
  1270. /** Get the length of the peer id in bytes from given inputs.
  1271. *
  1272. * \param[in] inputs Operation inputs.
  1273. * \param[out] peer_len Peer id length.
  1274. *
  1275. * \retval #PSA_SUCCESS
  1276. * Success.
  1277. * \retval #PSA_ERROR_BAD_STATE
  1278. * Peer id hasn't been set yet.
  1279. */
  1280. psa_status_t psa_crypto_driver_pake_get_peer_len(
  1281. const psa_crypto_driver_pake_inputs_t *inputs,
  1282. size_t *peer_len);
  1283. /** Get the user id from given inputs.
  1284. *
  1285. * \param[in] inputs Operation inputs.
  1286. * \param[out] user_id User id.
  1287. * \param user_id_size Size of \p user_id in bytes.
  1288. * \param[out] user_id_len Size of the user id in bytes.
  1289. *
  1290. * \retval #PSA_SUCCESS
  1291. * Success.
  1292. * \retval #PSA_ERROR_BAD_STATE
  1293. * User id hasn't been set yet.
  1294. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  1295. * The size of the \p user_id is too small.
  1296. */
  1297. psa_status_t psa_crypto_driver_pake_get_user(
  1298. const psa_crypto_driver_pake_inputs_t *inputs,
  1299. uint8_t *user_id, size_t user_id_size, size_t *user_id_len);
  1300. /** Get the peer id from given inputs.
  1301. *
  1302. * \param[in] inputs Operation inputs.
  1303. * \param[out] peer_id Peer id.
  1304. * \param peer_id_size Size of \p peer_id in bytes.
  1305. * \param[out] peer_id_length Size of the peer id in bytes.
  1306. *
  1307. * \retval #PSA_SUCCESS
  1308. * Success.
  1309. * \retval #PSA_ERROR_BAD_STATE
  1310. * Peer id hasn't been set yet.
  1311. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  1312. * The size of the \p peer_id is too small.
  1313. */
  1314. psa_status_t psa_crypto_driver_pake_get_peer(
  1315. const psa_crypto_driver_pake_inputs_t *inputs,
  1316. uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length);
  1317. /** Get the cipher suite from given inputs.
  1318. *
  1319. * \param[in] inputs Operation inputs.
  1320. * \param[out] cipher_suite Return buffer for role.
  1321. *
  1322. * \retval #PSA_SUCCESS
  1323. * Success.
  1324. * \retval #PSA_ERROR_BAD_STATE
  1325. * Cipher_suite hasn't been set yet.
  1326. */
  1327. psa_status_t psa_crypto_driver_pake_get_cipher_suite(
  1328. const psa_crypto_driver_pake_inputs_t *inputs,
  1329. psa_pake_cipher_suite_t *cipher_suite);
  1330. /** Set the session information for a password-authenticated key exchange.
  1331. *
  1332. * The sequence of operations to set up a password-authenticated key exchange
  1333. * is as follows:
  1334. * -# Allocate an operation object which will be passed to all the functions
  1335. * listed here.
  1336. * -# Initialize the operation object with one of the methods described in the
  1337. * documentation for #psa_pake_operation_t, e.g.
  1338. * #PSA_PAKE_OPERATION_INIT.
  1339. * -# Call psa_pake_setup() to specify the cipher suite.
  1340. * -# Call \c psa_pake_set_xxx() functions on the operation to complete the
  1341. * setup. The exact sequence of \c psa_pake_set_xxx() functions that needs
  1342. * to be called depends on the algorithm in use.
  1343. *
  1344. * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
  1345. * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
  1346. * for more information.
  1347. *
  1348. * A typical sequence of calls to perform a password-authenticated key
  1349. * exchange:
  1350. * -# Call psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to get the
  1351. * key share that needs to be sent to the peer.
  1352. * -# Call psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to provide
  1353. * the key share that was received from the peer.
  1354. * -# Depending on the algorithm additional calls to psa_pake_output() and
  1355. * psa_pake_input() might be necessary.
  1356. * -# Call psa_pake_get_implicit_key() for accessing the shared secret.
  1357. *
  1358. * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
  1359. * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
  1360. * for more information.
  1361. *
  1362. * If an error occurs at any step after a call to psa_pake_setup(),
  1363. * the operation will need to be reset by a call to psa_pake_abort(). The
  1364. * application may call psa_pake_abort() at any time after the operation
  1365. * has been initialized.
  1366. *
  1367. * After a successful call to psa_pake_setup(), the application must
  1368. * eventually terminate the operation. The following events terminate an
  1369. * operation:
  1370. * - A call to psa_pake_abort().
  1371. * - A successful call to psa_pake_get_implicit_key().
  1372. *
  1373. * \param[in,out] operation The operation object to set up. It must have
  1374. * been initialized but not set up yet.
  1375. * \param[in] cipher_suite The cipher suite to use. (A cipher suite fully
  1376. * characterizes a PAKE algorithm and determines
  1377. * the algorithm as well.)
  1378. *
  1379. * \retval #PSA_SUCCESS
  1380. * Success.
  1381. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1382. * The algorithm in \p cipher_suite is not a PAKE algorithm, or the
  1383. * PAKE primitive in \p cipher_suite is not compatible with the
  1384. * PAKE algorithm, or the hash algorithm in \p cipher_suite is invalid
  1385. * or not compatible with the PAKE algorithm and primitive.
  1386. * \retval #PSA_ERROR_NOT_SUPPORTED
  1387. * The algorithm in \p cipher_suite is not a supported PAKE algorithm,
  1388. * or the PAKE primitive in \p cipher_suite is not supported or not
  1389. * compatible with the PAKE algorithm, or the hash algorithm in
  1390. * \p cipher_suite is not supported or not compatible with the PAKE
  1391. * algorithm and primitive.
  1392. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1393. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1394. * \retval #PSA_ERROR_BAD_STATE
  1395. * The operation state is not valid, or
  1396. * the library has not been previously initialized by psa_crypto_init().
  1397. * It is implementation-dependent whether a failure to initialize
  1398. * results in this error code.
  1399. */
  1400. psa_status_t psa_pake_setup(psa_pake_operation_t *operation,
  1401. const psa_pake_cipher_suite_t *cipher_suite);
  1402. /** Set the password for a password-authenticated key exchange from key ID.
  1403. *
  1404. * Call this function when the password, or a value derived from the password,
  1405. * is already present in the key store.
  1406. *
  1407. * \param[in,out] operation The operation object to set the password for. It
  1408. * must have been set up by psa_pake_setup() and
  1409. * not yet in use (neither psa_pake_output() nor
  1410. * psa_pake_input() has been called yet). It must
  1411. * be on operation for which the password hasn't
  1412. * been set yet (psa_pake_set_password_key()
  1413. * hasn't been called yet).
  1414. * \param password Identifier of the key holding the password or a
  1415. * value derived from the password (eg. by a
  1416. * memory-hard function). It must remain valid
  1417. * until the operation terminates. It must be of
  1418. * type #PSA_KEY_TYPE_PASSWORD or
  1419. * #PSA_KEY_TYPE_PASSWORD_HASH. It has to allow
  1420. * the usage #PSA_KEY_USAGE_DERIVE.
  1421. *
  1422. * \retval #PSA_SUCCESS
  1423. * Success.
  1424. * \retval #PSA_ERROR_INVALID_HANDLE
  1425. * \p password is not a valid key identifier.
  1426. * \retval #PSA_ERROR_NOT_PERMITTED
  1427. * The key does not have the #PSA_KEY_USAGE_DERIVE flag, or it does not
  1428. * permit the \p operation's algorithm.
  1429. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1430. * The key type for \p password is not #PSA_KEY_TYPE_PASSWORD or
  1431. * #PSA_KEY_TYPE_PASSWORD_HASH, or \p password is not compatible with
  1432. * the \p operation's cipher suite.
  1433. * \retval #PSA_ERROR_NOT_SUPPORTED
  1434. * The key type or key size of \p password is not supported with the
  1435. * \p operation's cipher suite.
  1436. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1437. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1438. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  1439. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  1440. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  1441. * \retval #PSA_ERROR_BAD_STATE
  1442. * The operation state is not valid (it must have been set up.), or
  1443. * the library has not been previously initialized by psa_crypto_init().
  1444. * It is implementation-dependent whether a failure to initialize
  1445. * results in this error code.
  1446. */
  1447. psa_status_t psa_pake_set_password_key(psa_pake_operation_t *operation,
  1448. mbedtls_svc_key_id_t password);
  1449. /** Set the user ID for a password-authenticated key exchange.
  1450. *
  1451. * Call this function to set the user ID. For PAKE algorithms that associate a
  1452. * user identifier with each side of the session you need to call
  1453. * psa_pake_set_peer() as well. For PAKE algorithms that associate a single
  1454. * user identifier with the session, call psa_pake_set_user() only.
  1455. *
  1456. * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
  1457. * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
  1458. * for more information.
  1459. *
  1460. * \param[in,out] operation The operation object to set the user ID for. It
  1461. * must have been set up by psa_pake_setup() and
  1462. * not yet in use (neither psa_pake_output() nor
  1463. * psa_pake_input() has been called yet). It must
  1464. * be on operation for which the user ID hasn't
  1465. * been set (psa_pake_set_user() hasn't been
  1466. * called yet).
  1467. * \param[in] user_id The user ID to authenticate with.
  1468. * (temporary limitation: "client" or "server" only)
  1469. * \param user_id_len Size of the \p user_id buffer in bytes.
  1470. *
  1471. * \retval #PSA_SUCCESS
  1472. * Success.
  1473. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1474. * \p user_id is not valid for the \p operation's algorithm and cipher
  1475. * suite.
  1476. * \retval #PSA_ERROR_NOT_SUPPORTED
  1477. * The value of \p user_id is not supported by the implementation.
  1478. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1479. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1480. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1481. * \retval #PSA_ERROR_BAD_STATE
  1482. * The operation state is not valid, or
  1483. * the library has not been previously initialized by psa_crypto_init().
  1484. * It is implementation-dependent whether a failure to initialize
  1485. * results in this error code.
  1486. */
  1487. psa_status_t psa_pake_set_user(psa_pake_operation_t *operation,
  1488. const uint8_t *user_id,
  1489. size_t user_id_len);
  1490. /** Set the peer ID for a password-authenticated key exchange.
  1491. *
  1492. * Call this function in addition to psa_pake_set_user() for PAKE algorithms
  1493. * that associate a user identifier with each side of the session. For PAKE
  1494. * algorithms that associate a single user identifier with the session, call
  1495. * psa_pake_set_user() only.
  1496. *
  1497. * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
  1498. * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
  1499. * for more information.
  1500. *
  1501. * \param[in,out] operation The operation object to set the peer ID for. It
  1502. * must have been set up by psa_pake_setup() and
  1503. * not yet in use (neither psa_pake_output() nor
  1504. * psa_pake_input() has been called yet). It must
  1505. * be on operation for which the peer ID hasn't
  1506. * been set (psa_pake_set_peer() hasn't been
  1507. * called yet).
  1508. * \param[in] peer_id The peer's ID to authenticate.
  1509. * (temporary limitation: "client" or "server" only)
  1510. * \param peer_id_len Size of the \p peer_id buffer in bytes.
  1511. *
  1512. * \retval #PSA_SUCCESS
  1513. * Success.
  1514. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1515. * \p user_id is not valid for the \p operation's algorithm and cipher
  1516. * suite.
  1517. * \retval #PSA_ERROR_NOT_SUPPORTED
  1518. * The algorithm doesn't associate a second identity with the session.
  1519. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1520. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1521. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1522. * \retval #PSA_ERROR_BAD_STATE
  1523. * Calling psa_pake_set_peer() is invalid with the \p operation's
  1524. * algorithm, the operation state is not valid, or the library has not
  1525. * been previously initialized by psa_crypto_init().
  1526. * It is implementation-dependent whether a failure to initialize
  1527. * results in this error code.
  1528. */
  1529. psa_status_t psa_pake_set_peer(psa_pake_operation_t *operation,
  1530. const uint8_t *peer_id,
  1531. size_t peer_id_len);
  1532. /** Set the application role for a password-authenticated key exchange.
  1533. *
  1534. * Not all PAKE algorithms need to differentiate the communicating entities.
  1535. * It is optional to call this function for PAKEs that don't require a role
  1536. * to be specified. For such PAKEs the application role parameter is ignored,
  1537. * or #PSA_PAKE_ROLE_NONE can be passed as \c role.
  1538. *
  1539. * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
  1540. * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
  1541. * for more information.
  1542. *
  1543. * \param[in,out] operation The operation object to specify the
  1544. * application's role for. It must have been set up
  1545. * by psa_pake_setup() and not yet in use (neither
  1546. * psa_pake_output() nor psa_pake_input() has been
  1547. * called yet). It must be on operation for which
  1548. * the application's role hasn't been specified
  1549. * (psa_pake_set_role() hasn't been called yet).
  1550. * \param role A value of type ::psa_pake_role_t indicating the
  1551. * application's role in the PAKE the algorithm
  1552. * that is being set up. For more information see
  1553. * the documentation of \c PSA_PAKE_ROLE_XXX
  1554. * constants.
  1555. *
  1556. * \retval #PSA_SUCCESS
  1557. * Success.
  1558. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1559. * The \p role is not a valid PAKE role in the \p operation’s algorithm.
  1560. * \retval #PSA_ERROR_NOT_SUPPORTED
  1561. * The \p role for this algorithm is not supported or is not valid.
  1562. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1563. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1564. * \retval #PSA_ERROR_BAD_STATE
  1565. * The operation state is not valid, or
  1566. * the library has not been previously initialized by psa_crypto_init().
  1567. * It is implementation-dependent whether a failure to initialize
  1568. * results in this error code.
  1569. */
  1570. psa_status_t psa_pake_set_role(psa_pake_operation_t *operation,
  1571. psa_pake_role_t role);
  1572. /** Get output for a step of a password-authenticated key exchange.
  1573. *
  1574. * Depending on the algorithm being executed, you might need to call this
  1575. * function several times or you might not need to call this at all.
  1576. *
  1577. * The exact sequence of calls to perform a password-authenticated key
  1578. * exchange depends on the algorithm in use. Refer to the documentation of
  1579. * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
  1580. * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
  1581. * information.
  1582. *
  1583. * If this function returns an error status, the operation enters an error
  1584. * state and must be aborted by calling psa_pake_abort().
  1585. *
  1586. * \param[in,out] operation Active PAKE operation.
  1587. * \param step The step of the algorithm for which the output is
  1588. * requested.
  1589. * \param[out] output Buffer where the output is to be written in the
  1590. * format appropriate for this \p step. Refer to
  1591. * the documentation of the individual
  1592. * \c PSA_PAKE_STEP_XXX constants for more
  1593. * information.
  1594. * \param output_size Size of the \p output buffer in bytes. This must
  1595. * be at least #PSA_PAKE_OUTPUT_SIZE(\p alg, \p
  1596. * primitive, \p step) where \p alg and
  1597. * \p primitive are the PAKE algorithm and primitive
  1598. * in the operation's cipher suite, and \p step is
  1599. * the output step.
  1600. *
  1601. * \param[out] output_length On success, the number of bytes of the returned
  1602. * output.
  1603. *
  1604. * \retval #PSA_SUCCESS
  1605. * Success.
  1606. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  1607. * The size of the \p output buffer is too small.
  1608. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1609. * \p step is not compatible with the operation's algorithm.
  1610. * \retval #PSA_ERROR_NOT_SUPPORTED
  1611. * \p step is not supported with the operation's algorithm.
  1612. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  1613. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1614. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1615. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1616. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  1617. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  1618. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  1619. * \retval #PSA_ERROR_BAD_STATE
  1620. * The operation state is not valid (it must be active, and fully set
  1621. * up, and this call must conform to the algorithm's requirements
  1622. * for ordering of input and output steps), or
  1623. * the library has not been previously initialized by psa_crypto_init().
  1624. * It is implementation-dependent whether a failure to initialize
  1625. * results in this error code.
  1626. */
  1627. psa_status_t psa_pake_output(psa_pake_operation_t *operation,
  1628. psa_pake_step_t step,
  1629. uint8_t *output,
  1630. size_t output_size,
  1631. size_t *output_length);
  1632. /** Provide input for a step of a password-authenticated key exchange.
  1633. *
  1634. * Depending on the algorithm being executed, you might need to call this
  1635. * function several times or you might not need to call this at all.
  1636. *
  1637. * The exact sequence of calls to perform a password-authenticated key
  1638. * exchange depends on the algorithm in use. Refer to the documentation of
  1639. * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
  1640. * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
  1641. * information.
  1642. *
  1643. * If this function returns an error status, the operation enters an error
  1644. * state and must be aborted by calling psa_pake_abort().
  1645. *
  1646. * \param[in,out] operation Active PAKE operation.
  1647. * \param step The step for which the input is provided.
  1648. * \param[in] input Buffer containing the input in the format
  1649. * appropriate for this \p step. Refer to the
  1650. * documentation of the individual
  1651. * \c PSA_PAKE_STEP_XXX constants for more
  1652. * information.
  1653. * \param input_length Size of the \p input buffer in bytes.
  1654. *
  1655. * \retval #PSA_SUCCESS
  1656. * Success.
  1657. * \retval #PSA_ERROR_INVALID_SIGNATURE
  1658. * The verification fails for a #PSA_PAKE_STEP_ZK_PROOF input step.
  1659. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1660. * \p is not compatible with the \p operation’s algorithm, or the
  1661. * \p input is not valid for the \p operation's algorithm, cipher suite
  1662. * or \p step.
  1663. * \retval #PSA_ERROR_NOT_SUPPORTED
  1664. * \p step p is not supported with the \p operation's algorithm, or the
  1665. * \p input is not supported for the \p operation's algorithm, cipher
  1666. * suite or \p step.
  1667. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1668. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1669. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1670. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  1671. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  1672. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  1673. * \retval #PSA_ERROR_BAD_STATE
  1674. * The operation state is not valid (it must be active, and fully set
  1675. * up, and this call must conform to the algorithm's requirements
  1676. * for ordering of input and output steps), or
  1677. * the library has not been previously initialized by psa_crypto_init().
  1678. * It is implementation-dependent whether a failure to initialize
  1679. * results in this error code.
  1680. */
  1681. psa_status_t psa_pake_input(psa_pake_operation_t *operation,
  1682. psa_pake_step_t step,
  1683. const uint8_t *input,
  1684. size_t input_length);
  1685. /** Get implicitly confirmed shared secret from a PAKE.
  1686. *
  1687. * At this point there is a cryptographic guarantee that only the authenticated
  1688. * party who used the same password is able to compute the key. But there is no
  1689. * guarantee that the peer is the party it claims to be and was able to do so.
  1690. *
  1691. * That is, the authentication is only implicit. Since the peer is not
  1692. * authenticated yet, no action should be taken yet that assumes that the peer
  1693. * is who it claims to be. For example, do not access restricted files on the
  1694. * peer's behalf until an explicit authentication has succeeded.
  1695. *
  1696. * This function can be called after the key exchange phase of the operation
  1697. * has completed. It imports the shared secret output of the PAKE into the
  1698. * provided derivation operation. The input step
  1699. * #PSA_KEY_DERIVATION_INPUT_SECRET is used when placing the shared key
  1700. * material in the key derivation operation.
  1701. *
  1702. * The exact sequence of calls to perform a password-authenticated key
  1703. * exchange depends on the algorithm in use. Refer to the documentation of
  1704. * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
  1705. * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
  1706. * information.
  1707. *
  1708. * When this function returns successfully, \p operation becomes inactive.
  1709. * If this function returns an error status, both \p operation
  1710. * and \p key_derivation operations enter an error state and must be aborted by
  1711. * calling psa_pake_abort() and psa_key_derivation_abort() respectively.
  1712. *
  1713. * \param[in,out] operation Active PAKE operation.
  1714. * \param[out] output A key derivation operation that is ready
  1715. * for an input step of type
  1716. * #PSA_KEY_DERIVATION_INPUT_SECRET.
  1717. *
  1718. * \retval #PSA_SUCCESS
  1719. * Success.
  1720. * \retval #PSA_ERROR_INVALID_ARGUMENT
  1721. * #PSA_KEY_DERIVATION_INPUT_SECRET is not compatible with the
  1722. * algorithm in the \p output key derivation operation.
  1723. * \retval #PSA_ERROR_NOT_SUPPORTED
  1724. * Input from a PAKE is not supported by the algorithm in the \p output
  1725. * key derivation operation.
  1726. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  1727. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1728. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1729. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  1730. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  1731. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  1732. * \retval #PSA_ERROR_BAD_STATE
  1733. * The PAKE operation state is not valid (it must be active, but beyond
  1734. * that validity is specific to the algorithm), or
  1735. * the library has not been previously initialized by psa_crypto_init(),
  1736. * or the state of \p output is not valid for
  1737. * the #PSA_KEY_DERIVATION_INPUT_SECRET step. This can happen if the
  1738. * step is out of order or the application has done this step already
  1739. * and it may not be repeated.
  1740. * It is implementation-dependent whether a failure to initialize
  1741. * results in this error code.
  1742. */
  1743. psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation,
  1744. psa_key_derivation_operation_t *output);
  1745. /** Abort a PAKE operation.
  1746. *
  1747. * Aborting an operation frees all associated resources except for the \c
  1748. * operation structure itself. Once aborted, the operation object can be reused
  1749. * for another operation by calling psa_pake_setup() again.
  1750. *
  1751. * This function may be called at any time after the operation
  1752. * object has been initialized as described in #psa_pake_operation_t.
  1753. *
  1754. * In particular, calling psa_pake_abort() after the operation has been
  1755. * terminated by a call to psa_pake_abort() or psa_pake_get_implicit_key()
  1756. * is safe and has no effect.
  1757. *
  1758. * \param[in,out] operation The operation to abort.
  1759. *
  1760. * \retval #PSA_SUCCESS
  1761. * Success.
  1762. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  1763. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  1764. * \retval #PSA_ERROR_BAD_STATE
  1765. * The library has not been previously initialized by psa_crypto_init().
  1766. * It is implementation-dependent whether a failure to initialize
  1767. * results in this error code.
  1768. */
  1769. psa_status_t psa_pake_abort(psa_pake_operation_t *operation);
  1770. /**@}*/
  1771. /** A sufficient output buffer size for psa_pake_output().
  1772. *
  1773. * If the size of the output buffer is at least this large, it is guaranteed
  1774. * that psa_pake_output() will not fail due to an insufficient output buffer
  1775. * size. The actual size of the output might be smaller in any given call.
  1776. *
  1777. * See also #PSA_PAKE_OUTPUT_MAX_SIZE
  1778. *
  1779. * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that
  1780. * #PSA_ALG_IS_PAKE(\p alg) is true).
  1781. * \param primitive A primitive of type ::psa_pake_primitive_t that is
  1782. * compatible with algorithm \p alg.
  1783. * \param output_step A value of type ::psa_pake_step_t that is valid for the
  1784. * algorithm \p alg.
  1785. * \return A sufficient output buffer size for the specified
  1786. * PAKE algorithm, primitive, and output step. If the
  1787. * PAKE algorithm, primitive, or output step is not
  1788. * recognized, or the parameters are incompatible,
  1789. * return 0.
  1790. */
  1791. #define PSA_PAKE_OUTPUT_SIZE(alg, primitive, output_step) \
  1792. (alg == PSA_ALG_JPAKE && \
  1793. primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
  1794. PSA_ECC_FAMILY_SECP_R1, 256) ? \
  1795. ( \
  1796. output_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
  1797. output_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
  1798. 32 \
  1799. ) : \
  1800. 0)
  1801. /** A sufficient input buffer size for psa_pake_input().
  1802. *
  1803. * The value returned by this macro is guaranteed to be large enough for any
  1804. * valid input to psa_pake_input() in an operation with the specified
  1805. * parameters.
  1806. *
  1807. * See also #PSA_PAKE_INPUT_MAX_SIZE
  1808. *
  1809. * \param alg A PAKE algorithm (\c PSA_ALG_XXX value such that
  1810. * #PSA_ALG_IS_PAKE(\p alg) is true).
  1811. * \param primitive A primitive of type ::psa_pake_primitive_t that is
  1812. * compatible with algorithm \p alg.
  1813. * \param input_step A value of type ::psa_pake_step_t that is valid for the
  1814. * algorithm \p alg.
  1815. * \return A sufficient input buffer size for the specified
  1816. * input, cipher suite and algorithm. If the cipher suite,
  1817. * the input type or PAKE algorithm is not recognized, or
  1818. * the parameters are incompatible, return 0.
  1819. */
  1820. #define PSA_PAKE_INPUT_SIZE(alg, primitive, input_step) \
  1821. (alg == PSA_ALG_JPAKE && \
  1822. primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, \
  1823. PSA_ECC_FAMILY_SECP_R1, 256) ? \
  1824. ( \
  1825. input_step == PSA_PAKE_STEP_KEY_SHARE ? 65 : \
  1826. input_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 : \
  1827. 32 \
  1828. ) : \
  1829. 0)
  1830. /** Output buffer size for psa_pake_output() for any of the supported PAKE
  1831. * algorithm and primitive suites and output step.
  1832. *
  1833. * This macro must expand to a compile-time constant integer.
  1834. *
  1835. * See also #PSA_PAKE_OUTPUT_SIZE(\p alg, \p primitive, \p step).
  1836. */
  1837. #define PSA_PAKE_OUTPUT_MAX_SIZE 65
  1838. /** Input buffer size for psa_pake_input() for any of the supported PAKE
  1839. * algorithm and primitive suites and input step.
  1840. *
  1841. * This macro must expand to a compile-time constant integer.
  1842. *
  1843. * See also #PSA_PAKE_INPUT_SIZE(\p alg, \p primitive, \p step).
  1844. */
  1845. #define PSA_PAKE_INPUT_MAX_SIZE 65
  1846. /** Returns a suitable initializer for a PAKE cipher suite object of type
  1847. * psa_pake_cipher_suite_t.
  1848. */
  1849. #define PSA_PAKE_CIPHER_SUITE_INIT { PSA_ALG_NONE, 0, 0, 0, PSA_ALG_NONE }
  1850. /** Returns a suitable initializer for a PAKE operation object of type
  1851. * psa_pake_operation_t.
  1852. */
  1853. #define PSA_PAKE_OPERATION_INIT { 0, PSA_ALG_NONE, PSA_PAKE_OPERATION_STAGE_SETUP, \
  1854. { 0 }, { { 0 } } }
  1855. struct psa_pake_cipher_suite_s {
  1856. psa_algorithm_t algorithm;
  1857. psa_pake_primitive_type_t type;
  1858. psa_pake_family_t family;
  1859. uint16_t bits;
  1860. psa_algorithm_t hash;
  1861. };
  1862. static inline psa_algorithm_t psa_pake_cs_get_algorithm(
  1863. const psa_pake_cipher_suite_t *cipher_suite)
  1864. {
  1865. return cipher_suite->algorithm;
  1866. }
  1867. static inline void psa_pake_cs_set_algorithm(
  1868. psa_pake_cipher_suite_t *cipher_suite,
  1869. psa_algorithm_t algorithm)
  1870. {
  1871. if (!PSA_ALG_IS_PAKE(algorithm)) {
  1872. cipher_suite->algorithm = 0;
  1873. } else {
  1874. cipher_suite->algorithm = algorithm;
  1875. }
  1876. }
  1877. static inline psa_pake_primitive_t psa_pake_cs_get_primitive(
  1878. const psa_pake_cipher_suite_t *cipher_suite)
  1879. {
  1880. return PSA_PAKE_PRIMITIVE(cipher_suite->type, cipher_suite->family,
  1881. cipher_suite->bits);
  1882. }
  1883. static inline void psa_pake_cs_set_primitive(
  1884. psa_pake_cipher_suite_t *cipher_suite,
  1885. psa_pake_primitive_t primitive)
  1886. {
  1887. cipher_suite->type = (psa_pake_primitive_type_t) (primitive >> 24);
  1888. cipher_suite->family = (psa_pake_family_t) (0xFF & (primitive >> 16));
  1889. cipher_suite->bits = (uint16_t) (0xFFFF & primitive);
  1890. }
  1891. static inline psa_pake_family_t psa_pake_cs_get_family(
  1892. const psa_pake_cipher_suite_t *cipher_suite)
  1893. {
  1894. return cipher_suite->family;
  1895. }
  1896. static inline uint16_t psa_pake_cs_get_bits(
  1897. const psa_pake_cipher_suite_t *cipher_suite)
  1898. {
  1899. return cipher_suite->bits;
  1900. }
  1901. static inline psa_algorithm_t psa_pake_cs_get_hash(
  1902. const psa_pake_cipher_suite_t *cipher_suite)
  1903. {
  1904. return cipher_suite->hash;
  1905. }
  1906. static inline void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
  1907. psa_algorithm_t hash)
  1908. {
  1909. if (!PSA_ALG_IS_HASH(hash)) {
  1910. cipher_suite->hash = 0;
  1911. } else {
  1912. cipher_suite->hash = hash;
  1913. }
  1914. }
  1915. struct psa_crypto_driver_pake_inputs_s {
  1916. uint8_t *MBEDTLS_PRIVATE(password);
  1917. size_t MBEDTLS_PRIVATE(password_len);
  1918. psa_pake_role_t MBEDTLS_PRIVATE(role);
  1919. uint8_t *MBEDTLS_PRIVATE(user);
  1920. size_t MBEDTLS_PRIVATE(user_len);
  1921. uint8_t *MBEDTLS_PRIVATE(peer);
  1922. size_t MBEDTLS_PRIVATE(peer_len);
  1923. psa_key_attributes_t MBEDTLS_PRIVATE(attributes);
  1924. psa_pake_cipher_suite_t MBEDTLS_PRIVATE(cipher_suite);
  1925. };
  1926. typedef enum psa_jpake_step {
  1927. PSA_PAKE_STEP_INVALID = 0,
  1928. PSA_PAKE_STEP_X1_X2 = 1,
  1929. PSA_PAKE_STEP_X2S = 2,
  1930. PSA_PAKE_STEP_DERIVE = 3,
  1931. } psa_jpake_step_t;
  1932. typedef enum psa_jpake_state {
  1933. PSA_PAKE_STATE_INVALID = 0,
  1934. PSA_PAKE_STATE_SETUP = 1,
  1935. PSA_PAKE_STATE_READY = 2,
  1936. PSA_PAKE_OUTPUT_X1_X2 = 3,
  1937. PSA_PAKE_OUTPUT_X2S = 4,
  1938. PSA_PAKE_INPUT_X1_X2 = 5,
  1939. PSA_PAKE_INPUT_X4S = 6,
  1940. } psa_jpake_state_t;
  1941. typedef enum psa_jpake_sequence {
  1942. PSA_PAKE_SEQ_INVALID = 0,
  1943. PSA_PAKE_X1_STEP_KEY_SHARE = 1, /* also X2S & X4S KEY_SHARE */
  1944. PSA_PAKE_X1_STEP_ZK_PUBLIC = 2, /* also X2S & X4S ZK_PUBLIC */
  1945. PSA_PAKE_X1_STEP_ZK_PROOF = 3, /* also X2S & X4S ZK_PROOF */
  1946. PSA_PAKE_X2_STEP_KEY_SHARE = 4,
  1947. PSA_PAKE_X2_STEP_ZK_PUBLIC = 5,
  1948. PSA_PAKE_X2_STEP_ZK_PROOF = 6,
  1949. PSA_PAKE_SEQ_END = 7,
  1950. } psa_jpake_sequence_t;
  1951. typedef enum psa_crypto_driver_pake_step {
  1952. PSA_JPAKE_STEP_INVALID = 0, /* Invalid step */
  1953. PSA_JPAKE_X1_STEP_KEY_SHARE = 1, /* Round 1: input/output key share (for ephemeral private key X1).*/
  1954. PSA_JPAKE_X1_STEP_ZK_PUBLIC = 2, /* Round 1: input/output Schnorr NIZKP public key for the X1 key */
  1955. PSA_JPAKE_X1_STEP_ZK_PROOF = 3, /* Round 1: input/output Schnorr NIZKP proof for the X1 key */
  1956. PSA_JPAKE_X2_STEP_KEY_SHARE = 4, /* Round 1: input/output key share (for ephemeral private key X2).*/
  1957. PSA_JPAKE_X2_STEP_ZK_PUBLIC = 5, /* Round 1: input/output Schnorr NIZKP public key for the X2 key */
  1958. PSA_JPAKE_X2_STEP_ZK_PROOF = 6, /* Round 1: input/output Schnorr NIZKP proof for the X2 key */
  1959. PSA_JPAKE_X2S_STEP_KEY_SHARE = 7, /* Round 2: output X2S key (our key) */
  1960. PSA_JPAKE_X2S_STEP_ZK_PUBLIC = 8, /* Round 2: output Schnorr NIZKP public key for the X2S key (our key) */
  1961. PSA_JPAKE_X2S_STEP_ZK_PROOF = 9, /* Round 2: output Schnorr NIZKP proof for the X2S key (our key) */
  1962. PSA_JPAKE_X4S_STEP_KEY_SHARE = 10, /* Round 2: input X4S key (from peer) */
  1963. PSA_JPAKE_X4S_STEP_ZK_PUBLIC = 11, /* Round 2: input Schnorr NIZKP public key for the X4S key (from peer) */
  1964. PSA_JPAKE_X4S_STEP_ZK_PROOF = 12 /* Round 2: input Schnorr NIZKP proof for the X4S key (from peer) */
  1965. } psa_crypto_driver_pake_step_t;
  1966. struct psa_jpake_computation_stage_s {
  1967. psa_jpake_state_t MBEDTLS_PRIVATE(state);
  1968. psa_jpake_sequence_t MBEDTLS_PRIVATE(sequence);
  1969. psa_jpake_step_t MBEDTLS_PRIVATE(input_step);
  1970. psa_jpake_step_t MBEDTLS_PRIVATE(output_step);
  1971. };
  1972. struct psa_pake_operation_s {
  1973. /** Unique ID indicating which driver got assigned to do the
  1974. * operation. Since driver contexts are driver-specific, swapping
  1975. * drivers halfway through the operation is not supported.
  1976. * ID values are auto-generated in psa_crypto_driver_wrappers.h
  1977. * ID value zero means the context is not valid or not assigned to
  1978. * any driver (i.e. none of the driver contexts are active). */
  1979. unsigned int MBEDTLS_PRIVATE(id);
  1980. /* Algorithm of the PAKE operation */
  1981. psa_algorithm_t MBEDTLS_PRIVATE(alg);
  1982. /* Stage of the PAKE operation: waiting for the setup, collecting inputs
  1983. * or computing. */
  1984. uint8_t MBEDTLS_PRIVATE(stage);
  1985. /* Holds computation stage of the PAKE algorithms. */
  1986. union {
  1987. uint8_t MBEDTLS_PRIVATE(dummy);
  1988. #if defined(PSA_WANT_ALG_JPAKE)
  1989. psa_jpake_computation_stage_t MBEDTLS_PRIVATE(jpake);
  1990. #endif
  1991. } MBEDTLS_PRIVATE(computation_stage);
  1992. union {
  1993. psa_driver_pake_context_t MBEDTLS_PRIVATE(ctx);
  1994. psa_crypto_driver_pake_inputs_t MBEDTLS_PRIVATE(inputs);
  1995. } MBEDTLS_PRIVATE(data);
  1996. };
  1997. static inline struct psa_pake_cipher_suite_s psa_pake_cipher_suite_init(void)
  1998. {
  1999. const struct psa_pake_cipher_suite_s v = PSA_PAKE_CIPHER_SUITE_INIT;
  2000. return v;
  2001. }
  2002. static inline struct psa_pake_operation_s psa_pake_operation_init(void)
  2003. {
  2004. const struct psa_pake_operation_s v = PSA_PAKE_OPERATION_INIT;
  2005. return v;
  2006. }
  2007. #ifdef __cplusplus
  2008. }
  2009. #endif
  2010. #endif /* PSA_CRYPTO_EXTRA_H */