1a8e1175bSopenharmony_ci/*
2a8e1175bSopenharmony_ci *  Functions to delegate cryptographic operations to an available
3a8e1175bSopenharmony_ci *  and appropriate accelerator.
4a8e1175bSopenharmony_ci *  Warning: This file is now auto-generated.
5a8e1175bSopenharmony_ci */
6a8e1175bSopenharmony_ci/*  Copyright The Mbed TLS Contributors
7a8e1175bSopenharmony_ci *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
8a8e1175bSopenharmony_ci */
9a8e1175bSopenharmony_ci
10a8e1175bSopenharmony_ci
11a8e1175bSopenharmony_ci/* BEGIN-common headers */
12a8e1175bSopenharmony_ci#include "common.h"
13a8e1175bSopenharmony_ci#include "psa_crypto_aead.h"
14a8e1175bSopenharmony_ci#include "psa_crypto_cipher.h"
15a8e1175bSopenharmony_ci#include "psa_crypto_core.h"
16a8e1175bSopenharmony_ci#include "psa_crypto_driver_wrappers_no_static.h"
17a8e1175bSopenharmony_ci#include "psa_crypto_hash.h"
18a8e1175bSopenharmony_ci#include "psa_crypto_mac.h"
19a8e1175bSopenharmony_ci#include "psa_crypto_pake.h"
20a8e1175bSopenharmony_ci#include "psa_crypto_rsa.h"
21a8e1175bSopenharmony_ci
22a8e1175bSopenharmony_ci#include "mbedtls/platform.h"
23a8e1175bSopenharmony_ci/* END-common headers */
24a8e1175bSopenharmony_ci
25a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_C)
26a8e1175bSopenharmony_ci
27a8e1175bSopenharmony_ci/* BEGIN-driver headers */
28a8e1175bSopenharmony_ci/* Headers for mbedtls_test opaque driver */
29a8e1175bSopenharmony_ci#if defined(PSA_CRYPTO_DRIVER_TEST)
30a8e1175bSopenharmony_ci#include "test/drivers/test_driver.h"
31a8e1175bSopenharmony_ci
32a8e1175bSopenharmony_ci#endif
33a8e1175bSopenharmony_ci/* Headers for mbedtls_test transparent driver */
34a8e1175bSopenharmony_ci#if defined(PSA_CRYPTO_DRIVER_TEST)
35a8e1175bSopenharmony_ci#include "test/drivers/test_driver.h"
36a8e1175bSopenharmony_ci
37a8e1175bSopenharmony_ci#endif
38a8e1175bSopenharmony_ci/* Headers for p256 transparent driver */
39a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_P256M_DRIVER_ENABLED)
40a8e1175bSopenharmony_ci#include "../3rdparty/p256-m/p256-m_driver_entrypoints.h"
41a8e1175bSopenharmony_ci
42a8e1175bSopenharmony_ci#endif
43a8e1175bSopenharmony_ci
44a8e1175bSopenharmony_ci/* END-driver headers */
45a8e1175bSopenharmony_ci
46a8e1175bSopenharmony_ci/* Auto-generated values depending on which drivers are registered.
47a8e1175bSopenharmony_ci * ID 0 is reserved for unallocated operations.
48a8e1175bSopenharmony_ci * ID 1 is reserved for the Mbed TLS software driver. */
49a8e1175bSopenharmony_ci/* BEGIN-driver id definition */
50a8e1175bSopenharmony_ci#define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
51a8e1175bSopenharmony_ci#define MBEDTLS_TEST_OPAQUE_DRIVER_ID (2)
52a8e1175bSopenharmony_ci#define MBEDTLS_TEST_TRANSPARENT_DRIVER_ID (3)
53a8e1175bSopenharmony_ci#define P256_TRANSPARENT_DRIVER_ID (4)
54a8e1175bSopenharmony_ci
55a8e1175bSopenharmony_ci/* END-driver id */
56a8e1175bSopenharmony_ci
57a8e1175bSopenharmony_ci/* BEGIN-Common Macro definitions */
58a8e1175bSopenharmony_ci
59a8e1175bSopenharmony_ci/* END-Common Macro definitions */
60a8e1175bSopenharmony_ci
61a8e1175bSopenharmony_ci/* Support the 'old' SE interface when asked to */
62a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
63a8e1175bSopenharmony_ci/* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
64a8e1175bSopenharmony_ci * SE driver is present, to avoid unused argument errors at compile time. */
65a8e1175bSopenharmony_ci#ifndef PSA_CRYPTO_DRIVER_PRESENT
66a8e1175bSopenharmony_ci#define PSA_CRYPTO_DRIVER_PRESENT
67a8e1175bSopenharmony_ci#endif
68a8e1175bSopenharmony_ci#include "psa_crypto_se.h"
69a8e1175bSopenharmony_ci#endif
70a8e1175bSopenharmony_ci
71a8e1175bSopenharmony_ci/** Get the key buffer size required to store the key material of a key
72a8e1175bSopenharmony_ci *  associated with an opaque driver.
73a8e1175bSopenharmony_ci *
74a8e1175bSopenharmony_ci * \param[in] attributes  The key attributes.
75a8e1175bSopenharmony_ci * \param[out] key_buffer_size  Minimum buffer size to contain the key material
76a8e1175bSopenharmony_ci *
77a8e1175bSopenharmony_ci * \retval #PSA_SUCCESS
78a8e1175bSopenharmony_ci *         The minimum size for a buffer to contain the key material has been
79a8e1175bSopenharmony_ci *         returned successfully.
80a8e1175bSopenharmony_ci * \retval #PSA_ERROR_NOT_SUPPORTED
81a8e1175bSopenharmony_ci *         The type and/or the size in bits of the key or the combination of
82a8e1175bSopenharmony_ci *         the two is not supported.
83a8e1175bSopenharmony_ci * \retval #PSA_ERROR_INVALID_ARGUMENT
84a8e1175bSopenharmony_ci *         The key is declared with a lifetime not known to us.
85a8e1175bSopenharmony_ci */
86a8e1175bSopenharmony_cipsa_status_t psa_driver_wrapper_get_key_buffer_size(
87a8e1175bSopenharmony_ci    const psa_key_attributes_t *attributes,
88a8e1175bSopenharmony_ci    size_t *key_buffer_size )
89a8e1175bSopenharmony_ci{
90a8e1175bSopenharmony_ci    psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
91a8e1175bSopenharmony_ci    psa_key_type_t key_type = psa_get_key_type(attributes);
92a8e1175bSopenharmony_ci    size_t key_bits = psa_get_key_bits(attributes);
93a8e1175bSopenharmony_ci
94a8e1175bSopenharmony_ci    *key_buffer_size = 0;
95a8e1175bSopenharmony_ci    switch( location )
96a8e1175bSopenharmony_ci    {
97a8e1175bSopenharmony_ci#if defined(PSA_CRYPTO_DRIVER_TEST)
98a8e1175bSopenharmony_ci        case PSA_CRYPTO_TEST_DRIVER_LOCATION:
99a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
100a8e1175bSopenharmony_ci            /* Emulate property 'builtin_key_size' */
101a8e1175bSopenharmony_ci            if( psa_key_id_is_builtin(
102a8e1175bSopenharmony_ci                    MBEDTLS_SVC_KEY_ID_GET_KEY_ID(
103a8e1175bSopenharmony_ci                        psa_get_key_id( attributes ) ) ) )
104a8e1175bSopenharmony_ci            {
105a8e1175bSopenharmony_ci                *key_buffer_size = sizeof( psa_drv_slot_number_t );
106a8e1175bSopenharmony_ci                return( PSA_SUCCESS );
107a8e1175bSopenharmony_ci            }
108a8e1175bSopenharmony_ci#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
109a8e1175bSopenharmony_ci            *key_buffer_size = mbedtls_test_opaque_size_function( key_type,
110a8e1175bSopenharmony_ci                                                                  key_bits );
111a8e1175bSopenharmony_ci            return( ( *key_buffer_size != 0 ) ?
112a8e1175bSopenharmony_ci                    PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
113a8e1175bSopenharmony_ci#endif /* PSA_CRYPTO_DRIVER_TEST */
114a8e1175bSopenharmony_ci
115a8e1175bSopenharmony_ci        default:
116a8e1175bSopenharmony_ci            (void)key_type;
117a8e1175bSopenharmony_ci            (void)key_bits;
118a8e1175bSopenharmony_ci            return( PSA_ERROR_INVALID_ARGUMENT );
119a8e1175bSopenharmony_ci    }
120a8e1175bSopenharmony_ci}
121a8e1175bSopenharmony_ci
122a8e1175bSopenharmony_cipsa_status_t psa_driver_wrapper_export_public_key(
123a8e1175bSopenharmony_ci    const psa_key_attributes_t *attributes,
124a8e1175bSopenharmony_ci    const uint8_t *key_buffer, size_t key_buffer_size,
125a8e1175bSopenharmony_ci    uint8_t *data, size_t data_size, size_t *data_length )
126a8e1175bSopenharmony_ci
127a8e1175bSopenharmony_ci{
128a8e1175bSopenharmony_ci
129a8e1175bSopenharmony_ci    psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
130a8e1175bSopenharmony_ci    psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
131a8e1175bSopenharmony_ci                                      psa_get_key_lifetime( attributes ) );
132a8e1175bSopenharmony_ci
133a8e1175bSopenharmony_ci    /* Try dynamically-registered SE interface first */
134a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
135a8e1175bSopenharmony_ci    const psa_drv_se_t *drv;
136a8e1175bSopenharmony_ci    psa_drv_se_context_t *drv_context;
137a8e1175bSopenharmony_ci
138a8e1175bSopenharmony_ci    if( psa_get_se_driver( psa_get_key_lifetime(attributes), &drv, &drv_context ) )
139a8e1175bSopenharmony_ci    {
140a8e1175bSopenharmony_ci        if( ( drv->key_management == NULL ) ||
141a8e1175bSopenharmony_ci            ( drv->key_management->p_export_public == NULL ) )
142a8e1175bSopenharmony_ci        {
143a8e1175bSopenharmony_ci            return( PSA_ERROR_NOT_SUPPORTED );
144a8e1175bSopenharmony_ci        }
145a8e1175bSopenharmony_ci
146a8e1175bSopenharmony_ci        return( drv->key_management->p_export_public(
147a8e1175bSopenharmony_ci                    drv_context,
148a8e1175bSopenharmony_ci                    *( (psa_key_slot_number_t *)key_buffer ),
149a8e1175bSopenharmony_ci                    data, data_size, data_length ) );
150a8e1175bSopenharmony_ci    }
151a8e1175bSopenharmony_ci#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
152a8e1175bSopenharmony_ci
153a8e1175bSopenharmony_ci    switch( location )
154a8e1175bSopenharmony_ci    {
155a8e1175bSopenharmony_ci        case PSA_KEY_LOCATION_LOCAL_STORAGE:
156a8e1175bSopenharmony_ci            /* Key is stored in the slot in export representation, so
157a8e1175bSopenharmony_ci             * cycle through all known transparent accelerators */
158a8e1175bSopenharmony_ci#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
159a8e1175bSopenharmony_ci
160a8e1175bSopenharmony_ci#if (defined(PSA_CRYPTO_DRIVER_TEST) )
161a8e1175bSopenharmony_ci            status = mbedtls_test_transparent_export_public_key
162a8e1175bSopenharmony_ci                (attributes,
163a8e1175bSopenharmony_ci                                key_buffer,
164a8e1175bSopenharmony_ci                                key_buffer_size,
165a8e1175bSopenharmony_ci                                data,
166a8e1175bSopenharmony_ci                                data_size,
167a8e1175bSopenharmony_ci                                data_length
168a8e1175bSopenharmony_ci            );
169a8e1175bSopenharmony_ci
170a8e1175bSopenharmony_ci            if( status != PSA_ERROR_NOT_SUPPORTED )
171a8e1175bSopenharmony_ci                return( status );
172a8e1175bSopenharmony_ci#endif
173a8e1175bSopenharmony_ci
174a8e1175bSopenharmony_ci#if (defined(MBEDTLS_PSA_P256M_DRIVER_ENABLED) )
175a8e1175bSopenharmony_ci            status = p256_transparent_export_public_key
176a8e1175bSopenharmony_ci                (attributes,
177a8e1175bSopenharmony_ci                                key_buffer,
178a8e1175bSopenharmony_ci                                key_buffer_size,
179a8e1175bSopenharmony_ci                                data,
180a8e1175bSopenharmony_ci                                data_size,
181a8e1175bSopenharmony_ci                                data_length
182a8e1175bSopenharmony_ci            );
183a8e1175bSopenharmony_ci
184a8e1175bSopenharmony_ci            if( status != PSA_ERROR_NOT_SUPPORTED )
185a8e1175bSopenharmony_ci                return( status );
186a8e1175bSopenharmony_ci#endif
187a8e1175bSopenharmony_ci
188a8e1175bSopenharmony_ci
189a8e1175bSopenharmony_ci#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
190a8e1175bSopenharmony_ci            /* Fell through, meaning no accelerator supports this operation */
191a8e1175bSopenharmony_ci            return( psa_export_public_key_internal( attributes,
192a8e1175bSopenharmony_ci                                                    key_buffer,
193a8e1175bSopenharmony_ci                                                    key_buffer_size,
194a8e1175bSopenharmony_ci                                                    data,
195a8e1175bSopenharmony_ci                                                    data_size,
196a8e1175bSopenharmony_ci                                                    data_length ) );
197a8e1175bSopenharmony_ci
198a8e1175bSopenharmony_ci        /* Add cases for opaque driver here */
199a8e1175bSopenharmony_ci#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
200a8e1175bSopenharmony_ci
201a8e1175bSopenharmony_ci#if (defined(PSA_CRYPTO_DRIVER_TEST) )
202a8e1175bSopenharmony_ci        case 0x7fffff:
203a8e1175bSopenharmony_ci            return( mbedtls_test_opaque_export_public_key
204a8e1175bSopenharmony_ci            (attributes,
205a8e1175bSopenharmony_ci                            key_buffer,
206a8e1175bSopenharmony_ci                            key_buffer_size,
207a8e1175bSopenharmony_ci                            data,
208a8e1175bSopenharmony_ci                            data_size,
209a8e1175bSopenharmony_ci                            data_length
210a8e1175bSopenharmony_ci        ));
211a8e1175bSopenharmony_ci#endif
212a8e1175bSopenharmony_ci
213a8e1175bSopenharmony_ci
214a8e1175bSopenharmony_ci#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
215a8e1175bSopenharmony_ci        default:
216a8e1175bSopenharmony_ci            /* Key is declared with a lifetime not known to us */
217a8e1175bSopenharmony_ci            return( status );
218a8e1175bSopenharmony_ci    }
219a8e1175bSopenharmony_ci
220a8e1175bSopenharmony_ci}
221a8e1175bSopenharmony_ci
222a8e1175bSopenharmony_cipsa_status_t psa_driver_wrapper_get_builtin_key(
223a8e1175bSopenharmony_ci    psa_drv_slot_number_t slot_number,
224a8e1175bSopenharmony_ci    psa_key_attributes_t *attributes,
225a8e1175bSopenharmony_ci    uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
226a8e1175bSopenharmony_ci{
227a8e1175bSopenharmony_ci
228a8e1175bSopenharmony_ci    psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
229a8e1175bSopenharmony_ci    switch( location )
230a8e1175bSopenharmony_ci    {
231a8e1175bSopenharmony_ci#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
232a8e1175bSopenharmony_ci
233a8e1175bSopenharmony_ci#if (defined(PSA_CRYPTO_DRIVER_TEST) )
234a8e1175bSopenharmony_ci        case 0x7fffff:
235a8e1175bSopenharmony_ci            return( mbedtls_test_opaque_get_builtin_key
236a8e1175bSopenharmony_ci            (slot_number,
237a8e1175bSopenharmony_ci                            attributes,
238a8e1175bSopenharmony_ci                            key_buffer,
239a8e1175bSopenharmony_ci                            key_buffer_size,
240a8e1175bSopenharmony_ci                            key_buffer_length
241a8e1175bSopenharmony_ci        ));
242a8e1175bSopenharmony_ci#endif
243a8e1175bSopenharmony_ci
244a8e1175bSopenharmony_ci
245a8e1175bSopenharmony_ci#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
246a8e1175bSopenharmony_ci        default:
247a8e1175bSopenharmony_ci            (void) slot_number;
248a8e1175bSopenharmony_ci            (void) key_buffer;
249a8e1175bSopenharmony_ci            (void) key_buffer_size;
250a8e1175bSopenharmony_ci            (void) key_buffer_length;
251a8e1175bSopenharmony_ci            return( PSA_ERROR_DOES_NOT_EXIST );
252a8e1175bSopenharmony_ci    }
253a8e1175bSopenharmony_ci
254a8e1175bSopenharmony_ci}
255a8e1175bSopenharmony_ci
256a8e1175bSopenharmony_ci#endif /* MBEDTLS_PSA_CRYPTO_C */
257