blob: bb9d0caee5d060ae457d4402e6cd064f47217124 [file] [log] [blame]
Antonio de Angelis14276e92018-07-10 14:35:43 +01001/*
Summer Qin614002c2023-01-19 15:22:39 +08002 * Copyright (c) 2018-2023, 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 Angelis90bee0f2022-07-13 11:22:41 +010098/** \def PSA_KEY_ATTRIBUTES_INIT
99 *
100 * This macro returns a suitable initializer for a key attribute structure
101 * of type #psa_key_attributes_t.
102 */
103
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100104/** Return an initial value for a key attributes structure.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100105 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100106static psa_key_attributes_t psa_key_attributes_init(void);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100107
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100108/** Declare a key as persistent and set its key identifier.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100109 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100110 * If the attribute structure currently declares the key as volatile (which
111 * is the default content of an attribute structure), this function sets
112 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000113 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100114 * This function does not access storage, it merely stores the given
115 * value in the structure.
116 * The persistent key will be written to storage when the attribute
117 * structure is passed to a key creation function such as
118 * psa_import_key(), psa_generate_key(),
119 * psa_key_derivation_output_key() or psa_copy_key().
120 *
121 * This function may be declared as `static` (i.e. without external
122 * linkage). This function may be provided as a function-like macro,
123 * but in this case it must evaluate each of its arguments exactly once.
124 *
Maulik Patel28659c42021-01-06 14:09:22 +0000125 * \param[out] attributes The attribute structure to write to.
126 * \param key The persistent identifier for the key.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100127 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100128static void psa_set_key_id(psa_key_attributes_t *attributes,
Maulik Patel28659c42021-01-06 14:09:22 +0000129 psa_key_id_t key);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100130
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100131/** Set the location of a persistent key.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000132 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100133 * To make a key persistent, you must give it a persistent key identifier
134 * with psa_set_key_id(). By default, a key that has a persistent identifier
135 * is stored in the default storage area identifier by
136 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
137 * area, or to explicitly declare the key as volatile.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000138 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100139 * This function does not access storage, it merely stores the given
140 * value in the structure.
141 * The persistent key will be written to storage when the attribute
142 * structure is passed to a key creation function such as
143 * psa_import_key(), psa_generate_key(),
144 * psa_key_derivation_output_key() or psa_copy_key().
145 *
146 * This function may be declared as `static` (i.e. without external
147 * linkage). This function may be provided as a function-like macro,
148 * but in this case it must evaluate each of its arguments exactly once.
149 *
150 * \param[out] attributes The attribute structure to write to.
151 * \param lifetime The lifetime for the key.
152 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
153 * key will be volatile, and the key identifier
154 * attribute is reset to 0.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000155 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100156static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
157 psa_key_lifetime_t lifetime);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100158
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100159/** Retrieve the key identifier from key attributes.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000160 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100161 * This function may be declared as `static` (i.e. without external
162 * linkage). This function may be provided as a function-like macro,
163 * but in this case it must evaluate its argument exactly once.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000164 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100165 * \param[in] attributes The key attribute structure to query.
166 *
167 * \return The persistent identifier stored in the attribute structure.
168 * This value is unspecified if the attribute structure declares
169 * the key as volatile.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000170 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100171static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100172
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100173/** Retrieve the lifetime from key attributes.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100174 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100175 * This function may be declared as `static` (i.e. without external
176 * linkage). This function may be provided as a function-like macro,
177 * but in this case it must evaluate its argument exactly once.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100178 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100179 * \param[in] attributes The key attribute structure to query.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000180 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100181 * \return The lifetime value stored in the attribute structure.
182 */
183static psa_key_lifetime_t psa_get_key_lifetime(
184 const psa_key_attributes_t *attributes);
185
186/** Declare usage flags for a key.
187 *
188 * Usage flags are part of a key's usage policy. They encode what
189 * kind of operations are permitted on the key. For more details,
190 * refer to the documentation of the type #psa_key_usage_t.
191 *
192 * This function overwrites any usage flags
193 * previously set in \p attributes.
194 *
195 * This function may be declared as `static` (i.e. without external
196 * linkage). This function may be provided as a function-like macro,
197 * but in this case it must evaluate each of its arguments exactly once.
198 *
199 * \param[out] attributes The attribute structure to write to.
200 * \param usage_flags The usage flags to write.
201 */
202static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
203 psa_key_usage_t usage_flags);
204
205/** Retrieve the usage flags from key attributes.
206 *
207 * This function may be declared as `static` (i.e. without external
208 * linkage). This function may be provided as a function-like macro,
209 * but in this case it must evaluate its argument exactly once.
210 *
211 * \param[in] attributes The key attribute structure to query.
212 *
213 * \return The usage flags stored in the attribute structure.
214 */
215static psa_key_usage_t psa_get_key_usage_flags(
216 const psa_key_attributes_t *attributes);
217
218/** Declare the permitted algorithm policy for a key.
219 *
220 * The permitted algorithm policy of a key encodes which algorithm or
221 * algorithms are permitted to be used with this key. The following
222 * algorithm policies are supported:
223 * - 0 does not allow any cryptographic operation with the key. The key
224 * may be used for non-cryptographic actions such as exporting (if
225 * permitted by the usage flags).
226 * - An algorithm value permits this particular algorithm.
227 * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
228 * signature scheme with any hash algorithm.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100229 * - An algorithm built from #PSA_ALG_AT_LEAST_THIS_LENGTH_MAC allows
230 * any MAC algorithm from the same base class (e.g. CMAC) which
231 * generates/verifies a MAC length greater than or equal to the length
232 * encoded in the wildcard algorithm.
233 * - An algorithm built from #PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG
234 * allows any AEAD algorithm from the same base class (e.g. CCM) which
235 * generates/verifies a tag length greater than or equal to the length
236 * encoded in the wildcard algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100237 *
238 * This function overwrites any algorithm policy
239 * previously set in \p attributes.
240 *
241 * This function may be declared as `static` (i.e. without external
242 * linkage). This function may be provided as a function-like macro,
243 * but in this case it must evaluate each of its arguments exactly once.
244 *
245 * \param[out] attributes The attribute structure to write to.
246 * \param alg The permitted algorithm policy to write.
247 */
248static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
249 psa_algorithm_t alg);
250
251
252/** Retrieve the algorithm policy from key attributes.
253 *
254 * This function may be declared as `static` (i.e. without external
255 * linkage). This function may be provided as a function-like macro,
256 * but in this case it must evaluate its argument exactly once.
257 *
258 * \param[in] attributes The key attribute structure to query.
259 *
260 * \return The algorithm stored in the attribute structure.
261 */
262static psa_algorithm_t psa_get_key_algorithm(
263 const psa_key_attributes_t *attributes);
264
265/** Declare the type of a key.
266 *
267 * This function overwrites any key type
268 * previously set in \p attributes.
269 *
270 * This function may be declared as `static` (i.e. without external
271 * linkage). This function may be provided as a function-like macro,
272 * but in this case it must evaluate each of its arguments exactly once.
273 *
274 * \param[out] attributes The attribute structure to write to.
275 * \param type The key type to write.
276 * If this is 0, the key type in \p attributes
277 * becomes unspecified.
278 */
279static void psa_set_key_type(psa_key_attributes_t *attributes,
280 psa_key_type_t type);
281
282
283/** Declare the size of a key.
284 *
285 * This function overwrites any key size previously set in \p attributes.
286 *
287 * This function may be declared as `static` (i.e. without external
288 * linkage). This function may be provided as a function-like macro,
289 * but in this case it must evaluate each of its arguments exactly once.
290 *
291 * \param[out] attributes The attribute structure to write to.
292 * \param bits The key size in bits.
293 * If this is 0, the key size in \p attributes
294 * becomes unspecified. Keys of size 0 are
295 * not supported.
296 */
297static void psa_set_key_bits(psa_key_attributes_t *attributes,
298 size_t bits);
299
300/** Retrieve the key type from key attributes.
301 *
302 * This function may be declared as `static` (i.e. without external
303 * linkage). This function may be provided as a function-like macro,
304 * but in this case it must evaluate its argument exactly once.
305 *
306 * \param[in] attributes The key attribute structure to query.
307 *
308 * \return The key type stored in the attribute structure.
309 */
310static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
311
312/** Retrieve the key size from key attributes.
313 *
314 * This function may be declared as `static` (i.e. without external
315 * linkage). This function may be provided as a function-like macro,
316 * but in this case it must evaluate its argument exactly once.
317 *
318 * \param[in] attributes The key attribute structure to query.
319 *
320 * \return The key size stored in the attribute structure, in bits.
321 */
322static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
323
324/** Retrieve the attributes of a key.
325 *
326 * This function first resets the attribute structure as with
327 * psa_reset_key_attributes(). It then copies the attributes of
328 * the given key into the given attribute structure.
329 *
330 * \note This function may allocate memory or other resources.
331 * Once you have called this function on an attribute structure,
332 * you must call psa_reset_key_attributes() to free these resources.
333 *
Maulik Patel28659c42021-01-06 14:09:22 +0000334 * \param[in] key Identifier of the key to query.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100335 * \param[in,out] attributes On success, the attributes of the key.
336 * On failure, equivalent to a
337 * freshly-initialized structure.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000338 *
339 * \retval #PSA_SUCCESS
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100340 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100341 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Antonio de Angelis377a1552018-11-22 17:02:40 +0000342 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100343 * \retval #PSA_ERROR_CORRUPTION_DETECTED
344 * \retval #PSA_ERROR_STORAGE_FAILURE
Maulik Patel13b27cf2021-05-14 11:44:53 +0100345 * \retval #PSA_ERROR_DATA_CORRUPT
346 * \retval #PSA_ERROR_DATA_INVALID
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100347 * \retval #PSA_ERROR_BAD_STATE
348 * The library has not been previously initialized by psa_crypto_init().
349 * It is implementation-dependent whether a failure to initialize
350 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100351 */
Maulik Patel28659c42021-01-06 14:09:22 +0000352psa_status_t psa_get_key_attributes(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100353 psa_key_attributes_t *attributes);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100354
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100355/** Reset a key attribute structure to a freshly initialized state.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000356 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100357 * You must initialize the attribute structure as described in the
358 * documentation of the type #psa_key_attributes_t before calling this
359 * function. Once the structure has been initialized, you may call this
360 * function at any time.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000361 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100362 * This function frees any auxiliary resources that the structure
363 * may contain.
364 *
365 * \param[in,out] attributes The attribute structure to reset.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000366 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100367void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Antonio de Angelis14276e92018-07-10 14:35:43 +0100368
369/**@}*/
370
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100371/** \defgroup key_management Key management
Antonio de Angelis14276e92018-07-10 14:35:43 +0100372 * @{
373 */
374
Maulik Patel28659c42021-01-06 14:09:22 +0000375/** Remove non-essential copies of key material from memory.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100376 *
Maulik Patel28659c42021-01-06 14:09:22 +0000377 * If the key identifier designates a volatile key, this functions does not do
378 * anything and returns successfully.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100379 *
Maulik Patel28659c42021-01-06 14:09:22 +0000380 * If the key identifier designates a persistent key, then this function will
381 * free all resources associated with the key in volatile memory. The key
382 * data in persistent storage is not affected and the key can still be used.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100383 *
Maulik Patel28659c42021-01-06 14:09:22 +0000384 * \param key Identifier of the key to purge.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100385 *
386 * \retval #PSA_SUCCESS
Maulik Patel28659c42021-01-06 14:09:22 +0000387 * The key material will have been removed from memory if it is not
388 * currently required.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100389 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +0000390 * \p key is not a valid key identifier.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100391 * \retval #PSA_ERROR_BAD_STATE
392 * The library has not been previously initialized by psa_crypto_init().
393 * It is implementation-dependent whether a failure to initialize
394 * results in this error code.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100395 */
Maulik Patel28659c42021-01-06 14:09:22 +0000396psa_status_t psa_purge_key(psa_key_id_t key);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100397
398/** Make a copy of a key.
399 *
400 * Copy key material from one location to another.
401 *
402 * This function is primarily useful to copy a key from one location
403 * to another, since it populates a key using the material from
404 * another key which may have a different lifetime.
405 *
406 * This function may be used to share a key with a different party,
407 * subject to implementation-defined restrictions on key sharing.
408 *
409 * The policy on the source key must have the usage flag
410 * #PSA_KEY_USAGE_COPY set.
411 * This flag is sufficient to permit the copy if the key has the lifetime
412 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
413 * Some secure elements do not provide a way to copy a key without
414 * making it extractable from the secure element. If a key is located
415 * in such a secure element, then the key must have both usage flags
416 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
417 * a copy of the key outside the secure element.
418 *
419 * The resulting key may only be used in a way that conforms to
420 * both the policy of the original key and the policy specified in
421 * the \p attributes parameter:
422 * - The usage flags on the resulting key are the bitwise-and of the
423 * usage flags on the source policy and the usage flags in \p attributes.
424 * - If both allow the same algorithm or wildcard-based
425 * algorithm policy, the resulting key has the same algorithm policy.
426 * - If either of the policies allows an algorithm and the other policy
427 * allows a wildcard-based algorithm policy that includes this algorithm,
428 * the resulting key allows the same algorithm.
429 * - If the policies do not allow any algorithm in common, this function
430 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
431 *
432 * The effect of this function on implementation-defined attributes is
433 * implementation-defined.
434 *
Maulik Patel28659c42021-01-06 14:09:22 +0000435 * \param source_key The key to copy. It must allow the usage
436 * #PSA_KEY_USAGE_COPY. If a private or secret key is
437 * being copied outside of a secure element it must
438 * also allow #PSA_KEY_USAGE_EXPORT.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100439 * \param[in] attributes The attributes for the new key.
440 * They are used as follows:
441 * - The key type and size may be 0. If either is
442 * nonzero, it must match the corresponding
443 * attribute of the source key.
444 * - The key location (the lifetime and, for
445 * persistent keys, the key identifier) is
446 * used directly.
447 * - The policy constraints (usage flags and
448 * algorithm policy) are combined from
449 * the source key and \p attributes so that
450 * both sets of restrictions apply, as
451 * described in the documentation of this function.
Maulik Patel28659c42021-01-06 14:09:22 +0000452 * \param[out] target_key On success, an identifier for the newly created
453 * key. For persistent keys, this is the key
454 * identifier defined in \p attributes.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100455 * \c 0 on failure.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100456 *
457 * \retval #PSA_SUCCESS
458 * \retval #PSA_ERROR_INVALID_HANDLE
Maulik Patel28659c42021-01-06 14:09:22 +0000459 * \p source_key is invalid.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100460 * \retval #PSA_ERROR_ALREADY_EXISTS
461 * This is an attempt to create a persistent key, and there is
462 * already a persistent key with the given identifier.
463 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100464 * The lifetime or identifier in \p attributes are invalid, or
465 * the policy constraints on the source and specified in
466 * \p attributes are incompatible, or
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100467 * \p attributes specifies a key type or key size
468 * which does not match the attributes of the source key.
469 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100470 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag, or
471 * the source key is not exportable and its lifetime does not
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100472 * allow copying it to the target's lifetime.
473 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
474 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100475 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100476 * \retval #PSA_ERROR_HARDWARE_FAILURE
Maulik Patel13b27cf2021-05-14 11:44:53 +0100477 * \retval #PSA_ERROR_DATA_INVALID
478 * \retval #PSA_ERROR_DATA_CORRUPT
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100479 * \retval #PSA_ERROR_STORAGE_FAILURE
480 * \retval #PSA_ERROR_CORRUPTION_DETECTED
481 * \retval #PSA_ERROR_BAD_STATE
482 * The library has not been previously initialized by psa_crypto_init().
483 * It is implementation-dependent whether a failure to initialize
484 * results in this error code.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100485 */
Maulik Patel28659c42021-01-06 14:09:22 +0000486psa_status_t psa_copy_key(psa_key_id_t source_key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100487 const psa_key_attributes_t *attributes,
Maulik Patel28659c42021-01-06 14:09:22 +0000488 psa_key_id_t *target_key);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100489
490
491/**
492 * \brief Destroy a key.
493 *
494 * This function destroys a key from both volatile
495 * memory and, if applicable, non-volatile storage. Implementations shall
Summer Qin614002c2023-01-19 15:22:39 +0800496 * make a best effort to ensure that the key material cannot be recovered.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100497 *
498 * This function also erases any metadata such as policies and frees
Maulik Patel28659c42021-01-06 14:09:22 +0000499 * resources associated with the key.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100500 *
501 * If a key is currently in use in a multipart operation, then destroying the
502 * key will cause the multipart operation to fail.
503 *
Maulik Patel28659c42021-01-06 14:09:22 +0000504 * \param key Identifier of the key to erase. If this is \c 0, do nothing and
505 * return #PSA_SUCCESS.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100506 *
507 * \retval #PSA_SUCCESS
Maulik Patel28659c42021-01-06 14:09:22 +0000508 * \p key was a valid identifier and the key material that it
509 * referred to has been erased. Alternatively, \p key is \c 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100510 * \retval #PSA_ERROR_NOT_PERMITTED
511 * The key cannot be erased because it is
512 * read-only, either due to a policy or due to physical restrictions.
513 * \retval #PSA_ERROR_INVALID_HANDLE
Maulik Patel28659c42021-01-06 14:09:22 +0000514 * \p key is not a valid identifier nor \c 0.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100515 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Summer Qin614002c2023-01-19 15:22:39 +0800516 * There was a failure in communication with the cryptoprocessor.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100517 * The key material may still be present in the cryptoprocessor.
Maulik Patel13b27cf2021-05-14 11:44:53 +0100518 * \retval #PSA_ERROR_DATA_INVALID
519 * This error is typically a result of either storage corruption on a
520 * cleartext storage backend, or an attempt to read data that was
521 * written by an incompatible version of the library.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100522 * \retval #PSA_ERROR_STORAGE_FAILURE
523 * The storage is corrupted. Implementations shall make a best effort
524 * to erase key material even in this stage, however applications
525 * should be aware that it may be impossible to guarantee that the
526 * key material is not recoverable in such cases.
527 * \retval #PSA_ERROR_CORRUPTION_DETECTED
528 * An unexpected condition which is not a storage corruption or
529 * a communication failure occurred. The cryptoprocessor may have
530 * been compromised.
531 * \retval #PSA_ERROR_BAD_STATE
532 * The library has not been previously initialized by psa_crypto_init().
533 * It is implementation-dependent whether a failure to initialize
534 * results in this error code.
535 */
Maulik Patel28659c42021-01-06 14:09:22 +0000536psa_status_t psa_destroy_key(psa_key_id_t key);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100537
538/**@}*/
539
540/** \defgroup import_export Key import and export
541 * @{
542 */
543
544/**
545 * \brief Import a key in binary format.
546 *
547 * This function supports any output from psa_export_key(). Refer to the
548 * documentation of psa_export_public_key() for the format of public keys
549 * and to the documentation of psa_export_key() for the format for
550 * other key types.
551 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100552 * The key data determines the key size. The attributes may optionally
553 * specify a key size; in this case it must match the size determined
554 * from the key data. A key size of 0 in \p attributes indicates that
555 * the key size is solely determined by the key data.
556 *
557 * Implementations must reject an attempt to import a key of size 0.
558 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100559 * This specification supports a single format for each key type.
560 * Implementations may support other formats as long as the standard
561 * format is supported. Implementations that support other formats
562 * should ensure that the formats are clearly unambiguous so as to
563 * minimize the risk that an invalid input is accidentally interpreted
564 * according to a different format.
565 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100566 * \param[in] attributes The attributes for the new key.
567 * The key size is always determined from the
568 * \p data buffer.
569 * If the key size in \p attributes is nonzero,
570 * it must be equal to the size from \p data.
Maulik Patel28659c42021-01-06 14:09:22 +0000571 * \param[out] key On success, an identifier to the newly created key.
572 * For persistent keys, this is the key identifier
573 * defined in \p attributes.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100574 * \c 0 on failure.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100575 * \param[in] data Buffer containing the key data. The content of this
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100576 * buffer is interpreted according to the type declared
577 * in \p attributes.
578 * All implementations must support at least the format
579 * described in the documentation
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100580 * of psa_export_key() or psa_export_public_key() for
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100581 * the chosen type. Implementations may allow other
582 * formats, but should be conservative: implementations
583 * should err on the side of rejecting content if it
584 * may be erroneous (e.g. wrong type or truncated data).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100585 * \param data_length Size of the \p data buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100586 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000587 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +0100588 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100589 * If the key is persistent, the key material and the key's metadata
590 * have been saved to persistent storage.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100591 * \retval #PSA_ERROR_ALREADY_EXISTS
592 * This is an attempt to create a persistent key, and there is
593 * already a persistent key with the given identifier.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100594 * \retval #PSA_ERROR_NOT_SUPPORTED
595 * The key type or key size is not supported, either by the
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100596 * implementation in general or in this particular persistent location.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000597 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100598 * The key attributes, as a whole, are invalid, or
599 * the key data is not correctly formatted, or
600 * the size in \p attributes is nonzero and does not match the size
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100601 * of the key data.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100602 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
603 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
604 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Maulik Patel13b27cf2021-05-14 11:44:53 +0100605 * \retval #PSA_ERROR_DATA_CORRUPT
606 * \retval #PSA_ERROR_DATA_INVALID
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100607 * \retval #PSA_ERROR_STORAGE_FAILURE
608 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100609 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100610 * \retval #PSA_ERROR_BAD_STATE
611 * The library has not been previously initialized by psa_crypto_init().
612 * It is implementation-dependent whether a failure to initialize
613 * results in this error code.
614 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100615psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100616 const uint8_t *data,
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100617 size_t data_length,
Maulik Patel28659c42021-01-06 14:09:22 +0000618 psa_key_id_t *key);
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100619
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100620
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100621
622/**
623 * \brief Export a key in binary format.
624 *
625 * The output of this function can be passed to psa_import_key() to
626 * create an equivalent object.
627 *
628 * If the implementation of psa_import_key() supports other formats
629 * beyond the format specified here, the output from psa_export_key()
630 * must use the representation specified here, not the original
631 * representation.
632 *
633 * For standard key types, the output format is as follows:
634 *
635 * - For symmetric keys (including MAC keys), the format is the
636 * raw bytes of the key.
637 * - For DES, the key data consists of 8 bytes. The parity bits must be
638 * correct.
639 * - For Triple-DES, the format is the concatenation of the
640 * two or three DES keys.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100641 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100642 * is the non-encrypted DER encoding of the representation defined by
643 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
644 * ```
645 * RSAPrivateKey ::= SEQUENCE {
646 * version INTEGER, -- must be 0
647 * modulus INTEGER, -- n
648 * publicExponent INTEGER, -- e
649 * privateExponent INTEGER, -- d
650 * prime1 INTEGER, -- p
651 * prime2 INTEGER, -- q
652 * exponent1 INTEGER, -- d mod (p-1)
653 * exponent2 INTEGER, -- d mod (q-1)
654 * coefficient INTEGER, -- (inverse of q) mod p
655 * }
656 * ```
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100657 * - For elliptic curve key pairs (key types for which
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100658 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100659 * a representation of the private value as a `ceiling(m/8)`-byte string
660 * where `m` is the bit size associated with the curve, i.e. the bit size
661 * of the order of the curve's coordinate field. This byte string is
662 * in little-endian order for Montgomery curves (curve types
Summer Qin0e5b2e02020-10-22 11:23:39 +0800663 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
664 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
665 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
666 * For Weierstrass curves, this is the content of the `privateKey` field of
667 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
668 * the format is defined by RFC 7748, and output is masked according to §5.
Summer Qin359167d2021-07-05 18:11:50 +0800669 * For twisted Edwards curves, the private key is as defined by RFC 8032
670 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100671 * - For Diffie-Hellman key exchange key pairs (key types for which
672 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
673 * format is the representation of the private key `x` as a big-endian byte
674 * string. The length of the byte string is the private key size in bytes
675 * (leading zeroes are not stripped).
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100676 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
677 * true), the format is the same as for psa_export_public_key().
678 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100679 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
680 *
Maulik Patel28659c42021-01-06 14:09:22 +0000681 * \param key Identifier of the key to export. It must allow the
682 * usage #PSA_KEY_USAGE_EXPORT, unless it is a public
683 * key.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100684 * \param[out] data Buffer where the key data is to be written.
685 * \param data_size Size of the \p data buffer in bytes.
686 * \param[out] data_length On success, the number of bytes
687 * that make up the key data.
688 *
689 * \retval #PSA_SUCCESS
690 * \retval #PSA_ERROR_INVALID_HANDLE
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100691 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100692 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000693 * \retval #PSA_ERROR_NOT_SUPPORTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100694 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
695 * The size of the \p data buffer is too small. You can determine a
696 * sufficient buffer size by calling
Maulik Patel13b27cf2021-05-14 11:44:53 +0100697 * #PSA_EXPORT_KEY_OUTPUT_SIZE(\c type, \c bits)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100698 * where \c type is the key type
699 * and \c bits is the key size in bits.
700 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
701 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100702 * \retval #PSA_ERROR_CORRUPTION_DETECTED
703 * \retval #PSA_ERROR_STORAGE_FAILURE
704 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100705 * \retval #PSA_ERROR_BAD_STATE
706 * The library has not been previously initialized by psa_crypto_init().
707 * It is implementation-dependent whether a failure to initialize
708 * results in this error code.
709 */
Maulik Patel28659c42021-01-06 14:09:22 +0000710psa_status_t psa_export_key(psa_key_id_t key,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100711 uint8_t *data,
712 size_t data_size,
713 size_t *data_length);
714
715/**
716 * \brief Export a public key or the public part of a key pair in binary format.
717 *
718 * The output of this function can be passed to psa_import_key() to
719 * create an object that is equivalent to the public key.
720 *
721 * This specification supports a single format for each key type.
722 * Implementations may support other formats as long as the standard
723 * format is supported. Implementations that support other formats
724 * should ensure that the formats are clearly unambiguous so as to
725 * minimize the risk that an invalid input is accidentally interpreted
726 * according to a different format.
727 *
728 * For standard key types, the output format is as follows:
729 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
730 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
731 * ```
732 * RSAPublicKey ::= SEQUENCE {
733 * modulus INTEGER, -- n
734 * publicExponent INTEGER } -- e
735 * ```
Summer Qin359167d2021-07-05 18:11:50 +0800736 * - For elliptic curve keys on a twisted Edwards curve (key types for which
737 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true and #PSA_KEY_TYPE_ECC_GET_FAMILY
738 * returns #PSA_ECC_FAMILY_TWISTED_EDWARDS), the public key is as defined
739 * by RFC 8032
740 * (a 32-byte string for Edwards25519, a 57-byte string for Edwards448).
741 * - For other elliptic curve public keys (key types for which
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100742 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100743 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100744 * Let `m` be the bit size associated with the curve, i.e. the bit size of
745 * `q` for a curve over `F_q`. The representation consists of:
746 * - The byte 0x04;
747 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
748 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100749 * - For Diffie-Hellman key exchange public keys (key types for which
750 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
751 * the format is the representation of the public key `y = g^x mod p` as a
752 * big-endian byte string. The length of the byte string is the length of the
753 * base prime `p` in bytes.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100754 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100755 * Exporting a public key object or the public part of a key pair is
756 * always permitted, regardless of the key's usage flags.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100757 *
Maulik Patel28659c42021-01-06 14:09:22 +0000758 * \param key Identifier of the key to export.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100759 * \param[out] data Buffer where the key data is to be written.
760 * \param data_size Size of the \p data buffer in bytes.
761 * \param[out] data_length On success, the number of bytes
762 * that make up the key data.
763 *
764 * \retval #PSA_SUCCESS
765 * \retval #PSA_ERROR_INVALID_HANDLE
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100766 * \retval #PSA_ERROR_INVALID_ARGUMENT
767 * The key is neither a public key nor a key pair.
768 * \retval #PSA_ERROR_NOT_SUPPORTED
769 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
770 * The size of the \p data buffer is too small. You can determine a
771 * sufficient buffer size by calling
Maulik Patel13b27cf2021-05-14 11:44:53 +0100772 * #PSA_EXPORT_KEY_OUTPUT_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100773 * where \c type is the key type
774 * and \c bits is the key size in bits.
775 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
776 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100777 * \retval #PSA_ERROR_CORRUPTION_DETECTED
778 * \retval #PSA_ERROR_STORAGE_FAILURE
779 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100780 * \retval #PSA_ERROR_BAD_STATE
781 * The library has not been previously initialized by psa_crypto_init().
782 * It is implementation-dependent whether a failure to initialize
783 * results in this error code.
784 */
Maulik Patel28659c42021-01-06 14:09:22 +0000785psa_status_t psa_export_public_key(psa_key_id_t key,
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100786 uint8_t *data,
787 size_t data_size,
788 size_t *data_length);
789
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100790
Antonio de Angelis14276e92018-07-10 14:35:43 +0100791
792/**@}*/
793
794/** \defgroup hash Message digests
795 * @{
796 */
797
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100798/** Calculate the hash (digest) of a message.
799 *
800 * \note To verify the hash of a message against an
801 * expected value, use psa_hash_compare() instead.
802 *
803 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
804 * such that #PSA_ALG_IS_HASH(\p alg) is true).
805 * \param[in] input Buffer containing the message to hash.
806 * \param input_length Size of the \p input buffer in bytes.
807 * \param[out] hash Buffer where the hash is to be written.
808 * \param hash_size Size of the \p hash buffer in bytes.
809 * \param[out] hash_length On success, the number of bytes
810 * that make up the hash value. This is always
Maulik Patel13b27cf2021-05-14 11:44:53 +0100811 * #PSA_HASH_LENGTH(\p alg).
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100812 *
813 * \retval #PSA_SUCCESS
814 * Success.
815 * \retval #PSA_ERROR_NOT_SUPPORTED
816 * \p alg is not supported or is not a hash algorithm.
817 * \retval #PSA_ERROR_INVALID_ARGUMENT
818 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
819 * \p hash_size is too small
820 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
821 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
822 * \retval #PSA_ERROR_HARDWARE_FAILURE
823 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100824 * \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
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100859 * \retval #PSA_ERROR_BAD_STATE
860 * The library has not been previously initialized by psa_crypto_init().
861 * It is implementation-dependent whether a failure to initialize
862 * results in this error code.
863 */
864psa_status_t psa_hash_compare(psa_algorithm_t alg,
865 const uint8_t *input,
866 size_t input_length,
867 const uint8_t *hash,
Soby Mathew07ef6e42020-07-20 21:09:23 +0100868 size_t hash_length);
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100869
Antonio de Angelis377a1552018-11-22 17:02:40 +0000870/** The type of the state data structure for multipart hash operations.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100871 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100872 * Before calling any function on a hash operation object, the application must
873 * initialize it by any of the following means:
874 * - Set the structure to all-bits-zero, for example:
875 * \code
876 * psa_hash_operation_t operation;
877 * memset(&operation, 0, sizeof(operation));
878 * \endcode
879 * - Initialize the structure to logical zero values, for example:
880 * \code
881 * psa_hash_operation_t operation = {0};
882 * \endcode
883 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
884 * for example:
885 * \code
886 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
887 * \endcode
888 * - Assign the result of the function psa_hash_operation_init()
889 * to the structure, for example:
890 * \code
891 * psa_hash_operation_t operation;
892 * operation = psa_hash_operation_init();
893 * \endcode
894 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000895 * This is an implementation-defined \c struct. Applications should not
Summer Qin359167d2021-07-05 18:11:50 +0800896 * make any assumptions about the content of this structure.
897 * Implementation details can change in future versions without notice. */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000898typedef struct psa_hash_operation_s psa_hash_operation_t;
Antonio de Angelis14276e92018-07-10 14:35:43 +0100899
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100900/** \def PSA_HASH_OPERATION_INIT
901 *
902 * This macro returns a suitable initializer for a hash operation object
903 * of type #psa_hash_operation_t.
904 */
905
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100906/** Return an initial value for a hash operation object.
907 */
908static psa_hash_operation_t psa_hash_operation_init(void);
909
910/** Set up a multipart hash operation.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100911 *
912 * The sequence of operations to calculate a hash (message digest)
913 * is as follows:
914 * -# Allocate an operation object which will be passed to all the functions
915 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100916 * -# Initialize the operation object with one of the methods described in the
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100917 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000918 * -# Call psa_hash_setup() to specify the algorithm.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100919 * -# Call psa_hash_update() zero, one or more times, passing a fragment
920 * of the message each time. The hash that is calculated is the hash
921 * of the concatenation of these messages in order.
922 * -# To calculate the hash, call psa_hash_finish().
923 * To compare the hash with an expected value, call psa_hash_verify().
924 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100925 * If an error occurs at any step after a call to psa_hash_setup(), the
926 * operation will need to be reset by a call to psa_hash_abort(). The
927 * application may call psa_hash_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100928 * has been initialized.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100929 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000930 * After a successful call to psa_hash_setup(), the application must
Antonio de Angelis14276e92018-07-10 14:35:43 +0100931 * eventually terminate the operation. The following events terminate an
932 * operation:
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100933 * - A successful call to psa_hash_finish() or psa_hash_verify().
934 * - A call to psa_hash_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +0100935 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +0100936 * \param[in,out] operation The operation object to set up. It must have
937 * been initialized as per the documentation for
938 * #psa_hash_operation_t and not yet in use.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000939 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
940 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +0100941 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000942 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +0100943 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000944 * \retval #PSA_ERROR_NOT_SUPPORTED
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100945 * \p alg is not a supported hash algorithm.
946 * \retval #PSA_ERROR_INVALID_ARGUMENT
947 * \p alg is not a hash algorithm.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000948 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
949 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
950 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100951 * \retval #PSA_ERROR_CORRUPTION_DETECTED
952 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100953 * The operation state is not valid (it must be inactive), or
954 * the library has not been previously initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100955 * It is implementation-dependent whether a failure to initialize
956 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100957 */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000958psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Antonio de Angelis14276e92018-07-10 14:35:43 +0100959 psa_algorithm_t alg);
960
961/** Add a message fragment to a multipart hash operation.
962 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000963 * The application must call psa_hash_setup() before calling this function.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100964 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100965 * If this function returns an error status, the operation enters an error
966 * state and must be aborted by calling psa_hash_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +0100967 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000968 * \param[in,out] operation Active hash operation.
969 * \param[in] input Buffer containing the message fragment to hash.
970 * \param input_length Size of the \p input buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100971 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000972 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +0100973 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000974 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
975 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
976 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100977 * \retval #PSA_ERROR_CORRUPTION_DETECTED
978 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100979 * The operation state is not valid (it must be active), or
980 * the library has not been previously initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100981 * It is implementation-dependent whether a failure to initialize
982 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100983 */
984psa_status_t psa_hash_update(psa_hash_operation_t *operation,
985 const uint8_t *input,
986 size_t input_length);
987
988/** Finish the calculation of the hash of a message.
989 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000990 * The application must call psa_hash_setup() before calling this function.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100991 * This function calculates the hash of the message formed by concatenating
992 * the inputs passed to preceding calls to psa_hash_update().
993 *
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100994 * When this function returns successfully, the operation becomes inactive.
Antonio de Angelis04debbd2019-10-14 12:12:52 +0100995 * If this function returns an error status, the operation enters an error
996 * state and must be aborted by calling psa_hash_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +0100997 *
998 * \warning Applications should not call this function if they expect
999 * a specific value for the hash. Call psa_hash_verify() instead.
1000 * Beware that comparing integrity or authenticity data such as
1001 * hash values with a function such as \c memcmp is risky
1002 * because the time taken by the comparison may leak information
1003 * about the hashed data which could allow an attacker to guess
1004 * a valid hash and thereby bypass security controls.
1005 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001006 * \param[in,out] operation Active hash operation.
1007 * \param[out] hash Buffer where the hash is to be written.
1008 * \param hash_size Size of the \p hash buffer in bytes.
1009 * \param[out] hash_length On success, the number of bytes
1010 * that make up the hash value. This is always
Maulik Patel13b27cf2021-05-14 11:44:53 +01001011 * #PSA_HASH_LENGTH(\c alg) where \c alg is the
Antonio de Angelis377a1552018-11-22 17:02:40 +00001012 * hash algorithm that is calculated.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001013 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001014 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001015 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001016 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1017 * The size of the \p hash buffer is too small. You can determine a
Maulik Patel13b27cf2021-05-14 11:44:53 +01001018 * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01001019 * where \c alg is the hash algorithm that is calculated.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001020 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1021 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1022 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001023 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1024 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001025 * The operation state is not valid (it must be active), or
1026 * the library has not been previously initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001027 * It is implementation-dependent whether a failure to initialize
1028 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001029 */
1030psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1031 uint8_t *hash,
1032 size_t hash_size,
1033 size_t *hash_length);
1034
1035/** Finish the calculation of the hash of a message and compare it with
1036 * an expected value.
1037 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001038 * The application must call psa_hash_setup() before calling this function.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001039 * This function calculates the hash of the message formed by concatenating
1040 * the inputs passed to preceding calls to psa_hash_update(). It then
1041 * compares the calculated hash with the expected hash passed as a
1042 * parameter to this function.
1043 *
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001044 * When this function returns successfully, the operation becomes inactive.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001045 * If this function returns an error status, the operation enters an error
1046 * state and must be aborted by calling psa_hash_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001047 *
1048 * \note Implementations shall make the best effort to ensure that the
1049 * comparison between the actual hash and the expected hash is performed
1050 * in constant time.
1051 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001052 * \param[in,out] operation Active hash operation.
1053 * \param[in] hash Buffer containing the expected hash value.
1054 * \param hash_length Size of the \p hash buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001055 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001056 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001057 * The expected hash is identical to the actual hash of the message.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001058 * \retval #PSA_ERROR_INVALID_SIGNATURE
Antonio de Angelis14276e92018-07-10 14:35:43 +01001059 * The hash of the message was calculated successfully, but it
1060 * differs from the expected hash.
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
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001066 * The operation state is not valid (it must be active), or
1067 * the library has not been previously initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001068 * It is implementation-dependent whether a failure to initialize
1069 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001070 */
1071psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1072 const uint8_t *hash,
1073 size_t hash_length);
1074
1075/** Abort a hash operation.
1076 *
Antonio de Angelis14276e92018-07-10 14:35:43 +01001077 * Aborting an operation frees all associated resources except for the
Antonio de Angelis377a1552018-11-22 17:02:40 +00001078 * \p operation structure itself. Once aborted, the operation object
1079 * can be reused for another operation by calling
1080 * psa_hash_setup() again.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001081 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001082 * You may call this function any time after the operation object has
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001083 * been initialized by one of the methods described in #psa_hash_operation_t.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001084 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001085 * In particular, calling psa_hash_abort() after the operation has been
1086 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1087 * psa_hash_verify() is safe and has no effect.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001088 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001089 * \param[in,out] operation Initialized hash operation.
1090 *
1091 * \retval #PSA_SUCCESS
Antonio de Angelis377a1552018-11-22 17:02:40 +00001092 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1093 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001094 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1095 * \retval #PSA_ERROR_BAD_STATE
1096 * The library has not been previously initialized by psa_crypto_init().
1097 * It is implementation-dependent whether a failure to initialize
1098 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001099 */
1100psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
1101
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001102/** Clone a hash operation.
1103 *
1104 * This function copies the state of an ongoing hash operation to
1105 * a new operation object. In other words, this function is equivalent
1106 * to calling psa_hash_setup() on \p target_operation with the same
1107 * algorithm that \p source_operation was set up for, then
1108 * psa_hash_update() on \p target_operation with the same input that
1109 * that was passed to \p source_operation. After this function returns, the
1110 * two objects are independent, i.e. subsequent calls involving one of
1111 * the objects do not affect the other object.
1112 *
1113 * \param[in] source_operation The active hash operation to clone.
1114 * \param[in,out] target_operation The operation object to set up.
1115 * It must be initialized but not active.
1116 *
1117 * \retval #PSA_SUCCESS
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001118 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1119 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001120 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1121 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1122 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001123 * The \p source_operation state is not valid (it must be active), or
1124 * the \p target_operation state is not valid (it must be inactive), or
1125 * the library has not been previously initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001126 * It is implementation-dependent whether a failure to initialize
1127 * results in this error code.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001128 */
1129psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1130 psa_hash_operation_t *target_operation);
1131
Antonio de Angelis14276e92018-07-10 14:35:43 +01001132/**@}*/
1133
1134/** \defgroup MAC Message authentication codes
1135 * @{
1136 */
1137
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001138/** Calculate the MAC (message authentication code) of a message.
1139 *
1140 * \note To verify the MAC of a message against an
1141 * expected value, use psa_mac_verify() instead.
1142 * Beware that comparing integrity or authenticity data such as
1143 * MAC values with a function such as \c memcmp is risky
1144 * because the time taken by the comparison may leak information
1145 * about the MAC value which could allow an attacker to guess
1146 * a valid MAC and thereby bypass security controls.
1147 *
Maulik Patel28659c42021-01-06 14:09:22 +00001148 * \param key Identifier of the key to use for the operation. It
1149 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001150 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1151 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1152 * \param[in] input Buffer containing the input message.
1153 * \param input_length Size of the \p input buffer in bytes.
1154 * \param[out] mac Buffer where the MAC value is to be written.
1155 * \param mac_size Size of the \p mac buffer in bytes.
1156 * \param[out] mac_length On success, the number of bytes
1157 * that make up the MAC value.
1158 *
1159 * \retval #PSA_SUCCESS
1160 * Success.
1161 * \retval #PSA_ERROR_INVALID_HANDLE
1162 * \retval #PSA_ERROR_NOT_PERMITTED
1163 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00001164 * \p key is not compatible with \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001165 * \retval #PSA_ERROR_NOT_SUPPORTED
1166 * \p alg is not supported or is not a MAC algorithm.
1167 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1168 * \p mac_size is too small
1169 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1170 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1171 * \retval #PSA_ERROR_HARDWARE_FAILURE
1172 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1173 * \retval #PSA_ERROR_STORAGE_FAILURE
1174 * The key could not be retrieved from storage.
1175 * \retval #PSA_ERROR_BAD_STATE
1176 * The library has not been previously initialized by psa_crypto_init().
1177 * It is implementation-dependent whether a failure to initialize
1178 * results in this error code.
1179 */
Maulik Patel28659c42021-01-06 14:09:22 +00001180psa_status_t psa_mac_compute(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001181 psa_algorithm_t alg,
1182 const uint8_t *input,
1183 size_t input_length,
1184 uint8_t *mac,
1185 size_t mac_size,
1186 size_t *mac_length);
1187
1188/** Calculate the MAC of a message and compare it with a reference value.
1189 *
Maulik Patel28659c42021-01-06 14:09:22 +00001190 * \param key Identifier of the key to use for the operation. It
1191 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001192 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1193 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1194 * \param[in] input Buffer containing the input message.
1195 * \param input_length Size of the \p input buffer in bytes.
1196 * \param[out] mac Buffer containing the expected MAC value.
1197 * \param mac_length Size of the \p mac buffer in bytes.
1198 *
1199 * \retval #PSA_SUCCESS
1200 * The expected MAC is identical to the actual MAC of the input.
1201 * \retval #PSA_ERROR_INVALID_SIGNATURE
1202 * The MAC of the message was calculated successfully, but it
1203 * differs from the expected value.
1204 * \retval #PSA_ERROR_INVALID_HANDLE
1205 * \retval #PSA_ERROR_NOT_PERMITTED
1206 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00001207 * \p key is not compatible with \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001208 * \retval #PSA_ERROR_NOT_SUPPORTED
1209 * \p alg is not supported or is not a MAC algorithm.
1210 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1211 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1212 * \retval #PSA_ERROR_HARDWARE_FAILURE
1213 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1214 * \retval #PSA_ERROR_STORAGE_FAILURE
1215 * The key could not be retrieved from storage.
1216 * \retval #PSA_ERROR_BAD_STATE
1217 * The library has not been previously initialized by psa_crypto_init().
1218 * It is implementation-dependent whether a failure to initialize
1219 * results in this error code.
1220 */
Maulik Patel28659c42021-01-06 14:09:22 +00001221psa_status_t psa_mac_verify(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001222 psa_algorithm_t alg,
1223 const uint8_t *input,
1224 size_t input_length,
1225 const uint8_t *mac,
Soby Mathew07ef6e42020-07-20 21:09:23 +01001226 size_t mac_length);
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001227
Antonio de Angelis377a1552018-11-22 17:02:40 +00001228/** The type of the state data structure for multipart MAC operations.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001229 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001230 * Before calling any function on a MAC operation object, the application must
1231 * initialize it by any of the following means:
1232 * - Set the structure to all-bits-zero, for example:
1233 * \code
1234 * psa_mac_operation_t operation;
1235 * memset(&operation, 0, sizeof(operation));
1236 * \endcode
1237 * - Initialize the structure to logical zero values, for example:
1238 * \code
1239 * psa_mac_operation_t operation = {0};
1240 * \endcode
1241 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1242 * for example:
1243 * \code
1244 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1245 * \endcode
1246 * - Assign the result of the function psa_mac_operation_init()
1247 * to the structure, for example:
1248 * \code
1249 * psa_mac_operation_t operation;
1250 * operation = psa_mac_operation_init();
1251 * \endcode
1252 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001253 *
Summer Qin359167d2021-07-05 18:11:50 +08001254 * This is an implementation-defined \c struct. Applications should not
1255 * make any assumptions about the content of this structure.
1256 * Implementation details can change in future versions without notice. */
1257typedef struct psa_mac_operation_s psa_mac_operation_t;
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001258
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001259/** \def PSA_MAC_OPERATION_INIT
1260 *
1261 * This macro returns a suitable initializer for a MAC operation object of type
1262 * #psa_mac_operation_t.
1263 */
1264
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001265/** Return an initial value for a MAC operation object.
1266 */
1267static psa_mac_operation_t psa_mac_operation_init(void);
1268
1269/** Set up a multipart MAC calculation operation.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001270 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001271 * This function sets up the calculation of the MAC
1272 * (message authentication code) of a byte string.
1273 * To verify the MAC of a message against an
1274 * expected value, use psa_mac_verify_setup() instead.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001275 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001276 * The sequence of operations to calculate a MAC is as follows:
Antonio de Angelis14276e92018-07-10 14:35:43 +01001277 * -# Allocate an operation object which will be passed to all the functions
1278 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001279 * -# Initialize the operation object with one of the methods described in the
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001280 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001281 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001282 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1283 * of the message each time. The MAC that is calculated is the MAC
1284 * of the concatenation of these messages in order.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001285 * -# At the end of the message, call psa_mac_sign_finish() to finish
1286 * calculating the MAC value and retrieve it.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001287 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001288 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1289 * operation will need to be reset by a call to psa_mac_abort(). The
1290 * application may call psa_mac_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001291 * has been initialized.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001292 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001293 * After a successful call to psa_mac_sign_setup(), the application must
1294 * eventually terminate the operation through one of the following methods:
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001295 * - A successful call to psa_mac_sign_finish().
1296 * - A call to psa_mac_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001297 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001298 * \param[in,out] operation The operation object to set up. It must have
1299 * been initialized as per the documentation for
1300 * #psa_mac_operation_t and not yet in use.
Maulik Patel28659c42021-01-06 14:09:22 +00001301 * \param key Identifier of the key to use for the operation. It
1302 * must remain valid until the operation terminates.
1303 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001304 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001305 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +01001306 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001307 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001308 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001309 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00001310 * \retval #PSA_ERROR_NOT_PERMITTED
1311 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00001312 * \p key is not compatible with \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001313 * \retval #PSA_ERROR_NOT_SUPPORTED
1314 * \p alg is not supported or is not a MAC algorithm.
1315 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1316 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1317 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001318 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1319 * \retval #PSA_ERROR_STORAGE_FAILURE
1320 * The key could not be retrieved from storage.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001321 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001322 * The operation state is not valid (it must be inactive), or
1323 * the library has not been previously initialized by psa_crypto_init().
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001324 * It is implementation-dependent whether a failure to initialize
1325 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001326 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001327psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00001328 psa_key_id_t key,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001329 psa_algorithm_t alg);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001330
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001331/** Set up a multipart MAC verification operation.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001332 *
1333 * This function sets up the verification of the MAC
1334 * (message authentication code) of a byte string against an expected value.
1335 *
1336 * The sequence of operations to verify a MAC is as follows:
1337 * -# Allocate an operation object which will be passed to all the functions
1338 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001339 * -# Initialize the operation object with one of the methods described in the
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001340 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001341 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001342 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1343 * of the message each time. The MAC that is calculated is the MAC
1344 * of the concatenation of these messages in order.
1345 * -# At the end of the message, call psa_mac_verify_finish() to finish
1346 * calculating the actual MAC of the message and verify it against
1347 * the expected value.
1348 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001349 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1350 * operation will need to be reset by a call to psa_mac_abort(). The
1351 * application may call psa_mac_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001352 * has been initialized.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001353 *
1354 * After a successful call to psa_mac_verify_setup(), the application must
1355 * eventually terminate the operation through one of the following methods:
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001356 * - A successful call to psa_mac_verify_finish().
1357 * - A call to psa_mac_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001358 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001359 * \param[in,out] operation The operation object to set up. It must have
1360 * been initialized as per the documentation for
1361 * #psa_mac_operation_t and not yet in use.
Maulik Patel28659c42021-01-06 14:09:22 +00001362 * \param key Identifier of the key to use for the operation. It
1363 * must remain valid until the operation terminates.
1364 * It must allow the usage
1365 * PSA_KEY_USAGE_VERIFY_MESSAGE.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001366 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1367 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1368 *
1369 * \retval #PSA_SUCCESS
1370 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001371 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00001372 * \retval #PSA_ERROR_NOT_PERMITTED
1373 * \retval #PSA_ERROR_INVALID_ARGUMENT
1374 * \c key is not compatible with \c alg.
1375 * \retval #PSA_ERROR_NOT_SUPPORTED
1376 * \c alg is not supported or is not a MAC algorithm.
1377 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1378 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1379 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001380 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1381 * \retval #PSA_ERROR_STORAGE_FAILURE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001382 * The key could not be retrieved from storage.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001383 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001384 * The operation state is not valid (it must be inactive), or
1385 * the library has not been previously initialized by psa_crypto_init().
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001386 * It is implementation-dependent whether a failure to initialize
1387 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001388 */
1389psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00001390 psa_key_id_t key,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001391 psa_algorithm_t alg);
1392
1393/** Add a message fragment to a multipart MAC operation.
1394 *
1395 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1396 * before calling this function.
1397 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001398 * If this function returns an error status, the operation enters an error
1399 * state and must be aborted by calling psa_mac_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001400 *
1401 * \param[in,out] operation Active MAC operation.
1402 * \param[in] input Buffer containing the message fragment to add to
1403 * the MAC calculation.
1404 * \param input_length Size of the \p input buffer in bytes.
1405 *
1406 * \retval #PSA_SUCCESS
1407 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001408 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1409 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1410 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001411 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1412 * \retval #PSA_ERROR_STORAGE_FAILURE
1413 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001414 * The operation state is not valid (it must be active), or
1415 * the library has not been previously initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001416 * It is implementation-dependent whether a failure to initialize
1417 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001418 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001419psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1420 const uint8_t *input,
1421 size_t input_length);
1422
Antonio de Angelis377a1552018-11-22 17:02:40 +00001423/** Finish the calculation of the MAC of a message.
1424 *
1425 * The application must call psa_mac_sign_setup() before calling this function.
1426 * This function calculates the MAC of the message formed by concatenating
1427 * the inputs passed to preceding calls to psa_mac_update().
1428 *
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001429 * When this function returns successfully, the operation becomes inactive.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001430 * If this function returns an error status, the operation enters an error
1431 * state and must be aborted by calling psa_mac_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001432 *
1433 * \warning Applications should not call this function if they expect
1434 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1435 * Beware that comparing integrity or authenticity data such as
1436 * MAC values with a function such as \c memcmp is risky
1437 * because the time taken by the comparison may leak information
1438 * about the MAC value which could allow an attacker to guess
1439 * a valid MAC and thereby bypass security controls.
1440 *
1441 * \param[in,out] operation Active MAC operation.
1442 * \param[out] mac Buffer where the MAC value is to be written.
1443 * \param mac_size Size of the \p mac buffer in bytes.
1444 * \param[out] mac_length On success, the number of bytes
1445 * that make up the MAC value. This is always
Maulik Patel13b27cf2021-05-14 11:44:53 +01001446 * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)
Antonio de Angelis377a1552018-11-22 17:02:40 +00001447 * where \c key_type and \c key_bits are the type and
1448 * bit-size respectively of the key and \c alg is the
1449 * MAC algorithm that is calculated.
1450 *
1451 * \retval #PSA_SUCCESS
1452 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001453 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1454 * The size of the \p mac buffer is too small. You can determine a
Maulik Patel13b27cf2021-05-14 11:44:53 +01001455 * sufficient buffer size by calling PSA_MAC_LENGTH().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001456 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1457 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1458 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001459 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1460 * \retval #PSA_ERROR_STORAGE_FAILURE
1461 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001462 * The operation state is not valid (it must be an active mac sign
1463 * operation), or the library has not been previously initialized
1464 * by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001465 * It is implementation-dependent whether a failure to initialize
1466 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001467 */
1468psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1469 uint8_t *mac,
1470 size_t mac_size,
1471 size_t *mac_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001472
Antonio de Angelis377a1552018-11-22 17:02:40 +00001473/** Finish the calculation of the MAC of a message and compare it with
1474 * an expected value.
1475 *
1476 * The application must call psa_mac_verify_setup() before calling this function.
1477 * This function calculates the MAC of the message formed by concatenating
1478 * the inputs passed to preceding calls to psa_mac_update(). It then
1479 * compares the calculated MAC with the expected MAC passed as a
1480 * parameter to this function.
1481 *
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001482 * When this function returns successfully, the operation becomes inactive.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001483 * If this function returns an error status, the operation enters an error
1484 * state and must be aborted by calling psa_mac_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001485 *
1486 * \note Implementations shall make the best effort to ensure that the
1487 * comparison between the actual MAC and the expected MAC is performed
1488 * in constant time.
1489 *
1490 * \param[in,out] operation Active MAC operation.
1491 * \param[in] mac Buffer containing the expected MAC value.
1492 * \param mac_length Size of the \p mac buffer in bytes.
1493 *
1494 * \retval #PSA_SUCCESS
1495 * The expected MAC is identical to the actual MAC of the message.
1496 * \retval #PSA_ERROR_INVALID_SIGNATURE
1497 * The MAC of the message was calculated successfully, but it
1498 * differs from the expected MAC.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001499 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1500 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1501 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001502 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1503 * \retval #PSA_ERROR_STORAGE_FAILURE
1504 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001505 * The operation state is not valid (it must be an active mac verify
1506 * operation), or the library has not been previously initialized
1507 * by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001508 * 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
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001672/** \def PSA_CIPHER_OPERATION_INIT
1673 *
1674 * This macro returns a suitable initializer for a cipher operation object of
1675 * type #psa_cipher_operation_t.
1676 */
1677
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001678/** Return an initial value for a cipher operation object.
1679 */
1680static psa_cipher_operation_t psa_cipher_operation_init(void);
1681
Antonio de Angelis14276e92018-07-10 14:35:43 +01001682/** Set the key for a multipart symmetric encryption operation.
1683 *
1684 * The sequence of operations to encrypt a message with a symmetric cipher
1685 * is as follows:
1686 * -# Allocate an operation object which will be passed to all the functions
1687 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001688 * -# Initialize the operation object with one of the methods described in the
1689 * documentation for #psa_cipher_operation_t, e.g.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001690 * #PSA_CIPHER_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001691 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001692 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Antonio de Angelis14276e92018-07-10 14:35:43 +01001693 * generate or set the IV (initialization vector). You should use
Antonio de Angelis377a1552018-11-22 17:02:40 +00001694 * psa_cipher_generate_iv() unless the protocol you are implementing
Antonio de Angelis14276e92018-07-10 14:35:43 +01001695 * requires a specific IV value.
1696 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1697 * of the message each time.
1698 * -# Call psa_cipher_finish().
1699 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001700 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1701 * the operation will need to be reset by a call to psa_cipher_abort(). The
1702 * application may call psa_cipher_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001703 * has been initialized.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001704 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001705 * After a successful call to psa_cipher_encrypt_setup(), the application must
Antonio de Angelis14276e92018-07-10 14:35:43 +01001706 * eventually terminate the operation. The following events terminate an
1707 * operation:
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001708 * - A successful call to psa_cipher_finish().
1709 * - A call to psa_cipher_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001710 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001711 * \param[in,out] operation The operation object to set up. It must have
1712 * been initialized as per the documentation for
1713 * #psa_cipher_operation_t and not yet in use.
Maulik Patel28659c42021-01-06 14:09:22 +00001714 * \param key Identifier of the key to use for the operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001715 * It must remain valid until the operation
Maulik Patel28659c42021-01-06 14:09:22 +00001716 * terminates. It must allow the usage
1717 * #PSA_KEY_USAGE_ENCRYPT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001718 * \param alg The cipher algorithm to compute
1719 * (\c PSA_ALG_XXX value such that
1720 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +01001721 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001722 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001723 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001724 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00001725 * \retval #PSA_ERROR_NOT_PERMITTED
1726 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00001727 * \p key is not compatible with \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001728 * \retval #PSA_ERROR_NOT_SUPPORTED
1729 * \p alg is not supported or is not a cipher algorithm.
1730 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1731 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1732 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001733 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1734 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001735 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001736 * The operation state is not valid (it must be inactive), or
1737 * the library has not been previously initialized by psa_crypto_init().
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001738 * It is implementation-dependent whether a failure to initialize
1739 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001740 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001741psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00001742 psa_key_id_t key,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001743 psa_algorithm_t alg);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001744
1745/** Set the key for a multipart symmetric decryption operation.
1746 *
1747 * The sequence of operations to decrypt a message with a symmetric cipher
1748 * is as follows:
1749 * -# Allocate an operation object which will be passed to all the functions
1750 * listed here.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001751 * -# Initialize the operation object with one of the methods described in the
1752 * documentation for #psa_cipher_operation_t, e.g.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001753 * #PSA_CIPHER_OPERATION_INIT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001754 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001755 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Antonio de Angelis14276e92018-07-10 14:35:43 +01001756 * decryption. If the IV is prepended to the ciphertext, you can call
1757 * psa_cipher_update() on a buffer containing the IV followed by the
1758 * beginning of the message.
1759 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1760 * of the message each time.
1761 * -# Call psa_cipher_finish().
1762 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001763 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1764 * the operation will need to be reset by a call to psa_cipher_abort(). The
1765 * application may call psa_cipher_abort() at any time after the operation
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001766 * has been initialized.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001767 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001768 * After a successful call to psa_cipher_decrypt_setup(), the application must
Antonio de Angelis14276e92018-07-10 14:35:43 +01001769 * eventually terminate the operation. The following events terminate an
1770 * operation:
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001771 * - A successful call to psa_cipher_finish().
1772 * - A call to psa_cipher_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001773 *
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001774 * \param[in,out] operation The operation object to set up. It must have
1775 * been initialized as per the documentation for
1776 * #psa_cipher_operation_t and not yet in use.
Maulik Patel28659c42021-01-06 14:09:22 +00001777 * \param key Identifier of the key to use for the operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001778 * It must remain valid until the operation
Maulik Patel28659c42021-01-06 14:09:22 +00001779 * terminates. It must allow the usage
1780 * #PSA_KEY_USAGE_DECRYPT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001781 * \param alg The cipher algorithm to compute
1782 * (\c PSA_ALG_XXX value such that
1783 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +01001784 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001785 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001786 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001787 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00001788 * \retval #PSA_ERROR_NOT_PERMITTED
1789 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00001790 * \p key is not compatible with \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001791 * \retval #PSA_ERROR_NOT_SUPPORTED
1792 * \p alg is not supported or is not a cipher algorithm.
1793 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1794 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1795 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001796 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1797 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001798 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001799 * The operation state is not valid (it must be inactive), or
1800 * the library has not been previously initialized by psa_crypto_init().
Jamie Fox0e54ebc2019-04-09 14:21:04 +01001801 * It is implementation-dependent whether a failure to initialize
1802 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001803 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001804psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00001805 psa_key_id_t key,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001806 psa_algorithm_t alg);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001807
Antonio de Angelis377a1552018-11-22 17:02:40 +00001808/** Generate an IV for a symmetric encryption operation.
1809 *
1810 * This function generates a random IV (initialization vector), nonce
1811 * or initial counter value for the encryption operation as appropriate
1812 * for the chosen algorithm, key type and key size.
1813 *
1814 * The application must call psa_cipher_encrypt_setup() before
1815 * calling this function.
1816 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001817 * If this function returns an error status, the operation enters an error
1818 * state and must be aborted by calling psa_cipher_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001819 *
1820 * \param[in,out] operation Active cipher operation.
1821 * \param[out] iv Buffer where the generated IV is to be written.
1822 * \param iv_size Size of the \p iv buffer in bytes.
1823 * \param[out] iv_length On success, the number of bytes of the
1824 * generated IV.
1825 *
1826 * \retval #PSA_SUCCESS
1827 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001828 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1829 * The size of the \p iv buffer is too small.
1830 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1831 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1832 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001833 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1834 * \retval #PSA_ERROR_STORAGE_FAILURE
1835 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001836 * The operation state is not valid (it must be active, with no IV set),
1837 * or the library has not been previously initialized
1838 * by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001839 * It is implementation-dependent whether a failure to initialize
1840 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001841 */
1842psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001843 uint8_t *iv,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001844 size_t iv_size,
1845 size_t *iv_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001846
Antonio de Angelis377a1552018-11-22 17:02:40 +00001847/** Set the IV for a symmetric encryption or decryption operation.
1848 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001849 * This function sets the IV (initialization vector), nonce
Antonio de Angelis377a1552018-11-22 17:02:40 +00001850 * or initial counter value for the encryption or decryption operation.
1851 *
1852 * The application must call psa_cipher_encrypt_setup() before
1853 * calling this function.
1854 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001855 * If this function returns an error status, the operation enters an error
1856 * state and must be aborted by calling psa_cipher_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001857 *
1858 * \note When encrypting, applications should use psa_cipher_generate_iv()
1859 * instead of this function, unless implementing a protocol that requires
1860 * a non-random IV.
1861 *
1862 * \param[in,out] operation Active cipher operation.
1863 * \param[in] iv Buffer containing the IV to use.
1864 * \param iv_length Size of the IV in bytes.
1865 *
1866 * \retval #PSA_SUCCESS
1867 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001868 * \retval #PSA_ERROR_INVALID_ARGUMENT
1869 * The size of \p iv is not acceptable for the chosen algorithm,
1870 * or the chosen algorithm does not use an IV.
1871 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1872 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1873 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001874 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1875 * \retval #PSA_ERROR_STORAGE_FAILURE
1876 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001877 * The operation state is not valid (it must be an active cipher
1878 * encrypt operation, with no IV set), or the library has not been
1879 * previously initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001880 * It is implementation-dependent whether a failure to initialize
1881 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001882 */
1883psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001884 const uint8_t *iv,
Antonio de Angelis377a1552018-11-22 17:02:40 +00001885 size_t iv_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001886
Antonio de Angelis377a1552018-11-22 17:02:40 +00001887/** Encrypt or decrypt a message fragment in an active cipher operation.
1888 *
1889 * Before calling this function, you must:
1890 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1891 * The choice of setup function determines whether this function
1892 * encrypts or decrypts its input.
1893 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1894 * (recommended when encrypting) or psa_cipher_set_iv().
1895 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001896 * If this function returns an error status, the operation enters an error
1897 * state and must be aborted by calling psa_cipher_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001898 *
1899 * \param[in,out] operation Active cipher operation.
1900 * \param[in] input Buffer containing the message fragment to
1901 * encrypt or decrypt.
1902 * \param input_length Size of the \p input buffer in bytes.
1903 * \param[out] output Buffer where the output is to be written.
1904 * \param output_size Size of the \p output buffer in bytes.
1905 * \param[out] output_length On success, the number of bytes
1906 * that make up the returned output.
1907 *
1908 * \retval #PSA_SUCCESS
1909 * Success.
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
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001918 * The operation state is not valid (it must be active, with an IV set
1919 * if required for the algorithm), or the library has not been
1920 * previously initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001921 * It is implementation-dependent whether a failure to initialize
1922 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001923 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001924psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1925 const uint8_t *input,
1926 size_t input_length,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001927 uint8_t *output,
Antonio de Angelis14276e92018-07-10 14:35:43 +01001928 size_t output_size,
1929 size_t *output_length);
1930
Antonio de Angelis377a1552018-11-22 17:02:40 +00001931/** Finish encrypting or decrypting a message in a cipher operation.
1932 *
1933 * The application must call psa_cipher_encrypt_setup() or
1934 * psa_cipher_decrypt_setup() before calling this function. The choice
1935 * of setup function determines whether this function encrypts or
1936 * decrypts its input.
1937 *
1938 * This function finishes the encryption or decryption of the message
1939 * formed by concatenating the inputs passed to preceding calls to
1940 * psa_cipher_update().
1941 *
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001942 * When this function returns successfully, the operation becomes inactive.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001943 * If this function returns an error status, the operation enters an error
1944 * state and must be aborted by calling psa_cipher_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001945 *
1946 * \param[in,out] operation Active cipher operation.
1947 * \param[out] output Buffer where the output is to be written.
1948 * \param output_size Size of the \p output buffer in bytes.
1949 * \param[out] output_length On success, the number of bytes
1950 * that make up the returned output.
1951 *
1952 * \retval #PSA_SUCCESS
1953 * Success.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001954 * \retval #PSA_ERROR_INVALID_ARGUMENT
1955 * The total input size passed to this operation is not valid for
1956 * this particular algorithm. For example, the algorithm is a based
1957 * on block cipher and requires a whole number of blocks, but the
1958 * total input size is not a multiple of the block size.
1959 * \retval #PSA_ERROR_INVALID_PADDING
1960 * This is a decryption operation for an algorithm that includes
1961 * padding, and the ciphertext does not contain valid padding.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001962 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1963 * The size of the \p output buffer is too small.
1964 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1965 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1966 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001967 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1968 * \retval #PSA_ERROR_STORAGE_FAILURE
1969 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001970 * The operation state is not valid (it must be active, with an IV set
1971 * if required for the algorithm), or the library has not been
1972 * previously initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001973 * It is implementation-dependent whether a failure to initialize
1974 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001975 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001976psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
1977 uint8_t *output,
1978 size_t output_size,
1979 size_t *output_length);
1980
Antonio de Angelis377a1552018-11-22 17:02:40 +00001981/** Abort a cipher operation.
1982 *
1983 * Aborting an operation frees all associated resources except for the
1984 * \p operation structure itself. Once aborted, the operation object
1985 * can be reused for another operation by calling
1986 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
1987 *
1988 * You may call this function any time after the operation object has
Antonio de Angelis04debbd2019-10-14 12:12:52 +01001989 * been initialized as described in #psa_cipher_operation_t.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001990 *
1991 * In particular, calling psa_cipher_abort() after the operation has been
1992 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1993 * is safe and has no effect.
1994 *
1995 * \param[in,out] operation Initialized cipher operation.
1996 *
1997 * \retval #PSA_SUCCESS
Antonio de Angelis377a1552018-11-22 17:02:40 +00001998 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1999 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002000 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2001 * \retval #PSA_ERROR_BAD_STATE
2002 * The library has not been previously initialized by psa_crypto_init().
2003 * It is implementation-dependent whether a failure to initialize
2004 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002005 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01002006psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2007
2008/**@}*/
2009
2010/** \defgroup aead Authenticated encryption with associated data (AEAD)
2011 * @{
2012 */
2013
Antonio de Angelis14276e92018-07-10 14:35:43 +01002014/** Process an authenticated encryption operation.
2015 *
Maulik Patel28659c42021-01-06 14:09:22 +00002016 * \param key Identifier of the key to use for the
2017 * operation. It must allow the usage
2018 * #PSA_KEY_USAGE_ENCRYPT.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002019 * \param alg The AEAD algorithm to compute
2020 * (\c PSA_ALG_XXX value such that
Antonio de Angelis377a1552018-11-22 17:02:40 +00002021 * #PSA_ALG_IS_AEAD(\p alg) is true).
2022 * \param[in] nonce Nonce or IV to use.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002023 * \param nonce_length Size of the \p nonce buffer in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002024 * \param[in] additional_data Additional data that will be authenticated
Antonio de Angelis14276e92018-07-10 14:35:43 +01002025 * but not encrypted.
2026 * \param additional_data_length Size of \p additional_data in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002027 * \param[in] plaintext Data that will be authenticated and
Antonio de Angelis14276e92018-07-10 14:35:43 +01002028 * encrypted.
2029 * \param plaintext_length Size of \p plaintext in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002030 * \param[out] ciphertext Output buffer for the authenticated and
Antonio de Angelis14276e92018-07-10 14:35:43 +01002031 * encrypted data. The additional data is not
2032 * part of this output. For algorithms where the
2033 * encrypted data and the authentication tag
2034 * are defined as separate outputs, the
2035 * authentication tag is appended to the
2036 * encrypted data.
2037 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Summer Qin359167d2021-07-05 18:11:50 +08002038 * This must be appropriate for the selected
2039 * algorithm and key:
2040 * - A sufficient output size is
2041 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type,
2042 * \p alg, \p plaintext_length) where
2043 * \c key_type is the type of \p key.
2044 * - #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p
2045 * plaintext_length) evaluates to the maximum
2046 * ciphertext size of any supported AEAD
2047 * encryption.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002048 * \param[out] ciphertext_length On success, the size of the output
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002049 * in the \p ciphertext buffer.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002050 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002051 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01002052 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002053 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00002054 * \retval #PSA_ERROR_NOT_PERMITTED
2055 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00002056 * \p key is not compatible with \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002057 * \retval #PSA_ERROR_NOT_SUPPORTED
2058 * \p alg is not supported or is not an AEAD algorithm.
2059 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002060 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Summer Qin359167d2021-07-05 18:11:50 +08002061 * \p ciphertext_size is too small.
2062 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2063 * \p plaintext_length) or
2064 * #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length) can be used to
2065 * determine the required buffer size.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002066 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2067 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002068 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2069 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002070 * \retval #PSA_ERROR_BAD_STATE
2071 * The library has not been previously initialized by psa_crypto_init().
2072 * It is implementation-dependent whether a failure to initialize
2073 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002074 */
Maulik Patel28659c42021-01-06 14:09:22 +00002075psa_status_t psa_aead_encrypt(psa_key_id_t key,
Antonio de Angelis377a1552018-11-22 17:02:40 +00002076 psa_algorithm_t alg,
2077 const uint8_t *nonce,
2078 size_t nonce_length,
2079 const uint8_t *additional_data,
2080 size_t additional_data_length,
2081 const uint8_t *plaintext,
2082 size_t plaintext_length,
2083 uint8_t *ciphertext,
2084 size_t ciphertext_size,
2085 size_t *ciphertext_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01002086
2087/** Process an authenticated decryption operation.
2088 *
Maulik Patel28659c42021-01-06 14:09:22 +00002089 * \param key Identifier of the key to use for the
2090 * operation. It must allow the usage
2091 * #PSA_KEY_USAGE_DECRYPT.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002092 * \param alg The AEAD algorithm to compute
2093 * (\c PSA_ALG_XXX value such that
Antonio de Angelis377a1552018-11-22 17:02:40 +00002094 * #PSA_ALG_IS_AEAD(\p alg) is true).
2095 * \param[in] nonce Nonce or IV to use.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002096 * \param nonce_length Size of the \p nonce buffer in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002097 * \param[in] additional_data Additional data that has been authenticated
Antonio de Angelis14276e92018-07-10 14:35:43 +01002098 * but not encrypted.
2099 * \param additional_data_length Size of \p additional_data in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002100 * \param[in] ciphertext Data that has been authenticated and
Antonio de Angelis14276e92018-07-10 14:35:43 +01002101 * encrypted. For algorithms where the
2102 * encrypted data and the authentication tag
2103 * are defined as separate inputs, the buffer
2104 * must contain the encrypted data followed
2105 * by the authentication tag.
2106 * \param ciphertext_length Size of \p ciphertext in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002107 * \param[out] plaintext Output buffer for the decrypted data.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002108 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Summer Qin359167d2021-07-05 18:11:50 +08002109 * This must be appropriate for the selected
2110 * algorithm and key:
2111 * - A sufficient output size is
2112 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type,
2113 * \p alg, \p ciphertext_length) where
2114 * \c key_type is the type of \p key.
2115 * - #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p
2116 * ciphertext_length) evaluates to the maximum
2117 * plaintext size of any supported AEAD
2118 * decryption.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002119 * \param[out] plaintext_length On success, the size of the output
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002120 * in the \p plaintext buffer.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002121 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002122 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01002123 * Success.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002124 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis377a1552018-11-22 17:02:40 +00002125 * \retval #PSA_ERROR_INVALID_SIGNATURE
Antonio de Angelis14276e92018-07-10 14:35:43 +01002126 * The ciphertext is not authentic.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002127 * \retval #PSA_ERROR_NOT_PERMITTED
2128 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00002129 * \p key is not compatible with \p alg.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002130 * \retval #PSA_ERROR_NOT_SUPPORTED
2131 * \p alg is not supported or is not an AEAD algorithm.
2132 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002133 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Summer Qin359167d2021-07-05 18:11:50 +08002134 * \p plaintext_size is too small.
2135 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\c key_type, \p alg,
2136 * \p ciphertext_length) or
2137 * #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length) can be used
2138 * to determine the required buffer size.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002139 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2140 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002141 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2142 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002143 * \retval #PSA_ERROR_BAD_STATE
2144 * The library has not been previously initialized by psa_crypto_init().
2145 * It is implementation-dependent whether a failure to initialize
2146 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002147 */
Maulik Patel28659c42021-01-06 14:09:22 +00002148psa_status_t psa_aead_decrypt(psa_key_id_t key,
Antonio de Angelis377a1552018-11-22 17:02:40 +00002149 psa_algorithm_t alg,
2150 const uint8_t *nonce,
2151 size_t nonce_length,
2152 const uint8_t *additional_data,
2153 size_t additional_data_length,
2154 const uint8_t *ciphertext,
2155 size_t ciphertext_length,
2156 uint8_t *plaintext,
2157 size_t plaintext_size,
2158 size_t *plaintext_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01002159
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002160/** The type of the state data structure for multipart AEAD operations.
2161 *
2162 * Before calling any function on an AEAD operation object, the application
2163 * must initialize it by any of the following means:
2164 * - Set the structure to all-bits-zero, for example:
2165 * \code
2166 * psa_aead_operation_t operation;
2167 * memset(&operation, 0, sizeof(operation));
2168 * \endcode
2169 * - Initialize the structure to logical zero values, for example:
2170 * \code
2171 * psa_aead_operation_t operation = {0};
2172 * \endcode
2173 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2174 * for example:
2175 * \code
2176 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2177 * \endcode
2178 * - Assign the result of the function psa_aead_operation_init()
2179 * to the structure, for example:
2180 * \code
2181 * psa_aead_operation_t operation;
2182 * operation = psa_aead_operation_init();
2183 * \endcode
2184 *
2185 * This is an implementation-defined \c struct. Applications should not
Summer Qin359167d2021-07-05 18:11:50 +08002186 * make any assumptions about the content of this structure.
2187 * Implementation details can change in future versions without notice. */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002188typedef struct psa_aead_operation_s psa_aead_operation_t;
2189
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002190/** \def PSA_AEAD_OPERATION_INIT
2191 *
2192 * This macro returns a suitable initializer for an AEAD operation object of
2193 * type #psa_aead_operation_t.
2194 */
2195
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002196/** Return an initial value for an AEAD operation object.
2197 */
2198static psa_aead_operation_t psa_aead_operation_init(void);
2199
2200/** Set the key for a multipart authenticated encryption operation.
2201 *
2202 * The sequence of operations to encrypt a message with authentication
2203 * is as follows:
2204 * -# Allocate an operation object which will be passed to all the functions
2205 * listed here.
2206 * -# Initialize the operation object with one of the methods described in the
2207 * documentation for #psa_aead_operation_t, e.g.
2208 * #PSA_AEAD_OPERATION_INIT.
2209 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
2210 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2211 * inputs to the subsequent calls to psa_aead_update_ad() and
2212 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2213 * for details.
2214 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2215 * generate or set the nonce. You should use
2216 * psa_aead_generate_nonce() unless the protocol you are implementing
2217 * requires a specific nonce value.
2218 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2219 * of the non-encrypted additional authenticated data each time.
2220 * -# Call psa_aead_update() zero, one or more times, passing a fragment
2221 * of the message to encrypt each time.
2222 * -# Call psa_aead_finish().
2223 *
2224 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2225 * the operation will need to be reset by a call to psa_aead_abort(). The
2226 * application may call psa_aead_abort() at any time after the operation
2227 * has been initialized.
2228 *
2229 * After a successful call to psa_aead_encrypt_setup(), the application must
2230 * eventually terminate the operation. The following events terminate an
2231 * operation:
2232 * - A successful call to psa_aead_finish().
2233 * - A call to psa_aead_abort().
2234 *
2235 * \param[in,out] operation The operation object to set up. It must have
2236 * been initialized as per the documentation for
2237 * #psa_aead_operation_t and not yet in use.
Maulik Patel28659c42021-01-06 14:09:22 +00002238 * \param key Identifier of the key to use for the operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002239 * It must remain valid until the operation
Maulik Patel28659c42021-01-06 14:09:22 +00002240 * terminates. It must allow the usage
2241 * #PSA_KEY_USAGE_ENCRYPT.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002242 * \param alg The AEAD algorithm to compute
2243 * (\c PSA_ALG_XXX value such that
2244 * #PSA_ALG_IS_AEAD(\p alg) is true).
2245 *
2246 * \retval #PSA_SUCCESS
2247 * Success.
2248 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002249 * The operation state is not valid (it must be inactive), or
2250 * the library has not been previously initialized by psa_crypto_init().
Maulik Patel28659c42021-01-06 14:09:22 +00002251 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002252 * \retval #PSA_ERROR_NOT_PERMITTED
2253 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00002254 * \p key is not compatible with \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002255 * \retval #PSA_ERROR_NOT_SUPPORTED
2256 * \p alg is not supported or is not an AEAD algorithm.
2257 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2258 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2259 * \retval #PSA_ERROR_HARDWARE_FAILURE
2260 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2261 * \retval #PSA_ERROR_STORAGE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002262 * The library has not been previously initialized by psa_crypto_init().
2263 * It is implementation-dependent whether a failure to initialize
2264 * results in this error code.
2265 */
2266psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00002267 psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002268 psa_algorithm_t alg);
2269
2270/** Set the key for a multipart authenticated decryption operation.
2271 *
2272 * The sequence of operations to decrypt a message with authentication
2273 * is as follows:
2274 * -# Allocate an operation object which will be passed to all the functions
2275 * listed here.
2276 * -# Initialize the operation object with one of the methods described in the
2277 * documentation for #psa_aead_operation_t, e.g.
2278 * #PSA_AEAD_OPERATION_INIT.
2279 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
2280 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2281 * inputs to the subsequent calls to psa_aead_update_ad() and
2282 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2283 * for details.
2284 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2285 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2286 * of the non-encrypted additional authenticated data each time.
2287 * -# Call psa_aead_update() zero, one or more times, passing a fragment
2288 * of the ciphertext to decrypt each time.
2289 * -# Call psa_aead_verify().
2290 *
2291 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2292 * the operation will need to be reset by a call to psa_aead_abort(). The
2293 * application may call psa_aead_abort() at any time after the operation
2294 * has been initialized.
2295 *
2296 * After a successful call to psa_aead_decrypt_setup(), the application must
2297 * eventually terminate the operation. The following events terminate an
2298 * operation:
2299 * - A successful call to psa_aead_verify().
2300 * - A call to psa_aead_abort().
2301 *
2302 * \param[in,out] operation The operation object to set up. It must have
2303 * been initialized as per the documentation for
2304 * #psa_aead_operation_t and not yet in use.
Maulik Patel28659c42021-01-06 14:09:22 +00002305 * \param key Identifier of the key to use for the operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002306 * It must remain valid until the operation
Maulik Patel28659c42021-01-06 14:09:22 +00002307 * terminates. It must allow the usage
2308 * #PSA_KEY_USAGE_DECRYPT.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002309 * \param alg The AEAD algorithm to compute
2310 * (\c PSA_ALG_XXX value such that
2311 * #PSA_ALG_IS_AEAD(\p alg) is true).
2312 *
2313 * \retval #PSA_SUCCESS
2314 * Success.
Maulik Patel28659c42021-01-06 14:09:22 +00002315 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002316 * \retval #PSA_ERROR_NOT_PERMITTED
2317 * \retval #PSA_ERROR_INVALID_ARGUMENT
Maulik Patel28659c42021-01-06 14:09:22 +00002318 * \p key is not compatible with \p alg.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002319 * \retval #PSA_ERROR_NOT_SUPPORTED
2320 * \p alg is not supported or is not an AEAD algorithm.
2321 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2322 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2323 * \retval #PSA_ERROR_HARDWARE_FAILURE
2324 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2325 * \retval #PSA_ERROR_STORAGE_FAILURE
2326 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002327 * The operation state is not valid (it must be inactive), or the
2328 * library has not been previously initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002329 * It is implementation-dependent whether a failure to initialize
2330 * results in this error code.
2331 */
2332psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00002333 psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002334 psa_algorithm_t alg);
2335
2336/** Generate a random nonce for an authenticated encryption operation.
2337 *
2338 * This function generates a random nonce for the authenticated encryption
2339 * operation with an appropriate size for the chosen algorithm, key type
2340 * and key size.
2341 *
2342 * The application must call psa_aead_encrypt_setup() before
2343 * calling this function.
2344 *
2345 * If this function returns an error status, the operation enters an error
2346 * state and must be aborted by calling psa_aead_abort().
2347 *
2348 * \param[in,out] operation Active AEAD operation.
2349 * \param[out] nonce Buffer where the generated nonce is to be
2350 * written.
2351 * \param nonce_size Size of the \p nonce buffer in bytes.
2352 * \param[out] nonce_length On success, the number of bytes of the
2353 * generated nonce.
2354 *
2355 * \retval #PSA_SUCCESS
2356 * Success.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002357 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2358 * The size of the \p nonce buffer is too small.
2359 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2360 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2361 * \retval #PSA_ERROR_HARDWARE_FAILURE
2362 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2363 * \retval #PSA_ERROR_STORAGE_FAILURE
2364 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002365 * The operation state is not valid (it must be an active aead encrypt
2366 * operation, with no nonce set), or the library has not been
2367 * previously initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002368 * It is implementation-dependent whether a failure to initialize
2369 * results in this error code.
2370 */
2371psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2372 uint8_t *nonce,
2373 size_t nonce_size,
2374 size_t *nonce_length);
2375
2376/** Set the nonce for an authenticated encryption or decryption operation.
2377 *
2378 * This function sets the nonce for the authenticated
2379 * encryption or decryption operation.
2380 *
2381 * The application must call psa_aead_encrypt_setup() or
2382 * psa_aead_decrypt_setup() before calling this function.
2383 *
2384 * If this function returns an error status, the operation enters an error
2385 * state and must be aborted by calling psa_aead_abort().
2386 *
2387 * \note When encrypting, applications should use psa_aead_generate_nonce()
2388 * instead of this function, unless implementing a protocol that requires
2389 * a non-random IV.
2390 *
2391 * \param[in,out] operation Active AEAD operation.
2392 * \param[in] nonce Buffer containing the nonce to use.
2393 * \param nonce_length Size of the nonce in bytes.
2394 *
2395 * \retval #PSA_SUCCESS
2396 * Success.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002397 * \retval #PSA_ERROR_INVALID_ARGUMENT
2398 * The size of \p nonce is not acceptable for the chosen algorithm.
2399 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2400 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2401 * \retval #PSA_ERROR_HARDWARE_FAILURE
2402 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2403 * \retval #PSA_ERROR_STORAGE_FAILURE
2404 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002405 * The operation state is not valid (it must be active, with no nonce
2406 * set), or the library has not been previously initialized
2407 * by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002408 * It is implementation-dependent whether a failure to initialize
2409 * results in this error code.
2410 */
2411psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2412 const uint8_t *nonce,
2413 size_t nonce_length);
2414
2415/** Declare the lengths of the message and additional data for AEAD.
2416 *
2417 * The application must call this function before calling
2418 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2419 * the operation requires it. If the algorithm does not require it,
2420 * calling this function is optional, but if this function is called
2421 * then the implementation must enforce the lengths.
2422 *
2423 * You may call this function before or after setting the nonce with
2424 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2425 *
2426 * - For #PSA_ALG_CCM, calling this function is required.
2427 * - For the other AEAD algorithms defined in this specification, calling
2428 * this function is not required.
2429 * - For vendor-defined algorithm, refer to the vendor documentation.
2430 *
2431 * If this function returns an error status, the operation enters an error
2432 * state and must be aborted by calling psa_aead_abort().
2433 *
2434 * \param[in,out] operation Active AEAD operation.
2435 * \param ad_length Size of the non-encrypted additional
2436 * authenticated data in bytes.
2437 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2438 *
2439 * \retval #PSA_SUCCESS
2440 * Success.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002441 * \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
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002449 * The operation state is not valid (it must be active, and
2450 * psa_aead_update_ad() and psa_aead_update() must not have been
2451 * called yet), or the library has not been previously initialized
2452 * by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002453 * It is implementation-dependent whether a failure to initialize
2454 * results in this error code.
2455 */
2456psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2457 size_t ad_length,
2458 size_t plaintext_length);
2459
2460/** Pass additional data to an active AEAD operation.
2461 *
2462 * Additional data is authenticated, but not encrypted.
2463 *
2464 * You may call this function multiple times to pass successive fragments
2465 * of the additional data. You may not call this function after passing
2466 * data to encrypt or decrypt with psa_aead_update().
2467 *
2468 * Before calling this function, you must:
2469 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2470 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2471 *
2472 * If this function returns an error status, the operation enters an error
2473 * state and must be aborted by calling psa_aead_abort().
2474 *
2475 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2476 * there is no guarantee that the input is valid. Therefore, until
2477 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2478 * treat the input as untrusted and prepare to undo any action that
2479 * depends on the input if psa_aead_verify() returns an error status.
2480 *
2481 * \param[in,out] operation Active AEAD operation.
2482 * \param[in] input Buffer containing the fragment of
2483 * additional data.
2484 * \param input_length Size of the \p input buffer in bytes.
2485 *
2486 * \retval #PSA_SUCCESS
2487 * Success.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002488 * \retval #PSA_ERROR_INVALID_ARGUMENT
2489 * The total input length overflows the additional data length that
2490 * was previously specified with psa_aead_set_lengths().
2491 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2492 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2493 * \retval #PSA_ERROR_HARDWARE_FAILURE
2494 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2495 * \retval #PSA_ERROR_STORAGE_FAILURE
2496 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002497 * The operation state is not valid (it must be active, have a nonce
2498 * set, have lengths set if required by the algorithm, and
2499 * psa_aead_update() must not have been called yet), or the library
2500 * has not been previously initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002501 * It is implementation-dependent whether a failure to initialize
2502 * results in this error code.
2503 */
2504psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2505 const uint8_t *input,
2506 size_t input_length);
2507
2508/** Encrypt or decrypt a message fragment in an active AEAD operation.
2509 *
2510 * Before calling this function, you must:
2511 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2512 * The choice of setup function determines whether this function
2513 * encrypts or decrypts its input.
2514 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2515 * 3. Call psa_aead_update_ad() to pass all the additional data.
2516 *
2517 * If this function returns an error status, the operation enters an error
2518 * state and must be aborted by calling psa_aead_abort().
2519 *
2520 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2521 * there is no guarantee that the input is valid. Therefore, until
2522 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2523 * - Do not use the output in any way other than storing it in a
2524 * confidential location. If you take any action that depends
2525 * on the tentative decrypted data, this action will need to be
2526 * undone if the input turns out not to be valid. Furthermore,
2527 * if an adversary can observe that this action took place
2528 * (for example through timing), they may be able to use this
2529 * fact as an oracle to decrypt any message encrypted with the
2530 * same key.
2531 * - In particular, do not copy the output anywhere but to a
2532 * memory or storage space that you have exclusive access to.
2533 *
2534 * This function does not require the input to be aligned to any
2535 * particular block boundary. If the implementation can only process
2536 * a whole block at a time, it must consume all the input provided, but
2537 * it may delay the end of the corresponding output until a subsequent
2538 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2539 * provides sufficient input. The amount of data that can be delayed
2540 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
2541 *
2542 * \param[in,out] operation Active AEAD operation.
2543 * \param[in] input Buffer containing the message fragment to
2544 * encrypt or decrypt.
2545 * \param input_length Size of the \p input buffer in bytes.
2546 * \param[out] output Buffer where the output is to be written.
2547 * \param output_size Size of the \p output buffer in bytes.
Summer Qin359167d2021-07-05 18:11:50 +08002548 * This must be appropriate for the selected
2549 * algorithm and key:
2550 * - A sufficient output size is
2551 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type,
2552 * \c alg, \p input_length) where
2553 * \c key_type is the type of key and \c alg is
2554 * the algorithm that were used to set up the
2555 * operation.
2556 * - #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p
2557 * input_length) evaluates to the maximum
2558 * output size of any supported AEAD
2559 * algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002560 * \param[out] output_length On success, the number of bytes
2561 * that make up the returned output.
2562 *
2563 * \retval #PSA_SUCCESS
2564 * Success.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002565 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2566 * The size of the \p output buffer is too small.
Summer Qin359167d2021-07-05 18:11:50 +08002567 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c key_type, \c alg, \p input_length) or
2568 * #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length) can be used to
2569 * determine the required buffer size.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002570 * \retval #PSA_ERROR_INVALID_ARGUMENT
2571 * The total length of input to psa_aead_update_ad() so far is
2572 * less than the additional data length that was previously
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002573 * specified with psa_aead_set_lengths(), or
2574 * the total input length overflows the plaintext length that
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002575 * was previously specified with psa_aead_set_lengths().
2576 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2577 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2578 * \retval #PSA_ERROR_HARDWARE_FAILURE
2579 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2580 * \retval #PSA_ERROR_STORAGE_FAILURE
2581 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002582 * The operation state is not valid (it must be active, have a nonce
2583 * set, and have lengths set if required by the algorithm), or the
2584 * library has not been previously initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002585 * 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 *
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002609 * When this function returns successfully, the operation becomes inactive.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002610 * 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.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002648 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2649 * The size of the \p ciphertext or \p tag buffer is too small.
Summer Qin359167d2021-07-05 18:11:50 +08002650 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c key_type, \c alg) or
2651 * #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE can be used to determine the
2652 * required \p ciphertext buffer size. #PSA_AEAD_TAG_LENGTH(\c key_type,
2653 * \c key_bits, \c alg) or #PSA_AEAD_TAG_MAX_SIZE can be used to
2654 * determine the required \p tag buffer size.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002655 * \retval #PSA_ERROR_INVALID_ARGUMENT
2656 * The total length of input to psa_aead_update_ad() so far is
2657 * less than the additional data length that was previously
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002658 * specified with psa_aead_set_lengths(), or
2659 * the total length of input to psa_aead_update() so far is
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002660 * less than the plaintext length that was previously
2661 * specified with psa_aead_set_lengths().
2662 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2663 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2664 * \retval #PSA_ERROR_HARDWARE_FAILURE
2665 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2666 * \retval #PSA_ERROR_STORAGE_FAILURE
2667 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002668 * The operation state is not valid (it must be an active encryption
2669 * operation with a nonce set), or the library has not been previously
2670 * initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002671 * It is implementation-dependent whether a failure to initialize
2672 * results in this error code.
2673 */
2674psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
2675 uint8_t *ciphertext,
2676 size_t ciphertext_size,
2677 size_t *ciphertext_length,
2678 uint8_t *tag,
2679 size_t tag_size,
2680 size_t *tag_length);
2681
2682/** Finish authenticating and decrypting a message in an AEAD operation.
2683 *
2684 * The operation must have been set up with psa_aead_decrypt_setup().
2685 *
2686 * This function finishes the authenticated decryption of the message
2687 * components:
2688 *
2689 * - The additional data consisting of the concatenation of the inputs
2690 * passed to preceding calls to psa_aead_update_ad().
2691 * - The ciphertext consisting of the concatenation of the inputs passed to
2692 * preceding calls to psa_aead_update().
2693 * - The tag passed to this function call.
2694 *
2695 * If the authentication tag is correct, this function outputs any remaining
2696 * plaintext and reports success. If the authentication tag is not correct,
2697 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
2698 *
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002699 * When this function returns successfully, the operation becomes inactive.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002700 * If this function returns an error status, the operation enters an error
2701 * state and must be aborted by calling psa_aead_abort().
2702 *
2703 * \note Implementations shall make the best effort to ensure that the
2704 * comparison between the actual tag and the expected tag is performed
2705 * in constant time.
2706 *
2707 * \param[in,out] operation Active AEAD operation.
2708 * \param[out] plaintext Buffer where the last part of the plaintext
2709 * is to be written. This is the remaining data
2710 * from previous calls to psa_aead_update()
2711 * that could not be processed until the end
2712 * of the input.
2713 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Summer Qin359167d2021-07-05 18:11:50 +08002714 * This must be appropriate for the selected algorithm and key:
2715 * - A sufficient output size is
2716 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type,
2717 * \c alg) where \c key_type is the type of key
2718 * and \c alg is the algorithm that were used to
2719 * set up the operation.
2720 * - #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE evaluates to
2721 * the maximum output size of any supported AEAD
2722 * algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002723 * \param[out] plaintext_length On success, the number of bytes of
2724 * returned plaintext.
2725 * \param[in] tag Buffer containing the authentication tag.
2726 * \param tag_length Size of the \p tag buffer in bytes.
2727 *
2728 * \retval #PSA_SUCCESS
2729 * Success.
2730 * \retval #PSA_ERROR_INVALID_SIGNATURE
2731 * The calculations were successful, but the authentication tag is
2732 * not correct.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002733 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2734 * The size of the \p plaintext buffer is too small.
Summer Qin359167d2021-07-05 18:11:50 +08002735 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c key_type, \c alg) or
2736 * #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE can be used to determine the
2737 * required buffer size.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002738 * \retval #PSA_ERROR_INVALID_ARGUMENT
2739 * The total length of input to psa_aead_update_ad() so far is
2740 * less than the additional data length that was previously
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002741 * specified with psa_aead_set_lengths(), or
2742 * the total length of input to psa_aead_update() so far is
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002743 * less than the plaintext length that was previously
2744 * specified with psa_aead_set_lengths().
2745 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2746 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2747 * \retval #PSA_ERROR_HARDWARE_FAILURE
2748 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2749 * \retval #PSA_ERROR_STORAGE_FAILURE
2750 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002751 * The operation state is not valid (it must be an active decryption
2752 * operation with a nonce set), or the library has not been previously
2753 * initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002754 * It is implementation-dependent whether a failure to initialize
2755 * results in this error code.
2756 */
2757psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2758 uint8_t *plaintext,
2759 size_t plaintext_size,
2760 size_t *plaintext_length,
2761 const uint8_t *tag,
2762 size_t tag_length);
2763
2764/** Abort an AEAD operation.
2765 *
2766 * Aborting an operation frees all associated resources except for the
2767 * \p operation structure itself. Once aborted, the operation object
2768 * can be reused for another operation by calling
2769 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2770 *
2771 * You may call this function any time after the operation object has
2772 * been initialized as described in #psa_aead_operation_t.
2773 *
2774 * In particular, calling psa_aead_abort() after the operation has been
2775 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2776 * psa_aead_verify() is safe and has no effect.
2777 *
2778 * \param[in,out] operation Initialized AEAD operation.
2779 *
2780 * \retval #PSA_SUCCESS
2781 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2782 * \retval #PSA_ERROR_HARDWARE_FAILURE
2783 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2784 * \retval #PSA_ERROR_BAD_STATE
2785 * The library has not been previously initialized by psa_crypto_init().
2786 * It is implementation-dependent whether a failure to initialize
2787 * results in this error code.
2788 */
2789psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2790
Antonio de Angelis14276e92018-07-10 14:35:43 +01002791/**@}*/
2792
2793/** \defgroup asymmetric Asymmetric cryptography
2794 * @{
2795 */
2796
2797/**
Summer Qin359167d2021-07-05 18:11:50 +08002798 * \brief Sign a message with a private key. For hash-and-sign algorithms,
2799 * this includes the hashing step.
2800 *
2801 * \note To perform a multi-part hash-and-sign signature algorithm, first use
2802 * a multi-part hash operation and then pass the resulting hash to
2803 * psa_sign_hash(). PSA_ALG_GET_HASH(\p alg) can be used to determine the
2804 * hash algorithm to use.
2805 *
2806 * \param[in] key Identifier of the key to use for the operation.
2807 * It must be an asymmetric key pair. The key must
2808 * allow the usage #PSA_KEY_USAGE_SIGN_MESSAGE.
2809 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
2810 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2811 * is true), that is compatible with the type of
2812 * \p key.
2813 * \param[in] input The input message to sign.
2814 * \param[in] input_length Size of the \p input buffer in bytes.
2815 * \param[out] signature Buffer where the signature is to be written.
2816 * \param[in] signature_size Size of the \p signature buffer in bytes. This
2817 * must be appropriate for the selected
2818 * algorithm and key:
2819 * - The required signature size is
2820 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2821 * where \c key_type and \c key_bits are the type and
2822 * bit-size respectively of key.
2823 * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
2824 * maximum signature size of any supported
2825 * signature algorithm.
2826 * \param[out] signature_length On success, the number of bytes that make up
2827 * the returned signature value.
2828 *
2829 * \retval #PSA_SUCCESS
2830 * \retval #PSA_ERROR_INVALID_HANDLE
2831 * \retval #PSA_ERROR_NOT_PERMITTED
2832 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2833 * or it does not permit the requested algorithm.
2834 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2835 * The size of the \p signature buffer is too small. You can
2836 * determine a sufficient buffer size by calling
2837 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2838 * where \c key_type and \c key_bits are the type and bit-size
2839 * respectively of \p key.
2840 * \retval #PSA_ERROR_NOT_SUPPORTED
2841 * \retval #PSA_ERROR_INVALID_ARGUMENT
2842 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2843 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2844 * \retval #PSA_ERROR_HARDWARE_FAILURE
2845 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2846 * \retval #PSA_ERROR_STORAGE_FAILURE
2847 * \retval #PSA_ERROR_DATA_CORRUPT
2848 * \retval #PSA_ERROR_DATA_INVALID
2849 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2850 * \retval #PSA_ERROR_BAD_STATE
2851 * The library has not been previously initialized by psa_crypto_init().
2852 * It is implementation-dependent whether a failure to initialize
2853 * results in this error code.
2854 */
2855psa_status_t psa_sign_message( psa_key_id_t key,
2856 psa_algorithm_t alg,
2857 const uint8_t * input,
2858 size_t input_length,
2859 uint8_t * signature,
2860 size_t signature_size,
2861 size_t * signature_length );
2862
2863/** \brief Verify the signature of a message with a public key, using
2864 * a hash-and-sign verification algorithm.
2865 *
2866 * \note To perform a multi-part hash-and-sign signature verification
2867 * algorithm, first use a multi-part hash operation to hash the message
2868 * and then pass the resulting hash to psa_verify_hash().
2869 * PSA_ALG_GET_HASH(\p alg) can be used to determine the hash algorithm
2870 * to use.
2871 *
2872 * \param[in] key Identifier of the key to use for the operation.
2873 * It must be a public key or an asymmetric key
2874 * pair. The key must allow the usage
2875 * #PSA_KEY_USAGE_VERIFY_MESSAGE.
2876 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
2877 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2878 * is true), that is compatible with the type of
2879 * \p key.
2880 * \param[in] input The message whose signature is to be verified.
2881 * \param[in] input_length Size of the \p input buffer in bytes.
2882 * \param[out] signature Buffer containing the signature to verify.
2883 * \param[in] signature_length Size of the \p signature buffer in bytes.
2884 *
2885 * \retval #PSA_SUCCESS
2886 * \retval #PSA_ERROR_INVALID_HANDLE
2887 * \retval #PSA_ERROR_NOT_PERMITTED
2888 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2889 * or it does not permit the requested algorithm.
2890 * \retval #PSA_ERROR_INVALID_SIGNATURE
2891 * The calculation was performed successfully, but the passed signature
2892 * is not a valid signature.
2893 * \retval #PSA_ERROR_NOT_SUPPORTED
2894 * \retval #PSA_ERROR_INVALID_ARGUMENT
2895 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2896 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2897 * \retval #PSA_ERROR_HARDWARE_FAILURE
2898 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2899 * \retval #PSA_ERROR_STORAGE_FAILURE
2900 * \retval #PSA_ERROR_DATA_CORRUPT
2901 * \retval #PSA_ERROR_DATA_INVALID
2902 * \retval #PSA_ERROR_BAD_STATE
2903 * The library has not been previously initialized by psa_crypto_init().
2904 * It is implementation-dependent whether a failure to initialize
2905 * results in this error code.
2906 */
2907psa_status_t psa_verify_message( psa_key_id_t key,
2908 psa_algorithm_t alg,
2909 const uint8_t * input,
2910 size_t input_length,
2911 const uint8_t * signature,
2912 size_t signature_length );
2913
2914/**
Antonio de Angelis14276e92018-07-10 14:35:43 +01002915 * \brief Sign a hash or short message with a private key.
2916 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002917 * Note that to perform a hash-and-sign signature algorithm, you must
2918 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Summer Qin359167d2021-07-05 18:11:50 +08002919 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
2920 * Then pass the resulting hash as the \p hash
Antonio de Angelis377a1552018-11-22 17:02:40 +00002921 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2922 * to determine the hash algorithm to use.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002923 *
Maulik Patel28659c42021-01-06 14:09:22 +00002924 * \param key Identifier of the key to use for the operation.
2925 * It must be an asymmetric key pair. The key must
2926 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
Summer Qinf07cc312022-01-05 16:52:54 +08002927 * \param alg A signature algorithm (PSA_ALG_XXX
2928 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
2929 * is true), that is compatible with
Maulik Patel28659c42021-01-06 14:09:22 +00002930 * the type of \p key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002931 * \param[in] hash The hash or message to sign.
2932 * \param hash_length Size of the \p hash buffer in bytes.
2933 * \param[out] signature Buffer where the signature is to be written.
2934 * \param signature_size Size of the \p signature buffer in bytes.
2935 * \param[out] signature_length On success, the number of bytes
2936 * that make up the returned signature value.
2937 *
2938 * \retval #PSA_SUCCESS
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002939 * \retval #PSA_ERROR_INVALID_HANDLE
2940 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis377a1552018-11-22 17:02:40 +00002941 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2942 * The size of the \p signature buffer is too small. You can
Antonio de Angelis14276e92018-07-10 14:35:43 +01002943 * determine a sufficient buffer size by calling
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002944 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01002945 * where \c key_type and \c key_bits are the type and bit-size
Maulik Patel28659c42021-01-06 14:09:22 +00002946 * respectively of \p key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002947 * \retval #PSA_ERROR_NOT_SUPPORTED
2948 * \retval #PSA_ERROR_INVALID_ARGUMENT
2949 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2950 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2951 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002952 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2953 * \retval #PSA_ERROR_STORAGE_FAILURE
Antonio de Angelis377a1552018-11-22 17:02:40 +00002954 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Jamie Fox0e54ebc2019-04-09 14:21:04 +01002955 * \retval #PSA_ERROR_BAD_STATE
2956 * The library has not been previously initialized by psa_crypto_init().
2957 * It is implementation-dependent whether a failure to initialize
2958 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002959 */
Maulik Patel28659c42021-01-06 14:09:22 +00002960psa_status_t psa_sign_hash(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002961 psa_algorithm_t alg,
2962 const uint8_t *hash,
2963 size_t hash_length,
2964 uint8_t *signature,
2965 size_t signature_size,
2966 size_t *signature_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01002967
2968/**
Summer Qin359167d2021-07-05 18:11:50 +08002969 * \brief Verify the signature of a hash or short message using a public key.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002970 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002971 * Note that to perform a hash-and-sign signature algorithm, you must
2972 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Summer Qin359167d2021-07-05 18:11:50 +08002973 * and psa_hash_finish(), or alternatively by calling psa_hash_compute().
2974 * Then pass the resulting hash as the \p hash
Antonio de Angelis377a1552018-11-22 17:02:40 +00002975 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2976 * to determine the hash algorithm to use.
2977 *
Maulik Patel28659c42021-01-06 14:09:22 +00002978 * \param key Identifier of the key to use for the operation. It
2979 * must be a public key or an asymmetric key pair. The
2980 * key must allow the usage
2981 * #PSA_KEY_USAGE_VERIFY_HASH.
Summer Qinf07cc312022-01-05 16:52:54 +08002982 * \param alg A signature algorithm (PSA_ALG_XXX
2983 * value such that #PSA_ALG_IS_SIGN_HASH(\p alg)
2984 * is true), that is compatible with
Maulik Patel28659c42021-01-06 14:09:22 +00002985 * the type of \p key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002986 * \param[in] hash The hash or message whose signature is to be
2987 * verified.
2988 * \param hash_length Size of the \p hash buffer in bytes.
2989 * \param[in] signature Buffer containing the signature to verify.
2990 * \param signature_length Size of the \p signature buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002991 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002992 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01002993 * The signature is valid.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01002994 * \retval #PSA_ERROR_INVALID_HANDLE
2995 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis377a1552018-11-22 17:02:40 +00002996 * \retval #PSA_ERROR_INVALID_SIGNATURE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002997 * The calculation was performed successfully, but the passed
Antonio de Angelis14276e92018-07-10 14:35:43 +01002998 * signature is not a valid signature.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002999 * \retval #PSA_ERROR_NOT_SUPPORTED
3000 * \retval #PSA_ERROR_INVALID_ARGUMENT
3001 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3002 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3003 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003004 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3005 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003006 * \retval #PSA_ERROR_BAD_STATE
3007 * The library has not been previously initialized by psa_crypto_init().
3008 * It is implementation-dependent whether a failure to initialize
3009 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003010 */
Maulik Patel28659c42021-01-06 14:09:22 +00003011psa_status_t psa_verify_hash(psa_key_id_t key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003012 psa_algorithm_t alg,
3013 const uint8_t *hash,
3014 size_t hash_length,
3015 const uint8_t *signature,
3016 size_t signature_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01003017
Antonio de Angelis14276e92018-07-10 14:35:43 +01003018/**
3019 * \brief Encrypt a short message with a public key.
3020 *
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01003021 * \param key Identifier of the key to use for the operation.
Maulik Patel28659c42021-01-06 14:09:22 +00003022 * It must be a public key or an asymmetric key
3023 * pair. It must allow the usage
3024 * #PSA_KEY_USAGE_ENCRYPT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003025 * \param alg An asymmetric encryption algorithm that is
Maulik Patel28659c42021-01-06 14:09:22 +00003026 * compatible with the type of \p key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003027 * \param[in] input The message to encrypt.
3028 * \param input_length Size of the \p input buffer in bytes.
3029 * \param[in] salt A salt or label, if supported by the
3030 * encryption algorithm.
3031 * If the algorithm does not support a
3032 * salt, pass \c NULL.
3033 * If the algorithm supports an optional
3034 * salt and you do not want to pass a salt,
3035 * pass \c NULL.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003036 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00003037 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3038 * supported.
3039 * \param salt_length Size of the \p salt buffer in bytes.
3040 * If \p salt is \c NULL, pass 0.
3041 * \param[out] output Buffer where the encrypted message is to
3042 * be written.
3043 * \param output_size Size of the \p output buffer in bytes.
3044 * \param[out] output_length On success, the number of bytes
3045 * that make up the returned output.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003046 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00003047 * \retval #PSA_SUCCESS
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003048 * \retval #PSA_ERROR_INVALID_HANDLE
3049 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis377a1552018-11-22 17:02:40 +00003050 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3051 * The size of the \p output buffer is too small. You can
Antonio de Angelis14276e92018-07-10 14:35:43 +01003052 * determine a sufficient buffer size by calling
Antonio de Angelis377a1552018-11-22 17:02:40 +00003053 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01003054 * where \c key_type and \c key_bits are the type and bit-size
Maulik Patel28659c42021-01-06 14:09:22 +00003055 * respectively of \p key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003056 * \retval #PSA_ERROR_NOT_SUPPORTED
3057 * \retval #PSA_ERROR_INVALID_ARGUMENT
3058 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3059 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3060 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003061 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3062 * \retval #PSA_ERROR_STORAGE_FAILURE
Antonio de Angelis377a1552018-11-22 17:02:40 +00003063 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003064 * \retval #PSA_ERROR_BAD_STATE
3065 * The library has not been previously initialized by psa_crypto_init().
3066 * It is implementation-dependent whether a failure to initialize
3067 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003068 */
Maulik Patel28659c42021-01-06 14:09:22 +00003069psa_status_t psa_asymmetric_encrypt(psa_key_id_t key,
Antonio de Angelis14276e92018-07-10 14:35:43 +01003070 psa_algorithm_t alg,
3071 const uint8_t *input,
3072 size_t input_length,
3073 const uint8_t *salt,
3074 size_t salt_length,
3075 uint8_t *output,
3076 size_t output_size,
3077 size_t *output_length);
3078
3079/**
3080 * \brief Decrypt a short message with a private key.
3081 *
Maulik Patel28659c42021-01-06 14:09:22 +00003082 * \param key Identifier of the key to use for the operation.
3083 * It must be an asymmetric key pair. It must
3084 * allow the usage #PSA_KEY_USAGE_DECRYPT.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003085 * \param alg An asymmetric encryption algorithm that is
Maulik Patel28659c42021-01-06 14:09:22 +00003086 * compatible with the type of \p key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003087 * \param[in] input The message to decrypt.
3088 * \param input_length Size of the \p input buffer in bytes.
3089 * \param[in] salt A salt or label, if supported by the
3090 * encryption algorithm.
3091 * If the algorithm does not support a
3092 * salt, pass \c NULL.
3093 * If the algorithm supports an optional
3094 * salt and you do not want to pass a salt,
3095 * pass \c NULL.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003096 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00003097 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3098 * supported.
3099 * \param salt_length Size of the \p salt buffer in bytes.
3100 * If \p salt is \c NULL, pass 0.
3101 * \param[out] output Buffer where the decrypted message is to
3102 * be written.
3103 * \param output_size Size of the \c output buffer in bytes.
3104 * \param[out] output_length On success, the number of bytes
3105 * that make up the returned output.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003106 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00003107 * \retval #PSA_SUCCESS
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003108 * \retval #PSA_ERROR_INVALID_HANDLE
3109 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis377a1552018-11-22 17:02:40 +00003110 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3111 * The size of the \p output buffer is too small. You can
Antonio de Angelis14276e92018-07-10 14:35:43 +01003112 * determine a sufficient buffer size by calling
Antonio de Angelis377a1552018-11-22 17:02:40 +00003113 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01003114 * where \c key_type and \c key_bits are the type and bit-size
Maulik Patel28659c42021-01-06 14:09:22 +00003115 * respectively of \p key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003116 * \retval #PSA_ERROR_NOT_SUPPORTED
3117 * \retval #PSA_ERROR_INVALID_ARGUMENT
3118 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3119 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3120 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003121 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3122 * \retval #PSA_ERROR_STORAGE_FAILURE
Antonio de Angelis377a1552018-11-22 17:02:40 +00003123 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3124 * \retval #PSA_ERROR_INVALID_PADDING
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003125 * \retval #PSA_ERROR_BAD_STATE
3126 * The library has not been previously initialized by psa_crypto_init().
3127 * It is implementation-dependent whether a failure to initialize
3128 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003129 */
Maulik Patel28659c42021-01-06 14:09:22 +00003130psa_status_t psa_asymmetric_decrypt(psa_key_id_t key,
Antonio de Angelis14276e92018-07-10 14:35:43 +01003131 psa_algorithm_t alg,
3132 const uint8_t *input,
3133 size_t input_length,
3134 const uint8_t *salt,
3135 size_t salt_length,
3136 uint8_t *output,
3137 size_t output_size,
3138 size_t *output_length);
3139
3140/**@}*/
3141
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003142/** \defgroup key_derivation Key derivation and pseudorandom generation
Antonio de Angelis377a1552018-11-22 17:02:40 +00003143 * @{
3144 */
3145
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003146/** The type of the state data structure for key derivation operations.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003147 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003148 * Before calling any function on a key derivation operation object, the
3149 * application must initialize it by any of the following means:
Antonio de Angelis377a1552018-11-22 17:02:40 +00003150 * - Set the structure to all-bits-zero, for example:
3151 * \code
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003152 * psa_key_derivation_operation_t operation;
3153 * memset(&operation, 0, sizeof(operation));
Antonio de Angelis377a1552018-11-22 17:02:40 +00003154 * \endcode
3155 * - Initialize the structure to logical zero values, for example:
3156 * \code
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003157 * psa_key_derivation_operation_t operation = {0};
Antonio de Angelis377a1552018-11-22 17:02:40 +00003158 * \endcode
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003159 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Antonio de Angelis377a1552018-11-22 17:02:40 +00003160 * for example:
3161 * \code
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003162 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Antonio de Angelis377a1552018-11-22 17:02:40 +00003163 * \endcode
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003164 * - Assign the result of the function psa_key_derivation_operation_init()
Antonio de Angelis377a1552018-11-22 17:02:40 +00003165 * to the structure, for example:
3166 * \code
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003167 * psa_key_derivation_operation_t operation;
3168 * operation = psa_key_derivation_operation_init();
Antonio de Angelis377a1552018-11-22 17:02:40 +00003169 * \endcode
3170 *
3171 * This is an implementation-defined \c struct. Applications should not
Summer Qin359167d2021-07-05 18:11:50 +08003172 * make any assumptions about the content of this structure.
3173 * Implementation details can change in future versions without notice.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003174 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003175typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Antonio de Angelis377a1552018-11-22 17:02:40 +00003176
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01003177/** \def PSA_KEY_DERIVATION_OPERATION_INIT
3178 *
3179 * This macro returns a suitable initializer for a key derivation operation
3180 * object of type #psa_key_derivation_operation_t.
3181 */
3182
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003183/** Return an initial value for a key derivation operation object.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003184 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003185static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Antonio de Angelis377a1552018-11-22 17:02:40 +00003186
3187/** Set up a key derivation operation.
3188 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003189 * A key derivation algorithm takes some inputs and uses them to generate
3190 * a byte stream in a deterministic way.
3191 * This byte stream can be used to produce keys and other
3192 * cryptographic material.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003193 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003194 * To derive a key:
3195 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3196 * -# Call psa_key_derivation_setup() to select the algorithm.
3197 * -# Provide the inputs for the key derivation by calling
3198 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3199 * as appropriate. Which inputs are needed, in what order, and whether
3200 * they may be keys and if so of what type depends on the algorithm.
3201 * -# Optionally set the operation's maximum capacity with
3202 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3203 * of or after providing inputs. For some algorithms, this step is mandatory
3204 * because the output depends on the maximum capacity.
3205 * -# To derive a key, call psa_key_derivation_output_key().
3206 * To derive a byte string for a different purpose, call
3207 * psa_key_derivation_output_bytes().
3208 * Successive calls to these functions use successive output bytes
3209 * calculated by the key derivation algorithm.
3210 * -# Clean up the key derivation operation object with
3211 * psa_key_derivation_abort().
Antonio de Angelis377a1552018-11-22 17:02:40 +00003212 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003213 * If this function returns an error, the key derivation operation object is
3214 * not changed.
3215 *
3216 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3217 * the operation will need to be reset by a call to psa_key_derivation_abort().
3218 *
3219 * Implementations must reject an attempt to derive a key of size 0.
3220 *
3221 * \param[in,out] operation The key derivation operation object
3222 * to set up. It must
3223 * have been initialized but not set up yet.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003224 * \param alg The key derivation algorithm to compute
3225 * (\c PSA_ALG_XXX value such that
3226 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Antonio de Angelis377a1552018-11-22 17:02:40 +00003227 *
3228 * \retval #PSA_SUCCESS
3229 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003230 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003231 * \c alg is not a key derivation algorithm.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003232 * \retval #PSA_ERROR_NOT_SUPPORTED
3233 * \c alg is not supported or is not a key derivation algorithm.
3234 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3235 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3236 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003237 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3238 * \retval #PSA_ERROR_STORAGE_FAILURE
3239 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01003240 * The operation state is not valid (it must be inactive), or
3241 * the library has not been previously initialized by psa_crypto_init().
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003242 * It is implementation-dependent whether a failure to initialize
3243 * results in this error code.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003244 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003245psa_status_t psa_key_derivation_setup(
3246 psa_key_derivation_operation_t *operation,
3247 psa_algorithm_t alg);
Antonio de Angelis377a1552018-11-22 17:02:40 +00003248
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003249/** Retrieve the current capacity of a key derivation operation.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003250 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003251 * The capacity of a key derivation is the maximum number of bytes that it can
3252 * return. When you get *N* bytes of output from a key derivation operation,
3253 * this reduces its capacity by *N*.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003254 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003255 * \param[in] operation The operation to query.
3256 * \param[out] capacity On success, the capacity of the operation.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003257 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003258 * \retval #PSA_SUCCESS
3259 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003260 * \retval #PSA_ERROR_HARDWARE_FAILURE
3261 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3262 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01003263 * The operation state is not valid (it must be active), or
3264 * the library has not been previously initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003265 * 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.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003287 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3288 * \retval #PSA_ERROR_HARDWARE_FAILURE
3289 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3290 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01003291 * The operation state is not valid (it must be active), or the
3292 * library has not been previously initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003293 * It is implementation-dependent whether a failure to initialize
3294 * results in this error code.
3295 */
3296psa_status_t psa_key_derivation_set_capacity(
3297 psa_key_derivation_operation_t *operation,
3298 size_t capacity);
3299
3300/** Use the maximum possible capacity for a key derivation operation.
3301 *
3302 * Use this value as the capacity argument when setting up a key derivation
3303 * to indicate that the operation should have the maximum possible capacity.
3304 * The value of the maximum possible capacity depends on the key derivation
3305 * algorithm.
3306 */
3307#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3308
3309/** Provide an input for key derivation or key agreement.
3310 *
3311 * Which inputs are required and in what order depends on the algorithm.
3312 * Refer to the documentation of each key derivation or key agreement
3313 * algorithm for information.
3314 *
3315 * This function passes direct inputs, which is usually correct for
3316 * non-secret inputs. To pass a secret input, which should be in a key
3317 * object, call psa_key_derivation_input_key() instead of this function.
3318 * Refer to the documentation of individual step types
3319 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3320 * for more information.
3321 *
3322 * If this function returns an error status, the operation enters an error
3323 * state and must be aborted by calling psa_key_derivation_abort().
3324 *
3325 * \param[in,out] operation The key derivation operation object to use.
3326 * It must have been set up with
3327 * psa_key_derivation_setup() and must not
3328 * have produced any output yet.
3329 * \param step Which step the input data is for.
3330 * \param[in] data Input data to use.
3331 * \param data_length Size of the \p data buffer in bytes.
3332 *
3333 * \retval #PSA_SUCCESS
3334 * Success.
3335 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01003336 * \c step is not compatible with the operation's algorithm, or
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003337 * \c step does not allow direct inputs.
3338 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3339 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3340 * \retval #PSA_ERROR_HARDWARE_FAILURE
3341 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3342 * \retval #PSA_ERROR_STORAGE_FAILURE
3343 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01003344 * The operation state is not valid for this input \p step, or
3345 * the library has not been previously initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003346 * It is implementation-dependent whether a failure to initialize
3347 * results in this error code.
3348 */
3349psa_status_t psa_key_derivation_input_bytes(
3350 psa_key_derivation_operation_t *operation,
3351 psa_key_derivation_step_t step,
3352 const uint8_t *data,
3353 size_t data_length);
3354
Summer Qin359167d2021-07-05 18:11:50 +08003355/** Provide a numeric input for key derivation or key agreement.
3356 *
3357 * Which inputs are required and in what order depends on the algorithm.
3358 * However, when an algorithm requires a particular order, numeric inputs
3359 * usually come first as they tend to be configuration parameters.
3360 * Refer to the documentation of each key derivation or key agreement
3361 * algorithm for information.
3362 *
3363 * This function is used for inputs which are fixed-size non-negative
3364 * integers.
3365 *
3366 * If this function returns an error status, the operation enters an error
3367 * state and must be aborted by calling psa_key_derivation_abort().
3368 *
3369 * \param[in,out] operation The key derivation operation object to use.
3370 * It must have been set up with
3371 * psa_key_derivation_setup() and must not
3372 * have produced any output yet.
3373 * \param step Which step the input data is for.
3374 * \param[in] value The value of the numeric input.
3375 *
3376 * \retval #PSA_SUCCESS
3377 * Success.
3378 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01003379 * \c step is not compatible with the operation's algorithm, or
Summer Qin359167d2021-07-05 18:11:50 +08003380 * \c step does not allow numeric inputs.
3381 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3382 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3383 * \retval #PSA_ERROR_HARDWARE_FAILURE
3384 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3385 * \retval #PSA_ERROR_STORAGE_FAILURE
3386 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01003387 * The operation state is not valid for this input \p step, or
3388 * the library has not been previously initialized by psa_crypto_init().
Summer Qin359167d2021-07-05 18:11:50 +08003389 * It is implementation-dependent whether a failure to initialize
3390 * results in this error code.
3391 */
3392psa_status_t psa_key_derivation_input_integer(
3393 psa_key_derivation_operation_t *operation,
3394 psa_key_derivation_step_t step,
3395 uint64_t value);
3396
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003397/** Provide an input for key derivation in the form of a key.
3398 *
3399 * Which inputs are required and in what order depends on the algorithm.
3400 * Refer to the documentation of each key derivation or key agreement
3401 * algorithm for information.
3402 *
3403 * This function obtains input from a key object, which is usually correct for
3404 * secret inputs or for non-secret personalization strings kept in the key
3405 * store. To pass a non-secret parameter which is not in the key store,
3406 * call psa_key_derivation_input_bytes() instead of this function.
3407 * Refer to the documentation of individual step types
3408 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3409 * for more information.
3410 *
3411 * If this function returns an error status, the operation enters an error
3412 * state and must be aborted by calling psa_key_derivation_abort().
3413 *
3414 * \param[in,out] operation The key derivation operation object to use.
3415 * It must have been set up with
3416 * psa_key_derivation_setup() and must not
3417 * have produced any output yet.
3418 * \param step Which step the input data is for.
Maulik Patel28659c42021-01-06 14:09:22 +00003419 * \param key Identifier of the key. It must have an
3420 * appropriate type for step and must allow the
Summer Qin359167d2021-07-05 18:11:50 +08003421 * usage #PSA_KEY_USAGE_DERIVE or
3422 * #PSA_KEY_USAGE_VERIFY_DERIVATION (see note)
3423 * and the algorithm used by the operation.
3424 *
3425 * \note Once all inputs steps are completed, the operations will allow:
3426 * - psa_key_derivation_output_bytes() if each input was either a direct input
3427 * or a key with #PSA_KEY_USAGE_DERIVE set;
3428 * - psa_key_derivation_output_key() if the input for step
3429 * #PSA_KEY_DERIVATION_INPUT_SECRET or #PSA_KEY_DERIVATION_INPUT_PASSWORD
3430 * was from a key slot with #PSA_KEY_USAGE_DERIVE and each other input was
3431 * either a direct input or a key with #PSA_KEY_USAGE_DERIVE set;
3432 * - psa_key_derivation_verify_bytes() if each input was either a direct input
3433 * or a key with #PSA_KEY_USAGE_VERIFY_DERIVATION set;
3434 * - psa_key_derivation_verify_key() under the same conditions as
3435 * psa_key_derivation_verify_bytes().
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003436 *
3437 * \retval #PSA_SUCCESS
3438 * Success.
3439 * \retval #PSA_ERROR_INVALID_HANDLE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003440 * \retval #PSA_ERROR_NOT_PERMITTED
Summer Qin359167d2021-07-05 18:11:50 +08003441 * The key allows neither #PSA_KEY_USAGE_DERIVE nor
3442 * #PSA_KEY_USAGE_VERIFY_DERIVATION, or it doesn't allow this
3443 * algorithm.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003444 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01003445 * \c step is not compatible with the operation's algorithm, or
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003446 * \c step does not allow key inputs of the given type
3447 * or does not allow key inputs at all.
3448 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3449 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3450 * \retval #PSA_ERROR_HARDWARE_FAILURE
3451 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3452 * \retval #PSA_ERROR_STORAGE_FAILURE
3453 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01003454 * The operation state is not valid for this input \p step, or
3455 * the library has not been previously initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003456 * It is implementation-dependent whether a failure to initialize
3457 * results in this error code.
3458 */
3459psa_status_t psa_key_derivation_input_key(
3460 psa_key_derivation_operation_t *operation,
3461 psa_key_derivation_step_t step,
Maulik Patel28659c42021-01-06 14:09:22 +00003462 psa_key_id_t key);
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003463
3464/** Perform a key agreement and use the shared secret as input to a key
3465 * derivation.
3466 *
3467 * A key agreement algorithm takes two inputs: a private key \p private_key
3468 * a public key \p peer_key.
3469 * The result of this function is passed as input to a key derivation.
3470 * The output of this key derivation can be extracted by reading from the
3471 * resulting operation to produce keys and other cryptographic material.
3472 *
3473 * If this function returns an error status, the operation enters an error
3474 * state and must be aborted by calling psa_key_derivation_abort().
3475 *
3476 * \param[in,out] operation The key derivation operation object to use.
3477 * It must have been set up with
3478 * psa_key_derivation_setup() with a
3479 * key agreement and derivation algorithm
3480 * \c alg (\c PSA_ALG_XXX value such that
3481 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3482 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3483 * is false).
3484 * The operation must be ready for an
3485 * input of the type given by \p step.
3486 * \param step Which step the input data is for.
Maulik Patel28659c42021-01-06 14:09:22 +00003487 * \param private_key Identifier of the private key to use. It must
3488 * allow the usage #PSA_KEY_USAGE_DERIVE.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003489 * \param[in] peer_key Public key of the peer. The peer key must be in the
3490 * same format that psa_import_key() accepts for the
3491 * public key type corresponding to the type of
3492 * private_key. That is, this function performs the
3493 * equivalent of
3494 * #psa_import_key(...,
3495 * `peer_key`, `peer_key_length`) where
3496 * with key attributes indicating the public key
3497 * type corresponding to the type of `private_key`.
3498 * For example, for EC keys, this means that peer_key
3499 * is interpreted as a point on the curve that the
3500 * private key is on. The standard formats for public
3501 * keys are documented in the documentation of
3502 * psa_export_public_key().
3503 * \param peer_key_length Size of \p peer_key in bytes.
3504 *
3505 * \retval #PSA_SUCCESS
3506 * Success.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003507 * \retval #PSA_ERROR_INVALID_HANDLE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003508 * \retval #PSA_ERROR_NOT_PERMITTED
3509 * \retval #PSA_ERROR_INVALID_ARGUMENT
3510 * \c private_key is not compatible with \c alg,
3511 * or \p peer_key is not valid for \c alg or not compatible with
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01003512 * \c private_key, or \c step does not allow an input resulting
3513 * from a key agreement.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003514 * \retval #PSA_ERROR_NOT_SUPPORTED
3515 * \c alg is not supported or is not a key derivation algorithm.
3516 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3517 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3518 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003519 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3520 * \retval #PSA_ERROR_STORAGE_FAILURE
3521 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01003522 * The operation state is not valid for this key agreement \p step,
3523 * or the library has not been previously initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003524 * It is implementation-dependent whether a failure to initialize
3525 * results in this error code.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003526 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003527psa_status_t psa_key_derivation_key_agreement(
3528 psa_key_derivation_operation_t *operation,
3529 psa_key_derivation_step_t step,
Maulik Patel28659c42021-01-06 14:09:22 +00003530 psa_key_id_t private_key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003531 const uint8_t *peer_key,
3532 size_t peer_key_length);
3533
3534/** Read some data from a key derivation operation.
3535 *
3536 * This function calculates output bytes from a key derivation algorithm and
3537 * return those bytes.
3538 * If you view the key derivation's output as a stream of bytes, this
3539 * function destructively reads the requested number of bytes from the
3540 * stream.
3541 * The operation's capacity decreases by the number of bytes read.
3542 *
3543 * If this function returns an error status other than
3544 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
3545 * state and must be aborted by calling psa_key_derivation_abort().
3546 *
3547 * \param[in,out] operation The key derivation operation object to read from.
3548 * \param[out] output Buffer where the output will be written.
3549 * \param output_length Number of bytes to output.
3550 *
3551 * \retval #PSA_SUCCESS
Summer Qin359167d2021-07-05 18:11:50 +08003552 * \retval #PSA_ERROR_NOT_PERMITTED
3553 * One of the inputs was a key whose policy didn't allow
3554 * #PSA_KEY_USAGE_DERIVE.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003555 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3556 * The operation's capacity was less than
3557 * \p output_length bytes. Note that in this case,
3558 * no output is written to the output buffer.
3559 * The operation's capacity is set to 0, thus
3560 * subsequent calls to this function will not
3561 * succeed, even with a smaller output buffer.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003562 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3563 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3564 * \retval #PSA_ERROR_HARDWARE_FAILURE
3565 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3566 * \retval #PSA_ERROR_STORAGE_FAILURE
3567 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01003568 * The operation state is not valid (it must be active and completed
3569 * all required input steps), or the library has not been previously
3570 * initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003571 * It is implementation-dependent whether a failure to initialize
3572 * results in this error code.
3573 */
3574psa_status_t psa_key_derivation_output_bytes(
3575 psa_key_derivation_operation_t *operation,
3576 uint8_t *output,
3577 size_t output_length);
3578
3579/** Derive a key from an ongoing key derivation operation.
3580 *
3581 * This function calculates output bytes from a key derivation algorithm
3582 * and uses those bytes to generate a key deterministically.
3583 * The key's location, usage policy, type and size are taken from
3584 * \p attributes.
3585 *
3586 * If you view the key derivation's output as a stream of bytes, this
3587 * function destructively reads as many bytes as required from the
3588 * stream.
3589 * The operation's capacity decreases by the number of bytes read.
3590 *
3591 * If this function returns an error status other than
3592 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
3593 * state and must be aborted by calling psa_key_derivation_abort().
3594 *
3595 * How much output is produced and consumed from the operation, and how
Summer Qin359167d2021-07-05 18:11:50 +08003596 * the key is derived, depends on the key type and on the key size
3597 * (denoted \c bits below):
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003598 *
3599 * - For key types for which the key is an arbitrary sequence of bytes
3600 * of a given size, this function is functionally equivalent to
3601 * calling #psa_key_derivation_output_bytes
3602 * and passing the resulting output to #psa_import_key.
3603 * However, this function has a security benefit:
3604 * if the implementation provides an isolation boundary then
3605 * the key material is not exposed outside the isolation boundary.
3606 * As a consequence, for these key types, this function always consumes
Summer Qin359167d2021-07-05 18:11:50 +08003607 * exactly (\c bits / 8) bytes from the operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003608 * The following key types defined in this specification follow this scheme:
3609 *
3610 * - #PSA_KEY_TYPE_AES;
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01003611 * - #PSA_KEY_TYPE_ARIA;
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003612 * - #PSA_KEY_TYPE_CAMELLIA;
3613 * - #PSA_KEY_TYPE_DERIVE;
Summer Qin359167d2021-07-05 18:11:50 +08003614 * - #PSA_KEY_TYPE_HMAC;
3615 * - #PSA_KEY_TYPE_PASSWORD_HASH.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003616 *
3617 * - For ECC keys on a Montgomery elliptic curve
3618 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
3619 * Montgomery curve), this function always draws a byte string whose
3620 * length is determined by the curve, and sets the mandatory bits
3621 * accordingly. That is:
3622 *
Summer Qin0e5b2e02020-10-22 11:23:39 +08003623 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Soby Mathew07ef6e42020-07-20 21:09:23 +01003624 * string and process it as specified in RFC 7748 &sect;5.
Summer Qin0e5b2e02020-10-22 11:23:39 +08003625 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Soby Mathew07ef6e42020-07-20 21:09:23 +01003626 * string and process it as specified in RFC 7748 &sect;5.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003627 *
3628 * - For key types for which the key is represented by a single sequence of
Summer Qin359167d2021-07-05 18:11:50 +08003629 * \c bits bits with constraints as to which bit sequences are acceptable,
3630 * this function draws a byte string of length (\c bits / 8) bytes rounded
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003631 * up to the nearest whole number of bytes. If the resulting byte string
3632 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3633 * This process is repeated until an acceptable byte string is drawn.
3634 * The byte string drawn from the operation is interpreted as specified
3635 * for the output produced by psa_export_key().
3636 * The following key types defined in this specification follow this scheme:
3637 *
3638 * - #PSA_KEY_TYPE_DES.
3639 * Force-set the parity bits, but discard forbidden weak keys.
3640 * For 2-key and 3-key triple-DES, the three keys are generated
3641 * successively (for example, for 3-key triple-DES,
3642 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3643 * discard the first 8 bytes, use the next 8 bytes as the first key,
3644 * and continue reading output from the operation to derive the other
3645 * two keys).
3646 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
3647 * where \c group designates any Diffie-Hellman group) and
3648 * ECC keys on a Weierstrass elliptic curve
3649 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
3650 * Weierstrass curve).
3651 * For these key types, interpret the byte string as integer
3652 * in big-endian order. Discard it if it is not in the range
3653 * [0, *N* - 2] where *N* is the boundary of the private key domain
3654 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
3655 * or the order of the curve's base point for ECC).
3656 * Add 1 to the resulting integer and use this as the private key *x*.
3657 * This method allows compliance to NIST standards, specifically
3658 * the methods titled "key-pair generation by testing candidates"
3659 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3660 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3661 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3662 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
3663 *
3664 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
3665 * the way in which the operation output is consumed is
3666 * implementation-defined.
3667 *
3668 * In all cases, the data that is read is discarded from the operation.
3669 * The operation's capacity is decreased by the number of bytes read.
3670 *
3671 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3672 * the input to that step must be provided with psa_key_derivation_input_key().
3673 * Future versions of this specification may include additional restrictions
3674 * on the derived key based on the attributes and strength of the secret key.
3675 *
3676 * \param[in] attributes The attributes for the new key.
Summer Qin359167d2021-07-05 18:11:50 +08003677 * If the key type to be created is
3678 * #PSA_KEY_TYPE_PASSWORD_HASH then the algorithm in
3679 * the policy must be the same as in the current
3680 * operation.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003681 * \param[in,out] operation The key derivation operation object to read from.
Maulik Patel28659c42021-01-06 14:09:22 +00003682 * \param[out] key On success, an identifier for the newly created
3683 * key. For persistent keys, this is the key
3684 * identifier defined in \p attributes.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003685 * \c 0 on failure.
3686 *
3687 * \retval #PSA_SUCCESS
3688 * Success.
3689 * If the key is persistent, the key material and the key's metadata
3690 * have been saved to persistent storage.
3691 * \retval #PSA_ERROR_ALREADY_EXISTS
3692 * This is an attempt to create a persistent key, and there is
3693 * already a persistent key with the given identifier.
3694 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3695 * There was not enough data to create the desired key.
3696 * Note that in this case, no output is written to the output buffer.
3697 * The operation's capacity is set to 0, thus subsequent calls to
3698 * this function will not succeed, even with a smaller output buffer.
3699 * \retval #PSA_ERROR_NOT_SUPPORTED
3700 * The key type or key size is not supported, either by the
3701 * implementation in general or in this particular location.
3702 * \retval #PSA_ERROR_INVALID_ARGUMENT
3703 * The provided key attributes are not valid for the operation.
3704 * \retval #PSA_ERROR_NOT_PERMITTED
Summer Qin359167d2021-07-05 18:11:50 +08003705 * The #PSA_KEY_DERIVATION_INPUT_SECRET or
3706 * #PSA_KEY_DERIVATION_INPUT_PASSWORD input was not provided through a
3707 * key; or one of the inputs was a key whose policy didn't allow
3708 * #PSA_KEY_USAGE_DERIVE.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003709 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3710 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3711 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3712 * \retval #PSA_ERROR_HARDWARE_FAILURE
3713 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Maulik Patel13b27cf2021-05-14 11:44:53 +01003714 * \retval #PSA_ERROR_DATA_INVALID
3715 * \retval #PSA_ERROR_DATA_CORRUPT
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003716 * \retval #PSA_ERROR_STORAGE_FAILURE
3717 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01003718 * The operation state is not valid (it must be active and completed
3719 * all required input steps), or the library has not been previously
3720 * initialized by psa_crypto_init().
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003721 * It is implementation-dependent whether a failure to initialize
3722 * results in this error code.
3723 */
3724psa_status_t psa_key_derivation_output_key(
3725 const psa_key_attributes_t *attributes,
3726 psa_key_derivation_operation_t *operation,
Maulik Patel28659c42021-01-06 14:09:22 +00003727 psa_key_id_t *key);
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003728
Summer Qin359167d2021-07-05 18:11:50 +08003729/** Compare output data from a key derivation operation to an expected value.
3730 *
3731 * This function calculates output bytes from a key derivation algorithm and
3732 * compares those bytes to an expected value in constant time.
3733 * If you view the key derivation's output as a stream of bytes, this
3734 * function destructively reads the expected number of bytes from the
3735 * stream before comparing them.
3736 * The operation's capacity decreases by the number of bytes read.
3737 *
3738 * This is functionally equivalent to the following code:
3739 * \code
3740 * psa_key_derivation_output_bytes(operation, tmp, output_length);
3741 * if (memcmp(output, tmp, output_length) != 0)
3742 * return PSA_ERROR_INVALID_SIGNATURE;
3743 * \endcode
3744 * except (1) it works even if the key's policy does not allow outputting the
3745 * bytes, and (2) the comparison will be done in constant time.
3746 *
3747 * If this function returns an error status other than
3748 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
3749 * the operation enters an error state and must be aborted by calling
3750 * psa_key_derivation_abort().
3751 *
3752 * \param[in,out] operation The key derivation operation object to read from.
3753 * \param[in] expected_output Buffer containing the expected derivation output.
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01003754 * \param output_length Length of the expected output; this is also the
Summer Qin359167d2021-07-05 18:11:50 +08003755 * number of bytes that will be read.
3756 *
3757 * \retval #PSA_SUCCESS
3758 * \retval #PSA_ERROR_INVALID_SIGNATURE
3759 * The output was read successfully, but it differs from the expected
3760 * output.
3761 * \retval #PSA_ERROR_NOT_PERMITTED
3762 * One of the inputs was a key whose policy didn't allow
3763 * #PSA_KEY_USAGE_VERIFY_DERIVATION.
3764 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3765 * The operation's capacity was less than
3766 * \p output_length bytes. Note that in this case,
3767 * the operation's capacity is set to 0, thus
3768 * subsequent calls to this function will not
3769 * succeed, even with a smaller expected output.
Summer Qin359167d2021-07-05 18:11:50 +08003770 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3771 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3772 * \retval #PSA_ERROR_HARDWARE_FAILURE
3773 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3774 * \retval #PSA_ERROR_STORAGE_FAILURE
3775 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01003776 * The operation state is not valid (it must be active and completed
3777 * all required input steps), or the library has not been previously
3778 * initialized by psa_crypto_init().
Summer Qin359167d2021-07-05 18:11:50 +08003779 * It is implementation-dependent whether a failure to initialize
3780 * results in this error code.
3781 */
3782psa_status_t psa_key_derivation_verify_bytes(
3783 psa_key_derivation_operation_t *operation,
3784 const uint8_t *expected_output,
3785 size_t output_length);
3786
3787/** Compare output data from a key derivation operation to an expected value
3788 * stored in a key object.
3789 *
3790 * This function calculates output bytes from a key derivation algorithm and
3791 * compares those bytes to an expected value, provided as key of type
3792 * #PSA_KEY_TYPE_PASSWORD_HASH.
3793 * If you view the key derivation's output as a stream of bytes, this
Summer Qin614002c2023-01-19 15:22:39 +08003794 * function destructively reads the number of bytes corresponding to the
Summer Qin359167d2021-07-05 18:11:50 +08003795 * length of the expected value from the stream before comparing them.
3796 * The operation's capacity decreases by the number of bytes read.
3797 *
3798 * This is functionally equivalent to exporting the key and calling
3799 * psa_key_derivation_verify_bytes() on the result, except that it
3800 * works even if the key cannot be exported.
3801 *
3802 * If this function returns an error status other than
3803 * #PSA_ERROR_INSUFFICIENT_DATA or #PSA_ERROR_INVALID_SIGNATURE,
3804 * the operation enters an error state and must be aborted by calling
3805 * psa_key_derivation_abort().
3806 *
3807 * \param[in,out] operation The key derivation operation object to read from.
3808 * \param[in] expected A key of type #PSA_KEY_TYPE_PASSWORD_HASH
3809 * containing the expected output. Its policy must
3810 * include the #PSA_KEY_USAGE_VERIFY_DERIVATION flag
3811 * and the permitted algorithm must match the
3812 * operation. The value of this key was likely
3813 * computed by a previous call to
3814 * psa_key_derivation_output_key().
3815 *
3816 * \retval #PSA_SUCCESS
3817 * \retval #PSA_ERROR_INVALID_SIGNATURE
3818 * The output was read successfully, but if differs from the expected
3819 * output.
3820 * \retval #PSA_ERROR_INVALID_HANDLE
3821 * The key passed as the expected value does not exist.
3822 * \retval #PSA_ERROR_INVALID_ARGUMENT
3823 * The key passed as the expected value has an invalid type.
3824 * \retval #PSA_ERROR_NOT_PERMITTED
3825 * The key passed as the expected value does not allow this usage or
3826 * this algorithm; or one of the inputs was a key whose policy didn't
3827 * allow #PSA_KEY_USAGE_VERIFY_DERIVATION.
3828 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3829 * The operation's capacity was less than
3830 * the length of the expected value. In this case,
3831 * the operation's capacity is set to 0, thus
3832 * subsequent calls to this function will not
3833 * succeed, even with a smaller expected output.
Summer Qin359167d2021-07-05 18:11:50 +08003834 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3835 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3836 * \retval #PSA_ERROR_HARDWARE_FAILURE
3837 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3838 * \retval #PSA_ERROR_STORAGE_FAILURE
3839 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01003840 * The operation state is not valid (it must be active and completed
3841 * all required input steps), or the library has not been previously
3842 * initialized by psa_crypto_init().
Summer Qin359167d2021-07-05 18:11:50 +08003843 * It is implementation-dependent whether a failure to initialize
3844 * results in this error code.
3845 */
3846psa_status_t psa_key_derivation_verify_key(
3847 psa_key_derivation_operation_t *operation,
3848 psa_key_id_t expected);
3849
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003850/** Abort a key derivation operation.
3851 *
3852 * Aborting an operation frees all associated resources except for the \c
3853 * operation structure itself. Once aborted, the operation object can be reused
3854 * for another operation by calling psa_key_derivation_setup() again.
3855 *
3856 * This function may be called at any time after the operation
3857 * object has been initialized as described in #psa_key_derivation_operation_t.
3858 *
3859 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3860 * call psa_key_derivation_abort() on an operation that has not been set up.
3861 *
3862 * \param[in,out] operation The operation to abort.
3863 *
3864 * \retval #PSA_SUCCESS
3865 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3866 * \retval #PSA_ERROR_HARDWARE_FAILURE
3867 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3868 * \retval #PSA_ERROR_BAD_STATE
3869 * The library has not been previously initialized by psa_crypto_init().
3870 * It is implementation-dependent whether a failure to initialize
3871 * results in this error code.
3872 */
3873psa_status_t psa_key_derivation_abort(
3874 psa_key_derivation_operation_t *operation);
3875
3876/** Perform a key agreement and return the raw shared secret.
3877 *
3878 * \warning The raw result of a key agreement algorithm such as finite-field
3879 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3880 * not be used directly as key material. It should instead be passed as
3881 * input to a key derivation algorithm. To chain a key agreement with
3882 * a key derivation, use psa_key_derivation_key_agreement() and other
3883 * functions from the key derivation interface.
3884 *
3885 * \param alg The key agreement algorithm to compute
3886 * (\c PSA_ALG_XXX value such that
3887 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3888 * is true).
Maulik Patel28659c42021-01-06 14:09:22 +00003889 * \param private_key Identifier of the private key to use. It must
3890 * allow the usage #PSA_KEY_USAGE_DERIVE.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003891 * \param[in] peer_key Public key of the peer. It must be
3892 * in the same format that psa_import_key()
3893 * accepts. The standard formats for public
3894 * keys are documented in the documentation
3895 * of psa_export_public_key().
3896 * \param peer_key_length Size of \p peer_key in bytes.
3897 * \param[out] output Buffer where the decrypted message is to
3898 * be written.
3899 * \param output_size Size of the \c output buffer in bytes.
3900 * \param[out] output_length On success, the number of bytes
3901 * that make up the returned output.
3902 *
3903 * \retval #PSA_SUCCESS
3904 * Success.
3905 * \retval #PSA_ERROR_INVALID_HANDLE
3906 * \retval #PSA_ERROR_NOT_PERMITTED
3907 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01003908 * \p alg is not a key agreement algorithm, or
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003909 * \p private_key is not compatible with \p alg,
3910 * or \p peer_key is not valid for \p alg or not compatible with
3911 * \p private_key.
3912 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3913 * \p output_size is too small
3914 * \retval #PSA_ERROR_NOT_SUPPORTED
3915 * \p alg is not a supported key agreement algorithm.
3916 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3917 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3918 * \retval #PSA_ERROR_HARDWARE_FAILURE
3919 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3920 * \retval #PSA_ERROR_STORAGE_FAILURE
3921 * \retval #PSA_ERROR_BAD_STATE
3922 * The library has not been previously initialized by psa_crypto_init().
3923 * It is implementation-dependent whether a failure to initialize
3924 * results in this error code.
3925 */
3926psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Maulik Patel28659c42021-01-06 14:09:22 +00003927 psa_key_id_t private_key,
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003928 const uint8_t *peer_key,
3929 size_t peer_key_length,
3930 uint8_t *output,
3931 size_t output_size,
3932 size_t *output_length);
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003933
Antonio de Angelis377a1552018-11-22 17:02:40 +00003934/**@}*/
3935
3936/** \defgroup random Random generation
Antonio de Angelis14276e92018-07-10 14:35:43 +01003937 * @{
3938 */
3939
3940/**
3941 * \brief Generate random bytes.
3942 *
3943 * \warning This function **can** fail! Callers MUST check the return status
3944 * and MUST NOT use the content of the output buffer if the return
3945 * status is not #PSA_SUCCESS.
3946 *
3947 * \note To generate a key, use psa_generate_key() instead.
3948 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00003949 * \param[out] output Output buffer for the generated data.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003950 * \param output_size Number of bytes to generate and output.
3951 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00003952 * \retval #PSA_SUCCESS
3953 * \retval #PSA_ERROR_NOT_SUPPORTED
3954 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003955 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Antonio de Angelis377a1552018-11-22 17:02:40 +00003956 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3957 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003958 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003959 * \retval #PSA_ERROR_BAD_STATE
3960 * The library has not been previously initialized by psa_crypto_init().
3961 * It is implementation-dependent whether a failure to initialize
3962 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003963 */
3964psa_status_t psa_generate_random(uint8_t *output,
3965 size_t output_size);
3966
3967/**
3968 * \brief Generate a key or key pair.
3969 *
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003970 * The key is generated randomly.
3971 * Its location, usage policy, type and size are taken from \p attributes.
3972 *
3973 * Implementations must reject an attempt to generate a key of size 0.
3974 *
3975 * The following type-specific considerations apply:
3976 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
3977 * the public exponent is 65537.
3978 * The modulus is a product of two probabilistic primes
3979 * between 2^{n-1} and 2^n where n is the bit size specified in the
3980 * attributes.
3981 *
3982 * \param[in] attributes The attributes for the new key.
Maulik Patel28659c42021-01-06 14:09:22 +00003983 * \param[out] key On success, an identifier for the newly created
3984 * key. For persistent keys, this is the key
3985 * identifier defined in \p attributes.
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003986 * \c 0 on failure.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003987 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00003988 * \retval #PSA_SUCCESS
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003989 * Success.
3990 * If the key is persistent, the key material and the key's metadata
3991 * have been saved to persistent storage.
Jamie Fox0e54ebc2019-04-09 14:21:04 +01003992 * \retval #PSA_ERROR_ALREADY_EXISTS
Antonio de Angelis04debbd2019-10-14 12:12:52 +01003993 * This is an attempt to create a persistent key, and there is
3994 * already a persistent key with the given identifier.
Antonio de Angelis377a1552018-11-22 17:02:40 +00003995 * \retval #PSA_ERROR_NOT_SUPPORTED
3996 * \retval #PSA_ERROR_INVALID_ARGUMENT
3997 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3998 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3999 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
4000 * \retval #PSA_ERROR_HARDWARE_FAILURE
Antonio de Angelis04debbd2019-10-14 12:12:52 +01004001 * \retval #PSA_ERROR_CORRUPTION_DETECTED
4002 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Maulik Patel13b27cf2021-05-14 11:44:53 +01004003 * \retval #PSA_ERROR_DATA_INVALID
4004 * \retval #PSA_ERROR_DATA_CORRUPT
Antonio de Angelis04debbd2019-10-14 12:12:52 +01004005 * \retval #PSA_ERROR_STORAGE_FAILURE
Jamie Fox0e54ebc2019-04-09 14:21:04 +01004006 * \retval #PSA_ERROR_BAD_STATE
4007 * The library has not been previously initialized by psa_crypto_init().
4008 * It is implementation-dependent whether a failure to initialize
4009 * results in this error code.
Antonio de Angelis14276e92018-07-10 14:35:43 +01004010 */
Antonio de Angelis04debbd2019-10-14 12:12:52 +01004011psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Maulik Patel28659c42021-01-06 14:09:22 +00004012 psa_key_id_t *key);
Antonio de Angelis14276e92018-07-10 14:35:43 +01004013
4014/**@}*/
4015
4016#ifdef __cplusplus
4017}
4018#endif
4019
Antonio de Angelis377a1552018-11-22 17:02:40 +00004020/* The file "crypto_sizes.h" contains definitions for size calculation
4021 * macros whose definitions are implementation-specific. */
Jamie Foxcc31d402019-01-28 17:13:52 +00004022#include "psa/crypto_sizes.h"
Antonio de Angelis377a1552018-11-22 17:02:40 +00004023
Soby Mathewd7b79f22020-05-21 15:06:54 +01004024/* The file "crypto_client_struct.h" contains definitions for structures
4025 * whose definitions differ in the client view and the PSA server
4026 * implementation in TF-M. */
4027#include "psa/crypto_client_struct.h"
4028
4029
Antonio de Angelis377a1552018-11-22 17:02:40 +00004030/* The file "crypto_struct.h" contains definitions for
4031 * implementation-specific structs that are declared above. */
Jamie Foxcc31d402019-01-28 17:13:52 +00004032#include "psa/crypto_struct.h"
Antonio de Angelis377a1552018-11-22 17:02:40 +00004033
4034/* The file "crypto_extra.h" contains vendor-specific definitions. This
4035 * can include vendor-defined algorithms, extra functions, etc. */
Jamie Foxcc31d402019-01-28 17:13:52 +00004036#include "psa/crypto_extra.h"
Antonio de Angelis8908f472018-08-31 15:44:25 +01004037
Antonio de Angelis14276e92018-07-10 14:35:43 +01004038#endif /* PSA_CRYPTO_H */