XenevaOS
Loading...
Searching...
No Matches
psa_crypto_core.h
Go to the documentation of this file.
1/*
2 * PSA crypto core internal interfaces
3 */
4/*
5 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7 */
8
9#ifndef PSA_CRYPTO_CORE_H
10#define PSA_CRYPTO_CORE_H
11
12/*
13 * Include the build-time configuration information header. Here, we do not
14 * include `"mbedtls/build_info.h"` directly but `"psa/build_info.h"`, which
15 * is basically just an alias to it. This is to ease the maintenance of the
16 * TF-PSA-Crypto repository which has a different build system and
17 * configuration.
18 */
19#include "psa/build_info.h"
20
21#include "psa/crypto.h"
23#if defined(MBEDTLS_THREADING_C)
24#include "mbedtls/threading.h"
25#endif
26
38
45
49typedef struct {
50 /* This field is accessed in a lot of places. Putting it first
51 * reduces the code size. */
53
54 /*
55 * The current state of the key slot, as described in
56 * docs/architecture/psa-thread-safety/psa-thread-safety.md.
57 *
58 * Library functions can modify the state of a key slot by calling
59 * psa_key_slot_state_transition.
60 *
61 * The state variable is used to help determine whether library functions
62 * which operate on the slot succeed. For example, psa_finish_key_creation,
63 * which transfers the state of a slot from PSA_SLOT_FILLING to
64 * PSA_SLOT_FULL, must fail with error code PSA_ERROR_CORRUPTION_DETECTED
65 * if the state of the slot is not PSA_SLOT_FILLING.
66 *
67 * Library functions which traverse the array of key slots only consider
68 * slots that are in a suitable state for the function.
69 * For example, psa_get_and_lock_key_slot_in_memory, which finds a slot
70 * containing a given key ID, will only check slots whose state variable is
71 * PSA_SLOT_FULL.
72 */
74
75#if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
76 /* The index of the slice containing this slot.
77 * This field must be filled if the slot contains a key
78 * (including keys being created or destroyed), and can be either
79 * filled or 0 when the slot is free.
80 *
81 * In most cases, the slice index can be deduced from the key identifer.
82 * We keep it in a separate field for robustness (it reduces the chance
83 * that a coding mistake in the key store will result in accessing the
84 * wrong slice), and also so that it's available even on code paths
85 * during creation or destruction where the key identifier might not be
86 * filled in.
87 * */
88 uint8_t slice_index;
89#endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
90
91 union {
92 struct {
93 /* The index of the next slot in the free list for this
94 * slice, relative * to the next array element.
95 *
96 * That is, 0 means the next slot, 1 means the next slot
97 * but one, etc. -1 would mean the slot itself. -2 means
98 * the previous slot, etc.
99 *
100 * If this is beyond the array length, the free list ends with the
101 * current element.
102 *
103 * The reason for this strange encoding is that 0 means the next
104 * element. This way, when we allocate a slice and initialize it
105 * to all-zero, the slice is ready for use, with a free list that
106 * consists of all the slots in order.
107 */
110
111 struct {
112 /*
113 * Number of functions registered as reading the material in the key slot.
114 *
115 * Library functions must not write directly to registered_readers
116 *
117 * A function must call psa_register_read(slot) before reading
118 * the current contents of the slot for an operation.
119 * They then must call psa_unregister_read(slot) once they have
120 * finished reading the current contents of the slot. If the key
121 * slot mutex is not held (when mutexes are enabled), this call
122 * must be done via a call to
123 * psa_unregister_read_under_mutex(slot).
124 * A function must call psa_key_slot_has_readers(slot) to check if
125 * the slot is in use for reading.
126 *
127 * This counter is used to prevent resetting the key slot while
128 * the library may access it. For example, such control is needed
129 * in the following scenarios:
130 * . In case of key slot starvation, all key slots contain the
131 * description of a key, and the library asks for the
132 * description of a persistent key not present in the
133 * key slots, the key slots currently accessed by the
134 * library cannot be reclaimed to free a key slot to load
135 * the persistent key.
136 * . In case of a multi-threaded application where one thread
137 * asks to close or purge or destroy a key while it is in use
138 * by the library through another thread. */
140 } occupied;
141 } var;
142
143 /* Dynamically allocated key data buffer.
144 * Format as specified in psa_export_key(). */
145 struct key_data {
146#if defined(MBEDTLS_PSA_STATIC_KEY_SLOTS)
148#else
150#endif
151 size_t bytes;
152 } key;
154
155#if defined(MBEDTLS_THREADING_C)
156
166#define PSA_THREADING_CHK_RET(f) \
167 do \
168 { \
169 if ((f) != 0) { \
170 if (status == PSA_SUCCESS) { \
171 return PSA_ERROR_SERVICE_FAILURE; \
172 } \
173 return status; \
174 } \
175 } while (0);
176
186#define PSA_THREADING_CHK_GOTO_EXIT(f) \
187 do \
188 { \
189 if ((f) != 0) { \
190 if (status == PSA_SUCCESS) { \
191 status = PSA_ERROR_SERVICE_FAILURE; \
192 } \
193 goto exit; \
194 } \
195 } while (0);
196#endif
197
206static inline int psa_key_slot_has_readers(const psa_key_slot_t *slot)
207{
208 return slot->var.occupied.registered_readers > 0;
209}
210
211#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
218static inline psa_key_slot_number_t psa_key_slot_get_slot_number(
219 const psa_key_slot_t *slot)
220{
221 return *((psa_key_slot_number_t *) (slot->key.data));
222}
223#endif
224
244
258 size_t buffer_length);
259
262
281 const uint8_t *data,
282 size_t data_length);
283
294
322 const psa_key_attributes_t *attributes,
323 const uint8_t *data, size_t data_length,
324 uint8_t *key_buffer, size_t key_buffer_size,
325 size_t *key_buffer_length, size_t *bits);
326
350 const psa_key_attributes_t *attributes,
351 const uint8_t *key_buffer, size_t key_buffer_size,
352 uint8_t *data, size_t data_size, size_t *data_length);
353
378 const psa_key_attributes_t *attributes,
379 const uint8_t *key_buffer, size_t key_buffer_size,
380 uint8_t *data, size_t data_size, size_t *data_length);
381
392 const psa_custom_key_parameters_t *custom,
393 size_t custom_data_length);
394
419 const psa_custom_key_parameters_t *custom,
420 const uint8_t *custom_data,
421 size_t custom_data_length,
422 uint8_t *key_buffer,
423 size_t key_buffer_size,
424 size_t *key_buffer_length);
425
464 const psa_key_attributes_t *attributes,
465 const uint8_t *key_buffer, size_t key_buffer_size,
466 psa_algorithm_t alg, const uint8_t *input, size_t input_length,
467 uint8_t *signature, size_t signature_size, size_t *signature_length);
468
501 const psa_key_attributes_t *attributes,
502 const uint8_t *key_buffer, size_t key_buffer_size,
503 psa_algorithm_t alg, const uint8_t *input, size_t input_length,
504 const uint8_t *signature, size_t signature_length);
505
540 const psa_key_attributes_t *attributes,
541 const uint8_t *key_buffer, size_t key_buffer_size,
542 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
543 uint8_t *signature, size_t signature_size, size_t *signature_length);
544
575 const psa_key_attributes_t *attributes,
576 const uint8_t *key_buffer, size_t key_buffer_size,
577 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
578 const uint8_t *signature, size_t signature_length);
579
598 size_t bits);
599
647 const psa_key_attributes_t *attributes,
648 const uint8_t *key_buffer,
649 size_t key_buffer_size,
650 psa_algorithm_t alg,
651 const uint8_t *peer_key,
652 size_t peer_key_length,
653 uint8_t *shared_secret,
654 size_t shared_secret_size,
655 size_t *shared_secret_length);
656
673
687
708
729
764 const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
765 size_t key_buffer_size, psa_algorithm_t alg,
766 const uint8_t *hash, size_t hash_length);
767
811 uint8_t *signature, size_t signature_size,
812 size_t *signature_length);
813
831
869 const psa_key_attributes_t *attributes,
870 const uint8_t *key_buffer, size_t key_buffer_size,
871 psa_algorithm_t alg,
872 const uint8_t *hash, size_t hash_length,
873 const uint8_t *signature, size_t signature_length);
874
906
924
929
930#define PSA_CRYPTO_LOCAL_INPUT_INIT ((psa_crypto_local_input_t) { NULL, 0 })
931
944 psa_crypto_local_input_t *local_input);
945
953
959
960#define PSA_CRYPTO_LOCAL_OUTPUT_INIT ((psa_crypto_local_output_t) { NULL, NULL, 0 })
961
980 psa_crypto_local_output_t *local_output);
981
994
995#endif /* PSA_CRYPTO_CORE_H */
Platform Security Architecture cryptography module.
int int32_t
Definition acefiex.h:160
unsigned int uint32_t
Definition acefiex.h:163
unsigned char uint8_t
Definition acefiex.h:161
#define MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE
Definition crypto_extra.h:40
PSA external cryptoprocessor driver module.
uint16_t psa_key_type_t
Encoding of a key type.
Definition crypto_types.h:78
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
Definition crypto_types.h:134
int32_t psa_status_t
Function return status.
Definition crypto_types.h:59
uint64_t psa_key_slot_number_t
Definition crypto_se_driver.h:137
void * data
Definition kthread.h:30
Build-time PSA configuration info.
psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot)
psa_status_t psa_sign_hash_builtin(const psa_key_attributes_t *attributes, const uint8_t *key_buffer, size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, uint8_t *signature, size_t signature_size, size_t *signature_length)
int psa_can_do_cipher(psa_key_type_t key_type, psa_algorithm_t cipher_alg)
psa_status_t mbedtls_psa_sign_hash_start(mbedtls_psa_sign_hash_interruptible_operation_t *operation, const psa_key_attributes_t *attributes, const uint8_t *key_buffer, size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *hash, size_t hash_length)
Start signing a hash or short message with a private key, in an interruptible manner.
void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input)
psa_status_t mbedtls_psa_verify_hash_start(mbedtls_psa_verify_hash_interruptible_operation_t *operation, const psa_key_attributes_t *attributes, const uint8_t *key_buffer, size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, const uint8_t *signature, size_t signature_length)
Start reading and verifying a hash or short message, in an interruptible manner.
psa_status_t psa_key_agreement_raw_builtin(const psa_key_attributes_t *attributes, const uint8_t *key_buffer, size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *peer_key, size_t peer_key_length, uint8_t *shared_secret, size_t shared_secret_size, size_t *shared_secret_length)
void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops)
Set the maximum number of ops allowed to be executed by an interruptible function in a single call.
struct psa_crypto_local_input_s psa_crypto_local_input_t
psa_status_t psa_verify_hash_builtin(const psa_key_attributes_t *attributes, const uint8_t *key_buffer, size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, const uint8_t *signature, size_t signature_length)
Verify the signature a hash or short message using a public key.
psa_status_t mbedtls_psa_sign_hash_complete(mbedtls_psa_sign_hash_interruptible_operation_t *operation, uint8_t *signature, size_t signature_size, size_t *signature_length)
Continue and eventually complete the action of signing a hash or short message with a private key,...
int psa_custom_key_parameters_are_default(const psa_custom_key_parameters_t *custom, size_t custom_data_length)
psa_status_t mbedtls_psa_verify_hash_abort(mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Abort a verify signed hash operation.
psa_status_t psa_sign_message_builtin(const psa_key_attributes_t *attributes, const uint8_t *key_buffer, size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *input, size_t input_length, uint8_t *signature, size_t signature_size, size_t *signature_length)
psa_key_slot_state_t
Definition psa_crypto_core.h:39
@ PSA_SLOT_FILLING
Definition psa_crypto_core.h:41
@ PSA_SLOT_FULL
Definition psa_crypto_core.h:42
@ PSA_SLOT_EMPTY
Definition psa_crypto_core.h:40
@ PSA_SLOT_PENDING_DELETION
Definition psa_crypto_core.h:43
psa_status_t psa_verify_message_builtin(const psa_key_attributes_t *attributes, const uint8_t *key_buffer, size_t key_buffer_size, psa_algorithm_t alg, const uint8_t *input, size_t input_length, const uint8_t *signature, size_t signature_length)
uint32_t mbedtls_psa_interruptible_get_max_ops(void)
Get the maximum number of ops allowed to be executed by an interruptible function in a single call.
struct psa_crypto_local_output_s psa_crypto_local_output_t
psa_status_t mbedtls_psa_verify_hash_complete(mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Continue and eventually complete the action of signing a hash or short message with a private key,...
psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot)
psa_status_t mbedtls_to_psa_error(int ret)
psa_status_t psa_generate_key_internal(const psa_key_attributes_t *attributes, const psa_custom_key_parameters_t *custom, const uint8_t *custom_data, size_t custom_data_length, uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Generate a key.
uint32_t mbedtls_psa_verify_hash_get_num_ops(const mbedtls_psa_verify_hash_interruptible_operation_t *operation)
Get the number of ops that a hash verification operation has taken for the previous call....
uint32_t mbedtls_psa_sign_hash_get_num_ops(const mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Get the number of ops that a hash signing operation has taken for the previous call....
psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len, psa_crypto_local_output_t *local_output)
psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot, size_t buffer_length)
psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output)
psa_status_t psa_export_key_internal(const psa_key_attributes_t *attributes, const uint8_t *key_buffer, size_t key_buffer_size, uint8_t *data, size_t data_size, size_t *data_length)
psa_status_t mbedtls_psa_sign_hash_abort(mbedtls_psa_sign_hash_interruptible_operation_t *operation)
Abort a sign hash operation.
psa_status_t psa_import_key_into_slot(const psa_key_attributes_t *attributes, const uint8_t *data, size_t data_length, uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length, size_t *bits)
psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot, const uint8_t *data, size_t data_length)
psa_status_t psa_export_public_key_internal(const psa_key_attributes_t *attributes, const uint8_t *key_buffer, size_t key_buffer_size, uint8_t *data, size_t data_size, size_t *data_length)
psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len, psa_crypto_local_input_t *local_input)
psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type, size_t bits)
Validate the key bit size for unstructured keys.
#define free(p)
Definition quickjs.c:2291
Definition crypto_builtin_composites.h:108
Definition crypto_builtin_composites.h:142
Definition psa_crypto_core.h:925
size_t length
Definition psa_crypto_core.h:927
uint8_t * buffer
Definition psa_crypto_core.h:926
Definition psa_crypto_core.h:954
uint8_t * buffer
Definition psa_crypto_core.h:956
uint8_t * original
Definition psa_crypto_core.h:955
size_t length
Definition psa_crypto_core.h:957
Definition crypto_struct.h:226
Definition crypto_struct.h:295
Definition psa_crypto_core.h:145
size_t bytes
Definition psa_crypto_core.h:151
uint8_t * data
Definition psa_crypto_core.h:149
Definition psa_crypto_core.h:49
size_t registered_readers
Definition psa_crypto_core.h:139
union psa_key_slot_t::@900 var
struct psa_key_slot_t::@900::@902 occupied
struct psa_key_slot_t::key_data key
int32_t next_free_relative_to_next
Definition psa_crypto_core.h:108
psa_key_attributes_t attr
Definition psa_crypto_core.h:52
psa_key_slot_state_t state
Definition psa_crypto_core.h:73
Threading abstraction layer.
struct VirtioInputEvent * input
Definition virtiokbd.c:46