blob: aab66ab20fbd8b171f2c5ea9f6cae98a5cd131cd [file] [log] [blame]
Steven Cooremancd84cb42020-07-16 20:28:36 +02001/*
2 * Functions to delegate cryptographic operations to an available
3 * and appropriate accelerator.
Steven Cooreman56250fd2020-09-04 13:07:15 +02004 * Warning: This file will be auto-generated in the future.
Steven Cooremancd84cb42020-07-16 20:28:36 +02005 */
Steven Cooreman2c7b2f82020-09-02 13:43:46 +02006/* Copyright The Mbed TLS Contributors
Steven Cooremancd84cb42020-07-16 20:28:36 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Steven Cooremancd84cb42020-07-16 20:28:36 +020020 */
21
Ronald Cronde822812021-03-17 16:08:20 +010022#include "psa_crypto_aead.h"
Ronald Cron0b805592020-12-14 18:08:20 +010023#include "psa_crypto_cipher.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020024#include "psa_crypto_core.h"
25#include "psa_crypto_driver_wrappers.h"
Steven Cooreman830aff22021-03-09 09:50:44 +010026#include "psa_crypto_hash.h"
Steven Cooreman896d51e2021-03-19 15:24:23 +010027#include "psa_crypto_mac.h"
Steven Cooreman830aff22021-03-09 09:50:44 +010028
Steven Cooreman2a1664c2020-07-20 15:33:08 +020029#include "mbedtls/platform.h"
30
31#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
Steven Cooremancd84cb42020-07-16 20:28:36 +020032
33/* Include test driver definition when running tests */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020034#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman56250fd2020-09-04 13:07:15 +020035#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020036#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020037#endif
38#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020039#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020040#endif
Ronald Cron05ee58d2021-04-30 17:00:34 +020041#include "test/drivers/test_driver.h"
Steven Cooremanf1720ea2020-07-24 18:41:58 +020042#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +020043
Steven Cooreman56250fd2020-09-04 13:07:15 +020044/* Repeat above block for each JSON-declared driver during autogeneration */
Steven Cooremanaa87fd02021-03-15 18:54:03 +010045#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Steven Cooreman56250fd2020-09-04 13:07:15 +020046
Steven Cooreman1e582352021-02-18 17:24:37 +010047/* Auto-generated values depending on which drivers are registered.
48 * ID 0 is reserved for unallocated operations.
49 * ID 1 is reserved for the Mbed TLS software driver. */
Steven Cooreman830aff22021-03-09 09:50:44 +010050#define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
51
Steven Cooreman37941cb2020-07-28 18:49:51 +020052#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman1e582352021-02-18 17:24:37 +010053#define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2)
54#define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3)
Steven Cooreman37941cb2020-07-28 18:49:51 +020055#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +020056
57/* Support the 'old' SE interface when asked to */
Steven Cooreman7a250572020-07-17 16:43:05 +020058#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman56250fd2020-09-04 13:07:15 +020059/* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
60 * SE driver is present, to avoid unused argument errors at compile time. */
61#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020062#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020063#endif
Steven Cooreman7a250572020-07-17 16:43:05 +020064#include "psa_crypto_se.h"
65#endif
66
Steven Cooremancd84cb42020-07-16 20:28:36 +020067/* Start delegation functions */
gabor-mezei-armc53f4f62021-04-22 11:32:19 +020068psa_status_t psa_driver_wrapper_sign_message(
69 const psa_key_attributes_t *attributes,
70 const uint8_t *key_buffer,
71 size_t key_buffer_size,
72 psa_algorithm_t alg,
73 const uint8_t *input,
74 size_t input_length,
75 uint8_t *signature,
76 size_t signature_size,
77 size_t *signature_length )
78{
79 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +020080 psa_key_location_t location =
81 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
82
83 switch( location )
84 {
85 case PSA_KEY_LOCATION_LOCAL_STORAGE:
86 /* Key is stored in the slot in export representation, so
87 * cycle through all known transparent accelerators */
88#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
89#if defined(PSA_CRYPTO_DRIVER_TEST)
90 status = mbedtls_test_transparent_signature_sign_message(
91 attributes,
92 key_buffer,
93 key_buffer_size,
94 alg,
95 input,
96 input_length,
97 signature,
98 signature_size,
99 signature_length );
100 /* Declared with fallback == true */
101 if( status != PSA_ERROR_NOT_SUPPORTED )
102 return( status );
103#endif /* PSA_CRYPTO_DRIVER_TEST */
104#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200105 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200106
107 /* Add cases for opaque driver here */
108#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
109#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arm77588fb2021-04-27 18:52:51 +0200110 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200111 status = mbedtls_test_opaque_signature_sign_message(
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200112 attributes,
113 key_buffer,
114 key_buffer_size,
115 alg,
116 input,
117 input_length,
118 signature,
119 signature_size,
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200120 signature_length );
121 if( status != PSA_ERROR_NOT_SUPPORTED )
122 return( status );
123 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200124#endif /* PSA_CRYPTO_DRIVER_TEST */
125#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
126 default:
127 /* Key is declared with a lifetime not known to us */
128 (void)status;
gabor-mezei-armf25c9762021-05-12 11:12:25 +0200129 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200130 }
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200131
132 return( psa_sign_message_builtin( attributes,
133 key_buffer,
134 key_buffer_size,
135 alg,
136 input,
137 input_length,
138 signature,
139 signature_size,
140 signature_length ) );
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200141}
142
143psa_status_t psa_driver_wrapper_verify_message(
144 const psa_key_attributes_t *attributes,
145 const uint8_t *key_buffer,
146 size_t key_buffer_size,
147 psa_algorithm_t alg,
148 const uint8_t *input,
149 size_t input_length,
150 const uint8_t *signature,
151 size_t signature_length )
152{
153 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200154 psa_key_location_t location =
155 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
156
157 switch( location )
158 {
159 case PSA_KEY_LOCATION_LOCAL_STORAGE:
160 /* Key is stored in the slot in export representation, so
161 * cycle through all known transparent accelerators */
162#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
163#if defined(PSA_CRYPTO_DRIVER_TEST)
164 status = mbedtls_test_transparent_signature_verify_message(
165 attributes,
166 key_buffer,
167 key_buffer_size,
168 alg,
169 input,
170 input_length,
171 signature,
172 signature_length );
173 /* Declared with fallback == true */
174 if( status != PSA_ERROR_NOT_SUPPORTED )
175 return( status );
176#endif /* PSA_CRYPTO_DRIVER_TEST */
177#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200178 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200179
180 /* Add cases for opaque driver here */
181#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
182#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arm77588fb2021-04-27 18:52:51 +0200183 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200184 return( mbedtls_test_opaque_signature_verify_message(
185 attributes,
186 key_buffer,
187 key_buffer_size,
188 alg,
189 input,
190 input_length,
191 signature,
192 signature_length ) );
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200193 if( status != PSA_ERROR_NOT_SUPPORTED )
194 return( status );
195 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200196#endif /* PSA_CRYPTO_DRIVER_TEST */
197#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
198 default:
199 /* Key is declared with a lifetime not known to us */
200 (void)status;
gabor-mezei-armf25c9762021-05-12 11:12:25 +0200201 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200202 }
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200203
204 return( psa_verify_message_builtin( attributes,
205 key_buffer,
206 key_buffer_size,
207 alg,
208 input,
209 input_length,
210 signature,
211 signature_length ) );
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200212}
213
Ronald Cron9f17aa42020-12-08 17:07:25 +0100214psa_status_t psa_driver_wrapper_sign_hash(
215 const psa_key_attributes_t *attributes,
216 const uint8_t *key_buffer, size_t key_buffer_size,
217 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
218 uint8_t *signature, size_t signature_size, size_t *signature_length )
Steven Cooremancd84cb42020-07-16 20:28:36 +0200219{
Steven Cooreman7a250572020-07-17 16:43:05 +0200220 /* Try dynamically-registered SE interface first */
221#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
222 const psa_drv_se_t *drv;
223 psa_drv_se_context_t *drv_context;
224
Ronald Cron9f17aa42020-12-08 17:07:25 +0100225 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman7a250572020-07-17 16:43:05 +0200226 {
227 if( drv->asymmetric == NULL ||
228 drv->asymmetric->p_sign == NULL )
229 {
230 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200231 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman7a250572020-07-17 16:43:05 +0200232 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100233 return( drv->asymmetric->p_sign(
234 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
235 alg, hash, hash_length,
236 signature, signature_size, signature_length ) );
Steven Cooreman7a250572020-07-17 16:43:05 +0200237 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200238#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman7a250572020-07-17 16:43:05 +0200239
Ronald Cronfce9df22020-12-08 18:06:03 +0100240 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100241 psa_key_location_t location =
242 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200243
244 switch( location )
245 {
246 case PSA_KEY_LOCATION_LOCAL_STORAGE:
247 /* Key is stored in the slot in export representation, so
248 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100249#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200250#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200251 status = mbedtls_test_transparent_signature_sign_hash( attributes,
Ronald Cron9f17aa42020-12-08 17:07:25 +0100252 key_buffer,
253 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200254 alg,
255 hash,
256 hash_length,
257 signature,
258 signature_size,
259 signature_length );
260 /* Declared with fallback == true */
261 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200262 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200263#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100264#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200265 /* Fell through, meaning no accelerator supports this operation */
gabor-mezei-armf3c5c862021-05-05 14:18:36 +0200266 return( psa_sign_hash_builtin( attributes,
267 key_buffer,
268 key_buffer_size,
269 alg,
270 hash,
271 hash_length,
272 signature,
273 signature_size,
274 signature_length ) );
Ronald Cronfce9df22020-12-08 18:06:03 +0100275
Steven Cooremancd84cb42020-07-16 20:28:36 +0200276 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100277#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200278#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100279 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200280 return( mbedtls_test_opaque_signature_sign_hash( attributes,
281 key_buffer,
282 key_buffer_size,
283 alg,
284 hash,
285 hash_length,
286 signature,
287 signature_size,
288 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200289#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100290#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200291 default:
292 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100293 (void)status;
294 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200295 }
Steven Cooremancd84cb42020-07-16 20:28:36 +0200296}
297
Ronald Cron9f17aa42020-12-08 17:07:25 +0100298psa_status_t psa_driver_wrapper_verify_hash(
299 const psa_key_attributes_t *attributes,
300 const uint8_t *key_buffer, size_t key_buffer_size,
301 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
302 const uint8_t *signature, size_t signature_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200303{
Steven Cooreman55ae2172020-07-17 19:46:15 +0200304 /* Try dynamically-registered SE interface first */
305#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
306 const psa_drv_se_t *drv;
307 psa_drv_se_context_t *drv_context;
308
Ronald Cron9f17aa42020-12-08 17:07:25 +0100309 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200310 {
311 if( drv->asymmetric == NULL ||
312 drv->asymmetric->p_verify == NULL )
313 {
314 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200315 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200316 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100317 return( drv->asymmetric->p_verify(
318 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
319 alg, hash, hash_length,
320 signature, signature_length ) );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200321 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200322#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200323
Ronald Cronfce9df22020-12-08 18:06:03 +0100324 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100325 psa_key_location_t location =
326 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200327
328 switch( location )
329 {
330 case PSA_KEY_LOCATION_LOCAL_STORAGE:
331 /* Key is stored in the slot in export representation, so
332 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100333#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200334#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200335 status = mbedtls_test_transparent_signature_verify_hash(
336 attributes,
337 key_buffer,
338 key_buffer_size,
339 alg,
340 hash,
341 hash_length,
342 signature,
343 signature_length );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200344 /* Declared with fallback == true */
345 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200346 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200347#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100348#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
349
gabor-mezei-armf3c5c862021-05-05 14:18:36 +0200350 return( psa_verify_hash_builtin( attributes,
351 key_buffer,
352 key_buffer_size,
353 alg,
354 hash,
355 hash_length,
356 signature,
357 signature_length ) );
Ronald Cronfce9df22020-12-08 18:06:03 +0100358
Steven Cooreman55ae2172020-07-17 19:46:15 +0200359 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100360#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200361#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100362 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200363 return( mbedtls_test_opaque_signature_verify_hash( attributes,
364 key_buffer,
365 key_buffer_size,
366 alg,
367 hash,
368 hash_length,
369 signature,
370 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200371#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100372#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200373 default:
374 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100375 (void)status;
376 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200377 }
Steven Cooreman55ae2172020-07-17 19:46:15 +0200378}
379
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200380/** Get the key buffer size required to store the key material of a key
381 * associated with an opaque driver without storage.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200382 *
Ronald Cron31216282020-12-05 18:47:56 +0100383 * \param[in] attributes The key attributes.
384 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200385 *
386 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100387 * The minimum size for a buffer to contain the key material has been
388 * returned successfully.
389 * \retval #PSA_ERROR_INVALID_ARGUMENT
390 * The size in bits of the key is not valid.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200391 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100392 * The type and/or the size in bits of the key or the combination of
393 * the two is not supported.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200394 */
Ronald Cron9df74be2020-12-05 19:15:23 +0100395psa_status_t psa_driver_wrapper_get_key_buffer_size(
Ronald Cron31216282020-12-05 18:47:56 +0100396 const psa_key_attributes_t *attributes,
397 size_t *key_buffer_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200398{
John Durkop2c618352020-09-22 06:54:01 -0700399 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
400 psa_key_type_t key_type = attributes->core.type;
401 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200402
Ronald Cron31216282020-12-05 18:47:56 +0100403 *key_buffer_size = 0;
John Durkop2c618352020-09-22 06:54:01 -0700404 switch( location )
405 {
John Durkop2c618352020-09-22 06:54:01 -0700406#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100407 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100408#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
409 /* Emulate property 'builtin_key_size' */
410 if( psa_key_id_is_builtin(
411 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(
412 psa_get_key_id( attributes ) ) ) )
413 {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100414 *key_buffer_size = sizeof( psa_drv_slot_number_t );
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100415 return( PSA_SUCCESS );
416 }
417#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200418 *key_buffer_size = mbedtls_test_size_function( key_type, key_bits );
Ronald Cronb0737da2021-04-14 10:55:34 +0200419 return( ( *key_buffer_size != 0 ) ?
420 PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
John Durkop2c618352020-09-22 06:54:01 -0700421#endif /* PSA_CRYPTO_DRIVER_TEST */
422
423 default:
Ronald Cron9df74be2020-12-05 19:15:23 +0100424 (void)key_type;
425 (void)key_bits;
Steven Cooreman56250fd2020-09-04 13:07:15 +0200426 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200427 }
428}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200429
Ronald Cron977c2472020-10-13 08:32:21 +0200430psa_status_t psa_driver_wrapper_generate_key(
431 const psa_key_attributes_t *attributes,
432 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200433{
Ronald Cron977c2472020-10-13 08:32:21 +0200434 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
435 psa_key_location_t location =
436 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
437
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200438 /* Try dynamically-registered SE interface first */
439#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
440 const psa_drv_se_t *drv;
441 psa_drv_se_context_t *drv_context;
442
Ronald Cron977c2472020-10-13 08:32:21 +0200443 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200444 {
445 size_t pubkey_length = 0; /* We don't support this feature yet */
446 if( drv->key_management == NULL ||
447 drv->key_management->p_generate == NULL )
448 {
449 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200450 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200451 }
452 return( drv->key_management->p_generate(
Ronald Cron977c2472020-10-13 08:32:21 +0200453 drv_context,
454 *( (psa_key_slot_number_t *)key_buffer ),
Ronald Cronea0f8a62020-11-25 17:52:23 +0100455 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200456 }
457#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
458
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200459 switch( location )
460 {
461 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Ronald Cron977c2472020-10-13 08:32:21 +0200462#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200463 /* Transparent drivers are limited to generating asymmetric keys */
Ronald Cron977c2472020-10-13 08:32:21 +0200464 if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200465 {
Ronald Cron977c2472020-10-13 08:32:21 +0200466 /* Cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200467#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200468 status = mbedtls_test_transparent_generate_key(
Ronald Cron977c2472020-10-13 08:32:21 +0200469 attributes, key_buffer, key_buffer_size,
470 key_buffer_length );
471 /* Declared with fallback == true */
472 if( status != PSA_ERROR_NOT_SUPPORTED )
473 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200474#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200475 }
476#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
477
478 /* Software fallback */
479 status = psa_generate_key_internal(
480 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200481 break;
Ronald Cron977c2472020-10-13 08:32:21 +0200482
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200483 /* Add cases for opaque driver here */
Ronald Cron977c2472020-10-13 08:32:21 +0200484#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200485#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100486 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200487 status = mbedtls_test_opaque_generate_key(
Ronald Cron977c2472020-10-13 08:32:21 +0200488 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200489 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200490#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200491#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
492
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200493 default:
494 /* Key is declared with a lifetime not known to us */
495 status = PSA_ERROR_INVALID_ARGUMENT;
496 break;
497 }
498
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200499 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200500}
501
Ronald Cron83282872020-11-22 14:02:39 +0100502psa_status_t psa_driver_wrapper_import_key(
503 const psa_key_attributes_t *attributes,
504 const uint8_t *data,
505 size_t data_length,
506 uint8_t *key_buffer,
507 size_t key_buffer_size,
508 size_t *key_buffer_length,
509 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200510{
Steven Cooreman04524762020-10-13 17:43:44 +0200511 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100512 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
513 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200514
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100515 /* Try dynamically-registered SE interface first */
516#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
517 const psa_drv_se_t *drv;
518 psa_drv_se_context_t *drv_context;
519
520 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
521 {
522 if( drv->key_management == NULL ||
523 drv->key_management->p_import == NULL )
524 return( PSA_ERROR_NOT_SUPPORTED );
525
526 /* The driver should set the number of key bits, however in
527 * case it doesn't, we initialize bits to an invalid value. */
528 *bits = PSA_MAX_KEY_BITS + 1;
529 status = drv->key_management->p_import(
530 drv_context,
531 *( (psa_key_slot_number_t *)key_buffer ),
532 attributes, data, data_length, bits );
533
534 if( status != PSA_SUCCESS )
535 return( status );
536
537 if( (*bits) > PSA_MAX_KEY_BITS )
538 return( PSA_ERROR_NOT_SUPPORTED );
539
540 return( PSA_SUCCESS );
541 }
542#endif /* PSA_CRYPTO_SE_C */
543
Ronald Cronbf33c932020-11-28 18:06:53 +0100544 switch( location )
545 {
546 case PSA_KEY_LOCATION_LOCAL_STORAGE:
547 /* Key is stored in the slot in export representation, so
548 * cycle through all known transparent accelerators */
549#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
550#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200551 status = mbedtls_test_transparent_import_key(
552 attributes,
553 data, data_length,
554 key_buffer, key_buffer_size,
555 key_buffer_length, bits );
Ronald Cronbf33c932020-11-28 18:06:53 +0100556 /* Declared with fallback == true */
557 if( status != PSA_ERROR_NOT_SUPPORTED )
558 return( status );
559#endif /* PSA_CRYPTO_DRIVER_TEST */
560#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
561 /* Fell through, meaning no accelerator supports this operation */
562 return( psa_import_key_into_slot( attributes,
563 data, data_length,
564 key_buffer, key_buffer_size,
565 key_buffer_length, bits ) );
566
567 default:
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100568 /* Importing a key with external storage in not yet supported.
569 * Return in error indicating that the lifetime is not valid. */
Ronald Cronbf33c932020-11-28 18:06:53 +0100570 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100571 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100572 }
573
Steven Cooreman04524762020-10-13 17:43:44 +0200574}
575
Ronald Cron67227982020-11-26 15:16:05 +0100576psa_status_t psa_driver_wrapper_export_key(
577 const psa_key_attributes_t *attributes,
578 const uint8_t *key_buffer, size_t key_buffer_size,
579 uint8_t *data, size_t data_size, size_t *data_length )
580
581{
582 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
583 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
584 psa_get_key_lifetime( attributes ) );
585
Ronald Cron152e3f82020-11-26 16:06:41 +0100586 /* Try dynamically-registered SE interface first */
587#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
588 const psa_drv_se_t *drv;
589 psa_drv_se_context_t *drv_context;
590
591 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
592 {
593 if( ( drv->key_management == NULL ) ||
594 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100595 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100596 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100597 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100598
599 return( drv->key_management->p_export(
600 drv_context,
601 *( (psa_key_slot_number_t *)key_buffer ),
602 data, data_size, data_length ) );
603 }
604#endif /* PSA_CRYPTO_SE_C */
605
Ronald Cron67227982020-11-26 15:16:05 +0100606 switch( location )
607 {
608 case PSA_KEY_LOCATION_LOCAL_STORAGE:
609 return( psa_export_key_internal( attributes,
610 key_buffer,
611 key_buffer_size,
612 data,
613 data_size,
614 data_length ) );
615
616 /* Add cases for opaque driver here */
617#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
618#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100619 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200620 return( mbedtls_test_opaque_export_key( attributes,
621 key_buffer,
622 key_buffer_size,
623 data,
624 data_size,
625 data_length ) );
Ronald Cron67227982020-11-26 15:16:05 +0100626#endif /* PSA_CRYPTO_DRIVER_TEST */
627#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
628 default:
629 /* Key is declared with a lifetime not known to us */
630 return( status );
631 }
632}
633
Ronald Cron84cc9942020-11-25 14:30:05 +0100634psa_status_t psa_driver_wrapper_export_public_key(
635 const psa_key_attributes_t *attributes,
636 const uint8_t *key_buffer, size_t key_buffer_size,
637 uint8_t *data, size_t data_size, size_t *data_length )
638
Steven Cooremanb9b84422020-10-14 14:39:20 +0200639{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200640 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100641 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
642 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200643
Ronald Cron152e3f82020-11-26 16:06:41 +0100644 /* Try dynamically-registered SE interface first */
645#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
646 const psa_drv_se_t *drv;
647 psa_drv_se_context_t *drv_context;
648
649 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
650 {
651 if( ( drv->key_management == NULL ) ||
652 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100653 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100654 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100655 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100656
657 return( drv->key_management->p_export_public(
658 drv_context,
659 *( (psa_key_slot_number_t *)key_buffer ),
660 data, data_size, data_length ) );
661 }
662#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
663
Steven Cooremanb9b84422020-10-14 14:39:20 +0200664 switch( location )
665 {
666 case PSA_KEY_LOCATION_LOCAL_STORAGE:
667 /* Key is stored in the slot in export representation, so
668 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100669#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200670#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200671 status = mbedtls_test_transparent_export_public_key(
672 attributes,
673 key_buffer,
674 key_buffer_size,
675 data,
676 data_size,
677 data_length );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200678 /* Declared with fallback == true */
679 if( status != PSA_ERROR_NOT_SUPPORTED )
680 return( status );
681#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100682#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200683 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100684 return( psa_export_public_key_internal( attributes,
685 key_buffer,
686 key_buffer_size,
687 data,
688 data_size,
689 data_length ) );
690
Steven Cooremanb9b84422020-10-14 14:39:20 +0200691 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100692#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200693#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100694 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200695 return( mbedtls_test_opaque_export_public_key( attributes,
696 key_buffer,
697 key_buffer_size,
698 data,
699 data_size,
700 data_length ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200701#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100702#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200703 default:
704 /* Key is declared with a lifetime not known to us */
705 return( status );
706 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200707}
708
Steven Cooreman6801f082021-02-19 17:21:22 +0100709psa_status_t psa_driver_wrapper_get_builtin_key(
710 psa_drv_slot_number_t slot_number,
711 psa_key_attributes_t *attributes,
712 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
713{
714 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
715 switch( location )
716 {
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100717#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100718 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200719 return( mbedtls_test_opaque_get_builtin_key(
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100720 slot_number,
721 attributes,
722 key_buffer, key_buffer_size, key_buffer_length ) );
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100723#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman6801f082021-02-19 17:21:22 +0100724 default:
725 (void) slot_number;
726 (void) key_buffer;
727 (void) key_buffer_size;
728 (void) key_buffer_length;
729 return( PSA_ERROR_DOES_NOT_EXIST );
730 }
731}
732
Steven Cooreman37941cb2020-07-28 18:49:51 +0200733/*
734 * Cipher functions
735 */
736psa_status_t psa_driver_wrapper_cipher_encrypt(
737 psa_key_slot_t *slot,
738 psa_algorithm_t alg,
739 const uint8_t *input,
740 size_t input_length,
741 uint8_t *output,
742 size_t output_size,
743 size_t *output_length )
744{
745#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
746 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
747 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
748 psa_key_attributes_t attributes = {
749 .core = slot->attr
750 };
751
752 switch( location )
753 {
754 case PSA_KEY_LOCATION_LOCAL_STORAGE:
755 /* Key is stored in the slot in export representation, so
756 * cycle through all known transparent accelerators */
757#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200758 status = mbedtls_test_transparent_cipher_encrypt( &attributes,
759 slot->key.data,
760 slot->key.bytes,
761 alg,
762 input,
763 input_length,
764 output,
765 output_size,
766 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200767 /* Declared with fallback == true */
768 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200769 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200770#endif /* PSA_CRYPTO_DRIVER_TEST */
771 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200772 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200773 /* Add cases for opaque driver here */
774#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100775 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200776 return( mbedtls_test_opaque_cipher_encrypt( &attributes,
777 slot->key.data,
778 slot->key.bytes,
779 alg,
780 input,
781 input_length,
782 output,
783 output_size,
784 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200785#endif /* PSA_CRYPTO_DRIVER_TEST */
786 default:
787 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200788 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200789 }
790#else /* PSA_CRYPTO_DRIVER_PRESENT */
791 (void) slot;
792 (void) alg;
793 (void) input;
794 (void) input_length;
795 (void) output;
796 (void) output_size;
797 (void) output_length;
798
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200799 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200800#endif /* PSA_CRYPTO_DRIVER_PRESENT */
801}
802
803psa_status_t psa_driver_wrapper_cipher_decrypt(
804 psa_key_slot_t *slot,
805 psa_algorithm_t alg,
806 const uint8_t *input,
807 size_t input_length,
808 uint8_t *output,
809 size_t output_size,
810 size_t *output_length )
811{
812#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
813 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
814 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
815 psa_key_attributes_t attributes = {
816 .core = slot->attr
817 };
818
819 switch( location )
820 {
821 case PSA_KEY_LOCATION_LOCAL_STORAGE:
822 /* Key is stored in the slot in export representation, so
823 * cycle through all known transparent accelerators */
824#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200825 status = mbedtls_test_transparent_cipher_decrypt( &attributes,
826 slot->key.data,
827 slot->key.bytes,
828 alg,
829 input,
830 input_length,
831 output,
832 output_size,
833 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200834 /* Declared with fallback == true */
835 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200836 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200837#endif /* PSA_CRYPTO_DRIVER_TEST */
838 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200839 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200840 /* Add cases for opaque driver here */
841#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100842 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200843 return( mbedtls_test_opaque_cipher_decrypt( &attributes,
844 slot->key.data,
845 slot->key.bytes,
846 alg,
847 input,
848 input_length,
849 output,
850 output_size,
851 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200852#endif /* PSA_CRYPTO_DRIVER_TEST */
853 default:
854 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200855 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200856 }
857#else /* PSA_CRYPTO_DRIVER_PRESENT */
858 (void) slot;
859 (void) alg;
860 (void) input;
861 (void) input_length;
862 (void) output;
863 (void) output_size;
864 (void) output_length;
865
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200866 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200867#endif /* PSA_CRYPTO_DRIVER_PRESENT */
868}
869
870psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100871 psa_cipher_operation_t *operation,
872 const psa_key_attributes_t *attributes,
873 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200874 psa_algorithm_t alg )
875{
Ronald Cron0b805592020-12-14 18:08:20 +0100876 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona4af55f2020-12-14 14:36:06 +0100877 psa_key_location_t location =
878 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200879
Steven Cooreman37941cb2020-07-28 18:49:51 +0200880 switch( location )
881 {
882 case PSA_KEY_LOCATION_LOCAL_STORAGE:
883 /* Key is stored in the slot in export representation, so
884 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100885#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200886#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200887 status = mbedtls_test_transparent_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100888 &operation->ctx.transparent_test_driver_ctx,
889 attributes,
890 key_buffer,
891 key_buffer_size,
892 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200893 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200894 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100895 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +0200896
Ronald Cron0b805592020-12-14 18:08:20 +0100897 if( status != PSA_ERROR_NOT_SUPPORTED )
898 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200899#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100900#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100901#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200902 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +0100903 status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100904 attributes,
905 key_buffer,
906 key_buffer_size,
907 alg );
908 if( status == PSA_SUCCESS )
Ronald Cron6e412a72021-03-10 09:58:47 +0100909 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Ronald Cron49fafa92021-03-10 08:34:23 +0100910
Ronald Cron7b4154d2021-03-19 14:49:41 +0100911 if( status != PSA_ERROR_NOT_SUPPORTED )
912 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100913#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
914 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron0b805592020-12-14 18:08:20 +0100915
Steven Cooreman37941cb2020-07-28 18:49:51 +0200916 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100917#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200918#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100919 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200920 status = mbedtls_test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100921 &operation->ctx.opaque_test_driver_ctx,
922 attributes,
923 key_buffer, key_buffer_size,
924 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200925
Steven Cooreman37941cb2020-07-28 18:49:51 +0200926 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100927 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200928
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200929 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200930#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100931#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +0200932 default:
933 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +0100934 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100935 (void)key_buffer;
936 (void)key_buffer_size;
937 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +0200938 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200939 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200940}
941
942psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100943 psa_cipher_operation_t *operation,
944 const psa_key_attributes_t *attributes,
945 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200946 psa_algorithm_t alg )
947{
Steven Cooreman37941cb2020-07-28 18:49:51 +0200948 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crona4af55f2020-12-14 14:36:06 +0100949 psa_key_location_t location =
950 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200951
Steven Cooreman37941cb2020-07-28 18:49:51 +0200952 switch( location )
953 {
954 case PSA_KEY_LOCATION_LOCAL_STORAGE:
955 /* Key is stored in the slot in export representation, so
956 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100957#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200958#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200959 status = mbedtls_test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100960 &operation->ctx.transparent_test_driver_ctx,
961 attributes,
962 key_buffer,
963 key_buffer_size,
964 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200965 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200966 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100967 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +0200968
Ronald Cron0b805592020-12-14 18:08:20 +0100969 if( status != PSA_ERROR_NOT_SUPPORTED )
970 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200971#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100972#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100973#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200974 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +0100975 status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100976 attributes,
977 key_buffer,
978 key_buffer_size,
979 alg );
980 if( status == PSA_SUCCESS )
981 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
982
983 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100984#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
985 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron49fafa92021-03-10 08:34:23 +0100986
Steven Cooreman37941cb2020-07-28 18:49:51 +0200987 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100988#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200989#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100990 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200991 status = mbedtls_test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100992 &operation->ctx.opaque_test_driver_ctx,
993 attributes,
994 key_buffer, key_buffer_size,
995 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200996
Steven Cooreman37941cb2020-07-28 18:49:51 +0200997 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100998 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200999
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001000 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001001#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001002#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +02001003 default:
1004 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +01001005 (void)status;
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
Steven Cooreman37941cb2020-07-28 18:49:51 +02001013psa_status_t psa_driver_wrapper_cipher_set_iv(
Ronald Cron6056fe82020-12-15 13:58:07 +01001014 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001015 const uint8_t *iv,
1016 size_t iv_length )
1017{
Ronald Cron49fafa92021-03-10 08:34:23 +01001018 switch( operation->id )
1019 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001020#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001021 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001022 return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001023 iv,
1024 iv_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001025#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001026
1027#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001028#if defined(PSA_CRYPTO_DRIVER_TEST)
1029 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001030 return( mbedtls_test_transparent_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001031 &operation->ctx.transparent_test_driver_ctx,
1032 iv, iv_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001033
Steven Cooreman37941cb2020-07-28 18:49:51 +02001034 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001035 return( mbedtls_test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001036 &operation->ctx.opaque_test_driver_ctx,
1037 iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001038#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001039#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001040 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001041
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001042 (void)iv;
1043 (void)iv_length;
1044
Ronald Crondd24c9b2020-12-15 14:10:01 +01001045 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001046}
1047
1048psa_status_t psa_driver_wrapper_cipher_update(
Ronald Cron6056fe82020-12-15 13:58:07 +01001049 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001050 const uint8_t *input,
1051 size_t input_length,
1052 uint8_t *output,
1053 size_t output_size,
1054 size_t *output_length )
1055{
Ronald Cron49fafa92021-03-10 08:34:23 +01001056 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001057 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001058#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001059 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001060 return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001061 input,
1062 input_length,
1063 output,
1064 output_size,
1065 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001066#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1067
Ronald Cron49fafa92021-03-10 08:34:23 +01001068#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001069#if defined(PSA_CRYPTO_DRIVER_TEST)
1070 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001071 return( mbedtls_test_transparent_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001072 &operation->ctx.transparent_test_driver_ctx,
1073 input, input_length,
1074 output, output_size, output_length ) );
1075
Steven Cooreman37941cb2020-07-28 18:49:51 +02001076 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001077 return( mbedtls_test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001078 &operation->ctx.opaque_test_driver_ctx,
1079 input, input_length,
1080 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001081#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001082#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001083 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001084
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001085 (void)input;
1086 (void)input_length;
1087 (void)output;
1088 (void)output_size;
1089 (void)output_length;
1090
Ronald Crondd24c9b2020-12-15 14:10:01 +01001091 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001092}
1093
1094psa_status_t psa_driver_wrapper_cipher_finish(
Ronald Cron6056fe82020-12-15 13:58:07 +01001095 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001096 uint8_t *output,
1097 size_t output_size,
1098 size_t *output_length )
1099{
Ronald Cron49fafa92021-03-10 08:34:23 +01001100 switch( operation->id )
1101 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001102#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001103 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001104 return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001105 output,
1106 output_size,
1107 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001108#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001109
1110#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001111#if defined(PSA_CRYPTO_DRIVER_TEST)
1112 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001113 return( mbedtls_test_transparent_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001114 &operation->ctx.transparent_test_driver_ctx,
1115 output, output_size, output_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001116
Steven Cooreman37941cb2020-07-28 18:49:51 +02001117 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001118 return( mbedtls_test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001119 &operation->ctx.opaque_test_driver_ctx,
1120 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001121#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001122#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001123 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001124
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001125 (void)output;
1126 (void)output_size;
1127 (void)output_length;
1128
Ronald Crondd24c9b2020-12-15 14:10:01 +01001129 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001130}
1131
1132psa_status_t psa_driver_wrapper_cipher_abort(
Ronald Cron6056fe82020-12-15 13:58:07 +01001133 psa_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001134{
Ronald Crondd24c9b2020-12-15 14:10:01 +01001135 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001136
Ronald Cron49fafa92021-03-10 08:34:23 +01001137 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001138 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001139#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001140 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001141 return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001142#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +01001143
1144#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001145#if defined(PSA_CRYPTO_DRIVER_TEST)
1146 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001147 status = mbedtls_test_transparent_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001148 &operation->ctx.transparent_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001149 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001150 &operation->ctx.transparent_test_driver_ctx,
1151 sizeof( operation->ctx.transparent_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001152 return( status );
Ronald Cron49fafa92021-03-10 08:34:23 +01001153
Steven Cooreman37941cb2020-07-28 18:49:51 +02001154 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001155 status = mbedtls_test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001156 &operation->ctx.opaque_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001157 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001158 &operation->ctx.opaque_test_driver_ctx,
1159 sizeof( operation->ctx.opaque_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001160 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001161#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001162#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001163 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001164
Ronald Cron49fafa92021-03-10 08:34:23 +01001165 (void)status;
Ronald Crondd24c9b2020-12-15 14:10:01 +01001166 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001167}
1168
Steven Cooreman1e582352021-02-18 17:24:37 +01001169/*
1170 * Hashing functions
1171 */
1172psa_status_t psa_driver_wrapper_hash_compute(
1173 psa_algorithm_t alg,
1174 const uint8_t *input,
1175 size_t input_length,
1176 uint8_t *hash,
1177 size_t hash_size,
1178 size_t *hash_length)
1179{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001180 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001181
1182 /* Try accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001183#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001184 status = mbedtls_test_transparent_hash_compute(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001185 alg, input, input_length, hash, hash_size, hash_length );
Steven Cooremanf7638102021-03-04 15:14:36 +01001186 if( status != PSA_ERROR_NOT_SUPPORTED )
1187 return( status );
1188#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001189
1190 /* If software fallback is compiled in, try fallback */
1191#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1192 status = mbedtls_psa_hash_compute( alg, input, input_length,
1193 hash, hash_size, hash_length );
1194 if( status != PSA_ERROR_NOT_SUPPORTED )
1195 return( status );
1196#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001197 (void) status;
1198 (void) alg;
1199 (void) input;
1200 (void) input_length;
1201 (void) hash;
1202 (void) hash_size;
1203 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001204
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001205 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman1e582352021-02-18 17:24:37 +01001206}
1207
1208psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001209 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001210 psa_algorithm_t alg )
1211{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001212 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001213
Steven Cooreman1e582352021-02-18 17:24:37 +01001214 /* Try setup on accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001215#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001216 status = mbedtls_test_transparent_hash_setup(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001217 &operation->ctx.test_driver_ctx, alg );
Steven Cooremanf7638102021-03-04 15:14:36 +01001218 if( status == PSA_SUCCESS )
1219 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1220
1221 if( status != PSA_ERROR_NOT_SUPPORTED )
1222 return( status );
1223#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001224
1225 /* If software fallback is compiled in, try fallback */
1226#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001227 status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
Steven Cooreman1e582352021-02-18 17:24:37 +01001228 if( status == PSA_SUCCESS )
Steven Cooreman1e582352021-02-18 17:24:37 +01001229 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Steven Cooreman1e582352021-02-18 17:24:37 +01001230
1231 if( status != PSA_ERROR_NOT_SUPPORTED )
1232 return( status );
1233#endif
1234 /* Nothing left to try if we fall through here */
1235 (void) status;
1236 (void) operation;
1237 (void) alg;
1238 return( PSA_ERROR_NOT_SUPPORTED );
1239}
1240
1241psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001242 const psa_hash_operation_t *source_operation,
1243 psa_hash_operation_t *target_operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001244{
Steven Cooreman1e582352021-02-18 17:24:37 +01001245 switch( source_operation->id )
1246 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001247#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1248 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1249 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1250 return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
1251 &target_operation->ctx.mbedtls_ctx ) );
1252#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001253#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001254 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1255 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001256 return( mbedtls_test_transparent_hash_clone(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001257 &source_operation->ctx.test_driver_ctx,
1258 &target_operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001259#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001260 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001261 (void) target_operation;
1262 return( PSA_ERROR_BAD_STATE );
1263 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001264}
1265
1266psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001267 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001268 const uint8_t *input,
1269 size_t input_length )
1270{
Steven Cooreman1e582352021-02-18 17:24:37 +01001271 switch( operation->id )
1272 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001273#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1274 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1275 return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
1276 input, input_length ) );
1277#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001278#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001279 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001280 return( mbedtls_test_transparent_hash_update(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001281 &operation->ctx.test_driver_ctx,
1282 input, input_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001283#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001284 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001285 (void) input;
1286 (void) input_length;
1287 return( PSA_ERROR_BAD_STATE );
1288 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001289}
1290
1291psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001292 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001293 uint8_t *hash,
1294 size_t hash_size,
1295 size_t *hash_length )
1296{
Steven Cooreman1e582352021-02-18 17:24:37 +01001297 switch( operation->id )
1298 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001299#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1300 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1301 return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
1302 hash, hash_size, hash_length ) );
1303#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001304#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001305 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001306 return( mbedtls_test_transparent_hash_finish(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001307 &operation->ctx.test_driver_ctx,
1308 hash, hash_size, hash_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001309#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001310 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001311 (void) hash;
1312 (void) hash_size;
1313 (void) hash_length;
1314 return( PSA_ERROR_BAD_STATE );
1315 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001316}
1317
1318psa_status_t psa_driver_wrapper_hash_abort(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001319 psa_hash_operation_t *operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001320{
Steven Cooreman1e582352021-02-18 17:24:37 +01001321 switch( operation->id )
1322 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001323#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1324 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1325 return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
1326#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001327#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001328 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001329 return( mbedtls_test_transparent_hash_abort(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001330 &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 return( PSA_ERROR_BAD_STATE );
1334 }
1335}
1336
Ronald Cronde822812021-03-17 16:08:20 +01001337psa_status_t psa_driver_wrapper_aead_encrypt(
1338 const psa_key_attributes_t *attributes,
1339 const uint8_t *key_buffer, size_t key_buffer_size,
1340 psa_algorithm_t alg,
1341 const uint8_t *nonce, size_t nonce_length,
1342 const uint8_t *additional_data, size_t additional_data_length,
1343 const uint8_t *plaintext, size_t plaintext_length,
1344 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
1345{
1346 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1347 psa_key_location_t location =
1348 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1349
1350 switch( location )
1351 {
1352 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1353 /* Key is stored in the slot in export representation, so
1354 * cycle through all known transparent accelerators */
1355
1356#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1357#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001358 status = mbedtls_test_transparent_aead_encrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001359 attributes, key_buffer, key_buffer_size,
1360 alg,
1361 nonce, nonce_length,
1362 additional_data, additional_data_length,
1363 plaintext, plaintext_length,
1364 ciphertext, ciphertext_size, ciphertext_length );
1365 /* Declared with fallback == true */
1366 if( status != PSA_ERROR_NOT_SUPPORTED )
1367 return( status );
1368#endif /* PSA_CRYPTO_DRIVER_TEST */
1369#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1370
1371 /* Fell through, meaning no accelerator supports this operation */
1372 return( mbedtls_psa_aead_encrypt(
1373 attributes, key_buffer, key_buffer_size,
1374 alg,
1375 nonce, nonce_length,
1376 additional_data, additional_data_length,
1377 plaintext, plaintext_length,
1378 ciphertext, ciphertext_size, ciphertext_length ) );
1379
1380 /* Add cases for opaque driver here */
1381
1382 default:
1383 /* Key is declared with a lifetime not known to us */
1384 (void)status;
1385 return( PSA_ERROR_INVALID_ARGUMENT );
1386 }
1387}
1388
1389psa_status_t psa_driver_wrapper_aead_decrypt(
1390 const psa_key_attributes_t *attributes,
1391 const uint8_t *key_buffer, size_t key_buffer_size,
1392 psa_algorithm_t alg,
1393 const uint8_t *nonce, size_t nonce_length,
1394 const uint8_t *additional_data, size_t additional_data_length,
1395 const uint8_t *ciphertext, size_t ciphertext_length,
1396 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
1397{
1398 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1399 psa_key_location_t location =
1400 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1401
1402 switch( location )
1403 {
1404 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1405 /* Key is stored in the slot in export representation, so
1406 * cycle through all known transparent accelerators */
1407
1408#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1409#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001410 status = mbedtls_test_transparent_aead_decrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001411 attributes, key_buffer, key_buffer_size,
1412 alg,
1413 nonce, nonce_length,
1414 additional_data, additional_data_length,
1415 ciphertext, ciphertext_length,
1416 plaintext, plaintext_size, plaintext_length );
1417 /* Declared with fallback == true */
1418 if( status != PSA_ERROR_NOT_SUPPORTED )
1419 return( status );
1420#endif /* PSA_CRYPTO_DRIVER_TEST */
1421#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1422
1423 /* Fell through, meaning no accelerator supports this operation */
1424 return( mbedtls_psa_aead_decrypt(
1425 attributes, key_buffer, key_buffer_size,
1426 alg,
1427 nonce, nonce_length,
1428 additional_data, additional_data_length,
1429 ciphertext, ciphertext_length,
1430 plaintext, plaintext_size, plaintext_length ) );
1431
1432 /* Add cases for opaque driver here */
1433
1434 default:
1435 /* Key is declared with a lifetime not known to us */
1436 (void)status;
1437 return( PSA_ERROR_INVALID_ARGUMENT );
1438 }
1439}
Steven Cooreman896d51e2021-03-19 15:24:23 +01001440
1441
1442/*
1443 * MAC functions
1444 */
1445psa_status_t psa_driver_wrapper_mac_compute(
1446 const psa_key_attributes_t *attributes,
1447 const uint8_t *key_buffer,
1448 size_t key_buffer_size,
1449 psa_algorithm_t alg,
1450 const uint8_t *input,
1451 size_t input_length,
1452 uint8_t *mac,
1453 size_t mac_size,
1454 size_t *mac_length )
1455{
1456 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1457 psa_key_location_t location =
1458 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1459
1460 switch( location )
1461 {
1462 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1463 /* Key is stored in the slot in export representation, so
1464 * cycle through all known transparent accelerators */
1465#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1466#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001467 status = mbedtls_test_transparent_mac_compute(
Steven Cooreman896d51e2021-03-19 15:24:23 +01001468 attributes, key_buffer, key_buffer_size, alg,
1469 input, input_length,
1470 mac, mac_size, mac_length );
1471 /* Declared with fallback == true */
1472 if( status != PSA_ERROR_NOT_SUPPORTED )
1473 return( status );
1474#endif /* PSA_CRYPTO_DRIVER_TEST */
1475#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1476#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1477 /* Fell through, meaning no accelerator supports this operation */
1478 status = mbedtls_psa_mac_compute(
1479 attributes, key_buffer, key_buffer_size, alg,
1480 input, input_length,
1481 mac, mac_size, mac_length );
1482 if( status != PSA_ERROR_NOT_SUPPORTED )
1483 return( status );
1484#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1485 return( PSA_ERROR_NOT_SUPPORTED );
1486
1487 /* Add cases for opaque driver here */
1488#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1489#if defined(PSA_CRYPTO_DRIVER_TEST)
1490 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001491 status = mbedtls_test_opaque_mac_compute(
Steven Cooreman896d51e2021-03-19 15:24:23 +01001492 attributes, key_buffer, key_buffer_size, alg,
1493 input, input_length,
1494 mac, mac_size, mac_length );
1495 return( status );
1496#endif /* PSA_CRYPTO_DRIVER_TEST */
1497#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1498 default:
1499 /* Key is declared with a lifetime not known to us */
1500 (void) key_buffer;
1501 (void) key_buffer_size;
1502 (void) alg;
1503 (void) input;
1504 (void) input_length;
1505 (void) mac;
1506 (void) mac_size;
1507 (void) mac_length;
1508 (void) status;
1509 return( PSA_ERROR_INVALID_ARGUMENT );
1510 }
1511}
1512
1513psa_status_t psa_driver_wrapper_mac_sign_setup(
1514 psa_mac_operation_t *operation,
1515 const psa_key_attributes_t *attributes,
1516 const uint8_t *key_buffer,
1517 size_t key_buffer_size,
1518 psa_algorithm_t alg )
1519{
1520 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1521 psa_key_location_t location =
1522 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1523
1524 switch( location )
1525 {
1526 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1527 /* Key is stored in the slot in export representation, so
1528 * cycle through all known transparent accelerators */
1529#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1530#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001531 status = mbedtls_test_transparent_mac_sign_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001532 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001533 attributes,
1534 key_buffer, key_buffer_size,
1535 alg );
1536 /* Declared with fallback == true */
1537 if( status == PSA_SUCCESS )
1538 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1539
1540 if( status != PSA_ERROR_NOT_SUPPORTED )
1541 return( status );
1542#endif /* PSA_CRYPTO_DRIVER_TEST */
1543#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1544#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1545 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001546 status = mbedtls_psa_mac_sign_setup( &operation->ctx.mbedtls_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001547 attributes,
1548 key_buffer, key_buffer_size,
1549 alg );
1550 if( status == PSA_SUCCESS )
1551 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1552
1553 if( status != PSA_ERROR_NOT_SUPPORTED )
1554 return( status );
1555#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1556 return( PSA_ERROR_NOT_SUPPORTED );
1557
1558 /* Add cases for opaque driver here */
1559#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1560#if defined(PSA_CRYPTO_DRIVER_TEST)
1561 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001562 status = mbedtls_test_opaque_mac_sign_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001563 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001564 attributes,
1565 key_buffer, key_buffer_size,
1566 alg );
1567
1568 if( status == PSA_SUCCESS )
1569 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
1570
1571 return( status );
1572#endif /* PSA_CRYPTO_DRIVER_TEST */
1573#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1574 default:
1575 /* Key is declared with a lifetime not known to us */
1576 (void) status;
1577 (void) key_buffer;
1578 (void) key_buffer_size;
1579 (void) alg;
1580 return( PSA_ERROR_INVALID_ARGUMENT );
1581 }
1582}
1583
1584psa_status_t psa_driver_wrapper_mac_verify_setup(
1585 psa_mac_operation_t *operation,
1586 const psa_key_attributes_t *attributes,
1587 const uint8_t *key_buffer,
1588 size_t key_buffer_size,
1589 psa_algorithm_t alg )
1590{
1591 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1592 psa_key_location_t location =
1593 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1594
1595 switch( location )
1596 {
1597 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1598 /* Key is stored in the slot in export representation, so
1599 * cycle through all known transparent accelerators */
1600#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1601#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001602 status = mbedtls_test_transparent_mac_verify_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001603 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001604 attributes,
1605 key_buffer, key_buffer_size,
1606 alg );
1607 /* Declared with fallback == true */
1608 if( status == PSA_SUCCESS )
1609 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1610
1611 if( status != PSA_ERROR_NOT_SUPPORTED )
1612 return( status );
1613#endif /* PSA_CRYPTO_DRIVER_TEST */
1614#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1615#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1616 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001617 status = mbedtls_psa_mac_verify_setup( &operation->ctx.mbedtls_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001618 attributes,
1619 key_buffer, key_buffer_size,
1620 alg );
1621 if( status == PSA_SUCCESS )
1622 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1623
1624 if( status != PSA_ERROR_NOT_SUPPORTED )
1625 return( status );
1626#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1627 return( PSA_ERROR_NOT_SUPPORTED );
1628
1629 /* Add cases for opaque driver here */
1630#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1631#if defined(PSA_CRYPTO_DRIVER_TEST)
1632 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001633 status = mbedtls_test_opaque_mac_verify_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001634 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001635 attributes,
1636 key_buffer, key_buffer_size,
1637 alg );
1638
1639 if( status == PSA_SUCCESS )
1640 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
1641
1642 return( status );
1643#endif /* PSA_CRYPTO_DRIVER_TEST */
1644#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1645 default:
1646 /* Key is declared with a lifetime not known to us */
1647 (void) status;
1648 (void) key_buffer;
1649 (void) key_buffer_size;
1650 (void) alg;
1651 return( PSA_ERROR_INVALID_ARGUMENT );
1652 }
1653}
1654
1655psa_status_t psa_driver_wrapper_mac_update(
1656 psa_mac_operation_t *operation,
1657 const uint8_t *input,
1658 size_t input_length )
1659{
1660 switch( operation->id )
1661 {
1662#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1663 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001664 return( mbedtls_psa_mac_update( &operation->ctx.mbedtls_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001665 input, input_length ) );
1666#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1667
1668#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1669#if defined(PSA_CRYPTO_DRIVER_TEST)
1670 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001671 return( mbedtls_test_transparent_mac_update(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001672 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001673 input, input_length ) );
1674
1675 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001676 return( mbedtls_test_opaque_mac_update(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001677 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001678 input, input_length ) );
1679#endif /* PSA_CRYPTO_DRIVER_TEST */
1680#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1681 default:
1682 (void) input;
1683 (void) input_length;
1684 return( PSA_ERROR_INVALID_ARGUMENT );
1685 }
1686}
1687
1688psa_status_t psa_driver_wrapper_mac_sign_finish(
1689 psa_mac_operation_t *operation,
1690 uint8_t *mac,
1691 size_t mac_size,
1692 size_t *mac_length )
1693{
1694 switch( operation->id )
1695 {
1696#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1697 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001698 return( mbedtls_psa_mac_sign_finish( &operation->ctx.mbedtls_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001699 mac, mac_size, mac_length ) );
1700#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1701
1702#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1703#if defined(PSA_CRYPTO_DRIVER_TEST)
1704 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001705 return( mbedtls_test_transparent_mac_sign_finish(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001706 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001707 mac, mac_size, mac_length ) );
1708
1709 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001710 return( mbedtls_test_opaque_mac_sign_finish(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001711 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001712 mac, mac_size, mac_length ) );
1713#endif /* PSA_CRYPTO_DRIVER_TEST */
1714#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1715 default:
1716 (void) mac;
1717 (void) mac_size;
1718 (void) mac_length;
1719 return( PSA_ERROR_INVALID_ARGUMENT );
1720 }
1721}
1722
1723psa_status_t psa_driver_wrapper_mac_verify_finish(
1724 psa_mac_operation_t *operation,
1725 const uint8_t *mac,
1726 size_t mac_length )
1727{
1728 switch( operation->id )
1729 {
1730#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1731 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001732 return( mbedtls_psa_mac_verify_finish( &operation->ctx.mbedtls_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001733 mac, mac_length ) );
1734#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1735
1736#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1737#if defined(PSA_CRYPTO_DRIVER_TEST)
1738 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001739 return( mbedtls_test_transparent_mac_verify_finish(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001740 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001741 mac, mac_length ) );
1742
1743 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001744 return( mbedtls_test_opaque_mac_verify_finish(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001745 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001746 mac, mac_length ) );
1747#endif /* PSA_CRYPTO_DRIVER_TEST */
1748#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1749 default:
1750 (void) mac;
1751 (void) mac_length;
1752 return( PSA_ERROR_INVALID_ARGUMENT );
1753 }
1754}
1755
1756psa_status_t psa_driver_wrapper_mac_abort(
1757 psa_mac_operation_t *operation )
1758{
Steven Cooreman896d51e2021-03-19 15:24:23 +01001759 switch( operation->id )
1760 {
1761#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1762 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman07897832021-03-19 18:28:56 +01001763 return( mbedtls_psa_mac_abort( &operation->ctx.mbedtls_ctx ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +01001764#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1765
1766#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1767#if defined(PSA_CRYPTO_DRIVER_TEST)
1768 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001769 return( mbedtls_test_transparent_mac_abort(
Steven Cooreman07897832021-03-19 18:28:56 +01001770 &operation->ctx.transparent_test_driver_ctx ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +01001771 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001772 return( mbedtls_test_opaque_mac_abort(
Steven Cooreman07897832021-03-19 18:28:56 +01001773 &operation->ctx.opaque_test_driver_ctx ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +01001774#endif /* PSA_CRYPTO_DRIVER_TEST */
1775#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1776 default:
Steven Cooreman07897832021-03-19 18:28:56 +01001777 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman896d51e2021-03-19 15:24:23 +01001778 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001779}
Steven Cooremancd84cb42020-07-16 20:28:36 +02001780/* End of automatically generated file. */