blob: 5edc04fcdb6b5f150469c31945af568f8395aa20 [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
57 by this implementation. */
58 PSA_ERROR_NOT_SUPPORTED,
59 /** The requested action is denied by a policy. */
60 PSA_ERROR_NOT_PERMITTED,
61 /** An output buffer is too small. */
62 PSA_ERROR_BUFFER_TOO_SMALL,
63 /** A slot is occupied, but must be empty to carry out the
64 requested action. */
65 PSA_ERROR_OCCUPIED_SLOT,
66 /** A slot is empty, but must be occupied to carry out the
67 requested action. */
68 PSA_ERROR_EMPTY_SLOT,
69 /** The requested action cannot be performed in the current state. */
70 PSA_ERROR_BAD_STATE,
71 /** The parameters passed to the function are invalid. */
72 PSA_ERROR_INVALID_ARGUMENT,
73 /** There is not enough runtime memory. */
74 PSA_ERROR_INSUFFICIENT_MEMORY,
75 /** There is not enough persistent storage. */
76 PSA_ERROR_INSUFFICIENT_STORAGE,
77 /** There was a communication failure inside the implementation. */
78 PSA_ERROR_COMMUNICATION_FAILURE,
Gilles Peskinea5905292018-02-07 20:59:33 +010079 /** There was a storage failure that may have led to data loss. */
80 PSA_ERROR_STORAGE_FAILURE,
Gilles Peskinee59236f2018-01-27 23:32:46 +010081 /** A hardware failure was detected. */
82 PSA_ERROR_HARDWARE_FAILURE,
83 /** A tampering attempt was detected. */
84 PSA_ERROR_TAMPERING_DETECTED,
85 /** There is not enough entropy to generate random data needed
86 for the requested action. */
87 PSA_ERROR_INSUFFICIENT_ENTROPY,
Gilles Peskinea5905292018-02-07 20:59:33 +010088 /** The signature, MAC or hash is incorrect. */
Gilles Peskinee59236f2018-01-27 23:32:46 +010089 PSA_ERROR_INVALID_SIGNATURE,
Gilles Peskinea5905292018-02-07 20:59:33 +010090 /** The decrypted padding is incorrect. */
91 PSA_ERROR_INVALID_PADDING,
Gilles Peskinee59236f2018-01-27 23:32:46 +010092 /** An error occurred that does not correspond to any defined
93 failure cause. */
94 PSA_ERROR_UNKNOWN_ERROR,
95} psa_status_t;
96
97/**
98 * \brief Library initialization.
99 *
100 * Applications must call this function before calling any other
101 * function in this module.
102 *
103 * Applications may call this function more than once. Once a call
104 * succeeds, subsequent calls are guaranteed to succeed.
105 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100106 * \retval PSA_SUCCESS
107 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
108 * \retval PSA_ERROR_COMMUNICATION_FAILURE
109 * \retval PSA_ERROR_HARDWARE_FAILURE
110 * \retval PSA_ERROR_TAMPERING_DETECTED
111 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +0100112 */
113psa_status_t psa_crypto_init(void);
114
Gilles Peskine2905a7a2018-03-07 16:39:31 +0100115#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
116#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
Gilles Peskine0189e752018-02-03 23:57:22 +0100117
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118/**@}*/
119
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100120/** \defgroup crypto_types Key and algorithm types
121 * @{
122 */
123
Gilles Peskine308b91d2018-02-08 09:47:44 +0100124/** \brief Encoding of a key type.
125 */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100126typedef uint32_t psa_key_type_t;
127
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100128/** An invalid key type value.
129 *
130 * Zero is not the encoding of any key type.
131 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100132#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100133
134/** Vendor-defined flag
135 *
136 * Key types defined by this standard will never have the
137 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
138 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
139 * respect the bitwise structure used by standard encodings whenever practical.
140 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100141#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100142
Gilles Peskine98f0a242018-02-06 18:57:29 +0100143#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7e000000)
144#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x02000000)
145#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x04000000)
146#define PSA_KEY_TYPE_CATEGORY_ASYMMETRIC ((psa_key_type_t)0x06000000)
147#define PSA_KEY_TYPE_PAIR_FLAG ((psa_key_type_t)0x01000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100148
Gilles Peskine98f0a242018-02-06 18:57:29 +0100149#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x02000001)
150#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x04000001)
151#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x04000002)
152#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x04000003)
153#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x04000004)
154
Gilles Peskine308b91d2018-02-08 09:47:44 +0100155/** RSA public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100156#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x06010000)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100157/** RSA key pair (private and public key). */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100158#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x07010000)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100159/** DSA public key. */
160#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x06020000)
161/** DSA key pair (private and public key). */
162#define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x07020000)
163#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x06030000)
164#define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x07030000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100165#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100166#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
167 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
168#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
169 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
Gilles Peskine98f0a242018-02-06 18:57:29 +0100170
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100171/** Whether a key type is vendor-defined. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100172#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100173 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100174#define PSA_KEY_TYPE_IS_RAW_BYTES(type) \
175 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_RAW_DATA || \
176 ((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100177
178/** Whether a key type is asymmetric: either a key pair or a public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100179#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
180 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100181/** Whether a key type is the public part of a key pair. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100182#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
183 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG) == \
184 PSA_KEY_TYPE_CATEGORY_ASYMMETRIC))
Gilles Peskine06dc2632018-03-08 07:47:25 +0100185/** Whether a key type is a key pair containing a private part and a public
186 * part. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100187#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
188 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
189 (PSA_KEY_TYPE_CATEGORY_ASYMMETRIC | PSA_KEY_TYPE_PAIR_FLAG))
Gilles Peskine06dc2632018-03-08 07:47:25 +0100190/** Whether a key type is an RSA key pair or public key. */
191/** The key pair type corresponding to a public key type. */
192#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
193 ((type) | PSA_KEY_TYPE_PAIR_FLAG)
194/** The public key type corresponding to a key pair type. */
195#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
196 ((type) & ~PSA_KEY_TYPE_PAIR_FLAG)
Gilles Peskine0189e752018-02-03 23:57:22 +0100197#define PSA_KEY_TYPE_IS_RSA(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100198 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
199/** Whether a key type is an elliptic curve key pair or public key. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100200#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100201 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
202 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100203
Gilles Peskine03182e92018-03-07 16:40:52 +0100204#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100205 ( \
206 (type) == PSA_KEY_TYPE_AES ? 16 : \
207 (type) == PSA_KEY_TYPE_DES ? 8 : \
208 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
209 0)
210
Gilles Peskine308b91d2018-02-08 09:47:44 +0100211/** \brief Encoding of a cryptographic algorithm.
212 *
213 * For algorithms that can be applied to multiple key types, this type
214 * does not encode the key type. For example, for symmetric ciphers
215 * based on a block cipher, #psa_algorithm_t encodes the block cipher
216 * mode and the padding mode while the block cipher itself is encoded
217 * via #psa_key_type_t.
218 */
Gilles Peskine20035e32018-02-03 22:44:14 +0100219typedef uint32_t psa_algorithm_t;
220
Gilles Peskine98f0a242018-02-06 18:57:29 +0100221#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
222#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
223#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
224#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
225#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
226#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
227#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
228#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
229#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
230#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100231
Gilles Peskine98f0a242018-02-06 18:57:29 +0100232#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
233 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100234/** Whether the specified algorithm is a hash algorithm.
235 *
236 * \param alg An algorithm identifier (\c PSA_ALG_XXX value)
237 *
238 * \return 1 if \c alg is a hash algorithm, 0 otherwise.
239 * This macro may return either 0 or 1 if \c alg is not a valid
240 * algorithm identifier. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100241#define PSA_ALG_IS_HASH(alg) \
242 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
243#define PSA_ALG_IS_MAC(alg) \
244 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
245#define PSA_ALG_IS_CIPHER(alg) \
246 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
247#define PSA_ALG_IS_AEAD(alg) \
248 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
249#define PSA_ALG_IS_SIGN(alg) \
250 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
251#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
252 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
253#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
254 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
255#define PSA_ALG_IS_KEY_DERIVATION(alg) \
256 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
257
258#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
259#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
260#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
261#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
Gilles Peskinee3f694f2018-03-08 07:48:40 +0100262#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
263#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100264#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
265#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
266#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
267#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
268#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
269#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
270#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
271#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
272#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
273#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
274
Gilles Peskine8c9def32018-02-08 10:02:12 +0100275#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100276#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
277#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100278 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
279#define PSA_ALG_HMAC_HASH(hmac_alg) \
280 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
281#define PSA_ALG_IS_HMAC(alg) \
282 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
283 PSA_ALG_HMAC_BASE)
284#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
285#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
286#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
287#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
288#define PSA_ALG_IS_CIPHER_MAC(alg) \
289 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
290 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100291
Gilles Peskine8c9def32018-02-08 10:02:12 +0100292#define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100293#define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100294#define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100295#define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000)
296#define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100297#define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100298#define PSA_ALG_IS_BLOCK_CIPHER(alg) \
299 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
300 PSA_ALG_BLOCK_CIPHER_BASE)
301
Gilles Peskine98f0a242018-02-06 18:57:29 +0100302#define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100303#define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002)
304#define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003)
305#define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100306#define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t)0x04800000)
307#define PSA_ALG_CTR ((psa_algorithm_t)0x04800001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100308#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100309
Gilles Peskine8c9def32018-02-08 10:02:12 +0100310#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
311#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100312
313#define PSA_ALG_RSA_PKCS1V15_RAW ((psa_algorithm_t)0x10010000)
314#define PSA_ALG_RSA_PSS_MGF1 ((psa_algorithm_t)0x10020000)
315#define PSA_ALG_RSA_OAEP ((psa_algorithm_t)0x12020000)
316#define PSA_ALG_RSA_PKCS1V15(hash_alg) \
317 (PSA_ALG_RSA_PKCS1V15_RAW | ((hash_alg) & PSA_ALG_HASH_MASK))
318#define PSA_ALG_IS_RSA_PKCS1V15(alg) \
Gilles Peskine20035e32018-02-03 22:44:14 +0100319 (((alg) & 0x7fffff00) == PSA_ALG_RSA_PKCS1V15_RAW)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100320#define PSA_ALG_RSA_GET_HASH(alg) \
321 (((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100322
323/**@}*/
324
325/** \defgroup key_management Key management
326 * @{
327 */
328
329/**
330 * \brief Import a key in binary format.
331 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100332 * This function supports any output from psa_export_key(). Refer to the
333 * documentation of psa_export_key() for the format for each key type.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100334 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100335 * \param key Slot where the key will be stored. This must be a
336 * valid slot for a key of the chosen type. It must
337 * be unoccupied.
338 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
339 * \param data Buffer containing the key data.
340 * \param data_length Size of the \c data buffer in bytes.
341 *
342 * \retval PSA_SUCCESS
343 * Success.
344 * \retval PSA_ERROR_NOT_SUPPORTED
345 * The key type or key size is not supported.
346 * \retval PSA_ERROR_INVALID_ARGUMENT
347 * The key slot is invalid,
348 * or the key data is not correctly formatted.
349 * \retval PSA_ERROR_OCCUPIED_SLOT
350 There is already a key in the specified slot.
351 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
352 * \retval PSA_ERROR_COMMUNICATION_FAILURE
353 * \retval PSA_ERROR_HARDWARE_FAILURE
354 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100355 */
356psa_status_t psa_import_key(psa_key_slot_t key,
357 psa_key_type_t type,
358 const uint8_t *data,
359 size_t data_length);
360
361/**
362 * \brief Destroy a key.
363 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100364 * \retval PSA_SUCCESS
365 * \retval PSA_ERROR_EMPTY_SLOT
366 * \retval PSA_ERROR_COMMUNICATION_FAILURE
367 * \retval PSA_ERROR_HARDWARE_FAILURE
368 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100369 */
370psa_status_t psa_destroy_key(psa_key_slot_t key);
371
372/**
373 * \brief Get basic metadata about a key.
374 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100375 * \param key Slot whose content is queried. This must
376 * be an occupied key slot.
377 * \param type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
378 * This may be a null pointer, in which case the key type
379 * is not written.
380 * \param bits On success, the key size in bits.
381 * This may be a null pointer, in which case the key type
382 * is not written.
383 *
384 * \retval PSA_SUCCESS
385 * \retval PSA_ERROR_EMPTY_SLOT
386 * \retval PSA_ERROR_COMMUNICATION_FAILURE
387 * \retval PSA_ERROR_HARDWARE_FAILURE
388 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100389 */
390psa_status_t psa_get_key_information(psa_key_slot_t key,
391 psa_key_type_t *type,
392 size_t *bits);
393
394/**
395 * \brief Export a key in binary format.
396 *
397 * The output of this function can be passed to psa_import_key() to
398 * create an equivalent object.
399 *
400 * If a key is created with psa_import_key() and then exported with
401 * this function, it is not guaranteed that the resulting data is
402 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +0100403 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100404 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100405 * For standard key types, the output format is as follows:
406 *
407 * - For symmetric keys (including MAC keys), the format is the
408 * raw bytes of the key.
409 * - For DES, the key data consists of 8 bytes. The parity bits must be
410 * correct.
411 * - For Triple-DES, the format is the concatenation of the
412 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100413 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine308b91d2018-02-08 09:47:44 +0100414 * is the non-encrypted DER representation defined by PKCS\#8 (RFC 5208)
415 * as PrivateKeyInfo.
416 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
417 * is the DER representation defined by X.509.
418 *
419 * \param key Slot whose content is to be exported. This must
420 * be an occupied key slot.
421 * \param data Buffer where the key data is to be written.
422 * \param data_size Size of the \c data buffer in bytes.
423 * \param data_length On success, the number of bytes
424 * that make up the key data.
425 *
426 * \retval PSA_SUCCESS
427 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine92b30732018-03-03 21:29:30 +0100428 * \retval PSA_ERROR_NOT_PERMITTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100429 * \retval PSA_ERROR_COMMUNICATION_FAILURE
430 * \retval PSA_ERROR_HARDWARE_FAILURE
431 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100432 */
433psa_status_t psa_export_key(psa_key_slot_t key,
434 uint8_t *data,
435 size_t data_size,
436 size_t *data_length);
437
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100438/**
439 * \brief Export a public key or the public part of a key pair in binary format.
440 *
441 * The output of this function can be passed to psa_import_key() to
442 * create an object that is equivalent to the public key.
443 *
444 * For standard key types, the output format is as follows:
445 *
446 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
447 * the format is the DER representation defined by X.509.
448 *
449 * \param key Slot whose content is to be exported. This must
450 * be an occupied key slot.
451 * \param data Buffer where the key data is to be written.
452 * \param data_size Size of the \c data buffer in bytes.
453 * \param data_length On success, the number of bytes
454 * that make up the key data.
455 *
456 * \retval PSA_SUCCESS
457 * \retval PSA_ERROR_EMPTY_SLOT
458 * \retval PSA_ERROR_INVALID_ARGUMENT
459 * \retval PSA_ERROR_COMMUNICATION_FAILURE
460 * \retval PSA_ERROR_HARDWARE_FAILURE
461 * \retval PSA_ERROR_TAMPERING_DETECTED
462 */
463psa_status_t psa_export_public_key(psa_key_slot_t key,
464 uint8_t *data,
465 size_t data_size,
466 size_t *data_length);
467
468/**@}*/
469
470/** \defgroup policy Key policies
471 * @{
472 */
473
474/** \brief Encoding of permitted usage on a key. */
475typedef uint32_t psa_key_usage_t;
476
477#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
478
479#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
480#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
481#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
482#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
483
484/** The type of the key policy data structure.
485 *
486 * This is an implementation-defined \c struct. Applications should not
487 * make any assumptions about the content of this structure except
488 * as directed by the documentation of a specific implementation. */
489typedef struct psa_key_policy_s psa_key_policy_t;
490
491/** \brief Initialize a key policy structure to a default that forbids all
492 * usage of the key. */
493void psa_key_policy_init(psa_key_policy_t *policy);
494
495void psa_key_policy_set_usage(psa_key_policy_t *policy,
496 psa_key_usage_t usage,
497 psa_algorithm_t alg);
498
499psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy);
500
501psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy);
502
503/** \brief Set the usage policy on a key slot.
504 *
505 * This function must be called on an empty key slot, before importing,
506 * generating or creating a key in the slot. Changing the policy of an
507 * existing key is not permitted.
508 */
509psa_status_t psa_set_key_policy(psa_key_slot_t key,
510 const psa_key_policy_t *policy);
511
512psa_status_t psa_get_key_policy(psa_key_slot_t key,
513 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100514
515/**@}*/
516
Gilles Peskine609b6a52018-03-03 21:31:50 +0100517/** \defgroup persistence Key lifetime
518 * @{
519 */
520
521/** Encoding of key lifetimes.
522 */
523typedef uint32_t psa_key_lifetime_t;
524
525/** A volatile key slot retains its content as long as the application is
526 * running. It is guaranteed to be erased on a power reset.
527 */
528#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
529
530/** A persistent key slot retains its content as long as it is not explicitly
531 * destroyed.
532 */
533#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
534
535/** A write-once key slot may not be modified once a key has been set.
536 * It will retain its content as long as the device remains operational.
537 */
538#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
539
Gilles Peskined393e182018-03-08 07:49:16 +0100540/** \brief Retrieve the lifetime of a key slot.
541 *
542 * The assignment of lifetimes to slots is implementation-dependent.
543 */
Gilles Peskine609b6a52018-03-03 21:31:50 +0100544psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
545 psa_key_lifetime_t *lifetime);
546
Gilles Peskined393e182018-03-08 07:49:16 +0100547/** \brief Change the lifetime of a key slot.
548 *
549 * Whether the lifetime of a key slot can be changed at all, and if so
550 * whether the lifetime of an occupied key slot can be chaned, is
551 * implementation-dependent.
552 */
553psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
554 const psa_key_lifetime_t *lifetime);
555
Gilles Peskine609b6a52018-03-03 21:31:50 +0100556/**@}*/
557
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100558/** \defgroup hash Message digests
559 * @{
560 */
561
Gilles Peskine308b91d2018-02-08 09:47:44 +0100562/** The type of the state data structure for multipart hash operations.
563 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100564 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100565 * make any assumptions about the content of this structure except
566 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100567typedef struct psa_hash_operation_s psa_hash_operation_t;
568
Gilles Peskine308b91d2018-02-08 09:47:44 +0100569/** The size of the output of psa_hash_finish(), in bytes.
570 *
571 * This is also the hash size that psa_hash_verify() expects.
572 *
573 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
574 * #PSA_ALG_IS_HASH(alg) is true).
575 *
576 * \return The hash size for the specified hash algorithm.
577 * If the hash algorithm is not recognized, return 0.
578 * An implementation may return either 0 or the correct size
579 * for a hash algorithm that it recognizes, but does not support.
580 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100581#define PSA_HASH_FINAL_SIZE(alg) \
582 ( \
583 (alg) == PSA_ALG_MD2 ? 16 : \
584 (alg) == PSA_ALG_MD4 ? 16 : \
585 (alg) == PSA_ALG_MD5 ? 16 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100586 (alg) == PSA_ALG_RIPEMD160 ? 20 : \
587 (alg) == PSA_ALG_SHA_1 ? 20 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100588 (alg) == PSA_ALG_SHA_224 ? 28 : \
589 (alg) == PSA_ALG_SHA_256 ? 32 : \
590 (alg) == PSA_ALG_SHA_384 ? 48 : \
591 (alg) == PSA_ALG_SHA_512 ? 64 : \
592 (alg) == PSA_ALG_SHA_512_224 ? 28 : \
593 (alg) == PSA_ALG_SHA_512_256 ? 32 : \
594 (alg) == PSA_ALG_SHA3_224 ? 28 : \
595 (alg) == PSA_ALG_SHA3_256 ? 32 : \
596 (alg) == PSA_ALG_SHA3_384 ? 48 : \
597 (alg) == PSA_ALG_SHA3_512 ? 64 : \
598 0)
599
Gilles Peskine308b91d2018-02-08 09:47:44 +0100600/** Start a multipart hash operation.
601 *
602 * The sequence of operations to calculate a hash (message digest)
603 * is as follows:
604 * -# Allocate an operation object which will be passed to all the functions
605 * listed here.
606 * -# Call psa_hash_start() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100607 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100608 * of the message each time. The hash that is calculated is the hash
609 * of the concatenation of these messages in order.
610 * -# To calculate the hash, call psa_hash_finish().
611 * To compare the hash with an expected value, call psa_hash_verify().
612 *
613 * The application may call psa_hash_abort() at any time after the operation
614 * has been initialized with psa_hash_start().
615 *
616 * After a successful call to psa_hash_start(), the application must
617 * eventually destroy the operation through one of the following means:
618 * - A failed call to psa_hash_update().
619 * - A call to psa_hash_final(), psa_hash_verify() or psa_hash_abort().
620 *
621 * \param operation
622 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
623 * such that #PSA_ALG_IS_HASH(alg) is true).
624 *
625 * \retval PSA_SUCCESS
626 * Success.
627 * \retval PSA_ERROR_NOT_SUPPORTED
628 * \c alg is not supported or is not a hash algorithm.
629 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
630 * \retval PSA_ERROR_COMMUNICATION_FAILURE
631 * \retval PSA_ERROR_HARDWARE_FAILURE
632 * \retval PSA_ERROR_TAMPERING_DETECTED
633 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100634psa_status_t psa_hash_start(psa_hash_operation_t *operation,
635 psa_algorithm_t alg);
636
Gilles Peskine308b91d2018-02-08 09:47:44 +0100637/** Add a message fragment to a multipart hash operation.
638 *
639 * The application must call psa_hash_start() before calling this function.
640 *
641 * If this function returns an error status, the operation becomes inactive.
642 *
643 * \param operation Active hash operation.
644 * \param input Buffer containing the message fragment to hash.
645 * \param input_length Size of the \c input buffer in bytes.
646 *
647 * \retval PSA_SUCCESS
648 * Success.
649 * \retval PSA_ERROR_BAD_STATE
650 * The operation state is not valid (not started, or already completed).
651 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
652 * \retval PSA_ERROR_COMMUNICATION_FAILURE
653 * \retval PSA_ERROR_HARDWARE_FAILURE
654 * \retval PSA_ERROR_TAMPERING_DETECTED
655 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100656psa_status_t psa_hash_update(psa_hash_operation_t *operation,
657 const uint8_t *input,
658 size_t input_length);
659
Gilles Peskine308b91d2018-02-08 09:47:44 +0100660/** Finish the calculation of the hash of a message.
661 *
662 * The application must call psa_hash_start() before calling this function.
663 * This function calculates the hash of the message formed by concatenating
664 * the inputs passed to preceding calls to psa_hash_update().
665 *
666 * When this function returns, the operation becomes inactive.
667 *
668 * \warning Applications should not call this function if they expect
669 * a specific value for the hash. Call psa_hash_verify() instead.
670 * Beware that comparing integrity or authenticity data such as
671 * hash values with a function such as \c memcmp is risky
672 * because the time taken by the comparison may leak information
673 * about the hashed data which could allow an attacker to guess
674 * a valid hash and thereby bypass security controls.
675 *
676 * \param operation Active hash operation.
677 * \param hash Buffer where the hash is to be written.
678 * \param hash_size Size of the \c hash buffer in bytes.
679 * \param hash_length On success, the number of bytes
680 * that make up the hash value. This is always
681 * #PSA_HASH_FINAL_SIZE(alg) where \c alg is the
682 * hash algorithm that is calculated.
683 *
684 * \retval PSA_SUCCESS
685 * Success.
686 * \retval PSA_ERROR_BAD_STATE
687 * The operation state is not valid (not started, or already completed).
688 * \retval PSA_ERROR_BUFFER_TOO_SMALL
689 * The size of the \c hash buffer is too small. You can determine a
690 * sufficient buffer size by calling #PSA_HASH_FINAL_SIZE(alg)
691 * where \c alg is the hash algorithm that is calculated.
692 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
693 * \retval PSA_ERROR_COMMUNICATION_FAILURE
694 * \retval PSA_ERROR_HARDWARE_FAILURE
695 * \retval PSA_ERROR_TAMPERING_DETECTED
696 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100697psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
698 uint8_t *hash,
699 size_t hash_size,
700 size_t *hash_length);
701
Gilles Peskine308b91d2018-02-08 09:47:44 +0100702/** Finish the calculation of the hash of a message and compare it with
703 * an expected value.
704 *
705 * The application must call psa_hash_start() before calling this function.
706 * This function calculates the hash of the message formed by concatenating
707 * the inputs passed to preceding calls to psa_hash_update(). It then
708 * compares the calculated hash with the expected hash passed as a
709 * parameter to this function.
710 *
711 * When this function returns, the operation becomes inactive.
712 *
713 * \note Applications shall make the best effort to ensure that the
714 * comparison between the actual hash and the expected hash is performed
715 * in constant time.
716 *
717 * \param operation Active hash operation.
718 * \param hash Buffer containing the expected hash value.
719 * \param hash_length Size of the \c hash buffer in bytes.
720 *
721 * \retval PSA_SUCCESS
722 * The expected hash is identical to the actual hash of the message.
723 * \retval PSA_ERROR_INVALID_SIGNATURE
724 * The hash of the message was calculated successfully, but it
725 * differs from the expected hash.
726 * \retval PSA_ERROR_BAD_STATE
727 * The operation state is not valid (not started, or already completed).
728 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
729 * \retval PSA_ERROR_COMMUNICATION_FAILURE
730 * \retval PSA_ERROR_HARDWARE_FAILURE
731 * \retval PSA_ERROR_TAMPERING_DETECTED
732 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100733psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
734 const uint8_t *hash,
735 size_t hash_length);
736
Gilles Peskine308b91d2018-02-08 09:47:44 +0100737/** Abort a hash operation.
738 *
739 * This function may be called at any time after psa_hash_start().
740 * Aborting an operation frees all associated resources except for the
741 * \c operation structure itself.
742 *
743 * Implementation should strive to be robust and handle inactive hash
744 * operations safely (do nothing and return #PSA_ERROR_BAD_STATE). However,
745 * application writers should beware that uninitialized memory may happen
746 * to be indistinguishable from an active hash operation, and the behavior
747 * of psa_hash_abort() is undefined in this case.
748 *
749 * \param operation Active hash operation.
750 *
751 * \retval PSA_SUCCESS
752 * \retval PSA_ERROR_BAD_STATE
753 * \c operation is not an active hash operation.
754 * \retval PSA_ERROR_COMMUNICATION_FAILURE
755 * \retval PSA_ERROR_HARDWARE_FAILURE
756 * \retval PSA_ERROR_TAMPERING_DETECTED
757 */
758psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100759
760/**@}*/
761
Gilles Peskine8c9def32018-02-08 10:02:12 +0100762/** \defgroup MAC Message authentication codes
763 * @{
764 */
765
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100766/** The type of the state data structure for multipart MAC operations.
767 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100768 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100769 * make any assumptions about the content of this structure except
770 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100771typedef struct psa_mac_operation_s psa_mac_operation_t;
772
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100773/** The size of the output of psa_mac_finish(), in bytes.
774 *
775 * This is also the MAC size that psa_mac_verify() expects.
776 *
777 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
778 * #PSA_ALG_IS_MAC(alg) is true).
779 *
780 * \return The MAC size for the specified algorithm.
781 * If the MAC algorithm is not recognized, return 0.
782 * An implementation may return either 0 or the correct size
783 * for a MAC algorithm that it recognizes, but does not support.
784 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100785#define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \
786 (PSA_ALG_IS_HMAC(alg) ? PSA_HASH_FINAL_SIZE(PSA_ALG_HMAC_HASH(alg)) : \
787 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
788 0)
789
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100790/** Start a multipart MAC operation.
791 *
792 * The sequence of operations to calculate a MAC (message authentication code)
793 * is as follows:
794 * -# Allocate an operation object which will be passed to all the functions
795 * listed here.
796 * -# Call psa_mac_start() to specify the algorithm and key.
797 * The key remains associated with the operation even if the content
798 * of the key slot changes.
799 * -# Call psa_mac_update() zero, one or more times, passing a fragment
800 * of the message each time. The MAC that is calculated is the MAC
801 * of the concatenation of these messages in order.
802 * -# To calculate the MAC, call psa_mac_finish().
803 * To compare the MAC with an expected value, call psa_mac_verify().
804 *
805 * The application may call psa_mac_abort() at any time after the operation
806 * has been initialized with psa_mac_start().
807 *
808 * After a successful call to psa_mac_start(), the application must
809 * eventually destroy the operation through one of the following means:
810 * - A failed call to psa_mac_update().
811 * - A call to psa_mac_final(), psa_mac_verify() or psa_mac_abort().
812 *
813 * \param operation
814 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
815 * such that #PSA_ALG_IS_MAC(alg) is true).
816 *
817 * \retval PSA_SUCCESS
818 * Success.
819 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine92b30732018-03-03 21:29:30 +0100820 * \retval PSA_ERROR_NOT_PERMITTED
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100821 * \retval PSA_ERROR_INVALID_ARGUMENT
822 * \c key is not compatible with \c alg.
823 * \retval PSA_ERROR_NOT_SUPPORTED
824 * \c alg is not supported or is not a MAC algorithm.
825 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
826 * \retval PSA_ERROR_COMMUNICATION_FAILURE
827 * \retval PSA_ERROR_HARDWARE_FAILURE
828 * \retval PSA_ERROR_TAMPERING_DETECTED
829 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100830psa_status_t psa_mac_start(psa_mac_operation_t *operation,
831 psa_key_slot_t key,
832 psa_algorithm_t alg);
833
834psa_status_t psa_mac_update(psa_mac_operation_t *operation,
835 const uint8_t *input,
836 size_t input_length);
837
838psa_status_t psa_mac_finish(psa_mac_operation_t *operation,
839 uint8_t *mac,
840 size_t mac_size,
841 size_t *mac_length);
842
843psa_status_t psa_mac_verify(psa_mac_operation_t *operation,
844 const uint8_t *mac,
845 size_t mac_length);
846
847psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
848
849/**@}*/
850
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100851/** \defgroup cipher Symmetric ciphers
852 * @{
853 */
854
855/** The type of the state data structure for multipart cipher operations.
856 *
857 * This is an implementation-defined \c struct. Applications should not
858 * make any assumptions about the content of this structure except
859 * as directed by the documentation of a specific implementation. */
860typedef struct psa_cipher_operation_s psa_cipher_operation_t;
861
862/** Set the key for a multipart symmetric encryption operation.
863 *
864 * The sequence of operations to encrypt a message with a symmetric cipher
865 * is as follows:
866 * -# Allocate an operation object which will be passed to all the functions
867 * listed here.
868 * -# Call psa_encrypt_setup() to specify the algorithm and key.
869 * The key remains associated with the operation even if the content
870 * of the key slot changes.
871 * -# Call either psa_encrypt_generate_iv() or psa_encrypt_set_iv() to
872 * generate or set the IV (initialization vector). You should use
873 * psa_encrypt_generate_iv() unless the protocol you are implementing
874 * requires a specific IV value.
875 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
876 * of the message each time.
877 * -# Call psa_cipher_finish().
878 *
879 * The application may call psa_cipher_abort() at any time after the operation
880 * has been initialized with psa_encrypt_setup().
881 *
882 * After a successful call to psa_encrypt_setup(), the application must
883 * eventually destroy the operation through one of the following means:
884 * - A failed call to psa_encrypt_generate_iv(), psa_encrypt_set_iv()
885 * or psa_cipher_update().
886 * - A call to psa_cipher_final() or psa_cipher_abort().
887 *
888 * \param operation
889 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
890 * such that #PSA_ALG_IS_CIPHER(alg) is true).
891 *
892 * \retval PSA_SUCCESS
893 * Success.
894 * \retval PSA_ERROR_EMPTY_SLOT
895 * \retval PSA_ERROR_NOT_PERMITTED
896 * \retval PSA_ERROR_INVALID_ARGUMENT
897 * \c key is not compatible with \c alg.
898 * \retval PSA_ERROR_NOT_SUPPORTED
899 * \c alg is not supported or is not a cipher algorithm.
900 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
901 * \retval PSA_ERROR_COMMUNICATION_FAILURE
902 * \retval PSA_ERROR_HARDWARE_FAILURE
903 * \retval PSA_ERROR_TAMPERING_DETECTED
904 */
905psa_status_t psa_encrypt_setup(psa_cipher_operation_t *operation,
906 psa_key_slot_t key,
907 psa_algorithm_t alg);
908
909/** Set the key for a multipart symmetric decryption operation.
910 *
911 * The sequence of operations to decrypt a message with a symmetric cipher
912 * is as follows:
913 * -# Allocate an operation object which will be passed to all the functions
914 * listed here.
915 * -# Call psa_decrypt_setup() to specify the algorithm and key.
916 * The key remains associated with the operation even if the content
917 * of the key slot changes.
918 * -# Call psa_cipher_update() with the IV (initialization vector) for the
919 * decryption. If the IV is prepended to the ciphertext, you can call
920 * psa_cipher_update() on a buffer containing the IV followed by the
921 * beginning of the message.
922 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
923 * of the message each time.
924 * -# Call psa_cipher_finish().
925 *
926 * The application may call psa_cipher_abort() at any time after the operation
927 * has been initialized with psa_encrypt_setup().
928 *
929 * After a successful call to psa_decrypt_setup(), the application must
930 * eventually destroy the operation through one of the following means:
931 * - A failed call to psa_cipher_update().
932 * - A call to psa_cipher_final() or psa_cipher_abort().
933 *
934 * \param operation
935 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
936 * such that #PSA_ALG_IS_CIPHER(alg) is true).
937 *
938 * \retval PSA_SUCCESS
939 * Success.
940 * \retval PSA_ERROR_EMPTY_SLOT
941 * \retval PSA_ERROR_NOT_PERMITTED
942 * \retval PSA_ERROR_INVALID_ARGUMENT
943 * \c key is not compatible with \c alg.
944 * \retval PSA_ERROR_NOT_SUPPORTED
945 * \c alg is not supported or is not a cipher algorithm.
946 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
947 * \retval PSA_ERROR_COMMUNICATION_FAILURE
948 * \retval PSA_ERROR_HARDWARE_FAILURE
949 * \retval PSA_ERROR_TAMPERING_DETECTED
950 */
951psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation,
952 psa_key_slot_t key,
953 psa_algorithm_t alg);
954
955psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation,
956 unsigned char *iv,
957 size_t iv_size,
958 size_t *iv_length);
959
960psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation,
961 const unsigned char *iv,
962 size_t iv_length);
963
964psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
965 const uint8_t *input,
966 size_t input_length);
967
968psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
969 uint8_t *mac,
970 size_t mac_size,
971 size_t *mac_length);
972
973psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
974
975/**@}*/
976
Gilles Peskine3b555712018-03-03 21:27:57 +0100977/** \defgroup aead Authenticated encryption with associated data (AEAD)
978 * @{
979 */
980
981/** The type of the state data structure for multipart AEAD operations.
982 *
983 * This is an implementation-defined \c struct. Applications should not
984 * make any assumptions about the content of this structure except
985 * as directed by the documentation of a specific implementation. */
986typedef struct psa_aead_operation_s psa_aead_operation_t;
987
988/** Set the key for a multipart authenticated encryption operation.
989 *
990 * The sequence of operations to authenticate-and-encrypt a message
991 * is as follows:
992 * -# Allocate an operation object which will be passed to all the functions
993 * listed here.
994 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
995 * The key remains associated with the operation even if the content
996 * of the key slot changes.
997 * -# Call either psa_aead_generate_iv() or psa_aead_set_iv() to
998 * generate or set the IV (initialization vector). You should use
999 * psa_encrypt_generate_iv() unless the protocol you are implementing
1000 * requires a specific IV value.
1001 * -# Call psa_aead_update_ad() to pass the associated data that is
1002 * to be authenticated but not encrypted. You may omit this step if
1003 * there is no associated data.
1004 * -# Call psa_aead_update() zero, one or more times, passing a fragment
1005 * of the data to encrypt each time.
1006 * -# Call psa_aead_finish().
1007 *
1008 * The application may call psa_aead_abort() at any time after the operation
1009 * has been initialized with psa_aead_encrypt_setup().
1010 *
1011 * After a successful call to psa_aead_setup(), the application must
1012 * eventually destroy the operation through one of the following means:
1013 * - A failed call to psa_aead_generate_iv(), psa_aead_set_iv(),
1014 * psa_aead_update_ad() or psa_aead_update().
1015 * - A call to psa_aead_final() or psa_aead_abort().
1016 *
1017 * \param operation
1018 * \param alg The AEAD algorithm to compute (\c PSA_ALG_XXX value
1019 * such that #PSA_ALG_IS_AEAD(alg) is true).
1020 *
1021 * \retval PSA_SUCCESS
1022 * Success.
1023 * \retval PSA_ERROR_EMPTY_SLOT
1024 * \retval PSA_ERROR_NOT_PERMITTED
1025 * \retval PSA_ERROR_INVALID_ARGUMENT
1026 * \c key is not compatible with \c alg.
1027 * \retval PSA_ERROR_NOT_SUPPORTED
1028 * \c alg is not supported or is not an AEAD algorithm.
1029 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1030 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1031 * \retval PSA_ERROR_HARDWARE_FAILURE
1032 * \retval PSA_ERROR_TAMPERING_DETECTED
1033 */
1034psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
1035 psa_key_slot_t key,
1036 psa_algorithm_t alg);
1037
1038/** Set the key for a multipart authenticated decryption operation.
1039 *
1040 * The sequence of operations to authenticated and decrypt a message
1041 * is as follows:
1042 * -# Allocate an operation object which will be passed to all the functions
1043 * listed here.
1044 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
1045 * The key remains associated with the operation even if the content
1046 * of the key slot changes.
1047 * -# Call psa_aead_set_iv() to pass the initialization vector (IV)
1048 * for the authenticated decryption.
1049 * -# Call psa_aead_update_ad() to pass the associated data that is
1050 * to be authenticated but not encrypted. You may omit this step if
1051 * there is no associated data.
1052 * -# Call psa_aead_update() zero, one or more times, passing a fragment
1053 * of the data to decrypt each time.
1054 * -# Call psa_aead_finish().
1055 *
1056 * The application may call psa_aead_abort() at any time after the operation
1057 * has been initialized with psa_aead_decrypt_setup().
1058 *
1059 * After a successful call to psa_decrypt_setup(), the application must
1060 * eventually destroy the operation through one of the following means:
1061 * - A failed call to psa_aead_update().
1062 * - A call to psa_cipher_final() or psa_cipher_abort().
1063 *
1064 * \param operation
1065 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
1066 * such that #PSA_ALG_IS_CIPHER(alg) is true).
1067 *
1068 * \retval PSA_SUCCESS
1069 * Success.
1070 * \retval PSA_ERROR_EMPTY_SLOT
1071 * \retval PSA_ERROR_NOT_PERMITTED
1072 * \retval PSA_ERROR_INVALID_ARGUMENT
1073 * \c key is not compatible with \c alg.
1074 * \retval PSA_ERROR_NOT_SUPPORTED
1075 * \c alg is not supported or is not a cipher algorithm.
1076 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1077 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1078 * \retval PSA_ERROR_HARDWARE_FAILURE
1079 * \retval PSA_ERROR_TAMPERING_DETECTED
1080 */
1081psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
1082 psa_key_slot_t key,
1083 psa_algorithm_t alg);
1084
1085psa_status_t psa_aead_generate_iv(psa_aead_operation_t *operation,
1086 unsigned char *iv,
1087 size_t iv_size,
1088 size_t *iv_length);
1089
1090psa_status_t psa_aead_set_iv(psa_aead_operation_t *operation,
1091 const unsigned char *iv,
1092 size_t iv_length);
1093
1094psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
1095 const uint8_t *input,
1096 size_t input_length);
1097
1098psa_status_t psa_aead_update(psa_aead_operation_t *operation,
1099 const uint8_t *input,
1100 size_t input_length);
1101
1102psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
1103 uint8_t *tag,
1104 size_t tag_size,
1105 size_t *tag_length);
1106
1107psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
1108 uint8_t *tag,
1109 size_t tag_length);
1110
1111psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
1112
1113/**@}*/
1114
Gilles Peskine20035e32018-02-03 22:44:14 +01001115/** \defgroup asymmetric Asymmetric cryptography
1116 * @{
1117 */
1118
1119/**
Gilles Peskine0189e752018-02-03 23:57:22 +01001120 * \brief Maximum ECDSA signature size for a given curve bit size
1121 *
1122 * \param curve_bits Curve size in bits
1123 * \return Maximum signature size in bytes
1124 *
1125 * \note This macro returns a compile-time constant if its argument is one.
1126 *
1127 * \warning This macro may evaluate its argument multiple times.
1128 */
1129/*
1130 * RFC 4492 page 20:
1131 *
1132 * Ecdsa-Sig-Value ::= SEQUENCE {
1133 * r INTEGER,
1134 * s INTEGER
1135 * }
1136 *
1137 * Size is at most
1138 * 1 (tag) + 1 (len) + 1 (initial 0) + curve_bytes for each of r and s,
1139 * twice that + 1 (tag) + 2 (len) for the sequence
1140 * (assuming curve_bytes is less than 126 for r and s,
1141 * and less than 124 (total len <= 255) for the sequence)
1142 */
1143#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
1144 ( /*T,L of SEQUENCE*/ ((curve_bits) >= 61 * 8 ? 3 : 2) + \
1145 /*T,L of r,s*/ 2 * (((curve_bits) >= 127 * 8 ? 3 : 2) + \
1146 /*V of r,s*/ ((curve_bits) + 8) / 8))
1147
1148
Gilles Peskine308b91d2018-02-08 09:47:44 +01001149/** Safe signature buffer size for psa_asymmetric_sign().
1150 *
1151 * This macro returns a safe buffer size for a signature using a key
1152 * of the specified type and size, with the specified algorithm.
1153 * Note that the actual size of the signature may be smaller
1154 * (some algorithms produce a variable-size signature).
1155 *
1156 * \warning This function may call its arguments multiple times or
1157 * zero times, so you should not pass arguments that contain
1158 * side effects.
1159 *
1160 * \param key_type An asymmetric key type (this may indifferently be a
1161 * key pair type or a public key type).
1162 * \param key_bits The size of the key in bits.
1163 * \param alg The signature algorithm.
1164 *
1165 * \return If the parameters are valid and supported, return
1166 * a buffer size in bytes that guarantees that
1167 * psa_asymmetric_sign() will not fail with
1168 * #PSA_ERROR_BUFFER_TOO_SMALL.
1169 * If the parameters are a valid combination that is not supported
1170 * by the implementation, this macro either shall return either a
1171 * sensible size or 0.
1172 * If the parameters are not valid, the
1173 * return value is unspecified.
1174 *
1175 */
Gilles Peskine0189e752018-02-03 23:57:22 +01001176#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine2905a7a2018-03-07 16:39:31 +01001177 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine0189e752018-02-03 23:57:22 +01001178 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
1179 0)
1180
1181/**
Gilles Peskine20035e32018-02-03 22:44:14 +01001182 * \brief Sign a hash or short message with a private key.
1183 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001184 * \param key Key slot containing an asymmetric key pair.
1185 * \param alg A signature algorithm that is compatible with
1186 * the type of \c key.
1187 * \param hash The message to sign.
1188 * \param hash_length Size of the \c hash buffer in bytes.
1189 * \param salt A salt or label, if supported by the signature
1190 * algorithm.
1191 * If the signature algorithm does not support a
1192 * salt, pass \c NULL.
1193 * If the signature algorithm supports an optional
1194 * salt and you do not want to pass a salt,
1195 * pass \c NULL.
1196 * \param salt_length Size of the \c salt buffer in bytes.
1197 * If \c salt is \c NULL, pass 0.
1198 * \param signature Buffer where the signature is to be written.
1199 * \param signature_size Size of the \c signature buffer in bytes.
1200 * \param signature_length On success, the number of bytes
1201 * that make up the returned signature value.
1202 * This is at most #PSA_HASH_FINAL_SIZE(alg)
1203 * (note that it may be less).
1204 *
1205 * \retval PSA_SUCCESS
1206 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1207 * The size of the \c signature buffer is too small. You can
1208 * determine a sufficient buffer size by calling
1209 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg)
1210 * where \c key_type and \c key_bits are the type and bit-size
1211 * respectively of \c key.
1212 * \retval PSA_ERROR_NOT_SUPPORTED
1213 * \retval PSA_ERROR_INVALID_ARGUMENT
1214 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1215 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1216 * \retval PSA_ERROR_HARDWARE_FAILURE
1217 * \retval PSA_ERROR_TAMPERING_DETECTED
1218 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01001219 */
1220psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
1221 psa_algorithm_t alg,
1222 const uint8_t *hash,
1223 size_t hash_length,
1224 const uint8_t *salt,
1225 size_t salt_length,
1226 uint8_t *signature,
1227 size_t signature_size,
1228 size_t *signature_length);
1229
1230/**
1231 * \brief Verify the signature a hash or short message using a public key.
1232 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001233 * \param key Key slot containing a public key or an
1234 * asymmetric key pair.
1235 * \param alg A signature algorithm that is compatible with
1236 * the type of \c key.
1237 * \param hash The message whose signature is to be verified.
1238 * \param hash_length Size of the \c hash buffer in bytes.
1239 * \param salt A salt or label, if supported by the signature
1240 * algorithm.
1241 * If the signature algorithm does not support a
1242 * salt, pass \c NULL.
1243 * If the signature algorithm supports an optional
1244 * salt and you do not want to pass a salt,
1245 * pass \c NULL.
1246 * \param salt_length Size of the \c salt buffer in bytes.
1247 * If \c salt is \c NULL, pass 0.
1248 * \param signature Buffer containing the signature to verify.
1249 * \param signature_size Size of the \c signature buffer in bytes.
1250 *
1251 * \retval PSA_SUCCESS
1252 * The signature is valid.
1253 * \retval PSA_ERROR_INVALID_SIGNATURE
1254 * The calculation was perfomed successfully, but the passed
1255 * signature is not a valid signature.
1256 * \retval PSA_ERROR_NOT_SUPPORTED
1257 * \retval PSA_ERROR_INVALID_ARGUMENT
1258 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1259 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1260 * \retval PSA_ERROR_HARDWARE_FAILURE
1261 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01001262 */
1263psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
1264 psa_algorithm_t alg,
1265 const uint8_t *hash,
1266 size_t hash_length,
1267 const uint8_t *salt,
1268 size_t salt_length,
1269 uint8_t *signature,
1270 size_t signature_size);
1271
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001272/**@}*/
1273
Gilles Peskinee59236f2018-01-27 23:32:46 +01001274#ifdef __cplusplus
1275}
1276#endif
1277
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001278/* The file "crypto_struct.h" contains definitions for
1279 * implementation-specific structs that are declared above. */
1280#include "crypto_struct.h"
1281
1282/* The file "crypto_extra.h" contains vendor-specific definitions. This
1283 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01001284#include "crypto_extra.h"
1285
1286#endif /* PSA_CRYPTO_H */