blob: 78836288cebae9bead545fd369399d6002a25838 [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 *
Gilles Peskinebe42f312018-07-13 14:38:15 +0200100 * Applications can call the \c PSA_xxx_SIZE macro listed in the function
itayzafrirc2a79762018-06-18 16:20:16 +0300101 * 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 Peskinebe42f312018-07-13 14:38:15 +0200359 * This size can be calculated with #PSA_HASH_SIZE(\c alg) where
360 * \c 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)
Gilles Peskined8008d62018-06-29 19:51:51 +0200436/** Whether a key type is an RSA key (pair or public-only). */
437#define PSA_KEY_TYPE_IS_RSA(type) \
438 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == \
439 PSA_KEY_TYPE_RSA_PUBLIC_KEY)
440/** Whether a key type is an elliptic curve key (pair or public-only). */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100441#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100442 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
443 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100444
Gilles Peskinee1fed0d2018-06-18 20:45:45 +0200445/** The type of PSA elliptic curve identifiers. */
446typedef uint16_t psa_ecc_curve_t;
447/** Extract the curve from an elliptic curve key type. */
448#define PSA_KEY_TYPE_GET_CURVE(type) \
449 ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
450 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
451 0))
452
453/* The encoding of curve identifiers is currently aligned with the
454 * TLS Supported Groups Registry (formerly known as the
455 * TLS EC Named Curve Registry)
456 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
457 * The values are defined by RFC 4492, RFC 7027 and RFC 7919. */
458#define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001)
459#define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002)
460#define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003)
461#define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004)
462#define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005)
463#define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006)
464#define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007)
465#define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008)
466#define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009)
467#define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a)
468#define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b)
469#define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c)
470#define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d)
471#define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e)
472#define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f)
473#define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010)
474#define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011)
475#define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012)
476#define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013)
477#define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014)
478#define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015)
479#define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016)
480#define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017)
481#define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018)
482#define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019)
483#define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a)
484#define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b)
485#define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c)
486#define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d)
487#define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e)
488#define PSA_ECC_CURVE_FFDHE_2048 ((psa_ecc_curve_t) 0x0100)
489#define PSA_ECC_CURVE_FFDHE_3072 ((psa_ecc_curve_t) 0x0101)
490#define PSA_ECC_CURVE_FFDHE_4096 ((psa_ecc_curve_t) 0x0102)
491#define PSA_ECC_CURVE_FFDHE_6144 ((psa_ecc_curve_t) 0x0103)
492#define PSA_ECC_CURVE_FFDHE_8192 ((psa_ecc_curve_t) 0x0104)
493
Gilles Peskine7e198532018-03-08 07:50:30 +0100494/** The block size of a block cipher.
495 *
496 * \param type A cipher key type (value of type #psa_key_type_t).
497 *
498 * \return The block size for a block cipher, or 1 for a stream cipher.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200499 * The return value is undefined if \p type is not a supported
Gilles Peskine35855962018-04-19 08:39:16 +0200500 * cipher key type.
501 *
502 * \note It is possible to build stream cipher algorithms on top of a block
503 * cipher, for example CTR mode (#PSA_ALG_CTR).
504 * This macro only takes the key type into account, so it cannot be
505 * used to determine the size of the data that #psa_cipher_update()
506 * might buffer for future processing in general.
Gilles Peskine7e198532018-03-08 07:50:30 +0100507 *
508 * \note This macro returns a compile-time constant if its argument is one.
509 *
510 * \warning This macro may evaluate its argument multiple times.
511 */
Gilles Peskine03182e92018-03-07 16:40:52 +0100512#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100513 ( \
514 (type) == PSA_KEY_TYPE_AES ? 16 : \
515 (type) == PSA_KEY_TYPE_DES ? 8 : \
516 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
Gilles Peskine7e198532018-03-08 07:50:30 +0100517 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100518 0)
519
Gilles Peskine308b91d2018-02-08 09:47:44 +0100520/** \brief Encoding of a cryptographic algorithm.
521 *
522 * For algorithms that can be applied to multiple key types, this type
523 * does not encode the key type. For example, for symmetric ciphers
524 * based on a block cipher, #psa_algorithm_t encodes the block cipher
525 * mode and the padding mode while the block cipher itself is encoded
526 * via #psa_key_type_t.
527 */
Gilles Peskine20035e32018-02-03 22:44:14 +0100528typedef uint32_t psa_algorithm_t;
529
Gilles Peskine98f0a242018-02-06 18:57:29 +0100530#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
531#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
532#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
533#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
534#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
535#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
536#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
537#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
538#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
539#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100540
Gilles Peskine98f0a242018-02-06 18:57:29 +0100541#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
542 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200543
Gilles Peskine308b91d2018-02-08 09:47:44 +0100544/** Whether the specified algorithm is a hash algorithm.
545 *
Gilles Peskine7e198532018-03-08 07:50:30 +0100546 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100547 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200548 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
549 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskine7e198532018-03-08 07:50:30 +0100550 * algorithm identifier.
551 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100552#define PSA_ALG_IS_HASH(alg) \
553 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200554
555/** Whether the specified algorithm is a MAC algorithm.
556 *
557 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
558 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200559 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
560 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200561 * algorithm identifier.
562 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100563#define PSA_ALG_IS_MAC(alg) \
564 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200565
566/** Whether the specified algorithm is a symmetric cipher algorithm.
567 *
568 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
569 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200570 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
571 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200572 * algorithm identifier.
573 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100574#define PSA_ALG_IS_CIPHER(alg) \
575 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200576
577/** Whether the specified algorithm is an authenticated encryption
578 * with associated data (AEAD) algorithm.
579 *
580 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
581 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200582 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
583 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200584 * algorithm identifier.
585 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100586#define PSA_ALG_IS_AEAD(alg) \
587 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200588
589/** Whether the specified algorithm is a public-key signature algorithm.
590 *
591 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
592 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200593 * \return 1 if \p alg is a public-key signature algorithm, 0 otherwise.
594 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200595 * algorithm identifier.
596 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100597#define PSA_ALG_IS_SIGN(alg) \
598 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200599
600/** Whether the specified algorithm is a public-key encryption algorithm.
601 *
602 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
603 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200604 * \return 1 if \p alg is a public-key encryption algorithm, 0 otherwise.
605 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200606 * algorithm identifier.
607 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100608#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
609 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200610
611/** Whether the specified algorithm is a key agreement algorithm.
612 *
613 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
614 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200615 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
616 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200617 * algorithm identifier.
618 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100619#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
620 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200621
622/** Whether the specified algorithm is a key derivation algorithm.
623 *
624 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
625 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200626 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
627 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200628 * algorithm identifier.
629 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100630#define PSA_ALG_IS_KEY_DERIVATION(alg) \
631 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
632
633#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
634#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
635#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
636#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
Gilles Peskinee3f694f2018-03-08 07:48:40 +0100637#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
638#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100639#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
640#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
641#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
642#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
643#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
644#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
645#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
646#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
647#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
648#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
649
Gilles Peskine8c9def32018-02-08 10:02:12 +0100650#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100651#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
Gilles Peskine35855962018-04-19 08:39:16 +0200652/** Macro to build an HMAC algorithm.
653 *
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200654 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
Gilles Peskine35855962018-04-19 08:39:16 +0200655 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200656 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200657 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine35855962018-04-19 08:39:16 +0200658 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200659 * \return The corresponding HMAC algorithm.
660 * \return Unspecified if \p alg is not a supported
661 * hash algorithm.
Gilles Peskine35855962018-04-19 08:39:16 +0200662 */
663#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100664 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200665
Gilles Peskine8c9def32018-02-08 10:02:12 +0100666#define PSA_ALG_HMAC_HASH(hmac_alg) \
667 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200668
669/** Whether the specified algorithm is an HMAC algorithm.
670 *
671 * HMAC is a family of MAC algorithms that are based on a hash function.
672 *
673 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
674 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200675 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
676 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200677 * algorithm identifier.
678 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100679#define PSA_ALG_IS_HMAC(alg) \
680 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
681 PSA_ALG_HMAC_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200682
Gilles Peskine8c9def32018-02-08 10:02:12 +0100683#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
684#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
685#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
686#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200687
688/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
689 *
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200690 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
691 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200692 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
693 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200694 * algorithm identifier.
695 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100696#define PSA_ALG_IS_CIPHER_MAC(alg) \
697 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
698 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100699
Gilles Peskine8c9def32018-02-08 10:02:12 +0100700#define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100701#define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100702#define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100703#define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200704
705/** Use a block cipher mode without padding.
706 *
707 * This padding mode may only be used with messages whose lengths are a
708 * whole number of blocks for the chosen block cipher.
709 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100710#define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200711
Gilles Peskine98f0a242018-02-06 18:57:29 +0100712#define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200713
714/** Whether the specified algorithm is a block cipher.
715 *
716 * A block cipher is a symmetric cipher that encrypts or decrypts messages
717 * by chopping them into fixed-size blocks. Processing a message requires
718 * applying a _padding mode_ to transform the message into one whose
719 * length is a whole number of blocks. To construct an algorithm
720 * identifier for a block cipher, apply a bitwise-or between the block
721 * cipher mode and the padding mode. For example, CBC with PKCS#7 padding
722 * is `PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7`.
723 *
724 * The transformation applied to each block is determined by the key type.
725 * For example, to use AES-128-CBC-PKCS7, use the algorithm above with
726 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
727 *
728 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
729 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200730 * \return 1 if \p alg is a block cipher algorithm, 0 otherwise.
731 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200732 * algorithm identifier or if it is not a symmetric cipher algorithm.
733 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100734#define PSA_ALG_IS_BLOCK_CIPHER(alg) \
735 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
736 PSA_ALG_BLOCK_CIPHER_BASE)
737
Gilles Peskinedcd14942018-07-12 00:30:52 +0200738/** The CBC block cipher mode.
739 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100740#define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100741#define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002)
742#define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003)
743#define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004)
Gilles Peskine5d1888e2018-07-12 00:32:42 +0200744
745#define PSA_ALG_STREAM_CIPHER_BASE ((psa_algorithm_t)0x04800000)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200746
Gilles Peskinedcd14942018-07-12 00:30:52 +0200747/** The CTR stream cipher mode.
748 *
749 * CTR is a stream cipher which is built from a block cipher. The
750 * underlying block cipher is determined by the key type. For example,
751 * to use AES-128-CTR, use this algorithm with
752 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
753 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100754#define PSA_ALG_CTR ((psa_algorithm_t)0x04800001)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200755
Gilles Peskinedcd14942018-07-12 00:30:52 +0200756/** The ARC4 stream cipher algorithm.
757 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100758#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100759
Gilles Peskinedcd14942018-07-12 00:30:52 +0200760/** Whether the specified algorithm is a stream cipher.
761 *
762 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
763 * by applying a bitwise-xor with a stream of bytes that is generated
764 * from a key.
765 *
766 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
767 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200768 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
769 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200770 * algorithm identifier or if it is not a symmetric cipher algorithm.
771 */
Moran Pekerbed71a22018-04-22 20:19:20 +0300772#define PSA_ALG_IS_STREAM_CIPHER(alg) \
773 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
Gilles Peskine5d1888e2018-07-12 00:32:42 +0200774 PSA_ALG_STREAM_CIPHER_BASE)
Moran Pekerbed71a22018-04-22 20:19:20 +0300775
Gilles Peskine8c9def32018-02-08 10:02:12 +0100776#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
777#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100778
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200779#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
780/** RSA PKCS#1 v1.5 signature with hashing.
781 *
782 * This is the signature scheme defined by RFC 8017
783 * (PKCS#1: RSA Cryptography Specifications) under the name
784 * RSASSA-PKCS1-v1_5.
785 *
786 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200787 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200788 *
789 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
790 * \return Unspecified if \p alg is not a supported
791 * hash algorithm.
792 */
Gilles Peskinea5926232018-03-28 14:16:50 +0200793#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200794 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
795/** Raw PKCS#1 v1.5 signature.
796 *
797 * The input to this algorithm is the DigestInfo structure used by
798 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
799 * steps 3&ndash;6.
800 */
801#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
Gilles Peskinea5926232018-03-28 14:16:50 +0200802#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200803 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200804
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200805#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
806/** RSA PSS signature with hashing.
807 *
808 * This is the signature scheme defined by RFC 8017
809 * (PKCS#1: RSA Cryptography Specifications) under the name
Gilles Peskinea4d20bd2018-06-29 23:35:02 +0200810 * RSASSA-PSS, with the message generation function MGF1, and with
811 * a salt length equal to the length of the hash. The specified
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200812 * hash algorithm is used to hash the input message, to create the
813 * salted hash, and for the mask generation.
814 *
815 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200816 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200817 *
818 * \return The corresponding RSA PSS signature algorithm.
819 * \return Unspecified if \p alg is not a supported
820 * hash algorithm.
821 */
822#define PSA_ALG_RSA_PSS(hash_alg) \
823 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
824#define PSA_ALG_IS_RSA_PSS(alg) \
825 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
826
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200827#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
828/** DSA signature with hashing.
829 *
830 * This is the signature scheme defined by FIPS 186-4,
831 * with a random per-message secret number (*k*).
832 *
833 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200834 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200835 *
836 * \return The corresponding DSA signature algorithm.
837 * \return Unspecified if \p alg is not a supported
838 * hash algorithm.
839 */
840#define PSA_ALG_DSA(hash_alg) \
841 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
842#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
843#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
844#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
845 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
846#define PSA_ALG_IS_DSA(alg) \
847 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
848 PSA_ALG_DSA_BASE)
849#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
850 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
851
852#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
853/** ECDSA signature with hashing.
854 *
855 * This is the ECDSA signature scheme defined by ANSI X9.62,
856 * with a random per-message secret number (*k*).
857 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200858 * The representation of the signature as a byte string consists of
859 * the concatentation of the signature values *r* and *s*. Each of
860 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
861 * of the base point of the curve in octets. Each value is represented
862 * in big-endian order (most significant octet first).
863 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200864 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200865 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200866 *
867 * \return The corresponding ECDSA signature algorithm.
868 * \return Unspecified if \p alg is not a supported
869 * hash algorithm.
870 */
871#define PSA_ALG_ECDSA(hash_alg) \
872 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
873/** ECDSA signature without hashing.
874 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200875 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200876 * without specifying a hash algorithm. This algorithm may only be
877 * used to sign or verify a sequence of bytes that should be an
878 * already-calculated hash. Note that the input is padded with
879 * zeros on the left or truncated on the left as required to fit
880 * the curve size.
881 */
882#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
883#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
884/** Deterministic ECDSA signature with hashing.
885 *
886 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
887 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200888 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
889 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200890 * Note that when this algorithm is used for verification, signatures
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200891 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200892 * same private key are accepted. In other words,
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200893 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
894 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200895 *
896 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200897 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200898 *
899 * \return The corresponding deterministic ECDSA signature
900 * algorithm.
901 * \return Unspecified if \p alg is not a supported
902 * hash algorithm.
903 */
904#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
905 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
906#define PSA_ALG_IS_ECDSA(alg) \
907 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
908 PSA_ALG_ECDSA_BASE)
909#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
910 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
911
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200912/** Get the hash used by a hash-and-sign signature algorithm.
913 *
914 * A hash-and-sign algorithm is a signature algorithm which is
915 * composed of two phases: first a hashing phase which does not use
916 * the key and produces a hash of the input message, then a signing
917 * phase which only uses the hash and the key and not the message
918 * itself.
919 *
920 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200921 * #PSA_ALG_IS_SIGN(\p alg) is true).
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200922 *
923 * \return The underlying hash algorithm if \p alg is a hash-and-sign
924 * algorithm.
925 * \return 0 if \p alg is a signature algorithm that does not
926 * follow the hash-and-sign structure.
927 * \return Unspecified if \p alg is not a signature algorithm or
928 * if it is not supported by the implementation.
929 */
930#define PSA_ALG_SIGN_GET_HASH(alg) \
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200931 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
932 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg) ? \
Gilles Peskine54622ae2018-06-29 22:24:24 +0200933 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200934 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
935 0)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100936
Gilles Peskinedcd14942018-07-12 00:30:52 +0200937/** RSA PKCS#1 v1.5 encryption.
938 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200939#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200940
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200941#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200942/** RSA OAEP encryption.
943 *
944 * This is the encryption scheme defined by RFC 8017
945 * (PKCS#1: RSA Cryptography Specifications) under the name
946 * RSAES-OAEP, with the message generation function MGF1.
947 *
948 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
949 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
950 * for MGF1.
951 *
952 * \return The corresponding RSA OAEP signature algorithm.
953 * \return Unspecified if \p alg is not a supported
954 * hash algorithm.
955 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200956#define PSA_ALG_RSA_OAEP(hash_alg) \
957 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
958#define PSA_ALG_IS_RSA_OAEP(alg) \
959 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
Gilles Peskine072ac562018-06-30 00:21:29 +0200960#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
961 (PSA_ALG_IS_RSA_OAEP(alg) ? \
962 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
963 0)
Gilles Peskined1e8e412018-06-07 09:49:39 +0200964
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100965/**@}*/
966
967/** \defgroup key_management Key management
968 * @{
969 */
970
971/**
972 * \brief Import a key in binary format.
973 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100974 * This function supports any output from psa_export_key(). Refer to the
975 * documentation of psa_export_key() for the format for each key type.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100976 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100977 * \param key Slot where the key will be stored. This must be a
978 * valid slot for a key of the chosen type. It must
979 * be unoccupied.
980 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskineedd11a12018-07-12 01:08:58 +0200981 * \param[in] data Buffer containing the key data.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200982 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100983 *
Gilles Peskine28538492018-07-11 17:34:00 +0200984 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100985 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200986 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200987 * The key type or key size is not supported, either by the
988 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200989 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +0100990 * The key slot is invalid,
991 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200992 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +0200993 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200994 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
995 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
996 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
997 * \retval #PSA_ERROR_HARDWARE_FAILURE
998 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100999 */
1000psa_status_t psa_import_key(psa_key_slot_t key,
1001 psa_key_type_t type,
1002 const uint8_t *data,
1003 size_t data_length);
1004
1005/**
Gilles Peskine154bd952018-04-19 08:38:16 +02001006 * \brief Destroy a key and restore the slot to its default state.
1007 *
1008 * This function destroys the content of the key slot from both volatile
1009 * memory and, if applicable, non-volatile storage. Implementations shall
1010 * make a best effort to ensure that any previous content of the slot is
1011 * unrecoverable.
1012 *
1013 * This function also erases any metadata such as policies. It returns the
1014 * specified slot to its default state.
1015 *
1016 * \param key The key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017 *
Gilles Peskine28538492018-07-11 17:34:00 +02001018 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +02001019 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +02001020 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001021 * The slot holds content and cannot be erased because it is
1022 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskine28538492018-07-11 17:34:00 +02001023 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001024 * The specified slot number does not designate a valid slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001025 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001026 * There was an failure in communication with the cryptoprocessor.
1027 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +02001028 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001029 * The storage is corrupted. Implementations shall make a best effort
1030 * to erase key material even in this stage, however applications
1031 * should be aware that it may be impossible to guarantee that the
1032 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +02001033 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001034 * An unexpected condition which is not a storage corruption or
1035 * a communication failure occurred. The cryptoprocessor may have
1036 * been compromised.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001037 */
1038psa_status_t psa_destroy_key(psa_key_slot_t key);
1039
1040/**
1041 * \brief Get basic metadata about a key.
1042 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001043 * \param key Slot whose content is queried. This must
1044 * be an occupied key slot.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001045 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001046 * This may be a null pointer, in which case the key type
1047 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001048 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +01001049 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +01001050 * is not written.
1051 *
Gilles Peskine28538492018-07-11 17:34:00 +02001052 * \retval #PSA_SUCCESS
1053 * \retval #PSA_ERROR_EMPTY_SLOT
1054 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1055 * \retval #PSA_ERROR_HARDWARE_FAILURE
1056 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001057 */
1058psa_status_t psa_get_key_information(psa_key_slot_t key,
1059 psa_key_type_t *type,
1060 size_t *bits);
1061
1062/**
1063 * \brief Export a key in binary format.
1064 *
1065 * The output of this function can be passed to psa_import_key() to
1066 * create an equivalent object.
1067 *
1068 * If a key is created with psa_import_key() and then exported with
1069 * this function, it is not guaranteed that the resulting data is
1070 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +01001071 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001072 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001073 * For standard key types, the output format is as follows:
1074 *
1075 * - For symmetric keys (including MAC keys), the format is the
1076 * raw bytes of the key.
1077 * - For DES, the key data consists of 8 bytes. The parity bits must be
1078 * correct.
1079 * - For Triple-DES, the format is the concatenation of the
1080 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +01001081 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine2743e422018-06-27 22:57:11 +02001082 * is the non-encrypted DER representation defined by PKCS\#1 (RFC 8017)
1083 * as RSAPrivateKey.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001084 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
Gilles Peskine971f7062018-03-20 17:52:58 +01001085 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001086 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001087 * \param key Slot whose content is to be exported. This must
1088 * be an occupied key slot.
1089 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001090 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001091 * \param[out] data_length On success, the number of bytes
1092 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001093 *
Gilles Peskine28538492018-07-11 17:34:00 +02001094 * \retval #PSA_SUCCESS
1095 * \retval #PSA_ERROR_EMPTY_SLOT
1096 * \retval #PSA_ERROR_NOT_PERMITTED
1097 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1098 * \retval #PSA_ERROR_HARDWARE_FAILURE
1099 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001100 */
1101psa_status_t psa_export_key(psa_key_slot_t key,
1102 uint8_t *data,
1103 size_t data_size,
1104 size_t *data_length);
1105
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001106/**
1107 * \brief Export a public key or the public part of a key pair in binary format.
1108 *
1109 * The output of this function can be passed to psa_import_key() to
1110 * create an object that is equivalent to the public key.
1111 *
1112 * For standard key types, the output format is as follows:
1113 *
1114 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
Moran Pekerdd4ea382018-04-03 15:30:03 +03001115 * the format is the DER representation of the public key defined by RFC 5280
Gilles Peskine971f7062018-03-20 17:52:58 +01001116 * as SubjectPublicKeyInfo.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001117 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001118 * \param key Slot whose content is to be exported. This must
1119 * be an occupied key slot.
1120 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001121 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001122 * \param[out] data_length On success, the number of bytes
1123 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001124 *
Gilles Peskine28538492018-07-11 17:34:00 +02001125 * \retval #PSA_SUCCESS
1126 * \retval #PSA_ERROR_EMPTY_SLOT
1127 * \retval #PSA_ERROR_INVALID_ARGUMENT
1128 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1129 * \retval #PSA_ERROR_HARDWARE_FAILURE
1130 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001131 */
1132psa_status_t psa_export_public_key(psa_key_slot_t key,
1133 uint8_t *data,
1134 size_t data_size,
1135 size_t *data_length);
1136
1137/**@}*/
1138
1139/** \defgroup policy Key policies
1140 * @{
1141 */
1142
1143/** \brief Encoding of permitted usage on a key. */
1144typedef uint32_t psa_key_usage_t;
1145
Gilles Peskine7e198532018-03-08 07:50:30 +01001146/** Whether the key may be exported.
1147 *
1148 * A public key or the public part of a key pair may always be exported
1149 * regardless of the value of this permission flag.
1150 *
1151 * If a key does not have export permission, implementations shall not
1152 * allow the key to be exported in plain form from the cryptoprocessor,
1153 * whether through psa_export_key() or through a proprietary interface.
1154 * The key may however be exportable in a wrapped form, i.e. in a form
1155 * where it is encrypted by another key.
1156 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001157#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1158
Gilles Peskine7e198532018-03-08 07:50:30 +01001159/** Whether the key may be used to encrypt a message.
1160 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001161 * This flag allows the key to be used for a symmetric encryption operation,
1162 * for an AEAD encryption-and-authentication operation,
1163 * or for an asymmetric encryption operation,
1164 * if otherwise permitted by the key's type and policy.
1165 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001166 * For a key pair, this concerns the public key.
1167 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001168#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +01001169
1170/** Whether the key may be used to decrypt a message.
1171 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001172 * This flag allows the key to be used for a symmetric decryption operation,
1173 * for an AEAD decryption-and-verification operation,
1174 * or for an asymmetric decryption operation,
1175 * if otherwise permitted by the key's type and policy.
1176 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001177 * For a key pair, this concerns the private key.
1178 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001179#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +01001180
1181/** Whether the key may be used to sign a message.
1182 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001183 * This flag allows the key to be used for a MAC calculation operation
1184 * or for an asymmetric signature operation,
1185 * if otherwise permitted by the key's type and policy.
1186 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001187 * For a key pair, this concerns the private key.
1188 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001189#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +01001190
1191/** Whether the key may be used to verify a message signature.
1192 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001193 * This flag allows the key to be used for a MAC verification operation
1194 * or for an asymmetric signature verification operation,
1195 * if otherwise permitted by by the key's type and policy.
1196 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001197 * For a key pair, this concerns the public key.
1198 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001199#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
1200
1201/** The type of the key policy data structure.
1202 *
1203 * This is an implementation-defined \c struct. Applications should not
1204 * make any assumptions about the content of this structure except
1205 * as directed by the documentation of a specific implementation. */
1206typedef struct psa_key_policy_s psa_key_policy_t;
1207
1208/** \brief Initialize a key policy structure to a default that forbids all
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001209 * usage of the key.
1210 *
1211 * \param[out] policy The policy object to initialize.
1212 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001213void psa_key_policy_init(psa_key_policy_t *policy);
1214
Gilles Peskine7e198532018-03-08 07:50:30 +01001215/** \brief Set the standard fields of a policy structure.
1216 *
1217 * Note that this function does not make any consistency check of the
1218 * parameters. The values are only checked when applying the policy to
1219 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001220 *
1221 * \param[out] policy The policy object to modify.
1222 * \param usage The permitted uses for the key.
1223 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +01001224 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001225void psa_key_policy_set_usage(psa_key_policy_t *policy,
1226 psa_key_usage_t usage,
1227 psa_algorithm_t alg);
1228
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001229/** \brief Retrieve the usage field of a policy structure.
1230 *
1231 * \param[in] policy The policy object to query.
1232 *
1233 * \return The permitted uses for a key with this policy.
1234 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001235psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001236
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001237/** \brief Retrieve the algorithm field of a policy structure.
1238 *
1239 * \param[in] policy The policy object to query.
1240 *
1241 * \return The permitted algorithm for a key with this policy.
1242 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001243psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001244
1245/** \brief Set the usage policy on a key slot.
1246 *
1247 * This function must be called on an empty key slot, before importing,
1248 * generating or creating a key in the slot. Changing the policy of an
1249 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +01001250 *
1251 * Implementations may set restrictions on supported key policies
1252 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001253 *
1254 * \param key The key slot whose policy is to be changed.
1255 * \param[in] policy The policy object to query.
1256 *
1257 * \retval #PSA_SUCCESS
1258 * \retval #PSA_ERROR_OCCUPIED_SLOT
1259 * \retval #PSA_ERROR_NOT_SUPPORTED
1260 * \retval #PSA_ERROR_INVALID_ARGUMENT
1261 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1262 * \retval #PSA_ERROR_HARDWARE_FAILURE
1263 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001264 */
1265psa_status_t psa_set_key_policy(psa_key_slot_t key,
1266 const psa_key_policy_t *policy);
1267
Gilles Peskine7e198532018-03-08 07:50:30 +01001268/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001269 *
1270 * \param key The key slot whose policy is being queried.
1271 * \param[out] policy On success, the key's policy.
1272 *
1273 * \retval #PSA_SUCCESS
1274 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1275 * \retval #PSA_ERROR_HARDWARE_FAILURE
1276 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7e198532018-03-08 07:50:30 +01001277 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001278psa_status_t psa_get_key_policy(psa_key_slot_t key,
1279 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +01001280
1281/**@}*/
1282
Gilles Peskine609b6a52018-03-03 21:31:50 +01001283/** \defgroup persistence Key lifetime
1284 * @{
1285 */
1286
1287/** Encoding of key lifetimes.
1288 */
1289typedef uint32_t psa_key_lifetime_t;
1290
1291/** A volatile key slot retains its content as long as the application is
1292 * running. It is guaranteed to be erased on a power reset.
1293 */
1294#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1295
1296/** A persistent key slot retains its content as long as it is not explicitly
1297 * destroyed.
1298 */
1299#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1300
1301/** A write-once key slot may not be modified once a key has been set.
1302 * It will retain its content as long as the device remains operational.
1303 */
1304#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
1305
Gilles Peskined393e182018-03-08 07:49:16 +01001306/** \brief Retrieve the lifetime of a key slot.
1307 *
1308 * The assignment of lifetimes to slots is implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001309 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001310 * \param key Slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001311 * \param[out] lifetime On success, the lifetime value.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001312 *
Gilles Peskine28538492018-07-11 17:34:00 +02001313 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001314 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001315 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603a7d245a2018-04-17 00:40:08 -07001316 * The key slot is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001317 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1318 * \retval #PSA_ERROR_HARDWARE_FAILURE
1319 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +01001320 */
Gilles Peskine609b6a52018-03-03 21:31:50 +01001321psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
1322 psa_key_lifetime_t *lifetime);
1323
Gilles Peskined393e182018-03-08 07:49:16 +01001324/** \brief Change the lifetime of a key slot.
1325 *
1326 * Whether the lifetime of a key slot can be changed at all, and if so
Gilles Peskine19067982018-03-20 17:54:53 +01001327 * whether the lifetime of an occupied key slot can be changed, is
Gilles Peskined393e182018-03-08 07:49:16 +01001328 * implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001329 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001330 * \param key Slot whose lifetime is to be changed.
1331 * \param lifetime The lifetime value to set for the given key slot.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001332 *
Gilles Peskine28538492018-07-11 17:34:00 +02001333 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001334 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001335 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603804cd712018-03-20 22:44:08 +02001336 * The key slot is invalid,
mohammad1603a7d245a2018-04-17 00:40:08 -07001337 * or the lifetime value is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001338 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001339 * The implementation does not support the specified lifetime value,
1340 * at least for the specified key slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001341 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001342 * The slot contains a key, and the implementation does not support
1343 * changing the lifetime of an occupied slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001344 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1345 * \retval #PSA_ERROR_HARDWARE_FAILURE
1346 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +01001347 */
1348psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
mohammad1603ea050092018-04-17 00:31:34 -07001349 psa_key_lifetime_t lifetime);
Gilles Peskined393e182018-03-08 07:49:16 +01001350
Gilles Peskine609b6a52018-03-03 21:31:50 +01001351/**@}*/
1352
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001353/** \defgroup hash Message digests
1354 * @{
1355 */
1356
Gilles Peskine308b91d2018-02-08 09:47:44 +01001357/** The type of the state data structure for multipart hash operations.
1358 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001359 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01001360 * make any assumptions about the content of this structure except
1361 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001362typedef struct psa_hash_operation_s psa_hash_operation_t;
1363
Gilles Peskine308b91d2018-02-08 09:47:44 +01001364/** The size of the output of psa_hash_finish(), in bytes.
1365 *
1366 * This is also the hash size that psa_hash_verify() expects.
1367 *
1368 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001369 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
Gilles Peskinebe42f312018-07-13 14:38:15 +02001370 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
Gilles Peskine35855962018-04-19 08:39:16 +02001371 * hash algorithm).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001372 *
1373 * \return The hash size for the specified hash algorithm.
1374 * If the hash algorithm is not recognized, return 0.
1375 * An implementation may return either 0 or the correct size
1376 * for a hash algorithm that it recognizes, but does not support.
1377 */
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001378#define PSA_HASH_SIZE(alg) \
1379 ( \
1380 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD2 ? 16 : \
1381 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD4 ? 16 : \
1382 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD5 ? 16 : \
1383 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
1384 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
1385 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
1386 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
1387 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
1388 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
1389 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
1390 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
1391 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
1392 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
1393 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
1394 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001395 0)
1396
Gilles Peskine308b91d2018-02-08 09:47:44 +01001397/** Start a multipart hash operation.
1398 *
1399 * The sequence of operations to calculate a hash (message digest)
1400 * is as follows:
1401 * -# Allocate an operation object which will be passed to all the functions
1402 * listed here.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001403 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001404 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001405 * of the message each time. The hash that is calculated is the hash
1406 * of the concatenation of these messages in order.
1407 * -# To calculate the hash, call psa_hash_finish().
1408 * To compare the hash with an expected value, call psa_hash_verify().
1409 *
1410 * The application may call psa_hash_abort() at any time after the operation
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001411 * has been initialized with psa_hash_setup().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001412 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001413 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001414 * eventually terminate the operation. The following events terminate an
1415 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001416 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001417 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001418 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001419 * \param[out] operation The operation object to use.
1420 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1421 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001422 *
Gilles Peskine28538492018-07-11 17:34:00 +02001423 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001424 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001425 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001426 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001427 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1428 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1429 * \retval #PSA_ERROR_HARDWARE_FAILURE
1430 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001431 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001432psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001433 psa_algorithm_t alg);
1434
Gilles Peskine308b91d2018-02-08 09:47:44 +01001435/** Add a message fragment to a multipart hash operation.
1436 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001437 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001438 *
1439 * If this function returns an error status, the operation becomes inactive.
1440 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001441 * \param[in,out] operation Active hash operation.
1442 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001443 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001444 *
Gilles Peskine28538492018-07-11 17:34:00 +02001445 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001446 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001447 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001448 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001449 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1450 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1451 * \retval #PSA_ERROR_HARDWARE_FAILURE
1452 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001453 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001454psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1455 const uint8_t *input,
1456 size_t input_length);
1457
Gilles Peskine308b91d2018-02-08 09:47:44 +01001458/** Finish the calculation of the hash of a message.
1459 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001460 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001461 * This function calculates the hash of the message formed by concatenating
1462 * the inputs passed to preceding calls to psa_hash_update().
1463 *
1464 * When this function returns, the operation becomes inactive.
1465 *
1466 * \warning Applications should not call this function if they expect
1467 * a specific value for the hash. Call psa_hash_verify() instead.
1468 * Beware that comparing integrity or authenticity data such as
1469 * hash values with a function such as \c memcmp is risky
1470 * because the time taken by the comparison may leak information
1471 * about the hashed data which could allow an attacker to guess
1472 * a valid hash and thereby bypass security controls.
1473 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001474 * \param[in,out] operation Active hash operation.
1475 * \param[out] hash Buffer where the hash is to be written.
1476 * \param hash_size Size of the \p hash buffer in bytes.
1477 * \param[out] hash_length On success, the number of bytes
1478 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001479 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001480 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001481 *
Gilles Peskine28538492018-07-11 17:34:00 +02001482 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001483 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001484 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001485 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001486 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001487 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001488 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001489 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001490 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1491 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1492 * \retval #PSA_ERROR_HARDWARE_FAILURE
1493 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001494 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001495psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1496 uint8_t *hash,
1497 size_t hash_size,
1498 size_t *hash_length);
1499
Gilles Peskine308b91d2018-02-08 09:47:44 +01001500/** Finish the calculation of the hash of a message and compare it with
1501 * an expected value.
1502 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001503 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001504 * This function calculates the hash of the message formed by concatenating
1505 * the inputs passed to preceding calls to psa_hash_update(). It then
1506 * compares the calculated hash with the expected hash passed as a
1507 * parameter to this function.
1508 *
1509 * When this function returns, the operation becomes inactive.
1510 *
Gilles Peskine19067982018-03-20 17:54:53 +01001511 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001512 * comparison between the actual hash and the expected hash is performed
1513 * in constant time.
1514 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001515 * \param[in,out] operation Active hash operation.
1516 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001517 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001518 *
Gilles Peskine28538492018-07-11 17:34:00 +02001519 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001520 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001521 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001522 * The hash of the message was calculated successfully, but it
1523 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001524 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001525 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001526 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1527 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1528 * \retval #PSA_ERROR_HARDWARE_FAILURE
1529 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001530 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001531psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1532 const uint8_t *hash,
1533 size_t hash_length);
1534
Gilles Peskine308b91d2018-02-08 09:47:44 +01001535/** Abort a hash operation.
1536 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001537 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001538 * \p operation structure itself. Once aborted, the operation object
1539 * can be reused for another operation by calling
1540 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001541 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001542 * You may call this function any time after the operation object has
1543 * been initialized by any of the following methods:
1544 * - A call to psa_hash_setup(), whether it succeeds or not.
1545 * - Initializing the \c struct to all-bits-zero.
1546 * - Initializing the \c struct to logical zeros, e.g.
1547 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001548 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001549 * In particular, calling psa_hash_abort() after the operation has been
1550 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1551 * psa_hash_verify() is safe and has no effect.
1552 *
1553 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001554 *
Gilles Peskine28538492018-07-11 17:34:00 +02001555 * \retval #PSA_SUCCESS
1556 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001557 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001558 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1559 * \retval #PSA_ERROR_HARDWARE_FAILURE
1560 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001561 */
1562psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001563
1564/**@}*/
1565
Gilles Peskine8c9def32018-02-08 10:02:12 +01001566/** \defgroup MAC Message authentication codes
1567 * @{
1568 */
1569
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001570/** The type of the state data structure for multipart MAC operations.
1571 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001572 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001573 * make any assumptions about the content of this structure except
1574 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001575typedef struct psa_mac_operation_s psa_mac_operation_t;
1576
Gilles Peskine89167cb2018-07-08 20:12:23 +02001577/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001578 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001579 * This function sets up the calculation of the MAC
1580 * (message authentication code) of a byte string.
1581 * To verify the MAC of a message against an
1582 * expected value, use psa_mac_verify_setup() instead.
1583 *
1584 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001585 * -# Allocate an operation object which will be passed to all the functions
1586 * listed here.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001587 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001588 * The key remains associated with the operation even if the content
1589 * of the key slot changes.
1590 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1591 * of the message each time. The MAC that is calculated is the MAC
1592 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001593 * -# At the end of the message, call psa_mac_sign_finish() to finish
1594 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001595 *
1596 * The application may call psa_mac_abort() at any time after the operation
Gilles Peskine89167cb2018-07-08 20:12:23 +02001597 * has been initialized with psa_mac_sign_setup().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001598 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001599 * After a successful call to psa_mac_sign_setup(), the application must
1600 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001601 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001602 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001603 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001604 * \param[out] operation The operation object to use.
1605 * \param key Slot containing the key to use for the operation.
1606 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1607 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001608 *
Gilles Peskine28538492018-07-11 17:34:00 +02001609 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001610 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001611 * \retval #PSA_ERROR_EMPTY_SLOT
1612 * \retval #PSA_ERROR_NOT_PERMITTED
1613 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001614 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001615 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001616 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001617 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1618 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1619 * \retval #PSA_ERROR_HARDWARE_FAILURE
1620 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001621 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001622psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
1623 psa_key_slot_t key,
1624 psa_algorithm_t alg);
1625
1626/** Start a multipart MAC verification operation.
1627 *
1628 * This function sets up the verification of the MAC
1629 * (message authentication code) of a byte string against an expected value.
1630 *
1631 * The sequence of operations to verify a MAC is as follows:
1632 * -# Allocate an operation object which will be passed to all the functions
1633 * listed here.
1634 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1635 * The key remains associated with the operation even if the content
1636 * of the key slot changes.
1637 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1638 * of the message each time. The MAC that is calculated is the MAC
1639 * of the concatenation of these messages in order.
1640 * -# At the end of the message, call psa_mac_verify_finish() to finish
1641 * calculating the actual MAC of the message and verify it against
1642 * the expected value.
1643 *
1644 * The application may call psa_mac_abort() at any time after the operation
1645 * has been initialized with psa_mac_verify_setup().
1646 *
1647 * After a successful call to psa_mac_verify_setup(), the application must
1648 * eventually terminate the operation through one of the following methods:
1649 * - A failed call to psa_mac_update().
1650 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1651 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001652 * \param[out] operation The operation object to use.
1653 * \param key Slot containing the key to use for the operation.
1654 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1655 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001656 *
Gilles Peskine28538492018-07-11 17:34:00 +02001657 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001658 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001659 * \retval #PSA_ERROR_EMPTY_SLOT
1660 * \retval #PSA_ERROR_NOT_PERMITTED
1661 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001662 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001663 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001664 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001665 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1666 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1667 * \retval #PSA_ERROR_HARDWARE_FAILURE
1668 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001669 */
1670psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
1671 psa_key_slot_t key,
1672 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001673
Gilles Peskinedcd14942018-07-12 00:30:52 +02001674/** Add a message fragment to a multipart MAC operation.
1675 *
1676 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1677 * before calling this function.
1678 *
1679 * If this function returns an error status, the operation becomes inactive.
1680 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001681 * \param[in,out] operation Active MAC operation.
1682 * \param[in] input Buffer containing the message fragment to add to
1683 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001684 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001685 *
1686 * \retval #PSA_SUCCESS
1687 * Success.
1688 * \retval #PSA_ERROR_BAD_STATE
1689 * The operation state is not valid (not started, or already completed).
1690 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1691 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1692 * \retval #PSA_ERROR_HARDWARE_FAILURE
1693 * \retval #PSA_ERROR_TAMPERING_DETECTED
1694 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001695psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1696 const uint8_t *input,
1697 size_t input_length);
1698
Gilles Peskinedcd14942018-07-12 00:30:52 +02001699/** Finish the calculation of the MAC of a message.
1700 *
1701 * The application must call psa_mac_sign_setup() before calling this function.
1702 * This function calculates the MAC of the message formed by concatenating
1703 * the inputs passed to preceding calls to psa_mac_update().
1704 *
1705 * When this function returns, the operation becomes inactive.
1706 *
1707 * \warning Applications should not call this function if they expect
1708 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1709 * Beware that comparing integrity or authenticity data such as
1710 * MAC values with a function such as \c memcmp is risky
1711 * because the time taken by the comparison may leak information
1712 * about the MAC value which could allow an attacker to guess
1713 * a valid MAC and thereby bypass security controls.
1714 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001715 * \param[in,out] operation Active MAC operation.
1716 * \param[out] mac Buffer where the MAC value is to be written.
1717 * \param mac_size Size of the \p mac buffer in bytes.
1718 * \param[out] mac_length On success, the number of bytes
1719 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001720 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001721 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001722 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001723 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001724 *
1725 * \retval #PSA_SUCCESS
1726 * Success.
1727 * \retval #PSA_ERROR_BAD_STATE
1728 * The operation state is not valid (not started, or already completed).
1729 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001730 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001731 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1732 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1733 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1734 * \retval #PSA_ERROR_HARDWARE_FAILURE
1735 * \retval #PSA_ERROR_TAMPERING_DETECTED
1736 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001737psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1738 uint8_t *mac,
1739 size_t mac_size,
1740 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001741
Gilles Peskinedcd14942018-07-12 00:30:52 +02001742/** Finish the calculation of the MAC of a message and compare it with
1743 * an expected value.
1744 *
1745 * The application must call psa_mac_verify_setup() before calling this function.
1746 * This function calculates the MAC of the message formed by concatenating
1747 * the inputs passed to preceding calls to psa_mac_update(). It then
1748 * compares the calculated MAC with the expected MAC passed as a
1749 * parameter to this function.
1750 *
1751 * When this function returns, the operation becomes inactive.
1752 *
1753 * \note Implementations shall make the best effort to ensure that the
1754 * comparison between the actual MAC and the expected MAC is performed
1755 * in constant time.
1756 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001757 * \param[in,out] operation Active MAC operation.
1758 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001759 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001760 *
1761 * \retval #PSA_SUCCESS
1762 * The expected MAC is identical to the actual MAC of the message.
1763 * \retval #PSA_ERROR_INVALID_SIGNATURE
1764 * The MAC of the message was calculated successfully, but it
1765 * differs from the expected MAC.
1766 * \retval #PSA_ERROR_BAD_STATE
1767 * The operation state is not valid (not started, or already completed).
1768 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1769 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1770 * \retval #PSA_ERROR_HARDWARE_FAILURE
1771 * \retval #PSA_ERROR_TAMPERING_DETECTED
1772 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001773psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1774 const uint8_t *mac,
1775 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001776
Gilles Peskinedcd14942018-07-12 00:30:52 +02001777/** Abort a MAC operation.
1778 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001779 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001780 * \p operation structure itself. Once aborted, the operation object
1781 * can be reused for another operation by calling
1782 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001783 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001784 * You may call this function any time after the operation object has
1785 * been initialized by any of the following methods:
1786 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1787 * it succeeds or not.
1788 * - Initializing the \c struct to all-bits-zero.
1789 * - Initializing the \c struct to logical zeros, e.g.
1790 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001791 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001792 * In particular, calling psa_mac_abort() after the operation has been
1793 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1794 * psa_mac_verify_finish() is safe and has no effect.
1795 *
1796 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001797 *
1798 * \retval #PSA_SUCCESS
1799 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001800 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001801 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1802 * \retval #PSA_ERROR_HARDWARE_FAILURE
1803 * \retval #PSA_ERROR_TAMPERING_DETECTED
1804 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001805psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1806
1807/**@}*/
1808
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001809/** \defgroup cipher Symmetric ciphers
1810 * @{
1811 */
1812
1813/** The type of the state data structure for multipart cipher operations.
1814 *
1815 * This is an implementation-defined \c struct. Applications should not
1816 * make any assumptions about the content of this structure except
1817 * as directed by the documentation of a specific implementation. */
1818typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1819
1820/** Set the key for a multipart symmetric encryption operation.
1821 *
1822 * The sequence of operations to encrypt a message with a symmetric cipher
1823 * is as follows:
1824 * -# Allocate an operation object which will be passed to all the functions
1825 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02001826 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001827 * The key remains associated with the operation even if the content
1828 * of the key slot changes.
Gilles Peskinefe119512018-07-08 21:39:34 +02001829 * -# Call either psa_encrypt_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001830 * generate or set the IV (initialization vector). You should use
1831 * psa_encrypt_generate_iv() unless the protocol you are implementing
1832 * requires a specific IV value.
1833 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1834 * of the message each time.
1835 * -# Call psa_cipher_finish().
1836 *
1837 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02001838 * has been initialized with psa_cipher_encrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001839 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001840 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001841 * eventually terminate the operation. The following events terminate an
1842 * operation:
Gilles Peskinefe119512018-07-08 21:39:34 +02001843 * - A failed call to psa_encrypt_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001844 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001845 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001846 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001847 * \param[out] operation The operation object to use.
1848 * \param key Slot containing the key to use for the operation.
1849 * \param alg The cipher algorithm to compute
1850 * (\c PSA_ALG_XXX value such that
1851 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001852 *
Gilles Peskine28538492018-07-11 17:34:00 +02001853 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001854 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001855 * \retval #PSA_ERROR_EMPTY_SLOT
1856 * \retval #PSA_ERROR_NOT_PERMITTED
1857 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001858 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001859 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001860 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001861 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1862 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1863 * \retval #PSA_ERROR_HARDWARE_FAILURE
1864 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001865 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001866psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
1867 psa_key_slot_t key,
1868 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001869
1870/** Set the key for a multipart symmetric decryption operation.
1871 *
1872 * The sequence of operations to decrypt a message with a symmetric cipher
1873 * is as follows:
1874 * -# Allocate an operation object which will be passed to all the functions
1875 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02001876 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001877 * The key remains associated with the operation even if the content
1878 * of the key slot changes.
1879 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1880 * decryption. If the IV is prepended to the ciphertext, you can call
1881 * psa_cipher_update() on a buffer containing the IV followed by the
1882 * beginning of the message.
1883 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1884 * of the message each time.
1885 * -# Call psa_cipher_finish().
1886 *
1887 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02001888 * has been initialized with psa_cipher_decrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001889 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001890 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001891 * eventually terminate the operation. The following events terminate an
1892 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001893 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001894 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001895 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001896 * \param[out] operation The operation object to use.
1897 * \param key Slot containing the key to use for the operation.
1898 * \param alg The cipher algorithm to compute
1899 * (\c PSA_ALG_XXX value such that
1900 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001901 *
Gilles Peskine28538492018-07-11 17:34:00 +02001902 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001903 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001904 * \retval #PSA_ERROR_EMPTY_SLOT
1905 * \retval #PSA_ERROR_NOT_PERMITTED
1906 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001907 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001908 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001909 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001910 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1911 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1912 * \retval #PSA_ERROR_HARDWARE_FAILURE
1913 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001914 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001915psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
1916 psa_key_slot_t key,
1917 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001918
Gilles Peskinedcd14942018-07-12 00:30:52 +02001919/** Generate an IV for a symmetric encryption operation.
1920 *
1921 * This function generates a random IV (initialization vector), nonce
1922 * or initial counter value for the encryption operation as appropriate
1923 * for the chosen algorithm, key type and key size.
1924 *
1925 * The application must call psa_cipher_encrypt_setup() before
1926 * calling this function.
1927 *
1928 * If this function returns an error status, the operation becomes inactive.
1929 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001930 * \param[in,out] operation Active cipher operation.
1931 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001932 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001933 * \param[out] iv_length On success, the number of bytes of the
1934 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001935 *
1936 * \retval #PSA_SUCCESS
1937 * Success.
1938 * \retval #PSA_ERROR_BAD_STATE
1939 * The operation state is not valid (not started, or IV already set).
1940 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001941 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001942 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1943 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1944 * \retval #PSA_ERROR_HARDWARE_FAILURE
1945 * \retval #PSA_ERROR_TAMPERING_DETECTED
1946 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001947psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1948 unsigned char *iv,
1949 size_t iv_size,
1950 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001951
Gilles Peskinedcd14942018-07-12 00:30:52 +02001952/** Set the IV for a symmetric encryption or decryption operation.
1953 *
1954 * This function sets the random IV (initialization vector), nonce
1955 * or initial counter value for the encryption or decryption operation.
1956 *
1957 * The application must call psa_cipher_encrypt_setup() before
1958 * calling this function.
1959 *
1960 * If this function returns an error status, the operation becomes inactive.
1961 *
1962 * \note When encrypting, applications should use psa_cipher_generate_iv()
1963 * instead of this function, unless implementing a protocol that requires
1964 * a non-random IV.
1965 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001966 * \param[in,out] operation Active cipher operation.
1967 * \param[in] iv Buffer containing the IV to use.
1968 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001969 *
1970 * \retval #PSA_SUCCESS
1971 * Success.
1972 * \retval #PSA_ERROR_BAD_STATE
1973 * The operation state is not valid (not started, or IV already set).
1974 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001975 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001976 * or the chosen algorithm does not use an IV.
1977 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1978 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1979 * \retval #PSA_ERROR_HARDWARE_FAILURE
1980 * \retval #PSA_ERROR_TAMPERING_DETECTED
1981 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001982psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1983 const unsigned char *iv,
1984 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001985
Gilles Peskinedcd14942018-07-12 00:30:52 +02001986/** Encrypt or decrypt a message fragment in an active cipher operation.
1987 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001988 * Before calling this function, you must:
1989 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1990 * The choice of setup function determines whether this function
1991 * encrypts or decrypts its input.
1992 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1993 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001994 *
1995 * If this function returns an error status, the operation becomes inactive.
1996 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001997 * \param[in,out] operation Active cipher operation.
1998 * \param[in] input Buffer containing the message fragment to
1999 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002000 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002001 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002002 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002003 * \param[out] output_length On success, the number of bytes
2004 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002005 *
2006 * \retval #PSA_SUCCESS
2007 * Success.
2008 * \retval #PSA_ERROR_BAD_STATE
2009 * The operation state is not valid (not started, IV required but
2010 * not set, or already completed).
2011 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2012 * The size of the \p output buffer is too small.
2013 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2014 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2015 * \retval #PSA_ERROR_HARDWARE_FAILURE
2016 * \retval #PSA_ERROR_TAMPERING_DETECTED
2017 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002018psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2019 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02002020 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002021 unsigned char *output,
2022 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002023 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002024
Gilles Peskinedcd14942018-07-12 00:30:52 +02002025/** Finish encrypting or decrypting a message in a cipher operation.
2026 *
2027 * The application must call psa_cipher_encrypt_setup() or
2028 * psa_cipher_decrypt_setup() before calling this function. The choice
2029 * of setup function determines whether this function encrypts or
2030 * decrypts its input.
2031 *
2032 * This function finishes the encryption or decryption of the message
2033 * formed by concatenating the inputs passed to preceding calls to
2034 * psa_cipher_update().
2035 *
2036 * When this function returns, the operation becomes inactive.
2037 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002038 * \param[in,out] operation Active cipher operation.
2039 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002040 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002041 * \param[out] output_length On success, the number of bytes
2042 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002043 *
2044 * \retval #PSA_SUCCESS
2045 * Success.
2046 * \retval #PSA_ERROR_BAD_STATE
2047 * The operation state is not valid (not started, IV required but
2048 * not set, or already completed).
2049 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2050 * The size of the \p output buffer is too small.
2051 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2052 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2053 * \retval #PSA_ERROR_HARDWARE_FAILURE
2054 * \retval #PSA_ERROR_TAMPERING_DETECTED
2055 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002056psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002057 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002058 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002059 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002060
Gilles Peskinedcd14942018-07-12 00:30:52 +02002061/** Abort a cipher operation.
2062 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002063 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002064 * \p operation structure itself. Once aborted, the operation object
2065 * can be reused for another operation by calling
2066 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002067 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002068 * You may call this function any time after the operation object has
2069 * been initialized by any of the following methods:
2070 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2071 * whether it succeeds or not.
2072 * - Initializing the \c struct to all-bits-zero.
2073 * - Initializing the \c struct to logical zeros, e.g.
2074 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002075 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002076 * In particular, calling psa_cipher_abort() after the operation has been
2077 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2078 * is safe and has no effect.
2079 *
2080 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002081 *
2082 * \retval #PSA_SUCCESS
2083 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002084 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002085 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2086 * \retval #PSA_ERROR_HARDWARE_FAILURE
2087 * \retval #PSA_ERROR_TAMPERING_DETECTED
2088 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002089psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2090
2091/**@}*/
2092
Gilles Peskine3b555712018-03-03 21:27:57 +01002093/** \defgroup aead Authenticated encryption with associated data (AEAD)
2094 * @{
2095 */
2096
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002097/** The tag size for an AEAD algorithm, in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01002098 *
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002099 * \param alg An AEAD algorithm
2100 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002101 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002102 *
2103 * \return The tag size for the specified algorithm.
2104 * If the AEAD algorithm does not have an identified
2105 * tag that can be distinguished from the rest of
2106 * the ciphertext, return 0.
2107 * If the AEAD algorithm is not recognized, return 0.
2108 * An implementation may return either 0 or a
2109 * correct size for an AEAD algorithm that it
2110 * recognizes, but does not support.
2111 */
2112#define PSA_AEAD_TAG_SIZE(alg) \
2113 ((alg) == PSA_ALG_GCM ? 16 : \
2114 (alg) == PSA_ALG_CCM ? 16 : \
2115 0)
Gilles Peskine3b555712018-03-03 21:27:57 +01002116
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002117/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002118 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002119 * \param key Slot containing the key to use.
2120 * \param alg The AEAD algorithm to compute
2121 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002122 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002123 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002124 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002125 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002126 * but not encrypted.
2127 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002128 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002129 * encrypted.
2130 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002131 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002132 * encrypted data. The additional data is not
2133 * part of this output. For algorithms where the
2134 * encrypted data and the authentication tag
2135 * are defined as separate outputs, the
2136 * authentication tag is appended to the
2137 * encrypted data.
2138 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2139 * This must be at least
2140 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2141 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002142 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002143 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002144 *
Gilles Peskine28538492018-07-11 17:34:00 +02002145 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002146 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002147 * \retval #PSA_ERROR_EMPTY_SLOT
2148 * \retval #PSA_ERROR_NOT_PERMITTED
2149 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002150 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002151 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002152 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002153 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2154 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2155 * \retval #PSA_ERROR_HARDWARE_FAILURE
2156 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine3b555712018-03-03 21:27:57 +01002157 */
mohammad160339ee8712018-04-26 00:51:02 +03002158psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2159 psa_algorithm_t alg,
2160 const uint8_t *nonce,
2161 size_t nonce_length,
2162 const uint8_t *additional_data,
2163 size_t additional_data_length,
2164 const uint8_t *plaintext,
2165 size_t plaintext_length,
2166 uint8_t *ciphertext,
2167 size_t ciphertext_size,
2168 size_t *ciphertext_length );
Gilles Peskine3b555712018-03-03 21:27:57 +01002169
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002170/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002171 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002172 * \param key Slot containing the key to use.
2173 * \param alg The AEAD algorithm to compute
2174 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002175 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002176 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002177 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002178 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002179 * but not encrypted.
2180 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002181 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002182 * encrypted. For algorithms where the
2183 * encrypted data and the authentication tag
2184 * are defined as separate inputs, the buffer
2185 * must contain the encrypted data followed
2186 * by the authentication tag.
2187 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002188 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002189 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2190 * This must be at least
2191 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2192 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002193 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03002194 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002195 *
Gilles Peskine28538492018-07-11 17:34:00 +02002196 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002197 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002198 * \retval #PSA_ERROR_EMPTY_SLOT
2199 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002200 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002201 * \retval #PSA_ERROR_NOT_PERMITTED
2202 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002203 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002204 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002205 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002206 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2207 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2208 * \retval #PSA_ERROR_HARDWARE_FAILURE
2209 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine3b555712018-03-03 21:27:57 +01002210 */
mohammad160339ee8712018-04-26 00:51:02 +03002211psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2212 psa_algorithm_t alg,
2213 const uint8_t *nonce,
2214 size_t nonce_length,
2215 const uint8_t *additional_data,
2216 size_t additional_data_length,
2217 const uint8_t *ciphertext,
2218 size_t ciphertext_length,
2219 uint8_t *plaintext,
2220 size_t plaintext_size,
2221 size_t *plaintext_length );
Gilles Peskine3b555712018-03-03 21:27:57 +01002222
2223/**@}*/
2224
Gilles Peskine20035e32018-02-03 22:44:14 +01002225/** \defgroup asymmetric Asymmetric cryptography
2226 * @{
2227 */
2228
2229/**
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002230 * \brief ECDSA signature size for a given curve bit size
Gilles Peskine0189e752018-02-03 23:57:22 +01002231 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002232 * \param curve_bits Curve size in bits.
2233 * \return Signature size in bytes.
Gilles Peskine0189e752018-02-03 23:57:22 +01002234 *
2235 * \note This macro returns a compile-time constant if its argument is one.
Gilles Peskine0189e752018-02-03 23:57:22 +01002236 */
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002237#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
2238 (PSA_BITS_TO_BYTES(curve_bits) * 2)
Gilles Peskine0189e752018-02-03 23:57:22 +01002239
Gilles Peskine0189e752018-02-03 23:57:22 +01002240/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002241 * \brief Sign a hash or short message with a private key.
2242 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002243 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002244 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002245 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2246 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2247 * to determine the hash algorithm to use.
2248 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002249 * \param key Key slot containing an asymmetric key pair.
2250 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002251 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002252 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002253 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002254 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002255 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002256 * \param[out] signature_length On success, the number of bytes
2257 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002258 *
Gilles Peskine28538492018-07-11 17:34:00 +02002259 * \retval #PSA_SUCCESS
2260 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002261 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002262 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002263 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002264 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002265 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002266 * \retval #PSA_ERROR_NOT_SUPPORTED
2267 * \retval #PSA_ERROR_INVALID_ARGUMENT
2268 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2269 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2270 * \retval #PSA_ERROR_HARDWARE_FAILURE
2271 * \retval #PSA_ERROR_TAMPERING_DETECTED
2272 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01002273 */
2274psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
2275 psa_algorithm_t alg,
2276 const uint8_t *hash,
2277 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002278 uint8_t *signature,
2279 size_t signature_size,
2280 size_t *signature_length);
2281
2282/**
2283 * \brief Verify the signature a hash or short message using a public key.
2284 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002285 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002286 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002287 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2288 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2289 * to determine the hash algorithm to use.
2290 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01002291 * \param key Key slot containing a public key or an
2292 * asymmetric key pair.
2293 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002294 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002295 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002296 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002297 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002298 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002299 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002300 *
Gilles Peskine28538492018-07-11 17:34:00 +02002301 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002302 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002303 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002304 * The calculation was perfomed successfully, but the passed
2305 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002306 * \retval #PSA_ERROR_NOT_SUPPORTED
2307 * \retval #PSA_ERROR_INVALID_ARGUMENT
2308 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2309 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2310 * \retval #PSA_ERROR_HARDWARE_FAILURE
2311 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01002312 */
2313psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
2314 psa_algorithm_t alg,
2315 const uint8_t *hash,
2316 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002317 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002318 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002319
Gilles Peskine723feff2018-05-31 20:08:13 +02002320#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
Gilles Peskine072ac562018-06-30 00:21:29 +02002321 (PSA_ALG_IS_RSA_OAEP(alg) ? \
2322 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskine723feff2018-05-31 20:08:13 +02002323 11 /*PKCS#1v1.5*/)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002324
2325/**
2326 * \brief Encrypt a short message with a public key.
2327 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002328 * \param key Key slot containing a public key or an
2329 * asymmetric key pair.
2330 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002331 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002332 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002333 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002334 * \param[in] salt A salt or label, if supported by the
2335 * encryption algorithm.
2336 * If the algorithm does not support a
2337 * salt, pass \c NULL.
2338 * If the algorithm supports an optional
2339 * salt and you do not want to pass a salt,
2340 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002341 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002342 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2343 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002344 * \param salt_length Size of the \p salt buffer in bytes.
2345 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002346 * \param[out] output Buffer where the encrypted message is to
2347 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002348 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002349 * \param[out] output_length On success, the number of bytes
2350 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002351 *
Gilles Peskine28538492018-07-11 17:34:00 +02002352 * \retval #PSA_SUCCESS
2353 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002354 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002355 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002356 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002357 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002358 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002359 * \retval #PSA_ERROR_NOT_SUPPORTED
2360 * \retval #PSA_ERROR_INVALID_ARGUMENT
2361 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2362 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2363 * \retval #PSA_ERROR_HARDWARE_FAILURE
2364 * \retval #PSA_ERROR_TAMPERING_DETECTED
2365 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002366 */
2367psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
2368 psa_algorithm_t alg,
2369 const uint8_t *input,
2370 size_t input_length,
2371 const uint8_t *salt,
2372 size_t salt_length,
2373 uint8_t *output,
2374 size_t output_size,
2375 size_t *output_length);
2376
2377/**
2378 * \brief Decrypt a short message with a private key.
2379 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002380 * \param key Key slot containing an asymmetric key pair.
2381 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002382 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002383 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002384 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002385 * \param[in] salt A salt or label, if supported by the
2386 * encryption algorithm.
2387 * If the algorithm does not support a
2388 * salt, pass \c NULL.
2389 * If the algorithm supports an optional
2390 * salt and you do not want to pass a salt,
2391 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002392 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002393 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2394 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002395 * \param salt_length Size of the \p salt buffer in bytes.
2396 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002397 * \param[out] output Buffer where the decrypted message is to
2398 * be written.
2399 * \param output_size Size of the \c output buffer in bytes.
2400 * \param[out] output_length On success, the number of bytes
2401 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002402 *
Gilles Peskine28538492018-07-11 17:34:00 +02002403 * \retval #PSA_SUCCESS
2404 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002405 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002406 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002407 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002408 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002409 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002410 * \retval #PSA_ERROR_NOT_SUPPORTED
2411 * \retval #PSA_ERROR_INVALID_ARGUMENT
2412 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2413 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2414 * \retval #PSA_ERROR_HARDWARE_FAILURE
2415 * \retval #PSA_ERROR_TAMPERING_DETECTED
2416 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2417 * \retval #PSA_ERROR_INVALID_PADDING
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002418 */
2419psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
2420 psa_algorithm_t alg,
2421 const uint8_t *input,
2422 size_t input_length,
2423 const uint8_t *salt,
2424 size_t salt_length,
2425 uint8_t *output,
2426 size_t output_size,
2427 size_t *output_length);
2428
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002429/**@}*/
2430
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002431/** \defgroup generation Key generation
2432 * @{
2433 */
2434
2435/**
2436 * \brief Generate random bytes.
2437 *
2438 * \warning This function **can** fail! Callers MUST check the return status
2439 * and MUST NOT use the content of the output buffer if the return
2440 * status is not #PSA_SUCCESS.
2441 *
2442 * \note To generate a key, use psa_generate_key() instead.
2443 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002444 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002445 * \param output_size Number of bytes to generate and output.
2446 *
Gilles Peskine28538492018-07-11 17:34:00 +02002447 * \retval #PSA_SUCCESS
2448 * \retval #PSA_ERROR_NOT_SUPPORTED
2449 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2450 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2451 * \retval #PSA_ERROR_HARDWARE_FAILURE
2452 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002453 */
2454psa_status_t psa_generate_random(uint8_t *output,
2455 size_t output_size);
2456
Gilles Peskine4c317f42018-07-12 01:24:09 +02002457/** Extra parameters for RSA key generation.
2458 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002459 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002460 * parameter to psa_generate_key().
2461 */
2462typedef struct {
2463 uint32_t e; /**! Public exponent value. Default: 65537. */
2464} psa_generate_key_extra_rsa;
2465
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002466/**
2467 * \brief Generate a key or key pair.
2468 *
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002469 * \param key Slot where the key will be stored. This must be a
2470 * valid slot for a key of the chosen type. It must
2471 * be unoccupied.
2472 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2473 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002474 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002475 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002476 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002477 * default parameters. Implementation that support
2478 * the generation of vendor-specific key types
2479 * that allow extra parameters shall document
2480 * the format of these extra parameters and
2481 * the default values. For standard parameters,
2482 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002483 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002484 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2485 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002486 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002487 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2488 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002489 * - For an RSA key (\p type is
2490 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2491 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002492 * specifying the public exponent. The
2493 * default public exponent used when \p extra
2494 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002495 * \param extra_size Size of the buffer that \p extra
2496 * points to, in bytes. Note that if \p extra is
2497 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002498 *
Gilles Peskine28538492018-07-11 17:34:00 +02002499 * \retval #PSA_SUCCESS
2500 * \retval #PSA_ERROR_NOT_SUPPORTED
2501 * \retval #PSA_ERROR_INVALID_ARGUMENT
2502 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2503 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2504 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2505 * \retval #PSA_ERROR_HARDWARE_FAILURE
2506 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002507 */
2508psa_status_t psa_generate_key(psa_key_slot_t key,
2509 psa_key_type_t type,
2510 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002511 const void *extra,
2512 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002513
2514/**@}*/
2515
Gilles Peskinee59236f2018-01-27 23:32:46 +01002516#ifdef __cplusplus
2517}
2518#endif
2519
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002520/* The file "crypto_sizes.h" contains definitions for size calculation
2521 * macros whose definitions are implementation-specific. */
2522#include "crypto_sizes.h"
2523
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002524/* The file "crypto_struct.h" contains definitions for
2525 * implementation-specific structs that are declared above. */
2526#include "crypto_struct.h"
2527
2528/* The file "crypto_extra.h" contains vendor-specific definitions. This
2529 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002530#include "crypto_extra.h"
2531
2532#endif /* PSA_CRYPTO_H */