blob: 28103c78bad2fe9315a463768dfaa988552a43fd [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
5
6#ifndef PSA_CRYPTO_H
7#define PSA_CRYPTO_H
8
9#include "crypto_platform.h"
10
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010011#include <stddef.h>
12
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010013#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010014/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
15 * must be defined in the crypto_platform.h header. These mock definitions
16 * are present in this file as a convenience to generate pretty-printed
17 * documentation that includes those definitions. */
18
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010019/** \defgroup platform Implementation-specific definitions
20 * @{
21 */
22
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010023/** \brief Key slot number.
24 *
25 * This type represents key slots. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010026 * type. The choice of type is implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027 * 0 is not a valid key slot number. The meaning of other values is
28 * implementation dependent.
29 *
30 * At any given point in time, each key slot either contains a
31 * cryptographic object, or is empty. Key slots are persistent:
32 * once set, the cryptographic object remains in the key slot until
33 * explicitly destroyed.
34 */
35typedef _unsigned_integral_type_ psa_key_slot_t;
36
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010037/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010038#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010039
Gilles Peskinee59236f2018-01-27 23:32:46 +010040#ifdef __cplusplus
41extern "C" {
42#endif
43
44/** \defgroup basic Basic definitions
45 * @{
46 */
47
48/**
49 * \brief Function return status.
50 *
51 * Zero indicates success, anything else indicates an error.
52 */
53typedef enum {
54 /** The action was completed successfully. */
55 PSA_SUCCESS = 0,
56 /** The requested operation or a parameter is not supported
Gilles Peskine65eb8582018-04-19 08:28:58 +020057 * by this implementation.
58 *
59 * Implementations should return this error code when an enumeration
60 * parameter such as a key type, algorithm, etc. is not recognized.
61 * If a combination of parameters is recognized and identified as
62 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
Gilles Peskinee59236f2018-01-27 23:32:46 +010063 PSA_ERROR_NOT_SUPPORTED,
Gilles Peskine65eb8582018-04-19 08:28:58 +020064 /** The requested action is denied by a policy.
65 *
66 * Implementations should return this error code when the parameters
67 * are recognized as valid and supported, and a policy explicitly
68 * denies the requested operation.
69 *
70 * If a subset of the parameters of a function call identify a
71 * forbidden operation, and another subset of the parameters are
72 * not valid or not supported, it is unspecified whether the function
73 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
74 * #PSA_ERROR_INVALID_ARGUMENT. */
Gilles Peskinee59236f2018-01-27 23:32:46 +010075 PSA_ERROR_NOT_PERMITTED,
Gilles Peskine65eb8582018-04-19 08:28:58 +020076 /** An output buffer is too small.
77 *
78 * Applications can call the `PSA_xxx_SIZE` macro listed in the function
79 * description to determine a sufficient buffer size.
80 *
81 * Implementations should preferably return this error code only
82 * in cases when performing the operation with a larger output
83 * buffer would succeed. However implementations may return this
84 * error if a function has invalid or unsupported parameters in addition
85 * to the parameters that determine the necessary output buffer size. */
Gilles Peskinee59236f2018-01-27 23:32:46 +010086 PSA_ERROR_BUFFER_TOO_SMALL,
87 /** A slot is occupied, but must be empty to carry out the
Gilles Peskine65eb8582018-04-19 08:28:58 +020088 * requested action.
89 *
90 * If the slot number is invalid (i.e. the requested action could
91 * not be performed even after erasing the slot's content),
92 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
Gilles Peskinee59236f2018-01-27 23:32:46 +010093 PSA_ERROR_OCCUPIED_SLOT,
94 /** A slot is empty, but must be occupied to carry out the
Gilles Peskine65eb8582018-04-19 08:28:58 +020095 * requested action.
96 *
97 * If the slot number is invalid (i.e. the requested action could
98 * not be performed even after creating appropriate content in the slot),
99 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
Gilles Peskinee59236f2018-01-27 23:32:46 +0100100 PSA_ERROR_EMPTY_SLOT,
Gilles Peskine65eb8582018-04-19 08:28:58 +0200101 /** The requested action cannot be performed in the current state.
102 *
103 * Multipart operations return this error when one of the
104 * functions is called out of sequence. Refer to the function
105 * descriptions for permitted sequencing of functions.
106 *
107 * Implementations shall not return this error code to indicate
108 * that a key slot is occupied when it needs to be free or vice versa,
109 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
110 * as applicable. */
Gilles Peskinee59236f2018-01-27 23:32:46 +0100111 PSA_ERROR_BAD_STATE,
Gilles Peskine65eb8582018-04-19 08:28:58 +0200112 /** The parameters passed to the function are invalid.
113 *
114 * Implementations may return this error any time a parameter or
115 * combination of parameters are recognized as invalid.
116 *
117 * Implementations shall not return this error code to indicate
118 * that a key slot is occupied when it needs to be free or vice versa,
119 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
120 * as applicable. */
Gilles Peskinee59236f2018-01-27 23:32:46 +0100121 PSA_ERROR_INVALID_ARGUMENT,
Gilles Peskine65eb8582018-04-19 08:28:58 +0200122 /** There is not enough runtime memory.
123 *
124 * If the action is carried out across multiple security realms, this
125 * error can refer to available memory in any of the security realms. */
Gilles Peskinee59236f2018-01-27 23:32:46 +0100126 PSA_ERROR_INSUFFICIENT_MEMORY,
Gilles Peskine65eb8582018-04-19 08:28:58 +0200127 /** There is not enough persistent storage.
128 *
129 * Functions that modify the key storage return this error code if
130 * there is insufficient storage space on the host media. In addition,
131 * many functions that do not otherwise access storage may return this
132 * error code if the implementation requires a mandatory log entry for
133 * the requested action and the log storage space is full. */
Gilles Peskinee59236f2018-01-27 23:32:46 +0100134 PSA_ERROR_INSUFFICIENT_STORAGE,
Gilles Peskine65eb8582018-04-19 08:28:58 +0200135 /** There was a communication failure inside the implementation.
136 *
137 * This can indicate a communication failure between the application
138 * and an external cryptoprocessor or between the cryptoprocessor and
139 * an external volatile or persistent memory. A communication failure
140 * may be transient or permanent depending on the cause.
141 *
142 * \warning If a function returns this error, it is undetermined
143 * whether the requested action has completed or not. Implementations
144 * should return #PSA_SUCCESS on successful completion whenver
145 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
146 * if the requested action was completed successfully in an external
147 * cryptoprocessor but there was a breakdown of communication before
148 * the cryptoprocessor could report the status to the application.
149 */
Gilles Peskinee59236f2018-01-27 23:32:46 +0100150 PSA_ERROR_COMMUNICATION_FAILURE,
Gilles Peskine65eb8582018-04-19 08:28:58 +0200151 /** There was a storage failure that may have led to data loss.
152 *
153 * This error indicates that some persistent storage is corrupted.
154 * It should not be used for a corruption of volatile memory
155 * (use #PSA_ERROR_TAMPERING_DETECTED), for a communication error
156 * between the cryptoprocessor and its external storage (use
157 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
158 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
159 *
160 * Note that a storage failure does not indicate that any data that was
161 * previously read is invalid. However this previously read data may no
162 * longer be readable from storage.
163 *
164 * When a storage failure occurs, it is no longer possible to ensure
165 * the global integrity of the keystore. Depending on the global
166 * integrity guarantees offered by the implementation, access to other
167 * data may or may not fail even if the data is still readable but
168 * its integrity canont be guaranteed.
169 *
170 * Implementations should only use this error code to report a
171 * permanent storage corruption. However application writers should
172 * keep in mind that transient errors while reading the storage may be
173 * reported using this error code. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 PSA_ERROR_STORAGE_FAILURE,
Gilles Peskine65eb8582018-04-19 08:28:58 +0200175 /** A hardware failure was detected.
176 *
177 * A hardware failure may be transient or permanent depending on the
178 * cause. */
Gilles Peskinee59236f2018-01-27 23:32:46 +0100179 PSA_ERROR_HARDWARE_FAILURE,
Gilles Peskine65eb8582018-04-19 08:28:58 +0200180 /** A tampering attempt was detected.
181 *
182 * If an application receives this error code, there is no guarantee
183 * that previously accessed or computed data was correct and remains
184 * confidential. Applications should not perform any security function
185 * and should enter a safe failure state.
186 *
187 * Implementations may return this error code if they detect an invalid
188 * state that cannot happen during normal operation and that indicates
189 * that the implementation's security guarantees no longer hold. Depending
190 * on the implementation architecture and on its security and safety goals,
191 * the implementation may forcibly terminate the application.
192 *
193 * This error code is intended as a last resort when a security breach
194 * is detected and it is unsure whether the keystore data is still
195 * protected. Implementations shall only return this error code
196 * to report an alarm from a tampering detector, to indicate that
197 * the confidentiality of stored data can no longer be guaranteed,
198 * or to indicate that the integrity of previously returned data is now
199 * considered compromised. Implementations shall not use this error code
200 * to indicate a hardware failure that merely makes it impossible to
201 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
202 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
203 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
204 * instead).
205 *
206 * This error indicates an attack against the application. Implementations
207 * shall not return this error code as a consequence of the behavior of
208 * the application itself. */
Gilles Peskinee59236f2018-01-27 23:32:46 +0100209 PSA_ERROR_TAMPERING_DETECTED,
210 /** There is not enough entropy to generate random data needed
Gilles Peskine65eb8582018-04-19 08:28:58 +0200211 * for the requested action.
212 *
213 * This error indicates a failure of a hardware random generator.
214 * Application writers should note that this error can be returned not
215 * only by functions whose purpose is to generate random data, such
216 * as key, IV or nonce generation, but also by functions that execute
217 * an algorithm with a randomized result, as well as functions that
218 * use randomization of intermediate computations as a countermeasure
219 * to certain attacks.
220 *
221 * Implementations should avoid returning this error after psa_crypto_init()
222 * has succeeded. Implementations should generate sufficient
223 * entropy during initialization and subsequently use a cryptographically
224 * secure pseudorandom generator (PRNG). However implementations may return
225 * this error at any time if a policy requires the PRNG to be reseeded
226 * during normal operation. */
Gilles Peskinee59236f2018-01-27 23:32:46 +0100227 PSA_ERROR_INSUFFICIENT_ENTROPY,
Gilles Peskine65eb8582018-04-19 08:28:58 +0200228 /** The signature, MAC or hash is incorrect.
229 *
230 * Verification functions return this error if the verification
231 * calculations completed successfully, and the value to be verified
232 * was determined to be incorrect.
233 *
234 * If the value to verify has an invalid size, implementations may return
235 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
Gilles Peskinee59236f2018-01-27 23:32:46 +0100236 PSA_ERROR_INVALID_SIGNATURE,
Gilles Peskine65eb8582018-04-19 08:28:58 +0200237 /** The decrypted padding is incorrect.
238 *
239 * \warning In some protocols, when decrypting data, it is essential that
240 * the behavior of the application does not depend on whether the padding
241 * is correct, down to precise timing. Applications should prefer
242 * protocols that use authenticated encryption rather than plain
243 * encryption. If the application must perform a decryption of
244 * unauthenticated data, the application writer should take care not
245 * to reveal whether the padding is invalid.
246 *
247 * Implementations should strive to make valid and invalid padding
248 * as close as possible to indistinguishable to an external observer.
249 * In particular, the timing of a decryption operation should not
250 * depend on the validity of the padding. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100251 PSA_ERROR_INVALID_PADDING,
Gilles Peskinee59236f2018-01-27 23:32:46 +0100252 /** An error occurred that does not correspond to any defined
Gilles Peskine65eb8582018-04-19 08:28:58 +0200253 * failure cause.
254 *
255 * Implementations may use this error code if none of the other standard
256 * error codes are applicable. */
Gilles Peskinee59236f2018-01-27 23:32:46 +0100257 PSA_ERROR_UNKNOWN_ERROR,
258} psa_status_t;
259
260/**
261 * \brief Library initialization.
262 *
263 * Applications must call this function before calling any other
264 * function in this module.
265 *
266 * Applications may call this function more than once. Once a call
267 * succeeds, subsequent calls are guaranteed to succeed.
268 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100269 * \retval PSA_SUCCESS
270 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
271 * \retval PSA_ERROR_COMMUNICATION_FAILURE
272 * \retval PSA_ERROR_HARDWARE_FAILURE
273 * \retval PSA_ERROR_TAMPERING_DETECTED
274 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +0100275 */
276psa_status_t psa_crypto_init(void);
277
Gilles Peskine2905a7a2018-03-07 16:39:31 +0100278#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
279#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
Gilles Peskine0189e752018-02-03 23:57:22 +0100280
Gilles Peskinee59236f2018-01-27 23:32:46 +0100281/**@}*/
282
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100283/** \defgroup crypto_types Key and algorithm types
284 * @{
285 */
286
Gilles Peskine308b91d2018-02-08 09:47:44 +0100287/** \brief Encoding of a key type.
288 */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100289typedef uint32_t psa_key_type_t;
290
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100291/** An invalid key type value.
292 *
293 * Zero is not the encoding of any key type.
294 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100295#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100296
297/** Vendor-defined flag
298 *
299 * Key types defined by this standard will never have the
300 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
301 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
302 * respect the bitwise structure used by standard encodings whenever practical.
303 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100304#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100305
Gilles Peskine98f0a242018-02-06 18:57:29 +0100306#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7e000000)
Gilles Peskine35855962018-04-19 08:39:16 +0200307/** Raw data.
308 *
309 * A "key" of this type cannot be used for any cryptographic operation.
310 * Applications may use this type to store arbitrary data in the keystore. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100311#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x02000000)
312#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x04000000)
313#define PSA_KEY_TYPE_CATEGORY_ASYMMETRIC ((psa_key_type_t)0x06000000)
314#define PSA_KEY_TYPE_PAIR_FLAG ((psa_key_type_t)0x01000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100315
Gilles Peskine35855962018-04-19 08:39:16 +0200316/** HMAC key.
317 *
318 * The key policy determines which underlying hash algorithm the key can be
319 * used for.
320 *
321 * HMAC keys should generally have the same size as the underlying hash.
322 * This size can be calculated with `PSA_HASH_SIZE(alg)` where
323 * `alg` is the HMAC algorithm or the underlying hash algorithm. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100324#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x02000001)
Gilles Peskine35855962018-04-19 08:39:16 +0200325/** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher.
326 *
327 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
328 * 32 bytes (AES-256).
329 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100330#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x04000001)
Gilles Peskine35855962018-04-19 08:39:16 +0200331/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
332 *
333 * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or
334 * 24 bytes (3-key 3DES).
335 *
336 * Note that single DES and 2-key 3DES are weak and strongly
337 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
338 * is weak and deprecated and should only be used in legacy protocols.
339 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100340#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x04000002)
Gilles Peskine35855962018-04-19 08:39:16 +0200341/** Key for an cipher, AEAD or MAC algorithm based on the
342 * Camellia block cipher. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100343#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x04000003)
Gilles Peskine35855962018-04-19 08:39:16 +0200344/** Key for the RC4 stream cipher.
345 *
346 * Note that RC4 is weak and deprecated and should only be used in
347 * legacy protocols. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100348#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x04000004)
349
Gilles Peskine308b91d2018-02-08 09:47:44 +0100350/** RSA public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100351#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x06010000)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100352/** RSA key pair (private and public key). */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100353#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x07010000)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100354/** DSA public key. */
355#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x06020000)
356/** DSA key pair (private and public key). */
357#define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x07020000)
358#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x06030000)
359#define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x07030000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100360#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100361#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
362 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
363#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
364 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
Gilles Peskine98f0a242018-02-06 18:57:29 +0100365
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100366/** Whether a key type is vendor-defined. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100367#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100368 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100369#define PSA_KEY_TYPE_IS_RAW_BYTES(type) \
370 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_RAW_DATA || \
371 ((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100372
373/** Whether a key type is asymmetric: either a key pair or a public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100374#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
375 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100376/** Whether a key type is the public part of a key pair. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100377#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
Moran Pekerb4d0ddd2018-04-04 12:47:52 +0300378 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
379 PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100380/** Whether a key type is a key pair containing a private part and a public
381 * part. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100382#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
383 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
384 (PSA_KEY_TYPE_CATEGORY_ASYMMETRIC | PSA_KEY_TYPE_PAIR_FLAG))
Gilles Peskine06dc2632018-03-08 07:47:25 +0100385/** Whether a key type is an RSA key pair or public key. */
386/** The key pair type corresponding to a public key type. */
387#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
388 ((type) | PSA_KEY_TYPE_PAIR_FLAG)
389/** The public key type corresponding to a key pair type. */
390#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
391 ((type) & ~PSA_KEY_TYPE_PAIR_FLAG)
Gilles Peskine0189e752018-02-03 23:57:22 +0100392#define PSA_KEY_TYPE_IS_RSA(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100393 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
394/** Whether a key type is an elliptic curve key pair or public key. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100395#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100396 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
397 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100398
Gilles Peskinee1fed0d2018-06-18 20:45:45 +0200399/** The type of PSA elliptic curve identifiers. */
400typedef uint16_t psa_ecc_curve_t;
401/** Extract the curve from an elliptic curve key type. */
402#define PSA_KEY_TYPE_GET_CURVE(type) \
403 ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
404 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
405 0))
406
407/* The encoding of curve identifiers is currently aligned with the
408 * TLS Supported Groups Registry (formerly known as the
409 * TLS EC Named Curve Registry)
410 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
411 * The values are defined by RFC 4492, RFC 7027 and RFC 7919. */
412#define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001)
413#define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002)
414#define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003)
415#define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004)
416#define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005)
417#define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006)
418#define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007)
419#define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008)
420#define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009)
421#define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a)
422#define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b)
423#define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c)
424#define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d)
425#define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e)
426#define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f)
427#define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010)
428#define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011)
429#define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012)
430#define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013)
431#define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014)
432#define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015)
433#define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016)
434#define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017)
435#define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018)
436#define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019)
437#define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a)
438#define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b)
439#define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c)
440#define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d)
441#define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e)
442#define PSA_ECC_CURVE_FFDHE_2048 ((psa_ecc_curve_t) 0x0100)
443#define PSA_ECC_CURVE_FFDHE_3072 ((psa_ecc_curve_t) 0x0101)
444#define PSA_ECC_CURVE_FFDHE_4096 ((psa_ecc_curve_t) 0x0102)
445#define PSA_ECC_CURVE_FFDHE_6144 ((psa_ecc_curve_t) 0x0103)
446#define PSA_ECC_CURVE_FFDHE_8192 ((psa_ecc_curve_t) 0x0104)
447
Gilles Peskine7e198532018-03-08 07:50:30 +0100448/** The block size of a block cipher.
449 *
450 * \param type A cipher key type (value of type #psa_key_type_t).
451 *
452 * \return The block size for a block cipher, or 1 for a stream cipher.
Gilles Peskine35855962018-04-19 08:39:16 +0200453 * The return value is undefined if \c type is not a supported
454 * cipher key type.
455 *
456 * \note It is possible to build stream cipher algorithms on top of a block
457 * cipher, for example CTR mode (#PSA_ALG_CTR).
458 * This macro only takes the key type into account, so it cannot be
459 * used to determine the size of the data that #psa_cipher_update()
460 * might buffer for future processing in general.
Gilles Peskine7e198532018-03-08 07:50:30 +0100461 *
462 * \note This macro returns a compile-time constant if its argument is one.
463 *
464 * \warning This macro may evaluate its argument multiple times.
465 */
Gilles Peskine03182e92018-03-07 16:40:52 +0100466#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100467 ( \
468 (type) == PSA_KEY_TYPE_AES ? 16 : \
469 (type) == PSA_KEY_TYPE_DES ? 8 : \
470 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
Gilles Peskine7e198532018-03-08 07:50:30 +0100471 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100472 0)
473
Gilles Peskine308b91d2018-02-08 09:47:44 +0100474/** \brief Encoding of a cryptographic algorithm.
475 *
476 * For algorithms that can be applied to multiple key types, this type
477 * does not encode the key type. For example, for symmetric ciphers
478 * based on a block cipher, #psa_algorithm_t encodes the block cipher
479 * mode and the padding mode while the block cipher itself is encoded
480 * via #psa_key_type_t.
481 */
Gilles Peskine20035e32018-02-03 22:44:14 +0100482typedef uint32_t psa_algorithm_t;
483
Gilles Peskine98f0a242018-02-06 18:57:29 +0100484#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
485#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
486#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
487#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
488#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
489#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
490#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
491#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
492#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
493#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100494
Gilles Peskine98f0a242018-02-06 18:57:29 +0100495#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
496 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100497/** Whether the specified algorithm is a hash algorithm.
498 *
Gilles Peskine7e198532018-03-08 07:50:30 +0100499 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100500 *
501 * \return 1 if \c alg is a hash algorithm, 0 otherwise.
502 * This macro may return either 0 or 1 if \c alg is not a valid
Gilles Peskine7e198532018-03-08 07:50:30 +0100503 * algorithm identifier.
504 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100505#define PSA_ALG_IS_HASH(alg) \
506 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
507#define PSA_ALG_IS_MAC(alg) \
508 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
509#define PSA_ALG_IS_CIPHER(alg) \
510 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
511#define PSA_ALG_IS_AEAD(alg) \
512 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
513#define PSA_ALG_IS_SIGN(alg) \
514 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
515#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
516 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
517#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
518 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
519#define PSA_ALG_IS_KEY_DERIVATION(alg) \
520 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
521
522#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
523#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
524#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
525#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
Gilles Peskinee3f694f2018-03-08 07:48:40 +0100526#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
527#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100528#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
529#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
530#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
531#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
532#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
533#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
534#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
535#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
536#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
537#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
538
Gilles Peskine8c9def32018-02-08 10:02:12 +0100539#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100540#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
Gilles Peskine35855962018-04-19 08:39:16 +0200541/** Macro to build an HMAC algorithm.
542 *
543 * For example, `PSA_ALG_HMAC(PSA_ALG_SHA256)` is HMAC-SHA-256.
544 *
545 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
546 * #PSA_ALG_IS_HASH(alg) is true).
547 *
548 * \return The corresponding HMAC algorithm.
549 * \return Unspecified if \p alg is not a hash algorithm.
550 */
551#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100552 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
553#define PSA_ALG_HMAC_HASH(hmac_alg) \
554 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
555#define PSA_ALG_IS_HMAC(alg) \
556 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
557 PSA_ALG_HMAC_BASE)
558#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
559#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
560#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
561#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
562#define PSA_ALG_IS_CIPHER_MAC(alg) \
563 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
564 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100565
Gilles Peskine8c9def32018-02-08 10:02:12 +0100566#define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100567#define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100568#define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100569#define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000)
570#define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100571#define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100572#define PSA_ALG_IS_BLOCK_CIPHER(alg) \
573 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
574 PSA_ALG_BLOCK_CIPHER_BASE)
575
Gilles Peskine98f0a242018-02-06 18:57:29 +0100576#define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100577#define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002)
578#define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003)
579#define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100580#define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t)0x04800000)
581#define PSA_ALG_CTR ((psa_algorithm_t)0x04800001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100582#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100583
Moran Pekerbed71a22018-04-22 20:19:20 +0300584#define PSA_ALG_IS_STREAM_CIPHER(alg) \
585 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
586 PSA_ALG_STREAM_CIPHER)
587
Gilles Peskine8c9def32018-02-08 10:02:12 +0100588#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
589#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100590
Gilles Peskinea5926232018-03-28 14:16:50 +0200591#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW ((psa_algorithm_t)0x10010000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100592#define PSA_ALG_RSA_PSS_MGF1 ((psa_algorithm_t)0x10020000)
Gilles Peskine6944f9a2018-03-28 14:18:39 +0200593#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12010000)
594#define PSA_ALG_RSA_OAEP_MGF1_BASE ((psa_algorithm_t)0x12020000)
Gilles Peskinea5926232018-03-28 14:16:50 +0200595#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
596 (PSA_ALG_RSA_PKCS1V15_SIGN_RAW | ((hash_alg) & PSA_ALG_HASH_MASK))
597#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Gilles Peskine9673cc82018-04-11 16:57:49 +0200598 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_RAW)
599#define PSA_ALG_RSA_OAEP_MGF1(hash_alg) \
600 (PSA_ALG_RSA_OAEP_MGF1_RAW | ((hash_alg) & PSA_ALG_HASH_MASK))
601#define PSA_ALG_IS_RSA_OAEP_MGF1(alg) \
Gilles Peskine625b01c2018-06-08 17:43:16 +0200602 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_MGF1_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100603#define PSA_ALG_RSA_GET_HASH(alg) \
604 (((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100605
Gilles Peskined1e8e412018-06-07 09:49:39 +0200606#define PSA_ALG_ECDSA_RAW ((psa_algorithm_t)0x10030000)
607
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100608/**@}*/
609
610/** \defgroup key_management Key management
611 * @{
612 */
613
614/**
615 * \brief Import a key in binary format.
616 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100617 * This function supports any output from psa_export_key(). Refer to the
618 * documentation of psa_export_key() for the format for each key type.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100619 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100620 * \param key Slot where the key will be stored. This must be a
621 * valid slot for a key of the chosen type. It must
622 * be unoccupied.
623 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
624 * \param data Buffer containing the key data.
625 * \param data_length Size of the \c data buffer in bytes.
626 *
627 * \retval PSA_SUCCESS
628 * Success.
629 * \retval PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200630 * The key type or key size is not supported, either by the
631 * implementation in general or in this particular slot.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100632 * \retval PSA_ERROR_INVALID_ARGUMENT
633 * The key slot is invalid,
634 * or the key data is not correctly formatted.
635 * \retval PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +0200636 * There is already a key in the specified slot.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100637 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine65eb8582018-04-19 08:28:58 +0200638 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100639 * \retval PSA_ERROR_COMMUNICATION_FAILURE
640 * \retval PSA_ERROR_HARDWARE_FAILURE
641 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100642 */
643psa_status_t psa_import_key(psa_key_slot_t key,
644 psa_key_type_t type,
645 const uint8_t *data,
646 size_t data_length);
647
648/**
Gilles Peskine154bd952018-04-19 08:38:16 +0200649 * \brief Destroy a key and restore the slot to its default state.
650 *
651 * This function destroys the content of the key slot from both volatile
652 * memory and, if applicable, non-volatile storage. Implementations shall
653 * make a best effort to ensure that any previous content of the slot is
654 * unrecoverable.
655 *
656 * This function also erases any metadata such as policies. It returns the
657 * specified slot to its default state.
658 *
659 * \param key The key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100660 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100661 * \retval PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200662 * The slot's content, if any, has been erased.
663 * \retval PSA_ERROR_NOT_PERMITTED
664 * The slot holds content and cannot be erased because it is
665 * read-only, either due to a policy or due to physical restrictions.
666 * \retval PSA_ERROR_INVALID_ARGUMENT
667 * The specified slot number does not designate a valid slot.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100668 * \retval PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200669 * There was an failure in communication with the cryptoprocessor.
670 * The key material may still be present in the cryptoprocessor.
671 * \retval PSA_ERROR_STORAGE_FAILURE
672 * The storage is corrupted. Implementations shall make a best effort
673 * to erase key material even in this stage, however applications
674 * should be aware that it may be impossible to guarantee that the
675 * key material is not recoverable in such cases.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100676 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200677 * An unexpected condition which is not a storage corruption or
678 * a communication failure occurred. The cryptoprocessor may have
679 * been compromised.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100680 */
681psa_status_t psa_destroy_key(psa_key_slot_t key);
682
683/**
684 * \brief Get basic metadata about a key.
685 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100686 * \param key Slot whose content is queried. This must
687 * be an occupied key slot.
688 * \param type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
689 * This may be a null pointer, in which case the key type
690 * is not written.
691 * \param bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100692 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100693 * is not written.
694 *
695 * \retval PSA_SUCCESS
696 * \retval PSA_ERROR_EMPTY_SLOT
697 * \retval PSA_ERROR_COMMUNICATION_FAILURE
698 * \retval PSA_ERROR_HARDWARE_FAILURE
699 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100700 */
701psa_status_t psa_get_key_information(psa_key_slot_t key,
702 psa_key_type_t *type,
703 size_t *bits);
704
705/**
706 * \brief Export a key in binary format.
707 *
708 * The output of this function can be passed to psa_import_key() to
709 * create an equivalent object.
710 *
711 * If a key is created with psa_import_key() and then exported with
712 * this function, it is not guaranteed that the resulting data is
713 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +0100714 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100715 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100716 * For standard key types, the output format is as follows:
717 *
718 * - For symmetric keys (including MAC keys), the format is the
719 * raw bytes of the key.
720 * - For DES, the key data consists of 8 bytes. The parity bits must be
721 * correct.
722 * - For Triple-DES, the format is the concatenation of the
723 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100724 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine308b91d2018-02-08 09:47:44 +0100725 * is the non-encrypted DER representation defined by PKCS\#8 (RFC 5208)
726 * as PrivateKeyInfo.
727 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
Gilles Peskine971f7062018-03-20 17:52:58 +0100728 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100729 *
730 * \param key Slot whose content is to be exported. This must
731 * be an occupied key slot.
732 * \param data Buffer where the key data is to be written.
733 * \param data_size Size of the \c data buffer in bytes.
734 * \param data_length On success, the number of bytes
735 * that make up the key data.
736 *
737 * \retval PSA_SUCCESS
738 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine92b30732018-03-03 21:29:30 +0100739 * \retval PSA_ERROR_NOT_PERMITTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100740 * \retval PSA_ERROR_COMMUNICATION_FAILURE
741 * \retval PSA_ERROR_HARDWARE_FAILURE
742 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743 */
744psa_status_t psa_export_key(psa_key_slot_t key,
745 uint8_t *data,
746 size_t data_size,
747 size_t *data_length);
748
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100749/**
750 * \brief Export a public key or the public part of a key pair in binary format.
751 *
752 * The output of this function can be passed to psa_import_key() to
753 * create an object that is equivalent to the public key.
754 *
755 * For standard key types, the output format is as follows:
756 *
757 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
Moran Pekerdd4ea382018-04-03 15:30:03 +0300758 * the format is the DER representation of the public key defined by RFC 5280
Gilles Peskine971f7062018-03-20 17:52:58 +0100759 * as SubjectPublicKeyInfo.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100760 *
761 * \param key Slot whose content is to be exported. This must
762 * be an occupied key slot.
763 * \param data Buffer where the key data is to be written.
764 * \param data_size Size of the \c data buffer in bytes.
765 * \param data_length On success, the number of bytes
766 * that make up the key data.
767 *
768 * \retval PSA_SUCCESS
769 * \retval PSA_ERROR_EMPTY_SLOT
770 * \retval PSA_ERROR_INVALID_ARGUMENT
771 * \retval PSA_ERROR_COMMUNICATION_FAILURE
772 * \retval PSA_ERROR_HARDWARE_FAILURE
773 * \retval PSA_ERROR_TAMPERING_DETECTED
774 */
775psa_status_t psa_export_public_key(psa_key_slot_t key,
776 uint8_t *data,
777 size_t data_size,
778 size_t *data_length);
779
780/**@}*/
781
782/** \defgroup policy Key policies
783 * @{
784 */
785
786/** \brief Encoding of permitted usage on a key. */
787typedef uint32_t psa_key_usage_t;
788
Gilles Peskine7e198532018-03-08 07:50:30 +0100789/** Whether the key may be exported.
790 *
791 * A public key or the public part of a key pair may always be exported
792 * regardless of the value of this permission flag.
793 *
794 * If a key does not have export permission, implementations shall not
795 * allow the key to be exported in plain form from the cryptoprocessor,
796 * whether through psa_export_key() or through a proprietary interface.
797 * The key may however be exportable in a wrapped form, i.e. in a form
798 * where it is encrypted by another key.
799 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100800#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
801
Gilles Peskine7e198532018-03-08 07:50:30 +0100802/** Whether the key may be used to encrypt a message.
803 *
804 * For a key pair, this concerns the public key.
805 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100806#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +0100807
808/** Whether the key may be used to decrypt a message.
809 *
810 * For a key pair, this concerns the private key.
811 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100812#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +0100813
814/** Whether the key may be used to sign a message.
815 *
816 * For a key pair, this concerns the private key.
817 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100818#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +0100819
820/** Whether the key may be used to verify a message signature.
821 *
822 * For a key pair, this concerns the public key.
823 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100824#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
825
826/** The type of the key policy data structure.
827 *
828 * This is an implementation-defined \c struct. Applications should not
829 * make any assumptions about the content of this structure except
830 * as directed by the documentation of a specific implementation. */
831typedef struct psa_key_policy_s psa_key_policy_t;
832
833/** \brief Initialize a key policy structure to a default that forbids all
834 * usage of the key. */
835void psa_key_policy_init(psa_key_policy_t *policy);
836
Gilles Peskine7e198532018-03-08 07:50:30 +0100837/** \brief Set the standard fields of a policy structure.
838 *
839 * Note that this function does not make any consistency check of the
840 * parameters. The values are only checked when applying the policy to
841 * a key slot with psa_set_key_policy().
842 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100843void psa_key_policy_set_usage(psa_key_policy_t *policy,
844 psa_key_usage_t usage,
845 psa_algorithm_t alg);
846
847psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy);
848
849psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy);
850
851/** \brief Set the usage policy on a key slot.
852 *
853 * This function must be called on an empty key slot, before importing,
854 * generating or creating a key in the slot. Changing the policy of an
855 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100856 *
857 * Implementations may set restrictions on supported key policies
858 * depending on the key type and the key slot.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100859 */
860psa_status_t psa_set_key_policy(psa_key_slot_t key,
861 const psa_key_policy_t *policy);
862
Gilles Peskine7e198532018-03-08 07:50:30 +0100863/** \brief Get the usage policy for a key slot.
864 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100865psa_status_t psa_get_key_policy(psa_key_slot_t key,
866 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100867
868/**@}*/
869
Gilles Peskine609b6a52018-03-03 21:31:50 +0100870/** \defgroup persistence Key lifetime
871 * @{
872 */
873
874/** Encoding of key lifetimes.
875 */
876typedef uint32_t psa_key_lifetime_t;
877
878/** A volatile key slot retains its content as long as the application is
879 * running. It is guaranteed to be erased on a power reset.
880 */
881#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
882
883/** A persistent key slot retains its content as long as it is not explicitly
884 * destroyed.
885 */
886#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
887
888/** A write-once key slot may not be modified once a key has been set.
889 * It will retain its content as long as the device remains operational.
890 */
891#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
892
Gilles Peskined393e182018-03-08 07:49:16 +0100893/** \brief Retrieve the lifetime of a key slot.
894 *
895 * The assignment of lifetimes to slots is implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200896 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +0200897 * \param key Slot to query.
mohammad1603804cd712018-03-20 22:44:08 +0200898 * \param lifetime On success, the lifetime value.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200899 *
mohammad1603804cd712018-03-20 22:44:08 +0200900 * \retval PSA_SUCCESS
901 * Success.
902 * \retval PSA_ERROR_INVALID_ARGUMENT
mohammad1603a7d245a2018-04-17 00:40:08 -0700903 * The key slot is invalid.
Gilles Peskinef0c9dd32018-04-17 14:11:07 +0200904 * \retval PSA_ERROR_COMMUNICATION_FAILURE
905 * \retval PSA_ERROR_HARDWARE_FAILURE
906 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +0100907 */
Gilles Peskine609b6a52018-03-03 21:31:50 +0100908psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
909 psa_key_lifetime_t *lifetime);
910
Gilles Peskined393e182018-03-08 07:49:16 +0100911/** \brief Change the lifetime of a key slot.
912 *
913 * Whether the lifetime of a key slot can be changed at all, and if so
Gilles Peskine19067982018-03-20 17:54:53 +0100914 * whether the lifetime of an occupied key slot can be changed, is
Gilles Peskined393e182018-03-08 07:49:16 +0100915 * implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200916 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +0200917 * \param key Slot whose lifetime is to be changed.
918 * \param lifetime The lifetime value to set for the given key slot.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200919 *
mohammad1603804cd712018-03-20 22:44:08 +0200920 * \retval PSA_SUCCESS
921 * Success.
922 * \retval PSA_ERROR_INVALID_ARGUMENT
923 * The key slot is invalid,
mohammad1603a7d245a2018-04-17 00:40:08 -0700924 * or the lifetime value is invalid.
Gilles Peskinef0c9dd32018-04-17 14:11:07 +0200925 * \retval PSA_ERROR_NOT_SUPPORTED
926 * The implementation does not support the specified lifetime value,
927 * at least for the specified key slot.
928 * \retval PSA_ERROR_OCCUPIED_SLOT
929 * The slot contains a key, and the implementation does not support
930 * changing the lifetime of an occupied slot.
931 * \retval PSA_ERROR_COMMUNICATION_FAILURE
932 * \retval PSA_ERROR_HARDWARE_FAILURE
933 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +0100934 */
935psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
mohammad1603ea050092018-04-17 00:31:34 -0700936 psa_key_lifetime_t lifetime);
Gilles Peskined393e182018-03-08 07:49:16 +0100937
Gilles Peskine609b6a52018-03-03 21:31:50 +0100938/**@}*/
939
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100940/** \defgroup hash Message digests
941 * @{
942 */
943
Gilles Peskine308b91d2018-02-08 09:47:44 +0100944/** The type of the state data structure for multipart hash operations.
945 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100946 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100947 * make any assumptions about the content of this structure except
948 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100949typedef struct psa_hash_operation_s psa_hash_operation_t;
950
Gilles Peskine308b91d2018-02-08 09:47:44 +0100951/** The size of the output of psa_hash_finish(), in bytes.
952 *
953 * This is also the hash size that psa_hash_verify() expects.
954 *
955 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine35855962018-04-19 08:39:16 +0200956 * #PSA_ALG_IS_HASH(alg) is true), or an HMAC algorithm
957 * (`PSA_ALG_HMAC(hash_alg)` where `hash_alg` is a
958 * hash algorithm).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100959 *
960 * \return The hash size for the specified hash algorithm.
961 * If the hash algorithm is not recognized, return 0.
962 * An implementation may return either 0 or the correct size
963 * for a hash algorithm that it recognizes, but does not support.
964 */
Gilles Peskine71bb7b72018-04-19 08:29:59 +0200965#define PSA_HASH_SIZE(alg) \
966 ( \
967 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
968 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \
969 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
970 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
971 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
972 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
973 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
974 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
975 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
976 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
977 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
978 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
979 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
980 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
981 PSA_ALG_RSA_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100982 0)
983
Gilles Peskine308b91d2018-02-08 09:47:44 +0100984/** Start a multipart hash operation.
985 *
986 * The sequence of operations to calculate a hash (message digest)
987 * is as follows:
988 * -# Allocate an operation object which will be passed to all the functions
989 * listed here.
990 * -# Call psa_hash_start() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100991 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100992 * of the message each time. The hash that is calculated is the hash
993 * of the concatenation of these messages in order.
994 * -# To calculate the hash, call psa_hash_finish().
995 * To compare the hash with an expected value, call psa_hash_verify().
996 *
997 * The application may call psa_hash_abort() at any time after the operation
998 * has been initialized with psa_hash_start().
999 *
1000 * After a successful call to psa_hash_start(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001001 * eventually terminate the operation. The following events terminate an
1002 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001003 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001004 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001005 *
Gilles Peskine36a74b72018-06-01 16:30:32 +02001006 * \param operation The operation object to use.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001007 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1008 * such that #PSA_ALG_IS_HASH(alg) is true).
1009 *
1010 * \retval PSA_SUCCESS
1011 * Success.
1012 * \retval PSA_ERROR_NOT_SUPPORTED
1013 * \c alg is not supported or is not a hash algorithm.
1014 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1015 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1016 * \retval PSA_ERROR_HARDWARE_FAILURE
1017 * \retval PSA_ERROR_TAMPERING_DETECTED
1018 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001019psa_status_t psa_hash_start(psa_hash_operation_t *operation,
1020 psa_algorithm_t alg);
1021
Gilles Peskine308b91d2018-02-08 09:47:44 +01001022/** Add a message fragment to a multipart hash operation.
1023 *
1024 * The application must call psa_hash_start() before calling this function.
1025 *
1026 * If this function returns an error status, the operation becomes inactive.
1027 *
1028 * \param operation Active hash operation.
1029 * \param input Buffer containing the message fragment to hash.
1030 * \param input_length Size of the \c input buffer in bytes.
1031 *
1032 * \retval PSA_SUCCESS
1033 * Success.
1034 * \retval PSA_ERROR_BAD_STATE
1035 * The operation state is not valid (not started, or already completed).
1036 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1037 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1038 * \retval PSA_ERROR_HARDWARE_FAILURE
1039 * \retval PSA_ERROR_TAMPERING_DETECTED
1040 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001041psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1042 const uint8_t *input,
1043 size_t input_length);
1044
Gilles Peskine308b91d2018-02-08 09:47:44 +01001045/** Finish the calculation of the hash of a message.
1046 *
1047 * The application must call psa_hash_start() before calling this function.
1048 * This function calculates the hash of the message formed by concatenating
1049 * the inputs passed to preceding calls to psa_hash_update().
1050 *
1051 * When this function returns, the operation becomes inactive.
1052 *
1053 * \warning Applications should not call this function if they expect
1054 * a specific value for the hash. Call psa_hash_verify() instead.
1055 * Beware that comparing integrity or authenticity data such as
1056 * hash values with a function such as \c memcmp is risky
1057 * because the time taken by the comparison may leak information
1058 * about the hashed data which could allow an attacker to guess
1059 * a valid hash and thereby bypass security controls.
1060 *
1061 * \param operation Active hash operation.
1062 * \param hash Buffer where the hash is to be written.
1063 * \param hash_size Size of the \c hash buffer in bytes.
1064 * \param hash_length On success, the number of bytes
1065 * that make up the hash value. This is always
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001066 * #PSA_HASH_SIZE(alg) where \c alg is the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001067 * hash algorithm that is calculated.
1068 *
1069 * \retval PSA_SUCCESS
1070 * Success.
1071 * \retval PSA_ERROR_BAD_STATE
1072 * The operation state is not valid (not started, or already completed).
1073 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1074 * The size of the \c hash buffer is too small. You can determine a
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001075 * sufficient buffer size by calling #PSA_HASH_SIZE(alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001076 * where \c alg is the hash algorithm that is calculated.
1077 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1078 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1079 * \retval PSA_ERROR_HARDWARE_FAILURE
1080 * \retval PSA_ERROR_TAMPERING_DETECTED
1081 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001082psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1083 uint8_t *hash,
1084 size_t hash_size,
1085 size_t *hash_length);
1086
Gilles Peskine308b91d2018-02-08 09:47:44 +01001087/** Finish the calculation of the hash of a message and compare it with
1088 * an expected value.
1089 *
1090 * The application must call psa_hash_start() before calling this function.
1091 * This function calculates the hash of the message formed by concatenating
1092 * the inputs passed to preceding calls to psa_hash_update(). It then
1093 * compares the calculated hash with the expected hash passed as a
1094 * parameter to this function.
1095 *
1096 * When this function returns, the operation becomes inactive.
1097 *
Gilles Peskine19067982018-03-20 17:54:53 +01001098 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001099 * comparison between the actual hash and the expected hash is performed
1100 * in constant time.
1101 *
1102 * \param operation Active hash operation.
1103 * \param hash Buffer containing the expected hash value.
1104 * \param hash_length Size of the \c hash buffer in bytes.
1105 *
1106 * \retval PSA_SUCCESS
1107 * The expected hash is identical to the actual hash of the message.
1108 * \retval PSA_ERROR_INVALID_SIGNATURE
1109 * The hash of the message was calculated successfully, but it
1110 * differs from the expected hash.
1111 * \retval PSA_ERROR_BAD_STATE
1112 * The operation state is not valid (not started, or already completed).
1113 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1114 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1115 * \retval PSA_ERROR_HARDWARE_FAILURE
1116 * \retval PSA_ERROR_TAMPERING_DETECTED
1117 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001118psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1119 const uint8_t *hash,
1120 size_t hash_length);
1121
Gilles Peskine308b91d2018-02-08 09:47:44 +01001122/** Abort a hash operation.
1123 *
1124 * This function may be called at any time after psa_hash_start().
1125 * Aborting an operation frees all associated resources except for the
1126 * \c operation structure itself.
1127 *
1128 * Implementation should strive to be robust and handle inactive hash
1129 * operations safely (do nothing and return #PSA_ERROR_BAD_STATE). However,
1130 * application writers should beware that uninitialized memory may happen
1131 * to be indistinguishable from an active hash operation, and the behavior
1132 * of psa_hash_abort() is undefined in this case.
1133 *
1134 * \param operation Active hash operation.
1135 *
1136 * \retval PSA_SUCCESS
1137 * \retval PSA_ERROR_BAD_STATE
1138 * \c operation is not an active hash operation.
1139 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1140 * \retval PSA_ERROR_HARDWARE_FAILURE
1141 * \retval PSA_ERROR_TAMPERING_DETECTED
1142 */
1143psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001144
1145/**@}*/
1146
Gilles Peskine8c9def32018-02-08 10:02:12 +01001147/** \defgroup MAC Message authentication codes
1148 * @{
1149 */
1150
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001151/** The type of the state data structure for multipart MAC operations.
1152 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001153 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001154 * make any assumptions about the content of this structure except
1155 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001156typedef struct psa_mac_operation_s psa_mac_operation_t;
1157
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001158/** The size of the output of psa_mac_finish(), in bytes.
1159 *
1160 * This is also the MAC size that psa_mac_verify() expects.
1161 *
1162 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
1163 * #PSA_ALG_IS_MAC(alg) is true).
1164 *
1165 * \return The MAC size for the specified algorithm.
1166 * If the MAC algorithm is not recognized, return 0.
1167 * An implementation may return either 0 or the correct size
1168 * for a MAC algorithm that it recognizes, but does not support.
1169 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001170#define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001171 (PSA_ALG_IS_HMAC(alg) ? PSA_HASH_SIZE(PSA_ALG_HMAC_HASH(alg)) : \
Gilles Peskine8c9def32018-02-08 10:02:12 +01001172 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
1173 0)
1174
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001175/** Start a multipart MAC operation.
1176 *
1177 * The sequence of operations to calculate a MAC (message authentication code)
1178 * is as follows:
1179 * -# Allocate an operation object which will be passed to all the functions
1180 * listed here.
1181 * -# Call psa_mac_start() to specify the algorithm and key.
1182 * The key remains associated with the operation even if the content
1183 * of the key slot changes.
1184 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1185 * of the message each time. The MAC that is calculated is the MAC
1186 * of the concatenation of these messages in order.
1187 * -# To calculate the MAC, call psa_mac_finish().
1188 * To compare the MAC with an expected value, call psa_mac_verify().
1189 *
1190 * The application may call psa_mac_abort() at any time after the operation
1191 * has been initialized with psa_mac_start().
1192 *
1193 * After a successful call to psa_mac_start(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001194 * eventually terminate the operation. The following events terminate an
1195 * operation:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001196 * - A failed call to psa_mac_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001197 * - A call to psa_mac_finish(), psa_mac_verify() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001198 *
Gilles Peskine36a74b72018-06-01 16:30:32 +02001199 * \param operation The operation object to use.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001200 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1201 * such that #PSA_ALG_IS_MAC(alg) is true).
1202 *
1203 * \retval PSA_SUCCESS
1204 * Success.
1205 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine92b30732018-03-03 21:29:30 +01001206 * \retval PSA_ERROR_NOT_PERMITTED
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001207 * \retval PSA_ERROR_INVALID_ARGUMENT
1208 * \c key is not compatible with \c alg.
1209 * \retval PSA_ERROR_NOT_SUPPORTED
1210 * \c alg is not supported or is not a MAC algorithm.
1211 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1212 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1213 * \retval PSA_ERROR_HARDWARE_FAILURE
1214 * \retval PSA_ERROR_TAMPERING_DETECTED
1215 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001216psa_status_t psa_mac_start(psa_mac_operation_t *operation,
1217 psa_key_slot_t key,
1218 psa_algorithm_t alg);
1219
1220psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1221 const uint8_t *input,
1222 size_t input_length);
1223
1224psa_status_t psa_mac_finish(psa_mac_operation_t *operation,
1225 uint8_t *mac,
1226 size_t mac_size,
1227 size_t *mac_length);
1228
1229psa_status_t psa_mac_verify(psa_mac_operation_t *operation,
1230 const uint8_t *mac,
1231 size_t mac_length);
1232
1233psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1234
1235/**@}*/
1236
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001237/** \defgroup cipher Symmetric ciphers
1238 * @{
1239 */
1240
1241/** The type of the state data structure for multipart cipher operations.
1242 *
1243 * This is an implementation-defined \c struct. Applications should not
1244 * make any assumptions about the content of this structure except
1245 * as directed by the documentation of a specific implementation. */
1246typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1247
1248/** Set the key for a multipart symmetric encryption operation.
1249 *
1250 * The sequence of operations to encrypt a message with a symmetric cipher
1251 * is as follows:
1252 * -# Allocate an operation object which will be passed to all the functions
1253 * listed here.
1254 * -# Call psa_encrypt_setup() to specify the algorithm and key.
1255 * The key remains associated with the operation even if the content
1256 * of the key slot changes.
1257 * -# Call either psa_encrypt_generate_iv() or psa_encrypt_set_iv() to
1258 * generate or set the IV (initialization vector). You should use
1259 * psa_encrypt_generate_iv() unless the protocol you are implementing
1260 * requires a specific IV value.
1261 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1262 * of the message each time.
1263 * -# Call psa_cipher_finish().
1264 *
1265 * The application may call psa_cipher_abort() at any time after the operation
1266 * has been initialized with psa_encrypt_setup().
1267 *
1268 * After a successful call to psa_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001269 * eventually terminate the operation. The following events terminate an
1270 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001271 * - A failed call to psa_encrypt_generate_iv(), psa_encrypt_set_iv()
1272 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001273 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001274 *
Gilles Peskine36a74b72018-06-01 16:30:32 +02001275 * \param operation The operation object to use.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001276 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
1277 * such that #PSA_ALG_IS_CIPHER(alg) is true).
1278 *
1279 * \retval PSA_SUCCESS
1280 * Success.
1281 * \retval PSA_ERROR_EMPTY_SLOT
1282 * \retval PSA_ERROR_NOT_PERMITTED
1283 * \retval PSA_ERROR_INVALID_ARGUMENT
1284 * \c key is not compatible with \c alg.
1285 * \retval PSA_ERROR_NOT_SUPPORTED
1286 * \c alg is not supported or is not a cipher algorithm.
1287 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1288 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1289 * \retval PSA_ERROR_HARDWARE_FAILURE
1290 * \retval PSA_ERROR_TAMPERING_DETECTED
1291 */
1292psa_status_t psa_encrypt_setup(psa_cipher_operation_t *operation,
1293 psa_key_slot_t key,
1294 psa_algorithm_t alg);
1295
1296/** Set the key for a multipart symmetric decryption operation.
1297 *
1298 * The sequence of operations to decrypt a message with a symmetric cipher
1299 * is as follows:
1300 * -# Allocate an operation object which will be passed to all the functions
1301 * listed here.
1302 * -# Call psa_decrypt_setup() to specify the algorithm and key.
1303 * The key remains associated with the operation even if the content
1304 * of the key slot changes.
1305 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1306 * decryption. If the IV is prepended to the ciphertext, you can call
1307 * psa_cipher_update() on a buffer containing the IV followed by the
1308 * beginning of the message.
1309 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1310 * of the message each time.
1311 * -# Call psa_cipher_finish().
1312 *
1313 * The application may call psa_cipher_abort() at any time after the operation
1314 * has been initialized with psa_encrypt_setup().
1315 *
1316 * After a successful call to psa_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001317 * eventually terminate the operation. The following events terminate an
1318 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001319 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001320 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001321 *
Gilles Peskine36a74b72018-06-01 16:30:32 +02001322 * \param operation The operation object to use.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001323 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
1324 * such that #PSA_ALG_IS_CIPHER(alg) is true).
1325 *
1326 * \retval PSA_SUCCESS
1327 * Success.
1328 * \retval PSA_ERROR_EMPTY_SLOT
1329 * \retval PSA_ERROR_NOT_PERMITTED
1330 * \retval PSA_ERROR_INVALID_ARGUMENT
1331 * \c key is not compatible with \c alg.
1332 * \retval PSA_ERROR_NOT_SUPPORTED
1333 * \c alg is not supported or is not a cipher algorithm.
1334 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1335 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1336 * \retval PSA_ERROR_HARDWARE_FAILURE
1337 * \retval PSA_ERROR_TAMPERING_DETECTED
1338 */
1339psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation,
1340 psa_key_slot_t key,
1341 psa_algorithm_t alg);
1342
1343psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation,
1344 unsigned char *iv,
1345 size_t iv_size,
1346 size_t *iv_length);
1347
1348psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation,
1349 const unsigned char *iv,
1350 size_t iv_length);
1351
1352psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1353 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001354 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001355 unsigned char *output,
1356 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001357 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001358
1359psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001360 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001361 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001362 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001363
1364psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1365
1366/**@}*/
1367
Gilles Peskine3b555712018-03-03 21:27:57 +01001368/** \defgroup aead Authenticated encryption with associated data (AEAD)
1369 * @{
1370 */
1371
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001372/** The tag size for an AEAD algorithm, in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001373 *
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001374 * \param alg An AEAD algorithm
1375 * (\c PSA_ALG_XXX value such that
1376 * #PSA_ALG_IS_AEAD(alg) is true).
1377 *
1378 * \return The tag size for the specified algorithm.
1379 * If the AEAD algorithm does not have an identified
1380 * tag that can be distinguished from the rest of
1381 * the ciphertext, return 0.
1382 * If the AEAD algorithm is not recognized, return 0.
1383 * An implementation may return either 0 or a
1384 * correct size for an AEAD algorithm that it
1385 * recognizes, but does not support.
1386 */
1387#define PSA_AEAD_TAG_SIZE(alg) \
1388 ((alg) == PSA_ALG_GCM ? 16 : \
1389 (alg) == PSA_ALG_CCM ? 16 : \
1390 0)
Gilles Peskine3b555712018-03-03 21:27:57 +01001391
Gilles Peskine212e4d82018-06-08 11:36:37 +02001392/** The maximum size of the output of psa_aead_encrypt(), in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001393 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001394 * If the size of the ciphertext buffer is at least this large, it is
1395 * guaranteed that psa_aead_encrypt() will not fail due to an
1396 * insufficient buffer size. Depending on the algorithm, the actual size of
1397 * the ciphertext may be smaller.
Gilles Peskine3b555712018-03-03 21:27:57 +01001398 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001399 * \param alg An AEAD algorithm
mohammad16031347a732018-06-07 01:38:45 +03001400 * (\c PSA_ALG_XXX value such that
1401 * #PSA_ALG_IS_AEAD(alg) is true).
Gilles Peskine212e4d82018-06-08 11:36:37 +02001402 * \param plaintext_length Size of the plaintext in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001403 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001404 * \return The AEAD ciphertext size for the specified
1405 * algorithm.
1406 * If the AEAD algorithm is not recognized, return 0.
1407 * An implementation may return either 0 or a
1408 * correct size for an AEAD algorithm that it
1409 * recognizes, but does not support.
mohammad16031347a732018-06-07 01:38:45 +03001410 */
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001411#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) \
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001412 (PSA_AEAD_TAG_SIZE(alg) != 0 ? \
1413 (plaintext_length) + PSA_AEAD_TAG_SIZE(alg) : \
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001414 0)
1415
1416/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001417 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001418 * \param key Slot containing the key to use.
1419 * \param alg The AEAD algorithm to compute
1420 * (\c PSA_ALG_XXX value such that
1421 * #PSA_ALG_IS_AEAD(alg) is true).
1422 * \param nonce Nonce or IV to use.
1423 * \param nonce_length Size of the \p nonce buffer in bytes.
1424 * \param additional_data Additional data that will be authenticated
1425 * but not encrypted.
1426 * \param additional_data_length Size of \p additional_data in bytes.
1427 * \param plaintext Data that will be authenticated and
1428 * encrypted.
1429 * \param plaintext_length Size of \p plaintext in bytes.
1430 * \param ciphertext Output buffer for the authenticated and
1431 * encrypted data. The additional data is not
1432 * part of this output. For algorithms where the
1433 * encrypted data and the authentication tag
1434 * are defined as separate outputs, the
1435 * authentication tag is appended to the
1436 * encrypted data.
1437 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1438 * This must be at least
1439 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1440 * \p plaintext_length).
1441 * \param ciphertext_length On success, the size of the output
1442 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001443 *
1444 * \retval PSA_SUCCESS
1445 * Success.
1446 * \retval PSA_ERROR_EMPTY_SLOT
1447 * \retval PSA_ERROR_NOT_PERMITTED
1448 * \retval PSA_ERROR_INVALID_ARGUMENT
1449 * \c key is not compatible with \c alg.
1450 * \retval PSA_ERROR_NOT_SUPPORTED
1451 * \c alg is not supported or is not an AEAD algorithm.
1452 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1453 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1454 * \retval PSA_ERROR_HARDWARE_FAILURE
1455 * \retval PSA_ERROR_TAMPERING_DETECTED
1456 */
mohammad160339ee8712018-04-26 00:51:02 +03001457psa_status_t psa_aead_encrypt( psa_key_slot_t key,
1458 psa_algorithm_t alg,
1459 const uint8_t *nonce,
1460 size_t nonce_length,
1461 const uint8_t *additional_data,
1462 size_t additional_data_length,
1463 const uint8_t *plaintext,
1464 size_t plaintext_length,
1465 uint8_t *ciphertext,
1466 size_t ciphertext_size,
1467 size_t *ciphertext_length );
Gilles Peskine3b555712018-03-03 21:27:57 +01001468
Gilles Peskine212e4d82018-06-08 11:36:37 +02001469/** The maximum size of the output of psa_aead_decrypt(), in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001470 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001471 * If the size of the plaintext buffer is at least this large, it is
1472 * guaranteed that psa_aead_decrypt() will not fail due to an
1473 * insufficient buffer size. Depending on the algorithm, the actual size of
1474 * the plaintext may be smaller.
Gilles Peskine3b555712018-03-03 21:27:57 +01001475 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001476 * \param alg An AEAD algorithm
mohammad16031347a732018-06-07 01:38:45 +03001477 * (\c PSA_ALG_XXX value such that
1478 * #PSA_ALG_IS_AEAD(alg) is true).
Gilles Peskine212e4d82018-06-08 11:36:37 +02001479 * \param ciphertext_length Size of the plaintext in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001480 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001481 * \return The AEAD ciphertext size for the specified
1482 * algorithm.
1483 * If the AEAD algorithm is not recognized, return 0.
1484 * An implementation may return either 0 or a
1485 * correct size for an AEAD algorithm that it
1486 * recognizes, but does not support.
mohammad16031347a732018-06-07 01:38:45 +03001487 */
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001488#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \
1489 (PSA_AEAD_TAG_SIZE(alg) != 0 ? \
1490 (plaintext_length) - PSA_AEAD_TAG_SIZE(alg) : \
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001491 0)
1492
1493/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001494 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001495 * \param key Slot containing the key to use.
1496 * \param alg The AEAD algorithm to compute
1497 * (\c PSA_ALG_XXX value such that
1498 * #PSA_ALG_IS_AEAD(alg) is true).
1499 * \param nonce Nonce or IV to use.
1500 * \param nonce_length Size of the \p nonce buffer in bytes.
1501 * \param additional_data Additional data that has been authenticated
1502 * but not encrypted.
1503 * \param additional_data_length Size of \p additional_data in bytes.
1504 * \param ciphertext Data that has been authenticated and
1505 * encrypted. For algorithms where the
1506 * encrypted data and the authentication tag
1507 * are defined as separate inputs, the buffer
1508 * must contain the encrypted data followed
1509 * by the authentication tag.
1510 * \param ciphertext_length Size of \p ciphertext in bytes.
1511 * \param plaintext Output buffer for the decrypted data.
1512 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1513 * This must be at least
1514 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1515 * \p ciphertext_length).
1516 * \param plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001517 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001518 *
1519 * \retval PSA_SUCCESS
1520 * Success.
1521 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001522 * \retval PSA_ERROR_INVALID_SIGNATURE
1523 * The ciphertext is not authentic.
Gilles Peskine3b555712018-03-03 21:27:57 +01001524 * \retval PSA_ERROR_NOT_PERMITTED
1525 * \retval PSA_ERROR_INVALID_ARGUMENT
1526 * \c key is not compatible with \c alg.
1527 * \retval PSA_ERROR_NOT_SUPPORTED
Gilles Peskine19067982018-03-20 17:54:53 +01001528 * \c alg is not supported or is not an AEAD algorithm.
Gilles Peskine3b555712018-03-03 21:27:57 +01001529 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1530 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1531 * \retval PSA_ERROR_HARDWARE_FAILURE
1532 * \retval PSA_ERROR_TAMPERING_DETECTED
1533 */
mohammad160339ee8712018-04-26 00:51:02 +03001534psa_status_t psa_aead_decrypt( psa_key_slot_t key,
1535 psa_algorithm_t alg,
1536 const uint8_t *nonce,
1537 size_t nonce_length,
1538 const uint8_t *additional_data,
1539 size_t additional_data_length,
1540 const uint8_t *ciphertext,
1541 size_t ciphertext_length,
1542 uint8_t *plaintext,
1543 size_t plaintext_size,
1544 size_t *plaintext_length );
Gilles Peskine3b555712018-03-03 21:27:57 +01001545
1546/**@}*/
1547
Gilles Peskine20035e32018-02-03 22:44:14 +01001548/** \defgroup asymmetric Asymmetric cryptography
1549 * @{
1550 */
1551
1552/**
Gilles Peskine0189e752018-02-03 23:57:22 +01001553 * \brief Maximum ECDSA signature size for a given curve bit size
1554 *
1555 * \param curve_bits Curve size in bits
1556 * \return Maximum signature size in bytes
1557 *
1558 * \note This macro returns a compile-time constant if its argument is one.
1559 *
1560 * \warning This macro may evaluate its argument multiple times.
1561 */
1562/*
1563 * RFC 4492 page 20:
1564 *
1565 * Ecdsa-Sig-Value ::= SEQUENCE {
1566 * r INTEGER,
1567 * s INTEGER
1568 * }
1569 *
1570 * Size is at most
1571 * 1 (tag) + 1 (len) + 1 (initial 0) + curve_bytes for each of r and s,
1572 * twice that + 1 (tag) + 2 (len) for the sequence
1573 * (assuming curve_bytes is less than 126 for r and s,
1574 * and less than 124 (total len <= 255) for the sequence)
1575 */
1576#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
1577 ( /*T,L of SEQUENCE*/ ((curve_bits) >= 61 * 8 ? 3 : 2) + \
1578 /*T,L of r,s*/ 2 * (((curve_bits) >= 127 * 8 ? 3 : 2) + \
1579 /*V of r,s*/ ((curve_bits) + 8) / 8))
1580
1581
Gilles Peskine308b91d2018-02-08 09:47:44 +01001582/** Safe signature buffer size for psa_asymmetric_sign().
1583 *
1584 * This macro returns a safe buffer size for a signature using a key
1585 * of the specified type and size, with the specified algorithm.
1586 * Note that the actual size of the signature may be smaller
1587 * (some algorithms produce a variable-size signature).
1588 *
1589 * \warning This function may call its arguments multiple times or
1590 * zero times, so you should not pass arguments that contain
1591 * side effects.
1592 *
1593 * \param key_type An asymmetric key type (this may indifferently be a
1594 * key pair type or a public key type).
1595 * \param key_bits The size of the key in bits.
1596 * \param alg The signature algorithm.
1597 *
1598 * \return If the parameters are valid and supported, return
1599 * a buffer size in bytes that guarantees that
1600 * psa_asymmetric_sign() will not fail with
1601 * #PSA_ERROR_BUFFER_TOO_SMALL.
1602 * If the parameters are a valid combination that is not supported
1603 * by the implementation, this macro either shall return either a
1604 * sensible size or 0.
1605 * If the parameters are not valid, the
1606 * return value is unspecified.
1607 *
1608 */
Gilles Peskine0189e752018-02-03 23:57:22 +01001609#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine2905a7a2018-03-07 16:39:31 +01001610 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine0189e752018-02-03 23:57:22 +01001611 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
Gilles Peskine84845652018-03-28 14:17:40 +02001612 ((void)alg, 0))
Gilles Peskine0189e752018-02-03 23:57:22 +01001613
1614/**
Gilles Peskine20035e32018-02-03 22:44:14 +01001615 * \brief Sign a hash or short message with a private key.
1616 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001617 * \param key Key slot containing an asymmetric key pair.
1618 * \param alg A signature algorithm that is compatible with
1619 * the type of \c key.
1620 * \param hash The message to sign.
1621 * \param hash_length Size of the \c hash buffer in bytes.
1622 * \param salt A salt or label, if supported by the signature
1623 * algorithm.
1624 * If the signature algorithm does not support a
1625 * salt, pass \c NULL.
1626 * If the signature algorithm supports an optional
1627 * salt and you do not want to pass a salt,
1628 * pass \c NULL.
1629 * \param salt_length Size of the \c salt buffer in bytes.
1630 * If \c salt is \c NULL, pass 0.
1631 * \param signature Buffer where the signature is to be written.
1632 * \param signature_size Size of the \c signature buffer in bytes.
1633 * \param signature_length On success, the number of bytes
1634 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001635 *
1636 * \retval PSA_SUCCESS
1637 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1638 * The size of the \c signature buffer is too small. You can
1639 * determine a sufficient buffer size by calling
1640 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg)
1641 * where \c key_type and \c key_bits are the type and bit-size
1642 * respectively of \c key.
1643 * \retval PSA_ERROR_NOT_SUPPORTED
1644 * \retval PSA_ERROR_INVALID_ARGUMENT
1645 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1646 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1647 * \retval PSA_ERROR_HARDWARE_FAILURE
1648 * \retval PSA_ERROR_TAMPERING_DETECTED
1649 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01001650 */
1651psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
1652 psa_algorithm_t alg,
1653 const uint8_t *hash,
1654 size_t hash_length,
1655 const uint8_t *salt,
1656 size_t salt_length,
1657 uint8_t *signature,
1658 size_t signature_size,
1659 size_t *signature_length);
1660
1661/**
1662 * \brief Verify the signature a hash or short message using a public key.
1663 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001664 * \param key Key slot containing a public key or an
1665 * asymmetric key pair.
1666 * \param alg A signature algorithm that is compatible with
1667 * the type of \c key.
1668 * \param hash The message whose signature is to be verified.
1669 * \param hash_length Size of the \c hash buffer in bytes.
1670 * \param salt A salt or label, if supported by the signature
1671 * algorithm.
1672 * If the signature algorithm does not support a
1673 * salt, pass \c NULL.
1674 * If the signature algorithm supports an optional
1675 * salt and you do not want to pass a salt,
1676 * pass \c NULL.
1677 * \param salt_length Size of the \c salt buffer in bytes.
1678 * If \c salt is \c NULL, pass 0.
1679 * \param signature Buffer containing the signature to verify.
1680 * \param signature_size Size of the \c signature buffer in bytes.
1681 *
1682 * \retval PSA_SUCCESS
1683 * The signature is valid.
1684 * \retval PSA_ERROR_INVALID_SIGNATURE
1685 * The calculation was perfomed successfully, but the passed
1686 * signature is not a valid signature.
1687 * \retval PSA_ERROR_NOT_SUPPORTED
1688 * \retval PSA_ERROR_INVALID_ARGUMENT
1689 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1690 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1691 * \retval PSA_ERROR_HARDWARE_FAILURE
1692 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01001693 */
1694psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
1695 psa_algorithm_t alg,
1696 const uint8_t *hash,
1697 size_t hash_length,
1698 const uint8_t *salt,
1699 size_t salt_length,
1700 uint8_t *signature,
1701 size_t signature_size);
1702
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001703#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine06297932018-04-11 16:58:22 +02001704 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
1705 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
1706 0)
Gilles Peskine723feff2018-05-31 20:08:13 +02001707#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
1708 (PSA_ALG_IS_RSA_OAEP_MGF1(alg) ? \
1709 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_GET_HASH(alg)) + 1 : \
1710 11 /*PKCS#1v1.5*/)
1711#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine06297932018-04-11 16:58:22 +02001712 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
Gilles Peskine723feff2018-05-31 20:08:13 +02001713 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
Gilles Peskine06297932018-04-11 16:58:22 +02001714 0)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001715
1716/**
1717 * \brief Encrypt a short message with a public key.
1718 *
1719 * \param key Key slot containing a public key or an asymmetric
1720 * key pair.
1721 * \param alg An asymmetric encryption algorithm that is
1722 * compatible with the type of \c key.
1723 * \param input The message to encrypt.
1724 * \param input_length Size of the \c input buffer in bytes.
1725 * \param salt A salt or label, if supported by the encryption
1726 * algorithm.
1727 * If the algorithm does not support a
1728 * salt, pass \c NULL.
1729 * If the algorithm supports an optional
1730 * salt and you do not want to pass a salt,
1731 * pass \c NULL.
1732 *
1733 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1734 * supported.
1735 * \param salt_length Size of the \c salt buffer in bytes.
1736 * If \c salt is \c NULL, pass 0.
1737 * \param output Buffer where the encrypted message is to be written.
1738 * \param output_size Size of the \c output buffer in bytes.
1739 * \param output_length On success, the number of bytes
1740 * that make up the returned output.
1741 *
1742 * \retval PSA_SUCCESS
1743 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1744 * The size of the \c output buffer is too small. You can
1745 * determine a sufficient buffer size by calling
1746 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1747 * where \c key_type and \c key_bits are the type and bit-size
1748 * respectively of \c key.
1749 * \retval PSA_ERROR_NOT_SUPPORTED
1750 * \retval PSA_ERROR_INVALID_ARGUMENT
1751 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1752 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1753 * \retval PSA_ERROR_HARDWARE_FAILURE
1754 * \retval PSA_ERROR_TAMPERING_DETECTED
1755 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1756 */
1757psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
1758 psa_algorithm_t alg,
1759 const uint8_t *input,
1760 size_t input_length,
1761 const uint8_t *salt,
1762 size_t salt_length,
1763 uint8_t *output,
1764 size_t output_size,
1765 size_t *output_length);
1766
1767/**
1768 * \brief Decrypt a short message with a private key.
1769 *
1770 * \param key Key slot containing an asymmetric key pair.
1771 * \param alg An asymmetric encryption algorithm that is
1772 * compatible with the type of \c key.
1773 * \param input The message to decrypt.
1774 * \param input_length Size of the \c input buffer in bytes.
1775 * \param salt A salt or label, if supported by the encryption
1776 * algorithm.
1777 * If the algorithm does not support a
1778 * salt, pass \c NULL.
1779 * If the algorithm supports an optional
1780 * salt and you do not want to pass a salt,
1781 * pass \c NULL.
1782 *
1783 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1784 * supported.
1785 * \param salt_length Size of the \c salt buffer in bytes.
1786 * If \c salt is \c NULL, pass 0.
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001787 * \param output Buffer where the decrypted message is to be written.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001788 * \param output_size Size of the \c output buffer in bytes.
1789 * \param output_length On success, the number of bytes
1790 * that make up the returned output.
1791 *
1792 * \retval PSA_SUCCESS
1793 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1794 * The size of the \c output buffer is too small. You can
1795 * determine a sufficient buffer size by calling
1796 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1797 * where \c key_type and \c key_bits are the type and bit-size
1798 * respectively of \c key.
1799 * \retval PSA_ERROR_NOT_SUPPORTED
1800 * \retval PSA_ERROR_INVALID_ARGUMENT
1801 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1802 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1803 * \retval PSA_ERROR_HARDWARE_FAILURE
1804 * \retval PSA_ERROR_TAMPERING_DETECTED
1805 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1806 * \retval PSA_ERROR_INVALID_PADDING
1807 */
1808psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
1809 psa_algorithm_t alg,
1810 const uint8_t *input,
1811 size_t input_length,
1812 const uint8_t *salt,
1813 size_t salt_length,
1814 uint8_t *output,
1815 size_t output_size,
1816 size_t *output_length);
1817
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001818/**@}*/
1819
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001820/** \defgroup generation Key generation
1821 * @{
1822 */
1823
1824/**
1825 * \brief Generate random bytes.
1826 *
1827 * \warning This function **can** fail! Callers MUST check the return status
1828 * and MUST NOT use the content of the output buffer if the return
1829 * status is not #PSA_SUCCESS.
1830 *
1831 * \note To generate a key, use psa_generate_key() instead.
1832 *
1833 * \param output Output buffer for the generated data.
1834 * \param output_size Number of bytes to generate and output.
1835 *
1836 * \retval PSA_SUCCESS
1837 * \retval PSA_ERROR_NOT_SUPPORTED
1838 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1839 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1840 * \retval PSA_ERROR_HARDWARE_FAILURE
1841 * \retval PSA_ERROR_TAMPERING_DETECTED
1842 */
1843psa_status_t psa_generate_random(uint8_t *output,
1844 size_t output_size);
1845
1846/**
1847 * \brief Generate a key or key pair.
1848 *
1849 * \param key Slot where the key will be stored. This must be a
1850 * valid slot for a key of the chosen type. It must
1851 * be unoccupied.
1852 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
1853 * \param bits Key size in bits.
1854 * \param parameters Extra parameters for key generation. The interpretation
1855 * of this parameter depends on \c type. All types support
1856 * \c NULL to use default parameters specified below.
1857 *
1858 * For any symmetric key type (type such that
1859 * `PSA_KEY_TYPE_IS_ASYMMETRIC(type)` is false), \c parameters must be
1860 * \c NULL. For asymmetric key types defined by this specification,
1861 * the parameter type and the default parameters are defined by the
1862 * table below. For vendor-defined key types, the vendor documentation
1863 * shall define the parameter type and the default parameters.
1864 *
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001865 * Type | Parameter type | Meaning | Parameters used if `parameters == NULL`
1866 * ---- | -------------- | ------- | ---------------------------------------
1867 * `PSA_KEY_TYPE_RSA_KEYPAIR` | `unsigned int` | Public exponent | 65537
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001868 *
1869 * \retval PSA_SUCCESS
1870 * \retval PSA_ERROR_NOT_SUPPORTED
1871 * \retval PSA_ERROR_INVALID_ARGUMENT
1872 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1873 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1874 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1875 * \retval PSA_ERROR_HARDWARE_FAILURE
1876 * \retval PSA_ERROR_TAMPERING_DETECTED
1877 */
1878psa_status_t psa_generate_key(psa_key_slot_t key,
1879 psa_key_type_t type,
1880 size_t bits,
1881 const void *parameters);
1882
1883/**@}*/
1884
Gilles Peskinee59236f2018-01-27 23:32:46 +01001885#ifdef __cplusplus
1886}
1887#endif
1888
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001889/* The file "crypto_struct.h" contains definitions for
1890 * implementation-specific structs that are declared above. */
1891#include "crypto_struct.h"
1892
1893/* The file "crypto_extra.h" contains vendor-specific definitions. This
1894 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01001895#include "crypto_extra.h"
1896
1897#endif /* PSA_CRYPTO_H */