blob: 11160d82d6f8c9248d7401e8103aaba961dcfb24 [file] [log] [blame]
Steven Cooremancd84cb42020-07-16 20:28:36 +02001/*
2 * Functions to delegate cryptographic operations to an available
3 * and appropriate accelerator.
Steven Cooreman56250fd2020-09-04 13:07:15 +02004 * Warning: This file will be auto-generated in the future.
Steven Cooremancd84cb42020-07-16 20:28:36 +02005 */
Steven Cooreman2c7b2f82020-09-02 13:43:46 +02006/* Copyright The Mbed TLS Contributors
Steven Cooremancd84cb42020-07-16 20:28:36 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Steven Cooremancd84cb42020-07-16 20:28:36 +020020 */
21
Ronald Cronde822812021-03-17 16:08:20 +010022#include "psa_crypto_aead.h"
Ronald Cron0b805592020-12-14 18:08:20 +010023#include "psa_crypto_cipher.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020024#include "psa_crypto_core.h"
25#include "psa_crypto_driver_wrappers.h"
Steven Cooreman830aff22021-03-09 09:50:44 +010026#include "psa_crypto_hash.h"
27
Steven Cooreman2a1664c2020-07-20 15:33:08 +020028#include "mbedtls/platform.h"
29
30#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
Steven Cooremancd84cb42020-07-16 20:28:36 +020031
32/* Include test driver definition when running tests */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020033#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman56250fd2020-09-04 13:07:15 +020034#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020035#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020036#endif
37#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020038#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020039#endif
Steven Cooreman0d7c64d2020-09-07 16:17:55 +020040#include "test/drivers/test_driver.h"
Steven Cooremanf1720ea2020-07-24 18:41:58 +020041#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +020042
Steven Cooreman56250fd2020-09-04 13:07:15 +020043/* Repeat above block for each JSON-declared driver during autogeneration */
Steven Cooremanaa87fd02021-03-15 18:54:03 +010044#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Steven Cooreman56250fd2020-09-04 13:07:15 +020045
Steven Cooreman1e582352021-02-18 17:24:37 +010046/* Auto-generated values depending on which drivers are registered.
47 * ID 0 is reserved for unallocated operations.
48 * ID 1 is reserved for the Mbed TLS software driver. */
Steven Cooreman830aff22021-03-09 09:50:44 +010049#define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
50
Steven Cooreman37941cb2020-07-28 18:49:51 +020051#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman1e582352021-02-18 17:24:37 +010052#define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2)
53#define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3)
Steven Cooreman37941cb2020-07-28 18:49:51 +020054#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +020055
56/* Support the 'old' SE interface when asked to */
Steven Cooreman7a250572020-07-17 16:43:05 +020057#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman56250fd2020-09-04 13:07:15 +020058/* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
59 * SE driver is present, to avoid unused argument errors at compile time. */
60#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020061#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020062#endif
Steven Cooreman7a250572020-07-17 16:43:05 +020063#include "psa_crypto_se.h"
64#endif
65
Steven Cooremancd84cb42020-07-16 20:28:36 +020066/* Start delegation functions */
Ronald Cron9f17aa42020-12-08 17:07:25 +010067psa_status_t psa_driver_wrapper_sign_hash(
68 const psa_key_attributes_t *attributes,
69 const uint8_t *key_buffer, size_t key_buffer_size,
70 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
71 uint8_t *signature, size_t signature_size, size_t *signature_length )
Steven Cooremancd84cb42020-07-16 20:28:36 +020072{
Steven Cooreman7a250572020-07-17 16:43:05 +020073 /* Try dynamically-registered SE interface first */
74#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
75 const psa_drv_se_t *drv;
76 psa_drv_se_context_t *drv_context;
77
Ronald Cron9f17aa42020-12-08 17:07:25 +010078 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman7a250572020-07-17 16:43:05 +020079 {
80 if( drv->asymmetric == NULL ||
81 drv->asymmetric->p_sign == NULL )
82 {
83 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +020084 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman7a250572020-07-17 16:43:05 +020085 }
Ronald Cron9f17aa42020-12-08 17:07:25 +010086 return( drv->asymmetric->p_sign(
87 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
88 alg, hash, hash_length,
89 signature, signature_size, signature_length ) );
Steven Cooreman7a250572020-07-17 16:43:05 +020090 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +020091#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman7a250572020-07-17 16:43:05 +020092
Ronald Cronfce9df22020-12-08 18:06:03 +010093 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +010094 psa_key_location_t location =
95 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooremancd84cb42020-07-16 20:28:36 +020096
97 switch( location )
98 {
99 case PSA_KEY_LOCATION_LOCAL_STORAGE:
100 /* Key is stored in the slot in export representation, so
101 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100102#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200103#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron9f17aa42020-12-08 17:07:25 +0100104 status = test_transparent_signature_sign_hash( attributes,
105 key_buffer,
106 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200107 alg,
108 hash,
109 hash_length,
110 signature,
111 signature_size,
112 signature_length );
113 /* Declared with fallback == true */
114 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200115 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200116#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100117#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200118 /* Fell through, meaning no accelerator supports this operation */
Ronald Cronfce9df22020-12-08 18:06:03 +0100119 return( psa_sign_hash_internal( attributes,
120 key_buffer,
121 key_buffer_size,
122 alg,
123 hash,
124 hash_length,
125 signature,
126 signature_size,
127 signature_length ) );
128
Steven Cooremancd84cb42020-07-16 20:28:36 +0200129 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100130#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200131#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100132 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron9f17aa42020-12-08 17:07:25 +0100133 return( test_opaque_signature_sign_hash( attributes,
134 key_buffer,
135 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200136 alg,
137 hash,
138 hash_length,
139 signature,
140 signature_size,
141 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200142#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100143#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200144 default:
145 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100146 (void)status;
147 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200148 }
Steven Cooremancd84cb42020-07-16 20:28:36 +0200149}
150
Ronald Cron9f17aa42020-12-08 17:07:25 +0100151psa_status_t psa_driver_wrapper_verify_hash(
152 const psa_key_attributes_t *attributes,
153 const uint8_t *key_buffer, size_t key_buffer_size,
154 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
155 const uint8_t *signature, size_t signature_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200156{
Steven Cooreman55ae2172020-07-17 19:46:15 +0200157 /* Try dynamically-registered SE interface first */
158#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
159 const psa_drv_se_t *drv;
160 psa_drv_se_context_t *drv_context;
161
Ronald Cron9f17aa42020-12-08 17:07:25 +0100162 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200163 {
164 if( drv->asymmetric == NULL ||
165 drv->asymmetric->p_verify == NULL )
166 {
167 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200168 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200169 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100170 return( drv->asymmetric->p_verify(
171 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
172 alg, hash, hash_length,
173 signature, signature_length ) );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200174 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200175#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200176
Ronald Cronfce9df22020-12-08 18:06:03 +0100177 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100178 psa_key_location_t location =
179 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200180
181 switch( location )
182 {
183 case PSA_KEY_LOCATION_LOCAL_STORAGE:
184 /* Key is stored in the slot in export representation, so
185 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100186#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200187#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron9f17aa42020-12-08 17:07:25 +0100188 status = test_transparent_signature_verify_hash( attributes,
189 key_buffer,
190 key_buffer_size,
Steven Cooreman55ae2172020-07-17 19:46:15 +0200191 alg,
192 hash,
193 hash_length,
194 signature,
195 signature_length );
196 /* Declared with fallback == true */
197 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200198 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200199#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100200#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
201
202 return( psa_verify_hash_internal( attributes,
203 key_buffer,
204 key_buffer_size,
205 alg,
206 hash,
207 hash_length,
208 signature,
209 signature_length ) );
210
Steven Cooreman55ae2172020-07-17 19:46:15 +0200211 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100212#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200213#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100214 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron9f17aa42020-12-08 17:07:25 +0100215 return( test_opaque_signature_verify_hash( attributes,
216 key_buffer,
217 key_buffer_size,
Steven Cooreman55ae2172020-07-17 19:46:15 +0200218 alg,
219 hash,
220 hash_length,
221 signature,
222 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200223#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100224#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200225 default:
226 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100227 (void)status;
228 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200229 }
Steven Cooreman55ae2172020-07-17 19:46:15 +0200230}
231
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200232/** Get the key buffer size required to store the key material of a key
233 * associated with an opaque driver without storage.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200234 *
Ronald Cron31216282020-12-05 18:47:56 +0100235 * \param[in] attributes The key attributes.
236 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200237 *
238 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100239 * The minimum size for a buffer to contain the key material has been
240 * returned successfully.
241 * \retval #PSA_ERROR_INVALID_ARGUMENT
242 * The size in bits of the key is not valid.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200243 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100244 * The type and/or the size in bits of the key or the combination of
245 * the two is not supported.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200246 */
Ronald Cron9df74be2020-12-05 19:15:23 +0100247psa_status_t psa_driver_wrapper_get_key_buffer_size(
Ronald Cron31216282020-12-05 18:47:56 +0100248 const psa_key_attributes_t *attributes,
249 size_t *key_buffer_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200250{
John Durkop2c618352020-09-22 06:54:01 -0700251 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
252 psa_key_type_t key_type = attributes->core.type;
253 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200254
Ronald Cron31216282020-12-05 18:47:56 +0100255 *key_buffer_size = 0;
John Durkop2c618352020-09-22 06:54:01 -0700256 switch( location )
257 {
John Durkop2c618352020-09-22 06:54:01 -0700258#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100259 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100260#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
261 /* Emulate property 'builtin_key_size' */
262 if( psa_key_id_is_builtin(
263 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(
264 psa_get_key_id( attributes ) ) ) )
265 {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100266 *key_buffer_size = sizeof( psa_drv_slot_number_t );
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100267 return( PSA_SUCCESS );
268 }
269#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
John Durkopac93e3b2020-10-16 06:48:55 -0700270#ifdef TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION
Ronald Cron31216282020-12-05 18:47:56 +0100271 *key_buffer_size = test_size_function( key_type, key_bits );
John Durkop2c618352020-09-22 06:54:01 -0700272 return( PSA_SUCCESS );
273#else /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
274 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) )
275 {
Ronald Cron31216282020-12-05 18:47:56 +0100276 int public_key_overhead =
277 ( ( TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 ) ?
278 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) : 0 );
279 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop135ce692020-10-19 07:12:28 -0700280 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE
281 + public_key_overhead;
282 }
Ronald Cron31216282020-12-05 18:47:56 +0100283 else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( key_type ) )
John Durkop135ce692020-10-19 07:12:28 -0700284 {
Ronald Cron31216282020-12-05 18:47:56 +0100285 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700286 + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE;
287 }
John Durkop135ce692020-10-19 07:12:28 -0700288 else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) &&
Ronald Cron31216282020-12-05 18:47:56 +0100289 !PSA_KEY_TYPE_IS_PUBLIC_KEY ( key_type ) )
John Durkop2c618352020-09-22 06:54:01 -0700290 {
Ronald Cron31216282020-12-05 18:47:56 +0100291 *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE
John Durkop2c618352020-09-22 06:54:01 -0700292 + TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR
293 * ( ( key_bits + 7 ) / 8 );
294 }
295 else
296 {
297 return( PSA_ERROR_NOT_SUPPORTED );
298 }
299 return( PSA_SUCCESS );
300#endif /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */
301#endif /* PSA_CRYPTO_DRIVER_TEST */
302
303 default:
Ronald Cron9df74be2020-12-05 19:15:23 +0100304 (void)key_type;
305 (void)key_bits;
Steven Cooreman56250fd2020-09-04 13:07:15 +0200306 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200307 }
308}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200309
Ronald Cron977c2472020-10-13 08:32:21 +0200310psa_status_t psa_driver_wrapper_generate_key(
311 const psa_key_attributes_t *attributes,
312 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200313{
Ronald Cron977c2472020-10-13 08:32:21 +0200314 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
315 psa_key_location_t location =
316 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
317
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200318 /* Try dynamically-registered SE interface first */
319#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
320 const psa_drv_se_t *drv;
321 psa_drv_se_context_t *drv_context;
322
Ronald Cron977c2472020-10-13 08:32:21 +0200323 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200324 {
325 size_t pubkey_length = 0; /* We don't support this feature yet */
326 if( drv->key_management == NULL ||
327 drv->key_management->p_generate == NULL )
328 {
329 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200330 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200331 }
332 return( drv->key_management->p_generate(
Ronald Cron977c2472020-10-13 08:32:21 +0200333 drv_context,
334 *( (psa_key_slot_number_t *)key_buffer ),
Ronald Cronea0f8a62020-11-25 17:52:23 +0100335 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200336 }
337#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
338
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200339 switch( location )
340 {
341 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Ronald Cron977c2472020-10-13 08:32:21 +0200342#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200343 /* Transparent drivers are limited to generating asymmetric keys */
Ronald Cron977c2472020-10-13 08:32:21 +0200344 if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200345 {
Ronald Cron977c2472020-10-13 08:32:21 +0200346 /* Cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200347#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron977c2472020-10-13 08:32:21 +0200348 status = test_transparent_generate_key(
349 attributes, key_buffer, key_buffer_size,
350 key_buffer_length );
351 /* Declared with fallback == true */
352 if( status != PSA_ERROR_NOT_SUPPORTED )
353 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200354#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200355 }
356#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
357
358 /* Software fallback */
359 status = psa_generate_key_internal(
360 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200361 break;
Ronald Cron977c2472020-10-13 08:32:21 +0200362
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200363 /* Add cases for opaque driver here */
Ronald Cron977c2472020-10-13 08:32:21 +0200364#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200365#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100366 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron977c2472020-10-13 08:32:21 +0200367 status = test_opaque_generate_key(
368 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200369 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200370#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200371#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
372
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200373 default:
374 /* Key is declared with a lifetime not known to us */
375 status = PSA_ERROR_INVALID_ARGUMENT;
376 break;
377 }
378
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200379 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200380}
381
Ronald Cron83282872020-11-22 14:02:39 +0100382psa_status_t psa_driver_wrapper_import_key(
383 const psa_key_attributes_t *attributes,
384 const uint8_t *data,
385 size_t data_length,
386 uint8_t *key_buffer,
387 size_t key_buffer_size,
388 size_t *key_buffer_length,
389 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200390{
Steven Cooreman04524762020-10-13 17:43:44 +0200391 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100392 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
393 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200394
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100395 /* Try dynamically-registered SE interface first */
396#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
397 const psa_drv_se_t *drv;
398 psa_drv_se_context_t *drv_context;
399
400 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
401 {
402 if( drv->key_management == NULL ||
403 drv->key_management->p_import == NULL )
404 return( PSA_ERROR_NOT_SUPPORTED );
405
406 /* The driver should set the number of key bits, however in
407 * case it doesn't, we initialize bits to an invalid value. */
408 *bits = PSA_MAX_KEY_BITS + 1;
409 status = drv->key_management->p_import(
410 drv_context,
411 *( (psa_key_slot_number_t *)key_buffer ),
412 attributes, data, data_length, bits );
413
414 if( status != PSA_SUCCESS )
415 return( status );
416
417 if( (*bits) > PSA_MAX_KEY_BITS )
418 return( PSA_ERROR_NOT_SUPPORTED );
419
420 return( PSA_SUCCESS );
421 }
422#endif /* PSA_CRYPTO_SE_C */
423
Ronald Cronbf33c932020-11-28 18:06:53 +0100424 switch( location )
425 {
426 case PSA_KEY_LOCATION_LOCAL_STORAGE:
427 /* Key is stored in the slot in export representation, so
428 * cycle through all known transparent accelerators */
429#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
430#if defined(PSA_CRYPTO_DRIVER_TEST)
431 status = test_transparent_import_key( attributes,
432 data, data_length,
433 key_buffer, key_buffer_size,
434 key_buffer_length, bits );
435 /* Declared with fallback == true */
436 if( status != PSA_ERROR_NOT_SUPPORTED )
437 return( status );
438#endif /* PSA_CRYPTO_DRIVER_TEST */
439#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
440 /* Fell through, meaning no accelerator supports this operation */
441 return( psa_import_key_into_slot( attributes,
442 data, data_length,
443 key_buffer, key_buffer_size,
444 key_buffer_length, bits ) );
445
446 default:
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100447 /* Importing a key with external storage in not yet supported.
448 * Return in error indicating that the lifetime is not valid. */
Ronald Cronbf33c932020-11-28 18:06:53 +0100449 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100450 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100451 }
452
Steven Cooreman04524762020-10-13 17:43:44 +0200453}
454
Ronald Cron67227982020-11-26 15:16:05 +0100455psa_status_t psa_driver_wrapper_export_key(
456 const psa_key_attributes_t *attributes,
457 const uint8_t *key_buffer, size_t key_buffer_size,
458 uint8_t *data, size_t data_size, size_t *data_length )
459
460{
461 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
462 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
463 psa_get_key_lifetime( attributes ) );
464
Ronald Cron152e3f82020-11-26 16:06:41 +0100465 /* Try dynamically-registered SE interface first */
466#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
467 const psa_drv_se_t *drv;
468 psa_drv_se_context_t *drv_context;
469
470 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
471 {
472 if( ( drv->key_management == NULL ) ||
473 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100474 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100475 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100476 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100477
478 return( drv->key_management->p_export(
479 drv_context,
480 *( (psa_key_slot_number_t *)key_buffer ),
481 data, data_size, data_length ) );
482 }
483#endif /* PSA_CRYPTO_SE_C */
484
Ronald Cron67227982020-11-26 15:16:05 +0100485 switch( location )
486 {
487 case PSA_KEY_LOCATION_LOCAL_STORAGE:
488 return( psa_export_key_internal( attributes,
489 key_buffer,
490 key_buffer_size,
491 data,
492 data_size,
493 data_length ) );
494
495 /* Add cases for opaque driver here */
496#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
497#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100498 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron67227982020-11-26 15:16:05 +0100499 return( test_opaque_export_key( attributes,
500 key_buffer,
501 key_buffer_size,
502 data,
503 data_size,
504 data_length ) );
505#endif /* PSA_CRYPTO_DRIVER_TEST */
506#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
507 default:
508 /* Key is declared with a lifetime not known to us */
509 return( status );
510 }
511}
512
Ronald Cron84cc9942020-11-25 14:30:05 +0100513psa_status_t psa_driver_wrapper_export_public_key(
514 const psa_key_attributes_t *attributes,
515 const uint8_t *key_buffer, size_t key_buffer_size,
516 uint8_t *data, size_t data_size, size_t *data_length )
517
Steven Cooremanb9b84422020-10-14 14:39:20 +0200518{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200519 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100520 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
521 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200522
Ronald Cron152e3f82020-11-26 16:06:41 +0100523 /* Try dynamically-registered SE interface first */
524#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
525 const psa_drv_se_t *drv;
526 psa_drv_se_context_t *drv_context;
527
528 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
529 {
530 if( ( drv->key_management == NULL ) ||
531 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100532 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100533 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100534 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100535
536 return( drv->key_management->p_export_public(
537 drv_context,
538 *( (psa_key_slot_number_t *)key_buffer ),
539 data, data_size, data_length ) );
540 }
541#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
542
Steven Cooremanb9b84422020-10-14 14:39:20 +0200543 switch( location )
544 {
545 case PSA_KEY_LOCATION_LOCAL_STORAGE:
546 /* Key is stored in the slot in export representation, so
547 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100548#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200549#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron84cc9942020-11-25 14:30:05 +0100550 status = test_transparent_export_public_key( attributes,
551 key_buffer,
552 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200553 data,
554 data_size,
555 data_length );
556 /* Declared with fallback == true */
557 if( status != PSA_ERROR_NOT_SUPPORTED )
558 return( status );
559#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100560#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200561 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100562 return( psa_export_public_key_internal( attributes,
563 key_buffer,
564 key_buffer_size,
565 data,
566 data_size,
567 data_length ) );
568
Steven Cooremanb9b84422020-10-14 14:39:20 +0200569 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100570#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200571#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100572 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron84cc9942020-11-25 14:30:05 +0100573 return( test_opaque_export_public_key( attributes,
574 key_buffer,
575 key_buffer_size,
Steven Cooremanb9b84422020-10-14 14:39:20 +0200576 data,
577 data_size,
578 data_length ) );
579#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100580#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200581 default:
582 /* Key is declared with a lifetime not known to us */
583 return( status );
584 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200585}
586
Steven Cooreman6801f082021-02-19 17:21:22 +0100587psa_status_t psa_driver_wrapper_get_builtin_key(
588 psa_drv_slot_number_t slot_number,
589 psa_key_attributes_t *attributes,
590 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
591{
592 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
593 switch( location )
594 {
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100595#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100596 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100597 return( test_opaque_get_builtin_key(
598 slot_number,
599 attributes,
600 key_buffer, key_buffer_size, key_buffer_length ) );
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100601#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman6801f082021-02-19 17:21:22 +0100602 default:
603 (void) slot_number;
604 (void) key_buffer;
605 (void) key_buffer_size;
606 (void) key_buffer_length;
607 return( PSA_ERROR_DOES_NOT_EXIST );
608 }
609}
610
Steven Cooreman37941cb2020-07-28 18:49:51 +0200611/*
612 * Cipher functions
613 */
614psa_status_t psa_driver_wrapper_cipher_encrypt(
615 psa_key_slot_t *slot,
616 psa_algorithm_t alg,
617 const uint8_t *input,
618 size_t input_length,
619 uint8_t *output,
620 size_t output_size,
621 size_t *output_length )
622{
623#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
624 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
625 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
626 psa_key_attributes_t attributes = {
627 .core = slot->attr
628 };
629
630 switch( location )
631 {
632 case PSA_KEY_LOCATION_LOCAL_STORAGE:
633 /* Key is stored in the slot in export representation, so
634 * cycle through all known transparent accelerators */
635#if defined(PSA_CRYPTO_DRIVER_TEST)
636 status = test_transparent_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100637 slot->key.data,
638 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200639 alg,
640 input,
641 input_length,
642 output,
643 output_size,
644 output_length );
645 /* Declared with fallback == true */
646 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200647 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200648#endif /* PSA_CRYPTO_DRIVER_TEST */
649 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200650 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200651 /* Add cases for opaque driver here */
652#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100653 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman37941cb2020-07-28 18:49:51 +0200654 return( test_opaque_cipher_encrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100655 slot->key.data,
656 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200657 alg,
658 input,
659 input_length,
660 output,
661 output_size,
662 output_length ) );
663#endif /* PSA_CRYPTO_DRIVER_TEST */
664 default:
665 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200666 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200667 }
668#else /* PSA_CRYPTO_DRIVER_PRESENT */
669 (void) slot;
670 (void) alg;
671 (void) input;
672 (void) input_length;
673 (void) output;
674 (void) output_size;
675 (void) output_length;
676
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200677 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200678#endif /* PSA_CRYPTO_DRIVER_PRESENT */
679}
680
681psa_status_t psa_driver_wrapper_cipher_decrypt(
682 psa_key_slot_t *slot,
683 psa_algorithm_t alg,
684 const uint8_t *input,
685 size_t input_length,
686 uint8_t *output,
687 size_t output_size,
688 size_t *output_length )
689{
690#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
691 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
692 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
693 psa_key_attributes_t attributes = {
694 .core = slot->attr
695 };
696
697 switch( location )
698 {
699 case PSA_KEY_LOCATION_LOCAL_STORAGE:
700 /* Key is stored in the slot in export representation, so
701 * cycle through all known transparent accelerators */
702#if defined(PSA_CRYPTO_DRIVER_TEST)
703 status = test_transparent_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100704 slot->key.data,
705 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200706 alg,
707 input,
708 input_length,
709 output,
710 output_size,
711 output_length );
712 /* Declared with fallback == true */
713 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200714 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200715#endif /* PSA_CRYPTO_DRIVER_TEST */
716 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200717 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200718 /* Add cases for opaque driver here */
719#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100720 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman37941cb2020-07-28 18:49:51 +0200721 return( test_opaque_cipher_decrypt( &attributes,
Ronald Cronea0f8a62020-11-25 17:52:23 +0100722 slot->key.data,
723 slot->key.bytes,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200724 alg,
725 input,
726 input_length,
727 output,
728 output_size,
729 output_length ) );
730#endif /* PSA_CRYPTO_DRIVER_TEST */
731 default:
732 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200733 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200734 }
735#else /* PSA_CRYPTO_DRIVER_PRESENT */
736 (void) slot;
737 (void) alg;
738 (void) input;
739 (void) input_length;
740 (void) output;
741 (void) output_size;
742 (void) output_length;
743
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200744 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200745#endif /* PSA_CRYPTO_DRIVER_PRESENT */
746}
747
748psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100749 psa_cipher_operation_t *operation,
750 const psa_key_attributes_t *attributes,
751 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200752 psa_algorithm_t alg )
753{
Ronald Cron0b805592020-12-14 18:08:20 +0100754 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona4af55f2020-12-14 14:36:06 +0100755 psa_key_location_t location =
756 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200757
Steven Cooreman37941cb2020-07-28 18:49:51 +0200758 switch( location )
759 {
760 case PSA_KEY_LOCATION_LOCAL_STORAGE:
761 /* Key is stored in the slot in export representation, so
762 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100763#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200764#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100765 status = test_transparent_cipher_encrypt_setup(
766 &operation->ctx.transparent_test_driver_ctx,
767 attributes,
768 key_buffer,
769 key_buffer_size,
770 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200771 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200772 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100773 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +0200774
Ronald Cron0b805592020-12-14 18:08:20 +0100775 if( status != PSA_ERROR_NOT_SUPPORTED )
776 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200777#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100778#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100779#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200780 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +0100781 status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100782 attributes,
783 key_buffer,
784 key_buffer_size,
785 alg );
786 if( status == PSA_SUCCESS )
Ronald Cron6e412a72021-03-10 09:58:47 +0100787 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Ronald Cron49fafa92021-03-10 08:34:23 +0100788
Ronald Cron7b4154d2021-03-19 14:49:41 +0100789 if( status != PSA_ERROR_NOT_SUPPORTED )
790 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100791#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
792 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron0b805592020-12-14 18:08:20 +0100793
Steven Cooreman37941cb2020-07-28 18:49:51 +0200794 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100795#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200796#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100797 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100798 status = test_opaque_cipher_encrypt_setup(
799 &operation->ctx.opaque_test_driver_ctx,
800 attributes,
801 key_buffer, key_buffer_size,
802 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200803
Steven Cooreman37941cb2020-07-28 18:49:51 +0200804 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100805 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200806
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200807 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200808#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100809#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +0200810 default:
811 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +0100812 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100813 (void)key_buffer;
814 (void)key_buffer_size;
815 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +0200816 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200817 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200818}
819
820psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100821 psa_cipher_operation_t *operation,
822 const psa_key_attributes_t *attributes,
823 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200824 psa_algorithm_t alg )
825{
Steven Cooreman37941cb2020-07-28 18:49:51 +0200826 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crona4af55f2020-12-14 14:36:06 +0100827 psa_key_location_t location =
828 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200829
Steven Cooreman37941cb2020-07-28 18:49:51 +0200830 switch( location )
831 {
832 case PSA_KEY_LOCATION_LOCAL_STORAGE:
833 /* Key is stored in the slot in export representation, so
834 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100835#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200836#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100837 status = test_transparent_cipher_decrypt_setup(
838 &operation->ctx.transparent_test_driver_ctx,
839 attributes,
840 key_buffer,
841 key_buffer_size,
842 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200843 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200844 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100845 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +0200846
Ronald Cron0b805592020-12-14 18:08:20 +0100847 if( status != PSA_ERROR_NOT_SUPPORTED )
848 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200849#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100850#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100851#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200852 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +0100853 status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100854 attributes,
855 key_buffer,
856 key_buffer_size,
857 alg );
858 if( status == PSA_SUCCESS )
859 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
860
861 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100862#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
863 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron49fafa92021-03-10 08:34:23 +0100864
Steven Cooreman37941cb2020-07-28 18:49:51 +0200865 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100866#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200867#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100868 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100869 status = test_opaque_cipher_decrypt_setup(
870 &operation->ctx.opaque_test_driver_ctx,
871 attributes,
872 key_buffer, key_buffer_size,
873 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200874
Steven Cooreman37941cb2020-07-28 18:49:51 +0200875 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100876 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200877
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200878 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200879#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100880#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +0200881 default:
882 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +0100883 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100884 (void)key_buffer;
885 (void)key_buffer_size;
886 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +0200887 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200888 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200889}
890
Steven Cooreman37941cb2020-07-28 18:49:51 +0200891psa_status_t psa_driver_wrapper_cipher_set_iv(
Ronald Cron6056fe82020-12-15 13:58:07 +0100892 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200893 const uint8_t *iv,
894 size_t iv_length )
895{
Ronald Cron49fafa92021-03-10 08:34:23 +0100896 switch( operation->id )
897 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100898#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +0100899 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +0100900 return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100901 iv,
902 iv_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100903#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100904
905#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200906#if defined(PSA_CRYPTO_DRIVER_TEST)
907 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100908 return( test_transparent_cipher_set_iv(
909 &operation->ctx.transparent_test_driver_ctx,
910 iv, iv_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +0100911
Steven Cooreman37941cb2020-07-28 18:49:51 +0200912 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100913 return( test_opaque_cipher_set_iv(
914 &operation->ctx.opaque_test_driver_ctx,
915 iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200916#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100917#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +0100918 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200919
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100920 (void)iv;
921 (void)iv_length;
922
Ronald Crondd24c9b2020-12-15 14:10:01 +0100923 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200924}
925
926psa_status_t psa_driver_wrapper_cipher_update(
Ronald Cron6056fe82020-12-15 13:58:07 +0100927 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200928 const uint8_t *input,
929 size_t input_length,
930 uint8_t *output,
931 size_t output_size,
932 size_t *output_length )
933{
Ronald Cron49fafa92021-03-10 08:34:23 +0100934 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200935 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100936#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +0100937 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +0100938 return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100939 input,
940 input_length,
941 output,
942 output_size,
943 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100944#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
945
Ronald Cron49fafa92021-03-10 08:34:23 +0100946#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200947#if defined(PSA_CRYPTO_DRIVER_TEST)
948 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100949 return( test_transparent_cipher_update(
950 &operation->ctx.transparent_test_driver_ctx,
951 input, input_length,
952 output, output_size, output_length ) );
953
Steven Cooreman37941cb2020-07-28 18:49:51 +0200954 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100955 return( test_opaque_cipher_update(
956 &operation->ctx.opaque_test_driver_ctx,
957 input, input_length,
958 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200959#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100960#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +0100961 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200962
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100963 (void)input;
964 (void)input_length;
965 (void)output;
966 (void)output_size;
967 (void)output_length;
968
Ronald Crondd24c9b2020-12-15 14:10:01 +0100969 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200970}
971
972psa_status_t psa_driver_wrapper_cipher_finish(
Ronald Cron6056fe82020-12-15 13:58:07 +0100973 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200974 uint8_t *output,
975 size_t output_size,
976 size_t *output_length )
977{
Ronald Cron49fafa92021-03-10 08:34:23 +0100978 switch( operation->id )
979 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100980#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +0100981 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +0100982 return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100983 output,
984 output_size,
985 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100986#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100987
988#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200989#if defined(PSA_CRYPTO_DRIVER_TEST)
990 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100991 return( test_transparent_cipher_finish(
992 &operation->ctx.transparent_test_driver_ctx,
993 output, output_size, output_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +0100994
Steven Cooreman37941cb2020-07-28 18:49:51 +0200995 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100996 return( test_opaque_cipher_finish(
997 &operation->ctx.opaque_test_driver_ctx,
998 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200999#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001000#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001001 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001002
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001003 (void)output;
1004 (void)output_size;
1005 (void)output_length;
1006
Ronald Crondd24c9b2020-12-15 14:10:01 +01001007 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001008}
1009
1010psa_status_t psa_driver_wrapper_cipher_abort(
Ronald Cron6056fe82020-12-15 13:58:07 +01001011 psa_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001012{
Ronald Crondd24c9b2020-12-15 14:10:01 +01001013 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001014
Ronald Cron49fafa92021-03-10 08:34:23 +01001015 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001016 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001017#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001018 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001019 return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001020#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +01001021
1022#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001023#if defined(PSA_CRYPTO_DRIVER_TEST)
1024 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001025 status = test_transparent_cipher_abort(
1026 &operation->ctx.transparent_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001027 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001028 &operation->ctx.transparent_test_driver_ctx,
1029 sizeof( operation->ctx.transparent_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001030 return( status );
Ronald Cron49fafa92021-03-10 08:34:23 +01001031
Steven Cooreman37941cb2020-07-28 18:49:51 +02001032 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001033 status = test_opaque_cipher_abort(
1034 &operation->ctx.opaque_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001035 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001036 &operation->ctx.opaque_test_driver_ctx,
1037 sizeof( operation->ctx.opaque_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001038 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001039#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001040#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001041 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001042
Ronald Cron49fafa92021-03-10 08:34:23 +01001043 (void)status;
Ronald Crondd24c9b2020-12-15 14:10:01 +01001044 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001045}
1046
Steven Cooreman1e582352021-02-18 17:24:37 +01001047/*
1048 * Hashing functions
1049 */
1050psa_status_t psa_driver_wrapper_hash_compute(
1051 psa_algorithm_t alg,
1052 const uint8_t *input,
1053 size_t input_length,
1054 uint8_t *hash,
1055 size_t hash_size,
1056 size_t *hash_length)
1057{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001058 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001059
1060 /* Try accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001061#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001062 status = mbedtls_transparent_test_driver_hash_compute(
1063 alg, input, input_length, hash, hash_size, hash_length );
Steven Cooremanf7638102021-03-04 15:14:36 +01001064 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)
1070 status = mbedtls_psa_hash_compute( alg, input, input_length,
1071 hash, hash_size, hash_length );
1072 if( status != PSA_ERROR_NOT_SUPPORTED )
1073 return( status );
1074#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001075 (void) status;
1076 (void) alg;
1077 (void) input;
1078 (void) input_length;
1079 (void) hash;
1080 (void) hash_size;
1081 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001082
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001083 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman1e582352021-02-18 17:24:37 +01001084}
1085
1086psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001087 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001088 psa_algorithm_t alg )
1089{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001090 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001091
Steven Cooreman1e582352021-02-18 17:24:37 +01001092 /* Try setup on accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001093#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001094 status = mbedtls_transparent_test_driver_hash_setup(
1095 &operation->ctx.test_driver_ctx, alg );
Steven Cooremanf7638102021-03-04 15:14:36 +01001096 if( status == PSA_SUCCESS )
1097 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1098
1099 if( status != PSA_ERROR_NOT_SUPPORTED )
1100 return( status );
1101#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001102
1103 /* If software fallback is compiled in, try fallback */
1104#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001105 status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
Steven Cooreman1e582352021-02-18 17:24:37 +01001106 if( status == PSA_SUCCESS )
Steven Cooreman1e582352021-02-18 17:24:37 +01001107 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Steven Cooreman1e582352021-02-18 17:24:37 +01001108
1109 if( status != PSA_ERROR_NOT_SUPPORTED )
1110 return( status );
1111#endif
1112 /* Nothing left to try if we fall through here */
1113 (void) status;
1114 (void) operation;
1115 (void) alg;
1116 return( PSA_ERROR_NOT_SUPPORTED );
1117}
1118
1119psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001120 const psa_hash_operation_t *source_operation,
1121 psa_hash_operation_t *target_operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001122{
Steven Cooreman1e582352021-02-18 17:24:37 +01001123 switch( source_operation->id )
1124 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001125#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1126 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1127 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1128 return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
1129 &target_operation->ctx.mbedtls_ctx ) );
1130#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001131#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001132 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1133 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001134 return( mbedtls_transparent_test_driver_hash_clone(
1135 &source_operation->ctx.test_driver_ctx,
1136 &target_operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001137#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001138 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001139 (void) target_operation;
1140 return( PSA_ERROR_BAD_STATE );
1141 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001142}
1143
1144psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001145 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001146 const uint8_t *input,
1147 size_t input_length )
1148{
Steven Cooreman1e582352021-02-18 17:24:37 +01001149 switch( operation->id )
1150 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001151#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1152 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1153 return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
1154 input, input_length ) );
1155#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001156#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001157 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001158 return( mbedtls_transparent_test_driver_hash_update(
1159 &operation->ctx.test_driver_ctx,
1160 input, input_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001161#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001162 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001163 (void) input;
1164 (void) input_length;
1165 return( PSA_ERROR_BAD_STATE );
1166 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001167}
1168
1169psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001170 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001171 uint8_t *hash,
1172 size_t hash_size,
1173 size_t *hash_length )
1174{
Steven Cooreman1e582352021-02-18 17:24:37 +01001175 switch( operation->id )
1176 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001177#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1178 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1179 return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
1180 hash, hash_size, hash_length ) );
1181#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001182#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001183 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001184 return( mbedtls_transparent_test_driver_hash_finish(
1185 &operation->ctx.test_driver_ctx,
1186 hash, hash_size, hash_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001187#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001188 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001189 (void) hash;
1190 (void) hash_size;
1191 (void) hash_length;
1192 return( PSA_ERROR_BAD_STATE );
1193 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001194}
1195
1196psa_status_t psa_driver_wrapper_hash_abort(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001197 psa_hash_operation_t *operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001198{
Steven Cooreman1e582352021-02-18 17:24:37 +01001199 switch( operation->id )
1200 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001201#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1202 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1203 return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
1204#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001205#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001206 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001207 return( mbedtls_transparent_test_driver_hash_abort(
1208 &operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001209#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001210 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001211 return( PSA_ERROR_BAD_STATE );
1212 }
1213}
1214
Ronald Cronde822812021-03-17 16:08:20 +01001215psa_status_t psa_driver_wrapper_aead_encrypt(
1216 const psa_key_attributes_t *attributes,
1217 const uint8_t *key_buffer, size_t key_buffer_size,
1218 psa_algorithm_t alg,
1219 const uint8_t *nonce, size_t nonce_length,
1220 const uint8_t *additional_data, size_t additional_data_length,
1221 const uint8_t *plaintext, size_t plaintext_length,
1222 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
1223{
1224 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1225 psa_key_location_t location =
1226 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1227
1228 switch( location )
1229 {
1230 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1231 /* Key is stored in the slot in export representation, so
1232 * cycle through all known transparent accelerators */
1233
1234#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1235#if defined(PSA_CRYPTO_DRIVER_TEST)
1236 status = test_transparent_aead_encrypt(
1237 attributes, key_buffer, key_buffer_size,
1238 alg,
1239 nonce, nonce_length,
1240 additional_data, additional_data_length,
1241 plaintext, plaintext_length,
1242 ciphertext, ciphertext_size, ciphertext_length );
1243 /* Declared with fallback == true */
1244 if( status != PSA_ERROR_NOT_SUPPORTED )
1245 return( status );
1246#endif /* PSA_CRYPTO_DRIVER_TEST */
1247#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1248
1249 /* Fell through, meaning no accelerator supports this operation */
1250 return( mbedtls_psa_aead_encrypt(
1251 attributes, key_buffer, key_buffer_size,
1252 alg,
1253 nonce, nonce_length,
1254 additional_data, additional_data_length,
1255 plaintext, plaintext_length,
1256 ciphertext, ciphertext_size, ciphertext_length ) );
1257
1258 /* Add cases for opaque driver here */
1259
1260 default:
1261 /* Key is declared with a lifetime not known to us */
1262 (void)status;
1263 return( PSA_ERROR_INVALID_ARGUMENT );
1264 }
1265}
1266
1267psa_status_t psa_driver_wrapper_aead_decrypt(
1268 const psa_key_attributes_t *attributes,
1269 const uint8_t *key_buffer, size_t key_buffer_size,
1270 psa_algorithm_t alg,
1271 const uint8_t *nonce, size_t nonce_length,
1272 const uint8_t *additional_data, size_t additional_data_length,
1273 const uint8_t *ciphertext, size_t ciphertext_length,
1274 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
1275{
1276 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1277 psa_key_location_t location =
1278 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1279
1280 switch( location )
1281 {
1282 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1283 /* Key is stored in the slot in export representation, so
1284 * cycle through all known transparent accelerators */
1285
1286#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1287#if defined(PSA_CRYPTO_DRIVER_TEST)
1288 status = test_transparent_aead_decrypt(
1289 attributes, key_buffer, key_buffer_size,
1290 alg,
1291 nonce, nonce_length,
1292 additional_data, additional_data_length,
1293 ciphertext, ciphertext_length,
1294 plaintext, plaintext_size, plaintext_length );
1295 /* Declared with fallback == true */
1296 if( status != PSA_ERROR_NOT_SUPPORTED )
1297 return( status );
1298#endif /* PSA_CRYPTO_DRIVER_TEST */
1299#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1300
1301 /* Fell through, meaning no accelerator supports this operation */
1302 return( mbedtls_psa_aead_decrypt(
1303 attributes, key_buffer, key_buffer_size,
1304 alg,
1305 nonce, nonce_length,
1306 additional_data, additional_data_length,
1307 ciphertext, ciphertext_length,
1308 plaintext, plaintext_size, plaintext_length ) );
1309
1310 /* Add cases for opaque driver here */
1311
1312 default:
1313 /* Key is declared with a lifetime not known to us */
1314 (void)status;
1315 return( PSA_ERROR_INVALID_ARGUMENT );
1316 }
1317}
Steven Cooremancd84cb42020-07-16 20:28:36 +02001318/* End of automatically generated file. */