blob: b18e22053f47eb8d13009844dba2437290194fdc [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.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200105 *
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
327#define PSA_ALG_RSA_PKCS1V15_RAW ((psa_algorithm_t)0x10010000)
328#define PSA_ALG_RSA_PSS_MGF1 ((psa_algorithm_t)0x10020000)
329#define PSA_ALG_RSA_OAEP ((psa_algorithm_t)0x12020000)
330#define PSA_ALG_RSA_PKCS1V15(hash_alg) \
331 (PSA_ALG_RSA_PKCS1V15_RAW | ((hash_alg) & PSA_ALG_HASH_MASK))
332#define PSA_ALG_IS_RSA_PKCS1V15(alg) \
Gilles Peskine20035e32018-02-03 22:44:14 +0100333 (((alg) & 0x7fffff00) == PSA_ALG_RSA_PKCS1V15_RAW)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100334#define PSA_ALG_RSA_GET_HASH(alg) \
335 (((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100336
337/**@}*/
338
339/** \defgroup key_management Key management
340 * @{
341 */
342
343/**
344 * \brief Import a key in binary format.
345 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100346 * This function supports any output from psa_export_key(). Refer to the
347 * documentation of psa_export_key() for the format for each key type.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100348 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100349 * \param key Slot where the key will be stored. This must be a
350 * valid slot for a key of the chosen type. It must
351 * be unoccupied.
352 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
353 * \param data Buffer containing the key data.
354 * \param data_length Size of the \c data buffer in bytes.
355 *
356 * \retval PSA_SUCCESS
357 * Success.
358 * \retval PSA_ERROR_NOT_SUPPORTED
359 * The key type or key size is not supported.
360 * \retval PSA_ERROR_INVALID_ARGUMENT
361 * The key slot is invalid,
362 * or the key data is not correctly formatted.
363 * \retval PSA_ERROR_OCCUPIED_SLOT
364 There is already a key in the specified slot.
365 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
366 * \retval PSA_ERROR_COMMUNICATION_FAILURE
367 * \retval PSA_ERROR_HARDWARE_FAILURE
368 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100369 */
370psa_status_t psa_import_key(psa_key_slot_t key,
371 psa_key_type_t type,
372 const uint8_t *data,
373 size_t data_length);
374
375/**
376 * \brief Destroy a key.
377 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100378 * \retval PSA_SUCCESS
379 * \retval PSA_ERROR_EMPTY_SLOT
380 * \retval PSA_ERROR_COMMUNICATION_FAILURE
381 * \retval PSA_ERROR_HARDWARE_FAILURE
382 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100383 */
384psa_status_t psa_destroy_key(psa_key_slot_t key);
385
386/**
387 * \brief Get basic metadata about a key.
388 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100389 * \param key Slot whose content is queried. This must
390 * be an occupied key slot.
391 * \param type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
392 * This may be a null pointer, in which case the key type
393 * is not written.
394 * \param bits On success, the key size in bits.
395 * This may be a null pointer, in which case the key type
396 * is not written.
397 *
398 * \retval PSA_SUCCESS
399 * \retval PSA_ERROR_EMPTY_SLOT
400 * \retval PSA_ERROR_COMMUNICATION_FAILURE
401 * \retval PSA_ERROR_HARDWARE_FAILURE
402 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100403 */
404psa_status_t psa_get_key_information(psa_key_slot_t key,
405 psa_key_type_t *type,
406 size_t *bits);
407
408/**
409 * \brief Export a key in binary format.
410 *
411 * The output of this function can be passed to psa_import_key() to
412 * create an equivalent object.
413 *
414 * If a key is created with psa_import_key() and then exported with
415 * this function, it is not guaranteed that the resulting data is
416 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +0100417 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100418 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100419 * For standard key types, the output format is as follows:
420 *
421 * - For symmetric keys (including MAC keys), the format is the
422 * raw bytes of the key.
423 * - For DES, the key data consists of 8 bytes. The parity bits must be
424 * correct.
425 * - For Triple-DES, the format is the concatenation of the
426 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100427 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine308b91d2018-02-08 09:47:44 +0100428 * is the non-encrypted DER representation defined by PKCS\#8 (RFC 5208)
429 * as PrivateKeyInfo.
430 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
Gilles Peskine971f7062018-03-20 17:52:58 +0100431 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100432 *
433 * \param key Slot whose content is to be exported. This must
434 * be an occupied key slot.
435 * \param data Buffer where the key data is to be written.
436 * \param data_size Size of the \c data buffer in bytes.
437 * \param data_length On success, the number of bytes
438 * that make up the key data.
439 *
440 * \retval PSA_SUCCESS
441 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine92b30732018-03-03 21:29:30 +0100442 * \retval PSA_ERROR_NOT_PERMITTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100443 * \retval PSA_ERROR_COMMUNICATION_FAILURE
444 * \retval PSA_ERROR_HARDWARE_FAILURE
445 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100446 */
447psa_status_t psa_export_key(psa_key_slot_t key,
448 uint8_t *data,
449 size_t data_size,
450 size_t *data_length);
451
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100452/**
453 * \brief Export a public key or the public part of a key pair in binary format.
454 *
455 * The output of this function can be passed to psa_import_key() to
456 * create an object that is equivalent to the public key.
457 *
458 * For standard key types, the output format is as follows:
459 *
460 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
Gilles Peskine971f7062018-03-20 17:52:58 +0100461 * is the DER representation of the public key defined by RFC 5280
462 * as SubjectPublicKeyInfo.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100463 *
464 * \param key Slot whose content is to be exported. This must
465 * be an occupied key slot.
466 * \param data Buffer where the key data is to be written.
467 * \param data_size Size of the \c data buffer in bytes.
468 * \param data_length On success, the number of bytes
469 * that make up the key data.
470 *
471 * \retval PSA_SUCCESS
472 * \retval PSA_ERROR_EMPTY_SLOT
473 * \retval PSA_ERROR_INVALID_ARGUMENT
474 * \retval PSA_ERROR_COMMUNICATION_FAILURE
475 * \retval PSA_ERROR_HARDWARE_FAILURE
476 * \retval PSA_ERROR_TAMPERING_DETECTED
477 */
478psa_status_t psa_export_public_key(psa_key_slot_t key,
479 uint8_t *data,
480 size_t data_size,
481 size_t *data_length);
482
483/**@}*/
484
485/** \defgroup policy Key policies
486 * @{
487 */
488
489/** \brief Encoding of permitted usage on a key. */
490typedef uint32_t psa_key_usage_t;
491
Gilles Peskine7e198532018-03-08 07:50:30 +0100492/** Whether the key may be exported.
493 *
494 * A public key or the public part of a key pair may always be exported
495 * regardless of the value of this permission flag.
496 *
497 * If a key does not have export permission, implementations shall not
498 * allow the key to be exported in plain form from the cryptoprocessor,
499 * whether through psa_export_key() or through a proprietary interface.
500 * The key may however be exportable in a wrapped form, i.e. in a form
501 * where it is encrypted by another key.
502 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100503#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
504
Gilles Peskine7e198532018-03-08 07:50:30 +0100505/** Whether the key may be used to encrypt a message.
506 *
507 * For a key pair, this concerns the public key.
508 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100509#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +0100510
511/** Whether the key may be used to decrypt a message.
512 *
513 * For a key pair, this concerns the private key.
514 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100515#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +0100516
517/** Whether the key may be used to sign 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_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +0100522
523/** Whether the key may be used to verify a message signature.
524 *
525 * For a key pair, this concerns the public key.
526 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100527#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
528
529/** The type of the key policy data structure.
530 *
531 * This is an implementation-defined \c struct. Applications should not
532 * make any assumptions about the content of this structure except
533 * as directed by the documentation of a specific implementation. */
534typedef struct psa_key_policy_s psa_key_policy_t;
535
536/** \brief Initialize a key policy structure to a default that forbids all
537 * usage of the key. */
538void psa_key_policy_init(psa_key_policy_t *policy);
539
Gilles Peskine7e198532018-03-08 07:50:30 +0100540/** \brief Set the standard fields of a policy structure.
541 *
542 * Note that this function does not make any consistency check of the
543 * parameters. The values are only checked when applying the policy to
544 * a key slot with psa_set_key_policy().
545 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100546void psa_key_policy_set_usage(psa_key_policy_t *policy,
547 psa_key_usage_t usage,
548 psa_algorithm_t alg);
549
550psa_key_usage_t psa_key_policy_get_usage(psa_key_policy_t *policy);
551
552psa_algorithm_t psa_key_policy_get_algorithm(psa_key_policy_t *policy);
553
554/** \brief Set the usage policy on a key slot.
555 *
556 * This function must be called on an empty key slot, before importing,
557 * generating or creating a key in the slot. Changing the policy of an
558 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100559 *
560 * Implementations may set restrictions on supported key policies
561 * depending on the key type and the key slot.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100562 */
563psa_status_t psa_set_key_policy(psa_key_slot_t key,
564 const psa_key_policy_t *policy);
565
Gilles Peskine7e198532018-03-08 07:50:30 +0100566/** \brief Get the usage policy for a key slot.
567 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100568psa_status_t psa_get_key_policy(psa_key_slot_t key,
569 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100570
571/**@}*/
572
Gilles Peskine609b6a52018-03-03 21:31:50 +0100573/** \defgroup persistence Key lifetime
574 * @{
575 */
576
577/** Encoding of key lifetimes.
578 */
579typedef uint32_t psa_key_lifetime_t;
580
581/** A volatile key slot retains its content as long as the application is
582 * running. It is guaranteed to be erased on a power reset.
583 */
mohammad16031c345452018-04-16 06:49:13 -0700584#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
Gilles Peskine609b6a52018-03-03 21:31:50 +0100585
586/** A persistent key slot retains its content as long as it is not explicitly
587 * destroyed.
588 */
mohammad16031c345452018-04-16 06:49:13 -0700589#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
Gilles Peskine609b6a52018-03-03 21:31:50 +0100590
591/** A write-once key slot may not be modified once a key has been set.
592 * It will retain its content as long as the device remains operational.
593 */
594#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
595
Gilles Peskined393e182018-03-08 07:49:16 +0100596/** \brief Retrieve the lifetime of a key slot.
597 *
598 * The assignment of lifetimes to slots is implementation-dependent.
mohammad1603804cd712018-03-20 22:44:08 +0200599 * \param key Slot whose content is to be exported. This must
600 * be an occupied key slot.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200601 *
mohammad1603804cd712018-03-20 22:44:08 +0200602 * \param lifetime On success, the lifetime value.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200603 *
mohammad1603804cd712018-03-20 22:44:08 +0200604 * \retval PSA_SUCCESS
605 * Success.
606 * \retval PSA_ERROR_INVALID_ARGUMENT
mohammad1603a7d245a2018-04-17 00:40:08 -0700607 * The key slot is invalid.
Gilles Peskined393e182018-03-08 07:49:16 +0100608 */
Gilles Peskine609b6a52018-03-03 21:31:50 +0100609psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
610 psa_key_lifetime_t *lifetime);
611
Gilles Peskined393e182018-03-08 07:49:16 +0100612/** \brief Change the lifetime of a key slot.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200613 *
mohammad1603ba178512018-03-21 04:35:20 -0700614 * Whether the lifetime of a key slot can be changed at all, and if so
615 * whether the lifetime of an occupied key slot can be changed, is
616 * implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200617 *
mohammad1603804cd712018-03-20 22:44:08 +0200618 * \param key Slot whose content is to be exported. This must
619 * be an occupied key slot.
620 * \param lifetime The lifetime value to be set for the given key.
Gilles Peskine8ca56022018-04-17 14:07:59 +0200621 *
mohammad1603804cd712018-03-20 22:44:08 +0200622 * \retval PSA_SUCCESS
623 * Success.
624 * \retval PSA_ERROR_INVALID_ARGUMENT
625 * The key slot is invalid,
mohammad1603a7d245a2018-04-17 00:40:08 -0700626 * or the lifetime value is invalid.
Gilles Peskined393e182018-03-08 07:49:16 +0100627 */
628psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
mohammad1603ea050092018-04-17 00:31:34 -0700629 psa_key_lifetime_t lifetime);
Gilles Peskined393e182018-03-08 07:49:16 +0100630
Gilles Peskine609b6a52018-03-03 21:31:50 +0100631/**@}*/
632
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100633/** \defgroup hash Message digests
634 * @{
635 */
636
Gilles Peskine308b91d2018-02-08 09:47:44 +0100637/** The type of the state data structure for multipart hash operations.
638 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100639 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100640 * make any assumptions about the content of this structure except
641 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100642typedef struct psa_hash_operation_s psa_hash_operation_t;
643
Gilles Peskine308b91d2018-02-08 09:47:44 +0100644/** The size of the output of psa_hash_finish(), in bytes.
645 *
646 * This is also the hash size that psa_hash_verify() expects.
647 *
648 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
649 * #PSA_ALG_IS_HASH(alg) is true).
650 *
651 * \return The hash size for the specified hash algorithm.
652 * If the hash algorithm is not recognized, return 0.
653 * An implementation may return either 0 or the correct size
654 * for a hash algorithm that it recognizes, but does not support.
655 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100656#define PSA_HASH_FINAL_SIZE(alg) \
657 ( \
658 (alg) == PSA_ALG_MD2 ? 16 : \
659 (alg) == PSA_ALG_MD4 ? 16 : \
660 (alg) == PSA_ALG_MD5 ? 16 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100661 (alg) == PSA_ALG_RIPEMD160 ? 20 : \
662 (alg) == PSA_ALG_SHA_1 ? 20 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100663 (alg) == PSA_ALG_SHA_224 ? 28 : \
664 (alg) == PSA_ALG_SHA_256 ? 32 : \
665 (alg) == PSA_ALG_SHA_384 ? 48 : \
666 (alg) == PSA_ALG_SHA_512 ? 64 : \
667 (alg) == PSA_ALG_SHA_512_224 ? 28 : \
668 (alg) == PSA_ALG_SHA_512_256 ? 32 : \
669 (alg) == PSA_ALG_SHA3_224 ? 28 : \
670 (alg) == PSA_ALG_SHA3_256 ? 32 : \
671 (alg) == PSA_ALG_SHA3_384 ? 48 : \
672 (alg) == PSA_ALG_SHA3_512 ? 64 : \
673 0)
674
Gilles Peskine308b91d2018-02-08 09:47:44 +0100675/** Start a multipart hash operation.
676 *
677 * The sequence of operations to calculate a hash (message digest)
678 * is as follows:
679 * -# Allocate an operation object which will be passed to all the functions
680 * listed here.
681 * -# Call psa_hash_start() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100682 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100683 * of the message each time. The hash that is calculated is the hash
684 * of the concatenation of these messages in order.
685 * -# To calculate the hash, call psa_hash_finish().
686 * To compare the hash with an expected value, call psa_hash_verify().
687 *
688 * The application may call psa_hash_abort() at any time after the operation
689 * has been initialized with psa_hash_start().
690 *
691 * After a successful call to psa_hash_start(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100692 * eventually terminate the operation. The following events terminate an
693 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100694 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100695 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100696 *
697 * \param operation
698 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
699 * such that #PSA_ALG_IS_HASH(alg) is true).
700 *
701 * \retval PSA_SUCCESS
702 * Success.
703 * \retval PSA_ERROR_NOT_SUPPORTED
704 * \c alg is not supported or is not a hash algorithm.
705 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
706 * \retval PSA_ERROR_COMMUNICATION_FAILURE
707 * \retval PSA_ERROR_HARDWARE_FAILURE
708 * \retval PSA_ERROR_TAMPERING_DETECTED
709 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100710psa_status_t psa_hash_start(psa_hash_operation_t *operation,
711 psa_algorithm_t alg);
712
Gilles Peskine308b91d2018-02-08 09:47:44 +0100713/** Add a message fragment to a multipart hash operation.
714 *
715 * The application must call psa_hash_start() before calling this function.
716 *
717 * If this function returns an error status, the operation becomes inactive.
718 *
719 * \param operation Active hash operation.
720 * \param input Buffer containing the message fragment to hash.
721 * \param input_length Size of the \c input buffer in bytes.
722 *
723 * \retval PSA_SUCCESS
724 * Success.
725 * \retval PSA_ERROR_BAD_STATE
726 * The operation state is not valid (not started, or already completed).
727 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
728 * \retval PSA_ERROR_COMMUNICATION_FAILURE
729 * \retval PSA_ERROR_HARDWARE_FAILURE
730 * \retval PSA_ERROR_TAMPERING_DETECTED
731 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100732psa_status_t psa_hash_update(psa_hash_operation_t *operation,
733 const uint8_t *input,
734 size_t input_length);
735
Gilles Peskine308b91d2018-02-08 09:47:44 +0100736/** Finish the calculation of the hash of a message.
737 *
738 * The application must call psa_hash_start() before calling this function.
739 * This function calculates the hash of the message formed by concatenating
740 * the inputs passed to preceding calls to psa_hash_update().
741 *
742 * When this function returns, the operation becomes inactive.
743 *
744 * \warning Applications should not call this function if they expect
745 * a specific value for the hash. Call psa_hash_verify() instead.
746 * Beware that comparing integrity or authenticity data such as
747 * hash values with a function such as \c memcmp is risky
748 * because the time taken by the comparison may leak information
749 * about the hashed data which could allow an attacker to guess
750 * a valid hash and thereby bypass security controls.
751 *
752 * \param operation Active hash operation.
753 * \param hash Buffer where the hash is to be written.
754 * \param hash_size Size of the \c hash buffer in bytes.
755 * \param hash_length On success, the number of bytes
756 * that make up the hash value. This is always
757 * #PSA_HASH_FINAL_SIZE(alg) where \c alg is the
758 * hash algorithm that is calculated.
759 *
760 * \retval PSA_SUCCESS
761 * Success.
762 * \retval PSA_ERROR_BAD_STATE
763 * The operation state is not valid (not started, or already completed).
764 * \retval PSA_ERROR_BUFFER_TOO_SMALL
765 * The size of the \c hash buffer is too small. You can determine a
766 * sufficient buffer size by calling #PSA_HASH_FINAL_SIZE(alg)
767 * where \c alg is the hash algorithm that is calculated.
768 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
769 * \retval PSA_ERROR_COMMUNICATION_FAILURE
770 * \retval PSA_ERROR_HARDWARE_FAILURE
771 * \retval PSA_ERROR_TAMPERING_DETECTED
772 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100773psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
774 uint8_t *hash,
775 size_t hash_size,
776 size_t *hash_length);
777
Gilles Peskine308b91d2018-02-08 09:47:44 +0100778/** Finish the calculation of the hash of a message and compare it with
779 * an expected value.
780 *
781 * The application must call psa_hash_start() before calling this function.
782 * This function calculates the hash of the message formed by concatenating
783 * the inputs passed to preceding calls to psa_hash_update(). It then
784 * compares the calculated hash with the expected hash passed as a
785 * parameter to this function.
786 *
787 * When this function returns, the operation becomes inactive.
788 *
Gilles Peskine19067982018-03-20 17:54:53 +0100789 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100790 * comparison between the actual hash and the expected hash is performed
791 * in constant time.
792 *
793 * \param operation Active hash operation.
794 * \param hash Buffer containing the expected hash value.
795 * \param hash_length Size of the \c hash buffer in bytes.
796 *
797 * \retval PSA_SUCCESS
798 * The expected hash is identical to the actual hash of the message.
799 * \retval PSA_ERROR_INVALID_SIGNATURE
800 * The hash of the message was calculated successfully, but it
801 * differs from the expected hash.
802 * \retval PSA_ERROR_BAD_STATE
803 * The operation state is not valid (not started, or already completed).
804 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
805 * \retval PSA_ERROR_COMMUNICATION_FAILURE
806 * \retval PSA_ERROR_HARDWARE_FAILURE
807 * \retval PSA_ERROR_TAMPERING_DETECTED
808 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100809psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
810 const uint8_t *hash,
811 size_t hash_length);
812
Gilles Peskine308b91d2018-02-08 09:47:44 +0100813/** Abort a hash operation.
814 *
815 * This function may be called at any time after psa_hash_start().
816 * Aborting an operation frees all associated resources except for the
817 * \c operation structure itself.
818 *
819 * Implementation should strive to be robust and handle inactive hash
820 * operations safely (do nothing and return #PSA_ERROR_BAD_STATE). However,
821 * application writers should beware that uninitialized memory may happen
822 * to be indistinguishable from an active hash operation, and the behavior
823 * of psa_hash_abort() is undefined in this case.
824 *
825 * \param operation Active hash operation.
826 *
827 * \retval PSA_SUCCESS
828 * \retval PSA_ERROR_BAD_STATE
829 * \c operation is not an active hash operation.
830 * \retval PSA_ERROR_COMMUNICATION_FAILURE
831 * \retval PSA_ERROR_HARDWARE_FAILURE
832 * \retval PSA_ERROR_TAMPERING_DETECTED
833 */
834psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100835
836/**@}*/
837
Gilles Peskine8c9def32018-02-08 10:02:12 +0100838/** \defgroup MAC Message authentication codes
839 * @{
840 */
841
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100842/** The type of the state data structure for multipart MAC operations.
843 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100844 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100845 * make any assumptions about the content of this structure except
846 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100847typedef struct psa_mac_operation_s psa_mac_operation_t;
848
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100849/** The size of the output of psa_mac_finish(), in bytes.
850 *
851 * This is also the MAC size that psa_mac_verify() expects.
852 *
853 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
854 * #PSA_ALG_IS_MAC(alg) is true).
855 *
856 * \return The MAC size for the specified algorithm.
857 * If the MAC algorithm is not recognized, return 0.
858 * An implementation may return either 0 or the correct size
859 * for a MAC algorithm that it recognizes, but does not support.
860 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100861#define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \
862 (PSA_ALG_IS_HMAC(alg) ? PSA_HASH_FINAL_SIZE(PSA_ALG_HMAC_HASH(alg)) : \
863 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
864 0)
865
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100866/** Start a multipart MAC operation.
867 *
868 * The sequence of operations to calculate a MAC (message authentication code)
869 * is as follows:
870 * -# Allocate an operation object which will be passed to all the functions
871 * listed here.
872 * -# Call psa_mac_start() to specify the algorithm and key.
873 * The key remains associated with the operation even if the content
874 * of the key slot changes.
875 * -# Call psa_mac_update() zero, one or more times, passing a fragment
876 * of the message each time. The MAC that is calculated is the MAC
877 * of the concatenation of these messages in order.
878 * -# To calculate the MAC, call psa_mac_finish().
879 * To compare the MAC with an expected value, call psa_mac_verify().
880 *
881 * The application may call psa_mac_abort() at any time after the operation
882 * has been initialized with psa_mac_start().
883 *
884 * After a successful call to psa_mac_start(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100885 * eventually terminate the operation. The following events terminate an
886 * operation:
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100887 * - A failed call to psa_mac_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100888 * - A call to psa_mac_finish(), psa_mac_verify() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100889 *
890 * \param operation
891 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
892 * such that #PSA_ALG_IS_MAC(alg) is true).
893 *
894 * \retval PSA_SUCCESS
895 * Success.
896 * \retval PSA_ERROR_EMPTY_SLOT
Gilles Peskine92b30732018-03-03 21:29:30 +0100897 * \retval PSA_ERROR_NOT_PERMITTED
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100898 * \retval PSA_ERROR_INVALID_ARGUMENT
899 * \c key is not compatible with \c alg.
900 * \retval PSA_ERROR_NOT_SUPPORTED
901 * \c alg is not supported or is not a MAC algorithm.
902 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
903 * \retval PSA_ERROR_COMMUNICATION_FAILURE
904 * \retval PSA_ERROR_HARDWARE_FAILURE
905 * \retval PSA_ERROR_TAMPERING_DETECTED
906 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100907psa_status_t psa_mac_start(psa_mac_operation_t *operation,
908 psa_key_slot_t key,
909 psa_algorithm_t alg);
910
911psa_status_t psa_mac_update(psa_mac_operation_t *operation,
912 const uint8_t *input,
913 size_t input_length);
914
915psa_status_t psa_mac_finish(psa_mac_operation_t *operation,
916 uint8_t *mac,
917 size_t mac_size,
918 size_t *mac_length);
919
920psa_status_t psa_mac_verify(psa_mac_operation_t *operation,
921 const uint8_t *mac,
922 size_t mac_length);
923
924psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
925
926/**@}*/
927
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100928/** \defgroup cipher Symmetric ciphers
929 * @{
930 */
931
932/** The type of the state data structure for multipart cipher operations.
933 *
934 * This is an implementation-defined \c struct. Applications should not
935 * make any assumptions about the content of this structure except
936 * as directed by the documentation of a specific implementation. */
937typedef struct psa_cipher_operation_s psa_cipher_operation_t;
938
939/** Set the key for a multipart symmetric encryption operation.
940 *
941 * The sequence of operations to encrypt a message with a symmetric cipher
942 * is as follows:
943 * -# Allocate an operation object which will be passed to all the functions
944 * listed here.
945 * -# Call psa_encrypt_setup() to specify the algorithm and key.
946 * The key remains associated with the operation even if the content
947 * of the key slot changes.
948 * -# Call either psa_encrypt_generate_iv() or psa_encrypt_set_iv() to
949 * generate or set the IV (initialization vector). You should use
950 * psa_encrypt_generate_iv() unless the protocol you are implementing
951 * requires a specific IV value.
952 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
953 * of the message each time.
954 * -# Call psa_cipher_finish().
955 *
956 * The application may call psa_cipher_abort() at any time after the operation
957 * has been initialized with psa_encrypt_setup().
958 *
959 * After a successful call to psa_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100960 * eventually terminate the operation. The following events terminate an
961 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100962 * - A failed call to psa_encrypt_generate_iv(), psa_encrypt_set_iv()
963 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100964 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100965 *
966 * \param operation
967 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
968 * such that #PSA_ALG_IS_CIPHER(alg) is true).
969 *
970 * \retval PSA_SUCCESS
971 * Success.
972 * \retval PSA_ERROR_EMPTY_SLOT
973 * \retval PSA_ERROR_NOT_PERMITTED
974 * \retval PSA_ERROR_INVALID_ARGUMENT
975 * \c key is not compatible with \c alg.
976 * \retval PSA_ERROR_NOT_SUPPORTED
977 * \c alg is not supported or is not a cipher algorithm.
978 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
979 * \retval PSA_ERROR_COMMUNICATION_FAILURE
980 * \retval PSA_ERROR_HARDWARE_FAILURE
981 * \retval PSA_ERROR_TAMPERING_DETECTED
982 */
983psa_status_t psa_encrypt_setup(psa_cipher_operation_t *operation,
984 psa_key_slot_t key,
985 psa_algorithm_t alg);
986
987/** Set the key for a multipart symmetric decryption operation.
988 *
989 * The sequence of operations to decrypt a message with a symmetric cipher
990 * is as follows:
991 * -# Allocate an operation object which will be passed to all the functions
992 * listed here.
993 * -# Call psa_decrypt_setup() to specify the algorithm and key.
994 * The key remains associated with the operation even if the content
995 * of the key slot changes.
996 * -# Call psa_cipher_update() with the IV (initialization vector) for the
997 * decryption. If the IV is prepended to the ciphertext, you can call
998 * psa_cipher_update() on a buffer containing the IV followed by the
999 * beginning of the message.
1000 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1001 * of the message each time.
1002 * -# Call psa_cipher_finish().
1003 *
1004 * The application may call psa_cipher_abort() at any time after the operation
1005 * has been initialized with psa_encrypt_setup().
1006 *
1007 * After a successful call to psa_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001008 * eventually terminate the operation. The following events terminate an
1009 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001010 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001011 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001012 *
1013 * \param operation
1014 * \param alg The cipher algorithm to compute (\c PSA_ALG_XXX value
1015 * such that #PSA_ALG_IS_CIPHER(alg) is true).
1016 *
1017 * \retval PSA_SUCCESS
1018 * Success.
1019 * \retval PSA_ERROR_EMPTY_SLOT
1020 * \retval PSA_ERROR_NOT_PERMITTED
1021 * \retval PSA_ERROR_INVALID_ARGUMENT
1022 * \c key is not compatible with \c alg.
1023 * \retval PSA_ERROR_NOT_SUPPORTED
1024 * \c alg is not supported or is not a cipher algorithm.
1025 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1026 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1027 * \retval PSA_ERROR_HARDWARE_FAILURE
1028 * \retval PSA_ERROR_TAMPERING_DETECTED
1029 */
1030psa_status_t psa_decrypt_setup(psa_cipher_operation_t *operation,
1031 psa_key_slot_t key,
1032 psa_algorithm_t alg);
1033
1034psa_status_t psa_encrypt_generate_iv(psa_cipher_operation_t *operation,
1035 unsigned char *iv,
1036 size_t iv_size,
1037 size_t *iv_length);
1038
1039psa_status_t psa_encrypt_set_iv(psa_cipher_operation_t *operation,
1040 const unsigned char *iv,
1041 size_t iv_length);
1042
1043psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1044 const uint8_t *input,
1045 size_t input_length);
1046
1047psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
1048 uint8_t *mac,
1049 size_t mac_size,
1050 size_t *mac_length);
1051
1052psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1053
1054/**@}*/
1055
Gilles Peskine3b555712018-03-03 21:27:57 +01001056/** \defgroup aead Authenticated encryption with associated data (AEAD)
1057 * @{
1058 */
1059
1060/** The type of the state data structure for multipart AEAD operations.
1061 *
1062 * This is an implementation-defined \c struct. Applications should not
1063 * make any assumptions about the content of this structure except
1064 * as directed by the documentation of a specific implementation. */
1065typedef struct psa_aead_operation_s psa_aead_operation_t;
1066
1067/** Set the key for a multipart authenticated encryption operation.
1068 *
1069 * The sequence of operations to authenticate-and-encrypt a message
1070 * is as follows:
1071 * -# Allocate an operation object which will be passed to all the functions
1072 * listed here.
1073 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
1074 * The key remains associated with the operation even if the content
1075 * of the key slot changes.
1076 * -# Call either psa_aead_generate_iv() or psa_aead_set_iv() to
1077 * generate or set the IV (initialization vector). You should use
1078 * psa_encrypt_generate_iv() unless the protocol you are implementing
1079 * requires a specific IV value.
1080 * -# Call psa_aead_update_ad() to pass the associated data that is
1081 * to be authenticated but not encrypted. You may omit this step if
1082 * there is no associated data.
1083 * -# Call psa_aead_update() zero, one or more times, passing a fragment
1084 * of the data to encrypt each time.
1085 * -# Call psa_aead_finish().
1086 *
1087 * The application may call psa_aead_abort() at any time after the operation
1088 * has been initialized with psa_aead_encrypt_setup().
1089 *
Gilles Peskineed522972018-03-20 17:54:15 +01001090 * After a successful call to psa_aead_encrypt_setup(), the application must
1091 * eventually terminate the operation. The following events terminate an
1092 * operation:
Gilles Peskine3b555712018-03-03 21:27:57 +01001093 * - A failed call to psa_aead_generate_iv(), psa_aead_set_iv(),
1094 * psa_aead_update_ad() or psa_aead_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001095 * - A call to psa_aead_finish() or psa_aead_abort().
Gilles Peskine3b555712018-03-03 21:27:57 +01001096 *
1097 * \param operation
1098 * \param alg The AEAD algorithm to compute (\c PSA_ALG_XXX value
1099 * such that #PSA_ALG_IS_AEAD(alg) is true).
1100 *
1101 * \retval PSA_SUCCESS
1102 * Success.
1103 * \retval PSA_ERROR_EMPTY_SLOT
1104 * \retval PSA_ERROR_NOT_PERMITTED
1105 * \retval PSA_ERROR_INVALID_ARGUMENT
1106 * \c key is not compatible with \c alg.
1107 * \retval PSA_ERROR_NOT_SUPPORTED
1108 * \c alg is not supported or is not an AEAD algorithm.
1109 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1110 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1111 * \retval PSA_ERROR_HARDWARE_FAILURE
1112 * \retval PSA_ERROR_TAMPERING_DETECTED
1113 */
1114psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
1115 psa_key_slot_t key,
1116 psa_algorithm_t alg);
1117
1118/** Set the key for a multipart authenticated decryption operation.
1119 *
1120 * The sequence of operations to authenticated and decrypt a message
1121 * is as follows:
1122 * -# Allocate an operation object which will be passed to all the functions
1123 * listed here.
1124 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
1125 * The key remains associated with the operation even if the content
1126 * of the key slot changes.
1127 * -# Call psa_aead_set_iv() to pass the initialization vector (IV)
1128 * for the authenticated decryption.
1129 * -# Call psa_aead_update_ad() to pass the associated data that is
1130 * to be authenticated but not encrypted. You may omit this step if
1131 * there is no associated data.
1132 * -# Call psa_aead_update() zero, one or more times, passing a fragment
1133 * of the data to decrypt each time.
1134 * -# Call psa_aead_finish().
1135 *
1136 * The application may call psa_aead_abort() at any time after the operation
1137 * has been initialized with psa_aead_decrypt_setup().
1138 *
Gilles Peskineed522972018-03-20 17:54:15 +01001139 * After a successful call to psa_aead_decrypt_setup(), the application must
1140 * eventually terminate the operation. The following events terminate an
1141 * operation:
Gilles Peskine3b555712018-03-03 21:27:57 +01001142 * - A failed call to psa_aead_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001143 * - A call to psa_aead_finish() or psa_aead_abort().
Gilles Peskine3b555712018-03-03 21:27:57 +01001144 *
1145 * \param operation
Gilles Peskine19067982018-03-20 17:54:53 +01001146 * \param alg The AEAD algorithm to compute (\c PSA_ALG_XXX value
1147 * such that #PSA_ALG_IS_AEAD(alg) is true).
Gilles Peskine3b555712018-03-03 21:27:57 +01001148 *
1149 * \retval PSA_SUCCESS
1150 * Success.
1151 * \retval PSA_ERROR_EMPTY_SLOT
1152 * \retval PSA_ERROR_NOT_PERMITTED
1153 * \retval PSA_ERROR_INVALID_ARGUMENT
1154 * \c key is not compatible with \c alg.
1155 * \retval PSA_ERROR_NOT_SUPPORTED
Gilles Peskine19067982018-03-20 17:54:53 +01001156 * \c alg is not supported or is not an AEAD algorithm.
Gilles Peskine3b555712018-03-03 21:27:57 +01001157 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1158 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1159 * \retval PSA_ERROR_HARDWARE_FAILURE
1160 * \retval PSA_ERROR_TAMPERING_DETECTED
1161 */
1162psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
1163 psa_key_slot_t key,
1164 psa_algorithm_t alg);
1165
1166psa_status_t psa_aead_generate_iv(psa_aead_operation_t *operation,
1167 unsigned char *iv,
1168 size_t iv_size,
1169 size_t *iv_length);
1170
1171psa_status_t psa_aead_set_iv(psa_aead_operation_t *operation,
1172 const unsigned char *iv,
1173 size_t iv_length);
1174
1175psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
1176 const uint8_t *input,
1177 size_t input_length);
1178
1179psa_status_t psa_aead_update(psa_aead_operation_t *operation,
1180 const uint8_t *input,
1181 size_t input_length);
1182
1183psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
1184 uint8_t *tag,
1185 size_t tag_size,
1186 size_t *tag_length);
1187
1188psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
1189 uint8_t *tag,
1190 size_t tag_length);
1191
1192psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
1193
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) : \
1260 0)
1261
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.
1283 * This is at most #PSA_HASH_FINAL_SIZE(alg)
1284 * (note that it may be less).
1285 *
1286 * \retval PSA_SUCCESS
1287 * \retval PSA_ERROR_BUFFER_TOO_SMALL
1288 * The size of the \c signature buffer is too small. You can
1289 * determine a sufficient buffer size by calling
1290 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg)
1291 * where \c key_type and \c key_bits are the type and bit-size
1292 * respectively of \c key.
1293 * \retval PSA_ERROR_NOT_SUPPORTED
1294 * \retval PSA_ERROR_INVALID_ARGUMENT
1295 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1296 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1297 * \retval PSA_ERROR_HARDWARE_FAILURE
1298 * \retval PSA_ERROR_TAMPERING_DETECTED
1299 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01001300 */
1301psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
1302 psa_algorithm_t alg,
1303 const uint8_t *hash,
1304 size_t hash_length,
1305 const uint8_t *salt,
1306 size_t salt_length,
1307 uint8_t *signature,
1308 size_t signature_size,
1309 size_t *signature_length);
1310
1311/**
1312 * \brief Verify the signature a hash or short message using a public key.
1313 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001314 * \param key Key slot containing a public key or an
1315 * asymmetric key pair.
1316 * \param alg A signature algorithm that is compatible with
1317 * the type of \c key.
1318 * \param hash The message whose signature is to be verified.
1319 * \param hash_length Size of the \c hash buffer in bytes.
1320 * \param salt A salt or label, if supported by the signature
1321 * algorithm.
1322 * If the signature algorithm does not support a
1323 * salt, pass \c NULL.
1324 * If the signature algorithm supports an optional
1325 * salt and you do not want to pass a salt,
1326 * pass \c NULL.
1327 * \param salt_length Size of the \c salt buffer in bytes.
1328 * If \c salt is \c NULL, pass 0.
1329 * \param signature Buffer containing the signature to verify.
1330 * \param signature_size Size of the \c signature buffer in bytes.
1331 *
1332 * \retval PSA_SUCCESS
1333 * The signature is valid.
1334 * \retval PSA_ERROR_INVALID_SIGNATURE
1335 * The calculation was perfomed successfully, but the passed
1336 * signature is not a valid signature.
1337 * \retval PSA_ERROR_NOT_SUPPORTED
1338 * \retval PSA_ERROR_INVALID_ARGUMENT
1339 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
1340 * \retval PSA_ERROR_COMMUNICATION_FAILURE
1341 * \retval PSA_ERROR_HARDWARE_FAILURE
1342 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01001343 */
1344psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
1345 psa_algorithm_t alg,
1346 const uint8_t *hash,
1347 size_t hash_length,
1348 const uint8_t *salt,
1349 size_t salt_length,
1350 uint8_t *signature,
1351 size_t signature_size);
1352
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001353/**@}*/
1354
Gilles Peskinee59236f2018-01-27 23:32:46 +01001355#ifdef __cplusplus
1356}
1357#endif
1358
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001359/* The file "crypto_struct.h" contains definitions for
1360 * implementation-specific structs that are declared above. */
1361#include "crypto_struct.h"
1362
1363/* The file "crypto_extra.h" contains vendor-specific definitions. This
1364 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01001365#include "crypto_extra.h"
1366
1367#endif /* PSA_CRYPTO_H */