blob: 5016ba87ca1dd68c3f3c736c47375854249b08bf [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 Peskinee59236f2018-01-27 23:32:46 +010065/**
66 * \brief Library deinitialization.
67 *
68 * This function clears all data associated with the PSA layer,
69 * including the whole key store.
70 *
71 * This is an Mbed TLS extension.
72 */
73void mbedtls_psa_crypto_free( void );
74
Netanel Gonen2bcd3122018-11-19 11:53:02 +020075
Netanel Gonen2bcd3122018-11-19 11:53:02 +020076/**
Gilles Peskineee2ffd32018-11-16 11:02:49 +010077 * \brief Inject an initial entropy seed for the random generator into
78 * secure storage.
Gilles Peskine0338ded2018-11-15 18:19:27 +010079 *
80 * This function injects data to be used as a seed for the random generator
81 * used by the PSA Crypto implementation. On devices that lack a trusted
82 * entropy source (preferably a hardware random number generator),
83 * the Mbed PSA Crypto implementation uses this value to seed its
84 * random generator.
85 *
86 * On devices without a trusted entropy source, this function must be
87 * called exactly once in the lifetime of the device. On devices with
88 * a trusted entropy source, calling this function is optional.
89 * In all cases, this function may only be called before calling any
90 * other function in the PSA Crypto API, including psa_crypto_init().
91 *
92 * When this function returns successfully, it populates a file in
93 * persistent storage. Once the file has been created, this function
94 * can no longer succeed.
Gilles Peskineee2ffd32018-11-16 11:02:49 +010095 *
96 * If any error occurs, this function does not change the system state.
97 * You can call this function again after correcting the reason for the
98 * error if possible.
Netanel Gonen2bcd3122018-11-19 11:53:02 +020099 *
100 * \warning This function **can** fail! Callers MUST check the return status.
101 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100102 * \warning If you use this function, you should use it as part of a
103 * factory provisioning process. The value of the injected seed
104 * is critical to the security of the device. It must be
105 * *secret*, *unpredictable* and (statistically) *unique per device*.
106 * You should be generate it randomly using a cryptographically
107 * secure random generator seeded from trusted entropy sources.
108 * You should transmit it securely to the device and ensure
109 * that its value is not leaked or stored anywhere beyond the
110 * needs of transmitting it from the point of generation to
111 * the call of this function, and erase all copies of the value
112 * once this function returns.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200113 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100114 * This is an Mbed TLS extension.
115 *
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200116 * \note This function is only available on the following platforms:
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100117 * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled.
118 * Note that you must provide compatible implementations of
119 * mbedtls_nv_seed_read and mbedtls_nv_seed_write.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200120 * * In a client-server integration of PSA Cryptography, on the client side,
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200121 * if the server supports this feature.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200122 * \param[in] seed Buffer containing the seed value to inject.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200123 * \param[in] seed_size Size of the \p seed buffer.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200124 * The size of the seed in bytes must be greater
125 * or equal to both #MBEDTLS_ENTROPY_MIN_PLATFORM
126 * and #MBEDTLS_ENTROPY_BLOCK_SIZE.
127 * It must be less or equal to
128 * #MBEDTLS_ENTROPY_MAX_SEED_SIZE.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200129 *
130 * \retval #PSA_SUCCESS
Gilles Peskine0338ded2018-11-15 18:19:27 +0100131 * The seed value was injected successfully. The random generator
132 * of the PSA Crypto implementation is now ready for use.
133 * You may now call psa_crypto_init() and use the PSA Crypto
134 * implementation.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200135 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100136 * \p seed_size is out of range.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200137 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine0338ded2018-11-15 18:19:27 +0100138 * There was a failure reading or writing from storage.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200139 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine0338ded2018-11-15 18:19:27 +0100140 * The library has already been initialized. It is no longer
141 * possible to call this function.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200142 */
143psa_status_t mbedtls_psa_inject_entropy(const unsigned char *seed,
144 size_t seed_size);
145
Gilles Peskine5dcd3ce2019-01-18 16:41:31 +0100146/** Set up a key derivation operation.
147 *
148 * FIMXE This function is no longer part of the official API. Its prototype
149 * is only kept around for the sake of tests that haven't been updated yet.
150 *
Gilles Peskine3be6b7f2019-03-05 19:32:26 +0100151 * A key derivation algorithm takes three inputs: a secret input \p handle and
Gilles Peskine5dcd3ce2019-01-18 16:41:31 +0100152 * two non-secret inputs \p label and p salt.
153 * The result of this function is a byte generator which can
154 * be used to produce keys and other cryptographic material.
155 *
156 * The role of \p label and \p salt is as follows:
157 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
158 * and \p label is the info string used in the "expand" step.
159 *
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200160 * \param[in,out] operation The key derivation object to set up. It must
161 * have been initialized as per the documentation
162 * for #psa_key_derivation_operation_t and not
163 * yet be in use.
Gilles Peskine5dcd3ce2019-01-18 16:41:31 +0100164 * \param handle Handle to the secret key.
165 * \param alg The key derivation algorithm to compute
166 * (\c PSA_ALG_XXX value such that
167 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
168 * \param[in] salt Salt to use.
169 * \param salt_length Size of the \p salt buffer in bytes.
170 * \param[in] label Label to use.
171 * \param label_length Size of the \p label buffer in bytes.
172 * \param capacity The maximum number of bytes that the
Gilles Peskine35675b62019-05-16 17:26:11 +0200173 * operation will be able to provide.
Gilles Peskine5dcd3ce2019-01-18 16:41:31 +0100174 *
175 * \retval #PSA_SUCCESS
176 * Success.
177 * \retval #PSA_ERROR_INVALID_HANDLE
178 * \retval #PSA_ERROR_EMPTY_SLOT
179 * \retval #PSA_ERROR_NOT_PERMITTED
180 * \retval #PSA_ERROR_INVALID_ARGUMENT
181 * \c key is not compatible with \c alg,
182 * or \p capacity is too large for the specified algorithm and key.
183 * \retval #PSA_ERROR_NOT_SUPPORTED
184 * \c alg is not supported or is not a key derivation algorithm.
185 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
186 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
187 * \retval #PSA_ERROR_HARDWARE_FAILURE
188 * \retval #PSA_ERROR_TAMPERING_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 */
Gilles Peskine35675b62019-05-16 17:26:11 +0200194psa_status_t psa_key_derivation(psa_key_derivation_operation_t *operation,
Gilles Peskine5dcd3ce2019-01-18 16:41:31 +0100195 psa_key_handle_t handle,
196 psa_algorithm_t alg,
197 const uint8_t *salt,
198 size_t salt_length,
199 const uint8_t *label,
200 size_t label_length,
201 size_t capacity);
202
Gilles Peskine6843c292019-01-18 16:44:49 +0100203/* FIXME Deprecated. Remove this as soon as all the tests are updated. */
204#define PSA_ALG_SELECT_RAW ((psa_algorithm_t)0x31000001)
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200205
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200206/** \defgroup policy Key policies
207 * @{
208 *
209 * The functions in this section are legacy interfaces where the properties
210 * of a key object are set after allocating a handle, in constrast with the
211 * preferred interface where key objects are created atomically from
212 * a structure that represents the properties.
213 */
214
215/** \def PSA_KEY_POLICY_INIT
216 *
217 * This macro returns a suitable initializer for a key policy object of type
218 * #psa_key_policy_t.
219 */
220#ifdef __DOXYGEN_ONLY__
221/* This is an example definition for documentation purposes.
222 * Implementations should define a suitable value in `crypto_struct.h`.
223 */
224#define PSA_KEY_POLICY_INIT {0}
225#endif
226
227/** Return an initial value for a key policy that forbids all usage of the key.
228 */
229static psa_key_policy_t psa_key_policy_init(void);
230
231/** \brief Set the standard fields of a policy structure.
232 *
233 * Note that this function does not make any consistency check of the
234 * parameters. The values are only checked when applying the policy to
235 * a key slot with psa_set_key_policy().
236 *
237 * \param[in,out] policy The key policy to modify. It must have been
238 * initialized as per the documentation for
239 * #psa_key_policy_t.
240 * \param usage The permitted uses for the key.
241 * \param alg The algorithm that the key may be used for.
242 */
243void psa_key_policy_set_usage(psa_key_policy_t *policy,
244 psa_key_usage_t usage,
245 psa_algorithm_t alg);
246
247/** \brief Retrieve the usage field of a policy structure.
248 *
249 * \param[in] policy The policy object to query.
250 *
251 * \return The permitted uses for a key with this policy.
252 */
253psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
254
255/** \brief Retrieve the algorithm field of a policy structure.
256 *
257 * \param[in] policy The policy object to query.
258 *
259 * \return The permitted algorithm for a key with this policy.
260 */
261psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
262
263/** \brief Set the usage policy on a key slot.
264 *
265 * This function must be called on an empty key slot, before importing,
266 * generating or creating a key in the slot. Changing the policy of an
267 * existing key is not permitted.
268 *
269 * Implementations may set restrictions on supported key policies
270 * depending on the key type and the key slot.
271 *
272 * \param handle Handle to the key whose policy is to be changed.
273 * \param[in] policy The policy object to query.
274 *
275 * \retval #PSA_SUCCESS
276 * Success.
277 * If the key is persistent, it is implementation-defined whether
278 * the policy has been saved to persistent storage. Implementations
279 * may defer saving the policy until the key material is created.
280 * \retval #PSA_ERROR_INVALID_HANDLE
281 * \retval #PSA_ERROR_ALREADY_EXISTS
282 * \retval #PSA_ERROR_NOT_SUPPORTED
283 * \retval #PSA_ERROR_INVALID_ARGUMENT
284 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
285 * \retval #PSA_ERROR_HARDWARE_FAILURE
286 * \retval #PSA_ERROR_TAMPERING_DETECTED
287 * \retval #PSA_ERROR_BAD_STATE
288 * The library has not been previously initialized by psa_crypto_init().
289 * It is implementation-dependent whether a failure to initialize
290 * results in this error code.
291 */
292psa_status_t psa_set_key_policy(psa_key_handle_t handle,
293 const psa_key_policy_t *policy);
294
295/** \brief Get the usage policy for a key slot.
296 *
297 * \param handle Handle to the key slot whose policy is being queried.
298 * \param[out] policy On success, the key's policy.
299 *
300 * \retval #PSA_SUCCESS
301 * \retval #PSA_ERROR_INVALID_HANDLE
302 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
303 * \retval #PSA_ERROR_HARDWARE_FAILURE
304 * \retval #PSA_ERROR_TAMPERING_DETECTED
305 * \retval #PSA_ERROR_BAD_STATE
306 * The library has not been previously initialized by psa_crypto_init().
307 * It is implementation-dependent whether a failure to initialize
308 * results in this error code.
309 */
310psa_status_t psa_get_key_policy(psa_key_handle_t handle,
311 psa_key_policy_t *policy);
312
313/**@}*/
314
Gilles Peskine87a5e562019-04-17 12:28:25 +0200315/** \defgroup to_handle Key creation to allocated handle
316 * @{
317 *
318 * The functions in this section are legacy interfaces where the properties
319 * of a key object are set after allocating a handle, in constrast with the
320 * preferred interface where key objects are created atomically from
321 * a structure that represents the properties.
322 */
323
324/** Create a new persistent key slot.
325 *
326 * Create a new persistent key slot and return a handle to it. The handle
327 * remains valid until the application calls psa_close_key() or terminates.
328 * The application can open the key again with psa_open_key() until it
329 * removes the key by calling psa_destroy_key().
330 *
331 * \param lifetime The lifetime of the key. This designates a storage
332 * area where the key material is stored. This must not
333 * be #PSA_KEY_LIFETIME_VOLATILE.
334 * \param id The persistent identifier of the key.
335 * \param[out] handle On success, a handle to the newly created key slot.
336 * When key material is later created in this key slot,
337 * it will be saved to the specified persistent location.
338 *
339 * \retval #PSA_SUCCESS
340 * Success. The application can now use the value of `*handle`
341 * to access the newly allocated key slot.
342 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
343 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
344 * \retval #PSA_ERROR_ALREADY_EXISTS
345 * There is already a key with the identifier \p id in the storage
346 * area designated by \p lifetime.
347 * \retval #PSA_ERROR_INVALID_ARGUMENT
348 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
349 * \retval #PSA_ERROR_INVALID_ARGUMENT
350 * \p id is invalid for the specified lifetime.
351 * \retval #PSA_ERROR_NOT_SUPPORTED
352 * \p lifetime is not supported.
353 * \retval #PSA_ERROR_NOT_PERMITTED
354 * \p lifetime is valid, but the application does not have the
355 * permission to create a key there.
356 */
357psa_status_t psa_create_key(psa_key_lifetime_t lifetime,
358 psa_key_id_t id,
359 psa_key_handle_t *handle);
360
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200361/** Allocate a key slot for a transient key, i.e. a key which is only stored
362 * in volatile memory.
363 *
364 * The allocated key slot and its handle remain valid until the
365 * application calls psa_close_key() or psa_destroy_key() or until the
366 * application terminates.
367 *
368 * \param[out] handle On success, a handle to a volatile key slot.
369 *
370 * \retval #PSA_SUCCESS
371 * Success. The application can now use the value of `*handle`
372 * to access the newly allocated key slot.
373 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
374 * There was not enough memory, or the maximum number of key slots
375 * has been reached.
376 */
377psa_status_t psa_allocate_key(psa_key_handle_t *handle);
378
379/**
380 * \brief Get basic metadata about a key.
381 *
382 * \param handle Handle to the key slot to query.
383 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
384 * This may be a null pointer, in which case the key type
385 * is not written.
386 * \param[out] bits On success, the key size in bits.
387 * This may be a null pointer, in which case the key size
388 * is not written.
389 *
390 * \retval #PSA_SUCCESS
391 * \retval #PSA_ERROR_INVALID_HANDLE
392 * \retval #PSA_ERROR_DOES_NOT_EXIST
393 * The handle is to a key slot which does not contain key material yet.
394 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
395 * \retval #PSA_ERROR_HARDWARE_FAILURE
396 * \retval #PSA_ERROR_TAMPERING_DETECTED
397 * \retval #PSA_ERROR_BAD_STATE
398 * The library has not been previously initialized by psa_crypto_init().
399 * It is implementation-dependent whether a failure to initialize
400 * results in this error code.
401 */
402psa_status_t psa_get_key_information(psa_key_handle_t handle,
403 psa_key_type_t *type,
404 size_t *bits);
405
Gilles Peskine87a5e562019-04-17 12:28:25 +0200406/** \brief Retrieve the lifetime of an open key.
407 *
408 * \param handle Handle to query.
409 * \param[out] lifetime On success, the lifetime value.
410 *
411 * \retval #PSA_SUCCESS
412 * Success.
413 * \retval #PSA_ERROR_INVALID_HANDLE
414 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
415 * \retval #PSA_ERROR_HARDWARE_FAILURE
416 * \retval #PSA_ERROR_TAMPERING_DETECTED
417 * \retval #PSA_ERROR_BAD_STATE
418 * The library has not been previously initialized by psa_crypto_init().
419 * It is implementation-dependent whether a failure to initialize
420 * results in this error code.
421 */
422psa_status_t psa_get_key_lifetime_from_handle(psa_key_handle_t handle,
423 psa_key_lifetime_t *lifetime);
424
425psa_status_t psa_import_key_to_handle(psa_key_handle_t handle,
426 psa_key_type_t type,
427 const uint8_t *data,
428 size_t data_length);
429
430psa_status_t psa_copy_key_to_handle(psa_key_handle_t source_handle,
431 psa_key_handle_t target_handle,
432 const psa_key_policy_t *constraint);
433
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100434psa_status_t psa_generate_derived_key_to_handle(psa_key_handle_t handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200435 psa_key_type_t type,
436 size_t bits,
Gilles Peskine35675b62019-05-16 17:26:11 +0200437 psa_key_derivation_operation_t *operation);
Gilles Peskine87a5e562019-04-17 12:28:25 +0200438
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100439psa_status_t psa_generate_random_key_to_handle(psa_key_handle_t handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200440 psa_key_type_t type,
441 size_t bits,
442 const void *extra,
443 size_t extra_size);
444
445/**@}*/
446
Gilles Peskine24f10f82019-05-16 12:18:32 +0200447
448/** \addtogroup attributes
449 * @{
450 */
451
452/**
453 * \brief Set domain parameters for a key.
454 *
455 * Some key types require additional domain parameters in addition to
456 * the key type identifier and the key size. Use this function instead
457 * of psa_set_key_type() when you need to specify domain parameters.
458 *
459 * The format for the required domain parameters varies based on the key type.
460 *
461 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEYPAIR),
462 * the domain parameter data consists of the public exponent,
463 * represented as a big-endian integer with no leading zeros.
464 * This information is used when generating an RSA key pair.
465 * When importing a key, the public exponent is read from the imported
466 * key data and the exponent recorded in the attribute structure is ignored.
467 * As an exception, the public exponent 65537 is represented by an empty
468 * byte string.
469 * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEYPAIR),
470 * the `Dss-Parms` format as defined by RFC 3279 §2.3.2.
471 * ```
472 * Dss-Parms ::= SEQUENCE {
473 * p INTEGER,
474 * q INTEGER,
475 * g INTEGER
476 * }
477 * ```
478 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY or
479 * #PSA_KEY_TYPE_DH_KEYPAIR), the
480 * `DomainParameters` format as defined by RFC 3279 §2.3.3.
481 * ```
482 * DomainParameters ::= SEQUENCE {
483 * p INTEGER, -- odd prime, p=jq +1
484 * g INTEGER, -- generator, g
485 * q INTEGER, -- factor of p-1
486 * j INTEGER OPTIONAL, -- subgroup factor
487 * validationParms ValidationParms OPTIONAL
488 * }
489 * ValidationParms ::= SEQUENCE {
490 * seed BIT STRING,
491 * pgenCounter INTEGER
492 * }
493 * ```
494 *
495 * \note This function may allocate memory or other resources.
496 * Once you have called this function on an attribute structure,
497 * you must call psa_reset_key_attributes() to free these resources.
498 *
499 * \note This is an experimental extension to the interface. It may change
500 * in future versions of the library.
501 *
502 * \param[in,out] attributes Attribute structure where the specified domain
503 * parameters will be stored.
504 * If this function fails, the content of
505 * \p attributes is not modified.
506 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
507 * \param[in] data Buffer containing the key domain parameters.
508 * The content of this buffer is interpreted
509 * according to \p type as described above.
510 * \param data_length Size of the \p data buffer in bytes.
511 *
512 * \retval #PSA_SUCCESS
513 * \retval #PSA_ERROR_INVALID_ARGUMENT
514 * \retval #PSA_ERROR_NOT_SUPPORTED
515 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
516 */
517psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
518 psa_key_type_t type,
519 const uint8_t *data,
520 size_t data_length);
521
522/**
523 * \brief Get domain parameters for a key.
524 *
525 * Get the domain parameters for a key with this function, if any. The format
526 * of the domain parameters written to \p data is specified in the
527 * documentation for psa_set_key_domain_parameters().
528 *
529 * \note This is an experimental extension to the interface. It may change
530 * in future versions of the library.
531 *
532 * \param[in] attributes The key attribute structure to query.
533 * \param[out] data On success, the key domain parameters.
534 * \param data_size Size of the \p data buffer in bytes.
535 * The buffer is guaranteed to be large
536 * enough if its size in bytes is at least
537 * the value given by
538 * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
539 * \param[out] data_length On success, the number of bytes
540 * that make up the key domain parameters data.
541 *
542 * \retval #PSA_SUCCESS
543 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
544 */
545psa_status_t psa_get_key_domain_parameters(
546 const psa_key_attributes_t *attributes,
547 uint8_t *data,
548 size_t data_size,
549 size_t *data_length);
550
551/** Safe output buffer size for psa_get_key_domain_parameters().
552 *
553 * This macro returns a compile-time constant if its arguments are
554 * compile-time constants.
555 *
556 * \warning This function may call its arguments multiple times or
557 * zero times, so you should not pass arguments that contain
558 * side effects.
559 *
560 * \note This is an experimental extension to the interface. It may change
561 * in future versions of the library.
562 *
563 * \param key_type A supported key type.
564 * \param key_bits The size of the key in bits.
565 *
566 * \return If the parameters are valid and supported, return
567 * a buffer size in bytes that guarantees that
568 * psa_get_key_domain_parameters() will not fail with
569 * #PSA_ERROR_BUFFER_TOO_SMALL.
570 * If the parameters are a valid combination that is not supported
571 * by the implementation, this macro either shall return either a
572 * sensible size or 0.
573 * If the parameters are not valid, the
574 * return value is unspecified.
575 */
576#define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \
577 (PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) : \
578 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
579 PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
580 0)
581#define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
582 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/)
583#define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
584 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/)
585
586/**@}*/
587
Gilles Peskinee59236f2018-01-27 23:32:46 +0100588#ifdef __cplusplus
589}
590#endif
591
592#endif /* PSA_CRYPTO_EXTRA_H */