NCBI C++ ToolKit
crypto_struct.h
Go to the documentation of this file.

Go to the SVN repository for this file.

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  * For multipart operations without driver delegation support, each multipart
19  * operation structure contains a `psa_algorithm_t alg` field which indicates
20  * which specific algorithm the structure is for. When the structure is not in
21  * use, `alg` is 0. Most of the structure consists of a union which is
22  * discriminated by `alg`.
23  *
24  * For multipart operations with driver delegation support, each multipart
25  * operation structure contains an `unsigned int id` field indicating which
26  * driver got assigned to do the operation. When the structure is not in use,
27  * 'id' is 0. The structure contains also a driver context which is the union
28  * of the contexts of all drivers able to handle the type of multipart
29  * operation.
30  *
31  * Note that when `alg` or `id` is 0, the content of other fields is undefined.
32  * In particular, it is not guaranteed that a freshly-initialized structure
33  * is all-zero: we initialize structures to something like `{0, 0}`, which
34  * is only guaranteed to initializes the first member of the union;
35  * GCC and Clang initialize the whole structure to 0 (at the time of writing),
36  * but MSVC and CompCert don't.
37  *
38  * In Mbed Crypto, multipart operation structures live independently from
39  * the key. This allows Mbed Crypto to free the key objects when destroying
40  * a key slot. If a multipart operation needs to remember the key after
41  * the setup function returns, the operation structure needs to contain a
42  * copy of the key.
43  */
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  /** Unique ID indicating which driver got assigned to do the
85  * operation. Since driver contexts are driver-specific, swapping
86  * drivers halfway through the operation is not supported.
87  * ID values are auto-generated in psa_driver_wrappers.h.
88  * ID value zero means the context is not valid or not assigned to
89  * any driver (i.e. the driver context is not active, in use). */
90  unsigned int id;
92 };
93 
94 #define PSA_HASH_OPERATION_INIT { 0, { 0 } }
95 static inline struct psa_hash_operation_s psa_hash_operation_init(void)
96 {
98  return v;
99 }
100 
102  /** Unique ID indicating which driver got assigned to do the
103  * operation. Since driver contexts are driver-specific, swapping
104  * drivers halfway through the operation is not supported.
105  * ID values are auto-generated in psa_crypto_driver_wrappers.h
106  * ID value zero means the context is not valid or not assigned to
107  * any driver (i.e. none of the driver contexts are active). */
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 } }
119 static inline struct psa_cipher_operation_s psa_cipher_operation_init(void)
120 {
122  return v;
123 }
124 
125 /* Include the context definition for the compiled-in drivers for the composite
126  * algorithms. */
128 
130  /** Unique ID indicating which driver got assigned to do the
131  * operation. Since driver contexts are driver-specific, swapping
132  * drivers halfway through the operation is not supported.
133  * ID values are auto-generated in psa_driver_wrappers.h
134  * ID value zero means the context is not valid or not assigned to
135  * any driver (i.e. none of the driver contexts are active). */
136  unsigned int id;
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;
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 } }
162 static inline struct psa_aead_operation_s psa_aead_operation_init(void)
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
175  uint8_t offset_in_block;
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];
181  struct psa_mac_operation_s hmac;
182 } psa_hkdf_key_derivation_t;
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 */
193 } psa_tls12_prf_key_derivation_state_t;
194 
195 typedef struct psa_tls12_prf_key_derivation_s {
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 
207  psa_tls12_prf_key_derivation_state_t state;
208 
209  uint8_t *secret;
210  size_t secret_length;
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. */
219  uint8_t output_block[PSA_HASH_MAX_SIZE];
220 } psa_tls12_prf_key_derivation_t;
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. */
231 #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
232  psa_hkdf_key_derivation_t hkdf;
233 #endif
234 #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
235  defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
236  psa_tls12_prf_key_derivation_t tls12_prf;
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 };
254 typedef struct psa_key_policy_s psa_key_policy_t;
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. */
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 
276 /** A mask of flags that can be stored in key attributes.
277  *
278  * This type is also used internally to store flags in slots. Internal
279  * flags are defined in library/psa_crypto_core.h. Internal flags may have
280  * the same value as external flags if they are properly handled during
281  * key creation and in psa_get_key_attributes.
282  */
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 
326 static inline struct psa_key_attributes_s psa_key_attributes_init(void)
327 {
329  return v;
330 }
331 
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 
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 
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 
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 
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 
400 {
401  return attributes->core.policy.usage;
402 }
403 
405  psa_algorithm_t alg)
406 {
407  attributes->core.policy.alg = alg;
408 }
409 
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. */
420  const uint8_t *data,
421  size_t data_length);
422 
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. */
435  }
436 }
437 
440 {
441  return attributes->core.type;
442 }
443 
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(
456 {
457  return attributes->core.bits;
458 }
459 
460 #ifdef __cplusplus
461 }
462 #endif
463 
464 #endif /* PSA_CRYPTO_STRUCT_H */
This file contains CMAC definitions and functions.
#define PSA_HASH_MAX_SIZE
Maximum size of a hash.
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
A mask of flags that can be stored in key attributes.
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)
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)
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)
static const struct attribute attributes[]
Definition: attributes.c:165
static int type
Definition: getdata.c:31
char data[12]
Definition: iconv.c:80
unsigned char uint8_t
Uint2 uint16_t
This file contains GCM definitions and functions.
#define NULL
Definition: ncbistd.hpp:225
#define PSA_MAC_OPERATION_INIT
This macro returns a suitable initializer for a MAC operation object of type psa_mac_operation_t.
#define PSA_AEAD_OPERATION_INIT
This macro returns a suitable initializer for an AEAD operation object of type psa_aead_operation_t.
#define PSA_KEY_ATTRIBUTES_INIT
This macro returns a suitable initializer for a key attribute structure of type psa_key_attributes_t.
static const char label[]
#define PSA_CIPHER_OPERATION_INIT
This macro returns a suitable initializer for a cipher operation object of type psa_cipher_operation_...
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
This macro returns a suitable initializer for a hash operation object of type psa_hash_operation_t.
Definition: crypto_struct.h:94
#define PSA_KEY_DERIVATION_OPERATION_INIT
This macro returns a suitable initializer for a key derivation operation object of type psa_key_deriv...
#define PSA_KEY_LIFETIME_PERSISTENT
The default lifetime for persistent keys.
#define PSA_KEY_LIFETIME_GET_LOCATION(lifetime)
#define PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)
Whether a key lifetime indicates that the key is volatile.
uint32_t psa_key_lifetime_t
Encoding of key lifetimes.
Definition: crypto_types.h:186
psa_key_id_t mbedtls_svc_key_id_t
Encoding of key identifiers as seen inside the PSA Crypto implementation.
Definition: crypto_types.h:295
#define PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(persistence, location)
Construct a lifetime from a persistence level and a location.
#define PSA_KEY_USAGE_SIGN_HASH
Whether the key may be used to sign a message.
#define PSA_KEY_USAGE_SIGN_MESSAGE
Whether the key may be used to sign a message.
uint32_t psa_key_usage_t
Encoding of permitted usage on a key.
Definition: crypto_types.h:326
#define PSA_KEY_USAGE_VERIFY_MESSAGE
Whether the key may be used to verify a message.
#define PSA_KEY_USAGE_VERIFY_HASH
Whether the key may be used to verify a message signature.
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 ...
static MDB_envinfo info
Definition: mdb_load.c:37
const struct ncbi::grid::netcache::search::fields::KEY key
Configuration options (set of defines)
test_type_t type
Definition: attributes.c:161
Generic cipher context.
Definition: cipher.h:317
unsigned int key_set
psa_algorithm_t alg
mbedtls_cipher_context_t cipher
union psa_aead_operation_s::@977 ctx
unsigned int id
Unique ID indicating which driver got assigned to do the operation.
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:91
unsigned int id
Unique ID indicating which driver got assigned to do the operation.
Definition: crypto_struct.h:90
psa_core_key_attributes_t core
union psa_key_derivation_s::@978 ctx
unsigned int can_output_key
psa_algorithm_t alg
psa_key_usage_t usage
psa_algorithm_t alg2
psa_algorithm_t alg
unsigned int id
Unique ID indicating which driver got assigned to do the operation.
psa_driver_mac_context_t ctx
unsigned int is_sign
Definition: type.c:6
static int seed
Definition: test_table.cpp:132
Modified on Sat Apr 13 11:43:48 2024 by modify_doxy.py rev. 669887