blob: d42d8c28a8364cc912de8a339f6604012368c351 [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
David Saadab4ecc272019-02-14 13:48:10 +020043/* PSA error codes */
44
Gilles Peskinef3b731e2018-12-12 13:38:31 +010045/** The action was completed successfully. */
46#define PSA_SUCCESS ((psa_status_t)0)
Gilles Peskinef3b731e2018-12-12 13:38:31 +010047
48/** An error occurred that does not correspond to any defined
49 * failure cause.
50 *
51 * Implementations may use this error code if none of the other standard
52 * error codes are applicable. */
David Saadab4ecc272019-02-14 13:48:10 +020053#define PSA_ERROR_GENERIC_ERROR ((psa_status_t)-132)
Gilles Peskinef3b731e2018-12-12 13:38:31 +010054
55/** The requested operation or a parameter is not supported
56 * by this implementation.
57 *
58 * Implementations should return this error code when an enumeration
59 * parameter such as a key type, algorithm, etc. is not recognized.
60 * If a combination of parameters is recognized and identified as
61 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
David Saadab4ecc272019-02-14 13:48:10 +020062#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)-134)
Gilles Peskinef3b731e2018-12-12 13:38:31 +010063
64/** The requested action is denied by a policy.
65 *
66 * Implementations should return this error code when the parameters
67 * are recognized as valid and supported, and a policy explicitly
68 * denies the requested operation.
69 *
70 * If a subset of the parameters of a function call identify a
71 * forbidden operation, and another subset of the parameters are
72 * not valid or not supported, it is unspecified whether the function
73 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
74 * #PSA_ERROR_INVALID_ARGUMENT. */
David Saadab4ecc272019-02-14 13:48:10 +020075#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)-133)
Gilles Peskinef3b731e2018-12-12 13:38:31 +010076
77/** An output buffer is too small.
78 *
79 * Applications can call the \c PSA_xxx_SIZE macro listed in the function
80 * description to determine a sufficient buffer size.
81 *
82 * Implementations should preferably return this error code only
83 * in cases when performing the operation with a larger output
84 * buffer would succeed. However implementations may return this
85 * error if a function has invalid or unsupported parameters in addition
86 * to the parameters that determine the necessary output buffer size. */
David Saadab4ecc272019-02-14 13:48:10 +020087#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)-138)
Gilles Peskinef3b731e2018-12-12 13:38:31 +010088
David Saadab4ecc272019-02-14 13:48:10 +020089/** Asking for an item that already exists
Gilles Peskinef3b731e2018-12-12 13:38:31 +010090 *
David Saadab4ecc272019-02-14 13:48:10 +020091 * Implementations should return this error, when attempting
92 * to write an item (like a key) that already exists. */
93#define PSA_ERROR_ALREADY_EXISTS ((psa_status_t)-139)
Gilles Peskinef3b731e2018-12-12 13:38:31 +010094
David Saadab4ecc272019-02-14 13:48:10 +020095/** Asking for an item that doesn't exist
Gilles Peskinef3b731e2018-12-12 13:38:31 +010096 *
David Saadab4ecc272019-02-14 13:48:10 +020097 * Implementations should return this error, if a requested item (like
98 * a key) does not exist. */
99#define PSA_ERROR_DOES_NOT_EXIST ((psa_status_t)-140)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100100
101/** The requested action cannot be performed in the current state.
102 *
103 * Multipart operations return this error when one of the
104 * functions is called out of sequence. Refer to the function
105 * descriptions for permitted sequencing of functions.
106 *
107 * Implementations shall not return this error code to indicate
108 * that a key slot is occupied when it needs to be free or vice versa,
David Saadab4ecc272019-02-14 13:48:10 +0200109 * but shall return #PSA_ERROR_ALREADY_EXISTS or #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100110 * as applicable. */
David Saadab4ecc272019-02-14 13:48:10 +0200111#define PSA_ERROR_BAD_STATE ((psa_status_t)-137)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100112
113/** The parameters passed to the function are invalid.
114 *
115 * Implementations may return this error any time a parameter or
116 * combination of parameters are recognized as invalid.
117 *
118 * Implementations shall not return this error code to indicate
119 * that a key slot is occupied when it needs to be free or vice versa,
David Saadab4ecc272019-02-14 13:48:10 +0200120 * but shall return #PSA_ERROR_ALREADY_EXISTS or #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100121 * as applicable.
122 *
123 * Implementation shall not return this error code to indicate that a
124 * key handle is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
125 * instead.
126 */
David Saadab4ecc272019-02-14 13:48:10 +0200127#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)-135)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100128
129/** There is not enough runtime memory.
130 *
131 * If the action is carried out across multiple security realms, this
132 * error can refer to available memory in any of the security realms. */
David Saadab4ecc272019-02-14 13:48:10 +0200133#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)-141)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100134
135/** There is not enough persistent storage.
136 *
137 * Functions that modify the key storage return this error code if
138 * there is insufficient storage space on the host media. In addition,
139 * many functions that do not otherwise access storage may return this
140 * error code if the implementation requires a mandatory log entry for
141 * the requested action and the log storage space is full. */
David Saadab4ecc272019-02-14 13:48:10 +0200142#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)-142)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100143
144/** There was a communication failure inside the implementation.
145 *
146 * This can indicate a communication failure between the application
147 * and an external cryptoprocessor or between the cryptoprocessor and
148 * an external volatile or persistent memory. A communication failure
149 * may be transient or permanent depending on the cause.
150 *
151 * \warning If a function returns this error, it is undetermined
152 * whether the requested action has completed or not. Implementations
153 * should return #PSA_SUCCESS on successful completion whenver
154 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
155 * if the requested action was completed successfully in an external
156 * cryptoprocessor but there was a breakdown of communication before
157 * the cryptoprocessor could report the status to the application.
158 */
David Saadab4ecc272019-02-14 13:48:10 +0200159#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)-145)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100160
161/** There was a storage failure that may have led to data loss.
162 *
163 * This error indicates that some persistent storage is corrupted.
164 * It should not be used for a corruption of volatile memory
165 * (use #PSA_ERROR_TAMPERING_DETECTED), for a communication error
166 * between the cryptoprocessor and its external storage (use
167 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
168 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
169 *
170 * Note that a storage failure does not indicate that any data that was
171 * previously read is invalid. However this previously read data may no
172 * longer be readable from storage.
173 *
174 * When a storage failure occurs, it is no longer possible to ensure
175 * the global integrity of the keystore. Depending on the global
176 * integrity guarantees offered by the implementation, access to other
177 * data may or may not fail even if the data is still readable but
178 * its integrity canont be guaranteed.
179 *
180 * Implementations should only use this error code to report a
181 * permanent storage corruption. However application writers should
182 * keep in mind that transient errors while reading the storage may be
183 * reported using this error code. */
David Saadab4ecc272019-02-14 13:48:10 +0200184#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)-146)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100185
186/** A hardware failure was detected.
187 *
188 * A hardware failure may be transient or permanent depending on the
189 * cause. */
David Saadab4ecc272019-02-14 13:48:10 +0200190#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)-147)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100191
192/** A tampering attempt was detected.
193 *
194 * If an application receives this error code, there is no guarantee
195 * that previously accessed or computed data was correct and remains
196 * confidential. Applications should not perform any security function
197 * and should enter a safe failure state.
198 *
199 * Implementations may return this error code if they detect an invalid
200 * state that cannot happen during normal operation and that indicates
201 * that the implementation's security guarantees no longer hold. Depending
202 * on the implementation architecture and on its security and safety goals,
203 * the implementation may forcibly terminate the application.
204 *
205 * This error code is intended as a last resort when a security breach
206 * is detected and it is unsure whether the keystore data is still
207 * protected. Implementations shall only return this error code
208 * to report an alarm from a tampering detector, to indicate that
209 * the confidentiality of stored data can no longer be guaranteed,
210 * or to indicate that the integrity of previously returned data is now
211 * considered compromised. Implementations shall not use this error code
212 * to indicate a hardware failure that merely makes it impossible to
213 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
214 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
215 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
216 * instead).
217 *
218 * This error indicates an attack against the application. Implementations
219 * shall not return this error code as a consequence of the behavior of
220 * the application itself. */
David Saadab4ecc272019-02-14 13:48:10 +0200221#define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)-151)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100222
223/** There is not enough entropy to generate random data needed
224 * for the requested action.
225 *
226 * This error indicates a failure of a hardware random generator.
227 * Application writers should note that this error can be returned not
228 * only by functions whose purpose is to generate random data, such
229 * as key, IV or nonce generation, but also by functions that execute
230 * an algorithm with a randomized result, as well as functions that
231 * use randomization of intermediate computations as a countermeasure
232 * to certain attacks.
233 *
234 * Implementations should avoid returning this error after psa_crypto_init()
235 * has succeeded. Implementations should generate sufficient
236 * entropy during initialization and subsequently use a cryptographically
237 * secure pseudorandom generator (PRNG). However implementations may return
238 * this error at any time if a policy requires the PRNG to be reseeded
239 * during normal operation. */
David Saadab4ecc272019-02-14 13:48:10 +0200240#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)-148)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100241
242/** The signature, MAC or hash is incorrect.
243 *
244 * Verification functions return this error if the verification
245 * calculations completed successfully, and the value to be verified
246 * was determined to be incorrect.
247 *
248 * If the value to verify has an invalid size, implementations may return
249 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
David Saadab4ecc272019-02-14 13:48:10 +0200250#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)-149)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100251
252/** The decrypted padding is incorrect.
253 *
254 * \warning In some protocols, when decrypting data, it is essential that
255 * the behavior of the application does not depend on whether the padding
256 * is correct, down to precise timing. Applications should prefer
257 * protocols that use authenticated encryption rather than plain
258 * encryption. If the application must perform a decryption of
259 * unauthenticated data, the application writer should take care not
260 * to reveal whether the padding is invalid.
261 *
262 * Implementations should strive to make valid and invalid padding
263 * as close as possible to indistinguishable to an external observer.
264 * In particular, the timing of a decryption operation should not
265 * depend on the validity of the padding. */
David Saadab4ecc272019-02-14 13:48:10 +0200266#define PSA_ERROR_INVALID_PADDING ((psa_status_t)-150)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100267
David Saadab4ecc272019-02-14 13:48:10 +0200268/** Return this error when there's insufficient data when attempting
269 * to read from a resource. */
270#define PSA_ERROR_INSUFFICIENT_DATA ((psa_status_t)-143)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100271
272/** The key handle is not valid.
273 */
David Saadab4ecc272019-02-14 13:48:10 +0200274#define PSA_ERROR_INVALID_HANDLE ((psa_status_t)-136)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100275
276/**@}*/
277
278/** \defgroup crypto_types Key and algorithm types
279 * @{
280 */
281
282/** An invalid key type value.
283 *
284 * Zero is not the encoding of any key type.
285 */
286#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
287
288/** Vendor-defined flag
289 *
290 * Key types defined by this standard will never have the
291 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
292 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
293 * respect the bitwise structure used by standard encodings whenever practical.
294 */
295#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
296
297#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x70000000)
298#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x40000000)
299#define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t)0x50000000)
300#define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t)0x60000000)
301#define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t)0x70000000)
302
303#define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t)0x10000000)
304
305/** Whether a key type is vendor-defined. */
306#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
307 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
308
309/** Whether a key type is an unstructured array of bytes.
310 *
311 * This encompasses both symmetric keys and non-key data.
312 */
313#define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \
314 (((type) & PSA_KEY_TYPE_CATEGORY_MASK & ~(psa_key_type_t)0x10000000) == \
315 PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
316
317/** Whether a key type is asymmetric: either a key pair or a public key. */
318#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
319 (((type) & PSA_KEY_TYPE_CATEGORY_MASK \
320 & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) == \
321 PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
322/** Whether a key type is the public part of a key pair. */
323#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
324 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
325/** Whether a key type is a key pair containing a private part and a public
326 * part. */
327#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
328 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR)
329/** The key pair type corresponding to a public key type.
330 *
331 * You may also pass a key pair type as \p type, it will be left unchanged.
332 *
333 * \param type A public key type or key pair type.
334 *
335 * \return The corresponding key pair type.
336 * If \p type is not a public key or a key pair,
337 * the return value is undefined.
338 */
339#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
340 ((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
341/** The public key type corresponding to a key pair type.
342 *
343 * You may also pass a key pair type as \p type, it will be left unchanged.
344 *
345 * \param type A public key type or key pair type.
346 *
347 * \return The corresponding public key type.
348 * If \p type is not a public key or a key pair,
349 * the return value is undefined.
350 */
351#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
352 ((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
353
354/** Raw data.
355 *
356 * A "key" of this type cannot be used for any cryptographic operation.
357 * Applications may use this type to store arbitrary data in the keystore. */
358#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x50000001)
359
360/** HMAC key.
361 *
362 * The key policy determines which underlying hash algorithm the key can be
363 * used for.
364 *
365 * HMAC keys should generally have the same size as the underlying hash.
366 * This size can be calculated with #PSA_HASH_SIZE(\c alg) where
367 * \c alg is the HMAC algorithm or the underlying hash algorithm. */
368#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x51000000)
369
370/** A secret for key derivation.
371 *
372 * The key policy determines which key derivation algorithm the key
373 * can be used for.
374 */
375#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x52000000)
376
377/** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher.
378 *
379 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
380 * 32 bytes (AES-256).
381 */
382#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x40000001)
383
384/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
385 *
386 * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or
387 * 24 bytes (3-key 3DES).
388 *
389 * Note that single DES and 2-key 3DES are weak and strongly
390 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
391 * is weak and deprecated and should only be used in legacy protocols.
392 */
393#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x40000002)
394
395/** Key for an cipher, AEAD or MAC algorithm based on the
396 * Camellia block cipher. */
397#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x40000003)
398
399/** Key for the RC4 stream cipher.
400 *
401 * Note that RC4 is weak and deprecated and should only be used in
402 * legacy protocols. */
403#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x40000004)
404
405/** RSA public key. */
406#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x60010000)
407/** RSA key pair (private and public key). */
408#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x70010000)
409/** Whether a key type is an RSA key (pair or public-only). */
410#define PSA_KEY_TYPE_IS_RSA(type) \
411 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
412
413/** DSA public key. */
414#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x60020000)
415/** DSA key pair (private and public key). */
416#define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x70020000)
417/** Whether a key type is an DSA key (pair or public-only). */
418#define PSA_KEY_TYPE_IS_DSA(type) \
419 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
420
421#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x60030000)
422#define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x70030000)
423#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
424/** Elliptic curve key pair. */
425#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
426 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
427/** Elliptic curve public key. */
428#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
429 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
430
431/** Whether a key type is an elliptic curve key (pair or public-only). */
432#define PSA_KEY_TYPE_IS_ECC(type) \
433 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
434 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine5e9c9cc2018-12-12 14:02:48 +0100435/** Whether a key type is an elliptic curve key pair. */
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100436#define PSA_KEY_TYPE_IS_ECC_KEYPAIR(type) \
437 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
438 PSA_KEY_TYPE_ECC_KEYPAIR_BASE)
Gilles Peskine5e9c9cc2018-12-12 14:02:48 +0100439/** Whether a key type is an elliptic curve public key. */
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100440#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
441 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
442 PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
443
444/** Extract the curve from an elliptic curve key type. */
445#define PSA_KEY_TYPE_GET_CURVE(type) \
446 ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
447 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
448 0))
449
450/* The encoding of curve identifiers is currently aligned with the
451 * TLS Supported Groups Registry (formerly known as the
452 * TLS EC Named Curve Registry)
453 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
454 * The values are defined by RFC 8422 and RFC 7027. */
455#define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001)
456#define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002)
457#define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003)
458#define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004)
459#define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005)
460#define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006)
461#define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007)
462#define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008)
463#define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009)
464#define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a)
465#define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b)
466#define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c)
467#define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d)
468#define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e)
469#define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f)
470#define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010)
471#define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011)
472#define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012)
473#define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013)
474#define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014)
475#define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015)
476#define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016)
477#define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017)
478#define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018)
479#define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019)
480#define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a)
481#define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b)
482#define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c)
483#define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d)
484#define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e)
485
486/** The block size of a block cipher.
487 *
488 * \param type A cipher key type (value of type #psa_key_type_t).
489 *
490 * \return The block size for a block cipher, or 1 for a stream cipher.
491 * The return value is undefined if \p type is not a supported
492 * cipher key type.
493 *
494 * \note It is possible to build stream cipher algorithms on top of a block
495 * cipher, for example CTR mode (#PSA_ALG_CTR).
496 * This macro only takes the key type into account, so it cannot be
497 * used to determine the size of the data that #psa_cipher_update()
498 * might buffer for future processing in general.
499 *
500 * \note This macro returns a compile-time constant if its argument is one.
501 *
502 * \warning This macro may evaluate its argument multiple times.
503 */
504#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
505 ( \
506 (type) == PSA_KEY_TYPE_AES ? 16 : \
507 (type) == PSA_KEY_TYPE_DES ? 8 : \
508 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
509 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
510 0)
511
512#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
513#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
514#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
515#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
516#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
517#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
518#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
519#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
520#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
521#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
522#define PSA_ALG_CATEGORY_KEY_SELECTION ((psa_algorithm_t)0x31000000)
523
524#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
525 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
526
527/** Whether the specified algorithm is a hash algorithm.
528 *
529 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
530 *
531 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
532 * This macro may return either 0 or 1 if \p alg is not a supported
533 * algorithm identifier.
534 */
535#define PSA_ALG_IS_HASH(alg) \
536 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
537
538/** Whether the specified algorithm is a MAC algorithm.
539 *
540 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
541 *
542 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
543 * This macro may return either 0 or 1 if \p alg is not a supported
544 * algorithm identifier.
545 */
546#define PSA_ALG_IS_MAC(alg) \
547 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
548
549/** Whether the specified algorithm is a symmetric cipher algorithm.
550 *
551 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
552 *
553 * \return 1 if \p alg is a symmetric cipher 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_CIPHER(alg) \
558 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
559
560/** Whether the specified algorithm is an authenticated encryption
561 * with associated data (AEAD) algorithm.
562 *
563 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
564 *
565 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
566 * This macro may return either 0 or 1 if \p alg is not a supported
567 * algorithm identifier.
568 */
569#define PSA_ALG_IS_AEAD(alg) \
570 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
571
572/** Whether the specified algorithm is a public-key signature algorithm.
573 *
574 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
575 *
576 * \return 1 if \p alg is a public-key signature algorithm, 0 otherwise.
577 * This macro may return either 0 or 1 if \p alg is not a supported
578 * algorithm identifier.
579 */
580#define PSA_ALG_IS_SIGN(alg) \
581 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
582
583/** Whether the specified algorithm is a public-key encryption algorithm.
584 *
585 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
586 *
587 * \return 1 if \p alg is a public-key encryption 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_ASYMMETRIC_ENCRYPTION(alg) \
592 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
593
594#define PSA_ALG_KEY_SELECTION_FLAG ((psa_algorithm_t)0x01000000)
595/** Whether the specified algorithm is a key agreement algorithm.
596 *
597 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
598 *
599 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
600 * This macro may return either 0 or 1 if \p alg is not a supported
601 * algorithm identifier.
602 */
603#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
604 (((alg) & PSA_ALG_CATEGORY_MASK & ~PSA_ALG_KEY_SELECTION_FLAG) == \
605 PSA_ALG_CATEGORY_KEY_AGREEMENT)
606
607/** Whether the specified algorithm is a key derivation algorithm.
608 *
609 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
610 *
611 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
612 * This macro may return either 0 or 1 if \p alg is not a supported
613 * algorithm identifier.
614 */
615#define PSA_ALG_IS_KEY_DERIVATION(alg) \
616 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
617
618/** Whether the specified algorithm is a key selection algorithm.
619 *
620 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
621 *
622 * \return 1 if \p alg is a key selection algorithm, 0 otherwise.
623 * This macro may return either 0 or 1 if \p alg is not a supported
624 * algorithm identifier.
625 */
626#define PSA_ALG_IS_KEY_SELECTION(alg) \
627 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_SELECTION)
628
629#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100630
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100631#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
632#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
633#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
634#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
635#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
636/** SHA2-224 */
637#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
638/** SHA2-256 */
639#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
640/** SHA2-384 */
641#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
642/** SHA2-512 */
643#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
644/** SHA2-512/224 */
645#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
646/** SHA2-512/256 */
647#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
648/** SHA3-224 */
649#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
650/** SHA3-256 */
651#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
652/** SHA3-384 */
653#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
654/** SHA3-512 */
655#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
656
Gilles Peskine763fb9a2019-01-28 13:29:01 +0100657/** In a hash-and-sign algorithm policy, allow any hash algorithm.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100658 *
Gilles Peskine763fb9a2019-01-28 13:29:01 +0100659 * This value may be used to form the algorithm usage field of a policy
660 * for a signature algorithm that is parametrized by a hash. The key
661 * may then be used to perform operations using the same signature
662 * algorithm parametrized with any supported hash.
663 *
664 * That is, suppose that `PSA_xxx_SIGNATURE` is one of the following macros:
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100665 * - #PSA_ALG_RSA_PKCS1V15_SIGN, #PSA_ALG_RSA_PSS,
666 * - #PSA_ALG_DSA, #PSA_ALG_DETERMINISTIC_DSA,
667 * - #PSA_ALG_ECDSA, #PSA_ALG_DETERMINISTIC_ECDSA.
Gilles Peskine763fb9a2019-01-28 13:29:01 +0100668 * Then you may create and use a key as follows:
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100669 * - Set the key usage field using #PSA_ALG_ANY_HASH, for example:
670 * ```
671 * psa_key_policy_set_usage(&policy,
672 * PSA_KEY_USAGE_SIGN, //or PSA_KEY_USAGE_VERIFY
673 * PSA_xxx_SIGNATURE(PSA_ALG_ANY_HASH));
674 * psa_set_key_policy(handle, &policy);
675 * ```
676 * - Import or generate key material.
677 * - Call psa_asymmetric_sign() or psa_asymmetric_verify(), passing
678 * an algorithm built from `PSA_xxx_SIGNATURE` and a specific hash. Each
679 * call to sign or verify a message may use a different hash.
680 * ```
681 * psa_asymmetric_sign(handle, PSA_xxx_SIGNATURE(PSA_ALG_SHA_256), ...);
682 * psa_asymmetric_sign(handle, PSA_xxx_SIGNATURE(PSA_ALG_SHA_512), ...);
683 * psa_asymmetric_sign(handle, PSA_xxx_SIGNATURE(PSA_ALG_SHA3_256), ...);
684 * ```
685 *
686 * This value may not be used to build other algorithms that are
687 * parametrized over a hash. For any valid use of this macro to build
688 * an algorithm `\p alg`, #PSA_ALG_IS_HASH_AND_SIGN(\p alg) is true.
689 *
690 * This value may not be used to build an algorithm specification to
691 * perform an operation. It is only valid to build policies.
692 */
693#define PSA_ALG_ANY_HASH ((psa_algorithm_t)0x010000ff)
694
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100695#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
696#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
697/** Macro to build an HMAC algorithm.
698 *
699 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
700 *
701 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
702 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
703 *
704 * \return The corresponding HMAC algorithm.
705 * \return Unspecified if \p alg is not a supported
706 * hash algorithm.
707 */
708#define PSA_ALG_HMAC(hash_alg) \
709 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
710
711#define PSA_ALG_HMAC_GET_HASH(hmac_alg) \
712 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
713
714/** Whether the specified algorithm is an HMAC algorithm.
715 *
716 * HMAC is a family of MAC algorithms that are based on a hash function.
717 *
718 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
719 *
720 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
721 * This macro may return either 0 or 1 if \p alg is not a supported
722 * algorithm identifier.
723 */
724#define PSA_ALG_IS_HMAC(alg) \
725 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
726 PSA_ALG_HMAC_BASE)
727
728/* In the encoding of a MAC algorithm, the bits corresponding to
729 * PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is
730 * truncated. As an exception, the value 0 means the untruncated algorithm,
731 * whatever its length is. The length is encoded in 6 bits, so it can
732 * reach up to 63; the largest MAC is 64 bytes so its trivial truncation
733 * to full length is correctly encoded as 0 and any non-trivial truncation
734 * is correctly encoded as a value between 1 and 63. */
735#define PSA_ALG_MAC_TRUNCATION_MASK ((psa_algorithm_t)0x00003f00)
736#define PSA_MAC_TRUNCATION_OFFSET 8
737
738/** Macro to build a truncated MAC algorithm.
739 *
740 * A truncated MAC algorithm is identical to the corresponding MAC
741 * algorithm except that the MAC value for the truncated algorithm
742 * consists of only the first \p mac_length bytes of the MAC value
743 * for the untruncated algorithm.
744 *
745 * \note This macro may allow constructing algorithm identifiers that
746 * are not valid, either because the specified length is larger
747 * than the untruncated MAC or because the specified length is
748 * smaller than permitted by the implementation.
749 *
750 * \note It is implementation-defined whether a truncated MAC that
751 * is truncated to the same length as the MAC of the untruncated
752 * algorithm is considered identical to the untruncated algorithm
753 * for policy comparison purposes.
754 *
Gilles Peskine434899f2018-10-19 11:30:26 +0200755 * \param mac_alg A MAC algorithm identifier (value of type
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100756 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
757 * is true). This may be a truncated or untruncated
758 * MAC algorithm.
759 * \param mac_length Desired length of the truncated MAC in bytes.
760 * This must be at most the full length of the MAC
761 * and must be at least an implementation-specified
762 * minimum. The implementation-specified minimum
763 * shall not be zero.
764 *
765 * \return The corresponding MAC algorithm with the specified
766 * length.
767 * \return Unspecified if \p alg is not a supported
768 * MAC algorithm or if \p mac_length is too small or
769 * too large for the specified MAC algorithm.
770 */
Gilles Peskine434899f2018-10-19 11:30:26 +0200771#define PSA_ALG_TRUNCATED_MAC(mac_alg, mac_length) \
772 (((mac_alg) & ~PSA_ALG_MAC_TRUNCATION_MASK) | \
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100773 ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))
774
775/** Macro to build the base MAC algorithm corresponding to a truncated
776 * MAC algorithm.
777 *
Gilles Peskine434899f2018-10-19 11:30:26 +0200778 * \param mac_alg A MAC algorithm identifier (value of type
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100779 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
780 * is true). This may be a truncated or untruncated
781 * MAC algorithm.
782 *
783 * \return The corresponding base MAC algorithm.
784 * \return Unspecified if \p alg is not a supported
785 * MAC algorithm.
786 */
Gilles Peskine434899f2018-10-19 11:30:26 +0200787#define PSA_ALG_FULL_LENGTH_MAC(mac_alg) \
788 ((mac_alg) & ~PSA_ALG_MAC_TRUNCATION_MASK)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100789
790/** Length to which a MAC algorithm is truncated.
791 *
Gilles Peskine434899f2018-10-19 11:30:26 +0200792 * \param mac_alg A MAC algorithm identifier (value of type
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100793 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
794 * is true).
795 *
796 * \return Length of the truncated MAC in bytes.
797 * \return 0 if \p alg is a non-truncated MAC algorithm.
798 * \return Unspecified if \p alg is not a supported
799 * MAC algorithm.
800 */
Gilles Peskine434899f2018-10-19 11:30:26 +0200801#define PSA_MAC_TRUNCATED_LENGTH(mac_alg) \
802 (((mac_alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100803
804#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
805#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
806#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
807#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
808
809/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
810 *
811 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
812 *
813 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
814 * This macro may return either 0 or 1 if \p alg is not a supported
815 * algorithm identifier.
816 */
817#define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) \
818 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
819 PSA_ALG_CIPHER_MAC_BASE)
820
821#define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t)0x00800000)
822#define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
823
824/** Whether the specified algorithm is a stream cipher.
825 *
826 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
827 * by applying a bitwise-xor with a stream of bytes that is generated
828 * from a key.
829 *
830 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
831 *
832 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
833 * This macro may return either 0 or 1 if \p alg is not a supported
834 * algorithm identifier or if it is not a symmetric cipher algorithm.
835 */
836#define PSA_ALG_IS_STREAM_CIPHER(alg) \
837 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
838 (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
839
840/** The ARC4 stream cipher algorithm.
841 */
842#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800001)
843
844/** The CTR stream cipher mode.
845 *
846 * CTR is a stream cipher which is built from a block cipher.
847 * The underlying block cipher is determined by the key type.
848 * For example, to use AES-128-CTR, use this algorithm with
849 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
850 */
851#define PSA_ALG_CTR ((psa_algorithm_t)0x04c00001)
852
853#define PSA_ALG_CFB ((psa_algorithm_t)0x04c00002)
854
855#define PSA_ALG_OFB ((psa_algorithm_t)0x04c00003)
856
857/** The XTS cipher mode.
858 *
859 * XTS is a cipher mode which is built from a block cipher. It requires at
860 * least one full block of input, but beyond this minimum the input
861 * does not need to be a whole number of blocks.
862 */
863#define PSA_ALG_XTS ((psa_algorithm_t)0x044000ff)
864
865/** The CBC block cipher chaining mode, with no padding.
866 *
867 * The underlying block cipher is determined by the key type.
868 *
869 * This symmetric cipher mode can only be used with messages whose lengths
870 * are whole number of blocks for the chosen block cipher.
871 */
872#define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t)0x04600100)
873
874/** The CBC block cipher chaining mode with PKCS#7 padding.
875 *
876 * The underlying block cipher is determined by the key type.
877 *
878 * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
879 */
880#define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t)0x04600101)
881
882#define PSA_ALG_CCM ((psa_algorithm_t)0x06001001)
883#define PSA_ALG_GCM ((psa_algorithm_t)0x06001002)
884
885/* In the encoding of a AEAD algorithm, the bits corresponding to
886 * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.
887 * The constants for default lengths follow this encoding.
888 */
889#define PSA_ALG_AEAD_TAG_LENGTH_MASK ((psa_algorithm_t)0x00003f00)
890#define PSA_AEAD_TAG_LENGTH_OFFSET 8
891
892/** Macro to build a shortened AEAD algorithm.
893 *
894 * A shortened AEAD algorithm is similar to the corresponding AEAD
895 * algorithm, but has an authentication tag that consists of fewer bytes.
896 * Depending on the algorithm, the tag length may affect the calculation
897 * of the ciphertext.
898 *
Gilles Peskine434899f2018-10-19 11:30:26 +0200899 * \param aead_alg An AEAD algorithm identifier (value of type
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100900 * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p alg)
901 * is true).
902 * \param tag_length Desired length of the authentication tag in bytes.
903 *
904 * \return The corresponding AEAD algorithm with the specified
905 * length.
906 * \return Unspecified if \p alg is not a supported
907 * AEAD algorithm or if \p tag_length is not valid
908 * for the specified AEAD algorithm.
909 */
Gilles Peskine434899f2018-10-19 11:30:26 +0200910#define PSA_ALG_AEAD_WITH_TAG_LENGTH(aead_alg, tag_length) \
911 (((aead_alg) & ~PSA_ALG_AEAD_TAG_LENGTH_MASK) | \
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100912 ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \
913 PSA_ALG_AEAD_TAG_LENGTH_MASK))
914
915/** Calculate the corresponding AEAD algorithm with the default tag length.
916 *
Gilles Peskine434899f2018-10-19 11:30:26 +0200917 * \param aead_alg An AEAD algorithm (\c PSA_ALG_XXX value such that
918 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100919 *
Gilles Peskine434899f2018-10-19 11:30:26 +0200920 * \return The corresponding AEAD algorithm with the default
921 * tag length for that algorithm.
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100922 */
Gilles Peskine434899f2018-10-19 11:30:26 +0200923#define PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(aead_alg) \
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100924 ( \
Gilles Peskine434899f2018-10-19 11:30:26 +0200925 PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(aead_alg, PSA_ALG_CCM) \
926 PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(aead_alg, PSA_ALG_GCM) \
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100927 0)
Gilles Peskine434899f2018-10-19 11:30:26 +0200928#define PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(aead_alg, ref) \
929 PSA_ALG_AEAD_WITH_TAG_LENGTH(aead_alg, 0) == \
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100930 PSA_ALG_AEAD_WITH_TAG_LENGTH(ref, 0) ? \
931 ref :
932
933#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
934/** RSA PKCS#1 v1.5 signature with hashing.
935 *
936 * This is the signature scheme defined by RFC 8017
937 * (PKCS#1: RSA Cryptography Specifications) under the name
938 * RSASSA-PKCS1-v1_5.
939 *
940 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
941 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100942 * This includes #PSA_ALG_ANY_HASH
943 * when specifying the algorithm in a usage policy.
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100944 *
945 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
946 * \return Unspecified if \p alg is not a supported
947 * hash algorithm.
948 */
949#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
950 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
951/** Raw PKCS#1 v1.5 signature.
952 *
953 * The input to this algorithm is the DigestInfo structure used by
954 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
955 * steps 3&ndash;6.
956 */
957#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
958#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
959 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
960
961#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
962/** RSA PSS signature with hashing.
963 *
964 * This is the signature scheme defined by RFC 8017
965 * (PKCS#1: RSA Cryptography Specifications) under the name
966 * RSASSA-PSS, with the message generation function MGF1, and with
967 * a salt length equal to the length of the hash. The specified
968 * hash algorithm is used to hash the input message, to create the
969 * salted hash, and for the mask generation.
970 *
971 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
972 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100973 * This includes #PSA_ALG_ANY_HASH
974 * when specifying the algorithm in a usage policy.
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100975 *
976 * \return The corresponding RSA PSS signature algorithm.
977 * \return Unspecified if \p alg is not a supported
978 * hash algorithm.
979 */
980#define PSA_ALG_RSA_PSS(hash_alg) \
981 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
982#define PSA_ALG_IS_RSA_PSS(alg) \
983 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
984
985#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
986/** DSA signature with hashing.
987 *
988 * This is the signature scheme defined by FIPS 186-4,
989 * with a random per-message secret number (*k*).
990 *
991 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
992 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100993 * This includes #PSA_ALG_ANY_HASH
994 * when specifying the algorithm in a usage policy.
Gilles Peskinef3b731e2018-12-12 13:38:31 +0100995 *
996 * \return The corresponding DSA signature algorithm.
997 * \return Unspecified if \p alg is not a supported
998 * hash algorithm.
999 */
1000#define PSA_ALG_DSA(hash_alg) \
1001 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1002#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
1003#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
1004#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
1005 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1006#define PSA_ALG_IS_DSA(alg) \
1007 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
1008 PSA_ALG_DSA_BASE)
1009#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
1010 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
1011#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
1012 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
1013#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
1014 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
1015
1016#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
1017/** ECDSA signature with hashing.
1018 *
1019 * This is the ECDSA signature scheme defined by ANSI X9.62,
1020 * with a random per-message secret number (*k*).
1021 *
1022 * The representation of the signature as a byte string consists of
1023 * the concatentation of the signature values *r* and *s*. Each of
1024 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
1025 * of the base point of the curve in octets. Each value is represented
1026 * in big-endian order (most significant octet first).
1027 *
1028 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1029 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001030 * This includes #PSA_ALG_ANY_HASH
1031 * when specifying the algorithm in a usage policy.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001032 *
1033 * \return The corresponding ECDSA signature algorithm.
1034 * \return Unspecified if \p alg is not a supported
1035 * hash algorithm.
1036 */
1037#define PSA_ALG_ECDSA(hash_alg) \
1038 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1039/** ECDSA signature without hashing.
1040 *
1041 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
1042 * without specifying a hash algorithm. This algorithm may only be
1043 * used to sign or verify a sequence of bytes that should be an
1044 * already-calculated hash. Note that the input is padded with
1045 * zeros on the left or truncated on the left as required to fit
1046 * the curve size.
1047 */
1048#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
1049#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
1050/** Deterministic ECDSA signature with hashing.
1051 *
1052 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
1053 *
1054 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
1055 *
1056 * Note that when this algorithm is used for verification, signatures
1057 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
1058 * same private key are accepted. In other words,
1059 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
1060 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
1061 *
1062 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1063 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001064 * This includes #PSA_ALG_ANY_HASH
1065 * when specifying the algorithm in a usage policy.
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001066 *
1067 * \return The corresponding deterministic ECDSA signature
1068 * algorithm.
1069 * \return Unspecified if \p alg is not a supported
1070 * hash algorithm.
1071 */
1072#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
1073 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1074#define PSA_ALG_IS_ECDSA(alg) \
1075 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
1076 PSA_ALG_ECDSA_BASE)
1077#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
1078 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
1079#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
1080 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1081#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
1082 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1083
Gilles Peskined35b4892019-01-14 16:02:15 +01001084/** Whether the specified algorithm is a hash-and-sign algorithm.
1085 *
1086 * Hash-and-sign algorithms are public-key signature algorithms structured
1087 * in two parts: first the calculation of a hash in a way that does not
1088 * depend on the key, then the calculation of a signature from the
1089 * hash value and the key.
1090 *
1091 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1092 *
1093 * \return 1 if \p alg is a hash-and-sign algorithm, 0 otherwise.
1094 * This macro may return either 0 or 1 if \p alg is not a supported
1095 * algorithm identifier.
1096 */
1097#define PSA_ALG_IS_HASH_AND_SIGN(alg) \
1098 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
1099 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg))
1100
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001101/** Get the hash used by a hash-and-sign signature algorithm.
1102 *
1103 * A hash-and-sign algorithm is a signature algorithm which is
1104 * composed of two phases: first a hashing phase which does not use
1105 * the key and produces a hash of the input message, then a signing
1106 * phase which only uses the hash and the key and not the message
1107 * itself.
1108 *
1109 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
1110 * #PSA_ALG_IS_SIGN(\p alg) is true).
1111 *
1112 * \return The underlying hash algorithm if \p alg is a hash-and-sign
1113 * algorithm.
1114 * \return 0 if \p alg is a signature algorithm that does not
1115 * follow the hash-and-sign structure.
1116 * \return Unspecified if \p alg is not a signature algorithm or
1117 * if it is not supported by the implementation.
1118 */
1119#define PSA_ALG_SIGN_GET_HASH(alg) \
Gilles Peskined35b4892019-01-14 16:02:15 +01001120 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001121 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
1122 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1123 0)
1124
1125/** RSA PKCS#1 v1.5 encryption.
1126 */
1127#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
1128
1129#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
1130/** RSA OAEP encryption.
1131 *
1132 * This is the encryption scheme defined by RFC 8017
1133 * (PKCS#1: RSA Cryptography Specifications) under the name
1134 * RSAES-OAEP, with the message generation function MGF1.
1135 *
1136 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1137 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1138 * for MGF1.
1139 *
1140 * \return The corresponding RSA OAEP signature algorithm.
1141 * \return Unspecified if \p alg is not a supported
1142 * hash algorithm.
1143 */
1144#define PSA_ALG_RSA_OAEP(hash_alg) \
1145 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1146#define PSA_ALG_IS_RSA_OAEP(alg) \
1147 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
1148#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1149 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1150 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1151 0)
1152
1153#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x30000100)
1154/** Macro to build an HKDF algorithm.
1155 *
1156 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1157 *
1158 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1159 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1160 *
1161 * \return The corresponding HKDF algorithm.
1162 * \return Unspecified if \p alg is not a supported
1163 * hash algorithm.
1164 */
1165#define PSA_ALG_HKDF(hash_alg) \
1166 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1167/** Whether the specified algorithm is an HKDF algorithm.
1168 *
1169 * HKDF is a family of key derivation algorithms that are based on a hash
1170 * function and the HMAC construction.
1171 *
1172 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1173 *
1174 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1175 * This macro may return either 0 or 1 if \c alg is not a supported
1176 * key derivation algorithm identifier.
1177 */
1178#define PSA_ALG_IS_HKDF(alg) \
1179 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1180#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1181 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1182
1183#define PSA_ALG_TLS12_PRF_BASE ((psa_algorithm_t)0x30000200)
1184/** Macro to build a TLS-1.2 PRF algorithm.
1185 *
1186 * TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule,
1187 * specified in Section 5 of RFC 5246. It is based on HMAC and can be
1188 * used with either SHA-256 or SHA-384.
1189 *
1190 * For the application to TLS-1.2, the salt and label arguments passed
1191 * to psa_key_derivation() are what's called 'seed' and 'label' in RFC 5246,
1192 * respectively. For example, for TLS key expansion, the salt is the
1193 * concatenation of ServerHello.Random + ClientHello.Random,
1194 * while the label is "key expansion".
1195 *
1196 * For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA256)` represents the
1197 * TLS 1.2 PRF using HMAC-SHA-256.
1198 *
1199 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1200 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1201 *
1202 * \return The corresponding TLS-1.2 PRF algorithm.
1203 * \return Unspecified if \p alg is not a supported
1204 * hash algorithm.
1205 */
1206#define PSA_ALG_TLS12_PRF(hash_alg) \
1207 (PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1208
1209/** Whether the specified algorithm is a TLS-1.2 PRF algorithm.
1210 *
1211 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1212 *
1213 * \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise.
1214 * This macro may return either 0 or 1 if \c alg is not a supported
1215 * key derivation algorithm identifier.
1216 */
1217#define PSA_ALG_IS_TLS12_PRF(alg) \
1218 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE)
1219#define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg) \
1220 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1221
1222#define PSA_ALG_TLS12_PSK_TO_MS_BASE ((psa_algorithm_t)0x30000300)
1223/** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm.
1224 *
1225 * In a pure-PSK handshake in TLS 1.2, the master secret is derived
1226 * from the PreSharedKey (PSK) through the application of padding
1227 * (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5).
1228 * The latter is based on HMAC and can be used with either SHA-256
1229 * or SHA-384.
1230 *
1231 * For the application to TLS-1.2, the salt passed to psa_key_derivation()
1232 * (and forwarded to the TLS-1.2 PRF) is the concatenation of the
1233 * ClientHello.Random + ServerHello.Random, while the label is "master secret"
1234 * or "extended master secret".
1235 *
1236 * For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA256)` represents the
1237 * TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256.
1238 *
1239 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1240 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1241 *
1242 * \return The corresponding TLS-1.2 PSK to MS algorithm.
1243 * \return Unspecified if \p alg is not a supported
1244 * hash algorithm.
1245 */
1246#define PSA_ALG_TLS12_PSK_TO_MS(hash_alg) \
1247 (PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1248
1249/** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm.
1250 *
1251 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1252 *
1253 * \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise.
1254 * This macro may return either 0 or 1 if \c alg is not a supported
1255 * key derivation algorithm identifier.
1256 */
1257#define PSA_ALG_IS_TLS12_PSK_TO_MS(alg) \
1258 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE)
1259#define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg) \
1260 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1261
1262#define PSA_ALG_KEY_DERIVATION_MASK ((psa_algorithm_t)0x010fffff)
1263
1264/** Use a shared secret as is.
1265 *
1266 * Specify this algorithm as the selection component of a key agreement
1267 * to use the raw result of the key agreement as key material.
1268 *
1269 * \warning The raw result of a key agreement algorithm such as finite-field
1270 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
1271 * not be used directly as key material. It can however be used as the secret
1272 * input in a key derivation algorithm.
1273 */
1274#define PSA_ALG_SELECT_RAW ((psa_algorithm_t)0x31000001)
1275
1276#define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) \
1277 (((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION)
1278
1279#define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) \
1280 ((alg) & ~PSA_ALG_KEY_DERIVATION_MASK)
1281
1282#define PSA_ALG_FFDH_BASE ((psa_algorithm_t)0x22100000)
1283/** The Diffie-Hellman key agreement algorithm.
1284 *
1285 * This algorithm combines the finite-field Diffie-Hellman (DH) key
1286 * agreement, also known as Diffie-Hellman-Merkle (DHM) key agreement,
1287 * to produce a shared secret from a private key and the peer's
1288 * public key, with a key selection or key derivation algorithm to produce
1289 * one or more shared keys and other shared cryptographic material.
1290 *
1291 * The shared secret produced by key agreement and passed as input to the
1292 * derivation or selection algorithm \p kdf_alg is the shared secret
1293 * `g^{ab}` in big-endian format.
1294 * It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p`
1295 * in bits.
1296 *
1297 * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
1298 * that #PSA_ALG_IS_KEY_DERIVATION(\p hash_alg) is true)
1299 * or a key selection algorithm (\c PSA_ALG_XXX value such
1300 * that #PSA_ALG_IS_KEY_SELECTION(\p hash_alg) is true).
1301 *
1302 * \return The Diffie-Hellman algorithm with the specified
1303 * selection or derivation algorithm.
1304 */
1305#define PSA_ALG_FFDH(kdf_alg) \
1306 (PSA_ALG_FFDH_BASE | ((kdf_alg) & PSA_ALG_KEY_DERIVATION_MASK))
1307/** Whether the specified algorithm is a finite field Diffie-Hellman algorithm.
1308 *
1309 * This includes every supported key selection or key agreement algorithm
1310 * for the output of the Diffie-Hellman calculation.
1311 *
1312 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1313 *
1314 * \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise.
1315 * This macro may return either 0 or 1 if \c alg is not a supported
1316 * key agreement algorithm identifier.
1317 */
1318#define PSA_ALG_IS_FFDH(alg) \
1319 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH_BASE)
1320
1321#define PSA_ALG_ECDH_BASE ((psa_algorithm_t)0x22200000)
1322/** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm.
1323 *
1324 * This algorithm combines the elliptic curve Diffie-Hellman key
1325 * agreement to produce a shared secret from a private key and the peer's
1326 * public key, with a key selection or key derivation algorithm to produce
1327 * one or more shared keys and other shared cryptographic material.
1328 *
1329 * The shared secret produced by key agreement and passed as input to the
1330 * derivation or selection algorithm \p kdf_alg is the x-coordinate of
1331 * the shared secret point. It is always `ceiling(m / 8)` bytes long where
1332 * `m` is the bit size associated with the curve, i.e. the bit size of the
1333 * order of the curve's coordinate field. When `m` is not a multiple of 8,
1334 * the byte containing the most significant bit of the shared secret
1335 * is padded with zero bits. The byte order is either little-endian
1336 * or big-endian depending on the curve type.
1337 *
1338 * - For Montgomery curves (curve types `PSA_ECC_CURVE_CURVEXXX`),
1339 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1340 * in little-endian byte order.
1341 * The bit size is 448 for Curve448 and 255 for Curve25519.
1342 * - For Weierstrass curves over prime fields (curve types
1343 * `PSA_ECC_CURVE_SECPXXX` and `PSA_ECC_CURVE_BRAINPOOL_PXXX`),
1344 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1345 * in big-endian byte order.
1346 * The bit size is `m = ceiling(log_2(p))` for the field `F_p`.
1347 * - For Weierstrass curves over binary fields (curve types
1348 * `PSA_ECC_CURVE_SECTXXX`),
1349 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1350 * in big-endian byte order.
1351 * The bit size is `m` for the field `F_{2^m}`.
1352 *
1353 * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
1354 * that #PSA_ALG_IS_KEY_DERIVATION(\p hash_alg) is true)
1355 * or a selection algorithm (\c PSA_ALG_XXX value such
1356 * that #PSA_ALG_IS_KEY_SELECTION(\p hash_alg) is true).
1357 *
1358 * \return The Diffie-Hellman algorithm with the specified
1359 * selection or derivation algorithm.
1360 */
1361#define PSA_ALG_ECDH(kdf_alg) \
1362 (PSA_ALG_ECDH_BASE | ((kdf_alg) & PSA_ALG_KEY_DERIVATION_MASK))
1363/** Whether the specified algorithm is an elliptic curve Diffie-Hellman
1364 * algorithm.
1365 *
1366 * This includes every supported key selection or key agreement algorithm
1367 * for the output of the Diffie-Hellman calculation.
1368 *
1369 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1370 *
1371 * \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm,
1372 * 0 otherwise.
1373 * This macro may return either 0 or 1 if \c alg is not a supported
1374 * key agreement algorithm identifier.
1375 */
1376#define PSA_ALG_IS_ECDH(alg) \
1377 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH_BASE)
1378
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001379/** Whether the specified algorithm encoding is a wildcard.
1380 *
1381 * Wildcard values may only be used to set the usage algorithm field in
1382 * a policy, not to perform an operation.
1383 *
1384 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1385 *
1386 * \return 1 if \c alg is a wildcard algorithm encoding.
1387 * \return 0 if \c alg is a non-wildcard algorithm encoding (suitable for
1388 * an operation).
1389 * \return This macro may return either 0 or 1 if \c alg is not a supported
1390 * algorithm identifier.
1391 */
1392#define PSA_ALG_IS_WILDCARD(alg) \
1393 (PSA_ALG_IS_HASH_AND_SIGN(alg) ? \
1394 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH : \
1395 (alg) == PSA_ALG_ANY_HASH)
1396
Gilles Peskinef3b731e2018-12-12 13:38:31 +01001397/**@}*/
1398
1399/** \defgroup key_lifetimes Key lifetimes
1400 * @{
1401 */
1402
1403/** A volatile key only exists as long as the handle to it is not closed.
1404 * The key material is guaranteed to be erased on a power reset.
1405 */
1406#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1407
1408/** The default storage area for persistent keys.
1409 *
1410 * A persistent key remains in storage until it is explicitly destroyed or
1411 * until the corresponding storage area is wiped. This specification does
1412 * not define any mechanism to wipe a storage area, but implementations may
1413 * provide their own mechanism (for example to perform a factory reset,
1414 * to prepare for device refurbishment, or to uninstall an application).
1415 *
1416 * This lifetime value is the default storage area for the calling
1417 * application. Implementations may offer other storage areas designated
1418 * by other lifetime values as implementation-specific extensions.
1419 */
1420#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1421
1422/**@}*/
1423
1424/** \defgroup policy Key policies
1425 * @{
1426 */
1427
1428/** Whether the key may be exported.
1429 *
1430 * A public key or the public part of a key pair may always be exported
1431 * regardless of the value of this permission flag.
1432 *
1433 * If a key does not have export permission, implementations shall not
1434 * allow the key to be exported in plain form from the cryptoprocessor,
1435 * whether through psa_export_key() or through a proprietary interface.
1436 * The key may however be exportable in a wrapped form, i.e. in a form
1437 * where it is encrypted by another key.
1438 */
1439#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1440
1441/** Whether the key may be used to encrypt a message.
1442 *
1443 * This flag allows the key to be used for a symmetric encryption operation,
1444 * for an AEAD encryption-and-authentication operation,
1445 * or for an asymmetric encryption operation,
1446 * if otherwise permitted by the key's type and policy.
1447 *
1448 * For a key pair, this concerns the public key.
1449 */
1450#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
1451
1452/** Whether the key may be used to decrypt a message.
1453 *
1454 * This flag allows the key to be used for a symmetric decryption operation,
1455 * for an AEAD decryption-and-verification operation,
1456 * or for an asymmetric decryption operation,
1457 * if otherwise permitted by the key's type and policy.
1458 *
1459 * For a key pair, this concerns the private key.
1460 */
1461#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
1462
1463/** Whether the key may be used to sign a message.
1464 *
1465 * This flag allows the key to be used for a MAC calculation operation
1466 * or for an asymmetric signature operation,
1467 * if otherwise permitted by the key's type and policy.
1468 *
1469 * For a key pair, this concerns the private key.
1470 */
1471#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
1472
1473/** Whether the key may be used to verify a message signature.
1474 *
1475 * This flag allows the key to be used for a MAC verification operation
1476 * or for an asymmetric signature verification operation,
1477 * if otherwise permitted by by the key's type and policy.
1478 *
1479 * For a key pair, this concerns the public key.
1480 */
1481#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
1482
1483/** Whether the key may be used to derive other keys.
1484 */
1485#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00001000)
1486
1487/**@}*/
1488
1489#endif /* PSA_CRYPTO_VALUES_H */