Mbed TLS v4.1.1
Loading...
Searching...
No Matches
crypto_struct.h
Go to the documentation of this file.
1
44/*
45 * Copyright The Mbed TLS Contributors
46 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
47 */
48
49#ifndef PSA_CRYPTO_STRUCT_H
50#define PSA_CRYPTO_STRUCT_H
51#include "mbedtls/private_access.h"
52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
58
59/* Include the context definition for the compiled-in drivers for the primitive
60 * algorithms. */
62
64#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
65 mbedtls_psa_client_handle_t handle;
66#else
73 unsigned int MBEDTLS_PRIVATE(id);
74 psa_driver_hash_context_t MBEDTLS_PRIVATE(ctx);
75#endif
76};
77#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
78#define PSA_HASH_OPERATION_INIT { 0 }
79#else
80#define PSA_HASH_OPERATION_INIT { 0, { 0 } }
81#endif
83{
85 return v;
86}
87
89#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
90 mbedtls_psa_client_handle_t handle;
91#else
98 unsigned int MBEDTLS_PRIVATE(id);
99
100 /* Algorithm properties */
101 unsigned requires_context : 1;
102 unsigned allows_context : 1;
103
104 /* State tracking */
105 unsigned active : 1;
106 unsigned has_context : 1;
107 unsigned has_input : 1;
108 unsigned has_output : 1;
109
110 psa_driver_xof_context_t MBEDTLS_PRIVATE(ctx);
111#endif
112};
113#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
114#define PSA_XOF_OPERATION_INIT { 0 }
115#else
116#define PSA_XOF_OPERATION_INIT { 0, 0, 0, 0, 0, 0, 0, { 0 } }
117#endif
119{
121 return v;
122}
123
125#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
126 mbedtls_psa_client_handle_t handle;
127#else
134 unsigned int MBEDTLS_PRIVATE(id);
135
136 unsigned int MBEDTLS_PRIVATE(iv_required) : 1;
137 unsigned int MBEDTLS_PRIVATE(iv_set) : 1;
138
139 uint8_t MBEDTLS_PRIVATE(default_iv_length);
140
141 psa_driver_cipher_context_t MBEDTLS_PRIVATE(ctx);
142#endif
143};
144
145#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
146#define PSA_CIPHER_OPERATION_INIT { 0 }
147#else
148#define PSA_CIPHER_OPERATION_INIT { 0, 0, 0, 0, { 0 } }
149#endif
151{
153 return v;
154}
155
156/* Include the context definition for the compiled-in drivers for the composite
157 * algorithms. */
159
160/* Include context definitions from the PQCP driver.
161 * (There are none yet at the time of writing, that will come with multipart
162 * ML-DSA sign/verify).
163 */
164#if defined(TF_PSA_CRYPTO_PQCP_MLDSA_ENABLED)
165#include "tf-psa-crypto/private/crypto_struct_pqcp.h"
166#endif
167
169#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
170 mbedtls_psa_client_handle_t handle;
171#else
178 unsigned int MBEDTLS_PRIVATE(id);
179 uint8_t MBEDTLS_PRIVATE(mac_size);
180 unsigned int MBEDTLS_PRIVATE(is_sign) : 1;
181 psa_driver_mac_context_t MBEDTLS_PRIVATE(ctx);
182#endif
183};
184
185#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
186#define PSA_MAC_OPERATION_INIT { 0 }
187#else
188#define PSA_MAC_OPERATION_INIT { 0, 0, 0, { 0 } }
189#endif
191{
193 return v;
194}
195
197#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
198 mbedtls_psa_client_handle_t handle;
199#else
206 unsigned int MBEDTLS_PRIVATE(id);
207
208 psa_algorithm_t MBEDTLS_PRIVATE(alg);
209 psa_key_type_t MBEDTLS_PRIVATE(key_type);
210
211 size_t MBEDTLS_PRIVATE(ad_remaining);
212 size_t MBEDTLS_PRIVATE(body_remaining);
213
214 unsigned int MBEDTLS_PRIVATE(nonce_set) : 1;
215 unsigned int MBEDTLS_PRIVATE(lengths_set) : 1;
216 unsigned int MBEDTLS_PRIVATE(ad_started) : 1;
217 unsigned int MBEDTLS_PRIVATE(body_started) : 1;
218 unsigned int MBEDTLS_PRIVATE(is_encrypt) : 1;
219
220 psa_driver_aead_context_t MBEDTLS_PRIVATE(ctx);
221#endif
222};
223
224#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
225#define PSA_AEAD_OPERATION_INIT { 0 }
226#else
227#define PSA_AEAD_OPERATION_INIT { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0 } }
228#endif
230{
232 return v;
233}
234
235/* Include the context definition for the compiled-in drivers for the key
236 * derivation algorithms. */
238
240#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
241 mbedtls_psa_client_handle_t handle;
242#else
243 psa_algorithm_t MBEDTLS_PRIVATE(alg);
244 unsigned int MBEDTLS_PRIVATE(can_output_key) : 1;
245 size_t MBEDTLS_PRIVATE(capacity);
247#endif
248};
249
250#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
251#define PSA_KEY_DERIVATION_OPERATION_INIT { 0 }
252#else
253/* This only zeroes out the first byte in the union, the rest is unspecified. */
254#define PSA_KEY_DERIVATION_OPERATION_INIT { 0, 0, 0, { 0 } }
255#endif
257 void)
258{
260 return v;
261}
262
264 /* Future versions may add other fields in this structure. */
265 uint32_t flags;
266};
267
275#define PSA_CUSTOM_KEY_PARAMETERS_INIT { 0 }
276
278 psa_key_usage_t MBEDTLS_PRIVATE(usage);
279 psa_algorithm_t MBEDTLS_PRIVATE(alg);
280 psa_algorithm_t MBEDTLS_PRIVATE(alg2);
281};
283
284#define PSA_KEY_POLICY_INIT { 0, 0, 0 }
285static inline struct psa_key_policy_s psa_key_policy_init(void)
286{
287 const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT;
288 return v;
289}
290
291/* The type used internally for key sizes.
292 * Public interfaces use size_t, but internally we use a smaller type. */
293typedef uint16_t psa_key_bits_t;
294/* The maximum value of the type used to represent bit-sizes.
295 * This is used to mark an invalid key size. */
296#define PSA_KEY_BITS_TOO_LARGE ((psa_key_bits_t) -1)
297/* The maximum size of a key in bits.
298 * Currently defined as the maximum that can be represented, rounded down
299 * to a whole number of bytes.
300 * This is an uncast value so that it can be used in preprocessor
301 * conditionals. */
302#define PSA_MAX_KEY_BITS 0xfff8
303
305 psa_key_type_t MBEDTLS_PRIVATE(type);
306 psa_key_bits_t MBEDTLS_PRIVATE(bits);
307 psa_key_lifetime_t MBEDTLS_PRIVATE(lifetime);
308 psa_key_policy_t MBEDTLS_PRIVATE(policy);
309 /* This type has a different layout in the client view wrt the
310 * service view of the key id, i.e. in service view usually is
311 * expected to have MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined
312 * thus adding an owner field to the standard psa_key_id_t. For
313 * implementations with client/service separation, this means the
314 * object will be marshalled through a transport channel and
315 * interpreted differently at each side of the transport. Placing
316 * it at the end of structures allows to interpret the structure
317 * at the client without reorganizing the memory layout of the
318 * struct
319 */
320 mbedtls_svc_key_id_t MBEDTLS_PRIVATE(id);
321};
322
323#define PSA_KEY_ATTRIBUTES_INIT { PSA_KEY_TYPE_NONE, 0, \
324 PSA_KEY_LIFETIME_VOLATILE, \
325 PSA_KEY_POLICY_INIT, \
326 MBEDTLS_SVC_KEY_ID_INIT }
327
329{
331 return v;
332}
333
334static inline void psa_set_key_id(psa_key_attributes_t *attributes,
336{
337 psa_key_lifetime_t lifetime = attributes->MBEDTLS_PRIVATE(lifetime);
338
339 attributes->MBEDTLS_PRIVATE(id) = key;
340
341 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
342 attributes->MBEDTLS_PRIVATE(lifetime) =
346 }
347}
348
350 const psa_key_attributes_t *attributes)
351{
352 return attributes->MBEDTLS_PRIVATE(id);
353}
354
355#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
356static inline void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes,
357 mbedtls_key_owner_id_t owner)
358{
359 attributes->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(owner) = owner;
360}
361#endif
362
363static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
364 psa_key_lifetime_t lifetime)
365{
366 attributes->MBEDTLS_PRIVATE(lifetime) = lifetime;
367 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
368#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
369 attributes->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id) = 0;
370#else
371 attributes->MBEDTLS_PRIVATE(id) = 0;
372#endif
373 }
374}
375
377 const psa_key_attributes_t *attributes)
378{
379 return attributes->MBEDTLS_PRIVATE(lifetime);
380}
381
382static inline void psa_extend_key_usage_flags(psa_key_usage_t *usage_flags)
383{
384 if (*usage_flags & PSA_KEY_USAGE_SIGN_HASH) {
385 *usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE;
386 }
387
388 if (*usage_flags & PSA_KEY_USAGE_VERIFY_HASH) {
389 *usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE;
390 }
391}
392
393static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
394 psa_key_usage_t usage_flags)
395{
396 psa_extend_key_usage_flags(&usage_flags);
397 attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage) = usage_flags;
398}
399
401 const psa_key_attributes_t *attributes)
402{
403 return attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage);
404}
405
406static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
407 psa_algorithm_t alg)
408{
409 attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg) = alg;
410}
411
413 const psa_key_attributes_t *attributes)
414{
415 return attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg);
416}
417
418static inline void psa_set_key_type(psa_key_attributes_t *attributes,
419 psa_key_type_t type)
420{
421 attributes->MBEDTLS_PRIVATE(type) = type;
422}
423
425 const psa_key_attributes_t *attributes)
426{
427 return attributes->MBEDTLS_PRIVATE(type);
428}
429
430static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
431 size_t bits)
432{
433 if (bits > PSA_MAX_KEY_BITS) {
434 attributes->MBEDTLS_PRIVATE(bits) = PSA_KEY_BITS_TOO_LARGE;
435 } else {
436 attributes->MBEDTLS_PRIVATE(bits) = (psa_key_bits_t) bits;
437 }
438}
439
440static inline size_t psa_get_key_bits(
441 const psa_key_attributes_t *attributes)
442{
443 return attributes->MBEDTLS_PRIVATE(bits);
444}
445
450#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
451 mbedtls_psa_client_handle_t handle;
452#else
459 unsigned int MBEDTLS_PRIVATE(id);
460
462
463 unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
464
465 uint32_t MBEDTLS_PRIVATE(num_ops);
466#endif
467};
468
469#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
470#define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
471#else
472#define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
473#endif
474
483
488#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
489 mbedtls_psa_client_handle_t handle;
490#else
497 unsigned int MBEDTLS_PRIVATE(id);
498
500
501 unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
502
503 uint32_t MBEDTLS_PRIVATE(num_ops);
504#endif
505};
506
507#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
508#define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
509#else
510#define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
511#endif
512
521
526#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
527 mbedtls_psa_client_handle_t handle;
528#else
537 unsigned int MBEDTLS_PRIVATE(id);
538 mbedtls_psa_key_agreement_interruptible_operation_t MBEDTLS_PRIVATE(mbedtls_ctx);
539 uint32_t MBEDTLS_PRIVATE(num_ops);
540 psa_key_attributes_t MBEDTLS_PRIVATE(attributes);
541 unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
542#endif
543};
544
545#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
546#define PSA_KEY_AGREEMENT_IOP_INIT { 0 }
547#else
548#define PSA_KEY_AGREEMENT_IOP_INIT { 0, MBEDTLS_PSA_KEY_AGREEMENT_IOP_INIT, 0, \
549 PSA_KEY_ATTRIBUTES_INIT, 0 }
550#endif
551
552static inline struct psa_key_agreement_iop_s
554{
556
557 return v;
558}
559
564#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
565 mbedtls_psa_client_handle_t handle;
566#else
575 unsigned int MBEDTLS_PRIVATE(id);
576 mbedtls_psa_generate_key_iop_t MBEDTLS_PRIVATE(ctx);
577 psa_key_attributes_t MBEDTLS_PRIVATE(attributes);
578 unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
579 uint32_t MBEDTLS_PRIVATE(num_ops);
580#endif
581};
582
583#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
584#define PSA_GENERATE_KEY_IOP_INIT { 0 }
585#else
586#define PSA_GENERATE_KEY_IOP_INIT { 0, MBEDTLS_PSA_GENERATE_KEY_IOP_INIT, PSA_KEY_ATTRIBUTES_INIT, \
587 0, 0 }
588#endif
589
590static inline struct psa_generate_key_iop_s
592{
594
595 return v;
596}
597
602#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
603 mbedtls_psa_client_handle_t handle;
604#else
613 unsigned int MBEDTLS_PRIVATE(id);
614 mbedtls_psa_export_public_key_iop_t MBEDTLS_PRIVATE(ctx);
615 unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
616 uint32_t MBEDTLS_PRIVATE(num_ops);
617#endif
618};
619
620#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
621#define PSA_EXPORT_PUBLIC_KEY_IOP_INIT { 0 }
622#else
623#define PSA_EXPORT_PUBLIC_KEY_IOP_INIT { 0, MBEDTLS_PSA_EXPORT_PUBLIC_KEY_IOP_INIT, 0, 0 }
624#endif
625
626static inline struct psa_export_public_key_iop_s
633
634#ifdef __cplusplus
635}
636#endif
637
638#endif /* PSA_CRYPTO_STRUCT_H */
Declaration of context structures for use with the PSA driver wrapper interface. This file contains t...
Declaration of context structures for use with the PSA driver wrapper interface. This file contains t...
Declaration of context structures for use with the PSA driver wrapper interface. This file contains t...
#define PSA_KEY_BITS_TOO_LARGE
static struct psa_sign_hash_interruptible_operation_s psa_sign_hash_interruptible_operation_init(void)
static void psa_set_key_usage_flags(psa_key_attributes_t *attributes, psa_key_usage_t usage_flags)
static struct psa_verify_hash_interruptible_operation_s psa_verify_hash_interruptible_operation_init(void)
uint16_t psa_key_bits_t
static size_t psa_get_key_bits(const psa_key_attributes_t *attributes)
#define PSA_KEY_POLICY_INIT
static void psa_set_key_type(psa_key_attributes_t *attributes, psa_key_type_t type)
static struct psa_key_agreement_iop_s psa_key_agreement_iop_init(void)
#define PSA_KEY_AGREEMENT_IOP_INIT
#define PSA_MAX_KEY_BITS
static psa_key_lifetime_t psa_get_key_lifetime(const psa_key_attributes_t *attributes)
static psa_key_usage_t psa_get_key_usage_flags(const psa_key_attributes_t *attributes)
static void psa_extend_key_usage_flags(psa_key_usage_t *usage_flags)
static void psa_set_key_lifetime(psa_key_attributes_t *attributes, psa_key_lifetime_t lifetime)
static struct psa_generate_key_iop_s psa_generate_key_iop_init(void)
static psa_algorithm_t psa_get_key_algorithm(const psa_key_attributes_t *attributes)
#define PSA_EXPORT_PUBLIC_KEY_IOP_INIT
static struct psa_export_public_key_iop_s psa_export_public_key_iop_init(void)
#define PSA_GENERATE_KEY_IOP_INIT
static void psa_set_key_id(psa_key_attributes_t *attributes, mbedtls_svc_key_id_t key)
static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes)
#define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT
static void psa_set_key_algorithm(psa_key_attributes_t *attributes, psa_algorithm_t alg)
#define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT
struct psa_key_policy_s psa_key_policy_t
static void psa_set_key_bits(psa_key_attributes_t *attributes, size_t bits)
static mbedtls_svc_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes)
static struct psa_key_policy_s psa_key_policy_init(void)
#define PSA_MAC_OPERATION_INIT
static psa_mac_operation_t psa_mac_operation_init(void)
static psa_xof_operation_t psa_xof_operation_init(void)
#define PSA_XOF_OPERATION_INIT
static psa_aead_operation_t psa_aead_operation_init(void)
#define PSA_AEAD_OPERATION_INIT
static psa_key_attributes_t psa_key_attributes_init(void)
struct psa_key_attributes_s psa_key_attributes_t
#define PSA_KEY_ATTRIBUTES_INIT
static psa_cipher_operation_t psa_cipher_operation_init(void)
#define PSA_CIPHER_OPERATION_INIT
uint16_t psa_key_type_t
Encoding of a key type.
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
static psa_hash_operation_t psa_hash_operation_init(void)
#define PSA_HASH_OPERATION_INIT
#define PSA_KEY_DERIVATION_OPERATION_INIT
static psa_key_derivation_operation_t psa_key_derivation_operation_init(void)
#define PSA_KEY_LIFETIME_PERSISTENT
#define PSA_KEY_LIFETIME_GET_LOCATION(lifetime)
#define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)
uint32_t psa_key_lifetime_t
psa_key_id_t mbedtls_svc_key_id_t
#define PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(persistence, location)
#define PSA_KEY_USAGE_SIGN_HASH
#define PSA_KEY_USAGE_SIGN_MESSAGE
uint32_t psa_key_usage_t
Encoding of permitted usage on a key.
#define PSA_KEY_USAGE_VERIFY_MESSAGE
#define PSA_KEY_USAGE_VERIFY_HASH
The context for PSA interruptible export public-key.
The context for PSA interruptible key generation.
The context for PSA interruptible key agreement.
The context for PSA interruptible hash signing.
The context for PSA interruptible hash verification.
Build-time configuration info.