blob: 28087de073c61189af005a2ed4dab1fc81dd48d0 [file] [log] [blame]
Steven Cooremancd84cb42020-07-16 20:28:36 +02001/*
2 * Functions to delegate cryptographic operations to an available
3 * and appropriate accelerator.
Steven Cooreman56250fd2020-09-04 13:07:15 +02004 * Warning: This file will be auto-generated in the future.
Steven Cooremancd84cb42020-07-16 20:28:36 +02005 */
Steven Cooreman2c7b2f82020-09-02 13:43:46 +02006/* Copyright The Mbed TLS Contributors
Steven Cooremancd84cb42020-07-16 20:28:36 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Steven Cooremancd84cb42020-07-16 20:28:36 +020020 */
21
Ronald Cronde822812021-03-17 16:08:20 +010022#include "psa_crypto_aead.h"
Ronald Cron0b805592020-12-14 18:08:20 +010023#include "psa_crypto_cipher.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020024#include "psa_crypto_core.h"
25#include "psa_crypto_driver_wrappers.h"
Steven Cooreman830aff22021-03-09 09:50:44 +010026#include "psa_crypto_hash.h"
27
Steven Cooreman2a1664c2020-07-20 15:33:08 +020028#include "mbedtls/platform.h"
29
30#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
Steven Cooremancd84cb42020-07-16 20:28:36 +020031
32/* Include test driver definition when running tests */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020033#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman56250fd2020-09-04 13:07:15 +020034#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020035#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020036#endif
37#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020038#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020039#endif
Steven Cooreman0d7c64d2020-09-07 16:17:55 +020040#include "test/drivers/test_driver.h"
Steven Cooremanf1720ea2020-07-24 18:41:58 +020041#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +020042
Steven Cooreman56250fd2020-09-04 13:07:15 +020043/* Repeat above block for each JSON-declared driver during autogeneration */
Steven Cooremanaa87fd02021-03-15 18:54:03 +010044#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Steven Cooreman56250fd2020-09-04 13:07:15 +020045
Steven Cooreman1e582352021-02-18 17:24:37 +010046/* Auto-generated values depending on which drivers are registered.
47 * ID 0 is reserved for unallocated operations.
48 * ID 1 is reserved for the Mbed TLS software driver. */
Steven Cooreman830aff22021-03-09 09:50:44 +010049#define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
50
Steven Cooreman37941cb2020-07-28 18:49:51 +020051#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman1e582352021-02-18 17:24:37 +010052#define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2)
53#define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3)
Steven Cooreman37941cb2020-07-28 18:49:51 +020054#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +020055
56/* Support the 'old' SE interface when asked to */
Steven Cooreman7a250572020-07-17 16:43:05 +020057#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman56250fd2020-09-04 13:07:15 +020058/* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
59 * SE driver is present, to avoid unused argument errors at compile time. */
60#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020061#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020062#endif
Steven Cooreman7a250572020-07-17 16:43:05 +020063#include "psa_crypto_se.h"
64#endif
65
Steven Cooremancd84cb42020-07-16 20:28:36 +020066/* Start delegation functions */
Ronald Cron9f17aa42020-12-08 17:07:25 +010067psa_status_t psa_driver_wrapper_sign_hash(
68 const psa_key_attributes_t *attributes,
69 const uint8_t *key_buffer, size_t key_buffer_size,
70 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
71 uint8_t *signature, size_t signature_size, size_t *signature_length )
Steven Cooremancd84cb42020-07-16 20:28:36 +020072{
Steven Cooreman7a250572020-07-17 16:43:05 +020073 /* Try dynamically-registered SE interface first */
74#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
75 const psa_drv_se_t *drv;
76 psa_drv_se_context_t *drv_context;
77
Ronald Cron9f17aa42020-12-08 17:07:25 +010078 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman7a250572020-07-17 16:43:05 +020079 {
80 if( drv->asymmetric == NULL ||
81 drv->asymmetric->p_sign == NULL )
82 {
83 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +020084 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman7a250572020-07-17 16:43:05 +020085 }
Ronald Cron9f17aa42020-12-08 17:07:25 +010086 return( drv->asymmetric->p_sign(
87 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
88 alg, hash, hash_length,
89 signature, signature_size, signature_length ) );
Steven Cooreman7a250572020-07-17 16:43:05 +020090 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +020091#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman7a250572020-07-17 16:43:05 +020092
Ronald Cronfce9df22020-12-08 18:06:03 +010093 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +010094 psa_key_location_t location =
95 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooremancd84cb42020-07-16 20:28:36 +020096
97 switch( location )
98 {
99 case PSA_KEY_LOCATION_LOCAL_STORAGE:
100 /* Key is stored in the slot in export representation, so
101 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100102#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200103#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron9f17aa42020-12-08 17:07:25 +0100104 status = test_transparent_signature_sign_hash( attributes,
105 key_buffer,
106 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200107 alg,
108 hash,
109 hash_length,
110 signature,
111 signature_size,
112 signature_length );
113 /* Declared with fallback == true */
114 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200115 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200116#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100117#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200118 /* Fell through, meaning no accelerator supports this operation */
Ronald Cronfce9df22020-12-08 18:06:03 +0100119 return( psa_sign_hash_internal( attributes,
120 key_buffer,
121 key_buffer_size,
122 alg,
123 hash,
124 hash_length,
125 signature,
126 signature_size,
127 signature_length ) );
128
Steven Cooremancd84cb42020-07-16 20:28:36 +0200129 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100130#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200131#if defined(PSA_CRYPTO_DRIVER_TEST)
132 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron9f17aa42020-12-08 17:07:25 +0100133 return( test_opaque_signature_sign_hash( attributes,
134 key_buffer,
135 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200136 alg,
137 hash,
138 hash_length,
139 signature,
140 signature_size,
141 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200142#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100143#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200144 default:
145 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100146 (void)status;
147 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200148 }
Steven Cooremancd84cb42020-07-16 20:28:36 +0200149}
150
Ronald Cron9f17aa42020-12-08 17:07:25 +0100151psa_status_t psa_driver_wrapper_verify_hash(
152 const psa_key_attributes_t *attributes,
153 const uint8_t *key_buffer, size_t key_buffer_size,
154 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
155 const uint8_t *signature, size_t signature_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200156{
Steven Cooreman55ae2172020-07-17 19:46:15 +0200157 /* Try dynamically-registered SE interface first */
158#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
159 const psa_drv_se_t *drv;
160 psa_drv_se_context_t *drv_context;
161
Ronald Cron9f17aa42020-12-08 17:07:25 +0100162 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200163 {
164 if( drv->asymmetric == NULL ||
165 drv->asymmetric->p_verify == NULL )
166 {
167 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200168 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200169 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100170 return( drv->asymmetric->p_verify(
171 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
172 alg, hash, hash_length,
173 signature, signature_length ) );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200174 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200175#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200176
Ronald Cronfce9df22020-12-08 18:06:03 +0100177 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100178 psa_key_location_t location =
179 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200180
181 switch( location )
182 {
183 case PSA_KEY_LOCATION_LOCAL_STORAGE:
184 /* Key is stored in the slot in export representation, so
185 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100186#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200187#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron9f17aa42020-12-08 17:07:25 +0100188 status = test_transparent_signature_verify_hash( attributes,
189 key_buffer,
190 key_buffer_size,
Steven Cooreman55ae2172020-07-17 19:46:15 +0200191 alg,
192 hash,
193 hash_length,
194 signature,
195 signature_length );
196 /* Declared with fallback == true */
197 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200198 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200199#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100200#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
201
202 return( psa_verify_hash_internal( attributes,
203 key_buffer,
204 key_buffer_size,
205 alg,
206 hash,
207 hash_length,
208 signature,
209 signature_length ) );
210
Steven Cooreman55ae2172020-07-17 19:46:15 +0200211 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100212#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200213#if defined(PSA_CRYPTO_DRIVER_TEST)
214 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron9f17aa42020-12-08 17:07:25 +0100215 return( test_opaque_signature_verify_hash( attributes,
216 key_buffer,
217 key_buffer_size,
Steven Cooreman55ae2172020-07-17 19:46:15 +0200218 alg,
219 hash,
220 hash_length,
221 signature,
222 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200223#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100224#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200225 default:
226 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100227 (void)status;
228 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200229 }
Steven Cooreman55ae2172020-07-17 19:46:15 +0200230}
231
Ronald Cron31216282020-12-05 18:47:56 +0100232/** Get the key buffer size for the key material of a generated key in the
233 * case of an opaque driver without storage.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200234 *
Ronald Cron31216282020-12-05 18:47:56 +0100235 * \param[in] attributes The key attributes.
236 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200237 *
238 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100239 * The minimum size for a buffer to contain the key material has been
240 * returned successfully.
241 * \retval #PSA_ERROR_INVALID_ARGUMENT
242 * The size in bits of the key is not valid.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200243 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100244 * The type and/or the size in bits of the key or the combination of
245 * the two is not supported.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200246 */
Ronald Cron9df74be2020-12-05 19:15:23 +0100247psa_status_t psa_driver_wrapper_get_key_buffer_size(
Ronald Cron31216282020-12-05 18:47:56 +0100248 const psa_key_attributes_t *attributes,
249 size_t *key_buffer_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200250{
John Durkop2c618352020-09-22 06:54:01 -0700251 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
252 psa_key_type_t key_type = attributes->core.type;
253 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200254
Ronald Cron31216282020-12-05 18:47:56 +0100255 *key_buffer_size = 0;
John Durkop2c618352020-09-22 06:54:01 -0700256 switch( location )
257 {
John Durkop2c618352020-09-22 06:54:01 -0700258#if defined(PSA_CRYPTO_DRIVER_TEST)
259 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100260#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
261 /* Emulate property 'builtin_key_size' */
262 if( psa_key_id_is_builtin(
263 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(
264 psa_get_key_id( attributes ) ) ) )
265 {
266 *key_buffer_size = sizeof(psa_drv_slot_number_t);
267 return( PSA_SUCCESS );
268 }
269#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
John Durkopac93e3b2020-10-16 06:48:55 -0700270#ifdef TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION
Ronald Cron31216282020-12-05 18:47:56 +0100271 *key_buffer_size = test_size_function( key_type, key_bits );
John Durkop2c618352020-09-22 06:54:01 -0700272 return( PSA_SUCCESS );
273#else /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
274 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) )
275 {
Ronald Cron31216282020-12-05 18:47:56 +0100276 int public_key_overhead =
277 ( ( TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 ) ?
278 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) : 0 );
279 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop135ce692020-10-19 07:12:28 -0700280 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE
281 + public_key_overhead;
282 }
Ronald Cron31216282020-12-05 18:47:56 +0100283 else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( key_type ) )
John Durkop135ce692020-10-19 07:12:28 -0700284 {
Ronald Cron31216282020-12-05 18:47:56 +0100285 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700286 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE;
287 }
John Durkop135ce692020-10-19 07:12:28 -0700288 else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) &&
Ronald Cron31216282020-12-05 18:47:56 +0100289 !PSA_KEY_TYPE_IS_PUBLIC_KEY ( key_type ) )
John Durkop2c618352020-09-22 06:54:01 -0700290 {
Ronald Cron31216282020-12-05 18:47:56 +0100291 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700292 + TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR
293 * ( ( key_bits + 7 ) / 8 );
294 }
295 else
296 {
297 return( PSA_ERROR_NOT_SUPPORTED );
298 }
299 return( PSA_SUCCESS );
300#endif /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
301#endif /* PSA_CRYPTO_DRIVER_TEST */
302
303 default:
Ronald Cron9df74be2020-12-05 19:15:23 +0100304 (void)key_type;
305 (void)key_bits;
Steven Cooreman56250fd2020-09-04 13:07:15 +0200306 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200307 }
308}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200309
Ronald Cron977c2472020-10-13 08:32:21 +0200310psa_status_t psa_driver_wrapper_generate_key(
311 const psa_key_attributes_t *attributes,
312 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200313{
Ronald Cron977c2472020-10-13 08:32:21 +0200314 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
315 psa_key_location_t location =
316 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
317
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200318 /* Try dynamically-registered SE interface first */
319#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
320 const psa_drv_se_t *drv;
321 psa_drv_se_context_t *drv_context;
322
Ronald Cron977c2472020-10-13 08:32:21 +0200323 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200324 {
325 size_t pubkey_length = 0; /* We don't support this feature yet */
326 if( drv->key_management == NULL ||
327 drv->key_management->p_generate == NULL )
328 {
329 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200330 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200331 }
332 return( drv->key_management->p_generate(
Ronald Cron977c2472020-10-13 08:32:21 +0200333 drv_context,
334 *( (psa_key_slot_number_t *)key_buffer ),
Ronald Cronea0f8a62020-11-25 17:52:23 +0100335 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200336 }
337#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
338
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200339 switch( location )
340 {
341 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Ronald Cron977c2472020-10-13 08:32:21 +0200342#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200343 /* Transparent drivers are limited to generating asymmetric keys */
Ronald Cron977c2472020-10-13 08:32:21 +0200344 if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200345 {
Ronald Cron977c2472020-10-13 08:32:21 +0200346 /* Cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200347#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron977c2472020-10-13 08:32:21 +0200348 status = test_transparent_generate_key(
349 attributes, key_buffer, key_buffer_size,
350 key_buffer_length );
351 /* Declared with fallback == true */
352 if( status != PSA_ERROR_NOT_SUPPORTED )
353 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200354#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200355 }
356#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
357
358 /* Software fallback */
359 status = psa_generate_key_internal(
360 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200361 break;
Ronald Cron977c2472020-10-13 08:32:21 +0200362
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200363 /* Add cases for opaque driver here */
Ronald Cron977c2472020-10-13 08:32:21 +0200364#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200365#if defined(PSA_CRYPTO_DRIVER_TEST)
366 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron977c2472020-10-13 08:32:21 +0200367 status = test_opaque_generate_key(
368 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200369 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200370#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200371#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
372
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200373 default:
374 /* Key is declared with a lifetime not known to us */
375 status = PSA_ERROR_INVALID_ARGUMENT;
376 break;
377 }
378
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200379 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200380}
381
Ronald Cron83282872020-11-22 14:02:39 +0100382psa_status_t psa_driver_wrapper_import_key(
383 const psa_key_attributes_t *attributes,
384 const uint8_t *data,
385 size_t data_length,
386 uint8_t *key_buffer,
387 size_t key_buffer_size,
388 size_t *key_buffer_length,
389 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200390{
Steven Cooreman04524762020-10-13 17:43:44 +0200391 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100392 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
393 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200394
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100395 /* Try dynamically-registered SE interface first */
396#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
397 const psa_drv_se_t *drv;
398 psa_drv_se_context_t *drv_context;
399
400 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
401 {
402 if( drv->key_management == NULL ||
403 drv->key_management->p_import == NULL )
404 return( PSA_ERROR_NOT_SUPPORTED );
405
406 /* The driver should set the number of key bits, however in
407 * case it doesn't, we initialize bits to an invalid value. */
408 *bits = PSA_MAX_KEY_BITS + 1;
409 status = drv->key_management->p_import(
410 drv_context,
411 *( (psa_key_slot_number_t *)key_buffer ),
412 attributes, data, data_length, bits );
413
414 if( status != PSA_SUCCESS )
415 return( status );
416
417 if( (*bits) > PSA_MAX_KEY_BITS )
418 return( PSA_ERROR_NOT_SUPPORTED );
419
420 return( PSA_SUCCESS );
421 }
422#endif /* PSA_CRYPTO_SE_C */
423
Ronald Cronbf33c932020-11-28 18:06:53 +0100424 switch( location )
425 {
426 case PSA_KEY_LOCATION_LOCAL_STORAGE:
427 /* Key is stored in the slot in export representation, so
428 * cycle through all known transparent accelerators */
429#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
430#if defined(PSA_CRYPTO_DRIVER_TEST)
431 status = test_transparent_import_key( attributes,
432 data, data_length,
433 key_buffer, key_buffer_size,
434 key_buffer_length, bits );
435 /* Declared with fallback == true */
436 if( status != PSA_ERROR_NOT_SUPPORTED )
437 return( status );
438#endif /* PSA_CRYPTO_DRIVER_TEST */
439#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
440 /* Fell through, meaning no accelerator supports this operation */
441 return( psa_import_key_into_slot( attributes,
442 data, data_length,
443 key_buffer, key_buffer_size,
444 key_buffer_length, bits ) );
445
446 default:
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100447 /* Importing a key with external storage in not yet supported.
448 * Return in error indicating that the lifetime is not valid. */
Ronald Cronbf33c932020-11-28 18:06:53 +0100449 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100450 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100451 }
452
Steven Cooreman04524762020-10-13 17:43:44 +0200453}
454
Ronald Cron67227982020-11-26 15:16:05 +0100455psa_status_t psa_driver_wrapper_export_key(
456 const psa_key_attributes_t *attributes,
457 const uint8_t *key_buffer, size_t key_buffer_size,
458 uint8_t *data, size_t data_size, size_t *data_length )
459
460{
461 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
462 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
463 psa_get_key_lifetime( attributes ) );
464
Ronald Cron152e3f82020-11-26 16:06:41 +0100465 /* Try dynamically-registered SE interface first */
466#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
467 const psa_drv_se_t *drv;
468 psa_drv_se_context_t *drv_context;
469
470 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
471 {
472 if( ( drv->key_management == NULL ) ||
473 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100474 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100475 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100476 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100477
478 return( drv->key_management->p_export(
479 drv_context,
480 *( (psa_key_slot_number_t *)key_buffer ),
481 data, data_size, data_length ) );
482 }
483#endif /* PSA_CRYPTO_SE_C */
484
Ronald Cron67227982020-11-26 15:16:05 +0100485 switch( location )
486 {
487 case PSA_KEY_LOCATION_LOCAL_STORAGE:
488 return( psa_export_key_internal( attributes,
489 key_buffer,
490 key_buffer_size,
491 data,
492 data_size,
493 data_length ) );
494
495 /* Add cases for opaque driver here */
496#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
497#if defined(PSA_CRYPTO_DRIVER_TEST)
498 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
499 return( test_opaque_export_key( attributes,
500 key_buffer,
501 key_buffer_size,
502 data,
503 data_size,
504 data_length ) );
505#endif /* PSA_CRYPTO_DRIVER_TEST */
506#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
507 default:
508 /* Key is declared with a lifetime not known to us */
509 return( status );
510 }
511}
512
Ronald Cron84cc9942020-11-25 14:30:05 +0100513psa_status_t psa_driver_wrapper_export_public_key(
514 const psa_key_attributes_t *attributes,
515 const uint8_t *key_buffer, size_t key_buffer_size,
516 uint8_t *data, size_t data_size, size_t *data_length )
517
Steven Cooremanb9b84422020-10-14 14:39:20 +0200518{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200519 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100520 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
521 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200522
Ronald Cron152e3f82020-11-26 16:06:41 +0100523 /* Try dynamically-registered SE interface first */
524#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
525 const psa_drv_se_t *drv;
526 psa_drv_se_context_t *drv_context;
527
528 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
529 {
530 if( ( drv->key_management == NULL ) ||
531 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100532 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100533 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100534 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100535
536 return( drv->key_management->p_export_public(
537 drv_context,
538 *( (psa_key_slot_number_t *)key_buffer ),
539 data, data_size, data_length ) );
540 }
541#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
542
Steven Cooremanb9b84422020-10-14 14:39:20 +0200543 switch( location )
544 {
545 case PSA_KEY_LOCATION_LOCAL_STORAGE:
546 /* Key is stored in the slot in export representation, so
547 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100548#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200549#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron84cc9942020-11-25 14:30:05 +0100550 status = test_transparent_export_public_key( attributes,
551 key_buffer,
552 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200553 data,
554 data_size,
555 data_length );
556 /* Declared with fallback == true */
557 if( status != PSA_ERROR_NOT_SUPPORTED )
558 return( status );
559#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100560#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200561 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100562 return( psa_export_public_key_internal( attributes,
563 key_buffer,
564 key_buffer_size,
565 data,
566 data_size,
567 data_length ) );
568
Steven Cooremanb9b84422020-10-14 14:39:20 +0200569 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100570#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200571#if defined(PSA_CRYPTO_DRIVER_TEST)
572 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron84cc9942020-11-25 14:30:05 +0100573 return( test_opaque_export_public_key( attributes,
574 key_buffer,
575 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200576 data,
577 data_size,
578 data_length ) );
579#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100580#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200581 default:
582 /* Key is declared with a lifetime not known to us */
583 return( status );
584 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200585}
586
Steven Cooreman6801f082021-02-19 17:21:22 +0100587psa_status_t psa_driver_wrapper_get_builtin_key(
588 psa_drv_slot_number_t slot_number,
589 psa_key_attributes_t *attributes,
590 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
591{
592 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
593 switch( location )
594 {
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100595#if defined(PSA_CRYPTO_DRIVER_TEST)
596 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
597#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
598 return( test_opaque_get_builtin_key(
599 slot_number,
600 attributes,
601 key_buffer, key_buffer_size, key_buffer_length ) );
602#else
603 return( PSA_ERROR_DOES_NOT_EXIST );
604#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
605#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman6801f082021-02-19 17:21:22 +0100606 default:
607 (void) slot_number;
608 (void) key_buffer;
609 (void) key_buffer_size;
610 (void) key_buffer_length;
611 return( PSA_ERROR_DOES_NOT_EXIST );
612 }
613}
614
Steven Cooreman37941cb2020-07-28 18:49:51 +0200615/*
616 * Cipher functions
617 */
618psa_status_t psa_driver_wrapper_cipher_encrypt(
619 psa_key_slot_t *slot,
620 psa_algorithm_t alg,
621 const uint8_t *input,
622 size_t input_length,
623 uint8_t *output,
624 size_t output_size,
625 size_t *output_length )
626{
627#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
628 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
629 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
630 psa_key_attributes_t attributes = {
631 .core = slot->attr
632 };
633
634 switch( location )
635 {
636 case PSA_KEY_LOCATION_LOCAL_STORAGE:
637 /* Key is stored in the slot in export representation, so
638 * cycle through all known transparent accelerators */
639#if defined(PSA_CRYPTO_DRIVER_TEST)
640 status = test_transparent_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100641 slot->key.data,
642 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200643 alg,
644 input,
645 input_length,
646 output,
647 output_size,
648 output_length );
649 /* Declared with fallback == true */
650 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200651 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200652#endif /* PSA_CRYPTO_DRIVER_TEST */
653 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200654 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200655 /* Add cases for opaque driver here */
656#if defined(PSA_CRYPTO_DRIVER_TEST)
657 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
658 return( test_opaque_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100659 slot->key.data,
660 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200661 alg,
662 input,
663 input_length,
664 output,
665 output_size,
666 output_length ) );
667#endif /* PSA_CRYPTO_DRIVER_TEST */
668 default:
669 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200670 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200671 }
672#else /* PSA_CRYPTO_DRIVER_PRESENT */
673 (void) slot;
674 (void) alg;
675 (void) input;
676 (void) input_length;
677 (void) output;
678 (void) output_size;
679 (void) output_length;
680
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200681 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200682#endif /* PSA_CRYPTO_DRIVER_PRESENT */
683}
684
685psa_status_t psa_driver_wrapper_cipher_decrypt(
686 psa_key_slot_t *slot,
687 psa_algorithm_t alg,
688 const uint8_t *input,
689 size_t input_length,
690 uint8_t *output,
691 size_t output_size,
692 size_t *output_length )
693{
694#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
695 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
696 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
697 psa_key_attributes_t attributes = {
698 .core = slot->attr
699 };
700
701 switch( location )
702 {
703 case PSA_KEY_LOCATION_LOCAL_STORAGE:
704 /* Key is stored in the slot in export representation, so
705 * cycle through all known transparent accelerators */
706#if defined(PSA_CRYPTO_DRIVER_TEST)
707 status = test_transparent_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100708 slot->key.data,
709 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200710 alg,
711 input,
712 input_length,
713 output,
714 output_size,
715 output_length );
716 /* Declared with fallback == true */
717 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200718 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200719#endif /* PSA_CRYPTO_DRIVER_TEST */
720 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200721 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200722 /* Add cases for opaque driver here */
723#if defined(PSA_CRYPTO_DRIVER_TEST)
724 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
725 return( test_opaque_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100726 slot->key.data,
727 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200728 alg,
729 input,
730 input_length,
731 output,
732 output_size,
733 output_length ) );
734#endif /* PSA_CRYPTO_DRIVER_TEST */
735 default:
736 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200737 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200738 }
739#else /* PSA_CRYPTO_DRIVER_PRESENT */
740 (void) slot;
741 (void) alg;
742 (void) input;
743 (void) input_length;
744 (void) output;
745 (void) output_size;
746 (void) output_length;
747
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200748 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200749#endif /* PSA_CRYPTO_DRIVER_PRESENT */
750}
751
752psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100753 psa_cipher_operation_t *operation,
754 const psa_key_attributes_t *attributes,
755 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200756 psa_algorithm_t alg )
757{
Ronald Cron0b805592020-12-14 18:08:20 +0100758 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona4af55f2020-12-14 14:36:06 +0100759 psa_key_location_t location =
760 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200761
Steven Cooreman37941cb2020-07-28 18:49:51 +0200762 switch( location )
763 {
764 case PSA_KEY_LOCATION_LOCAL_STORAGE:
765 /* Key is stored in the slot in export representation, so
766 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100767#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200768#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100769 status = test_transparent_cipher_encrypt_setup(
770 &operation->ctx.transparent_test_driver_ctx,
771 attributes,
772 key_buffer,
773 key_buffer_size,
774 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200775 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200776 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100777 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +0200778
Ronald Cron0b805592020-12-14 18:08:20 +0100779 if( status != PSA_ERROR_NOT_SUPPORTED )
780 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200781#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100782#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100783#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200784 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +0100785 status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100786 attributes,
787 key_buffer,
788 key_buffer_size,
789 alg );
790 if( status == PSA_SUCCESS )
Ronald Cron6e412a72021-03-10 09:58:47 +0100791 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Ronald Cron49fafa92021-03-10 08:34:23 +0100792
Ronald Cron7b4154d2021-03-19 14:49:41 +0100793 if( status != PSA_ERROR_NOT_SUPPORTED )
794 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100795#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
796 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron0b805592020-12-14 18:08:20 +0100797
Steven Cooreman37941cb2020-07-28 18:49:51 +0200798 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100799#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200800#if defined(PSA_CRYPTO_DRIVER_TEST)
801 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100802 status = test_opaque_cipher_encrypt_setup(
803 &operation->ctx.opaque_test_driver_ctx,
804 attributes,
805 key_buffer, key_buffer_size,
806 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200807
Steven Cooreman37941cb2020-07-28 18:49:51 +0200808 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100809 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200810
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200811 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200812#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100813#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +0200814 default:
815 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +0100816 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100817 (void)key_buffer;
818 (void)key_buffer_size;
819 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +0200820 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200821 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200822}
823
824psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100825 psa_cipher_operation_t *operation,
826 const psa_key_attributes_t *attributes,
827 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200828 psa_algorithm_t alg )
829{
Steven Cooreman37941cb2020-07-28 18:49:51 +0200830 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crona4af55f2020-12-14 14:36:06 +0100831 psa_key_location_t location =
832 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200833
Steven Cooreman37941cb2020-07-28 18:49:51 +0200834 switch( location )
835 {
836 case PSA_KEY_LOCATION_LOCAL_STORAGE:
837 /* Key is stored in the slot in export representation, so
838 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100839#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200840#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100841 status = test_transparent_cipher_decrypt_setup(
842 &operation->ctx.transparent_test_driver_ctx,
843 attributes,
844 key_buffer,
845 key_buffer_size,
846 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200847 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200848 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100849 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +0200850
Ronald Cron0b805592020-12-14 18:08:20 +0100851 if( status != PSA_ERROR_NOT_SUPPORTED )
852 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200853#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100854#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100855#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200856 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +0100857 status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100858 attributes,
859 key_buffer,
860 key_buffer_size,
861 alg );
862 if( status == PSA_SUCCESS )
863 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
864
865 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100866#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
867 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron49fafa92021-03-10 08:34:23 +0100868
Steven Cooreman37941cb2020-07-28 18:49:51 +0200869 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100870#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200871#if defined(PSA_CRYPTO_DRIVER_TEST)
872 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100873 status = test_opaque_cipher_decrypt_setup(
874 &operation->ctx.opaque_test_driver_ctx,
875 attributes,
876 key_buffer, key_buffer_size,
877 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200878
Steven Cooreman37941cb2020-07-28 18:49:51 +0200879 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100880 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200881
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200882 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200883#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100884#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +0200885 default:
886 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +0100887 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100888 (void)key_buffer;
889 (void)key_buffer_size;
890 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +0200891 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200892 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200893}
894
Steven Cooreman37941cb2020-07-28 18:49:51 +0200895psa_status_t psa_driver_wrapper_cipher_set_iv(
Ronald Cron6056fe82020-12-15 13:58:07 +0100896 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200897 const uint8_t *iv,
898 size_t iv_length )
899{
Ronald Cron49fafa92021-03-10 08:34:23 +0100900 switch( operation->id )
901 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100902#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +0100903 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +0100904 return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100905 iv,
906 iv_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100907#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100908
909#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200910#if defined(PSA_CRYPTO_DRIVER_TEST)
911 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100912 return( test_transparent_cipher_set_iv(
913 &operation->ctx.transparent_test_driver_ctx,
914 iv, iv_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +0100915
Steven Cooreman37941cb2020-07-28 18:49:51 +0200916 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100917 return( test_opaque_cipher_set_iv(
918 &operation->ctx.opaque_test_driver_ctx,
919 iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200920#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100921#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +0100922 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200923
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100924 (void)iv;
925 (void)iv_length;
926
Ronald Crondd24c9b2020-12-15 14:10:01 +0100927 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200928}
929
930psa_status_t psa_driver_wrapper_cipher_update(
Ronald Cron6056fe82020-12-15 13:58:07 +0100931 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200932 const uint8_t *input,
933 size_t input_length,
934 uint8_t *output,
935 size_t output_size,
936 size_t *output_length )
937{
Ronald Cron49fafa92021-03-10 08:34:23 +0100938 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200939 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100940#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +0100941 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +0100942 return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100943 input,
944 input_length,
945 output,
946 output_size,
947 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100948#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
949
Ronald Cron49fafa92021-03-10 08:34:23 +0100950#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200951#if defined(PSA_CRYPTO_DRIVER_TEST)
952 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100953 return( test_transparent_cipher_update(
954 &operation->ctx.transparent_test_driver_ctx,
955 input, input_length,
956 output, output_size, output_length ) );
957
Steven Cooreman37941cb2020-07-28 18:49:51 +0200958 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100959 return( test_opaque_cipher_update(
960 &operation->ctx.opaque_test_driver_ctx,
961 input, input_length,
962 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200963#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100964#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +0100965 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200966
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100967 (void)input;
968 (void)input_length;
969 (void)output;
970 (void)output_size;
971 (void)output_length;
972
Ronald Crondd24c9b2020-12-15 14:10:01 +0100973 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200974}
975
976psa_status_t psa_driver_wrapper_cipher_finish(
Ronald Cron6056fe82020-12-15 13:58:07 +0100977 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200978 uint8_t *output,
979 size_t output_size,
980 size_t *output_length )
981{
Ronald Cron49fafa92021-03-10 08:34:23 +0100982 switch( operation->id )
983 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100984#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +0100985 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +0100986 return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100987 output,
988 output_size,
989 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100990#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100991
992#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200993#if defined(PSA_CRYPTO_DRIVER_TEST)
994 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100995 return( test_transparent_cipher_finish(
996 &operation->ctx.transparent_test_driver_ctx,
997 output, output_size, output_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +0100998
Steven Cooreman37941cb2020-07-28 18:49:51 +0200999 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001000 return( test_opaque_cipher_finish(
1001 &operation->ctx.opaque_test_driver_ctx,
1002 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001003#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001004#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001005 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001006
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001007 (void)output;
1008 (void)output_size;
1009 (void)output_length;
1010
Ronald Crondd24c9b2020-12-15 14:10:01 +01001011 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001012}
1013
1014psa_status_t psa_driver_wrapper_cipher_abort(
Ronald Cron6056fe82020-12-15 13:58:07 +01001015 psa_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001016{
Ronald Crondd24c9b2020-12-15 14:10:01 +01001017 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001018
Ronald Cron49fafa92021-03-10 08:34:23 +01001019 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001020 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001021#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001022 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001023 return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001024#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +01001025
1026#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001027#if defined(PSA_CRYPTO_DRIVER_TEST)
1028 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001029 status = test_transparent_cipher_abort(
1030 &operation->ctx.transparent_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001031 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001032 &operation->ctx.transparent_test_driver_ctx,
1033 sizeof( operation->ctx.transparent_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001034 return( status );
Ronald Cron49fafa92021-03-10 08:34:23 +01001035
Steven Cooreman37941cb2020-07-28 18:49:51 +02001036 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001037 status = test_opaque_cipher_abort(
1038 &operation->ctx.opaque_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001039 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001040 &operation->ctx.opaque_test_driver_ctx,
1041 sizeof( operation->ctx.opaque_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001042 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001043#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001044#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001045 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001046
Ronald Cron49fafa92021-03-10 08:34:23 +01001047 (void)status;
Ronald Crondd24c9b2020-12-15 14:10:01 +01001048 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001049}
1050
Steven Cooreman1e582352021-02-18 17:24:37 +01001051/*
1052 * Hashing functions
1053 */
1054psa_status_t psa_driver_wrapper_hash_compute(
1055 psa_algorithm_t alg,
1056 const uint8_t *input,
1057 size_t input_length,
1058 uint8_t *hash,
1059 size_t hash_size,
1060 size_t *hash_length)
1061{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001062 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001063
1064 /* Try accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001065#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001066 status = mbedtls_transparent_test_driver_hash_compute(
1067 alg, input, input_length, hash, hash_size, hash_length );
Steven Cooremanf7638102021-03-04 15:14:36 +01001068 if( status != PSA_ERROR_NOT_SUPPORTED )
1069 return( status );
1070#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001071
1072 /* If software fallback is compiled in, try fallback */
1073#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1074 status = mbedtls_psa_hash_compute( alg, input, input_length,
1075 hash, hash_size, hash_length );
1076 if( status != PSA_ERROR_NOT_SUPPORTED )
1077 return( status );
1078#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001079 (void) status;
1080 (void) alg;
1081 (void) input;
1082 (void) input_length;
1083 (void) hash;
1084 (void) hash_size;
1085 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001086
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001087 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman1e582352021-02-18 17:24:37 +01001088}
1089
1090psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001091 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001092 psa_algorithm_t alg )
1093{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001094 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001095
Steven Cooreman1e582352021-02-18 17:24:37 +01001096 /* Try setup on accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001097#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001098 status = mbedtls_transparent_test_driver_hash_setup(
1099 &operation->ctx.test_driver_ctx, alg );
Steven Cooremanf7638102021-03-04 15:14:36 +01001100 if( status == PSA_SUCCESS )
1101 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1102
1103 if( status != PSA_ERROR_NOT_SUPPORTED )
1104 return( status );
1105#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001106
1107 /* If software fallback is compiled in, try fallback */
1108#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001109 status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
Steven Cooreman1e582352021-02-18 17:24:37 +01001110 if( status == PSA_SUCCESS )
Steven Cooreman1e582352021-02-18 17:24:37 +01001111 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Steven Cooreman1e582352021-02-18 17:24:37 +01001112
1113 if( status != PSA_ERROR_NOT_SUPPORTED )
1114 return( status );
1115#endif
1116 /* Nothing left to try if we fall through here */
1117 (void) status;
1118 (void) operation;
1119 (void) alg;
1120 return( PSA_ERROR_NOT_SUPPORTED );
1121}
1122
1123psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001124 const psa_hash_operation_t *source_operation,
1125 psa_hash_operation_t *target_operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001126{
Steven Cooreman1e582352021-02-18 17:24:37 +01001127 switch( source_operation->id )
1128 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001129#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1130 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1131 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1132 return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
1133 &target_operation->ctx.mbedtls_ctx ) );
1134#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001135#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001136 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1137 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001138 return( mbedtls_transparent_test_driver_hash_clone(
1139 &source_operation->ctx.test_driver_ctx,
1140 &target_operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001141#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001142 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001143 (void) target_operation;
1144 return( PSA_ERROR_BAD_STATE );
1145 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001146}
1147
1148psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001149 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001150 const uint8_t *input,
1151 size_t input_length )
1152{
Steven Cooreman1e582352021-02-18 17:24:37 +01001153 switch( operation->id )
1154 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001155#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1156 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1157 return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
1158 input, input_length ) );
1159#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001160#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001161 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001162 return( mbedtls_transparent_test_driver_hash_update(
1163 &operation->ctx.test_driver_ctx,
1164 input, input_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001165#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001166 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001167 (void) input;
1168 (void) input_length;
1169 return( PSA_ERROR_BAD_STATE );
1170 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001171}
1172
1173psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001174 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001175 uint8_t *hash,
1176 size_t hash_size,
1177 size_t *hash_length )
1178{
Steven Cooreman1e582352021-02-18 17:24:37 +01001179 switch( operation->id )
1180 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001181#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1182 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1183 return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
1184 hash, hash_size, hash_length ) );
1185#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001186#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001187 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001188 return( mbedtls_transparent_test_driver_hash_finish(
1189 &operation->ctx.test_driver_ctx,
1190 hash, hash_size, hash_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001191#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001192 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001193 (void) hash;
1194 (void) hash_size;
1195 (void) hash_length;
1196 return( PSA_ERROR_BAD_STATE );
1197 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001198}
1199
1200psa_status_t psa_driver_wrapper_hash_abort(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001201 psa_hash_operation_t *operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001202{
Steven Cooreman1e582352021-02-18 17:24:37 +01001203 switch( operation->id )
1204 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001205#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1206 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1207 return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
1208#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001209#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001210 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001211 return( mbedtls_transparent_test_driver_hash_abort(
1212 &operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001213#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001214 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001215 return( PSA_ERROR_BAD_STATE );
1216 }
1217}
1218
Ronald Cronde822812021-03-17 16:08:20 +01001219psa_status_t psa_driver_wrapper_aead_encrypt(
1220 const psa_key_attributes_t *attributes,
1221 const uint8_t *key_buffer, size_t key_buffer_size,
1222 psa_algorithm_t alg,
1223 const uint8_t *nonce, size_t nonce_length,
1224 const uint8_t *additional_data, size_t additional_data_length,
1225 const uint8_t *plaintext, size_t plaintext_length,
1226 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
1227{
1228 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1229 psa_key_location_t location =
1230 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1231
1232 switch( location )
1233 {
1234 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1235 /* Key is stored in the slot in export representation, so
1236 * cycle through all known transparent accelerators */
1237
1238#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1239#if defined(PSA_CRYPTO_DRIVER_TEST)
1240 status = test_transparent_aead_encrypt(
1241 attributes, key_buffer, key_buffer_size,
1242 alg,
1243 nonce, nonce_length,
1244 additional_data, additional_data_length,
1245 plaintext, plaintext_length,
1246 ciphertext, ciphertext_size, ciphertext_length );
1247 /* Declared with fallback == true */
1248 if( status != PSA_ERROR_NOT_SUPPORTED )
1249 return( status );
1250#endif /* PSA_CRYPTO_DRIVER_TEST */
1251#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1252
1253 /* Fell through, meaning no accelerator supports this operation */
1254 return( mbedtls_psa_aead_encrypt(
1255 attributes, key_buffer, key_buffer_size,
1256 alg,
1257 nonce, nonce_length,
1258 additional_data, additional_data_length,
1259 plaintext, plaintext_length,
1260 ciphertext, ciphertext_size, ciphertext_length ) );
1261
1262 /* Add cases for opaque driver here */
1263
1264 default:
1265 /* Key is declared with a lifetime not known to us */
1266 (void)status;
1267 return( PSA_ERROR_INVALID_ARGUMENT );
1268 }
1269}
1270
1271psa_status_t psa_driver_wrapper_aead_decrypt(
1272 const psa_key_attributes_t *attributes,
1273 const uint8_t *key_buffer, size_t key_buffer_size,
1274 psa_algorithm_t alg,
1275 const uint8_t *nonce, size_t nonce_length,
1276 const uint8_t *additional_data, size_t additional_data_length,
1277 const uint8_t *ciphertext, size_t ciphertext_length,
1278 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
1279{
1280 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1281 psa_key_location_t location =
1282 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1283
1284 switch( location )
1285 {
1286 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1287 /* Key is stored in the slot in export representation, so
1288 * cycle through all known transparent accelerators */
1289
1290#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1291#if defined(PSA_CRYPTO_DRIVER_TEST)
1292 status = test_transparent_aead_decrypt(
1293 attributes, key_buffer, key_buffer_size,
1294 alg,
1295 nonce, nonce_length,
1296 additional_data, additional_data_length,
1297 ciphertext, ciphertext_length,
1298 plaintext, plaintext_size, plaintext_length );
1299 /* Declared with fallback == true */
1300 if( status != PSA_ERROR_NOT_SUPPORTED )
1301 return( status );
1302#endif /* PSA_CRYPTO_DRIVER_TEST */
1303#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1304
1305 /* Fell through, meaning no accelerator supports this operation */
1306 return( mbedtls_psa_aead_decrypt(
1307 attributes, key_buffer, key_buffer_size,
1308 alg,
1309 nonce, nonce_length,
1310 additional_data, additional_data_length,
1311 ciphertext, ciphertext_length,
1312 plaintext, plaintext_size, plaintext_length ) );
1313
1314 /* Add cases for opaque driver here */
1315
1316 default:
1317 /* Key is declared with a lifetime not known to us */
1318 (void)status;
1319 return( PSA_ERROR_INVALID_ARGUMENT );
1320 }
1321}
Steven Cooremancd84cb42020-07-16 20:28:36 +02001322/* End of automatically generated file. */