blob: 48ad6d5fbba007b394399903dbe98ba16298e60e [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
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010069psa_status_t psa_driver_wrapper_init(void)
Ronald Cron088d5d02021-04-10 16:57:30 +020070{
71 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
72
73#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 status = psa_init_all_se_drivers();
75 if (status != PSA_SUCCESS) {
76 return status;
77 }
Ronald Cron088d5d02021-04-10 16:57:30 +020078#endif
79
80#if defined(PSA_CRYPTO_DRIVER_TEST)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010081 status = mbedtls_test_transparent_init();
82 if (status != PSA_SUCCESS) {
83 return status;
84 }
Ronald Cron088d5d02021-04-10 16:57:30 +020085
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 status = mbedtls_test_opaque_init();
87 if (status != PSA_SUCCESS) {
88 return status;
89 }
Ronald Cron088d5d02021-04-10 16:57:30 +020090#endif
91
92 (void) status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093 return PSA_SUCCESS;
Ronald Cron088d5d02021-04-10 16:57:30 +020094}
95
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096void psa_driver_wrapper_free(void)
Ronald Cron088d5d02021-04-10 16:57:30 +020097{
98#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
99 /* Unregister all secure element drivers, so that we restart from
100 * a pristine state. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 psa_unregister_all_se_drivers();
Ronald Cron088d5d02021-04-10 16:57:30 +0200102#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
103
104#if defined(PSA_CRYPTO_DRIVER_TEST)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 mbedtls_test_transparent_free();
106 mbedtls_test_opaque_free();
Ronald Cron088d5d02021-04-10 16:57:30 +0200107#endif
108}
109
Steven Cooremancd84cb42020-07-16 20:28:36 +0200110/* Start delegation functions */
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200111psa_status_t psa_driver_wrapper_sign_message(
112 const psa_key_attributes_t *attributes,
113 const uint8_t *key_buffer,
114 size_t key_buffer_size,
115 psa_algorithm_t alg,
116 const uint8_t *input,
117 size_t input_length,
118 uint8_t *signature,
119 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200121{
122 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200123 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 switch (location) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200127 case PSA_KEY_LOCATION_LOCAL_STORAGE:
128 /* Key is stored in the slot in export representation, so
129 * cycle through all known transparent accelerators */
130#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
131#if defined(PSA_CRYPTO_DRIVER_TEST)
132 status = mbedtls_test_transparent_signature_sign_message(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133 attributes,
134 key_buffer,
135 key_buffer_size,
136 alg,
137 input,
138 input_length,
139 signature,
140 signature_size,
141 signature_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200142 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 if (status != PSA_ERROR_NOT_SUPPORTED) {
144 return status;
145 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200146#endif /* PSA_CRYPTO_DRIVER_TEST */
147#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200148 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150 /* Add cases for opaque driver here */
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200151#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
152#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arm77588fb2021-04-27 18:52:51 +0200153 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200154 status = mbedtls_test_opaque_signature_sign_message(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 attributes,
156 key_buffer,
157 key_buffer_size,
158 alg,
159 input,
160 input_length,
161 signature,
162 signature_size,
163 signature_length);
164 if (status != PSA_ERROR_NOT_SUPPORTED) {
165 return status;
166 }
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200167 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200168#endif /* PSA_CRYPTO_DRIVER_TEST */
169#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
170 default:
171 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172 (void) status;
gabor-mezei-armf25c9762021-05-12 11:12:25 +0200173 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200174 }
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176 return psa_sign_message_builtin(attributes,
177 key_buffer,
178 key_buffer_size,
179 alg,
180 input,
181 input_length,
182 signature,
183 signature_size,
184 signature_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200185}
186
187psa_status_t psa_driver_wrapper_verify_message(
188 const psa_key_attributes_t *attributes,
189 const uint8_t *key_buffer,
190 size_t key_buffer_size,
191 psa_algorithm_t alg,
192 const uint8_t *input,
193 size_t input_length,
194 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200196{
197 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200198 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100201 switch (location) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200202 case PSA_KEY_LOCATION_LOCAL_STORAGE:
203 /* Key is stored in the slot in export representation, so
204 * cycle through all known transparent accelerators */
205#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
206#if defined(PSA_CRYPTO_DRIVER_TEST)
207 status = mbedtls_test_transparent_signature_verify_message(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208 attributes,
209 key_buffer,
210 key_buffer_size,
211 alg,
212 input,
213 input_length,
214 signature,
215 signature_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200216 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100217 if (status != PSA_ERROR_NOT_SUPPORTED) {
218 return status;
219 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200220#endif /* PSA_CRYPTO_DRIVER_TEST */
221#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200222 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200223
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224 /* Add cases for opaque driver here */
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200225#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
226#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arm77588fb2021-04-27 18:52:51 +0200227 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 return mbedtls_test_opaque_signature_verify_message(
229 attributes,
230 key_buffer,
231 key_buffer_size,
232 alg,
233 input,
234 input_length,
235 signature,
236 signature_length);
237 if (status != PSA_ERROR_NOT_SUPPORTED) {
238 return status;
239 }
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200240 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200241#endif /* PSA_CRYPTO_DRIVER_TEST */
242#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
243 default:
244 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 (void) status;
gabor-mezei-armf25c9762021-05-12 11:12:25 +0200246 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200247 }
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249 return psa_verify_message_builtin(attributes,
250 key_buffer,
251 key_buffer_size,
252 alg,
253 input,
254 input_length,
255 signature,
256 signature_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200257}
258
Ronald Cron9f17aa42020-12-08 17:07:25 +0100259psa_status_t psa_driver_wrapper_sign_hash(
260 const psa_key_attributes_t *attributes,
261 const uint8_t *key_buffer, size_t key_buffer_size,
262 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263 uint8_t *signature, size_t signature_size, size_t *signature_length)
Steven Cooremancd84cb42020-07-16 20:28:36 +0200264{
Steven Cooreman7a250572020-07-17 16:43:05 +0200265 /* Try dynamically-registered SE interface first */
266#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
267 const psa_drv_se_t *drv;
268 psa_drv_se_context_t *drv_context;
269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270 if (psa_get_se_driver(attributes->core.lifetime, &drv, &drv_context)) {
271 if (drv->asymmetric == NULL ||
272 drv->asymmetric->p_sign == NULL) {
Steven Cooreman7a250572020-07-17 16:43:05 +0200273 /* Key is defined in SE, but we have no way to exercise it */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman7a250572020-07-17 16:43:05 +0200275 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 return drv->asymmetric->p_sign(
277 drv_context, *((psa_key_slot_number_t *) key_buffer),
278 alg, hash, hash_length,
279 signature, signature_size, signature_length);
Steven Cooreman7a250572020-07-17 16:43:05 +0200280 }
Pengyu Lvf5131972022-11-08 18:17:00 +0800281#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooreman7a250572020-07-17 16:43:05 +0200282
Ronald Cronfce9df22020-12-08 18:06:03 +0100283 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100284 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Steven Cooremancd84cb42020-07-16 20:28:36 +0200286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 switch (location) {
Steven Cooremancd84cb42020-07-16 20:28:36 +0200288 case PSA_KEY_LOCATION_LOCAL_STORAGE:
289 /* Key is stored in the slot in export representation, so
290 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100291#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200292#if defined(PSA_CRYPTO_DRIVER_TEST)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 status = mbedtls_test_transparent_signature_sign_hash(attributes,
294 key_buffer,
295 key_buffer_size,
296 alg,
297 hash,
298 hash_length,
299 signature,
300 signature_size,
301 signature_length);
302 /* Declared with fallback == true */
303 if (status != PSA_ERROR_NOT_SUPPORTED) {
304 return status;
305 }
306#endif /* PSA_CRYPTO_DRIVER_TEST */
307#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
308 /* Fell through, meaning no accelerator supports this operation */
309 return psa_sign_hash_builtin(attributes,
310 key_buffer,
311 key_buffer_size,
312 alg,
313 hash,
314 hash_length,
315 signature,
316 signature_size,
317 signature_length);
318
319 /* Add cases for opaque driver here */
320#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
321#if defined(PSA_CRYPTO_DRIVER_TEST)
322 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
323 return mbedtls_test_opaque_signature_sign_hash(attributes,
Ronald Cron9f17aa42020-12-08 17:07:25 +0100324 key_buffer,
325 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200326 alg,
327 hash,
328 hash_length,
329 signature,
330 signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331 signature_length);
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200332#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100333#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200334 default:
335 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336 (void) status;
337 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremancd84cb42020-07-16 20:28:36 +0200338 }
Steven Cooremancd84cb42020-07-16 20:28:36 +0200339}
340
Ronald Cron9f17aa42020-12-08 17:07:25 +0100341psa_status_t psa_driver_wrapper_verify_hash(
342 const psa_key_attributes_t *attributes,
343 const uint8_t *key_buffer, size_t key_buffer_size,
344 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 const uint8_t *signature, size_t signature_length)
Steven Cooreman55ae2172020-07-17 19:46:15 +0200346{
Steven Cooreman55ae2172020-07-17 19:46:15 +0200347 /* Try dynamically-registered SE interface first */
348#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
349 const psa_drv_se_t *drv;
350 psa_drv_se_context_t *drv_context;
351
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100352 if (psa_get_se_driver(attributes->core.lifetime, &drv, &drv_context)) {
353 if (drv->asymmetric == NULL ||
354 drv->asymmetric->p_verify == NULL) {
Steven Cooreman55ae2172020-07-17 19:46:15 +0200355 /* Key is defined in SE, but we have no way to exercise it */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman55ae2172020-07-17 19:46:15 +0200357 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100358 return drv->asymmetric->p_verify(
359 drv_context, *((psa_key_slot_number_t *) key_buffer),
360 alg, hash, hash_length,
361 signature, signature_length);
Steven Cooreman55ae2172020-07-17 19:46:15 +0200362 }
Pengyu Lvf5131972022-11-08 18:17:00 +0800363#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200364
Ronald Cronfce9df22020-12-08 18:06:03 +0100365 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100366 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100367 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Steven Cooreman55ae2172020-07-17 19:46:15 +0200368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100369 switch (location) {
Steven Cooreman55ae2172020-07-17 19:46:15 +0200370 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(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 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 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 if (status != PSA_ERROR_NOT_SUPPORTED) {
386 return status;
387 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200388#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100389#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 return psa_verify_hash_builtin(attributes,
392 key_buffer,
393 key_buffer_size,
394 alg,
395 hash,
396 hash_length,
397 signature,
398 signature_length);
Ronald Cronfce9df22020-12-08 18:06:03 +0100399
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100400 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100401#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200402#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100403 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 return mbedtls_test_opaque_signature_verify_hash(attributes,
405 key_buffer,
406 key_buffer_size,
407 alg,
408 hash,
409 hash_length,
410 signature,
411 signature_length);
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200412#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100413#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200414 default:
415 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416 (void) status;
417 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman55ae2172020-07-17 19:46:15 +0200418 }
Steven Cooreman55ae2172020-07-17 19:46:15 +0200419}
420
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200421/** Get the key buffer size required to store the key material of a key
422 * associated with an opaque driver without storage.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200423 *
Ronald Cron31216282020-12-05 18:47:56 +0100424 * \param[in] attributes The key attributes.
425 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200426 *
427 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100428 * The minimum size for a buffer to contain the key material has been
429 * returned successfully.
430 * \retval #PSA_ERROR_INVALID_ARGUMENT
431 * The size in bits of the key is not valid.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200432 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100433 * The type and/or the size in bits of the key or the combination of
434 * the two is not supported.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200435 */
Ronald Cron9df74be2020-12-05 19:15:23 +0100436psa_status_t psa_driver_wrapper_get_key_buffer_size(
Ronald Cron31216282020-12-05 18:47:56 +0100437 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100438 size_t *key_buffer_size)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200439{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100440 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
John Durkop2c618352020-09-22 06:54:01 -0700441 psa_key_type_t key_type = attributes->core.type;
442 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200443
Ronald Cron31216282020-12-05 18:47:56 +0100444 *key_buffer_size = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445 switch (location) {
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' */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100450 if (psa_key_id_is_builtin(
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100451 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100452 psa_get_key_id(attributes)))) {
453 *key_buffer_size = sizeof(psa_drv_slot_number_t);
454 return PSA_SUCCESS;
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100455 }
456#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457 *key_buffer_size = mbedtls_test_size_function(key_type, key_bits);
458 return (*key_buffer_size != 0) ?
459 PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED;
John Durkop2c618352020-09-22 06:54:01 -0700460#endif /* PSA_CRYPTO_DRIVER_TEST */
461
462 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100463 (void) key_type;
464 (void) key_bits;
465 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200466 }
467}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200468
Ronald Cron977c2472020-10-13 08:32:21 +0200469psa_status_t psa_driver_wrapper_generate_key(
470 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100471 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Steven Cooreman55ae2172020-07-17 19:46:15 +0200472{
Ronald Cron977c2472020-10-13 08:32:21 +0200473 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
474 psa_key_location_t location =
475 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
476
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200477 /* Try dynamically-registered SE interface first */
478#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
479 const psa_drv_se_t *drv;
480 psa_drv_se_context_t *drv_context;
481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100482 if (psa_get_se_driver(attributes->core.lifetime, &drv, &drv_context)) {
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200483 size_t pubkey_length = 0; /* We don't support this feature yet */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100484 if (drv->key_management == NULL ||
485 drv->key_management->p_generate == NULL) {
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200486 /* Key is defined as being in SE, but we have no way to generate it */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100487 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200488 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100489 return drv->key_management->p_generate(
Ronald Cron977c2472020-10-13 08:32:21 +0200490 drv_context,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100491 *((psa_key_slot_number_t *) key_buffer),
492 attributes, NULL, 0, &pubkey_length);
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200493 }
494#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
495
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100496 switch (location) {
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200497 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Ronald Cron977c2472020-10-13 08:32:21 +0200498#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200499 /* Transparent drivers are limited to generating asymmetric keys */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100500 if (PSA_KEY_TYPE_IS_ASYMMETRIC(attributes->core.type)) {
501 /* Cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200502#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200503 status = mbedtls_test_transparent_generate_key(
Ronald Cron977c2472020-10-13 08:32:21 +0200504 attributes, key_buffer, key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100505 key_buffer_length);
Ronald Cron977c2472020-10-13 08:32:21 +0200506 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100507 if (status != PSA_ERROR_NOT_SUPPORTED) {
Ronald Cron977c2472020-10-13 08:32:21 +0200508 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100509 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200510#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200511 }
512#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
513
514 /* Software fallback */
515 status = psa_generate_key_internal(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100516 attributes, key_buffer, key_buffer_size, key_buffer_length);
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200517 break;
Ronald Cron977c2472020-10-13 08:32:21 +0200518
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100519 /* Add cases for opaque driver here */
Ronald Cron977c2472020-10-13 08:32:21 +0200520#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200521#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100522 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200523 status = mbedtls_test_opaque_generate_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100524 attributes, key_buffer, key_buffer_size, key_buffer_length);
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200525 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200526#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200527#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
528
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200529 default:
530 /* Key is declared with a lifetime not known to us */
531 status = PSA_ERROR_INVALID_ARGUMENT;
532 break;
533 }
534
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100535 return status;
Steven Cooreman55ae2172020-07-17 19:46:15 +0200536}
537
Ronald Cron83282872020-11-22 14:02:39 +0100538psa_status_t psa_driver_wrapper_import_key(
539 const psa_key_attributes_t *attributes,
540 const uint8_t *data,
541 size_t data_length,
542 uint8_t *key_buffer,
543 size_t key_buffer_size,
544 size_t *key_buffer_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100545 size_t *bits)
Steven Cooreman04524762020-10-13 17:43:44 +0200546{
Steven Cooreman04524762020-10-13 17:43:44 +0200547 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100548 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100549 psa_get_key_lifetime(attributes));
Steven Cooreman04524762020-10-13 17:43:44 +0200550
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100551 /* Try dynamically-registered SE interface first */
552#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
553 const psa_drv_se_t *drv;
554 psa_drv_se_context_t *drv_context;
555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100556 if (psa_get_se_driver(attributes->core.lifetime, &drv, &drv_context)) {
557 if (drv->key_management == NULL ||
558 drv->key_management->p_import == NULL) {
559 return PSA_ERROR_NOT_SUPPORTED;
560 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100561
562 /* The driver should set the number of key bits, however in
563 * case it doesn't, we initialize bits to an invalid value. */
564 *bits = PSA_MAX_KEY_BITS + 1;
565 status = drv->key_management->p_import(
566 drv_context,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100567 *((psa_key_slot_number_t *) key_buffer),
568 attributes, data, data_length, bits);
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100570 if (status != PSA_SUCCESS) {
571 return status;
572 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100573
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100574 if ((*bits) > PSA_MAX_KEY_BITS) {
575 return PSA_ERROR_NOT_SUPPORTED;
576 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100577
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100578 return PSA_SUCCESS;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100579 }
Pengyu Lvf5131972022-11-08 18:17:00 +0800580#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100582 switch (location) {
Ronald Cronbf33c932020-11-28 18:06:53 +0100583 case PSA_KEY_LOCATION_LOCAL_STORAGE:
584 /* Key is stored in the slot in export representation, so
585 * cycle through all known transparent accelerators */
586#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
587#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200588 status = mbedtls_test_transparent_import_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100589 attributes,
590 data, data_length,
591 key_buffer, key_buffer_size,
592 key_buffer_length, bits);
Ronald Cronbf33c932020-11-28 18:06:53 +0100593 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100594 if (status != PSA_ERROR_NOT_SUPPORTED) {
595 return status;
596 }
Ronald Cronbf33c932020-11-28 18:06:53 +0100597#endif /* PSA_CRYPTO_DRIVER_TEST */
598#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
599 /* Fell through, meaning no accelerator supports this operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100600 return psa_import_key_into_slot(attributes,
601 data, data_length,
602 key_buffer, key_buffer_size,
603 key_buffer_length, bits);
Ronald Cronbf33c932020-11-28 18:06:53 +0100604
605 default:
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100606 /* Importing a key with external storage in not yet supported.
607 * Return in error indicating that the lifetime is not valid. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100608 (void) status;
609 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cronbf33c932020-11-28 18:06:53 +0100610 }
611
Steven Cooreman04524762020-10-13 17:43:44 +0200612}
613
Ronald Cron67227982020-11-26 15:16:05 +0100614psa_status_t psa_driver_wrapper_export_key(
615 const psa_key_attributes_t *attributes,
616 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100617 uint8_t *data, size_t data_size, size_t *data_length)
Ronald Cron67227982020-11-26 15:16:05 +0100618
619{
620 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
621 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100622 psa_get_key_lifetime(attributes));
Ronald Cron67227982020-11-26 15:16:05 +0100623
Ronald Cron152e3f82020-11-26 16:06:41 +0100624 /* Try dynamically-registered SE interface first */
625#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
626 const psa_drv_se_t *drv;
627 psa_drv_se_context_t *drv_context;
628
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100629 if (psa_get_se_driver(attributes->core.lifetime, &drv, &drv_context)) {
630 if ((drv->key_management == NULL) ||
631 (drv->key_management->p_export == NULL)) {
632 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100633 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100634
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100635 return drv->key_management->p_export(
636 drv_context,
637 *((psa_key_slot_number_t *) key_buffer),
638 data, data_size, data_length);
Ronald Cron152e3f82020-11-26 16:06:41 +0100639 }
Pengyu Lvf5131972022-11-08 18:17:00 +0800640#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Ronald Cron152e3f82020-11-26 16:06:41 +0100641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100642 switch (location) {
Ronald Cron67227982020-11-26 15:16:05 +0100643 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100644 return psa_export_key_internal(attributes,
645 key_buffer,
646 key_buffer_size,
647 data,
648 data_size,
649 data_length);
Ronald Cron67227982020-11-26 15:16:05 +0100650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100651 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100652#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
653#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100654 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100655 return mbedtls_test_opaque_export_key(attributes,
656 key_buffer,
657 key_buffer_size,
658 data,
659 data_size,
660 data_length);
Ronald Cron67227982020-11-26 15:16:05 +0100661#endif /* PSA_CRYPTO_DRIVER_TEST */
662#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
663 default:
664 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100665 return status;
Ronald Cron67227982020-11-26 15:16:05 +0100666 }
667}
668
Ronald Cron84cc9942020-11-25 14:30:05 +0100669psa_status_t psa_driver_wrapper_export_public_key(
670 const psa_key_attributes_t *attributes,
671 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100672 uint8_t *data, size_t data_size, size_t *data_length)
Ronald Cron84cc9942020-11-25 14:30:05 +0100673
Steven Cooremanb9b84422020-10-14 14:39:20 +0200674{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200675 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100676 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100677 psa_get_key_lifetime(attributes));
Steven Cooremanb9b84422020-10-14 14:39:20 +0200678
Ronald Cron152e3f82020-11-26 16:06:41 +0100679 /* Try dynamically-registered SE interface first */
680#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
681 const psa_drv_se_t *drv;
682 psa_drv_se_context_t *drv_context;
683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100684 if (psa_get_se_driver(attributes->core.lifetime, &drv, &drv_context)) {
685 if ((drv->key_management == NULL) ||
686 (drv->key_management->p_export_public == NULL)) {
687 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100688 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100690 return drv->key_management->p_export_public(
691 drv_context,
692 *((psa_key_slot_number_t *) key_buffer),
693 data, data_size, data_length);
Ronald Cron152e3f82020-11-26 16:06:41 +0100694 }
695#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
696
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100697 switch (location) {
Steven Cooremanb9b84422020-10-14 14:39:20 +0200698 case PSA_KEY_LOCATION_LOCAL_STORAGE:
699 /* Key is stored in the slot in export representation, so
700 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100701#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200702#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200703 status = mbedtls_test_transparent_export_public_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100704 attributes,
705 key_buffer,
706 key_buffer_size,
707 data,
708 data_size,
709 data_length);
Steven Cooremanb9b84422020-10-14 14:39:20 +0200710 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100711 if (status != PSA_ERROR_NOT_SUPPORTED) {
712 return status;
713 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200714#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100715#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200716 /* Fell through, meaning no accelerator supports this operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100717 return psa_export_public_key_internal(attributes,
718 key_buffer,
719 key_buffer_size,
720 data,
721 data_size,
722 data_length);
Ronald Cron67227982020-11-26 15:16:05 +0100723
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100724 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100725#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200726#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100727 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100728 return mbedtls_test_opaque_export_public_key(attributes,
729 key_buffer,
730 key_buffer_size,
731 data,
732 data_size,
733 data_length);
Steven Cooremanb9b84422020-10-14 14:39:20 +0200734#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100735#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200736 default:
737 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100738 return status;
Steven Cooremanb9b84422020-10-14 14:39:20 +0200739 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200740}
741
Steven Cooreman6801f082021-02-19 17:21:22 +0100742psa_status_t psa_driver_wrapper_get_builtin_key(
743 psa_drv_slot_number_t slot_number,
744 psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100745 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Steven Cooreman6801f082021-02-19 17:21:22 +0100746{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100747 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
748 switch (location) {
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100749#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100750 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100751 return mbedtls_test_opaque_get_builtin_key(
752 slot_number,
753 attributes,
754 key_buffer, key_buffer_size, key_buffer_length);
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100755#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman6801f082021-02-19 17:21:22 +0100756 default:
757 (void) slot_number;
758 (void) key_buffer;
759 (void) key_buffer_size;
760 (void) key_buffer_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100761 return PSA_ERROR_DOES_NOT_EXIST;
Steven Cooreman6801f082021-02-19 17:21:22 +0100762 }
763}
764
Steven Cooreman37941cb2020-07-28 18:49:51 +0200765/*
766 * Cipher functions
767 */
768psa_status_t psa_driver_wrapper_cipher_encrypt(
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100769 const psa_key_attributes_t *attributes,
770 const uint8_t *key_buffer,
771 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200772 psa_algorithm_t alg,
Ronald Crona8331692021-07-09 09:19:35 +0200773 const uint8_t *iv,
774 size_t iv_length,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200775 const uint8_t *input,
776 size_t input_length,
777 uint8_t *output,
778 size_t output_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100779 size_t *output_length)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200780{
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100781 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
782 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100783 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200784
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100785 switch (location) {
Steven Cooreman37941cb2020-07-28 18:49:51 +0200786 case PSA_KEY_LOCATION_LOCAL_STORAGE:
787 /* Key is stored in the slot in export representation, so
788 * cycle through all known transparent accelerators */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100789#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200790#if defined(PSA_CRYPTO_DRIVER_TEST)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100791 status = mbedtls_test_transparent_cipher_encrypt(attributes,
792 key_buffer,
793 key_buffer_size,
794 alg,
795 iv,
796 iv_length,
797 input,
798 input_length,
799 output,
800 output_size,
801 output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200802 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100803 if (status != PSA_ERROR_NOT_SUPPORTED) {
804 return status;
805 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200806#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100807#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
808
809#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100810 return mbedtls_psa_cipher_encrypt(attributes,
811 key_buffer,
812 key_buffer_size,
813 alg,
814 iv,
815 iv_length,
816 input,
817 input_length,
818 output,
819 output_size,
820 output_length);
gabor-mezei-armd52c8dc2021-06-25 15:43:32 +0200821#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100822 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armd52c8dc2021-06-25 15:43:32 +0200823#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100824
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100825 /* Add cases for opaque driver here */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100826#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200827#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100828 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100829 return mbedtls_test_opaque_cipher_encrypt(attributes,
830 key_buffer,
831 key_buffer_size,
832 alg,
833 iv,
834 iv_length,
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 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100845 (void) status;
846 (void) key_buffer;
847 (void) key_buffer_size;
848 (void) alg;
849 (void) iv;
850 (void) iv_length;
851 (void) input;
852 (void) input_length;
853 (void) output;
854 (void) output_size;
855 (void) output_length;
856 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200857 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200858}
859
860psa_status_t psa_driver_wrapper_cipher_decrypt(
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100861 const psa_key_attributes_t *attributes,
862 const uint8_t *key_buffer,
863 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200864 psa_algorithm_t alg,
865 const uint8_t *input,
866 size_t input_length,
867 uint8_t *output,
868 size_t output_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100869 size_t *output_length)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200870{
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100871 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
872 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100873 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200874
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100875 switch (location) {
Steven Cooreman37941cb2020-07-28 18:49:51 +0200876 case PSA_KEY_LOCATION_LOCAL_STORAGE:
877 /* Key is stored in the slot in export representation, so
878 * cycle through all known transparent accelerators */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100879#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200880#if defined(PSA_CRYPTO_DRIVER_TEST)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100881 status = mbedtls_test_transparent_cipher_decrypt(attributes,
882 key_buffer,
883 key_buffer_size,
884 alg,
885 input,
886 input_length,
887 output,
888 output_size,
889 output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200890 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100891 if (status != PSA_ERROR_NOT_SUPPORTED) {
892 return status;
893 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200894#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100895#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
896
897#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100898 return mbedtls_psa_cipher_decrypt(attributes,
899 key_buffer,
900 key_buffer_size,
901 alg,
902 input,
903 input_length,
904 output,
905 output_size,
906 output_length);
gabor-mezei-armd52c8dc2021-06-25 15:43:32 +0200907#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100908 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armd52c8dc2021-06-25 15:43:32 +0200909#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100910
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100911 /* Add cases for opaque driver here */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100912#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200913#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100914 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100915 return mbedtls_test_opaque_cipher_decrypt(attributes,
916 key_buffer,
917 key_buffer_size,
918 alg,
919 input,
920 input_length,
921 output,
922 output_size,
923 output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200924#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100925#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
926
Steven Cooreman37941cb2020-07-28 18:49:51 +0200927 default:
928 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100929 (void) status;
930 (void) key_buffer;
931 (void) key_buffer_size;
932 (void) alg;
933 (void) input;
934 (void) input_length;
935 (void) output;
936 (void) output_size;
937 (void) output_length;
938 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_encrypt_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,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100946 psa_algorithm_t alg)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200947{
Ronald Cron0b805592020-12-14 18:08:20 +0100948 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona4af55f2020-12-14 14:36:06 +0100949 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100950 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100952 switch (location) {
Steven Cooreman37941cb2020-07-28 18:49:51 +0200953 case PSA_KEY_LOCATION_LOCAL_STORAGE:
954 /* Key is stored in the slot in export representation, so
955 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100956#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200957#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200958 status = mbedtls_test_transparent_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100959 &operation->ctx.transparent_test_driver_ctx,
960 attributes,
961 key_buffer,
962 key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100963 alg);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200964 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100965 if (status == PSA_SUCCESS) {
Ronald Cron49fafa92021-03-10 08:34:23 +0100966 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100967 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200968
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100969 if (status != PSA_ERROR_NOT_SUPPORTED) {
970 return status;
971 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200972#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100973#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100974#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200975 /* Fell through, meaning no accelerator supports this operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100976 status = mbedtls_psa_cipher_encrypt_setup(&operation->ctx.mbedtls_ctx,
977 attributes,
978 key_buffer,
979 key_buffer_size,
980 alg);
981 if (status == PSA_SUCCESS) {
Ronald Cron6e412a72021-03-10 09:58:47 +0100982 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100983 }
Ronald Cron49fafa92021-03-10 08:34:23 +0100984
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100985 if (status != PSA_ERROR_NOT_SUPPORTED) {
986 return status;
987 }
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100988#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100989 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron0b805592020-12-14 18:08:20 +0100990
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100991 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100992#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200993#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100994 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200995 status = mbedtls_test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100996 &operation->ctx.opaque_test_driver_ctx,
997 attributes,
998 key_buffer, key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100999 alg);
Steven Cooreman37941cb2020-07-28 18:49:51 +02001000
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001001 if (status == PSA_SUCCESS) {
Ronald Cron49fafa92021-03-10 08:34:23 +01001002 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001003 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001004
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001005 return status;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001006#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001007#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +02001008 default:
1009 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001010 (void) status;
1011 (void) operation;
1012 (void) key_buffer;
1013 (void) key_buffer_size;
1014 (void) alg;
1015 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001016 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001017}
1018
1019psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +01001020 psa_cipher_operation_t *operation,
1021 const psa_key_attributes_t *attributes,
1022 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001023 psa_algorithm_t alg)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001024{
Steven Cooreman37941cb2020-07-28 18:49:51 +02001025 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crona4af55f2020-12-14 14:36:06 +01001026 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001027 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Steven Cooreman37941cb2020-07-28 18:49:51 +02001028
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001029 switch (location) {
Steven Cooreman37941cb2020-07-28 18:49:51 +02001030 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1031 /* Key is stored in the slot in export representation, so
1032 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +01001033#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001034#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001035 status = mbedtls_test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001036 &operation->ctx.transparent_test_driver_ctx,
1037 attributes,
1038 key_buffer,
1039 key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001040 alg);
Steven Cooreman37941cb2020-07-28 18:49:51 +02001041 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001042 if (status == PSA_SUCCESS) {
Ronald Cron49fafa92021-03-10 08:34:23 +01001043 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001044 }
Steven Cooreman150c99b2020-09-09 14:32:44 +02001045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001046 if (status != PSA_ERROR_NOT_SUPPORTED) {
1047 return status;
1048 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001049#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001050#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001051#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001052 /* Fell through, meaning no accelerator supports this operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001053 status = mbedtls_psa_cipher_decrypt_setup(&operation->ctx.mbedtls_ctx,
1054 attributes,
1055 key_buffer,
1056 key_buffer_size,
1057 alg);
1058 if (status == PSA_SUCCESS) {
Ronald Cron49fafa92021-03-10 08:34:23 +01001059 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001060 }
Ronald Cron49fafa92021-03-10 08:34:23 +01001061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001062 return status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001063#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001064 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron49fafa92021-03-10 08:34:23 +01001065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001066 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +01001067#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001068#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +01001069 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001070 status = mbedtls_test_opaque_cipher_decrypt_setup(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001071 &operation->ctx.opaque_test_driver_ctx,
1072 attributes,
1073 key_buffer, key_buffer_size,
1074 alg);
Steven Cooreman37941cb2020-07-28 18:49:51 +02001075
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001076 if (status == PSA_SUCCESS) {
Ronald Cron49fafa92021-03-10 08:34:23 +01001077 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001078 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001080 return status;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001081#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001082#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +02001083 default:
1084 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001085 (void) status;
1086 (void) operation;
1087 (void) key_buffer;
1088 (void) key_buffer_size;
1089 (void) alg;
1090 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001091 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001092}
1093
Steven Cooreman37941cb2020-07-28 18:49:51 +02001094psa_status_t psa_driver_wrapper_cipher_set_iv(
Ronald Cron6056fe82020-12-15 13:58:07 +01001095 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001096 const uint8_t *iv,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001097 size_t iv_length)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001098{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001099 switch (operation->id) {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001100#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001101 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001102 return mbedtls_psa_cipher_set_iv(&operation->ctx.mbedtls_ctx,
1103 iv,
1104 iv_length);
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001105#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001106
1107#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001108#if defined(PSA_CRYPTO_DRIVER_TEST)
1109 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001110 return mbedtls_test_transparent_cipher_set_iv(
1111 &operation->ctx.transparent_test_driver_ctx,
1112 iv, iv_length);
Ronald Cron49fafa92021-03-10 08:34:23 +01001113
Steven Cooreman37941cb2020-07-28 18:49:51 +02001114 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001115 return mbedtls_test_opaque_cipher_set_iv(
1116 &operation->ctx.opaque_test_driver_ctx,
1117 iv, iv_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +02001118#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001119#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001120 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001122 (void) iv;
1123 (void) iv_length;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001125 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001126}
1127
1128psa_status_t psa_driver_wrapper_cipher_update(
Ronald Cron6056fe82020-12-15 13:58:07 +01001129 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001130 const uint8_t *input,
1131 size_t input_length,
1132 uint8_t *output,
1133 size_t output_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001134 size_t *output_length)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001135{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001136 switch (operation->id) {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001137#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001138 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001139 return mbedtls_psa_cipher_update(&operation->ctx.mbedtls_ctx,
1140 input,
1141 input_length,
1142 output,
1143 output_size,
1144 output_length);
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001145#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1146
Ronald Cron49fafa92021-03-10 08:34:23 +01001147#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001148#if defined(PSA_CRYPTO_DRIVER_TEST)
1149 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001150 return mbedtls_test_transparent_cipher_update(
1151 &operation->ctx.transparent_test_driver_ctx,
1152 input, input_length,
1153 output, output_size, output_length);
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001154
Steven Cooreman37941cb2020-07-28 18:49:51 +02001155 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001156 return mbedtls_test_opaque_cipher_update(
1157 &operation->ctx.opaque_test_driver_ctx,
1158 input, input_length,
1159 output, output_size, output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +02001160#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001161#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001162 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001164 (void) input;
1165 (void) input_length;
1166 (void) output;
1167 (void) output_size;
1168 (void) output_length;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001170 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001171}
1172
1173psa_status_t psa_driver_wrapper_cipher_finish(
Ronald Cron6056fe82020-12-15 13:58:07 +01001174 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001175 uint8_t *output,
1176 size_t output_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001177 size_t *output_length)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001178{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001179 switch (operation->id) {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001180#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001181 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001182 return mbedtls_psa_cipher_finish(&operation->ctx.mbedtls_ctx,
1183 output,
1184 output_size,
1185 output_length);
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001186#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001187
1188#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001189#if defined(PSA_CRYPTO_DRIVER_TEST)
1190 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001191 return mbedtls_test_transparent_cipher_finish(
1192 &operation->ctx.transparent_test_driver_ctx,
1193 output, output_size, output_length);
Ronald Cron49fafa92021-03-10 08:34:23 +01001194
Steven Cooreman37941cb2020-07-28 18:49:51 +02001195 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001196 return mbedtls_test_opaque_cipher_finish(
1197 &operation->ctx.opaque_test_driver_ctx,
1198 output, output_size, output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +02001199#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001200#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001201 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001203 (void) output;
1204 (void) output_size;
1205 (void) output_length;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001207 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001208}
1209
1210psa_status_t psa_driver_wrapper_cipher_abort(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001211 psa_cipher_operation_t *operation)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001212{
Ronald Crondd24c9b2020-12-15 14:10:01 +01001213 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001215 switch (operation->id) {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001216#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001217 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001218 return mbedtls_psa_cipher_abort(&operation->ctx.mbedtls_ctx);
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001219#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +01001220
1221#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001222#if defined(PSA_CRYPTO_DRIVER_TEST)
1223 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001224 status = mbedtls_test_transparent_cipher_abort(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001225 &operation->ctx.transparent_test_driver_ctx);
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001226 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001227 &operation->ctx.transparent_test_driver_ctx,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001228 sizeof(operation->ctx.transparent_test_driver_ctx));
1229 return status;
Ronald Cron49fafa92021-03-10 08:34:23 +01001230
Steven Cooreman37941cb2020-07-28 18:49:51 +02001231 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001232 status = mbedtls_test_opaque_cipher_abort(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001233 &operation->ctx.opaque_test_driver_ctx);
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001234 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001235 &operation->ctx.opaque_test_driver_ctx,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001236 sizeof(operation->ctx.opaque_test_driver_ctx));
1237 return status;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001238#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001239#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001240 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001241
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001242 (void) status;
1243 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001244}
1245
Steven Cooreman1e582352021-02-18 17:24:37 +01001246/*
1247 * Hashing functions
1248 */
1249psa_status_t psa_driver_wrapper_hash_compute(
1250 psa_algorithm_t alg,
1251 const uint8_t *input,
1252 size_t input_length,
1253 uint8_t *hash,
1254 size_t hash_size,
1255 size_t *hash_length)
1256{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001257 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001258
1259 /* Try accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001260#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001261 status = mbedtls_test_transparent_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001262 alg, input, input_length, hash, hash_size, hash_length);
1263 if (status != PSA_ERROR_NOT_SUPPORTED) {
1264 return status;
1265 }
Steven Cooremanf7638102021-03-04 15:14:36 +01001266#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001267
1268 /* If software fallback is compiled in, try fallback */
1269#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001270 status = mbedtls_psa_hash_compute(alg, input, input_length,
1271 hash, hash_size, hash_length);
1272 if (status != PSA_ERROR_NOT_SUPPORTED) {
1273 return status;
1274 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001275#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001276 (void) status;
1277 (void) alg;
1278 (void) input;
1279 (void) input_length;
1280 (void) hash;
1281 (void) hash_size;
1282 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001283
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001284 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001285}
1286
1287psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001288 psa_hash_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001289 psa_algorithm_t alg)
Steven Cooreman1e582352021-02-18 17:24:37 +01001290{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001291 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001292
Steven Cooreman1e582352021-02-18 17:24:37 +01001293 /* Try setup on accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001294#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001295 status = mbedtls_test_transparent_hash_setup(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001296 &operation->ctx.test_driver_ctx, alg);
1297 if (status == PSA_SUCCESS) {
Steven Cooremanf7638102021-03-04 15:14:36 +01001298 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001299 }
Steven Cooremanf7638102021-03-04 15:14:36 +01001300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001301 if (status != PSA_ERROR_NOT_SUPPORTED) {
1302 return status;
1303 }
Steven Cooremanf7638102021-03-04 15:14:36 +01001304#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001305
1306 /* If software fallback is compiled in, try fallback */
1307#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001308 status = mbedtls_psa_hash_setup(&operation->ctx.mbedtls_ctx, alg);
1309 if (status == PSA_SUCCESS) {
Steven Cooreman1e582352021-02-18 17:24:37 +01001310 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001311 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001313 if (status != PSA_ERROR_NOT_SUPPORTED) {
1314 return status;
1315 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001316#endif
1317 /* Nothing left to try if we fall through here */
1318 (void) status;
1319 (void) operation;
1320 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001321 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001322}
1323
1324psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001325 const psa_hash_operation_t *source_operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001326 psa_hash_operation_t *target_operation)
Steven Cooreman1e582352021-02-18 17:24:37 +01001327{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001328 switch (source_operation->id) {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001329#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1330 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1331 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001332 return mbedtls_psa_hash_clone(&source_operation->ctx.mbedtls_ctx,
1333 &target_operation->ctx.mbedtls_ctx);
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001334#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001335#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001336 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1337 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001338 return mbedtls_test_transparent_hash_clone(
1339 &source_operation->ctx.test_driver_ctx,
1340 &target_operation->ctx.test_driver_ctx);
Steven Cooremanf7638102021-03-04 15:14:36 +01001341#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001342 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001343 (void) target_operation;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001344 return PSA_ERROR_BAD_STATE;
Steven Cooreman1e582352021-02-18 17:24:37 +01001345 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001346}
1347
1348psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001349 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001350 const uint8_t *input,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001351 size_t input_length)
Steven Cooreman1e582352021-02-18 17:24:37 +01001352{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001353 switch (operation->id) {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001354#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1355 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001356 return mbedtls_psa_hash_update(&operation->ctx.mbedtls_ctx,
1357 input, input_length);
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001358#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001359#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001360 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001361 return mbedtls_test_transparent_hash_update(
1362 &operation->ctx.test_driver_ctx,
1363 input, input_length);
Steven Cooremanf7638102021-03-04 15:14:36 +01001364#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001365 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001366 (void) input;
1367 (void) input_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001368 return PSA_ERROR_BAD_STATE;
Steven Cooreman1e582352021-02-18 17:24:37 +01001369 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001370}
1371
1372psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001373 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001374 uint8_t *hash,
1375 size_t hash_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001376 size_t *hash_length)
Steven Cooreman1e582352021-02-18 17:24:37 +01001377{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001378 switch (operation->id) {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001379#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1380 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001381 return mbedtls_psa_hash_finish(&operation->ctx.mbedtls_ctx,
1382 hash, hash_size, hash_length);
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001383#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001384#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001385 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001386 return mbedtls_test_transparent_hash_finish(
1387 &operation->ctx.test_driver_ctx,
1388 hash, hash_size, hash_length);
Steven Cooremanf7638102021-03-04 15:14:36 +01001389#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001390 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001391 (void) hash;
1392 (void) hash_size;
1393 (void) hash_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001394 return PSA_ERROR_BAD_STATE;
Steven Cooreman1e582352021-02-18 17:24:37 +01001395 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001396}
1397
1398psa_status_t psa_driver_wrapper_hash_abort(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001399 psa_hash_operation_t *operation)
Steven Cooreman1e582352021-02-18 17:24:37 +01001400{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001401 switch (operation->id) {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001402#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1403 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001404 return mbedtls_psa_hash_abort(&operation->ctx.mbedtls_ctx);
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001405#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001406#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001407 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001408 return mbedtls_test_transparent_hash_abort(
1409 &operation->ctx.test_driver_ctx);
Steven Cooremanf7638102021-03-04 15:14:36 +01001410#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001411 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001412 return PSA_ERROR_BAD_STATE;
Steven Cooreman1e582352021-02-18 17:24:37 +01001413 }
1414}
1415
Ronald Cronde822812021-03-17 16:08:20 +01001416psa_status_t psa_driver_wrapper_aead_encrypt(
1417 const psa_key_attributes_t *attributes,
1418 const uint8_t *key_buffer, size_t key_buffer_size,
1419 psa_algorithm_t alg,
1420 const uint8_t *nonce, size_t nonce_length,
1421 const uint8_t *additional_data, size_t additional_data_length,
1422 const uint8_t *plaintext, size_t plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001423 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length)
Ronald Cronde822812021-03-17 16:08:20 +01001424{
1425 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1426 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001427 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Ronald Cronde822812021-03-17 16:08:20 +01001428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001429 switch (location) {
Ronald Cronde822812021-03-17 16:08:20 +01001430 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1431 /* Key is stored in the slot in export representation, so
1432 * cycle through all known transparent accelerators */
1433
1434#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1435#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001436 status = mbedtls_test_transparent_aead_encrypt(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001437 attributes, key_buffer, key_buffer_size,
1438 alg,
1439 nonce, nonce_length,
1440 additional_data, additional_data_length,
1441 plaintext, plaintext_length,
1442 ciphertext, ciphertext_size, ciphertext_length);
Ronald Cronde822812021-03-17 16:08:20 +01001443 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001444 if (status != PSA_ERROR_NOT_SUPPORTED) {
1445 return status;
1446 }
Ronald Cronde822812021-03-17 16:08:20 +01001447#endif /* PSA_CRYPTO_DRIVER_TEST */
1448#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1449
1450 /* Fell through, meaning no accelerator supports this operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001451 return mbedtls_psa_aead_encrypt(
1452 attributes, key_buffer, key_buffer_size,
1453 alg,
1454 nonce, nonce_length,
1455 additional_data, additional_data_length,
1456 plaintext, plaintext_length,
1457 ciphertext, ciphertext_size, ciphertext_length);
Ronald Cronde822812021-03-17 16:08:20 +01001458
1459 /* Add cases for opaque driver here */
1460
1461 default:
1462 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001463 (void) status;
1464 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cronde822812021-03-17 16:08:20 +01001465 }
1466}
1467
1468psa_status_t psa_driver_wrapper_aead_decrypt(
1469 const psa_key_attributes_t *attributes,
1470 const uint8_t *key_buffer, size_t key_buffer_size,
1471 psa_algorithm_t alg,
1472 const uint8_t *nonce, size_t nonce_length,
1473 const uint8_t *additional_data, size_t additional_data_length,
1474 const uint8_t *ciphertext, size_t ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001475 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length)
Ronald Cronde822812021-03-17 16:08:20 +01001476{
1477 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1478 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001479 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Ronald Cronde822812021-03-17 16:08:20 +01001480
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001481 switch (location) {
Ronald Cronde822812021-03-17 16:08:20 +01001482 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1483 /* Key is stored in the slot in export representation, so
1484 * cycle through all known transparent accelerators */
1485
1486#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1487#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001488 status = mbedtls_test_transparent_aead_decrypt(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001489 attributes, key_buffer, key_buffer_size,
1490 alg,
1491 nonce, nonce_length,
1492 additional_data, additional_data_length,
1493 ciphertext, ciphertext_length,
1494 plaintext, plaintext_size, plaintext_length);
Ronald Cronde822812021-03-17 16:08:20 +01001495 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001496 if (status != PSA_ERROR_NOT_SUPPORTED) {
1497 return status;
1498 }
Ronald Cronde822812021-03-17 16:08:20 +01001499#endif /* PSA_CRYPTO_DRIVER_TEST */
1500#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1501
1502 /* Fell through, meaning no accelerator supports this operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001503 return mbedtls_psa_aead_decrypt(
1504 attributes, key_buffer, key_buffer_size,
1505 alg,
1506 nonce, nonce_length,
1507 additional_data, additional_data_length,
1508 ciphertext, ciphertext_length,
1509 plaintext, plaintext_size, plaintext_length);
Ronald Cronde822812021-03-17 16:08:20 +01001510
1511 /* Add cases for opaque driver here */
1512
1513 default:
1514 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001515 (void) status;
1516 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cronde822812021-03-17 16:08:20 +01001517 }
1518}
Steven Cooreman896d51e2021-03-19 15:24:23 +01001519
1520
1521/*
1522 * MAC functions
1523 */
1524psa_status_t psa_driver_wrapper_mac_compute(
1525 const psa_key_attributes_t *attributes,
1526 const uint8_t *key_buffer,
1527 size_t key_buffer_size,
1528 psa_algorithm_t alg,
1529 const uint8_t *input,
1530 size_t input_length,
1531 uint8_t *mac,
1532 size_t mac_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001533 size_t *mac_length)
Steven Cooreman896d51e2021-03-19 15:24:23 +01001534{
1535 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1536 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001537 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001538
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001539 switch (location) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001540 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1541 /* Key is stored in the slot in export representation, so
1542 * cycle through all known transparent accelerators */
1543#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1544#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001545 status = mbedtls_test_transparent_mac_compute(
Steven Cooreman896d51e2021-03-19 15:24:23 +01001546 attributes, key_buffer, key_buffer_size, alg,
1547 input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001548 mac, mac_size, mac_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001549 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001550 if (status != PSA_ERROR_NOT_SUPPORTED) {
1551 return status;
1552 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001553#endif /* PSA_CRYPTO_DRIVER_TEST */
1554#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1555#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1556 /* Fell through, meaning no accelerator supports this operation */
1557 status = mbedtls_psa_mac_compute(
1558 attributes, key_buffer, key_buffer_size, alg,
1559 input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001560 mac, mac_size, mac_length);
1561 if (status != PSA_ERROR_NOT_SUPPORTED) {
1562 return status;
1563 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001564#endif /* MBEDTLS_PSA_BUILTIN_MAC */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001565 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001566
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001567 /* Add cases for opaque driver here */
Steven Cooreman896d51e2021-03-19 15:24:23 +01001568#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1569#if defined(PSA_CRYPTO_DRIVER_TEST)
1570 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001571 status = mbedtls_test_opaque_mac_compute(
Steven Cooreman896d51e2021-03-19 15:24:23 +01001572 attributes, key_buffer, key_buffer_size, alg,
1573 input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001574 mac, mac_size, mac_length);
1575 return status;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001576#endif /* PSA_CRYPTO_DRIVER_TEST */
1577#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1578 default:
1579 /* Key is declared with a lifetime not known to us */
1580 (void) key_buffer;
1581 (void) key_buffer_size;
1582 (void) alg;
1583 (void) input;
1584 (void) input_length;
1585 (void) mac;
1586 (void) mac_size;
1587 (void) mac_length;
1588 (void) status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001589 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001590 }
1591}
1592
1593psa_status_t psa_driver_wrapper_mac_sign_setup(
1594 psa_mac_operation_t *operation,
1595 const psa_key_attributes_t *attributes,
1596 const uint8_t *key_buffer,
1597 size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001598 psa_algorithm_t alg)
Steven Cooreman896d51e2021-03-19 15:24:23 +01001599{
1600 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1601 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001602 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001603
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001604 switch (location) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001605 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1606 /* Key is stored in the slot in export representation, so
1607 * cycle through all known transparent accelerators */
1608#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1609#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001610 status = mbedtls_test_transparent_mac_sign_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001611 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001612 attributes,
1613 key_buffer, key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001614 alg);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001615 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001616 if (status == PSA_SUCCESS) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001617 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001618 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001620 if (status != PSA_ERROR_NOT_SUPPORTED) {
1621 return status;
1622 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001623#endif /* PSA_CRYPTO_DRIVER_TEST */
1624#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1625#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1626 /* Fell through, meaning no accelerator supports this operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001627 status = mbedtls_psa_mac_sign_setup(&operation->ctx.mbedtls_ctx,
1628 attributes,
1629 key_buffer, key_buffer_size,
1630 alg);
1631 if (status == PSA_SUCCESS) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001632 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001633 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001634
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001635 if (status != PSA_ERROR_NOT_SUPPORTED) {
1636 return status;
1637 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001638#endif /* MBEDTLS_PSA_BUILTIN_MAC */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001639 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001640
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001641 /* Add cases for opaque driver here */
Steven Cooreman896d51e2021-03-19 15:24:23 +01001642#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1643#if defined(PSA_CRYPTO_DRIVER_TEST)
1644 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001645 status = mbedtls_test_opaque_mac_sign_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001646 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001647 attributes,
1648 key_buffer, key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001649 alg);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001651 if (status == PSA_SUCCESS) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001652 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001653 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001655 return status;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001656#endif /* PSA_CRYPTO_DRIVER_TEST */
1657#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1658 default:
1659 /* Key is declared with a lifetime not known to us */
1660 (void) status;
Ronald Cron485559e2021-04-28 14:29:00 +02001661 (void) operation;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001662 (void) key_buffer;
1663 (void) key_buffer_size;
1664 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001665 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001666 }
1667}
1668
1669psa_status_t psa_driver_wrapper_mac_verify_setup(
1670 psa_mac_operation_t *operation,
1671 const psa_key_attributes_t *attributes,
1672 const uint8_t *key_buffer,
1673 size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001674 psa_algorithm_t alg)
Steven Cooreman896d51e2021-03-19 15:24:23 +01001675{
1676 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1677 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001678 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001679
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001680 switch (location) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001681 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1682 /* Key is stored in the slot in export representation, so
1683 * cycle through all known transparent accelerators */
1684#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1685#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001686 status = mbedtls_test_transparent_mac_verify_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001687 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001688 attributes,
1689 key_buffer, key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001690 alg);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001691 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001692 if (status == PSA_SUCCESS) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001693 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001694 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001695
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001696 if (status != PSA_ERROR_NOT_SUPPORTED) {
1697 return status;
1698 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001699#endif /* PSA_CRYPTO_DRIVER_TEST */
1700#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1701#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1702 /* Fell through, meaning no accelerator supports this operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001703 status = mbedtls_psa_mac_verify_setup(&operation->ctx.mbedtls_ctx,
1704 attributes,
1705 key_buffer, key_buffer_size,
1706 alg);
1707 if (status == PSA_SUCCESS) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001708 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001709 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001710
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001711 if (status != PSA_ERROR_NOT_SUPPORTED) {
1712 return status;
1713 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001714#endif /* MBEDTLS_PSA_BUILTIN_MAC */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001715 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001716
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001717 /* Add cases for opaque driver here */
Steven Cooreman896d51e2021-03-19 15:24:23 +01001718#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1719#if defined(PSA_CRYPTO_DRIVER_TEST)
1720 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001721 status = mbedtls_test_opaque_mac_verify_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001722 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001723 attributes,
1724 key_buffer, key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001725 alg);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001727 if (status == PSA_SUCCESS) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001728 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001729 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001730
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001731 return status;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001732#endif /* PSA_CRYPTO_DRIVER_TEST */
1733#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1734 default:
1735 /* Key is declared with a lifetime not known to us */
1736 (void) status;
Ronald Cron485559e2021-04-28 14:29:00 +02001737 (void) operation;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001738 (void) key_buffer;
1739 (void) key_buffer_size;
1740 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001741 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001742 }
1743}
1744
1745psa_status_t psa_driver_wrapper_mac_update(
1746 psa_mac_operation_t *operation,
1747 const uint8_t *input,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001748 size_t input_length)
Steven Cooreman896d51e2021-03-19 15:24:23 +01001749{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001750 switch (operation->id) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001751#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1752 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001753 return mbedtls_psa_mac_update(&operation->ctx.mbedtls_ctx,
1754 input, input_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001755#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1756
1757#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1758#if defined(PSA_CRYPTO_DRIVER_TEST)
1759 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001760 return mbedtls_test_transparent_mac_update(
1761 &operation->ctx.transparent_test_driver_ctx,
1762 input, input_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001763
1764 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001765 return mbedtls_test_opaque_mac_update(
1766 &operation->ctx.opaque_test_driver_ctx,
1767 input, input_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001768#endif /* PSA_CRYPTO_DRIVER_TEST */
1769#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1770 default:
1771 (void) input;
1772 (void) input_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001773 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001774 }
1775}
1776
1777psa_status_t psa_driver_wrapper_mac_sign_finish(
1778 psa_mac_operation_t *operation,
1779 uint8_t *mac,
1780 size_t mac_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001781 size_t *mac_length)
Steven Cooreman896d51e2021-03-19 15:24:23 +01001782{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001783 switch (operation->id) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001784#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1785 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001786 return mbedtls_psa_mac_sign_finish(&operation->ctx.mbedtls_ctx,
1787 mac, mac_size, mac_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001788#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1789
1790#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1791#if defined(PSA_CRYPTO_DRIVER_TEST)
1792 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001793 return mbedtls_test_transparent_mac_sign_finish(
1794 &operation->ctx.transparent_test_driver_ctx,
1795 mac, mac_size, mac_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001796
1797 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001798 return mbedtls_test_opaque_mac_sign_finish(
1799 &operation->ctx.opaque_test_driver_ctx,
1800 mac, mac_size, mac_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001801#endif /* PSA_CRYPTO_DRIVER_TEST */
1802#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1803 default:
1804 (void) mac;
1805 (void) mac_size;
1806 (void) mac_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001807 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001808 }
1809}
1810
1811psa_status_t psa_driver_wrapper_mac_verify_finish(
1812 psa_mac_operation_t *operation,
1813 const uint8_t *mac,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001814 size_t mac_length)
Steven Cooreman896d51e2021-03-19 15:24:23 +01001815{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001816 switch (operation->id) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001817#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1818 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001819 return mbedtls_psa_mac_verify_finish(&operation->ctx.mbedtls_ctx,
1820 mac, mac_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001821#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1822
1823#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1824#if defined(PSA_CRYPTO_DRIVER_TEST)
1825 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001826 return mbedtls_test_transparent_mac_verify_finish(
1827 &operation->ctx.transparent_test_driver_ctx,
1828 mac, mac_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001829
1830 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001831 return mbedtls_test_opaque_mac_verify_finish(
1832 &operation->ctx.opaque_test_driver_ctx,
1833 mac, mac_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001834#endif /* PSA_CRYPTO_DRIVER_TEST */
1835#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1836 default:
1837 (void) mac;
1838 (void) mac_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001839 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001840 }
1841}
1842
1843psa_status_t psa_driver_wrapper_mac_abort(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001844 psa_mac_operation_t *operation)
Steven Cooreman896d51e2021-03-19 15:24:23 +01001845{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001846 switch (operation->id) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001847#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1848 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001849 return mbedtls_psa_mac_abort(&operation->ctx.mbedtls_ctx);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001850#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1851
1852#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1853#if defined(PSA_CRYPTO_DRIVER_TEST)
1854 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001855 return mbedtls_test_transparent_mac_abort(
1856 &operation->ctx.transparent_test_driver_ctx);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001857 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001858 return mbedtls_test_opaque_mac_abort(
1859 &operation->ctx.opaque_test_driver_ctx);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001860#endif /* PSA_CRYPTO_DRIVER_TEST */
1861#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1862 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001863 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001864 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001865}
Gilles Peskinead0e0122021-04-24 13:19:45 +02001866
1867#endif /* MBEDTLS_PSA_CRYPTO_C */