blob: 20f1abd99e3aa99defaae1fc4f1c528352996433 [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
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +020022#include "common.h"
Ronald Cronde822812021-03-17 16:08:20 +010023#include "psa_crypto_aead.h"
Ronald Cron0b805592020-12-14 18:08:20 +010024#include "psa_crypto_cipher.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020025#include "psa_crypto_core.h"
26#include "psa_crypto_driver_wrappers.h"
Steven Cooreman830aff22021-03-09 09:50:44 +010027#include "psa_crypto_hash.h"
Steven Cooremand13a70f2021-03-19 15:24:23 +010028#include "psa_crypto_mac.h"
Steven Cooreman830aff22021-03-09 09:50:44 +010029
Steven Cooreman2a1664c2020-07-20 15:33:08 +020030#include "mbedtls/platform.h"
31
Gilles Peskine1905a242021-04-24 13:19:45 +020032#if defined(MBEDTLS_PSA_CRYPTO_C)
33
Steven Cooreman2a1664c2020-07-20 15:33:08 +020034#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
Steven Cooremancd84cb42020-07-16 20:28:36 +020035
36/* Include test driver definition when running tests */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020037#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman56250fd2020-09-04 13:07:15 +020038#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020039#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020040#endif
41#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020042#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020043#endif
Ronald Cronce1d8d22021-04-30 17:00:34 +020044#include "test/drivers/test_driver.h"
Steven Cooremanf1720ea2020-07-24 18:41:58 +020045#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +020046
Steven Cooreman56250fd2020-09-04 13:07:15 +020047/* Repeat above block for each JSON-declared driver during autogeneration */
Steven Cooremanaa87fd02021-03-15 18:54:03 +010048#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Steven Cooreman56250fd2020-09-04 13:07:15 +020049
Steven Cooreman1e582352021-02-18 17:24:37 +010050/* Auto-generated values depending on which drivers are registered.
51 * ID 0 is reserved for unallocated operations.
52 * ID 1 is reserved for the Mbed TLS software driver. */
Steven Cooreman830aff22021-03-09 09:50:44 +010053#define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
54
Steven Cooreman37941cb2020-07-28 18:49:51 +020055#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman1e582352021-02-18 17:24:37 +010056#define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2)
57#define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3)
Steven Cooreman37941cb2020-07-28 18:49:51 +020058#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +020059
60/* Support the 'old' SE interface when asked to */
Steven Cooreman7a250572020-07-17 16:43:05 +020061#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman56250fd2020-09-04 13:07:15 +020062/* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
63 * SE driver is present, to avoid unused argument errors at compile time. */
64#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020065#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020066#endif
Steven Cooreman7a250572020-07-17 16:43:05 +020067#include "psa_crypto_se.h"
68#endif
69
Ronald Cron9ba76912021-04-10 16:57:30 +020070psa_status_t psa_driver_wrapper_init( void )
71{
72 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
73
74#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
75 status = psa_init_all_se_drivers( );
76 if( status != PSA_SUCCESS )
77 return( status );
78#endif
79
80#if defined(PSA_CRYPTO_DRIVER_TEST)
81 status = mbedtls_test_transparent_init( );
82 if( status != PSA_SUCCESS )
83 return( status );
84
85 status = mbedtls_test_opaque_init( );
86 if( status != PSA_SUCCESS )
87 return( status );
88#endif
89
90 (void) status;
91 return( PSA_SUCCESS );
92}
93
94void psa_driver_wrapper_free( void )
95{
96#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
97 /* Unregister all secure element drivers, so that we restart from
98 * a pristine state. */
99 psa_unregister_all_se_drivers( );
100#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
101
102#if defined(PSA_CRYPTO_DRIVER_TEST)
103 mbedtls_test_transparent_free( );
104 mbedtls_test_opaque_free( );
105#endif
106}
107
Steven Cooremancd84cb42020-07-16 20:28:36 +0200108/* Start delegation functions */
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200109psa_status_t psa_driver_wrapper_sign_message(
110 const psa_key_attributes_t *attributes,
111 const uint8_t *key_buffer,
112 size_t key_buffer_size,
113 psa_algorithm_t alg,
114 const uint8_t *input,
115 size_t input_length,
116 uint8_t *signature,
117 size_t signature_size,
118 size_t *signature_length )
119{
120 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200121 psa_key_location_t location =
122 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
123
124 switch( location )
125 {
126 case PSA_KEY_LOCATION_LOCAL_STORAGE:
127 /* Key is stored in the slot in export representation, so
128 * cycle through all known transparent accelerators */
129#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
130#if defined(PSA_CRYPTO_DRIVER_TEST)
131 status = mbedtls_test_transparent_signature_sign_message(
132 attributes,
133 key_buffer,
134 key_buffer_size,
135 alg,
136 input,
137 input_length,
138 signature,
139 signature_size,
140 signature_length );
141 /* Declared with fallback == true */
142 if( status != PSA_ERROR_NOT_SUPPORTED )
143 return( status );
144#endif /* PSA_CRYPTO_DRIVER_TEST */
145#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200146 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200147
148 /* Add cases for opaque driver here */
149#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
150#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arm2522c0b2021-04-27 18:52:51 +0200151 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200152 status = mbedtls_test_opaque_signature_sign_message(
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200153 attributes,
154 key_buffer,
155 key_buffer_size,
156 alg,
157 input,
158 input_length,
159 signature,
160 signature_size,
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200161 signature_length );
162 if( status != PSA_ERROR_NOT_SUPPORTED )
163 return( status );
164 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200165#endif /* PSA_CRYPTO_DRIVER_TEST */
166#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
167 default:
168 /* Key is declared with a lifetime not known to us */
169 (void)status;
gabor-mezei-armd5218df2021-05-12 11:12:25 +0200170 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200171 }
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200172
173 return( psa_sign_message_builtin( attributes,
174 key_buffer,
175 key_buffer_size,
176 alg,
177 input,
178 input_length,
179 signature,
180 signature_size,
181 signature_length ) );
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200182}
183
184psa_status_t psa_driver_wrapper_verify_message(
185 const psa_key_attributes_t *attributes,
186 const uint8_t *key_buffer,
187 size_t key_buffer_size,
188 psa_algorithm_t alg,
189 const uint8_t *input,
190 size_t input_length,
191 const uint8_t *signature,
192 size_t signature_length )
193{
194 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200195 psa_key_location_t location =
196 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
197
198 switch( location )
199 {
200 case PSA_KEY_LOCATION_LOCAL_STORAGE:
201 /* Key is stored in the slot in export representation, so
202 * cycle through all known transparent accelerators */
203#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
204#if defined(PSA_CRYPTO_DRIVER_TEST)
205 status = mbedtls_test_transparent_signature_verify_message(
206 attributes,
207 key_buffer,
208 key_buffer_size,
209 alg,
210 input,
211 input_length,
212 signature,
213 signature_length );
214 /* Declared with fallback == true */
215 if( status != PSA_ERROR_NOT_SUPPORTED )
216 return( status );
217#endif /* PSA_CRYPTO_DRIVER_TEST */
218#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200219 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200220
221 /* Add cases for opaque driver here */
222#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
223#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arm2522c0b2021-04-27 18:52:51 +0200224 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200225 return( mbedtls_test_opaque_signature_verify_message(
226 attributes,
227 key_buffer,
228 key_buffer_size,
229 alg,
230 input,
231 input_length,
232 signature,
233 signature_length ) );
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200234 if( status != PSA_ERROR_NOT_SUPPORTED )
235 return( status );
236 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200237#endif /* PSA_CRYPTO_DRIVER_TEST */
238#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
239 default:
240 /* Key is declared with a lifetime not known to us */
241 (void)status;
gabor-mezei-armd5218df2021-05-12 11:12:25 +0200242 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200243 }
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200244
245 return( psa_verify_message_builtin( attributes,
246 key_buffer,
247 key_buffer_size,
248 alg,
249 input,
250 input_length,
251 signature,
252 signature_length ) );
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200253}
254
Ronald Cron9f17aa42020-12-08 17:07:25 +0100255psa_status_t psa_driver_wrapper_sign_hash(
256 const psa_key_attributes_t *attributes,
257 const uint8_t *key_buffer, size_t key_buffer_size,
258 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
259 uint8_t *signature, size_t signature_size, size_t *signature_length )
Steven Cooremancd84cb42020-07-16 20:28:36 +0200260{
Steven Cooreman7a250572020-07-17 16:43:05 +0200261 /* Try dynamically-registered SE interface first */
262#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
263 const psa_drv_se_t *drv;
264 psa_drv_se_context_t *drv_context;
265
Ronald Cron9f17aa42020-12-08 17:07:25 +0100266 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman7a250572020-07-17 16:43:05 +0200267 {
268 if( drv->asymmetric == NULL ||
269 drv->asymmetric->p_sign == NULL )
270 {
271 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200272 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman7a250572020-07-17 16:43:05 +0200273 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100274 return( drv->asymmetric->p_sign(
275 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
276 alg, hash, hash_length,
277 signature, signature_size, signature_length ) );
Steven Cooreman7a250572020-07-17 16:43:05 +0200278 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200279#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman7a250572020-07-17 16:43:05 +0200280
Ronald Cronfce9df22020-12-08 18:06:03 +0100281 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100282 psa_key_location_t location =
283 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200284
285 switch( location )
286 {
287 case PSA_KEY_LOCATION_LOCAL_STORAGE:
288 /* Key is stored in the slot in export representation, so
289 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100290#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200291#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200292 status = mbedtls_test_transparent_signature_sign_hash( attributes,
Ronald Cron9f17aa42020-12-08 17:07:25 +0100293 key_buffer,
294 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200295 alg,
296 hash,
297 hash_length,
298 signature,
299 signature_size,
300 signature_length );
301 /* Declared with fallback == true */
302 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200303 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200304#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100305#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200306 /* Fell through, meaning no accelerator supports this operation */
gabor-mezei-arm6883fd22021-05-05 14:18:36 +0200307 return( psa_sign_hash_builtin( attributes,
308 key_buffer,
309 key_buffer_size,
310 alg,
311 hash,
312 hash_length,
313 signature,
314 signature_size,
315 signature_length ) );
Ronald Cronfce9df22020-12-08 18:06:03 +0100316
Steven Cooremancd84cb42020-07-16 20:28:36 +0200317 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100318#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200319#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100320 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200321 return( mbedtls_test_opaque_signature_sign_hash( attributes,
322 key_buffer,
323 key_buffer_size,
324 alg,
325 hash,
326 hash_length,
327 signature,
328 signature_size,
329 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200330#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100331#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200332 default:
333 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100334 (void)status;
335 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200336 }
Steven Cooremancd84cb42020-07-16 20:28:36 +0200337}
338
Ronald Cron9f17aa42020-12-08 17:07:25 +0100339psa_status_t psa_driver_wrapper_verify_hash(
340 const psa_key_attributes_t *attributes,
341 const uint8_t *key_buffer, size_t key_buffer_size,
342 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
343 const uint8_t *signature, size_t signature_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200344{
Steven Cooreman55ae2172020-07-17 19:46:15 +0200345 /* Try dynamically-registered SE interface first */
346#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
347 const psa_drv_se_t *drv;
348 psa_drv_se_context_t *drv_context;
349
Ronald Cron9f17aa42020-12-08 17:07:25 +0100350 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200351 {
352 if( drv->asymmetric == NULL ||
353 drv->asymmetric->p_verify == NULL )
354 {
355 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200356 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200357 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100358 return( drv->asymmetric->p_verify(
359 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
360 alg, hash, hash_length,
361 signature, signature_length ) );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200362 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200363#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200364
Ronald Cronfce9df22020-12-08 18:06:03 +0100365 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100366 psa_key_location_t location =
367 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200368
369 switch( location )
370 {
371 case PSA_KEY_LOCATION_LOCAL_STORAGE:
372 /* Key is stored in the slot in export representation, so
373 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100374#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200375#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200376 status = mbedtls_test_transparent_signature_verify_hash(
377 attributes,
378 key_buffer,
379 key_buffer_size,
380 alg,
381 hash,
382 hash_length,
383 signature,
384 signature_length );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200385 /* Declared with fallback == true */
386 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200387 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200388#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100389#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
390
gabor-mezei-arm6883fd22021-05-05 14:18:36 +0200391 return( psa_verify_hash_builtin( attributes,
392 key_buffer,
393 key_buffer_size,
394 alg,
395 hash,
396 hash_length,
397 signature,
398 signature_length ) );
Ronald Cronfce9df22020-12-08 18:06:03 +0100399
Steven Cooreman55ae2172020-07-17 19:46:15 +0200400 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100401#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200402#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100403 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200404 return( mbedtls_test_opaque_signature_verify_hash( attributes,
405 key_buffer,
406 key_buffer_size,
407 alg,
408 hash,
409 hash_length,
410 signature,
411 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200412#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100413#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200414 default:
415 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100416 (void)status;
417 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200418 }
Steven Cooreman55ae2172020-07-17 19:46:15 +0200419}
420
Archana449608b2021-09-08 15:36:05 +0530421/** Calculate the key buffer size required to store the key material of a key
Archanad8a83dc2021-06-14 10:04:16 +0530422 * associated with an opaque driver from input key data.
423 *
Archanad8a83dc2021-06-14 10:04:16 +0530424 * \param[in] attributes The key attributes
425 * \param[in] data The input key data.
426 * \param[in] data_length The input data length.
427 * \param[out] key_buffer_size Minimum buffer size to contain the key material.
428 *
429 * \retval #PSA_SUCCESS
430 * \retval #PSA_ERROR_INVALID_ARGUMENT
431 * \retval #PSA_ERROR_NOT_SUPPORTED
432 */
433psa_status_t psa_driver_wrapper_get_key_buffer_size_from_key_data(
434 const psa_key_attributes_t *attributes,
435 const uint8_t *data,
436 size_t data_length,
437 size_t *key_buffer_size )
438{
Archana449608b2021-09-08 15:36:05 +0530439 psa_key_location_t location =
440 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Archanad8a83dc2021-06-14 10:04:16 +0530441 psa_key_type_t key_type = attributes->core.type;
442
443 *key_buffer_size = 0;
444 switch( location )
445 {
446#if defined(PSA_CRYPTO_DRIVER_TEST)
447 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
448 *key_buffer_size = mbedtls_test_opaque_size_function( key_type,
449 PSA_BYTES_TO_BITS( data_length ) );
450 return( ( *key_buffer_size != 0 ) ?
451 PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
452#endif /* PSA_CRYPTO_DRIVER_TEST */
453
454 default:
455 (void)key_type;
456 (void)data;
457 (void)data_length;
458 return( PSA_ERROR_INVALID_ARGUMENT );
459 }
460}
461
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200462/** Get the key buffer size required to store the key material of a key
Archanad8a83dc2021-06-14 10:04:16 +0530463 * associated with an opaque driver.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200464 *
Ronald Cron31216282020-12-05 18:47:56 +0100465 * \param[in] attributes The key attributes.
466 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200467 *
468 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100469 * The minimum size for a buffer to contain the key material has been
470 * returned successfully.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200471 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100472 * The type and/or the size in bits of the key or the combination of
473 * the two is not supported.
Archanad8a83dc2021-06-14 10:04:16 +0530474 * \retval #PSA_ERROR_INVALID_ARGUMENT
475 * The key is declared with a lifetime not known to us.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200476 */
Ronald Cron9df74be2020-12-05 19:15:23 +0100477psa_status_t psa_driver_wrapper_get_key_buffer_size(
Ronald Cron31216282020-12-05 18:47:56 +0100478 const psa_key_attributes_t *attributes,
479 size_t *key_buffer_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200480{
John Durkop2c618352020-09-22 06:54:01 -0700481 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
482 psa_key_type_t key_type = attributes->core.type;
483 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200484
Ronald Cron31216282020-12-05 18:47:56 +0100485 *key_buffer_size = 0;
John Durkop2c618352020-09-22 06:54:01 -0700486 switch( location )
487 {
John Durkop2c618352020-09-22 06:54:01 -0700488#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100489 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100490#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
491 /* Emulate property 'builtin_key_size' */
492 if( psa_key_id_is_builtin(
493 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(
494 psa_get_key_id( attributes ) ) ) )
495 {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100496 *key_buffer_size = sizeof( psa_drv_slot_number_t );
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100497 return( PSA_SUCCESS );
498 }
499#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Archana449608b2021-09-08 15:36:05 +0530500 *key_buffer_size = mbedtls_test_opaque_size_function( key_type,
501 key_bits );
Ronald Cron4607c822021-04-14 10:55:34 +0200502 return( ( *key_buffer_size != 0 ) ?
503 PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
John Durkop2c618352020-09-22 06:54:01 -0700504#endif /* PSA_CRYPTO_DRIVER_TEST */
505
506 default:
Ronald Cron9df74be2020-12-05 19:15:23 +0100507 (void)key_type;
508 (void)key_bits;
Archanad8a83dc2021-06-14 10:04:16 +0530509 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200510 }
511}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200512
Ronald Cron977c2472020-10-13 08:32:21 +0200513psa_status_t psa_driver_wrapper_generate_key(
514 const psa_key_attributes_t *attributes,
515 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200516{
Ronald Cron977c2472020-10-13 08:32:21 +0200517 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
518 psa_key_location_t location =
519 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
520
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200521 /* Try dynamically-registered SE interface first */
522#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
523 const psa_drv_se_t *drv;
524 psa_drv_se_context_t *drv_context;
525
Ronald Cron977c2472020-10-13 08:32:21 +0200526 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200527 {
528 size_t pubkey_length = 0; /* We don't support this feature yet */
529 if( drv->key_management == NULL ||
530 drv->key_management->p_generate == NULL )
531 {
532 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200533 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200534 }
535 return( drv->key_management->p_generate(
Ronald Cron977c2472020-10-13 08:32:21 +0200536 drv_context,
537 *( (psa_key_slot_number_t *)key_buffer ),
Ronald Cronea0f8a62020-11-25 17:52:23 +0100538 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200539 }
540#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
541
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200542 switch( location )
543 {
544 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Ronald Cron977c2472020-10-13 08:32:21 +0200545#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200546 /* Transparent drivers are limited to generating asymmetric keys */
Ronald Cron977c2472020-10-13 08:32:21 +0200547 if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200548 {
Ronald Cron977c2472020-10-13 08:32:21 +0200549 /* Cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200550#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200551 status = mbedtls_test_transparent_generate_key(
Ronald Cron977c2472020-10-13 08:32:21 +0200552 attributes, key_buffer, key_buffer_size,
553 key_buffer_length );
554 /* Declared with fallback == true */
555 if( status != PSA_ERROR_NOT_SUPPORTED )
556 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200557#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200558 }
559#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
560
561 /* Software fallback */
562 status = psa_generate_key_internal(
563 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200564 break;
Ronald Cron977c2472020-10-13 08:32:21 +0200565
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200566 /* Add cases for opaque driver here */
Ronald Cron977c2472020-10-13 08:32:21 +0200567#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200568#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100569 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200570 status = mbedtls_test_opaque_generate_key(
Ronald Cron977c2472020-10-13 08:32:21 +0200571 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200572 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200573#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200574#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
575
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200576 default:
577 /* Key is declared with a lifetime not known to us */
578 status = PSA_ERROR_INVALID_ARGUMENT;
579 break;
580 }
581
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200582 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200583}
584
Ronald Cron83282872020-11-22 14:02:39 +0100585psa_status_t psa_driver_wrapper_import_key(
586 const psa_key_attributes_t *attributes,
587 const uint8_t *data,
588 size_t data_length,
589 uint8_t *key_buffer,
590 size_t key_buffer_size,
591 size_t *key_buffer_length,
592 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200593{
Steven Cooreman04524762020-10-13 17:43:44 +0200594 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100595 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
596 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200597
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100598 /* Try dynamically-registered SE interface first */
599#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
600 const psa_drv_se_t *drv;
601 psa_drv_se_context_t *drv_context;
602
603 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
604 {
605 if( drv->key_management == NULL ||
606 drv->key_management->p_import == NULL )
607 return( PSA_ERROR_NOT_SUPPORTED );
608
609 /* The driver should set the number of key bits, however in
610 * case it doesn't, we initialize bits to an invalid value. */
611 *bits = PSA_MAX_KEY_BITS + 1;
612 status = drv->key_management->p_import(
613 drv_context,
614 *( (psa_key_slot_number_t *)key_buffer ),
615 attributes, data, data_length, bits );
616
617 if( status != PSA_SUCCESS )
618 return( status );
619
620 if( (*bits) > PSA_MAX_KEY_BITS )
621 return( PSA_ERROR_NOT_SUPPORTED );
622
623 return( PSA_SUCCESS );
624 }
625#endif /* PSA_CRYPTO_SE_C */
626
Ronald Cronbf33c932020-11-28 18:06:53 +0100627 switch( location )
628 {
629 case PSA_KEY_LOCATION_LOCAL_STORAGE:
630 /* Key is stored in the slot in export representation, so
631 * cycle through all known transparent accelerators */
632#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
633#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200634 status = mbedtls_test_transparent_import_key(
635 attributes,
636 data, data_length,
637 key_buffer, key_buffer_size,
638 key_buffer_length, bits );
Ronald Cronbf33c932020-11-28 18:06:53 +0100639 /* Declared with fallback == true */
640 if( status != PSA_ERROR_NOT_SUPPORTED )
641 return( status );
642#endif /* PSA_CRYPTO_DRIVER_TEST */
643#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
644 /* Fell through, meaning no accelerator supports this operation */
645 return( psa_import_key_into_slot( attributes,
646 data, data_length,
647 key_buffer, key_buffer_size,
648 key_buffer_length, bits ) );
Archana4d7ae1d2021-07-07 02:50:22 +0530649 /* Add cases for opaque driver here */
650#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
651#if defined(PSA_CRYPTO_DRIVER_TEST)
652 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
653 return( mbedtls_test_opaque_import_key(
654 attributes,
655 data, data_length,
656 key_buffer, key_buffer_size,
657 key_buffer_length, bits ) );
658#endif /* PSA_CRYPTO_DRIVER_TEST */
659#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cronbf33c932020-11-28 18:06:53 +0100660 default:
Ronald Cronbf33c932020-11-28 18:06:53 +0100661 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100662 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100663 }
664
Steven Cooreman04524762020-10-13 17:43:44 +0200665}
666
Ronald Cron67227982020-11-26 15:16:05 +0100667psa_status_t psa_driver_wrapper_export_key(
668 const psa_key_attributes_t *attributes,
669 const uint8_t *key_buffer, size_t key_buffer_size,
670 uint8_t *data, size_t data_size, size_t *data_length )
671
672{
673 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
674 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
675 psa_get_key_lifetime( attributes ) );
676
Ronald Cron152e3f82020-11-26 16:06:41 +0100677 /* Try dynamically-registered SE interface first */
678#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
679 const psa_drv_se_t *drv;
680 psa_drv_se_context_t *drv_context;
681
682 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
683 {
684 if( ( drv->key_management == NULL ) ||
685 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100686 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100687 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100688 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100689
690 return( drv->key_management->p_export(
691 drv_context,
692 *( (psa_key_slot_number_t *)key_buffer ),
693 data, data_size, data_length ) );
694 }
695#endif /* PSA_CRYPTO_SE_C */
696
Ronald Cron67227982020-11-26 15:16:05 +0100697 switch( location )
698 {
699 case PSA_KEY_LOCATION_LOCAL_STORAGE:
700 return( psa_export_key_internal( attributes,
701 key_buffer,
702 key_buffer_size,
703 data,
704 data_size,
705 data_length ) );
706
707 /* Add cases for opaque driver here */
708#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
709#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100710 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200711 return( mbedtls_test_opaque_export_key( attributes,
712 key_buffer,
713 key_buffer_size,
714 data,
715 data_size,
716 data_length ) );
Ronald Cron67227982020-11-26 15:16:05 +0100717#endif /* PSA_CRYPTO_DRIVER_TEST */
718#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
719 default:
720 /* Key is declared with a lifetime not known to us */
721 return( status );
722 }
723}
724
Ronald Cron84cc9942020-11-25 14:30:05 +0100725psa_status_t psa_driver_wrapper_export_public_key(
726 const psa_key_attributes_t *attributes,
727 const uint8_t *key_buffer, size_t key_buffer_size,
728 uint8_t *data, size_t data_size, size_t *data_length )
729
Steven Cooremanb9b84422020-10-14 14:39:20 +0200730{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200731 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100732 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
733 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200734
Ronald Cron152e3f82020-11-26 16:06:41 +0100735 /* Try dynamically-registered SE interface first */
736#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
737 const psa_drv_se_t *drv;
738 psa_drv_se_context_t *drv_context;
739
740 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
741 {
742 if( ( drv->key_management == NULL ) ||
743 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100744 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100745 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100746 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100747
748 return( drv->key_management->p_export_public(
749 drv_context,
750 *( (psa_key_slot_number_t *)key_buffer ),
751 data, data_size, data_length ) );
752 }
753#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
754
Steven Cooremanb9b84422020-10-14 14:39:20 +0200755 switch( location )
756 {
757 case PSA_KEY_LOCATION_LOCAL_STORAGE:
758 /* Key is stored in the slot in export representation, so
759 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100760#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200761#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200762 status = mbedtls_test_transparent_export_public_key(
763 attributes,
764 key_buffer,
765 key_buffer_size,
766 data,
767 data_size,
768 data_length );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200769 /* Declared with fallback == true */
770 if( status != PSA_ERROR_NOT_SUPPORTED )
771 return( status );
772#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100773#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200774 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100775 return( psa_export_public_key_internal( attributes,
776 key_buffer,
777 key_buffer_size,
778 data,
779 data_size,
780 data_length ) );
781
Steven Cooremanb9b84422020-10-14 14:39:20 +0200782 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100783#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200784#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100785 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200786 return( mbedtls_test_opaque_export_public_key( attributes,
787 key_buffer,
788 key_buffer_size,
789 data,
790 data_size,
791 data_length ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200792#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100793#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200794 default:
795 /* Key is declared with a lifetime not known to us */
796 return( status );
797 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200798}
799
Steven Cooreman6801f082021-02-19 17:21:22 +0100800psa_status_t psa_driver_wrapper_get_builtin_key(
801 psa_drv_slot_number_t slot_number,
802 psa_key_attributes_t *attributes,
803 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
804{
805 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
806 switch( location )
807 {
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100808#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100809 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200810 return( mbedtls_test_opaque_get_builtin_key(
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100811 slot_number,
812 attributes,
813 key_buffer, key_buffer_size, key_buffer_length ) );
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100814#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman6801f082021-02-19 17:21:22 +0100815 default:
816 (void) slot_number;
817 (void) key_buffer;
818 (void) key_buffer_size;
819 (void) key_buffer_length;
820 return( PSA_ERROR_DOES_NOT_EXIST );
821 }
822}
823
Archana8a180362021-07-05 02:18:48 +0530824psa_status_t psa_driver_wrapper_copy_key(
825 psa_key_attributes_t *attributes,
Archana9d17bf42021-09-10 06:22:44 +0530826 const uint8_t *source_key, size_t source_key_length,
Archana449608b2021-09-08 15:36:05 +0530827 uint8_t *target_key_buffer, size_t target_key_buffer_size,
828 size_t *target_key_buffer_length )
Archana8a180362021-07-05 02:18:48 +0530829{
Archana449608b2021-09-08 15:36:05 +0530830 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
831 psa_key_location_t location =
832 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Archana8a180362021-07-05 02:18:48 +0530833
Archana9d17bf42021-09-10 06:22:44 +0530834#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
835 const psa_drv_se_t *drv;
836 psa_drv_se_context_t *drv_context;
837
838 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
839 {
840 /* Copying to a secure element is not implemented yet. */
841 return( PSA_ERROR_NOT_SUPPORTED );
842 }
843#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
844
Archana8a180362021-07-05 02:18:48 +0530845 switch( location )
846 {
847#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
848#if defined(PSA_CRYPTO_DRIVER_TEST)
849 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
850 return( mbedtls_test_opaque_copy_key( attributes, source_key,
Archana9d17bf42021-09-10 06:22:44 +0530851 source_key_length,
Archana8a180362021-07-05 02:18:48 +0530852 target_key_buffer,
Archana449608b2021-09-08 15:36:05 +0530853 target_key_buffer_size,
854 target_key_buffer_length) );
Archana8a180362021-07-05 02:18:48 +0530855#endif /* PSA_CRYPTO_DRIVER_TEST */
856#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
857 default:
858 (void)source_key;
Archana9d17bf42021-09-10 06:22:44 +0530859 (void)source_key_length;
Archana8a180362021-07-05 02:18:48 +0530860 (void)target_key_buffer;
Archana449608b2021-09-08 15:36:05 +0530861 (void)target_key_buffer_size;
862 (void)target_key_buffer_length;
Archana8a180362021-07-05 02:18:48 +0530863 status = PSA_ERROR_INVALID_ARGUMENT;
864 }
865 return( status );
866}
867
Steven Cooreman37941cb2020-07-28 18:49:51 +0200868/*
869 * Cipher functions
870 */
871psa_status_t psa_driver_wrapper_cipher_encrypt(
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100872 const psa_key_attributes_t *attributes,
873 const uint8_t *key_buffer,
874 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200875 psa_algorithm_t alg,
Ronald Cron9b674282021-07-09 09:19:35 +0200876 const uint8_t *iv,
877 size_t iv_length,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200878 const uint8_t *input,
879 size_t input_length,
880 uint8_t *output,
881 size_t output_size,
882 size_t *output_length )
883{
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100884 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
885 psa_key_location_t location =
886 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200887
888 switch( location )
889 {
890 case PSA_KEY_LOCATION_LOCAL_STORAGE:
891 /* Key is stored in the slot in export representation, so
892 * cycle through all known transparent accelerators */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100893#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200894#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100895 status = mbedtls_test_transparent_cipher_encrypt( attributes,
896 key_buffer,
897 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200898 alg,
Ronald Cron9b674282021-07-09 09:19:35 +0200899 iv,
900 iv_length,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200901 input,
902 input_length,
903 output,
904 output_size,
905 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200906 /* Declared with fallback == true */
907 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200908 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200909#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100910#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
911
912#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
913 return( mbedtls_psa_cipher_encrypt( attributes,
914 key_buffer,
915 key_buffer_size,
916 alg,
Ronald Cron9b674282021-07-09 09:19:35 +0200917 iv,
918 iv_length,
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100919 input,
920 input_length,
921 output,
922 output_size,
923 output_length ) );
gabor-mezei-arm0a93b662021-06-25 15:43:32 +0200924#else
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200925 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-arm0a93b662021-06-25 15:43:32 +0200926#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100927
Steven Cooreman37941cb2020-07-28 18:49:51 +0200928 /* Add cases for opaque driver here */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100929#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200930#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100931 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100932 return( mbedtls_test_opaque_cipher_encrypt( attributes,
933 key_buffer,
934 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200935 alg,
Ronald Cron9b674282021-07-09 09:19:35 +0200936 iv,
937 iv_length,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200938 input,
939 input_length,
940 output,
941 output_size,
942 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200943#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100944#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
945
Steven Cooreman37941cb2020-07-28 18:49:51 +0200946 default:
947 /* Key is declared with a lifetime not known to us */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100948 (void)status;
949 (void)key_buffer;
950 (void)key_buffer_size;
951 (void)alg;
952 (void)input;
953 (void)input_length;
954 (void)output;
955 (void)output_size;
956 (void)output_length;
957 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200958 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200959}
960
961psa_status_t psa_driver_wrapper_cipher_decrypt(
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100962 const psa_key_attributes_t *attributes,
963 const uint8_t *key_buffer,
964 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200965 psa_algorithm_t alg,
966 const uint8_t *input,
967 size_t input_length,
968 uint8_t *output,
969 size_t output_size,
970 size_t *output_length )
971{
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100972 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
973 psa_key_location_t location =
974 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200975
976 switch( location )
977 {
978 case PSA_KEY_LOCATION_LOCAL_STORAGE:
979 /* Key is stored in the slot in export representation, so
980 * cycle through all known transparent accelerators */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100981#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200982#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100983 status = mbedtls_test_transparent_cipher_decrypt( attributes,
984 key_buffer,
985 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200986 alg,
987 input,
988 input_length,
989 output,
990 output_size,
991 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200992 /* Declared with fallback == true */
993 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200994 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200995#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100996#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
997
998#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
999 return( mbedtls_psa_cipher_decrypt( attributes,
1000 key_buffer,
1001 key_buffer_size,
1002 alg,
1003 input,
1004 input_length,
1005 output,
1006 output_size,
1007 output_length ) );
gabor-mezei-arm0a93b662021-06-25 15:43:32 +02001008#else
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001009 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-arm0a93b662021-06-25 15:43:32 +02001010#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
gabor-mezei-arma9449a02021-03-25 11:17:10 +01001011
Steven Cooreman37941cb2020-07-28 18:49:51 +02001012 /* Add cases for opaque driver here */
gabor-mezei-arma9449a02021-03-25 11:17:10 +01001013#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001014#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +01001015 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arma9449a02021-03-25 11:17:10 +01001016 return( mbedtls_test_opaque_cipher_decrypt( attributes,
1017 key_buffer,
1018 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +02001019 alg,
1020 input,
1021 input_length,
1022 output,
1023 output_size,
1024 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001025#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +01001026#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1027
Steven Cooreman37941cb2020-07-28 18:49:51 +02001028 default:
1029 /* Key is declared with a lifetime not known to us */
gabor-mezei-arma9449a02021-03-25 11:17:10 +01001030 (void)status;
1031 (void)key_buffer;
1032 (void)key_buffer_size;
1033 (void)alg;
1034 (void)input;
1035 (void)input_length;
1036 (void)output;
1037 (void)output_size;
1038 (void)output_length;
1039 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001040 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001041}
1042
1043psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +01001044 psa_cipher_operation_t *operation,
1045 const psa_key_attributes_t *attributes,
1046 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001047 psa_algorithm_t alg )
1048{
Ronald Cron0b805592020-12-14 18:08:20 +01001049 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona4af55f2020-12-14 14:36:06 +01001050 psa_key_location_t location =
1051 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001052
Steven Cooreman37941cb2020-07-28 18:49:51 +02001053 switch( location )
1054 {
1055 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1056 /* Key is stored in the slot in export representation, so
1057 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +01001058#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001059#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001060 status = mbedtls_test_transparent_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001061 &operation->ctx.transparent_test_driver_ctx,
1062 attributes,
1063 key_buffer,
1064 key_buffer_size,
1065 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001066 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +02001067 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001068 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +02001069
Ronald Cron0b805592020-12-14 18:08:20 +01001070 if( status != PSA_ERROR_NOT_SUPPORTED )
1071 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001072#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001073#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001074#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001075 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +01001076 status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001077 attributes,
1078 key_buffer,
1079 key_buffer_size,
1080 alg );
1081 if( status == PSA_SUCCESS )
Ronald Cron6e412a72021-03-10 09:58:47 +01001082 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Ronald Cron49fafa92021-03-10 08:34:23 +01001083
Ronald Cron7b4154d2021-03-19 14:49:41 +01001084 if( status != PSA_ERROR_NOT_SUPPORTED )
1085 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001086#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1087 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron0b805592020-12-14 18:08:20 +01001088
Steven Cooreman37941cb2020-07-28 18:49:51 +02001089 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +01001090#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001091#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +01001092 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001093 status = mbedtls_test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001094 &operation->ctx.opaque_test_driver_ctx,
1095 attributes,
1096 key_buffer, key_buffer_size,
1097 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001098
Steven Cooreman37941cb2020-07-28 18:49:51 +02001099 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001100 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001101
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001102 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001103#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001104#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +02001105 default:
1106 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +01001107 (void)status;
Ronald Cron7a55deb2021-04-28 14:29:00 +02001108 (void)operation;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001109 (void)key_buffer;
1110 (void)key_buffer_size;
1111 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +02001112 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001113 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001114}
1115
1116psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +01001117 psa_cipher_operation_t *operation,
1118 const psa_key_attributes_t *attributes,
1119 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001120 psa_algorithm_t alg )
1121{
Steven Cooreman37941cb2020-07-28 18:49:51 +02001122 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crona4af55f2020-12-14 14:36:06 +01001123 psa_key_location_t location =
1124 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001125
Steven Cooreman37941cb2020-07-28 18:49:51 +02001126 switch( location )
1127 {
1128 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1129 /* Key is stored in the slot in export representation, so
1130 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +01001131#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001132#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001133 status = mbedtls_test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001134 &operation->ctx.transparent_test_driver_ctx,
1135 attributes,
1136 key_buffer,
1137 key_buffer_size,
1138 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001139 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +02001140 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001141 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +02001142
Ronald Cron0b805592020-12-14 18:08:20 +01001143 if( status != PSA_ERROR_NOT_SUPPORTED )
1144 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001145#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001146#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001147#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001148 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +01001149 status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001150 attributes,
1151 key_buffer,
1152 key_buffer_size,
1153 alg );
1154 if( status == PSA_SUCCESS )
1155 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1156
1157 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001158#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1159 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron49fafa92021-03-10 08:34:23 +01001160
Steven Cooreman37941cb2020-07-28 18:49:51 +02001161 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +01001162#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001163#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +01001164 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001165 status = mbedtls_test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001166 &operation->ctx.opaque_test_driver_ctx,
1167 attributes,
1168 key_buffer, key_buffer_size,
1169 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001170
Steven Cooreman37941cb2020-07-28 18:49:51 +02001171 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001172 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001173
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001174 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001175#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001176#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +02001177 default:
1178 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +01001179 (void)status;
Ronald Cron17006702021-12-03 15:25:24 +01001180 (void)operation;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001181 (void)key_buffer;
1182 (void)key_buffer_size;
1183 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +02001184 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001185 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001186}
1187
Steven Cooreman37941cb2020-07-28 18:49:51 +02001188psa_status_t psa_driver_wrapper_cipher_set_iv(
Ronald Cron6056fe82020-12-15 13:58:07 +01001189 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001190 const uint8_t *iv,
1191 size_t iv_length )
1192{
Ronald Cron49fafa92021-03-10 08:34:23 +01001193 switch( operation->id )
1194 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001195#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001196 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001197 return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001198 iv,
1199 iv_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001200#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001201
1202#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001203#if defined(PSA_CRYPTO_DRIVER_TEST)
1204 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001205 return( mbedtls_test_transparent_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001206 &operation->ctx.transparent_test_driver_ctx,
1207 iv, iv_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001208
Steven Cooreman37941cb2020-07-28 18:49:51 +02001209 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001210 return( mbedtls_test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001211 &operation->ctx.opaque_test_driver_ctx,
1212 iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001213#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001214#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001215 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001216
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001217 (void)iv;
1218 (void)iv_length;
1219
Ronald Crondd24c9b2020-12-15 14:10:01 +01001220 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001221}
1222
1223psa_status_t psa_driver_wrapper_cipher_update(
Ronald Cron6056fe82020-12-15 13:58:07 +01001224 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001225 const uint8_t *input,
1226 size_t input_length,
1227 uint8_t *output,
1228 size_t output_size,
1229 size_t *output_length )
1230{
Ronald Cron49fafa92021-03-10 08:34:23 +01001231 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001232 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001233#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001234 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001235 return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001236 input,
1237 input_length,
1238 output,
1239 output_size,
1240 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001241#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1242
Ronald Cron49fafa92021-03-10 08:34:23 +01001243#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001244#if defined(PSA_CRYPTO_DRIVER_TEST)
1245 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001246 return( mbedtls_test_transparent_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001247 &operation->ctx.transparent_test_driver_ctx,
1248 input, input_length,
1249 output, output_size, output_length ) );
1250
Steven Cooreman37941cb2020-07-28 18:49:51 +02001251 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001252 return( mbedtls_test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001253 &operation->ctx.opaque_test_driver_ctx,
1254 input, input_length,
1255 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001256#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001257#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001258 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001259
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001260 (void)input;
1261 (void)input_length;
1262 (void)output;
1263 (void)output_size;
1264 (void)output_length;
1265
Ronald Crondd24c9b2020-12-15 14:10:01 +01001266 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001267}
1268
1269psa_status_t psa_driver_wrapper_cipher_finish(
Ronald Cron6056fe82020-12-15 13:58:07 +01001270 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001271 uint8_t *output,
1272 size_t output_size,
1273 size_t *output_length )
1274{
Ronald Cron49fafa92021-03-10 08:34:23 +01001275 switch( operation->id )
1276 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001277#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001278 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001279 return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001280 output,
1281 output_size,
1282 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001283#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001284
1285#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001286#if defined(PSA_CRYPTO_DRIVER_TEST)
1287 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001288 return( mbedtls_test_transparent_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001289 &operation->ctx.transparent_test_driver_ctx,
1290 output, output_size, output_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001291
Steven Cooreman37941cb2020-07-28 18:49:51 +02001292 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001293 return( mbedtls_test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001294 &operation->ctx.opaque_test_driver_ctx,
1295 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001296#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001297#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001298 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001299
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001300 (void)output;
1301 (void)output_size;
1302 (void)output_length;
1303
Ronald Crondd24c9b2020-12-15 14:10:01 +01001304 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001305}
1306
1307psa_status_t psa_driver_wrapper_cipher_abort(
Ronald Cron6056fe82020-12-15 13:58:07 +01001308 psa_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001309{
Ronald Crondd24c9b2020-12-15 14:10:01 +01001310 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001311
Ronald Cron49fafa92021-03-10 08:34:23 +01001312 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001313 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001314#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001315 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001316 return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001317#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +01001318
1319#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001320#if defined(PSA_CRYPTO_DRIVER_TEST)
1321 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001322 status = mbedtls_test_transparent_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001323 &operation->ctx.transparent_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001324 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001325 &operation->ctx.transparent_test_driver_ctx,
1326 sizeof( operation->ctx.transparent_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001327 return( status );
Ronald Cron49fafa92021-03-10 08:34:23 +01001328
Steven Cooreman37941cb2020-07-28 18:49:51 +02001329 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001330 status = mbedtls_test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001331 &operation->ctx.opaque_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001332 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001333 &operation->ctx.opaque_test_driver_ctx,
1334 sizeof( operation->ctx.opaque_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001335 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001336#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001337#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001338 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001339
Ronald Cron49fafa92021-03-10 08:34:23 +01001340 (void)status;
Ronald Crondd24c9b2020-12-15 14:10:01 +01001341 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001342}
1343
Steven Cooreman1e582352021-02-18 17:24:37 +01001344/*
1345 * Hashing functions
1346 */
1347psa_status_t psa_driver_wrapper_hash_compute(
1348 psa_algorithm_t alg,
1349 const uint8_t *input,
1350 size_t input_length,
1351 uint8_t *hash,
1352 size_t hash_size,
1353 size_t *hash_length)
1354{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001355 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001356
1357 /* Try accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001358#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001359 status = mbedtls_test_transparent_hash_compute(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001360 alg, input, input_length, hash, hash_size, hash_length );
Steven Cooremanf7638102021-03-04 15:14:36 +01001361 if( status != PSA_ERROR_NOT_SUPPORTED )
1362 return( status );
1363#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001364
1365 /* If software fallback is compiled in, try fallback */
1366#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1367 status = mbedtls_psa_hash_compute( alg, input, input_length,
1368 hash, hash_size, hash_length );
1369 if( status != PSA_ERROR_NOT_SUPPORTED )
1370 return( status );
1371#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001372 (void) status;
1373 (void) alg;
1374 (void) input;
1375 (void) input_length;
1376 (void) hash;
1377 (void) hash_size;
1378 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001379
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001380 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman1e582352021-02-18 17:24:37 +01001381}
1382
1383psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001384 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001385 psa_algorithm_t alg )
1386{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001387 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001388
Steven Cooreman1e582352021-02-18 17:24:37 +01001389 /* Try setup on accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001390#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001391 status = mbedtls_test_transparent_hash_setup(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001392 &operation->ctx.test_driver_ctx, alg );
Steven Cooremanf7638102021-03-04 15:14:36 +01001393 if( status == PSA_SUCCESS )
1394 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1395
1396 if( status != PSA_ERROR_NOT_SUPPORTED )
1397 return( status );
1398#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001399
1400 /* If software fallback is compiled in, try fallback */
1401#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001402 status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
Steven Cooreman1e582352021-02-18 17:24:37 +01001403 if( status == PSA_SUCCESS )
Steven Cooreman1e582352021-02-18 17:24:37 +01001404 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Steven Cooreman1e582352021-02-18 17:24:37 +01001405
1406 if( status != PSA_ERROR_NOT_SUPPORTED )
1407 return( status );
1408#endif
1409 /* Nothing left to try if we fall through here */
1410 (void) status;
1411 (void) operation;
1412 (void) alg;
1413 return( PSA_ERROR_NOT_SUPPORTED );
1414}
1415
1416psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001417 const psa_hash_operation_t *source_operation,
1418 psa_hash_operation_t *target_operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001419{
Steven Cooreman1e582352021-02-18 17:24:37 +01001420 switch( source_operation->id )
1421 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001422#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1423 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1424 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1425 return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
1426 &target_operation->ctx.mbedtls_ctx ) );
1427#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001428#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001429 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1430 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Ronald Cron7f13fa22021-04-13 12:41:34 +02001431 return( mbedtls_test_transparent_hash_clone(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001432 &source_operation->ctx.test_driver_ctx,
1433 &target_operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001434#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001435 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001436 (void) target_operation;
1437 return( PSA_ERROR_BAD_STATE );
1438 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001439}
1440
1441psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001442 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001443 const uint8_t *input,
1444 size_t input_length )
1445{
Steven Cooreman1e582352021-02-18 17:24:37 +01001446 switch( operation->id )
1447 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001448#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1449 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1450 return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
1451 input, input_length ) );
1452#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001453#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001454 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001455 return( mbedtls_test_transparent_hash_update(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001456 &operation->ctx.test_driver_ctx,
1457 input, input_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001458#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001459 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001460 (void) input;
1461 (void) input_length;
1462 return( PSA_ERROR_BAD_STATE );
1463 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001464}
1465
1466psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001467 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001468 uint8_t *hash,
1469 size_t hash_size,
1470 size_t *hash_length )
1471{
Steven Cooreman1e582352021-02-18 17:24:37 +01001472 switch( operation->id )
1473 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001474#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1475 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1476 return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
1477 hash, hash_size, hash_length ) );
1478#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001479#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001480 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001481 return( mbedtls_test_transparent_hash_finish(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001482 &operation->ctx.test_driver_ctx,
1483 hash, hash_size, hash_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001484#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001485 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001486 (void) hash;
1487 (void) hash_size;
1488 (void) hash_length;
1489 return( PSA_ERROR_BAD_STATE );
1490 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001491}
1492
1493psa_status_t psa_driver_wrapper_hash_abort(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001494 psa_hash_operation_t *operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001495{
Steven Cooreman1e582352021-02-18 17:24:37 +01001496 switch( operation->id )
1497 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001498#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1499 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1500 return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
1501#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001502#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001503 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001504 return( mbedtls_test_transparent_hash_abort(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001505 &operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001506#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001507 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001508 return( PSA_ERROR_BAD_STATE );
1509 }
1510}
1511
Ronald Cronde822812021-03-17 16:08:20 +01001512psa_status_t psa_driver_wrapper_aead_encrypt(
1513 const psa_key_attributes_t *attributes,
1514 const uint8_t *key_buffer, size_t key_buffer_size,
1515 psa_algorithm_t alg,
1516 const uint8_t *nonce, size_t nonce_length,
1517 const uint8_t *additional_data, size_t additional_data_length,
1518 const uint8_t *plaintext, size_t plaintext_length,
1519 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
1520{
1521 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1522 psa_key_location_t location =
1523 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1524
1525 switch( location )
1526 {
1527 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1528 /* Key is stored in the slot in export representation, so
1529 * cycle through all known transparent accelerators */
1530
1531#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1532#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001533 status = mbedtls_test_transparent_aead_encrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001534 attributes, key_buffer, key_buffer_size,
1535 alg,
1536 nonce, nonce_length,
1537 additional_data, additional_data_length,
1538 plaintext, plaintext_length,
1539 ciphertext, ciphertext_size, ciphertext_length );
1540 /* Declared with fallback == true */
1541 if( status != PSA_ERROR_NOT_SUPPORTED )
1542 return( status );
1543#endif /* PSA_CRYPTO_DRIVER_TEST */
1544#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1545
1546 /* Fell through, meaning no accelerator supports this operation */
1547 return( mbedtls_psa_aead_encrypt(
1548 attributes, key_buffer, key_buffer_size,
1549 alg,
1550 nonce, nonce_length,
1551 additional_data, additional_data_length,
1552 plaintext, plaintext_length,
1553 ciphertext, ciphertext_size, ciphertext_length ) );
1554
1555 /* Add cases for opaque driver here */
1556
1557 default:
1558 /* Key is declared with a lifetime not known to us */
1559 (void)status;
1560 return( PSA_ERROR_INVALID_ARGUMENT );
1561 }
1562}
1563
1564psa_status_t psa_driver_wrapper_aead_decrypt(
1565 const psa_key_attributes_t *attributes,
1566 const uint8_t *key_buffer, size_t key_buffer_size,
1567 psa_algorithm_t alg,
1568 const uint8_t *nonce, size_t nonce_length,
1569 const uint8_t *additional_data, size_t additional_data_length,
1570 const uint8_t *ciphertext, size_t ciphertext_length,
1571 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
1572{
1573 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1574 psa_key_location_t location =
1575 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1576
1577 switch( location )
1578 {
1579 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1580 /* Key is stored in the slot in export representation, so
1581 * cycle through all known transparent accelerators */
1582
1583#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1584#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001585 status = mbedtls_test_transparent_aead_decrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001586 attributes, key_buffer, key_buffer_size,
1587 alg,
1588 nonce, nonce_length,
1589 additional_data, additional_data_length,
1590 ciphertext, ciphertext_length,
1591 plaintext, plaintext_size, plaintext_length );
1592 /* Declared with fallback == true */
1593 if( status != PSA_ERROR_NOT_SUPPORTED )
1594 return( status );
1595#endif /* PSA_CRYPTO_DRIVER_TEST */
1596#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1597
1598 /* Fell through, meaning no accelerator supports this operation */
1599 return( mbedtls_psa_aead_decrypt(
1600 attributes, key_buffer, key_buffer_size,
1601 alg,
1602 nonce, nonce_length,
1603 additional_data, additional_data_length,
1604 ciphertext, ciphertext_length,
1605 plaintext, plaintext_size, plaintext_length ) );
1606
1607 /* Add cases for opaque driver here */
1608
1609 default:
1610 /* Key is declared with a lifetime not known to us */
1611 (void)status;
1612 return( PSA_ERROR_INVALID_ARGUMENT );
1613 }
1614}
Steven Cooremand13a70f2021-03-19 15:24:23 +01001615
Paul Elliott6504aa62021-04-20 17:09:36 +01001616psa_status_t psa_driver_wrapper_aead_encrypt_setup(
1617 psa_aead_operation_t *operation,
1618 const psa_key_attributes_t *attributes,
1619 const uint8_t *key_buffer, size_t key_buffer_size,
1620 psa_algorithm_t alg )
1621{
1622 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1623 psa_key_location_t location =
1624 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1625
1626 switch( location )
1627 {
1628 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1629 /* Key is stored in the slot in export representation, so
1630 * cycle through all known transparent accelerators */
1631
1632#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1633#if defined(PSA_CRYPTO_DRIVER_TEST)
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001634 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Paul Elliotta218ceb2021-05-07 15:10:31 +01001635 status = mbedtls_test_transparent_aead_encrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001636 &operation->ctx.transparent_test_driver_ctx,
1637 attributes, key_buffer, key_buffer_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001638 alg );
Paul Elliott6504aa62021-04-20 17:09:36 +01001639
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001640 /* Declared with fallback == true */
Paul Elliott6504aa62021-04-20 17:09:36 +01001641 if( status != PSA_ERROR_NOT_SUPPORTED )
1642 return( status );
1643#endif /* PSA_CRYPTO_DRIVER_TEST */
1644#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1645
1646 /* Fell through, meaning no accelerator supports this operation */
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001647 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Paul Elliott6504aa62021-04-20 17:09:36 +01001648 status = mbedtls_psa_aead_encrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001649 &operation->ctx.mbedtls_ctx, attributes,
Paul Elliott6504aa62021-04-20 17:09:36 +01001650 key_buffer, key_buffer_size,
1651 alg );
1652
Paul Elliott6504aa62021-04-20 17:09:36 +01001653 return( status );
1654
1655 /* Add cases for opaque driver here */
1656
1657 default:
1658 /* Key is declared with a lifetime not known to us */
1659 (void)status;
1660 return( PSA_ERROR_INVALID_ARGUMENT );
1661 }
1662}
1663
1664psa_status_t psa_driver_wrapper_aead_decrypt_setup(
1665 psa_aead_operation_t *operation,
1666 const psa_key_attributes_t *attributes,
1667 const uint8_t *key_buffer, size_t key_buffer_size,
1668 psa_algorithm_t alg )
1669{
1670 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1671 psa_key_location_t location =
1672 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1673
1674 switch( location )
1675 {
1676 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1677 /* Key is stored in the slot in export representation, so
1678 * cycle through all known transparent accelerators */
1679
1680#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1681#if defined(PSA_CRYPTO_DRIVER_TEST)
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001682 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Paul Elliotta218ceb2021-05-07 15:10:31 +01001683 status = mbedtls_test_transparent_aead_decrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001684 &operation->ctx.transparent_test_driver_ctx,
Paul Elliott5d3a3c32021-08-19 18:34:41 +01001685 attributes,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001686 key_buffer, key_buffer_size,
1687 alg );
Paul Elliott6504aa62021-04-20 17:09:36 +01001688
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001689 /* Declared with fallback == true */
Paul Elliott6504aa62021-04-20 17:09:36 +01001690 if( status != PSA_ERROR_NOT_SUPPORTED )
1691 return( status );
1692#endif /* PSA_CRYPTO_DRIVER_TEST */
1693#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1694
1695 /* Fell through, meaning no accelerator supports this operation */
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001696 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Paul Elliott6504aa62021-04-20 17:09:36 +01001697 status = mbedtls_psa_aead_decrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001698 &operation->ctx.mbedtls_ctx,
Paul Elliott5d3a3c32021-08-19 18:34:41 +01001699 attributes,
Paul Elliott6504aa62021-04-20 17:09:36 +01001700 key_buffer, key_buffer_size,
1701 alg );
1702
Paul Elliott6504aa62021-04-20 17:09:36 +01001703 return( status );
1704
1705 /* Add cases for opaque driver here */
1706
1707 default:
1708 /* Key is declared with a lifetime not known to us */
1709 (void)status;
1710 return( PSA_ERROR_INVALID_ARGUMENT );
1711 }
1712}
1713
Paul Elliott6504aa62021-04-20 17:09:36 +01001714psa_status_t psa_driver_wrapper_aead_set_nonce(
1715 psa_aead_operation_t *operation,
1716 const uint8_t *nonce,
1717 size_t nonce_length )
1718{
1719 switch( operation->id )
1720 {
Paul Elliott5c656cb2021-05-19 14:15:01 +01001721#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001722 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001723 return( mbedtls_psa_aead_set_nonce( &operation->ctx.mbedtls_ctx,
1724 nonce,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001725 nonce_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001726
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001727#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001728
1729#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1730#if defined(PSA_CRYPTO_DRIVER_TEST)
1731 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001732 return( mbedtls_test_transparent_aead_set_nonce(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001733 &operation->ctx.transparent_test_driver_ctx,
1734 nonce, nonce_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001735
1736 /* Add cases for opaque driver here */
1737
1738#endif /* PSA_CRYPTO_DRIVER_TEST */
1739#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1740 }
1741
1742 (void)nonce;
1743 (void)nonce_length;
1744
1745 return( PSA_ERROR_INVALID_ARGUMENT );
1746}
1747
1748psa_status_t psa_driver_wrapper_aead_set_lengths(
1749 psa_aead_operation_t *operation,
1750 size_t ad_length,
1751 size_t plaintext_length )
1752{
1753 switch( operation->id )
1754 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001755#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001756 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottdff6c5d2021-09-28 11:00:20 +01001757 return( mbedtls_psa_aead_set_lengths( &operation->ctx.mbedtls_ctx,
1758 ad_length,
1759 plaintext_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001760
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001761#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001762
1763#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1764#if defined(PSA_CRYPTO_DRIVER_TEST)
1765 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001766 return( mbedtls_test_transparent_aead_set_lengths(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001767 &operation->ctx.transparent_test_driver_ctx,
1768 ad_length, plaintext_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001769
1770 /* Add cases for opaque driver here */
1771
1772#endif /* PSA_CRYPTO_DRIVER_TEST */
1773#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1774 }
1775
1776 (void)ad_length;
1777 (void)plaintext_length;
1778
1779 return( PSA_ERROR_INVALID_ARGUMENT );
1780}
1781
1782psa_status_t psa_driver_wrapper_aead_update_ad(
1783 psa_aead_operation_t *operation,
1784 const uint8_t *input,
1785 size_t input_length )
1786{
1787 switch( operation->id )
1788 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001789#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001790 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001791 return( mbedtls_psa_aead_update_ad( &operation->ctx.mbedtls_ctx,
1792 input,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001793 input_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001794
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001795#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001796
1797#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1798#if defined(PSA_CRYPTO_DRIVER_TEST)
1799 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001800 return( mbedtls_test_transparent_aead_update_ad(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001801 &operation->ctx.transparent_test_driver_ctx,
1802 input, input_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001803
1804 /* Add cases for opaque driver here */
1805
1806#endif /* PSA_CRYPTO_DRIVER_TEST */
1807#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1808 }
1809
1810 (void)input;
1811 (void)input_length;
1812
1813 return( PSA_ERROR_INVALID_ARGUMENT );
1814}
1815
1816psa_status_t psa_driver_wrapper_aead_update(
1817 psa_aead_operation_t *operation,
1818 const uint8_t *input,
1819 size_t input_length,
1820 uint8_t *output,
1821 size_t output_size,
1822 size_t *output_length )
1823{
1824 switch( operation->id )
1825 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001826#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001827 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001828 return( mbedtls_psa_aead_update( &operation->ctx.mbedtls_ctx,
1829 input, input_length,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001830 output, output_size,
1831 output_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001832
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001833#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001834
1835#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1836#if defined(PSA_CRYPTO_DRIVER_TEST)
1837 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001838 return( mbedtls_test_transparent_aead_update(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001839 &operation->ctx.transparent_test_driver_ctx,
1840 input, input_length, output, output_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001841 output_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001842
1843 /* Add cases for opaque driver here */
1844
1845#endif /* PSA_CRYPTO_DRIVER_TEST */
1846#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1847 }
1848
1849 (void)input;
1850 (void)input_length;
1851 (void)output;
1852 (void)output_size;
1853 (void)output_length;
1854
1855 return( PSA_ERROR_INVALID_ARGUMENT );
1856}
1857
1858psa_status_t psa_driver_wrapper_aead_finish(
1859 psa_aead_operation_t *operation,
1860 uint8_t *ciphertext,
1861 size_t ciphertext_size,
1862 size_t *ciphertext_length,
1863 uint8_t *tag,
1864 size_t tag_size,
1865 size_t *tag_length )
1866{
1867 switch( operation->id )
1868 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001869#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001870 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001871 return( mbedtls_psa_aead_finish( &operation->ctx.mbedtls_ctx,
1872 ciphertext,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001873 ciphertext_size,
1874 ciphertext_length, tag,
1875 tag_size, tag_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001876
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001877#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001878
1879#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1880#if defined(PSA_CRYPTO_DRIVER_TEST)
1881 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001882 return( mbedtls_test_transparent_aead_finish(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001883 &operation->ctx.transparent_test_driver_ctx,
1884 ciphertext, ciphertext_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001885 ciphertext_length, tag, tag_size, tag_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001886
1887 /* Add cases for opaque driver here */
1888
1889#endif /* PSA_CRYPTO_DRIVER_TEST */
1890#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1891 }
1892
1893 (void)ciphertext;
1894 (void)ciphertext_size;
1895 (void)ciphertext_length;
1896 (void)tag;
1897 (void)tag_size;
1898 (void)tag_length;
1899
1900 return( PSA_ERROR_INVALID_ARGUMENT );
1901}
1902
1903psa_status_t psa_driver_wrapper_aead_verify(
1904 psa_aead_operation_t *operation,
1905 uint8_t *plaintext,
1906 size_t plaintext_size,
1907 size_t *plaintext_length,
1908 const uint8_t *tag,
1909 size_t tag_length )
1910{
1911 switch( operation->id )
1912 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001913#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001914 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliott315628d2021-07-20 18:25:54 +01001915 {
1916 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1917 uint8_t check_tag[PSA_AEAD_TAG_MAX_SIZE];
1918 size_t check_tag_length;
1919
1920 status = mbedtls_psa_aead_finish( &operation->ctx.mbedtls_ctx,
1921 plaintext,
1922 plaintext_size,
1923 plaintext_length,
1924 check_tag,
Paul Elliottb183d562021-09-13 19:02:57 +01001925 sizeof( check_tag ),
Paul Elliott315628d2021-07-20 18:25:54 +01001926 &check_tag_length );
1927
1928 if( status == PSA_SUCCESS )
1929 {
1930 if( tag_length != check_tag_length ||
1931 mbedtls_psa_safer_memcmp( tag, check_tag, tag_length )
1932 != 0 )
1933 status = PSA_ERROR_INVALID_SIGNATURE;
1934 }
1935
Paul Elliott06b6b8c2021-09-13 19:02:04 +01001936 mbedtls_platform_zeroize( check_tag, sizeof( check_tag ) );
1937
Paul Elliott315628d2021-07-20 18:25:54 +01001938 return( status );
1939 }
Paul Elliott6504aa62021-04-20 17:09:36 +01001940
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001941#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001942
1943#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1944#if defined(PSA_CRYPTO_DRIVER_TEST)
1945 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001946 return( mbedtls_test_transparent_aead_verify(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001947 &operation->ctx.transparent_test_driver_ctx,
1948 plaintext, plaintext_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001949 plaintext_length, tag, tag_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001950
1951 /* Add cases for opaque driver here */
1952
1953#endif /* PSA_CRYPTO_DRIVER_TEST */
1954#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1955 }
1956
1957 (void)plaintext;
1958 (void)plaintext_size;
1959 (void)plaintext_length;
1960 (void)tag;
1961 (void)tag_length;
1962
1963 return( PSA_ERROR_INVALID_ARGUMENT );
1964}
1965
1966psa_status_t psa_driver_wrapper_aead_abort(
1967 psa_aead_operation_t *operation )
1968{
1969 switch( operation->id )
1970 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001971#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001972 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001973 return( mbedtls_psa_aead_abort( &operation->ctx.mbedtls_ctx ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001974
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001975#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001976
1977#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1978#if defined(PSA_CRYPTO_DRIVER_TEST)
1979 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001980 return( mbedtls_test_transparent_aead_abort(
1981 &operation->ctx.transparent_test_driver_ctx ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001982
1983 /* Add cases for opaque driver here */
1984
1985#endif /* PSA_CRYPTO_DRIVER_TEST */
1986#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1987 }
1988
1989 return( PSA_ERROR_INVALID_ARGUMENT );
1990}
Steven Cooremand13a70f2021-03-19 15:24:23 +01001991
1992/*
1993 * MAC functions
1994 */
1995psa_status_t psa_driver_wrapper_mac_compute(
1996 const psa_key_attributes_t *attributes,
1997 const uint8_t *key_buffer,
1998 size_t key_buffer_size,
1999 psa_algorithm_t alg,
2000 const uint8_t *input,
2001 size_t input_length,
2002 uint8_t *mac,
2003 size_t mac_size,
2004 size_t *mac_length )
2005{
2006 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2007 psa_key_location_t location =
2008 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
2009
2010 switch( location )
2011 {
2012 case PSA_KEY_LOCATION_LOCAL_STORAGE:
2013 /* Key is stored in the slot in export representation, so
2014 * cycle through all known transparent accelerators */
2015#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2016#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002017 status = mbedtls_test_transparent_mac_compute(
Steven Cooremand13a70f2021-03-19 15:24:23 +01002018 attributes, key_buffer, key_buffer_size, alg,
2019 input, input_length,
2020 mac, mac_size, mac_length );
2021 /* Declared with fallback == true */
2022 if( status != PSA_ERROR_NOT_SUPPORTED )
2023 return( status );
2024#endif /* PSA_CRYPTO_DRIVER_TEST */
2025#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2026#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2027 /* Fell through, meaning no accelerator supports this operation */
2028 status = mbedtls_psa_mac_compute(
2029 attributes, key_buffer, key_buffer_size, alg,
2030 input, input_length,
2031 mac, mac_size, mac_length );
2032 if( status != PSA_ERROR_NOT_SUPPORTED )
2033 return( status );
2034#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2035 return( PSA_ERROR_NOT_SUPPORTED );
2036
2037 /* Add cases for opaque driver here */
2038#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2039#if defined(PSA_CRYPTO_DRIVER_TEST)
2040 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002041 status = mbedtls_test_opaque_mac_compute(
Steven Cooremand13a70f2021-03-19 15:24:23 +01002042 attributes, key_buffer, key_buffer_size, alg,
2043 input, input_length,
2044 mac, mac_size, mac_length );
2045 return( status );
2046#endif /* PSA_CRYPTO_DRIVER_TEST */
2047#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2048 default:
2049 /* Key is declared with a lifetime not known to us */
2050 (void) key_buffer;
2051 (void) key_buffer_size;
2052 (void) alg;
2053 (void) input;
2054 (void) input_length;
2055 (void) mac;
2056 (void) mac_size;
2057 (void) mac_length;
2058 (void) status;
2059 return( PSA_ERROR_INVALID_ARGUMENT );
2060 }
2061}
2062
2063psa_status_t psa_driver_wrapper_mac_sign_setup(
2064 psa_mac_operation_t *operation,
2065 const psa_key_attributes_t *attributes,
2066 const uint8_t *key_buffer,
2067 size_t key_buffer_size,
2068 psa_algorithm_t alg )
2069{
2070 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2071 psa_key_location_t location =
2072 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
2073
2074 switch( location )
2075 {
2076 case PSA_KEY_LOCATION_LOCAL_STORAGE:
2077 /* Key is stored in the slot in export representation, so
2078 * cycle through all known transparent accelerators */
2079#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2080#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002081 status = mbedtls_test_transparent_mac_sign_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002082 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002083 attributes,
2084 key_buffer, key_buffer_size,
2085 alg );
2086 /* Declared with fallback == true */
2087 if( status == PSA_SUCCESS )
2088 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
2089
2090 if( status != PSA_ERROR_NOT_SUPPORTED )
2091 return( status );
2092#endif /* PSA_CRYPTO_DRIVER_TEST */
2093#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2094#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2095 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002096 status = mbedtls_psa_mac_sign_setup( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002097 attributes,
2098 key_buffer, key_buffer_size,
2099 alg );
2100 if( status == PSA_SUCCESS )
2101 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
2102
2103 if( status != PSA_ERROR_NOT_SUPPORTED )
2104 return( status );
2105#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2106 return( PSA_ERROR_NOT_SUPPORTED );
2107
2108 /* Add cases for opaque driver here */
2109#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2110#if defined(PSA_CRYPTO_DRIVER_TEST)
2111 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002112 status = mbedtls_test_opaque_mac_sign_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002113 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002114 attributes,
2115 key_buffer, key_buffer_size,
2116 alg );
2117
2118 if( status == PSA_SUCCESS )
2119 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
2120
2121 return( status );
2122#endif /* PSA_CRYPTO_DRIVER_TEST */
2123#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2124 default:
2125 /* Key is declared with a lifetime not known to us */
2126 (void) status;
Ronald Cron7a55deb2021-04-28 14:29:00 +02002127 (void) operation;
Steven Cooremand13a70f2021-03-19 15:24:23 +01002128 (void) key_buffer;
2129 (void) key_buffer_size;
2130 (void) alg;
2131 return( PSA_ERROR_INVALID_ARGUMENT );
2132 }
2133}
2134
2135psa_status_t psa_driver_wrapper_mac_verify_setup(
2136 psa_mac_operation_t *operation,
2137 const psa_key_attributes_t *attributes,
2138 const uint8_t *key_buffer,
2139 size_t key_buffer_size,
2140 psa_algorithm_t alg )
2141{
2142 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2143 psa_key_location_t location =
2144 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
2145
2146 switch( location )
2147 {
2148 case PSA_KEY_LOCATION_LOCAL_STORAGE:
2149 /* Key is stored in the slot in export representation, so
2150 * cycle through all known transparent accelerators */
2151#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2152#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002153 status = mbedtls_test_transparent_mac_verify_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002154 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002155 attributes,
2156 key_buffer, key_buffer_size,
2157 alg );
2158 /* Declared with fallback == true */
2159 if( status == PSA_SUCCESS )
2160 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
2161
2162 if( status != PSA_ERROR_NOT_SUPPORTED )
2163 return( status );
2164#endif /* PSA_CRYPTO_DRIVER_TEST */
2165#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2166#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2167 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002168 status = mbedtls_psa_mac_verify_setup( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002169 attributes,
2170 key_buffer, key_buffer_size,
2171 alg );
2172 if( status == PSA_SUCCESS )
2173 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
2174
2175 if( status != PSA_ERROR_NOT_SUPPORTED )
2176 return( status );
2177#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2178 return( PSA_ERROR_NOT_SUPPORTED );
2179
2180 /* Add cases for opaque driver here */
2181#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2182#if defined(PSA_CRYPTO_DRIVER_TEST)
2183 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002184 status = mbedtls_test_opaque_mac_verify_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002185 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002186 attributes,
2187 key_buffer, key_buffer_size,
2188 alg );
2189
2190 if( status == PSA_SUCCESS )
2191 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
2192
2193 return( status );
2194#endif /* PSA_CRYPTO_DRIVER_TEST */
2195#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2196 default:
2197 /* Key is declared with a lifetime not known to us */
2198 (void) status;
Ronald Cron7a55deb2021-04-28 14:29:00 +02002199 (void) operation;
Steven Cooremand13a70f2021-03-19 15:24:23 +01002200 (void) key_buffer;
2201 (void) key_buffer_size;
2202 (void) alg;
2203 return( PSA_ERROR_INVALID_ARGUMENT );
2204 }
2205}
2206
2207psa_status_t psa_driver_wrapper_mac_update(
2208 psa_mac_operation_t *operation,
2209 const uint8_t *input,
2210 size_t input_length )
2211{
2212 switch( operation->id )
2213 {
2214#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2215 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002216 return( mbedtls_psa_mac_update( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002217 input, input_length ) );
2218#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2219
2220#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2221#if defined(PSA_CRYPTO_DRIVER_TEST)
2222 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002223 return( mbedtls_test_transparent_mac_update(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002224 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002225 input, input_length ) );
2226
2227 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002228 return( mbedtls_test_opaque_mac_update(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002229 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002230 input, input_length ) );
2231#endif /* PSA_CRYPTO_DRIVER_TEST */
2232#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2233 default:
2234 (void) input;
2235 (void) input_length;
2236 return( PSA_ERROR_INVALID_ARGUMENT );
2237 }
2238}
2239
2240psa_status_t psa_driver_wrapper_mac_sign_finish(
2241 psa_mac_operation_t *operation,
2242 uint8_t *mac,
2243 size_t mac_size,
2244 size_t *mac_length )
2245{
2246 switch( operation->id )
2247 {
2248#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2249 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002250 return( mbedtls_psa_mac_sign_finish( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002251 mac, mac_size, mac_length ) );
2252#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2253
2254#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2255#if defined(PSA_CRYPTO_DRIVER_TEST)
2256 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002257 return( mbedtls_test_transparent_mac_sign_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002258 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002259 mac, mac_size, mac_length ) );
2260
2261 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002262 return( mbedtls_test_opaque_mac_sign_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002263 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002264 mac, mac_size, mac_length ) );
2265#endif /* PSA_CRYPTO_DRIVER_TEST */
2266#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2267 default:
2268 (void) mac;
2269 (void) mac_size;
2270 (void) mac_length;
2271 return( PSA_ERROR_INVALID_ARGUMENT );
2272 }
2273}
2274
2275psa_status_t psa_driver_wrapper_mac_verify_finish(
2276 psa_mac_operation_t *operation,
2277 const uint8_t *mac,
2278 size_t mac_length )
2279{
2280 switch( operation->id )
2281 {
2282#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2283 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002284 return( mbedtls_psa_mac_verify_finish( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002285 mac, mac_length ) );
2286#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2287
2288#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2289#if defined(PSA_CRYPTO_DRIVER_TEST)
2290 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002291 return( mbedtls_test_transparent_mac_verify_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002292 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002293 mac, mac_length ) );
2294
2295 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002296 return( mbedtls_test_opaque_mac_verify_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002297 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002298 mac, mac_length ) );
2299#endif /* PSA_CRYPTO_DRIVER_TEST */
2300#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2301 default:
2302 (void) mac;
2303 (void) mac_length;
2304 return( PSA_ERROR_INVALID_ARGUMENT );
2305 }
2306}
2307
2308psa_status_t psa_driver_wrapper_mac_abort(
2309 psa_mac_operation_t *operation )
2310{
Steven Cooremand13a70f2021-03-19 15:24:23 +01002311 switch( operation->id )
2312 {
2313#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2314 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooremane6804192021-03-19 18:28:56 +01002315 return( mbedtls_psa_mac_abort( &operation->ctx.mbedtls_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002316#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2317
2318#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2319#if defined(PSA_CRYPTO_DRIVER_TEST)
2320 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002321 return( mbedtls_test_transparent_mac_abort(
Steven Cooremane6804192021-03-19 18:28:56 +01002322 &operation->ctx.transparent_test_driver_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002323 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002324 return( mbedtls_test_opaque_mac_abort(
Steven Cooremane6804192021-03-19 18:28:56 +01002325 &operation->ctx.opaque_test_driver_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002326#endif /* PSA_CRYPTO_DRIVER_TEST */
2327#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2328 default:
Steven Cooremane6804192021-03-19 18:28:56 +01002329 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002330 }
Steven Cooremand13a70f2021-03-19 15:24:23 +01002331}
Gilles Peskine1905a242021-04-24 13:19:45 +02002332
2333#endif /* MBEDTLS_PSA_CRYPTO_C */