blob: db5b5f6ab251c11a14caafb4b069b9d27631ca39 [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) \
184 (((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
Gilles Peskine8c9def32018-02-08 10:02:12 +0100325#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
326#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100327
Gilles Peskinea5926232018-03-28 14:16:50 +0200328#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW ((psa_algorithm_t)0x10010000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100329#define PSA_ALG_RSA_PSS_MGF1 ((psa_algorithm_t)0x10020000)
Gilles Peskine6944f9a2018-03-28 14:18:39 +0200330#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12010000)
331#define PSA_ALG_RSA_OAEP_MGF1_BASE ((psa_algorithm_t)0x12020000)
Gilles Peskinea5926232018-03-28 14:16:50 +0200332#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
333 (PSA_ALG_RSA_PKCS1V15_SIGN_RAW | ((hash_alg) & PSA_ALG_HASH_MASK))
334#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Gilles Peskine9673cc82018-04-11 16:57:49 +0200335 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_RAW)
336#define PSA_ALG_RSA_OAEP_MGF1(hash_alg) \
337 (PSA_ALG_RSA_OAEP_MGF1_RAW | ((hash_alg) & PSA_ALG_HASH_MASK))
338#define PSA_ALG_IS_RSA_OAEP_MGF1(alg) \
Gilles Peskine625b01c2018-06-08 17:43:16 +0200339 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_MGF1_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100340#define PSA_ALG_RSA_GET_HASH(alg) \
341 (((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100342
343/**@}*/
344
345/** \defgroup key_management Key management
346 * @{
347 */
348
349/**
350 * \brief Import a key in binary format.
351 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100352 * This function supports any output from psa_export_key(). Refer to the
353 * documentation of psa_export_key() for the format for each key type.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100354 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100355 * \param key Slot where the key will be stored. This must be a
356 * valid slot for a key of the chosen type. It must
357 * be unoccupied.
358 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
359 * \param data Buffer containing the key data.
360 * \param data_length Size of the \c data buffer in bytes.
361 *
362 * \retval PSA_SUCCESS
363 * Success.
364 * \retval PSA_ERROR_NOT_SUPPORTED
365 * The key type or key size is not supported.
366 * \retval PSA_ERROR_INVALID_ARGUMENT
367 * The key slot is invalid,
368 * or the key data is not correctly formatted.
369 * \retval PSA_ERROR_OCCUPIED_SLOT
370 There is already a key in the specified slot.
371 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
372 * \retval PSA_ERROR_COMMUNICATION_FAILURE
373 * \retval PSA_ERROR_HARDWARE_FAILURE
374 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100375 */
376psa_status_t psa_import_key(psa_key_slot_t key,
377 psa_key_type_t type,
378 const uint8_t *data,
379 size_t data_length);
380
381/**
382 * \brief Destroy a key.
383 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100384 * \retval PSA_SUCCESS
385 * \retval PSA_ERROR_EMPTY_SLOT
386 * \retval PSA_ERROR_COMMUNICATION_FAILURE
387 * \retval PSA_ERROR_HARDWARE_FAILURE
388 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100389 */
390psa_status_t psa_destroy_key(psa_key_slot_t key);
391
392/**
393 * \brief Get basic metadata about a key.
394 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100395 * \param key Slot whose content is queried. This must
396 * be an occupied key slot.
397 * \param type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
398 * This may be a null pointer, in which case the key type
399 * is not written.
400 * \param bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100401 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100402 * is not written.
403 *
404 * \retval PSA_SUCCESS
405 * \retval PSA_ERROR_EMPTY_SLOT
406 * \retval PSA_ERROR_COMMUNICATION_FAILURE
407 * \retval PSA_ERROR_HARDWARE_FAILURE
408 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100409 */
410psa_status_t psa_get_key_information(psa_key_slot_t key,
411 psa_key_type_t *type,
412 size_t *bits);
413
414/**
415 * \brief Export a key in binary format.
416 *
417 * The output of this function can be passed to psa_import_key() to
418 * create an equivalent object.
419 *
420 * If a key is created with psa_import_key() and then exported with
421 * this function, it is not guaranteed that the resulting data is
422 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +0100423 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100424 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100425 * For standard key types, the output format is as follows:
426 *
427 * - For symmetric keys (including MAC keys), the format is the
428 * raw bytes of the key.
429 * - For DES, the key data consists of 8 bytes. The parity bits must be
430 * correct.
431 * - For Triple-DES, the format is the concatenation of the
432 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100433 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine308b91d2018-02-08 09:47:44 +0100434 * is the non-encrypted DER representation defined by PKCS\#8 (RFC 5208)
435 * as PrivateKeyInfo.
436 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
Gilles Peskine971f7062018-03-20 17:52:58 +0100437 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100438 *
439 * \param key Slot whose content is to be exported. This must
440 * be an occupied key slot.
441 * \param data Buffer where the key data is to be written.
442 * \param data_size Size of the \c data buffer in bytes.
443 * \param data_length On success, the number of bytes
444 * that make up the key data.
445 *
446 * \retval PSA_SUCCESS
447 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine92b30732018-03-03 21:29:30 +0100448 * \retval PSA_ERROR_NOT_PERMITTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100449 * \retval PSA_ERROR_COMMUNICATION_FAILURE
450 * \retval PSA_ERROR_HARDWARE_FAILURE
451 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100452 */
453psa_status_t psa_export_key(psa_key_slot_t key,
454 uint8_t *data,
455 size_t data_size,
456 size_t *data_length);
457
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100458/**
459 * \brief Export a public key or the public part of a key pair in binary format.
460 *
461 * The output of this function can be passed to psa_import_key() to
462 * create an object that is equivalent to the public key.
463 *
464 * For standard key types, the output format is as follows:
465 *
466 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
Gilles Peskine971f7062018-03-20 17:52:58 +0100467 * is the DER representation of the public key defined by RFC 5280
468 * as SubjectPublicKeyInfo.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100469 *
470 * \param key Slot whose content is to be exported. This must
471 * be an occupied key slot.
472 * \param data Buffer where the key data is to be written.
473 * \param data_size Size of the \c data buffer in bytes.
474 * \param data_length On success, the number of bytes
475 * that make up the key data.
476 *
477 * \retval PSA_SUCCESS
478 * \retval PSA_ERROR_EMPTY_SLOT
479 * \retval PSA_ERROR_INVALID_ARGUMENT
480 * \retval PSA_ERROR_COMMUNICATION_FAILURE
481 * \retval PSA_ERROR_HARDWARE_FAILURE
482 * \retval PSA_ERROR_TAMPERING_DETECTED
483 */
484psa_status_t psa_export_public_key(psa_key_slot_t key,
485 uint8_t *data,
486 size_t data_size,
487 size_t *data_length);
488
489/**@}*/
490
491/** \defgroup policy Key policies
492 * @{
493 */
494
495/** \brief Encoding of permitted usage on a key. */
496typedef uint32_t psa_key_usage_t;
497
Gilles Peskine7e198532018-03-08 07:50:30 +0100498/** Whether the key may be exported.
499 *
500 * A public key or the public part of a key pair may always be exported
501 * regardless of the value of this permission flag.
502 *
503 * If a key does not have export permission, implementations shall not
504 * allow the key to be exported in plain form from the cryptoprocessor,
505 * whether through psa_export_key() or through a proprietary interface.
506 * The key may however be exportable in a wrapped form, i.e. in a form
507 * where it is encrypted by another key.
508 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100509#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
510
Gilles Peskine7e198532018-03-08 07:50:30 +0100511/** Whether the key may be used to encrypt a message.
512 *
513 * For a key pair, this concerns the public key.
514 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100515#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +0100516
517/** Whether the key may be used to decrypt a message.
518 *
519 * For a key pair, this concerns the private key.
520 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100521#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +0100522
523/** Whether the key may be used to sign 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_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +0100528
529/** Whether the key may be used to verify a message signature.
530 *
531 * For a key pair, this concerns the public key.
532 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100533#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
534
535/** The type of the key policy data structure.
536 *
537 * This is an implementation-defined \c struct. Applications should not
538 * make any assumptions about the content of this structure except
539 * as directed by the documentation of a specific implementation. */
540typedef struct psa_key_policy_s psa_key_policy_t;
541
542/** \brief Initialize a key policy structure to a default that forbids all
543 * usage of the key. */
544void psa_key_policy_init(psa_key_policy_t *policy);
545
Gilles Peskine7e198532018-03-08 07:50:30 +0100546/** \brief Set the standard fields of a policy structure.
547 *
548 * Note that this function does not make any consistency check of the
549 * parameters. The values are only checked when applying the policy to
550 * a key slot with psa_set_key_policy().
551 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100552void psa_key_policy_set_usage(psa_key_policy_t *policy,
553 psa_key_usage_t usage,
554 psa_algorithm_t alg);
555
556psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy);
557
558psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy);
559
560/** \brief Set the usage policy on a key slot.
561 *
562 * This function must be called on an empty key slot, before importing,
563 * generating or creating a key in the slot. Changing the policy of an
564 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100565 *
566 * Implementations may set restrictions on supported key policies
567 * depending on the key type and the key slot.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100568 */
569psa_status_t psa_set_key_policy(psa_key_slot_t key,
570 const psa_key_policy_t *policy);
571
Gilles Peskine7e198532018-03-08 07:50:30 +0100572/** \brief Get the usage policy for a key slot.
573 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100574psa_status_t psa_get_key_policy(psa_key_slot_t key,
575 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100576
577/**@}*/
578
Gilles Peskine609b6a52018-03-03 21:31:50 +0100579/** \defgroup persistence Key lifetime
580 * @{
581 */
582
583/** Encoding of key lifetimes.
584 */
585typedef uint32_t psa_key_lifetime_t;
586
587/** A volatile key slot retains its content as long as the application is
588 * running. It is guaranteed to be erased on a power reset.
589 */
590#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
591
592/** A persistent key slot retains its content as long as it is not explicitly
593 * destroyed.
594 */
595#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
596
597/** A write-once key slot may not be modified once a key has been set.
598 * It will retain its content as long as the device remains operational.
599 */
600#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
601
Gilles Peskined393e182018-03-08 07:49:16 +0100602/** \brief Retrieve the lifetime of a key slot.
603 *
604 * The assignment of lifetimes to slots is implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200605 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +0200606 * \param key Slot to query.
mohammad1603804cd712018-03-20 22:44:08 +0200607 * \param lifetime On success, the lifetime value.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200608 *
mohammad1603804cd712018-03-20 22:44:08 +0200609 * \retval PSA_SUCCESS
610 * Success.
611 * \retval PSA_ERROR_INVALID_ARGUMENT
mohammad1603a7d245a2018-04-17 00:40:08 -0700612 * The key slot is invalid.
Gilles Peskinef0c9dd32018-04-17 14:11:07 +0200613 * \retval PSA_ERROR_COMMUNICATION_FAILURE
614 * \retval PSA_ERROR_HARDWARE_FAILURE
615 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +0100616 */
Gilles Peskine609b6a52018-03-03 21:31:50 +0100617psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
618 psa_key_lifetime_t *lifetime);
619
Gilles Peskined393e182018-03-08 07:49:16 +0100620/** \brief Change the lifetime of a key slot.
621 *
622 * Whether the lifetime of a key slot can be changed at all, and if so
Gilles Peskine19067982018-03-20 17:54:53 +0100623 * whether the lifetime of an occupied key slot can be changed, is
Gilles Peskined393e182018-03-08 07:49:16 +0100624 * implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200625 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +0200626 * \param key Slot whose lifetime is to be changed.
627 * \param lifetime The lifetime value to set for the given key slot.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200628 *
mohammad1603804cd712018-03-20 22:44:08 +0200629 * \retval PSA_SUCCESS
630 * Success.
631 * \retval PSA_ERROR_INVALID_ARGUMENT
632 * The key slot is invalid,
mohammad1603a7d245a2018-04-17 00:40:08 -0700633 * or the lifetime value is invalid.
Gilles Peskinef0c9dd32018-04-17 14:11:07 +0200634 * \retval PSA_ERROR_NOT_SUPPORTED
635 * The implementation does not support the specified lifetime value,
636 * at least for the specified key slot.
637 * \retval PSA_ERROR_OCCUPIED_SLOT
638 * The slot contains a key, and the implementation does not support
639 * changing the lifetime of an occupied slot.
640 * \retval PSA_ERROR_COMMUNICATION_FAILURE
641 * \retval PSA_ERROR_HARDWARE_FAILURE
642 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +0100643 */
644psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
mohammad1603ea050092018-04-17 00:31:34 -0700645 psa_key_lifetime_t lifetime);
Gilles Peskined393e182018-03-08 07:49:16 +0100646
Gilles Peskine609b6a52018-03-03 21:31:50 +0100647/**@}*/
648
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100649/** \defgroup hash Message digests
650 * @{
651 */
652
Gilles Peskine308b91d2018-02-08 09:47:44 +0100653/** The type of the state data structure for multipart hash operations.
654 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100655 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100656 * make any assumptions about the content of this structure except
657 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100658typedef struct psa_hash_operation_s psa_hash_operation_t;
659
Gilles Peskine308b91d2018-02-08 09:47:44 +0100660/** The size of the output of psa_hash_finish(), in bytes.
661 *
662 * This is also the hash size that psa_hash_verify() expects.
663 *
664 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
665 * #PSA_ALG_IS_HASH(alg) is true).
666 *
667 * \return The hash size for the specified hash algorithm.
668 * If the hash algorithm is not recognized, return 0.
669 * An implementation may return either 0 or the correct size
670 * for a hash algorithm that it recognizes, but does not support.
671 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100672#define PSA_HASH_FINAL_SIZE(alg) \
673 ( \
674 (alg) == PSA_ALG_MD2 ? 16 : \
675 (alg) == PSA_ALG_MD4 ? 16 : \
676 (alg) == PSA_ALG_MD5 ? 16 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100677 (alg) == PSA_ALG_RIPEMD160 ? 20 : \
678 (alg) == PSA_ALG_SHA_1 ? 20 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100679 (alg) == PSA_ALG_SHA_224 ? 28 : \
680 (alg) == PSA_ALG_SHA_256 ? 32 : \
681 (alg) == PSA_ALG_SHA_384 ? 48 : \
682 (alg) == PSA_ALG_SHA_512 ? 64 : \
683 (alg) == PSA_ALG_SHA_512_224 ? 28 : \
684 (alg) == PSA_ALG_SHA_512_256 ? 32 : \
685 (alg) == PSA_ALG_SHA3_224 ? 28 : \
686 (alg) == PSA_ALG_SHA3_256 ? 32 : \
687 (alg) == PSA_ALG_SHA3_384 ? 48 : \
688 (alg) == PSA_ALG_SHA3_512 ? 64 : \
689 0)
690
Gilles Peskine308b91d2018-02-08 09:47:44 +0100691/** Start a multipart hash operation.
692 *
693 * The sequence of operations to calculate a hash (message digest)
694 * is as follows:
695 * -# Allocate an operation object which will be passed to all the functions
696 * listed here.
697 * -# Call psa_hash_start() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100698 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100699 * of the message each time. The hash that is calculated is the hash
700 * of the concatenation of these messages in order.
701 * -# To calculate the hash, call psa_hash_finish().
702 * To compare the hash with an expected value, call psa_hash_verify().
703 *
704 * The application may call psa_hash_abort() at any time after the operation
705 * has been initialized with psa_hash_start().
706 *
707 * After a successful call to psa_hash_start(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100708 * eventually terminate the operation. The following events terminate an
709 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100710 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100711 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100712 *
713 * \param operation
714 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
715 * such that #PSA_ALG_IS_HASH(alg) is true).
716 *
717 * \retval PSA_SUCCESS
718 * Success.
719 * \retval PSA_ERROR_NOT_SUPPORTED
720 * \c alg is not supported or is not a hash algorithm.
721 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
722 * \retval PSA_ERROR_COMMUNICATION_FAILURE
723 * \retval PSA_ERROR_HARDWARE_FAILURE
724 * \retval PSA_ERROR_TAMPERING_DETECTED
725 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100726psa_status_t psa_hash_start(psa_hash_operation_t *operation,
727 psa_algorithm_t alg);
728
Gilles Peskine308b91d2018-02-08 09:47:44 +0100729/** Add a message fragment to a multipart hash operation.
730 *
731 * The application must call psa_hash_start() before calling this function.
732 *
733 * If this function returns an error status, the operation becomes inactive.
734 *
735 * \param operation Active hash operation.
736 * \param input Buffer containing the message fragment to hash.
737 * \param input_length Size of the \c input buffer in bytes.
738 *
739 * \retval PSA_SUCCESS
740 * Success.
741 * \retval PSA_ERROR_BAD_STATE
742 * The operation state is not valid (not started, or already completed).
743 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
744 * \retval PSA_ERROR_COMMUNICATION_FAILURE
745 * \retval PSA_ERROR_HARDWARE_FAILURE
746 * \retval PSA_ERROR_TAMPERING_DETECTED
747 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100748psa_status_t psa_hash_update(psa_hash_operation_t *operation,
749 const uint8_t *input,
750 size_t input_length);
751
Gilles Peskine308b91d2018-02-08 09:47:44 +0100752/** Finish the calculation of the hash of a message.
753 *
754 * The application must call psa_hash_start() before calling this function.
755 * This function calculates the hash of the message formed by concatenating
756 * the inputs passed to preceding calls to psa_hash_update().
757 *
758 * When this function returns, the operation becomes inactive.
759 *
760 * \warning Applications should not call this function if they expect
761 * a specific value for the hash. Call psa_hash_verify() instead.
762 * Beware that comparing integrity or authenticity data such as
763 * hash values with a function such as \c memcmp is risky
764 * because the time taken by the comparison may leak information
765 * about the hashed data which could allow an attacker to guess
766 * a valid hash and thereby bypass security controls.
767 *
768 * \param operation Active hash operation.
769 * \param hash Buffer where the hash is to be written.
770 * \param hash_size Size of the \c hash buffer in bytes.
771 * \param hash_length On success, the number of bytes
772 * that make up the hash value. This is always
773 * #PSA_HASH_FINAL_SIZE(alg) where \c alg is the
774 * hash algorithm that is calculated.
775 *
776 * \retval PSA_SUCCESS
777 * Success.
778 * \retval PSA_ERROR_BAD_STATE
779 * The operation state is not valid (not started, or already completed).
780 * \retval PSA_ERROR_BUFFER_TOO_SMALL
781 * The size of the \c hash buffer is too small. You can determine a
782 * sufficient buffer size by calling #PSA_HASH_FINAL_SIZE(alg)
783 * where \c alg is the hash algorithm that is calculated.
784 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
785 * \retval PSA_ERROR_COMMUNICATION_FAILURE
786 * \retval PSA_ERROR_HARDWARE_FAILURE
787 * \retval PSA_ERROR_TAMPERING_DETECTED
788 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100789psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
790 uint8_t *hash,
791 size_t hash_size,
792 size_t *hash_length);
793
Gilles Peskine308b91d2018-02-08 09:47:44 +0100794/** Finish the calculation of the hash of a message and compare it with
795 * an expected value.
796 *
797 * The application must call psa_hash_start() before calling this function.
798 * This function calculates the hash of the message formed by concatenating
799 * the inputs passed to preceding calls to psa_hash_update(). It then
800 * compares the calculated hash with the expected hash passed as a
801 * parameter to this function.
802 *
803 * When this function returns, the operation becomes inactive.
804 *
Gilles Peskine19067982018-03-20 17:54:53 +0100805 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100806 * comparison between the actual hash and the expected hash is performed
807 * in constant time.
808 *
809 * \param operation Active hash operation.
810 * \param hash Buffer containing the expected hash value.
811 * \param hash_length Size of the \c hash buffer in bytes.
812 *
813 * \retval PSA_SUCCESS
814 * The expected hash is identical to the actual hash of the message.
815 * \retval PSA_ERROR_INVALID_SIGNATURE
816 * The hash of the message was calculated successfully, but it
817 * differs from the expected hash.
818 * \retval PSA_ERROR_BAD_STATE
819 * The operation state is not valid (not started, or already completed).
820 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
821 * \retval PSA_ERROR_COMMUNICATION_FAILURE
822 * \retval PSA_ERROR_HARDWARE_FAILURE
823 * \retval PSA_ERROR_TAMPERING_DETECTED
824 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100825psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
826 const uint8_t *hash,
827 size_t hash_length);
828
Gilles Peskine308b91d2018-02-08 09:47:44 +0100829/** Abort a hash operation.
830 *
831 * This function may be called at any time after psa_hash_start().
832 * Aborting an operation frees all associated resources except for the
833 * \c operation structure itself.
834 *
835 * Implementation should strive to be robust and handle inactive hash
836 * operations safely (do nothing and return #PSA_ERROR_BAD_STATE). However,
837 * application writers should beware that uninitialized memory may happen
838 * to be indistinguishable from an active hash operation, and the behavior
839 * of psa_hash_abort() is undefined in this case.
840 *
841 * \param operation Active hash operation.
842 *
843 * \retval PSA_SUCCESS
844 * \retval PSA_ERROR_BAD_STATE
845 * \c operation is not an active hash operation.
846 * \retval PSA_ERROR_COMMUNICATION_FAILURE
847 * \retval PSA_ERROR_HARDWARE_FAILURE
848 * \retval PSA_ERROR_TAMPERING_DETECTED
849 */
850psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100851
852/**@}*/
853
Gilles Peskine8c9def32018-02-08 10:02:12 +0100854/** \defgroup MAC Message authentication codes
855 * @{
856 */
857
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100858/** The type of the state data structure for multipart MAC operations.
859 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100860 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100861 * make any assumptions about the content of this structure except
862 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100863typedef struct psa_mac_operation_s psa_mac_operation_t;
864
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100865/** The size of the output of psa_mac_finish(), in bytes.
866 *
867 * This is also the MAC size that psa_mac_verify() expects.
868 *
869 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
870 * #PSA_ALG_IS_MAC(alg) is true).
871 *
872 * \return The MAC size for the specified algorithm.
873 * If the MAC algorithm is not recognized, return 0.
874 * An implementation may return either 0 or the correct size
875 * for a MAC algorithm that it recognizes, but does not support.
876 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100877#define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \
878 (PSA_ALG_IS_HMAC(alg) ? PSA_HASH_FINAL_SIZE(PSA_ALG_HMAC_HASH(alg)) : \
879 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
880 0)
881
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100882/** Start a multipart MAC operation.
883 *
884 * The sequence of operations to calculate a MAC (message authentication code)
885 * is as follows:
886 * -# Allocate an operation object which will be passed to all the functions
887 * listed here.
888 * -# Call psa_mac_start() to specify the algorithm and key.
889 * The key remains associated with the operation even if the content
890 * of the key slot changes.
891 * -# Call psa_mac_update() zero, one or more times, passing a fragment
892 * of the message each time. The MAC that is calculated is the MAC
893 * of the concatenation of these messages in order.
894 * -# To calculate the MAC, call psa_mac_finish().
895 * To compare the MAC with an expected value, call psa_mac_verify().
896 *
897 * The application may call psa_mac_abort() at any time after the operation
898 * has been initialized with psa_mac_start().
899 *
900 * After a successful call to psa_mac_start(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100901 * eventually terminate the operation. The following events terminate an
902 * operation:
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100903 * - A failed call to psa_mac_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100904 * - A call to psa_mac_finish(), psa_mac_verify() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100905 *
906 * \param operation
907 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
908 * such that #PSA_ALG_IS_MAC(alg) is true).
909 *
910 * \retval PSA_SUCCESS
911 * Success.
912 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine92b30732018-03-03 21:29:30 +0100913 * \retval PSA_ERROR_NOT_PERMITTED
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100914 * \retval PSA_ERROR_INVALID_ARGUMENT
915 * \c key is not compatible with \c alg.
916 * \retval PSA_ERROR_NOT_SUPPORTED
917 * \c alg is not supported or is not a MAC algorithm.
918 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
919 * \retval PSA_ERROR_COMMUNICATION_FAILURE
920 * \retval PSA_ERROR_HARDWARE_FAILURE
921 * \retval PSA_ERROR_TAMPERING_DETECTED
922 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100923psa_status_t psa_mac_start(psa_mac_operation_t *operation,
924 psa_key_slot_t key,
925 psa_algorithm_t alg);
926
927psa_status_t psa_mac_update(psa_mac_operation_t *operation,
928 const uint8_t *input,
929 size_t input_length);
930
931psa_status_t psa_mac_finish(psa_mac_operation_t *operation,
932 uint8_t *mac,
933 size_t mac_size,
934 size_t *mac_length);
935
936psa_status_t psa_mac_verify(psa_mac_operation_t *operation,
937 const uint8_t *mac,
938 size_t mac_length);
939
940psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
941
942/**@}*/
943
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100944/** \defgroup cipher Symmetric ciphers
945 * @{
946 */
947
948/** The type of the state data structure for multipart cipher operations.
949 *
950 * This is an implementation-defined \c struct. Applications should not
951 * make any assumptions about the content of this structure except
952 * as directed by the documentation of a specific implementation. */
953typedef struct psa_cipher_operation_s psa_cipher_operation_t;
954
955/** Set the key for a multipart symmetric encryption operation.
956 *
957 * The sequence of operations to encrypt a message with a symmetric cipher
958 * is as follows:
959 * -# Allocate an operation object which will be passed to all the functions
960 * listed here.
961 * -# Call psa_encrypt_setup() to specify the algorithm and key.
962 * The key remains associated with the operation even if the content
963 * of the key slot changes.
964 * -# Call either psa_encrypt_generate_iv() or psa_encrypt_set_iv() to
965 * generate or set the IV (initialization vector). You should use
966 * psa_encrypt_generate_iv() unless the protocol you are implementing
967 * requires a specific IV value.
968 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
969 * of the message each time.
970 * -# Call psa_cipher_finish().
971 *
972 * The application may call psa_cipher_abort() at any time after the operation
973 * has been initialized with psa_encrypt_setup().
974 *
975 * After a successful call to psa_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100976 * eventually terminate the operation. The following events terminate an
977 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100978 * - A failed call to psa_encrypt_generate_iv(), psa_encrypt_set_iv()
979 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100980 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100981 *
982 * \param operation
983 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
984 * such that #PSA_ALG_IS_CIPHER(alg) is true).
985 *
986 * \retval PSA_SUCCESS
987 * Success.
988 * \retval PSA_ERROR_EMPTY_SLOT
989 * \retval PSA_ERROR_NOT_PERMITTED
990 * \retval PSA_ERROR_INVALID_ARGUMENT
991 * \c key is not compatible with \c alg.
992 * \retval PSA_ERROR_NOT_SUPPORTED
993 * \c alg is not supported or is not a cipher algorithm.
994 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
995 * \retval PSA_ERROR_COMMUNICATION_FAILURE
996 * \retval PSA_ERROR_HARDWARE_FAILURE
997 * \retval PSA_ERROR_TAMPERING_DETECTED
998 */
999psa_status_t psa_encrypt_setup(psa_cipher_operation_t *operation,
1000 psa_key_slot_t key,
1001 psa_algorithm_t alg);
1002
1003/** Set the key for a multipart symmetric decryption operation.
1004 *
1005 * The sequence of operations to decrypt a message with a symmetric cipher
1006 * is as follows:
1007 * -# Allocate an operation object which will be passed to all the functions
1008 * listed here.
1009 * -# Call psa_decrypt_setup() to specify the algorithm and key.
1010 * The key remains associated with the operation even if the content
1011 * of the key slot changes.
1012 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1013 * decryption. If the IV is prepended to the ciphertext, you can call
1014 * psa_cipher_update() on a buffer containing the IV followed by the
1015 * beginning of the message.
1016 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1017 * of the message each time.
1018 * -# Call psa_cipher_finish().
1019 *
1020 * The application may call psa_cipher_abort() at any time after the operation
1021 * has been initialized with psa_encrypt_setup().
1022 *
1023 * After a successful call to psa_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001024 * eventually terminate the operation. The following events terminate an
1025 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001026 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001027 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001028 *
1029 * \param operation
1030 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
1031 * such that #PSA_ALG_IS_CIPHER(alg) is true).
1032 *
1033 * \retval PSA_SUCCESS
1034 * Success.
1035 * \retval PSA_ERROR_EMPTY_SLOT
1036 * \retval PSA_ERROR_NOT_PERMITTED
1037 * \retval PSA_ERROR_INVALID_ARGUMENT
1038 * \c key is not compatible with \c alg.
1039 * \retval PSA_ERROR_NOT_SUPPORTED
1040 * \c alg is not supported or is not a cipher algorithm.
1041 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1042 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1043 * \retval PSA_ERROR_HARDWARE_FAILURE
1044 * \retval PSA_ERROR_TAMPERING_DETECTED
1045 */
1046psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation,
1047 psa_key_slot_t key,
1048 psa_algorithm_t alg);
1049
1050psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation,
1051 unsigned char *iv,
1052 size_t iv_size,
1053 size_t *iv_length);
1054
1055psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation,
1056 const unsigned char *iv,
1057 size_t iv_length);
1058
1059psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1060 const uint8_t *input,
1061 size_t input_length);
1062
1063psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
1064 uint8_t *mac,
1065 size_t mac_size,
1066 size_t *mac_length);
1067
1068psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1069
1070/**@}*/
1071
Gilles Peskine3b555712018-03-03 21:27:57 +01001072/** \defgroup aead Authenticated encryption with associated data (AEAD)
1073 * @{
1074 */
1075
1076/** The type of the state data structure for multipart AEAD operations.
1077 *
1078 * This is an implementation-defined \c struct. Applications should not
1079 * make any assumptions about the content of this structure except
1080 * as directed by the documentation of a specific implementation. */
1081typedef struct psa_aead_operation_s psa_aead_operation_t;
1082
1083/** Set the key for a multipart authenticated encryption operation.
1084 *
1085 * The sequence of operations to authenticate-and-encrypt a message
1086 * is as follows:
1087 * -# Allocate an operation object which will be passed to all the functions
1088 * listed here.
1089 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
1090 * The key remains associated with the operation even if the content
1091 * of the key slot changes.
1092 * -# Call either psa_aead_generate_iv() or psa_aead_set_iv() to
1093 * generate or set the IV (initialization vector). You should use
1094 * psa_encrypt_generate_iv() unless the protocol you are implementing
1095 * requires a specific IV value.
1096 * -# Call psa_aead_update_ad() to pass the associated data that is
1097 * to be authenticated but not encrypted. You may omit this step if
1098 * there is no associated data.
1099 * -# Call psa_aead_update() zero, one or more times, passing a fragment
1100 * of the data to encrypt each time.
1101 * -# Call psa_aead_finish().
1102 *
1103 * The application may call psa_aead_abort() at any time after the operation
1104 * has been initialized with psa_aead_encrypt_setup().
1105 *
Gilles Peskineed522972018-03-20 17:54:15 +01001106 * After a successful call to psa_aead_encrypt_setup(), the application must
1107 * eventually terminate the operation. The following events terminate an
1108 * operation:
Gilles Peskine3b555712018-03-03 21:27:57 +01001109 * - A failed call to psa_aead_generate_iv(), psa_aead_set_iv(),
1110 * psa_aead_update_ad() or psa_aead_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001111 * - A call to psa_aead_finish() or psa_aead_abort().
Gilles Peskine3b555712018-03-03 21:27:57 +01001112 *
1113 * \param operation
1114 * \param alg The AEAD algorithm to compute (\c PSA_ALG_XXX value
1115 * such that #PSA_ALG_IS_AEAD(alg) is true).
1116 *
1117 * \retval PSA_SUCCESS
1118 * Success.
1119 * \retval PSA_ERROR_EMPTY_SLOT
1120 * \retval PSA_ERROR_NOT_PERMITTED
1121 * \retval PSA_ERROR_INVALID_ARGUMENT
1122 * \c key is not compatible with \c alg.
1123 * \retval PSA_ERROR_NOT_SUPPORTED
1124 * \c alg is not supported or is not an AEAD algorithm.
1125 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1126 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1127 * \retval PSA_ERROR_HARDWARE_FAILURE
1128 * \retval PSA_ERROR_TAMPERING_DETECTED
1129 */
1130psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
1131 psa_key_slot_t key,
1132 psa_algorithm_t alg);
1133
1134/** Set the key for a multipart authenticated decryption operation.
1135 *
1136 * The sequence of operations to authenticated and decrypt a message
1137 * is as follows:
1138 * -# Allocate an operation object which will be passed to all the functions
1139 * listed here.
1140 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
1141 * The key remains associated with the operation even if the content
1142 * of the key slot changes.
1143 * -# Call psa_aead_set_iv() to pass the initialization vector (IV)
1144 * for the authenticated decryption.
1145 * -# Call psa_aead_update_ad() to pass the associated data that is
1146 * to be authenticated but not encrypted. You may omit this step if
1147 * there is no associated data.
1148 * -# Call psa_aead_update() zero, one or more times, passing a fragment
1149 * of the data to decrypt each time.
1150 * -# Call psa_aead_finish().
1151 *
1152 * The application may call psa_aead_abort() at any time after the operation
1153 * has been initialized with psa_aead_decrypt_setup().
1154 *
Gilles Peskineed522972018-03-20 17:54:15 +01001155 * After a successful call to psa_aead_decrypt_setup(), the application must
1156 * eventually terminate the operation. The following events terminate an
1157 * operation:
Gilles Peskine3b555712018-03-03 21:27:57 +01001158 * - A failed call to psa_aead_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001159 * - A call to psa_aead_finish() or psa_aead_abort().
Gilles Peskine3b555712018-03-03 21:27:57 +01001160 *
1161 * \param operation
Gilles Peskine19067982018-03-20 17:54:53 +01001162 * \param alg The AEAD algorithm to compute (\c PSA_ALG_XXX value
1163 * such that #PSA_ALG_IS_AEAD(alg) is true).
Gilles Peskine3b555712018-03-03 21:27:57 +01001164 *
1165 * \retval PSA_SUCCESS
1166 * Success.
1167 * \retval PSA_ERROR_EMPTY_SLOT
1168 * \retval PSA_ERROR_NOT_PERMITTED
1169 * \retval PSA_ERROR_INVALID_ARGUMENT
1170 * \c key is not compatible with \c alg.
1171 * \retval PSA_ERROR_NOT_SUPPORTED
Gilles Peskine19067982018-03-20 17:54:53 +01001172 * \c alg is not supported or is not an AEAD algorithm.
Gilles Peskine3b555712018-03-03 21:27:57 +01001173 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1174 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1175 * \retval PSA_ERROR_HARDWARE_FAILURE
1176 * \retval PSA_ERROR_TAMPERING_DETECTED
1177 */
1178psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
1179 psa_key_slot_t key,
1180 psa_algorithm_t alg);
1181
1182psa_status_t psa_aead_generate_iv(psa_aead_operation_t *operation,
1183 unsigned char *iv,
1184 size_t iv_size,
1185 size_t *iv_length);
1186
1187psa_status_t psa_aead_set_iv(psa_aead_operation_t *operation,
1188 const unsigned char *iv,
1189 size_t iv_length);
1190
1191psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
1192 const uint8_t *input,
1193 size_t input_length);
1194
1195psa_status_t psa_aead_update(psa_aead_operation_t *operation,
1196 const uint8_t *input,
1197 size_t input_length);
1198
1199psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
1200 uint8_t *tag,
1201 size_t tag_size,
1202 size_t *tag_length);
1203
1204psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
1205 uint8_t *tag,
1206 size_t tag_length);
1207
1208psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
1209
1210/**@}*/
1211
Gilles Peskine20035e32018-02-03 22:44:14 +01001212/** \defgroup asymmetric Asymmetric cryptography
1213 * @{
1214 */
1215
1216/**
Gilles Peskine0189e752018-02-03 23:57:22 +01001217 * \brief Maximum ECDSA signature size for a given curve bit size
1218 *
1219 * \param curve_bits Curve size in bits
1220 * \return Maximum signature size in bytes
1221 *
1222 * \note This macro returns a compile-time constant if its argument is one.
1223 *
1224 * \warning This macro may evaluate its argument multiple times.
1225 */
1226/*
1227 * RFC 4492 page 20:
1228 *
1229 * Ecdsa-Sig-Value ::= SEQUENCE {
1230 * r INTEGER,
1231 * s INTEGER
1232 * }
1233 *
1234 * Size is at most
1235 * 1 (tag) + 1 (len) + 1 (initial 0) + curve_bytes for each of r and s,
1236 * twice that + 1 (tag) + 2 (len) for the sequence
1237 * (assuming curve_bytes is less than 126 for r and s,
1238 * and less than 124 (total len <= 255) for the sequence)
1239 */
1240#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
1241 ( /*T,L of SEQUENCE*/ ((curve_bits) >= 61 * 8 ? 3 : 2) + \
1242 /*T,L of r,s*/ 2 * (((curve_bits) >= 127 * 8 ? 3 : 2) + \
1243 /*V of r,s*/ ((curve_bits) + 8) / 8))
1244
1245
Gilles Peskine308b91d2018-02-08 09:47:44 +01001246/** Safe signature buffer size for psa_asymmetric_sign().
1247 *
1248 * This macro returns a safe buffer size for a signature using a key
1249 * of the specified type and size, with the specified algorithm.
1250 * Note that the actual size of the signature may be smaller
1251 * (some algorithms produce a variable-size signature).
1252 *
1253 * \warning This function may call its arguments multiple times or
1254 * zero times, so you should not pass arguments that contain
1255 * side effects.
1256 *
1257 * \param key_type An asymmetric key type (this may indifferently be a
1258 * key pair type or a public key type).
1259 * \param key_bits The size of the key in bits.
1260 * \param alg The signature algorithm.
1261 *
1262 * \return If the parameters are valid and supported, return
1263 * a buffer size in bytes that guarantees that
1264 * psa_asymmetric_sign() will not fail with
1265 * #PSA_ERROR_BUFFER_TOO_SMALL.
1266 * If the parameters are a valid combination that is not supported
1267 * by the implementation, this macro either shall return either a
1268 * sensible size or 0.
1269 * If the parameters are not valid, the
1270 * return value is unspecified.
1271 *
1272 */
Gilles Peskine0189e752018-02-03 23:57:22 +01001273#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine2905a7a2018-03-07 16:39:31 +01001274 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine0189e752018-02-03 23:57:22 +01001275 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
Gilles Peskine84845652018-03-28 14:17:40 +02001276 ((void)alg, 0))
Gilles Peskine0189e752018-02-03 23:57:22 +01001277
1278/**
Gilles Peskine20035e32018-02-03 22:44:14 +01001279 * \brief Sign a hash or short message with a private key.
1280 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001281 * \param key Key slot containing an asymmetric key pair.
1282 * \param alg A signature algorithm that is compatible with
1283 * the type of \c key.
1284 * \param hash The message to sign.
1285 * \param hash_length Size of the \c hash buffer in bytes.
1286 * \param salt A salt or label, if supported by the signature
1287 * algorithm.
1288 * If the signature algorithm does not support a
1289 * salt, pass \c NULL.
1290 * If the signature algorithm supports an optional
1291 * salt and you do not want to pass a salt,
1292 * pass \c NULL.
1293 * \param salt_length Size of the \c salt buffer in bytes.
1294 * If \c salt is \c NULL, pass 0.
1295 * \param signature Buffer where the signature is to be written.
1296 * \param signature_size Size of the \c signature buffer in bytes.
1297 * \param signature_length On success, the number of bytes
1298 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001299 *
1300 * \retval PSA_SUCCESS
1301 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1302 * The size of the \c signature buffer is too small. You can
1303 * determine a sufficient buffer size by calling
1304 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg)
1305 * where \c key_type and \c key_bits are the type and bit-size
1306 * respectively of \c key.
1307 * \retval PSA_ERROR_NOT_SUPPORTED
1308 * \retval PSA_ERROR_INVALID_ARGUMENT
1309 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1310 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1311 * \retval PSA_ERROR_HARDWARE_FAILURE
1312 * \retval PSA_ERROR_TAMPERING_DETECTED
1313 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01001314 */
1315psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
1316 psa_algorithm_t alg,
1317 const uint8_t *hash,
1318 size_t hash_length,
1319 const uint8_t *salt,
1320 size_t salt_length,
1321 uint8_t *signature,
1322 size_t signature_size,
1323 size_t *signature_length);
1324
1325/**
1326 * \brief Verify the signature a hash or short message using a public key.
1327 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001328 * \param key Key slot containing a public key or an
1329 * asymmetric key pair.
1330 * \param alg A signature algorithm that is compatible with
1331 * the type of \c key.
1332 * \param hash The message whose signature is to be verified.
1333 * \param hash_length Size of the \c hash buffer in bytes.
1334 * \param salt A salt or label, if supported by the signature
1335 * algorithm.
1336 * If the signature algorithm does not support a
1337 * salt, pass \c NULL.
1338 * If the signature algorithm supports an optional
1339 * salt and you do not want to pass a salt,
1340 * pass \c NULL.
1341 * \param salt_length Size of the \c salt buffer in bytes.
1342 * If \c salt is \c NULL, pass 0.
1343 * \param signature Buffer containing the signature to verify.
1344 * \param signature_size Size of the \c signature buffer in bytes.
1345 *
1346 * \retval PSA_SUCCESS
1347 * The signature is valid.
1348 * \retval PSA_ERROR_INVALID_SIGNATURE
1349 * The calculation was perfomed successfully, but the passed
1350 * signature is not a valid signature.
1351 * \retval PSA_ERROR_NOT_SUPPORTED
1352 * \retval PSA_ERROR_INVALID_ARGUMENT
1353 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1354 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1355 * \retval PSA_ERROR_HARDWARE_FAILURE
1356 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01001357 */
1358psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
1359 psa_algorithm_t alg,
1360 const uint8_t *hash,
1361 size_t hash_length,
1362 const uint8_t *salt,
1363 size_t salt_length,
1364 uint8_t *signature,
1365 size_t signature_size);
1366
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001367#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine06297932018-04-11 16:58:22 +02001368 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
1369 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
1370 0)
Gilles Peskine723feff2018-05-31 20:08:13 +02001371#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
1372 (PSA_ALG_IS_RSA_OAEP_MGF1(alg) ? \
1373 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_GET_HASH(alg)) + 1 : \
1374 11 /*PKCS#1v1.5*/)
1375#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine06297932018-04-11 16:58:22 +02001376 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
Gilles Peskine723feff2018-05-31 20:08:13 +02001377 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
Gilles Peskine06297932018-04-11 16:58:22 +02001378 0)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001379
1380/**
1381 * \brief Encrypt a short message with a public key.
1382 *
1383 * \param key Key slot containing a public key or an asymmetric
1384 * key pair.
1385 * \param alg An asymmetric encryption algorithm that is
1386 * compatible with the type of \c key.
1387 * \param input The message to encrypt.
1388 * \param input_length Size of the \c input buffer in bytes.
1389 * \param salt A salt or label, if supported by the encryption
1390 * algorithm.
1391 * If the algorithm does not support a
1392 * salt, pass \c NULL.
1393 * If the algorithm supports an optional
1394 * salt and you do not want to pass a salt,
1395 * pass \c NULL.
1396 *
1397 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1398 * supported.
1399 * \param salt_length Size of the \c salt buffer in bytes.
1400 * If \c salt is \c NULL, pass 0.
1401 * \param output Buffer where the encrypted message is to be written.
1402 * \param output_size Size of the \c output buffer in bytes.
1403 * \param output_length On success, the number of bytes
1404 * that make up the returned output.
1405 *
1406 * \retval PSA_SUCCESS
1407 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1408 * The size of the \c output buffer is too small. You can
1409 * determine a sufficient buffer size by calling
1410 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1411 * where \c key_type and \c key_bits are the type and bit-size
1412 * respectively of \c key.
1413 * \retval PSA_ERROR_NOT_SUPPORTED
1414 * \retval PSA_ERROR_INVALID_ARGUMENT
1415 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1416 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1417 * \retval PSA_ERROR_HARDWARE_FAILURE
1418 * \retval PSA_ERROR_TAMPERING_DETECTED
1419 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1420 */
1421psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
1422 psa_algorithm_t alg,
1423 const uint8_t *input,
1424 size_t input_length,
1425 const uint8_t *salt,
1426 size_t salt_length,
1427 uint8_t *output,
1428 size_t output_size,
1429 size_t *output_length);
1430
1431/**
1432 * \brief Decrypt a short message with a private key.
1433 *
1434 * \param key Key slot containing an asymmetric key pair.
1435 * \param alg An asymmetric encryption algorithm that is
1436 * compatible with the type of \c key.
1437 * \param input The message to decrypt.
1438 * \param input_length Size of the \c input buffer in bytes.
1439 * \param salt A salt or label, if supported by the encryption
1440 * algorithm.
1441 * If the algorithm does not support a
1442 * salt, pass \c NULL.
1443 * If the algorithm supports an optional
1444 * salt and you do not want to pass a salt,
1445 * pass \c NULL.
1446 *
1447 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1448 * supported.
1449 * \param salt_length Size of the \c salt buffer in bytes.
1450 * If \c salt is \c NULL, pass 0.
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001451 * \param output Buffer where the decrypted message is to be written.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001452 * \param output_size Size of the \c output buffer in bytes.
1453 * \param output_length On success, the number of bytes
1454 * that make up the returned output.
1455 *
1456 * \retval PSA_SUCCESS
1457 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1458 * The size of the \c output buffer is too small. You can
1459 * determine a sufficient buffer size by calling
1460 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1461 * where \c key_type and \c key_bits are the type and bit-size
1462 * respectively of \c key.
1463 * \retval PSA_ERROR_NOT_SUPPORTED
1464 * \retval PSA_ERROR_INVALID_ARGUMENT
1465 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1466 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1467 * \retval PSA_ERROR_HARDWARE_FAILURE
1468 * \retval PSA_ERROR_TAMPERING_DETECTED
1469 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1470 * \retval PSA_ERROR_INVALID_PADDING
1471 */
1472psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
1473 psa_algorithm_t alg,
1474 const uint8_t *input,
1475 size_t input_length,
1476 const uint8_t *salt,
1477 size_t salt_length,
1478 uint8_t *output,
1479 size_t output_size,
1480 size_t *output_length);
1481
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001482/**@}*/
1483
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001484/** \defgroup generation Key generation
1485 * @{
1486 */
1487
1488/**
1489 * \brief Generate random bytes.
1490 *
1491 * \warning This function **can** fail! Callers MUST check the return status
1492 * and MUST NOT use the content of the output buffer if the return
1493 * status is not #PSA_SUCCESS.
1494 *
1495 * \note To generate a key, use psa_generate_key() instead.
1496 *
1497 * \param output Output buffer for the generated data.
1498 * \param output_size Number of bytes to generate and output.
1499 *
1500 * \retval PSA_SUCCESS
1501 * \retval PSA_ERROR_NOT_SUPPORTED
1502 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1503 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1504 * \retval PSA_ERROR_HARDWARE_FAILURE
1505 * \retval PSA_ERROR_TAMPERING_DETECTED
1506 */
1507psa_status_t psa_generate_random(uint8_t *output,
1508 size_t output_size);
1509
1510/**
1511 * \brief Generate a key or key pair.
1512 *
1513 * \param key Slot where the key will be stored. This must be a
1514 * valid slot for a key of the chosen type. It must
1515 * be unoccupied.
1516 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
1517 * \param bits Key size in bits.
1518 * \param parameters Extra parameters for key generation. The interpretation
1519 * of this parameter depends on \c type. All types support
1520 * \c NULL to use default parameters specified below.
1521 *
1522 * For any symmetric key type (type such that
1523 * `PSA_KEY_TYPE_IS_ASYMMETRIC(type)` is false), \c parameters must be
1524 * \c NULL. For asymmetric key types defined by this specification,
1525 * the parameter type and the default parameters are defined by the
1526 * table below. For vendor-defined key types, the vendor documentation
1527 * shall define the parameter type and the default parameters.
1528 *
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001529 * Type | Parameter type | Meaning | Parameters used if `parameters == NULL`
1530 * ---- | -------------- | ------- | ---------------------------------------
1531 * `PSA_KEY_TYPE_RSA_KEYPAIR` | `unsigned int` | Public exponent | 65537
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001532 *
1533 * \retval PSA_SUCCESS
1534 * \retval PSA_ERROR_NOT_SUPPORTED
1535 * \retval PSA_ERROR_INVALID_ARGUMENT
1536 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1537 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1538 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1539 * \retval PSA_ERROR_HARDWARE_FAILURE
1540 * \retval PSA_ERROR_TAMPERING_DETECTED
1541 */
1542psa_status_t psa_generate_key(psa_key_slot_t key,
1543 psa_key_type_t type,
1544 size_t bits,
1545 const void *parameters);
1546
1547/**@}*/
1548
Gilles Peskinee59236f2018-01-27 23:32:46 +01001549#ifdef __cplusplus
1550}
1551#endif
1552
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001553/* The file "crypto_struct.h" contains definitions for
1554 * implementation-specific structs that are declared above. */
1555#include "crypto_struct.h"
1556
1557/* The file "crypto_extra.h" contains vendor-specific definitions. This
1558 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01001559#include "crypto_extra.h"
1560
1561#endif /* PSA_CRYPTO_H */