blob: 213625fd6dc3352f1200f577cb07db7153f5f6ca [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 Hesse5b673a82021-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 Rodgman7ff79652023-11-03 12:04:52 +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
51
Jaeden Amero8013f442019-08-16 16:13:51 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskine9ef733f2018-02-07 21:05:37 +010056/* Include the Mbed TLS configuration file, the way Mbed TLS does it
57 * in each of its header files. */
58#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Amerod58a00d2019-06-07 11:49:59 +010059#include "mbedtls/config.h"
Gilles Peskine9ef733f2018-02-07 21:05:37 +010060#else
61#include MBEDTLS_CONFIG_FILE
62#endif
63
Gilles Peskine9ef733f2018-02-07 21:05:37 +010064#include "mbedtls/cmac.h"
65#include "mbedtls/gcm.h"
Steven Cooremandbf8ced2021-03-04 13:01:18 +010066
Steven Cooreman14d09f42021-04-26 12:04:53 +020067/* Include the context definition for the compiled-in drivers for the primitive
68 * algorithms. */
Steven Cooreman040d1ce2021-04-26 11:54:58 +020069#include "psa/crypto_driver_contexts_primitives.h"
Gilles Peskine9ef733f2018-02-07 21:05:37 +010070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071struct psa_hash_operation_s {
Steven Cooremandbf8ced2021-03-04 13:01:18 +010072 /** Unique ID indicating which driver got assigned to do the
73 * operation. Since driver contexts are driver-specific, swapping
74 * drivers halfway through the operation is not supported.
Ronald Cron980230e2021-04-01 15:37:49 +020075 * ID values are auto-generated in psa_driver_wrappers.h.
Steven Cooremandbf8ced2021-03-04 13:01:18 +010076 * ID value zero means the context is not valid or not assigned to
Ronald Cron980230e2021-04-01 15:37:49 +020077 * any driver (i.e. the driver context is not active, in use). */
Steven Cooremandbf8ced2021-03-04 13:01:18 +010078 unsigned int id;
Steven Cooremanb1777312021-03-04 15:22:38 +010079 psa_driver_hash_context_t ctx;
Gilles Peskine9ef733f2018-02-07 21:05:37 +010080};
81
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082#define PSA_HASH_OPERATION_INIT { 0, { 0 } }
83static inline struct psa_hash_operation_s psa_hash_operation_init(void)
Jaeden Amero6a25b412019-01-04 11:47:44 +000084{
85 const struct psa_hash_operation_s v = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 return v;
Jaeden Amero6a25b412019-01-04 11:47:44 +000087}
88
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089struct psa_cipher_operation_s {
Steven Cooreman14d09f42021-04-26 12:04:53 +020090 /** Unique ID indicating which driver got assigned to do the
91 * operation. Since driver contexts are driver-specific, swapping
92 * drivers halfway through the operation is not supported.
93 * ID values are auto-generated in psa_crypto_driver_wrappers.h
94 * ID value zero means the context is not valid or not assigned to
95 * any driver (i.e. none of the driver contexts are active). */
96 unsigned int id;
97
98 unsigned int iv_required : 1;
99 unsigned int iv_set : 1;
100
101 uint8_t default_iv_length;
102
103 psa_driver_cipher_context_t ctx;
104};
105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106#define PSA_CIPHER_OPERATION_INIT { 0, 0, 0, 0, { 0 } }
107static inline struct psa_cipher_operation_s psa_cipher_operation_init(void)
Steven Cooreman14d09f42021-04-26 12:04:53 +0200108{
109 const struct psa_cipher_operation_s v = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110 return v;
Steven Cooreman14d09f42021-04-26 12:04:53 +0200111}
112
Steven Cooremanaecf0d32021-04-26 12:16:27 +0200113/* Include the context definition for the compiled-in drivers for the composite
114 * algorithms. */
115#include "psa/crypto_driver_contexts_composites.h"
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +0300116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100117struct psa_mac_operation_s {
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +0100118 /** Unique ID indicating which driver got assigned to do the
119 * operation. Since driver contexts are driver-specific, swapping
120 * drivers halfway through the operation is not supported.
121 * ID values are auto-generated in psa_driver_wrappers.h
122 * ID value zero means the context is not valid or not assigned to
123 * any driver (i.e. none of the driver contexts are active). */
Steven Cooreman896d51e2021-03-19 15:24:23 +0100124 unsigned int id;
Steven Cooreman15f0d922021-05-07 14:14:37 +0200125 uint8_t mac_size;
126 unsigned int is_sign : 1;
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +0100127 psa_driver_mac_context_t ctx;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100128};
129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130#define PSA_MAC_OPERATION_INIT { 0, 0, 0, { 0 } }
131static inline struct psa_mac_operation_s psa_mac_operation_init(void)
Jaeden Amero769ce272019-01-04 11:48:03 +0000132{
133 const struct psa_mac_operation_s v = PSA_MAC_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134 return v;
Jaeden Amero769ce272019-01-04 11:48:03 +0000135}
136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137struct psa_aead_operation_s {
Gilles Peskine30a9e412019-01-14 18:36:12 +0100138 psa_algorithm_t alg;
139 unsigned int key_set : 1;
140 unsigned int iv_set : 1;
141 uint8_t iv_size;
142 uint8_t block_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 union {
Gilles Peskine30a9e412019-01-14 18:36:12 +0100144 unsigned dummy; /* Enable easier initializing of the union. */
145 mbedtls_cipher_context_t cipher;
146 } ctx;
147};
148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149#define PSA_AEAD_OPERATION_INIT { 0, 0, 0, 0, 0, { 0 } }
150static inline struct psa_aead_operation_s psa_aead_operation_init(void)
Gilles Peskine30a9e412019-01-14 18:36:12 +0100151{
152 const struct psa_aead_operation_s v = PSA_AEAD_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153 return v;
Gilles Peskine30a9e412019-01-14 18:36:12 +0100154}
155
Steven Cooremanc2cbac02021-04-29 18:01:53 +0200156#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157typedef struct {
Gilles Peskinebef7f142018-07-12 17:22:21 +0200158 uint8_t *info;
159 size_t info_length;
Gilles Peskinebef7f142018-07-12 17:22:21 +0200160#if PSA_HASH_MAX_SIZE > 0xff
161#error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
162#endif
163 uint8_t offset_in_block;
164 uint8_t block_number;
Gilles Peskine22c51512019-04-12 15:07:32 +0200165 unsigned int state : 2;
166 unsigned int info_set : 1;
Gilles Peskinef5d7eef2021-11-08 22:12:47 +0100167 uint8_t output_block[PSA_HASH_MAX_SIZE];
168 uint8_t prk[PSA_HASH_MAX_SIZE];
Gilles Peskine9501bd72021-11-25 20:30:47 +0100169 struct psa_mac_operation_s hmac;
Gilles Peskinecbe66502019-05-16 16:59:18 +0200170} psa_hkdf_key_derivation_t;
Steven Cooremanc2cbac02021-04-29 18:01:53 +0200171#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Gilles Peskinebef7f142018-07-12 17:22:21 +0200172
Steven Cooremanc2cbac02021-04-29 18:01:53 +0200173#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
174 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175typedef enum {
Gilles Peskine43f958b2020-12-13 14:55:14 +0100176 PSA_TLS12_PRF_STATE_INIT, /* no input provided */
177 PSA_TLS12_PRF_STATE_SEED_SET, /* seed has been set */
178 PSA_TLS12_PRF_STATE_KEY_SET, /* key has been set */
179 PSA_TLS12_PRF_STATE_LABEL_SET, /* label has been set */
180 PSA_TLS12_PRF_STATE_OUTPUT /* output has been started */
Janos Follath999f6482019-06-11 12:04:10 +0100181} psa_tls12_prf_key_derivation_state_t;
182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100183typedef struct psa_tls12_prf_key_derivation_s {
Janos Follath999f6482019-06-11 12:04:10 +0100184#if PSA_HASH_MAX_SIZE > 0xff
185#error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
186#endif
187
188 /* Indicates how many bytes in the current HMAC block have
Janos Follath844eb0e2019-06-19 12:10:49 +0100189 * not yet been read by the user. */
190 uint8_t left_in_block;
Janos Follath999f6482019-06-11 12:04:10 +0100191
192 /* The 1-based number of the block. */
193 uint8_t block_number;
194
195 psa_tls12_prf_key_derivation_state_t state;
196
Steven Cooreman22dea1d2021-04-29 19:32:25 +0200197 uint8_t *secret;
198 size_t secret_length;
Janos Follath999f6482019-06-11 12:04:10 +0100199 uint8_t *seed;
200 size_t seed_length;
201 uint8_t *label;
202 size_t label_length;
Steven Cooreman22dea1d2021-04-29 19:32:25 +0200203
Janos Follath999f6482019-06-11 12:04:10 +0100204 uint8_t Ai[PSA_HASH_MAX_SIZE];
205
206 /* `HMAC_hash( prk, A(i) + seed )` in the notation of RFC 5246, Sect. 5. */
207 uint8_t output_block[PSA_HASH_MAX_SIZE];
208} psa_tls12_prf_key_derivation_t;
Steven Cooremanc2cbac02021-04-29 18:01:53 +0200209#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
210 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Hanno Beckerc8a41d72018-10-09 17:33:01 +0100211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100212struct psa_key_derivation_s {
Gilles Peskineeab56e42018-07-12 17:12:33 +0200213 psa_algorithm_t alg;
Gilles Peskine178c9aa2019-09-24 18:21:06 +0200214 unsigned int can_output_key : 1;
Gilles Peskineeab56e42018-07-12 17:12:33 +0200215 size_t capacity;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 union {
Janos Follathadbec812019-06-14 11:05:39 +0100217 /* Make the union non-empty even with no supported algorithms. */
218 uint8_t dummy;
Steven Cooremanc2cbac02021-04-29 18:01:53 +0200219#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +0200220 psa_hkdf_key_derivation_t hkdf;
Steven Cooremanc2cbac02021-04-29 18:01:53 +0200221#endif
222#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Gilles Peskinecbe66502019-05-16 16:59:18 +0200224 psa_tls12_prf_key_derivation_t tls12_prf;
Gilles Peskinebef7f142018-07-12 17:22:21 +0200225#endif
Gilles Peskineeab56e42018-07-12 17:12:33 +0200226 } ctx;
227};
228
Janos Follathadbec812019-06-14 11:05:39 +0100229/* This only zeroes out the first byte in the union, the rest is unspecified. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230#define PSA_KEY_DERIVATION_OPERATION_INIT { 0, 0, 0, { 0 } }
231static inline struct psa_key_derivation_s psa_key_derivation_operation_init(void)
Gilles Peskineeab56e42018-07-12 17:12:33 +0200232{
Gilles Peskinecbe66502019-05-16 16:59:18 +0200233 const struct psa_key_derivation_s v = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 return v;
Gilles Peskineeab56e42018-07-12 17:12:33 +0200235}
236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237struct psa_key_policy_s {
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100238 psa_key_usage_t usage;
239 psa_algorithm_t alg;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200240 psa_algorithm_t alg2;
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100241};
Gilles Peskinea3dd7372019-04-19 19:42:26 +0200242typedef struct psa_key_policy_s psa_key_policy_t;
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244#define PSA_KEY_POLICY_INIT { 0, 0, 0 }
245static inline struct psa_key_policy_s psa_key_policy_init(void)
Jaeden Amero70261c52019-01-04 11:47:20 +0000246{
247 const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 return v;
Jaeden Amero70261c52019-01-04 11:47:20 +0000249}
250
Gilles Peskine68cc433b2019-07-30 17:42:47 +0200251/* The type used internally for key sizes.
252 * Public interfaces use size_t, but internally we use a smaller type. */
253typedef uint16_t psa_key_bits_t;
254/* The maximum value of the type used to represent bit-sizes.
255 * This is used to mark an invalid key size. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256#define PSA_KEY_BITS_TOO_LARGE ((psa_key_bits_t) (-1))
Gilles Peskinec744d992019-07-30 17:26:54 +0200257/* The maximum size of a key in bits.
Gilles Peskine68cc433b2019-07-30 17:42:47 +0200258 * Currently defined as the maximum that can be represented, rounded down
259 * to a whole number of bytes.
260 * This is an uncast value so that it can be used in preprocessor
261 * conditionals. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200262#define PSA_MAX_KEY_BITS 0xfff8
263
Gilles Peskine91e8c332019-08-02 19:19:39 +0200264/** A mask of flags that can be stored in key attributes.
265 *
266 * This type is also used internally to store flags in slots. Internal
267 * flags are defined in library/psa_crypto_core.h. Internal flags may have
268 * the same value as external flags if they are properly handled during
269 * key creation and in psa_get_key_attributes.
270 */
271typedef uint16_t psa_key_attributes_flag_t;
272
Gilles Peskinec8000c02019-08-02 20:15:51 +0200273#define MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 ((psa_key_attributes_flag_t) 0x0001)
Gilles Peskine91e8c332019-08-02 19:19:39 +0200275
276/* A mask of key attribute flags used externally only.
277 * Only meant for internal checks inside the library. */
278#define MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ( \
Gilles Peskinec8000c02019-08-02 20:15:51 +0200279 MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER | \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 0)
Gilles Peskine91e8c332019-08-02 19:19:39 +0200281
282/* A mask of key attribute flags used both internally and externally.
283 * Currently there aren't any. */
284#define MBEDTLS_PSA_KA_MASK_DUAL_USE ( \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285 0)
Gilles Peskine91e8c332019-08-02 19:19:39 +0200286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287typedef struct {
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200288 psa_key_type_t type;
Gilles Peskinef65ed6f2019-12-04 17:18:41 +0100289 psa_key_bits_t bits;
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200290 psa_key_lifetime_t lifetime;
Ronald Cron71016a92020-08-28 19:01:50 +0200291 mbedtls_svc_key_id_t id;
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200292 psa_key_policy_t policy;
Gilles Peskine91e8c332019-08-02 19:19:39 +0200293 psa_key_attributes_flag_t flags;
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200294} psa_core_key_attributes_t;
295
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100296#define PSA_CORE_KEY_ATTRIBUTES_INIT { PSA_KEY_TYPE_NONE, 0, PSA_KEY_LIFETIME_VOLATILE, \
297 MBEDTLS_SVC_KEY_ID_INIT, PSA_KEY_POLICY_INIT, 0 }
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299struct psa_key_attributes_s {
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200300 psa_core_key_attributes_t core;
Gilles Peskinec8000c02019-08-02 20:15:51 +0200301#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
302 psa_key_slot_number_t slot_number;
303#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +0200304 void *domain_parameters;
305 size_t domain_parameters_size;
Gilles Peskine4747d192019-04-17 15:05:45 +0200306};
307
Gilles Peskinec8000c02019-08-02 20:15:51 +0200308#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100309#define PSA_KEY_ATTRIBUTES_INIT { PSA_CORE_KEY_ATTRIBUTES_INIT, 0, NULL, 0 }
Gilles Peskinec8000c02019-08-02 20:15:51 +0200310#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311#define PSA_KEY_ATTRIBUTES_INIT { PSA_CORE_KEY_ATTRIBUTES_INIT, NULL, 0 }
Gilles Peskinec8000c02019-08-02 20:15:51 +0200312#endif
313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314static inline struct psa_key_attributes_s psa_key_attributes_init(void)
Gilles Peskine4747d192019-04-17 15:05:45 +0200315{
316 const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100317 return v;
Gilles Peskine4747d192019-04-17 15:05:45 +0200318}
319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320static inline void psa_set_key_id(psa_key_attributes_t *attributes,
321 mbedtls_svc_key_id_t key)
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200322{
Ronald Crond98059d2020-10-23 18:00:55 +0200323 psa_key_lifetime_t lifetime = attributes->core.lifetime;
324
Ronald Cron27238fc2020-07-23 12:30:41 +0200325 attributes->core.id = key;
Ronald Crond98059d2020-10-23 18:00:55 +0200326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
Ronald Crond98059d2020-10-23 18:00:55 +0200328 attributes->core.lifetime =
329 PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
330 PSA_KEY_LIFETIME_PERSISTENT,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331 PSA_KEY_LIFETIME_GET_LOCATION(lifetime));
Ronald Crond98059d2020-10-23 18:00:55 +0200332 }
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200333}
334
Ronald Cron71016a92020-08-28 19:01:50 +0200335static inline mbedtls_svc_key_id_t psa_get_key_id(
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200336 const psa_key_attributes_t *attributes)
337{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100338 return attributes->core.id;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200339}
340
Ronald Cron6b5ff532020-10-16 14:38:19 +0200341#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100342static inline void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes,
343 mbedtls_key_owner_id_t owner)
Ronald Cron6b5ff532020-10-16 14:38:19 +0200344{
345 attributes->core.id.owner = owner;
346}
347#endif
348
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200349static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
350 psa_key_lifetime_t lifetime)
351{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200352 attributes->core.lifetime = lifetime;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
Ronald Cron71016a92020-08-28 19:01:50 +0200354#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
Jaeden Ameroe3cdf282019-08-20 12:58:20 +0100355 attributes->core.id.key_id = 0;
Jaeden Ameroe3cdf282019-08-20 12:58:20 +0100356#else
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200357 attributes->core.id = 0;
Jaeden Ameroe3cdf282019-08-20 12:58:20 +0100358#endif
359 }
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200360}
361
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200362static inline psa_key_lifetime_t psa_get_key_lifetime(
363 const psa_key_attributes_t *attributes)
364{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100365 return attributes->core.lifetime;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200366}
367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368static inline void psa_extend_key_usage_flags(psa_key_usage_t *usage_flags)
gabor-mezei-arm6439e852021-06-23 16:48:08 +0200369{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370 if (*usage_flags & PSA_KEY_USAGE_SIGN_HASH) {
gabor-mezei-arm6439e852021-06-23 16:48:08 +0200371 *usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 }
gabor-mezei-arm6439e852021-06-23 16:48:08 +0200373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 if (*usage_flags & PSA_KEY_USAGE_VERIFY_HASH) {
gabor-mezei-arm6439e852021-06-23 16:48:08 +0200375 *usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 }
gabor-mezei-arm6439e852021-06-23 16:48:08 +0200377}
378
Gilles Peskine4747d192019-04-17 15:05:45 +0200379static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
380 psa_key_usage_t usage_flags)
381{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382 psa_extend_key_usage_flags(&usage_flags);
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200383 attributes->core.policy.usage = usage_flags;
Gilles Peskine4747d192019-04-17 15:05:45 +0200384}
385
386static inline psa_key_usage_t psa_get_key_usage_flags(
387 const psa_key_attributes_t *attributes)
388{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389 return attributes->core.policy.usage;
Gilles Peskine4747d192019-04-17 15:05:45 +0200390}
391
392static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
393 psa_algorithm_t alg)
394{
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200395 attributes->core.policy.alg = alg;
Gilles Peskine4747d192019-04-17 15:05:45 +0200396}
397
398static inline psa_algorithm_t psa_get_key_algorithm(
399 const psa_key_attributes_t *attributes)
400{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100401 return attributes->core.policy.alg;
Gilles Peskine4747d192019-04-17 15:05:45 +0200402}
403
Gilles Peskine24f10f82019-05-16 12:18:32 +0200404/* This function is declared in crypto_extra.h, which comes after this
405 * header file, but we need the function here, so repeat the declaration. */
406psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
407 psa_key_type_t type,
408 const uint8_t *data,
409 size_t data_length);
410
Gilles Peskine4747d192019-04-17 15:05:45 +0200411static inline void psa_set_key_type(psa_key_attributes_t *attributes,
412 psa_key_type_t type)
413{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414 if (attributes->domain_parameters == NULL) {
Gilles Peskineb699f072019-04-26 16:06:02 +0200415 /* Common case: quick path */
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200416 attributes->core.type = type;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417 } else {
Shaun Case0e7791f2021-12-20 21:14:10 -0800418 /* Call the bigger function to free the old domain parameters.
Gilles Peskineb699f072019-04-26 16:06:02 +0200419 * Ignore any errors which may arise due to type requiring
420 * non-default domain parameters, since this function can't
421 * report errors. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422 (void) psa_set_key_domain_parameters(attributes, type, NULL, 0);
Gilles Peskineb699f072019-04-26 16:06:02 +0200423 }
Gilles Peskine4747d192019-04-17 15:05:45 +0200424}
425
426static inline psa_key_type_t psa_get_key_type(
427 const psa_key_attributes_t *attributes)
428{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100429 return attributes->core.type;
Gilles Peskine4747d192019-04-17 15:05:45 +0200430}
431
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200432static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
433 size_t bits)
434{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435 if (bits > PSA_MAX_KEY_BITS) {
Gilles Peskine68cc433b2019-07-30 17:42:47 +0200436 attributes->core.bits = PSA_KEY_BITS_TOO_LARGE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100437 } else {
Gilles Peskine68cc433b2019-07-30 17:42:47 +0200438 attributes->core.bits = (psa_key_bits_t) bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100439 }
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200440}
441
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200442static inline size_t psa_get_key_bits(
443 const psa_key_attributes_t *attributes)
444{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445 return attributes->core.bits;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +0200446}
447
Jaeden Amero8013f442019-08-16 16:13:51 +0100448#ifdef __cplusplus
449}
450#endif
451
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100452#endif /* PSA_CRYPTO_STRUCT_H */