blob: c45fccd4bc1b69e9f0574de9c94022ed84df3642 [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 *
Gilles Peskine36a74b72018-06-01 16:30:32 +0200713 * \param operation The operation object to use.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100714 * \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 *
Gilles Peskine36a74b72018-06-01 16:30:32 +0200906 * \param operation The operation object to use.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100907 * \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 *
Gilles Peskine36a74b72018-06-01 16:30:32 +0200982 * \param operation The operation object to use.
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100983 * \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 *
Gilles Peskine36a74b72018-06-01 16:30:32 +02001029 * \param operation The operation object to use.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001030 * \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 Peskine1e7d8f12018-06-01 16:29:38 +02001075
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001076// This macro calculates the encryption output size according to given algorithm
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001077#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) \
1078 ((alg) == PSA_ALG_GCM ? (plaintext_length) + 16 : \
1079 (alg) == PSA_ALG_CCM ? (plaintext_length) + 16 : \
1080 0)
1081
1082/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001083 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001084 * \param key Slot containing the key to use.
1085 * \param alg The AEAD algorithm to compute
1086 * (\c PSA_ALG_XXX value such that
1087 * #PSA_ALG_IS_AEAD(alg) is true).
1088 * \param nonce Nonce or IV to use.
1089 * \param nonce_length Size of the \p nonce buffer in bytes.
1090 * \param additional_data Additional data that will be authenticated
1091 * but not encrypted.
1092 * \param additional_data_length Size of \p additional_data in bytes.
1093 * \param plaintext Data that will be authenticated and
1094 * encrypted.
1095 * \param plaintext_length Size of \p plaintext in bytes.
1096 * \param ciphertext Output buffer for the authenticated and
1097 * encrypted data. The additional data is not
1098 * part of this output. For algorithms where the
1099 * encrypted data and the authentication tag
1100 * are defined as separate outputs, the
1101 * authentication tag is appended to the
1102 * encrypted data.
1103 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1104 * This must be at least
1105 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1106 * \p plaintext_length).
1107 * \param ciphertext_length On success, the size of the output
1108 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001109 *
1110 * \retval PSA_SUCCESS
1111 * Success.
1112 * \retval PSA_ERROR_EMPTY_SLOT
1113 * \retval PSA_ERROR_NOT_PERMITTED
1114 * \retval PSA_ERROR_INVALID_ARGUMENT
1115 * \c key is not compatible with \c alg.
1116 * \retval PSA_ERROR_NOT_SUPPORTED
Gilles Peskine19067982018-03-20 17:54:53 +01001117 * \c alg is not supported or is not an AEAD algorithm.
Gilles Peskine3b555712018-03-03 21:27:57 +01001118 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1119 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1120 * \retval PSA_ERROR_HARDWARE_FAILURE
1121 * \retval PSA_ERROR_TAMPERING_DETECTED
1122 */
mohammad160339ee8712018-04-26 00:51:02 +03001123psa_status_t psa_aead_encrypt( psa_key_slot_t key,
1124 psa_algorithm_t alg,
1125 const uint8_t *nonce,
1126 size_t nonce_length,
1127 const uint8_t *additional_data,
1128 size_t additional_data_length,
1129 const uint8_t *plaintext,
1130 size_t plaintext_length,
1131 uint8_t *ciphertext,
1132 size_t ciphertext_size,
1133 size_t *ciphertext_length );
Gilles Peskine3b555712018-03-03 21:27:57 +01001134
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001135// This macro calculates the decryption output size according to given algorithm
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001136#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \
1137 ((alg) == PSA_ALG_GCM ? (ciphertext_length) - 16 : \
1138 (alg) == PSA_ALG_CCM ? (ciphertext_length) - 16 : \
1139 0)
1140
1141/** Process an authenticated decryption operation.
1142 *
1143 * \param key Slot containing the key to use.
1144 * \param alg The AEAD algorithm to compute
1145 * (\c PSA_ALG_XXX value such that
1146 * #PSA_ALG_IS_AEAD(alg) is true).
1147 * \param nonce Nonce or IV to use.
1148 * \param nonce_length Size of the \p nonce buffer in bytes.
1149 * \param additional_data Additional data that has been authenticated
1150 * but not encrypted.
1151 * \param additional_data_length Size of \p additional_data in bytes.
1152 * \param ciphertext Data that has been authenticated and
1153 * encrypted. For algorithms where the
1154 * encrypted data and the authentication tag
1155 * are defined as separate inputs, the buffer
1156 * must contain the encrypted data followed
1157 * by the authentication tag.
1158 * \param ciphertext_length Size of \p ciphertext in bytes.
1159 * \param plaintext Output buffer for the decrypted data.
1160 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1161 * This must be at least
1162 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1163 * \p ciphertext_length).
1164 * \param plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001165 * in the \b plaintext buffer.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001166 *
1167 * \retval PSA_SUCCESS
1168 * Success.
1169 * \retval PSA_ERROR_EMPTY_SLOT
1170 * \retval PSA_ERROR_INVALID_SIGNATURE
1171 * The ciphertext is not authentic.
1172 * \retval PSA_ERROR_NOT_PERMITTED
1173 * \retval PSA_ERROR_INVALID_ARGUMENT
1174 * \c key is not compatible with \c alg.
1175 * \retval PSA_ERROR_NOT_SUPPORTED
1176 * \c alg is not supported or is not an AEAD algorithm.
1177 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1178 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1179 * \retval PSA_ERROR_HARDWARE_FAILURE
1180 * \retval PSA_ERROR_TAMPERING_DETECTED
1181 */
mohammad160339ee8712018-04-26 00:51:02 +03001182psa_status_t psa_aead_decrypt( psa_key_slot_t key,
1183 psa_algorithm_t alg,
1184 const uint8_t *nonce,
1185 size_t nonce_length,
1186 const uint8_t *additional_data,
1187 size_t additional_data_length,
1188 const uint8_t *ciphertext,
1189 size_t ciphertext_length,
1190 uint8_t *plaintext,
1191 size_t plaintext_size,
1192 size_t *plaintext_length );
Gilles Peskine3b555712018-03-03 21:27:57 +01001193
1194/**@}*/
1195
Gilles Peskine20035e32018-02-03 22:44:14 +01001196/** \defgroup asymmetric Asymmetric cryptography
1197 * @{
1198 */
1199
1200/**
Gilles Peskine0189e752018-02-03 23:57:22 +01001201 * \brief Maximum ECDSA signature size for a given curve bit size
1202 *
1203 * \param curve_bits Curve size in bits
1204 * \return Maximum signature size in bytes
1205 *
1206 * \note This macro returns a compile-time constant if its argument is one.
1207 *
1208 * \warning This macro may evaluate its argument multiple times.
1209 */
1210/*
1211 * RFC 4492 page 20:
1212 *
1213 * Ecdsa-Sig-Value ::= SEQUENCE {
1214 * r INTEGER,
1215 * s INTEGER
1216 * }
1217 *
1218 * Size is at most
1219 * 1 (tag) + 1 (len) + 1 (initial 0) + curve_bytes for each of r and s,
1220 * twice that + 1 (tag) + 2 (len) for the sequence
1221 * (assuming curve_bytes is less than 126 for r and s,
1222 * and less than 124 (total len <= 255) for the sequence)
1223 */
1224#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
1225 ( /*T,L of SEQUENCE*/ ((curve_bits) >= 61 * 8 ? 3 : 2) + \
1226 /*T,L of r,s*/ 2 * (((curve_bits) >= 127 * 8 ? 3 : 2) + \
1227 /*V of r,s*/ ((curve_bits) + 8) / 8))
1228
1229
Gilles Peskine308b91d2018-02-08 09:47:44 +01001230/** Safe signature buffer size for psa_asymmetric_sign().
1231 *
1232 * This macro returns a safe buffer size for a signature using a key
1233 * of the specified type and size, with the specified algorithm.
1234 * Note that the actual size of the signature may be smaller
1235 * (some algorithms produce a variable-size signature).
1236 *
1237 * \warning This function may call its arguments multiple times or
1238 * zero times, so you should not pass arguments that contain
1239 * side effects.
1240 *
1241 * \param key_type An asymmetric key type (this may indifferently be a
1242 * key pair type or a public key type).
1243 * \param key_bits The size of the key in bits.
1244 * \param alg The signature algorithm.
1245 *
1246 * \return If the parameters are valid and supported, return
1247 * a buffer size in bytes that guarantees that
1248 * psa_asymmetric_sign() will not fail with
1249 * #PSA_ERROR_BUFFER_TOO_SMALL.
1250 * If the parameters are a valid combination that is not supported
1251 * by the implementation, this macro either shall return either a
1252 * sensible size or 0.
1253 * If the parameters are not valid, the
1254 * return value is unspecified.
1255 *
1256 */
Gilles Peskine0189e752018-02-03 23:57:22 +01001257#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine2905a7a2018-03-07 16:39:31 +01001258 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
Gilles Peskine0189e752018-02-03 23:57:22 +01001259 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
Gilles Peskine84845652018-03-28 14:17:40 +02001260 ((void)alg, 0))
Gilles Peskine0189e752018-02-03 23:57:22 +01001261
1262/**
Gilles Peskine20035e32018-02-03 22:44:14 +01001263 * \brief Sign a hash or short message with a private key.
1264 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001265 * \param key Key slot containing an asymmetric key pair.
1266 * \param alg A signature algorithm that is compatible with
1267 * the type of \c key.
1268 * \param hash The message to sign.
1269 * \param hash_length Size of the \c hash buffer in bytes.
1270 * \param salt A salt or label, if supported by the signature
1271 * algorithm.
1272 * If the signature algorithm does not support a
1273 * salt, pass \c NULL.
1274 * If the signature algorithm supports an optional
1275 * salt and you do not want to pass a salt,
1276 * pass \c NULL.
1277 * \param salt_length Size of the \c salt buffer in bytes.
1278 * If \c salt is \c NULL, pass 0.
1279 * \param signature Buffer where the signature is to be written.
1280 * \param signature_size Size of the \c signature buffer in bytes.
1281 * \param signature_length On success, the number of bytes
1282 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001283 *
1284 * \retval PSA_SUCCESS
1285 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1286 * The size of the \c signature buffer is too small. You can
1287 * determine a sufficient buffer size by calling
1288 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg)
1289 * where \c key_type and \c key_bits are the type and bit-size
1290 * respectively of \c key.
1291 * \retval PSA_ERROR_NOT_SUPPORTED
1292 * \retval PSA_ERROR_INVALID_ARGUMENT
1293 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1294 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1295 * \retval PSA_ERROR_HARDWARE_FAILURE
1296 * \retval PSA_ERROR_TAMPERING_DETECTED
1297 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01001298 */
1299psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
1300 psa_algorithm_t alg,
1301 const uint8_t *hash,
1302 size_t hash_length,
1303 const uint8_t *salt,
1304 size_t salt_length,
1305 uint8_t *signature,
1306 size_t signature_size,
1307 size_t *signature_length);
1308
1309/**
1310 * \brief Verify the signature a hash or short message using a public key.
1311 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001312 * \param key Key slot containing a public key or an
1313 * asymmetric key pair.
1314 * \param alg A signature algorithm that is compatible with
1315 * the type of \c key.
1316 * \param hash The message whose signature is to be verified.
1317 * \param hash_length Size of the \c hash buffer in bytes.
1318 * \param salt A salt or label, if supported by the signature
1319 * algorithm.
1320 * If the signature algorithm does not support a
1321 * salt, pass \c NULL.
1322 * If the signature algorithm supports an optional
1323 * salt and you do not want to pass a salt,
1324 * pass \c NULL.
1325 * \param salt_length Size of the \c salt buffer in bytes.
1326 * If \c salt is \c NULL, pass 0.
1327 * \param signature Buffer containing the signature to verify.
1328 * \param signature_size Size of the \c signature buffer in bytes.
1329 *
1330 * \retval PSA_SUCCESS
1331 * The signature is valid.
1332 * \retval PSA_ERROR_INVALID_SIGNATURE
1333 * The calculation was perfomed successfully, but the passed
1334 * signature is not a valid signature.
1335 * \retval PSA_ERROR_NOT_SUPPORTED
1336 * \retval PSA_ERROR_INVALID_ARGUMENT
1337 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1338 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1339 * \retval PSA_ERROR_HARDWARE_FAILURE
1340 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01001341 */
1342psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
1343 psa_algorithm_t alg,
1344 const uint8_t *hash,
1345 size_t hash_length,
1346 const uint8_t *salt,
1347 size_t salt_length,
1348 uint8_t *signature,
1349 size_t signature_size);
1350
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001351#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine06297932018-04-11 16:58:22 +02001352 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
1353 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
1354 0)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001355#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
Gilles Peskine06297932018-04-11 16:58:22 +02001356 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
1357 PSA_BITS_TO_BYTES(key_bits) - ((alg) == PSA_ALG_IS_RSA_OAEP_MGF1 ? \
1358 2 * (PSA_ALG_RSA_GET_HASH(alg) + 1) : \
1359 11 /*PKCS#1v1.5*/) : \
1360 0)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001361
1362/**
1363 * \brief Encrypt a short message with a public key.
1364 *
1365 * \param key Key slot containing a public key or an asymmetric
1366 * key pair.
1367 * \param alg An asymmetric encryption algorithm that is
1368 * compatible with the type of \c key.
1369 * \param input The message to encrypt.
1370 * \param input_length Size of the \c input buffer in bytes.
1371 * \param salt A salt or label, if supported by the encryption
1372 * algorithm.
1373 * If the algorithm does not support a
1374 * salt, pass \c NULL.
1375 * If the algorithm supports an optional
1376 * salt and you do not want to pass a salt,
1377 * pass \c NULL.
1378 *
1379 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1380 * supported.
1381 * \param salt_length Size of the \c salt buffer in bytes.
1382 * If \c salt is \c NULL, pass 0.
1383 * \param output Buffer where the encrypted message is to be written.
1384 * \param output_size Size of the \c output buffer in bytes.
1385 * \param output_length On success, the number of bytes
1386 * that make up the returned output.
1387 *
1388 * \retval PSA_SUCCESS
1389 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1390 * The size of the \c output buffer is too small. You can
1391 * determine a sufficient buffer size by calling
1392 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1393 * where \c key_type and \c key_bits are the type and bit-size
1394 * respectively of \c key.
1395 * \retval PSA_ERROR_NOT_SUPPORTED
1396 * \retval PSA_ERROR_INVALID_ARGUMENT
1397 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1398 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1399 * \retval PSA_ERROR_HARDWARE_FAILURE
1400 * \retval PSA_ERROR_TAMPERING_DETECTED
1401 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1402 */
1403psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
1404 psa_algorithm_t alg,
1405 const uint8_t *input,
1406 size_t input_length,
1407 const uint8_t *salt,
1408 size_t salt_length,
1409 uint8_t *output,
1410 size_t output_size,
1411 size_t *output_length);
1412
1413/**
1414 * \brief Decrypt a short message with a private key.
1415 *
1416 * \param key Key slot containing an asymmetric key pair.
1417 * \param alg An asymmetric encryption algorithm that is
1418 * compatible with the type of \c key.
1419 * \param input The message to decrypt.
1420 * \param input_length Size of the \c input buffer in bytes.
1421 * \param salt A salt or label, if supported by the encryption
1422 * algorithm.
1423 * If the algorithm does not support a
1424 * salt, pass \c NULL.
1425 * If the algorithm supports an optional
1426 * salt and you do not want to pass a salt,
1427 * pass \c NULL.
1428 *
1429 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1430 * supported.
1431 * \param salt_length Size of the \c salt buffer in bytes.
1432 * If \c salt is \c NULL, pass 0.
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001433 * \param output Buffer where the decrypted message is to be written.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001434 * \param output_size Size of the \c output buffer in bytes.
1435 * \param output_length On success, the number of bytes
1436 * that make up the returned output.
1437 *
1438 * \retval PSA_SUCCESS
1439 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1440 * The size of the \c output buffer is too small. You can
1441 * determine a sufficient buffer size by calling
1442 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg)
1443 * where \c key_type and \c key_bits are the type and bit-size
1444 * respectively of \c key.
1445 * \retval PSA_ERROR_NOT_SUPPORTED
1446 * \retval PSA_ERROR_INVALID_ARGUMENT
1447 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1448 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1449 * \retval PSA_ERROR_HARDWARE_FAILURE
1450 * \retval PSA_ERROR_TAMPERING_DETECTED
1451 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1452 * \retval PSA_ERROR_INVALID_PADDING
1453 */
1454psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
1455 psa_algorithm_t alg,
1456 const uint8_t *input,
1457 size_t input_length,
1458 const uint8_t *salt,
1459 size_t salt_length,
1460 uint8_t *output,
1461 size_t output_size,
1462 size_t *output_length);
1463
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001464/**@}*/
1465
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001466/** \defgroup generation Key generation
1467 * @{
1468 */
1469
1470/**
1471 * \brief Generate random bytes.
1472 *
1473 * \warning This function **can** fail! Callers MUST check the return status
1474 * and MUST NOT use the content of the output buffer if the return
1475 * status is not #PSA_SUCCESS.
1476 *
1477 * \note To generate a key, use psa_generate_key() instead.
1478 *
1479 * \param output Output buffer for the generated data.
1480 * \param output_size Number of bytes to generate and output.
1481 *
1482 * \retval PSA_SUCCESS
1483 * \retval PSA_ERROR_NOT_SUPPORTED
1484 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1485 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1486 * \retval PSA_ERROR_HARDWARE_FAILURE
1487 * \retval PSA_ERROR_TAMPERING_DETECTED
1488 */
1489psa_status_t psa_generate_random(uint8_t *output,
1490 size_t output_size);
1491
1492/**
1493 * \brief Generate a key or key pair.
1494 *
1495 * \param key Slot where the key will be stored. This must be a
1496 * valid slot for a key of the chosen type. It must
1497 * be unoccupied.
1498 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
1499 * \param bits Key size in bits.
1500 * \param parameters Extra parameters for key generation. The interpretation
1501 * of this parameter depends on \c type. All types support
1502 * \c NULL to use default parameters specified below.
1503 *
1504 * For any symmetric key type (type such that
1505 * `PSA_KEY_TYPE_IS_ASYMMETRIC(type)` is false), \c parameters must be
1506 * \c NULL. For asymmetric key types defined by this specification,
1507 * the parameter type and the default parameters are defined by the
1508 * table below. For vendor-defined key types, the vendor documentation
1509 * shall define the parameter type and the default parameters.
1510 *
Gilles Peskinef48af7f2018-03-28 18:44:14 +02001511 * Type | Parameter type | Meaning | Parameters used if `parameters == NULL`
1512 * ---- | -------------- | ------- | ---------------------------------------
1513 * `PSA_KEY_TYPE_RSA_KEYPAIR` | `unsigned int` | Public exponent | 65537
Gilles Peskine9e7dc712018-03-28 14:18:50 +02001514 *
1515 * \retval PSA_SUCCESS
1516 * \retval PSA_ERROR_NOT_SUPPORTED
1517 * \retval PSA_ERROR_INVALID_ARGUMENT
1518 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1519 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
1520 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1521 * \retval PSA_ERROR_HARDWARE_FAILURE
1522 * \retval PSA_ERROR_TAMPERING_DETECTED
1523 */
1524psa_status_t psa_generate_key(psa_key_slot_t key,
1525 psa_key_type_t type,
1526 size_t bits,
1527 const void *parameters);
1528
1529/**@}*/
1530
Gilles Peskinee59236f2018-01-27 23:32:46 +01001531#ifdef __cplusplus
1532}
1533#endif
1534
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001535/* The file "crypto_struct.h" contains definitions for
1536 * implementation-specific structs that are declared above. */
1537#include "crypto_struct.h"
1538
1539/* The file "crypto_extra.h" contains vendor-specific definitions. This
1540 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01001541#include "crypto_extra.h"
1542
1543#endif /* PSA_CRYPTO_H */