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