Mbed TLS v3.5.0
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
47  *
48  * Licensed under the Apache License, Version 2.0 (the "License"); you may
49  * not use this file except in compliance with the License.
50  * You may obtain a copy of the License at
51  *
52  * http://www.apache.org/licenses/LICENSE-2.0
53  *
54  * Unless required by applicable law or agreed to in writing, software
55  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
56  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
57  * See the License for the specific language governing permissions and
58  * limitations under the License.
59  */
60 
61 #ifndef PSA_CRYPTO_STRUCT_H
62 #define PSA_CRYPTO_STRUCT_H
63 #include "mbedtls/private_access.h"
64 
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68 
69 /* Include the Mbed TLS configuration file, the way Mbed TLS does it
70  * in each of its header files. */
71 #include "mbedtls/build_info.h"
72 
73 /* Include the context definition for the compiled-in drivers for the primitive
74  * algorithms. */
76 
84  unsigned int MBEDTLS_PRIVATE(id);
86 };
87 
88 #define PSA_HASH_OPERATION_INIT { 0, { 0 } }
89 static inline struct psa_hash_operation_s psa_hash_operation_init(void)
90 {
92  return v;
93 }
94 
102  unsigned int MBEDTLS_PRIVATE(id);
103 
104  unsigned int MBEDTLS_PRIVATE(iv_required) : 1;
105  unsigned int MBEDTLS_PRIVATE(iv_set) : 1;
106 
107  uint8_t MBEDTLS_PRIVATE(default_iv_length);
108 
110 };
111 
112 #define PSA_CIPHER_OPERATION_INIT { 0, 0, 0, 0, { 0 } }
113 static inline struct psa_cipher_operation_s psa_cipher_operation_init(void)
114 {
116  return v;
117 }
118 
119 /* Include the context definition for the compiled-in drivers for the composite
120  * algorithms. */
122 
130  unsigned int MBEDTLS_PRIVATE(id);
131  uint8_t MBEDTLS_PRIVATE(mac_size);
132  unsigned int MBEDTLS_PRIVATE(is_sign) : 1;
134 };
135 
136 #define PSA_MAC_OPERATION_INIT { 0, 0, 0, { 0 } }
137 static inline struct psa_mac_operation_s psa_mac_operation_init(void)
138 {
140  return v;
141 }
142 
144 
151  unsigned int MBEDTLS_PRIVATE(id);
152 
155 
156  size_t MBEDTLS_PRIVATE(ad_remaining);
157  size_t MBEDTLS_PRIVATE(body_remaining);
158 
159  unsigned int MBEDTLS_PRIVATE(nonce_set) : 1;
160  unsigned int MBEDTLS_PRIVATE(lengths_set) : 1;
161  unsigned int MBEDTLS_PRIVATE(ad_started) : 1;
162  unsigned int MBEDTLS_PRIVATE(body_started) : 1;
163  unsigned int MBEDTLS_PRIVATE(is_encrypt) : 1;
164 
166 };
167 
168 #define PSA_AEAD_OPERATION_INIT { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0 } }
169 static inline struct psa_aead_operation_s psa_aead_operation_init(void)
170 {
172  return v;
173 }
174 
175 /* Include the context definition for the compiled-in drivers for the key
176  * derivation algorithms. */
178 
181  unsigned int MBEDTLS_PRIVATE(can_output_key) : 1;
182  size_t MBEDTLS_PRIVATE(capacity);
184 };
185 
186 /* This only zeroes out the first byte in the union, the rest is unspecified. */
187 #define PSA_KEY_DERIVATION_OPERATION_INIT { 0, 0, 0, { 0 } }
189  void)
190 {
192  return v;
193 }
194 
199 };
200 typedef struct psa_key_policy_s psa_key_policy_t;
201 
202 #define PSA_KEY_POLICY_INIT { 0, 0, 0 }
203 static inline struct psa_key_policy_s psa_key_policy_init(void)
204 {
205  const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT;
206  return v;
207 }
208 
209 /* The type used internally for key sizes.
210  * Public interfaces use size_t, but internally we use a smaller type. */
211 typedef uint16_t psa_key_bits_t;
212 /* The maximum value of the type used to represent bit-sizes.
213  * This is used to mark an invalid key size. */
214 #define PSA_KEY_BITS_TOO_LARGE ((psa_key_bits_t) -1)
215 /* The maximum size of a key in bits.
216  * Currently defined as the maximum that can be represented, rounded down
217  * to a whole number of bytes.
218  * This is an uncast value so that it can be used in preprocessor
219  * conditionals. */
220 #define PSA_MAX_KEY_BITS 0xfff8
221 
229 typedef uint16_t psa_key_attributes_flag_t;
230 
231 #define MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER \
232  ((psa_key_attributes_flag_t) 0x0001)
233 
234 /* A mask of key attribute flags used externally only.
235  * Only meant for internal checks inside the library. */
236 #define MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ( \
237  MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER | \
238  0)
239 
240 /* A mask of key attribute flags used both internally and externally.
241  * Currently there aren't any. */
242 #define MBEDTLS_PSA_KA_MASK_DUAL_USE ( \
243  0)
244 
245 typedef struct {
253 
254 #define PSA_CORE_KEY_ATTRIBUTES_INIT { PSA_KEY_TYPE_NONE, 0, \
255  PSA_KEY_LIFETIME_VOLATILE, \
256  MBEDTLS_SVC_KEY_ID_INIT, \
257  PSA_KEY_POLICY_INIT, 0 }
258 
261 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
263 #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
264  void *MBEDTLS_PRIVATE(domain_parameters);
265  size_t MBEDTLS_PRIVATE(domain_parameters_size);
266 };
267 
268 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
269 #define PSA_KEY_ATTRIBUTES_INIT { PSA_CORE_KEY_ATTRIBUTES_INIT, 0, NULL, 0 }
270 #else
271 #define PSA_KEY_ATTRIBUTES_INIT { PSA_CORE_KEY_ATTRIBUTES_INIT, NULL, 0 }
272 #endif
273 
274 static inline struct psa_key_attributes_s psa_key_attributes_init(void)
275 {
277  return v;
278 }
279 
280 static inline void psa_set_key_id(psa_key_attributes_t *attributes,
282 {
283  psa_key_lifetime_t lifetime = attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime);
284 
285  attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id) = key;
286 
287  if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
288  attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime) =
292  }
293 }
294 
296  const psa_key_attributes_t *attributes)
297 {
298  return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id);
299 }
300 
301 #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
302 static inline void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes,
303  mbedtls_key_owner_id_t owner)
304 {
305  attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(owner) = owner;
306 }
307 #endif
308 
309 static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
310  psa_key_lifetime_t lifetime)
311 {
312  attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime) = lifetime;
313  if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
314 #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
315  attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id) = 0;
316 #else
317  attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id) = 0;
318 #endif
319  }
320 }
321 
323  const psa_key_attributes_t *attributes)
324 {
325  return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime);
326 }
327 
328 static inline void psa_extend_key_usage_flags(psa_key_usage_t *usage_flags)
329 {
330  if (*usage_flags & PSA_KEY_USAGE_SIGN_HASH) {
331  *usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE;
332  }
333 
334  if (*usage_flags & PSA_KEY_USAGE_VERIFY_HASH) {
335  *usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE;
336  }
337 }
338 
339 static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
340  psa_key_usage_t usage_flags)
341 {
342  psa_extend_key_usage_flags(&usage_flags);
343  attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage) = usage_flags;
344 }
345 
347  const psa_key_attributes_t *attributes)
348 {
349  return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage);
350 }
351 
352 static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
353  psa_algorithm_t alg)
354 {
355  attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg) = alg;
356 }
357 
359  const psa_key_attributes_t *attributes)
360 {
361  return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg);
362 }
363 
364 /* This function is declared in crypto_extra.h, which comes after this
365  * header file, but we need the function here, so repeat the declaration. */
367  psa_key_type_t type,
368  const uint8_t *data,
369  size_t data_length);
370 
371 static inline void psa_set_key_type(psa_key_attributes_t *attributes,
372  psa_key_type_t type)
373 {
374  if (attributes->MBEDTLS_PRIVATE(domain_parameters) == NULL) {
375  /* Common case: quick path */
376  attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(type) = type;
377  } else {
378  /* Call the bigger function to free the old domain parameters.
379  * Ignore any errors which may arise due to type requiring
380  * non-default domain parameters, since this function can't
381  * report errors. */
382  (void) psa_set_key_domain_parameters(attributes, type, NULL, 0);
383  }
384 }
385 
387  const psa_key_attributes_t *attributes)
388 {
389  return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(type);
390 }
391 
392 static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
393  size_t bits)
394 {
395  if (bits > PSA_MAX_KEY_BITS) {
396  attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits) = PSA_KEY_BITS_TOO_LARGE;
397  } else {
398  attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits) = (psa_key_bits_t) bits;
399  }
400 }
401 
402 static inline size_t psa_get_key_bits(
403  const psa_key_attributes_t *attributes)
404 {
405  return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits);
406 }
407 
418  unsigned int MBEDTLS_PRIVATE(id);
419 
421 
422  unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
423 
424  uint32_t MBEDTLS_PRIVATE(num_ops);
425 };
426 
427 #define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
428 
429 static inline struct psa_sign_hash_interruptible_operation_s
431 {
434 
435  return v;
436 }
437 
448  unsigned int MBEDTLS_PRIVATE(id);
449 
451 
452  unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
453 
454  uint32_t MBEDTLS_PRIVATE(num_ops);
455 };
456 
457 #define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
458 
461 {
464 
465  return v;
466 }
467 
468 #ifdef __cplusplus
469 }
470 #endif
471 
472 #endif /* PSA_CRYPTO_STRUCT_H */
#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_aead_operation_s psa_aead_operation_init(void)
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_attributes_s psa_key_attributes_init(void)
uint16_t psa_key_attributes_flag_t
static struct psa_cipher_operation_s psa_cipher_operation_init(void)
static struct psa_key_derivation_s psa_key_derivation_operation_init(void)
psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes, psa_key_type_t type, const uint8_t *data, size_t data_length)
#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 struct psa_mac_operation_s psa_mac_operation_init(void)
static void psa_set_key_lifetime(psa_key_attributes_t *attributes, psa_key_lifetime_t lifetime)
static psa_algorithm_t psa_get_key_algorithm(const psa_key_attributes_t *attributes)
static struct psa_hash_operation_s psa_hash_operation_init(void)
Definition: crypto_struct.h:89
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
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
#define PSA_AEAD_OPERATION_INIT
#define PSA_KEY_ATTRIBUTES_INIT
#define PSA_CIPHER_OPERATION_INIT
uint16_t psa_key_type_t
Encoding of a key type.
Definition: crypto_types.h:83
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
Definition: crypto_types.h:139
int32_t psa_status_t
Function return status.
Definition: crypto_types.h:64
#define PSA_HASH_OPERATION_INIT
Definition: crypto_struct.h:88
#define PSA_KEY_DERIVATION_OPERATION_INIT
#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
Definition: crypto_types.h:188
psa_key_id_t mbedtls_svc_key_id_t
Definition: crypto_types.h:297
#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.
Definition: crypto_types.h:328
#define PSA_KEY_USAGE_VERIFY_MESSAGE
#define PSA_KEY_USAGE_VERIFY_HASH
uint64_t psa_key_slot_number_t
Build-time configuration info.
Macro wrapper for struct's members.
#define MBEDTLS_PRIVATE(member)
The context for PSA interruptible hash signing.
The context for PSA interruptible hash verification.