Mbed TLS v4.1.2
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
161#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
162 mbedtls_psa_client_handle_t handle;
163#else
170 unsigned int MBEDTLS_PRIVATE(id);
171 uint8_t MBEDTLS_PRIVATE(mac_size);
172 unsigned int MBEDTLS_PRIVATE(is_sign) : 1;
173 psa_driver_mac_context_t MBEDTLS_PRIVATE(ctx);
174#endif
175};
176
177#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
178#define PSA_MAC_OPERATION_INIT { 0 }
179#else
180#define PSA_MAC_OPERATION_INIT { 0, 0, 0, { 0 } }
181#endif
183{
185 return v;
186}
187
189#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
190 mbedtls_psa_client_handle_t handle;
191#else
198 unsigned int MBEDTLS_PRIVATE(id);
199
200 psa_algorithm_t MBEDTLS_PRIVATE(alg);
201 psa_key_type_t MBEDTLS_PRIVATE(key_type);
202
203 size_t MBEDTLS_PRIVATE(ad_remaining);
204 size_t MBEDTLS_PRIVATE(body_remaining);
205
206 unsigned int MBEDTLS_PRIVATE(nonce_set) : 1;
207 unsigned int MBEDTLS_PRIVATE(lengths_set) : 1;
208 unsigned int MBEDTLS_PRIVATE(ad_started) : 1;
209 unsigned int MBEDTLS_PRIVATE(body_started) : 1;
210 unsigned int MBEDTLS_PRIVATE(is_encrypt) : 1;
211
212 psa_driver_aead_context_t MBEDTLS_PRIVATE(ctx);
213#endif
214};
215
216#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
217#define PSA_AEAD_OPERATION_INIT { 0 }
218#else
219#define PSA_AEAD_OPERATION_INIT { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0 } }
220#endif
222{
224 return v;
225}
226
227/* Include the context definition for the compiled-in drivers for the key
228 * derivation algorithms. */
230
232#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
233 mbedtls_psa_client_handle_t handle;
234#else
235 psa_algorithm_t MBEDTLS_PRIVATE(alg);
236 unsigned int MBEDTLS_PRIVATE(can_output_key) : 1;
237 size_t MBEDTLS_PRIVATE(capacity);
239#endif
240};
241
242#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
243#define PSA_KEY_DERIVATION_OPERATION_INIT { 0 }
244#else
245/* This only zeroes out the first byte in the union, the rest is unspecified. */
246#define PSA_KEY_DERIVATION_OPERATION_INIT { 0, 0, 0, { 0 } }
247#endif
249 void)
250{
252 return v;
253}
254
256 /* Future versions may add other fields in this structure. */
257 uint32_t flags;
258};
259
267#define PSA_CUSTOM_KEY_PARAMETERS_INIT { 0 }
268
270 psa_key_usage_t MBEDTLS_PRIVATE(usage);
271 psa_algorithm_t MBEDTLS_PRIVATE(alg);
272 psa_algorithm_t MBEDTLS_PRIVATE(alg2);
273};
275
276#define PSA_KEY_POLICY_INIT { 0, 0, 0 }
277static inline struct psa_key_policy_s psa_key_policy_init(void)
278{
279 const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT;
280 return v;
281}
282
283/* The type used internally for key sizes.
284 * Public interfaces use size_t, but internally we use a smaller type. */
285typedef uint16_t psa_key_bits_t;
286/* The maximum value of the type used to represent bit-sizes.
287 * This is used to mark an invalid key size. */
288#define PSA_KEY_BITS_TOO_LARGE ((psa_key_bits_t) -1)
289/* The maximum size of a key in bits.
290 * Currently defined as the maximum that can be represented, rounded down
291 * to a whole number of bytes.
292 * This is an uncast value so that it can be used in preprocessor
293 * conditionals. */
294#define PSA_MAX_KEY_BITS 0xfff8
295
297 psa_key_type_t MBEDTLS_PRIVATE(type);
298 psa_key_bits_t MBEDTLS_PRIVATE(bits);
299 psa_key_lifetime_t MBEDTLS_PRIVATE(lifetime);
300 psa_key_policy_t MBEDTLS_PRIVATE(policy);
301 /* This type has a different layout in the client view wrt the
302 * service view of the key id, i.e. in service view usually is
303 * expected to have MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined
304 * thus adding an owner field to the standard psa_key_id_t. For
305 * implementations with client/service separation, this means the
306 * object will be marshalled through a transport channel and
307 * interpreted differently at each side of the transport. Placing
308 * it at the end of structures allows to interpret the structure
309 * at the client without reorganizing the memory layout of the
310 * struct
311 */
312 mbedtls_svc_key_id_t MBEDTLS_PRIVATE(id);
313};
314
315#define PSA_KEY_ATTRIBUTES_INIT { PSA_KEY_TYPE_NONE, 0, \
316 PSA_KEY_LIFETIME_VOLATILE, \
317 PSA_KEY_POLICY_INIT, \
318 MBEDTLS_SVC_KEY_ID_INIT }
319
321{
323 return v;
324}
325
326static inline void psa_set_key_id(psa_key_attributes_t *attributes,
328{
329 psa_key_lifetime_t lifetime = attributes->MBEDTLS_PRIVATE(lifetime);
330
331 attributes->MBEDTLS_PRIVATE(id) = key;
332
333 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
334 attributes->MBEDTLS_PRIVATE(lifetime) =
338 }
339}
340
342 const psa_key_attributes_t *attributes)
343{
344 return attributes->MBEDTLS_PRIVATE(id);
345}
346
347#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
348static inline void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes,
349 mbedtls_key_owner_id_t owner)
350{
351 attributes->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(owner) = owner;
352}
353#endif
354
355static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
356 psa_key_lifetime_t lifetime)
357{
358 attributes->MBEDTLS_PRIVATE(lifetime) = lifetime;
359 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
360#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
361 attributes->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id) = 0;
362#else
363 attributes->MBEDTLS_PRIVATE(id) = 0;
364#endif
365 }
366}
367
369 const psa_key_attributes_t *attributes)
370{
371 return attributes->MBEDTLS_PRIVATE(lifetime);
372}
373
374static inline void psa_extend_key_usage_flags(psa_key_usage_t *usage_flags)
375{
376 if (*usage_flags & PSA_KEY_USAGE_SIGN_HASH) {
377 *usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE;
378 }
379
380 if (*usage_flags & PSA_KEY_USAGE_VERIFY_HASH) {
381 *usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE;
382 }
383}
384
385static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
386 psa_key_usage_t usage_flags)
387{
388 psa_extend_key_usage_flags(&usage_flags);
389 attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage) = usage_flags;
390}
391
393 const psa_key_attributes_t *attributes)
394{
395 return attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage);
396}
397
398static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
399 psa_algorithm_t alg)
400{
401 attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg) = alg;
402}
403
405 const psa_key_attributes_t *attributes)
406{
407 return attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg);
408}
409
410static inline void psa_set_key_type(psa_key_attributes_t *attributes,
411 psa_key_type_t type)
412{
413 attributes->MBEDTLS_PRIVATE(type) = type;
414}
415
417 const psa_key_attributes_t *attributes)
418{
419 return attributes->MBEDTLS_PRIVATE(type);
420}
421
422static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
423 size_t bits)
424{
425 if (bits > PSA_MAX_KEY_BITS) {
426 attributes->MBEDTLS_PRIVATE(bits) = PSA_KEY_BITS_TOO_LARGE;
427 } else {
428 attributes->MBEDTLS_PRIVATE(bits) = (psa_key_bits_t) bits;
429 }
430}
431
432static inline size_t psa_get_key_bits(
433 const psa_key_attributes_t *attributes)
434{
435 return attributes->MBEDTLS_PRIVATE(bits);
436}
437
442#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
443 mbedtls_psa_client_handle_t handle;
444#else
451 unsigned int MBEDTLS_PRIVATE(id);
452
454
455 unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
456
457 uint32_t MBEDTLS_PRIVATE(num_ops);
458#endif
459};
460
461#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
462#define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
463#else
464#define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
465#endif
466
475
480#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
481 mbedtls_psa_client_handle_t handle;
482#else
489 unsigned int MBEDTLS_PRIVATE(id);
490
492
493 unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
494
495 uint32_t MBEDTLS_PRIVATE(num_ops);
496#endif
497};
498
499#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
500#define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
501#else
502#define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
503#endif
504
513
518#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
519 mbedtls_psa_client_handle_t handle;
520#else
529 unsigned int MBEDTLS_PRIVATE(id);
530 mbedtls_psa_key_agreement_interruptible_operation_t MBEDTLS_PRIVATE(mbedtls_ctx);
531 uint32_t MBEDTLS_PRIVATE(num_ops);
532 psa_key_attributes_t MBEDTLS_PRIVATE(attributes);
533 unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
534#endif
535};
536
537#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
538#define PSA_KEY_AGREEMENT_IOP_INIT { 0 }
539#else
540#define PSA_KEY_AGREEMENT_IOP_INIT { 0, MBEDTLS_PSA_KEY_AGREEMENT_IOP_INIT, 0, \
541 PSA_KEY_ATTRIBUTES_INIT, 0 }
542#endif
543
544static inline struct psa_key_agreement_iop_s
546{
548
549 return v;
550}
551
556#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
557 mbedtls_psa_client_handle_t handle;
558#else
567 unsigned int MBEDTLS_PRIVATE(id);
568 mbedtls_psa_generate_key_iop_t MBEDTLS_PRIVATE(ctx);
569 psa_key_attributes_t MBEDTLS_PRIVATE(attributes);
570 unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
571 uint32_t MBEDTLS_PRIVATE(num_ops);
572#endif
573};
574
575#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
576#define PSA_GENERATE_KEY_IOP_INIT { 0 }
577#else
578#define PSA_GENERATE_KEY_IOP_INIT { 0, MBEDTLS_PSA_GENERATE_KEY_IOP_INIT, PSA_KEY_ATTRIBUTES_INIT, \
579 0, 0 }
580#endif
581
582static inline struct psa_generate_key_iop_s
584{
586
587 return v;
588}
589
594#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
595 mbedtls_psa_client_handle_t handle;
596#else
605 unsigned int MBEDTLS_PRIVATE(id);
606 mbedtls_psa_export_public_key_iop_t MBEDTLS_PRIVATE(ctx);
607 unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
608 uint32_t MBEDTLS_PRIVATE(num_ops);
609#endif
610};
611
612#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
613#define PSA_EXPORT_PUBLIC_KEY_IOP_INIT { 0 }
614#else
615#define PSA_EXPORT_PUBLIC_KEY_IOP_INIT { 0, MBEDTLS_PSA_EXPORT_PUBLIC_KEY_IOP_INIT, 0, 0 }
616#endif
617
618static inline struct psa_export_public_key_iop_s
625
626#ifdef __cplusplus
627}
628#endif
629
630#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.