blob: 196cd2eda7d498eb86354edb288393daa4b7079d [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00007 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Steven Cooremancd84cb42020-07-16 20:28:36 +02008 */
9
Ronald Cronde822812021-03-17 16:08:20 +010010#include "psa_crypto_aead.h"
Ronald Cron0b805592020-12-14 18:08:20 +010011#include "psa_crypto_cipher.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020012#include "psa_crypto_core.h"
13#include "psa_crypto_driver_wrappers.h"
Steven Cooreman830aff22021-03-09 09:50:44 +010014#include "psa_crypto_hash.h"
Steven Cooreman896d51e2021-03-19 15:24:23 +010015#include "psa_crypto_mac.h"
Steven Cooreman830aff22021-03-09 09:50:44 +010016
Steven Cooreman2a1664c2020-07-20 15:33:08 +020017#include "mbedtls/platform.h"
18
Gilles Peskinead0e0122021-04-24 13:19:45 +020019#if defined(MBEDTLS_PSA_CRYPTO_C)
20
Steven Cooreman2a1664c2020-07-20 15:33:08 +020021#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
Steven Cooremancd84cb42020-07-16 20:28:36 +020022
23/* Include test driver definition when running tests */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020024#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman56250fd2020-09-04 13:07:15 +020025#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020026#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020027#endif
28#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020029#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020030#endif
Ronald Cron05ee58d2021-04-30 17:00:34 +020031#include "test/drivers/test_driver.h"
Steven Cooremanf1720ea2020-07-24 18:41:58 +020032#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +020033
Steven Cooreman56250fd2020-09-04 13:07:15 +020034/* Repeat above block for each JSON-declared driver during autogeneration */
Steven Cooremanaa87fd02021-03-15 18:54:03 +010035#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Steven Cooreman56250fd2020-09-04 13:07:15 +020036
Steven Cooreman1e582352021-02-18 17:24:37 +010037/* Auto-generated values depending on which drivers are registered.
38 * ID 0 is reserved for unallocated operations.
39 * ID 1 is reserved for the Mbed TLS software driver. */
Steven Cooreman830aff22021-03-09 09:50:44 +010040#define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
41
Steven Cooreman37941cb2020-07-28 18:49:51 +020042#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman1e582352021-02-18 17:24:37 +010043#define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2)
44#define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3)
Steven Cooreman37941cb2020-07-28 18:49:51 +020045#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +020046
47/* Support the 'old' SE interface when asked to */
Steven Cooreman7a250572020-07-17 16:43:05 +020048#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman56250fd2020-09-04 13:07:15 +020049/* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
50 * SE driver is present, to avoid unused argument errors at compile time. */
51#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020052#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020053#endif
Steven Cooreman7a250572020-07-17 16:43:05 +020054#include "psa_crypto_se.h"
55#endif
56
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057psa_status_t psa_driver_wrapper_init(void)
Ronald Cron088d5d02021-04-10 16:57:30 +020058{
59 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
60
61#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062 status = psa_init_all_se_drivers();
63 if (status != PSA_SUCCESS) {
64 return status;
65 }
Ronald Cron088d5d02021-04-10 16:57:30 +020066#endif
67
68#if defined(PSA_CRYPTO_DRIVER_TEST)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010069 status = mbedtls_test_transparent_init();
70 if (status != PSA_SUCCESS) {
71 return status;
72 }
Ronald Cron088d5d02021-04-10 16:57:30 +020073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 status = mbedtls_test_opaque_init();
75 if (status != PSA_SUCCESS) {
76 return status;
77 }
Ronald Cron088d5d02021-04-10 16:57:30 +020078#endif
79
80 (void) status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010081 return PSA_SUCCESS;
Ronald Cron088d5d02021-04-10 16:57:30 +020082}
83
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084void psa_driver_wrapper_free(void)
Ronald Cron088d5d02021-04-10 16:57:30 +020085{
86#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
87 /* Unregister all secure element drivers, so that we restart from
88 * a pristine state. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 psa_unregister_all_se_drivers();
Ronald Cron088d5d02021-04-10 16:57:30 +020090#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
91
92#if defined(PSA_CRYPTO_DRIVER_TEST)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093 mbedtls_test_transparent_free();
94 mbedtls_test_opaque_free();
Ronald Cron088d5d02021-04-10 16:57:30 +020095#endif
96}
97
Steven Cooremancd84cb42020-07-16 20:28:36 +020098/* Start delegation functions */
gabor-mezei-armc53f4f62021-04-22 11:32:19 +020099psa_status_t psa_driver_wrapper_sign_message(
100 const psa_key_attributes_t *attributes,
101 const uint8_t *key_buffer,
102 size_t key_buffer_size,
103 psa_algorithm_t alg,
104 const uint8_t *input,
105 size_t input_length,
106 uint8_t *signature,
107 size_t signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 size_t *signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200109{
110 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200111 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114 switch (location) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200115 case PSA_KEY_LOCATION_LOCAL_STORAGE:
116 /* Key is stored in the slot in export representation, so
117 * cycle through all known transparent accelerators */
118#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
119#if defined(PSA_CRYPTO_DRIVER_TEST)
120 status = mbedtls_test_transparent_signature_sign_message(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 attributes,
122 key_buffer,
123 key_buffer_size,
124 alg,
125 input,
126 input_length,
127 signature,
128 signature_size,
129 signature_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200130 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 if (status != PSA_ERROR_NOT_SUPPORTED) {
132 return status;
133 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200134#endif /* PSA_CRYPTO_DRIVER_TEST */
135#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200136 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 /* Add cases for opaque driver here */
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200139#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
140#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arm77588fb2021-04-27 18:52:51 +0200141 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200142 status = mbedtls_test_opaque_signature_sign_message(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 attributes,
144 key_buffer,
145 key_buffer_size,
146 alg,
147 input,
148 input_length,
149 signature,
150 signature_size,
151 signature_length);
152 if (status != PSA_ERROR_NOT_SUPPORTED) {
153 return status;
154 }
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200155 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200156#endif /* PSA_CRYPTO_DRIVER_TEST */
157#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
158 default:
159 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 (void) status;
gabor-mezei-armf25c9762021-05-12 11:12:25 +0200161 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200162 }
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100164 return psa_sign_message_builtin(attributes,
165 key_buffer,
166 key_buffer_size,
167 alg,
168 input,
169 input_length,
170 signature,
171 signature_size,
172 signature_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200173}
174
175psa_status_t psa_driver_wrapper_verify_message(
176 const psa_key_attributes_t *attributes,
177 const uint8_t *key_buffer,
178 size_t key_buffer_size,
179 psa_algorithm_t alg,
180 const uint8_t *input,
181 size_t input_length,
182 const uint8_t *signature,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100183 size_t signature_length)
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200184{
185 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200186 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189 switch (location) {
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200190 case PSA_KEY_LOCATION_LOCAL_STORAGE:
191 /* Key is stored in the slot in export representation, so
192 * cycle through all known transparent accelerators */
193#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
194#if defined(PSA_CRYPTO_DRIVER_TEST)
195 status = mbedtls_test_transparent_signature_verify_message(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 attributes,
197 key_buffer,
198 key_buffer_size,
199 alg,
200 input,
201 input_length,
202 signature,
203 signature_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200204 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 if (status != PSA_ERROR_NOT_SUPPORTED) {
206 return status;
207 }
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200208#endif /* PSA_CRYPTO_DRIVER_TEST */
209#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200210 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100212 /* Add cases for opaque driver here */
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200213#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
214#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arm77588fb2021-04-27 18:52:51 +0200215 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 return mbedtls_test_opaque_signature_verify_message(
217 attributes,
218 key_buffer,
219 key_buffer_size,
220 alg,
221 input,
222 input_length,
223 signature,
224 signature_length);
225 if (status != PSA_ERROR_NOT_SUPPORTED) {
226 return status;
227 }
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200228 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200229#endif /* PSA_CRYPTO_DRIVER_TEST */
230#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
231 default:
232 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 (void) status;
gabor-mezei-armf25c9762021-05-12 11:12:25 +0200234 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200235 }
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 return psa_verify_message_builtin(attributes,
238 key_buffer,
239 key_buffer_size,
240 alg,
241 input,
242 input_length,
243 signature,
244 signature_length);
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200245}
246
Ronald Cron9f17aa42020-12-08 17:07:25 +0100247psa_status_t psa_driver_wrapper_sign_hash(
248 const psa_key_attributes_t *attributes,
249 const uint8_t *key_buffer, size_t key_buffer_size,
250 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251 uint8_t *signature, size_t signature_size, size_t *signature_length)
Steven Cooremancd84cb42020-07-16 20:28:36 +0200252{
Steven Cooreman7a250572020-07-17 16:43:05 +0200253 /* Try dynamically-registered SE interface first */
254#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
255 const psa_drv_se_t *drv;
256 psa_drv_se_context_t *drv_context;
257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 if (psa_get_se_driver(attributes->core.lifetime, &drv, &drv_context)) {
259 if (drv->asymmetric == NULL ||
260 drv->asymmetric->p_sign == NULL) {
Steven Cooreman7a250572020-07-17 16:43:05 +0200261 /* Key is defined in SE, but we have no way to exercise it */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman7a250572020-07-17 16:43:05 +0200263 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 return drv->asymmetric->p_sign(
265 drv_context, *((psa_key_slot_number_t *) key_buffer),
266 alg, hash, hash_length,
267 signature, signature_size, signature_length);
Steven Cooreman7a250572020-07-17 16:43:05 +0200268 }
Pengyu Lvf5131972022-11-08 18:17:00 +0800269#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooreman7a250572020-07-17 16:43:05 +0200270
Ronald Cronfce9df22020-12-08 18:06:03 +0100271 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100272 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Steven Cooremancd84cb42020-07-16 20:28:36 +0200274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275 switch (location) {
Steven Cooremancd84cb42020-07-16 20:28:36 +0200276 case PSA_KEY_LOCATION_LOCAL_STORAGE:
277 /* Key is stored in the slot in export representation, so
278 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100279#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200280#if defined(PSA_CRYPTO_DRIVER_TEST)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 status = mbedtls_test_transparent_signature_sign_hash(attributes,
282 key_buffer,
283 key_buffer_size,
284 alg,
285 hash,
286 hash_length,
287 signature,
288 signature_size,
289 signature_length);
290 /* Declared with fallback == true */
291 if (status != PSA_ERROR_NOT_SUPPORTED) {
292 return status;
293 }
294#endif /* PSA_CRYPTO_DRIVER_TEST */
295#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
296 /* Fell through, meaning no accelerator supports this operation */
297 return psa_sign_hash_builtin(attributes,
298 key_buffer,
299 key_buffer_size,
300 alg,
301 hash,
302 hash_length,
303 signature,
304 signature_size,
305 signature_length);
306
307 /* Add cases for opaque driver here */
308#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
309#if defined(PSA_CRYPTO_DRIVER_TEST)
310 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
311 return mbedtls_test_opaque_signature_sign_hash(attributes,
Ronald Cron9f17aa42020-12-08 17:07:25 +0100312 key_buffer,
313 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200314 alg,
315 hash,
316 hash_length,
317 signature,
318 signature_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319 signature_length);
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200320#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100321#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200322 default:
323 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100324 (void) status;
325 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremancd84cb42020-07-16 20:28:36 +0200326 }
Steven Cooremancd84cb42020-07-16 20:28:36 +0200327}
328
Ronald Cron9f17aa42020-12-08 17:07:25 +0100329psa_status_t psa_driver_wrapper_verify_hash(
330 const psa_key_attributes_t *attributes,
331 const uint8_t *key_buffer, size_t key_buffer_size,
332 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 const uint8_t *signature, size_t signature_length)
Steven Cooreman55ae2172020-07-17 19:46:15 +0200334{
Steven Cooreman55ae2172020-07-17 19:46:15 +0200335 /* Try dynamically-registered SE interface first */
336#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
337 const psa_drv_se_t *drv;
338 psa_drv_se_context_t *drv_context;
339
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100340 if (psa_get_se_driver(attributes->core.lifetime, &drv, &drv_context)) {
341 if (drv->asymmetric == NULL ||
342 drv->asymmetric->p_verify == NULL) {
Steven Cooreman55ae2172020-07-17 19:46:15 +0200343 /* Key is defined in SE, but we have no way to exercise it */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman55ae2172020-07-17 19:46:15 +0200345 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346 return drv->asymmetric->p_verify(
347 drv_context, *((psa_key_slot_number_t *) key_buffer),
348 alg, hash, hash_length,
349 signature, signature_length);
Steven Cooreman55ae2172020-07-17 19:46:15 +0200350 }
Pengyu Lvf5131972022-11-08 18:17:00 +0800351#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200352
Ronald Cronfce9df22020-12-08 18:06:03 +0100353 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100354 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Steven Cooreman55ae2172020-07-17 19:46:15 +0200356
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357 switch (location) {
Steven Cooreman55ae2172020-07-17 19:46:15 +0200358 case PSA_KEY_LOCATION_LOCAL_STORAGE:
359 /* Key is stored in the slot in export representation, so
360 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100361#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200362#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200363 status = mbedtls_test_transparent_signature_verify_hash(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100364 attributes,
365 key_buffer,
366 key_buffer_size,
367 alg,
368 hash,
369 hash_length,
370 signature,
371 signature_length);
Steven Cooreman55ae2172020-07-17 19:46:15 +0200372 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100373 if (status != PSA_ERROR_NOT_SUPPORTED) {
374 return status;
375 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200376#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100377#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
378
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100379 return psa_verify_hash_builtin(attributes,
380 key_buffer,
381 key_buffer_size,
382 alg,
383 hash,
384 hash_length,
385 signature,
386 signature_length);
Ronald Cronfce9df22020-12-08 18:06:03 +0100387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100389#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200390#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100391 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100392 return mbedtls_test_opaque_signature_verify_hash(attributes,
393 key_buffer,
394 key_buffer_size,
395 alg,
396 hash,
397 hash_length,
398 signature,
399 signature_length);
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200400#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100401#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200402 default:
403 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 (void) status;
405 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman55ae2172020-07-17 19:46:15 +0200406 }
Steven Cooreman55ae2172020-07-17 19:46:15 +0200407}
408
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200409/** Get the key buffer size required to store the key material of a key
410 * associated with an opaque driver without storage.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200411 *
Ronald Cron31216282020-12-05 18:47:56 +0100412 * \param[in] attributes The key attributes.
413 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200414 *
415 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100416 * The minimum size for a buffer to contain the key material has been
417 * returned successfully.
418 * \retval #PSA_ERROR_INVALID_ARGUMENT
419 * The size in bits of the key is not valid.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200420 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100421 * The type and/or the size in bits of the key or the combination of
422 * the two is not supported.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200423 */
Ronald Cron9df74be2020-12-05 19:15:23 +0100424psa_status_t psa_driver_wrapper_get_key_buffer_size(
Ronald Cron31216282020-12-05 18:47:56 +0100425 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426 size_t *key_buffer_size)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200427{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
John Durkop2c618352020-09-22 06:54:01 -0700429 psa_key_type_t key_type = attributes->core.type;
430 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200431
Ronald Cron31216282020-12-05 18:47:56 +0100432 *key_buffer_size = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100433 switch (location) {
John Durkop2c618352020-09-22 06:54:01 -0700434#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100435 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100436#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
437 /* Emulate property 'builtin_key_size' */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100438 if (psa_key_id_is_builtin(
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100439 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100440 psa_get_key_id(attributes)))) {
441 *key_buffer_size = sizeof(psa_drv_slot_number_t);
442 return PSA_SUCCESS;
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100443 }
444#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445 *key_buffer_size = mbedtls_test_size_function(key_type, key_bits);
446 return (*key_buffer_size != 0) ?
447 PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED;
John Durkop2c618352020-09-22 06:54:01 -0700448#endif /* PSA_CRYPTO_DRIVER_TEST */
449
450 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451 (void) key_type;
452 (void) key_bits;
453 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200454 }
455}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200456
Ronald Cron977c2472020-10-13 08:32:21 +0200457psa_status_t psa_driver_wrapper_generate_key(
458 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Steven Cooreman55ae2172020-07-17 19:46:15 +0200460{
Ronald Cron977c2472020-10-13 08:32:21 +0200461 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
462 psa_key_location_t location =
463 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
464
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200465 /* Try dynamically-registered SE interface first */
466#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
467 const psa_drv_se_t *drv;
468 psa_drv_se_context_t *drv_context;
469
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100470 if (psa_get_se_driver(attributes->core.lifetime, &drv, &drv_context)) {
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200471 size_t pubkey_length = 0; /* We don't support this feature yet */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100472 if (drv->key_management == NULL ||
473 drv->key_management->p_generate == NULL) {
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200474 /* Key is defined as being in SE, but we have no way to generate it */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100475 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200476 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100477 return drv->key_management->p_generate(
Ronald Cron977c2472020-10-13 08:32:21 +0200478 drv_context,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100479 *((psa_key_slot_number_t *) key_buffer),
480 attributes, NULL, 0, &pubkey_length);
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200481 }
482#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100484 switch (location) {
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200485 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Ronald Cron977c2472020-10-13 08:32:21 +0200486#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200487 /* Transparent drivers are limited to generating asymmetric keys */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100488 if (PSA_KEY_TYPE_IS_ASYMMETRIC(attributes->core.type)) {
489 /* Cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200490#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200491 status = mbedtls_test_transparent_generate_key(
Ronald Cron977c2472020-10-13 08:32:21 +0200492 attributes, key_buffer, key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100493 key_buffer_length);
Ronald Cron977c2472020-10-13 08:32:21 +0200494 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100495 if (status != PSA_ERROR_NOT_SUPPORTED) {
Ronald Cron977c2472020-10-13 08:32:21 +0200496 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100497 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200498#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200499 }
500#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
501
502 /* Software fallback */
503 status = psa_generate_key_internal(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100504 attributes, key_buffer, key_buffer_size, key_buffer_length);
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200505 break;
Ronald Cron977c2472020-10-13 08:32:21 +0200506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100507 /* Add cases for opaque driver here */
Ronald Cron977c2472020-10-13 08:32:21 +0200508#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200509#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100510 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200511 status = mbedtls_test_opaque_generate_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100512 attributes, key_buffer, key_buffer_size, key_buffer_length);
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200513 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200514#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200515#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
516
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200517 default:
518 /* Key is declared with a lifetime not known to us */
519 status = PSA_ERROR_INVALID_ARGUMENT;
520 break;
521 }
522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100523 return status;
Steven Cooreman55ae2172020-07-17 19:46:15 +0200524}
525
Ronald Cron83282872020-11-22 14:02:39 +0100526psa_status_t psa_driver_wrapper_import_key(
527 const psa_key_attributes_t *attributes,
528 const uint8_t *data,
529 size_t data_length,
530 uint8_t *key_buffer,
531 size_t key_buffer_size,
532 size_t *key_buffer_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100533 size_t *bits)
Steven Cooreman04524762020-10-13 17:43:44 +0200534{
Steven Cooreman04524762020-10-13 17:43:44 +0200535 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100536 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100537 psa_get_key_lifetime(attributes));
Steven Cooreman04524762020-10-13 17:43:44 +0200538
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100539 /* Try dynamically-registered SE interface first */
540#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
541 const psa_drv_se_t *drv;
542 psa_drv_se_context_t *drv_context;
543
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100544 if (psa_get_se_driver(attributes->core.lifetime, &drv, &drv_context)) {
545 if (drv->key_management == NULL ||
546 drv->key_management->p_import == NULL) {
547 return PSA_ERROR_NOT_SUPPORTED;
548 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100549
550 /* The driver should set the number of key bits, however in
551 * case it doesn't, we initialize bits to an invalid value. */
552 *bits = PSA_MAX_KEY_BITS + 1;
553 status = drv->key_management->p_import(
554 drv_context,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100555 *((psa_key_slot_number_t *) key_buffer),
556 attributes, data, data_length, bits);
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100558 if (status != PSA_SUCCESS) {
559 return status;
560 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100561
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100562 if ((*bits) > PSA_MAX_KEY_BITS) {
563 return PSA_ERROR_NOT_SUPPORTED;
564 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100565
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100566 return PSA_SUCCESS;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100567 }
Pengyu Lvf5131972022-11-08 18:17:00 +0800568#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100570 switch (location) {
Ronald Cronbf33c932020-11-28 18:06:53 +0100571 case PSA_KEY_LOCATION_LOCAL_STORAGE:
572 /* Key is stored in the slot in export representation, so
573 * cycle through all known transparent accelerators */
574#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
575#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200576 status = mbedtls_test_transparent_import_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100577 attributes,
578 data, data_length,
579 key_buffer, key_buffer_size,
580 key_buffer_length, bits);
Ronald Cronbf33c932020-11-28 18:06:53 +0100581 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100582 if (status != PSA_ERROR_NOT_SUPPORTED) {
583 return status;
584 }
Ronald Cronbf33c932020-11-28 18:06:53 +0100585#endif /* PSA_CRYPTO_DRIVER_TEST */
586#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
587 /* Fell through, meaning no accelerator supports this operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100588 return psa_import_key_into_slot(attributes,
589 data, data_length,
590 key_buffer, key_buffer_size,
591 key_buffer_length, bits);
Ronald Cronbf33c932020-11-28 18:06:53 +0100592
593 default:
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100594 /* Importing a key with external storage in not yet supported.
595 * Return in error indicating that the lifetime is not valid. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100596 (void) status;
597 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cronbf33c932020-11-28 18:06:53 +0100598 }
599
Steven Cooreman04524762020-10-13 17:43:44 +0200600}
601
Ronald Cron67227982020-11-26 15:16:05 +0100602psa_status_t psa_driver_wrapper_export_key(
603 const psa_key_attributes_t *attributes,
604 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100605 uint8_t *data, size_t data_size, size_t *data_length)
Ronald Cron67227982020-11-26 15:16:05 +0100606
607{
608 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
609 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100610 psa_get_key_lifetime(attributes));
Ronald Cron67227982020-11-26 15:16:05 +0100611
Ronald Cron152e3f82020-11-26 16:06:41 +0100612 /* Try dynamically-registered SE interface first */
613#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
614 const psa_drv_se_t *drv;
615 psa_drv_se_context_t *drv_context;
616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100617 if (psa_get_se_driver(attributes->core.lifetime, &drv, &drv_context)) {
618 if ((drv->key_management == NULL) ||
619 (drv->key_management->p_export == NULL)) {
620 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100621 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100622
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100623 return drv->key_management->p_export(
624 drv_context,
625 *((psa_key_slot_number_t *) key_buffer),
626 data, data_size, data_length);
Ronald Cron152e3f82020-11-26 16:06:41 +0100627 }
Pengyu Lvf5131972022-11-08 18:17:00 +0800628#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Ronald Cron152e3f82020-11-26 16:06:41 +0100629
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100630 switch (location) {
Ronald Cron67227982020-11-26 15:16:05 +0100631 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100632 return psa_export_key_internal(attributes,
633 key_buffer,
634 key_buffer_size,
635 data,
636 data_size,
637 data_length);
Ronald Cron67227982020-11-26 15:16:05 +0100638
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100639 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100640#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
641#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100642 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100643 return mbedtls_test_opaque_export_key(attributes,
644 key_buffer,
645 key_buffer_size,
646 data,
647 data_size,
648 data_length);
Ronald Cron67227982020-11-26 15:16:05 +0100649#endif /* PSA_CRYPTO_DRIVER_TEST */
650#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
651 default:
652 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100653 return status;
Ronald Cron67227982020-11-26 15:16:05 +0100654 }
655}
656
Ronald Cron84cc9942020-11-25 14:30:05 +0100657psa_status_t psa_driver_wrapper_export_public_key(
658 const psa_key_attributes_t *attributes,
659 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100660 uint8_t *data, size_t data_size, size_t *data_length)
Ronald Cron84cc9942020-11-25 14:30:05 +0100661
Steven Cooremanb9b84422020-10-14 14:39:20 +0200662{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200663 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100664 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100665 psa_get_key_lifetime(attributes));
Steven Cooremanb9b84422020-10-14 14:39:20 +0200666
Ronald Cron152e3f82020-11-26 16:06:41 +0100667 /* Try dynamically-registered SE interface first */
668#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
669 const psa_drv_se_t *drv;
670 psa_drv_se_context_t *drv_context;
671
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100672 if (psa_get_se_driver(attributes->core.lifetime, &drv, &drv_context)) {
673 if ((drv->key_management == NULL) ||
674 (drv->key_management->p_export_public == NULL)) {
675 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100676 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100678 return drv->key_management->p_export_public(
679 drv_context,
680 *((psa_key_slot_number_t *) key_buffer),
681 data, data_size, data_length);
Ronald Cron152e3f82020-11-26 16:06:41 +0100682 }
683#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
684
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100685 switch (location) {
Steven Cooremanb9b84422020-10-14 14:39:20 +0200686 case PSA_KEY_LOCATION_LOCAL_STORAGE:
687 /* Key is stored in the slot in export representation, so
688 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100689#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200690#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200691 status = mbedtls_test_transparent_export_public_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100692 attributes,
693 key_buffer,
694 key_buffer_size,
695 data,
696 data_size,
697 data_length);
Steven Cooremanb9b84422020-10-14 14:39:20 +0200698 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100699 if (status != PSA_ERROR_NOT_SUPPORTED) {
700 return status;
701 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200702#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100703#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200704 /* Fell through, meaning no accelerator supports this operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100705 return psa_export_public_key_internal(attributes,
706 key_buffer,
707 key_buffer_size,
708 data,
709 data_size,
710 data_length);
Ronald Cron67227982020-11-26 15:16:05 +0100711
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100712 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100713#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200714#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100715 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100716 return mbedtls_test_opaque_export_public_key(attributes,
717 key_buffer,
718 key_buffer_size,
719 data,
720 data_size,
721 data_length);
Steven Cooremanb9b84422020-10-14 14:39:20 +0200722#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100723#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200724 default:
725 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100726 return status;
Steven Cooremanb9b84422020-10-14 14:39:20 +0200727 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200728}
729
Steven Cooreman6801f082021-02-19 17:21:22 +0100730psa_status_t psa_driver_wrapper_get_builtin_key(
731 psa_drv_slot_number_t slot_number,
732 psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100733 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Steven Cooreman6801f082021-02-19 17:21:22 +0100734{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100735 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
736 switch (location) {
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100737#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100738 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100739 return mbedtls_test_opaque_get_builtin_key(
740 slot_number,
741 attributes,
742 key_buffer, key_buffer_size, key_buffer_length);
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100743#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman6801f082021-02-19 17:21:22 +0100744 default:
745 (void) slot_number;
746 (void) key_buffer;
747 (void) key_buffer_size;
748 (void) key_buffer_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100749 return PSA_ERROR_DOES_NOT_EXIST;
Steven Cooreman6801f082021-02-19 17:21:22 +0100750 }
751}
752
Steven Cooreman37941cb2020-07-28 18:49:51 +0200753/*
754 * Cipher functions
755 */
756psa_status_t psa_driver_wrapper_cipher_encrypt(
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100757 const psa_key_attributes_t *attributes,
758 const uint8_t *key_buffer,
759 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200760 psa_algorithm_t alg,
Ronald Crona8331692021-07-09 09:19:35 +0200761 const uint8_t *iv,
762 size_t iv_length,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200763 const uint8_t *input,
764 size_t input_length,
765 uint8_t *output,
766 size_t output_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100767 size_t *output_length)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200768{
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100769 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
770 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100771 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200772
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100773 switch (location) {
Steven Cooreman37941cb2020-07-28 18:49:51 +0200774 case PSA_KEY_LOCATION_LOCAL_STORAGE:
775 /* Key is stored in the slot in export representation, so
776 * cycle through all known transparent accelerators */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100777#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200778#if defined(PSA_CRYPTO_DRIVER_TEST)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100779 status = mbedtls_test_transparent_cipher_encrypt(attributes,
780 key_buffer,
781 key_buffer_size,
782 alg,
783 iv,
784 iv_length,
785 input,
786 input_length,
787 output,
788 output_size,
789 output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200790 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100791 if (status != PSA_ERROR_NOT_SUPPORTED) {
792 return status;
793 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200794#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100795#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
796
797#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100798 return mbedtls_psa_cipher_encrypt(attributes,
799 key_buffer,
800 key_buffer_size,
801 alg,
802 iv,
803 iv_length,
804 input,
805 input_length,
806 output,
807 output_size,
808 output_length);
gabor-mezei-armd52c8dc2021-06-25 15:43:32 +0200809#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100810 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armd52c8dc2021-06-25 15:43:32 +0200811#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100812
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100813 /* Add cases for opaque driver here */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100814#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200815#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100816 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100817 return mbedtls_test_opaque_cipher_encrypt(attributes,
818 key_buffer,
819 key_buffer_size,
820 alg,
821 iv,
822 iv_length,
823 input,
824 input_length,
825 output,
826 output_size,
827 output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200828#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100829#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
830
Steven Cooreman37941cb2020-07-28 18:49:51 +0200831 default:
832 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100833 (void) status;
834 (void) key_buffer;
835 (void) key_buffer_size;
836 (void) alg;
837 (void) iv;
838 (void) iv_length;
839 (void) input;
840 (void) input_length;
841 (void) output;
842 (void) output_size;
843 (void) output_length;
844 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200845 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200846}
847
848psa_status_t psa_driver_wrapper_cipher_decrypt(
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100849 const psa_key_attributes_t *attributes,
850 const uint8_t *key_buffer,
851 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200852 psa_algorithm_t alg,
853 const uint8_t *input,
854 size_t input_length,
855 uint8_t *output,
856 size_t output_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100857 size_t *output_length)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200858{
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100859 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
860 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100861 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200862
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100863 switch (location) {
Steven Cooreman37941cb2020-07-28 18:49:51 +0200864 case PSA_KEY_LOCATION_LOCAL_STORAGE:
865 /* Key is stored in the slot in export representation, so
866 * cycle through all known transparent accelerators */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100867#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200868#if defined(PSA_CRYPTO_DRIVER_TEST)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100869 status = mbedtls_test_transparent_cipher_decrypt(attributes,
870 key_buffer,
871 key_buffer_size,
872 alg,
873 input,
874 input_length,
875 output,
876 output_size,
877 output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200878 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100879 if (status != PSA_ERROR_NOT_SUPPORTED) {
880 return status;
881 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200882#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100883#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
884
885#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100886 return mbedtls_psa_cipher_decrypt(attributes,
887 key_buffer,
888 key_buffer_size,
889 alg,
890 input,
891 input_length,
892 output,
893 output_size,
894 output_length);
gabor-mezei-armd52c8dc2021-06-25 15:43:32 +0200895#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100896 return PSA_ERROR_NOT_SUPPORTED;
gabor-mezei-armd52c8dc2021-06-25 15:43:32 +0200897#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100898
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100899 /* Add cases for opaque driver here */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100900#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200901#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100902 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100903 return mbedtls_test_opaque_cipher_decrypt(attributes,
904 key_buffer,
905 key_buffer_size,
906 alg,
907 input,
908 input_length,
909 output,
910 output_size,
911 output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200912#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100913#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
914
Steven Cooreman37941cb2020-07-28 18:49:51 +0200915 default:
916 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100917 (void) status;
918 (void) key_buffer;
919 (void) key_buffer_size;
920 (void) alg;
921 (void) input;
922 (void) input_length;
923 (void) output;
924 (void) output_size;
925 (void) output_length;
926 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200927 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200928}
929
930psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100931 psa_cipher_operation_t *operation,
932 const psa_key_attributes_t *attributes,
933 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100934 psa_algorithm_t alg)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200935{
Ronald Cron0b805592020-12-14 18:08:20 +0100936 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona4af55f2020-12-14 14:36:06 +0100937 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100938 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200939
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100940 switch (location) {
Steven Cooreman37941cb2020-07-28 18:49:51 +0200941 case PSA_KEY_LOCATION_LOCAL_STORAGE:
942 /* Key is stored in the slot in export representation, so
943 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100944#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200945#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200946 status = mbedtls_test_transparent_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100947 &operation->ctx.transparent_test_driver_ctx,
948 attributes,
949 key_buffer,
950 key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100951 alg);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200952 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100953 if (status == PSA_SUCCESS) {
Ronald Cron49fafa92021-03-10 08:34:23 +0100954 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100955 }
Steven Cooreman150c99b2020-09-09 14:32:44 +0200956
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100957 if (status != PSA_ERROR_NOT_SUPPORTED) {
958 return status;
959 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200960#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100961#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100962#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200963 /* Fell through, meaning no accelerator supports this operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100964 status = mbedtls_psa_cipher_encrypt_setup(&operation->ctx.mbedtls_ctx,
965 attributes,
966 key_buffer,
967 key_buffer_size,
968 alg);
969 if (status == PSA_SUCCESS) {
Ronald Cron6e412a72021-03-10 09:58:47 +0100970 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100971 }
Ronald Cron49fafa92021-03-10 08:34:23 +0100972
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100973 if (status != PSA_ERROR_NOT_SUPPORTED) {
974 return status;
975 }
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100976#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100977 return PSA_ERROR_NOT_SUPPORTED;
Ronald Cron0b805592020-12-14 18:08:20 +0100978
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100979 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100980#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200981#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100982 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200983 status = mbedtls_test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100984 &operation->ctx.opaque_test_driver_ctx,
985 attributes,
986 key_buffer, key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100987 alg);
Steven Cooreman37941cb2020-07-28 18:49:51 +0200988
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100989 if (status == PSA_SUCCESS) {
Ronald Cron49fafa92021-03-10 08:34:23 +0100990 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100991 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200992
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100993 return status;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200994#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100995#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +0200996 default:
997 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100998 (void) status;
999 (void) operation;
1000 (void) key_buffer;
1001 (void) key_buffer_size;
1002 (void) alg;
1003 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001004 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001005}
1006
1007psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +01001008 psa_cipher_operation_t *operation,
1009 const psa_key_attributes_t *attributes,
1010 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001011 psa_algorithm_t alg)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001012{
Steven Cooreman37941cb2020-07-28 18:49:51 +02001013 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crona4af55f2020-12-14 14:36:06 +01001014 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001015 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Steven Cooreman37941cb2020-07-28 18:49:51 +02001016
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001017 switch (location) {
Steven Cooreman37941cb2020-07-28 18:49:51 +02001018 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1019 /* Key is stored in the slot in export representation, so
1020 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +01001021#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001022#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001023 status = mbedtls_test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001024 &operation->ctx.transparent_test_driver_ctx,
1025 attributes,
1026 key_buffer,
1027 key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001028 alg);
Steven Cooreman37941cb2020-07-28 18:49:51 +02001029 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001030 if (status == PSA_SUCCESS) {
Ronald Cron49fafa92021-03-10 08:34:23 +01001031 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001032 }
Steven Cooreman150c99b2020-09-09 14:32:44 +02001033
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001034 if (status != PSA_ERROR_NOT_SUPPORTED) {
1035 return status;
1036 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001037#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001038#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001039#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001040 /* Fell through, meaning no accelerator supports this operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001041 status = mbedtls_psa_cipher_decrypt_setup(&operation->ctx.mbedtls_ctx,
1042 attributes,
1043 key_buffer,
1044 key_buffer_size,
1045 alg);
1046 if (status == PSA_SUCCESS) {
Ronald Cron49fafa92021-03-10 08:34:23 +01001047 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001048 }
Ronald Cron49fafa92021-03-10 08:34:23 +01001049
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001050 return status;
Sergey26f53f12023-02-11 18:20:20 -07001051#else /* MBEDTLS_PSA_BUILTIN_CIPHER */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001052 return PSA_ERROR_NOT_SUPPORTED;
Sergey26f53f12023-02-11 18:20:20 -07001053#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +01001054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001055 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +01001056#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001057#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +01001058 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001059 status = mbedtls_test_opaque_cipher_decrypt_setup(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001060 &operation->ctx.opaque_test_driver_ctx,
1061 attributes,
1062 key_buffer, key_buffer_size,
1063 alg);
Steven Cooreman37941cb2020-07-28 18:49:51 +02001064
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001065 if (status == PSA_SUCCESS) {
Ronald Cron49fafa92021-03-10 08:34:23 +01001066 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001067 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001068
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001069 return status;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001070#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001071#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +02001072 default:
1073 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001074 (void) status;
1075 (void) operation;
1076 (void) key_buffer;
1077 (void) key_buffer_size;
1078 (void) alg;
1079 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001080 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001081}
1082
Steven Cooreman37941cb2020-07-28 18:49:51 +02001083psa_status_t psa_driver_wrapper_cipher_set_iv(
Ronald Cron6056fe82020-12-15 13:58:07 +01001084 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001085 const uint8_t *iv,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001086 size_t iv_length)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001087{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001088 switch (operation->id) {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001089#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001090 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001091 return mbedtls_psa_cipher_set_iv(&operation->ctx.mbedtls_ctx,
1092 iv,
1093 iv_length);
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001094#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001095
1096#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001097#if defined(PSA_CRYPTO_DRIVER_TEST)
1098 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001099 return mbedtls_test_transparent_cipher_set_iv(
1100 &operation->ctx.transparent_test_driver_ctx,
1101 iv, iv_length);
Ronald Cron49fafa92021-03-10 08:34:23 +01001102
Steven Cooreman37941cb2020-07-28 18:49:51 +02001103 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001104 return mbedtls_test_opaque_cipher_set_iv(
1105 &operation->ctx.opaque_test_driver_ctx,
1106 iv, iv_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +02001107#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001108#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001109 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001110
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001111 (void) iv;
1112 (void) iv_length;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001114 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001115}
1116
1117psa_status_t psa_driver_wrapper_cipher_update(
Ronald Cron6056fe82020-12-15 13:58:07 +01001118 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001119 const uint8_t *input,
1120 size_t input_length,
1121 uint8_t *output,
1122 size_t output_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001123 size_t *output_length)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001124{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001125 switch (operation->id) {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001126#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001127 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001128 return mbedtls_psa_cipher_update(&operation->ctx.mbedtls_ctx,
1129 input,
1130 input_length,
1131 output,
1132 output_size,
1133 output_length);
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001134#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1135
Ronald Cron49fafa92021-03-10 08:34:23 +01001136#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001137#if defined(PSA_CRYPTO_DRIVER_TEST)
1138 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001139 return mbedtls_test_transparent_cipher_update(
1140 &operation->ctx.transparent_test_driver_ctx,
1141 input, input_length,
1142 output, output_size, output_length);
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001143
Steven Cooreman37941cb2020-07-28 18:49:51 +02001144 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001145 return mbedtls_test_opaque_cipher_update(
1146 &operation->ctx.opaque_test_driver_ctx,
1147 input, input_length,
1148 output, output_size, output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +02001149#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001150#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001151 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001153 (void) input;
1154 (void) input_length;
1155 (void) output;
1156 (void) output_size;
1157 (void) output_length;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001159 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001160}
1161
1162psa_status_t psa_driver_wrapper_cipher_finish(
Ronald Cron6056fe82020-12-15 13:58:07 +01001163 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001164 uint8_t *output,
1165 size_t output_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001166 size_t *output_length)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001167{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001168 switch (operation->id) {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001169#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001170 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001171 return mbedtls_psa_cipher_finish(&operation->ctx.mbedtls_ctx,
1172 output,
1173 output_size,
1174 output_length);
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001175#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001176
1177#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001178#if defined(PSA_CRYPTO_DRIVER_TEST)
1179 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001180 return mbedtls_test_transparent_cipher_finish(
1181 &operation->ctx.transparent_test_driver_ctx,
1182 output, output_size, output_length);
Ronald Cron49fafa92021-03-10 08:34:23 +01001183
Steven Cooreman37941cb2020-07-28 18:49:51 +02001184 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001185 return mbedtls_test_opaque_cipher_finish(
1186 &operation->ctx.opaque_test_driver_ctx,
1187 output, output_size, output_length);
Steven Cooreman37941cb2020-07-28 18:49:51 +02001188#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001189#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001190 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001192 (void) output;
1193 (void) output_size;
1194 (void) output_length;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001196 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001197}
1198
1199psa_status_t psa_driver_wrapper_cipher_abort(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001200 psa_cipher_operation_t *operation)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001201{
Ronald Crondd24c9b2020-12-15 14:10:01 +01001202 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001204 switch (operation->id) {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001205#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001206 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001207 return mbedtls_psa_cipher_abort(&operation->ctx.mbedtls_ctx);
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001208#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +01001209
1210#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001211#if defined(PSA_CRYPTO_DRIVER_TEST)
1212 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001213 status = mbedtls_test_transparent_cipher_abort(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001214 &operation->ctx.transparent_test_driver_ctx);
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001215 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001216 &operation->ctx.transparent_test_driver_ctx,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001217 sizeof(operation->ctx.transparent_test_driver_ctx));
1218 return status;
Ronald Cron49fafa92021-03-10 08:34:23 +01001219
Steven Cooreman37941cb2020-07-28 18:49:51 +02001220 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001221 status = mbedtls_test_opaque_cipher_abort(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001222 &operation->ctx.opaque_test_driver_ctx);
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001223 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001224 &operation->ctx.opaque_test_driver_ctx,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001225 sizeof(operation->ctx.opaque_test_driver_ctx));
1226 return status;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001227#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001228#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001229 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001231 (void) status;
1232 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001233}
1234
Steven Cooreman1e582352021-02-18 17:24:37 +01001235/*
1236 * Hashing functions
1237 */
1238psa_status_t psa_driver_wrapper_hash_compute(
1239 psa_algorithm_t alg,
1240 const uint8_t *input,
1241 size_t input_length,
1242 uint8_t *hash,
1243 size_t hash_size,
1244 size_t *hash_length)
1245{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001246 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001247
1248 /* Try accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001249#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001250 status = mbedtls_test_transparent_hash_compute(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001251 alg, input, input_length, hash, hash_size, hash_length);
1252 if (status != PSA_ERROR_NOT_SUPPORTED) {
1253 return status;
1254 }
Steven Cooremanf7638102021-03-04 15:14:36 +01001255#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001256
1257 /* If software fallback is compiled in, try fallback */
1258#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001259 status = mbedtls_psa_hash_compute(alg, input, input_length,
1260 hash, hash_size, hash_length);
1261 if (status != PSA_ERROR_NOT_SUPPORTED) {
1262 return status;
1263 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001264#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001265 (void) status;
1266 (void) alg;
1267 (void) input;
1268 (void) input_length;
1269 (void) hash;
1270 (void) hash_size;
1271 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001272
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001273 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001274}
1275
1276psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001277 psa_hash_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001278 psa_algorithm_t alg)
Steven Cooreman1e582352021-02-18 17:24:37 +01001279{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001280 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001281
Steven Cooreman1e582352021-02-18 17:24:37 +01001282 /* Try setup on accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001283#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001284 status = mbedtls_test_transparent_hash_setup(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001285 &operation->ctx.test_driver_ctx, alg);
1286 if (status == PSA_SUCCESS) {
Steven Cooremanf7638102021-03-04 15:14:36 +01001287 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001288 }
Steven Cooremanf7638102021-03-04 15:14:36 +01001289
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001290 if (status != PSA_ERROR_NOT_SUPPORTED) {
1291 return status;
1292 }
Steven Cooremanf7638102021-03-04 15:14:36 +01001293#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001294
1295 /* If software fallback is compiled in, try fallback */
1296#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001297 status = mbedtls_psa_hash_setup(&operation->ctx.mbedtls_ctx, alg);
1298 if (status == PSA_SUCCESS) {
Steven Cooreman1e582352021-02-18 17:24:37 +01001299 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001300 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001301
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001302 if (status != PSA_ERROR_NOT_SUPPORTED) {
1303 return status;
1304 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001305#endif
1306 /* Nothing left to try if we fall through here */
1307 (void) status;
1308 (void) operation;
1309 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001310 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001311}
1312
1313psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001314 const psa_hash_operation_t *source_operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001315 psa_hash_operation_t *target_operation)
Steven Cooreman1e582352021-02-18 17:24:37 +01001316{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001317 switch (source_operation->id) {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001318#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1319 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1320 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001321 return mbedtls_psa_hash_clone(&source_operation->ctx.mbedtls_ctx,
1322 &target_operation->ctx.mbedtls_ctx);
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001323#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001324#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001325 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1326 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001327 return mbedtls_test_transparent_hash_clone(
1328 &source_operation->ctx.test_driver_ctx,
1329 &target_operation->ctx.test_driver_ctx);
Steven Cooremanf7638102021-03-04 15:14:36 +01001330#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001331 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001332 (void) target_operation;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001333 return PSA_ERROR_BAD_STATE;
Steven Cooreman1e582352021-02-18 17:24:37 +01001334 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001335}
1336
1337psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001338 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001339 const uint8_t *input,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001340 size_t input_length)
Steven Cooreman1e582352021-02-18 17:24:37 +01001341{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001342 switch (operation->id) {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001343#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1344 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001345 return mbedtls_psa_hash_update(&operation->ctx.mbedtls_ctx,
1346 input, input_length);
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001347#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001348#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001349 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001350 return mbedtls_test_transparent_hash_update(
1351 &operation->ctx.test_driver_ctx,
1352 input, input_length);
Steven Cooremanf7638102021-03-04 15:14:36 +01001353#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001354 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001355 (void) input;
1356 (void) input_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001357 return PSA_ERROR_BAD_STATE;
Steven Cooreman1e582352021-02-18 17:24:37 +01001358 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001359}
1360
1361psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001362 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001363 uint8_t *hash,
1364 size_t hash_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001365 size_t *hash_length)
Steven Cooreman1e582352021-02-18 17:24:37 +01001366{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001367 switch (operation->id) {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001368#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1369 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001370 return mbedtls_psa_hash_finish(&operation->ctx.mbedtls_ctx,
1371 hash, hash_size, hash_length);
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001372#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001373#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001374 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001375 return mbedtls_test_transparent_hash_finish(
1376 &operation->ctx.test_driver_ctx,
1377 hash, hash_size, hash_length);
Steven Cooremanf7638102021-03-04 15:14:36 +01001378#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001379 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001380 (void) hash;
1381 (void) hash_size;
1382 (void) hash_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001383 return PSA_ERROR_BAD_STATE;
Steven Cooreman1e582352021-02-18 17:24:37 +01001384 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001385}
1386
1387psa_status_t psa_driver_wrapper_hash_abort(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001388 psa_hash_operation_t *operation)
Steven Cooreman1e582352021-02-18 17:24:37 +01001389{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001390 switch (operation->id) {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001391#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1392 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001393 return mbedtls_psa_hash_abort(&operation->ctx.mbedtls_ctx);
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001394#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001395#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001396 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001397 return mbedtls_test_transparent_hash_abort(
1398 &operation->ctx.test_driver_ctx);
Steven Cooremanf7638102021-03-04 15:14:36 +01001399#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001400 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001401 return PSA_ERROR_BAD_STATE;
Steven Cooreman1e582352021-02-18 17:24:37 +01001402 }
1403}
1404
Ronald Cronde822812021-03-17 16:08:20 +01001405psa_status_t psa_driver_wrapper_aead_encrypt(
1406 const psa_key_attributes_t *attributes,
1407 const uint8_t *key_buffer, size_t key_buffer_size,
1408 psa_algorithm_t alg,
1409 const uint8_t *nonce, size_t nonce_length,
1410 const uint8_t *additional_data, size_t additional_data_length,
1411 const uint8_t *plaintext, size_t plaintext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001412 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length)
Ronald Cronde822812021-03-17 16:08:20 +01001413{
1414 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1415 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001416 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Ronald Cronde822812021-03-17 16:08:20 +01001417
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001418 switch (location) {
Ronald Cronde822812021-03-17 16:08:20 +01001419 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1420 /* Key is stored in the slot in export representation, so
1421 * cycle through all known transparent accelerators */
1422
1423#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1424#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001425 status = mbedtls_test_transparent_aead_encrypt(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001426 attributes, key_buffer, key_buffer_size,
1427 alg,
1428 nonce, nonce_length,
1429 additional_data, additional_data_length,
1430 plaintext, plaintext_length,
1431 ciphertext, ciphertext_size, ciphertext_length);
Ronald Cronde822812021-03-17 16:08:20 +01001432 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001433 if (status != PSA_ERROR_NOT_SUPPORTED) {
1434 return status;
1435 }
Ronald Cronde822812021-03-17 16:08:20 +01001436#endif /* PSA_CRYPTO_DRIVER_TEST */
1437#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1438
1439 /* Fell through, meaning no accelerator supports this operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001440 return mbedtls_psa_aead_encrypt(
1441 attributes, key_buffer, key_buffer_size,
1442 alg,
1443 nonce, nonce_length,
1444 additional_data, additional_data_length,
1445 plaintext, plaintext_length,
1446 ciphertext, ciphertext_size, ciphertext_length);
Ronald Cronde822812021-03-17 16:08:20 +01001447
1448 /* Add cases for opaque driver here */
1449
1450 default:
1451 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001452 (void) status;
1453 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cronde822812021-03-17 16:08:20 +01001454 }
1455}
1456
1457psa_status_t psa_driver_wrapper_aead_decrypt(
1458 const psa_key_attributes_t *attributes,
1459 const uint8_t *key_buffer, size_t key_buffer_size,
1460 psa_algorithm_t alg,
1461 const uint8_t *nonce, size_t nonce_length,
1462 const uint8_t *additional_data, size_t additional_data_length,
1463 const uint8_t *ciphertext, size_t ciphertext_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001464 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length)
Ronald Cronde822812021-03-17 16:08:20 +01001465{
1466 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1467 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001468 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Ronald Cronde822812021-03-17 16:08:20 +01001469
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001470 switch (location) {
Ronald Cronde822812021-03-17 16:08:20 +01001471 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1472 /* Key is stored in the slot in export representation, so
1473 * cycle through all known transparent accelerators */
1474
1475#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1476#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001477 status = mbedtls_test_transparent_aead_decrypt(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001478 attributes, key_buffer, key_buffer_size,
1479 alg,
1480 nonce, nonce_length,
1481 additional_data, additional_data_length,
1482 ciphertext, ciphertext_length,
1483 plaintext, plaintext_size, plaintext_length);
Ronald Cronde822812021-03-17 16:08:20 +01001484 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001485 if (status != PSA_ERROR_NOT_SUPPORTED) {
1486 return status;
1487 }
Ronald Cronde822812021-03-17 16:08:20 +01001488#endif /* PSA_CRYPTO_DRIVER_TEST */
1489#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1490
1491 /* Fell through, meaning no accelerator supports this operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001492 return mbedtls_psa_aead_decrypt(
1493 attributes, key_buffer, key_buffer_size,
1494 alg,
1495 nonce, nonce_length,
1496 additional_data, additional_data_length,
1497 ciphertext, ciphertext_length,
1498 plaintext, plaintext_size, plaintext_length);
Ronald Cronde822812021-03-17 16:08:20 +01001499
1500 /* Add cases for opaque driver here */
1501
1502 default:
1503 /* Key is declared with a lifetime not known to us */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001504 (void) status;
1505 return PSA_ERROR_INVALID_ARGUMENT;
Ronald Cronde822812021-03-17 16:08:20 +01001506 }
1507}
Steven Cooreman896d51e2021-03-19 15:24:23 +01001508
1509
1510/*
1511 * MAC functions
1512 */
1513psa_status_t psa_driver_wrapper_mac_compute(
1514 const psa_key_attributes_t *attributes,
1515 const uint8_t *key_buffer,
1516 size_t key_buffer_size,
1517 psa_algorithm_t alg,
1518 const uint8_t *input,
1519 size_t input_length,
1520 uint8_t *mac,
1521 size_t mac_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001522 size_t *mac_length)
Steven Cooreman896d51e2021-03-19 15:24:23 +01001523{
1524 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1525 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001526 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001528 switch (location) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001529 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1530 /* Key is stored in the slot in export representation, so
1531 * cycle through all known transparent accelerators */
1532#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1533#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001534 status = mbedtls_test_transparent_mac_compute(
Steven Cooreman896d51e2021-03-19 15:24:23 +01001535 attributes, key_buffer, key_buffer_size, alg,
1536 input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001537 mac, mac_size, mac_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001538 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001539 if (status != PSA_ERROR_NOT_SUPPORTED) {
1540 return status;
1541 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001542#endif /* PSA_CRYPTO_DRIVER_TEST */
1543#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1544#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1545 /* Fell through, meaning no accelerator supports this operation */
1546 status = mbedtls_psa_mac_compute(
1547 attributes, key_buffer, key_buffer_size, alg,
1548 input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001549 mac, mac_size, mac_length);
1550 if (status != PSA_ERROR_NOT_SUPPORTED) {
1551 return status;
1552 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001553#endif /* MBEDTLS_PSA_BUILTIN_MAC */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001554 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001556 /* Add cases for opaque driver here */
Steven Cooreman896d51e2021-03-19 15:24:23 +01001557#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1558#if defined(PSA_CRYPTO_DRIVER_TEST)
1559 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001560 status = mbedtls_test_opaque_mac_compute(
Steven Cooreman896d51e2021-03-19 15:24:23 +01001561 attributes, key_buffer, key_buffer_size, alg,
1562 input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001563 mac, mac_size, mac_length);
1564 return status;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001565#endif /* PSA_CRYPTO_DRIVER_TEST */
1566#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1567 default:
1568 /* Key is declared with a lifetime not known to us */
1569 (void) key_buffer;
1570 (void) key_buffer_size;
1571 (void) alg;
1572 (void) input;
1573 (void) input_length;
1574 (void) mac;
1575 (void) mac_size;
1576 (void) mac_length;
1577 (void) status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001578 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001579 }
1580}
1581
1582psa_status_t psa_driver_wrapper_mac_sign_setup(
1583 psa_mac_operation_t *operation,
1584 const psa_key_attributes_t *attributes,
1585 const uint8_t *key_buffer,
1586 size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001587 psa_algorithm_t alg)
Steven Cooreman896d51e2021-03-19 15:24:23 +01001588{
1589 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1590 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001591 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001592
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001593 switch (location) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001594 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1595 /* Key is stored in the slot in export representation, so
1596 * cycle through all known transparent accelerators */
1597#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1598#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001599 status = mbedtls_test_transparent_mac_sign_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001600 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001601 attributes,
1602 key_buffer, key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001603 alg);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001604 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001605 if (status == PSA_SUCCESS) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001606 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001607 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001609 if (status != PSA_ERROR_NOT_SUPPORTED) {
1610 return status;
1611 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001612#endif /* PSA_CRYPTO_DRIVER_TEST */
1613#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1614#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1615 /* Fell through, meaning no accelerator supports this operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001616 status = mbedtls_psa_mac_sign_setup(&operation->ctx.mbedtls_ctx,
1617 attributes,
1618 key_buffer, key_buffer_size,
1619 alg);
1620 if (status == PSA_SUCCESS) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001621 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001622 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001624 if (status != PSA_ERROR_NOT_SUPPORTED) {
1625 return status;
1626 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001627#endif /* MBEDTLS_PSA_BUILTIN_MAC */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001628 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001629
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001630 /* Add cases for opaque driver here */
Steven Cooreman896d51e2021-03-19 15:24:23 +01001631#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1632#if defined(PSA_CRYPTO_DRIVER_TEST)
1633 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001634 status = mbedtls_test_opaque_mac_sign_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001635 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001636 attributes,
1637 key_buffer, key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001638 alg);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001640 if (status == PSA_SUCCESS) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001641 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001642 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001644 return status;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001645#endif /* PSA_CRYPTO_DRIVER_TEST */
1646#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1647 default:
1648 /* Key is declared with a lifetime not known to us */
1649 (void) status;
Ronald Cron485559e2021-04-28 14:29:00 +02001650 (void) operation;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001651 (void) key_buffer;
1652 (void) key_buffer_size;
1653 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001654 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001655 }
1656}
1657
1658psa_status_t psa_driver_wrapper_mac_verify_setup(
1659 psa_mac_operation_t *operation,
1660 const psa_key_attributes_t *attributes,
1661 const uint8_t *key_buffer,
1662 size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001663 psa_algorithm_t alg)
Steven Cooreman896d51e2021-03-19 15:24:23 +01001664{
1665 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1666 psa_key_location_t location =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001667 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001669 switch (location) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001670 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1671 /* Key is stored in the slot in export representation, so
1672 * cycle through all known transparent accelerators */
1673#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1674#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001675 status = mbedtls_test_transparent_mac_verify_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001676 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001677 attributes,
1678 key_buffer, key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001679 alg);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001680 /* Declared with fallback == true */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001681 if (status == PSA_SUCCESS) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001682 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001683 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001684
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001685 if (status != PSA_ERROR_NOT_SUPPORTED) {
1686 return status;
1687 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001688#endif /* PSA_CRYPTO_DRIVER_TEST */
1689#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1690#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1691 /* Fell through, meaning no accelerator supports this operation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001692 status = mbedtls_psa_mac_verify_setup(&operation->ctx.mbedtls_ctx,
1693 attributes,
1694 key_buffer, key_buffer_size,
1695 alg);
1696 if (status == PSA_SUCCESS) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001697 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001698 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001699
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001700 if (status != PSA_ERROR_NOT_SUPPORTED) {
1701 return status;
1702 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001703#endif /* MBEDTLS_PSA_BUILTIN_MAC */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001704 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001705
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001706 /* Add cases for opaque driver here */
Steven Cooreman896d51e2021-03-19 15:24:23 +01001707#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1708#if defined(PSA_CRYPTO_DRIVER_TEST)
1709 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001710 status = mbedtls_test_opaque_mac_verify_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001711 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001712 attributes,
1713 key_buffer, key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001714 alg);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001715
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001716 if (status == PSA_SUCCESS) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001717 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001718 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001719
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001720 return status;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001721#endif /* PSA_CRYPTO_DRIVER_TEST */
1722#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1723 default:
1724 /* Key is declared with a lifetime not known to us */
1725 (void) status;
Ronald Cron485559e2021-04-28 14:29:00 +02001726 (void) operation;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001727 (void) key_buffer;
1728 (void) key_buffer_size;
1729 (void) alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001730 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001731 }
1732}
1733
1734psa_status_t psa_driver_wrapper_mac_update(
1735 psa_mac_operation_t *operation,
1736 const uint8_t *input,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001737 size_t input_length)
Steven Cooreman896d51e2021-03-19 15:24:23 +01001738{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001739 switch (operation->id) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001740#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1741 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001742 return mbedtls_psa_mac_update(&operation->ctx.mbedtls_ctx,
1743 input, input_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001744#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1745
1746#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1747#if defined(PSA_CRYPTO_DRIVER_TEST)
1748 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001749 return mbedtls_test_transparent_mac_update(
1750 &operation->ctx.transparent_test_driver_ctx,
1751 input, input_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001752
1753 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001754 return mbedtls_test_opaque_mac_update(
1755 &operation->ctx.opaque_test_driver_ctx,
1756 input, input_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001757#endif /* PSA_CRYPTO_DRIVER_TEST */
1758#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1759 default:
1760 (void) input;
1761 (void) input_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001762 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001763 }
1764}
1765
1766psa_status_t psa_driver_wrapper_mac_sign_finish(
1767 psa_mac_operation_t *operation,
1768 uint8_t *mac,
1769 size_t mac_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001770 size_t *mac_length)
Steven Cooreman896d51e2021-03-19 15:24:23 +01001771{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001772 switch (operation->id) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001773#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1774 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001775 return mbedtls_psa_mac_sign_finish(&operation->ctx.mbedtls_ctx,
1776 mac, mac_size, mac_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001777#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1778
1779#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1780#if defined(PSA_CRYPTO_DRIVER_TEST)
1781 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001782 return mbedtls_test_transparent_mac_sign_finish(
1783 &operation->ctx.transparent_test_driver_ctx,
1784 mac, mac_size, mac_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001785
1786 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001787 return mbedtls_test_opaque_mac_sign_finish(
1788 &operation->ctx.opaque_test_driver_ctx,
1789 mac, mac_size, mac_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001790#endif /* PSA_CRYPTO_DRIVER_TEST */
1791#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1792 default:
1793 (void) mac;
1794 (void) mac_size;
1795 (void) mac_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001796 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001797 }
1798}
1799
1800psa_status_t psa_driver_wrapper_mac_verify_finish(
1801 psa_mac_operation_t *operation,
1802 const uint8_t *mac,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001803 size_t mac_length)
Steven Cooreman896d51e2021-03-19 15:24:23 +01001804{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001805 switch (operation->id) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001806#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1807 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001808 return mbedtls_psa_mac_verify_finish(&operation->ctx.mbedtls_ctx,
1809 mac, mac_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001810#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1811
1812#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1813#if defined(PSA_CRYPTO_DRIVER_TEST)
1814 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001815 return mbedtls_test_transparent_mac_verify_finish(
1816 &operation->ctx.transparent_test_driver_ctx,
1817 mac, mac_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001818
1819 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001820 return mbedtls_test_opaque_mac_verify_finish(
1821 &operation->ctx.opaque_test_driver_ctx,
1822 mac, mac_length);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001823#endif /* PSA_CRYPTO_DRIVER_TEST */
1824#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1825 default:
1826 (void) mac;
1827 (void) mac_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001828 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001829 }
1830}
1831
1832psa_status_t psa_driver_wrapper_mac_abort(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001833 psa_mac_operation_t *operation)
Steven Cooreman896d51e2021-03-19 15:24:23 +01001834{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001835 switch (operation->id) {
Steven Cooreman896d51e2021-03-19 15:24:23 +01001836#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1837 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001838 return mbedtls_psa_mac_abort(&operation->ctx.mbedtls_ctx);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001839#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1840
1841#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1842#if defined(PSA_CRYPTO_DRIVER_TEST)
1843 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001844 return mbedtls_test_transparent_mac_abort(
1845 &operation->ctx.transparent_test_driver_ctx);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001846 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001847 return mbedtls_test_opaque_mac_abort(
1848 &operation->ctx.opaque_test_driver_ctx);
Steven Cooreman896d51e2021-03-19 15:24:23 +01001849#endif /* PSA_CRYPTO_DRIVER_TEST */
1850#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1851 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001852 return PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman896d51e2021-03-19 15:24:23 +01001853 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001854}
Gilles Peskinead0e0122021-04-24 13:19:45 +02001855
1856#endif /* MBEDTLS_PSA_CRYPTO_C */