Mbed TLS v4.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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
54 extern "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
67 
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
92 
98  unsigned int MBEDTLS_PRIVATE(id);
99 
100  unsigned int MBEDTLS_PRIVATE(iv_required) : 1;
101  unsigned int MBEDTLS_PRIVATE(iv_set) : 1;
102 
103  uint8_t MBEDTLS_PRIVATE(default_iv_length);
104 
105  psa_driver_cipher_context_t MBEDTLS_PRIVATE(ctx);
106 #endif
107 };
108 
109 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
110 #define PSA_CIPHER_OPERATION_INIT { 0 }
111 #else
112 #define PSA_CIPHER_OPERATION_INIT { 0, 0, 0, 0, { 0 } }
113 #endif
115 {
117  return v;
118 }
119 
120 /* Include the context definition for the compiled-in drivers for the composite
121  * algorithms. */
123 
125 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
126  mbedtls_psa_client_handle_t handle;
127 #else
128 
134  unsigned int MBEDTLS_PRIVATE(id);
135  uint8_t MBEDTLS_PRIVATE(mac_size);
136  unsigned int MBEDTLS_PRIVATE(is_sign) : 1;
137  psa_driver_mac_context_t MBEDTLS_PRIVATE(ctx);
138 #endif
139 };
140 
141 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
142 #define PSA_MAC_OPERATION_INIT { 0 }
143 #else
144 #define PSA_MAC_OPERATION_INIT { 0, 0, 0, { 0 } }
145 #endif
146 static inline struct psa_mac_operation_s psa_mac_operation_init(void)
147 {
149  return v;
150 }
151 
153 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
154  mbedtls_psa_client_handle_t handle;
155 #else
156 
162  unsigned int MBEDTLS_PRIVATE(id);
163 
164  psa_algorithm_t MBEDTLS_PRIVATE(alg);
165  psa_key_type_t MBEDTLS_PRIVATE(key_type);
166 
167  size_t MBEDTLS_PRIVATE(ad_remaining);
168  size_t MBEDTLS_PRIVATE(body_remaining);
169 
170  unsigned int MBEDTLS_PRIVATE(nonce_set) : 1;
171  unsigned int MBEDTLS_PRIVATE(lengths_set) : 1;
172  unsigned int MBEDTLS_PRIVATE(ad_started) : 1;
173  unsigned int MBEDTLS_PRIVATE(body_started) : 1;
174  unsigned int MBEDTLS_PRIVATE(is_encrypt) : 1;
175 
176  psa_driver_aead_context_t MBEDTLS_PRIVATE(ctx);
177 #endif
178 };
179 
180 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
181 #define PSA_AEAD_OPERATION_INIT { 0 }
182 #else
183 #define PSA_AEAD_OPERATION_INIT { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0 } }
184 #endif
186 {
188  return v;
189 }
190 
191 /* Include the context definition for the compiled-in drivers for the key
192  * derivation algorithms. */
194 
196 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
197  mbedtls_psa_client_handle_t handle;
198 #else
199  psa_algorithm_t MBEDTLS_PRIVATE(alg);
200  unsigned int MBEDTLS_PRIVATE(can_output_key) : 1;
201  size_t MBEDTLS_PRIVATE(capacity);
203 #endif
204 };
205 
206 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
207 #define PSA_KEY_DERIVATION_OPERATION_INIT { 0 }
208 #else
209 /* This only zeroes out the first byte in the union, the rest is unspecified. */
210 #define PSA_KEY_DERIVATION_OPERATION_INIT { 0, 0, 0, { 0 } }
211 #endif
213  void)
214 {
216  return v;
217 }
218 
220  /* Future versions may add other fields in this structure. */
221  uint32_t flags;
222 };
223 
231 #define PSA_CUSTOM_KEY_PARAMETERS_INIT { 0 }
232 
234  psa_key_usage_t MBEDTLS_PRIVATE(usage);
235  psa_algorithm_t MBEDTLS_PRIVATE(alg);
236  psa_algorithm_t MBEDTLS_PRIVATE(alg2);
237 };
239 
240 #define PSA_KEY_POLICY_INIT { 0, 0, 0 }
241 static inline struct psa_key_policy_s psa_key_policy_init(void)
242 {
243  const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT;
244  return v;
245 }
246 
247 /* The type used internally for key sizes.
248  * Public interfaces use size_t, but internally we use a smaller type. */
249 typedef uint16_t psa_key_bits_t;
250 /* The maximum value of the type used to represent bit-sizes.
251  * This is used to mark an invalid key size. */
252 #define PSA_KEY_BITS_TOO_LARGE ((psa_key_bits_t) -1)
253 /* The maximum size of a key in bits.
254  * Currently defined as the maximum that can be represented, rounded down
255  * to a whole number of bytes.
256  * This is an uncast value so that it can be used in preprocessor
257  * conditionals. */
258 #define PSA_MAX_KEY_BITS 0xfff8
259 
261  psa_key_type_t MBEDTLS_PRIVATE(type);
262  psa_key_bits_t MBEDTLS_PRIVATE(bits);
263  psa_key_lifetime_t MBEDTLS_PRIVATE(lifetime);
264  psa_key_policy_t MBEDTLS_PRIVATE(policy);
265  /* This type has a different layout in the client view wrt the
266  * service view of the key id, i.e. in service view usually is
267  * expected to have MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined
268  * thus adding an owner field to the standard psa_key_id_t. For
269  * implementations with client/service separation, this means the
270  * object will be marshalled through a transport channel and
271  * interpreted differently at each side of the transport. Placing
272  * it at the end of structures allows to interpret the structure
273  * at the client without reorganizing the memory layout of the
274  * struct
275  */
276  mbedtls_svc_key_id_t MBEDTLS_PRIVATE(id);
277 };
278 
279 #define PSA_KEY_ATTRIBUTES_INIT { PSA_KEY_TYPE_NONE, 0, \
280  PSA_KEY_LIFETIME_VOLATILE, \
281  PSA_KEY_POLICY_INIT, \
282  MBEDTLS_SVC_KEY_ID_INIT }
283 
285 {
287  return v;
288 }
289 
290 static inline void psa_set_key_id(psa_key_attributes_t *attributes,
292 {
293  psa_key_lifetime_t lifetime = attributes->MBEDTLS_PRIVATE(lifetime);
294 
295  attributes->MBEDTLS_PRIVATE(id) = key;
296 
297  if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
298  attributes->MBEDTLS_PRIVATE(lifetime) =
302  }
303 }
304 
306  const psa_key_attributes_t *attributes)
307 {
308  return attributes->MBEDTLS_PRIVATE(id);
309 }
310 
311 #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
312 static inline void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes,
314 {
315  attributes->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(owner) = owner;
316 }
317 #endif
318 
319 static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
320  psa_key_lifetime_t lifetime)
321 {
322  attributes->MBEDTLS_PRIVATE(lifetime) = lifetime;
323  if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
324 #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
325  attributes->MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id) = 0;
326 #else
327  attributes->MBEDTLS_PRIVATE(id) = 0;
328 #endif
329  }
330 }
331 
333  const psa_key_attributes_t *attributes)
334 {
335  return attributes->MBEDTLS_PRIVATE(lifetime);
336 }
337 
338 static inline void psa_extend_key_usage_flags(psa_key_usage_t *usage_flags)
339 {
340  if (*usage_flags & PSA_KEY_USAGE_SIGN_HASH) {
341  *usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE;
342  }
343 
344  if (*usage_flags & PSA_KEY_USAGE_VERIFY_HASH) {
345  *usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE;
346  }
347 }
348 
349 static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
350  psa_key_usage_t usage_flags)
351 {
352  psa_extend_key_usage_flags(&usage_flags);
353  attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage) = usage_flags;
354 }
355 
357  const psa_key_attributes_t *attributes)
358 {
359  return attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage);
360 }
361 
362 static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
363  psa_algorithm_t alg)
364 {
365  attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg) = alg;
366 }
367 
369  const psa_key_attributes_t *attributes)
370 {
371  return attributes->MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg);
372 }
373 
374 static inline void psa_set_key_type(psa_key_attributes_t *attributes,
375  psa_key_type_t type)
376 {
377  attributes->MBEDTLS_PRIVATE(type) = type;
378 }
379 
381  const psa_key_attributes_t *attributes)
382 {
383  return attributes->MBEDTLS_PRIVATE(type);
384 }
385 
386 static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
387  size_t bits)
388 {
389  if (bits > PSA_MAX_KEY_BITS) {
390  attributes->MBEDTLS_PRIVATE(bits) = PSA_KEY_BITS_TOO_LARGE;
391  } else {
392  attributes->MBEDTLS_PRIVATE(bits) = (psa_key_bits_t) bits;
393  }
394 }
395 
396 static inline size_t psa_get_key_bits(
397  const psa_key_attributes_t *attributes)
398 {
399  return attributes->MBEDTLS_PRIVATE(bits);
400 }
401 
406 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
407  mbedtls_psa_client_handle_t handle;
408 #else
409 
415  unsigned int MBEDTLS_PRIVATE(id);
416 
418 
419  unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
420 
421  uint32_t MBEDTLS_PRIVATE(num_ops);
422 #endif
423 };
424 
425 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
426 #define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
427 #else
428 #define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
429 #endif
430 
431 static inline struct psa_sign_hash_interruptible_operation_s
433 {
436 
437  return v;
438 }
439 
444 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
445  mbedtls_psa_client_handle_t handle;
446 #else
447 
453  unsigned int MBEDTLS_PRIVATE(id);
454 
456 
457  unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
458 
459  uint32_t MBEDTLS_PRIVATE(num_ops);
460 #endif
461 };
462 
463 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
464 #define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0 }
465 #else
466 #define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
467 #endif
468 
471 {
474 
475  return v;
476 }
477 
482 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
483  mbedtls_psa_client_handle_t handle;
484 #else
485 
493  unsigned int MBEDTLS_PRIVATE(id);
495  uint32_t MBEDTLS_PRIVATE(num_ops);
496  psa_key_attributes_t MBEDTLS_PRIVATE(attributes);
497  unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
498 #endif
499 };
500 
501 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
502 #define PSA_KEY_AGREEMENT_IOP_INIT { 0 }
503 #else
504 #define PSA_KEY_AGREEMENT_IOP_INIT { 0, MBEDTLS_PSA_KEY_AGREEMENT_IOP_INIT, 0, \
505  PSA_KEY_ATTRIBUTES_INIT, 0 }
506 #endif
507 
508 static inline struct psa_key_agreement_iop_s
510 {
512 
513  return v;
514 }
515 
520 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
521  mbedtls_psa_client_handle_t handle;
522 #else
523 
531  unsigned int MBEDTLS_PRIVATE(id);
532  mbedtls_psa_generate_key_iop_t MBEDTLS_PRIVATE(ctx);
533  psa_key_attributes_t MBEDTLS_PRIVATE(attributes);
534  unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
535  uint32_t MBEDTLS_PRIVATE(num_ops);
536 #endif
537 };
538 
539 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
540 #define PSA_GENERATE_KEY_IOP_INIT { 0 }
541 #else
542 #define PSA_GENERATE_KEY_IOP_INIT { 0, MBEDTLS_PSA_GENERATE_KEY_IOP_INIT, PSA_KEY_ATTRIBUTES_INIT, \
543  0, 0 }
544 #endif
545 
546 static inline struct psa_generate_key_iop_s
548 {
550 
551  return v;
552 }
553 
558 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
559  mbedtls_psa_client_handle_t handle;
560 #else
561 
569  unsigned int MBEDTLS_PRIVATE(id);
571  unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
572  uint32_t MBEDTLS_PRIVATE(num_ops);
573 #endif
574 };
575 
576 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
577 #define PSA_EXPORT_PUBLIC_KEY_IOP_INIT { 0 }
578 #else
579 #define PSA_EXPORT_PUBLIC_KEY_IOP_INIT { 0, MBEDTLS_PSA_EXPORT_PUBLIC_KEY_IOP_INIT, 0, 0 }
580 #endif
581 
582 static inline struct psa_export_public_key_iop_s
584 {
586 
587  return v;
588 }
589 
590 #ifdef __cplusplus
591 }
592 #endif
593 
594 #endif /* PSA_CRYPTO_STRUCT_H */
#define PSA_EXPORT_PUBLIC_KEY_IOP_INIT
#define PSA_GENERATE_KEY_IOP_INIT
static void psa_set_key_usage_flags(psa_key_attributes_t *attributes, psa_key_usage_t usage_flags)
#define PSA_MAX_KEY_BITS
Declaration of context structures for use with the PSA driver wrapper interface. This file contains t...
static psa_key_lifetime_t psa_get_key_lifetime(const psa_key_attributes_t *attributes)
#define PSA_MAC_OPERATION_INIT
static struct psa_aead_operation_s psa_aead_operation_init(void)
uint16_t psa_key_bits_t
#define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT
static void psa_set_key_id(psa_key_attributes_t *attributes, mbedtls_svc_key_id_t key)
The context for PSA interruptible key generation.
static struct psa_sign_hash_interruptible_operation_s psa_sign_hash_interruptible_operation_init(void)
int32_t mbedtls_key_owner_id_t
#define PSA_CIPHER_OPERATION_INIT
#define PSA_KEY_ATTRIBUTES_INIT
#define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)
Declaration of context structures for use with the PSA driver wrapper interface. This file contains t...
#define PSA_KEY_USAGE_VERIFY_MESSAGE
static struct psa_key_agreement_iop_s psa_key_agreement_iop_init(void)
static struct psa_key_derivation_s psa_key_derivation_operation_init(void)
#define PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(persistence, location)
#define PSA_KEY_LIFETIME_GET_LOCATION(lifetime)
#define PSA_KEY_USAGE_SIGN_MESSAGE
static struct psa_export_public_key_iop_s psa_export_public_key_iop_init(void)
static void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes, mbedtls_key_owner_id_t owner)
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
Definition: crypto_types.h:127
#define PSA_KEY_AGREEMENT_IOP_INIT
#define PSA_KEY_LIFETIME_PERSISTENT
uint32_t psa_key_usage_t
Encoding of permitted usage on a key.
Definition: crypto_types.h:316
uint16_t psa_key_type_t
Encoding of a key type.
Definition: crypto_types.h:71
#define PSA_KEY_POLICY_INIT
#define PSA_KEY_DERIVATION_OPERATION_INIT
static struct psa_generate_key_iop_s psa_generate_key_iop_init(void)
#define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT
static psa_algorithm_t psa_get_key_algorithm(const psa_key_attributes_t *attributes)
#define PSA_AEAD_OPERATION_INIT
#define PSA_KEY_BITS_TOO_LARGE
static mbedtls_svc_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes)
static psa_key_usage_t psa_get_key_usage_flags(const psa_key_attributes_t *attributes)
static struct psa_verify_hash_interruptible_operation_s psa_verify_hash_interruptible_operation_init(void)
#define PSA_HASH_OPERATION_INIT
Definition: crypto_struct.h:80
static struct psa_mac_operation_s psa_mac_operation_init(void)
The context for PSA interruptible hash signing.
static struct psa_key_attributes_s psa_key_attributes_init(void)
static size_t psa_get_key_bits(const psa_key_attributes_t *attributes)
Build-time configuration info.
Declaration of context structures for use with the PSA driver wrapper interface. This file contains t...
static struct psa_cipher_operation_s psa_cipher_operation_init(void)
The context for PSA interruptible hash verification.
static void psa_set_key_bits(psa_key_attributes_t *attributes, size_t bits)
static void psa_set_key_lifetime(psa_key_attributes_t *attributes, psa_key_lifetime_t lifetime)
uint32_t psa_key_lifetime_t
Definition: crypto_types.h:176
static void psa_extend_key_usage_flags(psa_key_usage_t *usage_flags)
static void psa_set_key_algorithm(psa_key_attributes_t *attributes, psa_algorithm_t alg)
The context for PSA interruptible export public-key.
static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes)
static void psa_set_key_type(psa_key_attributes_t *attributes, psa_key_type_t type)
The context for PSA interruptible key agreement.
#define PSA_KEY_USAGE_VERIFY_HASH
static struct psa_key_policy_s psa_key_policy_init(void)
#define PSA_KEY_USAGE_SIGN_HASH
static struct psa_hash_operation_s psa_hash_operation_init(void)
Definition: crypto_struct.h:82