blob: c58abda240ea00b4da070279233ac0ec92a5eb37 [file] [log] [blame]
Antonio de Angelis14276e92018-07-10 14:35:43 +01001/*
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002 * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01007/**
Jamie Foxcc31d402019-01-28 17:13:52 +00008 * \file psa/crypto.h
Antonio de Angelis14276e92018-07-10 14:35:43 +01009 * \brief Platform Security Architecture cryptography module
10 */
11
12#ifndef PSA_CRYPTO_H
13#define PSA_CRYPTO_H
14
15#include <stddef.h>
16
17#ifdef __DOXYGEN_ONLY__
18/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
Jamie Fox0e54ebc2019-04-09 14:21:04 +010019 * must be defined in the crypto_platform.h header. These mock definitions
Antonio de Angelis14276e92018-07-10 14:35:43 +010020 * are present in this file as a convenience to generate pretty-printed
Antonio de Angelis377a1552018-11-22 17:02:40 +000021 * documentation that includes those definitions. */
Antonio de Angelis14276e92018-07-10 14:35:43 +010022
23/** \defgroup platform Implementation-specific definitions
24 * @{
25 */
26
Jamie Fox0e54ebc2019-04-09 14:21:04 +010027/** \brief Key handle.
Antonio de Angelis14276e92018-07-10 14:35:43 +010028 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +010029 * This type represents open handles to keys. It must be an unsigned integral
Antonio de Angelis14276e92018-07-10 14:35:43 +010030 * type. The choice of type is implementation-dependent.
Antonio de Angelis14276e92018-07-10 14:35:43 +010031 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +010032 * 0 is not a valid key handle. How other handle values are assigned is
33 * implementation-dependent.
Antonio de Angelis14276e92018-07-10 14:35:43 +010034 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +010035typedef _unsigned_integral_type_ psa_key_handle_t;
Antonio de Angelis14276e92018-07-10 14:35:43 +010036
37/**@}*/
38#endif /* __DOXYGEN_ONLY__ */
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
Jamie Fox0e54ebc2019-04-09 14:21:04 +010044/* The file "crypto_types.h" declares types that encode errors,
45 * algorithms, key types, policies, etc. */
Jamie Foxcc31d402019-01-28 17:13:52 +000046#include "psa/crypto_types.h"
Jamie Fox0e54ebc2019-04-09 14:21:04 +010047
Antonio de Angelis04debbd2019-10-14 12:12:52 +010048/** \defgroup version API version
49 * @{
50 */
51
52/**
53 * The major version of this implementation of the PSA Crypto API
54 */
55#define PSA_CRYPTO_API_VERSION_MAJOR 1
56
57/**
58 * The minor version of this implementation of the PSA Crypto API
59 */
60#define PSA_CRYPTO_API_VERSION_MINOR 0
61
62/**@}*/
63
Jamie Fox0e54ebc2019-04-09 14:21:04 +010064/* The file "crypto_values.h" declares macros to build and analyze values
65 * of integral types defined in "crypto_types.h". */
Jamie Foxcc31d402019-01-28 17:13:52 +000066#include "psa/crypto_values.h"
Jamie Fox0e54ebc2019-04-09 14:21:04 +010067
68/** \defgroup initialization Library initialization
Antonio de Angelis14276e92018-07-10 14:35:43 +010069 * @{
70 */
71
Antonio de Angelis14276e92018-07-10 14:35:43 +010072/**
73 * \brief Library initialization.
74 *
75 * Applications must call this function before calling any other
76 * function in this module.
77 *
78 * Applications may call this function more than once. Once a call
79 * succeeds, subsequent calls are guaranteed to succeed.
80 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +010081 * If the application calls other functions before calling psa_crypto_init(),
82 * the behavior is undefined. Implementations are encouraged to either perform
83 * the operation as if the library had been initialized or to return
84 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
85 * implementations should not return a success status if the lack of
86 * initialization may have security implications, for example due to improper
87 * seeding of the random number generator.
88 *
Antonio de Angelis377a1552018-11-22 17:02:40 +000089 * \retval #PSA_SUCCESS
90 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
91 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
92 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +010093 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Antonio de Angelis377a1552018-11-22 17:02:40 +000094 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Antonio de Angelis14276e92018-07-10 14:35:43 +010095 */
96psa_status_t psa_crypto_init(void);
97
Antonio de Angelis14276e92018-07-10 14:35:43 +010098/**@}*/
99
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100100/** \addtogroup attributes
Antonio de Angelis14276e92018-07-10 14:35:43 +0100101 * @{
102 */
103
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100104/** \def PSA_KEY_ATTRIBUTES_INIT
Antonio de Angelis377a1552018-11-22 17:02:40 +0000105 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100106 * This macro returns a suitable initializer for a key attribute structure
107 * of type #psa_key_attributes_t.
Antonio de Angelis8908f472018-08-31 15:44:25 +0100108 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100109#ifdef __DOXYGEN_ONLY__
110/* This is an example definition for documentation purposes.
111 * Implementations should define a suitable value in `crypto_struct.h`.
112 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100113#define PSA_KEY_ATTRIBUTES_INIT {0}
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100114#endif
115
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100116/** Return an initial value for a key attributes structure.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100117 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100118static psa_key_attributes_t psa_key_attributes_init(void);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100119
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100120/** Declare a key as persistent and set its key identifier.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100121 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100122 * If the attribute structure currently declares the key as volatile (which
123 * is the default content of an attribute structure), this function sets
124 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000125 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100126 * This function does not access storage, it merely stores the given
127 * value in the structure.
128 * The persistent key will be written to storage when the attribute
129 * structure is passed to a key creation function such as
130 * psa_import_key(), psa_generate_key(),
131 * psa_key_derivation_output_key() or psa_copy_key().
132 *
133 * This function may be declared as `static` (i.e. without external
134 * linkage). This function may be provided as a function-like macro,
135 * but in this case it must evaluate each of its arguments exactly once.
136 *
137 * \param[out] attributes The attribute structure to write to.
138 * \param id The persistent identifier for the key.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100139 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100140static void psa_set_key_id(psa_key_attributes_t *attributes,
141 psa_key_id_t id);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100142
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100143/** Set the location of a persistent key.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000144 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100145 * To make a key persistent, you must give it a persistent key identifier
146 * with psa_set_key_id(). By default, a key that has a persistent identifier
147 * is stored in the default storage area identifier by
148 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
149 * area, or to explicitly declare the key as volatile.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000150 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100151 * This function does not access storage, it merely stores the given
152 * value in the structure.
153 * The persistent key will be written to storage when the attribute
154 * structure is passed to a key creation function such as
155 * psa_import_key(), psa_generate_key(),
156 * psa_key_derivation_output_key() or psa_copy_key().
157 *
158 * This function may be declared as `static` (i.e. without external
159 * linkage). This function may be provided as a function-like macro,
160 * but in this case it must evaluate each of its arguments exactly once.
161 *
162 * \param[out] attributes The attribute structure to write to.
163 * \param lifetime The lifetime for the key.
164 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
165 * key will be volatile, and the key identifier
166 * attribute is reset to 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000167 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100168static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
169 psa_key_lifetime_t lifetime);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100170
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100171/** Retrieve the key identifier from key attributes.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000172 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100173 * This function may be declared as `static` (i.e. without external
174 * linkage). This function may be provided as a function-like macro,
175 * but in this case it must evaluate its argument exactly once.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000176 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100177 * \param[in] attributes The key attribute structure to query.
178 *
179 * \return The persistent identifier stored in the attribute structure.
180 * This value is unspecified if the attribute structure declares
181 * the key as volatile.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000182 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100183static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100184
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100185/** Retrieve the lifetime from key attributes.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100186 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100187 * This function may be declared as `static` (i.e. without external
188 * linkage). This function may be provided as a function-like macro,
189 * but in this case it must evaluate its argument exactly once.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100190 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100191 * \param[in] attributes The key attribute structure to query.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000192 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100193 * \return The lifetime value stored in the attribute structure.
194 */
195static psa_key_lifetime_t psa_get_key_lifetime(
196 const psa_key_attributes_t *attributes);
197
198/** Declare usage flags for a key.
199 *
200 * Usage flags are part of a key's usage policy. They encode what
201 * kind of operations are permitted on the key. For more details,
202 * refer to the documentation of the type #psa_key_usage_t.
203 *
204 * This function overwrites any usage flags
205 * previously set in \p attributes.
206 *
207 * This function may be declared as `static` (i.e. without external
208 * linkage). This function may be provided as a function-like macro,
209 * but in this case it must evaluate each of its arguments exactly once.
210 *
211 * \param[out] attributes The attribute structure to write to.
212 * \param usage_flags The usage flags to write.
213 */
214static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
215 psa_key_usage_t usage_flags);
216
217/** Retrieve the usage flags from key attributes.
218 *
219 * This function may be declared as `static` (i.e. without external
220 * linkage). This function may be provided as a function-like macro,
221 * but in this case it must evaluate its argument exactly once.
222 *
223 * \param[in] attributes The key attribute structure to query.
224 *
225 * \return The usage flags stored in the attribute structure.
226 */
227static psa_key_usage_t psa_get_key_usage_flags(
228 const psa_key_attributes_t *attributes);
229
230/** Declare the permitted algorithm policy for a key.
231 *
232 * The permitted algorithm policy of a key encodes which algorithm or
233 * algorithms are permitted to be used with this key. The following
234 * algorithm policies are supported:
235 * - 0 does not allow any cryptographic operation with the key. The key
236 * may be used for non-cryptographic actions such as exporting (if
237 * permitted by the usage flags).
238 * - An algorithm value permits this particular algorithm.
239 * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
240 * signature scheme with any hash algorithm.
241 *
242 * This function overwrites any algorithm policy
243 * previously set in \p attributes.
244 *
245 * This function may be declared as `static` (i.e. without external
246 * linkage). This function may be provided as a function-like macro,
247 * but in this case it must evaluate each of its arguments exactly once.
248 *
249 * \param[out] attributes The attribute structure to write to.
250 * \param alg The permitted algorithm policy to write.
251 */
252static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
253 psa_algorithm_t alg);
254
255
256/** Retrieve the algorithm policy from key attributes.
257 *
258 * This function may be declared as `static` (i.e. without external
259 * linkage). This function may be provided as a function-like macro,
260 * but in this case it must evaluate its argument exactly once.
261 *
262 * \param[in] attributes The key attribute structure to query.
263 *
264 * \return The algorithm stored in the attribute structure.
265 */
266static psa_algorithm_t psa_get_key_algorithm(
267 const psa_key_attributes_t *attributes);
268
269/** Declare the type of a key.
270 *
271 * This function overwrites any key type
272 * previously set in \p attributes.
273 *
274 * This function may be declared as `static` (i.e. without external
275 * linkage). This function may be provided as a function-like macro,
276 * but in this case it must evaluate each of its arguments exactly once.
277 *
278 * \param[out] attributes The attribute structure to write to.
279 * \param type The key type to write.
280 * If this is 0, the key type in \p attributes
281 * becomes unspecified.
282 */
283static void psa_set_key_type(psa_key_attributes_t *attributes,
284 psa_key_type_t type);
285
286
287/** Declare the size of a key.
288 *
289 * This function overwrites any key size previously set in \p attributes.
290 *
291 * This function may be declared as `static` (i.e. without external
292 * linkage). This function may be provided as a function-like macro,
293 * but in this case it must evaluate each of its arguments exactly once.
294 *
295 * \param[out] attributes The attribute structure to write to.
296 * \param bits The key size in bits.
297 * If this is 0, the key size in \p attributes
298 * becomes unspecified. Keys of size 0 are
299 * not supported.
300 */
301static void psa_set_key_bits(psa_key_attributes_t *attributes,
302 size_t bits);
303
304/** Retrieve the key type from key attributes.
305 *
306 * This function may be declared as `static` (i.e. without external
307 * linkage). This function may be provided as a function-like macro,
308 * but in this case it must evaluate its argument exactly once.
309 *
310 * \param[in] attributes The key attribute structure to query.
311 *
312 * \return The key type stored in the attribute structure.
313 */
314static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
315
316/** Retrieve the key size from key attributes.
317 *
318 * This function may be declared as `static` (i.e. without external
319 * linkage). This function may be provided as a function-like macro,
320 * but in this case it must evaluate its argument exactly once.
321 *
322 * \param[in] attributes The key attribute structure to query.
323 *
324 * \return The key size stored in the attribute structure, in bits.
325 */
326static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
327
328/** Retrieve the attributes of a key.
329 *
330 * This function first resets the attribute structure as with
331 * psa_reset_key_attributes(). It then copies the attributes of
332 * the given key into the given attribute structure.
333 *
334 * \note This function may allocate memory or other resources.
335 * Once you have called this function on an attribute structure,
336 * you must call psa_reset_key_attributes() to free these resources.
337 *
338 * \param[in] handle Handle to the key to query.
339 * \param[in,out] attributes On success, the attributes of the key.
340 * On failure, equivalent to a
341 * freshly-initialized structure.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000342 *
343 * \retval #PSA_SUCCESS
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100344 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100345 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Antonio de Angelis377a1552018-11-22 17:02:40 +0000346 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100347 * \retval #PSA_ERROR_CORRUPTION_DETECTED
348 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100349 * \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.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100353 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100354psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
355 psa_key_attributes_t *attributes);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100356
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100357/** Reset a key attribute structure to a freshly initialized state.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000358 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100359 * You must initialize the attribute structure as described in the
360 * documentation of the type #psa_key_attributes_t before calling this
361 * function. Once the structure has been initialized, you may call this
362 * function at any time.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000363 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100364 * This function frees any auxiliary resources that the structure
365 * may contain.
366 *
367 * \param[in,out] attributes The attribute structure to reset.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000368 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100369void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100370
371/**@}*/
372
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100373/** \defgroup key_management Key management
Antonio de Angelis14276e92018-07-10 14:35:43 +0100374 * @{
375 */
376
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100377/** Open a handle to an existing persistent key.
378 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100379 * Open a handle to a persistent key. A key is persistent if it was created
380 * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
381 * always has a nonzero key identifier, set with psa_set_key_id() when
382 * creating the key. Implementations may provide additional pre-provisioned
383 * keys that can be opened with psa_open_key(). Such keys have a key identifier
384 * in the vendor range, as documented in the description of #psa_key_id_t.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100385 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100386 * The application must eventually close the handle with psa_close_key() or
387 * psa_destroy_key() to release associated resources. If the application dies
388 * without calling one of these functions, the implementation should perform
389 * the equivalent of a call to psa_close_key().
390 *
391 * Some implementations permit an application to open the same key multiple
392 * times. If this is successful, each call to psa_open_key() will return a
393 * different key handle.
394 *
395 * \note Applications that rely on opening a key multiple times will not be
396 * portable to implementations that only permit a single key handle to be
397 * opened. See also :ref:\`key-handles\`.
398 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100399 * \param id The persistent identifier of the key.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100400 * \param[out] handle On success, a handle to the key.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100401 *
402 * \retval #PSA_SUCCESS
403 * Success. The application can now use the value of `*handle`
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100404 * to access the key.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100405 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100406 * The implementation does not have sufficient resources to open the
407 * key. This can be due to reaching an implementation limit on the
408 * number of open keys, the number of open key handles, or available
409 * memory.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100410 * \retval #PSA_ERROR_DOES_NOT_EXIST
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100411 * There is no persistent key with key identifier \p id.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100412 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100413 * \p id is not a valid persistent key identifier.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100414 * \retval #PSA_ERROR_NOT_PERMITTED
415 * The specified key exists, but the application does not have the
416 * permission to access it. Note that this specification does not
417 * define any way to create such a key, but it may be possible
418 * through implementation-specific means.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100419 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
420 * \retval #PSA_ERROR_CORRUPTION_DETECTED
421 * \retval #PSA_ERROR_STORAGE_FAILURE
422 * \retval #PSA_ERROR_BAD_STATE
423 * The library has not been previously initialized by psa_crypto_init().
424 * It is implementation-dependent whether a failure to initialize
425 * results in this error code.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100426 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100427psa_status_t psa_open_key(psa_key_id_t id,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100428 psa_key_handle_t *handle);
429
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100430
431/** Close a key handle.
432 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100433 * If the handle designates a volatile key, this will destroy the key material
434 * and free all associated resources, just like psa_destroy_key().
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100435 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100436 * If this is the last open handle to a persistent key, then closing the handle
437 * will free all resources associated with the key in volatile memory. The key
438 * data in persistent storage is not affected and can be opened again later
439 * with a call to psa_open_key().
440 *
441 * Closing the key handle makes the handle invalid, and the key handle
442 * must not be used again by the application.
443 *
444 * \note If the key handle was used to set up an active
445 * :ref:\`multipart operation <multipart-operations>\`, then closing the
446 * key handle can cause the multipart operation to fail. Applications should
447 * maintain the key handle until after the multipart operation has finished.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100448 *
449 * \param handle The key handle to close.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100450 * If this is \c 0, do nothing and return \c PSA_SUCCESS.
451 *
452 * \retval #PSA_SUCCESS
453 * \p handle was a valid handle or \c 0. It is now closed.
454 * \retval #PSA_ERROR_INVALID_HANDLE
455 * \p handle is not a valid handle nor \c 0.
456 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
457 * \retval #PSA_ERROR_CORRUPTION_DETECTED
458 * \retval #PSA_ERROR_BAD_STATE
459 * The library has not been previously initialized by psa_crypto_init().
460 * It is implementation-dependent whether a failure to initialize
461 * results in this error code.
462 */
463psa_status_t psa_close_key(psa_key_handle_t handle);
464
465/** Make a copy of a key.
466 *
467 * Copy key material from one location to another.
468 *
469 * This function is primarily useful to copy a key from one location
470 * to another, since it populates a key using the material from
471 * another key which may have a different lifetime.
472 *
473 * This function may be used to share a key with a different party,
474 * subject to implementation-defined restrictions on key sharing.
475 *
476 * The policy on the source key must have the usage flag
477 * #PSA_KEY_USAGE_COPY set.
478 * This flag is sufficient to permit the copy if the key has the lifetime
479 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
480 * Some secure elements do not provide a way to copy a key without
481 * making it extractable from the secure element. If a key is located
482 * in such a secure element, then the key must have both usage flags
483 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
484 * a copy of the key outside the secure element.
485 *
486 * The resulting key may only be used in a way that conforms to
487 * both the policy of the original key and the policy specified in
488 * the \p attributes parameter:
489 * - The usage flags on the resulting key are the bitwise-and of the
490 * usage flags on the source policy and the usage flags in \p attributes.
491 * - If both allow the same algorithm or wildcard-based
492 * algorithm policy, the resulting key has the same algorithm policy.
493 * - If either of the policies allows an algorithm and the other policy
494 * allows a wildcard-based algorithm policy that includes this algorithm,
495 * the resulting key allows the same algorithm.
496 * - If the policies do not allow any algorithm in common, this function
497 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
498 *
499 * The effect of this function on implementation-defined attributes is
500 * implementation-defined.
501 *
502 * \param source_handle The key to copy. It must be a valid key handle.
503 * \param[in] attributes The attributes for the new key.
504 * They are used as follows:
505 * - The key type and size may be 0. If either is
506 * nonzero, it must match the corresponding
507 * attribute of the source key.
508 * - The key location (the lifetime and, for
509 * persistent keys, the key identifier) is
510 * used directly.
511 * - The policy constraints (usage flags and
512 * algorithm policy) are combined from
513 * the source key and \p attributes so that
514 * both sets of restrictions apply, as
515 * described in the documentation of this function.
516 * \param[out] target_handle On success, a handle to the newly created key.
517 * \c 0 on failure.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100518 *
519 * \retval #PSA_SUCCESS
520 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100521 * \p source_handle is invalid.
522 * \retval #PSA_ERROR_ALREADY_EXISTS
523 * This is an attempt to create a persistent key, and there is
524 * already a persistent key with the given identifier.
525 * \retval #PSA_ERROR_INVALID_ARGUMENT
526 * The lifetime or identifier in \p attributes are invalid.
527 * \retval #PSA_ERROR_INVALID_ARGUMENT
528 * The policy constraints on the source and specified in
529 * \p attributes are incompatible.
530 * \retval #PSA_ERROR_INVALID_ARGUMENT
531 * \p attributes specifies a key type or key size
532 * which does not match the attributes of the source key.
533 * \retval #PSA_ERROR_NOT_PERMITTED
534 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
535 * \retval #PSA_ERROR_NOT_PERMITTED
536 * The source key is not exportable and its lifetime does not
537 * allow copying it to the target's lifetime.
538 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
539 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100540 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100541 * \retval #PSA_ERROR_HARDWARE_FAILURE
542 * \retval #PSA_ERROR_STORAGE_FAILURE
543 * \retval #PSA_ERROR_CORRUPTION_DETECTED
544 * \retval #PSA_ERROR_BAD_STATE
545 * The library has not been previously initialized by psa_crypto_init().
546 * It is implementation-dependent whether a failure to initialize
547 * results in this error code.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100548 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100549psa_status_t psa_copy_key(psa_key_handle_t source_handle,
550 const psa_key_attributes_t *attributes,
551 psa_key_handle_t *target_handle);
552
553
554/**
555 * \brief Destroy a key.
556 *
557 * This function destroys a key from both volatile
558 * memory and, if applicable, non-volatile storage. Implementations shall
559 * make a best effort to ensure that that the key material cannot be recovered.
560 *
561 * This function also erases any metadata such as policies and frees
562 * resources associated with the key. To free all resources associated with
563 * the key, all handles to the key must be closed or destroyed.
564 *
565 * Destroying the key makes the handle invalid, and the key handle
566 * must not be used again by the application. Using other open handles to the
567 * destroyed key in a cryptographic operation will result in an error.
568 *
569 * If a key is currently in use in a multipart operation, then destroying the
570 * key will cause the multipart operation to fail.
571 *
572 * \param handle Handle to the key to erase.
573 * If this is \c 0, do nothing and return \c PSA_SUCCESS.
574 *
575 * \retval #PSA_SUCCESS
576 * \p handle was a valid handle and the key material that it
577 * referred to has been erased.
578 * Alternatively, \p handle is \c 0.
579 * \retval #PSA_ERROR_NOT_PERMITTED
580 * The key cannot be erased because it is
581 * read-only, either due to a policy or due to physical restrictions.
582 * \retval #PSA_ERROR_INVALID_HANDLE
583 * \p handle is not a valid handle nor \c 0.
584 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
585 * There was an failure in communication with the cryptoprocessor.
586 * The key material may still be present in the cryptoprocessor.
587 * \retval #PSA_ERROR_STORAGE_FAILURE
588 * The storage is corrupted. Implementations shall make a best effort
589 * to erase key material even in this stage, however applications
590 * should be aware that it may be impossible to guarantee that the
591 * key material is not recoverable in such cases.
592 * \retval #PSA_ERROR_CORRUPTION_DETECTED
593 * An unexpected condition which is not a storage corruption or
594 * a communication failure occurred. The cryptoprocessor may have
595 * been compromised.
596 * \retval #PSA_ERROR_BAD_STATE
597 * The library has not been previously initialized by psa_crypto_init().
598 * It is implementation-dependent whether a failure to initialize
599 * results in this error code.
600 */
601psa_status_t psa_destroy_key(psa_key_handle_t handle);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100602
603/**@}*/
604
605/** \defgroup import_export Key import and export
606 * @{
607 */
608
609/**
610 * \brief Import a key in binary format.
611 *
612 * This function supports any output from psa_export_key(). Refer to the
613 * documentation of psa_export_public_key() for the format of public keys
614 * and to the documentation of psa_export_key() for the format for
615 * other key types.
616 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100617 * The key data determines the key size. The attributes may optionally
618 * specify a key size; in this case it must match the size determined
619 * from the key data. A key size of 0 in \p attributes indicates that
620 * the key size is solely determined by the key data.
621 *
622 * Implementations must reject an attempt to import a key of size 0.
623 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100624 * This specification supports a single format for each key type.
625 * Implementations may support other formats as long as the standard
626 * format is supported. Implementations that support other formats
627 * should ensure that the formats are clearly unambiguous so as to
628 * minimize the risk that an invalid input is accidentally interpreted
629 * according to a different format.
630 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100631 * \param[in] attributes The attributes for the new key.
632 * The key size is always determined from the
633 * \p data buffer.
634 * If the key size in \p attributes is nonzero,
635 * it must be equal to the size from \p data.
636 * \param[out] handle On success, a handle to the newly created key.
637 * \c 0 on failure.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100638 * \param[in] data Buffer containing the key data. The content of this
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100639 * buffer is interpreted according to the type declared
640 * in \p attributes.
641 * All implementations must support at least the format
642 * described in the documentation
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100643 * of psa_export_key() or psa_export_public_key() for
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100644 * the chosen type. Implementations may allow other
645 * formats, but should be conservative: implementations
646 * should err on the side of rejecting content if it
647 * may be erroneous (e.g. wrong type or truncated data).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100648 * \param data_length Size of the \p data buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100649 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000650 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +0100651 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100652 * If the key is persistent, the key material and the key's metadata
653 * have been saved to persistent storage.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100654 * \retval #PSA_ERROR_ALREADY_EXISTS
655 * This is an attempt to create a persistent key, and there is
656 * already a persistent key with the given identifier.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100657 * \retval #PSA_ERROR_NOT_SUPPORTED
658 * The key type or key size is not supported, either by the
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100659 * implementation in general or in this particular persistent location.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000660 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100661 * The key attributes, as a whole, are invalid.
662 * \retval #PSA_ERROR_INVALID_ARGUMENT
663 * The key data is not correctly formatted.
664 * \retval #PSA_ERROR_INVALID_ARGUMENT
665 * The size in \p attributes is nonzero and does not match the size
666 * of the key data.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100667 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
668 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
669 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
670 * \retval #PSA_ERROR_STORAGE_FAILURE
671 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100672 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100673 * \retval #PSA_ERROR_BAD_STATE
674 * The library has not been previously initialized by psa_crypto_init().
675 * It is implementation-dependent whether a failure to initialize
676 * results in this error code.
677 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100678psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100679 const uint8_t *data,
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100680 size_t data_length,
681 psa_key_handle_t *handle);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100682
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100683
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100684
685/**
686 * \brief Export a key in binary format.
687 *
688 * The output of this function can be passed to psa_import_key() to
689 * create an equivalent object.
690 *
691 * If the implementation of psa_import_key() supports other formats
692 * beyond the format specified here, the output from psa_export_key()
693 * must use the representation specified here, not the original
694 * representation.
695 *
696 * For standard key types, the output format is as follows:
697 *
698 * - For symmetric keys (including MAC keys), the format is the
699 * raw bytes of the key.
700 * - For DES, the key data consists of 8 bytes. The parity bits must be
701 * correct.
702 * - For Triple-DES, the format is the concatenation of the
703 * two or three DES keys.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100704 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100705 * is the non-encrypted DER encoding of the representation defined by
706 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
707 * ```
708 * RSAPrivateKey ::= SEQUENCE {
709 * version INTEGER, -- must be 0
710 * modulus INTEGER, -- n
711 * publicExponent INTEGER, -- e
712 * privateExponent INTEGER, -- d
713 * prime1 INTEGER, -- p
714 * prime2 INTEGER, -- q
715 * exponent1 INTEGER, -- d mod (p-1)
716 * exponent2 INTEGER, -- d mod (q-1)
717 * coefficient INTEGER, -- (inverse of q) mod p
718 * }
719 * ```
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100720 * - For elliptic curve key pairs (key types for which
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100721 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100722 * a representation of the private value as a `ceiling(m/8)`-byte string
723 * where `m` is the bit size associated with the curve, i.e. the bit size
724 * of the order of the curve's coordinate field. This byte string is
725 * in little-endian order for Montgomery curves (curve types
Summer Qin0e5b2e02020-10-22 11:23:39 +0800726 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
727 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
728 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
729 * For Weierstrass curves, this is the content of the `privateKey` field of
730 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
731 * the format is defined by RFC 7748, and output is masked according to §5.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100732 * - For Diffie-Hellman key exchange key pairs (key types for which
733 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
734 * format is the representation of the private key `x` as a big-endian byte
735 * string. The length of the byte string is the private key size in bytes
736 * (leading zeroes are not stripped).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100737 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
738 * true), the format is the same as for psa_export_public_key().
739 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100740 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
741 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100742 * \param handle Handle to the key to export.
743 * \param[out] data Buffer where the key data is to be written.
744 * \param data_size Size of the \p data buffer in bytes.
745 * \param[out] data_length On success, the number of bytes
746 * that make up the key data.
747 *
748 * \retval #PSA_SUCCESS
749 * \retval #PSA_ERROR_INVALID_HANDLE
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100750 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100751 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000752 * \retval #PSA_ERROR_NOT_SUPPORTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100753 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
754 * The size of the \p data buffer is too small. You can determine a
755 * sufficient buffer size by calling
756 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
757 * where \c type is the key type
758 * and \c bits is the key size in bits.
759 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
760 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100761 * \retval #PSA_ERROR_CORRUPTION_DETECTED
762 * \retval #PSA_ERROR_STORAGE_FAILURE
763 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100764 * \retval #PSA_ERROR_BAD_STATE
765 * The library has not been previously initialized by psa_crypto_init().
766 * It is implementation-dependent whether a failure to initialize
767 * results in this error code.
768 */
769psa_status_t psa_export_key(psa_key_handle_t handle,
770 uint8_t *data,
771 size_t data_size,
772 size_t *data_length);
773
774/**
775 * \brief Export a public key or the public part of a key pair in binary format.
776 *
777 * The output of this function can be passed to psa_import_key() to
778 * create an object that is equivalent to the public key.
779 *
780 * This specification supports a single format for each key type.
781 * Implementations may support other formats as long as the standard
782 * format is supported. Implementations that support other formats
783 * should ensure that the formats are clearly unambiguous so as to
784 * minimize the risk that an invalid input is accidentally interpreted
785 * according to a different format.
786 *
787 * For standard key types, the output format is as follows:
788 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
789 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
790 * ```
791 * RSAPublicKey ::= SEQUENCE {
792 * modulus INTEGER, -- n
793 * publicExponent INTEGER } -- e
794 * ```
795 * - For elliptic curve public keys (key types for which
796 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100797 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100798 * Let `m` be the bit size associated with the curve, i.e. the bit size of
799 * `q` for a curve over `F_q`. The representation consists of:
800 * - The byte 0x04;
801 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
802 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100803 * - For Diffie-Hellman key exchange public keys (key types for which
804 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
805 * the format is the representation of the public key `y = g^x mod p` as a
806 * big-endian byte string. The length of the byte string is the length of the
807 * base prime `p` in bytes.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100808 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100809 * Exporting a public key object or the public part of a key pair is
810 * always permitted, regardless of the key's usage flags.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100811 *
812 * \param handle Handle to the key to export.
813 * \param[out] data Buffer where the key data is to be written.
814 * \param data_size Size of the \p data buffer in bytes.
815 * \param[out] data_length On success, the number of bytes
816 * that make up the key data.
817 *
818 * \retval #PSA_SUCCESS
819 * \retval #PSA_ERROR_INVALID_HANDLE
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100820 * \retval #PSA_ERROR_INVALID_ARGUMENT
821 * The key is neither a public key nor a key pair.
822 * \retval #PSA_ERROR_NOT_SUPPORTED
823 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
824 * The size of the \p data buffer is too small. You can determine a
825 * sufficient buffer size by calling
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100826 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100827 * where \c type is the key type
828 * and \c bits is the key size in bits.
829 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
830 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100831 * \retval #PSA_ERROR_CORRUPTION_DETECTED
832 * \retval #PSA_ERROR_STORAGE_FAILURE
833 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100834 * \retval #PSA_ERROR_BAD_STATE
835 * The library has not been previously initialized by psa_crypto_init().
836 * It is implementation-dependent whether a failure to initialize
837 * results in this error code.
838 */
839psa_status_t psa_export_public_key(psa_key_handle_t handle,
840 uint8_t *data,
841 size_t data_size,
842 size_t *data_length);
843
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100844
Antonio de Angelis14276e92018-07-10 14:35:43 +0100845
846/**@}*/
847
848/** \defgroup hash Message digests
849 * @{
850 */
851
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100852/** Calculate the hash (digest) of a message.
853 *
854 * \note To verify the hash of a message against an
855 * expected value, use psa_hash_compare() instead.
856 *
857 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
858 * such that #PSA_ALG_IS_HASH(\p alg) is true).
859 * \param[in] input Buffer containing the message to hash.
860 * \param input_length Size of the \p input buffer in bytes.
861 * \param[out] hash Buffer where the hash is to be written.
862 * \param hash_size Size of the \p hash buffer in bytes.
863 * \param[out] hash_length On success, the number of bytes
864 * that make up the hash value. This is always
865 * #PSA_HASH_SIZE(\p alg).
866 *
867 * \retval #PSA_SUCCESS
868 * Success.
869 * \retval #PSA_ERROR_NOT_SUPPORTED
870 * \p alg is not supported or is not a hash algorithm.
871 * \retval #PSA_ERROR_INVALID_ARGUMENT
872 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
873 * \p hash_size is too small
874 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
875 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
876 * \retval #PSA_ERROR_HARDWARE_FAILURE
877 * \retval #PSA_ERROR_CORRUPTION_DETECTED
878 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
879 * \retval #PSA_ERROR_BAD_STATE
880 * The library has not been previously initialized by psa_crypto_init().
881 * It is implementation-dependent whether a failure to initialize
882 * results in this error code.
883 */
884psa_status_t psa_hash_compute(psa_algorithm_t alg,
885 const uint8_t *input,
886 size_t input_length,
887 uint8_t *hash,
888 size_t hash_size,
889 size_t *hash_length);
890
891/** Calculate the hash (digest) of a message and compare it with a
892 * reference value.
893 *
894 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
895 * such that #PSA_ALG_IS_HASH(\p alg) is true).
896 * \param[in] input Buffer containing the message to hash.
897 * \param input_length Size of the \p input buffer in bytes.
898 * \param[out] hash Buffer containing the expected hash value.
899 * \param hash_length Size of the \p hash buffer in bytes.
900 *
901 * \retval #PSA_SUCCESS
902 * The expected hash is identical to the actual hash of the input.
903 * \retval #PSA_ERROR_INVALID_SIGNATURE
904 * The hash of the message was calculated successfully, but it
905 * differs from the expected hash.
906 * \retval #PSA_ERROR_NOT_SUPPORTED
907 * \p alg is not supported or is not a hash algorithm.
908 * \retval #PSA_ERROR_INVALID_ARGUMENT
909 * \p input_length or \p hash_length do not match the hash size for \p alg
910 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
911 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
912 * \retval #PSA_ERROR_HARDWARE_FAILURE
913 * \retval #PSA_ERROR_CORRUPTION_DETECTED
914 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
915 * \retval #PSA_ERROR_BAD_STATE
916 * The library has not been previously initialized by psa_crypto_init().
917 * It is implementation-dependent whether a failure to initialize
918 * results in this error code.
919 */
920psa_status_t psa_hash_compare(psa_algorithm_t alg,
921 const uint8_t *input,
922 size_t input_length,
923 const uint8_t *hash,
Soby Mathew07ef6e42020-07-20 21:09:23 +0100924 size_t hash_length);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100925
Antonio de Angelis377a1552018-11-22 17:02:40 +0000926/** The type of the state data structure for multipart hash operations.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100927 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100928 * Before calling any function on a hash operation object, the application must
929 * initialize it by any of the following means:
930 * - Set the structure to all-bits-zero, for example:
931 * \code
932 * psa_hash_operation_t operation;
933 * memset(&operation, 0, sizeof(operation));
934 * \endcode
935 * - Initialize the structure to logical zero values, for example:
936 * \code
937 * psa_hash_operation_t operation = {0};
938 * \endcode
939 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
940 * for example:
941 * \code
942 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
943 * \endcode
944 * - Assign the result of the function psa_hash_operation_init()
945 * to the structure, for example:
946 * \code
947 * psa_hash_operation_t operation;
948 * operation = psa_hash_operation_init();
949 * \endcode
950 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000951 * This is an implementation-defined \c struct. Applications should not
952 * make any assumptions about the content of this structure except
953 * as directed by the documentation of a specific implementation. */
954typedef struct psa_hash_operation_s psa_hash_operation_t;
Antonio de Angelis14276e92018-07-10 14:35:43 +0100955
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100956/** \def PSA_HASH_OPERATION_INIT
Antonio de Angelis14276e92018-07-10 14:35:43 +0100957 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100958 * This macro returns a suitable initializer for a hash operation object
959 * of type #psa_hash_operation_t.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100960 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100961#ifdef __DOXYGEN_ONLY__
962/* This is an example definition for documentation purposes.
963 * Implementations should define a suitable value in `crypto_struct.h`.
964 */
965#define PSA_HASH_OPERATION_INIT {0}
966#endif
Antonio de Angelis14276e92018-07-10 14:35:43 +0100967
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100968/** Return an initial value for a hash operation object.
969 */
970static psa_hash_operation_t psa_hash_operation_init(void);
971
972/** Set up a multipart hash operation.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100973 *
974 * The sequence of operations to calculate a hash (message digest)
975 * is as follows:
976 * -# Allocate an operation object which will be passed to all the functions
977 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100978 * -# Initialize the operation object with one of the methods described in the
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100979 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000980 * -# Call psa_hash_setup() to specify the algorithm.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100981 * -# Call psa_hash_update() zero, one or more times, passing a fragment
982 * of the message each time. The hash that is calculated is the hash
983 * of the concatenation of these messages in order.
984 * -# To calculate the hash, call psa_hash_finish().
985 * To compare the hash with an expected value, call psa_hash_verify().
986 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100987 * If an error occurs at any step after a call to psa_hash_setup(), the
988 * operation will need to be reset by a call to psa_hash_abort(). The
989 * application may call psa_hash_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100990 * has been initialized.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100991 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000992 * After a successful call to psa_hash_setup(), the application must
Antonio de Angelis14276e92018-07-10 14:35:43 +0100993 * eventually terminate the operation. The following events terminate an
994 * operation:
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100995 * - A successful call to psa_hash_finish() or psa_hash_verify().
996 * - A call to psa_hash_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +0100997 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100998 * \param[in,out] operation The operation object to set up. It must have
999 * been initialized as per the documentation for
1000 * #psa_hash_operation_t and not yet in use.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001001 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1002 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +01001003 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001004 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001005 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001006 * \retval #PSA_ERROR_NOT_SUPPORTED
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001007 * \p alg is not a supported hash algorithm.
1008 * \retval #PSA_ERROR_INVALID_ARGUMENT
1009 * \p alg is not a hash algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001010 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001011 * The operation state is not valid (it must be inactive).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001012 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1013 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1014 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001015 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1016 * \retval #PSA_ERROR_BAD_STATE
1017 * The library has not been previously initialized by psa_crypto_init().
1018 * It is implementation-dependent whether a failure to initialize
1019 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001020 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001021psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Antonio de Angelis14276e92018-07-10 14:35:43 +01001022 psa_algorithm_t alg);
1023
1024/** Add a message fragment to a multipart hash operation.
1025 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001026 * The application must call psa_hash_setup() before calling this function.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001027 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001028 * If this function returns an error status, the operation enters an error
1029 * state and must be aborted by calling psa_hash_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001030 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001031 * \param[in,out] operation Active hash operation.
1032 * \param[in] input Buffer containing the message fragment to hash.
1033 * \param input_length Size of the \p input buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001034 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001035 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001036 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001037 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001038 * The operation state is not valid (it muct be active).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001039 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1040 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1041 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001042 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1043 * \retval #PSA_ERROR_BAD_STATE
1044 * The library has not been previously initialized by psa_crypto_init().
1045 * It is implementation-dependent whether a failure to initialize
1046 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001047 */
1048psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1049 const uint8_t *input,
1050 size_t input_length);
1051
1052/** Finish the calculation of the hash of a message.
1053 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001054 * The application must call psa_hash_setup() before calling this function.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001055 * This function calculates the hash of the message formed by concatenating
1056 * the inputs passed to preceding calls to psa_hash_update().
1057 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001058 * When this function returns successfuly, the operation becomes inactive.
1059 * If this function returns an error status, the operation enters an error
1060 * state and must be aborted by calling psa_hash_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001061 *
1062 * \warning Applications should not call this function if they expect
1063 * a specific value for the hash. Call psa_hash_verify() instead.
1064 * Beware that comparing integrity or authenticity data such as
1065 * hash values with a function such as \c memcmp is risky
1066 * because the time taken by the comparison may leak information
1067 * about the hashed data which could allow an attacker to guess
1068 * a valid hash and thereby bypass security controls.
1069 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001070 * \param[in,out] operation Active hash operation.
1071 * \param[out] hash Buffer where the hash is to be written.
1072 * \param hash_size Size of the \p hash buffer in bytes.
1073 * \param[out] hash_length On success, the number of bytes
1074 * that make up the hash value. This is always
1075 * #PSA_HASH_SIZE(\c alg) where \c alg is the
1076 * hash algorithm that is calculated.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001077 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001078 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001079 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001080 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001081 * The operation state is not valid (it must be active).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001082 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1083 * The size of the \p hash buffer is too small. You can determine a
1084 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01001085 * where \c alg is the hash algorithm that is calculated.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001086 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1087 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1088 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001089 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1090 * \retval #PSA_ERROR_BAD_STATE
1091 * The library has not been previously initialized by psa_crypto_init().
1092 * It is implementation-dependent whether a failure to initialize
1093 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001094 */
1095psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1096 uint8_t *hash,
1097 size_t hash_size,
1098 size_t *hash_length);
1099
1100/** Finish the calculation of the hash of a message and compare it with
1101 * an expected value.
1102 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001103 * The application must call psa_hash_setup() before calling this function.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001104 * This function calculates the hash of the message formed by concatenating
1105 * the inputs passed to preceding calls to psa_hash_update(). It then
1106 * compares the calculated hash with the expected hash passed as a
1107 * parameter to this function.
1108 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001109 * When this function returns successfuly, the operation becomes inactive.
1110 * If this function returns an error status, the operation enters an error
1111 * state and must be aborted by calling psa_hash_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001112 *
1113 * \note Implementations shall make the best effort to ensure that the
1114 * comparison between the actual hash and the expected hash is performed
1115 * in constant time.
1116 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001117 * \param[in,out] operation Active hash operation.
1118 * \param[in] hash Buffer containing the expected hash value.
1119 * \param hash_length Size of the \p hash buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001120 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001121 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001122 * The expected hash is identical to the actual hash of the message.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001123 * \retval #PSA_ERROR_INVALID_SIGNATURE
Antonio de Angelis14276e92018-07-10 14:35:43 +01001124 * The hash of the message was calculated successfully, but it
1125 * differs from the expected hash.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001126 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001127 * The operation state is not valid (it must be active).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001128 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1129 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1130 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001131 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1132 * \retval #PSA_ERROR_BAD_STATE
1133 * The library has not been previously initialized by psa_crypto_init().
1134 * It is implementation-dependent whether a failure to initialize
1135 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001136 */
1137psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1138 const uint8_t *hash,
1139 size_t hash_length);
1140
1141/** Abort a hash operation.
1142 *
Antonio de Angelis14276e92018-07-10 14:35:43 +01001143 * Aborting an operation frees all associated resources except for the
Antonio de Angelis377a1552018-11-22 17:02:40 +00001144 * \p operation structure itself. Once aborted, the operation object
1145 * can be reused for another operation by calling
1146 * psa_hash_setup() again.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001147 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001148 * You may call this function any time after the operation object has
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001149 * been initialized by one of the methods described in #psa_hash_operation_t.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001150 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001151 * In particular, calling psa_hash_abort() after the operation has been
1152 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1153 * psa_hash_verify() is safe and has no effect.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001154 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001155 * \param[in,out] operation Initialized hash operation.
1156 *
1157 * \retval #PSA_SUCCESS
Antonio de Angelis377a1552018-11-22 17:02:40 +00001158 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1159 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001160 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1161 * \retval #PSA_ERROR_BAD_STATE
1162 * The library has not been previously initialized by psa_crypto_init().
1163 * It is implementation-dependent whether a failure to initialize
1164 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001165 */
1166psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
1167
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001168/** Clone a hash operation.
1169 *
1170 * This function copies the state of an ongoing hash operation to
1171 * a new operation object. In other words, this function is equivalent
1172 * to calling psa_hash_setup() on \p target_operation with the same
1173 * algorithm that \p source_operation was set up for, then
1174 * psa_hash_update() on \p target_operation with the same input that
1175 * that was passed to \p source_operation. After this function returns, the
1176 * two objects are independent, i.e. subsequent calls involving one of
1177 * the objects do not affect the other object.
1178 *
1179 * \param[in] source_operation The active hash operation to clone.
1180 * \param[in,out] target_operation The operation object to set up.
1181 * It must be initialized but not active.
1182 *
1183 * \retval #PSA_SUCCESS
1184 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001185 * The \p source_operation state is not valid (it must be active).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001186 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001187 * The \p target_operation state is not valid (it must be inactive).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001188 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1189 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001190 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1191 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1192 * \retval #PSA_ERROR_BAD_STATE
1193 * The library has not been previously initialized by psa_crypto_init().
1194 * It is implementation-dependent whether a failure to initialize
1195 * results in this error code.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001196 */
1197psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1198 psa_hash_operation_t *target_operation);
1199
Antonio de Angelis14276e92018-07-10 14:35:43 +01001200/**@}*/
1201
1202/** \defgroup MAC Message authentication codes
1203 * @{
1204 */
1205
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001206/** Calculate the MAC (message authentication code) of a message.
1207 *
1208 * \note To verify the MAC of a message against an
1209 * expected value, use psa_mac_verify() instead.
1210 * Beware that comparing integrity or authenticity data such as
1211 * MAC values with a function such as \c memcmp is risky
1212 * because the time taken by the comparison may leak information
1213 * about the MAC value which could allow an attacker to guess
1214 * a valid MAC and thereby bypass security controls.
1215 *
1216 * \param handle Handle to the key to use for the operation.
1217 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1218 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1219 * \param[in] input Buffer containing the input message.
1220 * \param input_length Size of the \p input buffer in bytes.
1221 * \param[out] mac Buffer where the MAC value is to be written.
1222 * \param mac_size Size of the \p mac buffer in bytes.
1223 * \param[out] mac_length On success, the number of bytes
1224 * that make up the MAC value.
1225 *
1226 * \retval #PSA_SUCCESS
1227 * Success.
1228 * \retval #PSA_ERROR_INVALID_HANDLE
1229 * \retval #PSA_ERROR_NOT_PERMITTED
1230 * \retval #PSA_ERROR_INVALID_ARGUMENT
1231 * \p handle is not compatible with \p alg.
1232 * \retval #PSA_ERROR_NOT_SUPPORTED
1233 * \p alg is not supported or is not a MAC algorithm.
1234 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1235 * \p mac_size is too small
1236 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1237 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1238 * \retval #PSA_ERROR_HARDWARE_FAILURE
1239 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1240 * \retval #PSA_ERROR_STORAGE_FAILURE
1241 * The key could not be retrieved from storage.
1242 * \retval #PSA_ERROR_BAD_STATE
1243 * The library has not been previously initialized by psa_crypto_init().
1244 * It is implementation-dependent whether a failure to initialize
1245 * results in this error code.
1246 */
1247psa_status_t psa_mac_compute(psa_key_handle_t handle,
1248 psa_algorithm_t alg,
1249 const uint8_t *input,
1250 size_t input_length,
1251 uint8_t *mac,
1252 size_t mac_size,
1253 size_t *mac_length);
1254
1255/** Calculate the MAC of a message and compare it with a reference value.
1256 *
1257 * \param handle Handle to the key to use for the operation.
1258 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1259 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1260 * \param[in] input Buffer containing the input message.
1261 * \param input_length Size of the \p input buffer in bytes.
1262 * \param[out] mac Buffer containing the expected MAC value.
1263 * \param mac_length Size of the \p mac buffer in bytes.
1264 *
1265 * \retval #PSA_SUCCESS
1266 * The expected MAC is identical to the actual MAC of the input.
1267 * \retval #PSA_ERROR_INVALID_SIGNATURE
1268 * The MAC of the message was calculated successfully, but it
1269 * differs from the expected value.
1270 * \retval #PSA_ERROR_INVALID_HANDLE
1271 * \retval #PSA_ERROR_NOT_PERMITTED
1272 * \retval #PSA_ERROR_INVALID_ARGUMENT
1273 * \p handle is not compatible with \p alg.
1274 * \retval #PSA_ERROR_NOT_SUPPORTED
1275 * \p alg is not supported or is not a MAC algorithm.
1276 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1277 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1278 * \retval #PSA_ERROR_HARDWARE_FAILURE
1279 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1280 * \retval #PSA_ERROR_STORAGE_FAILURE
1281 * The key could not be retrieved from storage.
1282 * \retval #PSA_ERROR_BAD_STATE
1283 * The library has not been previously initialized by psa_crypto_init().
1284 * It is implementation-dependent whether a failure to initialize
1285 * results in this error code.
1286 */
1287psa_status_t psa_mac_verify(psa_key_handle_t handle,
1288 psa_algorithm_t alg,
1289 const uint8_t *input,
1290 size_t input_length,
1291 const uint8_t *mac,
Soby Mathew07ef6e42020-07-20 21:09:23 +01001292 size_t mac_length);
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001293
Antonio de Angelis377a1552018-11-22 17:02:40 +00001294/** The type of the state data structure for multipart MAC operations.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001295 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001296 * Before calling any function on a MAC operation object, the application must
1297 * initialize it by any of the following means:
1298 * - Set the structure to all-bits-zero, for example:
1299 * \code
1300 * psa_mac_operation_t operation;
1301 * memset(&operation, 0, sizeof(operation));
1302 * \endcode
1303 * - Initialize the structure to logical zero values, for example:
1304 * \code
1305 * psa_mac_operation_t operation = {0};
1306 * \endcode
1307 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1308 * for example:
1309 * \code
1310 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1311 * \endcode
1312 * - Assign the result of the function psa_mac_operation_init()
1313 * to the structure, for example:
1314 * \code
1315 * psa_mac_operation_t operation;
1316 * operation = psa_mac_operation_init();
1317 * \endcode
1318 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001319 * This is an implementation-defined \c struct. Applications should not
1320 * make any assumptions about the content of this structure except
1321 * as directed by the documentation of a specific implementation. */
1322typedef struct psa_mac_operation_s psa_mac_operation_t;
Antonio de Angelis14276e92018-07-10 14:35:43 +01001323
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001324/** \def PSA_MAC_OPERATION_INIT
1325 *
1326 * This macro returns a suitable initializer for a MAC operation object of type
1327 * #psa_mac_operation_t.
1328 */
1329#ifdef __DOXYGEN_ONLY__
1330/* This is an example definition for documentation purposes.
1331 * Implementations should define a suitable value in `crypto_struct.h`.
1332 */
1333#define PSA_MAC_OPERATION_INIT {0}
1334#endif
1335
1336/** Return an initial value for a MAC operation object.
1337 */
1338static psa_mac_operation_t psa_mac_operation_init(void);
1339
1340/** Set up a multipart MAC calculation operation.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001341 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001342 * This function sets up the calculation of the MAC
1343 * (message authentication code) of a byte string.
1344 * To verify the MAC of a message against an
1345 * expected value, use psa_mac_verify_setup() instead.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001346 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001347 * The sequence of operations to calculate a MAC is as follows:
Antonio de Angelis14276e92018-07-10 14:35:43 +01001348 * -# Allocate an operation object which will be passed to all the functions
1349 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001350 * -# Initialize the operation object with one of the methods described in the
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001351 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001352 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001353 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1354 * of the message each time. The MAC that is calculated is the MAC
1355 * of the concatenation of these messages in order.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001356 * -# At the end of the message, call psa_mac_sign_finish() to finish
1357 * calculating the MAC value and retrieve it.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001358 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001359 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1360 * operation will need to be reset by a call to psa_mac_abort(). The
1361 * application may call psa_mac_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001362 * has been initialized.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001363 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001364 * After a successful call to psa_mac_sign_setup(), the application must
1365 * eventually terminate the operation through one of the following methods:
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001366 * - A successful call to psa_mac_sign_finish().
1367 * - A call to psa_mac_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001368 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001369 * \param[in,out] operation The operation object to set up. It must have
1370 * been initialized as per the documentation for
1371 * #psa_mac_operation_t and not yet in use.
1372 * \param handle Handle to the key to use for the operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001373 * It must remain valid until the operation
1374 * terminates.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001375 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001376 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +01001377 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001378 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001379 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001380 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00001381 * \retval #PSA_ERROR_NOT_PERMITTED
1382 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001383 * \p handle is not compatible with \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001384 * \retval #PSA_ERROR_NOT_SUPPORTED
1385 * \p alg is not supported or is not a MAC algorithm.
1386 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1387 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1388 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001389 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1390 * \retval #PSA_ERROR_STORAGE_FAILURE
1391 * The key could not be retrieved from storage.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001392 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001393 * The operation state is not valid (it must be inactive).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001394 * \retval #PSA_ERROR_BAD_STATE
1395 * The library has not been previously initialized by psa_crypto_init().
1396 * It is implementation-dependent whether a failure to initialize
1397 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001398 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001399psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001400 psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001401 psa_algorithm_t alg);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001402
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001403/** Set up a multipart MAC verification operation.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001404 *
1405 * This function sets up the verification of the MAC
1406 * (message authentication code) of a byte string against an expected value.
1407 *
1408 * The sequence of operations to verify a MAC is as follows:
1409 * -# Allocate an operation object which will be passed to all the functions
1410 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001411 * -# Initialize the operation object with one of the methods described in the
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001412 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001413 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001414 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1415 * of the message each time. The MAC that is calculated is the MAC
1416 * of the concatenation of these messages in order.
1417 * -# At the end of the message, call psa_mac_verify_finish() to finish
1418 * calculating the actual MAC of the message and verify it against
1419 * the expected value.
1420 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001421 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1422 * operation will need to be reset by a call to psa_mac_abort(). The
1423 * application may call psa_mac_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001424 * has been initialized.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001425 *
1426 * After a successful call to psa_mac_verify_setup(), the application must
1427 * eventually terminate the operation through one of the following methods:
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001428 * - A successful call to psa_mac_verify_finish().
1429 * - A call to psa_mac_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001430 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001431 * \param[in,out] operation The operation object to set up. It must have
1432 * been initialized as per the documentation for
1433 * #psa_mac_operation_t and not yet in use.
1434 * \param handle Handle to the key to use for the operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001435 * It must remain valid until the operation
1436 * terminates.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001437 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1438 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1439 *
1440 * \retval #PSA_SUCCESS
1441 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001442 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00001443 * \retval #PSA_ERROR_NOT_PERMITTED
1444 * \retval #PSA_ERROR_INVALID_ARGUMENT
1445 * \c key is not compatible with \c alg.
1446 * \retval #PSA_ERROR_NOT_SUPPORTED
1447 * \c alg is not supported or is not a MAC algorithm.
1448 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1449 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1450 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001451 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1452 * \retval #PSA_ERROR_STORAGE_FAILURE
1453 * The key could not be retrieved from storage
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001454 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001455 * The operation state is not valid (it must be inactive).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001456 * \retval #PSA_ERROR_BAD_STATE
1457 * The library has not been previously initialized by psa_crypto_init().
1458 * It is implementation-dependent whether a failure to initialize
1459 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001460 */
1461psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001462 psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001463 psa_algorithm_t alg);
1464
1465/** Add a message fragment to a multipart MAC operation.
1466 *
1467 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1468 * before calling this function.
1469 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001470 * If this function returns an error status, the operation enters an error
1471 * state and must be aborted by calling psa_mac_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001472 *
1473 * \param[in,out] operation Active MAC operation.
1474 * \param[in] input Buffer containing the message fragment to add to
1475 * the MAC calculation.
1476 * \param input_length Size of the \p input buffer in bytes.
1477 *
1478 * \retval #PSA_SUCCESS
1479 * Success.
1480 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001481 * The operation state is not valid (it must be active).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001482 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1483 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1484 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001485 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1486 * \retval #PSA_ERROR_STORAGE_FAILURE
1487 * \retval #PSA_ERROR_BAD_STATE
1488 * The library has not been previously initialized by psa_crypto_init().
1489 * It is implementation-dependent whether a failure to initialize
1490 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001491 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001492psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1493 const uint8_t *input,
1494 size_t input_length);
1495
Antonio de Angelis377a1552018-11-22 17:02:40 +00001496/** Finish the calculation of the MAC of a message.
1497 *
1498 * The application must call psa_mac_sign_setup() before calling this function.
1499 * This function calculates the MAC of the message formed by concatenating
1500 * the inputs passed to preceding calls to psa_mac_update().
1501 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001502 * When this function returns successfuly, the operation becomes inactive.
1503 * If this function returns an error status, the operation enters an error
1504 * state and must be aborted by calling psa_mac_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001505 *
1506 * \warning Applications should not call this function if they expect
1507 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1508 * Beware that comparing integrity or authenticity data such as
1509 * MAC values with a function such as \c memcmp is risky
1510 * because the time taken by the comparison may leak information
1511 * about the MAC value which could allow an attacker to guess
1512 * a valid MAC and thereby bypass security controls.
1513 *
1514 * \param[in,out] operation Active MAC operation.
1515 * \param[out] mac Buffer where the MAC value is to be written.
1516 * \param mac_size Size of the \p mac buffer in bytes.
1517 * \param[out] mac_length On success, the number of bytes
1518 * that make up the MAC value. This is always
1519 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
1520 * where \c key_type and \c key_bits are the type and
1521 * bit-size respectively of the key and \c alg is the
1522 * MAC algorithm that is calculated.
1523 *
1524 * \retval #PSA_SUCCESS
1525 * Success.
1526 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001527 * The operation state is not valid (it must be an active mac sign
1528 * operation).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001529 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1530 * The size of the \p mac buffer is too small. You can determine a
1531 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1532 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1533 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1534 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001535 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1536 * \retval #PSA_ERROR_STORAGE_FAILURE
1537 * \retval #PSA_ERROR_BAD_STATE
1538 * The library has not been previously initialized by psa_crypto_init().
1539 * It is implementation-dependent whether a failure to initialize
1540 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001541 */
1542psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1543 uint8_t *mac,
1544 size_t mac_size,
1545 size_t *mac_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001546
Antonio de Angelis377a1552018-11-22 17:02:40 +00001547/** Finish the calculation of the MAC of a message and compare it with
1548 * an expected value.
1549 *
1550 * The application must call psa_mac_verify_setup() before calling this function.
1551 * This function calculates the MAC of the message formed by concatenating
1552 * the inputs passed to preceding calls to psa_mac_update(). It then
1553 * compares the calculated MAC with the expected MAC passed as a
1554 * parameter to this function.
1555 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001556 * When this function returns successfuly, the operation becomes inactive.
1557 * If this function returns an error status, the operation enters an error
1558 * state and must be aborted by calling psa_mac_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001559 *
1560 * \note Implementations shall make the best effort to ensure that the
1561 * comparison between the actual MAC and the expected MAC is performed
1562 * in constant time.
1563 *
1564 * \param[in,out] operation Active MAC operation.
1565 * \param[in] mac Buffer containing the expected MAC value.
1566 * \param mac_length Size of the \p mac buffer in bytes.
1567 *
1568 * \retval #PSA_SUCCESS
1569 * The expected MAC is identical to the actual MAC of the message.
1570 * \retval #PSA_ERROR_INVALID_SIGNATURE
1571 * The MAC of the message was calculated successfully, but it
1572 * differs from the expected MAC.
1573 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001574 * The operation state is not valid (it must be an active mac verify
1575 * operation).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001576 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1577 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1578 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001579 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1580 * \retval #PSA_ERROR_STORAGE_FAILURE
1581 * \retval #PSA_ERROR_BAD_STATE
1582 * The library has not been previously initialized by psa_crypto_init().
1583 * It is implementation-dependent whether a failure to initialize
1584 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001585 */
1586psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1587 const uint8_t *mac,
1588 size_t mac_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001589
Antonio de Angelis377a1552018-11-22 17:02:40 +00001590/** Abort a MAC operation.
1591 *
1592 * Aborting an operation frees all associated resources except for the
1593 * \p operation structure itself. Once aborted, the operation object
1594 * can be reused for another operation by calling
1595 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
1596 *
1597 * You may call this function any time after the operation object has
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001598 * been initialized by one of the methods described in #psa_mac_operation_t.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001599 *
1600 * In particular, calling psa_mac_abort() after the operation has been
1601 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1602 * psa_mac_verify_finish() is safe and has no effect.
1603 *
1604 * \param[in,out] operation Initialized MAC operation.
1605 *
1606 * \retval #PSA_SUCCESS
Antonio de Angelis377a1552018-11-22 17:02:40 +00001607 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1608 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001609 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1610 * \retval #PSA_ERROR_BAD_STATE
1611 * The library has not been previously initialized by psa_crypto_init().
1612 * It is implementation-dependent whether a failure to initialize
1613 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001614 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001615psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1616
1617/**@}*/
1618
1619/** \defgroup cipher Symmetric ciphers
1620 * @{
1621 */
1622
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001623/** Encrypt a message using a symmetric cipher.
1624 *
1625 * This function encrypts a message with a random IV (initialization
1626 * vector). Use the multipart operation interface with a
1627 * #psa_cipher_operation_t object to provide other forms of IV.
1628 *
1629 * \param handle Handle to the key to use for the operation.
1630 * It must remain valid until the operation
1631 * terminates.
1632 * \param alg The cipher algorithm to compute
1633 * (\c PSA_ALG_XXX value such that
1634 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1635 * \param[in] input Buffer containing the message to encrypt.
1636 * \param input_length Size of the \p input buffer in bytes.
1637 * \param[out] output Buffer where the output is to be written.
1638 * The output contains the IV followed by
1639 * the ciphertext proper.
1640 * \param output_size Size of the \p output buffer in bytes.
1641 * \param[out] output_length On success, the number of bytes
1642 * that make up the output.
1643 *
1644 * \retval #PSA_SUCCESS
1645 * Success.
1646 * \retval #PSA_ERROR_INVALID_HANDLE
1647 * \retval #PSA_ERROR_NOT_PERMITTED
1648 * \retval #PSA_ERROR_INVALID_ARGUMENT
1649 * \p handle is not compatible with \p alg.
1650 * \retval #PSA_ERROR_NOT_SUPPORTED
1651 * \p alg is not supported or is not a cipher algorithm.
1652 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1653 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1654 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1655 * \retval #PSA_ERROR_HARDWARE_FAILURE
1656 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1657 * \retval #PSA_ERROR_STORAGE_FAILURE
1658 * \retval #PSA_ERROR_BAD_STATE
1659 * The library has not been previously initialized by psa_crypto_init().
1660 * It is implementation-dependent whether a failure to initialize
1661 * results in this error code.
1662 */
1663psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1664 psa_algorithm_t alg,
1665 const uint8_t *input,
1666 size_t input_length,
1667 uint8_t *output,
1668 size_t output_size,
1669 size_t *output_length);
1670
1671/** Decrypt a message using a symmetric cipher.
1672 *
1673 * This function decrypts a message encrypted with a symmetric cipher.
1674 *
1675 * \param handle Handle to the key to use for the operation.
1676 * It must remain valid until the operation
1677 * terminates.
1678 * \param alg The cipher algorithm to compute
1679 * (\c PSA_ALG_XXX value such that
1680 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1681 * \param[in] input Buffer containing the message to decrypt.
1682 * This consists of the IV followed by the
1683 * ciphertext proper.
1684 * \param input_length Size of the \p input buffer in bytes.
1685 * \param[out] output Buffer where the plaintext is to be written.
1686 * \param output_size Size of the \p output buffer in bytes.
1687 * \param[out] output_length On success, the number of bytes
1688 * that make up the output.
1689 *
1690 * \retval #PSA_SUCCESS
1691 * Success.
1692 * \retval #PSA_ERROR_INVALID_HANDLE
1693 * \retval #PSA_ERROR_NOT_PERMITTED
1694 * \retval #PSA_ERROR_INVALID_ARGUMENT
1695 * \p handle is not compatible with \p alg.
1696 * \retval #PSA_ERROR_NOT_SUPPORTED
1697 * \p alg is not supported or is not a cipher algorithm.
1698 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1699 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1700 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1701 * \retval #PSA_ERROR_HARDWARE_FAILURE
1702 * \retval #PSA_ERROR_STORAGE_FAILURE
1703 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1704 * \retval #PSA_ERROR_BAD_STATE
1705 * The library has not been previously initialized by psa_crypto_init().
1706 * It is implementation-dependent whether a failure to initialize
1707 * results in this error code.
1708 */
1709psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1710 psa_algorithm_t alg,
1711 const uint8_t *input,
1712 size_t input_length,
1713 uint8_t *output,
1714 size_t output_size,
1715 size_t *output_length);
1716
Antonio de Angelis377a1552018-11-22 17:02:40 +00001717/** The type of the state data structure for multipart cipher operations.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001718 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001719 * Before calling any function on a cipher operation object, the application
1720 * must initialize it by any of the following means:
1721 * - Set the structure to all-bits-zero, for example:
1722 * \code
1723 * psa_cipher_operation_t operation;
1724 * memset(&operation, 0, sizeof(operation));
1725 * \endcode
1726 * - Initialize the structure to logical zero values, for example:
1727 * \code
1728 * psa_cipher_operation_t operation = {0};
1729 * \endcode
1730 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1731 * for example:
1732 * \code
1733 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1734 * \endcode
1735 * - Assign the result of the function psa_cipher_operation_init()
1736 * to the structure, for example:
1737 * \code
1738 * psa_cipher_operation_t operation;
1739 * operation = psa_cipher_operation_init();
1740 * \endcode
1741 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001742 * This is an implementation-defined \c struct. Applications should not
1743 * make any assumptions about the content of this structure except
1744 * as directed by the documentation of a specific implementation. */
1745typedef struct psa_cipher_operation_s psa_cipher_operation_t;
Antonio de Angelis14276e92018-07-10 14:35:43 +01001746
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001747/** \def PSA_CIPHER_OPERATION_INIT
1748 *
1749 * This macro returns a suitable initializer for a cipher operation object of
1750 * type #psa_cipher_operation_t.
1751 */
1752#ifdef __DOXYGEN_ONLY__
1753/* This is an example definition for documentation purposes.
1754 * Implementations should define a suitable value in `crypto_struct.h`.
1755 */
1756#define PSA_CIPHER_OPERATION_INIT {0}
1757#endif
1758
1759/** Return an initial value for a cipher operation object.
1760 */
1761static psa_cipher_operation_t psa_cipher_operation_init(void);
1762
Antonio de Angelis14276e92018-07-10 14:35:43 +01001763/** Set the key for a multipart symmetric encryption operation.
1764 *
1765 * The sequence of operations to encrypt a message with a symmetric cipher
1766 * is as follows:
1767 * -# Allocate an operation object which will be passed to all the functions
1768 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001769 * -# Initialize the operation object with one of the methods described in the
1770 * documentation for #psa_cipher_operation_t, e.g.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001771 * #PSA_CIPHER_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001772 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001773 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Antonio de Angelis14276e92018-07-10 14:35:43 +01001774 * generate or set the IV (initialization vector). You should use
Antonio de Angelis377a1552018-11-22 17:02:40 +00001775 * psa_cipher_generate_iv() unless the protocol you are implementing
Antonio de Angelis14276e92018-07-10 14:35:43 +01001776 * requires a specific IV value.
1777 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1778 * of the message each time.
1779 * -# Call psa_cipher_finish().
1780 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001781 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1782 * the operation will need to be reset by a call to psa_cipher_abort(). The
1783 * application may call psa_cipher_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001784 * has been initialized.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001785 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001786 * After a successful call to psa_cipher_encrypt_setup(), the application must
Antonio de Angelis14276e92018-07-10 14:35:43 +01001787 * eventually terminate the operation. The following events terminate an
1788 * operation:
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001789 * - A successful call to psa_cipher_finish().
1790 * - A call to psa_cipher_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001791 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001792 * \param[in,out] operation The operation object to set up. It must have
1793 * been initialized as per the documentation for
1794 * #psa_cipher_operation_t and not yet in use.
1795 * \param handle Handle to the key to use for the operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001796 * It must remain valid until the operation
1797 * terminates.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001798 * \param alg The cipher algorithm to compute
1799 * (\c PSA_ALG_XXX value such that
1800 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +01001801 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001802 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001803 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001804 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00001805 * \retval #PSA_ERROR_NOT_PERMITTED
1806 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001807 * \p handle is not compatible with \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001808 * \retval #PSA_ERROR_NOT_SUPPORTED
1809 * \p alg is not supported or is not a cipher algorithm.
1810 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1811 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1812 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001813 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1814 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001815 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001816 * The operation state is not valid (it must be inactive).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001817 * \retval #PSA_ERROR_BAD_STATE
1818 * The library has not been previously initialized by psa_crypto_init().
1819 * It is implementation-dependent whether a failure to initialize
1820 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001821 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001822psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001823 psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001824 psa_algorithm_t alg);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001825
1826/** Set the key for a multipart symmetric decryption operation.
1827 *
1828 * The sequence of operations to decrypt a message with a symmetric cipher
1829 * is as follows:
1830 * -# Allocate an operation object which will be passed to all the functions
1831 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001832 * -# Initialize the operation object with one of the methods described in the
1833 * documentation for #psa_cipher_operation_t, e.g.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001834 * #PSA_CIPHER_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001835 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001836 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Antonio de Angelis14276e92018-07-10 14:35:43 +01001837 * decryption. If the IV is prepended to the ciphertext, you can call
1838 * psa_cipher_update() on a buffer containing the IV followed by the
1839 * beginning of the message.
1840 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1841 * of the message each time.
1842 * -# Call psa_cipher_finish().
1843 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001844 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1845 * the operation will need to be reset by a call to psa_cipher_abort(). The
1846 * application may call psa_cipher_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001847 * has been initialized.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001848 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001849 * After a successful call to psa_cipher_decrypt_setup(), the application must
Antonio de Angelis14276e92018-07-10 14:35:43 +01001850 * eventually terminate the operation. The following events terminate an
1851 * operation:
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001852 * - A successful call to psa_cipher_finish().
1853 * - A call to psa_cipher_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001854 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001855 * \param[in,out] operation The operation object to set up. It must have
1856 * been initialized as per the documentation for
1857 * #psa_cipher_operation_t and not yet in use.
1858 * \param handle Handle to the key to use for the operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001859 * It must remain valid until the operation
1860 * terminates.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001861 * \param alg The cipher algorithm to compute
1862 * (\c PSA_ALG_XXX value such that
1863 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +01001864 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001865 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001866 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001867 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00001868 * \retval #PSA_ERROR_NOT_PERMITTED
1869 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001870 * \p handle is not compatible with \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001871 * \retval #PSA_ERROR_NOT_SUPPORTED
1872 * \p alg is not supported or is not a cipher algorithm.
1873 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1874 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1875 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001876 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1877 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001878 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001879 * The operation state is not valid (it must be inactive).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001880 * \retval #PSA_ERROR_BAD_STATE
1881 * The library has not been previously initialized by psa_crypto_init().
1882 * It is implementation-dependent whether a failure to initialize
1883 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001884 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001885psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001886 psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001887 psa_algorithm_t alg);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001888
Antonio de Angelis377a1552018-11-22 17:02:40 +00001889/** Generate an IV for a symmetric encryption operation.
1890 *
1891 * This function generates a random IV (initialization vector), nonce
1892 * or initial counter value for the encryption operation as appropriate
1893 * for the chosen algorithm, key type and key size.
1894 *
1895 * The application must call psa_cipher_encrypt_setup() before
1896 * calling this function.
1897 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001898 * If this function returns an error status, the operation enters an error
1899 * state and must be aborted by calling psa_cipher_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001900 *
1901 * \param[in,out] operation Active cipher operation.
1902 * \param[out] iv Buffer where the generated IV is to be written.
1903 * \param iv_size Size of the \p iv buffer in bytes.
1904 * \param[out] iv_length On success, the number of bytes of the
1905 * generated IV.
1906 *
1907 * \retval #PSA_SUCCESS
1908 * Success.
1909 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001910 * The operation state is not valid (it must be active, with no IV set).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001911 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1912 * The size of the \p iv buffer is too small.
1913 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1914 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1915 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001916 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1917 * \retval #PSA_ERROR_STORAGE_FAILURE
1918 * \retval #PSA_ERROR_BAD_STATE
1919 * The library has not been previously initialized by psa_crypto_init().
1920 * It is implementation-dependent whether a failure to initialize
1921 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001922 */
1923psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001924 uint8_t *iv,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001925 size_t iv_size,
1926 size_t *iv_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001927
Antonio de Angelis377a1552018-11-22 17:02:40 +00001928/** Set the IV for a symmetric encryption or decryption operation.
1929 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001930 * This function sets the IV (initialization vector), nonce
Antonio de Angelis377a1552018-11-22 17:02:40 +00001931 * or initial counter value for the encryption or decryption operation.
1932 *
1933 * The application must call psa_cipher_encrypt_setup() before
1934 * calling this function.
1935 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001936 * If this function returns an error status, the operation enters an error
1937 * state and must be aborted by calling psa_cipher_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001938 *
1939 * \note When encrypting, applications should use psa_cipher_generate_iv()
1940 * instead of this function, unless implementing a protocol that requires
1941 * a non-random IV.
1942 *
1943 * \param[in,out] operation Active cipher operation.
1944 * \param[in] iv Buffer containing the IV to use.
1945 * \param iv_length Size of the IV in bytes.
1946 *
1947 * \retval #PSA_SUCCESS
1948 * Success.
1949 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001950 * The operation state is not valid (it must be an active cipher
1951 * encrypt operation, with no IV set).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001952 * \retval #PSA_ERROR_INVALID_ARGUMENT
1953 * The size of \p iv is not acceptable for the chosen algorithm,
1954 * or the chosen algorithm does not use an IV.
1955 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1956 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1957 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001958 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1959 * \retval #PSA_ERROR_STORAGE_FAILURE
1960 * \retval #PSA_ERROR_BAD_STATE
1961 * The library has not been previously initialized by psa_crypto_init().
1962 * It is implementation-dependent whether a failure to initialize
1963 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001964 */
1965psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001966 const uint8_t *iv,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001967 size_t iv_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001968
Antonio de Angelis377a1552018-11-22 17:02:40 +00001969/** Encrypt or decrypt a message fragment in an active cipher operation.
1970 *
1971 * Before calling this function, you must:
1972 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1973 * The choice of setup function determines whether this function
1974 * encrypts or decrypts its input.
1975 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1976 * (recommended when encrypting) or psa_cipher_set_iv().
1977 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001978 * If this function returns an error status, the operation enters an error
1979 * state and must be aborted by calling psa_cipher_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001980 *
1981 * \param[in,out] operation Active cipher operation.
1982 * \param[in] input Buffer containing the message fragment to
1983 * encrypt or decrypt.
1984 * \param input_length Size of the \p input buffer in bytes.
1985 * \param[out] output Buffer where the output is to be written.
1986 * \param output_size Size of the \p output buffer in bytes.
1987 * \param[out] output_length On success, the number of bytes
1988 * that make up the returned output.
1989 *
1990 * \retval #PSA_SUCCESS
1991 * Success.
1992 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001993 * The operation state is not valid (it must be active, with an IV set
1994 * if required for the algorithm).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001995 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1996 * The size of the \p output buffer is too small.
1997 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1998 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1999 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002000 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2001 * \retval #PSA_ERROR_STORAGE_FAILURE
2002 * \retval #PSA_ERROR_BAD_STATE
2003 * The library has not been previously initialized by psa_crypto_init().
2004 * It is implementation-dependent whether a failure to initialize
2005 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002006 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01002007psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2008 const uint8_t *input,
2009 size_t input_length,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002010 uint8_t *output,
Antonio de Angelis14276e92018-07-10 14:35:43 +01002011 size_t output_size,
2012 size_t *output_length);
2013
Antonio de Angelis377a1552018-11-22 17:02:40 +00002014/** Finish encrypting or decrypting a message in a cipher operation.
2015 *
2016 * The application must call psa_cipher_encrypt_setup() or
2017 * psa_cipher_decrypt_setup() before calling this function. The choice
2018 * of setup function determines whether this function encrypts or
2019 * decrypts its input.
2020 *
2021 * This function finishes the encryption or decryption of the message
2022 * formed by concatenating the inputs passed to preceding calls to
2023 * psa_cipher_update().
2024 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002025 * When this function returns successfuly, the operation becomes inactive.
2026 * If this function returns an error status, the operation enters an error
2027 * state and must be aborted by calling psa_cipher_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00002028 *
2029 * \param[in,out] operation Active cipher operation.
2030 * \param[out] output Buffer where the output is to be written.
2031 * \param output_size Size of the \p output buffer in bytes.
2032 * \param[out] output_length On success, the number of bytes
2033 * that make up the returned output.
2034 *
2035 * \retval #PSA_SUCCESS
2036 * Success.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002037 * \retval #PSA_ERROR_INVALID_ARGUMENT
2038 * The total input size passed to this operation is not valid for
2039 * this particular algorithm. For example, the algorithm is a based
2040 * on block cipher and requires a whole number of blocks, but the
2041 * total input size is not a multiple of the block size.
2042 * \retval #PSA_ERROR_INVALID_PADDING
2043 * This is a decryption operation for an algorithm that includes
2044 * padding, and the ciphertext does not contain valid padding.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002045 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002046 * The operation state is not valid (it must be active, with an IV set
2047 * if required for the algorithm).
Antonio de Angelis377a1552018-11-22 17:02:40 +00002048 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2049 * The size of the \p output buffer is too small.
2050 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2051 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2052 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002053 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2054 * \retval #PSA_ERROR_STORAGE_FAILURE
2055 * \retval #PSA_ERROR_BAD_STATE
2056 * The library has not been previously initialized by psa_crypto_init().
2057 * It is implementation-dependent whether a failure to initialize
2058 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002059 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01002060psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
2061 uint8_t *output,
2062 size_t output_size,
2063 size_t *output_length);
2064
Antonio de Angelis377a1552018-11-22 17:02:40 +00002065/** Abort a cipher operation.
2066 *
2067 * Aborting an operation frees all associated resources except for the
2068 * \p operation structure itself. Once aborted, the operation object
2069 * can be reused for another operation by calling
2070 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
2071 *
2072 * You may call this function any time after the operation object has
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002073 * been initialized as described in #psa_cipher_operation_t.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002074 *
2075 * In particular, calling psa_cipher_abort() after the operation has been
2076 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2077 * is safe and has no effect.
2078 *
2079 * \param[in,out] operation Initialized cipher operation.
2080 *
2081 * \retval #PSA_SUCCESS
Antonio de Angelis377a1552018-11-22 17:02:40 +00002082 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2083 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002084 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2085 * \retval #PSA_ERROR_BAD_STATE
2086 * The library has not been previously initialized by psa_crypto_init().
2087 * It is implementation-dependent whether a failure to initialize
2088 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002089 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01002090psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2091
2092/**@}*/
2093
2094/** \defgroup aead Authenticated encryption with associated data (AEAD)
2095 * @{
2096 */
2097
Antonio de Angelis14276e92018-07-10 14:35:43 +01002098/** Process an authenticated encryption operation.
2099 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002100 * \param handle Handle to the key to use for the operation.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002101 * \param alg The AEAD algorithm to compute
2102 * (\c PSA_ALG_XXX value such that
Antonio de Angelis377a1552018-11-22 17:02:40 +00002103 * #PSA_ALG_IS_AEAD(\p alg) is true).
2104 * \param[in] nonce Nonce or IV to use.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002105 * \param nonce_length Size of the \p nonce buffer in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002106 * \param[in] additional_data Additional data that will be authenticated
Antonio de Angelis14276e92018-07-10 14:35:43 +01002107 * but not encrypted.
2108 * \param additional_data_length Size of \p additional_data in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002109 * \param[in] plaintext Data that will be authenticated and
Antonio de Angelis14276e92018-07-10 14:35:43 +01002110 * encrypted.
2111 * \param plaintext_length Size of \p plaintext in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002112 * \param[out] ciphertext Output buffer for the authenticated and
Antonio de Angelis14276e92018-07-10 14:35:43 +01002113 * encrypted data. The additional data is not
2114 * part of this output. For algorithms where the
2115 * encrypted data and the authentication tag
2116 * are defined as separate outputs, the
2117 * authentication tag is appended to the
2118 * encrypted data.
2119 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2120 * This must be at least
2121 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2122 * \p plaintext_length).
Antonio de Angelis377a1552018-11-22 17:02:40 +00002123 * \param[out] ciphertext_length On success, the size of the output
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002124 * in the \p ciphertext buffer.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002125 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002126 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01002127 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002128 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00002129 * \retval #PSA_ERROR_NOT_PERMITTED
2130 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002131 * \p handle is not compatible with \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002132 * \retval #PSA_ERROR_NOT_SUPPORTED
2133 * \p alg is not supported or is not an AEAD algorithm.
2134 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002135 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2136 * \p ciphertext_size is too small
Antonio de Angelis377a1552018-11-22 17:02:40 +00002137 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2138 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002139 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2140 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002141 * \retval #PSA_ERROR_BAD_STATE
2142 * The library has not been previously initialized by psa_crypto_init().
2143 * It is implementation-dependent whether a failure to initialize
2144 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002145 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002146psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +00002147 psa_algorithm_t alg,
2148 const uint8_t *nonce,
2149 size_t nonce_length,
2150 const uint8_t *additional_data,
2151 size_t additional_data_length,
2152 const uint8_t *plaintext,
2153 size_t plaintext_length,
2154 uint8_t *ciphertext,
2155 size_t ciphertext_size,
2156 size_t *ciphertext_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01002157
2158/** Process an authenticated decryption operation.
2159 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002160 * \param handle Handle to the key to use for the operation.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002161 * \param alg The AEAD algorithm to compute
2162 * (\c PSA_ALG_XXX value such that
Antonio de Angelis377a1552018-11-22 17:02:40 +00002163 * #PSA_ALG_IS_AEAD(\p alg) is true).
2164 * \param[in] nonce Nonce or IV to use.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002165 * \param nonce_length Size of the \p nonce buffer in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002166 * \param[in] additional_data Additional data that has been authenticated
Antonio de Angelis14276e92018-07-10 14:35:43 +01002167 * but not encrypted.
2168 * \param additional_data_length Size of \p additional_data in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002169 * \param[in] ciphertext Data that has been authenticated and
Antonio de Angelis14276e92018-07-10 14:35:43 +01002170 * encrypted. For algorithms where the
2171 * encrypted data and the authentication tag
2172 * are defined as separate inputs, the buffer
2173 * must contain the encrypted data followed
2174 * by the authentication tag.
2175 * \param ciphertext_length Size of \p ciphertext in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002176 * \param[out] plaintext Output buffer for the decrypted data.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002177 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2178 * This must be at least
2179 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2180 * \p ciphertext_length).
Antonio de Angelis377a1552018-11-22 17:02:40 +00002181 * \param[out] plaintext_length On success, the size of the output
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002182 * in the \p plaintext buffer.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002183 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002184 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01002185 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002186 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00002187 * \retval #PSA_ERROR_INVALID_SIGNATURE
Antonio de Angelis14276e92018-07-10 14:35:43 +01002188 * The ciphertext is not authentic.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002189 * \retval #PSA_ERROR_NOT_PERMITTED
2190 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002191 * \p handle is not compatible with \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002192 * \retval #PSA_ERROR_NOT_SUPPORTED
2193 * \p alg is not supported or is not an AEAD algorithm.
2194 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002195 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2196 * \p plaintext_size or \p nonce_length is too small
Antonio de Angelis377a1552018-11-22 17:02:40 +00002197 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2198 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002199 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2200 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002201 * \retval #PSA_ERROR_BAD_STATE
2202 * The library has not been previously initialized by psa_crypto_init().
2203 * It is implementation-dependent whether a failure to initialize
2204 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002205 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002206psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Antonio de Angelis377a1552018-11-22 17:02:40 +00002207 psa_algorithm_t alg,
2208 const uint8_t *nonce,
2209 size_t nonce_length,
2210 const uint8_t *additional_data,
2211 size_t additional_data_length,
2212 const uint8_t *ciphertext,
2213 size_t ciphertext_length,
2214 uint8_t *plaintext,
2215 size_t plaintext_size,
2216 size_t *plaintext_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01002217
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002218/** The type of the state data structure for multipart AEAD operations.
2219 *
2220 * Before calling any function on an AEAD operation object, the application
2221 * must initialize it by any of the following means:
2222 * - Set the structure to all-bits-zero, for example:
2223 * \code
2224 * psa_aead_operation_t operation;
2225 * memset(&operation, 0, sizeof(operation));
2226 * \endcode
2227 * - Initialize the structure to logical zero values, for example:
2228 * \code
2229 * psa_aead_operation_t operation = {0};
2230 * \endcode
2231 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2232 * for example:
2233 * \code
2234 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2235 * \endcode
2236 * - Assign the result of the function psa_aead_operation_init()
2237 * to the structure, for example:
2238 * \code
2239 * psa_aead_operation_t operation;
2240 * operation = psa_aead_operation_init();
2241 * \endcode
2242 *
2243 * This is an implementation-defined \c struct. Applications should not
2244 * make any assumptions about the content of this structure except
2245 * as directed by the documentation of a specific implementation. */
2246typedef struct psa_aead_operation_s psa_aead_operation_t;
2247
2248/** \def PSA_AEAD_OPERATION_INIT
2249 *
2250 * This macro returns a suitable initializer for an AEAD operation object of
2251 * type #psa_aead_operation_t.
2252 */
2253#ifdef __DOXYGEN_ONLY__
2254/* This is an example definition for documentation purposes.
2255 * Implementations should define a suitable value in `crypto_struct.h`.
2256 */
2257#define PSA_AEAD_OPERATION_INIT {0}
2258#endif
2259
2260/** Return an initial value for an AEAD operation object.
2261 */
2262static psa_aead_operation_t psa_aead_operation_init(void);
2263
2264/** Set the key for a multipart authenticated encryption operation.
2265 *
2266 * The sequence of operations to encrypt a message with authentication
2267 * is as follows:
2268 * -# Allocate an operation object which will be passed to all the functions
2269 * listed here.
2270 * -# Initialize the operation object with one of the methods described in the
2271 * documentation for #psa_aead_operation_t, e.g.
2272 * #PSA_AEAD_OPERATION_INIT.
2273 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
2274 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2275 * inputs to the subsequent calls to psa_aead_update_ad() and
2276 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2277 * for details.
2278 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2279 * generate or set the nonce. You should use
2280 * psa_aead_generate_nonce() unless the protocol you are implementing
2281 * requires a specific nonce value.
2282 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2283 * of the non-encrypted additional authenticated data each time.
2284 * -# Call psa_aead_update() zero, one or more times, passing a fragment
2285 * of the message to encrypt each time.
2286 * -# Call psa_aead_finish().
2287 *
2288 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2289 * the operation will need to be reset by a call to psa_aead_abort(). The
2290 * application may call psa_aead_abort() at any time after the operation
2291 * has been initialized.
2292 *
2293 * After a successful call to psa_aead_encrypt_setup(), the application must
2294 * eventually terminate the operation. The following events terminate an
2295 * operation:
2296 * - A successful call to psa_aead_finish().
2297 * - A call to psa_aead_abort().
2298 *
2299 * \param[in,out] operation The operation object to set up. It must have
2300 * been initialized as per the documentation for
2301 * #psa_aead_operation_t and not yet in use.
2302 * \param handle Handle to the key to use for the operation.
2303 * It must remain valid until the operation
2304 * terminates.
2305 * \param alg The AEAD algorithm to compute
2306 * (\c PSA_ALG_XXX value such that
2307 * #PSA_ALG_IS_AEAD(\p alg) is true).
2308 *
2309 * \retval #PSA_SUCCESS
2310 * Success.
2311 * \retval #PSA_ERROR_BAD_STATE
2312 * The operation state is not valid (it must be inactive).
2313 * \retval #PSA_ERROR_INVALID_HANDLE
2314 * \retval #PSA_ERROR_NOT_PERMITTED
2315 * \retval #PSA_ERROR_INVALID_ARGUMENT
2316 * \p handle is not compatible with \p alg.
2317 * \retval #PSA_ERROR_NOT_SUPPORTED
2318 * \p alg is not supported or is not an AEAD algorithm.
2319 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2320 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2321 * \retval #PSA_ERROR_HARDWARE_FAILURE
2322 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2323 * \retval #PSA_ERROR_STORAGE_FAILURE
2324 * \retval #PSA_ERROR_BAD_STATE
2325 * The library has not been previously initialized by psa_crypto_init().
2326 * It is implementation-dependent whether a failure to initialize
2327 * results in this error code.
2328 */
2329psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2330 psa_key_handle_t handle,
2331 psa_algorithm_t alg);
2332
2333/** Set the key for a multipart authenticated decryption operation.
2334 *
2335 * The sequence of operations to decrypt a message with authentication
2336 * is as follows:
2337 * -# Allocate an operation object which will be passed to all the functions
2338 * listed here.
2339 * -# Initialize the operation object with one of the methods described in the
2340 * documentation for #psa_aead_operation_t, e.g.
2341 * #PSA_AEAD_OPERATION_INIT.
2342 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
2343 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2344 * inputs to the subsequent calls to psa_aead_update_ad() and
2345 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2346 * for details.
2347 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2348 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2349 * of the non-encrypted additional authenticated data each time.
2350 * -# Call psa_aead_update() zero, one or more times, passing a fragment
2351 * of the ciphertext to decrypt each time.
2352 * -# Call psa_aead_verify().
2353 *
2354 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2355 * the operation will need to be reset by a call to psa_aead_abort(). The
2356 * application may call psa_aead_abort() at any time after the operation
2357 * has been initialized.
2358 *
2359 * After a successful call to psa_aead_decrypt_setup(), the application must
2360 * eventually terminate the operation. The following events terminate an
2361 * operation:
2362 * - A successful call to psa_aead_verify().
2363 * - A call to psa_aead_abort().
2364 *
2365 * \param[in,out] operation The operation object to set up. It must have
2366 * been initialized as per the documentation for
2367 * #psa_aead_operation_t and not yet in use.
2368 * \param handle Handle to the key to use for the operation.
2369 * It must remain valid until the operation
2370 * terminates.
2371 * \param alg The AEAD algorithm to compute
2372 * (\c PSA_ALG_XXX value such that
2373 * #PSA_ALG_IS_AEAD(\p alg) is true).
2374 *
2375 * \retval #PSA_SUCCESS
2376 * Success.
2377 * \retval #PSA_ERROR_BAD_STATE
2378 * The operation state is not valid (it must be inactive).
2379 * \retval #PSA_ERROR_INVALID_HANDLE
2380 * \retval #PSA_ERROR_NOT_PERMITTED
2381 * \retval #PSA_ERROR_INVALID_ARGUMENT
2382 * \p handle is not compatible with \p alg.
2383 * \retval #PSA_ERROR_NOT_SUPPORTED
2384 * \p alg is not supported or is not an AEAD algorithm.
2385 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2386 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2387 * \retval #PSA_ERROR_HARDWARE_FAILURE
2388 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2389 * \retval #PSA_ERROR_STORAGE_FAILURE
2390 * \retval #PSA_ERROR_BAD_STATE
2391 * The library has not been previously initialized by psa_crypto_init().
2392 * It is implementation-dependent whether a failure to initialize
2393 * results in this error code.
2394 */
2395psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2396 psa_key_handle_t handle,
2397 psa_algorithm_t alg);
2398
2399/** Generate a random nonce for an authenticated encryption operation.
2400 *
2401 * This function generates a random nonce for the authenticated encryption
2402 * operation with an appropriate size for the chosen algorithm, key type
2403 * and key size.
2404 *
2405 * The application must call psa_aead_encrypt_setup() before
2406 * calling this function.
2407 *
2408 * If this function returns an error status, the operation enters an error
2409 * state and must be aborted by calling psa_aead_abort().
2410 *
2411 * \param[in,out] operation Active AEAD operation.
2412 * \param[out] nonce Buffer where the generated nonce is to be
2413 * written.
2414 * \param nonce_size Size of the \p nonce buffer in bytes.
2415 * \param[out] nonce_length On success, the number of bytes of the
2416 * generated nonce.
2417 *
2418 * \retval #PSA_SUCCESS
2419 * Success.
2420 * \retval #PSA_ERROR_BAD_STATE
2421 * The operation state is not valid (it must be an active aead encrypt
2422 operation, with no nonce set).
2423 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2424 * The size of the \p nonce buffer is too small.
2425 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2426 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2427 * \retval #PSA_ERROR_HARDWARE_FAILURE
2428 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2429 * \retval #PSA_ERROR_STORAGE_FAILURE
2430 * \retval #PSA_ERROR_BAD_STATE
2431 * The library has not been previously initialized by psa_crypto_init().
2432 * It is implementation-dependent whether a failure to initialize
2433 * results in this error code.
2434 */
2435psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2436 uint8_t *nonce,
2437 size_t nonce_size,
2438 size_t *nonce_length);
2439
2440/** Set the nonce for an authenticated encryption or decryption operation.
2441 *
2442 * This function sets the nonce for the authenticated
2443 * encryption or decryption operation.
2444 *
2445 * The application must call psa_aead_encrypt_setup() or
2446 * psa_aead_decrypt_setup() before calling this function.
2447 *
2448 * If this function returns an error status, the operation enters an error
2449 * state and must be aborted by calling psa_aead_abort().
2450 *
2451 * \note When encrypting, applications should use psa_aead_generate_nonce()
2452 * instead of this function, unless implementing a protocol that requires
2453 * a non-random IV.
2454 *
2455 * \param[in,out] operation Active AEAD operation.
2456 * \param[in] nonce Buffer containing the nonce to use.
2457 * \param nonce_length Size of the nonce in bytes.
2458 *
2459 * \retval #PSA_SUCCESS
2460 * Success.
2461 * \retval #PSA_ERROR_BAD_STATE
2462 * The operation state is not valid (it must be active, with no nonce
2463 * set).
2464 * \retval #PSA_ERROR_INVALID_ARGUMENT
2465 * The size of \p nonce is not acceptable for the chosen algorithm.
2466 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2467 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2468 * \retval #PSA_ERROR_HARDWARE_FAILURE
2469 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2470 * \retval #PSA_ERROR_STORAGE_FAILURE
2471 * \retval #PSA_ERROR_BAD_STATE
2472 * The library has not been previously initialized by psa_crypto_init().
2473 * It is implementation-dependent whether a failure to initialize
2474 * results in this error code.
2475 */
2476psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2477 const uint8_t *nonce,
2478 size_t nonce_length);
2479
2480/** Declare the lengths of the message and additional data for AEAD.
2481 *
2482 * The application must call this function before calling
2483 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2484 * the operation requires it. If the algorithm does not require it,
2485 * calling this function is optional, but if this function is called
2486 * then the implementation must enforce the lengths.
2487 *
2488 * You may call this function before or after setting the nonce with
2489 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2490 *
2491 * - For #PSA_ALG_CCM, calling this function is required.
2492 * - For the other AEAD algorithms defined in this specification, calling
2493 * this function is not required.
2494 * - For vendor-defined algorithm, refer to the vendor documentation.
2495 *
2496 * If this function returns an error status, the operation enters an error
2497 * state and must be aborted by calling psa_aead_abort().
2498 *
2499 * \param[in,out] operation Active AEAD operation.
2500 * \param ad_length Size of the non-encrypted additional
2501 * authenticated data in bytes.
2502 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2503 *
2504 * \retval #PSA_SUCCESS
2505 * Success.
2506 * \retval #PSA_ERROR_BAD_STATE
2507 * The operation state is not valid (it must be active, and
2508 * psa_aead_update_ad() and psa_aead_update() must not have been
2509 * called yet).
2510 * \retval #PSA_ERROR_INVALID_ARGUMENT
2511 * At least one of the lengths is not acceptable for the chosen
2512 * algorithm.
2513 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2514 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2515 * \retval #PSA_ERROR_HARDWARE_FAILURE
2516 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2517 * \retval #PSA_ERROR_BAD_STATE
2518 * The library has not been previously initialized by psa_crypto_init().
2519 * It is implementation-dependent whether a failure to initialize
2520 * results in this error code.
2521 */
2522psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2523 size_t ad_length,
2524 size_t plaintext_length);
2525
2526/** Pass additional data to an active AEAD operation.
2527 *
2528 * Additional data is authenticated, but not encrypted.
2529 *
2530 * You may call this function multiple times to pass successive fragments
2531 * of the additional data. You may not call this function after passing
2532 * data to encrypt or decrypt with psa_aead_update().
2533 *
2534 * Before calling this function, you must:
2535 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2536 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2537 *
2538 * If this function returns an error status, the operation enters an error
2539 * state and must be aborted by calling psa_aead_abort().
2540 *
2541 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2542 * there is no guarantee that the input is valid. Therefore, until
2543 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2544 * treat the input as untrusted and prepare to undo any action that
2545 * depends on the input if psa_aead_verify() returns an error status.
2546 *
2547 * \param[in,out] operation Active AEAD operation.
2548 * \param[in] input Buffer containing the fragment of
2549 * additional data.
2550 * \param input_length Size of the \p input buffer in bytes.
2551 *
2552 * \retval #PSA_SUCCESS
2553 * Success.
2554 * \retval #PSA_ERROR_BAD_STATE
2555 * The operation state is not valid (it must be active, have a nonce
2556 * set, have lengths set if required by the algorithm, and
2557 * psa_aead_update() must not have been called yet).
2558 * \retval #PSA_ERROR_INVALID_ARGUMENT
2559 * The total input length overflows the additional data length that
2560 * was previously specified with psa_aead_set_lengths().
2561 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2562 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2563 * \retval #PSA_ERROR_HARDWARE_FAILURE
2564 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2565 * \retval #PSA_ERROR_STORAGE_FAILURE
2566 * \retval #PSA_ERROR_BAD_STATE
2567 * The library has not been previously initialized by psa_crypto_init().
2568 * It is implementation-dependent whether a failure to initialize
2569 * results in this error code.
2570 */
2571psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2572 const uint8_t *input,
2573 size_t input_length);
2574
2575/** Encrypt or decrypt a message fragment in an active AEAD operation.
2576 *
2577 * Before calling this function, you must:
2578 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2579 * The choice of setup function determines whether this function
2580 * encrypts or decrypts its input.
2581 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2582 * 3. Call psa_aead_update_ad() to pass all the additional data.
2583 *
2584 * If this function returns an error status, the operation enters an error
2585 * state and must be aborted by calling psa_aead_abort().
2586 *
2587 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2588 * there is no guarantee that the input is valid. Therefore, until
2589 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2590 * - Do not use the output in any way other than storing it in a
2591 * confidential location. If you take any action that depends
2592 * on the tentative decrypted data, this action will need to be
2593 * undone if the input turns out not to be valid. Furthermore,
2594 * if an adversary can observe that this action took place
2595 * (for example through timing), they may be able to use this
2596 * fact as an oracle to decrypt any message encrypted with the
2597 * same key.
2598 * - In particular, do not copy the output anywhere but to a
2599 * memory or storage space that you have exclusive access to.
2600 *
2601 * This function does not require the input to be aligned to any
2602 * particular block boundary. If the implementation can only process
2603 * a whole block at a time, it must consume all the input provided, but
2604 * it may delay the end of the corresponding output until a subsequent
2605 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2606 * provides sufficient input. The amount of data that can be delayed
2607 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
2608 *
2609 * \param[in,out] operation Active AEAD operation.
2610 * \param[in] input Buffer containing the message fragment to
2611 * encrypt or decrypt.
2612 * \param input_length Size of the \p input buffer in bytes.
2613 * \param[out] output Buffer where the output is to be written.
2614 * \param output_size Size of the \p output buffer in bytes.
2615 * This must be at least
2616 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2617 * \p input_length) where \c alg is the
2618 * algorithm that is being calculated.
2619 * \param[out] output_length On success, the number of bytes
2620 * that make up the returned output.
2621 *
2622 * \retval #PSA_SUCCESS
2623 * Success.
2624 * \retval #PSA_ERROR_BAD_STATE
2625 * The operation state is not valid (it must be active, have a nonce
2626 * set, and have lengths set if required by the algorithm).
2627 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2628 * The size of the \p output buffer is too small.
2629 * You can determine a sufficient buffer size by calling
2630 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2631 * where \c alg is the algorithm that is being calculated.
2632 * \retval #PSA_ERROR_INVALID_ARGUMENT
2633 * The total length of input to psa_aead_update_ad() so far is
2634 * less than the additional data length that was previously
2635 * specified with psa_aead_set_lengths().
2636 * \retval #PSA_ERROR_INVALID_ARGUMENT
2637 * The total input length overflows the plaintext length that
2638 * was previously specified with psa_aead_set_lengths().
2639 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2640 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2641 * \retval #PSA_ERROR_HARDWARE_FAILURE
2642 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2643 * \retval #PSA_ERROR_STORAGE_FAILURE
2644 * \retval #PSA_ERROR_BAD_STATE
2645 * The library has not been previously initialized by psa_crypto_init().
2646 * It is implementation-dependent whether a failure to initialize
2647 * results in this error code.
2648 */
2649psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2650 const uint8_t *input,
2651 size_t input_length,
2652 uint8_t *output,
2653 size_t output_size,
2654 size_t *output_length);
2655
2656/** Finish encrypting a message in an AEAD operation.
2657 *
2658 * The operation must have been set up with psa_aead_encrypt_setup().
2659 *
2660 * This function finishes the authentication of the additional data
2661 * formed by concatenating the inputs passed to preceding calls to
2662 * psa_aead_update_ad() with the plaintext formed by concatenating the
2663 * inputs passed to preceding calls to psa_aead_update().
2664 *
2665 * This function has two output buffers:
2666 * - \p ciphertext contains trailing ciphertext that was buffered from
2667 * preceding calls to psa_aead_update().
2668 * - \p tag contains the authentication tag. Its length is always
2669 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
2670 * that the operation performs.
2671 *
2672 * When this function returns successfuly, the operation becomes inactive.
2673 * If this function returns an error status, the operation enters an error
2674 * state and must be aborted by calling psa_aead_abort().
2675 *
2676 * \param[in,out] operation Active AEAD operation.
2677 * \param[out] ciphertext Buffer where the last part of the ciphertext
2678 * is to be written.
2679 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2680 * This must be at least
2681 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2682 * \c alg is the algorithm that is being
2683 * calculated.
2684 * \param[out] ciphertext_length On success, the number of bytes of
2685 * returned ciphertext.
2686 * \param[out] tag Buffer where the authentication tag is
2687 * to be written.
2688 * \param tag_size Size of the \p tag buffer in bytes.
2689 * This must be at least
2690 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2691 * the algorithm that is being calculated.
2692 * \param[out] tag_length On success, the number of bytes
2693 * that make up the returned tag.
2694 *
2695 * \retval #PSA_SUCCESS
2696 * Success.
2697 * \retval #PSA_ERROR_BAD_STATE
2698 * The operation state is not valid (it must be an active encryption
2699 * operation with a nonce set).
2700 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2701 * The size of the \p ciphertext or \p tag buffer is too small.
2702 * You can determine a sufficient buffer size for \p ciphertext by
2703 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2704 * where \c alg is the algorithm that is being calculated.
2705 * You can determine a sufficient buffer size for \p tag by
2706 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
2707 * \retval #PSA_ERROR_INVALID_ARGUMENT
2708 * The total length of input to psa_aead_update_ad() so far is
2709 * less than the additional data length that was previously
2710 * specified with psa_aead_set_lengths().
2711 * \retval #PSA_ERROR_INVALID_ARGUMENT
2712 * The total length of input to psa_aead_update() so far is
2713 * less than the plaintext length that was previously
2714 * specified with psa_aead_set_lengths().
2715 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2716 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2717 * \retval #PSA_ERROR_HARDWARE_FAILURE
2718 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2719 * \retval #PSA_ERROR_STORAGE_FAILURE
2720 * \retval #PSA_ERROR_BAD_STATE
2721 * The library has not been previously initialized by psa_crypto_init().
2722 * It is implementation-dependent whether a failure to initialize
2723 * results in this error code.
2724 */
2725psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
2726 uint8_t *ciphertext,
2727 size_t ciphertext_size,
2728 size_t *ciphertext_length,
2729 uint8_t *tag,
2730 size_t tag_size,
2731 size_t *tag_length);
2732
2733/** Finish authenticating and decrypting a message in an AEAD operation.
2734 *
2735 * The operation must have been set up with psa_aead_decrypt_setup().
2736 *
2737 * This function finishes the authenticated decryption of the message
2738 * components:
2739 *
2740 * - The additional data consisting of the concatenation of the inputs
2741 * passed to preceding calls to psa_aead_update_ad().
2742 * - The ciphertext consisting of the concatenation of the inputs passed to
2743 * preceding calls to psa_aead_update().
2744 * - The tag passed to this function call.
2745 *
2746 * If the authentication tag is correct, this function outputs any remaining
2747 * plaintext and reports success. If the authentication tag is not correct,
2748 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
2749 *
2750 * When this function returns successfuly, the operation becomes inactive.
2751 * If this function returns an error status, the operation enters an error
2752 * state and must be aborted by calling psa_aead_abort().
2753 *
2754 * \note Implementations shall make the best effort to ensure that the
2755 * comparison between the actual tag and the expected tag is performed
2756 * in constant time.
2757 *
2758 * \param[in,out] operation Active AEAD operation.
2759 * \param[out] plaintext Buffer where the last part of the plaintext
2760 * is to be written. This is the remaining data
2761 * from previous calls to psa_aead_update()
2762 * that could not be processed until the end
2763 * of the input.
2764 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2765 * This must be at least
2766 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2767 * \c alg is the algorithm that is being
2768 * calculated.
2769 * \param[out] plaintext_length On success, the number of bytes of
2770 * returned plaintext.
2771 * \param[in] tag Buffer containing the authentication tag.
2772 * \param tag_length Size of the \p tag buffer in bytes.
2773 *
2774 * \retval #PSA_SUCCESS
2775 * Success.
2776 * \retval #PSA_ERROR_INVALID_SIGNATURE
2777 * The calculations were successful, but the authentication tag is
2778 * not correct.
2779 * \retval #PSA_ERROR_BAD_STATE
2780 * The operation state is not valid (it must be an active decryption
2781 * operation with a nonce set).
2782 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2783 * The size of the \p plaintext buffer is too small.
2784 * You can determine a sufficient buffer size for \p plaintext by
2785 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2786 * where \c alg is the algorithm that is being calculated.
2787 * \retval #PSA_ERROR_INVALID_ARGUMENT
2788 * The total length of input to psa_aead_update_ad() so far is
2789 * less than the additional data length that was previously
2790 * specified with psa_aead_set_lengths().
2791 * \retval #PSA_ERROR_INVALID_ARGUMENT
2792 * The total length of input to psa_aead_update() so far is
2793 * less than the plaintext length that was previously
2794 * specified with psa_aead_set_lengths().
2795 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2796 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2797 * \retval #PSA_ERROR_HARDWARE_FAILURE
2798 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2799 * \retval #PSA_ERROR_STORAGE_FAILURE
2800 * \retval #PSA_ERROR_BAD_STATE
2801 * The library has not been previously initialized by psa_crypto_init().
2802 * It is implementation-dependent whether a failure to initialize
2803 * results in this error code.
2804 */
2805psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2806 uint8_t *plaintext,
2807 size_t plaintext_size,
2808 size_t *plaintext_length,
2809 const uint8_t *tag,
2810 size_t tag_length);
2811
2812/** Abort an AEAD operation.
2813 *
2814 * Aborting an operation frees all associated resources except for the
2815 * \p operation structure itself. Once aborted, the operation object
2816 * can be reused for another operation by calling
2817 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2818 *
2819 * You may call this function any time after the operation object has
2820 * been initialized as described in #psa_aead_operation_t.
2821 *
2822 * In particular, calling psa_aead_abort() after the operation has been
2823 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2824 * psa_aead_verify() is safe and has no effect.
2825 *
2826 * \param[in,out] operation Initialized AEAD operation.
2827 *
2828 * \retval #PSA_SUCCESS
2829 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2830 * \retval #PSA_ERROR_HARDWARE_FAILURE
2831 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2832 * \retval #PSA_ERROR_BAD_STATE
2833 * The library has not been previously initialized by psa_crypto_init().
2834 * It is implementation-dependent whether a failure to initialize
2835 * results in this error code.
2836 */
2837psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2838
Antonio de Angelis14276e92018-07-10 14:35:43 +01002839/**@}*/
2840
2841/** \defgroup asymmetric Asymmetric cryptography
2842 * @{
2843 */
2844
2845/**
Antonio de Angelis14276e92018-07-10 14:35:43 +01002846 * \brief Sign a hash or short message with a private key.
2847 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002848 * Note that to perform a hash-and-sign signature algorithm, you must
2849 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
2850 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2851 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2852 * to determine the hash algorithm to use.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002853 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002854 * \param handle Handle to the key to use for the operation.
2855 * It must be an asymmetric key pair.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002856 * \param alg A signature algorithm that is compatible with
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002857 * the type of \p handle.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002858 * \param[in] hash The hash or message to sign.
2859 * \param hash_length Size of the \p hash buffer in bytes.
2860 * \param[out] signature Buffer where the signature is to be written.
2861 * \param signature_size Size of the \p signature buffer in bytes.
2862 * \param[out] signature_length On success, the number of bytes
2863 * that make up the returned signature value.
2864 *
2865 * \retval #PSA_SUCCESS
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002866 * \retval #PSA_ERROR_INVALID_HANDLE
2867 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis377a1552018-11-22 17:02:40 +00002868 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2869 * The size of the \p signature buffer is too small. You can
Antonio de Angelis14276e92018-07-10 14:35:43 +01002870 * determine a sufficient buffer size by calling
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002871 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01002872 * where \c key_type and \c key_bits are the type and bit-size
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002873 * respectively of \p handle.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002874 * \retval #PSA_ERROR_NOT_SUPPORTED
2875 * \retval #PSA_ERROR_INVALID_ARGUMENT
2876 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2877 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2878 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002879 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2880 * \retval #PSA_ERROR_STORAGE_FAILURE
Antonio de Angelis377a1552018-11-22 17:02:40 +00002881 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002882 * \retval #PSA_ERROR_BAD_STATE
2883 * The library has not been previously initialized by psa_crypto_init().
2884 * It is implementation-dependent whether a failure to initialize
2885 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002886 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002887psa_status_t psa_sign_hash(psa_key_handle_t handle,
2888 psa_algorithm_t alg,
2889 const uint8_t *hash,
2890 size_t hash_length,
2891 uint8_t *signature,
2892 size_t signature_size,
2893 size_t *signature_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01002894
2895/**
2896 * \brief Verify the signature a hash or short message using a public key.
2897 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002898 * Note that to perform a hash-and-sign signature algorithm, you must
2899 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
2900 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2901 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2902 * to determine the hash algorithm to use.
2903 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002904 * \param handle Handle to the key to use for the operation.
2905 * It must be a public key or an asymmetric key pair.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002906 * \param alg A signature algorithm that is compatible with
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002907 * the type of \p handle.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002908 * \param[in] hash The hash or message whose signature is to be
2909 * verified.
2910 * \param hash_length Size of the \p hash buffer in bytes.
2911 * \param[in] signature Buffer containing the signature to verify.
2912 * \param signature_length Size of the \p signature buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002913 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002914 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01002915 * The signature is valid.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002916 * \retval #PSA_ERROR_INVALID_HANDLE
2917 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis377a1552018-11-22 17:02:40 +00002918 * \retval #PSA_ERROR_INVALID_SIGNATURE
Antonio de Angelis14276e92018-07-10 14:35:43 +01002919 * The calculation was perfomed successfully, but the passed
2920 * signature is not a valid signature.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002921 * \retval #PSA_ERROR_NOT_SUPPORTED
2922 * \retval #PSA_ERROR_INVALID_ARGUMENT
2923 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2924 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2925 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002926 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2927 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002928 * \retval #PSA_ERROR_BAD_STATE
2929 * The library has not been previously initialized by psa_crypto_init().
2930 * It is implementation-dependent whether a failure to initialize
2931 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002932 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002933psa_status_t psa_verify_hash(psa_key_handle_t handle,
2934 psa_algorithm_t alg,
2935 const uint8_t *hash,
2936 size_t hash_length,
2937 const uint8_t *signature,
2938 size_t signature_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01002939
Antonio de Angelis14276e92018-07-10 14:35:43 +01002940/**
2941 * \brief Encrypt a short message with a public key.
2942 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002943 * \param handle Handle to the key to use for the operation.
2944 * It must be a public key or an asymmetric
2945 * key pair.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002946 * \param alg An asymmetric encryption algorithm that is
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002947 * compatible with the type of \p handle.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002948 * \param[in] input The message to encrypt.
2949 * \param input_length Size of the \p input buffer in bytes.
2950 * \param[in] salt A salt or label, if supported by the
2951 * encryption algorithm.
2952 * If the algorithm does not support a
2953 * salt, pass \c NULL.
2954 * If the algorithm supports an optional
2955 * salt and you do not want to pass a salt,
2956 * pass \c NULL.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002957 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002958 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2959 * supported.
2960 * \param salt_length Size of the \p salt buffer in bytes.
2961 * If \p salt is \c NULL, pass 0.
2962 * \param[out] output Buffer where the encrypted message is to
2963 * be written.
2964 * \param output_size Size of the \p output buffer in bytes.
2965 * \param[out] output_length On success, the number of bytes
2966 * that make up the returned output.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002967 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002968 * \retval #PSA_SUCCESS
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002969 * \retval #PSA_ERROR_INVALID_HANDLE
2970 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis377a1552018-11-22 17:02:40 +00002971 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2972 * The size of the \p output buffer is too small. You can
Antonio de Angelis14276e92018-07-10 14:35:43 +01002973 * determine a sufficient buffer size by calling
Antonio de Angelis377a1552018-11-22 17:02:40 +00002974 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01002975 * where \c key_type and \c key_bits are the type and bit-size
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002976 * respectively of \p handle.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002977 * \retval #PSA_ERROR_NOT_SUPPORTED
2978 * \retval #PSA_ERROR_INVALID_ARGUMENT
2979 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2980 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2981 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002982 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2983 * \retval #PSA_ERROR_STORAGE_FAILURE
Antonio de Angelis377a1552018-11-22 17:02:40 +00002984 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002985 * \retval #PSA_ERROR_BAD_STATE
2986 * The library has not been previously initialized by psa_crypto_init().
2987 * It is implementation-dependent whether a failure to initialize
2988 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002989 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002990psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Antonio de Angelis14276e92018-07-10 14:35:43 +01002991 psa_algorithm_t alg,
2992 const uint8_t *input,
2993 size_t input_length,
2994 const uint8_t *salt,
2995 size_t salt_length,
2996 uint8_t *output,
2997 size_t output_size,
2998 size_t *output_length);
2999
3000/**
3001 * \brief Decrypt a short message with a private key.
3002 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003003 * \param handle Handle to the key to use for the operation.
3004 * It must be an asymmetric key pair.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003005 * \param alg An asymmetric encryption algorithm that is
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003006 * compatible with the type of \p handle.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003007 * \param[in] input The message to decrypt.
3008 * \param input_length Size of the \p input buffer in bytes.
3009 * \param[in] salt A salt or label, if supported by the
3010 * encryption algorithm.
3011 * If the algorithm does not support a
3012 * salt, pass \c NULL.
3013 * If the algorithm supports an optional
3014 * salt and you do not want to pass a salt,
3015 * pass \c NULL.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003016 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00003017 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3018 * supported.
3019 * \param salt_length Size of the \p salt buffer in bytes.
3020 * If \p salt is \c NULL, pass 0.
3021 * \param[out] output Buffer where the decrypted message is to
3022 * be written.
3023 * \param output_size Size of the \c output buffer in bytes.
3024 * \param[out] output_length On success, the number of bytes
3025 * that make up the returned output.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003026 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00003027 * \retval #PSA_SUCCESS
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003028 * \retval #PSA_ERROR_INVALID_HANDLE
3029 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis377a1552018-11-22 17:02:40 +00003030 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3031 * The size of the \p output buffer is too small. You can
Antonio de Angelis14276e92018-07-10 14:35:43 +01003032 * determine a sufficient buffer size by calling
Antonio de Angelis377a1552018-11-22 17:02:40 +00003033 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01003034 * where \c key_type and \c key_bits are the type and bit-size
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003035 * respectively of \p handle.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003036 * \retval #PSA_ERROR_NOT_SUPPORTED
3037 * \retval #PSA_ERROR_INVALID_ARGUMENT
3038 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3039 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3040 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003041 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3042 * \retval #PSA_ERROR_STORAGE_FAILURE
Antonio de Angelis377a1552018-11-22 17:02:40 +00003043 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3044 * \retval #PSA_ERROR_INVALID_PADDING
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003045 * \retval #PSA_ERROR_BAD_STATE
3046 * The library has not been previously initialized by psa_crypto_init().
3047 * It is implementation-dependent whether a failure to initialize
3048 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003049 */
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003050psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Antonio de Angelis14276e92018-07-10 14:35:43 +01003051 psa_algorithm_t alg,
3052 const uint8_t *input,
3053 size_t input_length,
3054 const uint8_t *salt,
3055 size_t salt_length,
3056 uint8_t *output,
3057 size_t output_size,
3058 size_t *output_length);
3059
3060/**@}*/
3061
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003062/** \defgroup key_derivation Key derivation and pseudorandom generation
Antonio de Angelis377a1552018-11-22 17:02:40 +00003063 * @{
3064 */
3065
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003066/** The type of the state data structure for key derivation operations.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003067 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003068 * Before calling any function on a key derivation operation object, the
3069 * application must initialize it by any of the following means:
Antonio de Angelis377a1552018-11-22 17:02:40 +00003070 * - Set the structure to all-bits-zero, for example:
3071 * \code
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003072 * psa_key_derivation_operation_t operation;
3073 * memset(&operation, 0, sizeof(operation));
Antonio de Angelis377a1552018-11-22 17:02:40 +00003074 * \endcode
3075 * - Initialize the structure to logical zero values, for example:
3076 * \code
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003077 * psa_key_derivation_operation_t operation = {0};
Antonio de Angelis377a1552018-11-22 17:02:40 +00003078 * \endcode
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003079 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Antonio de Angelis377a1552018-11-22 17:02:40 +00003080 * for example:
3081 * \code
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003082 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Antonio de Angelis377a1552018-11-22 17:02:40 +00003083 * \endcode
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003084 * - Assign the result of the function psa_key_derivation_operation_init()
Antonio de Angelis377a1552018-11-22 17:02:40 +00003085 * to the structure, for example:
3086 * \code
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003087 * psa_key_derivation_operation_t operation;
3088 * operation = psa_key_derivation_operation_init();
Antonio de Angelis377a1552018-11-22 17:02:40 +00003089 * \endcode
3090 *
3091 * This is an implementation-defined \c struct. Applications should not
3092 * make any assumptions about the content of this structure except
3093 * as directed by the documentation of a specific implementation.
3094 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003095typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Antonio de Angelis377a1552018-11-22 17:02:40 +00003096
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003097/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Antonio de Angelis377a1552018-11-22 17:02:40 +00003098 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003099 * This macro returns a suitable initializer for a key derivation operation
3100 * object of type #psa_key_derivation_operation_t.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003101 */
3102#ifdef __DOXYGEN_ONLY__
3103/* This is an example definition for documentation purposes.
3104 * Implementations should define a suitable value in `crypto_struct.h`.
3105 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003106#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Antonio de Angelis377a1552018-11-22 17:02:40 +00003107#endif
3108
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003109/** Return an initial value for a key derivation operation object.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003110 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003111static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Antonio de Angelis377a1552018-11-22 17:02:40 +00003112
3113/** Set up a key derivation operation.
3114 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003115 * A key derivation algorithm takes some inputs and uses them to generate
3116 * a byte stream in a deterministic way.
3117 * This byte stream can be used to produce keys and other
3118 * cryptographic material.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003119 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003120 * To derive a key:
3121 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3122 * -# Call psa_key_derivation_setup() to select the algorithm.
3123 * -# Provide the inputs for the key derivation by calling
3124 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3125 * as appropriate. Which inputs are needed, in what order, and whether
3126 * they may be keys and if so of what type depends on the algorithm.
3127 * -# Optionally set the operation's maximum capacity with
3128 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3129 * of or after providing inputs. For some algorithms, this step is mandatory
3130 * because the output depends on the maximum capacity.
3131 * -# To derive a key, call psa_key_derivation_output_key().
3132 * To derive a byte string for a different purpose, call
3133 * psa_key_derivation_output_bytes().
3134 * Successive calls to these functions use successive output bytes
3135 * calculated by the key derivation algorithm.
3136 * -# Clean up the key derivation operation object with
3137 * psa_key_derivation_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00003138 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003139 * If this function returns an error, the key derivation operation object is
3140 * not changed.
3141 *
3142 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3143 * the operation will need to be reset by a call to psa_key_derivation_abort().
3144 *
3145 * Implementations must reject an attempt to derive a key of size 0.
3146 *
3147 * \param[in,out] operation The key derivation operation object
3148 * to set up. It must
3149 * have been initialized but not set up yet.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003150 * \param alg The key derivation algorithm to compute
3151 * (\c PSA_ALG_XXX value such that
3152 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +00003153 *
3154 * \retval #PSA_SUCCESS
3155 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003156 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003157 * \c alg is not a key derivation algorithm.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003158 * \retval #PSA_ERROR_NOT_SUPPORTED
3159 * \c alg is not supported or is not a key derivation algorithm.
3160 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3161 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3162 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003163 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3164 * \retval #PSA_ERROR_STORAGE_FAILURE
3165 * \retval #PSA_ERROR_BAD_STATE
3166 * The operation state is not valid (it must be inactive).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003167 * \retval #PSA_ERROR_BAD_STATE
3168 * The library has not been previously initialized by psa_crypto_init().
3169 * It is implementation-dependent whether a failure to initialize
3170 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003171 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003172psa_status_t psa_key_derivation_setup(
3173 psa_key_derivation_operation_t *operation,
3174 psa_algorithm_t alg);
Antonio de Angelis377a1552018-11-22 17:02:40 +00003175
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003176/** Retrieve the current capacity of a key derivation operation.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003177 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003178 * The capacity of a key derivation is the maximum number of bytes that it can
3179 * return. When you get *N* bytes of output from a key derivation operation,
3180 * this reduces its capacity by *N*.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003181 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003182 * \param[in] operation The operation to query.
3183 * \param[out] capacity On success, the capacity of the operation.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003184 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003185 * \retval #PSA_SUCCESS
3186 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3187 * \retval #PSA_ERROR_BAD_STATE
3188 * The operation state is not valid (it must be active).
3189 * \retval #PSA_ERROR_HARDWARE_FAILURE
3190 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3191 * \retval #PSA_ERROR_BAD_STATE
3192 * The library has not been previously initialized by psa_crypto_init().
3193 * It is implementation-dependent whether a failure to initialize
3194 * results in this error code.
3195 */
3196psa_status_t psa_key_derivation_get_capacity(
3197 const psa_key_derivation_operation_t *operation,
3198 size_t *capacity);
3199
3200/** Set the maximum capacity of a key derivation operation.
3201 *
3202 * The capacity of a key derivation operation is the maximum number of bytes
3203 * that the key derivation operation can return from this point onwards.
3204 *
3205 * \param[in,out] operation The key derivation operation object to modify.
3206 * \param capacity The new capacity of the operation.
3207 * It must be less or equal to the operation's
3208 * current capacity.
3209 *
3210 * \retval #PSA_SUCCESS
3211 * \retval #PSA_ERROR_INVALID_ARGUMENT
3212 * \p capacity is larger than the operation's current capacity.
3213 * In this case, the operation object remains valid and its capacity
3214 * remains unchanged.
3215 * \retval #PSA_ERROR_BAD_STATE
3216 * The operation state is not valid (it must be active).
3217 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3218 * \retval #PSA_ERROR_HARDWARE_FAILURE
3219 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3220 * \retval #PSA_ERROR_BAD_STATE
3221 * The library has not been previously initialized by psa_crypto_init().
3222 * It is implementation-dependent whether a failure to initialize
3223 * results in this error code.
3224 */
3225psa_status_t psa_key_derivation_set_capacity(
3226 psa_key_derivation_operation_t *operation,
3227 size_t capacity);
3228
3229/** Use the maximum possible capacity for a key derivation operation.
3230 *
3231 * Use this value as the capacity argument when setting up a key derivation
3232 * to indicate that the operation should have the maximum possible capacity.
3233 * The value of the maximum possible capacity depends on the key derivation
3234 * algorithm.
3235 */
3236#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3237
3238/** Provide an input for key derivation or key agreement.
3239 *
3240 * Which inputs are required and in what order depends on the algorithm.
3241 * Refer to the documentation of each key derivation or key agreement
3242 * algorithm for information.
3243 *
3244 * This function passes direct inputs, which is usually correct for
3245 * non-secret inputs. To pass a secret input, which should be in a key
3246 * object, call psa_key_derivation_input_key() instead of this function.
3247 * Refer to the documentation of individual step types
3248 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3249 * for more information.
3250 *
3251 * If this function returns an error status, the operation enters an error
3252 * state and must be aborted by calling psa_key_derivation_abort().
3253 *
3254 * \param[in,out] operation The key derivation operation object to use.
3255 * It must have been set up with
3256 * psa_key_derivation_setup() and must not
3257 * have produced any output yet.
3258 * \param step Which step the input data is for.
3259 * \param[in] data Input data to use.
3260 * \param data_length Size of the \p data buffer in bytes.
3261 *
3262 * \retval #PSA_SUCCESS
3263 * Success.
3264 * \retval #PSA_ERROR_INVALID_ARGUMENT
3265 * \c step is not compatible with the operation's algorithm.
3266 * \retval #PSA_ERROR_INVALID_ARGUMENT
3267 * \c step does not allow direct inputs.
3268 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3269 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3270 * \retval #PSA_ERROR_HARDWARE_FAILURE
3271 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3272 * \retval #PSA_ERROR_STORAGE_FAILURE
3273 * \retval #PSA_ERROR_BAD_STATE
3274 * The operation state is not valid for this input \p step.
3275 * \retval #PSA_ERROR_BAD_STATE
3276 * The library has not been previously initialized by psa_crypto_init().
3277 * It is implementation-dependent whether a failure to initialize
3278 * results in this error code.
3279 */
3280psa_status_t psa_key_derivation_input_bytes(
3281 psa_key_derivation_operation_t *operation,
3282 psa_key_derivation_step_t step,
3283 const uint8_t *data,
3284 size_t data_length);
3285
3286/** Provide an input for key derivation in the form of a key.
3287 *
3288 * Which inputs are required and in what order depends on the algorithm.
3289 * Refer to the documentation of each key derivation or key agreement
3290 * algorithm for information.
3291 *
3292 * This function obtains input from a key object, which is usually correct for
3293 * secret inputs or for non-secret personalization strings kept in the key
3294 * store. To pass a non-secret parameter which is not in the key store,
3295 * call psa_key_derivation_input_bytes() instead of this function.
3296 * Refer to the documentation of individual step types
3297 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3298 * for more information.
3299 *
3300 * If this function returns an error status, the operation enters an error
3301 * state and must be aborted by calling psa_key_derivation_abort().
3302 *
3303 * \param[in,out] operation The key derivation operation object to use.
3304 * It must have been set up with
3305 * psa_key_derivation_setup() and must not
3306 * have produced any output yet.
3307 * \param step Which step the input data is for.
3308 * \param handle Handle to the key. It must have an
3309 * appropriate type for \p step and must
3310 * allow the usage #PSA_KEY_USAGE_DERIVE.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003311 *
3312 * \retval #PSA_SUCCESS
3313 * Success.
3314 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003315 * \retval #PSA_ERROR_NOT_PERMITTED
3316 * \retval #PSA_ERROR_INVALID_ARGUMENT
3317 * \c step is not compatible with the operation's algorithm.
3318 * \retval #PSA_ERROR_INVALID_ARGUMENT
3319 * \c step does not allow key inputs of the given type
3320 * or does not allow key inputs at all.
3321 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3322 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3323 * \retval #PSA_ERROR_HARDWARE_FAILURE
3324 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3325 * \retval #PSA_ERROR_STORAGE_FAILURE
3326 * \retval #PSA_ERROR_BAD_STATE
3327 * The operation state is not valid for this input \p step.
3328 * \retval #PSA_ERROR_BAD_STATE
3329 * The library has not been previously initialized by psa_crypto_init().
3330 * It is implementation-dependent whether a failure to initialize
3331 * results in this error code.
3332 */
3333psa_status_t psa_key_derivation_input_key(
3334 psa_key_derivation_operation_t *operation,
3335 psa_key_derivation_step_t step,
3336 psa_key_handle_t handle);
3337
3338/** Perform a key agreement and use the shared secret as input to a key
3339 * derivation.
3340 *
3341 * A key agreement algorithm takes two inputs: a private key \p private_key
3342 * a public key \p peer_key.
3343 * The result of this function is passed as input to a key derivation.
3344 * The output of this key derivation can be extracted by reading from the
3345 * resulting operation to produce keys and other cryptographic material.
3346 *
3347 * If this function returns an error status, the operation enters an error
3348 * state and must be aborted by calling psa_key_derivation_abort().
3349 *
3350 * \param[in,out] operation The key derivation operation object to use.
3351 * It must have been set up with
3352 * psa_key_derivation_setup() with a
3353 * key agreement and derivation algorithm
3354 * \c alg (\c PSA_ALG_XXX value such that
3355 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3356 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3357 * is false).
3358 * The operation must be ready for an
3359 * input of the type given by \p step.
3360 * \param step Which step the input data is for.
3361 * \param private_key Handle to the private key to use.
3362 * \param[in] peer_key Public key of the peer. The peer key must be in the
3363 * same format that psa_import_key() accepts for the
3364 * public key type corresponding to the type of
3365 * private_key. That is, this function performs the
3366 * equivalent of
3367 * #psa_import_key(...,
3368 * `peer_key`, `peer_key_length`) where
3369 * with key attributes indicating the public key
3370 * type corresponding to the type of `private_key`.
3371 * For example, for EC keys, this means that peer_key
3372 * is interpreted as a point on the curve that the
3373 * private key is on. The standard formats for public
3374 * keys are documented in the documentation of
3375 * psa_export_public_key().
3376 * \param peer_key_length Size of \p peer_key in bytes.
3377 *
3378 * \retval #PSA_SUCCESS
3379 * Success.
3380 * \retval #PSA_ERROR_BAD_STATE
3381 * The operation state is not valid for this key agreement \p step.
3382 * \retval #PSA_ERROR_INVALID_HANDLE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003383 * \retval #PSA_ERROR_NOT_PERMITTED
3384 * \retval #PSA_ERROR_INVALID_ARGUMENT
3385 * \c private_key is not compatible with \c alg,
3386 * or \p peer_key is not valid for \c alg or not compatible with
3387 * \c private_key.
3388 * \retval #PSA_ERROR_NOT_SUPPORTED
3389 * \c alg is not supported or is not a key derivation algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003390 * \retval #PSA_ERROR_INVALID_ARGUMENT
3391 * \c step does not allow an input resulting from a key agreement.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003392 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3393 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3394 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003395 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3396 * \retval #PSA_ERROR_STORAGE_FAILURE
3397 * \retval #PSA_ERROR_BAD_STATE
3398 * The library has not been previously initialized by psa_crypto_init().
3399 * It is implementation-dependent whether a failure to initialize
3400 * results in this error code.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003401 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003402psa_status_t psa_key_derivation_key_agreement(
3403 psa_key_derivation_operation_t *operation,
3404 psa_key_derivation_step_t step,
3405 psa_key_handle_t private_key,
3406 const uint8_t *peer_key,
3407 size_t peer_key_length);
3408
3409/** Read some data from a key derivation operation.
3410 *
3411 * This function calculates output bytes from a key derivation algorithm and
3412 * return those bytes.
3413 * If you view the key derivation's output as a stream of bytes, this
3414 * function destructively reads the requested number of bytes from the
3415 * stream.
3416 * The operation's capacity decreases by the number of bytes read.
3417 *
3418 * If this function returns an error status other than
3419 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
3420 * state and must be aborted by calling psa_key_derivation_abort().
3421 *
3422 * \param[in,out] operation The key derivation operation object to read from.
3423 * \param[out] output Buffer where the output will be written.
3424 * \param output_length Number of bytes to output.
3425 *
3426 * \retval #PSA_SUCCESS
3427 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3428 * The operation's capacity was less than
3429 * \p output_length bytes. Note that in this case,
3430 * no output is written to the output buffer.
3431 * The operation's capacity is set to 0, thus
3432 * subsequent calls to this function will not
3433 * succeed, even with a smaller output buffer.
3434 * \retval #PSA_ERROR_BAD_STATE
3435 * The operation state is not valid (it must be active and completed
3436 * all required input steps).
3437 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3438 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3439 * \retval #PSA_ERROR_HARDWARE_FAILURE
3440 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3441 * \retval #PSA_ERROR_STORAGE_FAILURE
3442 * \retval #PSA_ERROR_BAD_STATE
3443 * The library has not been previously initialized by psa_crypto_init().
3444 * It is implementation-dependent whether a failure to initialize
3445 * results in this error code.
3446 */
3447psa_status_t psa_key_derivation_output_bytes(
3448 psa_key_derivation_operation_t *operation,
3449 uint8_t *output,
3450 size_t output_length);
3451
3452/** Derive a key from an ongoing key derivation operation.
3453 *
3454 * This function calculates output bytes from a key derivation algorithm
3455 * and uses those bytes to generate a key deterministically.
3456 * The key's location, usage policy, type and size are taken from
3457 * \p attributes.
3458 *
3459 * If you view the key derivation's output as a stream of bytes, this
3460 * function destructively reads as many bytes as required from the
3461 * stream.
3462 * The operation's capacity decreases by the number of bytes read.
3463 *
3464 * If this function returns an error status other than
3465 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
3466 * state and must be aborted by calling psa_key_derivation_abort().
3467 *
3468 * How much output is produced and consumed from the operation, and how
3469 * the key is derived, depends on the key type:
3470 *
3471 * - For key types for which the key is an arbitrary sequence of bytes
3472 * of a given size, this function is functionally equivalent to
3473 * calling #psa_key_derivation_output_bytes
3474 * and passing the resulting output to #psa_import_key.
3475 * However, this function has a security benefit:
3476 * if the implementation provides an isolation boundary then
3477 * the key material is not exposed outside the isolation boundary.
3478 * As a consequence, for these key types, this function always consumes
3479 * exactly (\p bits / 8) bytes from the operation.
3480 * The following key types defined in this specification follow this scheme:
3481 *
3482 * - #PSA_KEY_TYPE_AES;
3483 * - #PSA_KEY_TYPE_ARC4;
3484 * - #PSA_KEY_TYPE_CAMELLIA;
3485 * - #PSA_KEY_TYPE_DERIVE;
3486 * - #PSA_KEY_TYPE_HMAC.
3487 *
3488 * - For ECC keys on a Montgomery elliptic curve
3489 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
3490 * Montgomery curve), this function always draws a byte string whose
3491 * length is determined by the curve, and sets the mandatory bits
3492 * accordingly. That is:
3493 *
Summer Qin0e5b2e02020-10-22 11:23:39 +08003494 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Soby Mathew07ef6e42020-07-20 21:09:23 +01003495 * string and process it as specified in RFC 7748 &sect;5.
Summer Qin0e5b2e02020-10-22 11:23:39 +08003496 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Soby Mathew07ef6e42020-07-20 21:09:23 +01003497 * string and process it as specified in RFC 7748 &sect;5.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003498 *
3499 * - For key types for which the key is represented by a single sequence of
3500 * \p bits bits with constraints as to which bit sequences are acceptable,
3501 * this function draws a byte string of length (\p bits / 8) bytes rounded
3502 * up to the nearest whole number of bytes. If the resulting byte string
3503 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3504 * This process is repeated until an acceptable byte string is drawn.
3505 * The byte string drawn from the operation is interpreted as specified
3506 * for the output produced by psa_export_key().
3507 * The following key types defined in this specification follow this scheme:
3508 *
3509 * - #PSA_KEY_TYPE_DES.
3510 * Force-set the parity bits, but discard forbidden weak keys.
3511 * For 2-key and 3-key triple-DES, the three keys are generated
3512 * successively (for example, for 3-key triple-DES,
3513 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3514 * discard the first 8 bytes, use the next 8 bytes as the first key,
3515 * and continue reading output from the operation to derive the other
3516 * two keys).
3517 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
3518 * where \c group designates any Diffie-Hellman group) and
3519 * ECC keys on a Weierstrass elliptic curve
3520 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
3521 * Weierstrass curve).
3522 * For these key types, interpret the byte string as integer
3523 * in big-endian order. Discard it if it is not in the range
3524 * [0, *N* - 2] where *N* is the boundary of the private key domain
3525 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
3526 * or the order of the curve's base point for ECC).
3527 * Add 1 to the resulting integer and use this as the private key *x*.
3528 * This method allows compliance to NIST standards, specifically
3529 * the methods titled "key-pair generation by testing candidates"
3530 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3531 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3532 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3533 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
3534 *
3535 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
3536 * the way in which the operation output is consumed is
3537 * implementation-defined.
3538 *
3539 * In all cases, the data that is read is discarded from the operation.
3540 * The operation's capacity is decreased by the number of bytes read.
3541 *
3542 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3543 * the input to that step must be provided with psa_key_derivation_input_key().
3544 * Future versions of this specification may include additional restrictions
3545 * on the derived key based on the attributes and strength of the secret key.
3546 *
3547 * \param[in] attributes The attributes for the new key.
3548 * \param[in,out] operation The key derivation operation object to read from.
3549 * \param[out] handle On success, a handle to the newly created key.
3550 * \c 0 on failure.
3551 *
3552 * \retval #PSA_SUCCESS
3553 * Success.
3554 * If the key is persistent, the key material and the key's metadata
3555 * have been saved to persistent storage.
3556 * \retval #PSA_ERROR_ALREADY_EXISTS
3557 * This is an attempt to create a persistent key, and there is
3558 * already a persistent key with the given identifier.
3559 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3560 * There was not enough data to create the desired key.
3561 * Note that in this case, no output is written to the output buffer.
3562 * The operation's capacity is set to 0, thus subsequent calls to
3563 * this function will not succeed, even with a smaller output buffer.
3564 * \retval #PSA_ERROR_NOT_SUPPORTED
3565 * The key type or key size is not supported, either by the
3566 * implementation in general or in this particular location.
3567 * \retval #PSA_ERROR_INVALID_ARGUMENT
3568 * The provided key attributes are not valid for the operation.
3569 * \retval #PSA_ERROR_NOT_PERMITTED
3570 * The #PSA_KEY_DERIVATION_INPUT_SECRET input was not provided through
3571 * a key.
3572 * \retval #PSA_ERROR_BAD_STATE
3573 * The operation state is not valid (it must be active and completed
3574 * all required input steps).
3575 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3576 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3577 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3578 * \retval #PSA_ERROR_HARDWARE_FAILURE
3579 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3580 * \retval #PSA_ERROR_STORAGE_FAILURE
3581 * \retval #PSA_ERROR_BAD_STATE
3582 * The library has not been previously initialized by psa_crypto_init().
3583 * It is implementation-dependent whether a failure to initialize
3584 * results in this error code.
3585 */
3586psa_status_t psa_key_derivation_output_key(
3587 const psa_key_attributes_t *attributes,
3588 psa_key_derivation_operation_t *operation,
3589 psa_key_handle_t *handle);
3590
3591/** Abort a key derivation operation.
3592 *
3593 * Aborting an operation frees all associated resources except for the \c
3594 * operation structure itself. Once aborted, the operation object can be reused
3595 * for another operation by calling psa_key_derivation_setup() again.
3596 *
3597 * This function may be called at any time after the operation
3598 * object has been initialized as described in #psa_key_derivation_operation_t.
3599 *
3600 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3601 * call psa_key_derivation_abort() on an operation that has not been set up.
3602 *
3603 * \param[in,out] operation The operation to abort.
3604 *
3605 * \retval #PSA_SUCCESS
3606 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3607 * \retval #PSA_ERROR_HARDWARE_FAILURE
3608 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3609 * \retval #PSA_ERROR_BAD_STATE
3610 * The library has not been previously initialized by psa_crypto_init().
3611 * It is implementation-dependent whether a failure to initialize
3612 * results in this error code.
3613 */
3614psa_status_t psa_key_derivation_abort(
3615 psa_key_derivation_operation_t *operation);
3616
3617/** Perform a key agreement and return the raw shared secret.
3618 *
3619 * \warning The raw result of a key agreement algorithm such as finite-field
3620 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3621 * not be used directly as key material. It should instead be passed as
3622 * input to a key derivation algorithm. To chain a key agreement with
3623 * a key derivation, use psa_key_derivation_key_agreement() and other
3624 * functions from the key derivation interface.
3625 *
3626 * \param alg The key agreement algorithm to compute
3627 * (\c PSA_ALG_XXX value such that
3628 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3629 * is true).
3630 * \param private_key Handle to the private key to use.
3631 * \param[in] peer_key Public key of the peer. It must be
3632 * in the same format that psa_import_key()
3633 * accepts. The standard formats for public
3634 * keys are documented in the documentation
3635 * of psa_export_public_key().
3636 * \param peer_key_length Size of \p peer_key in bytes.
3637 * \param[out] output Buffer where the decrypted message is to
3638 * be written.
3639 * \param output_size Size of the \c output buffer in bytes.
3640 * \param[out] output_length On success, the number of bytes
3641 * that make up the returned output.
3642 *
3643 * \retval #PSA_SUCCESS
3644 * Success.
3645 * \retval #PSA_ERROR_INVALID_HANDLE
3646 * \retval #PSA_ERROR_NOT_PERMITTED
3647 * \retval #PSA_ERROR_INVALID_ARGUMENT
3648 * \p alg is not a key agreement algorithm
3649 * \retval #PSA_ERROR_INVALID_ARGUMENT
3650 * \p private_key is not compatible with \p alg,
3651 * or \p peer_key is not valid for \p alg or not compatible with
3652 * \p private_key.
3653 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3654 * \p output_size is too small
3655 * \retval #PSA_ERROR_NOT_SUPPORTED
3656 * \p alg is not a supported key agreement algorithm.
3657 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3658 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3659 * \retval #PSA_ERROR_HARDWARE_FAILURE
3660 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3661 * \retval #PSA_ERROR_STORAGE_FAILURE
3662 * \retval #PSA_ERROR_BAD_STATE
3663 * The library has not been previously initialized by psa_crypto_init().
3664 * It is implementation-dependent whether a failure to initialize
3665 * results in this error code.
3666 */
3667psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3668 psa_key_handle_t private_key,
3669 const uint8_t *peer_key,
3670 size_t peer_key_length,
3671 uint8_t *output,
3672 size_t output_size,
3673 size_t *output_length);
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003674
Antonio de Angelis377a1552018-11-22 17:02:40 +00003675/**@}*/
3676
3677/** \defgroup random Random generation
Antonio de Angelis14276e92018-07-10 14:35:43 +01003678 * @{
3679 */
3680
3681/**
3682 * \brief Generate random bytes.
3683 *
3684 * \warning This function **can** fail! Callers MUST check the return status
3685 * and MUST NOT use the content of the output buffer if the return
3686 * status is not #PSA_SUCCESS.
3687 *
3688 * \note To generate a key, use psa_generate_key() instead.
3689 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00003690 * \param[out] output Output buffer for the generated data.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003691 * \param output_size Number of bytes to generate and output.
3692 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00003693 * \retval #PSA_SUCCESS
3694 * \retval #PSA_ERROR_NOT_SUPPORTED
3695 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003696 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Antonio de Angelis377a1552018-11-22 17:02:40 +00003697 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3698 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003699 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003700 * \retval #PSA_ERROR_BAD_STATE
3701 * The library has not been previously initialized by psa_crypto_init().
3702 * It is implementation-dependent whether a failure to initialize
3703 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003704 */
3705psa_status_t psa_generate_random(uint8_t *output,
3706 size_t output_size);
3707
3708/**
3709 * \brief Generate a key or key pair.
3710 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003711 * The key is generated randomly.
3712 * Its location, usage policy, type and size are taken from \p attributes.
3713 *
3714 * Implementations must reject an attempt to generate a key of size 0.
3715 *
3716 * The following type-specific considerations apply:
3717 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
3718 * the public exponent is 65537.
3719 * The modulus is a product of two probabilistic primes
3720 * between 2^{n-1} and 2^n where n is the bit size specified in the
3721 * attributes.
3722 *
3723 * \param[in] attributes The attributes for the new key.
3724 * \param[out] handle On success, a handle to the newly created key.
3725 * \c 0 on failure.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003726 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00003727 * \retval #PSA_SUCCESS
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003728 * Success.
3729 * If the key is persistent, the key material and the key's metadata
3730 * have been saved to persistent storage.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003731 * \retval #PSA_ERROR_ALREADY_EXISTS
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003732 * This is an attempt to create a persistent key, and there is
3733 * already a persistent key with the given identifier.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003734 * \retval #PSA_ERROR_NOT_SUPPORTED
3735 * \retval #PSA_ERROR_INVALID_ARGUMENT
3736 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3737 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3738 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3739 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003740 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3741 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3742 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003743 * \retval #PSA_ERROR_BAD_STATE
3744 * The library has not been previously initialized by psa_crypto_init().
3745 * It is implementation-dependent whether a failure to initialize
3746 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003747 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003748psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
3749 psa_key_handle_t *handle);
Antonio de Angelis14276e92018-07-10 14:35:43 +01003750
3751/**@}*/
3752
3753#ifdef __cplusplus
3754}
3755#endif
3756
Antonio de Angelis377a1552018-11-22 17:02:40 +00003757/* The file "crypto_sizes.h" contains definitions for size calculation
3758 * macros whose definitions are implementation-specific. */
Jamie Foxcc31d402019-01-28 17:13:52 +00003759#include "psa/crypto_sizes.h"
Antonio de Angelis377a1552018-11-22 17:02:40 +00003760
Soby Mathewd7b79f22020-05-21 15:06:54 +01003761/* The file "crypto_client_struct.h" contains definitions for structures
3762 * whose definitions differ in the client view and the PSA server
3763 * implementation in TF-M. */
3764#include "psa/crypto_client_struct.h"
3765
3766
Antonio de Angelis377a1552018-11-22 17:02:40 +00003767/* The file "crypto_struct.h" contains definitions for
3768 * implementation-specific structs that are declared above. */
Jamie Foxcc31d402019-01-28 17:13:52 +00003769#include "psa/crypto_struct.h"
Antonio de Angelis377a1552018-11-22 17:02:40 +00003770
3771/* The file "crypto_extra.h" contains vendor-specific definitions. This
3772 * can include vendor-defined algorithms, extra functions, etc. */
Jamie Foxcc31d402019-01-28 17:13:52 +00003773#include "psa/crypto_extra.h"
Antonio de Angelis8908f472018-08-31 15:44:25 +01003774
Antonio de Angelis14276e92018-07-10 14:35:43 +01003775#endif /* PSA_CRYPTO_H */