blob: a10bb8bfde7c482b525b9d795e30b3b29eeab399 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto_extra.h
3 *
4 * \brief PSA cryptography module: Mbed TLS vendor extensions
Gilles Peskine07c91f52018-06-28 18:02:53 +02005 *
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.
Gilles Peskinee59236f2018-01-27 23:32:46 +010010 */
11/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020012 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +010013 * 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.
Gilles Peskinee59236f2018-01-27 23:32:46 +010026 */
27
28#ifndef PSA_CRYPTO_EXTRA_H
29#define PSA_CRYPTO_EXTRA_H
30
Jaeden Amero81cefed2019-02-25 08:51:27 +000031#include "mbedtls/platform_util.h"
32
Gilles Peskine7a894f22019-11-26 16:06:46 +010033#include "crypto_compat.h"
34
Gilles Peskinee59236f2018-01-27 23:32:46 +010035#ifdef __cplusplus
36extern "C" {
37#endif
38
Netanel Gonen2bcd3122018-11-19 11:53:02 +020039/* UID for secure storage seed */
avolinski0d2c2662018-11-21 17:31:07 +020040#define PSA_CRYPTO_ITS_RANDOM_SEED_UID 0xFFFFFF52
Netanel Gonen2bcd3122018-11-19 11:53:02 +020041
Steven Cooreman1f968fd2021-02-15 14:00:24 +010042/* See config.h for definition */
43#if !defined(PSA_KEY_SLOT_COUNT)
44#define PSA_KEY_SLOT_COUNT 32
45#endif
Jaeden Amero5e6d24c2019-02-21 10:41:29 +000046
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020047/** \addtogroup attributes
48 * @{
49 */
50
51/** \brief Declare the enrollment algorithm for a key.
52 *
53 * An operation on a key may indifferently use the algorithm set with
54 * psa_set_key_algorithm() or with this function.
55 *
56 * \param[out] attributes The attribute structure to write to.
57 * \param alg2 A second algorithm that the key may be used
58 * for, in addition to the algorithm set with
59 * psa_set_key_algorithm().
60 *
61 * \warning Setting an enrollment algorithm is not recommended, because
62 * using the same key with different algorithms can allow some
63 * attacks based on arithmetic relations between different
64 * computations made with the same key, or can escalate harmless
65 * side channels into exploitable ones. Use this function only
Gilles Peskinef25c9ec2019-05-22 11:45:59 +020066 * if it is necessary to support a protocol for which it has been
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020067 * verified that the usage of the key with multiple algorithms
68 * is safe.
69 */
70static inline void psa_set_key_enrollment_algorithm(
71 psa_key_attributes_t *attributes,
72 psa_algorithm_t alg2)
73{
Gilles Peskine7e0cff92019-07-30 13:48:52 +020074 attributes->core.policy.alg2 = alg2;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020075}
76
77/** Retrieve the enrollment algorithm policy from key attributes.
78 *
79 * \param[in] attributes The key attribute structure to query.
80 *
81 * \return The enrollment algorithm stored in the attribute structure.
82 */
83static inline psa_algorithm_t psa_get_key_enrollment_algorithm(
84 const psa_key_attributes_t *attributes)
85{
Gilles Peskine7e0cff92019-07-30 13:48:52 +020086 return( attributes->core.policy.alg2 );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020087}
88
Gilles Peskinec8000c02019-08-02 20:15:51 +020089#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
90
91/** Retrieve the slot number where a key is stored.
92 *
93 * A slot number is only defined for keys that are stored in a secure
94 * element.
95 *
96 * This information is only useful if the secure element is not entirely
97 * managed through the PSA Cryptography API. It is up to the secure
98 * element driver to decide how PSA slot numbers map to any other interface
99 * that the secure element may have.
100 *
101 * \param[in] attributes The key attribute structure to query.
102 * \param[out] slot_number On success, the slot number containing the key.
103 *
104 * \retval #PSA_SUCCESS
105 * The key is located in a secure element, and \p *slot_number
106 * indicates the slot number that contains it.
107 * \retval #PSA_ERROR_NOT_PERMITTED
108 * The caller is not permitted to query the slot number.
109 * Mbed Crypto currently does not return this error.
110 * \retval #PSA_ERROR_INVALID_ARGUMENT
111 * The key is not located in a secure element.
112 */
113psa_status_t psa_get_key_slot_number(
114 const psa_key_attributes_t *attributes,
115 psa_key_slot_number_t *slot_number );
116
117/** Choose the slot number where a key is stored.
118 *
119 * This function declares a slot number in the specified attribute
120 * structure.
121 *
122 * A slot number is only meaningful for keys that are stored in a secure
123 * element. It is up to the secure element driver to decide how PSA slot
124 * numbers map to any other interface that the secure element may have.
125 *
126 * \note Setting a slot number in key attributes for a key creation can
127 * cause the following errors when creating the key:
128 * - #PSA_ERROR_NOT_SUPPORTED if the selected secure element does
129 * not support choosing a specific slot number.
130 * - #PSA_ERROR_NOT_PERMITTED if the caller is not permitted to
131 * choose slot numbers in general or to choose this specific slot.
132 * - #PSA_ERROR_INVALID_ARGUMENT if the chosen slot number is not
133 * valid in general or not valid for this specific key.
134 * - #PSA_ERROR_ALREADY_EXISTS if there is already a key in the
135 * selected slot.
136 *
137 * \param[out] attributes The attribute structure to write to.
138 * \param slot_number The slot number to set.
139 */
140static inline void psa_set_key_slot_number(
141 psa_key_attributes_t *attributes,
142 psa_key_slot_number_t slot_number )
143{
144 attributes->core.flags |= MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
145 attributes->slot_number = slot_number;
146}
147
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200148/** Remove the slot number attribute from a key attribute structure.
149 *
150 * This function undoes the action of psa_set_key_slot_number().
151 *
152 * \param[out] attributes The attribute structure to write to.
153 */
154static inline void psa_clear_key_slot_number(
155 psa_key_attributes_t *attributes )
156{
157 attributes->core.flags &= ~MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
158}
159
Gilles Peskined7729582019-08-05 15:55:54 +0200160/** Register a key that is already present in a secure element.
161 *
162 * The key must be located in a secure element designated by the
163 * lifetime field in \p attributes, in the slot set with
164 * psa_set_key_slot_number() in the attribute structure.
165 * This function makes the key available through the key identifier
166 * specified in \p attributes.
167 *
168 * \param[in] attributes The attributes of the existing key.
169 *
170 * \retval #PSA_SUCCESS
171 * The key was successfully registered.
172 * Note that depending on the design of the driver, this may or may
173 * not guarantee that a key actually exists in the designated slot
174 * and is compatible with the specified attributes.
175 * \retval #PSA_ERROR_ALREADY_EXISTS
176 * There is already a key with the identifier specified in
177 * \p attributes.
Gilles Peskine3efcebb2019-10-01 14:18:35 +0200178 * \retval #PSA_ERROR_NOT_SUPPORTED
179 * The secure element driver for the specified lifetime does not
180 * support registering a key.
Gilles Peskined7729582019-08-05 15:55:54 +0200181 * \retval #PSA_ERROR_INVALID_ARGUMENT
182 * \p attributes specifies a lifetime which is not located
183 * in a secure element.
184 * \retval #PSA_ERROR_INVALID_ARGUMENT
185 * No slot number is specified in \p attributes,
186 * or the specified slot number is not valid.
187 * \retval #PSA_ERROR_NOT_PERMITTED
188 * The caller is not authorized to register the specified key slot.
189 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100190 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskined7729582019-08-05 15:55:54 +0200191 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100192 * \retval #PSA_ERROR_DATA_INVALID
193 * \retval #PSA_ERROR_DATA_CORRUPT
Gilles Peskined7729582019-08-05 15:55:54 +0200194 * \retval #PSA_ERROR_CORRUPTION_DETECTED
195 * \retval #PSA_ERROR_BAD_STATE
196 * The library has not been previously initialized by psa_crypto_init().
197 * It is implementation-dependent whether a failure to initialize
198 * results in this error code.
199 */
200psa_status_t mbedtls_psa_register_se_key(
201 const psa_key_attributes_t *attributes);
202
Gilles Peskinec8000c02019-08-02 20:15:51 +0200203#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
204
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200205/**@}*/
206
Gilles Peskinee59236f2018-01-27 23:32:46 +0100207/**
208 * \brief Library deinitialization.
209 *
210 * This function clears all data associated with the PSA layer,
211 * including the whole key store.
212 *
213 * This is an Mbed TLS extension.
214 */
215void mbedtls_psa_crypto_free( void );
216
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200217/** \brief Statistics about
218 * resource consumption related to the PSA keystore.
219 *
220 * \note The content of this structure is not part of the stable API and ABI
221 * of Mbed Crypto and may change arbitrarily from version to version.
222 */
223typedef struct mbedtls_psa_stats_s
224{
225 /** Number of slots containing key material for a volatile key. */
226 size_t volatile_slots;
227 /** Number of slots containing key material for a key which is in
228 * internal persistent storage. */
229 size_t persistent_slots;
230 /** Number of slots containing a reference to a key in a
231 * secure element. */
232 size_t external_slots;
233 /** Number of slots which are occupied, but do not contain
234 * key material yet. */
235 size_t half_filled_slots;
236 /** Number of slots that contain cache data. */
237 size_t cache_slots;
238 /** Number of slots that are not used for anything. */
239 size_t empty_slots;
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100240 /** Number of slots that are locked. */
241 size_t locked_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200242 /** Largest key id value among open keys in internal persistent storage. */
Ronald Cron039a98b2020-07-23 16:07:42 +0200243 psa_key_id_t max_open_internal_key_id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200244 /** Largest key id value among open keys in secure elements. */
Ronald Cron039a98b2020-07-23 16:07:42 +0200245 psa_key_id_t max_open_external_key_id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200246} mbedtls_psa_stats_t;
247
248/** \brief Get statistics about
249 * resource consumption related to the PSA keystore.
250 *
251 * \note When Mbed Crypto is built as part of a service, with isolation
252 * between the application and the keystore, the service may or
253 * may not expose this function.
254 */
255void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats );
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200256
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200257/**
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100258 * \brief Inject an initial entropy seed for the random generator into
259 * secure storage.
Gilles Peskine0338ded2018-11-15 18:19:27 +0100260 *
261 * This function injects data to be used as a seed for the random generator
262 * used by the PSA Crypto implementation. On devices that lack a trusted
263 * entropy source (preferably a hardware random number generator),
264 * the Mbed PSA Crypto implementation uses this value to seed its
265 * random generator.
266 *
267 * On devices without a trusted entropy source, this function must be
268 * called exactly once in the lifetime of the device. On devices with
269 * a trusted entropy source, calling this function is optional.
270 * In all cases, this function may only be called before calling any
271 * other function in the PSA Crypto API, including psa_crypto_init().
272 *
273 * When this function returns successfully, it populates a file in
274 * persistent storage. Once the file has been created, this function
275 * can no longer succeed.
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100276 *
277 * If any error occurs, this function does not change the system state.
278 * You can call this function again after correcting the reason for the
279 * error if possible.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200280 *
281 * \warning This function **can** fail! Callers MUST check the return status.
282 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100283 * \warning If you use this function, you should use it as part of a
284 * factory provisioning process. The value of the injected seed
285 * is critical to the security of the device. It must be
286 * *secret*, *unpredictable* and (statistically) *unique per device*.
287 * You should be generate it randomly using a cryptographically
288 * secure random generator seeded from trusted entropy sources.
289 * You should transmit it securely to the device and ensure
290 * that its value is not leaked or stored anywhere beyond the
291 * needs of transmitting it from the point of generation to
292 * the call of this function, and erase all copies of the value
293 * once this function returns.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200294 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100295 * This is an Mbed TLS extension.
296 *
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200297 * \note This function is only available on the following platforms:
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100298 * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled.
299 * Note that you must provide compatible implementations of
300 * mbedtls_nv_seed_read and mbedtls_nv_seed_write.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200301 * * In a client-server integration of PSA Cryptography, on the client side,
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200302 * if the server supports this feature.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200303 * \param[in] seed Buffer containing the seed value to inject.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200304 * \param[in] seed_size Size of the \p seed buffer.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200305 * The size of the seed in bytes must be greater
306 * or equal to both #MBEDTLS_ENTROPY_MIN_PLATFORM
307 * and #MBEDTLS_ENTROPY_BLOCK_SIZE.
308 * It must be less or equal to
309 * #MBEDTLS_ENTROPY_MAX_SEED_SIZE.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200310 *
311 * \retval #PSA_SUCCESS
Gilles Peskine0338ded2018-11-15 18:19:27 +0100312 * The seed value was injected successfully. The random generator
313 * of the PSA Crypto implementation is now ready for use.
314 * You may now call psa_crypto_init() and use the PSA Crypto
315 * implementation.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200316 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100317 * \p seed_size is out of range.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200318 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine0338ded2018-11-15 18:19:27 +0100319 * There was a failure reading or writing from storage.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200320 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine0338ded2018-11-15 18:19:27 +0100321 * The library has already been initialized. It is no longer
322 * possible to call this function.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200323 */
Jaeden Ameroc7529c92019-08-19 11:08:04 +0100324psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200325 size_t seed_size);
326
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200327/** \addtogroup crypto_types
328 * @{
329 */
330
Gilles Peskinea1302192019-05-16 13:58:24 +0200331/** DSA public key.
332 *
333 * The import and export format is the
334 * representation of the public key `y = g^x mod p` as a big-endian byte
335 * string. The length of the byte string is the length of the base prime `p`
336 * in bytes.
337 */
Gilles Peskine7cfcb3f2019-12-04 18:58:44 +0100338#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x4002)
Gilles Peskinea1302192019-05-16 13:58:24 +0200339
340/** DSA key pair (private and public key).
341 *
342 * The import and export format is the
343 * representation of the private key `x` as a big-endian byte string. The
344 * length of the byte string is the private key size in bytes (leading zeroes
345 * are not stripped).
346 *
347 * Determinstic DSA key derivation with psa_generate_derived_key follows
348 * FIPS 186-4 §B.1.2: interpret the byte string as integer
349 * in big-endian order. Discard it if it is not in the range
350 * [0, *N* - 2] where *N* is the boundary of the private key domain
351 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
352 * or the order of the curve's base point for ECC).
353 * Add 1 to the resulting integer and use this as the private key *x*.
354 *
355 */
Gilles Peskine7cfcb3f2019-12-04 18:58:44 +0100356#define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t)0x7002)
Gilles Peskinea1302192019-05-16 13:58:24 +0200357
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200358/** Whether a key type is an DSA key (pair or public-only). */
359#define PSA_KEY_TYPE_IS_DSA(type) \
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200360 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200361
Bence Szépkútia2945512020-12-03 21:40:17 +0100362#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x06000400)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200363/** DSA signature with hashing.
364 *
365 * This is the signature scheme defined by FIPS 186-4,
366 * with a random per-message secret number (*k*).
367 *
368 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
369 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
370 * This includes #PSA_ALG_ANY_HASH
371 * when specifying the algorithm in a usage policy.
372 *
373 * \return The corresponding DSA signature algorithm.
374 * \return Unspecified if \p hash_alg is not a supported
375 * hash algorithm.
376 */
377#define PSA_ALG_DSA(hash_alg) \
378 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Bence Szépkútia2945512020-12-03 21:40:17 +0100379#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x06000500)
Gilles Peskine972630e2019-11-29 11:55:48 +0100380#define PSA_ALG_DSA_DETERMINISTIC_FLAG PSA_ALG_ECDSA_DETERMINISTIC_FLAG
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200381/** Deterministic DSA signature with hashing.
382 *
383 * This is the deterministic variant defined by RFC 6979 of
384 * the signature scheme defined by FIPS 186-4.
385 *
386 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
387 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
388 * This includes #PSA_ALG_ANY_HASH
389 * when specifying the algorithm in a usage policy.
390 *
391 * \return The corresponding DSA signature algorithm.
392 * \return Unspecified if \p hash_alg is not a supported
393 * hash algorithm.
394 */
395#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
396 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
397#define PSA_ALG_IS_DSA(alg) \
398 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
399 PSA_ALG_DSA_BASE)
400#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
401 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
402#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
403 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
404#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
405 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
406
407
408/* We need to expand the sample definition of this macro from
409 * the API definition. */
410#undef PSA_ALG_IS_HASH_AND_SIGN
411#define PSA_ALG_IS_HASH_AND_SIGN(alg) \
412 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
413 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg))
414
415/**@}*/
416
Gilles Peskine24f10f82019-05-16 12:18:32 +0200417/** \addtogroup attributes
418 * @{
419 */
420
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200421/** Custom Diffie-Hellman group.
422 *
Paul Elliott75e27032020-06-03 15:17:39 +0100423 * For keys of type #PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or
424 * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM), the group data comes
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200425 * from domain parameters set by psa_set_key_domain_parameters().
426 */
Paul Elliott75e27032020-06-03 15:17:39 +0100427#define PSA_DH_FAMILY_CUSTOM ((psa_dh_family_t) 0x7e)
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200428
429
Gilles Peskine24f10f82019-05-16 12:18:32 +0200430/**
431 * \brief Set domain parameters for a key.
432 *
433 * Some key types require additional domain parameters in addition to
434 * the key type identifier and the key size. Use this function instead
435 * of psa_set_key_type() when you need to specify domain parameters.
436 *
437 * The format for the required domain parameters varies based on the key type.
438 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200439 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine24f10f82019-05-16 12:18:32 +0200440 * the domain parameter data consists of the public exponent,
441 * represented as a big-endian integer with no leading zeros.
442 * This information is used when generating an RSA key pair.
443 * When importing a key, the public exponent is read from the imported
444 * key data and the exponent recorded in the attribute structure is ignored.
445 * As an exception, the public exponent 65537 is represented by an empty
446 * byte string.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200447 * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEY_PAIR),
Gilles Peskine24f10f82019-05-16 12:18:32 +0200448 * the `Dss-Parms` format as defined by RFC 3279 §2.3.2.
449 * ```
450 * Dss-Parms ::= SEQUENCE {
451 * p INTEGER,
452 * q INTEGER,
453 * g INTEGER
454 * }
455 * ```
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200456 * - For Diffie-Hellman key exchange keys
Paul Elliott75e27032020-06-03 15:17:39 +0100457 * (#PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or
458 * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM)), the
Gilles Peskine24f10f82019-05-16 12:18:32 +0200459 * `DomainParameters` format as defined by RFC 3279 §2.3.3.
460 * ```
461 * DomainParameters ::= SEQUENCE {
462 * p INTEGER, -- odd prime, p=jq +1
463 * g INTEGER, -- generator, g
464 * q INTEGER, -- factor of p-1
465 * j INTEGER OPTIONAL, -- subgroup factor
466 * validationParms ValidationParms OPTIONAL
467 * }
468 * ValidationParms ::= SEQUENCE {
469 * seed BIT STRING,
470 * pgenCounter INTEGER
471 * }
472 * ```
473 *
474 * \note This function may allocate memory or other resources.
475 * Once you have called this function on an attribute structure,
476 * you must call psa_reset_key_attributes() to free these resources.
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,out] attributes Attribute structure where the specified domain
482 * parameters will be stored.
483 * If this function fails, the content of
484 * \p attributes is not modified.
485 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
486 * \param[in] data Buffer containing the key domain parameters.
487 * The content of this buffer is interpreted
488 * according to \p type as described above.
489 * \param data_length Size of the \p data buffer in bytes.
490 *
491 * \retval #PSA_SUCCESS
492 * \retval #PSA_ERROR_INVALID_ARGUMENT
493 * \retval #PSA_ERROR_NOT_SUPPORTED
494 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
495 */
496psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
497 psa_key_type_t type,
498 const uint8_t *data,
499 size_t data_length);
500
501/**
502 * \brief Get domain parameters for a key.
503 *
504 * Get the domain parameters for a key with this function, if any. The format
505 * of the domain parameters written to \p data is specified in the
506 * documentation for psa_set_key_domain_parameters().
507 *
508 * \note This is an experimental extension to the interface. It may change
509 * in future versions of the library.
510 *
511 * \param[in] attributes The key attribute structure to query.
512 * \param[out] data On success, the key domain parameters.
513 * \param data_size Size of the \p data buffer in bytes.
514 * The buffer is guaranteed to be large
515 * enough if its size in bytes is at least
516 * the value given by
517 * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
518 * \param[out] data_length On success, the number of bytes
519 * that make up the key domain parameters data.
520 *
521 * \retval #PSA_SUCCESS
522 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
523 */
524psa_status_t psa_get_key_domain_parameters(
525 const psa_key_attributes_t *attributes,
526 uint8_t *data,
527 size_t data_size,
528 size_t *data_length);
529
530/** Safe output buffer size for psa_get_key_domain_parameters().
531 *
532 * This macro returns a compile-time constant if its arguments are
533 * compile-time constants.
534 *
535 * \warning This function may call its arguments multiple times or
536 * zero times, so you should not pass arguments that contain
537 * side effects.
538 *
539 * \note This is an experimental extension to the interface. It may change
540 * in future versions of the library.
541 *
542 * \param key_type A supported key type.
543 * \param key_bits The size of the key in bits.
544 *
545 * \return If the parameters are valid and supported, return
546 * a buffer size in bytes that guarantees that
547 * psa_get_key_domain_parameters() will not fail with
548 * #PSA_ERROR_BUFFER_TOO_SMALL.
549 * If the parameters are a valid combination that is not supported
Gilles Peskine27a983d2019-05-16 17:24:53 +0200550 * by the implementation, this macro shall return either a
Gilles Peskine24f10f82019-05-16 12:18:32 +0200551 * sensible size or 0.
552 * If the parameters are not valid, the
553 * return value is unspecified.
554 */
555#define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \
556 (PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) : \
557 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
558 PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
559 0)
560#define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
561 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/)
562#define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
563 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/)
564
565/**@}*/
566
Gilles Peskine5055b232019-12-12 17:49:31 +0100567/** \defgroup psa_tls_helpers TLS helper functions
568 * @{
569 */
570
571#if defined(MBEDTLS_ECP_C)
572#include <mbedtls/ecp.h>
573
574/** Convert an ECC curve identifier from the Mbed TLS encoding to PSA.
575 *
576 * \note This function is provided solely for the convenience of
577 * Mbed TLS and may be removed at any time without notice.
578 *
579 * \param grpid An Mbed TLS elliptic curve identifier
580 * (`MBEDTLS_ECP_DP_xxx`).
581 * \param[out] bits On success, the bit size of the curve.
582 *
583 * \return The corresponding PSA elliptic curve identifier
Paul Elliott8ff510a2020-06-02 17:19:28 +0100584 * (`PSA_ECC_FAMILY_xxx`).
Gilles Peskine5055b232019-12-12 17:49:31 +0100585 * \return \c 0 on failure (\p grpid is not recognized).
586 */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100587static inline psa_ecc_family_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid,
Darryl Green2f0eb512020-04-24 15:21:14 +0100588 size_t *bits )
589{
590 switch( grpid )
591 {
592 case MBEDTLS_ECP_DP_SECP192R1:
593 *bits = 192;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100594 return( PSA_ECC_FAMILY_SECP_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100595 case MBEDTLS_ECP_DP_SECP224R1:
596 *bits = 224;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100597 return( PSA_ECC_FAMILY_SECP_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100598 case MBEDTLS_ECP_DP_SECP256R1:
599 *bits = 256;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100600 return( PSA_ECC_FAMILY_SECP_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100601 case MBEDTLS_ECP_DP_SECP384R1:
602 *bits = 384;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100603 return( PSA_ECC_FAMILY_SECP_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100604 case MBEDTLS_ECP_DP_SECP521R1:
605 *bits = 521;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100606 return( PSA_ECC_FAMILY_SECP_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100607 case MBEDTLS_ECP_DP_BP256R1:
608 *bits = 256;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100609 return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100610 case MBEDTLS_ECP_DP_BP384R1:
611 *bits = 384;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100612 return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100613 case MBEDTLS_ECP_DP_BP512R1:
614 *bits = 512;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100615 return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100616 case MBEDTLS_ECP_DP_CURVE25519:
617 *bits = 255;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100618 return( PSA_ECC_FAMILY_MONTGOMERY );
Darryl Green2f0eb512020-04-24 15:21:14 +0100619 case MBEDTLS_ECP_DP_SECP192K1:
620 *bits = 192;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100621 return( PSA_ECC_FAMILY_SECP_K1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100622 case MBEDTLS_ECP_DP_SECP224K1:
623 *bits = 224;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100624 return( PSA_ECC_FAMILY_SECP_K1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100625 case MBEDTLS_ECP_DP_SECP256K1:
626 *bits = 256;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100627 return( PSA_ECC_FAMILY_SECP_K1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100628 case MBEDTLS_ECP_DP_CURVE448:
629 *bits = 448;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100630 return( PSA_ECC_FAMILY_MONTGOMERY );
Darryl Green2f0eb512020-04-24 15:21:14 +0100631 default:
632 *bits = 0;
633 return( 0 );
634 }
635}
Gilles Peskine5055b232019-12-12 17:49:31 +0100636
637/** Convert an ECC curve identifier from the PSA encoding to Mbed TLS.
638 *
639 * \note This function is provided solely for the convenience of
640 * Mbed TLS and may be removed at any time without notice.
641 *
642 * \param curve A PSA elliptic curve identifier
Paul Elliott8ff510a2020-06-02 17:19:28 +0100643 * (`PSA_ECC_FAMILY_xxx`).
Gilles Peskine5055b232019-12-12 17:49:31 +0100644 * \param byte_length The byte-length of a private key on \p curve.
645 *
646 * \return The corresponding Mbed TLS elliptic curve identifier
647 * (`MBEDTLS_ECP_DP_xxx`).
648 * \return #MBEDTLS_ECP_DP_NONE if \c curve is not recognized.
649 * \return #MBEDTLS_ECP_DP_NONE if \p byte_length is not
650 * correct for \p curve.
651 */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100652mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100653 size_t byte_length );
654#endif /* MBEDTLS_ECP_C */
655
656/**@}*/
657
Gilles Peskineb8af2282020-11-13 18:00:34 +0100658/** \defgroup psa_external_rng External random generator
659 * @{
660 */
661
662#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
663/** External random generator function, implemented by the platform.
664 *
665 * When the compile-time option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled,
666 * this function replaces Mbed TLS's entropy and DRBG modules for all
667 * random generation triggered via PSA crypto interfaces.
668 *
Gilles Peskineb663a602020-11-18 15:27:37 +0100669 * \note This random generator must deliver random numbers with cryptographic
670 * quality and high performance. It must supply unpredictable numbers
671 * with a uniform distribution. The implementation of this function
672 * is responsible for ensuring that the random generator is seeded
673 * with sufficient entropy. If you have a hardware TRNG which is slow
674 * or delivers non-uniform output, declare it as an entropy source
675 * with mbedtls_entropy_add_source() instead of enabling this option.
676 *
Gilles Peskineb8af2282020-11-13 18:00:34 +0100677 * \param[in,out] context Pointer to the random generator context.
678 * This is all-bits-zero on the first call
679 * and preserved between successive calls.
680 * \param[out] output Output buffer. On success, this buffer
681 * contains random data with a uniform
682 * distribution.
683 * \param output_size The size of the \p output buffer in bytes.
684 * \param[out] output_length On success, set this value to \p output_size.
685 *
686 * \retval #PSA_SUCCESS
Gilles Peskinee995b9b2020-11-30 12:08:00 +0100687 * Success. The output buffer contains \p output_size bytes of
688 * cryptographic-quality random data, and \c *output_length is
689 * set to \p output_size.
690 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
691 * The random generator requires extra entropy and there is no
692 * way to obtain entropy under current environment conditions.
693 * This error should not happen under normal circumstances since
694 * this function is responsible for obtaining as much entropy as
695 * it needs. However implementations of this function may return
696 * #PSA_ERROR_INSUFFICIENT_ENTROPY if there is no way to obtain
697 * entropy without blocking indefinitely.
Gilles Peskineb8af2282020-11-13 18:00:34 +0100698 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskinee995b9b2020-11-30 12:08:00 +0100699 * A failure of the random generator hardware that isn't covered
700 * by #PSA_ERROR_INSUFFICIENT_ENTROPY.
Gilles Peskineb8af2282020-11-13 18:00:34 +0100701 */
702psa_status_t mbedtls_psa_external_get_random(
703 mbedtls_psa_external_random_context_t *context,
704 uint8_t *output, size_t output_size, size_t *output_length );
705#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
706
707/**@}*/
708
Gilles Peskinee59236f2018-01-27 23:32:46 +0100709#ifdef __cplusplus
710}
711#endif
712
713#endif /* PSA_CRYPTO_EXTRA_H */