blob: f0c7979a81d9a89e9b5c1edbd43d03dc72707619 [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
Jaeden Amero5e6d24c2019-02-21 10:41:29 +000042
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020043/** \addtogroup attributes
44 * @{
45 */
46
47/** \brief Declare the enrollment algorithm for a key.
48 *
49 * An operation on a key may indifferently use the algorithm set with
50 * psa_set_key_algorithm() or with this function.
51 *
52 * \param[out] attributes The attribute structure to write to.
53 * \param alg2 A second algorithm that the key may be used
54 * for, in addition to the algorithm set with
55 * psa_set_key_algorithm().
56 *
57 * \warning Setting an enrollment algorithm is not recommended, because
58 * using the same key with different algorithms can allow some
59 * attacks based on arithmetic relations between different
60 * computations made with the same key, or can escalate harmless
61 * side channels into exploitable ones. Use this function only
Gilles Peskinef25c9ec2019-05-22 11:45:59 +020062 * if it is necessary to support a protocol for which it has been
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020063 * verified that the usage of the key with multiple algorithms
64 * is safe.
65 */
66static inline void psa_set_key_enrollment_algorithm(
67 psa_key_attributes_t *attributes,
68 psa_algorithm_t alg2)
69{
Gilles Peskine7e0cff92019-07-30 13:48:52 +020070 attributes->core.policy.alg2 = alg2;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020071}
72
73/** Retrieve the enrollment algorithm policy from key attributes.
74 *
75 * \param[in] attributes The key attribute structure to query.
76 *
77 * \return The enrollment algorithm stored in the attribute structure.
78 */
79static inline psa_algorithm_t psa_get_key_enrollment_algorithm(
80 const psa_key_attributes_t *attributes)
81{
Gilles Peskine7e0cff92019-07-30 13:48:52 +020082 return( attributes->core.policy.alg2 );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020083}
84
Gilles Peskinec8000c02019-08-02 20:15:51 +020085#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
86
87/** Retrieve the slot number where a key is stored.
88 *
89 * A slot number is only defined for keys that are stored in a secure
90 * element.
91 *
92 * This information is only useful if the secure element is not entirely
93 * managed through the PSA Cryptography API. It is up to the secure
94 * element driver to decide how PSA slot numbers map to any other interface
95 * that the secure element may have.
96 *
97 * \param[in] attributes The key attribute structure to query.
98 * \param[out] slot_number On success, the slot number containing the key.
99 *
100 * \retval #PSA_SUCCESS
101 * The key is located in a secure element, and \p *slot_number
102 * indicates the slot number that contains it.
103 * \retval #PSA_ERROR_NOT_PERMITTED
104 * The caller is not permitted to query the slot number.
105 * Mbed Crypto currently does not return this error.
106 * \retval #PSA_ERROR_INVALID_ARGUMENT
107 * The key is not located in a secure element.
108 */
109psa_status_t psa_get_key_slot_number(
110 const psa_key_attributes_t *attributes,
111 psa_key_slot_number_t *slot_number );
112
113/** Choose the slot number where a key is stored.
114 *
115 * This function declares a slot number in the specified attribute
116 * structure.
117 *
118 * A slot number is only meaningful for keys that are stored in a secure
119 * element. It is up to the secure element driver to decide how PSA slot
120 * numbers map to any other interface that the secure element may have.
121 *
122 * \note Setting a slot number in key attributes for a key creation can
123 * cause the following errors when creating the key:
124 * - #PSA_ERROR_NOT_SUPPORTED if the selected secure element does
125 * not support choosing a specific slot number.
126 * - #PSA_ERROR_NOT_PERMITTED if the caller is not permitted to
127 * choose slot numbers in general or to choose this specific slot.
128 * - #PSA_ERROR_INVALID_ARGUMENT if the chosen slot number is not
129 * valid in general or not valid for this specific key.
130 * - #PSA_ERROR_ALREADY_EXISTS if there is already a key in the
131 * selected slot.
132 *
133 * \param[out] attributes The attribute structure to write to.
134 * \param slot_number The slot number to set.
135 */
136static inline void psa_set_key_slot_number(
137 psa_key_attributes_t *attributes,
138 psa_key_slot_number_t slot_number )
139{
140 attributes->core.flags |= MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
141 attributes->slot_number = slot_number;
142}
143
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200144/** Remove the slot number attribute from a key attribute structure.
145 *
146 * This function undoes the action of psa_set_key_slot_number().
147 *
148 * \param[out] attributes The attribute structure to write to.
149 */
150static inline void psa_clear_key_slot_number(
151 psa_key_attributes_t *attributes )
152{
153 attributes->core.flags &= ~MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
154}
155
Gilles Peskined7729582019-08-05 15:55:54 +0200156/** Register a key that is already present in a secure element.
157 *
158 * The key must be located in a secure element designated by the
159 * lifetime field in \p attributes, in the slot set with
160 * psa_set_key_slot_number() in the attribute structure.
161 * This function makes the key available through the key identifier
162 * specified in \p attributes.
163 *
164 * \param[in] attributes The attributes of the existing key.
165 *
166 * \retval #PSA_SUCCESS
167 * The key was successfully registered.
168 * Note that depending on the design of the driver, this may or may
169 * not guarantee that a key actually exists in the designated slot
170 * and is compatible with the specified attributes.
171 * \retval #PSA_ERROR_ALREADY_EXISTS
172 * There is already a key with the identifier specified in
173 * \p attributes.
Gilles Peskine3efcebb2019-10-01 14:18:35 +0200174 * \retval #PSA_ERROR_NOT_SUPPORTED
175 * The secure element driver for the specified lifetime does not
176 * support registering a key.
Gilles Peskined7729582019-08-05 15:55:54 +0200177 * \retval #PSA_ERROR_INVALID_ARGUMENT
178 * \p attributes specifies a lifetime which is not located
179 * in a secure element.
180 * \retval #PSA_ERROR_INVALID_ARGUMENT
181 * No slot number is specified in \p attributes,
182 * or the specified slot number is not valid.
183 * \retval #PSA_ERROR_NOT_PERMITTED
184 * The caller is not authorized to register the specified key slot.
185 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
186 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
187 * \retval #PSA_ERROR_HARDWARE_FAILURE
188 * \retval #PSA_ERROR_CORRUPTION_DETECTED
189 * \retval #PSA_ERROR_BAD_STATE
190 * The library has not been previously initialized by psa_crypto_init().
191 * It is implementation-dependent whether a failure to initialize
192 * results in this error code.
193 */
194psa_status_t mbedtls_psa_register_se_key(
195 const psa_key_attributes_t *attributes);
196
Gilles Peskinec8000c02019-08-02 20:15:51 +0200197#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
198
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200199/**@}*/
200
Gilles Peskinee59236f2018-01-27 23:32:46 +0100201/**
202 * \brief Library deinitialization.
203 *
204 * This function clears all data associated with the PSA layer,
205 * including the whole key store.
206 *
207 * This is an Mbed TLS extension.
208 */
209void mbedtls_psa_crypto_free( void );
210
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200211/** \brief Statistics about
212 * resource consumption related to the PSA keystore.
213 *
214 * \note The content of this structure is not part of the stable API and ABI
215 * of Mbed Crypto and may change arbitrarily from version to version.
216 */
217typedef struct mbedtls_psa_stats_s
218{
219 /** Number of slots containing key material for a volatile key. */
220 size_t volatile_slots;
221 /** Number of slots containing key material for a key which is in
222 * internal persistent storage. */
223 size_t persistent_slots;
224 /** Number of slots containing a reference to a key in a
225 * secure element. */
226 size_t external_slots;
227 /** Number of slots which are occupied, but do not contain
228 * key material yet. */
229 size_t half_filled_slots;
230 /** Number of slots that contain cache data. */
231 size_t cache_slots;
232 /** Number of slots that are not used for anything. */
233 size_t empty_slots;
234 /** Largest key id value among open keys in internal persistent storage. */
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100235 psa_app_key_id_t max_open_internal_key_id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200236 /** Largest key id value among open keys in secure elements. */
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100237 psa_app_key_id_t max_open_external_key_id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200238} mbedtls_psa_stats_t;
239
240/** \brief Get statistics about
241 * resource consumption related to the PSA keystore.
242 *
243 * \note When Mbed Crypto is built as part of a service, with isolation
244 * between the application and the keystore, the service may or
245 * may not expose this function.
246 */
247void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats );
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200248
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200249/**
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100250 * \brief Inject an initial entropy seed for the random generator into
251 * secure storage.
Gilles Peskine0338ded2018-11-15 18:19:27 +0100252 *
253 * This function injects data to be used as a seed for the random generator
254 * used by the PSA Crypto implementation. On devices that lack a trusted
255 * entropy source (preferably a hardware random number generator),
256 * the Mbed PSA Crypto implementation uses this value to seed its
257 * random generator.
258 *
259 * On devices without a trusted entropy source, this function must be
260 * called exactly once in the lifetime of the device. On devices with
261 * a trusted entropy source, calling this function is optional.
262 * In all cases, this function may only be called before calling any
263 * other function in the PSA Crypto API, including psa_crypto_init().
264 *
265 * When this function returns successfully, it populates a file in
266 * persistent storage. Once the file has been created, this function
267 * can no longer succeed.
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100268 *
269 * If any error occurs, this function does not change the system state.
270 * You can call this function again after correcting the reason for the
271 * error if possible.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200272 *
273 * \warning This function **can** fail! Callers MUST check the return status.
274 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100275 * \warning If you use this function, you should use it as part of a
276 * factory provisioning process. The value of the injected seed
277 * is critical to the security of the device. It must be
278 * *secret*, *unpredictable* and (statistically) *unique per device*.
279 * You should be generate it randomly using a cryptographically
280 * secure random generator seeded from trusted entropy sources.
281 * You should transmit it securely to the device and ensure
282 * that its value is not leaked or stored anywhere beyond the
283 * needs of transmitting it from the point of generation to
284 * the call of this function, and erase all copies of the value
285 * once this function returns.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200286 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100287 * This is an Mbed TLS extension.
288 *
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200289 * \note This function is only available on the following platforms:
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100290 * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled.
291 * Note that you must provide compatible implementations of
292 * mbedtls_nv_seed_read and mbedtls_nv_seed_write.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200293 * * In a client-server integration of PSA Cryptography, on the client side,
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200294 * if the server supports this feature.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200295 * \param[in] seed Buffer containing the seed value to inject.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200296 * \param[in] seed_size Size of the \p seed buffer.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200297 * The size of the seed in bytes must be greater
298 * or equal to both #MBEDTLS_ENTROPY_MIN_PLATFORM
299 * and #MBEDTLS_ENTROPY_BLOCK_SIZE.
300 * It must be less or equal to
301 * #MBEDTLS_ENTROPY_MAX_SEED_SIZE.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200302 *
303 * \retval #PSA_SUCCESS
Gilles Peskine0338ded2018-11-15 18:19:27 +0100304 * The seed value was injected successfully. The random generator
305 * of the PSA Crypto implementation is now ready for use.
306 * You may now call psa_crypto_init() and use the PSA Crypto
307 * implementation.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200308 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100309 * \p seed_size is out of range.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200310 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine0338ded2018-11-15 18:19:27 +0100311 * There was a failure reading or writing from storage.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200312 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine0338ded2018-11-15 18:19:27 +0100313 * The library has already been initialized. It is no longer
314 * possible to call this function.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200315 */
Jaeden Ameroc7529c92019-08-19 11:08:04 +0100316psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200317 size_t seed_size);
318
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200319/** \addtogroup crypto_types
320 * @{
321 */
322
Gilles Peskinea1302192019-05-16 13:58:24 +0200323/** DSA public key.
324 *
325 * The import and export format is the
326 * representation of the public key `y = g^x mod p` as a big-endian byte
327 * string. The length of the byte string is the length of the base prime `p`
328 * in bytes.
329 */
Gilles Peskine7cfcb3f2019-12-04 18:58:44 +0100330#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x4002)
Gilles Peskinea1302192019-05-16 13:58:24 +0200331
332/** DSA key pair (private and public key).
333 *
334 * The import and export format is the
335 * representation of the private key `x` as a big-endian byte string. The
336 * length of the byte string is the private key size in bytes (leading zeroes
337 * are not stripped).
338 *
339 * Determinstic DSA key derivation with psa_generate_derived_key follows
340 * FIPS 186-4 §B.1.2: interpret the byte string as integer
341 * in big-endian order. Discard it if it is not in the range
342 * [0, *N* - 2] where *N* is the boundary of the private key domain
343 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
344 * or the order of the curve's base point for ECC).
345 * Add 1 to the resulting integer and use this as the private key *x*.
346 *
347 */
Gilles Peskine7cfcb3f2019-12-04 18:58:44 +0100348#define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t)0x7002)
Gilles Peskinea1302192019-05-16 13:58:24 +0200349
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200350/** Whether a key type is an DSA key (pair or public-only). */
351#define PSA_KEY_TYPE_IS_DSA(type) \
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200352 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200353
354#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
355/** DSA signature with hashing.
356 *
357 * This is the signature scheme defined by FIPS 186-4,
358 * with a random per-message secret number (*k*).
359 *
360 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
361 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
362 * This includes #PSA_ALG_ANY_HASH
363 * when specifying the algorithm in a usage policy.
364 *
365 * \return The corresponding DSA signature algorithm.
366 * \return Unspecified if \p hash_alg is not a supported
367 * hash algorithm.
368 */
369#define PSA_ALG_DSA(hash_alg) \
370 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
371#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
Gilles Peskine972630e2019-11-29 11:55:48 +0100372#define PSA_ALG_DSA_DETERMINISTIC_FLAG PSA_ALG_ECDSA_DETERMINISTIC_FLAG
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200373/** Deterministic DSA signature with hashing.
374 *
375 * This is the deterministic variant defined by RFC 6979 of
376 * the signature scheme defined by FIPS 186-4.
377 *
378 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
379 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
380 * This includes #PSA_ALG_ANY_HASH
381 * when specifying the algorithm in a usage policy.
382 *
383 * \return The corresponding DSA signature algorithm.
384 * \return Unspecified if \p hash_alg is not a supported
385 * hash algorithm.
386 */
387#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
388 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
389#define PSA_ALG_IS_DSA(alg) \
390 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
391 PSA_ALG_DSA_BASE)
392#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
393 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
394#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
395 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
396#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
397 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
398
399
400/* We need to expand the sample definition of this macro from
401 * the API definition. */
402#undef PSA_ALG_IS_HASH_AND_SIGN
403#define PSA_ALG_IS_HASH_AND_SIGN(alg) \
404 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
405 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg))
406
407/**@}*/
408
Gilles Peskine24f10f82019-05-16 12:18:32 +0200409/** \addtogroup attributes
410 * @{
411 */
412
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200413/** Custom Diffie-Hellman group.
414 *
Paul Elliott75e27032020-06-03 15:17:39 +0100415 * For keys of type #PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or
416 * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM), the group data comes
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200417 * from domain parameters set by psa_set_key_domain_parameters().
418 */
Paul Elliott75e27032020-06-03 15:17:39 +0100419#define PSA_DH_FAMILY_CUSTOM ((psa_dh_family_t) 0x7e)
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200420
421
Gilles Peskine24f10f82019-05-16 12:18:32 +0200422/**
423 * \brief Set domain parameters for a key.
424 *
425 * Some key types require additional domain parameters in addition to
426 * the key type identifier and the key size. Use this function instead
427 * of psa_set_key_type() when you need to specify domain parameters.
428 *
429 * The format for the required domain parameters varies based on the key type.
430 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200431 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine24f10f82019-05-16 12:18:32 +0200432 * the domain parameter data consists of the public exponent,
433 * represented as a big-endian integer with no leading zeros.
434 * This information is used when generating an RSA key pair.
435 * When importing a key, the public exponent is read from the imported
436 * key data and the exponent recorded in the attribute structure is ignored.
437 * As an exception, the public exponent 65537 is represented by an empty
438 * byte string.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200439 * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEY_PAIR),
Gilles Peskine24f10f82019-05-16 12:18:32 +0200440 * the `Dss-Parms` format as defined by RFC 3279 §2.3.2.
441 * ```
442 * Dss-Parms ::= SEQUENCE {
443 * p INTEGER,
444 * q INTEGER,
445 * g INTEGER
446 * }
447 * ```
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200448 * - For Diffie-Hellman key exchange keys
Paul Elliott75e27032020-06-03 15:17:39 +0100449 * (#PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or
450 * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM)), the
Gilles Peskine24f10f82019-05-16 12:18:32 +0200451 * `DomainParameters` format as defined by RFC 3279 §2.3.3.
452 * ```
453 * DomainParameters ::= SEQUENCE {
454 * p INTEGER, -- odd prime, p=jq +1
455 * g INTEGER, -- generator, g
456 * q INTEGER, -- factor of p-1
457 * j INTEGER OPTIONAL, -- subgroup factor
458 * validationParms ValidationParms OPTIONAL
459 * }
460 * ValidationParms ::= SEQUENCE {
461 * seed BIT STRING,
462 * pgenCounter INTEGER
463 * }
464 * ```
465 *
466 * \note This function may allocate memory or other resources.
467 * Once you have called this function on an attribute structure,
468 * you must call psa_reset_key_attributes() to free these resources.
469 *
470 * \note This is an experimental extension to the interface. It may change
471 * in future versions of the library.
472 *
473 * \param[in,out] attributes Attribute structure where the specified domain
474 * parameters will be stored.
475 * If this function fails, the content of
476 * \p attributes is not modified.
477 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
478 * \param[in] data Buffer containing the key domain parameters.
479 * The content of this buffer is interpreted
480 * according to \p type as described above.
481 * \param data_length Size of the \p data buffer in bytes.
482 *
483 * \retval #PSA_SUCCESS
484 * \retval #PSA_ERROR_INVALID_ARGUMENT
485 * \retval #PSA_ERROR_NOT_SUPPORTED
486 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
487 */
488psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
489 psa_key_type_t type,
490 const uint8_t *data,
491 size_t data_length);
492
493/**
494 * \brief Get domain parameters for a key.
495 *
496 * Get the domain parameters for a key with this function, if any. The format
497 * of the domain parameters written to \p data is specified in the
498 * documentation for psa_set_key_domain_parameters().
499 *
500 * \note This is an experimental extension to the interface. It may change
501 * in future versions of the library.
502 *
503 * \param[in] attributes The key attribute structure to query.
504 * \param[out] data On success, the key domain parameters.
505 * \param data_size Size of the \p data buffer in bytes.
506 * The buffer is guaranteed to be large
507 * enough if its size in bytes is at least
508 * the value given by
509 * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
510 * \param[out] data_length On success, the number of bytes
511 * that make up the key domain parameters data.
512 *
513 * \retval #PSA_SUCCESS
514 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
515 */
516psa_status_t psa_get_key_domain_parameters(
517 const psa_key_attributes_t *attributes,
518 uint8_t *data,
519 size_t data_size,
520 size_t *data_length);
521
522/** Safe output buffer size for psa_get_key_domain_parameters().
523 *
524 * This macro returns a compile-time constant if its arguments are
525 * compile-time constants.
526 *
527 * \warning This function may call its arguments multiple times or
528 * zero times, so you should not pass arguments that contain
529 * side effects.
530 *
531 * \note This is an experimental extension to the interface. It may change
532 * in future versions of the library.
533 *
534 * \param key_type A supported key type.
535 * \param key_bits The size of the key in bits.
536 *
537 * \return If the parameters are valid and supported, return
538 * a buffer size in bytes that guarantees that
539 * psa_get_key_domain_parameters() will not fail with
540 * #PSA_ERROR_BUFFER_TOO_SMALL.
541 * If the parameters are a valid combination that is not supported
Gilles Peskine27a983d2019-05-16 17:24:53 +0200542 * by the implementation, this macro shall return either a
Gilles Peskine24f10f82019-05-16 12:18:32 +0200543 * sensible size or 0.
544 * If the parameters are not valid, the
545 * return value is unspecified.
546 */
547#define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \
548 (PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) : \
549 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
550 PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
551 0)
552#define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
553 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/)
554#define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
555 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/)
556
557/**@}*/
558
Gilles Peskine5055b232019-12-12 17:49:31 +0100559/** \defgroup psa_tls_helpers TLS helper functions
560 * @{
561 */
562
563#if defined(MBEDTLS_ECP_C)
564#include <mbedtls/ecp.h>
565
566/** Convert an ECC curve identifier from the Mbed TLS encoding to PSA.
567 *
568 * \note This function is provided solely for the convenience of
569 * Mbed TLS and may be removed at any time without notice.
570 *
571 * \param grpid An Mbed TLS elliptic curve identifier
572 * (`MBEDTLS_ECP_DP_xxx`).
573 * \param[out] bits On success, the bit size of the curve.
574 *
575 * \return The corresponding PSA elliptic curve identifier
Paul Elliott8ff510a2020-06-02 17:19:28 +0100576 * (`PSA_ECC_FAMILY_xxx`).
Gilles Peskine5055b232019-12-12 17:49:31 +0100577 * \return \c 0 on failure (\p grpid is not recognized).
578 */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100579static inline psa_ecc_family_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid,
Darryl Green2f0eb512020-04-24 15:21:14 +0100580 size_t *bits )
581{
582 switch( grpid )
583 {
584 case MBEDTLS_ECP_DP_SECP192R1:
585 *bits = 192;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100586 return( PSA_ECC_FAMILY_SECP_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100587 case MBEDTLS_ECP_DP_SECP224R1:
588 *bits = 224;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100589 return( PSA_ECC_FAMILY_SECP_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100590 case MBEDTLS_ECP_DP_SECP256R1:
591 *bits = 256;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100592 return( PSA_ECC_FAMILY_SECP_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100593 case MBEDTLS_ECP_DP_SECP384R1:
594 *bits = 384;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100595 return( PSA_ECC_FAMILY_SECP_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100596 case MBEDTLS_ECP_DP_SECP521R1:
597 *bits = 521;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100598 return( PSA_ECC_FAMILY_SECP_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100599 case MBEDTLS_ECP_DP_BP256R1:
600 *bits = 256;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100601 return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100602 case MBEDTLS_ECP_DP_BP384R1:
603 *bits = 384;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100604 return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100605 case MBEDTLS_ECP_DP_BP512R1:
606 *bits = 512;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100607 return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100608 case MBEDTLS_ECP_DP_CURVE25519:
609 *bits = 255;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100610 return( PSA_ECC_FAMILY_MONTGOMERY );
Darryl Green2f0eb512020-04-24 15:21:14 +0100611 case MBEDTLS_ECP_DP_SECP192K1:
612 *bits = 192;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100613 return( PSA_ECC_FAMILY_SECP_K1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100614 case MBEDTLS_ECP_DP_SECP224K1:
615 *bits = 224;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100616 return( PSA_ECC_FAMILY_SECP_K1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100617 case MBEDTLS_ECP_DP_SECP256K1:
618 *bits = 256;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100619 return( PSA_ECC_FAMILY_SECP_K1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100620 case MBEDTLS_ECP_DP_CURVE448:
621 *bits = 448;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100622 return( PSA_ECC_FAMILY_MONTGOMERY );
Darryl Green2f0eb512020-04-24 15:21:14 +0100623 default:
624 *bits = 0;
625 return( 0 );
626 }
627}
Gilles Peskine5055b232019-12-12 17:49:31 +0100628
629/** Convert an ECC curve identifier from the PSA encoding to Mbed TLS.
630 *
631 * \note This function is provided solely for the convenience of
632 * Mbed TLS and may be removed at any time without notice.
633 *
634 * \param curve A PSA elliptic curve identifier
Paul Elliott8ff510a2020-06-02 17:19:28 +0100635 * (`PSA_ECC_FAMILY_xxx`).
Gilles Peskine5055b232019-12-12 17:49:31 +0100636 * \param byte_length The byte-length of a private key on \p curve.
637 *
638 * \return The corresponding Mbed TLS elliptic curve identifier
639 * (`MBEDTLS_ECP_DP_xxx`).
640 * \return #MBEDTLS_ECP_DP_NONE if \c curve is not recognized.
641 * \return #MBEDTLS_ECP_DP_NONE if \p byte_length is not
642 * correct for \p curve.
643 */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100644mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100645 size_t byte_length );
646#endif /* MBEDTLS_ECP_C */
647
648/**@}*/
649
Gilles Peskinee59236f2018-01-27 23:32:46 +0100650#ifdef __cplusplus
651}
652#endif
653
654#endif /* PSA_CRYPTO_EXTRA_H */