Mbed TLS v4.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
crypto_builtin_composites.h
Go to the documentation of this file.
1 
18 /*
19  * Copyright The Mbed TLS Contributors
20  * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
21  */
22 
23 #ifndef PSA_CRYPTO_BUILTIN_COMPOSITES_H
24 #define PSA_CRYPTO_BUILTIN_COMPOSITES_H
25 #include "mbedtls/private_access.h"
26 
28 
29 #include "mbedtls/private/cmac.h"
30 #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
31 #include "mbedtls/private/gcm.h"
32 #endif
33 #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
34 #include "mbedtls/private/ccm.h"
35 #endif
36 #include "mbedtls/private/chachapoly.h"
37 
38 #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
39 #include "mbedtls/private/ecdh.h"
40 #endif
41 
42 /*
43  * MAC multi-part operation definitions.
44  */
45 #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) || \
46  defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
47 #define MBEDTLS_PSA_BUILTIN_MAC
48 #endif
49 
50 #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || defined(PSA_CRYPTO_DRIVER_TEST)
51 typedef struct {
53  psa_algorithm_t MBEDTLS_PRIVATE(alg);
55  struct psa_hash_operation_s hash_ctx;
57  uint8_t MBEDTLS_PRIVATE(opad)[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
59 
60 #define MBEDTLS_PSA_HMAC_OPERATION_INIT { 0, PSA_HASH_OPERATION_INIT, { 0 } }
61 #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
62 
63 typedef struct {
64  psa_algorithm_t MBEDTLS_PRIVATE(alg);
65  union {
66  unsigned MBEDTLS_PRIVATE(dummy); /* Make the union non-empty even with no supported algorithms. */
67 #if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || defined(PSA_CRYPTO_DRIVER_TEST)
68  mbedtls_psa_hmac_operation_t MBEDTLS_PRIVATE(hmac);
69 #endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
70 #if defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) || defined(PSA_CRYPTO_DRIVER_TEST)
71  mbedtls_cipher_context_t MBEDTLS_PRIVATE(cmac);
72 #endif /* MBEDTLS_PSA_BUILTIN_ALG_CMAC */
73  } MBEDTLS_PRIVATE(ctx);
75 
76 #define MBEDTLS_PSA_MAC_OPERATION_INIT { 0, { 0 } }
77 
78 #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) || \
79  defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) || \
80  defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
81 #define MBEDTLS_PSA_BUILTIN_AEAD 1
82 #endif
83 
84 /* Context structure for the Mbed TLS AEAD implementation. */
85 typedef struct {
86  psa_algorithm_t MBEDTLS_PRIVATE(alg);
87  psa_key_type_t MBEDTLS_PRIVATE(key_type);
88 
89  unsigned int MBEDTLS_PRIVATE(is_encrypt) : 1;
90 
91  uint8_t MBEDTLS_PRIVATE(tag_length);
92 
93  union {
94  unsigned dummy; /* Enable easier initializing of the union. */
95 #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
96  mbedtls_ccm_context MBEDTLS_PRIVATE(ccm);
97 #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */
98 #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
99  mbedtls_gcm_context MBEDTLS_PRIVATE(gcm);
100 #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */
101 #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
102  mbedtls_chachapoly_context MBEDTLS_PRIVATE(chachapoly);
103 #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */
104 
105  } ctx;
106 
108 
109 #define MBEDTLS_PSA_AEAD_OPERATION_INIT { 0, 0, 0, 0, { 0 } }
110 
111 #include "mbedtls/private/ecdsa.h"
112 
113 /* Context structure for the Mbed TLS interruptible sign hash implementation. */
114 typedef struct {
115 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
116  defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
117  defined(MBEDTLS_ECP_RESTARTABLE)
118  mbedtls_ecdsa_context *MBEDTLS_PRIVATE(ctx);
119  mbedtls_ecdsa_restart_ctx MBEDTLS_PRIVATE(restart_ctx);
120 
121  uint32_t MBEDTLS_PRIVATE(num_ops);
122 
123  size_t MBEDTLS_PRIVATE(coordinate_bytes);
124  psa_algorithm_t MBEDTLS_PRIVATE(alg);
125  mbedtls_md_type_t MBEDTLS_PRIVATE(md_alg);
126  uint8_t MBEDTLS_PRIVATE(hash)[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
127  size_t MBEDTLS_PRIVATE(hash_length);
128 
129 #else
130  /* Make the struct non-empty if algs not supported. */
131  unsigned MBEDTLS_PRIVATE(dummy);
132 
133 #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
134  * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
135  * defined( MBEDTLS_ECP_RESTARTABLE ) */
137 
138 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
139  defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
140  defined(MBEDTLS_ECP_RESTARTABLE)
141 #define MBEDTLS_PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { { 0 }, { 0 }, 0, 0, 0, 0, 0, 0 }
142 #else
143 #define MBEDTLS_PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
144 #endif
145 
146 /* Context structure for the Mbed TLS interruptible verify hash
147  * implementation.*/
148 typedef struct {
149 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
150  defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
151  defined(MBEDTLS_ECP_RESTARTABLE)
152 
153  mbedtls_ecdsa_context *MBEDTLS_PRIVATE(ctx);
154  mbedtls_ecdsa_restart_ctx MBEDTLS_PRIVATE(restart_ctx);
155 
156  uint32_t MBEDTLS_PRIVATE(num_ops);
157 
158  uint8_t MBEDTLS_PRIVATE(hash)[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
159  size_t MBEDTLS_PRIVATE(hash_length);
160 
161  mbedtls_mpi MBEDTLS_PRIVATE(r);
162  mbedtls_mpi MBEDTLS_PRIVATE(s);
163 
164 #else
165  /* Make the struct non-empty if algs not supported. */
166  unsigned MBEDTLS_PRIVATE(dummy);
167 
168 #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
169  * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) &&
170  * defined( MBEDTLS_ECP_RESTARTABLE ) */
171 
173 
174 #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
175  defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
176  defined(MBEDTLS_ECP_RESTARTABLE)
177 #define MBEDTLS_VERIFY_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { { 0 }, { 0 }, 0, 0, 0, 0, { 0 }, \
178  { 0 } }
179 #else
180 #define MBEDTLS_VERIFY_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
181 #endif
182 
183 
184 /* EC-JPAKE operation definitions */
185 
186 #include "mbedtls/private/ecjpake.h"
187 
188 #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
189 #define MBEDTLS_PSA_BUILTIN_PAKE 1
190 #endif
191 
192 /* Note: the format for mbedtls_ecjpake_read/write function has an extra
193  * length byte for each step, plus an extra 3 bytes for ECParameters in the
194  * server's 2nd round. */
195 #define MBEDTLS_PSA_JPAKE_BUFFER_SIZE ((3 + 1 + 65 + 1 + 65 + 1 + 32) * 2)
196 
197 typedef struct {
198  psa_algorithm_t MBEDTLS_PRIVATE(alg);
199 
200  uint8_t *MBEDTLS_PRIVATE(password);
201  size_t MBEDTLS_PRIVATE(password_len);
202 #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
203  mbedtls_ecjpake_role MBEDTLS_PRIVATE(role);
204  uint8_t MBEDTLS_PRIVATE(buffer[MBEDTLS_PSA_JPAKE_BUFFER_SIZE]);
205  size_t MBEDTLS_PRIVATE(buffer_length);
206  size_t MBEDTLS_PRIVATE(buffer_offset);
207 #endif
208  /* Context structure for the Mbed TLS EC-JPAKE implementation. */
209  union {
210  unsigned int MBEDTLS_PRIVATE(dummy);
211 #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
212  mbedtls_ecjpake_context MBEDTLS_PRIVATE(jpake);
213 #endif
214  } MBEDTLS_PRIVATE(ctx);
215 
217 
218 #define MBEDTLS_PSA_PAKE_OPERATION_INIT { { 0 } }
219 
220 typedef struct {
221 #if defined(MBEDTLS_ECP_C)
222  mbedtls_ecp_keypair MBEDTLS_PRIVATE(ecp);
223  uint32_t num_ops;
224 #else
225  /* Make the struct non-empty if algs not supported. */
226  unsigned MBEDTLS_PRIVATE(dummy);
227 #endif
229 
230 #if defined(MBEDTLS_ECP_C)
231 #define MBEDTLS_PSA_GENERATE_KEY_IOP_INIT { MBEDTLS_ECP_KEYPAIR_INIT, 0 }
232 #else
233 #define MBEDTLS_PSA_GENERATE_KEY_IOP_INIT { 0 }
234 #endif
235 
236 /* Context structure for the Mbed TLS interruptible key agreement implementation. */
237 typedef struct {
238 #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) && defined(MBEDTLS_ECP_RESTARTABLE)
239  mbedtls_ecdh_context MBEDTLS_PRIVATE(ctx);
240  uint32_t MBEDTLS_PRIVATE(num_ops);
241 #else
242  /* Make the struct non-empty if algs not supported. */
243  unsigned MBEDTLS_PRIVATE(dummy);
244 #endif
246 
247 #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) && defined(MBEDTLS_ECP_RESTARTABLE)
248 #define MBEDTLS_PSA_KEY_AGREEMENT_IOP_INIT { MBEDTLS_ECDH_CONTEXT_INIT, 0 }
249 #else
250 #define MBEDTLS_PSA_KEY_AGREEMENT_IOP_INIT { 0 }
251 #endif
252 
253 /* Context structure for the Mbed TLS interruptible export public-key implementation. */
254 typedef struct {
255 #if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ECP_RESTARTABLE)
256  mbedtls_ecp_keypair *MBEDTLS_PRIVATE(key);
257  mbedtls_ecp_restart_ctx MBEDTLS_PRIVATE(restart_ctx);
258  uint32_t MBEDTLS_PRIVATE(num_ops);
259 #else
260  /* Make the struct non-empty if algs not supported. */
261  unsigned MBEDTLS_PRIVATE(dummy);
262 #endif
264 
265 #if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ECP_RESTARTABLE)
266 #define MBEDTLS_PSA_EXPORT_PUBLIC_KEY_IOP_INIT { NULL, MBEDTLS_ECP_RESTART_INIT, 0 }
267 #else
268 #define MBEDTLS_PSA_EXPORT_PUBLIC_KEY_IOP_INIT { 0 }
269 #endif
270 
271 #endif /* PSA_CRYPTO_BUILTIN_COMPOSITES_H */
#define PSA_HMAC_MAX_HASH_BLOCK_SIZE
Definition: crypto_sizes.h:115
#define MBEDTLS_PSA_JPAKE_BUFFER_SIZE
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
Definition: crypto_types.h:127
uint16_t psa_key_type_t
Encoding of a key type.
Definition: crypto_types.h:71
Definitions for all PSA crypto drivers.
mbedtls_md_type_t
Supported message digests.
Definition: md.h:50
#define PSA_VENDOR_ECC_MAX_CURVE_BITS
Definition: crypto_sizes.h:238
#define PSA_BITS_TO_BYTES(bits)
Definition: crypto_sizes.h:33