blob: cc826df1ec44b8e07b954d0ebc6ace64d2d7087e [file] [log] [blame]
Steven Cooremancd84cb42020-07-16 20:28:36 +02001/*
2 * Functions to delegate cryptographic operations to an available
3 * and appropriate accelerator.
Steven Cooreman56250fd2020-09-04 13:07:15 +02004 * Warning: This file will be auto-generated in the future.
Steven Cooremancd84cb42020-07-16 20:28:36 +02005 */
Steven Cooreman2c7b2f82020-09-02 13:43:46 +02006/* Copyright The Mbed TLS Contributors
Steven Cooremancd84cb42020-07-16 20:28:36 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Steven Cooremancd84cb42020-07-16 20:28:36 +020020 */
21
Ronald Cronde822812021-03-17 16:08:20 +010022#include "psa_crypto_aead.h"
Ronald Cron0b805592020-12-14 18:08:20 +010023#include "psa_crypto_cipher.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020024#include "psa_crypto_core.h"
25#include "psa_crypto_driver_wrappers.h"
Steven Cooreman830aff22021-03-09 09:50:44 +010026#include "psa_crypto_hash.h"
Steven Cooreman896d51e2021-03-19 15:24:23 +010027#include "psa_crypto_mac.h"
Steven Cooreman830aff22021-03-09 09:50:44 +010028
Steven Cooreman2a1664c2020-07-20 15:33:08 +020029#include "mbedtls/platform.h"
30
Gilles Peskinead0e0122021-04-24 13:19:45 +020031#if defined(MBEDTLS_PSA_CRYPTO_C)
32
Steven Cooreman2a1664c2020-07-20 15:33:08 +020033#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
Steven Cooremancd84cb42020-07-16 20:28:36 +020034
35/* Include test driver definition when running tests */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020036#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman56250fd2020-09-04 13:07:15 +020037#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020038#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020039#endif
40#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020041#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020042#endif
Ronald Cron05ee58d2021-04-30 17:00:34 +020043#include "test/drivers/test_driver.h"
Steven Cooremanf1720ea2020-07-24 18:41:58 +020044#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +020045
Steven Cooreman56250fd2020-09-04 13:07:15 +020046/* Repeat above block for each JSON-declared driver during autogeneration */
Steven Cooremanaa87fd02021-03-15 18:54:03 +010047#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Steven Cooreman56250fd2020-09-04 13:07:15 +020048
Steven Cooreman1e582352021-02-18 17:24:37 +010049/* Auto-generated values depending on which drivers are registered.
50 * ID 0 is reserved for unallocated operations.
51 * ID 1 is reserved for the Mbed TLS software driver. */
Steven Cooreman830aff22021-03-09 09:50:44 +010052#define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
53
Steven Cooreman37941cb2020-07-28 18:49:51 +020054#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman1e582352021-02-18 17:24:37 +010055#define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2)
56#define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3)
Steven Cooreman37941cb2020-07-28 18:49:51 +020057#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +020058
59/* Support the 'old' SE interface when asked to */
Steven Cooreman7a250572020-07-17 16:43:05 +020060#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman56250fd2020-09-04 13:07:15 +020061/* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
62 * SE driver is present, to avoid unused argument errors at compile time. */
63#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020064#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020065#endif
Steven Cooreman7a250572020-07-17 16:43:05 +020066#include "psa_crypto_se.h"
67#endif
68
Ronald Cron088d5d02021-04-10 16:57:30 +020069psa_status_t psa_driver_wrapper_init( void )
70{
71 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
72
73#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
74 status = psa_init_all_se_drivers( );
75 if( status != PSA_SUCCESS )
76 return( status );
77#endif
78
79#if defined(PSA_CRYPTO_DRIVER_TEST)
80 status = mbedtls_test_transparent_init( );
81 if( status != PSA_SUCCESS )
82 return( status );
83
84 status = mbedtls_test_opaque_init( );
85 if( status != PSA_SUCCESS )
86 return( status );
87#endif
88
89 (void) status;
90 return( PSA_SUCCESS );
91}
92
93void psa_driver_wrapper_free( void )
94{
95#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
96 /* Unregister all secure element drivers, so that we restart from
97 * a pristine state. */
98 psa_unregister_all_se_drivers( );
99#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
100
101#if defined(PSA_CRYPTO_DRIVER_TEST)
102 mbedtls_test_transparent_free( );
103 mbedtls_test_opaque_free( );
104#endif
105}
106
Steven Cooremancd84cb42020-07-16 20:28:36 +0200107/* Start delegation functions */
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200108psa_status_t psa_driver_wrapper_sign_message(
109 const psa_key_attributes_t *attributes,
110 const uint8_t *key_buffer,
111 size_t key_buffer_size,
112 psa_algorithm_t alg,
113 const uint8_t *input,
114 size_t input_length,
115 uint8_t *signature,
116 size_t signature_size,
117 size_t *signature_length )
118{
119 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200120 psa_key_location_t location =
121 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
122
123 switch( location )
124 {
125 case PSA_KEY_LOCATION_LOCAL_STORAGE:
126 /* Key is stored in the slot in export representation, so
127 * cycle through all known transparent accelerators */
128#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
129#if defined(PSA_CRYPTO_DRIVER_TEST)
130 status = mbedtls_test_transparent_signature_sign_message(
131 attributes,
132 key_buffer,
133 key_buffer_size,
134 alg,
135 input,
136 input_length,
137 signature,
138 signature_size,
139 signature_length );
140 /* Declared with fallback == true */
141 if( status != PSA_ERROR_NOT_SUPPORTED )
142 return( status );
143#endif /* PSA_CRYPTO_DRIVER_TEST */
144#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200145 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200146
147 /* Add cases for opaque driver here */
148#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
149#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arm77588fb2021-04-27 18:52:51 +0200150 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200151 status = mbedtls_test_opaque_signature_sign_message(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200152 attributes,
153 key_buffer,
154 key_buffer_size,
155 alg,
156 input,
157 input_length,
158 signature,
159 signature_size,
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200160 signature_length );
161 if( status != PSA_ERROR_NOT_SUPPORTED )
162 return( status );
163 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200164#endif /* PSA_CRYPTO_DRIVER_TEST */
165#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
166 default:
167 /* Key is declared with a lifetime not known to us */
168 (void)status;
gabor-mezei-armf25c9762021-05-12 11:12:25 +0200169 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200170 }
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200171
172 return( psa_sign_message_builtin( attributes,
173 key_buffer,
174 key_buffer_size,
175 alg,
176 input,
177 input_length,
178 signature,
179 signature_size,
180 signature_length ) );
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200181}
182
183psa_status_t psa_driver_wrapper_verify_message(
184 const psa_key_attributes_t *attributes,
185 const uint8_t *key_buffer,
186 size_t key_buffer_size,
187 psa_algorithm_t alg,
188 const uint8_t *input,
189 size_t input_length,
190 const uint8_t *signature,
191 size_t signature_length )
192{
193 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200194 psa_key_location_t location =
195 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
196
197 switch( location )
198 {
199 case PSA_KEY_LOCATION_LOCAL_STORAGE:
200 /* Key is stored in the slot in export representation, so
201 * cycle through all known transparent accelerators */
202#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
203#if defined(PSA_CRYPTO_DRIVER_TEST)
204 status = mbedtls_test_transparent_signature_verify_message(
205 attributes,
206 key_buffer,
207 key_buffer_size,
208 alg,
209 input,
210 input_length,
211 signature,
212 signature_length );
213 /* Declared with fallback == true */
214 if( status != PSA_ERROR_NOT_SUPPORTED )
215 return( status );
216#endif /* PSA_CRYPTO_DRIVER_TEST */
217#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200218 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200219
220 /* Add cases for opaque driver here */
221#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
222#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arm77588fb2021-04-27 18:52:51 +0200223 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200224 return( mbedtls_test_opaque_signature_verify_message(
225 attributes,
226 key_buffer,
227 key_buffer_size,
228 alg,
229 input,
230 input_length,
231 signature,
232 signature_length ) );
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200233 if( status != PSA_ERROR_NOT_SUPPORTED )
234 return( status );
235 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200236#endif /* PSA_CRYPTO_DRIVER_TEST */
237#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
238 default:
239 /* Key is declared with a lifetime not known to us */
240 (void)status;
gabor-mezei-armf25c9762021-05-12 11:12:25 +0200241 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200242 }
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200243
244 return( psa_verify_message_builtin( attributes,
245 key_buffer,
246 key_buffer_size,
247 alg,
248 input,
249 input_length,
250 signature,
251 signature_length ) );
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200252}
253
Ronald Cron9f17aa42020-12-08 17:07:25 +0100254psa_status_t psa_driver_wrapper_sign_hash(
255 const psa_key_attributes_t *attributes,
256 const uint8_t *key_buffer, size_t key_buffer_size,
257 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
258 uint8_t *signature, size_t signature_size, size_t *signature_length )
Steven Cooremancd84cb42020-07-16 20:28:36 +0200259{
Steven Cooreman7a250572020-07-17 16:43:05 +0200260 /* Try dynamically-registered SE interface first */
261#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
262 const psa_drv_se_t *drv;
263 psa_drv_se_context_t *drv_context;
264
Ronald Cron9f17aa42020-12-08 17:07:25 +0100265 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman7a250572020-07-17 16:43:05 +0200266 {
267 if( drv->asymmetric == NULL ||
268 drv->asymmetric->p_sign == NULL )
269 {
270 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200271 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman7a250572020-07-17 16:43:05 +0200272 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100273 return( drv->asymmetric->p_sign(
274 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
275 alg, hash, hash_length,
276 signature, signature_size, signature_length ) );
Steven Cooreman7a250572020-07-17 16:43:05 +0200277 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200278#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman7a250572020-07-17 16:43:05 +0200279
Ronald Cronfce9df22020-12-08 18:06:03 +0100280 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100281 psa_key_location_t location =
282 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200283
284 switch( location )
285 {
286 case PSA_KEY_LOCATION_LOCAL_STORAGE:
287 /* Key is stored in the slot in export representation, so
288 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100289#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200290#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200291 status = mbedtls_test_transparent_signature_sign_hash( attributes,
Ronald Cron9f17aa42020-12-08 17:07:25 +0100292 key_buffer,
293 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200294 alg,
295 hash,
296 hash_length,
297 signature,
298 signature_size,
299 signature_length );
300 /* Declared with fallback == true */
301 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200302 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200303#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100304#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200305 /* Fell through, meaning no accelerator supports this operation */
gabor-mezei-armf3c5c862021-05-05 14:18:36 +0200306 return( psa_sign_hash_builtin( attributes,
307 key_buffer,
308 key_buffer_size,
309 alg,
310 hash,
311 hash_length,
312 signature,
313 signature_size,
314 signature_length ) );
Ronald Cronfce9df22020-12-08 18:06:03 +0100315
Steven Cooremancd84cb42020-07-16 20:28:36 +0200316 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100317#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200318#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100319 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200320 return( mbedtls_test_opaque_signature_sign_hash( attributes,
321 key_buffer,
322 key_buffer_size,
323 alg,
324 hash,
325 hash_length,
326 signature,
327 signature_size,
328 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200329#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100330#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200331 default:
332 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100333 (void)status;
334 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200335 }
Steven Cooremancd84cb42020-07-16 20:28:36 +0200336}
337
Ronald Cron9f17aa42020-12-08 17:07:25 +0100338psa_status_t psa_driver_wrapper_verify_hash(
339 const psa_key_attributes_t *attributes,
340 const uint8_t *key_buffer, size_t key_buffer_size,
341 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
342 const uint8_t *signature, size_t signature_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200343{
Steven Cooreman55ae2172020-07-17 19:46:15 +0200344 /* Try dynamically-registered SE interface first */
345#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
346 const psa_drv_se_t *drv;
347 psa_drv_se_context_t *drv_context;
348
Ronald Cron9f17aa42020-12-08 17:07:25 +0100349 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200350 {
351 if( drv->asymmetric == NULL ||
352 drv->asymmetric->p_verify == NULL )
353 {
354 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200355 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200356 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100357 return( drv->asymmetric->p_verify(
358 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
359 alg, hash, hash_length,
360 signature, signature_length ) );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200361 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200362#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200363
Ronald Cronfce9df22020-12-08 18:06:03 +0100364 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100365 psa_key_location_t location =
366 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200367
368 switch( location )
369 {
370 case PSA_KEY_LOCATION_LOCAL_STORAGE:
371 /* Key is stored in the slot in export representation, so
372 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100373#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200374#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200375 status = mbedtls_test_transparent_signature_verify_hash(
376 attributes,
377 key_buffer,
378 key_buffer_size,
379 alg,
380 hash,
381 hash_length,
382 signature,
383 signature_length );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200384 /* Declared with fallback == true */
385 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200386 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200387#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100388#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
389
gabor-mezei-armf3c5c862021-05-05 14:18:36 +0200390 return( psa_verify_hash_builtin( attributes,
391 key_buffer,
392 key_buffer_size,
393 alg,
394 hash,
395 hash_length,
396 signature,
397 signature_length ) );
Ronald Cronfce9df22020-12-08 18:06:03 +0100398
Steven Cooreman55ae2172020-07-17 19:46:15 +0200399 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100400#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200401#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100402 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200403 return( mbedtls_test_opaque_signature_verify_hash( attributes,
404 key_buffer,
405 key_buffer_size,
406 alg,
407 hash,
408 hash_length,
409 signature,
410 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200411#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100412#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200413 default:
414 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100415 (void)status;
416 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200417 }
Steven Cooreman55ae2172020-07-17 19:46:15 +0200418}
419
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200420/** Get the key buffer size required to store the key material of a key
421 * associated with an opaque driver without storage.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200422 *
Ronald Cron31216282020-12-05 18:47:56 +0100423 * \param[in] attributes The key attributes.
424 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200425 *
426 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100427 * The minimum size for a buffer to contain the key material has been
428 * returned successfully.
429 * \retval #PSA_ERROR_INVALID_ARGUMENT
430 * The size in bits of the key is not valid.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200431 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100432 * The type and/or the size in bits of the key or the combination of
433 * the two is not supported.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200434 */
Ronald Cron9df74be2020-12-05 19:15:23 +0100435psa_status_t psa_driver_wrapper_get_key_buffer_size(
Ronald Cron31216282020-12-05 18:47:56 +0100436 const psa_key_attributes_t *attributes,
437 size_t *key_buffer_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200438{
John Durkop2c618352020-09-22 06:54:01 -0700439 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
440 psa_key_type_t key_type = attributes->core.type;
441 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200442
Ronald Cron31216282020-12-05 18:47:56 +0100443 *key_buffer_size = 0;
John Durkop2c618352020-09-22 06:54:01 -0700444 switch( location )
445 {
John Durkop2c618352020-09-22 06:54:01 -0700446#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100447 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100448#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
449 /* Emulate property 'builtin_key_size' */
450 if( psa_key_id_is_builtin(
451 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(
452 psa_get_key_id( attributes ) ) ) )
453 {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100454 *key_buffer_size = sizeof( psa_drv_slot_number_t );
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100455 return( PSA_SUCCESS );
456 }
457#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200458 *key_buffer_size = mbedtls_test_size_function( key_type, key_bits );
Ronald Cronb0737da2021-04-14 10:55:34 +0200459 return( ( *key_buffer_size != 0 ) ?
460 PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
John Durkop2c618352020-09-22 06:54:01 -0700461#endif /* PSA_CRYPTO_DRIVER_TEST */
462
463 default:
Ronald Cron9df74be2020-12-05 19:15:23 +0100464 (void)key_type;
465 (void)key_bits;
Steven Cooreman56250fd2020-09-04 13:07:15 +0200466 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200467 }
468}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200469
Ronald Cron977c2472020-10-13 08:32:21 +0200470psa_status_t psa_driver_wrapper_generate_key(
471 const psa_key_attributes_t *attributes,
472 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200473{
Ronald Cron977c2472020-10-13 08:32:21 +0200474 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
475 psa_key_location_t location =
476 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
477
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200478 /* Try dynamically-registered SE interface first */
479#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
480 const psa_drv_se_t *drv;
481 psa_drv_se_context_t *drv_context;
482
Ronald Cron977c2472020-10-13 08:32:21 +0200483 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200484 {
485 size_t pubkey_length = 0; /* We don't support this feature yet */
486 if( drv->key_management == NULL ||
487 drv->key_management->p_generate == NULL )
488 {
489 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200490 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200491 }
492 return( drv->key_management->p_generate(
Ronald Cron977c2472020-10-13 08:32:21 +0200493 drv_context,
494 *( (psa_key_slot_number_t *)key_buffer ),
Ronald Cronea0f8a62020-11-25 17:52:23 +0100495 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200496 }
497#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
498
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200499 switch( location )
500 {
501 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Ronald Cron977c2472020-10-13 08:32:21 +0200502#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200503 /* Transparent drivers are limited to generating asymmetric keys */
Ronald Cron977c2472020-10-13 08:32:21 +0200504 if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200505 {
Ronald Cron977c2472020-10-13 08:32:21 +0200506 /* Cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200507#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200508 status = mbedtls_test_transparent_generate_key(
Ronald Cron977c2472020-10-13 08:32:21 +0200509 attributes, key_buffer, key_buffer_size,
510 key_buffer_length );
511 /* Declared with fallback == true */
512 if( status != PSA_ERROR_NOT_SUPPORTED )
513 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200514#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200515 }
516#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
517
518 /* Software fallback */
519 status = psa_generate_key_internal(
520 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200521 break;
Ronald Cron977c2472020-10-13 08:32:21 +0200522
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200523 /* Add cases for opaque driver here */
Ronald Cron977c2472020-10-13 08:32:21 +0200524#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200525#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100526 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200527 status = mbedtls_test_opaque_generate_key(
Ronald Cron977c2472020-10-13 08:32:21 +0200528 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200529 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200530#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200531#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
532
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200533 default:
534 /* Key is declared with a lifetime not known to us */
535 status = PSA_ERROR_INVALID_ARGUMENT;
536 break;
537 }
538
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200539 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200540}
541
Ronald Cron83282872020-11-22 14:02:39 +0100542psa_status_t psa_driver_wrapper_import_key(
543 const psa_key_attributes_t *attributes,
544 const uint8_t *data,
545 size_t data_length,
546 uint8_t *key_buffer,
547 size_t key_buffer_size,
548 size_t *key_buffer_length,
549 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200550{
Steven Cooreman04524762020-10-13 17:43:44 +0200551 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100552 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
553 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200554
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100555 /* Try dynamically-registered SE interface first */
556#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
557 const psa_drv_se_t *drv;
558 psa_drv_se_context_t *drv_context;
559
560 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
561 {
562 if( drv->key_management == NULL ||
563 drv->key_management->p_import == NULL )
564 return( PSA_ERROR_NOT_SUPPORTED );
565
566 /* The driver should set the number of key bits, however in
567 * case it doesn't, we initialize bits to an invalid value. */
568 *bits = PSA_MAX_KEY_BITS + 1;
569 status = drv->key_management->p_import(
570 drv_context,
571 *( (psa_key_slot_number_t *)key_buffer ),
572 attributes, data, data_length, bits );
573
574 if( status != PSA_SUCCESS )
575 return( status );
576
577 if( (*bits) > PSA_MAX_KEY_BITS )
578 return( PSA_ERROR_NOT_SUPPORTED );
579
580 return( PSA_SUCCESS );
581 }
582#endif /* PSA_CRYPTO_SE_C */
583
Ronald Cronbf33c932020-11-28 18:06:53 +0100584 switch( location )
585 {
586 case PSA_KEY_LOCATION_LOCAL_STORAGE:
587 /* Key is stored in the slot in export representation, so
588 * cycle through all known transparent accelerators */
589#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
590#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200591 status = mbedtls_test_transparent_import_key(
592 attributes,
593 data, data_length,
594 key_buffer, key_buffer_size,
595 key_buffer_length, bits );
Ronald Cronbf33c932020-11-28 18:06:53 +0100596 /* Declared with fallback == true */
597 if( status != PSA_ERROR_NOT_SUPPORTED )
598 return( status );
599#endif /* PSA_CRYPTO_DRIVER_TEST */
600#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
601 /* Fell through, meaning no accelerator supports this operation */
602 return( psa_import_key_into_slot( attributes,
603 data, data_length,
604 key_buffer, key_buffer_size,
605 key_buffer_length, bits ) );
606
607 default:
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100608 /* Importing a key with external storage in not yet supported.
609 * Return in error indicating that the lifetime is not valid. */
Ronald Cronbf33c932020-11-28 18:06:53 +0100610 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100611 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100612 }
613
Steven Cooreman04524762020-10-13 17:43:44 +0200614}
615
Ronald Cron67227982020-11-26 15:16:05 +0100616psa_status_t psa_driver_wrapper_export_key(
617 const psa_key_attributes_t *attributes,
618 const uint8_t *key_buffer, size_t key_buffer_size,
619 uint8_t *data, size_t data_size, size_t *data_length )
620
621{
622 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
623 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
624 psa_get_key_lifetime( attributes ) );
625
Ronald Cron152e3f82020-11-26 16:06:41 +0100626 /* Try dynamically-registered SE interface first */
627#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
628 const psa_drv_se_t *drv;
629 psa_drv_se_context_t *drv_context;
630
631 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
632 {
633 if( ( drv->key_management == NULL ) ||
634 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100635 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100636 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100637 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100638
639 return( drv->key_management->p_export(
640 drv_context,
641 *( (psa_key_slot_number_t *)key_buffer ),
642 data, data_size, data_length ) );
643 }
644#endif /* PSA_CRYPTO_SE_C */
645
Ronald Cron67227982020-11-26 15:16:05 +0100646 switch( location )
647 {
648 case PSA_KEY_LOCATION_LOCAL_STORAGE:
649 return( psa_export_key_internal( attributes,
650 key_buffer,
651 key_buffer_size,
652 data,
653 data_size,
654 data_length ) );
655
656 /* Add cases for opaque driver here */
657#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
658#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100659 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200660 return( mbedtls_test_opaque_export_key( attributes,
661 key_buffer,
662 key_buffer_size,
663 data,
664 data_size,
665 data_length ) );
Ronald Cron67227982020-11-26 15:16:05 +0100666#endif /* PSA_CRYPTO_DRIVER_TEST */
667#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
668 default:
669 /* Key is declared with a lifetime not known to us */
670 return( status );
671 }
672}
673
Ronald Cron84cc9942020-11-25 14:30:05 +0100674psa_status_t psa_driver_wrapper_export_public_key(
675 const psa_key_attributes_t *attributes,
676 const uint8_t *key_buffer, size_t key_buffer_size,
677 uint8_t *data, size_t data_size, size_t *data_length )
678
Steven Cooremanb9b84422020-10-14 14:39:20 +0200679{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200680 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100681 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
682 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200683
Ronald Cron152e3f82020-11-26 16:06:41 +0100684 /* Try dynamically-registered SE interface first */
685#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
686 const psa_drv_se_t *drv;
687 psa_drv_se_context_t *drv_context;
688
689 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
690 {
691 if( ( drv->key_management == NULL ) ||
692 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100693 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100694 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100695 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100696
697 return( drv->key_management->p_export_public(
698 drv_context,
699 *( (psa_key_slot_number_t *)key_buffer ),
700 data, data_size, data_length ) );
701 }
702#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
703
Steven Cooremanb9b84422020-10-14 14:39:20 +0200704 switch( location )
705 {
706 case PSA_KEY_LOCATION_LOCAL_STORAGE:
707 /* Key is stored in the slot in export representation, so
708 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100709#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200710#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200711 status = mbedtls_test_transparent_export_public_key(
712 attributes,
713 key_buffer,
714 key_buffer_size,
715 data,
716 data_size,
717 data_length );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200718 /* Declared with fallback == true */
719 if( status != PSA_ERROR_NOT_SUPPORTED )
720 return( status );
721#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100722#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200723 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100724 return( psa_export_public_key_internal( attributes,
725 key_buffer,
726 key_buffer_size,
727 data,
728 data_size,
729 data_length ) );
730
Steven Cooremanb9b84422020-10-14 14:39:20 +0200731 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100732#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200733#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100734 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200735 return( mbedtls_test_opaque_export_public_key( attributes,
736 key_buffer,
737 key_buffer_size,
738 data,
739 data_size,
740 data_length ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200741#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100742#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200743 default:
744 /* Key is declared with a lifetime not known to us */
745 return( status );
746 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200747}
748
Steven Cooreman6801f082021-02-19 17:21:22 +0100749psa_status_t psa_driver_wrapper_get_builtin_key(
750 psa_drv_slot_number_t slot_number,
751 psa_key_attributes_t *attributes,
752 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
753{
754 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
755 switch( location )
756 {
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100757#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100758 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200759 return( mbedtls_test_opaque_get_builtin_key(
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100760 slot_number,
761 attributes,
762 key_buffer, key_buffer_size, key_buffer_length ) );
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100763#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman6801f082021-02-19 17:21:22 +0100764 default:
765 (void) slot_number;
766 (void) key_buffer;
767 (void) key_buffer_size;
768 (void) key_buffer_length;
769 return( PSA_ERROR_DOES_NOT_EXIST );
770 }
771}
772
Steven Cooreman37941cb2020-07-28 18:49:51 +0200773/*
774 * Cipher functions
775 */
776psa_status_t psa_driver_wrapper_cipher_encrypt(
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100777 const psa_key_attributes_t *attributes,
778 const uint8_t *key_buffer,
779 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200780 psa_algorithm_t alg,
781 const uint8_t *input,
782 size_t input_length,
783 uint8_t *output,
784 size_t output_size,
785 size_t *output_length )
786{
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100787 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
788 psa_key_location_t location =
789 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200790
791 switch( location )
792 {
793 case PSA_KEY_LOCATION_LOCAL_STORAGE:
794 /* Key is stored in the slot in export representation, so
795 * cycle through all known transparent accelerators */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100796#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200797#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100798 status = mbedtls_test_transparent_cipher_encrypt( attributes,
799 key_buffer,
800 key_buffer_size,
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200801 alg,
802 input,
803 input_length,
804 output,
805 output_size,
806 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200807 /* Declared with fallback == true */
808 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200809 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200810#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100811#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
812
813#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
814 return( mbedtls_psa_cipher_encrypt( attributes,
815 key_buffer,
816 key_buffer_size,
817 alg,
818 input,
819 input_length,
820 output,
821 output_size,
822 output_length ) );
gabor-mezei-armd52c8dc2021-06-25 15:43:32 +0200823#else
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200824 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-armd52c8dc2021-06-25 15:43:32 +0200825#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100826
Steven Cooreman37941cb2020-07-28 18:49:51 +0200827 /* Add cases for opaque driver here */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100828#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200829#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100830 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100831 return( mbedtls_test_opaque_cipher_encrypt( attributes,
832 key_buffer,
833 key_buffer_size,
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200834 alg,
835 input,
836 input_length,
837 output,
838 output_size,
839 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200840#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100841#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
842
Steven Cooreman37941cb2020-07-28 18:49:51 +0200843 default:
844 /* Key is declared with a lifetime not known to us */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100845 (void)status;
846 (void)key_buffer;
847 (void)key_buffer_size;
848 (void)alg;
849 (void)input;
850 (void)input_length;
851 (void)output;
852 (void)output_size;
853 (void)output_length;
854 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200855 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200856}
857
858psa_status_t psa_driver_wrapper_cipher_decrypt(
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100859 const psa_key_attributes_t *attributes,
860 const uint8_t *key_buffer,
861 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200862 psa_algorithm_t alg,
863 const uint8_t *input,
864 size_t input_length,
865 uint8_t *output,
866 size_t output_size,
867 size_t *output_length )
868{
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100869 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
870 psa_key_location_t location =
871 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200872
873 switch( location )
874 {
875 case PSA_KEY_LOCATION_LOCAL_STORAGE:
876 /* Key is stored in the slot in export representation, so
877 * cycle through all known transparent accelerators */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100878#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200879#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100880 status = mbedtls_test_transparent_cipher_decrypt( attributes,
881 key_buffer,
882 key_buffer_size,
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200883 alg,
884 input,
885 input_length,
886 output,
887 output_size,
888 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200889 /* Declared with fallback == true */
890 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200891 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200892#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100893#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
894
895#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
896 return( mbedtls_psa_cipher_decrypt( attributes,
897 key_buffer,
898 key_buffer_size,
899 alg,
900 input,
901 input_length,
902 output,
903 output_size,
904 output_length ) );
gabor-mezei-armd52c8dc2021-06-25 15:43:32 +0200905#else
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200906 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-armd52c8dc2021-06-25 15:43:32 +0200907#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100908
Steven Cooreman37941cb2020-07-28 18:49:51 +0200909 /* Add cases for opaque driver here */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100910#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200911#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100912 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100913 return( mbedtls_test_opaque_cipher_decrypt( attributes,
914 key_buffer,
915 key_buffer_size,
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200916 alg,
917 input,
918 input_length,
919 output,
920 output_size,
921 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200922#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100923#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
924
Steven Cooreman37941cb2020-07-28 18:49:51 +0200925 default:
926 /* Key is declared with a lifetime not known to us */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100927 (void)status;
928 (void)key_buffer;
929 (void)key_buffer_size;
930 (void)alg;
931 (void)input;
932 (void)input_length;
933 (void)output;
934 (void)output_size;
935 (void)output_length;
936 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200937 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200938}
939
940psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100941 psa_cipher_operation_t *operation,
942 const psa_key_attributes_t *attributes,
943 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200944 psa_algorithm_t alg )
945{
Ronald Cron0b805592020-12-14 18:08:20 +0100946 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona4af55f2020-12-14 14:36:06 +0100947 psa_key_location_t location =
948 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200949
Steven Cooreman37941cb2020-07-28 18:49:51 +0200950 switch( location )
951 {
952 case PSA_KEY_LOCATION_LOCAL_STORAGE:
953 /* Key is stored in the slot in export representation, so
954 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100955#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200956#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200957 status = mbedtls_test_transparent_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100958 &operation->ctx.transparent_test_driver_ctx,
959 attributes,
960 key_buffer,
961 key_buffer_size,
962 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200963 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200964 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100965 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +0200966
Ronald Cron0b805592020-12-14 18:08:20 +0100967 if( status != PSA_ERROR_NOT_SUPPORTED )
968 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200969#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100970#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100971#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200972 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +0100973 status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100974 attributes,
975 key_buffer,
976 key_buffer_size,
977 alg );
978 if( status == PSA_SUCCESS )
Ronald Cron6e412a72021-03-10 09:58:47 +0100979 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Ronald Cron49fafa92021-03-10 08:34:23 +0100980
Ronald Cron7b4154d2021-03-19 14:49:41 +0100981 if( status != PSA_ERROR_NOT_SUPPORTED )
982 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100983#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
984 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron0b805592020-12-14 18:08:20 +0100985
Steven Cooreman37941cb2020-07-28 18:49:51 +0200986 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100987#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200988#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100989 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200990 status = mbedtls_test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100991 &operation->ctx.opaque_test_driver_ctx,
992 attributes,
993 key_buffer, key_buffer_size,
994 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200995
Steven Cooreman37941cb2020-07-28 18:49:51 +0200996 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100997 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200998
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200999 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001000#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001001#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +02001002 default:
1003 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +01001004 (void)status;
Ronald Cron485559e2021-04-28 14:29:00 +02001005 (void)operation;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001006 (void)key_buffer;
1007 (void)key_buffer_size;
1008 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +02001009 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001010 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001011}
1012
1013psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +01001014 psa_cipher_operation_t *operation,
1015 const psa_key_attributes_t *attributes,
1016 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001017 psa_algorithm_t alg )
1018{
Steven Cooreman37941cb2020-07-28 18:49:51 +02001019 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crona4af55f2020-12-14 14:36:06 +01001020 psa_key_location_t location =
1021 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001022
Steven Cooreman37941cb2020-07-28 18:49:51 +02001023 switch( location )
1024 {
1025 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1026 /* Key is stored in the slot in export representation, so
1027 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +01001028#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001029#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001030 status = mbedtls_test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001031 &operation->ctx.transparent_test_driver_ctx,
1032 attributes,
1033 key_buffer,
1034 key_buffer_size,
1035 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001036 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +02001037 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001038 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +02001039
Ronald Cron0b805592020-12-14 18:08:20 +01001040 if( status != PSA_ERROR_NOT_SUPPORTED )
1041 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001042#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001043#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001044#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001045 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +01001046 status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001047 attributes,
1048 key_buffer,
1049 key_buffer_size,
1050 alg );
1051 if( status == PSA_SUCCESS )
1052 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1053
1054 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001055#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1056 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron49fafa92021-03-10 08:34:23 +01001057
Steven Cooreman37941cb2020-07-28 18:49:51 +02001058 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +01001059#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001060#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +01001061 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001062 status = mbedtls_test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001063 &operation->ctx.opaque_test_driver_ctx,
1064 attributes,
1065 key_buffer, key_buffer_size,
1066 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001067
Steven Cooreman37941cb2020-07-28 18:49:51 +02001068 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001069 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001070
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001071 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 */
Steven Cooreman37941cb2020-07-28 18:49:51 +02001074 default:
1075 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +01001076 (void)status;
Ronald Cron485559e2021-04-28 14:29:00 +02001077 (void)operation;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001078 (void)key_buffer;
1079 (void)key_buffer_size;
1080 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +02001081 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001082 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001083}
1084
Steven Cooreman37941cb2020-07-28 18:49:51 +02001085psa_status_t psa_driver_wrapper_cipher_set_iv(
Ronald Cron6056fe82020-12-15 13:58:07 +01001086 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001087 const uint8_t *iv,
1088 size_t iv_length )
1089{
Ronald Cron49fafa92021-03-10 08:34:23 +01001090 switch( operation->id )
1091 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001092#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001093 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001094 return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001095 iv,
1096 iv_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001097#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001098
1099#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001100#if defined(PSA_CRYPTO_DRIVER_TEST)
1101 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001102 return( mbedtls_test_transparent_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001103 &operation->ctx.transparent_test_driver_ctx,
1104 iv, iv_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001105
Steven Cooreman37941cb2020-07-28 18:49:51 +02001106 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001107 return( mbedtls_test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001108 &operation->ctx.opaque_test_driver_ctx,
1109 iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001110#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001111#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001112 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001113
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001114 (void)iv;
1115 (void)iv_length;
1116
Ronald Crondd24c9b2020-12-15 14:10:01 +01001117 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001118}
1119
1120psa_status_t psa_driver_wrapper_cipher_update(
Ronald Cron6056fe82020-12-15 13:58:07 +01001121 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001122 const uint8_t *input,
1123 size_t input_length,
1124 uint8_t *output,
1125 size_t output_size,
1126 size_t *output_length )
1127{
Ronald Cron49fafa92021-03-10 08:34:23 +01001128 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001129 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001130#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001131 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001132 return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001133 input,
1134 input_length,
1135 output,
1136 output_size,
1137 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001138#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1139
Ronald Cron49fafa92021-03-10 08:34:23 +01001140#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001141#if defined(PSA_CRYPTO_DRIVER_TEST)
1142 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001143 return( mbedtls_test_transparent_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001144 &operation->ctx.transparent_test_driver_ctx,
1145 input, input_length,
1146 output, output_size, output_length ) );
1147
Steven Cooreman37941cb2020-07-28 18:49:51 +02001148 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001149 return( mbedtls_test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001150 &operation->ctx.opaque_test_driver_ctx,
1151 input, input_length,
1152 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001153#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001154#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001155 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001156
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001157 (void)input;
1158 (void)input_length;
1159 (void)output;
1160 (void)output_size;
1161 (void)output_length;
1162
Ronald Crondd24c9b2020-12-15 14:10:01 +01001163 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001164}
1165
1166psa_status_t psa_driver_wrapper_cipher_finish(
Ronald Cron6056fe82020-12-15 13:58:07 +01001167 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001168 uint8_t *output,
1169 size_t output_size,
1170 size_t *output_length )
1171{
Ronald Cron49fafa92021-03-10 08:34:23 +01001172 switch( operation->id )
1173 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001174#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001175 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001176 return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001177 output,
1178 output_size,
1179 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001180#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001181
1182#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001183#if defined(PSA_CRYPTO_DRIVER_TEST)
1184 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001185 return( mbedtls_test_transparent_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001186 &operation->ctx.transparent_test_driver_ctx,
1187 output, output_size, output_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001188
Steven Cooreman37941cb2020-07-28 18:49:51 +02001189 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001190 return( mbedtls_test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001191 &operation->ctx.opaque_test_driver_ctx,
1192 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001193#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001194#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001195 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001196
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001197 (void)output;
1198 (void)output_size;
1199 (void)output_length;
1200
Ronald Crondd24c9b2020-12-15 14:10:01 +01001201 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001202}
1203
1204psa_status_t psa_driver_wrapper_cipher_abort(
Ronald Cron6056fe82020-12-15 13:58:07 +01001205 psa_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001206{
Ronald Crondd24c9b2020-12-15 14:10:01 +01001207 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001208
Ronald Cron49fafa92021-03-10 08:34:23 +01001209 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001210 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001211#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001212 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001213 return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001214#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +01001215
1216#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001217#if defined(PSA_CRYPTO_DRIVER_TEST)
1218 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001219 status = mbedtls_test_transparent_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001220 &operation->ctx.transparent_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001221 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001222 &operation->ctx.transparent_test_driver_ctx,
1223 sizeof( operation->ctx.transparent_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001224 return( status );
Ronald Cron49fafa92021-03-10 08:34:23 +01001225
Steven Cooreman37941cb2020-07-28 18:49:51 +02001226 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001227 status = mbedtls_test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001228 &operation->ctx.opaque_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001229 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001230 &operation->ctx.opaque_test_driver_ctx,
1231 sizeof( operation->ctx.opaque_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001232 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001233#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001234#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001235 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001236
Ronald Cron49fafa92021-03-10 08:34:23 +01001237 (void)status;
Ronald Crondd24c9b2020-12-15 14:10:01 +01001238 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001239}
1240
Steven Cooreman1e582352021-02-18 17:24:37 +01001241/*
1242 * Hashing functions
1243 */
1244psa_status_t psa_driver_wrapper_hash_compute(
1245 psa_algorithm_t alg,
1246 const uint8_t *input,
1247 size_t input_length,
1248 uint8_t *hash,
1249 size_t hash_size,
1250 size_t *hash_length)
1251{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001252 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001253
1254 /* Try accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001255#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001256 status = mbedtls_test_transparent_hash_compute(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001257 alg, input, input_length, hash, hash_size, hash_length );
Steven Cooremanf7638102021-03-04 15:14:36 +01001258 if( status != PSA_ERROR_NOT_SUPPORTED )
1259 return( status );
1260#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001261
1262 /* If software fallback is compiled in, try fallback */
1263#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1264 status = mbedtls_psa_hash_compute( alg, input, input_length,
1265 hash, hash_size, hash_length );
1266 if( status != PSA_ERROR_NOT_SUPPORTED )
1267 return( status );
1268#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001269 (void) status;
1270 (void) alg;
1271 (void) input;
1272 (void) input_length;
1273 (void) hash;
1274 (void) hash_size;
1275 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001276
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001277 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman1e582352021-02-18 17:24:37 +01001278}
1279
1280psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001281 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001282 psa_algorithm_t alg )
1283{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001284 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001285
Steven Cooreman1e582352021-02-18 17:24:37 +01001286 /* Try setup on accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001287#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001288 status = mbedtls_test_transparent_hash_setup(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001289 &operation->ctx.test_driver_ctx, alg );
Steven Cooremanf7638102021-03-04 15:14:36 +01001290 if( status == PSA_SUCCESS )
1291 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1292
1293 if( status != PSA_ERROR_NOT_SUPPORTED )
1294 return( status );
1295#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001296
1297 /* If software fallback is compiled in, try fallback */
1298#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001299 status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
Steven Cooreman1e582352021-02-18 17:24:37 +01001300 if( status == PSA_SUCCESS )
Steven Cooreman1e582352021-02-18 17:24:37 +01001301 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Steven Cooreman1e582352021-02-18 17:24:37 +01001302
1303 if( status != PSA_ERROR_NOT_SUPPORTED )
1304 return( status );
1305#endif
1306 /* Nothing left to try if we fall through here */
1307 (void) status;
1308 (void) operation;
1309 (void) alg;
1310 return( PSA_ERROR_NOT_SUPPORTED );
1311}
1312
1313psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001314 const psa_hash_operation_t *source_operation,
1315 psa_hash_operation_t *target_operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001316{
Steven Cooreman1e582352021-02-18 17:24:37 +01001317 switch( source_operation->id )
1318 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001319#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1320 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1321 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1322 return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
1323 &target_operation->ctx.mbedtls_ctx ) );
1324#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001325#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001326 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1327 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001328 return( mbedtls_test_transparent_hash_clone(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001329 &source_operation->ctx.test_driver_ctx,
1330 &target_operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001331#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001332 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001333 (void) target_operation;
1334 return( PSA_ERROR_BAD_STATE );
1335 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001336}
1337
1338psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001339 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001340 const uint8_t *input,
1341 size_t input_length )
1342{
Steven Cooreman1e582352021-02-18 17:24:37 +01001343 switch( operation->id )
1344 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001345#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1346 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1347 return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
1348 input, input_length ) );
1349#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001350#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001351 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001352 return( mbedtls_test_transparent_hash_update(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001353 &operation->ctx.test_driver_ctx,
1354 input, input_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001355#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001356 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001357 (void) input;
1358 (void) input_length;
1359 return( PSA_ERROR_BAD_STATE );
1360 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001361}
1362
1363psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001364 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001365 uint8_t *hash,
1366 size_t hash_size,
1367 size_t *hash_length )
1368{
Steven Cooreman1e582352021-02-18 17:24:37 +01001369 switch( operation->id )
1370 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001371#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1372 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1373 return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
1374 hash, hash_size, hash_length ) );
1375#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001376#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001377 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001378 return( mbedtls_test_transparent_hash_finish(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001379 &operation->ctx.test_driver_ctx,
1380 hash, hash_size, hash_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001381#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001382 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001383 (void) hash;
1384 (void) hash_size;
1385 (void) hash_length;
1386 return( PSA_ERROR_BAD_STATE );
1387 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001388}
1389
1390psa_status_t psa_driver_wrapper_hash_abort(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001391 psa_hash_operation_t *operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001392{
Steven Cooreman1e582352021-02-18 17:24:37 +01001393 switch( operation->id )
1394 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001395#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1396 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1397 return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
1398#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001399#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001400 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001401 return( mbedtls_test_transparent_hash_abort(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001402 &operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001403#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001404 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001405 return( PSA_ERROR_BAD_STATE );
1406 }
1407}
1408
Ronald Cronde822812021-03-17 16:08:20 +01001409psa_status_t psa_driver_wrapper_aead_encrypt(
1410 const psa_key_attributes_t *attributes,
1411 const uint8_t *key_buffer, size_t key_buffer_size,
1412 psa_algorithm_t alg,
1413 const uint8_t *nonce, size_t nonce_length,
1414 const uint8_t *additional_data, size_t additional_data_length,
1415 const uint8_t *plaintext, size_t plaintext_length,
1416 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
1417{
1418 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1419 psa_key_location_t location =
1420 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1421
1422 switch( location )
1423 {
1424 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1425 /* Key is stored in the slot in export representation, so
1426 * cycle through all known transparent accelerators */
1427
1428#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1429#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001430 status = mbedtls_test_transparent_aead_encrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001431 attributes, key_buffer, key_buffer_size,
1432 alg,
1433 nonce, nonce_length,
1434 additional_data, additional_data_length,
1435 plaintext, plaintext_length,
1436 ciphertext, ciphertext_size, ciphertext_length );
1437 /* Declared with fallback == true */
1438 if( status != PSA_ERROR_NOT_SUPPORTED )
1439 return( status );
1440#endif /* PSA_CRYPTO_DRIVER_TEST */
1441#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1442
1443 /* Fell through, meaning no accelerator supports this operation */
1444 return( mbedtls_psa_aead_encrypt(
1445 attributes, key_buffer, key_buffer_size,
1446 alg,
1447 nonce, nonce_length,
1448 additional_data, additional_data_length,
1449 plaintext, plaintext_length,
1450 ciphertext, ciphertext_size, ciphertext_length ) );
1451
1452 /* Add cases for opaque driver here */
1453
1454 default:
1455 /* Key is declared with a lifetime not known to us */
1456 (void)status;
1457 return( PSA_ERROR_INVALID_ARGUMENT );
1458 }
1459}
1460
1461psa_status_t psa_driver_wrapper_aead_decrypt(
1462 const psa_key_attributes_t *attributes,
1463 const uint8_t *key_buffer, size_t key_buffer_size,
1464 psa_algorithm_t alg,
1465 const uint8_t *nonce, size_t nonce_length,
1466 const uint8_t *additional_data, size_t additional_data_length,
1467 const uint8_t *ciphertext, size_t ciphertext_length,
1468 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
1469{
1470 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1471 psa_key_location_t location =
1472 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1473
1474 switch( location )
1475 {
1476 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1477 /* Key is stored in the slot in export representation, so
1478 * cycle through all known transparent accelerators */
1479
1480#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1481#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001482 status = mbedtls_test_transparent_aead_decrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001483 attributes, key_buffer, key_buffer_size,
1484 alg,
1485 nonce, nonce_length,
1486 additional_data, additional_data_length,
1487 ciphertext, ciphertext_length,
1488 plaintext, plaintext_size, plaintext_length );
1489 /* Declared with fallback == true */
1490 if( status != PSA_ERROR_NOT_SUPPORTED )
1491 return( status );
1492#endif /* PSA_CRYPTO_DRIVER_TEST */
1493#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1494
1495 /* Fell through, meaning no accelerator supports this operation */
1496 return( mbedtls_psa_aead_decrypt(
1497 attributes, key_buffer, key_buffer_size,
1498 alg,
1499 nonce, nonce_length,
1500 additional_data, additional_data_length,
1501 ciphertext, ciphertext_length,
1502 plaintext, plaintext_size, plaintext_length ) );
1503
1504 /* Add cases for opaque driver here */
1505
1506 default:
1507 /* Key is declared with a lifetime not known to us */
1508 (void)status;
1509 return( PSA_ERROR_INVALID_ARGUMENT );
1510 }
1511}
Steven Cooreman896d51e2021-03-19 15:24:23 +01001512
1513
1514/*
1515 * MAC functions
1516 */
1517psa_status_t psa_driver_wrapper_mac_compute(
1518 const psa_key_attributes_t *attributes,
1519 const uint8_t *key_buffer,
1520 size_t key_buffer_size,
1521 psa_algorithm_t alg,
1522 const uint8_t *input,
1523 size_t input_length,
1524 uint8_t *mac,
1525 size_t mac_size,
1526 size_t *mac_length )
1527{
1528 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1529 psa_key_location_t location =
1530 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1531
1532 switch( location )
1533 {
1534 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1535 /* Key is stored in the slot in export representation, so
1536 * cycle through all known transparent accelerators */
1537#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1538#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001539 status = mbedtls_test_transparent_mac_compute(
Steven Cooreman896d51e2021-03-19 15:24:23 +01001540 attributes, key_buffer, key_buffer_size, alg,
1541 input, input_length,
1542 mac, mac_size, mac_length );
1543 /* Declared with fallback == true */
1544 if( status != PSA_ERROR_NOT_SUPPORTED )
1545 return( status );
1546#endif /* PSA_CRYPTO_DRIVER_TEST */
1547#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1548#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1549 /* Fell through, meaning no accelerator supports this operation */
1550 status = mbedtls_psa_mac_compute(
1551 attributes, key_buffer, key_buffer_size, alg,
1552 input, input_length,
1553 mac, mac_size, mac_length );
1554 if( status != PSA_ERROR_NOT_SUPPORTED )
1555 return( status );
1556#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1557 return( PSA_ERROR_NOT_SUPPORTED );
1558
1559 /* Add cases for opaque driver here */
1560#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1561#if defined(PSA_CRYPTO_DRIVER_TEST)
1562 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001563 status = mbedtls_test_opaque_mac_compute(
Steven Cooreman896d51e2021-03-19 15:24:23 +01001564 attributes, key_buffer, key_buffer_size, alg,
1565 input, input_length,
1566 mac, mac_size, mac_length );
1567 return( status );
1568#endif /* PSA_CRYPTO_DRIVER_TEST */
1569#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1570 default:
1571 /* Key is declared with a lifetime not known to us */
1572 (void) key_buffer;
1573 (void) key_buffer_size;
1574 (void) alg;
1575 (void) input;
1576 (void) input_length;
1577 (void) mac;
1578 (void) mac_size;
1579 (void) mac_length;
1580 (void) status;
1581 return( PSA_ERROR_INVALID_ARGUMENT );
1582 }
1583}
1584
1585psa_status_t psa_driver_wrapper_mac_sign_setup(
1586 psa_mac_operation_t *operation,
1587 const psa_key_attributes_t *attributes,
1588 const uint8_t *key_buffer,
1589 size_t key_buffer_size,
1590 psa_algorithm_t alg )
1591{
1592 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1593 psa_key_location_t location =
1594 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1595
1596 switch( location )
1597 {
1598 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1599 /* Key is stored in the slot in export representation, so
1600 * cycle through all known transparent accelerators */
1601#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1602#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001603 status = mbedtls_test_transparent_mac_sign_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001604 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001605 attributes,
1606 key_buffer, key_buffer_size,
1607 alg );
1608 /* Declared with fallback == true */
1609 if( status == PSA_SUCCESS )
1610 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1611
1612 if( status != PSA_ERROR_NOT_SUPPORTED )
1613 return( status );
1614#endif /* PSA_CRYPTO_DRIVER_TEST */
1615#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1616#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1617 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001618 status = mbedtls_psa_mac_sign_setup( &operation->ctx.mbedtls_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001619 attributes,
1620 key_buffer, key_buffer_size,
1621 alg );
1622 if( status == PSA_SUCCESS )
1623 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1624
1625 if( status != PSA_ERROR_NOT_SUPPORTED )
1626 return( status );
1627#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1628 return( PSA_ERROR_NOT_SUPPORTED );
1629
1630 /* Add cases for opaque driver here */
1631#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1632#if defined(PSA_CRYPTO_DRIVER_TEST)
1633 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001634 status = mbedtls_test_opaque_mac_sign_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001635 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001636 attributes,
1637 key_buffer, key_buffer_size,
1638 alg );
1639
1640 if( status == PSA_SUCCESS )
1641 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
1642
1643 return( status );
1644#endif /* PSA_CRYPTO_DRIVER_TEST */
1645#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1646 default:
1647 /* Key is declared with a lifetime not known to us */
1648 (void) status;
Ronald Cron485559e2021-04-28 14:29:00 +02001649 (void) operation;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001650 (void) key_buffer;
1651 (void) key_buffer_size;
1652 (void) alg;
1653 return( PSA_ERROR_INVALID_ARGUMENT );
1654 }
1655}
1656
1657psa_status_t psa_driver_wrapper_mac_verify_setup(
1658 psa_mac_operation_t *operation,
1659 const psa_key_attributes_t *attributes,
1660 const uint8_t *key_buffer,
1661 size_t key_buffer_size,
1662 psa_algorithm_t alg )
1663{
1664 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1665 psa_key_location_t location =
1666 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1667
1668 switch( location )
1669 {
1670 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1671 /* Key is stored in the slot in export representation, so
1672 * cycle through all known transparent accelerators */
1673#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1674#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001675 status = mbedtls_test_transparent_mac_verify_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001676 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001677 attributes,
1678 key_buffer, key_buffer_size,
1679 alg );
1680 /* Declared with fallback == true */
1681 if( status == PSA_SUCCESS )
1682 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1683
1684 if( status != PSA_ERROR_NOT_SUPPORTED )
1685 return( status );
1686#endif /* PSA_CRYPTO_DRIVER_TEST */
1687#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1688#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1689 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001690 status = mbedtls_psa_mac_verify_setup( &operation->ctx.mbedtls_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001691 attributes,
1692 key_buffer, key_buffer_size,
1693 alg );
1694 if( status == PSA_SUCCESS )
1695 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1696
1697 if( status != PSA_ERROR_NOT_SUPPORTED )
1698 return( status );
1699#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1700 return( PSA_ERROR_NOT_SUPPORTED );
1701
1702 /* Add cases for opaque driver here */
1703#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1704#if defined(PSA_CRYPTO_DRIVER_TEST)
1705 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001706 status = mbedtls_test_opaque_mac_verify_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001707 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001708 attributes,
1709 key_buffer, key_buffer_size,
1710 alg );
1711
1712 if( status == PSA_SUCCESS )
1713 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
1714
1715 return( status );
1716#endif /* PSA_CRYPTO_DRIVER_TEST */
1717#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1718 default:
1719 /* Key is declared with a lifetime not known to us */
1720 (void) status;
Ronald Cron485559e2021-04-28 14:29:00 +02001721 (void) operation;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001722 (void) key_buffer;
1723 (void) key_buffer_size;
1724 (void) alg;
1725 return( PSA_ERROR_INVALID_ARGUMENT );
1726 }
1727}
1728
1729psa_status_t psa_driver_wrapper_mac_update(
1730 psa_mac_operation_t *operation,
1731 const uint8_t *input,
1732 size_t input_length )
1733{
1734 switch( operation->id )
1735 {
1736#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1737 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001738 return( mbedtls_psa_mac_update( &operation->ctx.mbedtls_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001739 input, input_length ) );
1740#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1741
1742#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1743#if defined(PSA_CRYPTO_DRIVER_TEST)
1744 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001745 return( mbedtls_test_transparent_mac_update(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001746 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001747 input, input_length ) );
1748
1749 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001750 return( mbedtls_test_opaque_mac_update(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001751 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001752 input, input_length ) );
1753#endif /* PSA_CRYPTO_DRIVER_TEST */
1754#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1755 default:
1756 (void) input;
1757 (void) input_length;
1758 return( PSA_ERROR_INVALID_ARGUMENT );
1759 }
1760}
1761
1762psa_status_t psa_driver_wrapper_mac_sign_finish(
1763 psa_mac_operation_t *operation,
1764 uint8_t *mac,
1765 size_t mac_size,
1766 size_t *mac_length )
1767{
1768 switch( operation->id )
1769 {
1770#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1771 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001772 return( mbedtls_psa_mac_sign_finish( &operation->ctx.mbedtls_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001773 mac, mac_size, mac_length ) );
1774#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1775
1776#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1777#if defined(PSA_CRYPTO_DRIVER_TEST)
1778 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001779 return( mbedtls_test_transparent_mac_sign_finish(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001780 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001781 mac, mac_size, mac_length ) );
1782
1783 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001784 return( mbedtls_test_opaque_mac_sign_finish(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001785 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001786 mac, mac_size, mac_length ) );
1787#endif /* PSA_CRYPTO_DRIVER_TEST */
1788#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1789 default:
1790 (void) mac;
1791 (void) mac_size;
1792 (void) mac_length;
1793 return( PSA_ERROR_INVALID_ARGUMENT );
1794 }
1795}
1796
1797psa_status_t psa_driver_wrapper_mac_verify_finish(
1798 psa_mac_operation_t *operation,
1799 const uint8_t *mac,
1800 size_t mac_length )
1801{
1802 switch( operation->id )
1803 {
1804#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1805 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001806 return( mbedtls_psa_mac_verify_finish( &operation->ctx.mbedtls_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001807 mac, mac_length ) );
1808#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1809
1810#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1811#if defined(PSA_CRYPTO_DRIVER_TEST)
1812 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001813 return( mbedtls_test_transparent_mac_verify_finish(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001814 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001815 mac, mac_length ) );
1816
1817 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001818 return( mbedtls_test_opaque_mac_verify_finish(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001819 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001820 mac, mac_length ) );
1821#endif /* PSA_CRYPTO_DRIVER_TEST */
1822#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1823 default:
1824 (void) mac;
1825 (void) mac_length;
1826 return( PSA_ERROR_INVALID_ARGUMENT );
1827 }
1828}
1829
1830psa_status_t psa_driver_wrapper_mac_abort(
1831 psa_mac_operation_t *operation )
1832{
Steven Cooreman896d51e2021-03-19 15:24:23 +01001833 switch( operation->id )
1834 {
1835#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1836 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman07897832021-03-19 18:28:56 +01001837 return( mbedtls_psa_mac_abort( &operation->ctx.mbedtls_ctx ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +01001838#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1839
1840#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1841#if defined(PSA_CRYPTO_DRIVER_TEST)
1842 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001843 return( mbedtls_test_transparent_mac_abort(
Steven Cooreman07897832021-03-19 18:28:56 +01001844 &operation->ctx.transparent_test_driver_ctx ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +01001845 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001846 return( mbedtls_test_opaque_mac_abort(
Steven Cooreman07897832021-03-19 18:28:56 +01001847 &operation->ctx.opaque_test_driver_ctx ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +01001848#endif /* PSA_CRYPTO_DRIVER_TEST */
1849#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1850 default:
Steven Cooreman07897832021-03-19 18:28:56 +01001851 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman896d51e2021-03-19 15:24:23 +01001852 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001853}
Gilles Peskinead0e0122021-04-24 13:19:45 +02001854
1855#endif /* MBEDTLS_PSA_CRYPTO_C */