blob: 998b092b9a74bd6503cad6c6db73ad3f5bcdd284 [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 *
Gilles Peskine36a74b72018-06-01 16:30:32 +0200719 * \param operation The operation object to use.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100720 * \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 *
Gilles Peskine36a74b72018-06-01 16:30:32 +0200912 * \param operation The operation object to use.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100913 * \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 *
Gilles Peskine36a74b72018-06-01 16:30:32 +0200988 * \param operation The operation object to use.
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100989 * \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 *
Gilles Peskine36a74b72018-06-01 16:30:32 +02001035 * \param operation The operation object to use.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001036 * \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
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001085/** The tag size for an AEAD algorithm, in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001086 *
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001087 * \param alg An AEAD algorithm
1088 * (\c PSA_ALG_XXX value such that
1089 * #PSA_ALG_IS_AEAD(alg) is true).
1090 *
1091 * \return The tag size for the specified algorithm.
1092 * If the AEAD algorithm does not have an identified
1093 * tag that can be distinguished from the rest of
1094 * the ciphertext, return 0.
1095 * If the AEAD algorithm is not recognized, return 0.
1096 * An implementation may return either 0 or a
1097 * correct size for an AEAD algorithm that it
1098 * recognizes, but does not support.
1099 */
1100#define PSA_AEAD_TAG_SIZE(alg) \
1101 ((alg) == PSA_ALG_GCM ? 16 : \
1102 (alg) == PSA_ALG_CCM ? 16 : \
1103 0)
Gilles Peskine3b555712018-03-03 21:27:57 +01001104
Gilles Peskine212e4d82018-06-08 11:36:37 +02001105/** The maximum size of the output of psa_aead_encrypt(), in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001106 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001107 * If the size of the ciphertext buffer is at least this large, it is
1108 * guaranteed that psa_aead_encrypt() will not fail due to an
1109 * insufficient buffer size. Depending on the algorithm, the actual size of
1110 * the ciphertext may be smaller.
Gilles Peskine3b555712018-03-03 21:27:57 +01001111 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001112 * \param alg An AEAD algorithm
mohammad16031347a732018-06-07 01:38:45 +03001113 * (\c PSA_ALG_XXX value such that
1114 * #PSA_ALG_IS_AEAD(alg) is true).
Gilles Peskine212e4d82018-06-08 11:36:37 +02001115 * \param plaintext_length Size of the plaintext in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001116 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001117 * \return The AEAD ciphertext size for the specified
1118 * algorithm.
1119 * If the AEAD algorithm is not recognized, return 0.
1120 * An implementation may return either 0 or a
1121 * correct size for an AEAD algorithm that it
1122 * recognizes, but does not support.
mohammad16031347a732018-06-07 01:38:45 +03001123 */
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001124#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) \
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001125 (PSA_AEAD_TAG_SIZE(alg) != 0 ? \
1126 (plaintext_length) + PSA_AEAD_TAG_SIZE(alg) : \
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001127 0)
1128
1129/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001130 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001131 * \param key Slot containing the key to use.
1132 * \param alg The AEAD algorithm to compute
1133 * (\c PSA_ALG_XXX value such that
1134 * #PSA_ALG_IS_AEAD(alg) is true).
1135 * \param nonce Nonce or IV to use.
1136 * \param nonce_length Size of the \p nonce buffer in bytes.
1137 * \param additional_data Additional data that will be authenticated
1138 * but not encrypted.
1139 * \param additional_data_length Size of \p additional_data in bytes.
1140 * \param plaintext Data that will be authenticated and
1141 * encrypted.
1142 * \param plaintext_length Size of \p plaintext in bytes.
1143 * \param ciphertext Output buffer for the authenticated and
1144 * encrypted data. The additional data is not
1145 * part of this output. For algorithms where the
1146 * encrypted data and the authentication tag
1147 * are defined as separate outputs, the
1148 * authentication tag is appended to the
1149 * encrypted data.
1150 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1151 * This must be at least
1152 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1153 * \p plaintext_length).
1154 * \param ciphertext_length On success, the size of the output
1155 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001156 *
1157 * \retval PSA_SUCCESS
1158 * Success.
1159 * \retval PSA_ERROR_EMPTY_SLOT
1160 * \retval PSA_ERROR_NOT_PERMITTED
1161 * \retval PSA_ERROR_INVALID_ARGUMENT
1162 * \c key is not compatible with \c alg.
1163 * \retval PSA_ERROR_NOT_SUPPORTED
1164 * \c alg is not supported or is not an AEAD algorithm.
1165 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1166 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1167 * \retval PSA_ERROR_HARDWARE_FAILURE
1168 * \retval PSA_ERROR_TAMPERING_DETECTED
1169 */
mohammad160339ee8712018-04-26 00:51:02 +03001170psa_status_t psa_aead_encrypt( psa_key_slot_t key,
1171 psa_algorithm_t alg,
1172 const uint8_t *nonce,
1173 size_t nonce_length,
1174 const uint8_t *additional_data,
1175 size_t additional_data_length,
1176 const uint8_t *plaintext,
1177 size_t plaintext_length,
1178 uint8_t *ciphertext,
1179 size_t ciphertext_size,
1180 size_t *ciphertext_length );
Gilles Peskine3b555712018-03-03 21:27:57 +01001181
Gilles Peskine212e4d82018-06-08 11:36:37 +02001182/** The maximum size of the output of psa_aead_decrypt(), in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001183 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001184 * If the size of the plaintext buffer is at least this large, it is
1185 * guaranteed that psa_aead_decrypt() will not fail due to an
1186 * insufficient buffer size. Depending on the algorithm, the actual size of
1187 * the plaintext may be smaller.
Gilles Peskine3b555712018-03-03 21:27:57 +01001188 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001189 * \param alg An AEAD algorithm
mohammad16031347a732018-06-07 01:38:45 +03001190 * (\c PSA_ALG_XXX value such that
1191 * #PSA_ALG_IS_AEAD(alg) is true).
Gilles Peskine212e4d82018-06-08 11:36:37 +02001192 * \param ciphertext_length Size of the plaintext in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001193 *
Gilles Peskine212e4d82018-06-08 11:36:37 +02001194 * \return The AEAD ciphertext size for the specified
1195 * algorithm.
1196 * If the AEAD algorithm is not recognized, return 0.
1197 * An implementation may return either 0 or a
1198 * correct size for an AEAD algorithm that it
1199 * recognizes, but does not support.
mohammad16031347a732018-06-07 01:38:45 +03001200 */
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001201#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \
1202 (PSA_AEAD_TAG_SIZE(alg) != 0 ? \
1203 (plaintext_length) - PSA_AEAD_TAG_SIZE(alg) : \
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001204 0)
1205
1206/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001207 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001208 * \param key Slot containing the key to use.
1209 * \param alg The AEAD algorithm to compute
1210 * (\c PSA_ALG_XXX value such that
1211 * #PSA_ALG_IS_AEAD(alg) is true).
1212 * \param nonce Nonce or IV to use.
1213 * \param nonce_length Size of the \p nonce buffer in bytes.
1214 * \param additional_data Additional data that has been authenticated
1215 * but not encrypted.
1216 * \param additional_data_length Size of \p additional_data in bytes.
1217 * \param ciphertext Data that has been authenticated and
1218 * encrypted. For algorithms where the
1219 * encrypted data and the authentication tag
1220 * are defined as separate inputs, the buffer
1221 * must contain the encrypted data followed
1222 * by the authentication tag.
1223 * \param ciphertext_length Size of \p ciphertext in bytes.
1224 * \param plaintext Output buffer for the decrypted data.
1225 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1226 * This must be at least
1227 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1228 * \p ciphertext_length).
1229 * \param plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001230 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001231 *
1232 * \retval PSA_SUCCESS
1233 * Success.
1234 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001235 * \retval PSA_ERROR_INVALID_SIGNATURE
1236 * The ciphertext is not authentic.
Gilles Peskine3b555712018-03-03 21:27:57 +01001237 * \retval PSA_ERROR_NOT_PERMITTED
1238 * \retval PSA_ERROR_INVALID_ARGUMENT
1239 * \c key is not compatible with \c alg.
1240 * \retval PSA_ERROR_NOT_SUPPORTED
Gilles Peskine19067982018-03-20 17:54:53 +01001241 * \c alg is not supported or is not an AEAD algorithm.
Gilles Peskine3b555712018-03-03 21:27:57 +01001242 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1243 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1244 * \retval PSA_ERROR_HARDWARE_FAILURE
1245 * \retval PSA_ERROR_TAMPERING_DETECTED
1246 */
mohammad160339ee8712018-04-26 00:51:02 +03001247psa_status_t psa_aead_decrypt( psa_key_slot_t key,
1248 psa_algorithm_t alg,
1249 const uint8_t *nonce,
1250 size_t nonce_length,
1251 const uint8_t *additional_data,
1252 size_t additional_data_length,
1253 const uint8_t *ciphertext,
1254 size_t ciphertext_length,
1255 uint8_t *plaintext,
1256 size_t plaintext_size,
1257 size_t *plaintext_length );
Gilles Peskine3b555712018-03-03 21:27:57 +01001258
1259/**@}*/
1260
Gilles Peskine20035e32018-02-03 22:44:14 +01001261/** \defgroup asymmetric Asymmetric cryptography
1262 * @{
1263 */
1264
1265/**
Gilles Peskine0189e752018-02-03 23:57:22 +01001266 * \brief Maximum ECDSA signature size for a given curve bit size
1267 *
1268 * \param curve_bits Curve size in bits
1269 * \return Maximum signature size in bytes
1270 *
1271 * \note This macro returns a compile-time constant if its argument is one.
1272 *
1273 * \warning This macro may evaluate its argument multiple times.
1274 */
1275/*
1276 * RFC 4492 page 20:
1277 *
1278 * Ecdsa-Sig-Value ::= SEQUENCE {
1279 * r INTEGER,
1280 * s INTEGER
1281 * }
1282 *
1283 * Size is at most
1284 * 1 (tag) + 1 (len) + 1 (initial 0) + curve_bytes for each of r and s,
1285 * twice that + 1 (tag) + 2 (len) for the sequence
1286 * (assuming curve_bytes is less than 126 for r and s,
1287 * and less than 124 (total len <= 255) for the sequence)
1288 */
1289#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
1290 ( /*T,L of SEQUENCE*/ ((curve_bits) >= 61 * 8 ? 3 : 2) + \
1291 /*T,L of r,s*/ 2 * (((curve_bits) >= 127 * 8 ? 3 : 2) + \
1292 /*V of r,s*/ ((curve_bits) + 8) / 8))
1293
1294
Gilles Peskine308b91d2018-02-08 09:47:44 +01001295/** Safe signature buffer size for psa_asymmetric_sign().
1296 *
1297 * This macro returns a safe buffer size for a signature using a key
1298 * of the specified type and size, with the specified algorithm.
1299 * Note that the actual size of the signature may be smaller
1300 * (some algorithms produce a variable-size signature).
1301 *
1302 * \warning This function may call its arguments multiple times or
1303 * zero times, so you should not pass arguments that contain
1304 * side effects.
1305 *
1306 * \param key_type An asymmetric key type (this may indifferently be a
1307 * key pair type or a public key type).
1308 * \param key_bits The size of the key in bits.
1309 * \param alg The signature algorithm.
1310 *
1311 * \return If the parameters are valid and supported, return
1312 * a buffer size in bytes that guarantees that
1313 * psa_asymmetric_sign() will not fail with
1314 * #PSA_ERROR_BUFFER_TOO_SMALL.
1315 * If the parameters are a valid combination that is not supported
1316 * by the implementation, this macro either shall return either a
1317 * sensible size or 0.
1318 * If the parameters are not valid, the
1319 * return value is unspecified.
1320 *
1321 */
Gilles Peskine0189e752018-02-03 23:57:22 +01001322#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine2905a7a2018-03-07 16:39:31 +01001323 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine0189e752018-02-03 23:57:22 +01001324 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
Gilles Peskine84845652018-03-28 14:17:40 +02001325 ((void)alg, 0))
Gilles Peskine0189e752018-02-03 23:57:22 +01001326
1327/**
Gilles Peskine20035e32018-02-03 22:44:14 +01001328 * \brief Sign a hash or short message with a private key.
1329 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001330 * \param key Key slot containing an asymmetric key pair.
1331 * \param alg A signature algorithm that is compatible with
1332 * the type of \c key.
1333 * \param hash The message to sign.
1334 * \param hash_length Size of the \c hash buffer in bytes.
1335 * \param salt A salt or label, if supported by the signature
1336 * algorithm.
1337 * If the signature algorithm does not support a
1338 * salt, pass \c NULL.
1339 * If the signature algorithm supports an optional
1340 * salt and you do not want to pass a salt,
1341 * pass \c NULL.
1342 * \param salt_length Size of the \c salt buffer in bytes.
1343 * If \c salt is \c NULL, pass 0.
1344 * \param signature Buffer where the signature is to be written.
1345 * \param signature_size Size of the \c signature buffer in bytes.
1346 * \param signature_length On success, the number of bytes
1347 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001348 *
1349 * \retval PSA_SUCCESS
1350 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1351 * The size of the \c signature buffer is too small. You can
1352 * determine a sufficient buffer size by calling
1353 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg)
1354 * where \c key_type and \c key_bits are the type and bit-size
1355 * respectively of \c key.
1356 * \retval PSA_ERROR_NOT_SUPPORTED
1357 * \retval PSA_ERROR_INVALID_ARGUMENT
1358 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1359 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1360 * \retval PSA_ERROR_HARDWARE_FAILURE
1361 * \retval PSA_ERROR_TAMPERING_DETECTED
1362 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01001363 */
1364psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
1365 psa_algorithm_t alg,
1366 const uint8_t *hash,
1367 size_t hash_length,
1368 const uint8_t *salt,
1369 size_t salt_length,
1370 uint8_t *signature,
1371 size_t signature_size,
1372 size_t *signature_length);
1373
1374/**
1375 * \brief Verify the signature a hash or short message using a public key.
1376 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001377 * \param key Key slot containing a public key or an
1378 * asymmetric key pair.
1379 * \param alg A signature algorithm that is compatible with
1380 * the type of \c key.
1381 * \param hash The message whose signature is to be verified.
1382 * \param hash_length Size of the \c hash buffer in bytes.
1383 * \param salt A salt or label, if supported by the signature
1384 * algorithm.
1385 * If the signature algorithm does not support a
1386 * salt, pass \c NULL.
1387 * If the signature algorithm supports an optional
1388 * salt and you do not want to pass a salt,
1389 * pass \c NULL.
1390 * \param salt_length Size of the \c salt buffer in bytes.
1391 * If \c salt is \c NULL, pass 0.
1392 * \param signature Buffer containing the signature to verify.
1393 * \param signature_size Size of the \c signature buffer in bytes.
1394 *
1395 * \retval PSA_SUCCESS
1396 * The signature is valid.
1397 * \retval PSA_ERROR_INVALID_SIGNATURE
1398 * The calculation was perfomed successfully, but the passed
1399 * signature is not a valid signature.
1400 * \retval PSA_ERROR_NOT_SUPPORTED
1401 * \retval PSA_ERROR_INVALID_ARGUMENT
1402 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1403 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1404 * \retval PSA_ERROR_HARDWARE_FAILURE
1405 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01001406 */
1407psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
1408 psa_algorithm_t alg,
1409 const uint8_t *hash,
1410 size_t hash_length,
1411 const uint8_t *salt,
1412 size_t salt_length,
1413 uint8_t *signature,
1414 size_t signature_size);
1415
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001416#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine06297932018-04-11 16:58:22 +02001417 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
1418 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
1419 0)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001420#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine06297932018-04-11 16:58:22 +02001421 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
1422 PSA_BITS_TO_BYTES(key_bits) - ((alg) == PSA_ALG_IS_RSA_OAEP_MGF1 ? \
1423 2 * (PSA_ALG_RSA_GET_HASH(alg) + 1) : \
1424 11 /*PKCS#1v1.5*/) : \
1425 0)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001426
1427/**
1428 * \brief Encrypt a short message with a public key.
1429 *
1430 * \param key Key slot containing a public key or an asymmetric
1431 * key pair.
1432 * \param alg An asymmetric encryption algorithm that is
1433 * compatible with the type of \c key.
1434 * \param input The message to encrypt.
1435 * \param input_length Size of the \c input buffer in bytes.
1436 * \param salt A salt or label, if supported by the encryption
1437 * algorithm.
1438 * If the algorithm does not support a
1439 * salt, pass \c NULL.
1440 * If the algorithm supports an optional
1441 * salt and you do not want to pass a salt,
1442 * pass \c NULL.
1443 *
1444 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1445 * supported.
1446 * \param salt_length Size of the \c salt buffer in bytes.
1447 * If \c salt is \c NULL, pass 0.
1448 * \param output Buffer where the encrypted message is to be written.
1449 * \param output_size Size of the \c output buffer in bytes.
1450 * \param output_length On success, the number of bytes
1451 * that make up the returned output.
1452 *
1453 * \retval PSA_SUCCESS
1454 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1455 * The size of the \c output buffer is too small. You can
1456 * determine a sufficient buffer size by calling
1457 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1458 * where \c key_type and \c key_bits are the type and bit-size
1459 * respectively of \c key.
1460 * \retval PSA_ERROR_NOT_SUPPORTED
1461 * \retval PSA_ERROR_INVALID_ARGUMENT
1462 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1463 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1464 * \retval PSA_ERROR_HARDWARE_FAILURE
1465 * \retval PSA_ERROR_TAMPERING_DETECTED
1466 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1467 */
1468psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
1469 psa_algorithm_t alg,
1470 const uint8_t *input,
1471 size_t input_length,
1472 const uint8_t *salt,
1473 size_t salt_length,
1474 uint8_t *output,
1475 size_t output_size,
1476 size_t *output_length);
1477
1478/**
1479 * \brief Decrypt a short message with a private key.
1480 *
1481 * \param key Key slot containing an asymmetric key pair.
1482 * \param alg An asymmetric encryption algorithm that is
1483 * compatible with the type of \c key.
1484 * \param input The message to decrypt.
1485 * \param input_length Size of the \c input buffer in bytes.
1486 * \param salt A salt or label, if supported by the encryption
1487 * algorithm.
1488 * If the algorithm does not support a
1489 * salt, pass \c NULL.
1490 * If the algorithm supports an optional
1491 * salt and you do not want to pass a salt,
1492 * pass \c NULL.
1493 *
1494 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1495 * supported.
1496 * \param salt_length Size of the \c salt buffer in bytes.
1497 * If \c salt is \c NULL, pass 0.
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001498 * \param output Buffer where the decrypted message is to be written.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001499 * \param output_size Size of the \c output buffer in bytes.
1500 * \param output_length On success, the number of bytes
1501 * that make up the returned output.
1502 *
1503 * \retval PSA_SUCCESS
1504 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1505 * The size of the \c output buffer is too small. You can
1506 * determine a sufficient buffer size by calling
1507 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1508 * where \c key_type and \c key_bits are the type and bit-size
1509 * respectively of \c key.
1510 * \retval PSA_ERROR_NOT_SUPPORTED
1511 * \retval PSA_ERROR_INVALID_ARGUMENT
1512 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1513 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1514 * \retval PSA_ERROR_HARDWARE_FAILURE
1515 * \retval PSA_ERROR_TAMPERING_DETECTED
1516 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1517 * \retval PSA_ERROR_INVALID_PADDING
1518 */
1519psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
1520 psa_algorithm_t alg,
1521 const uint8_t *input,
1522 size_t input_length,
1523 const uint8_t *salt,
1524 size_t salt_length,
1525 uint8_t *output,
1526 size_t output_size,
1527 size_t *output_length);
1528
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001529/**@}*/
1530
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001531/** \defgroup generation Key generation
1532 * @{
1533 */
1534
1535/**
1536 * \brief Generate random bytes.
1537 *
1538 * \warning This function **can** fail! Callers MUST check the return status
1539 * and MUST NOT use the content of the output buffer if the return
1540 * status is not #PSA_SUCCESS.
1541 *
1542 * \note To generate a key, use psa_generate_key() instead.
1543 *
1544 * \param output Output buffer for the generated data.
1545 * \param output_size Number of bytes to generate and output.
1546 *
1547 * \retval PSA_SUCCESS
1548 * \retval PSA_ERROR_NOT_SUPPORTED
1549 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1550 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1551 * \retval PSA_ERROR_HARDWARE_FAILURE
1552 * \retval PSA_ERROR_TAMPERING_DETECTED
1553 */
1554psa_status_t psa_generate_random(uint8_t *output,
1555 size_t output_size);
1556
1557/**
1558 * \brief Generate a key or key pair.
1559 *
1560 * \param key Slot where the key will be stored. This must be a
1561 * valid slot for a key of the chosen type. It must
1562 * be unoccupied.
1563 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
1564 * \param bits Key size in bits.
1565 * \param parameters Extra parameters for key generation. The interpretation
1566 * of this parameter depends on \c type. All types support
1567 * \c NULL to use default parameters specified below.
1568 *
1569 * For any symmetric key type (type such that
1570 * `PSA_KEY_TYPE_IS_ASYMMETRIC(type)` is false), \c parameters must be
1571 * \c NULL. For asymmetric key types defined by this specification,
1572 * the parameter type and the default parameters are defined by the
1573 * table below. For vendor-defined key types, the vendor documentation
1574 * shall define the parameter type and the default parameters.
1575 *
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001576 * Type | Parameter type | Meaning | Parameters used if `parameters == NULL`
1577 * ---- | -------------- | ------- | ---------------------------------------
1578 * `PSA_KEY_TYPE_RSA_KEYPAIR` | `unsigned int` | Public exponent | 65537
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001579 *
1580 * \retval PSA_SUCCESS
1581 * \retval PSA_ERROR_NOT_SUPPORTED
1582 * \retval PSA_ERROR_INVALID_ARGUMENT
1583 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1584 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1585 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1586 * \retval PSA_ERROR_HARDWARE_FAILURE
1587 * \retval PSA_ERROR_TAMPERING_DETECTED
1588 */
1589psa_status_t psa_generate_key(psa_key_slot_t key,
1590 psa_key_type_t type,
1591 size_t bits,
1592 const void *parameters);
1593
1594/**@}*/
1595
Gilles Peskinee59236f2018-01-27 23:32:46 +01001596#ifdef __cplusplus
1597}
1598#endif
1599
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001600/* The file "crypto_struct.h" contains definitions for
1601 * implementation-specific structs that are declared above. */
1602#include "crypto_struct.h"
1603
1604/* The file "crypto_extra.h" contains vendor-specific definitions. This
1605 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01001606#include "crypto_extra.h"
1607
1608#endif /* PSA_CRYPTO_H */