blob: 43d8d0986f9a17073d368cadf250257f5d04e7ad [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
5
6#ifndef PSA_CRYPTO_H
7#define PSA_CRYPTO_H
8
9#include "crypto_platform.h"
10
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010011#include <stddef.h>
12
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010013#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010014/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
15 * must be defined in the crypto_platform.h header. These mock definitions
16 * are present in this file as a convenience to generate pretty-printed
17 * documentation that includes those definitions. */
18
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010019/** \defgroup platform Implementation-specific definitions
20 * @{
21 */
22
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010023/** \brief Key slot number.
24 *
25 * This type represents key slots. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010026 * type. The choice of type is implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027 * 0 is not a valid key slot number. The meaning of other values is
28 * implementation dependent.
29 *
30 * At any given point in time, each key slot either contains a
31 * cryptographic object, or is empty. Key slots are persistent:
32 * once set, the cryptographic object remains in the key slot until
33 * explicitly destroyed.
34 */
35typedef _unsigned_integral_type_ psa_key_slot_t;
36
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010037/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010038#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010039
Gilles Peskinee59236f2018-01-27 23:32:46 +010040#ifdef __cplusplus
41extern "C" {
42#endif
43
44/** \defgroup basic Basic definitions
45 * @{
46 */
47
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020048#if defined(PSA_SUCCESS)
49/* If PSA_SUCCESS is defined, assume that PSA crypto is being used
50 * together with PSA IPC, which also defines the identifier
51 * PSA_SUCCESS. We must not define PSA_SUCCESS ourselves in that case;
52 * the other error code names don't clash. Also define psa_status_t as
53 * an alias for the type used by PSA IPC. This is a temporary hack
54 * until we unify error reporting in PSA IPC and PSA crypo.
55 *
56 * Note that psa_defs.h must be included before this header!
57 */
58typedef psa_error_t psa_status_t;
59
60#else /* defined(PSA_SUCCESS) */
61
Gilles Peskinee59236f2018-01-27 23:32:46 +010062/**
63 * \brief Function return status.
64 *
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020065 * This is either #PSA_SUCCESS (which is zero), indicating success,
66 * or a nonzero value indicating that an error occurred. Errors are
67 * encoded as one of the \c PSA_ERROR_xxx values defined here.
Gilles Peskinee59236f2018-01-27 23:32:46 +010068 */
itayzafrirc2a79762018-06-18 16:20:16 +030069typedef int32_t psa_status_t;
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020070
itayzafrirc2a79762018-06-18 16:20:16 +030071/** The action was completed successfully. */
72#define PSA_SUCCESS ((psa_status_t)0)
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020073
74#endif /* !defined(PSA_SUCCESS) */
itayzafrirc2a79762018-06-18 16:20:16 +030075
76/** The requested operation or a parameter is not supported
77 * by this implementation.
78 *
79 * Implementations should return this error code when an enumeration
80 * parameter such as a key type, algorithm, etc. is not recognized.
81 * If a combination of parameters is recognized and identified as
82 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
83#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)1)
84
85/** The requested action is denied by a policy.
86 *
87 * Implementations should return this error code when the parameters
88 * are recognized as valid and supported, and a policy explicitly
89 * denies the requested operation.
90 *
91 * If a subset of the parameters of a function call identify a
92 * forbidden operation, and another subset of the parameters are
93 * not valid or not supported, it is unspecified whether the function
94 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
95 * #PSA_ERROR_INVALID_ARGUMENT. */
96#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)2)
97
98/** An output buffer is too small.
99 *
100 * Applications can call the `PSA_xxx_SIZE` macro listed in the function
101 * description to determine a sufficient buffer size.
102 *
103 * Implementations should preferably return this error code only
104 * in cases when performing the operation with a larger output
105 * buffer would succeed. However implementations may return this
106 * error if a function has invalid or unsupported parameters in addition
107 * to the parameters that determine the necessary output buffer size. */
108#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)3)
109
110/** A slot is occupied, but must be empty to carry out the
111 * requested action.
112 *
113 * If the slot number is invalid (i.e. the requested action could
114 * not be performed even after erasing the slot's content),
115 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
116#define PSA_ERROR_OCCUPIED_SLOT ((psa_status_t)4)
117
118/** A slot is empty, but must be occupied to carry out the
119 * requested action.
120 *
121 * If the slot number is invalid (i.e. the requested action could
122 * not be performed even after creating appropriate content in the slot),
123 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
124#define PSA_ERROR_EMPTY_SLOT ((psa_status_t)5)
125
126/** The requested action cannot be performed in the current state.
127 *
128 * Multipart operations return this error when one of the
129 * functions is called out of sequence. Refer to the function
130 * descriptions for permitted sequencing of functions.
131 *
132 * Implementations shall not return this error code to indicate
133 * that a key slot is occupied when it needs to be free or vice versa,
134 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
135 * as applicable. */
136#define PSA_ERROR_BAD_STATE ((psa_status_t)6)
137
138/** The parameters passed to the function are invalid.
139 *
140 * Implementations may return this error any time a parameter or
141 * combination of parameters are recognized as invalid.
142 *
143 * Implementations shall not return this error code to indicate
144 * that a key slot is occupied when it needs to be free or vice versa,
145 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
146 * as applicable. */
147#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)7)
148
149/** There is not enough runtime memory.
150 *
151 * If the action is carried out across multiple security realms, this
152 * error can refer to available memory in any of the security realms. */
153#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)8)
154
155/** There is not enough persistent storage.
156 *
157 * Functions that modify the key storage return this error code if
158 * there is insufficient storage space on the host media. In addition,
159 * many functions that do not otherwise access storage may return this
160 * error code if the implementation requires a mandatory log entry for
161 * the requested action and the log storage space is full. */
162#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)9)
163
164/** There was a communication failure inside the implementation.
165 *
166 * This can indicate a communication failure between the application
167 * and an external cryptoprocessor or between the cryptoprocessor and
168 * an external volatile or persistent memory. A communication failure
169 * may be transient or permanent depending on the cause.
170 *
171 * \warning If a function returns this error, it is undetermined
172 * whether the requested action has completed or not. Implementations
173 * should return #PSA_SUCCESS on successful completion whenver
174 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
175 * if the requested action was completed successfully in an external
176 * cryptoprocessor but there was a breakdown of communication before
177 * the cryptoprocessor could report the status to the application.
178 */
179#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)10)
180
181/** There was a storage failure that may have led to data loss.
182 *
183 * This error indicates that some persistent storage is corrupted.
184 * It should not be used for a corruption of volatile memory
185 * (use #PSA_ERROR_TAMPERING_DETECTED), for a communication error
186 * between the cryptoprocessor and its external storage (use
187 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
188 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
189 *
190 * Note that a storage failure does not indicate that any data that was
191 * previously read is invalid. However this previously read data may no
192 * longer be readable from storage.
193 *
194 * When a storage failure occurs, it is no longer possible to ensure
195 * the global integrity of the keystore. Depending on the global
196 * integrity guarantees offered by the implementation, access to other
197 * data may or may not fail even if the data is still readable but
198 * its integrity canont be guaranteed.
199 *
200 * Implementations should only use this error code to report a
201 * permanent storage corruption. However application writers should
202 * keep in mind that transient errors while reading the storage may be
203 * reported using this error code. */
204#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)11)
205
206/** A hardware failure was detected.
207 *
208 * A hardware failure may be transient or permanent depending on the
209 * cause. */
210#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)12)
211
212/** A tampering attempt was detected.
213 *
214 * If an application receives this error code, there is no guarantee
215 * that previously accessed or computed data was correct and remains
216 * confidential. Applications should not perform any security function
217 * and should enter a safe failure state.
218 *
219 * Implementations may return this error code if they detect an invalid
220 * state that cannot happen during normal operation and that indicates
221 * that the implementation's security guarantees no longer hold. Depending
222 * on the implementation architecture and on its security and safety goals,
223 * the implementation may forcibly terminate the application.
224 *
225 * This error code is intended as a last resort when a security breach
226 * is detected and it is unsure whether the keystore data is still
227 * protected. Implementations shall only return this error code
228 * to report an alarm from a tampering detector, to indicate that
229 * the confidentiality of stored data can no longer be guaranteed,
230 * or to indicate that the integrity of previously returned data is now
231 * considered compromised. Implementations shall not use this error code
232 * to indicate a hardware failure that merely makes it impossible to
233 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
234 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
235 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
236 * instead).
237 *
238 * This error indicates an attack against the application. Implementations
239 * shall not return this error code as a consequence of the behavior of
240 * the application itself. */
241#define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)13)
242
243/** There is not enough entropy to generate random data needed
244 * for the requested action.
245 *
246 * This error indicates a failure of a hardware random generator.
247 * Application writers should note that this error can be returned not
248 * only by functions whose purpose is to generate random data, such
249 * as key, IV or nonce generation, but also by functions that execute
250 * an algorithm with a randomized result, as well as functions that
251 * use randomization of intermediate computations as a countermeasure
252 * to certain attacks.
253 *
254 * Implementations should avoid returning this error after psa_crypto_init()
255 * has succeeded. Implementations should generate sufficient
256 * entropy during initialization and subsequently use a cryptographically
257 * secure pseudorandom generator (PRNG). However implementations may return
258 * this error at any time if a policy requires the PRNG to be reseeded
259 * during normal operation. */
260#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)14)
261
262/** The signature, MAC or hash is incorrect.
263 *
264 * Verification functions return this error if the verification
265 * calculations completed successfully, and the value to be verified
266 * was determined to be incorrect.
267 *
268 * If the value to verify has an invalid size, implementations may return
269 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
270#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)15)
271
272/** The decrypted padding is incorrect.
273 *
274 * \warning In some protocols, when decrypting data, it is essential that
275 * the behavior of the application does not depend on whether the padding
276 * is correct, down to precise timing. Applications should prefer
277 * protocols that use authenticated encryption rather than plain
278 * encryption. If the application must perform a decryption of
279 * unauthenticated data, the application writer should take care not
280 * to reveal whether the padding is invalid.
281 *
282 * Implementations should strive to make valid and invalid padding
283 * as close as possible to indistinguishable to an external observer.
284 * In particular, the timing of a decryption operation should not
285 * depend on the validity of the padding. */
286#define PSA_ERROR_INVALID_PADDING ((psa_status_t)16)
287
288/** An error occurred that does not correspond to any defined
289 * failure cause.
290 *
291 * Implementations may use this error code if none of the other standard
292 * error codes are applicable. */
293#define PSA_ERROR_UNKNOWN_ERROR ((psa_status_t)17)
Gilles Peskinee59236f2018-01-27 23:32:46 +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 *
Gilles Peskine28538492018-07-11 17:34:00 +0200304 * \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
Gilles Peskinee59236f2018-01-27 23:32:46 +0100310 */
311psa_status_t psa_crypto_init(void);
312
Gilles Peskine2905a7a2018-03-07 16:39:31 +0100313#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
314#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
Gilles Peskine0189e752018-02-03 23:57:22 +0100315
Gilles Peskinee59236f2018-01-27 23:32:46 +0100316/**@}*/
317
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100318/** \defgroup crypto_types Key and algorithm types
319 * @{
320 */
321
Gilles Peskine308b91d2018-02-08 09:47:44 +0100322/** \brief Encoding of a key type.
323 */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100324typedef uint32_t psa_key_type_t;
325
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100326/** An invalid key type value.
327 *
328 * Zero is not the encoding of any key type.
329 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100330#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100331
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 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100339#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100340
Gilles Peskine98f0a242018-02-06 18:57:29 +0100341#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7e000000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200342
Gilles Peskine35855962018-04-19 08:39:16 +0200343/** Raw data.
344 *
345 * A "key" of this type cannot be used for any cryptographic operation.
346 * Applications may use this type to store arbitrary data in the keystore. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100347#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x02000000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200348
Gilles Peskine98f0a242018-02-06 18:57:29 +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)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100352
Gilles Peskine35855962018-04-19 08:39:16 +0200353/** 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.
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200359 * This size can be calculated with #PSA_HASH_SIZE(\p alg) where
Gilles Peskine35855962018-04-19 08:39:16 +0200360 * `alg` is the HMAC algorithm or the underlying hash algorithm. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100361#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x02000001)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200362
Gilles Peskine35855962018-04-19 08:39:16 +0200363/** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher.
364 *
365 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
366 * 32 bytes (AES-256).
367 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100368#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x04000001)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200369
Gilles Peskine35855962018-04-19 08:39:16 +0200370/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
371 *
372 * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or
373 * 24 bytes (3-key 3DES).
374 *
375 * Note that single DES and 2-key 3DES are weak and strongly
376 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
377 * is weak and deprecated and should only be used in legacy protocols.
378 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100379#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x04000002)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200380
Gilles Peskine35855962018-04-19 08:39:16 +0200381/** Key for an cipher, AEAD or MAC algorithm based on the
382 * Camellia block cipher. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100383#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x04000003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200384
Gilles Peskine35855962018-04-19 08:39:16 +0200385/** Key for the RC4 stream cipher.
386 *
387 * Note that RC4 is weak and deprecated and should only be used in
388 * legacy protocols. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100389#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x04000004)
390
Gilles Peskine308b91d2018-02-08 09:47:44 +0100391/** RSA public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100392#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x06010000)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100393/** RSA key pair (private and public key). */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100394#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x07010000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200395
Gilles Peskine06dc2632018-03-08 07:47:25 +0100396/** DSA public key. */
397#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x06020000)
398/** DSA key pair (private and public key). */
399#define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x07020000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200400
Gilles Peskine06dc2632018-03-08 07:47:25 +0100401#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x06030000)
402#define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x07030000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100403#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200404/** Elliptic curve key pair. */
Gilles Peskine06dc2632018-03-08 07:47:25 +0100405#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
406 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200407/** Elliptic curve public key. */
Gilles Peskine06dc2632018-03-08 07:47:25 +0100408#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
409 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
Gilles Peskine98f0a242018-02-06 18:57:29 +0100410
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100411/** Whether a key type is vendor-defined. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100412#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100413 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100414
415/** Whether a key type is asymmetric: either a key pair or a public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100416#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
417 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100418/** Whether a key type is the public part of a key pair. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100419#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
Moran Pekerb4d0ddd2018-04-04 12:47:52 +0300420 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
421 PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100422/** Whether a key type is a key pair containing a private part and a public
423 * part. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100424#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
425 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
426 (PSA_KEY_TYPE_CATEGORY_ASYMMETRIC | PSA_KEY_TYPE_PAIR_FLAG))
Gilles Peskine06dc2632018-03-08 07:47:25 +0100427/** The key pair type corresponding to a public key type. */
428#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
429 ((type) | PSA_KEY_TYPE_PAIR_FLAG)
430/** The public key type corresponding to a key pair type. */
431#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
432 ((type) & ~PSA_KEY_TYPE_PAIR_FLAG)
Gilles Peskine61a60372018-07-08 21:48:44 +0200433/** Whether a key type is an RSA key pair or public key. */
Gilles Peskine0189e752018-02-03 23:57:22 +0100434#define PSA_KEY_TYPE_IS_RSA(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100435 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
436/** Whether a key type is an elliptic curve key pair or public key. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100437#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100438 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
439 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100440
Gilles Peskinee1fed0d2018-06-18 20:45:45 +0200441/** The type of PSA elliptic curve identifiers. */
442typedef uint16_t psa_ecc_curve_t;
443/** Extract the curve from an elliptic curve key type. */
444#define PSA_KEY_TYPE_GET_CURVE(type) \
445 ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
446 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
447 0))
448
449/* The encoding of curve identifiers is currently aligned with the
450 * TLS Supported Groups Registry (formerly known as the
451 * TLS EC Named Curve Registry)
452 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
453 * The values are defined by RFC 4492, RFC 7027 and RFC 7919. */
454#define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001)
455#define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002)
456#define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003)
457#define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004)
458#define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005)
459#define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006)
460#define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007)
461#define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008)
462#define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009)
463#define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a)
464#define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b)
465#define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c)
466#define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d)
467#define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e)
468#define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f)
469#define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010)
470#define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011)
471#define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012)
472#define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013)
473#define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014)
474#define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015)
475#define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016)
476#define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017)
477#define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018)
478#define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019)
479#define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a)
480#define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b)
481#define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c)
482#define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d)
483#define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e)
484#define PSA_ECC_CURVE_FFDHE_2048 ((psa_ecc_curve_t) 0x0100)
485#define PSA_ECC_CURVE_FFDHE_3072 ((psa_ecc_curve_t) 0x0101)
486#define PSA_ECC_CURVE_FFDHE_4096 ((psa_ecc_curve_t) 0x0102)
487#define PSA_ECC_CURVE_FFDHE_6144 ((psa_ecc_curve_t) 0x0103)
488#define PSA_ECC_CURVE_FFDHE_8192 ((psa_ecc_curve_t) 0x0104)
489
Gilles Peskine7e198532018-03-08 07:50:30 +0100490/** The block size of a block cipher.
491 *
492 * \param type A cipher key type (value of type #psa_key_type_t).
493 *
494 * \return The block size for a block cipher, or 1 for a stream cipher.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200495 * The return value is undefined if \p type is not a supported
Gilles Peskine35855962018-04-19 08:39:16 +0200496 * cipher key type.
497 *
498 * \note It is possible to build stream cipher algorithms on top of a block
499 * cipher, for example CTR mode (#PSA_ALG_CTR).
500 * This macro only takes the key type into account, so it cannot be
501 * used to determine the size of the data that #psa_cipher_update()
502 * might buffer for future processing in general.
Gilles Peskine7e198532018-03-08 07:50:30 +0100503 *
504 * \note This macro returns a compile-time constant if its argument is one.
505 *
506 * \warning This macro may evaluate its argument multiple times.
507 */
Gilles Peskine03182e92018-03-07 16:40:52 +0100508#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100509 ( \
510 (type) == PSA_KEY_TYPE_AES ? 16 : \
511 (type) == PSA_KEY_TYPE_DES ? 8 : \
512 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
Gilles Peskine7e198532018-03-08 07:50:30 +0100513 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100514 0)
515
Gilles Peskine308b91d2018-02-08 09:47:44 +0100516/** \brief Encoding of a cryptographic algorithm.
517 *
518 * For algorithms that can be applied to multiple key types, this type
519 * does not encode the key type. For example, for symmetric ciphers
520 * based on a block cipher, #psa_algorithm_t encodes the block cipher
521 * mode and the padding mode while the block cipher itself is encoded
522 * via #psa_key_type_t.
523 */
Gilles Peskine20035e32018-02-03 22:44:14 +0100524typedef uint32_t psa_algorithm_t;
525
Gilles Peskine98f0a242018-02-06 18:57:29 +0100526#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
527#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
528#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
529#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
530#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
531#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
532#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
533#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
534#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
535#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100536
Gilles Peskine98f0a242018-02-06 18:57:29 +0100537#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
538 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200539
Gilles Peskine308b91d2018-02-08 09:47:44 +0100540/** Whether the specified algorithm is a hash algorithm.
541 *
Gilles Peskine7e198532018-03-08 07:50:30 +0100542 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100543 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200544 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
545 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskine7e198532018-03-08 07:50:30 +0100546 * algorithm identifier.
547 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100548#define PSA_ALG_IS_HASH(alg) \
549 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200550
551/** Whether the specified algorithm is a MAC algorithm.
552 *
553 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
554 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200555 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
556 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200557 * algorithm identifier.
558 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100559#define PSA_ALG_IS_MAC(alg) \
560 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200561
562/** Whether the specified algorithm is a symmetric cipher algorithm.
563 *
564 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
565 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200566 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
567 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200568 * algorithm identifier.
569 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100570#define PSA_ALG_IS_CIPHER(alg) \
571 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200572
573/** Whether the specified algorithm is an authenticated encryption
574 * with associated data (AEAD) algorithm.
575 *
576 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
577 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200578 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
579 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200580 * algorithm identifier.
581 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100582#define PSA_ALG_IS_AEAD(alg) \
583 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200584
585/** Whether the specified algorithm is a public-key signature algorithm.
586 *
587 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
588 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200589 * \return 1 if \p alg is a public-key signature algorithm, 0 otherwise.
590 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200591 * algorithm identifier.
592 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100593#define PSA_ALG_IS_SIGN(alg) \
594 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200595
596/** Whether the specified algorithm is a public-key encryption algorithm.
597 *
598 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
599 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200600 * \return 1 if \p alg is a public-key encryption algorithm, 0 otherwise.
601 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200602 * algorithm identifier.
603 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100604#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
605 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200606
607/** Whether the specified algorithm is a key agreement algorithm.
608 *
609 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
610 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200611 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
612 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200613 * algorithm identifier.
614 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100615#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
616 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200617
618/** Whether the specified algorithm is a key derivation algorithm.
619 *
620 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
621 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200622 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
623 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200624 * algorithm identifier.
625 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100626#define PSA_ALG_IS_KEY_DERIVATION(alg) \
627 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
628
629#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
630#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
631#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
632#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
Gilles Peskinee3f694f2018-03-08 07:48:40 +0100633#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
634#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100635#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
636#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
637#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
638#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
639#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
640#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
641#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
642#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
643#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
644#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
645
Gilles Peskine8c9def32018-02-08 10:02:12 +0100646#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100647#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
Gilles Peskine35855962018-04-19 08:39:16 +0200648/** Macro to build an HMAC algorithm.
649 *
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200650 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
Gilles Peskine35855962018-04-19 08:39:16 +0200651 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200652 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200653 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine35855962018-04-19 08:39:16 +0200654 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200655 * \return The corresponding HMAC algorithm.
656 * \return Unspecified if \p alg is not a supported
657 * hash algorithm.
Gilles Peskine35855962018-04-19 08:39:16 +0200658 */
659#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100660 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200661
Gilles Peskine8c9def32018-02-08 10:02:12 +0100662#define PSA_ALG_HMAC_HASH(hmac_alg) \
663 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200664
665/** Whether the specified algorithm is an HMAC algorithm.
666 *
667 * HMAC is a family of MAC algorithms that are based on a hash function.
668 *
669 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
670 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200671 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
672 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200673 * algorithm identifier.
674 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100675#define PSA_ALG_IS_HMAC(alg) \
676 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
677 PSA_ALG_HMAC_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200678
Gilles Peskine8c9def32018-02-08 10:02:12 +0100679#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
680#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
681#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
682#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200683
684/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
685 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200686 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
687 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200688 * algorithm identifier.
689 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100690#define PSA_ALG_IS_CIPHER_MAC(alg) \
691 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
692 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100693
Gilles Peskine8c9def32018-02-08 10:02:12 +0100694#define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100695#define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100696#define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100697#define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200698
699/** Use a block cipher mode without padding.
700 *
701 * This padding mode may only be used with messages whose lengths are a
702 * whole number of blocks for the chosen block cipher.
703 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100704#define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200705
Gilles Peskine98f0a242018-02-06 18:57:29 +0100706#define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200707
708/** Whether the specified algorithm is a block cipher.
709 *
710 * A block cipher is a symmetric cipher that encrypts or decrypts messages
711 * by chopping them into fixed-size blocks. Processing a message requires
712 * applying a _padding mode_ to transform the message into one whose
713 * length is a whole number of blocks. To construct an algorithm
714 * identifier for a block cipher, apply a bitwise-or between the block
715 * cipher mode and the padding mode. For example, CBC with PKCS#7 padding
716 * is `PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7`.
717 *
718 * The transformation applied to each block is determined by the key type.
719 * For example, to use AES-128-CBC-PKCS7, use the algorithm above with
720 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
721 *
722 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
723 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200724 * \return 1 if \p alg is a block cipher algorithm, 0 otherwise.
725 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200726 * algorithm identifier or if it is not a symmetric cipher algorithm.
727 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100728#define PSA_ALG_IS_BLOCK_CIPHER(alg) \
729 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
730 PSA_ALG_BLOCK_CIPHER_BASE)
731
Gilles Peskinedcd14942018-07-12 00:30:52 +0200732/** The CBC block cipher mode.
733 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100734#define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100735#define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002)
736#define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003)
737#define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004)
Gilles Peskine5d1888e2018-07-12 00:32:42 +0200738
739#define PSA_ALG_STREAM_CIPHER_BASE ((psa_algorithm_t)0x04800000)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200740
Gilles Peskinedcd14942018-07-12 00:30:52 +0200741/** The CTR stream cipher mode.
742 *
743 * CTR is a stream cipher which is built from a block cipher. The
744 * underlying block cipher is determined by the key type. For example,
745 * to use AES-128-CTR, use this algorithm with
746 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
747 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100748#define PSA_ALG_CTR ((psa_algorithm_t)0x04800001)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200749
Gilles Peskinedcd14942018-07-12 00:30:52 +0200750/** The ARC4 stream cipher algorithm.
751 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100752#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100753
Gilles Peskinedcd14942018-07-12 00:30:52 +0200754/** Whether the specified algorithm is a stream cipher.
755 *
756 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
757 * by applying a bitwise-xor with a stream of bytes that is generated
758 * from a key.
759 *
760 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
761 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200762 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
763 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200764 * algorithm identifier or if it is not a symmetric cipher algorithm.
765 */
Moran Pekerbed71a22018-04-22 20:19:20 +0300766#define PSA_ALG_IS_STREAM_CIPHER(alg) \
767 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
Gilles Peskine5d1888e2018-07-12 00:32:42 +0200768 PSA_ALG_STREAM_CIPHER_BASE)
Moran Pekerbed71a22018-04-22 20:19:20 +0300769
Gilles Peskine8c9def32018-02-08 10:02:12 +0100770#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
771#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100772
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200773#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
774/** RSA PKCS#1 v1.5 signature with hashing.
775 *
776 * This is the signature scheme defined by RFC 8017
777 * (PKCS#1: RSA Cryptography Specifications) under the name
778 * RSASSA-PKCS1-v1_5.
779 *
780 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200781 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200782 *
783 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
784 * \return Unspecified if \p alg is not a supported
785 * hash algorithm.
786 */
Gilles Peskinea5926232018-03-28 14:16:50 +0200787#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200788 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
789/** Raw PKCS#1 v1.5 signature.
790 *
791 * The input to this algorithm is the DigestInfo structure used by
792 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
793 * steps 3&ndash;6.
794 */
795#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
Gilles Peskinea5926232018-03-28 14:16:50 +0200796#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200797 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200798
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200799#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
800/** RSA PSS 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-PSS, with the message generation function MGF1. The specified
805 * hash algorithm is used to hash the input message, to create the
806 * salted hash, and for the mask generation.
807 *
808 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200809 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200810 *
811 * \return The corresponding RSA PSS signature algorithm.
812 * \return Unspecified if \p alg is not a supported
813 * hash algorithm.
814 */
815#define PSA_ALG_RSA_PSS(hash_alg) \
816 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
817#define PSA_ALG_IS_RSA_PSS(alg) \
818 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
819
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200820#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
821/** DSA signature with hashing.
822 *
823 * This is the signature scheme defined by FIPS 186-4,
824 * with a random per-message secret number (*k*).
825 *
826 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200827 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200828 *
829 * \return The corresponding DSA signature algorithm.
830 * \return Unspecified if \p alg is not a supported
831 * hash algorithm.
832 */
833#define PSA_ALG_DSA(hash_alg) \
834 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
835#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
836#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
837#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
838 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
839#define PSA_ALG_IS_DSA(alg) \
840 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
841 PSA_ALG_DSA_BASE)
842#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
843 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
844
845#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
846/** ECDSA signature with hashing.
847 *
848 * This is the ECDSA signature scheme defined by ANSI X9.62,
849 * with a random per-message secret number (*k*).
850 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200851 * The representation of the signature as a byte string consists of
852 * the concatentation of the signature values *r* and *s*. Each of
853 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
854 * of the base point of the curve in octets. Each value is represented
855 * in big-endian order (most significant octet first).
856 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200857 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200858 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200859 *
860 * \return The corresponding ECDSA signature algorithm.
861 * \return Unspecified if \p alg is not a supported
862 * hash algorithm.
863 */
864#define PSA_ALG_ECDSA(hash_alg) \
865 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
866/** ECDSA signature without hashing.
867 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200868 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200869 * without specifying a hash algorithm. This algorithm may only be
870 * used to sign or verify a sequence of bytes that should be an
871 * already-calculated hash. Note that the input is padded with
872 * zeros on the left or truncated on the left as required to fit
873 * the curve size.
874 */
875#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
876#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
877/** Deterministic ECDSA signature with hashing.
878 *
879 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
880 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200881 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
882 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200883 * Note that when this algorithm is used for verification, signatures
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200884 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200885 * same private key are accepted. In other words,
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200886 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
887 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200888 *
889 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200890 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200891 *
892 * \return The corresponding deterministic ECDSA signature
893 * algorithm.
894 * \return Unspecified if \p alg is not a supported
895 * hash algorithm.
896 */
897#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
898 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
899#define PSA_ALG_IS_ECDSA(alg) \
900 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
901 PSA_ALG_ECDSA_BASE)
902#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
903 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
904
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200905/** Get the hash used by a hash-and-sign signature algorithm.
906 *
907 * A hash-and-sign algorithm is a signature algorithm which is
908 * composed of two phases: first a hashing phase which does not use
909 * the key and produces a hash of the input message, then a signing
910 * phase which only uses the hash and the key and not the message
911 * itself.
912 *
913 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200914 * #PSA_ALG_IS_SIGN(\p alg) is true).
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200915 *
916 * \return The underlying hash algorithm if \p alg is a hash-and-sign
917 * algorithm.
918 * \return 0 if \p alg is a signature algorithm that does not
919 * follow the hash-and-sign structure.
920 * \return Unspecified if \p alg is not a signature algorithm or
921 * if it is not supported by the implementation.
922 */
923#define PSA_ALG_SIGN_GET_HASH(alg) \
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200924 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
925 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg) ? \
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200926 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
927 0)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100928
Gilles Peskinedcd14942018-07-12 00:30:52 +0200929/** RSA PKCS#1 v1.5 encryption.
930 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200931#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200932
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200933#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200934/** RSA OAEP encryption.
935 *
936 * This is the encryption scheme defined by RFC 8017
937 * (PKCS#1: RSA Cryptography Specifications) under the name
938 * RSAES-OAEP, with the message generation function MGF1.
939 *
940 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
941 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
942 * for MGF1.
943 *
944 * \return The corresponding RSA OAEP signature algorithm.
945 * \return Unspecified if \p alg is not a supported
946 * hash algorithm.
947 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200948#define PSA_ALG_RSA_OAEP(hash_alg) \
949 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
950#define PSA_ALG_IS_RSA_OAEP(alg) \
951 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
Gilles Peskined1e8e412018-06-07 09:49:39 +0200952
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100953/**@}*/
954
955/** \defgroup key_management Key management
956 * @{
957 */
958
959/**
960 * \brief Import a key in binary format.
961 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100962 * This function supports any output from psa_export_key(). Refer to the
963 * documentation of psa_export_key() for the format for each key type.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100964 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100965 * \param key Slot where the key will be stored. This must be a
966 * valid slot for a key of the chosen type. It must
967 * be unoccupied.
968 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskineedd11a12018-07-12 01:08:58 +0200969 * \param[in] data Buffer containing the key data.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200970 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100971 *
Gilles Peskine28538492018-07-11 17:34:00 +0200972 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100973 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200974 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200975 * The key type or key size is not supported, either by the
976 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200977 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +0100978 * The key slot is invalid,
979 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200980 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +0200981 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200982 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
983 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
984 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
985 * \retval #PSA_ERROR_HARDWARE_FAILURE
986 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100987 */
988psa_status_t psa_import_key(psa_key_slot_t key,
989 psa_key_type_t type,
990 const uint8_t *data,
991 size_t data_length);
992
993/**
Gilles Peskine154bd952018-04-19 08:38:16 +0200994 * \brief Destroy a key and restore the slot to its default state.
995 *
996 * This function destroys the content of the key slot from both volatile
997 * memory and, if applicable, non-volatile storage. Implementations shall
998 * make a best effort to ensure that any previous content of the slot is
999 * unrecoverable.
1000 *
1001 * This function also erases any metadata such as policies. It returns the
1002 * specified slot to its default state.
1003 *
1004 * \param key The key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001005 *
Gilles Peskine28538492018-07-11 17:34:00 +02001006 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +02001007 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +02001008 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001009 * The slot holds content and cannot be erased because it is
1010 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskine28538492018-07-11 17:34:00 +02001011 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001012 * The specified slot number does not designate a valid slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001013 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001014 * There was an failure in communication with the cryptoprocessor.
1015 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +02001016 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001017 * The storage is corrupted. Implementations shall make a best effort
1018 * to erase key material even in this stage, however applications
1019 * should be aware that it may be impossible to guarantee that the
1020 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +02001021 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001022 * An unexpected condition which is not a storage corruption or
1023 * a communication failure occurred. The cryptoprocessor may have
1024 * been compromised.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001025 */
1026psa_status_t psa_destroy_key(psa_key_slot_t key);
1027
1028/**
1029 * \brief Get basic metadata about a key.
1030 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001031 * \param key Slot whose content is queried. This must
1032 * be an occupied key slot.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001033 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001034 * This may be a null pointer, in which case the key type
1035 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001036 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +01001037 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +01001038 * is not written.
1039 *
Gilles Peskine28538492018-07-11 17:34:00 +02001040 * \retval #PSA_SUCCESS
1041 * \retval #PSA_ERROR_EMPTY_SLOT
1042 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1043 * \retval #PSA_ERROR_HARDWARE_FAILURE
1044 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001045 */
1046psa_status_t psa_get_key_information(psa_key_slot_t key,
1047 psa_key_type_t *type,
1048 size_t *bits);
1049
1050/**
1051 * \brief Export a key in binary format.
1052 *
1053 * The output of this function can be passed to psa_import_key() to
1054 * create an equivalent object.
1055 *
1056 * If a key is created with psa_import_key() and then exported with
1057 * this function, it is not guaranteed that the resulting data is
1058 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +01001059 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001060 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001061 * For standard key types, the output format is as follows:
1062 *
1063 * - For symmetric keys (including MAC keys), the format is the
1064 * raw bytes of the key.
1065 * - For DES, the key data consists of 8 bytes. The parity bits must be
1066 * correct.
1067 * - For Triple-DES, the format is the concatenation of the
1068 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +01001069 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine2743e422018-06-27 22:57:11 +02001070 * is the non-encrypted DER representation defined by PKCS\#1 (RFC 8017)
1071 * as RSAPrivateKey.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001072 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
Gilles Peskine971f7062018-03-20 17:52:58 +01001073 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001075 * \param key Slot whose content is to be exported. This must
1076 * be an occupied key slot.
1077 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001078 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001079 * \param[out] data_length On success, the number of bytes
1080 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001081 *
Gilles Peskine28538492018-07-11 17:34:00 +02001082 * \retval #PSA_SUCCESS
1083 * \retval #PSA_ERROR_EMPTY_SLOT
1084 * \retval #PSA_ERROR_NOT_PERMITTED
1085 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1086 * \retval #PSA_ERROR_HARDWARE_FAILURE
1087 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001088 */
1089psa_status_t psa_export_key(psa_key_slot_t key,
1090 uint8_t *data,
1091 size_t data_size,
1092 size_t *data_length);
1093
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001094/**
1095 * \brief Export a public key or the public part of a key pair in binary format.
1096 *
1097 * The output of this function can be passed to psa_import_key() to
1098 * create an object that is equivalent to the public key.
1099 *
1100 * For standard key types, the output format is as follows:
1101 *
1102 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
Moran Pekerdd4ea382018-04-03 15:30:03 +03001103 * the format is the DER representation of the public key defined by RFC 5280
Gilles Peskine971f7062018-03-20 17:52:58 +01001104 * as SubjectPublicKeyInfo.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001105 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001106 * \param key Slot whose content is to be exported. This must
1107 * be an occupied key slot.
1108 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001109 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001110 * \param[out] data_length On success, the number of bytes
1111 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001112 *
Gilles Peskine28538492018-07-11 17:34:00 +02001113 * \retval #PSA_SUCCESS
1114 * \retval #PSA_ERROR_EMPTY_SLOT
1115 * \retval #PSA_ERROR_INVALID_ARGUMENT
1116 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1117 * \retval #PSA_ERROR_HARDWARE_FAILURE
1118 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001119 */
1120psa_status_t psa_export_public_key(psa_key_slot_t key,
1121 uint8_t *data,
1122 size_t data_size,
1123 size_t *data_length);
1124
1125/**@}*/
1126
1127/** \defgroup policy Key policies
1128 * @{
1129 */
1130
1131/** \brief Encoding of permitted usage on a key. */
1132typedef uint32_t psa_key_usage_t;
1133
Gilles Peskine7e198532018-03-08 07:50:30 +01001134/** Whether the key may be exported.
1135 *
1136 * A public key or the public part of a key pair may always be exported
1137 * regardless of the value of this permission flag.
1138 *
1139 * If a key does not have export permission, implementations shall not
1140 * allow the key to be exported in plain form from the cryptoprocessor,
1141 * whether through psa_export_key() or through a proprietary interface.
1142 * The key may however be exportable in a wrapped form, i.e. in a form
1143 * where it is encrypted by another key.
1144 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001145#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1146
Gilles Peskine7e198532018-03-08 07:50:30 +01001147/** Whether the key may be used to encrypt a message.
1148 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001149 * This flag allows the key to be used for a symmetric encryption operation,
1150 * for an AEAD encryption-and-authentication operation,
1151 * or for an asymmetric encryption operation,
1152 * if otherwise permitted by the key's type and policy.
1153 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001154 * For a key pair, this concerns the public key.
1155 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001156#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +01001157
1158/** Whether the key may be used to decrypt a message.
1159 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001160 * This flag allows the key to be used for a symmetric decryption operation,
1161 * for an AEAD decryption-and-verification operation,
1162 * or for an asymmetric decryption operation,
1163 * if otherwise permitted by the key's type and policy.
1164 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001165 * For a key pair, this concerns the private key.
1166 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001167#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +01001168
1169/** Whether the key may be used to sign a message.
1170 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001171 * This flag allows the key to be used for a MAC calculation operation
1172 * or for an asymmetric signature operation,
1173 * if otherwise permitted by the key's type and policy.
1174 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001175 * For a key pair, this concerns the private key.
1176 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001177#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +01001178
1179/** Whether the key may be used to verify a message signature.
1180 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001181 * This flag allows the key to be used for a MAC verification operation
1182 * or for an asymmetric signature verification operation,
1183 * if otherwise permitted by by the key's type and policy.
1184 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001185 * For a key pair, this concerns the public key.
1186 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001187#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
1188
1189/** The type of the key policy data structure.
1190 *
1191 * This is an implementation-defined \c struct. Applications should not
1192 * make any assumptions about the content of this structure except
1193 * as directed by the documentation of a specific implementation. */
1194typedef struct psa_key_policy_s psa_key_policy_t;
1195
1196/** \brief Initialize a key policy structure to a default that forbids all
1197 * usage of the key. */
1198void psa_key_policy_init(psa_key_policy_t *policy);
1199
Gilles Peskine7e198532018-03-08 07:50:30 +01001200/** \brief Set the standard fields of a policy structure.
1201 *
1202 * Note that this function does not make any consistency check of the
1203 * parameters. The values are only checked when applying the policy to
1204 * a key slot with psa_set_key_policy().
1205 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001206void psa_key_policy_set_usage(psa_key_policy_t *policy,
1207 psa_key_usage_t usage,
1208 psa_algorithm_t alg);
1209
Gilles Peskinedcd14942018-07-12 00:30:52 +02001210/** \brief Retrieve the usage field of a policy structure. */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001211psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001212
Gilles Peskinedcd14942018-07-12 00:30:52 +02001213/** \brief Retrieve the algorithm field of a policy structure. */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001214psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001215
1216/** \brief Set the usage policy on a key slot.
1217 *
1218 * This function must be called on an empty key slot, before importing,
1219 * generating or creating a key in the slot. Changing the policy of an
1220 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +01001221 *
1222 * Implementations may set restrictions on supported key policies
1223 * depending on the key type and the key slot.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001224 */
1225psa_status_t psa_set_key_policy(psa_key_slot_t key,
1226 const psa_key_policy_t *policy);
1227
Gilles Peskine7e198532018-03-08 07:50:30 +01001228/** \brief Get the usage policy for a key slot.
1229 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001230psa_status_t psa_get_key_policy(psa_key_slot_t key,
1231 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +01001232
1233/**@}*/
1234
Gilles Peskine609b6a52018-03-03 21:31:50 +01001235/** \defgroup persistence Key lifetime
1236 * @{
1237 */
1238
1239/** Encoding of key lifetimes.
1240 */
1241typedef uint32_t psa_key_lifetime_t;
1242
1243/** A volatile key slot retains its content as long as the application is
1244 * running. It is guaranteed to be erased on a power reset.
1245 */
1246#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1247
1248/** A persistent key slot retains its content as long as it is not explicitly
1249 * destroyed.
1250 */
1251#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1252
1253/** A write-once key slot may not be modified once a key has been set.
1254 * It will retain its content as long as the device remains operational.
1255 */
1256#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
1257
Gilles Peskined393e182018-03-08 07:49:16 +01001258/** \brief Retrieve the lifetime of a key slot.
1259 *
1260 * The assignment of lifetimes to slots is implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001261 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001262 * \param key Slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001263 * \param[out] lifetime On success, the lifetime value.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001264 *
Gilles Peskine28538492018-07-11 17:34:00 +02001265 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001266 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001267 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603a7d245a2018-04-17 00:40:08 -07001268 * The key slot is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001269 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1270 * \retval #PSA_ERROR_HARDWARE_FAILURE
1271 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +01001272 */
Gilles Peskine609b6a52018-03-03 21:31:50 +01001273psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
1274 psa_key_lifetime_t *lifetime);
1275
Gilles Peskined393e182018-03-08 07:49:16 +01001276/** \brief Change the lifetime of a key slot.
1277 *
1278 * Whether the lifetime of a key slot can be changed at all, and if so
Gilles Peskine19067982018-03-20 17:54:53 +01001279 * whether the lifetime of an occupied key slot can be changed, is
Gilles Peskined393e182018-03-08 07:49:16 +01001280 * implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001281 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001282 * \param key Slot whose lifetime is to be changed.
1283 * \param lifetime The lifetime value to set for the given key slot.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001284 *
Gilles Peskine28538492018-07-11 17:34:00 +02001285 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001286 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001287 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603804cd712018-03-20 22:44:08 +02001288 * The key slot is invalid,
mohammad1603a7d245a2018-04-17 00:40:08 -07001289 * or the lifetime value is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001290 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001291 * The implementation does not support the specified lifetime value,
1292 * at least for the specified key slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001293 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001294 * The slot contains a key, and the implementation does not support
1295 * changing the lifetime of an occupied slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001296 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1297 * \retval #PSA_ERROR_HARDWARE_FAILURE
1298 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +01001299 */
1300psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
mohammad1603ea050092018-04-17 00:31:34 -07001301 psa_key_lifetime_t lifetime);
Gilles Peskined393e182018-03-08 07:49:16 +01001302
Gilles Peskine609b6a52018-03-03 21:31:50 +01001303/**@}*/
1304
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001305/** \defgroup hash Message digests
1306 * @{
1307 */
1308
Gilles Peskine308b91d2018-02-08 09:47:44 +01001309/** The type of the state data structure for multipart hash operations.
1310 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001311 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01001312 * make any assumptions about the content of this structure except
1313 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001314typedef struct psa_hash_operation_s psa_hash_operation_t;
1315
Gilles Peskine308b91d2018-02-08 09:47:44 +01001316/** The size of the output of psa_hash_finish(), in bytes.
1317 *
1318 * This is also the hash size that psa_hash_verify() expects.
1319 *
1320 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001321 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
Gilles Peskine28538492018-07-11 17:34:00 +02001322 * (#PSA_ALG_HMAC(`hash_alg`) where `hash_alg` is a
Gilles Peskine35855962018-04-19 08:39:16 +02001323 * hash algorithm).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001324 *
1325 * \return The hash size for the specified hash algorithm.
1326 * If the hash algorithm is not recognized, return 0.
1327 * An implementation may return either 0 or the correct size
1328 * for a hash algorithm that it recognizes, but does not support.
1329 */
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001330#define PSA_HASH_SIZE(alg) \
1331 ( \
1332 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD2 ? 16 : \
1333 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD4 ? 16 : \
1334 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD5 ? 16 : \
1335 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
1336 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
1337 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
1338 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
1339 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
1340 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
1341 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
1342 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
1343 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
1344 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
1345 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
1346 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001347 0)
1348
Gilles Peskine308b91d2018-02-08 09:47:44 +01001349/** Start a multipart hash operation.
1350 *
1351 * The sequence of operations to calculate a hash (message digest)
1352 * is as follows:
1353 * -# Allocate an operation object which will be passed to all the functions
1354 * listed here.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001355 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001356 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001357 * of the message each time. The hash that is calculated is the hash
1358 * of the concatenation of these messages in order.
1359 * -# To calculate the hash, call psa_hash_finish().
1360 * To compare the hash with an expected value, call psa_hash_verify().
1361 *
1362 * The application may call psa_hash_abort() at any time after the operation
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001363 * has been initialized with psa_hash_setup().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001364 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001365 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001366 * eventually terminate the operation. The following events terminate an
1367 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001368 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001369 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001370 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001371 * \param[out] operation The operation object to use.
1372 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1373 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001374 *
Gilles Peskine28538492018-07-11 17:34:00 +02001375 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001376 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001377 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001378 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001379 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1380 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1381 * \retval #PSA_ERROR_HARDWARE_FAILURE
1382 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001383 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001384psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001385 psa_algorithm_t alg);
1386
Gilles Peskine308b91d2018-02-08 09:47:44 +01001387/** Add a message fragment to a multipart hash operation.
1388 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001389 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001390 *
1391 * If this function returns an error status, the operation becomes inactive.
1392 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001393 * \param[in,out] operation Active hash operation.
1394 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001395 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001396 *
Gilles Peskine28538492018-07-11 17:34:00 +02001397 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001398 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001399 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001400 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001401 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1402 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1403 * \retval #PSA_ERROR_HARDWARE_FAILURE
1404 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001405 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001406psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1407 const uint8_t *input,
1408 size_t input_length);
1409
Gilles Peskine308b91d2018-02-08 09:47:44 +01001410/** Finish the calculation of the hash of a message.
1411 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001412 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001413 * This function calculates the hash of the message formed by concatenating
1414 * the inputs passed to preceding calls to psa_hash_update().
1415 *
1416 * When this function returns, the operation becomes inactive.
1417 *
1418 * \warning Applications should not call this function if they expect
1419 * a specific value for the hash. Call psa_hash_verify() instead.
1420 * Beware that comparing integrity or authenticity data such as
1421 * hash values with a function such as \c memcmp is risky
1422 * because the time taken by the comparison may leak information
1423 * about the hashed data which could allow an attacker to guess
1424 * a valid hash and thereby bypass security controls.
1425 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001426 * \param[in,out] operation Active hash operation.
1427 * \param[out] hash Buffer where the hash is to be written.
1428 * \param hash_size Size of the \p hash buffer in bytes.
1429 * \param[out] hash_length On success, the number of bytes
1430 * that make up the hash value. This is always
1431 * #PSA_HASH_SIZE(`alg`) where `alg` is the
1432 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001433 *
Gilles Peskine28538492018-07-11 17:34:00 +02001434 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001435 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001436 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001437 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001438 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001439 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001440 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001441 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001442 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1443 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1444 * \retval #PSA_ERROR_HARDWARE_FAILURE
1445 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001446 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001447psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1448 uint8_t *hash,
1449 size_t hash_size,
1450 size_t *hash_length);
1451
Gilles Peskine308b91d2018-02-08 09:47:44 +01001452/** Finish the calculation of the hash of a message and compare it with
1453 * an expected value.
1454 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001455 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001456 * This function calculates the hash of the message formed by concatenating
1457 * the inputs passed to preceding calls to psa_hash_update(). It then
1458 * compares the calculated hash with the expected hash passed as a
1459 * parameter to this function.
1460 *
1461 * When this function returns, the operation becomes inactive.
1462 *
Gilles Peskine19067982018-03-20 17:54:53 +01001463 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001464 * comparison between the actual hash and the expected hash is performed
1465 * in constant time.
1466 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001467 * \param[in,out] operation Active hash operation.
1468 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001469 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001470 *
Gilles Peskine28538492018-07-11 17:34:00 +02001471 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001472 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001473 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001474 * The hash of the message was calculated successfully, but it
1475 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001476 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001477 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001478 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1479 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1480 * \retval #PSA_ERROR_HARDWARE_FAILURE
1481 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001482 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001483psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1484 const uint8_t *hash,
1485 size_t hash_length);
1486
Gilles Peskine308b91d2018-02-08 09:47:44 +01001487/** Abort a hash operation.
1488 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001489 * This function may be called at any time after psa_hash_setup().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001490 * Aborting an operation frees all associated resources except for the
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001491 * \p operation structure itself.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001492 *
1493 * Implementation should strive to be robust and handle inactive hash
1494 * operations safely (do nothing and return #PSA_ERROR_BAD_STATE). However,
1495 * application writers should beware that uninitialized memory may happen
1496 * to be indistinguishable from an active hash operation, and the behavior
1497 * of psa_hash_abort() is undefined in this case.
1498 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001499 * \param[in,out] operation Active hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001500 *
Gilles Peskine28538492018-07-11 17:34:00 +02001501 * \retval #PSA_SUCCESS
1502 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001503 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001504 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1505 * \retval #PSA_ERROR_HARDWARE_FAILURE
1506 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001507 */
1508psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001509
1510/**@}*/
1511
Gilles Peskine8c9def32018-02-08 10:02:12 +01001512/** \defgroup MAC Message authentication codes
1513 * @{
1514 */
1515
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001516/** The type of the state data structure for multipart MAC operations.
1517 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001518 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001519 * make any assumptions about the content of this structure except
1520 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001521typedef struct psa_mac_operation_s psa_mac_operation_t;
1522
Gilles Peskine89167cb2018-07-08 20:12:23 +02001523/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001524 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001525 * This function sets up the calculation of the MAC
1526 * (message authentication code) of a byte string.
1527 * To verify the MAC of a message against an
1528 * expected value, use psa_mac_verify_setup() instead.
1529 *
1530 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001531 * -# Allocate an operation object which will be passed to all the functions
1532 * listed here.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001533 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001534 * The key remains associated with the operation even if the content
1535 * of the key slot changes.
1536 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1537 * of the message each time. The MAC that is calculated is the MAC
1538 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001539 * -# At the end of the message, call psa_mac_sign_finish() to finish
1540 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001541 *
1542 * The application may call psa_mac_abort() at any time after the operation
Gilles Peskine89167cb2018-07-08 20:12:23 +02001543 * has been initialized with psa_mac_sign_setup().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001544 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001545 * After a successful call to psa_mac_sign_setup(), the application must
1546 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001547 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001548 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001549 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001550 * \param[out] operation The operation object to use.
1551 * \param key Slot containing the key to use for the operation.
1552 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1553 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001554 *
Gilles Peskine28538492018-07-11 17:34:00 +02001555 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001556 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001557 * \retval #PSA_ERROR_EMPTY_SLOT
1558 * \retval #PSA_ERROR_NOT_PERMITTED
1559 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001560 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001561 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001562 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001563 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1564 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1565 * \retval #PSA_ERROR_HARDWARE_FAILURE
1566 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001567 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001568psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
1569 psa_key_slot_t key,
1570 psa_algorithm_t alg);
1571
1572/** Start a multipart MAC verification operation.
1573 *
1574 * This function sets up the verification of the MAC
1575 * (message authentication code) of a byte string against an expected value.
1576 *
1577 * The sequence of operations to verify a MAC is as follows:
1578 * -# Allocate an operation object which will be passed to all the functions
1579 * listed here.
1580 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1581 * The key remains associated with the operation even if the content
1582 * of the key slot changes.
1583 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1584 * of the message each time. The MAC that is calculated is the MAC
1585 * of the concatenation of these messages in order.
1586 * -# At the end of the message, call psa_mac_verify_finish() to finish
1587 * calculating the actual MAC of the message and verify it against
1588 * the expected value.
1589 *
1590 * The application may call psa_mac_abort() at any time after the operation
1591 * has been initialized with psa_mac_verify_setup().
1592 *
1593 * After a successful call to psa_mac_verify_setup(), the application must
1594 * eventually terminate the operation through one of the following methods:
1595 * - A failed call to psa_mac_update().
1596 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1597 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001598 * \param[out] operation The operation object to use.
1599 * \param key Slot containing the key to use for the operation.
1600 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1601 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001602 *
Gilles Peskine28538492018-07-11 17:34:00 +02001603 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001604 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001605 * \retval #PSA_ERROR_EMPTY_SLOT
1606 * \retval #PSA_ERROR_NOT_PERMITTED
1607 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001608 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001609 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001610 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001611 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1612 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1613 * \retval #PSA_ERROR_HARDWARE_FAILURE
1614 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001615 */
1616psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
1617 psa_key_slot_t key,
1618 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001619
Gilles Peskinedcd14942018-07-12 00:30:52 +02001620/** Add a message fragment to a multipart MAC operation.
1621 *
1622 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1623 * before calling this function.
1624 *
1625 * If this function returns an error status, the operation becomes inactive.
1626 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001627 * \param[in,out] operation Active MAC operation.
1628 * \param[in] input Buffer containing the message fragment to add to
1629 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001630 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001631 *
1632 * \retval #PSA_SUCCESS
1633 * Success.
1634 * \retval #PSA_ERROR_BAD_STATE
1635 * The operation state is not valid (not started, or already completed).
1636 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1637 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1638 * \retval #PSA_ERROR_HARDWARE_FAILURE
1639 * \retval #PSA_ERROR_TAMPERING_DETECTED
1640 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001641psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1642 const uint8_t *input,
1643 size_t input_length);
1644
Gilles Peskinedcd14942018-07-12 00:30:52 +02001645/** Finish the calculation of the MAC of a message.
1646 *
1647 * The application must call psa_mac_sign_setup() before calling this function.
1648 * This function calculates the MAC of the message formed by concatenating
1649 * the inputs passed to preceding calls to psa_mac_update().
1650 *
1651 * When this function returns, the operation becomes inactive.
1652 *
1653 * \warning Applications should not call this function if they expect
1654 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1655 * Beware that comparing integrity or authenticity data such as
1656 * MAC values with a function such as \c memcmp is risky
1657 * because the time taken by the comparison may leak information
1658 * about the MAC value which could allow an attacker to guess
1659 * a valid MAC and thereby bypass security controls.
1660 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001661 * \param[in,out] operation Active MAC operation.
1662 * \param[out] mac Buffer where the MAC value is to be written.
1663 * \param mac_size Size of the \p mac buffer in bytes.
1664 * \param[out] mac_length On success, the number of bytes
1665 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001666 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001667 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001668 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001669 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001670 *
1671 * \retval #PSA_SUCCESS
1672 * Success.
1673 * \retval #PSA_ERROR_BAD_STATE
1674 * The operation state is not valid (not started, or already completed).
1675 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001676 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001677 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1678 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1679 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1680 * \retval #PSA_ERROR_HARDWARE_FAILURE
1681 * \retval #PSA_ERROR_TAMPERING_DETECTED
1682 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001683psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1684 uint8_t *mac,
1685 size_t mac_size,
1686 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001687
Gilles Peskinedcd14942018-07-12 00:30:52 +02001688/** Finish the calculation of the MAC of a message and compare it with
1689 * an expected value.
1690 *
1691 * The application must call psa_mac_verify_setup() before calling this function.
1692 * This function calculates the MAC of the message formed by concatenating
1693 * the inputs passed to preceding calls to psa_mac_update(). It then
1694 * compares the calculated MAC with the expected MAC passed as a
1695 * parameter to this function.
1696 *
1697 * When this function returns, the operation becomes inactive.
1698 *
1699 * \note Implementations shall make the best effort to ensure that the
1700 * comparison between the actual MAC and the expected MAC is performed
1701 * in constant time.
1702 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001703 * \param[in,out] operation Active MAC operation.
1704 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001705 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001706 *
1707 * \retval #PSA_SUCCESS
1708 * The expected MAC is identical to the actual MAC of the message.
1709 * \retval #PSA_ERROR_INVALID_SIGNATURE
1710 * The MAC of the message was calculated successfully, but it
1711 * differs from the expected MAC.
1712 * \retval #PSA_ERROR_BAD_STATE
1713 * The operation state is not valid (not started, or already completed).
1714 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1715 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1716 * \retval #PSA_ERROR_HARDWARE_FAILURE
1717 * \retval #PSA_ERROR_TAMPERING_DETECTED
1718 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001719psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1720 const uint8_t *mac,
1721 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001722
Gilles Peskinedcd14942018-07-12 00:30:52 +02001723/** Abort a MAC operation.
1724 *
1725 * This function may be called at any time after psa_mac_sign_setup()
1726 * or psa_mac_verify_setup().
1727 * Aborting an operation frees all associated resources except for the
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001728 * \p operation structure itself.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001729 *
1730 * Implementation should strive to be robust and handle inactive MAC
1731 * operations safely (do nothing and return #PSA_ERROR_BAD_STATE). However,
1732 * application writers should beware that uninitialized memory may happen
1733 * to be indistinguishable from an active MAC operation, and the behavior
1734 * of psa_mac_abort() is undefined in this case.
1735 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001736 * \param[in,out] operation Active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001737 *
1738 * \retval #PSA_SUCCESS
1739 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001740 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001741 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1742 * \retval #PSA_ERROR_HARDWARE_FAILURE
1743 * \retval #PSA_ERROR_TAMPERING_DETECTED
1744 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001745psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1746
1747/**@}*/
1748
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001749/** \defgroup cipher Symmetric ciphers
1750 * @{
1751 */
1752
1753/** The type of the state data structure for multipart cipher operations.
1754 *
1755 * This is an implementation-defined \c struct. Applications should not
1756 * make any assumptions about the content of this structure except
1757 * as directed by the documentation of a specific implementation. */
1758typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1759
1760/** Set the key for a multipart symmetric encryption operation.
1761 *
1762 * The sequence of operations to encrypt a message with a symmetric cipher
1763 * is as follows:
1764 * -# Allocate an operation object which will be passed to all the functions
1765 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02001766 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001767 * The key remains associated with the operation even if the content
1768 * of the key slot changes.
Gilles Peskinefe119512018-07-08 21:39:34 +02001769 * -# Call either psa_encrypt_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001770 * generate or set the IV (initialization vector). You should use
1771 * psa_encrypt_generate_iv() unless the protocol you are implementing
1772 * requires a specific IV value.
1773 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1774 * of the message each time.
1775 * -# Call psa_cipher_finish().
1776 *
1777 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02001778 * has been initialized with psa_cipher_encrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001779 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001780 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001781 * eventually terminate the operation. The following events terminate an
1782 * operation:
Gilles Peskinefe119512018-07-08 21:39:34 +02001783 * - A failed call to psa_encrypt_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001784 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001785 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001786 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001787 * \param[out] operation The operation object to use.
1788 * \param key Slot containing the key to use for the operation.
1789 * \param alg The cipher algorithm to compute
1790 * (\c PSA_ALG_XXX value such that
1791 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001792 *
Gilles Peskine28538492018-07-11 17:34:00 +02001793 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001794 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001795 * \retval #PSA_ERROR_EMPTY_SLOT
1796 * \retval #PSA_ERROR_NOT_PERMITTED
1797 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001798 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001799 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001800 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001801 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1802 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1803 * \retval #PSA_ERROR_HARDWARE_FAILURE
1804 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001805 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001806psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
1807 psa_key_slot_t key,
1808 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001809
1810/** Set the key for a multipart symmetric decryption operation.
1811 *
1812 * The sequence of operations to decrypt a message with a symmetric cipher
1813 * is as follows:
1814 * -# Allocate an operation object which will be passed to all the functions
1815 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02001816 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001817 * The key remains associated with the operation even if the content
1818 * of the key slot changes.
1819 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1820 * decryption. If the IV is prepended to the ciphertext, you can call
1821 * psa_cipher_update() on a buffer containing the IV followed by the
1822 * beginning of the message.
1823 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1824 * of the message each time.
1825 * -# Call psa_cipher_finish().
1826 *
1827 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02001828 * has been initialized with psa_cipher_decrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001829 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001830 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001831 * eventually terminate the operation. The following events terminate an
1832 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001833 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001834 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001835 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001836 * \param[out] operation The operation object to use.
1837 * \param key Slot containing the key to use for the operation.
1838 * \param alg The cipher algorithm to compute
1839 * (\c PSA_ALG_XXX value such that
1840 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001841 *
Gilles Peskine28538492018-07-11 17:34:00 +02001842 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001843 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001844 * \retval #PSA_ERROR_EMPTY_SLOT
1845 * \retval #PSA_ERROR_NOT_PERMITTED
1846 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001847 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001848 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001849 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001850 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1851 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1852 * \retval #PSA_ERROR_HARDWARE_FAILURE
1853 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001854 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001855psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
1856 psa_key_slot_t key,
1857 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001858
Gilles Peskinedcd14942018-07-12 00:30:52 +02001859/** Generate an IV for a symmetric encryption operation.
1860 *
1861 * This function generates a random IV (initialization vector), nonce
1862 * or initial counter value for the encryption operation as appropriate
1863 * for the chosen algorithm, key type and key size.
1864 *
1865 * The application must call psa_cipher_encrypt_setup() before
1866 * calling this function.
1867 *
1868 * If this function returns an error status, the operation becomes inactive.
1869 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001870 * \param[in,out] operation Active cipher operation.
1871 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001872 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001873 * \param[out] iv_length On success, the number of bytes of the
1874 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001875 *
1876 * \retval #PSA_SUCCESS
1877 * Success.
1878 * \retval #PSA_ERROR_BAD_STATE
1879 * The operation state is not valid (not started, or IV already set).
1880 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001881 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001882 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1883 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1884 * \retval #PSA_ERROR_HARDWARE_FAILURE
1885 * \retval #PSA_ERROR_TAMPERING_DETECTED
1886 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001887psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1888 unsigned char *iv,
1889 size_t iv_size,
1890 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001891
Gilles Peskinedcd14942018-07-12 00:30:52 +02001892/** Set the IV for a symmetric encryption or decryption operation.
1893 *
1894 * This function sets the random IV (initialization vector), nonce
1895 * or initial counter value for the encryption or decryption operation.
1896 *
1897 * The application must call psa_cipher_encrypt_setup() before
1898 * calling this function.
1899 *
1900 * If this function returns an error status, the operation becomes inactive.
1901 *
1902 * \note When encrypting, applications should use psa_cipher_generate_iv()
1903 * instead of this function, unless implementing a protocol that requires
1904 * a non-random IV.
1905 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001906 * \param[in,out] operation Active cipher operation.
1907 * \param[in] iv Buffer containing the IV to use.
1908 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001909 *
1910 * \retval #PSA_SUCCESS
1911 * Success.
1912 * \retval #PSA_ERROR_BAD_STATE
1913 * The operation state is not valid (not started, or IV already set).
1914 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001915 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001916 * or the chosen algorithm does not use an IV.
1917 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1918 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1919 * \retval #PSA_ERROR_HARDWARE_FAILURE
1920 * \retval #PSA_ERROR_TAMPERING_DETECTED
1921 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001922psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1923 const unsigned char *iv,
1924 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001925
Gilles Peskinedcd14942018-07-12 00:30:52 +02001926/** Encrypt or decrypt a message fragment in an active cipher operation.
1927 *
1928 * The application must call psa_cipher_encrypt_setup() or
1929 * psa_cipher_decrypt_setup() before calling this function. The choice
1930 * of setup function determines whether this function encrypts or
1931 * decrypts its input. After calling a setup function, if the chosen
1932 * algorithm requires an IV, the application must call
1933 * psa_cipher_generate_iv() or psa_cipher_set_iv().
1934 *
1935 * If this function returns an error status, the operation becomes inactive.
1936 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001937 * \param[in,out] operation Active cipher operation.
1938 * \param[in] input Buffer containing the message fragment to
1939 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001940 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001941 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001942 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001943 * \param[out] output_length On success, the number of bytes
1944 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001945 *
1946 * \retval #PSA_SUCCESS
1947 * Success.
1948 * \retval #PSA_ERROR_BAD_STATE
1949 * The operation state is not valid (not started, IV required but
1950 * not set, or already completed).
1951 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1952 * The size of the \p output buffer is too small.
1953 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1954 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1955 * \retval #PSA_ERROR_HARDWARE_FAILURE
1956 * \retval #PSA_ERROR_TAMPERING_DETECTED
1957 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001958psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1959 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001960 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001961 unsigned char *output,
1962 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001963 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001964
Gilles Peskinedcd14942018-07-12 00:30:52 +02001965/** Finish encrypting or decrypting a message in a cipher operation.
1966 *
1967 * The application must call psa_cipher_encrypt_setup() or
1968 * psa_cipher_decrypt_setup() before calling this function. The choice
1969 * of setup function determines whether this function encrypts or
1970 * decrypts its input.
1971 *
1972 * This function finishes the encryption or decryption of the message
1973 * formed by concatenating the inputs passed to preceding calls to
1974 * psa_cipher_update().
1975 *
1976 * When this function returns, the operation becomes inactive.
1977 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001978 * \param[in,out] operation Active cipher operation.
1979 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001980 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001981 * \param[out] output_length On success, the number of bytes
1982 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001983 *
1984 * \retval #PSA_SUCCESS
1985 * Success.
1986 * \retval #PSA_ERROR_BAD_STATE
1987 * The operation state is not valid (not started, IV required but
1988 * not set, or already completed).
1989 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1990 * The size of the \p output buffer is too small.
1991 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1992 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1993 * \retval #PSA_ERROR_HARDWARE_FAILURE
1994 * \retval #PSA_ERROR_TAMPERING_DETECTED
1995 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001996psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001997 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001998 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001999 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002000
Gilles Peskinedcd14942018-07-12 00:30:52 +02002001/** Abort a cipher operation.
2002 *
2003 * This function may be called at any time after
2004 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
2005 * Aborting an operation frees all associated resources except for the
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002006 * \p operation structure itself.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002007 *
2008 * Implementation should strive to be robust and handle inactive cipher
2009 * operations safely (do nothing and return #PSA_ERROR_BAD_STATE). However,
2010 * application writers should beware that uninitialized memory may happen
2011 * to be indistinguishable from an active cipher operation, and the behavior
2012 * of psa_cipher_abort() is undefined in this case.
2013 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002014 * \param[in,out] operation Active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002015 *
2016 * \retval #PSA_SUCCESS
2017 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002018 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002019 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2020 * \retval #PSA_ERROR_HARDWARE_FAILURE
2021 * \retval #PSA_ERROR_TAMPERING_DETECTED
2022 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002023psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2024
2025/**@}*/
2026
Gilles Peskine3b555712018-03-03 21:27:57 +01002027/** \defgroup aead Authenticated encryption with associated data (AEAD)
2028 * @{
2029 */
2030
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002031/** The tag size for an AEAD algorithm, in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01002032 *
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002033 * \param alg An AEAD algorithm
2034 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002035 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002036 *
2037 * \return The tag size for the specified algorithm.
2038 * If the AEAD algorithm does not have an identified
2039 * tag that can be distinguished from the rest of
2040 * the ciphertext, return 0.
2041 * If the AEAD algorithm is not recognized, return 0.
2042 * An implementation may return either 0 or a
2043 * correct size for an AEAD algorithm that it
2044 * recognizes, but does not support.
2045 */
2046#define PSA_AEAD_TAG_SIZE(alg) \
2047 ((alg) == PSA_ALG_GCM ? 16 : \
2048 (alg) == PSA_ALG_CCM ? 16 : \
2049 0)
Gilles Peskine3b555712018-03-03 21:27:57 +01002050
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002051/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002052 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002053 * \param key Slot containing the key to use.
2054 * \param alg The AEAD algorithm to compute
2055 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002056 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002057 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002058 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002059 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002060 * but not encrypted.
2061 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002062 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002063 * encrypted.
2064 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002065 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002066 * encrypted data. The additional data is not
2067 * part of this output. For algorithms where the
2068 * encrypted data and the authentication tag
2069 * are defined as separate outputs, the
2070 * authentication tag is appended to the
2071 * encrypted data.
2072 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2073 * This must be at least
2074 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2075 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002076 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002077 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002078 *
Gilles Peskine28538492018-07-11 17:34:00 +02002079 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002080 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002081 * \retval #PSA_ERROR_EMPTY_SLOT
2082 * \retval #PSA_ERROR_NOT_PERMITTED
2083 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002084 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002085 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002086 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002087 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2088 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2089 * \retval #PSA_ERROR_HARDWARE_FAILURE
2090 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine3b555712018-03-03 21:27:57 +01002091 */
mohammad160339ee8712018-04-26 00:51:02 +03002092psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2093 psa_algorithm_t alg,
2094 const uint8_t *nonce,
2095 size_t nonce_length,
2096 const uint8_t *additional_data,
2097 size_t additional_data_length,
2098 const uint8_t *plaintext,
2099 size_t plaintext_length,
2100 uint8_t *ciphertext,
2101 size_t ciphertext_size,
2102 size_t *ciphertext_length );
Gilles Peskine3b555712018-03-03 21:27:57 +01002103
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002104/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002105 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002106 * \param key Slot containing the key to use.
2107 * \param alg The AEAD algorithm to compute
2108 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002109 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002110 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002111 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002112 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002113 * but not encrypted.
2114 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002115 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002116 * encrypted. For algorithms where the
2117 * encrypted data and the authentication tag
2118 * are defined as separate inputs, the buffer
2119 * must contain the encrypted data followed
2120 * by the authentication tag.
2121 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002122 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002123 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2124 * This must be at least
2125 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2126 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002127 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03002128 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002129 *
Gilles Peskine28538492018-07-11 17:34:00 +02002130 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002131 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002132 * \retval #PSA_ERROR_EMPTY_SLOT
2133 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002134 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002135 * \retval #PSA_ERROR_NOT_PERMITTED
2136 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002137 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002138 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002139 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002140 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2141 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2142 * \retval #PSA_ERROR_HARDWARE_FAILURE
2143 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine3b555712018-03-03 21:27:57 +01002144 */
mohammad160339ee8712018-04-26 00:51:02 +03002145psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2146 psa_algorithm_t alg,
2147 const uint8_t *nonce,
2148 size_t nonce_length,
2149 const uint8_t *additional_data,
2150 size_t additional_data_length,
2151 const uint8_t *ciphertext,
2152 size_t ciphertext_length,
2153 uint8_t *plaintext,
2154 size_t plaintext_size,
2155 size_t *plaintext_length );
Gilles Peskine3b555712018-03-03 21:27:57 +01002156
2157/**@}*/
2158
Gilles Peskine20035e32018-02-03 22:44:14 +01002159/** \defgroup asymmetric Asymmetric cryptography
2160 * @{
2161 */
2162
2163/**
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002164 * \brief ECDSA signature size for a given curve bit size
Gilles Peskine0189e752018-02-03 23:57:22 +01002165 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002166 * \param curve_bits Curve size in bits.
2167 * \return Signature size in bytes.
Gilles Peskine0189e752018-02-03 23:57:22 +01002168 *
2169 * \note This macro returns a compile-time constant if its argument is one.
Gilles Peskine0189e752018-02-03 23:57:22 +01002170 */
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002171#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
2172 (PSA_BITS_TO_BYTES(curve_bits) * 2)
Gilles Peskine0189e752018-02-03 23:57:22 +01002173
Gilles Peskine0189e752018-02-03 23:57:22 +01002174/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002175 * \brief Sign a hash or short message with a private key.
2176 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002177 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002178 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002179 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2180 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2181 * to determine the hash algorithm to use.
2182 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002183 * \param key Key slot containing an asymmetric key pair.
2184 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002185 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002186 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002187 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002188 * \param[in] salt A salt or label, if supported by the
2189 * signature algorithm.
2190 * If the signature algorithm does not support
2191 * a salt, pass \c NULL.
2192 * If the signature algorithm supports an
2193 * optional salt and you do not want to pass
2194 * a salt, pass \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002195 * \param salt_length Size of the \p salt buffer in bytes.
2196 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002197 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002198 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002199 * \param[out] signature_length On success, the number of bytes
2200 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002201 *
Gilles Peskine28538492018-07-11 17:34:00 +02002202 * \retval #PSA_SUCCESS
2203 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002204 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002205 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002206 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002207 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002208 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002209 * \retval #PSA_ERROR_NOT_SUPPORTED
2210 * \retval #PSA_ERROR_INVALID_ARGUMENT
2211 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2212 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2213 * \retval #PSA_ERROR_HARDWARE_FAILURE
2214 * \retval #PSA_ERROR_TAMPERING_DETECTED
2215 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01002216 */
2217psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
2218 psa_algorithm_t alg,
2219 const uint8_t *hash,
2220 size_t hash_length,
2221 const uint8_t *salt,
2222 size_t salt_length,
2223 uint8_t *signature,
2224 size_t signature_size,
2225 size_t *signature_length);
2226
2227/**
2228 * \brief Verify the signature a hash or short message using a public key.
2229 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002230 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002231 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002232 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2233 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2234 * to determine the hash algorithm to use.
2235 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01002236 * \param key Key slot containing a public key or an
2237 * asymmetric key pair.
2238 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002239 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002240 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002241 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002242 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002243 * \param[in] salt A salt or label, if supported by the signature
Gilles Peskine308b91d2018-02-08 09:47:44 +01002244 * algorithm.
2245 * If the signature algorithm does not support a
2246 * salt, pass \c NULL.
2247 * If the signature algorithm supports an optional
2248 * salt and you do not want to pass a salt,
2249 * pass \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002250 * \param salt_length Size of the \p salt buffer in bytes.
2251 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002252 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002253 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002254 *
Gilles Peskine28538492018-07-11 17:34:00 +02002255 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002256 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002257 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002258 * The calculation was perfomed successfully, but the passed
2259 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002260 * \retval #PSA_ERROR_NOT_SUPPORTED
2261 * \retval #PSA_ERROR_INVALID_ARGUMENT
2262 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2263 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2264 * \retval #PSA_ERROR_HARDWARE_FAILURE
2265 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01002266 */
2267psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
2268 psa_algorithm_t alg,
2269 const uint8_t *hash,
2270 size_t hash_length,
2271 const uint8_t *salt,
2272 size_t salt_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002273 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002274 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002275
Gilles Peskine723feff2018-05-31 20:08:13 +02002276#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
2277 (PSA_ALG_IS_RSA_OAEP_MGF1(alg) ? \
2278 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_GET_HASH(alg)) + 1 : \
2279 11 /*PKCS#1v1.5*/)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002280
2281/**
2282 * \brief Encrypt a short message with a public key.
2283 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002284 * \param key Key slot containing a public key or an
2285 * asymmetric key pair.
2286 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002287 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002288 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002289 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002290 * \param[in] salt A salt or label, if supported by the
2291 * encryption algorithm.
2292 * If the algorithm does not support a
2293 * salt, pass \c NULL.
2294 * If the algorithm supports an optional
2295 * salt and you do not want to pass a salt,
2296 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002297 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002298 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2299 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002300 * \param salt_length Size of the \p salt buffer in bytes.
2301 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002302 * \param[out] output Buffer where the encrypted message is to
2303 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002304 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002305 * \param[out] output_length On success, the number of bytes
2306 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002307 *
Gilles Peskine28538492018-07-11 17:34:00 +02002308 * \retval #PSA_SUCCESS
2309 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002310 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002311 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002312 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002313 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002314 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002315 * \retval #PSA_ERROR_NOT_SUPPORTED
2316 * \retval #PSA_ERROR_INVALID_ARGUMENT
2317 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2318 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2319 * \retval #PSA_ERROR_HARDWARE_FAILURE
2320 * \retval #PSA_ERROR_TAMPERING_DETECTED
2321 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002322 */
2323psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
2324 psa_algorithm_t alg,
2325 const uint8_t *input,
2326 size_t input_length,
2327 const uint8_t *salt,
2328 size_t salt_length,
2329 uint8_t *output,
2330 size_t output_size,
2331 size_t *output_length);
2332
2333/**
2334 * \brief Decrypt a short message with a private key.
2335 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002336 * \param key Key slot containing an asymmetric key pair.
2337 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002338 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002339 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002340 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002341 * \param[in] salt A salt or label, if supported by the
2342 * encryption algorithm.
2343 * If the algorithm does not support a
2344 * salt, pass \c NULL.
2345 * If the algorithm supports an optional
2346 * salt and you do not want to pass a salt,
2347 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002348 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002349 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2350 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002351 * \param salt_length Size of the \p salt buffer in bytes.
2352 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002353 * \param[out] output Buffer where the decrypted message is to
2354 * be written.
2355 * \param output_size Size of the \c output buffer in bytes.
2356 * \param[out] output_length On success, the number of bytes
2357 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002358 *
Gilles Peskine28538492018-07-11 17:34:00 +02002359 * \retval #PSA_SUCCESS
2360 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002361 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002362 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002363 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002364 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002365 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002366 * \retval #PSA_ERROR_NOT_SUPPORTED
2367 * \retval #PSA_ERROR_INVALID_ARGUMENT
2368 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2369 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2370 * \retval #PSA_ERROR_HARDWARE_FAILURE
2371 * \retval #PSA_ERROR_TAMPERING_DETECTED
2372 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2373 * \retval #PSA_ERROR_INVALID_PADDING
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002374 */
2375psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
2376 psa_algorithm_t alg,
2377 const uint8_t *input,
2378 size_t input_length,
2379 const uint8_t *salt,
2380 size_t salt_length,
2381 uint8_t *output,
2382 size_t output_size,
2383 size_t *output_length);
2384
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002385/**@}*/
2386
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002387/** \defgroup generation Key generation
2388 * @{
2389 */
2390
2391/**
2392 * \brief Generate random bytes.
2393 *
2394 * \warning This function **can** fail! Callers MUST check the return status
2395 * and MUST NOT use the content of the output buffer if the return
2396 * status is not #PSA_SUCCESS.
2397 *
2398 * \note To generate a key, use psa_generate_key() instead.
2399 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002400 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002401 * \param output_size Number of bytes to generate and output.
2402 *
Gilles Peskine28538492018-07-11 17:34:00 +02002403 * \retval #PSA_SUCCESS
2404 * \retval #PSA_ERROR_NOT_SUPPORTED
2405 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2406 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2407 * \retval #PSA_ERROR_HARDWARE_FAILURE
2408 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002409 */
2410psa_status_t psa_generate_random(uint8_t *output,
2411 size_t output_size);
2412
Gilles Peskine4c317f42018-07-12 01:24:09 +02002413/** Extra parameters for RSA key generation.
2414 *
2415 * You may pass a pointer to a structure of this type as the `extra`
2416 * parameter to psa_generate_key().
2417 */
2418typedef struct {
2419 uint32_t e; /**! Public exponent value. Default: 65537. */
2420} psa_generate_key_extra_rsa;
2421
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002422/**
2423 * \brief Generate a key or key pair.
2424 *
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002425 * \param key Slot where the key will be stored. This must be a
2426 * valid slot for a key of the chosen type. It must
2427 * be unoccupied.
2428 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2429 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002430 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002431 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002432 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002433 * default parameters. Implementation that support
2434 * the generation of vendor-specific key types
2435 * that allow extra parameters shall document
2436 * the format of these extra parameters and
2437 * the default values. For standard parameters,
2438 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002439 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002440 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2441 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002442 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002443 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2444 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002445 * - For an RSA key (\p type is
2446 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2447 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002448 * specifying the public exponent. The
2449 * default public exponent used when \p extra
2450 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002451 * \param extra_size Size of the buffer that \p extra
2452 * points to, in bytes. Note that if \p extra is
2453 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002454 *
Gilles Peskine28538492018-07-11 17:34:00 +02002455 * \retval #PSA_SUCCESS
2456 * \retval #PSA_ERROR_NOT_SUPPORTED
2457 * \retval #PSA_ERROR_INVALID_ARGUMENT
2458 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2459 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2460 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2461 * \retval #PSA_ERROR_HARDWARE_FAILURE
2462 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002463 */
2464psa_status_t psa_generate_key(psa_key_slot_t key,
2465 psa_key_type_t type,
2466 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002467 const void *extra,
2468 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002469
2470/**@}*/
2471
Gilles Peskinee59236f2018-01-27 23:32:46 +01002472#ifdef __cplusplus
2473}
2474#endif
2475
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002476/* The file "crypto_sizes.h" contains definitions for size calculation
2477 * macros whose definitions are implementation-specific. */
2478#include "crypto_sizes.h"
2479
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002480/* The file "crypto_struct.h" contains definitions for
2481 * implementation-specific structs that are declared above. */
2482#include "crypto_struct.h"
2483
2484/* The file "crypto_extra.h" contains vendor-specific definitions. This
2485 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002486#include "crypto_extra.h"
2487
2488#endif /* PSA_CRYPTO_H */