blob: 795e424894ef47874572d0aa22e391892199ae30 [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"
Steven Cooreman896d51e2021-03-19 15:24:23 +010027#include "psa_crypto_mac.h"
Steven Cooreman830aff22021-03-09 09:50:44 +010028
Steven Cooreman2a1664c2020-07-20 15:33:08 +020029#include "mbedtls/platform.h"
30
31#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
Steven Cooremancd84cb42020-07-16 20:28:36 +020032
33/* Include test driver definition when running tests */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020034#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman56250fd2020-09-04 13:07:15 +020035#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020036#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020037#endif
38#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020039#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020040#endif
Ronald Cron05ee58d2021-04-30 17:00:34 +020041#include "test/drivers/test_driver.h"
Steven Cooremanf1720ea2020-07-24 18:41:58 +020042#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +020043
Steven Cooreman56250fd2020-09-04 13:07:15 +020044/* Repeat above block for each JSON-declared driver during autogeneration */
Steven Cooremanaa87fd02021-03-15 18:54:03 +010045#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Steven Cooreman56250fd2020-09-04 13:07:15 +020046
Steven Cooreman1e582352021-02-18 17:24:37 +010047/* Auto-generated values depending on which drivers are registered.
48 * ID 0 is reserved for unallocated operations.
49 * ID 1 is reserved for the Mbed TLS software driver. */
Steven Cooreman830aff22021-03-09 09:50:44 +010050#define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
51
Steven Cooreman37941cb2020-07-28 18:49:51 +020052#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman1e582352021-02-18 17:24:37 +010053#define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2)
54#define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3)
Steven Cooreman37941cb2020-07-28 18:49:51 +020055#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +020056
57/* Support the 'old' SE interface when asked to */
Steven Cooreman7a250572020-07-17 16:43:05 +020058#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman56250fd2020-09-04 13:07:15 +020059/* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
60 * SE driver is present, to avoid unused argument errors at compile time. */
61#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020062#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020063#endif
Steven Cooreman7a250572020-07-17 16:43:05 +020064#include "psa_crypto_se.h"
65#endif
66
Steven Cooremancd84cb42020-07-16 20:28:36 +020067/* Start delegation functions */
Ronald Cron9f17aa42020-12-08 17:07:25 +010068psa_status_t psa_driver_wrapper_sign_hash(
69 const psa_key_attributes_t *attributes,
70 const uint8_t *key_buffer, size_t key_buffer_size,
71 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
72 uint8_t *signature, size_t signature_size, size_t *signature_length )
Steven Cooremancd84cb42020-07-16 20:28:36 +020073{
Steven Cooreman7a250572020-07-17 16:43:05 +020074 /* Try dynamically-registered SE interface first */
75#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
76 const psa_drv_se_t *drv;
77 psa_drv_se_context_t *drv_context;
78
Ronald Cron9f17aa42020-12-08 17:07:25 +010079 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman7a250572020-07-17 16:43:05 +020080 {
81 if( drv->asymmetric == NULL ||
82 drv->asymmetric->p_sign == NULL )
83 {
84 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +020085 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman7a250572020-07-17 16:43:05 +020086 }
Ronald Cron9f17aa42020-12-08 17:07:25 +010087 return( drv->asymmetric->p_sign(
88 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
89 alg, hash, hash_length,
90 signature, signature_size, signature_length ) );
Steven Cooreman7a250572020-07-17 16:43:05 +020091 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +020092#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman7a250572020-07-17 16:43:05 +020093
Ronald Cronfce9df22020-12-08 18:06:03 +010094 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +010095 psa_key_location_t location =
96 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooremancd84cb42020-07-16 20:28:36 +020097
98 switch( location )
99 {
100 case PSA_KEY_LOCATION_LOCAL_STORAGE:
101 /* Key is stored in the slot in export representation, so
102 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100103#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200104#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200105 status = mbedtls_test_transparent_signature_sign_hash( attributes,
Ronald Cron9f17aa42020-12-08 17:07:25 +0100106 key_buffer,
107 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200108 alg,
109 hash,
110 hash_length,
111 signature,
112 signature_size,
113 signature_length );
114 /* Declared with fallback == true */
115 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200116 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200117#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100118#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200119 /* Fell through, meaning no accelerator supports this operation */
Ronald Cronfce9df22020-12-08 18:06:03 +0100120 return( psa_sign_hash_internal( attributes,
121 key_buffer,
122 key_buffer_size,
123 alg,
124 hash,
125 hash_length,
126 signature,
127 signature_size,
128 signature_length ) );
129
Steven Cooremancd84cb42020-07-16 20:28:36 +0200130 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100131#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200132#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100133 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200134 return( mbedtls_test_opaque_signature_sign_hash( attributes,
135 key_buffer,
136 key_buffer_size,
137 alg,
138 hash,
139 hash_length,
140 signature,
141 signature_size,
142 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200143#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100144#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200145 default:
146 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100147 (void)status;
148 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200149 }
Steven Cooremancd84cb42020-07-16 20:28:36 +0200150}
151
Ronald Cron9f17aa42020-12-08 17:07:25 +0100152psa_status_t psa_driver_wrapper_verify_hash(
153 const psa_key_attributes_t *attributes,
154 const uint8_t *key_buffer, size_t key_buffer_size,
155 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
156 const uint8_t *signature, size_t signature_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200157{
Steven Cooreman55ae2172020-07-17 19:46:15 +0200158 /* Try dynamically-registered SE interface first */
159#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
160 const psa_drv_se_t *drv;
161 psa_drv_se_context_t *drv_context;
162
Ronald Cron9f17aa42020-12-08 17:07:25 +0100163 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200164 {
165 if( drv->asymmetric == NULL ||
166 drv->asymmetric->p_verify == NULL )
167 {
168 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200169 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200170 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100171 return( drv->asymmetric->p_verify(
172 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
173 alg, hash, hash_length,
174 signature, signature_length ) );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200175 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200176#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200177
Ronald Cronfce9df22020-12-08 18:06:03 +0100178 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100179 psa_key_location_t location =
180 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200181
182 switch( location )
183 {
184 case PSA_KEY_LOCATION_LOCAL_STORAGE:
185 /* Key is stored in the slot in export representation, so
186 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100187#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200188#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200189 status = mbedtls_test_transparent_signature_verify_hash(
190 attributes,
191 key_buffer,
192 key_buffer_size,
193 alg,
194 hash,
195 hash_length,
196 signature,
197 signature_length );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200198 /* Declared with fallback == true */
199 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200200 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200201#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100202#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
203
204 return( psa_verify_hash_internal( attributes,
205 key_buffer,
206 key_buffer_size,
207 alg,
208 hash,
209 hash_length,
210 signature,
211 signature_length ) );
212
Steven Cooreman55ae2172020-07-17 19:46:15 +0200213 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100214#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200215#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100216 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200217 return( mbedtls_test_opaque_signature_verify_hash( attributes,
218 key_buffer,
219 key_buffer_size,
220 alg,
221 hash,
222 hash_length,
223 signature,
224 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200225#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100226#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200227 default:
228 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100229 (void)status;
230 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200231 }
Steven Cooreman55ae2172020-07-17 19:46:15 +0200232}
233
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200234/** Get the key buffer size required to store the key material of a key
235 * associated with an opaque driver without storage.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200236 *
Ronald Cron31216282020-12-05 18:47:56 +0100237 * \param[in] attributes The key attributes.
238 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200239 *
240 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100241 * The minimum size for a buffer to contain the key material has been
242 * returned successfully.
243 * \retval #PSA_ERROR_INVALID_ARGUMENT
244 * The size in bits of the key is not valid.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200245 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100246 * The type and/or the size in bits of the key or the combination of
247 * the two is not supported.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200248 */
Ronald Cron9df74be2020-12-05 19:15:23 +0100249psa_status_t psa_driver_wrapper_get_key_buffer_size(
Ronald Cron31216282020-12-05 18:47:56 +0100250 const psa_key_attributes_t *attributes,
251 size_t *key_buffer_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200252{
John Durkop2c618352020-09-22 06:54:01 -0700253 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
254 psa_key_type_t key_type = attributes->core.type;
255 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200256
Ronald Cron31216282020-12-05 18:47:56 +0100257 *key_buffer_size = 0;
John Durkop2c618352020-09-22 06:54:01 -0700258 switch( location )
259 {
John Durkop2c618352020-09-22 06:54:01 -0700260#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100261 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100262#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
263 /* Emulate property 'builtin_key_size' */
264 if( psa_key_id_is_builtin(
265 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(
266 psa_get_key_id( attributes ) ) ) )
267 {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100268 *key_buffer_size = sizeof( psa_drv_slot_number_t );
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100269 return( PSA_SUCCESS );
270 }
271#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200272 *key_buffer_size = mbedtls_test_size_function( key_type, key_bits );
Ronald Cronb0737da2021-04-14 10:55:34 +0200273 return( ( *key_buffer_size != 0 ) ?
274 PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
John Durkop2c618352020-09-22 06:54:01 -0700275#endif /* PSA_CRYPTO_DRIVER_TEST */
276
277 default:
Ronald Cron9df74be2020-12-05 19:15:23 +0100278 (void)key_type;
279 (void)key_bits;
Steven Cooreman56250fd2020-09-04 13:07:15 +0200280 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200281 }
282}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200283
Ronald Cron977c2472020-10-13 08:32:21 +0200284psa_status_t psa_driver_wrapper_generate_key(
285 const psa_key_attributes_t *attributes,
286 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200287{
Ronald Cron977c2472020-10-13 08:32:21 +0200288 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
289 psa_key_location_t location =
290 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
291
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200292 /* Try dynamically-registered SE interface first */
293#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
294 const psa_drv_se_t *drv;
295 psa_drv_se_context_t *drv_context;
296
Ronald Cron977c2472020-10-13 08:32:21 +0200297 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200298 {
299 size_t pubkey_length = 0; /* We don't support this feature yet */
300 if( drv->key_management == NULL ||
301 drv->key_management->p_generate == NULL )
302 {
303 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200304 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200305 }
306 return( drv->key_management->p_generate(
Ronald Cron977c2472020-10-13 08:32:21 +0200307 drv_context,
308 *( (psa_key_slot_number_t *)key_buffer ),
Ronald Cronea0f8a62020-11-25 17:52:23 +0100309 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200310 }
311#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
312
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200313 switch( location )
314 {
315 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Ronald Cron977c2472020-10-13 08:32:21 +0200316#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200317 /* Transparent drivers are limited to generating asymmetric keys */
Ronald Cron977c2472020-10-13 08:32:21 +0200318 if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200319 {
Ronald Cron977c2472020-10-13 08:32:21 +0200320 /* Cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200321#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200322 status = mbedtls_test_transparent_generate_key(
Ronald Cron977c2472020-10-13 08:32:21 +0200323 attributes, key_buffer, key_buffer_size,
324 key_buffer_length );
325 /* Declared with fallback == true */
326 if( status != PSA_ERROR_NOT_SUPPORTED )
327 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200328#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200329 }
330#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
331
332 /* Software fallback */
333 status = psa_generate_key_internal(
334 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200335 break;
Ronald Cron977c2472020-10-13 08:32:21 +0200336
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200337 /* Add cases for opaque driver here */
Ronald Cron977c2472020-10-13 08:32:21 +0200338#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200339#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100340 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200341 status = mbedtls_test_opaque_generate_key(
Ronald Cron977c2472020-10-13 08:32:21 +0200342 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200343 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200344#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200345#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
346
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200347 default:
348 /* Key is declared with a lifetime not known to us */
349 status = PSA_ERROR_INVALID_ARGUMENT;
350 break;
351 }
352
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200353 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200354}
355
Ronald Cron83282872020-11-22 14:02:39 +0100356psa_status_t psa_driver_wrapper_import_key(
357 const psa_key_attributes_t *attributes,
358 const uint8_t *data,
359 size_t data_length,
360 uint8_t *key_buffer,
361 size_t key_buffer_size,
362 size_t *key_buffer_length,
363 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200364{
Steven Cooreman04524762020-10-13 17:43:44 +0200365 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100366 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
367 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200368
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100369 /* Try dynamically-registered SE interface first */
370#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
371 const psa_drv_se_t *drv;
372 psa_drv_se_context_t *drv_context;
373
374 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
375 {
376 if( drv->key_management == NULL ||
377 drv->key_management->p_import == NULL )
378 return( PSA_ERROR_NOT_SUPPORTED );
379
380 /* The driver should set the number of key bits, however in
381 * case it doesn't, we initialize bits to an invalid value. */
382 *bits = PSA_MAX_KEY_BITS + 1;
383 status = drv->key_management->p_import(
384 drv_context,
385 *( (psa_key_slot_number_t *)key_buffer ),
386 attributes, data, data_length, bits );
387
388 if( status != PSA_SUCCESS )
389 return( status );
390
391 if( (*bits) > PSA_MAX_KEY_BITS )
392 return( PSA_ERROR_NOT_SUPPORTED );
393
394 return( PSA_SUCCESS );
395 }
396#endif /* PSA_CRYPTO_SE_C */
397
Ronald Cronbf33c932020-11-28 18:06:53 +0100398 switch( location )
399 {
400 case PSA_KEY_LOCATION_LOCAL_STORAGE:
401 /* Key is stored in the slot in export representation, so
402 * cycle through all known transparent accelerators */
403#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
404#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200405 status = mbedtls_test_transparent_import_key(
406 attributes,
407 data, data_length,
408 key_buffer, key_buffer_size,
409 key_buffer_length, bits );
Ronald Cronbf33c932020-11-28 18:06:53 +0100410 /* Declared with fallback == true */
411 if( status != PSA_ERROR_NOT_SUPPORTED )
412 return( status );
413#endif /* PSA_CRYPTO_DRIVER_TEST */
414#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
415 /* Fell through, meaning no accelerator supports this operation */
416 return( psa_import_key_into_slot( attributes,
417 data, data_length,
418 key_buffer, key_buffer_size,
419 key_buffer_length, bits ) );
420
421 default:
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100422 /* Importing a key with external storage in not yet supported.
423 * Return in error indicating that the lifetime is not valid. */
Ronald Cronbf33c932020-11-28 18:06:53 +0100424 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100425 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100426 }
427
Steven Cooreman04524762020-10-13 17:43:44 +0200428}
429
Ronald Cron67227982020-11-26 15:16:05 +0100430psa_status_t psa_driver_wrapper_export_key(
431 const psa_key_attributes_t *attributes,
432 const uint8_t *key_buffer, size_t key_buffer_size,
433 uint8_t *data, size_t data_size, size_t *data_length )
434
435{
436 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
437 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
438 psa_get_key_lifetime( attributes ) );
439
Ronald Cron152e3f82020-11-26 16:06:41 +0100440 /* Try dynamically-registered SE interface first */
441#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
442 const psa_drv_se_t *drv;
443 psa_drv_se_context_t *drv_context;
444
445 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
446 {
447 if( ( drv->key_management == NULL ) ||
448 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100449 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100450 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100451 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100452
453 return( drv->key_management->p_export(
454 drv_context,
455 *( (psa_key_slot_number_t *)key_buffer ),
456 data, data_size, data_length ) );
457 }
458#endif /* PSA_CRYPTO_SE_C */
459
Ronald Cron67227982020-11-26 15:16:05 +0100460 switch( location )
461 {
462 case PSA_KEY_LOCATION_LOCAL_STORAGE:
463 return( psa_export_key_internal( attributes,
464 key_buffer,
465 key_buffer_size,
466 data,
467 data_size,
468 data_length ) );
469
470 /* Add cases for opaque driver here */
471#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
472#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100473 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200474 return( mbedtls_test_opaque_export_key( attributes,
475 key_buffer,
476 key_buffer_size,
477 data,
478 data_size,
479 data_length ) );
Ronald Cron67227982020-11-26 15:16:05 +0100480#endif /* PSA_CRYPTO_DRIVER_TEST */
481#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
482 default:
483 /* Key is declared with a lifetime not known to us */
484 return( status );
485 }
486}
487
Ronald Cron84cc9942020-11-25 14:30:05 +0100488psa_status_t psa_driver_wrapper_export_public_key(
489 const psa_key_attributes_t *attributes,
490 const uint8_t *key_buffer, size_t key_buffer_size,
491 uint8_t *data, size_t data_size, size_t *data_length )
492
Steven Cooremanb9b84422020-10-14 14:39:20 +0200493{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200494 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100495 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
496 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200497
Ronald Cron152e3f82020-11-26 16:06:41 +0100498 /* Try dynamically-registered SE interface first */
499#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
500 const psa_drv_se_t *drv;
501 psa_drv_se_context_t *drv_context;
502
503 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
504 {
505 if( ( drv->key_management == NULL ) ||
506 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100507 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100508 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100509 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100510
511 return( drv->key_management->p_export_public(
512 drv_context,
513 *( (psa_key_slot_number_t *)key_buffer ),
514 data, data_size, data_length ) );
515 }
516#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
517
Steven Cooremanb9b84422020-10-14 14:39:20 +0200518 switch( location )
519 {
520 case PSA_KEY_LOCATION_LOCAL_STORAGE:
521 /* Key is stored in the slot in export representation, so
522 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100523#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200524#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200525 status = mbedtls_test_transparent_export_public_key(
526 attributes,
527 key_buffer,
528 key_buffer_size,
529 data,
530 data_size,
531 data_length );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200532 /* Declared with fallback == true */
533 if( status != PSA_ERROR_NOT_SUPPORTED )
534 return( status );
535#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100536#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200537 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100538 return( psa_export_public_key_internal( attributes,
539 key_buffer,
540 key_buffer_size,
541 data,
542 data_size,
543 data_length ) );
544
Steven Cooremanb9b84422020-10-14 14:39:20 +0200545 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100546#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200547#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100548 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200549 return( mbedtls_test_opaque_export_public_key( attributes,
550 key_buffer,
551 key_buffer_size,
552 data,
553 data_size,
554 data_length ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200555#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100556#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200557 default:
558 /* Key is declared with a lifetime not known to us */
559 return( status );
560 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200561}
562
Steven Cooreman6801f082021-02-19 17:21:22 +0100563psa_status_t psa_driver_wrapper_get_builtin_key(
564 psa_drv_slot_number_t slot_number,
565 psa_key_attributes_t *attributes,
566 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
567{
568 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
569 switch( location )
570 {
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100571#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100572 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200573 return( mbedtls_test_opaque_get_builtin_key(
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100574 slot_number,
575 attributes,
576 key_buffer, key_buffer_size, key_buffer_length ) );
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100577#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman6801f082021-02-19 17:21:22 +0100578 default:
579 (void) slot_number;
580 (void) key_buffer;
581 (void) key_buffer_size;
582 (void) key_buffer_length;
583 return( PSA_ERROR_DOES_NOT_EXIST );
584 }
585}
586
Steven Cooreman37941cb2020-07-28 18:49:51 +0200587/*
588 * Cipher functions
589 */
590psa_status_t psa_driver_wrapper_cipher_encrypt(
591 psa_key_slot_t *slot,
592 psa_algorithm_t alg,
593 const uint8_t *input,
594 size_t input_length,
595 uint8_t *output,
596 size_t output_size,
597 size_t *output_length )
598{
599#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
600 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
601 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
602 psa_key_attributes_t attributes = {
603 .core = slot->attr
604 };
605
606 switch( location )
607 {
608 case PSA_KEY_LOCATION_LOCAL_STORAGE:
609 /* Key is stored in the slot in export representation, so
610 * cycle through all known transparent accelerators */
611#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200612 status = mbedtls_test_transparent_cipher_encrypt( &attributes,
613 slot->key.data,
614 slot->key.bytes,
615 alg,
616 input,
617 input_length,
618 output,
619 output_size,
620 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200621 /* Declared with fallback == true */
622 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200623 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200624#endif /* PSA_CRYPTO_DRIVER_TEST */
625 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200626 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200627 /* Add cases for opaque driver here */
628#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100629 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200630 return( mbedtls_test_opaque_cipher_encrypt( &attributes,
631 slot->key.data,
632 slot->key.bytes,
633 alg,
634 input,
635 input_length,
636 output,
637 output_size,
638 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200639#endif /* PSA_CRYPTO_DRIVER_TEST */
640 default:
641 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200642 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200643 }
644#else /* PSA_CRYPTO_DRIVER_PRESENT */
645 (void) slot;
646 (void) alg;
647 (void) input;
648 (void) input_length;
649 (void) output;
650 (void) output_size;
651 (void) output_length;
652
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200653 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200654#endif /* PSA_CRYPTO_DRIVER_PRESENT */
655}
656
657psa_status_t psa_driver_wrapper_cipher_decrypt(
658 psa_key_slot_t *slot,
659 psa_algorithm_t alg,
660 const uint8_t *input,
661 size_t input_length,
662 uint8_t *output,
663 size_t output_size,
664 size_t *output_length )
665{
666#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
667 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
668 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
669 psa_key_attributes_t attributes = {
670 .core = slot->attr
671 };
672
673 switch( location )
674 {
675 case PSA_KEY_LOCATION_LOCAL_STORAGE:
676 /* Key is stored in the slot in export representation, so
677 * cycle through all known transparent accelerators */
678#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200679 status = mbedtls_test_transparent_cipher_decrypt( &attributes,
680 slot->key.data,
681 slot->key.bytes,
682 alg,
683 input,
684 input_length,
685 output,
686 output_size,
687 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200688 /* Declared with fallback == true */
689 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200690 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200691#endif /* PSA_CRYPTO_DRIVER_TEST */
692 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200693 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200694 /* Add cases for opaque driver here */
695#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100696 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200697 return( mbedtls_test_opaque_cipher_decrypt( &attributes,
698 slot->key.data,
699 slot->key.bytes,
700 alg,
701 input,
702 input_length,
703 output,
704 output_size,
705 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200706#endif /* PSA_CRYPTO_DRIVER_TEST */
707 default:
708 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200709 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200710 }
711#else /* PSA_CRYPTO_DRIVER_PRESENT */
712 (void) slot;
713 (void) alg;
714 (void) input;
715 (void) input_length;
716 (void) output;
717 (void) output_size;
718 (void) output_length;
719
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200720 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200721#endif /* PSA_CRYPTO_DRIVER_PRESENT */
722}
723
724psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100725 psa_cipher_operation_t *operation,
726 const psa_key_attributes_t *attributes,
727 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200728 psa_algorithm_t alg )
729{
Ronald Cron0b805592020-12-14 18:08:20 +0100730 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona4af55f2020-12-14 14:36:06 +0100731 psa_key_location_t location =
732 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200733
Steven Cooreman37941cb2020-07-28 18:49:51 +0200734 switch( location )
735 {
736 case PSA_KEY_LOCATION_LOCAL_STORAGE:
737 /* Key is stored in the slot in export representation, so
738 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100739#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200740#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200741 status = mbedtls_test_transparent_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100742 &operation->ctx.transparent_test_driver_ctx,
743 attributes,
744 key_buffer,
745 key_buffer_size,
746 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200747 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200748 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100749 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +0200750
Ronald Cron0b805592020-12-14 18:08:20 +0100751 if( status != PSA_ERROR_NOT_SUPPORTED )
752 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200753#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100754#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100755#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200756 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +0100757 status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100758 attributes,
759 key_buffer,
760 key_buffer_size,
761 alg );
762 if( status == PSA_SUCCESS )
Ronald Cron6e412a72021-03-10 09:58:47 +0100763 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Ronald Cron49fafa92021-03-10 08:34:23 +0100764
Ronald Cron7b4154d2021-03-19 14:49:41 +0100765 if( status != PSA_ERROR_NOT_SUPPORTED )
766 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100767#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
768 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron0b805592020-12-14 18:08:20 +0100769
Steven Cooreman37941cb2020-07-28 18:49:51 +0200770 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100771#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200772#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100773 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200774 status = mbedtls_test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100775 &operation->ctx.opaque_test_driver_ctx,
776 attributes,
777 key_buffer, key_buffer_size,
778 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200779
Steven Cooreman37941cb2020-07-28 18:49:51 +0200780 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100781 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200782
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200783 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200784#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100785#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +0200786 default:
787 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +0100788 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100789 (void)key_buffer;
790 (void)key_buffer_size;
791 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +0200792 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200793 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200794}
795
796psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100797 psa_cipher_operation_t *operation,
798 const psa_key_attributes_t *attributes,
799 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200800 psa_algorithm_t alg )
801{
Steven Cooreman37941cb2020-07-28 18:49:51 +0200802 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crona4af55f2020-12-14 14:36:06 +0100803 psa_key_location_t location =
804 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200805
Steven Cooreman37941cb2020-07-28 18:49:51 +0200806 switch( location )
807 {
808 case PSA_KEY_LOCATION_LOCAL_STORAGE:
809 /* Key is stored in the slot in export representation, so
810 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100811#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200812#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200813 status = mbedtls_test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100814 &operation->ctx.transparent_test_driver_ctx,
815 attributes,
816 key_buffer,
817 key_buffer_size,
818 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200819 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200820 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100821 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +0200822
Ronald Cron0b805592020-12-14 18:08:20 +0100823 if( status != PSA_ERROR_NOT_SUPPORTED )
824 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200825#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100826#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100827#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200828 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +0100829 status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100830 attributes,
831 key_buffer,
832 key_buffer_size,
833 alg );
834 if( status == PSA_SUCCESS )
835 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
836
837 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100838#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
839 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron49fafa92021-03-10 08:34:23 +0100840
Steven Cooreman37941cb2020-07-28 18:49:51 +0200841 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100842#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200843#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100844 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200845 status = mbedtls_test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100846 &operation->ctx.opaque_test_driver_ctx,
847 attributes,
848 key_buffer, key_buffer_size,
849 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200850
Steven Cooreman37941cb2020-07-28 18:49:51 +0200851 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100852 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200853
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200854 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200855#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100856#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +0200857 default:
858 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +0100859 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100860 (void)key_buffer;
861 (void)key_buffer_size;
862 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +0200863 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200864 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200865}
866
Steven Cooreman37941cb2020-07-28 18:49:51 +0200867psa_status_t psa_driver_wrapper_cipher_set_iv(
Ronald Cron6056fe82020-12-15 13:58:07 +0100868 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200869 const uint8_t *iv,
870 size_t iv_length )
871{
Ronald Cron49fafa92021-03-10 08:34:23 +0100872 switch( operation->id )
873 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100874#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +0100875 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +0100876 return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100877 iv,
878 iv_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100879#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100880
881#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200882#if defined(PSA_CRYPTO_DRIVER_TEST)
883 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200884 return( mbedtls_test_transparent_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100885 &operation->ctx.transparent_test_driver_ctx,
886 iv, iv_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +0100887
Steven Cooreman37941cb2020-07-28 18:49:51 +0200888 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200889 return( mbedtls_test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100890 &operation->ctx.opaque_test_driver_ctx,
891 iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200892#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100893#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +0100894 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200895
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100896 (void)iv;
897 (void)iv_length;
898
Ronald Crondd24c9b2020-12-15 14:10:01 +0100899 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200900}
901
902psa_status_t psa_driver_wrapper_cipher_update(
Ronald Cron6056fe82020-12-15 13:58:07 +0100903 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200904 const uint8_t *input,
905 size_t input_length,
906 uint8_t *output,
907 size_t output_size,
908 size_t *output_length )
909{
Ronald Cron49fafa92021-03-10 08:34:23 +0100910 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200911 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100912#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +0100913 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +0100914 return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100915 input,
916 input_length,
917 output,
918 output_size,
919 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100920#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
921
Ronald Cron49fafa92021-03-10 08:34:23 +0100922#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200923#if defined(PSA_CRYPTO_DRIVER_TEST)
924 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200925 return( mbedtls_test_transparent_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100926 &operation->ctx.transparent_test_driver_ctx,
927 input, input_length,
928 output, output_size, output_length ) );
929
Steven Cooreman37941cb2020-07-28 18:49:51 +0200930 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200931 return( mbedtls_test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100932 &operation->ctx.opaque_test_driver_ctx,
933 input, input_length,
934 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200935#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100936#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +0100937 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200938
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100939 (void)input;
940 (void)input_length;
941 (void)output;
942 (void)output_size;
943 (void)output_length;
944
Ronald Crondd24c9b2020-12-15 14:10:01 +0100945 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200946}
947
948psa_status_t psa_driver_wrapper_cipher_finish(
Ronald Cron6056fe82020-12-15 13:58:07 +0100949 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200950 uint8_t *output,
951 size_t output_size,
952 size_t *output_length )
953{
Ronald Cron49fafa92021-03-10 08:34:23 +0100954 switch( operation->id )
955 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100956#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +0100957 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +0100958 return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100959 output,
960 output_size,
961 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100962#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100963
964#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200965#if defined(PSA_CRYPTO_DRIVER_TEST)
966 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200967 return( mbedtls_test_transparent_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100968 &operation->ctx.transparent_test_driver_ctx,
969 output, output_size, output_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +0100970
Steven Cooreman37941cb2020-07-28 18:49:51 +0200971 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200972 return( mbedtls_test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100973 &operation->ctx.opaque_test_driver_ctx,
974 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200975#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +0100976#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +0100977 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200978
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100979 (void)output;
980 (void)output_size;
981 (void)output_length;
982
Ronald Crondd24c9b2020-12-15 14:10:01 +0100983 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200984}
985
986psa_status_t psa_driver_wrapper_cipher_abort(
Ronald Cron6056fe82020-12-15 13:58:07 +0100987 psa_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200988{
Ronald Crondd24c9b2020-12-15 14:10:01 +0100989 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200990
Ronald Cron49fafa92021-03-10 08:34:23 +0100991 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +0200992 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100993#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +0100994 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +0100995 return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100996#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +0100997
998#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200999#if defined(PSA_CRYPTO_DRIVER_TEST)
1000 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001001 status = mbedtls_test_transparent_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001002 &operation->ctx.transparent_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001003 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001004 &operation->ctx.transparent_test_driver_ctx,
1005 sizeof( operation->ctx.transparent_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001006 return( status );
Ronald Cron49fafa92021-03-10 08:34:23 +01001007
Steven Cooreman37941cb2020-07-28 18:49:51 +02001008 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001009 status = mbedtls_test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001010 &operation->ctx.opaque_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001011 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001012 &operation->ctx.opaque_test_driver_ctx,
1013 sizeof( operation->ctx.opaque_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001014 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001015#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001016#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001017 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001018
Ronald Cron49fafa92021-03-10 08:34:23 +01001019 (void)status;
Ronald Crondd24c9b2020-12-15 14:10:01 +01001020 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001021}
1022
Steven Cooreman1e582352021-02-18 17:24:37 +01001023/*
1024 * Hashing functions
1025 */
1026psa_status_t psa_driver_wrapper_hash_compute(
1027 psa_algorithm_t alg,
1028 const uint8_t *input,
1029 size_t input_length,
1030 uint8_t *hash,
1031 size_t hash_size,
1032 size_t *hash_length)
1033{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001034 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001035
1036 /* Try accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001037#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001038 status = mbedtls_test_transparent_hash_compute(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001039 alg, input, input_length, hash, hash_size, hash_length );
Steven Cooremanf7638102021-03-04 15:14:36 +01001040 if( status != PSA_ERROR_NOT_SUPPORTED )
1041 return( status );
1042#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001043
1044 /* If software fallback is compiled in, try fallback */
1045#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1046 status = mbedtls_psa_hash_compute( alg, input, input_length,
1047 hash, hash_size, hash_length );
1048 if( status != PSA_ERROR_NOT_SUPPORTED )
1049 return( status );
1050#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001051 (void) status;
1052 (void) alg;
1053 (void) input;
1054 (void) input_length;
1055 (void) hash;
1056 (void) hash_size;
1057 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001058
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001059 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman1e582352021-02-18 17:24:37 +01001060}
1061
1062psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001063 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001064 psa_algorithm_t alg )
1065{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001066 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001067
Steven Cooreman1e582352021-02-18 17:24:37 +01001068 /* Try setup on accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001069#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001070 status = mbedtls_test_transparent_hash_setup(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001071 &operation->ctx.test_driver_ctx, alg );
Steven Cooremanf7638102021-03-04 15:14:36 +01001072 if( status == PSA_SUCCESS )
1073 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1074
1075 if( status != PSA_ERROR_NOT_SUPPORTED )
1076 return( status );
1077#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001078
1079 /* If software fallback is compiled in, try fallback */
1080#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001081 status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
Steven Cooreman1e582352021-02-18 17:24:37 +01001082 if( status == PSA_SUCCESS )
Steven Cooreman1e582352021-02-18 17:24:37 +01001083 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Steven Cooreman1e582352021-02-18 17:24:37 +01001084
1085 if( status != PSA_ERROR_NOT_SUPPORTED )
1086 return( status );
1087#endif
1088 /* Nothing left to try if we fall through here */
1089 (void) status;
1090 (void) operation;
1091 (void) alg;
1092 return( PSA_ERROR_NOT_SUPPORTED );
1093}
1094
1095psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001096 const psa_hash_operation_t *source_operation,
1097 psa_hash_operation_t *target_operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001098{
Steven Cooreman1e582352021-02-18 17:24:37 +01001099 switch( source_operation->id )
1100 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001101#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1102 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1103 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1104 return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
1105 &target_operation->ctx.mbedtls_ctx ) );
1106#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001107#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001108 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1109 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001110 return( mbedtls_test_transparent_hash_clone(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001111 &source_operation->ctx.test_driver_ctx,
1112 &target_operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001113#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001114 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001115 (void) target_operation;
1116 return( PSA_ERROR_BAD_STATE );
1117 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001118}
1119
1120psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001121 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001122 const uint8_t *input,
1123 size_t input_length )
1124{
Steven Cooreman1e582352021-02-18 17:24:37 +01001125 switch( operation->id )
1126 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001127#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1128 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1129 return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
1130 input, input_length ) );
1131#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001132#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001133 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001134 return( mbedtls_test_transparent_hash_update(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001135 &operation->ctx.test_driver_ctx,
1136 input, input_length ) );
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) input;
1140 (void) input_length;
1141 return( PSA_ERROR_BAD_STATE );
1142 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001143}
1144
1145psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001146 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001147 uint8_t *hash,
1148 size_t hash_size,
1149 size_t *hash_length )
1150{
Steven Cooreman1e582352021-02-18 17:24:37 +01001151 switch( operation->id )
1152 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001153#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1154 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1155 return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
1156 hash, hash_size, hash_length ) );
1157#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001158#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001159 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001160 return( mbedtls_test_transparent_hash_finish(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001161 &operation->ctx.test_driver_ctx,
1162 hash, hash_size, hash_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001163#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001164 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001165 (void) hash;
1166 (void) hash_size;
1167 (void) hash_length;
1168 return( PSA_ERROR_BAD_STATE );
1169 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001170}
1171
1172psa_status_t psa_driver_wrapper_hash_abort(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001173 psa_hash_operation_t *operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001174{
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_abort( &operation->ctx.mbedtls_ctx ) );
1180#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001181#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001182 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001183 return( mbedtls_test_transparent_hash_abort(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001184 &operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001185#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001186 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001187 return( PSA_ERROR_BAD_STATE );
1188 }
1189}
1190
Ronald Cronde822812021-03-17 16:08:20 +01001191psa_status_t psa_driver_wrapper_aead_encrypt(
1192 const psa_key_attributes_t *attributes,
1193 const uint8_t *key_buffer, size_t key_buffer_size,
1194 psa_algorithm_t alg,
1195 const uint8_t *nonce, size_t nonce_length,
1196 const uint8_t *additional_data, size_t additional_data_length,
1197 const uint8_t *plaintext, size_t plaintext_length,
1198 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
1199{
1200 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1201 psa_key_location_t location =
1202 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1203
1204 switch( location )
1205 {
1206 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1207 /* Key is stored in the slot in export representation, so
1208 * cycle through all known transparent accelerators */
1209
1210#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1211#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001212 status = mbedtls_test_transparent_aead_encrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001213 attributes, key_buffer, key_buffer_size,
1214 alg,
1215 nonce, nonce_length,
1216 additional_data, additional_data_length,
1217 plaintext, plaintext_length,
1218 ciphertext, ciphertext_size, ciphertext_length );
1219 /* Declared with fallback == true */
1220 if( status != PSA_ERROR_NOT_SUPPORTED )
1221 return( status );
1222#endif /* PSA_CRYPTO_DRIVER_TEST */
1223#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1224
1225 /* Fell through, meaning no accelerator supports this operation */
1226 return( mbedtls_psa_aead_encrypt(
1227 attributes, key_buffer, key_buffer_size,
1228 alg,
1229 nonce, nonce_length,
1230 additional_data, additional_data_length,
1231 plaintext, plaintext_length,
1232 ciphertext, ciphertext_size, ciphertext_length ) );
1233
1234 /* Add cases for opaque driver here */
1235
1236 default:
1237 /* Key is declared with a lifetime not known to us */
1238 (void)status;
1239 return( PSA_ERROR_INVALID_ARGUMENT );
1240 }
1241}
1242
1243psa_status_t psa_driver_wrapper_aead_decrypt(
1244 const psa_key_attributes_t *attributes,
1245 const uint8_t *key_buffer, size_t key_buffer_size,
1246 psa_algorithm_t alg,
1247 const uint8_t *nonce, size_t nonce_length,
1248 const uint8_t *additional_data, size_t additional_data_length,
1249 const uint8_t *ciphertext, size_t ciphertext_length,
1250 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
1251{
1252 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1253 psa_key_location_t location =
1254 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1255
1256 switch( location )
1257 {
1258 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1259 /* Key is stored in the slot in export representation, so
1260 * cycle through all known transparent accelerators */
1261
1262#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1263#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001264 status = mbedtls_test_transparent_aead_decrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001265 attributes, key_buffer, key_buffer_size,
1266 alg,
1267 nonce, nonce_length,
1268 additional_data, additional_data_length,
1269 ciphertext, ciphertext_length,
1270 plaintext, plaintext_size, plaintext_length );
1271 /* Declared with fallback == true */
1272 if( status != PSA_ERROR_NOT_SUPPORTED )
1273 return( status );
1274#endif /* PSA_CRYPTO_DRIVER_TEST */
1275#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1276
1277 /* Fell through, meaning no accelerator supports this operation */
1278 return( mbedtls_psa_aead_decrypt(
1279 attributes, key_buffer, key_buffer_size,
1280 alg,
1281 nonce, nonce_length,
1282 additional_data, additional_data_length,
1283 ciphertext, ciphertext_length,
1284 plaintext, plaintext_size, plaintext_length ) );
1285
1286 /* Add cases for opaque driver here */
1287
1288 default:
1289 /* Key is declared with a lifetime not known to us */
1290 (void)status;
1291 return( PSA_ERROR_INVALID_ARGUMENT );
1292 }
1293}
Steven Cooreman896d51e2021-03-19 15:24:23 +01001294
1295
1296/*
1297 * MAC functions
1298 */
1299psa_status_t psa_driver_wrapper_mac_compute(
1300 const psa_key_attributes_t *attributes,
1301 const uint8_t *key_buffer,
1302 size_t key_buffer_size,
1303 psa_algorithm_t alg,
1304 const uint8_t *input,
1305 size_t input_length,
1306 uint8_t *mac,
1307 size_t mac_size,
1308 size_t *mac_length )
1309{
1310 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1311 psa_key_location_t location =
1312 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1313
1314 switch( location )
1315 {
1316 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1317 /* Key is stored in the slot in export representation, so
1318 * cycle through all known transparent accelerators */
1319#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1320#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001321 status = mbedtls_test_transparent_mac_compute(
Steven Cooreman896d51e2021-03-19 15:24:23 +01001322 attributes, key_buffer, key_buffer_size, alg,
1323 input, input_length,
1324 mac, mac_size, mac_length );
1325 /* Declared with fallback == true */
1326 if( status != PSA_ERROR_NOT_SUPPORTED )
1327 return( status );
1328#endif /* PSA_CRYPTO_DRIVER_TEST */
1329#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1330#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1331 /* Fell through, meaning no accelerator supports this operation */
1332 status = mbedtls_psa_mac_compute(
1333 attributes, key_buffer, key_buffer_size, alg,
1334 input, input_length,
1335 mac, mac_size, mac_length );
1336 if( status != PSA_ERROR_NOT_SUPPORTED )
1337 return( status );
1338#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1339 return( PSA_ERROR_NOT_SUPPORTED );
1340
1341 /* Add cases for opaque driver here */
1342#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1343#if defined(PSA_CRYPTO_DRIVER_TEST)
1344 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001345 status = mbedtls_test_opaque_mac_compute(
Steven Cooreman896d51e2021-03-19 15:24:23 +01001346 attributes, key_buffer, key_buffer_size, alg,
1347 input, input_length,
1348 mac, mac_size, mac_length );
1349 return( status );
1350#endif /* PSA_CRYPTO_DRIVER_TEST */
1351#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1352 default:
1353 /* Key is declared with a lifetime not known to us */
1354 (void) key_buffer;
1355 (void) key_buffer_size;
1356 (void) alg;
1357 (void) input;
1358 (void) input_length;
1359 (void) mac;
1360 (void) mac_size;
1361 (void) mac_length;
1362 (void) status;
1363 return( PSA_ERROR_INVALID_ARGUMENT );
1364 }
1365}
1366
1367psa_status_t psa_driver_wrapper_mac_sign_setup(
1368 psa_mac_operation_t *operation,
1369 const psa_key_attributes_t *attributes,
1370 const uint8_t *key_buffer,
1371 size_t key_buffer_size,
1372 psa_algorithm_t alg )
1373{
1374 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1375 psa_key_location_t location =
1376 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1377
1378 switch( location )
1379 {
1380 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1381 /* Key is stored in the slot in export representation, so
1382 * cycle through all known transparent accelerators */
1383#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1384#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001385 status = mbedtls_test_transparent_mac_sign_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001386 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001387 attributes,
1388 key_buffer, key_buffer_size,
1389 alg );
1390 /* Declared with fallback == true */
1391 if( status == PSA_SUCCESS )
1392 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1393
1394 if( status != PSA_ERROR_NOT_SUPPORTED )
1395 return( status );
1396#endif /* PSA_CRYPTO_DRIVER_TEST */
1397#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1398#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1399 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001400 status = mbedtls_psa_mac_sign_setup( &operation->ctx.mbedtls_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001401 attributes,
1402 key_buffer, key_buffer_size,
1403 alg );
1404 if( status == PSA_SUCCESS )
1405 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1406
1407 if( status != PSA_ERROR_NOT_SUPPORTED )
1408 return( status );
1409#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1410 return( PSA_ERROR_NOT_SUPPORTED );
1411
1412 /* Add cases for opaque driver here */
1413#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1414#if defined(PSA_CRYPTO_DRIVER_TEST)
1415 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001416 status = mbedtls_test_opaque_mac_sign_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001417 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001418 attributes,
1419 key_buffer, key_buffer_size,
1420 alg );
1421
1422 if( status == PSA_SUCCESS )
1423 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
1424
1425 return( status );
1426#endif /* PSA_CRYPTO_DRIVER_TEST */
1427#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1428 default:
1429 /* Key is declared with a lifetime not known to us */
1430 (void) status;
1431 (void) key_buffer;
1432 (void) key_buffer_size;
1433 (void) alg;
1434 return( PSA_ERROR_INVALID_ARGUMENT );
1435 }
1436}
1437
1438psa_status_t psa_driver_wrapper_mac_verify_setup(
1439 psa_mac_operation_t *operation,
1440 const psa_key_attributes_t *attributes,
1441 const uint8_t *key_buffer,
1442 size_t key_buffer_size,
1443 psa_algorithm_t alg )
1444{
1445 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1446 psa_key_location_t location =
1447 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1448
1449 switch( location )
1450 {
1451 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1452 /* Key is stored in the slot in export representation, so
1453 * cycle through all known transparent accelerators */
1454#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1455#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001456 status = mbedtls_test_transparent_mac_verify_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001457 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001458 attributes,
1459 key_buffer, key_buffer_size,
1460 alg );
1461 /* Declared with fallback == true */
1462 if( status == PSA_SUCCESS )
1463 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1464
1465 if( status != PSA_ERROR_NOT_SUPPORTED )
1466 return( status );
1467#endif /* PSA_CRYPTO_DRIVER_TEST */
1468#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1469#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1470 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001471 status = mbedtls_psa_mac_verify_setup( &operation->ctx.mbedtls_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001472 attributes,
1473 key_buffer, key_buffer_size,
1474 alg );
1475 if( status == PSA_SUCCESS )
1476 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1477
1478 if( status != PSA_ERROR_NOT_SUPPORTED )
1479 return( status );
1480#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1481 return( PSA_ERROR_NOT_SUPPORTED );
1482
1483 /* Add cases for opaque driver here */
1484#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1485#if defined(PSA_CRYPTO_DRIVER_TEST)
1486 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001487 status = mbedtls_test_opaque_mac_verify_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001488 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001489 attributes,
1490 key_buffer, key_buffer_size,
1491 alg );
1492
1493 if( status == PSA_SUCCESS )
1494 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
1495
1496 return( status );
1497#endif /* PSA_CRYPTO_DRIVER_TEST */
1498#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1499 default:
1500 /* Key is declared with a lifetime not known to us */
1501 (void) status;
1502 (void) key_buffer;
1503 (void) key_buffer_size;
1504 (void) alg;
1505 return( PSA_ERROR_INVALID_ARGUMENT );
1506 }
1507}
1508
1509psa_status_t psa_driver_wrapper_mac_update(
1510 psa_mac_operation_t *operation,
1511 const uint8_t *input,
1512 size_t input_length )
1513{
1514 switch( operation->id )
1515 {
1516#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1517 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001518 return( mbedtls_psa_mac_update( &operation->ctx.mbedtls_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001519 input, input_length ) );
1520#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1521
1522#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1523#if defined(PSA_CRYPTO_DRIVER_TEST)
1524 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001525 return( mbedtls_test_transparent_mac_update(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001526 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001527 input, input_length ) );
1528
1529 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001530 return( mbedtls_test_opaque_mac_update(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001531 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001532 input, input_length ) );
1533#endif /* PSA_CRYPTO_DRIVER_TEST */
1534#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1535 default:
1536 (void) input;
1537 (void) input_length;
1538 return( PSA_ERROR_INVALID_ARGUMENT );
1539 }
1540}
1541
1542psa_status_t psa_driver_wrapper_mac_sign_finish(
1543 psa_mac_operation_t *operation,
1544 uint8_t *mac,
1545 size_t mac_size,
1546 size_t *mac_length )
1547{
1548 switch( operation->id )
1549 {
1550#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1551 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001552 return( mbedtls_psa_mac_sign_finish( &operation->ctx.mbedtls_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001553 mac, mac_size, mac_length ) );
1554#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1555
1556#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1557#if defined(PSA_CRYPTO_DRIVER_TEST)
1558 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001559 return( mbedtls_test_transparent_mac_sign_finish(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001560 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001561 mac, mac_size, mac_length ) );
1562
1563 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001564 return( mbedtls_test_opaque_mac_sign_finish(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001565 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001566 mac, mac_size, mac_length ) );
1567#endif /* PSA_CRYPTO_DRIVER_TEST */
1568#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1569 default:
1570 (void) mac;
1571 (void) mac_size;
1572 (void) mac_length;
1573 return( PSA_ERROR_INVALID_ARGUMENT );
1574 }
1575}
1576
1577psa_status_t psa_driver_wrapper_mac_verify_finish(
1578 psa_mac_operation_t *operation,
1579 const uint8_t *mac,
1580 size_t mac_length )
1581{
1582 switch( operation->id )
1583 {
1584#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1585 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001586 return( mbedtls_psa_mac_verify_finish( &operation->ctx.mbedtls_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001587 mac, mac_length ) );
1588#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1589
1590#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1591#if defined(PSA_CRYPTO_DRIVER_TEST)
1592 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001593 return( mbedtls_test_transparent_mac_verify_finish(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001594 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001595 mac, mac_length ) );
1596
1597 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001598 return( mbedtls_test_opaque_mac_verify_finish(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001599 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001600 mac, mac_length ) );
1601#endif /* PSA_CRYPTO_DRIVER_TEST */
1602#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1603 default:
1604 (void) mac;
1605 (void) mac_length;
1606 return( PSA_ERROR_INVALID_ARGUMENT );
1607 }
1608}
1609
1610psa_status_t psa_driver_wrapper_mac_abort(
1611 psa_mac_operation_t *operation )
1612{
Steven Cooreman896d51e2021-03-19 15:24:23 +01001613 switch( operation->id )
1614 {
1615#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1616 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman07897832021-03-19 18:28:56 +01001617 return( mbedtls_psa_mac_abort( &operation->ctx.mbedtls_ctx ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +01001618#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1619
1620#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1621#if defined(PSA_CRYPTO_DRIVER_TEST)
1622 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001623 return( mbedtls_test_transparent_mac_abort(
Steven Cooreman07897832021-03-19 18:28:56 +01001624 &operation->ctx.transparent_test_driver_ctx ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +01001625 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001626 return( mbedtls_test_opaque_mac_abort(
Steven Cooreman07897832021-03-19 18:28:56 +01001627 &operation->ctx.opaque_test_driver_ctx ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +01001628#endif /* PSA_CRYPTO_DRIVER_TEST */
1629#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1630 default:
Steven Cooreman07897832021-03-19 18:28:56 +01001631 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman896d51e2021-03-19 15:24:23 +01001632 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001633}
Steven Cooremancd84cb42020-07-16 20:28:36 +02001634/* End of automatically generated file. */