blob: f5db4d26bc18945a8f144c88e6a53a22d574291a [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
5
6#ifndef PSA_CRYPTO_H
7#define PSA_CRYPTO_H
8
9#include "crypto_platform.h"
10
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010011#include <stddef.h>
12
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010013#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010014/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
15 * must be defined in the crypto_platform.h header. These mock definitions
16 * are present in this file as a convenience to generate pretty-printed
17 * documentation that includes those definitions. */
18
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010019/** \defgroup platform Implementation-specific definitions
20 * @{
21 */
22
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010023/** \brief Key slot number.
24 *
25 * This type represents key slots. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010026 * type. The choice of type is implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027 * 0 is not a valid key slot number. The meaning of other values is
28 * implementation dependent.
29 *
30 * At any given point in time, each key slot either contains a
31 * cryptographic object, or is empty. Key slots are persistent:
32 * once set, the cryptographic object remains in the key slot until
33 * explicitly destroyed.
34 */
35typedef _unsigned_integral_type_ psa_key_slot_t;
36
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010037/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010038#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010039
Gilles Peskinee59236f2018-01-27 23:32:46 +010040#ifdef __cplusplus
41extern "C" {
42#endif
43
44/** \defgroup basic Basic definitions
45 * @{
46 */
47
48/**
49 * \brief Function return status.
50 *
51 * Zero indicates success, anything else indicates an error.
52 */
53typedef enum {
54 /** The action was completed successfully. */
55 PSA_SUCCESS = 0,
56 /** The requested operation or a parameter is not supported
57 by this implementation. */
58 PSA_ERROR_NOT_SUPPORTED,
59 /** The requested action is denied by a policy. */
60 PSA_ERROR_NOT_PERMITTED,
61 /** An output buffer is too small. */
62 PSA_ERROR_BUFFER_TOO_SMALL,
63 /** A slot is occupied, but must be empty to carry out the
64 requested action. */
65 PSA_ERROR_OCCUPIED_SLOT,
66 /** A slot is empty, but must be occupied to carry out the
67 requested action. */
68 PSA_ERROR_EMPTY_SLOT,
69 /** The requested action cannot be performed in the current state. */
70 PSA_ERROR_BAD_STATE,
71 /** The parameters passed to the function are invalid. */
72 PSA_ERROR_INVALID_ARGUMENT,
73 /** There is not enough runtime memory. */
74 PSA_ERROR_INSUFFICIENT_MEMORY,
75 /** There is not enough persistent storage. */
76 PSA_ERROR_INSUFFICIENT_STORAGE,
77 /** There was a communication failure inside the implementation. */
78 PSA_ERROR_COMMUNICATION_FAILURE,
Gilles Peskinea5905292018-02-07 20:59:33 +010079 /** There was a storage failure that may have led to data loss. */
80 PSA_ERROR_STORAGE_FAILURE,
Gilles Peskinee59236f2018-01-27 23:32:46 +010081 /** A hardware failure was detected. */
82 PSA_ERROR_HARDWARE_FAILURE,
83 /** A tampering attempt was detected. */
84 PSA_ERROR_TAMPERING_DETECTED,
85 /** There is not enough entropy to generate random data needed
86 for the requested action. */
87 PSA_ERROR_INSUFFICIENT_ENTROPY,
Gilles Peskinea5905292018-02-07 20:59:33 +010088 /** The signature, MAC or hash is incorrect. */
Gilles Peskinee59236f2018-01-27 23:32:46 +010089 PSA_ERROR_INVALID_SIGNATURE,
Gilles Peskinea5905292018-02-07 20:59:33 +010090 /** The decrypted padding is incorrect. */
91 PSA_ERROR_INVALID_PADDING,
Gilles Peskinee59236f2018-01-27 23:32:46 +010092 /** An error occurred that does not correspond to any defined
93 failure cause. */
94 PSA_ERROR_UNKNOWN_ERROR,
95} psa_status_t;
96
97/**
98 * \brief Library initialization.
99 *
100 * Applications must call this function before calling any other
101 * function in this module.
102 *
103 * Applications may call this function more than once. Once a call
104 * succeeds, subsequent calls are guaranteed to succeed.
105 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100106 * \retval PSA_SUCCESS
107 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
108 * \retval PSA_ERROR_COMMUNICATION_FAILURE
109 * \retval PSA_ERROR_HARDWARE_FAILURE
110 * \retval PSA_ERROR_TAMPERING_DETECTED
111 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +0100112 */
113psa_status_t psa_crypto_init(void);
114
Gilles Peskine2905a7a2018-03-07 16:39:31 +0100115#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
116#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
Gilles Peskine0189e752018-02-03 23:57:22 +0100117
Gilles Peskinee59236f2018-01-27 23:32:46 +0100118/**@}*/
119
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100120/** \defgroup crypto_types Key and algorithm types
121 * @{
122 */
123
Gilles Peskine308b91d2018-02-08 09:47:44 +0100124/** \brief Encoding of a key type.
125 */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100126typedef uint32_t psa_key_type_t;
127
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100128/** An invalid key type value.
129 *
130 * Zero is not the encoding of any key type.
131 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100132#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100133
134/** Vendor-defined flag
135 *
136 * Key types defined by this standard will never have the
137 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
138 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
139 * respect the bitwise structure used by standard encodings whenever practical.
140 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100141#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100142
Gilles Peskine98f0a242018-02-06 18:57:29 +0100143#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7e000000)
144#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x02000000)
145#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x04000000)
146#define PSA_KEY_TYPE_CATEGORY_ASYMMETRIC ((psa_key_type_t)0x06000000)
147#define PSA_KEY_TYPE_PAIR_FLAG ((psa_key_type_t)0x01000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100148
Gilles Peskine98f0a242018-02-06 18:57:29 +0100149#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x02000001)
150#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x04000001)
151#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x04000002)
152#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x04000003)
153#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x04000004)
154
Gilles Peskine308b91d2018-02-08 09:47:44 +0100155/** RSA public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100156#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x06010000)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100157/** RSA key pair (private and public key). */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100158#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x07010000)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100159/** DSA public key. */
160#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x06020000)
161/** DSA key pair (private and public key). */
162#define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x07020000)
163#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x06030000)
164#define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x07030000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100165#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100166#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
167 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
168#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
169 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
Gilles Peskine98f0a242018-02-06 18:57:29 +0100170
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100171/** Whether a key type is vendor-defined. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100172#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100173 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100174#define PSA_KEY_TYPE_IS_RAW_BYTES(type) \
175 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_RAW_DATA || \
176 ((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100177
178/** Whether a key type is asymmetric: either a key pair or a public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100179#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
180 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100181/** Whether a key type is the public part of a key pair. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100182#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
183 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG) == \
184 PSA_KEY_TYPE_CATEGORY_ASYMMETRIC))
Gilles Peskine06dc2632018-03-08 07:47:25 +0100185/** Whether a key type is a key pair containing a private part and a public
186 * part. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100187#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
188 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
189 (PSA_KEY_TYPE_CATEGORY_ASYMMETRIC | PSA_KEY_TYPE_PAIR_FLAG))
Gilles Peskine06dc2632018-03-08 07:47:25 +0100190/** Whether a key type is an RSA key pair or public key. */
191/** The key pair type corresponding to a public key type. */
192#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
193 ((type) | PSA_KEY_TYPE_PAIR_FLAG)
194/** The public key type corresponding to a key pair type. */
195#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
196 ((type) & ~PSA_KEY_TYPE_PAIR_FLAG)
Gilles Peskine0189e752018-02-03 23:57:22 +0100197#define PSA_KEY_TYPE_IS_RSA(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100198 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
199/** Whether a key type is an elliptic curve key pair or public key. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100200#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100201 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
202 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100203
Gilles Peskine7e198532018-03-08 07:50:30 +0100204/** The block size of a block cipher.
205 *
206 * \param type A cipher key type (value of type #psa_key_type_t).
207 *
208 * \return The block size for a block cipher, or 1 for a stream cipher.
209 * The return value is undefined if \c type does not identify
210 * a cipher algorithm.
211 *
212 * \note This macro returns a compile-time constant if its argument is one.
213 *
214 * \warning This macro may evaluate its argument multiple times.
215 */
Gilles Peskine03182e92018-03-07 16:40:52 +0100216#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100217 ( \
218 (type) == PSA_KEY_TYPE_AES ? 16 : \
219 (type) == PSA_KEY_TYPE_DES ? 8 : \
220 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
Gilles Peskine7e198532018-03-08 07:50:30 +0100221 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100222 0)
223
Gilles Peskine308b91d2018-02-08 09:47:44 +0100224/** \brief Encoding of a cryptographic algorithm.
225 *
226 * For algorithms that can be applied to multiple key types, this type
227 * does not encode the key type. For example, for symmetric ciphers
228 * based on a block cipher, #psa_algorithm_t encodes the block cipher
229 * mode and the padding mode while the block cipher itself is encoded
230 * via #psa_key_type_t.
231 */
Gilles Peskine20035e32018-02-03 22:44:14 +0100232typedef uint32_t psa_algorithm_t;
233
Gilles Peskine98f0a242018-02-06 18:57:29 +0100234#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
235#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
236#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
237#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
238#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
239#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
240#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
241#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
242#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
243#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100244
Gilles Peskine98f0a242018-02-06 18:57:29 +0100245#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
246 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100247/** Whether the specified algorithm is a hash algorithm.
248 *
Gilles Peskine7e198532018-03-08 07:50:30 +0100249 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100250 *
251 * \return 1 if \c alg is a hash algorithm, 0 otherwise.
252 * This macro may return either 0 or 1 if \c alg is not a valid
Gilles Peskine7e198532018-03-08 07:50:30 +0100253 * algorithm identifier.
254 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100255#define PSA_ALG_IS_HASH(alg) \
256 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
257#define PSA_ALG_IS_MAC(alg) \
258 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
259#define PSA_ALG_IS_CIPHER(alg) \
260 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
261#define PSA_ALG_IS_AEAD(alg) \
262 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
263#define PSA_ALG_IS_SIGN(alg) \
264 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
265#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
266 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
267#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
268 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
269#define PSA_ALG_IS_KEY_DERIVATION(alg) \
270 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
271
272#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
273#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
274#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
275#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
Gilles Peskinee3f694f2018-03-08 07:48:40 +0100276#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
277#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100278#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
279#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
280#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
281#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
282#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
283#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
284#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
285#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
286#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
287#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
288
Gilles Peskine8c9def32018-02-08 10:02:12 +0100289#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100290#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
291#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100292 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
293#define PSA_ALG_HMAC_HASH(hmac_alg) \
294 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
295#define PSA_ALG_IS_HMAC(alg) \
296 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
297 PSA_ALG_HMAC_BASE)
298#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
299#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
300#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
301#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
302#define PSA_ALG_IS_CIPHER_MAC(alg) \
303 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
304 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100305
Gilles Peskine8c9def32018-02-08 10:02:12 +0100306#define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100307#define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100308#define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100309#define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000)
310#define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100311#define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100312#define PSA_ALG_IS_BLOCK_CIPHER(alg) \
313 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
314 PSA_ALG_BLOCK_CIPHER_BASE)
315
Gilles Peskine98f0a242018-02-06 18:57:29 +0100316#define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100317#define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002)
318#define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003)
319#define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100320#define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t)0x04800000)
321#define PSA_ALG_CTR ((psa_algorithm_t)0x04800001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100322#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100323
Gilles Peskine8c9def32018-02-08 10:02:12 +0100324#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
325#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100326
Gilles Peskinea5926232018-03-28 14:16:50 +0200327#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW ((psa_algorithm_t)0x10010000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100328#define PSA_ALG_RSA_PSS_MGF1 ((psa_algorithm_t)0x10020000)
Gilles Peskine6944f9a2018-03-28 14:18:39 +0200329#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12010000)
330#define PSA_ALG_RSA_OAEP_MGF1_BASE ((psa_algorithm_t)0x12020000)
Gilles Peskinea5926232018-03-28 14:16:50 +0200331#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
332 (PSA_ALG_RSA_PKCS1V15_SIGN_RAW | ((hash_alg) & PSA_ALG_HASH_MASK))
333#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
334 (((alg) & 0x7fffff00) == PSA_ALG_RSA_PKCS1V15_SIGN_RAW)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100335#define PSA_ALG_RSA_GET_HASH(alg) \
336 (((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100337
338/**@}*/
339
340/** \defgroup key_management Key management
341 * @{
342 */
343
344/**
345 * \brief Import a key in binary format.
346 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100347 * This function supports any output from psa_export_key(). Refer to the
348 * documentation of psa_export_key() for the format for each key type.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100349 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100350 * \param key Slot where the key will be stored. This must be a
351 * valid slot for a key of the chosen type. It must
352 * be unoccupied.
353 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
354 * \param data Buffer containing the key data.
355 * \param data_length Size of the \c data buffer in bytes.
356 *
357 * \retval PSA_SUCCESS
358 * Success.
359 * \retval PSA_ERROR_NOT_SUPPORTED
360 * The key type or key size is not supported.
361 * \retval PSA_ERROR_INVALID_ARGUMENT
362 * The key slot is invalid,
363 * or the key data is not correctly formatted.
364 * \retval PSA_ERROR_OCCUPIED_SLOT
365 There is already a key in the specified slot.
366 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
367 * \retval PSA_ERROR_COMMUNICATION_FAILURE
368 * \retval PSA_ERROR_HARDWARE_FAILURE
369 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100370 */
371psa_status_t psa_import_key(psa_key_slot_t key,
372 psa_key_type_t type,
373 const uint8_t *data,
374 size_t data_length);
375
376/**
377 * \brief Destroy a key.
378 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100379 * \retval PSA_SUCCESS
380 * \retval PSA_ERROR_EMPTY_SLOT
381 * \retval PSA_ERROR_COMMUNICATION_FAILURE
382 * \retval PSA_ERROR_HARDWARE_FAILURE
383 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100384 */
385psa_status_t psa_destroy_key(psa_key_slot_t key);
386
387/**
388 * \brief Get basic metadata about a key.
389 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100390 * \param key Slot whose content is queried. This must
391 * be an occupied key slot.
392 * \param type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
393 * This may be a null pointer, in which case the key type
394 * is not written.
395 * \param bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100396 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100397 * is not written.
398 *
399 * \retval PSA_SUCCESS
400 * \retval PSA_ERROR_EMPTY_SLOT
401 * \retval PSA_ERROR_COMMUNICATION_FAILURE
402 * \retval PSA_ERROR_HARDWARE_FAILURE
403 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100404 */
405psa_status_t psa_get_key_information(psa_key_slot_t key,
406 psa_key_type_t *type,
407 size_t *bits);
408
409/**
410 * \brief Export a key in binary format.
411 *
412 * The output of this function can be passed to psa_import_key() to
413 * create an equivalent object.
414 *
415 * If a key is created with psa_import_key() and then exported with
416 * this function, it is not guaranteed that the resulting data is
417 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +0100418 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100419 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100420 * For standard key types, the output format is as follows:
421 *
422 * - For symmetric keys (including MAC keys), the format is the
423 * raw bytes of the key.
424 * - For DES, the key data consists of 8 bytes. The parity bits must be
425 * correct.
426 * - For Triple-DES, the format is the concatenation of the
427 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100428 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine308b91d2018-02-08 09:47:44 +0100429 * is the non-encrypted DER representation defined by PKCS\#8 (RFC 5208)
430 * as PrivateKeyInfo.
431 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
Gilles Peskine971f7062018-03-20 17:52:58 +0100432 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100433 *
434 * \param key Slot whose content is to be exported. This must
435 * be an occupied key slot.
436 * \param data Buffer where the key data is to be written.
437 * \param data_size Size of the \c data buffer in bytes.
438 * \param data_length On success, the number of bytes
439 * that make up the key data.
440 *
441 * \retval PSA_SUCCESS
442 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine92b30732018-03-03 21:29:30 +0100443 * \retval PSA_ERROR_NOT_PERMITTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100444 * \retval PSA_ERROR_COMMUNICATION_FAILURE
445 * \retval PSA_ERROR_HARDWARE_FAILURE
446 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100447 */
448psa_status_t psa_export_key(psa_key_slot_t key,
449 uint8_t *data,
450 size_t data_size,
451 size_t *data_length);
452
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100453/**
454 * \brief Export a public key or the public part of a key pair in binary format.
455 *
456 * The output of this function can be passed to psa_import_key() to
457 * create an object that is equivalent to the public key.
458 *
459 * For standard key types, the output format is as follows:
460 *
461 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
Gilles Peskine971f7062018-03-20 17:52:58 +0100462 * is the DER representation of the public key defined by RFC 5280
463 * as SubjectPublicKeyInfo.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100464 *
465 * \param key Slot whose content is to be exported. This must
466 * be an occupied key slot.
467 * \param data Buffer where the key data is to be written.
468 * \param data_size Size of the \c data buffer in bytes.
469 * \param data_length On success, the number of bytes
470 * that make up the key data.
471 *
472 * \retval PSA_SUCCESS
473 * \retval PSA_ERROR_EMPTY_SLOT
474 * \retval PSA_ERROR_INVALID_ARGUMENT
475 * \retval PSA_ERROR_COMMUNICATION_FAILURE
476 * \retval PSA_ERROR_HARDWARE_FAILURE
477 * \retval PSA_ERROR_TAMPERING_DETECTED
478 */
479psa_status_t psa_export_public_key(psa_key_slot_t key,
480 uint8_t *data,
481 size_t data_size,
482 size_t *data_length);
483
484/**@}*/
485
486/** \defgroup policy Key policies
487 * @{
488 */
489
490/** \brief Encoding of permitted usage on a key. */
491typedef uint32_t psa_key_usage_t;
492
Gilles Peskine7e198532018-03-08 07:50:30 +0100493/** Whether the key may be exported.
494 *
495 * A public key or the public part of a key pair may always be exported
496 * regardless of the value of this permission flag.
497 *
498 * If a key does not have export permission, implementations shall not
499 * allow the key to be exported in plain form from the cryptoprocessor,
500 * whether through psa_export_key() or through a proprietary interface.
501 * The key may however be exportable in a wrapped form, i.e. in a form
502 * where it is encrypted by another key.
503 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100504#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
505
Gilles Peskine7e198532018-03-08 07:50:30 +0100506/** Whether the key may be used to encrypt a message.
507 *
508 * For a key pair, this concerns the public key.
509 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100510#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +0100511
512/** Whether the key may be used to decrypt a message.
513 *
514 * For a key pair, this concerns the private key.
515 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100516#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +0100517
518/** Whether the key may be used to sign a message.
519 *
520 * For a key pair, this concerns the private key.
521 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100522#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +0100523
524/** Whether the key may be used to verify a message signature.
525 *
526 * For a key pair, this concerns the public key.
527 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100528#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
529
530/** The type of the key policy data structure.
531 *
532 * This is an implementation-defined \c struct. Applications should not
533 * make any assumptions about the content of this structure except
534 * as directed by the documentation of a specific implementation. */
535typedef struct psa_key_policy_s psa_key_policy_t;
536
537/** \brief Initialize a key policy structure to a default that forbids all
538 * usage of the key. */
539void psa_key_policy_init(psa_key_policy_t *policy);
540
Gilles Peskine7e198532018-03-08 07:50:30 +0100541/** \brief Set the standard fields of a policy structure.
542 *
543 * Note that this function does not make any consistency check of the
544 * parameters. The values are only checked when applying the policy to
545 * a key slot with psa_set_key_policy().
546 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100547void psa_key_policy_set_usage(psa_key_policy_t *policy,
548 psa_key_usage_t usage,
549 psa_algorithm_t alg);
550
551psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy);
552
553psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy);
554
555/** \brief Set the usage policy on a key slot.
556 *
557 * This function must be called on an empty key slot, before importing,
558 * generating or creating a key in the slot. Changing the policy of an
559 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100560 *
561 * Implementations may set restrictions on supported key policies
562 * depending on the key type and the key slot.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100563 */
564psa_status_t psa_set_key_policy(psa_key_slot_t key,
565 const psa_key_policy_t *policy);
566
Gilles Peskine7e198532018-03-08 07:50:30 +0100567/** \brief Get the usage policy for a key slot.
568 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100569psa_status_t psa_get_key_policy(psa_key_slot_t key,
570 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100571
572/**@}*/
573
Gilles Peskine609b6a52018-03-03 21:31:50 +0100574/** \defgroup persistence Key lifetime
575 * @{
576 */
577
578/** Encoding of key lifetimes.
579 */
580typedef uint32_t psa_key_lifetime_t;
581
582/** A volatile key slot retains its content as long as the application is
583 * running. It is guaranteed to be erased on a power reset.
584 */
585#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
586
587/** A persistent key slot retains its content as long as it is not explicitly
588 * destroyed.
589 */
590#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
591
592/** A write-once key slot may not be modified once a key has been set.
593 * It will retain its content as long as the device remains operational.
594 */
595#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
596
Gilles Peskined393e182018-03-08 07:49:16 +0100597/** \brief Retrieve the lifetime of a key slot.
598 *
599 * The assignment of lifetimes to slots is implementation-dependent.
600 */
Gilles Peskine609b6a52018-03-03 21:31:50 +0100601psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
602 psa_key_lifetime_t *lifetime);
603
Gilles Peskined393e182018-03-08 07:49:16 +0100604/** \brief Change the lifetime of a key slot.
605 *
606 * Whether the lifetime of a key slot can be changed at all, and if so
Gilles Peskine19067982018-03-20 17:54:53 +0100607 * whether the lifetime of an occupied key slot can be changed, is
Gilles Peskined393e182018-03-08 07:49:16 +0100608 * implementation-dependent.
609 */
610psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
611 const psa_key_lifetime_t *lifetime);
612
Gilles Peskine609b6a52018-03-03 21:31:50 +0100613/**@}*/
614
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100615/** \defgroup hash Message digests
616 * @{
617 */
618
Gilles Peskine308b91d2018-02-08 09:47:44 +0100619/** The type of the state data structure for multipart hash operations.
620 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100621 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100622 * make any assumptions about the content of this structure except
623 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100624typedef struct psa_hash_operation_s psa_hash_operation_t;
625
Gilles Peskine308b91d2018-02-08 09:47:44 +0100626/** The size of the output of psa_hash_finish(), in bytes.
627 *
628 * This is also the hash size that psa_hash_verify() expects.
629 *
630 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
631 * #PSA_ALG_IS_HASH(alg) is true).
632 *
633 * \return The hash size for the specified hash algorithm.
634 * If the hash algorithm is not recognized, return 0.
635 * An implementation may return either 0 or the correct size
636 * for a hash algorithm that it recognizes, but does not support.
637 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100638#define PSA_HASH_FINAL_SIZE(alg) \
639 ( \
640 (alg) == PSA_ALG_MD2 ? 16 : \
641 (alg) == PSA_ALG_MD4 ? 16 : \
642 (alg) == PSA_ALG_MD5 ? 16 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100643 (alg) == PSA_ALG_RIPEMD160 ? 20 : \
644 (alg) == PSA_ALG_SHA_1 ? 20 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100645 (alg) == PSA_ALG_SHA_224 ? 28 : \
646 (alg) == PSA_ALG_SHA_256 ? 32 : \
647 (alg) == PSA_ALG_SHA_384 ? 48 : \
648 (alg) == PSA_ALG_SHA_512 ? 64 : \
649 (alg) == PSA_ALG_SHA_512_224 ? 28 : \
650 (alg) == PSA_ALG_SHA_512_256 ? 32 : \
651 (alg) == PSA_ALG_SHA3_224 ? 28 : \
652 (alg) == PSA_ALG_SHA3_256 ? 32 : \
653 (alg) == PSA_ALG_SHA3_384 ? 48 : \
654 (alg) == PSA_ALG_SHA3_512 ? 64 : \
655 0)
656
Gilles Peskine308b91d2018-02-08 09:47:44 +0100657/** Start a multipart hash operation.
658 *
659 * The sequence of operations to calculate a hash (message digest)
660 * is as follows:
661 * -# Allocate an operation object which will be passed to all the functions
662 * listed here.
663 * -# Call psa_hash_start() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100664 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100665 * of the message each time. The hash that is calculated is the hash
666 * of the concatenation of these messages in order.
667 * -# To calculate the hash, call psa_hash_finish().
668 * To compare the hash with an expected value, call psa_hash_verify().
669 *
670 * The application may call psa_hash_abort() at any time after the operation
671 * has been initialized with psa_hash_start().
672 *
673 * After a successful call to psa_hash_start(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100674 * eventually terminate the operation. The following events terminate an
675 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100676 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100677 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100678 *
679 * \param operation
680 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
681 * such that #PSA_ALG_IS_HASH(alg) is true).
682 *
683 * \retval PSA_SUCCESS
684 * Success.
685 * \retval PSA_ERROR_NOT_SUPPORTED
686 * \c alg is not supported or is not a hash algorithm.
687 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
688 * \retval PSA_ERROR_COMMUNICATION_FAILURE
689 * \retval PSA_ERROR_HARDWARE_FAILURE
690 * \retval PSA_ERROR_TAMPERING_DETECTED
691 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100692psa_status_t psa_hash_start(psa_hash_operation_t *operation,
693 psa_algorithm_t alg);
694
Gilles Peskine308b91d2018-02-08 09:47:44 +0100695/** Add a message fragment to a multipart hash operation.
696 *
697 * The application must call psa_hash_start() before calling this function.
698 *
699 * If this function returns an error status, the operation becomes inactive.
700 *
701 * \param operation Active hash operation.
702 * \param input Buffer containing the message fragment to hash.
703 * \param input_length Size of the \c input buffer in bytes.
704 *
705 * \retval PSA_SUCCESS
706 * Success.
707 * \retval PSA_ERROR_BAD_STATE
708 * The operation state is not valid (not started, or already completed).
709 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
710 * \retval PSA_ERROR_COMMUNICATION_FAILURE
711 * \retval PSA_ERROR_HARDWARE_FAILURE
712 * \retval PSA_ERROR_TAMPERING_DETECTED
713 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100714psa_status_t psa_hash_update(psa_hash_operation_t *operation,
715 const uint8_t *input,
716 size_t input_length);
717
Gilles Peskine308b91d2018-02-08 09:47:44 +0100718/** Finish the calculation of the hash of a message.
719 *
720 * The application must call psa_hash_start() before calling this function.
721 * This function calculates the hash of the message formed by concatenating
722 * the inputs passed to preceding calls to psa_hash_update().
723 *
724 * When this function returns, the operation becomes inactive.
725 *
726 * \warning Applications should not call this function if they expect
727 * a specific value for the hash. Call psa_hash_verify() instead.
728 * Beware that comparing integrity or authenticity data such as
729 * hash values with a function such as \c memcmp is risky
730 * because the time taken by the comparison may leak information
731 * about the hashed data which could allow an attacker to guess
732 * a valid hash and thereby bypass security controls.
733 *
734 * \param operation Active hash operation.
735 * \param hash Buffer where the hash is to be written.
736 * \param hash_size Size of the \c hash buffer in bytes.
737 * \param hash_length On success, the number of bytes
738 * that make up the hash value. This is always
739 * #PSA_HASH_FINAL_SIZE(alg) where \c alg is the
740 * hash algorithm that is calculated.
741 *
742 * \retval PSA_SUCCESS
743 * Success.
744 * \retval PSA_ERROR_BAD_STATE
745 * The operation state is not valid (not started, or already completed).
746 * \retval PSA_ERROR_BUFFER_TOO_SMALL
747 * The size of the \c hash buffer is too small. You can determine a
748 * sufficient buffer size by calling #PSA_HASH_FINAL_SIZE(alg)
749 * where \c alg is the hash algorithm that is calculated.
750 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
751 * \retval PSA_ERROR_COMMUNICATION_FAILURE
752 * \retval PSA_ERROR_HARDWARE_FAILURE
753 * \retval PSA_ERROR_TAMPERING_DETECTED
754 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100755psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
756 uint8_t *hash,
757 size_t hash_size,
758 size_t *hash_length);
759
Gilles Peskine308b91d2018-02-08 09:47:44 +0100760/** Finish the calculation of the hash of a message and compare it with
761 * an expected value.
762 *
763 * The application must call psa_hash_start() before calling this function.
764 * This function calculates the hash of the message formed by concatenating
765 * the inputs passed to preceding calls to psa_hash_update(). It then
766 * compares the calculated hash with the expected hash passed as a
767 * parameter to this function.
768 *
769 * When this function returns, the operation becomes inactive.
770 *
Gilles Peskine19067982018-03-20 17:54:53 +0100771 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100772 * comparison between the actual hash and the expected hash is performed
773 * in constant time.
774 *
775 * \param operation Active hash operation.
776 * \param hash Buffer containing the expected hash value.
777 * \param hash_length Size of the \c hash buffer in bytes.
778 *
779 * \retval PSA_SUCCESS
780 * The expected hash is identical to the actual hash of the message.
781 * \retval PSA_ERROR_INVALID_SIGNATURE
782 * The hash of the message was calculated successfully, but it
783 * differs from the expected hash.
784 * \retval PSA_ERROR_BAD_STATE
785 * The operation state is not valid (not started, or already completed).
786 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
787 * \retval PSA_ERROR_COMMUNICATION_FAILURE
788 * \retval PSA_ERROR_HARDWARE_FAILURE
789 * \retval PSA_ERROR_TAMPERING_DETECTED
790 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100791psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
792 const uint8_t *hash,
793 size_t hash_length);
794
Gilles Peskine308b91d2018-02-08 09:47:44 +0100795/** Abort a hash operation.
796 *
797 * This function may be called at any time after psa_hash_start().
798 * Aborting an operation frees all associated resources except for the
799 * \c operation structure itself.
800 *
801 * Implementation should strive to be robust and handle inactive hash
802 * operations safely (do nothing and return #PSA_ERROR_BAD_STATE). However,
803 * application writers should beware that uninitialized memory may happen
804 * to be indistinguishable from an active hash operation, and the behavior
805 * of psa_hash_abort() is undefined in this case.
806 *
807 * \param operation Active hash operation.
808 *
809 * \retval PSA_SUCCESS
810 * \retval PSA_ERROR_BAD_STATE
811 * \c operation is not an active hash operation.
812 * \retval PSA_ERROR_COMMUNICATION_FAILURE
813 * \retval PSA_ERROR_HARDWARE_FAILURE
814 * \retval PSA_ERROR_TAMPERING_DETECTED
815 */
816psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100817
818/**@}*/
819
Gilles Peskine8c9def32018-02-08 10:02:12 +0100820/** \defgroup MAC Message authentication codes
821 * @{
822 */
823
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100824/** The type of the state data structure for multipart MAC operations.
825 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100826 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100827 * make any assumptions about the content of this structure except
828 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100829typedef struct psa_mac_operation_s psa_mac_operation_t;
830
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100831/** The size of the output of psa_mac_finish(), in bytes.
832 *
833 * This is also the MAC size that psa_mac_verify() expects.
834 *
835 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
836 * #PSA_ALG_IS_MAC(alg) is true).
837 *
838 * \return The MAC size for the specified algorithm.
839 * If the MAC algorithm is not recognized, return 0.
840 * An implementation may return either 0 or the correct size
841 * for a MAC algorithm that it recognizes, but does not support.
842 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100843#define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \
844 (PSA_ALG_IS_HMAC(alg) ? PSA_HASH_FINAL_SIZE(PSA_ALG_HMAC_HASH(alg)) : \
845 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
846 0)
847
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100848/** Start a multipart MAC operation.
849 *
850 * The sequence of operations to calculate a MAC (message authentication code)
851 * is as follows:
852 * -# Allocate an operation object which will be passed to all the functions
853 * listed here.
854 * -# Call psa_mac_start() to specify the algorithm and key.
855 * The key remains associated with the operation even if the content
856 * of the key slot changes.
857 * -# Call psa_mac_update() zero, one or more times, passing a fragment
858 * of the message each time. The MAC that is calculated is the MAC
859 * of the concatenation of these messages in order.
860 * -# To calculate the MAC, call psa_mac_finish().
861 * To compare the MAC with an expected value, call psa_mac_verify().
862 *
863 * The application may call psa_mac_abort() at any time after the operation
864 * has been initialized with psa_mac_start().
865 *
866 * After a successful call to psa_mac_start(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100867 * eventually terminate the operation. The following events terminate an
868 * operation:
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100869 * - A failed call to psa_mac_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100870 * - A call to psa_mac_finish(), psa_mac_verify() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100871 *
872 * \param operation
873 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
874 * such that #PSA_ALG_IS_MAC(alg) is true).
875 *
876 * \retval PSA_SUCCESS
877 * Success.
878 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine92b30732018-03-03 21:29:30 +0100879 * \retval PSA_ERROR_NOT_PERMITTED
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100880 * \retval PSA_ERROR_INVALID_ARGUMENT
881 * \c key is not compatible with \c alg.
882 * \retval PSA_ERROR_NOT_SUPPORTED
883 * \c alg is not supported or is not a MAC algorithm.
884 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
885 * \retval PSA_ERROR_COMMUNICATION_FAILURE
886 * \retval PSA_ERROR_HARDWARE_FAILURE
887 * \retval PSA_ERROR_TAMPERING_DETECTED
888 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100889psa_status_t psa_mac_start(psa_mac_operation_t *operation,
890 psa_key_slot_t key,
891 psa_algorithm_t alg);
892
893psa_status_t psa_mac_update(psa_mac_operation_t *operation,
894 const uint8_t *input,
895 size_t input_length);
896
897psa_status_t psa_mac_finish(psa_mac_operation_t *operation,
898 uint8_t *mac,
899 size_t mac_size,
900 size_t *mac_length);
901
902psa_status_t psa_mac_verify(psa_mac_operation_t *operation,
903 const uint8_t *mac,
904 size_t mac_length);
905
906psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
907
908/**@}*/
909
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100910/** \defgroup cipher Symmetric ciphers
911 * @{
912 */
913
914/** The type of the state data structure for multipart cipher operations.
915 *
916 * This is an implementation-defined \c struct. Applications should not
917 * make any assumptions about the content of this structure except
918 * as directed by the documentation of a specific implementation. */
919typedef struct psa_cipher_operation_s psa_cipher_operation_t;
920
921/** Set the key for a multipart symmetric encryption operation.
922 *
923 * The sequence of operations to encrypt a message with a symmetric cipher
924 * is as follows:
925 * -# Allocate an operation object which will be passed to all the functions
926 * listed here.
927 * -# Call psa_encrypt_setup() to specify the algorithm and key.
928 * The key remains associated with the operation even if the content
929 * of the key slot changes.
930 * -# Call either psa_encrypt_generate_iv() or psa_encrypt_set_iv() to
931 * generate or set the IV (initialization vector). You should use
932 * psa_encrypt_generate_iv() unless the protocol you are implementing
933 * requires a specific IV value.
934 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
935 * of the message each time.
936 * -# Call psa_cipher_finish().
937 *
938 * The application may call psa_cipher_abort() at any time after the operation
939 * has been initialized with psa_encrypt_setup().
940 *
941 * After a successful call to psa_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100942 * eventually terminate the operation. The following events terminate an
943 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100944 * - A failed call to psa_encrypt_generate_iv(), psa_encrypt_set_iv()
945 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100946 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100947 *
948 * \param operation
949 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
950 * such that #PSA_ALG_IS_CIPHER(alg) is true).
951 *
952 * \retval PSA_SUCCESS
953 * Success.
954 * \retval PSA_ERROR_EMPTY_SLOT
955 * \retval PSA_ERROR_NOT_PERMITTED
956 * \retval PSA_ERROR_INVALID_ARGUMENT
957 * \c key is not compatible with \c alg.
958 * \retval PSA_ERROR_NOT_SUPPORTED
959 * \c alg is not supported or is not a cipher algorithm.
960 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
961 * \retval PSA_ERROR_COMMUNICATION_FAILURE
962 * \retval PSA_ERROR_HARDWARE_FAILURE
963 * \retval PSA_ERROR_TAMPERING_DETECTED
964 */
965psa_status_t psa_encrypt_setup(psa_cipher_operation_t *operation,
966 psa_key_slot_t key,
967 psa_algorithm_t alg);
968
969/** Set the key for a multipart symmetric decryption operation.
970 *
971 * The sequence of operations to decrypt a message with a symmetric cipher
972 * is as follows:
973 * -# Allocate an operation object which will be passed to all the functions
974 * listed here.
975 * -# Call psa_decrypt_setup() to specify the algorithm and key.
976 * The key remains associated with the operation even if the content
977 * of the key slot changes.
978 * -# Call psa_cipher_update() with the IV (initialization vector) for the
979 * decryption. If the IV is prepended to the ciphertext, you can call
980 * psa_cipher_update() on a buffer containing the IV followed by the
981 * beginning of the message.
982 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
983 * of the message each time.
984 * -# Call psa_cipher_finish().
985 *
986 * The application may call psa_cipher_abort() at any time after the operation
987 * has been initialized with psa_encrypt_setup().
988 *
989 * After a successful call to psa_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100990 * eventually terminate the operation. The following events terminate an
991 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100992 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100993 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100994 *
995 * \param operation
996 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
997 * such that #PSA_ALG_IS_CIPHER(alg) is true).
998 *
999 * \retval PSA_SUCCESS
1000 * Success.
1001 * \retval PSA_ERROR_EMPTY_SLOT
1002 * \retval PSA_ERROR_NOT_PERMITTED
1003 * \retval PSA_ERROR_INVALID_ARGUMENT
1004 * \c key is not compatible with \c alg.
1005 * \retval PSA_ERROR_NOT_SUPPORTED
1006 * \c alg is not supported or is not a cipher algorithm.
1007 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1008 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1009 * \retval PSA_ERROR_HARDWARE_FAILURE
1010 * \retval PSA_ERROR_TAMPERING_DETECTED
1011 */
1012psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation,
1013 psa_key_slot_t key,
1014 psa_algorithm_t alg);
1015
1016psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation,
1017 unsigned char *iv,
1018 size_t iv_size,
1019 size_t *iv_length);
1020
1021psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation,
1022 const unsigned char *iv,
1023 size_t iv_length);
1024
1025psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1026 const uint8_t *input,
1027 size_t input_length);
1028
1029psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
1030 uint8_t *mac,
1031 size_t mac_size,
1032 size_t *mac_length);
1033
1034psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1035
1036/**@}*/
1037
Gilles Peskine3b555712018-03-03 21:27:57 +01001038/** \defgroup aead Authenticated encryption with associated data (AEAD)
1039 * @{
1040 */
1041
1042/** The type of the state data structure for multipart AEAD operations.
1043 *
1044 * This is an implementation-defined \c struct. Applications should not
1045 * make any assumptions about the content of this structure except
1046 * as directed by the documentation of a specific implementation. */
1047typedef struct psa_aead_operation_s psa_aead_operation_t;
1048
1049/** Set the key for a multipart authenticated encryption operation.
1050 *
1051 * The sequence of operations to authenticate-and-encrypt a message
1052 * is as follows:
1053 * -# Allocate an operation object which will be passed to all the functions
1054 * listed here.
1055 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
1056 * The key remains associated with the operation even if the content
1057 * of the key slot changes.
1058 * -# Call either psa_aead_generate_iv() or psa_aead_set_iv() to
1059 * generate or set the IV (initialization vector). You should use
1060 * psa_encrypt_generate_iv() unless the protocol you are implementing
1061 * requires a specific IV value.
1062 * -# Call psa_aead_update_ad() to pass the associated data that is
1063 * to be authenticated but not encrypted. You may omit this step if
1064 * there is no associated data.
1065 * -# Call psa_aead_update() zero, one or more times, passing a fragment
1066 * of the data to encrypt each time.
1067 * -# Call psa_aead_finish().
1068 *
1069 * The application may call psa_aead_abort() at any time after the operation
1070 * has been initialized with psa_aead_encrypt_setup().
1071 *
Gilles Peskineed522972018-03-20 17:54:15 +01001072 * After a successful call to psa_aead_encrypt_setup(), the application must
1073 * eventually terminate the operation. The following events terminate an
1074 * operation:
Gilles Peskine3b555712018-03-03 21:27:57 +01001075 * - A failed call to psa_aead_generate_iv(), psa_aead_set_iv(),
1076 * psa_aead_update_ad() or psa_aead_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001077 * - A call to psa_aead_finish() or psa_aead_abort().
Gilles Peskine3b555712018-03-03 21:27:57 +01001078 *
1079 * \param operation
1080 * \param alg The AEAD algorithm to compute (\c PSA_ALG_XXX value
1081 * such that #PSA_ALG_IS_AEAD(alg) is true).
1082 *
1083 * \retval PSA_SUCCESS
1084 * Success.
1085 * \retval PSA_ERROR_EMPTY_SLOT
1086 * \retval PSA_ERROR_NOT_PERMITTED
1087 * \retval PSA_ERROR_INVALID_ARGUMENT
1088 * \c key is not compatible with \c alg.
1089 * \retval PSA_ERROR_NOT_SUPPORTED
1090 * \c alg is not supported or is not an AEAD algorithm.
1091 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1092 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1093 * \retval PSA_ERROR_HARDWARE_FAILURE
1094 * \retval PSA_ERROR_TAMPERING_DETECTED
1095 */
1096psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
1097 psa_key_slot_t key,
1098 psa_algorithm_t alg);
1099
1100/** Set the key for a multipart authenticated decryption operation.
1101 *
1102 * The sequence of operations to authenticated and decrypt a message
1103 * is as follows:
1104 * -# Allocate an operation object which will be passed to all the functions
1105 * listed here.
1106 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
1107 * The key remains associated with the operation even if the content
1108 * of the key slot changes.
1109 * -# Call psa_aead_set_iv() to pass the initialization vector (IV)
1110 * for the authenticated decryption.
1111 * -# Call psa_aead_update_ad() to pass the associated data that is
1112 * to be authenticated but not encrypted. You may omit this step if
1113 * there is no associated data.
1114 * -# Call psa_aead_update() zero, one or more times, passing a fragment
1115 * of the data to decrypt each time.
1116 * -# Call psa_aead_finish().
1117 *
1118 * The application may call psa_aead_abort() at any time after the operation
1119 * has been initialized with psa_aead_decrypt_setup().
1120 *
Gilles Peskineed522972018-03-20 17:54:15 +01001121 * After a successful call to psa_aead_decrypt_setup(), the application must
1122 * eventually terminate the operation. The following events terminate an
1123 * operation:
Gilles Peskine3b555712018-03-03 21:27:57 +01001124 * - A failed call to psa_aead_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001125 * - A call to psa_aead_finish() or psa_aead_abort().
Gilles Peskine3b555712018-03-03 21:27:57 +01001126 *
1127 * \param operation
Gilles Peskine19067982018-03-20 17:54:53 +01001128 * \param alg The AEAD algorithm to compute (\c PSA_ALG_XXX value
1129 * such that #PSA_ALG_IS_AEAD(alg) is true).
Gilles Peskine3b555712018-03-03 21:27:57 +01001130 *
1131 * \retval PSA_SUCCESS
1132 * Success.
1133 * \retval PSA_ERROR_EMPTY_SLOT
1134 * \retval PSA_ERROR_NOT_PERMITTED
1135 * \retval PSA_ERROR_INVALID_ARGUMENT
1136 * \c key is not compatible with \c alg.
1137 * \retval PSA_ERROR_NOT_SUPPORTED
Gilles Peskine19067982018-03-20 17:54:53 +01001138 * \c alg is not supported or is not an AEAD algorithm.
Gilles Peskine3b555712018-03-03 21:27:57 +01001139 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1140 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1141 * \retval PSA_ERROR_HARDWARE_FAILURE
1142 * \retval PSA_ERROR_TAMPERING_DETECTED
1143 */
1144psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
1145 psa_key_slot_t key,
1146 psa_algorithm_t alg);
1147
1148psa_status_t psa_aead_generate_iv(psa_aead_operation_t *operation,
1149 unsigned char *iv,
1150 size_t iv_size,
1151 size_t *iv_length);
1152
1153psa_status_t psa_aead_set_iv(psa_aead_operation_t *operation,
1154 const unsigned char *iv,
1155 size_t iv_length);
1156
1157psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
1158 const uint8_t *input,
1159 size_t input_length);
1160
1161psa_status_t psa_aead_update(psa_aead_operation_t *operation,
1162 const uint8_t *input,
1163 size_t input_length);
1164
1165psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
1166 uint8_t *tag,
1167 size_t tag_size,
1168 size_t *tag_length);
1169
1170psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
1171 uint8_t *tag,
1172 size_t tag_length);
1173
1174psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
1175
1176/**@}*/
1177
Gilles Peskine20035e32018-02-03 22:44:14 +01001178/** \defgroup asymmetric Asymmetric cryptography
1179 * @{
1180 */
1181
1182/**
Gilles Peskine0189e752018-02-03 23:57:22 +01001183 * \brief Maximum ECDSA signature size for a given curve bit size
1184 *
1185 * \param curve_bits Curve size in bits
1186 * \return Maximum signature size in bytes
1187 *
1188 * \note This macro returns a compile-time constant if its argument is one.
1189 *
1190 * \warning This macro may evaluate its argument multiple times.
1191 */
1192/*
1193 * RFC 4492 page 20:
1194 *
1195 * Ecdsa-Sig-Value ::= SEQUENCE {
1196 * r INTEGER,
1197 * s INTEGER
1198 * }
1199 *
1200 * Size is at most
1201 * 1 (tag) + 1 (len) + 1 (initial 0) + curve_bytes for each of r and s,
1202 * twice that + 1 (tag) + 2 (len) for the sequence
1203 * (assuming curve_bytes is less than 126 for r and s,
1204 * and less than 124 (total len <= 255) for the sequence)
1205 */
1206#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
1207 ( /*T,L of SEQUENCE*/ ((curve_bits) >= 61 * 8 ? 3 : 2) + \
1208 /*T,L of r,s*/ 2 * (((curve_bits) >= 127 * 8 ? 3 : 2) + \
1209 /*V of r,s*/ ((curve_bits) + 8) / 8))
1210
1211
Gilles Peskine308b91d2018-02-08 09:47:44 +01001212/** Safe signature buffer size for psa_asymmetric_sign().
1213 *
1214 * This macro returns a safe buffer size for a signature using a key
1215 * of the specified type and size, with the specified algorithm.
1216 * Note that the actual size of the signature may be smaller
1217 * (some algorithms produce a variable-size signature).
1218 *
1219 * \warning This function may call its arguments multiple times or
1220 * zero times, so you should not pass arguments that contain
1221 * side effects.
1222 *
1223 * \param key_type An asymmetric key type (this may indifferently be a
1224 * key pair type or a public key type).
1225 * \param key_bits The size of the key in bits.
1226 * \param alg The signature algorithm.
1227 *
1228 * \return If the parameters are valid and supported, return
1229 * a buffer size in bytes that guarantees that
1230 * psa_asymmetric_sign() will not fail with
1231 * #PSA_ERROR_BUFFER_TOO_SMALL.
1232 * If the parameters are a valid combination that is not supported
1233 * by the implementation, this macro either shall return either a
1234 * sensible size or 0.
1235 * If the parameters are not valid, the
1236 * return value is unspecified.
1237 *
1238 */
Gilles Peskine0189e752018-02-03 23:57:22 +01001239#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine2905a7a2018-03-07 16:39:31 +01001240 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine0189e752018-02-03 23:57:22 +01001241 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
Gilles Peskine84845652018-03-28 14:17:40 +02001242 ((void)alg, 0))
Gilles Peskine0189e752018-02-03 23:57:22 +01001243
1244/**
Gilles Peskine20035e32018-02-03 22:44:14 +01001245 * \brief Sign a hash or short message with a private key.
1246 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001247 * \param key Key slot containing an asymmetric key pair.
1248 * \param alg A signature algorithm that is compatible with
1249 * the type of \c key.
1250 * \param hash The message to sign.
1251 * \param hash_length Size of the \c hash buffer in bytes.
1252 * \param salt A salt or label, if supported by the signature
1253 * algorithm.
1254 * If the signature algorithm does not support a
1255 * salt, pass \c NULL.
1256 * If the signature algorithm supports an optional
1257 * salt and you do not want to pass a salt,
1258 * pass \c NULL.
1259 * \param salt_length Size of the \c salt buffer in bytes.
1260 * If \c salt is \c NULL, pass 0.
1261 * \param signature Buffer where the signature is to be written.
1262 * \param signature_size Size of the \c signature buffer in bytes.
1263 * \param signature_length On success, the number of bytes
1264 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001265 *
1266 * \retval PSA_SUCCESS
1267 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1268 * The size of the \c signature buffer is too small. You can
1269 * determine a sufficient buffer size by calling
1270 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg)
1271 * where \c key_type and \c key_bits are the type and bit-size
1272 * respectively of \c key.
1273 * \retval PSA_ERROR_NOT_SUPPORTED
1274 * \retval PSA_ERROR_INVALID_ARGUMENT
1275 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1276 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1277 * \retval PSA_ERROR_HARDWARE_FAILURE
1278 * \retval PSA_ERROR_TAMPERING_DETECTED
1279 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01001280 */
1281psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
1282 psa_algorithm_t alg,
1283 const uint8_t *hash,
1284 size_t hash_length,
1285 const uint8_t *salt,
1286 size_t salt_length,
1287 uint8_t *signature,
1288 size_t signature_size,
1289 size_t *signature_length);
1290
1291/**
1292 * \brief Verify the signature a hash or short message using a public key.
1293 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001294 * \param key Key slot containing a public key or an
1295 * asymmetric key pair.
1296 * \param alg A signature algorithm that is compatible with
1297 * the type of \c key.
1298 * \param hash The message whose signature is to be verified.
1299 * \param hash_length Size of the \c hash buffer in bytes.
1300 * \param salt A salt or label, if supported by the signature
1301 * algorithm.
1302 * If the signature algorithm does not support a
1303 * salt, pass \c NULL.
1304 * If the signature algorithm supports an optional
1305 * salt and you do not want to pass a salt,
1306 * pass \c NULL.
1307 * \param salt_length Size of the \c salt buffer in bytes.
1308 * If \c salt is \c NULL, pass 0.
1309 * \param signature Buffer containing the signature to verify.
1310 * \param signature_size Size of the \c signature buffer in bytes.
1311 *
1312 * \retval PSA_SUCCESS
1313 * The signature is valid.
1314 * \retval PSA_ERROR_INVALID_SIGNATURE
1315 * The calculation was perfomed successfully, but the passed
1316 * signature is not a valid signature.
1317 * \retval PSA_ERROR_NOT_SUPPORTED
1318 * \retval PSA_ERROR_INVALID_ARGUMENT
1319 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1320 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1321 * \retval PSA_ERROR_HARDWARE_FAILURE
1322 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01001323 */
1324psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
1325 psa_algorithm_t alg,
1326 const uint8_t *hash,
1327 size_t hash_length,
1328 const uint8_t *salt,
1329 size_t salt_length,
1330 uint8_t *signature,
1331 size_t signature_size);
1332
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001333#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
1334 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
1335 ((void)alg, 0))
1336#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
1337 PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1338
1339/**
1340 * \brief Encrypt a short message with a public key.
1341 *
1342 * \param key Key slot containing a public key or an asymmetric
1343 * key pair.
1344 * \param alg An asymmetric encryption algorithm that is
1345 * compatible with the type of \c key.
1346 * \param input The message to encrypt.
1347 * \param input_length Size of the \c input buffer in bytes.
1348 * \param salt A salt or label, if supported by the encryption
1349 * algorithm.
1350 * If the algorithm does not support a
1351 * salt, pass \c NULL.
1352 * If the algorithm supports an optional
1353 * salt and you do not want to pass a salt,
1354 * pass \c NULL.
1355 *
1356 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1357 * supported.
1358 * \param salt_length Size of the \c salt buffer in bytes.
1359 * If \c salt is \c NULL, pass 0.
1360 * \param output Buffer where the encrypted message is to be written.
1361 * \param output_size Size of the \c output buffer in bytes.
1362 * \param output_length On success, the number of bytes
1363 * that make up the returned output.
1364 *
1365 * \retval PSA_SUCCESS
1366 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1367 * The size of the \c output buffer is too small. You can
1368 * determine a sufficient buffer size by calling
1369 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1370 * where \c key_type and \c key_bits are the type and bit-size
1371 * respectively of \c key.
1372 * \retval PSA_ERROR_NOT_SUPPORTED
1373 * \retval PSA_ERROR_INVALID_ARGUMENT
1374 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1375 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1376 * \retval PSA_ERROR_HARDWARE_FAILURE
1377 * \retval PSA_ERROR_TAMPERING_DETECTED
1378 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1379 */
1380psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
1381 psa_algorithm_t alg,
1382 const uint8_t *input,
1383 size_t input_length,
1384 const uint8_t *salt,
1385 size_t salt_length,
1386 uint8_t *output,
1387 size_t output_size,
1388 size_t *output_length);
1389
1390/**
1391 * \brief Decrypt a short message with a private key.
1392 *
1393 * \param key Key slot containing an asymmetric key pair.
1394 * \param alg An asymmetric encryption algorithm that is
1395 * compatible with the type of \c key.
1396 * \param input The message to decrypt.
1397 * \param input_length Size of the \c input buffer in bytes.
1398 * \param salt A salt or label, if supported by the encryption
1399 * algorithm.
1400 * If the algorithm does not support a
1401 * salt, pass \c NULL.
1402 * If the algorithm supports an optional
1403 * salt and you do not want to pass a salt,
1404 * pass \c NULL.
1405 *
1406 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1407 * supported.
1408 * \param salt_length Size of the \c salt buffer in bytes.
1409 * If \c salt is \c NULL, pass 0.
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001410 * \param output Buffer where the decrypted message is to be written.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001411 * \param output_size Size of the \c output buffer in bytes.
1412 * \param output_length On success, the number of bytes
1413 * that make up the returned output.
1414 *
1415 * \retval PSA_SUCCESS
1416 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1417 * The size of the \c output buffer is too small. You can
1418 * determine a sufficient buffer size by calling
1419 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1420 * where \c key_type and \c key_bits are the type and bit-size
1421 * respectively of \c key.
1422 * \retval PSA_ERROR_NOT_SUPPORTED
1423 * \retval PSA_ERROR_INVALID_ARGUMENT
1424 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1425 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1426 * \retval PSA_ERROR_HARDWARE_FAILURE
1427 * \retval PSA_ERROR_TAMPERING_DETECTED
1428 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1429 * \retval PSA_ERROR_INVALID_PADDING
1430 */
1431psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
1432 psa_algorithm_t alg,
1433 const uint8_t *input,
1434 size_t input_length,
1435 const uint8_t *salt,
1436 size_t salt_length,
1437 uint8_t *output,
1438 size_t output_size,
1439 size_t *output_length);
1440
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001441/**@}*/
1442
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001443/** \defgroup generation Key generation
1444 * @{
1445 */
1446
1447/**
1448 * \brief Generate random bytes.
1449 *
1450 * \warning This function **can** fail! Callers MUST check the return status
1451 * and MUST NOT use the content of the output buffer if the return
1452 * status is not #PSA_SUCCESS.
1453 *
1454 * \note To generate a key, use psa_generate_key() instead.
1455 *
1456 * \param output Output buffer for the generated data.
1457 * \param output_size Number of bytes to generate and output.
1458 *
1459 * \retval PSA_SUCCESS
1460 * \retval PSA_ERROR_NOT_SUPPORTED
1461 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1462 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1463 * \retval PSA_ERROR_HARDWARE_FAILURE
1464 * \retval PSA_ERROR_TAMPERING_DETECTED
1465 */
1466psa_status_t psa_generate_random(uint8_t *output,
1467 size_t output_size);
1468
1469/**
1470 * \brief Generate a key or key pair.
1471 *
1472 * \param key Slot where the key will be stored. This must be a
1473 * valid slot for a key of the chosen type. It must
1474 * be unoccupied.
1475 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
1476 * \param bits Key size in bits.
1477 * \param parameters Extra parameters for key generation. The interpretation
1478 * of this parameter depends on \c type. All types support
1479 * \c NULL to use default parameters specified below.
1480 *
1481 * For any symmetric key type (type such that
1482 * `PSA_KEY_TYPE_IS_ASYMMETRIC(type)` is false), \c parameters must be
1483 * \c NULL. For asymmetric key types defined by this specification,
1484 * the parameter type and the default parameters are defined by the
1485 * table below. For vendor-defined key types, the vendor documentation
1486 * shall define the parameter type and the default parameters.
1487 *
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001488 * Type | Parameter type | Meaning | Parameters used if `parameters == NULL`
1489 * ---- | -------------- | ------- | ---------------------------------------
1490 * `PSA_KEY_TYPE_RSA_KEYPAIR` | `unsigned int` | Public exponent | 65537
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001491 *
1492 * \retval PSA_SUCCESS
1493 * \retval PSA_ERROR_NOT_SUPPORTED
1494 * \retval PSA_ERROR_INVALID_ARGUMENT
1495 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1496 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1497 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1498 * \retval PSA_ERROR_HARDWARE_FAILURE
1499 * \retval PSA_ERROR_TAMPERING_DETECTED
1500 */
1501psa_status_t psa_generate_key(psa_key_slot_t key,
1502 psa_key_type_t type,
1503 size_t bits,
1504 const void *parameters);
1505
1506/**@}*/
1507
Gilles Peskinee59236f2018-01-27 23:32:46 +01001508#ifdef __cplusplus
1509}
1510#endif
1511
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001512/* The file "crypto_struct.h" contains definitions for
1513 * implementation-specific structs that are declared above. */
1514#include "crypto_struct.h"
1515
1516/* The file "crypto_extra.h" contains vendor-specific definitions. This
1517 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01001518#include "crypto_extra.h"
1519
1520#endif /* PSA_CRYPTO_H */