crypto_extra.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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/platform_util.h"
  30. #include "crypto_types.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 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->core.policy.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->core.policy.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->core.flags |= MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
  134. attributes->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->core.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 volatile_slots;
  213. /** Number of slots containing key material for a key which is in
  214. * internal persistent storage. */
  215. size_t persistent_slots;
  216. /** Number of slots containing a reference to a key in a
  217. * secure element. */
  218. size_t external_slots;
  219. /** Number of slots which are occupied, but do not contain
  220. * key material yet. */
  221. size_t half_filled_slots;
  222. /** Number of slots that contain cache data. */
  223. size_t cache_slots;
  224. /** Number of slots that are not used for anything. */
  225. size_t empty_slots;
  226. /** Number of slots that are locked. */
  227. size_t locked_slots;
  228. /** Largest key id value among open keys in internal persistent storage. */
  229. psa_key_id_t max_open_internal_key_id;
  230. /** Largest key id value among open keys in secure elements. */
  231. psa_key_id_t 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_MIN_PLATFORM
  291. * and #MBEDTLS_ENTROPY_BLOCK_SIZE.
  292. * It must be less or equal to
  293. * #MBEDTLS_ENTROPY_MAX_SEED_SIZE.
  294. *
  295. * \retval #PSA_SUCCESS
  296. * The seed value was injected successfully. The random generator
  297. * of the PSA Crypto implementation is now ready for use.
  298. * You may now call psa_crypto_init() and use the PSA Crypto
  299. * implementation.
  300. * \retval #PSA_ERROR_INVALID_ARGUMENT
  301. * \p seed_size is out of range.
  302. * \retval #PSA_ERROR_STORAGE_FAILURE
  303. * There was a failure reading or writing from storage.
  304. * \retval #PSA_ERROR_NOT_PERMITTED
  305. * The library has already been initialized. It is no longer
  306. * possible to call this function.
  307. */
  308. psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
  309. size_t seed_size);
  310. /** \addtogroup crypto_types
  311. * @{
  312. */
  313. /** DSA public key.
  314. *
  315. * The import and export format is the
  316. * representation of the public key `y = g^x mod p` as a big-endian byte
  317. * string. The length of the byte string is the length of the base prime `p`
  318. * in bytes.
  319. */
  320. #define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x4002)
  321. /** DSA key pair (private and public key).
  322. *
  323. * The import and export format is the
  324. * representation of the private key `x` as a big-endian byte string. The
  325. * length of the byte string is the private key size in bytes (leading zeroes
  326. * are not stripped).
  327. *
  328. * Determinstic DSA key derivation with psa_generate_derived_key follows
  329. * FIPS 186-4 §B.1.2: interpret the byte string as integer
  330. * in big-endian order. Discard it if it is not in the range
  331. * [0, *N* - 2] where *N* is the boundary of the private key domain
  332. * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
  333. * or the order of the curve's base point for ECC).
  334. * Add 1 to the resulting integer and use this as the private key *x*.
  335. *
  336. */
  337. #define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t)0x7002)
  338. /** Whether a key type is an DSA key (pair or public-only). */
  339. #define PSA_KEY_TYPE_IS_DSA(type) \
  340. (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
  341. #define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x06000400)
  342. /** DSA signature with hashing.
  343. *
  344. * This is the signature scheme defined by FIPS 186-4,
  345. * with a random per-message secret number (*k*).
  346. *
  347. * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
  348. * #PSA_ALG_IS_HASH(\p hash_alg) is true).
  349. * This includes #PSA_ALG_ANY_HASH
  350. * when specifying the algorithm in a usage policy.
  351. *
  352. * \return The corresponding DSA signature algorithm.
  353. * \return Unspecified if \p hash_alg is not a supported
  354. * hash algorithm.
  355. */
  356. #define PSA_ALG_DSA(hash_alg) \
  357. (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
  358. #define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x06000500)
  359. #define PSA_ALG_DSA_DETERMINISTIC_FLAG PSA_ALG_ECDSA_DETERMINISTIC_FLAG
  360. /** Deterministic DSA signature with hashing.
  361. *
  362. * This is the deterministic variant defined by RFC 6979 of
  363. * the signature scheme defined by FIPS 186-4.
  364. *
  365. * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
  366. * #PSA_ALG_IS_HASH(\p hash_alg) is true).
  367. * This includes #PSA_ALG_ANY_HASH
  368. * when specifying the algorithm in a usage policy.
  369. *
  370. * \return The corresponding DSA signature algorithm.
  371. * \return Unspecified if \p hash_alg is not a supported
  372. * hash algorithm.
  373. */
  374. #define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
  375. (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
  376. #define PSA_ALG_IS_DSA(alg) \
  377. (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
  378. PSA_ALG_DSA_BASE)
  379. #define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
  380. (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
  381. #define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
  382. (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
  383. #define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
  384. (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
  385. /* We need to expand the sample definition of this macro from
  386. * the API definition. */
  387. #undef PSA_ALG_IS_VENDOR_HASH_AND_SIGN
  388. #define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) \
  389. PSA_ALG_IS_DSA(alg)
  390. /**@}*/
  391. /** \addtogroup attributes
  392. * @{
  393. */
  394. /** Custom Diffie-Hellman group.
  395. *
  396. * For keys of type #PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or
  397. * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM), the group data comes
  398. * from domain parameters set by psa_set_key_domain_parameters().
  399. */
  400. #define PSA_DH_FAMILY_CUSTOM ((psa_dh_family_t) 0x7e)
  401. /**
  402. * \brief Set domain parameters for a key.
  403. *
  404. * Some key types require additional domain parameters in addition to
  405. * the key type identifier and the key size. Use this function instead
  406. * of psa_set_key_type() when you need to specify domain parameters.
  407. *
  408. * The format for the required domain parameters varies based on the key type.
  409. *
  410. * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEY_PAIR),
  411. * the domain parameter data consists of the public exponent,
  412. * represented as a big-endian integer with no leading zeros.
  413. * This information is used when generating an RSA key pair.
  414. * When importing a key, the public exponent is read from the imported
  415. * key data and the exponent recorded in the attribute structure is ignored.
  416. * As an exception, the public exponent 65537 is represented by an empty
  417. * byte string.
  418. * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEY_PAIR),
  419. * the `Dss-Parms` format as defined by RFC 3279 §2.3.2.
  420. * ```
  421. * Dss-Parms ::= SEQUENCE {
  422. * p INTEGER,
  423. * q INTEGER,
  424. * g INTEGER
  425. * }
  426. * ```
  427. * - For Diffie-Hellman key exchange keys
  428. * (#PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or
  429. * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM)), the
  430. * `DomainParameters` format as defined by RFC 3279 §2.3.3.
  431. * ```
  432. * DomainParameters ::= SEQUENCE {
  433. * p INTEGER, -- odd prime, p=jq +1
  434. * g INTEGER, -- generator, g
  435. * q INTEGER, -- factor of p-1
  436. * j INTEGER OPTIONAL, -- subgroup factor
  437. * validationParms ValidationParms OPTIONAL
  438. * }
  439. * ValidationParms ::= SEQUENCE {
  440. * seed BIT STRING,
  441. * pgenCounter INTEGER
  442. * }
  443. * ```
  444. *
  445. * \note This function may allocate memory or other resources.
  446. * Once you have called this function on an attribute structure,
  447. * you must call psa_reset_key_attributes() to free these resources.
  448. *
  449. * \note This is an experimental extension to the interface. It may change
  450. * in future versions of the library.
  451. *
  452. * \param[in,out] attributes Attribute structure where the specified domain
  453. * parameters will be stored.
  454. * If this function fails, the content of
  455. * \p attributes is not modified.
  456. * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
  457. * \param[in] data Buffer containing the key domain parameters.
  458. * The content of this buffer is interpreted
  459. * according to \p type as described above.
  460. * \param data_length Size of the \p data buffer in bytes.
  461. *
  462. * \retval #PSA_SUCCESS
  463. * \retval #PSA_ERROR_INVALID_ARGUMENT
  464. * \retval #PSA_ERROR_NOT_SUPPORTED
  465. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  466. */
  467. psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
  468. psa_key_type_t type,
  469. const uint8_t *data,
  470. size_t data_length);
  471. /**
  472. * \brief Get domain parameters for a key.
  473. *
  474. * Get the domain parameters for a key with this function, if any. The format
  475. * of the domain parameters written to \p data is specified in the
  476. * documentation for psa_set_key_domain_parameters().
  477. *
  478. * \note This is an experimental extension to the interface. It may change
  479. * in future versions of the library.
  480. *
  481. * \param[in] attributes The key attribute structure to query.
  482. * \param[out] data On success, the key domain parameters.
  483. * \param data_size Size of the \p data buffer in bytes.
  484. * The buffer is guaranteed to be large
  485. * enough if its size in bytes is at least
  486. * the value given by
  487. * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
  488. * \param[out] data_length On success, the number of bytes
  489. * that make up the key domain parameters data.
  490. *
  491. * \retval #PSA_SUCCESS
  492. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  493. */
  494. psa_status_t psa_get_key_domain_parameters(
  495. const psa_key_attributes_t *attributes,
  496. uint8_t *data,
  497. size_t data_size,
  498. size_t *data_length);
  499. /** Safe output buffer size for psa_get_key_domain_parameters().
  500. *
  501. * This macro returns a compile-time constant if its arguments are
  502. * compile-time constants.
  503. *
  504. * \warning This function may call its arguments multiple times or
  505. * zero times, so you should not pass arguments that contain
  506. * side effects.
  507. *
  508. * \note This is an experimental extension to the interface. It may change
  509. * in future versions of the library.
  510. *
  511. * \param key_type A supported key type.
  512. * \param key_bits The size of the key in bits.
  513. *
  514. * \return If the parameters are valid and supported, return
  515. * a buffer size in bytes that guarantees that
  516. * psa_get_key_domain_parameters() will not fail with
  517. * #PSA_ERROR_BUFFER_TOO_SMALL.
  518. * If the parameters are a valid combination that is not supported
  519. * by the implementation, this macro shall return either a
  520. * sensible size or 0.
  521. * If the parameters are not valid, the
  522. * return value is unspecified.
  523. */
  524. #define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \
  525. (PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) : \
  526. PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
  527. PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
  528. 0)
  529. #define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
  530. (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/)
  531. #define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
  532. (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/)
  533. /**@}*/
  534. /** \defgroup psa_tls_helpers TLS helper functions
  535. * @{
  536. */
  537. #if defined(MBEDTLS_ECP_C)
  538. #include <mbedtls/ecp.h>
  539. /** Convert an ECC curve identifier from the Mbed TLS encoding to PSA.
  540. *
  541. * \note This function is provided solely for the convenience of
  542. * Mbed TLS and may be removed at any time without notice.
  543. *
  544. * \param grpid An Mbed TLS elliptic curve identifier
  545. * (`MBEDTLS_ECP_DP_xxx`).
  546. * \param[out] bits On success, the bit size of the curve.
  547. *
  548. * \return The corresponding PSA elliptic curve identifier
  549. * (`PSA_ECC_FAMILY_xxx`).
  550. * \return \c 0 on failure (\p grpid is not recognized).
  551. */
  552. static inline psa_ecc_family_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid,
  553. size_t *bits )
  554. {
  555. switch( grpid )
  556. {
  557. case MBEDTLS_ECP_DP_SECP192R1:
  558. *bits = 192;
  559. return( PSA_ECC_FAMILY_SECP_R1 );
  560. case MBEDTLS_ECP_DP_SECP224R1:
  561. *bits = 224;
  562. return( PSA_ECC_FAMILY_SECP_R1 );
  563. case MBEDTLS_ECP_DP_SECP256R1:
  564. *bits = 256;
  565. return( PSA_ECC_FAMILY_SECP_R1 );
  566. case MBEDTLS_ECP_DP_SECP384R1:
  567. *bits = 384;
  568. return( PSA_ECC_FAMILY_SECP_R1 );
  569. case MBEDTLS_ECP_DP_SECP521R1:
  570. *bits = 521;
  571. return( PSA_ECC_FAMILY_SECP_R1 );
  572. case MBEDTLS_ECP_DP_BP256R1:
  573. *bits = 256;
  574. return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
  575. case MBEDTLS_ECP_DP_BP384R1:
  576. *bits = 384;
  577. return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
  578. case MBEDTLS_ECP_DP_BP512R1:
  579. *bits = 512;
  580. return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
  581. case MBEDTLS_ECP_DP_CURVE25519:
  582. *bits = 255;
  583. return( PSA_ECC_FAMILY_MONTGOMERY );
  584. case MBEDTLS_ECP_DP_SECP192K1:
  585. *bits = 192;
  586. return( PSA_ECC_FAMILY_SECP_K1 );
  587. case MBEDTLS_ECP_DP_SECP224K1:
  588. *bits = 224;
  589. return( PSA_ECC_FAMILY_SECP_K1 );
  590. case MBEDTLS_ECP_DP_SECP256K1:
  591. *bits = 256;
  592. return( PSA_ECC_FAMILY_SECP_K1 );
  593. case MBEDTLS_ECP_DP_CURVE448:
  594. *bits = 448;
  595. return( PSA_ECC_FAMILY_MONTGOMERY );
  596. default:
  597. *bits = 0;
  598. return( 0 );
  599. }
  600. }
  601. /** Convert an ECC curve identifier from the PSA encoding to Mbed TLS.
  602. *
  603. * \note This function is provided solely for the convenience of
  604. * Mbed TLS and may be removed at any time without notice.
  605. *
  606. * \param curve A PSA elliptic curve identifier
  607. * (`PSA_ECC_FAMILY_xxx`).
  608. * \param bits The bit-length of a private key on \p curve.
  609. * \param bits_is_sloppy If true, \p bits may be the bit-length rounded up
  610. * to the nearest multiple of 8. This allows the caller
  611. * to infer the exact curve from the length of a key
  612. * which is supplied as a byte string.
  613. *
  614. * \return The corresponding Mbed TLS elliptic curve identifier
  615. * (`MBEDTLS_ECP_DP_xxx`).
  616. * \return #MBEDTLS_ECP_DP_NONE if \c curve is not recognized.
  617. * \return #MBEDTLS_ECP_DP_NONE if \p bits is not
  618. * correct for \p curve.
  619. */
  620. mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
  621. size_t bits,
  622. int bits_is_sloppy );
  623. #endif /* MBEDTLS_ECP_C */
  624. /**@}*/
  625. /** \defgroup psa_external_rng External random generator
  626. * @{
  627. */
  628. #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
  629. /** External random generator function, implemented by the platform.
  630. *
  631. * When the compile-time option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled,
  632. * this function replaces Mbed TLS's entropy and DRBG modules for all
  633. * random generation triggered via PSA crypto interfaces.
  634. *
  635. * \note This random generator must deliver random numbers with cryptographic
  636. * quality and high performance. It must supply unpredictable numbers
  637. * with a uniform distribution. The implementation of this function
  638. * is responsible for ensuring that the random generator is seeded
  639. * with sufficient entropy. If you have a hardware TRNG which is slow
  640. * or delivers non-uniform output, declare it as an entropy source
  641. * with mbedtls_entropy_add_source() instead of enabling this option.
  642. *
  643. * \param[in,out] context Pointer to the random generator context.
  644. * This is all-bits-zero on the first call
  645. * and preserved between successive calls.
  646. * \param[out] output Output buffer. On success, this buffer
  647. * contains random data with a uniform
  648. * distribution.
  649. * \param output_size The size of the \p output buffer in bytes.
  650. * \param[out] output_length On success, set this value to \p output_size.
  651. *
  652. * \retval #PSA_SUCCESS
  653. * Success. The output buffer contains \p output_size bytes of
  654. * cryptographic-quality random data, and \c *output_length is
  655. * set to \p output_size.
  656. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
  657. * The random generator requires extra entropy and there is no
  658. * way to obtain entropy under current environment conditions.
  659. * This error should not happen under normal circumstances since
  660. * this function is responsible for obtaining as much entropy as
  661. * it needs. However implementations of this function may return
  662. * #PSA_ERROR_INSUFFICIENT_ENTROPY if there is no way to obtain
  663. * entropy without blocking indefinitely.
  664. * \retval #PSA_ERROR_HARDWARE_FAILURE
  665. * A failure of the random generator hardware that isn't covered
  666. * by #PSA_ERROR_INSUFFICIENT_ENTROPY.
  667. */
  668. psa_status_t mbedtls_psa_external_get_random(
  669. mbedtls_psa_external_random_context_t *context,
  670. uint8_t *output, size_t output_size, size_t *output_length );
  671. #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  672. /**@}*/
  673. /** \defgroup psa_builtin_keys Built-in keys
  674. * @{
  675. */
  676. /** The minimum value for a key identifier that is built into the
  677. * implementation.
  678. *
  679. * The range of key identifiers from #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN
  680. * to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX within the range from
  681. * #PSA_KEY_ID_VENDOR_MIN and #PSA_KEY_ID_VENDOR_MAX and must not intersect
  682. * with any other set of implementation-chosen key identifiers.
  683. *
  684. * This value is part of the library's ABI since changing it would invalidate
  685. * the values of built-in key identifiers in applications.
  686. */
  687. #define MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ((psa_key_id_t)0x7fff0000)
  688. /** The maximum value for a key identifier that is built into the
  689. * implementation.
  690. *
  691. * See #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN for more information.
  692. */
  693. #define MBEDTLS_PSA_KEY_ID_BUILTIN_MAX ((psa_key_id_t)0x7fffefff)
  694. /** A slot number identifying a key in a driver.
  695. *
  696. * Values of this type are used to identify built-in keys.
  697. */
  698. typedef uint64_t psa_drv_slot_number_t;
  699. #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
  700. /** Test whether a key identifier belongs to the builtin key range.
  701. *
  702. * \param key_id Key identifier to test.
  703. *
  704. * \retval 1
  705. * The key identifier is a builtin key identifier.
  706. * \retval 0
  707. * The key identifier is not a builtin key identifier.
  708. */
  709. static inline int psa_key_id_is_builtin( psa_key_id_t key_id )
  710. {
  711. return( ( key_id >= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ) &&
  712. ( key_id <= MBEDTLS_PSA_KEY_ID_BUILTIN_MAX ) );
  713. }
  714. /** Platform function to obtain the location and slot number of a built-in key.
  715. *
  716. * An application-specific implementation of this function must be provided if
  717. * #MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS is enabled. This would typically be provided
  718. * as part of a platform's system image.
  719. *
  720. * #MBEDTLS_SVC_KEY_ID_GET_KEY_ID(\p key_id) needs to be in the range from
  721. * #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX.
  722. *
  723. * In a multi-application configuration
  724. * (\c MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER is defined),
  725. * this function should check that #MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(\p key_id)
  726. * is allowed to use the given key.
  727. *
  728. * \param key_id The key ID for which to retrieve the
  729. * location and slot attributes.
  730. * \param[out] lifetime On success, the lifetime associated with the key
  731. * corresponding to \p key_id. Lifetime is a
  732. * combination of which driver contains the key,
  733. * and with what persistence level the key is
  734. * intended to be used. If the platform
  735. * implementation does not contain specific
  736. * information about the intended key persistence
  737. * level, the persistence level may be reported as
  738. * #PSA_KEY_PERSISTENCE_DEFAULT.
  739. * \param[out] slot_number On success, the slot number known to the driver
  740. * registered at the lifetime location reported
  741. * through \p lifetime which corresponds to the
  742. * requested built-in key.
  743. *
  744. * \retval #PSA_SUCCESS
  745. * The requested key identifier designates a built-in key.
  746. * In a multi-application configuration, the requested owner
  747. * is allowed to access it.
  748. * \retval #PSA_ERROR_DOES_NOT_EXIST
  749. * The requested key identifier is not a built-in key which is known
  750. * to this function. If a key exists in the key storage with this
  751. * identifier, the data from the storage will be used.
  752. * \return (any other error)
  753. * Any other error is propagated to the function that requested the key.
  754. * Common errors include:
  755. * - #PSA_ERROR_NOT_PERMITTED: the key exists but the requested owner
  756. * is not allowed to access it.
  757. */
  758. psa_status_t mbedtls_psa_platform_get_builtin_key(
  759. mbedtls_svc_key_id_t key_id,
  760. psa_key_lifetime_t *lifetime,
  761. psa_drv_slot_number_t *slot_number );
  762. #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
  763. /** @} */
  764. #ifdef __cplusplus
  765. }
  766. #endif
  767. #endif /* PSA_CRYPTO_EXTRA_H */