blob: c4a103dc1d232bdc1aa3b02d8c3c7630ac56c090 [file] [log] [blame]
Antonio de Angelis14276e92018-07-10 14:35:43 +01001/*
Summer Qinf07cc312022-01-05 16:52:54 +08002 * Copyright (c) 2018-2022, 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
Antonio de Angelis14276e92018-07-10 14:35:43 +010027/**@}*/
28#endif /* __DOXYGEN_ONLY__ */
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
Jamie Fox0e54ebc2019-04-09 14:21:04 +010034/* The file "crypto_types.h" declares types that encode errors,
35 * algorithms, key types, policies, etc. */
Jamie Foxcc31d402019-01-28 17:13:52 +000036#include "psa/crypto_types.h"
Jamie Fox0e54ebc2019-04-09 14:21:04 +010037
Antonio de Angelis04debbd2019-10-14 12:12:52 +010038/** \defgroup version API version
39 * @{
40 */
41
42/**
43 * The major version of this implementation of the PSA Crypto API
44 */
45#define PSA_CRYPTO_API_VERSION_MAJOR 1
46
47/**
48 * The minor version of this implementation of the PSA Crypto API
49 */
50#define PSA_CRYPTO_API_VERSION_MINOR 0
51
52/**@}*/
53
Jamie Fox0e54ebc2019-04-09 14:21:04 +010054/* The file "crypto_values.h" declares macros to build and analyze values
55 * of integral types defined in "crypto_types.h". */
Jamie Foxcc31d402019-01-28 17:13:52 +000056#include "psa/crypto_values.h"
Jamie Fox0e54ebc2019-04-09 14:21:04 +010057
58/** \defgroup initialization Library initialization
Antonio de Angelis14276e92018-07-10 14:35:43 +010059 * @{
60 */
61
Antonio de Angelis14276e92018-07-10 14:35:43 +010062/**
63 * \brief Library initialization.
64 *
65 * Applications must call this function before calling any other
66 * function in this module.
67 *
68 * Applications may call this function more than once. Once a call
69 * succeeds, subsequent calls are guaranteed to succeed.
70 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +010071 * If the application calls other functions before calling psa_crypto_init(),
72 * the behavior is undefined. Implementations are encouraged to either perform
73 * the operation as if the library had been initialized or to return
74 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
75 * implementations should not return a success status if the lack of
76 * initialization may have security implications, for example due to improper
77 * seeding of the random number generator.
78 *
Antonio de Angelis377a1552018-11-22 17:02:40 +000079 * \retval #PSA_SUCCESS
80 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Maulik Patel13b27cf2021-05-14 11:44:53 +010081 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Antonio de Angelis377a1552018-11-22 17:02:40 +000082 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
83 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +010084 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Antonio de Angelis377a1552018-11-22 17:02:40 +000085 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Maulik Patel13b27cf2021-05-14 11:44:53 +010086 * \retval #PSA_ERROR_STORAGE_FAILURE
87 * \retval #PSA_ERROR_DATA_INVALID
88 * \retval #PSA_ERROR_DATA_CORRUPT
Antonio de Angelis14276e92018-07-10 14:35:43 +010089 */
90psa_status_t psa_crypto_init(void);
91
Antonio de Angelis14276e92018-07-10 14:35:43 +010092/**@}*/
93
Antonio de Angelis04debbd2019-10-14 12:12:52 +010094/** \addtogroup attributes
Antonio de Angelis14276e92018-07-10 14:35:43 +010095 * @{
96 */
97
Antonio de Angelis04debbd2019-10-14 12:12:52 +010098/** Return an initial value for a key attributes structure.
Jamie Fox0e54ebc2019-04-09 14:21:04 +010099 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100100static psa_key_attributes_t psa_key_attributes_init(void);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100101
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100102/** Declare a key as persistent and set its key identifier.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100103 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100104 * If the attribute structure currently declares the key as volatile (which
105 * is the default content of an attribute structure), this function sets
106 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000107 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100108 * This function does not access storage, it merely stores the given
109 * value in the structure.
110 * The persistent key will be written to storage when the attribute
111 * structure is passed to a key creation function such as
112 * psa_import_key(), psa_generate_key(),
113 * psa_key_derivation_output_key() or psa_copy_key().
114 *
115 * This function may be declared as `static` (i.e. without external
116 * linkage). This function may be provided as a function-like macro,
117 * but in this case it must evaluate each of its arguments exactly once.
118 *
Maulik Patel28659c42021-01-06 14:09:22 +0000119 * \param[out] attributes The attribute structure to write to.
120 * \param key The persistent identifier for the key.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100121 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100122static void psa_set_key_id(psa_key_attributes_t *attributes,
Maulik Patel28659c42021-01-06 14:09:22 +0000123 psa_key_id_t key);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100124
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100125/** Set the location of a persistent key.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000126 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100127 * To make a key persistent, you must give it a persistent key identifier
128 * with psa_set_key_id(). By default, a key that has a persistent identifier
129 * is stored in the default storage area identifier by
130 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
131 * area, or to explicitly declare the key as volatile.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000132 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100133 * This function does not access storage, it merely stores the given
134 * value in the structure.
135 * The persistent key will be written to storage when the attribute
136 * structure is passed to a key creation function such as
137 * psa_import_key(), psa_generate_key(),
138 * psa_key_derivation_output_key() or psa_copy_key().
139 *
140 * This function may be declared as `static` (i.e. without external
141 * linkage). This function may be provided as a function-like macro,
142 * but in this case it must evaluate each of its arguments exactly once.
143 *
144 * \param[out] attributes The attribute structure to write to.
145 * \param lifetime The lifetime for the key.
146 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
147 * key will be volatile, and the key identifier
148 * attribute is reset to 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000149 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100150static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
151 psa_key_lifetime_t lifetime);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100152
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100153/** Retrieve the key identifier from key attributes.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000154 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100155 * This function may be declared as `static` (i.e. without external
156 * linkage). This function may be provided as a function-like macro,
157 * but in this case it must evaluate its argument exactly once.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000158 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100159 * \param[in] attributes The key attribute structure to query.
160 *
161 * \return The persistent identifier stored in the attribute structure.
162 * This value is unspecified if the attribute structure declares
163 * the key as volatile.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000164 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100165static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100166
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100167/** Retrieve the lifetime from key attributes.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100168 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100169 * This function may be declared as `static` (i.e. without external
170 * linkage). This function may be provided as a function-like macro,
171 * but in this case it must evaluate its argument exactly once.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100172 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100173 * \param[in] attributes The key attribute structure to query.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000174 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100175 * \return The lifetime value stored in the attribute structure.
176 */
177static psa_key_lifetime_t psa_get_key_lifetime(
178 const psa_key_attributes_t *attributes);
179
180/** Declare usage flags for a key.
181 *
182 * Usage flags are part of a key's usage policy. They encode what
183 * kind of operations are permitted on the key. For more details,
184 * refer to the documentation of the type #psa_key_usage_t.
185 *
186 * This function overwrites any usage flags
187 * previously set in \p attributes.
188 *
189 * This function may be declared as `static` (i.e. without external
190 * linkage). This function may be provided as a function-like macro,
191 * but in this case it must evaluate each of its arguments exactly once.
192 *
193 * \param[out] attributes The attribute structure to write to.
194 * \param usage_flags The usage flags to write.
195 */
196static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
197 psa_key_usage_t usage_flags);
198
199/** Retrieve the usage flags from key attributes.
200 *
201 * This function may be declared as `static` (i.e. without external
202 * linkage). This function may be provided as a function-like macro,
203 * but in this case it must evaluate its argument exactly once.
204 *
205 * \param[in] attributes The key attribute structure to query.
206 *
207 * \return The usage flags stored in the attribute structure.
208 */
209static psa_key_usage_t psa_get_key_usage_flags(
210 const psa_key_attributes_t *attributes);
211
212/** Declare the permitted algorithm policy for a key.
213 *
214 * The permitted algorithm policy of a key encodes which algorithm or
215 * algorithms are permitted to be used with this key. The following
216 * algorithm policies are supported:
217 * - 0 does not allow any cryptographic operation with the key. The key
218 * may be used for non-cryptographic actions such as exporting (if
219 * permitted by the usage flags).
220 * - An algorithm value permits this particular algorithm.
221 * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
222 * signature scheme with any hash algorithm.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100223 * - An algorithm built from #PSA_ALG_AT_LEAST_THIS_LENGTH_MAC allows
224 * any MAC algorithm from the same base class (e.g. CMAC) which
225 * generates/verifies a MAC length greater than or equal to the length
226 * encoded in the wildcard algorithm.
227 * - An algorithm built from #PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG
228 * allows any AEAD algorithm from the same base class (e.g. CCM) which
229 * generates/verifies a tag length greater than or equal to the length
230 * encoded in the wildcard algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100231 *
232 * This function overwrites any algorithm policy
233 * previously set in \p attributes.
234 *
235 * This function may be declared as `static` (i.e. without external
236 * linkage). This function may be provided as a function-like macro,
237 * but in this case it must evaluate each of its arguments exactly once.
238 *
239 * \param[out] attributes The attribute structure to write to.
240 * \param alg The permitted algorithm policy to write.
241 */
242static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
243 psa_algorithm_t alg);
244
245
246/** Retrieve the algorithm policy from key attributes.
247 *
248 * This function may be declared as `static` (i.e. without external
249 * linkage). This function may be provided as a function-like macro,
250 * but in this case it must evaluate its argument exactly once.
251 *
252 * \param[in] attributes The key attribute structure to query.
253 *
254 * \return The algorithm stored in the attribute structure.
255 */
256static psa_algorithm_t psa_get_key_algorithm(
257 const psa_key_attributes_t *attributes);
258
259/** Declare the type of a key.
260 *
261 * This function overwrites any key type
262 * previously set in \p attributes.
263 *
264 * This function may be declared as `static` (i.e. without external
265 * linkage). This function may be provided as a function-like macro,
266 * but in this case it must evaluate each of its arguments exactly once.
267 *
268 * \param[out] attributes The attribute structure to write to.
269 * \param type The key type to write.
270 * If this is 0, the key type in \p attributes
271 * becomes unspecified.
272 */
273static void psa_set_key_type(psa_key_attributes_t *attributes,
274 psa_key_type_t type);
275
276
277/** Declare the size of a key.
278 *
279 * This function overwrites any key size previously set in \p attributes.
280 *
281 * This function may be declared as `static` (i.e. without external
282 * linkage). This function may be provided as a function-like macro,
283 * but in this case it must evaluate each of its arguments exactly once.
284 *
285 * \param[out] attributes The attribute structure to write to.
286 * \param bits The key size in bits.
287 * If this is 0, the key size in \p attributes
288 * becomes unspecified. Keys of size 0 are
289 * not supported.
290 */
291static void psa_set_key_bits(psa_key_attributes_t *attributes,
292 size_t bits);
293
294/** Retrieve the key type from key attributes.
295 *
296 * This function may be declared as `static` (i.e. without external
297 * linkage). This function may be provided as a function-like macro,
298 * but in this case it must evaluate its argument exactly once.
299 *
300 * \param[in] attributes The key attribute structure to query.
301 *
302 * \return The key type stored in the attribute structure.
303 */
304static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
305
306/** Retrieve the key size from key attributes.
307 *
308 * This function may be declared as `static` (i.e. without external
309 * linkage). This function may be provided as a function-like macro,
310 * but in this case it must evaluate its argument exactly once.
311 *
312 * \param[in] attributes The key attribute structure to query.
313 *
314 * \return The key size stored in the attribute structure, in bits.
315 */
316static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
317
318/** Retrieve the attributes of a key.
319 *
320 * This function first resets the attribute structure as with
321 * psa_reset_key_attributes(). It then copies the attributes of
322 * the given key into the given attribute structure.
323 *
324 * \note This function may allocate memory or other resources.
325 * Once you have called this function on an attribute structure,
326 * you must call psa_reset_key_attributes() to free these resources.
327 *
Maulik Patel28659c42021-01-06 14:09:22 +0000328 * \param[in] key Identifier of the key to query.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100329 * \param[in,out] attributes On success, the attributes of the key.
330 * On failure, equivalent to a
331 * freshly-initialized structure.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000332 *
333 * \retval #PSA_SUCCESS
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100334 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100335 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Antonio de Angelis377a1552018-11-22 17:02:40 +0000336 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100337 * \retval #PSA_ERROR_CORRUPTION_DETECTED
338 * \retval #PSA_ERROR_STORAGE_FAILURE
Maulik Patel13b27cf2021-05-14 11:44:53 +0100339 * \retval #PSA_ERROR_DATA_CORRUPT
340 * \retval #PSA_ERROR_DATA_INVALID
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100341 * \retval #PSA_ERROR_BAD_STATE
342 * The library has not been previously initialized by psa_crypto_init().
343 * It is implementation-dependent whether a failure to initialize
344 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100345 */
Maulik Patel28659c42021-01-06 14:09:22 +0000346psa_status_t psa_get_key_attributes(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100347 psa_key_attributes_t *attributes);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100348
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100349/** Reset a key attribute structure to a freshly initialized state.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000350 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100351 * You must initialize the attribute structure as described in the
352 * documentation of the type #psa_key_attributes_t before calling this
353 * function. Once the structure has been initialized, you may call this
354 * function at any time.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000355 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100356 * This function frees any auxiliary resources that the structure
357 * may contain.
358 *
359 * \param[in,out] attributes The attribute structure to reset.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000360 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100361void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100362
363/**@}*/
364
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100365/** \defgroup key_management Key management
Antonio de Angelis14276e92018-07-10 14:35:43 +0100366 * @{
367 */
368
Maulik Patel28659c42021-01-06 14:09:22 +0000369/** Remove non-essential copies of key material from memory.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100370 *
Maulik Patel28659c42021-01-06 14:09:22 +0000371 * If the key identifier designates a volatile key, this functions does not do
372 * anything and returns successfully.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100373 *
Maulik Patel28659c42021-01-06 14:09:22 +0000374 * If the key identifier designates a persistent key, then this function will
375 * free all resources associated with the key in volatile memory. The key
376 * data in persistent storage is not affected and the key can still be used.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100377 *
Maulik Patel28659c42021-01-06 14:09:22 +0000378 * \param key Identifier of the key to purge.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100379 *
380 * \retval #PSA_SUCCESS
Maulik Patel28659c42021-01-06 14:09:22 +0000381 * The key material will have been removed from memory if it is not
382 * currently required.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100383 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +0000384 * \p key is not a valid key identifier.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100385 * \retval #PSA_ERROR_BAD_STATE
386 * The library has not been previously initialized by psa_crypto_init().
387 * It is implementation-dependent whether a failure to initialize
388 * results in this error code.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100389 */
Maulik Patel28659c42021-01-06 14:09:22 +0000390psa_status_t psa_purge_key(psa_key_id_t key);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100391
392/** Make a copy of a key.
393 *
394 * Copy key material from one location to another.
395 *
396 * This function is primarily useful to copy a key from one location
397 * to another, since it populates a key using the material from
398 * another key which may have a different lifetime.
399 *
400 * This function may be used to share a key with a different party,
401 * subject to implementation-defined restrictions on key sharing.
402 *
403 * The policy on the source key must have the usage flag
404 * #PSA_KEY_USAGE_COPY set.
405 * This flag is sufficient to permit the copy if the key has the lifetime
406 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
407 * Some secure elements do not provide a way to copy a key without
408 * making it extractable from the secure element. If a key is located
409 * in such a secure element, then the key must have both usage flags
410 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
411 * a copy of the key outside the secure element.
412 *
413 * The resulting key may only be used in a way that conforms to
414 * both the policy of the original key and the policy specified in
415 * the \p attributes parameter:
416 * - The usage flags on the resulting key are the bitwise-and of the
417 * usage flags on the source policy and the usage flags in \p attributes.
418 * - If both allow the same algorithm or wildcard-based
419 * algorithm policy, the resulting key has the same algorithm policy.
420 * - If either of the policies allows an algorithm and the other policy
421 * allows a wildcard-based algorithm policy that includes this algorithm,
422 * the resulting key allows the same algorithm.
423 * - If the policies do not allow any algorithm in common, this function
424 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
425 *
426 * The effect of this function on implementation-defined attributes is
427 * implementation-defined.
428 *
Maulik Patel28659c42021-01-06 14:09:22 +0000429 * \param source_key The key to copy. It must allow the usage
430 * #PSA_KEY_USAGE_COPY. If a private or secret key is
431 * being copied outside of a secure element it must
432 * also allow #PSA_KEY_USAGE_EXPORT.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100433 * \param[in] attributes The attributes for the new key.
434 * They are used as follows:
435 * - The key type and size may be 0. If either is
436 * nonzero, it must match the corresponding
437 * attribute of the source key.
438 * - The key location (the lifetime and, for
439 * persistent keys, the key identifier) is
440 * used directly.
441 * - The policy constraints (usage flags and
442 * algorithm policy) are combined from
443 * the source key and \p attributes so that
444 * both sets of restrictions apply, as
445 * described in the documentation of this function.
Maulik Patel28659c42021-01-06 14:09:22 +0000446 * \param[out] target_key On success, an identifier for the newly created
447 * key. For persistent keys, this is the key
448 * identifier defined in \p attributes.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100449 * \c 0 on failure.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100450 *
451 * \retval #PSA_SUCCESS
452 * \retval #PSA_ERROR_INVALID_HANDLE
Maulik Patel28659c42021-01-06 14:09:22 +0000453 * \p source_key is invalid.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100454 * \retval #PSA_ERROR_ALREADY_EXISTS
455 * This is an attempt to create a persistent key, and there is
456 * already a persistent key with the given identifier.
457 * \retval #PSA_ERROR_INVALID_ARGUMENT
458 * The lifetime or identifier in \p attributes are invalid.
459 * \retval #PSA_ERROR_INVALID_ARGUMENT
460 * The policy constraints on the source and specified in
461 * \p attributes are incompatible.
462 * \retval #PSA_ERROR_INVALID_ARGUMENT
463 * \p attributes specifies a key type or key size
464 * which does not match the attributes of the source key.
465 * \retval #PSA_ERROR_NOT_PERMITTED
466 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
467 * \retval #PSA_ERROR_NOT_PERMITTED
468 * The source key is not exportable and its lifetime does not
469 * allow copying it to the target's lifetime.
470 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
471 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100472 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100473 * \retval #PSA_ERROR_HARDWARE_FAILURE
Maulik Patel13b27cf2021-05-14 11:44:53 +0100474 * \retval #PSA_ERROR_DATA_INVALID
475 * \retval #PSA_ERROR_DATA_CORRUPT
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100476 * \retval #PSA_ERROR_STORAGE_FAILURE
477 * \retval #PSA_ERROR_CORRUPTION_DETECTED
478 * \retval #PSA_ERROR_BAD_STATE
479 * The library has not been previously initialized by psa_crypto_init().
480 * It is implementation-dependent whether a failure to initialize
481 * results in this error code.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100482 */
Maulik Patel28659c42021-01-06 14:09:22 +0000483psa_status_t psa_copy_key(psa_key_id_t source_key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100484 const psa_key_attributes_t *attributes,
Maulik Patel28659c42021-01-06 14:09:22 +0000485 psa_key_id_t *target_key);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100486
487
488/**
489 * \brief Destroy a key.
490 *
491 * This function destroys a key from both volatile
492 * memory and, if applicable, non-volatile storage. Implementations shall
493 * make a best effort to ensure that that the key material cannot be recovered.
494 *
495 * This function also erases any metadata such as policies and frees
Maulik Patel28659c42021-01-06 14:09:22 +0000496 * resources associated with the key.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100497 *
498 * If a key is currently in use in a multipart operation, then destroying the
499 * key will cause the multipart operation to fail.
500 *
Maulik Patel28659c42021-01-06 14:09:22 +0000501 * \param key Identifier of the key to erase. If this is \c 0, do nothing and
502 * return #PSA_SUCCESS.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100503 *
504 * \retval #PSA_SUCCESS
Maulik Patel28659c42021-01-06 14:09:22 +0000505 * \p key was a valid identifier and the key material that it
506 * referred to has been erased. Alternatively, \p key is \c 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100507 * \retval #PSA_ERROR_NOT_PERMITTED
508 * The key cannot be erased because it is
509 * read-only, either due to a policy or due to physical restrictions.
510 * \retval #PSA_ERROR_INVALID_HANDLE
Maulik Patel28659c42021-01-06 14:09:22 +0000511 * \p key is not a valid identifier nor \c 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100512 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
513 * There was an failure in communication with the cryptoprocessor.
514 * The key material may still be present in the cryptoprocessor.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100515 * \retval #PSA_ERROR_DATA_INVALID
516 * This error is typically a result of either storage corruption on a
517 * cleartext storage backend, or an attempt to read data that was
518 * written by an incompatible version of the library.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100519 * \retval #PSA_ERROR_STORAGE_FAILURE
520 * The storage is corrupted. Implementations shall make a best effort
521 * to erase key material even in this stage, however applications
522 * should be aware that it may be impossible to guarantee that the
523 * key material is not recoverable in such cases.
524 * \retval #PSA_ERROR_CORRUPTION_DETECTED
525 * An unexpected condition which is not a storage corruption or
526 * a communication failure occurred. The cryptoprocessor may have
527 * been compromised.
528 * \retval #PSA_ERROR_BAD_STATE
529 * The library has not been previously initialized by psa_crypto_init().
530 * It is implementation-dependent whether a failure to initialize
531 * results in this error code.
532 */
Maulik Patel28659c42021-01-06 14:09:22 +0000533psa_status_t psa_destroy_key(psa_key_id_t key);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100534
535/**@}*/
536
537/** \defgroup import_export Key import and export
538 * @{
539 */
540
541/**
542 * \brief Import a key in binary format.
543 *
544 * This function supports any output from psa_export_key(). Refer to the
545 * documentation of psa_export_public_key() for the format of public keys
546 * and to the documentation of psa_export_key() for the format for
547 * other key types.
548 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100549 * The key data determines the key size. The attributes may optionally
550 * specify a key size; in this case it must match the size determined
551 * from the key data. A key size of 0 in \p attributes indicates that
552 * the key size is solely determined by the key data.
553 *
554 * Implementations must reject an attempt to import a key of size 0.
555 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100556 * This specification supports a single format for each key type.
557 * Implementations may support other formats as long as the standard
558 * format is supported. Implementations that support other formats
559 * should ensure that the formats are clearly unambiguous so as to
560 * minimize the risk that an invalid input is accidentally interpreted
561 * according to a different format.
562 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100563 * \param[in] attributes The attributes for the new key.
564 * The key size is always determined from the
565 * \p data buffer.
566 * If the key size in \p attributes is nonzero,
567 * it must be equal to the size from \p data.
Maulik Patel28659c42021-01-06 14:09:22 +0000568 * \param[out] key On success, an identifier to the newly created key.
569 * For persistent keys, this is the key identifier
570 * defined in \p attributes.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100571 * \c 0 on failure.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100572 * \param[in] data Buffer containing the key data. The content of this
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100573 * buffer is interpreted according to the type declared
574 * in \p attributes.
575 * All implementations must support at least the format
576 * described in the documentation
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100577 * of psa_export_key() or psa_export_public_key() for
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100578 * the chosen type. Implementations may allow other
579 * formats, but should be conservative: implementations
580 * should err on the side of rejecting content if it
581 * may be erroneous (e.g. wrong type or truncated data).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100582 * \param data_length Size of the \p data buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100583 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000584 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +0100585 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100586 * If the key is persistent, the key material and the key's metadata
587 * have been saved to persistent storage.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100588 * \retval #PSA_ERROR_ALREADY_EXISTS
589 * This is an attempt to create a persistent key, and there is
590 * already a persistent key with the given identifier.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100591 * \retval #PSA_ERROR_NOT_SUPPORTED
592 * The key type or key size is not supported, either by the
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100593 * implementation in general or in this particular persistent location.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000594 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100595 * The key attributes, as a whole, are invalid.
596 * \retval #PSA_ERROR_INVALID_ARGUMENT
597 * The key data is not correctly formatted.
598 * \retval #PSA_ERROR_INVALID_ARGUMENT
599 * The size in \p attributes is nonzero and does not match the size
600 * of the key data.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100601 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
602 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
603 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Maulik Patel13b27cf2021-05-14 11:44:53 +0100604 * \retval #PSA_ERROR_DATA_CORRUPT
605 * \retval #PSA_ERROR_DATA_INVALID
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100606 * \retval #PSA_ERROR_STORAGE_FAILURE
607 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100608 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100609 * \retval #PSA_ERROR_BAD_STATE
610 * The library has not been previously initialized by psa_crypto_init().
611 * It is implementation-dependent whether a failure to initialize
612 * results in this error code.
613 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100614psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100615 const uint8_t *data,
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100616 size_t data_length,
Maulik Patel28659c42021-01-06 14:09:22 +0000617 psa_key_id_t *key);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100618
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100619
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100620
621/**
622 * \brief Export a key in binary format.
623 *
624 * The output of this function can be passed to psa_import_key() to
625 * create an equivalent object.
626 *
627 * If the implementation of psa_import_key() supports other formats
628 * beyond the format specified here, the output from psa_export_key()
629 * must use the representation specified here, not the original
630 * representation.
631 *
632 * For standard key types, the output format is as follows:
633 *
634 * - For symmetric keys (including MAC keys), the format is the
635 * raw bytes of the key.
636 * - For DES, the key data consists of 8 bytes. The parity bits must be
637 * correct.
638 * - For Triple-DES, the format is the concatenation of the
639 * two or three DES keys.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100640 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100641 * is the non-encrypted DER encoding of the representation defined by
642 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
643 * ```
644 * RSAPrivateKey ::= SEQUENCE {
645 * version INTEGER, -- must be 0
646 * modulus INTEGER, -- n
647 * publicExponent INTEGER, -- e
648 * privateExponent INTEGER, -- d
649 * prime1 INTEGER, -- p
650 * prime2 INTEGER, -- q
651 * exponent1 INTEGER, -- d mod (p-1)
652 * exponent2 INTEGER, -- d mod (q-1)
653 * coefficient INTEGER, -- (inverse of q) mod p
654 * }
655 * ```
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100656 * - For elliptic curve key pairs (key types for which
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100657 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100658 * a representation of the private value as a `ceiling(m/8)`-byte string
659 * where `m` is the bit size associated with the curve, i.e. the bit size
660 * of the order of the curve's coordinate field. This byte string is
661 * in little-endian order for Montgomery curves (curve types
Summer Qin0e5b2e02020-10-22 11:23:39 +0800662 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
663 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
664 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
665 * For Weierstrass curves, this is the content of the `privateKey` field of
666 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
667 * the format is defined by RFC 7748, and output is masked according to §5.
Summer Qin359167d2021-07-05 18:11:50 +0800668 * For twisted Edwards curves, the private key is as defined by RFC 8032
669 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100670 * - For Diffie-Hellman key exchange key pairs (key types for which
671 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
672 * format is the representation of the private key `x` as a big-endian byte
673 * string. The length of the byte string is the private key size in bytes
674 * (leading zeroes are not stripped).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100675 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
676 * true), the format is the same as for psa_export_public_key().
677 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100678 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
679 *
Maulik Patel28659c42021-01-06 14:09:22 +0000680 * \param key Identifier of the key to export. It must allow the
681 * usage #PSA_KEY_USAGE_EXPORT, unless it is a public
682 * key.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100683 * \param[out] data Buffer where the key data is to be written.
684 * \param data_size Size of the \p data buffer in bytes.
685 * \param[out] data_length On success, the number of bytes
686 * that make up the key data.
687 *
688 * \retval #PSA_SUCCESS
689 * \retval #PSA_ERROR_INVALID_HANDLE
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100690 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100691 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000692 * \retval #PSA_ERROR_NOT_SUPPORTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100693 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
694 * The size of the \p data buffer is too small. You can determine a
695 * sufficient buffer size by calling
Maulik Patel13b27cf2021-05-14 11:44:53 +0100696 * #PSA_EXPORT_KEY_OUTPUT_SIZE(\c type, \c bits)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100697 * where \c type is the key type
698 * and \c bits is the key size in bits.
699 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
700 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100701 * \retval #PSA_ERROR_CORRUPTION_DETECTED
702 * \retval #PSA_ERROR_STORAGE_FAILURE
703 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100704 * \retval #PSA_ERROR_BAD_STATE
705 * The library has not been previously initialized by psa_crypto_init().
706 * It is implementation-dependent whether a failure to initialize
707 * results in this error code.
708 */
Maulik Patel28659c42021-01-06 14:09:22 +0000709psa_status_t psa_export_key(psa_key_id_t key,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100710 uint8_t *data,
711 size_t data_size,
712 size_t *data_length);
713
714/**
715 * \brief Export a public key or the public part of a key pair in binary format.
716 *
717 * The output of this function can be passed to psa_import_key() to
718 * create an object that is equivalent to the public key.
719 *
720 * This specification supports a single format for each key type.
721 * Implementations may support other formats as long as the standard
722 * format is supported. Implementations that support other formats
723 * should ensure that the formats are clearly unambiguous so as to
724 * minimize the risk that an invalid input is accidentally interpreted
725 * according to a different format.
726 *
727 * For standard key types, the output format is as follows:
728 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
729 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
730 * ```
731 * RSAPublicKey ::= SEQUENCE {
732 * modulus INTEGER, -- n
733 * publicExponent INTEGER } -- e
734 * ```
Summer Qin359167d2021-07-05 18:11:50 +0800735 * - For elliptic curve keys on a twisted Edwards curve (key types for which
736 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true and #PSA_KEY_TYPE_ECC_GET_FAMILY
737 * returns #PSA_ECC_FAMILY_TWISTED_EDWARDS), the public key is as defined
738 * by RFC 8032
739 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
740 * - For other elliptic curve public keys (key types for which
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100741 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100742 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100743 * Let `m` be the bit size associated with the curve, i.e. the bit size of
744 * `q` for a curve over `F_q`. The representation consists of:
745 * - The byte 0x04;
746 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
747 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100748 * - For Diffie-Hellman key exchange public keys (key types for which
749 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
750 * the format is the representation of the public key `y = g^x mod p` as a
751 * big-endian byte string. The length of the byte string is the length of the
752 * base prime `p` in bytes.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100753 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100754 * Exporting a public key object or the public part of a key pair is
755 * always permitted, regardless of the key's usage flags.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100756 *
Maulik Patel28659c42021-01-06 14:09:22 +0000757 * \param key Identifier of the key to export.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100758 * \param[out] data Buffer where the key data is to be written.
759 * \param data_size Size of the \p data buffer in bytes.
760 * \param[out] data_length On success, the number of bytes
761 * that make up the key data.
762 *
763 * \retval #PSA_SUCCESS
764 * \retval #PSA_ERROR_INVALID_HANDLE
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100765 * \retval #PSA_ERROR_INVALID_ARGUMENT
766 * The key is neither a public key nor a key pair.
767 * \retval #PSA_ERROR_NOT_SUPPORTED
768 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
769 * The size of the \p data buffer is too small. You can determine a
770 * sufficient buffer size by calling
Maulik Patel13b27cf2021-05-14 11:44:53 +0100771 * #PSA_EXPORT_KEY_OUTPUT_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100772 * where \c type is the key type
773 * and \c bits is the key size in bits.
774 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
775 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100776 * \retval #PSA_ERROR_CORRUPTION_DETECTED
777 * \retval #PSA_ERROR_STORAGE_FAILURE
778 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100779 * \retval #PSA_ERROR_BAD_STATE
780 * The library has not been previously initialized by psa_crypto_init().
781 * It is implementation-dependent whether a failure to initialize
782 * results in this error code.
783 */
Maulik Patel28659c42021-01-06 14:09:22 +0000784psa_status_t psa_export_public_key(psa_key_id_t key,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100785 uint8_t *data,
786 size_t data_size,
787 size_t *data_length);
788
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100789
Antonio de Angelis14276e92018-07-10 14:35:43 +0100790
791/**@}*/
792
793/** \defgroup hash Message digests
794 * @{
795 */
796
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100797/** Calculate the hash (digest) of a message.
798 *
799 * \note To verify the hash of a message against an
800 * expected value, use psa_hash_compare() instead.
801 *
802 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
803 * such that #PSA_ALG_IS_HASH(\p alg) is true).
804 * \param[in] input Buffer containing the message to hash.
805 * \param input_length Size of the \p input buffer in bytes.
806 * \param[out] hash Buffer where the hash is to be written.
807 * \param hash_size Size of the \p hash buffer in bytes.
808 * \param[out] hash_length On success, the number of bytes
809 * that make up the hash value. This is always
Maulik Patel13b27cf2021-05-14 11:44:53 +0100810 * #PSA_HASH_LENGTH(\p alg).
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100811 *
812 * \retval #PSA_SUCCESS
813 * Success.
814 * \retval #PSA_ERROR_NOT_SUPPORTED
815 * \p alg is not supported or is not a hash algorithm.
816 * \retval #PSA_ERROR_INVALID_ARGUMENT
817 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
818 * \p hash_size is too small
819 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
820 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
821 * \retval #PSA_ERROR_HARDWARE_FAILURE
822 * \retval #PSA_ERROR_CORRUPTION_DETECTED
823 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
824 * \retval #PSA_ERROR_BAD_STATE
825 * The library has not been previously initialized by psa_crypto_init().
826 * It is implementation-dependent whether a failure to initialize
827 * results in this error code.
828 */
829psa_status_t psa_hash_compute(psa_algorithm_t alg,
830 const uint8_t *input,
831 size_t input_length,
832 uint8_t *hash,
833 size_t hash_size,
834 size_t *hash_length);
835
836/** Calculate the hash (digest) of a message and compare it with a
837 * reference value.
838 *
839 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
840 * such that #PSA_ALG_IS_HASH(\p alg) is true).
841 * \param[in] input Buffer containing the message to hash.
842 * \param input_length Size of the \p input buffer in bytes.
843 * \param[out] hash Buffer containing the expected hash value.
844 * \param hash_length Size of the \p hash buffer in bytes.
845 *
846 * \retval #PSA_SUCCESS
847 * The expected hash is identical to the actual hash of the input.
848 * \retval #PSA_ERROR_INVALID_SIGNATURE
849 * The hash of the message was calculated successfully, but it
850 * differs from the expected hash.
851 * \retval #PSA_ERROR_NOT_SUPPORTED
852 * \p alg is not supported or is not a hash algorithm.
853 * \retval #PSA_ERROR_INVALID_ARGUMENT
854 * \p input_length or \p hash_length do not match the hash size for \p alg
855 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
856 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
857 * \retval #PSA_ERROR_HARDWARE_FAILURE
858 * \retval #PSA_ERROR_CORRUPTION_DETECTED
859 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
860 * \retval #PSA_ERROR_BAD_STATE
861 * The library has not been previously initialized by psa_crypto_init().
862 * It is implementation-dependent whether a failure to initialize
863 * results in this error code.
864 */
865psa_status_t psa_hash_compare(psa_algorithm_t alg,
866 const uint8_t *input,
867 size_t input_length,
868 const uint8_t *hash,
Soby Mathew07ef6e42020-07-20 21:09:23 +0100869 size_t hash_length);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100870
Antonio de Angelis377a1552018-11-22 17:02:40 +0000871/** The type of the state data structure for multipart hash operations.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100872 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100873 * Before calling any function on a hash operation object, the application must
874 * initialize it by any of the following means:
875 * - Set the structure to all-bits-zero, for example:
876 * \code
877 * psa_hash_operation_t operation;
878 * memset(&operation, 0, sizeof(operation));
879 * \endcode
880 * - Initialize the structure to logical zero values, for example:
881 * \code
882 * psa_hash_operation_t operation = {0};
883 * \endcode
884 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
885 * for example:
886 * \code
887 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
888 * \endcode
889 * - Assign the result of the function psa_hash_operation_init()
890 * to the structure, for example:
891 * \code
892 * psa_hash_operation_t operation;
893 * operation = psa_hash_operation_init();
894 * \endcode
895 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000896 * This is an implementation-defined \c struct. Applications should not
Summer Qin359167d2021-07-05 18:11:50 +0800897 * make any assumptions about the content of this structure.
898 * Implementation details can change in future versions without notice. */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000899typedef struct psa_hash_operation_s psa_hash_operation_t;
Antonio de Angelis14276e92018-07-10 14:35:43 +0100900
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100901/** Return an initial value for a hash operation object.
902 */
903static psa_hash_operation_t psa_hash_operation_init(void);
904
905/** Set up a multipart hash operation.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100906 *
907 * The sequence of operations to calculate a hash (message digest)
908 * is as follows:
909 * -# Allocate an operation object which will be passed to all the functions
910 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100911 * -# Initialize the operation object with one of the methods described in the
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100912 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000913 * -# Call psa_hash_setup() to specify the algorithm.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100914 * -# Call psa_hash_update() zero, one or more times, passing a fragment
915 * of the message each time. The hash that is calculated is the hash
916 * of the concatenation of these messages in order.
917 * -# To calculate the hash, call psa_hash_finish().
918 * To compare the hash with an expected value, call psa_hash_verify().
919 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100920 * If an error occurs at any step after a call to psa_hash_setup(), the
921 * operation will need to be reset by a call to psa_hash_abort(). The
922 * application may call psa_hash_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100923 * has been initialized.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100924 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000925 * After a successful call to psa_hash_setup(), the application must
Antonio de Angelis14276e92018-07-10 14:35:43 +0100926 * eventually terminate the operation. The following events terminate an
927 * operation:
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100928 * - A successful call to psa_hash_finish() or psa_hash_verify().
929 * - A call to psa_hash_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +0100930 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100931 * \param[in,out] operation The operation object to set up. It must have
932 * been initialized as per the documentation for
933 * #psa_hash_operation_t and not yet in use.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000934 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
935 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +0100936 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000937 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +0100938 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000939 * \retval #PSA_ERROR_NOT_SUPPORTED
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100940 * \p alg is not a supported hash algorithm.
941 * \retval #PSA_ERROR_INVALID_ARGUMENT
942 * \p alg is not a hash algorithm.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100943 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100944 * The operation state is not valid (it must be inactive).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000945 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
946 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
947 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100948 * \retval #PSA_ERROR_CORRUPTION_DETECTED
949 * \retval #PSA_ERROR_BAD_STATE
950 * The library has not been previously initialized by psa_crypto_init().
951 * It is implementation-dependent whether a failure to initialize
952 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100953 */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000954psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Antonio de Angelis14276e92018-07-10 14:35:43 +0100955 psa_algorithm_t alg);
956
957/** Add a message fragment to a multipart hash operation.
958 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000959 * The application must call psa_hash_setup() before calling this function.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100960 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100961 * If this function returns an error status, the operation enters an error
962 * state and must be aborted by calling psa_hash_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +0100963 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000964 * \param[in,out] operation Active hash operation.
965 * \param[in] input Buffer containing the message fragment to hash.
966 * \param input_length Size of the \p input buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100967 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000968 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +0100969 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000970 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100971 * The operation state is not valid (it muct be active).
Antonio de Angelis377a1552018-11-22 17:02:40 +0000972 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
973 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
974 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100975 * \retval #PSA_ERROR_CORRUPTION_DETECTED
976 * \retval #PSA_ERROR_BAD_STATE
977 * The library has not been previously initialized by psa_crypto_init().
978 * It is implementation-dependent whether a failure to initialize
979 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100980 */
981psa_status_t psa_hash_update(psa_hash_operation_t *operation,
982 const uint8_t *input,
983 size_t input_length);
984
985/** Finish the calculation of the hash of a message.
986 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000987 * The application must call psa_hash_setup() before calling this function.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100988 * This function calculates the hash of the message formed by concatenating
989 * the inputs passed to preceding calls to psa_hash_update().
990 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100991 * When this function returns successfuly, the operation becomes inactive.
992 * If this function returns an error status, the operation enters an error
993 * state and must be aborted by calling psa_hash_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +0100994 *
995 * \warning Applications should not call this function if they expect
996 * a specific value for the hash. Call psa_hash_verify() instead.
997 * Beware that comparing integrity or authenticity data such as
998 * hash values with a function such as \c memcmp is risky
999 * because the time taken by the comparison may leak information
1000 * about the hashed data which could allow an attacker to guess
1001 * a valid hash and thereby bypass security controls.
1002 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001003 * \param[in,out] operation Active hash operation.
1004 * \param[out] hash Buffer where the hash is to be written.
1005 * \param hash_size Size of the \p hash buffer in bytes.
1006 * \param[out] hash_length On success, the number of bytes
1007 * that make up the hash value. This is always
Maulik Patel13b27cf2021-05-14 11:44:53 +01001008 * #PSA_HASH_LENGTH(\c alg) where \c alg is the
Antonio de Angelis377a1552018-11-22 17:02:40 +00001009 * hash algorithm that is calculated.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001010 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001011 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001012 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001013 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001014 * The operation state is not valid (it must be active).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001015 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1016 * The size of the \p hash buffer is too small. You can determine a
Maulik Patel13b27cf2021-05-14 11:44:53 +01001017 * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01001018 * where \c alg is the hash algorithm that is calculated.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001019 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1020 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1021 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001022 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1023 * \retval #PSA_ERROR_BAD_STATE
1024 * The library has not been previously initialized by psa_crypto_init().
1025 * It is implementation-dependent whether a failure to initialize
1026 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001027 */
1028psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1029 uint8_t *hash,
1030 size_t hash_size,
1031 size_t *hash_length);
1032
1033/** Finish the calculation of the hash of a message and compare it with
1034 * an expected value.
1035 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001036 * The application must call psa_hash_setup() before calling this function.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001037 * This function calculates the hash of the message formed by concatenating
1038 * the inputs passed to preceding calls to psa_hash_update(). It then
1039 * compares the calculated hash with the expected hash passed as a
1040 * parameter to this function.
1041 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001042 * When this function returns successfuly, the operation becomes inactive.
1043 * If this function returns an error status, the operation enters an error
1044 * state and must be aborted by calling psa_hash_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001045 *
1046 * \note Implementations shall make the best effort to ensure that the
1047 * comparison between the actual hash and the expected hash is performed
1048 * in constant time.
1049 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001050 * \param[in,out] operation Active hash operation.
1051 * \param[in] hash Buffer containing the expected hash value.
1052 * \param hash_length Size of the \p hash buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001053 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001054 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001055 * The expected hash is identical to the actual hash of the message.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001056 * \retval #PSA_ERROR_INVALID_SIGNATURE
Antonio de Angelis14276e92018-07-10 14:35:43 +01001057 * The hash of the message was calculated successfully, but it
1058 * differs from the expected hash.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001059 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001060 * The operation state is not valid (it must be active).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001061 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1062 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1063 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001064 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1065 * \retval #PSA_ERROR_BAD_STATE
1066 * The library has not been previously initialized by psa_crypto_init().
1067 * It is implementation-dependent whether a failure to initialize
1068 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001069 */
1070psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1071 const uint8_t *hash,
1072 size_t hash_length);
1073
1074/** Abort a hash operation.
1075 *
Antonio de Angelis14276e92018-07-10 14:35:43 +01001076 * Aborting an operation frees all associated resources except for the
Antonio de Angelis377a1552018-11-22 17:02:40 +00001077 * \p operation structure itself. Once aborted, the operation object
1078 * can be reused for another operation by calling
1079 * psa_hash_setup() again.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001080 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001081 * You may call this function any time after the operation object has
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001082 * been initialized by one of the methods described in #psa_hash_operation_t.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001083 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001084 * In particular, calling psa_hash_abort() after the operation has been
1085 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1086 * psa_hash_verify() is safe and has no effect.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001087 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001088 * \param[in,out] operation Initialized hash operation.
1089 *
1090 * \retval #PSA_SUCCESS
Antonio de Angelis377a1552018-11-22 17:02:40 +00001091 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1092 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001093 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1094 * \retval #PSA_ERROR_BAD_STATE
1095 * The library has not been previously initialized by psa_crypto_init().
1096 * It is implementation-dependent whether a failure to initialize
1097 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001098 */
1099psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
1100
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001101/** Clone a hash operation.
1102 *
1103 * This function copies the state of an ongoing hash operation to
1104 * a new operation object. In other words, this function is equivalent
1105 * to calling psa_hash_setup() on \p target_operation with the same
1106 * algorithm that \p source_operation was set up for, then
1107 * psa_hash_update() on \p target_operation with the same input that
1108 * that was passed to \p source_operation. After this function returns, the
1109 * two objects are independent, i.e. subsequent calls involving one of
1110 * the objects do not affect the other object.
1111 *
1112 * \param[in] source_operation The active hash operation to clone.
1113 * \param[in,out] target_operation The operation object to set up.
1114 * It must be initialized but not active.
1115 *
1116 * \retval #PSA_SUCCESS
1117 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001118 * The \p source_operation state is not valid (it must be active).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001119 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001120 * The \p target_operation state is not valid (it must be inactive).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001121 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1122 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001123 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1124 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1125 * \retval #PSA_ERROR_BAD_STATE
1126 * The library has not been previously initialized by psa_crypto_init().
1127 * It is implementation-dependent whether a failure to initialize
1128 * results in this error code.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001129 */
1130psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1131 psa_hash_operation_t *target_operation);
1132
Antonio de Angelis14276e92018-07-10 14:35:43 +01001133/**@}*/
1134
1135/** \defgroup MAC Message authentication codes
1136 * @{
1137 */
1138
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001139/** Calculate the MAC (message authentication code) of a message.
1140 *
1141 * \note To verify the MAC of a message against an
1142 * expected value, use psa_mac_verify() instead.
1143 * Beware that comparing integrity or authenticity data such as
1144 * MAC values with a function such as \c memcmp is risky
1145 * because the time taken by the comparison may leak information
1146 * about the MAC value which could allow an attacker to guess
1147 * a valid MAC and thereby bypass security controls.
1148 *
Maulik Patel28659c42021-01-06 14:09:22 +00001149 * \param key Identifier of the key to use for the operation. It
1150 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001151 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1152 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1153 * \param[in] input Buffer containing the input message.
1154 * \param input_length Size of the \p input buffer in bytes.
1155 * \param[out] mac Buffer where the MAC value is to be written.
1156 * \param mac_size Size of the \p mac buffer in bytes.
1157 * \param[out] mac_length On success, the number of bytes
1158 * that make up the MAC value.
1159 *
1160 * \retval #PSA_SUCCESS
1161 * Success.
1162 * \retval #PSA_ERROR_INVALID_HANDLE
1163 * \retval #PSA_ERROR_NOT_PERMITTED
1164 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00001165 * \p key is not compatible with \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001166 * \retval #PSA_ERROR_NOT_SUPPORTED
1167 * \p alg is not supported or is not a MAC algorithm.
1168 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1169 * \p mac_size is too small
1170 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1171 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1172 * \retval #PSA_ERROR_HARDWARE_FAILURE
1173 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1174 * \retval #PSA_ERROR_STORAGE_FAILURE
1175 * The key could not be retrieved from storage.
1176 * \retval #PSA_ERROR_BAD_STATE
1177 * The library has not been previously initialized by psa_crypto_init().
1178 * It is implementation-dependent whether a failure to initialize
1179 * results in this error code.
1180 */
Maulik Patel28659c42021-01-06 14:09:22 +00001181psa_status_t psa_mac_compute(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001182 psa_algorithm_t alg,
1183 const uint8_t *input,
1184 size_t input_length,
1185 uint8_t *mac,
1186 size_t mac_size,
1187 size_t *mac_length);
1188
1189/** Calculate the MAC of a message and compare it with a reference value.
1190 *
Maulik Patel28659c42021-01-06 14:09:22 +00001191 * \param key Identifier of the key to use for the operation. It
1192 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001193 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1194 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1195 * \param[in] input Buffer containing the input message.
1196 * \param input_length Size of the \p input buffer in bytes.
1197 * \param[out] mac Buffer containing the expected MAC value.
1198 * \param mac_length Size of the \p mac buffer in bytes.
1199 *
1200 * \retval #PSA_SUCCESS
1201 * The expected MAC is identical to the actual MAC of the input.
1202 * \retval #PSA_ERROR_INVALID_SIGNATURE
1203 * The MAC of the message was calculated successfully, but it
1204 * differs from the expected value.
1205 * \retval #PSA_ERROR_INVALID_HANDLE
1206 * \retval #PSA_ERROR_NOT_PERMITTED
1207 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00001208 * \p key is not compatible with \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001209 * \retval #PSA_ERROR_NOT_SUPPORTED
1210 * \p alg is not supported or is not a MAC algorithm.
1211 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1212 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1213 * \retval #PSA_ERROR_HARDWARE_FAILURE
1214 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1215 * \retval #PSA_ERROR_STORAGE_FAILURE
1216 * The key could not be retrieved from storage.
1217 * \retval #PSA_ERROR_BAD_STATE
1218 * The library has not been previously initialized by psa_crypto_init().
1219 * It is implementation-dependent whether a failure to initialize
1220 * results in this error code.
1221 */
Maulik Patel28659c42021-01-06 14:09:22 +00001222psa_status_t psa_mac_verify(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001223 psa_algorithm_t alg,
1224 const uint8_t *input,
1225 size_t input_length,
1226 const uint8_t *mac,
Soby Mathew07ef6e42020-07-20 21:09:23 +01001227 size_t mac_length);
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001228
Antonio de Angelis377a1552018-11-22 17:02:40 +00001229/** The type of the state data structure for multipart MAC operations.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001230 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001231 * Before calling any function on a MAC operation object, the application must
1232 * initialize it by any of the following means:
1233 * - Set the structure to all-bits-zero, for example:
1234 * \code
1235 * psa_mac_operation_t operation;
1236 * memset(&operation, 0, sizeof(operation));
1237 * \endcode
1238 * - Initialize the structure to logical zero values, for example:
1239 * \code
1240 * psa_mac_operation_t operation = {0};
1241 * \endcode
1242 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1243 * for example:
1244 * \code
1245 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1246 * \endcode
1247 * - Assign the result of the function psa_mac_operation_init()
1248 * to the structure, for example:
1249 * \code
1250 * psa_mac_operation_t operation;
1251 * operation = psa_mac_operation_init();
1252 * \endcode
1253 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001254 *
Summer Qin359167d2021-07-05 18:11:50 +08001255 * This is an implementation-defined \c struct. Applications should not
1256 * make any assumptions about the content of this structure.
1257 * Implementation details can change in future versions without notice. */
1258typedef struct psa_mac_operation_s psa_mac_operation_t;
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001259
1260/** Return an initial value for a MAC operation object.
1261 */
1262static psa_mac_operation_t psa_mac_operation_init(void);
1263
1264/** Set up a multipart MAC calculation operation.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001265 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001266 * This function sets up the calculation of the MAC
1267 * (message authentication code) of a byte string.
1268 * To verify the MAC of a message against an
1269 * expected value, use psa_mac_verify_setup() instead.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001270 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001271 * The sequence of operations to calculate a MAC is as follows:
Antonio de Angelis14276e92018-07-10 14:35:43 +01001272 * -# Allocate an operation object which will be passed to all the functions
1273 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001274 * -# Initialize the operation object with one of the methods described in the
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001275 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001276 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001277 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1278 * of the message each time. The MAC that is calculated is the MAC
1279 * of the concatenation of these messages in order.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001280 * -# At the end of the message, call psa_mac_sign_finish() to finish
1281 * calculating the MAC value and retrieve it.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001282 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001283 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1284 * operation will need to be reset by a call to psa_mac_abort(). The
1285 * application may call psa_mac_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001286 * has been initialized.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001287 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001288 * After a successful call to psa_mac_sign_setup(), the application must
1289 * eventually terminate the operation through one of the following methods:
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001290 * - A successful call to psa_mac_sign_finish().
1291 * - A call to psa_mac_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001292 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001293 * \param[in,out] operation The operation object to set up. It must have
1294 * been initialized as per the documentation for
1295 * #psa_mac_operation_t and not yet in use.
Maulik Patel28659c42021-01-06 14:09:22 +00001296 * \param key Identifier of the key to use for the operation. It
1297 * must remain valid until the operation terminates.
1298 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001299 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001300 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +01001301 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001302 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001303 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001304 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00001305 * \retval #PSA_ERROR_NOT_PERMITTED
1306 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00001307 * \p key is not compatible with \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001308 * \retval #PSA_ERROR_NOT_SUPPORTED
1309 * \p alg is not supported or is not a MAC algorithm.
1310 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1311 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1312 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001313 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1314 * \retval #PSA_ERROR_STORAGE_FAILURE
1315 * The key could not be retrieved from storage.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001316 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001317 * The operation state is not valid (it must be inactive).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001318 * \retval #PSA_ERROR_BAD_STATE
1319 * The library has not been previously initialized by psa_crypto_init().
1320 * It is implementation-dependent whether a failure to initialize
1321 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001322 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001323psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00001324 psa_key_id_t key,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001325 psa_algorithm_t alg);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001326
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001327/** Set up a multipart MAC verification operation.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001328 *
1329 * This function sets up the verification of the MAC
1330 * (message authentication code) of a byte string against an expected value.
1331 *
1332 * The sequence of operations to verify a MAC is as follows:
1333 * -# Allocate an operation object which will be passed to all the functions
1334 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001335 * -# Initialize the operation object with one of the methods described in the
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001336 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001337 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001338 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1339 * of the message each time. The MAC that is calculated is the MAC
1340 * of the concatenation of these messages in order.
1341 * -# At the end of the message, call psa_mac_verify_finish() to finish
1342 * calculating the actual MAC of the message and verify it against
1343 * the expected value.
1344 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001345 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1346 * operation will need to be reset by a call to psa_mac_abort(). The
1347 * application may call psa_mac_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001348 * has been initialized.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001349 *
1350 * After a successful call to psa_mac_verify_setup(), the application must
1351 * eventually terminate the operation through one of the following methods:
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001352 * - A successful call to psa_mac_verify_finish().
1353 * - A call to psa_mac_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001354 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001355 * \param[in,out] operation The operation object to set up. It must have
1356 * been initialized as per the documentation for
1357 * #psa_mac_operation_t and not yet in use.
Maulik Patel28659c42021-01-06 14:09:22 +00001358 * \param key Identifier of the key to use for the operation. It
1359 * must remain valid until the operation terminates.
1360 * It must allow the usage
1361 * PSA_KEY_USAGE_VERIFY_MESSAGE.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001362 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1363 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1364 *
1365 * \retval #PSA_SUCCESS
1366 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001367 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00001368 * \retval #PSA_ERROR_NOT_PERMITTED
1369 * \retval #PSA_ERROR_INVALID_ARGUMENT
1370 * \c key is not compatible with \c alg.
1371 * \retval #PSA_ERROR_NOT_SUPPORTED
1372 * \c alg is not supported or is not a MAC algorithm.
1373 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1374 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1375 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001376 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1377 * \retval #PSA_ERROR_STORAGE_FAILURE
1378 * The key could not be retrieved from storage
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001379 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001380 * The operation state is not valid (it must be inactive).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001381 * \retval #PSA_ERROR_BAD_STATE
1382 * The library has not been previously initialized by psa_crypto_init().
1383 * It is implementation-dependent whether a failure to initialize
1384 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001385 */
1386psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00001387 psa_key_id_t key,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001388 psa_algorithm_t alg);
1389
1390/** Add a message fragment to a multipart MAC operation.
1391 *
1392 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1393 * before calling this function.
1394 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001395 * If this function returns an error status, the operation enters an error
1396 * state and must be aborted by calling psa_mac_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001397 *
1398 * \param[in,out] operation Active MAC operation.
1399 * \param[in] input Buffer containing the message fragment to add to
1400 * the MAC calculation.
1401 * \param input_length Size of the \p input buffer in bytes.
1402 *
1403 * \retval #PSA_SUCCESS
1404 * Success.
1405 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001406 * The operation state is not valid (it must be active).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001407 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1408 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1409 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001410 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1411 * \retval #PSA_ERROR_STORAGE_FAILURE
1412 * \retval #PSA_ERROR_BAD_STATE
1413 * The library has not been previously initialized by psa_crypto_init().
1414 * It is implementation-dependent whether a failure to initialize
1415 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001416 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001417psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1418 const uint8_t *input,
1419 size_t input_length);
1420
Antonio de Angelis377a1552018-11-22 17:02:40 +00001421/** Finish the calculation of the MAC of a message.
1422 *
1423 * The application must call psa_mac_sign_setup() before calling this function.
1424 * This function calculates the MAC of the message formed by concatenating
1425 * the inputs passed to preceding calls to psa_mac_update().
1426 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001427 * When this function returns successfuly, the operation becomes inactive.
1428 * If this function returns an error status, the operation enters an error
1429 * state and must be aborted by calling psa_mac_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001430 *
1431 * \warning Applications should not call this function if they expect
1432 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1433 * Beware that comparing integrity or authenticity data such as
1434 * MAC values with a function such as \c memcmp is risky
1435 * because the time taken by the comparison may leak information
1436 * about the MAC value which could allow an attacker to guess
1437 * a valid MAC and thereby bypass security controls.
1438 *
1439 * \param[in,out] operation Active MAC operation.
1440 * \param[out] mac Buffer where the MAC value is to be written.
1441 * \param mac_size Size of the \p mac buffer in bytes.
1442 * \param[out] mac_length On success, the number of bytes
1443 * that make up the MAC value. This is always
Maulik Patel13b27cf2021-05-14 11:44:53 +01001444 * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)
Antonio de Angelis377a1552018-11-22 17:02:40 +00001445 * where \c key_type and \c key_bits are the type and
1446 * bit-size respectively of the key and \c alg is the
1447 * MAC algorithm that is calculated.
1448 *
1449 * \retval #PSA_SUCCESS
1450 * Success.
1451 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001452 * The operation state is not valid (it must be an active mac sign
1453 * operation).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001454 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1455 * The size of the \p mac buffer is too small. You can determine a
Maulik Patel13b27cf2021-05-14 11:44:53 +01001456 * sufficient buffer size by calling PSA_MAC_LENGTH().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001457 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1458 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1459 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001460 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1461 * \retval #PSA_ERROR_STORAGE_FAILURE
1462 * \retval #PSA_ERROR_BAD_STATE
1463 * The library has not been previously initialized by psa_crypto_init().
1464 * It is implementation-dependent whether a failure to initialize
1465 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001466 */
1467psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1468 uint8_t *mac,
1469 size_t mac_size,
1470 size_t *mac_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001471
Antonio de Angelis377a1552018-11-22 17:02:40 +00001472/** Finish the calculation of the MAC of a message and compare it with
1473 * an expected value.
1474 *
1475 * The application must call psa_mac_verify_setup() before calling this function.
1476 * This function calculates the MAC of the message formed by concatenating
1477 * the inputs passed to preceding calls to psa_mac_update(). It then
1478 * compares the calculated MAC with the expected MAC passed as a
1479 * parameter to this function.
1480 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001481 * When this function returns successfuly, the operation becomes inactive.
1482 * If this function returns an error status, the operation enters an error
1483 * state and must be aborted by calling psa_mac_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001484 *
1485 * \note Implementations shall make the best effort to ensure that the
1486 * comparison between the actual MAC and the expected MAC is performed
1487 * in constant time.
1488 *
1489 * \param[in,out] operation Active MAC operation.
1490 * \param[in] mac Buffer containing the expected MAC value.
1491 * \param mac_length Size of the \p mac buffer in bytes.
1492 *
1493 * \retval #PSA_SUCCESS
1494 * The expected MAC is identical to the actual MAC of the message.
1495 * \retval #PSA_ERROR_INVALID_SIGNATURE
1496 * The MAC of the message was calculated successfully, but it
1497 * differs from the expected MAC.
1498 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001499 * The operation state is not valid (it must be an active mac verify
1500 * operation).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001501 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1502 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1503 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001504 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1505 * \retval #PSA_ERROR_STORAGE_FAILURE
1506 * \retval #PSA_ERROR_BAD_STATE
1507 * The library has not been previously initialized by psa_crypto_init().
1508 * It is implementation-dependent whether a failure to initialize
1509 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001510 */
1511psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1512 const uint8_t *mac,
1513 size_t mac_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001514
Antonio de Angelis377a1552018-11-22 17:02:40 +00001515/** Abort a MAC operation.
1516 *
1517 * Aborting an operation frees all associated resources except for the
1518 * \p operation structure itself. Once aborted, the operation object
1519 * can be reused for another operation by calling
1520 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
1521 *
1522 * You may call this function any time after the operation object has
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001523 * been initialized by one of the methods described in #psa_mac_operation_t.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001524 *
1525 * In particular, calling psa_mac_abort() after the operation has been
1526 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1527 * psa_mac_verify_finish() is safe and has no effect.
1528 *
1529 * \param[in,out] operation Initialized MAC operation.
1530 *
1531 * \retval #PSA_SUCCESS
Antonio de Angelis377a1552018-11-22 17:02:40 +00001532 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1533 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001534 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1535 * \retval #PSA_ERROR_BAD_STATE
1536 * The library has not been previously initialized by psa_crypto_init().
1537 * It is implementation-dependent whether a failure to initialize
1538 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001539 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001540psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1541
1542/**@}*/
1543
1544/** \defgroup cipher Symmetric ciphers
1545 * @{
1546 */
1547
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001548/** Encrypt a message using a symmetric cipher.
1549 *
1550 * This function encrypts a message with a random IV (initialization
1551 * vector). Use the multipart operation interface with a
1552 * #psa_cipher_operation_t object to provide other forms of IV.
1553 *
Maulik Patel28659c42021-01-06 14:09:22 +00001554 * \param key Identifier of the key to use for the operation.
1555 * It must allow the usage #PSA_KEY_USAGE_ENCRYPT.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001556 * \param alg The cipher algorithm to compute
1557 * (\c PSA_ALG_XXX value such that
1558 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1559 * \param[in] input Buffer containing the message to encrypt.
1560 * \param input_length Size of the \p input buffer in bytes.
1561 * \param[out] output Buffer where the output is to be written.
1562 * The output contains the IV followed by
1563 * the ciphertext proper.
1564 * \param output_size Size of the \p output buffer in bytes.
1565 * \param[out] output_length On success, the number of bytes
1566 * that make up the output.
1567 *
1568 * \retval #PSA_SUCCESS
1569 * Success.
1570 * \retval #PSA_ERROR_INVALID_HANDLE
1571 * \retval #PSA_ERROR_NOT_PERMITTED
1572 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00001573 * \p key is not compatible with \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001574 * \retval #PSA_ERROR_NOT_SUPPORTED
1575 * \p alg is not supported or is not a cipher algorithm.
1576 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1577 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1578 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1579 * \retval #PSA_ERROR_HARDWARE_FAILURE
1580 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1581 * \retval #PSA_ERROR_STORAGE_FAILURE
1582 * \retval #PSA_ERROR_BAD_STATE
1583 * The library has not been previously initialized by psa_crypto_init().
1584 * It is implementation-dependent whether a failure to initialize
1585 * results in this error code.
1586 */
Maulik Patel28659c42021-01-06 14:09:22 +00001587psa_status_t psa_cipher_encrypt(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001588 psa_algorithm_t alg,
1589 const uint8_t *input,
1590 size_t input_length,
1591 uint8_t *output,
1592 size_t output_size,
1593 size_t *output_length);
1594
1595/** Decrypt a message using a symmetric cipher.
1596 *
1597 * This function decrypts a message encrypted with a symmetric cipher.
1598 *
Maulik Patel28659c42021-01-06 14:09:22 +00001599 * \param key Identifier of the key to use for the operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001600 * It must remain valid until the operation
Maulik Patel28659c42021-01-06 14:09:22 +00001601 * terminates. It must allow the usage
1602 * #PSA_KEY_USAGE_DECRYPT.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001603 * \param alg The cipher algorithm to compute
1604 * (\c PSA_ALG_XXX value such that
1605 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1606 * \param[in] input Buffer containing the message to decrypt.
1607 * This consists of the IV followed by the
1608 * ciphertext proper.
1609 * \param input_length Size of the \p input buffer in bytes.
1610 * \param[out] output Buffer where the plaintext is to be written.
1611 * \param output_size Size of the \p output buffer in bytes.
1612 * \param[out] output_length On success, the number of bytes
1613 * that make up the output.
1614 *
1615 * \retval #PSA_SUCCESS
1616 * Success.
1617 * \retval #PSA_ERROR_INVALID_HANDLE
1618 * \retval #PSA_ERROR_NOT_PERMITTED
1619 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00001620 * \p key is not compatible with \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001621 * \retval #PSA_ERROR_NOT_SUPPORTED
1622 * \p alg is not supported or is not a cipher algorithm.
1623 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1624 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1625 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1626 * \retval #PSA_ERROR_HARDWARE_FAILURE
1627 * \retval #PSA_ERROR_STORAGE_FAILURE
1628 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1629 * \retval #PSA_ERROR_BAD_STATE
1630 * The library has not been previously initialized by psa_crypto_init().
1631 * It is implementation-dependent whether a failure to initialize
1632 * results in this error code.
1633 */
Maulik Patel28659c42021-01-06 14:09:22 +00001634psa_status_t psa_cipher_decrypt(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001635 psa_algorithm_t alg,
1636 const uint8_t *input,
1637 size_t input_length,
1638 uint8_t *output,
1639 size_t output_size,
1640 size_t *output_length);
1641
Antonio de Angelis377a1552018-11-22 17:02:40 +00001642/** The type of the state data structure for multipart cipher operations.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001643 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001644 * Before calling any function on a cipher operation object, the application
1645 * must initialize it by any of the following means:
1646 * - Set the structure to all-bits-zero, for example:
1647 * \code
1648 * psa_cipher_operation_t operation;
1649 * memset(&operation, 0, sizeof(operation));
1650 * \endcode
1651 * - Initialize the structure to logical zero values, for example:
1652 * \code
1653 * psa_cipher_operation_t operation = {0};
1654 * \endcode
1655 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1656 * for example:
1657 * \code
1658 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1659 * \endcode
1660 * - Assign the result of the function psa_cipher_operation_init()
1661 * to the structure, for example:
1662 * \code
1663 * psa_cipher_operation_t operation;
1664 * operation = psa_cipher_operation_init();
1665 * \endcode
1666 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001667 * This is an implementation-defined \c struct. Applications should not
Summer Qin359167d2021-07-05 18:11:50 +08001668 * make any assumptions about the content of this structure.
1669 * Implementation details can change in future versions without notice. */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001670typedef struct psa_cipher_operation_s psa_cipher_operation_t;
Antonio de Angelis14276e92018-07-10 14:35:43 +01001671
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001672/** Return an initial value for a cipher operation object.
1673 */
1674static psa_cipher_operation_t psa_cipher_operation_init(void);
1675
Antonio de Angelis14276e92018-07-10 14:35:43 +01001676/** Set the key for a multipart symmetric encryption operation.
1677 *
1678 * The sequence of operations to encrypt a message with a symmetric cipher
1679 * is as follows:
1680 * -# Allocate an operation object which will be passed to all the functions
1681 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001682 * -# Initialize the operation object with one of the methods described in the
1683 * documentation for #psa_cipher_operation_t, e.g.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001684 * #PSA_CIPHER_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001685 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001686 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Antonio de Angelis14276e92018-07-10 14:35:43 +01001687 * generate or set the IV (initialization vector). You should use
Antonio de Angelis377a1552018-11-22 17:02:40 +00001688 * psa_cipher_generate_iv() unless the protocol you are implementing
Antonio de Angelis14276e92018-07-10 14:35:43 +01001689 * requires a specific IV value.
1690 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1691 * of the message each time.
1692 * -# Call psa_cipher_finish().
1693 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001694 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1695 * the operation will need to be reset by a call to psa_cipher_abort(). The
1696 * application may call psa_cipher_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001697 * has been initialized.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001698 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001699 * After a successful call to psa_cipher_encrypt_setup(), the application must
Antonio de Angelis14276e92018-07-10 14:35:43 +01001700 * eventually terminate the operation. The following events terminate an
1701 * operation:
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001702 * - A successful call to psa_cipher_finish().
1703 * - A call to psa_cipher_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001704 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001705 * \param[in,out] operation The operation object to set up. It must have
1706 * been initialized as per the documentation for
1707 * #psa_cipher_operation_t and not yet in use.
Maulik Patel28659c42021-01-06 14:09:22 +00001708 * \param key Identifier of the key to use for the operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001709 * It must remain valid until the operation
Maulik Patel28659c42021-01-06 14:09:22 +00001710 * terminates. It must allow the usage
1711 * #PSA_KEY_USAGE_ENCRYPT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001712 * \param alg The cipher algorithm to compute
1713 * (\c PSA_ALG_XXX value such that
1714 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +01001715 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001716 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001717 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001718 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00001719 * \retval #PSA_ERROR_NOT_PERMITTED
1720 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00001721 * \p key is not compatible with \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001722 * \retval #PSA_ERROR_NOT_SUPPORTED
1723 * \p alg is not supported or is not a cipher algorithm.
1724 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1725 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1726 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001727 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1728 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001729 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001730 * The operation state is not valid (it must be inactive).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001731 * \retval #PSA_ERROR_BAD_STATE
1732 * The library has not been previously initialized by psa_crypto_init().
1733 * It is implementation-dependent whether a failure to initialize
1734 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001735 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001736psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00001737 psa_key_id_t key,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001738 psa_algorithm_t alg);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001739
1740/** Set the key for a multipart symmetric decryption operation.
1741 *
1742 * The sequence of operations to decrypt a message with a symmetric cipher
1743 * is as follows:
1744 * -# Allocate an operation object which will be passed to all the functions
1745 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001746 * -# Initialize the operation object with one of the methods described in the
1747 * documentation for #psa_cipher_operation_t, e.g.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001748 * #PSA_CIPHER_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001749 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001750 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Antonio de Angelis14276e92018-07-10 14:35:43 +01001751 * decryption. If the IV is prepended to the ciphertext, you can call
1752 * psa_cipher_update() on a buffer containing the IV followed by the
1753 * beginning of the message.
1754 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1755 * of the message each time.
1756 * -# Call psa_cipher_finish().
1757 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001758 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1759 * the operation will need to be reset by a call to psa_cipher_abort(). The
1760 * application may call psa_cipher_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001761 * has been initialized.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001762 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001763 * After a successful call to psa_cipher_decrypt_setup(), the application must
Antonio de Angelis14276e92018-07-10 14:35:43 +01001764 * eventually terminate the operation. The following events terminate an
1765 * operation:
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001766 * - A successful call to psa_cipher_finish().
1767 * - A call to psa_cipher_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001768 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001769 * \param[in,out] operation The operation object to set up. It must have
1770 * been initialized as per the documentation for
1771 * #psa_cipher_operation_t and not yet in use.
Maulik Patel28659c42021-01-06 14:09:22 +00001772 * \param key Identifier of the key to use for the operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001773 * It must remain valid until the operation
Maulik Patel28659c42021-01-06 14:09:22 +00001774 * terminates. It must allow the usage
1775 * #PSA_KEY_USAGE_DECRYPT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001776 * \param alg The cipher algorithm to compute
1777 * (\c PSA_ALG_XXX value such that
1778 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +01001779 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001780 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001781 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001782 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00001783 * \retval #PSA_ERROR_NOT_PERMITTED
1784 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00001785 * \p key is not compatible with \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001786 * \retval #PSA_ERROR_NOT_SUPPORTED
1787 * \p alg is not supported or is not a cipher algorithm.
1788 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1789 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1790 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001791 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1792 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001793 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001794 * The operation state is not valid (it must be inactive).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001795 * \retval #PSA_ERROR_BAD_STATE
1796 * The library has not been previously initialized by psa_crypto_init().
1797 * It is implementation-dependent whether a failure to initialize
1798 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001799 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001800psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00001801 psa_key_id_t key,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001802 psa_algorithm_t alg);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001803
Antonio de Angelis377a1552018-11-22 17:02:40 +00001804/** Generate an IV for a symmetric encryption operation.
1805 *
1806 * This function generates a random IV (initialization vector), nonce
1807 * or initial counter value for the encryption operation as appropriate
1808 * for the chosen algorithm, key type and key size.
1809 *
1810 * The application must call psa_cipher_encrypt_setup() before
1811 * calling this function.
1812 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001813 * If this function returns an error status, the operation enters an error
1814 * state and must be aborted by calling psa_cipher_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001815 *
1816 * \param[in,out] operation Active cipher operation.
1817 * \param[out] iv Buffer where the generated IV is to be written.
1818 * \param iv_size Size of the \p iv buffer in bytes.
1819 * \param[out] iv_length On success, the number of bytes of the
1820 * generated IV.
1821 *
1822 * \retval #PSA_SUCCESS
1823 * Success.
1824 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001825 * The operation state is not valid (it must be active, with no IV set).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001826 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1827 * The size of the \p iv buffer is too small.
1828 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1829 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1830 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001831 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1832 * \retval #PSA_ERROR_STORAGE_FAILURE
1833 * \retval #PSA_ERROR_BAD_STATE
1834 * The library has not been previously initialized by psa_crypto_init().
1835 * It is implementation-dependent whether a failure to initialize
1836 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001837 */
1838psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001839 uint8_t *iv,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001840 size_t iv_size,
1841 size_t *iv_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001842
Antonio de Angelis377a1552018-11-22 17:02:40 +00001843/** Set the IV for a symmetric encryption or decryption operation.
1844 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001845 * This function sets the IV (initialization vector), nonce
Antonio de Angelis377a1552018-11-22 17:02:40 +00001846 * or initial counter value for the encryption or decryption operation.
1847 *
1848 * The application must call psa_cipher_encrypt_setup() before
1849 * calling this function.
1850 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001851 * If this function returns an error status, the operation enters an error
1852 * state and must be aborted by calling psa_cipher_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001853 *
1854 * \note When encrypting, applications should use psa_cipher_generate_iv()
1855 * instead of this function, unless implementing a protocol that requires
1856 * a non-random IV.
1857 *
1858 * \param[in,out] operation Active cipher operation.
1859 * \param[in] iv Buffer containing the IV to use.
1860 * \param iv_length Size of the IV in bytes.
1861 *
1862 * \retval #PSA_SUCCESS
1863 * Success.
1864 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001865 * The operation state is not valid (it must be an active cipher
1866 * encrypt operation, with no IV set).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001867 * \retval #PSA_ERROR_INVALID_ARGUMENT
1868 * The size of \p iv is not acceptable for the chosen algorithm,
1869 * or the chosen algorithm does not use an IV.
1870 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1871 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1872 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001873 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1874 * \retval #PSA_ERROR_STORAGE_FAILURE
1875 * \retval #PSA_ERROR_BAD_STATE
1876 * The library has not been previously initialized by psa_crypto_init().
1877 * It is implementation-dependent whether a failure to initialize
1878 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001879 */
1880psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001881 const uint8_t *iv,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001882 size_t iv_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001883
Antonio de Angelis377a1552018-11-22 17:02:40 +00001884/** Encrypt or decrypt a message fragment in an active cipher operation.
1885 *
1886 * Before calling this function, you must:
1887 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1888 * The choice of setup function determines whether this function
1889 * encrypts or decrypts its input.
1890 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1891 * (recommended when encrypting) or psa_cipher_set_iv().
1892 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001893 * If this function returns an error status, the operation enters an error
1894 * state and must be aborted by calling psa_cipher_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001895 *
1896 * \param[in,out] operation Active cipher operation.
1897 * \param[in] input Buffer containing the message fragment to
1898 * encrypt or decrypt.
1899 * \param input_length Size of the \p input buffer in bytes.
1900 * \param[out] output Buffer where the output is to be written.
1901 * \param output_size Size of the \p output buffer in bytes.
1902 * \param[out] output_length On success, the number of bytes
1903 * that make up the returned output.
1904 *
1905 * \retval #PSA_SUCCESS
1906 * Success.
1907 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001908 * The operation state is not valid (it must be active, with an IV set
1909 * if required for the algorithm).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001910 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1911 * The size of the \p output buffer is too small.
1912 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1913 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1914 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001915 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1916 * \retval #PSA_ERROR_STORAGE_FAILURE
1917 * \retval #PSA_ERROR_BAD_STATE
1918 * The library has not been previously initialized by psa_crypto_init().
1919 * It is implementation-dependent whether a failure to initialize
1920 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001921 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001922psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1923 const uint8_t *input,
1924 size_t input_length,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001925 uint8_t *output,
Antonio de Angelis14276e92018-07-10 14:35:43 +01001926 size_t output_size,
1927 size_t *output_length);
1928
Antonio de Angelis377a1552018-11-22 17:02:40 +00001929/** Finish encrypting or decrypting a message in a cipher operation.
1930 *
1931 * The application must call psa_cipher_encrypt_setup() or
1932 * psa_cipher_decrypt_setup() before calling this function. The choice
1933 * of setup function determines whether this function encrypts or
1934 * decrypts its input.
1935 *
1936 * This function finishes the encryption or decryption of the message
1937 * formed by concatenating the inputs passed to preceding calls to
1938 * psa_cipher_update().
1939 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001940 * When this function returns successfuly, the operation becomes inactive.
1941 * If this function returns an error status, the operation enters an error
1942 * state and must be aborted by calling psa_cipher_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001943 *
1944 * \param[in,out] operation Active cipher operation.
1945 * \param[out] output Buffer where the output is to be written.
1946 * \param output_size Size of the \p output buffer in bytes.
1947 * \param[out] output_length On success, the number of bytes
1948 * that make up the returned output.
1949 *
1950 * \retval #PSA_SUCCESS
1951 * Success.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001952 * \retval #PSA_ERROR_INVALID_ARGUMENT
1953 * The total input size passed to this operation is not valid for
1954 * this particular algorithm. For example, the algorithm is a based
1955 * on block cipher and requires a whole number of blocks, but the
1956 * total input size is not a multiple of the block size.
1957 * \retval #PSA_ERROR_INVALID_PADDING
1958 * This is a decryption operation for an algorithm that includes
1959 * padding, and the ciphertext does not contain valid padding.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001960 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001961 * The operation state is not valid (it must be active, with an IV set
1962 * if required for the algorithm).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001963 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1964 * The size of the \p output buffer is too small.
1965 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1966 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1967 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001968 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1969 * \retval #PSA_ERROR_STORAGE_FAILURE
1970 * \retval #PSA_ERROR_BAD_STATE
1971 * The library has not been previously initialized by psa_crypto_init().
1972 * It is implementation-dependent whether a failure to initialize
1973 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001974 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001975psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
1976 uint8_t *output,
1977 size_t output_size,
1978 size_t *output_length);
1979
Antonio de Angelis377a1552018-11-22 17:02:40 +00001980/** Abort a cipher operation.
1981 *
1982 * Aborting an operation frees all associated resources except for the
1983 * \p operation structure itself. Once aborted, the operation object
1984 * can be reused for another operation by calling
1985 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
1986 *
1987 * You may call this function any time after the operation object has
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001988 * been initialized as described in #psa_cipher_operation_t.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001989 *
1990 * In particular, calling psa_cipher_abort() after the operation has been
1991 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1992 * is safe and has no effect.
1993 *
1994 * \param[in,out] operation Initialized cipher operation.
1995 *
1996 * \retval #PSA_SUCCESS
Antonio de Angelis377a1552018-11-22 17:02:40 +00001997 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1998 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001999 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2000 * \retval #PSA_ERROR_BAD_STATE
2001 * The library has not been previously initialized by psa_crypto_init().
2002 * It is implementation-dependent whether a failure to initialize
2003 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002004 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01002005psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2006
2007/**@}*/
2008
2009/** \defgroup aead Authenticated encryption with associated data (AEAD)
2010 * @{
2011 */
2012
Antonio de Angelis14276e92018-07-10 14:35:43 +01002013/** Process an authenticated encryption operation.
2014 *
Maulik Patel28659c42021-01-06 14:09:22 +00002015 * \param key Identifier of the key to use for the
2016 * operation. It must allow the usage
2017 * #PSA_KEY_USAGE_ENCRYPT.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002018 * \param alg The AEAD algorithm to compute
2019 * (\c PSA_ALG_XXX value such that
Antonio de Angelis377a1552018-11-22 17:02:40 +00002020 * #PSA_ALG_IS_AEAD(\p alg) is true).
2021 * \param[in] nonce Nonce or IV to use.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002022 * \param nonce_length Size of the \p nonce buffer in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002023 * \param[in] additional_data Additional data that will be authenticated
Antonio de Angelis14276e92018-07-10 14:35:43 +01002024 * but not encrypted.
2025 * \param additional_data_length Size of \p additional_data in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002026 * \param[in] plaintext Data that will be authenticated and
Antonio de Angelis14276e92018-07-10 14:35:43 +01002027 * encrypted.
2028 * \param plaintext_length Size of \p plaintext in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002029 * \param[out] ciphertext Output buffer for the authenticated and
Antonio de Angelis14276e92018-07-10 14:35:43 +01002030 * encrypted data. The additional data is not
2031 * part of this output. For algorithms where the
2032 * encrypted data and the authentication tag
2033 * are defined as separate outputs, the
2034 * authentication tag is appended to the
2035 * encrypted data.
2036 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Summer Qin359167d2021-07-05 18:11:50 +08002037 * This must be appropriate for the selected
2038 * algorithm and key:
2039 * - A sufficient output size is
2040 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type,
2041 * \p alg, \p plaintext_length) where
2042 * \c key_type is the type of \p key.
2043 * - #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p
2044 * plaintext_length) evaluates to the maximum
2045 * ciphertext size of any supported AEAD
2046 * encryption.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002047 * \param[out] ciphertext_length On success, the size of the output
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002048 * in the \p ciphertext buffer.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002049 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002050 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01002051 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002052 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00002053 * \retval #PSA_ERROR_NOT_PERMITTED
2054 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00002055 * \p key is not compatible with \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002056 * \retval #PSA_ERROR_NOT_SUPPORTED
2057 * \p alg is not supported or is not an AEAD algorithm.
2058 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002059 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Summer Qin359167d2021-07-05 18:11:50 +08002060 * \p ciphertext_size is too small.
2061 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2062 * \p plaintext_length) or
2063 * #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length) can be used to
2064 * determine the required buffer size.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002065 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2066 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002067 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2068 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002069 * \retval #PSA_ERROR_BAD_STATE
2070 * The library has not been previously initialized by psa_crypto_init().
2071 * It is implementation-dependent whether a failure to initialize
2072 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002073 */
Maulik Patel28659c42021-01-06 14:09:22 +00002074psa_status_t psa_aead_encrypt(psa_key_id_t key,
Antonio de Angelis377a1552018-11-22 17:02:40 +00002075 psa_algorithm_t alg,
2076 const uint8_t *nonce,
2077 size_t nonce_length,
2078 const uint8_t *additional_data,
2079 size_t additional_data_length,
2080 const uint8_t *plaintext,
2081 size_t plaintext_length,
2082 uint8_t *ciphertext,
2083 size_t ciphertext_size,
2084 size_t *ciphertext_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01002085
2086/** Process an authenticated decryption operation.
2087 *
Maulik Patel28659c42021-01-06 14:09:22 +00002088 * \param key Identifier of the key to use for the
2089 * operation. It must allow the usage
2090 * #PSA_KEY_USAGE_DECRYPT.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002091 * \param alg The AEAD algorithm to compute
2092 * (\c PSA_ALG_XXX value such that
Antonio de Angelis377a1552018-11-22 17:02:40 +00002093 * #PSA_ALG_IS_AEAD(\p alg) is true).
2094 * \param[in] nonce Nonce or IV to use.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002095 * \param nonce_length Size of the \p nonce buffer in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002096 * \param[in] additional_data Additional data that has been authenticated
Antonio de Angelis14276e92018-07-10 14:35:43 +01002097 * but not encrypted.
2098 * \param additional_data_length Size of \p additional_data in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002099 * \param[in] ciphertext Data that has been authenticated and
Antonio de Angelis14276e92018-07-10 14:35:43 +01002100 * encrypted. For algorithms where the
2101 * encrypted data and the authentication tag
2102 * are defined as separate inputs, the buffer
2103 * must contain the encrypted data followed
2104 * by the authentication tag.
2105 * \param ciphertext_length Size of \p ciphertext in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002106 * \param[out] plaintext Output buffer for the decrypted data.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002107 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Summer Qin359167d2021-07-05 18:11:50 +08002108 * This must be appropriate for the selected
2109 * algorithm and key:
2110 * - A sufficient output size is
2111 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type,
2112 * \p alg, \p ciphertext_length) where
2113 * \c key_type is the type of \p key.
2114 * - #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p
2115 * ciphertext_length) evaluates to the maximum
2116 * plaintext size of any supported AEAD
2117 * decryption.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002118 * \param[out] plaintext_length On success, the size of the output
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002119 * in the \p plaintext buffer.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002120 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002121 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01002122 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002123 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00002124 * \retval #PSA_ERROR_INVALID_SIGNATURE
Antonio de Angelis14276e92018-07-10 14:35:43 +01002125 * The ciphertext is not authentic.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002126 * \retval #PSA_ERROR_NOT_PERMITTED
2127 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00002128 * \p key is not compatible with \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002129 * \retval #PSA_ERROR_NOT_SUPPORTED
2130 * \p alg is not supported or is not an AEAD algorithm.
2131 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002132 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Summer Qin359167d2021-07-05 18:11:50 +08002133 * \p plaintext_size is too small.
2134 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2135 * \p ciphertext_length) or
2136 * #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length) can be used
2137 * to determine the required buffer size.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002138 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2139 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002140 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2141 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002142 * \retval #PSA_ERROR_BAD_STATE
2143 * The library has not been previously initialized by psa_crypto_init().
2144 * It is implementation-dependent whether a failure to initialize
2145 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002146 */
Maulik Patel28659c42021-01-06 14:09:22 +00002147psa_status_t psa_aead_decrypt(psa_key_id_t key,
Antonio de Angelis377a1552018-11-22 17:02:40 +00002148 psa_algorithm_t alg,
2149 const uint8_t *nonce,
2150 size_t nonce_length,
2151 const uint8_t *additional_data,
2152 size_t additional_data_length,
2153 const uint8_t *ciphertext,
2154 size_t ciphertext_length,
2155 uint8_t *plaintext,
2156 size_t plaintext_size,
2157 size_t *plaintext_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01002158
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002159/** The type of the state data structure for multipart AEAD operations.
2160 *
2161 * Before calling any function on an AEAD operation object, the application
2162 * must initialize it by any of the following means:
2163 * - Set the structure to all-bits-zero, for example:
2164 * \code
2165 * psa_aead_operation_t operation;
2166 * memset(&operation, 0, sizeof(operation));
2167 * \endcode
2168 * - Initialize the structure to logical zero values, for example:
2169 * \code
2170 * psa_aead_operation_t operation = {0};
2171 * \endcode
2172 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2173 * for example:
2174 * \code
2175 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2176 * \endcode
2177 * - Assign the result of the function psa_aead_operation_init()
2178 * to the structure, for example:
2179 * \code
2180 * psa_aead_operation_t operation;
2181 * operation = psa_aead_operation_init();
2182 * \endcode
2183 *
2184 * This is an implementation-defined \c struct. Applications should not
Summer Qin359167d2021-07-05 18:11:50 +08002185 * make any assumptions about the content of this structure.
2186 * Implementation details can change in future versions without notice. */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002187typedef struct psa_aead_operation_s psa_aead_operation_t;
2188
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002189/** Return an initial value for an AEAD operation object.
2190 */
2191static psa_aead_operation_t psa_aead_operation_init(void);
2192
2193/** Set the key for a multipart authenticated encryption operation.
2194 *
2195 * The sequence of operations to encrypt a message with authentication
2196 * is as follows:
2197 * -# Allocate an operation object which will be passed to all the functions
2198 * listed here.
2199 * -# Initialize the operation object with one of the methods described in the
2200 * documentation for #psa_aead_operation_t, e.g.
2201 * #PSA_AEAD_OPERATION_INIT.
2202 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
2203 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2204 * inputs to the subsequent calls to psa_aead_update_ad() and
2205 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2206 * for details.
2207 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2208 * generate or set the nonce. You should use
2209 * psa_aead_generate_nonce() unless the protocol you are implementing
2210 * requires a specific nonce value.
2211 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2212 * of the non-encrypted additional authenticated data each time.
2213 * -# Call psa_aead_update() zero, one or more times, passing a fragment
2214 * of the message to encrypt each time.
2215 * -# Call psa_aead_finish().
2216 *
2217 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2218 * the operation will need to be reset by a call to psa_aead_abort(). The
2219 * application may call psa_aead_abort() at any time after the operation
2220 * has been initialized.
2221 *
2222 * After a successful call to psa_aead_encrypt_setup(), the application must
2223 * eventually terminate the operation. The following events terminate an
2224 * operation:
2225 * - A successful call to psa_aead_finish().
2226 * - A call to psa_aead_abort().
2227 *
2228 * \param[in,out] operation The operation object to set up. It must have
2229 * been initialized as per the documentation for
2230 * #psa_aead_operation_t and not yet in use.
Maulik Patel28659c42021-01-06 14:09:22 +00002231 * \param key Identifier of the key to use for the operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002232 * It must remain valid until the operation
Maulik Patel28659c42021-01-06 14:09:22 +00002233 * terminates. It must allow the usage
2234 * #PSA_KEY_USAGE_ENCRYPT.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002235 * \param alg The AEAD algorithm to compute
2236 * (\c PSA_ALG_XXX value such that
2237 * #PSA_ALG_IS_AEAD(\p alg) is true).
2238 *
2239 * \retval #PSA_SUCCESS
2240 * Success.
2241 * \retval #PSA_ERROR_BAD_STATE
2242 * The operation state is not valid (it must be inactive).
Maulik Patel28659c42021-01-06 14:09:22 +00002243 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002244 * \retval #PSA_ERROR_NOT_PERMITTED
2245 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00002246 * \p key is not compatible with \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002247 * \retval #PSA_ERROR_NOT_SUPPORTED
2248 * \p alg is not supported or is not an AEAD algorithm.
2249 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2250 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2251 * \retval #PSA_ERROR_HARDWARE_FAILURE
2252 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2253 * \retval #PSA_ERROR_STORAGE_FAILURE
2254 * \retval #PSA_ERROR_BAD_STATE
2255 * The library has not been previously initialized by psa_crypto_init().
2256 * It is implementation-dependent whether a failure to initialize
2257 * results in this error code.
2258 */
2259psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00002260 psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002261 psa_algorithm_t alg);
2262
2263/** Set the key for a multipart authenticated decryption operation.
2264 *
2265 * The sequence of operations to decrypt a message with authentication
2266 * is as follows:
2267 * -# Allocate an operation object which will be passed to all the functions
2268 * listed here.
2269 * -# Initialize the operation object with one of the methods described in the
2270 * documentation for #psa_aead_operation_t, e.g.
2271 * #PSA_AEAD_OPERATION_INIT.
2272 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
2273 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2274 * inputs to the subsequent calls to psa_aead_update_ad() and
2275 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2276 * for details.
2277 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2278 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2279 * of the non-encrypted additional authenticated data each time.
2280 * -# Call psa_aead_update() zero, one or more times, passing a fragment
2281 * of the ciphertext to decrypt each time.
2282 * -# Call psa_aead_verify().
2283 *
2284 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2285 * the operation will need to be reset by a call to psa_aead_abort(). The
2286 * application may call psa_aead_abort() at any time after the operation
2287 * has been initialized.
2288 *
2289 * After a successful call to psa_aead_decrypt_setup(), the application must
2290 * eventually terminate the operation. The following events terminate an
2291 * operation:
2292 * - A successful call to psa_aead_verify().
2293 * - A call to psa_aead_abort().
2294 *
2295 * \param[in,out] operation The operation object to set up. It must have
2296 * been initialized as per the documentation for
2297 * #psa_aead_operation_t and not yet in use.
Maulik Patel28659c42021-01-06 14:09:22 +00002298 * \param key Identifier of the key to use for the operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002299 * It must remain valid until the operation
Maulik Patel28659c42021-01-06 14:09:22 +00002300 * terminates. It must allow the usage
2301 * #PSA_KEY_USAGE_DECRYPT.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002302 * \param alg The AEAD algorithm to compute
2303 * (\c PSA_ALG_XXX value such that
2304 * #PSA_ALG_IS_AEAD(\p alg) is true).
2305 *
2306 * \retval #PSA_SUCCESS
2307 * Success.
2308 * \retval #PSA_ERROR_BAD_STATE
2309 * The operation state is not valid (it must be inactive).
Maulik Patel28659c42021-01-06 14:09:22 +00002310 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002311 * \retval #PSA_ERROR_NOT_PERMITTED
2312 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00002313 * \p key is not compatible with \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002314 * \retval #PSA_ERROR_NOT_SUPPORTED
2315 * \p alg is not supported or is not an AEAD algorithm.
2316 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2317 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2318 * \retval #PSA_ERROR_HARDWARE_FAILURE
2319 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2320 * \retval #PSA_ERROR_STORAGE_FAILURE
2321 * \retval #PSA_ERROR_BAD_STATE
2322 * The library has not been previously initialized by psa_crypto_init().
2323 * It is implementation-dependent whether a failure to initialize
2324 * results in this error code.
2325 */
2326psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00002327 psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002328 psa_algorithm_t alg);
2329
2330/** Generate a random nonce for an authenticated encryption operation.
2331 *
2332 * This function generates a random nonce for the authenticated encryption
2333 * operation with an appropriate size for the chosen algorithm, key type
2334 * and key size.
2335 *
2336 * The application must call psa_aead_encrypt_setup() before
2337 * calling this function.
2338 *
2339 * If this function returns an error status, the operation enters an error
2340 * state and must be aborted by calling psa_aead_abort().
2341 *
2342 * \param[in,out] operation Active AEAD operation.
2343 * \param[out] nonce Buffer where the generated nonce is to be
2344 * written.
2345 * \param nonce_size Size of the \p nonce buffer in bytes.
2346 * \param[out] nonce_length On success, the number of bytes of the
2347 * generated nonce.
2348 *
2349 * \retval #PSA_SUCCESS
2350 * Success.
2351 * \retval #PSA_ERROR_BAD_STATE
2352 * The operation state is not valid (it must be an active aead encrypt
Maulik Patel28659c42021-01-06 14:09:22 +00002353 * operation, with no nonce set).
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002354 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2355 * The size of the \p nonce buffer is too small.
2356 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2357 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2358 * \retval #PSA_ERROR_HARDWARE_FAILURE
2359 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2360 * \retval #PSA_ERROR_STORAGE_FAILURE
2361 * \retval #PSA_ERROR_BAD_STATE
2362 * The library has not been previously initialized by psa_crypto_init().
2363 * It is implementation-dependent whether a failure to initialize
2364 * results in this error code.
2365 */
2366psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2367 uint8_t *nonce,
2368 size_t nonce_size,
2369 size_t *nonce_length);
2370
2371/** Set the nonce for an authenticated encryption or decryption operation.
2372 *
2373 * This function sets the nonce for the authenticated
2374 * encryption or decryption operation.
2375 *
2376 * The application must call psa_aead_encrypt_setup() or
2377 * psa_aead_decrypt_setup() before calling this function.
2378 *
2379 * If this function returns an error status, the operation enters an error
2380 * state and must be aborted by calling psa_aead_abort().
2381 *
2382 * \note When encrypting, applications should use psa_aead_generate_nonce()
2383 * instead of this function, unless implementing a protocol that requires
2384 * a non-random IV.
2385 *
2386 * \param[in,out] operation Active AEAD operation.
2387 * \param[in] nonce Buffer containing the nonce to use.
2388 * \param nonce_length Size of the nonce in bytes.
2389 *
2390 * \retval #PSA_SUCCESS
2391 * Success.
2392 * \retval #PSA_ERROR_BAD_STATE
2393 * The operation state is not valid (it must be active, with no nonce
2394 * set).
2395 * \retval #PSA_ERROR_INVALID_ARGUMENT
2396 * The size of \p nonce is not acceptable for the chosen algorithm.
2397 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2398 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2399 * \retval #PSA_ERROR_HARDWARE_FAILURE
2400 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2401 * \retval #PSA_ERROR_STORAGE_FAILURE
2402 * \retval #PSA_ERROR_BAD_STATE
2403 * The library has not been previously initialized by psa_crypto_init().
2404 * It is implementation-dependent whether a failure to initialize
2405 * results in this error code.
2406 */
2407psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2408 const uint8_t *nonce,
2409 size_t nonce_length);
2410
2411/** Declare the lengths of the message and additional data for AEAD.
2412 *
2413 * The application must call this function before calling
2414 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2415 * the operation requires it. If the algorithm does not require it,
2416 * calling this function is optional, but if this function is called
2417 * then the implementation must enforce the lengths.
2418 *
2419 * You may call this function before or after setting the nonce with
2420 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2421 *
2422 * - For #PSA_ALG_CCM, calling this function is required.
2423 * - For the other AEAD algorithms defined in this specification, calling
2424 * this function is not required.
2425 * - For vendor-defined algorithm, refer to the vendor documentation.
2426 *
2427 * If this function returns an error status, the operation enters an error
2428 * state and must be aborted by calling psa_aead_abort().
2429 *
2430 * \param[in,out] operation Active AEAD operation.
2431 * \param ad_length Size of the non-encrypted additional
2432 * authenticated data in bytes.
2433 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2434 *
2435 * \retval #PSA_SUCCESS
2436 * Success.
2437 * \retval #PSA_ERROR_BAD_STATE
2438 * The operation state is not valid (it must be active, and
2439 * psa_aead_update_ad() and psa_aead_update() must not have been
2440 * called yet).
2441 * \retval #PSA_ERROR_INVALID_ARGUMENT
2442 * At least one of the lengths is not acceptable for the chosen
2443 * algorithm.
2444 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2445 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2446 * \retval #PSA_ERROR_HARDWARE_FAILURE
2447 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2448 * \retval #PSA_ERROR_BAD_STATE
2449 * The library has not been previously initialized by psa_crypto_init().
2450 * It is implementation-dependent whether a failure to initialize
2451 * results in this error code.
2452 */
2453psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2454 size_t ad_length,
2455 size_t plaintext_length);
2456
2457/** Pass additional data to an active AEAD operation.
2458 *
2459 * Additional data is authenticated, but not encrypted.
2460 *
2461 * You may call this function multiple times to pass successive fragments
2462 * of the additional data. You may not call this function after passing
2463 * data to encrypt or decrypt with psa_aead_update().
2464 *
2465 * Before calling this function, you must:
2466 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2467 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2468 *
2469 * If this function returns an error status, the operation enters an error
2470 * state and must be aborted by calling psa_aead_abort().
2471 *
2472 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2473 * there is no guarantee that the input is valid. Therefore, until
2474 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2475 * treat the input as untrusted and prepare to undo any action that
2476 * depends on the input if psa_aead_verify() returns an error status.
2477 *
2478 * \param[in,out] operation Active AEAD operation.
2479 * \param[in] input Buffer containing the fragment of
2480 * additional data.
2481 * \param input_length Size of the \p input buffer in bytes.
2482 *
2483 * \retval #PSA_SUCCESS
2484 * Success.
2485 * \retval #PSA_ERROR_BAD_STATE
2486 * The operation state is not valid (it must be active, have a nonce
2487 * set, have lengths set if required by the algorithm, and
2488 * psa_aead_update() must not have been called yet).
2489 * \retval #PSA_ERROR_INVALID_ARGUMENT
2490 * The total input length overflows the additional data length that
2491 * was previously specified with psa_aead_set_lengths().
2492 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2493 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2494 * \retval #PSA_ERROR_HARDWARE_FAILURE
2495 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2496 * \retval #PSA_ERROR_STORAGE_FAILURE
2497 * \retval #PSA_ERROR_BAD_STATE
2498 * The library has not been previously initialized by psa_crypto_init().
2499 * It is implementation-dependent whether a failure to initialize
2500 * results in this error code.
2501 */
2502psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2503 const uint8_t *input,
2504 size_t input_length);
2505
2506/** Encrypt or decrypt a message fragment in an active AEAD operation.
2507 *
2508 * Before calling this function, you must:
2509 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2510 * The choice of setup function determines whether this function
2511 * encrypts or decrypts its input.
2512 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2513 * 3. Call psa_aead_update_ad() to pass all the additional data.
2514 *
2515 * If this function returns an error status, the operation enters an error
2516 * state and must be aborted by calling psa_aead_abort().
2517 *
2518 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2519 * there is no guarantee that the input is valid. Therefore, until
2520 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2521 * - Do not use the output in any way other than storing it in a
2522 * confidential location. If you take any action that depends
2523 * on the tentative decrypted data, this action will need to be
2524 * undone if the input turns out not to be valid. Furthermore,
2525 * if an adversary can observe that this action took place
2526 * (for example through timing), they may be able to use this
2527 * fact as an oracle to decrypt any message encrypted with the
2528 * same key.
2529 * - In particular, do not copy the output anywhere but to a
2530 * memory or storage space that you have exclusive access to.
2531 *
2532 * This function does not require the input to be aligned to any
2533 * particular block boundary. If the implementation can only process
2534 * a whole block at a time, it must consume all the input provided, but
2535 * it may delay the end of the corresponding output until a subsequent
2536 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2537 * provides sufficient input. The amount of data that can be delayed
2538 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
2539 *
2540 * \param[in,out] operation Active AEAD operation.
2541 * \param[in] input Buffer containing the message fragment to
2542 * encrypt or decrypt.
2543 * \param input_length Size of the \p input buffer in bytes.
2544 * \param[out] output Buffer where the output is to be written.
2545 * \param output_size Size of the \p output buffer in bytes.
Summer Qin359167d2021-07-05 18:11:50 +08002546 * This must be appropriate for the selected
2547 * algorithm and key:
2548 * - A sufficient output size is
2549 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type,
2550 * \c alg, \p input_length) where
2551 * \c key_type is the type of key and \c alg is
2552 * the algorithm that were used to set up the
2553 * operation.
2554 * - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p
2555 * input_length) evaluates to the maximum
2556 * output size of any supported AEAD
2557 * algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002558 * \param[out] output_length On success, the number of bytes
2559 * that make up the returned output.
2560 *
2561 * \retval #PSA_SUCCESS
2562 * Success.
2563 * \retval #PSA_ERROR_BAD_STATE
2564 * The operation state is not valid (it must be active, have a nonce
2565 * set, and have lengths set if required by the algorithm).
2566 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2567 * The size of the \p output buffer is too small.
Summer Qin359167d2021-07-05 18:11:50 +08002568 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or
2569 * #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to
2570 * determine the required buffer size.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002571 * \retval #PSA_ERROR_INVALID_ARGUMENT
2572 * The total length of input to psa_aead_update_ad() so far is
2573 * less than the additional data length that was previously
2574 * specified with psa_aead_set_lengths().
2575 * \retval #PSA_ERROR_INVALID_ARGUMENT
2576 * The total input length overflows the plaintext length that
2577 * was previously specified with psa_aead_set_lengths().
2578 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2579 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2580 * \retval #PSA_ERROR_HARDWARE_FAILURE
2581 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2582 * \retval #PSA_ERROR_STORAGE_FAILURE
2583 * \retval #PSA_ERROR_BAD_STATE
2584 * The library has not been previously initialized by psa_crypto_init().
2585 * It is implementation-dependent whether a failure to initialize
2586 * results in this error code.
2587 */
2588psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2589 const uint8_t *input,
2590 size_t input_length,
2591 uint8_t *output,
2592 size_t output_size,
2593 size_t *output_length);
2594
2595/** Finish encrypting a message in an AEAD operation.
2596 *
2597 * The operation must have been set up with psa_aead_encrypt_setup().
2598 *
2599 * This function finishes the authentication of the additional data
2600 * formed by concatenating the inputs passed to preceding calls to
2601 * psa_aead_update_ad() with the plaintext formed by concatenating the
2602 * inputs passed to preceding calls to psa_aead_update().
2603 *
2604 * This function has two output buffers:
2605 * - \p ciphertext contains trailing ciphertext that was buffered from
2606 * preceding calls to psa_aead_update().
Summer Qin359167d2021-07-05 18:11:50 +08002607 * - \p tag contains the authentication tag.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002608 *
2609 * When this function returns successfuly, the operation becomes inactive.
2610 * If this function returns an error status, the operation enters an error
2611 * state and must be aborted by calling psa_aead_abort().
2612 *
2613 * \param[in,out] operation Active AEAD operation.
2614 * \param[out] ciphertext Buffer where the last part of the ciphertext
2615 * is to be written.
2616 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Summer Qin359167d2021-07-05 18:11:50 +08002617 * This must be appropriate for the selected
2618 * algorithm and key:
2619 * - A sufficient output size is
2620 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type,
2621 * \c alg) where \c key_type is the type of key
2622 * and \c alg is the algorithm that were used to
2623 * set up the operation.
2624 * - #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE evaluates to
2625 * the maximum output size of any supported AEAD
2626 * algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002627 * \param[out] ciphertext_length On success, the number of bytes of
2628 * returned ciphertext.
2629 * \param[out] tag Buffer where the authentication tag is
2630 * to be written.
2631 * \param tag_size Size of the \p tag buffer in bytes.
Summer Qin359167d2021-07-05 18:11:50 +08002632 * This must be appropriate for the selected
2633 * algorithm and key:
2634 * - The exact tag size is #PSA_AEAD_TAG_LENGTH(\c
2635 * key_type, \c key_bits, \c alg) where
2636 * \c key_type and \c key_bits are the type and
2637 * bit-size of the key, and \c alg is the
2638 * algorithm that were used in the call to
2639 * psa_aead_encrypt_setup().
2640 * - #PSA_AEAD_TAG_MAX_SIZE evaluates to the
2641 * maximum tag size of any supported AEAD
2642 * algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002643 * \param[out] tag_length On success, the number of bytes
2644 * that make up the returned tag.
2645 *
2646 * \retval #PSA_SUCCESS
2647 * Success.
2648 * \retval #PSA_ERROR_BAD_STATE
2649 * The operation state is not valid (it must be an active encryption
2650 * operation with a nonce set).
2651 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2652 * The size of the \p ciphertext or \p tag buffer is too small.
Summer Qin359167d2021-07-05 18:11:50 +08002653 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, \c alg) or
2654 * #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE can be used to determine the
2655 * required \p ciphertext buffer size. #PSA_AEAD_TAG_LENGTH(\c key_type,
2656 * \c key_bits, \c alg) or #PSA_AEAD_TAG_MAX_SIZE can be used to
2657 * determine the required \p tag buffer size.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002658 * \retval #PSA_ERROR_INVALID_ARGUMENT
2659 * The total length of input to psa_aead_update_ad() so far is
2660 * less than the additional data length that was previously
2661 * specified with psa_aead_set_lengths().
2662 * \retval #PSA_ERROR_INVALID_ARGUMENT
2663 * The total length of input to psa_aead_update() so far is
2664 * less than the plaintext length that was previously
2665 * specified with psa_aead_set_lengths().
2666 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2667 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2668 * \retval #PSA_ERROR_HARDWARE_FAILURE
2669 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2670 * \retval #PSA_ERROR_STORAGE_FAILURE
2671 * \retval #PSA_ERROR_BAD_STATE
2672 * The library has not been previously initialized by psa_crypto_init().
2673 * It is implementation-dependent whether a failure to initialize
2674 * results in this error code.
2675 */
2676psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
2677 uint8_t *ciphertext,
2678 size_t ciphertext_size,
2679 size_t *ciphertext_length,
2680 uint8_t *tag,
2681 size_t tag_size,
2682 size_t *tag_length);
2683
2684/** Finish authenticating and decrypting a message in an AEAD operation.
2685 *
2686 * The operation must have been set up with psa_aead_decrypt_setup().
2687 *
2688 * This function finishes the authenticated decryption of the message
2689 * components:
2690 *
2691 * - The additional data consisting of the concatenation of the inputs
2692 * passed to preceding calls to psa_aead_update_ad().
2693 * - The ciphertext consisting of the concatenation of the inputs passed to
2694 * preceding calls to psa_aead_update().
2695 * - The tag passed to this function call.
2696 *
2697 * If the authentication tag is correct, this function outputs any remaining
2698 * plaintext and reports success. If the authentication tag is not correct,
2699 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
2700 *
2701 * When this function returns successfuly, the operation becomes inactive.
2702 * If this function returns an error status, the operation enters an error
2703 * state and must be aborted by calling psa_aead_abort().
2704 *
2705 * \note Implementations shall make the best effort to ensure that the
2706 * comparison between the actual tag and the expected tag is performed
2707 * in constant time.
2708 *
2709 * \param[in,out] operation Active AEAD operation.
2710 * \param[out] plaintext Buffer where the last part of the plaintext
2711 * is to be written. This is the remaining data
2712 * from previous calls to psa_aead_update()
2713 * that could not be processed until the end
2714 * of the input.
2715 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Summer Qin359167d2021-07-05 18:11:50 +08002716 * This must be appropriate for the selected algorithm and key:
2717 * - A sufficient output size is
2718 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type,
2719 * \c alg) where \c key_type is the type of key
2720 * and \c alg is the algorithm that were used to
2721 * set up the operation.
2722 * - #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE evaluates to
2723 * the maximum output size of any supported AEAD
2724 * algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002725 * \param[out] plaintext_length On success, the number of bytes of
2726 * returned plaintext.
2727 * \param[in] tag Buffer containing the authentication tag.
2728 * \param tag_length Size of the \p tag buffer in bytes.
2729 *
2730 * \retval #PSA_SUCCESS
2731 * Success.
2732 * \retval #PSA_ERROR_INVALID_SIGNATURE
2733 * The calculations were successful, but the authentication tag is
2734 * not correct.
2735 * \retval #PSA_ERROR_BAD_STATE
2736 * The operation state is not valid (it must be an active decryption
2737 * operation with a nonce set).
2738 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2739 * The size of the \p plaintext buffer is too small.
Summer Qin359167d2021-07-05 18:11:50 +08002740 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type, \c alg) or
2741 * #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE can be used to determine the
2742 * required buffer size.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002743 * \retval #PSA_ERROR_INVALID_ARGUMENT
2744 * The total length of input to psa_aead_update_ad() so far is
2745 * less than the additional data length that was previously
2746 * specified with psa_aead_set_lengths().
2747 * \retval #PSA_ERROR_INVALID_ARGUMENT
2748 * The total length of input to psa_aead_update() so far is
2749 * less than the plaintext length that was previously
2750 * specified with psa_aead_set_lengths().
2751 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2752 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2753 * \retval #PSA_ERROR_HARDWARE_FAILURE
2754 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2755 * \retval #PSA_ERROR_STORAGE_FAILURE
2756 * \retval #PSA_ERROR_BAD_STATE
2757 * The library has not been previously initialized by psa_crypto_init().
2758 * It is implementation-dependent whether a failure to initialize
2759 * results in this error code.
2760 */
2761psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2762 uint8_t *plaintext,
2763 size_t plaintext_size,
2764 size_t *plaintext_length,
2765 const uint8_t *tag,
2766 size_t tag_length);
2767
2768/** Abort an AEAD operation.
2769 *
2770 * Aborting an operation frees all associated resources except for the
2771 * \p operation structure itself. Once aborted, the operation object
2772 * can be reused for another operation by calling
2773 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2774 *
2775 * You may call this function any time after the operation object has
2776 * been initialized as described in #psa_aead_operation_t.
2777 *
2778 * In particular, calling psa_aead_abort() after the operation has been
2779 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2780 * psa_aead_verify() is safe and has no effect.
2781 *
2782 * \param[in,out] operation Initialized AEAD operation.
2783 *
2784 * \retval #PSA_SUCCESS
2785 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2786 * \retval #PSA_ERROR_HARDWARE_FAILURE
2787 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2788 * \retval #PSA_ERROR_BAD_STATE
2789 * The library has not been previously initialized by psa_crypto_init().
2790 * It is implementation-dependent whether a failure to initialize
2791 * results in this error code.
2792 */
2793psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2794
Antonio de Angelis14276e92018-07-10 14:35:43 +01002795/**@}*/
2796
2797/** \defgroup asymmetric Asymmetric cryptography
2798 * @{
2799 */
2800
2801/**
Summer Qin359167d2021-07-05 18:11:50 +08002802 * \brief Sign a message with a private key. For hash-and-sign algorithms,
2803 * this includes the hashing step.
2804 *
2805 * \note To perform a multi-part hash-and-sign signature algorithm, first use
2806 * a multi-part hash operation and then pass the resulting hash to
2807 * psa_sign_hash(). PSA_ALG_GET_HASH(\p alg) can be used to determine the
2808 * hash algorithm to use.
2809 *
2810 * \param[in] key Identifier of the key to use for the operation.
2811 * It must be an asymmetric key pair. The key must
2812 * allow the usage #PSA_KEY_USAGE_SIGN_MESSAGE.
2813 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
2814 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2815 * is true), that is compatible with the type of
2816 * \p key.
2817 * \param[in] input The input message to sign.
2818 * \param[in] input_length Size of the \p input buffer in bytes.
2819 * \param[out] signature Buffer where the signature is to be written.
2820 * \param[in] signature_size Size of the \p signature buffer in bytes. This
2821 * must be appropriate for the selected
2822 * algorithm and key:
2823 * - The required signature size is
2824 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2825 * where \c key_type and \c key_bits are the type and
2826 * bit-size respectively of key.
2827 * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
2828 * maximum signature size of any supported
2829 * signature algorithm.
2830 * \param[out] signature_length On success, the number of bytes that make up
2831 * the returned signature value.
2832 *
2833 * \retval #PSA_SUCCESS
2834 * \retval #PSA_ERROR_INVALID_HANDLE
2835 * \retval #PSA_ERROR_NOT_PERMITTED
2836 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2837 * or it does not permit the requested algorithm.
2838 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2839 * The size of the \p signature buffer is too small. You can
2840 * determine a sufficient buffer size by calling
2841 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2842 * where \c key_type and \c key_bits are the type and bit-size
2843 * respectively of \p key.
2844 * \retval #PSA_ERROR_NOT_SUPPORTED
2845 * \retval #PSA_ERROR_INVALID_ARGUMENT
2846 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2847 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2848 * \retval #PSA_ERROR_HARDWARE_FAILURE
2849 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2850 * \retval #PSA_ERROR_STORAGE_FAILURE
2851 * \retval #PSA_ERROR_DATA_CORRUPT
2852 * \retval #PSA_ERROR_DATA_INVALID
2853 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2854 * \retval #PSA_ERROR_BAD_STATE
2855 * The library has not been previously initialized by psa_crypto_init().
2856 * It is implementation-dependent whether a failure to initialize
2857 * results in this error code.
2858 */
2859psa_status_t psa_sign_message( psa_key_id_t key,
2860 psa_algorithm_t alg,
2861 const uint8_t * input,
2862 size_t input_length,
2863 uint8_t * signature,
2864 size_t signature_size,
2865 size_t * signature_length );
2866
2867/** \brief Verify the signature of a message with a public key, using
2868 * a hash-and-sign verification algorithm.
2869 *
2870 * \note To perform a multi-part hash-and-sign signature verification
2871 * algorithm, first use a multi-part hash operation to hash the message
2872 * and then pass the resulting hash to psa_verify_hash().
2873 * PSA_ALG_GET_HASH(\p alg) can be used to determine the hash algorithm
2874 * to use.
2875 *
2876 * \param[in] key Identifier of the key to use for the operation.
2877 * It must be a public key or an asymmetric key
2878 * pair. The key must allow the usage
2879 * #PSA_KEY_USAGE_VERIFY_MESSAGE.
2880 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
2881 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2882 * is true), that is compatible with the type of
2883 * \p key.
2884 * \param[in] input The message whose signature is to be verified.
2885 * \param[in] input_length Size of the \p input buffer in bytes.
2886 * \param[out] signature Buffer containing the signature to verify.
2887 * \param[in] signature_length Size of the \p signature buffer in bytes.
2888 *
2889 * \retval #PSA_SUCCESS
2890 * \retval #PSA_ERROR_INVALID_HANDLE
2891 * \retval #PSA_ERROR_NOT_PERMITTED
2892 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2893 * or it does not permit the requested algorithm.
2894 * \retval #PSA_ERROR_INVALID_SIGNATURE
2895 * The calculation was performed successfully, but the passed signature
2896 * is not a valid signature.
2897 * \retval #PSA_ERROR_NOT_SUPPORTED
2898 * \retval #PSA_ERROR_INVALID_ARGUMENT
2899 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2900 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2901 * \retval #PSA_ERROR_HARDWARE_FAILURE
2902 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2903 * \retval #PSA_ERROR_STORAGE_FAILURE
2904 * \retval #PSA_ERROR_DATA_CORRUPT
2905 * \retval #PSA_ERROR_DATA_INVALID
2906 * \retval #PSA_ERROR_BAD_STATE
2907 * The library has not been previously initialized by psa_crypto_init().
2908 * It is implementation-dependent whether a failure to initialize
2909 * results in this error code.
2910 */
2911psa_status_t psa_verify_message( psa_key_id_t key,
2912 psa_algorithm_t alg,
2913 const uint8_t * input,
2914 size_t input_length,
2915 const uint8_t * signature,
2916 size_t signature_length );
2917
2918/**
Antonio de Angelis14276e92018-07-10 14:35:43 +01002919 * \brief Sign a hash or short message with a private key.
2920 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002921 * Note that to perform a hash-and-sign signature algorithm, you must
2922 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Summer Qin359167d2021-07-05 18:11:50 +08002923 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
2924 * Then pass the resulting hash as the \p hash
Antonio de Angelis377a1552018-11-22 17:02:40 +00002925 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2926 * to determine the hash algorithm to use.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002927 *
Maulik Patel28659c42021-01-06 14:09:22 +00002928 * \param key Identifier of the key to use for the operation.
2929 * It must be an asymmetric key pair. The key must
2930 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
Summer Qinf07cc312022-01-05 16:52:54 +08002931 * \param alg A signature algorithm (PSA_ALG_XXX
2932 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
2933 * is true), that is compatible with
Maulik Patel28659c42021-01-06 14:09:22 +00002934 * the type of \p key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002935 * \param[in] hash The hash or message to sign.
2936 * \param hash_length Size of the \p hash buffer in bytes.
2937 * \param[out] signature Buffer where the signature is to be written.
2938 * \param signature_size Size of the \p signature buffer in bytes.
2939 * \param[out] signature_length On success, the number of bytes
2940 * that make up the returned signature value.
2941 *
2942 * \retval #PSA_SUCCESS
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002943 * \retval #PSA_ERROR_INVALID_HANDLE
2944 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis377a1552018-11-22 17:02:40 +00002945 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2946 * The size of the \p signature buffer is too small. You can
Antonio de Angelis14276e92018-07-10 14:35:43 +01002947 * determine a sufficient buffer size by calling
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002948 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01002949 * where \c key_type and \c key_bits are the type and bit-size
Maulik Patel28659c42021-01-06 14:09:22 +00002950 * respectively of \p key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002951 * \retval #PSA_ERROR_NOT_SUPPORTED
2952 * \retval #PSA_ERROR_INVALID_ARGUMENT
2953 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2954 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2955 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002956 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2957 * \retval #PSA_ERROR_STORAGE_FAILURE
Antonio de Angelis377a1552018-11-22 17:02:40 +00002958 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002959 * \retval #PSA_ERROR_BAD_STATE
2960 * The library has not been previously initialized by psa_crypto_init().
2961 * It is implementation-dependent whether a failure to initialize
2962 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002963 */
Maulik Patel28659c42021-01-06 14:09:22 +00002964psa_status_t psa_sign_hash(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002965 psa_algorithm_t alg,
2966 const uint8_t *hash,
2967 size_t hash_length,
2968 uint8_t *signature,
2969 size_t signature_size,
2970 size_t *signature_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01002971
2972/**
Summer Qin359167d2021-07-05 18:11:50 +08002973 * \brief Verify the signature of a hash or short message using a public key.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002974 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002975 * Note that to perform a hash-and-sign signature algorithm, you must
2976 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Summer Qin359167d2021-07-05 18:11:50 +08002977 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
2978 * Then pass the resulting hash as the \p hash
Antonio de Angelis377a1552018-11-22 17:02:40 +00002979 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2980 * to determine the hash algorithm to use.
2981 *
Maulik Patel28659c42021-01-06 14:09:22 +00002982 * \param key Identifier of the key to use for the operation. It
2983 * must be a public key or an asymmetric key pair. The
2984 * key must allow the usage
2985 * #PSA_KEY_USAGE_VERIFY_HASH.
Summer Qinf07cc312022-01-05 16:52:54 +08002986 * \param alg A signature algorithm (PSA_ALG_XXX
2987 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
2988 * is true), that is compatible with
Maulik Patel28659c42021-01-06 14:09:22 +00002989 * the type of \p key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002990 * \param[in] hash The hash or message whose signature is to be
2991 * verified.
2992 * \param hash_length Size of the \p hash buffer in bytes.
2993 * \param[in] signature Buffer containing the signature to verify.
2994 * \param signature_length Size of the \p signature buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002995 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002996 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01002997 * The signature is valid.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002998 * \retval #PSA_ERROR_INVALID_HANDLE
2999 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis377a1552018-11-22 17:02:40 +00003000 * \retval #PSA_ERROR_INVALID_SIGNATURE
Antonio de Angelis14276e92018-07-10 14:35:43 +01003001 * The calculation was perfomed successfully, but the passed
3002 * signature is not a valid signature.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003003 * \retval #PSA_ERROR_NOT_SUPPORTED
3004 * \retval #PSA_ERROR_INVALID_ARGUMENT
3005 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3006 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3007 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003008 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3009 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003010 * \retval #PSA_ERROR_BAD_STATE
3011 * The library has not been previously initialized by psa_crypto_init().
3012 * It is implementation-dependent whether a failure to initialize
3013 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003014 */
Maulik Patel28659c42021-01-06 14:09:22 +00003015psa_status_t psa_verify_hash(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003016 psa_algorithm_t alg,
3017 const uint8_t *hash,
3018 size_t hash_length,
3019 const uint8_t *signature,
3020 size_t signature_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01003021
Antonio de Angelis14276e92018-07-10 14:35:43 +01003022/**
3023 * \brief Encrypt a short message with a public key.
3024 *
Maulik Patel28659c42021-01-06 14:09:22 +00003025 * \param key Identifer of the key to use for the operation.
3026 * It must be a public key or an asymmetric key
3027 * pair. It must allow the usage
3028 * #PSA_KEY_USAGE_ENCRYPT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003029 * \param alg An asymmetric encryption algorithm that is
Maulik Patel28659c42021-01-06 14:09:22 +00003030 * compatible with the type of \p key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003031 * \param[in] input The message to encrypt.
3032 * \param input_length Size of the \p input buffer in bytes.
3033 * \param[in] salt A salt or label, if supported by the
3034 * encryption algorithm.
3035 * If the algorithm does not support a
3036 * salt, pass \c NULL.
3037 * If the algorithm supports an optional
3038 * salt and you do not want to pass a salt,
3039 * pass \c NULL.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003040 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00003041 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3042 * supported.
3043 * \param salt_length Size of the \p salt buffer in bytes.
3044 * If \p salt is \c NULL, pass 0.
3045 * \param[out] output Buffer where the encrypted message is to
3046 * be written.
3047 * \param output_size Size of the \p output buffer in bytes.
3048 * \param[out] output_length On success, the number of bytes
3049 * that make up the returned output.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003050 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00003051 * \retval #PSA_SUCCESS
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003052 * \retval #PSA_ERROR_INVALID_HANDLE
3053 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis377a1552018-11-22 17:02:40 +00003054 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3055 * The size of the \p output buffer is too small. You can
Antonio de Angelis14276e92018-07-10 14:35:43 +01003056 * determine a sufficient buffer size by calling
Antonio de Angelis377a1552018-11-22 17:02:40 +00003057 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01003058 * where \c key_type and \c key_bits are the type and bit-size
Maulik Patel28659c42021-01-06 14:09:22 +00003059 * respectively of \p key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003060 * \retval #PSA_ERROR_NOT_SUPPORTED
3061 * \retval #PSA_ERROR_INVALID_ARGUMENT
3062 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3063 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3064 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003065 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3066 * \retval #PSA_ERROR_STORAGE_FAILURE
Antonio de Angelis377a1552018-11-22 17:02:40 +00003067 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003068 * \retval #PSA_ERROR_BAD_STATE
3069 * The library has not been previously initialized by psa_crypto_init().
3070 * It is implementation-dependent whether a failure to initialize
3071 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003072 */
Maulik Patel28659c42021-01-06 14:09:22 +00003073psa_status_t psa_asymmetric_encrypt(psa_key_id_t key,
Antonio de Angelis14276e92018-07-10 14:35:43 +01003074 psa_algorithm_t alg,
3075 const uint8_t *input,
3076 size_t input_length,
3077 const uint8_t *salt,
3078 size_t salt_length,
3079 uint8_t *output,
3080 size_t output_size,
3081 size_t *output_length);
3082
3083/**
3084 * \brief Decrypt a short message with a private key.
3085 *
Maulik Patel28659c42021-01-06 14:09:22 +00003086 * \param key Identifier of the key to use for the operation.
3087 * It must be an asymmetric key pair. It must
3088 * allow the usage #PSA_KEY_USAGE_DECRYPT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003089 * \param alg An asymmetric encryption algorithm that is
Maulik Patel28659c42021-01-06 14:09:22 +00003090 * compatible with the type of \p key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003091 * \param[in] input The message to decrypt.
3092 * \param input_length Size of the \p input buffer in bytes.
3093 * \param[in] salt A salt or label, if supported by the
3094 * encryption algorithm.
3095 * If the algorithm does not support a
3096 * salt, pass \c NULL.
3097 * If the algorithm supports an optional
3098 * salt and you do not want to pass a salt,
3099 * pass \c NULL.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003100 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00003101 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3102 * supported.
3103 * \param salt_length Size of the \p salt buffer in bytes.
3104 * If \p salt is \c NULL, pass 0.
3105 * \param[out] output Buffer where the decrypted message is to
3106 * be written.
3107 * \param output_size Size of the \c output buffer in bytes.
3108 * \param[out] output_length On success, the number of bytes
3109 * that make up the returned output.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003110 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00003111 * \retval #PSA_SUCCESS
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003112 * \retval #PSA_ERROR_INVALID_HANDLE
3113 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis377a1552018-11-22 17:02:40 +00003114 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3115 * The size of the \p output buffer is too small. You can
Antonio de Angelis14276e92018-07-10 14:35:43 +01003116 * determine a sufficient buffer size by calling
Antonio de Angelis377a1552018-11-22 17:02:40 +00003117 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01003118 * where \c key_type and \c key_bits are the type and bit-size
Maulik Patel28659c42021-01-06 14:09:22 +00003119 * respectively of \p key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003120 * \retval #PSA_ERROR_NOT_SUPPORTED
3121 * \retval #PSA_ERROR_INVALID_ARGUMENT
3122 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3123 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3124 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003125 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3126 * \retval #PSA_ERROR_STORAGE_FAILURE
Antonio de Angelis377a1552018-11-22 17:02:40 +00003127 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3128 * \retval #PSA_ERROR_INVALID_PADDING
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003129 * \retval #PSA_ERROR_BAD_STATE
3130 * The library has not been previously initialized by psa_crypto_init().
3131 * It is implementation-dependent whether a failure to initialize
3132 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003133 */
Maulik Patel28659c42021-01-06 14:09:22 +00003134psa_status_t psa_asymmetric_decrypt(psa_key_id_t key,
Antonio de Angelis14276e92018-07-10 14:35:43 +01003135 psa_algorithm_t alg,
3136 const uint8_t *input,
3137 size_t input_length,
3138 const uint8_t *salt,
3139 size_t salt_length,
3140 uint8_t *output,
3141 size_t output_size,
3142 size_t *output_length);
3143
3144/**@}*/
3145
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003146/** \defgroup key_derivation Key derivation and pseudorandom generation
Antonio de Angelis377a1552018-11-22 17:02:40 +00003147 * @{
3148 */
3149
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003150/** The type of the state data structure for key derivation operations.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003151 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003152 * Before calling any function on a key derivation operation object, the
3153 * application must initialize it by any of the following means:
Antonio de Angelis377a1552018-11-22 17:02:40 +00003154 * - Set the structure to all-bits-zero, for example:
3155 * \code
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003156 * psa_key_derivation_operation_t operation;
3157 * memset(&operation, 0, sizeof(operation));
Antonio de Angelis377a1552018-11-22 17:02:40 +00003158 * \endcode
3159 * - Initialize the structure to logical zero values, for example:
3160 * \code
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003161 * psa_key_derivation_operation_t operation = {0};
Antonio de Angelis377a1552018-11-22 17:02:40 +00003162 * \endcode
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003163 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Antonio de Angelis377a1552018-11-22 17:02:40 +00003164 * for example:
3165 * \code
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003166 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Antonio de Angelis377a1552018-11-22 17:02:40 +00003167 * \endcode
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003168 * - Assign the result of the function psa_key_derivation_operation_init()
Antonio de Angelis377a1552018-11-22 17:02:40 +00003169 * to the structure, for example:
3170 * \code
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003171 * psa_key_derivation_operation_t operation;
3172 * operation = psa_key_derivation_operation_init();
Antonio de Angelis377a1552018-11-22 17:02:40 +00003173 * \endcode
3174 *
3175 * This is an implementation-defined \c struct. Applications should not
Summer Qin359167d2021-07-05 18:11:50 +08003176 * make any assumptions about the content of this structure.
3177 * Implementation details can change in future versions without notice.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003178 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003179typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Antonio de Angelis377a1552018-11-22 17:02:40 +00003180
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003181/** Return an initial value for a key derivation operation object.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003182 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003183static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Antonio de Angelis377a1552018-11-22 17:02:40 +00003184
3185/** Set up a key derivation operation.
3186 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003187 * A key derivation algorithm takes some inputs and uses them to generate
3188 * a byte stream in a deterministic way.
3189 * This byte stream can be used to produce keys and other
3190 * cryptographic material.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003191 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003192 * To derive a key:
3193 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3194 * -# Call psa_key_derivation_setup() to select the algorithm.
3195 * -# Provide the inputs for the key derivation by calling
3196 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3197 * as appropriate. Which inputs are needed, in what order, and whether
3198 * they may be keys and if so of what type depends on the algorithm.
3199 * -# Optionally set the operation's maximum capacity with
3200 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3201 * of or after providing inputs. For some algorithms, this step is mandatory
3202 * because the output depends on the maximum capacity.
3203 * -# To derive a key, call psa_key_derivation_output_key().
3204 * To derive a byte string for a different purpose, call
3205 * psa_key_derivation_output_bytes().
3206 * Successive calls to these functions use successive output bytes
3207 * calculated by the key derivation algorithm.
3208 * -# Clean up the key derivation operation object with
3209 * psa_key_derivation_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00003210 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003211 * If this function returns an error, the key derivation operation object is
3212 * not changed.
3213 *
3214 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3215 * the operation will need to be reset by a call to psa_key_derivation_abort().
3216 *
3217 * Implementations must reject an attempt to derive a key of size 0.
3218 *
3219 * \param[in,out] operation The key derivation operation object
3220 * to set up. It must
3221 * have been initialized but not set up yet.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003222 * \param alg The key derivation algorithm to compute
3223 * (\c PSA_ALG_XXX value such that
3224 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +00003225 *
3226 * \retval #PSA_SUCCESS
3227 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003228 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003229 * \c alg is not a key derivation algorithm.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003230 * \retval #PSA_ERROR_NOT_SUPPORTED
3231 * \c alg is not supported or is not a key derivation algorithm.
3232 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3233 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3234 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003235 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3236 * \retval #PSA_ERROR_STORAGE_FAILURE
3237 * \retval #PSA_ERROR_BAD_STATE
3238 * The operation state is not valid (it must be inactive).
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003239 * \retval #PSA_ERROR_BAD_STATE
3240 * The library has not been previously initialized by psa_crypto_init().
3241 * It is implementation-dependent whether a failure to initialize
3242 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003243 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003244psa_status_t psa_key_derivation_setup(
3245 psa_key_derivation_operation_t *operation,
3246 psa_algorithm_t alg);
Antonio de Angelis377a1552018-11-22 17:02:40 +00003247
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003248/** Retrieve the current capacity of a key derivation operation.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003249 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003250 * The capacity of a key derivation is the maximum number of bytes that it can
3251 * return. When you get *N* bytes of output from a key derivation operation,
3252 * this reduces its capacity by *N*.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003253 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003254 * \param[in] operation The operation to query.
3255 * \param[out] capacity On success, the capacity of the operation.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003256 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003257 * \retval #PSA_SUCCESS
3258 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3259 * \retval #PSA_ERROR_BAD_STATE
3260 * The operation state is not valid (it must be active).
3261 * \retval #PSA_ERROR_HARDWARE_FAILURE
3262 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3263 * \retval #PSA_ERROR_BAD_STATE
3264 * The library has not been previously initialized by psa_crypto_init().
3265 * It is implementation-dependent whether a failure to initialize
3266 * results in this error code.
3267 */
3268psa_status_t psa_key_derivation_get_capacity(
3269 const psa_key_derivation_operation_t *operation,
3270 size_t *capacity);
3271
3272/** Set the maximum capacity of a key derivation operation.
3273 *
3274 * The capacity of a key derivation operation is the maximum number of bytes
3275 * that the key derivation operation can return from this point onwards.
3276 *
3277 * \param[in,out] operation The key derivation operation object to modify.
3278 * \param capacity The new capacity of the operation.
3279 * It must be less or equal to the operation's
3280 * current capacity.
3281 *
3282 * \retval #PSA_SUCCESS
3283 * \retval #PSA_ERROR_INVALID_ARGUMENT
3284 * \p capacity is larger than the operation's current capacity.
3285 * In this case, the operation object remains valid and its capacity
3286 * remains unchanged.
3287 * \retval #PSA_ERROR_BAD_STATE
3288 * The operation state is not valid (it must be active).
3289 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3290 * \retval #PSA_ERROR_HARDWARE_FAILURE
3291 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3292 * \retval #PSA_ERROR_BAD_STATE
3293 * The library has not been previously initialized by psa_crypto_init().
3294 * It is implementation-dependent whether a failure to initialize
3295 * results in this error code.
3296 */
3297psa_status_t psa_key_derivation_set_capacity(
3298 psa_key_derivation_operation_t *operation,
3299 size_t capacity);
3300
3301/** Use the maximum possible capacity for a key derivation operation.
3302 *
3303 * Use this value as the capacity argument when setting up a key derivation
3304 * to indicate that the operation should have the maximum possible capacity.
3305 * The value of the maximum possible capacity depends on the key derivation
3306 * algorithm.
3307 */
3308#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3309
3310/** Provide an input for key derivation or key agreement.
3311 *
3312 * Which inputs are required and in what order depends on the algorithm.
3313 * Refer to the documentation of each key derivation or key agreement
3314 * algorithm for information.
3315 *
3316 * This function passes direct inputs, which is usually correct for
3317 * non-secret inputs. To pass a secret input, which should be in a key
3318 * object, call psa_key_derivation_input_key() instead of this function.
3319 * Refer to the documentation of individual step types
3320 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3321 * for more information.
3322 *
3323 * If this function returns an error status, the operation enters an error
3324 * state and must be aborted by calling psa_key_derivation_abort().
3325 *
3326 * \param[in,out] operation The key derivation operation object to use.
3327 * It must have been set up with
3328 * psa_key_derivation_setup() and must not
3329 * have produced any output yet.
3330 * \param step Which step the input data is for.
3331 * \param[in] data Input data to use.
3332 * \param data_length Size of the \p data buffer in bytes.
3333 *
3334 * \retval #PSA_SUCCESS
3335 * Success.
3336 * \retval #PSA_ERROR_INVALID_ARGUMENT
3337 * \c step is not compatible with the operation's algorithm.
3338 * \retval #PSA_ERROR_INVALID_ARGUMENT
3339 * \c step does not allow direct inputs.
3340 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3341 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3342 * \retval #PSA_ERROR_HARDWARE_FAILURE
3343 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3344 * \retval #PSA_ERROR_STORAGE_FAILURE
3345 * \retval #PSA_ERROR_BAD_STATE
3346 * The operation state is not valid for this input \p step.
3347 * \retval #PSA_ERROR_BAD_STATE
3348 * The library has not been previously initialized by psa_crypto_init().
3349 * It is implementation-dependent whether a failure to initialize
3350 * results in this error code.
3351 */
3352psa_status_t psa_key_derivation_input_bytes(
3353 psa_key_derivation_operation_t *operation,
3354 psa_key_derivation_step_t step,
3355 const uint8_t *data,
3356 size_t data_length);
3357
Summer Qin359167d2021-07-05 18:11:50 +08003358/** Provide a numeric input for key derivation or key agreement.
3359 *
3360 * Which inputs are required and in what order depends on the algorithm.
3361 * However, when an algorithm requires a particular order, numeric inputs
3362 * usually come first as they tend to be configuration parameters.
3363 * Refer to the documentation of each key derivation or key agreement
3364 * algorithm for information.
3365 *
3366 * This function is used for inputs which are fixed-size non-negative
3367 * integers.
3368 *
3369 * If this function returns an error status, the operation enters an error
3370 * state and must be aborted by calling psa_key_derivation_abort().
3371 *
3372 * \param[in,out] operation The key derivation operation object to use.
3373 * It must have been set up with
3374 * psa_key_derivation_setup() and must not
3375 * have produced any output yet.
3376 * \param step Which step the input data is for.
3377 * \param[in] value The value of the numeric input.
3378 *
3379 * \retval #PSA_SUCCESS
3380 * Success.
3381 * \retval #PSA_ERROR_INVALID_ARGUMENT
3382 * \c step is not compatible with the operation's algorithm.
3383 * \retval #PSA_ERROR_INVALID_ARGUMENT
3384 * \c step does not allow numeric inputs.
3385 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3386 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3387 * \retval #PSA_ERROR_HARDWARE_FAILURE
3388 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3389 * \retval #PSA_ERROR_STORAGE_FAILURE
3390 * \retval #PSA_ERROR_BAD_STATE
3391 * The operation state is not valid for this input \p step.
3392 * \retval #PSA_ERROR_BAD_STATE
3393 * The library has not been previously initialized by psa_crypto_init().
3394 * It is implementation-dependent whether a failure to initialize
3395 * results in this error code.
3396 */
3397psa_status_t psa_key_derivation_input_integer(
3398 psa_key_derivation_operation_t *operation,
3399 psa_key_derivation_step_t step,
3400 uint64_t value);
3401
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003402/** Provide an input for key derivation in the form of a key.
3403 *
3404 * Which inputs are required and in what order depends on the algorithm.
3405 * Refer to the documentation of each key derivation or key agreement
3406 * algorithm for information.
3407 *
3408 * This function obtains input from a key object, which is usually correct for
3409 * secret inputs or for non-secret personalization strings kept in the key
3410 * store. To pass a non-secret parameter which is not in the key store,
3411 * call psa_key_derivation_input_bytes() instead of this function.
3412 * Refer to the documentation of individual step types
3413 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3414 * for more information.
3415 *
3416 * If this function returns an error status, the operation enters an error
3417 * state and must be aborted by calling psa_key_derivation_abort().
3418 *
3419 * \param[in,out] operation The key derivation operation object to use.
3420 * It must have been set up with
3421 * psa_key_derivation_setup() and must not
3422 * have produced any output yet.
3423 * \param step Which step the input data is for.
Maulik Patel28659c42021-01-06 14:09:22 +00003424 * \param key Identifier of the key. It must have an
3425 * appropriate type for step and must allow the
Summer Qin359167d2021-07-05 18:11:50 +08003426 * usage #PSA_KEY_USAGE_DERIVE or
3427 * #PSA_KEY_USAGE_VERIFY_DERIVATION (see note)
3428 * and the algorithm used by the operation.
3429 *
3430 * \note Once all inputs steps are completed, the operations will allow:
3431 * - psa_key_derivation_output_bytes() if each input was either a direct input
3432 * or a key with #PSA_KEY_USAGE_DERIVE set;
3433 * - psa_key_derivation_output_key() if the input for step
3434 * #PSA_KEY_DERIVATION_INPUT_SECRET or #PSA_KEY_DERIVATION_INPUT_PASSWORD
3435 * was from a key slot with #PSA_KEY_USAGE_DERIVE and each other input was
3436 * either a direct input or a key with #PSA_KEY_USAGE_DERIVE set;
3437 * - psa_key_derivation_verify_bytes() if each input was either a direct input
3438 * or a key with #PSA_KEY_USAGE_VERIFY_DERIVATION set;
3439 * - psa_key_derivation_verify_key() under the same conditions as
3440 * psa_key_derivation_verify_bytes().
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003441 *
3442 * \retval #PSA_SUCCESS
3443 * Success.
3444 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003445 * \retval #PSA_ERROR_NOT_PERMITTED
Summer Qin359167d2021-07-05 18:11:50 +08003446 * The key allows neither #PSA_KEY_USAGE_DERIVE nor
3447 * #PSA_KEY_USAGE_VERIFY_DERIVATION, or it doesn't allow this
3448 * algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003449 * \retval #PSA_ERROR_INVALID_ARGUMENT
3450 * \c step is not compatible with the operation's algorithm.
3451 * \retval #PSA_ERROR_INVALID_ARGUMENT
3452 * \c step does not allow key inputs of the given type
3453 * or does not allow key inputs at all.
3454 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3455 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3456 * \retval #PSA_ERROR_HARDWARE_FAILURE
3457 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3458 * \retval #PSA_ERROR_STORAGE_FAILURE
3459 * \retval #PSA_ERROR_BAD_STATE
3460 * The operation state is not valid for this input \p step.
3461 * \retval #PSA_ERROR_BAD_STATE
3462 * The library has not been previously initialized by psa_crypto_init().
3463 * It is implementation-dependent whether a failure to initialize
3464 * results in this error code.
3465 */
3466psa_status_t psa_key_derivation_input_key(
3467 psa_key_derivation_operation_t *operation,
3468 psa_key_derivation_step_t step,
Maulik Patel28659c42021-01-06 14:09:22 +00003469 psa_key_id_t key);
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003470
3471/** Perform a key agreement and use the shared secret as input to a key
3472 * derivation.
3473 *
3474 * A key agreement algorithm takes two inputs: a private key \p private_key
3475 * a public key \p peer_key.
3476 * The result of this function is passed as input to a key derivation.
3477 * The output of this key derivation can be extracted by reading from the
3478 * resulting operation to produce keys and other cryptographic material.
3479 *
3480 * If this function returns an error status, the operation enters an error
3481 * state and must be aborted by calling psa_key_derivation_abort().
3482 *
3483 * \param[in,out] operation The key derivation operation object to use.
3484 * It must have been set up with
3485 * psa_key_derivation_setup() with a
3486 * key agreement and derivation algorithm
3487 * \c alg (\c PSA_ALG_XXX value such that
3488 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3489 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3490 * is false).
3491 * The operation must be ready for an
3492 * input of the type given by \p step.
3493 * \param step Which step the input data is for.
Maulik Patel28659c42021-01-06 14:09:22 +00003494 * \param private_key Identifier of the private key to use. It must
3495 * allow the usage #PSA_KEY_USAGE_DERIVE.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003496 * \param[in] peer_key Public key of the peer. The peer key must be in the
3497 * same format that psa_import_key() accepts for the
3498 * public key type corresponding to the type of
3499 * private_key. That is, this function performs the
3500 * equivalent of
3501 * #psa_import_key(...,
3502 * `peer_key`, `peer_key_length`) where
3503 * with key attributes indicating the public key
3504 * type corresponding to the type of `private_key`.
3505 * For example, for EC keys, this means that peer_key
3506 * is interpreted as a point on the curve that the
3507 * private key is on. The standard formats for public
3508 * keys are documented in the documentation of
3509 * psa_export_public_key().
3510 * \param peer_key_length Size of \p peer_key in bytes.
3511 *
3512 * \retval #PSA_SUCCESS
3513 * Success.
3514 * \retval #PSA_ERROR_BAD_STATE
3515 * The operation state is not valid for this key agreement \p step.
3516 * \retval #PSA_ERROR_INVALID_HANDLE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003517 * \retval #PSA_ERROR_NOT_PERMITTED
3518 * \retval #PSA_ERROR_INVALID_ARGUMENT
3519 * \c private_key is not compatible with \c alg,
3520 * or \p peer_key is not valid for \c alg or not compatible with
3521 * \c private_key.
3522 * \retval #PSA_ERROR_NOT_SUPPORTED
3523 * \c alg is not supported or is not a key derivation algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003524 * \retval #PSA_ERROR_INVALID_ARGUMENT
3525 * \c step does not allow an input resulting from a key agreement.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003526 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3527 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3528 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003529 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3530 * \retval #PSA_ERROR_STORAGE_FAILURE
3531 * \retval #PSA_ERROR_BAD_STATE
3532 * The library has not been previously initialized by psa_crypto_init().
3533 * It is implementation-dependent whether a failure to initialize
3534 * results in this error code.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003535 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003536psa_status_t psa_key_derivation_key_agreement(
3537 psa_key_derivation_operation_t *operation,
3538 psa_key_derivation_step_t step,
Maulik Patel28659c42021-01-06 14:09:22 +00003539 psa_key_id_t private_key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003540 const uint8_t *peer_key,
3541 size_t peer_key_length);
3542
3543/** Read some data from a key derivation operation.
3544 *
3545 * This function calculates output bytes from a key derivation algorithm and
3546 * return those bytes.
3547 * If you view the key derivation's output as a stream of bytes, this
3548 * function destructively reads the requested number of bytes from the
3549 * stream.
3550 * The operation's capacity decreases by the number of bytes read.
3551 *
3552 * If this function returns an error status other than
3553 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
3554 * state and must be aborted by calling psa_key_derivation_abort().
3555 *
3556 * \param[in,out] operation The key derivation operation object to read from.
3557 * \param[out] output Buffer where the output will be written.
3558 * \param output_length Number of bytes to output.
3559 *
3560 * \retval #PSA_SUCCESS
Summer Qin359167d2021-07-05 18:11:50 +08003561 * \retval #PSA_ERROR_NOT_PERMITTED
3562 * One of the inputs was a key whose policy didn't allow
3563 * #PSA_KEY_USAGE_DERIVE.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003564 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3565 * The operation's capacity was less than
3566 * \p output_length bytes. Note that in this case,
3567 * no output is written to the output buffer.
3568 * The operation's capacity is set to 0, thus
3569 * subsequent calls to this function will not
3570 * succeed, even with a smaller output buffer.
3571 * \retval #PSA_ERROR_BAD_STATE
3572 * The operation state is not valid (it must be active and completed
3573 * all required input steps).
3574 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3575 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3576 * \retval #PSA_ERROR_HARDWARE_FAILURE
3577 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3578 * \retval #PSA_ERROR_STORAGE_FAILURE
3579 * \retval #PSA_ERROR_BAD_STATE
3580 * The library has not been previously initialized by psa_crypto_init().
3581 * It is implementation-dependent whether a failure to initialize
3582 * results in this error code.
3583 */
3584psa_status_t psa_key_derivation_output_bytes(
3585 psa_key_derivation_operation_t *operation,
3586 uint8_t *output,
3587 size_t output_length);
3588
3589/** Derive a key from an ongoing key derivation operation.
3590 *
3591 * This function calculates output bytes from a key derivation algorithm
3592 * and uses those bytes to generate a key deterministically.
3593 * The key's location, usage policy, type and size are taken from
3594 * \p attributes.
3595 *
3596 * If you view the key derivation's output as a stream of bytes, this
3597 * function destructively reads as many bytes as required from the
3598 * stream.
3599 * The operation's capacity decreases by the number of bytes read.
3600 *
3601 * If this function returns an error status other than
3602 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
3603 * state and must be aborted by calling psa_key_derivation_abort().
3604 *
3605 * How much output is produced and consumed from the operation, and how
Summer Qin359167d2021-07-05 18:11:50 +08003606 * the key is derived, depends on the key type and on the key size
3607 * (denoted \c bits below):
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003608 *
3609 * - For key types for which the key is an arbitrary sequence of bytes
3610 * of a given size, this function is functionally equivalent to
3611 * calling #psa_key_derivation_output_bytes
3612 * and passing the resulting output to #psa_import_key.
3613 * However, this function has a security benefit:
3614 * if the implementation provides an isolation boundary then
3615 * the key material is not exposed outside the isolation boundary.
3616 * As a consequence, for these key types, this function always consumes
Summer Qin359167d2021-07-05 18:11:50 +08003617 * exactly (\c bits / 8) bytes from the operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003618 * The following key types defined in this specification follow this scheme:
3619 *
3620 * - #PSA_KEY_TYPE_AES;
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003621 * - #PSA_KEY_TYPE_CAMELLIA;
3622 * - #PSA_KEY_TYPE_DERIVE;
Summer Qin359167d2021-07-05 18:11:50 +08003623 * - #PSA_KEY_TYPE_HMAC;
3624 * - #PSA_KEY_TYPE_PASSWORD_HASH.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003625 *
3626 * - For ECC keys on a Montgomery elliptic curve
3627 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
3628 * Montgomery curve), this function always draws a byte string whose
3629 * length is determined by the curve, and sets the mandatory bits
3630 * accordingly. That is:
3631 *
Summer Qin0e5b2e02020-10-22 11:23:39 +08003632 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Soby Mathew07ef6e42020-07-20 21:09:23 +01003633 * string and process it as specified in RFC 7748 &sect;5.
Summer Qin0e5b2e02020-10-22 11:23:39 +08003634 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Soby Mathew07ef6e42020-07-20 21:09:23 +01003635 * string and process it as specified in RFC 7748 &sect;5.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003636 *
3637 * - For key types for which the key is represented by a single sequence of
Summer Qin359167d2021-07-05 18:11:50 +08003638 * \c bits bits with constraints as to which bit sequences are acceptable,
3639 * this function draws a byte string of length (\c bits / 8) bytes rounded
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003640 * up to the nearest whole number of bytes. If the resulting byte string
3641 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3642 * This process is repeated until an acceptable byte string is drawn.
3643 * The byte string drawn from the operation is interpreted as specified
3644 * for the output produced by psa_export_key().
3645 * The following key types defined in this specification follow this scheme:
3646 *
3647 * - #PSA_KEY_TYPE_DES.
3648 * Force-set the parity bits, but discard forbidden weak keys.
3649 * For 2-key and 3-key triple-DES, the three keys are generated
3650 * successively (for example, for 3-key triple-DES,
3651 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3652 * discard the first 8 bytes, use the next 8 bytes as the first key,
3653 * and continue reading output from the operation to derive the other
3654 * two keys).
3655 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
3656 * where \c group designates any Diffie-Hellman group) and
3657 * ECC keys on a Weierstrass elliptic curve
3658 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
3659 * Weierstrass curve).
3660 * For these key types, interpret the byte string as integer
3661 * in big-endian order. Discard it if it is not in the range
3662 * [0, *N* - 2] where *N* is the boundary of the private key domain
3663 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
3664 * or the order of the curve's base point for ECC).
3665 * Add 1 to the resulting integer and use this as the private key *x*.
3666 * This method allows compliance to NIST standards, specifically
3667 * the methods titled "key-pair generation by testing candidates"
3668 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3669 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3670 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3671 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
3672 *
3673 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
3674 * the way in which the operation output is consumed is
3675 * implementation-defined.
3676 *
3677 * In all cases, the data that is read is discarded from the operation.
3678 * The operation's capacity is decreased by the number of bytes read.
3679 *
3680 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3681 * the input to that step must be provided with psa_key_derivation_input_key().
3682 * Future versions of this specification may include additional restrictions
3683 * on the derived key based on the attributes and strength of the secret key.
3684 *
3685 * \param[in] attributes The attributes for the new key.
Summer Qin359167d2021-07-05 18:11:50 +08003686 * If the key type to be created is
3687 * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in
3688 * the policy must be the same as in the current
3689 * operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003690 * \param[in,out] operation The key derivation operation object to read from.
Maulik Patel28659c42021-01-06 14:09:22 +00003691 * \param[out] key On success, an identifier for the newly created
3692 * key. For persistent keys, this is the key
3693 * identifier defined in \p attributes.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003694 * \c 0 on failure.
3695 *
3696 * \retval #PSA_SUCCESS
3697 * Success.
3698 * If the key is persistent, the key material and the key's metadata
3699 * have been saved to persistent storage.
3700 * \retval #PSA_ERROR_ALREADY_EXISTS
3701 * This is an attempt to create a persistent key, and there is
3702 * already a persistent key with the given identifier.
3703 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3704 * There was not enough data to create the desired key.
3705 * Note that in this case, no output is written to the output buffer.
3706 * The operation's capacity is set to 0, thus subsequent calls to
3707 * this function will not succeed, even with a smaller output buffer.
3708 * \retval #PSA_ERROR_NOT_SUPPORTED
3709 * The key type or key size is not supported, either by the
3710 * implementation in general or in this particular location.
3711 * \retval #PSA_ERROR_INVALID_ARGUMENT
3712 * The provided key attributes are not valid for the operation.
3713 * \retval #PSA_ERROR_NOT_PERMITTED
Summer Qin359167d2021-07-05 18:11:50 +08003714 * The #PSA_KEY_DERIVATION_INPUT_SECRET or
3715 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a
3716 * key; or one of the inputs was a key whose policy didn't allow
3717 * #PSA_KEY_USAGE_DERIVE.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003718 * \retval #PSA_ERROR_BAD_STATE
3719 * The operation state is not valid (it must be active and completed
3720 * all required input steps).
3721 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3722 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3723 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3724 * \retval #PSA_ERROR_HARDWARE_FAILURE
3725 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Maulik Patel13b27cf2021-05-14 11:44:53 +01003726 * \retval #PSA_ERROR_DATA_INVALID
3727 * \retval #PSA_ERROR_DATA_CORRUPT
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003728 * \retval #PSA_ERROR_STORAGE_FAILURE
3729 * \retval #PSA_ERROR_BAD_STATE
3730 * The library has not been previously initialized by psa_crypto_init().
3731 * It is implementation-dependent whether a failure to initialize
3732 * results in this error code.
3733 */
3734psa_status_t psa_key_derivation_output_key(
3735 const psa_key_attributes_t *attributes,
3736 psa_key_derivation_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00003737 psa_key_id_t *key);
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003738
Summer Qin359167d2021-07-05 18:11:50 +08003739/** Compare output data from a key derivation operation to an expected value.
3740 *
3741 * This function calculates output bytes from a key derivation algorithm and
3742 * compares those bytes to an expected value in constant time.
3743 * If you view the key derivation's output as a stream of bytes, this
3744 * function destructively reads the expected number of bytes from the
3745 * stream before comparing them.
3746 * The operation's capacity decreases by the number of bytes read.
3747 *
3748 * This is functionally equivalent to the following code:
3749 * \code
3750 * psa_key_derivation_output_bytes(operation, tmp, output_length);
3751 * if (memcmp(output, tmp, output_length) != 0)
3752 * return PSA_ERROR_INVALID_SIGNATURE;
3753 * \endcode
3754 * except (1) it works even if the key's policy does not allow outputting the
3755 * bytes, and (2) the comparison will be done in constant time.
3756 *
3757 * If this function returns an error status other than
3758 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
3759 * the operation enters an error state and must be aborted by calling
3760 * psa_key_derivation_abort().
3761 *
3762 * \param[in,out] operation The key derivation operation object to read from.
3763 * \param[in] expected_output Buffer containing the expected derivation output.
3764 * \param output_length Length ot the expected output; this is also the
3765 * number of bytes that will be read.
3766 *
3767 * \retval #PSA_SUCCESS
3768 * \retval #PSA_ERROR_INVALID_SIGNATURE
3769 * The output was read successfully, but it differs from the expected
3770 * output.
3771 * \retval #PSA_ERROR_NOT_PERMITTED
3772 * One of the inputs was a key whose policy didn't allow
3773 * #PSA_KEY_USAGE_VERIFY_DERIVATION.
3774 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3775 * The operation's capacity was less than
3776 * \p output_length bytes. Note that in this case,
3777 * the operation's capacity is set to 0, thus
3778 * subsequent calls to this function will not
3779 * succeed, even with a smaller expected output.
3780 * \retval #PSA_ERROR_BAD_STATE
3781 * The operation state is not valid (it must be active and completed
3782 * all required input steps).
3783 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3784 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3785 * \retval #PSA_ERROR_HARDWARE_FAILURE
3786 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3787 * \retval #PSA_ERROR_STORAGE_FAILURE
3788 * \retval #PSA_ERROR_BAD_STATE
3789 * The library has not been previously initialized by psa_crypto_init().
3790 * It is implementation-dependent whether a failure to initialize
3791 * results in this error code.
3792 */
3793psa_status_t psa_key_derivation_verify_bytes(
3794 psa_key_derivation_operation_t *operation,
3795 const uint8_t *expected_output,
3796 size_t output_length);
3797
3798/** Compare output data from a key derivation operation to an expected value
3799 * stored in a key object.
3800 *
3801 * This function calculates output bytes from a key derivation algorithm and
3802 * compares those bytes to an expected value, provided as key of type
3803 * #PSA_KEY_TYPE_PASSWORD_HASH.
3804 * If you view the key derivation's output as a stream of bytes, this
3805 * function destructively reads the number of bytes corresponding the the
3806 * length of the expected value from the stream before comparing them.
3807 * The operation's capacity decreases by the number of bytes read.
3808 *
3809 * This is functionally equivalent to exporting the key and calling
3810 * psa_key_derivation_verify_bytes() on the result, except that it
3811 * works even if the key cannot be exported.
3812 *
3813 * If this function returns an error status other than
3814 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
3815 * the operation enters an error state and must be aborted by calling
3816 * psa_key_derivation_abort().
3817 *
3818 * \param[in,out] operation The key derivation operation object to read from.
3819 * \param[in] expected A key of type #PSA_KEY_TYPE_PASSWORD_HASH
3820 * containing the expected output. Its policy must
3821 * include the #PSA_KEY_USAGE_VERIFY_DERIVATION flag
3822 * and the permitted algorithm must match the
3823 * operation. The value of this key was likely
3824 * computed by a previous call to
3825 * psa_key_derivation_output_key().
3826 *
3827 * \retval #PSA_SUCCESS
3828 * \retval #PSA_ERROR_INVALID_SIGNATURE
3829 * The output was read successfully, but if differs from the expected
3830 * output.
3831 * \retval #PSA_ERROR_INVALID_HANDLE
3832 * The key passed as the expected value does not exist.
3833 * \retval #PSA_ERROR_INVALID_ARGUMENT
3834 * The key passed as the expected value has an invalid type.
3835 * \retval #PSA_ERROR_NOT_PERMITTED
3836 * The key passed as the expected value does not allow this usage or
3837 * this algorithm; or one of the inputs was a key whose policy didn't
3838 * allow #PSA_KEY_USAGE_VERIFY_DERIVATION.
3839 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3840 * The operation's capacity was less than
3841 * the length of the expected value. In this case,
3842 * the operation's capacity is set to 0, thus
3843 * subsequent calls to this function will not
3844 * succeed, even with a smaller expected output.
3845 * \retval #PSA_ERROR_BAD_STATE
3846 * The operation state is not valid (it must be active and completed
3847 * all required input steps).
3848 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3849 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3850 * \retval #PSA_ERROR_HARDWARE_FAILURE
3851 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3852 * \retval #PSA_ERROR_STORAGE_FAILURE
3853 * \retval #PSA_ERROR_BAD_STATE
3854 * The library has not been previously initialized by psa_crypto_init().
3855 * It is implementation-dependent whether a failure to initialize
3856 * results in this error code.
3857 */
3858psa_status_t psa_key_derivation_verify_key(
3859 psa_key_derivation_operation_t *operation,
3860 psa_key_id_t expected);
3861
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003862/** Abort a key derivation operation.
3863 *
3864 * Aborting an operation frees all associated resources except for the \c
3865 * operation structure itself. Once aborted, the operation object can be reused
3866 * for another operation by calling psa_key_derivation_setup() again.
3867 *
3868 * This function may be called at any time after the operation
3869 * object has been initialized as described in #psa_key_derivation_operation_t.
3870 *
3871 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3872 * call psa_key_derivation_abort() on an operation that has not been set up.
3873 *
3874 * \param[in,out] operation The operation to abort.
3875 *
3876 * \retval #PSA_SUCCESS
3877 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3878 * \retval #PSA_ERROR_HARDWARE_FAILURE
3879 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3880 * \retval #PSA_ERROR_BAD_STATE
3881 * The library has not been previously initialized by psa_crypto_init().
3882 * It is implementation-dependent whether a failure to initialize
3883 * results in this error code.
3884 */
3885psa_status_t psa_key_derivation_abort(
3886 psa_key_derivation_operation_t *operation);
3887
3888/** Perform a key agreement and return the raw shared secret.
3889 *
3890 * \warning The raw result of a key agreement algorithm such as finite-field
3891 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3892 * not be used directly as key material. It should instead be passed as
3893 * input to a key derivation algorithm. To chain a key agreement with
3894 * a key derivation, use psa_key_derivation_key_agreement() and other
3895 * functions from the key derivation interface.
3896 *
3897 * \param alg The key agreement algorithm to compute
3898 * (\c PSA_ALG_XXX value such that
3899 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3900 * is true).
Maulik Patel28659c42021-01-06 14:09:22 +00003901 * \param private_key Identifier of the private key to use. It must
3902 * allow the usage #PSA_KEY_USAGE_DERIVE.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003903 * \param[in] peer_key Public key of the peer. It must be
3904 * in the same format that psa_import_key()
3905 * accepts. The standard formats for public
3906 * keys are documented in the documentation
3907 * of psa_export_public_key().
3908 * \param peer_key_length Size of \p peer_key in bytes.
3909 * \param[out] output Buffer where the decrypted message is to
3910 * be written.
3911 * \param output_size Size of the \c output buffer in bytes.
3912 * \param[out] output_length On success, the number of bytes
3913 * that make up the returned output.
3914 *
3915 * \retval #PSA_SUCCESS
3916 * Success.
3917 * \retval #PSA_ERROR_INVALID_HANDLE
3918 * \retval #PSA_ERROR_NOT_PERMITTED
3919 * \retval #PSA_ERROR_INVALID_ARGUMENT
3920 * \p alg is not a key agreement algorithm
3921 * \retval #PSA_ERROR_INVALID_ARGUMENT
3922 * \p private_key is not compatible with \p alg,
3923 * or \p peer_key is not valid for \p alg or not compatible with
3924 * \p private_key.
3925 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3926 * \p output_size is too small
3927 * \retval #PSA_ERROR_NOT_SUPPORTED
3928 * \p alg is not a supported key agreement algorithm.
3929 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3930 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3931 * \retval #PSA_ERROR_HARDWARE_FAILURE
3932 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3933 * \retval #PSA_ERROR_STORAGE_FAILURE
3934 * \retval #PSA_ERROR_BAD_STATE
3935 * The library has not been previously initialized by psa_crypto_init().
3936 * It is implementation-dependent whether a failure to initialize
3937 * results in this error code.
3938 */
3939psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Maulik Patel28659c42021-01-06 14:09:22 +00003940 psa_key_id_t private_key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003941 const uint8_t *peer_key,
3942 size_t peer_key_length,
3943 uint8_t *output,
3944 size_t output_size,
3945 size_t *output_length);
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003946
Antonio de Angelis377a1552018-11-22 17:02:40 +00003947/**@}*/
3948
3949/** \defgroup random Random generation
Antonio de Angelis14276e92018-07-10 14:35:43 +01003950 * @{
3951 */
3952
3953/**
3954 * \brief Generate random bytes.
3955 *
3956 * \warning This function **can** fail! Callers MUST check the return status
3957 * and MUST NOT use the content of the output buffer if the return
3958 * status is not #PSA_SUCCESS.
3959 *
3960 * \note To generate a key, use psa_generate_key() instead.
3961 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00003962 * \param[out] output Output buffer for the generated data.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003963 * \param output_size Number of bytes to generate and output.
3964 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00003965 * \retval #PSA_SUCCESS
3966 * \retval #PSA_ERROR_NOT_SUPPORTED
3967 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003968 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Antonio de Angelis377a1552018-11-22 17:02:40 +00003969 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3970 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003971 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003972 * \retval #PSA_ERROR_BAD_STATE
3973 * The library has not been previously initialized by psa_crypto_init().
3974 * It is implementation-dependent whether a failure to initialize
3975 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003976 */
3977psa_status_t psa_generate_random(uint8_t *output,
3978 size_t output_size);
3979
3980/**
3981 * \brief Generate a key or key pair.
3982 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003983 * The key is generated randomly.
3984 * Its location, usage policy, type and size are taken from \p attributes.
3985 *
3986 * Implementations must reject an attempt to generate a key of size 0.
3987 *
3988 * The following type-specific considerations apply:
3989 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
3990 * the public exponent is 65537.
3991 * The modulus is a product of two probabilistic primes
3992 * between 2^{n-1} and 2^n where n is the bit size specified in the
3993 * attributes.
3994 *
3995 * \param[in] attributes The attributes for the new key.
Maulik Patel28659c42021-01-06 14:09:22 +00003996 * \param[out] key On success, an identifier for the newly created
3997 * key. For persistent keys, this is the key
3998 * identifier defined in \p attributes.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003999 * \c 0 on failure.
Antonio de Angelis14276e92018-07-10 14:35:43 +01004000 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00004001 * \retval #PSA_SUCCESS
Jamie Fox0e54ebc2019-04-09 14:21:04 +01004002 * Success.
4003 * If the key is persistent, the key material and the key's metadata
4004 * have been saved to persistent storage.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01004005 * \retval #PSA_ERROR_ALREADY_EXISTS
Antonio de Angelis04debbd2019-10-14 12:12:52 +01004006 * This is an attempt to create a persistent key, and there is
4007 * already a persistent key with the given identifier.
Antonio de Angelis377a1552018-11-22 17:02:40 +00004008 * \retval #PSA_ERROR_NOT_SUPPORTED
4009 * \retval #PSA_ERROR_INVALID_ARGUMENT
4010 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
4011 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
4012 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
4013 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01004014 * \retval #PSA_ERROR_CORRUPTION_DETECTED
4015 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Maulik Patel13b27cf2021-05-14 11:44:53 +01004016 * \retval #PSA_ERROR_DATA_INVALID
4017 * \retval #PSA_ERROR_DATA_CORRUPT
Antonio de Angelis04debbd2019-10-14 12:12:52 +01004018 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01004019 * \retval #PSA_ERROR_BAD_STATE
4020 * The library has not been previously initialized by psa_crypto_init().
4021 * It is implementation-dependent whether a failure to initialize
4022 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01004023 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01004024psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Maulik Patel28659c42021-01-06 14:09:22 +00004025 psa_key_id_t *key);
Antonio de Angelis14276e92018-07-10 14:35:43 +01004026
4027/**@}*/
4028
4029#ifdef __cplusplus
4030}
4031#endif
4032
Antonio de Angelis377a1552018-11-22 17:02:40 +00004033/* The file "crypto_sizes.h" contains definitions for size calculation
4034 * macros whose definitions are implementation-specific. */
Jamie Foxcc31d402019-01-28 17:13:52 +00004035#include "psa/crypto_sizes.h"
Antonio de Angelis377a1552018-11-22 17:02:40 +00004036
Soby Mathewd7b79f22020-05-21 15:06:54 +01004037/* The file "crypto_client_struct.h" contains definitions for structures
4038 * whose definitions differ in the client view and the PSA server
4039 * implementation in TF-M. */
4040#include "psa/crypto_client_struct.h"
4041
4042
Antonio de Angelis377a1552018-11-22 17:02:40 +00004043/* The file "crypto_struct.h" contains definitions for
4044 * implementation-specific structs that are declared above. */
Jamie Foxcc31d402019-01-28 17:13:52 +00004045#include "psa/crypto_struct.h"
Antonio de Angelis377a1552018-11-22 17:02:40 +00004046
4047/* The file "crypto_extra.h" contains vendor-specific definitions. This
4048 * can include vendor-defined algorithms, extra functions, etc. */
Jamie Foxcc31d402019-01-28 17:13:52 +00004049#include "psa/crypto_extra.h"
Antonio de Angelis8908f472018-08-31 15:44:25 +01004050
Antonio de Angelis14276e92018-07-10 14:35:43 +01004051#endif /* PSA_CRYPTO_H */