blob: 6156385337e868b68d4107f61c65470100e67873 [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;
Sergey26f53f12023-02-11 18:20:20 -07001063#else /* MBEDTLS_PSA_BUILTIN_CIPHER */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001064 return PSA_ERROR_NOT_SUPPORTED;
Sergey26f53f12023-02-11 18:20:20 -07001065#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +01001066
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001067 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +01001068#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001069#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +01001070 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001071 status = mbedtls_test_opaque_cipher_decrypt_setup(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001072 &operation->ctx.opaque_test_driver_ctx,
1073 attributes,
1074 key_buffer, key_buffer_size,
1075 alg);
Steven Cooreman37941cb2020-07-28 18:49:51 +02001076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001077 if (status == PSA_SUCCESS) {
Ronald Cron49fafa92021-03-10 08:34:23 +01001078 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001079 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001080
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001081 return status;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001082#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001083#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +02001084 default:
1085 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001086 (void) status;
1087 (void) operation;
1088 (void) key_buffer;
1089 (void) key_buffer_size;
1090 (void) alg;
1091 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001092 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001093}
1094
Steven Cooreman37941cb2020-07-28 18:49:51 +02001095psa_status_t psa_driver_wrapper_cipher_set_iv(
Ronald Cron6056fe82020-12-15 13:58:07 +01001096 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001097 const uint8_t *iv,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001098 size_t iv_length)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001099{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001100 switch (operation->id) {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001101#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001102 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001103 return mbedtls_psa_cipher_set_iv(&operation->ctx.mbedtls_ctx,
1104 iv,
1105 iv_length);
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001106#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001107
1108#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001109#if defined(PSA_CRYPTO_DRIVER_TEST)
1110 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001111 return mbedtls_test_transparent_cipher_set_iv(
1112 &operation->ctx.transparent_test_driver_ctx,
1113 iv, iv_length);
Ronald Cron49fafa92021-03-10 08:34:23 +01001114
Steven Cooreman37941cb2020-07-28 18:49:51 +02001115 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001116 return mbedtls_test_opaque_cipher_set_iv(
1117 &operation->ctx.opaque_test_driver_ctx,
1118 iv, iv_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +02001119#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001120#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001121 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001123 (void) iv;
1124 (void) iv_length;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001126 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001127}
1128
1129psa_status_t psa_driver_wrapper_cipher_update(
Ronald Cron6056fe82020-12-15 13:58:07 +01001130 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001131 const uint8_t *input,
1132 size_t input_length,
1133 uint8_t *output,
1134 size_t output_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001135 size_t *output_length)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001136{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001137 switch (operation->id) {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001138#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001139 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001140 return mbedtls_psa_cipher_update(&operation->ctx.mbedtls_ctx,
1141 input,
1142 input_length,
1143 output,
1144 output_size,
1145 output_length);
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001146#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1147
Ronald Cron49fafa92021-03-10 08:34:23 +01001148#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001149#if defined(PSA_CRYPTO_DRIVER_TEST)
1150 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001151 return mbedtls_test_transparent_cipher_update(
1152 &operation->ctx.transparent_test_driver_ctx,
1153 input, input_length,
1154 output, output_size, output_length);
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001155
Steven Cooreman37941cb2020-07-28 18:49:51 +02001156 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001157 return mbedtls_test_opaque_cipher_update(
1158 &operation->ctx.opaque_test_driver_ctx,
1159 input, input_length,
1160 output, output_size, output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +02001161#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001162#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001163 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001165 (void) input;
1166 (void) input_length;
1167 (void) output;
1168 (void) output_size;
1169 (void) output_length;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001171 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001172}
1173
1174psa_status_t psa_driver_wrapper_cipher_finish(
Ronald Cron6056fe82020-12-15 13:58:07 +01001175 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001176 uint8_t *output,
1177 size_t output_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001178 size_t *output_length)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001179{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001180 switch (operation->id) {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001181#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001182 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001183 return mbedtls_psa_cipher_finish(&operation->ctx.mbedtls_ctx,
1184 output,
1185 output_size,
1186 output_length);
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001187#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001188
1189#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001190#if defined(PSA_CRYPTO_DRIVER_TEST)
1191 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001192 return mbedtls_test_transparent_cipher_finish(
1193 &operation->ctx.transparent_test_driver_ctx,
1194 output, output_size, output_length);
Ronald Cron49fafa92021-03-10 08:34:23 +01001195
Steven Cooreman37941cb2020-07-28 18:49:51 +02001196 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001197 return mbedtls_test_opaque_cipher_finish(
1198 &operation->ctx.opaque_test_driver_ctx,
1199 output, output_size, output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +02001200#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001201#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001202 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001204 (void) output;
1205 (void) output_size;
1206 (void) output_length;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001208 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001209}
1210
1211psa_status_t psa_driver_wrapper_cipher_abort(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001212 psa_cipher_operation_t *operation)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001213{
Ronald Crondd24c9b2020-12-15 14:10:01 +01001214 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001216 switch (operation->id) {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001217#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001218 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001219 return mbedtls_psa_cipher_abort(&operation->ctx.mbedtls_ctx);
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001220#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +01001221
1222#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001223#if defined(PSA_CRYPTO_DRIVER_TEST)
1224 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001225 status = mbedtls_test_transparent_cipher_abort(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001226 &operation->ctx.transparent_test_driver_ctx);
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001227 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001228 &operation->ctx.transparent_test_driver_ctx,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001229 sizeof(operation->ctx.transparent_test_driver_ctx));
1230 return status;
Ronald Cron49fafa92021-03-10 08:34:23 +01001231
Steven Cooreman37941cb2020-07-28 18:49:51 +02001232 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001233 status = mbedtls_test_opaque_cipher_abort(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001234 &operation->ctx.opaque_test_driver_ctx);
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001235 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001236 &operation->ctx.opaque_test_driver_ctx,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001237 sizeof(operation->ctx.opaque_test_driver_ctx));
1238 return status;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001239#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001240#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001241 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001242
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001243 (void) status;
1244 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001245}
1246
Steven Cooreman1e582352021-02-18 17:24:37 +01001247/*
1248 * Hashing functions
1249 */
1250psa_status_t psa_driver_wrapper_hash_compute(
1251 psa_algorithm_t alg,
1252 const uint8_t *input,
1253 size_t input_length,
1254 uint8_t *hash,
1255 size_t hash_size,
1256 size_t *hash_length)
1257{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001258 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001259
1260 /* Try accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001261#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001262 status = mbedtls_test_transparent_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001263 alg, input, input_length, hash, hash_size, hash_length);
1264 if (status != PSA_ERROR_NOT_SUPPORTED) {
1265 return status;
1266 }
Steven Cooremanf7638102021-03-04 15:14:36 +01001267#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001268
1269 /* If software fallback is compiled in, try fallback */
1270#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001271 status = mbedtls_psa_hash_compute(alg, input, input_length,
1272 hash, hash_size, hash_length);
1273 if (status != PSA_ERROR_NOT_SUPPORTED) {
1274 return status;
1275 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001276#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001277 (void) status;
1278 (void) alg;
1279 (void) input;
1280 (void) input_length;
1281 (void) hash;
1282 (void) hash_size;
1283 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001284
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001285 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001286}
1287
1288psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001289 psa_hash_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001290 psa_algorithm_t alg)
Steven Cooreman1e582352021-02-18 17:24:37 +01001291{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001292 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001293
Steven Cooreman1e582352021-02-18 17:24:37 +01001294 /* Try setup on accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001295#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001296 status = mbedtls_test_transparent_hash_setup(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001297 &operation->ctx.test_driver_ctx, alg);
1298 if (status == PSA_SUCCESS) {
Steven Cooremanf7638102021-03-04 15:14:36 +01001299 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001300 }
Steven Cooremanf7638102021-03-04 15:14:36 +01001301
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001302 if (status != PSA_ERROR_NOT_SUPPORTED) {
1303 return status;
1304 }
Steven Cooremanf7638102021-03-04 15:14:36 +01001305#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001306
1307 /* If software fallback is compiled in, try fallback */
1308#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001309 status = mbedtls_psa_hash_setup(&operation->ctx.mbedtls_ctx, alg);
1310 if (status == PSA_SUCCESS) {
Steven Cooreman1e582352021-02-18 17:24:37 +01001311 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001312 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001314 if (status != PSA_ERROR_NOT_SUPPORTED) {
1315 return status;
1316 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001317#endif
1318 /* Nothing left to try if we fall through here */
1319 (void) status;
1320 (void) operation;
1321 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001322 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001323}
1324
1325psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001326 const psa_hash_operation_t *source_operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001327 psa_hash_operation_t *target_operation)
Steven Cooreman1e582352021-02-18 17:24:37 +01001328{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001329 switch (source_operation->id) {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001330#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1331 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1332 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001333 return mbedtls_psa_hash_clone(&source_operation->ctx.mbedtls_ctx,
1334 &target_operation->ctx.mbedtls_ctx);
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001335#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001336#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001337 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1338 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001339 return mbedtls_test_transparent_hash_clone(
1340 &source_operation->ctx.test_driver_ctx,
1341 &target_operation->ctx.test_driver_ctx);
Steven Cooremanf7638102021-03-04 15:14:36 +01001342#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001343 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001344 (void) target_operation;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001345 return PSA_ERROR_BAD_STATE;
Steven Cooreman1e582352021-02-18 17:24:37 +01001346 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001347}
1348
1349psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001350 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001351 const uint8_t *input,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001352 size_t input_length)
Steven Cooreman1e582352021-02-18 17:24:37 +01001353{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001354 switch (operation->id) {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001355#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1356 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001357 return mbedtls_psa_hash_update(&operation->ctx.mbedtls_ctx,
1358 input, input_length);
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001359#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001360#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001361 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001362 return mbedtls_test_transparent_hash_update(
1363 &operation->ctx.test_driver_ctx,
1364 input, input_length);
Steven Cooremanf7638102021-03-04 15:14:36 +01001365#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001366 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001367 (void) input;
1368 (void) input_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001369 return PSA_ERROR_BAD_STATE;
Steven Cooreman1e582352021-02-18 17:24:37 +01001370 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001371}
1372
1373psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001374 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001375 uint8_t *hash,
1376 size_t hash_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001377 size_t *hash_length)
Steven Cooreman1e582352021-02-18 17:24:37 +01001378{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001379 switch (operation->id) {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001380#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1381 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001382 return mbedtls_psa_hash_finish(&operation->ctx.mbedtls_ctx,
1383 hash, hash_size, hash_length);
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001384#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001385#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001386 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001387 return mbedtls_test_transparent_hash_finish(
1388 &operation->ctx.test_driver_ctx,
1389 hash, hash_size, hash_length);
Steven Cooremanf7638102021-03-04 15:14:36 +01001390#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001391 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001392 (void) hash;
1393 (void) hash_size;
1394 (void) hash_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001395 return PSA_ERROR_BAD_STATE;
Steven Cooreman1e582352021-02-18 17:24:37 +01001396 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001397}
1398
1399psa_status_t psa_driver_wrapper_hash_abort(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001400 psa_hash_operation_t *operation)
Steven Cooreman1e582352021-02-18 17:24:37 +01001401{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001402 switch (operation->id) {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001403#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1404 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001405 return mbedtls_psa_hash_abort(&operation->ctx.mbedtls_ctx);
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001406#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001407#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001408 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001409 return mbedtls_test_transparent_hash_abort(
1410 &operation->ctx.test_driver_ctx);
Steven Cooremanf7638102021-03-04 15:14:36 +01001411#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001412 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001413 return PSA_ERROR_BAD_STATE;
Steven Cooreman1e582352021-02-18 17:24:37 +01001414 }
1415}
1416
Ronald Cronde822812021-03-17 16:08:20 +01001417psa_status_t psa_driver_wrapper_aead_encrypt(
1418 const psa_key_attributes_t *attributes,
1419 const uint8_t *key_buffer, size_t key_buffer_size,
1420 psa_algorithm_t alg,
1421 const uint8_t *nonce, size_t nonce_length,
1422 const uint8_t *additional_data, size_t additional_data_length,
1423 const uint8_t *plaintext, size_t plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001424 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length)
Ronald Cronde822812021-03-17 16:08:20 +01001425{
1426 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1427 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001428 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Ronald Cronde822812021-03-17 16:08:20 +01001429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001430 switch (location) {
Ronald Cronde822812021-03-17 16:08:20 +01001431 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1432 /* Key is stored in the slot in export representation, so
1433 * cycle through all known transparent accelerators */
1434
1435#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1436#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001437 status = mbedtls_test_transparent_aead_encrypt(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001438 attributes, key_buffer, key_buffer_size,
1439 alg,
1440 nonce, nonce_length,
1441 additional_data, additional_data_length,
1442 plaintext, plaintext_length,
1443 ciphertext, ciphertext_size, ciphertext_length);
Ronald Cronde822812021-03-17 16:08:20 +01001444 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001445 if (status != PSA_ERROR_NOT_SUPPORTED) {
1446 return status;
1447 }
Ronald Cronde822812021-03-17 16:08:20 +01001448#endif /* PSA_CRYPTO_DRIVER_TEST */
1449#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1450
1451 /* Fell through, meaning no accelerator supports this operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001452 return mbedtls_psa_aead_encrypt(
1453 attributes, key_buffer, key_buffer_size,
1454 alg,
1455 nonce, nonce_length,
1456 additional_data, additional_data_length,
1457 plaintext, plaintext_length,
1458 ciphertext, ciphertext_size, ciphertext_length);
Ronald Cronde822812021-03-17 16:08:20 +01001459
1460 /* Add cases for opaque driver here */
1461
1462 default:
1463 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001464 (void) status;
1465 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cronde822812021-03-17 16:08:20 +01001466 }
1467}
1468
1469psa_status_t psa_driver_wrapper_aead_decrypt(
1470 const psa_key_attributes_t *attributes,
1471 const uint8_t *key_buffer, size_t key_buffer_size,
1472 psa_algorithm_t alg,
1473 const uint8_t *nonce, size_t nonce_length,
1474 const uint8_t *additional_data, size_t additional_data_length,
1475 const uint8_t *ciphertext, size_t ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001476 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length)
Ronald Cronde822812021-03-17 16:08:20 +01001477{
1478 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1479 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001480 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Ronald Cronde822812021-03-17 16:08:20 +01001481
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001482 switch (location) {
Ronald Cronde822812021-03-17 16:08:20 +01001483 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1484 /* Key is stored in the slot in export representation, so
1485 * cycle through all known transparent accelerators */
1486
1487#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1488#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001489 status = mbedtls_test_transparent_aead_decrypt(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001490 attributes, key_buffer, key_buffer_size,
1491 alg,
1492 nonce, nonce_length,
1493 additional_data, additional_data_length,
1494 ciphertext, ciphertext_length,
1495 plaintext, plaintext_size, plaintext_length);
Ronald Cronde822812021-03-17 16:08:20 +01001496 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001497 if (status != PSA_ERROR_NOT_SUPPORTED) {
1498 return status;
1499 }
Ronald Cronde822812021-03-17 16:08:20 +01001500#endif /* PSA_CRYPTO_DRIVER_TEST */
1501#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1502
1503 /* Fell through, meaning no accelerator supports this operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001504 return mbedtls_psa_aead_decrypt(
1505 attributes, key_buffer, key_buffer_size,
1506 alg,
1507 nonce, nonce_length,
1508 additional_data, additional_data_length,
1509 ciphertext, ciphertext_length,
1510 plaintext, plaintext_size, plaintext_length);
Ronald Cronde822812021-03-17 16:08:20 +01001511
1512 /* Add cases for opaque driver here */
1513
1514 default:
1515 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001516 (void) status;
1517 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cronde822812021-03-17 16:08:20 +01001518 }
1519}
Steven Cooreman896d51e2021-03-19 15:24:23 +01001520
1521
1522/*
1523 * MAC functions
1524 */
1525psa_status_t psa_driver_wrapper_mac_compute(
1526 const psa_key_attributes_t *attributes,
1527 const uint8_t *key_buffer,
1528 size_t key_buffer_size,
1529 psa_algorithm_t alg,
1530 const uint8_t *input,
1531 size_t input_length,
1532 uint8_t *mac,
1533 size_t mac_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001534 size_t *mac_length)
Steven Cooreman896d51e2021-03-19 15:24:23 +01001535{
1536 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1537 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001538 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001540 switch (location) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001541 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1542 /* Key is stored in the slot in export representation, so
1543 * cycle through all known transparent accelerators */
1544#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1545#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001546 status = mbedtls_test_transparent_mac_compute(
Steven Cooreman896d51e2021-03-19 15:24:23 +01001547 attributes, key_buffer, key_buffer_size, alg,
1548 input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001549 mac, mac_size, mac_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001550 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001551 if (status != PSA_ERROR_NOT_SUPPORTED) {
1552 return status;
1553 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001554#endif /* PSA_CRYPTO_DRIVER_TEST */
1555#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1556#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1557 /* Fell through, meaning no accelerator supports this operation */
1558 status = mbedtls_psa_mac_compute(
1559 attributes, key_buffer, key_buffer_size, alg,
1560 input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001561 mac, mac_size, mac_length);
1562 if (status != PSA_ERROR_NOT_SUPPORTED) {
1563 return status;
1564 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001565#endif /* MBEDTLS_PSA_BUILTIN_MAC */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001566 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001567
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001568 /* Add cases for opaque driver here */
Steven Cooreman896d51e2021-03-19 15:24:23 +01001569#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1570#if defined(PSA_CRYPTO_DRIVER_TEST)
1571 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001572 status = mbedtls_test_opaque_mac_compute(
Steven Cooreman896d51e2021-03-19 15:24:23 +01001573 attributes, key_buffer, key_buffer_size, alg,
1574 input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001575 mac, mac_size, mac_length);
1576 return status;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001577#endif /* PSA_CRYPTO_DRIVER_TEST */
1578#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1579 default:
1580 /* Key is declared with a lifetime not known to us */
1581 (void) key_buffer;
1582 (void) key_buffer_size;
1583 (void) alg;
1584 (void) input;
1585 (void) input_length;
1586 (void) mac;
1587 (void) mac_size;
1588 (void) mac_length;
1589 (void) status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001590 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001591 }
1592}
1593
1594psa_status_t psa_driver_wrapper_mac_sign_setup(
1595 psa_mac_operation_t *operation,
1596 const psa_key_attributes_t *attributes,
1597 const uint8_t *key_buffer,
1598 size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001599 psa_algorithm_t alg)
Steven Cooreman896d51e2021-03-19 15:24:23 +01001600{
1601 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1602 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001603 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001605 switch (location) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001606 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1607 /* Key is stored in the slot in export representation, so
1608 * cycle through all known transparent accelerators */
1609#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1610#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001611 status = mbedtls_test_transparent_mac_sign_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001612 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001613 attributes,
1614 key_buffer, key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001615 alg);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001616 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001617 if (status == PSA_SUCCESS) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001618 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001619 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001620
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001621 if (status != PSA_ERROR_NOT_SUPPORTED) {
1622 return status;
1623 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001624#endif /* PSA_CRYPTO_DRIVER_TEST */
1625#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1626#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1627 /* Fell through, meaning no accelerator supports this operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001628 status = mbedtls_psa_mac_sign_setup(&operation->ctx.mbedtls_ctx,
1629 attributes,
1630 key_buffer, key_buffer_size,
1631 alg);
1632 if (status == PSA_SUCCESS) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001633 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001634 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001636 if (status != PSA_ERROR_NOT_SUPPORTED) {
1637 return status;
1638 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001639#endif /* MBEDTLS_PSA_BUILTIN_MAC */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001640 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001642 /* Add cases for opaque driver here */
Steven Cooreman896d51e2021-03-19 15:24:23 +01001643#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1644#if defined(PSA_CRYPTO_DRIVER_TEST)
1645 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001646 status = mbedtls_test_opaque_mac_sign_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001647 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001648 attributes,
1649 key_buffer, key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001650 alg);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001651
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001652 if (status == PSA_SUCCESS) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001653 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001654 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001656 return status;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001657#endif /* PSA_CRYPTO_DRIVER_TEST */
1658#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1659 default:
1660 /* Key is declared with a lifetime not known to us */
1661 (void) status;
Ronald Cron485559e2021-04-28 14:29:00 +02001662 (void) operation;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001663 (void) key_buffer;
1664 (void) key_buffer_size;
1665 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001666 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001667 }
1668}
1669
1670psa_status_t psa_driver_wrapper_mac_verify_setup(
1671 psa_mac_operation_t *operation,
1672 const psa_key_attributes_t *attributes,
1673 const uint8_t *key_buffer,
1674 size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001675 psa_algorithm_t alg)
Steven Cooreman896d51e2021-03-19 15:24:23 +01001676{
1677 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1678 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001679 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001681 switch (location) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001682 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1683 /* Key is stored in the slot in export representation, so
1684 * cycle through all known transparent accelerators */
1685#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1686#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001687 status = mbedtls_test_transparent_mac_verify_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001688 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001689 attributes,
1690 key_buffer, key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001691 alg);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001692 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001693 if (status == PSA_SUCCESS) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001694 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001695 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001696
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001697 if (status != PSA_ERROR_NOT_SUPPORTED) {
1698 return status;
1699 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001700#endif /* PSA_CRYPTO_DRIVER_TEST */
1701#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1702#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1703 /* Fell through, meaning no accelerator supports this operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001704 status = mbedtls_psa_mac_verify_setup(&operation->ctx.mbedtls_ctx,
1705 attributes,
1706 key_buffer, key_buffer_size,
1707 alg);
1708 if (status == PSA_SUCCESS) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001709 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001710 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001711
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001712 if (status != PSA_ERROR_NOT_SUPPORTED) {
1713 return status;
1714 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001715#endif /* MBEDTLS_PSA_BUILTIN_MAC */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001716 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001717
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001718 /* Add cases for opaque driver here */
Steven Cooreman896d51e2021-03-19 15:24:23 +01001719#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1720#if defined(PSA_CRYPTO_DRIVER_TEST)
1721 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001722 status = mbedtls_test_opaque_mac_verify_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001723 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001724 attributes,
1725 key_buffer, key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001726 alg);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001727
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001728 if (status == PSA_SUCCESS) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001729 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001730 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001731
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001732 return status;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001733#endif /* PSA_CRYPTO_DRIVER_TEST */
1734#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1735 default:
1736 /* Key is declared with a lifetime not known to us */
1737 (void) status;
Ronald Cron485559e2021-04-28 14:29:00 +02001738 (void) operation;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001739 (void) key_buffer;
1740 (void) key_buffer_size;
1741 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001742 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001743 }
1744}
1745
1746psa_status_t psa_driver_wrapper_mac_update(
1747 psa_mac_operation_t *operation,
1748 const uint8_t *input,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001749 size_t input_length)
Steven Cooreman896d51e2021-03-19 15:24:23 +01001750{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001751 switch (operation->id) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001752#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1753 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001754 return mbedtls_psa_mac_update(&operation->ctx.mbedtls_ctx,
1755 input, input_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001756#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1757
1758#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1759#if defined(PSA_CRYPTO_DRIVER_TEST)
1760 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001761 return mbedtls_test_transparent_mac_update(
1762 &operation->ctx.transparent_test_driver_ctx,
1763 input, input_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001764
1765 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001766 return mbedtls_test_opaque_mac_update(
1767 &operation->ctx.opaque_test_driver_ctx,
1768 input, input_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001769#endif /* PSA_CRYPTO_DRIVER_TEST */
1770#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1771 default:
1772 (void) input;
1773 (void) input_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001774 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001775 }
1776}
1777
1778psa_status_t psa_driver_wrapper_mac_sign_finish(
1779 psa_mac_operation_t *operation,
1780 uint8_t *mac,
1781 size_t mac_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001782 size_t *mac_length)
Steven Cooreman896d51e2021-03-19 15:24:23 +01001783{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001784 switch (operation->id) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001785#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1786 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001787 return mbedtls_psa_mac_sign_finish(&operation->ctx.mbedtls_ctx,
1788 mac, mac_size, mac_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001789#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1790
1791#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1792#if defined(PSA_CRYPTO_DRIVER_TEST)
1793 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001794 return mbedtls_test_transparent_mac_sign_finish(
1795 &operation->ctx.transparent_test_driver_ctx,
1796 mac, mac_size, mac_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001797
1798 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001799 return mbedtls_test_opaque_mac_sign_finish(
1800 &operation->ctx.opaque_test_driver_ctx,
1801 mac, mac_size, mac_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001802#endif /* PSA_CRYPTO_DRIVER_TEST */
1803#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1804 default:
1805 (void) mac;
1806 (void) mac_size;
1807 (void) mac_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001808 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001809 }
1810}
1811
1812psa_status_t psa_driver_wrapper_mac_verify_finish(
1813 psa_mac_operation_t *operation,
1814 const uint8_t *mac,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001815 size_t mac_length)
Steven Cooreman896d51e2021-03-19 15:24:23 +01001816{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001817 switch (operation->id) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001818#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1819 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001820 return mbedtls_psa_mac_verify_finish(&operation->ctx.mbedtls_ctx,
1821 mac, mac_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001822#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1823
1824#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1825#if defined(PSA_CRYPTO_DRIVER_TEST)
1826 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001827 return mbedtls_test_transparent_mac_verify_finish(
1828 &operation->ctx.transparent_test_driver_ctx,
1829 mac, mac_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001830
1831 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001832 return mbedtls_test_opaque_mac_verify_finish(
1833 &operation->ctx.opaque_test_driver_ctx,
1834 mac, mac_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001835#endif /* PSA_CRYPTO_DRIVER_TEST */
1836#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1837 default:
1838 (void) mac;
1839 (void) mac_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001840 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001841 }
1842}
1843
1844psa_status_t psa_driver_wrapper_mac_abort(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001845 psa_mac_operation_t *operation)
Steven Cooreman896d51e2021-03-19 15:24:23 +01001846{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001847 switch (operation->id) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001848#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1849 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001850 return mbedtls_psa_mac_abort(&operation->ctx.mbedtls_ctx);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001851#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1852
1853#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1854#if defined(PSA_CRYPTO_DRIVER_TEST)
1855 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001856 return mbedtls_test_transparent_mac_abort(
1857 &operation->ctx.transparent_test_driver_ctx);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001858 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001859 return mbedtls_test_opaque_mac_abort(
1860 &operation->ctx.opaque_test_driver_ctx);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001861#endif /* PSA_CRYPTO_DRIVER_TEST */
1862#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1863 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001864 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001865 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001866}
Gilles Peskinead0e0122021-04-24 13:19:45 +02001867
1868#endif /* MBEDTLS_PSA_CRYPTO_C */