blob: b3ec54fb2ae755d8ed22fcbf7014e98b9338a6a2 [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
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200119
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200120/**
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100121 * \brief Inject an initial entropy seed for the random generator into
122 * secure storage.
Gilles Peskine0338ded2018-11-15 18:19:27 +0100123 *
124 * This function injects data to be used as a seed for the random generator
125 * used by the PSA Crypto implementation. On devices that lack a trusted
126 * entropy source (preferably a hardware random number generator),
127 * the Mbed PSA Crypto implementation uses this value to seed its
128 * random generator.
129 *
130 * On devices without a trusted entropy source, this function must be
131 * called exactly once in the lifetime of the device. On devices with
132 * a trusted entropy source, calling this function is optional.
133 * In all cases, this function may only be called before calling any
134 * other function in the PSA Crypto API, including psa_crypto_init().
135 *
136 * When this function returns successfully, it populates a file in
137 * persistent storage. Once the file has been created, this function
138 * can no longer succeed.
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100139 *
140 * If any error occurs, this function does not change the system state.
141 * You can call this function again after correcting the reason for the
142 * error if possible.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200143 *
144 * \warning This function **can** fail! Callers MUST check the return status.
145 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100146 * \warning If you use this function, you should use it as part of a
147 * factory provisioning process. The value of the injected seed
148 * is critical to the security of the device. It must be
149 * *secret*, *unpredictable* and (statistically) *unique per device*.
150 * You should be generate it randomly using a cryptographically
151 * secure random generator seeded from trusted entropy sources.
152 * You should transmit it securely to the device and ensure
153 * that its value is not leaked or stored anywhere beyond the
154 * needs of transmitting it from the point of generation to
155 * the call of this function, and erase all copies of the value
156 * once this function returns.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200157 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100158 * This is an Mbed TLS extension.
159 *
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200160 * \note This function is only available on the following platforms:
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100161 * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled.
162 * Note that you must provide compatible implementations of
163 * mbedtls_nv_seed_read and mbedtls_nv_seed_write.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200164 * * In a client-server integration of PSA Cryptography, on the client side,
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200165 * if the server supports this feature.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200166 * \param[in] seed Buffer containing the seed value to inject.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200167 * \param[in] seed_size Size of the \p seed buffer.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200168 * The size of the seed in bytes must be greater
169 * or equal to both #MBEDTLS_ENTROPY_MIN_PLATFORM
170 * and #MBEDTLS_ENTROPY_BLOCK_SIZE.
171 * It must be less or equal to
172 * #MBEDTLS_ENTROPY_MAX_SEED_SIZE.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200173 *
174 * \retval #PSA_SUCCESS
Gilles Peskine0338ded2018-11-15 18:19:27 +0100175 * The seed value was injected successfully. The random generator
176 * of the PSA Crypto implementation is now ready for use.
177 * You may now call psa_crypto_init() and use the PSA Crypto
178 * implementation.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200179 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100180 * \p seed_size is out of range.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200181 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine0338ded2018-11-15 18:19:27 +0100182 * There was a failure reading or writing from storage.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200183 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine0338ded2018-11-15 18:19:27 +0100184 * The library has already been initialized. It is no longer
185 * possible to call this function.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200186 */
187psa_status_t mbedtls_psa_inject_entropy(const unsigned char *seed,
188 size_t seed_size);
189
Gilles Peskine5dcd3ce2019-01-18 16:41:31 +0100190/** Set up a key derivation operation.
191 *
192 * FIMXE This function is no longer part of the official API. Its prototype
193 * is only kept around for the sake of tests that haven't been updated yet.
194 *
Gilles Peskine3be6b7f2019-03-05 19:32:26 +0100195 * A key derivation algorithm takes three inputs: a secret input \p handle and
Gilles Peskine5dcd3ce2019-01-18 16:41:31 +0100196 * two non-secret inputs \p label and p salt.
197 * The result of this function is a byte generator which can
198 * be used to produce keys and other cryptographic material.
199 *
200 * The role of \p label and \p salt is as follows:
201 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
202 * and \p label is the info string used in the "expand" step.
203 *
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200204 * \param[in,out] operation The key derivation object to set up. It must
205 * have been initialized as per the documentation
206 * for #psa_key_derivation_operation_t and not
207 * yet be in use.
Gilles Peskine5dcd3ce2019-01-18 16:41:31 +0100208 * \param handle Handle to the secret key.
209 * \param alg The key derivation algorithm to compute
210 * (\c PSA_ALG_XXX value such that
211 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
212 * \param[in] salt Salt to use.
213 * \param salt_length Size of the \p salt buffer in bytes.
214 * \param[in] label Label to use.
215 * \param label_length Size of the \p label buffer in bytes.
216 * \param capacity The maximum number of bytes that the
Gilles Peskine35675b62019-05-16 17:26:11 +0200217 * operation will be able to provide.
Gilles Peskine5dcd3ce2019-01-18 16:41:31 +0100218 *
219 * \retval #PSA_SUCCESS
220 * Success.
221 * \retval #PSA_ERROR_INVALID_HANDLE
222 * \retval #PSA_ERROR_EMPTY_SLOT
223 * \retval #PSA_ERROR_NOT_PERMITTED
224 * \retval #PSA_ERROR_INVALID_ARGUMENT
225 * \c key is not compatible with \c alg,
226 * or \p capacity is too large for the specified algorithm and key.
227 * \retval #PSA_ERROR_NOT_SUPPORTED
228 * \c alg is not supported or is not a key derivation algorithm.
229 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
230 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
231 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200232 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine5dcd3ce2019-01-18 16:41:31 +0100233 * \retval #PSA_ERROR_BAD_STATE
234 * The library has not been previously initialized by psa_crypto_init().
235 * It is implementation-dependent whether a failure to initialize
236 * results in this error code.
237 */
Gilles Peskine35675b62019-05-16 17:26:11 +0200238psa_status_t psa_key_derivation(psa_key_derivation_operation_t *operation,
Gilles Peskine5dcd3ce2019-01-18 16:41:31 +0100239 psa_key_handle_t handle,
240 psa_algorithm_t alg,
241 const uint8_t *salt,
242 size_t salt_length,
243 const uint8_t *label,
244 size_t label_length,
245 size_t capacity);
246
Gilles Peskine6843c292019-01-18 16:44:49 +0100247/* FIXME Deprecated. Remove this as soon as all the tests are updated. */
248#define PSA_ALG_SELECT_RAW ((psa_algorithm_t)0x31000001)
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200249
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200250/** \defgroup policy Key policies
251 * @{
252 *
253 * The functions in this section are legacy interfaces where the properties
254 * of a key object are set after allocating a handle, in constrast with the
255 * preferred interface where key objects are created atomically from
256 * a structure that represents the properties.
257 */
258
259/** \def PSA_KEY_POLICY_INIT
260 *
261 * This macro returns a suitable initializer for a key policy object of type
262 * #psa_key_policy_t.
263 */
264#ifdef __DOXYGEN_ONLY__
265/* This is an example definition for documentation purposes.
266 * Implementations should define a suitable value in `crypto_struct.h`.
267 */
268#define PSA_KEY_POLICY_INIT {0}
269#endif
270
271/** Return an initial value for a key policy that forbids all usage of the key.
272 */
273static psa_key_policy_t psa_key_policy_init(void);
274
275/** \brief Set the standard fields of a policy structure.
276 *
277 * Note that this function does not make any consistency check of the
278 * parameters. The values are only checked when applying the policy to
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100279 * a key with psa_set_key_policy().
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200280 *
281 * \param[in,out] policy The key policy to modify. It must have been
282 * initialized as per the documentation for
283 * #psa_key_policy_t.
284 * \param usage The permitted uses for the key.
285 * \param alg The algorithm that the key may be used for.
286 */
287void psa_key_policy_set_usage(psa_key_policy_t *policy,
288 psa_key_usage_t usage,
289 psa_algorithm_t alg);
290
291/** \brief Retrieve the usage field of a policy structure.
292 *
293 * \param[in] policy The policy object to query.
294 *
295 * \return The permitted uses for a key with this policy.
296 */
297psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
298
299/** \brief Retrieve the algorithm field of a policy structure.
300 *
301 * \param[in] policy The policy object to query.
302 *
303 * \return The permitted algorithm for a key with this policy.
304 */
305psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
306
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100307/** \brief Set the usage policy for a key.
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200308 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100309 * This function must be called on a key handle before importing,
310 * generating or creating a key. Changing the policy of an
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200311 * existing key is not permitted.
312 *
313 * Implementations may set restrictions on supported key policies
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100314 * depending on the key type.
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200315 *
316 * \param handle Handle to the key whose policy is to be changed.
317 * \param[in] policy The policy object to query.
318 *
319 * \retval #PSA_SUCCESS
320 * Success.
321 * If the key is persistent, it is implementation-defined whether
322 * the policy has been saved to persistent storage. Implementations
323 * may defer saving the policy until the key material is created.
324 * \retval #PSA_ERROR_INVALID_HANDLE
325 * \retval #PSA_ERROR_ALREADY_EXISTS
326 * \retval #PSA_ERROR_NOT_SUPPORTED
327 * \retval #PSA_ERROR_INVALID_ARGUMENT
328 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
329 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200330 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200331 * \retval #PSA_ERROR_BAD_STATE
332 * The library has not been previously initialized by psa_crypto_init().
333 * It is implementation-dependent whether a failure to initialize
334 * results in this error code.
335 */
336psa_status_t psa_set_key_policy(psa_key_handle_t handle,
337 const psa_key_policy_t *policy);
338
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100339/** \brief Get the usage policy for a key.
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200340 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100341 * \param handle Handle to the key whose policy is being queried.
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200342 * \param[out] policy On success, the key's policy.
343 *
344 * \retval #PSA_SUCCESS
345 * \retval #PSA_ERROR_INVALID_HANDLE
346 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
347 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200348 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200349 * \retval #PSA_ERROR_BAD_STATE
350 * The library has not been previously initialized by psa_crypto_init().
351 * It is implementation-dependent whether a failure to initialize
352 * results in this error code.
353 */
354psa_status_t psa_get_key_policy(psa_key_handle_t handle,
355 psa_key_policy_t *policy);
356
357/**@}*/
358
Gilles Peskine87a5e562019-04-17 12:28:25 +0200359/** \defgroup to_handle Key creation to allocated handle
360 * @{
361 *
362 * The functions in this section are legacy interfaces where the properties
363 * of a key object are set after allocating a handle, in constrast with the
364 * preferred interface where key objects are created atomically from
365 * a structure that represents the properties.
366 */
367
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100368/** Create a new persistent key.
Gilles Peskine87a5e562019-04-17 12:28:25 +0200369 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100370 * Create a new persistent key and return a handle to it. The handle
Gilles Peskine87a5e562019-04-17 12:28:25 +0200371 * remains valid until the application calls psa_close_key() or terminates.
372 * The application can open the key again with psa_open_key() until it
373 * removes the key by calling psa_destroy_key().
374 *
375 * \param lifetime The lifetime of the key. This designates a storage
376 * area where the key material is stored. This must not
377 * be #PSA_KEY_LIFETIME_VOLATILE.
378 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100379 * \param[out] handle On success, a handle to the newly created key.
380 * When key material is later created in this key,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200381 * it will be saved to the specified persistent location.
382 *
383 * \retval #PSA_SUCCESS
384 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100385 * for key operations.
Gilles Peskine87a5e562019-04-17 12:28:25 +0200386 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
387 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
388 * \retval #PSA_ERROR_ALREADY_EXISTS
389 * There is already a key with the identifier \p id in the storage
390 * area designated by \p lifetime.
391 * \retval #PSA_ERROR_INVALID_ARGUMENT
392 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
393 * \retval #PSA_ERROR_INVALID_ARGUMENT
394 * \p id is invalid for the specified lifetime.
395 * \retval #PSA_ERROR_NOT_SUPPORTED
396 * \p lifetime is not supported.
397 * \retval #PSA_ERROR_NOT_PERMITTED
398 * \p lifetime is valid, but the application does not have the
399 * permission to create a key there.
400 */
401psa_status_t psa_create_key(psa_key_lifetime_t lifetime,
402 psa_key_id_t id,
403 psa_key_handle_t *handle);
404
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100405/** Allocate space for a transient key, i.e. a key which is only stored
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200406 * in volatile memory.
407 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100408 * The allocated key and its handle remain valid until the
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200409 * application calls psa_close_key() or psa_destroy_key() or until the
410 * application terminates.
411 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100412 * \param[out] handle On success, a handle to a volatile key.
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200413 *
414 * \retval #PSA_SUCCESS
415 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100416 * to refer to the key.
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200417 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100418 * There was not enough memory, or the maximum number of transient keys
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200419 * has been reached.
420 */
421psa_status_t psa_allocate_key(psa_key_handle_t *handle);
422
423/**
424 * \brief Get basic metadata about a key.
425 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100426 * \param handle Handle to the key to query.
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200427 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
428 * This may be a null pointer, in which case the key type
429 * is not written.
430 * \param[out] bits On success, the key size in bits.
431 * This may be a null pointer, in which case the key size
432 * is not written.
433 *
434 * \retval #PSA_SUCCESS
435 * \retval #PSA_ERROR_INVALID_HANDLE
436 * \retval #PSA_ERROR_DOES_NOT_EXIST
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100437 * The handle does not contain a key.
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200438 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
439 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200440 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200441 * \retval #PSA_ERROR_BAD_STATE
442 * The library has not been previously initialized by psa_crypto_init().
443 * It is implementation-dependent whether a failure to initialize
444 * results in this error code.
445 */
446psa_status_t psa_get_key_information(psa_key_handle_t handle,
447 psa_key_type_t *type,
448 size_t *bits);
449
Gilles Peskine87a5e562019-04-17 12:28:25 +0200450/** \brief Retrieve the lifetime of an open key.
451 *
452 * \param handle Handle to query.
453 * \param[out] lifetime On success, the lifetime value.
454 *
455 * \retval #PSA_SUCCESS
456 * Success.
457 * \retval #PSA_ERROR_INVALID_HANDLE
458 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
459 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200460 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine87a5e562019-04-17 12:28:25 +0200461 * \retval #PSA_ERROR_BAD_STATE
462 * The library has not been previously initialized by psa_crypto_init().
463 * It is implementation-dependent whether a failure to initialize
464 * results in this error code.
465 */
466psa_status_t psa_get_key_lifetime_from_handle(psa_key_handle_t handle,
467 psa_key_lifetime_t *lifetime);
468
469psa_status_t psa_import_key_to_handle(psa_key_handle_t handle,
470 psa_key_type_t type,
471 const uint8_t *data,
472 size_t data_length);
473
474psa_status_t psa_copy_key_to_handle(psa_key_handle_t source_handle,
475 psa_key_handle_t target_handle,
476 const psa_key_policy_t *constraint);
477
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100478psa_status_t psa_generate_derived_key_to_handle(psa_key_handle_t handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200479 psa_key_type_t type,
480 size_t bits,
Gilles Peskine35675b62019-05-16 17:26:11 +0200481 psa_key_derivation_operation_t *operation);
Gilles Peskine87a5e562019-04-17 12:28:25 +0200482
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200483psa_status_t psa_generate_key_to_handle(psa_key_handle_t handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200484 psa_key_type_t type,
485 size_t bits,
486 const void *extra,
487 size_t extra_size);
488
489/**@}*/
490
Gilles Peskine24f10f82019-05-16 12:18:32 +0200491
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200492/** \addtogroup crypto_types
493 * @{
494 */
495
Gilles Peskinea1302192019-05-16 13:58:24 +0200496/** DSA public key.
497 *
498 * The import and export format is the
499 * representation of the public key `y = g^x mod p` as a big-endian byte
500 * string. The length of the byte string is the length of the base prime `p`
501 * in bytes.
502 */
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200503#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x60020000)
Gilles Peskinea1302192019-05-16 13:58:24 +0200504
505/** DSA key pair (private and public key).
506 *
507 * The import and export format is the
508 * representation of the private key `x` as a big-endian byte string. The
509 * length of the byte string is the private key size in bytes (leading zeroes
510 * are not stripped).
511 *
512 * Determinstic DSA key derivation with psa_generate_derived_key follows
513 * FIPS 186-4 §B.1.2: interpret the byte string as integer
514 * in big-endian order. Discard it if it is not in the range
515 * [0, *N* - 2] where *N* is the boundary of the private key domain
516 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
517 * or the order of the curve's base point for ECC).
518 * Add 1 to the resulting integer and use this as the private key *x*.
519 *
520 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200521#define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t)0x70020000)
Gilles Peskinea1302192019-05-16 13:58:24 +0200522
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200523/** Whether a key type is an DSA key (pair or public-only). */
524#define PSA_KEY_TYPE_IS_DSA(type) \
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200525 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200526
527#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
528/** DSA signature with hashing.
529 *
530 * This is the signature scheme defined by FIPS 186-4,
531 * with a random per-message secret number (*k*).
532 *
533 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
534 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
535 * This includes #PSA_ALG_ANY_HASH
536 * when specifying the algorithm in a usage policy.
537 *
538 * \return The corresponding DSA signature algorithm.
539 * \return Unspecified if \p hash_alg is not a supported
540 * hash algorithm.
541 */
542#define PSA_ALG_DSA(hash_alg) \
543 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
544#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
545#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
546/** Deterministic DSA signature with hashing.
547 *
548 * This is the deterministic variant defined by RFC 6979 of
549 * the signature scheme defined by FIPS 186-4.
550 *
551 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
552 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
553 * This includes #PSA_ALG_ANY_HASH
554 * when specifying the algorithm in a usage policy.
555 *
556 * \return The corresponding DSA signature algorithm.
557 * \return Unspecified if \p hash_alg is not a supported
558 * hash algorithm.
559 */
560#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
561 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
562#define PSA_ALG_IS_DSA(alg) \
563 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
564 PSA_ALG_DSA_BASE)
565#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
566 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
567#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
568 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
569#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
570 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
571
572
573/* We need to expand the sample definition of this macro from
574 * the API definition. */
575#undef PSA_ALG_IS_HASH_AND_SIGN
576#define PSA_ALG_IS_HASH_AND_SIGN(alg) \
577 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
578 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg))
579
580/**@}*/
581
Gilles Peskine24f10f82019-05-16 12:18:32 +0200582/** \addtogroup attributes
583 * @{
584 */
585
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200586/** Custom Diffie-Hellman group.
587 *
588 * For keys of type #PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_GROUP_CUSTOM) or
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200589 * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_GROUP_CUSTOM), the group data comes
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200590 * from domain parameters set by psa_set_key_domain_parameters().
591 */
592/* This value is reserved for private use in the TLS named group registry. */
593#define PSA_DH_GROUP_CUSTOM ((psa_dh_group_t) 0x01fc)
594
595
Gilles Peskine24f10f82019-05-16 12:18:32 +0200596/**
597 * \brief Set domain parameters for a key.
598 *
599 * Some key types require additional domain parameters in addition to
600 * the key type identifier and the key size. Use this function instead
601 * of psa_set_key_type() when you need to specify domain parameters.
602 *
603 * The format for the required domain parameters varies based on the key type.
604 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200605 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine24f10f82019-05-16 12:18:32 +0200606 * the domain parameter data consists of the public exponent,
607 * represented as a big-endian integer with no leading zeros.
608 * This information is used when generating an RSA key pair.
609 * When importing a key, the public exponent is read from the imported
610 * key data and the exponent recorded in the attribute structure is ignored.
611 * As an exception, the public exponent 65537 is represented by an empty
612 * byte string.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200613 * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEY_PAIR),
Gilles Peskine24f10f82019-05-16 12:18:32 +0200614 * the `Dss-Parms` format as defined by RFC 3279 §2.3.2.
615 * ```
616 * Dss-Parms ::= SEQUENCE {
617 * p INTEGER,
618 * q INTEGER,
619 * g INTEGER
620 * }
621 * ```
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200622 * - For Diffie-Hellman key exchange keys
623 * (#PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_GROUP_CUSTOM) or
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200624 * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_GROUP_CUSTOM)), the
Gilles Peskine24f10f82019-05-16 12:18:32 +0200625 * `DomainParameters` format as defined by RFC 3279 §2.3.3.
626 * ```
627 * DomainParameters ::= SEQUENCE {
628 * p INTEGER, -- odd prime, p=jq +1
629 * g INTEGER, -- generator, g
630 * q INTEGER, -- factor of p-1
631 * j INTEGER OPTIONAL, -- subgroup factor
632 * validationParms ValidationParms OPTIONAL
633 * }
634 * ValidationParms ::= SEQUENCE {
635 * seed BIT STRING,
636 * pgenCounter INTEGER
637 * }
638 * ```
639 *
640 * \note This function may allocate memory or other resources.
641 * Once you have called this function on an attribute structure,
642 * you must call psa_reset_key_attributes() to free these resources.
643 *
644 * \note This is an experimental extension to the interface. It may change
645 * in future versions of the library.
646 *
647 * \param[in,out] attributes Attribute structure where the specified domain
648 * parameters will be stored.
649 * If this function fails, the content of
650 * \p attributes is not modified.
651 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
652 * \param[in] data Buffer containing the key domain parameters.
653 * The content of this buffer is interpreted
654 * according to \p type as described above.
655 * \param data_length Size of the \p data buffer in bytes.
656 *
657 * \retval #PSA_SUCCESS
658 * \retval #PSA_ERROR_INVALID_ARGUMENT
659 * \retval #PSA_ERROR_NOT_SUPPORTED
660 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
661 */
662psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
663 psa_key_type_t type,
664 const uint8_t *data,
665 size_t data_length);
666
667/**
668 * \brief Get domain parameters for a key.
669 *
670 * Get the domain parameters for a key with this function, if any. The format
671 * of the domain parameters written to \p data is specified in the
672 * documentation for psa_set_key_domain_parameters().
673 *
674 * \note This is an experimental extension to the interface. It may change
675 * in future versions of the library.
676 *
677 * \param[in] attributes The key attribute structure to query.
678 * \param[out] data On success, the key domain parameters.
679 * \param data_size Size of the \p data buffer in bytes.
680 * The buffer is guaranteed to be large
681 * enough if its size in bytes is at least
682 * the value given by
683 * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
684 * \param[out] data_length On success, the number of bytes
685 * that make up the key domain parameters data.
686 *
687 * \retval #PSA_SUCCESS
688 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
689 */
690psa_status_t psa_get_key_domain_parameters(
691 const psa_key_attributes_t *attributes,
692 uint8_t *data,
693 size_t data_size,
694 size_t *data_length);
695
696/** Safe output buffer size for psa_get_key_domain_parameters().
697 *
698 * This macro returns a compile-time constant if its arguments are
699 * compile-time constants.
700 *
701 * \warning This function may call its arguments multiple times or
702 * zero times, so you should not pass arguments that contain
703 * side effects.
704 *
705 * \note This is an experimental extension to the interface. It may change
706 * in future versions of the library.
707 *
708 * \param key_type A supported key type.
709 * \param key_bits The size of the key in bits.
710 *
711 * \return If the parameters are valid and supported, return
712 * a buffer size in bytes that guarantees that
713 * psa_get_key_domain_parameters() will not fail with
714 * #PSA_ERROR_BUFFER_TOO_SMALL.
715 * If the parameters are a valid combination that is not supported
Gilles Peskine27a983d2019-05-16 17:24:53 +0200716 * by the implementation, this macro shall return either a
Gilles Peskine24f10f82019-05-16 12:18:32 +0200717 * sensible size or 0.
718 * If the parameters are not valid, the
719 * return value is unspecified.
720 */
721#define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \
722 (PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) : \
723 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
724 PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
725 0)
726#define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
727 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/)
728#define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
729 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/)
730
731/**@}*/
732
Gilles Peskinee59236f2018-01-27 23:32:46 +0100733#ifdef __cplusplus
734}
735#endif
736
737#endif /* PSA_CRYPTO_EXTRA_H */