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