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