blob: 0b40c5fd3818789e743c26a2e4b2b60024535020 [file] [log] [blame]
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001/**
2 * \file psa/crypto_values.h
3 *
4 * \brief PSA cryptography module: macros to build and analyze integer values.
5 *
6 * \note This file may not be included directly. Applications must
7 * include psa/crypto.h. Drivers must include the appropriate driver
8 * header file.
9 *
10 * This file contains portable definitions of macros to build and analyze
11 * values of integral types that encode properties of cryptographic keys,
12 * designations of cryptographic algorithms, and error codes returned by
13 * the library.
14 *
15 * This header file only defines preprocessor macros.
16 */
17/*
18 * Copyright (C) 2018, ARM Limited, All Rights Reserved
19 * SPDX-License-Identifier: Apache-2.0
20 *
21 * Licensed under the Apache License, Version 2.0 (the "License"); you may
22 * not use this file except in compliance with the License.
23 * You may obtain a copy of the License at
24 *
25 * http://www.apache.org/licenses/LICENSE-2.0
26 *
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 * See the License for the specific language governing permissions and
31 * limitations under the License.
32 *
33 * This file is part of mbed TLS (https://tls.mbed.org)
34 */
35
36#ifndef PSA_CRYPTO_VALUES_H
37#define PSA_CRYPTO_VALUES_H
38
39/** \defgroup error Error codes
40 * @{
41 */
42
43#if !defined(PSA_SUCCESS)
44/* If PSA_SUCCESS is defined, assume that PSA crypto is being used
45 * together with PSA IPC, which also defines the identifier
46 * PSA_SUCCESS. We must not define PSA_SUCCESS ourselves in that case;
47 * the other error code names don't clash. This is a temporary hack
48 * until we unify error reporting in PSA IPC and PSA crypto.
49 *
50 * Note that psa_defs.h must be included before this header!
51 */
52/** The action was completed successfully. */
53#define PSA_SUCCESS ((psa_status_t)0)
54#endif /* !defined(PSA_SUCCESS) */
55
56/** An error occurred that does not correspond to any defined
57 * failure cause.
58 *
59 * Implementations may use this error code if none of the other standard
60 * error codes are applicable. */
61#define PSA_ERROR_UNKNOWN_ERROR ((psa_status_t)1)
62
63/** The requested operation or a parameter is not supported
64 * by this implementation.
65 *
66 * Implementations should return this error code when an enumeration
67 * parameter such as a key type, algorithm, etc. is not recognized.
68 * If a combination of parameters is recognized and identified as
69 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
70#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)2)
71
72/** The requested action is denied by a policy.
73 *
74 * Implementations should return this error code when the parameters
75 * are recognized as valid and supported, and a policy explicitly
76 * denies the requested operation.
77 *
78 * If a subset of the parameters of a function call identify a
79 * forbidden operation, and another subset of the parameters are
80 * not valid or not supported, it is unspecified whether the function
81 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
82 * #PSA_ERROR_INVALID_ARGUMENT. */
83#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)3)
84
85/** An output buffer is too small.
86 *
87 * Applications can call the \c PSA_xxx_SIZE macro listed in the function
88 * description to determine a sufficient buffer size.
89 *
90 * Implementations should preferably return this error code only
91 * in cases when performing the operation with a larger output
92 * buffer would succeed. However implementations may return this
93 * error if a function has invalid or unsupported parameters in addition
94 * to the parameters that determine the necessary output buffer size. */
95#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)4)
96
97/** A slot is occupied, but must be empty to carry out the
98 * requested action.
99 *
100 * If a handle is invalid, it does not designate an occupied slot.
101 * The error for an invalid handle is #PSA_ERROR_INVALID_HANDLE.
102 */
103#define PSA_ERROR_OCCUPIED_SLOT ((psa_status_t)5)
104
105/** A slot is empty, but must be occupied to carry out the
106 * requested action.
107 *
108 * If a handle is invalid, it does not designate an empty slot.
109 * The error for an invalid handle is #PSA_ERROR_INVALID_HANDLE.
110 */
111#define PSA_ERROR_EMPTY_SLOT ((psa_status_t)6)
112
113/** The requested action cannot be performed in the current state.
114 *
115 * Multipart operations return this error when one of the
116 * functions is called out of sequence. Refer to the function
117 * descriptions for permitted sequencing of functions.
118 *
119 * Implementations shall not return this error code to indicate
120 * that a key slot is occupied when it needs to be free or vice versa,
121 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
122 * as applicable. */
123#define PSA_ERROR_BAD_STATE ((psa_status_t)7)
124
125/** The parameters passed to the function are invalid.
126 *
127 * Implementations may return this error any time a parameter or
128 * combination of parameters are recognized as invalid.
129 *
130 * Implementations shall not return this error code to indicate
131 * that a key slot is occupied when it needs to be free or vice versa,
132 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
133 * as applicable.
134 *
135 * Implementation shall not return this error code to indicate that a
136 * key handle is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
137 * instead.
138 */
139#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)8)
140
141/** There is not enough runtime memory.
142 *
143 * If the action is carried out across multiple security realms, this
144 * error can refer to available memory in any of the security realms. */
145#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)9)
146
147/** There is not enough persistent storage.
148 *
149 * Functions that modify the key storage return this error code if
150 * there is insufficient storage space on the host media. In addition,
151 * many functions that do not otherwise access storage may return this
152 * error code if the implementation requires a mandatory log entry for
153 * the requested action and the log storage space is full. */
154#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)10)
155
156/** There was a communication failure inside the implementation.
157 *
158 * This can indicate a communication failure between the application
159 * and an external cryptoprocessor or between the cryptoprocessor and
160 * an external volatile or persistent memory. A communication failure
161 * may be transient or permanent depending on the cause.
162 *
163 * \warning If a function returns this error, it is undetermined
164 * whether the requested action has completed or not. Implementations
165 * should return #PSA_SUCCESS on successful completion whenver
166 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
167 * if the requested action was completed successfully in an external
168 * cryptoprocessor but there was a breakdown of communication before
169 * the cryptoprocessor could report the status to the application.
170 */
171#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)11)
172
173/** There was a storage failure that may have led to data loss.
174 *
175 * This error indicates that some persistent storage is corrupted.
176 * It should not be used for a corruption of volatile memory
177 * (use #PSA_ERROR_TAMPERING_DETECTED), for a communication error
178 * between the cryptoprocessor and its external storage (use
179 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
180 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
181 *
182 * Note that a storage failure does not indicate that any data that was
183 * previously read is invalid. However this previously read data may no
184 * longer be readable from storage.
185 *
186 * When a storage failure occurs, it is no longer possible to ensure
187 * the global integrity of the keystore. Depending on the global
188 * integrity guarantees offered by the implementation, access to other
189 * data may or may not fail even if the data is still readable but
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100190 * its integrity cannot be guaranteed.
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100191 *
192 * Implementations should only use this error code to report a
193 * permanent storage corruption. However application writers should
194 * keep in mind that transient errors while reading the storage may be
195 * reported using this error code. */
196#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)12)
197
198/** A hardware failure was detected.
199 *
200 * A hardware failure may be transient or permanent depending on the
201 * cause. */
202#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)13)
203
204/** A tampering attempt was detected.
205 *
206 * If an application receives this error code, there is no guarantee
207 * that previously accessed or computed data was correct and remains
208 * confidential. Applications should not perform any security function
209 * and should enter a safe failure state.
210 *
211 * Implementations may return this error code if they detect an invalid
212 * state that cannot happen during normal operation and that indicates
213 * that the implementation's security guarantees no longer hold. Depending
214 * on the implementation architecture and on its security and safety goals,
215 * the implementation may forcibly terminate the application.
216 *
217 * This error code is intended as a last resort when a security breach
218 * is detected and it is unsure whether the keystore data is still
219 * protected. Implementations shall only return this error code
220 * to report an alarm from a tampering detector, to indicate that
221 * the confidentiality of stored data can no longer be guaranteed,
222 * or to indicate that the integrity of previously returned data is now
223 * considered compromised. Implementations shall not use this error code
224 * to indicate a hardware failure that merely makes it impossible to
225 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
226 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
227 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
228 * instead).
229 *
230 * This error indicates an attack against the application. Implementations
231 * shall not return this error code as a consequence of the behavior of
232 * the application itself. */
233#define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)14)
234
235/** There is not enough entropy to generate random data needed
236 * for the requested action.
237 *
238 * This error indicates a failure of a hardware random generator.
239 * Application writers should note that this error can be returned not
240 * only by functions whose purpose is to generate random data, such
241 * as key, IV or nonce generation, but also by functions that execute
242 * an algorithm with a randomized result, as well as functions that
243 * use randomization of intermediate computations as a countermeasure
244 * to certain attacks.
245 *
246 * Implementations should avoid returning this error after psa_crypto_init()
247 * has succeeded. Implementations should generate sufficient
248 * entropy during initialization and subsequently use a cryptographically
249 * secure pseudorandom generator (PRNG). However implementations may return
250 * this error at any time if a policy requires the PRNG to be reseeded
251 * during normal operation. */
252#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)15)
253
254/** The signature, MAC or hash is incorrect.
255 *
256 * Verification functions return this error if the verification
257 * calculations completed successfully, and the value to be verified
258 * was determined to be incorrect.
259 *
260 * If the value to verify has an invalid size, implementations may return
261 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
262#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)16)
263
264/** The decrypted padding is incorrect.
265 *
266 * \warning In some protocols, when decrypting data, it is essential that
267 * the behavior of the application does not depend on whether the padding
268 * is correct, down to precise timing. Applications should prefer
269 * protocols that use authenticated encryption rather than plain
270 * encryption. If the application must perform a decryption of
271 * unauthenticated data, the application writer should take care not
272 * to reveal whether the padding is invalid.
273 *
274 * Implementations should strive to make valid and invalid padding
275 * as close as possible to indistinguishable to an external observer.
276 * In particular, the timing of a decryption operation should not
277 * depend on the validity of the padding. */
278#define PSA_ERROR_INVALID_PADDING ((psa_status_t)17)
279
280/** The generator has insufficient capacity left.
281 *
282 * Once a function returns this error, attempts to read from the
283 * generator will always return this error. */
284#define PSA_ERROR_INSUFFICIENT_CAPACITY ((psa_status_t)18)
285
286/** The key handle is not valid.
287 */
288#define PSA_ERROR_INVALID_HANDLE ((psa_status_t)19)
289
290/**@}*/
291
292/** \defgroup crypto_types Key and algorithm types
293 * @{
294 */
295
296/** An invalid key type value.
297 *
298 * Zero is not the encoding of any key type.
299 */
300#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
301
302/** Vendor-defined flag
303 *
304 * Key types defined by this standard will never have the
305 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
306 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
307 * respect the bitwise structure used by standard encodings whenever practical.
308 */
309#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
310
311#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x70000000)
312#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x40000000)
313#define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t)0x50000000)
314#define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t)0x60000000)
315#define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t)0x70000000)
316
317#define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t)0x10000000)
318
319/** Whether a key type is vendor-defined. */
320#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
321 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
322
323/** Whether a key type is an unstructured array of bytes.
324 *
325 * This encompasses both symmetric keys and non-key data.
326 */
327#define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \
328 (((type) & PSA_KEY_TYPE_CATEGORY_MASK & ~(psa_key_type_t)0x10000000) == \
329 PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
330
331/** Whether a key type is asymmetric: either a key pair or a public key. */
332#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
333 (((type) & PSA_KEY_TYPE_CATEGORY_MASK \
334 & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) == \
335 PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
336/** Whether a key type is the public part of a key pair. */
337#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
338 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
339/** Whether a key type is a key pair containing a private part and a public
340 * part. */
341#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
342 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR)
343/** The key pair type corresponding to a public key type.
344 *
345 * You may also pass a key pair type as \p type, it will be left unchanged.
346 *
347 * \param type A public key type or key pair type.
348 *
349 * \return The corresponding key pair type.
350 * If \p type is not a public key or a key pair,
351 * the return value is undefined.
352 */
353#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
354 ((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
355/** The public key type corresponding to a key pair type.
356 *
357 * You may also pass a key pair type as \p type, it will be left unchanged.
358 *
359 * \param type A public key type or key pair type.
360 *
361 * \return The corresponding public key type.
362 * If \p type is not a public key or a key pair,
363 * the return value is undefined.
364 */
365#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
366 ((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
367
368/** Raw data.
369 *
370 * A "key" of this type cannot be used for any cryptographic operation.
371 * Applications may use this type to store arbitrary data in the keystore. */
372#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x50000001)
373
374/** HMAC key.
375 *
376 * The key policy determines which underlying hash algorithm the key can be
377 * used for.
378 *
379 * HMAC keys should generally have the same size as the underlying hash.
380 * This size can be calculated with #PSA_HASH_SIZE(\c alg) where
381 * \c alg is the HMAC algorithm or the underlying hash algorithm. */
382#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x51000000)
383
384/** A secret for key derivation.
385 *
386 * The key policy determines which key derivation algorithm the key
387 * can be used for.
388 */
389#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x52000000)
390
391/** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher.
392 *
393 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
394 * 32 bytes (AES-256).
395 */
396#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x40000001)
397
398/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
399 *
400 * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or
401 * 24 bytes (3-key 3DES).
402 *
403 * Note that single DES and 2-key 3DES are weak and strongly
404 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
405 * is weak and deprecated and should only be used in legacy protocols.
406 */
407#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x40000002)
408
409/** Key for an cipher, AEAD or MAC algorithm based on the
410 * Camellia block cipher. */
411#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x40000003)
412
413/** Key for the RC4 stream cipher.
414 *
415 * Note that RC4 is weak and deprecated and should only be used in
416 * legacy protocols. */
417#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x40000004)
418
419/** RSA public key. */
420#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x60010000)
421/** RSA key pair (private and public key). */
422#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x70010000)
423/** Whether a key type is an RSA key (pair or public-only). */
424#define PSA_KEY_TYPE_IS_RSA(type) \
425 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
426
427/** DSA public key. */
428#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x60020000)
429/** DSA key pair (private and public key). */
430#define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x70020000)
431/** Whether a key type is an DSA key (pair or public-only). */
432#define PSA_KEY_TYPE_IS_DSA(type) \
433 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
434
435#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x60030000)
436#define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x70030000)
437#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
438/** Elliptic curve key pair. */
439#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
440 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
441/** Elliptic curve public key. */
442#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
443 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
444
445/** Whether a key type is an elliptic curve key (pair or public-only). */
446#define PSA_KEY_TYPE_IS_ECC(type) \
447 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
448 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine5e9c9cc2018-12-12 14:02:48 +0100449/** Whether a key type is an elliptic curve key pair. */
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100450#define PSA_KEY_TYPE_IS_ECC_KEYPAIR(type) \
451 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
452 PSA_KEY_TYPE_ECC_KEYPAIR_BASE)
Gilles Peskine5e9c9cc2018-12-12 14:02:48 +0100453/** Whether a key type is an elliptic curve public key. */
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100454#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
455 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
456 PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
457
458/** Extract the curve from an elliptic curve key type. */
459#define PSA_KEY_TYPE_GET_CURVE(type) \
460 ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
461 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
462 0))
463
464/* The encoding of curve identifiers is currently aligned with the
465 * TLS Supported Groups Registry (formerly known as the
466 * TLS EC Named Curve Registry)
467 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
468 * The values are defined by RFC 8422 and RFC 7027. */
469#define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001)
470#define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002)
471#define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003)
472#define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004)
473#define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005)
474#define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006)
475#define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007)
476#define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008)
477#define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009)
478#define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a)
479#define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b)
480#define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c)
481#define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d)
482#define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e)
483#define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f)
484#define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010)
485#define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011)
486#define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012)
487#define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013)
488#define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014)
489#define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015)
490#define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016)
491#define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017)
492#define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018)
493#define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019)
494#define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a)
495#define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b)
496#define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c)
497#define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d)
498#define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e)
499
Jaeden Amero8851c402019-01-11 14:20:03 +0000500/** Diffie-Hellman key exchange public key. */
501#define PSA_KEY_TYPE_DH_PUBLIC_KEY ((psa_key_type_t)0x60040000)
502/** Diffie-Hellman key exchange key pair (private and public key). */
503#define PSA_KEY_TYPE_DH_KEYPAIR ((psa_key_type_t)0x70040000)
504/** Whether a key type is a Diffie-Hellman key exchange key (pair or
505 * public-only). */
506#define PSA_KEY_TYPE_IS_DH(type) \
507 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_DH_PUBLIC_KEY)
508
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100509/** The block size of a block cipher.
510 *
511 * \param type A cipher key type (value of type #psa_key_type_t).
512 *
513 * \return The block size for a block cipher, or 1 for a stream cipher.
514 * The return value is undefined if \p type is not a supported
515 * cipher key type.
516 *
517 * \note It is possible to build stream cipher algorithms on top of a block
518 * cipher, for example CTR mode (#PSA_ALG_CTR).
519 * This macro only takes the key type into account, so it cannot be
520 * used to determine the size of the data that #psa_cipher_update()
521 * might buffer for future processing in general.
522 *
523 * \note This macro returns a compile-time constant if its argument is one.
524 *
525 * \warning This macro may evaluate its argument multiple times.
526 */
527#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
528 ( \
529 (type) == PSA_KEY_TYPE_AES ? 16 : \
530 (type) == PSA_KEY_TYPE_DES ? 8 : \
531 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
532 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
533 0)
534
535#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
536#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
537#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
538#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
539#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
540#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
541#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
542#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
Gilles Peskine6843c292019-01-18 16:44:49 +0100543#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x20000000)
544#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x30000000)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100545
546#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
547 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
548
549/** Whether the specified algorithm is a hash algorithm.
550 *
551 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
552 *
553 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
554 * This macro may return either 0 or 1 if \p alg is not a supported
555 * algorithm identifier.
556 */
557#define PSA_ALG_IS_HASH(alg) \
558 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
559
560/** Whether the specified algorithm is a MAC algorithm.
561 *
562 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
563 *
564 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
565 * This macro may return either 0 or 1 if \p alg is not a supported
566 * algorithm identifier.
567 */
568#define PSA_ALG_IS_MAC(alg) \
569 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
570
571/** Whether the specified algorithm is a symmetric cipher algorithm.
572 *
573 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
574 *
575 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
576 * This macro may return either 0 or 1 if \p alg is not a supported
577 * algorithm identifier.
578 */
579#define PSA_ALG_IS_CIPHER(alg) \
580 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
581
582/** Whether the specified algorithm is an authenticated encryption
583 * with associated data (AEAD) algorithm.
584 *
585 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
586 *
587 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
588 * This macro may return either 0 or 1 if \p alg is not a supported
589 * algorithm identifier.
590 */
591#define PSA_ALG_IS_AEAD(alg) \
592 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
593
594/** Whether the specified algorithm is a public-key signature algorithm.
595 *
596 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
597 *
598 * \return 1 if \p alg is a public-key signature algorithm, 0 otherwise.
599 * This macro may return either 0 or 1 if \p alg is not a supported
600 * algorithm identifier.
601 */
602#define PSA_ALG_IS_SIGN(alg) \
603 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
604
605/** Whether the specified algorithm is a public-key encryption algorithm.
606 *
607 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
608 *
609 * \return 1 if \p alg is a public-key encryption algorithm, 0 otherwise.
610 * This macro may return either 0 or 1 if \p alg is not a supported
611 * algorithm identifier.
612 */
613#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
614 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
615
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100616/** Whether the specified algorithm is a key agreement algorithm.
617 *
618 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
619 *
620 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
621 * This macro may return either 0 or 1 if \p alg is not a supported
622 * algorithm identifier.
623 */
624#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
Gilles Peskine47e79fb2019-02-08 11:24:59 +0100625 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100626
627/** Whether the specified algorithm is a key derivation algorithm.
628 *
629 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
630 *
631 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
632 * This macro may return either 0 or 1 if \p alg is not a supported
633 * algorithm identifier.
634 */
635#define PSA_ALG_IS_KEY_DERIVATION(alg) \
636 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
637
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100638#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100639
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100640#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
641#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
642#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
643#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
644#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
645/** SHA2-224 */
646#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
647/** SHA2-256 */
648#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
649/** SHA2-384 */
650#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
651/** SHA2-512 */
652#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
653/** SHA2-512/224 */
654#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
655/** SHA2-512/256 */
656#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
657/** SHA3-224 */
658#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
659/** SHA3-256 */
660#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
661/** SHA3-384 */
662#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
663/** SHA3-512 */
664#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
665
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100666/** Allow any hash algorithm.
667 *
668 * This value may only be used to form the algorithm usage field of a policy
669 * for a signature algorithm that is parametrized by a hash. That is,
670 * suppose that `PSA_xxx_SIGNATURE` is one of the following macros:
671 * - #PSA_ALG_RSA_PKCS1V15_SIGN, #PSA_ALG_RSA_PSS,
672 * - #PSA_ALG_DSA, #PSA_ALG_DETERMINISTIC_DSA,
673 * - #PSA_ALG_ECDSA, #PSA_ALG_DETERMINISTIC_ECDSA.
674 * Then you may create a key as follows:
675 * - Set the key usage field using #PSA_ALG_ANY_HASH, for example:
676 * ```
677 * psa_key_policy_set_usage(&policy,
678 * PSA_KEY_USAGE_SIGN, //or PSA_KEY_USAGE_VERIFY
679 * PSA_xxx_SIGNATURE(PSA_ALG_ANY_HASH));
680 * psa_set_key_policy(handle, &policy);
681 * ```
682 * - Import or generate key material.
683 * - Call psa_asymmetric_sign() or psa_asymmetric_verify(), passing
684 * an algorithm built from `PSA_xxx_SIGNATURE` and a specific hash. Each
685 * call to sign or verify a message may use a different hash.
686 * ```
687 * psa_asymmetric_sign(handle, PSA_xxx_SIGNATURE(PSA_ALG_SHA_256), ...);
688 * psa_asymmetric_sign(handle, PSA_xxx_SIGNATURE(PSA_ALG_SHA_512), ...);
689 * psa_asymmetric_sign(handle, PSA_xxx_SIGNATURE(PSA_ALG_SHA3_256), ...);
690 * ```
691 *
692 * This value may not be used to build other algorithms that are
693 * parametrized over a hash. For any valid use of this macro to build
694 * an algorithm `\p alg`, #PSA_ALG_IS_HASH_AND_SIGN(\p alg) is true.
695 *
696 * This value may not be used to build an algorithm specification to
697 * perform an operation. It is only valid to build policies.
698 */
699#define PSA_ALG_ANY_HASH ((psa_algorithm_t)0x010000ff)
700
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100701#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
702#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
703/** Macro to build an HMAC algorithm.
704 *
705 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
706 *
707 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
708 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
709 *
710 * \return The corresponding HMAC algorithm.
711 * \return Unspecified if \p alg is not a supported
712 * hash algorithm.
713 */
714#define PSA_ALG_HMAC(hash_alg) \
715 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
716
717#define PSA_ALG_HMAC_GET_HASH(hmac_alg) \
718 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
719
720/** Whether the specified algorithm is an HMAC algorithm.
721 *
722 * HMAC is a family of MAC algorithms that are based on a hash function.
723 *
724 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
725 *
726 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
727 * This macro may return either 0 or 1 if \p alg is not a supported
728 * algorithm identifier.
729 */
730#define PSA_ALG_IS_HMAC(alg) \
731 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
732 PSA_ALG_HMAC_BASE)
733
734/* In the encoding of a MAC algorithm, the bits corresponding to
735 * PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is
736 * truncated. As an exception, the value 0 means the untruncated algorithm,
737 * whatever its length is. The length is encoded in 6 bits, so it can
738 * reach up to 63; the largest MAC is 64 bytes so its trivial truncation
739 * to full length is correctly encoded as 0 and any non-trivial truncation
740 * is correctly encoded as a value between 1 and 63. */
741#define PSA_ALG_MAC_TRUNCATION_MASK ((psa_algorithm_t)0x00003f00)
742#define PSA_MAC_TRUNCATION_OFFSET 8
743
744/** Macro to build a truncated MAC algorithm.
745 *
746 * A truncated MAC algorithm is identical to the corresponding MAC
747 * algorithm except that the MAC value for the truncated algorithm
748 * consists of only the first \p mac_length bytes of the MAC value
749 * for the untruncated algorithm.
750 *
751 * \note This macro may allow constructing algorithm identifiers that
752 * are not valid, either because the specified length is larger
753 * than the untruncated MAC or because the specified length is
754 * smaller than permitted by the implementation.
755 *
756 * \note It is implementation-defined whether a truncated MAC that
757 * is truncated to the same length as the MAC of the untruncated
758 * algorithm is considered identical to the untruncated algorithm
759 * for policy comparison purposes.
760 *
761 * \param alg A MAC algorithm identifier (value of type
762 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
763 * is true). This may be a truncated or untruncated
764 * MAC algorithm.
765 * \param mac_length Desired length of the truncated MAC in bytes.
766 * This must be at most the full length of the MAC
767 * and must be at least an implementation-specified
768 * minimum. The implementation-specified minimum
769 * shall not be zero.
770 *
771 * \return The corresponding MAC algorithm with the specified
772 * length.
773 * \return Unspecified if \p alg is not a supported
774 * MAC algorithm or if \p mac_length is too small or
775 * too large for the specified MAC algorithm.
776 */
777#define PSA_ALG_TRUNCATED_MAC(alg, mac_length) \
778 (((alg) & ~PSA_ALG_MAC_TRUNCATION_MASK) | \
779 ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))
780
781/** Macro to build the base MAC algorithm corresponding to a truncated
782 * MAC algorithm.
783 *
784 * \param alg A MAC algorithm identifier (value of type
785 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
786 * is true). This may be a truncated or untruncated
787 * MAC algorithm.
788 *
789 * \return The corresponding base MAC algorithm.
790 * \return Unspecified if \p alg is not a supported
791 * MAC algorithm.
792 */
793#define PSA_ALG_FULL_LENGTH_MAC(alg) \
794 ((alg) & ~PSA_ALG_MAC_TRUNCATION_MASK)
795
796/** Length to which a MAC algorithm is truncated.
797 *
798 * \param alg A MAC algorithm identifier (value of type
799 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
800 * is true).
801 *
802 * \return Length of the truncated MAC in bytes.
803 * \return 0 if \p alg is a non-truncated MAC algorithm.
804 * \return Unspecified if \p alg is not a supported
805 * MAC algorithm.
806 */
807#define PSA_MAC_TRUNCATED_LENGTH(alg) \
808 (((alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
809
810#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
811#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
812#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
813#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
814
815/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
816 *
817 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
818 *
819 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
820 * This macro may return either 0 or 1 if \p alg is not a supported
821 * algorithm identifier.
822 */
823#define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) \
824 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
825 PSA_ALG_CIPHER_MAC_BASE)
826
827#define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t)0x00800000)
828#define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
829
830/** Whether the specified algorithm is a stream cipher.
831 *
832 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
833 * by applying a bitwise-xor with a stream of bytes that is generated
834 * from a key.
835 *
836 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
837 *
838 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
839 * This macro may return either 0 or 1 if \p alg is not a supported
840 * algorithm identifier or if it is not a symmetric cipher algorithm.
841 */
842#define PSA_ALG_IS_STREAM_CIPHER(alg) \
843 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
844 (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
845
846/** The ARC4 stream cipher algorithm.
847 */
848#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800001)
849
850/** The CTR stream cipher mode.
851 *
852 * CTR is a stream cipher which is built from a block cipher.
853 * The underlying block cipher is determined by the key type.
854 * For example, to use AES-128-CTR, use this algorithm with
855 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
856 */
857#define PSA_ALG_CTR ((psa_algorithm_t)0x04c00001)
858
859#define PSA_ALG_CFB ((psa_algorithm_t)0x04c00002)
860
861#define PSA_ALG_OFB ((psa_algorithm_t)0x04c00003)
862
863/** The XTS cipher mode.
864 *
865 * XTS is a cipher mode which is built from a block cipher. It requires at
866 * least one full block of input, but beyond this minimum the input
867 * does not need to be a whole number of blocks.
868 */
869#define PSA_ALG_XTS ((psa_algorithm_t)0x044000ff)
870
871/** The CBC block cipher chaining mode, with no padding.
872 *
873 * The underlying block cipher is determined by the key type.
874 *
875 * This symmetric cipher mode can only be used with messages whose lengths
876 * are whole number of blocks for the chosen block cipher.
877 */
878#define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t)0x04600100)
879
880/** The CBC block cipher chaining mode with PKCS#7 padding.
881 *
882 * The underlying block cipher is determined by the key type.
883 *
884 * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
885 */
886#define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t)0x04600101)
887
Gilles Peskine9153ec02019-02-15 13:02:02 +0100888/** The CCM authenticated encryption algorithm.
889 */
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100890#define PSA_ALG_CCM ((psa_algorithm_t)0x06001001)
Gilles Peskine9153ec02019-02-15 13:02:02 +0100891
892/** The GCM authenticated encryption algorithm.
893 */
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100894#define PSA_ALG_GCM ((psa_algorithm_t)0x06001002)
895
896/* In the encoding of a AEAD algorithm, the bits corresponding to
897 * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.
898 * The constants for default lengths follow this encoding.
899 */
900#define PSA_ALG_AEAD_TAG_LENGTH_MASK ((psa_algorithm_t)0x00003f00)
901#define PSA_AEAD_TAG_LENGTH_OFFSET 8
902
903/** Macro to build a shortened AEAD algorithm.
904 *
905 * A shortened AEAD algorithm is similar to the corresponding AEAD
906 * algorithm, but has an authentication tag that consists of fewer bytes.
907 * Depending on the algorithm, the tag length may affect the calculation
908 * of the ciphertext.
909 *
910 * \param alg A AEAD algorithm identifier (value of type
911 * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p alg)
912 * is true).
913 * \param tag_length Desired length of the authentication tag in bytes.
914 *
915 * \return The corresponding AEAD algorithm with the specified
916 * length.
917 * \return Unspecified if \p alg is not a supported
918 * AEAD algorithm or if \p tag_length is not valid
919 * for the specified AEAD algorithm.
920 */
921#define PSA_ALG_AEAD_WITH_TAG_LENGTH(alg, tag_length) \
922 (((alg) & ~PSA_ALG_AEAD_TAG_LENGTH_MASK) | \
923 ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \
924 PSA_ALG_AEAD_TAG_LENGTH_MASK))
925
926/** Calculate the corresponding AEAD algorithm with the default tag length.
927 *
928 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
929 * #PSA_ALG_IS_AEAD(\p alg) is true).
930 *
931 * \return The corresponding AEAD algorithm with the default tag length
932 * for that algorithm.
933 */
934#define PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(alg) \
935 ( \
936 PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(alg, PSA_ALG_CCM) \
937 PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(alg, PSA_ALG_GCM) \
938 0)
939#define PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(alg, ref) \
940 PSA_ALG_AEAD_WITH_TAG_LENGTH(alg, 0) == \
941 PSA_ALG_AEAD_WITH_TAG_LENGTH(ref, 0) ? \
942 ref :
943
944#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
945/** RSA PKCS#1 v1.5 signature with hashing.
946 *
947 * This is the signature scheme defined by RFC 8017
948 * (PKCS#1: RSA Cryptography Specifications) under the name
949 * RSASSA-PKCS1-v1_5.
950 *
951 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
952 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100953 * This includes #PSA_ALG_ANY_HASH
954 * when specifying the algorithm in a usage policy.
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100955 *
956 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
957 * \return Unspecified if \p alg is not a supported
958 * hash algorithm.
959 */
960#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
961 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
962/** Raw PKCS#1 v1.5 signature.
963 *
964 * The input to this algorithm is the DigestInfo structure used by
965 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
966 * steps 3&ndash;6.
967 */
968#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
969#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
970 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
971
972#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
973/** RSA PSS signature with hashing.
974 *
975 * This is the signature scheme defined by RFC 8017
976 * (PKCS#1: RSA Cryptography Specifications) under the name
977 * RSASSA-PSS, with the message generation function MGF1, and with
978 * a salt length equal to the length of the hash. The specified
979 * hash algorithm is used to hash the input message, to create the
980 * salted hash, and for the mask generation.
981 *
982 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
983 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100984 * This includes #PSA_ALG_ANY_HASH
985 * when specifying the algorithm in a usage policy.
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100986 *
987 * \return The corresponding RSA PSS signature algorithm.
988 * \return Unspecified if \p alg is not a supported
989 * hash algorithm.
990 */
991#define PSA_ALG_RSA_PSS(hash_alg) \
992 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
993#define PSA_ALG_IS_RSA_PSS(alg) \
994 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
995
996#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
997/** DSA signature with hashing.
998 *
999 * This is the signature scheme defined by FIPS 186-4,
1000 * with a random per-message secret number (*k*).
1001 *
1002 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1003 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001004 * This includes #PSA_ALG_ANY_HASH
1005 * when specifying the algorithm in a usage policy.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001006 *
1007 * \return The corresponding DSA signature algorithm.
1008 * \return Unspecified if \p alg is not a supported
1009 * hash algorithm.
1010 */
1011#define PSA_ALG_DSA(hash_alg) \
1012 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1013#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
1014#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
Gilles Peskine9153ec02019-02-15 13:02:02 +01001015/** Deterministic DSA signature with hashing.
1016 *
1017 * This is the deterministic variant defined by RFC 6979 of
1018 * the signature scheme defined by FIPS 186-4.
1019 *
1020 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1021 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1022 * This includes #PSA_ALG_ANY_HASH
1023 * when specifying the algorithm in a usage policy.
1024 *
1025 * \return The corresponding DSA signature algorithm.
1026 * \return Unspecified if \p alg is not a supported
1027 * hash algorithm.
1028 */
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001029#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
1030 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1031#define PSA_ALG_IS_DSA(alg) \
1032 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
1033 PSA_ALG_DSA_BASE)
1034#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
1035 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
1036#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
1037 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
1038#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
1039 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
1040
1041#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
1042/** ECDSA signature with hashing.
1043 *
1044 * This is the ECDSA signature scheme defined by ANSI X9.62,
1045 * with a random per-message secret number (*k*).
1046 *
1047 * The representation of the signature as a byte string consists of
1048 * the concatentation of the signature values *r* and *s*. Each of
1049 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
1050 * of the base point of the curve in octets. Each value is represented
1051 * in big-endian order (most significant octet first).
1052 *
1053 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1054 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001055 * This includes #PSA_ALG_ANY_HASH
1056 * when specifying the algorithm in a usage policy.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001057 *
1058 * \return The corresponding ECDSA signature algorithm.
1059 * \return Unspecified if \p alg is not a supported
1060 * hash algorithm.
1061 */
1062#define PSA_ALG_ECDSA(hash_alg) \
1063 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1064/** ECDSA signature without hashing.
1065 *
1066 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
1067 * without specifying a hash algorithm. This algorithm may only be
1068 * used to sign or verify a sequence of bytes that should be an
1069 * already-calculated hash. Note that the input is padded with
1070 * zeros on the left or truncated on the left as required to fit
1071 * the curve size.
1072 */
1073#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
1074#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
1075/** Deterministic ECDSA signature with hashing.
1076 *
1077 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
1078 *
1079 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
1080 *
1081 * Note that when this algorithm is used for verification, signatures
1082 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
1083 * same private key are accepted. In other words,
1084 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
1085 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
1086 *
1087 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1088 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001089 * This includes #PSA_ALG_ANY_HASH
1090 * when specifying the algorithm in a usage policy.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001091 *
1092 * \return The corresponding deterministic ECDSA signature
1093 * algorithm.
1094 * \return Unspecified if \p alg is not a supported
1095 * hash algorithm.
1096 */
1097#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
1098 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1099#define PSA_ALG_IS_ECDSA(alg) \
1100 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
1101 PSA_ALG_ECDSA_BASE)
1102#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
1103 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
1104#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
1105 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1106#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
1107 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1108
Gilles Peskined35b4892019-01-14 16:02:15 +01001109/** Whether the specified algorithm is a hash-and-sign algorithm.
1110 *
1111 * Hash-and-sign algorithms are public-key signature algorithms structured
1112 * in two parts: first the calculation of a hash in a way that does not
1113 * depend on the key, then the calculation of a signature from the
1114 * hash value and the key.
1115 *
1116 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1117 *
1118 * \return 1 if \p alg is a hash-and-sign algorithm, 0 otherwise.
1119 * This macro may return either 0 or 1 if \p alg is not a supported
1120 * algorithm identifier.
1121 */
1122#define PSA_ALG_IS_HASH_AND_SIGN(alg) \
1123 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
1124 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg))
1125
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001126/** Get the hash used by a hash-and-sign signature algorithm.
1127 *
1128 * A hash-and-sign algorithm is a signature algorithm which is
1129 * composed of two phases: first a hashing phase which does not use
1130 * the key and produces a hash of the input message, then a signing
1131 * phase which only uses the hash and the key and not the message
1132 * itself.
1133 *
1134 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
1135 * #PSA_ALG_IS_SIGN(\p alg) is true).
1136 *
1137 * \return The underlying hash algorithm if \p alg is a hash-and-sign
1138 * algorithm.
1139 * \return 0 if \p alg is a signature algorithm that does not
1140 * follow the hash-and-sign structure.
1141 * \return Unspecified if \p alg is not a signature algorithm or
1142 * if it is not supported by the implementation.
1143 */
1144#define PSA_ALG_SIGN_GET_HASH(alg) \
Gilles Peskined35b4892019-01-14 16:02:15 +01001145 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001146 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
1147 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1148 0)
1149
1150/** RSA PKCS#1 v1.5 encryption.
1151 */
1152#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
1153
1154#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
1155/** RSA OAEP encryption.
1156 *
1157 * This is the encryption scheme defined by RFC 8017
1158 * (PKCS#1: RSA Cryptography Specifications) under the name
1159 * RSAES-OAEP, with the message generation function MGF1.
1160 *
1161 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1162 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1163 * for MGF1.
1164 *
1165 * \return The corresponding RSA OAEP signature algorithm.
1166 * \return Unspecified if \p alg is not a supported
1167 * hash algorithm.
1168 */
1169#define PSA_ALG_RSA_OAEP(hash_alg) \
1170 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1171#define PSA_ALG_IS_RSA_OAEP(alg) \
1172 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
1173#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1174 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1175 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1176 0)
1177
Gilles Peskine6843c292019-01-18 16:44:49 +01001178#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x20000100)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001179/** Macro to build an HKDF algorithm.
1180 *
1181 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1182 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01001183 * This key derivation algorithm uses the following inputs:
1184 * - #PSA_KDF_STEP_SALT is the salt used in the "extract" step.
1185 * It is optional; if omitted, the derivation uses an empty salt.
1186 * - #PSA_KDF_STEP_SECRET is the secret key used in the "extract" step.
1187 * - #PSA_KDF_STEP_INFO is the info string used in the "expand" step.
1188 * You must pass #PSA_KDF_STEP_SALT before #PSA_KDF_STEP_SECRET.
1189 * You may pass #PSA_KDF_STEP_INFO at any time after steup and before
1190 * starting to generate output.
1191 *
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001192 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1193 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1194 *
1195 * \return The corresponding HKDF algorithm.
1196 * \return Unspecified if \p alg is not a supported
1197 * hash algorithm.
1198 */
1199#define PSA_ALG_HKDF(hash_alg) \
1200 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1201/** Whether the specified algorithm is an HKDF algorithm.
1202 *
1203 * HKDF is a family of key derivation algorithms that are based on a hash
1204 * function and the HMAC construction.
1205 *
1206 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1207 *
1208 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1209 * This macro may return either 0 or 1 if \c alg is not a supported
1210 * key derivation algorithm identifier.
1211 */
1212#define PSA_ALG_IS_HKDF(alg) \
1213 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1214#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1215 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1216
Gilles Peskine6843c292019-01-18 16:44:49 +01001217#define PSA_ALG_TLS12_PRF_BASE ((psa_algorithm_t)0x20000200)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001218/** Macro to build a TLS-1.2 PRF algorithm.
1219 *
1220 * TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule,
1221 * specified in Section 5 of RFC 5246. It is based on HMAC and can be
1222 * used with either SHA-256 or SHA-384.
1223 *
1224 * For the application to TLS-1.2, the salt and label arguments passed
1225 * to psa_key_derivation() are what's called 'seed' and 'label' in RFC 5246,
1226 * respectively. For example, for TLS key expansion, the salt is the
1227 * concatenation of ServerHello.Random + ClientHello.Random,
1228 * while the label is "key expansion".
1229 *
1230 * For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA256)` represents the
1231 * TLS 1.2 PRF using HMAC-SHA-256.
1232 *
1233 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1234 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1235 *
1236 * \return The corresponding TLS-1.2 PRF algorithm.
1237 * \return Unspecified if \p alg is not a supported
1238 * hash algorithm.
1239 */
1240#define PSA_ALG_TLS12_PRF(hash_alg) \
1241 (PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1242
1243/** Whether the specified algorithm is a TLS-1.2 PRF algorithm.
1244 *
1245 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1246 *
1247 * \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise.
1248 * This macro may return either 0 or 1 if \c alg is not a supported
1249 * key derivation algorithm identifier.
1250 */
1251#define PSA_ALG_IS_TLS12_PRF(alg) \
1252 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE)
1253#define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg) \
1254 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1255
Gilles Peskine6843c292019-01-18 16:44:49 +01001256#define PSA_ALG_TLS12_PSK_TO_MS_BASE ((psa_algorithm_t)0x20000300)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001257/** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm.
1258 *
1259 * In a pure-PSK handshake in TLS 1.2, the master secret is derived
1260 * from the PreSharedKey (PSK) through the application of padding
1261 * (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5).
1262 * The latter is based on HMAC and can be used with either SHA-256
1263 * or SHA-384.
1264 *
1265 * For the application to TLS-1.2, the salt passed to psa_key_derivation()
1266 * (and forwarded to the TLS-1.2 PRF) is the concatenation of the
1267 * ClientHello.Random + ServerHello.Random, while the label is "master secret"
1268 * or "extended master secret".
1269 *
1270 * For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA256)` represents the
1271 * TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256.
1272 *
1273 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1274 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1275 *
1276 * \return The corresponding TLS-1.2 PSK to MS algorithm.
1277 * \return Unspecified if \p alg is not a supported
1278 * hash algorithm.
1279 */
1280#define PSA_ALG_TLS12_PSK_TO_MS(hash_alg) \
1281 (PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1282
1283/** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm.
1284 *
1285 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1286 *
1287 * \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise.
1288 * This macro may return either 0 or 1 if \c alg is not a supported
1289 * key derivation algorithm identifier.
1290 */
1291#define PSA_ALG_IS_TLS12_PSK_TO_MS(alg) \
1292 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE)
1293#define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg) \
1294 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1295
Gilles Peskine6843c292019-01-18 16:44:49 +01001296#define PSA_ALG_KEY_DERIVATION_MASK ((psa_algorithm_t)0x080fffff)
1297#define PSA_ALG_KEY_AGREEMENT_MASK ((psa_algorithm_t)0x10f00000)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001298
Gilles Peskine6843c292019-01-18 16:44:49 +01001299/** Macro to build a combined algorithm that chains a key agreement with
1300 * a key derivation.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001301 *
Gilles Peskine6843c292019-01-18 16:44:49 +01001302 * \param ka_alg A key agreement algorithm (\c PSA_ALG_XXX value such
1303 * that #PSA_ALG_IS_KEY_AGREEMENT(\p ka_alg) is true).
1304 * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
1305 * that #PSA_ALG_IS_KEY_DERIVATION(\p kdf_alg) is true).
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001306 *
Gilles Peskine6843c292019-01-18 16:44:49 +01001307 * \return The corresponding key agreement and derivation
1308 * algorithm.
1309 * \return Unspecified if \p ka_alg is not a supported
1310 * key agreement algorithm or \p kdf_alg is not a
1311 * supported key derivation algorithm.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001312 */
Gilles Peskine6843c292019-01-18 16:44:49 +01001313#define PSA_ALG_KEY_AGREEMENT(ka_alg, kdf_alg) \
1314 ((ka_alg) | (kdf_alg))
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001315
1316#define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) \
1317 (((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION)
1318
Gilles Peskine6843c292019-01-18 16:44:49 +01001319#define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) \
1320 (((alg) & PSA_ALG_KEY_AGREEMENT_MASK) | PSA_ALG_CATEGORY_KEY_AGREEMENT)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001321
Gilles Peskine47e79fb2019-02-08 11:24:59 +01001322/** Whether the specified algorithm is a raw key agreement algorithm.
1323 *
1324 * A raw key agreement algorithm is one that does not specify
1325 * a key derivation function.
1326 * Usually, raw key agreement algorithms are constructed directly with
1327 * a \c PSA_ALG_xxx macro while non-raw key agreement algorithms are
1328 * constructed with PSA_ALG_KEY_AGREEMENT().
1329 *
1330 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1331 *
1332 * \return 1 if \p alg is a raw key agreement algorithm, 0 otherwise.
1333 * This macro may return either 0 or 1 if \p alg is not a supported
1334 * algorithm identifier.
1335 */
Gilles Peskine6843c292019-01-18 16:44:49 +01001336#define PSA_ALG_IS_RAW_KEY_AGREEMENT(alg) \
Gilles Peskine47e79fb2019-02-08 11:24:59 +01001337 (PSA_ALG_IS_KEY_AGREEMENT(alg) && \
1338 PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) == PSA_ALG_CATEGORY_KEY_DERIVATION)
Gilles Peskine6843c292019-01-18 16:44:49 +01001339
1340#define PSA_ALG_IS_KEY_DERIVATION_OR_AGREEMENT(alg) \
1341 ((PSA_ALG_IS_KEY_DERIVATION(alg) || PSA_ALG_IS_KEY_AGREEMENT(alg)))
1342
1343/** The finite-field Diffie-Hellman (DH) key agreement algorithm.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001344 *
Gilles Peskine2e37c0d2019-03-05 19:32:02 +01001345 * The shared secret produced by key agreement is
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001346 * `g^{ab}` in big-endian format.
1347 * It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p`
1348 * in bits.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001349 */
Gilles Peskine6843c292019-01-18 16:44:49 +01001350#define PSA_ALG_FFDH ((psa_algorithm_t)0x30100000)
1351
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001352/** Whether the specified algorithm is a finite field Diffie-Hellman algorithm.
1353 *
Gilles Peskine2e37c0d2019-03-05 19:32:02 +01001354 * This includes the raw finite field Diffie-Hellman algorithm as well as
1355 * finite-field Diffie-Hellman followed by any supporter key derivation
1356 * algorithm.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001357 *
1358 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1359 *
1360 * \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise.
1361 * This macro may return either 0 or 1 if \c alg is not a supported
1362 * key agreement algorithm identifier.
1363 */
1364#define PSA_ALG_IS_FFDH(alg) \
Gilles Peskine6843c292019-01-18 16:44:49 +01001365 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001366
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001367/** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm.
1368 *
Gilles Peskine6843c292019-01-18 16:44:49 +01001369 * The shared secret produced by key agreement is the x-coordinate of
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001370 * the shared secret point. It is always `ceiling(m / 8)` bytes long where
1371 * `m` is the bit size associated with the curve, i.e. the bit size of the
1372 * order of the curve's coordinate field. When `m` is not a multiple of 8,
1373 * the byte containing the most significant bit of the shared secret
1374 * is padded with zero bits. The byte order is either little-endian
1375 * or big-endian depending on the curve type.
1376 *
1377 * - For Montgomery curves (curve types `PSA_ECC_CURVE_CURVEXXX`),
1378 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1379 * in little-endian byte order.
1380 * The bit size is 448 for Curve448 and 255 for Curve25519.
1381 * - For Weierstrass curves over prime fields (curve types
1382 * `PSA_ECC_CURVE_SECPXXX` and `PSA_ECC_CURVE_BRAINPOOL_PXXX`),
1383 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1384 * in big-endian byte order.
1385 * The bit size is `m = ceiling(log_2(p))` for the field `F_p`.
1386 * - For Weierstrass curves over binary fields (curve types
1387 * `PSA_ECC_CURVE_SECTXXX`),
1388 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1389 * in big-endian byte order.
1390 * The bit size is `m` for the field `F_{2^m}`.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001391 */
Gilles Peskine6843c292019-01-18 16:44:49 +01001392#define PSA_ALG_ECDH ((psa_algorithm_t)0x30200000)
1393
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001394/** Whether the specified algorithm is an elliptic curve Diffie-Hellman
1395 * algorithm.
1396 *
Gilles Peskine2e37c0d2019-03-05 19:32:02 +01001397 * This includes the raw elliptic curve Diffie-Hellman algorithm as well as
1398 * elliptic curve Diffie-Hellman followed by any supporter key derivation
1399 * algorithm.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001400 *
1401 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1402 *
1403 * \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm,
1404 * 0 otherwise.
1405 * This macro may return either 0 or 1 if \c alg is not a supported
1406 * key agreement algorithm identifier.
1407 */
1408#define PSA_ALG_IS_ECDH(alg) \
Gilles Peskine6843c292019-01-18 16:44:49 +01001409 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH)
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001410
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001411/** Whether the specified algorithm encoding is a wildcard.
1412 *
1413 * Wildcard values may only be used to set the usage algorithm field in
1414 * a policy, not to perform an operation.
1415 *
1416 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1417 *
1418 * \return 1 if \c alg is a wildcard algorithm encoding.
1419 * \return 0 if \c alg is a non-wildcard algorithm encoding (suitable for
1420 * an operation).
1421 * \return This macro may return either 0 or 1 if \c alg is not a supported
1422 * algorithm identifier.
1423 */
1424#define PSA_ALG_IS_WILDCARD(alg) \
1425 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
1426 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH : \
1427 (alg) == PSA_ALG_ANY_HASH)
1428
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001429/**@}*/
1430
1431/** \defgroup key_lifetimes Key lifetimes
1432 * @{
1433 */
1434
1435/** A volatile key only exists as long as the handle to it is not closed.
1436 * The key material is guaranteed to be erased on a power reset.
1437 */
1438#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1439
1440/** The default storage area for persistent keys.
1441 *
1442 * A persistent key remains in storage until it is explicitly destroyed or
1443 * until the corresponding storage area is wiped. This specification does
1444 * not define any mechanism to wipe a storage area, but implementations may
1445 * provide their own mechanism (for example to perform a factory reset,
1446 * to prepare for device refurbishment, or to uninstall an application).
1447 *
1448 * This lifetime value is the default storage area for the calling
1449 * application. Implementations may offer other storage areas designated
1450 * by other lifetime values as implementation-specific extensions.
1451 */
1452#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1453
1454/**@}*/
1455
1456/** \defgroup policy Key policies
1457 * @{
1458 */
1459
1460/** Whether the key may be exported.
1461 *
1462 * A public key or the public part of a key pair may always be exported
1463 * regardless of the value of this permission flag.
1464 *
1465 * If a key does not have export permission, implementations shall not
1466 * allow the key to be exported in plain form from the cryptoprocessor,
1467 * whether through psa_export_key() or through a proprietary interface.
1468 * The key may however be exportable in a wrapped form, i.e. in a form
1469 * where it is encrypted by another key.
1470 */
1471#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1472
1473/** Whether the key may be used to encrypt a message.
1474 *
1475 * This flag allows the key to be used for a symmetric encryption operation,
1476 * for an AEAD encryption-and-authentication operation,
1477 * or for an asymmetric encryption operation,
1478 * if otherwise permitted by the key's type and policy.
1479 *
1480 * For a key pair, this concerns the public key.
1481 */
1482#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
1483
1484/** Whether the key may be used to decrypt a message.
1485 *
1486 * This flag allows the key to be used for a symmetric decryption operation,
1487 * for an AEAD decryption-and-verification operation,
1488 * or for an asymmetric decryption operation,
1489 * if otherwise permitted by the key's type and policy.
1490 *
1491 * For a key pair, this concerns the private key.
1492 */
1493#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
1494
1495/** Whether the key may be used to sign a message.
1496 *
1497 * This flag allows the key to be used for a MAC calculation operation
1498 * or for an asymmetric signature operation,
1499 * if otherwise permitted by the key's type and policy.
1500 *
1501 * For a key pair, this concerns the private key.
1502 */
1503#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
1504
1505/** Whether the key may be used to verify a message signature.
1506 *
1507 * This flag allows the key to be used for a MAC verification operation
1508 * or for an asymmetric signature verification operation,
1509 * if otherwise permitted by by the key's type and policy.
1510 *
1511 * For a key pair, this concerns the public key.
1512 */
1513#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
1514
1515/** Whether the key may be used to derive other keys.
1516 */
1517#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00001000)
1518
1519/**@}*/
1520
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01001521/** \defgroup derivation Key derivation
1522 * @{
1523 */
1524
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01001525/** A secret input for key derivation.
1526 *
1527 * This must be a key of type #PSA_KEY_TYPE_DERIVE.
1528 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01001529#define PSA_KDF_STEP_SECRET ((psa_key_derivation_step_t)0x0101)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01001530
1531/** A label for key derivation.
1532 *
1533 * This must be a direct input.
1534 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01001535#define PSA_KDF_STEP_LABEL ((psa_key_derivation_step_t)0x0201)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01001536
1537/** A salt for key derivation.
1538 *
1539 * This must be a direct input.
1540 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01001541#define PSA_KDF_STEP_SALT ((psa_key_derivation_step_t)0x0202)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01001542
1543/** An information string for key derivation.
1544 *
1545 * This must be a direct input.
1546 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01001547#define PSA_KDF_STEP_INFO ((psa_key_derivation_step_t)0x0203)
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01001548
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01001549/**@}*/
1550
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001551#endif /* PSA_CRYPTO_VALUES_H */