mbed TLS v2.28.2
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 
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
68 /* Include the Mbed TLS configuration file, the way Mbed TLS does it
69  * in each of its header files. */
70 #if !defined(MBEDTLS_CONFIG_FILE)
71 #include "mbedtls/config.h"
72 #else
73 #include MBEDTLS_CONFIG_FILE
74 #endif
75 
76 #include "mbedtls/cmac.h"
77 #include "mbedtls/gcm.h"
78 
79 /* Include the context definition for the compiled-in drivers for the primitive
80  * algorithms. */
82 
84 {
91  unsigned int id;
93 };
94 
95 #define PSA_HASH_OPERATION_INIT {0, {0}}
96 static inline struct psa_hash_operation_s psa_hash_operation_init( void )
97 {
99  return( v );
100 }
101 
103 {
110  unsigned int id;
111 
112  unsigned int iv_required : 1;
113  unsigned int iv_set : 1;
114 
116 
118 };
119 
120 #define PSA_CIPHER_OPERATION_INIT {0, 0, 0, 0, {0}}
121 static inline struct psa_cipher_operation_s psa_cipher_operation_init( void )
122 {
124  return( v );
125 }
126 
127 /* Include the context definition for the compiled-in drivers for the composite
128  * algorithms. */
130 
132 {
139  unsigned int id;
140  uint8_t mac_size;
141  unsigned int is_sign : 1;
143 };
144 
145 #define PSA_MAC_OPERATION_INIT {0, 0, 0, {0}}
146 static inline struct psa_mac_operation_s psa_mac_operation_init( void )
147 {
149  return( v );
150 }
151 
153 {
155  unsigned int key_set : 1;
156  unsigned int iv_set : 1;
157  uint8_t iv_size;
158  uint8_t block_size;
159  union
160  {
161  unsigned dummy; /* Enable easier initializing of the union. */
163  } ctx;
164 };
165 
166 #define PSA_AEAD_OPERATION_INIT {0, 0, 0, 0, 0, {0}}
167 static inline struct psa_aead_operation_s psa_aead_operation_init( void )
168 {
170  return( v );
171 }
172 
173 #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
174 typedef struct
175 {
176  uint8_t *info;
177  size_t info_length;
178 #if PSA_HASH_MAX_SIZE > 0xff
179 #error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
180 #endif
182  uint8_t block_number;
183  unsigned int state : 2;
184  unsigned int info_set : 1;
185  uint8_t output_block[PSA_HASH_MAX_SIZE];
186  uint8_t prk[PSA_HASH_MAX_SIZE];
187  struct psa_mac_operation_s hmac;
189 #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
190 
191 #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
192  defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
193 typedef enum
194 {
195  PSA_TLS12_PRF_STATE_INIT, /* no input provided */
196  PSA_TLS12_PRF_STATE_SEED_SET, /* seed has been set */
197  PSA_TLS12_PRF_STATE_KEY_SET, /* key has been set */
198  PSA_TLS12_PRF_STATE_LABEL_SET, /* label has been set */
199  PSA_TLS12_PRF_STATE_OUTPUT /* output has been started */
201 
203 {
204 #if PSA_HASH_MAX_SIZE > 0xff
205 #error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
206 #endif
207 
208  /* Indicates how many bytes in the current HMAC block have
209  * not yet been read by the user. */
210  uint8_t left_in_block;
211 
212  /* The 1-based number of the block. */
213  uint8_t block_number;
214 
216 
217  uint8_t *secret;
219  uint8_t *seed;
220  size_t seed_length;
221  uint8_t *label;
222  size_t label_length;
223 
225 
226  /* `HMAC_hash( prk, A(i) + seed )` in the notation of RFC 5246, Sect. 5. */
229 #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
230  * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
231 
233 {
235  unsigned int can_output_key : 1;
236  size_t capacity;
237  union
238  {
239  /* Make the union non-empty even with no supported algorithms. */
240  uint8_t dummy;
241 #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
243 #endif
244 #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
245  defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
247 #endif
248  } ctx;
249 };
250 
251 /* This only zeroes out the first byte in the union, the rest is unspecified. */
252 #define PSA_KEY_DERIVATION_OPERATION_INIT {0, 0, 0, {0}}
253 static inline struct psa_key_derivation_s psa_key_derivation_operation_init( void )
254 {
256  return( v );
257 }
258 
260 {
264 };
265 typedef struct psa_key_policy_s psa_key_policy_t;
266 
267 #define PSA_KEY_POLICY_INIT {0, 0, 0}
268 static inline struct psa_key_policy_s psa_key_policy_init( void )
269 {
270  const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT;
271  return( v );
272 }
273 
274 /* The type used internally for key sizes.
275  * Public interfaces use size_t, but internally we use a smaller type. */
276 typedef uint16_t psa_key_bits_t;
277 /* The maximum value of the type used to represent bit-sizes.
278  * This is used to mark an invalid key size. */
279 #define PSA_KEY_BITS_TOO_LARGE ( (psa_key_bits_t) ( -1 ) )
280 /* The maximum size of a key in bits.
281  * Currently defined as the maximum that can be represented, rounded down
282  * to a whole number of bytes.
283  * This is an uncast value so that it can be used in preprocessor
284  * conditionals. */
285 #define PSA_MAX_KEY_BITS 0xfff8
286 
294 typedef uint16_t psa_key_attributes_flag_t;
295 
296 #define MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER \
297  ( (psa_key_attributes_flag_t) 0x0001 )
298 
299 /* A mask of key attribute flags used externally only.
300  * Only meant for internal checks inside the library. */
301 #define MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ( \
302  MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER | \
303  0 )
304 
305 /* A mask of key attribute flags used both internally and externally.
306  * Currently there aren't any. */
307 #define MBEDTLS_PSA_KA_MASK_DUAL_USE ( \
308  0 )
309 
310 typedef struct
311 {
319 
320 #define PSA_CORE_KEY_ATTRIBUTES_INIT {PSA_KEY_TYPE_NONE, 0, PSA_KEY_LIFETIME_VOLATILE, MBEDTLS_SVC_KEY_ID_INIT, PSA_KEY_POLICY_INIT, 0}
321 
323 {
325 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
326  psa_key_slot_number_t slot_number;
327 #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
330 };
331 
332 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
333 #define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT, 0, NULL, 0}
334 #else
335 #define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT, NULL, 0}
336 #endif
337 
338 static inline struct psa_key_attributes_s psa_key_attributes_init( void )
339 {
341  return( v );
342 }
343 
344 static inline void psa_set_key_id( psa_key_attributes_t *attributes,
346 {
347  psa_key_lifetime_t lifetime = attributes->core.lifetime;
348 
349  attributes->core.id = key;
350 
351  if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
352  {
353  attributes->core.lifetime =
356  PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) );
357  }
358 }
359 
361  const psa_key_attributes_t *attributes)
362 {
363  return( attributes->core.id );
364 }
365 
366 #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
367 static inline void mbedtls_set_key_owner_id( psa_key_attributes_t *attributes,
368  mbedtls_key_owner_id_t owner )
369 {
370  attributes->core.id.owner = owner;
371 }
372 #endif
373 
374 static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
375  psa_key_lifetime_t lifetime)
376 {
377  attributes->core.lifetime = lifetime;
378  if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
379  {
380 #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
381  attributes->core.id.key_id = 0;
382 #else
383  attributes->core.id = 0;
384 #endif
385  }
386 }
387 
389  const psa_key_attributes_t *attributes)
390 {
391  return( attributes->core.lifetime );
392 }
393 
394 static inline void psa_extend_key_usage_flags( psa_key_usage_t *usage_flags )
395 {
396  if( *usage_flags & PSA_KEY_USAGE_SIGN_HASH )
397  *usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE;
398 
399  if( *usage_flags & PSA_KEY_USAGE_VERIFY_HASH )
400  *usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE;
401 }
402 
403 static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
404  psa_key_usage_t usage_flags)
405 {
406  psa_extend_key_usage_flags( &usage_flags );
407  attributes->core.policy.usage = usage_flags;
408 }
409 
411  const psa_key_attributes_t *attributes)
412 {
413  return( attributes->core.policy.usage );
414 }
415 
416 static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
417  psa_algorithm_t alg)
418 {
419  attributes->core.policy.alg = alg;
420 }
421 
423  const psa_key_attributes_t *attributes)
424 {
425  return( attributes->core.policy.alg );
426 }
427 
428 /* This function is declared in crypto_extra.h, which comes after this
429  * header file, but we need the function here, so repeat the declaration. */
431  psa_key_type_t type,
432  const uint8_t *data,
433  size_t data_length);
434 
435 static inline void psa_set_key_type(psa_key_attributes_t *attributes,
436  psa_key_type_t type)
437 {
438  if( attributes->domain_parameters == NULL )
439  {
440  /* Common case: quick path */
441  attributes->core.type = type;
442  }
443  else
444  {
445  /* Call the bigger function to free the old domain parameters.
446  * Ignore any errors which may arise due to type requiring
447  * non-default domain parameters, since this function can't
448  * report errors. */
449  (void) psa_set_key_domain_parameters( attributes, type, NULL, 0 );
450  }
451 }
452 
454  const psa_key_attributes_t *attributes)
455 {
456  return( attributes->core.type );
457 }
458 
459 static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
460  size_t bits)
461 {
462  if( bits > PSA_MAX_KEY_BITS )
463  attributes->core.bits = PSA_KEY_BITS_TOO_LARGE;
464  else
465  attributes->core.bits = (psa_key_bits_t) bits;
466 }
467 
468 static inline size_t psa_get_key_bits(
469  const psa_key_attributes_t *attributes)
470 {
471  return( attributes->core.bits );
472 }
473 
474 #ifdef __cplusplus
475 }
476 #endif
477 
478 #endif /* PSA_CRYPTO_STRUCT_H */
This file contains CMAC definitions and functions.
Configuration options (set of defines)
#define PSA_HASH_MAX_SIZE
Definition: crypto_sizes.h:136
#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:96
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_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:81
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
Definition: crypto_types.h:137
int32_t psa_status_t
Function return status.
Definition: crypto_types.h:62
#define PSA_HASH_OPERATION_INIT
Definition: crypto_struct.h:95
#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:186
psa_key_id_t mbedtls_svc_key_id_t
Definition: crypto_types.h:295
#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:327
#define PSA_KEY_USAGE_VERIFY_MESSAGE
#define PSA_KEY_USAGE_VERIFY_HASH
uint64_t psa_key_slot_number_t
union psa_aead_operation_s::@7 ctx
unsigned int key_set
psa_algorithm_t alg
mbedtls_cipher_context_t cipher
psa_driver_cipher_context_t ctx
unsigned int iv_required
mbedtls_svc_key_id_t id
psa_key_lifetime_t lifetime
psa_key_policy_t policy
psa_key_attributes_flag_t flags
psa_driver_hash_context_t ctx
Definition: crypto_struct.h:92
psa_core_key_attributes_t core
union psa_key_derivation_s::@8 ctx
unsigned int can_output_key
psa_algorithm_t alg
psa_hkdf_key_derivation_t hkdf
psa_tls12_prf_key_derivation_t tls12_prf
psa_key_usage_t usage
psa_algorithm_t alg2
psa_algorithm_t alg
psa_driver_mac_context_t ctx
unsigned int is_sign
psa_tls12_prf_key_derivation_state_t state
uint8_t Ai[PSA_HASH_MAX_SIZE]
uint8_t output_block[PSA_HASH_MAX_SIZE]