blob: 975edb408d4c9b20afebc957dcf998b4bbc4e341 [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)
itayzafrir5c753392018-05-08 11:18:38 +0300165#define PSA_KEY_TYPE_ECC_CURVE_NISTP256R1 ((psa_key_type_t)0x00000001)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100166#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100167#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
168 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
169#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
170 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
Gilles Peskine98f0a242018-02-06 18:57:29 +0100171
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100172/** Whether a key type is vendor-defined. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100173#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100174 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100175#define PSA_KEY_TYPE_IS_RAW_BYTES(type) \
176 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_RAW_DATA || \
177 ((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100178
179/** Whether a key type is asymmetric: either a key pair or a public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100180#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
181 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100182/** Whether a key type is the public part of a key pair. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100183#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
Moran Pekerb4d0ddd2018-04-04 12:47:52 +0300184 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
185 PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100186/** Whether a key type is a key pair containing a private part and a public
187 * part. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100188#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
189 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
190 (PSA_KEY_TYPE_CATEGORY_ASYMMETRIC | PSA_KEY_TYPE_PAIR_FLAG))
Gilles Peskine06dc2632018-03-08 07:47:25 +0100191/** Whether a key type is an RSA key pair or public key. */
192/** The key pair type corresponding to a public key type. */
193#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
194 ((type) | PSA_KEY_TYPE_PAIR_FLAG)
195/** The public key type corresponding to a key pair type. */
196#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
197 ((type) & ~PSA_KEY_TYPE_PAIR_FLAG)
Gilles Peskine0189e752018-02-03 23:57:22 +0100198#define PSA_KEY_TYPE_IS_RSA(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100199 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
200/** Whether a key type is an elliptic curve key pair or public key. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100201#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100202 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
203 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100204
Gilles Peskine7e198532018-03-08 07:50:30 +0100205/** The block size of a block cipher.
206 *
207 * \param type A cipher key type (value of type #psa_key_type_t).
208 *
209 * \return The block size for a block cipher, or 1 for a stream cipher.
210 * The return value is undefined if \c type does not identify
211 * a cipher algorithm.
212 *
213 * \note This macro returns a compile-time constant if its argument is one.
214 *
215 * \warning This macro may evaluate its argument multiple times.
216 */
Gilles Peskine03182e92018-03-07 16:40:52 +0100217#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100218 ( \
219 (type) == PSA_KEY_TYPE_AES ? 16 : \
220 (type) == PSA_KEY_TYPE_DES ? 8 : \
221 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
Gilles Peskine7e198532018-03-08 07:50:30 +0100222 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100223 0)
224
Gilles Peskine308b91d2018-02-08 09:47:44 +0100225/** \brief Encoding of a cryptographic algorithm.
226 *
227 * For algorithms that can be applied to multiple key types, this type
228 * does not encode the key type. For example, for symmetric ciphers
229 * based on a block cipher, #psa_algorithm_t encodes the block cipher
230 * mode and the padding mode while the block cipher itself is encoded
231 * via #psa_key_type_t.
232 */
Gilles Peskine20035e32018-02-03 22:44:14 +0100233typedef uint32_t psa_algorithm_t;
234
Gilles Peskine98f0a242018-02-06 18:57:29 +0100235#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
236#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
237#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
238#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
239#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
240#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
241#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
242#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
243#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
244#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100245
Gilles Peskine98f0a242018-02-06 18:57:29 +0100246#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
247 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100248/** Whether the specified algorithm is a hash algorithm.
249 *
Gilles Peskine7e198532018-03-08 07:50:30 +0100250 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100251 *
252 * \return 1 if \c alg is a hash algorithm, 0 otherwise.
253 * This macro may return either 0 or 1 if \c alg is not a valid
Gilles Peskine7e198532018-03-08 07:50:30 +0100254 * algorithm identifier.
255 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100256#define PSA_ALG_IS_HASH(alg) \
257 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
258#define PSA_ALG_IS_MAC(alg) \
259 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
260#define PSA_ALG_IS_CIPHER(alg) \
261 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
262#define PSA_ALG_IS_AEAD(alg) \
263 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
264#define PSA_ALG_IS_SIGN(alg) \
265 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
266#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
267 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
268#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
269 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
270#define PSA_ALG_IS_KEY_DERIVATION(alg) \
271 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
272
273#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
274#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
275#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
276#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
Gilles Peskinee3f694f2018-03-08 07:48:40 +0100277#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
278#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100279#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
280#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
281#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
282#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
283#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
284#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
285#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
286#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
287#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
288#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
289
Gilles Peskine8c9def32018-02-08 10:02:12 +0100290#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100291#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
292#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100293 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
294#define PSA_ALG_HMAC_HASH(hmac_alg) \
295 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
296#define PSA_ALG_IS_HMAC(alg) \
297 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
298 PSA_ALG_HMAC_BASE)
299#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
300#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
301#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
302#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
303#define PSA_ALG_IS_CIPHER_MAC(alg) \
304 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
305 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100306
Gilles Peskine8c9def32018-02-08 10:02:12 +0100307#define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100308#define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100309#define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100310#define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000)
311#define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100312#define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100313#define PSA_ALG_IS_BLOCK_CIPHER(alg) \
314 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
315 PSA_ALG_BLOCK_CIPHER_BASE)
316
Gilles Peskine98f0a242018-02-06 18:57:29 +0100317#define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100318#define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002)
319#define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003)
320#define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100321#define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t)0x04800000)
322#define PSA_ALG_CTR ((psa_algorithm_t)0x04800001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100323#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100324
Moran Pekerbed71a22018-04-22 20:19:20 +0300325#define PSA_ALG_IS_STREAM_CIPHER(alg) \
326 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
327 PSA_ALG_STREAM_CIPHER)
328
Gilles Peskine8c9def32018-02-08 10:02:12 +0100329#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
330#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100331
Gilles Peskinea5926232018-03-28 14:16:50 +0200332#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW ((psa_algorithm_t)0x10010000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100333#define PSA_ALG_RSA_PSS_MGF1 ((psa_algorithm_t)0x10020000)
Gilles Peskine6944f9a2018-03-28 14:18:39 +0200334#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12010000)
335#define PSA_ALG_RSA_OAEP_MGF1_BASE ((psa_algorithm_t)0x12020000)
Gilles Peskinea5926232018-03-28 14:16:50 +0200336#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
337 (PSA_ALG_RSA_PKCS1V15_SIGN_RAW | ((hash_alg) & PSA_ALG_HASH_MASK))
338#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Gilles Peskine9673cc82018-04-11 16:57:49 +0200339 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_RAW)
340#define PSA_ALG_RSA_OAEP_MGF1(hash_alg) \
341 (PSA_ALG_RSA_OAEP_MGF1_RAW | ((hash_alg) & PSA_ALG_HASH_MASK))
342#define PSA_ALG_IS_RSA_OAEP_MGF1(alg) \
343 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_MGF1_RAW)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100344#define PSA_ALG_RSA_GET_HASH(alg) \
345 (((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100346
Gilles Peskined1e8e412018-06-07 09:49:39 +0200347#define PSA_ALG_ECDSA_RAW ((psa_algorithm_t)0x10030000)
348
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100349/**@}*/
350
351/** \defgroup key_management Key management
352 * @{
353 */
354
355/**
356 * \brief Import a key in binary format.
357 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100358 * This function supports any output from psa_export_key(). Refer to the
359 * documentation of psa_export_key() for the format for each key type.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100360 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100361 * \param key Slot where the key will be stored. This must be a
362 * valid slot for a key of the chosen type. It must
363 * be unoccupied.
364 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
365 * \param data Buffer containing the key data.
366 * \param data_length Size of the \c data buffer in bytes.
367 *
368 * \retval PSA_SUCCESS
369 * Success.
370 * \retval PSA_ERROR_NOT_SUPPORTED
371 * The key type or key size is not supported.
372 * \retval PSA_ERROR_INVALID_ARGUMENT
373 * The key slot is invalid,
374 * or the key data is not correctly formatted.
375 * \retval PSA_ERROR_OCCUPIED_SLOT
376 There is already a key in the specified slot.
377 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
378 * \retval PSA_ERROR_COMMUNICATION_FAILURE
379 * \retval PSA_ERROR_HARDWARE_FAILURE
380 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100381 */
382psa_status_t psa_import_key(psa_key_slot_t key,
383 psa_key_type_t type,
384 const uint8_t *data,
385 size_t data_length);
386
387/**
388 * \brief Destroy a key.
389 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100390 * \retval PSA_SUCCESS
391 * \retval PSA_ERROR_EMPTY_SLOT
392 * \retval PSA_ERROR_COMMUNICATION_FAILURE
393 * \retval PSA_ERROR_HARDWARE_FAILURE
394 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100395 */
396psa_status_t psa_destroy_key(psa_key_slot_t key);
397
398/**
399 * \brief Get basic metadata about a key.
400 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100401 * \param key Slot whose content is queried. This must
402 * be an occupied key slot.
403 * \param type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
404 * This may be a null pointer, in which case the key type
405 * is not written.
406 * \param bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100407 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100408 * is not written.
409 *
410 * \retval PSA_SUCCESS
411 * \retval PSA_ERROR_EMPTY_SLOT
412 * \retval PSA_ERROR_COMMUNICATION_FAILURE
413 * \retval PSA_ERROR_HARDWARE_FAILURE
414 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100415 */
416psa_status_t psa_get_key_information(psa_key_slot_t key,
417 psa_key_type_t *type,
418 size_t *bits);
419
420/**
421 * \brief Export a key in binary format.
422 *
423 * The output of this function can be passed to psa_import_key() to
424 * create an equivalent object.
425 *
426 * If a key is created with psa_import_key() and then exported with
427 * this function, it is not guaranteed that the resulting data is
428 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +0100429 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100430 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100431 * For standard key types, the output format is as follows:
432 *
433 * - For symmetric keys (including MAC keys), the format is the
434 * raw bytes of the key.
435 * - For DES, the key data consists of 8 bytes. The parity bits must be
436 * correct.
437 * - For Triple-DES, the format is the concatenation of the
438 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100439 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine308b91d2018-02-08 09:47:44 +0100440 * is the non-encrypted DER representation defined by PKCS\#8 (RFC 5208)
441 * as PrivateKeyInfo.
442 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
Gilles Peskine971f7062018-03-20 17:52:58 +0100443 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100444 *
445 * \param key Slot whose content is to be exported. This must
446 * be an occupied key slot.
447 * \param data Buffer where the key data is to be written.
448 * \param data_size Size of the \c data buffer in bytes.
449 * \param data_length On success, the number of bytes
450 * that make up the key data.
451 *
452 * \retval PSA_SUCCESS
453 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine92b30732018-03-03 21:29:30 +0100454 * \retval PSA_ERROR_NOT_PERMITTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100455 * \retval PSA_ERROR_COMMUNICATION_FAILURE
456 * \retval PSA_ERROR_HARDWARE_FAILURE
457 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100458 */
459psa_status_t psa_export_key(psa_key_slot_t key,
460 uint8_t *data,
461 size_t data_size,
462 size_t *data_length);
463
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100464/**
465 * \brief Export a public key or the public part of a key pair in binary format.
466 *
467 * The output of this function can be passed to psa_import_key() to
468 * create an object that is equivalent to the public key.
469 *
470 * For standard key types, the output format is as follows:
471 *
472 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
Moran Pekerdd4ea382018-04-03 15:30:03 +0300473 * the format is the DER representation of the public key defined by RFC 5280
Gilles Peskine971f7062018-03-20 17:52:58 +0100474 * as SubjectPublicKeyInfo.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100475 *
476 * \param key Slot whose content is to be exported. This must
477 * be an occupied key slot.
478 * \param data Buffer where the key data is to be written.
479 * \param data_size Size of the \c data buffer in bytes.
480 * \param data_length On success, the number of bytes
481 * that make up the key data.
482 *
483 * \retval PSA_SUCCESS
484 * \retval PSA_ERROR_EMPTY_SLOT
485 * \retval PSA_ERROR_INVALID_ARGUMENT
486 * \retval PSA_ERROR_COMMUNICATION_FAILURE
487 * \retval PSA_ERROR_HARDWARE_FAILURE
488 * \retval PSA_ERROR_TAMPERING_DETECTED
489 */
490psa_status_t psa_export_public_key(psa_key_slot_t key,
491 uint8_t *data,
492 size_t data_size,
493 size_t *data_length);
494
495/**@}*/
496
497/** \defgroup policy Key policies
498 * @{
499 */
500
501/** \brief Encoding of permitted usage on a key. */
502typedef uint32_t psa_key_usage_t;
503
Gilles Peskine7e198532018-03-08 07:50:30 +0100504/** Whether the key may be exported.
505 *
506 * A public key or the public part of a key pair may always be exported
507 * regardless of the value of this permission flag.
508 *
509 * If a key does not have export permission, implementations shall not
510 * allow the key to be exported in plain form from the cryptoprocessor,
511 * whether through psa_export_key() or through a proprietary interface.
512 * The key may however be exportable in a wrapped form, i.e. in a form
513 * where it is encrypted by another key.
514 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100515#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
516
Gilles Peskine7e198532018-03-08 07:50:30 +0100517/** Whether the key may be used to encrypt a message.
518 *
519 * For a key pair, this concerns the public key.
520 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100521#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +0100522
523/** Whether the key may be used to decrypt a message.
524 *
525 * For a key pair, this concerns the private key.
526 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100527#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +0100528
529/** Whether the key may be used to sign a message.
530 *
531 * For a key pair, this concerns the private key.
532 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100533#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +0100534
535/** Whether the key may be used to verify a message signature.
536 *
537 * For a key pair, this concerns the public key.
538 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100539#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
540
541/** The type of the key policy data structure.
542 *
543 * This is an implementation-defined \c struct. Applications should not
544 * make any assumptions about the content of this structure except
545 * as directed by the documentation of a specific implementation. */
546typedef struct psa_key_policy_s psa_key_policy_t;
547
548/** \brief Initialize a key policy structure to a default that forbids all
549 * usage of the key. */
550void psa_key_policy_init(psa_key_policy_t *policy);
551
Gilles Peskine7e198532018-03-08 07:50:30 +0100552/** \brief Set the standard fields of a policy structure.
553 *
554 * Note that this function does not make any consistency check of the
555 * parameters. The values are only checked when applying the policy to
556 * a key slot with psa_set_key_policy().
557 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100558void psa_key_policy_set_usage(psa_key_policy_t *policy,
559 psa_key_usage_t usage,
560 psa_algorithm_t alg);
561
562psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy);
563
564psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy);
565
566/** \brief Set the usage policy on a key slot.
567 *
568 * This function must be called on an empty key slot, before importing,
569 * generating or creating a key in the slot. Changing the policy of an
570 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100571 *
572 * Implementations may set restrictions on supported key policies
573 * depending on the key type and the key slot.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100574 */
575psa_status_t psa_set_key_policy(psa_key_slot_t key,
576 const psa_key_policy_t *policy);
577
Gilles Peskine7e198532018-03-08 07:50:30 +0100578/** \brief Get the usage policy for a key slot.
579 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100580psa_status_t psa_get_key_policy(psa_key_slot_t key,
581 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100582
583/**@}*/
584
Gilles Peskine609b6a52018-03-03 21:31:50 +0100585/** \defgroup persistence Key lifetime
586 * @{
587 */
588
589/** Encoding of key lifetimes.
590 */
591typedef uint32_t psa_key_lifetime_t;
592
593/** A volatile key slot retains its content as long as the application is
594 * running. It is guaranteed to be erased on a power reset.
595 */
596#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
597
598/** A persistent key slot retains its content as long as it is not explicitly
599 * destroyed.
600 */
601#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
602
603/** A write-once key slot may not be modified once a key has been set.
604 * It will retain its content as long as the device remains operational.
605 */
606#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
607
Gilles Peskined393e182018-03-08 07:49:16 +0100608/** \brief Retrieve the lifetime of a key slot.
609 *
610 * The assignment of lifetimes to slots is implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200611 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +0200612 * \param key Slot to query.
mohammad1603804cd712018-03-20 22:44:08 +0200613 * \param lifetime On success, the lifetime value.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200614 *
mohammad1603804cd712018-03-20 22:44:08 +0200615 * \retval PSA_SUCCESS
616 * Success.
617 * \retval PSA_ERROR_INVALID_ARGUMENT
mohammad1603a7d245a2018-04-17 00:40:08 -0700618 * The key slot is invalid.
Gilles Peskinef0c9dd32018-04-17 14:11:07 +0200619 * \retval PSA_ERROR_COMMUNICATION_FAILURE
620 * \retval PSA_ERROR_HARDWARE_FAILURE
621 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +0100622 */
Gilles Peskine609b6a52018-03-03 21:31:50 +0100623psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
624 psa_key_lifetime_t *lifetime);
625
Gilles Peskined393e182018-03-08 07:49:16 +0100626/** \brief Change the lifetime of a key slot.
627 *
628 * Whether the lifetime of a key slot can be changed at all, and if so
Gilles Peskine19067982018-03-20 17:54:53 +0100629 * whether the lifetime of an occupied key slot can be changed, is
Gilles Peskined393e182018-03-08 07:49:16 +0100630 * implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200631 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +0200632 * \param key Slot whose lifetime is to be changed.
633 * \param lifetime The lifetime value to set for the given key slot.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200634 *
mohammad1603804cd712018-03-20 22:44:08 +0200635 * \retval PSA_SUCCESS
636 * Success.
637 * \retval PSA_ERROR_INVALID_ARGUMENT
638 * The key slot is invalid,
mohammad1603a7d245a2018-04-17 00:40:08 -0700639 * or the lifetime value is invalid.
Gilles Peskinef0c9dd32018-04-17 14:11:07 +0200640 * \retval PSA_ERROR_NOT_SUPPORTED
641 * The implementation does not support the specified lifetime value,
642 * at least for the specified key slot.
643 * \retval PSA_ERROR_OCCUPIED_SLOT
644 * The slot contains a key, and the implementation does not support
645 * changing the lifetime of an occupied slot.
646 * \retval PSA_ERROR_COMMUNICATION_FAILURE
647 * \retval PSA_ERROR_HARDWARE_FAILURE
648 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +0100649 */
650psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
mohammad1603ea050092018-04-17 00:31:34 -0700651 psa_key_lifetime_t lifetime);
Gilles Peskined393e182018-03-08 07:49:16 +0100652
Gilles Peskine609b6a52018-03-03 21:31:50 +0100653/**@}*/
654
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100655/** \defgroup hash Message digests
656 * @{
657 */
658
Gilles Peskine308b91d2018-02-08 09:47:44 +0100659/** The type of the state data structure for multipart hash operations.
660 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100661 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100662 * make any assumptions about the content of this structure except
663 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100664typedef struct psa_hash_operation_s psa_hash_operation_t;
665
Gilles Peskine308b91d2018-02-08 09:47:44 +0100666/** The size of the output of psa_hash_finish(), in bytes.
667 *
668 * This is also the hash size that psa_hash_verify() expects.
669 *
670 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
671 * #PSA_ALG_IS_HASH(alg) is true).
672 *
673 * \return The hash size for the specified hash algorithm.
674 * If the hash algorithm is not recognized, return 0.
675 * An implementation may return either 0 or the correct size
676 * for a hash algorithm that it recognizes, but does not support.
677 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100678#define PSA_HASH_FINAL_SIZE(alg) \
679 ( \
680 (alg) == PSA_ALG_MD2 ? 16 : \
681 (alg) == PSA_ALG_MD4 ? 16 : \
682 (alg) == PSA_ALG_MD5 ? 16 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100683 (alg) == PSA_ALG_RIPEMD160 ? 20 : \
684 (alg) == PSA_ALG_SHA_1 ? 20 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100685 (alg) == PSA_ALG_SHA_224 ? 28 : \
686 (alg) == PSA_ALG_SHA_256 ? 32 : \
687 (alg) == PSA_ALG_SHA_384 ? 48 : \
688 (alg) == PSA_ALG_SHA_512 ? 64 : \
689 (alg) == PSA_ALG_SHA_512_224 ? 28 : \
690 (alg) == PSA_ALG_SHA_512_256 ? 32 : \
691 (alg) == PSA_ALG_SHA3_224 ? 28 : \
692 (alg) == PSA_ALG_SHA3_256 ? 32 : \
693 (alg) == PSA_ALG_SHA3_384 ? 48 : \
694 (alg) == PSA_ALG_SHA3_512 ? 64 : \
695 0)
696
Gilles Peskine308b91d2018-02-08 09:47:44 +0100697/** Start a multipart hash operation.
698 *
699 * The sequence of operations to calculate a hash (message digest)
700 * is as follows:
701 * -# Allocate an operation object which will be passed to all the functions
702 * listed here.
703 * -# Call psa_hash_start() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100704 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100705 * of the message each time. The hash that is calculated is the hash
706 * of the concatenation of these messages in order.
707 * -# To calculate the hash, call psa_hash_finish().
708 * To compare the hash with an expected value, call psa_hash_verify().
709 *
710 * The application may call psa_hash_abort() at any time after the operation
711 * has been initialized with psa_hash_start().
712 *
713 * After a successful call to psa_hash_start(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100714 * eventually terminate the operation. The following events terminate an
715 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100716 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100717 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100718 *
719 * \param operation
720 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
721 * such that #PSA_ALG_IS_HASH(alg) is true).
722 *
723 * \retval PSA_SUCCESS
724 * Success.
725 * \retval PSA_ERROR_NOT_SUPPORTED
726 * \c alg is not supported or is not a hash algorithm.
727 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
728 * \retval PSA_ERROR_COMMUNICATION_FAILURE
729 * \retval PSA_ERROR_HARDWARE_FAILURE
730 * \retval PSA_ERROR_TAMPERING_DETECTED
731 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100732psa_status_t psa_hash_start(psa_hash_operation_t *operation,
733 psa_algorithm_t alg);
734
Gilles Peskine308b91d2018-02-08 09:47:44 +0100735/** Add a message fragment to a multipart hash operation.
736 *
737 * The application must call psa_hash_start() before calling this function.
738 *
739 * If this function returns an error status, the operation becomes inactive.
740 *
741 * \param operation Active hash operation.
742 * \param input Buffer containing the message fragment to hash.
743 * \param input_length Size of the \c input buffer in bytes.
744 *
745 * \retval PSA_SUCCESS
746 * Success.
747 * \retval PSA_ERROR_BAD_STATE
748 * The operation state is not valid (not started, or already completed).
749 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
750 * \retval PSA_ERROR_COMMUNICATION_FAILURE
751 * \retval PSA_ERROR_HARDWARE_FAILURE
752 * \retval PSA_ERROR_TAMPERING_DETECTED
753 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100754psa_status_t psa_hash_update(psa_hash_operation_t *operation,
755 const uint8_t *input,
756 size_t input_length);
757
Gilles Peskine308b91d2018-02-08 09:47:44 +0100758/** Finish the calculation of the hash of a message.
759 *
760 * The application must call psa_hash_start() before calling this function.
761 * This function calculates the hash of the message formed by concatenating
762 * the inputs passed to preceding calls to psa_hash_update().
763 *
764 * When this function returns, the operation becomes inactive.
765 *
766 * \warning Applications should not call this function if they expect
767 * a specific value for the hash. Call psa_hash_verify() instead.
768 * Beware that comparing integrity or authenticity data such as
769 * hash values with a function such as \c memcmp is risky
770 * because the time taken by the comparison may leak information
771 * about the hashed data which could allow an attacker to guess
772 * a valid hash and thereby bypass security controls.
773 *
774 * \param operation Active hash operation.
775 * \param hash Buffer where the hash is to be written.
776 * \param hash_size Size of the \c hash buffer in bytes.
777 * \param hash_length On success, the number of bytes
778 * that make up the hash value. This is always
779 * #PSA_HASH_FINAL_SIZE(alg) where \c alg is the
780 * hash algorithm that is calculated.
781 *
782 * \retval PSA_SUCCESS
783 * Success.
784 * \retval PSA_ERROR_BAD_STATE
785 * The operation state is not valid (not started, or already completed).
786 * \retval PSA_ERROR_BUFFER_TOO_SMALL
787 * The size of the \c hash buffer is too small. You can determine a
788 * sufficient buffer size by calling #PSA_HASH_FINAL_SIZE(alg)
789 * where \c alg is the hash algorithm that is calculated.
790 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
791 * \retval PSA_ERROR_COMMUNICATION_FAILURE
792 * \retval PSA_ERROR_HARDWARE_FAILURE
793 * \retval PSA_ERROR_TAMPERING_DETECTED
794 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100795psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
796 uint8_t *hash,
797 size_t hash_size,
798 size_t *hash_length);
799
Gilles Peskine308b91d2018-02-08 09:47:44 +0100800/** Finish the calculation of the hash of a message and compare it with
801 * an expected value.
802 *
803 * The application must call psa_hash_start() before calling this function.
804 * This function calculates the hash of the message formed by concatenating
805 * the inputs passed to preceding calls to psa_hash_update(). It then
806 * compares the calculated hash with the expected hash passed as a
807 * parameter to this function.
808 *
809 * When this function returns, the operation becomes inactive.
810 *
Gilles Peskine19067982018-03-20 17:54:53 +0100811 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100812 * comparison between the actual hash and the expected hash is performed
813 * in constant time.
814 *
815 * \param operation Active hash operation.
816 * \param hash Buffer containing the expected hash value.
817 * \param hash_length Size of the \c hash buffer in bytes.
818 *
819 * \retval PSA_SUCCESS
820 * The expected hash is identical to the actual hash of the message.
821 * \retval PSA_ERROR_INVALID_SIGNATURE
822 * The hash of the message was calculated successfully, but it
823 * differs from the expected hash.
824 * \retval PSA_ERROR_BAD_STATE
825 * The operation state is not valid (not started, or already completed).
826 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
827 * \retval PSA_ERROR_COMMUNICATION_FAILURE
828 * \retval PSA_ERROR_HARDWARE_FAILURE
829 * \retval PSA_ERROR_TAMPERING_DETECTED
830 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100831psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
832 const uint8_t *hash,
833 size_t hash_length);
834
Gilles Peskine308b91d2018-02-08 09:47:44 +0100835/** Abort a hash operation.
836 *
837 * This function may be called at any time after psa_hash_start().
838 * Aborting an operation frees all associated resources except for the
839 * \c operation structure itself.
840 *
841 * Implementation should strive to be robust and handle inactive hash
842 * operations safely (do nothing and return #PSA_ERROR_BAD_STATE). However,
843 * application writers should beware that uninitialized memory may happen
844 * to be indistinguishable from an active hash operation, and the behavior
845 * of psa_hash_abort() is undefined in this case.
846 *
847 * \param operation Active hash operation.
848 *
849 * \retval PSA_SUCCESS
850 * \retval PSA_ERROR_BAD_STATE
851 * \c operation is not an active hash operation.
852 * \retval PSA_ERROR_COMMUNICATION_FAILURE
853 * \retval PSA_ERROR_HARDWARE_FAILURE
854 * \retval PSA_ERROR_TAMPERING_DETECTED
855 */
856psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100857
858/**@}*/
859
Gilles Peskine8c9def32018-02-08 10:02:12 +0100860/** \defgroup MAC Message authentication codes
861 * @{
862 */
863
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100864/** The type of the state data structure for multipart MAC operations.
865 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100866 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100867 * make any assumptions about the content of this structure except
868 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100869typedef struct psa_mac_operation_s psa_mac_operation_t;
870
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100871/** The size of the output of psa_mac_finish(), in bytes.
872 *
873 * This is also the MAC size that psa_mac_verify() expects.
874 *
875 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
876 * #PSA_ALG_IS_MAC(alg) is true).
877 *
878 * \return The MAC size for the specified algorithm.
879 * If the MAC algorithm is not recognized, return 0.
880 * An implementation may return either 0 or the correct size
881 * for a MAC algorithm that it recognizes, but does not support.
882 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100883#define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \
884 (PSA_ALG_IS_HMAC(alg) ? PSA_HASH_FINAL_SIZE(PSA_ALG_HMAC_HASH(alg)) : \
885 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
886 0)
887
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100888/** Start a multipart MAC operation.
889 *
890 * The sequence of operations to calculate a MAC (message authentication code)
891 * is as follows:
892 * -# Allocate an operation object which will be passed to all the functions
893 * listed here.
894 * -# Call psa_mac_start() to specify the algorithm and key.
895 * The key remains associated with the operation even if the content
896 * of the key slot changes.
897 * -# Call psa_mac_update() zero, one or more times, passing a fragment
898 * of the message each time. The MAC that is calculated is the MAC
899 * of the concatenation of these messages in order.
900 * -# To calculate the MAC, call psa_mac_finish().
901 * To compare the MAC with an expected value, call psa_mac_verify().
902 *
903 * The application may call psa_mac_abort() at any time after the operation
904 * has been initialized with psa_mac_start().
905 *
906 * After a successful call to psa_mac_start(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100907 * eventually terminate the operation. The following events terminate an
908 * operation:
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100909 * - A failed call to psa_mac_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100910 * - A call to psa_mac_finish(), psa_mac_verify() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100911 *
912 * \param operation
913 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
914 * such that #PSA_ALG_IS_MAC(alg) is true).
915 *
916 * \retval PSA_SUCCESS
917 * Success.
918 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine92b30732018-03-03 21:29:30 +0100919 * \retval PSA_ERROR_NOT_PERMITTED
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100920 * \retval PSA_ERROR_INVALID_ARGUMENT
921 * \c key is not compatible with \c alg.
922 * \retval PSA_ERROR_NOT_SUPPORTED
923 * \c alg is not supported or is not a MAC algorithm.
924 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
925 * \retval PSA_ERROR_COMMUNICATION_FAILURE
926 * \retval PSA_ERROR_HARDWARE_FAILURE
927 * \retval PSA_ERROR_TAMPERING_DETECTED
928 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100929psa_status_t psa_mac_start(psa_mac_operation_t *operation,
930 psa_key_slot_t key,
931 psa_algorithm_t alg);
932
933psa_status_t psa_mac_update(psa_mac_operation_t *operation,
934 const uint8_t *input,
935 size_t input_length);
936
937psa_status_t psa_mac_finish(psa_mac_operation_t *operation,
938 uint8_t *mac,
939 size_t mac_size,
940 size_t *mac_length);
941
942psa_status_t psa_mac_verify(psa_mac_operation_t *operation,
943 const uint8_t *mac,
944 size_t mac_length);
945
946psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
947
948/**@}*/
949
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100950/** \defgroup cipher Symmetric ciphers
951 * @{
952 */
953
954/** The type of the state data structure for multipart cipher operations.
955 *
956 * This is an implementation-defined \c struct. Applications should not
957 * make any assumptions about the content of this structure except
958 * as directed by the documentation of a specific implementation. */
959typedef struct psa_cipher_operation_s psa_cipher_operation_t;
960
961/** Set the key for a multipart symmetric encryption operation.
962 *
963 * The sequence of operations to encrypt a message with a symmetric cipher
964 * is as follows:
965 * -# Allocate an operation object which will be passed to all the functions
966 * listed here.
967 * -# Call psa_encrypt_setup() to specify the algorithm and key.
968 * The key remains associated with the operation even if the content
969 * of the key slot changes.
970 * -# Call either psa_encrypt_generate_iv() or psa_encrypt_set_iv() to
971 * generate or set the IV (initialization vector). You should use
972 * psa_encrypt_generate_iv() unless the protocol you are implementing
973 * requires a specific IV value.
974 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
975 * of the message each time.
976 * -# Call psa_cipher_finish().
977 *
978 * The application may call psa_cipher_abort() at any time after the operation
979 * has been initialized with psa_encrypt_setup().
980 *
981 * After a successful call to psa_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100982 * eventually terminate the operation. The following events terminate an
983 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100984 * - A failed call to psa_encrypt_generate_iv(), psa_encrypt_set_iv()
985 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100986 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100987 *
988 * \param operation
989 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
990 * such that #PSA_ALG_IS_CIPHER(alg) is true).
991 *
992 * \retval PSA_SUCCESS
993 * Success.
994 * \retval PSA_ERROR_EMPTY_SLOT
995 * \retval PSA_ERROR_NOT_PERMITTED
996 * \retval PSA_ERROR_INVALID_ARGUMENT
997 * \c key is not compatible with \c alg.
998 * \retval PSA_ERROR_NOT_SUPPORTED
999 * \c alg is not supported or is not a cipher algorithm.
1000 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1001 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1002 * \retval PSA_ERROR_HARDWARE_FAILURE
1003 * \retval PSA_ERROR_TAMPERING_DETECTED
1004 */
1005psa_status_t psa_encrypt_setup(psa_cipher_operation_t *operation,
1006 psa_key_slot_t key,
1007 psa_algorithm_t alg);
1008
1009/** Set the key for a multipart symmetric decryption operation.
1010 *
1011 * The sequence of operations to decrypt a message with a symmetric cipher
1012 * is as follows:
1013 * -# Allocate an operation object which will be passed to all the functions
1014 * listed here.
1015 * -# Call psa_decrypt_setup() to specify the algorithm and key.
1016 * The key remains associated with the operation even if the content
1017 * of the key slot changes.
1018 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1019 * decryption. If the IV is prepended to the ciphertext, you can call
1020 * psa_cipher_update() on a buffer containing the IV followed by the
1021 * beginning of the message.
1022 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1023 * of the message each time.
1024 * -# Call psa_cipher_finish().
1025 *
1026 * The application may call psa_cipher_abort() at any time after the operation
1027 * has been initialized with psa_encrypt_setup().
1028 *
1029 * After a successful call to psa_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001030 * eventually terminate the operation. The following events terminate an
1031 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001032 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001033 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001034 *
1035 * \param operation
1036 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
1037 * such that #PSA_ALG_IS_CIPHER(alg) is true).
1038 *
1039 * \retval PSA_SUCCESS
1040 * Success.
1041 * \retval PSA_ERROR_EMPTY_SLOT
1042 * \retval PSA_ERROR_NOT_PERMITTED
1043 * \retval PSA_ERROR_INVALID_ARGUMENT
1044 * \c key is not compatible with \c alg.
1045 * \retval PSA_ERROR_NOT_SUPPORTED
1046 * \c alg is not supported or is not a cipher algorithm.
1047 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1048 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1049 * \retval PSA_ERROR_HARDWARE_FAILURE
1050 * \retval PSA_ERROR_TAMPERING_DETECTED
1051 */
1052psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation,
1053 psa_key_slot_t key,
1054 psa_algorithm_t alg);
1055
1056psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation,
1057 unsigned char *iv,
1058 size_t iv_size,
1059 size_t *iv_length);
1060
1061psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation,
1062 const unsigned char *iv,
1063 size_t iv_length);
1064
1065psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1066 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001067 size_t input_length,
1068 unsigned char *output,
1069 size_t output_size,
1070 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001071
1072psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001073 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001074 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001075 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001076
1077psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1078
1079/**@}*/
1080
Gilles Peskine3b555712018-03-03 21:27:57 +01001081/** \defgroup aead Authenticated encryption with associated data (AEAD)
1082 * @{
1083 */
1084
1085/** The type of the state data structure for multipart AEAD operations.
1086 *
1087 * This is an implementation-defined \c struct. Applications should not
1088 * make any assumptions about the content of this structure except
1089 * as directed by the documentation of a specific implementation. */
1090typedef struct psa_aead_operation_s psa_aead_operation_t;
1091
1092/** Set the key for a multipart authenticated encryption operation.
1093 *
1094 * The sequence of operations to authenticate-and-encrypt a message
1095 * is as follows:
1096 * -# Allocate an operation object which will be passed to all the functions
1097 * listed here.
1098 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
1099 * The key remains associated with the operation even if the content
1100 * of the key slot changes.
1101 * -# Call either psa_aead_generate_iv() or psa_aead_set_iv() to
1102 * generate or set the IV (initialization vector). You should use
1103 * psa_encrypt_generate_iv() unless the protocol you are implementing
1104 * requires a specific IV value.
1105 * -# Call psa_aead_update_ad() to pass the associated data that is
1106 * to be authenticated but not encrypted. You may omit this step if
1107 * there is no associated data.
1108 * -# Call psa_aead_update() zero, one or more times, passing a fragment
1109 * of the data to encrypt each time.
1110 * -# Call psa_aead_finish().
1111 *
1112 * The application may call psa_aead_abort() at any time after the operation
1113 * has been initialized with psa_aead_encrypt_setup().
1114 *
Gilles Peskineed522972018-03-20 17:54:15 +01001115 * After a successful call to psa_aead_encrypt_setup(), the application must
1116 * eventually terminate the operation. The following events terminate an
1117 * operation:
Gilles Peskine3b555712018-03-03 21:27:57 +01001118 * - A failed call to psa_aead_generate_iv(), psa_aead_set_iv(),
1119 * psa_aead_update_ad() or psa_aead_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001120 * - A call to psa_aead_finish() or psa_aead_abort().
Gilles Peskine3b555712018-03-03 21:27:57 +01001121 *
1122 * \param operation
1123 * \param alg The AEAD algorithm to compute (\c PSA_ALG_XXX value
1124 * such that #PSA_ALG_IS_AEAD(alg) is true).
1125 *
1126 * \retval PSA_SUCCESS
1127 * Success.
1128 * \retval PSA_ERROR_EMPTY_SLOT
1129 * \retval PSA_ERROR_NOT_PERMITTED
1130 * \retval PSA_ERROR_INVALID_ARGUMENT
1131 * \c key is not compatible with \c alg.
1132 * \retval PSA_ERROR_NOT_SUPPORTED
1133 * \c alg is not supported or is not an AEAD algorithm.
1134 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1135 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1136 * \retval PSA_ERROR_HARDWARE_FAILURE
1137 * \retval PSA_ERROR_TAMPERING_DETECTED
1138 */
1139psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
1140 psa_key_slot_t key,
1141 psa_algorithm_t alg);
1142
1143/** Set the key for a multipart authenticated decryption operation.
1144 *
1145 * The sequence of operations to authenticated and decrypt a message
1146 * is as follows:
1147 * -# Allocate an operation object which will be passed to all the functions
1148 * listed here.
1149 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
1150 * The key remains associated with the operation even if the content
1151 * of the key slot changes.
1152 * -# Call psa_aead_set_iv() to pass the initialization vector (IV)
1153 * for the authenticated decryption.
1154 * -# Call psa_aead_update_ad() to pass the associated data that is
1155 * to be authenticated but not encrypted. You may omit this step if
1156 * there is no associated data.
1157 * -# Call psa_aead_update() zero, one or more times, passing a fragment
1158 * of the data to decrypt each time.
1159 * -# Call psa_aead_finish().
1160 *
1161 * The application may call psa_aead_abort() at any time after the operation
1162 * has been initialized with psa_aead_decrypt_setup().
1163 *
Gilles Peskineed522972018-03-20 17:54:15 +01001164 * After a successful call to psa_aead_decrypt_setup(), the application must
1165 * eventually terminate the operation. The following events terminate an
1166 * operation:
Gilles Peskine3b555712018-03-03 21:27:57 +01001167 * - A failed call to psa_aead_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001168 * - A call to psa_aead_finish() or psa_aead_abort().
Gilles Peskine3b555712018-03-03 21:27:57 +01001169 *
1170 * \param operation
Gilles Peskine19067982018-03-20 17:54:53 +01001171 * \param alg The AEAD algorithm to compute (\c PSA_ALG_XXX value
1172 * such that #PSA_ALG_IS_AEAD(alg) is true).
Gilles Peskine3b555712018-03-03 21:27:57 +01001173 *
1174 * \retval PSA_SUCCESS
1175 * Success.
1176 * \retval PSA_ERROR_EMPTY_SLOT
1177 * \retval PSA_ERROR_NOT_PERMITTED
1178 * \retval PSA_ERROR_INVALID_ARGUMENT
1179 * \c key is not compatible with \c alg.
1180 * \retval PSA_ERROR_NOT_SUPPORTED
Gilles Peskine19067982018-03-20 17:54:53 +01001181 * \c alg is not supported or is not an AEAD algorithm.
Gilles Peskine3b555712018-03-03 21:27:57 +01001182 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1183 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1184 * \retval PSA_ERROR_HARDWARE_FAILURE
1185 * \retval PSA_ERROR_TAMPERING_DETECTED
1186 */
1187psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
1188 psa_key_slot_t key,
1189 psa_algorithm_t alg);
1190
1191psa_status_t psa_aead_generate_iv(psa_aead_operation_t *operation,
1192 unsigned char *iv,
1193 size_t iv_size,
1194 size_t *iv_length);
1195
1196psa_status_t psa_aead_set_iv(psa_aead_operation_t *operation,
1197 const unsigned char *iv,
1198 size_t iv_length);
1199
1200psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
1201 const uint8_t *input,
1202 size_t input_length);
1203
1204psa_status_t psa_aead_update(psa_aead_operation_t *operation,
1205 const uint8_t *input,
1206 size_t input_length);
1207
1208psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
1209 uint8_t *tag,
1210 size_t tag_size,
1211 size_t *tag_length);
1212
1213psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
1214 uint8_t *tag,
1215 size_t tag_length);
1216
1217psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
1218
1219/**@}*/
1220
Gilles Peskine20035e32018-02-03 22:44:14 +01001221/** \defgroup asymmetric Asymmetric cryptography
1222 * @{
1223 */
1224
1225/**
Gilles Peskine0189e752018-02-03 23:57:22 +01001226 * \brief Maximum ECDSA signature size for a given curve bit size
1227 *
1228 * \param curve_bits Curve size in bits
1229 * \return Maximum signature size in bytes
1230 *
1231 * \note This macro returns a compile-time constant if its argument is one.
1232 *
1233 * \warning This macro may evaluate its argument multiple times.
1234 */
1235/*
1236 * RFC 4492 page 20:
1237 *
1238 * Ecdsa-Sig-Value ::= SEQUENCE {
1239 * r INTEGER,
1240 * s INTEGER
1241 * }
1242 *
1243 * Size is at most
1244 * 1 (tag) + 1 (len) + 1 (initial 0) + curve_bytes for each of r and s,
1245 * twice that + 1 (tag) + 2 (len) for the sequence
1246 * (assuming curve_bytes is less than 126 for r and s,
1247 * and less than 124 (total len <= 255) for the sequence)
1248 */
1249#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
1250 ( /*T,L of SEQUENCE*/ ((curve_bits) >= 61 * 8 ? 3 : 2) + \
1251 /*T,L of r,s*/ 2 * (((curve_bits) >= 127 * 8 ? 3 : 2) + \
1252 /*V of r,s*/ ((curve_bits) + 8) / 8))
1253
1254
Gilles Peskine308b91d2018-02-08 09:47:44 +01001255/** Safe signature buffer size for psa_asymmetric_sign().
1256 *
1257 * This macro returns a safe buffer size for a signature using a key
1258 * of the specified type and size, with the specified algorithm.
1259 * Note that the actual size of the signature may be smaller
1260 * (some algorithms produce a variable-size signature).
1261 *
1262 * \warning This function may call its arguments multiple times or
1263 * zero times, so you should not pass arguments that contain
1264 * side effects.
1265 *
1266 * \param key_type An asymmetric key type (this may indifferently be a
1267 * key pair type or a public key type).
1268 * \param key_bits The size of the key in bits.
1269 * \param alg The signature algorithm.
1270 *
1271 * \return If the parameters are valid and supported, return
1272 * a buffer size in bytes that guarantees that
1273 * psa_asymmetric_sign() will not fail with
1274 * #PSA_ERROR_BUFFER_TOO_SMALL.
1275 * If the parameters are a valid combination that is not supported
1276 * by the implementation, this macro either shall return either a
1277 * sensible size or 0.
1278 * If the parameters are not valid, the
1279 * return value is unspecified.
1280 *
1281 */
Gilles Peskine0189e752018-02-03 23:57:22 +01001282#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine2905a7a2018-03-07 16:39:31 +01001283 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine0189e752018-02-03 23:57:22 +01001284 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
Gilles Peskine84845652018-03-28 14:17:40 +02001285 ((void)alg, 0))
Gilles Peskine0189e752018-02-03 23:57:22 +01001286
1287/**
Gilles Peskine20035e32018-02-03 22:44:14 +01001288 * \brief Sign a hash or short message with a private key.
1289 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001290 * \param key Key slot containing an asymmetric key pair.
1291 * \param alg A signature algorithm that is compatible with
1292 * the type of \c key.
1293 * \param hash The message to sign.
1294 * \param hash_length Size of the \c hash buffer in bytes.
1295 * \param salt A salt or label, if supported by the signature
1296 * algorithm.
1297 * If the signature algorithm does not support a
1298 * salt, pass \c NULL.
1299 * If the signature algorithm supports an optional
1300 * salt and you do not want to pass a salt,
1301 * pass \c NULL.
1302 * \param salt_length Size of the \c salt buffer in bytes.
1303 * If \c salt is \c NULL, pass 0.
1304 * \param signature Buffer where the signature is to be written.
1305 * \param signature_size Size of the \c signature buffer in bytes.
1306 * \param signature_length On success, the number of bytes
1307 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001308 *
1309 * \retval PSA_SUCCESS
1310 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1311 * The size of the \c signature buffer is too small. You can
1312 * determine a sufficient buffer size by calling
1313 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg)
1314 * where \c key_type and \c key_bits are the type and bit-size
1315 * respectively of \c key.
1316 * \retval PSA_ERROR_NOT_SUPPORTED
1317 * \retval PSA_ERROR_INVALID_ARGUMENT
1318 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1319 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1320 * \retval PSA_ERROR_HARDWARE_FAILURE
1321 * \retval PSA_ERROR_TAMPERING_DETECTED
1322 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01001323 */
1324psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
1325 psa_algorithm_t alg,
1326 const uint8_t *hash,
1327 size_t hash_length,
1328 const uint8_t *salt,
1329 size_t salt_length,
1330 uint8_t *signature,
1331 size_t signature_size,
1332 size_t *signature_length);
1333
1334/**
1335 * \brief Verify the signature a hash or short message using a public key.
1336 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001337 * \param key Key slot containing a public key or an
1338 * asymmetric key pair.
1339 * \param alg A signature algorithm that is compatible with
1340 * the type of \c key.
1341 * \param hash The message whose signature is to be verified.
1342 * \param hash_length Size of the \c hash buffer in bytes.
1343 * \param salt A salt or label, if supported by the signature
1344 * algorithm.
1345 * If the signature algorithm does not support a
1346 * salt, pass \c NULL.
1347 * If the signature algorithm supports an optional
1348 * salt and you do not want to pass a salt,
1349 * pass \c NULL.
1350 * \param salt_length Size of the \c salt buffer in bytes.
1351 * If \c salt is \c NULL, pass 0.
1352 * \param signature Buffer containing the signature to verify.
1353 * \param signature_size Size of the \c signature buffer in bytes.
1354 *
1355 * \retval PSA_SUCCESS
1356 * The signature is valid.
1357 * \retval PSA_ERROR_INVALID_SIGNATURE
1358 * The calculation was perfomed successfully, but the passed
1359 * signature is not a valid signature.
1360 * \retval PSA_ERROR_NOT_SUPPORTED
1361 * \retval PSA_ERROR_INVALID_ARGUMENT
1362 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1363 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1364 * \retval PSA_ERROR_HARDWARE_FAILURE
1365 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01001366 */
1367psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
1368 psa_algorithm_t alg,
1369 const uint8_t *hash,
1370 size_t hash_length,
1371 const uint8_t *salt,
1372 size_t salt_length,
1373 uint8_t *signature,
1374 size_t signature_size);
1375
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001376#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine06297932018-04-11 16:58:22 +02001377 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
1378 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
1379 0)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001380#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine06297932018-04-11 16:58:22 +02001381 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
1382 PSA_BITS_TO_BYTES(key_bits) - ((alg) == PSA_ALG_IS_RSA_OAEP_MGF1 ? \
1383 2 * (PSA_ALG_RSA_GET_HASH(alg) + 1) : \
1384 11 /*PKCS#1v1.5*/) : \
1385 0)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001386
1387/**
1388 * \brief Encrypt a short message with a public key.
1389 *
1390 * \param key Key slot containing a public key or an asymmetric
1391 * key pair.
1392 * \param alg An asymmetric encryption algorithm that is
1393 * compatible with the type of \c key.
1394 * \param input The message to encrypt.
1395 * \param input_length Size of the \c input buffer in bytes.
1396 * \param salt A salt or label, if supported by the encryption
1397 * algorithm.
1398 * If the algorithm does not support a
1399 * salt, pass \c NULL.
1400 * If the algorithm supports an optional
1401 * salt and you do not want to pass a salt,
1402 * pass \c NULL.
1403 *
1404 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1405 * supported.
1406 * \param salt_length Size of the \c salt buffer in bytes.
1407 * If \c salt is \c NULL, pass 0.
1408 * \param output Buffer where the encrypted message is to be written.
1409 * \param output_size Size of the \c output buffer in bytes.
1410 * \param output_length On success, the number of bytes
1411 * that make up the returned output.
1412 *
1413 * \retval PSA_SUCCESS
1414 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1415 * The size of the \c output buffer is too small. You can
1416 * determine a sufficient buffer size by calling
1417 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1418 * where \c key_type and \c key_bits are the type and bit-size
1419 * respectively of \c key.
1420 * \retval PSA_ERROR_NOT_SUPPORTED
1421 * \retval PSA_ERROR_INVALID_ARGUMENT
1422 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1423 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1424 * \retval PSA_ERROR_HARDWARE_FAILURE
1425 * \retval PSA_ERROR_TAMPERING_DETECTED
1426 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1427 */
1428psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
1429 psa_algorithm_t alg,
1430 const uint8_t *input,
1431 size_t input_length,
1432 const uint8_t *salt,
1433 size_t salt_length,
1434 uint8_t *output,
1435 size_t output_size,
1436 size_t *output_length);
1437
1438/**
1439 * \brief Decrypt a short message with a private key.
1440 *
1441 * \param key Key slot containing an asymmetric key pair.
1442 * \param alg An asymmetric encryption algorithm that is
1443 * compatible with the type of \c key.
1444 * \param input The message to decrypt.
1445 * \param input_length Size of the \c input buffer in bytes.
1446 * \param salt A salt or label, if supported by the encryption
1447 * algorithm.
1448 * If the algorithm does not support a
1449 * salt, pass \c NULL.
1450 * If the algorithm supports an optional
1451 * salt and you do not want to pass a salt,
1452 * pass \c NULL.
1453 *
1454 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1455 * supported.
1456 * \param salt_length Size of the \c salt buffer in bytes.
1457 * If \c salt is \c NULL, pass 0.
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001458 * \param output Buffer where the decrypted message is to be written.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001459 * \param output_size Size of the \c output buffer in bytes.
1460 * \param output_length On success, the number of bytes
1461 * that make up the returned output.
1462 *
1463 * \retval PSA_SUCCESS
1464 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1465 * The size of the \c output buffer is too small. You can
1466 * determine a sufficient buffer size by calling
1467 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1468 * where \c key_type and \c key_bits are the type and bit-size
1469 * respectively of \c key.
1470 * \retval PSA_ERROR_NOT_SUPPORTED
1471 * \retval PSA_ERROR_INVALID_ARGUMENT
1472 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1473 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1474 * \retval PSA_ERROR_HARDWARE_FAILURE
1475 * \retval PSA_ERROR_TAMPERING_DETECTED
1476 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1477 * \retval PSA_ERROR_INVALID_PADDING
1478 */
1479psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
1480 psa_algorithm_t alg,
1481 const uint8_t *input,
1482 size_t input_length,
1483 const uint8_t *salt,
1484 size_t salt_length,
1485 uint8_t *output,
1486 size_t output_size,
1487 size_t *output_length);
1488
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001489/**@}*/
1490
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001491/** \defgroup generation Key generation
1492 * @{
1493 */
1494
1495/**
1496 * \brief Generate random bytes.
1497 *
1498 * \warning This function **can** fail! Callers MUST check the return status
1499 * and MUST NOT use the content of the output buffer if the return
1500 * status is not #PSA_SUCCESS.
1501 *
1502 * \note To generate a key, use psa_generate_key() instead.
1503 *
1504 * \param output Output buffer for the generated data.
1505 * \param output_size Number of bytes to generate and output.
1506 *
1507 * \retval PSA_SUCCESS
1508 * \retval PSA_ERROR_NOT_SUPPORTED
1509 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1510 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1511 * \retval PSA_ERROR_HARDWARE_FAILURE
1512 * \retval PSA_ERROR_TAMPERING_DETECTED
1513 */
1514psa_status_t psa_generate_random(uint8_t *output,
1515 size_t output_size);
1516
1517/**
1518 * \brief Generate a key or key pair.
1519 *
1520 * \param key Slot where the key will be stored. This must be a
1521 * valid slot for a key of the chosen type. It must
1522 * be unoccupied.
1523 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
1524 * \param bits Key size in bits.
1525 * \param parameters Extra parameters for key generation. The interpretation
1526 * of this parameter depends on \c type. All types support
1527 * \c NULL to use default parameters specified below.
1528 *
1529 * For any symmetric key type (type such that
1530 * `PSA_KEY_TYPE_IS_ASYMMETRIC(type)` is false), \c parameters must be
1531 * \c NULL. For asymmetric key types defined by this specification,
1532 * the parameter type and the default parameters are defined by the
1533 * table below. For vendor-defined key types, the vendor documentation
1534 * shall define the parameter type and the default parameters.
1535 *
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001536 * Type | Parameter type | Meaning | Parameters used if `parameters == NULL`
1537 * ---- | -------------- | ------- | ---------------------------------------
1538 * `PSA_KEY_TYPE_RSA_KEYPAIR` | `unsigned int` | Public exponent | 65537
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001539 *
1540 * \retval PSA_SUCCESS
1541 * \retval PSA_ERROR_NOT_SUPPORTED
1542 * \retval PSA_ERROR_INVALID_ARGUMENT
1543 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1544 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1545 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1546 * \retval PSA_ERROR_HARDWARE_FAILURE
1547 * \retval PSA_ERROR_TAMPERING_DETECTED
1548 */
1549psa_status_t psa_generate_key(psa_key_slot_t key,
1550 psa_key_type_t type,
1551 size_t bits,
1552 const void *parameters);
1553
1554/**@}*/
1555
Gilles Peskinee59236f2018-01-27 23:32:46 +01001556#ifdef __cplusplus
1557}
1558#endif
1559
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001560/* The file "crypto_struct.h" contains definitions for
1561 * implementation-specific structs that are declared above. */
1562#include "crypto_struct.h"
1563
1564/* The file "crypto_extra.h" contains vendor-specific definitions. This
1565 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01001566#include "crypto_extra.h"
1567
1568#endif /* PSA_CRYPTO_H */