blob: 8db3767ad52885ee3bdc8fb1647df0419475e1e5 [file] [log] [blame]
Antonio de Angelis14276e92018-07-10 14:35:43 +01001/*
Mate Toth-Pal2a6f8c22018-12-13 16:37:17 +01002 * Copyright (c) 2018-2019, Arm Limited. All rights reserved.
Antonio de Angelis14276e92018-07-10 14:35:43 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01007/**
8 * \file psa_crypto.h
9 * \brief Platform Security Architecture cryptography module
10 */
11
12#ifndef PSA_CRYPTO_H
13#define PSA_CRYPTO_H
14
Antonio de Angelis8908f472018-08-31 15:44:25 +010015#include "psa_crypto_platform.h"
16
Antonio de Angelis14276e92018-07-10 14:35:43 +010017#include <stddef.h>
18
19#ifdef __DOXYGEN_ONLY__
20/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
21 * must be defined in the psa_crypto_platform.h header. These mock definitions
22 * are present in this file as a convenience to generate pretty-printed
Antonio de Angelis377a1552018-11-22 17:02:40 +000023 * documentation that includes those definitions. */
Antonio de Angelis14276e92018-07-10 14:35:43 +010024
25/** \defgroup platform Implementation-specific definitions
26 * @{
27 */
28
29/** \brief Key slot number.
30 *
31 * This type represents key slots. It must be an unsigned integral
32 * type. The choice of type is implementation-dependent.
33 * 0 is not a valid key slot number. The meaning of other values is
34 * implementation dependent.
35 *
36 * At any given point in time, each key slot either contains a
37 * cryptographic object, or is empty. Key slots are persistent:
38 * once set, the cryptographic object remains in the key slot until
39 * explicitly destroyed.
40 */
41typedef _unsigned_integral_type_ psa_key_slot_t;
42
43/**@}*/
44#endif /* __DOXYGEN_ONLY__ */
45
46#ifdef __cplusplus
47extern "C" {
48#endif
49
50/** \defgroup basic Basic definitions
51 * @{
52 */
53
Mate Toth-Pal2a6f8c22018-12-13 16:37:17 +010054#if !defined(PSA_SUCCESS)
Antonio de Angelis14276e92018-07-10 14:35:43 +010055
Antonio de Angelis377a1552018-11-22 17:02:40 +000056/**
57 * \brief Function return status.
58 *
59 * This is either #PSA_SUCCESS (which is zero), indicating success,
60 * or a nonzero value indicating that an error occurred. Errors are
61 * encoded as one of the \c PSA_ERROR_xxx values defined here.
62 */
63typedef int32_t psa_status_t;
64
Antonio de Angelis14276e92018-07-10 14:35:43 +010065/** The action was completed successfully. */
66#define PSA_SUCCESS ((psa_status_t)0)
67
68#endif /* !defined(PSA_SUCCESS) */
69
Antonio de Angelis377a1552018-11-22 17:02:40 +000070/** An error occurred that does not correspond to any defined
71 * failure cause.
72 *
73 * Implementations may use this error code if none of the other standard
74 * error codes are applicable. */
75#define PSA_ERROR_UNKNOWN_ERROR ((psa_status_t)1)
76
Antonio de Angelis14276e92018-07-10 14:35:43 +010077/** The requested operation or a parameter is not supported
78 * by this implementation.
79 *
80 * Implementations should return this error code when an enumeration
81 * parameter such as a key type, algorithm, etc. is not recognized.
82 * If a combination of parameters is recognized and identified as
Antonio de Angelis377a1552018-11-22 17:02:40 +000083 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
84#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)2)
Antonio de Angelis14276e92018-07-10 14:35:43 +010085
86/** The requested action is denied by a policy.
87 *
88 * Implementations should return this error code when the parameters
89 * are recognized as valid and supported, and a policy explicitly
90 * denies the requested operation.
91 *
92 * If a subset of the parameters of a function call identify a
93 * forbidden operation, and another subset of the parameters are
94 * not valid or not supported, it is unspecified whether the function
95 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
Antonio de Angelis377a1552018-11-22 17:02:40 +000096 * #PSA_ERROR_INVALID_ARGUMENT. */
97#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)3)
Antonio de Angelis14276e92018-07-10 14:35:43 +010098
99/** An output buffer is too small.
100 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000101 * Applications can call the \c PSA_xxx_SIZE macro listed in the function
Antonio de Angelis14276e92018-07-10 14:35:43 +0100102 * description to determine a sufficient buffer size.
103 *
104 * Implementations should preferably return this error code only
105 * in cases when performing the operation with a larger output
106 * buffer would succeed. However implementations may return this
107 * error if a function has invalid or unsupported parameters in addition
Antonio de Angelis377a1552018-11-22 17:02:40 +0000108 * to the parameters that determine the necessary output buffer size. */
109#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)4)
Antonio de Angelis14276e92018-07-10 14:35:43 +0100110
111/** A slot is occupied, but must be empty to carry out the
112 * requested action.
113 *
114 * If the slot number is invalid (i.e. the requested action could
115 * not be performed even after erasing the slot's content),
Antonio de Angelis377a1552018-11-22 17:02:40 +0000116 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
117#define PSA_ERROR_OCCUPIED_SLOT ((psa_status_t)5)
Antonio de Angelis14276e92018-07-10 14:35:43 +0100118
119/** A slot is empty, but must be occupied to carry out the
120 * requested action.
121 *
122 * If the slot number is invalid (i.e. the requested action could
123 * not be performed even after creating appropriate content in the slot),
Antonio de Angelis377a1552018-11-22 17:02:40 +0000124 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
125#define PSA_ERROR_EMPTY_SLOT ((psa_status_t)6)
Antonio de Angelis14276e92018-07-10 14:35:43 +0100126
127/** The requested action cannot be performed in the current state.
128 *
129 * Multipart operations return this error when one of the
130 * functions is called out of sequence. Refer to the function
131 * descriptions for permitted sequencing of functions.
132 *
133 * Implementations shall not return this error code to indicate
134 * that a key slot is occupied when it needs to be free or vice versa,
135 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
Antonio de Angelis377a1552018-11-22 17:02:40 +0000136 * as applicable. */
137#define PSA_ERROR_BAD_STATE ((psa_status_t)7)
Antonio de Angelis14276e92018-07-10 14:35:43 +0100138
139/** The parameters passed to the function are invalid.
140 *
141 * Implementations may return this error any time a parameter or
142 * combination of parameters are recognized as invalid.
143 *
144 * Implementations shall not return this error code to indicate
145 * that a key slot is occupied when it needs to be free or vice versa,
146 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
Antonio de Angelis377a1552018-11-22 17:02:40 +0000147 * as applicable. */
148#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)8)
Antonio de Angelis14276e92018-07-10 14:35:43 +0100149
150/** There is not enough runtime memory.
151 *
152 * If the action is carried out across multiple security realms, this
Antonio de Angelis377a1552018-11-22 17:02:40 +0000153 * error can refer to available memory in any of the security realms. */
154#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)9)
Antonio de Angelis14276e92018-07-10 14:35:43 +0100155
156/** There is not enough persistent storage.
157 *
158 * Functions that modify the key storage return this error code if
159 * there is insufficient storage space on the host media. In addition,
160 * many functions that do not otherwise access storage may return this
161 * error code if the implementation requires a mandatory log entry for
Antonio de Angelis377a1552018-11-22 17:02:40 +0000162 * the requested action and the log storage space is full. */
163#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)10)
Antonio de Angelis14276e92018-07-10 14:35:43 +0100164
165/** There was a communication failure inside the implementation.
166 *
167 * This can indicate a communication failure between the application
168 * and an external cryptoprocessor or between the cryptoprocessor and
169 * an external volatile or persistent memory. A communication failure
170 * may be transient or permanent depending on the cause.
171 *
172 * \warning If a function returns this error, it is undetermined
173 * whether the requested action has completed or not. Implementations
174 * should return #PSA_SUCCESS on successful completion whenver
175 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
176 * if the requested action was completed successfully in an external
177 * cryptoprocessor but there was a breakdown of communication before
178 * the cryptoprocessor could report the status to the application.
179 */
Antonio de Angelis377a1552018-11-22 17:02:40 +0000180#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)11)
Antonio de Angelis14276e92018-07-10 14:35:43 +0100181
182/** There was a storage failure that may have led to data loss.
183 *
184 * This error indicates that some persistent storage is corrupted.
185 * It should not be used for a corruption of volatile memory
186 * (use #PSA_ERROR_TAMPERING_DETECTED), for a communication error
187 * between the cryptoprocessor and its external storage (use
188 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
189 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
190 *
191 * Note that a storage failure does not indicate that any data that was
192 * previously read is invalid. However this previously read data may no
193 * longer be readable from storage.
194 *
195 * When a storage failure occurs, it is no longer possible to ensure
196 * the global integrity of the keystore. Depending on the global
197 * integrity guarantees offered by the implementation, access to other
198 * data may or may not fail even if the data is still readable but
199 * its integrity canont be guaranteed.
200 *
201 * Implementations should only use this error code to report a
202 * permanent storage corruption. However application writers should
203 * keep in mind that transient errors while reading the storage may be
Antonio de Angelis377a1552018-11-22 17:02:40 +0000204 * reported using this error code. */
205#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)12)
Antonio de Angelis14276e92018-07-10 14:35:43 +0100206
207/** A hardware failure was detected.
208 *
209 * A hardware failure may be transient or permanent depending on the
Antonio de Angelis377a1552018-11-22 17:02:40 +0000210 * cause. */
211#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)13)
Antonio de Angelis14276e92018-07-10 14:35:43 +0100212
213/** A tampering attempt was detected.
214 *
215 * If an application receives this error code, there is no guarantee
216 * that previously accessed or computed data was correct and remains
217 * confidential. Applications should not perform any security function
218 * and should enter a safe failure state.
219 *
220 * Implementations may return this error code if they detect an invalid
221 * state that cannot happen during normal operation and that indicates
222 * that the implementation's security guarantees no longer hold. Depending
223 * on the implementation architecture and on its security and safety goals,
224 * the implementation may forcibly terminate the application.
225 *
226 * This error code is intended as a last resort when a security breach
227 * is detected and it is unsure whether the keystore data is still
228 * protected. Implementations shall only return this error code
229 * to report an alarm from a tampering detector, to indicate that
230 * the confidentiality of stored data can no longer be guaranteed,
231 * or to indicate that the integrity of previously returned data is now
232 * considered compromised. Implementations shall not use this error code
233 * to indicate a hardware failure that merely makes it impossible to
234 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
235 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
236 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
237 * instead).
238 *
239 * This error indicates an attack against the application. Implementations
240 * shall not return this error code as a consequence of the behavior of
Antonio de Angelis377a1552018-11-22 17:02:40 +0000241 * the application itself. */
242#define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)14)
Antonio de Angelis14276e92018-07-10 14:35:43 +0100243
244/** There is not enough entropy to generate random data needed
245 * for the requested action.
246 *
247 * This error indicates a failure of a hardware random generator.
248 * Application writers should note that this error can be returned not
249 * only by functions whose purpose is to generate random data, such
250 * as key, IV or nonce generation, but also by functions that execute
251 * an algorithm with a randomized result, as well as functions that
252 * use randomization of intermediate computations as a countermeasure
253 * to certain attacks.
254 *
255 * Implementations should avoid returning this error after psa_crypto_init()
256 * has succeeded. Implementations should generate sufficient
257 * entropy during initialization and subsequently use a cryptographically
258 * secure pseudorandom generator (PRNG). However implementations may return
259 * this error at any time if a policy requires the PRNG to be reseeded
Antonio de Angelis377a1552018-11-22 17:02:40 +0000260 * during normal operation. */
261#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)15)
Antonio de Angelis14276e92018-07-10 14:35:43 +0100262
263/** The signature, MAC or hash is incorrect.
264 *
265 * Verification functions return this error if the verification
266 * calculations completed successfully, and the value to be verified
267 * was determined to be incorrect.
268 *
269 * If the value to verify has an invalid size, implementations may return
Antonio de Angelis377a1552018-11-22 17:02:40 +0000270 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
271#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)16)
Antonio de Angelis14276e92018-07-10 14:35:43 +0100272
273/** The decrypted padding is incorrect.
274 *
275 * \warning In some protocols, when decrypting data, it is essential that
276 * the behavior of the application does not depend on whether the padding
277 * is correct, down to precise timing. Applications should prefer
278 * protocols that use authenticated encryption rather than plain
279 * encryption. If the application must perform a decryption of
280 * unauthenticated data, the application writer should take care not
281 * to reveal whether the padding is invalid.
282 *
283 * Implementations should strive to make valid and invalid padding
284 * as close as possible to indistinguishable to an external observer.
285 * In particular, the timing of a decryption operation should not
Antonio de Angelis377a1552018-11-22 17:02:40 +0000286 * depend on the validity of the padding. */
287#define PSA_ERROR_INVALID_PADDING ((psa_status_t)17)
Antonio de Angelis14276e92018-07-10 14:35:43 +0100288
Antonio de Angelis377a1552018-11-22 17:02:40 +0000289/** The generator has insufficient capacity left.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100290 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000291 * Once a function returns this error, attempts to read from the
292 * generator will always return this error. */
293#define PSA_ERROR_INSUFFICIENT_CAPACITY ((psa_status_t)18)
Antonio de Angelis14276e92018-07-10 14:35:43 +0100294
295/**
296 * \brief Library initialization.
297 *
298 * Applications must call this function before calling any other
299 * function in this module.
300 *
301 * Applications may call this function more than once. Once a call
302 * succeeds, subsequent calls are guaranteed to succeed.
303 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000304 * \retval #PSA_SUCCESS
305 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
306 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
307 * \retval #PSA_ERROR_HARDWARE_FAILURE
308 * \retval #PSA_ERROR_TAMPERING_DETECTED
309 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Antonio de Angelis14276e92018-07-10 14:35:43 +0100310 */
311psa_status_t psa_crypto_init(void);
312
313#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
314#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
315
316/**@}*/
317
318/** \defgroup crypto_types Key and algorithm types
319 * @{
320 */
321
Antonio de Angelis377a1552018-11-22 17:02:40 +0000322/** \brief Encoding of a key type.
323 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100324typedef uint32_t psa_key_type_t;
325
326/** An invalid key type value.
327 *
328 * Zero is not the encoding of any key type.
329 */
330#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
331
332/** Vendor-defined flag
333 *
334 * Key types defined by this standard will never have the
335 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
336 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
337 * respect the bitwise structure used by standard encodings whenever practical.
338 */
339#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
340
341#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7e000000)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000342
Antonio de Angelis14276e92018-07-10 14:35:43 +0100343/** Raw data.
344 *
345 * A "key" of this type cannot be used for any cryptographic operation.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000346 * Applications may use this type to store arbitrary data in the keystore. */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100347#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x02000000)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000348
Antonio de Angelis14276e92018-07-10 14:35:43 +0100349#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x04000000)
350#define PSA_KEY_TYPE_CATEGORY_ASYMMETRIC ((psa_key_type_t)0x06000000)
351#define PSA_KEY_TYPE_PAIR_FLAG ((psa_key_type_t)0x01000000)
352
353/** HMAC key.
354 *
355 * The key policy determines which underlying hash algorithm the key can be
356 * used for.
357 *
358 * HMAC keys should generally have the same size as the underlying hash.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000359 * This size can be calculated with #PSA_HASH_SIZE(\c alg) where
360 * \c alg is the HMAC algorithm or the underlying hash algorithm. */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100361#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x02000001)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000362
363/** A secret for key derivation.
364 *
365 * The key policy determines which key derivation algorithm the key
366 * can be used for.
367 */
368#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x02000101)
369
Antonio de Angelis14276e92018-07-10 14:35:43 +0100370/** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher.
371 *
372 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
373 * 32 bytes (AES-256).
374 */
375#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x04000001)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000376
Antonio de Angelis14276e92018-07-10 14:35:43 +0100377/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
378 *
379 * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or
380 * 24 bytes (3-key 3DES).
381 *
382 * Note that single DES and 2-key 3DES are weak and strongly
383 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
384 * is weak and deprecated and should only be used in legacy protocols.
385 */
386#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x04000002)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000387
Antonio de Angelis14276e92018-07-10 14:35:43 +0100388/** Key for an cipher, AEAD or MAC algorithm based on the
Antonio de Angelis377a1552018-11-22 17:02:40 +0000389 * Camellia block cipher. */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100390#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x04000003)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000391
Antonio de Angelis14276e92018-07-10 14:35:43 +0100392/** Key for the RC4 stream cipher.
393 *
394 * Note that RC4 is weak and deprecated and should only be used in
Antonio de Angelis377a1552018-11-22 17:02:40 +0000395 * legacy protocols. */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100396#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x04000004)
397
398/** RSA public key. */
399#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x06010000)
400/** RSA key pair (private and public key). */
401#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x07010000)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000402
Antonio de Angelis14276e92018-07-10 14:35:43 +0100403/** DSA public key. */
404#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x06020000)
405/** DSA key pair (private and public key). */
406#define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x07020000)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000407
Antonio de Angelis14276e92018-07-10 14:35:43 +0100408#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x06030000)
409#define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x07030000)
410#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000411/** Elliptic curve key pair. */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100412#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
413 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
Antonio de Angelis377a1552018-11-22 17:02:40 +0000414/** Elliptic curve public key. */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100415#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
416 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
417
418/** Whether a key type is vendor-defined. */
419#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
420 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
421
422/** Whether a key type is asymmetric: either a key pair or a public key. */
423#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
424 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
425/** Whether a key type is the public part of a key pair. */
426#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
427 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
428 PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
429/** Whether a key type is a key pair containing a private part and a public
Antonio de Angelis377a1552018-11-22 17:02:40 +0000430 * part. */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100431#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
432 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
433 (PSA_KEY_TYPE_CATEGORY_ASYMMETRIC | PSA_KEY_TYPE_PAIR_FLAG))
Antonio de Angelis14276e92018-07-10 14:35:43 +0100434/** The key pair type corresponding to a public key type. */
435#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
436 ((type) | PSA_KEY_TYPE_PAIR_FLAG)
437/** The public key type corresponding to a key pair type. */
438#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
439 ((type) & ~PSA_KEY_TYPE_PAIR_FLAG)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000440/** Whether a key type is an RSA key (pair or public-only). */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100441#define PSA_KEY_TYPE_IS_RSA(type) \
442 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000443
444/** Whether a key type is an elliptic curve key (pair or public-only). */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100445#define PSA_KEY_TYPE_IS_ECC(type) \
446 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
447 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000448#define PSA_KEY_TYPE_IS_ECC_KEYPAIR(type) \
449 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
450 PSA_KEY_TYPE_ECC_KEYPAIR_BASE)
451#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
452 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
453 PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Antonio de Angelis14276e92018-07-10 14:35:43 +0100454
Antonio de Angelis377a1552018-11-22 17:02:40 +0000455/** The type of PSA elliptic curve identifiers. */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100456typedef uint16_t psa_ecc_curve_t;
457/** Extract the curve from an elliptic curve key type. */
458#define PSA_KEY_TYPE_GET_CURVE(type) \
459 ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
460 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
461 0))
462
463/* The encoding of curve identifiers is currently aligned with the
464 * TLS Supported Groups Registry (formerly known as the
465 * TLS EC Named Curve Registry)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000466 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
467 * The values are defined by RFC 4492, RFC 7027 and RFC 7919. */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100468#define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001)
469#define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002)
470#define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003)
471#define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004)
472#define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005)
473#define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006)
474#define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007)
475#define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008)
476#define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009)
477#define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a)
478#define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b)
479#define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c)
480#define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d)
481#define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e)
482#define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f)
483#define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010)
484#define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011)
485#define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012)
486#define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013)
487#define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014)
488#define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015)
489#define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016)
490#define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017)
491#define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018)
492#define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019)
493#define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a)
494#define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b)
495#define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c)
496#define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d)
497#define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e)
498#define PSA_ECC_CURVE_FFDHE_2048 ((psa_ecc_curve_t) 0x0100)
499#define PSA_ECC_CURVE_FFDHE_3072 ((psa_ecc_curve_t) 0x0101)
500#define PSA_ECC_CURVE_FFDHE_4096 ((psa_ecc_curve_t) 0x0102)
501#define PSA_ECC_CURVE_FFDHE_6144 ((psa_ecc_curve_t) 0x0103)
502#define PSA_ECC_CURVE_FFDHE_8192 ((psa_ecc_curve_t) 0x0104)
503
504/** The block size of a block cipher.
505 *
506 * \param type A cipher key type (value of type #psa_key_type_t).
507 *
508 * \return The block size for a block cipher, or 1 for a stream cipher.
Antonio de Angelis377a1552018-11-22 17:02:40 +0000509 * The return value is undefined if \p type is not a supported
Antonio de Angelis14276e92018-07-10 14:35:43 +0100510 * cipher key type.
511 *
512 * \note It is possible to build stream cipher algorithms on top of a block
513 * cipher, for example CTR mode (#PSA_ALG_CTR).
514 * This macro only takes the key type into account, so it cannot be
515 * used to determine the size of the data that #psa_cipher_update()
516 * might buffer for future processing in general.
517 *
518 * \note This macro returns a compile-time constant if its argument is one.
519 *
520 * \warning This macro may evaluate its argument multiple times.
521 */
522#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
523 ( \
524 (type) == PSA_KEY_TYPE_AES ? 16 : \
525 (type) == PSA_KEY_TYPE_DES ? 8 : \
526 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
527 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
528 0)
529
530/** \brief Encoding of a cryptographic algorithm.
531 *
532 * For algorithms that can be applied to multiple key types, this type
533 * does not encode the key type. For example, for symmetric ciphers
534 * based on a block cipher, #psa_algorithm_t encodes the block cipher
535 * mode and the padding mode while the block cipher itself is encoded
536 * via #psa_key_type_t.
537 */
538typedef uint32_t psa_algorithm_t;
539
540#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
541#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
542#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
543#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
544#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
545#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
546#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
547#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
548#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
549#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
550
551#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
552 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000553
Antonio de Angelis14276e92018-07-10 14:35:43 +0100554/** Whether the specified algorithm is a hash algorithm.
555 *
556 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
557 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000558 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
559 * This macro may return either 0 or 1 if \p alg is not a supported
Antonio de Angelis14276e92018-07-10 14:35:43 +0100560 * algorithm identifier.
561 */
562#define PSA_ALG_IS_HASH(alg) \
563 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000564
565/** Whether the specified algorithm is a MAC algorithm.
566 *
567 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
568 *
569 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
570 * This macro may return either 0 or 1 if \p alg is not a supported
571 * algorithm identifier.
572 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100573#define PSA_ALG_IS_MAC(alg) \
574 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000575
576/** Whether the specified algorithm is a symmetric cipher algorithm.
577 *
578 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
579 *
580 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
581 * This macro may return either 0 or 1 if \p alg is not a supported
582 * algorithm identifier.
583 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100584#define PSA_ALG_IS_CIPHER(alg) \
585 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000586
587/** Whether the specified algorithm is an authenticated encryption
588 * with associated data (AEAD) algorithm.
589 *
590 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
591 *
592 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
593 * This macro may return either 0 or 1 if \p alg is not a supported
594 * algorithm identifier.
595 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100596#define PSA_ALG_IS_AEAD(alg) \
597 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000598
599/** Whether the specified algorithm is a public-key signature algorithm.
600 *
601 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
602 *
603 * \return 1 if \p alg is a public-key signature algorithm, 0 otherwise.
604 * This macro may return either 0 or 1 if \p alg is not a supported
605 * algorithm identifier.
606 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100607#define PSA_ALG_IS_SIGN(alg) \
608 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000609
610/** Whether the specified algorithm is a public-key encryption algorithm.
611 *
612 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
613 *
614 * \return 1 if \p alg is a public-key encryption algorithm, 0 otherwise.
615 * This macro may return either 0 or 1 if \p alg is not a supported
616 * algorithm identifier.
617 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100618#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
619 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000620
621/** Whether the specified algorithm is a key agreement algorithm.
622 *
623 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
624 *
625 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
626 * This macro may return either 0 or 1 if \p alg is not a supported
627 * algorithm identifier.
628 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100629#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
630 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000631
632/** Whether the specified algorithm is a key derivation algorithm.
633 *
634 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
635 *
636 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
637 * This macro may return either 0 or 1 if \p alg is not a supported
638 * algorithm identifier.
639 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100640#define PSA_ALG_IS_KEY_DERIVATION(alg) \
641 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
642
643#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
644#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
645#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
646#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
647#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
648#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000649/** SHA2-224 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100650#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000651/** SHA2-256 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100652#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000653/** SHA2-384 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100654#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000655/** SHA2-512 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100656#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000657/** SHA2-512/224 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100658#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000659/** SHA2-512/256 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100660#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000661/** SHA3-224 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100662#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000663/** SHA3-256 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100664#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000665/** SHA3-384 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100666#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000667/** SHA3-512 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100668#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
669
670#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
671#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
672/** Macro to build an HMAC algorithm.
673 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000674 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100675 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000676 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
677 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +0100678 *
Antonio de Angelis377a1552018-11-22 17:02:40 +0000679 * \return The corresponding HMAC algorithm.
680 * \return Unspecified if \p alg is not a supported
681 * hash algorithm.
Antonio de Angelis14276e92018-07-10 14:35:43 +0100682 */
683#define PSA_ALG_HMAC(hash_alg) \
684 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Antonio de Angelis377a1552018-11-22 17:02:40 +0000685
Antonio de Angelis14276e92018-07-10 14:35:43 +0100686#define PSA_ALG_HMAC_HASH(hmac_alg) \
687 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
Antonio de Angelis377a1552018-11-22 17:02:40 +0000688
689/** Whether the specified algorithm is an HMAC algorithm.
690 *
691 * HMAC is a family of MAC algorithms that are based on a hash function.
692 *
693 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
694 *
695 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
696 * This macro may return either 0 or 1 if \p alg is not a supported
697 * algorithm identifier.
698 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100699#define PSA_ALG_IS_HMAC(alg) \
700 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
701 PSA_ALG_HMAC_BASE)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000702
Antonio de Angelis14276e92018-07-10 14:35:43 +0100703#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
704#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
705#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
706#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000707
708/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
709 *
710 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
711 *
712 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
713 * This macro may return either 0 or 1 if \p alg is not a supported
714 * algorithm identifier.
715 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100716#define PSA_ALG_IS_CIPHER_MAC(alg) \
717 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
718 PSA_ALG_CIPHER_MAC_BASE)
719
720#define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
721#define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000)
722#define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff)
723#define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000724
725/** Use a block cipher mode without padding.
726 *
727 * This padding mode may only be used with messages whose lengths are a
728 * whole number of blocks for the chosen block cipher.
729 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100730#define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000731
Antonio de Angelis14276e92018-07-10 14:35:43 +0100732#define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000733
734/** Whether the specified algorithm is a block cipher.
735 *
736 * A block cipher is a symmetric cipher that encrypts or decrypts messages
737 * by chopping them into fixed-size blocks. Processing a message requires
738 * applying a _padding mode_ to transform the message into one whose
739 * length is a whole number of blocks. To construct an algorithm
740 * identifier for a block cipher, apply a bitwise-or between the block
741 * cipher mode and the padding mode. For example, CBC with PKCS#7 padding
742 * is `PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7`.
743 *
744 * The transformation applied to each block is determined by the key type.
745 * For example, to use AES-128-CBC-PKCS7, use the algorithm above with
746 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
747 *
748 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
749 *
750 * \return 1 if \p alg is a block cipher algorithm, 0 otherwise.
751 * This macro may return either 0 or 1 if \p alg is not a supported
752 * algorithm identifier or if it is not a symmetric cipher algorithm.
753 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100754#define PSA_ALG_IS_BLOCK_CIPHER(alg) \
755 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
756 PSA_ALG_BLOCK_CIPHER_BASE)
757
Antonio de Angelis377a1552018-11-22 17:02:40 +0000758/** The CBC block cipher mode.
759 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100760#define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001)
761#define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002)
762#define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003)
763#define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000764
765#define PSA_ALG_STREAM_CIPHER_BASE ((psa_algorithm_t)0x04800000)
766
767/** The CTR stream cipher mode.
768 *
769 * CTR is a stream cipher which is built from a block cipher. The
770 * underlying block cipher is determined by the key type. For example,
771 * to use AES-128-CTR, use this algorithm with
772 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
773 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100774#define PSA_ALG_CTR ((psa_algorithm_t)0x04800001)
Antonio de Angelis377a1552018-11-22 17:02:40 +0000775
776/** The ARC4 stream cipher algorithm.
777 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100778#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002)
779
Antonio de Angelis377a1552018-11-22 17:02:40 +0000780/** Whether the specified algorithm is a stream cipher.
781 *
782 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
783 * by applying a bitwise-xor with a stream of bytes that is generated
784 * from a key.
785 *
786 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
787 *
788 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
789 * This macro may return either 0 or 1 if \p alg is not a supported
790 * algorithm identifier or if it is not a symmetric cipher algorithm.
791 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100792#define PSA_ALG_IS_STREAM_CIPHER(alg) \
793 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000794 PSA_ALG_STREAM_CIPHER_BASE)
Antonio de Angelis14276e92018-07-10 14:35:43 +0100795
796#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
797#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
798
Antonio de Angelis377a1552018-11-22 17:02:40 +0000799#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
800/** RSA PKCS#1 v1.5 signature with hashing.
801 *
802 * This is the signature scheme defined by RFC 8017
803 * (PKCS#1: RSA Cryptography Specifications) under the name
804 * RSASSA-PKCS1-v1_5.
805 *
806 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
807 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
808 *
809 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
810 * \return Unspecified if \p alg is not a supported
811 * hash algorithm.
812 */
Antonio de Angelis14276e92018-07-10 14:35:43 +0100813#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000814 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
815/** Raw PKCS#1 v1.5 signature.
816 *
817 * The input to this algorithm is the DigestInfo structure used by
818 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
819 * steps 3&ndash;6.
820 */
821#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
Antonio de Angelis14276e92018-07-10 14:35:43 +0100822#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Antonio de Angelis377a1552018-11-22 17:02:40 +0000823 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
Antonio de Angelis14276e92018-07-10 14:35:43 +0100824
Antonio de Angelis377a1552018-11-22 17:02:40 +0000825#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
826/** RSA PSS signature with hashing.
827 *
828 * This is the signature scheme defined by RFC 8017
829 * (PKCS#1: RSA Cryptography Specifications) under the name
830 * RSASSA-PSS, with the message generation function MGF1, and with
831 * a salt length equal to the length of the hash. The specified
832 * hash algorithm is used to hash the input message, to create the
833 * salted hash, and for the mask generation.
834 *
835 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
836 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
837 *
838 * \return The corresponding RSA PSS signature algorithm.
839 * \return Unspecified if \p alg is not a supported
840 * hash algorithm.
841 */
842#define PSA_ALG_RSA_PSS(hash_alg) \
843 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
844#define PSA_ALG_IS_RSA_PSS(alg) \
845 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
846
847#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
848/** DSA signature with hashing.
849 *
850 * This is the signature scheme defined by FIPS 186-4,
851 * with a random per-message secret number (*k*).
852 *
853 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
854 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
855 *
856 * \return The corresponding DSA signature algorithm.
857 * \return Unspecified if \p alg is not a supported
858 * hash algorithm.
859 */
860#define PSA_ALG_DSA(hash_alg) \
861 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
862#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
863#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
864#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
865 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
866#define PSA_ALG_IS_DSA(alg) \
867 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
868 PSA_ALG_DSA_BASE)
869#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
870 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
871#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
872 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
873#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
874 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
875
876#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
877/** ECDSA signature with hashing.
878 *
879 * This is the ECDSA signature scheme defined by ANSI X9.62,
880 * with a random per-message secret number (*k*).
881 *
882 * The representation of the signature as a byte string consists of
883 * the concatentation of the signature values *r* and *s*. Each of
884 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
885 * of the base point of the curve in octets. Each value is represented
886 * in big-endian order (most significant octet first).
887 *
888 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
889 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
890 *
891 * \return The corresponding ECDSA signature algorithm.
892 * \return Unspecified if \p alg is not a supported
893 * hash algorithm.
894 */
895#define PSA_ALG_ECDSA(hash_alg) \
896 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
897/** ECDSA signature without hashing.
898 *
899 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
900 * without specifying a hash algorithm. This algorithm may only be
901 * used to sign or verify a sequence of bytes that should be an
902 * already-calculated hash. Note that the input is padded with
903 * zeros on the left or truncated on the left as required to fit
904 * the curve size.
905 */
906#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
907#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
908/** Deterministic ECDSA signature with hashing.
909 *
910 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
911 *
912 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
913 *
914 * Note that when this algorithm is used for verification, signatures
915 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
916 * same private key are accepted. In other words,
917 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
918 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
919 *
920 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
921 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
922 *
923 * \return The corresponding deterministic ECDSA signature
924 * algorithm.
925 * \return Unspecified if \p alg is not a supported
926 * hash algorithm.
927 */
928#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
929 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
930#define PSA_ALG_IS_ECDSA(alg) \
931 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
932 PSA_ALG_ECDSA_BASE)
933#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
934 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
935#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
936 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
937#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
938 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
939
940/** Get the hash used by a hash-and-sign signature algorithm.
941 *
942 * A hash-and-sign algorithm is a signature algorithm which is
943 * composed of two phases: first a hashing phase which does not use
944 * the key and produces a hash of the input message, then a signing
945 * phase which only uses the hash and the key and not the message
946 * itself.
947 *
948 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
949 * #PSA_ALG_IS_SIGN(\p alg) is true).
950 *
951 * \return The underlying hash algorithm if \p alg is a hash-and-sign
952 * algorithm.
953 * \return 0 if \p alg is a signature algorithm that does not
954 * follow the hash-and-sign structure.
955 * \return Unspecified if \p alg is not a signature algorithm or
956 * if it is not supported by the implementation.
957 */
958#define PSA_ALG_SIGN_GET_HASH(alg) \
959 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
960 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg) ? \
961 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
962 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
963 0)
964
965/** RSA PKCS#1 v1.5 encryption.
966 */
967#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
968
969#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
970/** RSA OAEP encryption.
971 *
972 * This is the encryption scheme defined by RFC 8017
973 * (PKCS#1: RSA Cryptography Specifications) under the name
974 * RSAES-OAEP, with the message generation function MGF1.
975 *
976 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
977 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
978 * for MGF1.
979 *
980 * \return The corresponding RSA OAEP signature algorithm.
981 * \return Unspecified if \p alg is not a supported
982 * hash algorithm.
983 */
984#define PSA_ALG_RSA_OAEP(hash_alg) \
985 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
986#define PSA_ALG_IS_RSA_OAEP(alg) \
987 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
988#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
989 (PSA_ALG_IS_RSA_OAEP(alg) ? \
990 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
991 0)
992
993#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x30000100)
994/** Macro to build an HKDF algorithm.
995 *
996 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
997 *
998 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
999 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1000 *
1001 * \return The corresponding HKDF algorithm.
1002 * \return Unspecified if \p alg is not a supported
1003 * hash algorithm.
1004 */
1005#define PSA_ALG_HKDF(hash_alg) \
1006 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1007/** Whether the specified algorithm is an HKDF algorithm.
1008 *
1009 * HKDF is a family of key derivation algorithms that are based on a hash
1010 * function and the HMAC construction.
1011 *
1012 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1013 *
1014 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1015 * This macro may return either 0 or 1 if \c alg is not a supported
1016 * key derivation algorithm identifier.
1017 */
1018#define PSA_ALG_IS_HKDF(alg) \
1019 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1020#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1021 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
Antonio de Angelis14276e92018-07-10 14:35:43 +01001022
1023/**@}*/
1024
1025/** \defgroup key_management Key management
1026 * @{
1027 */
1028
1029/**
1030 * \brief Import a key in binary format.
1031 *
1032 * This function supports any output from psa_export_key(). Refer to the
1033 * documentation of psa_export_key() for the format for each key type.
1034 *
1035 * \param key Slot where the key will be stored. This must be a
1036 * valid slot for a key of the chosen type. It must
1037 * be unoccupied.
1038 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001039 * \param[in] data Buffer containing the key data.
1040 * \param data_length Size of the \p data buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001041 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001042 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001043 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001044 * \retval #PSA_ERROR_NOT_SUPPORTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01001045 * The key type or key size is not supported, either by the
1046 * implementation in general or in this particular slot.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001047 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis14276e92018-07-10 14:35:43 +01001048 * The key slot is invalid,
1049 * or the key data is not correctly formatted.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001050 * \retval #PSA_ERROR_OCCUPIED_SLOT
Antonio de Angelis14276e92018-07-10 14:35:43 +01001051 * There is already a key in the specified slot.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001052 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1053 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1054 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1055 * \retval #PSA_ERROR_HARDWARE_FAILURE
1056 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01001057 */
1058psa_status_t psa_import_key(psa_key_slot_t key,
1059 psa_key_type_t type,
1060 const uint8_t *data,
1061 size_t data_length);
1062
1063/**
1064 * \brief Destroy a key and restore the slot to its default state.
1065 *
1066 * This function destroys the content of the key slot from both volatile
1067 * memory and, if applicable, non-volatile storage. Implementations shall
1068 * make a best effort to ensure that any previous content of the slot is
1069 * unrecoverable.
1070 *
1071 * This function also erases any metadata such as policies. It returns the
1072 * specified slot to its default state.
1073 *
1074 * \param key The key slot to erase.
1075 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001076 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001077 * The slot's content, if any, has been erased.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001078 * \retval #PSA_ERROR_NOT_PERMITTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01001079 * The slot holds content and cannot be erased because it is
1080 * read-only, either due to a policy or due to physical restrictions.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001081 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis14276e92018-07-10 14:35:43 +01001082 * The specified slot number does not designate a valid slot.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001083 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Antonio de Angelis14276e92018-07-10 14:35:43 +01001084 * There was an failure in communication with the cryptoprocessor.
1085 * The key material may still be present in the cryptoprocessor.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001086 * \retval #PSA_ERROR_STORAGE_FAILURE
Antonio de Angelis14276e92018-07-10 14:35:43 +01001087 * The storage is corrupted. Implementations shall make a best effort
1088 * to erase key material even in this stage, however applications
1089 * should be aware that it may be impossible to guarantee that the
1090 * key material is not recoverable in such cases.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001091 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01001092 * An unexpected condition which is not a storage corruption or
1093 * a communication failure occurred. The cryptoprocessor may have
1094 * been compromised.
1095 */
1096psa_status_t psa_destroy_key(psa_key_slot_t key);
1097
1098/**
1099 * \brief Get basic metadata about a key.
1100 *
1101 * \param key Slot whose content is queried. This must
1102 * be an occupied key slot.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001103 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Antonio de Angelis14276e92018-07-10 14:35:43 +01001104 * This may be a null pointer, in which case the key type
1105 * is not written.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001106 * \param[out] bits On success, the key size in bits.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001107 * This may be a null pointer, in which case the key size
1108 * is not written.
1109 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001110 * \retval #PSA_SUCCESS
1111 * \retval #PSA_ERROR_EMPTY_SLOT
1112 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1113 * \retval #PSA_ERROR_HARDWARE_FAILURE
1114 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01001115 */
1116psa_status_t psa_get_key_information(psa_key_slot_t key,
1117 psa_key_type_t *type,
1118 size_t *bits);
1119
1120/**
1121 * \brief Export a key in binary format.
1122 *
1123 * The output of this function can be passed to psa_import_key() to
1124 * create an equivalent object.
1125 *
1126 * If a key is created with psa_import_key() and then exported with
1127 * this function, it is not guaranteed that the resulting data is
1128 * identical: the implementation may choose a different representation
1129 * of the same key if the format permits it.
1130 *
1131 * For standard key types, the output format is as follows:
1132 *
1133 * - For symmetric keys (including MAC keys), the format is the
1134 * raw bytes of the key.
1135 * - For DES, the key data consists of 8 bytes. The parity bits must be
1136 * correct.
1137 * - For Triple-DES, the format is the concatenation of the
1138 * two or three DES keys.
1139 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Antonio de Angelis377a1552018-11-22 17:02:40 +00001140 * is the non-encrypted DER representation defined by PKCS\#1 (RFC 8017)
1141 * as RSAPrivateKey.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001142 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
1143 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
1144 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001145 * \param key Slot whose content is to be exported. This must
1146 * be an occupied key slot.
1147 * \param[out] data Buffer where the key data is to be written.
1148 * \param data_size Size of the \p data buffer in bytes.
1149 * \param[out] data_length On success, the number of bytes
1150 * that make up the key data.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001151 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001152 * \retval #PSA_SUCCESS
1153 * \retval #PSA_ERROR_EMPTY_SLOT
1154 * \retval #PSA_ERROR_NOT_PERMITTED
1155 * \retval #PSA_ERROR_NOT_SUPPORTED
1156 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1157 * \retval #PSA_ERROR_HARDWARE_FAILURE
1158 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01001159 */
1160psa_status_t psa_export_key(psa_key_slot_t key,
1161 uint8_t *data,
1162 size_t data_size,
1163 size_t *data_length);
1164
1165/**
1166 * \brief Export a public key or the public part of a key pair in binary format.
1167 *
1168 * The output of this function can be passed to psa_import_key() to
1169 * create an object that is equivalent to the public key.
1170 *
1171 * For standard key types, the output format is as follows:
1172 *
1173 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
1174 * the format is the DER representation of the public key defined by RFC 5280
1175 * as SubjectPublicKeyInfo.
1176 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001177 * \param key Slot whose content is to be exported. This must
1178 * be an occupied key slot.
1179 * \param[out] data Buffer where the key data is to be written.
1180 * \param data_size Size of the \p data buffer in bytes.
1181 * \param[out] data_length On success, the number of bytes
1182 * that make up the key data.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001183 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001184 * \retval #PSA_SUCCESS
1185 * \retval #PSA_ERROR_EMPTY_SLOT
1186 * \retval #PSA_ERROR_INVALID_ARGUMENT
1187 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1188 * \retval #PSA_ERROR_HARDWARE_FAILURE
1189 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01001190 */
1191psa_status_t psa_export_public_key(psa_key_slot_t key,
1192 uint8_t *data,
1193 size_t data_size,
1194 size_t *data_length);
1195
1196/**@}*/
1197
1198/** \defgroup policy Key policies
1199 * @{
1200 */
1201
1202/** \brief Encoding of permitted usage on a key. */
1203typedef uint32_t psa_key_usage_t;
1204
1205/** Whether the key may be exported.
1206 *
1207 * A public key or the public part of a key pair may always be exported
1208 * regardless of the value of this permission flag.
1209 *
1210 * If a key does not have export permission, implementations shall not
1211 * allow the key to be exported in plain form from the cryptoprocessor,
1212 * whether through psa_export_key() or through a proprietary interface.
1213 * The key may however be exportable in a wrapped form, i.e. in a form
1214 * where it is encrypted by another key.
1215 */
1216#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1217
1218/** Whether the key may be used to encrypt a message.
1219 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001220 * This flag allows the key to be used for a symmetric encryption operation,
1221 * for an AEAD encryption-and-authentication operation,
1222 * or for an asymmetric encryption operation,
1223 * if otherwise permitted by the key's type and policy.
1224 *
Antonio de Angelis14276e92018-07-10 14:35:43 +01001225 * For a key pair, this concerns the public key.
1226 */
1227#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
1228
1229/** Whether the key may be used to decrypt a message.
1230 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001231 * This flag allows the key to be used for a symmetric decryption operation,
1232 * for an AEAD decryption-and-verification operation,
1233 * or for an asymmetric decryption operation,
1234 * if otherwise permitted by the key's type and policy.
1235 *
Antonio de Angelis14276e92018-07-10 14:35:43 +01001236 * For a key pair, this concerns the private key.
1237 */
1238#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
1239
1240/** Whether the key may be used to sign a message.
1241 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001242 * This flag allows the key to be used for a MAC calculation operation
1243 * or for an asymmetric signature operation,
1244 * if otherwise permitted by the key's type and policy.
1245 *
Antonio de Angelis14276e92018-07-10 14:35:43 +01001246 * For a key pair, this concerns the private key.
1247 */
1248#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
1249
1250/** Whether the key may be used to verify a message signature.
1251 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001252 * This flag allows the key to be used for a MAC verification operation
1253 * or for an asymmetric signature verification operation,
1254 * if otherwise permitted by by the key's type and policy.
1255 *
Antonio de Angelis14276e92018-07-10 14:35:43 +01001256 * For a key pair, this concerns the public key.
1257 */
1258#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
1259
Antonio de Angelis377a1552018-11-22 17:02:40 +00001260/** Whether the key may be used to derive other keys.
Antonio de Angelis8908f472018-08-31 15:44:25 +01001261 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001262#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00001000)
1263
1264/** The type of the key policy data structure.
1265 *
1266 * This is an implementation-defined \c struct. Applications should not
1267 * make any assumptions about the content of this structure except
1268 * as directed by the documentation of a specific implementation. */
1269typedef struct psa_key_policy_s psa_key_policy_t;
Antonio de Angelis14276e92018-07-10 14:35:43 +01001270
1271/** \brief Initialize a key policy structure to a default that forbids all
Antonio de Angelis8908f472018-08-31 15:44:25 +01001272 * usage of the key.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001273 *
1274 * \param[out] policy The policy object to initialize.
Antonio de Angelis8908f472018-08-31 15:44:25 +01001275 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001276void psa_key_policy_init(psa_key_policy_t *policy);
1277
1278/** \brief Set the standard fields of a policy structure.
1279 *
1280 * Note that this function does not make any consistency check of the
1281 * parameters. The values are only checked when applying the policy to
1282 * a key slot with psa_set_key_policy().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001283 *
1284 * \param[out] policy The policy object to modify.
1285 * \param usage The permitted uses for the key.
1286 * \param alg The algorithm that the key may be used for.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001287 */
1288void psa_key_policy_set_usage(psa_key_policy_t *policy,
1289 psa_key_usage_t usage,
1290 psa_algorithm_t alg);
1291
Antonio de Angelis377a1552018-11-22 17:02:40 +00001292/** \brief Retrieve the usage field of a policy structure.
1293 *
1294 * \param[in] policy The policy object to query.
1295 *
1296 * \return The permitted uses for a key with this policy.
1297 */
1298psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001299
Antonio de Angelis377a1552018-11-22 17:02:40 +00001300/** \brief Retrieve the algorithm field of a policy structure.
1301 *
1302 * \param[in] policy The policy object to query.
1303 *
1304 * \return The permitted algorithm for a key with this policy.
1305 */
1306psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001307
1308/** \brief Set the usage policy on a key slot.
1309 *
1310 * This function must be called on an empty key slot, before importing,
1311 * generating or creating a key in the slot. Changing the policy of an
1312 * existing key is not permitted.
1313 *
1314 * Implementations may set restrictions on supported key policies
1315 * depending on the key type and the key slot.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001316 *
1317 * \param key The key slot whose policy is to be changed.
1318 * \param[in] policy The policy object to query.
1319 *
1320 * \retval #PSA_SUCCESS
1321 * \retval #PSA_ERROR_OCCUPIED_SLOT
1322 * \retval #PSA_ERROR_NOT_SUPPORTED
1323 * \retval #PSA_ERROR_INVALID_ARGUMENT
1324 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1325 * \retval #PSA_ERROR_HARDWARE_FAILURE
1326 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01001327 */
1328psa_status_t psa_set_key_policy(psa_key_slot_t key,
1329 const psa_key_policy_t *policy);
1330
Antonio de Angelis377a1552018-11-22 17:02:40 +00001331/** \brief Get the usage policy for a key slot.
1332 *
1333 * \param key The key slot whose policy is being queried.
1334 * \param[out] policy On success, the key's policy.
1335 *
1336 * \retval #PSA_SUCCESS
1337 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1338 * \retval #PSA_ERROR_HARDWARE_FAILURE
1339 * \retval #PSA_ERROR_TAMPERING_DETECTED
1340 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001341psa_status_t psa_get_key_policy(psa_key_slot_t key,
1342 psa_key_policy_t *policy);
1343
1344/**@}*/
1345
1346/** \defgroup persistence Key lifetime
1347 * @{
1348 */
1349
Antonio de Angelis377a1552018-11-22 17:02:40 +00001350/** Encoding of key lifetimes.
1351 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001352typedef uint32_t psa_key_lifetime_t;
1353
1354/** A volatile key slot retains its content as long as the application is
1355 * running. It is guaranteed to be erased on a power reset.
1356 */
1357#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1358
1359/** A persistent key slot retains its content as long as it is not explicitly
1360 * destroyed.
1361 */
1362#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1363
1364/** A write-once key slot may not be modified once a key has been set.
1365 * It will retain its content as long as the device remains operational.
1366 */
1367#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
1368
1369/** \brief Retrieve the lifetime of a key slot.
1370 *
1371 * The assignment of lifetimes to slots is implementation-dependent.
1372 *
1373 * \param key Slot to query.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001374 * \param[out] lifetime On success, the lifetime value.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001375 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001376 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001377 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001378 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis14276e92018-07-10 14:35:43 +01001379 * The key slot is invalid.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001380 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1381 * \retval #PSA_ERROR_HARDWARE_FAILURE
1382 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01001383 */
1384psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
1385 psa_key_lifetime_t *lifetime);
1386
1387/** \brief Change the lifetime of a key slot.
1388 *
1389 * Whether the lifetime of a key slot can be changed at all, and if so
1390 * whether the lifetime of an occupied key slot can be changed, is
1391 * implementation-dependent.
1392 *
1393 * \param key Slot whose lifetime is to be changed.
1394 * \param lifetime The lifetime value to set for the given key slot.
1395 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001396 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001397 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001398 * \retval #PSA_ERROR_INVALID_ARGUMENT
Antonio de Angelis14276e92018-07-10 14:35:43 +01001399 * The key slot is invalid,
1400 * or the lifetime value is invalid.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001401 * \retval #PSA_ERROR_NOT_SUPPORTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01001402 * The implementation does not support the specified lifetime value,
1403 * at least for the specified key slot.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001404 * \retval #PSA_ERROR_OCCUPIED_SLOT
Antonio de Angelis14276e92018-07-10 14:35:43 +01001405 * The slot contains a key, and the implementation does not support
1406 * changing the lifetime of an occupied slot.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001407 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1408 * \retval #PSA_ERROR_HARDWARE_FAILURE
1409 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01001410 */
1411psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
1412 psa_key_lifetime_t lifetime);
1413
1414/**@}*/
1415
1416/** \defgroup hash Message digests
1417 * @{
1418 */
1419
Antonio de Angelis377a1552018-11-22 17:02:40 +00001420/** The type of the state data structure for multipart hash operations.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001421 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001422 * This is an implementation-defined \c struct. Applications should not
1423 * make any assumptions about the content of this structure except
1424 * as directed by the documentation of a specific implementation. */
1425typedef struct psa_hash_operation_s psa_hash_operation_t;
Antonio de Angelis14276e92018-07-10 14:35:43 +01001426
1427/** The size of the output of psa_hash_finish(), in bytes.
1428 *
1429 * This is also the hash size that psa_hash_verify() expects.
1430 *
1431 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
Antonio de Angelis377a1552018-11-22 17:02:40 +00001432 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
1433 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
Antonio de Angelis14276e92018-07-10 14:35:43 +01001434 * hash algorithm).
1435 *
1436 * \return The hash size for the specified hash algorithm.
1437 * If the hash algorithm is not recognized, return 0.
1438 * An implementation may return either 0 or the correct size
1439 * for a hash algorithm that it recognizes, but does not support.
1440 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001441#define PSA_HASH_SIZE(alg) \
1442 ( \
1443 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD2 ? 16 : \
1444 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD4 ? 16 : \
1445 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD5 ? 16 : \
1446 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
1447 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
1448 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
1449 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
1450 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
1451 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
1452 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
1453 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
1454 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
1455 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
1456 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
1457 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
Antonio de Angelis14276e92018-07-10 14:35:43 +01001458 0)
1459
1460/** Start a multipart hash operation.
1461 *
1462 * The sequence of operations to calculate a hash (message digest)
1463 * is as follows:
1464 * -# Allocate an operation object which will be passed to all the functions
1465 * listed here.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001466 * -# Call psa_hash_setup() to specify the algorithm.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001467 * -# Call psa_hash_update() zero, one or more times, passing a fragment
1468 * of the message each time. The hash that is calculated is the hash
1469 * of the concatenation of these messages in order.
1470 * -# To calculate the hash, call psa_hash_finish().
1471 * To compare the hash with an expected value, call psa_hash_verify().
1472 *
1473 * The application may call psa_hash_abort() at any time after the operation
Antonio de Angelis377a1552018-11-22 17:02:40 +00001474 * has been initialized with psa_hash_setup().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001475 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001476 * After a successful call to psa_hash_setup(), the application must
Antonio de Angelis14276e92018-07-10 14:35:43 +01001477 * eventually terminate the operation. The following events terminate an
1478 * operation:
1479 * - A failed call to psa_hash_update().
1480 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
1481 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001482 * \param[out] operation The operation object to use.
1483 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1484 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +01001485 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001486 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001487 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001488 * \retval #PSA_ERROR_NOT_SUPPORTED
1489 * \p alg is not supported or is not a hash algorithm.
1490 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1491 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1492 * \retval #PSA_ERROR_HARDWARE_FAILURE
1493 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01001494 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001495psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Antonio de Angelis14276e92018-07-10 14:35:43 +01001496 psa_algorithm_t alg);
1497
1498/** Add a message fragment to a multipart hash operation.
1499 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001500 * The application must call psa_hash_setup() before calling this function.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001501 *
1502 * If this function returns an error status, the operation becomes inactive.
1503 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001504 * \param[in,out] operation Active hash operation.
1505 * \param[in] input Buffer containing the message fragment to hash.
1506 * \param input_length Size of the \p input buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001507 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001508 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001509 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001510 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis14276e92018-07-10 14:35:43 +01001511 * The operation state is not valid (not started, or already completed).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001512 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1513 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1514 * \retval #PSA_ERROR_HARDWARE_FAILURE
1515 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01001516 */
1517psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1518 const uint8_t *input,
1519 size_t input_length);
1520
1521/** Finish the calculation of the hash of a message.
1522 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001523 * The application must call psa_hash_setup() before calling this function.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001524 * This function calculates the hash of the message formed by concatenating
1525 * the inputs passed to preceding calls to psa_hash_update().
1526 *
1527 * When this function returns, the operation becomes inactive.
1528 *
1529 * \warning Applications should not call this function if they expect
1530 * a specific value for the hash. Call psa_hash_verify() instead.
1531 * Beware that comparing integrity or authenticity data such as
1532 * hash values with a function such as \c memcmp is risky
1533 * because the time taken by the comparison may leak information
1534 * about the hashed data which could allow an attacker to guess
1535 * a valid hash and thereby bypass security controls.
1536 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001537 * \param[in,out] operation Active hash operation.
1538 * \param[out] hash Buffer where the hash is to be written.
1539 * \param hash_size Size of the \p hash buffer in bytes.
1540 * \param[out] hash_length On success, the number of bytes
1541 * that make up the hash value. This is always
1542 * #PSA_HASH_SIZE(\c alg) where \c alg is the
1543 * hash algorithm that is calculated.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001544 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001545 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001546 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001547 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis14276e92018-07-10 14:35:43 +01001548 * The operation state is not valid (not started, or already completed).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001549 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1550 * The size of the \p hash buffer is too small. You can determine a
1551 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01001552 * where \c alg is the hash algorithm that is calculated.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001553 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1554 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1555 * \retval #PSA_ERROR_HARDWARE_FAILURE
1556 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01001557 */
1558psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1559 uint8_t *hash,
1560 size_t hash_size,
1561 size_t *hash_length);
1562
1563/** Finish the calculation of the hash of a message and compare it with
1564 * an expected value.
1565 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001566 * The application must call psa_hash_setup() before calling this function.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001567 * This function calculates the hash of the message formed by concatenating
1568 * the inputs passed to preceding calls to psa_hash_update(). It then
1569 * compares the calculated hash with the expected hash passed as a
1570 * parameter to this function.
1571 *
1572 * When this function returns, the operation becomes inactive.
1573 *
1574 * \note Implementations shall make the best effort to ensure that the
1575 * comparison between the actual hash and the expected hash is performed
1576 * in constant time.
1577 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001578 * \param[in,out] operation Active hash operation.
1579 * \param[in] hash Buffer containing the expected hash value.
1580 * \param hash_length Size of the \p hash buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001581 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001582 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001583 * The expected hash is identical to the actual hash of the message.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001584 * \retval #PSA_ERROR_INVALID_SIGNATURE
Antonio de Angelis14276e92018-07-10 14:35:43 +01001585 * The hash of the message was calculated successfully, but it
1586 * differs from the expected hash.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001587 * \retval #PSA_ERROR_BAD_STATE
Antonio de Angelis14276e92018-07-10 14:35:43 +01001588 * The operation state is not valid (not started, or already completed).
Antonio de Angelis377a1552018-11-22 17:02:40 +00001589 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1590 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1591 * \retval #PSA_ERROR_HARDWARE_FAILURE
1592 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01001593 */
1594psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1595 const uint8_t *hash,
1596 size_t hash_length);
1597
1598/** Abort a hash operation.
1599 *
Antonio de Angelis14276e92018-07-10 14:35:43 +01001600 * Aborting an operation frees all associated resources except for the
Antonio de Angelis377a1552018-11-22 17:02:40 +00001601 * \p operation structure itself. Once aborted, the operation object
1602 * can be reused for another operation by calling
1603 * psa_hash_setup() again.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001604 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001605 * You may call this function any time after the operation object has
1606 * been initialized by any of the following methods:
1607 * - A call to psa_hash_setup(), whether it succeeds or not.
1608 * - Initializing the \c struct to all-bits-zero.
1609 * - Initializing the \c struct to logical zeros, e.g.
1610 * `psa_hash_operation_t operation = {0}`.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001611 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001612 * In particular, calling psa_hash_abort() after the operation has been
1613 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1614 * psa_hash_verify() is safe and has no effect.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001615 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001616 * \param[in,out] operation Initialized hash operation.
1617 *
1618 * \retval #PSA_SUCCESS
1619 * \retval #PSA_ERROR_BAD_STATE
1620 * \p operation is not an active hash operation.
1621 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1622 * \retval #PSA_ERROR_HARDWARE_FAILURE
1623 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01001624 */
1625psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
1626
1627/**@}*/
1628
1629/** \defgroup MAC Message authentication codes
1630 * @{
1631 */
1632
Antonio de Angelis377a1552018-11-22 17:02:40 +00001633/** The type of the state data structure for multipart MAC operations.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001634 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001635 * This is an implementation-defined \c struct. Applications should not
1636 * make any assumptions about the content of this structure except
1637 * as directed by the documentation of a specific implementation. */
1638typedef struct psa_mac_operation_s psa_mac_operation_t;
Antonio de Angelis14276e92018-07-10 14:35:43 +01001639
Antonio de Angelis377a1552018-11-22 17:02:40 +00001640/** Start a multipart MAC calculation operation.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001641 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001642 * This function sets up the calculation of the MAC
1643 * (message authentication code) of a byte string.
1644 * To verify the MAC of a message against an
1645 * expected value, use psa_mac_verify_setup() instead.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001646 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001647 * The sequence of operations to calculate a MAC is as follows:
Antonio de Angelis14276e92018-07-10 14:35:43 +01001648 * -# Allocate an operation object which will be passed to all the functions
1649 * listed here.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001650 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001651 * The key remains associated with the operation even if the content
1652 * of the key slot changes.
1653 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1654 * of the message each time. The MAC that is calculated is the MAC
1655 * of the concatenation of these messages in order.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001656 * -# At the end of the message, call psa_mac_sign_finish() to finish
1657 * calculating the MAC value and retrieve it.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001658 *
1659 * The application may call psa_mac_abort() at any time after the operation
Antonio de Angelis377a1552018-11-22 17:02:40 +00001660 * has been initialized with psa_mac_sign_setup().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001661 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001662 * After a successful call to psa_mac_sign_setup(), the application must
1663 * eventually terminate the operation through one of the following methods:
Antonio de Angelis14276e92018-07-10 14:35:43 +01001664 * - A failed call to psa_mac_update().
Antonio de Angelis377a1552018-11-22 17:02:40 +00001665 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001666 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001667 * \param[out] operation The operation object to use.
1668 * \param key Slot containing the key to use for the operation.
1669 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1670 * such that #PSA_ALG_IS_MAC(alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +01001671 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001672 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001673 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001674 * \retval #PSA_ERROR_EMPTY_SLOT
1675 * \retval #PSA_ERROR_NOT_PERMITTED
1676 * \retval #PSA_ERROR_INVALID_ARGUMENT
1677 * \p key is not compatible with \p alg.
1678 * \retval #PSA_ERROR_NOT_SUPPORTED
1679 * \p alg is not supported or is not a MAC algorithm.
1680 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1681 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1682 * \retval #PSA_ERROR_HARDWARE_FAILURE
1683 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01001684 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001685psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
1686 psa_key_slot_t key,
1687 psa_algorithm_t alg);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001688
Antonio de Angelis377a1552018-11-22 17:02:40 +00001689/** Start a multipart MAC verification operation.
1690 *
1691 * This function sets up the verification of the MAC
1692 * (message authentication code) of a byte string against an expected value.
1693 *
1694 * The sequence of operations to verify a MAC is as follows:
1695 * -# Allocate an operation object which will be passed to all the functions
1696 * listed here.
1697 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1698 * The key remains associated with the operation even if the content
1699 * of the key slot changes.
1700 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1701 * of the message each time. The MAC that is calculated is the MAC
1702 * of the concatenation of these messages in order.
1703 * -# At the end of the message, call psa_mac_verify_finish() to finish
1704 * calculating the actual MAC of the message and verify it against
1705 * the expected value.
1706 *
1707 * The application may call psa_mac_abort() at any time after the operation
1708 * has been initialized with psa_mac_verify_setup().
1709 *
1710 * After a successful call to psa_mac_verify_setup(), the application must
1711 * eventually terminate the operation through one of the following methods:
1712 * - A failed call to psa_mac_update().
1713 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1714 *
1715 * \param[out] operation The operation object to use.
1716 * \param key Slot containing the key to use for the operation.
1717 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1718 * such that #PSA_ALG_IS_MAC(\p alg) is true).
1719 *
1720 * \retval #PSA_SUCCESS
1721 * Success.
1722 * \retval #PSA_ERROR_EMPTY_SLOT
1723 * \retval #PSA_ERROR_NOT_PERMITTED
1724 * \retval #PSA_ERROR_INVALID_ARGUMENT
1725 * \c key is not compatible with \c alg.
1726 * \retval #PSA_ERROR_NOT_SUPPORTED
1727 * \c alg is not supported or is not a MAC algorithm.
1728 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1729 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1730 * \retval #PSA_ERROR_HARDWARE_FAILURE
1731 * \retval #PSA_ERROR_TAMPERING_DETECTED
1732 */
1733psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
1734 psa_key_slot_t key,
1735 psa_algorithm_t alg);
1736
1737/** Add a message fragment to a multipart MAC operation.
1738 *
1739 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1740 * before calling this function.
1741 *
1742 * If this function returns an error status, the operation becomes inactive.
1743 *
1744 * \param[in,out] operation Active MAC operation.
1745 * \param[in] input Buffer containing the message fragment to add to
1746 * the MAC calculation.
1747 * \param input_length Size of the \p input buffer in bytes.
1748 *
1749 * \retval #PSA_SUCCESS
1750 * Success.
1751 * \retval #PSA_ERROR_BAD_STATE
1752 * The operation state is not valid (not started, or already completed).
1753 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1754 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1755 * \retval #PSA_ERROR_HARDWARE_FAILURE
1756 * \retval #PSA_ERROR_TAMPERING_DETECTED
1757 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001758psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1759 const uint8_t *input,
1760 size_t input_length);
1761
Antonio de Angelis377a1552018-11-22 17:02:40 +00001762/** Finish the calculation of the MAC of a message.
1763 *
1764 * The application must call psa_mac_sign_setup() before calling this function.
1765 * This function calculates the MAC of the message formed by concatenating
1766 * the inputs passed to preceding calls to psa_mac_update().
1767 *
1768 * When this function returns, the operation becomes inactive.
1769 *
1770 * \warning Applications should not call this function if they expect
1771 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1772 * Beware that comparing integrity or authenticity data such as
1773 * MAC values with a function such as \c memcmp is risky
1774 * because the time taken by the comparison may leak information
1775 * about the MAC value which could allow an attacker to guess
1776 * a valid MAC and thereby bypass security controls.
1777 *
1778 * \param[in,out] operation Active MAC operation.
1779 * \param[out] mac Buffer where the MAC value is to be written.
1780 * \param mac_size Size of the \p mac buffer in bytes.
1781 * \param[out] mac_length On success, the number of bytes
1782 * that make up the MAC value. This is always
1783 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
1784 * where \c key_type and \c key_bits are the type and
1785 * bit-size respectively of the key and \c alg is the
1786 * MAC algorithm that is calculated.
1787 *
1788 * \retval #PSA_SUCCESS
1789 * Success.
1790 * \retval #PSA_ERROR_BAD_STATE
1791 * The operation state is not valid (not started, or already completed).
1792 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1793 * The size of the \p mac buffer is too small. You can determine a
1794 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1795 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1796 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1797 * \retval #PSA_ERROR_HARDWARE_FAILURE
1798 * \retval #PSA_ERROR_TAMPERING_DETECTED
1799 */
1800psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1801 uint8_t *mac,
1802 size_t mac_size,
1803 size_t *mac_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001804
Antonio de Angelis377a1552018-11-22 17:02:40 +00001805/** Finish the calculation of the MAC of a message and compare it with
1806 * an expected value.
1807 *
1808 * The application must call psa_mac_verify_setup() before calling this function.
1809 * This function calculates the MAC of the message formed by concatenating
1810 * the inputs passed to preceding calls to psa_mac_update(). It then
1811 * compares the calculated MAC with the expected MAC passed as a
1812 * parameter to this function.
1813 *
1814 * When this function returns, the operation becomes inactive.
1815 *
1816 * \note Implementations shall make the best effort to ensure that the
1817 * comparison between the actual MAC and the expected MAC is performed
1818 * in constant time.
1819 *
1820 * \param[in,out] operation Active MAC operation.
1821 * \param[in] mac Buffer containing the expected MAC value.
1822 * \param mac_length Size of the \p mac buffer in bytes.
1823 *
1824 * \retval #PSA_SUCCESS
1825 * The expected MAC is identical to the actual MAC of the message.
1826 * \retval #PSA_ERROR_INVALID_SIGNATURE
1827 * The MAC of the message was calculated successfully, but it
1828 * differs from the expected MAC.
1829 * \retval #PSA_ERROR_BAD_STATE
1830 * The operation state is not valid (not started, or already completed).
1831 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1832 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1833 * \retval #PSA_ERROR_HARDWARE_FAILURE
1834 * \retval #PSA_ERROR_TAMPERING_DETECTED
1835 */
1836psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1837 const uint8_t *mac,
1838 size_t mac_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001839
Antonio de Angelis377a1552018-11-22 17:02:40 +00001840/** Abort a MAC operation.
1841 *
1842 * Aborting an operation frees all associated resources except for the
1843 * \p operation structure itself. Once aborted, the operation object
1844 * can be reused for another operation by calling
1845 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
1846 *
1847 * You may call this function any time after the operation object has
1848 * been initialized by any of the following methods:
1849 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1850 * it succeeds or not.
1851 * - Initializing the \c struct to all-bits-zero.
1852 * - Initializing the \c struct to logical zeros, e.g.
1853 * `psa_mac_operation_t operation = {0}`.
1854 *
1855 * In particular, calling psa_mac_abort() after the operation has been
1856 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1857 * psa_mac_verify_finish() is safe and has no effect.
1858 *
1859 * \param[in,out] operation Initialized MAC operation.
1860 *
1861 * \retval #PSA_SUCCESS
1862 * \retval #PSA_ERROR_BAD_STATE
1863 * \p operation is not an active MAC operation.
1864 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1865 * \retval #PSA_ERROR_HARDWARE_FAILURE
1866 * \retval #PSA_ERROR_TAMPERING_DETECTED
1867 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01001868psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1869
1870/**@}*/
1871
1872/** \defgroup cipher Symmetric ciphers
1873 * @{
1874 */
1875
Antonio de Angelis377a1552018-11-22 17:02:40 +00001876/** The type of the state data structure for multipart cipher operations.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001877 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001878 * This is an implementation-defined \c struct. Applications should not
1879 * make any assumptions about the content of this structure except
1880 * as directed by the documentation of a specific implementation. */
1881typedef struct psa_cipher_operation_s psa_cipher_operation_t;
Antonio de Angelis14276e92018-07-10 14:35:43 +01001882
1883/** Set the key for a multipart symmetric encryption operation.
1884 *
1885 * The sequence of operations to encrypt a message with a symmetric cipher
1886 * is as follows:
1887 * -# Allocate an operation object which will be passed to all the functions
1888 * listed here.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001889 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001890 * The key remains associated with the operation even if the content
1891 * of the key slot changes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001892 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Antonio de Angelis14276e92018-07-10 14:35:43 +01001893 * generate or set the IV (initialization vector). You should use
Antonio de Angelis377a1552018-11-22 17:02:40 +00001894 * psa_cipher_generate_iv() unless the protocol you are implementing
Antonio de Angelis14276e92018-07-10 14:35:43 +01001895 * requires a specific IV value.
1896 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1897 * of the message each time.
1898 * -# Call psa_cipher_finish().
1899 *
1900 * The application may call psa_cipher_abort() at any time after the operation
Antonio de Angelis377a1552018-11-22 17:02:40 +00001901 * has been initialized with psa_cipher_encrypt_setup().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001902 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001903 * After a successful call to psa_cipher_encrypt_setup(), the application must
Antonio de Angelis14276e92018-07-10 14:35:43 +01001904 * eventually terminate the operation. The following events terminate an
1905 * operation:
Antonio de Angelis377a1552018-11-22 17:02:40 +00001906 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Antonio de Angelis14276e92018-07-10 14:35:43 +01001907 * or psa_cipher_update().
1908 * - A call to psa_cipher_finish() or psa_cipher_abort().
1909 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001910 * \param[out] operation The operation object to use.
1911 * \param key Slot containing the key to use for the operation.
1912 * \param alg The cipher algorithm to compute
1913 * (\c PSA_ALG_XXX value such that
1914 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +01001915 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001916 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001917 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001918 * \retval #PSA_ERROR_EMPTY_SLOT
1919 * \retval #PSA_ERROR_NOT_PERMITTED
1920 * \retval #PSA_ERROR_INVALID_ARGUMENT
1921 * \p key is not compatible with \p alg.
1922 * \retval #PSA_ERROR_NOT_SUPPORTED
1923 * \p alg is not supported or is not a cipher algorithm.
1924 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1925 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1926 * \retval #PSA_ERROR_HARDWARE_FAILURE
1927 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01001928 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001929psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
1930 psa_key_slot_t key,
1931 psa_algorithm_t alg);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001932
1933/** Set the key for a multipart symmetric decryption operation.
1934 *
1935 * The sequence of operations to decrypt a message with a symmetric cipher
1936 * is as follows:
1937 * -# Allocate an operation object which will be passed to all the functions
1938 * listed here.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001939 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Antonio de Angelis14276e92018-07-10 14:35:43 +01001940 * The key remains associated with the operation even if the content
1941 * of the key slot changes.
1942 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1943 * decryption. If the IV is prepended to the ciphertext, you can call
1944 * psa_cipher_update() on a buffer containing the IV followed by the
1945 * beginning of the message.
1946 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1947 * of the message each time.
1948 * -# Call psa_cipher_finish().
1949 *
1950 * The application may call psa_cipher_abort() at any time after the operation
Antonio de Angelis377a1552018-11-22 17:02:40 +00001951 * has been initialized with psa_cipher_decrypt_setup().
Antonio de Angelis14276e92018-07-10 14:35:43 +01001952 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001953 * After a successful call to psa_cipher_decrypt_setup(), the application must
Antonio de Angelis14276e92018-07-10 14:35:43 +01001954 * eventually terminate the operation. The following events terminate an
1955 * operation:
1956 * - A failed call to psa_cipher_update().
1957 * - A call to psa_cipher_finish() or psa_cipher_abort().
1958 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001959 * \param[out] operation The operation object to use.
1960 * \param key Slot containing the key to use for the operation.
1961 * \param alg The cipher algorithm to compute
1962 * (\c PSA_ALG_XXX value such that
1963 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +01001964 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00001965 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01001966 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00001967 * \retval #PSA_ERROR_EMPTY_SLOT
1968 * \retval #PSA_ERROR_NOT_PERMITTED
1969 * \retval #PSA_ERROR_INVALID_ARGUMENT
1970 * \p key is not compatible with \p alg.
1971 * \retval #PSA_ERROR_NOT_SUPPORTED
1972 * \p alg is not supported or is not a cipher algorithm.
1973 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1974 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1975 * \retval #PSA_ERROR_HARDWARE_FAILURE
1976 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01001977 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00001978psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
1979 psa_key_slot_t key,
1980 psa_algorithm_t alg);
Antonio de Angelis14276e92018-07-10 14:35:43 +01001981
Antonio de Angelis377a1552018-11-22 17:02:40 +00001982/** Generate an IV for a symmetric encryption operation.
1983 *
1984 * This function generates a random IV (initialization vector), nonce
1985 * or initial counter value for the encryption operation as appropriate
1986 * for the chosen algorithm, key type and key size.
1987 *
1988 * The application must call psa_cipher_encrypt_setup() before
1989 * calling this function.
1990 *
1991 * If this function returns an error status, the operation becomes inactive.
1992 *
1993 * \param[in,out] operation Active cipher operation.
1994 * \param[out] iv Buffer where the generated IV is to be written.
1995 * \param iv_size Size of the \p iv buffer in bytes.
1996 * \param[out] iv_length On success, the number of bytes of the
1997 * generated IV.
1998 *
1999 * \retval #PSA_SUCCESS
2000 * Success.
2001 * \retval #PSA_ERROR_BAD_STATE
2002 * The operation state is not valid (not started, or IV already set).
2003 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2004 * The size of the \p iv buffer is too small.
2005 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2006 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2007 * \retval #PSA_ERROR_HARDWARE_FAILURE
2008 * \retval #PSA_ERROR_TAMPERING_DETECTED
2009 */
2010psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
2011 unsigned char *iv,
2012 size_t iv_size,
2013 size_t *iv_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01002014
Antonio de Angelis377a1552018-11-22 17:02:40 +00002015/** Set the IV for a symmetric encryption or decryption operation.
2016 *
2017 * This function sets the random IV (initialization vector), nonce
2018 * or initial counter value for the encryption or decryption operation.
2019 *
2020 * The application must call psa_cipher_encrypt_setup() before
2021 * calling this function.
2022 *
2023 * If this function returns an error status, the operation becomes inactive.
2024 *
2025 * \note When encrypting, applications should use psa_cipher_generate_iv()
2026 * instead of this function, unless implementing a protocol that requires
2027 * a non-random IV.
2028 *
2029 * \param[in,out] operation Active cipher operation.
2030 * \param[in] iv Buffer containing the IV to use.
2031 * \param iv_length Size of the IV in bytes.
2032 *
2033 * \retval #PSA_SUCCESS
2034 * Success.
2035 * \retval #PSA_ERROR_BAD_STATE
2036 * The operation state is not valid (not started, or IV already set).
2037 * \retval #PSA_ERROR_INVALID_ARGUMENT
2038 * The size of \p iv is not acceptable for the chosen algorithm,
2039 * or the chosen algorithm does not use an IV.
2040 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2041 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2042 * \retval #PSA_ERROR_HARDWARE_FAILURE
2043 * \retval #PSA_ERROR_TAMPERING_DETECTED
2044 */
2045psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
2046 const unsigned char *iv,
2047 size_t iv_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01002048
Antonio de Angelis377a1552018-11-22 17:02:40 +00002049/** Encrypt or decrypt a message fragment in an active cipher operation.
2050 *
2051 * Before calling this function, you must:
2052 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
2053 * The choice of setup function determines whether this function
2054 * encrypts or decrypts its input.
2055 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
2056 * (recommended when encrypting) or psa_cipher_set_iv().
2057 *
2058 * If this function returns an error status, the operation becomes inactive.
2059 *
2060 * \param[in,out] operation Active cipher operation.
2061 * \param[in] input Buffer containing the message fragment to
2062 * encrypt or decrypt.
2063 * \param input_length Size of the \p input buffer in bytes.
2064 * \param[out] output Buffer where the output is to be written.
2065 * \param output_size Size of the \p output buffer in bytes.
2066 * \param[out] output_length On success, the number of bytes
2067 * that make up the returned output.
2068 *
2069 * \retval #PSA_SUCCESS
2070 * Success.
2071 * \retval #PSA_ERROR_BAD_STATE
2072 * The operation state is not valid (not started, IV required but
2073 * not set, or already completed).
2074 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2075 * The size of the \p output buffer is too small.
2076 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2077 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2078 * \retval #PSA_ERROR_HARDWARE_FAILURE
2079 * \retval #PSA_ERROR_TAMPERING_DETECTED
2080 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01002081psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2082 const uint8_t *input,
2083 size_t input_length,
2084 unsigned char *output,
2085 size_t output_size,
2086 size_t *output_length);
2087
Antonio de Angelis377a1552018-11-22 17:02:40 +00002088/** Finish encrypting or decrypting a message in a cipher operation.
2089 *
2090 * The application must call psa_cipher_encrypt_setup() or
2091 * psa_cipher_decrypt_setup() before calling this function. The choice
2092 * of setup function determines whether this function encrypts or
2093 * decrypts its input.
2094 *
2095 * This function finishes the encryption or decryption of the message
2096 * formed by concatenating the inputs passed to preceding calls to
2097 * psa_cipher_update().
2098 *
2099 * When this function returns, the operation becomes inactive.
2100 *
2101 * \param[in,out] operation Active cipher operation.
2102 * \param[out] output Buffer where the output is to be written.
2103 * \param output_size Size of the \p output buffer in bytes.
2104 * \param[out] output_length On success, the number of bytes
2105 * that make up the returned output.
2106 *
2107 * \retval #PSA_SUCCESS
2108 * Success.
2109 * \retval #PSA_ERROR_BAD_STATE
2110 * The operation state is not valid (not started, IV required but
2111 * not set, or already completed).
2112 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2113 * The size of the \p output buffer is too small.
2114 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2115 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2116 * \retval #PSA_ERROR_HARDWARE_FAILURE
2117 * \retval #PSA_ERROR_TAMPERING_DETECTED
2118 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01002119psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
2120 uint8_t *output,
2121 size_t output_size,
2122 size_t *output_length);
2123
Antonio de Angelis377a1552018-11-22 17:02:40 +00002124/** Abort a cipher operation.
2125 *
2126 * Aborting an operation frees all associated resources except for the
2127 * \p operation structure itself. Once aborted, the operation object
2128 * can be reused for another operation by calling
2129 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
2130 *
2131 * You may call this function any time after the operation object has
2132 * been initialized by any of the following methods:
2133 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2134 * whether it succeeds or not.
2135 * - Initializing the \c struct to all-bits-zero.
2136 * - Initializing the \c struct to logical zeros, e.g.
2137 * `psa_cipher_operation_t operation = {0}`.
2138 *
2139 * In particular, calling psa_cipher_abort() after the operation has been
2140 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2141 * is safe and has no effect.
2142 *
2143 * \param[in,out] operation Initialized cipher operation.
2144 *
2145 * \retval #PSA_SUCCESS
2146 * \retval #PSA_ERROR_BAD_STATE
2147 * \p operation is not an active cipher operation.
2148 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2149 * \retval #PSA_ERROR_HARDWARE_FAILURE
2150 * \retval #PSA_ERROR_TAMPERING_DETECTED
2151 */
Antonio de Angelis14276e92018-07-10 14:35:43 +01002152psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2153
2154/**@}*/
2155
2156/** \defgroup aead Authenticated encryption with associated data (AEAD)
2157 * @{
2158 */
2159
2160/** The tag size for an AEAD algorithm, in bytes.
2161 *
2162 * \param alg An AEAD algorithm
2163 * (\c PSA_ALG_XXX value such that
Antonio de Angelis377a1552018-11-22 17:02:40 +00002164 * #PSA_ALG_IS_AEAD(\p alg) is true).
Antonio de Angelis14276e92018-07-10 14:35:43 +01002165 *
2166 * \return The tag size for the specified algorithm.
2167 * If the AEAD algorithm does not have an identified
2168 * tag that can be distinguished from the rest of
2169 * the ciphertext, return 0.
2170 * If the AEAD algorithm is not recognized, return 0.
2171 * An implementation may return either 0 or a
2172 * correct size for an AEAD algorithm that it
2173 * recognizes, but does not support.
2174 */
2175#define PSA_AEAD_TAG_SIZE(alg) \
2176 ((alg) == PSA_ALG_GCM ? 16 : \
2177 (alg) == PSA_ALG_CCM ? 16 : \
2178 0)
2179
Antonio de Angelis14276e92018-07-10 14:35:43 +01002180/** Process an authenticated encryption operation.
2181 *
2182 * \param key Slot containing the key to use.
2183 * \param alg The AEAD algorithm to compute
2184 * (\c PSA_ALG_XXX value such that
Antonio de Angelis377a1552018-11-22 17:02:40 +00002185 * #PSA_ALG_IS_AEAD(\p alg) is true).
2186 * \param[in] nonce Nonce or IV to use.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002187 * \param nonce_length Size of the \p nonce buffer in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002188 * \param[in] additional_data Additional data that will be authenticated
Antonio de Angelis14276e92018-07-10 14:35:43 +01002189 * but not encrypted.
2190 * \param additional_data_length Size of \p additional_data in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002191 * \param[in] plaintext Data that will be authenticated and
Antonio de Angelis14276e92018-07-10 14:35:43 +01002192 * encrypted.
2193 * \param plaintext_length Size of \p plaintext in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002194 * \param[out] ciphertext Output buffer for the authenticated and
Antonio de Angelis14276e92018-07-10 14:35:43 +01002195 * encrypted data. The additional data is not
2196 * part of this output. For algorithms where the
2197 * encrypted data and the authentication tag
2198 * are defined as separate outputs, the
2199 * authentication tag is appended to the
2200 * encrypted data.
2201 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2202 * This must be at least
2203 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2204 * \p plaintext_length).
Antonio de Angelis377a1552018-11-22 17:02:40 +00002205 * \param[out] ciphertext_length On success, the size of the output
Antonio de Angelis14276e92018-07-10 14:35:43 +01002206 * in the \b ciphertext buffer.
2207 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002208 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01002209 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002210 * \retval #PSA_ERROR_EMPTY_SLOT
2211 * \retval #PSA_ERROR_NOT_PERMITTED
2212 * \retval #PSA_ERROR_INVALID_ARGUMENT
2213 * \p key is not compatible with \p alg.
2214 * \retval #PSA_ERROR_NOT_SUPPORTED
2215 * \p alg is not supported or is not an AEAD algorithm.
2216 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2217 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2218 * \retval #PSA_ERROR_HARDWARE_FAILURE
2219 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01002220 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00002221psa_status_t psa_aead_encrypt(psa_key_slot_t key,
2222 psa_algorithm_t alg,
2223 const uint8_t *nonce,
2224 size_t nonce_length,
2225 const uint8_t *additional_data,
2226 size_t additional_data_length,
2227 const uint8_t *plaintext,
2228 size_t plaintext_length,
2229 uint8_t *ciphertext,
2230 size_t ciphertext_size,
2231 size_t *ciphertext_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01002232
2233/** Process an authenticated decryption operation.
2234 *
2235 * \param key Slot containing the key to use.
2236 * \param alg The AEAD algorithm to compute
2237 * (\c PSA_ALG_XXX value such that
Antonio de Angelis377a1552018-11-22 17:02:40 +00002238 * #PSA_ALG_IS_AEAD(\p alg) is true).
2239 * \param[in] nonce Nonce or IV to use.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002240 * \param nonce_length Size of the \p nonce buffer in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002241 * \param[in] additional_data Additional data that has been authenticated
Antonio de Angelis14276e92018-07-10 14:35:43 +01002242 * but not encrypted.
2243 * \param additional_data_length Size of \p additional_data in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002244 * \param[in] ciphertext Data that has been authenticated and
Antonio de Angelis14276e92018-07-10 14:35:43 +01002245 * encrypted. For algorithms where the
2246 * encrypted data and the authentication tag
2247 * are defined as separate inputs, the buffer
2248 * must contain the encrypted data followed
2249 * by the authentication tag.
2250 * \param ciphertext_length Size of \p ciphertext in bytes.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002251 * \param[out] plaintext Output buffer for the decrypted data.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002252 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2253 * This must be at least
2254 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2255 * \p ciphertext_length).
Antonio de Angelis377a1552018-11-22 17:02:40 +00002256 * \param[out] plaintext_length On success, the size of the output
Antonio de Angelis14276e92018-07-10 14:35:43 +01002257 * in the \b plaintext buffer.
2258 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002259 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01002260 * Success.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002261 * \retval #PSA_ERROR_EMPTY_SLOT
2262 * \retval #PSA_ERROR_INVALID_SIGNATURE
Antonio de Angelis14276e92018-07-10 14:35:43 +01002263 * The ciphertext is not authentic.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002264 * \retval #PSA_ERROR_NOT_PERMITTED
2265 * \retval #PSA_ERROR_INVALID_ARGUMENT
2266 * \p key is not compatible with \p alg.
2267 * \retval #PSA_ERROR_NOT_SUPPORTED
2268 * \p alg is not supported or is not an AEAD algorithm.
2269 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2270 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2271 * \retval #PSA_ERROR_HARDWARE_FAILURE
2272 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01002273 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00002274psa_status_t psa_aead_decrypt(psa_key_slot_t key,
2275 psa_algorithm_t alg,
2276 const uint8_t *nonce,
2277 size_t nonce_length,
2278 const uint8_t *additional_data,
2279 size_t additional_data_length,
2280 const uint8_t *ciphertext,
2281 size_t ciphertext_length,
2282 uint8_t *plaintext,
2283 size_t plaintext_size,
2284 size_t *plaintext_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01002285
2286/**@}*/
2287
2288/** \defgroup asymmetric Asymmetric cryptography
2289 * @{
2290 */
2291
2292/**
Antonio de Angelis377a1552018-11-22 17:02:40 +00002293 * \brief ECDSA signature size for a given curve bit size
Antonio de Angelis14276e92018-07-10 14:35:43 +01002294 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002295 * \param curve_bits Curve size in bits.
2296 * \return Signature size in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002297 *
2298 * \note This macro returns a compile-time constant if its argument is one.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002299 */
Antonio de Angelis377a1552018-11-22 17:02:40 +00002300#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
2301 (PSA_BITS_TO_BYTES(curve_bits) * 2)
Antonio de Angelis14276e92018-07-10 14:35:43 +01002302
2303/**
2304 * \brief Sign a hash or short message with a private key.
2305 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002306 * Note that to perform a hash-and-sign signature algorithm, you must
2307 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
2308 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2309 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2310 * to determine the hash algorithm to use.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002311 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002312 * \param key Key slot containing an asymmetric key pair.
2313 * \param alg A signature algorithm that is compatible with
2314 * the type of \p key.
2315 * \param[in] hash The hash or message to sign.
2316 * \param hash_length Size of the \p hash buffer in bytes.
2317 * \param[out] signature Buffer where the signature is to be written.
2318 * \param signature_size Size of the \p signature buffer in bytes.
2319 * \param[out] signature_length On success, the number of bytes
2320 * that make up the returned signature value.
2321 *
2322 * \retval #PSA_SUCCESS
2323 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2324 * The size of the \p signature buffer is too small. You can
Antonio de Angelis14276e92018-07-10 14:35:43 +01002325 * determine a sufficient buffer size by calling
Antonio de Angelis377a1552018-11-22 17:02:40 +00002326 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01002327 * where \c key_type and \c key_bits are the type and bit-size
Antonio de Angelis377a1552018-11-22 17:02:40 +00002328 * respectively of \p key.
2329 * \retval #PSA_ERROR_NOT_SUPPORTED
2330 * \retval #PSA_ERROR_INVALID_ARGUMENT
2331 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2332 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2333 * \retval #PSA_ERROR_HARDWARE_FAILURE
2334 * \retval #PSA_ERROR_TAMPERING_DETECTED
2335 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Antonio de Angelis14276e92018-07-10 14:35:43 +01002336 */
2337psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
2338 psa_algorithm_t alg,
2339 const uint8_t *hash,
2340 size_t hash_length,
Antonio de Angelis14276e92018-07-10 14:35:43 +01002341 uint8_t *signature,
2342 size_t signature_size,
2343 size_t *signature_length);
2344
2345/**
2346 * \brief Verify the signature a hash or short message using a public key.
2347 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002348 * Note that to perform a hash-and-sign signature algorithm, you must
2349 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
2350 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2351 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2352 * to determine the hash algorithm to use.
2353 *
Antonio de Angelis14276e92018-07-10 14:35:43 +01002354 * \param key Key slot containing a public key or an
2355 * asymmetric key pair.
2356 * \param alg A signature algorithm that is compatible with
Antonio de Angelis377a1552018-11-22 17:02:40 +00002357 * the type of \p key.
2358 * \param[in] hash The hash or message whose signature is to be
2359 * verified.
2360 * \param hash_length Size of the \p hash buffer in bytes.
2361 * \param[in] signature Buffer containing the signature to verify.
2362 * \param signature_length Size of the \p signature buffer in bytes.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002363 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002364 * \retval #PSA_SUCCESS
Antonio de Angelis14276e92018-07-10 14:35:43 +01002365 * The signature is valid.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002366 * \retval #PSA_ERROR_INVALID_SIGNATURE
Antonio de Angelis14276e92018-07-10 14:35:43 +01002367 * The calculation was perfomed successfully, but the passed
2368 * signature is not a valid signature.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002369 * \retval #PSA_ERROR_NOT_SUPPORTED
2370 * \retval #PSA_ERROR_INVALID_ARGUMENT
2371 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2372 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2373 * \retval #PSA_ERROR_HARDWARE_FAILURE
2374 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01002375 */
2376psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
2377 psa_algorithm_t alg,
2378 const uint8_t *hash,
2379 size_t hash_length,
Antonio de Angelis377a1552018-11-22 17:02:40 +00002380 const uint8_t *signature,
2381 size_t signature_length);
Antonio de Angelis14276e92018-07-10 14:35:43 +01002382
Antonio de Angelis14276e92018-07-10 14:35:43 +01002383#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
Antonio de Angelis377a1552018-11-22 17:02:40 +00002384 (PSA_ALG_IS_RSA_OAEP(alg) ? \
2385 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Antonio de Angelis14276e92018-07-10 14:35:43 +01002386 11 /*PKCS#1v1.5*/)
Antonio de Angelis14276e92018-07-10 14:35:43 +01002387
2388/**
2389 * \brief Encrypt a short message with a public key.
2390 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002391 * \param key Key slot containing a public key or an
2392 * asymmetric key pair.
2393 * \param alg An asymmetric encryption algorithm that is
2394 * compatible with the type of \p key.
2395 * \param[in] input The message to encrypt.
2396 * \param input_length Size of the \p input buffer in bytes.
2397 * \param[in] salt A salt or label, if supported by the
2398 * encryption algorithm.
2399 * If the algorithm does not support a
2400 * salt, pass \c NULL.
2401 * If the algorithm supports an optional
2402 * salt and you do not want to pass a salt,
2403 * pass \c NULL.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002404 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002405 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2406 * supported.
2407 * \param salt_length Size of the \p salt buffer in bytes.
2408 * If \p salt is \c NULL, pass 0.
2409 * \param[out] output Buffer where the encrypted message is to
2410 * be written.
2411 * \param output_size Size of the \p output buffer in bytes.
2412 * \param[out] output_length On success, the number of bytes
2413 * that make up the returned output.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002414 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002415 * \retval #PSA_SUCCESS
2416 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2417 * The size of the \p output buffer is too small. You can
Antonio de Angelis14276e92018-07-10 14:35:43 +01002418 * determine a sufficient buffer size by calling
Antonio de Angelis377a1552018-11-22 17:02:40 +00002419 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01002420 * where \c key_type and \c key_bits are the type and bit-size
Antonio de Angelis377a1552018-11-22 17:02:40 +00002421 * respectively of \p key.
2422 * \retval #PSA_ERROR_NOT_SUPPORTED
2423 * \retval #PSA_ERROR_INVALID_ARGUMENT
2424 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2425 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2426 * \retval #PSA_ERROR_HARDWARE_FAILURE
2427 * \retval #PSA_ERROR_TAMPERING_DETECTED
2428 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Antonio de Angelis14276e92018-07-10 14:35:43 +01002429 */
2430psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
2431 psa_algorithm_t alg,
2432 const uint8_t *input,
2433 size_t input_length,
2434 const uint8_t *salt,
2435 size_t salt_length,
2436 uint8_t *output,
2437 size_t output_size,
2438 size_t *output_length);
2439
2440/**
2441 * \brief Decrypt a short message with a private key.
2442 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002443 * \param key Key slot containing an asymmetric key pair.
2444 * \param alg An asymmetric encryption algorithm that is
2445 * compatible with the type of \p key.
2446 * \param[in] input The message to decrypt.
2447 * \param input_length Size of the \p input buffer in bytes.
2448 * \param[in] salt A salt or label, if supported by the
2449 * encryption algorithm.
2450 * If the algorithm does not support a
2451 * salt, pass \c NULL.
2452 * If the algorithm supports an optional
2453 * salt and you do not want to pass a salt,
2454 * pass \c NULL.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002455 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002456 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2457 * supported.
2458 * \param salt_length Size of the \p salt buffer in bytes.
2459 * If \p salt is \c NULL, pass 0.
2460 * \param[out] output Buffer where the decrypted message is to
2461 * be written.
2462 * \param output_size Size of the \c output buffer in bytes.
2463 * \param[out] output_length On success, the number of bytes
2464 * that make up the returned output.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002465 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002466 * \retval #PSA_SUCCESS
2467 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2468 * The size of the \p output buffer is too small. You can
Antonio de Angelis14276e92018-07-10 14:35:43 +01002469 * determine a sufficient buffer size by calling
Antonio de Angelis377a1552018-11-22 17:02:40 +00002470 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Antonio de Angelis14276e92018-07-10 14:35:43 +01002471 * where \c key_type and \c key_bits are the type and bit-size
Antonio de Angelis377a1552018-11-22 17:02:40 +00002472 * respectively of \p key.
2473 * \retval #PSA_ERROR_NOT_SUPPORTED
2474 * \retval #PSA_ERROR_INVALID_ARGUMENT
2475 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2476 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2477 * \retval #PSA_ERROR_HARDWARE_FAILURE
2478 * \retval #PSA_ERROR_TAMPERING_DETECTED
2479 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2480 * \retval #PSA_ERROR_INVALID_PADDING
Antonio de Angelis14276e92018-07-10 14:35:43 +01002481 */
2482psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
2483 psa_algorithm_t alg,
2484 const uint8_t *input,
2485 size_t input_length,
2486 const uint8_t *salt,
2487 size_t salt_length,
2488 uint8_t *output,
2489 size_t output_size,
2490 size_t *output_length);
2491
2492/**@}*/
2493
Antonio de Angelis377a1552018-11-22 17:02:40 +00002494/** \defgroup generators Generators
2495 * @{
2496 */
2497
2498/** The type of the state data structure for generators.
2499 *
2500 * Before calling any function on a generator, the application must
2501 * initialize it by any of the following means:
2502 * - Set the structure to all-bits-zero, for example:
2503 * \code
2504 * psa_crypto_generator_t generator;
2505 * memset(&generator, 0, sizeof(generator));
2506 * \endcode
2507 * - Initialize the structure to logical zero values, for example:
2508 * \code
2509 * psa_crypto_generator_t generator = {0};
2510 * \endcode
2511 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2512 * for example:
2513 * \code
2514 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2515 * \endcode
2516 * - Assign the result of the function psa_crypto_generator_init()
2517 * to the structure, for example:
2518 * \code
2519 * psa_crypto_generator_t generator;
2520 * generator = psa_crypto_generator_init();
2521 * \endcode
2522 *
2523 * This is an implementation-defined \c struct. Applications should not
2524 * make any assumptions about the content of this structure except
2525 * as directed by the documentation of a specific implementation.
2526 */
2527typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2528
2529/** \def PSA_CRYPTO_GENERATOR_INIT
2530 *
2531 * This macro returns a suitable initializer for a generator object
2532 * of type #psa_crypto_generator_t.
2533 */
2534#ifdef __DOXYGEN_ONLY__
2535/* This is an example definition for documentation purposes.
2536 * Implementations should define a suitable value in `crypto_struct.h`.
2537 */
2538#define PSA_CRYPTO_GENERATOR_INIT {0}
2539#endif
2540
2541/** Return an initial value for a generator object.
2542 */
2543psa_crypto_generator_t psa_crypto_generator_init(void);
2544
2545/** Retrieve the current capacity of a generator.
2546 *
2547 * The capacity of a generator is the maximum number of bytes that it can
2548 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2549 *
2550 * \param[in] generator The generator to query.
2551 * \param[out] capacity On success, the capacity of the generator.
2552 *
2553 * \retval PSA_SUCCESS
2554 * \retval PSA_ERROR_BAD_STATE
2555 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2556 */
2557psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2558 size_t *capacity);
2559
2560/** Read some data from a generator.
2561 *
2562 * This function reads and returns a sequence of bytes from a generator.
2563 * The data that is read is discarded from the generator. The generator's
2564 * capacity is decreased by the number of bytes read.
2565 *
2566 * \param[in,out] generator The generator object to read from.
2567 * \param[out] output Buffer where the generator output will be
2568 * written.
2569 * \param output_length Number of bytes to output.
2570 *
2571 * \retval PSA_SUCCESS
2572 * \retval PSA_ERROR_INSUFFICIENT_CAPACITY
2573 * There were fewer than \p output_length bytes
2574 * in the generator. Note that in this case, no
2575 * output is written to the output buffer.
2576 * The generator's capacity is set to 0, thus
2577 * subsequent calls to this function will not
2578 * succeed, even with a smaller output buffer.
2579 * \retval PSA_ERROR_BAD_STATE
2580 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
2581 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2582 * \retval PSA_ERROR_HARDWARE_FAILURE
2583 * \retval PSA_ERROR_TAMPERING_DETECTED
2584 */
2585psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2586 uint8_t *output,
2587 size_t output_length);
2588
2589/** Create a symmetric key from data read from a generator.
2590 *
2591 * This function reads a sequence of bytes from a generator and imports
2592 * these bytes as a key.
2593 * The data that is read is discarded from the generator. The generator's
2594 * capacity is decreased by the number of bytes read.
2595 *
2596 * This function is equivalent to calling #psa_generator_read and
2597 * passing the resulting output to #psa_import_key, but
2598 * if the implementation provides an isolation boundary then
2599 * the key material is not exposed outside the isolation boundary.
2600 *
2601 * \param key Slot where the key will be stored. This must be a
2602 * valid slot for a key of the chosen type. It must
2603 * be unoccupied.
2604 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2605 * This must be a symmetric key type.
2606 * \param bits Key size in bits.
2607 * \param[in,out] generator The generator object to read from.
2608 *
2609 * \retval PSA_SUCCESS
2610 * Success.
2611 * \retval PSA_ERROR_INSUFFICIENT_CAPACITY
2612 * There were fewer than \p output_length bytes
2613 * in the generator. Note that in this case, no
2614 * output is written to the output buffer.
2615 * The generator's capacity is set to 0, thus
2616 * subsequent calls to this function will not
2617 * succeed, even with a smaller output buffer.
2618 * \retval PSA_ERROR_NOT_SUPPORTED
2619 * The key type or key size is not supported, either by the
2620 * implementation in general or in this particular slot.
2621 * \retval PSA_ERROR_BAD_STATE
2622 * \retval PSA_ERROR_INVALID_ARGUMENT
2623 * The key slot is invalid.
2624 * \retval PSA_ERROR_OCCUPIED_SLOT
2625 * There is already a key in the specified slot.
2626 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
2627 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
2628 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2629 * \retval PSA_ERROR_HARDWARE_FAILURE
2630 * \retval PSA_ERROR_TAMPERING_DETECTED
2631 */
2632psa_status_t psa_generator_import_key(psa_key_slot_t key,
2633 psa_key_type_t type,
2634 size_t bits,
2635 psa_crypto_generator_t *generator);
2636
2637/** Abort a generator.
2638 *
2639 * Once a generator has been aborted, its capacity is zero.
2640 * Aborting a generator frees all associated resources except for the
2641 * \c generator structure itself.
2642 *
2643 * This function may be called at any time as long as the generator
2644 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2645 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2646 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2647 * on a generator that has not been set up.
2648 *
2649 * Once aborted, the generator object may be called.
2650 *
2651 * \param[in,out] generator The generator to abort.
2652 *
2653 * \retval PSA_SUCCESS
2654 * \retval PSA_ERROR_BAD_STATE
2655 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2656 * \retval PSA_ERROR_HARDWARE_FAILURE
2657 * \retval PSA_ERROR_TAMPERING_DETECTED
2658 */
2659psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2660
2661/**@}*/
2662
2663/** \defgroup derivation Key derivation
2664 * @{
2665 */
2666
2667/** Set up a key derivation operation.
2668 *
2669 * A key derivation algorithm takes three inputs: a secret input \p key and
2670 * two non-secret inputs \p label and p salt.
2671 * The result of this function is a byte generator which can
2672 * be used to produce keys and other cryptographic material.
2673 *
2674 * The role of \p label and \p salt is as follows:
2675 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2676 * and \p label is the info string used in the "expand" step.
2677 *
2678 * \param[in,out] generator The generator object to set up. It must
2679 * have been initialized to .
2680 * \param key Slot containing the secret key to use.
2681 * \param alg The key derivation algorithm to compute
2682 * (\c PSA_ALG_XXX value such that
2683 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2684 * \param[in] salt Salt to use.
2685 * \param salt_length Size of the \p salt buffer in bytes.
2686 * \param[in] label Label to use.
2687 * \param label_length Size of the \p label buffer in bytes.
2688 * \param capacity The maximum number of bytes that the
2689 * generator will be able to provide.
2690 *
2691 * \retval #PSA_SUCCESS
2692 * Success.
2693 * \retval #PSA_ERROR_EMPTY_SLOT
2694 * \retval #PSA_ERROR_NOT_PERMITTED
2695 * \retval #PSA_ERROR_INVALID_ARGUMENT
2696 * \c key is not compatible with \c alg,
2697 * or \p capacity is too large for the specified algorithm and key.
2698 * \retval #PSA_ERROR_NOT_SUPPORTED
2699 * \c alg is not supported or is not a key derivation algorithm.
2700 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2701 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2702 * \retval #PSA_ERROR_HARDWARE_FAILURE
2703 * \retval #PSA_ERROR_TAMPERING_DETECTED
2704 */
2705psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
2706 psa_key_slot_t key,
2707 psa_algorithm_t alg,
2708 const uint8_t *salt,
2709 size_t salt_length,
2710 const uint8_t *label,
2711 size_t label_length,
2712 size_t capacity);
2713
2714/**@}*/
2715
2716/** \defgroup random Random generation
Antonio de Angelis14276e92018-07-10 14:35:43 +01002717 * @{
2718 */
2719
2720/**
2721 * \brief Generate random bytes.
2722 *
2723 * \warning This function **can** fail! Callers MUST check the return status
2724 * and MUST NOT use the content of the output buffer if the return
2725 * status is not #PSA_SUCCESS.
2726 *
2727 * \note To generate a key, use psa_generate_key() instead.
2728 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002729 * \param[out] output Output buffer for the generated data.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002730 * \param output_size Number of bytes to generate and output.
2731 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002732 * \retval #PSA_SUCCESS
2733 * \retval #PSA_ERROR_NOT_SUPPORTED
2734 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2735 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2736 * \retval #PSA_ERROR_HARDWARE_FAILURE
2737 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01002738 */
2739psa_status_t psa_generate_random(uint8_t *output,
2740 size_t output_size);
2741
Antonio de Angelis377a1552018-11-22 17:02:40 +00002742/** Extra parameters for RSA key generation.
2743 *
2744 * You may pass a pointer to a structure of this type as the \c extra
2745 * parameter to psa_generate_key().
2746 */
2747typedef struct {
2748 uint32_t e; /**< Public exponent value. Default: 65537. */
2749} psa_generate_key_extra_rsa;
2750
Antonio de Angelis14276e92018-07-10 14:35:43 +01002751/**
2752 * \brief Generate a key or key pair.
2753 *
2754 * \param key Slot where the key will be stored. This must be a
2755 * valid slot for a key of the chosen type. It must
2756 * be unoccupied.
2757 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2758 * \param bits Key size in bits.
Antonio de Angelis377a1552018-11-22 17:02:40 +00002759 * \param[in] extra Extra parameters for key generation. The
Antonio de Angelis14276e92018-07-10 14:35:43 +01002760 * interpretation of this parameter depends on
Antonio de Angelis377a1552018-11-22 17:02:40 +00002761 * \p type. All types support \c NULL to use
2762 * default parameters. Implementation that support
2763 * the generation of vendor-specific key types
2764 * that allow extra parameters shall document
2765 * the format of these extra parameters and
2766 * the default values. For standard parameters,
2767 * the meaning of \p extra is as follows:
2768 * - For a symmetric key type (a type such
2769 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2770 * false), \p extra must be \c NULL.
2771 * - For an elliptic curve key type (a type
2772 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2773 * false), \p extra must be \c NULL.
2774 * - For an RSA key (\p type is
2775 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2776 * optional #psa_generate_key_extra_rsa structure
2777 * specifying the public exponent. The
2778 * default public exponent used when \p extra
2779 * is \c NULL is 65537.
2780 * \param extra_size Size of the buffer that \p extra
2781 * points to, in bytes. Note that if \p extra is
2782 * \c NULL then \p extra_size must be zero.
Antonio de Angelis14276e92018-07-10 14:35:43 +01002783 *
Antonio de Angelis377a1552018-11-22 17:02:40 +00002784 * \retval #PSA_SUCCESS
2785 * \retval #PSA_ERROR_NOT_SUPPORTED
2786 * \retval #PSA_ERROR_INVALID_ARGUMENT
2787 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2788 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2789 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2790 * \retval #PSA_ERROR_HARDWARE_FAILURE
2791 * \retval #PSA_ERROR_TAMPERING_DETECTED
Antonio de Angelis14276e92018-07-10 14:35:43 +01002792 */
2793psa_status_t psa_generate_key(psa_key_slot_t key,
2794 psa_key_type_t type,
2795 size_t bits,
Antonio de Angelis377a1552018-11-22 17:02:40 +00002796 const void *extra,
2797 size_t extra_size);
Antonio de Angelis14276e92018-07-10 14:35:43 +01002798
2799/**@}*/
2800
2801#ifdef __cplusplus
2802}
2803#endif
2804
Antonio de Angelis377a1552018-11-22 17:02:40 +00002805/* The file "crypto_sizes.h" contains definitions for size calculation
2806 * macros whose definitions are implementation-specific. */
2807#include "psa_crypto_sizes.h"
2808
2809/* The file "crypto_struct.h" contains definitions for
2810 * implementation-specific structs that are declared above. */
2811#include "psa_crypto_struct.h"
2812
2813/* The file "crypto_extra.h" contains vendor-specific definitions. This
2814 * can include vendor-defined algorithms, extra functions, etc. */
Antonio de Angelis8908f472018-08-31 15:44:25 +01002815#include "psa_crypto_extra.h"
2816
Antonio de Angelis14276e92018-07-10 14:35:43 +01002817#endif /* PSA_CRYPTO_H */