crypto_extra.h 76 KB

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