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