blob: cbc7f4d4598f6194920e7b61483eedb76f5ed8a7 [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 Peskined8008d62018-06-29 19:51:51 +0200433/** Whether a key type is an RSA key (pair or public-only). */
434#define PSA_KEY_TYPE_IS_RSA(type) \
Gilles Peskine3bd1a422018-07-19 11:55:51 +0200435 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
436
Gilles Peskined8008d62018-06-29 19:51:51 +0200437/** Whether a key type is an elliptic curve key (pair or public-only). */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100438#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100439 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
440 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100441
Gilles Peskinee1fed0d2018-06-18 20:45:45 +0200442/** The type of PSA elliptic curve identifiers. */
443typedef uint16_t psa_ecc_curve_t;
444/** Extract the curve from an elliptic curve key type. */
445#define PSA_KEY_TYPE_GET_CURVE(type) \
446 ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
447 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
448 0))
449
450/* The encoding of curve identifiers is currently aligned with the
451 * TLS Supported Groups Registry (formerly known as the
452 * TLS EC Named Curve Registry)
453 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
454 * The values are defined by RFC 4492, RFC 7027 and RFC 7919. */
455#define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001)
456#define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002)
457#define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003)
458#define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004)
459#define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005)
460#define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006)
461#define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007)
462#define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008)
463#define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009)
464#define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a)
465#define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b)
466#define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c)
467#define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d)
468#define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e)
469#define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f)
470#define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010)
471#define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011)
472#define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012)
473#define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013)
474#define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014)
475#define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015)
476#define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016)
477#define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017)
478#define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018)
479#define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019)
480#define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a)
481#define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b)
482#define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c)
483#define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d)
484#define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e)
485#define PSA_ECC_CURVE_FFDHE_2048 ((psa_ecc_curve_t) 0x0100)
486#define PSA_ECC_CURVE_FFDHE_3072 ((psa_ecc_curve_t) 0x0101)
487#define PSA_ECC_CURVE_FFDHE_4096 ((psa_ecc_curve_t) 0x0102)
488#define PSA_ECC_CURVE_FFDHE_6144 ((psa_ecc_curve_t) 0x0103)
489#define PSA_ECC_CURVE_FFDHE_8192 ((psa_ecc_curve_t) 0x0104)
490
Gilles Peskine7e198532018-03-08 07:50:30 +0100491/** The block size of a block cipher.
492 *
493 * \param type A cipher key type (value of type #psa_key_type_t).
494 *
495 * \return The block size for a block cipher, or 1 for a stream cipher.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200496 * The return value is undefined if \p type is not a supported
Gilles Peskine35855962018-04-19 08:39:16 +0200497 * cipher key type.
498 *
499 * \note It is possible to build stream cipher algorithms on top of a block
500 * cipher, for example CTR mode (#PSA_ALG_CTR).
501 * This macro only takes the key type into account, so it cannot be
502 * used to determine the size of the data that #psa_cipher_update()
503 * might buffer for future processing in general.
Gilles Peskine7e198532018-03-08 07:50:30 +0100504 *
505 * \note This macro returns a compile-time constant if its argument is one.
506 *
507 * \warning This macro may evaluate its argument multiple times.
508 */
Gilles Peskine03182e92018-03-07 16:40:52 +0100509#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100510 ( \
511 (type) == PSA_KEY_TYPE_AES ? 16 : \
512 (type) == PSA_KEY_TYPE_DES ? 8 : \
513 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
Gilles Peskine7e198532018-03-08 07:50:30 +0100514 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100515 0)
516
Gilles Peskine308b91d2018-02-08 09:47:44 +0100517/** \brief Encoding of a cryptographic algorithm.
518 *
519 * For algorithms that can be applied to multiple key types, this type
520 * does not encode the key type. For example, for symmetric ciphers
521 * based on a block cipher, #psa_algorithm_t encodes the block cipher
522 * mode and the padding mode while the block cipher itself is encoded
523 * via #psa_key_type_t.
524 */
Gilles Peskine20035e32018-02-03 22:44:14 +0100525typedef uint32_t psa_algorithm_t;
526
Gilles Peskine98f0a242018-02-06 18:57:29 +0100527#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
528#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
529#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
530#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
531#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
532#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
533#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
534#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
535#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
536#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100537
Gilles Peskine98f0a242018-02-06 18:57:29 +0100538#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
539 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200540
Gilles Peskine308b91d2018-02-08 09:47:44 +0100541/** Whether the specified algorithm is a hash algorithm.
542 *
Gilles Peskine7e198532018-03-08 07:50:30 +0100543 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100544 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200545 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
546 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskine7e198532018-03-08 07:50:30 +0100547 * algorithm identifier.
548 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100549#define PSA_ALG_IS_HASH(alg) \
550 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200551
552/** Whether the specified algorithm is a MAC algorithm.
553 *
554 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
555 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200556 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
557 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200558 * algorithm identifier.
559 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100560#define PSA_ALG_IS_MAC(alg) \
561 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200562
563/** Whether the specified algorithm is a symmetric cipher algorithm.
564 *
565 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
566 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200567 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
568 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200569 * algorithm identifier.
570 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100571#define PSA_ALG_IS_CIPHER(alg) \
572 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200573
574/** Whether the specified algorithm is an authenticated encryption
575 * with associated data (AEAD) algorithm.
576 *
577 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
578 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200579 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
580 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200581 * algorithm identifier.
582 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100583#define PSA_ALG_IS_AEAD(alg) \
584 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200585
586/** Whether the specified algorithm is a public-key signature algorithm.
587 *
588 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
589 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200590 * \return 1 if \p alg is a public-key signature algorithm, 0 otherwise.
591 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200592 * algorithm identifier.
593 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100594#define PSA_ALG_IS_SIGN(alg) \
595 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200596
597/** Whether the specified algorithm is a public-key encryption algorithm.
598 *
599 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
600 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200601 * \return 1 if \p alg is a public-key encryption algorithm, 0 otherwise.
602 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200603 * algorithm identifier.
604 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100605#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
606 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200607
608/** Whether the specified algorithm is a key agreement algorithm.
609 *
610 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
611 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200612 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
613 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200614 * algorithm identifier.
615 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100616#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
617 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200618
619/** Whether the specified algorithm is a key derivation algorithm.
620 *
621 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
622 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200623 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
624 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200625 * algorithm identifier.
626 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100627#define PSA_ALG_IS_KEY_DERIVATION(alg) \
628 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
629
630#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
631#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
632#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
633#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
Gilles Peskinee3f694f2018-03-08 07:48:40 +0100634#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
635#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100636#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
637#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
638#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
639#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
640#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
641#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
642#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
643#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
644#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
645#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
646
Gilles Peskine8c9def32018-02-08 10:02:12 +0100647#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100648#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
Gilles Peskine35855962018-04-19 08:39:16 +0200649/** Macro to build an HMAC algorithm.
650 *
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200651 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
Gilles Peskine35855962018-04-19 08:39:16 +0200652 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200653 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200654 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine35855962018-04-19 08:39:16 +0200655 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200656 * \return The corresponding HMAC algorithm.
657 * \return Unspecified if \p alg is not a supported
658 * hash algorithm.
Gilles Peskine35855962018-04-19 08:39:16 +0200659 */
660#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100661 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200662
Gilles Peskine8c9def32018-02-08 10:02:12 +0100663#define PSA_ALG_HMAC_HASH(hmac_alg) \
664 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200665
666/** Whether the specified algorithm is an HMAC algorithm.
667 *
668 * HMAC is a family of MAC algorithms that are based on a hash function.
669 *
670 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
671 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200672 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
673 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200674 * algorithm identifier.
675 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100676#define PSA_ALG_IS_HMAC(alg) \
677 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
678 PSA_ALG_HMAC_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200679
Gilles Peskine8c9def32018-02-08 10:02:12 +0100680#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
681#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
682#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
683#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200684
685/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
686 *
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200687 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
688 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200689 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
690 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200691 * algorithm identifier.
692 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100693#define PSA_ALG_IS_CIPHER_MAC(alg) \
694 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
695 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100696
Gilles Peskine8c9def32018-02-08 10:02:12 +0100697#define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100698#define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100699#define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100700#define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200701
702/** Use a block cipher mode without padding.
703 *
704 * This padding mode may only be used with messages whose lengths are a
705 * whole number of blocks for the chosen block cipher.
706 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100707#define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200708
Gilles Peskine98f0a242018-02-06 18:57:29 +0100709#define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200710
711/** Whether the specified algorithm is a block cipher.
712 *
713 * A block cipher is a symmetric cipher that encrypts or decrypts messages
714 * by chopping them into fixed-size blocks. Processing a message requires
715 * applying a _padding mode_ to transform the message into one whose
716 * length is a whole number of blocks. To construct an algorithm
717 * identifier for a block cipher, apply a bitwise-or between the block
718 * cipher mode and the padding mode. For example, CBC with PKCS#7 padding
719 * is `PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7`.
720 *
721 * The transformation applied to each block is determined by the key type.
722 * For example, to use AES-128-CBC-PKCS7, use the algorithm above with
723 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
724 *
725 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
726 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200727 * \return 1 if \p alg is a block cipher algorithm, 0 otherwise.
728 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200729 * algorithm identifier or if it is not a symmetric cipher algorithm.
730 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100731#define PSA_ALG_IS_BLOCK_CIPHER(alg) \
732 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
733 PSA_ALG_BLOCK_CIPHER_BASE)
734
Gilles Peskinedcd14942018-07-12 00:30:52 +0200735/** The CBC block cipher mode.
736 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100737#define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100738#define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002)
739#define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003)
740#define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004)
Gilles Peskine5d1888e2018-07-12 00:32:42 +0200741
742#define PSA_ALG_STREAM_CIPHER_BASE ((psa_algorithm_t)0x04800000)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200743
Gilles Peskinedcd14942018-07-12 00:30:52 +0200744/** The CTR stream cipher mode.
745 *
746 * CTR is a stream cipher which is built from a block cipher. The
747 * underlying block cipher is determined by the key type. For example,
748 * to use AES-128-CTR, use this algorithm with
749 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
750 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100751#define PSA_ALG_CTR ((psa_algorithm_t)0x04800001)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200752
Gilles Peskinedcd14942018-07-12 00:30:52 +0200753/** The ARC4 stream cipher algorithm.
754 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100755#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100756
Gilles Peskinedcd14942018-07-12 00:30:52 +0200757/** Whether the specified algorithm is a stream cipher.
758 *
759 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
760 * by applying a bitwise-xor with a stream of bytes that is generated
761 * from a key.
762 *
763 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
764 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200765 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
766 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200767 * algorithm identifier or if it is not a symmetric cipher algorithm.
768 */
Moran Pekerbed71a22018-04-22 20:19:20 +0300769#define PSA_ALG_IS_STREAM_CIPHER(alg) \
770 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
Gilles Peskine5d1888e2018-07-12 00:32:42 +0200771 PSA_ALG_STREAM_CIPHER_BASE)
Moran Pekerbed71a22018-04-22 20:19:20 +0300772
Gilles Peskine8c9def32018-02-08 10:02:12 +0100773#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
774#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100775
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200776#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
777/** RSA PKCS#1 v1.5 signature with hashing.
778 *
779 * This is the signature scheme defined by RFC 8017
780 * (PKCS#1: RSA Cryptography Specifications) under the name
781 * RSASSA-PKCS1-v1_5.
782 *
783 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200784 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200785 *
786 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
787 * \return Unspecified if \p alg is not a supported
788 * hash algorithm.
789 */
Gilles Peskinea5926232018-03-28 14:16:50 +0200790#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200791 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
792/** Raw PKCS#1 v1.5 signature.
793 *
794 * The input to this algorithm is the DigestInfo structure used by
795 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
796 * steps 3&ndash;6.
797 */
798#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
Gilles Peskinea5926232018-03-28 14:16:50 +0200799#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200800 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200801
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200802#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
803/** RSA PSS signature with hashing.
804 *
805 * This is the signature scheme defined by RFC 8017
806 * (PKCS#1: RSA Cryptography Specifications) under the name
Gilles Peskinea4d20bd2018-06-29 23:35:02 +0200807 * RSASSA-PSS, with the message generation function MGF1, and with
808 * a salt length equal to the length of the hash. The specified
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200809 * hash algorithm is used to hash the input message, to create the
810 * salted hash, and for the mask generation.
811 *
812 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200813 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200814 *
815 * \return The corresponding RSA PSS signature algorithm.
816 * \return Unspecified if \p alg is not a supported
817 * hash algorithm.
818 */
819#define PSA_ALG_RSA_PSS(hash_alg) \
820 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
821#define PSA_ALG_IS_RSA_PSS(alg) \
822 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
823
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200824#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
825/** DSA signature with hashing.
826 *
827 * This is the signature scheme defined by FIPS 186-4,
828 * with a random per-message secret number (*k*).
829 *
830 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200831 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200832 *
833 * \return The corresponding DSA signature algorithm.
834 * \return Unspecified if \p alg is not a supported
835 * hash algorithm.
836 */
837#define PSA_ALG_DSA(hash_alg) \
838 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
839#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
840#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
841#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
842 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
843#define PSA_ALG_IS_DSA(alg) \
844 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
845 PSA_ALG_DSA_BASE)
846#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
847 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
848
849#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
850/** ECDSA signature with hashing.
851 *
852 * This is the ECDSA signature scheme defined by ANSI X9.62,
853 * with a random per-message secret number (*k*).
854 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200855 * The representation of the signature as a byte string consists of
856 * the concatentation of the signature values *r* and *s*. Each of
857 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
858 * of the base point of the curve in octets. Each value is represented
859 * in big-endian order (most significant octet first).
860 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200861 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200862 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200863 *
864 * \return The corresponding ECDSA signature algorithm.
865 * \return Unspecified if \p alg is not a supported
866 * hash algorithm.
867 */
868#define PSA_ALG_ECDSA(hash_alg) \
869 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
870/** ECDSA signature without hashing.
871 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200872 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200873 * without specifying a hash algorithm. This algorithm may only be
874 * used to sign or verify a sequence of bytes that should be an
875 * already-calculated hash. Note that the input is padded with
876 * zeros on the left or truncated on the left as required to fit
877 * the curve size.
878 */
879#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
880#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
881/** Deterministic ECDSA signature with hashing.
882 *
883 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
884 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200885 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
886 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200887 * Note that when this algorithm is used for verification, signatures
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200888 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200889 * same private key are accepted. In other words,
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200890 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
891 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200892 *
893 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200894 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200895 *
896 * \return The corresponding deterministic ECDSA signature
897 * algorithm.
898 * \return Unspecified if \p alg is not a supported
899 * hash algorithm.
900 */
901#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
902 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
903#define PSA_ALG_IS_ECDSA(alg) \
904 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
905 PSA_ALG_ECDSA_BASE)
906#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
907 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
908
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200909/** Get the hash used by a hash-and-sign signature algorithm.
910 *
911 * A hash-and-sign algorithm is a signature algorithm which is
912 * composed of two phases: first a hashing phase which does not use
913 * the key and produces a hash of the input message, then a signing
914 * phase which only uses the hash and the key and not the message
915 * itself.
916 *
917 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200918 * #PSA_ALG_IS_SIGN(\p alg) is true).
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200919 *
920 * \return The underlying hash algorithm if \p alg is a hash-and-sign
921 * algorithm.
922 * \return 0 if \p alg is a signature algorithm that does not
923 * follow the hash-and-sign structure.
924 * \return Unspecified if \p alg is not a signature algorithm or
925 * if it is not supported by the implementation.
926 */
927#define PSA_ALG_SIGN_GET_HASH(alg) \
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200928 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
929 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg) ? \
Gilles Peskine54622ae2018-06-29 22:24:24 +0200930 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200931 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
932 0)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100933
Gilles Peskinedcd14942018-07-12 00:30:52 +0200934/** RSA PKCS#1 v1.5 encryption.
935 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200936#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200937
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200938#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200939/** RSA OAEP encryption.
940 *
941 * This is the encryption scheme defined by RFC 8017
942 * (PKCS#1: RSA Cryptography Specifications) under the name
943 * RSAES-OAEP, with the message generation function MGF1.
944 *
945 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
946 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
947 * for MGF1.
948 *
949 * \return The corresponding RSA OAEP signature algorithm.
950 * \return Unspecified if \p alg is not a supported
951 * hash algorithm.
952 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200953#define PSA_ALG_RSA_OAEP(hash_alg) \
954 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
955#define PSA_ALG_IS_RSA_OAEP(alg) \
956 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
Gilles Peskine072ac562018-06-30 00:21:29 +0200957#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
958 (PSA_ALG_IS_RSA_OAEP(alg) ? \
959 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
960 0)
Gilles Peskined1e8e412018-06-07 09:49:39 +0200961
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100962/**@}*/
963
964/** \defgroup key_management Key management
965 * @{
966 */
967
968/**
969 * \brief Import a key in binary format.
970 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100971 * This function supports any output from psa_export_key(). Refer to the
972 * documentation of psa_export_key() for the format for each key type.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100973 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100974 * \param key Slot where the key will be stored. This must be a
975 * valid slot for a key of the chosen type. It must
976 * be unoccupied.
977 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskineedd11a12018-07-12 01:08:58 +0200978 * \param[in] data Buffer containing the key data.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200979 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100980 *
Gilles Peskine28538492018-07-11 17:34:00 +0200981 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100982 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200983 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200984 * The key type or key size is not supported, either by the
985 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200986 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +0100987 * The key slot is invalid,
988 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200989 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +0200990 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200991 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
992 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
993 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
994 * \retval #PSA_ERROR_HARDWARE_FAILURE
995 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100996 */
997psa_status_t psa_import_key(psa_key_slot_t key,
998 psa_key_type_t type,
999 const uint8_t *data,
1000 size_t data_length);
1001
1002/**
Gilles Peskine154bd952018-04-19 08:38:16 +02001003 * \brief Destroy a key and restore the slot to its default state.
1004 *
1005 * This function destroys the content of the key slot from both volatile
1006 * memory and, if applicable, non-volatile storage. Implementations shall
1007 * make a best effort to ensure that any previous content of the slot is
1008 * unrecoverable.
1009 *
1010 * This function also erases any metadata such as policies. It returns the
1011 * specified slot to its default state.
1012 *
1013 * \param key The key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001014 *
Gilles Peskine28538492018-07-11 17:34:00 +02001015 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +02001016 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +02001017 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001018 * The slot holds content and cannot be erased because it is
1019 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskine28538492018-07-11 17:34:00 +02001020 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001021 * The specified slot number does not designate a valid slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001022 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001023 * There was an failure in communication with the cryptoprocessor.
1024 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +02001025 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001026 * The storage is corrupted. Implementations shall make a best effort
1027 * to erase key material even in this stage, however applications
1028 * should be aware that it may be impossible to guarantee that the
1029 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +02001030 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001031 * An unexpected condition which is not a storage corruption or
1032 * a communication failure occurred. The cryptoprocessor may have
1033 * been compromised.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001034 */
1035psa_status_t psa_destroy_key(psa_key_slot_t key);
1036
1037/**
1038 * \brief Get basic metadata about a key.
1039 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001040 * \param key Slot whose content is queried. This must
1041 * be an occupied key slot.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001042 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001043 * This may be a null pointer, in which case the key type
1044 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001045 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +01001046 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +01001047 * is not written.
1048 *
Gilles Peskine28538492018-07-11 17:34:00 +02001049 * \retval #PSA_SUCCESS
1050 * \retval #PSA_ERROR_EMPTY_SLOT
1051 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1052 * \retval #PSA_ERROR_HARDWARE_FAILURE
1053 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001054 */
1055psa_status_t psa_get_key_information(psa_key_slot_t key,
1056 psa_key_type_t *type,
1057 size_t *bits);
1058
1059/**
1060 * \brief Export a key in binary format.
1061 *
1062 * The output of this function can be passed to psa_import_key() to
1063 * create an equivalent object.
1064 *
1065 * If a key is created with psa_import_key() and then exported with
1066 * this function, it is not guaranteed that the resulting data is
1067 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +01001068 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001069 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001070 * For standard key types, the output format is as follows:
1071 *
1072 * - For symmetric keys (including MAC keys), the format is the
1073 * raw bytes of the key.
1074 * - For DES, the key data consists of 8 bytes. The parity bits must be
1075 * correct.
1076 * - For Triple-DES, the format is the concatenation of the
1077 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +01001078 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine2743e422018-06-27 22:57:11 +02001079 * is the non-encrypted DER representation defined by PKCS\#1 (RFC 8017)
1080 * as RSAPrivateKey.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001081 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
Gilles Peskine971f7062018-03-20 17:52:58 +01001082 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001083 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001084 * \param key Slot whose content is to be exported. This must
1085 * be an occupied key slot.
1086 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001087 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001088 * \param[out] data_length On success, the number of bytes
1089 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001090 *
Gilles Peskine28538492018-07-11 17:34:00 +02001091 * \retval #PSA_SUCCESS
1092 * \retval #PSA_ERROR_EMPTY_SLOT
1093 * \retval #PSA_ERROR_NOT_PERMITTED
1094 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1095 * \retval #PSA_ERROR_HARDWARE_FAILURE
1096 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001097 */
1098psa_status_t psa_export_key(psa_key_slot_t key,
1099 uint8_t *data,
1100 size_t data_size,
1101 size_t *data_length);
1102
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001103/**
1104 * \brief Export a public key or the public part of a key pair in binary format.
1105 *
1106 * The output of this function can be passed to psa_import_key() to
1107 * create an object that is equivalent to the public key.
1108 *
1109 * For standard key types, the output format is as follows:
1110 *
1111 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
Moran Pekerdd4ea382018-04-03 15:30:03 +03001112 * the format is the DER representation of the public key defined by RFC 5280
Gilles Peskine971f7062018-03-20 17:52:58 +01001113 * as SubjectPublicKeyInfo.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001114 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001115 * \param key Slot whose content is to be exported. This must
1116 * be an occupied key slot.
1117 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001118 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001119 * \param[out] data_length On success, the number of bytes
1120 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001121 *
Gilles Peskine28538492018-07-11 17:34:00 +02001122 * \retval #PSA_SUCCESS
1123 * \retval #PSA_ERROR_EMPTY_SLOT
1124 * \retval #PSA_ERROR_INVALID_ARGUMENT
1125 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1126 * \retval #PSA_ERROR_HARDWARE_FAILURE
1127 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001128 */
1129psa_status_t psa_export_public_key(psa_key_slot_t key,
1130 uint8_t *data,
1131 size_t data_size,
1132 size_t *data_length);
1133
1134/**@}*/
1135
1136/** \defgroup policy Key policies
1137 * @{
1138 */
1139
1140/** \brief Encoding of permitted usage on a key. */
1141typedef uint32_t psa_key_usage_t;
1142
Gilles Peskine7e198532018-03-08 07:50:30 +01001143/** Whether the key may be exported.
1144 *
1145 * A public key or the public part of a key pair may always be exported
1146 * regardless of the value of this permission flag.
1147 *
1148 * If a key does not have export permission, implementations shall not
1149 * allow the key to be exported in plain form from the cryptoprocessor,
1150 * whether through psa_export_key() or through a proprietary interface.
1151 * The key may however be exportable in a wrapped form, i.e. in a form
1152 * where it is encrypted by another key.
1153 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001154#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1155
Gilles Peskine7e198532018-03-08 07:50:30 +01001156/** Whether the key may be used to encrypt a message.
1157 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001158 * This flag allows the key to be used for a symmetric encryption operation,
1159 * for an AEAD encryption-and-authentication operation,
1160 * or for an asymmetric encryption operation,
1161 * if otherwise permitted by the key's type and policy.
1162 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001163 * For a key pair, this concerns the public key.
1164 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001165#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +01001166
1167/** Whether the key may be used to decrypt a message.
1168 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001169 * This flag allows the key to be used for a symmetric decryption operation,
1170 * for an AEAD decryption-and-verification operation,
1171 * or for an asymmetric decryption operation,
1172 * if otherwise permitted by the key's type and policy.
1173 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001174 * For a key pair, this concerns the private key.
1175 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001176#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +01001177
1178/** Whether the key may be used to sign a message.
1179 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001180 * This flag allows the key to be used for a MAC calculation operation
1181 * or for an asymmetric signature operation,
1182 * if otherwise permitted by the key's type and policy.
1183 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001184 * For a key pair, this concerns the private key.
1185 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001186#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +01001187
1188/** Whether the key may be used to verify a message signature.
1189 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001190 * This flag allows the key to be used for a MAC verification operation
1191 * or for an asymmetric signature verification operation,
1192 * if otherwise permitted by by the key's type and policy.
1193 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001194 * For a key pair, this concerns the public key.
1195 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001196#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
1197
1198/** The type of the key policy data structure.
1199 *
1200 * This is an implementation-defined \c struct. Applications should not
1201 * make any assumptions about the content of this structure except
1202 * as directed by the documentation of a specific implementation. */
1203typedef struct psa_key_policy_s psa_key_policy_t;
1204
1205/** \brief Initialize a key policy structure to a default that forbids all
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001206 * usage of the key.
1207 *
1208 * \param[out] policy The policy object to initialize.
1209 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001210void psa_key_policy_init(psa_key_policy_t *policy);
1211
Gilles Peskine7e198532018-03-08 07:50:30 +01001212/** \brief Set the standard fields of a policy structure.
1213 *
1214 * Note that this function does not make any consistency check of the
1215 * parameters. The values are only checked when applying the policy to
1216 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001217 *
1218 * \param[out] policy The policy object to modify.
1219 * \param usage The permitted uses for the key.
1220 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +01001221 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001222void psa_key_policy_set_usage(psa_key_policy_t *policy,
1223 psa_key_usage_t usage,
1224 psa_algorithm_t alg);
1225
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001226/** \brief Retrieve the usage field of a policy structure.
1227 *
1228 * \param[in] policy The policy object to query.
1229 *
1230 * \return The permitted uses for a key with this policy.
1231 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001232psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001233
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001234/** \brief Retrieve the algorithm field of a policy structure.
1235 *
1236 * \param[in] policy The policy object to query.
1237 *
1238 * \return The permitted algorithm for a key with this policy.
1239 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001240psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001241
1242/** \brief Set the usage policy on a key slot.
1243 *
1244 * This function must be called on an empty key slot, before importing,
1245 * generating or creating a key in the slot. Changing the policy of an
1246 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +01001247 *
1248 * Implementations may set restrictions on supported key policies
1249 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001250 *
1251 * \param key The key slot whose policy is to be changed.
1252 * \param[in] policy The policy object to query.
1253 *
1254 * \retval #PSA_SUCCESS
1255 * \retval #PSA_ERROR_OCCUPIED_SLOT
1256 * \retval #PSA_ERROR_NOT_SUPPORTED
1257 * \retval #PSA_ERROR_INVALID_ARGUMENT
1258 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1259 * \retval #PSA_ERROR_HARDWARE_FAILURE
1260 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001261 */
1262psa_status_t psa_set_key_policy(psa_key_slot_t key,
1263 const psa_key_policy_t *policy);
1264
Gilles Peskine7e198532018-03-08 07:50:30 +01001265/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001266 *
1267 * \param key The key slot whose policy is being queried.
1268 * \param[out] policy On success, the key's policy.
1269 *
1270 * \retval #PSA_SUCCESS
1271 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1272 * \retval #PSA_ERROR_HARDWARE_FAILURE
1273 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7e198532018-03-08 07:50:30 +01001274 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001275psa_status_t psa_get_key_policy(psa_key_slot_t key,
1276 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +01001277
1278/**@}*/
1279
Gilles Peskine609b6a52018-03-03 21:31:50 +01001280/** \defgroup persistence Key lifetime
1281 * @{
1282 */
1283
1284/** Encoding of key lifetimes.
1285 */
1286typedef uint32_t psa_key_lifetime_t;
1287
1288/** A volatile key slot retains its content as long as the application is
1289 * running. It is guaranteed to be erased on a power reset.
1290 */
1291#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1292
1293/** A persistent key slot retains its content as long as it is not explicitly
1294 * destroyed.
1295 */
1296#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1297
1298/** A write-once key slot may not be modified once a key has been set.
1299 * It will retain its content as long as the device remains operational.
1300 */
1301#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
1302
Gilles Peskined393e182018-03-08 07:49:16 +01001303/** \brief Retrieve the lifetime of a key slot.
1304 *
1305 * The assignment of lifetimes to slots is implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001306 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001307 * \param key Slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001308 * \param[out] lifetime On success, the lifetime value.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001309 *
Gilles Peskine28538492018-07-11 17:34:00 +02001310 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001311 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001312 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603a7d245a2018-04-17 00:40:08 -07001313 * The key slot is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001314 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1315 * \retval #PSA_ERROR_HARDWARE_FAILURE
1316 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +01001317 */
Gilles Peskine609b6a52018-03-03 21:31:50 +01001318psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
1319 psa_key_lifetime_t *lifetime);
1320
Gilles Peskined393e182018-03-08 07:49:16 +01001321/** \brief Change the lifetime of a key slot.
1322 *
1323 * Whether the lifetime of a key slot can be changed at all, and if so
Gilles Peskine19067982018-03-20 17:54:53 +01001324 * whether the lifetime of an occupied key slot can be changed, is
Gilles Peskined393e182018-03-08 07:49:16 +01001325 * implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001326 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001327 * \param key Slot whose lifetime is to be changed.
1328 * \param lifetime The lifetime value to set for the given key slot.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001329 *
Gilles Peskine28538492018-07-11 17:34:00 +02001330 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001331 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001332 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603804cd712018-03-20 22:44:08 +02001333 * The key slot is invalid,
mohammad1603a7d245a2018-04-17 00:40:08 -07001334 * or the lifetime value is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001335 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001336 * The implementation does not support the specified lifetime value,
1337 * at least for the specified key slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001338 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001339 * The slot contains a key, and the implementation does not support
1340 * changing the lifetime of an occupied slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001341 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1342 * \retval #PSA_ERROR_HARDWARE_FAILURE
1343 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +01001344 */
1345psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
mohammad1603ea050092018-04-17 00:31:34 -07001346 psa_key_lifetime_t lifetime);
Gilles Peskined393e182018-03-08 07:49:16 +01001347
Gilles Peskine609b6a52018-03-03 21:31:50 +01001348/**@}*/
1349
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001350/** \defgroup hash Message digests
1351 * @{
1352 */
1353
Gilles Peskine308b91d2018-02-08 09:47:44 +01001354/** The type of the state data structure for multipart hash operations.
1355 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001356 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01001357 * make any assumptions about the content of this structure except
1358 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001359typedef struct psa_hash_operation_s psa_hash_operation_t;
1360
Gilles Peskine308b91d2018-02-08 09:47:44 +01001361/** The size of the output of psa_hash_finish(), in bytes.
1362 *
1363 * This is also the hash size that psa_hash_verify() expects.
1364 *
1365 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001366 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
Gilles Peskinebe42f312018-07-13 14:38:15 +02001367 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
Gilles Peskine35855962018-04-19 08:39:16 +02001368 * hash algorithm).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001369 *
1370 * \return The hash size for the specified hash algorithm.
1371 * If the hash algorithm is not recognized, return 0.
1372 * An implementation may return either 0 or the correct size
1373 * for a hash algorithm that it recognizes, but does not support.
1374 */
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001375#define PSA_HASH_SIZE(alg) \
1376 ( \
1377 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD2 ? 16 : \
1378 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD4 ? 16 : \
1379 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD5 ? 16 : \
1380 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
1381 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
1382 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
1383 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
1384 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
1385 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
1386 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
1387 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
1388 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
1389 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
1390 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
1391 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001392 0)
1393
Gilles Peskine308b91d2018-02-08 09:47:44 +01001394/** Start a multipart hash operation.
1395 *
1396 * The sequence of operations to calculate a hash (message digest)
1397 * is as follows:
1398 * -# Allocate an operation object which will be passed to all the functions
1399 * listed here.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001400 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001401 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001402 * of the message each time. The hash that is calculated is the hash
1403 * of the concatenation of these messages in order.
1404 * -# To calculate the hash, call psa_hash_finish().
1405 * To compare the hash with an expected value, call psa_hash_verify().
1406 *
1407 * The application may call psa_hash_abort() at any time after the operation
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001408 * has been initialized with psa_hash_setup().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001409 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001410 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001411 * eventually terminate the operation. The following events terminate an
1412 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001413 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001414 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001415 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001416 * \param[out] operation The operation object to use.
1417 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1418 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001419 *
Gilles Peskine28538492018-07-11 17:34:00 +02001420 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001421 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001422 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001423 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001424 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1425 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1426 * \retval #PSA_ERROR_HARDWARE_FAILURE
1427 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001428 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001429psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001430 psa_algorithm_t alg);
1431
Gilles Peskine308b91d2018-02-08 09:47:44 +01001432/** Add a message fragment to a multipart hash operation.
1433 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001434 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001435 *
1436 * If this function returns an error status, the operation becomes inactive.
1437 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001438 * \param[in,out] operation Active hash operation.
1439 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001440 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001441 *
Gilles Peskine28538492018-07-11 17:34:00 +02001442 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001443 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001444 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001445 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001446 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1447 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1448 * \retval #PSA_ERROR_HARDWARE_FAILURE
1449 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001450 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001451psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1452 const uint8_t *input,
1453 size_t input_length);
1454
Gilles Peskine308b91d2018-02-08 09:47:44 +01001455/** Finish the calculation of the hash of a message.
1456 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001457 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001458 * This function calculates the hash of the message formed by concatenating
1459 * the inputs passed to preceding calls to psa_hash_update().
1460 *
1461 * When this function returns, the operation becomes inactive.
1462 *
1463 * \warning Applications should not call this function if they expect
1464 * a specific value for the hash. Call psa_hash_verify() instead.
1465 * Beware that comparing integrity or authenticity data such as
1466 * hash values with a function such as \c memcmp is risky
1467 * because the time taken by the comparison may leak information
1468 * about the hashed data which could allow an attacker to guess
1469 * a valid hash and thereby bypass security controls.
1470 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001471 * \param[in,out] operation Active hash operation.
1472 * \param[out] hash Buffer where the hash is to be written.
1473 * \param hash_size Size of the \p hash buffer in bytes.
1474 * \param[out] hash_length On success, the number of bytes
1475 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001476 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001477 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001478 *
Gilles Peskine28538492018-07-11 17:34:00 +02001479 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001480 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001481 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001482 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001483 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001484 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001485 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001486 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001487 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1488 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1489 * \retval #PSA_ERROR_HARDWARE_FAILURE
1490 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001491 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001492psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1493 uint8_t *hash,
1494 size_t hash_size,
1495 size_t *hash_length);
1496
Gilles Peskine308b91d2018-02-08 09:47:44 +01001497/** Finish the calculation of the hash of a message and compare it with
1498 * an expected value.
1499 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001500 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001501 * This function calculates the hash of the message formed by concatenating
1502 * the inputs passed to preceding calls to psa_hash_update(). It then
1503 * compares the calculated hash with the expected hash passed as a
1504 * parameter to this function.
1505 *
1506 * When this function returns, the operation becomes inactive.
1507 *
Gilles Peskine19067982018-03-20 17:54:53 +01001508 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001509 * comparison between the actual hash and the expected hash is performed
1510 * in constant time.
1511 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001512 * \param[in,out] operation Active hash operation.
1513 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001514 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001515 *
Gilles Peskine28538492018-07-11 17:34:00 +02001516 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001517 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001518 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001519 * The hash of the message was calculated successfully, but it
1520 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001521 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001522 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001523 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1524 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1525 * \retval #PSA_ERROR_HARDWARE_FAILURE
1526 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001527 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001528psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1529 const uint8_t *hash,
1530 size_t hash_length);
1531
Gilles Peskine308b91d2018-02-08 09:47:44 +01001532/** Abort a hash operation.
1533 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001534 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001535 * \p operation structure itself. Once aborted, the operation object
1536 * can be reused for another operation by calling
1537 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001538 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001539 * You may call this function any time after the operation object has
1540 * been initialized by any of the following methods:
1541 * - A call to psa_hash_setup(), whether it succeeds or not.
1542 * - Initializing the \c struct to all-bits-zero.
1543 * - Initializing the \c struct to logical zeros, e.g.
1544 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001545 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001546 * In particular, calling psa_hash_abort() after the operation has been
1547 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1548 * psa_hash_verify() is safe and has no effect.
1549 *
1550 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001551 *
Gilles Peskine28538492018-07-11 17:34:00 +02001552 * \retval #PSA_SUCCESS
1553 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001554 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001555 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1556 * \retval #PSA_ERROR_HARDWARE_FAILURE
1557 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001558 */
1559psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001560
1561/**@}*/
1562
Gilles Peskine8c9def32018-02-08 10:02:12 +01001563/** \defgroup MAC Message authentication codes
1564 * @{
1565 */
1566
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001567/** The type of the state data structure for multipart MAC operations.
1568 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001569 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001570 * make any assumptions about the content of this structure except
1571 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001572typedef struct psa_mac_operation_s psa_mac_operation_t;
1573
Gilles Peskine89167cb2018-07-08 20:12:23 +02001574/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001575 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001576 * This function sets up the calculation of the MAC
1577 * (message authentication code) of a byte string.
1578 * To verify the MAC of a message against an
1579 * expected value, use psa_mac_verify_setup() instead.
1580 *
1581 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001582 * -# Allocate an operation object which will be passed to all the functions
1583 * listed here.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001584 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001585 * The key remains associated with the operation even if the content
1586 * of the key slot changes.
1587 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1588 * of the message each time. The MAC that is calculated is the MAC
1589 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001590 * -# At the end of the message, call psa_mac_sign_finish() to finish
1591 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001592 *
1593 * The application may call psa_mac_abort() at any time after the operation
Gilles Peskine89167cb2018-07-08 20:12:23 +02001594 * has been initialized with psa_mac_sign_setup().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001595 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001596 * After a successful call to psa_mac_sign_setup(), the application must
1597 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001598 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001599 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001600 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001601 * \param[out] operation The operation object to use.
1602 * \param key Slot containing the key to use for the operation.
1603 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1604 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001605 *
Gilles Peskine28538492018-07-11 17:34:00 +02001606 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001607 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001608 * \retval #PSA_ERROR_EMPTY_SLOT
1609 * \retval #PSA_ERROR_NOT_PERMITTED
1610 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001611 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001612 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001613 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001614 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1615 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1616 * \retval #PSA_ERROR_HARDWARE_FAILURE
1617 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001618 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001619psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
1620 psa_key_slot_t key,
1621 psa_algorithm_t alg);
1622
1623/** Start a multipart MAC verification operation.
1624 *
1625 * This function sets up the verification of the MAC
1626 * (message authentication code) of a byte string against an expected value.
1627 *
1628 * The sequence of operations to verify a MAC is as follows:
1629 * -# Allocate an operation object which will be passed to all the functions
1630 * listed here.
1631 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1632 * The key remains associated with the operation even if the content
1633 * of the key slot changes.
1634 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1635 * of the message each time. The MAC that is calculated is the MAC
1636 * of the concatenation of these messages in order.
1637 * -# At the end of the message, call psa_mac_verify_finish() to finish
1638 * calculating the actual MAC of the message and verify it against
1639 * the expected value.
1640 *
1641 * The application may call psa_mac_abort() at any time after the operation
1642 * has been initialized with psa_mac_verify_setup().
1643 *
1644 * After a successful call to psa_mac_verify_setup(), the application must
1645 * eventually terminate the operation through one of the following methods:
1646 * - A failed call to psa_mac_update().
1647 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1648 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001649 * \param[out] operation The operation object to use.
1650 * \param key Slot containing the key to use for the operation.
1651 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1652 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001653 *
Gilles Peskine28538492018-07-11 17:34:00 +02001654 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001655 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001656 * \retval #PSA_ERROR_EMPTY_SLOT
1657 * \retval #PSA_ERROR_NOT_PERMITTED
1658 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001659 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001660 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001661 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001662 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1663 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1664 * \retval #PSA_ERROR_HARDWARE_FAILURE
1665 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001666 */
1667psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
1668 psa_key_slot_t key,
1669 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001670
Gilles Peskinedcd14942018-07-12 00:30:52 +02001671/** Add a message fragment to a multipart MAC operation.
1672 *
1673 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1674 * before calling this function.
1675 *
1676 * If this function returns an error status, the operation becomes inactive.
1677 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001678 * \param[in,out] operation Active MAC operation.
1679 * \param[in] input Buffer containing the message fragment to add to
1680 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001681 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001682 *
1683 * \retval #PSA_SUCCESS
1684 * Success.
1685 * \retval #PSA_ERROR_BAD_STATE
1686 * The operation state is not valid (not started, or already completed).
1687 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1688 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1689 * \retval #PSA_ERROR_HARDWARE_FAILURE
1690 * \retval #PSA_ERROR_TAMPERING_DETECTED
1691 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001692psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1693 const uint8_t *input,
1694 size_t input_length);
1695
Gilles Peskinedcd14942018-07-12 00:30:52 +02001696/** Finish the calculation of the MAC of a message.
1697 *
1698 * The application must call psa_mac_sign_setup() before calling this function.
1699 * This function calculates the MAC of the message formed by concatenating
1700 * the inputs passed to preceding calls to psa_mac_update().
1701 *
1702 * When this function returns, the operation becomes inactive.
1703 *
1704 * \warning Applications should not call this function if they expect
1705 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1706 * Beware that comparing integrity or authenticity data such as
1707 * MAC values with a function such as \c memcmp is risky
1708 * because the time taken by the comparison may leak information
1709 * about the MAC value which could allow an attacker to guess
1710 * a valid MAC and thereby bypass security controls.
1711 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001712 * \param[in,out] operation Active MAC operation.
1713 * \param[out] mac Buffer where the MAC value is to be written.
1714 * \param mac_size Size of the \p mac buffer in bytes.
1715 * \param[out] mac_length On success, the number of bytes
1716 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001717 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001718 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001719 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001720 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001721 *
1722 * \retval #PSA_SUCCESS
1723 * Success.
1724 * \retval #PSA_ERROR_BAD_STATE
1725 * The operation state is not valid (not started, or already completed).
1726 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001727 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001728 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1729 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1730 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1731 * \retval #PSA_ERROR_HARDWARE_FAILURE
1732 * \retval #PSA_ERROR_TAMPERING_DETECTED
1733 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001734psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1735 uint8_t *mac,
1736 size_t mac_size,
1737 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001738
Gilles Peskinedcd14942018-07-12 00:30:52 +02001739/** Finish the calculation of the MAC of a message and compare it with
1740 * an expected value.
1741 *
1742 * The application must call psa_mac_verify_setup() before calling this function.
1743 * This function calculates the MAC of the message formed by concatenating
1744 * the inputs passed to preceding calls to psa_mac_update(). It then
1745 * compares the calculated MAC with the expected MAC passed as a
1746 * parameter to this function.
1747 *
1748 * When this function returns, the operation becomes inactive.
1749 *
1750 * \note Implementations shall make the best effort to ensure that the
1751 * comparison between the actual MAC and the expected MAC is performed
1752 * in constant time.
1753 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001754 * \param[in,out] operation Active MAC operation.
1755 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001756 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001757 *
1758 * \retval #PSA_SUCCESS
1759 * The expected MAC is identical to the actual MAC of the message.
1760 * \retval #PSA_ERROR_INVALID_SIGNATURE
1761 * The MAC of the message was calculated successfully, but it
1762 * differs from the expected MAC.
1763 * \retval #PSA_ERROR_BAD_STATE
1764 * The operation state is not valid (not started, or already completed).
1765 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1766 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1767 * \retval #PSA_ERROR_HARDWARE_FAILURE
1768 * \retval #PSA_ERROR_TAMPERING_DETECTED
1769 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001770psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1771 const uint8_t *mac,
1772 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001773
Gilles Peskinedcd14942018-07-12 00:30:52 +02001774/** Abort a MAC operation.
1775 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001776 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001777 * \p operation structure itself. Once aborted, the operation object
1778 * can be reused for another operation by calling
1779 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001780 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001781 * You may call this function any time after the operation object has
1782 * been initialized by any of the following methods:
1783 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1784 * it succeeds or not.
1785 * - Initializing the \c struct to all-bits-zero.
1786 * - Initializing the \c struct to logical zeros, e.g.
1787 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001788 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001789 * In particular, calling psa_mac_abort() after the operation has been
1790 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1791 * psa_mac_verify_finish() is safe and has no effect.
1792 *
1793 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001794 *
1795 * \retval #PSA_SUCCESS
1796 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001797 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001798 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1799 * \retval #PSA_ERROR_HARDWARE_FAILURE
1800 * \retval #PSA_ERROR_TAMPERING_DETECTED
1801 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001802psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1803
1804/**@}*/
1805
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001806/** \defgroup cipher Symmetric ciphers
1807 * @{
1808 */
1809
1810/** The type of the state data structure for multipart cipher operations.
1811 *
1812 * This is an implementation-defined \c struct. Applications should not
1813 * make any assumptions about the content of this structure except
1814 * as directed by the documentation of a specific implementation. */
1815typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1816
1817/** Set the key for a multipart symmetric encryption operation.
1818 *
1819 * The sequence of operations to encrypt a message with a symmetric cipher
1820 * is as follows:
1821 * -# Allocate an operation object which will be passed to all the functions
1822 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02001823 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001824 * The key remains associated with the operation even if the content
1825 * of the key slot changes.
Gilles Peskinefe119512018-07-08 21:39:34 +02001826 * -# Call either psa_encrypt_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001827 * generate or set the IV (initialization vector). You should use
1828 * psa_encrypt_generate_iv() unless the protocol you are implementing
1829 * requires a specific IV value.
1830 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1831 * of the message each time.
1832 * -# Call psa_cipher_finish().
1833 *
1834 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02001835 * has been initialized with psa_cipher_encrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001836 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001837 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001838 * eventually terminate the operation. The following events terminate an
1839 * operation:
Gilles Peskinefe119512018-07-08 21:39:34 +02001840 * - A failed call to psa_encrypt_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001841 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001842 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001843 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001844 * \param[out] operation The operation object to use.
1845 * \param key Slot containing the key to use for the operation.
1846 * \param alg The cipher algorithm to compute
1847 * (\c PSA_ALG_XXX value such that
1848 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001849 *
Gilles Peskine28538492018-07-11 17:34:00 +02001850 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001851 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001852 * \retval #PSA_ERROR_EMPTY_SLOT
1853 * \retval #PSA_ERROR_NOT_PERMITTED
1854 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001855 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001856 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001857 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001858 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1859 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1860 * \retval #PSA_ERROR_HARDWARE_FAILURE
1861 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001862 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001863psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
1864 psa_key_slot_t key,
1865 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001866
1867/** Set the key for a multipart symmetric decryption operation.
1868 *
1869 * The sequence of operations to decrypt a message with a symmetric cipher
1870 * is as follows:
1871 * -# Allocate an operation object which will be passed to all the functions
1872 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02001873 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001874 * The key remains associated with the operation even if the content
1875 * of the key slot changes.
1876 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1877 * decryption. If the IV is prepended to the ciphertext, you can call
1878 * psa_cipher_update() on a buffer containing the IV followed by the
1879 * beginning of the message.
1880 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1881 * of the message each time.
1882 * -# Call psa_cipher_finish().
1883 *
1884 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02001885 * has been initialized with psa_cipher_decrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001886 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001887 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001888 * eventually terminate the operation. The following events terminate an
1889 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001890 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001891 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001892 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001893 * \param[out] operation The operation object to use.
1894 * \param key Slot containing the key to use for the operation.
1895 * \param alg The cipher algorithm to compute
1896 * (\c PSA_ALG_XXX value such that
1897 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001898 *
Gilles Peskine28538492018-07-11 17:34:00 +02001899 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001900 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001901 * \retval #PSA_ERROR_EMPTY_SLOT
1902 * \retval #PSA_ERROR_NOT_PERMITTED
1903 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001904 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001905 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001906 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001907 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1908 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1909 * \retval #PSA_ERROR_HARDWARE_FAILURE
1910 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001911 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001912psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
1913 psa_key_slot_t key,
1914 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001915
Gilles Peskinedcd14942018-07-12 00:30:52 +02001916/** Generate an IV for a symmetric encryption operation.
1917 *
1918 * This function generates a random IV (initialization vector), nonce
1919 * or initial counter value for the encryption operation as appropriate
1920 * for the chosen algorithm, key type and key size.
1921 *
1922 * The application must call psa_cipher_encrypt_setup() before
1923 * calling this function.
1924 *
1925 * If this function returns an error status, the operation becomes inactive.
1926 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001927 * \param[in,out] operation Active cipher operation.
1928 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001929 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001930 * \param[out] iv_length On success, the number of bytes of the
1931 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001932 *
1933 * \retval #PSA_SUCCESS
1934 * Success.
1935 * \retval #PSA_ERROR_BAD_STATE
1936 * The operation state is not valid (not started, or IV already set).
1937 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001938 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001939 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1940 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1941 * \retval #PSA_ERROR_HARDWARE_FAILURE
1942 * \retval #PSA_ERROR_TAMPERING_DETECTED
1943 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001944psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1945 unsigned char *iv,
1946 size_t iv_size,
1947 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001948
Gilles Peskinedcd14942018-07-12 00:30:52 +02001949/** Set the IV for a symmetric encryption or decryption operation.
1950 *
1951 * This function sets the random IV (initialization vector), nonce
1952 * or initial counter value for the encryption or decryption operation.
1953 *
1954 * The application must call psa_cipher_encrypt_setup() before
1955 * calling this function.
1956 *
1957 * If this function returns an error status, the operation becomes inactive.
1958 *
1959 * \note When encrypting, applications should use psa_cipher_generate_iv()
1960 * instead of this function, unless implementing a protocol that requires
1961 * a non-random IV.
1962 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001963 * \param[in,out] operation Active cipher operation.
1964 * \param[in] iv Buffer containing the IV to use.
1965 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001966 *
1967 * \retval #PSA_SUCCESS
1968 * Success.
1969 * \retval #PSA_ERROR_BAD_STATE
1970 * The operation state is not valid (not started, or IV already set).
1971 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001972 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001973 * or the chosen algorithm does not use an IV.
1974 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1975 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1976 * \retval #PSA_ERROR_HARDWARE_FAILURE
1977 * \retval #PSA_ERROR_TAMPERING_DETECTED
1978 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001979psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1980 const unsigned char *iv,
1981 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001982
Gilles Peskinedcd14942018-07-12 00:30:52 +02001983/** Encrypt or decrypt a message fragment in an active cipher operation.
1984 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001985 * Before calling this function, you must:
1986 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1987 * The choice of setup function determines whether this function
1988 * encrypts or decrypts its input.
1989 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1990 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001991 *
1992 * If this function returns an error status, the operation becomes inactive.
1993 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001994 * \param[in,out] operation Active cipher operation.
1995 * \param[in] input Buffer containing the message fragment to
1996 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001997 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001998 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001999 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002000 * \param[out] output_length On success, the number of bytes
2001 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002002 *
2003 * \retval #PSA_SUCCESS
2004 * Success.
2005 * \retval #PSA_ERROR_BAD_STATE
2006 * The operation state is not valid (not started, IV required but
2007 * not set, or already completed).
2008 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2009 * The size of the \p output buffer is too small.
2010 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2011 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2012 * \retval #PSA_ERROR_HARDWARE_FAILURE
2013 * \retval #PSA_ERROR_TAMPERING_DETECTED
2014 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002015psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2016 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02002017 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002018 unsigned char *output,
2019 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002020 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002021
Gilles Peskinedcd14942018-07-12 00:30:52 +02002022/** Finish encrypting or decrypting a message in a cipher operation.
2023 *
2024 * The application must call psa_cipher_encrypt_setup() or
2025 * psa_cipher_decrypt_setup() before calling this function. The choice
2026 * of setup function determines whether this function encrypts or
2027 * decrypts its input.
2028 *
2029 * This function finishes the encryption or decryption of the message
2030 * formed by concatenating the inputs passed to preceding calls to
2031 * psa_cipher_update().
2032 *
2033 * When this function returns, the operation becomes inactive.
2034 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002035 * \param[in,out] operation Active cipher operation.
2036 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002037 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002038 * \param[out] output_length On success, the number of bytes
2039 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002040 *
2041 * \retval #PSA_SUCCESS
2042 * Success.
2043 * \retval #PSA_ERROR_BAD_STATE
2044 * The operation state is not valid (not started, IV required but
2045 * not set, or already completed).
2046 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2047 * The size of the \p output buffer is too small.
2048 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2049 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2050 * \retval #PSA_ERROR_HARDWARE_FAILURE
2051 * \retval #PSA_ERROR_TAMPERING_DETECTED
2052 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002053psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002054 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002055 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002056 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002057
Gilles Peskinedcd14942018-07-12 00:30:52 +02002058/** Abort a cipher operation.
2059 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002060 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002061 * \p operation structure itself. Once aborted, the operation object
2062 * can be reused for another operation by calling
2063 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002064 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002065 * You may call this function any time after the operation object has
2066 * been initialized by any of the following methods:
2067 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2068 * whether it succeeds or not.
2069 * - Initializing the \c struct to all-bits-zero.
2070 * - Initializing the \c struct to logical zeros, e.g.
2071 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002072 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002073 * In particular, calling psa_cipher_abort() after the operation has been
2074 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2075 * is safe and has no effect.
2076 *
2077 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002078 *
2079 * \retval #PSA_SUCCESS
2080 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002081 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002082 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2083 * \retval #PSA_ERROR_HARDWARE_FAILURE
2084 * \retval #PSA_ERROR_TAMPERING_DETECTED
2085 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002086psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2087
2088/**@}*/
2089
Gilles Peskine3b555712018-03-03 21:27:57 +01002090/** \defgroup aead Authenticated encryption with associated data (AEAD)
2091 * @{
2092 */
2093
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002094/** The tag size for an AEAD algorithm, in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01002095 *
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002096 * \param alg An AEAD algorithm
2097 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002098 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002099 *
2100 * \return The tag size for the specified algorithm.
2101 * If the AEAD algorithm does not have an identified
2102 * tag that can be distinguished from the rest of
2103 * the ciphertext, return 0.
2104 * If the AEAD algorithm is not recognized, return 0.
2105 * An implementation may return either 0 or a
2106 * correct size for an AEAD algorithm that it
2107 * recognizes, but does not support.
2108 */
2109#define PSA_AEAD_TAG_SIZE(alg) \
2110 ((alg) == PSA_ALG_GCM ? 16 : \
2111 (alg) == PSA_ALG_CCM ? 16 : \
2112 0)
Gilles Peskine3b555712018-03-03 21:27:57 +01002113
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002114/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002115 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002116 * \param key Slot containing the key to use.
2117 * \param alg The AEAD algorithm to compute
2118 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002119 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002120 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002121 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002122 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002123 * but not encrypted.
2124 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002125 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002126 * encrypted.
2127 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002128 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002129 * encrypted data. The additional data is not
2130 * part of this output. For algorithms where the
2131 * encrypted data and the authentication tag
2132 * are defined as separate outputs, the
2133 * authentication tag is appended to the
2134 * encrypted data.
2135 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2136 * This must be at least
2137 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2138 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002139 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002140 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002141 *
Gilles Peskine28538492018-07-11 17:34:00 +02002142 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002143 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002144 * \retval #PSA_ERROR_EMPTY_SLOT
2145 * \retval #PSA_ERROR_NOT_PERMITTED
2146 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002147 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002148 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002149 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002150 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2151 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2152 * \retval #PSA_ERROR_HARDWARE_FAILURE
2153 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine3b555712018-03-03 21:27:57 +01002154 */
mohammad160339ee8712018-04-26 00:51:02 +03002155psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2156 psa_algorithm_t alg,
2157 const uint8_t *nonce,
2158 size_t nonce_length,
2159 const uint8_t *additional_data,
2160 size_t additional_data_length,
2161 const uint8_t *plaintext,
2162 size_t plaintext_length,
2163 uint8_t *ciphertext,
2164 size_t ciphertext_size,
2165 size_t *ciphertext_length );
Gilles Peskine3b555712018-03-03 21:27:57 +01002166
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002167/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002168 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002169 * \param key Slot containing the key to use.
2170 * \param alg The AEAD algorithm to compute
2171 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002172 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002173 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002174 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002175 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002176 * but not encrypted.
2177 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002178 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002179 * encrypted. For algorithms where the
2180 * encrypted data and the authentication tag
2181 * are defined as separate inputs, the buffer
2182 * must contain the encrypted data followed
2183 * by the authentication tag.
2184 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002185 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002186 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2187 * This must be at least
2188 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2189 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002190 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03002191 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002192 *
Gilles Peskine28538492018-07-11 17:34:00 +02002193 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002194 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002195 * \retval #PSA_ERROR_EMPTY_SLOT
2196 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002197 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002198 * \retval #PSA_ERROR_NOT_PERMITTED
2199 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002200 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002201 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002202 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002203 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2204 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2205 * \retval #PSA_ERROR_HARDWARE_FAILURE
2206 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine3b555712018-03-03 21:27:57 +01002207 */
mohammad160339ee8712018-04-26 00:51:02 +03002208psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2209 psa_algorithm_t alg,
2210 const uint8_t *nonce,
2211 size_t nonce_length,
2212 const uint8_t *additional_data,
2213 size_t additional_data_length,
2214 const uint8_t *ciphertext,
2215 size_t ciphertext_length,
2216 uint8_t *plaintext,
2217 size_t plaintext_size,
2218 size_t *plaintext_length );
Gilles Peskine3b555712018-03-03 21:27:57 +01002219
2220/**@}*/
2221
Gilles Peskine20035e32018-02-03 22:44:14 +01002222/** \defgroup asymmetric Asymmetric cryptography
2223 * @{
2224 */
2225
2226/**
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002227 * \brief ECDSA signature size for a given curve bit size
Gilles Peskine0189e752018-02-03 23:57:22 +01002228 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002229 * \param curve_bits Curve size in bits.
2230 * \return Signature size in bytes.
Gilles Peskine0189e752018-02-03 23:57:22 +01002231 *
2232 * \note This macro returns a compile-time constant if its argument is one.
Gilles Peskine0189e752018-02-03 23:57:22 +01002233 */
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002234#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
2235 (PSA_BITS_TO_BYTES(curve_bits) * 2)
Gilles Peskine0189e752018-02-03 23:57:22 +01002236
Gilles Peskine0189e752018-02-03 23:57:22 +01002237/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002238 * \brief Sign a hash or short message with a private key.
2239 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002240 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002241 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002242 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2243 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2244 * to determine the hash algorithm to use.
2245 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002246 * \param key Key slot containing an asymmetric key pair.
2247 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002248 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002249 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002250 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002251 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002252 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002253 * \param[out] signature_length On success, the number of bytes
2254 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002255 *
Gilles Peskine28538492018-07-11 17:34:00 +02002256 * \retval #PSA_SUCCESS
2257 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002258 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002259 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002260 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002261 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002262 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002263 * \retval #PSA_ERROR_NOT_SUPPORTED
2264 * \retval #PSA_ERROR_INVALID_ARGUMENT
2265 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2266 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2267 * \retval #PSA_ERROR_HARDWARE_FAILURE
2268 * \retval #PSA_ERROR_TAMPERING_DETECTED
2269 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01002270 */
2271psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
2272 psa_algorithm_t alg,
2273 const uint8_t *hash,
2274 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002275 uint8_t *signature,
2276 size_t signature_size,
2277 size_t *signature_length);
2278
2279/**
2280 * \brief Verify the signature a hash or short message using a public key.
2281 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002282 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002283 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002284 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2285 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2286 * to determine the hash algorithm to use.
2287 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01002288 * \param key Key slot containing a public key or an
2289 * asymmetric key pair.
2290 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002291 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002292 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002293 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002294 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002295 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002296 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002297 *
Gilles Peskine28538492018-07-11 17:34:00 +02002298 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002299 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002300 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002301 * The calculation was perfomed successfully, but the passed
2302 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002303 * \retval #PSA_ERROR_NOT_SUPPORTED
2304 * \retval #PSA_ERROR_INVALID_ARGUMENT
2305 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2306 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2307 * \retval #PSA_ERROR_HARDWARE_FAILURE
2308 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01002309 */
2310psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
2311 psa_algorithm_t alg,
2312 const uint8_t *hash,
2313 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002314 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002315 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002316
Gilles Peskine723feff2018-05-31 20:08:13 +02002317#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
Gilles Peskine072ac562018-06-30 00:21:29 +02002318 (PSA_ALG_IS_RSA_OAEP(alg) ? \
2319 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskine723feff2018-05-31 20:08:13 +02002320 11 /*PKCS#1v1.5*/)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002321
2322/**
2323 * \brief Encrypt a short message with a public key.
2324 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002325 * \param key Key slot containing a public key or an
2326 * asymmetric key pair.
2327 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002328 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002329 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002330 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002331 * \param[in] salt A salt or label, if supported by the
2332 * encryption algorithm.
2333 * If the algorithm does not support a
2334 * salt, pass \c NULL.
2335 * If the algorithm supports an optional
2336 * salt and you do not want to pass a salt,
2337 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002338 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002339 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2340 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002341 * \param salt_length Size of the \p salt buffer in bytes.
2342 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002343 * \param[out] output Buffer where the encrypted message is to
2344 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002345 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002346 * \param[out] output_length On success, the number of bytes
2347 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002348 *
Gilles Peskine28538492018-07-11 17:34:00 +02002349 * \retval #PSA_SUCCESS
2350 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002351 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002352 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002353 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002354 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002355 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002356 * \retval #PSA_ERROR_NOT_SUPPORTED
2357 * \retval #PSA_ERROR_INVALID_ARGUMENT
2358 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2359 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2360 * \retval #PSA_ERROR_HARDWARE_FAILURE
2361 * \retval #PSA_ERROR_TAMPERING_DETECTED
2362 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002363 */
2364psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
2365 psa_algorithm_t alg,
2366 const uint8_t *input,
2367 size_t input_length,
2368 const uint8_t *salt,
2369 size_t salt_length,
2370 uint8_t *output,
2371 size_t output_size,
2372 size_t *output_length);
2373
2374/**
2375 * \brief Decrypt a short message with a private key.
2376 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002377 * \param key Key slot containing an asymmetric key pair.
2378 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002379 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002380 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002381 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002382 * \param[in] salt A salt or label, if supported by the
2383 * encryption algorithm.
2384 * If the algorithm does not support a
2385 * salt, pass \c NULL.
2386 * If the algorithm supports an optional
2387 * salt and you do not want to pass a salt,
2388 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002389 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002390 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2391 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002392 * \param salt_length Size of the \p salt buffer in bytes.
2393 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002394 * \param[out] output Buffer where the decrypted message is to
2395 * be written.
2396 * \param output_size Size of the \c output buffer in bytes.
2397 * \param[out] output_length On success, the number of bytes
2398 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002399 *
Gilles Peskine28538492018-07-11 17:34:00 +02002400 * \retval #PSA_SUCCESS
2401 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002402 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002403 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002404 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002405 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002406 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002407 * \retval #PSA_ERROR_NOT_SUPPORTED
2408 * \retval #PSA_ERROR_INVALID_ARGUMENT
2409 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2410 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2411 * \retval #PSA_ERROR_HARDWARE_FAILURE
2412 * \retval #PSA_ERROR_TAMPERING_DETECTED
2413 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2414 * \retval #PSA_ERROR_INVALID_PADDING
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002415 */
2416psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
2417 psa_algorithm_t alg,
2418 const uint8_t *input,
2419 size_t input_length,
2420 const uint8_t *salt,
2421 size_t salt_length,
2422 uint8_t *output,
2423 size_t output_size,
2424 size_t *output_length);
2425
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002426/**@}*/
2427
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002428/** \defgroup generation Key generation
2429 * @{
2430 */
2431
2432/**
2433 * \brief Generate random bytes.
2434 *
2435 * \warning This function **can** fail! Callers MUST check the return status
2436 * and MUST NOT use the content of the output buffer if the return
2437 * status is not #PSA_SUCCESS.
2438 *
2439 * \note To generate a key, use psa_generate_key() instead.
2440 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002441 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002442 * \param output_size Number of bytes to generate and output.
2443 *
Gilles Peskine28538492018-07-11 17:34:00 +02002444 * \retval #PSA_SUCCESS
2445 * \retval #PSA_ERROR_NOT_SUPPORTED
2446 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2447 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2448 * \retval #PSA_ERROR_HARDWARE_FAILURE
2449 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002450 */
2451psa_status_t psa_generate_random(uint8_t *output,
2452 size_t output_size);
2453
Gilles Peskine4c317f42018-07-12 01:24:09 +02002454/** Extra parameters for RSA key generation.
2455 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002456 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002457 * parameter to psa_generate_key().
2458 */
2459typedef struct {
2460 uint32_t e; /**! Public exponent value. Default: 65537. */
2461} psa_generate_key_extra_rsa;
2462
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002463/**
2464 * \brief Generate a key or key pair.
2465 *
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002466 * \param key Slot where the key will be stored. This must be a
2467 * valid slot for a key of the chosen type. It must
2468 * be unoccupied.
2469 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2470 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002471 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002472 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002473 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002474 * default parameters. Implementation that support
2475 * the generation of vendor-specific key types
2476 * that allow extra parameters shall document
2477 * the format of these extra parameters and
2478 * the default values. For standard parameters,
2479 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002480 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002481 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2482 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002483 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002484 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2485 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002486 * - For an RSA key (\p type is
2487 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2488 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002489 * specifying the public exponent. The
2490 * default public exponent used when \p extra
2491 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002492 * \param extra_size Size of the buffer that \p extra
2493 * points to, in bytes. Note that if \p extra is
2494 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002495 *
Gilles Peskine28538492018-07-11 17:34:00 +02002496 * \retval #PSA_SUCCESS
2497 * \retval #PSA_ERROR_NOT_SUPPORTED
2498 * \retval #PSA_ERROR_INVALID_ARGUMENT
2499 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2500 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2501 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2502 * \retval #PSA_ERROR_HARDWARE_FAILURE
2503 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002504 */
2505psa_status_t psa_generate_key(psa_key_slot_t key,
2506 psa_key_type_t type,
2507 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002508 const void *extra,
2509 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002510
2511/**@}*/
2512
Gilles Peskinee59236f2018-01-27 23:32:46 +01002513#ifdef __cplusplus
2514}
2515#endif
2516
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002517/* The file "crypto_sizes.h" contains definitions for size calculation
2518 * macros whose definitions are implementation-specific. */
2519#include "crypto_sizes.h"
2520
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002521/* The file "crypto_struct.h" contains definitions for
2522 * implementation-specific structs that are declared above. */
2523#include "crypto_struct.h"
2524
2525/* The file "crypto_extra.h" contains vendor-specific definitions. This
2526 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002527#include "crypto_extra.h"
2528
2529#endif /* PSA_CRYPTO_H */