blob: 9780681be9484772407d2b1e13df9d914ea6c595 [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
48/**
49 * \brief Function return status.
50 *
51 * Zero indicates success, anything else indicates an error.
52 */
itayzafrirc2a79762018-06-18 16:20:16 +030053#if defined(PSA_SUCCESS)
54typedef psa_error_t psa_status_t;
55#else
56typedef int32_t psa_status_t;
57/** The action was completed successfully. */
58#define PSA_SUCCESS ((psa_status_t)0)
59#endif // PSA_SUCCESS
60
61/** The requested operation or a parameter is not supported
62 * by this implementation.
63 *
64 * Implementations should return this error code when an enumeration
65 * parameter such as a key type, algorithm, etc. is not recognized.
66 * If a combination of parameters is recognized and identified as
67 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
68#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)1)
69
70/** The requested action is denied by a policy.
71 *
72 * Implementations should return this error code when the parameters
73 * are recognized as valid and supported, and a policy explicitly
74 * denies the requested operation.
75 *
76 * If a subset of the parameters of a function call identify a
77 * forbidden operation, and another subset of the parameters are
78 * not valid or not supported, it is unspecified whether the function
79 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
80 * #PSA_ERROR_INVALID_ARGUMENT. */
81#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)2)
82
83/** An output buffer is too small.
84 *
85 * Applications can call the `PSA_xxx_SIZE` macro listed in the function
86 * description to determine a sufficient buffer size.
87 *
88 * Implementations should preferably return this error code only
89 * in cases when performing the operation with a larger output
90 * buffer would succeed. However implementations may return this
91 * error if a function has invalid or unsupported parameters in addition
92 * to the parameters that determine the necessary output buffer size. */
93#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)3)
94
95/** A slot is occupied, but must be empty to carry out the
96 * requested action.
97 *
98 * If the slot number is invalid (i.e. the requested action could
99 * not be performed even after erasing the slot's content),
100 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
101#define PSA_ERROR_OCCUPIED_SLOT ((psa_status_t)4)
102
103/** A slot is empty, but must be occupied to carry out the
104 * requested action.
105 *
106 * If the slot number is invalid (i.e. the requested action could
107 * not be performed even after creating appropriate content in the slot),
108 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
109#define PSA_ERROR_EMPTY_SLOT ((psa_status_t)5)
110
111/** The requested action cannot be performed in the current state.
112 *
113 * Multipart operations return this error when one of the
114 * functions is called out of sequence. Refer to the function
115 * descriptions for permitted sequencing of functions.
116 *
117 * Implementations shall not return this error code to indicate
118 * that a key slot is occupied when it needs to be free or vice versa,
119 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
120 * as applicable. */
121#define PSA_ERROR_BAD_STATE ((psa_status_t)6)
122
123/** The parameters passed to the function are invalid.
124 *
125 * Implementations may return this error any time a parameter or
126 * combination of parameters are recognized as invalid.
127 *
128 * Implementations shall not return this error code to indicate
129 * that a key slot is occupied when it needs to be free or vice versa,
130 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
131 * as applicable. */
132#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)7)
133
134/** There is not enough runtime memory.
135 *
136 * If the action is carried out across multiple security realms, this
137 * error can refer to available memory in any of the security realms. */
138#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)8)
139
140/** There is not enough persistent storage.
141 *
142 * Functions that modify the key storage return this error code if
143 * there is insufficient storage space on the host media. In addition,
144 * many functions that do not otherwise access storage may return this
145 * error code if the implementation requires a mandatory log entry for
146 * the requested action and the log storage space is full. */
147#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)9)
148
149/** There was a communication failure inside the implementation.
150 *
151 * This can indicate a communication failure between the application
152 * and an external cryptoprocessor or between the cryptoprocessor and
153 * an external volatile or persistent memory. A communication failure
154 * may be transient or permanent depending on the cause.
155 *
156 * \warning If a function returns this error, it is undetermined
157 * whether the requested action has completed or not. Implementations
158 * should return #PSA_SUCCESS on successful completion whenver
159 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
160 * if the requested action was completed successfully in an external
161 * cryptoprocessor but there was a breakdown of communication before
162 * the cryptoprocessor could report the status to the application.
163 */
164#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)10)
165
166/** There was a storage failure that may have led to data loss.
167 *
168 * This error indicates that some persistent storage is corrupted.
169 * It should not be used for a corruption of volatile memory
170 * (use #PSA_ERROR_TAMPERING_DETECTED), for a communication error
171 * between the cryptoprocessor and its external storage (use
172 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
173 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
174 *
175 * Note that a storage failure does not indicate that any data that was
176 * previously read is invalid. However this previously read data may no
177 * longer be readable from storage.
178 *
179 * When a storage failure occurs, it is no longer possible to ensure
180 * the global integrity of the keystore. Depending on the global
181 * integrity guarantees offered by the implementation, access to other
182 * data may or may not fail even if the data is still readable but
183 * its integrity canont be guaranteed.
184 *
185 * Implementations should only use this error code to report a
186 * permanent storage corruption. However application writers should
187 * keep in mind that transient errors while reading the storage may be
188 * reported using this error code. */
189#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)11)
190
191/** A hardware failure was detected.
192 *
193 * A hardware failure may be transient or permanent depending on the
194 * cause. */
195#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)12)
196
197/** A tampering attempt was detected.
198 *
199 * If an application receives this error code, there is no guarantee
200 * that previously accessed or computed data was correct and remains
201 * confidential. Applications should not perform any security function
202 * and should enter a safe failure state.
203 *
204 * Implementations may return this error code if they detect an invalid
205 * state that cannot happen during normal operation and that indicates
206 * that the implementation's security guarantees no longer hold. Depending
207 * on the implementation architecture and on its security and safety goals,
208 * the implementation may forcibly terminate the application.
209 *
210 * This error code is intended as a last resort when a security breach
211 * is detected and it is unsure whether the keystore data is still
212 * protected. Implementations shall only return this error code
213 * to report an alarm from a tampering detector, to indicate that
214 * the confidentiality of stored data can no longer be guaranteed,
215 * or to indicate that the integrity of previously returned data is now
216 * considered compromised. Implementations shall not use this error code
217 * to indicate a hardware failure that merely makes it impossible to
218 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
219 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
220 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
221 * instead).
222 *
223 * This error indicates an attack against the application. Implementations
224 * shall not return this error code as a consequence of the behavior of
225 * the application itself. */
226#define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)13)
227
228/** There is not enough entropy to generate random data needed
229 * for the requested action.
230 *
231 * This error indicates a failure of a hardware random generator.
232 * Application writers should note that this error can be returned not
233 * only by functions whose purpose is to generate random data, such
234 * as key, IV or nonce generation, but also by functions that execute
235 * an algorithm with a randomized result, as well as functions that
236 * use randomization of intermediate computations as a countermeasure
237 * to certain attacks.
238 *
239 * Implementations should avoid returning this error after psa_crypto_init()
240 * has succeeded. Implementations should generate sufficient
241 * entropy during initialization and subsequently use a cryptographically
242 * secure pseudorandom generator (PRNG). However implementations may return
243 * this error at any time if a policy requires the PRNG to be reseeded
244 * during normal operation. */
245#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)14)
246
247/** The signature, MAC or hash is incorrect.
248 *
249 * Verification functions return this error if the verification
250 * calculations completed successfully, and the value to be verified
251 * was determined to be incorrect.
252 *
253 * If the value to verify has an invalid size, implementations may return
254 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
255#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)15)
256
257/** The decrypted padding is incorrect.
258 *
259 * \warning In some protocols, when decrypting data, it is essential that
260 * the behavior of the application does not depend on whether the padding
261 * is correct, down to precise timing. Applications should prefer
262 * protocols that use authenticated encryption rather than plain
263 * encryption. If the application must perform a decryption of
264 * unauthenticated data, the application writer should take care not
265 * to reveal whether the padding is invalid.
266 *
267 * Implementations should strive to make valid and invalid padding
268 * as close as possible to indistinguishable to an external observer.
269 * In particular, the timing of a decryption operation should not
270 * depend on the validity of the padding. */
271#define PSA_ERROR_INVALID_PADDING ((psa_status_t)16)
272
273/** An error occurred that does not correspond to any defined
274 * failure cause.
275 *
276 * Implementations may use this error code if none of the other standard
277 * error codes are applicable. */
278#define PSA_ERROR_UNKNOWN_ERROR ((psa_status_t)17)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100279
280/**
281 * \brief Library initialization.
282 *
283 * Applications must call this function before calling any other
284 * function in this module.
285 *
286 * Applications may call this function more than once. Once a call
287 * succeeds, subsequent calls are guaranteed to succeed.
288 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100289 * \retval PSA_SUCCESS
290 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
291 * \retval PSA_ERROR_COMMUNICATION_FAILURE
292 * \retval PSA_ERROR_HARDWARE_FAILURE
293 * \retval PSA_ERROR_TAMPERING_DETECTED
294 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +0100295 */
296psa_status_t psa_crypto_init(void);
297
Gilles Peskine2905a7a2018-03-07 16:39:31 +0100298#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
299#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
Gilles Peskine0189e752018-02-03 23:57:22 +0100300
Gilles Peskinee59236f2018-01-27 23:32:46 +0100301/**@}*/
302
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100303/** \defgroup crypto_types Key and algorithm types
304 * @{
305 */
306
Gilles Peskine308b91d2018-02-08 09:47:44 +0100307/** \brief Encoding of a key type.
308 */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100309typedef uint32_t psa_key_type_t;
310
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100311/** An invalid key type value.
312 *
313 * Zero is not the encoding of any key type.
314 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100315#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100316
317/** Vendor-defined flag
318 *
319 * Key types defined by this standard will never have the
320 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
321 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
322 * respect the bitwise structure used by standard encodings whenever practical.
323 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100324#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100325
Gilles Peskine98f0a242018-02-06 18:57:29 +0100326#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7e000000)
Gilles Peskine35855962018-04-19 08:39:16 +0200327/** Raw data.
328 *
329 * A "key" of this type cannot be used for any cryptographic operation.
330 * Applications may use this type to store arbitrary data in the keystore. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100331#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x02000000)
332#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x04000000)
333#define PSA_KEY_TYPE_CATEGORY_ASYMMETRIC ((psa_key_type_t)0x06000000)
334#define PSA_KEY_TYPE_PAIR_FLAG ((psa_key_type_t)0x01000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100335
Gilles Peskine35855962018-04-19 08:39:16 +0200336/** HMAC key.
337 *
338 * The key policy determines which underlying hash algorithm the key can be
339 * used for.
340 *
341 * HMAC keys should generally have the same size as the underlying hash.
342 * This size can be calculated with `PSA_HASH_SIZE(alg)` where
343 * `alg` is the HMAC algorithm or the underlying hash algorithm. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100344#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x02000001)
Gilles Peskine35855962018-04-19 08:39:16 +0200345/** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher.
346 *
347 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
348 * 32 bytes (AES-256).
349 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100350#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x04000001)
Gilles Peskine35855962018-04-19 08:39:16 +0200351/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
352 *
353 * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or
354 * 24 bytes (3-key 3DES).
355 *
356 * Note that single DES and 2-key 3DES are weak and strongly
357 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
358 * is weak and deprecated and should only be used in legacy protocols.
359 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100360#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x04000002)
Gilles Peskine35855962018-04-19 08:39:16 +0200361/** Key for an cipher, AEAD or MAC algorithm based on the
362 * Camellia block cipher. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100363#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x04000003)
Gilles Peskine35855962018-04-19 08:39:16 +0200364/** Key for the RC4 stream cipher.
365 *
366 * Note that RC4 is weak and deprecated and should only be used in
367 * legacy protocols. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100368#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x04000004)
369
Gilles Peskine308b91d2018-02-08 09:47:44 +0100370/** RSA public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100371#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x06010000)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100372/** RSA key pair (private and public key). */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100373#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x07010000)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100374/** DSA public key. */
375#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x06020000)
376/** DSA key pair (private and public key). */
377#define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x07020000)
378#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x06030000)
379#define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x07030000)
itayzafrir5c753392018-05-08 11:18:38 +0300380#define PSA_KEY_TYPE_ECC_CURVE_NISTP256R1 ((psa_key_type_t)0x00000001)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100381#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100382#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
383 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
384#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
385 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
Gilles Peskine98f0a242018-02-06 18:57:29 +0100386
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100387/** Whether a key type is vendor-defined. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100388#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100389 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100390#define PSA_KEY_TYPE_IS_RAW_BYTES(type) \
391 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_RAW_DATA || \
392 ((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100393
394/** Whether a key type is asymmetric: either a key pair or a public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100395#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
396 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100397/** Whether a key type is the public part of a key pair. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100398#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
Moran Pekerb4d0ddd2018-04-04 12:47:52 +0300399 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
400 PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100401/** Whether a key type is a key pair containing a private part and a public
402 * part. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100403#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
404 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
405 (PSA_KEY_TYPE_CATEGORY_ASYMMETRIC | PSA_KEY_TYPE_PAIR_FLAG))
Gilles Peskine06dc2632018-03-08 07:47:25 +0100406/** Whether a key type is an RSA key pair or public key. */
407/** The key pair type corresponding to a public key type. */
408#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
409 ((type) | PSA_KEY_TYPE_PAIR_FLAG)
410/** The public key type corresponding to a key pair type. */
411#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
412 ((type) & ~PSA_KEY_TYPE_PAIR_FLAG)
Gilles Peskine0189e752018-02-03 23:57:22 +0100413#define PSA_KEY_TYPE_IS_RSA(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100414 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
415/** Whether a key type is an elliptic curve key pair or public key. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100416#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100417 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
418 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100419
Gilles Peskine7e198532018-03-08 07:50:30 +0100420/** The block size of a block cipher.
421 *
422 * \param type A cipher key type (value of type #psa_key_type_t).
423 *
424 * \return The block size for a block cipher, or 1 for a stream cipher.
Gilles Peskine35855962018-04-19 08:39:16 +0200425 * The return value is undefined if \c type is not a supported
426 * cipher key type.
427 *
428 * \note It is possible to build stream cipher algorithms on top of a block
429 * cipher, for example CTR mode (#PSA_ALG_CTR).
430 * This macro only takes the key type into account, so it cannot be
431 * used to determine the size of the data that #psa_cipher_update()
432 * might buffer for future processing in general.
Gilles Peskine7e198532018-03-08 07:50:30 +0100433 *
434 * \note This macro returns a compile-time constant if its argument is one.
435 *
436 * \warning This macro may evaluate its argument multiple times.
437 */
Gilles Peskine03182e92018-03-07 16:40:52 +0100438#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100439 ( \
440 (type) == PSA_KEY_TYPE_AES ? 16 : \
441 (type) == PSA_KEY_TYPE_DES ? 8 : \
442 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
Gilles Peskine7e198532018-03-08 07:50:30 +0100443 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100444 0)
445
Gilles Peskine308b91d2018-02-08 09:47:44 +0100446/** \brief Encoding of a cryptographic algorithm.
447 *
448 * For algorithms that can be applied to multiple key types, this type
449 * does not encode the key type. For example, for symmetric ciphers
450 * based on a block cipher, #psa_algorithm_t encodes the block cipher
451 * mode and the padding mode while the block cipher itself is encoded
452 * via #psa_key_type_t.
453 */
Gilles Peskine20035e32018-02-03 22:44:14 +0100454typedef uint32_t psa_algorithm_t;
455
Gilles Peskine98f0a242018-02-06 18:57:29 +0100456#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
457#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
458#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
459#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
460#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
461#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
462#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
463#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
464#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
465#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100466
Gilles Peskine98f0a242018-02-06 18:57:29 +0100467#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
468 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100469/** Whether the specified algorithm is a hash algorithm.
470 *
Gilles Peskine7e198532018-03-08 07:50:30 +0100471 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100472 *
473 * \return 1 if \c alg is a hash algorithm, 0 otherwise.
474 * This macro may return either 0 or 1 if \c alg is not a valid
Gilles Peskine7e198532018-03-08 07:50:30 +0100475 * algorithm identifier.
476 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100477#define PSA_ALG_IS_HASH(alg) \
478 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
479#define PSA_ALG_IS_MAC(alg) \
480 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
481#define PSA_ALG_IS_CIPHER(alg) \
482 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
483#define PSA_ALG_IS_AEAD(alg) \
484 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
485#define PSA_ALG_IS_SIGN(alg) \
486 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
487#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
488 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
489#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
490 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
491#define PSA_ALG_IS_KEY_DERIVATION(alg) \
492 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
493
494#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
495#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
496#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
497#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
Gilles Peskinee3f694f2018-03-08 07:48:40 +0100498#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
499#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100500#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
501#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
502#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
503#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
504#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
505#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
506#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
507#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
508#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
509#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
510
Gilles Peskine8c9def32018-02-08 10:02:12 +0100511#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100512#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
Gilles Peskine35855962018-04-19 08:39:16 +0200513/** Macro to build an HMAC algorithm.
514 *
515 * For example, `PSA_ALG_HMAC(PSA_ALG_SHA256)` is HMAC-SHA-256.
516 *
517 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
518 * #PSA_ALG_IS_HASH(alg) is true).
519 *
520 * \return The corresponding HMAC algorithm.
521 * \return Unspecified if \p alg is not a hash algorithm.
522 */
523#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100524 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
525#define PSA_ALG_HMAC_HASH(hmac_alg) \
526 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
527#define PSA_ALG_IS_HMAC(alg) \
528 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
529 PSA_ALG_HMAC_BASE)
530#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
531#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
532#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
533#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
534#define PSA_ALG_IS_CIPHER_MAC(alg) \
535 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
536 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100537
Gilles Peskine8c9def32018-02-08 10:02:12 +0100538#define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100539#define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100540#define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100541#define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000)
542#define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100543#define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100544#define PSA_ALG_IS_BLOCK_CIPHER(alg) \
545 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
546 PSA_ALG_BLOCK_CIPHER_BASE)
547
Gilles Peskine98f0a242018-02-06 18:57:29 +0100548#define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100549#define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002)
550#define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003)
551#define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100552#define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t)0x04800000)
553#define PSA_ALG_CTR ((psa_algorithm_t)0x04800001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100554#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100555
Moran Pekerbed71a22018-04-22 20:19:20 +0300556#define PSA_ALG_IS_STREAM_CIPHER(alg) \
557 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
558 PSA_ALG_STREAM_CIPHER)
559
Gilles Peskine8c9def32018-02-08 10:02:12 +0100560#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
561#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100562
Gilles Peskinea5926232018-03-28 14:16:50 +0200563#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW ((psa_algorithm_t)0x10010000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100564#define PSA_ALG_RSA_PSS_MGF1 ((psa_algorithm_t)0x10020000)
Gilles Peskine6944f9a2018-03-28 14:18:39 +0200565#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12010000)
566#define PSA_ALG_RSA_OAEP_MGF1_BASE ((psa_algorithm_t)0x12020000)
Gilles Peskinea5926232018-03-28 14:16:50 +0200567#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
568 (PSA_ALG_RSA_PKCS1V15_SIGN_RAW | ((hash_alg) & PSA_ALG_HASH_MASK))
569#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Gilles Peskine9673cc82018-04-11 16:57:49 +0200570 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_RAW)
571#define PSA_ALG_RSA_OAEP_MGF1(hash_alg) \
572 (PSA_ALG_RSA_OAEP_MGF1_RAW | ((hash_alg) & PSA_ALG_HASH_MASK))
573#define PSA_ALG_IS_RSA_OAEP_MGF1(alg) \
Gilles Peskine625b01c2018-06-08 17:43:16 +0200574 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_MGF1_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100575#define PSA_ALG_RSA_GET_HASH(alg) \
576 (((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100577
Gilles Peskined1e8e412018-06-07 09:49:39 +0200578#define PSA_ALG_ECDSA_RAW ((psa_algorithm_t)0x10030000)
579
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100580/**@}*/
581
582/** \defgroup key_management Key management
583 * @{
584 */
585
586/**
587 * \brief Import a key in binary format.
588 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100589 * This function supports any output from psa_export_key(). Refer to the
590 * documentation of psa_export_key() for the format for each key type.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100591 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100592 * \param key Slot where the key will be stored. This must be a
593 * valid slot for a key of the chosen type. It must
594 * be unoccupied.
595 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
596 * \param data Buffer containing the key data.
597 * \param data_length Size of the \c data buffer in bytes.
598 *
599 * \retval PSA_SUCCESS
600 * Success.
601 * \retval PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200602 * The key type or key size is not supported, either by the
603 * implementation in general or in this particular slot.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100604 * \retval PSA_ERROR_INVALID_ARGUMENT
605 * The key slot is invalid,
606 * or the key data is not correctly formatted.
607 * \retval PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +0200608 * There is already a key in the specified slot.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100609 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine65eb8582018-04-19 08:28:58 +0200610 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100611 * \retval PSA_ERROR_COMMUNICATION_FAILURE
612 * \retval PSA_ERROR_HARDWARE_FAILURE
613 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100614 */
615psa_status_t psa_import_key(psa_key_slot_t key,
616 psa_key_type_t type,
617 const uint8_t *data,
618 size_t data_length);
619
620/**
Gilles Peskine154bd952018-04-19 08:38:16 +0200621 * \brief Destroy a key and restore the slot to its default state.
622 *
623 * This function destroys the content of the key slot from both volatile
624 * memory and, if applicable, non-volatile storage. Implementations shall
625 * make a best effort to ensure that any previous content of the slot is
626 * unrecoverable.
627 *
628 * This function also erases any metadata such as policies. It returns the
629 * specified slot to its default state.
630 *
631 * \param key The key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100632 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100633 * \retval PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200634 * The slot's content, if any, has been erased.
635 * \retval PSA_ERROR_NOT_PERMITTED
636 * The slot holds content and cannot be erased because it is
637 * read-only, either due to a policy or due to physical restrictions.
638 * \retval PSA_ERROR_INVALID_ARGUMENT
639 * The specified slot number does not designate a valid slot.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100640 * \retval PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200641 * There was an failure in communication with the cryptoprocessor.
642 * The key material may still be present in the cryptoprocessor.
643 * \retval PSA_ERROR_STORAGE_FAILURE
644 * The storage is corrupted. Implementations shall make a best effort
645 * to erase key material even in this stage, however applications
646 * should be aware that it may be impossible to guarantee that the
647 * key material is not recoverable in such cases.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100648 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200649 * An unexpected condition which is not a storage corruption or
650 * a communication failure occurred. The cryptoprocessor may have
651 * been compromised.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100652 */
653psa_status_t psa_destroy_key(psa_key_slot_t key);
654
655/**
656 * \brief Get basic metadata about a key.
657 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100658 * \param key Slot whose content is queried. This must
659 * be an occupied key slot.
660 * \param type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
661 * This may be a null pointer, in which case the key type
662 * is not written.
663 * \param bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100664 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100665 * is not written.
666 *
667 * \retval PSA_SUCCESS
668 * \retval PSA_ERROR_EMPTY_SLOT
669 * \retval PSA_ERROR_COMMUNICATION_FAILURE
670 * \retval PSA_ERROR_HARDWARE_FAILURE
671 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100672 */
673psa_status_t psa_get_key_information(psa_key_slot_t key,
674 psa_key_type_t *type,
675 size_t *bits);
676
677/**
678 * \brief Export a key in binary format.
679 *
680 * The output of this function can be passed to psa_import_key() to
681 * create an equivalent object.
682 *
683 * If a key is created with psa_import_key() and then exported with
684 * this function, it is not guaranteed that the resulting data is
685 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +0100686 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100687 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100688 * For standard key types, the output format is as follows:
689 *
690 * - For symmetric keys (including MAC keys), the format is the
691 * raw bytes of the key.
692 * - For DES, the key data consists of 8 bytes. The parity bits must be
693 * correct.
694 * - For Triple-DES, the format is the concatenation of the
695 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100696 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine308b91d2018-02-08 09:47:44 +0100697 * is the non-encrypted DER representation defined by PKCS\#8 (RFC 5208)
698 * as PrivateKeyInfo.
699 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
Gilles Peskine971f7062018-03-20 17:52:58 +0100700 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100701 *
702 * \param key Slot whose content is to be exported. This must
703 * be an occupied key slot.
704 * \param data Buffer where the key data is to be written.
705 * \param data_size Size of the \c data buffer in bytes.
706 * \param data_length On success, the number of bytes
707 * that make up the key data.
708 *
709 * \retval PSA_SUCCESS
710 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine92b30732018-03-03 21:29:30 +0100711 * \retval PSA_ERROR_NOT_PERMITTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100712 * \retval PSA_ERROR_COMMUNICATION_FAILURE
713 * \retval PSA_ERROR_HARDWARE_FAILURE
714 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100715 */
716psa_status_t psa_export_key(psa_key_slot_t key,
717 uint8_t *data,
718 size_t data_size,
719 size_t *data_length);
720
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100721/**
722 * \brief Export a public key or the public part of a key pair in binary format.
723 *
724 * The output of this function can be passed to psa_import_key() to
725 * create an object that is equivalent to the public key.
726 *
727 * For standard key types, the output format is as follows:
728 *
729 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
Moran Pekerdd4ea382018-04-03 15:30:03 +0300730 * the format is the DER representation of the public key defined by RFC 5280
Gilles Peskine971f7062018-03-20 17:52:58 +0100731 * as SubjectPublicKeyInfo.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100732 *
733 * \param key Slot whose content is to be exported. This must
734 * be an occupied key slot.
735 * \param data Buffer where the key data is to be written.
736 * \param data_size Size of the \c data buffer in bytes.
737 * \param data_length On success, the number of bytes
738 * that make up the key data.
739 *
740 * \retval PSA_SUCCESS
741 * \retval PSA_ERROR_EMPTY_SLOT
742 * \retval PSA_ERROR_INVALID_ARGUMENT
743 * \retval PSA_ERROR_COMMUNICATION_FAILURE
744 * \retval PSA_ERROR_HARDWARE_FAILURE
745 * \retval PSA_ERROR_TAMPERING_DETECTED
746 */
747psa_status_t psa_export_public_key(psa_key_slot_t key,
748 uint8_t *data,
749 size_t data_size,
750 size_t *data_length);
751
752/**@}*/
753
754/** \defgroup policy Key policies
755 * @{
756 */
757
758/** \brief Encoding of permitted usage on a key. */
759typedef uint32_t psa_key_usage_t;
760
Gilles Peskine7e198532018-03-08 07:50:30 +0100761/** Whether the key may be exported.
762 *
763 * A public key or the public part of a key pair may always be exported
764 * regardless of the value of this permission flag.
765 *
766 * If a key does not have export permission, implementations shall not
767 * allow the key to be exported in plain form from the cryptoprocessor,
768 * whether through psa_export_key() or through a proprietary interface.
769 * The key may however be exportable in a wrapped form, i.e. in a form
770 * where it is encrypted by another key.
771 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100772#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
773
Gilles Peskine7e198532018-03-08 07:50:30 +0100774/** Whether the key may be used to encrypt a message.
775 *
776 * For a key pair, this concerns the public key.
777 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100778#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +0100779
780/** Whether the key may be used to decrypt a message.
781 *
782 * For a key pair, this concerns the private key.
783 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100784#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +0100785
786/** Whether the key may be used to sign a message.
787 *
788 * For a key pair, this concerns the private key.
789 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100790#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +0100791
792/** Whether the key may be used to verify a message signature.
793 *
794 * For a key pair, this concerns the public key.
795 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100796#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
797
798/** The type of the key policy data structure.
799 *
800 * This is an implementation-defined \c struct. Applications should not
801 * make any assumptions about the content of this structure except
802 * as directed by the documentation of a specific implementation. */
803typedef struct psa_key_policy_s psa_key_policy_t;
804
805/** \brief Initialize a key policy structure to a default that forbids all
806 * usage of the key. */
807void psa_key_policy_init(psa_key_policy_t *policy);
808
Gilles Peskine7e198532018-03-08 07:50:30 +0100809/** \brief Set the standard fields of a policy structure.
810 *
811 * Note that this function does not make any consistency check of the
812 * parameters. The values are only checked when applying the policy to
813 * a key slot with psa_set_key_policy().
814 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100815void psa_key_policy_set_usage(psa_key_policy_t *policy,
816 psa_key_usage_t usage,
817 psa_algorithm_t alg);
818
819psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy);
820
821psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy);
822
823/** \brief Set the usage policy on a key slot.
824 *
825 * This function must be called on an empty key slot, before importing,
826 * generating or creating a key in the slot. Changing the policy of an
827 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100828 *
829 * Implementations may set restrictions on supported key policies
830 * depending on the key type and the key slot.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100831 */
832psa_status_t psa_set_key_policy(psa_key_slot_t key,
833 const psa_key_policy_t *policy);
834
Gilles Peskine7e198532018-03-08 07:50:30 +0100835/** \brief Get the usage policy for a key slot.
836 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100837psa_status_t psa_get_key_policy(psa_key_slot_t key,
838 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100839
840/**@}*/
841
Gilles Peskine609b6a52018-03-03 21:31:50 +0100842/** \defgroup persistence Key lifetime
843 * @{
844 */
845
846/** Encoding of key lifetimes.
847 */
848typedef uint32_t psa_key_lifetime_t;
849
850/** A volatile key slot retains its content as long as the application is
851 * running. It is guaranteed to be erased on a power reset.
852 */
853#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
854
855/** A persistent key slot retains its content as long as it is not explicitly
856 * destroyed.
857 */
858#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
859
860/** A write-once key slot may not be modified once a key has been set.
861 * It will retain its content as long as the device remains operational.
862 */
863#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
864
Gilles Peskined393e182018-03-08 07:49:16 +0100865/** \brief Retrieve the lifetime of a key slot.
866 *
867 * The assignment of lifetimes to slots is implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200868 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +0200869 * \param key Slot to query.
mohammad1603804cd712018-03-20 22:44:08 +0200870 * \param lifetime On success, the lifetime value.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200871 *
mohammad1603804cd712018-03-20 22:44:08 +0200872 * \retval PSA_SUCCESS
873 * Success.
874 * \retval PSA_ERROR_INVALID_ARGUMENT
mohammad1603a7d245a2018-04-17 00:40:08 -0700875 * The key slot is invalid.
Gilles Peskinef0c9dd32018-04-17 14:11:07 +0200876 * \retval PSA_ERROR_COMMUNICATION_FAILURE
877 * \retval PSA_ERROR_HARDWARE_FAILURE
878 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +0100879 */
Gilles Peskine609b6a52018-03-03 21:31:50 +0100880psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
881 psa_key_lifetime_t *lifetime);
882
Gilles Peskined393e182018-03-08 07:49:16 +0100883/** \brief Change the lifetime of a key slot.
884 *
885 * Whether the lifetime of a key slot can be changed at all, and if so
Gilles Peskine19067982018-03-20 17:54:53 +0100886 * whether the lifetime of an occupied key slot can be changed, is
Gilles Peskined393e182018-03-08 07:49:16 +0100887 * implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200888 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +0200889 * \param key Slot whose lifetime is to be changed.
890 * \param lifetime The lifetime value to set for the given key slot.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200891 *
mohammad1603804cd712018-03-20 22:44:08 +0200892 * \retval PSA_SUCCESS
893 * Success.
894 * \retval PSA_ERROR_INVALID_ARGUMENT
895 * The key slot is invalid,
mohammad1603a7d245a2018-04-17 00:40:08 -0700896 * or the lifetime value is invalid.
Gilles Peskinef0c9dd32018-04-17 14:11:07 +0200897 * \retval PSA_ERROR_NOT_SUPPORTED
898 * The implementation does not support the specified lifetime value,
899 * at least for the specified key slot.
900 * \retval PSA_ERROR_OCCUPIED_SLOT
901 * The slot contains a key, and the implementation does not support
902 * changing the lifetime of an occupied slot.
903 * \retval PSA_ERROR_COMMUNICATION_FAILURE
904 * \retval PSA_ERROR_HARDWARE_FAILURE
905 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +0100906 */
907psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
mohammad1603ea050092018-04-17 00:31:34 -0700908 psa_key_lifetime_t lifetime);
Gilles Peskined393e182018-03-08 07:49:16 +0100909
Gilles Peskine609b6a52018-03-03 21:31:50 +0100910/**@}*/
911
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100912/** \defgroup hash Message digests
913 * @{
914 */
915
Gilles Peskine308b91d2018-02-08 09:47:44 +0100916/** The type of the state data structure for multipart hash operations.
917 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100918 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100919 * make any assumptions about the content of this structure except
920 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100921typedef struct psa_hash_operation_s psa_hash_operation_t;
922
Gilles Peskine308b91d2018-02-08 09:47:44 +0100923/** The size of the output of psa_hash_finish(), in bytes.
924 *
925 * This is also the hash size that psa_hash_verify() expects.
926 *
927 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine35855962018-04-19 08:39:16 +0200928 * #PSA_ALG_IS_HASH(alg) is true), or an HMAC algorithm
929 * (`PSA_ALG_HMAC(hash_alg)` where `hash_alg` is a
930 * hash algorithm).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100931 *
932 * \return The hash size for the specified hash algorithm.
933 * If the hash algorithm is not recognized, return 0.
934 * An implementation may return either 0 or the correct size
935 * for a hash algorithm that it recognizes, but does not support.
936 */
Gilles Peskine71bb7b72018-04-19 08:29:59 +0200937#define PSA_HASH_SIZE(alg) \
938 ( \
939 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
940 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \
941 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
942 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
943 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
944 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
945 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
946 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
947 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
948 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
949 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
950 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
951 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
952 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
953 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100954 0)
955
Gilles Peskine308b91d2018-02-08 09:47:44 +0100956/** Start a multipart hash operation.
957 *
958 * The sequence of operations to calculate a hash (message digest)
959 * is as follows:
960 * -# Allocate an operation object which will be passed to all the functions
961 * listed here.
962 * -# Call psa_hash_start() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100963 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100964 * of the message each time. The hash that is calculated is the hash
965 * of the concatenation of these messages in order.
966 * -# To calculate the hash, call psa_hash_finish().
967 * To compare the hash with an expected value, call psa_hash_verify().
968 *
969 * The application may call psa_hash_abort() at any time after the operation
970 * has been initialized with psa_hash_start().
971 *
972 * After a successful call to psa_hash_start(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100973 * eventually terminate the operation. The following events terminate an
974 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100975 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100976 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100977 *
Gilles Peskine36a74b72018-06-01 16:30:32 +0200978 * \param operation The operation object to use.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100979 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
980 * such that #PSA_ALG_IS_HASH(alg) is true).
981 *
982 * \retval PSA_SUCCESS
983 * Success.
984 * \retval PSA_ERROR_NOT_SUPPORTED
985 * \c alg is not supported or is not a hash algorithm.
986 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
987 * \retval PSA_ERROR_COMMUNICATION_FAILURE
988 * \retval PSA_ERROR_HARDWARE_FAILURE
989 * \retval PSA_ERROR_TAMPERING_DETECTED
990 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100991psa_status_t psa_hash_start(psa_hash_operation_t *operation,
992 psa_algorithm_t alg);
993
Gilles Peskine308b91d2018-02-08 09:47:44 +0100994/** Add a message fragment to a multipart hash operation.
995 *
996 * The application must call psa_hash_start() before calling this function.
997 *
998 * If this function returns an error status, the operation becomes inactive.
999 *
1000 * \param operation Active hash operation.
1001 * \param input Buffer containing the message fragment to hash.
1002 * \param input_length Size of the \c input buffer in bytes.
1003 *
1004 * \retval PSA_SUCCESS
1005 * Success.
1006 * \retval PSA_ERROR_BAD_STATE
1007 * The operation state is not valid (not started, or already completed).
1008 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1009 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1010 * \retval PSA_ERROR_HARDWARE_FAILURE
1011 * \retval PSA_ERROR_TAMPERING_DETECTED
1012 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001013psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1014 const uint8_t *input,
1015 size_t input_length);
1016
Gilles Peskine308b91d2018-02-08 09:47:44 +01001017/** Finish the calculation of the hash of a message.
1018 *
1019 * The application must call psa_hash_start() before calling this function.
1020 * This function calculates the hash of the message formed by concatenating
1021 * the inputs passed to preceding calls to psa_hash_update().
1022 *
1023 * When this function returns, the operation becomes inactive.
1024 *
1025 * \warning Applications should not call this function if they expect
1026 * a specific value for the hash. Call psa_hash_verify() instead.
1027 * Beware that comparing integrity or authenticity data such as
1028 * hash values with a function such as \c memcmp is risky
1029 * because the time taken by the comparison may leak information
1030 * about the hashed data which could allow an attacker to guess
1031 * a valid hash and thereby bypass security controls.
1032 *
1033 * \param operation Active hash operation.
1034 * \param hash Buffer where the hash is to be written.
1035 * \param hash_size Size of the \c hash buffer in bytes.
1036 * \param hash_length On success, the number of bytes
1037 * that make up the hash value. This is always
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001038 * #PSA_HASH_SIZE(alg) where \c alg is the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001039 * hash algorithm that is calculated.
1040 *
1041 * \retval PSA_SUCCESS
1042 * Success.
1043 * \retval PSA_ERROR_BAD_STATE
1044 * The operation state is not valid (not started, or already completed).
1045 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1046 * The size of the \c hash buffer is too small. You can determine a
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001047 * sufficient buffer size by calling #PSA_HASH_SIZE(alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001048 * where \c alg is the hash algorithm that is calculated.
1049 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1050 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1051 * \retval PSA_ERROR_HARDWARE_FAILURE
1052 * \retval PSA_ERROR_TAMPERING_DETECTED
1053 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001054psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1055 uint8_t *hash,
1056 size_t hash_size,
1057 size_t *hash_length);
1058
Gilles Peskine308b91d2018-02-08 09:47:44 +01001059/** Finish the calculation of the hash of a message and compare it with
1060 * an expected value.
1061 *
1062 * The application must call psa_hash_start() before calling this function.
1063 * This function calculates the hash of the message formed by concatenating
1064 * the inputs passed to preceding calls to psa_hash_update(). It then
1065 * compares the calculated hash with the expected hash passed as a
1066 * parameter to this function.
1067 *
1068 * When this function returns, the operation becomes inactive.
1069 *
Gilles Peskine19067982018-03-20 17:54:53 +01001070 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001071 * comparison between the actual hash and the expected hash is performed
1072 * in constant time.
1073 *
1074 * \param operation Active hash operation.
1075 * \param hash Buffer containing the expected hash value.
1076 * \param hash_length Size of the \c hash buffer in bytes.
1077 *
1078 * \retval PSA_SUCCESS
1079 * The expected hash is identical to the actual hash of the message.
1080 * \retval PSA_ERROR_INVALID_SIGNATURE
1081 * The hash of the message was calculated successfully, but it
1082 * differs from the expected hash.
1083 * \retval PSA_ERROR_BAD_STATE
1084 * The operation state is not valid (not started, or already completed).
1085 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1086 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1087 * \retval PSA_ERROR_HARDWARE_FAILURE
1088 * \retval PSA_ERROR_TAMPERING_DETECTED
1089 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001090psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1091 const uint8_t *hash,
1092 size_t hash_length);
1093
Gilles Peskine308b91d2018-02-08 09:47:44 +01001094/** Abort a hash operation.
1095 *
1096 * This function may be called at any time after psa_hash_start().
1097 * Aborting an operation frees all associated resources except for the
1098 * \c operation structure itself.
1099 *
1100 * Implementation should strive to be robust and handle inactive hash
1101 * operations safely (do nothing and return #PSA_ERROR_BAD_STATE). However,
1102 * application writers should beware that uninitialized memory may happen
1103 * to be indistinguishable from an active hash operation, and the behavior
1104 * of psa_hash_abort() is undefined in this case.
1105 *
1106 * \param operation Active hash operation.
1107 *
1108 * \retval PSA_SUCCESS
1109 * \retval PSA_ERROR_BAD_STATE
1110 * \c operation is not an active hash operation.
1111 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1112 * \retval PSA_ERROR_HARDWARE_FAILURE
1113 * \retval PSA_ERROR_TAMPERING_DETECTED
1114 */
1115psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001116
1117/**@}*/
1118
Gilles Peskine8c9def32018-02-08 10:02:12 +01001119/** \defgroup MAC Message authentication codes
1120 * @{
1121 */
1122
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001123/** The type of the state data structure for multipart MAC operations.
1124 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001125 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001126 * make any assumptions about the content of this structure except
1127 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001128typedef struct psa_mac_operation_s psa_mac_operation_t;
1129
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001130/** The size of the output of psa_mac_finish(), in bytes.
1131 *
1132 * This is also the MAC size that psa_mac_verify() expects.
1133 *
1134 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
1135 * #PSA_ALG_IS_MAC(alg) is true).
1136 *
1137 * \return The MAC size for the specified algorithm.
1138 * If the MAC algorithm is not recognized, return 0.
1139 * An implementation may return either 0 or the correct size
1140 * for a MAC algorithm that it recognizes, but does not support.
1141 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001142#define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001143 (PSA_ALG_IS_HMAC(alg) ? PSA_HASH_SIZE(PSA_ALG_HMAC_HASH(alg)) : \
Gilles Peskine8c9def32018-02-08 10:02:12 +01001144 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
1145 0)
1146
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001147/** Start a multipart MAC operation.
1148 *
1149 * The sequence of operations to calculate a MAC (message authentication code)
1150 * is as follows:
1151 * -# Allocate an operation object which will be passed to all the functions
1152 * listed here.
1153 * -# Call psa_mac_start() to specify the algorithm and key.
1154 * The key remains associated with the operation even if the content
1155 * of the key slot changes.
1156 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1157 * of the message each time. The MAC that is calculated is the MAC
1158 * of the concatenation of these messages in order.
1159 * -# To calculate the MAC, call psa_mac_finish().
1160 * To compare the MAC with an expected value, call psa_mac_verify().
1161 *
1162 * The application may call psa_mac_abort() at any time after the operation
1163 * has been initialized with psa_mac_start().
1164 *
1165 * After a successful call to psa_mac_start(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001166 * eventually terminate the operation. The following events terminate an
1167 * operation:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001168 * - A failed call to psa_mac_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001169 * - A call to psa_mac_finish(), psa_mac_verify() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001170 *
Gilles Peskine36a74b72018-06-01 16:30:32 +02001171 * \param operation The operation object to use.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001172 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1173 * such that #PSA_ALG_IS_MAC(alg) is true).
1174 *
1175 * \retval PSA_SUCCESS
1176 * Success.
1177 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine92b30732018-03-03 21:29:30 +01001178 * \retval PSA_ERROR_NOT_PERMITTED
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001179 * \retval PSA_ERROR_INVALID_ARGUMENT
1180 * \c key is not compatible with \c alg.
1181 * \retval PSA_ERROR_NOT_SUPPORTED
1182 * \c alg is not supported or is not a MAC algorithm.
1183 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1184 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1185 * \retval PSA_ERROR_HARDWARE_FAILURE
1186 * \retval PSA_ERROR_TAMPERING_DETECTED
1187 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001188psa_status_t psa_mac_start(psa_mac_operation_t *operation,
1189 psa_key_slot_t key,
1190 psa_algorithm_t alg);
1191
1192psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1193 const uint8_t *input,
1194 size_t input_length);
1195
1196psa_status_t psa_mac_finish(psa_mac_operation_t *operation,
1197 uint8_t *mac,
1198 size_t mac_size,
1199 size_t *mac_length);
1200
1201psa_status_t psa_mac_verify(psa_mac_operation_t *operation,
1202 const uint8_t *mac,
1203 size_t mac_length);
1204
1205psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1206
1207/**@}*/
1208
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001209/** \defgroup cipher Symmetric ciphers
1210 * @{
1211 */
1212
1213/** The type of the state data structure for multipart cipher operations.
1214 *
1215 * This is an implementation-defined \c struct. Applications should not
1216 * make any assumptions about the content of this structure except
1217 * as directed by the documentation of a specific implementation. */
1218typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1219
1220/** Set the key for a multipart symmetric encryption operation.
1221 *
1222 * The sequence of operations to encrypt a message with a symmetric cipher
1223 * is as follows:
1224 * -# Allocate an operation object which will be passed to all the functions
1225 * listed here.
1226 * -# Call psa_encrypt_setup() to specify the algorithm and key.
1227 * The key remains associated with the operation even if the content
1228 * of the key slot changes.
1229 * -# Call either psa_encrypt_generate_iv() or psa_encrypt_set_iv() to
1230 * generate or set the IV (initialization vector). You should use
1231 * psa_encrypt_generate_iv() unless the protocol you are implementing
1232 * requires a specific IV value.
1233 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1234 * of the message each time.
1235 * -# Call psa_cipher_finish().
1236 *
1237 * The application may call psa_cipher_abort() at any time after the operation
1238 * has been initialized with psa_encrypt_setup().
1239 *
1240 * After a successful call to psa_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001241 * eventually terminate the operation. The following events terminate an
1242 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001243 * - A failed call to psa_encrypt_generate_iv(), psa_encrypt_set_iv()
1244 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001245 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001246 *
Gilles Peskine36a74b72018-06-01 16:30:32 +02001247 * \param operation The operation object to use.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001248 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
1249 * such that #PSA_ALG_IS_CIPHER(alg) is true).
1250 *
1251 * \retval PSA_SUCCESS
1252 * Success.
1253 * \retval PSA_ERROR_EMPTY_SLOT
1254 * \retval PSA_ERROR_NOT_PERMITTED
1255 * \retval PSA_ERROR_INVALID_ARGUMENT
1256 * \c key is not compatible with \c alg.
1257 * \retval PSA_ERROR_NOT_SUPPORTED
1258 * \c alg is not supported or is not a cipher algorithm.
1259 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1260 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1261 * \retval PSA_ERROR_HARDWARE_FAILURE
1262 * \retval PSA_ERROR_TAMPERING_DETECTED
1263 */
1264psa_status_t psa_encrypt_setup(psa_cipher_operation_t *operation,
1265 psa_key_slot_t key,
1266 psa_algorithm_t alg);
1267
1268/** Set the key for a multipart symmetric decryption operation.
1269 *
1270 * The sequence of operations to decrypt a message with a symmetric cipher
1271 * is as follows:
1272 * -# Allocate an operation object which will be passed to all the functions
1273 * listed here.
1274 * -# Call psa_decrypt_setup() to specify the algorithm and key.
1275 * The key remains associated with the operation even if the content
1276 * of the key slot changes.
1277 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1278 * decryption. If the IV is prepended to the ciphertext, you can call
1279 * psa_cipher_update() on a buffer containing the IV followed by the
1280 * beginning of the message.
1281 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1282 * of the message each time.
1283 * -# Call psa_cipher_finish().
1284 *
1285 * The application may call psa_cipher_abort() at any time after the operation
1286 * has been initialized with psa_encrypt_setup().
1287 *
1288 * After a successful call to psa_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001289 * eventually terminate the operation. The following events terminate an
1290 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001291 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001292 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001293 *
Gilles Peskine36a74b72018-06-01 16:30:32 +02001294 * \param operation The operation object to use.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001295 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
1296 * such that #PSA_ALG_IS_CIPHER(alg) is true).
1297 *
1298 * \retval PSA_SUCCESS
1299 * Success.
1300 * \retval PSA_ERROR_EMPTY_SLOT
1301 * \retval PSA_ERROR_NOT_PERMITTED
1302 * \retval PSA_ERROR_INVALID_ARGUMENT
1303 * \c key is not compatible with \c alg.
1304 * \retval PSA_ERROR_NOT_SUPPORTED
1305 * \c alg is not supported or is not a cipher algorithm.
1306 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1307 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1308 * \retval PSA_ERROR_HARDWARE_FAILURE
1309 * \retval PSA_ERROR_TAMPERING_DETECTED
1310 */
1311psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation,
1312 psa_key_slot_t key,
1313 psa_algorithm_t alg);
1314
1315psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation,
1316 unsigned char *iv,
1317 size_t iv_size,
1318 size_t *iv_length);
1319
1320psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation,
1321 const unsigned char *iv,
1322 size_t iv_length);
1323
1324psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1325 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001326 size_t input_length,
1327 unsigned char *output,
1328 size_t output_size,
1329 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001330
1331psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001332 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001333 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001334 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001335
1336psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1337
1338/**@}*/
1339
Gilles Peskine3b555712018-03-03 21:27:57 +01001340/** \defgroup aead Authenticated encryption with associated data (AEAD)
1341 * @{
1342 */
1343
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001344/** The tag size for an AEAD algorithm, in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001345 *
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001346 * \param alg An AEAD algorithm
1347 * (\c PSA_ALG_XXX value such that
1348 * #PSA_ALG_IS_AEAD(alg) is true).
1349 *
1350 * \return The tag size for the specified algorithm.
1351 * If the AEAD algorithm does not have an identified
1352 * tag that can be distinguished from the rest of
1353 * the ciphertext, return 0.
1354 * If the AEAD algorithm is not recognized, return 0.
1355 * An implementation may return either 0 or a
1356 * correct size for an AEAD algorithm that it
1357 * recognizes, but does not support.
1358 */
1359#define PSA_AEAD_TAG_SIZE(alg) \
1360 ((alg) == PSA_ALG_GCM ? 16 : \
1361 (alg) == PSA_ALG_CCM ? 16 : \
1362 0)
Gilles Peskine3b555712018-03-03 21:27:57 +01001363
Gilles Peskine212e4d82018-06-08 11:36:37 +02001364/** The maximum size of the output of psa_aead_encrypt(), in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001365 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001366 * If the size of the ciphertext buffer is at least this large, it is
1367 * guaranteed that psa_aead_encrypt() will not fail due to an
1368 * insufficient buffer size. Depending on the algorithm, the actual size of
1369 * the ciphertext may be smaller.
Gilles Peskine3b555712018-03-03 21:27:57 +01001370 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001371 * \param alg An AEAD algorithm
mohammad16031347a732018-06-07 01:38:45 +03001372 * (\c PSA_ALG_XXX value such that
1373 * #PSA_ALG_IS_AEAD(alg) is true).
Gilles Peskine212e4d82018-06-08 11:36:37 +02001374 * \param plaintext_length Size of the plaintext in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001375 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001376 * \return The AEAD ciphertext size for the specified
1377 * algorithm.
1378 * If the AEAD algorithm is not recognized, return 0.
1379 * An implementation may return either 0 or a
1380 * correct size for an AEAD algorithm that it
1381 * recognizes, but does not support.
mohammad16031347a732018-06-07 01:38:45 +03001382 */
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001383#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) \
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001384 (PSA_AEAD_TAG_SIZE(alg) != 0 ? \
1385 (plaintext_length) + PSA_AEAD_TAG_SIZE(alg) : \
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001386 0)
1387
1388/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001389 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001390 * \param key Slot containing the key to use.
1391 * \param alg The AEAD algorithm to compute
1392 * (\c PSA_ALG_XXX value such that
1393 * #PSA_ALG_IS_AEAD(alg) is true).
1394 * \param nonce Nonce or IV to use.
1395 * \param nonce_length Size of the \p nonce buffer in bytes.
1396 * \param additional_data Additional data that will be authenticated
1397 * but not encrypted.
1398 * \param additional_data_length Size of \p additional_data in bytes.
1399 * \param plaintext Data that will be authenticated and
1400 * encrypted.
1401 * \param plaintext_length Size of \p plaintext in bytes.
1402 * \param ciphertext Output buffer for the authenticated and
1403 * encrypted data. The additional data is not
1404 * part of this output. For algorithms where the
1405 * encrypted data and the authentication tag
1406 * are defined as separate outputs, the
1407 * authentication tag is appended to the
1408 * encrypted data.
1409 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1410 * This must be at least
1411 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1412 * \p plaintext_length).
1413 * \param ciphertext_length On success, the size of the output
1414 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001415 *
1416 * \retval PSA_SUCCESS
1417 * Success.
1418 * \retval PSA_ERROR_EMPTY_SLOT
1419 * \retval PSA_ERROR_NOT_PERMITTED
1420 * \retval PSA_ERROR_INVALID_ARGUMENT
1421 * \c key is not compatible with \c alg.
1422 * \retval PSA_ERROR_NOT_SUPPORTED
1423 * \c alg is not supported or is not an AEAD algorithm.
1424 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1425 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1426 * \retval PSA_ERROR_HARDWARE_FAILURE
1427 * \retval PSA_ERROR_TAMPERING_DETECTED
1428 */
mohammad160339ee8712018-04-26 00:51:02 +03001429psa_status_t psa_aead_encrypt( psa_key_slot_t key,
1430 psa_algorithm_t alg,
1431 const uint8_t *nonce,
1432 size_t nonce_length,
1433 const uint8_t *additional_data,
1434 size_t additional_data_length,
1435 const uint8_t *plaintext,
1436 size_t plaintext_length,
1437 uint8_t *ciphertext,
1438 size_t ciphertext_size,
1439 size_t *ciphertext_length );
Gilles Peskine3b555712018-03-03 21:27:57 +01001440
Gilles Peskine212e4d82018-06-08 11:36:37 +02001441/** The maximum size of the output of psa_aead_decrypt(), in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001442 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001443 * If the size of the plaintext buffer is at least this large, it is
1444 * guaranteed that psa_aead_decrypt() will not fail due to an
1445 * insufficient buffer size. Depending on the algorithm, the actual size of
1446 * the plaintext may be smaller.
Gilles Peskine3b555712018-03-03 21:27:57 +01001447 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001448 * \param alg An AEAD algorithm
mohammad16031347a732018-06-07 01:38:45 +03001449 * (\c PSA_ALG_XXX value such that
1450 * #PSA_ALG_IS_AEAD(alg) is true).
Gilles Peskine212e4d82018-06-08 11:36:37 +02001451 * \param ciphertext_length Size of the plaintext in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001452 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001453 * \return The AEAD ciphertext size for the specified
1454 * algorithm.
1455 * If the AEAD algorithm is not recognized, return 0.
1456 * An implementation may return either 0 or a
1457 * correct size for an AEAD algorithm that it
1458 * recognizes, but does not support.
mohammad16031347a732018-06-07 01:38:45 +03001459 */
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001460#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \
1461 (PSA_AEAD_TAG_SIZE(alg) != 0 ? \
1462 (plaintext_length) - PSA_AEAD_TAG_SIZE(alg) : \
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001463 0)
1464
1465/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001466 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001467 * \param key Slot containing the key to use.
1468 * \param alg The AEAD algorithm to compute
1469 * (\c PSA_ALG_XXX value such that
1470 * #PSA_ALG_IS_AEAD(alg) is true).
1471 * \param nonce Nonce or IV to use.
1472 * \param nonce_length Size of the \p nonce buffer in bytes.
1473 * \param additional_data Additional data that has been authenticated
1474 * but not encrypted.
1475 * \param additional_data_length Size of \p additional_data in bytes.
1476 * \param ciphertext Data that has been authenticated and
1477 * encrypted. For algorithms where the
1478 * encrypted data and the authentication tag
1479 * are defined as separate inputs, the buffer
1480 * must contain the encrypted data followed
1481 * by the authentication tag.
1482 * \param ciphertext_length Size of \p ciphertext in bytes.
1483 * \param plaintext Output buffer for the decrypted data.
1484 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1485 * This must be at least
1486 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1487 * \p ciphertext_length).
1488 * \param plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001489 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001490 *
1491 * \retval PSA_SUCCESS
1492 * Success.
1493 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001494 * \retval PSA_ERROR_INVALID_SIGNATURE
1495 * The ciphertext is not authentic.
Gilles Peskine3b555712018-03-03 21:27:57 +01001496 * \retval PSA_ERROR_NOT_PERMITTED
1497 * \retval PSA_ERROR_INVALID_ARGUMENT
1498 * \c key is not compatible with \c alg.
1499 * \retval PSA_ERROR_NOT_SUPPORTED
Gilles Peskine19067982018-03-20 17:54:53 +01001500 * \c alg is not supported or is not an AEAD algorithm.
Gilles Peskine3b555712018-03-03 21:27:57 +01001501 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1502 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1503 * \retval PSA_ERROR_HARDWARE_FAILURE
1504 * \retval PSA_ERROR_TAMPERING_DETECTED
1505 */
mohammad160339ee8712018-04-26 00:51:02 +03001506psa_status_t psa_aead_decrypt( psa_key_slot_t key,
1507 psa_algorithm_t alg,
1508 const uint8_t *nonce,
1509 size_t nonce_length,
1510 const uint8_t *additional_data,
1511 size_t additional_data_length,
1512 const uint8_t *ciphertext,
1513 size_t ciphertext_length,
1514 uint8_t *plaintext,
1515 size_t plaintext_size,
1516 size_t *plaintext_length );
Gilles Peskine3b555712018-03-03 21:27:57 +01001517
1518/**@}*/
1519
Gilles Peskine20035e32018-02-03 22:44:14 +01001520/** \defgroup asymmetric Asymmetric cryptography
1521 * @{
1522 */
1523
1524/**
Gilles Peskine0189e752018-02-03 23:57:22 +01001525 * \brief Maximum ECDSA signature size for a given curve bit size
1526 *
1527 * \param curve_bits Curve size in bits
1528 * \return Maximum signature size in bytes
1529 *
1530 * \note This macro returns a compile-time constant if its argument is one.
1531 *
1532 * \warning This macro may evaluate its argument multiple times.
1533 */
1534/*
1535 * RFC 4492 page 20:
1536 *
1537 * Ecdsa-Sig-Value ::= SEQUENCE {
1538 * r INTEGER,
1539 * s INTEGER
1540 * }
1541 *
1542 * Size is at most
1543 * 1 (tag) + 1 (len) + 1 (initial 0) + curve_bytes for each of r and s,
1544 * twice that + 1 (tag) + 2 (len) for the sequence
1545 * (assuming curve_bytes is less than 126 for r and s,
1546 * and less than 124 (total len <= 255) for the sequence)
1547 */
1548#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
1549 ( /*T,L of SEQUENCE*/ ((curve_bits) >= 61 * 8 ? 3 : 2) + \
1550 /*T,L of r,s*/ 2 * (((curve_bits) >= 127 * 8 ? 3 : 2) + \
1551 /*V of r,s*/ ((curve_bits) + 8) / 8))
1552
1553
Gilles Peskine308b91d2018-02-08 09:47:44 +01001554/** Safe signature buffer size for psa_asymmetric_sign().
1555 *
1556 * This macro returns a safe buffer size for a signature using a key
1557 * of the specified type and size, with the specified algorithm.
1558 * Note that the actual size of the signature may be smaller
1559 * (some algorithms produce a variable-size signature).
1560 *
1561 * \warning This function may call its arguments multiple times or
1562 * zero times, so you should not pass arguments that contain
1563 * side effects.
1564 *
1565 * \param key_type An asymmetric key type (this may indifferently be a
1566 * key pair type or a public key type).
1567 * \param key_bits The size of the key in bits.
1568 * \param alg The signature algorithm.
1569 *
1570 * \return If the parameters are valid and supported, return
1571 * a buffer size in bytes that guarantees that
1572 * psa_asymmetric_sign() will not fail with
1573 * #PSA_ERROR_BUFFER_TOO_SMALL.
1574 * If the parameters are a valid combination that is not supported
1575 * by the implementation, this macro either shall return either a
1576 * sensible size or 0.
1577 * If the parameters are not valid, the
1578 * return value is unspecified.
1579 *
1580 */
Gilles Peskine0189e752018-02-03 23:57:22 +01001581#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine2905a7a2018-03-07 16:39:31 +01001582 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine0189e752018-02-03 23:57:22 +01001583 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
Gilles Peskine84845652018-03-28 14:17:40 +02001584 ((void)alg, 0))
Gilles Peskine0189e752018-02-03 23:57:22 +01001585
1586/**
Gilles Peskine20035e32018-02-03 22:44:14 +01001587 * \brief Sign a hash or short message with a private key.
1588 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001589 * \param key Key slot containing an asymmetric key pair.
1590 * \param alg A signature algorithm that is compatible with
1591 * the type of \c key.
1592 * \param hash The message to sign.
1593 * \param hash_length Size of the \c hash buffer in bytes.
1594 * \param salt A salt or label, if supported by the signature
1595 * algorithm.
1596 * If the signature algorithm does not support a
1597 * salt, pass \c NULL.
1598 * If the signature algorithm supports an optional
1599 * salt and you do not want to pass a salt,
1600 * pass \c NULL.
1601 * \param salt_length Size of the \c salt buffer in bytes.
1602 * If \c salt is \c NULL, pass 0.
1603 * \param signature Buffer where the signature is to be written.
1604 * \param signature_size Size of the \c signature buffer in bytes.
1605 * \param signature_length On success, the number of bytes
1606 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001607 *
1608 * \retval PSA_SUCCESS
1609 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1610 * The size of the \c signature buffer is too small. You can
1611 * determine a sufficient buffer size by calling
1612 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg)
1613 * where \c key_type and \c key_bits are the type and bit-size
1614 * respectively of \c key.
1615 * \retval PSA_ERROR_NOT_SUPPORTED
1616 * \retval PSA_ERROR_INVALID_ARGUMENT
1617 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1618 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1619 * \retval PSA_ERROR_HARDWARE_FAILURE
1620 * \retval PSA_ERROR_TAMPERING_DETECTED
1621 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01001622 */
1623psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
1624 psa_algorithm_t alg,
1625 const uint8_t *hash,
1626 size_t hash_length,
1627 const uint8_t *salt,
1628 size_t salt_length,
1629 uint8_t *signature,
1630 size_t signature_size,
1631 size_t *signature_length);
1632
1633/**
1634 * \brief Verify the signature a hash or short message using a public key.
1635 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001636 * \param key Key slot containing a public key or an
1637 * asymmetric key pair.
1638 * \param alg A signature algorithm that is compatible with
1639 * the type of \c key.
1640 * \param hash The message whose signature is to be verified.
1641 * \param hash_length Size of the \c hash buffer in bytes.
1642 * \param salt A salt or label, if supported by the signature
1643 * algorithm.
1644 * If the signature algorithm does not support a
1645 * salt, pass \c NULL.
1646 * If the signature algorithm supports an optional
1647 * salt and you do not want to pass a salt,
1648 * pass \c NULL.
1649 * \param salt_length Size of the \c salt buffer in bytes.
1650 * If \c salt is \c NULL, pass 0.
1651 * \param signature Buffer containing the signature to verify.
1652 * \param signature_size Size of the \c signature buffer in bytes.
1653 *
1654 * \retval PSA_SUCCESS
1655 * The signature is valid.
1656 * \retval PSA_ERROR_INVALID_SIGNATURE
1657 * The calculation was perfomed successfully, but the passed
1658 * signature is not a valid signature.
1659 * \retval PSA_ERROR_NOT_SUPPORTED
1660 * \retval PSA_ERROR_INVALID_ARGUMENT
1661 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1662 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1663 * \retval PSA_ERROR_HARDWARE_FAILURE
1664 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01001665 */
1666psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
1667 psa_algorithm_t alg,
1668 const uint8_t *hash,
1669 size_t hash_length,
1670 const uint8_t *salt,
1671 size_t salt_length,
1672 uint8_t *signature,
1673 size_t signature_size);
1674
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001675#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine06297932018-04-11 16:58:22 +02001676 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
1677 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
1678 0)
Gilles Peskine723feff2018-05-31 20:08:13 +02001679#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
1680 (PSA_ALG_IS_RSA_OAEP_MGF1(alg) ? \
1681 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_GET_HASH(alg)) + 1 : \
1682 11 /*PKCS#1v1.5*/)
1683#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine06297932018-04-11 16:58:22 +02001684 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
Gilles Peskine723feff2018-05-31 20:08:13 +02001685 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
Gilles Peskine06297932018-04-11 16:58:22 +02001686 0)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001687
1688/**
1689 * \brief Encrypt a short message with a public key.
1690 *
1691 * \param key Key slot containing a public key or an asymmetric
1692 * key pair.
1693 * \param alg An asymmetric encryption algorithm that is
1694 * compatible with the type of \c key.
1695 * \param input The message to encrypt.
1696 * \param input_length Size of the \c input buffer in bytes.
1697 * \param salt A salt or label, if supported by the encryption
1698 * algorithm.
1699 * If the algorithm does not support a
1700 * salt, pass \c NULL.
1701 * If the algorithm supports an optional
1702 * salt and you do not want to pass a salt,
1703 * pass \c NULL.
1704 *
1705 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1706 * supported.
1707 * \param salt_length Size of the \c salt buffer in bytes.
1708 * If \c salt is \c NULL, pass 0.
1709 * \param output Buffer where the encrypted message is to be written.
1710 * \param output_size Size of the \c output buffer in bytes.
1711 * \param output_length On success, the number of bytes
1712 * that make up the returned output.
1713 *
1714 * \retval PSA_SUCCESS
1715 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1716 * The size of the \c output buffer is too small. You can
1717 * determine a sufficient buffer size by calling
1718 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1719 * where \c key_type and \c key_bits are the type and bit-size
1720 * respectively of \c key.
1721 * \retval PSA_ERROR_NOT_SUPPORTED
1722 * \retval PSA_ERROR_INVALID_ARGUMENT
1723 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1724 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1725 * \retval PSA_ERROR_HARDWARE_FAILURE
1726 * \retval PSA_ERROR_TAMPERING_DETECTED
1727 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1728 */
1729psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
1730 psa_algorithm_t alg,
1731 const uint8_t *input,
1732 size_t input_length,
1733 const uint8_t *salt,
1734 size_t salt_length,
1735 uint8_t *output,
1736 size_t output_size,
1737 size_t *output_length);
1738
1739/**
1740 * \brief Decrypt a short message with a private key.
1741 *
1742 * \param key Key slot containing an asymmetric key pair.
1743 * \param alg An asymmetric encryption algorithm that is
1744 * compatible with the type of \c key.
1745 * \param input The message to decrypt.
1746 * \param input_length Size of the \c input buffer in bytes.
1747 * \param salt A salt or label, if supported by the encryption
1748 * algorithm.
1749 * If the algorithm does not support a
1750 * salt, pass \c NULL.
1751 * If the algorithm supports an optional
1752 * salt and you do not want to pass a salt,
1753 * pass \c NULL.
1754 *
1755 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1756 * supported.
1757 * \param salt_length Size of the \c salt buffer in bytes.
1758 * If \c salt is \c NULL, pass 0.
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001759 * \param output Buffer where the decrypted message is to be written.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001760 * \param output_size Size of the \c output buffer in bytes.
1761 * \param output_length On success, the number of bytes
1762 * that make up the returned output.
1763 *
1764 * \retval PSA_SUCCESS
1765 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1766 * The size of the \c output buffer is too small. You can
1767 * determine a sufficient buffer size by calling
1768 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1769 * where \c key_type and \c key_bits are the type and bit-size
1770 * respectively of \c key.
1771 * \retval PSA_ERROR_NOT_SUPPORTED
1772 * \retval PSA_ERROR_INVALID_ARGUMENT
1773 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1774 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1775 * \retval PSA_ERROR_HARDWARE_FAILURE
1776 * \retval PSA_ERROR_TAMPERING_DETECTED
1777 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1778 * \retval PSA_ERROR_INVALID_PADDING
1779 */
1780psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
1781 psa_algorithm_t alg,
1782 const uint8_t *input,
1783 size_t input_length,
1784 const uint8_t *salt,
1785 size_t salt_length,
1786 uint8_t *output,
1787 size_t output_size,
1788 size_t *output_length);
1789
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001790/**@}*/
1791
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001792/** \defgroup generation Key generation
1793 * @{
1794 */
1795
1796/**
1797 * \brief Generate random bytes.
1798 *
1799 * \warning This function **can** fail! Callers MUST check the return status
1800 * and MUST NOT use the content of the output buffer if the return
1801 * status is not #PSA_SUCCESS.
1802 *
1803 * \note To generate a key, use psa_generate_key() instead.
1804 *
1805 * \param output Output buffer for the generated data.
1806 * \param output_size Number of bytes to generate and output.
1807 *
1808 * \retval PSA_SUCCESS
1809 * \retval PSA_ERROR_NOT_SUPPORTED
1810 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1811 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1812 * \retval PSA_ERROR_HARDWARE_FAILURE
1813 * \retval PSA_ERROR_TAMPERING_DETECTED
1814 */
1815psa_status_t psa_generate_random(uint8_t *output,
1816 size_t output_size);
1817
1818/**
1819 * \brief Generate a key or key pair.
1820 *
1821 * \param key Slot where the key will be stored. This must be a
1822 * valid slot for a key of the chosen type. It must
1823 * be unoccupied.
1824 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
1825 * \param bits Key size in bits.
1826 * \param parameters Extra parameters for key generation. The interpretation
1827 * of this parameter depends on \c type. All types support
1828 * \c NULL to use default parameters specified below.
1829 *
1830 * For any symmetric key type (type such that
1831 * `PSA_KEY_TYPE_IS_ASYMMETRIC(type)` is false), \c parameters must be
1832 * \c NULL. For asymmetric key types defined by this specification,
1833 * the parameter type and the default parameters are defined by the
1834 * table below. For vendor-defined key types, the vendor documentation
1835 * shall define the parameter type and the default parameters.
1836 *
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001837 * Type | Parameter type | Meaning | Parameters used if `parameters == NULL`
1838 * ---- | -------------- | ------- | ---------------------------------------
1839 * `PSA_KEY_TYPE_RSA_KEYPAIR` | `unsigned int` | Public exponent | 65537
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001840 *
1841 * \retval PSA_SUCCESS
1842 * \retval PSA_ERROR_NOT_SUPPORTED
1843 * \retval PSA_ERROR_INVALID_ARGUMENT
1844 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1845 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1846 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1847 * \retval PSA_ERROR_HARDWARE_FAILURE
1848 * \retval PSA_ERROR_TAMPERING_DETECTED
1849 */
1850psa_status_t psa_generate_key(psa_key_slot_t key,
1851 psa_key_type_t type,
1852 size_t bits,
1853 const void *parameters);
1854
1855/**@}*/
1856
Gilles Peskinee59236f2018-01-27 23:32:46 +01001857#ifdef __cplusplus
1858}
1859#endif
1860
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001861/* The file "crypto_struct.h" contains definitions for
1862 * implementation-specific structs that are declared above. */
1863#include "crypto_struct.h"
1864
1865/* The file "crypto_extra.h" contains vendor-specific definitions. This
1866 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01001867#include "crypto_extra.h"
1868
1869#endif /* PSA_CRYPTO_H */