crypto_extra.h 80 KB

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