blob: 31079525bc333468041a02e038947dd16cf33989 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
5
6#ifndef PSA_CRYPTO_H
7#define PSA_CRYPTO_H
8
9#include "crypto_platform.h"
10
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010011#include <stddef.h>
12
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010013#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010014/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
15 * must be defined in the crypto_platform.h header. These mock definitions
16 * are present in this file as a convenience to generate pretty-printed
17 * documentation that includes those definitions. */
18
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010019/** \defgroup platform Implementation-specific definitions
20 * @{
21 */
22
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010023/** \brief Key slot number.
24 *
25 * This type represents key slots. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010026 * type. The choice of type is implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027 * 0 is not a valid key slot number. The meaning of other values is
28 * implementation dependent.
29 *
30 * At any given point in time, each key slot either contains a
31 * cryptographic object, or is empty. Key slots are persistent:
32 * once set, the cryptographic object remains in the key slot until
33 * explicitly destroyed.
34 */
35typedef _unsigned_integral_type_ psa_key_slot_t;
36
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010037/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010038#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010039
Gilles Peskinee59236f2018-01-27 23:32:46 +010040#ifdef __cplusplus
41extern "C" {
42#endif
43
44/** \defgroup basic Basic definitions
45 * @{
46 */
47
48/**
49 * \brief Function return status.
50 *
51 * Zero indicates success, anything else indicates an error.
52 */
53typedef enum {
54 /** The action was completed successfully. */
55 PSA_SUCCESS = 0,
56 /** The requested operation or a parameter is not supported
57 by this implementation. */
58 PSA_ERROR_NOT_SUPPORTED,
59 /** The requested action is denied by a policy. */
60 PSA_ERROR_NOT_PERMITTED,
61 /** An output buffer is too small. */
62 PSA_ERROR_BUFFER_TOO_SMALL,
63 /** A slot is occupied, but must be empty to carry out the
64 requested action. */
65 PSA_ERROR_OCCUPIED_SLOT,
66 /** A slot is empty, but must be occupied to carry out the
67 requested action. */
68 PSA_ERROR_EMPTY_SLOT,
69 /** The requested action cannot be performed in the current state. */
70 PSA_ERROR_BAD_STATE,
71 /** The parameters passed to the function are invalid. */
72 PSA_ERROR_INVALID_ARGUMENT,
73 /** There is not enough runtime memory. */
74 PSA_ERROR_INSUFFICIENT_MEMORY,
75 /** There is not enough persistent storage. */
76 PSA_ERROR_INSUFFICIENT_STORAGE,
77 /** There was a communication failure inside the implementation. */
78 PSA_ERROR_COMMUNICATION_FAILURE,
Gilles Peskinea5905292018-02-07 20:59:33 +010079 /** There was a storage failure that may have led to data loss. */
80 PSA_ERROR_STORAGE_FAILURE,
Gilles Peskinee59236f2018-01-27 23:32:46 +010081 /** A hardware failure was detected. */
82 PSA_ERROR_HARDWARE_FAILURE,
83 /** A tampering attempt was detected. */
84 PSA_ERROR_TAMPERING_DETECTED,
85 /** There is not enough entropy to generate random data needed
86 for the requested action. */
87 PSA_ERROR_INSUFFICIENT_ENTROPY,
Gilles Peskinea5905292018-02-07 20:59:33 +010088 /** The signature, MAC or hash is incorrect. */
Gilles Peskinee59236f2018-01-27 23:32:46 +010089 PSA_ERROR_INVALID_SIGNATURE,
Gilles Peskinea5905292018-02-07 20:59:33 +010090 /** The decrypted padding is incorrect. */
91 PSA_ERROR_INVALID_PADDING,
Gilles Peskinee59236f2018-01-27 23:32:46 +010092 /** An error occurred that does not correspond to any defined
93 failure cause. */
94 PSA_ERROR_UNKNOWN_ERROR,
95} psa_status_t;
96
97/**
98 * \brief Library initialization.
99 *
100 * Applications must call this function before calling any other
101 * function in this module.
102 *
103 * Applications may call this function more than once. Once a call
104 * succeeds, subsequent calls are guaranteed to succeed.
105 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100106 * \retval PSA_SUCCESS
107 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
108 * \retval PSA_ERROR_COMMUNICATION_FAILURE
109 * \retval PSA_ERROR_HARDWARE_FAILURE
110 * \retval PSA_ERROR_TAMPERING_DETECTED
111 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +0100112 */
113psa_status_t psa_crypto_init(void);
114
Gilles Peskine2905a7a2018-03-07 16:39:31 +0100115#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
116#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
Gilles Peskine0189e752018-02-03 23:57:22 +0100117
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118/**@}*/
119
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100120/** \defgroup crypto_types Key and algorithm types
121 * @{
122 */
123
Gilles Peskine308b91d2018-02-08 09:47:44 +0100124/** \brief Encoding of a key type.
125 */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100126typedef uint32_t psa_key_type_t;
127
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100128/** An invalid key type value.
129 *
130 * Zero is not the encoding of any key type.
131 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100132#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100133
134/** Vendor-defined flag
135 *
136 * Key types defined by this standard will never have the
137 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
138 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
139 * respect the bitwise structure used by standard encodings whenever practical.
140 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100141#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100142
Gilles Peskine98f0a242018-02-06 18:57:29 +0100143#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7e000000)
144#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x02000000)
145#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x04000000)
146#define PSA_KEY_TYPE_CATEGORY_ASYMMETRIC ((psa_key_type_t)0x06000000)
147#define PSA_KEY_TYPE_PAIR_FLAG ((psa_key_type_t)0x01000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100148
Gilles Peskine98f0a242018-02-06 18:57:29 +0100149#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x02000001)
150#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x04000001)
151#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x04000002)
152#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x04000003)
153#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x04000004)
154
Gilles Peskine308b91d2018-02-08 09:47:44 +0100155/** RSA public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100156#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x06010000)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100157/** RSA key pair (private and public key). */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100158#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x07010000)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100159/** DSA public key. */
160#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x06020000)
161/** DSA key pair (private and public key). */
162#define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x07020000)
163#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x06030000)
164#define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x07030000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100165#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100166#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
167 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
168#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
169 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
Gilles Peskine98f0a242018-02-06 18:57:29 +0100170
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100171/** Whether a key type is vendor-defined. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100172#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100173 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100174#define PSA_KEY_TYPE_IS_RAW_BYTES(type) \
175 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_RAW_DATA || \
176 ((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100177
178/** Whether a key type is asymmetric: either a key pair or a public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100179#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
180 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100181/** Whether a key type is the public part of a key pair. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100182#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
183 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG) == \
184 PSA_KEY_TYPE_CATEGORY_ASYMMETRIC))
Gilles Peskine06dc2632018-03-08 07:47:25 +0100185/** Whether a key type is a key pair containing a private part and a public
186 * part. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100187#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
188 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
189 (PSA_KEY_TYPE_CATEGORY_ASYMMETRIC | PSA_KEY_TYPE_PAIR_FLAG))
Gilles Peskine06dc2632018-03-08 07:47:25 +0100190/** Whether a key type is an RSA key pair or public key. */
191/** The key pair type corresponding to a public key type. */
192#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
193 ((type) | PSA_KEY_TYPE_PAIR_FLAG)
194/** The public key type corresponding to a key pair type. */
195#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
196 ((type) & ~PSA_KEY_TYPE_PAIR_FLAG)
Gilles Peskine0189e752018-02-03 23:57:22 +0100197#define PSA_KEY_TYPE_IS_RSA(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100198 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
199/** Whether a key type is an elliptic curve key pair or public key. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100200#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100201 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
202 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100203
Gilles Peskine7e198532018-03-08 07:50:30 +0100204/** The block size of a block cipher.
205 *
206 * \param type A cipher key type (value of type #psa_key_type_t).
207 *
208 * \return The block size for a block cipher, or 1 for a stream cipher.
209 * The return value is undefined if \c type does not identify
210 * a cipher algorithm.
211 *
212 * \note This macro returns a compile-time constant if its argument is one.
213 *
214 * \warning This macro may evaluate its argument multiple times.
215 */
Gilles Peskine03182e92018-03-07 16:40:52 +0100216#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100217 ( \
218 (type) == PSA_KEY_TYPE_AES ? 16 : \
219 (type) == PSA_KEY_TYPE_DES ? 8 : \
220 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
Gilles Peskine7e198532018-03-08 07:50:30 +0100221 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100222 0)
223
Gilles Peskine308b91d2018-02-08 09:47:44 +0100224/** \brief Encoding of a cryptographic algorithm.
225 *
226 * For algorithms that can be applied to multiple key types, this type
227 * does not encode the key type. For example, for symmetric ciphers
228 * based on a block cipher, #psa_algorithm_t encodes the block cipher
229 * mode and the padding mode while the block cipher itself is encoded
230 * via #psa_key_type_t.
231 */
Gilles Peskine20035e32018-02-03 22:44:14 +0100232typedef uint32_t psa_algorithm_t;
233
Gilles Peskine98f0a242018-02-06 18:57:29 +0100234#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
235#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
236#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
237#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
238#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
239#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
240#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
241#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
242#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
243#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100244
Gilles Peskine98f0a242018-02-06 18:57:29 +0100245#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
246 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100247/** Whether the specified algorithm is a hash algorithm.
248 *
Gilles Peskine7e198532018-03-08 07:50:30 +0100249 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100250 *
251 * \return 1 if \c alg is a hash algorithm, 0 otherwise.
252 * This macro may return either 0 or 1 if \c alg is not a valid
Gilles Peskine7e198532018-03-08 07:50:30 +0100253 * algorithm identifier.
254 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100255#define PSA_ALG_IS_HASH(alg) \
256 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
257#define PSA_ALG_IS_MAC(alg) \
258 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
259#define PSA_ALG_IS_CIPHER(alg) \
260 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
261#define PSA_ALG_IS_AEAD(alg) \
262 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
263#define PSA_ALG_IS_SIGN(alg) \
264 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
265#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
266 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
267#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
268 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
269#define PSA_ALG_IS_KEY_DERIVATION(alg) \
270 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
271
272#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
273#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
274#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
275#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
Gilles Peskinee3f694f2018-03-08 07:48:40 +0100276#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
277#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100278#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
279#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
280#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
281#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
282#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
283#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
284#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
285#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
286#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
287#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
288
Gilles Peskine8c9def32018-02-08 10:02:12 +0100289#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100290#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
291#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100292 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
293#define PSA_ALG_HMAC_HASH(hmac_alg) \
294 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
295#define PSA_ALG_IS_HMAC(alg) \
296 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
297 PSA_ALG_HMAC_BASE)
298#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
299#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
300#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
301#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
302#define PSA_ALG_IS_CIPHER_MAC(alg) \
303 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
304 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100305
Gilles Peskine8c9def32018-02-08 10:02:12 +0100306#define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100307#define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100308#define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100309#define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000)
310#define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100311#define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100312#define PSA_ALG_IS_BLOCK_CIPHER(alg) \
313 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
314 PSA_ALG_BLOCK_CIPHER_BASE)
315
Gilles Peskine98f0a242018-02-06 18:57:29 +0100316#define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100317#define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002)
318#define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003)
319#define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100320#define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t)0x04800000)
321#define PSA_ALG_CTR ((psa_algorithm_t)0x04800001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100322#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100323
Moran Pekerbed71a22018-04-22 20:19:20 +0300324#define PSA_ALG_IS_STREAM_CIPHER(alg) \
325 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
326 PSA_ALG_STREAM_CIPHER)
327
Gilles Peskine8c9def32018-02-08 10:02:12 +0100328#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
329#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100330
Gilles Peskinea5926232018-03-28 14:16:50 +0200331#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW ((psa_algorithm_t)0x10010000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100332#define PSA_ALG_RSA_PSS_MGF1 ((psa_algorithm_t)0x10020000)
Gilles Peskine6944f9a2018-03-28 14:18:39 +0200333#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12010000)
334#define PSA_ALG_RSA_OAEP_MGF1_BASE ((psa_algorithm_t)0x12020000)
Gilles Peskinea5926232018-03-28 14:16:50 +0200335#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
336 (PSA_ALG_RSA_PKCS1V15_SIGN_RAW | ((hash_alg) & PSA_ALG_HASH_MASK))
337#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Gilles Peskine9673cc82018-04-11 16:57:49 +0200338 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_RAW)
339#define PSA_ALG_RSA_OAEP_MGF1(hash_alg) \
340 (PSA_ALG_RSA_OAEP_MGF1_RAW | ((hash_alg) & PSA_ALG_HASH_MASK))
341#define PSA_ALG_IS_RSA_OAEP_MGF1(alg) \
342 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_MGF1_RAW)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100343#define PSA_ALG_RSA_GET_HASH(alg) \
344 (((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100345
346/**@}*/
347
348/** \defgroup key_management Key management
349 * @{
350 */
351
352/**
353 * \brief Import a key in binary format.
354 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100355 * This function supports any output from psa_export_key(). Refer to the
356 * documentation of psa_export_key() for the format for each key type.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100357 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100358 * \param key Slot where the key will be stored. This must be a
359 * valid slot for a key of the chosen type. It must
360 * be unoccupied.
361 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
362 * \param data Buffer containing the key data.
363 * \param data_length Size of the \c data buffer in bytes.
364 *
365 * \retval PSA_SUCCESS
366 * Success.
367 * \retval PSA_ERROR_NOT_SUPPORTED
368 * The key type or key size is not supported.
369 * \retval PSA_ERROR_INVALID_ARGUMENT
370 * The key slot is invalid,
371 * or the key data is not correctly formatted.
372 * \retval PSA_ERROR_OCCUPIED_SLOT
373 There is already a key in the specified slot.
374 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
375 * \retval PSA_ERROR_COMMUNICATION_FAILURE
376 * \retval PSA_ERROR_HARDWARE_FAILURE
377 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100378 */
379psa_status_t psa_import_key(psa_key_slot_t key,
380 psa_key_type_t type,
381 const uint8_t *data,
382 size_t data_length);
383
384/**
385 * \brief Destroy a key.
386 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100387 * \retval PSA_SUCCESS
388 * \retval PSA_ERROR_EMPTY_SLOT
389 * \retval PSA_ERROR_COMMUNICATION_FAILURE
390 * \retval PSA_ERROR_HARDWARE_FAILURE
391 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100392 */
393psa_status_t psa_destroy_key(psa_key_slot_t key);
394
395/**
396 * \brief Get basic metadata about a key.
397 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100398 * \param key Slot whose content is queried. This must
399 * be an occupied key slot.
400 * \param type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
401 * This may be a null pointer, in which case the key type
402 * is not written.
403 * \param bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100404 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100405 * is not written.
406 *
407 * \retval PSA_SUCCESS
408 * \retval PSA_ERROR_EMPTY_SLOT
409 * \retval PSA_ERROR_COMMUNICATION_FAILURE
410 * \retval PSA_ERROR_HARDWARE_FAILURE
411 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100412 */
413psa_status_t psa_get_key_information(psa_key_slot_t key,
414 psa_key_type_t *type,
415 size_t *bits);
416
417/**
418 * \brief Export a key in binary format.
419 *
420 * The output of this function can be passed to psa_import_key() to
421 * create an equivalent object.
422 *
423 * If a key is created with psa_import_key() and then exported with
424 * this function, it is not guaranteed that the resulting data is
425 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +0100426 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100427 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100428 * For standard key types, the output format is as follows:
429 *
430 * - For symmetric keys (including MAC keys), the format is the
431 * raw bytes of the key.
432 * - For DES, the key data consists of 8 bytes. The parity bits must be
433 * correct.
434 * - For Triple-DES, the format is the concatenation of the
435 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100436 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine308b91d2018-02-08 09:47:44 +0100437 * is the non-encrypted DER representation defined by PKCS\#8 (RFC 5208)
438 * as PrivateKeyInfo.
439 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
Gilles Peskine971f7062018-03-20 17:52:58 +0100440 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100441 *
442 * \param key Slot whose content is to be exported. This must
443 * be an occupied key slot.
444 * \param data Buffer where the key data is to be written.
445 * \param data_size Size of the \c data buffer in bytes.
446 * \param data_length On success, the number of bytes
447 * that make up the key data.
448 *
449 * \retval PSA_SUCCESS
450 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine92b30732018-03-03 21:29:30 +0100451 * \retval PSA_ERROR_NOT_PERMITTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100452 * \retval PSA_ERROR_COMMUNICATION_FAILURE
453 * \retval PSA_ERROR_HARDWARE_FAILURE
454 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100455 */
456psa_status_t psa_export_key(psa_key_slot_t key,
457 uint8_t *data,
458 size_t data_size,
459 size_t *data_length);
460
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100461/**
462 * \brief Export a public key or the public part of a key pair in binary format.
463 *
464 * The output of this function can be passed to psa_import_key() to
465 * create an object that is equivalent to the public key.
466 *
467 * For standard key types, the output format is as follows:
468 *
469 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
Gilles Peskine971f7062018-03-20 17:52:58 +0100470 * is the DER representation of the public key defined by RFC 5280
471 * as SubjectPublicKeyInfo.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100472 *
473 * \param key Slot whose content is to be exported. This must
474 * be an occupied key slot.
475 * \param data Buffer where the key data is to be written.
476 * \param data_size Size of the \c data buffer in bytes.
477 * \param data_length On success, the number of bytes
478 * that make up the key data.
479 *
480 * \retval PSA_SUCCESS
481 * \retval PSA_ERROR_EMPTY_SLOT
482 * \retval PSA_ERROR_INVALID_ARGUMENT
483 * \retval PSA_ERROR_COMMUNICATION_FAILURE
484 * \retval PSA_ERROR_HARDWARE_FAILURE
485 * \retval PSA_ERROR_TAMPERING_DETECTED
486 */
487psa_status_t psa_export_public_key(psa_key_slot_t key,
488 uint8_t *data,
489 size_t data_size,
490 size_t *data_length);
491
492/**@}*/
493
494/** \defgroup policy Key policies
495 * @{
496 */
497
498/** \brief Encoding of permitted usage on a key. */
499typedef uint32_t psa_key_usage_t;
500
Gilles Peskine7e198532018-03-08 07:50:30 +0100501/** Whether the key may be exported.
502 *
503 * A public key or the public part of a key pair may always be exported
504 * regardless of the value of this permission flag.
505 *
506 * If a key does not have export permission, implementations shall not
507 * allow the key to be exported in plain form from the cryptoprocessor,
508 * whether through psa_export_key() or through a proprietary interface.
509 * The key may however be exportable in a wrapped form, i.e. in a form
510 * where it is encrypted by another key.
511 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100512#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
513
Gilles Peskine7e198532018-03-08 07:50:30 +0100514/** Whether the key may be used to encrypt a message.
515 *
516 * For a key pair, this concerns the public key.
517 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100518#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +0100519
520/** Whether the key may be used to decrypt a message.
521 *
522 * For a key pair, this concerns the private key.
523 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100524#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +0100525
526/** Whether the key may be used to sign a message.
527 *
528 * For a key pair, this concerns the private key.
529 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100530#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +0100531
532/** Whether the key may be used to verify a message signature.
533 *
534 * For a key pair, this concerns the public key.
535 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100536#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
537
538/** The type of the key policy data structure.
539 *
540 * This is an implementation-defined \c struct. Applications should not
541 * make any assumptions about the content of this structure except
542 * as directed by the documentation of a specific implementation. */
543typedef struct psa_key_policy_s psa_key_policy_t;
544
545/** \brief Initialize a key policy structure to a default that forbids all
546 * usage of the key. */
547void psa_key_policy_init(psa_key_policy_t *policy);
548
Gilles Peskine7e198532018-03-08 07:50:30 +0100549/** \brief Set the standard fields of a policy structure.
550 *
551 * Note that this function does not make any consistency check of the
552 * parameters. The values are only checked when applying the policy to
553 * a key slot with psa_set_key_policy().
554 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100555void psa_key_policy_set_usage(psa_key_policy_t *policy,
556 psa_key_usage_t usage,
557 psa_algorithm_t alg);
558
559psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy);
560
561psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy);
562
563/** \brief Set the usage policy on a key slot.
564 *
565 * This function must be called on an empty key slot, before importing,
566 * generating or creating a key in the slot. Changing the policy of an
567 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100568 *
569 * Implementations may set restrictions on supported key policies
570 * depending on the key type and the key slot.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100571 */
572psa_status_t psa_set_key_policy(psa_key_slot_t key,
573 const psa_key_policy_t *policy);
574
Gilles Peskine7e198532018-03-08 07:50:30 +0100575/** \brief Get the usage policy for a key slot.
576 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100577psa_status_t psa_get_key_policy(psa_key_slot_t key,
578 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100579
580/**@}*/
581
Gilles Peskine609b6a52018-03-03 21:31:50 +0100582/** \defgroup persistence Key lifetime
583 * @{
584 */
585
586/** Encoding of key lifetimes.
587 */
588typedef uint32_t psa_key_lifetime_t;
589
590/** A volatile key slot retains its content as long as the application is
591 * running. It is guaranteed to be erased on a power reset.
592 */
593#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
594
595/** A persistent key slot retains its content as long as it is not explicitly
596 * destroyed.
597 */
598#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
599
600/** A write-once key slot may not be modified once a key has been set.
601 * It will retain its content as long as the device remains operational.
602 */
603#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
604
Gilles Peskined393e182018-03-08 07:49:16 +0100605/** \brief Retrieve the lifetime of a key slot.
606 *
607 * The assignment of lifetimes to slots is implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200608 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +0200609 * \param key Slot to query.
mohammad1603804cd712018-03-20 22:44:08 +0200610 * \param lifetime On success, the lifetime value.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200611 *
mohammad1603804cd712018-03-20 22:44:08 +0200612 * \retval PSA_SUCCESS
613 * Success.
614 * \retval PSA_ERROR_INVALID_ARGUMENT
mohammad1603a7d245a2018-04-17 00:40:08 -0700615 * The key slot is invalid.
Gilles Peskinef0c9dd32018-04-17 14:11:07 +0200616 * \retval PSA_ERROR_COMMUNICATION_FAILURE
617 * \retval PSA_ERROR_HARDWARE_FAILURE
618 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +0100619 */
Gilles Peskine609b6a52018-03-03 21:31:50 +0100620psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
621 psa_key_lifetime_t *lifetime);
622
Gilles Peskined393e182018-03-08 07:49:16 +0100623/** \brief Change the lifetime of a key slot.
624 *
625 * Whether the lifetime of a key slot can be changed at all, and if so
Gilles Peskine19067982018-03-20 17:54:53 +0100626 * whether the lifetime of an occupied key slot can be changed, is
Gilles Peskined393e182018-03-08 07:49:16 +0100627 * implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200628 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +0200629 * \param key Slot whose lifetime is to be changed.
630 * \param lifetime The lifetime value to set for the given key slot.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200631 *
mohammad1603804cd712018-03-20 22:44:08 +0200632 * \retval PSA_SUCCESS
633 * Success.
634 * \retval PSA_ERROR_INVALID_ARGUMENT
635 * The key slot is invalid,
mohammad1603a7d245a2018-04-17 00:40:08 -0700636 * or the lifetime value is invalid.
Gilles Peskinef0c9dd32018-04-17 14:11:07 +0200637 * \retval PSA_ERROR_NOT_SUPPORTED
638 * The implementation does not support the specified lifetime value,
639 * at least for the specified key slot.
640 * \retval PSA_ERROR_OCCUPIED_SLOT
641 * The slot contains a key, and the implementation does not support
642 * changing the lifetime of an occupied slot.
643 * \retval PSA_ERROR_COMMUNICATION_FAILURE
644 * \retval PSA_ERROR_HARDWARE_FAILURE
645 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +0100646 */
647psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
mohammad1603ea050092018-04-17 00:31:34 -0700648 psa_key_lifetime_t lifetime);
Gilles Peskined393e182018-03-08 07:49:16 +0100649
Gilles Peskine609b6a52018-03-03 21:31:50 +0100650/**@}*/
651
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100652/** \defgroup hash Message digests
653 * @{
654 */
655
Gilles Peskine308b91d2018-02-08 09:47:44 +0100656/** The type of the state data structure for multipart hash operations.
657 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100658 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100659 * make any assumptions about the content of this structure except
660 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100661typedef struct psa_hash_operation_s psa_hash_operation_t;
662
Gilles Peskine308b91d2018-02-08 09:47:44 +0100663/** The size of the output of psa_hash_finish(), in bytes.
664 *
665 * This is also the hash size that psa_hash_verify() expects.
666 *
667 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
668 * #PSA_ALG_IS_HASH(alg) is true).
669 *
670 * \return The hash size for the specified hash algorithm.
671 * If the hash algorithm is not recognized, return 0.
672 * An implementation may return either 0 or the correct size
673 * for a hash algorithm that it recognizes, but does not support.
674 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100675#define PSA_HASH_FINAL_SIZE(alg) \
676 ( \
677 (alg) == PSA_ALG_MD2 ? 16 : \
678 (alg) == PSA_ALG_MD4 ? 16 : \
679 (alg) == PSA_ALG_MD5 ? 16 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100680 (alg) == PSA_ALG_RIPEMD160 ? 20 : \
681 (alg) == PSA_ALG_SHA_1 ? 20 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100682 (alg) == PSA_ALG_SHA_224 ? 28 : \
683 (alg) == PSA_ALG_SHA_256 ? 32 : \
684 (alg) == PSA_ALG_SHA_384 ? 48 : \
685 (alg) == PSA_ALG_SHA_512 ? 64 : \
686 (alg) == PSA_ALG_SHA_512_224 ? 28 : \
687 (alg) == PSA_ALG_SHA_512_256 ? 32 : \
688 (alg) == PSA_ALG_SHA3_224 ? 28 : \
689 (alg) == PSA_ALG_SHA3_256 ? 32 : \
690 (alg) == PSA_ALG_SHA3_384 ? 48 : \
691 (alg) == PSA_ALG_SHA3_512 ? 64 : \
692 0)
693
Gilles Peskine308b91d2018-02-08 09:47:44 +0100694/** Start a multipart hash operation.
695 *
696 * The sequence of operations to calculate a hash (message digest)
697 * is as follows:
698 * -# Allocate an operation object which will be passed to all the functions
699 * listed here.
700 * -# Call psa_hash_start() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100701 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100702 * of the message each time. The hash that is calculated is the hash
703 * of the concatenation of these messages in order.
704 * -# To calculate the hash, call psa_hash_finish().
705 * To compare the hash with an expected value, call psa_hash_verify().
706 *
707 * The application may call psa_hash_abort() at any time after the operation
708 * has been initialized with psa_hash_start().
709 *
710 * After a successful call to psa_hash_start(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100711 * eventually terminate the operation. The following events terminate an
712 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100713 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100714 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100715 *
716 * \param operation
717 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
718 * such that #PSA_ALG_IS_HASH(alg) is true).
719 *
720 * \retval PSA_SUCCESS
721 * Success.
722 * \retval PSA_ERROR_NOT_SUPPORTED
723 * \c alg is not supported or is not a hash algorithm.
724 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
725 * \retval PSA_ERROR_COMMUNICATION_FAILURE
726 * \retval PSA_ERROR_HARDWARE_FAILURE
727 * \retval PSA_ERROR_TAMPERING_DETECTED
728 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100729psa_status_t psa_hash_start(psa_hash_operation_t *operation,
730 psa_algorithm_t alg);
731
Gilles Peskine308b91d2018-02-08 09:47:44 +0100732/** Add a message fragment to a multipart hash operation.
733 *
734 * The application must call psa_hash_start() before calling this function.
735 *
736 * If this function returns an error status, the operation becomes inactive.
737 *
738 * \param operation Active hash operation.
739 * \param input Buffer containing the message fragment to hash.
740 * \param input_length Size of the \c input buffer in bytes.
741 *
742 * \retval PSA_SUCCESS
743 * Success.
744 * \retval PSA_ERROR_BAD_STATE
745 * The operation state is not valid (not started, or already completed).
746 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
747 * \retval PSA_ERROR_COMMUNICATION_FAILURE
748 * \retval PSA_ERROR_HARDWARE_FAILURE
749 * \retval PSA_ERROR_TAMPERING_DETECTED
750 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100751psa_status_t psa_hash_update(psa_hash_operation_t *operation,
752 const uint8_t *input,
753 size_t input_length);
754
Gilles Peskine308b91d2018-02-08 09:47:44 +0100755/** Finish the calculation of the hash of a message.
756 *
757 * The application must call psa_hash_start() before calling this function.
758 * This function calculates the hash of the message formed by concatenating
759 * the inputs passed to preceding calls to psa_hash_update().
760 *
761 * When this function returns, the operation becomes inactive.
762 *
763 * \warning Applications should not call this function if they expect
764 * a specific value for the hash. Call psa_hash_verify() instead.
765 * Beware that comparing integrity or authenticity data such as
766 * hash values with a function such as \c memcmp is risky
767 * because the time taken by the comparison may leak information
768 * about the hashed data which could allow an attacker to guess
769 * a valid hash and thereby bypass security controls.
770 *
771 * \param operation Active hash operation.
772 * \param hash Buffer where the hash is to be written.
773 * \param hash_size Size of the \c hash buffer in bytes.
774 * \param hash_length On success, the number of bytes
775 * that make up the hash value. This is always
776 * #PSA_HASH_FINAL_SIZE(alg) where \c alg is the
777 * hash algorithm that is calculated.
778 *
779 * \retval PSA_SUCCESS
780 * Success.
781 * \retval PSA_ERROR_BAD_STATE
782 * The operation state is not valid (not started, or already completed).
783 * \retval PSA_ERROR_BUFFER_TOO_SMALL
784 * The size of the \c hash buffer is too small. You can determine a
785 * sufficient buffer size by calling #PSA_HASH_FINAL_SIZE(alg)
786 * where \c alg is the hash algorithm that is calculated.
787 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
788 * \retval PSA_ERROR_COMMUNICATION_FAILURE
789 * \retval PSA_ERROR_HARDWARE_FAILURE
790 * \retval PSA_ERROR_TAMPERING_DETECTED
791 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100792psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
793 uint8_t *hash,
794 size_t hash_size,
795 size_t *hash_length);
796
Gilles Peskine308b91d2018-02-08 09:47:44 +0100797/** Finish the calculation of the hash of a message and compare it with
798 * an expected value.
799 *
800 * The application must call psa_hash_start() before calling this function.
801 * This function calculates the hash of the message formed by concatenating
802 * the inputs passed to preceding calls to psa_hash_update(). It then
803 * compares the calculated hash with the expected hash passed as a
804 * parameter to this function.
805 *
806 * When this function returns, the operation becomes inactive.
807 *
Gilles Peskine19067982018-03-20 17:54:53 +0100808 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100809 * comparison between the actual hash and the expected hash is performed
810 * in constant time.
811 *
812 * \param operation Active hash operation.
813 * \param hash Buffer containing the expected hash value.
814 * \param hash_length Size of the \c hash buffer in bytes.
815 *
816 * \retval PSA_SUCCESS
817 * The expected hash is identical to the actual hash of the message.
818 * \retval PSA_ERROR_INVALID_SIGNATURE
819 * The hash of the message was calculated successfully, but it
820 * differs from the expected hash.
821 * \retval PSA_ERROR_BAD_STATE
822 * The operation state is not valid (not started, or already completed).
823 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
824 * \retval PSA_ERROR_COMMUNICATION_FAILURE
825 * \retval PSA_ERROR_HARDWARE_FAILURE
826 * \retval PSA_ERROR_TAMPERING_DETECTED
827 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100828psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
829 const uint8_t *hash,
830 size_t hash_length);
831
Gilles Peskine308b91d2018-02-08 09:47:44 +0100832/** Abort a hash operation.
833 *
834 * This function may be called at any time after psa_hash_start().
835 * Aborting an operation frees all associated resources except for the
836 * \c operation structure itself.
837 *
838 * Implementation should strive to be robust and handle inactive hash
839 * operations safely (do nothing and return #PSA_ERROR_BAD_STATE). However,
840 * application writers should beware that uninitialized memory may happen
841 * to be indistinguishable from an active hash operation, and the behavior
842 * of psa_hash_abort() is undefined in this case.
843 *
844 * \param operation Active hash operation.
845 *
846 * \retval PSA_SUCCESS
847 * \retval PSA_ERROR_BAD_STATE
848 * \c operation is not an active hash operation.
849 * \retval PSA_ERROR_COMMUNICATION_FAILURE
850 * \retval PSA_ERROR_HARDWARE_FAILURE
851 * \retval PSA_ERROR_TAMPERING_DETECTED
852 */
853psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100854
855/**@}*/
856
Gilles Peskine8c9def32018-02-08 10:02:12 +0100857/** \defgroup MAC Message authentication codes
858 * @{
859 */
860
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100861/** The type of the state data structure for multipart MAC operations.
862 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100863 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100864 * make any assumptions about the content of this structure except
865 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100866typedef struct psa_mac_operation_s psa_mac_operation_t;
867
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100868/** The size of the output of psa_mac_finish(), in bytes.
869 *
870 * This is also the MAC size that psa_mac_verify() expects.
871 *
872 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
873 * #PSA_ALG_IS_MAC(alg) is true).
874 *
875 * \return The MAC size for the specified algorithm.
876 * If the MAC algorithm is not recognized, return 0.
877 * An implementation may return either 0 or the correct size
878 * for a MAC algorithm that it recognizes, but does not support.
879 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100880#define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \
881 (PSA_ALG_IS_HMAC(alg) ? PSA_HASH_FINAL_SIZE(PSA_ALG_HMAC_HASH(alg)) : \
882 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
883 0)
884
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100885/** Start a multipart MAC operation.
886 *
887 * The sequence of operations to calculate a MAC (message authentication code)
888 * is as follows:
889 * -# Allocate an operation object which will be passed to all the functions
890 * listed here.
891 * -# Call psa_mac_start() to specify the algorithm and key.
892 * The key remains associated with the operation even if the content
893 * of the key slot changes.
894 * -# Call psa_mac_update() zero, one or more times, passing a fragment
895 * of the message each time. The MAC that is calculated is the MAC
896 * of the concatenation of these messages in order.
897 * -# To calculate the MAC, call psa_mac_finish().
898 * To compare the MAC with an expected value, call psa_mac_verify().
899 *
900 * The application may call psa_mac_abort() at any time after the operation
901 * has been initialized with psa_mac_start().
902 *
903 * After a successful call to psa_mac_start(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100904 * eventually terminate the operation. The following events terminate an
905 * operation:
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100906 * - A failed call to psa_mac_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100907 * - A call to psa_mac_finish(), psa_mac_verify() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100908 *
909 * \param operation
910 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
911 * such that #PSA_ALG_IS_MAC(alg) is true).
912 *
913 * \retval PSA_SUCCESS
914 * Success.
915 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine92b30732018-03-03 21:29:30 +0100916 * \retval PSA_ERROR_NOT_PERMITTED
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100917 * \retval PSA_ERROR_INVALID_ARGUMENT
918 * \c key is not compatible with \c alg.
919 * \retval PSA_ERROR_NOT_SUPPORTED
920 * \c alg is not supported or is not a MAC algorithm.
921 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
922 * \retval PSA_ERROR_COMMUNICATION_FAILURE
923 * \retval PSA_ERROR_HARDWARE_FAILURE
924 * \retval PSA_ERROR_TAMPERING_DETECTED
925 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100926psa_status_t psa_mac_start(psa_mac_operation_t *operation,
927 psa_key_slot_t key,
928 psa_algorithm_t alg);
929
930psa_status_t psa_mac_update(psa_mac_operation_t *operation,
931 const uint8_t *input,
932 size_t input_length);
933
934psa_status_t psa_mac_finish(psa_mac_operation_t *operation,
935 uint8_t *mac,
936 size_t mac_size,
937 size_t *mac_length);
938
939psa_status_t psa_mac_verify(psa_mac_operation_t *operation,
940 const uint8_t *mac,
941 size_t mac_length);
942
943psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
944
945/**@}*/
946
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100947/** \defgroup cipher Symmetric ciphers
948 * @{
949 */
950
951/** The type of the state data structure for multipart cipher operations.
952 *
953 * This is an implementation-defined \c struct. Applications should not
954 * make any assumptions about the content of this structure except
955 * as directed by the documentation of a specific implementation. */
956typedef struct psa_cipher_operation_s psa_cipher_operation_t;
957
958/** Set the key for a multipart symmetric encryption operation.
959 *
960 * The sequence of operations to encrypt a message with a symmetric cipher
961 * is as follows:
962 * -# Allocate an operation object which will be passed to all the functions
963 * listed here.
964 * -# Call psa_encrypt_setup() to specify the algorithm and key.
965 * The key remains associated with the operation even if the content
966 * of the key slot changes.
967 * -# Call either psa_encrypt_generate_iv() or psa_encrypt_set_iv() to
968 * generate or set the IV (initialization vector). You should use
969 * psa_encrypt_generate_iv() unless the protocol you are implementing
970 * requires a specific IV value.
971 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
972 * of the message each time.
973 * -# Call psa_cipher_finish().
974 *
975 * The application may call psa_cipher_abort() at any time after the operation
976 * has been initialized with psa_encrypt_setup().
977 *
978 * After a successful call to psa_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100979 * eventually terminate the operation. The following events terminate an
980 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100981 * - A failed call to psa_encrypt_generate_iv(), psa_encrypt_set_iv()
982 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100983 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100984 *
985 * \param operation
986 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
987 * such that #PSA_ALG_IS_CIPHER(alg) is true).
988 *
989 * \retval PSA_SUCCESS
990 * Success.
991 * \retval PSA_ERROR_EMPTY_SLOT
992 * \retval PSA_ERROR_NOT_PERMITTED
993 * \retval PSA_ERROR_INVALID_ARGUMENT
994 * \c key is not compatible with \c alg.
995 * \retval PSA_ERROR_NOT_SUPPORTED
996 * \c alg is not supported or is not a cipher algorithm.
997 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
998 * \retval PSA_ERROR_COMMUNICATION_FAILURE
999 * \retval PSA_ERROR_HARDWARE_FAILURE
1000 * \retval PSA_ERROR_TAMPERING_DETECTED
1001 */
1002psa_status_t psa_encrypt_setup(psa_cipher_operation_t *operation,
1003 psa_key_slot_t key,
1004 psa_algorithm_t alg);
1005
1006/** Set the key for a multipart symmetric decryption operation.
1007 *
1008 * The sequence of operations to decrypt a message with a symmetric cipher
1009 * is as follows:
1010 * -# Allocate an operation object which will be passed to all the functions
1011 * listed here.
1012 * -# Call psa_decrypt_setup() to specify the algorithm and key.
1013 * The key remains associated with the operation even if the content
1014 * of the key slot changes.
1015 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1016 * decryption. If the IV is prepended to the ciphertext, you can call
1017 * psa_cipher_update() on a buffer containing the IV followed by the
1018 * beginning of the message.
1019 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1020 * of the message each time.
1021 * -# Call psa_cipher_finish().
1022 *
1023 * The application may call psa_cipher_abort() at any time after the operation
1024 * has been initialized with psa_encrypt_setup().
1025 *
1026 * After a successful call to psa_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001027 * eventually terminate the operation. The following events terminate an
1028 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001029 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001030 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001031 *
1032 * \param operation
1033 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
1034 * such that #PSA_ALG_IS_CIPHER(alg) is true).
1035 *
1036 * \retval PSA_SUCCESS
1037 * Success.
1038 * \retval PSA_ERROR_EMPTY_SLOT
1039 * \retval PSA_ERROR_NOT_PERMITTED
1040 * \retval PSA_ERROR_INVALID_ARGUMENT
1041 * \c key is not compatible with \c alg.
1042 * \retval PSA_ERROR_NOT_SUPPORTED
1043 * \c alg is not supported or is not a cipher algorithm.
1044 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1045 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1046 * \retval PSA_ERROR_HARDWARE_FAILURE
1047 * \retval PSA_ERROR_TAMPERING_DETECTED
1048 */
1049psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation,
1050 psa_key_slot_t key,
1051 psa_algorithm_t alg);
1052
mohammad16038481e742018-03-18 13:57:31 +02001053psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation,
1054 unsigned char *iv,
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001055 size_t iv_size,
1056 size_t *iv_length);
1057
1058psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation,
1059 const unsigned char *iv,
1060 size_t iv_length);
1061
1062psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1063 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001064 size_t input_length,
1065 unsigned char *output,
1066 size_t output_size,
1067 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001068
1069psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001070 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001071 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001072 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001073
1074psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1075
1076/**@}*/
1077
Gilles Peskine3b555712018-03-03 21:27:57 +01001078/** \defgroup aead Authenticated encryption with associated data (AEAD)
1079 * @{
1080 */
1081
1082/** The type of the state data structure for multipart AEAD operations.
1083 *
1084 * This is an implementation-defined \c struct. Applications should not
1085 * make any assumptions about the content of this structure except
1086 * as directed by the documentation of a specific implementation. */
1087typedef struct psa_aead_operation_s psa_aead_operation_t;
1088
1089/** Set the key for a multipart authenticated encryption operation.
1090 *
1091 * The sequence of operations to authenticate-and-encrypt a message
1092 * is as follows:
1093 * -# Allocate an operation object which will be passed to all the functions
1094 * listed here.
1095 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
1096 * The key remains associated with the operation even if the content
1097 * of the key slot changes.
1098 * -# Call either psa_aead_generate_iv() or psa_aead_set_iv() to
1099 * generate or set the IV (initialization vector). You should use
1100 * psa_encrypt_generate_iv() unless the protocol you are implementing
1101 * requires a specific IV value.
1102 * -# Call psa_aead_update_ad() to pass the associated data that is
1103 * to be authenticated but not encrypted. You may omit this step if
1104 * there is no associated data.
1105 * -# Call psa_aead_update() zero, one or more times, passing a fragment
1106 * of the data to encrypt each time.
1107 * -# Call psa_aead_finish().
1108 *
1109 * The application may call psa_aead_abort() at any time after the operation
1110 * has been initialized with psa_aead_encrypt_setup().
1111 *
Gilles Peskineed522972018-03-20 17:54:15 +01001112 * After a successful call to psa_aead_encrypt_setup(), the application must
1113 * eventually terminate the operation. The following events terminate an
1114 * operation:
Gilles Peskine3b555712018-03-03 21:27:57 +01001115 * - A failed call to psa_aead_generate_iv(), psa_aead_set_iv(),
1116 * psa_aead_update_ad() or psa_aead_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001117 * - A call to psa_aead_finish() or psa_aead_abort().
Gilles Peskine3b555712018-03-03 21:27:57 +01001118 *
1119 * \param operation
1120 * \param alg The AEAD algorithm to compute (\c PSA_ALG_XXX value
1121 * such that #PSA_ALG_IS_AEAD(alg) is true).
1122 *
1123 * \retval PSA_SUCCESS
1124 * Success.
1125 * \retval PSA_ERROR_EMPTY_SLOT
1126 * \retval PSA_ERROR_NOT_PERMITTED
1127 * \retval PSA_ERROR_INVALID_ARGUMENT
1128 * \c key is not compatible with \c alg.
1129 * \retval PSA_ERROR_NOT_SUPPORTED
1130 * \c alg is not supported or is not an AEAD algorithm.
1131 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1132 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1133 * \retval PSA_ERROR_HARDWARE_FAILURE
1134 * \retval PSA_ERROR_TAMPERING_DETECTED
1135 */
1136psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
1137 psa_key_slot_t key,
1138 psa_algorithm_t alg);
1139
1140/** Set the key for a multipart authenticated decryption operation.
1141 *
1142 * The sequence of operations to authenticated and decrypt a message
1143 * is as follows:
1144 * -# Allocate an operation object which will be passed to all the functions
1145 * listed here.
1146 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
1147 * The key remains associated with the operation even if the content
1148 * of the key slot changes.
1149 * -# Call psa_aead_set_iv() to pass the initialization vector (IV)
1150 * for the authenticated decryption.
1151 * -# Call psa_aead_update_ad() to pass the associated data that is
1152 * to be authenticated but not encrypted. You may omit this step if
1153 * there is no associated data.
1154 * -# Call psa_aead_update() zero, one or more times, passing a fragment
1155 * of the data to decrypt each time.
1156 * -# Call psa_aead_finish().
1157 *
1158 * The application may call psa_aead_abort() at any time after the operation
1159 * has been initialized with psa_aead_decrypt_setup().
1160 *
Gilles Peskineed522972018-03-20 17:54:15 +01001161 * After a successful call to psa_aead_decrypt_setup(), the application must
1162 * eventually terminate the operation. The following events terminate an
1163 * operation:
Gilles Peskine3b555712018-03-03 21:27:57 +01001164 * - A failed call to psa_aead_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001165 * - A call to psa_aead_finish() or psa_aead_abort().
Gilles Peskine3b555712018-03-03 21:27:57 +01001166 *
1167 * \param operation
Gilles Peskine19067982018-03-20 17:54:53 +01001168 * \param alg The AEAD algorithm to compute (\c PSA_ALG_XXX value
1169 * such that #PSA_ALG_IS_AEAD(alg) is true).
Gilles Peskine3b555712018-03-03 21:27:57 +01001170 *
1171 * \retval PSA_SUCCESS
1172 * Success.
1173 * \retval PSA_ERROR_EMPTY_SLOT
1174 * \retval PSA_ERROR_NOT_PERMITTED
1175 * \retval PSA_ERROR_INVALID_ARGUMENT
1176 * \c key is not compatible with \c alg.
1177 * \retval PSA_ERROR_NOT_SUPPORTED
Gilles Peskine19067982018-03-20 17:54:53 +01001178 * \c alg is not supported or is not an AEAD algorithm.
Gilles Peskine3b555712018-03-03 21:27:57 +01001179 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1180 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1181 * \retval PSA_ERROR_HARDWARE_FAILURE
1182 * \retval PSA_ERROR_TAMPERING_DETECTED
1183 */
1184psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
1185 psa_key_slot_t key,
1186 psa_algorithm_t alg);
1187
1188psa_status_t psa_aead_generate_iv(psa_aead_operation_t *operation,
1189 unsigned char *iv,
1190 size_t iv_size,
1191 size_t *iv_length);
1192
1193psa_status_t psa_aead_set_iv(psa_aead_operation_t *operation,
1194 const unsigned char *iv,
1195 size_t iv_length);
1196
1197psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
1198 const uint8_t *input,
1199 size_t input_length);
1200
1201psa_status_t psa_aead_update(psa_aead_operation_t *operation,
1202 const uint8_t *input,
1203 size_t input_length);
1204
1205psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
1206 uint8_t *tag,
1207 size_t tag_size,
1208 size_t *tag_length);
1209
1210psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
1211 uint8_t *tag,
1212 size_t tag_length);
1213
1214psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
1215
1216/**@}*/
1217
Gilles Peskine20035e32018-02-03 22:44:14 +01001218/** \defgroup asymmetric Asymmetric cryptography
1219 * @{
1220 */
1221
1222/**
Gilles Peskine0189e752018-02-03 23:57:22 +01001223 * \brief Maximum ECDSA signature size for a given curve bit size
1224 *
1225 * \param curve_bits Curve size in bits
1226 * \return Maximum signature size in bytes
1227 *
1228 * \note This macro returns a compile-time constant if its argument is one.
1229 *
1230 * \warning This macro may evaluate its argument multiple times.
1231 */
1232/*
1233 * RFC 4492 page 20:
1234 *
1235 * Ecdsa-Sig-Value ::= SEQUENCE {
1236 * r INTEGER,
1237 * s INTEGER
1238 * }
1239 *
1240 * Size is at most
1241 * 1 (tag) + 1 (len) + 1 (initial 0) + curve_bytes for each of r and s,
1242 * twice that + 1 (tag) + 2 (len) for the sequence
1243 * (assuming curve_bytes is less than 126 for r and s,
1244 * and less than 124 (total len <= 255) for the sequence)
1245 */
1246#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
1247 ( /*T,L of SEQUENCE*/ ((curve_bits) >= 61 * 8 ? 3 : 2) + \
1248 /*T,L of r,s*/ 2 * (((curve_bits) >= 127 * 8 ? 3 : 2) + \
1249 /*V of r,s*/ ((curve_bits) + 8) / 8))
1250
1251
Gilles Peskine308b91d2018-02-08 09:47:44 +01001252/** Safe signature buffer size for psa_asymmetric_sign().
1253 *
1254 * This macro returns a safe buffer size for a signature using a key
1255 * of the specified type and size, with the specified algorithm.
1256 * Note that the actual size of the signature may be smaller
1257 * (some algorithms produce a variable-size signature).
1258 *
1259 * \warning This function may call its arguments multiple times or
1260 * zero times, so you should not pass arguments that contain
1261 * side effects.
1262 *
1263 * \param key_type An asymmetric key type (this may indifferently be a
1264 * key pair type or a public key type).
1265 * \param key_bits The size of the key in bits.
1266 * \param alg The signature algorithm.
1267 *
1268 * \return If the parameters are valid and supported, return
1269 * a buffer size in bytes that guarantees that
1270 * psa_asymmetric_sign() will not fail with
1271 * #PSA_ERROR_BUFFER_TOO_SMALL.
1272 * If the parameters are a valid combination that is not supported
1273 * by the implementation, this macro either shall return either a
1274 * sensible size or 0.
1275 * If the parameters are not valid, the
1276 * return value is unspecified.
1277 *
1278 */
Gilles Peskine0189e752018-02-03 23:57:22 +01001279#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine2905a7a2018-03-07 16:39:31 +01001280 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine0189e752018-02-03 23:57:22 +01001281 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
Gilles Peskine84845652018-03-28 14:17:40 +02001282 ((void)alg, 0))
Gilles Peskine0189e752018-02-03 23:57:22 +01001283
1284/**
Gilles Peskine20035e32018-02-03 22:44:14 +01001285 * \brief Sign a hash or short message with a private key.
1286 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001287 * \param key Key slot containing an asymmetric key pair.
1288 * \param alg A signature algorithm that is compatible with
1289 * the type of \c key.
1290 * \param hash The message to sign.
1291 * \param hash_length Size of the \c hash buffer in bytes.
1292 * \param salt A salt or label, if supported by the signature
1293 * algorithm.
1294 * If the signature algorithm does not support a
1295 * salt, pass \c NULL.
1296 * If the signature algorithm supports an optional
1297 * salt and you do not want to pass a salt,
1298 * pass \c NULL.
1299 * \param salt_length Size of the \c salt buffer in bytes.
1300 * If \c salt is \c NULL, pass 0.
1301 * \param signature Buffer where the signature is to be written.
1302 * \param signature_size Size of the \c signature buffer in bytes.
1303 * \param signature_length On success, the number of bytes
1304 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001305 *
1306 * \retval PSA_SUCCESS
1307 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1308 * The size of the \c signature buffer is too small. You can
1309 * determine a sufficient buffer size by calling
1310 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg)
1311 * where \c key_type and \c key_bits are the type and bit-size
1312 * respectively of \c key.
1313 * \retval PSA_ERROR_NOT_SUPPORTED
1314 * \retval PSA_ERROR_INVALID_ARGUMENT
1315 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1316 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1317 * \retval PSA_ERROR_HARDWARE_FAILURE
1318 * \retval PSA_ERROR_TAMPERING_DETECTED
1319 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01001320 */
1321psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
1322 psa_algorithm_t alg,
1323 const uint8_t *hash,
1324 size_t hash_length,
1325 const uint8_t *salt,
1326 size_t salt_length,
1327 uint8_t *signature,
1328 size_t signature_size,
1329 size_t *signature_length);
1330
1331/**
1332 * \brief Verify the signature a hash or short message using a public key.
1333 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001334 * \param key Key slot containing a public key or an
1335 * asymmetric key pair.
1336 * \param alg A signature algorithm that is compatible with
1337 * the type of \c key.
1338 * \param hash The message whose signature is to be verified.
1339 * \param hash_length Size of the \c hash buffer in bytes.
1340 * \param salt A salt or label, if supported by the signature
1341 * algorithm.
1342 * If the signature algorithm does not support a
1343 * salt, pass \c NULL.
1344 * If the signature algorithm supports an optional
1345 * salt and you do not want to pass a salt,
1346 * pass \c NULL.
1347 * \param salt_length Size of the \c salt buffer in bytes.
1348 * If \c salt is \c NULL, pass 0.
1349 * \param signature Buffer containing the signature to verify.
1350 * \param signature_size Size of the \c signature buffer in bytes.
1351 *
1352 * \retval PSA_SUCCESS
1353 * The signature is valid.
1354 * \retval PSA_ERROR_INVALID_SIGNATURE
1355 * The calculation was perfomed successfully, but the passed
1356 * signature is not a valid signature.
1357 * \retval PSA_ERROR_NOT_SUPPORTED
1358 * \retval PSA_ERROR_INVALID_ARGUMENT
1359 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1360 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1361 * \retval PSA_ERROR_HARDWARE_FAILURE
1362 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01001363 */
1364psa_status_t psa_asymmetric_verify(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
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001373#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine06297932018-04-11 16:58:22 +02001374 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
1375 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
1376 0)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001377#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine06297932018-04-11 16:58:22 +02001378 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
1379 PSA_BITS_TO_BYTES(key_bits) - ((alg) == PSA_ALG_IS_RSA_OAEP_MGF1 ? \
1380 2 * (PSA_ALG_RSA_GET_HASH(alg) + 1) : \
1381 11 /*PKCS#1v1.5*/) : \
1382 0)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001383
1384/**
1385 * \brief Encrypt a short message with a public key.
1386 *
1387 * \param key Key slot containing a public key or an asymmetric
1388 * key pair.
1389 * \param alg An asymmetric encryption algorithm that is
1390 * compatible with the type of \c key.
1391 * \param input The message to encrypt.
1392 * \param input_length Size of the \c input buffer in bytes.
1393 * \param salt A salt or label, if supported by the encryption
1394 * algorithm.
1395 * If the algorithm does not support a
1396 * salt, pass \c NULL.
1397 * If the algorithm supports an optional
1398 * salt and you do not want to pass a salt,
1399 * pass \c NULL.
1400 *
1401 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1402 * supported.
1403 * \param salt_length Size of the \c salt buffer in bytes.
1404 * If \c salt is \c NULL, pass 0.
1405 * \param output Buffer where the encrypted message is to be written.
1406 * \param output_size Size of the \c output buffer in bytes.
1407 * \param output_length On success, the number of bytes
1408 * that make up the returned output.
1409 *
1410 * \retval PSA_SUCCESS
1411 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1412 * The size of the \c output buffer is too small. You can
1413 * determine a sufficient buffer size by calling
1414 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1415 * where \c key_type and \c key_bits are the type and bit-size
1416 * respectively of \c key.
1417 * \retval PSA_ERROR_NOT_SUPPORTED
1418 * \retval PSA_ERROR_INVALID_ARGUMENT
1419 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1420 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1421 * \retval PSA_ERROR_HARDWARE_FAILURE
1422 * \retval PSA_ERROR_TAMPERING_DETECTED
1423 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1424 */
1425psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
1426 psa_algorithm_t alg,
1427 const uint8_t *input,
1428 size_t input_length,
1429 const uint8_t *salt,
1430 size_t salt_length,
1431 uint8_t *output,
1432 size_t output_size,
1433 size_t *output_length);
1434
1435/**
1436 * \brief Decrypt a short message with a private key.
1437 *
1438 * \param key Key slot containing an asymmetric key pair.
1439 * \param alg An asymmetric encryption algorithm that is
1440 * compatible with the type of \c key.
1441 * \param input The message to decrypt.
1442 * \param input_length Size of the \c input buffer in bytes.
1443 * \param salt A salt or label, if supported by the encryption
1444 * algorithm.
1445 * If the algorithm does not support a
1446 * salt, pass \c NULL.
1447 * If the algorithm supports an optional
1448 * salt and you do not want to pass a salt,
1449 * pass \c NULL.
1450 *
1451 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1452 * supported.
1453 * \param salt_length Size of the \c salt buffer in bytes.
1454 * If \c salt is \c NULL, pass 0.
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001455 * \param output Buffer where the decrypted message is to be written.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001456 * \param output_size Size of the \c output buffer in bytes.
1457 * \param output_length On success, the number of bytes
1458 * that make up the returned output.
1459 *
1460 * \retval PSA_SUCCESS
1461 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1462 * The size of the \c output buffer is too small. You can
1463 * determine a sufficient buffer size by calling
1464 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1465 * where \c key_type and \c key_bits are the type and bit-size
1466 * respectively of \c key.
1467 * \retval PSA_ERROR_NOT_SUPPORTED
1468 * \retval PSA_ERROR_INVALID_ARGUMENT
1469 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1470 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1471 * \retval PSA_ERROR_HARDWARE_FAILURE
1472 * \retval PSA_ERROR_TAMPERING_DETECTED
1473 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1474 * \retval PSA_ERROR_INVALID_PADDING
1475 */
1476psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
1477 psa_algorithm_t alg,
1478 const uint8_t *input,
1479 size_t input_length,
1480 const uint8_t *salt,
1481 size_t salt_length,
1482 uint8_t *output,
1483 size_t output_size,
1484 size_t *output_length);
1485
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001486/**@}*/
1487
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001488/** \defgroup generation Key generation
1489 * @{
1490 */
1491
1492/**
1493 * \brief Generate random bytes.
1494 *
1495 * \warning This function **can** fail! Callers MUST check the return status
1496 * and MUST NOT use the content of the output buffer if the return
1497 * status is not #PSA_SUCCESS.
1498 *
1499 * \note To generate a key, use psa_generate_key() instead.
1500 *
1501 * \param output Output buffer for the generated data.
1502 * \param output_size Number of bytes to generate and output.
1503 *
1504 * \retval PSA_SUCCESS
1505 * \retval PSA_ERROR_NOT_SUPPORTED
1506 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1507 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1508 * \retval PSA_ERROR_HARDWARE_FAILURE
1509 * \retval PSA_ERROR_TAMPERING_DETECTED
1510 */
1511psa_status_t psa_generate_random(uint8_t *output,
1512 size_t output_size);
1513
1514/**
1515 * \brief Generate a key or key pair.
1516 *
1517 * \param key Slot where the key will be stored. This must be a
1518 * valid slot for a key of the chosen type. It must
1519 * be unoccupied.
1520 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
1521 * \param bits Key size in bits.
1522 * \param parameters Extra parameters for key generation. The interpretation
1523 * of this parameter depends on \c type. All types support
1524 * \c NULL to use default parameters specified below.
1525 *
1526 * For any symmetric key type (type such that
1527 * `PSA_KEY_TYPE_IS_ASYMMETRIC(type)` is false), \c parameters must be
1528 * \c NULL. For asymmetric key types defined by this specification,
1529 * the parameter type and the default parameters are defined by the
1530 * table below. For vendor-defined key types, the vendor documentation
1531 * shall define the parameter type and the default parameters.
1532 *
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001533 * Type | Parameter type | Meaning | Parameters used if `parameters == NULL`
1534 * ---- | -------------- | ------- | ---------------------------------------
1535 * `PSA_KEY_TYPE_RSA_KEYPAIR` | `unsigned int` | Public exponent | 65537
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001536 *
1537 * \retval PSA_SUCCESS
1538 * \retval PSA_ERROR_NOT_SUPPORTED
1539 * \retval PSA_ERROR_INVALID_ARGUMENT
1540 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1541 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1542 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1543 * \retval PSA_ERROR_HARDWARE_FAILURE
1544 * \retval PSA_ERROR_TAMPERING_DETECTED
1545 */
1546psa_status_t psa_generate_key(psa_key_slot_t key,
1547 psa_key_type_t type,
1548 size_t bits,
1549 const void *parameters);
1550
1551/**@}*/
1552
Gilles Peskinee59236f2018-01-27 23:32:46 +01001553#ifdef __cplusplus
1554}
1555#endif
1556
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001557/* The file "crypto_struct.h" contains definitions for
1558 * implementation-specific structs that are declared above. */
1559#include "crypto_struct.h"
1560
1561/* The file "crypto_extra.h" contains vendor-specific definitions. This
1562 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01001563#include "crypto_extra.h"
1564
1565#endif /* PSA_CRYPTO_H */