1 /*
2 * Test driver for generating and verifying keys.
3 */
4 /* Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
7
8 #ifndef PSA_CRYPTO_TEST_DRIVERS_KEY_MANAGEMENT_H
9 #define PSA_CRYPTO_TEST_DRIVERS_KEY_MANAGEMENT_H
10
11 #include "mbedtls/build_info.h"
12
13 #if defined(PSA_CRYPTO_DRIVER_TEST)
14 #include <psa/crypto_driver_common.h>
15
16 #define PSA_CRYPTO_TEST_DRIVER_BUILTIN_AES_KEY_SLOT 0
17 #define PSA_CRYPTO_TEST_DRIVER_BUILTIN_ECDSA_KEY_SLOT 1
18
19 typedef struct {
20 /* If non-null, on success, copy this to the output. */
21 void *forced_output;
22 size_t forced_output_length;
23 /* If not PSA_SUCCESS, return this error code instead of processing the
24 * function call. */
25 psa_status_t forced_status;
26 /* Count the amount of times one of the key management driver functions
27 * is called. */
28 unsigned long hits;
29 /* Subset of hits which only counts key operations with EC key */
30 unsigned long hits_export_public_key;
31 /* Location of the last key management driver called to import a key. */
32 psa_key_location_t location;
33 } mbedtls_test_driver_key_management_hooks_t;
34
35 /* The location is initialized to the invalid value 0x800000. Invalid in the
36 * sense that no PSA specification will assign a meaning to this location
37 * (stated first in version 1.0.1 of the specification) and that it is not
38 * used as a location of an opaque test drivers. */
39 #define MBEDTLS_TEST_DRIVER_KEY_MANAGEMENT_INIT { NULL, 0, PSA_SUCCESS, 0, 0, 0x800000 }
40 static inline mbedtls_test_driver_key_management_hooks_t
mbedtls_test_driver_key_management_hooks_init(void)41 mbedtls_test_driver_key_management_hooks_init(void)
42 {
43 const mbedtls_test_driver_key_management_hooks_t
44 v = MBEDTLS_TEST_DRIVER_KEY_MANAGEMENT_INIT;
45 return v;
46 }
47
48 /*
49 * In order to convert the plain text keys to Opaque, the size of the key is
50 * padded up by PSA_CRYPTO_TEST_DRIVER_OPAQUE_PAD_PREFIX_SIZE in addition to
51 * xor mangling the key. The pad prefix needs to be accounted for while
52 * sizing for the key.
53 */
54 #define PSA_CRYPTO_TEST_DRIVER_OPAQUE_PAD_PREFIX 0xBEEFED00U
55 #define PSA_CRYPTO_TEST_DRIVER_OPAQUE_PAD_PREFIX_SIZE sizeof( \
56 PSA_CRYPTO_TEST_DRIVER_OPAQUE_PAD_PREFIX)
57
58 size_t mbedtls_test_opaque_size_function(
59 const psa_key_type_t key_type,
60 const size_t key_bits);
61
62 extern mbedtls_test_driver_key_management_hooks_t
63 mbedtls_test_driver_key_management_hooks;
64
65 psa_status_t mbedtls_test_transparent_init(void);
66 void mbedtls_test_transparent_free(void);
67 psa_status_t mbedtls_test_opaque_init(void);
68 void mbedtls_test_opaque_free(void);
69
70 psa_status_t mbedtls_test_opaque_unwrap_key(
71 const uint8_t *wrapped_key, size_t wrapped_key_length, uint8_t *key_buffer,
72 size_t key_buffer_size, size_t *key_buffer_length);
73
74 psa_status_t mbedtls_test_transparent_generate_key(
75 const psa_key_attributes_t *attributes,
76 uint8_t *key, size_t key_size, size_t *key_length);
77
78 psa_status_t mbedtls_test_opaque_generate_key(
79 const psa_key_attributes_t *attributes,
80 uint8_t *key, size_t key_size, size_t *key_length);
81
82 psa_status_t mbedtls_test_opaque_export_key(
83 const psa_key_attributes_t *attributes,
84 const uint8_t *key, size_t key_length,
85 uint8_t *data, size_t data_size, size_t *data_length);
86
87 psa_status_t mbedtls_test_transparent_export_public_key(
88 const psa_key_attributes_t *attributes,
89 const uint8_t *key, size_t key_length,
90 uint8_t *data, size_t data_size, size_t *data_length);
91
92 psa_status_t mbedtls_test_opaque_export_public_key(
93 const psa_key_attributes_t *attributes,
94 const uint8_t *key, size_t key_length,
95 uint8_t *data, size_t data_size, size_t *data_length);
96
97 psa_status_t mbedtls_test_transparent_import_key(
98 const psa_key_attributes_t *attributes,
99 const uint8_t *data,
100 size_t data_length,
101 uint8_t *key_buffer,
102 size_t key_buffer_size,
103 size_t *key_buffer_length,
104 size_t *bits);
105
106 psa_status_t mbedtls_test_opaque_import_key(
107 const psa_key_attributes_t *attributes,
108 const uint8_t *data,
109 size_t data_length,
110 uint8_t *key_buffer,
111 size_t key_buffer_size,
112 size_t *key_buffer_length,
113 size_t *bits);
114
115 psa_status_t mbedtls_test_opaque_get_builtin_key(
116 psa_drv_slot_number_t slot_number,
117 psa_key_attributes_t *attributes,
118 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length);
119
120 psa_status_t mbedtls_test_opaque_copy_key(
121 psa_key_attributes_t *attributes,
122 const uint8_t *source_key,
123 size_t source_key_length,
124 uint8_t *target_key_buffer,
125 size_t target_key_buffer_size,
126 size_t *target_key_buffer_length);
127
128 #endif /* PSA_CRYPTO_DRIVER_TEST */
129 #endif /* PSA_CRYPTO_TEST_DRIVERS_KEY_MANAGEMENT_H */
130