blob: e2068e8840cf6020f16fa0e44d58efe1f570f289 [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 Cron7871cb12023-10-10 08:51:39 +020057/*
58 * Include the build-time configuration information header. Here, we do not
59 * include `"mbedtls/build_info.h"` directly but `"psa/build_info.h"`, which
60 * is basically just an alias to it. This is to ease the maintenance of the
61 * TF-PSA-Crypto repository which has a different build system and
62 * configuration.
63 */
64#include "psa/build_info.h"
Gilles Peskine9ef733f2018-02-07 21:05:37 +010065
Steven Cooreman61398ec2021-04-26 12:04:53 +020066/* Include the context definition for the compiled-in drivers for the primitive
67 * algorithms. */
Steven Cooreman675501d2021-04-26 11:54:58 +020068#include "psa/crypto_driver_contexts_primitives.h"
Gilles Peskine9ef733f2018-02-07 21:05:37 +010069
Gilles Peskine449bd832023-01-11 14:50:10 +010070struct psa_hash_operation_s {
Antonio de Angelis6425a182024-01-22 11:39:34 +000071#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
72 mbedtls_psa_client_handle_t handle;
73#else
Steven Cooremandbf8ced2021-03-04 13:01:18 +010074 /** Unique ID indicating which driver got assigned to do the
75 * operation. Since driver contexts are driver-specific, swapping
76 * drivers halfway through the operation is not supported.
Ronald Cron980230e2021-04-01 15:37:49 +020077 * ID values are auto-generated in psa_driver_wrappers.h.
Steven Cooremandbf8ced2021-03-04 13:01:18 +010078 * ID value zero means the context is not valid or not assigned to
Ronald Cron980230e2021-04-01 15:37:49 +020079 * any driver (i.e. the driver context is not active, in use). */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020080 unsigned int MBEDTLS_PRIVATE(id);
81 psa_driver_hash_context_t MBEDTLS_PRIVATE(ctx);
Antonio de Angelis6425a182024-01-22 11:39:34 +000082#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +010083};
Antonio de Angelis90d18342024-01-22 13:15:37 +000084#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
85#define PSA_HASH_OPERATION_INIT { 0 }
86#else
Janos Follath33434a92021-05-26 09:25:33 +010087#define PSA_HASH_OPERATION_INIT { 0, { 0 } }
Antonio de Angelis90d18342024-01-22 13:15:37 +000088#endif
Gilles Peskine449bd832023-01-11 14:50:10 +010089static inline struct psa_hash_operation_s psa_hash_operation_init(void)
Jaeden Amero6a25b412019-01-04 11:47:44 +000090{
91 const struct psa_hash_operation_s v = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +010092 return v;
Jaeden Amero6a25b412019-01-04 11:47:44 +000093}
94
Gilles Peskine449bd832023-01-11 14:50:10 +010095struct psa_cipher_operation_s {
Antonio de Angelis6425a182024-01-22 11:39:34 +000096#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
97 mbedtls_psa_client_handle_t handle;
98#else
Steven Cooreman61398ec2021-04-26 12:04:53 +020099 /** Unique ID indicating which driver got assigned to do the
100 * operation. Since driver contexts are driver-specific, swapping
101 * drivers halfway through the operation is not supported.
102 * ID values are auto-generated in psa_crypto_driver_wrappers.h
103 * ID value zero means the context is not valid or not assigned to
104 * any driver (i.e. none of the driver contexts are active). */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200105 unsigned int MBEDTLS_PRIVATE(id);
Steven Cooreman61398ec2021-04-26 12:04:53 +0200106
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200107 unsigned int MBEDTLS_PRIVATE(iv_required) : 1;
108 unsigned int MBEDTLS_PRIVATE(iv_set) : 1;
Steven Cooreman61398ec2021-04-26 12:04:53 +0200109
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200110 uint8_t MBEDTLS_PRIVATE(default_iv_length);
Steven Cooreman61398ec2021-04-26 12:04:53 +0200111
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200112 psa_driver_cipher_context_t MBEDTLS_PRIVATE(ctx);
Antonio de Angelis6425a182024-01-22 11:39:34 +0000113#endif
Steven Cooreman61398ec2021-04-26 12:04:53 +0200114};
115
Antonio de Angelis90d18342024-01-22 13:15:37 +0000116#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
117#define PSA_CIPHER_OPERATION_INIT { 0 }
118#else
Janos Follath33434a92021-05-26 09:25:33 +0100119#define PSA_CIPHER_OPERATION_INIT { 0, 0, 0, 0, { 0 } }
Antonio de Angelis90d18342024-01-22 13:15:37 +0000120#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100121static inline struct psa_cipher_operation_s psa_cipher_operation_init(void)
Steven Cooreman61398ec2021-04-26 12:04:53 +0200122{
123 const struct psa_cipher_operation_s v = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 return v;
Steven Cooreman61398ec2021-04-26 12:04:53 +0200125}
126
Steven Cooreman3c8dd632021-04-26 12:16:27 +0200127/* Include the context definition for the compiled-in drivers for the composite
128 * algorithms. */
129#include "psa/crypto_driver_contexts_composites.h"
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +0300130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131struct psa_mac_operation_s {
Antonio de Angelis6425a182024-01-22 11:39:34 +0000132#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
133 mbedtls_psa_client_handle_t handle;
134#else
Steven Cooreman77e2cc52021-03-19 17:05:52 +0100135 /** Unique ID indicating which driver got assigned to do the
136 * operation. Since driver contexts are driver-specific, swapping
137 * drivers halfway through the operation is not supported.
138 * ID values are auto-generated in psa_driver_wrappers.h
139 * ID value zero means the context is not valid or not assigned to
140 * any driver (i.e. none of the driver contexts are active). */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200141 unsigned int MBEDTLS_PRIVATE(id);
142 uint8_t MBEDTLS_PRIVATE(mac_size);
143 unsigned int MBEDTLS_PRIVATE(is_sign) : 1;
144 psa_driver_mac_context_t MBEDTLS_PRIVATE(ctx);
Antonio de Angelis6425a182024-01-22 11:39:34 +0000145#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100146};
147
Antonio de Angelis90d18342024-01-22 13:15:37 +0000148#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
149#define PSA_MAC_OPERATION_INIT { 0 }
150#else
Janos Follath33434a92021-05-26 09:25:33 +0100151#define PSA_MAC_OPERATION_INIT { 0, 0, 0, { 0 } }
Antonio de Angelis90d18342024-01-22 13:15:37 +0000152#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100153static inline struct psa_mac_operation_s psa_mac_operation_init(void)
Jaeden Amero769ce272019-01-04 11:48:03 +0000154{
155 const struct psa_mac_operation_s v = PSA_MAC_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return v;
Jaeden Amero769ce272019-01-04 11:48:03 +0000157}
158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159struct psa_aead_operation_s {
Antonio de Angelis6425a182024-01-22 11:39:34 +0000160#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
161 mbedtls_psa_client_handle_t handle;
162#else
Paul Elliott6504aa62021-04-20 17:09:36 +0100163 /** Unique ID indicating which driver got assigned to do the
164 * operation. Since driver contexts are driver-specific, swapping
165 * drivers halfway through the operation is not supported.
166 * ID values are auto-generated in psa_crypto_driver_wrappers.h
167 * ID value zero means the context is not valid or not assigned to
168 * any driver (i.e. none of the driver contexts are active). */
Paul Elliottc7e7fe52021-09-27 09:23:40 +0100169 unsigned int MBEDTLS_PRIVATE(id);
Paul Elliott6504aa62021-04-20 17:09:36 +0100170
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200171 psa_algorithm_t MBEDTLS_PRIVATE(alg);
Paul Elliott71b05672021-09-24 11:18:13 +0100172 psa_key_type_t MBEDTLS_PRIVATE(key_type);
Paul Elliottcbbde5f2021-05-10 18:19:46 +0100173
Paul Elliott71b05672021-09-24 11:18:13 +0100174 size_t MBEDTLS_PRIVATE(ad_remaining);
175 size_t MBEDTLS_PRIVATE(body_remaining);
Paul Elliottee4ffe02021-05-20 17:25:06 +0100176
Paul Elliott71b05672021-09-24 11:18:13 +0100177 unsigned int MBEDTLS_PRIVATE(nonce_set) : 1;
178 unsigned int MBEDTLS_PRIVATE(lengths_set) : 1;
179 unsigned int MBEDTLS_PRIVATE(ad_started) : 1;
180 unsigned int MBEDTLS_PRIVATE(body_started) : 1;
181 unsigned int MBEDTLS_PRIVATE(is_encrypt) : 1;
Paul Elliott07a30c42021-04-20 14:13:23 +0100182
Paul Elliott71b05672021-09-24 11:18:13 +0100183 psa_driver_aead_context_t MBEDTLS_PRIVATE(ctx);
Antonio de Angelis6425a182024-01-22 11:39:34 +0000184#endif
Gilles Peskine30a9e412019-01-14 18:36:12 +0100185};
186
Antonio de Angelis90d18342024-01-22 13:15:37 +0000187#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
188#define PSA_AEAD_OPERATION_INIT { 0 }
189#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100190#define PSA_AEAD_OPERATION_INIT { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0 } }
Antonio de Angelis90d18342024-01-22 13:15:37 +0000191#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100192static inline struct psa_aead_operation_s psa_aead_operation_init(void)
Gilles Peskine30a9e412019-01-14 18:36:12 +0100193{
194 const struct psa_aead_operation_s v = PSA_AEAD_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 return v;
Gilles Peskine30a9e412019-01-14 18:36:12 +0100196}
197
Ronald Cron2f10fce2023-01-11 09:21:47 +0100198/* Include the context definition for the compiled-in drivers for the key
199 * derivation algorithms. */
200#include "psa/crypto_driver_contexts_key_derivation.h"
Hanno Beckerc8a41d72018-10-09 17:33:01 +0100201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202struct psa_key_derivation_s {
Antonio de Angelis6425a182024-01-22 11:39:34 +0000203#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
204 mbedtls_psa_client_handle_t handle;
205#else
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200206 psa_algorithm_t MBEDTLS_PRIVATE(alg);
207 unsigned int MBEDTLS_PRIVATE(can_output_key) : 1;
208 size_t MBEDTLS_PRIVATE(capacity);
Ronald Cron2f10fce2023-01-11 09:21:47 +0100209 psa_driver_key_derivation_context_t MBEDTLS_PRIVATE(ctx);
Antonio de Angelis6425a182024-01-22 11:39:34 +0000210#endif
Gilles Peskineeab56e42018-07-12 17:12:33 +0200211};
212
Antonio de Angelis90d18342024-01-22 13:15:37 +0000213#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
214#define PSA_KEY_DERIVATION_OPERATION_INIT { 0 }
215#else
Janos Follathadbec812019-06-14 11:05:39 +0100216/* This only zeroes out the first byte in the union, the rest is unspecified. */
Janos Follath33434a92021-05-26 09:25:33 +0100217#define PSA_KEY_DERIVATION_OPERATION_INIT { 0, 0, 0, { 0 } }
Antonio de Angelis90d18342024-01-22 13:15:37 +0000218#endif
Janos Follath0dcda952021-06-07 14:52:13 +0100219static inline struct psa_key_derivation_s psa_key_derivation_operation_init(
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 void)
Gilles Peskineeab56e42018-07-12 17:12:33 +0200221{
Gilles Peskinecbe66502019-05-16 16:59:18 +0200222 const struct psa_key_derivation_s v = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 return v;
Gilles Peskineeab56e42018-07-12 17:12:33 +0200224}
225
Gilles Peskine092ce512024-02-20 12:31:24 +0100226struct psa_key_production_parameters_s {
Gilles Peskine6d81cbc2024-02-12 16:25:19 +0100227 /* Future versions may add other fields in this structure. */
228 uint32_t flags;
229 uint8_t data[];
230};
231
Gilles Peskine092ce512024-02-20 12:31:24 +0100232/** The default production parameters for key generation or key derivation.
Gilles Peskine6d81cbc2024-02-12 16:25:19 +0100233 *
234 * Calling psa_generate_key_ext() or psa_key_derivation_output_key_ext()
Gilles Peskine092ce512024-02-20 12:31:24 +0100235 * with `params=PSA_KEY_PRODUCTION_PARAMETERS_INIT` and
236 * `params_data_length == 0` is equivalent to
Gilles Peskine6d81cbc2024-02-12 16:25:19 +0100237 * calling psa_generate_key() or psa_key_derivation_output_key()
238 * respectively.
239 */
Gilles Peskine092ce512024-02-20 12:31:24 +0100240#define PSA_KEY_PRODUCTION_PARAMETERS_INIT { 0 }
Gilles Peskine6d81cbc2024-02-12 16:25:19 +0100241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242struct psa_key_policy_s {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200243 psa_key_usage_t MBEDTLS_PRIVATE(usage);
244 psa_algorithm_t MBEDTLS_PRIVATE(alg);
245 psa_algorithm_t MBEDTLS_PRIVATE(alg2);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100246};
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200247typedef struct psa_key_policy_s psa_key_policy_t;
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100248
Janos Follath33434a92021-05-26 09:25:33 +0100249#define PSA_KEY_POLICY_INIT { 0, 0, 0 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100250static inline struct psa_key_policy_s psa_key_policy_init(void)
Jaeden Amero70261c52019-01-04 11:47:20 +0000251{
252 const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 return v;
Jaeden Amero70261c52019-01-04 11:47:20 +0000254}
255
Gilles Peskine68cc433b2019-07-30 17:42:47 +0200256/* The type used internally for key sizes.
257 * Public interfaces use size_t, but internally we use a smaller type. */
258typedef uint16_t psa_key_bits_t;
259/* The maximum value of the type used to represent bit-sizes.
260 * This is used to mark an invalid key size. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100261#define PSA_KEY_BITS_TOO_LARGE ((psa_key_bits_t) -1)
Gilles Peskinec744d992019-07-30 17:26:54 +0200262/* The maximum size of a key in bits.
Gilles Peskine68cc433b2019-07-30 17:42:47 +0200263 * Currently defined as the maximum that can be represented, rounded down
264 * to a whole number of bytes.
265 * This is an uncast value so that it can be used in preprocessor
266 * conditionals. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200267#define PSA_MAX_KEY_BITS 0xfff8
268
Gilles Peskine91e8c332019-08-02 19:19:39 +0200269/** A mask of flags that can be stored in key attributes.
270 *
271 * This type is also used internally to store flags in slots. Internal
272 * flags are defined in library/psa_crypto_core.h. Internal flags may have
273 * the same value as external flags if they are properly handled during
274 * key creation and in psa_get_key_attributes.
275 */
276typedef uint16_t psa_key_attributes_flag_t;
277
Gilles Peskinec8000c02019-08-02 20:15:51 +0200278#define MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER \
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 ((psa_key_attributes_flag_t) 0x0001)
Gilles Peskine91e8c332019-08-02 19:19:39 +0200280
281/* A mask of key attribute flags used externally only.
282 * Only meant for internal checks inside the library. */
283#define MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ( \
Gilles Peskinec8000c02019-08-02 20:15:51 +0200284 MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER | \
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 0)
Gilles Peskine91e8c332019-08-02 19:19:39 +0200286
287/* A mask of key attribute flags used both internally and externally.
288 * Currently there aren't any. */
289#define MBEDTLS_PSA_KA_MASK_DUAL_USE ( \
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 0)
Gilles Peskine91e8c332019-08-02 19:19:39 +0200291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292typedef struct {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200293 psa_key_type_t MBEDTLS_PRIVATE(type);
294 psa_key_bits_t MBEDTLS_PRIVATE(bits);
295 psa_key_lifetime_t MBEDTLS_PRIVATE(lifetime);
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200296 psa_key_policy_t MBEDTLS_PRIVATE(policy);
297 psa_key_attributes_flag_t MBEDTLS_PRIVATE(flags);
Antonio de Angelis6fb1be62024-02-02 14:05:32 +0000298 /* This type has a different layout in the client view wrt the
299 * service view of the key id, i.e. in service view usually is
Antonio de Angelis6932e292024-02-05 09:49:43 +0000300 * expected to have MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined
Antonio de Angelis6fb1be62024-02-02 14:05:32 +0000301 * thus adding an owner field to the standard psa_key_id_t. For
302 * implementations with client/service separation, this means the
303 * object will be marshalled through a transport channel and
304 * interpreted differently at each side of the transport. Placing
305 * it at the end of structures allows to interpret the structure
306 * at the client without reorganizing the memory layout of the
307 * struct
308 */
Antonio de Angelis667cad52024-01-24 13:34:46 +0000309 mbedtls_svc_key_id_t MBEDTLS_PRIVATE(id);
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200310} psa_core_key_attributes_t;
311
Janos Follath33434a92021-05-26 09:25:33 +0100312#define PSA_CORE_KEY_ATTRIBUTES_INIT { PSA_KEY_TYPE_NONE, 0, \
313 PSA_KEY_LIFETIME_VOLATILE, \
Antonio de Angelis667cad52024-01-24 13:34:46 +0000314 PSA_KEY_POLICY_INIT, 0, \
315 MBEDTLS_SVC_KEY_ID_INIT }
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317struct psa_key_attributes_s {
Gilles Peskinec8000c02019-08-02 20:15:51 +0200318#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200319 psa_key_slot_number_t MBEDTLS_PRIVATE(slot_number);
Gilles Peskinec8000c02019-08-02 20:15:51 +0200320#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine9deb5492023-12-18 21:01:18 +0100321 /* Unlike normal buffers, there are three cases for domain_parameters
322 * and domain_parameters_size:
323 * - domain_parameters_size == SIZE_MAX && domain_parameters == NULL:
324 * Access to domain parameters is not supported for this key.
325 * This is a hack which should not exist, intended for keys managed
Gilles Peskine1a9e05b2023-12-19 12:23:22 +0100326 * by a driver, because drivers don't support domain parameters.
Gilles Peskine9deb5492023-12-18 21:01:18 +0100327 * - domain_parameters_size == 0 && domain_parameters == NULL:
328 * The domain parameters are empty.
329 * - domain_parameters_size > 0 &&
330 * domain_parameters == valid pointer to domain_parameters_size bytes:
331 * The domain parameters are non-empty.
332 */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200333 void *MBEDTLS_PRIVATE(domain_parameters);
334 size_t MBEDTLS_PRIVATE(domain_parameters_size);
Antonio de Angelis6fb1be62024-02-02 14:05:32 +0000335 /* With client/service separation, struct psa_key_attributes_s is
336 * marshalled through a transport channel between the client and
337 * service side implementation of the PSA Crypto APIs, thus having
338 * the mbedtls_svc_key_id_t id as the last field of this structure
339 * allows for a more efficient marshalling/unmarshalling of parameters
340 */
Antonio de Angelis667cad52024-01-24 13:34:46 +0000341 psa_core_key_attributes_t MBEDTLS_PRIVATE(core);
Gilles Peskine4747d192019-04-17 15:05:45 +0200342};
343
Gilles Peskinec8000c02019-08-02 20:15:51 +0200344#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Antonio de Angelis667cad52024-01-24 13:34:46 +0000345#define PSA_KEY_ATTRIBUTES_INIT { 0, NULL, 0, PSA_CORE_KEY_ATTRIBUTES_INIT }
Gilles Peskinec8000c02019-08-02 20:15:51 +0200346#else
Antonio de Angelis667cad52024-01-24 13:34:46 +0000347#define PSA_KEY_ATTRIBUTES_INIT { NULL, 0, PSA_CORE_KEY_ATTRIBUTES_INIT }
Gilles Peskinec8000c02019-08-02 20:15:51 +0200348#endif
349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350static inline struct psa_key_attributes_s psa_key_attributes_init(void)
Gilles Peskine4747d192019-04-17 15:05:45 +0200351{
352 const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return v;
Gilles Peskine4747d192019-04-17 15:05:45 +0200354}
355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356static inline void psa_set_key_id(psa_key_attributes_t *attributes,
357 mbedtls_svc_key_id_t key)
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200358{
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200359 psa_key_lifetime_t lifetime = attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime);
Ronald Crond98059d2020-10-23 18:00:55 +0200360
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200361 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id) = key;
Ronald Crond98059d2020-10-23 18:00:55 +0200362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200364 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime) =
Ronald Crond98059d2020-10-23 18:00:55 +0200365 PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
366 PSA_KEY_LIFETIME_PERSISTENT,
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 PSA_KEY_LIFETIME_GET_LOCATION(lifetime));
Ronald Crond98059d2020-10-23 18:00:55 +0200368 }
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200369}
370
Ronald Cron71016a92020-08-28 19:01:50 +0200371static inline mbedtls_svc_key_id_t psa_get_key_id(
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 const psa_key_attributes_t *attributes)
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200373{
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id);
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200375}
376
Ronald Cron6b5ff532020-10-16 14:38:19 +0200377#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
Gilles Peskine449bd832023-01-11 14:50:10 +0100378static inline void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes,
379 mbedtls_key_owner_id_t owner)
Ronald Cron6b5ff532020-10-16 14:38:19 +0200380{
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200381 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(owner) = owner;
Ronald Cron6b5ff532020-10-16 14:38:19 +0200382}
383#endif
384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
386 psa_key_lifetime_t lifetime)
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200387{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200388 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime) = lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
Ronald Cron71016a92020-08-28 19:01:50 +0200390#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200391 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id) = 0;
Jaeden Ameroe3cdf282019-08-20 12:58:20 +0100392#else
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200393 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id) = 0;
Jaeden Ameroe3cdf282019-08-20 12:58:20 +0100394#endif
395 }
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200396}
397
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200398static inline psa_key_lifetime_t psa_get_key_lifetime(
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 const psa_key_attributes_t *attributes)
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200400{
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime);
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200402}
403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404static inline void psa_extend_key_usage_flags(psa_key_usage_t *usage_flags)
Gilles Peskine4747d192019-04-17 15:05:45 +0200405{
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 if (*usage_flags & PSA_KEY_USAGE_SIGN_HASH) {
gabor-mezei-arm43110b62021-06-23 16:48:08 +0200407 *usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 }
gabor-mezei-arm86bf0082021-04-29 15:57:57 +0200409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 if (*usage_flags & PSA_KEY_USAGE_VERIFY_HASH) {
gabor-mezei-arm43110b62021-06-23 16:48:08 +0200411 *usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 }
Gilles Peskine4747d192019-04-17 15:05:45 +0200413}
414
415static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
416 psa_key_usage_t usage_flags)
417{
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 psa_extend_key_usage_flags(&usage_flags);
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200419 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage) = usage_flags;
Gilles Peskine4747d192019-04-17 15:05:45 +0200420}
421
422static inline psa_key_usage_t psa_get_key_usage_flags(
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 const psa_key_attributes_t *attributes)
Gilles Peskine4747d192019-04-17 15:05:45 +0200424{
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage);
Gilles Peskine4747d192019-04-17 15:05:45 +0200426}
427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
429 psa_algorithm_t alg)
Gilles Peskine4747d192019-04-17 15:05:45 +0200430{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200431 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg) = alg;
Gilles Peskine4747d192019-04-17 15:05:45 +0200432}
433
434static inline psa_algorithm_t psa_get_key_algorithm(
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 const psa_key_attributes_t *attributes)
Gilles Peskine4747d192019-04-17 15:05:45 +0200436{
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg);
Gilles Peskine4747d192019-04-17 15:05:45 +0200438}
439
Gilles Peskine24f10f82019-05-16 12:18:32 +0200440/* This function is declared in crypto_extra.h, which comes after this
441 * header file, but we need the function here, so repeat the declaration. */
Matthias Schulz5a39c4e2023-11-09 15:53:01 +0100442#if !defined(PSA_SET_KEY_DOMAIN_PARAMETERS)
443#define PSA_SET_KEY_DOMAIN_PARAMETERS
Gilles Peskine449bd832023-01-11 14:50:10 +0100444psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
Gilles Peskine24f10f82019-05-16 12:18:32 +0200445 psa_key_type_t type,
446 const uint8_t *data,
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 size_t data_length);
Matthias Schulz5a39c4e2023-11-09 15:53:01 +0100448#endif /* PSA_SET_KEY_DOMAIN_PARAMETERS */
Gilles Peskine24f10f82019-05-16 12:18:32 +0200449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450static inline void psa_set_key_type(psa_key_attributes_t *attributes,
451 psa_key_type_t type)
Gilles Peskine4747d192019-04-17 15:05:45 +0200452{
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 if (attributes->MBEDTLS_PRIVATE(domain_parameters) == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +0200454 /* Common case: quick path */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200455 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(type) = type;
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 } else {
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800457 /* Call the bigger function to free the old domain parameters.
Gilles Peskineb699f072019-04-26 16:06:02 +0200458 * Ignore any errors which may arise due to type requiring
459 * non-default domain parameters, since this function can't
460 * report errors. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 (void) psa_set_key_domain_parameters(attributes, type, NULL, 0);
Gilles Peskineb699f072019-04-26 16:06:02 +0200462 }
Gilles Peskine4747d192019-04-17 15:05:45 +0200463}
464
465static inline psa_key_type_t psa_get_key_type(
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 const psa_key_attributes_t *attributes)
Gilles Peskine4747d192019-04-17 15:05:45 +0200467{
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(type);
Gilles Peskine4747d192019-04-17 15:05:45 +0200469}
470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
472 size_t bits)
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200473{
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 if (bits > PSA_MAX_KEY_BITS) {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200475 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits) = PSA_KEY_BITS_TOO_LARGE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 } else {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200477 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits) = (psa_key_bits_t) bits;
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 }
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200479}
480
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200481static inline size_t psa_get_key_bits(
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 const psa_key_attributes_t *attributes)
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200483{
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits);
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200485}
486
Paul Elliott1265f002022-09-09 17:15:43 +0100487/**
488 * \brief The context for PSA interruptible hash signing.
Paul Elliott1265f002022-09-09 17:15:43 +0100489 */
490struct psa_sign_hash_interruptible_operation_s {
Antonio de Angelis4380a332024-02-02 14:21:24 +0000491#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
492 mbedtls_psa_client_handle_t handle;
493#else
Paul Elliott2d247922022-11-29 14:54:44 +0000494 /** Unique ID indicating which driver got assigned to do the
495 * operation. Since driver contexts are driver-specific, swapping
496 * drivers halfway through the operation is not supported.
497 * ID values are auto-generated in psa_crypto_driver_wrappers.h
498 * ID value zero means the context is not valid or not assigned to
499 * any driver (i.e. none of the driver contexts are active). */
500 unsigned int MBEDTLS_PRIVATE(id);
501
Paul Elliott588f8ed2022-12-02 18:10:26 +0000502 psa_driver_sign_hash_interruptible_context_t MBEDTLS_PRIVATE(ctx);
503
Paul Elliottc9774412023-02-06 15:14:07 +0000504 unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
505
Paul Elliott296ede92022-12-15 17:00:30 +0000506 uint32_t MBEDTLS_PRIVATE(num_ops);
Antonio de Angelis4380a332024-02-02 14:21:24 +0000507#endif
Paul Elliott1265f002022-09-09 17:15:43 +0100508};
509
Antonio de Angelis4380a332024-02-02 14:21:24 +0000510#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
511#define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
512#else
Paul Elliottc9774412023-02-06 15:14:07 +0000513#define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
Antonio de Angelis4380a332024-02-02 14:21:24 +0000514#endif
Paul Elliott1265f002022-09-09 17:15:43 +0100515
516static inline struct psa_sign_hash_interruptible_operation_s
517psa_sign_hash_interruptible_operation_init(void)
518{
519 const struct psa_sign_hash_interruptible_operation_s v =
520 PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT;
521
522 return v;
523}
524
525/**
526 * \brief The context for PSA interruptible hash verification.
Paul Elliott1265f002022-09-09 17:15:43 +0100527 */
528struct psa_verify_hash_interruptible_operation_s {
Antonio de Angelis4380a332024-02-02 14:21:24 +0000529#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
530 mbedtls_psa_client_handle_t handle;
531#else
Paul Elliott2d247922022-11-29 14:54:44 +0000532 /** Unique ID indicating which driver got assigned to do the
533 * operation. Since driver contexts are driver-specific, swapping
534 * drivers halfway through the operation is not supported.
535 * ID values are auto-generated in psa_crypto_driver_wrappers.h
536 * ID value zero means the context is not valid or not assigned to
537 * any driver (i.e. none of the driver contexts are active). */
538 unsigned int MBEDTLS_PRIVATE(id);
539
Paul Elliott588f8ed2022-12-02 18:10:26 +0000540 psa_driver_verify_hash_interruptible_context_t MBEDTLS_PRIVATE(ctx);
541
Paul Elliottc9774412023-02-06 15:14:07 +0000542 unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
543
Paul Elliott296ede92022-12-15 17:00:30 +0000544 uint32_t MBEDTLS_PRIVATE(num_ops);
Antonio de Angelis4380a332024-02-02 14:21:24 +0000545#endif
Paul Elliott1265f002022-09-09 17:15:43 +0100546};
547
Antonio de Angelis4380a332024-02-02 14:21:24 +0000548#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
549#define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
550#else
Paul Elliottc9774412023-02-06 15:14:07 +0000551#define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
Antonio de Angelis4380a332024-02-02 14:21:24 +0000552#endif
Paul Elliott1265f002022-09-09 17:15:43 +0100553
554static inline struct psa_verify_hash_interruptible_operation_s
555psa_verify_hash_interruptible_operation_init(void)
556{
557 const struct psa_verify_hash_interruptible_operation_s v =
558 PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT;
559
560 return v;
561}
562
Jaeden Amero8013f442019-08-16 16:13:51 +0100563#ifdef __cplusplus
564}
565#endif
566
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100567#endif /* PSA_CRYPTO_STRUCT_H */