blob: 0c55463245962dce26d0d7ef03116a46f4def4a0 [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(
Ronald Crona4af55f2020-12-14 14:36:06 +0100713 psa_cipher_operation_t *operation,
714 const psa_key_attributes_t *attributes,
715 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200716 psa_algorithm_t alg )
717{
718#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
719 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crona4af55f2020-12-14 14:36:06 +0100720 psa_key_location_t location =
721 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
722 void *driver_ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200723
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)
Ronald Crona4af55f2020-12-14 14:36:06 +0100730 driver_ctx = mbedtls_calloc( 1,
731 sizeof( test_transparent_cipher_operation_t ) );
732 if( driver_ctx == NULL )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200733 return PSA_ERROR_INSUFFICIENT_MEMORY;
734
Ronald Crona4af55f2020-12-14 14:36:06 +0100735 status = test_transparent_cipher_encrypt_setup( driver_ctx,
736 attributes,
737 key_buffer,
738 key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200739 alg );
740 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200741 if( status == PSA_SUCCESS )
Ronald Crona4af55f2020-12-14 14:36:06 +0100742 {
743 operation->ctx.driver.id =
744 PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
745 operation->ctx.driver.ctx = driver_ctx;
746 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200747 else
748 {
Ronald Crona4af55f2020-12-14 14:36:06 +0100749 mbedtls_platform_zeroize( driver_ctx,
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200750 sizeof( test_transparent_cipher_operation_t ) );
Ronald Crona4af55f2020-12-14 14:36:06 +0100751 mbedtls_free( driver_ctx );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200752 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200753
754 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200755#endif /* PSA_CRYPTO_DRIVER_TEST */
756 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200757 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200758 /* Add cases for opaque driver here */
759#if defined(PSA_CRYPTO_DRIVER_TEST)
760 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Crona4af55f2020-12-14 14:36:06 +0100761 driver_ctx =
762 mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
763 if( driver_ctx == NULL )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200764 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200765
Ronald Crona4af55f2020-12-14 14:36:06 +0100766 status = test_opaque_cipher_encrypt_setup( driver_ctx,
767 attributes,
768 key_buffer,
769 key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200770 alg );
771 if( status == PSA_SUCCESS )
Ronald Crona4af55f2020-12-14 14:36:06 +0100772 {
773 operation->ctx.driver.id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
774 operation->ctx.driver.ctx = driver_ctx;
775 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200776 else
777 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200778 mbedtls_platform_zeroize(
Ronald Crona4af55f2020-12-14 14:36:06 +0100779 driver_ctx, sizeof( test_opaque_cipher_operation_t ) );
780 mbedtls_free( driver_ctx );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200781 }
782
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200783 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200784#endif /* PSA_CRYPTO_DRIVER_TEST */
785 default:
786 /* Key is declared with a lifetime not known to us */
Ronald Cronc45b4af2020-09-29 16:18:05 +0200787 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200788 }
789#else /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +0200790 (void)operation;
Ronald Crona4af55f2020-12-14 14:36:06 +0100791 (void)attributes;
792 (void)key_buffer;
793 (void)key_buffer_size;
794 (void)alg;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200795
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200796 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200797#endif /* PSA_CRYPTO_DRIVER_PRESENT */
798}
799
800psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100801 psa_cipher_operation_t *operation,
802 const psa_key_attributes_t *attributes,
803 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200804 psa_algorithm_t alg )
805{
806#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
807 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crona4af55f2020-12-14 14:36:06 +0100808 psa_key_location_t location =
809 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
810 void *driver_ctx = NULL;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200811
Steven Cooreman37941cb2020-07-28 18:49:51 +0200812 switch( location )
813 {
814 case PSA_KEY_LOCATION_LOCAL_STORAGE:
815 /* Key is stored in the slot in export representation, so
816 * cycle through all known transparent accelerators */
817#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Crona4af55f2020-12-14 14:36:06 +0100818 driver_ctx = mbedtls_calloc( 1,
819 sizeof( test_transparent_cipher_operation_t ) );
820 if( driver_ctx == NULL )
821 return PSA_ERROR_INSUFFICIENT_MEMORY;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200822
Ronald Crona4af55f2020-12-14 14:36:06 +0100823 status = test_transparent_cipher_decrypt_setup( driver_ctx,
824 attributes,
825 key_buffer,
826 key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200827 alg );
828 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200829 if( status == PSA_SUCCESS )
Ronald Crona4af55f2020-12-14 14:36:06 +0100830 {
831 operation->ctx.driver.id =
832 PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
833 operation->ctx.driver.ctx = driver_ctx;
834 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200835 else
836 {
Ronald Crona4af55f2020-12-14 14:36:06 +0100837 mbedtls_platform_zeroize( driver_ctx,
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200838 sizeof( test_transparent_cipher_operation_t ) );
Ronald Crona4af55f2020-12-14 14:36:06 +0100839 mbedtls_free( driver_ctx );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200840 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200841
842 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200843#endif /* PSA_CRYPTO_DRIVER_TEST */
844 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200845 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200846 /* Add cases for opaque driver here */
847#if defined(PSA_CRYPTO_DRIVER_TEST)
848 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Crona4af55f2020-12-14 14:36:06 +0100849 driver_ctx =
850 mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) );
851 if( driver_ctx == NULL )
852 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200853
Ronald Crona4af55f2020-12-14 14:36:06 +0100854 status = test_opaque_cipher_decrypt_setup( driver_ctx,
855 attributes,
856 key_buffer,
857 key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200858 alg );
859 if( status == PSA_SUCCESS )
Ronald Crona4af55f2020-12-14 14:36:06 +0100860 {
861 operation->ctx.driver.id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
862 operation->ctx.driver.ctx = driver_ctx;
863 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200864 else
865 {
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200866 mbedtls_platform_zeroize(
Ronald Crona4af55f2020-12-14 14:36:06 +0100867 driver_ctx, sizeof( test_opaque_cipher_operation_t ) );
868 mbedtls_free( driver_ctx );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200869 }
870
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200871 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200872#endif /* PSA_CRYPTO_DRIVER_TEST */
873 default:
874 /* Key is declared with a lifetime not known to us */
Ronald Cronc45b4af2020-09-29 16:18:05 +0200875 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200876 }
877#else /* PSA_CRYPTO_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +0200878 (void)operation;
Ronald Crona4af55f2020-12-14 14:36:06 +0100879 (void)attributes;
880 (void)key_buffer;
881 (void)key_buffer_size;
882 (void)alg;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200883
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200884 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200885#endif /* PSA_CRYPTO_DRIVER_PRESENT */
886}
887
888psa_status_t psa_driver_wrapper_cipher_generate_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200889 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200890 uint8_t *iv,
891 size_t iv_size,
892 size_t *iv_length )
893{
894#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200895 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200896 {
897#if defined(PSA_CRYPTO_DRIVER_TEST)
898 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200899 return( test_transparent_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200900 iv,
901 iv_size,
902 iv_length ) );
903#endif /* PSA_CRYPTO_DRIVER_TEST */
904#if defined(PSA_CRYPTO_DRIVER_TEST)
905 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200906 return( test_opaque_cipher_generate_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200907 iv,
908 iv_size,
909 iv_length ) );
910#endif /* PSA_CRYPTO_DRIVER_TEST */
911 default:
912 /* Key is attached to a driver not known to us */
Ronald Cronc45b4af2020-09-29 16:18:05 +0200913 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200914 }
915#else /* PSA_CRYPTO_DRIVER_PRESENT */
916 (void) operation;
917 (void) iv;
918 (void) iv_size;
919 (void) iv_length;
920
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200921 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200922#endif /* PSA_CRYPTO_DRIVER_PRESENT */
923}
924
925psa_status_t psa_driver_wrapper_cipher_set_iv(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200926 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200927 const uint8_t *iv,
928 size_t iv_length )
929{
930#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200931 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200932 {
933#if defined(PSA_CRYPTO_DRIVER_TEST)
934 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200935 return( test_transparent_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200936 iv,
937 iv_length ) );
938#endif /* PSA_CRYPTO_DRIVER_TEST */
939#if defined(PSA_CRYPTO_DRIVER_TEST)
940 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200941 return( test_opaque_cipher_set_iv( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200942 iv,
943 iv_length ) );
944#endif /* PSA_CRYPTO_DRIVER_TEST */
945 default:
946 /* Key is attached to a driver not known to us */
Ronald Cronc45b4af2020-09-29 16:18:05 +0200947 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200948 }
949#else /* PSA_CRYPTO_DRIVER_PRESENT */
950 (void) operation;
951 (void) iv;
952 (void) iv_length;
953
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200954 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200955#endif /* PSA_CRYPTO_DRIVER_PRESENT */
956}
957
958psa_status_t psa_driver_wrapper_cipher_update(
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200959 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200960 const uint8_t *input,
961 size_t input_length,
962 uint8_t *output,
963 size_t output_size,
964 size_t *output_length )
965{
966#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200967 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200968 {
969#if defined(PSA_CRYPTO_DRIVER_TEST)
970 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200971 return( test_transparent_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200972 input,
973 input_length,
974 output,
975 output_size,
976 output_length ) );
977#endif /* PSA_CRYPTO_DRIVER_TEST */
978#if defined(PSA_CRYPTO_DRIVER_TEST)
979 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +0200980 return( test_opaque_cipher_update( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200981 input,
982 input_length,
983 output,
984 output_size,
985 output_length ) );
986#endif /* PSA_CRYPTO_DRIVER_TEST */
987 default:
988 /* Key is attached to a driver not known to us */
Ronald Cronc45b4af2020-09-29 16:18:05 +0200989 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200990 }
991#else /* PSA_CRYPTO_DRIVER_PRESENT */
992 (void) operation;
993 (void) input;
994 (void) input_length;
995 (void) output;
996 (void) output_length;
997 (void) output_size;
998
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200999 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001000#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1001}
1002
1003psa_status_t psa_driver_wrapper_cipher_finish(
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001004 psa_operation_driver_context_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001005 uint8_t *output,
1006 size_t output_size,
1007 size_t *output_length )
1008{
1009#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001010 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001011 {
1012#if defined(PSA_CRYPTO_DRIVER_TEST)
1013 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001014 return( test_transparent_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001015 output,
1016 output_size,
1017 output_length ) );
1018#endif /* PSA_CRYPTO_DRIVER_TEST */
1019#if defined(PSA_CRYPTO_DRIVER_TEST)
1020 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001021 return( test_opaque_cipher_finish( operation->ctx,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001022 output,
1023 output_size,
1024 output_length ) );
1025#endif /* PSA_CRYPTO_DRIVER_TEST */
1026 default:
1027 /* Key is attached to a driver not known to us */
Ronald Cronc45b4af2020-09-29 16:18:05 +02001028 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001029 }
1030#else /* PSA_CRYPTO_DRIVER_PRESENT */
1031 (void) operation;
1032 (void) output;
1033 (void) output_size;
1034 (void) output_length;
1035
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001036 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001037#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1038}
1039
1040psa_status_t psa_driver_wrapper_cipher_abort(
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001041 psa_operation_driver_context_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001042{
1043#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1044 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001045
1046 /* The object has (apparently) been initialized but it is not in use. It's
1047 * ok to call abort on such an object, and there's nothing to do. */
1048 if( operation->ctx == NULL && operation->id == 0 )
1049 return( PSA_SUCCESS );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001050
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001051 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001052 {
1053#if defined(PSA_CRYPTO_DRIVER_TEST)
1054 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001055 status = test_transparent_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001056 mbedtls_platform_zeroize(
1057 operation->ctx,
1058 sizeof( test_transparent_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001059 mbedtls_free( operation->ctx );
1060 operation->ctx = NULL;
1061 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001062
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001063 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001064#endif /* PSA_CRYPTO_DRIVER_TEST */
1065#if defined(PSA_CRYPTO_DRIVER_TEST)
1066 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001067 status = test_opaque_cipher_abort( operation->ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001068 mbedtls_platform_zeroize(
1069 operation->ctx,
1070 sizeof( test_opaque_cipher_operation_t ) );
Steven Cooremanfb81aa52020-09-09 12:01:43 +02001071 mbedtls_free( operation->ctx );
1072 operation->ctx = NULL;
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001073 operation->id = 0;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001074
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001075 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001076#endif /* PSA_CRYPTO_DRIVER_TEST */
1077 default:
1078 /* Operation is attached to a driver not known to us */
Ronald Cronc45b4af2020-09-29 16:18:05 +02001079 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001080 }
1081#else /* PSA_CRYPTO_DRIVER_PRESENT */
1082 (void)operation;
1083
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001084 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001085#endif /* PSA_CRYPTO_DRIVER_PRESENT */
1086}
1087
Steven Cooreman1e582352021-02-18 17:24:37 +01001088/*
1089 * Hashing functions
1090 */
1091psa_status_t psa_driver_wrapper_hash_compute(
1092 psa_algorithm_t alg,
1093 const uint8_t *input,
1094 size_t input_length,
1095 uint8_t *hash,
1096 size_t hash_size,
1097 size_t *hash_length)
1098{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001099 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001100
1101 /* Try accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001102#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001103 status = mbedtls_transparent_test_driver_hash_compute(
1104 alg, input, input_length, hash, hash_size, hash_length );
Steven Cooremanf7638102021-03-04 15:14:36 +01001105 if( status != PSA_ERROR_NOT_SUPPORTED )
1106 return( status );
1107#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001108
1109 /* If software fallback is compiled in, try fallback */
1110#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1111 status = mbedtls_psa_hash_compute( alg, input, input_length,
1112 hash, hash_size, hash_length );
1113 if( status != PSA_ERROR_NOT_SUPPORTED )
1114 return( status );
1115#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001116 (void) status;
1117 (void) alg;
1118 (void) input;
1119 (void) input_length;
1120 (void) hash;
1121 (void) hash_size;
1122 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001123
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001124 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman1e582352021-02-18 17:24:37 +01001125}
1126
1127psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001128 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001129 psa_algorithm_t alg )
1130{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001131 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001132
Steven Cooreman1e582352021-02-18 17:24:37 +01001133 /* Try setup on accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001134#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001135 status = mbedtls_transparent_test_driver_hash_setup(
1136 &operation->ctx.test_driver_ctx, alg );
Steven Cooremanf7638102021-03-04 15:14:36 +01001137 if( status == PSA_SUCCESS )
1138 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1139
1140 if( status != PSA_ERROR_NOT_SUPPORTED )
1141 return( status );
1142#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001143
1144 /* If software fallback is compiled in, try fallback */
1145#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001146 status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
Steven Cooreman1e582352021-02-18 17:24:37 +01001147 if( status == PSA_SUCCESS )
Steven Cooreman1e582352021-02-18 17:24:37 +01001148 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Steven Cooreman1e582352021-02-18 17:24:37 +01001149
1150 if( status != PSA_ERROR_NOT_SUPPORTED )
1151 return( status );
1152#endif
1153 /* Nothing left to try if we fall through here */
1154 (void) status;
1155 (void) operation;
1156 (void) alg;
1157 return( PSA_ERROR_NOT_SUPPORTED );
1158}
1159
1160psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001161 const psa_hash_operation_t *source_operation,
1162 psa_hash_operation_t *target_operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001163{
Steven Cooreman1e582352021-02-18 17:24:37 +01001164 switch( source_operation->id )
1165 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001166#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1167 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1168 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1169 return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
1170 &target_operation->ctx.mbedtls_ctx ) );
1171#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001172#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001173 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1174 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001175 return( mbedtls_transparent_test_driver_hash_clone(
1176 &source_operation->ctx.test_driver_ctx,
1177 &target_operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001178#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001179 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001180 (void) target_operation;
1181 return( PSA_ERROR_BAD_STATE );
1182 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001183}
1184
1185psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001186 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001187 const uint8_t *input,
1188 size_t input_length )
1189{
Steven Cooreman1e582352021-02-18 17:24:37 +01001190 switch( operation->id )
1191 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001192#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1193 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1194 return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
1195 input, input_length ) );
1196#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001197#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001198 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001199 return( mbedtls_transparent_test_driver_hash_update(
1200 &operation->ctx.test_driver_ctx,
1201 input, input_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001202#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001203 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001204 (void) input;
1205 (void) input_length;
1206 return( PSA_ERROR_BAD_STATE );
1207 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001208}
1209
1210psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001211 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001212 uint8_t *hash,
1213 size_t hash_size,
1214 size_t *hash_length )
1215{
Steven Cooreman1e582352021-02-18 17:24:37 +01001216 switch( operation->id )
1217 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001218#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1219 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1220 return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
1221 hash, hash_size, hash_length ) );
1222#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001223#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001224 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001225 return( mbedtls_transparent_test_driver_hash_finish(
1226 &operation->ctx.test_driver_ctx,
1227 hash, hash_size, hash_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001228#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001229 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001230 (void) hash;
1231 (void) hash_size;
1232 (void) hash_length;
1233 return( PSA_ERROR_BAD_STATE );
1234 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001235}
1236
1237psa_status_t psa_driver_wrapper_hash_abort(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001238 psa_hash_operation_t *operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001239{
Steven Cooreman1e582352021-02-18 17:24:37 +01001240 switch( operation->id )
1241 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001242#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1243 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1244 return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
1245#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001246#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001247 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001248 return( mbedtls_transparent_test_driver_hash_abort(
1249 &operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001250#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001251 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001252 return( PSA_ERROR_BAD_STATE );
1253 }
1254}
1255
Steven Cooremancd84cb42020-07-16 20:28:36 +02001256/* End of automatically generated file. */