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     /* Location of the last key management driver called to import a key. */
30     psa_key_location_t location;
31 } mbedtls_test_driver_key_management_hooks_t;
32 
33 /* The location is initialized to the invalid value 0x800000. Invalid in the
34  * sense that no PSA specification will assign a meaning to this location
35  * (stated first in version 1.0.1 of the specification) and that it is not
36  * used as a location of an opaque test drivers. */
37 #define MBEDTLS_TEST_DRIVER_KEY_MANAGEMENT_INIT { NULL, 0, PSA_SUCCESS, 0, 0x800000 }
38 static inline mbedtls_test_driver_key_management_hooks_t
mbedtls_test_driver_key_management_hooks_init(void)39 mbedtls_test_driver_key_management_hooks_init(void)
40 {
41     const mbedtls_test_driver_key_management_hooks_t
42         v = MBEDTLS_TEST_DRIVER_KEY_MANAGEMENT_INIT;
43     return v;
44 }
45 
46 /*
47  * In order to convert the plain text keys to Opaque, the size of the key is
48  * padded up by PSA_CRYPTO_TEST_DRIVER_OPAQUE_PAD_PREFIX_SIZE in addition to
49  * xor mangling the key. The pad prefix needs to be accounted for while
50  * sizing for the key.
51  */
52 #define PSA_CRYPTO_TEST_DRIVER_OPAQUE_PAD_PREFIX           0xBEEFED00U
53 #define PSA_CRYPTO_TEST_DRIVER_OPAQUE_PAD_PREFIX_SIZE      sizeof( \
54         PSA_CRYPTO_TEST_DRIVER_OPAQUE_PAD_PREFIX)
55 
56 size_t mbedtls_test_opaque_size_function(
57     const psa_key_type_t key_type,
58     const size_t key_bits);
59 
60 extern mbedtls_test_driver_key_management_hooks_t
61     mbedtls_test_driver_key_management_hooks;
62 
63 psa_status_t mbedtls_test_transparent_init(void);
64 void mbedtls_test_transparent_free(void);
65 psa_status_t mbedtls_test_opaque_init(void);
66 void mbedtls_test_opaque_free(void);
67 
68 psa_status_t mbedtls_test_transparent_generate_key(
69     const psa_key_attributes_t *attributes,
70     uint8_t *key, size_t key_size, size_t *key_length);
71 
72 psa_status_t mbedtls_test_opaque_generate_key(
73     const psa_key_attributes_t *attributes,
74     uint8_t *key, size_t key_size, size_t *key_length);
75 
76 psa_status_t mbedtls_test_opaque_export_key(
77     const psa_key_attributes_t *attributes,
78     const uint8_t *key, size_t key_length,
79     uint8_t *data, size_t data_size, size_t *data_length);
80 
81 psa_status_t mbedtls_test_transparent_export_public_key(
82     const psa_key_attributes_t *attributes,
83     const uint8_t *key, size_t key_length,
84     uint8_t *data, size_t data_size, size_t *data_length);
85 
86 psa_status_t mbedtls_test_opaque_export_public_key(
87     const psa_key_attributes_t *attributes,
88     const uint8_t *key, size_t key_length,
89     uint8_t *data, size_t data_size, size_t *data_length);
90 
91 psa_status_t mbedtls_test_transparent_import_key(
92     const psa_key_attributes_t *attributes,
93     const uint8_t *data,
94     size_t data_length,
95     uint8_t *key_buffer,
96     size_t key_buffer_size,
97     size_t *key_buffer_length,
98     size_t *bits);
99 
100 psa_status_t mbedtls_test_opaque_import_key(
101     const psa_key_attributes_t *attributes,
102     const uint8_t *data,
103     size_t data_length,
104     uint8_t *key_buffer,
105     size_t key_buffer_size,
106     size_t *key_buffer_length,
107     size_t *bits);
108 
109 psa_status_t mbedtls_test_opaque_get_builtin_key(
110     psa_drv_slot_number_t slot_number,
111     psa_key_attributes_t *attributes,
112     uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length);
113 
114 psa_status_t mbedtls_test_opaque_copy_key(
115     psa_key_attributes_t *attributes,
116     const uint8_t *source_key,
117     size_t source_key_length,
118     uint8_t *target_key_buffer,
119     size_t target_key_buffer_size,
120     size_t *target_key_buffer_length);
121 
122 #endif /* PSA_CRYPTO_DRIVER_TEST */
123 #endif /* PSA_CRYPTO_TEST_DRIVERS_KEY_MANAGEMENT_H */
124