Mistake on this page?
Report an issue in GitHub or email us
TARGET_MBED_PSA_SRV/mbedtls/crypto_struct.h
1 /**
2  * \file psa/crypto_struct.h
3  *
4  * \brief PSA cryptography module: Mbed TLS structured type implementations
5  *
6  * \note This file may not be included directly. Applications must
7  * include psa/crypto.h.
8  *
9  * This file contains the definitions of some data structures with
10  * implementation-specific definitions.
11  *
12  * In implementations with isolation between the application and the
13  * cryptography module, it is expected that the front-end and the back-end
14  * would have different versions of this file.
15  *
16  * <h3>Design notes about multipart operation structures</h3>
17  *
18  * Each multipart operation structure contains a `psa_algorithm_t alg`
19  * field which indicates which specific algorithm the structure is for.
20  * When the structure is not in use, `alg` is 0. Most of the structure
21  * consists of a union which is discriminated by `alg`.
22  *
23  * Note that when `alg` is 0, the content of other fields is undefined.
24  * In particular, it is not guaranteed that a freshly-initialized structure
25  * is all-zero: we initialize structures to something like `{0, 0}`, which
26  * is only guaranteed to initializes the first member of the union;
27  * GCC and Clang initialize the whole structure to 0 (at the time of writing),
28  * but MSVC and CompCert don't.
29  *
30  * In Mbed Crypto, multipart operation structures live independently from
31  * the key. This allows Mbed Crypto to free the key objects when destroying
32  * a key slot. If a multipart operation needs to remember the key after
33  * the setup function returns, the operation structure needs to contain a
34  * copy of the key.
35  */
36 /*
37  * Copyright (C) 2018, ARM Limited, All Rights Reserved
38  * SPDX-License-Identifier: Apache-2.0
39  *
40  * Licensed under the Apache License, Version 2.0 (the "License"); you may
41  * not use this file except in compliance with the License.
42  * You may obtain a copy of the License at
43  *
44  * http://www.apache.org/licenses/LICENSE-2.0
45  *
46  * Unless required by applicable law or agreed to in writing, software
47  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
48  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
49  * See the License for the specific language governing permissions and
50  * limitations under the License.
51  *
52  * This file is part of mbed TLS (https://tls.mbed.org)
53  */
54 
55 #ifndef PSA_CRYPTO_STRUCT_H
56 #define PSA_CRYPTO_STRUCT_H
57 
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61 
62 /* Include the Mbed TLS configuration file, the way Mbed TLS does it
63  * in each of its header files. */
64 #if !defined(MBEDTLS_CONFIG_FILE)
65 #include "mbedtls/config.h"
66 #else
67 #include MBEDTLS_CONFIG_FILE
68 #endif
69 
70 #include "mbedtls/cipher.h"
71 #include "mbedtls/cmac.h"
72 #include "mbedtls/gcm.h"
73 #include "mbedtls/md.h"
74 #include "mbedtls/md2.h"
75 #include "mbedtls/md4.h"
76 #include "mbedtls/md5.h"
77 #include "mbedtls/ripemd160.h"
78 #include "mbedtls/sha1.h"
79 #include "mbedtls/sha256.h"
80 #include "mbedtls/sha512.h"
81 
83 {
84  psa_algorithm_t alg;
85  union
86  {
87  unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
88 #if defined(MBEDTLS_MD2_C)
89  mbedtls_md2_context md2;
90 #endif
91 #if defined(MBEDTLS_MD4_C)
92  mbedtls_md4_context md4;
93 #endif
94 #if defined(MBEDTLS_MD5_C)
95  mbedtls_md5_context md5;
96 #endif
97 #if defined(MBEDTLS_RIPEMD160_C)
98  mbedtls_ripemd160_context ripemd160;
99 #endif
100 #if defined(MBEDTLS_SHA1_C)
101  mbedtls_sha1_context sha1;
102 #endif
103 #if defined(MBEDTLS_SHA256_C)
104  mbedtls_sha256_context sha256;
105 #endif
106 #if defined(MBEDTLS_SHA512_C)
107  mbedtls_sha512_context sha512;
108 #endif
109  } ctx;
110 };
111 
112 #define PSA_HASH_OPERATION_INIT {0, {0}}
113 static inline struct psa_hash_operation_s psa_hash_operation_init( void )
114 {
115  const struct psa_hash_operation_s v = PSA_HASH_OPERATION_INIT;
116  return( v );
117 }
118 
119 #if defined(MBEDTLS_MD_C)
120 typedef struct
121 {
122  /** The hash context. */
123  struct psa_hash_operation_s hash_ctx;
124  /** The HMAC part of the context. */
125  uint8_t opad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
126 } psa_hmac_internal_data;
127 #endif /* MBEDTLS_MD_C */
128 
130 {
131  psa_algorithm_t alg;
132  unsigned int key_set : 1;
133  unsigned int iv_required : 1;
134  unsigned int iv_set : 1;
135  unsigned int has_input : 1;
136  unsigned int is_sign : 1;
137  uint8_t mac_size;
138  union
139  {
140  unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
141 #if defined(MBEDTLS_MD_C)
142  psa_hmac_internal_data hmac;
143 #endif
144 #if defined(MBEDTLS_CMAC_C)
145  mbedtls_cipher_context_t cmac;
146 #endif
147  } ctx;
148 };
149 
150 #define PSA_MAC_OPERATION_INIT {0, 0, 0, 0, 0, 0, 0, {0}}
151 static inline struct psa_mac_operation_s psa_mac_operation_init( void )
152 {
153  const struct psa_mac_operation_s v = PSA_MAC_OPERATION_INIT;
154  return( v );
155 }
156 
158 {
159  psa_algorithm_t alg;
160  unsigned int key_set : 1;
161  unsigned int iv_required : 1;
162  unsigned int iv_set : 1;
163  uint8_t iv_size;
164  uint8_t block_size;
165  union
166  {
167  unsigned dummy; /* Enable easier initializing of the union. */
168  mbedtls_cipher_context_t cipher;
169  } ctx;
170 };
171 
172 #define PSA_CIPHER_OPERATION_INIT {0, 0, 0, 0, 0, 0, {0}}
173 static inline struct psa_cipher_operation_s psa_cipher_operation_init( void )
174 {
175  const struct psa_cipher_operation_s v = PSA_CIPHER_OPERATION_INIT;
176  return( v );
177 }
178 
180 {
181  psa_algorithm_t alg;
182  unsigned int key_set : 1;
183  unsigned int iv_set : 1;
184  uint8_t iv_size;
185  uint8_t block_size;
186  union
187  {
188  unsigned dummy; /* Enable easier initializing of the union. */
189  mbedtls_cipher_context_t cipher;
190  } ctx;
191 };
192 
193 #define PSA_AEAD_OPERATION_INIT {0, 0, 0, 0, 0, {0}}
194 static inline struct psa_aead_operation_s psa_aead_operation_init( void )
195 {
196  const struct psa_aead_operation_s v = PSA_AEAD_OPERATION_INIT;
197  return( v );
198 }
199 
200 #if defined(MBEDTLS_MD_C)
201 typedef struct
202 {
203  uint8_t *info;
204  size_t info_length;
205  psa_hmac_internal_data hmac;
206  uint8_t prk[PSA_HASH_MAX_SIZE];
207  uint8_t output_block[PSA_HASH_MAX_SIZE];
208 #if PSA_HASH_MAX_SIZE > 0xff
209 #error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
210 #endif
211  uint8_t offset_in_block;
212  uint8_t block_number;
213  unsigned int state : 2;
214  unsigned int info_set : 1;
215 } psa_hkdf_key_derivation_t;
216 #endif /* MBEDTLS_MD_C */
217 
218 #if defined(MBEDTLS_MD_C)
219 typedef enum
220 {
221  TLS12_PRF_STATE_INIT, /* no input provided */
222  TLS12_PRF_STATE_SEED_SET, /* seed has been set */
223  TLS12_PRF_STATE_KEY_SET, /* key has been set */
224  TLS12_PRF_STATE_LABEL_SET, /* label has been set */
225  TLS12_PRF_STATE_OUTPUT /* output has been started */
226 } psa_tls12_prf_key_derivation_state_t;
227 
228 typedef struct psa_tls12_prf_key_derivation_s
229 {
230 #if PSA_HASH_MAX_SIZE > 0xff
231 #error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
232 #endif
233 
234  /* Indicates how many bytes in the current HMAC block have
235  * not yet been read by the user. */
236  uint8_t left_in_block;
237 
238  /* The 1-based number of the block. */
239  uint8_t block_number;
240 
241  psa_tls12_prf_key_derivation_state_t state;
242 
243  uint8_t *seed;
244  size_t seed_length;
245  uint8_t *label;
246  size_t label_length;
247  psa_hmac_internal_data hmac;
248  uint8_t Ai[PSA_HASH_MAX_SIZE];
249 
250  /* `HMAC_hash( prk, A(i) + seed )` in the notation of RFC 5246, Sect. 5. */
251  uint8_t output_block[PSA_HASH_MAX_SIZE];
252 } psa_tls12_prf_key_derivation_t;
253 #endif /* MBEDTLS_MD_C */
254 
256 {
257  psa_algorithm_t alg;
258  unsigned int can_output_key : 1;
259  size_t capacity;
260  union
261  {
262  /* Make the union non-empty even with no supported algorithms. */
263  uint8_t dummy;
264 #if defined(MBEDTLS_MD_C)
265  psa_hkdf_key_derivation_t hkdf;
266  psa_tls12_prf_key_derivation_t tls12_prf;
267 #endif
268  } ctx;
269 };
270 
271 /* This only zeroes out the first byte in the union, the rest is unspecified. */
272 #define PSA_KEY_DERIVATION_OPERATION_INIT {0, 0, 0, {0}}
273 static inline struct psa_key_derivation_s psa_key_derivation_operation_init( void )
274 {
275  const struct psa_key_derivation_s v = PSA_KEY_DERIVATION_OPERATION_INIT;
276  return( v );
277 }
278 
280 {
281  psa_key_usage_t usage;
282  psa_algorithm_t alg;
283  psa_algorithm_t alg2;
284 };
285 typedef struct psa_key_policy_s psa_key_policy_t;
286 
287 #define PSA_KEY_POLICY_INIT {0, 0, 0}
288 static inline struct psa_key_policy_s psa_key_policy_init( void )
289 {
290  const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT;
291  return( v );
292 }
293 
294 /* The type used internally for key sizes.
295  * Public interfaces use size_t, but internally we use a smaller type. */
296 typedef uint16_t psa_key_bits_t;
297 /* The maximum value of the type used to represent bit-sizes.
298  * This is used to mark an invalid key size. */
299 #define PSA_KEY_BITS_TOO_LARGE ( (psa_key_bits_t) ( -1 ) )
300 /* The maximum size of a key in bits.
301  * Currently defined as the maximum that can be represented, rounded down
302  * to a whole number of bytes.
303  * This is an uncast value so that it can be used in preprocessor
304  * conditionals. */
305 #define PSA_MAX_KEY_BITS 0xfff8
306 
307 /** A mask of flags that can be stored in key attributes.
308  *
309  * This type is also used internally to store flags in slots. Internal
310  * flags are defined in library/psa_crypto_core.h. Internal flags may have
311  * the same value as external flags if they are properly handled during
312  * key creation and in psa_get_key_attributes.
313  */
314 typedef uint16_t psa_key_attributes_flag_t;
315 
316 #define MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER \
317  ( (psa_key_attributes_flag_t) 0x0001 )
318 
319 /* A mask of key attribute flags used externally only.
320  * Only meant for internal checks inside the library. */
321 #define MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ( \
322  MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER | \
323  0 )
324 
325 /* A mask of key attribute flags used both internally and externally.
326  * Currently there aren't any. */
327 #define MBEDTLS_PSA_KA_MASK_DUAL_USE ( \
328  0 )
329 
330 typedef struct
331 {
332  psa_key_type_t type;
333  psa_key_bits_t bits;
334  psa_key_lifetime_t lifetime;
335  psa_key_id_t id;
336  psa_key_policy_t policy;
337  psa_key_attributes_flag_t flags;
339 
340 #define PSA_CORE_KEY_ATTRIBUTES_INIT {PSA_KEY_TYPE_NONE, 0, PSA_KEY_LIFETIME_VOLATILE, PSA_KEY_ID_INIT, PSA_KEY_POLICY_INIT, 0}
341 
343 {
345 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
346  psa_key_slot_number_t slot_number;
347 #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
348  void *domain_parameters;
349  size_t domain_parameters_size;
350 };
351 
352 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
353 #define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT, 0, NULL, 0}
354 #else
355 #define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT, NULL, 0}
356 #endif
357 
358 static inline struct psa_key_attributes_s psa_key_attributes_init( void )
359 {
360  const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT;
361  return( v );
362 }
363 
364 static inline void psa_set_key_id(psa_key_attributes_t *attributes,
365  psa_key_id_t id)
366 {
367  attributes->core.id = id;
368  if( attributes->core.lifetime == PSA_KEY_LIFETIME_VOLATILE )
369  attributes->core.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
370 }
371 
372 static inline psa_key_id_t psa_get_key_id(
373  const psa_key_attributes_t *attributes)
374 {
375  return( attributes->core.id );
376 }
377 
378 static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
379  psa_key_lifetime_t lifetime)
380 {
381  attributes->core.lifetime = lifetime;
382  if( lifetime == PSA_KEY_LIFETIME_VOLATILE )
383  {
384 #ifdef MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER
385  attributes->core.id.key_id = 0;
386  attributes->core.id.owner = 0;
387 #else
388  attributes->core.id = 0;
389 #endif
390  }
391 }
392 
394  const psa_key_attributes_t *attributes)
395 {
396  return( attributes->core.lifetime );
397 }
398 
399 static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
400  psa_key_usage_t usage_flags)
401 {
402  attributes->core.policy.usage = usage_flags;
403 }
404 
406  const psa_key_attributes_t *attributes)
407 {
408  return( attributes->core.policy.usage );
409 }
410 
411 static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
412  psa_algorithm_t alg)
413 {
414  attributes->core.policy.alg = alg;
415 }
416 
418  const psa_key_attributes_t *attributes)
419 {
420  return( attributes->core.policy.alg );
421 }
422 
423 /* This function is declared in crypto_extra.h, which comes after this
424  * header file, but we need the function here, so repeat the declaration. */
426  psa_key_type_t type,
427  const uint8_t *data,
428  size_t data_length);
429 
430 static inline void psa_set_key_type(psa_key_attributes_t *attributes,
431  psa_key_type_t type)
432 {
433  if( attributes->domain_parameters == NULL )
434  {
435  /* Common case: quick path */
436  attributes->core.type = type;
437  }
438  else
439  {
440  /* Call the bigger function to free the old domain paramteres.
441  * Ignore any errors which may arise due to type requiring
442  * non-default domain parameters, since this function can't
443  * report errors. */
444  (void) psa_set_key_domain_parameters( attributes, type, NULL, 0 );
445  }
446 }
447 
448 static inline psa_key_type_t psa_get_key_type(
449  const psa_key_attributes_t *attributes)
450 {
451  return( attributes->core.type );
452 }
453 
454 static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
455  size_t bits)
456 {
457  if( bits > PSA_MAX_KEY_BITS )
458  attributes->core.bits = PSA_KEY_BITS_TOO_LARGE;
459  else
460  attributes->core.bits = (psa_key_bits_t) bits;
461 }
462 
463 static inline size_t psa_get_key_bits(
464  const psa_key_attributes_t *attributes)
465 {
466  return( attributes->core.bits );
467 }
468 
469 #ifdef __cplusplus
470 }
471 #endif
472 
473 #endif /* PSA_CRYPTO_STRUCT_H */
static void psa_set_key_id(psa_key_attributes_t *attributes, psa_key_id_t id)
Declare a key as persistent and set its key identifier.
static void psa_set_key_bits(psa_key_attributes_t *attributes, size_t bits)
Declare the size of a key.
void md5(unsigned char *input, int ilen, unsigned char output[16])
Output = MD5( input buffer )
#define PSA_KEY_LIFETIME_VOLATILE
A volatile key only exists as long as the handle to it is not closed.
uint32_t psa_key_id_t
Encoding of identifiers of persistent keys.
The key size.
static psa_algorithm_t psa_get_key_algorithm(const psa_key_attributes_t *attributes)
Retrieve the algorithm policy from key attributes.
static void psa_set_key_usage_flags(psa_key_attributes_t *attributes, psa_key_usage_t usage_flags)
Declare usage flags for a key.
static void psa_set_key_lifetime(psa_key_attributes_t *attributes, psa_key_lifetime_t lifetime)
Set the location of a persistent key.
static psa_key_lifetime_t psa_get_key_lifetime(const psa_key_attributes_t *attributes)
Retrieve the lifetime from key attributes.
static void psa_set_key_type(psa_key_attributes_t *attributes, psa_key_type_t type)
Declare the type of a key.
static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes)
Retrieve the key identifier from key attributes.
static size_t psa_get_key_bits(const psa_key_attributes_t *attributes)
Retrieve the key size from key attributes.
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)
Set domain parameters for a key.
uint64_t psa_key_slot_number_t
An internal designation of a key slot between the core part of the PSA Crypto implementation and the ...
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
#define PSA_KEY_LIFETIME_PERSISTENT
The default storage area for persistent keys.
uint32_t psa_key_usage_t
Encoding of permitted usage on a key.
static void psa_set_key_algorithm(psa_key_attributes_t *attributes, psa_algorithm_t alg)
Declare the permitted algorithm policy for a key.
uint16_t psa_key_type_t
Encoding of a key type.
static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes)
Retrieve the key type from key attributes.
static psa_key_usage_t psa_get_key_usage_flags(const psa_key_attributes_t *attributes)
Retrieve the usage flags from key attributes.
uint32_t psa_key_lifetime_t
Encoding of key lifetimes.
int32_t psa_status_t
Function return status.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.