Mbed TLS v2.28.5
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 
90  unsigned int id;
92 };
93 
94 #define PSA_HASH_OPERATION_INIT { 0, { 0 } }
96 {
98  return v;
99 }
100 
108  unsigned int id;
109 
110  unsigned int iv_required : 1;
111  unsigned int iv_set : 1;
112 
114 
116 };
117 
118 #define PSA_CIPHER_OPERATION_INIT { 0, 0, 0, 0, { 0 } }
120 {
122  return v;
123 }
124 
125 /* Include the context definition for the compiled-in drivers for the composite
126  * algorithms. */
128 
136  unsigned int id;
137  uint8_t mac_size;
138  unsigned int is_sign : 1;
140 };
141 
142 #define PSA_MAC_OPERATION_INIT { 0, 0, 0, { 0 } }
143 static inline struct psa_mac_operation_s psa_mac_operation_init(void)
144 {
146  return v;
147 }
148 
151  unsigned int key_set : 1;
152  unsigned int iv_set : 1;
153  uint8_t iv_size;
154  uint8_t block_size;
155  union {
156  unsigned dummy; /* Enable easier initializing of the union. */
158  } ctx;
159 };
160 
161 #define PSA_AEAD_OPERATION_INIT { 0, 0, 0, 0, 0, { 0 } }
163 {
165  return v;
166 }
167 
168 #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
169 typedef struct {
170  uint8_t *info;
171  size_t info_length;
172 #if PSA_HASH_MAX_SIZE > 0xff
173 #error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
174 #endif
176  uint8_t block_number;
177  unsigned int state : 2;
178  unsigned int info_set : 1;
179  uint8_t output_block[PSA_HASH_MAX_SIZE];
180  uint8_t prk[PSA_HASH_MAX_SIZE];
181  struct psa_mac_operation_s hmac;
183 #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
184 
185 #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
186  defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
187 typedef enum {
188  PSA_TLS12_PRF_STATE_INIT, /* no input provided */
189  PSA_TLS12_PRF_STATE_SEED_SET, /* seed has been set */
190  PSA_TLS12_PRF_STATE_KEY_SET, /* key has been set */
191  PSA_TLS12_PRF_STATE_LABEL_SET, /* label has been set */
192  PSA_TLS12_PRF_STATE_OUTPUT /* output has been started */
194 
196 #if PSA_HASH_MAX_SIZE > 0xff
197 #error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
198 #endif
199 
200  /* Indicates how many bytes in the current HMAC block have
201  * not yet been read by the user. */
202  uint8_t left_in_block;
203 
204  /* The 1-based number of the block. */
205  uint8_t block_number;
206 
208 
209  uint8_t *secret;
211  uint8_t *seed;
212  size_t seed_length;
213  uint8_t *label;
214  size_t label_length;
215 
217 
218  /* `HMAC_hash( prk, A(i) + seed )` in the notation of RFC 5246, Sect. 5. */
221 #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
222  * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
223 
226  unsigned int can_output_key : 1;
227  size_t capacity;
228  union {
229  /* Make the union non-empty even with no supported algorithms. */
230  uint8_t dummy;
231 #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
233 #endif
234 #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
235  defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
237 #endif
238  } ctx;
239 };
240 
241 /* This only zeroes out the first byte in the union, the rest is unspecified. */
242 #define PSA_KEY_DERIVATION_OPERATION_INIT { 0, 0, 0, { 0 } }
244 {
246  return v;
247 }
248 
253 };
255 
256 #define PSA_KEY_POLICY_INIT { 0, 0, 0 }
257 static inline struct psa_key_policy_s psa_key_policy_init(void)
258 {
259  const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT;
260  return v;
261 }
262 
263 /* The type used internally for key sizes.
264  * Public interfaces use size_t, but internally we use a smaller type. */
265 typedef uint16_t psa_key_bits_t;
266 /* The maximum value of the type used to represent bit-sizes.
267  * This is used to mark an invalid key size. */
268 #define PSA_KEY_BITS_TOO_LARGE ((psa_key_bits_t) (-1))
269 /* The maximum size of a key in bits.
270  * Currently defined as the maximum that can be represented, rounded down
271  * to a whole number of bytes.
272  * This is an uncast value so that it can be used in preprocessor
273  * conditionals. */
274 #define PSA_MAX_KEY_BITS 0xfff8
275 
283 typedef uint16_t psa_key_attributes_flag_t;
284 
285 #define MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER \
286  ((psa_key_attributes_flag_t) 0x0001)
287 
288 /* A mask of key attribute flags used externally only.
289  * Only meant for internal checks inside the library. */
290 #define MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ( \
291  MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER | \
292  0)
293 
294 /* A mask of key attribute flags used both internally and externally.
295  * Currently there aren't any. */
296 #define MBEDTLS_PSA_KA_MASK_DUAL_USE ( \
297  0)
298 
299 typedef struct {
307 
308 #define PSA_CORE_KEY_ATTRIBUTES_INIT { PSA_KEY_TYPE_NONE, 0, PSA_KEY_LIFETIME_VOLATILE, \
309  MBEDTLS_SVC_KEY_ID_INIT, PSA_KEY_POLICY_INIT, 0 }
310 
313 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
314  psa_key_slot_number_t slot_number;
315 #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
318 };
319 
320 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
321 #define PSA_KEY_ATTRIBUTES_INIT { PSA_CORE_KEY_ATTRIBUTES_INIT, 0, NULL, 0 }
322 #else
323 #define PSA_KEY_ATTRIBUTES_INIT { PSA_CORE_KEY_ATTRIBUTES_INIT, NULL, 0 }
324 #endif
325 
327 {
329  return v;
330 }
331 
332 static inline void psa_set_key_id(psa_key_attributes_t *attributes,
334 {
335  psa_key_lifetime_t lifetime = attributes->core.lifetime;
336 
337  attributes->core.id = key;
338 
339  if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
340  attributes->core.lifetime =
344  }
345 }
346 
348  const psa_key_attributes_t *attributes)
349 {
350  return attributes->core.id;
351 }
352 
353 #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
354 static inline void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes,
355  mbedtls_key_owner_id_t owner)
356 {
357  attributes->core.id.owner = owner;
358 }
359 #endif
360 
361 static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
362  psa_key_lifetime_t lifetime)
363 {
364  attributes->core.lifetime = lifetime;
365  if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
366 #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
367  attributes->core.id.key_id = 0;
368 #else
369  attributes->core.id = 0;
370 #endif
371  }
372 }
373 
375  const psa_key_attributes_t *attributes)
376 {
377  return attributes->core.lifetime;
378 }
379 
380 static inline void psa_extend_key_usage_flags(psa_key_usage_t *usage_flags)
381 {
382  if (*usage_flags & PSA_KEY_USAGE_SIGN_HASH) {
383  *usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE;
384  }
385 
386  if (*usage_flags & PSA_KEY_USAGE_VERIFY_HASH) {
387  *usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE;
388  }
389 }
390 
391 static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
392  psa_key_usage_t usage_flags)
393 {
394  psa_extend_key_usage_flags(&usage_flags);
395  attributes->core.policy.usage = usage_flags;
396 }
397 
399  const psa_key_attributes_t *attributes)
400 {
401  return attributes->core.policy.usage;
402 }
403 
404 static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
405  psa_algorithm_t alg)
406 {
407  attributes->core.policy.alg = alg;
408 }
409 
411  const psa_key_attributes_t *attributes)
412 {
413  return attributes->core.policy.alg;
414 }
415 
416 /* This function is declared in crypto_extra.h, which comes after this
417  * header file, but we need the function here, so repeat the declaration. */
419  psa_key_type_t type,
420  const uint8_t *data,
421  size_t data_length);
422 
423 static inline void psa_set_key_type(psa_key_attributes_t *attributes,
424  psa_key_type_t type)
425 {
426  if (attributes->domain_parameters == NULL) {
427  /* Common case: quick path */
428  attributes->core.type = type;
429  } else {
430  /* Call the bigger function to free the old domain parameters.
431  * Ignore any errors which may arise due to type requiring
432  * non-default domain parameters, since this function can't
433  * report errors. */
434  (void) psa_set_key_domain_parameters(attributes, type, NULL, 0);
435  }
436 }
437 
439  const psa_key_attributes_t *attributes)
440 {
441  return attributes->core.type;
442 }
443 
444 static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
445  size_t bits)
446 {
447  if (bits > PSA_MAX_KEY_BITS) {
448  attributes->core.bits = PSA_KEY_BITS_TOO_LARGE;
449  } else {
450  attributes->core.bits = (psa_key_bits_t) bits;
451  }
452 }
453 
454 static inline size_t psa_get_key_bits(
455  const psa_key_attributes_t *attributes)
456 {
457  return attributes->core.bits;
458 }
459 
460 #ifdef __cplusplus
461 }
462 #endif
463 
464 #endif /* PSA_CRYPTO_STRUCT_H */
unsigned int is_sign
psa_key_lifetime_t lifetime
psa_tls12_prf_key_derivation_t tls12_prf
uint8_t Ai[PSA_HASH_MAX_SIZE]
static void psa_set_key_usage_flags(psa_key_attributes_t *attributes, psa_key_usage_t usage_flags)
#define PSA_MAX_KEY_BITS
static psa_key_lifetime_t psa_get_key_lifetime(const psa_key_attributes_t *attributes)
union psa_aead_operation_s::@7 ctx
#define PSA_MAC_OPERATION_INIT
static struct psa_aead_operation_s psa_aead_operation_init(void)
psa_key_usage_t usage
uint16_t psa_key_bits_t
psa_algorithm_t alg2
psa_tls12_prf_key_derivation_state_t
psa_key_attributes_flag_t flags
static void psa_set_key_id(psa_key_attributes_t *attributes, mbedtls_svc_key_id_t key)
Configuration options (set of defines)
psa_core_key_attributes_t core
#define PSA_CIPHER_OPERATION_INIT
This file contains CMAC definitions and functions.
#define PSA_KEY_ATTRIBUTES_INIT
#define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)
#define PSA_KEY_USAGE_VERIFY_MESSAGE
psa_driver_hash_context_t ctx
Definition: crypto_struct.h:91
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
psa_algorithm_t alg
psa_algorithm_t alg
psa_driver_cipher_context_t ctx
uint64_t psa_key_slot_number_t
uint16_t psa_key_attributes_flag_t
#define PSA_HASH_MAX_SIZE
Definition: crypto_sizes.h:136
psa_tls12_prf_key_derivation_state_t state
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
Definition: crypto_types.h:137
psa_algorithm_t alg
#define PSA_KEY_LIFETIME_PERSISTENT
union psa_key_derivation_s::@8 ctx
uint32_t psa_key_usage_t
Encoding of permitted usage on a key.
Definition: crypto_types.h:326
mbedtls_cipher_context_t cipher
uint16_t psa_key_type_t
Encoding of a key type.
Definition: crypto_types.h:81
#define PSA_KEY_POLICY_INIT
#define PSA_KEY_DERIVATION_OPERATION_INIT
psa_hkdf_key_derivation_t hkdf
static psa_algorithm_t psa_get_key_algorithm(const psa_key_attributes_t *attributes)
#define PSA_AEAD_OPERATION_INIT
unsigned int key_set
#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)
#define PSA_HASH_OPERATION_INIT
Definition: crypto_struct.h:94
psa_key_id_t mbedtls_svc_key_id_t
Definition: crypto_types.h:295
static struct psa_mac_operation_s psa_mac_operation_init(void)
mbedtls_svc_key_id_t id
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)
psa_key_policy_t policy
This file contains GCM definitions and functions.
unsigned int can_output_key
static struct psa_key_attributes_s psa_key_attributes_init(void)
uint8_t output_block[PSA_HASH_MAX_SIZE]
struct psa_tls12_prf_key_derivation_s psa_tls12_prf_key_derivation_t
static size_t psa_get_key_bits(const psa_key_attributes_t *attributes)
static struct psa_cipher_operation_s psa_cipher_operation_init(void)
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:186
static void psa_extend_key_usage_flags(psa_key_usage_t *usage_flags)
int32_t psa_status_t
Function return status.
Definition: crypto_types.h:62
static void psa_set_key_algorithm(psa_key_attributes_t *attributes, psa_algorithm_t alg)
unsigned int iv_required
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)
#define PSA_KEY_USAGE_VERIFY_HASH
static struct psa_key_policy_s psa_key_policy_init(void)
psa_driver_mac_context_t ctx
#define PSA_KEY_USAGE_SIGN_HASH
static struct psa_hash_operation_s psa_hash_operation_init(void)
Definition: crypto_struct.h:95