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