blob: 6d3132283400dcf173354ff98e8683a7658b7130 [file] [log] [blame]
Jaeden Ameroe54e6932018-08-06 16:19:58 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
5/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
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#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
27#include <stddef.h>
28
29#ifdef __DOXYGEN_ONLY__
30/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
35/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
39/** \brief Key slot number.
40 *
41 * This type represents key slots. It must be an unsigned integral
42 * type. The choice of type is implementation-dependent.
43 * 0 is not a valid key slot number. The meaning of other values is
44 * implementation dependent.
45 *
46 * At any given point in time, each key slot either contains a
47 * cryptographic object, or is empty. Key slots are persistent:
48 * once set, the cryptographic object remains in the key slot until
49 * explicitly destroyed.
50 */
51typedef _unsigned_integral_type_ psa_key_slot_t;
52
53/**@}*/
54#endif /* __DOXYGEN_ONLY__ */
55
56#ifdef __cplusplus
57extern "C" {
58#endif
59
60/** \defgroup basic Basic definitions
61 * @{
62 */
63
64#if defined(PSA_SUCCESS)
65/* If PSA_SUCCESS is defined, assume that PSA crypto is being used
66 * together with PSA IPC, which also defines the identifier
67 * PSA_SUCCESS. We must not define PSA_SUCCESS ourselves in that case;
68 * the other error code names don't clash. Also define psa_status_t as
69 * an alias for the type used by PSA IPC. This is a temporary hack
70 * until we unify error reporting in PSA IPC and PSA crypto.
71 *
72 * Note that psa_defs.h must be included before this header!
73 */
74typedef psa_error_t psa_status_t;
75
76#else /* defined(PSA_SUCCESS) */
77
78/**
79 * \brief Function return status.
80 *
81 * This is either #PSA_SUCCESS (which is zero), indicating success,
82 * or a nonzero value indicating that an error occurred. Errors are
83 * encoded as one of the \c PSA_ERROR_xxx values defined here.
84 */
85typedef int32_t psa_status_t;
86
87/** The action was completed successfully. */
88#define PSA_SUCCESS ((psa_status_t)0)
89
90#endif /* !defined(PSA_SUCCESS) */
91
92/** An error occurred that does not correspond to any defined
93 * failure cause.
94 *
95 * Implementations may use this error code if none of the other standard
96 * error codes are applicable. */
97#define PSA_ERROR_UNKNOWN_ERROR ((psa_status_t)1)
98
99/** The requested operation or a parameter is not supported
100 * by this implementation.
101 *
102 * Implementations should return this error code when an enumeration
103 * parameter such as a key type, algorithm, etc. is not recognized.
104 * If a combination of parameters is recognized and identified as
105 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
106#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)2)
107
108/** The requested action is denied by a policy.
109 *
110 * Implementations should return this error code when the parameters
111 * are recognized as valid and supported, and a policy explicitly
112 * denies the requested operation.
113 *
114 * If a subset of the parameters of a function call identify a
115 * forbidden operation, and another subset of the parameters are
116 * not valid or not supported, it is unspecified whether the function
117 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
118 * #PSA_ERROR_INVALID_ARGUMENT. */
119#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)3)
120
121/** An output buffer is too small.
122 *
123 * Applications can call the \c PSA_xxx_SIZE macro listed in the function
124 * description to determine a sufficient buffer size.
125 *
126 * Implementations should preferably return this error code only
127 * in cases when performing the operation with a larger output
128 * buffer would succeed. However implementations may return this
129 * error if a function has invalid or unsupported parameters in addition
130 * to the parameters that determine the necessary output buffer size. */
131#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)4)
132
133/** A slot is occupied, but must be empty to carry out the
134 * requested action.
135 *
136 * If the slot number is invalid (i.e. the requested action could
137 * not be performed even after erasing the slot's content),
138 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
139#define PSA_ERROR_OCCUPIED_SLOT ((psa_status_t)5)
140
141/** A slot is empty, but must be occupied to carry out the
142 * requested action.
143 *
144 * If the slot number is invalid (i.e. the requested action could
145 * not be performed even after creating appropriate content in the slot),
146 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
147#define PSA_ERROR_EMPTY_SLOT ((psa_status_t)6)
148
149/** The requested action cannot be performed in the current state.
150 *
151 * Multipart operations return this error when one of the
152 * functions is called out of sequence. Refer to the function
153 * descriptions for permitted sequencing of functions.
154 *
155 * Implementations shall not return this error code to indicate
156 * that a key slot is occupied when it needs to be free or vice versa,
157 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
158 * as applicable. */
159#define PSA_ERROR_BAD_STATE ((psa_status_t)7)
160
161/** The parameters passed to the function are invalid.
162 *
163 * Implementations may return this error any time a parameter or
164 * combination of parameters are recognized as invalid.
165 *
166 * Implementations shall not return this error code to indicate
167 * that a key slot is occupied when it needs to be free or vice versa,
168 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
169 * as applicable. */
170#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)8)
171
172/** There is not enough runtime memory.
173 *
174 * If the action is carried out across multiple security realms, this
175 * error can refer to available memory in any of the security realms. */
176#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)9)
177
178/** There is not enough persistent storage.
179 *
180 * Functions that modify the key storage return this error code if
181 * there is insufficient storage space on the host media. In addition,
182 * many functions that do not otherwise access storage may return this
183 * error code if the implementation requires a mandatory log entry for
184 * the requested action and the log storage space is full. */
185#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)10)
186
187/** There was a communication failure inside the implementation.
188 *
189 * This can indicate a communication failure between the application
190 * and an external cryptoprocessor or between the cryptoprocessor and
191 * an external volatile or persistent memory. A communication failure
192 * may be transient or permanent depending on the cause.
193 *
194 * \warning If a function returns this error, it is undetermined
195 * whether the requested action has completed or not. Implementations
196 * should return #PSA_SUCCESS on successful completion whenver
197 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
198 * if the requested action was completed successfully in an external
199 * cryptoprocessor but there was a breakdown of communication before
200 * the cryptoprocessor could report the status to the application.
201 */
202#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)11)
203
204/** There was a storage failure that may have led to data loss.
205 *
206 * This error indicates that some persistent storage is corrupted.
207 * It should not be used for a corruption of volatile memory
208 * (use #PSA_ERROR_TAMPERING_DETECTED), for a communication error
209 * between the cryptoprocessor and its external storage (use
210 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
211 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
212 *
213 * Note that a storage failure does not indicate that any data that was
214 * previously read is invalid. However this previously read data may no
215 * longer be readable from storage.
216 *
217 * When a storage failure occurs, it is no longer possible to ensure
218 * the global integrity of the keystore. Depending on the global
219 * integrity guarantees offered by the implementation, access to other
220 * data may or may not fail even if the data is still readable but
221 * its integrity canont be guaranteed.
222 *
223 * Implementations should only use this error code to report a
224 * permanent storage corruption. However application writers should
225 * keep in mind that transient errors while reading the storage may be
226 * reported using this error code. */
227#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)12)
228
229/** A hardware failure was detected.
230 *
231 * A hardware failure may be transient or permanent depending on the
232 * cause. */
233#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)13)
234
235/** A tampering attempt was detected.
236 *
237 * If an application receives this error code, there is no guarantee
238 * that previously accessed or computed data was correct and remains
239 * confidential. Applications should not perform any security function
240 * and should enter a safe failure state.
241 *
242 * Implementations may return this error code if they detect an invalid
243 * state that cannot happen during normal operation and that indicates
244 * that the implementation's security guarantees no longer hold. Depending
245 * on the implementation architecture and on its security and safety goals,
246 * the implementation may forcibly terminate the application.
247 *
248 * This error code is intended as a last resort when a security breach
249 * is detected and it is unsure whether the keystore data is still
250 * protected. Implementations shall only return this error code
251 * to report an alarm from a tampering detector, to indicate that
252 * the confidentiality of stored data can no longer be guaranteed,
253 * or to indicate that the integrity of previously returned data is now
254 * considered compromised. Implementations shall not use this error code
255 * to indicate a hardware failure that merely makes it impossible to
256 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
257 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
258 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
259 * instead).
260 *
261 * This error indicates an attack against the application. Implementations
262 * shall not return this error code as a consequence of the behavior of
263 * the application itself. */
264#define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)14)
265
266/** There is not enough entropy to generate random data needed
267 * for the requested action.
268 *
269 * This error indicates a failure of a hardware random generator.
270 * Application writers should note that this error can be returned not
271 * only by functions whose purpose is to generate random data, such
272 * as key, IV or nonce generation, but also by functions that execute
273 * an algorithm with a randomized result, as well as functions that
274 * use randomization of intermediate computations as a countermeasure
275 * to certain attacks.
276 *
277 * Implementations should avoid returning this error after psa_crypto_init()
278 * has succeeded. Implementations should generate sufficient
279 * entropy during initialization and subsequently use a cryptographically
280 * secure pseudorandom generator (PRNG). However implementations may return
281 * this error at any time if a policy requires the PRNG to be reseeded
282 * during normal operation. */
283#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)15)
284
285/** The signature, MAC or hash is incorrect.
286 *
287 * Verification functions return this error if the verification
288 * calculations completed successfully, and the value to be verified
289 * was determined to be incorrect.
290 *
291 * If the value to verify has an invalid size, implementations may return
292 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
293#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)16)
294
295/** The decrypted padding is incorrect.
296 *
297 * \warning In some protocols, when decrypting data, it is essential that
298 * the behavior of the application does not depend on whether the padding
299 * is correct, down to precise timing. Applications should prefer
300 * protocols that use authenticated encryption rather than plain
301 * encryption. If the application must perform a decryption of
302 * unauthenticated data, the application writer should take care not
303 * to reveal whether the padding is invalid.
304 *
305 * Implementations should strive to make valid and invalid padding
306 * as close as possible to indistinguishable to an external observer.
307 * In particular, the timing of a decryption operation should not
308 * depend on the validity of the padding. */
309#define PSA_ERROR_INVALID_PADDING ((psa_status_t)17)
310
311/** The generator has insufficient capacity left.
312 *
313 * Once a function returns this error, attempts to read from the
314 * generator will always return this error. */
315#define PSA_ERROR_INSUFFICIENT_CAPACITY ((psa_status_t)18)
316
317/**
318 * \brief Library initialization.
319 *
320 * Applications must call this function before calling any other
321 * function in this module.
322 *
323 * Applications may call this function more than once. Once a call
324 * succeeds, subsequent calls are guaranteed to succeed.
325 *
326 * \retval #PSA_SUCCESS
327 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
328 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
329 * \retval #PSA_ERROR_HARDWARE_FAILURE
330 * \retval #PSA_ERROR_TAMPERING_DETECTED
331 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
332 */
333psa_status_t psa_crypto_init(void);
334
335#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
336#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
337
338/**@}*/
339
340/** \defgroup crypto_types Key and algorithm types
341 * @{
342 */
343
344/** \brief Encoding of a key type.
345 */
346typedef uint32_t psa_key_type_t;
347
348/** An invalid key type value.
349 *
350 * Zero is not the encoding of any key type.
351 */
352#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
353
354/** Vendor-defined flag
355 *
356 * Key types defined by this standard will never have the
357 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
358 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
359 * respect the bitwise structure used by standard encodings whenever practical.
360 */
361#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
362
363#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7e000000)
364
365/** Raw data.
366 *
367 * A "key" of this type cannot be used for any cryptographic operation.
368 * Applications may use this type to store arbitrary data in the keystore. */
369#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x02000000)
370
371#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x04000000)
372#define PSA_KEY_TYPE_CATEGORY_ASYMMETRIC ((psa_key_type_t)0x06000000)
373#define PSA_KEY_TYPE_PAIR_FLAG ((psa_key_type_t)0x01000000)
374
375/** HMAC key.
376 *
377 * The key policy determines which underlying hash algorithm the key can be
378 * used for.
379 *
380 * HMAC keys should generally have the same size as the underlying hash.
381 * This size can be calculated with #PSA_HASH_SIZE(\c alg) where
382 * \c alg is the HMAC algorithm or the underlying hash algorithm. */
383#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x02000001)
384
385/** A secret for key derivation.
386 *
387 * The key policy determines which key derivation algorithm the key
388 * can be used for.
389 */
390#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x02000101)
391
392/** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher.
393 *
394 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
395 * 32 bytes (AES-256).
396 */
397#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x04000001)
398
399/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
400 *
401 * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or
402 * 24 bytes (3-key 3DES).
403 *
404 * Note that single DES and 2-key 3DES are weak and strongly
405 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
406 * is weak and deprecated and should only be used in legacy protocols.
407 */
408#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x04000002)
409
410/** Key for an cipher, AEAD or MAC algorithm based on the
411 * Camellia block cipher. */
412#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x04000003)
413
414/** Key for the RC4 stream cipher.
415 *
416 * Note that RC4 is weak and deprecated and should only be used in
417 * legacy protocols. */
418#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x04000004)
419
420/** RSA public key. */
421#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x06010000)
422/** RSA key pair (private and public key). */
423#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x07010000)
424
425/** DSA public key. */
426#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x06020000)
427/** DSA key pair (private and public key). */
428#define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x07020000)
429
430#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x06030000)
431#define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x07030000)
432#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
433/** Elliptic curve key pair. */
434#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
435 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
436/** Elliptic curve public key. */
437#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
438 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
439
440/** Whether a key type is vendor-defined. */
441#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
442 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
443
444/** Whether a key type is asymmetric: either a key pair or a public key. */
445#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
446 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
447/** Whether a key type is the public part of a key pair. */
448#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
449 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
450 PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
451/** Whether a key type is a key pair containing a private part and a public
452 * part. */
453#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
454 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
455 (PSA_KEY_TYPE_CATEGORY_ASYMMETRIC | PSA_KEY_TYPE_PAIR_FLAG))
456/** The key pair type corresponding to a public key type. */
457#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
458 ((type) | PSA_KEY_TYPE_PAIR_FLAG)
459/** The public key type corresponding to a key pair type. */
460#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
461 ((type) & ~PSA_KEY_TYPE_PAIR_FLAG)
462/** Whether a key type is an RSA key (pair or public-only). */
463#define PSA_KEY_TYPE_IS_RSA(type) \
464 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
465
466/** Whether a key type is an elliptic curve key (pair or public-only). */
467#define PSA_KEY_TYPE_IS_ECC(type) \
468 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
469 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
470#define PSA_KEY_TYPE_IS_ECC_KEYPAIR(type) \
471 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
472 PSA_KEY_TYPE_ECC_KEYPAIR_BASE)
473#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
474 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
475 PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
476
477/** The type of PSA elliptic curve identifiers. */
478typedef uint16_t psa_ecc_curve_t;
479/** Extract the curve from an elliptic curve key type. */
480#define PSA_KEY_TYPE_GET_CURVE(type) \
481 ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
482 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
483 0))
484
485/* The encoding of curve identifiers is currently aligned with the
486 * TLS Supported Groups Registry (formerly known as the
487 * TLS EC Named Curve Registry)
488 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
489 * The values are defined by RFC 4492, RFC 7027 and RFC 7919. */
490#define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001)
491#define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002)
492#define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003)
493#define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004)
494#define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005)
495#define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006)
496#define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007)
497#define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008)
498#define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009)
499#define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a)
500#define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b)
501#define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c)
502#define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d)
503#define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e)
504#define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f)
505#define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010)
506#define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011)
507#define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012)
508#define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013)
509#define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014)
510#define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015)
511#define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016)
512#define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017)
513#define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018)
514#define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019)
515#define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a)
516#define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b)
517#define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c)
518#define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d)
519#define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e)
520#define PSA_ECC_CURVE_FFDHE_2048 ((psa_ecc_curve_t) 0x0100)
521#define PSA_ECC_CURVE_FFDHE_3072 ((psa_ecc_curve_t) 0x0101)
522#define PSA_ECC_CURVE_FFDHE_4096 ((psa_ecc_curve_t) 0x0102)
523#define PSA_ECC_CURVE_FFDHE_6144 ((psa_ecc_curve_t) 0x0103)
524#define PSA_ECC_CURVE_FFDHE_8192 ((psa_ecc_curve_t) 0x0104)
525
526/** The block size of a block cipher.
527 *
528 * \param type A cipher key type (value of type #psa_key_type_t).
529 *
530 * \return The block size for a block cipher, or 1 for a stream cipher.
531 * The return value is undefined if \p type is not a supported
532 * cipher key type.
533 *
534 * \note It is possible to build stream cipher algorithms on top of a block
535 * cipher, for example CTR mode (#PSA_ALG_CTR).
536 * This macro only takes the key type into account, so it cannot be
537 * used to determine the size of the data that #psa_cipher_update()
538 * might buffer for future processing in general.
539 *
540 * \note This macro returns a compile-time constant if its argument is one.
541 *
542 * \warning This macro may evaluate its argument multiple times.
543 */
544#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
545 ( \
546 (type) == PSA_KEY_TYPE_AES ? 16 : \
547 (type) == PSA_KEY_TYPE_DES ? 8 : \
548 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
549 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
550 0)
551
552/** \brief Encoding of a cryptographic algorithm.
553 *
554 * For algorithms that can be applied to multiple key types, this type
555 * does not encode the key type. For example, for symmetric ciphers
556 * based on a block cipher, #psa_algorithm_t encodes the block cipher
557 * mode and the padding mode while the block cipher itself is encoded
558 * via #psa_key_type_t.
559 */
560typedef uint32_t psa_algorithm_t;
561
562#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
563#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
564#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
565#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
566#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
567#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
568#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
569#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
570#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
571#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
572
573#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
574 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
575
576/** Whether the specified algorithm is a hash algorithm.
577 *
578 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
579 *
580 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
581 * This macro may return either 0 or 1 if \p alg is not a supported
582 * algorithm identifier.
583 */
584#define PSA_ALG_IS_HASH(alg) \
585 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
586
587/** Whether the specified algorithm is a MAC algorithm.
588 *
589 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
590 *
591 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
592 * This macro may return either 0 or 1 if \p alg is not a supported
593 * algorithm identifier.
594 */
595#define PSA_ALG_IS_MAC(alg) \
596 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
597
598/** Whether the specified algorithm is a symmetric cipher algorithm.
599 *
600 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
601 *
602 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
603 * This macro may return either 0 or 1 if \p alg is not a supported
604 * algorithm identifier.
605 */
606#define PSA_ALG_IS_CIPHER(alg) \
607 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
608
609/** Whether the specified algorithm is an authenticated encryption
610 * with associated data (AEAD) algorithm.
611 *
612 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
613 *
614 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
615 * This macro may return either 0 or 1 if \p alg is not a supported
616 * algorithm identifier.
617 */
618#define PSA_ALG_IS_AEAD(alg) \
619 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
620
621/** Whether the specified algorithm is a public-key signature algorithm.
622 *
623 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
624 *
625 * \return 1 if \p alg is a public-key signature algorithm, 0 otherwise.
626 * This macro may return either 0 or 1 if \p alg is not a supported
627 * algorithm identifier.
628 */
629#define PSA_ALG_IS_SIGN(alg) \
630 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
631
632/** Whether the specified algorithm is a public-key encryption algorithm.
633 *
634 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
635 *
636 * \return 1 if \p alg is a public-key encryption algorithm, 0 otherwise.
637 * This macro may return either 0 or 1 if \p alg is not a supported
638 * algorithm identifier.
639 */
640#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
641 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
642
643/** Whether the specified algorithm is a key agreement algorithm.
644 *
645 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
646 *
647 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
648 * This macro may return either 0 or 1 if \p alg is not a supported
649 * algorithm identifier.
650 */
651#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
652 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
653
654/** Whether the specified algorithm is a key derivation algorithm.
655 *
656 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
657 *
658 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
659 * This macro may return either 0 or 1 if \p alg is not a supported
660 * algorithm identifier.
661 */
662#define PSA_ALG_IS_KEY_DERIVATION(alg) \
663 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
664
665#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
666#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
667#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
668#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
669#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
670#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
671/** SHA2-224 */
672#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
673/** SHA2-256 */
674#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
675/** SHA2-384 */
676#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
677/** SHA2-512 */
678#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
679/** SHA2-512/224 */
680#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
681/** SHA2-512/256 */
682#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
683/** SHA3-224 */
684#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
685/** SHA3-256 */
686#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
687/** SHA3-384 */
688#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
689/** SHA3-512 */
690#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
691
692#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
693#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
694/** Macro to build an HMAC algorithm.
695 *
696 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
697 *
698 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
699 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
700 *
701 * \return The corresponding HMAC algorithm.
702 * \return Unspecified if \p alg is not a supported
703 * hash algorithm.
704 */
705#define PSA_ALG_HMAC(hash_alg) \
706 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
707
708#define PSA_ALG_HMAC_HASH(hmac_alg) \
709 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
710
711/** Whether the specified algorithm is an HMAC algorithm.
712 *
713 * HMAC is a family of MAC algorithms that are based on a hash function.
714 *
715 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
716 *
717 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
718 * This macro may return either 0 or 1 if \p alg is not a supported
719 * algorithm identifier.
720 */
721#define PSA_ALG_IS_HMAC(alg) \
722 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
723 PSA_ALG_HMAC_BASE)
724
725#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
726#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
727#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
728#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
729
730/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
731 *
732 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
733 *
734 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
735 * This macro may return either 0 or 1 if \p alg is not a supported
736 * algorithm identifier.
737 */
738#define PSA_ALG_IS_CIPHER_MAC(alg) \
739 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
740 PSA_ALG_CIPHER_MAC_BASE)
741
742#define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
743#define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000)
744#define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff)
745#define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000)
746
747/** Use a block cipher mode without padding.
748 *
749 * This padding mode may only be used with messages whose lengths are a
750 * whole number of blocks for the chosen block cipher.
751 */
752#define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000)
753
754#define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000)
755
756/** Whether the specified algorithm is a block cipher.
757 *
758 * A block cipher is a symmetric cipher that encrypts or decrypts messages
759 * by chopping them into fixed-size blocks. Processing a message requires
760 * applying a _padding mode_ to transform the message into one whose
761 * length is a whole number of blocks. To construct an algorithm
762 * identifier for a block cipher, apply a bitwise-or between the block
763 * cipher mode and the padding mode. For example, CBC with PKCS#7 padding
764 * is `PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7`.
765 *
766 * The transformation applied to each block is determined by the key type.
767 * For example, to use AES-128-CBC-PKCS7, use the algorithm above with
768 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
769 *
770 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
771 *
772 * \return 1 if \p alg is a block cipher algorithm, 0 otherwise.
773 * This macro may return either 0 or 1 if \p alg is not a supported
774 * algorithm identifier or if it is not a symmetric cipher algorithm.
775 */
776#define PSA_ALG_IS_BLOCK_CIPHER(alg) \
777 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
778 PSA_ALG_BLOCK_CIPHER_BASE)
779
780/** The CBC block cipher mode.
781 */
782#define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001)
783#define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002)
784#define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003)
785#define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004)
786
787#define PSA_ALG_STREAM_CIPHER_BASE ((psa_algorithm_t)0x04800000)
788
789/** The CTR stream cipher mode.
790 *
791 * CTR is a stream cipher which is built from a block cipher. The
792 * underlying block cipher is determined by the key type. For example,
793 * to use AES-128-CTR, use this algorithm with
794 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
795 */
796#define PSA_ALG_CTR ((psa_algorithm_t)0x04800001)
797
798/** The ARC4 stream cipher algorithm.
799 */
800#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002)
801
802/** Whether the specified algorithm is a stream cipher.
803 *
804 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
805 * by applying a bitwise-xor with a stream of bytes that is generated
806 * from a key.
807 *
808 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
809 *
810 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
811 * This macro may return either 0 or 1 if \p alg is not a supported
812 * algorithm identifier or if it is not a symmetric cipher algorithm.
813 */
814#define PSA_ALG_IS_STREAM_CIPHER(alg) \
815 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
816 PSA_ALG_STREAM_CIPHER_BASE)
817
818#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
819#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
820
821#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
822/** RSA PKCS#1 v1.5 signature with hashing.
823 *
824 * This is the signature scheme defined by RFC 8017
825 * (PKCS#1: RSA Cryptography Specifications) under the name
826 * RSASSA-PKCS1-v1_5.
827 *
828 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
829 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
830 *
831 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
832 * \return Unspecified if \p alg is not a supported
833 * hash algorithm.
834 */
835#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
836 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
837/** Raw PKCS#1 v1.5 signature.
838 *
839 * The input to this algorithm is the DigestInfo structure used by
840 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
841 * steps 3&ndash;6.
842 */
843#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
844#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
845 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
846
847#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
848/** RSA PSS signature with hashing.
849 *
850 * This is the signature scheme defined by RFC 8017
851 * (PKCS#1: RSA Cryptography Specifications) under the name
852 * RSASSA-PSS, with the message generation function MGF1, and with
853 * a salt length equal to the length of the hash. The specified
854 * hash algorithm is used to hash the input message, to create the
855 * salted hash, and for the mask generation.
856 *
857 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
858 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
859 *
860 * \return The corresponding RSA PSS signature algorithm.
861 * \return Unspecified if \p alg is not a supported
862 * hash algorithm.
863 */
864#define PSA_ALG_RSA_PSS(hash_alg) \
865 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
866#define PSA_ALG_IS_RSA_PSS(alg) \
867 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
868
869#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
870/** DSA signature with hashing.
871 *
872 * This is the signature scheme defined by FIPS 186-4,
873 * with a random per-message secret number (*k*).
874 *
875 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
876 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
877 *
878 * \return The corresponding DSA signature algorithm.
879 * \return Unspecified if \p alg is not a supported
880 * hash algorithm.
881 */
882#define PSA_ALG_DSA(hash_alg) \
883 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
884#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
885#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
886#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
887 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
888#define PSA_ALG_IS_DSA(alg) \
889 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
890 PSA_ALG_DSA_BASE)
891#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
892 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
893#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
894 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
895#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
896 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
897
898#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
899/** ECDSA signature with hashing.
900 *
901 * This is the ECDSA signature scheme defined by ANSI X9.62,
902 * with a random per-message secret number (*k*).
903 *
904 * The representation of the signature as a byte string consists of
905 * the concatentation of the signature values *r* and *s*. Each of
906 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
907 * of the base point of the curve in octets. Each value is represented
908 * in big-endian order (most significant octet first).
909 *
910 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
911 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
912 *
913 * \return The corresponding ECDSA signature algorithm.
914 * \return Unspecified if \p alg is not a supported
915 * hash algorithm.
916 */
917#define PSA_ALG_ECDSA(hash_alg) \
918 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
919/** ECDSA signature without hashing.
920 *
921 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
922 * without specifying a hash algorithm. This algorithm may only be
923 * used to sign or verify a sequence of bytes that should be an
924 * already-calculated hash. Note that the input is padded with
925 * zeros on the left or truncated on the left as required to fit
926 * the curve size.
927 */
928#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
929#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
930/** Deterministic ECDSA signature with hashing.
931 *
932 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
933 *
934 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
935 *
936 * Note that when this algorithm is used for verification, signatures
937 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
938 * same private key are accepted. In other words,
939 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
940 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
941 *
942 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
943 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
944 *
945 * \return The corresponding deterministic ECDSA signature
946 * algorithm.
947 * \return Unspecified if \p alg is not a supported
948 * hash algorithm.
949 */
950#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
951 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
952#define PSA_ALG_IS_ECDSA(alg) \
953 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
954 PSA_ALG_ECDSA_BASE)
955#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
956 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
957#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
958 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
959#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
960 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
961
962/** Get the hash used by a hash-and-sign signature algorithm.
963 *
964 * A hash-and-sign algorithm is a signature algorithm which is
965 * composed of two phases: first a hashing phase which does not use
966 * the key and produces a hash of the input message, then a signing
967 * phase which only uses the hash and the key and not the message
968 * itself.
969 *
970 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
971 * #PSA_ALG_IS_SIGN(\p alg) is true).
972 *
973 * \return The underlying hash algorithm if \p alg is a hash-and-sign
974 * algorithm.
975 * \return 0 if \p alg is a signature algorithm that does not
976 * follow the hash-and-sign structure.
977 * \return Unspecified if \p alg is not a signature algorithm or
978 * if it is not supported by the implementation.
979 */
980#define PSA_ALG_SIGN_GET_HASH(alg) \
981 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
982 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg) ? \
983 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
984 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
985 0)
986
987/** RSA PKCS#1 v1.5 encryption.
988 */
989#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
990
991#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
992/** RSA OAEP encryption.
993 *
994 * This is the encryption scheme defined by RFC 8017
995 * (PKCS#1: RSA Cryptography Specifications) under the name
996 * RSAES-OAEP, with the message generation function MGF1.
997 *
998 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
999 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1000 * for MGF1.
1001 *
1002 * \return The corresponding RSA OAEP signature algorithm.
1003 * \return Unspecified if \p alg is not a supported
1004 * hash algorithm.
1005 */
1006#define PSA_ALG_RSA_OAEP(hash_alg) \
1007 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1008#define PSA_ALG_IS_RSA_OAEP(alg) \
1009 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
1010#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1011 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1012 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1013 0)
1014
1015#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x30000100)
1016/** Macro to build an HKDF algorithm.
1017 *
1018 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1019 *
1020 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1021 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1022 *
1023 * \return The corresponding HKDF algorithm.
1024 * \return Unspecified if \p alg is not a supported
1025 * hash algorithm.
1026 */
1027#define PSA_ALG_HKDF(hash_alg) \
1028 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1029/** Whether the specified algorithm is an HKDF algorithm.
1030 *
1031 * HKDF is a family of key derivation algorithms that are based on a hash
1032 * function and the HMAC construction.
1033 *
1034 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1035 *
1036 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1037 * This macro may return either 0 or 1 if \c alg is not a supported
1038 * key derivation algorithm identifier.
1039 */
1040#define PSA_ALG_IS_HKDF(alg) \
1041 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1042#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1043 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1044
1045/**@}*/
1046
1047/** \defgroup key_management Key management
1048 * @{
1049 */
1050
1051/**
1052 * \brief Import a key in binary format.
1053 *
1054 * This function supports any output from psa_export_key(). Refer to the
1055 * documentation of psa_export_key() for the format for each key type.
1056 *
1057 * \param key Slot where the key will be stored. This must be a
1058 * valid slot for a key of the chosen type. It must
1059 * be unoccupied.
1060 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
1061 * \param[in] data Buffer containing the key data.
1062 * \param data_length Size of the \p data buffer in bytes.
1063 *
1064 * \retval #PSA_SUCCESS
1065 * Success.
1066 * \retval #PSA_ERROR_NOT_SUPPORTED
1067 * The key type or key size is not supported, either by the
1068 * implementation in general or in this particular slot.
1069 * \retval #PSA_ERROR_INVALID_ARGUMENT
1070 * The key slot is invalid,
1071 * or the key data is not correctly formatted.
1072 * \retval #PSA_ERROR_OCCUPIED_SLOT
1073 * There is already a key in the specified slot.
1074 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1075 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1076 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1077 * \retval #PSA_ERROR_HARDWARE_FAILURE
1078 * \retval #PSA_ERROR_TAMPERING_DETECTED
1079 */
1080psa_status_t psa_import_key(psa_key_slot_t key,
1081 psa_key_type_t type,
1082 const uint8_t *data,
1083 size_t data_length);
1084
1085/**
1086 * \brief Destroy a key and restore the slot to its default state.
1087 *
1088 * This function destroys the content of the key slot from both volatile
1089 * memory and, if applicable, non-volatile storage. Implementations shall
1090 * make a best effort to ensure that any previous content of the slot is
1091 * unrecoverable.
1092 *
1093 * This function also erases any metadata such as policies. It returns the
1094 * specified slot to its default state.
1095 *
1096 * \param key The key slot to erase.
1097 *
1098 * \retval #PSA_SUCCESS
1099 * The slot's content, if any, has been erased.
1100 * \retval #PSA_ERROR_NOT_PERMITTED
1101 * The slot holds content and cannot be erased because it is
1102 * read-only, either due to a policy or due to physical restrictions.
1103 * \retval #PSA_ERROR_INVALID_ARGUMENT
1104 * The specified slot number does not designate a valid slot.
1105 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1106 * There was an failure in communication with the cryptoprocessor.
1107 * The key material may still be present in the cryptoprocessor.
1108 * \retval #PSA_ERROR_STORAGE_FAILURE
1109 * The storage is corrupted. Implementations shall make a best effort
1110 * to erase key material even in this stage, however applications
1111 * should be aware that it may be impossible to guarantee that the
1112 * key material is not recoverable in such cases.
1113 * \retval #PSA_ERROR_TAMPERING_DETECTED
1114 * An unexpected condition which is not a storage corruption or
1115 * a communication failure occurred. The cryptoprocessor may have
1116 * been compromised.
1117 */
1118psa_status_t psa_destroy_key(psa_key_slot_t key);
1119
1120/**
1121 * \brief Get basic metadata about a key.
1122 *
1123 * \param key Slot whose content is queried. This must
1124 * be an occupied key slot.
1125 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
1126 * This may be a null pointer, in which case the key type
1127 * is not written.
1128 * \param[out] bits On success, the key size in bits.
1129 * This may be a null pointer, in which case the key size
1130 * is not written.
1131 *
1132 * \retval #PSA_SUCCESS
1133 * \retval #PSA_ERROR_EMPTY_SLOT
1134 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1135 * \retval #PSA_ERROR_HARDWARE_FAILURE
1136 * \retval #PSA_ERROR_TAMPERING_DETECTED
1137 */
1138psa_status_t psa_get_key_information(psa_key_slot_t key,
1139 psa_key_type_t *type,
1140 size_t *bits);
1141
1142/**
1143 * \brief Export a key in binary format.
1144 *
1145 * The output of this function can be passed to psa_import_key() to
1146 * create an equivalent object.
1147 *
1148 * If a key is created with psa_import_key() and then exported with
1149 * this function, it is not guaranteed that the resulting data is
1150 * identical: the implementation may choose a different representation
1151 * of the same key if the format permits it.
1152 *
1153 * For standard key types, the output format is as follows:
1154 *
1155 * - For symmetric keys (including MAC keys), the format is the
1156 * raw bytes of the key.
1157 * - For DES, the key data consists of 8 bytes. The parity bits must be
1158 * correct.
1159 * - For Triple-DES, the format is the concatenation of the
1160 * two or three DES keys.
1161 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
1162 * is the non-encrypted DER representation defined by PKCS\#1 (RFC 8017)
1163 * as RSAPrivateKey.
1164 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
1165 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
1166 *
1167 * \param key Slot whose content is to be exported. This must
1168 * be an occupied key slot.
1169 * \param[out] data Buffer where the key data is to be written.
1170 * \param data_size Size of the \p data buffer in bytes.
1171 * \param[out] data_length On success, the number of bytes
1172 * that make up the key data.
1173 *
1174 * \retval #PSA_SUCCESS
1175 * \retval #PSA_ERROR_EMPTY_SLOT
1176 * \retval #PSA_ERROR_NOT_PERMITTED
1177 * \retval #PSA_ERROR_NOT_SUPPORTED
1178 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1179 * \retval #PSA_ERROR_HARDWARE_FAILURE
1180 * \retval #PSA_ERROR_TAMPERING_DETECTED
1181 */
1182psa_status_t psa_export_key(psa_key_slot_t key,
1183 uint8_t *data,
1184 size_t data_size,
1185 size_t *data_length);
1186
1187/**
1188 * \brief Export a public key or the public part of a key pair in binary format.
1189 *
1190 * The output of this function can be passed to psa_import_key() to
1191 * create an object that is equivalent to the public key.
1192 *
1193 * For standard key types, the output format is as follows:
1194 *
1195 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
1196 * the format is the DER representation of the public key defined by RFC 5280
1197 * as SubjectPublicKeyInfo.
1198 *
1199 * \param key Slot whose content is to be exported. This must
1200 * be an occupied key slot.
1201 * \param[out] data Buffer where the key data is to be written.
1202 * \param data_size Size of the \p data buffer in bytes.
1203 * \param[out] data_length On success, the number of bytes
1204 * that make up the key data.
1205 *
1206 * \retval #PSA_SUCCESS
1207 * \retval #PSA_ERROR_EMPTY_SLOT
1208 * \retval #PSA_ERROR_INVALID_ARGUMENT
1209 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1210 * \retval #PSA_ERROR_HARDWARE_FAILURE
1211 * \retval #PSA_ERROR_TAMPERING_DETECTED
1212 */
1213psa_status_t psa_export_public_key(psa_key_slot_t key,
1214 uint8_t *data,
1215 size_t data_size,
1216 size_t *data_length);
1217
1218/**@}*/
1219
1220/** \defgroup policy Key policies
1221 * @{
1222 */
1223
1224/** \brief Encoding of permitted usage on a key. */
1225typedef uint32_t psa_key_usage_t;
1226
1227/** Whether the key may be exported.
1228 *
1229 * A public key or the public part of a key pair may always be exported
1230 * regardless of the value of this permission flag.
1231 *
1232 * If a key does not have export permission, implementations shall not
1233 * allow the key to be exported in plain form from the cryptoprocessor,
1234 * whether through psa_export_key() or through a proprietary interface.
1235 * The key may however be exportable in a wrapped form, i.e. in a form
1236 * where it is encrypted by another key.
1237 */
1238#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1239
1240/** Whether the key may be used to encrypt a message.
1241 *
1242 * This flag allows the key to be used for a symmetric encryption operation,
1243 * for an AEAD encryption-and-authentication operation,
1244 * or for an asymmetric encryption operation,
1245 * if otherwise permitted by the key's type and policy.
1246 *
1247 * For a key pair, this concerns the public key.
1248 */
1249#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
1250
1251/** Whether the key may be used to decrypt a message.
1252 *
1253 * This flag allows the key to be used for a symmetric decryption operation,
1254 * for an AEAD decryption-and-verification operation,
1255 * or for an asymmetric decryption operation,
1256 * if otherwise permitted by the key's type and policy.
1257 *
1258 * For a key pair, this concerns the private key.
1259 */
1260#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
1261
1262/** Whether the key may be used to sign a message.
1263 *
1264 * This flag allows the key to be used for a MAC calculation operation
1265 * or for an asymmetric signature operation,
1266 * if otherwise permitted by the key's type and policy.
1267 *
1268 * For a key pair, this concerns the private key.
1269 */
1270#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
1271
1272/** Whether the key may be used to verify a message signature.
1273 *
1274 * This flag allows the key to be used for a MAC verification operation
1275 * or for an asymmetric signature verification operation,
1276 * if otherwise permitted by by the key's type and policy.
1277 *
1278 * For a key pair, this concerns the public key.
1279 */
1280#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
1281
1282/** Whether the key may be used to derive other keys.
1283 */
1284#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00001000)
1285
1286/** The type of the key policy data structure.
1287 *
1288 * This is an implementation-defined \c struct. Applications should not
1289 * make any assumptions about the content of this structure except
1290 * as directed by the documentation of a specific implementation. */
1291typedef struct psa_key_policy_s psa_key_policy_t;
1292
1293/** \brief Initialize a key policy structure to a default that forbids all
1294 * usage of the key.
1295 *
1296 * \param[out] policy The policy object to initialize.
1297 */
1298void psa_key_policy_init(psa_key_policy_t *policy);
1299
1300/** \brief Set the standard fields of a policy structure.
1301 *
1302 * Note that this function does not make any consistency check of the
1303 * parameters. The values are only checked when applying the policy to
1304 * a key slot with psa_set_key_policy().
1305 *
1306 * \param[out] policy The policy object to modify.
1307 * \param usage The permitted uses for the key.
1308 * \param alg The algorithm that the key may be used for.
1309 */
1310void psa_key_policy_set_usage(psa_key_policy_t *policy,
1311 psa_key_usage_t usage,
1312 psa_algorithm_t alg);
1313
1314/** \brief Retrieve the usage field of a policy structure.
1315 *
1316 * \param[in] policy The policy object to query.
1317 *
1318 * \return The permitted uses for a key with this policy.
1319 */
1320psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
1321
1322/** \brief Retrieve the algorithm field of a policy structure.
1323 *
1324 * \param[in] policy The policy object to query.
1325 *
1326 * \return The permitted algorithm for a key with this policy.
1327 */
1328psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
1329
1330/** \brief Set the usage policy on a key slot.
1331 *
1332 * This function must be called on an empty key slot, before importing,
1333 * generating or creating a key in the slot. Changing the policy of an
1334 * existing key is not permitted.
1335 *
1336 * Implementations may set restrictions on supported key policies
1337 * depending on the key type and the key slot.
1338 *
1339 * \param key The key slot whose policy is to be changed.
1340 * \param[in] policy The policy object to query.
1341 *
1342 * \retval #PSA_SUCCESS
1343 * \retval #PSA_ERROR_OCCUPIED_SLOT
1344 * \retval #PSA_ERROR_NOT_SUPPORTED
1345 * \retval #PSA_ERROR_INVALID_ARGUMENT
1346 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1347 * \retval #PSA_ERROR_HARDWARE_FAILURE
1348 * \retval #PSA_ERROR_TAMPERING_DETECTED
1349 */
1350psa_status_t psa_set_key_policy(psa_key_slot_t key,
1351 const psa_key_policy_t *policy);
1352
1353/** \brief Get the usage policy for a key slot.
1354 *
1355 * \param key The key slot whose policy is being queried.
1356 * \param[out] policy On success, the key's policy.
1357 *
1358 * \retval #PSA_SUCCESS
1359 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1360 * \retval #PSA_ERROR_HARDWARE_FAILURE
1361 * \retval #PSA_ERROR_TAMPERING_DETECTED
1362 */
1363psa_status_t psa_get_key_policy(psa_key_slot_t key,
1364 psa_key_policy_t *policy);
1365
1366/**@}*/
1367
1368/** \defgroup persistence Key lifetime
1369 * @{
1370 */
1371
1372/** Encoding of key lifetimes.
1373 */
1374typedef uint32_t psa_key_lifetime_t;
1375
1376/** A volatile key slot retains its content as long as the application is
1377 * running. It is guaranteed to be erased on a power reset.
1378 */
1379#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1380
1381/** A persistent key slot retains its content as long as it is not explicitly
1382 * destroyed.
1383 */
1384#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1385
1386/** A write-once key slot may not be modified once a key has been set.
1387 * It will retain its content as long as the device remains operational.
1388 */
1389#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
1390
1391/** \brief Retrieve the lifetime of a key slot.
1392 *
1393 * The assignment of lifetimes to slots is implementation-dependent.
1394 *
1395 * \param key Slot to query.
1396 * \param[out] lifetime On success, the lifetime value.
1397 *
1398 * \retval #PSA_SUCCESS
1399 * Success.
1400 * \retval #PSA_ERROR_INVALID_ARGUMENT
1401 * The key slot is invalid.
1402 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1403 * \retval #PSA_ERROR_HARDWARE_FAILURE
1404 * \retval #PSA_ERROR_TAMPERING_DETECTED
1405 */
1406psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
1407 psa_key_lifetime_t *lifetime);
1408
1409/** \brief Change the lifetime of a key slot.
1410 *
1411 * Whether the lifetime of a key slot can be changed at all, and if so
1412 * whether the lifetime of an occupied key slot can be changed, is
1413 * implementation-dependent.
1414 *
1415 * \param key Slot whose lifetime is to be changed.
1416 * \param lifetime The lifetime value to set for the given key slot.
1417 *
1418 * \retval #PSA_SUCCESS
1419 * Success.
1420 * \retval #PSA_ERROR_INVALID_ARGUMENT
1421 * The key slot is invalid,
1422 * or the lifetime value is invalid.
1423 * \retval #PSA_ERROR_NOT_SUPPORTED
1424 * The implementation does not support the specified lifetime value,
1425 * at least for the specified key slot.
1426 * \retval #PSA_ERROR_OCCUPIED_SLOT
1427 * The slot contains a key, and the implementation does not support
1428 * changing the lifetime of an occupied slot.
1429 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1430 * \retval #PSA_ERROR_HARDWARE_FAILURE
1431 * \retval #PSA_ERROR_TAMPERING_DETECTED
1432 */
1433psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
1434 psa_key_lifetime_t lifetime);
1435
1436/**@}*/
1437
1438/** \defgroup hash Message digests
1439 * @{
1440 */
1441
1442/** The type of the state data structure for multipart hash operations.
1443 *
1444 * This is an implementation-defined \c struct. Applications should not
1445 * make any assumptions about the content of this structure except
1446 * as directed by the documentation of a specific implementation. */
1447typedef struct psa_hash_operation_s psa_hash_operation_t;
1448
1449/** The size of the output of psa_hash_finish(), in bytes.
1450 *
1451 * This is also the hash size that psa_hash_verify() expects.
1452 *
1453 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
1454 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
1455 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
1456 * hash algorithm).
1457 *
1458 * \return The hash size for the specified hash algorithm.
1459 * If the hash algorithm is not recognized, return 0.
1460 * An implementation may return either 0 or the correct size
1461 * for a hash algorithm that it recognizes, but does not support.
1462 */
1463#define PSA_HASH_SIZE(alg) \
1464 ( \
1465 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD2 ? 16 : \
1466 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD4 ? 16 : \
1467 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD5 ? 16 : \
1468 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
1469 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
1470 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
1471 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
1472 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
1473 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
1474 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
1475 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
1476 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
1477 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
1478 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
1479 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
1480 0)
1481
1482/** Start a multipart hash operation.
1483 *
1484 * The sequence of operations to calculate a hash (message digest)
1485 * is as follows:
1486 * -# Allocate an operation object which will be passed to all the functions
1487 * listed here.
1488 * -# Call psa_hash_setup() to specify the algorithm.
1489 * -# Call psa_hash_update() zero, one or more times, passing a fragment
1490 * of the message each time. The hash that is calculated is the hash
1491 * of the concatenation of these messages in order.
1492 * -# To calculate the hash, call psa_hash_finish().
1493 * To compare the hash with an expected value, call psa_hash_verify().
1494 *
1495 * The application may call psa_hash_abort() at any time after the operation
1496 * has been initialized with psa_hash_setup().
1497 *
1498 * After a successful call to psa_hash_setup(), the application must
1499 * eventually terminate the operation. The following events terminate an
1500 * operation:
1501 * - A failed call to psa_hash_update().
1502 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
1503 *
1504 * \param[out] operation The operation object to use.
1505 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1506 * such that #PSA_ALG_IS_HASH(\p alg) is true).
1507 *
1508 * \retval #PSA_SUCCESS
1509 * Success.
1510 * \retval #PSA_ERROR_NOT_SUPPORTED
1511 * \p alg is not supported or is not a hash algorithm.
1512 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1513 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1514 * \retval #PSA_ERROR_HARDWARE_FAILURE
1515 * \retval #PSA_ERROR_TAMPERING_DETECTED
1516 */
1517psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
1518 psa_algorithm_t alg);
1519
1520/** Add a message fragment to a multipart hash operation.
1521 *
1522 * The application must call psa_hash_setup() before calling this function.
1523 *
1524 * If this function returns an error status, the operation becomes inactive.
1525 *
1526 * \param[in,out] operation Active hash operation.
1527 * \param[in] input Buffer containing the message fragment to hash.
1528 * \param input_length Size of the \p input buffer in bytes.
1529 *
1530 * \retval #PSA_SUCCESS
1531 * Success.
1532 * \retval #PSA_ERROR_BAD_STATE
1533 * The operation state is not valid (not started, or already completed).
1534 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1535 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1536 * \retval #PSA_ERROR_HARDWARE_FAILURE
1537 * \retval #PSA_ERROR_TAMPERING_DETECTED
1538 */
1539psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1540 const uint8_t *input,
1541 size_t input_length);
1542
1543/** Finish the calculation of the hash of a message.
1544 *
1545 * The application must call psa_hash_setup() before calling this function.
1546 * This function calculates the hash of the message formed by concatenating
1547 * the inputs passed to preceding calls to psa_hash_update().
1548 *
1549 * When this function returns, the operation becomes inactive.
1550 *
1551 * \warning Applications should not call this function if they expect
1552 * a specific value for the hash. Call psa_hash_verify() instead.
1553 * Beware that comparing integrity or authenticity data such as
1554 * hash values with a function such as \c memcmp is risky
1555 * because the time taken by the comparison may leak information
1556 * about the hashed data which could allow an attacker to guess
1557 * a valid hash and thereby bypass security controls.
1558 *
1559 * \param[in,out] operation Active hash operation.
1560 * \param[out] hash Buffer where the hash is to be written.
1561 * \param hash_size Size of the \p hash buffer in bytes.
1562 * \param[out] hash_length On success, the number of bytes
1563 * that make up the hash value. This is always
1564 * #PSA_HASH_SIZE(\c alg) where \c alg is the
1565 * hash algorithm that is calculated.
1566 *
1567 * \retval #PSA_SUCCESS
1568 * Success.
1569 * \retval #PSA_ERROR_BAD_STATE
1570 * The operation state is not valid (not started, or already completed).
1571 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1572 * The size of the \p hash buffer is too small. You can determine a
1573 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
1574 * where \c alg is the hash algorithm that is calculated.
1575 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1576 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1577 * \retval #PSA_ERROR_HARDWARE_FAILURE
1578 * \retval #PSA_ERROR_TAMPERING_DETECTED
1579 */
1580psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1581 uint8_t *hash,
1582 size_t hash_size,
1583 size_t *hash_length);
1584
1585/** Finish the calculation of the hash of a message and compare it with
1586 * an expected value.
1587 *
1588 * The application must call psa_hash_setup() before calling this function.
1589 * This function calculates the hash of the message formed by concatenating
1590 * the inputs passed to preceding calls to psa_hash_update(). It then
1591 * compares the calculated hash with the expected hash passed as a
1592 * parameter to this function.
1593 *
1594 * When this function returns, the operation becomes inactive.
1595 *
1596 * \note Implementations shall make the best effort to ensure that the
1597 * comparison between the actual hash and the expected hash is performed
1598 * in constant time.
1599 *
1600 * \param[in,out] operation Active hash operation.
1601 * \param[in] hash Buffer containing the expected hash value.
1602 * \param hash_length Size of the \p hash buffer in bytes.
1603 *
1604 * \retval #PSA_SUCCESS
1605 * The expected hash is identical to the actual hash of the message.
1606 * \retval #PSA_ERROR_INVALID_SIGNATURE
1607 * The hash of the message was calculated successfully, but it
1608 * differs from the expected hash.
1609 * \retval #PSA_ERROR_BAD_STATE
1610 * The operation state is not valid (not started, or already completed).
1611 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1612 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1613 * \retval #PSA_ERROR_HARDWARE_FAILURE
1614 * \retval #PSA_ERROR_TAMPERING_DETECTED
1615 */
1616psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1617 const uint8_t *hash,
1618 size_t hash_length);
1619
1620/** Abort a hash operation.
1621 *
1622 * Aborting an operation frees all associated resources except for the
1623 * \p operation structure itself. Once aborted, the operation object
1624 * can be reused for another operation by calling
1625 * psa_hash_setup() again.
1626 *
1627 * You may call this function any time after the operation object has
1628 * been initialized by any of the following methods:
1629 * - A call to psa_hash_setup(), whether it succeeds or not.
1630 * - Initializing the \c struct to all-bits-zero.
1631 * - Initializing the \c struct to logical zeros, e.g.
1632 * `psa_hash_operation_t operation = {0}`.
1633 *
1634 * In particular, calling psa_hash_abort() after the operation has been
1635 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1636 * psa_hash_verify() is safe and has no effect.
1637 *
1638 * \param[in,out] operation Initialized hash operation.
1639 *
1640 * \retval #PSA_SUCCESS
1641 * \retval #PSA_ERROR_BAD_STATE
1642 * \p operation is not an active hash operation.
1643 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1644 * \retval #PSA_ERROR_HARDWARE_FAILURE
1645 * \retval #PSA_ERROR_TAMPERING_DETECTED
1646 */
1647psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
1648
1649/**@}*/
1650
1651/** \defgroup MAC Message authentication codes
1652 * @{
1653 */
1654
1655/** The type of the state data structure for multipart MAC operations.
1656 *
1657 * This is an implementation-defined \c struct. Applications should not
1658 * make any assumptions about the content of this structure except
1659 * as directed by the documentation of a specific implementation. */
1660typedef struct psa_mac_operation_s psa_mac_operation_t;
1661
1662/** Start a multipart MAC calculation operation.
1663 *
1664 * This function sets up the calculation of the MAC
1665 * (message authentication code) of a byte string.
1666 * To verify the MAC of a message against an
1667 * expected value, use psa_mac_verify_setup() instead.
1668 *
1669 * The sequence of operations to calculate a MAC is as follows:
1670 * -# Allocate an operation object which will be passed to all the functions
1671 * listed here.
1672 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
1673 * The key remains associated with the operation even if the content
1674 * of the key slot changes.
1675 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1676 * of the message each time. The MAC that is calculated is the MAC
1677 * of the concatenation of these messages in order.
1678 * -# At the end of the message, call psa_mac_sign_finish() to finish
1679 * calculating the MAC value and retrieve it.
1680 *
1681 * The application may call psa_mac_abort() at any time after the operation
1682 * has been initialized with psa_mac_sign_setup().
1683 *
1684 * After a successful call to psa_mac_sign_setup(), the application must
1685 * eventually terminate the operation through one of the following methods:
1686 * - A failed call to psa_mac_update().
1687 * - A call to psa_mac_sign_finish() or psa_mac_abort().
1688 *
1689 * \param[out] operation The operation object to use.
1690 * \param key Slot containing the key to use for the operation.
1691 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1692 * such that #PSA_ALG_IS_MAC(alg) is true).
1693 *
1694 * \retval #PSA_SUCCESS
1695 * Success.
1696 * \retval #PSA_ERROR_EMPTY_SLOT
1697 * \retval #PSA_ERROR_NOT_PERMITTED
1698 * \retval #PSA_ERROR_INVALID_ARGUMENT
1699 * \p key is not compatible with \p alg.
1700 * \retval #PSA_ERROR_NOT_SUPPORTED
1701 * \p alg is not supported or is not a MAC algorithm.
1702 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1703 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1704 * \retval #PSA_ERROR_HARDWARE_FAILURE
1705 * \retval #PSA_ERROR_TAMPERING_DETECTED
1706 */
1707psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
1708 psa_key_slot_t key,
1709 psa_algorithm_t alg);
1710
1711/** Start a multipart MAC verification operation.
1712 *
1713 * This function sets up the verification of the MAC
1714 * (message authentication code) of a byte string against an expected value.
1715 *
1716 * The sequence of operations to verify a MAC is as follows:
1717 * -# Allocate an operation object which will be passed to all the functions
1718 * listed here.
1719 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1720 * The key remains associated with the operation even if the content
1721 * of the key slot changes.
1722 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1723 * of the message each time. The MAC that is calculated is the MAC
1724 * of the concatenation of these messages in order.
1725 * -# At the end of the message, call psa_mac_verify_finish() to finish
1726 * calculating the actual MAC of the message and verify it against
1727 * the expected value.
1728 *
1729 * The application may call psa_mac_abort() at any time after the operation
1730 * has been initialized with psa_mac_verify_setup().
1731 *
1732 * After a successful call to psa_mac_verify_setup(), the application must
1733 * eventually terminate the operation through one of the following methods:
1734 * - A failed call to psa_mac_update().
1735 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1736 *
1737 * \param[out] operation The operation object to use.
1738 * \param key Slot containing the key to use for the operation.
1739 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1740 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1741 *
1742 * \retval #PSA_SUCCESS
1743 * Success.
1744 * \retval #PSA_ERROR_EMPTY_SLOT
1745 * \retval #PSA_ERROR_NOT_PERMITTED
1746 * \retval #PSA_ERROR_INVALID_ARGUMENT
1747 * \c key is not compatible with \c alg.
1748 * \retval #PSA_ERROR_NOT_SUPPORTED
1749 * \c alg is not supported or is not a MAC algorithm.
1750 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1751 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1752 * \retval #PSA_ERROR_HARDWARE_FAILURE
1753 * \retval #PSA_ERROR_TAMPERING_DETECTED
1754 */
1755psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
1756 psa_key_slot_t key,
1757 psa_algorithm_t alg);
1758
1759/** Add a message fragment to a multipart MAC operation.
1760 *
1761 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1762 * before calling this function.
1763 *
1764 * If this function returns an error status, the operation becomes inactive.
1765 *
1766 * \param[in,out] operation Active MAC operation.
1767 * \param[in] input Buffer containing the message fragment to add to
1768 * the MAC calculation.
1769 * \param input_length Size of the \p input buffer in bytes.
1770 *
1771 * \retval #PSA_SUCCESS
1772 * Success.
1773 * \retval #PSA_ERROR_BAD_STATE
1774 * The operation state is not valid (not started, or already completed).
1775 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1776 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1777 * \retval #PSA_ERROR_HARDWARE_FAILURE
1778 * \retval #PSA_ERROR_TAMPERING_DETECTED
1779 */
1780psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1781 const uint8_t *input,
1782 size_t input_length);
1783
1784/** Finish the calculation of the MAC of a message.
1785 *
1786 * The application must call psa_mac_sign_setup() before calling this function.
1787 * This function calculates the MAC of the message formed by concatenating
1788 * the inputs passed to preceding calls to psa_mac_update().
1789 *
1790 * When this function returns, the operation becomes inactive.
1791 *
1792 * \warning Applications should not call this function if they expect
1793 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1794 * Beware that comparing integrity or authenticity data such as
1795 * MAC values with a function such as \c memcmp is risky
1796 * because the time taken by the comparison may leak information
1797 * about the MAC value which could allow an attacker to guess
1798 * a valid MAC and thereby bypass security controls.
1799 *
1800 * \param[in,out] operation Active MAC operation.
1801 * \param[out] mac Buffer where the MAC value is to be written.
1802 * \param mac_size Size of the \p mac buffer in bytes.
1803 * \param[out] mac_length On success, the number of bytes
1804 * that make up the MAC value. This is always
1805 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
1806 * where \c key_type and \c key_bits are the type and
1807 * bit-size respectively of the key and \c alg is the
1808 * MAC algorithm that is calculated.
1809 *
1810 * \retval #PSA_SUCCESS
1811 * Success.
1812 * \retval #PSA_ERROR_BAD_STATE
1813 * The operation state is not valid (not started, or already completed).
1814 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1815 * The size of the \p mac buffer is too small. You can determine a
1816 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1817 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1818 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1819 * \retval #PSA_ERROR_HARDWARE_FAILURE
1820 * \retval #PSA_ERROR_TAMPERING_DETECTED
1821 */
1822psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1823 uint8_t *mac,
1824 size_t mac_size,
1825 size_t *mac_length);
1826
1827/** Finish the calculation of the MAC of a message and compare it with
1828 * an expected value.
1829 *
1830 * The application must call psa_mac_verify_setup() before calling this function.
1831 * This function calculates the MAC of the message formed by concatenating
1832 * the inputs passed to preceding calls to psa_mac_update(). It then
1833 * compares the calculated MAC with the expected MAC passed as a
1834 * parameter to this function.
1835 *
1836 * When this function returns, the operation becomes inactive.
1837 *
1838 * \note Implementations shall make the best effort to ensure that the
1839 * comparison between the actual MAC and the expected MAC is performed
1840 * in constant time.
1841 *
1842 * \param[in,out] operation Active MAC operation.
1843 * \param[in] mac Buffer containing the expected MAC value.
1844 * \param mac_length Size of the \p mac buffer in bytes.
1845 *
1846 * \retval #PSA_SUCCESS
1847 * The expected MAC is identical to the actual MAC of the message.
1848 * \retval #PSA_ERROR_INVALID_SIGNATURE
1849 * The MAC of the message was calculated successfully, but it
1850 * differs from the expected MAC.
1851 * \retval #PSA_ERROR_BAD_STATE
1852 * The operation state is not valid (not started, or already completed).
1853 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1854 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1855 * \retval #PSA_ERROR_HARDWARE_FAILURE
1856 * \retval #PSA_ERROR_TAMPERING_DETECTED
1857 */
1858psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1859 const uint8_t *mac,
1860 size_t mac_length);
1861
1862/** Abort a MAC operation.
1863 *
1864 * Aborting an operation frees all associated resources except for the
1865 * \p operation structure itself. Once aborted, the operation object
1866 * can be reused for another operation by calling
1867 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
1868 *
1869 * You may call this function any time after the operation object has
1870 * been initialized by any of the following methods:
1871 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1872 * it succeeds or not.
1873 * - Initializing the \c struct to all-bits-zero.
1874 * - Initializing the \c struct to logical zeros, e.g.
1875 * `psa_mac_operation_t operation = {0}`.
1876 *
1877 * In particular, calling psa_mac_abort() after the operation has been
1878 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1879 * psa_mac_verify_finish() is safe and has no effect.
1880 *
1881 * \param[in,out] operation Initialized MAC operation.
1882 *
1883 * \retval #PSA_SUCCESS
1884 * \retval #PSA_ERROR_BAD_STATE
1885 * \p operation is not an active MAC operation.
1886 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1887 * \retval #PSA_ERROR_HARDWARE_FAILURE
1888 * \retval #PSA_ERROR_TAMPERING_DETECTED
1889 */
1890psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1891
1892/**@}*/
1893
1894/** \defgroup cipher Symmetric ciphers
1895 * @{
1896 */
1897
1898/** The type of the state data structure for multipart cipher operations.
1899 *
1900 * This is an implementation-defined \c struct. Applications should not
1901 * make any assumptions about the content of this structure except
1902 * as directed by the documentation of a specific implementation. */
1903typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1904
1905/** Set the key for a multipart symmetric encryption operation.
1906 *
1907 * The sequence of operations to encrypt a message with a symmetric cipher
1908 * is as follows:
1909 * -# Allocate an operation object which will be passed to all the functions
1910 * listed here.
1911 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
1912 * The key remains associated with the operation even if the content
1913 * of the key slot changes.
1914 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
1915 * generate or set the IV (initialization vector). You should use
1916 * psa_cipher_generate_iv() unless the protocol you are implementing
1917 * requires a specific IV value.
1918 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1919 * of the message each time.
1920 * -# Call psa_cipher_finish().
1921 *
1922 * The application may call psa_cipher_abort() at any time after the operation
1923 * has been initialized with psa_cipher_encrypt_setup().
1924 *
1925 * After a successful call to psa_cipher_encrypt_setup(), the application must
1926 * eventually terminate the operation. The following events terminate an
1927 * operation:
1928 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
1929 * or psa_cipher_update().
1930 * - A call to psa_cipher_finish() or psa_cipher_abort().
1931 *
1932 * \param[out] operation The operation object to use.
1933 * \param key Slot containing the key to use for the operation.
1934 * \param alg The cipher algorithm to compute
1935 * (\c PSA_ALG_XXX value such that
1936 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1937 *
1938 * \retval #PSA_SUCCESS
1939 * Success.
1940 * \retval #PSA_ERROR_EMPTY_SLOT
1941 * \retval #PSA_ERROR_NOT_PERMITTED
1942 * \retval #PSA_ERROR_INVALID_ARGUMENT
1943 * \p key is not compatible with \p alg.
1944 * \retval #PSA_ERROR_NOT_SUPPORTED
1945 * \p alg is not supported or is not a cipher algorithm.
1946 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1947 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1948 * \retval #PSA_ERROR_HARDWARE_FAILURE
1949 * \retval #PSA_ERROR_TAMPERING_DETECTED
1950 */
1951psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
1952 psa_key_slot_t key,
1953 psa_algorithm_t alg);
1954
1955/** Set the key for a multipart symmetric decryption operation.
1956 *
1957 * The sequence of operations to decrypt a message with a symmetric cipher
1958 * is as follows:
1959 * -# Allocate an operation object which will be passed to all the functions
1960 * listed here.
1961 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
1962 * The key remains associated with the operation even if the content
1963 * of the key slot changes.
1964 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1965 * decryption. If the IV is prepended to the ciphertext, you can call
1966 * psa_cipher_update() on a buffer containing the IV followed by the
1967 * beginning of the message.
1968 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1969 * of the message each time.
1970 * -# Call psa_cipher_finish().
1971 *
1972 * The application may call psa_cipher_abort() at any time after the operation
1973 * has been initialized with psa_cipher_decrypt_setup().
1974 *
1975 * After a successful call to psa_cipher_decrypt_setup(), the application must
1976 * eventually terminate the operation. The following events terminate an
1977 * operation:
1978 * - A failed call to psa_cipher_update().
1979 * - A call to psa_cipher_finish() or psa_cipher_abort().
1980 *
1981 * \param[out] operation The operation object to use.
1982 * \param key Slot containing the key to use for the operation.
1983 * \param alg The cipher algorithm to compute
1984 * (\c PSA_ALG_XXX value such that
1985 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1986 *
1987 * \retval #PSA_SUCCESS
1988 * Success.
1989 * \retval #PSA_ERROR_EMPTY_SLOT
1990 * \retval #PSA_ERROR_NOT_PERMITTED
1991 * \retval #PSA_ERROR_INVALID_ARGUMENT
1992 * \p key is not compatible with \p alg.
1993 * \retval #PSA_ERROR_NOT_SUPPORTED
1994 * \p alg is not supported or is not a cipher algorithm.
1995 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1996 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1997 * \retval #PSA_ERROR_HARDWARE_FAILURE
1998 * \retval #PSA_ERROR_TAMPERING_DETECTED
1999 */
2000psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
2001 psa_key_slot_t key,
2002 psa_algorithm_t alg);
2003
2004/** Generate an IV for a symmetric encryption operation.
2005 *
2006 * This function generates a random IV (initialization vector), nonce
2007 * or initial counter value for the encryption operation as appropriate
2008 * for the chosen algorithm, key type and key size.
2009 *
2010 * The application must call psa_cipher_encrypt_setup() before
2011 * calling this function.
2012 *
2013 * If this function returns an error status, the operation becomes inactive.
2014 *
2015 * \param[in,out] operation Active cipher operation.
2016 * \param[out] iv Buffer where the generated IV is to be written.
2017 * \param iv_size Size of the \p iv buffer in bytes.
2018 * \param[out] iv_length On success, the number of bytes of the
2019 * generated IV.
2020 *
2021 * \retval #PSA_SUCCESS
2022 * Success.
2023 * \retval #PSA_ERROR_BAD_STATE
2024 * The operation state is not valid (not started, or IV already set).
2025 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2026 * The size of the \p iv buffer is too small.
2027 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2028 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2029 * \retval #PSA_ERROR_HARDWARE_FAILURE
2030 * \retval #PSA_ERROR_TAMPERING_DETECTED
2031 */
2032psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
2033 unsigned char *iv,
2034 size_t iv_size,
2035 size_t *iv_length);
2036
2037/** Set the IV for a symmetric encryption or decryption operation.
2038 *
2039 * This function sets the random IV (initialization vector), nonce
2040 * or initial counter value for the encryption or decryption operation.
2041 *
2042 * The application must call psa_cipher_encrypt_setup() before
2043 * calling this function.
2044 *
2045 * If this function returns an error status, the operation becomes inactive.
2046 *
2047 * \note When encrypting, applications should use psa_cipher_generate_iv()
2048 * instead of this function, unless implementing a protocol that requires
2049 * a non-random IV.
2050 *
2051 * \param[in,out] operation Active cipher operation.
2052 * \param[in] iv Buffer containing the IV to use.
2053 * \param iv_length Size of the IV in bytes.
2054 *
2055 * \retval #PSA_SUCCESS
2056 * Success.
2057 * \retval #PSA_ERROR_BAD_STATE
2058 * The operation state is not valid (not started, or IV already set).
2059 * \retval #PSA_ERROR_INVALID_ARGUMENT
2060 * The size of \p iv is not acceptable for the chosen algorithm,
2061 * or the chosen algorithm does not use an IV.
2062 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2063 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2064 * \retval #PSA_ERROR_HARDWARE_FAILURE
2065 * \retval #PSA_ERROR_TAMPERING_DETECTED
2066 */
2067psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
2068 const unsigned char *iv,
2069 size_t iv_length);
2070
2071/** Encrypt or decrypt a message fragment in an active cipher operation.
2072 *
2073 * Before calling this function, you must:
2074 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
2075 * The choice of setup function determines whether this function
2076 * encrypts or decrypts its input.
2077 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
2078 * (recommended when encrypting) or psa_cipher_set_iv().
2079 *
2080 * If this function returns an error status, the operation becomes inactive.
2081 *
2082 * \param[in,out] operation Active cipher operation.
2083 * \param[in] input Buffer containing the message fragment to
2084 * encrypt or decrypt.
2085 * \param input_length Size of the \p input buffer in bytes.
2086 * \param[out] output Buffer where the output is to be written.
2087 * \param output_size Size of the \p output buffer in bytes.
2088 * \param[out] output_length On success, the number of bytes
2089 * that make up the returned output.
2090 *
2091 * \retval #PSA_SUCCESS
2092 * Success.
2093 * \retval #PSA_ERROR_BAD_STATE
2094 * The operation state is not valid (not started, IV required but
2095 * not set, or already completed).
2096 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2097 * The size of the \p output buffer is too small.
2098 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2099 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2100 * \retval #PSA_ERROR_HARDWARE_FAILURE
2101 * \retval #PSA_ERROR_TAMPERING_DETECTED
2102 */
2103psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2104 const uint8_t *input,
2105 size_t input_length,
2106 unsigned char *output,
2107 size_t output_size,
2108 size_t *output_length);
2109
2110/** Finish encrypting or decrypting a message in a cipher operation.
2111 *
2112 * The application must call psa_cipher_encrypt_setup() or
2113 * psa_cipher_decrypt_setup() before calling this function. The choice
2114 * of setup function determines whether this function encrypts or
2115 * decrypts its input.
2116 *
2117 * This function finishes the encryption or decryption of the message
2118 * formed by concatenating the inputs passed to preceding calls to
2119 * psa_cipher_update().
2120 *
2121 * When this function returns, the operation becomes inactive.
2122 *
2123 * \param[in,out] operation Active cipher operation.
2124 * \param[out] output Buffer where the output is to be written.
2125 * \param output_size Size of the \p output buffer in bytes.
2126 * \param[out] output_length On success, the number of bytes
2127 * that make up the returned output.
2128 *
2129 * \retval #PSA_SUCCESS
2130 * Success.
2131 * \retval #PSA_ERROR_BAD_STATE
2132 * The operation state is not valid (not started, IV required but
2133 * not set, or already completed).
2134 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2135 * The size of the \p output buffer is too small.
2136 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2137 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2138 * \retval #PSA_ERROR_HARDWARE_FAILURE
2139 * \retval #PSA_ERROR_TAMPERING_DETECTED
2140 */
2141psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
2142 uint8_t *output,
2143 size_t output_size,
2144 size_t *output_length);
2145
2146/** Abort a cipher operation.
2147 *
2148 * Aborting an operation frees all associated resources except for the
2149 * \p operation structure itself. Once aborted, the operation object
2150 * can be reused for another operation by calling
2151 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
2152 *
2153 * You may call this function any time after the operation object has
2154 * been initialized by any of the following methods:
2155 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2156 * whether it succeeds or not.
2157 * - Initializing the \c struct to all-bits-zero.
2158 * - Initializing the \c struct to logical zeros, e.g.
2159 * `psa_cipher_operation_t operation = {0}`.
2160 *
2161 * In particular, calling psa_cipher_abort() after the operation has been
2162 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2163 * is safe and has no effect.
2164 *
2165 * \param[in,out] operation Initialized cipher operation.
2166 *
2167 * \retval #PSA_SUCCESS
2168 * \retval #PSA_ERROR_BAD_STATE
2169 * \p operation is not an active cipher operation.
2170 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2171 * \retval #PSA_ERROR_HARDWARE_FAILURE
2172 * \retval #PSA_ERROR_TAMPERING_DETECTED
2173 */
2174psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2175
2176/**@}*/
2177
2178/** \defgroup aead Authenticated encryption with associated data (AEAD)
2179 * @{
2180 */
2181
2182/** The tag size for an AEAD algorithm, in bytes.
2183 *
2184 * \param alg An AEAD algorithm
2185 * (\c PSA_ALG_XXX value such that
2186 * #PSA_ALG_IS_AEAD(\p alg) is true).
2187 *
2188 * \return The tag size for the specified algorithm.
2189 * If the AEAD algorithm does not have an identified
2190 * tag that can be distinguished from the rest of
2191 * the ciphertext, return 0.
2192 * If the AEAD algorithm is not recognized, return 0.
2193 * An implementation may return either 0 or a
2194 * correct size for an AEAD algorithm that it
2195 * recognizes, but does not support.
2196 */
2197#define PSA_AEAD_TAG_SIZE(alg) \
2198 ((alg) == PSA_ALG_GCM ? 16 : \
2199 (alg) == PSA_ALG_CCM ? 16 : \
2200 0)
2201
2202/** Process an authenticated encryption operation.
2203 *
2204 * \param key Slot containing the key to use.
2205 * \param alg The AEAD algorithm to compute
2206 * (\c PSA_ALG_XXX value such that
2207 * #PSA_ALG_IS_AEAD(\p alg) is true).
2208 * \param[in] nonce Nonce or IV to use.
2209 * \param nonce_length Size of the \p nonce buffer in bytes.
2210 * \param[in] additional_data Additional data that will be authenticated
2211 * but not encrypted.
2212 * \param additional_data_length Size of \p additional_data in bytes.
2213 * \param[in] plaintext Data that will be authenticated and
2214 * encrypted.
2215 * \param plaintext_length Size of \p plaintext in bytes.
2216 * \param[out] ciphertext Output buffer for the authenticated and
2217 * encrypted data. The additional data is not
2218 * part of this output. For algorithms where the
2219 * encrypted data and the authentication tag
2220 * are defined as separate outputs, the
2221 * authentication tag is appended to the
2222 * encrypted data.
2223 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2224 * This must be at least
2225 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2226 * \p plaintext_length).
2227 * \param[out] ciphertext_length On success, the size of the output
2228 * in the \b ciphertext buffer.
2229 *
2230 * \retval #PSA_SUCCESS
2231 * Success.
2232 * \retval #PSA_ERROR_EMPTY_SLOT
2233 * \retval #PSA_ERROR_NOT_PERMITTED
2234 * \retval #PSA_ERROR_INVALID_ARGUMENT
2235 * \p key is not compatible with \p alg.
2236 * \retval #PSA_ERROR_NOT_SUPPORTED
2237 * \p alg is not supported or is not an AEAD algorithm.
2238 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2239 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2240 * \retval #PSA_ERROR_HARDWARE_FAILURE
2241 * \retval #PSA_ERROR_TAMPERING_DETECTED
2242 */
2243psa_status_t psa_aead_encrypt(psa_key_slot_t key,
2244 psa_algorithm_t alg,
2245 const uint8_t *nonce,
2246 size_t nonce_length,
2247 const uint8_t *additional_data,
2248 size_t additional_data_length,
2249 const uint8_t *plaintext,
2250 size_t plaintext_length,
2251 uint8_t *ciphertext,
2252 size_t ciphertext_size,
2253 size_t *ciphertext_length);
2254
2255/** Process an authenticated decryption operation.
2256 *
2257 * \param key Slot containing the key to use.
2258 * \param alg The AEAD algorithm to compute
2259 * (\c PSA_ALG_XXX value such that
2260 * #PSA_ALG_IS_AEAD(\p alg) is true).
2261 * \param[in] nonce Nonce or IV to use.
2262 * \param nonce_length Size of the \p nonce buffer in bytes.
2263 * \param[in] additional_data Additional data that has been authenticated
2264 * but not encrypted.
2265 * \param additional_data_length Size of \p additional_data in bytes.
2266 * \param[in] ciphertext Data that has been authenticated and
2267 * encrypted. For algorithms where the
2268 * encrypted data and the authentication tag
2269 * are defined as separate inputs, the buffer
2270 * must contain the encrypted data followed
2271 * by the authentication tag.
2272 * \param ciphertext_length Size of \p ciphertext in bytes.
2273 * \param[out] plaintext Output buffer for the decrypted data.
2274 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2275 * This must be at least
2276 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2277 * \p ciphertext_length).
2278 * \param[out] plaintext_length On success, the size of the output
2279 * in the \b plaintext buffer.
2280 *
2281 * \retval #PSA_SUCCESS
2282 * Success.
2283 * \retval #PSA_ERROR_EMPTY_SLOT
2284 * \retval #PSA_ERROR_INVALID_SIGNATURE
2285 * The ciphertext is not authentic.
2286 * \retval #PSA_ERROR_NOT_PERMITTED
2287 * \retval #PSA_ERROR_INVALID_ARGUMENT
2288 * \p key is not compatible with \p alg.
2289 * \retval #PSA_ERROR_NOT_SUPPORTED
2290 * \p alg is not supported or is not an AEAD algorithm.
2291 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2292 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2293 * \retval #PSA_ERROR_HARDWARE_FAILURE
2294 * \retval #PSA_ERROR_TAMPERING_DETECTED
2295 */
2296psa_status_t psa_aead_decrypt(psa_key_slot_t key,
2297 psa_algorithm_t alg,
2298 const uint8_t *nonce,
2299 size_t nonce_length,
2300 const uint8_t *additional_data,
2301 size_t additional_data_length,
2302 const uint8_t *ciphertext,
2303 size_t ciphertext_length,
2304 uint8_t *plaintext,
2305 size_t plaintext_size,
2306 size_t *plaintext_length);
2307
2308/**@}*/
2309
2310/** \defgroup asymmetric Asymmetric cryptography
2311 * @{
2312 */
2313
2314/**
2315 * \brief ECDSA signature size for a given curve bit size
2316 *
2317 * \param curve_bits Curve size in bits.
2318 * \return Signature size in bytes.
2319 *
2320 * \note This macro returns a compile-time constant if its argument is one.
2321 */
2322#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
2323 (PSA_BITS_TO_BYTES(curve_bits) * 2)
2324
2325/**
2326 * \brief Sign a hash or short message with a private key.
2327 *
2328 * Note that to perform a hash-and-sign signature algorithm, you must
2329 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
2330 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2331 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2332 * to determine the hash algorithm to use.
2333 *
2334 * \param key Key slot containing an asymmetric key pair.
2335 * \param alg A signature algorithm that is compatible with
2336 * the type of \p key.
2337 * \param[in] hash The hash or message to sign.
2338 * \param hash_length Size of the \p hash buffer in bytes.
2339 * \param[out] signature Buffer where the signature is to be written.
2340 * \param signature_size Size of the \p signature buffer in bytes.
2341 * \param[out] signature_length On success, the number of bytes
2342 * that make up the returned signature value.
2343 *
2344 * \retval #PSA_SUCCESS
2345 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2346 * The size of the \p signature buffer is too small. You can
2347 * determine a sufficient buffer size by calling
2348 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2349 * where \c key_type and \c key_bits are the type and bit-size
2350 * respectively of \p key.
2351 * \retval #PSA_ERROR_NOT_SUPPORTED
2352 * \retval #PSA_ERROR_INVALID_ARGUMENT
2353 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2354 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2355 * \retval #PSA_ERROR_HARDWARE_FAILURE
2356 * \retval #PSA_ERROR_TAMPERING_DETECTED
2357 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2358 */
2359psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
2360 psa_algorithm_t alg,
2361 const uint8_t *hash,
2362 size_t hash_length,
2363 uint8_t *signature,
2364 size_t signature_size,
2365 size_t *signature_length);
2366
2367/**
2368 * \brief Verify the signature a hash or short message using a public key.
2369 *
2370 * Note that to perform a hash-and-sign signature algorithm, you must
2371 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
2372 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2373 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2374 * to determine the hash algorithm to use.
2375 *
2376 * \param key Key slot containing a public key or an
2377 * asymmetric key pair.
2378 * \param alg A signature algorithm that is compatible with
2379 * the type of \p key.
2380 * \param[in] hash The hash or message whose signature is to be
2381 * verified.
2382 * \param hash_length Size of the \p hash buffer in bytes.
2383 * \param[in] signature Buffer containing the signature to verify.
2384 * \param signature_length Size of the \p signature buffer in bytes.
2385 *
2386 * \retval #PSA_SUCCESS
2387 * The signature is valid.
2388 * \retval #PSA_ERROR_INVALID_SIGNATURE
2389 * The calculation was perfomed successfully, but the passed
2390 * signature is not a valid signature.
2391 * \retval #PSA_ERROR_NOT_SUPPORTED
2392 * \retval #PSA_ERROR_INVALID_ARGUMENT
2393 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2394 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2395 * \retval #PSA_ERROR_HARDWARE_FAILURE
2396 * \retval #PSA_ERROR_TAMPERING_DETECTED
2397 */
2398psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
2399 psa_algorithm_t alg,
2400 const uint8_t *hash,
2401 size_t hash_length,
2402 const uint8_t *signature,
2403 size_t signature_length);
2404
2405#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
2406 (PSA_ALG_IS_RSA_OAEP(alg) ? \
2407 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
2408 11 /*PKCS#1v1.5*/)
2409
2410/**
2411 * \brief Encrypt a short message with a public key.
2412 *
2413 * \param key Key slot containing a public key or an
2414 * asymmetric key pair.
2415 * \param alg An asymmetric encryption algorithm that is
2416 * compatible with the type of \p key.
2417 * \param[in] input The message to encrypt.
2418 * \param input_length Size of the \p input buffer in bytes.
2419 * \param[in] salt A salt or label, if supported by the
2420 * encryption algorithm.
2421 * If the algorithm does not support a
2422 * salt, pass \c NULL.
2423 * If the algorithm supports an optional
2424 * salt and you do not want to pass a salt,
2425 * pass \c NULL.
2426 *
2427 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2428 * supported.
2429 * \param salt_length Size of the \p salt buffer in bytes.
2430 * If \p salt is \c NULL, pass 0.
2431 * \param[out] output Buffer where the encrypted message is to
2432 * be written.
2433 * \param output_size Size of the \p output buffer in bytes.
2434 * \param[out] output_length On success, the number of bytes
2435 * that make up the returned output.
2436 *
2437 * \retval #PSA_SUCCESS
2438 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2439 * The size of the \p output buffer is too small. You can
2440 * determine a sufficient buffer size by calling
2441 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2442 * where \c key_type and \c key_bits are the type and bit-size
2443 * respectively of \p key.
2444 * \retval #PSA_ERROR_NOT_SUPPORTED
2445 * \retval #PSA_ERROR_INVALID_ARGUMENT
2446 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2447 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2448 * \retval #PSA_ERROR_HARDWARE_FAILURE
2449 * \retval #PSA_ERROR_TAMPERING_DETECTED
2450 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2451 */
2452psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
2453 psa_algorithm_t alg,
2454 const uint8_t *input,
2455 size_t input_length,
2456 const uint8_t *salt,
2457 size_t salt_length,
2458 uint8_t *output,
2459 size_t output_size,
2460 size_t *output_length);
2461
2462/**
2463 * \brief Decrypt a short message with a private key.
2464 *
2465 * \param key Key slot containing an asymmetric key pair.
2466 * \param alg An asymmetric encryption algorithm that is
2467 * compatible with the type of \p key.
2468 * \param[in] input The message to decrypt.
2469 * \param input_length Size of the \p input buffer in bytes.
2470 * \param[in] salt A salt or label, if supported by the
2471 * encryption algorithm.
2472 * If the algorithm does not support a
2473 * salt, pass \c NULL.
2474 * If the algorithm supports an optional
2475 * salt and you do not want to pass a salt,
2476 * pass \c NULL.
2477 *
2478 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2479 * supported.
2480 * \param salt_length Size of the \p salt buffer in bytes.
2481 * If \p salt is \c NULL, pass 0.
2482 * \param[out] output Buffer where the decrypted message is to
2483 * be written.
2484 * \param output_size Size of the \c output buffer in bytes.
2485 * \param[out] output_length On success, the number of bytes
2486 * that make up the returned output.
2487 *
2488 * \retval #PSA_SUCCESS
2489 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2490 * The size of the \p output buffer is too small. You can
2491 * determine a sufficient buffer size by calling
2492 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
2493 * where \c key_type and \c key_bits are the type and bit-size
2494 * respectively of \p key.
2495 * \retval #PSA_ERROR_NOT_SUPPORTED
2496 * \retval #PSA_ERROR_INVALID_ARGUMENT
2497 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2498 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2499 * \retval #PSA_ERROR_HARDWARE_FAILURE
2500 * \retval #PSA_ERROR_TAMPERING_DETECTED
2501 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2502 * \retval #PSA_ERROR_INVALID_PADDING
2503 */
2504psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
2505 psa_algorithm_t alg,
2506 const uint8_t *input,
2507 size_t input_length,
2508 const uint8_t *salt,
2509 size_t salt_length,
2510 uint8_t *output,
2511 size_t output_size,
2512 size_t *output_length);
2513
2514/**@}*/
2515
2516/** \defgroup generators Generators
2517 * @{
2518 */
2519
2520/** The type of the state data structure for generators.
2521 *
2522 * Before calling any function on a generator, the application must
2523 * initialize it by any of the following means:
2524 * - Set the structure to all-bits-zero, for example:
2525 * \code
2526 * psa_crypto_generator_t generator;
2527 * memset(&generator, 0, sizeof(generator));
2528 * \endcode
2529 * - Initialize the structure to logical zero values, for example:
2530 * \code
2531 * psa_crypto_generator_t generator = {0};
2532 * \endcode
2533 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2534 * for example:
2535 * \code
2536 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2537 * \endcode
2538 * - Assign the result of the function psa_crypto_generator_init()
2539 * to the structure, for example:
2540 * \code
2541 * psa_crypto_generator_t generator;
2542 * generator = psa_crypto_generator_init();
2543 * \endcode
2544 *
2545 * This is an implementation-defined \c struct. Applications should not
2546 * make any assumptions about the content of this structure except
2547 * as directed by the documentation of a specific implementation.
2548 */
2549typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2550
2551/** \def PSA_CRYPTO_GENERATOR_INIT
2552 *
2553 * This macro returns a suitable initializer for a generator object
2554 * of type #psa_crypto_generator_t.
2555 */
2556#ifdef __DOXYGEN_ONLY__
2557/* This is an example definition for documentation purposes.
2558 * Implementations should define a suitable value in `crypto_struct.h`.
2559 */
2560#define PSA_CRYPTO_GENERATOR_INIT {0}
2561#endif
2562
2563/** Return an initial value for a generator object.
2564 */
2565static psa_crypto_generator_t psa_crypto_generator_init(void);
2566
2567/** Retrieve the current capacity of a generator.
2568 *
2569 * The capacity of a generator is the maximum number of bytes that it can
2570 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2571 *
2572 * \param[in] generator The generator to query.
2573 * \param[out] capacity On success, the capacity of the generator.
2574 *
2575 * \retval PSA_SUCCESS
2576 * \retval PSA_ERROR_BAD_STATE
2577 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2578 */
2579psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2580 size_t *capacity);
2581
2582/** Read some data from a generator.
2583 *
2584 * This function reads and returns a sequence of bytes from a generator.
2585 * The data that is read is discarded from the generator. The generator's
2586 * capacity is decreased by the number of bytes read.
2587 *
2588 * \param[in,out] generator The generator object to read from.
2589 * \param[out] output Buffer where the generator output will be
2590 * written.
2591 * \param output_length Number of bytes to output.
2592 *
2593 * \retval PSA_SUCCESS
2594 * \retval PSA_ERROR_INSUFFICIENT_CAPACITY
2595 * There were fewer than \p output_length bytes
2596 * in the generator. Note that in this case, no
2597 * output is written to the output buffer.
2598 * The generator's capacity is set to 0, thus
2599 * subsequent calls to this function will not
2600 * succeed, even with a smaller output buffer.
2601 * \retval PSA_ERROR_BAD_STATE
2602 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
2603 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2604 * \retval PSA_ERROR_HARDWARE_FAILURE
2605 * \retval PSA_ERROR_TAMPERING_DETECTED
2606 */
2607psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2608 uint8_t *output,
2609 size_t output_length);
2610
2611/** Create a symmetric key from data read from a generator.
2612 *
2613 * This function reads a sequence of bytes from a generator and imports
2614 * these bytes as a key.
2615 * The data that is read is discarded from the generator. The generator's
2616 * capacity is decreased by the number of bytes read.
2617 *
2618 * This function is equivalent to calling #psa_generator_read and
2619 * passing the resulting output to #psa_import_key, but
2620 * if the implementation provides an isolation boundary then
2621 * the key material is not exposed outside the isolation boundary.
2622 *
2623 * \param key Slot where the key will be stored. This must be a
2624 * valid slot for a key of the chosen type. It must
2625 * be unoccupied.
2626 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2627 * This must be a symmetric key type.
2628 * \param bits Key size in bits.
2629 * \param[in,out] generator The generator object to read from.
2630 *
2631 * \retval PSA_SUCCESS
2632 * Success.
2633 * \retval PSA_ERROR_INSUFFICIENT_CAPACITY
2634 * There were fewer than \p output_length bytes
2635 * in the generator. Note that in this case, no
2636 * output is written to the output buffer.
2637 * The generator's capacity is set to 0, thus
2638 * subsequent calls to this function will not
2639 * succeed, even with a smaller output buffer.
2640 * \retval PSA_ERROR_NOT_SUPPORTED
2641 * The key type or key size is not supported, either by the
2642 * implementation in general or in this particular slot.
2643 * \retval PSA_ERROR_BAD_STATE
2644 * \retval PSA_ERROR_INVALID_ARGUMENT
2645 * The key slot is invalid.
2646 * \retval PSA_ERROR_OCCUPIED_SLOT
2647 * There is already a key in the specified slot.
2648 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
2649 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
2650 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2651 * \retval PSA_ERROR_HARDWARE_FAILURE
2652 * \retval PSA_ERROR_TAMPERING_DETECTED
2653 */
2654psa_status_t psa_generator_import_key(psa_key_slot_t key,
2655 psa_key_type_t type,
2656 size_t bits,
2657 psa_crypto_generator_t *generator);
2658
2659/** Abort a generator.
2660 *
2661 * Once a generator has been aborted, its capacity is zero.
2662 * Aborting a generator frees all associated resources except for the
2663 * \c generator structure itself.
2664 *
2665 * This function may be called at any time as long as the generator
2666 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2667 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2668 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2669 * on a generator that has not been set up.
2670 *
2671 * Once aborted, the generator object may be called.
2672 *
2673 * \param[in,out] generator The generator to abort.
2674 *
2675 * \retval PSA_SUCCESS
2676 * \retval PSA_ERROR_BAD_STATE
2677 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2678 * \retval PSA_ERROR_HARDWARE_FAILURE
2679 * \retval PSA_ERROR_TAMPERING_DETECTED
2680 */
2681psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2682
2683/**@}*/
2684
2685/** \defgroup derivation Key derivation
2686 * @{
2687 */
2688
2689/** Set up a key derivation operation.
2690 *
2691 * A key derivation algorithm takes three inputs: a secret input \p key and
2692 * two non-secret inputs \p label and p salt.
2693 * The result of this function is a byte generator which can
2694 * be used to produce keys and other cryptographic material.
2695 *
2696 * The role of \p label and \p salt is as follows:
2697 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2698 * and \p label is the info string used in the "expand" step.
2699 *
2700 * \param[in,out] generator The generator object to set up. It must
2701 * have been initialized to .
2702 * \param key Slot containing the secret key to use.
2703 * \param alg The key derivation algorithm to compute
2704 * (\c PSA_ALG_XXX value such that
2705 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2706 * \param[in] salt Salt to use.
2707 * \param salt_length Size of the \p salt buffer in bytes.
2708 * \param[in] label Label to use.
2709 * \param label_length Size of the \p label buffer in bytes.
2710 * \param capacity The maximum number of bytes that the
2711 * generator will be able to provide.
2712 *
2713 * \retval #PSA_SUCCESS
2714 * Success.
2715 * \retval #PSA_ERROR_EMPTY_SLOT
2716 * \retval #PSA_ERROR_NOT_PERMITTED
2717 * \retval #PSA_ERROR_INVALID_ARGUMENT
2718 * \c key is not compatible with \c alg,
2719 * or \p capacity is too large for the specified algorithm and key.
2720 * \retval #PSA_ERROR_NOT_SUPPORTED
2721 * \c alg is not supported or is not a key derivation algorithm.
2722 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2723 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2724 * \retval #PSA_ERROR_HARDWARE_FAILURE
2725 * \retval #PSA_ERROR_TAMPERING_DETECTED
2726 */
2727psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
2728 psa_key_slot_t key,
2729 psa_algorithm_t alg,
2730 const uint8_t *salt,
2731 size_t salt_length,
2732 const uint8_t *label,
2733 size_t label_length,
2734 size_t capacity);
2735
2736/**@}*/
2737
2738/** \defgroup random Random generation
2739 * @{
2740 */
2741
2742/**
2743 * \brief Generate random bytes.
2744 *
2745 * \warning This function **can** fail! Callers MUST check the return status
2746 * and MUST NOT use the content of the output buffer if the return
2747 * status is not #PSA_SUCCESS.
2748 *
2749 * \note To generate a key, use psa_generate_key() instead.
2750 *
2751 * \param[out] output Output buffer for the generated data.
2752 * \param output_size Number of bytes to generate and output.
2753 *
2754 * \retval #PSA_SUCCESS
2755 * \retval #PSA_ERROR_NOT_SUPPORTED
2756 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2757 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2758 * \retval #PSA_ERROR_HARDWARE_FAILURE
2759 * \retval #PSA_ERROR_TAMPERING_DETECTED
2760 */
2761psa_status_t psa_generate_random(uint8_t *output,
2762 size_t output_size);
2763
2764/** Extra parameters for RSA key generation.
2765 *
2766 * You may pass a pointer to a structure of this type as the \c extra
2767 * parameter to psa_generate_key().
2768 */
2769typedef struct {
2770 uint32_t e; /**< Public exponent value. Default: 65537. */
2771} psa_generate_key_extra_rsa;
2772
2773/**
2774 * \brief Generate a key or key pair.
2775 *
2776 * \param key Slot where the key will be stored. This must be a
2777 * valid slot for a key of the chosen type. It must
2778 * be unoccupied.
2779 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2780 * \param bits Key size in bits.
2781 * \param[in] extra Extra parameters for key generation. The
2782 * interpretation of this parameter depends on
2783 * \p type. All types support \c NULL to use
2784 * default parameters. Implementation that support
2785 * the generation of vendor-specific key types
2786 * that allow extra parameters shall document
2787 * the format of these extra parameters and
2788 * the default values. For standard parameters,
2789 * the meaning of \p extra is as follows:
2790 * - For a symmetric key type (a type such
2791 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2792 * false), \p extra must be \c NULL.
2793 * - For an elliptic curve key type (a type
2794 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2795 * false), \p extra must be \c NULL.
2796 * - For an RSA key (\p type is
2797 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2798 * optional #psa_generate_key_extra_rsa structure
2799 * specifying the public exponent. The
2800 * default public exponent used when \p extra
2801 * is \c NULL is 65537.
2802 * \param extra_size Size of the buffer that \p extra
2803 * points to, in bytes. Note that if \p extra is
2804 * \c NULL then \p extra_size must be zero.
2805 *
2806 * \retval #PSA_SUCCESS
2807 * \retval #PSA_ERROR_NOT_SUPPORTED
2808 * \retval #PSA_ERROR_INVALID_ARGUMENT
2809 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2810 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2811 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2812 * \retval #PSA_ERROR_HARDWARE_FAILURE
2813 * \retval #PSA_ERROR_TAMPERING_DETECTED
2814 */
2815psa_status_t psa_generate_key(psa_key_slot_t key,
2816 psa_key_type_t type,
2817 size_t bits,
2818 const void *extra,
2819 size_t extra_size);
2820
2821/**@}*/
2822
2823#ifdef __cplusplus
2824}
2825#endif
2826
2827/* The file "crypto_sizes.h" contains definitions for size calculation
2828 * macros whose definitions are implementation-specific. */
2829#include "crypto_sizes.h"
2830
2831/* The file "crypto_struct.h" contains definitions for
2832 * implementation-specific structs that are declared above. */
2833#include "crypto_struct.h"
2834
2835/* The file "crypto_extra.h" contains vendor-specific definitions. This
2836 * can include vendor-defined algorithms, extra functions, etc. */
2837#include "crypto_extra.h"
2838
2839#endif /* PSA_CRYPTO_H */