blob: f282aa20b66762ae9fd58d33c8122805ea68dd5a [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
Gilles Peskineeab56e42018-07-12 17:12:33 +0200288/** The generator has insufficient capacity left.
289 *
290 * Once a function returns this error, attempts to read from the
291 * generator will always return this error. */
292#define PSA_ERROR_INSUFFICIENT_CAPACITY ((psa_status_t)17)
293
itayzafrirc2a79762018-06-18 16:20:16 +0300294/** An error occurred that does not correspond to any defined
295 * failure cause.
296 *
297 * Implementations may use this error code if none of the other standard
298 * error codes are applicable. */
Gilles Peskineeab56e42018-07-12 17:12:33 +0200299#define PSA_ERROR_UNKNOWN_ERROR ((psa_status_t)18)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100300
301/**
302 * \brief Library initialization.
303 *
304 * Applications must call this function before calling any other
305 * function in this module.
306 *
307 * Applications may call this function more than once. Once a call
308 * succeeds, subsequent calls are guaranteed to succeed.
309 *
Gilles Peskine28538492018-07-11 17:34:00 +0200310 * \retval #PSA_SUCCESS
311 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
312 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
313 * \retval #PSA_ERROR_HARDWARE_FAILURE
314 * \retval #PSA_ERROR_TAMPERING_DETECTED
315 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +0100316 */
317psa_status_t psa_crypto_init(void);
318
Gilles Peskine2905a7a2018-03-07 16:39:31 +0100319#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
320#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
Gilles Peskine0189e752018-02-03 23:57:22 +0100321
Gilles Peskinee59236f2018-01-27 23:32:46 +0100322/**@}*/
323
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100324/** \defgroup crypto_types Key and algorithm types
325 * @{
326 */
327
Gilles Peskine308b91d2018-02-08 09:47:44 +0100328/** \brief Encoding of a key type.
329 */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100330typedef uint32_t psa_key_type_t;
331
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100332/** An invalid key type value.
333 *
334 * Zero is not the encoding of any key type.
335 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100336#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100337
338/** Vendor-defined flag
339 *
340 * Key types defined by this standard will never have the
341 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
342 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
343 * respect the bitwise structure used by standard encodings whenever practical.
344 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100345#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100346
Gilles Peskine98f0a242018-02-06 18:57:29 +0100347#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7e000000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200348
Gilles Peskine35855962018-04-19 08:39:16 +0200349/** Raw data.
350 *
351 * A "key" of this type cannot be used for any cryptographic operation.
352 * Applications may use this type to store arbitrary data in the keystore. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100353#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x02000000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200354
Gilles Peskine98f0a242018-02-06 18:57:29 +0100355#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x04000000)
356#define PSA_KEY_TYPE_CATEGORY_ASYMMETRIC ((psa_key_type_t)0x06000000)
357#define PSA_KEY_TYPE_PAIR_FLAG ((psa_key_type_t)0x01000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100358
Gilles Peskine35855962018-04-19 08:39:16 +0200359/** HMAC key.
360 *
361 * The key policy determines which underlying hash algorithm the key can be
362 * used for.
363 *
364 * HMAC keys should generally have the same size as the underlying hash.
Gilles Peskinebe42f312018-07-13 14:38:15 +0200365 * This size can be calculated with #PSA_HASH_SIZE(\c alg) where
366 * \c alg is the HMAC algorithm or the underlying hash algorithm. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100367#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x02000001)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200368
Gilles Peskineea0fb492018-07-12 17:17:20 +0200369/** A secret for key derivation.
370 *
371 * The key policy determines which key derivation algorithm the key
372 * can be used for.
373 */
374#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x02000101)
375
Gilles Peskine35855962018-04-19 08:39:16 +0200376/** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher.
377 *
378 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
379 * 32 bytes (AES-256).
380 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100381#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x04000001)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200382
Gilles Peskine35855962018-04-19 08:39:16 +0200383/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
384 *
385 * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or
386 * 24 bytes (3-key 3DES).
387 *
388 * Note that single DES and 2-key 3DES are weak and strongly
389 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
390 * is weak and deprecated and should only be used in legacy protocols.
391 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100392#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x04000002)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200393
Gilles Peskine35855962018-04-19 08:39:16 +0200394/** Key for an cipher, AEAD or MAC algorithm based on the
395 * Camellia block cipher. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100396#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x04000003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200397
Gilles Peskine35855962018-04-19 08:39:16 +0200398/** Key for the RC4 stream cipher.
399 *
400 * Note that RC4 is weak and deprecated and should only be used in
401 * legacy protocols. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100402#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x04000004)
403
Gilles Peskine308b91d2018-02-08 09:47:44 +0100404/** RSA public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100405#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x06010000)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100406/** RSA key pair (private and public key). */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100407#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x07010000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200408
Gilles Peskine06dc2632018-03-08 07:47:25 +0100409/** DSA public key. */
410#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x06020000)
411/** DSA key pair (private and public key). */
412#define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x07020000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200413
Gilles Peskine06dc2632018-03-08 07:47:25 +0100414#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x06030000)
415#define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x07030000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100416#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200417/** Elliptic curve key pair. */
Gilles Peskine06dc2632018-03-08 07:47:25 +0100418#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
419 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200420/** Elliptic curve public key. */
Gilles Peskine06dc2632018-03-08 07:47:25 +0100421#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
422 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
Gilles Peskine98f0a242018-02-06 18:57:29 +0100423
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100424/** Whether a key type is vendor-defined. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100425#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100426 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100427
428/** Whether a key type is asymmetric: either a key pair or a public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100429#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
430 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100431/** Whether a key type is the public part of a key pair. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100432#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
Moran Pekerb4d0ddd2018-04-04 12:47:52 +0300433 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
434 PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100435/** Whether a key type is a key pair containing a private part and a public
436 * part. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100437#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
438 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
439 (PSA_KEY_TYPE_CATEGORY_ASYMMETRIC | PSA_KEY_TYPE_PAIR_FLAG))
Gilles Peskine06dc2632018-03-08 07:47:25 +0100440/** The key pair type corresponding to a public key type. */
441#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
442 ((type) | PSA_KEY_TYPE_PAIR_FLAG)
443/** The public key type corresponding to a key pair type. */
444#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
445 ((type) & ~PSA_KEY_TYPE_PAIR_FLAG)
Gilles Peskined8008d62018-06-29 19:51:51 +0200446/** Whether a key type is an RSA key (pair or public-only). */
447#define PSA_KEY_TYPE_IS_RSA(type) \
Gilles Peskine3bd1a422018-07-19 11:55:51 +0200448 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
449
Gilles Peskined8008d62018-06-29 19:51:51 +0200450/** Whether a key type is an elliptic curve key (pair or public-only). */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100451#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100452 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
453 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine55728b02018-07-16 23:08:16 +0200454#define PSA_KEY_TYPE_IS_ECC_KEYPAIR(type) \
455 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
456 PSA_KEY_TYPE_ECC_KEYPAIR_BASE)
457#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
458 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
459 PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100460
Gilles Peskinee1fed0d2018-06-18 20:45:45 +0200461/** The type of PSA elliptic curve identifiers. */
462typedef uint16_t psa_ecc_curve_t;
463/** Extract the curve from an elliptic curve key type. */
464#define PSA_KEY_TYPE_GET_CURVE(type) \
465 ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
466 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
467 0))
468
469/* The encoding of curve identifiers is currently aligned with the
470 * TLS Supported Groups Registry (formerly known as the
471 * TLS EC Named Curve Registry)
472 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
473 * The values are defined by RFC 4492, RFC 7027 and RFC 7919. */
474#define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001)
475#define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002)
476#define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003)
477#define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004)
478#define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005)
479#define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006)
480#define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007)
481#define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008)
482#define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009)
483#define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a)
484#define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b)
485#define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c)
486#define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d)
487#define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e)
488#define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f)
489#define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010)
490#define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011)
491#define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012)
492#define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013)
493#define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014)
494#define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015)
495#define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016)
496#define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017)
497#define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018)
498#define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019)
499#define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a)
500#define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b)
501#define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c)
502#define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d)
503#define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e)
504#define PSA_ECC_CURVE_FFDHE_2048 ((psa_ecc_curve_t) 0x0100)
505#define PSA_ECC_CURVE_FFDHE_3072 ((psa_ecc_curve_t) 0x0101)
506#define PSA_ECC_CURVE_FFDHE_4096 ((psa_ecc_curve_t) 0x0102)
507#define PSA_ECC_CURVE_FFDHE_6144 ((psa_ecc_curve_t) 0x0103)
508#define PSA_ECC_CURVE_FFDHE_8192 ((psa_ecc_curve_t) 0x0104)
509
Gilles Peskine7e198532018-03-08 07:50:30 +0100510/** The block size of a block cipher.
511 *
512 * \param type A cipher key type (value of type #psa_key_type_t).
513 *
514 * \return The block size for a block cipher, or 1 for a stream cipher.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200515 * The return value is undefined if \p type is not a supported
Gilles Peskine35855962018-04-19 08:39:16 +0200516 * cipher key type.
517 *
518 * \note It is possible to build stream cipher algorithms on top of a block
519 * cipher, for example CTR mode (#PSA_ALG_CTR).
520 * This macro only takes the key type into account, so it cannot be
521 * used to determine the size of the data that #psa_cipher_update()
522 * might buffer for future processing in general.
Gilles Peskine7e198532018-03-08 07:50:30 +0100523 *
524 * \note This macro returns a compile-time constant if its argument is one.
525 *
526 * \warning This macro may evaluate its argument multiple times.
527 */
Gilles Peskine03182e92018-03-07 16:40:52 +0100528#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100529 ( \
530 (type) == PSA_KEY_TYPE_AES ? 16 : \
531 (type) == PSA_KEY_TYPE_DES ? 8 : \
532 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
Gilles Peskine7e198532018-03-08 07:50:30 +0100533 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100534 0)
535
Gilles Peskine308b91d2018-02-08 09:47:44 +0100536/** \brief Encoding of a cryptographic algorithm.
537 *
538 * For algorithms that can be applied to multiple key types, this type
539 * does not encode the key type. For example, for symmetric ciphers
540 * based on a block cipher, #psa_algorithm_t encodes the block cipher
541 * mode and the padding mode while the block cipher itself is encoded
542 * via #psa_key_type_t.
543 */
Gilles Peskine20035e32018-02-03 22:44:14 +0100544typedef uint32_t psa_algorithm_t;
545
Gilles Peskine98f0a242018-02-06 18:57:29 +0100546#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
547#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
548#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
549#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
550#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
551#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
552#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
553#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
554#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
555#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100556
Gilles Peskine98f0a242018-02-06 18:57:29 +0100557#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
558 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200559
Gilles Peskine308b91d2018-02-08 09:47:44 +0100560/** Whether the specified algorithm is a hash algorithm.
561 *
Gilles Peskine7e198532018-03-08 07:50:30 +0100562 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100563 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200564 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
565 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskine7e198532018-03-08 07:50:30 +0100566 * algorithm identifier.
567 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100568#define PSA_ALG_IS_HASH(alg) \
569 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200570
571/** Whether the specified algorithm is a MAC algorithm.
572 *
573 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
574 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200575 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
576 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200577 * algorithm identifier.
578 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100579#define PSA_ALG_IS_MAC(alg) \
580 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200581
582/** Whether the specified algorithm is a symmetric cipher algorithm.
583 *
584 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
585 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200586 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
587 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200588 * algorithm identifier.
589 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100590#define PSA_ALG_IS_CIPHER(alg) \
591 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200592
593/** Whether the specified algorithm is an authenticated encryption
594 * with associated data (AEAD) algorithm.
595 *
596 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
597 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200598 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
599 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200600 * algorithm identifier.
601 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100602#define PSA_ALG_IS_AEAD(alg) \
603 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200604
605/** Whether the specified algorithm is a public-key signature algorithm.
606 *
607 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
608 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200609 * \return 1 if \p alg is a public-key signature algorithm, 0 otherwise.
610 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200611 * algorithm identifier.
612 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100613#define PSA_ALG_IS_SIGN(alg) \
614 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200615
616/** Whether the specified algorithm is a public-key encryption algorithm.
617 *
618 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
619 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200620 * \return 1 if \p alg is a public-key encryption algorithm, 0 otherwise.
621 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200622 * algorithm identifier.
623 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100624#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
625 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200626
627/** Whether the specified algorithm is a key agreement algorithm.
628 *
629 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
630 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200631 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
632 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200633 * algorithm identifier.
634 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100635#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
636 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200637
638/** Whether the specified algorithm is a key derivation algorithm.
639 *
640 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
641 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200642 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
643 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200644 * algorithm identifier.
645 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100646#define PSA_ALG_IS_KEY_DERIVATION(alg) \
647 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
648
649#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
650#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
651#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
652#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
Gilles Peskinee3f694f2018-03-08 07:48:40 +0100653#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
654#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100655#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
656#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
657#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
658#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
659#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
660#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
661#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
662#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
663#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
664#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
665
Gilles Peskine8c9def32018-02-08 10:02:12 +0100666#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100667#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
Gilles Peskine35855962018-04-19 08:39:16 +0200668/** Macro to build an HMAC algorithm.
669 *
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200670 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
Gilles Peskine35855962018-04-19 08:39:16 +0200671 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200672 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200673 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine35855962018-04-19 08:39:16 +0200674 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200675 * \return The corresponding HMAC algorithm.
676 * \return Unspecified if \p alg is not a supported
677 * hash algorithm.
Gilles Peskine35855962018-04-19 08:39:16 +0200678 */
679#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100680 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200681
Gilles Peskine8c9def32018-02-08 10:02:12 +0100682#define PSA_ALG_HMAC_HASH(hmac_alg) \
683 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200684
685/** Whether the specified algorithm is an HMAC algorithm.
686 *
687 * HMAC is a family of MAC algorithms that are based on a hash function.
688 *
689 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
690 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200691 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
692 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200693 * algorithm identifier.
694 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100695#define PSA_ALG_IS_HMAC(alg) \
696 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
697 PSA_ALG_HMAC_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200698
Gilles Peskine8c9def32018-02-08 10:02:12 +0100699#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
700#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
701#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
702#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200703
704/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
705 *
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200706 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
707 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200708 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
709 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200710 * algorithm identifier.
711 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100712#define PSA_ALG_IS_CIPHER_MAC(alg) \
713 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
714 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100715
Gilles Peskine8c9def32018-02-08 10:02:12 +0100716#define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100717#define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100718#define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100719#define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200720
721/** Use a block cipher mode without padding.
722 *
723 * This padding mode may only be used with messages whose lengths are a
724 * whole number of blocks for the chosen block cipher.
725 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100726#define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200727
Gilles Peskine98f0a242018-02-06 18:57:29 +0100728#define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200729
730/** Whether the specified algorithm is a block cipher.
731 *
732 * A block cipher is a symmetric cipher that encrypts or decrypts messages
733 * by chopping them into fixed-size blocks. Processing a message requires
734 * applying a _padding mode_ to transform the message into one whose
735 * length is a whole number of blocks. To construct an algorithm
736 * identifier for a block cipher, apply a bitwise-or between the block
737 * cipher mode and the padding mode. For example, CBC with PKCS#7 padding
738 * is `PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7`.
739 *
740 * The transformation applied to each block is determined by the key type.
741 * For example, to use AES-128-CBC-PKCS7, use the algorithm above with
742 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
743 *
744 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
745 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200746 * \return 1 if \p alg is a block cipher algorithm, 0 otherwise.
747 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200748 * algorithm identifier or if it is not a symmetric cipher algorithm.
749 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100750#define PSA_ALG_IS_BLOCK_CIPHER(alg) \
751 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
752 PSA_ALG_BLOCK_CIPHER_BASE)
753
Gilles Peskinedcd14942018-07-12 00:30:52 +0200754/** The CBC block cipher mode.
755 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100756#define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100757#define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002)
758#define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003)
759#define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004)
Gilles Peskine5d1888e2018-07-12 00:32:42 +0200760
761#define PSA_ALG_STREAM_CIPHER_BASE ((psa_algorithm_t)0x04800000)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200762
Gilles Peskinedcd14942018-07-12 00:30:52 +0200763/** The CTR stream cipher mode.
764 *
765 * CTR is a stream cipher which is built from a block cipher. The
766 * underlying block cipher is determined by the key type. For example,
767 * to use AES-128-CTR, use this algorithm with
768 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
769 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100770#define PSA_ALG_CTR ((psa_algorithm_t)0x04800001)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200771
Gilles Peskinedcd14942018-07-12 00:30:52 +0200772/** The ARC4 stream cipher algorithm.
773 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100774#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100775
Gilles Peskinedcd14942018-07-12 00:30:52 +0200776/** Whether the specified algorithm is a stream cipher.
777 *
778 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
779 * by applying a bitwise-xor with a stream of bytes that is generated
780 * from a key.
781 *
782 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
783 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200784 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
785 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200786 * algorithm identifier or if it is not a symmetric cipher algorithm.
787 */
Moran Pekerbed71a22018-04-22 20:19:20 +0300788#define PSA_ALG_IS_STREAM_CIPHER(alg) \
789 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
Gilles Peskine5d1888e2018-07-12 00:32:42 +0200790 PSA_ALG_STREAM_CIPHER_BASE)
Moran Pekerbed71a22018-04-22 20:19:20 +0300791
Gilles Peskine8c9def32018-02-08 10:02:12 +0100792#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
793#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100794
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200795#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
796/** RSA PKCS#1 v1.5 signature with hashing.
797 *
798 * This is the signature scheme defined by RFC 8017
799 * (PKCS#1: RSA Cryptography Specifications) under the name
800 * RSASSA-PKCS1-v1_5.
801 *
802 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200803 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200804 *
805 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
806 * \return Unspecified if \p alg is not a supported
807 * hash algorithm.
808 */
Gilles Peskinea5926232018-03-28 14:16:50 +0200809#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200810 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
811/** Raw PKCS#1 v1.5 signature.
812 *
813 * The input to this algorithm is the DigestInfo structure used by
814 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
815 * steps 3&ndash;6.
816 */
817#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
Gilles Peskinea5926232018-03-28 14:16:50 +0200818#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200819 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200820
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200821#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
822/** RSA PSS signature with hashing.
823 *
824 * This is the signature scheme defined by RFC 8017
825 * (PKCS#1: RSA Cryptography Specifications) under the name
Gilles Peskinea4d20bd2018-06-29 23:35:02 +0200826 * RSASSA-PSS, with the message generation function MGF1, and with
827 * a salt length equal to the length of the hash. The specified
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200828 * hash algorithm is used to hash the input message, to create the
829 * salted hash, and for the mask generation.
830 *
831 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200832 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200833 *
834 * \return The corresponding RSA PSS signature algorithm.
835 * \return Unspecified if \p alg is not a supported
836 * hash algorithm.
837 */
838#define PSA_ALG_RSA_PSS(hash_alg) \
839 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
840#define PSA_ALG_IS_RSA_PSS(alg) \
841 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
842
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200843#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
844/** DSA signature with hashing.
845 *
846 * This is the signature scheme defined by FIPS 186-4,
847 * with a random per-message secret number (*k*).
848 *
849 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200850 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200851 *
852 * \return The corresponding DSA signature algorithm.
853 * \return Unspecified if \p alg is not a supported
854 * hash algorithm.
855 */
856#define PSA_ALG_DSA(hash_alg) \
857 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
858#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
859#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
860#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
861 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
862#define PSA_ALG_IS_DSA(alg) \
863 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
864 PSA_ALG_DSA_BASE)
865#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
866 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +0200867#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
868 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
869#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
870 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200871
872#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
873/** ECDSA signature with hashing.
874 *
875 * This is the ECDSA signature scheme defined by ANSI X9.62,
876 * with a random per-message secret number (*k*).
877 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200878 * The representation of the signature as a byte string consists of
879 * the concatentation of the signature values *r* and *s*. Each of
880 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
881 * of the base point of the curve in octets. Each value is represented
882 * in big-endian order (most significant octet first).
883 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200884 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200885 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200886 *
887 * \return The corresponding ECDSA signature algorithm.
888 * \return Unspecified if \p alg is not a supported
889 * hash algorithm.
890 */
891#define PSA_ALG_ECDSA(hash_alg) \
892 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
893/** ECDSA signature without hashing.
894 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200895 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200896 * without specifying a hash algorithm. This algorithm may only be
897 * used to sign or verify a sequence of bytes that should be an
898 * already-calculated hash. Note that the input is padded with
899 * zeros on the left or truncated on the left as required to fit
900 * the curve size.
901 */
902#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
903#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
904/** Deterministic ECDSA signature with hashing.
905 *
906 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
907 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200908 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
909 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200910 * Note that when this algorithm is used for verification, signatures
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200911 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200912 * same private key are accepted. In other words,
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200913 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
914 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200915 *
916 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200917 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200918 *
919 * \return The corresponding deterministic ECDSA signature
920 * algorithm.
921 * \return Unspecified if \p alg is not a supported
922 * hash algorithm.
923 */
924#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
925 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
926#define PSA_ALG_IS_ECDSA(alg) \
927 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
928 PSA_ALG_ECDSA_BASE)
929#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
930 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +0200931#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
932 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
933#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
934 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200935
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200936/** Get the hash used by a hash-and-sign signature algorithm.
937 *
938 * A hash-and-sign algorithm is a signature algorithm which is
939 * composed of two phases: first a hashing phase which does not use
940 * the key and produces a hash of the input message, then a signing
941 * phase which only uses the hash and the key and not the message
942 * itself.
943 *
944 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200945 * #PSA_ALG_IS_SIGN(\p alg) is true).
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200946 *
947 * \return The underlying hash algorithm if \p alg is a hash-and-sign
948 * algorithm.
949 * \return 0 if \p alg is a signature algorithm that does not
950 * follow the hash-and-sign structure.
951 * \return Unspecified if \p alg is not a signature algorithm or
952 * if it is not supported by the implementation.
953 */
954#define PSA_ALG_SIGN_GET_HASH(alg) \
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200955 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
956 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg) ? \
Gilles Peskine54622ae2018-06-29 22:24:24 +0200957 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200958 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
959 0)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100960
Gilles Peskinedcd14942018-07-12 00:30:52 +0200961/** RSA PKCS#1 v1.5 encryption.
962 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200963#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200964
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200965#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200966/** RSA OAEP encryption.
967 *
968 * This is the encryption scheme defined by RFC 8017
969 * (PKCS#1: RSA Cryptography Specifications) under the name
970 * RSAES-OAEP, with the message generation function MGF1.
971 *
972 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
973 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
974 * for MGF1.
975 *
976 * \return The corresponding RSA OAEP signature algorithm.
977 * \return Unspecified if \p alg is not a supported
978 * hash algorithm.
979 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200980#define PSA_ALG_RSA_OAEP(hash_alg) \
981 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
982#define PSA_ALG_IS_RSA_OAEP(alg) \
983 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
Gilles Peskine072ac562018-06-30 00:21:29 +0200984#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
985 (PSA_ALG_IS_RSA_OAEP(alg) ? \
986 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
987 0)
Gilles Peskined1e8e412018-06-07 09:49:39 +0200988
Gilles Peskinebef7f142018-07-12 17:22:21 +0200989#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x30000100)
990/** Macro to build an HKDF algorithm.
991 *
992 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
993 *
994 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
995 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
996 *
997 * \return The corresponding HKDF algorithm.
998 * \return Unspecified if \p alg is not a supported
999 * hash algorithm.
1000 */
1001#define PSA_ALG_HKDF(hash_alg) \
1002 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1003/** Whether the specified algorithm is an HKDF algorithm.
1004 *
1005 * HKDF is a family of key derivation algorithms that are based on a hash
1006 * function and the HMAC construction.
1007 *
1008 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1009 *
1010 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1011 * This macro may return either 0 or 1 if \c alg is not a supported
1012 * key derivation algorithm identifier.
1013 */
1014#define PSA_ALG_IS_HKDF(alg) \
1015 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1016#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1017 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1018
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001019/**@}*/
1020
1021/** \defgroup key_management Key management
1022 * @{
1023 */
1024
1025/**
1026 * \brief Import a key in binary format.
1027 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +01001028 * This function supports any output from psa_export_key(). Refer to the
1029 * documentation of psa_export_key() for the format for each key type.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001030 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001031 * \param key Slot where the key will be stored. This must be a
1032 * valid slot for a key of the chosen type. It must
1033 * be unoccupied.
1034 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001035 * \param[in] data Buffer containing the key data.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001036 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001037 *
Gilles Peskine28538492018-07-11 17:34:00 +02001038 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001039 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001040 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001041 * The key type or key size is not supported, either by the
1042 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001043 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +01001044 * The key slot is invalid,
1045 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +02001046 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001047 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001048 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1049 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1050 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1051 * \retval #PSA_ERROR_HARDWARE_FAILURE
1052 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001053 */
1054psa_status_t psa_import_key(psa_key_slot_t key,
1055 psa_key_type_t type,
1056 const uint8_t *data,
1057 size_t data_length);
1058
1059/**
Gilles Peskine154bd952018-04-19 08:38:16 +02001060 * \brief Destroy a key and restore the slot to its default state.
1061 *
1062 * This function destroys the content of the key slot from both volatile
1063 * memory and, if applicable, non-volatile storage. Implementations shall
1064 * make a best effort to ensure that any previous content of the slot is
1065 * unrecoverable.
1066 *
1067 * This function also erases any metadata such as policies. It returns the
1068 * specified slot to its default state.
1069 *
1070 * \param key The key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001071 *
Gilles Peskine28538492018-07-11 17:34:00 +02001072 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +02001073 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +02001074 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001075 * The slot holds content and cannot be erased because it is
1076 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskine28538492018-07-11 17:34:00 +02001077 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001078 * The specified slot number does not designate a valid slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001079 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001080 * There was an failure in communication with the cryptoprocessor.
1081 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +02001082 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001083 * The storage is corrupted. Implementations shall make a best effort
1084 * to erase key material even in this stage, however applications
1085 * should be aware that it may be impossible to guarantee that the
1086 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +02001087 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001088 * An unexpected condition which is not a storage corruption or
1089 * a communication failure occurred. The cryptoprocessor may have
1090 * been compromised.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001091 */
1092psa_status_t psa_destroy_key(psa_key_slot_t key);
1093
1094/**
1095 * \brief Get basic metadata about a key.
1096 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001097 * \param key Slot whose content is queried. This must
1098 * be an occupied key slot.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001099 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001100 * This may be a null pointer, in which case the key type
1101 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001102 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +01001103 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +01001104 * is not written.
1105 *
Gilles Peskine28538492018-07-11 17:34:00 +02001106 * \retval #PSA_SUCCESS
1107 * \retval #PSA_ERROR_EMPTY_SLOT
1108 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1109 * \retval #PSA_ERROR_HARDWARE_FAILURE
1110 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001111 */
1112psa_status_t psa_get_key_information(psa_key_slot_t key,
1113 psa_key_type_t *type,
1114 size_t *bits);
1115
1116/**
1117 * \brief Export a key in binary format.
1118 *
1119 * The output of this function can be passed to psa_import_key() to
1120 * create an equivalent object.
1121 *
1122 * If a key is created with psa_import_key() and then exported with
1123 * this function, it is not guaranteed that the resulting data is
1124 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +01001125 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001126 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001127 * For standard key types, the output format is as follows:
1128 *
1129 * - For symmetric keys (including MAC keys), the format is the
1130 * raw bytes of the key.
1131 * - For DES, the key data consists of 8 bytes. The parity bits must be
1132 * correct.
1133 * - For Triple-DES, the format is the concatenation of the
1134 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +01001135 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine2743e422018-06-27 22:57:11 +02001136 * is the non-encrypted DER representation defined by PKCS\#1 (RFC 8017)
1137 * as RSAPrivateKey.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001138 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
Gilles Peskine971f7062018-03-20 17:52:58 +01001139 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001140 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001141 * \param key Slot whose content is to be exported. This must
1142 * be an occupied key slot.
1143 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001144 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001145 * \param[out] data_length On success, the number of bytes
1146 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001147 *
Gilles Peskine28538492018-07-11 17:34:00 +02001148 * \retval #PSA_SUCCESS
1149 * \retval #PSA_ERROR_EMPTY_SLOT
1150 * \retval #PSA_ERROR_NOT_PERMITTED
1151 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1152 * \retval #PSA_ERROR_HARDWARE_FAILURE
1153 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001154 */
1155psa_status_t psa_export_key(psa_key_slot_t key,
1156 uint8_t *data,
1157 size_t data_size,
1158 size_t *data_length);
1159
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001160/**
1161 * \brief Export a public key or the public part of a key pair in binary format.
1162 *
1163 * The output of this function can be passed to psa_import_key() to
1164 * create an object that is equivalent to the public key.
1165 *
1166 * For standard key types, the output format is as follows:
1167 *
1168 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
Moran Pekerdd4ea382018-04-03 15:30:03 +03001169 * the format is the DER representation of the public key defined by RFC 5280
Gilles Peskine971f7062018-03-20 17:52:58 +01001170 * as SubjectPublicKeyInfo.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001171 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001172 * \param key Slot whose content is to be exported. This must
1173 * be an occupied key slot.
1174 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001175 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001176 * \param[out] data_length On success, the number of bytes
1177 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001178 *
Gilles Peskine28538492018-07-11 17:34:00 +02001179 * \retval #PSA_SUCCESS
1180 * \retval #PSA_ERROR_EMPTY_SLOT
1181 * \retval #PSA_ERROR_INVALID_ARGUMENT
1182 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1183 * \retval #PSA_ERROR_HARDWARE_FAILURE
1184 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001185 */
1186psa_status_t psa_export_public_key(psa_key_slot_t key,
1187 uint8_t *data,
1188 size_t data_size,
1189 size_t *data_length);
1190
1191/**@}*/
1192
1193/** \defgroup policy Key policies
1194 * @{
1195 */
1196
1197/** \brief Encoding of permitted usage on a key. */
1198typedef uint32_t psa_key_usage_t;
1199
Gilles Peskine7e198532018-03-08 07:50:30 +01001200/** Whether the key may be exported.
1201 *
1202 * A public key or the public part of a key pair may always be exported
1203 * regardless of the value of this permission flag.
1204 *
1205 * If a key does not have export permission, implementations shall not
1206 * allow the key to be exported in plain form from the cryptoprocessor,
1207 * whether through psa_export_key() or through a proprietary interface.
1208 * The key may however be exportable in a wrapped form, i.e. in a form
1209 * where it is encrypted by another key.
1210 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001211#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1212
Gilles Peskine7e198532018-03-08 07:50:30 +01001213/** Whether the key may be used to encrypt a message.
1214 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001215 * This flag allows the key to be used for a symmetric encryption operation,
1216 * for an AEAD encryption-and-authentication operation,
1217 * or for an asymmetric encryption operation,
1218 * if otherwise permitted by the key's type and policy.
1219 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001220 * For a key pair, this concerns the public key.
1221 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001222#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +01001223
1224/** Whether the key may be used to decrypt a message.
1225 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001226 * This flag allows the key to be used for a symmetric decryption operation,
1227 * for an AEAD decryption-and-verification operation,
1228 * or for an asymmetric decryption operation,
1229 * if otherwise permitted by the key's type and policy.
1230 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001231 * For a key pair, this concerns the private key.
1232 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001233#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +01001234
1235/** Whether the key may be used to sign a message.
1236 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001237 * This flag allows the key to be used for a MAC calculation operation
1238 * or for an asymmetric signature operation,
1239 * if otherwise permitted by the key's type and policy.
1240 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001241 * For a key pair, this concerns the private key.
1242 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001243#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +01001244
1245/** Whether the key may be used to verify a message signature.
1246 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001247 * This flag allows the key to be used for a MAC verification operation
1248 * or for an asymmetric signature verification operation,
1249 * if otherwise permitted by by the key's type and policy.
1250 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001251 * For a key pair, this concerns the public key.
1252 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001253#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
1254
Gilles Peskineea0fb492018-07-12 17:17:20 +02001255/** Whether the key may be used to derive other keys.
1256 */
1257#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00001000)
1258
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001259/** The type of the key policy data structure.
1260 *
1261 * This is an implementation-defined \c struct. Applications should not
1262 * make any assumptions about the content of this structure except
1263 * as directed by the documentation of a specific implementation. */
1264typedef struct psa_key_policy_s psa_key_policy_t;
1265
1266/** \brief Initialize a key policy structure to a default that forbids all
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001267 * usage of the key.
1268 *
1269 * \param[out] policy The policy object to initialize.
1270 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001271void psa_key_policy_init(psa_key_policy_t *policy);
1272
Gilles Peskine7e198532018-03-08 07:50:30 +01001273/** \brief Set the standard fields of a policy structure.
1274 *
1275 * Note that this function does not make any consistency check of the
1276 * parameters. The values are only checked when applying the policy to
1277 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001278 *
1279 * \param[out] policy The policy object to modify.
1280 * \param usage The permitted uses for the key.
1281 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +01001282 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001283void psa_key_policy_set_usage(psa_key_policy_t *policy,
1284 psa_key_usage_t usage,
1285 psa_algorithm_t alg);
1286
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001287/** \brief Retrieve the usage field of a policy structure.
1288 *
1289 * \param[in] policy The policy object to query.
1290 *
1291 * \return The permitted uses for a key with this policy.
1292 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001293psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001294
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001295/** \brief Retrieve the algorithm field of a policy structure.
1296 *
1297 * \param[in] policy The policy object to query.
1298 *
1299 * \return The permitted algorithm for a key with this policy.
1300 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001301psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001302
1303/** \brief Set the usage policy on a key slot.
1304 *
1305 * This function must be called on an empty key slot, before importing,
1306 * generating or creating a key in the slot. Changing the policy of an
1307 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +01001308 *
1309 * Implementations may set restrictions on supported key policies
1310 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001311 *
1312 * \param key The key slot whose policy is to be changed.
1313 * \param[in] policy The policy object to query.
1314 *
1315 * \retval #PSA_SUCCESS
1316 * \retval #PSA_ERROR_OCCUPIED_SLOT
1317 * \retval #PSA_ERROR_NOT_SUPPORTED
1318 * \retval #PSA_ERROR_INVALID_ARGUMENT
1319 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1320 * \retval #PSA_ERROR_HARDWARE_FAILURE
1321 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001322 */
1323psa_status_t psa_set_key_policy(psa_key_slot_t key,
1324 const psa_key_policy_t *policy);
1325
Gilles Peskine7e198532018-03-08 07:50:30 +01001326/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001327 *
1328 * \param key The key slot whose policy is being queried.
1329 * \param[out] policy On success, the key's policy.
1330 *
1331 * \retval #PSA_SUCCESS
1332 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1333 * \retval #PSA_ERROR_HARDWARE_FAILURE
1334 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7e198532018-03-08 07:50:30 +01001335 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001336psa_status_t psa_get_key_policy(psa_key_slot_t key,
1337 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +01001338
1339/**@}*/
1340
Gilles Peskine609b6a52018-03-03 21:31:50 +01001341/** \defgroup persistence Key lifetime
1342 * @{
1343 */
1344
1345/** Encoding of key lifetimes.
1346 */
1347typedef uint32_t psa_key_lifetime_t;
1348
1349/** A volatile key slot retains its content as long as the application is
1350 * running. It is guaranteed to be erased on a power reset.
1351 */
1352#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1353
1354/** A persistent key slot retains its content as long as it is not explicitly
1355 * destroyed.
1356 */
1357#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1358
1359/** A write-once key slot may not be modified once a key has been set.
1360 * It will retain its content as long as the device remains operational.
1361 */
1362#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
1363
Gilles Peskined393e182018-03-08 07:49:16 +01001364/** \brief Retrieve the lifetime of a key slot.
1365 *
1366 * The assignment of lifetimes to slots is implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001367 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001368 * \param key Slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001369 * \param[out] lifetime On success, the lifetime value.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001370 *
Gilles Peskine28538492018-07-11 17:34:00 +02001371 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001372 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001373 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603a7d245a2018-04-17 00:40:08 -07001374 * The key slot is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001375 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1376 * \retval #PSA_ERROR_HARDWARE_FAILURE
1377 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +01001378 */
Gilles Peskine609b6a52018-03-03 21:31:50 +01001379psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
1380 psa_key_lifetime_t *lifetime);
1381
Gilles Peskined393e182018-03-08 07:49:16 +01001382/** \brief Change the lifetime of a key slot.
1383 *
1384 * Whether the lifetime of a key slot can be changed at all, and if so
Gilles Peskine19067982018-03-20 17:54:53 +01001385 * whether the lifetime of an occupied key slot can be changed, is
Gilles Peskined393e182018-03-08 07:49:16 +01001386 * implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001387 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001388 * \param key Slot whose lifetime is to be changed.
1389 * \param lifetime The lifetime value to set for the given key slot.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001390 *
Gilles Peskine28538492018-07-11 17:34:00 +02001391 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001392 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001393 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603804cd712018-03-20 22:44:08 +02001394 * The key slot is invalid,
mohammad1603a7d245a2018-04-17 00:40:08 -07001395 * or the lifetime value is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001396 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001397 * The implementation does not support the specified lifetime value,
1398 * at least for the specified key slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001399 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001400 * The slot contains a key, and the implementation does not support
1401 * changing the lifetime of an occupied slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001402 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1403 * \retval #PSA_ERROR_HARDWARE_FAILURE
1404 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +01001405 */
1406psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
mohammad1603ea050092018-04-17 00:31:34 -07001407 psa_key_lifetime_t lifetime);
Gilles Peskined393e182018-03-08 07:49:16 +01001408
Gilles Peskine609b6a52018-03-03 21:31:50 +01001409/**@}*/
1410
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001411/** \defgroup hash Message digests
1412 * @{
1413 */
1414
Gilles Peskine308b91d2018-02-08 09:47:44 +01001415/** The type of the state data structure for multipart hash operations.
1416 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001417 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01001418 * make any assumptions about the content of this structure except
1419 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001420typedef struct psa_hash_operation_s psa_hash_operation_t;
1421
Gilles Peskine308b91d2018-02-08 09:47:44 +01001422/** The size of the output of psa_hash_finish(), in bytes.
1423 *
1424 * This is also the hash size that psa_hash_verify() expects.
1425 *
1426 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001427 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
Gilles Peskinebe42f312018-07-13 14:38:15 +02001428 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
Gilles Peskine35855962018-04-19 08:39:16 +02001429 * hash algorithm).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001430 *
1431 * \return The hash size for the specified hash algorithm.
1432 * If the hash algorithm is not recognized, return 0.
1433 * An implementation may return either 0 or the correct size
1434 * for a hash algorithm that it recognizes, but does not support.
1435 */
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001436#define PSA_HASH_SIZE(alg) \
1437 ( \
1438 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD2 ? 16 : \
1439 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD4 ? 16 : \
1440 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD5 ? 16 : \
1441 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
1442 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
1443 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
1444 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
1445 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
1446 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
1447 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
1448 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
1449 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
1450 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
1451 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
1452 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001453 0)
1454
Gilles Peskine308b91d2018-02-08 09:47:44 +01001455/** Start a multipart hash operation.
1456 *
1457 * The sequence of operations to calculate a hash (message digest)
1458 * is as follows:
1459 * -# Allocate an operation object which will be passed to all the functions
1460 * listed here.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001461 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001462 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001463 * of the message each time. The hash that is calculated is the hash
1464 * of the concatenation of these messages in order.
1465 * -# To calculate the hash, call psa_hash_finish().
1466 * To compare the hash with an expected value, call psa_hash_verify().
1467 *
1468 * The application may call psa_hash_abort() at any time after the operation
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001469 * has been initialized with psa_hash_setup().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001470 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001471 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001472 * eventually terminate the operation. The following events terminate an
1473 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001474 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001475 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001476 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001477 * \param[out] operation The operation object to use.
1478 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1479 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001480 *
Gilles Peskine28538492018-07-11 17:34:00 +02001481 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001482 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001483 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001484 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001485 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1486 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1487 * \retval #PSA_ERROR_HARDWARE_FAILURE
1488 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001489 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001490psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001491 psa_algorithm_t alg);
1492
Gilles Peskine308b91d2018-02-08 09:47:44 +01001493/** Add a message fragment to a multipart hash operation.
1494 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001495 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001496 *
1497 * If this function returns an error status, the operation becomes inactive.
1498 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001499 * \param[in,out] operation Active hash operation.
1500 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001501 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001502 *
Gilles Peskine28538492018-07-11 17:34:00 +02001503 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001504 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001505 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001506 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001507 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1508 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1509 * \retval #PSA_ERROR_HARDWARE_FAILURE
1510 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001511 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001512psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1513 const uint8_t *input,
1514 size_t input_length);
1515
Gilles Peskine308b91d2018-02-08 09:47:44 +01001516/** Finish the calculation of the hash of a message.
1517 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001518 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001519 * This function calculates the hash of the message formed by concatenating
1520 * the inputs passed to preceding calls to psa_hash_update().
1521 *
1522 * When this function returns, the operation becomes inactive.
1523 *
1524 * \warning Applications should not call this function if they expect
1525 * a specific value for the hash. Call psa_hash_verify() instead.
1526 * Beware that comparing integrity or authenticity data such as
1527 * hash values with a function such as \c memcmp is risky
1528 * because the time taken by the comparison may leak information
1529 * about the hashed data which could allow an attacker to guess
1530 * a valid hash and thereby bypass security controls.
1531 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001532 * \param[in,out] operation Active hash operation.
1533 * \param[out] hash Buffer where the hash is to be written.
1534 * \param hash_size Size of the \p hash buffer in bytes.
1535 * \param[out] hash_length On success, the number of bytes
1536 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001537 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001538 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001539 *
Gilles Peskine28538492018-07-11 17:34:00 +02001540 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001541 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001542 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001543 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001544 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001545 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001546 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001547 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001548 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1549 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1550 * \retval #PSA_ERROR_HARDWARE_FAILURE
1551 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001552 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001553psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1554 uint8_t *hash,
1555 size_t hash_size,
1556 size_t *hash_length);
1557
Gilles Peskine308b91d2018-02-08 09:47:44 +01001558/** Finish the calculation of the hash of a message and compare it with
1559 * an expected value.
1560 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001561 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001562 * This function calculates the hash of the message formed by concatenating
1563 * the inputs passed to preceding calls to psa_hash_update(). It then
1564 * compares the calculated hash with the expected hash passed as a
1565 * parameter to this function.
1566 *
1567 * When this function returns, the operation becomes inactive.
1568 *
Gilles Peskine19067982018-03-20 17:54:53 +01001569 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001570 * comparison between the actual hash and the expected hash is performed
1571 * in constant time.
1572 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001573 * \param[in,out] operation Active hash operation.
1574 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001575 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001576 *
Gilles Peskine28538492018-07-11 17:34:00 +02001577 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001578 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001579 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001580 * The hash of the message was calculated successfully, but it
1581 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001582 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001583 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001584 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1585 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1586 * \retval #PSA_ERROR_HARDWARE_FAILURE
1587 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001588 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001589psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1590 const uint8_t *hash,
1591 size_t hash_length);
1592
Gilles Peskine308b91d2018-02-08 09:47:44 +01001593/** Abort a hash operation.
1594 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001595 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001596 * \p operation structure itself. Once aborted, the operation object
1597 * can be reused for another operation by calling
1598 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001599 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001600 * You may call this function any time after the operation object has
1601 * been initialized by any of the following methods:
1602 * - A call to psa_hash_setup(), whether it succeeds or not.
1603 * - Initializing the \c struct to all-bits-zero.
1604 * - Initializing the \c struct to logical zeros, e.g.
1605 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001606 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001607 * In particular, calling psa_hash_abort() after the operation has been
1608 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1609 * psa_hash_verify() is safe and has no effect.
1610 *
1611 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001612 *
Gilles Peskine28538492018-07-11 17:34:00 +02001613 * \retval #PSA_SUCCESS
1614 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001615 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001616 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1617 * \retval #PSA_ERROR_HARDWARE_FAILURE
1618 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001619 */
1620psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001621
1622/**@}*/
1623
Gilles Peskine8c9def32018-02-08 10:02:12 +01001624/** \defgroup MAC Message authentication codes
1625 * @{
1626 */
1627
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001628/** The type of the state data structure for multipart MAC operations.
1629 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001630 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001631 * make any assumptions about the content of this structure except
1632 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001633typedef struct psa_mac_operation_s psa_mac_operation_t;
1634
Gilles Peskine89167cb2018-07-08 20:12:23 +02001635/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001636 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001637 * This function sets up the calculation of the MAC
1638 * (message authentication code) of a byte string.
1639 * To verify the MAC of a message against an
1640 * expected value, use psa_mac_verify_setup() instead.
1641 *
1642 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001643 * -# Allocate an operation object which will be passed to all the functions
1644 * listed here.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001645 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001646 * The key remains associated with the operation even if the content
1647 * of the key slot changes.
1648 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1649 * of the message each time. The MAC that is calculated is the MAC
1650 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001651 * -# At the end of the message, call psa_mac_sign_finish() to finish
1652 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001653 *
1654 * The application may call psa_mac_abort() at any time after the operation
Gilles Peskine89167cb2018-07-08 20:12:23 +02001655 * has been initialized with psa_mac_sign_setup().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001656 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001657 * After a successful call to psa_mac_sign_setup(), the application must
1658 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001659 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001660 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001661 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001662 * \param[out] operation The operation object to use.
1663 * \param key Slot containing the key to use for the operation.
1664 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1665 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001666 *
Gilles Peskine28538492018-07-11 17:34:00 +02001667 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001668 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001669 * \retval #PSA_ERROR_EMPTY_SLOT
1670 * \retval #PSA_ERROR_NOT_PERMITTED
1671 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001672 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001673 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001674 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001675 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1676 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1677 * \retval #PSA_ERROR_HARDWARE_FAILURE
1678 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001679 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001680psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
1681 psa_key_slot_t key,
1682 psa_algorithm_t alg);
1683
1684/** Start a multipart MAC verification operation.
1685 *
1686 * This function sets up the verification of the MAC
1687 * (message authentication code) of a byte string against an expected value.
1688 *
1689 * The sequence of operations to verify a MAC is as follows:
1690 * -# Allocate an operation object which will be passed to all the functions
1691 * listed here.
1692 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1693 * The key remains associated with the operation even if the content
1694 * of the key slot changes.
1695 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1696 * of the message each time. The MAC that is calculated is the MAC
1697 * of the concatenation of these messages in order.
1698 * -# At the end of the message, call psa_mac_verify_finish() to finish
1699 * calculating the actual MAC of the message and verify it against
1700 * the expected value.
1701 *
1702 * The application may call psa_mac_abort() at any time after the operation
1703 * has been initialized with psa_mac_verify_setup().
1704 *
1705 * After a successful call to psa_mac_verify_setup(), the application must
1706 * eventually terminate the operation through one of the following methods:
1707 * - A failed call to psa_mac_update().
1708 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1709 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001710 * \param[out] operation The operation object to use.
1711 * \param key Slot containing the key to use for the operation.
1712 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1713 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001714 *
Gilles Peskine28538492018-07-11 17:34:00 +02001715 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001716 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001717 * \retval #PSA_ERROR_EMPTY_SLOT
1718 * \retval #PSA_ERROR_NOT_PERMITTED
1719 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001720 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001721 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001722 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001723 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1724 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1725 * \retval #PSA_ERROR_HARDWARE_FAILURE
1726 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001727 */
1728psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
1729 psa_key_slot_t key,
1730 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001731
Gilles Peskinedcd14942018-07-12 00:30:52 +02001732/** Add a message fragment to a multipart MAC operation.
1733 *
1734 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1735 * before calling this function.
1736 *
1737 * If this function returns an error status, the operation becomes inactive.
1738 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001739 * \param[in,out] operation Active MAC operation.
1740 * \param[in] input Buffer containing the message fragment to add to
1741 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001742 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001743 *
1744 * \retval #PSA_SUCCESS
1745 * Success.
1746 * \retval #PSA_ERROR_BAD_STATE
1747 * The operation state is not valid (not started, or already completed).
1748 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1749 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1750 * \retval #PSA_ERROR_HARDWARE_FAILURE
1751 * \retval #PSA_ERROR_TAMPERING_DETECTED
1752 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001753psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1754 const uint8_t *input,
1755 size_t input_length);
1756
Gilles Peskinedcd14942018-07-12 00:30:52 +02001757/** Finish the calculation of the MAC of a message.
1758 *
1759 * The application must call psa_mac_sign_setup() before calling this function.
1760 * This function calculates the MAC of the message formed by concatenating
1761 * the inputs passed to preceding calls to psa_mac_update().
1762 *
1763 * When this function returns, the operation becomes inactive.
1764 *
1765 * \warning Applications should not call this function if they expect
1766 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1767 * Beware that comparing integrity or authenticity data such as
1768 * MAC values with a function such as \c memcmp is risky
1769 * because the time taken by the comparison may leak information
1770 * about the MAC value which could allow an attacker to guess
1771 * a valid MAC and thereby bypass security controls.
1772 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001773 * \param[in,out] operation Active MAC operation.
1774 * \param[out] mac Buffer where the MAC value is to be written.
1775 * \param mac_size Size of the \p mac buffer in bytes.
1776 * \param[out] mac_length On success, the number of bytes
1777 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001778 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001779 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001780 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001781 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001782 *
1783 * \retval #PSA_SUCCESS
1784 * Success.
1785 * \retval #PSA_ERROR_BAD_STATE
1786 * The operation state is not valid (not started, or already completed).
1787 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001788 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001789 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1790 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1791 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1792 * \retval #PSA_ERROR_HARDWARE_FAILURE
1793 * \retval #PSA_ERROR_TAMPERING_DETECTED
1794 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001795psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1796 uint8_t *mac,
1797 size_t mac_size,
1798 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001799
Gilles Peskinedcd14942018-07-12 00:30:52 +02001800/** Finish the calculation of the MAC of a message and compare it with
1801 * an expected value.
1802 *
1803 * The application must call psa_mac_verify_setup() before calling this function.
1804 * This function calculates the MAC of the message formed by concatenating
1805 * the inputs passed to preceding calls to psa_mac_update(). It then
1806 * compares the calculated MAC with the expected MAC passed as a
1807 * parameter to this function.
1808 *
1809 * When this function returns, the operation becomes inactive.
1810 *
1811 * \note Implementations shall make the best effort to ensure that the
1812 * comparison between the actual MAC and the expected MAC is performed
1813 * in constant time.
1814 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001815 * \param[in,out] operation Active MAC operation.
1816 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001817 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001818 *
1819 * \retval #PSA_SUCCESS
1820 * The expected MAC is identical to the actual MAC of the message.
1821 * \retval #PSA_ERROR_INVALID_SIGNATURE
1822 * The MAC of the message was calculated successfully, but it
1823 * differs from the expected MAC.
1824 * \retval #PSA_ERROR_BAD_STATE
1825 * The operation state is not valid (not started, or already completed).
1826 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1827 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1828 * \retval #PSA_ERROR_HARDWARE_FAILURE
1829 * \retval #PSA_ERROR_TAMPERING_DETECTED
1830 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001831psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1832 const uint8_t *mac,
1833 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001834
Gilles Peskinedcd14942018-07-12 00:30:52 +02001835/** Abort a MAC operation.
1836 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001837 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001838 * \p operation structure itself. Once aborted, the operation object
1839 * can be reused for another operation by calling
1840 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001841 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001842 * You may call this function any time after the operation object has
1843 * been initialized by any of the following methods:
1844 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1845 * it succeeds or not.
1846 * - Initializing the \c struct to all-bits-zero.
1847 * - Initializing the \c struct to logical zeros, e.g.
1848 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001849 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001850 * In particular, calling psa_mac_abort() after the operation has been
1851 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1852 * psa_mac_verify_finish() is safe and has no effect.
1853 *
1854 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001855 *
1856 * \retval #PSA_SUCCESS
1857 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001858 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001859 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1860 * \retval #PSA_ERROR_HARDWARE_FAILURE
1861 * \retval #PSA_ERROR_TAMPERING_DETECTED
1862 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001863psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1864
1865/**@}*/
1866
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001867/** \defgroup cipher Symmetric ciphers
1868 * @{
1869 */
1870
1871/** The type of the state data structure for multipart cipher operations.
1872 *
1873 * This is an implementation-defined \c struct. Applications should not
1874 * make any assumptions about the content of this structure except
1875 * as directed by the documentation of a specific implementation. */
1876typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1877
1878/** Set the key for a multipart symmetric encryption operation.
1879 *
1880 * The sequence of operations to encrypt a message with a symmetric cipher
1881 * is as follows:
1882 * -# Allocate an operation object which will be passed to all the functions
1883 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02001884 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001885 * The key remains associated with the operation even if the content
1886 * of the key slot changes.
Gilles Peskinefe119512018-07-08 21:39:34 +02001887 * -# Call either psa_encrypt_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001888 * generate or set the IV (initialization vector). You should use
1889 * psa_encrypt_generate_iv() unless the protocol you are implementing
1890 * requires a specific IV value.
1891 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1892 * of the message each time.
1893 * -# Call psa_cipher_finish().
1894 *
1895 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02001896 * has been initialized with psa_cipher_encrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001897 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001898 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001899 * eventually terminate the operation. The following events terminate an
1900 * operation:
Gilles Peskinefe119512018-07-08 21:39:34 +02001901 * - A failed call to psa_encrypt_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001902 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001903 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001904 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001905 * \param[out] operation The operation object to use.
1906 * \param key Slot containing the key to use for the operation.
1907 * \param alg The cipher algorithm to compute
1908 * (\c PSA_ALG_XXX value such that
1909 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001910 *
Gilles Peskine28538492018-07-11 17:34:00 +02001911 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001912 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001913 * \retval #PSA_ERROR_EMPTY_SLOT
1914 * \retval #PSA_ERROR_NOT_PERMITTED
1915 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001916 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001917 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001918 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001919 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1920 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1921 * \retval #PSA_ERROR_HARDWARE_FAILURE
1922 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001923 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001924psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
1925 psa_key_slot_t key,
1926 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001927
1928/** Set the key for a multipart symmetric decryption operation.
1929 *
1930 * The sequence of operations to decrypt a message with a symmetric cipher
1931 * is as follows:
1932 * -# Allocate an operation object which will be passed to all the functions
1933 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02001934 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001935 * The key remains associated with the operation even if the content
1936 * of the key slot changes.
1937 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1938 * decryption. If the IV is prepended to the ciphertext, you can call
1939 * psa_cipher_update() on a buffer containing the IV followed by the
1940 * beginning of the message.
1941 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1942 * of the message each time.
1943 * -# Call psa_cipher_finish().
1944 *
1945 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02001946 * has been initialized with psa_cipher_decrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001947 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001948 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001949 * eventually terminate the operation. The following events terminate an
1950 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001951 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001952 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001953 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001954 * \param[out] operation The operation object to use.
1955 * \param key Slot containing the key to use for the operation.
1956 * \param alg The cipher algorithm to compute
1957 * (\c PSA_ALG_XXX value such that
1958 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001959 *
Gilles Peskine28538492018-07-11 17:34:00 +02001960 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001961 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001962 * \retval #PSA_ERROR_EMPTY_SLOT
1963 * \retval #PSA_ERROR_NOT_PERMITTED
1964 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001965 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001966 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001967 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001968 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1969 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1970 * \retval #PSA_ERROR_HARDWARE_FAILURE
1971 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001972 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001973psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
1974 psa_key_slot_t key,
1975 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001976
Gilles Peskinedcd14942018-07-12 00:30:52 +02001977/** Generate an IV for a symmetric encryption operation.
1978 *
1979 * This function generates a random IV (initialization vector), nonce
1980 * or initial counter value for the encryption operation as appropriate
1981 * for the chosen algorithm, key type and key size.
1982 *
1983 * The application must call psa_cipher_encrypt_setup() before
1984 * calling this function.
1985 *
1986 * If this function returns an error status, the operation becomes inactive.
1987 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001988 * \param[in,out] operation Active cipher operation.
1989 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001990 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001991 * \param[out] iv_length On success, the number of bytes of the
1992 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001993 *
1994 * \retval #PSA_SUCCESS
1995 * Success.
1996 * \retval #PSA_ERROR_BAD_STATE
1997 * The operation state is not valid (not started, or IV already set).
1998 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001999 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002000 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2001 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2002 * \retval #PSA_ERROR_HARDWARE_FAILURE
2003 * \retval #PSA_ERROR_TAMPERING_DETECTED
2004 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002005psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
2006 unsigned char *iv,
2007 size_t iv_size,
2008 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002009
Gilles Peskinedcd14942018-07-12 00:30:52 +02002010/** Set the IV for a symmetric encryption or decryption operation.
2011 *
2012 * This function sets the random IV (initialization vector), nonce
2013 * or initial counter value for the encryption or decryption operation.
2014 *
2015 * The application must call psa_cipher_encrypt_setup() before
2016 * calling this function.
2017 *
2018 * If this function returns an error status, the operation becomes inactive.
2019 *
2020 * \note When encrypting, applications should use psa_cipher_generate_iv()
2021 * instead of this function, unless implementing a protocol that requires
2022 * a non-random IV.
2023 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002024 * \param[in,out] operation Active cipher operation.
2025 * \param[in] iv Buffer containing the IV to use.
2026 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002027 *
2028 * \retval #PSA_SUCCESS
2029 * Success.
2030 * \retval #PSA_ERROR_BAD_STATE
2031 * The operation state is not valid (not started, or IV already set).
2032 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002033 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02002034 * or the chosen algorithm does not use an IV.
2035 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2036 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2037 * \retval #PSA_ERROR_HARDWARE_FAILURE
2038 * \retval #PSA_ERROR_TAMPERING_DETECTED
2039 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002040psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
2041 const unsigned char *iv,
2042 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002043
Gilles Peskinedcd14942018-07-12 00:30:52 +02002044/** Encrypt or decrypt a message fragment in an active cipher operation.
2045 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02002046 * Before calling this function, you must:
2047 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
2048 * The choice of setup function determines whether this function
2049 * encrypts or decrypts its input.
2050 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
2051 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02002052 *
2053 * If this function returns an error status, the operation becomes inactive.
2054 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002055 * \param[in,out] operation Active cipher operation.
2056 * \param[in] input Buffer containing the message fragment to
2057 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002058 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002059 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002060 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002061 * \param[out] output_length On success, the number of bytes
2062 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002063 *
2064 * \retval #PSA_SUCCESS
2065 * Success.
2066 * \retval #PSA_ERROR_BAD_STATE
2067 * The operation state is not valid (not started, IV required but
2068 * not set, or already completed).
2069 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2070 * The size of the \p output buffer is too small.
2071 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2072 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2073 * \retval #PSA_ERROR_HARDWARE_FAILURE
2074 * \retval #PSA_ERROR_TAMPERING_DETECTED
2075 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002076psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2077 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02002078 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002079 unsigned char *output,
2080 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002081 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002082
Gilles Peskinedcd14942018-07-12 00:30:52 +02002083/** Finish encrypting or decrypting a message in a cipher operation.
2084 *
2085 * The application must call psa_cipher_encrypt_setup() or
2086 * psa_cipher_decrypt_setup() before calling this function. The choice
2087 * of setup function determines whether this function encrypts or
2088 * decrypts its input.
2089 *
2090 * This function finishes the encryption or decryption of the message
2091 * formed by concatenating the inputs passed to preceding calls to
2092 * psa_cipher_update().
2093 *
2094 * When this function returns, the operation becomes inactive.
2095 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002096 * \param[in,out] operation Active cipher operation.
2097 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002098 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002099 * \param[out] output_length On success, the number of bytes
2100 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002101 *
2102 * \retval #PSA_SUCCESS
2103 * Success.
2104 * \retval #PSA_ERROR_BAD_STATE
2105 * The operation state is not valid (not started, IV required but
2106 * not set, or already completed).
2107 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2108 * The size of the \p output buffer is too small.
2109 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2110 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2111 * \retval #PSA_ERROR_HARDWARE_FAILURE
2112 * \retval #PSA_ERROR_TAMPERING_DETECTED
2113 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002114psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002115 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002116 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002117 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002118
Gilles Peskinedcd14942018-07-12 00:30:52 +02002119/** Abort a cipher operation.
2120 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002121 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002122 * \p operation structure itself. Once aborted, the operation object
2123 * can be reused for another operation by calling
2124 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002125 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002126 * You may call this function any time after the operation object has
2127 * been initialized by any of the following methods:
2128 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2129 * whether it succeeds or not.
2130 * - Initializing the \c struct to all-bits-zero.
2131 * - Initializing the \c struct to logical zeros, e.g.
2132 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002133 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002134 * In particular, calling psa_cipher_abort() after the operation has been
2135 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2136 * is safe and has no effect.
2137 *
2138 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002139 *
2140 * \retval #PSA_SUCCESS
2141 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002142 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002143 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2144 * \retval #PSA_ERROR_HARDWARE_FAILURE
2145 * \retval #PSA_ERROR_TAMPERING_DETECTED
2146 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002147psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2148
2149/**@}*/
2150
Gilles Peskine3b555712018-03-03 21:27:57 +01002151/** \defgroup aead Authenticated encryption with associated data (AEAD)
2152 * @{
2153 */
2154
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002155/** The tag size for an AEAD algorithm, in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01002156 *
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002157 * \param alg An AEAD algorithm
2158 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002159 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002160 *
2161 * \return The tag size for the specified algorithm.
2162 * If the AEAD algorithm does not have an identified
2163 * tag that can be distinguished from the rest of
2164 * the ciphertext, return 0.
2165 * If the AEAD algorithm is not recognized, return 0.
2166 * An implementation may return either 0 or a
2167 * correct size for an AEAD algorithm that it
2168 * recognizes, but does not support.
2169 */
2170#define PSA_AEAD_TAG_SIZE(alg) \
2171 ((alg) == PSA_ALG_GCM ? 16 : \
2172 (alg) == PSA_ALG_CCM ? 16 : \
2173 0)
Gilles Peskine3b555712018-03-03 21:27:57 +01002174
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002175/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002176 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002177 * \param key Slot containing the key to use.
2178 * \param alg The AEAD algorithm to compute
2179 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002180 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002181 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002182 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002183 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002184 * but not encrypted.
2185 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002186 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002187 * encrypted.
2188 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002189 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002190 * encrypted data. The additional data is not
2191 * part of this output. For algorithms where the
2192 * encrypted data and the authentication tag
2193 * are defined as separate outputs, the
2194 * authentication tag is appended to the
2195 * encrypted data.
2196 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2197 * This must be at least
2198 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2199 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002200 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002201 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002202 *
Gilles Peskine28538492018-07-11 17:34:00 +02002203 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002204 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002205 * \retval #PSA_ERROR_EMPTY_SLOT
2206 * \retval #PSA_ERROR_NOT_PERMITTED
2207 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002208 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002209 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002210 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002211 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2212 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2213 * \retval #PSA_ERROR_HARDWARE_FAILURE
2214 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine3b555712018-03-03 21:27:57 +01002215 */
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002216psa_status_t psa_aead_encrypt(psa_key_slot_t key,
2217 psa_algorithm_t alg,
2218 const uint8_t *nonce,
2219 size_t nonce_length,
2220 const uint8_t *additional_data,
2221 size_t additional_data_length,
2222 const uint8_t *plaintext,
2223 size_t plaintext_length,
2224 uint8_t *ciphertext,
2225 size_t ciphertext_size,
2226 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002227
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002228/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002229 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002230 * \param key Slot containing the key to use.
2231 * \param alg The AEAD algorithm to compute
2232 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002233 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002234 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002235 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002236 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002237 * but not encrypted.
2238 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002239 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002240 * encrypted. For algorithms where the
2241 * encrypted data and the authentication tag
2242 * are defined as separate inputs, the buffer
2243 * must contain the encrypted data followed
2244 * by the authentication tag.
2245 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002246 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002247 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2248 * This must be at least
2249 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2250 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002251 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03002252 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002253 *
Gilles Peskine28538492018-07-11 17:34:00 +02002254 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002255 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002256 * \retval #PSA_ERROR_EMPTY_SLOT
2257 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002258 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002259 * \retval #PSA_ERROR_NOT_PERMITTED
2260 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002261 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002262 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002263 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002264 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2265 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2266 * \retval #PSA_ERROR_HARDWARE_FAILURE
2267 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine3b555712018-03-03 21:27:57 +01002268 */
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002269psa_status_t psa_aead_decrypt(psa_key_slot_t key,
2270 psa_algorithm_t alg,
2271 const uint8_t *nonce,
2272 size_t nonce_length,
2273 const uint8_t *additional_data,
2274 size_t additional_data_length,
2275 const uint8_t *ciphertext,
2276 size_t ciphertext_length,
2277 uint8_t *plaintext,
2278 size_t plaintext_size,
2279 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002280
2281/**@}*/
2282
Gilles Peskine20035e32018-02-03 22:44:14 +01002283/** \defgroup asymmetric Asymmetric cryptography
2284 * @{
2285 */
2286
2287/**
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002288 * \brief ECDSA signature size for a given curve bit size
Gilles Peskine0189e752018-02-03 23:57:22 +01002289 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002290 * \param curve_bits Curve size in bits.
2291 * \return Signature size in bytes.
Gilles Peskine0189e752018-02-03 23:57:22 +01002292 *
2293 * \note This macro returns a compile-time constant if its argument is one.
Gilles Peskine0189e752018-02-03 23:57:22 +01002294 */
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002295#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
2296 (PSA_BITS_TO_BYTES(curve_bits) * 2)
Gilles Peskine0189e752018-02-03 23:57:22 +01002297
Gilles Peskine0189e752018-02-03 23:57:22 +01002298/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002299 * \brief Sign a hash or short message with a private key.
2300 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002301 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002302 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002303 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2304 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2305 * to determine the hash algorithm to use.
2306 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002307 * \param key Key slot containing an asymmetric key pair.
2308 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002309 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002310 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002311 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002312 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002313 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002314 * \param[out] signature_length On success, the number of bytes
2315 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002316 *
Gilles Peskine28538492018-07-11 17:34:00 +02002317 * \retval #PSA_SUCCESS
2318 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002319 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002320 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002321 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002322 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002323 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002324 * \retval #PSA_ERROR_NOT_SUPPORTED
2325 * \retval #PSA_ERROR_INVALID_ARGUMENT
2326 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2327 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2328 * \retval #PSA_ERROR_HARDWARE_FAILURE
2329 * \retval #PSA_ERROR_TAMPERING_DETECTED
2330 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01002331 */
2332psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
2333 psa_algorithm_t alg,
2334 const uint8_t *hash,
2335 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002336 uint8_t *signature,
2337 size_t signature_size,
2338 size_t *signature_length);
2339
2340/**
2341 * \brief Verify the signature a hash or short message using a public key.
2342 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002343 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002344 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002345 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2346 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2347 * to determine the hash algorithm to use.
2348 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01002349 * \param key Key slot containing a public key or an
2350 * asymmetric key pair.
2351 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002352 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002353 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002354 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002355 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002356 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002357 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002358 *
Gilles Peskine28538492018-07-11 17:34:00 +02002359 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002360 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002361 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002362 * The calculation was perfomed successfully, but the passed
2363 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002364 * \retval #PSA_ERROR_NOT_SUPPORTED
2365 * \retval #PSA_ERROR_INVALID_ARGUMENT
2366 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2367 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2368 * \retval #PSA_ERROR_HARDWARE_FAILURE
2369 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01002370 */
2371psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
2372 psa_algorithm_t alg,
2373 const uint8_t *hash,
2374 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002375 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002376 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002377
Gilles Peskine723feff2018-05-31 20:08:13 +02002378#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
Gilles Peskine072ac562018-06-30 00:21:29 +02002379 (PSA_ALG_IS_RSA_OAEP(alg) ? \
2380 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskine723feff2018-05-31 20:08:13 +02002381 11 /*PKCS#1v1.5*/)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002382
2383/**
2384 * \brief Encrypt a short message with a public key.
2385 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002386 * \param key Key slot containing a public key or an
2387 * asymmetric key pair.
2388 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002389 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002390 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002391 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002392 * \param[in] salt A salt or label, if supported by the
2393 * encryption algorithm.
2394 * If the algorithm does not support a
2395 * salt, pass \c NULL.
2396 * If the algorithm supports an optional
2397 * salt and you do not want to pass a salt,
2398 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002399 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002400 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2401 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002402 * \param salt_length Size of the \p salt buffer in bytes.
2403 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002404 * \param[out] output Buffer where the encrypted message is to
2405 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002406 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002407 * \param[out] output_length On success, the number of bytes
2408 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002409 *
Gilles Peskine28538492018-07-11 17:34:00 +02002410 * \retval #PSA_SUCCESS
2411 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002412 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002413 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002414 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002415 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002416 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002417 * \retval #PSA_ERROR_NOT_SUPPORTED
2418 * \retval #PSA_ERROR_INVALID_ARGUMENT
2419 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2420 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2421 * \retval #PSA_ERROR_HARDWARE_FAILURE
2422 * \retval #PSA_ERROR_TAMPERING_DETECTED
2423 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002424 */
2425psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
2426 psa_algorithm_t alg,
2427 const uint8_t *input,
2428 size_t input_length,
2429 const uint8_t *salt,
2430 size_t salt_length,
2431 uint8_t *output,
2432 size_t output_size,
2433 size_t *output_length);
2434
2435/**
2436 * \brief Decrypt a short message with a private key.
2437 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002438 * \param key Key slot containing an asymmetric key pair.
2439 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002440 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002441 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002442 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002443 * \param[in] salt A salt or label, if supported by the
2444 * encryption algorithm.
2445 * If the algorithm does not support a
2446 * salt, pass \c NULL.
2447 * If the algorithm supports an optional
2448 * salt and you do not want to pass a salt,
2449 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002450 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002451 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2452 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002453 * \param salt_length Size of the \p salt buffer in bytes.
2454 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002455 * \param[out] output Buffer where the decrypted message is to
2456 * be written.
2457 * \param output_size Size of the \c output buffer in bytes.
2458 * \param[out] output_length On success, the number of bytes
2459 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002460 *
Gilles Peskine28538492018-07-11 17:34:00 +02002461 * \retval #PSA_SUCCESS
2462 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002463 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002464 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002465 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002466 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002467 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002468 * \retval #PSA_ERROR_NOT_SUPPORTED
2469 * \retval #PSA_ERROR_INVALID_ARGUMENT
2470 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2471 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2472 * \retval #PSA_ERROR_HARDWARE_FAILURE
2473 * \retval #PSA_ERROR_TAMPERING_DETECTED
2474 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2475 * \retval #PSA_ERROR_INVALID_PADDING
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002476 */
2477psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
2478 psa_algorithm_t alg,
2479 const uint8_t *input,
2480 size_t input_length,
2481 const uint8_t *salt,
2482 size_t salt_length,
2483 uint8_t *output,
2484 size_t output_size,
2485 size_t *output_length);
2486
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002487/**@}*/
2488
Gilles Peskineeab56e42018-07-12 17:12:33 +02002489/** \defgroup generation Generators
2490 * @{
2491 */
2492
2493/** The type of the state data structure for generators.
2494 *
2495 * Before calling any function on a generator, the application must
2496 * initialize it by any of the following means:
2497 * - Set the structure to all-bits-zero, for example:
2498 * \code
2499 * psa_crypto_generator_t generator;
2500 * memset(&generator, 0, sizeof(generator));
2501 * \endcode
2502 * - Initialize the structure to logical zero values, for example:
2503 * \code
2504 * psa_crypto_generator_t generator = {0};
2505 * \endcode
2506 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2507 * for example:
2508 * \code
2509 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2510 * \endcode
2511 * - Assign the result of the function psa_crypto_generator_init()
2512 * to the structure, for example:
2513 * \code
2514 * psa_crypto_generator_t generator;
2515 * generator = psa_crypto_generator_init();
2516 * \endcode
2517 *
2518 * This is an implementation-defined \c struct. Applications should not
2519 * make any assumptions about the content of this structure except
2520 * as directed by the documentation of a specific implementation.
2521 */
2522typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2523
2524/** \def PSA_CRYPTO_GENERATOR_INIT
2525 *
2526 * This macro returns a suitable initializer for a generator object
2527 * of type #psa_crypto_generator_t.
2528 */
2529#ifdef __DOXYGEN_ONLY__
2530/* This is an example definition for documentation purposes.
2531 * Implementations should define a suitable value in `crypto_struct.h`.
2532 */
2533#define PSA_CRYPTO_GENERATOR_INIT {0}
2534#endif
2535
2536/** Return an initial value for a generator object.
2537 */
2538static psa_crypto_generator_t psa_crypto_generator_init(void);
2539
2540/** Retrieve the current capacity of a generator.
2541 *
2542 * The capacity of a generator is the maximum number of bytes that it can
2543 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2544 *
2545 * \param[in] generator The generator to query.
2546 * \param[out] capacity On success, the capacity of the generator.
2547 *
2548 * \retval PSA_SUCCESS
2549 * \retval PSA_ERROR_BAD_STATE
2550 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2551 */
2552psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2553 size_t *capacity);
2554
2555/** Read some data from a generator.
2556 *
2557 * This function reads and returns a sequence of bytes from a generator.
2558 * The data that is read is discarded from the generator. The generator's
2559 * capacity is decreased by the number of bytes read.
2560 *
2561 * \param[in,out] generator The generator object to read from.
2562 * \param[out] output Buffer where the generator output will be
2563 * written.
2564 * \param output_length Number of bytes to output.
2565 *
2566 * \retval PSA_SUCCESS
2567 * \retval PSA_ERROR_INSUFFICIENT_CAPACITY
2568 * There were fewer than \p output_length bytes
2569 * in the generator. Note that in this case, no
2570 * output is written to the output buffer.
2571 * The generator's capacity is set to 0, thus
2572 * subsequent calls to this function will not
2573 * succeed, even with a smaller output buffer.
2574 * \retval PSA_ERROR_BAD_STATE
2575 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
2576 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2577 * \retval PSA_ERROR_HARDWARE_FAILURE
2578 * \retval PSA_ERROR_TAMPERING_DETECTED
2579 */
2580psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2581 uint8_t *output,
2582 size_t output_length);
2583
2584/** Create a symmetric key from data read from a generator.
2585 *
2586 * This function reads a sequence of bytes from a generator and imports
2587 * these bytes as a key.
2588 * The data that is read is discarded from the generator. The generator's
2589 * capacity is decreased by the number of bytes read.
2590 *
2591 * This function is equivalent to calling #psa_generator_read and
2592 * passing the resulting output to #psa_import_key, but
2593 * if the implementation provides an isolation boundary then
2594 * the key material is not exposed outside the isolation boundary.
2595 *
2596 * \param key Slot where the key will be stored. This must be a
2597 * valid slot for a key of the chosen type. It must
2598 * be unoccupied.
2599 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2600 * This must be a symmetric key type.
2601 * \param bits Key size in bits.
2602 * \param[in,out] generator The generator object to read from.
2603 *
2604 * \retval PSA_SUCCESS
2605 * Success.
2606 * \retval PSA_ERROR_INSUFFICIENT_CAPACITY
2607 * There were fewer than \p output_length bytes
2608 * in the generator. Note that in this case, no
2609 * output is written to the output buffer.
2610 * The generator's capacity is set to 0, thus
2611 * subsequent calls to this function will not
2612 * succeed, even with a smaller output buffer.
2613 * \retval PSA_ERROR_NOT_SUPPORTED
2614 * The key type or key size is not supported, either by the
2615 * implementation in general or in this particular slot.
2616 * \retval PSA_ERROR_BAD_STATE
2617 * \retval PSA_ERROR_INVALID_ARGUMENT
2618 * The key slot is invalid.
2619 * \retval PSA_ERROR_OCCUPIED_SLOT
2620 * There is already a key in the specified slot.
2621 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
2622 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
2623 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2624 * \retval PSA_ERROR_HARDWARE_FAILURE
2625 * \retval PSA_ERROR_TAMPERING_DETECTED
2626 */
2627psa_status_t psa_generator_import_key(psa_key_slot_t key,
2628 psa_key_type_t type,
2629 size_t bits,
2630 psa_crypto_generator_t *generator);
2631
2632/** Abort a generator.
2633 *
2634 * Once a generator has been aborted, its capacity is zero.
2635 * Aborting a generator frees all associated resources except for the
2636 * \c generator structure itself.
2637 *
2638 * This function may be called at any time as long as the generator
2639 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2640 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2641 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2642 * on a generator that has not been set up.
2643 *
2644 * Once aborted, the generator object may be called.
2645 *
2646 * \param[in,out] generator The generator to abort.
2647 *
2648 * \retval PSA_SUCCESS
2649 * \retval PSA_ERROR_BAD_STATE
2650 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2651 * \retval PSA_ERROR_HARDWARE_FAILURE
2652 * \retval PSA_ERROR_TAMPERING_DETECTED
2653 */
2654psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2655
2656/**@}*/
2657
Gilles Peskineea0fb492018-07-12 17:17:20 +02002658/** \defgroup derivation Key derivation
2659 * @{
2660 */
2661
2662/** Set up a key derivation operation.
2663 *
2664 * A key derivation algorithm takes three inputs: a secret input \p key and
2665 * two non-secret inputs \p label and p salt.
2666 * The result of this function is a byte generator which can
2667 * be used to produce keys and other cryptographic material.
2668 *
2669 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02002670 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2671 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002672 *
2673 * \param[in,out] generator The generator object to set up. It must
2674 * have been initialized to .
2675 * \param key Slot containing the secret key to use.
2676 * \param alg The key derivation algorithm to compute
2677 * (\c PSA_ALG_XXX value such that
2678 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2679 * \param[in] salt Salt to use.
2680 * \param salt_length Size of the \p salt buffer in bytes.
2681 * \param[in] label Label to use.
2682 * \param label_length Size of the \p label buffer in bytes.
2683 * \param capacity The maximum number of bytes that the
2684 * generator will be able to provide.
2685 *
2686 * \retval #PSA_SUCCESS
2687 * Success.
2688 * \retval #PSA_ERROR_EMPTY_SLOT
2689 * \retval #PSA_ERROR_NOT_PERMITTED
2690 * \retval #PSA_ERROR_INVALID_ARGUMENT
2691 * \c key is not compatible with \c alg,
2692 * or \p capacity is too large for the specified algorithm and key.
2693 * \retval #PSA_ERROR_NOT_SUPPORTED
2694 * \c alg is not supported or is not a key derivation algorithm.
2695 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2696 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2697 * \retval #PSA_ERROR_HARDWARE_FAILURE
2698 * \retval #PSA_ERROR_TAMPERING_DETECTED
2699 */
2700psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
2701 psa_key_type_t key,
2702 psa_algorithm_t alg,
2703 const uint8_t *salt,
2704 size_t salt_length,
2705 const uint8_t *label,
2706 size_t label_length,
2707 size_t capacity);
2708
2709/**@}*/
2710
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002711/** \defgroup generation Key generation
2712 * @{
2713 */
2714
2715/**
2716 * \brief Generate random bytes.
2717 *
2718 * \warning This function **can** fail! Callers MUST check the return status
2719 * and MUST NOT use the content of the output buffer if the return
2720 * status is not #PSA_SUCCESS.
2721 *
2722 * \note To generate a key, use psa_generate_key() instead.
2723 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002724 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002725 * \param output_size Number of bytes to generate and output.
2726 *
Gilles Peskine28538492018-07-11 17:34:00 +02002727 * \retval #PSA_SUCCESS
2728 * \retval #PSA_ERROR_NOT_SUPPORTED
2729 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2730 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2731 * \retval #PSA_ERROR_HARDWARE_FAILURE
2732 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002733 */
2734psa_status_t psa_generate_random(uint8_t *output,
2735 size_t output_size);
2736
Gilles Peskine4c317f42018-07-12 01:24:09 +02002737/** Extra parameters for RSA key generation.
2738 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002739 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002740 * parameter to psa_generate_key().
2741 */
2742typedef struct {
2743 uint32_t e; /**! Public exponent value. Default: 65537. */
2744} psa_generate_key_extra_rsa;
2745
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002746/**
2747 * \brief Generate a key or key pair.
2748 *
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002749 * \param key Slot where the key will be stored. This must be a
2750 * valid slot for a key of the chosen type. It must
2751 * be unoccupied.
2752 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2753 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002754 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002755 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002756 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002757 * default parameters. Implementation that support
2758 * the generation of vendor-specific key types
2759 * that allow extra parameters shall document
2760 * the format of these extra parameters and
2761 * the default values. For standard parameters,
2762 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002763 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002764 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2765 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002766 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002767 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2768 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002769 * - For an RSA key (\p type is
2770 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2771 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002772 * specifying the public exponent. The
2773 * default public exponent used when \p extra
2774 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002775 * \param extra_size Size of the buffer that \p extra
2776 * points to, in bytes. Note that if \p extra is
2777 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002778 *
Gilles Peskine28538492018-07-11 17:34:00 +02002779 * \retval #PSA_SUCCESS
2780 * \retval #PSA_ERROR_NOT_SUPPORTED
2781 * \retval #PSA_ERROR_INVALID_ARGUMENT
2782 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2783 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2784 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2785 * \retval #PSA_ERROR_HARDWARE_FAILURE
2786 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002787 */
2788psa_status_t psa_generate_key(psa_key_slot_t key,
2789 psa_key_type_t type,
2790 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002791 const void *extra,
2792 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002793
2794/**@}*/
2795
Gilles Peskinee59236f2018-01-27 23:32:46 +01002796#ifdef __cplusplus
2797}
2798#endif
2799
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002800/* The file "crypto_sizes.h" contains definitions for size calculation
2801 * macros whose definitions are implementation-specific. */
2802#include "crypto_sizes.h"
2803
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002804/* The file "crypto_struct.h" contains definitions for
2805 * implementation-specific structs that are declared above. */
2806#include "crypto_struct.h"
2807
2808/* The file "crypto_extra.h" contains vendor-specific definitions. This
2809 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002810#include "crypto_extra.h"
2811
2812#endif /* PSA_CRYPTO_H */