blob: 38b067a2d08743b38dc78d59ab17357744d88d88 [file] [log] [blame]
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001/**
2 * \file psa/crypto_struct.h
3 *
4 * \brief PSA cryptography module: Mbed TLS structured type implementations
Gilles Peskine07c91f52018-06-28 18:02:53 +02005 *
6 * \note This file may not be included directly. Applications must
7 * include psa/crypto.h.
8 *
9 * This file contains the definitions of some data structures with
10 * implementation-specific definitions.
11 *
12 * In implementations with isolation between the application and the
13 * cryptography module, it is expected that the front-end and the back-end
14 * would have different versions of this file.
Gilles Peskineb4e73e92019-08-13 15:00:57 +020015 *
16 * <h3>Design notes about multipart operation structures</h3>
17 *
Ronald Cron980230e2021-04-01 15:37:49 +020018 * For multipart operations without driver delegation support, each multipart
19 * operation structure contains a `psa_algorithm_t alg` field which indicates
20 * which specific algorithm the structure is for. When the structure is not in
21 * use, `alg` is 0. Most of the structure consists of a union which is
22 * discriminated by `alg`.
Gilles Peskineb4e73e92019-08-13 15:00:57 +020023 *
Ronald Cron980230e2021-04-01 15:37:49 +020024 * For multipart operations with driver delegation support, each multipart
25 * operation structure contains an `unsigned int id` field indicating which
26 * driver got assigned to do the operation. When the structure is not in use,
27 * 'id' is 0. The structure contains also a driver context which is the union
28 * of the contexts of all drivers able to handle the type of multipart
29 * operation.
30 *
31 * Note that when `alg` or `id` is 0, the content of other fields is undefined.
Gilles Peskineb4e73e92019-08-13 15:00:57 +020032 * In particular, it is not guaranteed that a freshly-initialized structure
33 * is all-zero: we initialize structures to something like `{0, 0}`, which
34 * is only guaranteed to initializes the first member of the union;
35 * GCC and Clang initialize the whole structure to 0 (at the time of writing),
36 * but MSVC and CompCert don't.
37 *
Fredrik Hessecc207bc2021-09-28 21:06:08 +020038 * In Mbed TLS, multipart operation structures live independently from
39 * the key. This allows Mbed TLS to free the key objects when destroying
Gilles Peskineb4e73e92019-08-13 15:00:57 +020040 * a key slot. If a multipart operation needs to remember the key after
41 * the setup function returns, the operation structure needs to contain a
42 * copy of the key.
Gilles Peskine9ef733f2018-02-07 21:05:37 +010043 */
44/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020045 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000046 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine9ef733f2018-02-07 21:05:37 +010047 */
48
49#ifndef PSA_CRYPTO_STRUCT_H
50#define PSA_CRYPTO_STRUCT_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020051#include "mbedtls/private_access.h"
Gilles Peskine9ef733f2018-02-07 21:05:37 +010052
Jaeden Amero8013f442019-08-16 16:13:51 +010053#ifdef __cplusplus
54extern "C" {
55#endif
56
Ronald Cronf6d17ca2024-11-06 14:11:15 +010057#include "tf-psa-crypto/build_info.h"
Gilles Peskine9ef733f2018-02-07 21:05:37 +010058
Steven Cooreman61398ec2021-04-26 12:04:53 +020059/* Include the context definition for the compiled-in drivers for the primitive
60 * algorithms. */
Steven Cooreman675501d2021-04-26 11:54:58 +020061#include "psa/crypto_driver_contexts_primitives.h"
Gilles Peskine9ef733f2018-02-07 21:05:37 +010062
Gilles Peskine449bd832023-01-11 14:50:10 +010063struct psa_hash_operation_s {
Antonio de Angelis6425a182024-01-22 11:39:34 +000064#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
65 mbedtls_psa_client_handle_t handle;
66#else
Steven Cooremandbf8ced2021-03-04 13:01:18 +010067 /** Unique ID indicating which driver got assigned to do the
68 * operation. Since driver contexts are driver-specific, swapping
69 * drivers halfway through the operation is not supported.
Ronald Cron980230e2021-04-01 15:37:49 +020070 * ID values are auto-generated in psa_driver_wrappers.h.
Steven Cooremandbf8ced2021-03-04 13:01:18 +010071 * ID value zero means the context is not valid or not assigned to
Ronald Cron980230e2021-04-01 15:37:49 +020072 * any driver (i.e. the driver context is not active, in use). */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020073 unsigned int MBEDTLS_PRIVATE(id);
74 psa_driver_hash_context_t MBEDTLS_PRIVATE(ctx);
Antonio de Angelis6425a182024-01-22 11:39:34 +000075#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +010076};
Antonio de Angelis90d18342024-01-22 13:15:37 +000077#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
78#define PSA_HASH_OPERATION_INIT { 0 }
79#else
Janos Follath33434a92021-05-26 09:25:33 +010080#define PSA_HASH_OPERATION_INIT { 0, { 0 } }
Antonio de Angelis90d18342024-01-22 13:15:37 +000081#endif
Gilles Peskine449bd832023-01-11 14:50:10 +010082static inline struct psa_hash_operation_s psa_hash_operation_init(void)
Jaeden Amero6a25b412019-01-04 11:47:44 +000083{
84 const struct psa_hash_operation_s v = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +010085 return v;
Jaeden Amero6a25b412019-01-04 11:47:44 +000086}
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088struct psa_cipher_operation_s {
Antonio de Angelis6425a182024-01-22 11:39:34 +000089#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
90 mbedtls_psa_client_handle_t handle;
91#else
Steven Cooreman61398ec2021-04-26 12:04:53 +020092 /** Unique ID indicating which driver got assigned to do the
93 * operation. Since driver contexts are driver-specific, swapping
94 * drivers halfway through the operation is not supported.
95 * ID values are auto-generated in psa_crypto_driver_wrappers.h
96 * ID value zero means the context is not valid or not assigned to
97 * any driver (i.e. none of the driver contexts are active). */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020098 unsigned int MBEDTLS_PRIVATE(id);
Steven Cooreman61398ec2021-04-26 12:04:53 +020099
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200100 unsigned int MBEDTLS_PRIVATE(iv_required) : 1;
101 unsigned int MBEDTLS_PRIVATE(iv_set) : 1;
Steven Cooreman61398ec2021-04-26 12:04:53 +0200102
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200103 uint8_t MBEDTLS_PRIVATE(default_iv_length);
Steven Cooreman61398ec2021-04-26 12:04:53 +0200104
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200105 psa_driver_cipher_context_t MBEDTLS_PRIVATE(ctx);
Antonio de Angelis6425a182024-01-22 11:39:34 +0000106#endif
Steven Cooreman61398ec2021-04-26 12:04:53 +0200107};
108
Antonio de Angelis90d18342024-01-22 13:15:37 +0000109#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
110#define PSA_CIPHER_OPERATION_INIT { 0 }
111#else
Janos Follath33434a92021-05-26 09:25:33 +0100112#define PSA_CIPHER_OPERATION_INIT { 0, 0, 0, 0, { 0 } }
Antonio de Angelis90d18342024-01-22 13:15:37 +0000113#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100114static inline struct psa_cipher_operation_s psa_cipher_operation_init(void)
Steven Cooreman61398ec2021-04-26 12:04:53 +0200115{
116 const struct psa_cipher_operation_s v = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 return v;
Steven Cooreman61398ec2021-04-26 12:04:53 +0200118}
119
Steven Cooreman3c8dd632021-04-26 12:16:27 +0200120/* Include the context definition for the compiled-in drivers for the composite
121 * algorithms. */
122#include "psa/crypto_driver_contexts_composites.h"
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +0300123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124struct psa_mac_operation_s {
Antonio de Angelis6425a182024-01-22 11:39:34 +0000125#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
126 mbedtls_psa_client_handle_t handle;
127#else
Steven Cooreman77e2cc52021-03-19 17:05:52 +0100128 /** Unique ID indicating which driver got assigned to do the
129 * operation. Since driver contexts are driver-specific, swapping
130 * drivers halfway through the operation is not supported.
131 * ID values are auto-generated in psa_driver_wrappers.h
132 * ID value zero means the context is not valid or not assigned to
133 * any driver (i.e. none of the driver contexts are active). */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200134 unsigned int MBEDTLS_PRIVATE(id);
135 uint8_t MBEDTLS_PRIVATE(mac_size);
136 unsigned int MBEDTLS_PRIVATE(is_sign) : 1;
137 psa_driver_mac_context_t MBEDTLS_PRIVATE(ctx);
Antonio de Angelis6425a182024-01-22 11:39:34 +0000138#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100139};
140
Antonio de Angelis90d18342024-01-22 13:15:37 +0000141#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
142#define PSA_MAC_OPERATION_INIT { 0 }
143#else
Janos Follath33434a92021-05-26 09:25:33 +0100144#define PSA_MAC_OPERATION_INIT { 0, 0, 0, { 0 } }
Antonio de Angelis90d18342024-01-22 13:15:37 +0000145#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100146static inline struct psa_mac_operation_s psa_mac_operation_init(void)
Jaeden Amero769ce272019-01-04 11:48:03 +0000147{
148 const struct psa_mac_operation_s v = PSA_MAC_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 return v;
Jaeden Amero769ce272019-01-04 11:48:03 +0000150}
151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152struct psa_aead_operation_s {
Antonio de Angelis6425a182024-01-22 11:39:34 +0000153#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
154 mbedtls_psa_client_handle_t handle;
155#else
Paul Elliott6504aa62021-04-20 17:09:36 +0100156 /** Unique ID indicating which driver got assigned to do the
157 * operation. Since driver contexts are driver-specific, swapping
158 * drivers halfway through the operation is not supported.
159 * ID values are auto-generated in psa_crypto_driver_wrappers.h
160 * ID value zero means the context is not valid or not assigned to
161 * any driver (i.e. none of the driver contexts are active). */
Paul Elliottc7e7fe52021-09-27 09:23:40 +0100162 unsigned int MBEDTLS_PRIVATE(id);
Paul Elliott6504aa62021-04-20 17:09:36 +0100163
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200164 psa_algorithm_t MBEDTLS_PRIVATE(alg);
Paul Elliott71b05672021-09-24 11:18:13 +0100165 psa_key_type_t MBEDTLS_PRIVATE(key_type);
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100166
Paul Elliott71b05672021-09-24 11:18:13 +0100167 size_t MBEDTLS_PRIVATE(ad_remaining);
168 size_t MBEDTLS_PRIVATE(body_remaining);
Paul Elliottee4ffe02021-05-20 17:25:06 +0100169
Paul Elliott71b05672021-09-24 11:18:13 +0100170 unsigned int MBEDTLS_PRIVATE(nonce_set) : 1;
171 unsigned int MBEDTLS_PRIVATE(lengths_set) : 1;
172 unsigned int MBEDTLS_PRIVATE(ad_started) : 1;
173 unsigned int MBEDTLS_PRIVATE(body_started) : 1;
174 unsigned int MBEDTLS_PRIVATE(is_encrypt) : 1;
Paul Elliott07a30c42021-04-20 14:13:23 +0100175
Paul Elliott71b05672021-09-24 11:18:13 +0100176 psa_driver_aead_context_t MBEDTLS_PRIVATE(ctx);
Antonio de Angelis6425a182024-01-22 11:39:34 +0000177#endif
Gilles Peskine30a9e412019-01-14 18:36:12 +0100178};
179
Antonio de Angelis90d18342024-01-22 13:15:37 +0000180#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
181#define PSA_AEAD_OPERATION_INIT { 0 }
182#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100183#define PSA_AEAD_OPERATION_INIT { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0 } }
Antonio de Angelis90d18342024-01-22 13:15:37 +0000184#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100185static inline struct psa_aead_operation_s psa_aead_operation_init(void)
Gilles Peskine30a9e412019-01-14 18:36:12 +0100186{
187 const struct psa_aead_operation_s v = PSA_AEAD_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 return v;
Gilles Peskine30a9e412019-01-14 18:36:12 +0100189}
190
Ronald Cron2f10fce2023-01-11 09:21:47 +0100191/* Include the context definition for the compiled-in drivers for the key
192 * derivation algorithms. */
193#include "psa/crypto_driver_contexts_key_derivation.h"
Hanno Beckerc8a41d72018-10-09 17:33:01 +0100194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195struct psa_key_derivation_s {
Antonio de Angelis6425a182024-01-22 11:39:34 +0000196#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
197 mbedtls_psa_client_handle_t handle;
198#else
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200199 psa_algorithm_t MBEDTLS_PRIVATE(alg);
200 unsigned int MBEDTLS_PRIVATE(can_output_key) : 1;
201 size_t MBEDTLS_PRIVATE(capacity);
Ronald Cron2f10fce2023-01-11 09:21:47 +0100202 psa_driver_key_derivation_context_t MBEDTLS_PRIVATE(ctx);
Antonio de Angelis6425a182024-01-22 11:39:34 +0000203#endif
Gilles Peskineeab56e42018-07-12 17:12:33 +0200204};
205
Antonio de Angelis90d18342024-01-22 13:15:37 +0000206#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
207#define PSA_KEY_DERIVATION_OPERATION_INIT { 0 }
208#else
Janos Follathadbec812019-06-14 11:05:39 +0100209/* This only zeroes out the first byte in the union, the rest is unspecified. */
Janos Follath33434a92021-05-26 09:25:33 +0100210#define PSA_KEY_DERIVATION_OPERATION_INIT { 0, 0, 0, { 0 } }
Antonio de Angelis90d18342024-01-22 13:15:37 +0000211#endif
Janos Follath0dcda952021-06-07 14:52:13 +0100212static inline struct psa_key_derivation_s psa_key_derivation_operation_init(
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 void)
Gilles Peskineeab56e42018-07-12 17:12:33 +0200214{
Gilles Peskinecbe66502019-05-16 16:59:18 +0200215 const struct psa_key_derivation_s v = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 return v;
Gilles Peskineeab56e42018-07-12 17:12:33 +0200217}
218
Gilles Peskinef9408282024-06-06 21:11:44 +0200219struct psa_custom_key_parameters_s {
Gilles Peskine6d81cbc2024-02-12 16:25:19 +0100220 /* Future versions may add other fields in this structure. */
221 uint32_t flags;
Gilles Peskinef9408282024-06-06 21:11:44 +0200222};
223
224/** The default production parameters for key generation or key derivation.
225 *
226 * Calling psa_generate_key_custom() or psa_key_derivation_output_key_custom()
227 * with `custom=PSA_CUSTOM_KEY_PARAMETERS_INIT` and `custom_data_length=0` is
228 * equivalent to calling psa_generate_key() or psa_key_derivation_output_key()
229 * respectively.
230 */
231#define PSA_CUSTOM_KEY_PARAMETERS_INIT { 0 }
232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233struct psa_key_policy_s {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200234 psa_key_usage_t MBEDTLS_PRIVATE(usage);
235 psa_algorithm_t MBEDTLS_PRIVATE(alg);
236 psa_algorithm_t MBEDTLS_PRIVATE(alg2);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100237};
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200238typedef struct psa_key_policy_s psa_key_policy_t;
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100239
Janos Follath33434a92021-05-26 09:25:33 +0100240#define PSA_KEY_POLICY_INIT { 0, 0, 0 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100241static inline struct psa_key_policy_s psa_key_policy_init(void)
Jaeden Amero70261c52019-01-04 11:47:20 +0000242{
243 const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return v;
Jaeden Amero70261c52019-01-04 11:47:20 +0000245}
246
Gilles Peskine68cc433b2019-07-30 17:42:47 +0200247/* The type used internally for key sizes.
248 * Public interfaces use size_t, but internally we use a smaller type. */
249typedef uint16_t psa_key_bits_t;
250/* The maximum value of the type used to represent bit-sizes.
251 * This is used to mark an invalid key size. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100252#define PSA_KEY_BITS_TOO_LARGE ((psa_key_bits_t) -1)
Gilles Peskinec744d992019-07-30 17:26:54 +0200253/* The maximum size of a key in bits.
Gilles Peskine68cc433b2019-07-30 17:42:47 +0200254 * Currently defined as the maximum that can be represented, rounded down
255 * to a whole number of bytes.
256 * This is an uncast value so that it can be used in preprocessor
257 * conditionals. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200258#define PSA_MAX_KEY_BITS 0xfff8
259
Gilles Peskine7fad3ef2024-02-28 01:08:27 +0100260struct psa_key_attributes_s {
Gilles Peskine7fad3ef2024-02-28 01:08:27 +0100261#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
262 psa_key_slot_number_t MBEDTLS_PRIVATE(slot_number);
Gilles Peskine972539c2024-02-28 01:49:45 +0100263 int MBEDTLS_PRIVATE(has_slot_number);
Gilles Peskine7fad3ef2024-02-28 01:08:27 +0100264#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
265 psa_key_type_t MBEDTLS_PRIVATE(type);
266 psa_key_bits_t MBEDTLS_PRIVATE(bits);
267 psa_key_lifetime_t MBEDTLS_PRIVATE(lifetime);
268 psa_key_policy_t MBEDTLS_PRIVATE(policy);
Gilles Peskine7fad3ef2024-02-28 01:08:27 +0100269 /* This type has a different layout in the client view wrt the
270 * service view of the key id, i.e. in service view usually is
271 * expected to have MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined
272 * thus adding an owner field to the standard psa_key_id_t. For
273 * implementations with client/service separation, this means the
274 * object will be marshalled through a transport channel and
275 * interpreted differently at each side of the transport. Placing
276 * it at the end of structures allows to interpret the structure
277 * at the client without reorganizing the memory layout of the
278 * struct
279 */
280 mbedtls_svc_key_id_t MBEDTLS_PRIVATE(id);
Gilles Peskine7fad3ef2024-02-28 01:08:27 +0100281};
282
Gilles Peskine0f40a412024-02-27 23:21:15 +0100283#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine972539c2024-02-28 01:49:45 +0100284#define PSA_KEY_ATTRIBUTES_MAYBE_SLOT_NUMBER 0, 0,
Gilles Peskine0f40a412024-02-27 23:21:15 +0100285#else
286#define PSA_KEY_ATTRIBUTES_MAYBE_SLOT_NUMBER
287#endif
Gilles Peskine2dc2bd72024-02-28 01:32:31 +0100288#define PSA_KEY_ATTRIBUTES_INIT { PSA_KEY_ATTRIBUTES_MAYBE_SLOT_NUMBER \
Gilles Peskine0eb4e7f2024-02-28 10:56:14 +0100289 PSA_KEY_TYPE_NONE, 0, \
290 PSA_KEY_LIFETIME_VOLATILE, \
291 PSA_KEY_POLICY_INIT, \
292 MBEDTLS_SVC_KEY_ID_INIT }
Gilles Peskinec8000c02019-08-02 20:15:51 +0200293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294static inline struct psa_key_attributes_s psa_key_attributes_init(void)
Gilles Peskine4747d192019-04-17 15:05:45 +0200295{
296 const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return v;
Gilles Peskine4747d192019-04-17 15:05:45 +0200298}
299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300static inline void psa_set_key_id(psa_key_attributes_t *attributes,
301 mbedtls_svc_key_id_t key)
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200302{
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100303 psa_key_lifetime_t lifetime = attributes->MBEDTLS_PRIVATE(lifetime);
Ronald Crond98059d2020-10-23 18:00:55 +0200304
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100305 attributes->MBEDTLS_PRIVATE(id) = key;
Ronald Crond98059d2020-10-23 18:00:55 +0200306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100308 attributes->MBEDTLS_PRIVATE(lifetime) =
Ronald Crond98059d2020-10-23 18:00:55 +0200309 PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
310 PSA_KEY_LIFETIME_PERSISTENT,
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 PSA_KEY_LIFETIME_GET_LOCATION(lifetime));
Ronald Crond98059d2020-10-23 18:00:55 +0200312 }
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200313}
314
Ronald Cron71016a92020-08-28 19:01:50 +0200315static inline mbedtls_svc_key_id_t psa_get_key_id(
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 const psa_key_attributes_t *attributes)
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200317{
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100318 return attributes->MBEDTLS_PRIVATE(id);
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200319}
320
Ronald Cron6b5ff532020-10-16 14:38:19 +0200321#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
Gilles Peskine449bd832023-01-11 14:50:10 +0100322static inline void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes,
323 mbedtls_key_owner_id_t owner)
Ronald Cron6b5ff532020-10-16 14:38:19 +0200324{
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100325 attributes->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(owner) = owner;
Ronald Cron6b5ff532020-10-16 14:38:19 +0200326}
327#endif
328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
330 psa_key_lifetime_t lifetime)
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200331{
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100332 attributes->MBEDTLS_PRIVATE(lifetime) = lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
Ronald Cron71016a92020-08-28 19:01:50 +0200334#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100335 attributes->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id) = 0;
Jaeden Ameroe3cdf282019-08-20 12:58:20 +0100336#else
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100337 attributes->MBEDTLS_PRIVATE(id) = 0;
Jaeden Ameroe3cdf282019-08-20 12:58:20 +0100338#endif
339 }
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200340}
341
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200342static inline psa_key_lifetime_t psa_get_key_lifetime(
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 const psa_key_attributes_t *attributes)
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200344{
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100345 return attributes->MBEDTLS_PRIVATE(lifetime);
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200346}
347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348static inline void psa_extend_key_usage_flags(psa_key_usage_t *usage_flags)
Gilles Peskine4747d192019-04-17 15:05:45 +0200349{
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (*usage_flags & PSA_KEY_USAGE_SIGN_HASH) {
gabor-mezei-arm43110b62021-06-23 16:48:08 +0200351 *usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 }
gabor-mezei-arm86bf0082021-04-29 15:57:57 +0200353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 if (*usage_flags & PSA_KEY_USAGE_VERIFY_HASH) {
gabor-mezei-arm43110b62021-06-23 16:48:08 +0200355 *usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 }
Gilles Peskine4747d192019-04-17 15:05:45 +0200357}
358
359static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
360 psa_key_usage_t usage_flags)
361{
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 psa_extend_key_usage_flags(&usage_flags);
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100363 attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage) = usage_flags;
Gilles Peskine4747d192019-04-17 15:05:45 +0200364}
365
366static inline psa_key_usage_t psa_get_key_usage_flags(
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 const psa_key_attributes_t *attributes)
Gilles Peskine4747d192019-04-17 15:05:45 +0200368{
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100369 return attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage);
Gilles Peskine4747d192019-04-17 15:05:45 +0200370}
371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
373 psa_algorithm_t alg)
Gilles Peskine4747d192019-04-17 15:05:45 +0200374{
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100375 attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg) = alg;
Gilles Peskine4747d192019-04-17 15:05:45 +0200376}
377
378static inline psa_algorithm_t psa_get_key_algorithm(
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 const psa_key_attributes_t *attributes)
Gilles Peskine4747d192019-04-17 15:05:45 +0200380{
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100381 return attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg);
Gilles Peskine4747d192019-04-17 15:05:45 +0200382}
383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384static inline void psa_set_key_type(psa_key_attributes_t *attributes,
385 psa_key_type_t type)
Gilles Peskine4747d192019-04-17 15:05:45 +0200386{
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100387 attributes->MBEDTLS_PRIVATE(type) = type;
Gilles Peskine4747d192019-04-17 15:05:45 +0200388}
389
390static inline psa_key_type_t psa_get_key_type(
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 const psa_key_attributes_t *attributes)
Gilles Peskine4747d192019-04-17 15:05:45 +0200392{
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100393 return attributes->MBEDTLS_PRIVATE(type);
Gilles Peskine4747d192019-04-17 15:05:45 +0200394}
395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
397 size_t bits)
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200398{
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if (bits > PSA_MAX_KEY_BITS) {
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100400 attributes->MBEDTLS_PRIVATE(bits) = PSA_KEY_BITS_TOO_LARGE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 } else {
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100402 attributes->MBEDTLS_PRIVATE(bits) = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 }
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200404}
405
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200406static inline size_t psa_get_key_bits(
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 const psa_key_attributes_t *attributes)
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200408{
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100409 return attributes->MBEDTLS_PRIVATE(bits);
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200410}
411
Paul Elliott1265f002022-09-09 17:15:43 +0100412/**
413 * \brief The context for PSA interruptible hash signing.
Paul Elliott1265f002022-09-09 17:15:43 +0100414 */
415struct psa_sign_hash_interruptible_operation_s {
Antonio de Angelis4380a332024-02-02 14:21:24 +0000416#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
417 mbedtls_psa_client_handle_t handle;
418#else
Paul Elliott2d247922022-11-29 14:54:44 +0000419 /** Unique ID indicating which driver got assigned to do the
420 * operation. Since driver contexts are driver-specific, swapping
421 * drivers halfway through the operation is not supported.
422 * ID values are auto-generated in psa_crypto_driver_wrappers.h
423 * ID value zero means the context is not valid or not assigned to
424 * any driver (i.e. none of the driver contexts are active). */
425 unsigned int MBEDTLS_PRIVATE(id);
426
Paul Elliott588f8ed2022-12-02 18:10:26 +0000427 psa_driver_sign_hash_interruptible_context_t MBEDTLS_PRIVATE(ctx);
428
Paul Elliottc9774412023-02-06 15:14:07 +0000429 unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
430
Paul Elliott296ede92022-12-15 17:00:30 +0000431 uint32_t MBEDTLS_PRIVATE(num_ops);
Antonio de Angelis4380a332024-02-02 14:21:24 +0000432#endif
Paul Elliott1265f002022-09-09 17:15:43 +0100433};
434
Antonio de Angelis4380a332024-02-02 14:21:24 +0000435#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
436#define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
437#else
Paul Elliottc9774412023-02-06 15:14:07 +0000438#define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
Antonio de Angelis4380a332024-02-02 14:21:24 +0000439#endif
Paul Elliott1265f002022-09-09 17:15:43 +0100440
441static inline struct psa_sign_hash_interruptible_operation_s
442psa_sign_hash_interruptible_operation_init(void)
443{
444 const struct psa_sign_hash_interruptible_operation_s v =
445 PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT;
446
447 return v;
448}
449
450/**
451 * \brief The context for PSA interruptible hash verification.
Paul Elliott1265f002022-09-09 17:15:43 +0100452 */
453struct psa_verify_hash_interruptible_operation_s {
Antonio de Angelis4380a332024-02-02 14:21:24 +0000454#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
455 mbedtls_psa_client_handle_t handle;
456#else
Paul Elliott2d247922022-11-29 14:54:44 +0000457 /** Unique ID indicating which driver got assigned to do the
458 * operation. Since driver contexts are driver-specific, swapping
459 * drivers halfway through the operation is not supported.
460 * ID values are auto-generated in psa_crypto_driver_wrappers.h
461 * ID value zero means the context is not valid or not assigned to
462 * any driver (i.e. none of the driver contexts are active). */
463 unsigned int MBEDTLS_PRIVATE(id);
464
Paul Elliott588f8ed2022-12-02 18:10:26 +0000465 psa_driver_verify_hash_interruptible_context_t MBEDTLS_PRIVATE(ctx);
466
Paul Elliottc9774412023-02-06 15:14:07 +0000467 unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
468
Paul Elliott296ede92022-12-15 17:00:30 +0000469 uint32_t MBEDTLS_PRIVATE(num_ops);
Antonio de Angelis4380a332024-02-02 14:21:24 +0000470#endif
Paul Elliott1265f002022-09-09 17:15:43 +0100471};
472
Antonio de Angelis4380a332024-02-02 14:21:24 +0000473#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
474#define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
475#else
Paul Elliottc9774412023-02-06 15:14:07 +0000476#define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
Antonio de Angelis4380a332024-02-02 14:21:24 +0000477#endif
Paul Elliott1265f002022-09-09 17:15:43 +0100478
479static inline struct psa_verify_hash_interruptible_operation_s
480psa_verify_hash_interruptible_operation_init(void)
481{
482 const struct psa_verify_hash_interruptible_operation_s v =
483 PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT;
484
485 return v;
486}
487
Paul Elliott03d62b12024-07-18 19:31:57 +0100488/**
489 * \brief The context for PSA interruptible key agreement.
490 */
491struct psa_key_agreement_iop_s {
492#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
493 mbedtls_psa_client_handle_t handle;
494#else
495 /**
496 * Unique ID indicating which driver got assigned to do the
497 * operation. Since driver contexts are driver-specific, swapping
498 * drivers halfway through the operation is not supported.
499 * ID values are auto-generated in psa_crypto_driver_wrappers.h
500 * ID value zero means the context is not valid or not assigned to
501 * any driver (i.e. none of the driver contexts are active).
502 */
503 unsigned int MBEDTLS_PRIVATE(id);
Waleed Elmelegy86e518b2024-10-30 16:38:31 +0000504 mbedtls_psa_key_agreement_interruptible_operation_t MBEDTLS_PRIVATE(mbedtls_ctx);
Waleed Elmelegya2891a92024-08-06 10:55:09 +0100505 uint32_t MBEDTLS_PRIVATE(num_ops);
Waleed Elmelegy93be7a12024-09-24 18:26:25 +0100506 psa_key_attributes_t MBEDTLS_PRIVATE(attributes);
Waleed Elmelegya98aeaf2024-09-24 21:04:54 +0100507 unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
Paul Elliott03d62b12024-07-18 19:31:57 +0100508#endif
509};
510
511#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
512#define PSA_KEY_AGREEMENT_IOP_INIT { 0 }
513#else
Waleed Elmelegye980fbe2024-11-01 16:19:28 +0000514#define PSA_KEY_AGREEMENT_IOP_INIT { 0, MBEDTLS_PSA_KEY_AGREEMENT_IOP_INIT, 0, \
515 PSA_KEY_ATTRIBUTES_INIT, 0 }
Paul Elliott03d62b12024-07-18 19:31:57 +0100516#endif
517
518static inline struct psa_key_agreement_iop_s
519psa_key_agreement_iop_init(void)
520{
521 const struct psa_key_agreement_iop_s v = PSA_KEY_AGREEMENT_IOP_INIT;
522
523 return v;
524}
525
Paul Elliott9e143a72024-07-18 19:31:57 +0100526/**
Waleed Elmelegy5fc76a92024-11-15 17:28:29 +0000527 * \brief The context for PSA interruptible key generation.
Paul Elliott9e143a72024-07-18 19:31:57 +0100528 */
529struct psa_generate_key_iop_s {
530#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
531 mbedtls_psa_client_handle_t handle;
532#else
533 /**
534 * Unique ID indicating which driver got assigned to do the
535 * operation. Since driver contexts are driver-specific, swapping
536 * drivers halfway through the operation is not supported.
537 * ID values are auto-generated in psa_crypto_driver_wrappers.h
538 * ID value zero means the context is not valid or not assigned to
539 * any driver (i.e. none of the driver contexts are active).
540 */
541 unsigned int MBEDTLS_PRIVATE(id);
Waleed Elmelegycca4dbe2024-09-25 17:49:14 +0100542 mbedtls_psa_generate_key_iop_t MBEDTLS_PRIVATE(ctx);
543 psa_key_attributes_t MBEDTLS_PRIVATE(attributes);
544 unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
545 uint32_t MBEDTLS_PRIVATE(num_ops);
Paul Elliott9e143a72024-07-18 19:31:57 +0100546#endif
547};
548
549#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
550#define PSA_GENERATE_KEY_IOP_INIT { 0 }
551#else
Waleed Elmelegycca4dbe2024-09-25 17:49:14 +0100552#define PSA_GENERATE_KEY_IOP_INIT { 0, MBEDTLS_PSA_GENERATE_KEY_IOP_INIT, PSA_KEY_ATTRIBUTES_INIT, \
553 0, 0 }
Paul Elliott9e143a72024-07-18 19:31:57 +0100554#endif
555
556static inline struct psa_generate_key_iop_s
557psa_generate_key_iop_init(void)
558{
559 const struct psa_generate_key_iop_s v = PSA_GENERATE_KEY_IOP_INIT;
560
561 return v;
562}
563
Waleed Elmelegye34a5c52024-11-15 16:31:34 +0000564/**
565 * \brief The context for PSA interruptible export public-key.
566 */
567struct psa_export_public_key_iop_s {
568#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
569 mbedtls_psa_client_handle_t handle;
570#else
571 /**
572 * Unique ID indicating which driver got assigned to do the
573 * operation. Since driver contexts are driver-specific, swapping
574 * drivers halfway through the operation is not supported.
575 * ID values are auto-generated in psa_crypto_driver_wrappers.h
576 * ID value zero means the context is not valid or not assigned to
577 * any driver (i.e. none of the driver contexts are active).
578 */
579 unsigned int MBEDTLS_PRIVATE(id);
Waleed Elmelegy3c465352024-12-03 14:34:35 +0000580 mbedtls_psa_export_public_key_iop_t MBEDTLS_PRIVATE(ctx);
Waleed Elmelegy9958ff62024-11-20 11:47:13 +0000581 unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
582 uint32_t MBEDTLS_PRIVATE(num_ops);
Waleed Elmelegye34a5c52024-11-15 16:31:34 +0000583#endif
584};
Waleed Elmelegy9958ff62024-11-20 11:47:13 +0000585
Waleed Elmelegye34a5c52024-11-15 16:31:34 +0000586#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
587#define PSA_EXPORT_PUBLIC_KEY_IOP_INIT { 0 }
588#else
Waleed Elmelegy9958ff62024-11-20 11:47:13 +0000589#define PSA_EXPORT_PUBLIC_KEY_IOP_INIT { 0, MBEDTLS_PSA_EXPORT_PUBLIC_KEY_IOP_INIT, 0, 0 }
Waleed Elmelegye34a5c52024-11-15 16:31:34 +0000590#endif
591
Waleed Elmelegy9958ff62024-11-20 11:47:13 +0000592static inline struct psa_export_public_key_iop_s
593psa_export_public_key_iop_init(void)
Waleed Elmelegye34a5c52024-11-15 16:31:34 +0000594{
595 const struct psa_export_public_key_iop_s v = PSA_EXPORT_PUBLIC_KEY_IOP_INIT;
596
597 return v;
598}
599
Jaeden Amero8013f442019-08-16 16:13:51 +0100600#ifdef __cplusplus
601}
602#endif
603
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100604#endif /* PSA_CRYPTO_STRUCT_H */