crypto_extra.h 83 KB

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