blob: 7eb45fbe8323127174c44c4a85b2aec855f61bbd [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__
14/** \defgroup platform Implementation-specific definitions
15 * @{
16 */
17
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010018/** \brief Key slot number.
19 *
20 * This type represents key slots. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010021 * type. The choice of type is implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010022 * 0 is not a valid key slot number. The meaning of other values is
23 * implementation dependent.
24 *
25 * At any given point in time, each key slot either contains a
26 * cryptographic object, or is empty. Key slots are persistent:
27 * once set, the cryptographic object remains in the key slot until
28 * explicitly destroyed.
29 */
30typedef _unsigned_integral_type_ psa_key_slot_t;
31
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010032/**@}*/
33#endif
34
Gilles Peskinee59236f2018-01-27 23:32:46 +010035#ifdef __cplusplus
36extern "C" {
37#endif
38
39/** \defgroup basic Basic definitions
40 * @{
41 */
42
43/**
44 * \brief Function return status.
45 *
46 * Zero indicates success, anything else indicates an error.
47 */
48typedef enum {
49 /** The action was completed successfully. */
50 PSA_SUCCESS = 0,
51 /** The requested operation or a parameter is not supported
52 by this implementation. */
53 PSA_ERROR_NOT_SUPPORTED,
54 /** The requested action is denied by a policy. */
55 PSA_ERROR_NOT_PERMITTED,
56 /** An output buffer is too small. */
57 PSA_ERROR_BUFFER_TOO_SMALL,
58 /** A slot is occupied, but must be empty to carry out the
59 requested action. */
60 PSA_ERROR_OCCUPIED_SLOT,
61 /** A slot is empty, but must be occupied to carry out the
62 requested action. */
63 PSA_ERROR_EMPTY_SLOT,
64 /** The requested action cannot be performed in the current state. */
65 PSA_ERROR_BAD_STATE,
66 /** The parameters passed to the function are invalid. */
67 PSA_ERROR_INVALID_ARGUMENT,
68 /** There is not enough runtime memory. */
69 PSA_ERROR_INSUFFICIENT_MEMORY,
70 /** There is not enough persistent storage. */
71 PSA_ERROR_INSUFFICIENT_STORAGE,
72 /** There was a communication failure inside the implementation. */
73 PSA_ERROR_COMMUNICATION_FAILURE,
Gilles Peskinea5905292018-02-07 20:59:33 +010074 /** There was a storage failure that may have led to data loss. */
75 PSA_ERROR_STORAGE_FAILURE,
Gilles Peskinee59236f2018-01-27 23:32:46 +010076 /** A hardware failure was detected. */
77 PSA_ERROR_HARDWARE_FAILURE,
78 /** A tampering attempt was detected. */
79 PSA_ERROR_TAMPERING_DETECTED,
80 /** There is not enough entropy to generate random data needed
81 for the requested action. */
82 PSA_ERROR_INSUFFICIENT_ENTROPY,
Gilles Peskinea5905292018-02-07 20:59:33 +010083 /** The signature, MAC or hash is incorrect. */
Gilles Peskinee59236f2018-01-27 23:32:46 +010084 PSA_ERROR_INVALID_SIGNATURE,
Gilles Peskinea5905292018-02-07 20:59:33 +010085 /** The decrypted padding is incorrect. */
86 PSA_ERROR_INVALID_PADDING,
Gilles Peskinee59236f2018-01-27 23:32:46 +010087 /** An error occurred that does not correspond to any defined
88 failure cause. */
89 PSA_ERROR_UNKNOWN_ERROR,
90} psa_status_t;
91
92/**
93 * \brief Library initialization.
94 *
95 * Applications must call this function before calling any other
96 * function in this module.
97 *
98 * Applications may call this function more than once. Once a call
99 * succeeds, subsequent calls are guaranteed to succeed.
100 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100101 * \retval PSA_SUCCESS
102 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
103 * \retval PSA_ERROR_COMMUNICATION_FAILURE
104 * \retval PSA_ERROR_HARDWARE_FAILURE
105 * \retval PSA_ERROR_TAMPERING_DETECTED
106 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +0100107 */
108psa_status_t psa_crypto_init(void);
109
Gilles Peskine2905a7a2018-03-07 16:39:31 +0100110#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
111#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
Gilles Peskine0189e752018-02-03 23:57:22 +0100112
Gilles Peskinee59236f2018-01-27 23:32:46 +0100113/**@}*/
114
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100115/** \defgroup crypto_types Key and algorithm types
116 * @{
117 */
118
Gilles Peskine308b91d2018-02-08 09:47:44 +0100119/** \brief Encoding of a key type.
120 */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100121typedef uint32_t psa_key_type_t;
122
Gilles Peskine98f0a242018-02-06 18:57:29 +0100123#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
124#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100125
Gilles Peskine98f0a242018-02-06 18:57:29 +0100126#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7e000000)
127#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x02000000)
128#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x04000000)
129#define PSA_KEY_TYPE_CATEGORY_ASYMMETRIC ((psa_key_type_t)0x06000000)
130#define PSA_KEY_TYPE_PAIR_FLAG ((psa_key_type_t)0x01000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100131
Gilles Peskine98f0a242018-02-06 18:57:29 +0100132#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x02000001)
133#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x04000001)
134#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x04000002)
135#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x04000003)
136#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x04000004)
137
Gilles Peskine308b91d2018-02-08 09:47:44 +0100138/** RSA public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100139#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x06010000)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100140/** RSA key pair (private and public key). */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100141#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x07010000)
142#define PSA_KEY_TYPE_ECC_BASE ((psa_key_type_t)0x06030000)
143#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
144
145#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100146 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100147#define PSA_KEY_TYPE_IS_RAW_BYTES(type) \
148 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_RAW_DATA || \
149 ((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100150#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
151 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
152#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
153 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG) == \
154 PSA_KEY_TYPE_CATEGORY_ASYMMETRIC))
155#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
156 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
157 (PSA_KEY_TYPE_CATEGORY_ASYMMETRIC | PSA_KEY_TYPE_PAIR_FLAG))
Gilles Peskine0189e752018-02-03 23:57:22 +0100158#define PSA_KEY_TYPE_IS_RSA(type) \
Gilles Peskine98f0a242018-02-06 18:57:29 +0100159 (((type) & ~PSA_KEY_TYPE_PAIR_FLAG) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100160#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine98f0a242018-02-06 18:57:29 +0100161 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100162
Gilles Peskine8c9def32018-02-08 10:02:12 +0100163#define PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) \
164 ( \
165 (type) == PSA_KEY_TYPE_AES ? 16 : \
166 (type) == PSA_KEY_TYPE_DES ? 8 : \
167 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
168 0)
169
Gilles Peskine308b91d2018-02-08 09:47:44 +0100170/** \brief Encoding of a cryptographic algorithm.
171 *
172 * For algorithms that can be applied to multiple key types, this type
173 * does not encode the key type. For example, for symmetric ciphers
174 * based on a block cipher, #psa_algorithm_t encodes the block cipher
175 * mode and the padding mode while the block cipher itself is encoded
176 * via #psa_key_type_t.
177 */
Gilles Peskine20035e32018-02-03 22:44:14 +0100178typedef uint32_t psa_algorithm_t;
179
Gilles Peskine98f0a242018-02-06 18:57:29 +0100180#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
181#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
182#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
183#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
184#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
185#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
186#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
187#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
188#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
189#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100190
Gilles Peskine98f0a242018-02-06 18:57:29 +0100191#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
192 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100193/** Whether the specified algorithm is a hash algorithm.
194 *
195 * \param alg An algorithm identifier (\c PSA_ALG_XXX value)
196 *
197 * \return 1 if \c alg is a hash algorithm, 0 otherwise.
198 * This macro may return either 0 or 1 if \c alg is not a valid
199 * algorithm identifier. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100200#define PSA_ALG_IS_HASH(alg) \
201 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
202#define PSA_ALG_IS_MAC(alg) \
203 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
204#define PSA_ALG_IS_CIPHER(alg) \
205 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
206#define PSA_ALG_IS_AEAD(alg) \
207 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
208#define PSA_ALG_IS_SIGN(alg) \
209 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
210#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
211 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
212#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
213 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
214#define PSA_ALG_IS_KEY_DERIVATION(alg) \
215 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
216
217#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
218#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
219#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
220#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
221#define PSA_ALG_SHA_256_128 ((psa_algorithm_t)0x01000004)
222#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000005)
223#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000006)
224#define PSA_ALG_SHA_256_160 ((psa_algorithm_t)0x01000007)
225#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
226#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
227#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
228#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
229#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
230#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
231#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
232#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
233#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
234#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
235
Gilles Peskine8c9def32018-02-08 10:02:12 +0100236#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100237#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
238#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100239 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
240#define PSA_ALG_HMAC_HASH(hmac_alg) \
241 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
242#define PSA_ALG_IS_HMAC(alg) \
243 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
244 PSA_ALG_HMAC_BASE)
245#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
246#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
247#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
248#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
249#define PSA_ALG_IS_CIPHER_MAC(alg) \
250 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
251 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100252
Gilles Peskine8c9def32018-02-08 10:02:12 +0100253#define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100254#define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100255#define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100256#define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000)
257#define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100258#define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100259#define PSA_ALG_IS_BLOCK_CIPHER(alg) \
260 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
261 PSA_ALG_BLOCK_CIPHER_BASE)
262
Gilles Peskine98f0a242018-02-06 18:57:29 +0100263#define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100264#define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002)
265#define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003)
266#define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100267#define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t)0x04800000)
268#define PSA_ALG_CTR ((psa_algorithm_t)0x04800001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100269#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100270
Gilles Peskine8c9def32018-02-08 10:02:12 +0100271#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
272#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100273
274#define PSA_ALG_RSA_PKCS1V15_RAW ((psa_algorithm_t)0x10010000)
275#define PSA_ALG_RSA_PSS_MGF1 ((psa_algorithm_t)0x10020000)
276#define PSA_ALG_RSA_OAEP ((psa_algorithm_t)0x12020000)
277#define PSA_ALG_RSA_PKCS1V15(hash_alg) \
278 (PSA_ALG_RSA_PKCS1V15_RAW | ((hash_alg) & PSA_ALG_HASH_MASK))
279#define PSA_ALG_IS_RSA_PKCS1V15(alg) \
Gilles Peskine20035e32018-02-03 22:44:14 +0100280 (((alg) & 0x7fffff00) == PSA_ALG_RSA_PKCS1V15_RAW)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100281#define PSA_ALG_RSA_GET_HASH(alg) \
282 (((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100283
284/**@}*/
285
286/** \defgroup key_management Key management
287 * @{
288 */
289
290/**
291 * \brief Import a key in binary format.
292 *
293 * This function supports any output from psa_export_key().
294 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100295 * \param key Slot where the key will be stored. This must be a
296 * valid slot for a key of the chosen type. It must
297 * be unoccupied.
298 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
299 * \param data Buffer containing the key data.
300 * \param data_length Size of the \c data buffer in bytes.
301 *
302 * \retval PSA_SUCCESS
303 * Success.
304 * \retval PSA_ERROR_NOT_SUPPORTED
305 * The key type or key size is not supported.
306 * \retval PSA_ERROR_INVALID_ARGUMENT
307 * The key slot is invalid,
308 * or the key data is not correctly formatted.
309 * \retval PSA_ERROR_OCCUPIED_SLOT
310 There is already a key in the specified slot.
311 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
312 * \retval PSA_ERROR_COMMUNICATION_FAILURE
313 * \retval PSA_ERROR_HARDWARE_FAILURE
314 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100315 */
316psa_status_t psa_import_key(psa_key_slot_t key,
317 psa_key_type_t type,
318 const uint8_t *data,
319 size_t data_length);
320
321/**
322 * \brief Destroy a key.
323 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100324 * \retval PSA_SUCCESS
325 * \retval PSA_ERROR_EMPTY_SLOT
326 * \retval PSA_ERROR_COMMUNICATION_FAILURE
327 * \retval PSA_ERROR_HARDWARE_FAILURE
328 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100329 */
330psa_status_t psa_destroy_key(psa_key_slot_t key);
331
332/**
333 * \brief Get basic metadata about a key.
334 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100335 * \param key Slot whose content is queried. This must
336 * be an occupied key slot.
337 * \param type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
338 * This may be a null pointer, in which case the key type
339 * is not written.
340 * \param bits On success, the key size in bits.
341 * This may be a null pointer, in which case the key type
342 * is not written.
343 *
344 * \retval PSA_SUCCESS
345 * \retval PSA_ERROR_EMPTY_SLOT
346 * \retval PSA_ERROR_COMMUNICATION_FAILURE
347 * \retval PSA_ERROR_HARDWARE_FAILURE
348 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100349 */
350psa_status_t psa_get_key_information(psa_key_slot_t key,
351 psa_key_type_t *type,
352 size_t *bits);
353
354/**
355 * \brief Export a key in binary format.
356 *
357 * The output of this function can be passed to psa_import_key() to
358 * create an equivalent object.
359 *
360 * If a key is created with psa_import_key() and then exported with
361 * this function, it is not guaranteed that the resulting data is
362 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +0100363 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100364 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100365 * For standard key types, the output format is as follows:
366 *
367 * - For symmetric keys (including MAC keys), the format is the
368 * raw bytes of the key.
369 * - For DES, the key data consists of 8 bytes. The parity bits must be
370 * correct.
371 * - For Triple-DES, the format is the concatenation of the
372 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100373 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine308b91d2018-02-08 09:47:44 +0100374 * is the non-encrypted DER representation defined by PKCS\#8 (RFC 5208)
375 * as PrivateKeyInfo.
376 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
377 * is the DER representation defined by X.509.
378 *
379 * \param key Slot whose content is to be exported. This must
380 * be an occupied key slot.
381 * \param data Buffer where the key data is to be written.
382 * \param data_size Size of the \c data buffer in bytes.
383 * \param data_length On success, the number of bytes
384 * that make up the key data.
385 *
386 * \retval PSA_SUCCESS
387 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine92b30732018-03-03 21:29:30 +0100388 * \retval PSA_ERROR_NOT_PERMITTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100389 * \retval PSA_ERROR_COMMUNICATION_FAILURE
390 * \retval PSA_ERROR_HARDWARE_FAILURE
391 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100392 */
393psa_status_t psa_export_key(psa_key_slot_t key,
394 uint8_t *data,
395 size_t data_size,
396 size_t *data_length);
397
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100398/**
399 * \brief Export a public key or the public part of a key pair in binary format.
400 *
401 * The output of this function can be passed to psa_import_key() to
402 * create an object that is equivalent to the public key.
403 *
404 * For standard key types, the output format is as follows:
405 *
406 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
407 * the format is the DER representation defined by X.509.
408 *
409 * \param key Slot whose content is to be exported. This must
410 * be an occupied key slot.
411 * \param data Buffer where the key data is to be written.
412 * \param data_size Size of the \c data buffer in bytes.
413 * \param data_length On success, the number of bytes
414 * that make up the key data.
415 *
416 * \retval PSA_SUCCESS
417 * \retval PSA_ERROR_EMPTY_SLOT
418 * \retval PSA_ERROR_INVALID_ARGUMENT
419 * \retval PSA_ERROR_COMMUNICATION_FAILURE
420 * \retval PSA_ERROR_HARDWARE_FAILURE
421 * \retval PSA_ERROR_TAMPERING_DETECTED
422 */
423psa_status_t psa_export_public_key(psa_key_slot_t key,
424 uint8_t *data,
425 size_t data_size,
426 size_t *data_length);
427
428/**@}*/
429
430/** \defgroup policy Key policies
431 * @{
432 */
433
434/** \brief Encoding of permitted usage on a key. */
435typedef uint32_t psa_key_usage_t;
436
437#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
438
439#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
440#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
441#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
442#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
443
444/** The type of the key policy data structure.
445 *
446 * This is an implementation-defined \c struct. Applications should not
447 * make any assumptions about the content of this structure except
448 * as directed by the documentation of a specific implementation. */
449typedef struct psa_key_policy_s psa_key_policy_t;
450
451/** \brief Initialize a key policy structure to a default that forbids all
452 * usage of the key. */
453void psa_key_policy_init(psa_key_policy_t *policy);
454
455void psa_key_policy_set_usage(psa_key_policy_t *policy,
456 psa_key_usage_t usage,
457 psa_algorithm_t alg);
458
459psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy);
460
461psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy);
462
463/** \brief Set the usage policy on a key slot.
464 *
465 * This function must be called on an empty key slot, before importing,
466 * generating or creating a key in the slot. Changing the policy of an
467 * existing key is not permitted.
468 */
469psa_status_t psa_set_key_policy(psa_key_slot_t key,
470 const psa_key_policy_t *policy);
471
472psa_status_t psa_get_key_policy(psa_key_slot_t key,
473 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100474
475/**@}*/
476
Gilles Peskine609b6a52018-03-03 21:31:50 +0100477/** \defgroup persistence Key lifetime
478 * @{
479 */
480
481/** Encoding of key lifetimes.
482 */
483typedef uint32_t psa_key_lifetime_t;
484
485/** A volatile key slot retains its content as long as the application is
486 * running. It is guaranteed to be erased on a power reset.
487 */
488#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
489
490/** A persistent key slot retains its content as long as it is not explicitly
491 * destroyed.
492 */
493#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
494
495/** A write-once key slot may not be modified once a key has been set.
496 * It will retain its content as long as the device remains operational.
497 */
498#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
499
500psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
501 psa_key_lifetime_t *lifetime);
502
503/**@}*/
504
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100505/** \defgroup hash Message digests
506 * @{
507 */
508
Gilles Peskine308b91d2018-02-08 09:47:44 +0100509/** The type of the state data structure for multipart hash operations.
510 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100511 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100512 * make any assumptions about the content of this structure except
513 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100514typedef struct psa_hash_operation_s psa_hash_operation_t;
515
Gilles Peskine308b91d2018-02-08 09:47:44 +0100516/** The size of the output of psa_hash_finish(), in bytes.
517 *
518 * This is also the hash size that psa_hash_verify() expects.
519 *
520 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
521 * #PSA_ALG_IS_HASH(alg) is true).
522 *
523 * \return The hash size for the specified hash algorithm.
524 * If the hash algorithm is not recognized, return 0.
525 * An implementation may return either 0 or the correct size
526 * for a hash algorithm that it recognizes, but does not support.
527 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100528#define PSA_HASH_FINAL_SIZE(alg) \
529 ( \
530 (alg) == PSA_ALG_MD2 ? 16 : \
531 (alg) == PSA_ALG_MD4 ? 16 : \
532 (alg) == PSA_ALG_MD5 ? 16 : \
533 (alg) == PSA_ALG_SHA_256_128 ? 16 : \
534 (alg) == PSA_ALG_RIPEMD160 ? 20 : \
535 (alg) == PSA_ALG_SHA_1 ? 20 : \
536 (alg) == PSA_ALG_SHA_256_160 ? 20 : \
537 (alg) == PSA_ALG_SHA_224 ? 28 : \
538 (alg) == PSA_ALG_SHA_256 ? 32 : \
539 (alg) == PSA_ALG_SHA_384 ? 48 : \
540 (alg) == PSA_ALG_SHA_512 ? 64 : \
541 (alg) == PSA_ALG_SHA_512_224 ? 28 : \
542 (alg) == PSA_ALG_SHA_512_256 ? 32 : \
543 (alg) == PSA_ALG_SHA3_224 ? 28 : \
544 (alg) == PSA_ALG_SHA3_256 ? 32 : \
545 (alg) == PSA_ALG_SHA3_384 ? 48 : \
546 (alg) == PSA_ALG_SHA3_512 ? 64 : \
547 0)
548
Gilles Peskine308b91d2018-02-08 09:47:44 +0100549/** Start a multipart hash operation.
550 *
551 * The sequence of operations to calculate a hash (message digest)
552 * is as follows:
553 * -# Allocate an operation object which will be passed to all the functions
554 * listed here.
555 * -# Call psa_hash_start() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100556 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100557 * of the message each time. The hash that is calculated is the hash
558 * of the concatenation of these messages in order.
559 * -# To calculate the hash, call psa_hash_finish().
560 * To compare the hash with an expected value, call psa_hash_verify().
561 *
562 * The application may call psa_hash_abort() at any time after the operation
563 * has been initialized with psa_hash_start().
564 *
565 * After a successful call to psa_hash_start(), the application must
566 * eventually destroy the operation through one of the following means:
567 * - A failed call to psa_hash_update().
568 * - A call to psa_hash_final(), psa_hash_verify() or psa_hash_abort().
569 *
570 * \param operation
571 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
572 * such that #PSA_ALG_IS_HASH(alg) is true).
573 *
574 * \retval PSA_SUCCESS
575 * Success.
576 * \retval PSA_ERROR_NOT_SUPPORTED
577 * \c alg is not supported or is not a hash algorithm.
578 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
579 * \retval PSA_ERROR_COMMUNICATION_FAILURE
580 * \retval PSA_ERROR_HARDWARE_FAILURE
581 * \retval PSA_ERROR_TAMPERING_DETECTED
582 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100583psa_status_t psa_hash_start(psa_hash_operation_t *operation,
584 psa_algorithm_t alg);
585
Gilles Peskine308b91d2018-02-08 09:47:44 +0100586/** Add a message fragment to a multipart hash operation.
587 *
588 * The application must call psa_hash_start() before calling this function.
589 *
590 * If this function returns an error status, the operation becomes inactive.
591 *
592 * \param operation Active hash operation.
593 * \param input Buffer containing the message fragment to hash.
594 * \param input_length Size of the \c input buffer in bytes.
595 *
596 * \retval PSA_SUCCESS
597 * Success.
598 * \retval PSA_ERROR_BAD_STATE
599 * The operation state is not valid (not started, or already completed).
600 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
601 * \retval PSA_ERROR_COMMUNICATION_FAILURE
602 * \retval PSA_ERROR_HARDWARE_FAILURE
603 * \retval PSA_ERROR_TAMPERING_DETECTED
604 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100605psa_status_t psa_hash_update(psa_hash_operation_t *operation,
606 const uint8_t *input,
607 size_t input_length);
608
Gilles Peskine308b91d2018-02-08 09:47:44 +0100609/** Finish the calculation of the hash of a message.
610 *
611 * The application must call psa_hash_start() before calling this function.
612 * This function calculates the hash of the message formed by concatenating
613 * the inputs passed to preceding calls to psa_hash_update().
614 *
615 * When this function returns, the operation becomes inactive.
616 *
617 * \warning Applications should not call this function if they expect
618 * a specific value for the hash. Call psa_hash_verify() instead.
619 * Beware that comparing integrity or authenticity data such as
620 * hash values with a function such as \c memcmp is risky
621 * because the time taken by the comparison may leak information
622 * about the hashed data which could allow an attacker to guess
623 * a valid hash and thereby bypass security controls.
624 *
625 * \param operation Active hash operation.
626 * \param hash Buffer where the hash is to be written.
627 * \param hash_size Size of the \c hash buffer in bytes.
628 * \param hash_length On success, the number of bytes
629 * that make up the hash value. This is always
630 * #PSA_HASH_FINAL_SIZE(alg) where \c alg is the
631 * hash algorithm that is calculated.
632 *
633 * \retval PSA_SUCCESS
634 * Success.
635 * \retval PSA_ERROR_BAD_STATE
636 * The operation state is not valid (not started, or already completed).
637 * \retval PSA_ERROR_BUFFER_TOO_SMALL
638 * The size of the \c hash buffer is too small. You can determine a
639 * sufficient buffer size by calling #PSA_HASH_FINAL_SIZE(alg)
640 * where \c alg is the hash algorithm that is calculated.
641 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
642 * \retval PSA_ERROR_COMMUNICATION_FAILURE
643 * \retval PSA_ERROR_HARDWARE_FAILURE
644 * \retval PSA_ERROR_TAMPERING_DETECTED
645 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100646psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
647 uint8_t *hash,
648 size_t hash_size,
649 size_t *hash_length);
650
Gilles Peskine308b91d2018-02-08 09:47:44 +0100651/** Finish the calculation of the hash of a message and compare it with
652 * an expected value.
653 *
654 * The application must call psa_hash_start() before calling this function.
655 * This function calculates the hash of the message formed by concatenating
656 * the inputs passed to preceding calls to psa_hash_update(). It then
657 * compares the calculated hash with the expected hash passed as a
658 * parameter to this function.
659 *
660 * When this function returns, the operation becomes inactive.
661 *
662 * \note Applications shall make the best effort to ensure that the
663 * comparison between the actual hash and the expected hash is performed
664 * in constant time.
665 *
666 * \param operation Active hash operation.
667 * \param hash Buffer containing the expected hash value.
668 * \param hash_length Size of the \c hash buffer in bytes.
669 *
670 * \retval PSA_SUCCESS
671 * The expected hash is identical to the actual hash of the message.
672 * \retval PSA_ERROR_INVALID_SIGNATURE
673 * The hash of the message was calculated successfully, but it
674 * differs from the expected hash.
675 * \retval PSA_ERROR_BAD_STATE
676 * The operation state is not valid (not started, or already completed).
677 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
678 * \retval PSA_ERROR_COMMUNICATION_FAILURE
679 * \retval PSA_ERROR_HARDWARE_FAILURE
680 * \retval PSA_ERROR_TAMPERING_DETECTED
681 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100682psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
683 const uint8_t *hash,
684 size_t hash_length);
685
Gilles Peskine308b91d2018-02-08 09:47:44 +0100686/** Abort a hash operation.
687 *
688 * This function may be called at any time after psa_hash_start().
689 * Aborting an operation frees all associated resources except for the
690 * \c operation structure itself.
691 *
692 * Implementation should strive to be robust and handle inactive hash
693 * operations safely (do nothing and return #PSA_ERROR_BAD_STATE). However,
694 * application writers should beware that uninitialized memory may happen
695 * to be indistinguishable from an active hash operation, and the behavior
696 * of psa_hash_abort() is undefined in this case.
697 *
698 * \param operation Active hash operation.
699 *
700 * \retval PSA_SUCCESS
701 * \retval PSA_ERROR_BAD_STATE
702 * \c operation is not an active hash operation.
703 * \retval PSA_ERROR_COMMUNICATION_FAILURE
704 * \retval PSA_ERROR_HARDWARE_FAILURE
705 * \retval PSA_ERROR_TAMPERING_DETECTED
706 */
707psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100708
709/**@}*/
710
Gilles Peskine8c9def32018-02-08 10:02:12 +0100711/** \defgroup MAC Message authentication codes
712 * @{
713 */
714
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100715/** The type of the state data structure for multipart MAC operations.
716 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100717 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100718 * make any assumptions about the content of this structure except
719 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100720typedef struct psa_mac_operation_s psa_mac_operation_t;
721
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100722/** The size of the output of psa_mac_finish(), in bytes.
723 *
724 * This is also the MAC size that psa_mac_verify() expects.
725 *
726 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
727 * #PSA_ALG_IS_MAC(alg) is true).
728 *
729 * \return The MAC size for the specified algorithm.
730 * If the MAC algorithm is not recognized, return 0.
731 * An implementation may return either 0 or the correct size
732 * for a MAC algorithm that it recognizes, but does not support.
733 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100734#define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \
735 (PSA_ALG_IS_HMAC(alg) ? PSA_HASH_FINAL_SIZE(PSA_ALG_HMAC_HASH(alg)) : \
736 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
737 0)
738
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100739/** Start a multipart MAC operation.
740 *
741 * The sequence of operations to calculate a MAC (message authentication code)
742 * is as follows:
743 * -# Allocate an operation object which will be passed to all the functions
744 * listed here.
745 * -# Call psa_mac_start() to specify the algorithm and key.
746 * The key remains associated with the operation even if the content
747 * of the key slot changes.
748 * -# Call psa_mac_update() zero, one or more times, passing a fragment
749 * of the message each time. The MAC that is calculated is the MAC
750 * of the concatenation of these messages in order.
751 * -# To calculate the MAC, call psa_mac_finish().
752 * To compare the MAC with an expected value, call psa_mac_verify().
753 *
754 * The application may call psa_mac_abort() at any time after the operation
755 * has been initialized with psa_mac_start().
756 *
757 * After a successful call to psa_mac_start(), the application must
758 * eventually destroy the operation through one of the following means:
759 * - A failed call to psa_mac_update().
760 * - A call to psa_mac_final(), psa_mac_verify() or psa_mac_abort().
761 *
762 * \param operation
763 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
764 * such that #PSA_ALG_IS_MAC(alg) is true).
765 *
766 * \retval PSA_SUCCESS
767 * Success.
768 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine92b30732018-03-03 21:29:30 +0100769 * \retval PSA_ERROR_NOT_PERMITTED
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100770 * \retval PSA_ERROR_INVALID_ARGUMENT
771 * \c key is not compatible with \c alg.
772 * \retval PSA_ERROR_NOT_SUPPORTED
773 * \c alg is not supported or is not a MAC algorithm.
774 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
775 * \retval PSA_ERROR_COMMUNICATION_FAILURE
776 * \retval PSA_ERROR_HARDWARE_FAILURE
777 * \retval PSA_ERROR_TAMPERING_DETECTED
778 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100779psa_status_t psa_mac_start(psa_mac_operation_t *operation,
780 psa_key_slot_t key,
781 psa_algorithm_t alg);
782
783psa_status_t psa_mac_update(psa_mac_operation_t *operation,
784 const uint8_t *input,
785 size_t input_length);
786
787psa_status_t psa_mac_finish(psa_mac_operation_t *operation,
788 uint8_t *mac,
789 size_t mac_size,
790 size_t *mac_length);
791
792psa_status_t psa_mac_verify(psa_mac_operation_t *operation,
793 const uint8_t *mac,
794 size_t mac_length);
795
796psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
797
798/**@}*/
799
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100800/** \defgroup cipher Symmetric ciphers
801 * @{
802 */
803
804/** The type of the state data structure for multipart cipher operations.
805 *
806 * This is an implementation-defined \c struct. Applications should not
807 * make any assumptions about the content of this structure except
808 * as directed by the documentation of a specific implementation. */
809typedef struct psa_cipher_operation_s psa_cipher_operation_t;
810
811/** Set the key for a multipart symmetric encryption operation.
812 *
813 * The sequence of operations to encrypt a message with a symmetric cipher
814 * is as follows:
815 * -# Allocate an operation object which will be passed to all the functions
816 * listed here.
817 * -# Call psa_encrypt_setup() to specify the algorithm and key.
818 * The key remains associated with the operation even if the content
819 * of the key slot changes.
820 * -# Call either psa_encrypt_generate_iv() or psa_encrypt_set_iv() to
821 * generate or set the IV (initialization vector). You should use
822 * psa_encrypt_generate_iv() unless the protocol you are implementing
823 * requires a specific IV value.
824 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
825 * of the message each time.
826 * -# Call psa_cipher_finish().
827 *
828 * The application may call psa_cipher_abort() at any time after the operation
829 * has been initialized with psa_encrypt_setup().
830 *
831 * After a successful call to psa_encrypt_setup(), the application must
832 * eventually destroy the operation through one of the following means:
833 * - A failed call to psa_encrypt_generate_iv(), psa_encrypt_set_iv()
834 * or psa_cipher_update().
835 * - A call to psa_cipher_final() or psa_cipher_abort().
836 *
837 * \param operation
838 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
839 * such that #PSA_ALG_IS_CIPHER(alg) is true).
840 *
841 * \retval PSA_SUCCESS
842 * Success.
843 * \retval PSA_ERROR_EMPTY_SLOT
844 * \retval PSA_ERROR_NOT_PERMITTED
845 * \retval PSA_ERROR_INVALID_ARGUMENT
846 * \c key is not compatible with \c alg.
847 * \retval PSA_ERROR_NOT_SUPPORTED
848 * \c alg is not supported or is not a cipher algorithm.
849 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
850 * \retval PSA_ERROR_COMMUNICATION_FAILURE
851 * \retval PSA_ERROR_HARDWARE_FAILURE
852 * \retval PSA_ERROR_TAMPERING_DETECTED
853 */
854psa_status_t psa_encrypt_setup(psa_cipher_operation_t *operation,
855 psa_key_slot_t key,
856 psa_algorithm_t alg);
857
858/** Set the key for a multipart symmetric decryption operation.
859 *
860 * The sequence of operations to decrypt a message with a symmetric cipher
861 * is as follows:
862 * -# Allocate an operation object which will be passed to all the functions
863 * listed here.
864 * -# Call psa_decrypt_setup() to specify the algorithm and key.
865 * The key remains associated with the operation even if the content
866 * of the key slot changes.
867 * -# Call psa_cipher_update() with the IV (initialization vector) for the
868 * decryption. If the IV is prepended to the ciphertext, you can call
869 * psa_cipher_update() on a buffer containing the IV followed by the
870 * beginning of the message.
871 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
872 * of the message each time.
873 * -# Call psa_cipher_finish().
874 *
875 * The application may call psa_cipher_abort() at any time after the operation
876 * has been initialized with psa_encrypt_setup().
877 *
878 * After a successful call to psa_decrypt_setup(), the application must
879 * eventually destroy the operation through one of the following means:
880 * - A failed call to psa_cipher_update().
881 * - A call to psa_cipher_final() or psa_cipher_abort().
882 *
883 * \param operation
884 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
885 * such that #PSA_ALG_IS_CIPHER(alg) is true).
886 *
887 * \retval PSA_SUCCESS
888 * Success.
889 * \retval PSA_ERROR_EMPTY_SLOT
890 * \retval PSA_ERROR_NOT_PERMITTED
891 * \retval PSA_ERROR_INVALID_ARGUMENT
892 * \c key is not compatible with \c alg.
893 * \retval PSA_ERROR_NOT_SUPPORTED
894 * \c alg is not supported or is not a cipher algorithm.
895 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
896 * \retval PSA_ERROR_COMMUNICATION_FAILURE
897 * \retval PSA_ERROR_HARDWARE_FAILURE
898 * \retval PSA_ERROR_TAMPERING_DETECTED
899 */
900psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation,
901 psa_key_slot_t key,
902 psa_algorithm_t alg);
903
904psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation,
905 unsigned char *iv,
906 size_t iv_size,
907 size_t *iv_length);
908
909psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation,
910 const unsigned char *iv,
911 size_t iv_length);
912
913psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
914 const uint8_t *input,
915 size_t input_length);
916
917psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
918 uint8_t *mac,
919 size_t mac_size,
920 size_t *mac_length);
921
922psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
923
924/**@}*/
925
Gilles Peskine3b555712018-03-03 21:27:57 +0100926/** \defgroup aead Authenticated encryption with associated data (AEAD)
927 * @{
928 */
929
930/** The type of the state data structure for multipart AEAD operations.
931 *
932 * This is an implementation-defined \c struct. Applications should not
933 * make any assumptions about the content of this structure except
934 * as directed by the documentation of a specific implementation. */
935typedef struct psa_aead_operation_s psa_aead_operation_t;
936
937/** Set the key for a multipart authenticated encryption operation.
938 *
939 * The sequence of operations to authenticate-and-encrypt a message
940 * is as follows:
941 * -# Allocate an operation object which will be passed to all the functions
942 * listed here.
943 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
944 * The key remains associated with the operation even if the content
945 * of the key slot changes.
946 * -# Call either psa_aead_generate_iv() or psa_aead_set_iv() to
947 * generate or set the IV (initialization vector). You should use
948 * psa_encrypt_generate_iv() unless the protocol you are implementing
949 * requires a specific IV value.
950 * -# Call psa_aead_update_ad() to pass the associated data that is
951 * to be authenticated but not encrypted. You may omit this step if
952 * there is no associated data.
953 * -# Call psa_aead_update() zero, one or more times, passing a fragment
954 * of the data to encrypt each time.
955 * -# Call psa_aead_finish().
956 *
957 * The application may call psa_aead_abort() at any time after the operation
958 * has been initialized with psa_aead_encrypt_setup().
959 *
960 * After a successful call to psa_aead_setup(), the application must
961 * eventually destroy the operation through one of the following means:
962 * - A failed call to psa_aead_generate_iv(), psa_aead_set_iv(),
963 * psa_aead_update_ad() or psa_aead_update().
964 * - A call to psa_aead_final() or psa_aead_abort().
965 *
966 * \param operation
967 * \param alg The AEAD algorithm to compute (\c PSA_ALG_XXX value
968 * such that #PSA_ALG_IS_AEAD(alg) is true).
969 *
970 * \retval PSA_SUCCESS
971 * Success.
972 * \retval PSA_ERROR_EMPTY_SLOT
973 * \retval PSA_ERROR_NOT_PERMITTED
974 * \retval PSA_ERROR_INVALID_ARGUMENT
975 * \c key is not compatible with \c alg.
976 * \retval PSA_ERROR_NOT_SUPPORTED
977 * \c alg is not supported or is not an AEAD algorithm.
978 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
979 * \retval PSA_ERROR_COMMUNICATION_FAILURE
980 * \retval PSA_ERROR_HARDWARE_FAILURE
981 * \retval PSA_ERROR_TAMPERING_DETECTED
982 */
983psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
984 psa_key_slot_t key,
985 psa_algorithm_t alg);
986
987/** Set the key for a multipart authenticated decryption operation.
988 *
989 * The sequence of operations to authenticated and decrypt a message
990 * is as follows:
991 * -# Allocate an operation object which will be passed to all the functions
992 * listed here.
993 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
994 * The key remains associated with the operation even if the content
995 * of the key slot changes.
996 * -# Call psa_aead_set_iv() to pass the initialization vector (IV)
997 * for the authenticated decryption.
998 * -# Call psa_aead_update_ad() to pass the associated data that is
999 * to be authenticated but not encrypted. You may omit this step if
1000 * there is no associated data.
1001 * -# Call psa_aead_update() zero, one or more times, passing a fragment
1002 * of the data to decrypt each time.
1003 * -# Call psa_aead_finish().
1004 *
1005 * The application may call psa_aead_abort() at any time after the operation
1006 * has been initialized with psa_aead_decrypt_setup().
1007 *
1008 * After a successful call to psa_decrypt_setup(), the application must
1009 * eventually destroy the operation through one of the following means:
1010 * - A failed call to psa_aead_update().
1011 * - A call to psa_cipher_final() or psa_cipher_abort().
1012 *
1013 * \param operation
1014 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
1015 * such that #PSA_ALG_IS_CIPHER(alg) is true).
1016 *
1017 * \retval PSA_SUCCESS
1018 * Success.
1019 * \retval PSA_ERROR_EMPTY_SLOT
1020 * \retval PSA_ERROR_NOT_PERMITTED
1021 * \retval PSA_ERROR_INVALID_ARGUMENT
1022 * \c key is not compatible with \c alg.
1023 * \retval PSA_ERROR_NOT_SUPPORTED
1024 * \c alg is not supported or is not a cipher algorithm.
1025 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1026 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1027 * \retval PSA_ERROR_HARDWARE_FAILURE
1028 * \retval PSA_ERROR_TAMPERING_DETECTED
1029 */
1030psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
1031 psa_key_slot_t key,
1032 psa_algorithm_t alg);
1033
1034psa_status_t psa_aead_generate_iv(psa_aead_operation_t *operation,
1035 unsigned char *iv,
1036 size_t iv_size,
1037 size_t *iv_length);
1038
1039psa_status_t psa_aead_set_iv(psa_aead_operation_t *operation,
1040 const unsigned char *iv,
1041 size_t iv_length);
1042
1043psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
1044 const uint8_t *input,
1045 size_t input_length);
1046
1047psa_status_t psa_aead_update(psa_aead_operation_t *operation,
1048 const uint8_t *input,
1049 size_t input_length);
1050
1051psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
1052 uint8_t *tag,
1053 size_t tag_size,
1054 size_t *tag_length);
1055
1056psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
1057 uint8_t *tag,
1058 size_t tag_length);
1059
1060psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
1061
1062/**@}*/
1063
Gilles Peskine20035e32018-02-03 22:44:14 +01001064/** \defgroup asymmetric Asymmetric cryptography
1065 * @{
1066 */
1067
1068/**
Gilles Peskine0189e752018-02-03 23:57:22 +01001069 * \brief Maximum ECDSA signature size for a given curve bit size
1070 *
1071 * \param curve_bits Curve size in bits
1072 * \return Maximum signature size in bytes
1073 *
1074 * \note This macro returns a compile-time constant if its argument is one.
1075 *
1076 * \warning This macro may evaluate its argument multiple times.
1077 */
1078/*
1079 * RFC 4492 page 20:
1080 *
1081 * Ecdsa-Sig-Value ::= SEQUENCE {
1082 * r INTEGER,
1083 * s INTEGER
1084 * }
1085 *
1086 * Size is at most
1087 * 1 (tag) + 1 (len) + 1 (initial 0) + curve_bytes for each of r and s,
1088 * twice that + 1 (tag) + 2 (len) for the sequence
1089 * (assuming curve_bytes is less than 126 for r and s,
1090 * and less than 124 (total len <= 255) for the sequence)
1091 */
1092#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
1093 ( /*T,L of SEQUENCE*/ ((curve_bits) >= 61 * 8 ? 3 : 2) + \
1094 /*T,L of r,s*/ 2 * (((curve_bits) >= 127 * 8 ? 3 : 2) + \
1095 /*V of r,s*/ ((curve_bits) + 8) / 8))
1096
1097
Gilles Peskine308b91d2018-02-08 09:47:44 +01001098/** Safe signature buffer size for psa_asymmetric_sign().
1099 *
1100 * This macro returns a safe buffer size for a signature using a key
1101 * of the specified type and size, with the specified algorithm.
1102 * Note that the actual size of the signature may be smaller
1103 * (some algorithms produce a variable-size signature).
1104 *
1105 * \warning This function may call its arguments multiple times or
1106 * zero times, so you should not pass arguments that contain
1107 * side effects.
1108 *
1109 * \param key_type An asymmetric key type (this may indifferently be a
1110 * key pair type or a public key type).
1111 * \param key_bits The size of the key in bits.
1112 * \param alg The signature algorithm.
1113 *
1114 * \return If the parameters are valid and supported, return
1115 * a buffer size in bytes that guarantees that
1116 * psa_asymmetric_sign() will not fail with
1117 * #PSA_ERROR_BUFFER_TOO_SMALL.
1118 * If the parameters are a valid combination that is not supported
1119 * by the implementation, this macro either shall return either a
1120 * sensible size or 0.
1121 * If the parameters are not valid, the
1122 * return value is unspecified.
1123 *
1124 */
Gilles Peskine0189e752018-02-03 23:57:22 +01001125#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine2905a7a2018-03-07 16:39:31 +01001126 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine0189e752018-02-03 23:57:22 +01001127 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
1128 0)
1129
1130/**
Gilles Peskine20035e32018-02-03 22:44:14 +01001131 * \brief Sign a hash or short message with a private key.
1132 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001133 * \param key Key slot containing an asymmetric key pair.
1134 * \param alg A signature algorithm that is compatible with
1135 * the type of \c key.
1136 * \param hash The message to sign.
1137 * \param hash_length Size of the \c hash buffer in bytes.
1138 * \param salt A salt or label, if supported by the signature
1139 * algorithm.
1140 * If the signature algorithm does not support a
1141 * salt, pass \c NULL.
1142 * If the signature algorithm supports an optional
1143 * salt and you do not want to pass a salt,
1144 * pass \c NULL.
1145 * \param salt_length Size of the \c salt buffer in bytes.
1146 * If \c salt is \c NULL, pass 0.
1147 * \param signature Buffer where the signature is to be written.
1148 * \param signature_size Size of the \c signature buffer in bytes.
1149 * \param signature_length On success, the number of bytes
1150 * that make up the returned signature value.
1151 * This is at most #PSA_HASH_FINAL_SIZE(alg)
1152 * (note that it may be less).
1153 *
1154 * \retval PSA_SUCCESS
1155 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1156 * The size of the \c signature buffer is too small. You can
1157 * determine a sufficient buffer size by calling
1158 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg)
1159 * where \c key_type and \c key_bits are the type and bit-size
1160 * respectively of \c key.
1161 * \retval PSA_ERROR_NOT_SUPPORTED
1162 * \retval PSA_ERROR_INVALID_ARGUMENT
1163 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1164 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1165 * \retval PSA_ERROR_HARDWARE_FAILURE
1166 * \retval PSA_ERROR_TAMPERING_DETECTED
1167 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01001168 */
1169psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
1170 psa_algorithm_t alg,
1171 const uint8_t *hash,
1172 size_t hash_length,
1173 const uint8_t *salt,
1174 size_t salt_length,
1175 uint8_t *signature,
1176 size_t signature_size,
1177 size_t *signature_length);
1178
1179/**
1180 * \brief Verify the signature a hash or short message using a public key.
1181 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001182 * \param key Key slot containing a public key or an
1183 * asymmetric key pair.
1184 * \param alg A signature algorithm that is compatible with
1185 * the type of \c key.
1186 * \param hash The message whose signature is to be verified.
1187 * \param hash_length Size of the \c hash buffer in bytes.
1188 * \param salt A salt or label, if supported by the signature
1189 * algorithm.
1190 * If the signature algorithm does not support a
1191 * salt, pass \c NULL.
1192 * If the signature algorithm supports an optional
1193 * salt and you do not want to pass a salt,
1194 * pass \c NULL.
1195 * \param salt_length Size of the \c salt buffer in bytes.
1196 * If \c salt is \c NULL, pass 0.
1197 * \param signature Buffer containing the signature to verify.
1198 * \param signature_size Size of the \c signature buffer in bytes.
1199 *
1200 * \retval PSA_SUCCESS
1201 * The signature is valid.
1202 * \retval PSA_ERROR_INVALID_SIGNATURE
1203 * The calculation was perfomed successfully, but the passed
1204 * signature is not a valid signature.
1205 * \retval PSA_ERROR_NOT_SUPPORTED
1206 * \retval PSA_ERROR_INVALID_ARGUMENT
1207 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1208 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1209 * \retval PSA_ERROR_HARDWARE_FAILURE
1210 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01001211 */
1212psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
1213 psa_algorithm_t alg,
1214 const uint8_t *hash,
1215 size_t hash_length,
1216 const uint8_t *salt,
1217 size_t salt_length,
1218 uint8_t *signature,
1219 size_t signature_size);
1220
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001221/**@}*/
1222
Gilles Peskinee59236f2018-01-27 23:32:46 +01001223#ifdef __cplusplus
1224}
1225#endif
1226
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001227/* The file "crypto_struct.h" contains definitions for
1228 * implementation-specific structs that are declared above. */
1229#include "crypto_struct.h"
1230
1231/* The file "crypto_extra.h" contains vendor-specific definitions. This
1232 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01001233#include "crypto_extra.h"
1234
1235#endif /* PSA_CRYPTO_H */