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