blob: c5313d619ef53d0901753335a4275e3c0d0e3b74 [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/*
12 * Copyright (C) 2018, ARM Limited, All Rights Reserved
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 * This file is part of mbed TLS (https://tls.mbed.org)
28 */
29
30#ifndef PSA_CRYPTO_EXTRA_H
31#define PSA_CRYPTO_EXTRA_H
32
Jaeden Amero81cefed2019-02-25 08:51:27 +000033#include "mbedtls/platform_util.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/*
43 * Deprecated PSA Crypto error code definitions
44 */
45#if !defined(MBEDTLS_DEPRECATED_REMOVED)
46#define PSA_ERROR_UNKNOWN_ERROR \
47 MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( PSA_ERROR_GENERIC_ERROR )
Jaeden Amero5e6d24c2019-02-21 10:41:29 +000048#define PSA_ERROR_OCCUPIED_SLOT \
49 MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( PSA_ERROR_ALREADY_EXISTS )
Jaeden Amero5e6d24c2019-02-21 10:41:29 +000050#define PSA_ERROR_EMPTY_SLOT \
51 MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( PSA_ERROR_DOES_NOT_EXIST )
Jaeden Amero5e6d24c2019-02-21 10:41:29 +000052#define PSA_ERROR_INSUFFICIENT_CAPACITY \
53 MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( PSA_ERROR_INSUFFICIENT_DATA )
Gilles Peskine19835122019-05-17 12:06:55 +020054#define PSA_ERROR_TAMPERING_DETECTED \
55 MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( PSA_ERROR_CORRUPTION_DETECTED )
Jaeden Amero5e6d24c2019-02-21 10:41:29 +000056#endif
57
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020058/** \addtogroup attributes
59 * @{
60 */
61
62/** \brief Declare the enrollment algorithm for a key.
63 *
64 * An operation on a key may indifferently use the algorithm set with
65 * psa_set_key_algorithm() or with this function.
66 *
67 * \param[out] attributes The attribute structure to write to.
68 * \param alg2 A second algorithm that the key may be used
69 * for, in addition to the algorithm set with
70 * psa_set_key_algorithm().
71 *
72 * \warning Setting an enrollment algorithm is not recommended, because
73 * using the same key with different algorithms can allow some
74 * attacks based on arithmetic relations between different
75 * computations made with the same key, or can escalate harmless
76 * side channels into exploitable ones. Use this function only
Gilles Peskinef25c9ec2019-05-22 11:45:59 +020077 * if it is necessary to support a protocol for which it has been
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020078 * verified that the usage of the key with multiple algorithms
79 * is safe.
80 */
81static inline void psa_set_key_enrollment_algorithm(
82 psa_key_attributes_t *attributes,
83 psa_algorithm_t alg2)
84{
Gilles Peskine7e0cff92019-07-30 13:48:52 +020085 attributes->core.policy.alg2 = alg2;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020086}
87
88/** Retrieve the enrollment algorithm policy from key attributes.
89 *
90 * \param[in] attributes The key attribute structure to query.
91 *
92 * \return The enrollment algorithm stored in the attribute structure.
93 */
94static inline psa_algorithm_t psa_get_key_enrollment_algorithm(
95 const psa_key_attributes_t *attributes)
96{
Gilles Peskine7e0cff92019-07-30 13:48:52 +020097 return( attributes->core.policy.alg2 );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020098}
99
Gilles Peskinec8000c02019-08-02 20:15:51 +0200100#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
101
102/** Retrieve the slot number where a key is stored.
103 *
104 * A slot number is only defined for keys that are stored in a secure
105 * element.
106 *
107 * This information is only useful if the secure element is not entirely
108 * managed through the PSA Cryptography API. It is up to the secure
109 * element driver to decide how PSA slot numbers map to any other interface
110 * that the secure element may have.
111 *
112 * \param[in] attributes The key attribute structure to query.
113 * \param[out] slot_number On success, the slot number containing the key.
114 *
115 * \retval #PSA_SUCCESS
116 * The key is located in a secure element, and \p *slot_number
117 * indicates the slot number that contains it.
118 * \retval #PSA_ERROR_NOT_PERMITTED
119 * The caller is not permitted to query the slot number.
120 * Mbed Crypto currently does not return this error.
121 * \retval #PSA_ERROR_INVALID_ARGUMENT
122 * The key is not located in a secure element.
123 */
124psa_status_t psa_get_key_slot_number(
125 const psa_key_attributes_t *attributes,
126 psa_key_slot_number_t *slot_number );
127
128/** Choose the slot number where a key is stored.
129 *
130 * This function declares a slot number in the specified attribute
131 * structure.
132 *
133 * A slot number is only meaningful for keys that are stored in a secure
134 * element. It is up to the secure element driver to decide how PSA slot
135 * numbers map to any other interface that the secure element may have.
136 *
137 * \note Setting a slot number in key attributes for a key creation can
138 * cause the following errors when creating the key:
139 * - #PSA_ERROR_NOT_SUPPORTED if the selected secure element does
140 * not support choosing a specific slot number.
141 * - #PSA_ERROR_NOT_PERMITTED if the caller is not permitted to
142 * choose slot numbers in general or to choose this specific slot.
143 * - #PSA_ERROR_INVALID_ARGUMENT if the chosen slot number is not
144 * valid in general or not valid for this specific key.
145 * - #PSA_ERROR_ALREADY_EXISTS if there is already a key in the
146 * selected slot.
147 *
148 * \param[out] attributes The attribute structure to write to.
149 * \param slot_number The slot number to set.
150 */
151static inline void psa_set_key_slot_number(
152 psa_key_attributes_t *attributes,
153 psa_key_slot_number_t slot_number )
154{
155 attributes->core.flags |= MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
156 attributes->slot_number = slot_number;
157}
158
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200159/** Remove the slot number attribute from a key attribute structure.
160 *
161 * This function undoes the action of psa_set_key_slot_number().
162 *
163 * \param[out] attributes The attribute structure to write to.
164 */
165static inline void psa_clear_key_slot_number(
166 psa_key_attributes_t *attributes )
167{
168 attributes->core.flags &= ~MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
169}
170
Gilles Peskined7729582019-08-05 15:55:54 +0200171/** Register a key that is already present in a secure element.
172 *
173 * The key must be located in a secure element designated by the
174 * lifetime field in \p attributes, in the slot set with
175 * psa_set_key_slot_number() in the attribute structure.
176 * This function makes the key available through the key identifier
177 * specified in \p attributes.
178 *
179 * \param[in] attributes The attributes of the existing key.
180 *
181 * \retval #PSA_SUCCESS
182 * The key was successfully registered.
183 * Note that depending on the design of the driver, this may or may
184 * not guarantee that a key actually exists in the designated slot
185 * and is compatible with the specified attributes.
186 * \retval #PSA_ERROR_ALREADY_EXISTS
187 * There is already a key with the identifier specified in
188 * \p attributes.
Gilles Peskine3efcebb2019-10-01 14:18:35 +0200189 * \retval #PSA_ERROR_NOT_SUPPORTED
190 * The secure element driver for the specified lifetime does not
191 * support registering a key.
Gilles Peskined7729582019-08-05 15:55:54 +0200192 * \retval #PSA_ERROR_INVALID_ARGUMENT
193 * \p attributes specifies a lifetime which is not located
194 * in a secure element.
195 * \retval #PSA_ERROR_INVALID_ARGUMENT
196 * No slot number is specified in \p attributes,
197 * or the specified slot number is not valid.
198 * \retval #PSA_ERROR_NOT_PERMITTED
199 * The caller is not authorized to register the specified key slot.
200 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
201 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
202 * \retval #PSA_ERROR_HARDWARE_FAILURE
203 * \retval #PSA_ERROR_CORRUPTION_DETECTED
204 * \retval #PSA_ERROR_BAD_STATE
205 * The library has not been previously initialized by psa_crypto_init().
206 * It is implementation-dependent whether a failure to initialize
207 * results in this error code.
208 */
209psa_status_t mbedtls_psa_register_se_key(
210 const psa_key_attributes_t *attributes);
211
Gilles Peskinec8000c02019-08-02 20:15:51 +0200212#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
213
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200214/**@}*/
215
Gilles Peskinee59236f2018-01-27 23:32:46 +0100216/**
217 * \brief Library deinitialization.
218 *
219 * This function clears all data associated with the PSA layer,
220 * including the whole key store.
221 *
222 * This is an Mbed TLS extension.
223 */
224void mbedtls_psa_crypto_free( void );
225
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200226/** \brief Statistics about
227 * resource consumption related to the PSA keystore.
228 *
229 * \note The content of this structure is not part of the stable API and ABI
230 * of Mbed Crypto and may change arbitrarily from version to version.
231 */
232typedef struct mbedtls_psa_stats_s
233{
234 /** Number of slots containing key material for a volatile key. */
235 size_t volatile_slots;
236 /** Number of slots containing key material for a key which is in
237 * internal persistent storage. */
238 size_t persistent_slots;
239 /** Number of slots containing a reference to a key in a
240 * secure element. */
241 size_t external_slots;
242 /** Number of slots which are occupied, but do not contain
243 * key material yet. */
244 size_t half_filled_slots;
245 /** Number of slots that contain cache data. */
246 size_t cache_slots;
247 /** Number of slots that are not used for anything. */
248 size_t empty_slots;
249 /** Largest key id value among open keys in internal persistent storage. */
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100250 psa_app_key_id_t max_open_internal_key_id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200251 /** Largest key id value among open keys in secure elements. */
Jaeden Amero6fa62a52019-08-20 17:43:48 +0100252 psa_app_key_id_t max_open_external_key_id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200253} mbedtls_psa_stats_t;
254
255/** \brief Get statistics about
256 * resource consumption related to the PSA keystore.
257 *
258 * \note When Mbed Crypto is built as part of a service, with isolation
259 * between the application and the keystore, the service may or
260 * may not expose this function.
261 */
262void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats );
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200263
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200264/**
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100265 * \brief Inject an initial entropy seed for the random generator into
266 * secure storage.
Gilles Peskine0338ded2018-11-15 18:19:27 +0100267 *
268 * This function injects data to be used as a seed for the random generator
269 * used by the PSA Crypto implementation. On devices that lack a trusted
270 * entropy source (preferably a hardware random number generator),
271 * the Mbed PSA Crypto implementation uses this value to seed its
272 * random generator.
273 *
274 * On devices without a trusted entropy source, this function must be
275 * called exactly once in the lifetime of the device. On devices with
276 * a trusted entropy source, calling this function is optional.
277 * In all cases, this function may only be called before calling any
278 * other function in the PSA Crypto API, including psa_crypto_init().
279 *
280 * When this function returns successfully, it populates a file in
281 * persistent storage. Once the file has been created, this function
282 * can no longer succeed.
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100283 *
284 * If any error occurs, this function does not change the system state.
285 * You can call this function again after correcting the reason for the
286 * error if possible.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200287 *
288 * \warning This function **can** fail! Callers MUST check the return status.
289 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100290 * \warning If you use this function, you should use it as part of a
291 * factory provisioning process. The value of the injected seed
292 * is critical to the security of the device. It must be
293 * *secret*, *unpredictable* and (statistically) *unique per device*.
294 * You should be generate it randomly using a cryptographically
295 * secure random generator seeded from trusted entropy sources.
296 * You should transmit it securely to the device and ensure
297 * that its value is not leaked or stored anywhere beyond the
298 * needs of transmitting it from the point of generation to
299 * the call of this function, and erase all copies of the value
300 * once this function returns.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200301 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100302 * This is an Mbed TLS extension.
303 *
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200304 * \note This function is only available on the following platforms:
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100305 * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled.
306 * Note that you must provide compatible implementations of
307 * mbedtls_nv_seed_read and mbedtls_nv_seed_write.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200308 * * In a client-server integration of PSA Cryptography, on the client side,
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200309 * if the server supports this feature.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200310 * \param[in] seed Buffer containing the seed value to inject.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200311 * \param[in] seed_size Size of the \p seed buffer.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200312 * The size of the seed in bytes must be greater
313 * or equal to both #MBEDTLS_ENTROPY_MIN_PLATFORM
314 * and #MBEDTLS_ENTROPY_BLOCK_SIZE.
315 * It must be less or equal to
316 * #MBEDTLS_ENTROPY_MAX_SEED_SIZE.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200317 *
318 * \retval #PSA_SUCCESS
Gilles Peskine0338ded2018-11-15 18:19:27 +0100319 * The seed value was injected successfully. The random generator
320 * of the PSA Crypto implementation is now ready for use.
321 * You may now call psa_crypto_init() and use the PSA Crypto
322 * implementation.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200323 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100324 * \p seed_size is out of range.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200325 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine0338ded2018-11-15 18:19:27 +0100326 * There was a failure reading or writing from storage.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200327 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine0338ded2018-11-15 18:19:27 +0100328 * The library has already been initialized. It is no longer
329 * possible to call this function.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200330 */
Jaeden Ameroc7529c92019-08-19 11:08:04 +0100331psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200332 size_t seed_size);
333
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200334/** \addtogroup crypto_types
335 * @{
336 */
337
Gilles Peskinea1302192019-05-16 13:58:24 +0200338/** DSA public key.
339 *
340 * The import and export format is the
341 * representation of the public key `y = g^x mod p` as a big-endian byte
342 * string. The length of the byte string is the length of the base prime `p`
343 * in bytes.
344 */
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200345#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x60020000)
Gilles Peskinea1302192019-05-16 13:58:24 +0200346
347/** DSA key pair (private and public key).
348 *
349 * The import and export format is the
350 * representation of the private key `x` as a big-endian byte string. The
351 * length of the byte string is the private key size in bytes (leading zeroes
352 * are not stripped).
353 *
354 * Determinstic DSA key derivation with psa_generate_derived_key follows
355 * FIPS 186-4 §B.1.2: interpret the byte string as integer
356 * in big-endian order. Discard it if it is not in the range
357 * [0, *N* - 2] where *N* is the boundary of the private key domain
358 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
359 * or the order of the curve's base point for ECC).
360 * Add 1 to the resulting integer and use this as the private key *x*.
361 *
362 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200363#define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t)0x70020000)
Gilles Peskinea1302192019-05-16 13:58:24 +0200364
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200365/** Whether a key type is an DSA key (pair or public-only). */
366#define PSA_KEY_TYPE_IS_DSA(type) \
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200367 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200368
369#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
370/** DSA signature with hashing.
371 *
372 * This is the signature scheme defined by FIPS 186-4,
373 * with a random per-message secret number (*k*).
374 *
375 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
376 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
377 * This includes #PSA_ALG_ANY_HASH
378 * when specifying the algorithm in a usage policy.
379 *
380 * \return The corresponding DSA signature algorithm.
381 * \return Unspecified if \p hash_alg is not a supported
382 * hash algorithm.
383 */
384#define PSA_ALG_DSA(hash_alg) \
385 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
386#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
387#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
388/** Deterministic DSA signature with hashing.
389 *
390 * This is the deterministic variant defined by RFC 6979 of
391 * the signature scheme defined by FIPS 186-4.
392 *
393 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
394 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
395 * This includes #PSA_ALG_ANY_HASH
396 * when specifying the algorithm in a usage policy.
397 *
398 * \return The corresponding DSA signature algorithm.
399 * \return Unspecified if \p hash_alg is not a supported
400 * hash algorithm.
401 */
402#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
403 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
404#define PSA_ALG_IS_DSA(alg) \
405 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
406 PSA_ALG_DSA_BASE)
407#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
408 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
409#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
410 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
411#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
412 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
413
414
415/* We need to expand the sample definition of this macro from
416 * the API definition. */
417#undef PSA_ALG_IS_HASH_AND_SIGN
418#define PSA_ALG_IS_HASH_AND_SIGN(alg) \
419 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
420 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg))
421
422/**@}*/
423
Gilles Peskine24f10f82019-05-16 12:18:32 +0200424/** \addtogroup attributes
425 * @{
426 */
427
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200428/** Custom Diffie-Hellman group.
429 *
430 * For keys of type #PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_GROUP_CUSTOM) or
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200431 * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_GROUP_CUSTOM), the group data comes
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200432 * from domain parameters set by psa_set_key_domain_parameters().
433 */
Gilles Peskine43326f02019-10-09 16:43:39 +0200434/* This value is a deprecated value meaning an explicit curve in the IANA
435 * registry. */
436#define PSA_DH_GROUP_CUSTOM ((psa_dh_group_t) 0xff01)
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200437
438
Gilles Peskine24f10f82019-05-16 12:18:32 +0200439/**
440 * \brief Set domain parameters for a key.
441 *
442 * Some key types require additional domain parameters in addition to
443 * the key type identifier and the key size. Use this function instead
444 * of psa_set_key_type() when you need to specify domain parameters.
445 *
446 * The format for the required domain parameters varies based on the key type.
447 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200448 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine24f10f82019-05-16 12:18:32 +0200449 * the domain parameter data consists of the public exponent,
450 * represented as a big-endian integer with no leading zeros.
451 * This information is used when generating an RSA key pair.
452 * When importing a key, the public exponent is read from the imported
453 * key data and the exponent recorded in the attribute structure is ignored.
454 * As an exception, the public exponent 65537 is represented by an empty
455 * byte string.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200456 * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEY_PAIR),
Gilles Peskine24f10f82019-05-16 12:18:32 +0200457 * the `Dss-Parms` format as defined by RFC 3279 §2.3.2.
458 * ```
459 * Dss-Parms ::= SEQUENCE {
460 * p INTEGER,
461 * q INTEGER,
462 * g INTEGER
463 * }
464 * ```
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200465 * - For Diffie-Hellman key exchange keys
466 * (#PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_GROUP_CUSTOM) or
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200467 * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_GROUP_CUSTOM)), the
Gilles Peskine24f10f82019-05-16 12:18:32 +0200468 * `DomainParameters` format as defined by RFC 3279 §2.3.3.
469 * ```
470 * DomainParameters ::= SEQUENCE {
471 * p INTEGER, -- odd prime, p=jq +1
472 * g INTEGER, -- generator, g
473 * q INTEGER, -- factor of p-1
474 * j INTEGER OPTIONAL, -- subgroup factor
475 * validationParms ValidationParms OPTIONAL
476 * }
477 * ValidationParms ::= SEQUENCE {
478 * seed BIT STRING,
479 * pgenCounter INTEGER
480 * }
481 * ```
482 *
483 * \note This function may allocate memory or other resources.
484 * Once you have called this function on an attribute structure,
485 * you must call psa_reset_key_attributes() to free these resources.
486 *
487 * \note This is an experimental extension to the interface. It may change
488 * in future versions of the library.
489 *
490 * \param[in,out] attributes Attribute structure where the specified domain
491 * parameters will be stored.
492 * If this function fails, the content of
493 * \p attributes is not modified.
494 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
495 * \param[in] data Buffer containing the key domain parameters.
496 * The content of this buffer is interpreted
497 * according to \p type as described above.
498 * \param data_length Size of the \p data buffer in bytes.
499 *
500 * \retval #PSA_SUCCESS
501 * \retval #PSA_ERROR_INVALID_ARGUMENT
502 * \retval #PSA_ERROR_NOT_SUPPORTED
503 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
504 */
505psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
506 psa_key_type_t type,
507 const uint8_t *data,
508 size_t data_length);
509
510/**
511 * \brief Get domain parameters for a key.
512 *
513 * Get the domain parameters for a key with this function, if any. The format
514 * of the domain parameters written to \p data is specified in the
515 * documentation for psa_set_key_domain_parameters().
516 *
517 * \note This is an experimental extension to the interface. It may change
518 * in future versions of the library.
519 *
520 * \param[in] attributes The key attribute structure to query.
521 * \param[out] data On success, the key domain parameters.
522 * \param data_size Size of the \p data buffer in bytes.
523 * The buffer is guaranteed to be large
524 * enough if its size in bytes is at least
525 * the value given by
526 * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
527 * \param[out] data_length On success, the number of bytes
528 * that make up the key domain parameters data.
529 *
530 * \retval #PSA_SUCCESS
531 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
532 */
533psa_status_t psa_get_key_domain_parameters(
534 const psa_key_attributes_t *attributes,
535 uint8_t *data,
536 size_t data_size,
537 size_t *data_length);
538
539/** Safe output buffer size for psa_get_key_domain_parameters().
540 *
541 * This macro returns a compile-time constant if its arguments are
542 * compile-time constants.
543 *
544 * \warning This function may call its arguments multiple times or
545 * zero times, so you should not pass arguments that contain
546 * side effects.
547 *
548 * \note This is an experimental extension to the interface. It may change
549 * in future versions of the library.
550 *
551 * \param key_type A supported key type.
552 * \param key_bits The size of the key in bits.
553 *
554 * \return If the parameters are valid and supported, return
555 * a buffer size in bytes that guarantees that
556 * psa_get_key_domain_parameters() will not fail with
557 * #PSA_ERROR_BUFFER_TOO_SMALL.
558 * If the parameters are a valid combination that is not supported
Gilles Peskine27a983d2019-05-16 17:24:53 +0200559 * by the implementation, this macro shall return either a
Gilles Peskine24f10f82019-05-16 12:18:32 +0200560 * sensible size or 0.
561 * If the parameters are not valid, the
562 * return value is unspecified.
563 */
564#define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \
565 (PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) : \
566 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
567 PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
568 0)
569#define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
570 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/)
571#define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
572 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/)
573
574/**@}*/
575
Gilles Peskinee59236f2018-01-27 23:32:46 +0100576#ifdef __cplusplus
577}
578#endif
579
580#endif /* PSA_CRYPTO_EXTRA_H */