blob: 060c007ec58c1b6f9fcd743f27a183b13faace3c [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__
14/** \defgroup platform Implementation-specific definitions
15 * @{
16 */
17
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010018/** \brief Key slot number.
19 *
20 * This type represents key slots. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010021 * type. The choice of type is implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010022 * 0 is not a valid key slot number. The meaning of other values is
23 * implementation dependent.
24 *
25 * At any given point in time, each key slot either contains a
26 * cryptographic object, or is empty. Key slots are persistent:
27 * once set, the cryptographic object remains in the key slot until
28 * explicitly destroyed.
29 */
30typedef _unsigned_integral_type_ psa_key_slot_t;
31
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010032/**@}*/
33#endif
34
Gilles Peskinee59236f2018-01-27 23:32:46 +010035#ifdef __cplusplus
36extern "C" {
37#endif
38
39/** \defgroup basic Basic definitions
40 * @{
41 */
42
43/**
44 * \brief Function return status.
45 *
46 * Zero indicates success, anything else indicates an error.
47 */
48typedef enum {
49 /** The action was completed successfully. */
50 PSA_SUCCESS = 0,
51 /** The requested operation or a parameter is not supported
52 by this implementation. */
53 PSA_ERROR_NOT_SUPPORTED,
54 /** The requested action is denied by a policy. */
55 PSA_ERROR_NOT_PERMITTED,
56 /** An output buffer is too small. */
57 PSA_ERROR_BUFFER_TOO_SMALL,
58 /** A slot is occupied, but must be empty to carry out the
59 requested action. */
60 PSA_ERROR_OCCUPIED_SLOT,
61 /** A slot is empty, but must be occupied to carry out the
62 requested action. */
63 PSA_ERROR_EMPTY_SLOT,
64 /** The requested action cannot be performed in the current state. */
65 PSA_ERROR_BAD_STATE,
66 /** The parameters passed to the function are invalid. */
67 PSA_ERROR_INVALID_ARGUMENT,
68 /** There is not enough runtime memory. */
69 PSA_ERROR_INSUFFICIENT_MEMORY,
70 /** There is not enough persistent storage. */
71 PSA_ERROR_INSUFFICIENT_STORAGE,
72 /** There was a communication failure inside the implementation. */
73 PSA_ERROR_COMMUNICATION_FAILURE,
Gilles Peskinea5905292018-02-07 20:59:33 +010074 /** There was a storage failure that may have led to data loss. */
75 PSA_ERROR_STORAGE_FAILURE,
Gilles Peskinee59236f2018-01-27 23:32:46 +010076 /** A hardware failure was detected. */
77 PSA_ERROR_HARDWARE_FAILURE,
78 /** A tampering attempt was detected. */
79 PSA_ERROR_TAMPERING_DETECTED,
80 /** There is not enough entropy to generate random data needed
81 for the requested action. */
82 PSA_ERROR_INSUFFICIENT_ENTROPY,
Gilles Peskinea5905292018-02-07 20:59:33 +010083 /** The signature, MAC or hash is incorrect. */
Gilles Peskinee59236f2018-01-27 23:32:46 +010084 PSA_ERROR_INVALID_SIGNATURE,
Gilles Peskinea5905292018-02-07 20:59:33 +010085 /** The decrypted padding is incorrect. */
86 PSA_ERROR_INVALID_PADDING,
Gilles Peskinee59236f2018-01-27 23:32:46 +010087 /** An error occurred that does not correspond to any defined
88 failure cause. */
89 PSA_ERROR_UNKNOWN_ERROR,
90} psa_status_t;
91
92/**
93 * \brief Library initialization.
94 *
95 * Applications must call this function before calling any other
96 * function in this module.
97 *
98 * Applications may call this function more than once. Once a call
99 * succeeds, subsequent calls are guaranteed to succeed.
100 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100101 * \retval PSA_SUCCESS
102 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
103 * \retval PSA_ERROR_COMMUNICATION_FAILURE
104 * \retval PSA_ERROR_HARDWARE_FAILURE
105 * \retval PSA_ERROR_TAMPERING_DETECTED
106 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +0100107 */
108psa_status_t psa_crypto_init(void);
109
Gilles Peskine0189e752018-02-03 23:57:22 +0100110#define BITS_TO_BYTES(bits) (((bits) + 7) / 8)
111#define BYTES_TO_BITS(bytes) ((bytes) * 8)
112
Gilles Peskinee59236f2018-01-27 23:32:46 +0100113/**@}*/
114
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100115/** \defgroup crypto_types Key and algorithm types
116 * @{
117 */
118
Gilles Peskine308b91d2018-02-08 09:47:44 +0100119/** \brief Encoding of a key type.
120 */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100121typedef uint32_t psa_key_type_t;
122
Gilles Peskine98f0a242018-02-06 18:57:29 +0100123#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
124#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100125
Gilles Peskine98f0a242018-02-06 18:57:29 +0100126#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7e000000)
127#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x02000000)
128#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x04000000)
129#define PSA_KEY_TYPE_CATEGORY_ASYMMETRIC ((psa_key_type_t)0x06000000)
130#define PSA_KEY_TYPE_PAIR_FLAG ((psa_key_type_t)0x01000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100131
Gilles Peskine98f0a242018-02-06 18:57:29 +0100132#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x02000001)
133#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x04000001)
134#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x04000002)
135#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x04000003)
136#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x04000004)
137
Gilles Peskine308b91d2018-02-08 09:47:44 +0100138/** RSA public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100139#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x06010000)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100140/** RSA key pair (private and public key). */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100141#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x07010000)
142#define PSA_KEY_TYPE_ECC_BASE ((psa_key_type_t)0x06030000)
143#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
144
145#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100146 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100147#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
148 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
149#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
150 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG) == \
151 PSA_KEY_TYPE_CATEGORY_ASYMMETRIC))
152#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
153 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
154 (PSA_KEY_TYPE_CATEGORY_ASYMMETRIC | PSA_KEY_TYPE_PAIR_FLAG))
Gilles Peskine0189e752018-02-03 23:57:22 +0100155#define PSA_KEY_TYPE_IS_RSA(type) \
Gilles Peskine98f0a242018-02-06 18:57:29 +0100156 (((type) & ~PSA_KEY_TYPE_PAIR_FLAG) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100157#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine98f0a242018-02-06 18:57:29 +0100158 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100159
Gilles Peskine308b91d2018-02-08 09:47:44 +0100160/** \brief Encoding of a cryptographic algorithm.
161 *
162 * For algorithms that can be applied to multiple key types, this type
163 * does not encode the key type. For example, for symmetric ciphers
164 * based on a block cipher, #psa_algorithm_t encodes the block cipher
165 * mode and the padding mode while the block cipher itself is encoded
166 * via #psa_key_type_t.
167 */
Gilles Peskine20035e32018-02-03 22:44:14 +0100168typedef uint32_t psa_algorithm_t;
169
Gilles Peskine98f0a242018-02-06 18:57:29 +0100170#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
171#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
172#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
173#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
174#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
175#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
176#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
177#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
178#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
179#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100180
Gilles Peskine98f0a242018-02-06 18:57:29 +0100181#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
182 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100183/** Whether the specified algorithm is a hash algorithm.
184 *
185 * \param alg An algorithm identifier (\c PSA_ALG_XXX value)
186 *
187 * \return 1 if \c alg is a hash algorithm, 0 otherwise.
188 * This macro may return either 0 or 1 if \c alg is not a valid
189 * algorithm identifier. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100190#define PSA_ALG_IS_HASH(alg) \
191 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
192#define PSA_ALG_IS_MAC(alg) \
193 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
194#define PSA_ALG_IS_CIPHER(alg) \
195 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
196#define PSA_ALG_IS_AEAD(alg) \
197 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
198#define PSA_ALG_IS_SIGN(alg) \
199 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
200#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
201 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
202#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
203 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
204#define PSA_ALG_IS_KEY_DERIVATION(alg) \
205 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
206
207#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
208#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
209#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
210#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
211#define PSA_ALG_SHA_256_128 ((psa_algorithm_t)0x01000004)
212#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000005)
213#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000006)
214#define PSA_ALG_SHA_256_160 ((psa_algorithm_t)0x01000007)
215#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
216#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
217#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
218#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
219#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
220#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
221#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
222#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
223#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
224#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
225
226#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
227#define PSA_ALG_HMAC(hash_alg) \
228 (PSA_ALG_HMAC_BASE | (hash_alg))
229#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02000001)
230#define PSA_ALG_CMAC ((psa_algorithm_t)0x02000002)
231#define PSA_ALG_GMAC ((psa_algorithm_t)0x02000003)
232
233#define PSA_ALG_BLOCK_CIPHER_BASE_MASK ((psa_algorithm_t)0x000000ff)
234#define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x007f0000)
235#define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000)
236#define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001)
237#define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000003)
238#define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000004)
239#define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000005)
240#define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t)0x04800000)
241#define PSA_ALG_CTR ((psa_algorithm_t)0x04800001)
242
243#define PSA_ALG_CCM ((psa_algorithm_t)0x06000002)
244#define PSA_ALG_GCM ((psa_algorithm_t)0x06000003)
245
246#define PSA_ALG_RSA_PKCS1V15_RAW ((psa_algorithm_t)0x10010000)
247#define PSA_ALG_RSA_PSS_MGF1 ((psa_algorithm_t)0x10020000)
248#define PSA_ALG_RSA_OAEP ((psa_algorithm_t)0x12020000)
249#define PSA_ALG_RSA_PKCS1V15(hash_alg) \
250 (PSA_ALG_RSA_PKCS1V15_RAW | ((hash_alg) & PSA_ALG_HASH_MASK))
251#define PSA_ALG_IS_RSA_PKCS1V15(alg) \
Gilles Peskine20035e32018-02-03 22:44:14 +0100252 (((alg) & 0x7fffff00) == PSA_ALG_RSA_PKCS1V15_RAW)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100253#define PSA_ALG_RSA_GET_HASH(alg) \
254 (((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100255
256/**@}*/
257
258/** \defgroup key_management Key management
259 * @{
260 */
261
262/**
263 * \brief Import a key in binary format.
264 *
265 * This function supports any output from psa_export_key().
266 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100267 * \param key Slot where the key will be stored. This must be a
268 * valid slot for a key of the chosen type. It must
269 * be unoccupied.
270 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
271 * \param data Buffer containing the key data.
272 * \param data_length Size of the \c data buffer in bytes.
273 *
274 * \retval PSA_SUCCESS
275 * Success.
276 * \retval PSA_ERROR_NOT_SUPPORTED
277 * The key type or key size is not supported.
278 * \retval PSA_ERROR_INVALID_ARGUMENT
279 * The key slot is invalid,
280 * or the key data is not correctly formatted.
281 * \retval PSA_ERROR_OCCUPIED_SLOT
282 There is already a key in the specified slot.
283 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
284 * \retval PSA_ERROR_COMMUNICATION_FAILURE
285 * \retval PSA_ERROR_HARDWARE_FAILURE
286 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100287 */
288psa_status_t psa_import_key(psa_key_slot_t key,
289 psa_key_type_t type,
290 const uint8_t *data,
291 size_t data_length);
292
293/**
294 * \brief Destroy a key.
295 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100296 * \retval PSA_SUCCESS
297 * \retval PSA_ERROR_EMPTY_SLOT
298 * \retval PSA_ERROR_COMMUNICATION_FAILURE
299 * \retval PSA_ERROR_HARDWARE_FAILURE
300 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100301 */
302psa_status_t psa_destroy_key(psa_key_slot_t key);
303
304/**
305 * \brief Get basic metadata about a key.
306 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100307 * \param key Slot whose content is queried. This must
308 * be an occupied key slot.
309 * \param type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
310 * This may be a null pointer, in which case the key type
311 * is not written.
312 * \param bits On success, the key size in bits.
313 * This may be a null pointer, in which case the key type
314 * is not written.
315 *
316 * \retval PSA_SUCCESS
317 * \retval PSA_ERROR_EMPTY_SLOT
318 * \retval PSA_ERROR_COMMUNICATION_FAILURE
319 * \retval PSA_ERROR_HARDWARE_FAILURE
320 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100321 */
322psa_status_t psa_get_key_information(psa_key_slot_t key,
323 psa_key_type_t *type,
324 size_t *bits);
325
326/**
327 * \brief Export a key in binary format.
328 *
329 * The output of this function can be passed to psa_import_key() to
330 * create an equivalent object.
331 *
332 * If a key is created with psa_import_key() and then exported with
333 * this function, it is not guaranteed that the resulting data is
334 * identical: the implementation may choose a different representation
335 * of the same key.
336 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100337 * For standard key types, the output format is as follows:
338 *
339 * - For symmetric keys (including MAC keys), the format is the
340 * raw bytes of the key.
341 * - For DES, the key data consists of 8 bytes. The parity bits must be
342 * correct.
343 * - For Triple-DES, the format is the concatenation of the
344 * two or three DES keys.
345 * - For RSA key pairs keys (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
346 * is the non-encrypted DER representation defined by PKCS\#8 (RFC 5208)
347 * as PrivateKeyInfo.
348 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
349 * is the DER representation defined by X.509.
350 *
351 * \param key Slot whose content is to be exported. This must
352 * be an occupied key slot.
353 * \param data Buffer where the key data is to be written.
354 * \param data_size Size of the \c data buffer in bytes.
355 * \param data_length On success, the number of bytes
356 * that make up the key data.
357 *
358 * \retval PSA_SUCCESS
359 * \retval PSA_ERROR_EMPTY_SLOT
360 * \retval PSA_ERROR_COMMUNICATION_FAILURE
361 * \retval PSA_ERROR_HARDWARE_FAILURE
362 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100363 */
364psa_status_t psa_export_key(psa_key_slot_t key,
365 uint8_t *data,
366 size_t data_size,
367 size_t *data_length);
368
Gilles Peskine20035e32018-02-03 22:44:14 +0100369
370/**@}*/
371
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100372/** \defgroup hash Message digests
373 * @{
374 */
375
Gilles Peskine308b91d2018-02-08 09:47:44 +0100376/** The type of the state data structure for multipart hash operations.
377 *
378 * This is an implementation-define \c struct. Applications should not
379 * make any assumptions about the content of this structure except
380 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100381typedef struct psa_hash_operation_s psa_hash_operation_t;
382
Gilles Peskine308b91d2018-02-08 09:47:44 +0100383/** The size of the output of psa_hash_finish(), in bytes.
384 *
385 * This is also the hash size that psa_hash_verify() expects.
386 *
387 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
388 * #PSA_ALG_IS_HASH(alg) is true).
389 *
390 * \return The hash size for the specified hash algorithm.
391 * If the hash algorithm is not recognized, return 0.
392 * An implementation may return either 0 or the correct size
393 * for a hash algorithm that it recognizes, but does not support.
394 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100395#define PSA_HASH_FINAL_SIZE(alg) \
396 ( \
397 (alg) == PSA_ALG_MD2 ? 16 : \
398 (alg) == PSA_ALG_MD4 ? 16 : \
399 (alg) == PSA_ALG_MD5 ? 16 : \
400 (alg) == PSA_ALG_SHA_256_128 ? 16 : \
401 (alg) == PSA_ALG_RIPEMD160 ? 20 : \
402 (alg) == PSA_ALG_SHA_1 ? 20 : \
403 (alg) == PSA_ALG_SHA_256_160 ? 20 : \
404 (alg) == PSA_ALG_SHA_224 ? 28 : \
405 (alg) == PSA_ALG_SHA_256 ? 32 : \
406 (alg) == PSA_ALG_SHA_384 ? 48 : \
407 (alg) == PSA_ALG_SHA_512 ? 64 : \
408 (alg) == PSA_ALG_SHA_512_224 ? 28 : \
409 (alg) == PSA_ALG_SHA_512_256 ? 32 : \
410 (alg) == PSA_ALG_SHA3_224 ? 28 : \
411 (alg) == PSA_ALG_SHA3_256 ? 32 : \
412 (alg) == PSA_ALG_SHA3_384 ? 48 : \
413 (alg) == PSA_ALG_SHA3_512 ? 64 : \
414 0)
415
Gilles Peskine308b91d2018-02-08 09:47:44 +0100416/** Start a multipart hash operation.
417 *
418 * The sequence of operations to calculate a hash (message digest)
419 * is as follows:
420 * -# Allocate an operation object which will be passed to all the functions
421 * listed here.
422 * -# Call psa_hash_start() to specify the algorithm.
423 * -# Call psa_hash_update() zero, one or more time, passing a fragment
424 * of the message each time. The hash that is calculated is the hash
425 * of the concatenation of these messages in order.
426 * -# To calculate the hash, call psa_hash_finish().
427 * To compare the hash with an expected value, call psa_hash_verify().
428 *
429 * The application may call psa_hash_abort() at any time after the operation
430 * has been initialized with psa_hash_start().
431 *
432 * After a successful call to psa_hash_start(), the application must
433 * eventually destroy the operation through one of the following means:
434 * - A failed call to psa_hash_update().
435 * - A call to psa_hash_final(), psa_hash_verify() or psa_hash_abort().
436 *
437 * \param operation
438 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
439 * such that #PSA_ALG_IS_HASH(alg) is true).
440 *
441 * \retval PSA_SUCCESS
442 * Success.
443 * \retval PSA_ERROR_NOT_SUPPORTED
444 * \c alg is not supported or is not a hash algorithm.
445 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
446 * \retval PSA_ERROR_COMMUNICATION_FAILURE
447 * \retval PSA_ERROR_HARDWARE_FAILURE
448 * \retval PSA_ERROR_TAMPERING_DETECTED
449 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100450psa_status_t psa_hash_start(psa_hash_operation_t *operation,
451 psa_algorithm_t alg);
452
Gilles Peskine308b91d2018-02-08 09:47:44 +0100453/** Add a message fragment to a multipart hash operation.
454 *
455 * The application must call psa_hash_start() before calling this function.
456 *
457 * If this function returns an error status, the operation becomes inactive.
458 *
459 * \param operation Active hash operation.
460 * \param input Buffer containing the message fragment to hash.
461 * \param input_length Size of the \c input buffer in bytes.
462 *
463 * \retval PSA_SUCCESS
464 * Success.
465 * \retval PSA_ERROR_BAD_STATE
466 * The operation state is not valid (not started, or already completed).
467 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
468 * \retval PSA_ERROR_COMMUNICATION_FAILURE
469 * \retval PSA_ERROR_HARDWARE_FAILURE
470 * \retval PSA_ERROR_TAMPERING_DETECTED
471 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100472psa_status_t psa_hash_update(psa_hash_operation_t *operation,
473 const uint8_t *input,
474 size_t input_length);
475
Gilles Peskine308b91d2018-02-08 09:47:44 +0100476/** Finish the calculation of the hash of a message.
477 *
478 * The application must call psa_hash_start() before calling this function.
479 * This function calculates the hash of the message formed by concatenating
480 * the inputs passed to preceding calls to psa_hash_update().
481 *
482 * When this function returns, the operation becomes inactive.
483 *
484 * \warning Applications should not call this function if they expect
485 * a specific value for the hash. Call psa_hash_verify() instead.
486 * Beware that comparing integrity or authenticity data such as
487 * hash values with a function such as \c memcmp is risky
488 * because the time taken by the comparison may leak information
489 * about the hashed data which could allow an attacker to guess
490 * a valid hash and thereby bypass security controls.
491 *
492 * \param operation Active hash operation.
493 * \param hash Buffer where the hash is to be written.
494 * \param hash_size Size of the \c hash buffer in bytes.
495 * \param hash_length On success, the number of bytes
496 * that make up the hash value. This is always
497 * #PSA_HASH_FINAL_SIZE(alg) where \c alg is the
498 * hash algorithm that is calculated.
499 *
500 * \retval PSA_SUCCESS
501 * Success.
502 * \retval PSA_ERROR_BAD_STATE
503 * The operation state is not valid (not started, or already completed).
504 * \retval PSA_ERROR_BUFFER_TOO_SMALL
505 * The size of the \c hash buffer is too small. You can determine a
506 * sufficient buffer size by calling #PSA_HASH_FINAL_SIZE(alg)
507 * where \c alg is the hash algorithm that is calculated.
508 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
509 * \retval PSA_ERROR_COMMUNICATION_FAILURE
510 * \retval PSA_ERROR_HARDWARE_FAILURE
511 * \retval PSA_ERROR_TAMPERING_DETECTED
512 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100513psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
514 uint8_t *hash,
515 size_t hash_size,
516 size_t *hash_length);
517
Gilles Peskine308b91d2018-02-08 09:47:44 +0100518/** Finish the calculation of the hash of a message and compare it with
519 * an expected value.
520 *
521 * The application must call psa_hash_start() before calling this function.
522 * This function calculates the hash of the message formed by concatenating
523 * the inputs passed to preceding calls to psa_hash_update(). It then
524 * compares the calculated hash with the expected hash passed as a
525 * parameter to this function.
526 *
527 * When this function returns, the operation becomes inactive.
528 *
529 * \note Applications shall make the best effort to ensure that the
530 * comparison between the actual hash and the expected hash is performed
531 * in constant time.
532 *
533 * \param operation Active hash operation.
534 * \param hash Buffer containing the expected hash value.
535 * \param hash_length Size of the \c hash buffer in bytes.
536 *
537 * \retval PSA_SUCCESS
538 * The expected hash is identical to the actual hash of the message.
539 * \retval PSA_ERROR_INVALID_SIGNATURE
540 * The hash of the message was calculated successfully, but it
541 * differs from the expected hash.
542 * \retval PSA_ERROR_BAD_STATE
543 * The operation state is not valid (not started, or already completed).
544 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
545 * \retval PSA_ERROR_COMMUNICATION_FAILURE
546 * \retval PSA_ERROR_HARDWARE_FAILURE
547 * \retval PSA_ERROR_TAMPERING_DETECTED
548 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100549psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
550 const uint8_t *hash,
551 size_t hash_length);
552
Gilles Peskine308b91d2018-02-08 09:47:44 +0100553/** Abort a hash operation.
554 *
555 * This function may be called at any time after psa_hash_start().
556 * Aborting an operation frees all associated resources except for the
557 * \c operation structure itself.
558 *
559 * Implementation should strive to be robust and handle inactive hash
560 * operations safely (do nothing and return #PSA_ERROR_BAD_STATE). However,
561 * application writers should beware that uninitialized memory may happen
562 * to be indistinguishable from an active hash operation, and the behavior
563 * of psa_hash_abort() is undefined in this case.
564 *
565 * \param operation Active hash operation.
566 *
567 * \retval PSA_SUCCESS
568 * \retval PSA_ERROR_BAD_STATE
569 * \c operation is not an active hash operation.
570 * \retval PSA_ERROR_COMMUNICATION_FAILURE
571 * \retval PSA_ERROR_HARDWARE_FAILURE
572 * \retval PSA_ERROR_TAMPERING_DETECTED
573 */
574psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100575
576/**@}*/
577
Gilles Peskine20035e32018-02-03 22:44:14 +0100578/** \defgroup asymmetric Asymmetric cryptography
579 * @{
580 */
581
582/**
Gilles Peskine0189e752018-02-03 23:57:22 +0100583 * \brief Maximum ECDSA signature size for a given curve bit size
584 *
585 * \param curve_bits Curve size in bits
586 * \return Maximum signature size in bytes
587 *
588 * \note This macro returns a compile-time constant if its argument is one.
589 *
590 * \warning This macro may evaluate its argument multiple times.
591 */
592/*
593 * RFC 4492 page 20:
594 *
595 * Ecdsa-Sig-Value ::= SEQUENCE {
596 * r INTEGER,
597 * s INTEGER
598 * }
599 *
600 * Size is at most
601 * 1 (tag) + 1 (len) + 1 (initial 0) + curve_bytes for each of r and s,
602 * twice that + 1 (tag) + 2 (len) for the sequence
603 * (assuming curve_bytes is less than 126 for r and s,
604 * and less than 124 (total len <= 255) for the sequence)
605 */
606#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
607 ( /*T,L of SEQUENCE*/ ((curve_bits) >= 61 * 8 ? 3 : 2) + \
608 /*T,L of r,s*/ 2 * (((curve_bits) >= 127 * 8 ? 3 : 2) + \
609 /*V of r,s*/ ((curve_bits) + 8) / 8))
610
611
Gilles Peskine308b91d2018-02-08 09:47:44 +0100612/** Safe signature buffer size for psa_asymmetric_sign().
613 *
614 * This macro returns a safe buffer size for a signature using a key
615 * of the specified type and size, with the specified algorithm.
616 * Note that the actual size of the signature may be smaller
617 * (some algorithms produce a variable-size signature).
618 *
619 * \warning This function may call its arguments multiple times or
620 * zero times, so you should not pass arguments that contain
621 * side effects.
622 *
623 * \param key_type An asymmetric key type (this may indifferently be a
624 * key pair type or a public key type).
625 * \param key_bits The size of the key in bits.
626 * \param alg The signature algorithm.
627 *
628 * \return If the parameters are valid and supported, return
629 * a buffer size in bytes that guarantees that
630 * psa_asymmetric_sign() will not fail with
631 * #PSA_ERROR_BUFFER_TOO_SMALL.
632 * If the parameters are a valid combination that is not supported
633 * by the implementation, this macro either shall return either a
634 * sensible size or 0.
635 * If the parameters are not valid, the
636 * return value is unspecified.
637 *
638 */
Gilles Peskine0189e752018-02-03 23:57:22 +0100639#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
640 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, BITS_TO_BYTES(key_bits)) : \
641 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
642 0)
643
644/**
Gilles Peskine20035e32018-02-03 22:44:14 +0100645 * \brief Sign a hash or short message with a private key.
646 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100647 * \param key Key slot containing an asymmetric key pair.
648 * \param alg A signature algorithm that is compatible with
649 * the type of \c key.
650 * \param hash The message to sign.
651 * \param hash_length Size of the \c hash buffer in bytes.
652 * \param salt A salt or label, if supported by the signature
653 * algorithm.
654 * If the signature algorithm does not support a
655 * salt, pass \c NULL.
656 * If the signature algorithm supports an optional
657 * salt and you do not want to pass a salt,
658 * pass \c NULL.
659 * \param salt_length Size of the \c salt buffer in bytes.
660 * If \c salt is \c NULL, pass 0.
661 * \param signature Buffer where the signature is to be written.
662 * \param signature_size Size of the \c signature buffer in bytes.
663 * \param signature_length On success, the number of bytes
664 * that make up the returned signature value.
665 * This is at most #PSA_HASH_FINAL_SIZE(alg)
666 * (note that it may be less).
667 *
668 * \retval PSA_SUCCESS
669 * \retval PSA_ERROR_BUFFER_TOO_SMALL
670 * The size of the \c signature buffer is too small. You can
671 * determine a sufficient buffer size by calling
672 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg)
673 * where \c key_type and \c key_bits are the type and bit-size
674 * respectively of \c key.
675 * \retval PSA_ERROR_NOT_SUPPORTED
676 * \retval PSA_ERROR_INVALID_ARGUMENT
677 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
678 * \retval PSA_ERROR_COMMUNICATION_FAILURE
679 * \retval PSA_ERROR_HARDWARE_FAILURE
680 * \retval PSA_ERROR_TAMPERING_DETECTED
681 * \retval PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +0100682 */
683psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
684 psa_algorithm_t alg,
685 const uint8_t *hash,
686 size_t hash_length,
687 const uint8_t *salt,
688 size_t salt_length,
689 uint8_t *signature,
690 size_t signature_size,
691 size_t *signature_length);
692
693/**
694 * \brief Verify the signature a hash or short message using a public key.
695 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100696 * \param key Key slot containing a public key or an
697 * asymmetric key pair.
698 * \param alg A signature algorithm that is compatible with
699 * the type of \c key.
700 * \param hash The message whose signature is to be verified.
701 * \param hash_length Size of the \c hash buffer in bytes.
702 * \param salt A salt or label, if supported by the signature
703 * algorithm.
704 * If the signature algorithm does not support a
705 * salt, pass \c NULL.
706 * If the signature algorithm supports an optional
707 * salt and you do not want to pass a salt,
708 * pass \c NULL.
709 * \param salt_length Size of the \c salt buffer in bytes.
710 * If \c salt is \c NULL, pass 0.
711 * \param signature Buffer containing the signature to verify.
712 * \param signature_size Size of the \c signature buffer in bytes.
713 *
714 * \retval PSA_SUCCESS
715 * The signature is valid.
716 * \retval PSA_ERROR_INVALID_SIGNATURE
717 * The calculation was perfomed successfully, but the passed
718 * signature is not a valid signature.
719 * \retval PSA_ERROR_NOT_SUPPORTED
720 * \retval PSA_ERROR_INVALID_ARGUMENT
721 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
722 * \retval PSA_ERROR_COMMUNICATION_FAILURE
723 * \retval PSA_ERROR_HARDWARE_FAILURE
724 * \retval PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +0100725 */
726psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
727 psa_algorithm_t alg,
728 const uint8_t *hash,
729 size_t hash_length,
730 const uint8_t *salt,
731 size_t salt_length,
732 uint8_t *signature,
733 size_t signature_size);
734
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100735/**@}*/
736
Gilles Peskinee59236f2018-01-27 23:32:46 +0100737#ifdef __cplusplus
738}
739#endif
740
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100741/* The file "crypto_struct.h" contains definitions for
742 * implementation-specific structs that are declared above. */
743#include "crypto_struct.h"
744
745/* The file "crypto_extra.h" contains vendor-specific definitions. This
746 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +0100747#include "crypto_extra.h"
748
749#endif /* PSA_CRYPTO_H */