blob: 6276bac6edb0b095ecf13211c2a16aacda5f5240 [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
21 * type.* The choice of type is implementation-dependent.
22 * 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,
74 /** A hardware failure was detected. */
75 PSA_ERROR_HARDWARE_FAILURE,
76 /** A tampering attempt was detected. */
77 PSA_ERROR_TAMPERING_DETECTED,
78 /** There is not enough entropy to generate random data needed
79 for the requested action. */
80 PSA_ERROR_INSUFFICIENT_ENTROPY,
81 /** The signature or MAC is incorrect. */
82 PSA_ERROR_INVALID_SIGNATURE,
83 /** An error occurred that does not correspond to any defined
84 failure cause. */
85 PSA_ERROR_UNKNOWN_ERROR,
86} psa_status_t;
87
88/**
89 * \brief Library initialization.
90 *
91 * Applications must call this function before calling any other
92 * function in this module.
93 *
94 * Applications may call this function more than once. Once a call
95 * succeeds, subsequent calls are guaranteed to succeed.
96 *
97 * \return * \c PSA_SUCCESS: success.
98 * * \c PSA_ERROR_INSUFFICIENT_MEMORY
99 * * \c PSA_ERROR_COMMUNICATION_FAILURE
100 * * \c PSA_ERROR_HARDWARE_FAILURE
101 * * \c PSA_ERROR_TAMPERING_DETECTED
102 * * \c PSA_ERROR_INSUFFICIENT_ENTROPY
103 */
104psa_status_t psa_crypto_init(void);
105
Gilles Peskine0189e752018-02-03 23:57:22 +0100106#define BITS_TO_BYTES(bits) (((bits) + 7) / 8)
107#define BYTES_TO_BITS(bytes) ((bytes) * 8)
108
Gilles Peskinee59236f2018-01-27 23:32:46 +0100109/**@}*/
110
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100111/** \defgroup crypto_types Key and algorithm types
112 * @{
113 */
114
115typedef uint32_t psa_key_type_t;
116
Gilles Peskine98f0a242018-02-06 18:57:29 +0100117#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
118#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100119
Gilles Peskine98f0a242018-02-06 18:57:29 +0100120#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7e000000)
121#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x02000000)
122#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x04000000)
123#define PSA_KEY_TYPE_CATEGORY_ASYMMETRIC ((psa_key_type_t)0x06000000)
124#define PSA_KEY_TYPE_PAIR_FLAG ((psa_key_type_t)0x01000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100125
Gilles Peskine98f0a242018-02-06 18:57:29 +0100126#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x02000001)
127#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x04000001)
128#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x04000002)
129#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x04000003)
130#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x04000004)
131
132#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x06010000)
133#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x07010000)
134#define PSA_KEY_TYPE_ECC_BASE ((psa_key_type_t)0x06030000)
135#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
136
137#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100138 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100139#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
140 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
141#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
142 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG) == \
143 PSA_KEY_TYPE_CATEGORY_ASYMMETRIC))
144#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
145 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
146 (PSA_KEY_TYPE_CATEGORY_ASYMMETRIC | PSA_KEY_TYPE_PAIR_FLAG))
Gilles Peskine0189e752018-02-03 23:57:22 +0100147#define PSA_KEY_TYPE_IS_RSA(type) \
Gilles Peskine98f0a242018-02-06 18:57:29 +0100148 (((type) & ~PSA_KEY_TYPE_PAIR_FLAG) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100149#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine98f0a242018-02-06 18:57:29 +0100150 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100151
Gilles Peskine20035e32018-02-03 22:44:14 +0100152typedef uint32_t psa_algorithm_t;
153
Gilles Peskine98f0a242018-02-06 18:57:29 +0100154#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
155#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
156#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
157#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
158#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
159#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
160#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
161#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
162#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
163#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100164
Gilles Peskine98f0a242018-02-06 18:57:29 +0100165#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
166 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
167#define PSA_ALG_IS_HASH(alg) \
168 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
169#define PSA_ALG_IS_MAC(alg) \
170 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
171#define PSA_ALG_IS_CIPHER(alg) \
172 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
173#define PSA_ALG_IS_AEAD(alg) \
174 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
175#define PSA_ALG_IS_SIGN(alg) \
176 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
177#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
178 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
179#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
180 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
181#define PSA_ALG_IS_KEY_DERIVATION(alg) \
182 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
183
184#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
185#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
186#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
187#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
188#define PSA_ALG_SHA_256_128 ((psa_algorithm_t)0x01000004)
189#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000005)
190#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000006)
191#define PSA_ALG_SHA_256_160 ((psa_algorithm_t)0x01000007)
192#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
193#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
194#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
195#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
196#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
197#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
198#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
199#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
200#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
201#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
202
203#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
204#define PSA_ALG_HMAC(hash_alg) \
205 (PSA_ALG_HMAC_BASE | (hash_alg))
206#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02000001)
207#define PSA_ALG_CMAC ((psa_algorithm_t)0x02000002)
208#define PSA_ALG_GMAC ((psa_algorithm_t)0x02000003)
209
210#define PSA_ALG_BLOCK_CIPHER_BASE_MASK ((psa_algorithm_t)0x000000ff)
211#define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x007f0000)
212#define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000)
213#define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001)
214#define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000003)
215#define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000004)
216#define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000005)
217#define PSA_ALG_STREAM_CIPHER ((psa_algorithm_t)0x04800000)
218#define PSA_ALG_CTR ((psa_algorithm_t)0x04800001)
219
220#define PSA_ALG_CCM ((psa_algorithm_t)0x06000002)
221#define PSA_ALG_GCM ((psa_algorithm_t)0x06000003)
222
223#define PSA_ALG_RSA_PKCS1V15_RAW ((psa_algorithm_t)0x10010000)
224#define PSA_ALG_RSA_PSS_MGF1 ((psa_algorithm_t)0x10020000)
225#define PSA_ALG_RSA_OAEP ((psa_algorithm_t)0x12020000)
226#define PSA_ALG_RSA_PKCS1V15(hash_alg) \
227 (PSA_ALG_RSA_PKCS1V15_RAW | ((hash_alg) & PSA_ALG_HASH_MASK))
228#define PSA_ALG_IS_RSA_PKCS1V15(alg) \
Gilles Peskine20035e32018-02-03 22:44:14 +0100229 (((alg) & 0x7fffff00) == PSA_ALG_RSA_PKCS1V15_RAW)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100230#define PSA_ALG_RSA_GET_HASH(alg) \
231 (((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100232
233/**@}*/
234
235/** \defgroup key_management Key management
236 * @{
237 */
238
239/**
240 * \brief Import a key in binary format.
241 *
242 * This function supports any output from psa_export_key().
243 *
244 * \return * \c PSA_SUCCESS: success.
245 * * \c PSA_ERROR_NOT_SUPPORTED
246 * * \c PSA_ERROR_INVALID_ARGUMENT
247 * * \c PSA_ERROR_INSUFFICIENT_MEMORY
248 * * \c PSA_ERROR_COMMUNICATION_FAILURE
249 * * \c PSA_ERROR_HARDWARE_FAILURE
250 * * \c PSA_ERROR_TAMPERING_DETECTED
251 */
252psa_status_t psa_import_key(psa_key_slot_t key,
253 psa_key_type_t type,
254 const uint8_t *data,
255 size_t data_length);
256
257/**
258 * \brief Destroy a key.
259 *
260 * \return * \c PSA_SUCCESS: success.
261 * * \c PSA_ERROR_EMPTY_SLOT
262 * * \c PSA_ERROR_COMMUNICATION_FAILURE
263 * * \c PSA_ERROR_HARDWARE_FAILURE
264 * * \c PSA_ERROR_TAMPERING_DETECTED
265 */
266psa_status_t psa_destroy_key(psa_key_slot_t key);
267
268/**
269 * \brief Get basic metadata about a key.
270 *
271 * \return * \c PSA_SUCCESS: success.
272 * * \c PSA_ERROR_EMPTY_SLOT
273 * * \c PSA_ERROR_COMMUNICATION_FAILURE
274 * * \c PSA_ERROR_HARDWARE_FAILURE
275 * * \c PSA_ERROR_TAMPERING_DETECTED
276 */
277psa_status_t psa_get_key_information(psa_key_slot_t key,
278 psa_key_type_t *type,
279 size_t *bits);
280
281/**
282 * \brief Export a key in binary format.
283 *
284 * The output of this function can be passed to psa_import_key() to
285 * create an equivalent object.
286 *
287 * If a key is created with psa_import_key() and then exported with
288 * this function, it is not guaranteed that the resulting data is
289 * identical: the implementation may choose a different representation
290 * of the same key.
291 *
292 * \return * \c PSA_SUCCESS: success.
293 * * \c PSA_ERROR_EMPTY_SLOT
294 * * \c PSA_ERROR_COMMUNICATION_FAILURE
295 * * \c PSA_ERROR_HARDWARE_FAILURE
296 * * \c PSA_ERROR_TAMPERING_DETECTED
297 */
298psa_status_t psa_export_key(psa_key_slot_t key,
299 uint8_t *data,
300 size_t data_size,
301 size_t *data_length);
302
Gilles Peskine20035e32018-02-03 22:44:14 +0100303
304/**@}*/
305
306/** \defgroup asymmetric Asymmetric cryptography
307 * @{
308 */
309
310/**
Gilles Peskine0189e752018-02-03 23:57:22 +0100311 * \brief Maximum ECDSA signature size for a given curve bit size
312 *
313 * \param curve_bits Curve size in bits
314 * \return Maximum signature size in bytes
315 *
316 * \note This macro returns a compile-time constant if its argument is one.
317 *
318 * \warning This macro may evaluate its argument multiple times.
319 */
320/*
321 * RFC 4492 page 20:
322 *
323 * Ecdsa-Sig-Value ::= SEQUENCE {
324 * r INTEGER,
325 * s INTEGER
326 * }
327 *
328 * Size is at most
329 * 1 (tag) + 1 (len) + 1 (initial 0) + curve_bytes for each of r and s,
330 * twice that + 1 (tag) + 2 (len) for the sequence
331 * (assuming curve_bytes is less than 126 for r and s,
332 * and less than 124 (total len <= 255) for the sequence)
333 */
334#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
335 ( /*T,L of SEQUENCE*/ ((curve_bits) >= 61 * 8 ? 3 : 2) + \
336 /*T,L of r,s*/ 2 * (((curve_bits) >= 127 * 8 ? 3 : 2) + \
337 /*V of r,s*/ ((curve_bits) + 8) / 8))
338
339
340#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
341 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, BITS_TO_BYTES(key_bits)) : \
342 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
343 0)
344
345/**
Gilles Peskine20035e32018-02-03 22:44:14 +0100346 * \brief Sign a hash or short message with a private key.
347 *
348 */
349psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
350 psa_algorithm_t alg,
351 const uint8_t *hash,
352 size_t hash_length,
353 const uint8_t *salt,
354 size_t salt_length,
355 uint8_t *signature,
356 size_t signature_size,
357 size_t *signature_length);
358
359/**
360 * \brief Verify the signature a hash or short message using a public key.
361 *
362 */
363psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
364 psa_algorithm_t alg,
365 const uint8_t *hash,
366 size_t hash_length,
367 const uint8_t *salt,
368 size_t salt_length,
369 uint8_t *signature,
370 size_t signature_size);
371
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100372/**@}*/
373
Gilles Peskinee59236f2018-01-27 23:32:46 +0100374#ifdef __cplusplus
375}
376#endif
377
378#include "crypto_extra.h"
379
380#endif /* PSA_CRYPTO_H */