1a8e1175bSopenharmony_ci/*
2a8e1175bSopenharmony_ci *  Context structure declaration of the Mbed TLS software-based PSA drivers
3a8e1175bSopenharmony_ci *  called through the PSA Crypto driver dispatch layer.
4a8e1175bSopenharmony_ci *  This file contains the context structures of those algorithms which need to
5a8e1175bSopenharmony_ci *  rely on other algorithms, i.e. are 'composite' algorithms.
6a8e1175bSopenharmony_ci *
7a8e1175bSopenharmony_ci * \note This file may not be included directly. Applications must
8a8e1175bSopenharmony_ci * include psa/crypto.h.
9a8e1175bSopenharmony_ci *
10a8e1175bSopenharmony_ci * \note This header and its content are not part of the Mbed TLS API and
11a8e1175bSopenharmony_ci * applications must not depend on it. Its main purpose is to define the
12a8e1175bSopenharmony_ci * multi-part state objects of the Mbed TLS software-based PSA drivers. The
13a8e1175bSopenharmony_ci * definitions of these objects are then used by crypto_struct.h to define the
14a8e1175bSopenharmony_ci * implementation-defined types of PSA multi-part state objects.
15a8e1175bSopenharmony_ci */
16a8e1175bSopenharmony_ci/*
17a8e1175bSopenharmony_ci *  Copyright The Mbed TLS Contributors
18a8e1175bSopenharmony_ci *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
19a8e1175bSopenharmony_ci */
20a8e1175bSopenharmony_ci
21a8e1175bSopenharmony_ci#ifndef PSA_CRYPTO_BUILTIN_COMPOSITES_H
22a8e1175bSopenharmony_ci#define PSA_CRYPTO_BUILTIN_COMPOSITES_H
23a8e1175bSopenharmony_ci#include "mbedtls/private_access.h"
24a8e1175bSopenharmony_ci
25a8e1175bSopenharmony_ci#include <psa/crypto_driver_common.h>
26a8e1175bSopenharmony_ci
27a8e1175bSopenharmony_ci#include "mbedtls/cmac.h"
28a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
29a8e1175bSopenharmony_ci#include "mbedtls/gcm.h"
30a8e1175bSopenharmony_ci#endif
31a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
32a8e1175bSopenharmony_ci#include "mbedtls/ccm.h"
33a8e1175bSopenharmony_ci#endif
34a8e1175bSopenharmony_ci#include "mbedtls/chachapoly.h"
35a8e1175bSopenharmony_ci
36a8e1175bSopenharmony_ci/*
37a8e1175bSopenharmony_ci * MAC multi-part operation definitions.
38a8e1175bSopenharmony_ci */
39a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) || \
40a8e1175bSopenharmony_ci    defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
41a8e1175bSopenharmony_ci#define MBEDTLS_PSA_BUILTIN_MAC
42a8e1175bSopenharmony_ci#endif
43a8e1175bSopenharmony_ci
44a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || defined(PSA_CRYPTO_DRIVER_TEST)
45a8e1175bSopenharmony_citypedef struct {
46a8e1175bSopenharmony_ci    /** The HMAC algorithm in use */
47a8e1175bSopenharmony_ci    psa_algorithm_t MBEDTLS_PRIVATE(alg);
48a8e1175bSopenharmony_ci    /** The hash context. */
49a8e1175bSopenharmony_ci    struct psa_hash_operation_s hash_ctx;
50a8e1175bSopenharmony_ci    /** The HMAC part of the context. */
51a8e1175bSopenharmony_ci    uint8_t MBEDTLS_PRIVATE(opad)[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
52a8e1175bSopenharmony_ci} mbedtls_psa_hmac_operation_t;
53a8e1175bSopenharmony_ci
54a8e1175bSopenharmony_ci#define MBEDTLS_PSA_HMAC_OPERATION_INIT { 0, PSA_HASH_OPERATION_INIT, { 0 } }
55a8e1175bSopenharmony_ci#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
56a8e1175bSopenharmony_ci
57a8e1175bSopenharmony_citypedef struct {
58a8e1175bSopenharmony_ci    psa_algorithm_t MBEDTLS_PRIVATE(alg);
59a8e1175bSopenharmony_ci    union {
60a8e1175bSopenharmony_ci        unsigned MBEDTLS_PRIVATE(dummy); /* Make the union non-empty even with no supported algorithms. */
61a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || defined(PSA_CRYPTO_DRIVER_TEST)
62a8e1175bSopenharmony_ci        mbedtls_psa_hmac_operation_t MBEDTLS_PRIVATE(hmac);
63a8e1175bSopenharmony_ci#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
64a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) || defined(PSA_CRYPTO_DRIVER_TEST)
65a8e1175bSopenharmony_ci        mbedtls_cipher_context_t MBEDTLS_PRIVATE(cmac);
66a8e1175bSopenharmony_ci#endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
67a8e1175bSopenharmony_ci    } MBEDTLS_PRIVATE(ctx);
68a8e1175bSopenharmony_ci} mbedtls_psa_mac_operation_t;
69a8e1175bSopenharmony_ci
70a8e1175bSopenharmony_ci#define MBEDTLS_PSA_MAC_OPERATION_INIT { 0, { 0 } }
71a8e1175bSopenharmony_ci
72a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) || \
73a8e1175bSopenharmony_ci    defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) || \
74a8e1175bSopenharmony_ci    defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
75a8e1175bSopenharmony_ci#define MBEDTLS_PSA_BUILTIN_AEAD  1
76a8e1175bSopenharmony_ci#endif
77a8e1175bSopenharmony_ci
78a8e1175bSopenharmony_ci/* Context structure for the Mbed TLS AEAD implementation. */
79a8e1175bSopenharmony_citypedef struct {
80a8e1175bSopenharmony_ci    psa_algorithm_t MBEDTLS_PRIVATE(alg);
81a8e1175bSopenharmony_ci    psa_key_type_t MBEDTLS_PRIVATE(key_type);
82a8e1175bSopenharmony_ci
83a8e1175bSopenharmony_ci    unsigned int MBEDTLS_PRIVATE(is_encrypt) : 1;
84a8e1175bSopenharmony_ci
85a8e1175bSopenharmony_ci    uint8_t MBEDTLS_PRIVATE(tag_length);
86a8e1175bSopenharmony_ci
87a8e1175bSopenharmony_ci    union {
88a8e1175bSopenharmony_ci        unsigned dummy; /* Enable easier initializing of the union. */
89a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
90a8e1175bSopenharmony_ci        mbedtls_ccm_context MBEDTLS_PRIVATE(ccm);
91a8e1175bSopenharmony_ci#endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
92a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
93a8e1175bSopenharmony_ci        mbedtls_gcm_context MBEDTLS_PRIVATE(gcm);
94a8e1175bSopenharmony_ci#endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
95a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
96a8e1175bSopenharmony_ci        mbedtls_chachapoly_context MBEDTLS_PRIVATE(chachapoly);
97a8e1175bSopenharmony_ci#endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
98a8e1175bSopenharmony_ci
99a8e1175bSopenharmony_ci    } ctx;
100a8e1175bSopenharmony_ci
101a8e1175bSopenharmony_ci} mbedtls_psa_aead_operation_t;
102a8e1175bSopenharmony_ci
103a8e1175bSopenharmony_ci#define MBEDTLS_PSA_AEAD_OPERATION_INIT { 0, 0, 0, 0, { 0 } }
104a8e1175bSopenharmony_ci
105a8e1175bSopenharmony_ci#include "mbedtls/ecdsa.h"
106a8e1175bSopenharmony_ci
107a8e1175bSopenharmony_ci/* Context structure for the Mbed TLS interruptible sign hash implementation. */
108a8e1175bSopenharmony_citypedef struct {
109a8e1175bSopenharmony_ci#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
110a8e1175bSopenharmony_ci    defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
111a8e1175bSopenharmony_ci    defined(MBEDTLS_ECP_RESTARTABLE)
112a8e1175bSopenharmony_ci    mbedtls_ecdsa_context *MBEDTLS_PRIVATE(ctx);
113a8e1175bSopenharmony_ci    mbedtls_ecdsa_restart_ctx MBEDTLS_PRIVATE(restart_ctx);
114a8e1175bSopenharmony_ci
115a8e1175bSopenharmony_ci    uint32_t MBEDTLS_PRIVATE(num_ops);
116a8e1175bSopenharmony_ci
117a8e1175bSopenharmony_ci    size_t MBEDTLS_PRIVATE(coordinate_bytes);
118a8e1175bSopenharmony_ci    psa_algorithm_t MBEDTLS_PRIVATE(alg);
119a8e1175bSopenharmony_ci    mbedtls_md_type_t MBEDTLS_PRIVATE(md_alg);
120a8e1175bSopenharmony_ci    uint8_t MBEDTLS_PRIVATE(hash)[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
121a8e1175bSopenharmony_ci    size_t MBEDTLS_PRIVATE(hash_length);
122a8e1175bSopenharmony_ci
123a8e1175bSopenharmony_ci#else
124a8e1175bSopenharmony_ci    /* Make the struct non-empty if algs not supported. */
125a8e1175bSopenharmony_ci    unsigned MBEDTLS_PRIVATE(dummy);
126a8e1175bSopenharmony_ci
127a8e1175bSopenharmony_ci#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
128a8e1175bSopenharmony_ci        * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
129a8e1175bSopenharmony_ci        * defined( MBEDTLS_ECP_RESTARTABLE ) */
130a8e1175bSopenharmony_ci} mbedtls_psa_sign_hash_interruptible_operation_t;
131a8e1175bSopenharmony_ci
132a8e1175bSopenharmony_ci#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
133a8e1175bSopenharmony_ci    defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
134a8e1175bSopenharmony_ci    defined(MBEDTLS_ECP_RESTARTABLE)
135a8e1175bSopenharmony_ci#define MBEDTLS_PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { { 0 }, { 0 }, 0, 0, 0, 0, 0, 0 }
136a8e1175bSopenharmony_ci#else
137a8e1175bSopenharmony_ci#define MBEDTLS_PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
138a8e1175bSopenharmony_ci#endif
139a8e1175bSopenharmony_ci
140a8e1175bSopenharmony_ci/* Context structure for the Mbed TLS interruptible verify hash
141a8e1175bSopenharmony_ci * implementation.*/
142a8e1175bSopenharmony_citypedef struct {
143a8e1175bSopenharmony_ci#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
144a8e1175bSopenharmony_ci    defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
145a8e1175bSopenharmony_ci    defined(MBEDTLS_ECP_RESTARTABLE)
146a8e1175bSopenharmony_ci
147a8e1175bSopenharmony_ci    mbedtls_ecdsa_context *MBEDTLS_PRIVATE(ctx);
148a8e1175bSopenharmony_ci    mbedtls_ecdsa_restart_ctx MBEDTLS_PRIVATE(restart_ctx);
149a8e1175bSopenharmony_ci
150a8e1175bSopenharmony_ci    uint32_t MBEDTLS_PRIVATE(num_ops);
151a8e1175bSopenharmony_ci
152a8e1175bSopenharmony_ci    uint8_t MBEDTLS_PRIVATE(hash)[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
153a8e1175bSopenharmony_ci    size_t MBEDTLS_PRIVATE(hash_length);
154a8e1175bSopenharmony_ci
155a8e1175bSopenharmony_ci    mbedtls_mpi MBEDTLS_PRIVATE(r);
156a8e1175bSopenharmony_ci    mbedtls_mpi MBEDTLS_PRIVATE(s);
157a8e1175bSopenharmony_ci
158a8e1175bSopenharmony_ci#else
159a8e1175bSopenharmony_ci    /* Make the struct non-empty if algs not supported. */
160a8e1175bSopenharmony_ci    unsigned MBEDTLS_PRIVATE(dummy);
161a8e1175bSopenharmony_ci
162a8e1175bSopenharmony_ci#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
163a8e1175bSopenharmony_ci        * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
164a8e1175bSopenharmony_ci        * defined( MBEDTLS_ECP_RESTARTABLE ) */
165a8e1175bSopenharmony_ci
166a8e1175bSopenharmony_ci} mbedtls_psa_verify_hash_interruptible_operation_t;
167a8e1175bSopenharmony_ci
168a8e1175bSopenharmony_ci#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
169a8e1175bSopenharmony_ci    defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
170a8e1175bSopenharmony_ci    defined(MBEDTLS_ECP_RESTARTABLE)
171a8e1175bSopenharmony_ci#define MBEDTLS_VERIFY_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { { 0 }, { 0 }, 0, 0, 0, 0, { 0 }, \
172a8e1175bSopenharmony_ci        { 0 } }
173a8e1175bSopenharmony_ci#else
174a8e1175bSopenharmony_ci#define MBEDTLS_VERIFY_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
175a8e1175bSopenharmony_ci#endif
176a8e1175bSopenharmony_ci
177a8e1175bSopenharmony_ci
178a8e1175bSopenharmony_ci/* EC-JPAKE operation definitions */
179a8e1175bSopenharmony_ci
180a8e1175bSopenharmony_ci#include "mbedtls/ecjpake.h"
181a8e1175bSopenharmony_ci
182a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
183a8e1175bSopenharmony_ci#define MBEDTLS_PSA_BUILTIN_PAKE  1
184a8e1175bSopenharmony_ci#endif
185a8e1175bSopenharmony_ci
186a8e1175bSopenharmony_ci/* Note: the format for mbedtls_ecjpake_read/write function has an extra
187a8e1175bSopenharmony_ci * length byte for each step, plus an extra 3 bytes for ECParameters in the
188a8e1175bSopenharmony_ci * server's 2nd round. */
189a8e1175bSopenharmony_ci#define MBEDTLS_PSA_JPAKE_BUFFER_SIZE ((3 + 1 + 65 + 1 + 65 + 1 + 32) * 2)
190a8e1175bSopenharmony_ci
191a8e1175bSopenharmony_citypedef struct {
192a8e1175bSopenharmony_ci    psa_algorithm_t MBEDTLS_PRIVATE(alg);
193a8e1175bSopenharmony_ci
194a8e1175bSopenharmony_ci    uint8_t *MBEDTLS_PRIVATE(password);
195a8e1175bSopenharmony_ci    size_t MBEDTLS_PRIVATE(password_len);
196a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
197a8e1175bSopenharmony_ci    mbedtls_ecjpake_role MBEDTLS_PRIVATE(role);
198a8e1175bSopenharmony_ci    uint8_t MBEDTLS_PRIVATE(buffer[MBEDTLS_PSA_JPAKE_BUFFER_SIZE]);
199a8e1175bSopenharmony_ci    size_t MBEDTLS_PRIVATE(buffer_length);
200a8e1175bSopenharmony_ci    size_t MBEDTLS_PRIVATE(buffer_offset);
201a8e1175bSopenharmony_ci#endif
202a8e1175bSopenharmony_ci    /* Context structure for the Mbed TLS EC-JPAKE implementation. */
203a8e1175bSopenharmony_ci    union {
204a8e1175bSopenharmony_ci        unsigned int MBEDTLS_PRIVATE(dummy);
205a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
206a8e1175bSopenharmony_ci        mbedtls_ecjpake_context MBEDTLS_PRIVATE(jpake);
207a8e1175bSopenharmony_ci#endif
208a8e1175bSopenharmony_ci    } MBEDTLS_PRIVATE(ctx);
209a8e1175bSopenharmony_ci
210a8e1175bSopenharmony_ci} mbedtls_psa_pake_operation_t;
211a8e1175bSopenharmony_ci
212a8e1175bSopenharmony_ci#define MBEDTLS_PSA_PAKE_OPERATION_INIT { { 0 } }
213a8e1175bSopenharmony_ci
214a8e1175bSopenharmony_ci#endif /* PSA_CRYPTO_BUILTIN_COMPOSITES_H */
215