blob: 3675ac61bde47e6f1b2ca59f610311e3c26841ce [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{
92 attributes->policy.alg2 = alg2;
93}
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{
104 return( attributes->policy.alg2 );
105}
106
107/**@}*/
108
Gilles Peskinee59236f2018-01-27 23:32:46 +0100109/**
110 * \brief Library deinitialization.
111 *
112 * This function clears all data associated with the PSA layer,
113 * including the whole key store.
114 *
115 * This is an Mbed TLS extension.
116 */
117void mbedtls_psa_crypto_free( void );
118
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200119/** \brief Statistics about
120 * resource consumption related to the PSA keystore.
121 *
122 * \note The content of this structure is not part of the stable API and ABI
123 * of Mbed Crypto and may change arbitrarily from version to version.
124 */
125typedef struct mbedtls_psa_stats_s
126{
127 /** Number of slots containing key material for a volatile key. */
128 size_t volatile_slots;
129 /** Number of slots containing key material for a key which is in
130 * internal persistent storage. */
131 size_t persistent_slots;
132 /** Number of slots containing a reference to a key in a
133 * secure element. */
134 size_t external_slots;
135 /** Number of slots which are occupied, but do not contain
136 * key material yet. */
137 size_t half_filled_slots;
138 /** Number of slots that contain cache data. */
139 size_t cache_slots;
140 /** Number of slots that are not used for anything. */
141 size_t empty_slots;
142 /** Largest key id value among open keys in internal persistent storage. */
143 psa_key_id_t max_open_internal_key_id;
144 /** Largest key id value among open keys in secure elements. */
145 psa_key_id_t max_open_external_key_id;
146} mbedtls_psa_stats_t;
147
148/** \brief Get statistics about
149 * resource consumption related to the PSA keystore.
150 *
151 * \note When Mbed Crypto is built as part of a service, with isolation
152 * between the application and the keystore, the service may or
153 * may not expose this function.
154 */
155void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats );
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200156
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200157/**
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100158 * \brief Inject an initial entropy seed for the random generator into
159 * secure storage.
Gilles Peskine0338ded2018-11-15 18:19:27 +0100160 *
161 * This function injects data to be used as a seed for the random generator
162 * used by the PSA Crypto implementation. On devices that lack a trusted
163 * entropy source (preferably a hardware random number generator),
164 * the Mbed PSA Crypto implementation uses this value to seed its
165 * random generator.
166 *
167 * On devices without a trusted entropy source, this function must be
168 * called exactly once in the lifetime of the device. On devices with
169 * a trusted entropy source, calling this function is optional.
170 * In all cases, this function may only be called before calling any
171 * other function in the PSA Crypto API, including psa_crypto_init().
172 *
173 * When this function returns successfully, it populates a file in
174 * persistent storage. Once the file has been created, this function
175 * can no longer succeed.
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100176 *
177 * If any error occurs, this function does not change the system state.
178 * You can call this function again after correcting the reason for the
179 * error if possible.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200180 *
181 * \warning This function **can** fail! Callers MUST check the return status.
182 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100183 * \warning If you use this function, you should use it as part of a
184 * factory provisioning process. The value of the injected seed
185 * is critical to the security of the device. It must be
186 * *secret*, *unpredictable* and (statistically) *unique per device*.
187 * You should be generate it randomly using a cryptographically
188 * secure random generator seeded from trusted entropy sources.
189 * You should transmit it securely to the device and ensure
190 * that its value is not leaked or stored anywhere beyond the
191 * needs of transmitting it from the point of generation to
192 * the call of this function, and erase all copies of the value
193 * once this function returns.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200194 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100195 * This is an Mbed TLS extension.
196 *
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200197 * \note This function is only available on the following platforms:
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100198 * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled.
199 * Note that you must provide compatible implementations of
200 * mbedtls_nv_seed_read and mbedtls_nv_seed_write.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200201 * * In a client-server integration of PSA Cryptography, on the client side,
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200202 * if the server supports this feature.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200203 * \param[in] seed Buffer containing the seed value to inject.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200204 * \param[in] seed_size Size of the \p seed buffer.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200205 * The size of the seed in bytes must be greater
206 * or equal to both #MBEDTLS_ENTROPY_MIN_PLATFORM
207 * and #MBEDTLS_ENTROPY_BLOCK_SIZE.
208 * It must be less or equal to
209 * #MBEDTLS_ENTROPY_MAX_SEED_SIZE.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200210 *
211 * \retval #PSA_SUCCESS
Gilles Peskine0338ded2018-11-15 18:19:27 +0100212 * The seed value was injected successfully. The random generator
213 * of the PSA Crypto implementation is now ready for use.
214 * You may now call psa_crypto_init() and use the PSA Crypto
215 * implementation.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200216 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100217 * \p seed_size is out of range.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200218 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine0338ded2018-11-15 18:19:27 +0100219 * There was a failure reading or writing from storage.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200220 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine0338ded2018-11-15 18:19:27 +0100221 * The library has already been initialized. It is no longer
222 * possible to call this function.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200223 */
224psa_status_t mbedtls_psa_inject_entropy(const unsigned char *seed,
225 size_t seed_size);
226
Janos Follath71a4c912019-06-11 09:14:47 +0100227/*
228 * If this option is not turned on, then the function `psa_key_derivation()`
229 * is removed.
230 *
231 * The sole purpose of this option is to make the transition to the new API
232 * smoother. Once the transition is complete it can and should be removed
233 * along with the old API and its implementation.
234 */
235#define PSA_PRE_1_0_KEY_DERIVATION
236
237#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskine5dcd3ce2019-01-18 16:41:31 +0100238/** Set up a key derivation operation.
239 *
240 * FIMXE This function is no longer part of the official API. Its prototype
241 * is only kept around for the sake of tests that haven't been updated yet.
242 *
Gilles Peskine3be6b7f2019-03-05 19:32:26 +0100243 * A key derivation algorithm takes three inputs: a secret input \p handle and
Gilles Peskine5dcd3ce2019-01-18 16:41:31 +0100244 * two non-secret inputs \p label and p salt.
245 * The result of this function is a byte generator which can
246 * be used to produce keys and other cryptographic material.
247 *
248 * The role of \p label and \p salt is as follows:
249 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
250 * and \p label is the info string used in the "expand" step.
251 *
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200252 * \param[in,out] operation The key derivation object to set up. It must
253 * have been initialized as per the documentation
254 * for #psa_key_derivation_operation_t and not
255 * yet be in use.
Gilles Peskine5dcd3ce2019-01-18 16:41:31 +0100256 * \param handle Handle to the secret key.
257 * \param alg The key derivation algorithm to compute
258 * (\c PSA_ALG_XXX value such that
259 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
260 * \param[in] salt Salt to use.
261 * \param salt_length Size of the \p salt buffer in bytes.
262 * \param[in] label Label to use.
263 * \param label_length Size of the \p label buffer in bytes.
264 * \param capacity The maximum number of bytes that the
Gilles Peskine35675b62019-05-16 17:26:11 +0200265 * operation will be able to provide.
Gilles Peskine5dcd3ce2019-01-18 16:41:31 +0100266 *
267 * \retval #PSA_SUCCESS
268 * Success.
269 * \retval #PSA_ERROR_INVALID_HANDLE
270 * \retval #PSA_ERROR_EMPTY_SLOT
271 * \retval #PSA_ERROR_NOT_PERMITTED
272 * \retval #PSA_ERROR_INVALID_ARGUMENT
273 * \c key is not compatible with \c alg,
274 * or \p capacity is too large for the specified algorithm and key.
275 * \retval #PSA_ERROR_NOT_SUPPORTED
276 * \c alg is not supported or is not a key derivation algorithm.
277 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
278 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
279 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200280 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine5dcd3ce2019-01-18 16:41:31 +0100281 * \retval #PSA_ERROR_BAD_STATE
282 * The library has not been previously initialized by psa_crypto_init().
283 * It is implementation-dependent whether a failure to initialize
284 * results in this error code.
285 */
Gilles Peskine35675b62019-05-16 17:26:11 +0200286psa_status_t psa_key_derivation(psa_key_derivation_operation_t *operation,
Gilles Peskine5dcd3ce2019-01-18 16:41:31 +0100287 psa_key_handle_t handle,
288 psa_algorithm_t alg,
289 const uint8_t *salt,
290 size_t salt_length,
291 const uint8_t *label,
292 size_t label_length,
293 size_t capacity);
Janos Follath71a4c912019-06-11 09:14:47 +0100294#endif /* PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine5dcd3ce2019-01-18 16:41:31 +0100295
Gilles Peskine6843c292019-01-18 16:44:49 +0100296/* FIXME Deprecated. Remove this as soon as all the tests are updated. */
297#define PSA_ALG_SELECT_RAW ((psa_algorithm_t)0x31000001)
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200298
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200299/** \addtogroup crypto_types
300 * @{
301 */
302
Gilles Peskinea1302192019-05-16 13:58:24 +0200303/** DSA public key.
304 *
305 * The import and export format is the
306 * representation of the public key `y = g^x mod p` as a big-endian byte
307 * string. The length of the byte string is the length of the base prime `p`
308 * in bytes.
309 */
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200310#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x60020000)
Gilles Peskinea1302192019-05-16 13:58:24 +0200311
312/** DSA key pair (private and public key).
313 *
314 * The import and export format is the
315 * representation of the private key `x` as a big-endian byte string. The
316 * length of the byte string is the private key size in bytes (leading zeroes
317 * are not stripped).
318 *
319 * Determinstic DSA key derivation with psa_generate_derived_key follows
320 * FIPS 186-4 §B.1.2: interpret the byte string as integer
321 * in big-endian order. Discard it if it is not in the range
322 * [0, *N* - 2] where *N* is the boundary of the private key domain
323 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
324 * or the order of the curve's base point for ECC).
325 * Add 1 to the resulting integer and use this as the private key *x*.
326 *
327 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200328#define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t)0x70020000)
Gilles Peskinea1302192019-05-16 13:58:24 +0200329
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200330/** Whether a key type is an DSA key (pair or public-only). */
331#define PSA_KEY_TYPE_IS_DSA(type) \
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200332 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200333
334#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
335/** DSA signature with hashing.
336 *
337 * This is the signature scheme defined by FIPS 186-4,
338 * with a random per-message secret number (*k*).
339 *
340 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
341 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
342 * This includes #PSA_ALG_ANY_HASH
343 * when specifying the algorithm in a usage policy.
344 *
345 * \return The corresponding DSA signature algorithm.
346 * \return Unspecified if \p hash_alg is not a supported
347 * hash algorithm.
348 */
349#define PSA_ALG_DSA(hash_alg) \
350 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
351#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
352#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
353/** Deterministic DSA signature with hashing.
354 *
355 * This is the deterministic variant defined by RFC 6979 of
356 * the signature scheme defined by FIPS 186-4.
357 *
358 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
359 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
360 * This includes #PSA_ALG_ANY_HASH
361 * when specifying the algorithm in a usage policy.
362 *
363 * \return The corresponding DSA signature algorithm.
364 * \return Unspecified if \p hash_alg is not a supported
365 * hash algorithm.
366 */
367#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
368 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
369#define PSA_ALG_IS_DSA(alg) \
370 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
371 PSA_ALG_DSA_BASE)
372#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
373 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
374#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
375 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
376#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
377 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
378
379
380/* We need to expand the sample definition of this macro from
381 * the API definition. */
382#undef PSA_ALG_IS_HASH_AND_SIGN
383#define PSA_ALG_IS_HASH_AND_SIGN(alg) \
384 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
385 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg))
386
387/**@}*/
388
Gilles Peskine24f10f82019-05-16 12:18:32 +0200389/** \addtogroup attributes
390 * @{
391 */
392
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200393/** Custom Diffie-Hellman group.
394 *
395 * For keys of type #PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_GROUP_CUSTOM) or
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200396 * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_GROUP_CUSTOM), the group data comes
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200397 * from domain parameters set by psa_set_key_domain_parameters().
398 */
399/* This value is reserved for private use in the TLS named group registry. */
400#define PSA_DH_GROUP_CUSTOM ((psa_dh_group_t) 0x01fc)
401
402
Gilles Peskine24f10f82019-05-16 12:18:32 +0200403/**
404 * \brief Set domain parameters for a key.
405 *
406 * Some key types require additional domain parameters in addition to
407 * the key type identifier and the key size. Use this function instead
408 * of psa_set_key_type() when you need to specify domain parameters.
409 *
410 * The format for the required domain parameters varies based on the key type.
411 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200412 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine24f10f82019-05-16 12:18:32 +0200413 * the domain parameter data consists of the public exponent,
414 * represented as a big-endian integer with no leading zeros.
415 * This information is used when generating an RSA key pair.
416 * When importing a key, the public exponent is read from the imported
417 * key data and the exponent recorded in the attribute structure is ignored.
418 * As an exception, the public exponent 65537 is represented by an empty
419 * byte string.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200420 * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEY_PAIR),
Gilles Peskine24f10f82019-05-16 12:18:32 +0200421 * the `Dss-Parms` format as defined by RFC 3279 §2.3.2.
422 * ```
423 * Dss-Parms ::= SEQUENCE {
424 * p INTEGER,
425 * q INTEGER,
426 * g INTEGER
427 * }
428 * ```
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200429 * - For Diffie-Hellman key exchange keys
430 * (#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
Gilles Peskine24f10f82019-05-16 12:18:32 +0200432 * `DomainParameters` format as defined by RFC 3279 §2.3.3.
433 * ```
434 * DomainParameters ::= SEQUENCE {
435 * p INTEGER, -- odd prime, p=jq +1
436 * g INTEGER, -- generator, g
437 * q INTEGER, -- factor of p-1
438 * j INTEGER OPTIONAL, -- subgroup factor
439 * validationParms ValidationParms OPTIONAL
440 * }
441 * ValidationParms ::= SEQUENCE {
442 * seed BIT STRING,
443 * pgenCounter INTEGER
444 * }
445 * ```
446 *
447 * \note This function may allocate memory or other resources.
448 * Once you have called this function on an attribute structure,
449 * you must call psa_reset_key_attributes() to free these resources.
450 *
451 * \note This is an experimental extension to the interface. It may change
452 * in future versions of the library.
453 *
454 * \param[in,out] attributes Attribute structure where the specified domain
455 * parameters will be stored.
456 * If this function fails, the content of
457 * \p attributes is not modified.
458 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
459 * \param[in] data Buffer containing the key domain parameters.
460 * The content of this buffer is interpreted
461 * according to \p type as described above.
462 * \param data_length Size of the \p data buffer in bytes.
463 *
464 * \retval #PSA_SUCCESS
465 * \retval #PSA_ERROR_INVALID_ARGUMENT
466 * \retval #PSA_ERROR_NOT_SUPPORTED
467 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
468 */
469psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
470 psa_key_type_t type,
471 const uint8_t *data,
472 size_t data_length);
473
474/**
475 * \brief Get domain parameters for a key.
476 *
477 * Get the domain parameters for a key with this function, if any. The format
478 * of the domain parameters written to \p data is specified in the
479 * documentation for psa_set_key_domain_parameters().
480 *
481 * \note This is an experimental extension to the interface. It may change
482 * in future versions of the library.
483 *
484 * \param[in] attributes The key attribute structure to query.
485 * \param[out] data On success, the key domain parameters.
486 * \param data_size Size of the \p data buffer in bytes.
487 * The buffer is guaranteed to be large
488 * enough if its size in bytes is at least
489 * the value given by
490 * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
491 * \param[out] data_length On success, the number of bytes
492 * that make up the key domain parameters data.
493 *
494 * \retval #PSA_SUCCESS
495 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
496 */
497psa_status_t psa_get_key_domain_parameters(
498 const psa_key_attributes_t *attributes,
499 uint8_t *data,
500 size_t data_size,
501 size_t *data_length);
502
503/** Safe output buffer size for psa_get_key_domain_parameters().
504 *
505 * This macro returns a compile-time constant if its arguments are
506 * compile-time constants.
507 *
508 * \warning This function may call its arguments multiple times or
509 * zero times, so you should not pass arguments that contain
510 * side effects.
511 *
512 * \note This is an experimental extension to the interface. It may change
513 * in future versions of the library.
514 *
515 * \param key_type A supported key type.
516 * \param key_bits The size of the key in bits.
517 *
518 * \return If the parameters are valid and supported, return
519 * a buffer size in bytes that guarantees that
520 * psa_get_key_domain_parameters() will not fail with
521 * #PSA_ERROR_BUFFER_TOO_SMALL.
522 * If the parameters are a valid combination that is not supported
Gilles Peskine27a983d2019-05-16 17:24:53 +0200523 * by the implementation, this macro shall return either a
Gilles Peskine24f10f82019-05-16 12:18:32 +0200524 * sensible size or 0.
525 * If the parameters are not valid, the
526 * return value is unspecified.
527 */
528#define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \
529 (PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) : \
530 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
531 PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
532 0)
533#define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
534 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/)
535#define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
536 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/)
537
538/**@}*/
539
Gilles Peskinee59236f2018-01-27 23:32:46 +0100540#ifdef __cplusplus
541}
542#endif
543
544#endif /* PSA_CRYPTO_EXTRA_H */