mbed TLS v3.3.0
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
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
64
65#ifdef __cplusplus
66extern "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 "mbedtls/cmac.h"
74#include "mbedtls/gcm.h"
75#include "mbedtls/ccm.h"
76#include "mbedtls/chachapoly.h"
77
78/* Include the context definition for the compiled-in drivers for the primitive
79 * algorithms. */
81
83{
90 unsigned int MBEDTLS_PRIVATE(id);
92};
93
94#define PSA_HASH_OPERATION_INIT { 0, { 0 } }
96{
98 return( v );
99}
100
102{
109 unsigned int MBEDTLS_PRIVATE(id);
110
111 unsigned int MBEDTLS_PRIVATE(iv_required) : 1;
112 unsigned int MBEDTLS_PRIVATE(iv_set) : 1;
113
114 uint8_t MBEDTLS_PRIVATE(default_iv_length);
115
117};
118
119#define PSA_CIPHER_OPERATION_INIT { 0, 0, 0, 0, { 0 } }
121{
123 return( v );
124}
125
126/* Include the context definition for the compiled-in drivers for the composite
127 * algorithms. */
129
131{
138 unsigned int MBEDTLS_PRIVATE(id);
139 uint8_t MBEDTLS_PRIVATE(mac_size);
140 unsigned int MBEDTLS_PRIVATE(is_sign) : 1;
142};
143
144#define PSA_MAC_OPERATION_INIT { 0, 0, 0, { 0 } }
145static inline struct psa_mac_operation_s psa_mac_operation_init( void )
146{
148 return( v );
149}
150
152{
153
160 unsigned int MBEDTLS_PRIVATE(id);
161
164
165 size_t MBEDTLS_PRIVATE(ad_remaining);
166 size_t MBEDTLS_PRIVATE(body_remaining);
167
168 unsigned int MBEDTLS_PRIVATE(nonce_set) : 1;
169 unsigned int MBEDTLS_PRIVATE(lengths_set) : 1;
170 unsigned int MBEDTLS_PRIVATE(ad_started) : 1;
171 unsigned int MBEDTLS_PRIVATE(body_started) : 1;
172 unsigned int MBEDTLS_PRIVATE(is_encrypt) : 1;
173
175};
176
177#define PSA_AEAD_OPERATION_INIT {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0}}
179{
181 return( v );
182}
183
184#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
185 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
186 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
187typedef struct
188{
189 uint8_t *MBEDTLS_PRIVATE(info);
190 size_t MBEDTLS_PRIVATE(info_length);
191#if PSA_HASH_MAX_SIZE > 0xff
192#error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
193#endif
194 uint8_t MBEDTLS_PRIVATE(offset_in_block);
195 uint8_t MBEDTLS_PRIVATE(block_number);
196 unsigned int MBEDTLS_PRIVATE(state) : 2;
197 unsigned int MBEDTLS_PRIVATE(info_set) : 1;
198 uint8_t MBEDTLS_PRIVATE(output_block)[PSA_HASH_MAX_SIZE];
202#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF ||
203 MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
204 MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
205#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
206typedef struct
207{
210#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
211
212#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
213 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
214typedef enum
215{
216 PSA_TLS12_PRF_STATE_INIT, /* no input provided */
217 PSA_TLS12_PRF_STATE_SEED_SET, /* seed has been set */
218 PSA_TLS12_PRF_STATE_OTHER_KEY_SET, /* other key has been set - optional */
219 PSA_TLS12_PRF_STATE_KEY_SET, /* key has been set */
220 PSA_TLS12_PRF_STATE_LABEL_SET, /* label has been set */
221 PSA_TLS12_PRF_STATE_OUTPUT /* output has been started */
223
225{
226#if PSA_HASH_MAX_SIZE > 0xff
227#error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
228#endif
229
230 /* Indicates how many bytes in the current HMAC block have
231 * not yet been read by the user. */
232 uint8_t MBEDTLS_PRIVATE(left_in_block);
233
234 /* The 1-based number of the block. */
235 uint8_t MBEDTLS_PRIVATE(block_number);
236
238
239 uint8_t *MBEDTLS_PRIVATE(secret);
240 size_t MBEDTLS_PRIVATE(secret_length);
241 uint8_t *MBEDTLS_PRIVATE(seed);
242 size_t MBEDTLS_PRIVATE(seed_length);
243 uint8_t *MBEDTLS_PRIVATE(label);
244 size_t MBEDTLS_PRIVATE(label_length);
245#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
246 uint8_t *MBEDTLS_PRIVATE(other_secret);
247 size_t MBEDTLS_PRIVATE(other_secret_length);
248#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
249
251
252 /* `HMAC_hash( prk, A( i ) + seed )` in the notation of RFC 5246, Sect. 5. */
253 uint8_t MBEDTLS_PRIVATE(output_block)[PSA_HASH_MAX_SIZE];
255#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
256 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
257
259{
261 unsigned int MBEDTLS_PRIVATE(can_output_key) : 1;
262 size_t MBEDTLS_PRIVATE(capacity);
263 union
264 {
265 /* Make the union non-empty even with no supported algorithms. */
266 uint8_t MBEDTLS_PRIVATE(dummy);
267#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
268 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
269 defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
271#endif
272#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
273 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
275#endif
276#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
278#endif
280};
281
282/* This only zeroes out the first byte in the union, the rest is unspecified. */
283#define PSA_KEY_DERIVATION_OPERATION_INIT { 0, 0, 0, { 0 } }
285 void )
286{
288 return( v );
289}
290
292{
296};
298
299#define PSA_KEY_POLICY_INIT { 0, 0, 0 }
300static inline struct psa_key_policy_s psa_key_policy_init( void )
301{
302 const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT;
303 return( v );
304}
305
306/* The type used internally for key sizes.
307 * Public interfaces use size_t, but internally we use a smaller type. */
308typedef uint16_t psa_key_bits_t;
309/* The maximum value of the type used to represent bit-sizes.
310 * This is used to mark an invalid key size. */
311#define PSA_KEY_BITS_TOO_LARGE ( ( psa_key_bits_t ) -1 )
312/* The maximum size of a key in bits.
313 * Currently defined as the maximum that can be represented, rounded down
314 * to a whole number of bytes.
315 * This is an uncast value so that it can be used in preprocessor
316 * conditionals. */
317#define PSA_MAX_KEY_BITS 0xfff8
318
327
328#define MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER \
329 ( (psa_key_attributes_flag_t) 0x0001 )
330
331/* A mask of key attribute flags used externally only.
332 * Only meant for internal checks inside the library. */
333#define MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ( \
334 MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER | \
335 0 )
336
337/* A mask of key attribute flags used both internally and externally.
338 * Currently there aren't any. */
339#define MBEDTLS_PSA_KA_MASK_DUAL_USE ( \
340 0 )
341
342typedef struct
343{
351
352#define PSA_CORE_KEY_ATTRIBUTES_INIT { PSA_KEY_TYPE_NONE, 0, \
353 PSA_KEY_LIFETIME_VOLATILE, \
354 MBEDTLS_SVC_KEY_ID_INIT, \
355 PSA_KEY_POLICY_INIT, 0 }
356
358{
360#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
362#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
363 void *MBEDTLS_PRIVATE(domain_parameters);
364 size_t MBEDTLS_PRIVATE(domain_parameters_size);
365};
366
367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
368#define PSA_KEY_ATTRIBUTES_INIT { PSA_CORE_KEY_ATTRIBUTES_INIT, 0, NULL, 0 }
369#else
370#define PSA_KEY_ATTRIBUTES_INIT { PSA_CORE_KEY_ATTRIBUTES_INIT, NULL, 0 }
371#endif
372
374{
376 return( v );
377}
378
379static inline void psa_set_key_id( psa_key_attributes_t *attributes,
381{
382 psa_key_lifetime_t lifetime = attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime);
383
384 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id) = key;
385
386 if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
387 {
388 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime) =
391 PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) );
392 }
393}
394
396 const psa_key_attributes_t *attributes )
397{
398 return( attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id) );
399}
400
401#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
402static inline void mbedtls_set_key_owner_id( psa_key_attributes_t *attributes,
403 mbedtls_key_owner_id_t owner )
404{
405 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(owner) = owner;
406}
407#endif
408
409static inline void psa_set_key_lifetime( psa_key_attributes_t *attributes,
410 psa_key_lifetime_t lifetime )
411{
412 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime) = lifetime;
413 if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
414 {
415#ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
416 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id) = 0;
417#else
418 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id) = 0;
419#endif
420 }
421}
422
424 const psa_key_attributes_t *attributes )
425{
426 return( attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime) );
427}
428
429static inline void psa_extend_key_usage_flags( psa_key_usage_t *usage_flags )
430{
431 if( *usage_flags & PSA_KEY_USAGE_SIGN_HASH )
432 *usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE;
433
434 if( *usage_flags & PSA_KEY_USAGE_VERIFY_HASH )
435 *usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE;
436}
437
438static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
439 psa_key_usage_t usage_flags)
440{
441 psa_extend_key_usage_flags( &usage_flags );
442 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage) = usage_flags;
443}
444
446 const psa_key_attributes_t *attributes )
447{
448 return( attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage) );
449}
450
451static inline void psa_set_key_algorithm( psa_key_attributes_t *attributes,
452 psa_algorithm_t alg )
453{
454 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg) = alg;
455}
456
458 const psa_key_attributes_t *attributes )
459{
460 return( attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg) );
461}
462
463/* This function is declared in crypto_extra.h, which comes after this
464 * header file, but we need the function here, so repeat the declaration. */
466 psa_key_type_t type,
467 const uint8_t *data,
468 size_t data_length );
469
470static inline void psa_set_key_type( psa_key_attributes_t *attributes,
471 psa_key_type_t type )
472{
473 if( attributes->MBEDTLS_PRIVATE(domain_parameters) == NULL )
474 {
475 /* Common case: quick path */
476 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(type) = type;
477 }
478 else
479 {
480 /* Call the bigger function to free the old domain parameters.
481 * Ignore any errors which may arise due to type requiring
482 * non-default domain parameters, since this function can't
483 * report errors. */
484 (void) psa_set_key_domain_parameters( attributes, type, NULL, 0 );
485 }
486}
487
489 const psa_key_attributes_t *attributes )
490{
491 return( attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(type) );
492}
493
494static inline void psa_set_key_bits( psa_key_attributes_t *attributes,
495 size_t bits )
496{
497 if( bits > PSA_MAX_KEY_BITS )
498 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits) = PSA_KEY_BITS_TOO_LARGE;
499 else
500 attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits) = (psa_key_bits_t) bits;
501}
502
503static inline size_t psa_get_key_bits(
504 const psa_key_attributes_t *attributes )
505{
506 return( attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits) );
507}
508
509#ifdef __cplusplus
510}
511#endif
512
513#endif /* PSA_CRYPTO_STRUCT_H */
Build-time configuration info.
This file provides an API for the CCM authenticated encryption mode for block ciphers.
This file contains the AEAD-ChaCha20-Poly1305 definitions and functions.
This file contains CMAC definitions and functions.
#define PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE
Definition: crypto_sizes.h:249
#define PSA_HASH_MAX_SIZE
Definition: crypto_sizes.h:125
#define PSA_KEY_BITS_TOO_LARGE
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)
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)
struct psa_tls12_prf_key_derivation_s psa_tls12_prf_key_derivation_t
#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:95
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)
static void psa_set_key_algorithm(psa_key_attributes_t *attributes, psa_algorithm_t alg)
psa_tls12_prf_key_derivation_state_t
@ PSA_TLS12_PRF_STATE_SEED_SET
@ PSA_TLS12_PRF_STATE_LABEL_SET
@ PSA_TLS12_PRF_STATE_OUTPUT
@ PSA_TLS12_PRF_STATE_KEY_SET
@ PSA_TLS12_PRF_STATE_OTHER_KEY_SET
@ PSA_TLS12_PRF_STATE_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)
This file contains GCM definitions and functions.
#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:82
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
Definition: crypto_types.h:138
int32_t psa_status_t
Function return status.
Definition: crypto_types.h:63
#define PSA_HASH_OPERATION_INIT
Definition: crypto_struct.h:94
#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:187
psa_key_id_t mbedtls_svc_key_id_t
Definition: crypto_types.h:296
#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
Macro wrapper for struct's members.
#define MBEDTLS_PRIVATE(member)