blob: 4c615f0e11dcc1123070c6ddb4dab742a87a792a [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 Peskineedd76872018-07-20 17:42:05 +0200655/** SHA2-224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100656#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
Gilles Peskineedd76872018-07-20 17:42:05 +0200657/** SHA2-256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100658#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
Gilles Peskineedd76872018-07-20 17:42:05 +0200659/** SHA2-384 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100660#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
Gilles Peskineedd76872018-07-20 17:42:05 +0200661/** SHA2-512 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100662#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
Gilles Peskineedd76872018-07-20 17:42:05 +0200663/** SHA2-512/224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100664#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
Gilles Peskineedd76872018-07-20 17:42:05 +0200665/** SHA2-512/256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100666#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
Gilles Peskineedd76872018-07-20 17:42:05 +0200667/** SHA3-224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100668#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
Gilles Peskineedd76872018-07-20 17:42:05 +0200669/** SHA3-256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100670#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
Gilles Peskineedd76872018-07-20 17:42:05 +0200671/** SHA3-384 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100672#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
Gilles Peskineedd76872018-07-20 17:42:05 +0200673/** SHA3-512 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100674#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
675
Gilles Peskine8c9def32018-02-08 10:02:12 +0100676#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100677#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
Gilles Peskine35855962018-04-19 08:39:16 +0200678/** Macro to build an HMAC algorithm.
679 *
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200680 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
Gilles Peskine35855962018-04-19 08:39:16 +0200681 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200682 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200683 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine35855962018-04-19 08:39:16 +0200684 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200685 * \return The corresponding HMAC algorithm.
686 * \return Unspecified if \p alg is not a supported
687 * hash algorithm.
Gilles Peskine35855962018-04-19 08:39:16 +0200688 */
689#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100690 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200691
Gilles Peskine8c9def32018-02-08 10:02:12 +0100692#define PSA_ALG_HMAC_HASH(hmac_alg) \
693 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200694
695/** Whether the specified algorithm is an HMAC algorithm.
696 *
697 * HMAC is a family of MAC algorithms that are based on a hash function.
698 *
699 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
700 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200701 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
702 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200703 * algorithm identifier.
704 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100705#define PSA_ALG_IS_HMAC(alg) \
706 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
707 PSA_ALG_HMAC_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200708
Gilles Peskine8c9def32018-02-08 10:02:12 +0100709#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
710#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
711#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
712#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200713
714/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
715 *
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200716 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
717 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200718 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
719 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200720 * algorithm identifier.
721 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100722#define PSA_ALG_IS_CIPHER_MAC(alg) \
723 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
724 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100725
Gilles Peskine8c9def32018-02-08 10:02:12 +0100726#define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100727#define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100728#define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100729#define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200730
731/** Use a block cipher mode without padding.
732 *
733 * This padding mode may only be used with messages whose lengths are a
734 * whole number of blocks for the chosen block cipher.
735 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100736#define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200737
Gilles Peskine98f0a242018-02-06 18:57:29 +0100738#define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200739
740/** Whether the specified algorithm is a block cipher.
741 *
742 * A block cipher is a symmetric cipher that encrypts or decrypts messages
743 * by chopping them into fixed-size blocks. Processing a message requires
744 * applying a _padding mode_ to transform the message into one whose
745 * length is a whole number of blocks. To construct an algorithm
746 * identifier for a block cipher, apply a bitwise-or between the block
747 * cipher mode and the padding mode. For example, CBC with PKCS#7 padding
748 * is `PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7`.
749 *
750 * The transformation applied to each block is determined by the key type.
751 * For example, to use AES-128-CBC-PKCS7, use the algorithm above with
752 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
753 *
754 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
755 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200756 * \return 1 if \p alg is a block cipher algorithm, 0 otherwise.
757 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200758 * algorithm identifier or if it is not a symmetric cipher algorithm.
759 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100760#define PSA_ALG_IS_BLOCK_CIPHER(alg) \
761 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
762 PSA_ALG_BLOCK_CIPHER_BASE)
763
Gilles Peskinedcd14942018-07-12 00:30:52 +0200764/** The CBC block cipher mode.
765 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100766#define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100767#define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002)
768#define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003)
769#define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004)
Gilles Peskine5d1888e2018-07-12 00:32:42 +0200770
771#define PSA_ALG_STREAM_CIPHER_BASE ((psa_algorithm_t)0x04800000)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200772
Gilles Peskinedcd14942018-07-12 00:30:52 +0200773/** The CTR stream cipher mode.
774 *
775 * CTR is a stream cipher which is built from a block cipher. The
776 * underlying block cipher is determined by the key type. For example,
777 * to use AES-128-CTR, use this algorithm with
778 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
779 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100780#define PSA_ALG_CTR ((psa_algorithm_t)0x04800001)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200781
Gilles Peskinedcd14942018-07-12 00:30:52 +0200782/** The ARC4 stream cipher algorithm.
783 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100784#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100785
Gilles Peskinedcd14942018-07-12 00:30:52 +0200786/** Whether the specified algorithm is a stream cipher.
787 *
788 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
789 * by applying a bitwise-xor with a stream of bytes that is generated
790 * from a key.
791 *
792 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
793 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200794 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
795 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200796 * algorithm identifier or if it is not a symmetric cipher algorithm.
797 */
Moran Pekerbed71a22018-04-22 20:19:20 +0300798#define PSA_ALG_IS_STREAM_CIPHER(alg) \
799 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
Gilles Peskine5d1888e2018-07-12 00:32:42 +0200800 PSA_ALG_STREAM_CIPHER_BASE)
Moran Pekerbed71a22018-04-22 20:19:20 +0300801
Gilles Peskine8c9def32018-02-08 10:02:12 +0100802#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
803#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100804
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200805#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
806/** RSA PKCS#1 v1.5 signature with hashing.
807 *
808 * This is the signature scheme defined by RFC 8017
809 * (PKCS#1: RSA Cryptography Specifications) under the name
810 * RSASSA-PKCS1-v1_5.
811 *
812 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200813 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200814 *
815 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
816 * \return Unspecified if \p alg is not a supported
817 * hash algorithm.
818 */
Gilles Peskinea5926232018-03-28 14:16:50 +0200819#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200820 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
821/** Raw PKCS#1 v1.5 signature.
822 *
823 * The input to this algorithm is the DigestInfo structure used by
824 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
825 * steps 3&ndash;6.
826 */
827#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
Gilles Peskinea5926232018-03-28 14:16:50 +0200828#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200829 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200830
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200831#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
832/** RSA PSS signature with hashing.
833 *
834 * This is the signature scheme defined by RFC 8017
835 * (PKCS#1: RSA Cryptography Specifications) under the name
Gilles Peskinea4d20bd2018-06-29 23:35:02 +0200836 * RSASSA-PSS, with the message generation function MGF1, and with
837 * a salt length equal to the length of the hash. The specified
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200838 * hash algorithm is used to hash the input message, to create the
839 * salted hash, and for the mask generation.
840 *
841 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200842 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200843 *
844 * \return The corresponding RSA PSS signature algorithm.
845 * \return Unspecified if \p alg is not a supported
846 * hash algorithm.
847 */
848#define PSA_ALG_RSA_PSS(hash_alg) \
849 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
850#define PSA_ALG_IS_RSA_PSS(alg) \
851 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
852
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200853#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
854/** DSA signature with hashing.
855 *
856 * This is the signature scheme defined by FIPS 186-4,
857 * with a random per-message secret number (*k*).
858 *
859 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200860 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200861 *
862 * \return The corresponding DSA signature algorithm.
863 * \return Unspecified if \p alg is not a supported
864 * hash algorithm.
865 */
866#define PSA_ALG_DSA(hash_alg) \
867 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
868#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
869#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
870#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
871 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
872#define PSA_ALG_IS_DSA(alg) \
873 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
874 PSA_ALG_DSA_BASE)
875#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
876 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +0200877#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
878 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
879#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
880 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200881
882#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
883/** ECDSA signature with hashing.
884 *
885 * This is the ECDSA signature scheme defined by ANSI X9.62,
886 * with a random per-message secret number (*k*).
887 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200888 * The representation of the signature as a byte string consists of
889 * the concatentation of the signature values *r* and *s*. Each of
890 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
891 * of the base point of the curve in octets. Each value is represented
892 * in big-endian order (most significant octet first).
893 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200894 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200895 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200896 *
897 * \return The corresponding ECDSA signature algorithm.
898 * \return Unspecified if \p alg is not a supported
899 * hash algorithm.
900 */
901#define PSA_ALG_ECDSA(hash_alg) \
902 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
903/** ECDSA signature without hashing.
904 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200905 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200906 * without specifying a hash algorithm. This algorithm may only be
907 * used to sign or verify a sequence of bytes that should be an
908 * already-calculated hash. Note that the input is padded with
909 * zeros on the left or truncated on the left as required to fit
910 * the curve size.
911 */
912#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
913#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
914/** Deterministic ECDSA signature with hashing.
915 *
916 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
917 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200918 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
919 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200920 * Note that when this algorithm is used for verification, signatures
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200921 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200922 * same private key are accepted. In other words,
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200923 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
924 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200925 *
926 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200927 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200928 *
929 * \return The corresponding deterministic ECDSA signature
930 * algorithm.
931 * \return Unspecified if \p alg is not a supported
932 * hash algorithm.
933 */
934#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
935 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
936#define PSA_ALG_IS_ECDSA(alg) \
937 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
938 PSA_ALG_ECDSA_BASE)
939#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
940 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +0200941#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
942 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
943#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
944 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200945
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200946/** Get the hash used by a hash-and-sign signature algorithm.
947 *
948 * A hash-and-sign algorithm is a signature algorithm which is
949 * composed of two phases: first a hashing phase which does not use
950 * the key and produces a hash of the input message, then a signing
951 * phase which only uses the hash and the key and not the message
952 * itself.
953 *
954 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200955 * #PSA_ALG_IS_SIGN(\p alg) is true).
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200956 *
957 * \return The underlying hash algorithm if \p alg is a hash-and-sign
958 * algorithm.
959 * \return 0 if \p alg is a signature algorithm that does not
960 * follow the hash-and-sign structure.
961 * \return Unspecified if \p alg is not a signature algorithm or
962 * if it is not supported by the implementation.
963 */
964#define PSA_ALG_SIGN_GET_HASH(alg) \
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200965 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
966 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg) ? \
Gilles Peskine54622ae2018-06-29 22:24:24 +0200967 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200968 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
969 0)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100970
Gilles Peskinedcd14942018-07-12 00:30:52 +0200971/** RSA PKCS#1 v1.5 encryption.
972 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200973#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200974
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200975#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200976/** RSA OAEP encryption.
977 *
978 * This is the encryption scheme defined by RFC 8017
979 * (PKCS#1: RSA Cryptography Specifications) under the name
980 * RSAES-OAEP, with the message generation function MGF1.
981 *
982 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
983 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
984 * for MGF1.
985 *
986 * \return The corresponding RSA OAEP signature algorithm.
987 * \return Unspecified if \p alg is not a supported
988 * hash algorithm.
989 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200990#define PSA_ALG_RSA_OAEP(hash_alg) \
991 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
992#define PSA_ALG_IS_RSA_OAEP(alg) \
993 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
Gilles Peskine072ac562018-06-30 00:21:29 +0200994#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
995 (PSA_ALG_IS_RSA_OAEP(alg) ? \
996 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
997 0)
Gilles Peskined1e8e412018-06-07 09:49:39 +0200998
Gilles Peskinebef7f142018-07-12 17:22:21 +0200999#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x30000100)
1000/** Macro to build an HKDF algorithm.
1001 *
1002 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1003 *
1004 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1005 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1006 *
1007 * \return The corresponding HKDF algorithm.
1008 * \return Unspecified if \p alg is not a supported
1009 * hash algorithm.
1010 */
1011#define PSA_ALG_HKDF(hash_alg) \
1012 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1013/** Whether the specified algorithm is an HKDF algorithm.
1014 *
1015 * HKDF is a family of key derivation algorithms that are based on a hash
1016 * function and the HMAC construction.
1017 *
1018 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1019 *
1020 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1021 * This macro may return either 0 or 1 if \c alg is not a supported
1022 * key derivation algorithm identifier.
1023 */
1024#define PSA_ALG_IS_HKDF(alg) \
1025 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1026#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1027 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1028
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001029/**@}*/
1030
1031/** \defgroup key_management Key management
1032 * @{
1033 */
1034
1035/**
1036 * \brief Import a key in binary format.
1037 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +01001038 * This function supports any output from psa_export_key(). Refer to the
1039 * documentation of psa_export_key() for the format for each key type.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001040 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001041 * \param key Slot where the key will be stored. This must be a
1042 * valid slot for a key of the chosen type. It must
1043 * be unoccupied.
1044 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001045 * \param[in] data Buffer containing the key data.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001046 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001047 *
Gilles Peskine28538492018-07-11 17:34:00 +02001048 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001049 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001050 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001051 * The key type or key size is not supported, either by the
1052 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001053 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +01001054 * The key slot is invalid,
1055 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +02001056 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001057 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001058 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1059 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1060 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1061 * \retval #PSA_ERROR_HARDWARE_FAILURE
1062 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001063 */
1064psa_status_t psa_import_key(psa_key_slot_t key,
1065 psa_key_type_t type,
1066 const uint8_t *data,
1067 size_t data_length);
1068
1069/**
Gilles Peskine154bd952018-04-19 08:38:16 +02001070 * \brief Destroy a key and restore the slot to its default state.
1071 *
1072 * This function destroys the content of the key slot from both volatile
1073 * memory and, if applicable, non-volatile storage. Implementations shall
1074 * make a best effort to ensure that any previous content of the slot is
1075 * unrecoverable.
1076 *
1077 * This function also erases any metadata such as policies. It returns the
1078 * specified slot to its default state.
1079 *
1080 * \param key The key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001081 *
Gilles Peskine28538492018-07-11 17:34:00 +02001082 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +02001083 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +02001084 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001085 * The slot holds content and cannot be erased because it is
1086 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskine28538492018-07-11 17:34:00 +02001087 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001088 * The specified slot number does not designate a valid slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001089 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001090 * There was an failure in communication with the cryptoprocessor.
1091 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +02001092 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001093 * The storage is corrupted. Implementations shall make a best effort
1094 * to erase key material even in this stage, however applications
1095 * should be aware that it may be impossible to guarantee that the
1096 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +02001097 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001098 * An unexpected condition which is not a storage corruption or
1099 * a communication failure occurred. The cryptoprocessor may have
1100 * been compromised.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001101 */
1102psa_status_t psa_destroy_key(psa_key_slot_t key);
1103
1104/**
1105 * \brief Get basic metadata about a key.
1106 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001107 * \param key Slot whose content is queried. This must
1108 * be an occupied key slot.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001109 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001110 * This may be a null pointer, in which case the key type
1111 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001112 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +01001113 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +01001114 * is not written.
1115 *
Gilles Peskine28538492018-07-11 17:34:00 +02001116 * \retval #PSA_SUCCESS
1117 * \retval #PSA_ERROR_EMPTY_SLOT
1118 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1119 * \retval #PSA_ERROR_HARDWARE_FAILURE
1120 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001121 */
1122psa_status_t psa_get_key_information(psa_key_slot_t key,
1123 psa_key_type_t *type,
1124 size_t *bits);
1125
1126/**
1127 * \brief Export a key in binary format.
1128 *
1129 * The output of this function can be passed to psa_import_key() to
1130 * create an equivalent object.
1131 *
1132 * If a key is created with psa_import_key() and then exported with
1133 * this function, it is not guaranteed that the resulting data is
1134 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +01001135 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001136 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001137 * For standard key types, the output format is as follows:
1138 *
1139 * - For symmetric keys (including MAC keys), the format is the
1140 * raw bytes of the key.
1141 * - For DES, the key data consists of 8 bytes. The parity bits must be
1142 * correct.
1143 * - For Triple-DES, the format is the concatenation of the
1144 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +01001145 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine2743e422018-06-27 22:57:11 +02001146 * is the non-encrypted DER representation defined by PKCS\#1 (RFC 8017)
1147 * as RSAPrivateKey.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001148 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
Gilles Peskine971f7062018-03-20 17:52:58 +01001149 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001150 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001151 * \param key Slot whose content is to be exported. This must
1152 * be an occupied key slot.
1153 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001154 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001155 * \param[out] data_length On success, the number of bytes
1156 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001157 *
Gilles Peskine28538492018-07-11 17:34:00 +02001158 * \retval #PSA_SUCCESS
1159 * \retval #PSA_ERROR_EMPTY_SLOT
1160 * \retval #PSA_ERROR_NOT_PERMITTED
1161 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1162 * \retval #PSA_ERROR_HARDWARE_FAILURE
1163 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001164 */
1165psa_status_t psa_export_key(psa_key_slot_t key,
1166 uint8_t *data,
1167 size_t data_size,
1168 size_t *data_length);
1169
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001170/**
1171 * \brief Export a public key or the public part of a key pair in binary format.
1172 *
1173 * The output of this function can be passed to psa_import_key() to
1174 * create an object that is equivalent to the public key.
1175 *
1176 * For standard key types, the output format is as follows:
1177 *
1178 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
Moran Pekerdd4ea382018-04-03 15:30:03 +03001179 * the format is the DER representation of the public key defined by RFC 5280
Gilles Peskine971f7062018-03-20 17:52:58 +01001180 * as SubjectPublicKeyInfo.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001181 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001182 * \param key Slot whose content is to be exported. This must
1183 * be an occupied key slot.
1184 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001185 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001186 * \param[out] data_length On success, the number of bytes
1187 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001188 *
Gilles Peskine28538492018-07-11 17:34:00 +02001189 * \retval #PSA_SUCCESS
1190 * \retval #PSA_ERROR_EMPTY_SLOT
1191 * \retval #PSA_ERROR_INVALID_ARGUMENT
1192 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1193 * \retval #PSA_ERROR_HARDWARE_FAILURE
1194 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001195 */
1196psa_status_t psa_export_public_key(psa_key_slot_t key,
1197 uint8_t *data,
1198 size_t data_size,
1199 size_t *data_length);
1200
1201/**@}*/
1202
1203/** \defgroup policy Key policies
1204 * @{
1205 */
1206
1207/** \brief Encoding of permitted usage on a key. */
1208typedef uint32_t psa_key_usage_t;
1209
Gilles Peskine7e198532018-03-08 07:50:30 +01001210/** Whether the key may be exported.
1211 *
1212 * A public key or the public part of a key pair may always be exported
1213 * regardless of the value of this permission flag.
1214 *
1215 * If a key does not have export permission, implementations shall not
1216 * allow the key to be exported in plain form from the cryptoprocessor,
1217 * whether through psa_export_key() or through a proprietary interface.
1218 * The key may however be exportable in a wrapped form, i.e. in a form
1219 * where it is encrypted by another key.
1220 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001221#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1222
Gilles Peskine7e198532018-03-08 07:50:30 +01001223/** Whether the key may be used to encrypt a message.
1224 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001225 * This flag allows the key to be used for a symmetric encryption operation,
1226 * for an AEAD encryption-and-authentication operation,
1227 * or for an asymmetric encryption operation,
1228 * if otherwise permitted by the key's type and policy.
1229 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001230 * For a key pair, this concerns the public key.
1231 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001232#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +01001233
1234/** Whether the key may be used to decrypt a message.
1235 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001236 * This flag allows the key to be used for a symmetric decryption operation,
1237 * for an AEAD decryption-and-verification operation,
1238 * or for an asymmetric decryption 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_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +01001244
1245/** Whether the key may be used to sign a message.
1246 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001247 * This flag allows the key to be used for a MAC calculation operation
1248 * or for an asymmetric signature operation,
1249 * if otherwise permitted by the key's type and policy.
1250 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001251 * For a key pair, this concerns the private key.
1252 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001253#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +01001254
1255/** Whether the key may be used to verify a message signature.
1256 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001257 * This flag allows the key to be used for a MAC verification operation
1258 * or for an asymmetric signature verification operation,
1259 * if otherwise permitted by by the key's type and policy.
1260 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001261 * For a key pair, this concerns the public key.
1262 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001263#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
1264
Gilles Peskineea0fb492018-07-12 17:17:20 +02001265/** Whether the key may be used to derive other keys.
1266 */
1267#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00001000)
1268
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001269/** The type of the key policy data structure.
1270 *
1271 * This is an implementation-defined \c struct. Applications should not
1272 * make any assumptions about the content of this structure except
1273 * as directed by the documentation of a specific implementation. */
1274typedef struct psa_key_policy_s psa_key_policy_t;
1275
1276/** \brief Initialize a key policy structure to a default that forbids all
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001277 * usage of the key.
1278 *
1279 * \param[out] policy The policy object to initialize.
1280 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001281void psa_key_policy_init(psa_key_policy_t *policy);
1282
Gilles Peskine7e198532018-03-08 07:50:30 +01001283/** \brief Set the standard fields of a policy structure.
1284 *
1285 * Note that this function does not make any consistency check of the
1286 * parameters. The values are only checked when applying the policy to
1287 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001288 *
1289 * \param[out] policy The policy object to modify.
1290 * \param usage The permitted uses for the key.
1291 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +01001292 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001293void psa_key_policy_set_usage(psa_key_policy_t *policy,
1294 psa_key_usage_t usage,
1295 psa_algorithm_t alg);
1296
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001297/** \brief Retrieve the usage field of a policy structure.
1298 *
1299 * \param[in] policy The policy object to query.
1300 *
1301 * \return The permitted uses for a key with this policy.
1302 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001303psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001304
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001305/** \brief Retrieve the algorithm field of a policy structure.
1306 *
1307 * \param[in] policy The policy object to query.
1308 *
1309 * \return The permitted algorithm for a key with this policy.
1310 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001311psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001312
1313/** \brief Set the usage policy on a key slot.
1314 *
1315 * This function must be called on an empty key slot, before importing,
1316 * generating or creating a key in the slot. Changing the policy of an
1317 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +01001318 *
1319 * Implementations may set restrictions on supported key policies
1320 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001321 *
1322 * \param key The key slot whose policy is to be changed.
1323 * \param[in] policy The policy object to query.
1324 *
1325 * \retval #PSA_SUCCESS
1326 * \retval #PSA_ERROR_OCCUPIED_SLOT
1327 * \retval #PSA_ERROR_NOT_SUPPORTED
1328 * \retval #PSA_ERROR_INVALID_ARGUMENT
1329 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1330 * \retval #PSA_ERROR_HARDWARE_FAILURE
1331 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001332 */
1333psa_status_t psa_set_key_policy(psa_key_slot_t key,
1334 const psa_key_policy_t *policy);
1335
Gilles Peskine7e198532018-03-08 07:50:30 +01001336/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001337 *
1338 * \param key The key slot whose policy is being queried.
1339 * \param[out] policy On success, the key's policy.
1340 *
1341 * \retval #PSA_SUCCESS
1342 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1343 * \retval #PSA_ERROR_HARDWARE_FAILURE
1344 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7e198532018-03-08 07:50:30 +01001345 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001346psa_status_t psa_get_key_policy(psa_key_slot_t key,
1347 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +01001348
1349/**@}*/
1350
Gilles Peskine609b6a52018-03-03 21:31:50 +01001351/** \defgroup persistence Key lifetime
1352 * @{
1353 */
1354
1355/** Encoding of key lifetimes.
1356 */
1357typedef uint32_t psa_key_lifetime_t;
1358
1359/** A volatile key slot retains its content as long as the application is
1360 * running. It is guaranteed to be erased on a power reset.
1361 */
1362#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1363
1364/** A persistent key slot retains its content as long as it is not explicitly
1365 * destroyed.
1366 */
1367#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1368
1369/** A write-once key slot may not be modified once a key has been set.
1370 * It will retain its content as long as the device remains operational.
1371 */
1372#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
1373
Gilles Peskined393e182018-03-08 07:49:16 +01001374/** \brief Retrieve the lifetime of a key slot.
1375 *
1376 * The assignment of lifetimes to slots is implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001377 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001378 * \param key Slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001379 * \param[out] lifetime On success, the lifetime value.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001380 *
Gilles Peskine28538492018-07-11 17:34:00 +02001381 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001382 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001383 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603a7d245a2018-04-17 00:40:08 -07001384 * The key slot is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001385 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1386 * \retval #PSA_ERROR_HARDWARE_FAILURE
1387 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +01001388 */
Gilles Peskine609b6a52018-03-03 21:31:50 +01001389psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
1390 psa_key_lifetime_t *lifetime);
1391
Gilles Peskined393e182018-03-08 07:49:16 +01001392/** \brief Change the lifetime of a key slot.
1393 *
1394 * Whether the lifetime of a key slot can be changed at all, and if so
Gilles Peskine19067982018-03-20 17:54:53 +01001395 * whether the lifetime of an occupied key slot can be changed, is
Gilles Peskined393e182018-03-08 07:49:16 +01001396 * implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001397 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001398 * \param key Slot whose lifetime is to be changed.
1399 * \param lifetime The lifetime value to set for the given key slot.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001400 *
Gilles Peskine28538492018-07-11 17:34:00 +02001401 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001402 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001403 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603804cd712018-03-20 22:44:08 +02001404 * The key slot is invalid,
mohammad1603a7d245a2018-04-17 00:40:08 -07001405 * or the lifetime value is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001406 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001407 * The implementation does not support the specified lifetime value,
1408 * at least for the specified key slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001409 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001410 * The slot contains a key, and the implementation does not support
1411 * changing the lifetime of an occupied slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001412 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1413 * \retval #PSA_ERROR_HARDWARE_FAILURE
1414 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +01001415 */
1416psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
mohammad1603ea050092018-04-17 00:31:34 -07001417 psa_key_lifetime_t lifetime);
Gilles Peskined393e182018-03-08 07:49:16 +01001418
Gilles Peskine609b6a52018-03-03 21:31:50 +01001419/**@}*/
1420
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001421/** \defgroup hash Message digests
1422 * @{
1423 */
1424
Gilles Peskine308b91d2018-02-08 09:47:44 +01001425/** The type of the state data structure for multipart hash operations.
1426 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001427 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01001428 * make any assumptions about the content of this structure except
1429 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001430typedef struct psa_hash_operation_s psa_hash_operation_t;
1431
Gilles Peskine308b91d2018-02-08 09:47:44 +01001432/** The size of the output of psa_hash_finish(), in bytes.
1433 *
1434 * This is also the hash size that psa_hash_verify() expects.
1435 *
1436 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001437 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
Gilles Peskinebe42f312018-07-13 14:38:15 +02001438 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
Gilles Peskine35855962018-04-19 08:39:16 +02001439 * hash algorithm).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001440 *
1441 * \return The hash size for the specified hash algorithm.
1442 * If the hash algorithm is not recognized, return 0.
1443 * An implementation may return either 0 or the correct size
1444 * for a hash algorithm that it recognizes, but does not support.
1445 */
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001446#define PSA_HASH_SIZE(alg) \
1447 ( \
1448 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD2 ? 16 : \
1449 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD4 ? 16 : \
1450 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD5 ? 16 : \
1451 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
1452 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
1453 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
1454 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
1455 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
1456 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
1457 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
1458 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
1459 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
1460 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
1461 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
1462 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001463 0)
1464
Gilles Peskine308b91d2018-02-08 09:47:44 +01001465/** Start a multipart hash operation.
1466 *
1467 * The sequence of operations to calculate a hash (message digest)
1468 * is as follows:
1469 * -# Allocate an operation object which will be passed to all the functions
1470 * listed here.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001471 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001472 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001473 * of the message each time. The hash that is calculated is the hash
1474 * of the concatenation of these messages in order.
1475 * -# To calculate the hash, call psa_hash_finish().
1476 * To compare the hash with an expected value, call psa_hash_verify().
1477 *
1478 * The application may call psa_hash_abort() at any time after the operation
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001479 * has been initialized with psa_hash_setup().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001480 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001481 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001482 * eventually terminate the operation. The following events terminate an
1483 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001484 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001485 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001486 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001487 * \param[out] operation The operation object to use.
1488 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1489 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001490 *
Gilles Peskine28538492018-07-11 17:34:00 +02001491 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001492 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001493 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001494 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001495 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1496 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1497 * \retval #PSA_ERROR_HARDWARE_FAILURE
1498 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001499 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001500psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001501 psa_algorithm_t alg);
1502
Gilles Peskine308b91d2018-02-08 09:47:44 +01001503/** Add a message fragment to a multipart hash operation.
1504 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001505 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001506 *
1507 * If this function returns an error status, the operation becomes inactive.
1508 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001509 * \param[in,out] operation Active hash operation.
1510 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001511 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001512 *
Gilles Peskine28538492018-07-11 17:34:00 +02001513 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001514 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001515 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001516 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001517 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1518 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1519 * \retval #PSA_ERROR_HARDWARE_FAILURE
1520 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001521 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001522psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1523 const uint8_t *input,
1524 size_t input_length);
1525
Gilles Peskine308b91d2018-02-08 09:47:44 +01001526/** Finish the calculation of the hash of a message.
1527 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001528 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001529 * This function calculates the hash of the message formed by concatenating
1530 * the inputs passed to preceding calls to psa_hash_update().
1531 *
1532 * When this function returns, the operation becomes inactive.
1533 *
1534 * \warning Applications should not call this function if they expect
1535 * a specific value for the hash. Call psa_hash_verify() instead.
1536 * Beware that comparing integrity or authenticity data such as
1537 * hash values with a function such as \c memcmp is risky
1538 * because the time taken by the comparison may leak information
1539 * about the hashed data which could allow an attacker to guess
1540 * a valid hash and thereby bypass security controls.
1541 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001542 * \param[in,out] operation Active hash operation.
1543 * \param[out] hash Buffer where the hash is to be written.
1544 * \param hash_size Size of the \p hash buffer in bytes.
1545 * \param[out] hash_length On success, the number of bytes
1546 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001547 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001548 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001549 *
Gilles Peskine28538492018-07-11 17:34:00 +02001550 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001551 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001552 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001553 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001554 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001555 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001556 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001557 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001558 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1559 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1560 * \retval #PSA_ERROR_HARDWARE_FAILURE
1561 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001562 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001563psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1564 uint8_t *hash,
1565 size_t hash_size,
1566 size_t *hash_length);
1567
Gilles Peskine308b91d2018-02-08 09:47:44 +01001568/** Finish the calculation of the hash of a message and compare it with
1569 * an expected value.
1570 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001571 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001572 * This function calculates the hash of the message formed by concatenating
1573 * the inputs passed to preceding calls to psa_hash_update(). It then
1574 * compares the calculated hash with the expected hash passed as a
1575 * parameter to this function.
1576 *
1577 * When this function returns, the operation becomes inactive.
1578 *
Gilles Peskine19067982018-03-20 17:54:53 +01001579 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001580 * comparison between the actual hash and the expected hash is performed
1581 * in constant time.
1582 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001583 * \param[in,out] operation Active hash operation.
1584 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001585 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001586 *
Gilles Peskine28538492018-07-11 17:34:00 +02001587 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001588 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001589 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001590 * The hash of the message was calculated successfully, but it
1591 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001592 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001593 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001594 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1595 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1596 * \retval #PSA_ERROR_HARDWARE_FAILURE
1597 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001598 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001599psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1600 const uint8_t *hash,
1601 size_t hash_length);
1602
Gilles Peskine308b91d2018-02-08 09:47:44 +01001603/** Abort a hash operation.
1604 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001605 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001606 * \p operation structure itself. Once aborted, the operation object
1607 * can be reused for another operation by calling
1608 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001609 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001610 * You may call this function any time after the operation object has
1611 * been initialized by any of the following methods:
1612 * - A call to psa_hash_setup(), whether it succeeds or not.
1613 * - Initializing the \c struct to all-bits-zero.
1614 * - Initializing the \c struct to logical zeros, e.g.
1615 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001616 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001617 * In particular, calling psa_hash_abort() after the operation has been
1618 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1619 * psa_hash_verify() is safe and has no effect.
1620 *
1621 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001622 *
Gilles Peskine28538492018-07-11 17:34:00 +02001623 * \retval #PSA_SUCCESS
1624 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001625 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001626 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1627 * \retval #PSA_ERROR_HARDWARE_FAILURE
1628 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001629 */
1630psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001631
1632/**@}*/
1633
Gilles Peskine8c9def32018-02-08 10:02:12 +01001634/** \defgroup MAC Message authentication codes
1635 * @{
1636 */
1637
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001638/** The type of the state data structure for multipart MAC operations.
1639 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001640 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001641 * make any assumptions about the content of this structure except
1642 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001643typedef struct psa_mac_operation_s psa_mac_operation_t;
1644
Gilles Peskine89167cb2018-07-08 20:12:23 +02001645/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001646 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001647 * This function sets up the calculation of the MAC
1648 * (message authentication code) of a byte string.
1649 * To verify the MAC of a message against an
1650 * expected value, use psa_mac_verify_setup() instead.
1651 *
1652 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001653 * -# Allocate an operation object which will be passed to all the functions
1654 * listed here.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001655 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001656 * The key remains associated with the operation even if the content
1657 * of the key slot changes.
1658 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1659 * of the message each time. The MAC that is calculated is the MAC
1660 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001661 * -# At the end of the message, call psa_mac_sign_finish() to finish
1662 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001663 *
1664 * The application may call psa_mac_abort() at any time after the operation
Gilles Peskine89167cb2018-07-08 20:12:23 +02001665 * has been initialized with psa_mac_sign_setup().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001666 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001667 * After a successful call to psa_mac_sign_setup(), the application must
1668 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001669 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001670 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001671 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001672 * \param[out] operation The operation object to use.
1673 * \param key Slot containing the key to use for the operation.
1674 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1675 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001676 *
Gilles Peskine28538492018-07-11 17:34:00 +02001677 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001678 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001679 * \retval #PSA_ERROR_EMPTY_SLOT
1680 * \retval #PSA_ERROR_NOT_PERMITTED
1681 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001682 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001683 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001684 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001685 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1686 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1687 * \retval #PSA_ERROR_HARDWARE_FAILURE
1688 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001689 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001690psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
1691 psa_key_slot_t key,
1692 psa_algorithm_t alg);
1693
1694/** Start a multipart MAC verification operation.
1695 *
1696 * This function sets up the verification of the MAC
1697 * (message authentication code) of a byte string against an expected value.
1698 *
1699 * The sequence of operations to verify a MAC is as follows:
1700 * -# Allocate an operation object which will be passed to all the functions
1701 * listed here.
1702 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1703 * The key remains associated with the operation even if the content
1704 * of the key slot changes.
1705 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1706 * of the message each time. The MAC that is calculated is the MAC
1707 * of the concatenation of these messages in order.
1708 * -# At the end of the message, call psa_mac_verify_finish() to finish
1709 * calculating the actual MAC of the message and verify it against
1710 * the expected value.
1711 *
1712 * The application may call psa_mac_abort() at any time after the operation
1713 * has been initialized with psa_mac_verify_setup().
1714 *
1715 * After a successful call to psa_mac_verify_setup(), the application must
1716 * eventually terminate the operation through one of the following methods:
1717 * - A failed call to psa_mac_update().
1718 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1719 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001720 * \param[out] operation The operation object to use.
1721 * \param key Slot containing the key to use for the operation.
1722 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1723 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001724 *
Gilles Peskine28538492018-07-11 17:34:00 +02001725 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001726 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001727 * \retval #PSA_ERROR_EMPTY_SLOT
1728 * \retval #PSA_ERROR_NOT_PERMITTED
1729 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001730 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001731 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001732 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001733 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1734 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1735 * \retval #PSA_ERROR_HARDWARE_FAILURE
1736 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001737 */
1738psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
1739 psa_key_slot_t key,
1740 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001741
Gilles Peskinedcd14942018-07-12 00:30:52 +02001742/** Add a message fragment to a multipart MAC operation.
1743 *
1744 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1745 * before calling this function.
1746 *
1747 * If this function returns an error status, the operation becomes inactive.
1748 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001749 * \param[in,out] operation Active MAC operation.
1750 * \param[in] input Buffer containing the message fragment to add to
1751 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001752 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001753 *
1754 * \retval #PSA_SUCCESS
1755 * Success.
1756 * \retval #PSA_ERROR_BAD_STATE
1757 * The operation state is not valid (not started, or already completed).
1758 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1759 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1760 * \retval #PSA_ERROR_HARDWARE_FAILURE
1761 * \retval #PSA_ERROR_TAMPERING_DETECTED
1762 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001763psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1764 const uint8_t *input,
1765 size_t input_length);
1766
Gilles Peskinedcd14942018-07-12 00:30:52 +02001767/** Finish the calculation of the MAC of a message.
1768 *
1769 * The application must call psa_mac_sign_setup() before calling this function.
1770 * This function calculates the MAC of the message formed by concatenating
1771 * the inputs passed to preceding calls to psa_mac_update().
1772 *
1773 * When this function returns, the operation becomes inactive.
1774 *
1775 * \warning Applications should not call this function if they expect
1776 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1777 * Beware that comparing integrity or authenticity data such as
1778 * MAC values with a function such as \c memcmp is risky
1779 * because the time taken by the comparison may leak information
1780 * about the MAC value which could allow an attacker to guess
1781 * a valid MAC and thereby bypass security controls.
1782 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001783 * \param[in,out] operation Active MAC operation.
1784 * \param[out] mac Buffer where the MAC value is to be written.
1785 * \param mac_size Size of the \p mac buffer in bytes.
1786 * \param[out] mac_length On success, the number of bytes
1787 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001788 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001789 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001790 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001791 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001792 *
1793 * \retval #PSA_SUCCESS
1794 * Success.
1795 * \retval #PSA_ERROR_BAD_STATE
1796 * The operation state is not valid (not started, or already completed).
1797 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001798 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001799 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1800 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1801 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1802 * \retval #PSA_ERROR_HARDWARE_FAILURE
1803 * \retval #PSA_ERROR_TAMPERING_DETECTED
1804 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001805psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1806 uint8_t *mac,
1807 size_t mac_size,
1808 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001809
Gilles Peskinedcd14942018-07-12 00:30:52 +02001810/** Finish the calculation of the MAC of a message and compare it with
1811 * an expected value.
1812 *
1813 * The application must call psa_mac_verify_setup() before calling this function.
1814 * This function calculates the MAC of the message formed by concatenating
1815 * the inputs passed to preceding calls to psa_mac_update(). It then
1816 * compares the calculated MAC with the expected MAC passed as a
1817 * parameter to this function.
1818 *
1819 * When this function returns, the operation becomes inactive.
1820 *
1821 * \note Implementations shall make the best effort to ensure that the
1822 * comparison between the actual MAC and the expected MAC is performed
1823 * in constant time.
1824 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001825 * \param[in,out] operation Active MAC operation.
1826 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001827 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001828 *
1829 * \retval #PSA_SUCCESS
1830 * The expected MAC is identical to the actual MAC of the message.
1831 * \retval #PSA_ERROR_INVALID_SIGNATURE
1832 * The MAC of the message was calculated successfully, but it
1833 * differs from the expected MAC.
1834 * \retval #PSA_ERROR_BAD_STATE
1835 * The operation state is not valid (not started, or already completed).
1836 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1837 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1838 * \retval #PSA_ERROR_HARDWARE_FAILURE
1839 * \retval #PSA_ERROR_TAMPERING_DETECTED
1840 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001841psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1842 const uint8_t *mac,
1843 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001844
Gilles Peskinedcd14942018-07-12 00:30:52 +02001845/** Abort a MAC operation.
1846 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001847 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001848 * \p operation structure itself. Once aborted, the operation object
1849 * can be reused for another operation by calling
1850 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001851 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001852 * You may call this function any time after the operation object has
1853 * been initialized by any of the following methods:
1854 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1855 * it succeeds or not.
1856 * - Initializing the \c struct to all-bits-zero.
1857 * - Initializing the \c struct to logical zeros, e.g.
1858 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001859 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001860 * In particular, calling psa_mac_abort() after the operation has been
1861 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1862 * psa_mac_verify_finish() is safe and has no effect.
1863 *
1864 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001865 *
1866 * \retval #PSA_SUCCESS
1867 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001868 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001869 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1870 * \retval #PSA_ERROR_HARDWARE_FAILURE
1871 * \retval #PSA_ERROR_TAMPERING_DETECTED
1872 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001873psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1874
1875/**@}*/
1876
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001877/** \defgroup cipher Symmetric ciphers
1878 * @{
1879 */
1880
1881/** The type of the state data structure for multipart cipher operations.
1882 *
1883 * This is an implementation-defined \c struct. Applications should not
1884 * make any assumptions about the content of this structure except
1885 * as directed by the documentation of a specific implementation. */
1886typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1887
1888/** Set the key for a multipart symmetric encryption operation.
1889 *
1890 * The sequence of operations to encrypt a message with a symmetric cipher
1891 * is as follows:
1892 * -# Allocate an operation object which will be passed to all the functions
1893 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02001894 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001895 * The key remains associated with the operation even if the content
1896 * of the key slot changes.
Gilles Peskinefe119512018-07-08 21:39:34 +02001897 * -# Call either psa_encrypt_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001898 * generate or set the IV (initialization vector). You should use
1899 * psa_encrypt_generate_iv() unless the protocol you are implementing
1900 * requires a specific IV value.
1901 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1902 * of the message each time.
1903 * -# Call psa_cipher_finish().
1904 *
1905 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02001906 * has been initialized with psa_cipher_encrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001907 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001908 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001909 * eventually terminate the operation. The following events terminate an
1910 * operation:
Gilles Peskinefe119512018-07-08 21:39:34 +02001911 * - A failed call to psa_encrypt_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001912 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001913 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001914 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001915 * \param[out] operation The operation object to use.
1916 * \param key Slot containing the key to use for the operation.
1917 * \param alg The cipher algorithm to compute
1918 * (\c PSA_ALG_XXX value such that
1919 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001920 *
Gilles Peskine28538492018-07-11 17:34:00 +02001921 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001922 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001923 * \retval #PSA_ERROR_EMPTY_SLOT
1924 * \retval #PSA_ERROR_NOT_PERMITTED
1925 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001926 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001927 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001928 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001929 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1930 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1931 * \retval #PSA_ERROR_HARDWARE_FAILURE
1932 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001933 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001934psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
1935 psa_key_slot_t key,
1936 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001937
1938/** Set the key for a multipart symmetric decryption operation.
1939 *
1940 * The sequence of operations to decrypt a message with a symmetric cipher
1941 * is as follows:
1942 * -# Allocate an operation object which will be passed to all the functions
1943 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02001944 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001945 * The key remains associated with the operation even if the content
1946 * of the key slot changes.
1947 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1948 * decryption. If the IV is prepended to the ciphertext, you can call
1949 * psa_cipher_update() on a buffer containing the IV followed by the
1950 * beginning of the message.
1951 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1952 * of the message each time.
1953 * -# Call psa_cipher_finish().
1954 *
1955 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02001956 * has been initialized with psa_cipher_decrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001957 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001958 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001959 * eventually terminate the operation. The following events terminate an
1960 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001961 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001962 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001963 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001964 * \param[out] operation The operation object to use.
1965 * \param key Slot containing the key to use for the operation.
1966 * \param alg The cipher algorithm to compute
1967 * (\c PSA_ALG_XXX value such that
1968 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001969 *
Gilles Peskine28538492018-07-11 17:34:00 +02001970 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001971 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001972 * \retval #PSA_ERROR_EMPTY_SLOT
1973 * \retval #PSA_ERROR_NOT_PERMITTED
1974 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001975 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001976 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001977 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001978 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1979 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1980 * \retval #PSA_ERROR_HARDWARE_FAILURE
1981 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001982 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001983psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
1984 psa_key_slot_t key,
1985 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001986
Gilles Peskinedcd14942018-07-12 00:30:52 +02001987/** Generate an IV for a symmetric encryption operation.
1988 *
1989 * This function generates a random IV (initialization vector), nonce
1990 * or initial counter value for the encryption operation as appropriate
1991 * for the chosen algorithm, key type and key size.
1992 *
1993 * The application must call psa_cipher_encrypt_setup() before
1994 * calling this function.
1995 *
1996 * If this function returns an error status, the operation becomes inactive.
1997 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001998 * \param[in,out] operation Active cipher operation.
1999 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002000 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002001 * \param[out] iv_length On success, the number of bytes of the
2002 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002003 *
2004 * \retval #PSA_SUCCESS
2005 * Success.
2006 * \retval #PSA_ERROR_BAD_STATE
2007 * The operation state is not valid (not started, or IV already set).
2008 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002009 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002010 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2011 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2012 * \retval #PSA_ERROR_HARDWARE_FAILURE
2013 * \retval #PSA_ERROR_TAMPERING_DETECTED
2014 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002015psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
2016 unsigned char *iv,
2017 size_t iv_size,
2018 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002019
Gilles Peskinedcd14942018-07-12 00:30:52 +02002020/** Set the IV for a symmetric encryption or decryption operation.
2021 *
2022 * This function sets the random IV (initialization vector), nonce
2023 * or initial counter value for the encryption or decryption operation.
2024 *
2025 * The application must call psa_cipher_encrypt_setup() before
2026 * calling this function.
2027 *
2028 * If this function returns an error status, the operation becomes inactive.
2029 *
2030 * \note When encrypting, applications should use psa_cipher_generate_iv()
2031 * instead of this function, unless implementing a protocol that requires
2032 * a non-random IV.
2033 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002034 * \param[in,out] operation Active cipher operation.
2035 * \param[in] iv Buffer containing the IV to use.
2036 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002037 *
2038 * \retval #PSA_SUCCESS
2039 * Success.
2040 * \retval #PSA_ERROR_BAD_STATE
2041 * The operation state is not valid (not started, or IV already set).
2042 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002043 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02002044 * or the chosen algorithm does not use an IV.
2045 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2046 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2047 * \retval #PSA_ERROR_HARDWARE_FAILURE
2048 * \retval #PSA_ERROR_TAMPERING_DETECTED
2049 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002050psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
2051 const unsigned char *iv,
2052 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002053
Gilles Peskinedcd14942018-07-12 00:30:52 +02002054/** Encrypt or decrypt a message fragment in an active cipher operation.
2055 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02002056 * Before calling this function, you must:
2057 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
2058 * The choice of setup function determines whether this function
2059 * encrypts or decrypts its input.
2060 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
2061 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02002062 *
2063 * If this function returns an error status, the operation becomes inactive.
2064 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002065 * \param[in,out] operation Active cipher operation.
2066 * \param[in] input Buffer containing the message fragment to
2067 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002068 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002069 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002070 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002071 * \param[out] output_length On success, the number of bytes
2072 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002073 *
2074 * \retval #PSA_SUCCESS
2075 * Success.
2076 * \retval #PSA_ERROR_BAD_STATE
2077 * The operation state is not valid (not started, IV required but
2078 * not set, or already completed).
2079 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2080 * The size of the \p output buffer is too small.
2081 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2082 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2083 * \retval #PSA_ERROR_HARDWARE_FAILURE
2084 * \retval #PSA_ERROR_TAMPERING_DETECTED
2085 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002086psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2087 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02002088 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002089 unsigned char *output,
2090 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002091 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002092
Gilles Peskinedcd14942018-07-12 00:30:52 +02002093/** Finish encrypting or decrypting a message in a cipher operation.
2094 *
2095 * The application must call psa_cipher_encrypt_setup() or
2096 * psa_cipher_decrypt_setup() before calling this function. The choice
2097 * of setup function determines whether this function encrypts or
2098 * decrypts its input.
2099 *
2100 * This function finishes the encryption or decryption of the message
2101 * formed by concatenating the inputs passed to preceding calls to
2102 * psa_cipher_update().
2103 *
2104 * When this function returns, the operation becomes inactive.
2105 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002106 * \param[in,out] operation Active cipher operation.
2107 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002108 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002109 * \param[out] output_length On success, the number of bytes
2110 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002111 *
2112 * \retval #PSA_SUCCESS
2113 * Success.
2114 * \retval #PSA_ERROR_BAD_STATE
2115 * The operation state is not valid (not started, IV required but
2116 * not set, or already completed).
2117 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2118 * The size of the \p output buffer is too small.
2119 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2120 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2121 * \retval #PSA_ERROR_HARDWARE_FAILURE
2122 * \retval #PSA_ERROR_TAMPERING_DETECTED
2123 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002124psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002125 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002126 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002127 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002128
Gilles Peskinedcd14942018-07-12 00:30:52 +02002129/** Abort a cipher operation.
2130 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002131 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002132 * \p operation structure itself. Once aborted, the operation object
2133 * can be reused for another operation by calling
2134 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002135 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002136 * You may call this function any time after the operation object has
2137 * been initialized by any of the following methods:
2138 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2139 * whether it succeeds or not.
2140 * - Initializing the \c struct to all-bits-zero.
2141 * - Initializing the \c struct to logical zeros, e.g.
2142 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002143 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002144 * In particular, calling psa_cipher_abort() after the operation has been
2145 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2146 * is safe and has no effect.
2147 *
2148 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002149 *
2150 * \retval #PSA_SUCCESS
2151 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002152 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002153 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2154 * \retval #PSA_ERROR_HARDWARE_FAILURE
2155 * \retval #PSA_ERROR_TAMPERING_DETECTED
2156 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002157psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2158
2159/**@}*/
2160
Gilles Peskine3b555712018-03-03 21:27:57 +01002161/** \defgroup aead Authenticated encryption with associated data (AEAD)
2162 * @{
2163 */
2164
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002165/** The tag size for an AEAD algorithm, in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01002166 *
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002167 * \param alg An AEAD algorithm
2168 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002169 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002170 *
2171 * \return The tag size for the specified algorithm.
2172 * If the AEAD algorithm does not have an identified
2173 * tag that can be distinguished from the rest of
2174 * the ciphertext, return 0.
2175 * If the AEAD algorithm is not recognized, return 0.
2176 * An implementation may return either 0 or a
2177 * correct size for an AEAD algorithm that it
2178 * recognizes, but does not support.
2179 */
2180#define PSA_AEAD_TAG_SIZE(alg) \
2181 ((alg) == PSA_ALG_GCM ? 16 : \
2182 (alg) == PSA_ALG_CCM ? 16 : \
2183 0)
Gilles Peskine3b555712018-03-03 21:27:57 +01002184
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002185/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002186 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002187 * \param key Slot containing the key to use.
2188 * \param alg The AEAD algorithm to compute
2189 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002190 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002191 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002192 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002193 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002194 * but not encrypted.
2195 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002196 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002197 * encrypted.
2198 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002199 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002200 * encrypted data. The additional data is not
2201 * part of this output. For algorithms where the
2202 * encrypted data and the authentication tag
2203 * are defined as separate outputs, the
2204 * authentication tag is appended to the
2205 * encrypted data.
2206 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2207 * This must be at least
2208 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2209 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002210 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002211 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002212 *
Gilles Peskine28538492018-07-11 17:34:00 +02002213 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002214 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002215 * \retval #PSA_ERROR_EMPTY_SLOT
2216 * \retval #PSA_ERROR_NOT_PERMITTED
2217 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002218 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002219 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002220 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002221 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2222 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2223 * \retval #PSA_ERROR_HARDWARE_FAILURE
2224 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine3b555712018-03-03 21:27:57 +01002225 */
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002226psa_status_t psa_aead_encrypt(psa_key_slot_t key,
2227 psa_algorithm_t alg,
2228 const uint8_t *nonce,
2229 size_t nonce_length,
2230 const uint8_t *additional_data,
2231 size_t additional_data_length,
2232 const uint8_t *plaintext,
2233 size_t plaintext_length,
2234 uint8_t *ciphertext,
2235 size_t ciphertext_size,
2236 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002237
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002238/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002239 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002240 * \param key Slot containing the key to use.
2241 * \param alg The AEAD algorithm to compute
2242 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002243 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002244 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002245 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002246 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002247 * but not encrypted.
2248 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002249 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002250 * encrypted. For algorithms where the
2251 * encrypted data and the authentication tag
2252 * are defined as separate inputs, the buffer
2253 * must contain the encrypted data followed
2254 * by the authentication tag.
2255 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002256 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002257 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2258 * This must be at least
2259 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2260 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002261 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03002262 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002263 *
Gilles Peskine28538492018-07-11 17:34:00 +02002264 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002265 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002266 * \retval #PSA_ERROR_EMPTY_SLOT
2267 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002268 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002269 * \retval #PSA_ERROR_NOT_PERMITTED
2270 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002271 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002272 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002273 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002274 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2275 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2276 * \retval #PSA_ERROR_HARDWARE_FAILURE
2277 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine3b555712018-03-03 21:27:57 +01002278 */
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002279psa_status_t psa_aead_decrypt(psa_key_slot_t key,
2280 psa_algorithm_t alg,
2281 const uint8_t *nonce,
2282 size_t nonce_length,
2283 const uint8_t *additional_data,
2284 size_t additional_data_length,
2285 const uint8_t *ciphertext,
2286 size_t ciphertext_length,
2287 uint8_t *plaintext,
2288 size_t plaintext_size,
2289 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002290
2291/**@}*/
2292
Gilles Peskine20035e32018-02-03 22:44:14 +01002293/** \defgroup asymmetric Asymmetric cryptography
2294 * @{
2295 */
2296
2297/**
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002298 * \brief ECDSA signature size for a given curve bit size
Gilles Peskine0189e752018-02-03 23:57:22 +01002299 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002300 * \param curve_bits Curve size in bits.
2301 * \return Signature size in bytes.
Gilles Peskine0189e752018-02-03 23:57:22 +01002302 *
2303 * \note This macro returns a compile-time constant if its argument is one.
Gilles Peskine0189e752018-02-03 23:57:22 +01002304 */
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002305#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
2306 (PSA_BITS_TO_BYTES(curve_bits) * 2)
Gilles Peskine0189e752018-02-03 23:57:22 +01002307
Gilles Peskine0189e752018-02-03 23:57:22 +01002308/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002309 * \brief Sign a hash or short message with a private key.
2310 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002311 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002312 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002313 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2314 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2315 * to determine the hash algorithm to use.
2316 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002317 * \param key Key slot containing an asymmetric key pair.
2318 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002319 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002320 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002321 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002322 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002323 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002324 * \param[out] signature_length On success, the number of bytes
2325 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002326 *
Gilles Peskine28538492018-07-11 17:34:00 +02002327 * \retval #PSA_SUCCESS
2328 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002329 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002330 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002331 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002332 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002333 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002334 * \retval #PSA_ERROR_NOT_SUPPORTED
2335 * \retval #PSA_ERROR_INVALID_ARGUMENT
2336 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2337 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2338 * \retval #PSA_ERROR_HARDWARE_FAILURE
2339 * \retval #PSA_ERROR_TAMPERING_DETECTED
2340 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01002341 */
2342psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
2343 psa_algorithm_t alg,
2344 const uint8_t *hash,
2345 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002346 uint8_t *signature,
2347 size_t signature_size,
2348 size_t *signature_length);
2349
2350/**
2351 * \brief Verify the signature a hash or short message using a public key.
2352 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002353 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002354 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002355 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2356 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2357 * to determine the hash algorithm to use.
2358 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01002359 * \param key Key slot containing a public key or an
2360 * asymmetric key pair.
2361 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002362 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002363 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002364 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002365 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002366 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002367 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002368 *
Gilles Peskine28538492018-07-11 17:34:00 +02002369 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002370 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002371 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002372 * The calculation was perfomed successfully, but the passed
2373 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002374 * \retval #PSA_ERROR_NOT_SUPPORTED
2375 * \retval #PSA_ERROR_INVALID_ARGUMENT
2376 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2377 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2378 * \retval #PSA_ERROR_HARDWARE_FAILURE
2379 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01002380 */
2381psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
2382 psa_algorithm_t alg,
2383 const uint8_t *hash,
2384 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002385 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002386 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002387
Gilles Peskine723feff2018-05-31 20:08:13 +02002388#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
Gilles Peskine072ac562018-06-30 00:21:29 +02002389 (PSA_ALG_IS_RSA_OAEP(alg) ? \
2390 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskine723feff2018-05-31 20:08:13 +02002391 11 /*PKCS#1v1.5*/)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002392
2393/**
2394 * \brief Encrypt a short message with a public key.
2395 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002396 * \param key Key slot containing a public key or an
2397 * asymmetric key pair.
2398 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002399 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002400 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002401 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002402 * \param[in] salt A salt or label, if supported by the
2403 * encryption algorithm.
2404 * If the algorithm does not support a
2405 * salt, pass \c NULL.
2406 * If the algorithm supports an optional
2407 * salt and you do not want to pass a salt,
2408 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002409 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002410 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2411 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002412 * \param salt_length Size of the \p salt buffer in bytes.
2413 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002414 * \param[out] output Buffer where the encrypted message is to
2415 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002416 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002417 * \param[out] output_length On success, the number of bytes
2418 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002419 *
Gilles Peskine28538492018-07-11 17:34:00 +02002420 * \retval #PSA_SUCCESS
2421 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002422 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002423 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002424 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002425 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002426 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002427 * \retval #PSA_ERROR_NOT_SUPPORTED
2428 * \retval #PSA_ERROR_INVALID_ARGUMENT
2429 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2430 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2431 * \retval #PSA_ERROR_HARDWARE_FAILURE
2432 * \retval #PSA_ERROR_TAMPERING_DETECTED
2433 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002434 */
2435psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
2436 psa_algorithm_t alg,
2437 const uint8_t *input,
2438 size_t input_length,
2439 const uint8_t *salt,
2440 size_t salt_length,
2441 uint8_t *output,
2442 size_t output_size,
2443 size_t *output_length);
2444
2445/**
2446 * \brief Decrypt a short message with a private key.
2447 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002448 * \param key Key slot containing an asymmetric key pair.
2449 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002450 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002451 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002452 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002453 * \param[in] salt A salt or label, if supported by the
2454 * encryption algorithm.
2455 * If the algorithm does not support a
2456 * salt, pass \c NULL.
2457 * If the algorithm supports an optional
2458 * salt and you do not want to pass a salt,
2459 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002460 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002461 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2462 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002463 * \param salt_length Size of the \p salt buffer in bytes.
2464 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002465 * \param[out] output Buffer where the decrypted message is to
2466 * be written.
2467 * \param output_size Size of the \c output buffer in bytes.
2468 * \param[out] output_length On success, the number of bytes
2469 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002470 *
Gilles Peskine28538492018-07-11 17:34:00 +02002471 * \retval #PSA_SUCCESS
2472 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002473 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002474 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002475 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002476 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002477 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002478 * \retval #PSA_ERROR_NOT_SUPPORTED
2479 * \retval #PSA_ERROR_INVALID_ARGUMENT
2480 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2481 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2482 * \retval #PSA_ERROR_HARDWARE_FAILURE
2483 * \retval #PSA_ERROR_TAMPERING_DETECTED
2484 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2485 * \retval #PSA_ERROR_INVALID_PADDING
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002486 */
2487psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
2488 psa_algorithm_t alg,
2489 const uint8_t *input,
2490 size_t input_length,
2491 const uint8_t *salt,
2492 size_t salt_length,
2493 uint8_t *output,
2494 size_t output_size,
2495 size_t *output_length);
2496
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002497/**@}*/
2498
Gilles Peskineedd76872018-07-20 17:42:05 +02002499/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002500 * @{
2501 */
2502
2503/** The type of the state data structure for generators.
2504 *
2505 * Before calling any function on a generator, the application must
2506 * initialize it by any of the following means:
2507 * - Set the structure to all-bits-zero, for example:
2508 * \code
2509 * psa_crypto_generator_t generator;
2510 * memset(&generator, 0, sizeof(generator));
2511 * \endcode
2512 * - Initialize the structure to logical zero values, for example:
2513 * \code
2514 * psa_crypto_generator_t generator = {0};
2515 * \endcode
2516 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2517 * for example:
2518 * \code
2519 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2520 * \endcode
2521 * - Assign the result of the function psa_crypto_generator_init()
2522 * to the structure, for example:
2523 * \code
2524 * psa_crypto_generator_t generator;
2525 * generator = psa_crypto_generator_init();
2526 * \endcode
2527 *
2528 * This is an implementation-defined \c struct. Applications should not
2529 * make any assumptions about the content of this structure except
2530 * as directed by the documentation of a specific implementation.
2531 */
2532typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2533
2534/** \def PSA_CRYPTO_GENERATOR_INIT
2535 *
2536 * This macro returns a suitable initializer for a generator object
2537 * of type #psa_crypto_generator_t.
2538 */
2539#ifdef __DOXYGEN_ONLY__
2540/* This is an example definition for documentation purposes.
2541 * Implementations should define a suitable value in `crypto_struct.h`.
2542 */
2543#define PSA_CRYPTO_GENERATOR_INIT {0}
2544#endif
2545
2546/** Return an initial value for a generator object.
2547 */
2548static psa_crypto_generator_t psa_crypto_generator_init(void);
2549
2550/** Retrieve the current capacity of a generator.
2551 *
2552 * The capacity of a generator is the maximum number of bytes that it can
2553 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2554 *
2555 * \param[in] generator The generator to query.
2556 * \param[out] capacity On success, the capacity of the generator.
2557 *
2558 * \retval PSA_SUCCESS
2559 * \retval PSA_ERROR_BAD_STATE
2560 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2561 */
2562psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2563 size_t *capacity);
2564
2565/** Read some data from a generator.
2566 *
2567 * This function reads and returns a sequence of bytes from a generator.
2568 * The data that is read is discarded from the generator. The generator's
2569 * capacity is decreased by the number of bytes read.
2570 *
2571 * \param[in,out] generator The generator object to read from.
2572 * \param[out] output Buffer where the generator output will be
2573 * written.
2574 * \param output_length Number of bytes to output.
2575 *
2576 * \retval PSA_SUCCESS
2577 * \retval PSA_ERROR_INSUFFICIENT_CAPACITY
2578 * There were fewer than \p output_length bytes
2579 * in the generator. Note that in this case, no
2580 * output is written to the output buffer.
2581 * The generator's capacity is set to 0, thus
2582 * subsequent calls to this function will not
2583 * succeed, even with a smaller output buffer.
2584 * \retval PSA_ERROR_BAD_STATE
2585 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
2586 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2587 * \retval PSA_ERROR_HARDWARE_FAILURE
2588 * \retval PSA_ERROR_TAMPERING_DETECTED
2589 */
2590psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2591 uint8_t *output,
2592 size_t output_length);
2593
2594/** Create a symmetric key from data read from a generator.
2595 *
2596 * This function reads a sequence of bytes from a generator and imports
2597 * these bytes as a key.
2598 * The data that is read is discarded from the generator. The generator's
2599 * capacity is decreased by the number of bytes read.
2600 *
2601 * This function is equivalent to calling #psa_generator_read and
2602 * passing the resulting output to #psa_import_key, but
2603 * if the implementation provides an isolation boundary then
2604 * the key material is not exposed outside the isolation boundary.
2605 *
2606 * \param key Slot where the key will be stored. This must be a
2607 * valid slot for a key of the chosen type. It must
2608 * be unoccupied.
2609 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2610 * This must be a symmetric key type.
2611 * \param bits Key size in bits.
2612 * \param[in,out] generator The generator object to read from.
2613 *
2614 * \retval PSA_SUCCESS
2615 * Success.
2616 * \retval PSA_ERROR_INSUFFICIENT_CAPACITY
2617 * There were fewer than \p output_length bytes
2618 * in the generator. Note that in this case, no
2619 * output is written to the output buffer.
2620 * The generator's capacity is set to 0, thus
2621 * subsequent calls to this function will not
2622 * succeed, even with a smaller output buffer.
2623 * \retval PSA_ERROR_NOT_SUPPORTED
2624 * The key type or key size is not supported, either by the
2625 * implementation in general or in this particular slot.
2626 * \retval PSA_ERROR_BAD_STATE
2627 * \retval PSA_ERROR_INVALID_ARGUMENT
2628 * The key slot is invalid.
2629 * \retval PSA_ERROR_OCCUPIED_SLOT
2630 * There is already a key in the specified slot.
2631 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
2632 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
2633 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2634 * \retval PSA_ERROR_HARDWARE_FAILURE
2635 * \retval PSA_ERROR_TAMPERING_DETECTED
2636 */
2637psa_status_t psa_generator_import_key(psa_key_slot_t key,
2638 psa_key_type_t type,
2639 size_t bits,
2640 psa_crypto_generator_t *generator);
2641
2642/** Abort a generator.
2643 *
2644 * Once a generator has been aborted, its capacity is zero.
2645 * Aborting a generator frees all associated resources except for the
2646 * \c generator structure itself.
2647 *
2648 * This function may be called at any time as long as the generator
2649 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2650 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2651 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2652 * on a generator that has not been set up.
2653 *
2654 * Once aborted, the generator object may be called.
2655 *
2656 * \param[in,out] generator The generator to abort.
2657 *
2658 * \retval PSA_SUCCESS
2659 * \retval PSA_ERROR_BAD_STATE
2660 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2661 * \retval PSA_ERROR_HARDWARE_FAILURE
2662 * \retval PSA_ERROR_TAMPERING_DETECTED
2663 */
2664psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2665
2666/**@}*/
2667
Gilles Peskineea0fb492018-07-12 17:17:20 +02002668/** \defgroup derivation Key derivation
2669 * @{
2670 */
2671
2672/** Set up a key derivation operation.
2673 *
2674 * A key derivation algorithm takes three inputs: a secret input \p key and
2675 * two non-secret inputs \p label and p salt.
2676 * The result of this function is a byte generator which can
2677 * be used to produce keys and other cryptographic material.
2678 *
2679 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02002680 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2681 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002682 *
2683 * \param[in,out] generator The generator object to set up. It must
2684 * have been initialized to .
2685 * \param key Slot containing the secret key to use.
2686 * \param alg The key derivation algorithm to compute
2687 * (\c PSA_ALG_XXX value such that
2688 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2689 * \param[in] salt Salt to use.
2690 * \param salt_length Size of the \p salt buffer in bytes.
2691 * \param[in] label Label to use.
2692 * \param label_length Size of the \p label buffer in bytes.
2693 * \param capacity The maximum number of bytes that the
2694 * generator will be able to provide.
2695 *
2696 * \retval #PSA_SUCCESS
2697 * Success.
2698 * \retval #PSA_ERROR_EMPTY_SLOT
2699 * \retval #PSA_ERROR_NOT_PERMITTED
2700 * \retval #PSA_ERROR_INVALID_ARGUMENT
2701 * \c key is not compatible with \c alg,
2702 * or \p capacity is too large for the specified algorithm and key.
2703 * \retval #PSA_ERROR_NOT_SUPPORTED
2704 * \c alg is not supported or is not a key derivation algorithm.
2705 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2706 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2707 * \retval #PSA_ERROR_HARDWARE_FAILURE
2708 * \retval #PSA_ERROR_TAMPERING_DETECTED
2709 */
2710psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
2711 psa_key_type_t key,
2712 psa_algorithm_t alg,
2713 const uint8_t *salt,
2714 size_t salt_length,
2715 const uint8_t *label,
2716 size_t label_length,
2717 size_t capacity);
2718
2719/**@}*/
2720
Gilles Peskineedd76872018-07-20 17:42:05 +02002721/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002722 * @{
2723 */
2724
2725/**
2726 * \brief Generate random bytes.
2727 *
2728 * \warning This function **can** fail! Callers MUST check the return status
2729 * and MUST NOT use the content of the output buffer if the return
2730 * status is not #PSA_SUCCESS.
2731 *
2732 * \note To generate a key, use psa_generate_key() instead.
2733 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002734 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002735 * \param output_size Number of bytes to generate and output.
2736 *
Gilles Peskine28538492018-07-11 17:34:00 +02002737 * \retval #PSA_SUCCESS
2738 * \retval #PSA_ERROR_NOT_SUPPORTED
2739 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2740 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2741 * \retval #PSA_ERROR_HARDWARE_FAILURE
2742 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002743 */
2744psa_status_t psa_generate_random(uint8_t *output,
2745 size_t output_size);
2746
Gilles Peskine4c317f42018-07-12 01:24:09 +02002747/** Extra parameters for RSA key generation.
2748 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002749 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002750 * parameter to psa_generate_key().
2751 */
2752typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02002753 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02002754} psa_generate_key_extra_rsa;
2755
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002756/**
2757 * \brief Generate a key or key pair.
2758 *
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002759 * \param key Slot where the key will be stored. This must be a
2760 * valid slot for a key of the chosen type. It must
2761 * be unoccupied.
2762 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2763 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002764 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002765 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002766 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002767 * default parameters. Implementation that support
2768 * the generation of vendor-specific key types
2769 * that allow extra parameters shall document
2770 * the format of these extra parameters and
2771 * the default values. For standard parameters,
2772 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002773 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002774 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2775 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002776 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002777 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2778 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002779 * - For an RSA key (\p type is
2780 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2781 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002782 * specifying the public exponent. The
2783 * default public exponent used when \p extra
2784 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002785 * \param extra_size Size of the buffer that \p extra
2786 * points to, in bytes. Note that if \p extra is
2787 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002788 *
Gilles Peskine28538492018-07-11 17:34:00 +02002789 * \retval #PSA_SUCCESS
2790 * \retval #PSA_ERROR_NOT_SUPPORTED
2791 * \retval #PSA_ERROR_INVALID_ARGUMENT
2792 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2793 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2794 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2795 * \retval #PSA_ERROR_HARDWARE_FAILURE
2796 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002797 */
2798psa_status_t psa_generate_key(psa_key_slot_t key,
2799 psa_key_type_t type,
2800 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002801 const void *extra,
2802 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002803
2804/**@}*/
2805
Gilles Peskinee59236f2018-01-27 23:32:46 +01002806#ifdef __cplusplus
2807}
2808#endif
2809
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002810/* The file "crypto_sizes.h" contains definitions for size calculation
2811 * macros whose definitions are implementation-specific. */
2812#include "crypto_sizes.h"
2813
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002814/* The file "crypto_struct.h" contains definitions for
2815 * implementation-specific structs that are declared above. */
2816#include "crypto_struct.h"
2817
2818/* The file "crypto_extra.h" contains vendor-specific definitions. This
2819 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002820#include "crypto_extra.h"
2821
2822#endif /* PSA_CRYPTO_H */