blob: fb7edf83c3f01bec79cc9915d5e39e7a10d4788b [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
5
6#ifndef PSA_CRYPTO_H
7#define PSA_CRYPTO_H
8
9#include "crypto_platform.h"
10
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010011#include <stddef.h>
12
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010013#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010014/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
15 * must be defined in the crypto_platform.h header. These mock definitions
16 * are present in this file as a convenience to generate pretty-printed
17 * documentation that includes those definitions. */
18
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010019/** \defgroup platform Implementation-specific definitions
20 * @{
21 */
22
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010023/** \brief Key slot number.
24 *
25 * This type represents key slots. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010026 * type. The choice of type is implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027 * 0 is not a valid key slot number. The meaning of other values is
28 * implementation dependent.
29 *
30 * At any given point in time, each key slot either contains a
31 * cryptographic object, or is empty. Key slots are persistent:
32 * once set, the cryptographic object remains in the key slot until
33 * explicitly destroyed.
34 */
35typedef _unsigned_integral_type_ psa_key_slot_t;
36
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010037/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010038#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010039
Gilles Peskinee59236f2018-01-27 23:32:46 +010040#ifdef __cplusplus
41extern "C" {
42#endif
43
44/** \defgroup basic Basic definitions
45 * @{
46 */
47
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020048#if defined(PSA_SUCCESS)
49/* If PSA_SUCCESS is defined, assume that PSA crypto is being used
50 * together with PSA IPC, which also defines the identifier
51 * PSA_SUCCESS. We must not define PSA_SUCCESS ourselves in that case;
52 * the other error code names don't clash. Also define psa_status_t as
53 * an alias for the type used by PSA IPC. This is a temporary hack
54 * until we unify error reporting in PSA IPC and PSA crypo.
55 *
56 * Note that psa_defs.h must be included before this header!
57 */
58typedef psa_error_t psa_status_t;
59
60#else /* defined(PSA_SUCCESS) */
61
Gilles Peskinee59236f2018-01-27 23:32:46 +010062/**
63 * \brief Function return status.
64 *
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020065 * This is either #PSA_SUCCESS (which is zero), indicating success,
66 * or a nonzero value indicating that an error occurred. Errors are
67 * encoded as one of the \c PSA_ERROR_xxx values defined here.
Gilles Peskinee59236f2018-01-27 23:32:46 +010068 */
itayzafrirc2a79762018-06-18 16:20:16 +030069typedef int32_t psa_status_t;
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020070
itayzafrirc2a79762018-06-18 16:20:16 +030071/** The action was completed successfully. */
72#define PSA_SUCCESS ((psa_status_t)0)
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020073
74#endif /* !defined(PSA_SUCCESS) */
itayzafrirc2a79762018-06-18 16:20:16 +030075
76/** The requested operation or a parameter is not supported
77 * by this implementation.
78 *
79 * Implementations should return this error code when an enumeration
80 * parameter such as a key type, algorithm, etc. is not recognized.
81 * If a combination of parameters is recognized and identified as
82 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
83#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)1)
84
85/** The requested action is denied by a policy.
86 *
87 * Implementations should return this error code when the parameters
88 * are recognized as valid and supported, and a policy explicitly
89 * denies the requested operation.
90 *
91 * If a subset of the parameters of a function call identify a
92 * forbidden operation, and another subset of the parameters are
93 * not valid or not supported, it is unspecified whether the function
94 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
95 * #PSA_ERROR_INVALID_ARGUMENT. */
96#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)2)
97
98/** An output buffer is too small.
99 *
100 * Applications can call the `PSA_xxx_SIZE` macro listed in the function
101 * description to determine a sufficient buffer size.
102 *
103 * Implementations should preferably return this error code only
104 * in cases when performing the operation with a larger output
105 * buffer would succeed. However implementations may return this
106 * error if a function has invalid or unsupported parameters in addition
107 * to the parameters that determine the necessary output buffer size. */
108#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)3)
109
110/** A slot is occupied, but must be empty to carry out the
111 * requested action.
112 *
113 * If the slot number is invalid (i.e. the requested action could
114 * not be performed even after erasing the slot's content),
115 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
116#define PSA_ERROR_OCCUPIED_SLOT ((psa_status_t)4)
117
118/** A slot is empty, but must be occupied to carry out the
119 * requested action.
120 *
121 * If the slot number is invalid (i.e. the requested action could
122 * not be performed even after creating appropriate content in the slot),
123 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
124#define PSA_ERROR_EMPTY_SLOT ((psa_status_t)5)
125
126/** The requested action cannot be performed in the current state.
127 *
128 * Multipart operations return this error when one of the
129 * functions is called out of sequence. Refer to the function
130 * descriptions for permitted sequencing of functions.
131 *
132 * Implementations shall not return this error code to indicate
133 * that a key slot is occupied when it needs to be free or vice versa,
134 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
135 * as applicable. */
136#define PSA_ERROR_BAD_STATE ((psa_status_t)6)
137
138/** The parameters passed to the function are invalid.
139 *
140 * Implementations may return this error any time a parameter or
141 * combination of parameters are recognized as invalid.
142 *
143 * Implementations shall not return this error code to indicate
144 * that a key slot is occupied when it needs to be free or vice versa,
145 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
146 * as applicable. */
147#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)7)
148
149/** There is not enough runtime memory.
150 *
151 * If the action is carried out across multiple security realms, this
152 * error can refer to available memory in any of the security realms. */
153#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)8)
154
155/** There is not enough persistent storage.
156 *
157 * Functions that modify the key storage return this error code if
158 * there is insufficient storage space on the host media. In addition,
159 * many functions that do not otherwise access storage may return this
160 * error code if the implementation requires a mandatory log entry for
161 * the requested action and the log storage space is full. */
162#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)9)
163
164/** There was a communication failure inside the implementation.
165 *
166 * This can indicate a communication failure between the application
167 * and an external cryptoprocessor or between the cryptoprocessor and
168 * an external volatile or persistent memory. A communication failure
169 * may be transient or permanent depending on the cause.
170 *
171 * \warning If a function returns this error, it is undetermined
172 * whether the requested action has completed or not. Implementations
173 * should return #PSA_SUCCESS on successful completion whenver
174 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
175 * if the requested action was completed successfully in an external
176 * cryptoprocessor but there was a breakdown of communication before
177 * the cryptoprocessor could report the status to the application.
178 */
179#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)10)
180
181/** There was a storage failure that may have led to data loss.
182 *
183 * This error indicates that some persistent storage is corrupted.
184 * It should not be used for a corruption of volatile memory
185 * (use #PSA_ERROR_TAMPERING_DETECTED), for a communication error
186 * between the cryptoprocessor and its external storage (use
187 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
188 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
189 *
190 * Note that a storage failure does not indicate that any data that was
191 * previously read is invalid. However this previously read data may no
192 * longer be readable from storage.
193 *
194 * When a storage failure occurs, it is no longer possible to ensure
195 * the global integrity of the keystore. Depending on the global
196 * integrity guarantees offered by the implementation, access to other
197 * data may or may not fail even if the data is still readable but
198 * its integrity canont be guaranteed.
199 *
200 * Implementations should only use this error code to report a
201 * permanent storage corruption. However application writers should
202 * keep in mind that transient errors while reading the storage may be
203 * reported using this error code. */
204#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)11)
205
206/** A hardware failure was detected.
207 *
208 * A hardware failure may be transient or permanent depending on the
209 * cause. */
210#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)12)
211
212/** A tampering attempt was detected.
213 *
214 * If an application receives this error code, there is no guarantee
215 * that previously accessed or computed data was correct and remains
216 * confidential. Applications should not perform any security function
217 * and should enter a safe failure state.
218 *
219 * Implementations may return this error code if they detect an invalid
220 * state that cannot happen during normal operation and that indicates
221 * that the implementation's security guarantees no longer hold. Depending
222 * on the implementation architecture and on its security and safety goals,
223 * the implementation may forcibly terminate the application.
224 *
225 * This error code is intended as a last resort when a security breach
226 * is detected and it is unsure whether the keystore data is still
227 * protected. Implementations shall only return this error code
228 * to report an alarm from a tampering detector, to indicate that
229 * the confidentiality of stored data can no longer be guaranteed,
230 * or to indicate that the integrity of previously returned data is now
231 * considered compromised. Implementations shall not use this error code
232 * to indicate a hardware failure that merely makes it impossible to
233 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
234 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
235 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
236 * instead).
237 *
238 * This error indicates an attack against the application. Implementations
239 * shall not return this error code as a consequence of the behavior of
240 * the application itself. */
241#define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)13)
242
243/** There is not enough entropy to generate random data needed
244 * for the requested action.
245 *
246 * This error indicates a failure of a hardware random generator.
247 * Application writers should note that this error can be returned not
248 * only by functions whose purpose is to generate random data, such
249 * as key, IV or nonce generation, but also by functions that execute
250 * an algorithm with a randomized result, as well as functions that
251 * use randomization of intermediate computations as a countermeasure
252 * to certain attacks.
253 *
254 * Implementations should avoid returning this error after psa_crypto_init()
255 * has succeeded. Implementations should generate sufficient
256 * entropy during initialization and subsequently use a cryptographically
257 * secure pseudorandom generator (PRNG). However implementations may return
258 * this error at any time if a policy requires the PRNG to be reseeded
259 * during normal operation. */
260#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)14)
261
262/** The signature, MAC or hash is incorrect.
263 *
264 * Verification functions return this error if the verification
265 * calculations completed successfully, and the value to be verified
266 * was determined to be incorrect.
267 *
268 * If the value to verify has an invalid size, implementations may return
269 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
270#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)15)
271
272/** The decrypted padding is incorrect.
273 *
274 * \warning In some protocols, when decrypting data, it is essential that
275 * the behavior of the application does not depend on whether the padding
276 * is correct, down to precise timing. Applications should prefer
277 * protocols that use authenticated encryption rather than plain
278 * encryption. If the application must perform a decryption of
279 * unauthenticated data, the application writer should take care not
280 * to reveal whether the padding is invalid.
281 *
282 * Implementations should strive to make valid and invalid padding
283 * as close as possible to indistinguishable to an external observer.
284 * In particular, the timing of a decryption operation should not
285 * depend on the validity of the padding. */
286#define PSA_ERROR_INVALID_PADDING ((psa_status_t)16)
287
288/** An error occurred that does not correspond to any defined
289 * failure cause.
290 *
291 * Implementations may use this error code if none of the other standard
292 * error codes are applicable. */
293#define PSA_ERROR_UNKNOWN_ERROR ((psa_status_t)17)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100294
295/**
296 * \brief Library initialization.
297 *
298 * Applications must call this function before calling any other
299 * function in this module.
300 *
301 * Applications may call this function more than once. Once a call
302 * succeeds, subsequent calls are guaranteed to succeed.
303 *
Gilles Peskine28538492018-07-11 17:34:00 +0200304 * \retval #PSA_SUCCESS
305 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
306 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
307 * \retval #PSA_ERROR_HARDWARE_FAILURE
308 * \retval #PSA_ERROR_TAMPERING_DETECTED
309 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +0100310 */
311psa_status_t psa_crypto_init(void);
312
Gilles Peskine2905a7a2018-03-07 16:39:31 +0100313#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
314#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
Gilles Peskine0189e752018-02-03 23:57:22 +0100315
Gilles Peskinee59236f2018-01-27 23:32:46 +0100316/**@}*/
317
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100318/** \defgroup crypto_types Key and algorithm types
319 * @{
320 */
321
Gilles Peskine308b91d2018-02-08 09:47:44 +0100322/** \brief Encoding of a key type.
323 */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100324typedef uint32_t psa_key_type_t;
325
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100326/** An invalid key type value.
327 *
328 * Zero is not the encoding of any key type.
329 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100330#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100331
332/** Vendor-defined flag
333 *
334 * Key types defined by this standard will never have the
335 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
336 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
337 * respect the bitwise structure used by standard encodings whenever practical.
338 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100339#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100340
Gilles Peskine98f0a242018-02-06 18:57:29 +0100341#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7e000000)
Gilles Peskine35855962018-04-19 08:39:16 +0200342/** Raw data.
343 *
344 * A "key" of this type cannot be used for any cryptographic operation.
345 * Applications may use this type to store arbitrary data in the keystore. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100346#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x02000000)
347#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x04000000)
348#define PSA_KEY_TYPE_CATEGORY_ASYMMETRIC ((psa_key_type_t)0x06000000)
349#define PSA_KEY_TYPE_PAIR_FLAG ((psa_key_type_t)0x01000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100350
Gilles Peskine35855962018-04-19 08:39:16 +0200351/** HMAC key.
352 *
353 * The key policy determines which underlying hash algorithm the key can be
354 * used for.
355 *
356 * HMAC keys should generally have the same size as the underlying hash.
Gilles Peskine28538492018-07-11 17:34:00 +0200357 * This size can be calculated with #PSA_HASH_SIZE(`alg`) where
Gilles Peskine35855962018-04-19 08:39:16 +0200358 * `alg` is the HMAC algorithm or the underlying hash algorithm. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100359#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x02000001)
Gilles Peskine35855962018-04-19 08:39:16 +0200360/** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher.
361 *
362 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
363 * 32 bytes (AES-256).
364 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100365#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x04000001)
Gilles Peskine35855962018-04-19 08:39:16 +0200366/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
367 *
368 * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or
369 * 24 bytes (3-key 3DES).
370 *
371 * Note that single DES and 2-key 3DES are weak and strongly
372 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
373 * is weak and deprecated and should only be used in legacy protocols.
374 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100375#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x04000002)
Gilles Peskine35855962018-04-19 08:39:16 +0200376/** Key for an cipher, AEAD or MAC algorithm based on the
377 * Camellia block cipher. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100378#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x04000003)
Gilles Peskine35855962018-04-19 08:39:16 +0200379/** Key for the RC4 stream cipher.
380 *
381 * Note that RC4 is weak and deprecated and should only be used in
382 * legacy protocols. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100383#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x04000004)
384
Gilles Peskine308b91d2018-02-08 09:47:44 +0100385/** RSA public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100386#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x06010000)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100387/** RSA key pair (private and public key). */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100388#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x07010000)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100389/** DSA public key. */
390#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x06020000)
391/** DSA key pair (private and public key). */
392#define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x07020000)
393#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x06030000)
394#define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x07030000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100395#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100396#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
397 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
398#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
399 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
Gilles Peskine98f0a242018-02-06 18:57:29 +0100400
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100401/** Whether a key type is vendor-defined. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100402#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100403 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100404
405/** Whether a key type is asymmetric: either a key pair or a public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100406#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
407 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100408/** Whether a key type is the public part of a key pair. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100409#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
Moran Pekerb4d0ddd2018-04-04 12:47:52 +0300410 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
411 PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100412/** Whether a key type is a key pair containing a private part and a public
413 * part. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100414#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
415 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
416 (PSA_KEY_TYPE_CATEGORY_ASYMMETRIC | PSA_KEY_TYPE_PAIR_FLAG))
Gilles Peskine06dc2632018-03-08 07:47:25 +0100417/** The key pair type corresponding to a public key type. */
418#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
419 ((type) | PSA_KEY_TYPE_PAIR_FLAG)
420/** The public key type corresponding to a key pair type. */
421#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
422 ((type) & ~PSA_KEY_TYPE_PAIR_FLAG)
Gilles Peskine61a60372018-07-08 21:48:44 +0200423/** Whether a key type is an RSA key pair or public key. */
Gilles Peskine0189e752018-02-03 23:57:22 +0100424#define PSA_KEY_TYPE_IS_RSA(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100425 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
426/** Whether a key type is an elliptic curve key pair or public key. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100427#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100428 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
429 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100430
Gilles Peskinee1fed0d2018-06-18 20:45:45 +0200431/** The type of PSA elliptic curve identifiers. */
432typedef uint16_t psa_ecc_curve_t;
433/** Extract the curve from an elliptic curve key type. */
434#define PSA_KEY_TYPE_GET_CURVE(type) \
435 ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
436 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
437 0))
438
439/* The encoding of curve identifiers is currently aligned with the
440 * TLS Supported Groups Registry (formerly known as the
441 * TLS EC Named Curve Registry)
442 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
443 * The values are defined by RFC 4492, RFC 7027 and RFC 7919. */
444#define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001)
445#define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002)
446#define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003)
447#define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004)
448#define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005)
449#define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006)
450#define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007)
451#define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008)
452#define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009)
453#define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a)
454#define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b)
455#define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c)
456#define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d)
457#define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e)
458#define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f)
459#define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010)
460#define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011)
461#define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012)
462#define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013)
463#define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014)
464#define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015)
465#define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016)
466#define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017)
467#define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018)
468#define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019)
469#define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a)
470#define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b)
471#define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c)
472#define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d)
473#define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e)
474#define PSA_ECC_CURVE_FFDHE_2048 ((psa_ecc_curve_t) 0x0100)
475#define PSA_ECC_CURVE_FFDHE_3072 ((psa_ecc_curve_t) 0x0101)
476#define PSA_ECC_CURVE_FFDHE_4096 ((psa_ecc_curve_t) 0x0102)
477#define PSA_ECC_CURVE_FFDHE_6144 ((psa_ecc_curve_t) 0x0103)
478#define PSA_ECC_CURVE_FFDHE_8192 ((psa_ecc_curve_t) 0x0104)
479
Gilles Peskine7e198532018-03-08 07:50:30 +0100480/** The block size of a block cipher.
481 *
482 * \param type A cipher key type (value of type #psa_key_type_t).
483 *
484 * \return The block size for a block cipher, or 1 for a stream cipher.
Gilles Peskine35855962018-04-19 08:39:16 +0200485 * The return value is undefined if \c type is not a supported
486 * cipher key type.
487 *
488 * \note It is possible to build stream cipher algorithms on top of a block
489 * cipher, for example CTR mode (#PSA_ALG_CTR).
490 * This macro only takes the key type into account, so it cannot be
491 * used to determine the size of the data that #psa_cipher_update()
492 * might buffer for future processing in general.
Gilles Peskine7e198532018-03-08 07:50:30 +0100493 *
494 * \note This macro returns a compile-time constant if its argument is one.
495 *
496 * \warning This macro may evaluate its argument multiple times.
497 */
Gilles Peskine03182e92018-03-07 16:40:52 +0100498#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100499 ( \
500 (type) == PSA_KEY_TYPE_AES ? 16 : \
501 (type) == PSA_KEY_TYPE_DES ? 8 : \
502 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
Gilles Peskine7e198532018-03-08 07:50:30 +0100503 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100504 0)
505
Gilles Peskine308b91d2018-02-08 09:47:44 +0100506/** \brief Encoding of a cryptographic algorithm.
507 *
508 * For algorithms that can be applied to multiple key types, this type
509 * does not encode the key type. For example, for symmetric ciphers
510 * based on a block cipher, #psa_algorithm_t encodes the block cipher
511 * mode and the padding mode while the block cipher itself is encoded
512 * via #psa_key_type_t.
513 */
Gilles Peskine20035e32018-02-03 22:44:14 +0100514typedef uint32_t psa_algorithm_t;
515
Gilles Peskine98f0a242018-02-06 18:57:29 +0100516#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
517#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
518#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
519#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
520#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
521#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
522#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
523#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
524#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
525#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100526
Gilles Peskine98f0a242018-02-06 18:57:29 +0100527#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
528 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100529/** Whether the specified algorithm is a hash algorithm.
530 *
Gilles Peskine7e198532018-03-08 07:50:30 +0100531 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100532 *
533 * \return 1 if \c alg is a hash algorithm, 0 otherwise.
Gilles Peskine5ce3e592018-07-12 00:35:06 +0200534 * This macro may return either 0 or 1 if \c alg is not a supported
Gilles Peskine7e198532018-03-08 07:50:30 +0100535 * algorithm identifier.
536 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100537#define PSA_ALG_IS_HASH(alg) \
538 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
539#define PSA_ALG_IS_MAC(alg) \
540 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
541#define PSA_ALG_IS_CIPHER(alg) \
542 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
543#define PSA_ALG_IS_AEAD(alg) \
544 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
545#define PSA_ALG_IS_SIGN(alg) \
546 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
547#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
548 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
549#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
550 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
551#define PSA_ALG_IS_KEY_DERIVATION(alg) \
552 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
553
554#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
555#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
556#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
557#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
Gilles Peskinee3f694f2018-03-08 07:48:40 +0100558#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
559#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100560#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
561#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
562#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
563#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
564#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
565#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
566#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
567#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
568#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
569#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
570
Gilles Peskine8c9def32018-02-08 10:02:12 +0100571#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100572#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
Gilles Peskine35855962018-04-19 08:39:16 +0200573/** Macro to build an HMAC algorithm.
574 *
575 * For example, `PSA_ALG_HMAC(PSA_ALG_SHA256)` is HMAC-SHA-256.
576 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200577 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
578 * #PSA_ALG_IS_HASH(alg) is true).
Gilles Peskine35855962018-04-19 08:39:16 +0200579 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200580 * \return The corresponding HMAC algorithm.
581 * \return Unspecified if \p alg is not a supported
582 * hash algorithm.
Gilles Peskine35855962018-04-19 08:39:16 +0200583 */
584#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100585 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
586#define PSA_ALG_HMAC_HASH(hmac_alg) \
587 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
588#define PSA_ALG_IS_HMAC(alg) \
589 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
590 PSA_ALG_HMAC_BASE)
591#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
592#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
593#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
594#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
595#define PSA_ALG_IS_CIPHER_MAC(alg) \
596 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
597 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100598
Gilles Peskine8c9def32018-02-08 10:02:12 +0100599#define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100600#define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100601#define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100602#define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000)
603#define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100604#define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100605#define PSA_ALG_IS_BLOCK_CIPHER(alg) \
606 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
607 PSA_ALG_BLOCK_CIPHER_BASE)
608
Gilles Peskine98f0a242018-02-06 18:57:29 +0100609#define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100610#define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002)
611#define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003)
612#define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004)
Gilles Peskine5d1888e2018-07-12 00:32:42 +0200613
614#define PSA_ALG_STREAM_CIPHER_BASE ((psa_algorithm_t)0x04800000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100615#define PSA_ALG_CTR ((psa_algorithm_t)0x04800001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100616#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100617
Moran Pekerbed71a22018-04-22 20:19:20 +0300618#define PSA_ALG_IS_STREAM_CIPHER(alg) \
619 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
Gilles Peskine5d1888e2018-07-12 00:32:42 +0200620 PSA_ALG_STREAM_CIPHER_BASE)
Moran Pekerbed71a22018-04-22 20:19:20 +0300621
Gilles Peskine8c9def32018-02-08 10:02:12 +0100622#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
623#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100624
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200625#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
626/** RSA PKCS#1 v1.5 signature with hashing.
627 *
628 * This is the signature scheme defined by RFC 8017
629 * (PKCS#1: RSA Cryptography Specifications) under the name
630 * RSASSA-PKCS1-v1_5.
631 *
632 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
633 * #PSA_ALG_IS_HASH(alg) is true).
634 *
635 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
636 * \return Unspecified if \p alg is not a supported
637 * hash algorithm.
638 */
Gilles Peskinea5926232018-03-28 14:16:50 +0200639#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200640 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
641/** Raw PKCS#1 v1.5 signature.
642 *
643 * The input to this algorithm is the DigestInfo structure used by
644 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
645 * steps 3&ndash;6.
646 */
647#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
Gilles Peskinea5926232018-03-28 14:16:50 +0200648#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200649 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
650#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
651/** RSA PSS signature with hashing.
652 *
653 * This is the signature scheme defined by RFC 8017
654 * (PKCS#1: RSA Cryptography Specifications) under the name
655 * RSASSA-PSS, with the message generation function MGF1. The specified
656 * hash algorithm is used to hash the input message, to create the
657 * salted hash, and for the mask generation.
658 *
659 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
660 * #PSA_ALG_IS_HASH(alg) is true).
661 *
662 * \return The corresponding RSA PSS signature algorithm.
663 * \return Unspecified if \p alg is not a supported
664 * hash algorithm.
665 */
666#define PSA_ALG_RSA_PSS(hash_alg) \
667 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
668#define PSA_ALG_IS_RSA_PSS(alg) \
669 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
670
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200671#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
672/** DSA signature with hashing.
673 *
674 * This is the signature scheme defined by FIPS 186-4,
675 * with a random per-message secret number (*k*).
676 *
677 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
678 * #PSA_ALG_IS_HASH(alg) is true).
679 *
680 * \return The corresponding DSA signature algorithm.
681 * \return Unspecified if \p alg is not a supported
682 * hash algorithm.
683 */
684#define PSA_ALG_DSA(hash_alg) \
685 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
686#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
687#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
688#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
689 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
690#define PSA_ALG_IS_DSA(alg) \
691 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
692 PSA_ALG_DSA_BASE)
693#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
694 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
695
696#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
697/** ECDSA signature with hashing.
698 *
699 * This is the ECDSA signature scheme defined by ANSI X9.62,
700 * with a random per-message secret number (*k*).
701 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200702 * The representation of the signature as a byte string consists of
703 * the concatentation of the signature values *r* and *s*. Each of
704 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
705 * of the base point of the curve in octets. Each value is represented
706 * in big-endian order (most significant octet first).
707 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200708 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
709 * #PSA_ALG_IS_HASH(alg) is true).
710 *
711 * \return The corresponding ECDSA signature algorithm.
712 * \return Unspecified if \p alg is not a supported
713 * hash algorithm.
714 */
715#define PSA_ALG_ECDSA(hash_alg) \
716 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
717/** ECDSA signature without hashing.
718 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200719 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200720 * without specifying a hash algorithm. This algorithm may only be
721 * used to sign or verify a sequence of bytes that should be an
722 * already-calculated hash. Note that the input is padded with
723 * zeros on the left or truncated on the left as required to fit
724 * the curve size.
725 */
726#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
727#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
728/** Deterministic ECDSA signature with hashing.
729 *
730 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
731 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200732 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
733 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200734 * Note that when this algorithm is used for verification, signatures
735 * made with randomized ECDSA (#PSA_ALG_ECDSA(\c hash_alg)) with the
736 * same private key are accepted. In other words,
737 * #PSA_ALG_DETERMINISTIC_ECDSA(\c hash_alg) differs from
738 * #PSA_ALG_ECDSA(\c hash_alg) only for signature, not for verification.
739 *
740 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
741 * #PSA_ALG_IS_HASH(alg) is true).
742 *
743 * \return The corresponding deterministic ECDSA signature
744 * algorithm.
745 * \return Unspecified if \p alg is not a supported
746 * hash algorithm.
747 */
748#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
749 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
750#define PSA_ALG_IS_ECDSA(alg) \
751 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
752 PSA_ALG_ECDSA_BASE)
753#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
754 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
755
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200756/** Get the hash used by a hash-and-sign signature algorithm.
757 *
758 * A hash-and-sign algorithm is a signature algorithm which is
759 * composed of two phases: first a hashing phase which does not use
760 * the key and produces a hash of the input message, then a signing
761 * phase which only uses the hash and the key and not the message
762 * itself.
763 *
764 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
765 * #PSA_ALG_IS_SIGN(alg) is true).
766 *
767 * \return The underlying hash algorithm if \p alg is a hash-and-sign
768 * algorithm.
769 * \return 0 if \p alg is a signature algorithm that does not
770 * follow the hash-and-sign structure.
771 * \return Unspecified if \p alg is not a signature algorithm or
772 * if it is not supported by the implementation.
773 */
774#define PSA_ALG_SIGN_GET_HASH(alg) \
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200775 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
776 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg) ? \
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200777 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
778 0)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100779
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200780#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
781#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
782#define PSA_ALG_RSA_OAEP(hash_alg) \
783 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
784#define PSA_ALG_IS_RSA_OAEP(alg) \
785 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
Gilles Peskined1e8e412018-06-07 09:49:39 +0200786
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100787/**@}*/
788
789/** \defgroup key_management Key management
790 * @{
791 */
792
793/**
794 * \brief Import a key in binary format.
795 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100796 * This function supports any output from psa_export_key(). Refer to the
797 * documentation of psa_export_key() for the format for each key type.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100798 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100799 * \param key Slot where the key will be stored. This must be a
800 * valid slot for a key of the chosen type. It must
801 * be unoccupied.
802 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
803 * \param data Buffer containing the key data.
804 * \param data_length Size of the \c data buffer in bytes.
805 *
Gilles Peskine28538492018-07-11 17:34:00 +0200806 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100807 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200808 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200809 * The key type or key size is not supported, either by the
810 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200811 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +0100812 * The key slot is invalid,
813 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200814 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +0200815 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200816 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
817 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
818 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
819 * \retval #PSA_ERROR_HARDWARE_FAILURE
820 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100821 */
822psa_status_t psa_import_key(psa_key_slot_t key,
823 psa_key_type_t type,
824 const uint8_t *data,
825 size_t data_length);
826
827/**
Gilles Peskine154bd952018-04-19 08:38:16 +0200828 * \brief Destroy a key and restore the slot to its default state.
829 *
830 * This function destroys the content of the key slot from both volatile
831 * memory and, if applicable, non-volatile storage. Implementations shall
832 * make a best effort to ensure that any previous content of the slot is
833 * unrecoverable.
834 *
835 * This function also erases any metadata such as policies. It returns the
836 * specified slot to its default state.
837 *
838 * \param key The key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100839 *
Gilles Peskine28538492018-07-11 17:34:00 +0200840 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200841 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200842 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200843 * The slot holds content and cannot be erased because it is
844 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskine28538492018-07-11 17:34:00 +0200845 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine65eb8582018-04-19 08:28:58 +0200846 * The specified slot number does not designate a valid slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200847 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200848 * There was an failure in communication with the cryptoprocessor.
849 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200850 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200851 * The storage is corrupted. Implementations shall make a best effort
852 * to erase key material even in this stage, however applications
853 * should be aware that it may be impossible to guarantee that the
854 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200855 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200856 * An unexpected condition which is not a storage corruption or
857 * a communication failure occurred. The cryptoprocessor may have
858 * been compromised.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100859 */
860psa_status_t psa_destroy_key(psa_key_slot_t key);
861
862/**
863 * \brief Get basic metadata about a key.
864 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100865 * \param key Slot whose content is queried. This must
866 * be an occupied key slot.
867 * \param type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
868 * This may be a null pointer, in which case the key type
869 * is not written.
870 * \param bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100871 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100872 * is not written.
873 *
Gilles Peskine28538492018-07-11 17:34:00 +0200874 * \retval #PSA_SUCCESS
875 * \retval #PSA_ERROR_EMPTY_SLOT
876 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
877 * \retval #PSA_ERROR_HARDWARE_FAILURE
878 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100879 */
880psa_status_t psa_get_key_information(psa_key_slot_t key,
881 psa_key_type_t *type,
882 size_t *bits);
883
884/**
885 * \brief Export a key in binary format.
886 *
887 * The output of this function can be passed to psa_import_key() to
888 * create an equivalent object.
889 *
890 * If a key is created with psa_import_key() and then exported with
891 * this function, it is not guaranteed that the resulting data is
892 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +0100893 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100894 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100895 * For standard key types, the output format is as follows:
896 *
897 * - For symmetric keys (including MAC keys), the format is the
898 * raw bytes of the key.
899 * - For DES, the key data consists of 8 bytes. The parity bits must be
900 * correct.
901 * - For Triple-DES, the format is the concatenation of the
902 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100903 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine2743e422018-06-27 22:57:11 +0200904 * is the non-encrypted DER representation defined by PKCS\#1 (RFC 8017)
905 * as RSAPrivateKey.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100906 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
Gilles Peskine971f7062018-03-20 17:52:58 +0100907 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100908 *
909 * \param key Slot whose content is to be exported. This must
910 * be an occupied key slot.
911 * \param data Buffer where the key data is to be written.
912 * \param data_size Size of the \c data buffer in bytes.
913 * \param data_length On success, the number of bytes
914 * that make up the key data.
915 *
Gilles Peskine28538492018-07-11 17:34:00 +0200916 * \retval #PSA_SUCCESS
917 * \retval #PSA_ERROR_EMPTY_SLOT
918 * \retval #PSA_ERROR_NOT_PERMITTED
919 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
920 * \retval #PSA_ERROR_HARDWARE_FAILURE
921 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100922 */
923psa_status_t psa_export_key(psa_key_slot_t key,
924 uint8_t *data,
925 size_t data_size,
926 size_t *data_length);
927
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100928/**
929 * \brief Export a public key or the public part of a key pair in binary format.
930 *
931 * The output of this function can be passed to psa_import_key() to
932 * create an object that is equivalent to the public key.
933 *
934 * For standard key types, the output format is as follows:
935 *
936 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
Moran Pekerdd4ea382018-04-03 15:30:03 +0300937 * the format is the DER representation of the public key defined by RFC 5280
Gilles Peskine971f7062018-03-20 17:52:58 +0100938 * as SubjectPublicKeyInfo.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100939 *
940 * \param key Slot whose content is to be exported. This must
941 * be an occupied key slot.
942 * \param data Buffer where the key data is to be written.
943 * \param data_size Size of the \c data buffer in bytes.
944 * \param data_length On success, the number of bytes
945 * that make up the key data.
946 *
Gilles Peskine28538492018-07-11 17:34:00 +0200947 * \retval #PSA_SUCCESS
948 * \retval #PSA_ERROR_EMPTY_SLOT
949 * \retval #PSA_ERROR_INVALID_ARGUMENT
950 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
951 * \retval #PSA_ERROR_HARDWARE_FAILURE
952 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100953 */
954psa_status_t psa_export_public_key(psa_key_slot_t key,
955 uint8_t *data,
956 size_t data_size,
957 size_t *data_length);
958
959/**@}*/
960
961/** \defgroup policy Key policies
962 * @{
963 */
964
965/** \brief Encoding of permitted usage on a key. */
966typedef uint32_t psa_key_usage_t;
967
Gilles Peskine7e198532018-03-08 07:50:30 +0100968/** Whether the key may be exported.
969 *
970 * A public key or the public part of a key pair may always be exported
971 * regardless of the value of this permission flag.
972 *
973 * If a key does not have export permission, implementations shall not
974 * allow the key to be exported in plain form from the cryptoprocessor,
975 * whether through psa_export_key() or through a proprietary interface.
976 * The key may however be exportable in a wrapped form, i.e. in a form
977 * where it is encrypted by another key.
978 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100979#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
980
Gilles Peskine7e198532018-03-08 07:50:30 +0100981/** Whether the key may be used to encrypt a message.
982 *
983 * For a key pair, this concerns the public key.
984 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100985#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +0100986
987/** Whether the key may be used to decrypt a message.
988 *
989 * For a key pair, this concerns the private key.
990 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100991#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +0100992
993/** Whether the key may be used to sign a message.
994 *
995 * For a key pair, this concerns the private key.
996 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100997#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +0100998
999/** Whether the key may be used to verify a message signature.
1000 *
1001 * For a key pair, this concerns the public key.
1002 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001003#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
1004
1005/** The type of the key policy data structure.
1006 *
1007 * This is an implementation-defined \c struct. Applications should not
1008 * make any assumptions about the content of this structure except
1009 * as directed by the documentation of a specific implementation. */
1010typedef struct psa_key_policy_s psa_key_policy_t;
1011
1012/** \brief Initialize a key policy structure to a default that forbids all
1013 * usage of the key. */
1014void psa_key_policy_init(psa_key_policy_t *policy);
1015
Gilles Peskine7e198532018-03-08 07:50:30 +01001016/** \brief Set the standard fields of a policy structure.
1017 *
1018 * Note that this function does not make any consistency check of the
1019 * parameters. The values are only checked when applying the policy to
1020 * a key slot with psa_set_key_policy().
1021 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001022void psa_key_policy_set_usage(psa_key_policy_t *policy,
1023 psa_key_usage_t usage,
1024 psa_algorithm_t alg);
1025
1026psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy);
1027
1028psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy);
1029
1030/** \brief Set the usage policy on a key slot.
1031 *
1032 * This function must be called on an empty key slot, before importing,
1033 * generating or creating a key in the slot. Changing the policy of an
1034 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +01001035 *
1036 * Implementations may set restrictions on supported key policies
1037 * depending on the key type and the key slot.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001038 */
1039psa_status_t psa_set_key_policy(psa_key_slot_t key,
1040 const psa_key_policy_t *policy);
1041
Gilles Peskine7e198532018-03-08 07:50:30 +01001042/** \brief Get the usage policy for a key slot.
1043 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001044psa_status_t psa_get_key_policy(psa_key_slot_t key,
1045 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +01001046
1047/**@}*/
1048
Gilles Peskine609b6a52018-03-03 21:31:50 +01001049/** \defgroup persistence Key lifetime
1050 * @{
1051 */
1052
1053/** Encoding of key lifetimes.
1054 */
1055typedef uint32_t psa_key_lifetime_t;
1056
1057/** A volatile key slot retains its content as long as the application is
1058 * running. It is guaranteed to be erased on a power reset.
1059 */
1060#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1061
1062/** A persistent key slot retains its content as long as it is not explicitly
1063 * destroyed.
1064 */
1065#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1066
1067/** A write-once key slot may not be modified once a key has been set.
1068 * It will retain its content as long as the device remains operational.
1069 */
1070#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
1071
Gilles Peskined393e182018-03-08 07:49:16 +01001072/** \brief Retrieve the lifetime of a key slot.
1073 *
1074 * The assignment of lifetimes to slots is implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001075 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001076 * \param key Slot to query.
mohammad1603804cd712018-03-20 22:44:08 +02001077 * \param lifetime On success, the lifetime value.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001078 *
Gilles Peskine28538492018-07-11 17:34:00 +02001079 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001080 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001081 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603a7d245a2018-04-17 00:40:08 -07001082 * The key slot is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001083 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1084 * \retval #PSA_ERROR_HARDWARE_FAILURE
1085 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +01001086 */
Gilles Peskine609b6a52018-03-03 21:31:50 +01001087psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
1088 psa_key_lifetime_t *lifetime);
1089
Gilles Peskined393e182018-03-08 07:49:16 +01001090/** \brief Change the lifetime of a key slot.
1091 *
1092 * Whether the lifetime of a key slot can be changed at all, and if so
Gilles Peskine19067982018-03-20 17:54:53 +01001093 * whether the lifetime of an occupied key slot can be changed, is
Gilles Peskined393e182018-03-08 07:49:16 +01001094 * implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001095 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001096 * \param key Slot whose lifetime is to be changed.
1097 * \param lifetime The lifetime value to set for the given key slot.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001098 *
Gilles Peskine28538492018-07-11 17:34:00 +02001099 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001100 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001101 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603804cd712018-03-20 22:44:08 +02001102 * The key slot is invalid,
mohammad1603a7d245a2018-04-17 00:40:08 -07001103 * or the lifetime value is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001104 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001105 * The implementation does not support the specified lifetime value,
1106 * at least for the specified key slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001107 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001108 * The slot contains a key, and the implementation does not support
1109 * changing the lifetime of an occupied slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001110 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1111 * \retval #PSA_ERROR_HARDWARE_FAILURE
1112 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +01001113 */
1114psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
mohammad1603ea050092018-04-17 00:31:34 -07001115 psa_key_lifetime_t lifetime);
Gilles Peskined393e182018-03-08 07:49:16 +01001116
Gilles Peskine609b6a52018-03-03 21:31:50 +01001117/**@}*/
1118
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001119/** \defgroup hash Message digests
1120 * @{
1121 */
1122
Gilles Peskine308b91d2018-02-08 09:47:44 +01001123/** The type of the state data structure for multipart hash operations.
1124 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001125 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01001126 * make any assumptions about the content of this structure except
1127 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001128typedef struct psa_hash_operation_s psa_hash_operation_t;
1129
Gilles Peskine308b91d2018-02-08 09:47:44 +01001130/** The size of the output of psa_hash_finish(), in bytes.
1131 *
1132 * This is also the hash size that psa_hash_verify() expects.
1133 *
1134 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine35855962018-04-19 08:39:16 +02001135 * #PSA_ALG_IS_HASH(alg) is true), or an HMAC algorithm
Gilles Peskine28538492018-07-11 17:34:00 +02001136 * (#PSA_ALG_HMAC(`hash_alg`) where `hash_alg` is a
Gilles Peskine35855962018-04-19 08:39:16 +02001137 * hash algorithm).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001138 *
1139 * \return The hash size for the specified hash algorithm.
1140 * If the hash algorithm is not recognized, return 0.
1141 * An implementation may return either 0 or the correct size
1142 * for a hash algorithm that it recognizes, but does not support.
1143 */
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001144#define PSA_HASH_SIZE(alg) \
1145 ( \
1146 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD2 ? 16 : \
1147 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD4 ? 16 : \
1148 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD5 ? 16 : \
1149 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
1150 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
1151 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
1152 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
1153 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
1154 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
1155 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
1156 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
1157 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
1158 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
1159 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
1160 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001161 0)
1162
Gilles Peskine308b91d2018-02-08 09:47:44 +01001163/** Start a multipart hash operation.
1164 *
1165 * The sequence of operations to calculate a hash (message digest)
1166 * is as follows:
1167 * -# Allocate an operation object which will be passed to all the functions
1168 * listed here.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001169 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001170 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001171 * of the message each time. The hash that is calculated is the hash
1172 * of the concatenation of these messages in order.
1173 * -# To calculate the hash, call psa_hash_finish().
1174 * To compare the hash with an expected value, call psa_hash_verify().
1175 *
1176 * The application may call psa_hash_abort() at any time after the operation
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001177 * has been initialized with psa_hash_setup().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001178 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001179 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001180 * eventually terminate the operation. The following events terminate an
1181 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001182 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001183 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001184 *
Gilles Peskine36a74b72018-06-01 16:30:32 +02001185 * \param operation The operation object to use.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001186 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1187 * such that #PSA_ALG_IS_HASH(alg) is true).
1188 *
Gilles Peskine28538492018-07-11 17:34:00 +02001189 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001190 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001191 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001192 * \c alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001193 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1194 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1195 * \retval #PSA_ERROR_HARDWARE_FAILURE
1196 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001197 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001198psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001199 psa_algorithm_t alg);
1200
Gilles Peskine308b91d2018-02-08 09:47:44 +01001201/** Add a message fragment to a multipart hash operation.
1202 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001203 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001204 *
1205 * If this function returns an error status, the operation becomes inactive.
1206 *
1207 * \param operation Active hash operation.
1208 * \param input Buffer containing the message fragment to hash.
1209 * \param input_length Size of the \c input buffer in bytes.
1210 *
Gilles Peskine28538492018-07-11 17:34:00 +02001211 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001212 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001213 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001214 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001215 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1216 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1217 * \retval #PSA_ERROR_HARDWARE_FAILURE
1218 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001219 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001220psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1221 const uint8_t *input,
1222 size_t input_length);
1223
Gilles Peskine308b91d2018-02-08 09:47:44 +01001224/** Finish the calculation of the hash of a message.
1225 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001226 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001227 * This function calculates the hash of the message formed by concatenating
1228 * the inputs passed to preceding calls to psa_hash_update().
1229 *
1230 * When this function returns, the operation becomes inactive.
1231 *
1232 * \warning Applications should not call this function if they expect
1233 * a specific value for the hash. Call psa_hash_verify() instead.
1234 * Beware that comparing integrity or authenticity data such as
1235 * hash values with a function such as \c memcmp is risky
1236 * because the time taken by the comparison may leak information
1237 * about the hashed data which could allow an attacker to guess
1238 * a valid hash and thereby bypass security controls.
1239 *
1240 * \param operation Active hash operation.
1241 * \param hash Buffer where the hash is to be written.
1242 * \param hash_size Size of the \c hash buffer in bytes.
1243 * \param hash_length On success, the number of bytes
1244 * that make up the hash value. This is always
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001245 * #PSA_HASH_SIZE(alg) where \c alg is the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001246 * hash algorithm that is calculated.
1247 *
Gilles Peskine28538492018-07-11 17:34:00 +02001248 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001249 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001250 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001251 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001252 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine308b91d2018-02-08 09:47:44 +01001253 * The size of the \c hash buffer is too small. You can determine a
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001254 * sufficient buffer size by calling #PSA_HASH_SIZE(alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001255 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001256 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1257 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1258 * \retval #PSA_ERROR_HARDWARE_FAILURE
1259 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001260 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001261psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1262 uint8_t *hash,
1263 size_t hash_size,
1264 size_t *hash_length);
1265
Gilles Peskine308b91d2018-02-08 09:47:44 +01001266/** Finish the calculation of the hash of a message and compare it with
1267 * an expected value.
1268 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001269 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001270 * This function calculates the hash of the message formed by concatenating
1271 * the inputs passed to preceding calls to psa_hash_update(). It then
1272 * compares the calculated hash with the expected hash passed as a
1273 * parameter to this function.
1274 *
1275 * When this function returns, the operation becomes inactive.
1276 *
Gilles Peskine19067982018-03-20 17:54:53 +01001277 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001278 * comparison between the actual hash and the expected hash is performed
1279 * in constant time.
1280 *
1281 * \param operation Active hash operation.
1282 * \param hash Buffer containing the expected hash value.
1283 * \param hash_length Size of the \c hash buffer in bytes.
1284 *
Gilles Peskine28538492018-07-11 17:34:00 +02001285 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001286 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001287 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001288 * The hash of the message was calculated successfully, but it
1289 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001290 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001291 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001292 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1293 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1294 * \retval #PSA_ERROR_HARDWARE_FAILURE
1295 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001296 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001297psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1298 const uint8_t *hash,
1299 size_t hash_length);
1300
Gilles Peskine308b91d2018-02-08 09:47:44 +01001301/** Abort a hash operation.
1302 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001303 * This function may be called at any time after psa_hash_setup().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001304 * Aborting an operation frees all associated resources except for the
1305 * \c operation structure itself.
1306 *
1307 * Implementation should strive to be robust and handle inactive hash
1308 * operations safely (do nothing and return #PSA_ERROR_BAD_STATE). However,
1309 * application writers should beware that uninitialized memory may happen
1310 * to be indistinguishable from an active hash operation, and the behavior
1311 * of psa_hash_abort() is undefined in this case.
1312 *
1313 * \param operation Active hash operation.
1314 *
Gilles Peskine28538492018-07-11 17:34:00 +02001315 * \retval #PSA_SUCCESS
1316 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001317 * \c operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001318 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1319 * \retval #PSA_ERROR_HARDWARE_FAILURE
1320 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001321 */
1322psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001323
1324/**@}*/
1325
Gilles Peskine8c9def32018-02-08 10:02:12 +01001326/** \defgroup MAC Message authentication codes
1327 * @{
1328 */
1329
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001330/** The type of the state data structure for multipart MAC operations.
1331 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001332 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001333 * make any assumptions about the content of this structure except
1334 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001335typedef struct psa_mac_operation_s psa_mac_operation_t;
1336
Gilles Peskine89167cb2018-07-08 20:12:23 +02001337/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001338 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001339 * This function sets up the calculation of the MAC
1340 * (message authentication code) of a byte string.
1341 * To verify the MAC of a message against an
1342 * expected value, use psa_mac_verify_setup() instead.
1343 *
1344 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001345 * -# Allocate an operation object which will be passed to all the functions
1346 * listed here.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001347 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001348 * The key remains associated with the operation even if the content
1349 * of the key slot changes.
1350 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1351 * of the message each time. The MAC that is calculated is the MAC
1352 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001353 * -# At the end of the message, call psa_mac_sign_finish() to finish
1354 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001355 *
1356 * The application may call psa_mac_abort() at any time after the operation
Gilles Peskine89167cb2018-07-08 20:12:23 +02001357 * has been initialized with psa_mac_sign_setup().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001358 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001359 * After a successful call to psa_mac_sign_setup(), the application must
1360 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001361 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001362 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001363 *
Gilles Peskine36a74b72018-06-01 16:30:32 +02001364 * \param operation The operation object to use.
Gilles Peskine9e73ff12018-06-26 21:25:40 +02001365 * \param key Slot containing the key to use for the operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001366 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1367 * such that #PSA_ALG_IS_MAC(alg) is true).
1368 *
Gilles Peskine28538492018-07-11 17:34:00 +02001369 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001370 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001371 * \retval #PSA_ERROR_EMPTY_SLOT
1372 * \retval #PSA_ERROR_NOT_PERMITTED
1373 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001374 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001375 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001376 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001377 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1378 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1379 * \retval #PSA_ERROR_HARDWARE_FAILURE
1380 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001381 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001382psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
1383 psa_key_slot_t key,
1384 psa_algorithm_t alg);
1385
1386/** Start a multipart MAC verification operation.
1387 *
1388 * This function sets up the verification of the MAC
1389 * (message authentication code) of a byte string against an expected value.
1390 *
1391 * The sequence of operations to verify a MAC is as follows:
1392 * -# Allocate an operation object which will be passed to all the functions
1393 * listed here.
1394 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1395 * The key remains associated with the operation even if the content
1396 * of the key slot changes.
1397 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1398 * of the message each time. The MAC that is calculated is the MAC
1399 * of the concatenation of these messages in order.
1400 * -# At the end of the message, call psa_mac_verify_finish() to finish
1401 * calculating the actual MAC of the message and verify it against
1402 * the expected value.
1403 *
1404 * The application may call psa_mac_abort() at any time after the operation
1405 * has been initialized with psa_mac_verify_setup().
1406 *
1407 * After a successful call to psa_mac_verify_setup(), the application must
1408 * eventually terminate the operation through one of the following methods:
1409 * - A failed call to psa_mac_update().
1410 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1411 *
1412 * \param operation The operation object to use.
1413 * \param key Slot containing the key to use for the operation.
1414 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1415 * such that #PSA_ALG_IS_MAC(alg) is true).
1416 *
Gilles Peskine28538492018-07-11 17:34:00 +02001417 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001418 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001419 * \retval #PSA_ERROR_EMPTY_SLOT
1420 * \retval #PSA_ERROR_NOT_PERMITTED
1421 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001422 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001423 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001424 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001425 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1426 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1427 * \retval #PSA_ERROR_HARDWARE_FAILURE
1428 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001429 */
1430psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
1431 psa_key_slot_t key,
1432 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001433
1434psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1435 const uint8_t *input,
1436 size_t input_length);
1437
Gilles Peskineacd4be32018-07-08 19:56:25 +02001438psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1439 uint8_t *mac,
1440 size_t mac_size,
1441 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001442
Gilles Peskineacd4be32018-07-08 19:56:25 +02001443psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1444 const uint8_t *mac,
1445 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001446
1447psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1448
1449/**@}*/
1450
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001451/** \defgroup cipher Symmetric ciphers
1452 * @{
1453 */
1454
1455/** The type of the state data structure for multipart cipher operations.
1456 *
1457 * This is an implementation-defined \c struct. Applications should not
1458 * make any assumptions about the content of this structure except
1459 * as directed by the documentation of a specific implementation. */
1460typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1461
1462/** Set the key for a multipart symmetric encryption operation.
1463 *
1464 * The sequence of operations to encrypt a message with a symmetric cipher
1465 * is as follows:
1466 * -# Allocate an operation object which will be passed to all the functions
1467 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02001468 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001469 * The key remains associated with the operation even if the content
1470 * of the key slot changes.
Gilles Peskinefe119512018-07-08 21:39:34 +02001471 * -# Call either psa_encrypt_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001472 * generate or set the IV (initialization vector). You should use
1473 * psa_encrypt_generate_iv() unless the protocol you are implementing
1474 * requires a specific IV value.
1475 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1476 * of the message each time.
1477 * -# Call psa_cipher_finish().
1478 *
1479 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02001480 * has been initialized with psa_cipher_encrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001481 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001482 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001483 * eventually terminate the operation. The following events terminate an
1484 * operation:
Gilles Peskinefe119512018-07-08 21:39:34 +02001485 * - A failed call to psa_encrypt_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001486 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001487 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001488 *
Gilles Peskine36a74b72018-06-01 16:30:32 +02001489 * \param operation The operation object to use.
Gilles Peskine9e73ff12018-06-26 21:25:40 +02001490 * \param key Slot containing the key to use for the operation.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001491 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
1492 * such that #PSA_ALG_IS_CIPHER(alg) is true).
1493 *
Gilles Peskine28538492018-07-11 17:34:00 +02001494 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001495 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001496 * \retval #PSA_ERROR_EMPTY_SLOT
1497 * \retval #PSA_ERROR_NOT_PERMITTED
1498 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001499 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001500 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001501 * \c alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001502 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1503 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1504 * \retval #PSA_ERROR_HARDWARE_FAILURE
1505 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001506 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001507psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
1508 psa_key_slot_t key,
1509 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001510
1511/** Set the key for a multipart symmetric decryption operation.
1512 *
1513 * The sequence of operations to decrypt a message with a symmetric cipher
1514 * is as follows:
1515 * -# Allocate an operation object which will be passed to all the functions
1516 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02001517 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001518 * The key remains associated with the operation even if the content
1519 * of the key slot changes.
1520 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1521 * decryption. If the IV is prepended to the ciphertext, you can call
1522 * psa_cipher_update() on a buffer containing the IV followed by the
1523 * beginning of the message.
1524 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1525 * of the message each time.
1526 * -# Call psa_cipher_finish().
1527 *
1528 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02001529 * has been initialized with psa_cipher_decrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001530 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001531 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001532 * eventually terminate the operation. The following events terminate an
1533 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001534 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001535 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001536 *
Gilles Peskine36a74b72018-06-01 16:30:32 +02001537 * \param operation The operation object to use.
Gilles Peskine9e73ff12018-06-26 21:25:40 +02001538 * \param key Slot containing the key to use for the operation.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001539 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
1540 * such that #PSA_ALG_IS_CIPHER(alg) is true).
1541 *
Gilles Peskine28538492018-07-11 17:34:00 +02001542 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001543 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001544 * \retval #PSA_ERROR_EMPTY_SLOT
1545 * \retval #PSA_ERROR_NOT_PERMITTED
1546 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001547 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001548 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001549 * \c alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001550 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1551 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1552 * \retval #PSA_ERROR_HARDWARE_FAILURE
1553 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001554 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001555psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
1556 psa_key_slot_t key,
1557 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001558
Gilles Peskinefe119512018-07-08 21:39:34 +02001559psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1560 unsigned char *iv,
1561 size_t iv_size,
1562 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001563
Gilles Peskinefe119512018-07-08 21:39:34 +02001564psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1565 const unsigned char *iv,
1566 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001567
1568psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1569 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001570 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001571 unsigned char *output,
1572 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001573 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001574
1575psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001576 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001577 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001578 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001579
1580psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1581
1582/**@}*/
1583
Gilles Peskine3b555712018-03-03 21:27:57 +01001584/** \defgroup aead Authenticated encryption with associated data (AEAD)
1585 * @{
1586 */
1587
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001588/** The tag size for an AEAD algorithm, in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001589 *
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001590 * \param alg An AEAD algorithm
1591 * (\c PSA_ALG_XXX value such that
1592 * #PSA_ALG_IS_AEAD(alg) is true).
1593 *
1594 * \return The tag size for the specified algorithm.
1595 * If the AEAD algorithm does not have an identified
1596 * tag that can be distinguished from the rest of
1597 * the ciphertext, return 0.
1598 * If the AEAD algorithm is not recognized, return 0.
1599 * An implementation may return either 0 or a
1600 * correct size for an AEAD algorithm that it
1601 * recognizes, but does not support.
1602 */
1603#define PSA_AEAD_TAG_SIZE(alg) \
1604 ((alg) == PSA_ALG_GCM ? 16 : \
1605 (alg) == PSA_ALG_CCM ? 16 : \
1606 0)
Gilles Peskine3b555712018-03-03 21:27:57 +01001607
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001608/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001609 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001610 * \param key Slot containing the key to use.
1611 * \param alg The AEAD algorithm to compute
1612 * (\c PSA_ALG_XXX value such that
1613 * #PSA_ALG_IS_AEAD(alg) is true).
1614 * \param nonce Nonce or IV to use.
1615 * \param nonce_length Size of the \p nonce buffer in bytes.
1616 * \param additional_data Additional data that will be authenticated
1617 * but not encrypted.
1618 * \param additional_data_length Size of \p additional_data in bytes.
1619 * \param plaintext Data that will be authenticated and
1620 * encrypted.
1621 * \param plaintext_length Size of \p plaintext in bytes.
1622 * \param ciphertext Output buffer for the authenticated and
1623 * encrypted data. The additional data is not
1624 * part of this output. For algorithms where the
1625 * encrypted data and the authentication tag
1626 * are defined as separate outputs, the
1627 * authentication tag is appended to the
1628 * encrypted data.
1629 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1630 * This must be at least
1631 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1632 * \p plaintext_length).
1633 * \param ciphertext_length On success, the size of the output
1634 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001635 *
Gilles Peskine28538492018-07-11 17:34:00 +02001636 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001637 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001638 * \retval #PSA_ERROR_EMPTY_SLOT
1639 * \retval #PSA_ERROR_NOT_PERMITTED
1640 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3b555712018-03-03 21:27:57 +01001641 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001642 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine3b555712018-03-03 21:27:57 +01001643 * \c alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001644 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1645 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1646 * \retval #PSA_ERROR_HARDWARE_FAILURE
1647 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine3b555712018-03-03 21:27:57 +01001648 */
mohammad160339ee8712018-04-26 00:51:02 +03001649psa_status_t psa_aead_encrypt( psa_key_slot_t key,
1650 psa_algorithm_t alg,
1651 const uint8_t *nonce,
1652 size_t nonce_length,
1653 const uint8_t *additional_data,
1654 size_t additional_data_length,
1655 const uint8_t *plaintext,
1656 size_t plaintext_length,
1657 uint8_t *ciphertext,
1658 size_t ciphertext_size,
1659 size_t *ciphertext_length );
Gilles Peskine3b555712018-03-03 21:27:57 +01001660
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001661/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001662 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001663 * \param key Slot containing the key to use.
1664 * \param alg The AEAD algorithm to compute
1665 * (\c PSA_ALG_XXX value such that
1666 * #PSA_ALG_IS_AEAD(alg) is true).
1667 * \param nonce Nonce or IV to use.
1668 * \param nonce_length Size of the \p nonce buffer in bytes.
1669 * \param additional_data Additional data that has been authenticated
1670 * but not encrypted.
1671 * \param additional_data_length Size of \p additional_data in bytes.
1672 * \param ciphertext Data that has been authenticated and
1673 * encrypted. For algorithms where the
1674 * encrypted data and the authentication tag
1675 * are defined as separate inputs, the buffer
1676 * must contain the encrypted data followed
1677 * by the authentication tag.
1678 * \param ciphertext_length Size of \p ciphertext in bytes.
1679 * \param plaintext Output buffer for the decrypted data.
1680 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1681 * This must be at least
1682 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1683 * \p ciphertext_length).
1684 * \param plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001685 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001686 *
Gilles Peskine28538492018-07-11 17:34:00 +02001687 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001688 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001689 * \retval #PSA_ERROR_EMPTY_SLOT
1690 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001691 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001692 * \retval #PSA_ERROR_NOT_PERMITTED
1693 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3b555712018-03-03 21:27:57 +01001694 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001695 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine19067982018-03-20 17:54:53 +01001696 * \c alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001697 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1698 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1699 * \retval #PSA_ERROR_HARDWARE_FAILURE
1700 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine3b555712018-03-03 21:27:57 +01001701 */
mohammad160339ee8712018-04-26 00:51:02 +03001702psa_status_t psa_aead_decrypt( psa_key_slot_t key,
1703 psa_algorithm_t alg,
1704 const uint8_t *nonce,
1705 size_t nonce_length,
1706 const uint8_t *additional_data,
1707 size_t additional_data_length,
1708 const uint8_t *ciphertext,
1709 size_t ciphertext_length,
1710 uint8_t *plaintext,
1711 size_t plaintext_size,
1712 size_t *plaintext_length );
Gilles Peskine3b555712018-03-03 21:27:57 +01001713
1714/**@}*/
1715
Gilles Peskine20035e32018-02-03 22:44:14 +01001716/** \defgroup asymmetric Asymmetric cryptography
1717 * @{
1718 */
1719
1720/**
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001721 * \brief ECDSA signature size for a given curve bit size
Gilles Peskine0189e752018-02-03 23:57:22 +01001722 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001723 * \param curve_bits Curve size in bits.
1724 * \return Signature size in bytes.
Gilles Peskine0189e752018-02-03 23:57:22 +01001725 *
1726 * \note This macro returns a compile-time constant if its argument is one.
Gilles Peskine0189e752018-02-03 23:57:22 +01001727 */
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001728#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
1729 (PSA_BITS_TO_BYTES(curve_bits) * 2)
Gilles Peskine0189e752018-02-03 23:57:22 +01001730
Gilles Peskine0189e752018-02-03 23:57:22 +01001731/**
Gilles Peskine20035e32018-02-03 22:44:14 +01001732 * \brief Sign a hash or short message with a private key.
1733 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001734 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001735 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001736 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1737 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1738 * to determine the hash algorithm to use.
1739 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001740 * \param key Key slot containing an asymmetric key pair.
1741 * \param alg A signature algorithm that is compatible with
1742 * the type of \c key.
Gilles Peskine08bac712018-06-26 16:14:46 +02001743 * \param hash The hash or message to sign.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001744 * \param hash_length Size of the \c hash buffer in bytes.
1745 * \param salt A salt or label, if supported by the signature
1746 * algorithm.
1747 * If the signature algorithm does not support a
1748 * salt, pass \c NULL.
1749 * If the signature algorithm supports an optional
1750 * salt and you do not want to pass a salt,
1751 * pass \c NULL.
1752 * \param salt_length Size of the \c salt buffer in bytes.
1753 * If \c salt is \c NULL, pass 0.
1754 * \param signature Buffer where the signature is to be written.
1755 * \param signature_size Size of the \c signature buffer in bytes.
1756 * \param signature_length On success, the number of bytes
1757 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001758 *
Gilles Peskine28538492018-07-11 17:34:00 +02001759 * \retval #PSA_SUCCESS
1760 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine308b91d2018-02-08 09:47:44 +01001761 * The size of the \c signature buffer is too small. You can
1762 * determine a sufficient buffer size by calling
1763 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg)
1764 * where \c key_type and \c key_bits are the type and bit-size
1765 * respectively of \c key.
Gilles Peskine28538492018-07-11 17:34:00 +02001766 * \retval #PSA_ERROR_NOT_SUPPORTED
1767 * \retval #PSA_ERROR_INVALID_ARGUMENT
1768 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1769 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1770 * \retval #PSA_ERROR_HARDWARE_FAILURE
1771 * \retval #PSA_ERROR_TAMPERING_DETECTED
1772 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01001773 */
1774psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
1775 psa_algorithm_t alg,
1776 const uint8_t *hash,
1777 size_t hash_length,
1778 const uint8_t *salt,
1779 size_t salt_length,
1780 uint8_t *signature,
1781 size_t signature_size,
1782 size_t *signature_length);
1783
1784/**
1785 * \brief Verify the signature a hash or short message using a public key.
1786 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001787 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001788 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001789 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1790 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1791 * to determine the hash algorithm to use.
1792 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001793 * \param key Key slot containing a public key or an
1794 * asymmetric key pair.
1795 * \param alg A signature algorithm that is compatible with
1796 * the type of \c key.
Gilles Peskine08bac712018-06-26 16:14:46 +02001797 * \param hash The hash or message whose signature is to be
1798 * verified.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001799 * \param hash_length Size of the \c hash buffer in bytes.
1800 * \param salt A salt or label, if supported by the signature
1801 * algorithm.
1802 * If the signature algorithm does not support a
1803 * salt, pass \c NULL.
1804 * If the signature algorithm supports an optional
1805 * salt and you do not want to pass a salt,
1806 * pass \c NULL.
1807 * \param salt_length Size of the \c salt buffer in bytes.
1808 * If \c salt is \c NULL, pass 0.
1809 * \param signature Buffer containing the signature to verify.
Gilles Peskine526fab02018-06-27 18:19:40 +02001810 * \param signature_length Size of the \c signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001811 *
Gilles Peskine28538492018-07-11 17:34:00 +02001812 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001813 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02001814 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001815 * The calculation was perfomed successfully, but the passed
1816 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02001817 * \retval #PSA_ERROR_NOT_SUPPORTED
1818 * \retval #PSA_ERROR_INVALID_ARGUMENT
1819 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1820 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1821 * \retval #PSA_ERROR_HARDWARE_FAILURE
1822 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01001823 */
1824psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
1825 psa_algorithm_t alg,
1826 const uint8_t *hash,
1827 size_t hash_length,
1828 const uint8_t *salt,
1829 size_t salt_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02001830 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02001831 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01001832
Gilles Peskine723feff2018-05-31 20:08:13 +02001833#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
1834 (PSA_ALG_IS_RSA_OAEP_MGF1(alg) ? \
1835 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_GET_HASH(alg)) + 1 : \
1836 11 /*PKCS#1v1.5*/)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001837
1838/**
1839 * \brief Encrypt a short message with a public key.
1840 *
1841 * \param key Key slot containing a public key or an asymmetric
1842 * key pair.
1843 * \param alg An asymmetric encryption algorithm that is
1844 * compatible with the type of \c key.
1845 * \param input The message to encrypt.
1846 * \param input_length Size of the \c input buffer in bytes.
1847 * \param salt A salt or label, if supported by the encryption
1848 * algorithm.
1849 * If the algorithm does not support a
1850 * salt, pass \c NULL.
1851 * If the algorithm supports an optional
1852 * salt and you do not want to pass a salt,
1853 * pass \c NULL.
1854 *
1855 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1856 * supported.
1857 * \param salt_length Size of the \c salt buffer in bytes.
1858 * If \c salt is \c NULL, pass 0.
1859 * \param output Buffer where the encrypted message is to be written.
1860 * \param output_size Size of the \c output buffer in bytes.
1861 * \param output_length On success, the number of bytes
1862 * that make up the returned output.
1863 *
Gilles Peskine28538492018-07-11 17:34:00 +02001864 * \retval #PSA_SUCCESS
1865 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001866 * The size of the \c output buffer is too small. You can
1867 * determine a sufficient buffer size by calling
1868 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1869 * where \c key_type and \c key_bits are the type and bit-size
1870 * respectively of \c key.
Gilles Peskine28538492018-07-11 17:34:00 +02001871 * \retval #PSA_ERROR_NOT_SUPPORTED
1872 * \retval #PSA_ERROR_INVALID_ARGUMENT
1873 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1874 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1875 * \retval #PSA_ERROR_HARDWARE_FAILURE
1876 * \retval #PSA_ERROR_TAMPERING_DETECTED
1877 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001878 */
1879psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
1880 psa_algorithm_t alg,
1881 const uint8_t *input,
1882 size_t input_length,
1883 const uint8_t *salt,
1884 size_t salt_length,
1885 uint8_t *output,
1886 size_t output_size,
1887 size_t *output_length);
1888
1889/**
1890 * \brief Decrypt a short message with a private key.
1891 *
1892 * \param key Key slot containing an asymmetric key pair.
1893 * \param alg An asymmetric encryption algorithm that is
1894 * compatible with the type of \c key.
1895 * \param input The message to decrypt.
1896 * \param input_length Size of the \c input buffer in bytes.
1897 * \param salt A salt or label, if supported by the encryption
1898 * algorithm.
1899 * If the algorithm does not support a
1900 * salt, pass \c NULL.
1901 * If the algorithm supports an optional
1902 * salt and you do not want to pass a salt,
1903 * pass \c NULL.
1904 *
1905 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1906 * supported.
1907 * \param salt_length Size of the \c salt buffer in bytes.
1908 * If \c salt is \c NULL, pass 0.
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001909 * \param output Buffer where the decrypted message is to be written.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001910 * \param output_size Size of the \c output buffer in bytes.
1911 * \param output_length On success, the number of bytes
1912 * that make up the returned output.
1913 *
Gilles Peskine28538492018-07-11 17:34:00 +02001914 * \retval #PSA_SUCCESS
1915 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001916 * The size of the \c output buffer is too small. You can
1917 * determine a sufficient buffer size by calling
1918 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1919 * where \c key_type and \c key_bits are the type and bit-size
1920 * respectively of \c key.
Gilles Peskine28538492018-07-11 17:34:00 +02001921 * \retval #PSA_ERROR_NOT_SUPPORTED
1922 * \retval #PSA_ERROR_INVALID_ARGUMENT
1923 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1924 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1925 * \retval #PSA_ERROR_HARDWARE_FAILURE
1926 * \retval #PSA_ERROR_TAMPERING_DETECTED
1927 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
1928 * \retval #PSA_ERROR_INVALID_PADDING
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001929 */
1930psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
1931 psa_algorithm_t alg,
1932 const uint8_t *input,
1933 size_t input_length,
1934 const uint8_t *salt,
1935 size_t salt_length,
1936 uint8_t *output,
1937 size_t output_size,
1938 size_t *output_length);
1939
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001940/**@}*/
1941
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001942/** \defgroup generation Key generation
1943 * @{
1944 */
1945
1946/**
1947 * \brief Generate random bytes.
1948 *
1949 * \warning This function **can** fail! Callers MUST check the return status
1950 * and MUST NOT use the content of the output buffer if the return
1951 * status is not #PSA_SUCCESS.
1952 *
1953 * \note To generate a key, use psa_generate_key() instead.
1954 *
1955 * \param output Output buffer for the generated data.
1956 * \param output_size Number of bytes to generate and output.
1957 *
Gilles Peskine28538492018-07-11 17:34:00 +02001958 * \retval #PSA_SUCCESS
1959 * \retval #PSA_ERROR_NOT_SUPPORTED
1960 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
1961 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1962 * \retval #PSA_ERROR_HARDWARE_FAILURE
1963 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001964 */
1965psa_status_t psa_generate_random(uint8_t *output,
1966 size_t output_size);
1967
1968/**
1969 * \brief Generate a key or key pair.
1970 *
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02001971 * \param key Slot where the key will be stored. This must be a
1972 * valid slot for a key of the chosen type. It must
1973 * be unoccupied.
1974 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
1975 * \param bits Key size in bits.
1976 * \param parameters Extra parameters for key generation. The
1977 * interpretation of this parameter depends on
1978 * \c type. All types support \c NULL to use
1979 * the default parameters specified below.
Jaeden Amero7baf0d52018-06-26 18:02:59 +01001980 * \param parameters_size Size of the buffer that \p parameters
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02001981 * points to, in bytes.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001982 *
Gilles Peskine28538492018-07-11 17:34:00 +02001983 * For any symmetric key type (a type such that
1984 * #PSA_KEY_TYPE_IS_ASYMMETRIC(`type`) is false), \c parameters must be
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001985 * \c NULL. For asymmetric key types defined by this specification,
1986 * the parameter type and the default parameters are defined by the
1987 * table below. For vendor-defined key types, the vendor documentation
1988 * shall define the parameter type and the default parameters.
1989 *
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001990 * Type | Parameter type | Meaning | Parameters used if `parameters == NULL`
1991 * ---- | -------------- | ------- | ---------------------------------------
1992 * `PSA_KEY_TYPE_RSA_KEYPAIR` | `unsigned int` | Public exponent | 65537
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001993 *
Gilles Peskine28538492018-07-11 17:34:00 +02001994 * \retval #PSA_SUCCESS
1995 * \retval #PSA_ERROR_NOT_SUPPORTED
1996 * \retval #PSA_ERROR_INVALID_ARGUMENT
1997 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1998 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
1999 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2000 * \retval #PSA_ERROR_HARDWARE_FAILURE
2001 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002002 */
2003psa_status_t psa_generate_key(psa_key_slot_t key,
2004 psa_key_type_t type,
2005 size_t bits,
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002006 const void *parameters,
2007 size_t parameters_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002008
2009/**@}*/
2010
Gilles Peskinee59236f2018-01-27 23:32:46 +01002011#ifdef __cplusplus
2012}
2013#endif
2014
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002015/* The file "crypto_sizes.h" contains definitions for size calculation
2016 * macros whose definitions are implementation-specific. */
2017#include "crypto_sizes.h"
2018
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002019/* The file "crypto_struct.h" contains definitions for
2020 * implementation-specific structs that are declared above. */
2021#include "crypto_struct.h"
2022
2023/* The file "crypto_extra.h" contains vendor-specific definitions. This
2024 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002025#include "crypto_extra.h"
2026
2027#endif /* PSA_CRYPTO_H */