1a8e1175bSopenharmony_ci/**
2a8e1175bSopenharmony_ci * \file psa/crypto_struct.h
3a8e1175bSopenharmony_ci *
4a8e1175bSopenharmony_ci * \brief PSA cryptography module: Mbed TLS structured type implementations
5a8e1175bSopenharmony_ci *
6a8e1175bSopenharmony_ci * \note This file may not be included directly. Applications must
7a8e1175bSopenharmony_ci * include psa/crypto.h.
8a8e1175bSopenharmony_ci *
9a8e1175bSopenharmony_ci * This file contains the definitions of some data structures with
10a8e1175bSopenharmony_ci * implementation-specific definitions.
11a8e1175bSopenharmony_ci *
12a8e1175bSopenharmony_ci * In implementations with isolation between the application and the
13a8e1175bSopenharmony_ci * cryptography module, it is expected that the front-end and the back-end
14a8e1175bSopenharmony_ci * would have different versions of this file.
15a8e1175bSopenharmony_ci *
16a8e1175bSopenharmony_ci * <h3>Design notes about multipart operation structures</h3>
17a8e1175bSopenharmony_ci *
18a8e1175bSopenharmony_ci * For multipart operations without driver delegation support, each multipart
19a8e1175bSopenharmony_ci * operation structure contains a `psa_algorithm_t alg` field which indicates
20a8e1175bSopenharmony_ci * which specific algorithm the structure is for. When the structure is not in
21a8e1175bSopenharmony_ci * use, `alg` is 0. Most of the structure consists of a union which is
22a8e1175bSopenharmony_ci * discriminated by `alg`.
23a8e1175bSopenharmony_ci *
24a8e1175bSopenharmony_ci * For multipart operations with driver delegation support, each multipart
25a8e1175bSopenharmony_ci * operation structure contains an `unsigned int id` field indicating which
26a8e1175bSopenharmony_ci * driver got assigned to do the operation. When the structure is not in use,
27a8e1175bSopenharmony_ci * 'id' is 0. The structure contains also a driver context which is the union
28a8e1175bSopenharmony_ci * of the contexts of all drivers able to handle the type of multipart
29a8e1175bSopenharmony_ci * operation.
30a8e1175bSopenharmony_ci *
31a8e1175bSopenharmony_ci * Note that when `alg` or `id` is 0, the content of other fields is undefined.
32a8e1175bSopenharmony_ci * In particular, it is not guaranteed that a freshly-initialized structure
33a8e1175bSopenharmony_ci * is all-zero: we initialize structures to something like `{0, 0}`, which
34a8e1175bSopenharmony_ci * is only guaranteed to initializes the first member of the union;
35a8e1175bSopenharmony_ci * GCC and Clang initialize the whole structure to 0 (at the time of writing),
36a8e1175bSopenharmony_ci * but MSVC and CompCert don't.
37a8e1175bSopenharmony_ci *
38a8e1175bSopenharmony_ci * In Mbed TLS, multipart operation structures live independently from
39a8e1175bSopenharmony_ci * the key. This allows Mbed TLS to free the key objects when destroying
40a8e1175bSopenharmony_ci * a key slot. If a multipart operation needs to remember the key after
41a8e1175bSopenharmony_ci * the setup function returns, the operation structure needs to contain a
42a8e1175bSopenharmony_ci * copy of the key.
43a8e1175bSopenharmony_ci */
44a8e1175bSopenharmony_ci/*
45a8e1175bSopenharmony_ci *  Copyright The Mbed TLS Contributors
46a8e1175bSopenharmony_ci *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
47a8e1175bSopenharmony_ci */
48a8e1175bSopenharmony_ci
49a8e1175bSopenharmony_ci#ifndef PSA_CRYPTO_STRUCT_H
50a8e1175bSopenharmony_ci#define PSA_CRYPTO_STRUCT_H
51a8e1175bSopenharmony_ci#include "mbedtls/private_access.h"
52a8e1175bSopenharmony_ci
53a8e1175bSopenharmony_ci#ifdef __cplusplus
54a8e1175bSopenharmony_ciextern "C" {
55a8e1175bSopenharmony_ci#endif
56a8e1175bSopenharmony_ci
57a8e1175bSopenharmony_ci/*
58a8e1175bSopenharmony_ci * Include the build-time configuration information header. Here, we do not
59a8e1175bSopenharmony_ci * include `"mbedtls/build_info.h"` directly but `"psa/build_info.h"`, which
60a8e1175bSopenharmony_ci * is basically just an alias to it. This is to ease the maintenance of the
61a8e1175bSopenharmony_ci * TF-PSA-Crypto repository which has a different build system and
62a8e1175bSopenharmony_ci * configuration.
63a8e1175bSopenharmony_ci */
64a8e1175bSopenharmony_ci#include "psa/build_info.h"
65a8e1175bSopenharmony_ci
66a8e1175bSopenharmony_ci/* Include the context definition for the compiled-in drivers for the primitive
67a8e1175bSopenharmony_ci * algorithms. */
68a8e1175bSopenharmony_ci#include "psa/crypto_driver_contexts_primitives.h"
69a8e1175bSopenharmony_ci
70a8e1175bSopenharmony_cistruct psa_hash_operation_s {
71a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
72a8e1175bSopenharmony_ci    mbedtls_psa_client_handle_t handle;
73a8e1175bSopenharmony_ci#else
74a8e1175bSopenharmony_ci    /** Unique ID indicating which driver got assigned to do the
75a8e1175bSopenharmony_ci     * operation. Since driver contexts are driver-specific, swapping
76a8e1175bSopenharmony_ci     * drivers halfway through the operation is not supported.
77a8e1175bSopenharmony_ci     * ID values are auto-generated in psa_driver_wrappers.h.
78a8e1175bSopenharmony_ci     * ID value zero means the context is not valid or not assigned to
79a8e1175bSopenharmony_ci     * any driver (i.e. the driver context is not active, in use). */
80a8e1175bSopenharmony_ci    unsigned int MBEDTLS_PRIVATE(id);
81a8e1175bSopenharmony_ci    psa_driver_hash_context_t MBEDTLS_PRIVATE(ctx);
82a8e1175bSopenharmony_ci#endif
83a8e1175bSopenharmony_ci};
84a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
85a8e1175bSopenharmony_ci#define PSA_HASH_OPERATION_INIT { 0 }
86a8e1175bSopenharmony_ci#else
87a8e1175bSopenharmony_ci#define PSA_HASH_OPERATION_INIT { 0, { 0 } }
88a8e1175bSopenharmony_ci#endif
89a8e1175bSopenharmony_cistatic inline struct psa_hash_operation_s psa_hash_operation_init(void)
90a8e1175bSopenharmony_ci{
91a8e1175bSopenharmony_ci    const struct psa_hash_operation_s v = PSA_HASH_OPERATION_INIT;
92a8e1175bSopenharmony_ci    return v;
93a8e1175bSopenharmony_ci}
94a8e1175bSopenharmony_ci
95a8e1175bSopenharmony_cistruct psa_cipher_operation_s {
96a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
97a8e1175bSopenharmony_ci    mbedtls_psa_client_handle_t handle;
98a8e1175bSopenharmony_ci#else
99a8e1175bSopenharmony_ci    /** Unique ID indicating which driver got assigned to do the
100a8e1175bSopenharmony_ci     * operation. Since driver contexts are driver-specific, swapping
101a8e1175bSopenharmony_ci     * drivers halfway through the operation is not supported.
102a8e1175bSopenharmony_ci     * ID values are auto-generated in psa_crypto_driver_wrappers.h
103a8e1175bSopenharmony_ci     * ID value zero means the context is not valid or not assigned to
104a8e1175bSopenharmony_ci     * any driver (i.e. none of the driver contexts are active). */
105a8e1175bSopenharmony_ci    unsigned int MBEDTLS_PRIVATE(id);
106a8e1175bSopenharmony_ci
107a8e1175bSopenharmony_ci    unsigned int MBEDTLS_PRIVATE(iv_required) : 1;
108a8e1175bSopenharmony_ci    unsigned int MBEDTLS_PRIVATE(iv_set) : 1;
109a8e1175bSopenharmony_ci
110a8e1175bSopenharmony_ci    uint8_t MBEDTLS_PRIVATE(default_iv_length);
111a8e1175bSopenharmony_ci
112a8e1175bSopenharmony_ci    psa_driver_cipher_context_t MBEDTLS_PRIVATE(ctx);
113a8e1175bSopenharmony_ci#endif
114a8e1175bSopenharmony_ci};
115a8e1175bSopenharmony_ci
116a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
117a8e1175bSopenharmony_ci#define PSA_CIPHER_OPERATION_INIT { 0 }
118a8e1175bSopenharmony_ci#else
119a8e1175bSopenharmony_ci#define PSA_CIPHER_OPERATION_INIT { 0, 0, 0, 0, { 0 } }
120a8e1175bSopenharmony_ci#endif
121a8e1175bSopenharmony_cistatic inline struct psa_cipher_operation_s psa_cipher_operation_init(void)
122a8e1175bSopenharmony_ci{
123a8e1175bSopenharmony_ci    const struct psa_cipher_operation_s v = PSA_CIPHER_OPERATION_INIT;
124a8e1175bSopenharmony_ci    return v;
125a8e1175bSopenharmony_ci}
126a8e1175bSopenharmony_ci
127a8e1175bSopenharmony_ci/* Include the context definition for the compiled-in drivers for the composite
128a8e1175bSopenharmony_ci * algorithms. */
129a8e1175bSopenharmony_ci#include "psa/crypto_driver_contexts_composites.h"
130a8e1175bSopenharmony_ci
131a8e1175bSopenharmony_cistruct psa_mac_operation_s {
132a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
133a8e1175bSopenharmony_ci    mbedtls_psa_client_handle_t handle;
134a8e1175bSopenharmony_ci#else
135a8e1175bSopenharmony_ci    /** Unique ID indicating which driver got assigned to do the
136a8e1175bSopenharmony_ci     * operation. Since driver contexts are driver-specific, swapping
137a8e1175bSopenharmony_ci     * drivers halfway through the operation is not supported.
138a8e1175bSopenharmony_ci     * ID values are auto-generated in psa_driver_wrappers.h
139a8e1175bSopenharmony_ci     * ID value zero means the context is not valid or not assigned to
140a8e1175bSopenharmony_ci     * any driver (i.e. none of the driver contexts are active). */
141a8e1175bSopenharmony_ci    unsigned int MBEDTLS_PRIVATE(id);
142a8e1175bSopenharmony_ci    uint8_t MBEDTLS_PRIVATE(mac_size);
143a8e1175bSopenharmony_ci    unsigned int MBEDTLS_PRIVATE(is_sign) : 1;
144a8e1175bSopenharmony_ci    psa_driver_mac_context_t MBEDTLS_PRIVATE(ctx);
145a8e1175bSopenharmony_ci#endif
146a8e1175bSopenharmony_ci};
147a8e1175bSopenharmony_ci
148a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
149a8e1175bSopenharmony_ci#define PSA_MAC_OPERATION_INIT { 0 }
150a8e1175bSopenharmony_ci#else
151a8e1175bSopenharmony_ci#define PSA_MAC_OPERATION_INIT { 0, 0, 0, { 0 } }
152a8e1175bSopenharmony_ci#endif
153a8e1175bSopenharmony_cistatic inline struct psa_mac_operation_s psa_mac_operation_init(void)
154a8e1175bSopenharmony_ci{
155a8e1175bSopenharmony_ci    const struct psa_mac_operation_s v = PSA_MAC_OPERATION_INIT;
156a8e1175bSopenharmony_ci    return v;
157a8e1175bSopenharmony_ci}
158a8e1175bSopenharmony_ci
159a8e1175bSopenharmony_cistruct psa_aead_operation_s {
160a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
161a8e1175bSopenharmony_ci    mbedtls_psa_client_handle_t handle;
162a8e1175bSopenharmony_ci#else
163a8e1175bSopenharmony_ci    /** Unique ID indicating which driver got assigned to do the
164a8e1175bSopenharmony_ci     * operation. Since driver contexts are driver-specific, swapping
165a8e1175bSopenharmony_ci     * drivers halfway through the operation is not supported.
166a8e1175bSopenharmony_ci     * ID values are auto-generated in psa_crypto_driver_wrappers.h
167a8e1175bSopenharmony_ci     * ID value zero means the context is not valid or not assigned to
168a8e1175bSopenharmony_ci     * any driver (i.e. none of the driver contexts are active). */
169a8e1175bSopenharmony_ci    unsigned int MBEDTLS_PRIVATE(id);
170a8e1175bSopenharmony_ci
171a8e1175bSopenharmony_ci    psa_algorithm_t MBEDTLS_PRIVATE(alg);
172a8e1175bSopenharmony_ci    psa_key_type_t MBEDTLS_PRIVATE(key_type);
173a8e1175bSopenharmony_ci
174a8e1175bSopenharmony_ci    size_t MBEDTLS_PRIVATE(ad_remaining);
175a8e1175bSopenharmony_ci    size_t MBEDTLS_PRIVATE(body_remaining);
176a8e1175bSopenharmony_ci
177a8e1175bSopenharmony_ci    unsigned int MBEDTLS_PRIVATE(nonce_set) : 1;
178a8e1175bSopenharmony_ci    unsigned int MBEDTLS_PRIVATE(lengths_set) : 1;
179a8e1175bSopenharmony_ci    unsigned int MBEDTLS_PRIVATE(ad_started) : 1;
180a8e1175bSopenharmony_ci    unsigned int MBEDTLS_PRIVATE(body_started) : 1;
181a8e1175bSopenharmony_ci    unsigned int MBEDTLS_PRIVATE(is_encrypt) : 1;
182a8e1175bSopenharmony_ci
183a8e1175bSopenharmony_ci    psa_driver_aead_context_t MBEDTLS_PRIVATE(ctx);
184a8e1175bSopenharmony_ci#endif
185a8e1175bSopenharmony_ci};
186a8e1175bSopenharmony_ci
187a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
188a8e1175bSopenharmony_ci#define PSA_AEAD_OPERATION_INIT { 0 }
189a8e1175bSopenharmony_ci#else
190a8e1175bSopenharmony_ci#define PSA_AEAD_OPERATION_INIT { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0 } }
191a8e1175bSopenharmony_ci#endif
192a8e1175bSopenharmony_cistatic inline struct psa_aead_operation_s psa_aead_operation_init(void)
193a8e1175bSopenharmony_ci{
194a8e1175bSopenharmony_ci    const struct psa_aead_operation_s v = PSA_AEAD_OPERATION_INIT;
195a8e1175bSopenharmony_ci    return v;
196a8e1175bSopenharmony_ci}
197a8e1175bSopenharmony_ci
198a8e1175bSopenharmony_ci/* Include the context definition for the compiled-in drivers for the key
199a8e1175bSopenharmony_ci * derivation algorithms. */
200a8e1175bSopenharmony_ci#include "psa/crypto_driver_contexts_key_derivation.h"
201a8e1175bSopenharmony_ci
202a8e1175bSopenharmony_cistruct psa_key_derivation_s {
203a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
204a8e1175bSopenharmony_ci    mbedtls_psa_client_handle_t handle;
205a8e1175bSopenharmony_ci#else
206a8e1175bSopenharmony_ci    psa_algorithm_t MBEDTLS_PRIVATE(alg);
207a8e1175bSopenharmony_ci    unsigned int MBEDTLS_PRIVATE(can_output_key) : 1;
208a8e1175bSopenharmony_ci    size_t MBEDTLS_PRIVATE(capacity);
209a8e1175bSopenharmony_ci    psa_driver_key_derivation_context_t MBEDTLS_PRIVATE(ctx);
210a8e1175bSopenharmony_ci#endif
211a8e1175bSopenharmony_ci};
212a8e1175bSopenharmony_ci
213a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
214a8e1175bSopenharmony_ci#define PSA_KEY_DERIVATION_OPERATION_INIT { 0 }
215a8e1175bSopenharmony_ci#else
216a8e1175bSopenharmony_ci/* This only zeroes out the first byte in the union, the rest is unspecified. */
217a8e1175bSopenharmony_ci#define PSA_KEY_DERIVATION_OPERATION_INIT { 0, 0, 0, { 0 } }
218a8e1175bSopenharmony_ci#endif
219a8e1175bSopenharmony_cistatic inline struct psa_key_derivation_s psa_key_derivation_operation_init(
220a8e1175bSopenharmony_ci    void)
221a8e1175bSopenharmony_ci{
222a8e1175bSopenharmony_ci    const struct psa_key_derivation_s v = PSA_KEY_DERIVATION_OPERATION_INIT;
223a8e1175bSopenharmony_ci    return v;
224a8e1175bSopenharmony_ci}
225a8e1175bSopenharmony_ci
226a8e1175bSopenharmony_cistruct psa_key_production_parameters_s {
227a8e1175bSopenharmony_ci    /* Future versions may add other fields in this structure. */
228a8e1175bSopenharmony_ci    uint32_t flags;
229a8e1175bSopenharmony_ci    uint8_t data[];
230a8e1175bSopenharmony_ci};
231a8e1175bSopenharmony_ci
232a8e1175bSopenharmony_ci/** The default production parameters for key generation or key derivation.
233a8e1175bSopenharmony_ci *
234a8e1175bSopenharmony_ci * Calling psa_generate_key_ext() or psa_key_derivation_output_key_ext()
235a8e1175bSopenharmony_ci * with `params=PSA_KEY_PRODUCTION_PARAMETERS_INIT` and
236a8e1175bSopenharmony_ci * `params_data_length == 0` is equivalent to
237a8e1175bSopenharmony_ci * calling psa_generate_key() or psa_key_derivation_output_key()
238a8e1175bSopenharmony_ci * respectively.
239a8e1175bSopenharmony_ci */
240a8e1175bSopenharmony_ci#define PSA_KEY_PRODUCTION_PARAMETERS_INIT { 0 }
241a8e1175bSopenharmony_ci
242a8e1175bSopenharmony_cistruct psa_key_policy_s {
243a8e1175bSopenharmony_ci    psa_key_usage_t MBEDTLS_PRIVATE(usage);
244a8e1175bSopenharmony_ci    psa_algorithm_t MBEDTLS_PRIVATE(alg);
245a8e1175bSopenharmony_ci    psa_algorithm_t MBEDTLS_PRIVATE(alg2);
246a8e1175bSopenharmony_ci};
247a8e1175bSopenharmony_citypedef struct psa_key_policy_s psa_key_policy_t;
248a8e1175bSopenharmony_ci
249a8e1175bSopenharmony_ci#define PSA_KEY_POLICY_INIT { 0, 0, 0 }
250a8e1175bSopenharmony_cistatic inline struct psa_key_policy_s psa_key_policy_init(void)
251a8e1175bSopenharmony_ci{
252a8e1175bSopenharmony_ci    const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT;
253a8e1175bSopenharmony_ci    return v;
254a8e1175bSopenharmony_ci}
255a8e1175bSopenharmony_ci
256a8e1175bSopenharmony_ci/* The type used internally for key sizes.
257a8e1175bSopenharmony_ci * Public interfaces use size_t, but internally we use a smaller type. */
258a8e1175bSopenharmony_citypedef uint16_t psa_key_bits_t;
259a8e1175bSopenharmony_ci/* The maximum value of the type used to represent bit-sizes.
260a8e1175bSopenharmony_ci * This is used to mark an invalid key size. */
261a8e1175bSopenharmony_ci#define PSA_KEY_BITS_TOO_LARGE          ((psa_key_bits_t) -1)
262a8e1175bSopenharmony_ci/* The maximum size of a key in bits.
263a8e1175bSopenharmony_ci * Currently defined as the maximum that can be represented, rounded down
264a8e1175bSopenharmony_ci * to a whole number of bytes.
265a8e1175bSopenharmony_ci * This is an uncast value so that it can be used in preprocessor
266a8e1175bSopenharmony_ci * conditionals. */
267a8e1175bSopenharmony_ci#define PSA_MAX_KEY_BITS 0xfff8
268a8e1175bSopenharmony_ci
269a8e1175bSopenharmony_cistruct psa_key_attributes_s {
270a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
271a8e1175bSopenharmony_ci    psa_key_slot_number_t MBEDTLS_PRIVATE(slot_number);
272a8e1175bSopenharmony_ci    int MBEDTLS_PRIVATE(has_slot_number);
273a8e1175bSopenharmony_ci#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
274a8e1175bSopenharmony_ci    psa_key_type_t MBEDTLS_PRIVATE(type);
275a8e1175bSopenharmony_ci    psa_key_bits_t MBEDTLS_PRIVATE(bits);
276a8e1175bSopenharmony_ci    psa_key_lifetime_t MBEDTLS_PRIVATE(lifetime);
277a8e1175bSopenharmony_ci    psa_key_policy_t MBEDTLS_PRIVATE(policy);
278a8e1175bSopenharmony_ci    /* This type has a different layout in the client view wrt the
279a8e1175bSopenharmony_ci     * service view of the key id, i.e. in service view usually is
280a8e1175bSopenharmony_ci     * expected to have MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined
281a8e1175bSopenharmony_ci     * thus adding an owner field to the standard psa_key_id_t. For
282a8e1175bSopenharmony_ci     * implementations with client/service separation, this means the
283a8e1175bSopenharmony_ci     * object will be marshalled through a transport channel and
284a8e1175bSopenharmony_ci     * interpreted differently at each side of the transport. Placing
285a8e1175bSopenharmony_ci     * it at the end of structures allows to interpret the structure
286a8e1175bSopenharmony_ci     * at the client without reorganizing the memory layout of the
287a8e1175bSopenharmony_ci     * struct
288a8e1175bSopenharmony_ci     */
289a8e1175bSopenharmony_ci    mbedtls_svc_key_id_t MBEDTLS_PRIVATE(id);
290a8e1175bSopenharmony_ci};
291a8e1175bSopenharmony_ci
292a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
293a8e1175bSopenharmony_ci#define PSA_KEY_ATTRIBUTES_MAYBE_SLOT_NUMBER 0, 0,
294a8e1175bSopenharmony_ci#else
295a8e1175bSopenharmony_ci#define PSA_KEY_ATTRIBUTES_MAYBE_SLOT_NUMBER
296a8e1175bSopenharmony_ci#endif
297a8e1175bSopenharmony_ci#define PSA_KEY_ATTRIBUTES_INIT { PSA_KEY_ATTRIBUTES_MAYBE_SLOT_NUMBER \
298a8e1175bSopenharmony_ci                                      PSA_KEY_TYPE_NONE, 0,            \
299a8e1175bSopenharmony_ci                                      PSA_KEY_LIFETIME_VOLATILE,       \
300a8e1175bSopenharmony_ci                                      PSA_KEY_POLICY_INIT,             \
301a8e1175bSopenharmony_ci                                      MBEDTLS_SVC_KEY_ID_INIT }
302a8e1175bSopenharmony_ci
303a8e1175bSopenharmony_cistatic inline struct psa_key_attributes_s psa_key_attributes_init(void)
304a8e1175bSopenharmony_ci{
305a8e1175bSopenharmony_ci    const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT;
306a8e1175bSopenharmony_ci    return v;
307a8e1175bSopenharmony_ci}
308a8e1175bSopenharmony_ci
309a8e1175bSopenharmony_cistatic inline void psa_set_key_id(psa_key_attributes_t *attributes,
310a8e1175bSopenharmony_ci                                  mbedtls_svc_key_id_t key)
311a8e1175bSopenharmony_ci{
312a8e1175bSopenharmony_ci    psa_key_lifetime_t lifetime = attributes->MBEDTLS_PRIVATE(lifetime);
313a8e1175bSopenharmony_ci
314a8e1175bSopenharmony_ci    attributes->MBEDTLS_PRIVATE(id) = key;
315a8e1175bSopenharmony_ci
316a8e1175bSopenharmony_ci    if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
317a8e1175bSopenharmony_ci        attributes->MBEDTLS_PRIVATE(lifetime) =
318a8e1175bSopenharmony_ci            PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
319a8e1175bSopenharmony_ci                PSA_KEY_LIFETIME_PERSISTENT,
320a8e1175bSopenharmony_ci                PSA_KEY_LIFETIME_GET_LOCATION(lifetime));
321a8e1175bSopenharmony_ci    }
322a8e1175bSopenharmony_ci}
323a8e1175bSopenharmony_ci
324a8e1175bSopenharmony_cistatic inline mbedtls_svc_key_id_t psa_get_key_id(
325a8e1175bSopenharmony_ci    const psa_key_attributes_t *attributes)
326a8e1175bSopenharmony_ci{
327a8e1175bSopenharmony_ci    return attributes->MBEDTLS_PRIVATE(id);
328a8e1175bSopenharmony_ci}
329a8e1175bSopenharmony_ci
330a8e1175bSopenharmony_ci#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
331a8e1175bSopenharmony_cistatic inline void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes,
332a8e1175bSopenharmony_ci                                            mbedtls_key_owner_id_t owner)
333a8e1175bSopenharmony_ci{
334a8e1175bSopenharmony_ci    attributes->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(owner) = owner;
335a8e1175bSopenharmony_ci}
336a8e1175bSopenharmony_ci#endif
337a8e1175bSopenharmony_ci
338a8e1175bSopenharmony_cistatic inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
339a8e1175bSopenharmony_ci                                        psa_key_lifetime_t lifetime)
340a8e1175bSopenharmony_ci{
341a8e1175bSopenharmony_ci    attributes->MBEDTLS_PRIVATE(lifetime) = lifetime;
342a8e1175bSopenharmony_ci    if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
343a8e1175bSopenharmony_ci#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
344a8e1175bSopenharmony_ci        attributes->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id) = 0;
345a8e1175bSopenharmony_ci#else
346a8e1175bSopenharmony_ci        attributes->MBEDTLS_PRIVATE(id) = 0;
347a8e1175bSopenharmony_ci#endif
348a8e1175bSopenharmony_ci    }
349a8e1175bSopenharmony_ci}
350a8e1175bSopenharmony_ci
351a8e1175bSopenharmony_cistatic inline psa_key_lifetime_t psa_get_key_lifetime(
352a8e1175bSopenharmony_ci    const psa_key_attributes_t *attributes)
353a8e1175bSopenharmony_ci{
354a8e1175bSopenharmony_ci    return attributes->MBEDTLS_PRIVATE(lifetime);
355a8e1175bSopenharmony_ci}
356a8e1175bSopenharmony_ci
357a8e1175bSopenharmony_cistatic inline void psa_extend_key_usage_flags(psa_key_usage_t *usage_flags)
358a8e1175bSopenharmony_ci{
359a8e1175bSopenharmony_ci    if (*usage_flags & PSA_KEY_USAGE_SIGN_HASH) {
360a8e1175bSopenharmony_ci        *usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE;
361a8e1175bSopenharmony_ci    }
362a8e1175bSopenharmony_ci
363a8e1175bSopenharmony_ci    if (*usage_flags & PSA_KEY_USAGE_VERIFY_HASH) {
364a8e1175bSopenharmony_ci        *usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE;
365a8e1175bSopenharmony_ci    }
366a8e1175bSopenharmony_ci}
367a8e1175bSopenharmony_ci
368a8e1175bSopenharmony_cistatic inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
369a8e1175bSopenharmony_ci                                           psa_key_usage_t usage_flags)
370a8e1175bSopenharmony_ci{
371a8e1175bSopenharmony_ci    psa_extend_key_usage_flags(&usage_flags);
372a8e1175bSopenharmony_ci    attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage) = usage_flags;
373a8e1175bSopenharmony_ci}
374a8e1175bSopenharmony_ci
375a8e1175bSopenharmony_cistatic inline psa_key_usage_t psa_get_key_usage_flags(
376a8e1175bSopenharmony_ci    const psa_key_attributes_t *attributes)
377a8e1175bSopenharmony_ci{
378a8e1175bSopenharmony_ci    return attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage);
379a8e1175bSopenharmony_ci}
380a8e1175bSopenharmony_ci
381a8e1175bSopenharmony_cistatic inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
382a8e1175bSopenharmony_ci                                         psa_algorithm_t alg)
383a8e1175bSopenharmony_ci{
384a8e1175bSopenharmony_ci    attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg) = alg;
385a8e1175bSopenharmony_ci}
386a8e1175bSopenharmony_ci
387a8e1175bSopenharmony_cistatic inline psa_algorithm_t psa_get_key_algorithm(
388a8e1175bSopenharmony_ci    const psa_key_attributes_t *attributes)
389a8e1175bSopenharmony_ci{
390a8e1175bSopenharmony_ci    return attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg);
391a8e1175bSopenharmony_ci}
392a8e1175bSopenharmony_ci
393a8e1175bSopenharmony_cistatic inline void psa_set_key_type(psa_key_attributes_t *attributes,
394a8e1175bSopenharmony_ci                                    psa_key_type_t type)
395a8e1175bSopenharmony_ci{
396a8e1175bSopenharmony_ci    attributes->MBEDTLS_PRIVATE(type) = type;
397a8e1175bSopenharmony_ci}
398a8e1175bSopenharmony_ci
399a8e1175bSopenharmony_cistatic inline psa_key_type_t psa_get_key_type(
400a8e1175bSopenharmony_ci    const psa_key_attributes_t *attributes)
401a8e1175bSopenharmony_ci{
402a8e1175bSopenharmony_ci    return attributes->MBEDTLS_PRIVATE(type);
403a8e1175bSopenharmony_ci}
404a8e1175bSopenharmony_ci
405a8e1175bSopenharmony_cistatic inline void psa_set_key_bits(psa_key_attributes_t *attributes,
406a8e1175bSopenharmony_ci                                    size_t bits)
407a8e1175bSopenharmony_ci{
408a8e1175bSopenharmony_ci    if (bits > PSA_MAX_KEY_BITS) {
409a8e1175bSopenharmony_ci        attributes->MBEDTLS_PRIVATE(bits) = PSA_KEY_BITS_TOO_LARGE;
410a8e1175bSopenharmony_ci    } else {
411a8e1175bSopenharmony_ci        attributes->MBEDTLS_PRIVATE(bits) = (psa_key_bits_t) bits;
412a8e1175bSopenharmony_ci    }
413a8e1175bSopenharmony_ci}
414a8e1175bSopenharmony_ci
415a8e1175bSopenharmony_cistatic inline size_t psa_get_key_bits(
416a8e1175bSopenharmony_ci    const psa_key_attributes_t *attributes)
417a8e1175bSopenharmony_ci{
418a8e1175bSopenharmony_ci    return attributes->MBEDTLS_PRIVATE(bits);
419a8e1175bSopenharmony_ci}
420a8e1175bSopenharmony_ci
421a8e1175bSopenharmony_ci/**
422a8e1175bSopenharmony_ci * \brief The context for PSA interruptible hash signing.
423a8e1175bSopenharmony_ci */
424a8e1175bSopenharmony_cistruct psa_sign_hash_interruptible_operation_s {
425a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
426a8e1175bSopenharmony_ci    mbedtls_psa_client_handle_t handle;
427a8e1175bSopenharmony_ci#else
428a8e1175bSopenharmony_ci    /** Unique ID indicating which driver got assigned to do the
429a8e1175bSopenharmony_ci     * operation. Since driver contexts are driver-specific, swapping
430a8e1175bSopenharmony_ci     * drivers halfway through the operation is not supported.
431a8e1175bSopenharmony_ci     * ID values are auto-generated in psa_crypto_driver_wrappers.h
432a8e1175bSopenharmony_ci     * ID value zero means the context is not valid or not assigned to
433a8e1175bSopenharmony_ci     * any driver (i.e. none of the driver contexts are active). */
434a8e1175bSopenharmony_ci    unsigned int MBEDTLS_PRIVATE(id);
435a8e1175bSopenharmony_ci
436a8e1175bSopenharmony_ci    psa_driver_sign_hash_interruptible_context_t MBEDTLS_PRIVATE(ctx);
437a8e1175bSopenharmony_ci
438a8e1175bSopenharmony_ci    unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
439a8e1175bSopenharmony_ci
440a8e1175bSopenharmony_ci    uint32_t MBEDTLS_PRIVATE(num_ops);
441a8e1175bSopenharmony_ci#endif
442a8e1175bSopenharmony_ci};
443a8e1175bSopenharmony_ci
444a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
445a8e1175bSopenharmony_ci#define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
446a8e1175bSopenharmony_ci#else
447a8e1175bSopenharmony_ci#define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
448a8e1175bSopenharmony_ci#endif
449a8e1175bSopenharmony_ci
450a8e1175bSopenharmony_cistatic inline struct psa_sign_hash_interruptible_operation_s
451a8e1175bSopenharmony_cipsa_sign_hash_interruptible_operation_init(void)
452a8e1175bSopenharmony_ci{
453a8e1175bSopenharmony_ci    const struct psa_sign_hash_interruptible_operation_s v =
454a8e1175bSopenharmony_ci        PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT;
455a8e1175bSopenharmony_ci
456a8e1175bSopenharmony_ci    return v;
457a8e1175bSopenharmony_ci}
458a8e1175bSopenharmony_ci
459a8e1175bSopenharmony_ci/**
460a8e1175bSopenharmony_ci * \brief The context for PSA interruptible hash verification.
461a8e1175bSopenharmony_ci */
462a8e1175bSopenharmony_cistruct psa_verify_hash_interruptible_operation_s {
463a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
464a8e1175bSopenharmony_ci    mbedtls_psa_client_handle_t handle;
465a8e1175bSopenharmony_ci#else
466a8e1175bSopenharmony_ci    /** Unique ID indicating which driver got assigned to do the
467a8e1175bSopenharmony_ci     * operation. Since driver contexts are driver-specific, swapping
468a8e1175bSopenharmony_ci     * drivers halfway through the operation is not supported.
469a8e1175bSopenharmony_ci     * ID values are auto-generated in psa_crypto_driver_wrappers.h
470a8e1175bSopenharmony_ci     * ID value zero means the context is not valid or not assigned to
471a8e1175bSopenharmony_ci     * any driver (i.e. none of the driver contexts are active). */
472a8e1175bSopenharmony_ci    unsigned int MBEDTLS_PRIVATE(id);
473a8e1175bSopenharmony_ci
474a8e1175bSopenharmony_ci    psa_driver_verify_hash_interruptible_context_t MBEDTLS_PRIVATE(ctx);
475a8e1175bSopenharmony_ci
476a8e1175bSopenharmony_ci    unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
477a8e1175bSopenharmony_ci
478a8e1175bSopenharmony_ci    uint32_t MBEDTLS_PRIVATE(num_ops);
479a8e1175bSopenharmony_ci#endif
480a8e1175bSopenharmony_ci};
481a8e1175bSopenharmony_ci
482a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
483a8e1175bSopenharmony_ci#define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
484a8e1175bSopenharmony_ci#else
485a8e1175bSopenharmony_ci#define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
486a8e1175bSopenharmony_ci#endif
487a8e1175bSopenharmony_ci
488a8e1175bSopenharmony_cistatic inline struct psa_verify_hash_interruptible_operation_s
489a8e1175bSopenharmony_cipsa_verify_hash_interruptible_operation_init(void)
490a8e1175bSopenharmony_ci{
491a8e1175bSopenharmony_ci    const struct psa_verify_hash_interruptible_operation_s v =
492a8e1175bSopenharmony_ci        PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT;
493a8e1175bSopenharmony_ci
494a8e1175bSopenharmony_ci    return v;
495a8e1175bSopenharmony_ci}
496a8e1175bSopenharmony_ci
497a8e1175bSopenharmony_ci#ifdef __cplusplus
498a8e1175bSopenharmony_ci}
499a8e1175bSopenharmony_ci#endif
500a8e1175bSopenharmony_ci
501a8e1175bSopenharmony_ci#endif /* PSA_CRYPTO_STRUCT_H */
502