Mbed TLS v3.5.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
52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
57/* Include the Mbed TLS configuration file, the way Mbed TLS does it
58 * in each of its header files. */
59#include "mbedtls/build_info.h"
60
61/* Include the context definition for the compiled-in drivers for the primitive
62 * algorithms. */
64
75
76#define PSA_HASH_OPERATION_INIT { 0, { 0 } }
78{
80 return v;
81}
82
90 unsigned int MBEDTLS_PRIVATE(id);
91
92 unsigned int MBEDTLS_PRIVATE(iv_required) : 1;
93 unsigned int MBEDTLS_PRIVATE(iv_set) : 1;
94
95 uint8_t MBEDTLS_PRIVATE(default_iv_length);
96
98};
99
100#define PSA_CIPHER_OPERATION_INIT { 0, 0, 0, 0, { 0 } }
102{
104 return v;
105}
106
107/* Include the context definition for the compiled-in drivers for the composite
108 * algorithms. */
110
118 unsigned int MBEDTLS_PRIVATE(id);
119 uint8_t MBEDTLS_PRIVATE(mac_size);
120 unsigned int MBEDTLS_PRIVATE(is_sign) : 1;
122};
123
124#define PSA_MAC_OPERATION_INIT { 0, 0, 0, { 0 } }
126{
128 return v;
129}
130
132
139 unsigned int MBEDTLS_PRIVATE(id);
140
143
144 size_t MBEDTLS_PRIVATE(ad_remaining);
145 size_t MBEDTLS_PRIVATE(body_remaining);
146
147 unsigned int MBEDTLS_PRIVATE(nonce_set) : 1;
148 unsigned int MBEDTLS_PRIVATE(lengths_set) : 1;
149 unsigned int MBEDTLS_PRIVATE(ad_started) : 1;
150 unsigned int MBEDTLS_PRIVATE(body_started) : 1;
151 unsigned int MBEDTLS_PRIVATE(is_encrypt) : 1;
152
154};
155
156#define PSA_AEAD_OPERATION_INIT { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0 } }
158{
160 return v;
161}
162
163/* Include the context definition for the compiled-in drivers for the key
164 * derivation algorithms. */
166
173
174/* This only zeroes out the first byte in the union, the rest is unspecified. */
175#define PSA_KEY_DERIVATION_OPERATION_INIT { 0, 0, 0, { 0 } }
177 void)
178{
180 return v;
181}
182
189
190#define PSA_KEY_POLICY_INIT { 0, 0, 0 }
191static inline struct psa_key_policy_s psa_key_policy_init(void)
192{
193 const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT;
194 return v;
195}
196
197/* The type used internally for key sizes.
198 * Public interfaces use size_t, but internally we use a smaller type. */
199typedef uint16_t psa_key_bits_t;
200/* The maximum value of the type used to represent bit-sizes.
201 * This is used to mark an invalid key size. */
202#define PSA_KEY_BITS_TOO_LARGE ((psa_key_bits_t) -1)
203/* The maximum size of a key in bits.
204 * Currently defined as the maximum that can be represented, rounded down
205 * to a whole number of bytes.
206 * This is an uncast value so that it can be used in preprocessor
207 * conditionals. */
208#define PSA_MAX_KEY_BITS 0xfff8
209
218
219#define MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER \
220 ((psa_key_attributes_flag_t) 0x0001)
221
222/* A mask of key attribute flags used externally only.
223 * Only meant for internal checks inside the library. */
224#define MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ( \
225 MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER | \
226 0)
227
228/* A mask of key attribute flags used both internally and externally.
229 * Currently there aren't any. */
230#define MBEDTLS_PSA_KA_MASK_DUAL_USE ( \
231 0)
232
241
242#define PSA_CORE_KEY_ATTRIBUTES_INIT { PSA_KEY_TYPE_NONE, 0, \
243 PSA_KEY_LIFETIME_VOLATILE, \
244 MBEDTLS_SVC_KEY_ID_INIT, \
245 PSA_KEY_POLICY_INIT, 0 }
246
249#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
251#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
252 void *MBEDTLS_PRIVATE(domain_parameters);
253 size_t MBEDTLS_PRIVATE(domain_parameters_size);
254};
255
256#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
257#define PSA_KEY_ATTRIBUTES_INIT { PSA_CORE_KEY_ATTRIBUTES_INIT, 0, NULL, 0 }
258#else
259#define PSA_KEY_ATTRIBUTES_INIT { PSA_CORE_KEY_ATTRIBUTES_INIT, NULL, 0 }
260#endif
261
263{
265 return v;
266}
267
268static inline void psa_set_key_id(psa_key_attributes_t *attributes,
270{
271 psa_key_lifetime_t lifetime = attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime);
272
273 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id) = key;
274
275 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
276 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime) =
280 }
281}
282
284 const psa_key_attributes_t *attributes)
285{
286 return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id);
287}
288
289#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
290static inline void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes,
291 mbedtls_key_owner_id_t owner)
292{
293 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(owner) = owner;
294}
295#endif
296
297static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
298 psa_key_lifetime_t lifetime)
299{
300 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime) = lifetime;
301 if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
302#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
303 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id) = 0;
304#else
305 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id) = 0;
306#endif
307 }
308}
309
311 const psa_key_attributes_t *attributes)
312{
313 return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime);
314}
315
316static inline void psa_extend_key_usage_flags(psa_key_usage_t *usage_flags)
317{
318 if (*usage_flags & PSA_KEY_USAGE_SIGN_HASH) {
319 *usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE;
320 }
321
322 if (*usage_flags & PSA_KEY_USAGE_VERIFY_HASH) {
323 *usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE;
324 }
325}
326
327static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
328 psa_key_usage_t usage_flags)
329{
330 psa_extend_key_usage_flags(&usage_flags);
331 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage) = usage_flags;
332}
333
335 const psa_key_attributes_t *attributes)
336{
337 return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage);
338}
339
340static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
341 psa_algorithm_t alg)
342{
343 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg) = alg;
344}
345
347 const psa_key_attributes_t *attributes)
348{
349 return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg);
350}
351
352/* This function is declared in crypto_extra.h, which comes after this
353 * header file, but we need the function here, so repeat the declaration. */
355 psa_key_type_t type,
356 const uint8_t *data,
357 size_t data_length);
358
359static inline void psa_set_key_type(psa_key_attributes_t *attributes,
360 psa_key_type_t type)
361{
362 if (attributes->MBEDTLS_PRIVATE(domain_parameters) == NULL) {
363 /* Common case: quick path */
364 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(type) = type;
365 } else {
366 /* Call the bigger function to free the old domain parameters.
367 * Ignore any errors which may arise due to type requiring
368 * non-default domain parameters, since this function can't
369 * report errors. */
370 (void) psa_set_key_domain_parameters(attributes, type, NULL, 0);
371 }
372}
373
375 const psa_key_attributes_t *attributes)
376{
377 return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(type);
378}
379
380static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
381 size_t bits)
382{
383 if (bits > PSA_MAX_KEY_BITS) {
384 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits) = PSA_KEY_BITS_TOO_LARGE;
385 } else {
386 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits) = (psa_key_bits_t) bits;
387 }
388}
389
390static inline size_t psa_get_key_bits(
391 const psa_key_attributes_t *attributes)
392{
393 return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits);
394}
395
406 unsigned int MBEDTLS_PRIVATE(id);
407
409
410 unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
411
412 uint32_t MBEDTLS_PRIVATE(num_ops);
413};
414
415#define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
416
425
444
445#define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
446
455
456#ifdef __cplusplus
457}
458#endif
459
460#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)
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.
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
int32_t psa_status_t
Function return status.
#define PSA_HASH_OPERATION_INIT
#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
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
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.