blob: 20e2942c9d8b4c81e74b42eb2ad1771eea7bfa76 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
5
6#ifndef PSA_CRYPTO_H
7#define PSA_CRYPTO_H
8
9#include "crypto_platform.h"
10
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010011#include <stddef.h>
12
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010013#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010014/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
15 * must be defined in the crypto_platform.h header. These mock definitions
16 * are present in this file as a convenience to generate pretty-printed
17 * documentation that includes those definitions. */
18
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010019/** \defgroup platform Implementation-specific definitions
20 * @{
21 */
22
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010023/** \brief Key slot number.
24 *
25 * This type represents key slots. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010026 * type. The choice of type is implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027 * 0 is not a valid key slot number. The meaning of other values is
28 * implementation dependent.
29 *
30 * At any given point in time, each key slot either contains a
31 * cryptographic object, or is empty. Key slots are persistent:
32 * once set, the cryptographic object remains in the key slot until
33 * explicitly destroyed.
34 */
35typedef _unsigned_integral_type_ psa_key_slot_t;
36
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010037/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010038#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010039
Gilles Peskinee59236f2018-01-27 23:32:46 +010040#ifdef __cplusplus
41extern "C" {
42#endif
43
44/** \defgroup basic Basic definitions
45 * @{
46 */
47
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020048#if defined(PSA_SUCCESS)
49/* If PSA_SUCCESS is defined, assume that PSA crypto is being used
50 * together with PSA IPC, which also defines the identifier
51 * PSA_SUCCESS. We must not define PSA_SUCCESS ourselves in that case;
52 * the other error code names don't clash. Also define psa_status_t as
53 * an alias for the type used by PSA IPC. This is a temporary hack
54 * until we unify error reporting in PSA IPC and PSA crypo.
55 *
56 * Note that psa_defs.h must be included before this header!
57 */
58typedef psa_error_t psa_status_t;
59
60#else /* defined(PSA_SUCCESS) */
61
Gilles Peskinee59236f2018-01-27 23:32:46 +010062/**
63 * \brief Function return status.
64 *
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020065 * This is either #PSA_SUCCESS (which is zero), indicating success,
66 * or a nonzero value indicating that an error occurred. Errors are
67 * encoded as one of the \c PSA_ERROR_xxx values defined here.
Gilles Peskinee59236f2018-01-27 23:32:46 +010068 */
itayzafrirc2a79762018-06-18 16:20:16 +030069typedef int32_t psa_status_t;
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020070
itayzafrirc2a79762018-06-18 16:20:16 +030071/** The action was completed successfully. */
72#define PSA_SUCCESS ((psa_status_t)0)
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020073
74#endif /* !defined(PSA_SUCCESS) */
itayzafrirc2a79762018-06-18 16:20:16 +030075
76/** The requested operation or a parameter is not supported
77 * by this implementation.
78 *
79 * Implementations should return this error code when an enumeration
80 * parameter such as a key type, algorithm, etc. is not recognized.
81 * If a combination of parameters is recognized and identified as
82 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
83#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)1)
84
85/** The requested action is denied by a policy.
86 *
87 * Implementations should return this error code when the parameters
88 * are recognized as valid and supported, and a policy explicitly
89 * denies the requested operation.
90 *
91 * If a subset of the parameters of a function call identify a
92 * forbidden operation, and another subset of the parameters are
93 * not valid or not supported, it is unspecified whether the function
94 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
95 * #PSA_ERROR_INVALID_ARGUMENT. */
96#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)2)
97
98/** An output buffer is too small.
99 *
100 * Applications can call the `PSA_xxx_SIZE` macro listed in the function
101 * description to determine a sufficient buffer size.
102 *
103 * Implementations should preferably return this error code only
104 * in cases when performing the operation with a larger output
105 * buffer would succeed. However implementations may return this
106 * error if a function has invalid or unsupported parameters in addition
107 * to the parameters that determine the necessary output buffer size. */
108#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)3)
109
110/** A slot is occupied, but must be empty to carry out the
111 * requested action.
112 *
113 * If the slot number is invalid (i.e. the requested action could
114 * not be performed even after erasing the slot's content),
115 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
116#define PSA_ERROR_OCCUPIED_SLOT ((psa_status_t)4)
117
118/** A slot is empty, but must be occupied to carry out the
119 * requested action.
120 *
121 * If the slot number is invalid (i.e. the requested action could
122 * not be performed even after creating appropriate content in the slot),
123 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
124#define PSA_ERROR_EMPTY_SLOT ((psa_status_t)5)
125
126/** The requested action cannot be performed in the current state.
127 *
128 * Multipart operations return this error when one of the
129 * functions is called out of sequence. Refer to the function
130 * descriptions for permitted sequencing of functions.
131 *
132 * Implementations shall not return this error code to indicate
133 * that a key slot is occupied when it needs to be free or vice versa,
134 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
135 * as applicable. */
136#define PSA_ERROR_BAD_STATE ((psa_status_t)6)
137
138/** The parameters passed to the function are invalid.
139 *
140 * Implementations may return this error any time a parameter or
141 * combination of parameters are recognized as invalid.
142 *
143 * Implementations shall not return this error code to indicate
144 * that a key slot is occupied when it needs to be free or vice versa,
145 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
146 * as applicable. */
147#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)7)
148
149/** There is not enough runtime memory.
150 *
151 * If the action is carried out across multiple security realms, this
152 * error can refer to available memory in any of the security realms. */
153#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)8)
154
155/** There is not enough persistent storage.
156 *
157 * Functions that modify the key storage return this error code if
158 * there is insufficient storage space on the host media. In addition,
159 * many functions that do not otherwise access storage may return this
160 * error code if the implementation requires a mandatory log entry for
161 * the requested action and the log storage space is full. */
162#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)9)
163
164/** There was a communication failure inside the implementation.
165 *
166 * This can indicate a communication failure between the application
167 * and an external cryptoprocessor or between the cryptoprocessor and
168 * an external volatile or persistent memory. A communication failure
169 * may be transient or permanent depending on the cause.
170 *
171 * \warning If a function returns this error, it is undetermined
172 * whether the requested action has completed or not. Implementations
173 * should return #PSA_SUCCESS on successful completion whenver
174 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
175 * if the requested action was completed successfully in an external
176 * cryptoprocessor but there was a breakdown of communication before
177 * the cryptoprocessor could report the status to the application.
178 */
179#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)10)
180
181/** There was a storage failure that may have led to data loss.
182 *
183 * This error indicates that some persistent storage is corrupted.
184 * It should not be used for a corruption of volatile memory
185 * (use #PSA_ERROR_TAMPERING_DETECTED), for a communication error
186 * between the cryptoprocessor and its external storage (use
187 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
188 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
189 *
190 * Note that a storage failure does not indicate that any data that was
191 * previously read is invalid. However this previously read data may no
192 * longer be readable from storage.
193 *
194 * When a storage failure occurs, it is no longer possible to ensure
195 * the global integrity of the keystore. Depending on the global
196 * integrity guarantees offered by the implementation, access to other
197 * data may or may not fail even if the data is still readable but
198 * its integrity canont be guaranteed.
199 *
200 * Implementations should only use this error code to report a
201 * permanent storage corruption. However application writers should
202 * keep in mind that transient errors while reading the storage may be
203 * reported using this error code. */
204#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)11)
205
206/** A hardware failure was detected.
207 *
208 * A hardware failure may be transient or permanent depending on the
209 * cause. */
210#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)12)
211
212/** A tampering attempt was detected.
213 *
214 * If an application receives this error code, there is no guarantee
215 * that previously accessed or computed data was correct and remains
216 * confidential. Applications should not perform any security function
217 * and should enter a safe failure state.
218 *
219 * Implementations may return this error code if they detect an invalid
220 * state that cannot happen during normal operation and that indicates
221 * that the implementation's security guarantees no longer hold. Depending
222 * on the implementation architecture and on its security and safety goals,
223 * the implementation may forcibly terminate the application.
224 *
225 * This error code is intended as a last resort when a security breach
226 * is detected and it is unsure whether the keystore data is still
227 * protected. Implementations shall only return this error code
228 * to report an alarm from a tampering detector, to indicate that
229 * the confidentiality of stored data can no longer be guaranteed,
230 * or to indicate that the integrity of previously returned data is now
231 * considered compromised. Implementations shall not use this error code
232 * to indicate a hardware failure that merely makes it impossible to
233 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
234 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
235 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
236 * instead).
237 *
238 * This error indicates an attack against the application. Implementations
239 * shall not return this error code as a consequence of the behavior of
240 * the application itself. */
241#define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)13)
242
243/** There is not enough entropy to generate random data needed
244 * for the requested action.
245 *
246 * This error indicates a failure of a hardware random generator.
247 * Application writers should note that this error can be returned not
248 * only by functions whose purpose is to generate random data, such
249 * as key, IV or nonce generation, but also by functions that execute
250 * an algorithm with a randomized result, as well as functions that
251 * use randomization of intermediate computations as a countermeasure
252 * to certain attacks.
253 *
254 * Implementations should avoid returning this error after psa_crypto_init()
255 * has succeeded. Implementations should generate sufficient
256 * entropy during initialization and subsequently use a cryptographically
257 * secure pseudorandom generator (PRNG). However implementations may return
258 * this error at any time if a policy requires the PRNG to be reseeded
259 * during normal operation. */
260#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)14)
261
262/** The signature, MAC or hash is incorrect.
263 *
264 * Verification functions return this error if the verification
265 * calculations completed successfully, and the value to be verified
266 * was determined to be incorrect.
267 *
268 * If the value to verify has an invalid size, implementations may return
269 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
270#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)15)
271
272/** The decrypted padding is incorrect.
273 *
274 * \warning In some protocols, when decrypting data, it is essential that
275 * the behavior of the application does not depend on whether the padding
276 * is correct, down to precise timing. Applications should prefer
277 * protocols that use authenticated encryption rather than plain
278 * encryption. If the application must perform a decryption of
279 * unauthenticated data, the application writer should take care not
280 * to reveal whether the padding is invalid.
281 *
282 * Implementations should strive to make valid and invalid padding
283 * as close as possible to indistinguishable to an external observer.
284 * In particular, the timing of a decryption operation should not
285 * depend on the validity of the padding. */
286#define PSA_ERROR_INVALID_PADDING ((psa_status_t)16)
287
288/** An error occurred that does not correspond to any defined
289 * failure cause.
290 *
291 * Implementations may use this error code if none of the other standard
292 * error codes are applicable. */
293#define PSA_ERROR_UNKNOWN_ERROR ((psa_status_t)17)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100294
295/**
296 * \brief Library initialization.
297 *
298 * Applications must call this function before calling any other
299 * function in this module.
300 *
301 * Applications may call this function more than once. Once a call
302 * succeeds, subsequent calls are guaranteed to succeed.
303 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100304 * \retval PSA_SUCCESS
305 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
306 * \retval PSA_ERROR_COMMUNICATION_FAILURE
307 * \retval PSA_ERROR_HARDWARE_FAILURE
308 * \retval PSA_ERROR_TAMPERING_DETECTED
309 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +0100310 */
311psa_status_t psa_crypto_init(void);
312
Gilles Peskine2905a7a2018-03-07 16:39:31 +0100313#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
314#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
Gilles Peskine0189e752018-02-03 23:57:22 +0100315
Gilles Peskinee59236f2018-01-27 23:32:46 +0100316/**@}*/
317
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100318/** \defgroup crypto_types Key and algorithm types
319 * @{
320 */
321
Gilles Peskine308b91d2018-02-08 09:47:44 +0100322/** \brief Encoding of a key type.
323 */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100324typedef uint32_t psa_key_type_t;
325
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100326/** An invalid key type value.
327 *
328 * Zero is not the encoding of any key type.
329 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100330#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100331
332/** Vendor-defined flag
333 *
334 * Key types defined by this standard will never have the
335 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
336 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
337 * respect the bitwise structure used by standard encodings whenever practical.
338 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100339#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100340
Gilles Peskine98f0a242018-02-06 18:57:29 +0100341#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7e000000)
Gilles Peskine35855962018-04-19 08:39:16 +0200342/** Raw data.
343 *
344 * A "key" of this type cannot be used for any cryptographic operation.
345 * Applications may use this type to store arbitrary data in the keystore. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100346#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x02000000)
347#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x04000000)
348#define PSA_KEY_TYPE_CATEGORY_ASYMMETRIC ((psa_key_type_t)0x06000000)
349#define PSA_KEY_TYPE_PAIR_FLAG ((psa_key_type_t)0x01000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100350
Gilles Peskine35855962018-04-19 08:39:16 +0200351/** HMAC key.
352 *
353 * The key policy determines which underlying hash algorithm the key can be
354 * used for.
355 *
356 * HMAC keys should generally have the same size as the underlying hash.
357 * This size can be calculated with `PSA_HASH_SIZE(alg)` where
358 * `alg` is the HMAC algorithm or the underlying hash algorithm. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100359#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x02000001)
Gilles Peskine35855962018-04-19 08:39:16 +0200360/** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher.
361 *
362 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
363 * 32 bytes (AES-256).
364 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100365#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x04000001)
Gilles Peskine35855962018-04-19 08:39:16 +0200366/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
367 *
368 * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or
369 * 24 bytes (3-key 3DES).
370 *
371 * Note that single DES and 2-key 3DES are weak and strongly
372 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
373 * is weak and deprecated and should only be used in legacy protocols.
374 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100375#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x04000002)
Gilles Peskine35855962018-04-19 08:39:16 +0200376/** Key for an cipher, AEAD or MAC algorithm based on the
377 * Camellia block cipher. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100378#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x04000003)
Gilles Peskine35855962018-04-19 08:39:16 +0200379/** Key for the RC4 stream cipher.
380 *
381 * Note that RC4 is weak and deprecated and should only be used in
382 * legacy protocols. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100383#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x04000004)
384
Gilles Peskine308b91d2018-02-08 09:47:44 +0100385/** RSA public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100386#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x06010000)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100387/** RSA key pair (private and public key). */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100388#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x07010000)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100389/** DSA public key. */
390#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x06020000)
391/** DSA key pair (private and public key). */
392#define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x07020000)
393#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x06030000)
394#define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x07030000)
itayzafrir5c753392018-05-08 11:18:38 +0300395#define PSA_KEY_TYPE_ECC_CURVE_NISTP256R1 ((psa_key_type_t)0x00000001)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100396#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100397#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
398 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
399#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
400 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
Gilles Peskine98f0a242018-02-06 18:57:29 +0100401
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100402/** Whether a key type is vendor-defined. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100403#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100404 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100405#define PSA_KEY_TYPE_IS_RAW_BYTES(type) \
406 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_RAW_DATA || \
407 ((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100408
409/** Whether a key type is asymmetric: either a key pair or a public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100410#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
411 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100412/** Whether a key type is the public part of a key pair. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100413#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
Moran Pekerb4d0ddd2018-04-04 12:47:52 +0300414 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
415 PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100416/** Whether a key type is a key pair containing a private part and a public
417 * part. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100418#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
419 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
420 (PSA_KEY_TYPE_CATEGORY_ASYMMETRIC | PSA_KEY_TYPE_PAIR_FLAG))
Gilles Peskine06dc2632018-03-08 07:47:25 +0100421/** Whether a key type is an RSA key pair or public key. */
422/** The key pair type corresponding to a public key type. */
423#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
424 ((type) | PSA_KEY_TYPE_PAIR_FLAG)
425/** The public key type corresponding to a key pair type. */
426#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
427 ((type) & ~PSA_KEY_TYPE_PAIR_FLAG)
Gilles Peskine0189e752018-02-03 23:57:22 +0100428#define PSA_KEY_TYPE_IS_RSA(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100429 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
430/** Whether a key type is an elliptic curve key pair or public key. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100431#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100432 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
433 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100434
Gilles Peskine7e198532018-03-08 07:50:30 +0100435/** The block size of a block cipher.
436 *
437 * \param type A cipher key type (value of type #psa_key_type_t).
438 *
439 * \return The block size for a block cipher, or 1 for a stream cipher.
Gilles Peskine35855962018-04-19 08:39:16 +0200440 * The return value is undefined if \c type is not a supported
441 * cipher key type.
442 *
443 * \note It is possible to build stream cipher algorithms on top of a block
444 * cipher, for example CTR mode (#PSA_ALG_CTR).
445 * This macro only takes the key type into account, so it cannot be
446 * used to determine the size of the data that #psa_cipher_update()
447 * might buffer for future processing in general.
Gilles Peskine7e198532018-03-08 07:50:30 +0100448 *
449 * \note This macro returns a compile-time constant if its argument is one.
450 *
451 * \warning This macro may evaluate its argument multiple times.
452 */
Gilles Peskine03182e92018-03-07 16:40:52 +0100453#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100454 ( \
455 (type) == PSA_KEY_TYPE_AES ? 16 : \
456 (type) == PSA_KEY_TYPE_DES ? 8 : \
457 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
Gilles Peskine7e198532018-03-08 07:50:30 +0100458 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100459 0)
460
Gilles Peskine308b91d2018-02-08 09:47:44 +0100461/** \brief Encoding of a cryptographic algorithm.
462 *
463 * For algorithms that can be applied to multiple key types, this type
464 * does not encode the key type. For example, for symmetric ciphers
465 * based on a block cipher, #psa_algorithm_t encodes the block cipher
466 * mode and the padding mode while the block cipher itself is encoded
467 * via #psa_key_type_t.
468 */
Gilles Peskine20035e32018-02-03 22:44:14 +0100469typedef uint32_t psa_algorithm_t;
470
Gilles Peskine98f0a242018-02-06 18:57:29 +0100471#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
472#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
473#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
474#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
475#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
476#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
477#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
478#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
479#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
480#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100481
Gilles Peskine98f0a242018-02-06 18:57:29 +0100482#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
483 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100484/** Whether the specified algorithm is a hash algorithm.
485 *
Gilles Peskine7e198532018-03-08 07:50:30 +0100486 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100487 *
488 * \return 1 if \c alg is a hash algorithm, 0 otherwise.
489 * This macro may return either 0 or 1 if \c alg is not a valid
Gilles Peskine7e198532018-03-08 07:50:30 +0100490 * algorithm identifier.
491 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100492#define PSA_ALG_IS_HASH(alg) \
493 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
494#define PSA_ALG_IS_MAC(alg) \
495 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
496#define PSA_ALG_IS_CIPHER(alg) \
497 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
498#define PSA_ALG_IS_AEAD(alg) \
499 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
500#define PSA_ALG_IS_SIGN(alg) \
501 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
502#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
503 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
504#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
505 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
506#define PSA_ALG_IS_KEY_DERIVATION(alg) \
507 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
508
509#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
510#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
511#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
512#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
Gilles Peskinee3f694f2018-03-08 07:48:40 +0100513#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
514#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100515#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
516#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
517#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
518#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
519#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
520#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
521#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
522#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
523#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
524#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
525
Gilles Peskine8c9def32018-02-08 10:02:12 +0100526#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100527#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
Gilles Peskine35855962018-04-19 08:39:16 +0200528/** Macro to build an HMAC algorithm.
529 *
530 * For example, `PSA_ALG_HMAC(PSA_ALG_SHA256)` is HMAC-SHA-256.
531 *
532 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
533 * #PSA_ALG_IS_HASH(alg) is true).
534 *
535 * \return The corresponding HMAC algorithm.
536 * \return Unspecified if \p alg is not a hash algorithm.
537 */
538#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100539 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
540#define PSA_ALG_HMAC_HASH(hmac_alg) \
541 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
542#define PSA_ALG_IS_HMAC(alg) \
543 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
544 PSA_ALG_HMAC_BASE)
545#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
546#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
547#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
548#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
549#define PSA_ALG_IS_CIPHER_MAC(alg) \
550 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
551 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100552
Gilles Peskine8c9def32018-02-08 10:02:12 +0100553#define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100554#define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100555#define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100556#define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000)
557#define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100558#define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100559#define PSA_ALG_IS_BLOCK_CIPHER(alg) \
560 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
561 PSA_ALG_BLOCK_CIPHER_BASE)
562
Gilles Peskine98f0a242018-02-06 18:57:29 +0100563#define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100564#define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002)
565#define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003)
566#define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100567#define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t)0x04800000)
568#define PSA_ALG_CTR ((psa_algorithm_t)0x04800001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100569#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100570
Moran Pekerbed71a22018-04-22 20:19:20 +0300571#define PSA_ALG_IS_STREAM_CIPHER(alg) \
572 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
573 PSA_ALG_STREAM_CIPHER)
574
Gilles Peskine8c9def32018-02-08 10:02:12 +0100575#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
576#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100577
Gilles Peskinea5926232018-03-28 14:16:50 +0200578#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW ((psa_algorithm_t)0x10010000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100579#define PSA_ALG_RSA_PSS_MGF1 ((psa_algorithm_t)0x10020000)
Gilles Peskine6944f9a2018-03-28 14:18:39 +0200580#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12010000)
581#define PSA_ALG_RSA_OAEP_MGF1_BASE ((psa_algorithm_t)0x12020000)
Gilles Peskinea5926232018-03-28 14:16:50 +0200582#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
583 (PSA_ALG_RSA_PKCS1V15_SIGN_RAW | ((hash_alg) & PSA_ALG_HASH_MASK))
584#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Gilles Peskine9673cc82018-04-11 16:57:49 +0200585 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_RAW)
586#define PSA_ALG_RSA_OAEP_MGF1(hash_alg) \
587 (PSA_ALG_RSA_OAEP_MGF1_RAW | ((hash_alg) & PSA_ALG_HASH_MASK))
588#define PSA_ALG_IS_RSA_OAEP_MGF1(alg) \
Gilles Peskine625b01c2018-06-08 17:43:16 +0200589 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_MGF1_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100590#define PSA_ALG_RSA_GET_HASH(alg) \
591 (((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100592
Gilles Peskined1e8e412018-06-07 09:49:39 +0200593#define PSA_ALG_ECDSA_RAW ((psa_algorithm_t)0x10030000)
594
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100595/**@}*/
596
597/** \defgroup key_management Key management
598 * @{
599 */
600
601/**
602 * \brief Import a key in binary format.
603 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100604 * This function supports any output from psa_export_key(). Refer to the
605 * documentation of psa_export_key() for the format for each key type.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100606 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100607 * \param key Slot where the key will be stored. This must be a
608 * valid slot for a key of the chosen type. It must
609 * be unoccupied.
610 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
611 * \param data Buffer containing the key data.
612 * \param data_length Size of the \c data buffer in bytes.
613 *
614 * \retval PSA_SUCCESS
615 * Success.
616 * \retval PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200617 * The key type or key size is not supported, either by the
618 * implementation in general or in this particular slot.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100619 * \retval PSA_ERROR_INVALID_ARGUMENT
620 * The key slot is invalid,
621 * or the key data is not correctly formatted.
622 * \retval PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +0200623 * There is already a key in the specified slot.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100624 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine65eb8582018-04-19 08:28:58 +0200625 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100626 * \retval PSA_ERROR_COMMUNICATION_FAILURE
627 * \retval PSA_ERROR_HARDWARE_FAILURE
628 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100629 */
630psa_status_t psa_import_key(psa_key_slot_t key,
631 psa_key_type_t type,
632 const uint8_t *data,
633 size_t data_length);
634
635/**
Gilles Peskine154bd952018-04-19 08:38:16 +0200636 * \brief Destroy a key and restore the slot to its default state.
637 *
638 * This function destroys the content of the key slot from both volatile
639 * memory and, if applicable, non-volatile storage. Implementations shall
640 * make a best effort to ensure that any previous content of the slot is
641 * unrecoverable.
642 *
643 * This function also erases any metadata such as policies. It returns the
644 * specified slot to its default state.
645 *
646 * \param key The key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100647 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100648 * \retval PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200649 * The slot's content, if any, has been erased.
650 * \retval PSA_ERROR_NOT_PERMITTED
651 * The slot holds content and cannot be erased because it is
652 * read-only, either due to a policy or due to physical restrictions.
653 * \retval PSA_ERROR_INVALID_ARGUMENT
654 * The specified slot number does not designate a valid slot.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100655 * \retval PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200656 * There was an failure in communication with the cryptoprocessor.
657 * The key material may still be present in the cryptoprocessor.
658 * \retval PSA_ERROR_STORAGE_FAILURE
659 * The storage is corrupted. Implementations shall make a best effort
660 * to erase key material even in this stage, however applications
661 * should be aware that it may be impossible to guarantee that the
662 * key material is not recoverable in such cases.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100663 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200664 * An unexpected condition which is not a storage corruption or
665 * a communication failure occurred. The cryptoprocessor may have
666 * been compromised.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100667 */
668psa_status_t psa_destroy_key(psa_key_slot_t key);
669
670/**
671 * \brief Get basic metadata about a key.
672 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100673 * \param key Slot whose content is queried. This must
674 * be an occupied key slot.
675 * \param type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
676 * This may be a null pointer, in which case the key type
677 * is not written.
678 * \param bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100679 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100680 * is not written.
681 *
682 * \retval PSA_SUCCESS
683 * \retval PSA_ERROR_EMPTY_SLOT
684 * \retval PSA_ERROR_COMMUNICATION_FAILURE
685 * \retval PSA_ERROR_HARDWARE_FAILURE
686 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100687 */
688psa_status_t psa_get_key_information(psa_key_slot_t key,
689 psa_key_type_t *type,
690 size_t *bits);
691
692/**
693 * \brief Export a key in binary format.
694 *
695 * The output of this function can be passed to psa_import_key() to
696 * create an equivalent object.
697 *
698 * If a key is created with psa_import_key() and then exported with
699 * this function, it is not guaranteed that the resulting data is
700 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +0100701 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100702 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100703 * For standard key types, the output format is as follows:
704 *
705 * - For symmetric keys (including MAC keys), the format is the
706 * raw bytes of the key.
707 * - For DES, the key data consists of 8 bytes. The parity bits must be
708 * correct.
709 * - For Triple-DES, the format is the concatenation of the
710 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100711 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine308b91d2018-02-08 09:47:44 +0100712 * is the non-encrypted DER representation defined by PKCS\#8 (RFC 5208)
713 * as PrivateKeyInfo.
714 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
Gilles Peskine971f7062018-03-20 17:52:58 +0100715 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100716 *
717 * \param key Slot whose content is to be exported. This must
718 * be an occupied key slot.
719 * \param data Buffer where the key data is to be written.
720 * \param data_size Size of the \c data buffer in bytes.
721 * \param data_length On success, the number of bytes
722 * that make up the key data.
723 *
724 * \retval PSA_SUCCESS
725 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine92b30732018-03-03 21:29:30 +0100726 * \retval PSA_ERROR_NOT_PERMITTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100727 * \retval PSA_ERROR_COMMUNICATION_FAILURE
728 * \retval PSA_ERROR_HARDWARE_FAILURE
729 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100730 */
731psa_status_t psa_export_key(psa_key_slot_t key,
732 uint8_t *data,
733 size_t data_size,
734 size_t *data_length);
735
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100736/**
737 * \brief Export a public key or the public part of a key pair in binary format.
738 *
739 * The output of this function can be passed to psa_import_key() to
740 * create an object that is equivalent to the public key.
741 *
742 * For standard key types, the output format is as follows:
743 *
744 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
Moran Pekerdd4ea382018-04-03 15:30:03 +0300745 * the format is the DER representation of the public key defined by RFC 5280
Gilles Peskine971f7062018-03-20 17:52:58 +0100746 * as SubjectPublicKeyInfo.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100747 *
748 * \param key Slot whose content is to be exported. This must
749 * be an occupied key slot.
750 * \param data Buffer where the key data is to be written.
751 * \param data_size Size of the \c data buffer in bytes.
752 * \param data_length On success, the number of bytes
753 * that make up the key data.
754 *
755 * \retval PSA_SUCCESS
756 * \retval PSA_ERROR_EMPTY_SLOT
757 * \retval PSA_ERROR_INVALID_ARGUMENT
758 * \retval PSA_ERROR_COMMUNICATION_FAILURE
759 * \retval PSA_ERROR_HARDWARE_FAILURE
760 * \retval PSA_ERROR_TAMPERING_DETECTED
761 */
762psa_status_t psa_export_public_key(psa_key_slot_t key,
763 uint8_t *data,
764 size_t data_size,
765 size_t *data_length);
766
767/**@}*/
768
769/** \defgroup policy Key policies
770 * @{
771 */
772
773/** \brief Encoding of permitted usage on a key. */
774typedef uint32_t psa_key_usage_t;
775
Gilles Peskine7e198532018-03-08 07:50:30 +0100776/** Whether the key may be exported.
777 *
778 * A public key or the public part of a key pair may always be exported
779 * regardless of the value of this permission flag.
780 *
781 * If a key does not have export permission, implementations shall not
782 * allow the key to be exported in plain form from the cryptoprocessor,
783 * whether through psa_export_key() or through a proprietary interface.
784 * The key may however be exportable in a wrapped form, i.e. in a form
785 * where it is encrypted by another key.
786 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100787#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
788
Gilles Peskine7e198532018-03-08 07:50:30 +0100789/** Whether the key may be used to encrypt a message.
790 *
791 * For a key pair, this concerns the public key.
792 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100793#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +0100794
795/** Whether the key may be used to decrypt a message.
796 *
797 * For a key pair, this concerns the private key.
798 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100799#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +0100800
801/** Whether the key may be used to sign a message.
802 *
803 * For a key pair, this concerns the private key.
804 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100805#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +0100806
807/** Whether the key may be used to verify a message signature.
808 *
809 * For a key pair, this concerns the public key.
810 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100811#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
812
813/** The type of the key policy data structure.
814 *
815 * This is an implementation-defined \c struct. Applications should not
816 * make any assumptions about the content of this structure except
817 * as directed by the documentation of a specific implementation. */
818typedef struct psa_key_policy_s psa_key_policy_t;
819
820/** \brief Initialize a key policy structure to a default that forbids all
821 * usage of the key. */
822void psa_key_policy_init(psa_key_policy_t *policy);
823
Gilles Peskine7e198532018-03-08 07:50:30 +0100824/** \brief Set the standard fields of a policy structure.
825 *
826 * Note that this function does not make any consistency check of the
827 * parameters. The values are only checked when applying the policy to
828 * a key slot with psa_set_key_policy().
829 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100830void psa_key_policy_set_usage(psa_key_policy_t *policy,
831 psa_key_usage_t usage,
832 psa_algorithm_t alg);
833
834psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy);
835
836psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy);
837
838/** \brief Set the usage policy on a key slot.
839 *
840 * This function must be called on an empty key slot, before importing,
841 * generating or creating a key in the slot. Changing the policy of an
842 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100843 *
844 * Implementations may set restrictions on supported key policies
845 * depending on the key type and the key slot.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100846 */
847psa_status_t psa_set_key_policy(psa_key_slot_t key,
848 const psa_key_policy_t *policy);
849
Gilles Peskine7e198532018-03-08 07:50:30 +0100850/** \brief Get the usage policy for a key slot.
851 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100852psa_status_t psa_get_key_policy(psa_key_slot_t key,
853 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100854
855/**@}*/
856
Gilles Peskine609b6a52018-03-03 21:31:50 +0100857/** \defgroup persistence Key lifetime
858 * @{
859 */
860
861/** Encoding of key lifetimes.
862 */
863typedef uint32_t psa_key_lifetime_t;
864
865/** A volatile key slot retains its content as long as the application is
866 * running. It is guaranteed to be erased on a power reset.
867 */
868#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
869
870/** A persistent key slot retains its content as long as it is not explicitly
871 * destroyed.
872 */
873#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
874
875/** A write-once key slot may not be modified once a key has been set.
876 * It will retain its content as long as the device remains operational.
877 */
878#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
879
Gilles Peskined393e182018-03-08 07:49:16 +0100880/** \brief Retrieve the lifetime of a key slot.
881 *
882 * The assignment of lifetimes to slots is implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200883 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +0200884 * \param key Slot to query.
mohammad1603804cd712018-03-20 22:44:08 +0200885 * \param lifetime On success, the lifetime value.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200886 *
mohammad1603804cd712018-03-20 22:44:08 +0200887 * \retval PSA_SUCCESS
888 * Success.
889 * \retval PSA_ERROR_INVALID_ARGUMENT
mohammad1603a7d245a2018-04-17 00:40:08 -0700890 * The key slot is invalid.
Gilles Peskinef0c9dd32018-04-17 14:11:07 +0200891 * \retval PSA_ERROR_COMMUNICATION_FAILURE
892 * \retval PSA_ERROR_HARDWARE_FAILURE
893 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +0100894 */
Gilles Peskine609b6a52018-03-03 21:31:50 +0100895psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
896 psa_key_lifetime_t *lifetime);
897
Gilles Peskined393e182018-03-08 07:49:16 +0100898/** \brief Change the lifetime of a key slot.
899 *
900 * Whether the lifetime of a key slot can be changed at all, and if so
Gilles Peskine19067982018-03-20 17:54:53 +0100901 * whether the lifetime of an occupied key slot can be changed, is
Gilles Peskined393e182018-03-08 07:49:16 +0100902 * implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200903 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +0200904 * \param key Slot whose lifetime is to be changed.
905 * \param lifetime The lifetime value to set for the given key slot.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200906 *
mohammad1603804cd712018-03-20 22:44:08 +0200907 * \retval PSA_SUCCESS
908 * Success.
909 * \retval PSA_ERROR_INVALID_ARGUMENT
910 * The key slot is invalid,
mohammad1603a7d245a2018-04-17 00:40:08 -0700911 * or the lifetime value is invalid.
Gilles Peskinef0c9dd32018-04-17 14:11:07 +0200912 * \retval PSA_ERROR_NOT_SUPPORTED
913 * The implementation does not support the specified lifetime value,
914 * at least for the specified key slot.
915 * \retval PSA_ERROR_OCCUPIED_SLOT
916 * The slot contains a key, and the implementation does not support
917 * changing the lifetime of an occupied slot.
918 * \retval PSA_ERROR_COMMUNICATION_FAILURE
919 * \retval PSA_ERROR_HARDWARE_FAILURE
920 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +0100921 */
922psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
mohammad1603ea050092018-04-17 00:31:34 -0700923 psa_key_lifetime_t lifetime);
Gilles Peskined393e182018-03-08 07:49:16 +0100924
Gilles Peskine609b6a52018-03-03 21:31:50 +0100925/**@}*/
926
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100927/** \defgroup hash Message digests
928 * @{
929 */
930
Gilles Peskine308b91d2018-02-08 09:47:44 +0100931/** The type of the state data structure for multipart hash operations.
932 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100933 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100934 * make any assumptions about the content of this structure except
935 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100936typedef struct psa_hash_operation_s psa_hash_operation_t;
937
Gilles Peskine308b91d2018-02-08 09:47:44 +0100938/** The size of the output of psa_hash_finish(), in bytes.
939 *
940 * This is also the hash size that psa_hash_verify() expects.
941 *
942 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine35855962018-04-19 08:39:16 +0200943 * #PSA_ALG_IS_HASH(alg) is true), or an HMAC algorithm
944 * (`PSA_ALG_HMAC(hash_alg)` where `hash_alg` is a
945 * hash algorithm).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100946 *
947 * \return The hash size for the specified hash algorithm.
948 * If the hash algorithm is not recognized, return 0.
949 * An implementation may return either 0 or the correct size
950 * for a hash algorithm that it recognizes, but does not support.
951 */
Gilles Peskine71bb7b72018-04-19 08:29:59 +0200952#define PSA_HASH_SIZE(alg) \
953 ( \
954 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
955 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \
956 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
957 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
958 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
959 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
960 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
961 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
962 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
963 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
964 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
965 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
966 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
967 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
968 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100969 0)
970
Gilles Peskine308b91d2018-02-08 09:47:44 +0100971/** Start a multipart hash operation.
972 *
973 * The sequence of operations to calculate a hash (message digest)
974 * is as follows:
975 * -# Allocate an operation object which will be passed to all the functions
976 * listed here.
977 * -# Call psa_hash_start() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100978 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100979 * of the message each time. The hash that is calculated is the hash
980 * of the concatenation of these messages in order.
981 * -# To calculate the hash, call psa_hash_finish().
982 * To compare the hash with an expected value, call psa_hash_verify().
983 *
984 * The application may call psa_hash_abort() at any time after the operation
985 * has been initialized with psa_hash_start().
986 *
987 * After a successful call to psa_hash_start(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100988 * eventually terminate the operation. The following events terminate an
989 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100990 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100991 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100992 *
Gilles Peskine36a74b72018-06-01 16:30:32 +0200993 * \param operation The operation object to use.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100994 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
995 * such that #PSA_ALG_IS_HASH(alg) is true).
996 *
997 * \retval PSA_SUCCESS
998 * Success.
999 * \retval PSA_ERROR_NOT_SUPPORTED
1000 * \c alg is not supported or is not a hash algorithm.
1001 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1002 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1003 * \retval PSA_ERROR_HARDWARE_FAILURE
1004 * \retval PSA_ERROR_TAMPERING_DETECTED
1005 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001006psa_status_t psa_hash_start(psa_hash_operation_t *operation,
1007 psa_algorithm_t alg);
1008
Gilles Peskine308b91d2018-02-08 09:47:44 +01001009/** Add a message fragment to a multipart hash operation.
1010 *
1011 * The application must call psa_hash_start() before calling this function.
1012 *
1013 * If this function returns an error status, the operation becomes inactive.
1014 *
1015 * \param operation Active hash operation.
1016 * \param input Buffer containing the message fragment to hash.
1017 * \param input_length Size of the \c input buffer in bytes.
1018 *
1019 * \retval PSA_SUCCESS
1020 * Success.
1021 * \retval PSA_ERROR_BAD_STATE
1022 * The operation state is not valid (not started, or already completed).
1023 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1024 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1025 * \retval PSA_ERROR_HARDWARE_FAILURE
1026 * \retval PSA_ERROR_TAMPERING_DETECTED
1027 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001028psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1029 const uint8_t *input,
1030 size_t input_length);
1031
Gilles Peskine308b91d2018-02-08 09:47:44 +01001032/** Finish the calculation of the hash of a message.
1033 *
1034 * The application must call psa_hash_start() before calling this function.
1035 * This function calculates the hash of the message formed by concatenating
1036 * the inputs passed to preceding calls to psa_hash_update().
1037 *
1038 * When this function returns, the operation becomes inactive.
1039 *
1040 * \warning Applications should not call this function if they expect
1041 * a specific value for the hash. Call psa_hash_verify() instead.
1042 * Beware that comparing integrity or authenticity data such as
1043 * hash values with a function such as \c memcmp is risky
1044 * because the time taken by the comparison may leak information
1045 * about the hashed data which could allow an attacker to guess
1046 * a valid hash and thereby bypass security controls.
1047 *
1048 * \param operation Active hash operation.
1049 * \param hash Buffer where the hash is to be written.
1050 * \param hash_size Size of the \c hash buffer in bytes.
1051 * \param hash_length On success, the number of bytes
1052 * that make up the hash value. This is always
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001053 * #PSA_HASH_SIZE(alg) where \c alg is the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001054 * hash algorithm that is calculated.
1055 *
1056 * \retval PSA_SUCCESS
1057 * Success.
1058 * \retval PSA_ERROR_BAD_STATE
1059 * The operation state is not valid (not started, or already completed).
1060 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1061 * The size of the \c hash buffer is too small. You can determine a
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001062 * sufficient buffer size by calling #PSA_HASH_SIZE(alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001063 * where \c alg is the hash algorithm that is calculated.
1064 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1065 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1066 * \retval PSA_ERROR_HARDWARE_FAILURE
1067 * \retval PSA_ERROR_TAMPERING_DETECTED
1068 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001069psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1070 uint8_t *hash,
1071 size_t hash_size,
1072 size_t *hash_length);
1073
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074/** Finish the calculation of the hash of a message and compare it with
1075 * an expected value.
1076 *
1077 * The application must call psa_hash_start() before calling this function.
1078 * This function calculates the hash of the message formed by concatenating
1079 * the inputs passed to preceding calls to psa_hash_update(). It then
1080 * compares the calculated hash with the expected hash passed as a
1081 * parameter to this function.
1082 *
1083 * When this function returns, the operation becomes inactive.
1084 *
Gilles Peskine19067982018-03-20 17:54:53 +01001085 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001086 * comparison between the actual hash and the expected hash is performed
1087 * in constant time.
1088 *
1089 * \param operation Active hash operation.
1090 * \param hash Buffer containing the expected hash value.
1091 * \param hash_length Size of the \c hash buffer in bytes.
1092 *
1093 * \retval PSA_SUCCESS
1094 * The expected hash is identical to the actual hash of the message.
1095 * \retval PSA_ERROR_INVALID_SIGNATURE
1096 * The hash of the message was calculated successfully, but it
1097 * differs from the expected hash.
1098 * \retval PSA_ERROR_BAD_STATE
1099 * The operation state is not valid (not started, or already completed).
1100 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1101 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1102 * \retval PSA_ERROR_HARDWARE_FAILURE
1103 * \retval PSA_ERROR_TAMPERING_DETECTED
1104 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001105psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1106 const uint8_t *hash,
1107 size_t hash_length);
1108
Gilles Peskine308b91d2018-02-08 09:47:44 +01001109/** Abort a hash operation.
1110 *
1111 * This function may be called at any time after psa_hash_start().
1112 * Aborting an operation frees all associated resources except for the
1113 * \c operation structure itself.
1114 *
1115 * Implementation should strive to be robust and handle inactive hash
1116 * operations safely (do nothing and return #PSA_ERROR_BAD_STATE). However,
1117 * application writers should beware that uninitialized memory may happen
1118 * to be indistinguishable from an active hash operation, and the behavior
1119 * of psa_hash_abort() is undefined in this case.
1120 *
1121 * \param operation Active hash operation.
1122 *
1123 * \retval PSA_SUCCESS
1124 * \retval PSA_ERROR_BAD_STATE
1125 * \c operation is not an active hash operation.
1126 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1127 * \retval PSA_ERROR_HARDWARE_FAILURE
1128 * \retval PSA_ERROR_TAMPERING_DETECTED
1129 */
1130psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001131
1132/**@}*/
1133
Gilles Peskine8c9def32018-02-08 10:02:12 +01001134/** \defgroup MAC Message authentication codes
1135 * @{
1136 */
1137
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001138/** The type of the state data structure for multipart MAC operations.
1139 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001140 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001141 * make any assumptions about the content of this structure except
1142 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001143typedef struct psa_mac_operation_s psa_mac_operation_t;
1144
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001145/** The size of the output of psa_mac_finish(), in bytes.
1146 *
1147 * This is also the MAC size that psa_mac_verify() expects.
1148 *
1149 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
1150 * #PSA_ALG_IS_MAC(alg) is true).
1151 *
1152 * \return The MAC size for the specified algorithm.
1153 * If the MAC algorithm is not recognized, return 0.
1154 * An implementation may return either 0 or the correct size
1155 * for a MAC algorithm that it recognizes, but does not support.
1156 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001157#define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001158 (PSA_ALG_IS_HMAC(alg) ? PSA_HASH_SIZE(PSA_ALG_HMAC_HASH(alg)) : \
Gilles Peskine8c9def32018-02-08 10:02:12 +01001159 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
1160 0)
1161
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001162/** Start a multipart MAC operation.
1163 *
1164 * The sequence of operations to calculate a MAC (message authentication code)
1165 * is as follows:
1166 * -# Allocate an operation object which will be passed to all the functions
1167 * listed here.
1168 * -# Call psa_mac_start() to specify the algorithm and key.
1169 * The key remains associated with the operation even if the content
1170 * of the key slot changes.
1171 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1172 * of the message each time. The MAC that is calculated is the MAC
1173 * of the concatenation of these messages in order.
1174 * -# To calculate the MAC, call psa_mac_finish().
1175 * To compare the MAC with an expected value, call psa_mac_verify().
1176 *
1177 * The application may call psa_mac_abort() at any time after the operation
1178 * has been initialized with psa_mac_start().
1179 *
1180 * After a successful call to psa_mac_start(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001181 * eventually terminate the operation. The following events terminate an
1182 * operation:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001183 * - A failed call to psa_mac_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001184 * - A call to psa_mac_finish(), psa_mac_verify() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001185 *
Gilles Peskine36a74b72018-06-01 16:30:32 +02001186 * \param operation The operation object to use.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001187 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1188 * such that #PSA_ALG_IS_MAC(alg) is true).
1189 *
1190 * \retval PSA_SUCCESS
1191 * Success.
1192 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine92b30732018-03-03 21:29:30 +01001193 * \retval PSA_ERROR_NOT_PERMITTED
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001194 * \retval PSA_ERROR_INVALID_ARGUMENT
1195 * \c key is not compatible with \c alg.
1196 * \retval PSA_ERROR_NOT_SUPPORTED
1197 * \c alg is not supported or is not a MAC algorithm.
1198 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1199 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1200 * \retval PSA_ERROR_HARDWARE_FAILURE
1201 * \retval PSA_ERROR_TAMPERING_DETECTED
1202 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001203psa_status_t psa_mac_start(psa_mac_operation_t *operation,
1204 psa_key_slot_t key,
1205 psa_algorithm_t alg);
1206
1207psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1208 const uint8_t *input,
1209 size_t input_length);
1210
1211psa_status_t psa_mac_finish(psa_mac_operation_t *operation,
1212 uint8_t *mac,
1213 size_t mac_size,
1214 size_t *mac_length);
1215
1216psa_status_t psa_mac_verify(psa_mac_operation_t *operation,
1217 const uint8_t *mac,
1218 size_t mac_length);
1219
1220psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1221
1222/**@}*/
1223
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001224/** \defgroup cipher Symmetric ciphers
1225 * @{
1226 */
1227
1228/** The type of the state data structure for multipart cipher operations.
1229 *
1230 * This is an implementation-defined \c struct. Applications should not
1231 * make any assumptions about the content of this structure except
1232 * as directed by the documentation of a specific implementation. */
1233typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1234
1235/** Set the key for a multipart symmetric encryption operation.
1236 *
1237 * The sequence of operations to encrypt a message with a symmetric cipher
1238 * is as follows:
1239 * -# Allocate an operation object which will be passed to all the functions
1240 * listed here.
1241 * -# Call psa_encrypt_setup() to specify the algorithm and key.
1242 * The key remains associated with the operation even if the content
1243 * of the key slot changes.
1244 * -# Call either psa_encrypt_generate_iv() or psa_encrypt_set_iv() to
1245 * generate or set the IV (initialization vector). You should use
1246 * psa_encrypt_generate_iv() unless the protocol you are implementing
1247 * requires a specific IV value.
1248 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1249 * of the message each time.
1250 * -# Call psa_cipher_finish().
1251 *
1252 * The application may call psa_cipher_abort() at any time after the operation
1253 * has been initialized with psa_encrypt_setup().
1254 *
1255 * After a successful call to psa_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001256 * eventually terminate the operation. The following events terminate an
1257 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001258 * - A failed call to psa_encrypt_generate_iv(), psa_encrypt_set_iv()
1259 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001260 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001261 *
Gilles Peskine36a74b72018-06-01 16:30:32 +02001262 * \param operation The operation object to use.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001263 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
1264 * such that #PSA_ALG_IS_CIPHER(alg) is true).
1265 *
1266 * \retval PSA_SUCCESS
1267 * Success.
1268 * \retval PSA_ERROR_EMPTY_SLOT
1269 * \retval PSA_ERROR_NOT_PERMITTED
1270 * \retval PSA_ERROR_INVALID_ARGUMENT
1271 * \c key is not compatible with \c alg.
1272 * \retval PSA_ERROR_NOT_SUPPORTED
1273 * \c alg is not supported or is not a cipher algorithm.
1274 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1275 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1276 * \retval PSA_ERROR_HARDWARE_FAILURE
1277 * \retval PSA_ERROR_TAMPERING_DETECTED
1278 */
1279psa_status_t psa_encrypt_setup(psa_cipher_operation_t *operation,
1280 psa_key_slot_t key,
1281 psa_algorithm_t alg);
1282
1283/** Set the key for a multipart symmetric decryption operation.
1284 *
1285 * The sequence of operations to decrypt a message with a symmetric cipher
1286 * is as follows:
1287 * -# Allocate an operation object which will be passed to all the functions
1288 * listed here.
1289 * -# Call psa_decrypt_setup() to specify the algorithm and key.
1290 * The key remains associated with the operation even if the content
1291 * of the key slot changes.
1292 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1293 * decryption. If the IV is prepended to the ciphertext, you can call
1294 * psa_cipher_update() on a buffer containing the IV followed by the
1295 * beginning of the message.
1296 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1297 * of the message each time.
1298 * -# Call psa_cipher_finish().
1299 *
1300 * The application may call psa_cipher_abort() at any time after the operation
1301 * has been initialized with psa_encrypt_setup().
1302 *
1303 * After a successful call to psa_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001304 * eventually terminate the operation. The following events terminate an
1305 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001306 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001307 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001308 *
Gilles Peskine36a74b72018-06-01 16:30:32 +02001309 * \param operation The operation object to use.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001310 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
1311 * such that #PSA_ALG_IS_CIPHER(alg) is true).
1312 *
1313 * \retval PSA_SUCCESS
1314 * Success.
1315 * \retval PSA_ERROR_EMPTY_SLOT
1316 * \retval PSA_ERROR_NOT_PERMITTED
1317 * \retval PSA_ERROR_INVALID_ARGUMENT
1318 * \c key is not compatible with \c alg.
1319 * \retval PSA_ERROR_NOT_SUPPORTED
1320 * \c alg is not supported or is not a cipher algorithm.
1321 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1322 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1323 * \retval PSA_ERROR_HARDWARE_FAILURE
1324 * \retval PSA_ERROR_TAMPERING_DETECTED
1325 */
1326psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation,
1327 psa_key_slot_t key,
1328 psa_algorithm_t alg);
1329
1330psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation,
1331 unsigned char *iv,
1332 size_t iv_size,
1333 size_t *iv_length);
1334
1335psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation,
1336 const unsigned char *iv,
1337 size_t iv_length);
1338
1339psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1340 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001341 size_t input_length,
1342 unsigned char *output,
1343 size_t output_size,
1344 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001345
1346psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001347 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001348 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001349 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001350
1351psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1352
1353/**@}*/
1354
Gilles Peskine3b555712018-03-03 21:27:57 +01001355/** \defgroup aead Authenticated encryption with associated data (AEAD)
1356 * @{
1357 */
1358
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001359/** The tag size for an AEAD algorithm, in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001360 *
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001361 * \param alg An AEAD algorithm
1362 * (\c PSA_ALG_XXX value such that
1363 * #PSA_ALG_IS_AEAD(alg) is true).
1364 *
1365 * \return The tag size for the specified algorithm.
1366 * If the AEAD algorithm does not have an identified
1367 * tag that can be distinguished from the rest of
1368 * the ciphertext, return 0.
1369 * If the AEAD algorithm is not recognized, return 0.
1370 * An implementation may return either 0 or a
1371 * correct size for an AEAD algorithm that it
1372 * recognizes, but does not support.
1373 */
1374#define PSA_AEAD_TAG_SIZE(alg) \
1375 ((alg) == PSA_ALG_GCM ? 16 : \
1376 (alg) == PSA_ALG_CCM ? 16 : \
1377 0)
Gilles Peskine3b555712018-03-03 21:27:57 +01001378
Gilles Peskine212e4d82018-06-08 11:36:37 +02001379/** The maximum size of the output of psa_aead_encrypt(), in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001380 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001381 * If the size of the ciphertext buffer is at least this large, it is
1382 * guaranteed that psa_aead_encrypt() will not fail due to an
1383 * insufficient buffer size. Depending on the algorithm, the actual size of
1384 * the ciphertext may be smaller.
Gilles Peskine3b555712018-03-03 21:27:57 +01001385 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001386 * \param alg An AEAD algorithm
mohammad16031347a732018-06-07 01:38:45 +03001387 * (\c PSA_ALG_XXX value such that
1388 * #PSA_ALG_IS_AEAD(alg) is true).
Gilles Peskine212e4d82018-06-08 11:36:37 +02001389 * \param plaintext_length Size of the plaintext in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001390 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001391 * \return The AEAD ciphertext size for the specified
1392 * algorithm.
1393 * If the AEAD algorithm is not recognized, return 0.
1394 * An implementation may return either 0 or a
1395 * correct size for an AEAD algorithm that it
1396 * recognizes, but does not support.
mohammad16031347a732018-06-07 01:38:45 +03001397 */
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001398#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) \
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001399 (PSA_AEAD_TAG_SIZE(alg) != 0 ? \
1400 (plaintext_length) + PSA_AEAD_TAG_SIZE(alg) : \
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001401 0)
1402
1403/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001404 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001405 * \param key Slot containing the key to use.
1406 * \param alg The AEAD algorithm to compute
1407 * (\c PSA_ALG_XXX value such that
1408 * #PSA_ALG_IS_AEAD(alg) is true).
1409 * \param nonce Nonce or IV to use.
1410 * \param nonce_length Size of the \p nonce buffer in bytes.
1411 * \param additional_data Additional data that will be authenticated
1412 * but not encrypted.
1413 * \param additional_data_length Size of \p additional_data in bytes.
1414 * \param plaintext Data that will be authenticated and
1415 * encrypted.
1416 * \param plaintext_length Size of \p plaintext in bytes.
1417 * \param ciphertext Output buffer for the authenticated and
1418 * encrypted data. The additional data is not
1419 * part of this output. For algorithms where the
1420 * encrypted data and the authentication tag
1421 * are defined as separate outputs, the
1422 * authentication tag is appended to the
1423 * encrypted data.
1424 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1425 * This must be at least
1426 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1427 * \p plaintext_length).
1428 * \param ciphertext_length On success, the size of the output
1429 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001430 *
1431 * \retval PSA_SUCCESS
1432 * Success.
1433 * \retval PSA_ERROR_EMPTY_SLOT
1434 * \retval PSA_ERROR_NOT_PERMITTED
1435 * \retval PSA_ERROR_INVALID_ARGUMENT
1436 * \c key is not compatible with \c alg.
1437 * \retval PSA_ERROR_NOT_SUPPORTED
1438 * \c alg is not supported or is not an AEAD algorithm.
1439 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1440 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1441 * \retval PSA_ERROR_HARDWARE_FAILURE
1442 * \retval PSA_ERROR_TAMPERING_DETECTED
1443 */
mohammad160339ee8712018-04-26 00:51:02 +03001444psa_status_t psa_aead_encrypt( psa_key_slot_t key,
1445 psa_algorithm_t alg,
1446 const uint8_t *nonce,
1447 size_t nonce_length,
1448 const uint8_t *additional_data,
1449 size_t additional_data_length,
1450 const uint8_t *plaintext,
1451 size_t plaintext_length,
1452 uint8_t *ciphertext,
1453 size_t ciphertext_size,
1454 size_t *ciphertext_length );
Gilles Peskine3b555712018-03-03 21:27:57 +01001455
Gilles Peskine212e4d82018-06-08 11:36:37 +02001456/** The maximum size of the output of psa_aead_decrypt(), in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001457 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001458 * If the size of the plaintext buffer is at least this large, it is
1459 * guaranteed that psa_aead_decrypt() will not fail due to an
1460 * insufficient buffer size. Depending on the algorithm, the actual size of
1461 * the plaintext may be smaller.
Gilles Peskine3b555712018-03-03 21:27:57 +01001462 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001463 * \param alg An AEAD algorithm
mohammad16031347a732018-06-07 01:38:45 +03001464 * (\c PSA_ALG_XXX value such that
1465 * #PSA_ALG_IS_AEAD(alg) is true).
Gilles Peskine212e4d82018-06-08 11:36:37 +02001466 * \param ciphertext_length Size of the plaintext in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001467 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001468 * \return The AEAD ciphertext size for the specified
1469 * algorithm.
1470 * If the AEAD algorithm is not recognized, return 0.
1471 * An implementation may return either 0 or a
1472 * correct size for an AEAD algorithm that it
1473 * recognizes, but does not support.
mohammad16031347a732018-06-07 01:38:45 +03001474 */
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001475#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \
1476 (PSA_AEAD_TAG_SIZE(alg) != 0 ? \
1477 (plaintext_length) - PSA_AEAD_TAG_SIZE(alg) : \
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001478 0)
1479
1480/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001481 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001482 * \param key Slot containing the key to use.
1483 * \param alg The AEAD algorithm to compute
1484 * (\c PSA_ALG_XXX value such that
1485 * #PSA_ALG_IS_AEAD(alg) is true).
1486 * \param nonce Nonce or IV to use.
1487 * \param nonce_length Size of the \p nonce buffer in bytes.
1488 * \param additional_data Additional data that has been authenticated
1489 * but not encrypted.
1490 * \param additional_data_length Size of \p additional_data in bytes.
1491 * \param ciphertext Data that has been authenticated and
1492 * encrypted. For algorithms where the
1493 * encrypted data and the authentication tag
1494 * are defined as separate inputs, the buffer
1495 * must contain the encrypted data followed
1496 * by the authentication tag.
1497 * \param ciphertext_length Size of \p ciphertext in bytes.
1498 * \param plaintext Output buffer for the decrypted data.
1499 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1500 * This must be at least
1501 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1502 * \p ciphertext_length).
1503 * \param plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001504 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001505 *
1506 * \retval PSA_SUCCESS
1507 * Success.
1508 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001509 * \retval PSA_ERROR_INVALID_SIGNATURE
1510 * The ciphertext is not authentic.
Gilles Peskine3b555712018-03-03 21:27:57 +01001511 * \retval PSA_ERROR_NOT_PERMITTED
1512 * \retval PSA_ERROR_INVALID_ARGUMENT
1513 * \c key is not compatible with \c alg.
1514 * \retval PSA_ERROR_NOT_SUPPORTED
Gilles Peskine19067982018-03-20 17:54:53 +01001515 * \c alg is not supported or is not an AEAD algorithm.
Gilles Peskine3b555712018-03-03 21:27:57 +01001516 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1517 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1518 * \retval PSA_ERROR_HARDWARE_FAILURE
1519 * \retval PSA_ERROR_TAMPERING_DETECTED
1520 */
mohammad160339ee8712018-04-26 00:51:02 +03001521psa_status_t psa_aead_decrypt( psa_key_slot_t key,
1522 psa_algorithm_t alg,
1523 const uint8_t *nonce,
1524 size_t nonce_length,
1525 const uint8_t *additional_data,
1526 size_t additional_data_length,
1527 const uint8_t *ciphertext,
1528 size_t ciphertext_length,
1529 uint8_t *plaintext,
1530 size_t plaintext_size,
1531 size_t *plaintext_length );
Gilles Peskine3b555712018-03-03 21:27:57 +01001532
1533/**@}*/
1534
Gilles Peskine20035e32018-02-03 22:44:14 +01001535/** \defgroup asymmetric Asymmetric cryptography
1536 * @{
1537 */
1538
1539/**
Gilles Peskine0189e752018-02-03 23:57:22 +01001540 * \brief Maximum ECDSA signature size for a given curve bit size
1541 *
1542 * \param curve_bits Curve size in bits
1543 * \return Maximum signature size in bytes
1544 *
1545 * \note This macro returns a compile-time constant if its argument is one.
1546 *
1547 * \warning This macro may evaluate its argument multiple times.
1548 */
1549/*
1550 * RFC 4492 page 20:
1551 *
1552 * Ecdsa-Sig-Value ::= SEQUENCE {
1553 * r INTEGER,
1554 * s INTEGER
1555 * }
1556 *
1557 * Size is at most
1558 * 1 (tag) + 1 (len) + 1 (initial 0) + curve_bytes for each of r and s,
1559 * twice that + 1 (tag) + 2 (len) for the sequence
1560 * (assuming curve_bytes is less than 126 for r and s,
1561 * and less than 124 (total len <= 255) for the sequence)
1562 */
1563#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
1564 ( /*T,L of SEQUENCE*/ ((curve_bits) >= 61 * 8 ? 3 : 2) + \
1565 /*T,L of r,s*/ 2 * (((curve_bits) >= 127 * 8 ? 3 : 2) + \
1566 /*V of r,s*/ ((curve_bits) + 8) / 8))
1567
1568
Gilles Peskine308b91d2018-02-08 09:47:44 +01001569/** Safe signature buffer size for psa_asymmetric_sign().
1570 *
1571 * This macro returns a safe buffer size for a signature using a key
1572 * of the specified type and size, with the specified algorithm.
1573 * Note that the actual size of the signature may be smaller
1574 * (some algorithms produce a variable-size signature).
1575 *
1576 * \warning This function may call its arguments multiple times or
1577 * zero times, so you should not pass arguments that contain
1578 * side effects.
1579 *
1580 * \param key_type An asymmetric key type (this may indifferently be a
1581 * key pair type or a public key type).
1582 * \param key_bits The size of the key in bits.
1583 * \param alg The signature algorithm.
1584 *
1585 * \return If the parameters are valid and supported, return
1586 * a buffer size in bytes that guarantees that
1587 * psa_asymmetric_sign() will not fail with
1588 * #PSA_ERROR_BUFFER_TOO_SMALL.
1589 * If the parameters are a valid combination that is not supported
1590 * by the implementation, this macro either shall return either a
1591 * sensible size or 0.
1592 * If the parameters are not valid, the
1593 * return value is unspecified.
1594 *
1595 */
Gilles Peskine0189e752018-02-03 23:57:22 +01001596#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine2905a7a2018-03-07 16:39:31 +01001597 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine0189e752018-02-03 23:57:22 +01001598 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
Gilles Peskine84845652018-03-28 14:17:40 +02001599 ((void)alg, 0))
Gilles Peskine0189e752018-02-03 23:57:22 +01001600
1601/**
Gilles Peskine20035e32018-02-03 22:44:14 +01001602 * \brief Sign a hash or short message with a private key.
1603 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001604 * \param key Key slot containing an asymmetric key pair.
1605 * \param alg A signature algorithm that is compatible with
1606 * the type of \c key.
1607 * \param hash The message to sign.
1608 * \param hash_length Size of the \c hash buffer in bytes.
1609 * \param salt A salt or label, if supported by the signature
1610 * algorithm.
1611 * If the signature algorithm does not support a
1612 * salt, pass \c NULL.
1613 * If the signature algorithm supports an optional
1614 * salt and you do not want to pass a salt,
1615 * pass \c NULL.
1616 * \param salt_length Size of the \c salt buffer in bytes.
1617 * If \c salt is \c NULL, pass 0.
1618 * \param signature Buffer where the signature is to be written.
1619 * \param signature_size Size of the \c signature buffer in bytes.
1620 * \param signature_length On success, the number of bytes
1621 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001622 *
1623 * \retval PSA_SUCCESS
1624 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1625 * The size of the \c signature buffer is too small. You can
1626 * determine a sufficient buffer size by calling
1627 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg)
1628 * where \c key_type and \c key_bits are the type and bit-size
1629 * respectively of \c key.
1630 * \retval PSA_ERROR_NOT_SUPPORTED
1631 * \retval PSA_ERROR_INVALID_ARGUMENT
1632 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1633 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1634 * \retval PSA_ERROR_HARDWARE_FAILURE
1635 * \retval PSA_ERROR_TAMPERING_DETECTED
1636 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01001637 */
1638psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
1639 psa_algorithm_t alg,
1640 const uint8_t *hash,
1641 size_t hash_length,
1642 const uint8_t *salt,
1643 size_t salt_length,
1644 uint8_t *signature,
1645 size_t signature_size,
1646 size_t *signature_length);
1647
1648/**
1649 * \brief Verify the signature a hash or short message using a public key.
1650 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001651 * \param key Key slot containing a public key or an
1652 * asymmetric key pair.
1653 * \param alg A signature algorithm that is compatible with
1654 * the type of \c key.
1655 * \param hash The message whose signature is to be verified.
1656 * \param hash_length Size of the \c hash buffer in bytes.
1657 * \param salt A salt or label, if supported by the signature
1658 * algorithm.
1659 * If the signature algorithm does not support a
1660 * salt, pass \c NULL.
1661 * If the signature algorithm supports an optional
1662 * salt and you do not want to pass a salt,
1663 * pass \c NULL.
1664 * \param salt_length Size of the \c salt buffer in bytes.
1665 * If \c salt is \c NULL, pass 0.
1666 * \param signature Buffer containing the signature to verify.
1667 * \param signature_size Size of the \c signature buffer in bytes.
1668 *
1669 * \retval PSA_SUCCESS
1670 * The signature is valid.
1671 * \retval PSA_ERROR_INVALID_SIGNATURE
1672 * The calculation was perfomed successfully, but the passed
1673 * signature is not a valid signature.
1674 * \retval PSA_ERROR_NOT_SUPPORTED
1675 * \retval PSA_ERROR_INVALID_ARGUMENT
1676 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1677 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1678 * \retval PSA_ERROR_HARDWARE_FAILURE
1679 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01001680 */
1681psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
1682 psa_algorithm_t alg,
1683 const uint8_t *hash,
1684 size_t hash_length,
1685 const uint8_t *salt,
1686 size_t salt_length,
1687 uint8_t *signature,
1688 size_t signature_size);
1689
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001690#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine06297932018-04-11 16:58:22 +02001691 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
1692 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
1693 0)
Gilles Peskine723feff2018-05-31 20:08:13 +02001694#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
1695 (PSA_ALG_IS_RSA_OAEP_MGF1(alg) ? \
1696 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_GET_HASH(alg)) + 1 : \
1697 11 /*PKCS#1v1.5*/)
1698#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine06297932018-04-11 16:58:22 +02001699 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
Gilles Peskine723feff2018-05-31 20:08:13 +02001700 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
Gilles Peskine06297932018-04-11 16:58:22 +02001701 0)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001702
1703/**
1704 * \brief Encrypt a short message with a public key.
1705 *
1706 * \param key Key slot containing a public key or an asymmetric
1707 * key pair.
1708 * \param alg An asymmetric encryption algorithm that is
1709 * compatible with the type of \c key.
1710 * \param input The message to encrypt.
1711 * \param input_length Size of the \c input buffer in bytes.
1712 * \param salt A salt or label, if supported by the encryption
1713 * algorithm.
1714 * If the algorithm does not support a
1715 * salt, pass \c NULL.
1716 * If the algorithm supports an optional
1717 * salt and you do not want to pass a salt,
1718 * pass \c NULL.
1719 *
1720 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1721 * supported.
1722 * \param salt_length Size of the \c salt buffer in bytes.
1723 * If \c salt is \c NULL, pass 0.
1724 * \param output Buffer where the encrypted message is to be written.
1725 * \param output_size Size of the \c output buffer in bytes.
1726 * \param output_length On success, the number of bytes
1727 * that make up the returned output.
1728 *
1729 * \retval PSA_SUCCESS
1730 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1731 * The size of the \c output buffer is too small. You can
1732 * determine a sufficient buffer size by calling
1733 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1734 * where \c key_type and \c key_bits are the type and bit-size
1735 * respectively of \c key.
1736 * \retval PSA_ERROR_NOT_SUPPORTED
1737 * \retval PSA_ERROR_INVALID_ARGUMENT
1738 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1739 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1740 * \retval PSA_ERROR_HARDWARE_FAILURE
1741 * \retval PSA_ERROR_TAMPERING_DETECTED
1742 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1743 */
1744psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
1745 psa_algorithm_t alg,
1746 const uint8_t *input,
1747 size_t input_length,
1748 const uint8_t *salt,
1749 size_t salt_length,
1750 uint8_t *output,
1751 size_t output_size,
1752 size_t *output_length);
1753
1754/**
1755 * \brief Decrypt a short message with a private key.
1756 *
1757 * \param key Key slot containing an asymmetric key pair.
1758 * \param alg An asymmetric encryption algorithm that is
1759 * compatible with the type of \c key.
1760 * \param input The message to decrypt.
1761 * \param input_length Size of the \c input buffer in bytes.
1762 * \param salt A salt or label, if supported by the encryption
1763 * algorithm.
1764 * If the algorithm does not support a
1765 * salt, pass \c NULL.
1766 * If the algorithm supports an optional
1767 * salt and you do not want to pass a salt,
1768 * pass \c NULL.
1769 *
1770 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1771 * supported.
1772 * \param salt_length Size of the \c salt buffer in bytes.
1773 * If \c salt is \c NULL, pass 0.
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001774 * \param output Buffer where the decrypted message is to be written.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001775 * \param output_size Size of the \c output buffer in bytes.
1776 * \param output_length On success, the number of bytes
1777 * that make up the returned output.
1778 *
1779 * \retval PSA_SUCCESS
1780 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1781 * The size of the \c output buffer is too small. You can
1782 * determine a sufficient buffer size by calling
1783 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1784 * where \c key_type and \c key_bits are the type and bit-size
1785 * respectively of \c key.
1786 * \retval PSA_ERROR_NOT_SUPPORTED
1787 * \retval PSA_ERROR_INVALID_ARGUMENT
1788 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1789 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1790 * \retval PSA_ERROR_HARDWARE_FAILURE
1791 * \retval PSA_ERROR_TAMPERING_DETECTED
1792 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1793 * \retval PSA_ERROR_INVALID_PADDING
1794 */
1795psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
1796 psa_algorithm_t alg,
1797 const uint8_t *input,
1798 size_t input_length,
1799 const uint8_t *salt,
1800 size_t salt_length,
1801 uint8_t *output,
1802 size_t output_size,
1803 size_t *output_length);
1804
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001805/**@}*/
1806
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001807/** \defgroup generation Key generation
1808 * @{
1809 */
1810
1811/**
1812 * \brief Generate random bytes.
1813 *
1814 * \warning This function **can** fail! Callers MUST check the return status
1815 * and MUST NOT use the content of the output buffer if the return
1816 * status is not #PSA_SUCCESS.
1817 *
1818 * \note To generate a key, use psa_generate_key() instead.
1819 *
1820 * \param output Output buffer for the generated data.
1821 * \param output_size Number of bytes to generate and output.
1822 *
1823 * \retval PSA_SUCCESS
1824 * \retval PSA_ERROR_NOT_SUPPORTED
1825 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1826 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1827 * \retval PSA_ERROR_HARDWARE_FAILURE
1828 * \retval PSA_ERROR_TAMPERING_DETECTED
1829 */
1830psa_status_t psa_generate_random(uint8_t *output,
1831 size_t output_size);
1832
1833/**
1834 * \brief Generate a key or key pair.
1835 *
1836 * \param key Slot where the key will be stored. This must be a
1837 * valid slot for a key of the chosen type. It must
1838 * be unoccupied.
1839 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
1840 * \param bits Key size in bits.
1841 * \param parameters Extra parameters for key generation. The interpretation
1842 * of this parameter depends on \c type. All types support
1843 * \c NULL to use default parameters specified below.
1844 *
1845 * For any symmetric key type (type such that
1846 * `PSA_KEY_TYPE_IS_ASYMMETRIC(type)` is false), \c parameters must be
1847 * \c NULL. For asymmetric key types defined by this specification,
1848 * the parameter type and the default parameters are defined by the
1849 * table below. For vendor-defined key types, the vendor documentation
1850 * shall define the parameter type and the default parameters.
1851 *
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001852 * Type | Parameter type | Meaning | Parameters used if `parameters == NULL`
1853 * ---- | -------------- | ------- | ---------------------------------------
1854 * `PSA_KEY_TYPE_RSA_KEYPAIR` | `unsigned int` | Public exponent | 65537
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001855 *
1856 * \retval PSA_SUCCESS
1857 * \retval PSA_ERROR_NOT_SUPPORTED
1858 * \retval PSA_ERROR_INVALID_ARGUMENT
1859 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1860 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1861 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1862 * \retval PSA_ERROR_HARDWARE_FAILURE
1863 * \retval PSA_ERROR_TAMPERING_DETECTED
1864 */
1865psa_status_t psa_generate_key(psa_key_slot_t key,
1866 psa_key_type_t type,
1867 size_t bits,
1868 const void *parameters);
1869
1870/**@}*/
1871
Gilles Peskinee59236f2018-01-27 23:32:46 +01001872#ifdef __cplusplus
1873}
1874#endif
1875
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001876/* The file "crypto_struct.h" contains definitions for
1877 * implementation-specific structs that are declared above. */
1878#include "crypto_struct.h"
1879
1880/* The file "crypto_extra.h" contains vendor-specific definitions. This
1881 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01001882#include "crypto_extra.h"
1883
1884#endif /* PSA_CRYPTO_H */