blob: 7fc14a222c7b0d4564214d9de15b2fc42a01de44 [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) \
339 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_MGF1_RAW)
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 */
Gilles Peskine3b555712018-03-03 21:27:57 +01001075/** Set the key for a multipart authenticated encryption operation.
1076 *
1077 * The sequence of operations to authenticate-and-encrypt a message
1078 * is as follows:
1079 * -# Allocate an operation object which will be passed to all the functions
1080 * listed here.
1081 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
1082 * The key remains associated with the operation even if the content
1083 * of the key slot changes.
1084 * -# Call either psa_aead_generate_iv() or psa_aead_set_iv() to
1085 * generate or set the IV (initialization vector). You should use
1086 * psa_encrypt_generate_iv() unless the protocol you are implementing
1087 * requires a specific IV value.
1088 * -# Call psa_aead_update_ad() to pass the associated data that is
1089 * to be authenticated but not encrypted. You may omit this step if
1090 * there is no associated data.
1091 * -# Call psa_aead_update() zero, one or more times, passing a fragment
1092 * of the data to encrypt each time.
1093 * -# Call psa_aead_finish().
1094 *
1095 * The application may call psa_aead_abort() at any time after the operation
1096 * has been initialized with psa_aead_encrypt_setup().
1097 *
Gilles Peskineed522972018-03-20 17:54:15 +01001098 * After a successful call to psa_aead_encrypt_setup(), the application must
1099 * eventually terminate the operation. The following events terminate an
1100 * operation:
Gilles Peskine3b555712018-03-03 21:27:57 +01001101 * - A failed call to psa_aead_generate_iv(), psa_aead_set_iv(),
1102 * psa_aead_update_ad() or psa_aead_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001103 * - A call to psa_aead_finish() or psa_aead_abort().
Gilles Peskine3b555712018-03-03 21:27:57 +01001104 *
1105 * \param operation
1106 * \param alg The AEAD algorithm to compute (\c PSA_ALG_XXX value
1107 * such that #PSA_ALG_IS_AEAD(alg) is true).
1108 *
1109 * \retval PSA_SUCCESS
1110 * Success.
1111 * \retval PSA_ERROR_EMPTY_SLOT
1112 * \retval PSA_ERROR_NOT_PERMITTED
1113 * \retval PSA_ERROR_INVALID_ARGUMENT
1114 * \c key is not compatible with \c alg.
1115 * \retval PSA_ERROR_NOT_SUPPORTED
1116 * \c alg is not supported or is not an AEAD algorithm.
1117 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1118 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1119 * \retval PSA_ERROR_HARDWARE_FAILURE
1120 * \retval PSA_ERROR_TAMPERING_DETECTED
1121 */
1122psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
1123 psa_key_slot_t key,
1124 psa_algorithm_t alg);
1125
mohammad160339ee8712018-04-26 00:51:02 +03001126/** Process an integrated authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001127 *
1128 * \param operation
Gilles Peskine19067982018-03-20 17:54:53 +01001129 * \param alg The AEAD algorithm to compute (\c PSA_ALG_XXX value
1130 * such that #PSA_ALG_IS_AEAD(alg) is true).
Gilles Peskine3b555712018-03-03 21:27:57 +01001131 *
1132 * \retval PSA_SUCCESS
1133 * Success.
1134 * \retval PSA_ERROR_EMPTY_SLOT
1135 * \retval PSA_ERROR_NOT_PERMITTED
1136 * \retval PSA_ERROR_INVALID_ARGUMENT
1137 * \c key is not compatible with \c alg.
1138 * \retval PSA_ERROR_NOT_SUPPORTED
Gilles Peskine19067982018-03-20 17:54:53 +01001139 * \c alg is not supported or is not an AEAD algorithm.
Gilles Peskine3b555712018-03-03 21:27:57 +01001140 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1141 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1142 * \retval PSA_ERROR_HARDWARE_FAILURE
1143 * \retval PSA_ERROR_TAMPERING_DETECTED
1144 */
mohammad160339ee8712018-04-26 00:51:02 +03001145psa_status_t psa_aead_encrypt( psa_key_slot_t key,
1146 psa_algorithm_t alg,
1147 const uint8_t *nonce,
1148 size_t nonce_length,
1149 const uint8_t *additional_data,
1150 size_t additional_data_length,
1151 const uint8_t *plaintext,
1152 size_t plaintext_length,
1153 uint8_t *ciphertext,
1154 size_t ciphertext_size,
1155 size_t *ciphertext_length );
Gilles Peskine3b555712018-03-03 21:27:57 +01001156
mohammad160339ee8712018-04-26 00:51:02 +03001157psa_status_t psa_aead_decrypt( psa_key_slot_t key,
1158 psa_algorithm_t alg,
1159 const uint8_t *nonce,
1160 size_t nonce_length,
1161 const uint8_t *additional_data,
1162 size_t additional_data_length,
1163 const uint8_t *ciphertext,
1164 size_t ciphertext_length,
1165 uint8_t *plaintext,
1166 size_t plaintext_size,
1167 size_t *plaintext_length );
Gilles Peskine3b555712018-03-03 21:27:57 +01001168
1169/**@}*/
1170
Gilles Peskine20035e32018-02-03 22:44:14 +01001171/** \defgroup asymmetric Asymmetric cryptography
1172 * @{
1173 */
1174
1175/**
Gilles Peskine0189e752018-02-03 23:57:22 +01001176 * \brief Maximum ECDSA signature size for a given curve bit size
1177 *
1178 * \param curve_bits Curve size in bits
1179 * \return Maximum signature size in bytes
1180 *
1181 * \note This macro returns a compile-time constant if its argument is one.
1182 *
1183 * \warning This macro may evaluate its argument multiple times.
1184 */
1185/*
1186 * RFC 4492 page 20:
1187 *
1188 * Ecdsa-Sig-Value ::= SEQUENCE {
1189 * r INTEGER,
1190 * s INTEGER
1191 * }
1192 *
1193 * Size is at most
1194 * 1 (tag) + 1 (len) + 1 (initial 0) + curve_bytes for each of r and s,
1195 * twice that + 1 (tag) + 2 (len) for the sequence
1196 * (assuming curve_bytes is less than 126 for r and s,
1197 * and less than 124 (total len <= 255) for the sequence)
1198 */
1199#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
1200 ( /*T,L of SEQUENCE*/ ((curve_bits) >= 61 * 8 ? 3 : 2) + \
1201 /*T,L of r,s*/ 2 * (((curve_bits) >= 127 * 8 ? 3 : 2) + \
1202 /*V of r,s*/ ((curve_bits) + 8) / 8))
1203
1204
Gilles Peskine308b91d2018-02-08 09:47:44 +01001205/** Safe signature buffer size for psa_asymmetric_sign().
1206 *
1207 * This macro returns a safe buffer size for a signature using a key
1208 * of the specified type and size, with the specified algorithm.
1209 * Note that the actual size of the signature may be smaller
1210 * (some algorithms produce a variable-size signature).
1211 *
1212 * \warning This function may call its arguments multiple times or
1213 * zero times, so you should not pass arguments that contain
1214 * side effects.
1215 *
1216 * \param key_type An asymmetric key type (this may indifferently be a
1217 * key pair type or a public key type).
1218 * \param key_bits The size of the key in bits.
1219 * \param alg The signature algorithm.
1220 *
1221 * \return If the parameters are valid and supported, return
1222 * a buffer size in bytes that guarantees that
1223 * psa_asymmetric_sign() will not fail with
1224 * #PSA_ERROR_BUFFER_TOO_SMALL.
1225 * If the parameters are a valid combination that is not supported
1226 * by the implementation, this macro either shall return either a
1227 * sensible size or 0.
1228 * If the parameters are not valid, the
1229 * return value is unspecified.
1230 *
1231 */
Gilles Peskine0189e752018-02-03 23:57:22 +01001232#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine2905a7a2018-03-07 16:39:31 +01001233 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine0189e752018-02-03 23:57:22 +01001234 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
Gilles Peskine84845652018-03-28 14:17:40 +02001235 ((void)alg, 0))
Gilles Peskine0189e752018-02-03 23:57:22 +01001236
1237/**
Gilles Peskine20035e32018-02-03 22:44:14 +01001238 * \brief Sign a hash or short message with a private key.
1239 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001240 * \param key Key slot containing an asymmetric key pair.
1241 * \param alg A signature algorithm that is compatible with
1242 * the type of \c key.
1243 * \param hash The message to sign.
1244 * \param hash_length Size of the \c hash buffer in bytes.
1245 * \param salt A salt or label, if supported by the signature
1246 * algorithm.
1247 * If the signature algorithm does not support a
1248 * salt, pass \c NULL.
1249 * If the signature algorithm supports an optional
1250 * salt and you do not want to pass a salt,
1251 * pass \c NULL.
1252 * \param salt_length Size of the \c salt buffer in bytes.
1253 * If \c salt is \c NULL, pass 0.
1254 * \param signature Buffer where the signature is to be written.
1255 * \param signature_size Size of the \c signature buffer in bytes.
1256 * \param signature_length On success, the number of bytes
1257 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001258 *
1259 * \retval PSA_SUCCESS
1260 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1261 * The size of the \c signature buffer is too small. You can
1262 * determine a sufficient buffer size by calling
1263 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg)
1264 * where \c key_type and \c key_bits are the type and bit-size
1265 * respectively of \c key.
1266 * \retval PSA_ERROR_NOT_SUPPORTED
1267 * \retval PSA_ERROR_INVALID_ARGUMENT
1268 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1269 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1270 * \retval PSA_ERROR_HARDWARE_FAILURE
1271 * \retval PSA_ERROR_TAMPERING_DETECTED
1272 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01001273 */
1274psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
1275 psa_algorithm_t alg,
1276 const uint8_t *hash,
1277 size_t hash_length,
1278 const uint8_t *salt,
1279 size_t salt_length,
1280 uint8_t *signature,
1281 size_t signature_size,
1282 size_t *signature_length);
1283
1284/**
1285 * \brief Verify the signature a hash or short message using a public key.
1286 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001287 * \param key Key slot containing a public key or an
1288 * asymmetric key pair.
1289 * \param alg A signature algorithm that is compatible with
1290 * the type of \c key.
1291 * \param hash The message whose signature is to be verified.
1292 * \param hash_length Size of the \c hash buffer in bytes.
1293 * \param salt A salt or label, if supported by the signature
1294 * algorithm.
1295 * If the signature algorithm does not support a
1296 * salt, pass \c NULL.
1297 * If the signature algorithm supports an optional
1298 * salt and you do not want to pass a salt,
1299 * pass \c NULL.
1300 * \param salt_length Size of the \c salt buffer in bytes.
1301 * If \c salt is \c NULL, pass 0.
1302 * \param signature Buffer containing the signature to verify.
1303 * \param signature_size Size of the \c signature buffer in bytes.
1304 *
1305 * \retval PSA_SUCCESS
1306 * The signature is valid.
1307 * \retval PSA_ERROR_INVALID_SIGNATURE
1308 * The calculation was perfomed successfully, but the passed
1309 * signature is not a valid signature.
1310 * \retval PSA_ERROR_NOT_SUPPORTED
1311 * \retval PSA_ERROR_INVALID_ARGUMENT
1312 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1313 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1314 * \retval PSA_ERROR_HARDWARE_FAILURE
1315 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01001316 */
1317psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
1318 psa_algorithm_t alg,
1319 const uint8_t *hash,
1320 size_t hash_length,
1321 const uint8_t *salt,
1322 size_t salt_length,
1323 uint8_t *signature,
1324 size_t signature_size);
1325
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001326#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine06297932018-04-11 16:58:22 +02001327 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
1328 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
1329 0)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001330#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine06297932018-04-11 16:58:22 +02001331 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
1332 PSA_BITS_TO_BYTES(key_bits) - ((alg) == PSA_ALG_IS_RSA_OAEP_MGF1 ? \
1333 2 * (PSA_ALG_RSA_GET_HASH(alg) + 1) : \
1334 11 /*PKCS#1v1.5*/) : \
1335 0)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001336
1337/**
1338 * \brief Encrypt a short message with a public key.
1339 *
1340 * \param key Key slot containing a public key or an asymmetric
1341 * key pair.
1342 * \param alg An asymmetric encryption algorithm that is
1343 * compatible with the type of \c key.
1344 * \param input The message to encrypt.
1345 * \param input_length Size of the \c input buffer in bytes.
1346 * \param salt A salt or label, if supported by the encryption
1347 * algorithm.
1348 * If the algorithm does not support a
1349 * salt, pass \c NULL.
1350 * If the algorithm supports an optional
1351 * salt and you do not want to pass a salt,
1352 * pass \c NULL.
1353 *
1354 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1355 * supported.
1356 * \param salt_length Size of the \c salt buffer in bytes.
1357 * If \c salt is \c NULL, pass 0.
1358 * \param output Buffer where the encrypted message is to be written.
1359 * \param output_size Size of the \c output buffer in bytes.
1360 * \param output_length On success, the number of bytes
1361 * that make up the returned output.
1362 *
1363 * \retval PSA_SUCCESS
1364 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1365 * The size of the \c output buffer is too small. You can
1366 * determine a sufficient buffer size by calling
1367 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1368 * where \c key_type and \c key_bits are the type and bit-size
1369 * respectively of \c key.
1370 * \retval PSA_ERROR_NOT_SUPPORTED
1371 * \retval PSA_ERROR_INVALID_ARGUMENT
1372 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1373 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1374 * \retval PSA_ERROR_HARDWARE_FAILURE
1375 * \retval PSA_ERROR_TAMPERING_DETECTED
1376 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1377 */
1378psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
1379 psa_algorithm_t alg,
1380 const uint8_t *input,
1381 size_t input_length,
1382 const uint8_t *salt,
1383 size_t salt_length,
1384 uint8_t *output,
1385 size_t output_size,
1386 size_t *output_length);
1387
1388/**
1389 * \brief Decrypt a short message with a private key.
1390 *
1391 * \param key Key slot containing an asymmetric key pair.
1392 * \param alg An asymmetric encryption algorithm that is
1393 * compatible with the type of \c key.
1394 * \param input The message to decrypt.
1395 * \param input_length Size of the \c input buffer in bytes.
1396 * \param salt A salt or label, if supported by the encryption
1397 * algorithm.
1398 * If the algorithm does not support a
1399 * salt, pass \c NULL.
1400 * If the algorithm supports an optional
1401 * salt and you do not want to pass a salt,
1402 * pass \c NULL.
1403 *
1404 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1405 * supported.
1406 * \param salt_length Size of the \c salt buffer in bytes.
1407 * If \c salt is \c NULL, pass 0.
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001408 * \param output Buffer where the decrypted message is to be written.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001409 * \param output_size Size of the \c output buffer in bytes.
1410 * \param output_length On success, the number of bytes
1411 * that make up the returned output.
1412 *
1413 * \retval PSA_SUCCESS
1414 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1415 * The size of the \c output buffer is too small. You can
1416 * determine a sufficient buffer size by calling
1417 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1418 * where \c key_type and \c key_bits are the type and bit-size
1419 * respectively of \c key.
1420 * \retval PSA_ERROR_NOT_SUPPORTED
1421 * \retval PSA_ERROR_INVALID_ARGUMENT
1422 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1423 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1424 * \retval PSA_ERROR_HARDWARE_FAILURE
1425 * \retval PSA_ERROR_TAMPERING_DETECTED
1426 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1427 * \retval PSA_ERROR_INVALID_PADDING
1428 */
1429psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
1430 psa_algorithm_t alg,
1431 const uint8_t *input,
1432 size_t input_length,
1433 const uint8_t *salt,
1434 size_t salt_length,
1435 uint8_t *output,
1436 size_t output_size,
1437 size_t *output_length);
1438
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001439/**@}*/
1440
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001441/** \defgroup generation Key generation
1442 * @{
1443 */
1444
1445/**
1446 * \brief Generate random bytes.
1447 *
1448 * \warning This function **can** fail! Callers MUST check the return status
1449 * and MUST NOT use the content of the output buffer if the return
1450 * status is not #PSA_SUCCESS.
1451 *
1452 * \note To generate a key, use psa_generate_key() instead.
1453 *
1454 * \param output Output buffer for the generated data.
1455 * \param output_size Number of bytes to generate and output.
1456 *
1457 * \retval PSA_SUCCESS
1458 * \retval PSA_ERROR_NOT_SUPPORTED
1459 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1460 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1461 * \retval PSA_ERROR_HARDWARE_FAILURE
1462 * \retval PSA_ERROR_TAMPERING_DETECTED
1463 */
1464psa_status_t psa_generate_random(uint8_t *output,
1465 size_t output_size);
1466
1467/**
1468 * \brief Generate a key or key pair.
1469 *
1470 * \param key Slot where the key will be stored. This must be a
1471 * valid slot for a key of the chosen type. It must
1472 * be unoccupied.
1473 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
1474 * \param bits Key size in bits.
1475 * \param parameters Extra parameters for key generation. The interpretation
1476 * of this parameter depends on \c type. All types support
1477 * \c NULL to use default parameters specified below.
1478 *
1479 * For any symmetric key type (type such that
1480 * `PSA_KEY_TYPE_IS_ASYMMETRIC(type)` is false), \c parameters must be
1481 * \c NULL. For asymmetric key types defined by this specification,
1482 * the parameter type and the default parameters are defined by the
1483 * table below. For vendor-defined key types, the vendor documentation
1484 * shall define the parameter type and the default parameters.
1485 *
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001486 * Type | Parameter type | Meaning | Parameters used if `parameters == NULL`
1487 * ---- | -------------- | ------- | ---------------------------------------
1488 * `PSA_KEY_TYPE_RSA_KEYPAIR` | `unsigned int` | Public exponent | 65537
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001489 *
1490 * \retval PSA_SUCCESS
1491 * \retval PSA_ERROR_NOT_SUPPORTED
1492 * \retval PSA_ERROR_INVALID_ARGUMENT
1493 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1494 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1495 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1496 * \retval PSA_ERROR_HARDWARE_FAILURE
1497 * \retval PSA_ERROR_TAMPERING_DETECTED
1498 */
1499psa_status_t psa_generate_key(psa_key_slot_t key,
1500 psa_key_type_t type,
1501 size_t bits,
1502 const void *parameters);
1503
1504/**@}*/
1505
Gilles Peskinee59236f2018-01-27 23:32:46 +01001506#ifdef __cplusplus
1507}
1508#endif
1509
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001510/* The file "crypto_struct.h" contains definitions for
1511 * implementation-specific structs that are declared above. */
1512#include "crypto_struct.h"
1513
1514/* The file "crypto_extra.h" contains vendor-specific definitions. This
1515 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01001516#include "crypto_extra.h"
1517
1518#endif /* PSA_CRYPTO_H */