blob: 7bb0185dd515238d1f3e4a1ae3d44ea2137c01d7 [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
22#include "psa_crypto_core.h"
23#include "psa_crypto_driver_wrappers.h"
Steven Cooreman2a1664c2020-07-20 15:33:08 +020024#include "mbedtls/platform.h"
25
26#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
Steven Cooremancd84cb42020-07-16 20:28:36 +020027
28/* Include test driver definition when running tests */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020029#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman56250fd2020-09-04 13:07:15 +020030#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020031#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020032#endif
33#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020034#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020035#endif
Steven Cooreman0d7c64d2020-09-07 16:17:55 +020036#include "test/drivers/test_driver.h"
Steven Cooremanf1720ea2020-07-24 18:41:58 +020037#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +020038
Steven Cooreman56250fd2020-09-04 13:07:15 +020039/* Repeat above block for each JSON-declared driver during autogeneration */
40
Steven Cooreman1e582352021-02-18 17:24:37 +010041/* Auto-generated values depending on which drivers are registered.
42 * ID 0 is reserved for unallocated operations.
43 * ID 1 is reserved for the Mbed TLS software driver. */
Steven Cooreman37941cb2020-07-28 18:49:51 +020044#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman1e582352021-02-18 17:24:37 +010045#define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2)
46#define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3)
Steven Cooreman37941cb2020-07-28 18:49:51 +020047#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +020048#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
49
Steven Cooreman1e582352021-02-18 17:24:37 +010050#define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
51
Steven Cooreman2a1664c2020-07-20 15:33:08 +020052/* Support the 'old' SE interface when asked to */
Steven Cooreman7a250572020-07-17 16:43:05 +020053#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman56250fd2020-09-04 13:07:15 +020054/* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
55 * SE driver is present, to avoid unused argument errors at compile time. */
56#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020057#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020058#endif
Steven Cooreman7a250572020-07-17 16:43:05 +020059#include "psa_crypto_se.h"
60#endif
61
Steven Cooremancd84cb42020-07-16 20:28:36 +020062/* Start delegation functions */
Ronald Cron9f17aa42020-12-08 17:07:25 +010063psa_status_t psa_driver_wrapper_sign_hash(
64 const psa_key_attributes_t *attributes,
65 const uint8_t *key_buffer, size_t key_buffer_size,
66 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
67 uint8_t *signature, size_t signature_size, size_t *signature_length )
Steven Cooremancd84cb42020-07-16 20:28:36 +020068{
Steven Cooreman7a250572020-07-17 16:43:05 +020069 /* Try dynamically-registered SE interface first */
70#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
71 const psa_drv_se_t *drv;
72 psa_drv_se_context_t *drv_context;
73
Ronald Cron9f17aa42020-12-08 17:07:25 +010074 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman7a250572020-07-17 16:43:05 +020075 {
76 if( drv->asymmetric == NULL ||
77 drv->asymmetric->p_sign == NULL )
78 {
79 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +020080 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman7a250572020-07-17 16:43:05 +020081 }
Ronald Cron9f17aa42020-12-08 17:07:25 +010082 return( drv->asymmetric->p_sign(
83 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
84 alg, hash, hash_length,
85 signature, signature_size, signature_length ) );
Steven Cooreman7a250572020-07-17 16:43:05 +020086 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +020087#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman7a250572020-07-17 16:43:05 +020088
Ronald Cronfce9df22020-12-08 18:06:03 +010089 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +010090 psa_key_location_t location =
91 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooremancd84cb42020-07-16 20:28:36 +020092
93 switch( location )
94 {
95 case PSA_KEY_LOCATION_LOCAL_STORAGE:
96 /* Key is stored in the slot in export representation, so
97 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +010098#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +020099#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron9f17aa42020-12-08 17:07:25 +0100100 status = test_transparent_signature_sign_hash( attributes,
101 key_buffer,
102 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200103 alg,
104 hash,
105 hash_length,
106 signature,
107 signature_size,
108 signature_length );
109 /* Declared with fallback == true */
110 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200111 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200112#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100113#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200114 /* Fell through, meaning no accelerator supports this operation */
Ronald Cronfce9df22020-12-08 18:06:03 +0100115 return( psa_sign_hash_internal( attributes,
116 key_buffer,
117 key_buffer_size,
118 alg,
119 hash,
120 hash_length,
121 signature,
122 signature_size,
123 signature_length ) );
124
Steven Cooremancd84cb42020-07-16 20:28:36 +0200125 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100126#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200127#if defined(PSA_CRYPTO_DRIVER_TEST)
128 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron9f17aa42020-12-08 17:07:25 +0100129 return( test_opaque_signature_sign_hash( attributes,
130 key_buffer,
131 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200132 alg,
133 hash,
134 hash_length,
135 signature,
136 signature_size,
137 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200138#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100139#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200140 default:
141 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100142 (void)status;
143 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200144 }
Steven Cooremancd84cb42020-07-16 20:28:36 +0200145}
146
Ronald Cron9f17aa42020-12-08 17:07:25 +0100147psa_status_t psa_driver_wrapper_verify_hash(
148 const psa_key_attributes_t *attributes,
149 const uint8_t *key_buffer, size_t key_buffer_size,
150 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
151 const uint8_t *signature, size_t signature_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200152{
Steven Cooreman55ae2172020-07-17 19:46:15 +0200153 /* Try dynamically-registered SE interface first */
154#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
155 const psa_drv_se_t *drv;
156 psa_drv_se_context_t *drv_context;
157
Ronald Cron9f17aa42020-12-08 17:07:25 +0100158 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200159 {
160 if( drv->asymmetric == NULL ||
161 drv->asymmetric->p_verify == NULL )
162 {
163 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200164 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200165 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100166 return( drv->asymmetric->p_verify(
167 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
168 alg, hash, hash_length,
169 signature, signature_length ) );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200170 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200171#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200172
Ronald Cronfce9df22020-12-08 18:06:03 +0100173 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100174 psa_key_location_t location =
175 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200176
177 switch( location )
178 {
179 case PSA_KEY_LOCATION_LOCAL_STORAGE:
180 /* Key is stored in the slot in export representation, so
181 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100182#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200183#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron9f17aa42020-12-08 17:07:25 +0100184 status = test_transparent_signature_verify_hash( attributes,
185 key_buffer,
186 key_buffer_size,
Steven Cooreman55ae2172020-07-17 19:46:15 +0200187 alg,
188 hash,
189 hash_length,
190 signature,
191 signature_length );
192 /* Declared with fallback == true */
193 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200194 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200195#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100196#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
197
198 return( psa_verify_hash_internal( attributes,
199 key_buffer,
200 key_buffer_size,
201 alg,
202 hash,
203 hash_length,
204 signature,
205 signature_length ) );
206
Steven Cooreman55ae2172020-07-17 19:46:15 +0200207 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100208#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200209#if defined(PSA_CRYPTO_DRIVER_TEST)
210 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron9f17aa42020-12-08 17:07:25 +0100211 return( test_opaque_signature_verify_hash( attributes,
212 key_buffer,
213 key_buffer_size,
Steven Cooreman55ae2172020-07-17 19:46:15 +0200214 alg,
215 hash,
216 hash_length,
217 signature,
218 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200219#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100220#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200221 default:
222 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100223 (void)status;
224 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200225 }
Steven Cooreman55ae2172020-07-17 19:46:15 +0200226}
227
Ronald Cron31216282020-12-05 18:47:56 +0100228/** Get the key buffer size for the key material of a generated key in the
229 * case of an opaque driver without storage.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200230 *
Ronald Cron31216282020-12-05 18:47:56 +0100231 * \param[in] attributes The key attributes.
232 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200233 *
234 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100235 * The minimum size for a buffer to contain the key material has been
236 * returned successfully.
237 * \retval #PSA_ERROR_INVALID_ARGUMENT
238 * The size in bits of the key is not valid.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200239 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100240 * The type and/or the size in bits of the key or the combination of
241 * the two is not supported.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200242 */
Ronald Cron9df74be2020-12-05 19:15:23 +0100243psa_status_t psa_driver_wrapper_get_key_buffer_size(
Ronald Cron31216282020-12-05 18:47:56 +0100244 const psa_key_attributes_t *attributes,
245 size_t *key_buffer_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200246{
John Durkop2c618352020-09-22 06:54:01 -0700247 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
248 psa_key_type_t key_type = attributes->core.type;
249 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200250
Ronald Cron31216282020-12-05 18:47:56 +0100251 *key_buffer_size = 0;
John Durkop2c618352020-09-22 06:54:01 -0700252 switch( location )
253 {
John Durkop2c618352020-09-22 06:54:01 -0700254#if defined(PSA_CRYPTO_DRIVER_TEST)
255 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
John Durkopac93e3b2020-10-16 06:48:55 -0700256#ifdef TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION
Ronald Cron31216282020-12-05 18:47:56 +0100257 *key_buffer_size = test_size_function( key_type, key_bits );
John Durkop2c618352020-09-22 06:54:01 -0700258 return( PSA_SUCCESS );
259#else /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
260 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) )
261 {
Ronald Cron31216282020-12-05 18:47:56 +0100262 int public_key_overhead =
263 ( ( TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 ) ?
264 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) : 0 );
265 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop135ce692020-10-19 07:12:28 -0700266 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE
267 + public_key_overhead;
268 }
Ronald Cron31216282020-12-05 18:47:56 +0100269 else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( key_type ) )
John Durkop135ce692020-10-19 07:12:28 -0700270 {
Ronald Cron31216282020-12-05 18:47:56 +0100271 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700272 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE;
273 }
John Durkop135ce692020-10-19 07:12:28 -0700274 else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) &&
Ronald Cron31216282020-12-05 18:47:56 +0100275 !PSA_KEY_TYPE_IS_PUBLIC_KEY ( key_type ) )
John Durkop2c618352020-09-22 06:54:01 -0700276 {
Ronald Cron31216282020-12-05 18:47:56 +0100277 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700278 + TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR
279 * ( ( key_bits + 7 ) / 8 );
280 }
281 else
282 {
283 return( PSA_ERROR_NOT_SUPPORTED );
284 }
285 return( PSA_SUCCESS );
286#endif /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
287#endif /* PSA_CRYPTO_DRIVER_TEST */
288
289 default:
Ronald Cron9df74be2020-12-05 19:15:23 +0100290 (void)key_type;
291 (void)key_bits;
Steven Cooreman56250fd2020-09-04 13:07:15 +0200292 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200293 }
294}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200295
Ronald Cron977c2472020-10-13 08:32:21 +0200296psa_status_t psa_driver_wrapper_generate_key(
297 const psa_key_attributes_t *attributes,
298 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200299{
Ronald Cron977c2472020-10-13 08:32:21 +0200300 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
301 psa_key_location_t location =
302 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
303
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200304 /* Try dynamically-registered SE interface first */
305#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
306 const psa_drv_se_t *drv;
307 psa_drv_se_context_t *drv_context;
308
Ronald Cron977c2472020-10-13 08:32:21 +0200309 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200310 {
311 size_t pubkey_length = 0; /* We don't support this feature yet */
312 if( drv->key_management == NULL ||
313 drv->key_management->p_generate == NULL )
314 {
315 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200316 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200317 }
318 return( drv->key_management->p_generate(
Ronald Cron977c2472020-10-13 08:32:21 +0200319 drv_context,
320 *( (psa_key_slot_number_t *)key_buffer ),
Ronald Cronea0f8a62020-11-25 17:52:23 +0100321 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200322 }
323#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
324
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200325 switch( location )
326 {
327 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Ronald Cron977c2472020-10-13 08:32:21 +0200328#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200329 /* Transparent drivers are limited to generating asymmetric keys */
Ronald Cron977c2472020-10-13 08:32:21 +0200330 if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200331 {
Ronald Cron977c2472020-10-13 08:32:21 +0200332 /* Cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200333#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron977c2472020-10-13 08:32:21 +0200334 status = test_transparent_generate_key(
335 attributes, key_buffer, key_buffer_size,
336 key_buffer_length );
337 /* Declared with fallback == true */
338 if( status != PSA_ERROR_NOT_SUPPORTED )
339 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200340#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200341 }
342#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
343
344 /* Software fallback */
345 status = psa_generate_key_internal(
346 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200347 break;
Ronald Cron977c2472020-10-13 08:32:21 +0200348
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200349 /* Add cases for opaque driver here */
Ronald Cron977c2472020-10-13 08:32:21 +0200350#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200351#if defined(PSA_CRYPTO_DRIVER_TEST)
352 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron977c2472020-10-13 08:32:21 +0200353 status = test_opaque_generate_key(
354 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200355 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200356#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200357#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
358
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200359 default:
360 /* Key is declared with a lifetime not known to us */
361 status = PSA_ERROR_INVALID_ARGUMENT;
362 break;
363 }
364
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200365 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200366}
367
Ronald Cron83282872020-11-22 14:02:39 +0100368psa_status_t psa_driver_wrapper_import_key(
369 const psa_key_attributes_t *attributes,
370 const uint8_t *data,
371 size_t data_length,
372 uint8_t *key_buffer,
373 size_t key_buffer_size,
374 size_t *key_buffer_length,
375 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200376{
Steven Cooreman04524762020-10-13 17:43:44 +0200377 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100378 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
379 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200380
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100381 /* Try dynamically-registered SE interface first */
382#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
383 const psa_drv_se_t *drv;
384 psa_drv_se_context_t *drv_context;
385
386 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
387 {
388 if( drv->key_management == NULL ||
389 drv->key_management->p_import == NULL )
390 return( PSA_ERROR_NOT_SUPPORTED );
391
392 /* The driver should set the number of key bits, however in
393 * case it doesn't, we initialize bits to an invalid value. */
394 *bits = PSA_MAX_KEY_BITS + 1;
395 status = drv->key_management->p_import(
396 drv_context,
397 *( (psa_key_slot_number_t *)key_buffer ),
398 attributes, data, data_length, bits );
399
400 if( status != PSA_SUCCESS )
401 return( status );
402
403 if( (*bits) > PSA_MAX_KEY_BITS )
404 return( PSA_ERROR_NOT_SUPPORTED );
405
406 return( PSA_SUCCESS );
407 }
408#endif /* PSA_CRYPTO_SE_C */
409
Ronald Cronbf33c932020-11-28 18:06:53 +0100410 switch( location )
411 {
412 case PSA_KEY_LOCATION_LOCAL_STORAGE:
413 /* Key is stored in the slot in export representation, so
414 * cycle through all known transparent accelerators */
415#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
416#if defined(PSA_CRYPTO_DRIVER_TEST)
417 status = test_transparent_import_key( attributes,
418 data, data_length,
419 key_buffer, key_buffer_size,
420 key_buffer_length, bits );
421 /* Declared with fallback == true */
422 if( status != PSA_ERROR_NOT_SUPPORTED )
423 return( status );
424#endif /* PSA_CRYPTO_DRIVER_TEST */
425#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
426 /* Fell through, meaning no accelerator supports this operation */
427 return( psa_import_key_into_slot( attributes,
428 data, data_length,
429 key_buffer, key_buffer_size,
430 key_buffer_length, bits ) );
431
432 default:
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100433 /* Importing a key with external storage in not yet supported.
434 * Return in error indicating that the lifetime is not valid. */
Ronald Cronbf33c932020-11-28 18:06:53 +0100435 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100436 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100437 }
438
Steven Cooreman04524762020-10-13 17:43:44 +0200439}
440
Ronald Cron67227982020-11-26 15:16:05 +0100441psa_status_t psa_driver_wrapper_export_key(
442 const psa_key_attributes_t *attributes,
443 const uint8_t *key_buffer, size_t key_buffer_size,
444 uint8_t *data, size_t data_size, size_t *data_length )
445
446{
447 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
448 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
449 psa_get_key_lifetime( attributes ) );
450
Ronald Cron152e3f82020-11-26 16:06:41 +0100451 /* Try dynamically-registered SE interface first */
452#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
453 const psa_drv_se_t *drv;
454 psa_drv_se_context_t *drv_context;
455
456 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
457 {
458 if( ( drv->key_management == NULL ) ||
459 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100460 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100461 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100462 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100463
464 return( drv->key_management->p_export(
465 drv_context,
466 *( (psa_key_slot_number_t *)key_buffer ),
467 data, data_size, data_length ) );
468 }
469#endif /* PSA_CRYPTO_SE_C */
470
Ronald Cron67227982020-11-26 15:16:05 +0100471 switch( location )
472 {
473 case PSA_KEY_LOCATION_LOCAL_STORAGE:
474 return( psa_export_key_internal( attributes,
475 key_buffer,
476 key_buffer_size,
477 data,
478 data_size,
479 data_length ) );
480
481 /* Add cases for opaque driver here */
482#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
483#if defined(PSA_CRYPTO_DRIVER_TEST)
484 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
485 return( test_opaque_export_key( attributes,
486 key_buffer,
487 key_buffer_size,
488 data,
489 data_size,
490 data_length ) );
491#endif /* PSA_CRYPTO_DRIVER_TEST */
492#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
493 default:
494 /* Key is declared with a lifetime not known to us */
495 return( status );
496 }
497}
498
Ronald Cron84cc9942020-11-25 14:30:05 +0100499psa_status_t psa_driver_wrapper_export_public_key(
500 const psa_key_attributes_t *attributes,
501 const uint8_t *key_buffer, size_t key_buffer_size,
502 uint8_t *data, size_t data_size, size_t *data_length )
503
Steven Cooremanb9b84422020-10-14 14:39:20 +0200504{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200505 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100506 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
507 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200508
Ronald Cron152e3f82020-11-26 16:06:41 +0100509 /* Try dynamically-registered SE interface first */
510#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
511 const psa_drv_se_t *drv;
512 psa_drv_se_context_t *drv_context;
513
514 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
515 {
516 if( ( drv->key_management == NULL ) ||
517 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100518 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100519 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100520 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100521
522 return( drv->key_management->p_export_public(
523 drv_context,
524 *( (psa_key_slot_number_t *)key_buffer ),
525 data, data_size, data_length ) );
526 }
527#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
528
Steven Cooremanb9b84422020-10-14 14:39:20 +0200529 switch( location )
530 {
531 case PSA_KEY_LOCATION_LOCAL_STORAGE:
532 /* Key is stored in the slot in export representation, so
533 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100534#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200535#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron84cc9942020-11-25 14:30:05 +0100536 status = test_transparent_export_public_key( attributes,
537 key_buffer,
538 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200539 data,
540 data_size,
541 data_length );
542 /* Declared with fallback == true */
543 if( status != PSA_ERROR_NOT_SUPPORTED )
544 return( status );
545#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100546#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200547 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100548 return( psa_export_public_key_internal( attributes,
549 key_buffer,
550 key_buffer_size,
551 data,
552 data_size,
553 data_length ) );
554
Steven Cooremanb9b84422020-10-14 14:39:20 +0200555 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100556#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200557#if defined(PSA_CRYPTO_DRIVER_TEST)
558 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron84cc9942020-11-25 14:30:05 +0100559 return( test_opaque_export_public_key( attributes,
560 key_buffer,
561 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200562 data,
563 data_size,
564 data_length ) );
565#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100566#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200567 default:
568 /* Key is declared with a lifetime not known to us */
569 return( status );
570 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200571}
572
Steven Cooreman37941cb2020-07-28 18:49:51 +0200573/*
574 * Cipher functions
575 */
576psa_status_t psa_driver_wrapper_cipher_encrypt(
577 psa_key_slot_t *slot,
578 psa_algorithm_t alg,
579 const uint8_t *input,
580 size_t input_length,
581 uint8_t *output,
582 size_t output_size,
583 size_t *output_length )
584{
585#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
586 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
587 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
588 psa_key_attributes_t attributes = {
589 .core = slot->attr
590 };
591
592 switch( location )
593 {
594 case PSA_KEY_LOCATION_LOCAL_STORAGE:
595 /* Key is stored in the slot in export representation, so
596 * cycle through all known transparent accelerators */
597#if defined(PSA_CRYPTO_DRIVER_TEST)
598 status = test_transparent_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100599 slot->key.data,
600 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200601 alg,
602 input,
603 input_length,
604 output,
605 output_size,
606 output_length );
607 /* Declared with fallback == true */
608 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200609 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200610#endif /* PSA_CRYPTO_DRIVER_TEST */
611 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200612 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200613 /* Add cases for opaque driver here */
614#if defined(PSA_CRYPTO_DRIVER_TEST)
615 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
616 return( test_opaque_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100617 slot->key.data,
618 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200619 alg,
620 input,
621 input_length,
622 output,
623 output_size,
624 output_length ) );
625#endif /* PSA_CRYPTO_DRIVER_TEST */
626 default:
627 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200628 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200629 }
630#else /* PSA_CRYPTO_DRIVER_PRESENT */
631 (void) slot;
632 (void) alg;
633 (void) input;
634 (void) input_length;
635 (void) output;
636 (void) output_size;
637 (void) output_length;
638
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200639 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200640#endif /* PSA_CRYPTO_DRIVER_PRESENT */
641}
642
643psa_status_t psa_driver_wrapper_cipher_decrypt(
644 psa_key_slot_t *slot,
645 psa_algorithm_t alg,
646 const uint8_t *input,
647 size_t input_length,
648 uint8_t *output,
649 size_t output_size,
650 size_t *output_length )
651{
652#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
653 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
654 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
655 psa_key_attributes_t attributes = {
656 .core = slot->attr
657 };
658
659 switch( location )
660 {
661 case PSA_KEY_LOCATION_LOCAL_STORAGE:
662 /* Key is stored in the slot in export representation, so
663 * cycle through all known transparent accelerators */
664#if defined(PSA_CRYPTO_DRIVER_TEST)
665 status = test_transparent_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100666 slot->key.data,
667 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200668 alg,
669 input,
670 input_length,
671 output,
672 output_size,
673 output_length );
674 /* Declared with fallback == true */
675 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200676 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200677#endif /* PSA_CRYPTO_DRIVER_TEST */
678 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200679 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200680 /* Add cases for opaque driver here */
681#if defined(PSA_CRYPTO_DRIVER_TEST)
682 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
683 return( test_opaque_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100684 slot->key.data,
685 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200686 alg,
687 input,
688 input_length,
689 output,
690 output_size,
691 output_length ) );
692#endif /* PSA_CRYPTO_DRIVER_TEST */
693 default:
694 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200695 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200696 }
697#else /* PSA_CRYPTO_DRIVER_PRESENT */
698 (void) slot;
699 (void) alg;
700 (void) input;
701 (void) input_length;
702 (void) output;
703 (void) output_size;
704 (void) output_length;
705
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200706 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200707#endif /* PSA_CRYPTO_DRIVER_PRESENT */
708}
709
710psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200711 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200712 psa_key_slot_t *slot,
713 psa_algorithm_t alg )
714{
715#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
716 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
717 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
718 psa_key_attributes_t attributes = {
719 .core = slot->attr
720 };
721
Steven Cooreman37941cb2020-07-28 18:49:51 +0200722 switch( location )
723 {
724 case PSA_KEY_LOCATION_LOCAL_STORAGE:
725 /* Key is stored in the slot in export representation, so
726 * cycle through all known transparent accelerators */
727#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200728 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
729 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200730 return PSA_ERROR_INSUFFICIENT_MEMORY;
731
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200732 status = test_transparent_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200733 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100734 slot->key.data,
735 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200736 alg );
737 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200738 if( status == PSA_SUCCESS )
739 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200740 else
741 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200742 mbedtls_platform_zeroize(
743 operation->ctx,
744 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200745 mbedtls_free( operation->ctx );
746 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200747 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200748
749 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200750#endif /* PSA_CRYPTO_DRIVER_TEST */
751 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200752 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200753 /* Add cases for opaque driver here */
754#if defined(PSA_CRYPTO_DRIVER_TEST)
755 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200756 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
757 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200758 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200759
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200760 status = test_opaque_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200761 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100762 slot->key.data,
763 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200764 alg );
765 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200766 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200767 else
768 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200769 mbedtls_platform_zeroize(
770 operation->ctx,
771 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200772 mbedtls_free( operation->ctx );
773 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200774 }
775
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200776 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200777#endif /* PSA_CRYPTO_DRIVER_TEST */
778 default:
779 /* Key is declared with a lifetime not known to us */
John Durkop714e3a12020-09-29 22:07:04 -0700780 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200781 }
782#else /* PSA_CRYPTO_DRIVER_PRESENT */
783 (void)slot;
784 (void)alg;
785 (void)operation;
786
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200787 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200788#endif /* PSA_CRYPTO_DRIVER_PRESENT */
789}
790
791psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200792 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200793 psa_key_slot_t *slot,
794 psa_algorithm_t alg )
795{
796#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
797 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
798 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
799 psa_key_attributes_t attributes = {
800 .core = slot->attr
801 };
802
Steven Cooreman37941cb2020-07-28 18:49:51 +0200803 switch( location )
804 {
805 case PSA_KEY_LOCATION_LOCAL_STORAGE:
806 /* Key is stored in the slot in export representation, so
807 * cycle through all known transparent accelerators */
808#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200809 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
810 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200811 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200812
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200813 status = test_transparent_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200814 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100815 slot->key.data,
816 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200817 alg );
818 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200819 if( status == PSA_SUCCESS )
820 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200821 else
822 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200823 mbedtls_platform_zeroize(
824 operation->ctx,
825 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200826 mbedtls_free( operation->ctx );
827 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200828 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200829
830 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200831#endif /* PSA_CRYPTO_DRIVER_TEST */
832 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200833 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200834 /* Add cases for opaque driver here */
835#if defined(PSA_CRYPTO_DRIVER_TEST)
836 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200837 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
838 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200839 return PSA_ERROR_INSUFFICIENT_MEMORY;
840
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200841 status = test_opaque_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200842 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100843 slot->key.data,
844 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200845 alg );
846 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200847 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200848 else
849 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200850 mbedtls_platform_zeroize(
851 operation->ctx,
852 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200853 mbedtls_free( operation->ctx );
854 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200855 }
856
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200857 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200858#endif /* PSA_CRYPTO_DRIVER_TEST */
859 default:
860 /* Key is declared with a lifetime not known to us */
John Durkop814dca72020-10-05 06:31:12 -0700861 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200862 }
863#else /* PSA_CRYPTO_DRIVER_PRESENT */
864 (void)slot;
865 (void)alg;
866 (void)operation;
867
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200868 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200869#endif /* PSA_CRYPTO_DRIVER_PRESENT */
870}
871
872psa_status_t psa_driver_wrapper_cipher_generate_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200873 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200874 uint8_t *iv,
875 size_t iv_size,
876 size_t *iv_length )
877{
878#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200879 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200880 {
881#if defined(PSA_CRYPTO_DRIVER_TEST)
882 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200883 return( test_transparent_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200884 iv,
885 iv_size,
886 iv_length ) );
887#endif /* PSA_CRYPTO_DRIVER_TEST */
888#if defined(PSA_CRYPTO_DRIVER_TEST)
889 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200890 return( test_opaque_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200891 iv,
892 iv_size,
893 iv_length ) );
894#endif /* PSA_CRYPTO_DRIVER_TEST */
895 default:
896 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200897 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200898 }
899#else /* PSA_CRYPTO_DRIVER_PRESENT */
900 (void) operation;
901 (void) iv;
902 (void) iv_size;
903 (void) iv_length;
904
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200905 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200906#endif /* PSA_CRYPTO_DRIVER_PRESENT */
907}
908
909psa_status_t psa_driver_wrapper_cipher_set_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200910 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200911 const uint8_t *iv,
912 size_t iv_length )
913{
914#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200915 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200916 {
917#if defined(PSA_CRYPTO_DRIVER_TEST)
918 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200919 return( test_transparent_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200920 iv,
921 iv_length ) );
922#endif /* PSA_CRYPTO_DRIVER_TEST */
923#if defined(PSA_CRYPTO_DRIVER_TEST)
924 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200925 return( test_opaque_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200926 iv,
927 iv_length ) );
928#endif /* PSA_CRYPTO_DRIVER_TEST */
929 default:
930 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200931 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200932 }
933#else /* PSA_CRYPTO_DRIVER_PRESENT */
934 (void) operation;
935 (void) iv;
936 (void) iv_length;
937
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200938 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200939#endif /* PSA_CRYPTO_DRIVER_PRESENT */
940}
941
942psa_status_t psa_driver_wrapper_cipher_update(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200943 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200944 const uint8_t *input,
945 size_t input_length,
946 uint8_t *output,
947 size_t output_size,
948 size_t *output_length )
949{
950#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200951 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200952 {
953#if defined(PSA_CRYPTO_DRIVER_TEST)
954 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200955 return( test_transparent_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200956 input,
957 input_length,
958 output,
959 output_size,
960 output_length ) );
961#endif /* PSA_CRYPTO_DRIVER_TEST */
962#if defined(PSA_CRYPTO_DRIVER_TEST)
963 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200964 return( test_opaque_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200965 input,
966 input_length,
967 output,
968 output_size,
969 output_length ) );
970#endif /* PSA_CRYPTO_DRIVER_TEST */
971 default:
972 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200973 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200974 }
975#else /* PSA_CRYPTO_DRIVER_PRESENT */
976 (void) operation;
977 (void) input;
978 (void) input_length;
979 (void) output;
980 (void) output_length;
981 (void) output_size;
982
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200983 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200984#endif /* PSA_CRYPTO_DRIVER_PRESENT */
985}
986
987psa_status_t psa_driver_wrapper_cipher_finish(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200988 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200989 uint8_t *output,
990 size_t output_size,
991 size_t *output_length )
992{
993#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200994 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200995 {
996#if defined(PSA_CRYPTO_DRIVER_TEST)
997 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200998 return( test_transparent_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200999 output,
1000 output_size,
1001 output_length ) );
1002#endif /* PSA_CRYPTO_DRIVER_TEST */
1003#if defined(PSA_CRYPTO_DRIVER_TEST)
1004 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001005 return( test_opaque_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001006 output,
1007 output_size,
1008 output_length ) );
1009#endif /* PSA_CRYPTO_DRIVER_TEST */
1010 default:
1011 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001012 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001013 }
1014#else /* PSA_CRYPTO_DRIVER_PRESENT */
1015 (void) operation;
1016 (void) output;
1017 (void) output_size;
1018 (void) output_length;
1019
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001020 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001021#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1022}
1023
1024psa_status_t psa_driver_wrapper_cipher_abort(
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001025 psa_operation_driver_context_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001026{
1027#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1028 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001029
1030 /* The object has (apparently) been initialized but it is not in use. It's
1031 * ok to call abort on such an object, and there's nothing to do. */
1032 if( operation->ctx == NULL && operation->id == 0 )
1033 return( PSA_SUCCESS );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001034
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001035 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001036 {
1037#if defined(PSA_CRYPTO_DRIVER_TEST)
1038 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001039 status = test_transparent_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001040 mbedtls_platform_zeroize(
1041 operation->ctx,
1042 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001043 mbedtls_free( operation->ctx );
1044 operation->ctx = NULL;
1045 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001046
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001047 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001048#endif /* PSA_CRYPTO_DRIVER_TEST */
1049#if defined(PSA_CRYPTO_DRIVER_TEST)
1050 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001051 status = test_opaque_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001052 mbedtls_platform_zeroize(
1053 operation->ctx,
1054 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001055 mbedtls_free( operation->ctx );
1056 operation->ctx = NULL;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001057 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001058
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001059 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001060#endif /* PSA_CRYPTO_DRIVER_TEST */
1061 default:
1062 /* Operation is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001063 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001064 }
1065#else /* PSA_CRYPTO_DRIVER_PRESENT */
1066 (void)operation;
1067
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001068 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001069#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1070}
1071
Steven Cooreman1e582352021-02-18 17:24:37 +01001072/*
1073 * Hashing functions
1074 */
1075psa_status_t psa_driver_wrapper_hash_compute(
1076 psa_algorithm_t alg,
1077 const uint8_t *input,
1078 size_t input_length,
1079 uint8_t *hash,
1080 size_t hash_size,
1081 size_t *hash_length)
1082{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001083 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001084
1085 /* Try accelerators first */
Steven Cooremanf7638102021-03-04 15:14:36 +01001086#if defined(PSA_CRYPTO_DRIVER_TEST)
1087 status = test_transparent_hash_compute( alg, input, input_length,
1088 hash, hash_size, hash_length );
1089 if( status != PSA_ERROR_NOT_SUPPORTED )
1090 return( status );
1091#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001092
1093 /* If software fallback is compiled in, try fallback */
1094#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1095 status = mbedtls_psa_hash_compute( alg, input, input_length,
1096 hash, hash_size, hash_length );
1097 if( status != PSA_ERROR_NOT_SUPPORTED )
1098 return( status );
1099#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001100 (void) status;
1101 (void) alg;
1102 (void) input;
1103 (void) input_length;
1104 (void) hash;
1105 (void) hash_size;
1106 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001107
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001108 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman1e582352021-02-18 17:24:37 +01001109}
1110
1111psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001112 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001113 psa_algorithm_t alg )
1114{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001115 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001116
Steven Cooreman1e582352021-02-18 17:24:37 +01001117 /* Try setup on accelerators first */
Steven Cooremanf7638102021-03-04 15:14:36 +01001118#if defined(PSA_CRYPTO_DRIVER_TEST)
1119 status = test_transparent_hash_setup( &operation->ctx.test_ctx, alg );
1120 if( status == PSA_SUCCESS )
1121 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1122
1123 if( status != PSA_ERROR_NOT_SUPPORTED )
1124 return( status );
1125#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001126
1127 /* If software fallback is compiled in, try fallback */
1128#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001129 status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
Steven Cooreman1e582352021-02-18 17:24:37 +01001130 if( status == PSA_SUCCESS )
Steven Cooreman1e582352021-02-18 17:24:37 +01001131 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Steven Cooreman1e582352021-02-18 17:24:37 +01001132
1133 if( status != PSA_ERROR_NOT_SUPPORTED )
1134 return( status );
1135#endif
1136 /* Nothing left to try if we fall through here */
1137 (void) status;
1138 (void) operation;
1139 (void) alg;
1140 return( PSA_ERROR_NOT_SUPPORTED );
1141}
1142
1143psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001144 const psa_hash_operation_t *source_operation,
1145 psa_hash_operation_t *target_operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001146{
Steven Cooreman1e582352021-02-18 17:24:37 +01001147 switch( source_operation->id )
1148 {
Steven Cooremanf7638102021-03-04 15:14:36 +01001149#if defined(PSA_CRYPTO_DRIVER_TEST)
1150 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1151 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1152 return( test_transparent_hash_clone( &source_operation->ctx.test_ctx,
1153 &target_operation->ctx.test_ctx ) );
1154#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001155#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1156 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman1e582352021-02-18 17:24:37 +01001157 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001158 return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
1159 &target_operation->ctx.mbedtls_ctx ) );
Steven Cooreman1e582352021-02-18 17:24:37 +01001160#endif
1161 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001162 (void) source_operation;
1163 (void) target_operation;
1164 return( PSA_ERROR_BAD_STATE );
1165 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001166}
1167
1168psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001169 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001170 const uint8_t *input,
1171 size_t input_length )
1172{
Steven Cooreman1e582352021-02-18 17:24:37 +01001173 switch( operation->id )
1174 {
Steven Cooremanf7638102021-03-04 15:14:36 +01001175#if defined(PSA_CRYPTO_DRIVER_TEST)
1176 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1177 return( test_transparent_hash_update( &operation->ctx.test_ctx,
1178 input, input_length ) );
1179#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001180#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1181 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001182 return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
1183 input, input_length ) );
Steven Cooreman1e582352021-02-18 17:24:37 +01001184#endif
1185 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001186 (void) operation;
1187 (void) input;
1188 (void) input_length;
1189 return( PSA_ERROR_BAD_STATE );
1190 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001191}
1192
1193psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001194 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001195 uint8_t *hash,
1196 size_t hash_size,
1197 size_t *hash_length )
1198{
Steven Cooreman1e582352021-02-18 17:24:37 +01001199 switch( operation->id )
1200 {
Steven Cooremanf7638102021-03-04 15:14:36 +01001201#if defined(PSA_CRYPTO_DRIVER_TEST)
1202 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1203 return( test_transparent_hash_finish( &operation->ctx.test_ctx,
1204 hash, hash_size, hash_length ) );
1205#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001206#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1207 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001208 return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
1209 hash, hash_size, hash_length ) );
Steven Cooreman1e582352021-02-18 17:24:37 +01001210 break;
1211#endif
1212 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001213 (void) operation;
1214 (void) hash;
1215 (void) hash_size;
1216 (void) hash_length;
1217 return( PSA_ERROR_BAD_STATE );
1218 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001219}
1220
1221psa_status_t psa_driver_wrapper_hash_abort(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001222 psa_hash_operation_t *operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001223{
Steven Cooreman1e582352021-02-18 17:24:37 +01001224 switch( operation->id )
1225 {
Steven Cooremanf7638102021-03-04 15:14:36 +01001226#if defined(PSA_CRYPTO_DRIVER_TEST)
1227 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1228 return( test_transparent_hash_abort( &operation->ctx.test_ctx ) );
1229#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001230#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1231 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001232 return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
Steven Cooreman1e582352021-02-18 17:24:37 +01001233#endif
1234 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001235 return( PSA_ERROR_BAD_STATE );
1236 }
1237}
1238
Steven Cooremancd84cb42020-07-16 20:28:36 +02001239/* End of automatically generated file. */