xref: /optee_os/lib/libmbedtls/mbedtls/include/psa/crypto_struct.h (revision cb03400251f98aed22a2664509e3ed9e183800b0)
132b31808SJens Wiklander /**
232b31808SJens Wiklander  * \file psa/crypto_struct.h
332b31808SJens Wiklander  *
432b31808SJens Wiklander  * \brief PSA cryptography module: Mbed TLS structured type implementations
532b31808SJens Wiklander  *
632b31808SJens Wiklander  * \note This file may not be included directly. Applications must
732b31808SJens Wiklander  * include psa/crypto.h.
832b31808SJens Wiklander  *
932b31808SJens Wiklander  * This file contains the definitions of some data structures with
1032b31808SJens Wiklander  * implementation-specific definitions.
1132b31808SJens Wiklander  *
1232b31808SJens Wiklander  * In implementations with isolation between the application and the
1332b31808SJens Wiklander  * cryptography module, it is expected that the front-end and the back-end
1432b31808SJens Wiklander  * would have different versions of this file.
1532b31808SJens Wiklander  *
1632b31808SJens Wiklander  * <h3>Design notes about multipart operation structures</h3>
1732b31808SJens Wiklander  *
1832b31808SJens Wiklander  * For multipart operations without driver delegation support, each multipart
1932b31808SJens Wiklander  * operation structure contains a `psa_algorithm_t alg` field which indicates
2032b31808SJens Wiklander  * which specific algorithm the structure is for. When the structure is not in
2132b31808SJens Wiklander  * use, `alg` is 0. Most of the structure consists of a union which is
2232b31808SJens Wiklander  * discriminated by `alg`.
2332b31808SJens Wiklander  *
2432b31808SJens Wiklander  * For multipart operations with driver delegation support, each multipart
2532b31808SJens Wiklander  * operation structure contains an `unsigned int id` field indicating which
2632b31808SJens Wiklander  * driver got assigned to do the operation. When the structure is not in use,
2732b31808SJens Wiklander  * 'id' is 0. The structure contains also a driver context which is the union
2832b31808SJens Wiklander  * of the contexts of all drivers able to handle the type of multipart
2932b31808SJens Wiklander  * operation.
3032b31808SJens Wiklander  *
3132b31808SJens Wiklander  * Note that when `alg` or `id` is 0, the content of other fields is undefined.
3232b31808SJens Wiklander  * In particular, it is not guaranteed that a freshly-initialized structure
3332b31808SJens Wiklander  * is all-zero: we initialize structures to something like `{0, 0}`, which
3432b31808SJens Wiklander  * is only guaranteed to initializes the first member of the union;
3532b31808SJens Wiklander  * GCC and Clang initialize the whole structure to 0 (at the time of writing),
3632b31808SJens Wiklander  * but MSVC and CompCert don't.
3732b31808SJens Wiklander  *
38b0563631STom Van Eyck  * In Mbed TLS, multipart operation structures live independently from
39b0563631STom Van Eyck  * the key. This allows Mbed TLS to free the key objects when destroying
4032b31808SJens Wiklander  * a key slot. If a multipart operation needs to remember the key after
4132b31808SJens Wiklander  * the setup function returns, the operation structure needs to contain a
4232b31808SJens Wiklander  * copy of the key.
4332b31808SJens Wiklander  */
4432b31808SJens Wiklander /*
4532b31808SJens Wiklander  *  Copyright The Mbed TLS Contributors
46b0563631STom Van Eyck  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
4732b31808SJens Wiklander  */
4832b31808SJens Wiklander 
4932b31808SJens Wiklander #ifndef PSA_CRYPTO_STRUCT_H
5032b31808SJens Wiklander #define PSA_CRYPTO_STRUCT_H
5132b31808SJens Wiklander #include "mbedtls/private_access.h"
5232b31808SJens Wiklander 
5332b31808SJens Wiklander #ifdef __cplusplus
5432b31808SJens Wiklander extern "C" {
5532b31808SJens Wiklander #endif
5632b31808SJens Wiklander 
57b0563631STom Van Eyck /*
58b0563631STom Van Eyck  * Include the build-time configuration information header. Here, we do not
59b0563631STom Van Eyck  * include `"mbedtls/build_info.h"` directly but `"psa/build_info.h"`, which
60b0563631STom Van Eyck  * is basically just an alias to it. This is to ease the maintenance of the
61b0563631STom Van Eyck  * TF-PSA-Crypto repository which has a different build system and
62b0563631STom Van Eyck  * configuration.
63b0563631STom Van Eyck  */
64b0563631STom Van Eyck #include "psa/build_info.h"
6532b31808SJens Wiklander 
6632b31808SJens Wiklander /* Include the context definition for the compiled-in drivers for the primitive
6732b31808SJens Wiklander  * algorithms. */
6832b31808SJens Wiklander #include "psa/crypto_driver_contexts_primitives.h"
6932b31808SJens Wiklander 
7032b31808SJens Wiklander struct psa_hash_operation_s {
71b0563631STom Van Eyck #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
72b0563631STom Van Eyck     mbedtls_psa_client_handle_t handle;
73b0563631STom Van Eyck #else
7432b31808SJens Wiklander     /** Unique ID indicating which driver got assigned to do the
7532b31808SJens Wiklander      * operation. Since driver contexts are driver-specific, swapping
7632b31808SJens Wiklander      * drivers halfway through the operation is not supported.
7732b31808SJens Wiklander      * ID values are auto-generated in psa_driver_wrappers.h.
7832b31808SJens Wiklander      * ID value zero means the context is not valid or not assigned to
7932b31808SJens Wiklander      * any driver (i.e. the driver context is not active, in use). */
8032b31808SJens Wiklander     unsigned int MBEDTLS_PRIVATE(id);
8132b31808SJens Wiklander     psa_driver_hash_context_t MBEDTLS_PRIVATE(ctx);
82b0563631STom Van Eyck #endif
8332b31808SJens Wiklander };
84b0563631STom Van Eyck #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
85b0563631STom Van Eyck #define PSA_HASH_OPERATION_INIT { 0 }
86b0563631STom Van Eyck #else
8732b31808SJens Wiklander #define PSA_HASH_OPERATION_INIT { 0, { 0 } }
88b0563631STom Van Eyck #endif
psa_hash_operation_init(void)8932b31808SJens Wiklander static inline struct psa_hash_operation_s psa_hash_operation_init(void)
9032b31808SJens Wiklander {
9132b31808SJens Wiklander     const struct psa_hash_operation_s v = PSA_HASH_OPERATION_INIT;
9232b31808SJens Wiklander     return v;
9332b31808SJens Wiklander }
9432b31808SJens Wiklander 
9532b31808SJens Wiklander struct psa_cipher_operation_s {
96b0563631STom Van Eyck #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
97b0563631STom Van Eyck     mbedtls_psa_client_handle_t handle;
98b0563631STom Van Eyck #else
9932b31808SJens Wiklander     /** Unique ID indicating which driver got assigned to do the
10032b31808SJens Wiklander      * operation. Since driver contexts are driver-specific, swapping
10132b31808SJens Wiklander      * drivers halfway through the operation is not supported.
10232b31808SJens Wiklander      * ID values are auto-generated in psa_crypto_driver_wrappers.h
10332b31808SJens Wiklander      * ID value zero means the context is not valid or not assigned to
10432b31808SJens Wiklander      * any driver (i.e. none of the driver contexts are active). */
10532b31808SJens Wiklander     unsigned int MBEDTLS_PRIVATE(id);
10632b31808SJens Wiklander 
10732b31808SJens Wiklander     unsigned int MBEDTLS_PRIVATE(iv_required) : 1;
10832b31808SJens Wiklander     unsigned int MBEDTLS_PRIVATE(iv_set) : 1;
10932b31808SJens Wiklander 
11032b31808SJens Wiklander     uint8_t MBEDTLS_PRIVATE(default_iv_length);
11132b31808SJens Wiklander 
11232b31808SJens Wiklander     psa_driver_cipher_context_t MBEDTLS_PRIVATE(ctx);
113b0563631STom Van Eyck #endif
11432b31808SJens Wiklander };
11532b31808SJens Wiklander 
116b0563631STom Van Eyck #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
117b0563631STom Van Eyck #define PSA_CIPHER_OPERATION_INIT { 0 }
118b0563631STom Van Eyck #else
11932b31808SJens Wiklander #define PSA_CIPHER_OPERATION_INIT { 0, 0, 0, 0, { 0 } }
120b0563631STom Van Eyck #endif
psa_cipher_operation_init(void)12132b31808SJens Wiklander static inline struct psa_cipher_operation_s psa_cipher_operation_init(void)
12232b31808SJens Wiklander {
12332b31808SJens Wiklander     const struct psa_cipher_operation_s v = PSA_CIPHER_OPERATION_INIT;
12432b31808SJens Wiklander     return v;
12532b31808SJens Wiklander }
12632b31808SJens Wiklander 
12732b31808SJens Wiklander /* Include the context definition for the compiled-in drivers for the composite
12832b31808SJens Wiklander  * algorithms. */
12932b31808SJens Wiklander #include "psa/crypto_driver_contexts_composites.h"
13032b31808SJens Wiklander 
13132b31808SJens Wiklander struct psa_mac_operation_s {
132b0563631STom Van Eyck #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
133b0563631STom Van Eyck     mbedtls_psa_client_handle_t handle;
134b0563631STom Van Eyck #else
13532b31808SJens Wiklander     /** Unique ID indicating which driver got assigned to do the
13632b31808SJens Wiklander      * operation. Since driver contexts are driver-specific, swapping
13732b31808SJens Wiklander      * drivers halfway through the operation is not supported.
13832b31808SJens Wiklander      * ID values are auto-generated in psa_driver_wrappers.h
13932b31808SJens Wiklander      * ID value zero means the context is not valid or not assigned to
14032b31808SJens Wiklander      * any driver (i.e. none of the driver contexts are active). */
14132b31808SJens Wiklander     unsigned int MBEDTLS_PRIVATE(id);
14232b31808SJens Wiklander     uint8_t MBEDTLS_PRIVATE(mac_size);
14332b31808SJens Wiklander     unsigned int MBEDTLS_PRIVATE(is_sign) : 1;
14432b31808SJens Wiklander     psa_driver_mac_context_t MBEDTLS_PRIVATE(ctx);
145b0563631STom Van Eyck #endif
14632b31808SJens Wiklander };
14732b31808SJens Wiklander 
148b0563631STom Van Eyck #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
149b0563631STom Van Eyck #define PSA_MAC_OPERATION_INIT { 0 }
150b0563631STom Van Eyck #else
15132b31808SJens Wiklander #define PSA_MAC_OPERATION_INIT { 0, 0, 0, { 0 } }
152b0563631STom Van Eyck #endif
psa_mac_operation_init(void)15332b31808SJens Wiklander static inline struct psa_mac_operation_s psa_mac_operation_init(void)
15432b31808SJens Wiklander {
15532b31808SJens Wiklander     const struct psa_mac_operation_s v = PSA_MAC_OPERATION_INIT;
15632b31808SJens Wiklander     return v;
15732b31808SJens Wiklander }
15832b31808SJens Wiklander 
15932b31808SJens Wiklander struct psa_aead_operation_s {
160b0563631STom Van Eyck #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
161b0563631STom Van Eyck     mbedtls_psa_client_handle_t handle;
162b0563631STom Van Eyck #else
16332b31808SJens Wiklander     /** Unique ID indicating which driver got assigned to do the
16432b31808SJens Wiklander      * operation. Since driver contexts are driver-specific, swapping
16532b31808SJens Wiklander      * drivers halfway through the operation is not supported.
16632b31808SJens Wiklander      * ID values are auto-generated in psa_crypto_driver_wrappers.h
16732b31808SJens Wiklander      * ID value zero means the context is not valid or not assigned to
16832b31808SJens Wiklander      * any driver (i.e. none of the driver contexts are active). */
16932b31808SJens Wiklander     unsigned int MBEDTLS_PRIVATE(id);
17032b31808SJens Wiklander 
17132b31808SJens Wiklander     psa_algorithm_t MBEDTLS_PRIVATE(alg);
17232b31808SJens Wiklander     psa_key_type_t MBEDTLS_PRIVATE(key_type);
17332b31808SJens Wiklander 
17432b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(ad_remaining);
17532b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(body_remaining);
17632b31808SJens Wiklander 
17732b31808SJens Wiklander     unsigned int MBEDTLS_PRIVATE(nonce_set) : 1;
17832b31808SJens Wiklander     unsigned int MBEDTLS_PRIVATE(lengths_set) : 1;
17932b31808SJens Wiklander     unsigned int MBEDTLS_PRIVATE(ad_started) : 1;
18032b31808SJens Wiklander     unsigned int MBEDTLS_PRIVATE(body_started) : 1;
18132b31808SJens Wiklander     unsigned int MBEDTLS_PRIVATE(is_encrypt) : 1;
18232b31808SJens Wiklander 
18332b31808SJens Wiklander     psa_driver_aead_context_t MBEDTLS_PRIVATE(ctx);
184b0563631STom Van Eyck #endif
18532b31808SJens Wiklander };
18632b31808SJens Wiklander 
187b0563631STom Van Eyck #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
188b0563631STom Van Eyck #define PSA_AEAD_OPERATION_INIT { 0 }
189b0563631STom Van Eyck #else
19032b31808SJens Wiklander #define PSA_AEAD_OPERATION_INIT { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0 } }
191b0563631STom Van Eyck #endif
psa_aead_operation_init(void)19232b31808SJens Wiklander static inline struct psa_aead_operation_s psa_aead_operation_init(void)
19332b31808SJens Wiklander {
19432b31808SJens Wiklander     const struct psa_aead_operation_s v = PSA_AEAD_OPERATION_INIT;
19532b31808SJens Wiklander     return v;
19632b31808SJens Wiklander }
19732b31808SJens Wiklander 
198b0563631STom Van Eyck /* Include the context definition for the compiled-in drivers for the key
199b0563631STom Van Eyck  * derivation algorithms. */
200b0563631STom Van Eyck #include "psa/crypto_driver_contexts_key_derivation.h"
20132b31808SJens Wiklander 
20232b31808SJens Wiklander struct psa_key_derivation_s {
203b0563631STom Van Eyck #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
204b0563631STom Van Eyck     mbedtls_psa_client_handle_t handle;
205b0563631STom Van Eyck #else
20632b31808SJens Wiklander     psa_algorithm_t MBEDTLS_PRIVATE(alg);
20732b31808SJens Wiklander     unsigned int MBEDTLS_PRIVATE(can_output_key) : 1;
20832b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(capacity);
209b0563631STom Van Eyck     psa_driver_key_derivation_context_t MBEDTLS_PRIVATE(ctx);
21032b31808SJens Wiklander #endif
21132b31808SJens Wiklander };
21232b31808SJens Wiklander 
213b0563631STom Van Eyck #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
214b0563631STom Van Eyck #define PSA_KEY_DERIVATION_OPERATION_INIT { 0 }
215b0563631STom Van Eyck #else
21632b31808SJens Wiklander /* This only zeroes out the first byte in the union, the rest is unspecified. */
21732b31808SJens Wiklander #define PSA_KEY_DERIVATION_OPERATION_INIT { 0, 0, 0, { 0 } }
218b0563631STom Van Eyck #endif
psa_key_derivation_operation_init(void)21932b31808SJens Wiklander static inline struct psa_key_derivation_s psa_key_derivation_operation_init(
22032b31808SJens Wiklander     void)
22132b31808SJens Wiklander {
22232b31808SJens Wiklander     const struct psa_key_derivation_s v = PSA_KEY_DERIVATION_OPERATION_INIT;
22332b31808SJens Wiklander     return v;
22432b31808SJens Wiklander }
22532b31808SJens Wiklander 
226*cb034002SJerome Forissier struct psa_custom_key_parameters_s {
227b0563631STom Van Eyck     /* Future versions may add other fields in this structure. */
228b0563631STom Van Eyck     uint32_t flags;
229*cb034002SJerome Forissier };
230*cb034002SJerome Forissier 
231*cb034002SJerome Forissier /** The default production parameters for key generation or key derivation.
232*cb034002SJerome Forissier  *
233*cb034002SJerome Forissier  * Calling psa_generate_key_custom() or psa_key_derivation_output_key_custom()
234*cb034002SJerome Forissier  * with `custom=PSA_CUSTOM_KEY_PARAMETERS_INIT` and `custom_data_length=0` is
235*cb034002SJerome Forissier  * equivalent to calling psa_generate_key() or psa_key_derivation_output_key()
236*cb034002SJerome Forissier  * respectively.
237*cb034002SJerome Forissier  */
238*cb034002SJerome Forissier #define PSA_CUSTOM_KEY_PARAMETERS_INIT { 0 }
239*cb034002SJerome Forissier 
240*cb034002SJerome Forissier #ifndef __cplusplus
241*cb034002SJerome Forissier /* Omitted when compiling in C++, because one of the parameters is a
242*cb034002SJerome Forissier  * pointer to a struct with a flexible array member, and that is not
243*cb034002SJerome Forissier  * standard C++.
244*cb034002SJerome Forissier  * https://github.com/Mbed-TLS/mbedtls/issues/9020
245*cb034002SJerome Forissier  */
246*cb034002SJerome Forissier /* This is a deprecated variant of `struct psa_custom_key_parameters_s`.
247*cb034002SJerome Forissier  * It has exactly the same layout, plus an extra field which is a flexible
248*cb034002SJerome Forissier  * array member. Thus a `const struct psa_key_production_parameters_s *`
249*cb034002SJerome Forissier  * can be passed to any function that reads a
250*cb034002SJerome Forissier  * `const struct psa_custom_key_parameters_s *`.
251*cb034002SJerome Forissier  */
252*cb034002SJerome Forissier struct psa_key_production_parameters_s {
253*cb034002SJerome Forissier     uint32_t flags;
254b0563631STom Van Eyck     uint8_t data[];
255b0563631STom Van Eyck };
256b0563631STom Van Eyck 
257b0563631STom Van Eyck /** The default production parameters for key generation or key derivation.
258b0563631STom Van Eyck  *
259b0563631STom Van Eyck  * Calling psa_generate_key_ext() or psa_key_derivation_output_key_ext()
260b0563631STom Van Eyck  * with `params=PSA_KEY_PRODUCTION_PARAMETERS_INIT` and
261b0563631STom Van Eyck  * `params_data_length == 0` is equivalent to
262b0563631STom Van Eyck  * calling psa_generate_key() or psa_key_derivation_output_key()
263b0563631STom Van Eyck  * respectively.
264b0563631STom Van Eyck  */
265b0563631STom Van Eyck #define PSA_KEY_PRODUCTION_PARAMETERS_INIT { 0 }
266*cb034002SJerome Forissier #endif /* !__cplusplus */
267b0563631STom Van Eyck 
26832b31808SJens Wiklander struct psa_key_policy_s {
26932b31808SJens Wiklander     psa_key_usage_t MBEDTLS_PRIVATE(usage);
27032b31808SJens Wiklander     psa_algorithm_t MBEDTLS_PRIVATE(alg);
27132b31808SJens Wiklander     psa_algorithm_t MBEDTLS_PRIVATE(alg2);
27232b31808SJens Wiklander };
27332b31808SJens Wiklander typedef struct psa_key_policy_s psa_key_policy_t;
27432b31808SJens Wiklander 
27532b31808SJens Wiklander #define PSA_KEY_POLICY_INIT { 0, 0, 0 }
psa_key_policy_init(void)27632b31808SJens Wiklander static inline struct psa_key_policy_s psa_key_policy_init(void)
27732b31808SJens Wiklander {
27832b31808SJens Wiklander     const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT;
27932b31808SJens Wiklander     return v;
28032b31808SJens Wiklander }
28132b31808SJens Wiklander 
28232b31808SJens Wiklander /* The type used internally for key sizes.
28332b31808SJens Wiklander  * Public interfaces use size_t, but internally we use a smaller type. */
28432b31808SJens Wiklander typedef uint16_t psa_key_bits_t;
28532b31808SJens Wiklander /* The maximum value of the type used to represent bit-sizes.
28632b31808SJens Wiklander  * This is used to mark an invalid key size. */
28732b31808SJens Wiklander #define PSA_KEY_BITS_TOO_LARGE          ((psa_key_bits_t) -1)
28832b31808SJens Wiklander /* The maximum size of a key in bits.
28932b31808SJens Wiklander  * Currently defined as the maximum that can be represented, rounded down
29032b31808SJens Wiklander  * to a whole number of bytes.
29132b31808SJens Wiklander  * This is an uncast value so that it can be used in preprocessor
29232b31808SJens Wiklander  * conditionals. */
29332b31808SJens Wiklander #define PSA_MAX_KEY_BITS 0xfff8
29432b31808SJens Wiklander 
295b0563631STom Van Eyck struct psa_key_attributes_s {
296b0563631STom Van Eyck #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
297b0563631STom Van Eyck     psa_key_slot_number_t MBEDTLS_PRIVATE(slot_number);
298b0563631STom Van Eyck     int MBEDTLS_PRIVATE(has_slot_number);
299b0563631STom Van Eyck #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
30032b31808SJens Wiklander     psa_key_type_t MBEDTLS_PRIVATE(type);
30132b31808SJens Wiklander     psa_key_bits_t MBEDTLS_PRIVATE(bits);
30232b31808SJens Wiklander     psa_key_lifetime_t MBEDTLS_PRIVATE(lifetime);
30332b31808SJens Wiklander     psa_key_policy_t MBEDTLS_PRIVATE(policy);
304b0563631STom Van Eyck     /* This type has a different layout in the client view wrt the
305b0563631STom Van Eyck      * service view of the key id, i.e. in service view usually is
306b0563631STom Van Eyck      * expected to have MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined
307b0563631STom Van Eyck      * thus adding an owner field to the standard psa_key_id_t. For
308b0563631STom Van Eyck      * implementations with client/service separation, this means the
309b0563631STom Van Eyck      * object will be marshalled through a transport channel and
310b0563631STom Van Eyck      * interpreted differently at each side of the transport. Placing
311b0563631STom Van Eyck      * it at the end of structures allows to interpret the structure
312b0563631STom Van Eyck      * at the client without reorganizing the memory layout of the
313b0563631STom Van Eyck      * struct
314b0563631STom Van Eyck      */
315b0563631STom Van Eyck     mbedtls_svc_key_id_t MBEDTLS_PRIVATE(id);
31632b31808SJens Wiklander };
31732b31808SJens Wiklander 
31832b31808SJens Wiklander #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
319b0563631STom Van Eyck #define PSA_KEY_ATTRIBUTES_MAYBE_SLOT_NUMBER 0, 0,
32032b31808SJens Wiklander #else
321b0563631STom Van Eyck #define PSA_KEY_ATTRIBUTES_MAYBE_SLOT_NUMBER
32232b31808SJens Wiklander #endif
323b0563631STom Van Eyck #define PSA_KEY_ATTRIBUTES_INIT { PSA_KEY_ATTRIBUTES_MAYBE_SLOT_NUMBER \
324b0563631STom Van Eyck                                       PSA_KEY_TYPE_NONE, 0,            \
325b0563631STom Van Eyck                                       PSA_KEY_LIFETIME_VOLATILE,       \
326b0563631STom Van Eyck                                       PSA_KEY_POLICY_INIT,             \
327b0563631STom Van Eyck                                       MBEDTLS_SVC_KEY_ID_INIT }
32832b31808SJens Wiklander 
psa_key_attributes_init(void)32932b31808SJens Wiklander static inline struct psa_key_attributes_s psa_key_attributes_init(void)
33032b31808SJens Wiklander {
33132b31808SJens Wiklander     const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT;
33232b31808SJens Wiklander     return v;
33332b31808SJens Wiklander }
33432b31808SJens Wiklander 
psa_set_key_id(psa_key_attributes_t * attributes,mbedtls_svc_key_id_t key)33532b31808SJens Wiklander static inline void psa_set_key_id(psa_key_attributes_t *attributes,
33632b31808SJens Wiklander                                   mbedtls_svc_key_id_t key)
33732b31808SJens Wiklander {
338b0563631STom Van Eyck     psa_key_lifetime_t lifetime = attributes->MBEDTLS_PRIVATE(lifetime);
33932b31808SJens Wiklander 
340b0563631STom Van Eyck     attributes->MBEDTLS_PRIVATE(id) = key;
34132b31808SJens Wiklander 
34232b31808SJens Wiklander     if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
343b0563631STom Van Eyck         attributes->MBEDTLS_PRIVATE(lifetime) =
34432b31808SJens Wiklander             PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
34532b31808SJens Wiklander                 PSA_KEY_LIFETIME_PERSISTENT,
34632b31808SJens Wiklander                 PSA_KEY_LIFETIME_GET_LOCATION(lifetime));
34732b31808SJens Wiklander     }
34832b31808SJens Wiklander }
34932b31808SJens Wiklander 
psa_get_key_id(const psa_key_attributes_t * attributes)35032b31808SJens Wiklander static inline mbedtls_svc_key_id_t psa_get_key_id(
35132b31808SJens Wiklander     const psa_key_attributes_t *attributes)
35232b31808SJens Wiklander {
353b0563631STom Van Eyck     return attributes->MBEDTLS_PRIVATE(id);
35432b31808SJens Wiklander }
35532b31808SJens Wiklander 
35632b31808SJens Wiklander #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
mbedtls_set_key_owner_id(psa_key_attributes_t * attributes,mbedtls_key_owner_id_t owner)35732b31808SJens Wiklander static inline void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes,
35832b31808SJens Wiklander                                             mbedtls_key_owner_id_t owner)
35932b31808SJens Wiklander {
360b0563631STom Van Eyck     attributes->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(owner) = owner;
36132b31808SJens Wiklander }
36232b31808SJens Wiklander #endif
36332b31808SJens Wiklander 
psa_set_key_lifetime(psa_key_attributes_t * attributes,psa_key_lifetime_t lifetime)36432b31808SJens Wiklander static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
36532b31808SJens Wiklander                                         psa_key_lifetime_t lifetime)
36632b31808SJens Wiklander {
367b0563631STom Van Eyck     attributes->MBEDTLS_PRIVATE(lifetime) = lifetime;
36832b31808SJens Wiklander     if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
36932b31808SJens Wiklander #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
370b0563631STom Van Eyck         attributes->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id) = 0;
37132b31808SJens Wiklander #else
372b0563631STom Van Eyck         attributes->MBEDTLS_PRIVATE(id) = 0;
37332b31808SJens Wiklander #endif
37432b31808SJens Wiklander     }
37532b31808SJens Wiklander }
37632b31808SJens Wiklander 
psa_get_key_lifetime(const psa_key_attributes_t * attributes)37732b31808SJens Wiklander static inline psa_key_lifetime_t psa_get_key_lifetime(
37832b31808SJens Wiklander     const psa_key_attributes_t *attributes)
37932b31808SJens Wiklander {
380b0563631STom Van Eyck     return attributes->MBEDTLS_PRIVATE(lifetime);
38132b31808SJens Wiklander }
38232b31808SJens Wiklander 
psa_extend_key_usage_flags(psa_key_usage_t * usage_flags)38332b31808SJens Wiklander static inline void psa_extend_key_usage_flags(psa_key_usage_t *usage_flags)
38432b31808SJens Wiklander {
38532b31808SJens Wiklander     if (*usage_flags & PSA_KEY_USAGE_SIGN_HASH) {
38632b31808SJens Wiklander         *usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE;
38732b31808SJens Wiklander     }
38832b31808SJens Wiklander 
38932b31808SJens Wiklander     if (*usage_flags & PSA_KEY_USAGE_VERIFY_HASH) {
39032b31808SJens Wiklander         *usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE;
39132b31808SJens Wiklander     }
39232b31808SJens Wiklander }
39332b31808SJens Wiklander 
psa_set_key_usage_flags(psa_key_attributes_t * attributes,psa_key_usage_t usage_flags)39432b31808SJens Wiklander static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
39532b31808SJens Wiklander                                            psa_key_usage_t usage_flags)
39632b31808SJens Wiklander {
39732b31808SJens Wiklander     psa_extend_key_usage_flags(&usage_flags);
398b0563631STom Van Eyck     attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage) = usage_flags;
39932b31808SJens Wiklander }
40032b31808SJens Wiklander 
psa_get_key_usage_flags(const psa_key_attributes_t * attributes)40132b31808SJens Wiklander static inline psa_key_usage_t psa_get_key_usage_flags(
40232b31808SJens Wiklander     const psa_key_attributes_t *attributes)
40332b31808SJens Wiklander {
404b0563631STom Van Eyck     return attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage);
40532b31808SJens Wiklander }
40632b31808SJens Wiklander 
psa_set_key_algorithm(psa_key_attributes_t * attributes,psa_algorithm_t alg)40732b31808SJens Wiklander static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
40832b31808SJens Wiklander                                          psa_algorithm_t alg)
40932b31808SJens Wiklander {
410b0563631STom Van Eyck     attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg) = alg;
41132b31808SJens Wiklander }
41232b31808SJens Wiklander 
psa_get_key_algorithm(const psa_key_attributes_t * attributes)41332b31808SJens Wiklander static inline psa_algorithm_t psa_get_key_algorithm(
41432b31808SJens Wiklander     const psa_key_attributes_t *attributes)
41532b31808SJens Wiklander {
416b0563631STom Van Eyck     return attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg);
41732b31808SJens Wiklander }
41832b31808SJens Wiklander 
psa_set_key_type(psa_key_attributes_t * attributes,psa_key_type_t type)41932b31808SJens Wiklander static inline void psa_set_key_type(psa_key_attributes_t *attributes,
42032b31808SJens Wiklander                                     psa_key_type_t type)
42132b31808SJens Wiklander {
422b0563631STom Van Eyck     attributes->MBEDTLS_PRIVATE(type) = type;
42332b31808SJens Wiklander }
42432b31808SJens Wiklander 
psa_get_key_type(const psa_key_attributes_t * attributes)42532b31808SJens Wiklander static inline psa_key_type_t psa_get_key_type(
42632b31808SJens Wiklander     const psa_key_attributes_t *attributes)
42732b31808SJens Wiklander {
428b0563631STom Van Eyck     return attributes->MBEDTLS_PRIVATE(type);
42932b31808SJens Wiklander }
43032b31808SJens Wiklander 
psa_set_key_bits(psa_key_attributes_t * attributes,size_t bits)43132b31808SJens Wiklander static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
43232b31808SJens Wiklander                                     size_t bits)
43332b31808SJens Wiklander {
43432b31808SJens Wiklander     if (bits > PSA_MAX_KEY_BITS) {
435b0563631STom Van Eyck         attributes->MBEDTLS_PRIVATE(bits) = PSA_KEY_BITS_TOO_LARGE;
43632b31808SJens Wiklander     } else {
437b0563631STom Van Eyck         attributes->MBEDTLS_PRIVATE(bits) = (psa_key_bits_t) bits;
43832b31808SJens Wiklander     }
43932b31808SJens Wiklander }
44032b31808SJens Wiklander 
psa_get_key_bits(const psa_key_attributes_t * attributes)44132b31808SJens Wiklander static inline size_t psa_get_key_bits(
44232b31808SJens Wiklander     const psa_key_attributes_t *attributes)
44332b31808SJens Wiklander {
444b0563631STom Van Eyck     return attributes->MBEDTLS_PRIVATE(bits);
44532b31808SJens Wiklander }
44632b31808SJens Wiklander 
44732b31808SJens Wiklander /**
44832b31808SJens Wiklander  * \brief The context for PSA interruptible hash signing.
44932b31808SJens Wiklander  */
45032b31808SJens Wiklander struct psa_sign_hash_interruptible_operation_s {
451b0563631STom Van Eyck #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
452b0563631STom Van Eyck     mbedtls_psa_client_handle_t handle;
453b0563631STom Van Eyck #else
45432b31808SJens Wiklander     /** Unique ID indicating which driver got assigned to do the
45532b31808SJens Wiklander      * operation. Since driver contexts are driver-specific, swapping
45632b31808SJens Wiklander      * drivers halfway through the operation is not supported.
45732b31808SJens Wiklander      * ID values are auto-generated in psa_crypto_driver_wrappers.h
45832b31808SJens Wiklander      * ID value zero means the context is not valid or not assigned to
45932b31808SJens Wiklander      * any driver (i.e. none of the driver contexts are active). */
46032b31808SJens Wiklander     unsigned int MBEDTLS_PRIVATE(id);
46132b31808SJens Wiklander 
46232b31808SJens Wiklander     psa_driver_sign_hash_interruptible_context_t MBEDTLS_PRIVATE(ctx);
46332b31808SJens Wiklander 
46432b31808SJens Wiklander     unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
46532b31808SJens Wiklander 
46632b31808SJens Wiklander     uint32_t MBEDTLS_PRIVATE(num_ops);
467b0563631STom Van Eyck #endif
46832b31808SJens Wiklander };
46932b31808SJens Wiklander 
470b0563631STom Van Eyck #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
471b0563631STom Van Eyck #define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
472b0563631STom Van Eyck #else
47332b31808SJens Wiklander #define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
474b0563631STom Van Eyck #endif
47532b31808SJens Wiklander 
47632b31808SJens Wiklander static inline struct psa_sign_hash_interruptible_operation_s
psa_sign_hash_interruptible_operation_init(void)47732b31808SJens Wiklander psa_sign_hash_interruptible_operation_init(void)
47832b31808SJens Wiklander {
47932b31808SJens Wiklander     const struct psa_sign_hash_interruptible_operation_s v =
48032b31808SJens Wiklander         PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT;
48132b31808SJens Wiklander 
48232b31808SJens Wiklander     return v;
48332b31808SJens Wiklander }
48432b31808SJens Wiklander 
48532b31808SJens Wiklander /**
48632b31808SJens Wiklander  * \brief The context for PSA interruptible hash verification.
48732b31808SJens Wiklander  */
48832b31808SJens Wiklander struct psa_verify_hash_interruptible_operation_s {
489b0563631STom Van Eyck #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
490b0563631STom Van Eyck     mbedtls_psa_client_handle_t handle;
491b0563631STom Van Eyck #else
49232b31808SJens Wiklander     /** Unique ID indicating which driver got assigned to do the
49332b31808SJens Wiklander      * operation. Since driver contexts are driver-specific, swapping
49432b31808SJens Wiklander      * drivers halfway through the operation is not supported.
49532b31808SJens Wiklander      * ID values are auto-generated in psa_crypto_driver_wrappers.h
49632b31808SJens Wiklander      * ID value zero means the context is not valid or not assigned to
49732b31808SJens Wiklander      * any driver (i.e. none of the driver contexts are active). */
49832b31808SJens Wiklander     unsigned int MBEDTLS_PRIVATE(id);
49932b31808SJens Wiklander 
50032b31808SJens Wiklander     psa_driver_verify_hash_interruptible_context_t MBEDTLS_PRIVATE(ctx);
50132b31808SJens Wiklander 
50232b31808SJens Wiklander     unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
50332b31808SJens Wiklander 
50432b31808SJens Wiklander     uint32_t MBEDTLS_PRIVATE(num_ops);
505b0563631STom Van Eyck #endif
50632b31808SJens Wiklander };
50732b31808SJens Wiklander 
508b0563631STom Van Eyck #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
509b0563631STom Van Eyck #define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
510b0563631STom Van Eyck #else
51132b31808SJens Wiklander #define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
512b0563631STom Van Eyck #endif
51332b31808SJens Wiklander 
51432b31808SJens Wiklander static inline struct psa_verify_hash_interruptible_operation_s
psa_verify_hash_interruptible_operation_init(void)51532b31808SJens Wiklander psa_verify_hash_interruptible_operation_init(void)
51632b31808SJens Wiklander {
51732b31808SJens Wiklander     const struct psa_verify_hash_interruptible_operation_s v =
51832b31808SJens Wiklander         PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT;
51932b31808SJens Wiklander 
52032b31808SJens Wiklander     return v;
52132b31808SJens Wiklander }
52232b31808SJens Wiklander 
52332b31808SJens Wiklander #ifdef __cplusplus
52432b31808SJens Wiklander }
52532b31808SJens Wiklander #endif
52632b31808SJens Wiklander 
52732b31808SJens Wiklander #endif /* PSA_CRYPTO_STRUCT_H */
528