blob: 9459c4636b3cde0e273169a9297f6bddd623c586 [file] [log] [blame]
Steven Cooremancd84cb42020-07-16 20:28:36 +02001/*
2 * Functions to delegate cryptographic operations to an available
3 * and appropriate accelerator.
Steven Cooreman56250fd2020-09-04 13:07:15 +02004 * Warning: This file will be auto-generated in the future.
Steven Cooremancd84cb42020-07-16 20:28:36 +02005 */
Steven Cooreman2c7b2f82020-09-02 13:43:46 +02006/* Copyright The Mbed TLS Contributors
Steven Cooremancd84cb42020-07-16 20:28:36 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Steven Cooremancd84cb42020-07-16 20:28:36 +020020 */
21
Ronald Cron0b805592020-12-14 18:08:20 +010022#include "psa_crypto_cipher.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020023#include "psa_crypto_core.h"
24#include "psa_crypto_driver_wrappers.h"
Steven Cooreman830aff22021-03-09 09:50:44 +010025#include "psa_crypto_hash.h"
26
Steven Cooreman2a1664c2020-07-20 15:33:08 +020027#include "mbedtls/platform.h"
28
29#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
Steven Cooremancd84cb42020-07-16 20:28:36 +020030
31/* Include test driver definition when running tests */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020032#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman56250fd2020-09-04 13:07:15 +020033#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020034#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020035#endif
36#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020037#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020038#endif
Steven Cooreman0d7c64d2020-09-07 16:17:55 +020039#include "test/drivers/test_driver.h"
Steven Cooremanf1720ea2020-07-24 18:41:58 +020040#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +020041
Steven Cooreman56250fd2020-09-04 13:07:15 +020042/* Repeat above block for each JSON-declared driver during autogeneration */
Steven Cooremanaa87fd02021-03-15 18:54:03 +010043#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Steven Cooreman56250fd2020-09-04 13:07:15 +020044
Steven Cooreman1e582352021-02-18 17:24:37 +010045/* Auto-generated values depending on which drivers are registered.
46 * ID 0 is reserved for unallocated operations.
47 * ID 1 is reserved for the Mbed TLS software driver. */
Steven Cooreman830aff22021-03-09 09:50:44 +010048#define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
49
Steven Cooreman37941cb2020-07-28 18:49:51 +020050#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman1e582352021-02-18 17:24:37 +010051#define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2)
52#define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3)
Steven Cooreman37941cb2020-07-28 18:49:51 +020053#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +020054
55/* Support the 'old' SE interface when asked to */
Steven Cooreman7a250572020-07-17 16:43:05 +020056#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman56250fd2020-09-04 13:07:15 +020057/* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
58 * SE driver is present, to avoid unused argument errors at compile time. */
59#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020060#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020061#endif
Steven Cooreman7a250572020-07-17 16:43:05 +020062#include "psa_crypto_se.h"
63#endif
64
Steven Cooremancd84cb42020-07-16 20:28:36 +020065/* Start delegation functions */
Ronald Cron9f17aa42020-12-08 17:07:25 +010066psa_status_t psa_driver_wrapper_sign_hash(
67 const psa_key_attributes_t *attributes,
68 const uint8_t *key_buffer, size_t key_buffer_size,
69 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
70 uint8_t *signature, size_t signature_size, size_t *signature_length )
Steven Cooremancd84cb42020-07-16 20:28:36 +020071{
Steven Cooreman7a250572020-07-17 16:43:05 +020072 /* Try dynamically-registered SE interface first */
73#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
74 const psa_drv_se_t *drv;
75 psa_drv_se_context_t *drv_context;
76
Ronald Cron9f17aa42020-12-08 17:07:25 +010077 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman7a250572020-07-17 16:43:05 +020078 {
79 if( drv->asymmetric == NULL ||
80 drv->asymmetric->p_sign == NULL )
81 {
82 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +020083 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman7a250572020-07-17 16:43:05 +020084 }
Ronald Cron9f17aa42020-12-08 17:07:25 +010085 return( drv->asymmetric->p_sign(
86 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
87 alg, hash, hash_length,
88 signature, signature_size, signature_length ) );
Steven Cooreman7a250572020-07-17 16:43:05 +020089 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +020090#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman7a250572020-07-17 16:43:05 +020091
Ronald Cronfce9df22020-12-08 18:06:03 +010092 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +010093 psa_key_location_t location =
94 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooremancd84cb42020-07-16 20:28:36 +020095
96 switch( location )
97 {
98 case PSA_KEY_LOCATION_LOCAL_STORAGE:
99 /* Key is stored in the slot in export representation, so
100 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100101#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200102#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron9f17aa42020-12-08 17:07:25 +0100103 status = test_transparent_signature_sign_hash( attributes,
104 key_buffer,
105 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200106 alg,
107 hash,
108 hash_length,
109 signature,
110 signature_size,
111 signature_length );
112 /* Declared with fallback == true */
113 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200114 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200115#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100116#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200117 /* Fell through, meaning no accelerator supports this operation */
Ronald Cronfce9df22020-12-08 18:06:03 +0100118 return( psa_sign_hash_internal( attributes,
119 key_buffer,
120 key_buffer_size,
121 alg,
122 hash,
123 hash_length,
124 signature,
125 signature_size,
126 signature_length ) );
127
Steven Cooremancd84cb42020-07-16 20:28:36 +0200128 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100129#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200130#if defined(PSA_CRYPTO_DRIVER_TEST)
131 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron9f17aa42020-12-08 17:07:25 +0100132 return( test_opaque_signature_sign_hash( attributes,
133 key_buffer,
134 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200135 alg,
136 hash,
137 hash_length,
138 signature,
139 signature_size,
140 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200141#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100142#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200143 default:
144 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100145 (void)status;
146 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200147 }
Steven Cooremancd84cb42020-07-16 20:28:36 +0200148}
149
Ronald Cron9f17aa42020-12-08 17:07:25 +0100150psa_status_t psa_driver_wrapper_verify_hash(
151 const psa_key_attributes_t *attributes,
152 const uint8_t *key_buffer, size_t key_buffer_size,
153 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
154 const uint8_t *signature, size_t signature_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200155{
Steven Cooreman55ae2172020-07-17 19:46:15 +0200156 /* Try dynamically-registered SE interface first */
157#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
158 const psa_drv_se_t *drv;
159 psa_drv_se_context_t *drv_context;
160
Ronald Cron9f17aa42020-12-08 17:07:25 +0100161 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200162 {
163 if( drv->asymmetric == NULL ||
164 drv->asymmetric->p_verify == NULL )
165 {
166 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200167 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200168 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100169 return( drv->asymmetric->p_verify(
170 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
171 alg, hash, hash_length,
172 signature, signature_length ) );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200173 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200174#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200175
Ronald Cronfce9df22020-12-08 18:06:03 +0100176 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100177 psa_key_location_t location =
178 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200179
180 switch( location )
181 {
182 case PSA_KEY_LOCATION_LOCAL_STORAGE:
183 /* Key is stored in the slot in export representation, so
184 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100185#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200186#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron9f17aa42020-12-08 17:07:25 +0100187 status = test_transparent_signature_verify_hash( attributes,
188 key_buffer,
189 key_buffer_size,
Steven Cooreman55ae2172020-07-17 19:46:15 +0200190 alg,
191 hash,
192 hash_length,
193 signature,
194 signature_length );
195 /* Declared with fallback == true */
196 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200197 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200198#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100199#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
200
201 return( psa_verify_hash_internal( attributes,
202 key_buffer,
203 key_buffer_size,
204 alg,
205 hash,
206 hash_length,
207 signature,
208 signature_length ) );
209
Steven Cooreman55ae2172020-07-17 19:46:15 +0200210 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100211#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200212#if defined(PSA_CRYPTO_DRIVER_TEST)
213 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron9f17aa42020-12-08 17:07:25 +0100214 return( test_opaque_signature_verify_hash( attributes,
215 key_buffer,
216 key_buffer_size,
Steven Cooreman55ae2172020-07-17 19:46:15 +0200217 alg,
218 hash,
219 hash_length,
220 signature,
221 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200222#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100223#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200224 default:
225 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100226 (void)status;
227 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200228 }
Steven Cooreman55ae2172020-07-17 19:46:15 +0200229}
230
Ronald Cron31216282020-12-05 18:47:56 +0100231/** Get the key buffer size for the key material of a generated key in the
232 * case of an opaque driver without storage.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200233 *
Ronald Cron31216282020-12-05 18:47:56 +0100234 * \param[in] attributes The key attributes.
235 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200236 *
237 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100238 * The minimum size for a buffer to contain the key material has been
239 * returned successfully.
240 * \retval #PSA_ERROR_INVALID_ARGUMENT
241 * The size in bits of the key is not valid.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200242 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100243 * The type and/or the size in bits of the key or the combination of
244 * the two is not supported.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200245 */
Ronald Cron9df74be2020-12-05 19:15:23 +0100246psa_status_t psa_driver_wrapper_get_key_buffer_size(
Ronald Cron31216282020-12-05 18:47:56 +0100247 const psa_key_attributes_t *attributes,
248 size_t *key_buffer_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200249{
John Durkop2c618352020-09-22 06:54:01 -0700250 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
251 psa_key_type_t key_type = attributes->core.type;
252 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200253
Ronald Cron31216282020-12-05 18:47:56 +0100254 *key_buffer_size = 0;
John Durkop2c618352020-09-22 06:54:01 -0700255 switch( location )
256 {
John Durkop2c618352020-09-22 06:54:01 -0700257#if defined(PSA_CRYPTO_DRIVER_TEST)
258 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
John Durkopac93e3b2020-10-16 06:48:55 -0700259#ifdef TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION
Ronald Cron31216282020-12-05 18:47:56 +0100260 *key_buffer_size = test_size_function( key_type, key_bits );
John Durkop2c618352020-09-22 06:54:01 -0700261 return( PSA_SUCCESS );
262#else /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
263 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) )
264 {
Ronald Cron31216282020-12-05 18:47:56 +0100265 int public_key_overhead =
266 ( ( TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 ) ?
267 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) : 0 );
268 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop135ce692020-10-19 07:12:28 -0700269 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE
270 + public_key_overhead;
271 }
Ronald Cron31216282020-12-05 18:47:56 +0100272 else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( key_type ) )
John Durkop135ce692020-10-19 07:12:28 -0700273 {
Ronald Cron31216282020-12-05 18:47:56 +0100274 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700275 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE;
276 }
John Durkop135ce692020-10-19 07:12:28 -0700277 else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) &&
Ronald Cron31216282020-12-05 18:47:56 +0100278 !PSA_KEY_TYPE_IS_PUBLIC_KEY ( key_type ) )
John Durkop2c618352020-09-22 06:54:01 -0700279 {
Ronald Cron31216282020-12-05 18:47:56 +0100280 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700281 + TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR
282 * ( ( key_bits + 7 ) / 8 );
283 }
284 else
285 {
286 return( PSA_ERROR_NOT_SUPPORTED );
287 }
288 return( PSA_SUCCESS );
289#endif /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
290#endif /* PSA_CRYPTO_DRIVER_TEST */
291
292 default:
Ronald Cron9df74be2020-12-05 19:15:23 +0100293 (void)key_type;
294 (void)key_bits;
Steven Cooreman56250fd2020-09-04 13:07:15 +0200295 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200296 }
297}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200298
Ronald Cron977c2472020-10-13 08:32:21 +0200299psa_status_t psa_driver_wrapper_generate_key(
300 const psa_key_attributes_t *attributes,
301 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200302{
Ronald Cron977c2472020-10-13 08:32:21 +0200303 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
304 psa_key_location_t location =
305 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
306
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200307 /* Try dynamically-registered SE interface first */
308#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
309 const psa_drv_se_t *drv;
310 psa_drv_se_context_t *drv_context;
311
Ronald Cron977c2472020-10-13 08:32:21 +0200312 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200313 {
314 size_t pubkey_length = 0; /* We don't support this feature yet */
315 if( drv->key_management == NULL ||
316 drv->key_management->p_generate == NULL )
317 {
318 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200319 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200320 }
321 return( drv->key_management->p_generate(
Ronald Cron977c2472020-10-13 08:32:21 +0200322 drv_context,
323 *( (psa_key_slot_number_t *)key_buffer ),
Ronald Cronea0f8a62020-11-25 17:52:23 +0100324 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200325 }
326#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
327
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200328 switch( location )
329 {
330 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Ronald Cron977c2472020-10-13 08:32:21 +0200331#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200332 /* Transparent drivers are limited to generating asymmetric keys */
Ronald Cron977c2472020-10-13 08:32:21 +0200333 if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200334 {
Ronald Cron977c2472020-10-13 08:32:21 +0200335 /* Cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200336#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron977c2472020-10-13 08:32:21 +0200337 status = test_transparent_generate_key(
338 attributes, key_buffer, key_buffer_size,
339 key_buffer_length );
340 /* Declared with fallback == true */
341 if( status != PSA_ERROR_NOT_SUPPORTED )
342 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200343#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200344 }
345#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
346
347 /* Software fallback */
348 status = psa_generate_key_internal(
349 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200350 break;
Ronald Cron977c2472020-10-13 08:32:21 +0200351
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200352 /* Add cases for opaque driver here */
Ronald Cron977c2472020-10-13 08:32:21 +0200353#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200354#if defined(PSA_CRYPTO_DRIVER_TEST)
355 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron977c2472020-10-13 08:32:21 +0200356 status = test_opaque_generate_key(
357 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200358 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200359#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200360#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
361
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200362 default:
363 /* Key is declared with a lifetime not known to us */
364 status = PSA_ERROR_INVALID_ARGUMENT;
365 break;
366 }
367
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200368 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200369}
370
Ronald Cron83282872020-11-22 14:02:39 +0100371psa_status_t psa_driver_wrapper_import_key(
372 const psa_key_attributes_t *attributes,
373 const uint8_t *data,
374 size_t data_length,
375 uint8_t *key_buffer,
376 size_t key_buffer_size,
377 size_t *key_buffer_length,
378 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200379{
Steven Cooreman04524762020-10-13 17:43:44 +0200380 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100381 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
382 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200383
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100384 /* Try dynamically-registered SE interface first */
385#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
386 const psa_drv_se_t *drv;
387 psa_drv_se_context_t *drv_context;
388
389 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
390 {
391 if( drv->key_management == NULL ||
392 drv->key_management->p_import == NULL )
393 return( PSA_ERROR_NOT_SUPPORTED );
394
395 /* The driver should set the number of key bits, however in
396 * case it doesn't, we initialize bits to an invalid value. */
397 *bits = PSA_MAX_KEY_BITS + 1;
398 status = drv->key_management->p_import(
399 drv_context,
400 *( (psa_key_slot_number_t *)key_buffer ),
401 attributes, data, data_length, bits );
402
403 if( status != PSA_SUCCESS )
404 return( status );
405
406 if( (*bits) > PSA_MAX_KEY_BITS )
407 return( PSA_ERROR_NOT_SUPPORTED );
408
409 return( PSA_SUCCESS );
410 }
411#endif /* PSA_CRYPTO_SE_C */
412
Ronald Cronbf33c932020-11-28 18:06:53 +0100413 switch( location )
414 {
415 case PSA_KEY_LOCATION_LOCAL_STORAGE:
416 /* Key is stored in the slot in export representation, so
417 * cycle through all known transparent accelerators */
418#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
419#if defined(PSA_CRYPTO_DRIVER_TEST)
420 status = test_transparent_import_key( attributes,
421 data, data_length,
422 key_buffer, key_buffer_size,
423 key_buffer_length, bits );
424 /* Declared with fallback == true */
425 if( status != PSA_ERROR_NOT_SUPPORTED )
426 return( status );
427#endif /* PSA_CRYPTO_DRIVER_TEST */
428#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
429 /* Fell through, meaning no accelerator supports this operation */
430 return( psa_import_key_into_slot( attributes,
431 data, data_length,
432 key_buffer, key_buffer_size,
433 key_buffer_length, bits ) );
434
435 default:
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100436 /* Importing a key with external storage in not yet supported.
437 * Return in error indicating that the lifetime is not valid. */
Ronald Cronbf33c932020-11-28 18:06:53 +0100438 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100439 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100440 }
441
Steven Cooreman04524762020-10-13 17:43:44 +0200442}
443
Ronald Cron67227982020-11-26 15:16:05 +0100444psa_status_t psa_driver_wrapper_export_key(
445 const psa_key_attributes_t *attributes,
446 const uint8_t *key_buffer, size_t key_buffer_size,
447 uint8_t *data, size_t data_size, size_t *data_length )
448
449{
450 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
451 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
452 psa_get_key_lifetime( attributes ) );
453
Ronald Cron152e3f82020-11-26 16:06:41 +0100454 /* Try dynamically-registered SE interface first */
455#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
456 const psa_drv_se_t *drv;
457 psa_drv_se_context_t *drv_context;
458
459 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
460 {
461 if( ( drv->key_management == NULL ) ||
462 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100463 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100464 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100465 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100466
467 return( drv->key_management->p_export(
468 drv_context,
469 *( (psa_key_slot_number_t *)key_buffer ),
470 data, data_size, data_length ) );
471 }
472#endif /* PSA_CRYPTO_SE_C */
473
Ronald Cron67227982020-11-26 15:16:05 +0100474 switch( location )
475 {
476 case PSA_KEY_LOCATION_LOCAL_STORAGE:
477 return( psa_export_key_internal( attributes,
478 key_buffer,
479 key_buffer_size,
480 data,
481 data_size,
482 data_length ) );
483
484 /* Add cases for opaque driver here */
485#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
486#if defined(PSA_CRYPTO_DRIVER_TEST)
487 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
488 return( test_opaque_export_key( attributes,
489 key_buffer,
490 key_buffer_size,
491 data,
492 data_size,
493 data_length ) );
494#endif /* PSA_CRYPTO_DRIVER_TEST */
495#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
496 default:
497 /* Key is declared with a lifetime not known to us */
498 return( status );
499 }
500}
501
Ronald Cron84cc9942020-11-25 14:30:05 +0100502psa_status_t psa_driver_wrapper_export_public_key(
503 const psa_key_attributes_t *attributes,
504 const uint8_t *key_buffer, size_t key_buffer_size,
505 uint8_t *data, size_t data_size, size_t *data_length )
506
Steven Cooremanb9b84422020-10-14 14:39:20 +0200507{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200508 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100509 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
510 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200511
Ronald Cron152e3f82020-11-26 16:06:41 +0100512 /* Try dynamically-registered SE interface first */
513#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
514 const psa_drv_se_t *drv;
515 psa_drv_se_context_t *drv_context;
516
517 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
518 {
519 if( ( drv->key_management == NULL ) ||
520 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100521 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100522 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100523 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100524
525 return( drv->key_management->p_export_public(
526 drv_context,
527 *( (psa_key_slot_number_t *)key_buffer ),
528 data, data_size, data_length ) );
529 }
530#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
531
Steven Cooremanb9b84422020-10-14 14:39:20 +0200532 switch( location )
533 {
534 case PSA_KEY_LOCATION_LOCAL_STORAGE:
535 /* Key is stored in the slot in export representation, so
536 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100537#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200538#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron84cc9942020-11-25 14:30:05 +0100539 status = test_transparent_export_public_key( attributes,
540 key_buffer,
541 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200542 data,
543 data_size,
544 data_length );
545 /* Declared with fallback == true */
546 if( status != PSA_ERROR_NOT_SUPPORTED )
547 return( status );
548#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100549#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200550 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100551 return( psa_export_public_key_internal( attributes,
552 key_buffer,
553 key_buffer_size,
554 data,
555 data_size,
556 data_length ) );
557
Steven Cooremanb9b84422020-10-14 14:39:20 +0200558 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100559#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200560#if defined(PSA_CRYPTO_DRIVER_TEST)
561 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron84cc9942020-11-25 14:30:05 +0100562 return( test_opaque_export_public_key( attributes,
563 key_buffer,
564 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200565 data,
566 data_size,
567 data_length ) );
568#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100569#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200570 default:
571 /* Key is declared with a lifetime not known to us */
572 return( status );
573 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200574}
575
Steven Cooreman37941cb2020-07-28 18:49:51 +0200576/*
577 * Cipher functions
578 */
579psa_status_t psa_driver_wrapper_cipher_encrypt(
580 psa_key_slot_t *slot,
581 psa_algorithm_t alg,
582 const uint8_t *input,
583 size_t input_length,
584 uint8_t *output,
585 size_t output_size,
586 size_t *output_length )
587{
588#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
589 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
590 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
591 psa_key_attributes_t attributes = {
592 .core = slot->attr
593 };
594
595 switch( location )
596 {
597 case PSA_KEY_LOCATION_LOCAL_STORAGE:
598 /* Key is stored in the slot in export representation, so
599 * cycle through all known transparent accelerators */
600#if defined(PSA_CRYPTO_DRIVER_TEST)
601 status = test_transparent_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100602 slot->key.data,
603 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200604 alg,
605 input,
606 input_length,
607 output,
608 output_size,
609 output_length );
610 /* Declared with fallback == true */
611 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200612 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200613#endif /* PSA_CRYPTO_DRIVER_TEST */
614 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200615 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200616 /* Add cases for opaque driver here */
617#if defined(PSA_CRYPTO_DRIVER_TEST)
618 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
619 return( test_opaque_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100620 slot->key.data,
621 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200622 alg,
623 input,
624 input_length,
625 output,
626 output_size,
627 output_length ) );
628#endif /* PSA_CRYPTO_DRIVER_TEST */
629 default:
630 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200631 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200632 }
633#else /* PSA_CRYPTO_DRIVER_PRESENT */
634 (void) slot;
635 (void) alg;
636 (void) input;
637 (void) input_length;
638 (void) output;
639 (void) output_size;
640 (void) output_length;
641
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200642 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200643#endif /* PSA_CRYPTO_DRIVER_PRESENT */
644}
645
646psa_status_t psa_driver_wrapper_cipher_decrypt(
647 psa_key_slot_t *slot,
648 psa_algorithm_t alg,
649 const uint8_t *input,
650 size_t input_length,
651 uint8_t *output,
652 size_t output_size,
653 size_t *output_length )
654{
655#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
656 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
657 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
658 psa_key_attributes_t attributes = {
659 .core = slot->attr
660 };
661
662 switch( location )
663 {
664 case PSA_KEY_LOCATION_LOCAL_STORAGE:
665 /* Key is stored in the slot in export representation, so
666 * cycle through all known transparent accelerators */
667#if defined(PSA_CRYPTO_DRIVER_TEST)
668 status = test_transparent_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100669 slot->key.data,
670 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200671 alg,
672 input,
673 input_length,
674 output,
675 output_size,
676 output_length );
677 /* Declared with fallback == true */
678 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200679 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200680#endif /* PSA_CRYPTO_DRIVER_TEST */
681 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200682 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200683 /* Add cases for opaque driver here */
684#if defined(PSA_CRYPTO_DRIVER_TEST)
685 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
686 return( test_opaque_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100687 slot->key.data,
688 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200689 alg,
690 input,
691 input_length,
692 output,
693 output_size,
694 output_length ) );
695#endif /* PSA_CRYPTO_DRIVER_TEST */
696 default:
697 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200698 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200699 }
700#else /* PSA_CRYPTO_DRIVER_PRESENT */
701 (void) slot;
702 (void) alg;
703 (void) input;
704 (void) input_length;
705 (void) output;
706 (void) output_size;
707 (void) output_length;
708
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200709 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200710#endif /* PSA_CRYPTO_DRIVER_PRESENT */
711}
712
713psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100714 psa_cipher_operation_t *operation,
715 const psa_key_attributes_t *attributes,
716 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200717 psa_algorithm_t alg )
718{
Ronald Cron0b805592020-12-14 18:08:20 +0100719 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona4af55f2020-12-14 14:36:06 +0100720 psa_key_location_t location =
721 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200722
Steven Cooreman37941cb2020-07-28 18:49:51 +0200723 switch( location )
724 {
725 case PSA_KEY_LOCATION_LOCAL_STORAGE:
726 /* Key is stored in the slot in export representation, so
727 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100728#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200729#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100730 status = test_transparent_cipher_encrypt_setup(
731 &operation->ctx.transparent_test_driver_ctx,
732 attributes,
733 key_buffer,
734 key_buffer_size,
735 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200736 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200737 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100738 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +0200739
Ronald Cron0b805592020-12-14 18:08:20 +0100740 if( status != PSA_ERROR_NOT_SUPPORTED )
741 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200742#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100743#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100744#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200745 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +0100746 status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100747 attributes,
748 key_buffer,
749 key_buffer_size,
750 alg );
751 if( status == PSA_SUCCESS )
Ronald Cron6e412a72021-03-10 09:58:47 +0100752 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Ronald Cron49fafa92021-03-10 08:34:23 +0100753
Ronald Cron7b4154d2021-03-19 14:49:41 +0100754 if( status != PSA_ERROR_NOT_SUPPORTED )
755 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100756#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
757 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron0b805592020-12-14 18:08:20 +0100758
Steven Cooreman37941cb2020-07-28 18:49:51 +0200759 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100760#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200761#if defined(PSA_CRYPTO_DRIVER_TEST)
762 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100763 status = test_opaque_cipher_encrypt_setup(
764 &operation->ctx.opaque_test_driver_ctx,
765 attributes,
766 key_buffer, key_buffer_size,
767 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200768
Steven Cooreman37941cb2020-07-28 18:49:51 +0200769 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100770 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200771
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200772 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200773#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100774#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +0200775 default:
776 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +0100777 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100778 (void)key_buffer;
779 (void)key_buffer_size;
780 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +0200781 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200782 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200783}
784
785psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100786 psa_cipher_operation_t *operation,
787 const psa_key_attributes_t *attributes,
788 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200789 psa_algorithm_t alg )
790{
Steven Cooreman37941cb2020-07-28 18:49:51 +0200791 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crona4af55f2020-12-14 14:36:06 +0100792 psa_key_location_t location =
793 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200794
Steven Cooreman37941cb2020-07-28 18:49:51 +0200795 switch( location )
796 {
797 case PSA_KEY_LOCATION_LOCAL_STORAGE:
798 /* Key is stored in the slot in export representation, so
799 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100800#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200801#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100802 status = test_transparent_cipher_decrypt_setup(
803 &operation->ctx.transparent_test_driver_ctx,
804 attributes,
805 key_buffer,
806 key_buffer_size,
807 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200808 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200809 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100810 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +0200811
Ronald Cron0b805592020-12-14 18:08:20 +0100812 if( status != PSA_ERROR_NOT_SUPPORTED )
813 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200814#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100815#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100816#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200817 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +0100818 status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100819 attributes,
820 key_buffer,
821 key_buffer_size,
822 alg );
823 if( status == PSA_SUCCESS )
824 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
825
826 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100827#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
828 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron49fafa92021-03-10 08:34:23 +0100829
Steven Cooreman37941cb2020-07-28 18:49:51 +0200830 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100831#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200832#if defined(PSA_CRYPTO_DRIVER_TEST)
833 case PSA_CRYPTO_TEST_DRIVER_LIFETIME:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100834 status = test_opaque_cipher_decrypt_setup(
835 &operation->ctx.opaque_test_driver_ctx,
836 attributes,
837 key_buffer, key_buffer_size,
838 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200839
Steven Cooreman37941cb2020-07-28 18:49:51 +0200840 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100841 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200842
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200843 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200844#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100845#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +0200846 default:
847 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +0100848 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100849 (void)key_buffer;
850 (void)key_buffer_size;
851 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +0200852 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200853 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200854}
855
Steven Cooreman37941cb2020-07-28 18:49:51 +0200856psa_status_t psa_driver_wrapper_cipher_set_iv(
Ronald Cron6056fe82020-12-15 13:58:07 +0100857 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200858 const uint8_t *iv,
859 size_t iv_length )
860{
Ronald Cron49fafa92021-03-10 08:34:23 +0100861 switch( operation->id )
862 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100863#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +0100864 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +0100865 return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100866 iv,
867 iv_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100868#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100869
870#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200871#if defined(PSA_CRYPTO_DRIVER_TEST)
872 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100873 return( test_transparent_cipher_set_iv(
874 &operation->ctx.transparent_test_driver_ctx,
875 iv, iv_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +0100876
Steven Cooreman37941cb2020-07-28 18:49:51 +0200877 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100878 return( test_opaque_cipher_set_iv(
879 &operation->ctx.opaque_test_driver_ctx,
880 iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200881#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100882#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +0100883 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200884
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100885 (void)iv;
886 (void)iv_length;
887
Ronald Crondd24c9b2020-12-15 14:10:01 +0100888 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200889}
890
891psa_status_t psa_driver_wrapper_cipher_update(
Ronald Cron6056fe82020-12-15 13:58:07 +0100892 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200893 const uint8_t *input,
894 size_t input_length,
895 uint8_t *output,
896 size_t output_size,
897 size_t *output_length )
898{
Ronald Cron49fafa92021-03-10 08:34:23 +0100899 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200900 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100901#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +0100902 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +0100903 return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100904 input,
905 input_length,
906 output,
907 output_size,
908 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100909#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
910
Ronald Cron49fafa92021-03-10 08:34:23 +0100911#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200912#if defined(PSA_CRYPTO_DRIVER_TEST)
913 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100914 return( test_transparent_cipher_update(
915 &operation->ctx.transparent_test_driver_ctx,
916 input, input_length,
917 output, output_size, output_length ) );
918
Steven Cooreman37941cb2020-07-28 18:49:51 +0200919 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100920 return( test_opaque_cipher_update(
921 &operation->ctx.opaque_test_driver_ctx,
922 input, input_length,
923 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200924#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100925#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +0100926 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200927
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100928 (void)input;
929 (void)input_length;
930 (void)output;
931 (void)output_size;
932 (void)output_length;
933
Ronald Crondd24c9b2020-12-15 14:10:01 +0100934 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200935}
936
937psa_status_t psa_driver_wrapper_cipher_finish(
Ronald Cron6056fe82020-12-15 13:58:07 +0100938 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200939 uint8_t *output,
940 size_t output_size,
941 size_t *output_length )
942{
Ronald Cron49fafa92021-03-10 08:34:23 +0100943 switch( operation->id )
944 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100945#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +0100946 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +0100947 return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100948 output,
949 output_size,
950 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100951#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100952
953#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200954#if defined(PSA_CRYPTO_DRIVER_TEST)
955 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100956 return( test_transparent_cipher_finish(
957 &operation->ctx.transparent_test_driver_ctx,
958 output, output_size, output_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +0100959
Steven Cooreman37941cb2020-07-28 18:49:51 +0200960 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100961 return( test_opaque_cipher_finish(
962 &operation->ctx.opaque_test_driver_ctx,
963 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200964#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100965#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +0100966 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200967
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100968 (void)output;
969 (void)output_size;
970 (void)output_length;
971
Ronald Crondd24c9b2020-12-15 14:10:01 +0100972 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200973}
974
975psa_status_t psa_driver_wrapper_cipher_abort(
Ronald Cron6056fe82020-12-15 13:58:07 +0100976 psa_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200977{
Ronald Crondd24c9b2020-12-15 14:10:01 +0100978 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200979
Ronald Cron49fafa92021-03-10 08:34:23 +0100980 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200981 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100982#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +0100983 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +0100984 return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100985#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +0100986
987#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200988#if defined(PSA_CRYPTO_DRIVER_TEST)
989 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100990 status = test_transparent_cipher_abort(
991 &operation->ctx.transparent_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +0200992 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100993 &operation->ctx.transparent_test_driver_ctx,
994 sizeof( operation->ctx.transparent_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200995 return( status );
Ronald Cron49fafa92021-03-10 08:34:23 +0100996
Steven Cooreman37941cb2020-07-28 18:49:51 +0200997 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100998 status = test_opaque_cipher_abort(
999 &operation->ctx.opaque_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001000 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001001 &operation->ctx.opaque_test_driver_ctx,
1002 sizeof( operation->ctx.opaque_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001003 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001004#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001005#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001006 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001007
Ronald Cron49fafa92021-03-10 08:34:23 +01001008 (void)status;
Ronald Crondd24c9b2020-12-15 14:10:01 +01001009 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001010}
1011
Steven Cooreman1e582352021-02-18 17:24:37 +01001012/*
1013 * Hashing functions
1014 */
1015psa_status_t psa_driver_wrapper_hash_compute(
1016 psa_algorithm_t alg,
1017 const uint8_t *input,
1018 size_t input_length,
1019 uint8_t *hash,
1020 size_t hash_size,
1021 size_t *hash_length)
1022{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001023 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001024
1025 /* Try accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001026#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001027 status = mbedtls_transparent_test_driver_hash_compute(
1028 alg, input, input_length, hash, hash_size, hash_length );
Steven Cooremanf7638102021-03-04 15:14:36 +01001029 if( status != PSA_ERROR_NOT_SUPPORTED )
1030 return( status );
1031#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001032
1033 /* If software fallback is compiled in, try fallback */
1034#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1035 status = mbedtls_psa_hash_compute( alg, input, input_length,
1036 hash, hash_size, hash_length );
1037 if( status != PSA_ERROR_NOT_SUPPORTED )
1038 return( status );
1039#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001040 (void) status;
1041 (void) alg;
1042 (void) input;
1043 (void) input_length;
1044 (void) hash;
1045 (void) hash_size;
1046 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001047
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001048 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman1e582352021-02-18 17:24:37 +01001049}
1050
1051psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001052 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001053 psa_algorithm_t alg )
1054{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001055 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001056
Steven Cooreman1e582352021-02-18 17:24:37 +01001057 /* Try setup on accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001058#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001059 status = mbedtls_transparent_test_driver_hash_setup(
1060 &operation->ctx.test_driver_ctx, alg );
Steven Cooremanf7638102021-03-04 15:14:36 +01001061 if( status == PSA_SUCCESS )
1062 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1063
1064 if( status != PSA_ERROR_NOT_SUPPORTED )
1065 return( status );
1066#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001067
1068 /* If software fallback is compiled in, try fallback */
1069#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001070 status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
Steven Cooreman1e582352021-02-18 17:24:37 +01001071 if( status == PSA_SUCCESS )
Steven Cooreman1e582352021-02-18 17:24:37 +01001072 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Steven Cooreman1e582352021-02-18 17:24:37 +01001073
1074 if( status != PSA_ERROR_NOT_SUPPORTED )
1075 return( status );
1076#endif
1077 /* Nothing left to try if we fall through here */
1078 (void) status;
1079 (void) operation;
1080 (void) alg;
1081 return( PSA_ERROR_NOT_SUPPORTED );
1082}
1083
1084psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001085 const psa_hash_operation_t *source_operation,
1086 psa_hash_operation_t *target_operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001087{
Steven Cooreman1e582352021-02-18 17:24:37 +01001088 switch( source_operation->id )
1089 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001090#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1091 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1092 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1093 return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
1094 &target_operation->ctx.mbedtls_ctx ) );
1095#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001096#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001097 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1098 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001099 return( mbedtls_transparent_test_driver_hash_clone(
1100 &source_operation->ctx.test_driver_ctx,
1101 &target_operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001102#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001103 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001104 (void) target_operation;
1105 return( PSA_ERROR_BAD_STATE );
1106 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001107}
1108
1109psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001110 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001111 const uint8_t *input,
1112 size_t input_length )
1113{
Steven Cooreman1e582352021-02-18 17:24:37 +01001114 switch( operation->id )
1115 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001116#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1117 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1118 return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
1119 input, input_length ) );
1120#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001121#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001122 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001123 return( mbedtls_transparent_test_driver_hash_update(
1124 &operation->ctx.test_driver_ctx,
1125 input, input_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001126#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001127 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001128 (void) input;
1129 (void) input_length;
1130 return( PSA_ERROR_BAD_STATE );
1131 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001132}
1133
1134psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001135 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001136 uint8_t *hash,
1137 size_t hash_size,
1138 size_t *hash_length )
1139{
Steven Cooreman1e582352021-02-18 17:24:37 +01001140 switch( operation->id )
1141 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001142#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1143 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1144 return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
1145 hash, hash_size, hash_length ) );
1146#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001147#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001148 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001149 return( mbedtls_transparent_test_driver_hash_finish(
1150 &operation->ctx.test_driver_ctx,
1151 hash, hash_size, hash_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001152#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001153 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001154 (void) hash;
1155 (void) hash_size;
1156 (void) hash_length;
1157 return( PSA_ERROR_BAD_STATE );
1158 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001159}
1160
1161psa_status_t psa_driver_wrapper_hash_abort(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001162 psa_hash_operation_t *operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001163{
Steven Cooreman1e582352021-02-18 17:24:37 +01001164 switch( 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 return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
1169#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001170#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001171 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001172 return( mbedtls_transparent_test_driver_hash_abort(
1173 &operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001174#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001175 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001176 return( PSA_ERROR_BAD_STATE );
1177 }
1178}
1179
Steven Cooremancd84cb42020-07-16 20:28:36 +02001180/* End of automatically generated file. */