blob: 89452dada6a089fa21009e533c8f119942d7f913 [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 Cooreman830aff22021-03-09 09:50:44 +010024#include "psa_crypto_hash.h"
25
Steven Cooreman2a1664c2020-07-20 15:33:08 +020026#include "mbedtls/platform.h"
27
28#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
Steven Cooremancd84cb42020-07-16 20:28:36 +020029
30/* Include test driver definition when running tests */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020031#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman56250fd2020-09-04 13:07:15 +020032#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020033#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020034#endif
35#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020036#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020037#endif
Steven Cooreman0d7c64d2020-09-07 16:17:55 +020038#include "test/drivers/test_driver.h"
Steven Cooremanf1720ea2020-07-24 18:41:58 +020039#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +020040
Steven Cooreman56250fd2020-09-04 13:07:15 +020041/* Repeat above block for each JSON-declared driver during autogeneration */
Steven Cooremanaa87fd02021-03-15 18:54:03 +010042#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Steven Cooreman56250fd2020-09-04 13:07:15 +020043
Steven Cooreman1e582352021-02-18 17:24:37 +010044/* Auto-generated values depending on which drivers are registered.
45 * ID 0 is reserved for unallocated operations.
46 * ID 1 is reserved for the Mbed TLS software driver. */
Steven Cooreman830aff22021-03-09 09:50:44 +010047#define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
48
Steven Cooreman37941cb2020-07-28 18:49:51 +020049#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman1e582352021-02-18 17:24:37 +010050#define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2)
51#define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3)
Steven Cooreman37941cb2020-07-28 18:49:51 +020052#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +020053
54/* Support the 'old' SE interface when asked to */
Steven Cooreman7a250572020-07-17 16:43:05 +020055#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman56250fd2020-09-04 13:07:15 +020056/* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
57 * SE driver is present, to avoid unused argument errors at compile time. */
58#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020059#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020060#endif
Steven Cooreman7a250572020-07-17 16:43:05 +020061#include "psa_crypto_se.h"
62#endif
63
Steven Cooremancd84cb42020-07-16 20:28:36 +020064/* Start delegation functions */
Ronald Cron9f17aa42020-12-08 17:07:25 +010065psa_status_t psa_driver_wrapper_sign_hash(
66 const psa_key_attributes_t *attributes,
67 const uint8_t *key_buffer, size_t key_buffer_size,
68 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
69 uint8_t *signature, size_t signature_size, size_t *signature_length )
Steven Cooremancd84cb42020-07-16 20:28:36 +020070{
Steven Cooreman7a250572020-07-17 16:43:05 +020071 /* Try dynamically-registered SE interface first */
72#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
73 const psa_drv_se_t *drv;
74 psa_drv_se_context_t *drv_context;
75
Ronald Cron9f17aa42020-12-08 17:07:25 +010076 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman7a250572020-07-17 16:43:05 +020077 {
78 if( drv->asymmetric == NULL ||
79 drv->asymmetric->p_sign == NULL )
80 {
81 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +020082 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman7a250572020-07-17 16:43:05 +020083 }
Ronald Cron9f17aa42020-12-08 17:07:25 +010084 return( drv->asymmetric->p_sign(
85 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
86 alg, hash, hash_length,
87 signature, signature_size, signature_length ) );
Steven Cooreman7a250572020-07-17 16:43:05 +020088 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +020089#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman7a250572020-07-17 16:43:05 +020090
Ronald Cronfce9df22020-12-08 18:06:03 +010091 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +010092 psa_key_location_t location =
93 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooremancd84cb42020-07-16 20:28:36 +020094
95 switch( location )
96 {
97 case PSA_KEY_LOCATION_LOCAL_STORAGE:
98 /* Key is stored in the slot in export representation, so
99 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100100#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200101#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron9f17aa42020-12-08 17:07:25 +0100102 status = test_transparent_signature_sign_hash( attributes,
103 key_buffer,
104 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200105 alg,
106 hash,
107 hash_length,
108 signature,
109 signature_size,
110 signature_length );
111 /* Declared with fallback == true */
112 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200113 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200114#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100115#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200116 /* Fell through, meaning no accelerator supports this operation */
Ronald Cronfce9df22020-12-08 18:06:03 +0100117 return( psa_sign_hash_internal( attributes,
118 key_buffer,
119 key_buffer_size,
120 alg,
121 hash,
122 hash_length,
123 signature,
124 signature_size,
125 signature_length ) );
126
Steven Cooremancd84cb42020-07-16 20:28:36 +0200127 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100128#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200129#if defined(PSA_CRYPTO_DRIVER_TEST)
130 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron9f17aa42020-12-08 17:07:25 +0100131 return( test_opaque_signature_sign_hash( attributes,
132 key_buffer,
133 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200134 alg,
135 hash,
136 hash_length,
137 signature,
138 signature_size,
139 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200140#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100141#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200142 default:
143 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100144 (void)status;
145 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200146 }
Steven Cooremancd84cb42020-07-16 20:28:36 +0200147}
148
Ronald Cron9f17aa42020-12-08 17:07:25 +0100149psa_status_t psa_driver_wrapper_verify_hash(
150 const psa_key_attributes_t *attributes,
151 const uint8_t *key_buffer, size_t key_buffer_size,
152 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
153 const uint8_t *signature, size_t signature_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200154{
Steven Cooreman55ae2172020-07-17 19:46:15 +0200155 /* Try dynamically-registered SE interface first */
156#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
157 const psa_drv_se_t *drv;
158 psa_drv_se_context_t *drv_context;
159
Ronald Cron9f17aa42020-12-08 17:07:25 +0100160 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200161 {
162 if( drv->asymmetric == NULL ||
163 drv->asymmetric->p_verify == NULL )
164 {
165 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200166 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200167 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100168 return( drv->asymmetric->p_verify(
169 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
170 alg, hash, hash_length,
171 signature, signature_length ) );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200172 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200173#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200174
Ronald Cronfce9df22020-12-08 18:06:03 +0100175 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100176 psa_key_location_t location =
177 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200178
179 switch( location )
180 {
181 case PSA_KEY_LOCATION_LOCAL_STORAGE:
182 /* Key is stored in the slot in export representation, so
183 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100184#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200185#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron9f17aa42020-12-08 17:07:25 +0100186 status = test_transparent_signature_verify_hash( attributes,
187 key_buffer,
188 key_buffer_size,
Steven Cooreman55ae2172020-07-17 19:46:15 +0200189 alg,
190 hash,
191 hash_length,
192 signature,
193 signature_length );
194 /* Declared with fallback == true */
195 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200196 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200197#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100198#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
199
200 return( psa_verify_hash_internal( attributes,
201 key_buffer,
202 key_buffer_size,
203 alg,
204 hash,
205 hash_length,
206 signature,
207 signature_length ) );
208
Steven Cooreman55ae2172020-07-17 19:46:15 +0200209 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100210#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200211#if defined(PSA_CRYPTO_DRIVER_TEST)
212 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron9f17aa42020-12-08 17:07:25 +0100213 return( test_opaque_signature_verify_hash( attributes,
214 key_buffer,
215 key_buffer_size,
Steven Cooreman55ae2172020-07-17 19:46:15 +0200216 alg,
217 hash,
218 hash_length,
219 signature,
220 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200221#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100222#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200223 default:
224 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100225 (void)status;
226 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200227 }
Steven Cooreman55ae2172020-07-17 19:46:15 +0200228}
229
Ronald Cron31216282020-12-05 18:47:56 +0100230/** Get the key buffer size for the key material of a generated key in the
231 * case of an opaque driver without storage.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200232 *
Ronald Cron31216282020-12-05 18:47:56 +0100233 * \param[in] attributes The key attributes.
234 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200235 *
236 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100237 * The minimum size for a buffer to contain the key material has been
238 * returned successfully.
239 * \retval #PSA_ERROR_INVALID_ARGUMENT
240 * The size in bits of the key is not valid.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200241 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100242 * The type and/or the size in bits of the key or the combination of
243 * the two is not supported.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200244 */
Ronald Cron9df74be2020-12-05 19:15:23 +0100245psa_status_t psa_driver_wrapper_get_key_buffer_size(
Ronald Cron31216282020-12-05 18:47:56 +0100246 const psa_key_attributes_t *attributes,
247 size_t *key_buffer_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200248{
John Durkop2c618352020-09-22 06:54:01 -0700249 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
250 psa_key_type_t key_type = attributes->core.type;
251 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200252
Ronald Cron31216282020-12-05 18:47:56 +0100253 *key_buffer_size = 0;
John Durkop2c618352020-09-22 06:54:01 -0700254 switch( location )
255 {
John Durkop2c618352020-09-22 06:54:01 -0700256#if defined(PSA_CRYPTO_DRIVER_TEST)
257 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
John Durkopac93e3b2020-10-16 06:48:55 -0700258#ifdef TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION
Ronald Cron31216282020-12-05 18:47:56 +0100259 *key_buffer_size = test_size_function( key_type, key_bits );
John Durkop2c618352020-09-22 06:54:01 -0700260 return( PSA_SUCCESS );
261#else /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
262 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) )
263 {
Ronald Cron31216282020-12-05 18:47:56 +0100264 int public_key_overhead =
265 ( ( TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 ) ?
266 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) : 0 );
267 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop135ce692020-10-19 07:12:28 -0700268 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE
269 + public_key_overhead;
270 }
Ronald Cron31216282020-12-05 18:47:56 +0100271 else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( key_type ) )
John Durkop135ce692020-10-19 07:12:28 -0700272 {
Ronald Cron31216282020-12-05 18:47:56 +0100273 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700274 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE;
275 }
John Durkop135ce692020-10-19 07:12:28 -0700276 else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) &&
Ronald Cron31216282020-12-05 18:47:56 +0100277 !PSA_KEY_TYPE_IS_PUBLIC_KEY ( key_type ) )
John Durkop2c618352020-09-22 06:54:01 -0700278 {
Ronald Cron31216282020-12-05 18:47:56 +0100279 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700280 + TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR
281 * ( ( key_bits + 7 ) / 8 );
282 }
283 else
284 {
285 return( PSA_ERROR_NOT_SUPPORTED );
286 }
287 return( PSA_SUCCESS );
288#endif /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
289#endif /* PSA_CRYPTO_DRIVER_TEST */
290
291 default:
Ronald Cron9df74be2020-12-05 19:15:23 +0100292 (void)key_type;
293 (void)key_bits;
Steven Cooreman56250fd2020-09-04 13:07:15 +0200294 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200295 }
296}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200297
Ronald Cron977c2472020-10-13 08:32:21 +0200298psa_status_t psa_driver_wrapper_generate_key(
299 const psa_key_attributes_t *attributes,
300 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200301{
Ronald Cron977c2472020-10-13 08:32:21 +0200302 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
303 psa_key_location_t location =
304 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
305
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200306 /* Try dynamically-registered SE interface first */
307#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
308 const psa_drv_se_t *drv;
309 psa_drv_se_context_t *drv_context;
310
Ronald Cron977c2472020-10-13 08:32:21 +0200311 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200312 {
313 size_t pubkey_length = 0; /* We don't support this feature yet */
314 if( drv->key_management == NULL ||
315 drv->key_management->p_generate == NULL )
316 {
317 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200318 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200319 }
320 return( drv->key_management->p_generate(
Ronald Cron977c2472020-10-13 08:32:21 +0200321 drv_context,
322 *( (psa_key_slot_number_t *)key_buffer ),
Ronald Cronea0f8a62020-11-25 17:52:23 +0100323 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200324 }
325#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
326
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200327 switch( location )
328 {
329 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Ronald Cron977c2472020-10-13 08:32:21 +0200330#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200331 /* Transparent drivers are limited to generating asymmetric keys */
Ronald Cron977c2472020-10-13 08:32:21 +0200332 if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200333 {
Ronald Cron977c2472020-10-13 08:32:21 +0200334 /* Cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200335#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron977c2472020-10-13 08:32:21 +0200336 status = test_transparent_generate_key(
337 attributes, key_buffer, key_buffer_size,
338 key_buffer_length );
339 /* Declared with fallback == true */
340 if( status != PSA_ERROR_NOT_SUPPORTED )
341 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200342#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200343 }
344#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
345
346 /* Software fallback */
347 status = psa_generate_key_internal(
348 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200349 break;
Ronald Cron977c2472020-10-13 08:32:21 +0200350
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200351 /* Add cases for opaque driver here */
Ronald Cron977c2472020-10-13 08:32:21 +0200352#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200353#if defined(PSA_CRYPTO_DRIVER_TEST)
354 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron977c2472020-10-13 08:32:21 +0200355 status = test_opaque_generate_key(
356 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200357 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200358#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200359#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
360
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200361 default:
362 /* Key is declared with a lifetime not known to us */
363 status = PSA_ERROR_INVALID_ARGUMENT;
364 break;
365 }
366
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200367 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200368}
369
Ronald Cron83282872020-11-22 14:02:39 +0100370psa_status_t psa_driver_wrapper_import_key(
371 const psa_key_attributes_t *attributes,
372 const uint8_t *data,
373 size_t data_length,
374 uint8_t *key_buffer,
375 size_t key_buffer_size,
376 size_t *key_buffer_length,
377 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200378{
Steven Cooreman04524762020-10-13 17:43:44 +0200379 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100380 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
381 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200382
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100383 /* Try dynamically-registered SE interface first */
384#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
385 const psa_drv_se_t *drv;
386 psa_drv_se_context_t *drv_context;
387
388 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
389 {
390 if( drv->key_management == NULL ||
391 drv->key_management->p_import == NULL )
392 return( PSA_ERROR_NOT_SUPPORTED );
393
394 /* The driver should set the number of key bits, however in
395 * case it doesn't, we initialize bits to an invalid value. */
396 *bits = PSA_MAX_KEY_BITS + 1;
397 status = drv->key_management->p_import(
398 drv_context,
399 *( (psa_key_slot_number_t *)key_buffer ),
400 attributes, data, data_length, bits );
401
402 if( status != PSA_SUCCESS )
403 return( status );
404
405 if( (*bits) > PSA_MAX_KEY_BITS )
406 return( PSA_ERROR_NOT_SUPPORTED );
407
408 return( PSA_SUCCESS );
409 }
410#endif /* PSA_CRYPTO_SE_C */
411
Ronald Cronbf33c932020-11-28 18:06:53 +0100412 switch( location )
413 {
414 case PSA_KEY_LOCATION_LOCAL_STORAGE:
415 /* Key is stored in the slot in export representation, so
416 * cycle through all known transparent accelerators */
417#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
418#if defined(PSA_CRYPTO_DRIVER_TEST)
419 status = test_transparent_import_key( attributes,
420 data, data_length,
421 key_buffer, key_buffer_size,
422 key_buffer_length, bits );
423 /* Declared with fallback == true */
424 if( status != PSA_ERROR_NOT_SUPPORTED )
425 return( status );
426#endif /* PSA_CRYPTO_DRIVER_TEST */
427#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
428 /* Fell through, meaning no accelerator supports this operation */
429 return( psa_import_key_into_slot( attributes,
430 data, data_length,
431 key_buffer, key_buffer_size,
432 key_buffer_length, bits ) );
433
434 default:
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100435 /* Importing a key with external storage in not yet supported.
436 * Return in error indicating that the lifetime is not valid. */
Ronald Cronbf33c932020-11-28 18:06:53 +0100437 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100438 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100439 }
440
Steven Cooreman04524762020-10-13 17:43:44 +0200441}
442
Ronald Cron67227982020-11-26 15:16:05 +0100443psa_status_t psa_driver_wrapper_export_key(
444 const psa_key_attributes_t *attributes,
445 const uint8_t *key_buffer, size_t key_buffer_size,
446 uint8_t *data, size_t data_size, size_t *data_length )
447
448{
449 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
450 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
451 psa_get_key_lifetime( attributes ) );
452
Ronald Cron152e3f82020-11-26 16:06:41 +0100453 /* Try dynamically-registered SE interface first */
454#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
455 const psa_drv_se_t *drv;
456 psa_drv_se_context_t *drv_context;
457
458 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
459 {
460 if( ( drv->key_management == NULL ) ||
461 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100462 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100463 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100464 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100465
466 return( drv->key_management->p_export(
467 drv_context,
468 *( (psa_key_slot_number_t *)key_buffer ),
469 data, data_size, data_length ) );
470 }
471#endif /* PSA_CRYPTO_SE_C */
472
Ronald Cron67227982020-11-26 15:16:05 +0100473 switch( location )
474 {
475 case PSA_KEY_LOCATION_LOCAL_STORAGE:
476 return( psa_export_key_internal( attributes,
477 key_buffer,
478 key_buffer_size,
479 data,
480 data_size,
481 data_length ) );
482
483 /* Add cases for opaque driver here */
484#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
485#if defined(PSA_CRYPTO_DRIVER_TEST)
486 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
487 return( test_opaque_export_key( attributes,
488 key_buffer,
489 key_buffer_size,
490 data,
491 data_size,
492 data_length ) );
493#endif /* PSA_CRYPTO_DRIVER_TEST */
494#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
495 default:
496 /* Key is declared with a lifetime not known to us */
497 return( status );
498 }
499}
500
Ronald Cron84cc9942020-11-25 14:30:05 +0100501psa_status_t psa_driver_wrapper_export_public_key(
502 const psa_key_attributes_t *attributes,
503 const uint8_t *key_buffer, size_t key_buffer_size,
504 uint8_t *data, size_t data_size, size_t *data_length )
505
Steven Cooremanb9b84422020-10-14 14:39:20 +0200506{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200507 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100508 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
509 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200510
Ronald Cron152e3f82020-11-26 16:06:41 +0100511 /* Try dynamically-registered SE interface first */
512#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
513 const psa_drv_se_t *drv;
514 psa_drv_se_context_t *drv_context;
515
516 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
517 {
518 if( ( drv->key_management == NULL ) ||
519 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100520 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100521 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100522 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100523
524 return( drv->key_management->p_export_public(
525 drv_context,
526 *( (psa_key_slot_number_t *)key_buffer ),
527 data, data_size, data_length ) );
528 }
529#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
530
Steven Cooremanb9b84422020-10-14 14:39:20 +0200531 switch( location )
532 {
533 case PSA_KEY_LOCATION_LOCAL_STORAGE:
534 /* Key is stored in the slot in export representation, so
535 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100536#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200537#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron84cc9942020-11-25 14:30:05 +0100538 status = test_transparent_export_public_key( attributes,
539 key_buffer,
540 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200541 data,
542 data_size,
543 data_length );
544 /* Declared with fallback == true */
545 if( status != PSA_ERROR_NOT_SUPPORTED )
546 return( status );
547#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100548#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200549 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100550 return( psa_export_public_key_internal( attributes,
551 key_buffer,
552 key_buffer_size,
553 data,
554 data_size,
555 data_length ) );
556
Steven Cooremanb9b84422020-10-14 14:39:20 +0200557 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100558#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200559#if defined(PSA_CRYPTO_DRIVER_TEST)
560 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron84cc9942020-11-25 14:30:05 +0100561 return( test_opaque_export_public_key( attributes,
562 key_buffer,
563 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200564 data,
565 data_size,
566 data_length ) );
567#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100568#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200569 default:
570 /* Key is declared with a lifetime not known to us */
571 return( status );
572 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200573}
574
Steven Cooreman37941cb2020-07-28 18:49:51 +0200575/*
576 * Cipher functions
577 */
578psa_status_t psa_driver_wrapper_cipher_encrypt(
579 psa_key_slot_t *slot,
580 psa_algorithm_t alg,
581 const uint8_t *input,
582 size_t input_length,
583 uint8_t *output,
584 size_t output_size,
585 size_t *output_length )
586{
587#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
588 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
589 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
590 psa_key_attributes_t attributes = {
591 .core = slot->attr
592 };
593
594 switch( location )
595 {
596 case PSA_KEY_LOCATION_LOCAL_STORAGE:
597 /* Key is stored in the slot in export representation, so
598 * cycle through all known transparent accelerators */
599#if defined(PSA_CRYPTO_DRIVER_TEST)
600 status = test_transparent_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100601 slot->key.data,
602 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200603 alg,
604 input,
605 input_length,
606 output,
607 output_size,
608 output_length );
609 /* Declared with fallback == true */
610 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200611 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200612#endif /* PSA_CRYPTO_DRIVER_TEST */
613 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200614 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200615 /* Add cases for opaque driver here */
616#if defined(PSA_CRYPTO_DRIVER_TEST)
617 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
618 return( test_opaque_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100619 slot->key.data,
620 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200621 alg,
622 input,
623 input_length,
624 output,
625 output_size,
626 output_length ) );
627#endif /* PSA_CRYPTO_DRIVER_TEST */
628 default:
629 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200630 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200631 }
632#else /* PSA_CRYPTO_DRIVER_PRESENT */
633 (void) slot;
634 (void) alg;
635 (void) input;
636 (void) input_length;
637 (void) output;
638 (void) output_size;
639 (void) output_length;
640
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200641 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200642#endif /* PSA_CRYPTO_DRIVER_PRESENT */
643}
644
645psa_status_t psa_driver_wrapper_cipher_decrypt(
646 psa_key_slot_t *slot,
647 psa_algorithm_t alg,
648 const uint8_t *input,
649 size_t input_length,
650 uint8_t *output,
651 size_t output_size,
652 size_t *output_length )
653{
654#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
655 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
656 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
657 psa_key_attributes_t attributes = {
658 .core = slot->attr
659 };
660
661 switch( location )
662 {
663 case PSA_KEY_LOCATION_LOCAL_STORAGE:
664 /* Key is stored in the slot in export representation, so
665 * cycle through all known transparent accelerators */
666#if defined(PSA_CRYPTO_DRIVER_TEST)
667 status = test_transparent_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100668 slot->key.data,
669 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200670 alg,
671 input,
672 input_length,
673 output,
674 output_size,
675 output_length );
676 /* Declared with fallback == true */
677 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200678 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200679#endif /* PSA_CRYPTO_DRIVER_TEST */
680 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200681 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200682 /* Add cases for opaque driver here */
683#if defined(PSA_CRYPTO_DRIVER_TEST)
684 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
685 return( test_opaque_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100686 slot->key.data,
687 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200688 alg,
689 input,
690 input_length,
691 output,
692 output_size,
693 output_length ) );
694#endif /* PSA_CRYPTO_DRIVER_TEST */
695 default:
696 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200697 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200698 }
699#else /* PSA_CRYPTO_DRIVER_PRESENT */
700 (void) slot;
701 (void) alg;
702 (void) input;
703 (void) input_length;
704 (void) output;
705 (void) output_size;
706 (void) output_length;
707
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200708 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200709#endif /* PSA_CRYPTO_DRIVER_PRESENT */
710}
711
712psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200713 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200714 psa_key_slot_t *slot,
715 psa_algorithm_t alg )
716{
717#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
718 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
719 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
720 psa_key_attributes_t attributes = {
721 .core = slot->attr
722 };
723
Steven Cooreman37941cb2020-07-28 18:49:51 +0200724 switch( location )
725 {
726 case PSA_KEY_LOCATION_LOCAL_STORAGE:
727 /* Key is stored in the slot in export representation, so
728 * cycle through all known transparent accelerators */
729#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200730 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
731 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200732 return PSA_ERROR_INSUFFICIENT_MEMORY;
733
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200734 status = test_transparent_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200735 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100736 slot->key.data,
737 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200738 alg );
739 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200740 if( status == PSA_SUCCESS )
741 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200742 else
743 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200744 mbedtls_platform_zeroize(
745 operation->ctx,
746 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200747 mbedtls_free( operation->ctx );
748 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200749 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200750
751 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200752#endif /* PSA_CRYPTO_DRIVER_TEST */
753 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200754 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200755 /* Add cases for opaque driver here */
756#if defined(PSA_CRYPTO_DRIVER_TEST)
757 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200758 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
759 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200760 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200761
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200762 status = test_opaque_cipher_encrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200763 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100764 slot->key.data,
765 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200766 alg );
767 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200768 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200769 else
770 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200771 mbedtls_platform_zeroize(
772 operation->ctx,
773 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200774 mbedtls_free( operation->ctx );
775 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200776 }
777
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200778 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200779#endif /* PSA_CRYPTO_DRIVER_TEST */
780 default:
781 /* Key is declared with a lifetime not known to us */
John Durkop714e3a12020-09-29 22:07:04 -0700782 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200783 }
784#else /* PSA_CRYPTO_DRIVER_PRESENT */
785 (void)slot;
786 (void)alg;
787 (void)operation;
788
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200789 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200790#endif /* PSA_CRYPTO_DRIVER_PRESENT */
791}
792
793psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200794 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200795 psa_key_slot_t *slot,
796 psa_algorithm_t alg )
797{
798#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
799 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
800 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
801 psa_key_attributes_t attributes = {
802 .core = slot->attr
803 };
804
Steven Cooreman37941cb2020-07-28 18:49:51 +0200805 switch( location )
806 {
807 case PSA_KEY_LOCATION_LOCAL_STORAGE:
808 /* Key is stored in the slot in export representation, so
809 * cycle through all known transparent accelerators */
810#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200811 operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) );
812 if( operation->ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200813 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200814
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200815 status = test_transparent_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200816 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100817 slot->key.data,
818 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200819 alg );
820 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200821 if( status == PSA_SUCCESS )
822 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200823 else
824 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200825 mbedtls_platform_zeroize(
826 operation->ctx,
827 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200828 mbedtls_free( operation->ctx );
829 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200830 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200831
832 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200833#endif /* PSA_CRYPTO_DRIVER_TEST */
834 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200835 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200836 /* Add cases for opaque driver here */
837#if defined(PSA_CRYPTO_DRIVER_TEST)
838 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200839 operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
840 if( operation->ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200841 return PSA_ERROR_INSUFFICIENT_MEMORY;
842
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200843 status = test_opaque_cipher_decrypt_setup( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200844 &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100845 slot->key.data,
846 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200847 alg );
848 if( status == PSA_SUCCESS )
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200849 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200850 else
851 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200852 mbedtls_platform_zeroize(
853 operation->ctx,
854 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200855 mbedtls_free( operation->ctx );
856 operation->ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200857 }
858
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200859 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200860#endif /* PSA_CRYPTO_DRIVER_TEST */
861 default:
862 /* Key is declared with a lifetime not known to us */
John Durkop814dca72020-10-05 06:31:12 -0700863 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200864 }
865#else /* PSA_CRYPTO_DRIVER_PRESENT */
866 (void)slot;
867 (void)alg;
868 (void)operation;
869
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200870 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200871#endif /* PSA_CRYPTO_DRIVER_PRESENT */
872}
873
874psa_status_t psa_driver_wrapper_cipher_generate_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200875 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200876 uint8_t *iv,
877 size_t iv_size,
878 size_t *iv_length )
879{
880#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200881 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200882 {
883#if defined(PSA_CRYPTO_DRIVER_TEST)
884 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200885 return( test_transparent_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200886 iv,
887 iv_size,
888 iv_length ) );
889#endif /* PSA_CRYPTO_DRIVER_TEST */
890#if defined(PSA_CRYPTO_DRIVER_TEST)
891 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200892 return( test_opaque_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200893 iv,
894 iv_size,
895 iv_length ) );
896#endif /* PSA_CRYPTO_DRIVER_TEST */
897 default:
898 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200899 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200900 }
901#else /* PSA_CRYPTO_DRIVER_PRESENT */
902 (void) operation;
903 (void) iv;
904 (void) iv_size;
905 (void) iv_length;
906
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200907 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200908#endif /* PSA_CRYPTO_DRIVER_PRESENT */
909}
910
911psa_status_t psa_driver_wrapper_cipher_set_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200912 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200913 const uint8_t *iv,
914 size_t iv_length )
915{
916#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200917 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200918 {
919#if defined(PSA_CRYPTO_DRIVER_TEST)
920 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200921 return( test_transparent_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200922 iv,
923 iv_length ) );
924#endif /* PSA_CRYPTO_DRIVER_TEST */
925#if defined(PSA_CRYPTO_DRIVER_TEST)
926 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200927 return( test_opaque_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200928 iv,
929 iv_length ) );
930#endif /* PSA_CRYPTO_DRIVER_TEST */
931 default:
932 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200933 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200934 }
935#else /* PSA_CRYPTO_DRIVER_PRESENT */
936 (void) operation;
937 (void) iv;
938 (void) iv_length;
939
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200940 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200941#endif /* PSA_CRYPTO_DRIVER_PRESENT */
942}
943
944psa_status_t psa_driver_wrapper_cipher_update(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200945 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200946 const uint8_t *input,
947 size_t input_length,
948 uint8_t *output,
949 size_t output_size,
950 size_t *output_length )
951{
952#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200953 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200954 {
955#if defined(PSA_CRYPTO_DRIVER_TEST)
956 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200957 return( test_transparent_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200958 input,
959 input_length,
960 output,
961 output_size,
962 output_length ) );
963#endif /* PSA_CRYPTO_DRIVER_TEST */
964#if defined(PSA_CRYPTO_DRIVER_TEST)
965 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200966 return( test_opaque_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200967 input,
968 input_length,
969 output,
970 output_size,
971 output_length ) );
972#endif /* PSA_CRYPTO_DRIVER_TEST */
973 default:
974 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200975 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200976 }
977#else /* PSA_CRYPTO_DRIVER_PRESENT */
978 (void) operation;
979 (void) input;
980 (void) input_length;
981 (void) output;
982 (void) output_length;
983 (void) output_size;
984
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200985 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200986#endif /* PSA_CRYPTO_DRIVER_PRESENT */
987}
988
989psa_status_t psa_driver_wrapper_cipher_finish(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200990 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200991 uint8_t *output,
992 size_t output_size,
993 size_t *output_length )
994{
995#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200996 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200997 {
998#if defined(PSA_CRYPTO_DRIVER_TEST)
999 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001000 return( test_transparent_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001001 output,
1002 output_size,
1003 output_length ) );
1004#endif /* PSA_CRYPTO_DRIVER_TEST */
1005#if defined(PSA_CRYPTO_DRIVER_TEST)
1006 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001007 return( test_opaque_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001008 output,
1009 output_size,
1010 output_length ) );
1011#endif /* PSA_CRYPTO_DRIVER_TEST */
1012 default:
1013 /* Key is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001014 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001015 }
1016#else /* PSA_CRYPTO_DRIVER_PRESENT */
1017 (void) operation;
1018 (void) output;
1019 (void) output_size;
1020 (void) output_length;
1021
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001022 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001023#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1024}
1025
1026psa_status_t psa_driver_wrapper_cipher_abort(
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001027 psa_operation_driver_context_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001028{
1029#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1030 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001031
1032 /* The object has (apparently) been initialized but it is not in use. It's
1033 * ok to call abort on such an object, and there's nothing to do. */
1034 if( operation->ctx == NULL && operation->id == 0 )
1035 return( PSA_SUCCESS );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001036
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001037 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001038 {
1039#if defined(PSA_CRYPTO_DRIVER_TEST)
1040 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001041 status = test_transparent_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001042 mbedtls_platform_zeroize(
1043 operation->ctx,
1044 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001045 mbedtls_free( operation->ctx );
1046 operation->ctx = NULL;
1047 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001048
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001049 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001050#endif /* PSA_CRYPTO_DRIVER_TEST */
1051#if defined(PSA_CRYPTO_DRIVER_TEST)
1052 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001053 status = test_opaque_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001054 mbedtls_platform_zeroize(
1055 operation->ctx,
1056 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001057 mbedtls_free( operation->ctx );
1058 operation->ctx = NULL;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001059 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001060
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001061 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001062#endif /* PSA_CRYPTO_DRIVER_TEST */
1063 default:
1064 /* Operation is attached to a driver not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001065 return( PSA_ERROR_BAD_STATE );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001066 }
1067#else /* PSA_CRYPTO_DRIVER_PRESENT */
1068 (void)operation;
1069
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001070 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001071#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1072}
1073
Steven Cooreman1e582352021-02-18 17:24:37 +01001074/*
1075 * Hashing functions
1076 */
1077psa_status_t psa_driver_wrapper_hash_compute(
1078 psa_algorithm_t alg,
1079 const uint8_t *input,
1080 size_t input_length,
1081 uint8_t *hash,
1082 size_t hash_size,
1083 size_t *hash_length)
1084{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001085 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001086
1087 /* Try accelerators first */
Steven Cooremana85e2f82021-03-15 11:00:12 +01001088#if defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_HASH)
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001089 status = mbedtls_transparent_test_driver_hash_compute(
1090 alg, input, input_length, hash, hash_size, hash_length );
Steven Cooremanf7638102021-03-04 15:14:36 +01001091 if( status != PSA_ERROR_NOT_SUPPORTED )
1092 return( status );
1093#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001094
1095 /* If software fallback is compiled in, try fallback */
1096#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1097 status = mbedtls_psa_hash_compute( alg, input, input_length,
1098 hash, hash_size, hash_length );
1099 if( status != PSA_ERROR_NOT_SUPPORTED )
1100 return( status );
1101#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001102 (void) status;
1103 (void) alg;
1104 (void) input;
1105 (void) input_length;
1106 (void) hash;
1107 (void) hash_size;
1108 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001109
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001110 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman1e582352021-02-18 17:24:37 +01001111}
1112
1113psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001114 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001115 psa_algorithm_t alg )
1116{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001117 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001118
Steven Cooreman1e582352021-02-18 17:24:37 +01001119 /* Try setup on accelerators first */
Steven Cooremana85e2f82021-03-15 11:00:12 +01001120#if defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_HASH)
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001121 status = mbedtls_transparent_test_driver_hash_setup(
1122 &operation->ctx.test_driver_ctx, alg );
Steven Cooremanf7638102021-03-04 15:14:36 +01001123 if( status == PSA_SUCCESS )
1124 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1125
1126 if( status != PSA_ERROR_NOT_SUPPORTED )
1127 return( status );
1128#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001129
1130 /* If software fallback is compiled in, try fallback */
1131#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001132 status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
Steven Cooreman1e582352021-02-18 17:24:37 +01001133 if( status == PSA_SUCCESS )
Steven Cooreman1e582352021-02-18 17:24:37 +01001134 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Steven Cooreman1e582352021-02-18 17:24:37 +01001135
1136 if( status != PSA_ERROR_NOT_SUPPORTED )
1137 return( status );
1138#endif
1139 /* Nothing left to try if we fall through here */
1140 (void) status;
1141 (void) operation;
1142 (void) alg;
1143 return( PSA_ERROR_NOT_SUPPORTED );
1144}
1145
1146psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001147 const psa_hash_operation_t *source_operation,
1148 psa_hash_operation_t *target_operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001149{
Steven Cooreman1e582352021-02-18 17:24:37 +01001150 switch( source_operation->id )
1151 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001152#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1153 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1154 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1155 return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
1156 &target_operation->ctx.mbedtls_ctx ) );
1157#endif
Steven Cooremana85e2f82021-03-15 11:00:12 +01001158#if defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_HASH)
Steven Cooremanf7638102021-03-04 15:14:36 +01001159 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1160 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001161 return( mbedtls_transparent_test_driver_hash_clone(
1162 &source_operation->ctx.test_driver_ctx,
1163 &target_operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001164#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001165 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001166 (void) target_operation;
1167 return( PSA_ERROR_BAD_STATE );
1168 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001169}
1170
1171psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001172 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001173 const uint8_t *input,
1174 size_t input_length )
1175{
Steven Cooreman1e582352021-02-18 17:24:37 +01001176 switch( operation->id )
1177 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001178#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1179 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1180 return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
1181 input, input_length ) );
1182#endif
Steven Cooremana85e2f82021-03-15 11:00:12 +01001183#if defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_HASH)
Steven Cooremanf7638102021-03-04 15:14:36 +01001184 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001185 return( mbedtls_transparent_test_driver_hash_update(
1186 &operation->ctx.test_driver_ctx,
1187 input, input_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001188#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001189 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001190 (void) input;
1191 (void) input_length;
1192 return( PSA_ERROR_BAD_STATE );
1193 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001194}
1195
1196psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001197 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001198 uint8_t *hash,
1199 size_t hash_size,
1200 size_t *hash_length )
1201{
Steven Cooreman1e582352021-02-18 17:24:37 +01001202 switch( operation->id )
1203 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001204#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1205 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1206 return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
1207 hash, hash_size, hash_length ) );
1208#endif
Steven Cooremana85e2f82021-03-15 11:00:12 +01001209#if defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_HASH)
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_finish(
1212 &operation->ctx.test_driver_ctx,
1213 hash, hash_size, hash_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001214#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001215 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001216 (void) hash;
1217 (void) hash_size;
1218 (void) hash_length;
1219 return( PSA_ERROR_BAD_STATE );
1220 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001221}
1222
1223psa_status_t psa_driver_wrapper_hash_abort(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001224 psa_hash_operation_t *operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001225{
Steven Cooreman1e582352021-02-18 17:24:37 +01001226 switch( operation->id )
1227 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001228#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1229 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1230 return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
1231#endif
Steven Cooremana85e2f82021-03-15 11:00:12 +01001232#if defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_HASH)
Steven Cooremanf7638102021-03-04 15:14:36 +01001233 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001234 return( mbedtls_transparent_test_driver_hash_abort(
1235 &operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001236#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001237 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001238 return( PSA_ERROR_BAD_STATE );
1239 }
1240}
1241
Steven Cooremancd84cb42020-07-16 20:28:36 +02001242/* End of automatically generated file. */