blob: cd5b374df60cc368788b0b95b24816e9e24414d1 [file] [log] [blame]
Julian Halla7e76c82021-04-14 11:12:11 +01001/*
Gabor Toth99e4ec52023-07-18 08:24:20 +02002 * Copyright (c) 2018-2023, Arm Limited. All rights reserved.
Julian Halla7e76c82021-04-14 11:12:11 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7/**
8 * \file psa/crypto.h
9 * \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 __cplusplus
18extern "C" {
19#endif
20
Gabor Toth99e4ec52023-07-18 08:24:20 +020021#ifdef EXPORT_PUBLIC_INTERFACE_PSA_CRYPTO
22#define PSA_CRYPTO_EXPORTED __attribute__((__visibility__("default")))
23#else
24#define PSA_CRYPTO_EXPORTED
25#endif
26
Julian Halla7e76c82021-04-14 11:12:11 +010027/* The file "crypto_types.h" declares types that encode errors,
28 * algorithms, key types, policies, etc. */
29#include "psa/crypto_types.h"
30
31/** \defgroup version API version
32 * @{
33 */
34
35/**
36 * The major version of this implementation of the PSA Crypto API
37 */
38#define PSA_CRYPTO_API_VERSION_MAJOR 1
39
40/**
41 * The minor version of this implementation of the PSA Crypto API
42 */
43#define PSA_CRYPTO_API_VERSION_MINOR 0
44
45/**@}*/
46
47/* The file "crypto_values.h" declares macros to build and analyze values
48 * of integral types defined in "crypto_types.h". */
49#include "psa/crypto_values.h"
50
51/** \defgroup initialization Library initialization
52 * @{
53 */
54
55/**
56 * \brief Library initialization.
57 *
58 * Applications must call this function before calling any other
59 * function in this module.
60 *
61 * Applications may call this function more than once. Once a call
62 * succeeds, subsequent calls are guaranteed to succeed.
63 *
64 * If the application calls other functions before calling psa_crypto_init(),
65 * the behavior is undefined. Implementations are encouraged to either perform
66 * the operation as if the library had been initialized or to return
67 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
68 * implementations should not return a success status if the lack of
69 * initialization may have security implications, for example due to improper
70 * seeding of the random number generator.
71 *
72 * \retval #PSA_SUCCESS
73 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
74 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
75 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
76 * \retval #PSA_ERROR_HARDWARE_FAILURE
77 * \retval #PSA_ERROR_CORRUPTION_DETECTED
78 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
79 * \retval #PSA_ERROR_STORAGE_FAILURE
80 * \retval #PSA_ERROR_DATA_INVALID
81 * \retval #PSA_ERROR_DATA_CORRUPT
82 */
Gabor Toth99e4ec52023-07-18 08:24:20 +020083PSA_CRYPTO_EXPORTED psa_status_t psa_crypto_init(void);
Julian Halla7e76c82021-04-14 11:12:11 +010084
85/**@}*/
86
87/** \addtogroup attributes
88 * @{
89 */
90
91/** \def PSA_KEY_ATTRIBUTES_INIT
92 *
93 * This macro returns a suitable initializer for a key attribute structure
94 * of type #psa_key_attributes_t.
95 */
96#ifdef __DOXYGEN_ONLY__
97/* This is an example definition for documentation purposes.
98 * Implementations should define a suitable value in `crypto_struct.h`.
99 */
100#define PSA_KEY_ATTRIBUTES_INIT {0}
101#endif
102
103/** Return an initial value for a key attributes structure.
104 */
105static psa_key_attributes_t psa_key_attributes_init(void);
106
107/** Declare a key as persistent and set its key identifier.
108 *
109 * If the attribute structure currently declares the key as volatile (which
110 * is the default content of an attribute structure), this function sets
111 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
112 *
113 * This function does not access storage, it merely stores the given
114 * value in the structure.
115 * The persistent key will be written to storage when the attribute
116 * structure is passed to a key creation function such as
117 * psa_import_key(), psa_generate_key(),
118 * psa_key_derivation_output_key() or psa_copy_key().
119 *
120 * This function may be declared as `static` (i.e. without external
121 * linkage). This function may be provided as a function-like macro,
122 * but in this case it must evaluate each of its arguments exactly once.
123 *
124 * \param[out] attributes The attribute structure to write to.
125 * \param key The persistent identifier for the key.
126 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200127static void psa_set_key_id(psa_key_attributes_t *attributes, psa_key_id_t key);
Julian Halla7e76c82021-04-14 11:12:11 +0100128
Julian Halla7e76c82021-04-14 11:12:11 +0100129/** Set the location of a persistent key.
130 *
131 * To make a key persistent, you must give it a persistent key identifier
132 * with psa_set_key_id(). By default, a key that has a persistent identifier
133 * is stored in the default storage area identifier by
134 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
135 * area, or to explicitly declare the key as volatile.
136 *
137 * This function does not access storage, it merely stores the given
138 * value in the structure.
139 * The persistent key will be written to storage when the attribute
140 * structure is passed to a key creation function such as
141 * psa_import_key(), psa_generate_key(),
142 * psa_key_derivation_output_key() or psa_copy_key().
143 *
144 * This function may be declared as `static` (i.e. without external
145 * linkage). This function may be provided as a function-like macro,
146 * but in this case it must evaluate each of its arguments exactly once.
147 *
148 * \param[out] attributes The attribute structure to write to.
149 * \param lifetime The lifetime for the key.
150 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
151 * key will be volatile, and the key identifier
152 * attribute is reset to 0.
153 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200154static void psa_set_key_lifetime(psa_key_attributes_t *attributes, psa_key_lifetime_t lifetime);
Julian Halla7e76c82021-04-14 11:12:11 +0100155
156/** Retrieve the key identifier from key attributes.
157 *
158 * This function may be declared as `static` (i.e. without external
159 * linkage). This function may be provided as a function-like macro,
160 * but in this case it must evaluate its argument exactly once.
161 *
162 * \param[in] attributes The key attribute structure to query.
163 *
164 * \return The persistent identifier stored in the attribute structure.
165 * This value is unspecified if the attribute structure declares
166 * the key as volatile.
167 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200168static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
Julian Halla7e76c82021-04-14 11:12:11 +0100169
170/** Retrieve the lifetime from key attributes.
171 *
172 * This function may be declared as `static` (i.e. without external
173 * linkage). This function may be provided as a function-like macro,
174 * but in this case it must evaluate its argument exactly once.
175 *
176 * \param[in] attributes The key attribute structure to query.
177 *
178 * \return The lifetime value stored in the attribute structure.
179 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200180static psa_key_lifetime_t psa_get_key_lifetime(const psa_key_attributes_t *attributes);
Julian Halla7e76c82021-04-14 11:12:11 +0100181
182/** Declare usage flags for a key.
183 *
184 * Usage flags are part of a key's usage policy. They encode what
185 * kind of operations are permitted on the key. For more details,
186 * refer to the documentation of the type #psa_key_usage_t.
187 *
188 * This function overwrites any usage flags
189 * previously set in \p attributes.
190 *
191 * This function may be declared as `static` (i.e. without external
192 * linkage). This function may be provided as a function-like macro,
193 * but in this case it must evaluate each of its arguments exactly once.
194 *
195 * \param[out] attributes The attribute structure to write to.
196 * \param usage_flags The usage flags to write.
197 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200198static void psa_set_key_usage_flags(psa_key_attributes_t *attributes, psa_key_usage_t usage_flags);
Julian Halla7e76c82021-04-14 11:12:11 +0100199
200/** Retrieve the usage flags from key attributes.
201 *
202 * This function may be declared as `static` (i.e. without external
203 * linkage). This function may be provided as a function-like macro,
204 * but in this case it must evaluate its argument exactly once.
205 *
206 * \param[in] attributes The key attribute structure to query.
207 *
208 * \return The usage flags stored in the attribute structure.
209 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200210static psa_key_usage_t psa_get_key_usage_flags(const psa_key_attributes_t *attributes);
Julian Halla7e76c82021-04-14 11:12:11 +0100211
212/** Declare the permitted algorithm policy for a key.
213 *
214 * The permitted algorithm policy of a key encodes which algorithm or
215 * algorithms are permitted to be used with this key. The following
216 * algorithm policies are supported:
217 * - 0 does not allow any cryptographic operation with the key. The key
218 * may be used for non-cryptographic actions such as exporting (if
219 * permitted by the usage flags).
220 * - An algorithm value permits this particular algorithm.
221 * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
222 * signature scheme with any hash algorithm.
223 * - An algorithm built from #PSA_ALG_AT_LEAST_THIS_LENGTH_MAC allows
224 * any MAC algorithm from the same base class (e.g. CMAC) which
225 * generates/verifies a MAC length greater than or equal to the length
226 * encoded in the wildcard algorithm.
227 * - An algorithm built from #PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG
228 * allows any AEAD algorithm from the same base class (e.g. CCM) which
229 * generates/verifies a tag length greater than or equal to the length
230 * encoded in the wildcard algorithm.
231 *
232 * This function overwrites any algorithm policy
233 * previously set in \p attributes.
234 *
235 * This function may be declared as `static` (i.e. without external
236 * linkage). This function may be provided as a function-like macro,
237 * but in this case it must evaluate each of its arguments exactly once.
238 *
239 * \param[out] attributes The attribute structure to write to.
240 * \param alg The permitted algorithm policy to write.
241 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200242static void psa_set_key_algorithm(psa_key_attributes_t *attributes, psa_algorithm_t alg);
Julian Halla7e76c82021-04-14 11:12:11 +0100243
244/** Retrieve the algorithm policy from key attributes.
245 *
246 * This function may be declared as `static` (i.e. without external
247 * linkage). This function may be provided as a function-like macro,
248 * but in this case it must evaluate its argument exactly once.
249 *
250 * \param[in] attributes The key attribute structure to query.
251 *
252 * \return The algorithm stored in the attribute structure.
253 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200254static psa_algorithm_t psa_get_key_algorithm(const psa_key_attributes_t *attributes);
Julian Halla7e76c82021-04-14 11:12:11 +0100255
256/** Declare the type of a key.
257 *
258 * This function overwrites any key type
259 * previously set in \p attributes.
260 *
261 * This function may be declared as `static` (i.e. without external
262 * linkage). This function may be provided as a function-like macro,
263 * but in this case it must evaluate each of its arguments exactly once.
264 *
265 * \param[out] attributes The attribute structure to write to.
266 * \param type The key type to write.
267 * If this is 0, the key type in \p attributes
268 * becomes unspecified.
269 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200270static void psa_set_key_type(psa_key_attributes_t *attributes, psa_key_type_t type);
Julian Halla7e76c82021-04-14 11:12:11 +0100271
272/** Declare the size of a key.
273 *
274 * This function overwrites any key size previously set in \p attributes.
275 *
276 * This function may be declared as `static` (i.e. without external
277 * linkage). This function may be provided as a function-like macro,
278 * but in this case it must evaluate each of its arguments exactly once.
279 *
280 * \param[out] attributes The attribute structure to write to.
281 * \param bits The key size in bits.
282 * If this is 0, the key size in \p attributes
283 * becomes unspecified. Keys of size 0 are
284 * not supported.
285 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200286static void psa_set_key_bits(psa_key_attributes_t *attributes, size_t bits);
Julian Halla7e76c82021-04-14 11:12:11 +0100287
288/** Retrieve the key type from key attributes.
289 *
290 * This function may be declared as `static` (i.e. without external
291 * linkage). This function may be provided as a function-like macro,
292 * but in this case it must evaluate its argument exactly once.
293 *
294 * \param[in] attributes The key attribute structure to query.
295 *
296 * \return The key type stored in the attribute structure.
297 */
298static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
299
300/** Retrieve the key size 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 size stored in the attribute structure, in bits.
309 */
310static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
311
312/** Retrieve the attributes of a key.
313 *
314 * This function first resets the attribute structure as with
315 * psa_reset_key_attributes(). It then copies the attributes of
316 * the given key into the given attribute structure.
317 *
318 * \note This function may allocate memory or other resources.
319 * Once you have called this function on an attribute structure,
320 * you must call psa_reset_key_attributes() to free these resources.
321 *
322 * \param[in] key Identifier of the key to query.
323 * \param[in,out] attributes On success, the attributes of the key.
324 * On failure, equivalent to a
325 * freshly-initialized structure.
326 *
327 * \retval #PSA_SUCCESS
328 * \retval #PSA_ERROR_INVALID_HANDLE
329 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
330 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
331 * \retval #PSA_ERROR_CORRUPTION_DETECTED
332 * \retval #PSA_ERROR_STORAGE_FAILURE
333 * \retval #PSA_ERROR_DATA_CORRUPT
334 * \retval #PSA_ERROR_DATA_INVALID
335 * \retval #PSA_ERROR_BAD_STATE
336 * The library has not been previously initialized by psa_crypto_init().
337 * It is implementation-dependent whether a failure to initialize
338 * results in this error code.
339 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200340PSA_CRYPTO_EXPORTED psa_status_t psa_get_key_attributes(psa_key_id_t key,
341 psa_key_attributes_t *attributes);
Julian Halla7e76c82021-04-14 11:12:11 +0100342
343/** Reset a key attribute structure to a freshly initialized state.
344 *
345 * You must initialize the attribute structure as described in the
346 * documentation of the type #psa_key_attributes_t before calling this
347 * function. Once the structure has been initialized, you may call this
348 * function at any time.
349 *
350 * This function frees any auxiliary resources that the structure
351 * may contain.
352 *
353 * \param[in,out] attributes The attribute structure to reset.
354 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200355PSA_CRYPTO_EXPORTED void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Julian Halla7e76c82021-04-14 11:12:11 +0100356
357/**@}*/
358
359/** \defgroup key_management Key management
360 * @{
361 */
362
363/** Remove non-essential copies of key material from memory.
364 *
365 * If the key identifier designates a volatile key, this functions does not do
366 * anything and returns successfully.
367 *
368 * If the key identifier designates a persistent key, then this function will
369 * free all resources associated with the key in volatile memory. The key
370 * data in persistent storage is not affected and the key can still be used.
371 *
372 * \param key Identifier of the key to purge.
373 *
374 * \retval #PSA_SUCCESS
375 * The key material will have been removed from memory if it is not
376 * currently required.
377 * \retval #PSA_ERROR_INVALID_ARGUMENT
378 * \p key is not a valid key identifier.
379 * \retval #PSA_ERROR_BAD_STATE
380 * The library has not been previously initialized by psa_crypto_init().
381 * It is implementation-dependent whether a failure to initialize
382 * results in this error code.
383 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200384PSA_CRYPTO_EXPORTED psa_status_t psa_purge_key(psa_key_id_t key);
Julian Halla7e76c82021-04-14 11:12:11 +0100385
386/** Make a copy of a key.
387 *
388 * Copy key material from one location to another.
389 *
390 * This function is primarily useful to copy a key from one location
391 * to another, since it populates a key using the material from
392 * another key which may have a different lifetime.
393 *
394 * This function may be used to share a key with a different party,
395 * subject to implementation-defined restrictions on key sharing.
396 *
397 * The policy on the source key must have the usage flag
398 * #PSA_KEY_USAGE_COPY set.
399 * This flag is sufficient to permit the copy if the key has the lifetime
400 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
401 * Some secure elements do not provide a way to copy a key without
402 * making it extractable from the secure element. If a key is located
403 * in such a secure element, then the key must have both usage flags
404 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
405 * a copy of the key outside the secure element.
406 *
407 * The resulting key may only be used in a way that conforms to
408 * both the policy of the original key and the policy specified in
409 * the \p attributes parameter:
410 * - The usage flags on the resulting key are the bitwise-and of the
411 * usage flags on the source policy and the usage flags in \p attributes.
412 * - If both allow the same algorithm or wildcard-based
413 * algorithm policy, the resulting key has the same algorithm policy.
414 * - If either of the policies allows an algorithm and the other policy
415 * allows a wildcard-based algorithm policy that includes this algorithm,
416 * the resulting key allows the same algorithm.
417 * - If the policies do not allow any algorithm in common, this function
418 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
419 *
420 * The effect of this function on implementation-defined attributes is
421 * implementation-defined.
422 *
423 * \param source_key The key to copy. It must allow the usage
424 * #PSA_KEY_USAGE_COPY. If a private or secret key is
425 * being copied outside of a secure element it must
426 * also allow #PSA_KEY_USAGE_EXPORT.
427 * \param[in] attributes The attributes for the new key.
428 * They are used as follows:
429 * - The key type and size may be 0. If either is
430 * nonzero, it must match the corresponding
431 * attribute of the source key.
432 * - The key location (the lifetime and, for
433 * persistent keys, the key identifier) is
434 * used directly.
435 * - The policy constraints (usage flags and
436 * algorithm policy) are combined from
437 * the source key and \p attributes so that
438 * both sets of restrictions apply, as
439 * described in the documentation of this function.
440 * \param[out] target_key On success, an identifier for the newly created
441 * key. For persistent keys, this is the key
442 * identifier defined in \p attributes.
443 * \c 0 on failure.
444 *
445 * \retval #PSA_SUCCESS
446 * \retval #PSA_ERROR_INVALID_HANDLE
447 * \p source_key is invalid.
448 * \retval #PSA_ERROR_ALREADY_EXISTS
449 * This is an attempt to create a persistent key, and there is
450 * already a persistent key with the given identifier.
451 * \retval #PSA_ERROR_INVALID_ARGUMENT
452 * The lifetime or identifier in \p attributes are invalid.
453 * \retval #PSA_ERROR_INVALID_ARGUMENT
454 * The policy constraints on the source and specified in
455 * \p attributes are incompatible.
456 * \retval #PSA_ERROR_INVALID_ARGUMENT
457 * \p attributes specifies a key type or key size
458 * which does not match the attributes of the source key.
459 * \retval #PSA_ERROR_NOT_PERMITTED
460 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
461 * \retval #PSA_ERROR_NOT_PERMITTED
462 * The source key is not exportable and its lifetime does not
463 * allow copying it to the target's lifetime.
464 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
465 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
466 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
467 * \retval #PSA_ERROR_HARDWARE_FAILURE
468 * \retval #PSA_ERROR_DATA_INVALID
469 * \retval #PSA_ERROR_DATA_CORRUPT
470 * \retval #PSA_ERROR_STORAGE_FAILURE
471 * \retval #PSA_ERROR_CORRUPTION_DETECTED
472 * \retval #PSA_ERROR_BAD_STATE
473 * The library has not been previously initialized by psa_crypto_init().
474 * It is implementation-dependent whether a failure to initialize
475 * results in this error code.
476 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200477PSA_CRYPTO_EXPORTED psa_status_t psa_copy_key(psa_key_id_t source_key,
478 const psa_key_attributes_t *attributes,
479 psa_key_id_t *target_key);
Julian Halla7e76c82021-04-14 11:12:11 +0100480
481/**
482 * \brief Destroy a key.
483 *
484 * This function destroys a key from both volatile
485 * memory and, if applicable, non-volatile storage. Implementations shall
486 * make a best effort to ensure that that the key material cannot be recovered.
487 *
488 * This function also erases any metadata such as policies and frees
489 * resources associated with the key.
490 *
491 * If a key is currently in use in a multipart operation, then destroying the
492 * key will cause the multipart operation to fail.
493 *
494 * \param key Identifier of the key to erase. If this is \c 0, do nothing and
495 * return #PSA_SUCCESS.
496 *
497 * \retval #PSA_SUCCESS
498 * \p key was a valid identifier and the key material that it
499 * referred to has been erased. Alternatively, \p key is \c 0.
500 * \retval #PSA_ERROR_NOT_PERMITTED
501 * The key cannot be erased because it is
502 * read-only, either due to a policy or due to physical restrictions.
503 * \retval #PSA_ERROR_INVALID_HANDLE
504 * \p key is not a valid identifier nor \c 0.
505 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
506 * There was an failure in communication with the cryptoprocessor.
507 * The key material may still be present in the cryptoprocessor.
508 * \retval #PSA_ERROR_DATA_INVALID
509 * This error is typically a result of either storage corruption on a
510 * cleartext storage backend, or an attempt to read data that was
511 * written by an incompatible version of the library.
512 * \retval #PSA_ERROR_STORAGE_FAILURE
513 * The storage is corrupted. Implementations shall make a best effort
514 * to erase key material even in this stage, however applications
515 * should be aware that it may be impossible to guarantee that the
516 * key material is not recoverable in such cases.
517 * \retval #PSA_ERROR_CORRUPTION_DETECTED
518 * An unexpected condition which is not a storage corruption or
519 * a communication failure occurred. The cryptoprocessor may have
520 * been compromised.
521 * \retval #PSA_ERROR_BAD_STATE
522 * The library has not been previously initialized by psa_crypto_init().
523 * It is implementation-dependent whether a failure to initialize
524 * results in this error code.
525 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200526PSA_CRYPTO_EXPORTED psa_status_t psa_destroy_key(psa_key_id_t key);
Julian Halla7e76c82021-04-14 11:12:11 +0100527
528/**@}*/
529
530/** \defgroup import_export Key import and export
531 * @{
532 */
533
534/**
535 * \brief Import a key in binary format.
536 *
537 * This function supports any output from psa_export_key(). Refer to the
538 * documentation of psa_export_public_key() for the format of public keys
539 * and to the documentation of psa_export_key() for the format for
540 * other key types.
541 *
542 * The key data determines the key size. The attributes may optionally
543 * specify a key size; in this case it must match the size determined
544 * from the key data. A key size of 0 in \p attributes indicates that
545 * the key size is solely determined by the key data.
546 *
547 * Implementations must reject an attempt to import a key of size 0.
548 *
549 * This specification supports a single format for each key type.
550 * Implementations may support other formats as long as the standard
551 * format is supported. Implementations that support other formats
552 * should ensure that the formats are clearly unambiguous so as to
553 * minimize the risk that an invalid input is accidentally interpreted
554 * according to a different format.
555 *
556 * \param[in] attributes The attributes for the new key.
557 * The key size is always determined from the
558 * \p data buffer.
559 * If the key size in \p attributes is nonzero,
560 * it must be equal to the size from \p data.
561 * \param[out] key On success, an identifier to the newly created key.
562 * For persistent keys, this is the key identifier
563 * defined in \p attributes.
564 * \c 0 on failure.
565 * \param[in] data Buffer containing the key data. The content of this
566 * buffer is interpreted according to the type declared
567 * in \p attributes.
568 * All implementations must support at least the format
569 * described in the documentation
570 * of psa_export_key() or psa_export_public_key() for
571 * the chosen type. Implementations may allow other
572 * formats, but should be conservative: implementations
573 * should err on the side of rejecting content if it
574 * may be erroneous (e.g. wrong type or truncated data).
575 * \param data_length Size of the \p data buffer in bytes.
576 *
577 * \retval #PSA_SUCCESS
578 * Success.
579 * If the key is persistent, the key material and the key's metadata
580 * have been saved to persistent storage.
581 * \retval #PSA_ERROR_ALREADY_EXISTS
582 * This is an attempt to create a persistent key, and there is
583 * already a persistent key with the given identifier.
584 * \retval #PSA_ERROR_NOT_SUPPORTED
585 * The key type or key size is not supported, either by the
586 * implementation in general or in this particular persistent location.
587 * \retval #PSA_ERROR_INVALID_ARGUMENT
588 * The key attributes, as a whole, are invalid.
589 * \retval #PSA_ERROR_INVALID_ARGUMENT
590 * The key data is not correctly formatted.
591 * \retval #PSA_ERROR_INVALID_ARGUMENT
592 * The size in \p attributes is nonzero and does not match the size
593 * of the key data.
594 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
595 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
596 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
597 * \retval #PSA_ERROR_DATA_CORRUPT
598 * \retval #PSA_ERROR_DATA_INVALID
599 * \retval #PSA_ERROR_STORAGE_FAILURE
600 * \retval #PSA_ERROR_HARDWARE_FAILURE
601 * \retval #PSA_ERROR_CORRUPTION_DETECTED
602 * \retval #PSA_ERROR_BAD_STATE
603 * The library has not been previously initialized by psa_crypto_init().
604 * It is implementation-dependent whether a failure to initialize
605 * results in this error code.
606 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200607PSA_CRYPTO_EXPORTED psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
608 const uint8_t *data, size_t data_length,
609 psa_key_id_t *key);
Julian Halla7e76c82021-04-14 11:12:11 +0100610
611/**
612 * \brief Export a key in binary format.
613 *
614 * The output of this function can be passed to psa_import_key() to
615 * create an equivalent object.
616 *
617 * If the implementation of psa_import_key() supports other formats
618 * beyond the format specified here, the output from psa_export_key()
619 * must use the representation specified here, not the original
620 * representation.
621 *
622 * For standard key types, the output format is as follows:
623 *
624 * - For symmetric keys (including MAC keys), the format is the
625 * raw bytes of the key.
626 * - For DES, the key data consists of 8 bytes. The parity bits must be
627 * correct.
628 * - For Triple-DES, the format is the concatenation of the
629 * two or three DES keys.
630 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
631 * is the non-encrypted DER encoding of the representation defined by
632 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
633 * ```
634 * RSAPrivateKey ::= SEQUENCE {
635 * version INTEGER, -- must be 0
636 * modulus INTEGER, -- n
637 * publicExponent INTEGER, -- e
638 * privateExponent INTEGER, -- d
639 * prime1 INTEGER, -- p
640 * prime2 INTEGER, -- q
641 * exponent1 INTEGER, -- d mod (p-1)
642 * exponent2 INTEGER, -- d mod (q-1)
643 * coefficient INTEGER, -- (inverse of q) mod p
644 * }
645 * ```
646 * - For elliptic curve key pairs (key types for which
647 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
648 * a representation of the private value as a `ceiling(m/8)`-byte string
649 * where `m` is the bit size associated with the curve, i.e. the bit size
650 * of the order of the curve's coordinate field. This byte string is
651 * in little-endian order for Montgomery curves (curve types
652 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
653 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
654 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
655 * For Weierstrass curves, this is the content of the `privateKey` field of
656 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
657 * the format is defined by RFC 7748, and output is masked according to §5.
658 * - For Diffie-Hellman key exchange key pairs (key types for which
659 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
660 * format is the representation of the private key `x` as a big-endian byte
661 * string. The length of the byte string is the private key size in bytes
662 * (leading zeroes are not stripped).
663 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
664 * true), the format is the same as for psa_export_public_key().
665 *
666 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
667 *
668 * \param key Identifier of the key to export. It must allow the
669 * usage #PSA_KEY_USAGE_EXPORT, unless it is a public
670 * key.
671 * \param[out] data Buffer where the key data is to be written.
672 * \param data_size Size of the \p data buffer in bytes.
673 * \param[out] data_length On success, the number of bytes
674 * that make up the key data.
675 *
676 * \retval #PSA_SUCCESS
677 * \retval #PSA_ERROR_INVALID_HANDLE
678 * \retval #PSA_ERROR_NOT_PERMITTED
679 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
680 * \retval #PSA_ERROR_NOT_SUPPORTED
681 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
682 * The size of the \p data buffer is too small. You can determine a
683 * sufficient buffer size by calling
684 * #PSA_EXPORT_KEY_OUTPUT_SIZE(\c type, \c bits)
685 * where \c type is the key type
686 * and \c bits is the key size in bits.
687 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
688 * \retval #PSA_ERROR_HARDWARE_FAILURE
689 * \retval #PSA_ERROR_CORRUPTION_DETECTED
690 * \retval #PSA_ERROR_STORAGE_FAILURE
691 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
692 * \retval #PSA_ERROR_BAD_STATE
693 * The library has not been previously initialized by psa_crypto_init().
694 * It is implementation-dependent whether a failure to initialize
695 * results in this error code.
696 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200697PSA_CRYPTO_EXPORTED psa_status_t psa_export_key(psa_key_id_t key, uint8_t *data, size_t data_size,
698 size_t *data_length);
Julian Halla7e76c82021-04-14 11:12:11 +0100699
700/**
701 * \brief Export a public key or the public part of a key pair in binary format.
702 *
703 * The output of this function can be passed to psa_import_key() to
704 * create an object that is equivalent to the public key.
705 *
706 * This specification supports a single format for each key type.
707 * Implementations may support other formats as long as the standard
708 * format is supported. Implementations that support other formats
709 * should ensure that the formats are clearly unambiguous so as to
710 * minimize the risk that an invalid input is accidentally interpreted
711 * according to a different format.
712 *
713 * For standard key types, the output format is as follows:
714 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
715 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
716 * ```
717 * RSAPublicKey ::= SEQUENCE {
718 * modulus INTEGER, -- n
719 * publicExponent INTEGER } -- e
720 * ```
721 * - For elliptic curve public keys (key types for which
722 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
723 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
724 * Let `m` be the bit size associated with the curve, i.e. the bit size of
725 * `q` for a curve over `F_q`. The representation consists of:
726 * - The byte 0x04;
727 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
728 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
729 * - For Diffie-Hellman key exchange public keys (key types for which
730 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
731 * the format is the representation of the public key `y = g^x mod p` as a
732 * big-endian byte string. The length of the byte string is the length of the
733 * base prime `p` in bytes.
734 *
735 * Exporting a public key object or the public part of a key pair is
736 * always permitted, regardless of the key's usage flags.
737 *
738 * \param key Identifier of the key to export.
739 * \param[out] data Buffer where the key data is to be written.
740 * \param data_size Size of the \p data buffer in bytes.
741 * \param[out] data_length On success, the number of bytes
742 * that make up the key data.
743 *
744 * \retval #PSA_SUCCESS
745 * \retval #PSA_ERROR_INVALID_HANDLE
746 * \retval #PSA_ERROR_INVALID_ARGUMENT
747 * The key is neither a public key nor a key pair.
748 * \retval #PSA_ERROR_NOT_SUPPORTED
749 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
750 * The size of the \p data buffer is too small. You can determine a
751 * sufficient buffer size by calling
752 * #PSA_EXPORT_KEY_OUTPUT_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
753 * where \c type is the key type
754 * and \c bits is the key size in bits.
755 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
756 * \retval #PSA_ERROR_HARDWARE_FAILURE
757 * \retval #PSA_ERROR_CORRUPTION_DETECTED
758 * \retval #PSA_ERROR_STORAGE_FAILURE
759 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
760 * \retval #PSA_ERROR_BAD_STATE
761 * The library has not been previously initialized by psa_crypto_init().
762 * It is implementation-dependent whether a failure to initialize
763 * results in this error code.
764 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200765PSA_CRYPTO_EXPORTED psa_status_t psa_export_public_key(psa_key_id_t key, uint8_t *data,
766 size_t data_size, size_t *data_length);
Julian Halla7e76c82021-04-14 11:12:11 +0100767
768/**@}*/
769
770/** \defgroup hash Message digests
771 * @{
772 */
773
774/** Calculate the hash (digest) of a message.
775 *
776 * \note To verify the hash of a message against an
777 * expected value, use psa_hash_compare() instead.
778 *
779 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
780 * such that #PSA_ALG_IS_HASH(\p alg) is true).
781 * \param[in] input Buffer containing the message to hash.
782 * \param input_length Size of the \p input buffer in bytes.
783 * \param[out] hash Buffer where the hash is to be written.
784 * \param hash_size Size of the \p hash buffer in bytes.
785 * \param[out] hash_length On success, the number of bytes
786 * that make up the hash value. This is always
787 * #PSA_HASH_LENGTH(\p alg).
788 *
789 * \retval #PSA_SUCCESS
790 * Success.
791 * \retval #PSA_ERROR_NOT_SUPPORTED
792 * \p alg is not supported or is not a hash algorithm.
793 * \retval #PSA_ERROR_INVALID_ARGUMENT
794 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
795 * \p hash_size is too small
796 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
797 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
798 * \retval #PSA_ERROR_HARDWARE_FAILURE
799 * \retval #PSA_ERROR_CORRUPTION_DETECTED
800 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
801 * \retval #PSA_ERROR_BAD_STATE
802 * The library has not been previously initialized by psa_crypto_init().
803 * It is implementation-dependent whether a failure to initialize
804 * results in this error code.
805 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200806PSA_CRYPTO_EXPORTED psa_status_t psa_hash_compute(psa_algorithm_t alg, const uint8_t *input,
807 size_t input_length, uint8_t *hash,
808 size_t hash_size, size_t *hash_length);
Julian Halla7e76c82021-04-14 11:12:11 +0100809
810/** Calculate the hash (digest) of a message and compare it with a
811 * reference value.
812 *
813 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
814 * such that #PSA_ALG_IS_HASH(\p alg) is true).
815 * \param[in] input Buffer containing the message to hash.
816 * \param input_length Size of the \p input buffer in bytes.
817 * \param[out] hash Buffer containing the expected hash value.
818 * \param hash_length Size of the \p hash buffer in bytes.
819 *
820 * \retval #PSA_SUCCESS
821 * The expected hash is identical to the actual hash of the input.
822 * \retval #PSA_ERROR_INVALID_SIGNATURE
823 * The hash of the message was calculated successfully, but it
824 * differs from the expected hash.
825 * \retval #PSA_ERROR_NOT_SUPPORTED
826 * \p alg is not supported or is not a hash algorithm.
827 * \retval #PSA_ERROR_INVALID_ARGUMENT
828 * \p input_length or \p hash_length do not match the hash size for \p alg
829 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
830 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
831 * \retval #PSA_ERROR_HARDWARE_FAILURE
832 * \retval #PSA_ERROR_CORRUPTION_DETECTED
833 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
834 * \retval #PSA_ERROR_BAD_STATE
835 * The library has not been previously initialized by psa_crypto_init().
836 * It is implementation-dependent whether a failure to initialize
837 * results in this error code.
838 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200839PSA_CRYPTO_EXPORTED psa_status_t psa_hash_compare(psa_algorithm_t alg, const uint8_t *input,
840 size_t input_length, const uint8_t *hash,
841 size_t hash_length);
Julian Halla7e76c82021-04-14 11:12:11 +0100842
843/** The type of the state data structure for multipart hash operations.
844 *
845 * Before calling any function on a hash operation object, the application must
846 * initialize it by any of the following means:
847 * - Set the structure to all-bits-zero, for example:
848 * \code
849 * psa_hash_operation_t operation;
850 * memset(&operation, 0, sizeof(operation));
851 * \endcode
852 * - Initialize the structure to logical zero values, for example:
853 * \code
854 * psa_hash_operation_t operation = {0};
855 * \endcode
856 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
857 * for example:
858 * \code
859 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
860 * \endcode
861 * - Assign the result of the function psa_hash_operation_init()
862 * to the structure, for example:
863 * \code
864 * psa_hash_operation_t operation;
865 * operation = psa_hash_operation_init();
866 * \endcode
867 *
868 * This is an implementation-defined \c struct. Applications should not
869 * make any assumptions about the content of this structure except
870 * as directed by the documentation of a specific implementation. */
871typedef struct psa_hash_operation_s psa_hash_operation_t;
872
873/** \def PSA_HASH_OPERATION_INIT
874 *
875 * This macro returns a suitable initializer for a hash operation object
876 * of type #psa_hash_operation_t.
877 */
878#ifdef __DOXYGEN_ONLY__
879/* This is an example definition for documentation purposes.
880 * Implementations should define a suitable value in `crypto_struct.h`.
881 */
882#define PSA_HASH_OPERATION_INIT {0}
883#endif
884
885/** Return an initial value for a hash operation object.
886 */
887static psa_hash_operation_t psa_hash_operation_init(void);
888
889/** Set up a multipart hash operation.
890 *
891 * The sequence of operations to calculate a hash (message digest)
892 * is as follows:
893 * -# Allocate an operation object which will be passed to all the functions
894 * listed here.
895 * -# Initialize the operation object with one of the methods described in the
896 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
897 * -# Call psa_hash_setup() to specify the algorithm.
898 * -# Call psa_hash_update() zero, one or more times, passing a fragment
899 * of the message each time. The hash that is calculated is the hash
900 * of the concatenation of these messages in order.
901 * -# To calculate the hash, call psa_hash_finish().
902 * To compare the hash with an expected value, call psa_hash_verify().
903 *
904 * If an error occurs at any step after a call to psa_hash_setup(), the
905 * operation will need to be reset by a call to psa_hash_abort(). The
906 * application may call psa_hash_abort() at any time after the operation
907 * has been initialized.
908 *
909 * After a successful call to psa_hash_setup(), the application must
910 * eventually terminate the operation. The following events terminate an
911 * operation:
912 * - A successful call to psa_hash_finish() or psa_hash_verify().
913 * - A call to psa_hash_abort().
914 *
915 * \param[in,out] operation The operation object to set up. It must have
916 * been initialized as per the documentation for
917 * #psa_hash_operation_t and not yet in use.
918 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
919 * such that #PSA_ALG_IS_HASH(\p alg) is true).
920 *
921 * \retval #PSA_SUCCESS
922 * Success.
923 * \retval #PSA_ERROR_NOT_SUPPORTED
924 * \p alg is not a supported hash algorithm.
925 * \retval #PSA_ERROR_INVALID_ARGUMENT
926 * \p alg is not a hash algorithm.
927 * \retval #PSA_ERROR_BAD_STATE
928 * The operation state is not valid (it must be inactive).
929 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
930 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
931 * \retval #PSA_ERROR_HARDWARE_FAILURE
932 * \retval #PSA_ERROR_CORRUPTION_DETECTED
933 * \retval #PSA_ERROR_BAD_STATE
934 * The library has not been previously initialized by psa_crypto_init().
935 * It is implementation-dependent whether a failure to initialize
936 * results in this error code.
937 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200938PSA_CRYPTO_EXPORTED psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
939 psa_algorithm_t alg);
Julian Halla7e76c82021-04-14 11:12:11 +0100940
941/** Add a message fragment to a multipart hash operation.
942 *
943 * The application must call psa_hash_setup() before calling this function.
944 *
945 * If this function returns an error status, the operation enters an error
946 * state and must be aborted by calling psa_hash_abort().
947 *
948 * \param[in,out] operation Active hash operation.
949 * \param[in] input Buffer containing the message fragment to hash.
950 * \param input_length Size of the \p input buffer in bytes.
951 *
952 * \retval #PSA_SUCCESS
953 * Success.
954 * \retval #PSA_ERROR_BAD_STATE
955 * The operation state is not valid (it muct be active).
956 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
957 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
958 * \retval #PSA_ERROR_HARDWARE_FAILURE
959 * \retval #PSA_ERROR_CORRUPTION_DETECTED
960 * \retval #PSA_ERROR_BAD_STATE
961 * The library has not been previously initialized by psa_crypto_init().
962 * It is implementation-dependent whether a failure to initialize
963 * results in this error code.
964 */
Gabor Toth99e4ec52023-07-18 08:24:20 +0200965PSA_CRYPTO_EXPORTED psa_status_t psa_hash_update(psa_hash_operation_t *operation,
966 const uint8_t *input, size_t input_length);
Julian Halla7e76c82021-04-14 11:12:11 +0100967
968/** Finish the calculation of the hash of a message.
969 *
970 * The application must call psa_hash_setup() before calling this function.
971 * This function calculates the hash of the message formed by concatenating
972 * the inputs passed to preceding calls to psa_hash_update().
973 *
974 * When this function returns successfuly, the operation becomes inactive.
975 * If this function returns an error status, the operation enters an error
976 * state and must be aborted by calling psa_hash_abort().
977 *
978 * \warning Applications should not call this function if they expect
979 * a specific value for the hash. Call psa_hash_verify() instead.
980 * Beware that comparing integrity or authenticity data such as
981 * hash values with a function such as \c memcmp is risky
982 * because the time taken by the comparison may leak information
983 * about the hashed data which could allow an attacker to guess
984 * a valid hash and thereby bypass security controls.
985 *
986 * \param[in,out] operation Active hash operation.
987 * \param[out] hash Buffer where the hash is to be written.
988 * \param hash_size Size of the \p hash buffer in bytes.
989 * \param[out] hash_length On success, the number of bytes
990 * that make up the hash value. This is always
991 * #PSA_HASH_LENGTH(\c alg) where \c alg is the
992 * hash algorithm that is calculated.
993 *
994 * \retval #PSA_SUCCESS
995 * Success.
996 * \retval #PSA_ERROR_BAD_STATE
997 * The operation state is not valid (it must be active).
998 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
999 * The size of the \p hash buffer is too small. You can determine a
1000 * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
1001 * where \c alg is the hash algorithm that is calculated.
1002 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1003 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1004 * \retval #PSA_ERROR_HARDWARE_FAILURE
1005 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1006 * \retval #PSA_ERROR_BAD_STATE
1007 * The library has not been previously initialized by psa_crypto_init().
1008 * It is implementation-dependent whether a failure to initialize
1009 * results in this error code.
1010 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02001011PSA_CRYPTO_EXPORTED psa_status_t psa_hash_finish(psa_hash_operation_t *operation, uint8_t *hash,
1012 size_t hash_size, size_t *hash_length);
Julian Halla7e76c82021-04-14 11:12:11 +01001013
1014/** Finish the calculation of the hash of a message and compare it with
1015 * an expected value.
1016 *
1017 * The application must call psa_hash_setup() before calling this function.
1018 * This function calculates the hash of the message formed by concatenating
1019 * the inputs passed to preceding calls to psa_hash_update(). It then
1020 * compares the calculated hash with the expected hash passed as a
1021 * parameter to this function.
1022 *
1023 * When this function returns successfuly, the operation becomes inactive.
1024 * If this function returns an error status, the operation enters an error
1025 * state and must be aborted by calling psa_hash_abort().
1026 *
1027 * \note Implementations shall make the best effort to ensure that the
1028 * comparison between the actual hash and the expected hash is performed
1029 * in constant time.
1030 *
1031 * \param[in,out] operation Active hash operation.
1032 * \param[in] hash Buffer containing the expected hash value.
1033 * \param hash_length Size of the \p hash buffer in bytes.
1034 *
1035 * \retval #PSA_SUCCESS
1036 * The expected hash is identical to the actual hash of the message.
1037 * \retval #PSA_ERROR_INVALID_SIGNATURE
1038 * The hash of the message was calculated successfully, but it
1039 * differs from the expected hash.
1040 * \retval #PSA_ERROR_BAD_STATE
1041 * The operation state is not valid (it must be active).
1042 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1043 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1044 * \retval #PSA_ERROR_HARDWARE_FAILURE
1045 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1046 * \retval #PSA_ERROR_BAD_STATE
1047 * The library has not been previously initialized by psa_crypto_init().
1048 * It is implementation-dependent whether a failure to initialize
1049 * results in this error code.
1050 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02001051PSA_CRYPTO_EXPORTED psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1052 const uint8_t *hash, size_t hash_length);
Julian Halla7e76c82021-04-14 11:12:11 +01001053
1054/** Abort a hash operation.
1055 *
1056 * Aborting an operation frees all associated resources except for the
1057 * \p operation structure itself. Once aborted, the operation object
1058 * can be reused for another operation by calling
1059 * psa_hash_setup() again.
1060 *
1061 * You may call this function any time after the operation object has
1062 * been initialized by one of the methods described in #psa_hash_operation_t.
1063 *
1064 * In particular, calling psa_hash_abort() after the operation has been
1065 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1066 * psa_hash_verify() is safe and has no effect.
1067 *
1068 * \param[in,out] operation Initialized hash operation.
1069 *
1070 * \retval #PSA_SUCCESS
1071 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1072 * \retval #PSA_ERROR_HARDWARE_FAILURE
1073 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1074 * \retval #PSA_ERROR_BAD_STATE
1075 * The library has not been previously initialized by psa_crypto_init().
1076 * It is implementation-dependent whether a failure to initialize
1077 * results in this error code.
1078 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02001079PSA_CRYPTO_EXPORTED psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Julian Halla7e76c82021-04-14 11:12:11 +01001080
1081/** Clone a hash operation.
1082 *
1083 * This function copies the state of an ongoing hash operation to
1084 * a new operation object. In other words, this function is equivalent
1085 * to calling psa_hash_setup() on \p target_operation with the same
1086 * algorithm that \p source_operation was set up for, then
1087 * psa_hash_update() on \p target_operation with the same input that
1088 * that was passed to \p source_operation. After this function returns, the
1089 * two objects are independent, i.e. subsequent calls involving one of
1090 * the objects do not affect the other object.
1091 *
1092 * \param[in] source_operation The active hash operation to clone.
1093 * \param[in,out] target_operation The operation object to set up.
1094 * It must be initialized but not active.
1095 *
1096 * \retval #PSA_SUCCESS
1097 * \retval #PSA_ERROR_BAD_STATE
1098 * The \p source_operation state is not valid (it must be active).
1099 * \retval #PSA_ERROR_BAD_STATE
1100 * The \p target_operation state is not valid (it must be inactive).
1101 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1102 * \retval #PSA_ERROR_HARDWARE_FAILURE
1103 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1104 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1105 * \retval #PSA_ERROR_BAD_STATE
1106 * The library has not been previously initialized by psa_crypto_init().
1107 * It is implementation-dependent whether a failure to initialize
1108 * results in this error code.
1109 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02001110PSA_CRYPTO_EXPORTED psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1111 psa_hash_operation_t *target_operation);
Julian Halla7e76c82021-04-14 11:12:11 +01001112
Julian Hallf284b092021-07-23 12:00:01 +01001113/** Suspend a hash operation.
1114 *
1115 * Suspends an operation frees, returns state that may be used to resume
1116 * the operation some time later.
1117 *
1118 * \param[in,out] operation Initialized hash operation.
1119 * \param[out] hash_state Buffer where the hash state is to be written.
1120 * \param hash_state_size Size of the \p hash_state buffer in bytes.
1121 * \param[out] hash_state_length On success, the number of bytes written
1122 * to the hash_state buffer.
1123 *
1124 * \retval #PSA_SUCCESS
1125 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1126 * \retval #PSA_ERROR_HARDWARE_FAILURE
1127 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1128 * \retval #PSA_ERROR_BAD_STATE
1129 * The library has not been previously initialized by psa_crypto_init().
1130 * It is implementation-dependent whether a failure to initialize
1131 * results in this error code.
1132 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02001133PSA_CRYPTO_EXPORTED psa_status_t psa_hash_suspend(psa_hash_operation_t *operation,
1134 uint8_t *hash_state, size_t hash_state_size,
1135 size_t *hash_state_length);
Julian Hallf284b092021-07-23 12:00:01 +01001136
1137/** Resume a hash operation.
1138 *
1139 * Set-up a new multi-part hash operation using the state from a
1140 * previously suspended operation.
1141 *
1142 * \param[in,out] operation The operation object to resume. It must have
1143 * been initialized as per the documentation for
1144 * #psa_hash_operation_t and not yet in use.
1145 * \param[in] hash_state The hash state obtained from a suspended
1146 * operation.
1147 * \param[in] hash_state_length The length of the hash state blob.
1148 *
1149 * \retval #PSA_SUCCESS
1150 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1151 * \retval #PSA_ERROR_HARDWARE_FAILURE
1152 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1153 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1154 * \retval #PSA_ERROR_BAD_STATE
1155 * The library has not been previously initialized by psa_crypto_init().
1156 * It is implementation-dependent whether a failure to initialize
1157 * results in this error code.
1158 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02001159PSA_CRYPTO_EXPORTED psa_status_t psa_hash_resume(psa_hash_operation_t *operation,
1160 const uint8_t *hash_state,
1161 size_t hash_state_length);
Julian Hallf284b092021-07-23 12:00:01 +01001162
Julian Halla7e76c82021-04-14 11:12:11 +01001163/**@}*/
1164
1165/** \defgroup MAC Message authentication codes
1166 * @{
1167 */
1168
1169/** Calculate the MAC (message authentication code) of a message.
1170 *
1171 * \note To verify the MAC of a message against an
1172 * expected value, use psa_mac_verify() instead.
1173 * Beware that comparing integrity or authenticity data such as
1174 * MAC values with a function such as \c memcmp is risky
1175 * because the time taken by the comparison may leak information
1176 * about the MAC value which could allow an attacker to guess
1177 * a valid MAC and thereby bypass security controls.
1178 *
1179 * \param key Identifier of the key to use for the operation. It
1180 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
1181 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1182 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1183 * \param[in] input Buffer containing the input message.
1184 * \param input_length Size of the \p input buffer in bytes.
1185 * \param[out] mac Buffer where the MAC value is to be written.
1186 * \param mac_size Size of the \p mac buffer in bytes.
1187 * \param[out] mac_length On success, the number of bytes
1188 * that make up the MAC value.
1189 *
1190 * \retval #PSA_SUCCESS
1191 * Success.
1192 * \retval #PSA_ERROR_INVALID_HANDLE
1193 * \retval #PSA_ERROR_NOT_PERMITTED
1194 * \retval #PSA_ERROR_INVALID_ARGUMENT
1195 * \p key is not compatible with \p alg.
1196 * \retval #PSA_ERROR_NOT_SUPPORTED
1197 * \p alg is not supported or is not a MAC algorithm.
1198 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1199 * \p mac_size is too small
1200 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1201 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1202 * \retval #PSA_ERROR_HARDWARE_FAILURE
1203 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1204 * \retval #PSA_ERROR_STORAGE_FAILURE
1205 * The key could not be retrieved from storage.
1206 * \retval #PSA_ERROR_BAD_STATE
1207 * The library has not been previously initialized by psa_crypto_init().
1208 * It is implementation-dependent whether a failure to initialize
1209 * results in this error code.
1210 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02001211PSA_CRYPTO_EXPORTED psa_status_t psa_mac_compute(psa_key_id_t key, psa_algorithm_t alg,
1212 const uint8_t *input, size_t input_length,
1213 uint8_t *mac, size_t mac_size, size_t *mac_length);
Julian Halla7e76c82021-04-14 11:12:11 +01001214
1215/** Calculate the MAC of a message and compare it with a reference value.
1216 *
1217 * \param key Identifier of the key to use for the operation. It
1218 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
1219 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1220 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1221 * \param[in] input Buffer containing the input message.
1222 * \param input_length Size of the \p input buffer in bytes.
1223 * \param[out] mac Buffer containing the expected MAC value.
1224 * \param mac_length Size of the \p mac buffer in bytes.
1225 *
1226 * \retval #PSA_SUCCESS
1227 * The expected MAC is identical to the actual MAC of the input.
1228 * \retval #PSA_ERROR_INVALID_SIGNATURE
1229 * The MAC of the message was calculated successfully, but it
1230 * differs from the expected value.
1231 * \retval #PSA_ERROR_INVALID_HANDLE
1232 * \retval #PSA_ERROR_NOT_PERMITTED
1233 * \retval #PSA_ERROR_INVALID_ARGUMENT
1234 * \p key is not compatible with \p alg.
1235 * \retval #PSA_ERROR_NOT_SUPPORTED
1236 * \p alg is not supported or is not a MAC algorithm.
1237 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1238 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1239 * \retval #PSA_ERROR_HARDWARE_FAILURE
1240 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1241 * \retval #PSA_ERROR_STORAGE_FAILURE
1242 * The key could not be retrieved from storage.
1243 * \retval #PSA_ERROR_BAD_STATE
1244 * The library has not been previously initialized by psa_crypto_init().
1245 * It is implementation-dependent whether a failure to initialize
1246 * results in this error code.
1247 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02001248PSA_CRYPTO_EXPORTED psa_status_t psa_mac_verify(psa_key_id_t key, psa_algorithm_t alg,
1249 const uint8_t *input, size_t input_length,
1250 const uint8_t *mac, size_t mac_length);
Julian Halla7e76c82021-04-14 11:12:11 +01001251
1252/** The type of the state data structure for multipart MAC operations.
1253 *
1254 * Before calling any function on a MAC operation object, the application must
1255 * initialize it by any of the following means:
1256 * - Set the structure to all-bits-zero, for example:
1257 * \code
1258 * psa_mac_operation_t operation;
1259 * memset(&operation, 0, sizeof(operation));
1260 * \endcode
1261 * - Initialize the structure to logical zero values, for example:
1262 * \code
1263 * psa_mac_operation_t operation = {0};
1264 * \endcode
1265 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1266 * for example:
1267 * \code
1268 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1269 * \endcode
1270 * - Assign the result of the function psa_mac_operation_init()
1271 * to the structure, for example:
1272 * \code
1273 * psa_mac_operation_t operation;
1274 * operation = psa_mac_operation_init();
1275 * \endcode
1276 *
1277 * This is an implementation-defined \c struct. Applications should not
1278 * make any assumptions about the content of this structure except
1279 * as directed by the documentation of a specific implementation. */
1280typedef struct psa_mac_operation_s psa_mac_operation_t;
1281
1282/** \def PSA_MAC_OPERATION_INIT
1283 *
1284 * This macro returns a suitable initializer for a MAC operation object of type
1285 * #psa_mac_operation_t.
1286 */
1287#ifdef __DOXYGEN_ONLY__
1288/* This is an example definition for documentation purposes.
1289 * Implementations should define a suitable value in `crypto_struct.h`.
1290 */
1291#define PSA_MAC_OPERATION_INIT {0}
1292#endif
1293
1294/** Return an initial value for a MAC operation object.
1295 */
1296static psa_mac_operation_t psa_mac_operation_init(void);
1297
1298/** Set up a multipart MAC calculation operation.
1299 *
1300 * This function sets up the calculation of the MAC
1301 * (message authentication code) of a byte string.
1302 * To verify the MAC of a message against an
1303 * expected value, use psa_mac_verify_setup() instead.
1304 *
1305 * The sequence of operations to calculate a MAC is as follows:
1306 * -# Allocate an operation object which will be passed to all the functions
1307 * listed here.
1308 * -# Initialize the operation object with one of the methods described in the
1309 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
1310 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
1311 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1312 * of the message each time. The MAC that is calculated is the MAC
1313 * of the concatenation of these messages in order.
1314 * -# At the end of the message, call psa_mac_sign_finish() to finish
1315 * calculating the MAC value and retrieve it.
1316 *
1317 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1318 * operation will need to be reset by a call to psa_mac_abort(). The
1319 * application may call psa_mac_abort() at any time after the operation
1320 * has been initialized.
1321 *
1322 * After a successful call to psa_mac_sign_setup(), the application must
1323 * eventually terminate the operation through one of the following methods:
1324 * - A successful call to psa_mac_sign_finish().
1325 * - A call to psa_mac_abort().
1326 *
1327 * \param[in,out] operation The operation object to set up. It must have
1328 * been initialized as per the documentation for
1329 * #psa_mac_operation_t and not yet in use.
1330 * \param key Identifier of the key to use for the operation. It
1331 * must remain valid until the operation terminates.
1332 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
1333 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1334 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1335 *
1336 * \retval #PSA_SUCCESS
1337 * Success.
1338 * \retval #PSA_ERROR_INVALID_HANDLE
1339 * \retval #PSA_ERROR_NOT_PERMITTED
1340 * \retval #PSA_ERROR_INVALID_ARGUMENT
1341 * \p key is not compatible with \p alg.
1342 * \retval #PSA_ERROR_NOT_SUPPORTED
1343 * \p alg is not supported or is not a MAC algorithm.
1344 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1345 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1346 * \retval #PSA_ERROR_HARDWARE_FAILURE
1347 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1348 * \retval #PSA_ERROR_STORAGE_FAILURE
1349 * The key could not be retrieved from storage.
1350 * \retval #PSA_ERROR_BAD_STATE
1351 * The operation state is not valid (it must be inactive).
1352 * \retval #PSA_ERROR_BAD_STATE
1353 * The library has not been previously initialized by psa_crypto_init().
1354 * It is implementation-dependent whether a failure to initialize
1355 * results in this error code.
1356 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02001357PSA_CRYPTO_EXPORTED psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
1358 psa_key_id_t key, psa_algorithm_t alg);
Julian Halla7e76c82021-04-14 11:12:11 +01001359
1360/** Set up a multipart MAC verification operation.
1361 *
1362 * This function sets up the verification of the MAC
1363 * (message authentication code) of a byte string against an expected value.
1364 *
1365 * The sequence of operations to verify a MAC is as follows:
1366 * -# Allocate an operation object which will be passed to all the functions
1367 * listed here.
1368 * -# Initialize the operation object with one of the methods described in the
1369 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
1370 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1371 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1372 * of the message each time. The MAC that is calculated is the MAC
1373 * of the concatenation of these messages in order.
1374 * -# At the end of the message, call psa_mac_verify_finish() to finish
1375 * calculating the actual MAC of the message and verify it against
1376 * the expected value.
1377 *
1378 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1379 * operation will need to be reset by a call to psa_mac_abort(). The
1380 * application may call psa_mac_abort() at any time after the operation
1381 * has been initialized.
1382 *
1383 * After a successful call to psa_mac_verify_setup(), the application must
1384 * eventually terminate the operation through one of the following methods:
1385 * - A successful call to psa_mac_verify_finish().
1386 * - A call to psa_mac_abort().
1387 *
1388 * \param[in,out] operation The operation object to set up. It must have
1389 * been initialized as per the documentation for
1390 * #psa_mac_operation_t and not yet in use.
1391 * \param key Identifier of the key to use for the operation. It
1392 * must remain valid until the operation terminates.
1393 * It must allow the usage
1394 * PSA_KEY_USAGE_VERIFY_MESSAGE.
1395 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1396 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1397 *
1398 * \retval #PSA_SUCCESS
1399 * Success.
1400 * \retval #PSA_ERROR_INVALID_HANDLE
1401 * \retval #PSA_ERROR_NOT_PERMITTED
1402 * \retval #PSA_ERROR_INVALID_ARGUMENT
1403 * \c key is not compatible with \c alg.
1404 * \retval #PSA_ERROR_NOT_SUPPORTED
1405 * \c alg is not supported or is not a MAC algorithm.
1406 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1407 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1408 * \retval #PSA_ERROR_HARDWARE_FAILURE
1409 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1410 * \retval #PSA_ERROR_STORAGE_FAILURE
1411 * The key could not be retrieved from storage
1412 * \retval #PSA_ERROR_BAD_STATE
1413 * The operation state is not valid (it must be inactive).
1414 * \retval #PSA_ERROR_BAD_STATE
1415 * The library has not been previously initialized by psa_crypto_init().
1416 * It is implementation-dependent whether a failure to initialize
1417 * results in this error code.
1418 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02001419PSA_CRYPTO_EXPORTED psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
1420 psa_key_id_t key, psa_algorithm_t alg);
Julian Halla7e76c82021-04-14 11:12:11 +01001421
1422/** Add a message fragment to a multipart MAC operation.
1423 *
1424 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1425 * before calling this function.
1426 *
1427 * If this function returns an error status, the operation enters an error
1428 * state and must be aborted by calling psa_mac_abort().
1429 *
1430 * \param[in,out] operation Active MAC operation.
1431 * \param[in] input Buffer containing the message fragment to add to
1432 * the MAC calculation.
1433 * \param input_length Size of the \p input buffer in bytes.
1434 *
1435 * \retval #PSA_SUCCESS
1436 * Success.
1437 * \retval #PSA_ERROR_BAD_STATE
1438 * The operation state is not valid (it must be active).
1439 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1440 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1441 * \retval #PSA_ERROR_HARDWARE_FAILURE
1442 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1443 * \retval #PSA_ERROR_STORAGE_FAILURE
1444 * \retval #PSA_ERROR_BAD_STATE
1445 * The library has not been previously initialized by psa_crypto_init().
1446 * It is implementation-dependent whether a failure to initialize
1447 * results in this error code.
1448 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02001449PSA_CRYPTO_EXPORTED psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1450 const uint8_t *input, size_t input_length);
Julian Halla7e76c82021-04-14 11:12:11 +01001451
1452/** Finish the calculation of the MAC of a message.
1453 *
1454 * The application must call psa_mac_sign_setup() before calling this function.
1455 * This function calculates the MAC of the message formed by concatenating
1456 * the inputs passed to preceding calls to psa_mac_update().
1457 *
1458 * When this function returns successfuly, the operation becomes inactive.
1459 * If this function returns an error status, the operation enters an error
1460 * state and must be aborted by calling psa_mac_abort().
1461 *
1462 * \warning Applications should not call this function if they expect
1463 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1464 * Beware that comparing integrity or authenticity data such as
1465 * MAC values with a function such as \c memcmp is risky
1466 * because the time taken by the comparison may leak information
1467 * about the MAC value which could allow an attacker to guess
1468 * a valid MAC and thereby bypass security controls.
1469 *
1470 * \param[in,out] operation Active MAC operation.
1471 * \param[out] mac Buffer where the MAC value is to be written.
1472 * \param mac_size Size of the \p mac buffer in bytes.
1473 * \param[out] mac_length On success, the number of bytes
1474 * that make up the MAC value. This is always
1475 * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg)
1476 * where \c key_type and \c key_bits are the type and
1477 * bit-size respectively of the key and \c alg is the
1478 * MAC algorithm that is calculated.
1479 *
1480 * \retval #PSA_SUCCESS
1481 * Success.
1482 * \retval #PSA_ERROR_BAD_STATE
1483 * The operation state is not valid (it must be an active mac sign
1484 * operation).
1485 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1486 * The size of the \p mac buffer is too small. You can determine a
1487 * sufficient buffer size by calling PSA_MAC_LENGTH().
1488 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1489 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1490 * \retval #PSA_ERROR_HARDWARE_FAILURE
1491 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1492 * \retval #PSA_ERROR_STORAGE_FAILURE
1493 * \retval #PSA_ERROR_BAD_STATE
1494 * The library has not been previously initialized by psa_crypto_init().
1495 * It is implementation-dependent whether a failure to initialize
1496 * results in this error code.
1497 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02001498PSA_CRYPTO_EXPORTED psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation, uint8_t *mac,
1499 size_t mac_size, size_t *mac_length);
Julian Halla7e76c82021-04-14 11:12:11 +01001500
1501/** Finish the calculation of the MAC of a message and compare it with
1502 * an expected value.
1503 *
1504 * The application must call psa_mac_verify_setup() before calling this function.
1505 * This function calculates the MAC of the message formed by concatenating
1506 * the inputs passed to preceding calls to psa_mac_update(). It then
1507 * compares the calculated MAC with the expected MAC passed as a
1508 * parameter to this function.
1509 *
1510 * When this function returns successfuly, the operation becomes inactive.
1511 * If this function returns an error status, the operation enters an error
1512 * state and must be aborted by calling psa_mac_abort().
1513 *
1514 * \note Implementations shall make the best effort to ensure that the
1515 * comparison between the actual MAC and the expected MAC is performed
1516 * in constant time.
1517 *
1518 * \param[in,out] operation Active MAC operation.
1519 * \param[in] mac Buffer containing the expected MAC value.
1520 * \param mac_length Size of the \p mac buffer in bytes.
1521 *
1522 * \retval #PSA_SUCCESS
1523 * The expected MAC is identical to the actual MAC of the message.
1524 * \retval #PSA_ERROR_INVALID_SIGNATURE
1525 * The MAC of the message was calculated successfully, but it
1526 * differs from the expected MAC.
1527 * \retval #PSA_ERROR_BAD_STATE
1528 * The operation state is not valid (it must be an active mac verify
1529 * operation).
1530 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1531 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1532 * \retval #PSA_ERROR_HARDWARE_FAILURE
1533 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1534 * \retval #PSA_ERROR_STORAGE_FAILURE
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.
1539 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02001540PSA_CRYPTO_EXPORTED psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1541 const uint8_t *mac, size_t mac_length);
Julian Halla7e76c82021-04-14 11:12:11 +01001542
1543/** Abort a MAC operation.
1544 *
1545 * Aborting an operation frees all associated resources except for the
1546 * \p operation structure itself. Once aborted, the operation object
1547 * can be reused for another operation by calling
1548 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
1549 *
1550 * You may call this function any time after the operation object has
1551 * been initialized by one of the methods described in #psa_mac_operation_t.
1552 *
1553 * In particular, calling psa_mac_abort() after the operation has been
1554 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1555 * psa_mac_verify_finish() is safe and has no effect.
1556 *
1557 * \param[in,out] operation Initialized MAC operation.
1558 *
1559 * \retval #PSA_SUCCESS
1560 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1561 * \retval #PSA_ERROR_HARDWARE_FAILURE
1562 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1563 * \retval #PSA_ERROR_BAD_STATE
1564 * The library has not been previously initialized by psa_crypto_init().
1565 * It is implementation-dependent whether a failure to initialize
1566 * results in this error code.
1567 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02001568PSA_CRYPTO_EXPORTED psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
Julian Halla7e76c82021-04-14 11:12:11 +01001569
1570/**@}*/
1571
1572/** \defgroup cipher Symmetric ciphers
1573 * @{
1574 */
1575
1576/** Encrypt a message using a symmetric cipher.
1577 *
1578 * This function encrypts a message with a random IV (initialization
1579 * vector). Use the multipart operation interface with a
1580 * #psa_cipher_operation_t object to provide other forms of IV.
1581 *
1582 * \param key Identifier of the key to use for the operation.
1583 * It must allow the usage #PSA_KEY_USAGE_ENCRYPT.
1584 * \param alg The cipher algorithm to compute
1585 * (\c PSA_ALG_XXX value such that
1586 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1587 * \param[in] input Buffer containing the message to encrypt.
1588 * \param input_length Size of the \p input buffer in bytes.
1589 * \param[out] output Buffer where the output is to be written.
1590 * The output contains the IV followed by
1591 * the ciphertext proper.
1592 * \param output_size Size of the \p output buffer in bytes.
1593 * \param[out] output_length On success, the number of bytes
1594 * that make up the output.
1595 *
1596 * \retval #PSA_SUCCESS
1597 * Success.
1598 * \retval #PSA_ERROR_INVALID_HANDLE
1599 * \retval #PSA_ERROR_NOT_PERMITTED
1600 * \retval #PSA_ERROR_INVALID_ARGUMENT
1601 * \p key is not compatible with \p alg.
1602 * \retval #PSA_ERROR_NOT_SUPPORTED
1603 * \p alg is not supported or is not a cipher algorithm.
1604 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1605 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1606 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1607 * \retval #PSA_ERROR_HARDWARE_FAILURE
1608 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1609 * \retval #PSA_ERROR_STORAGE_FAILURE
1610 * \retval #PSA_ERROR_BAD_STATE
1611 * The library has not been previously initialized by psa_crypto_init().
1612 * It is implementation-dependent whether a failure to initialize
1613 * results in this error code.
1614 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02001615PSA_CRYPTO_EXPORTED psa_status_t psa_cipher_encrypt(psa_key_id_t key, psa_algorithm_t alg,
1616 const uint8_t *input, size_t input_length,
1617 uint8_t *output, size_t output_size,
1618 size_t *output_length);
Julian Halla7e76c82021-04-14 11:12:11 +01001619
1620/** Decrypt a message using a symmetric cipher.
1621 *
1622 * This function decrypts a message encrypted with a symmetric cipher.
1623 *
1624 * \param key Identifier of the key to use for the operation.
1625 * It must remain valid until the operation
1626 * terminates. It must allow the usage
1627 * #PSA_KEY_USAGE_DECRYPT.
1628 * \param alg The cipher algorithm to compute
1629 * (\c PSA_ALG_XXX value such that
1630 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1631 * \param[in] input Buffer containing the message to decrypt.
1632 * This consists of the IV followed by the
1633 * ciphertext proper.
1634 * \param input_length Size of the \p input buffer in bytes.
1635 * \param[out] output Buffer where the plaintext is to be written.
1636 * \param output_size Size of the \p output buffer in bytes.
1637 * \param[out] output_length On success, the number of bytes
1638 * that make up the output.
1639 *
1640 * \retval #PSA_SUCCESS
1641 * Success.
1642 * \retval #PSA_ERROR_INVALID_HANDLE
1643 * \retval #PSA_ERROR_NOT_PERMITTED
1644 * \retval #PSA_ERROR_INVALID_ARGUMENT
1645 * \p key is not compatible with \p alg.
1646 * \retval #PSA_ERROR_NOT_SUPPORTED
1647 * \p alg is not supported or is not a cipher algorithm.
1648 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1649 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1650 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1651 * \retval #PSA_ERROR_HARDWARE_FAILURE
1652 * \retval #PSA_ERROR_STORAGE_FAILURE
1653 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1654 * \retval #PSA_ERROR_BAD_STATE
1655 * The library has not been previously initialized by psa_crypto_init().
1656 * It is implementation-dependent whether a failure to initialize
1657 * results in this error code.
1658 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02001659PSA_CRYPTO_EXPORTED psa_status_t psa_cipher_decrypt(psa_key_id_t key, psa_algorithm_t alg,
1660 const uint8_t *input, size_t input_length,
1661 uint8_t *output, size_t output_size,
1662 size_t *output_length);
Julian Halla7e76c82021-04-14 11:12:11 +01001663
1664/** The type of the state data structure for multipart cipher operations.
1665 *
1666 * Before calling any function on a cipher operation object, the application
1667 * must initialize it by any of the following means:
1668 * - Set the structure to all-bits-zero, for example:
1669 * \code
1670 * psa_cipher_operation_t operation;
1671 * memset(&operation, 0, sizeof(operation));
1672 * \endcode
1673 * - Initialize the structure to logical zero values, for example:
1674 * \code
1675 * psa_cipher_operation_t operation = {0};
1676 * \endcode
1677 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1678 * for example:
1679 * \code
1680 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1681 * \endcode
1682 * - Assign the result of the function psa_cipher_operation_init()
1683 * to the structure, for example:
1684 * \code
1685 * psa_cipher_operation_t operation;
1686 * operation = psa_cipher_operation_init();
1687 * \endcode
1688 *
1689 * This is an implementation-defined \c struct. Applications should not
1690 * make any assumptions about the content of this structure except
1691 * as directed by the documentation of a specific implementation. */
1692typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1693
1694/** \def PSA_CIPHER_OPERATION_INIT
1695 *
1696 * This macro returns a suitable initializer for a cipher operation object of
1697 * type #psa_cipher_operation_t.
1698 */
1699#ifdef __DOXYGEN_ONLY__
1700/* This is an example definition for documentation purposes.
1701 * Implementations should define a suitable value in `crypto_struct.h`.
1702 */
1703#define PSA_CIPHER_OPERATION_INIT {0}
1704#endif
1705
1706/** Return an initial value for a cipher operation object.
1707 */
1708static psa_cipher_operation_t psa_cipher_operation_init(void);
1709
1710/** Set the key for a multipart symmetric encryption operation.
1711 *
1712 * The sequence of operations to encrypt a message with a symmetric cipher
1713 * is as follows:
1714 * -# Allocate an operation object which will be passed to all the functions
1715 * listed here.
1716 * -# Initialize the operation object with one of the methods described in the
1717 * documentation for #psa_cipher_operation_t, e.g.
1718 * #PSA_CIPHER_OPERATION_INIT.
1719 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
1720 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
1721 * generate or set the IV (initialization vector). You should use
1722 * psa_cipher_generate_iv() unless the protocol you are implementing
1723 * requires a specific IV value.
1724 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1725 * of the message each time.
1726 * -# Call psa_cipher_finish().
1727 *
1728 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1729 * the operation will need to be reset by a call to psa_cipher_abort(). The
1730 * application may call psa_cipher_abort() at any time after the operation
1731 * has been initialized.
1732 *
1733 * After a successful call to psa_cipher_encrypt_setup(), the application must
1734 * eventually terminate the operation. The following events terminate an
1735 * operation:
1736 * - A successful call to psa_cipher_finish().
1737 * - A call to psa_cipher_abort().
1738 *
1739 * \param[in,out] operation The operation object to set up. It must have
1740 * been initialized as per the documentation for
1741 * #psa_cipher_operation_t and not yet in use.
1742 * \param key Identifier of the key to use for the operation.
1743 * It must remain valid until the operation
1744 * terminates. It must allow the usage
1745 * #PSA_KEY_USAGE_ENCRYPT.
1746 * \param alg The cipher algorithm to compute
1747 * (\c PSA_ALG_XXX value such that
1748 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1749 *
1750 * \retval #PSA_SUCCESS
1751 * Success.
1752 * \retval #PSA_ERROR_INVALID_HANDLE
1753 * \retval #PSA_ERROR_NOT_PERMITTED
1754 * \retval #PSA_ERROR_INVALID_ARGUMENT
1755 * \p key is not compatible with \p alg.
1756 * \retval #PSA_ERROR_NOT_SUPPORTED
1757 * \p alg is not supported or is not a cipher algorithm.
1758 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1759 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1760 * \retval #PSA_ERROR_HARDWARE_FAILURE
1761 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1762 * \retval #PSA_ERROR_STORAGE_FAILURE
1763 * \retval #PSA_ERROR_BAD_STATE
1764 * The operation state is not valid (it must be inactive).
1765 * \retval #PSA_ERROR_BAD_STATE
1766 * The library has not been previously initialized by psa_crypto_init().
1767 * It is implementation-dependent whether a failure to initialize
1768 * results in this error code.
1769 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02001770PSA_CRYPTO_EXPORTED psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
1771 psa_key_id_t key, psa_algorithm_t alg);
Julian Halla7e76c82021-04-14 11:12:11 +01001772
1773/** Set the key for a multipart symmetric decryption operation.
1774 *
1775 * The sequence of operations to decrypt a message with a symmetric cipher
1776 * is as follows:
1777 * -# Allocate an operation object which will be passed to all the functions
1778 * listed here.
1779 * -# Initialize the operation object with one of the methods described in the
1780 * documentation for #psa_cipher_operation_t, e.g.
1781 * #PSA_CIPHER_OPERATION_INIT.
1782 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
1783 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
1784 * decryption. If the IV is prepended to the ciphertext, you can call
1785 * psa_cipher_update() on a buffer containing the IV followed by the
1786 * beginning of the message.
1787 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1788 * of the message each time.
1789 * -# Call psa_cipher_finish().
1790 *
1791 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1792 * the operation will need to be reset by a call to psa_cipher_abort(). The
1793 * application may call psa_cipher_abort() at any time after the operation
1794 * has been initialized.
1795 *
1796 * After a successful call to psa_cipher_decrypt_setup(), the application must
1797 * eventually terminate the operation. The following events terminate an
1798 * operation:
1799 * - A successful call to psa_cipher_finish().
1800 * - A call to psa_cipher_abort().
1801 *
1802 * \param[in,out] operation The operation object to set up. It must have
1803 * been initialized as per the documentation for
1804 * #psa_cipher_operation_t and not yet in use.
1805 * \param key Identifier of the key to use for the operation.
1806 * It must remain valid until the operation
1807 * terminates. It must allow the usage
1808 * #PSA_KEY_USAGE_DECRYPT.
1809 * \param alg The cipher algorithm to compute
1810 * (\c PSA_ALG_XXX value such that
1811 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1812 *
1813 * \retval #PSA_SUCCESS
1814 * Success.
1815 * \retval #PSA_ERROR_INVALID_HANDLE
1816 * \retval #PSA_ERROR_NOT_PERMITTED
1817 * \retval #PSA_ERROR_INVALID_ARGUMENT
1818 * \p key is not compatible with \p alg.
1819 * \retval #PSA_ERROR_NOT_SUPPORTED
1820 * \p alg is not supported or is not a cipher algorithm.
1821 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1822 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1823 * \retval #PSA_ERROR_HARDWARE_FAILURE
1824 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1825 * \retval #PSA_ERROR_STORAGE_FAILURE
1826 * \retval #PSA_ERROR_BAD_STATE
1827 * The operation state is not valid (it must be inactive).
1828 * \retval #PSA_ERROR_BAD_STATE
1829 * The library has not been previously initialized by psa_crypto_init().
1830 * It is implementation-dependent whether a failure to initialize
1831 * results in this error code.
1832 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02001833PSA_CRYPTO_EXPORTED psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
1834 psa_key_id_t key, psa_algorithm_t alg);
Julian Halla7e76c82021-04-14 11:12:11 +01001835
1836/** Generate an IV for a symmetric encryption operation.
1837 *
1838 * This function generates a random IV (initialization vector), nonce
1839 * or initial counter value for the encryption operation as appropriate
1840 * for the chosen algorithm, key type and key size.
1841 *
1842 * The application must call psa_cipher_encrypt_setup() before
1843 * calling this function.
1844 *
1845 * If this function returns an error status, the operation enters an error
1846 * state and must be aborted by calling psa_cipher_abort().
1847 *
1848 * \param[in,out] operation Active cipher operation.
1849 * \param[out] iv Buffer where the generated IV is to be written.
1850 * \param iv_size Size of the \p iv buffer in bytes.
1851 * \param[out] iv_length On success, the number of bytes of the
1852 * generated IV.
1853 *
1854 * \retval #PSA_SUCCESS
1855 * Success.
1856 * \retval #PSA_ERROR_BAD_STATE
1857 * The operation state is not valid (it must be active, with no IV set).
1858 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1859 * The size of the \p iv buffer is too small.
1860 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1861 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1862 * \retval #PSA_ERROR_HARDWARE_FAILURE
1863 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1864 * \retval #PSA_ERROR_STORAGE_FAILURE
1865 * \retval #PSA_ERROR_BAD_STATE
1866 * The library has not been previously initialized by psa_crypto_init().
1867 * It is implementation-dependent whether a failure to initialize
1868 * results in this error code.
1869 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02001870PSA_CRYPTO_EXPORTED psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1871 uint8_t *iv, size_t iv_size,
1872 size_t *iv_length);
Julian Halla7e76c82021-04-14 11:12:11 +01001873
1874/** Set the IV for a symmetric encryption or decryption operation.
1875 *
1876 * This function sets the IV (initialization vector), nonce
1877 * or initial counter value for the encryption or decryption operation.
1878 *
1879 * The application must call psa_cipher_encrypt_setup() before
1880 * calling this function.
1881 *
1882 * If this function returns an error status, the operation enters an error
1883 * state and must be aborted by calling psa_cipher_abort().
1884 *
1885 * \note When encrypting, applications should use psa_cipher_generate_iv()
1886 * instead of this function, unless implementing a protocol that requires
1887 * a non-random IV.
1888 *
1889 * \param[in,out] operation Active cipher operation.
1890 * \param[in] iv Buffer containing the IV to use.
1891 * \param iv_length Size of the IV in bytes.
1892 *
1893 * \retval #PSA_SUCCESS
1894 * Success.
1895 * \retval #PSA_ERROR_BAD_STATE
1896 * The operation state is not valid (it must be an active cipher
1897 * encrypt operation, with no IV set).
1898 * \retval #PSA_ERROR_INVALID_ARGUMENT
1899 * The size of \p iv is not acceptable for the chosen algorithm,
1900 * or the chosen algorithm does not use an IV.
1901 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1902 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1903 * \retval #PSA_ERROR_HARDWARE_FAILURE
1904 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1905 * \retval #PSA_ERROR_STORAGE_FAILURE
1906 * \retval #PSA_ERROR_BAD_STATE
1907 * The library has not been previously initialized by psa_crypto_init().
1908 * It is implementation-dependent whether a failure to initialize
1909 * results in this error code.
1910 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02001911PSA_CRYPTO_EXPORTED psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1912 const uint8_t *iv, size_t iv_length);
Julian Halla7e76c82021-04-14 11:12:11 +01001913
1914/** Encrypt or decrypt a message fragment in an active cipher operation.
1915 *
1916 * Before calling this function, you must:
1917 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1918 * The choice of setup function determines whether this function
1919 * encrypts or decrypts its input.
1920 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1921 * (recommended when encrypting) or psa_cipher_set_iv().
1922 *
1923 * If this function returns an error status, the operation enters an error
1924 * state and must be aborted by calling psa_cipher_abort().
1925 *
1926 * \param[in,out] operation Active cipher operation.
1927 * \param[in] input Buffer containing the message fragment to
1928 * encrypt or decrypt.
1929 * \param input_length Size of the \p input buffer in bytes.
1930 * \param[out] output Buffer where the output is to be written.
1931 * \param output_size Size of the \p output buffer in bytes.
1932 * \param[out] output_length On success, the number of bytes
1933 * that make up the returned output.
1934 *
1935 * \retval #PSA_SUCCESS
1936 * Success.
1937 * \retval #PSA_ERROR_BAD_STATE
1938 * The operation state is not valid (it must be active, with an IV set
1939 * if required for the algorithm).
1940 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1941 * The size of the \p output buffer is too small.
1942 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1943 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1944 * \retval #PSA_ERROR_HARDWARE_FAILURE
1945 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1946 * \retval #PSA_ERROR_STORAGE_FAILURE
1947 * \retval #PSA_ERROR_BAD_STATE
1948 * The library has not been previously initialized by psa_crypto_init().
1949 * It is implementation-dependent whether a failure to initialize
1950 * results in this error code.
1951 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02001952PSA_CRYPTO_EXPORTED psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1953 const uint8_t *input, size_t input_length,
1954 uint8_t *output, size_t output_size,
1955 size_t *output_length);
Julian Halla7e76c82021-04-14 11:12:11 +01001956
1957/** Finish encrypting or decrypting a message in a cipher operation.
1958 *
1959 * The application must call psa_cipher_encrypt_setup() or
1960 * psa_cipher_decrypt_setup() before calling this function. The choice
1961 * of setup function determines whether this function encrypts or
1962 * decrypts its input.
1963 *
1964 * This function finishes the encryption or decryption of the message
1965 * formed by concatenating the inputs passed to preceding calls to
1966 * psa_cipher_update().
1967 *
1968 * When this function returns successfuly, the operation becomes inactive.
1969 * If this function returns an error status, the operation enters an error
1970 * state and must be aborted by calling psa_cipher_abort().
1971 *
1972 * \param[in,out] operation Active cipher operation.
1973 * \param[out] output Buffer where the output is to be written.
1974 * \param output_size Size of the \p output buffer in bytes.
1975 * \param[out] output_length On success, the number of bytes
1976 * that make up the returned output.
1977 *
1978 * \retval #PSA_SUCCESS
1979 * Success.
1980 * \retval #PSA_ERROR_INVALID_ARGUMENT
1981 * The total input size passed to this operation is not valid for
1982 * this particular algorithm. For example, the algorithm is a based
1983 * on block cipher and requires a whole number of blocks, but the
1984 * total input size is not a multiple of the block size.
1985 * \retval #PSA_ERROR_INVALID_PADDING
1986 * This is a decryption operation for an algorithm that includes
1987 * padding, and the ciphertext does not contain valid padding.
1988 * \retval #PSA_ERROR_BAD_STATE
1989 * The operation state is not valid (it must be active, with an IV set
1990 * if required for the algorithm).
1991 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1992 * The size of the \p output buffer is too small.
1993 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1994 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1995 * \retval #PSA_ERROR_HARDWARE_FAILURE
1996 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1997 * \retval #PSA_ERROR_STORAGE_FAILURE
1998 * \retval #PSA_ERROR_BAD_STATE
1999 * The library has not been previously initialized by psa_crypto_init().
2000 * It is implementation-dependent whether a failure to initialize
2001 * results in this error code.
2002 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02002003PSA_CRYPTO_EXPORTED psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
2004 uint8_t *output, size_t output_size,
2005 size_t *output_length);
Julian Halla7e76c82021-04-14 11:12:11 +01002006
2007/** Abort a cipher operation.
2008 *
2009 * Aborting an operation frees all associated resources except for the
2010 * \p operation structure itself. Once aborted, the operation object
2011 * can be reused for another operation by calling
2012 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
2013 *
2014 * You may call this function any time after the operation object has
2015 * been initialized as described in #psa_cipher_operation_t.
2016 *
2017 * In particular, calling psa_cipher_abort() after the operation has been
2018 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2019 * is safe and has no effect.
2020 *
2021 * \param[in,out] operation Initialized cipher operation.
2022 *
2023 * \retval #PSA_SUCCESS
2024 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2025 * \retval #PSA_ERROR_HARDWARE_FAILURE
2026 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2027 * \retval #PSA_ERROR_BAD_STATE
2028 * The library has not been previously initialized by psa_crypto_init().
2029 * It is implementation-dependent whether a failure to initialize
2030 * results in this error code.
2031 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02002032PSA_CRYPTO_EXPORTED psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
Julian Halla7e76c82021-04-14 11:12:11 +01002033
2034/**@}*/
2035
2036/** \defgroup aead Authenticated encryption with associated data (AEAD)
2037 * @{
2038 */
2039
2040/** Process an authenticated encryption operation.
2041 *
2042 * \param key Identifier of the key to use for the
2043 * operation. It must allow the usage
2044 * #PSA_KEY_USAGE_ENCRYPT.
2045 * \param alg The AEAD algorithm to compute
2046 * (\c PSA_ALG_XXX value such that
2047 * #PSA_ALG_IS_AEAD(\p alg) is true).
2048 * \param[in] nonce Nonce or IV to use.
2049 * \param nonce_length Size of the \p nonce buffer in bytes.
2050 * \param[in] additional_data Additional data that will be authenticated
2051 * but not encrypted.
2052 * \param additional_data_length Size of \p additional_data in bytes.
2053 * \param[in] plaintext Data that will be authenticated and
2054 * encrypted.
2055 * \param plaintext_length Size of \p plaintext in bytes.
2056 * \param[out] ciphertext Output buffer for the authenticated and
2057 * encrypted data. The additional data is not
2058 * part of this output. For algorithms where the
2059 * encrypted data and the authentication tag
2060 * are defined as separate outputs, the
2061 * authentication tag is appended to the
2062 * encrypted data.
2063 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2064 * This must be at least
2065 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2066 * \p plaintext_length).
2067 * \param[out] ciphertext_length On success, the size of the output
2068 * in the \p ciphertext buffer.
2069 *
2070 * \retval #PSA_SUCCESS
2071 * Success.
2072 * \retval #PSA_ERROR_INVALID_HANDLE
2073 * \retval #PSA_ERROR_NOT_PERMITTED
2074 * \retval #PSA_ERROR_INVALID_ARGUMENT
2075 * \p key is not compatible with \p alg.
2076 * \retval #PSA_ERROR_NOT_SUPPORTED
2077 * \p alg is not supported or is not an AEAD algorithm.
2078 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2079 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2080 * \p ciphertext_size is too small
2081 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2082 * \retval #PSA_ERROR_HARDWARE_FAILURE
2083 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2084 * \retval #PSA_ERROR_STORAGE_FAILURE
2085 * \retval #PSA_ERROR_BAD_STATE
2086 * The library has not been previously initialized by psa_crypto_init().
2087 * It is implementation-dependent whether a failure to initialize
2088 * results in this error code.
2089 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02002090PSA_CRYPTO_EXPORTED psa_status_t psa_aead_encrypt(psa_key_id_t key, psa_algorithm_t alg,
2091 const uint8_t *nonce, size_t nonce_length,
2092 const uint8_t *additional_data,
2093 size_t additional_data_length,
2094 const uint8_t *plaintext, size_t plaintext_length,
2095 uint8_t *ciphertext, size_t ciphertext_size,
2096 size_t *ciphertext_length);
Julian Halla7e76c82021-04-14 11:12:11 +01002097
2098/** Process an authenticated decryption operation.
2099 *
2100 * \param key Identifier of the key to use for the
2101 * operation. It must allow the usage
2102 * #PSA_KEY_USAGE_DECRYPT.
2103 * \param alg The AEAD algorithm to compute
2104 * (\c PSA_ALG_XXX value such that
2105 * #PSA_ALG_IS_AEAD(\p alg) is true).
2106 * \param[in] nonce Nonce or IV to use.
2107 * \param nonce_length Size of the \p nonce buffer in bytes.
2108 * \param[in] additional_data Additional data that has been authenticated
2109 * but not encrypted.
2110 * \param additional_data_length Size of \p additional_data in bytes.
2111 * \param[in] ciphertext Data that has been authenticated and
2112 * encrypted. For algorithms where the
2113 * encrypted data and the authentication tag
2114 * are defined as separate inputs, the buffer
2115 * must contain the encrypted data followed
2116 * by the authentication tag.
2117 * \param ciphertext_length Size of \p ciphertext in bytes.
2118 * \param[out] plaintext Output buffer for the decrypted data.
2119 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2120 * This must be at least
2121 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2122 * \p ciphertext_length).
2123 * \param[out] plaintext_length On success, the size of the output
2124 * in the \p plaintext buffer.
2125 *
2126 * \retval #PSA_SUCCESS
2127 * Success.
2128 * \retval #PSA_ERROR_INVALID_HANDLE
2129 * \retval #PSA_ERROR_INVALID_SIGNATURE
2130 * The ciphertext is not authentic.
2131 * \retval #PSA_ERROR_NOT_PERMITTED
2132 * \retval #PSA_ERROR_INVALID_ARGUMENT
2133 * \p key is not compatible with \p alg.
2134 * \retval #PSA_ERROR_NOT_SUPPORTED
2135 * \p alg is not supported or is not an AEAD algorithm.
2136 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2137 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2138 * \p plaintext_size or \p nonce_length is too small
2139 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2140 * \retval #PSA_ERROR_HARDWARE_FAILURE
2141 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2142 * \retval #PSA_ERROR_STORAGE_FAILURE
2143 * \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.
2147 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02002148PSA_CRYPTO_EXPORTED psa_status_t psa_aead_decrypt(psa_key_id_t key, psa_algorithm_t alg,
2149 const uint8_t *nonce, size_t nonce_length,
2150 const uint8_t *additional_data,
2151 size_t additional_data_length,
2152 const uint8_t *ciphertext,
2153 size_t ciphertext_length, uint8_t *plaintext,
2154 size_t plaintext_size, size_t *plaintext_length);
Julian Halla7e76c82021-04-14 11:12:11 +01002155
2156/** The type of the state data structure for multipart AEAD operations.
2157 *
2158 * Before calling any function on an AEAD operation object, the application
2159 * must initialize it by any of the following means:
2160 * - Set the structure to all-bits-zero, for example:
2161 * \code
2162 * psa_aead_operation_t operation;
2163 * memset(&operation, 0, sizeof(operation));
2164 * \endcode
2165 * - Initialize the structure to logical zero values, for example:
2166 * \code
2167 * psa_aead_operation_t operation = {0};
2168 * \endcode
2169 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2170 * for example:
2171 * \code
2172 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2173 * \endcode
2174 * - Assign the result of the function psa_aead_operation_init()
2175 * to the structure, for example:
2176 * \code
2177 * psa_aead_operation_t operation;
2178 * operation = psa_aead_operation_init();
2179 * \endcode
2180 *
2181 * This is an implementation-defined \c struct. Applications should not
2182 * make any assumptions about the content of this structure except
2183 * as directed by the documentation of a specific implementation. */
2184typedef struct psa_aead_operation_s psa_aead_operation_t;
2185
2186/** \def PSA_AEAD_OPERATION_INIT
2187 *
2188 * This macro returns a suitable initializer for an AEAD operation object of
2189 * type #psa_aead_operation_t.
2190 */
2191#ifdef __DOXYGEN_ONLY__
2192/* This is an example definition for documentation purposes.
2193 * Implementations should define a suitable value in `crypto_struct.h`.
2194 */
2195#define PSA_AEAD_OPERATION_INIT {0}
2196#endif
2197
2198/** Return an initial value for an AEAD operation object.
2199 */
2200static psa_aead_operation_t psa_aead_operation_init(void);
2201
2202/** Set the key for a multipart authenticated encryption operation.
2203 *
2204 * The sequence of operations to encrypt a message with authentication
2205 * is as follows:
2206 * -# Allocate an operation object which will be passed to all the functions
2207 * listed here.
2208 * -# Initialize the operation object with one of the methods described in the
2209 * documentation for #psa_aead_operation_t, e.g.
2210 * #PSA_AEAD_OPERATION_INIT.
2211 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
2212 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2213 * inputs to the subsequent calls to psa_aead_update_ad() and
2214 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2215 * for details.
2216 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2217 * generate or set the nonce. You should use
2218 * psa_aead_generate_nonce() unless the protocol you are implementing
2219 * requires a specific nonce value.
2220 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2221 * of the non-encrypted additional authenticated data each time.
2222 * -# Call psa_aead_update() zero, one or more times, passing a fragment
2223 * of the message to encrypt each time.
2224 * -# Call psa_aead_finish().
2225 *
2226 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2227 * the operation will need to be reset by a call to psa_aead_abort(). The
2228 * application may call psa_aead_abort() at any time after the operation
2229 * has been initialized.
2230 *
2231 * After a successful call to psa_aead_encrypt_setup(), the application must
2232 * eventually terminate the operation. The following events terminate an
2233 * operation:
2234 * - A successful call to psa_aead_finish().
2235 * - A call to psa_aead_abort().
2236 *
2237 * \param[in,out] operation The operation object to set up. It must have
2238 * been initialized as per the documentation for
2239 * #psa_aead_operation_t and not yet in use.
2240 * \param key Identifier of the key to use for the operation.
2241 * It must remain valid until the operation
2242 * terminates. It must allow the usage
2243 * #PSA_KEY_USAGE_ENCRYPT.
2244 * \param alg The AEAD algorithm to compute
2245 * (\c PSA_ALG_XXX value such that
2246 * #PSA_ALG_IS_AEAD(\p alg) is true).
2247 *
2248 * \retval #PSA_SUCCESS
2249 * Success.
2250 * \retval #PSA_ERROR_BAD_STATE
2251 * The operation state is not valid (it must be inactive).
2252 * \retval #PSA_ERROR_INVALID_HANDLE
2253 * \retval #PSA_ERROR_NOT_PERMITTED
2254 * \retval #PSA_ERROR_INVALID_ARGUMENT
2255 * \p key is not compatible with \p alg.
2256 * \retval #PSA_ERROR_NOT_SUPPORTED
2257 * \p alg is not supported or is not an AEAD algorithm.
2258 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2259 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2260 * \retval #PSA_ERROR_HARDWARE_FAILURE
2261 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2262 * \retval #PSA_ERROR_STORAGE_FAILURE
2263 * \retval #PSA_ERROR_BAD_STATE
2264 * The library has not been previously initialized by psa_crypto_init().
2265 * It is implementation-dependent whether a failure to initialize
2266 * results in this error code.
2267 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02002268PSA_CRYPTO_EXPORTED psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2269 psa_key_id_t key, psa_algorithm_t alg);
Julian Halla7e76c82021-04-14 11:12:11 +01002270
2271/** Set the key for a multipart authenticated decryption operation.
2272 *
2273 * The sequence of operations to decrypt a message with authentication
2274 * is as follows:
2275 * -# Allocate an operation object which will be passed to all the functions
2276 * listed here.
2277 * -# Initialize the operation object with one of the methods described in the
2278 * documentation for #psa_aead_operation_t, e.g.
2279 * #PSA_AEAD_OPERATION_INIT.
2280 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
2281 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2282 * inputs to the subsequent calls to psa_aead_update_ad() and
2283 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2284 * for details.
2285 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2286 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2287 * of the non-encrypted additional authenticated data each time.
2288 * -# Call psa_aead_update() zero, one or more times, passing a fragment
2289 * of the ciphertext to decrypt each time.
2290 * -# Call psa_aead_verify().
2291 *
2292 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2293 * the operation will need to be reset by a call to psa_aead_abort(). The
2294 * application may call psa_aead_abort() at any time after the operation
2295 * has been initialized.
2296 *
2297 * After a successful call to psa_aead_decrypt_setup(), the application must
2298 * eventually terminate the operation. The following events terminate an
2299 * operation:
2300 * - A successful call to psa_aead_verify().
2301 * - A call to psa_aead_abort().
2302 *
2303 * \param[in,out] operation The operation object to set up. It must have
2304 * been initialized as per the documentation for
2305 * #psa_aead_operation_t and not yet in use.
2306 * \param key Identifier of the key to use for the operation.
2307 * It must remain valid until the operation
2308 * terminates. It must allow the usage
2309 * #PSA_KEY_USAGE_DECRYPT.
2310 * \param alg The AEAD algorithm to compute
2311 * (\c PSA_ALG_XXX value such that
2312 * #PSA_ALG_IS_AEAD(\p alg) is true).
2313 *
2314 * \retval #PSA_SUCCESS
2315 * Success.
2316 * \retval #PSA_ERROR_BAD_STATE
2317 * The operation state is not valid (it must be inactive).
2318 * \retval #PSA_ERROR_INVALID_HANDLE
2319 * \retval #PSA_ERROR_NOT_PERMITTED
2320 * \retval #PSA_ERROR_INVALID_ARGUMENT
2321 * \p key is not compatible with \p alg.
2322 * \retval #PSA_ERROR_NOT_SUPPORTED
2323 * \p alg is not supported or is not an AEAD algorithm.
2324 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2325 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2326 * \retval #PSA_ERROR_HARDWARE_FAILURE
2327 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2328 * \retval #PSA_ERROR_STORAGE_FAILURE
2329 * \retval #PSA_ERROR_BAD_STATE
2330 * The library has not been previously initialized by psa_crypto_init().
2331 * It is implementation-dependent whether a failure to initialize
2332 * results in this error code.
2333 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02002334PSA_CRYPTO_EXPORTED psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2335 psa_key_id_t key, psa_algorithm_t alg);
Julian Halla7e76c82021-04-14 11:12:11 +01002336
2337/** Generate a random nonce for an authenticated encryption operation.
2338 *
2339 * This function generates a random nonce for the authenticated encryption
2340 * operation with an appropriate size for the chosen algorithm, key type
2341 * and key size.
2342 *
2343 * The application must call psa_aead_encrypt_setup() before
2344 * calling this function.
2345 *
2346 * If this function returns an error status, the operation enters an error
2347 * state and must be aborted by calling psa_aead_abort().
2348 *
2349 * \param[in,out] operation Active AEAD operation.
2350 * \param[out] nonce Buffer where the generated nonce is to be
2351 * written.
2352 * \param nonce_size Size of the \p nonce buffer in bytes.
2353 * \param[out] nonce_length On success, the number of bytes of the
2354 * generated nonce.
2355 *
2356 * \retval #PSA_SUCCESS
2357 * Success.
2358 * \retval #PSA_ERROR_BAD_STATE
2359 * The operation state is not valid (it must be an active aead encrypt
2360 * operation, with no nonce set).
2361 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2362 * The size of the \p nonce buffer is too small.
2363 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2364 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2365 * \retval #PSA_ERROR_HARDWARE_FAILURE
2366 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2367 * \retval #PSA_ERROR_STORAGE_FAILURE
2368 * \retval #PSA_ERROR_BAD_STATE
2369 * The library has not been previously initialized by psa_crypto_init().
2370 * It is implementation-dependent whether a failure to initialize
2371 * results in this error code.
2372 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02002373PSA_CRYPTO_EXPORTED psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2374 uint8_t *nonce, size_t nonce_size,
2375 size_t *nonce_length);
Julian Halla7e76c82021-04-14 11:12:11 +01002376
2377/** Set the nonce for an authenticated encryption or decryption operation.
2378 *
2379 * This function sets the nonce for the authenticated
2380 * encryption or decryption operation.
2381 *
2382 * The application must call psa_aead_encrypt_setup() or
2383 * psa_aead_decrypt_setup() before calling this function.
2384 *
2385 * If this function returns an error status, the operation enters an error
2386 * state and must be aborted by calling psa_aead_abort().
2387 *
2388 * \note When encrypting, applications should use psa_aead_generate_nonce()
2389 * instead of this function, unless implementing a protocol that requires
2390 * a non-random IV.
2391 *
2392 * \param[in,out] operation Active AEAD operation.
2393 * \param[in] nonce Buffer containing the nonce to use.
2394 * \param nonce_length Size of the nonce in bytes.
2395 *
2396 * \retval #PSA_SUCCESS
2397 * Success.
2398 * \retval #PSA_ERROR_BAD_STATE
2399 * The operation state is not valid (it must be active, with no nonce
2400 * set).
2401 * \retval #PSA_ERROR_INVALID_ARGUMENT
2402 * The size of \p nonce is not acceptable for the chosen algorithm.
2403 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2404 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2405 * \retval #PSA_ERROR_HARDWARE_FAILURE
2406 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2407 * \retval #PSA_ERROR_STORAGE_FAILURE
2408 * \retval #PSA_ERROR_BAD_STATE
2409 * The library has not been previously initialized by psa_crypto_init().
2410 * It is implementation-dependent whether a failure to initialize
2411 * results in this error code.
2412 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02002413PSA_CRYPTO_EXPORTED psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2414 const uint8_t *nonce, size_t nonce_length);
Julian Halla7e76c82021-04-14 11:12:11 +01002415
2416/** Declare the lengths of the message and additional data for AEAD.
2417 *
2418 * The application must call this function before calling
2419 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2420 * the operation requires it. If the algorithm does not require it,
2421 * calling this function is optional, but if this function is called
2422 * then the implementation must enforce the lengths.
2423 *
2424 * You may call this function before or after setting the nonce with
2425 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2426 *
2427 * - For #PSA_ALG_CCM, calling this function is required.
2428 * - For the other AEAD algorithms defined in this specification, calling
2429 * this function is not required.
2430 * - For vendor-defined algorithm, refer to the vendor documentation.
2431 *
2432 * If this function returns an error status, the operation enters an error
2433 * state and must be aborted by calling psa_aead_abort().
2434 *
2435 * \param[in,out] operation Active AEAD operation.
2436 * \param ad_length Size of the non-encrypted additional
2437 * authenticated data in bytes.
2438 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2439 *
2440 * \retval #PSA_SUCCESS
2441 * Success.
2442 * \retval #PSA_ERROR_BAD_STATE
2443 * The operation state is not valid (it must be active, and
2444 * psa_aead_update_ad() and psa_aead_update() must not have been
2445 * called yet).
2446 * \retval #PSA_ERROR_INVALID_ARGUMENT
2447 * At least one of the lengths is not acceptable for the chosen
2448 * algorithm.
2449 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2450 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2451 * \retval #PSA_ERROR_HARDWARE_FAILURE
2452 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2453 * \retval #PSA_ERROR_BAD_STATE
2454 * The library has not been previously initialized by psa_crypto_init().
2455 * It is implementation-dependent whether a failure to initialize
2456 * results in this error code.
2457 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02002458PSA_CRYPTO_EXPORTED psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2459 size_t ad_length, size_t plaintext_length);
Julian Halla7e76c82021-04-14 11:12:11 +01002460
2461/** Pass additional data to an active AEAD operation.
2462 *
2463 * Additional data is authenticated, but not encrypted.
2464 *
2465 * You may call this function multiple times to pass successive fragments
2466 * of the additional data. You may not call this function after passing
2467 * data to encrypt or decrypt with psa_aead_update().
2468 *
2469 * Before calling this function, you must:
2470 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2471 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2472 *
2473 * If this function returns an error status, the operation enters an error
2474 * state and must be aborted by calling psa_aead_abort().
2475 *
2476 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2477 * there is no guarantee that the input is valid. Therefore, until
2478 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2479 * treat the input as untrusted and prepare to undo any action that
2480 * depends on the input if psa_aead_verify() returns an error status.
2481 *
2482 * \param[in,out] operation Active AEAD operation.
2483 * \param[in] input Buffer containing the fragment of
2484 * additional data.
2485 * \param input_length Size of the \p input buffer in bytes.
2486 *
2487 * \retval #PSA_SUCCESS
2488 * Success.
2489 * \retval #PSA_ERROR_BAD_STATE
2490 * The operation state is not valid (it must be active, have a nonce
2491 * set, have lengths set if required by the algorithm, and
2492 * psa_aead_update() must not have been called yet).
2493 * \retval #PSA_ERROR_INVALID_ARGUMENT
2494 * The total input length overflows the additional data length that
2495 * was previously specified with psa_aead_set_lengths().
2496 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2497 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2498 * \retval #PSA_ERROR_HARDWARE_FAILURE
2499 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2500 * \retval #PSA_ERROR_STORAGE_FAILURE
2501 * \retval #PSA_ERROR_BAD_STATE
2502 * The library has not been previously initialized by psa_crypto_init().
2503 * It is implementation-dependent whether a failure to initialize
2504 * results in this error code.
2505 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02002506PSA_CRYPTO_EXPORTED psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2507 const uint8_t *input, size_t input_length);
Julian Halla7e76c82021-04-14 11:12:11 +01002508
2509/** Encrypt or decrypt a message fragment in an active AEAD operation.
2510 *
2511 * Before calling this function, you must:
2512 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2513 * The choice of setup function determines whether this function
2514 * encrypts or decrypts its input.
2515 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2516 * 3. Call psa_aead_update_ad() to pass all the additional data.
2517 *
2518 * If this function returns an error status, the operation enters an error
2519 * state and must be aborted by calling psa_aead_abort().
2520 *
2521 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2522 * there is no guarantee that the input is valid. Therefore, until
2523 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2524 * - Do not use the output in any way other than storing it in a
2525 * confidential location. If you take any action that depends
2526 * on the tentative decrypted data, this action will need to be
2527 * undone if the input turns out not to be valid. Furthermore,
2528 * if an adversary can observe that this action took place
2529 * (for example through timing), they may be able to use this
2530 * fact as an oracle to decrypt any message encrypted with the
2531 * same key.
2532 * - In particular, do not copy the output anywhere but to a
2533 * memory or storage space that you have exclusive access to.
2534 *
2535 * This function does not require the input to be aligned to any
2536 * particular block boundary. If the implementation can only process
2537 * a whole block at a time, it must consume all the input provided, but
2538 * it may delay the end of the corresponding output until a subsequent
2539 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2540 * provides sufficient input. The amount of data that can be delayed
2541 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
2542 *
2543 * \param[in,out] operation Active AEAD operation.
2544 * \param[in] input Buffer containing the message fragment to
2545 * encrypt or decrypt.
2546 * \param input_length Size of the \p input buffer in bytes.
2547 * \param[out] output Buffer where the output is to be written.
2548 * \param output_size Size of the \p output buffer in bytes.
2549 * This must be at least
2550 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2551 * \p input_length) where \c alg is the
2552 * algorithm that is being calculated.
2553 * \param[out] output_length On success, the number of bytes
2554 * that make up the returned output.
2555 *
2556 * \retval #PSA_SUCCESS
2557 * Success.
2558 * \retval #PSA_ERROR_BAD_STATE
2559 * The operation state is not valid (it must be active, have a nonce
2560 * set, and have lengths set if required by the algorithm).
2561 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2562 * The size of the \p output buffer is too small.
2563 * You can determine a sufficient buffer size by calling
2564 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2565 * where \c alg is the algorithm that is being calculated.
2566 * \retval #PSA_ERROR_INVALID_ARGUMENT
2567 * The total length of input to psa_aead_update_ad() so far is
2568 * less than the additional data length that was previously
2569 * specified with psa_aead_set_lengths().
2570 * \retval #PSA_ERROR_INVALID_ARGUMENT
2571 * The total input length overflows the plaintext length that
2572 * was previously specified with psa_aead_set_lengths().
2573 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2574 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2575 * \retval #PSA_ERROR_HARDWARE_FAILURE
2576 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2577 * \retval #PSA_ERROR_STORAGE_FAILURE
2578 * \retval #PSA_ERROR_BAD_STATE
2579 * The library has not been previously initialized by psa_crypto_init().
2580 * It is implementation-dependent whether a failure to initialize
2581 * results in this error code.
2582 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02002583PSA_CRYPTO_EXPORTED psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2584 const uint8_t *input, size_t input_length,
2585 uint8_t *output, size_t output_size,
2586 size_t *output_length);
Julian Halla7e76c82021-04-14 11:12:11 +01002587
2588/** Finish encrypting a message in an AEAD operation.
2589 *
2590 * The operation must have been set up with psa_aead_encrypt_setup().
2591 *
2592 * This function finishes the authentication of the additional data
2593 * formed by concatenating the inputs passed to preceding calls to
2594 * psa_aead_update_ad() with the plaintext formed by concatenating the
2595 * inputs passed to preceding calls to psa_aead_update().
2596 *
2597 * This function has two output buffers:
2598 * - \p ciphertext contains trailing ciphertext that was buffered from
2599 * preceding calls to psa_aead_update().
2600 * - \p tag contains the authentication tag. Its length is always
2601 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
2602 * that the operation performs.
2603 *
2604 * When this function returns successfuly, the operation becomes inactive.
2605 * If this function returns an error status, the operation enters an error
2606 * state and must be aborted by calling psa_aead_abort().
2607 *
2608 * \param[in,out] operation Active AEAD operation.
2609 * \param[out] ciphertext Buffer where the last part of the ciphertext
2610 * is to be written.
2611 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2612 * This must be at least
2613 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2614 * \c alg is the algorithm that is being
2615 * calculated.
2616 * \param[out] ciphertext_length On success, the number of bytes of
2617 * returned ciphertext.
2618 * \param[out] tag Buffer where the authentication tag is
2619 * to be written.
2620 * \param tag_size Size of the \p tag buffer in bytes.
2621 * This must be at least
2622 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2623 * the algorithm that is being calculated.
2624 * \param[out] tag_length On success, the number of bytes
2625 * that make up the returned tag.
2626 *
2627 * \retval #PSA_SUCCESS
2628 * Success.
2629 * \retval #PSA_ERROR_BAD_STATE
2630 * The operation state is not valid (it must be an active encryption
2631 * operation with a nonce set).
2632 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2633 * The size of the \p ciphertext or \p tag buffer is too small.
2634 * You can determine a sufficient buffer size for \p ciphertext by
2635 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2636 * where \c alg is the algorithm that is being calculated.
2637 * You can determine a sufficient buffer size for \p tag by
2638 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
2639 * \retval #PSA_ERROR_INVALID_ARGUMENT
2640 * The total length of input to psa_aead_update_ad() so far is
2641 * less than the additional data length that was previously
2642 * specified with psa_aead_set_lengths().
2643 * \retval #PSA_ERROR_INVALID_ARGUMENT
2644 * The total length of input to psa_aead_update() so far is
2645 * less than the plaintext length that was previously
2646 * specified with psa_aead_set_lengths().
2647 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2648 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2649 * \retval #PSA_ERROR_HARDWARE_FAILURE
2650 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2651 * \retval #PSA_ERROR_STORAGE_FAILURE
2652 * \retval #PSA_ERROR_BAD_STATE
2653 * The library has not been previously initialized by psa_crypto_init().
2654 * It is implementation-dependent whether a failure to initialize
2655 * results in this error code.
2656 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02002657PSA_CRYPTO_EXPORTED psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
2658 uint8_t *ciphertext, size_t ciphertext_size,
2659 size_t *ciphertext_length, uint8_t *tag,
2660 size_t tag_size, size_t *tag_length);
Julian Halla7e76c82021-04-14 11:12:11 +01002661
2662/** Finish authenticating and decrypting a message in an AEAD operation.
2663 *
2664 * The operation must have been set up with psa_aead_decrypt_setup().
2665 *
2666 * This function finishes the authenticated decryption of the message
2667 * components:
2668 *
2669 * - The additional data consisting of the concatenation of the inputs
2670 * passed to preceding calls to psa_aead_update_ad().
2671 * - The ciphertext consisting of the concatenation of the inputs passed to
2672 * preceding calls to psa_aead_update().
2673 * - The tag passed to this function call.
2674 *
2675 * If the authentication tag is correct, this function outputs any remaining
2676 * plaintext and reports success. If the authentication tag is not correct,
2677 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
2678 *
2679 * When this function returns successfuly, the operation becomes inactive.
2680 * If this function returns an error status, the operation enters an error
2681 * state and must be aborted by calling psa_aead_abort().
2682 *
2683 * \note Implementations shall make the best effort to ensure that the
2684 * comparison between the actual tag and the expected tag is performed
2685 * in constant time.
2686 *
2687 * \param[in,out] operation Active AEAD operation.
2688 * \param[out] plaintext Buffer where the last part of the plaintext
2689 * is to be written. This is the remaining data
2690 * from previous calls to psa_aead_update()
2691 * that could not be processed until the end
2692 * of the input.
2693 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2694 * This must be at least
2695 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2696 * \c alg is the algorithm that is being
2697 * calculated.
2698 * \param[out] plaintext_length On success, the number of bytes of
2699 * returned plaintext.
2700 * \param[in] tag Buffer containing the authentication tag.
2701 * \param tag_length Size of the \p tag buffer in bytes.
2702 *
2703 * \retval #PSA_SUCCESS
2704 * Success.
2705 * \retval #PSA_ERROR_INVALID_SIGNATURE
2706 * The calculations were successful, but the authentication tag is
2707 * not correct.
2708 * \retval #PSA_ERROR_BAD_STATE
2709 * The operation state is not valid (it must be an active decryption
2710 * operation with a nonce set).
2711 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2712 * The size of the \p plaintext buffer is too small.
2713 * You can determine a sufficient buffer size for \p plaintext by
2714 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2715 * where \c alg is the algorithm that is being calculated.
2716 * \retval #PSA_ERROR_INVALID_ARGUMENT
2717 * The total length of input to psa_aead_update_ad() so far is
2718 * less than the additional data length that was previously
2719 * specified with psa_aead_set_lengths().
2720 * \retval #PSA_ERROR_INVALID_ARGUMENT
2721 * The total length of input to psa_aead_update() so far is
2722 * less than the plaintext length that was previously
2723 * specified with psa_aead_set_lengths().
2724 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2725 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2726 * \retval #PSA_ERROR_HARDWARE_FAILURE
2727 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2728 * \retval #PSA_ERROR_STORAGE_FAILURE
2729 * \retval #PSA_ERROR_BAD_STATE
2730 * The library has not been previously initialized by psa_crypto_init().
2731 * It is implementation-dependent whether a failure to initialize
2732 * results in this error code.
2733 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02002734PSA_CRYPTO_EXPORTED psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2735 uint8_t *plaintext, size_t plaintext_size,
2736 size_t *plaintext_length, const uint8_t *tag,
2737 size_t tag_length);
Julian Halla7e76c82021-04-14 11:12:11 +01002738
2739/** Abort an AEAD operation.
2740 *
2741 * Aborting an operation frees all associated resources except for the
2742 * \p operation structure itself. Once aborted, the operation object
2743 * can be reused for another operation by calling
2744 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2745 *
2746 * You may call this function any time after the operation object has
2747 * been initialized as described in #psa_aead_operation_t.
2748 *
2749 * In particular, calling psa_aead_abort() after the operation has been
2750 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2751 * psa_aead_verify() is safe and has no effect.
2752 *
2753 * \param[in,out] operation Initialized AEAD operation.
2754 *
2755 * \retval #PSA_SUCCESS
2756 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2757 * \retval #PSA_ERROR_HARDWARE_FAILURE
2758 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2759 * \retval #PSA_ERROR_BAD_STATE
2760 * The library has not been previously initialized by psa_crypto_init().
2761 * It is implementation-dependent whether a failure to initialize
2762 * results in this error code.
2763 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02002764PSA_CRYPTO_EXPORTED psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
Julian Halla7e76c82021-04-14 11:12:11 +01002765
2766/**@}*/
2767
2768/** \defgroup asymmetric Asymmetric cryptography
2769 * @{
2770 */
2771
2772/**
Julian Hallf284b092021-07-23 12:00:01 +01002773 * \brief Sign a message with a private key. For hash-and-sign algorithms,
2774 * this includes the hashing step.
2775 *
2776 * \note To perform a multi-part hash-and-sign signature algorithm, first use
2777 * a multi-part hash operation and then pass the resulting hash to
2778 * psa_sign_hash(). PSA_ALG_GET_HASH(\p alg) can be used to determine the
2779 * hash algorithm to use.
2780 *
2781 * \param[in] key Identifier of the key to use for the operation.
2782 * It must be an asymmetric key pair. The key must
2783 * allow the usage #PSA_KEY_USAGE_SIGN_MESSAGE.
2784 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
2785 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2786 * is true), that is compatible with the type of
2787 * \p key.
2788 * \param[in] input The input message to sign.
2789 * \param[in] input_length Size of the \p input buffer in bytes.
2790 * \param[out] signature Buffer where the signature is to be written.
2791 * \param[in] signature_size Size of the \p signature buffer in bytes. This
2792 * must be appropriate for the selected
2793 * algorithm and key:
2794 * - The required signature size is
2795 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2796 * where \c key_type and \c key_bits are the type and
2797 * bit-size respectively of key.
2798 * - #PSA_SIGNATURE_MAX_SIZE evaluates to the
2799 * maximum signature size of any supported
2800 * signature algorithm.
2801 * \param[out] signature_length On success, the number of bytes that make up
2802 * the returned signature value.
2803 *
2804 * \retval #PSA_SUCCESS
2805 * \retval #PSA_ERROR_INVALID_HANDLE
2806 * \retval #PSA_ERROR_NOT_PERMITTED
2807 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2808 * or it does not permit the requested algorithm.
2809 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2810 * The size of the \p signature buffer is too small. You can
2811 * determine a sufficient buffer size by calling
2812 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2813 * where \c key_type and \c key_bits are the type and bit-size
2814 * respectively of \p key.
2815 * \retval #PSA_ERROR_NOT_SUPPORTED
2816 * \retval #PSA_ERROR_INVALID_ARGUMENT
2817 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2818 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2819 * \retval #PSA_ERROR_HARDWARE_FAILURE
2820 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2821 * \retval #PSA_ERROR_STORAGE_FAILURE
2822 * \retval #PSA_ERROR_DATA_CORRUPT
2823 * \retval #PSA_ERROR_DATA_INVALID
2824 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2825 * \retval #PSA_ERROR_BAD_STATE
2826 * The library has not been previously initialized by psa_crypto_init().
2827 * It is implementation-dependent whether a failure to initialize
2828 * results in this error code.
2829 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02002830PSA_CRYPTO_EXPORTED psa_status_t psa_sign_message(psa_key_id_t key, psa_algorithm_t alg,
2831 const uint8_t *input, size_t input_length,
2832 uint8_t *signature, size_t signature_size,
2833 size_t *signature_length);
Julian Hallf284b092021-07-23 12:00:01 +01002834
2835/** \brief Verify the signature of a message with a public key, using
2836 * a hash-and-sign verification algorithm.
2837 *
2838 * \note To perform a multi-part hash-and-sign signature verification
2839 * algorithm, first use a multi-part hash operation to hash the message
2840 * and then pass the resulting hash to psa_verify_hash().
2841 * PSA_ALG_GET_HASH(\p alg) can be used to determine the hash algorithm
2842 * to use.
2843 *
2844 * \param[in] key Identifier of the key to use for the operation.
2845 * It must be a public key or an asymmetric key
2846 * pair. The key must allow the usage
2847 * #PSA_KEY_USAGE_VERIFY_MESSAGE.
2848 * \param[in] alg An asymmetric signature algorithm (PSA_ALG_XXX
2849 * value such that #PSA_ALG_IS_SIGN_MESSAGE(\p alg)
2850 * is true), that is compatible with the type of
2851 * \p key.
2852 * \param[in] input The message whose signature is to be verified.
2853 * \param[in] input_length Size of the \p input buffer in bytes.
2854 * \param[out] signature Buffer containing the signature to verify.
2855 * \param[in] signature_length Size of the \p signature buffer in bytes.
2856 *
2857 * \retval #PSA_SUCCESS
2858 * \retval #PSA_ERROR_INVALID_HANDLE
2859 * \retval #PSA_ERROR_NOT_PERMITTED
2860 * The key does not have the #PSA_KEY_USAGE_SIGN_MESSAGE flag,
2861 * or it does not permit the requested algorithm.
2862 * \retval #PSA_ERROR_INVALID_SIGNATURE
2863 * The calculation was performed successfully, but the passed signature
2864 * is not a valid signature.
2865 * \retval #PSA_ERROR_NOT_SUPPORTED
2866 * \retval #PSA_ERROR_INVALID_ARGUMENT
2867 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2868 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2869 * \retval #PSA_ERROR_HARDWARE_FAILURE
2870 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2871 * \retval #PSA_ERROR_STORAGE_FAILURE
2872 * \retval #PSA_ERROR_DATA_CORRUPT
2873 * \retval #PSA_ERROR_DATA_INVALID
2874 * \retval #PSA_ERROR_BAD_STATE
2875 * The library has not been previously initialized by psa_crypto_init().
2876 * It is implementation-dependent whether a failure to initialize
2877 * results in this error code.
2878 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02002879PSA_CRYPTO_EXPORTED psa_status_t psa_verify_message(psa_key_id_t key, psa_algorithm_t alg,
2880 const uint8_t *input, size_t input_length,
2881 const uint8_t *signature,
2882 size_t signature_length);
Julian Hallf284b092021-07-23 12:00:01 +01002883
2884/**
Julian Halla7e76c82021-04-14 11:12:11 +01002885 * \brief Sign a hash or short message with a private key.
2886 *
2887 * Note that to perform a hash-and-sign signature algorithm, you must
2888 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
2889 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2890 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2891 * to determine the hash algorithm to use.
2892 *
2893 * \param key Identifier of the key to use for the operation.
2894 * It must be an asymmetric key pair. The key must
2895 * allow the usage #PSA_KEY_USAGE_SIGN_HASH.
2896 * \param alg A signature algorithm that is compatible with
2897 * the type of \p key.
2898 * \param[in] hash The hash or message to sign.
2899 * \param hash_length Size of the \p hash buffer in bytes.
2900 * \param[out] signature Buffer where the signature is to be written.
2901 * \param signature_size Size of the \p signature buffer in bytes.
2902 * \param[out] signature_length On success, the number of bytes
2903 * that make up the returned signature value.
2904 *
2905 * \retval #PSA_SUCCESS
2906 * \retval #PSA_ERROR_INVALID_HANDLE
2907 * \retval #PSA_ERROR_NOT_PERMITTED
2908 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2909 * The size of the \p signature buffer is too small. You can
2910 * determine a sufficient buffer size by calling
2911 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2912 * where \c key_type and \c key_bits are the type and bit-size
2913 * respectively of \p key.
2914 * \retval #PSA_ERROR_NOT_SUPPORTED
2915 * \retval #PSA_ERROR_INVALID_ARGUMENT
2916 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2917 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2918 * \retval #PSA_ERROR_HARDWARE_FAILURE
2919 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2920 * \retval #PSA_ERROR_STORAGE_FAILURE
2921 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2922 * \retval #PSA_ERROR_BAD_STATE
2923 * The library has not been previously initialized by psa_crypto_init().
2924 * It is implementation-dependent whether a failure to initialize
2925 * results in this error code.
2926 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02002927PSA_CRYPTO_EXPORTED psa_status_t psa_sign_hash(psa_key_id_t key, psa_algorithm_t alg,
2928 const uint8_t *hash, size_t hash_length,
2929 uint8_t *signature, size_t signature_size,
2930 size_t *signature_length);
Julian Halla7e76c82021-04-14 11:12:11 +01002931
2932/**
2933 * \brief Verify the signature a hash or short message using a public key.
2934 *
2935 * Note that to perform a hash-and-sign signature algorithm, you must
2936 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
2937 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2938 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2939 * to determine the hash algorithm to use.
2940 *
2941 * \param key Identifier of the key to use for the operation. It
2942 * must be a public key or an asymmetric key pair. The
2943 * key must allow the usage
2944 * #PSA_KEY_USAGE_VERIFY_HASH.
2945 * \param alg A signature algorithm that is compatible with
2946 * the type of \p key.
2947 * \param[in] hash The hash or message whose signature is to be
2948 * verified.
2949 * \param hash_length Size of the \p hash buffer in bytes.
2950 * \param[in] signature Buffer containing the signature to verify.
2951 * \param signature_length Size of the \p signature buffer in bytes.
2952 *
2953 * \retval #PSA_SUCCESS
2954 * The signature is valid.
2955 * \retval #PSA_ERROR_INVALID_HANDLE
2956 * \retval #PSA_ERROR_NOT_PERMITTED
2957 * \retval #PSA_ERROR_INVALID_SIGNATURE
2958 * The calculation was perfomed successfully, but the passed
2959 * signature is not a valid signature.
2960 * \retval #PSA_ERROR_NOT_SUPPORTED
2961 * \retval #PSA_ERROR_INVALID_ARGUMENT
2962 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2963 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2964 * \retval #PSA_ERROR_HARDWARE_FAILURE
2965 * \retval #PSA_ERROR_CORRUPTION_DETECTED
2966 * \retval #PSA_ERROR_STORAGE_FAILURE
2967 * \retval #PSA_ERROR_BAD_STATE
2968 * The library has not been previously initialized by psa_crypto_init().
2969 * It is implementation-dependent whether a failure to initialize
2970 * results in this error code.
2971 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02002972PSA_CRYPTO_EXPORTED psa_status_t psa_verify_hash(psa_key_id_t key, psa_algorithm_t alg,
2973 const uint8_t *hash, size_t hash_length,
2974 const uint8_t *signature, size_t signature_length);
Julian Halla7e76c82021-04-14 11:12:11 +01002975
2976/**
2977 * \brief Encrypt a short message with a public key.
2978 *
2979 * \param key Identifer of the key to use for the operation.
2980 * It must be a public key or an asymmetric key
2981 * pair. It must allow the usage
2982 * #PSA_KEY_USAGE_ENCRYPT.
2983 * \param alg An asymmetric encryption algorithm that is
2984 * compatible with the type of \p key.
2985 * \param[in] input The message to encrypt.
2986 * \param input_length Size of the \p input buffer in bytes.
2987 * \param[in] salt A salt or label, if supported by the
2988 * encryption algorithm.
2989 * If the algorithm does not support a
2990 * salt, pass \c NULL.
2991 * If the algorithm supports an optional
2992 * salt and you do not want to pass a salt,
2993 * pass \c NULL.
2994 *
2995 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2996 * supported.
2997 * \param salt_length Size of the \p salt buffer in bytes.
2998 * If \p salt is \c NULL, pass 0.
2999 * \param[out] output Buffer where the encrypted message is to
3000 * be written.
3001 * \param output_size Size of the \p output buffer in bytes.
3002 * \param[out] output_length On success, the number of bytes
3003 * that make up the returned output.
3004 *
3005 * \retval #PSA_SUCCESS
3006 * \retval #PSA_ERROR_INVALID_HANDLE
3007 * \retval #PSA_ERROR_NOT_PERMITTED
3008 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3009 * The size of the \p output buffer is too small. You can
3010 * determine a sufficient buffer size by calling
3011 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
3012 * where \c key_type and \c key_bits are the type and bit-size
3013 * respectively of \p key.
3014 * \retval #PSA_ERROR_NOT_SUPPORTED
3015 * \retval #PSA_ERROR_INVALID_ARGUMENT
3016 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3017 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3018 * \retval #PSA_ERROR_HARDWARE_FAILURE
3019 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3020 * \retval #PSA_ERROR_STORAGE_FAILURE
3021 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3022 * \retval #PSA_ERROR_BAD_STATE
3023 * The library has not been previously initialized by psa_crypto_init().
3024 * It is implementation-dependent whether a failure to initialize
3025 * results in this error code.
3026 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02003027PSA_CRYPTO_EXPORTED psa_status_t psa_asymmetric_encrypt(psa_key_id_t key, psa_algorithm_t alg,
3028 const uint8_t *input, size_t input_length,
3029 const uint8_t *salt, size_t salt_length,
3030 uint8_t *output, size_t output_size,
3031 size_t *output_length);
Julian Halla7e76c82021-04-14 11:12:11 +01003032
3033/**
3034 * \brief Decrypt a short message with a private key.
3035 *
3036 * \param key Identifier of the key to use for the operation.
3037 * It must be an asymmetric key pair. It must
3038 * allow the usage #PSA_KEY_USAGE_DECRYPT.
3039 * \param alg An asymmetric encryption algorithm that is
3040 * compatible with the type of \p key.
3041 * \param[in] input The message to decrypt.
3042 * \param input_length Size of the \p input buffer in bytes.
3043 * \param[in] salt A salt or label, if supported by the
3044 * encryption algorithm.
3045 * If the algorithm does not support a
3046 * salt, pass \c NULL.
3047 * If the algorithm supports an optional
3048 * salt and you do not want to pass a salt,
3049 * pass \c NULL.
3050 *
3051 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3052 * supported.
3053 * \param salt_length Size of the \p salt buffer in bytes.
3054 * If \p salt is \c NULL, pass 0.
3055 * \param[out] output Buffer where the decrypted message is to
3056 * be written.
3057 * \param output_size Size of the \c output buffer in bytes.
3058 * \param[out] output_length On success, the number of bytes
3059 * that make up the returned output.
3060 *
3061 * \retval #PSA_SUCCESS
3062 * \retval #PSA_ERROR_INVALID_HANDLE
3063 * \retval #PSA_ERROR_NOT_PERMITTED
3064 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3065 * The size of the \p output buffer is too small. You can
3066 * determine a sufficient buffer size by calling
3067 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
3068 * where \c key_type and \c key_bits are the type and bit-size
3069 * respectively of \p key.
3070 * \retval #PSA_ERROR_NOT_SUPPORTED
3071 * \retval #PSA_ERROR_INVALID_ARGUMENT
3072 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3073 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3074 * \retval #PSA_ERROR_HARDWARE_FAILURE
3075 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3076 * \retval #PSA_ERROR_STORAGE_FAILURE
3077 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3078 * \retval #PSA_ERROR_INVALID_PADDING
3079 * \retval #PSA_ERROR_BAD_STATE
3080 * The library has not been previously initialized by psa_crypto_init().
3081 * It is implementation-dependent whether a failure to initialize
3082 * results in this error code.
3083 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02003084PSA_CRYPTO_EXPORTED psa_status_t psa_asymmetric_decrypt(psa_key_id_t key, psa_algorithm_t alg,
3085 const uint8_t *input, size_t input_length,
3086 const uint8_t *salt, size_t salt_length,
3087 uint8_t *output, size_t output_size,
3088 size_t *output_length);
Julian Halla7e76c82021-04-14 11:12:11 +01003089
3090/**@}*/
3091
3092/** \defgroup key_derivation Key derivation and pseudorandom generation
3093 * @{
3094 */
3095
3096/** The type of the state data structure for key derivation operations.
3097 *
3098 * Before calling any function on a key derivation operation object, the
3099 * application must initialize it by any of the following means:
3100 * - Set the structure to all-bits-zero, for example:
3101 * \code
3102 * psa_key_derivation_operation_t operation;
3103 * memset(&operation, 0, sizeof(operation));
3104 * \endcode
3105 * - Initialize the structure to logical zero values, for example:
3106 * \code
3107 * psa_key_derivation_operation_t operation = {0};
3108 * \endcode
3109 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
3110 * for example:
3111 * \code
3112 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
3113 * \endcode
3114 * - Assign the result of the function psa_key_derivation_operation_init()
3115 * to the structure, for example:
3116 * \code
3117 * psa_key_derivation_operation_t operation;
3118 * operation = psa_key_derivation_operation_init();
3119 * \endcode
3120 *
3121 * This is an implementation-defined \c struct. Applications should not
3122 * make any assumptions about the content of this structure except
3123 * as directed by the documentation of a specific implementation.
3124 */
3125typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
3126
3127/** \def PSA_KEY_DERIVATION_OPERATION_INIT
3128 *
3129 * This macro returns a suitable initializer for a key derivation operation
3130 * object of type #psa_key_derivation_operation_t.
3131 */
3132#ifdef __DOXYGEN_ONLY__
3133/* This is an example definition for documentation purposes.
3134 * Implementations should define a suitable value in `crypto_struct.h`.
3135 */
3136#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
3137#endif
3138
3139/** Return an initial value for a key derivation operation object.
3140 */
3141static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
3142
3143/** Set up a key derivation operation.
3144 *
3145 * A key derivation algorithm takes some inputs and uses them to generate
3146 * a byte stream in a deterministic way.
3147 * This byte stream can be used to produce keys and other
3148 * cryptographic material.
3149 *
3150 * To derive a key:
3151 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3152 * -# Call psa_key_derivation_setup() to select the algorithm.
3153 * -# Provide the inputs for the key derivation by calling
3154 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3155 * as appropriate. Which inputs are needed, in what order, and whether
3156 * they may be keys and if so of what type depends on the algorithm.
3157 * -# Optionally set the operation's maximum capacity with
3158 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3159 * of or after providing inputs. For some algorithms, this step is mandatory
3160 * because the output depends on the maximum capacity.
3161 * -# To derive a key, call psa_key_derivation_output_key().
3162 * To derive a byte string for a different purpose, call
3163 * psa_key_derivation_output_bytes().
3164 * Successive calls to these functions use successive output bytes
3165 * calculated by the key derivation algorithm.
3166 * -# Clean up the key derivation operation object with
3167 * psa_key_derivation_abort().
3168 *
3169 * If this function returns an error, the key derivation operation object is
3170 * not changed.
3171 *
3172 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3173 * the operation will need to be reset by a call to psa_key_derivation_abort().
3174 *
3175 * Implementations must reject an attempt to derive a key of size 0.
3176 *
3177 * \param[in,out] operation The key derivation operation object
3178 * to set up. It must
3179 * have been initialized but not set up yet.
3180 * \param alg The key derivation algorithm to compute
3181 * (\c PSA_ALG_XXX value such that
3182 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3183 *
3184 * \retval #PSA_SUCCESS
3185 * Success.
3186 * \retval #PSA_ERROR_INVALID_ARGUMENT
3187 * \c alg is not a key derivation algorithm.
3188 * \retval #PSA_ERROR_NOT_SUPPORTED
3189 * \c alg is not supported or is not a key derivation algorithm.
3190 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3191 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3192 * \retval #PSA_ERROR_HARDWARE_FAILURE
3193 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3194 * \retval #PSA_ERROR_STORAGE_FAILURE
3195 * \retval #PSA_ERROR_BAD_STATE
3196 * The operation state is not valid (it must be inactive).
3197 * \retval #PSA_ERROR_BAD_STATE
3198 * The library has not been previously initialized by psa_crypto_init().
3199 * It is implementation-dependent whether a failure to initialize
3200 * results in this error code.
3201 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02003202PSA_CRYPTO_EXPORTED psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *operation,
3203 psa_algorithm_t alg);
Julian Halla7e76c82021-04-14 11:12:11 +01003204
3205/** Retrieve the current capacity of a key derivation operation.
3206 *
3207 * The capacity of a key derivation is the maximum number of bytes that it can
3208 * return. When you get *N* bytes of output from a key derivation operation,
3209 * this reduces its capacity by *N*.
3210 *
3211 * \param[in] operation The operation to query.
3212 * \param[out] capacity On success, the capacity of the operation.
3213 *
3214 * \retval #PSA_SUCCESS
3215 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3216 * \retval #PSA_ERROR_BAD_STATE
3217 * The operation state is not valid (it must be active).
3218 * \retval #PSA_ERROR_HARDWARE_FAILURE
3219 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3220 * \retval #PSA_ERROR_BAD_STATE
3221 * The library has not been previously initialized by psa_crypto_init().
3222 * It is implementation-dependent whether a failure to initialize
3223 * results in this error code.
3224 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02003225PSA_CRYPTO_EXPORTED psa_status_t
3226psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation, size_t *capacity);
Julian Halla7e76c82021-04-14 11:12:11 +01003227
3228/** Set the maximum capacity of a key derivation operation.
3229 *
3230 * The capacity of a key derivation operation is the maximum number of bytes
3231 * that the key derivation operation can return from this point onwards.
3232 *
3233 * \param[in,out] operation The key derivation operation object to modify.
3234 * \param capacity The new capacity of the operation.
3235 * It must be less or equal to the operation's
3236 * current capacity.
3237 *
3238 * \retval #PSA_SUCCESS
3239 * \retval #PSA_ERROR_INVALID_ARGUMENT
3240 * \p capacity is larger than the operation's current capacity.
3241 * In this case, the operation object remains valid and its capacity
3242 * remains unchanged.
3243 * \retval #PSA_ERROR_BAD_STATE
3244 * The operation state is not valid (it must be active).
3245 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3246 * \retval #PSA_ERROR_HARDWARE_FAILURE
3247 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3248 * \retval #PSA_ERROR_BAD_STATE
3249 * The library has not been previously initialized by psa_crypto_init().
3250 * It is implementation-dependent whether a failure to initialize
3251 * results in this error code.
3252 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02003253PSA_CRYPTO_EXPORTED psa_status_t
3254psa_key_derivation_set_capacity(psa_key_derivation_operation_t *operation, size_t capacity);
Julian Halla7e76c82021-04-14 11:12:11 +01003255
3256/** Use the maximum possible capacity for a key derivation operation.
3257 *
3258 * Use this value as the capacity argument when setting up a key derivation
3259 * to indicate that the operation should have the maximum possible capacity.
3260 * The value of the maximum possible capacity depends on the key derivation
3261 * algorithm.
3262 */
3263#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3264
3265/** Provide an input for key derivation or key agreement.
3266 *
3267 * Which inputs are required and in what order depends on the algorithm.
3268 * Refer to the documentation of each key derivation or key agreement
3269 * algorithm for information.
3270 *
3271 * This function passes direct inputs, which is usually correct for
3272 * non-secret inputs. To pass a secret input, which should be in a key
3273 * object, call psa_key_derivation_input_key() instead of this function.
3274 * Refer to the documentation of individual step types
3275 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3276 * for more information.
3277 *
3278 * If this function returns an error status, the operation enters an error
3279 * state and must be aborted by calling psa_key_derivation_abort().
3280 *
3281 * \param[in,out] operation The key derivation operation object to use.
3282 * It must have been set up with
3283 * psa_key_derivation_setup() and must not
3284 * have produced any output yet.
3285 * \param step Which step the input data is for.
3286 * \param[in] data Input data to use.
3287 * \param data_length Size of the \p data buffer in bytes.
3288 *
3289 * \retval #PSA_SUCCESS
3290 * Success.
3291 * \retval #PSA_ERROR_INVALID_ARGUMENT
3292 * \c step is not compatible with the operation's algorithm.
3293 * \retval #PSA_ERROR_INVALID_ARGUMENT
3294 * \c step does not allow direct inputs.
3295 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3296 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3297 * \retval #PSA_ERROR_HARDWARE_FAILURE
3298 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3299 * \retval #PSA_ERROR_STORAGE_FAILURE
3300 * \retval #PSA_ERROR_BAD_STATE
3301 * The operation state is not valid for this input \p step.
3302 * \retval #PSA_ERROR_BAD_STATE
3303 * The library has not been previously initialized by psa_crypto_init().
3304 * It is implementation-dependent whether a failure to initialize
3305 * results in this error code.
3306 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02003307PSA_CRYPTO_EXPORTED psa_status_t psa_key_derivation_input_bytes(
3308 psa_key_derivation_operation_t *operation, psa_key_derivation_step_t step,
3309 const uint8_t *data, size_t data_length);
Julian Halla7e76c82021-04-14 11:12:11 +01003310
3311/** Provide an input for key derivation in the form of a key.
3312 *
3313 * Which inputs are required and in what order depends on the algorithm.
3314 * Refer to the documentation of each key derivation or key agreement
3315 * algorithm for information.
3316 *
3317 * This function obtains input from a key object, which is usually correct for
3318 * secret inputs or for non-secret personalization strings kept in the key
3319 * store. To pass a non-secret parameter which is not in the key store,
3320 * call psa_key_derivation_input_bytes() instead of this function.
3321 * Refer to the documentation of individual step types
3322 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3323 * for more information.
3324 *
3325 * If this function returns an error status, the operation enters an error
3326 * state and must be aborted by calling psa_key_derivation_abort().
3327 *
3328 * \param[in,out] operation The key derivation operation object to use.
3329 * It must have been set up with
3330 * psa_key_derivation_setup() and must not
3331 * have produced any output yet.
3332 * \param step Which step the input data is for.
3333 * \param key Identifier of the key. It must have an
3334 * appropriate type for step and must allow the
3335 * usage #PSA_KEY_USAGE_DERIVE.
3336 *
3337 * \retval #PSA_SUCCESS
3338 * Success.
3339 * \retval #PSA_ERROR_INVALID_HANDLE
3340 * \retval #PSA_ERROR_NOT_PERMITTED
3341 * \retval #PSA_ERROR_INVALID_ARGUMENT
3342 * \c step is not compatible with the operation's algorithm.
3343 * \retval #PSA_ERROR_INVALID_ARGUMENT
3344 * \c step does not allow key inputs of the given type
3345 * or does not allow key inputs at all.
3346 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3347 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3348 * \retval #PSA_ERROR_HARDWARE_FAILURE
3349 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3350 * \retval #PSA_ERROR_STORAGE_FAILURE
3351 * \retval #PSA_ERROR_BAD_STATE
3352 * The operation state is not valid for this input \p step.
3353 * \retval #PSA_ERROR_BAD_STATE
3354 * The library has not been previously initialized by psa_crypto_init().
3355 * It is implementation-dependent whether a failure to initialize
3356 * results in this error code.
3357 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02003358PSA_CRYPTO_EXPORTED psa_status_t
3359psa_key_derivation_input_key(psa_key_derivation_operation_t *operation,
3360 psa_key_derivation_step_t step, psa_key_id_t key);
Julian Halla7e76c82021-04-14 11:12:11 +01003361
3362/** Perform a key agreement and use the shared secret as input to a key
3363 * derivation.
3364 *
3365 * A key agreement algorithm takes two inputs: a private key \p private_key
3366 * a public key \p peer_key.
3367 * The result of this function is passed as input to a key derivation.
3368 * The output of this key derivation can be extracted by reading from the
3369 * resulting operation to produce keys and other cryptographic material.
3370 *
3371 * If this function returns an error status, the operation enters an error
3372 * state and must be aborted by calling psa_key_derivation_abort().
3373 *
3374 * \param[in,out] operation The key derivation operation object to use.
3375 * It must have been set up with
3376 * psa_key_derivation_setup() with a
3377 * key agreement and derivation algorithm
3378 * \c alg (\c PSA_ALG_XXX value such that
3379 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3380 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3381 * is false).
3382 * The operation must be ready for an
3383 * input of the type given by \p step.
3384 * \param step Which step the input data is for.
3385 * \param private_key Identifier of the private key to use. It must
3386 * allow the usage #PSA_KEY_USAGE_DERIVE.
3387 * \param[in] peer_key Public key of the peer. The peer key must be in the
3388 * same format that psa_import_key() accepts for the
3389 * public key type corresponding to the type of
3390 * private_key. That is, this function performs the
3391 * equivalent of
3392 * #psa_import_key(...,
3393 * `peer_key`, `peer_key_length`) where
3394 * with key attributes indicating the public key
3395 * type corresponding to the type of `private_key`.
3396 * For example, for EC keys, this means that peer_key
3397 * is interpreted as a point on the curve that the
3398 * private key is on. The standard formats for public
3399 * keys are documented in the documentation of
3400 * psa_export_public_key().
3401 * \param peer_key_length Size of \p peer_key in bytes.
3402 *
3403 * \retval #PSA_SUCCESS
3404 * Success.
3405 * \retval #PSA_ERROR_BAD_STATE
3406 * The operation state is not valid for this key agreement \p step.
3407 * \retval #PSA_ERROR_INVALID_HANDLE
3408 * \retval #PSA_ERROR_NOT_PERMITTED
3409 * \retval #PSA_ERROR_INVALID_ARGUMENT
3410 * \c private_key is not compatible with \c alg,
3411 * or \p peer_key is not valid for \c alg or not compatible with
3412 * \c private_key.
3413 * \retval #PSA_ERROR_NOT_SUPPORTED
3414 * \c alg is not supported or is not a key derivation algorithm.
3415 * \retval #PSA_ERROR_INVALID_ARGUMENT
3416 * \c step does not allow an input resulting from a key agreement.
3417 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3418 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3419 * \retval #PSA_ERROR_HARDWARE_FAILURE
3420 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3421 * \retval #PSA_ERROR_STORAGE_FAILURE
3422 * \retval #PSA_ERROR_BAD_STATE
3423 * The library has not been previously initialized by psa_crypto_init().
3424 * It is implementation-dependent whether a failure to initialize
3425 * results in this error code.
3426 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02003427PSA_CRYPTO_EXPORTED psa_status_t psa_key_derivation_key_agreement(
3428 psa_key_derivation_operation_t *operation, psa_key_derivation_step_t step,
3429 psa_key_id_t private_key, const uint8_t *peer_key, size_t peer_key_length);
Julian Halla7e76c82021-04-14 11:12:11 +01003430
3431/** Read some data from a key derivation operation.
3432 *
3433 * This function calculates output bytes from a key derivation algorithm and
3434 * return those bytes.
3435 * If you view the key derivation's output as a stream of bytes, this
3436 * function destructively reads the requested number of bytes from the
3437 * stream.
3438 * The operation's capacity decreases by the number of bytes read.
3439 *
3440 * If this function returns an error status other than
3441 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
3442 * state and must be aborted by calling psa_key_derivation_abort().
3443 *
3444 * \param[in,out] operation The key derivation operation object to read from.
3445 * \param[out] output Buffer where the output will be written.
3446 * \param output_length Number of bytes to output.
3447 *
3448 * \retval #PSA_SUCCESS
3449 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3450 * The operation's capacity was less than
3451 * \p output_length bytes. Note that in this case,
3452 * no output is written to the output buffer.
3453 * The operation's capacity is set to 0, thus
3454 * subsequent calls to this function will not
3455 * succeed, even with a smaller output buffer.
3456 * \retval #PSA_ERROR_BAD_STATE
3457 * The operation state is not valid (it must be active and completed
3458 * all required input steps).
3459 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3460 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3461 * \retval #PSA_ERROR_HARDWARE_FAILURE
3462 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3463 * \retval #PSA_ERROR_STORAGE_FAILURE
3464 * \retval #PSA_ERROR_BAD_STATE
3465 * The library has not been previously initialized by psa_crypto_init().
3466 * It is implementation-dependent whether a failure to initialize
3467 * results in this error code.
3468 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02003469PSA_CRYPTO_EXPORTED psa_status_t psa_key_derivation_output_bytes(
3470 psa_key_derivation_operation_t *operation, uint8_t *output, size_t output_length);
Julian Halla7e76c82021-04-14 11:12:11 +01003471
3472/** Derive a key from an ongoing key derivation operation.
3473 *
3474 * This function calculates output bytes from a key derivation algorithm
3475 * and uses those bytes to generate a key deterministically.
3476 * The key's location, usage policy, type and size are taken from
3477 * \p attributes.
3478 *
3479 * If you view the key derivation's output as a stream of bytes, this
3480 * function destructively reads as many bytes as required from the
3481 * stream.
3482 * The operation's capacity decreases by the number of bytes read.
3483 *
3484 * If this function returns an error status other than
3485 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
3486 * state and must be aborted by calling psa_key_derivation_abort().
3487 *
3488 * How much output is produced and consumed from the operation, and how
3489 * the key is derived, depends on the key type:
3490 *
3491 * - For key types for which the key is an arbitrary sequence of bytes
3492 * of a given size, this function is functionally equivalent to
3493 * calling #psa_key_derivation_output_bytes
3494 * and passing the resulting output to #psa_import_key.
3495 * However, this function has a security benefit:
3496 * if the implementation provides an isolation boundary then
3497 * the key material is not exposed outside the isolation boundary.
3498 * As a consequence, for these key types, this function always consumes
3499 * exactly (\p bits / 8) bytes from the operation.
3500 * The following key types defined in this specification follow this scheme:
3501 *
3502 * - #PSA_KEY_TYPE_AES;
3503 * - #PSA_KEY_TYPE_ARC4;
3504 * - #PSA_KEY_TYPE_CAMELLIA;
3505 * - #PSA_KEY_TYPE_DERIVE;
3506 * - #PSA_KEY_TYPE_HMAC.
3507 *
3508 * - For ECC keys on a Montgomery elliptic curve
3509 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
3510 * Montgomery curve), this function always draws a byte string whose
3511 * length is determined by the curve, and sets the mandatory bits
3512 * accordingly. That is:
3513 *
3514 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
3515 * string and process it as specified in RFC 7748 &sect;5.
3516 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
3517 * string and process it as specified in RFC 7748 &sect;5.
3518 *
3519 * - For key types for which the key is represented by a single sequence of
3520 * \p bits bits with constraints as to which bit sequences are acceptable,
3521 * this function draws a byte string of length (\p bits / 8) bytes rounded
3522 * up to the nearest whole number of bytes. If the resulting byte string
3523 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3524 * This process is repeated until an acceptable byte string is drawn.
3525 * The byte string drawn from the operation is interpreted as specified
3526 * for the output produced by psa_export_key().
3527 * The following key types defined in this specification follow this scheme:
3528 *
3529 * - #PSA_KEY_TYPE_DES.
3530 * Force-set the parity bits, but discard forbidden weak keys.
3531 * For 2-key and 3-key triple-DES, the three keys are generated
3532 * successively (for example, for 3-key triple-DES,
3533 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3534 * discard the first 8 bytes, use the next 8 bytes as the first key,
3535 * and continue reading output from the operation to derive the other
3536 * two keys).
3537 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
3538 * where \c group designates any Diffie-Hellman group) and
3539 * ECC keys on a Weierstrass elliptic curve
3540 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
3541 * Weierstrass curve).
3542 * For these key types, interpret the byte string as integer
3543 * in big-endian order. Discard it if it is not in the range
3544 * [0, *N* - 2] where *N* is the boundary of the private key domain
3545 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
3546 * or the order of the curve's base point for ECC).
3547 * Add 1 to the resulting integer and use this as the private key *x*.
3548 * This method allows compliance to NIST standards, specifically
3549 * the methods titled "key-pair generation by testing candidates"
3550 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3551 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3552 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3553 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
3554 *
3555 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
3556 * the way in which the operation output is consumed is
3557 * implementation-defined.
3558 *
3559 * In all cases, the data that is read is discarded from the operation.
3560 * The operation's capacity is decreased by the number of bytes read.
3561 *
3562 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3563 * the input to that step must be provided with psa_key_derivation_input_key().
3564 * Future versions of this specification may include additional restrictions
3565 * on the derived key based on the attributes and strength of the secret key.
3566 *
3567 * \param[in] attributes The attributes for the new key.
3568 * \param[in,out] operation The key derivation operation object to read from.
3569 * \param[out] key On success, an identifier for the newly created
3570 * key. For persistent keys, this is the key
3571 * identifier defined in \p attributes.
3572 * \c 0 on failure.
3573 *
3574 * \retval #PSA_SUCCESS
3575 * Success.
3576 * If the key is persistent, the key material and the key's metadata
3577 * have been saved to persistent storage.
3578 * \retval #PSA_ERROR_ALREADY_EXISTS
3579 * This is an attempt to create a persistent key, and there is
3580 * already a persistent key with the given identifier.
3581 * \retval #PSA_ERROR_INSUFFICIENT_DATA
3582 * There was not enough data to create the desired key.
3583 * Note that in this case, no output is written to the output buffer.
3584 * The operation's capacity is set to 0, thus subsequent calls to
3585 * this function will not succeed, even with a smaller output buffer.
3586 * \retval #PSA_ERROR_NOT_SUPPORTED
3587 * The key type or key size is not supported, either by the
3588 * implementation in general or in this particular location.
3589 * \retval #PSA_ERROR_INVALID_ARGUMENT
3590 * The provided key attributes are not valid for the operation.
3591 * \retval #PSA_ERROR_NOT_PERMITTED
3592 * The #PSA_KEY_DERIVATION_INPUT_SECRET input was not provided through
3593 * a key.
3594 * \retval #PSA_ERROR_BAD_STATE
3595 * The operation state is not valid (it must be active and completed
3596 * all required input steps).
3597 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3598 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3599 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3600 * \retval #PSA_ERROR_HARDWARE_FAILURE
3601 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3602 * \retval #PSA_ERROR_DATA_INVALID
3603 * \retval #PSA_ERROR_DATA_CORRUPT
3604 * \retval #PSA_ERROR_STORAGE_FAILURE
3605 * \retval #PSA_ERROR_BAD_STATE
3606 * The library has not been previously initialized by psa_crypto_init().
3607 * It is implementation-dependent whether a failure to initialize
3608 * results in this error code.
3609 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02003610PSA_CRYPTO_EXPORTED psa_status_t
3611psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
3612 psa_key_derivation_operation_t *operation, psa_key_id_t *key);
Julian Halla7e76c82021-04-14 11:12:11 +01003613
3614/** Abort a key derivation operation.
3615 *
3616 * Aborting an operation frees all associated resources except for the \c
3617 * operation structure itself. Once aborted, the operation object can be reused
3618 * for another operation by calling psa_key_derivation_setup() again.
3619 *
3620 * This function may be called at any time after the operation
3621 * object has been initialized as described in #psa_key_derivation_operation_t.
3622 *
3623 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3624 * call psa_key_derivation_abort() on an operation that has not been set up.
3625 *
3626 * \param[in,out] operation The operation to abort.
3627 *
3628 * \retval #PSA_SUCCESS
3629 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3630 * \retval #PSA_ERROR_HARDWARE_FAILURE
3631 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3632 * \retval #PSA_ERROR_BAD_STATE
3633 * The library has not been previously initialized by psa_crypto_init().
3634 * It is implementation-dependent whether a failure to initialize
3635 * results in this error code.
3636 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02003637PSA_CRYPTO_EXPORTED psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *operation);
Julian Halla7e76c82021-04-14 11:12:11 +01003638
3639/** Perform a key agreement and return the raw shared secret.
3640 *
3641 * \warning The raw result of a key agreement algorithm such as finite-field
3642 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3643 * not be used directly as key material. It should instead be passed as
3644 * input to a key derivation algorithm. To chain a key agreement with
3645 * a key derivation, use psa_key_derivation_key_agreement() and other
3646 * functions from the key derivation interface.
3647 *
3648 * \param alg The key agreement algorithm to compute
3649 * (\c PSA_ALG_XXX value such that
3650 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3651 * is true).
3652 * \param private_key Identifier of the private key to use. It must
3653 * allow the usage #PSA_KEY_USAGE_DERIVE.
3654 * \param[in] peer_key Public key of the peer. It must be
3655 * in the same format that psa_import_key()
3656 * accepts. The standard formats for public
3657 * keys are documented in the documentation
3658 * of psa_export_public_key().
3659 * \param peer_key_length Size of \p peer_key in bytes.
3660 * \param[out] output Buffer where the decrypted message is to
3661 * be written.
3662 * \param output_size Size of the \c output buffer in bytes.
3663 * \param[out] output_length On success, the number of bytes
3664 * that make up the returned output.
3665 *
3666 * \retval #PSA_SUCCESS
3667 * Success.
3668 * \retval #PSA_ERROR_INVALID_HANDLE
3669 * \retval #PSA_ERROR_NOT_PERMITTED
3670 * \retval #PSA_ERROR_INVALID_ARGUMENT
3671 * \p alg is not a key agreement algorithm
3672 * \retval #PSA_ERROR_INVALID_ARGUMENT
3673 * \p private_key is not compatible with \p alg,
3674 * or \p peer_key is not valid for \p alg or not compatible with
3675 * \p private_key.
3676 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3677 * \p output_size is too small
3678 * \retval #PSA_ERROR_NOT_SUPPORTED
3679 * \p alg is not a supported key agreement algorithm.
3680 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3681 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3682 * \retval #PSA_ERROR_HARDWARE_FAILURE
3683 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3684 * \retval #PSA_ERROR_STORAGE_FAILURE
3685 * \retval #PSA_ERROR_BAD_STATE
3686 * The library has not been previously initialized by psa_crypto_init().
3687 * It is implementation-dependent whether a failure to initialize
3688 * results in this error code.
3689 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02003690PSA_CRYPTO_EXPORTED psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3691 psa_key_id_t private_key,
3692 const uint8_t *peer_key,
3693 size_t peer_key_length, uint8_t *output,
3694 size_t output_size, size_t *output_length);
Julian Halla7e76c82021-04-14 11:12:11 +01003695
3696/**@}*/
3697
3698/** \defgroup random Random generation
3699 * @{
3700 */
3701
3702/**
3703 * \brief Generate random bytes.
3704 *
3705 * \warning This function **can** fail! Callers MUST check the return status
3706 * and MUST NOT use the content of the output buffer if the return
3707 * status is not #PSA_SUCCESS.
3708 *
3709 * \note To generate a key, use psa_generate_key() instead.
3710 *
3711 * \param[out] output Output buffer for the generated data.
3712 * \param output_size Number of bytes to generate and output.
3713 *
3714 * \retval #PSA_SUCCESS
3715 * \retval #PSA_ERROR_NOT_SUPPORTED
3716 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3717 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3718 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3719 * \retval #PSA_ERROR_HARDWARE_FAILURE
3720 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3721 * \retval #PSA_ERROR_BAD_STATE
3722 * The library has not been previously initialized by psa_crypto_init().
3723 * It is implementation-dependent whether a failure to initialize
3724 * results in this error code.
3725 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02003726PSA_CRYPTO_EXPORTED psa_status_t psa_generate_random(uint8_t *output, size_t output_size);
Julian Halla7e76c82021-04-14 11:12:11 +01003727
3728/**
3729 * \brief Generate a key or key pair.
3730 *
3731 * The key is generated randomly.
3732 * Its location, usage policy, type and size are taken from \p attributes.
3733 *
3734 * Implementations must reject an attempt to generate a key of size 0.
3735 *
3736 * The following type-specific considerations apply:
3737 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
3738 * the public exponent is 65537.
3739 * The modulus is a product of two probabilistic primes
3740 * between 2^{n-1} and 2^n where n is the bit size specified in the
3741 * attributes.
3742 *
3743 * \param[in] attributes The attributes for the new key.
3744 * \param[out] key On success, an identifier for the newly created
3745 * key. For persistent keys, this is the key
3746 * identifier defined in \p attributes.
3747 * \c 0 on failure.
3748 *
3749 * \retval #PSA_SUCCESS
3750 * Success.
3751 * If the key is persistent, the key material and the key's metadata
3752 * have been saved to persistent storage.
3753 * \retval #PSA_ERROR_ALREADY_EXISTS
3754 * This is an attempt to create a persistent key, and there is
3755 * already a persistent key with the given identifier.
3756 * \retval #PSA_ERROR_NOT_SUPPORTED
3757 * \retval #PSA_ERROR_INVALID_ARGUMENT
3758 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3759 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3760 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3761 * \retval #PSA_ERROR_HARDWARE_FAILURE
3762 * \retval #PSA_ERROR_CORRUPTION_DETECTED
3763 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3764 * \retval #PSA_ERROR_DATA_INVALID
3765 * \retval #PSA_ERROR_DATA_CORRUPT
3766 * \retval #PSA_ERROR_STORAGE_FAILURE
3767 * \retval #PSA_ERROR_BAD_STATE
3768 * The library has not been previously initialized by psa_crypto_init().
3769 * It is implementation-dependent whether a failure to initialize
3770 * results in this error code.
3771 */
Gabor Toth99e4ec52023-07-18 08:24:20 +02003772PSA_CRYPTO_EXPORTED psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
3773 psa_key_id_t *key);
Julian Halla7e76c82021-04-14 11:12:11 +01003774
3775/**@}*/
3776
3777#ifdef __cplusplus
3778}
3779#endif
3780
3781/* The file "crypto_sizes.h" contains definitions for size calculation
3782 * macros whose definitions are implementation-specific. */
3783#include "psa/crypto_sizes.h"
3784
3785/* The file "crypto_client_struct.h" contains definitions for structures
3786 * whose definitions differ in the client view and the PSA server
3787 * implementation in TF-M. */
3788#include "psa/crypto_client_struct.h"
3789
Julian Halla7e76c82021-04-14 11:12:11 +01003790/* The file "crypto_struct.h" contains definitions for
3791 * implementation-specific structs that are declared above. */
3792#include "psa/crypto_struct.h"
3793
3794/* The file "crypto_extra.h" contains vendor-specific definitions. This
3795 * can include vendor-defined algorithms, extra functions, etc. */
3796#include "psa/crypto_extra.h"
3797
3798#endif /* PSA_CRYPTO_H */