blob: 09fff0c6bd7bda64f0de35a224b9b5b5b36b427d [file] [log] [blame]
Steven Cooremancd84cb42020-07-16 20:28:36 +02001/*
2 * Functions to delegate cryptographic operations to an available
3 * and appropriate accelerator.
Steven Cooreman56250fd2020-09-04 13:07:15 +02004 * Warning: This file will be auto-generated in the future.
Steven Cooremancd84cb42020-07-16 20:28:36 +02005 */
Steven Cooreman2c7b2f82020-09-02 13:43:46 +02006/* Copyright The Mbed TLS Contributors
Steven Cooremancd84cb42020-07-16 20:28:36 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Steven Cooremancd84cb42020-07-16 20:28:36 +020020 */
21
Ronald Cronde822812021-03-17 16:08:20 +010022#include "psa_crypto_aead.h"
Ronald Cron0b805592020-12-14 18:08:20 +010023#include "psa_crypto_cipher.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020024#include "psa_crypto_core.h"
25#include "psa_crypto_driver_wrappers.h"
Steven Cooreman830aff22021-03-09 09:50:44 +010026#include "psa_crypto_hash.h"
Steven Cooremand13a70f2021-03-19 15:24:23 +010027#include "psa_crypto_mac.h"
Steven Cooreman830aff22021-03-09 09:50:44 +010028
Steven Cooreman2a1664c2020-07-20 15:33:08 +020029#include "mbedtls/platform.h"
30
Gilles Peskine1905a242021-04-24 13:19:45 +020031#if defined(MBEDTLS_PSA_CRYPTO_C)
32
Steven Cooreman2a1664c2020-07-20 15:33:08 +020033#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
Steven Cooremancd84cb42020-07-16 20:28:36 +020034
35/* Include test driver definition when running tests */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020036#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman56250fd2020-09-04 13:07:15 +020037#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020038#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020039#endif
40#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020041#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020042#endif
Ronald Cronce1d8d22021-04-30 17:00:34 +020043#include "test/drivers/test_driver.h"
Steven Cooremanf1720ea2020-07-24 18:41:58 +020044#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +020045
Steven Cooreman56250fd2020-09-04 13:07:15 +020046/* Repeat above block for each JSON-declared driver during autogeneration */
Steven Cooremanaa87fd02021-03-15 18:54:03 +010047#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Steven Cooreman56250fd2020-09-04 13:07:15 +020048
Steven Cooreman1e582352021-02-18 17:24:37 +010049/* Auto-generated values depending on which drivers are registered.
50 * ID 0 is reserved for unallocated operations.
51 * ID 1 is reserved for the Mbed TLS software driver. */
Steven Cooreman830aff22021-03-09 09:50:44 +010052#define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
53
Steven Cooreman37941cb2020-07-28 18:49:51 +020054#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman1e582352021-02-18 17:24:37 +010055#define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2)
56#define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3)
Steven Cooreman37941cb2020-07-28 18:49:51 +020057#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +020058
59/* Support the 'old' SE interface when asked to */
Steven Cooreman7a250572020-07-17 16:43:05 +020060#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman56250fd2020-09-04 13:07:15 +020061/* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
62 * SE driver is present, to avoid unused argument errors at compile time. */
63#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020064#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020065#endif
Steven Cooreman7a250572020-07-17 16:43:05 +020066#include "psa_crypto_se.h"
67#endif
68
Steven Cooremancd84cb42020-07-16 20:28:36 +020069/* Start delegation functions */
gabor-mezei-arm50eac352021-04-22 11:32:19 +020070psa_status_t psa_driver_wrapper_sign_message(
71 const psa_key_attributes_t *attributes,
72 const uint8_t *key_buffer,
73 size_t key_buffer_size,
74 psa_algorithm_t alg,
75 const uint8_t *input,
76 size_t input_length,
77 uint8_t *signature,
78 size_t signature_size,
79 size_t *signature_length )
80{
81 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +020082 psa_key_location_t location =
83 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
84
85 switch( location )
86 {
87 case PSA_KEY_LOCATION_LOCAL_STORAGE:
88 /* Key is stored in the slot in export representation, so
89 * cycle through all known transparent accelerators */
90#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
91#if defined(PSA_CRYPTO_DRIVER_TEST)
92 status = mbedtls_test_transparent_signature_sign_message(
93 attributes,
94 key_buffer,
95 key_buffer_size,
96 alg,
97 input,
98 input_length,
99 signature,
100 signature_size,
101 signature_length );
102 /* Declared with fallback == true */
103 if( status != PSA_ERROR_NOT_SUPPORTED )
104 return( status );
105#endif /* PSA_CRYPTO_DRIVER_TEST */
106#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200107 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200108
109 /* Add cases for opaque driver here */
110#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
111#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arm2522c0b2021-04-27 18:52:51 +0200112 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200113 status = mbedtls_test_opaque_signature_sign_message(
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200114 attributes,
115 key_buffer,
116 key_buffer_size,
117 alg,
118 input,
119 input_length,
120 signature,
121 signature_size,
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200122 signature_length );
123 if( status != PSA_ERROR_NOT_SUPPORTED )
124 return( status );
125 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200126#endif /* PSA_CRYPTO_DRIVER_TEST */
127#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
128 default:
129 /* Key is declared with a lifetime not known to us */
130 (void)status;
gabor-mezei-armd5218df2021-05-12 11:12:25 +0200131 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200132 }
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200133
134 return( psa_sign_message_builtin( attributes,
135 key_buffer,
136 key_buffer_size,
137 alg,
138 input,
139 input_length,
140 signature,
141 signature_size,
142 signature_length ) );
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200143}
144
145psa_status_t psa_driver_wrapper_verify_message(
146 const psa_key_attributes_t *attributes,
147 const uint8_t *key_buffer,
148 size_t key_buffer_size,
149 psa_algorithm_t alg,
150 const uint8_t *input,
151 size_t input_length,
152 const uint8_t *signature,
153 size_t signature_length )
154{
155 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200156 psa_key_location_t location =
157 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
158
159 switch( location )
160 {
161 case PSA_KEY_LOCATION_LOCAL_STORAGE:
162 /* Key is stored in the slot in export representation, so
163 * cycle through all known transparent accelerators */
164#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
165#if defined(PSA_CRYPTO_DRIVER_TEST)
166 status = mbedtls_test_transparent_signature_verify_message(
167 attributes,
168 key_buffer,
169 key_buffer_size,
170 alg,
171 input,
172 input_length,
173 signature,
174 signature_length );
175 /* Declared with fallback == true */
176 if( status != PSA_ERROR_NOT_SUPPORTED )
177 return( status );
178#endif /* PSA_CRYPTO_DRIVER_TEST */
179#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200180 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200181
182 /* Add cases for opaque driver here */
183#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
184#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arm2522c0b2021-04-27 18:52:51 +0200185 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200186 return( mbedtls_test_opaque_signature_verify_message(
187 attributes,
188 key_buffer,
189 key_buffer_size,
190 alg,
191 input,
192 input_length,
193 signature,
194 signature_length ) );
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200195 if( status != PSA_ERROR_NOT_SUPPORTED )
196 return( status );
197 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200198#endif /* PSA_CRYPTO_DRIVER_TEST */
199#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
200 default:
201 /* Key is declared with a lifetime not known to us */
202 (void)status;
gabor-mezei-armd5218df2021-05-12 11:12:25 +0200203 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200204 }
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200205
206 return( psa_verify_message_builtin( attributes,
207 key_buffer,
208 key_buffer_size,
209 alg,
210 input,
211 input_length,
212 signature,
213 signature_length ) );
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200214}
215
Ronald Cron9f17aa42020-12-08 17:07:25 +0100216psa_status_t psa_driver_wrapper_sign_hash(
217 const psa_key_attributes_t *attributes,
218 const uint8_t *key_buffer, size_t key_buffer_size,
219 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
220 uint8_t *signature, size_t signature_size, size_t *signature_length )
Steven Cooremancd84cb42020-07-16 20:28:36 +0200221{
Steven Cooreman7a250572020-07-17 16:43:05 +0200222 /* Try dynamically-registered SE interface first */
223#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
224 const psa_drv_se_t *drv;
225 psa_drv_se_context_t *drv_context;
226
Ronald Cron9f17aa42020-12-08 17:07:25 +0100227 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman7a250572020-07-17 16:43:05 +0200228 {
229 if( drv->asymmetric == NULL ||
230 drv->asymmetric->p_sign == NULL )
231 {
232 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200233 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman7a250572020-07-17 16:43:05 +0200234 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100235 return( drv->asymmetric->p_sign(
236 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
237 alg, hash, hash_length,
238 signature, signature_size, signature_length ) );
Steven Cooreman7a250572020-07-17 16:43:05 +0200239 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200240#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman7a250572020-07-17 16:43:05 +0200241
Ronald Cronfce9df22020-12-08 18:06:03 +0100242 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100243 psa_key_location_t location =
244 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200245
246 switch( location )
247 {
248 case PSA_KEY_LOCATION_LOCAL_STORAGE:
249 /* Key is stored in the slot in export representation, so
250 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100251#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200252#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200253 status = mbedtls_test_transparent_signature_sign_hash( attributes,
Ronald Cron9f17aa42020-12-08 17:07:25 +0100254 key_buffer,
255 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200256 alg,
257 hash,
258 hash_length,
259 signature,
260 signature_size,
261 signature_length );
262 /* Declared with fallback == true */
263 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200264 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200265#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100266#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200267 /* Fell through, meaning no accelerator supports this operation */
gabor-mezei-arm6883fd22021-05-05 14:18:36 +0200268 return( psa_sign_hash_builtin( attributes,
269 key_buffer,
270 key_buffer_size,
271 alg,
272 hash,
273 hash_length,
274 signature,
275 signature_size,
276 signature_length ) );
Ronald Cronfce9df22020-12-08 18:06:03 +0100277
Steven Cooremancd84cb42020-07-16 20:28:36 +0200278 /* Add cases for opaque driver here */
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)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100281 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200282 return( mbedtls_test_opaque_signature_sign_hash( attributes,
283 key_buffer,
284 key_buffer_size,
285 alg,
286 hash,
287 hash_length,
288 signature,
289 signature_size,
290 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200291#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100292#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200293 default:
294 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100295 (void)status;
296 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200297 }
Steven Cooremancd84cb42020-07-16 20:28:36 +0200298}
299
Ronald Cron9f17aa42020-12-08 17:07:25 +0100300psa_status_t psa_driver_wrapper_verify_hash(
301 const psa_key_attributes_t *attributes,
302 const uint8_t *key_buffer, size_t key_buffer_size,
303 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
304 const uint8_t *signature, size_t signature_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200305{
Steven Cooreman55ae2172020-07-17 19:46:15 +0200306 /* Try dynamically-registered SE interface first */
307#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
308 const psa_drv_se_t *drv;
309 psa_drv_se_context_t *drv_context;
310
Ronald Cron9f17aa42020-12-08 17:07:25 +0100311 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200312 {
313 if( drv->asymmetric == NULL ||
314 drv->asymmetric->p_verify == NULL )
315 {
316 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200317 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200318 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100319 return( drv->asymmetric->p_verify(
320 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
321 alg, hash, hash_length,
322 signature, signature_length ) );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200323 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200324#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200325
Ronald Cronfce9df22020-12-08 18:06:03 +0100326 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100327 psa_key_location_t location =
328 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200329
330 switch( location )
331 {
332 case PSA_KEY_LOCATION_LOCAL_STORAGE:
333 /* Key is stored in the slot in export representation, so
334 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100335#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200336#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200337 status = mbedtls_test_transparent_signature_verify_hash(
338 attributes,
339 key_buffer,
340 key_buffer_size,
341 alg,
342 hash,
343 hash_length,
344 signature,
345 signature_length );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200346 /* Declared with fallback == true */
347 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200348 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200349#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100350#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
351
gabor-mezei-arm6883fd22021-05-05 14:18:36 +0200352 return( psa_verify_hash_builtin( attributes,
353 key_buffer,
354 key_buffer_size,
355 alg,
356 hash,
357 hash_length,
358 signature,
359 signature_length ) );
Ronald Cronfce9df22020-12-08 18:06:03 +0100360
Steven Cooreman55ae2172020-07-17 19:46:15 +0200361 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100362#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200363#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100364 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200365 return( mbedtls_test_opaque_signature_verify_hash( attributes,
366 key_buffer,
367 key_buffer_size,
368 alg,
369 hash,
370 hash_length,
371 signature,
372 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200373#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100374#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200375 default:
376 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100377 (void)status;
378 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200379 }
Steven Cooreman55ae2172020-07-17 19:46:15 +0200380}
381
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200382/** Get the key buffer size required to store the key material of a key
383 * associated with an opaque driver without storage.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200384 *
Ronald Cron31216282020-12-05 18:47:56 +0100385 * \param[in] attributes The key attributes.
386 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200387 *
388 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100389 * The minimum size for a buffer to contain the key material has been
390 * returned successfully.
391 * \retval #PSA_ERROR_INVALID_ARGUMENT
392 * The size in bits of the key is not valid.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200393 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100394 * The type and/or the size in bits of the key or the combination of
395 * the two is not supported.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200396 */
Ronald Cron9df74be2020-12-05 19:15:23 +0100397psa_status_t psa_driver_wrapper_get_key_buffer_size(
Ronald Cron31216282020-12-05 18:47:56 +0100398 const psa_key_attributes_t *attributes,
399 size_t *key_buffer_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200400{
John Durkop2c618352020-09-22 06:54:01 -0700401 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
402 psa_key_type_t key_type = attributes->core.type;
403 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200404
Ronald Cron31216282020-12-05 18:47:56 +0100405 *key_buffer_size = 0;
John Durkop2c618352020-09-22 06:54:01 -0700406 switch( location )
407 {
John Durkop2c618352020-09-22 06:54:01 -0700408#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100409 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100410#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
411 /* Emulate property 'builtin_key_size' */
412 if( psa_key_id_is_builtin(
413 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(
414 psa_get_key_id( attributes ) ) ) )
415 {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100416 *key_buffer_size = sizeof( psa_drv_slot_number_t );
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100417 return( PSA_SUCCESS );
418 }
419#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cron7f13fa22021-04-13 12:41:34 +0200420 *key_buffer_size = mbedtls_test_size_function( key_type, key_bits );
Ronald Cron4607c822021-04-14 10:55:34 +0200421 return( ( *key_buffer_size != 0 ) ?
422 PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
John Durkop2c618352020-09-22 06:54:01 -0700423#endif /* PSA_CRYPTO_DRIVER_TEST */
424
425 default:
Ronald Cron9df74be2020-12-05 19:15:23 +0100426 (void)key_type;
427 (void)key_bits;
Steven Cooreman56250fd2020-09-04 13:07:15 +0200428 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200429 }
430}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200431
Ronald Cron977c2472020-10-13 08:32:21 +0200432psa_status_t psa_driver_wrapper_generate_key(
433 const psa_key_attributes_t *attributes,
434 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200435{
Ronald Cron977c2472020-10-13 08:32:21 +0200436 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
437 psa_key_location_t location =
438 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
439
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200440 /* Try dynamically-registered SE interface first */
441#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
442 const psa_drv_se_t *drv;
443 psa_drv_se_context_t *drv_context;
444
Ronald Cron977c2472020-10-13 08:32:21 +0200445 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200446 {
447 size_t pubkey_length = 0; /* We don't support this feature yet */
448 if( drv->key_management == NULL ||
449 drv->key_management->p_generate == NULL )
450 {
451 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200452 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200453 }
454 return( drv->key_management->p_generate(
Ronald Cron977c2472020-10-13 08:32:21 +0200455 drv_context,
456 *( (psa_key_slot_number_t *)key_buffer ),
Ronald Cronea0f8a62020-11-25 17:52:23 +0100457 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200458 }
459#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
460
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200461 switch( location )
462 {
463 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Ronald Cron977c2472020-10-13 08:32:21 +0200464#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200465 /* Transparent drivers are limited to generating asymmetric keys */
Ronald Cron977c2472020-10-13 08:32:21 +0200466 if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200467 {
Ronald Cron977c2472020-10-13 08:32:21 +0200468 /* Cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200469#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200470 status = mbedtls_test_transparent_generate_key(
Ronald Cron977c2472020-10-13 08:32:21 +0200471 attributes, key_buffer, key_buffer_size,
472 key_buffer_length );
473 /* Declared with fallback == true */
474 if( status != PSA_ERROR_NOT_SUPPORTED )
475 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200476#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200477 }
478#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
479
480 /* Software fallback */
481 status = psa_generate_key_internal(
482 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200483 break;
Ronald Cron977c2472020-10-13 08:32:21 +0200484
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200485 /* Add cases for opaque driver here */
Ronald Cron977c2472020-10-13 08:32:21 +0200486#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200487#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100488 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200489 status = mbedtls_test_opaque_generate_key(
Ronald Cron977c2472020-10-13 08:32:21 +0200490 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200491 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200492#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200493#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
494
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200495 default:
496 /* Key is declared with a lifetime not known to us */
497 status = PSA_ERROR_INVALID_ARGUMENT;
498 break;
499 }
500
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200501 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200502}
503
Ronald Cron83282872020-11-22 14:02:39 +0100504psa_status_t psa_driver_wrapper_import_key(
505 const psa_key_attributes_t *attributes,
506 const uint8_t *data,
507 size_t data_length,
508 uint8_t *key_buffer,
509 size_t key_buffer_size,
510 size_t *key_buffer_length,
511 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200512{
Steven Cooreman04524762020-10-13 17:43:44 +0200513 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100514 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
515 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200516
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100517 /* Try dynamically-registered SE interface first */
518#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
519 const psa_drv_se_t *drv;
520 psa_drv_se_context_t *drv_context;
521
522 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
523 {
524 if( drv->key_management == NULL ||
525 drv->key_management->p_import == NULL )
526 return( PSA_ERROR_NOT_SUPPORTED );
527
528 /* The driver should set the number of key bits, however in
529 * case it doesn't, we initialize bits to an invalid value. */
530 *bits = PSA_MAX_KEY_BITS + 1;
531 status = drv->key_management->p_import(
532 drv_context,
533 *( (psa_key_slot_number_t *)key_buffer ),
534 attributes, data, data_length, bits );
535
536 if( status != PSA_SUCCESS )
537 return( status );
538
539 if( (*bits) > PSA_MAX_KEY_BITS )
540 return( PSA_ERROR_NOT_SUPPORTED );
541
542 return( PSA_SUCCESS );
543 }
544#endif /* PSA_CRYPTO_SE_C */
545
Ronald Cronbf33c932020-11-28 18:06:53 +0100546 switch( location )
547 {
548 case PSA_KEY_LOCATION_LOCAL_STORAGE:
549 /* Key is stored in the slot in export representation, so
550 * cycle through all known transparent accelerators */
551#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
552#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200553 status = mbedtls_test_transparent_import_key(
554 attributes,
555 data, data_length,
556 key_buffer, key_buffer_size,
557 key_buffer_length, bits );
Ronald Cronbf33c932020-11-28 18:06:53 +0100558 /* Declared with fallback == true */
559 if( status != PSA_ERROR_NOT_SUPPORTED )
560 return( status );
561#endif /* PSA_CRYPTO_DRIVER_TEST */
562#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
563 /* Fell through, meaning no accelerator supports this operation */
564 return( psa_import_key_into_slot( attributes,
565 data, data_length,
566 key_buffer, key_buffer_size,
567 key_buffer_length, bits ) );
568
569 default:
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100570 /* Importing a key with external storage in not yet supported.
571 * Return in error indicating that the lifetime is not valid. */
Ronald Cronbf33c932020-11-28 18:06:53 +0100572 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100573 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100574 }
575
Steven Cooreman04524762020-10-13 17:43:44 +0200576}
577
Ronald Cron67227982020-11-26 15:16:05 +0100578psa_status_t psa_driver_wrapper_export_key(
579 const psa_key_attributes_t *attributes,
580 const uint8_t *key_buffer, size_t key_buffer_size,
581 uint8_t *data, size_t data_size, size_t *data_length )
582
583{
584 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
585 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
586 psa_get_key_lifetime( attributes ) );
587
Ronald Cron152e3f82020-11-26 16:06:41 +0100588 /* Try dynamically-registered SE interface first */
589#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
590 const psa_drv_se_t *drv;
591 psa_drv_se_context_t *drv_context;
592
593 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
594 {
595 if( ( drv->key_management == NULL ) ||
596 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100597 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100598 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100599 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100600
601 return( drv->key_management->p_export(
602 drv_context,
603 *( (psa_key_slot_number_t *)key_buffer ),
604 data, data_size, data_length ) );
605 }
606#endif /* PSA_CRYPTO_SE_C */
607
Ronald Cron67227982020-11-26 15:16:05 +0100608 switch( location )
609 {
610 case PSA_KEY_LOCATION_LOCAL_STORAGE:
611 return( psa_export_key_internal( attributes,
612 key_buffer,
613 key_buffer_size,
614 data,
615 data_size,
616 data_length ) );
617
618 /* Add cases for opaque driver here */
619#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
620#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100621 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200622 return( mbedtls_test_opaque_export_key( attributes,
623 key_buffer,
624 key_buffer_size,
625 data,
626 data_size,
627 data_length ) );
Ronald Cron67227982020-11-26 15:16:05 +0100628#endif /* PSA_CRYPTO_DRIVER_TEST */
629#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
630 default:
631 /* Key is declared with a lifetime not known to us */
632 return( status );
633 }
634}
635
Ronald Cron84cc9942020-11-25 14:30:05 +0100636psa_status_t psa_driver_wrapper_export_public_key(
637 const psa_key_attributes_t *attributes,
638 const uint8_t *key_buffer, size_t key_buffer_size,
639 uint8_t *data, size_t data_size, size_t *data_length )
640
Steven Cooremanb9b84422020-10-14 14:39:20 +0200641{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200642 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100643 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
644 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200645
Ronald Cron152e3f82020-11-26 16:06:41 +0100646 /* Try dynamically-registered SE interface first */
647#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
648 const psa_drv_se_t *drv;
649 psa_drv_se_context_t *drv_context;
650
651 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
652 {
653 if( ( drv->key_management == NULL ) ||
654 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100655 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100656 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100657 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100658
659 return( drv->key_management->p_export_public(
660 drv_context,
661 *( (psa_key_slot_number_t *)key_buffer ),
662 data, data_size, data_length ) );
663 }
664#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
665
Steven Cooremanb9b84422020-10-14 14:39:20 +0200666 switch( location )
667 {
668 case PSA_KEY_LOCATION_LOCAL_STORAGE:
669 /* Key is stored in the slot in export representation, so
670 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100671#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200672#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200673 status = mbedtls_test_transparent_export_public_key(
674 attributes,
675 key_buffer,
676 key_buffer_size,
677 data,
678 data_size,
679 data_length );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200680 /* Declared with fallback == true */
681 if( status != PSA_ERROR_NOT_SUPPORTED )
682 return( status );
683#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100684#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200685 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100686 return( psa_export_public_key_internal( attributes,
687 key_buffer,
688 key_buffer_size,
689 data,
690 data_size,
691 data_length ) );
692
Steven Cooremanb9b84422020-10-14 14:39:20 +0200693 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100694#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200695#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100696 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200697 return( mbedtls_test_opaque_export_public_key( attributes,
698 key_buffer,
699 key_buffer_size,
700 data,
701 data_size,
702 data_length ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200703#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100704#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200705 default:
706 /* Key is declared with a lifetime not known to us */
707 return( status );
708 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200709}
710
Steven Cooreman6801f082021-02-19 17:21:22 +0100711psa_status_t psa_driver_wrapper_get_builtin_key(
712 psa_drv_slot_number_t slot_number,
713 psa_key_attributes_t *attributes,
714 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
715{
716 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
717 switch( location )
718 {
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100719#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100720 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200721 return( mbedtls_test_opaque_get_builtin_key(
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100722 slot_number,
723 attributes,
724 key_buffer, key_buffer_size, key_buffer_length ) );
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100725#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman6801f082021-02-19 17:21:22 +0100726 default:
727 (void) slot_number;
728 (void) key_buffer;
729 (void) key_buffer_size;
730 (void) key_buffer_length;
731 return( PSA_ERROR_DOES_NOT_EXIST );
732 }
733}
734
Steven Cooreman37941cb2020-07-28 18:49:51 +0200735/*
736 * Cipher functions
737 */
738psa_status_t psa_driver_wrapper_cipher_encrypt(
739 psa_key_slot_t *slot,
740 psa_algorithm_t alg,
741 const uint8_t *input,
742 size_t input_length,
743 uint8_t *output,
744 size_t output_size,
745 size_t *output_length )
746{
747#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
748 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
749 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
750 psa_key_attributes_t attributes = {
751 .core = slot->attr
752 };
753
754 switch( location )
755 {
756 case PSA_KEY_LOCATION_LOCAL_STORAGE:
757 /* Key is stored in the slot in export representation, so
758 * cycle through all known transparent accelerators */
759#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200760 status = mbedtls_test_transparent_cipher_encrypt( &attributes,
761 slot->key.data,
762 slot->key.bytes,
763 alg,
764 input,
765 input_length,
766 output,
767 output_size,
768 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200769 /* Declared with fallback == true */
770 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200771 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200772#endif /* PSA_CRYPTO_DRIVER_TEST */
773 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200774 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200775 /* Add cases for opaque driver here */
776#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100777 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200778 return( mbedtls_test_opaque_cipher_encrypt( &attributes,
779 slot->key.data,
780 slot->key.bytes,
781 alg,
782 input,
783 input_length,
784 output,
785 output_size,
786 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200787#endif /* PSA_CRYPTO_DRIVER_TEST */
788 default:
789 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200790 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200791 }
792#else /* PSA_CRYPTO_DRIVER_PRESENT */
793 (void) slot;
794 (void) alg;
795 (void) input;
796 (void) input_length;
797 (void) output;
798 (void) output_size;
799 (void) output_length;
800
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200801 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200802#endif /* PSA_CRYPTO_DRIVER_PRESENT */
803}
804
805psa_status_t psa_driver_wrapper_cipher_decrypt(
806 psa_key_slot_t *slot,
807 psa_algorithm_t alg,
808 const uint8_t *input,
809 size_t input_length,
810 uint8_t *output,
811 size_t output_size,
812 size_t *output_length )
813{
814#if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
815 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
816 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime);
817 psa_key_attributes_t attributes = {
818 .core = slot->attr
819 };
820
821 switch( location )
822 {
823 case PSA_KEY_LOCATION_LOCAL_STORAGE:
824 /* Key is stored in the slot in export representation, so
825 * cycle through all known transparent accelerators */
826#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200827 status = mbedtls_test_transparent_cipher_decrypt( &attributes,
828 slot->key.data,
829 slot->key.bytes,
830 alg,
831 input,
832 input_length,
833 output,
834 output_size,
835 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200836 /* Declared with fallback == true */
837 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200838 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200839#endif /* PSA_CRYPTO_DRIVER_TEST */
840 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200841 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200842 /* Add cases for opaque driver here */
843#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100844 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200845 return( mbedtls_test_opaque_cipher_decrypt( &attributes,
846 slot->key.data,
847 slot->key.bytes,
848 alg,
849 input,
850 input_length,
851 output,
852 output_size,
853 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200854#endif /* PSA_CRYPTO_DRIVER_TEST */
855 default:
856 /* Key is declared with a lifetime not known to us */
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200857 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200858 }
859#else /* PSA_CRYPTO_DRIVER_PRESENT */
860 (void) slot;
861 (void) alg;
862 (void) input;
863 (void) input_length;
864 (void) output;
865 (void) output_size;
866 (void) output_length;
867
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200868 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200869#endif /* PSA_CRYPTO_DRIVER_PRESENT */
870}
871
872psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100873 psa_cipher_operation_t *operation,
874 const psa_key_attributes_t *attributes,
875 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200876 psa_algorithm_t alg )
877{
Ronald Cron0b805592020-12-14 18:08:20 +0100878 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona4af55f2020-12-14 14:36:06 +0100879 psa_key_location_t location =
880 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200881
Steven Cooreman37941cb2020-07-28 18:49:51 +0200882 switch( location )
883 {
884 case PSA_KEY_LOCATION_LOCAL_STORAGE:
885 /* Key is stored in the slot in export representation, so
886 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100887#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200888#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200889 status = mbedtls_test_transparent_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100890 &operation->ctx.transparent_test_driver_ctx,
891 attributes,
892 key_buffer,
893 key_buffer_size,
894 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200895 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200896 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100897 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +0200898
Ronald Cron0b805592020-12-14 18:08:20 +0100899 if( status != PSA_ERROR_NOT_SUPPORTED )
900 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200901#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100902#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100903#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200904 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +0100905 status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100906 attributes,
907 key_buffer,
908 key_buffer_size,
909 alg );
910 if( status == PSA_SUCCESS )
Ronald Cron6e412a72021-03-10 09:58:47 +0100911 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Ronald Cron49fafa92021-03-10 08:34:23 +0100912
Ronald Cron7b4154d2021-03-19 14:49:41 +0100913 if( status != PSA_ERROR_NOT_SUPPORTED )
914 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100915#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
916 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron0b805592020-12-14 18:08:20 +0100917
Steven Cooreman37941cb2020-07-28 18:49:51 +0200918 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100919#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200920#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100921 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200922 status = mbedtls_test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100923 &operation->ctx.opaque_test_driver_ctx,
924 attributes,
925 key_buffer, key_buffer_size,
926 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200927
Steven Cooreman37941cb2020-07-28 18:49:51 +0200928 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100929 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200930
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200931 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200932#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100933#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +0200934 default:
935 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +0100936 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100937 (void)key_buffer;
938 (void)key_buffer_size;
939 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +0200940 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200941 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200942}
943
944psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100945 psa_cipher_operation_t *operation,
946 const psa_key_attributes_t *attributes,
947 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200948 psa_algorithm_t alg )
949{
Steven Cooreman37941cb2020-07-28 18:49:51 +0200950 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crona4af55f2020-12-14 14:36:06 +0100951 psa_key_location_t location =
952 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200953
Steven Cooreman37941cb2020-07-28 18:49:51 +0200954 switch( location )
955 {
956 case PSA_KEY_LOCATION_LOCAL_STORAGE:
957 /* Key is stored in the slot in export representation, so
958 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100959#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200960#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200961 status = mbedtls_test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100962 &operation->ctx.transparent_test_driver_ctx,
963 attributes,
964 key_buffer,
965 key_buffer_size,
966 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200967 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200968 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100969 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +0200970
Ronald Cron0b805592020-12-14 18:08:20 +0100971 if( status != PSA_ERROR_NOT_SUPPORTED )
972 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200973#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100974#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100975#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200976 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +0100977 status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100978 attributes,
979 key_buffer,
980 key_buffer_size,
981 alg );
982 if( status == PSA_SUCCESS )
983 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
984
985 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100986#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
987 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron49fafa92021-03-10 08:34:23 +0100988
Steven Cooreman37941cb2020-07-28 18:49:51 +0200989 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100990#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200991#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100992 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200993 status = mbedtls_test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100994 &operation->ctx.opaque_test_driver_ctx,
995 attributes,
996 key_buffer, key_buffer_size,
997 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200998
Steven Cooreman37941cb2020-07-28 18:49:51 +0200999 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001000 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001001
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001002 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001003#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001004#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +02001005 default:
1006 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +01001007 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001008 (void)key_buffer;
1009 (void)key_buffer_size;
1010 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +02001011 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001012 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001013}
1014
Steven Cooreman37941cb2020-07-28 18:49:51 +02001015psa_status_t psa_driver_wrapper_cipher_set_iv(
Ronald Cron6056fe82020-12-15 13:58:07 +01001016 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001017 const uint8_t *iv,
1018 size_t iv_length )
1019{
Ronald Cron49fafa92021-03-10 08:34:23 +01001020 switch( operation->id )
1021 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001022#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001023 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001024 return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001025 iv,
1026 iv_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001027#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001028
1029#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001030#if defined(PSA_CRYPTO_DRIVER_TEST)
1031 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001032 return( mbedtls_test_transparent_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001033 &operation->ctx.transparent_test_driver_ctx,
1034 iv, iv_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001035
Steven Cooreman37941cb2020-07-28 18:49:51 +02001036 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001037 return( mbedtls_test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001038 &operation->ctx.opaque_test_driver_ctx,
1039 iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001040#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001041#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001042 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001043
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001044 (void)iv;
1045 (void)iv_length;
1046
Ronald Crondd24c9b2020-12-15 14:10:01 +01001047 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001048}
1049
1050psa_status_t psa_driver_wrapper_cipher_update(
Ronald Cron6056fe82020-12-15 13:58:07 +01001051 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001052 const uint8_t *input,
1053 size_t input_length,
1054 uint8_t *output,
1055 size_t output_size,
1056 size_t *output_length )
1057{
Ronald Cron49fafa92021-03-10 08:34:23 +01001058 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001059 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001060#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001061 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001062 return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001063 input,
1064 input_length,
1065 output,
1066 output_size,
1067 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001068#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1069
Ronald Cron49fafa92021-03-10 08:34:23 +01001070#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001071#if defined(PSA_CRYPTO_DRIVER_TEST)
1072 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001073 return( mbedtls_test_transparent_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001074 &operation->ctx.transparent_test_driver_ctx,
1075 input, input_length,
1076 output, output_size, output_length ) );
1077
Steven Cooreman37941cb2020-07-28 18:49:51 +02001078 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001079 return( mbedtls_test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001080 &operation->ctx.opaque_test_driver_ctx,
1081 input, input_length,
1082 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001083#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001084#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001085 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001086
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001087 (void)input;
1088 (void)input_length;
1089 (void)output;
1090 (void)output_size;
1091 (void)output_length;
1092
Ronald Crondd24c9b2020-12-15 14:10:01 +01001093 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001094}
1095
1096psa_status_t psa_driver_wrapper_cipher_finish(
Ronald Cron6056fe82020-12-15 13:58:07 +01001097 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001098 uint8_t *output,
1099 size_t output_size,
1100 size_t *output_length )
1101{
Ronald Cron49fafa92021-03-10 08:34:23 +01001102 switch( operation->id )
1103 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001104#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001105 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001106 return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001107 output,
1108 output_size,
1109 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001110#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001111
1112#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001113#if defined(PSA_CRYPTO_DRIVER_TEST)
1114 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001115 return( mbedtls_test_transparent_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001116 &operation->ctx.transparent_test_driver_ctx,
1117 output, output_size, output_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001118
Steven Cooreman37941cb2020-07-28 18:49:51 +02001119 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001120 return( mbedtls_test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001121 &operation->ctx.opaque_test_driver_ctx,
1122 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001123#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001124#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001125 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001126
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001127 (void)output;
1128 (void)output_size;
1129 (void)output_length;
1130
Ronald Crondd24c9b2020-12-15 14:10:01 +01001131 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001132}
1133
1134psa_status_t psa_driver_wrapper_cipher_abort(
Ronald Cron6056fe82020-12-15 13:58:07 +01001135 psa_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001136{
Ronald Crondd24c9b2020-12-15 14:10:01 +01001137 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001138
Ronald Cron49fafa92021-03-10 08:34:23 +01001139 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001140 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001141#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001142 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001143 return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001144#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +01001145
1146#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001147#if defined(PSA_CRYPTO_DRIVER_TEST)
1148 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001149 status = mbedtls_test_transparent_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001150 &operation->ctx.transparent_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001151 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001152 &operation->ctx.transparent_test_driver_ctx,
1153 sizeof( operation->ctx.transparent_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001154 return( status );
Ronald Cron49fafa92021-03-10 08:34:23 +01001155
Steven Cooreman37941cb2020-07-28 18:49:51 +02001156 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001157 status = mbedtls_test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001158 &operation->ctx.opaque_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001159 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001160 &operation->ctx.opaque_test_driver_ctx,
1161 sizeof( operation->ctx.opaque_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001162 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001163#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001164#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001165 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001166
Ronald Cron49fafa92021-03-10 08:34:23 +01001167 (void)status;
Ronald Crondd24c9b2020-12-15 14:10:01 +01001168 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001169}
1170
Steven Cooreman1e582352021-02-18 17:24:37 +01001171/*
1172 * Hashing functions
1173 */
1174psa_status_t psa_driver_wrapper_hash_compute(
1175 psa_algorithm_t alg,
1176 const uint8_t *input,
1177 size_t input_length,
1178 uint8_t *hash,
1179 size_t hash_size,
1180 size_t *hash_length)
1181{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001182 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001183
1184 /* Try accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001185#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001186 status = mbedtls_test_transparent_hash_compute(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001187 alg, input, input_length, hash, hash_size, hash_length );
Steven Cooremanf7638102021-03-04 15:14:36 +01001188 if( status != PSA_ERROR_NOT_SUPPORTED )
1189 return( status );
1190#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001191
1192 /* If software fallback is compiled in, try fallback */
1193#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1194 status = mbedtls_psa_hash_compute( alg, input, input_length,
1195 hash, hash_size, hash_length );
1196 if( status != PSA_ERROR_NOT_SUPPORTED )
1197 return( status );
1198#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001199 (void) status;
1200 (void) alg;
1201 (void) input;
1202 (void) input_length;
1203 (void) hash;
1204 (void) hash_size;
1205 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001206
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001207 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman1e582352021-02-18 17:24:37 +01001208}
1209
1210psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001211 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001212 psa_algorithm_t alg )
1213{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001214 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001215
Steven Cooreman1e582352021-02-18 17:24:37 +01001216 /* Try setup on accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001217#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001218 status = mbedtls_test_transparent_hash_setup(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001219 &operation->ctx.test_driver_ctx, alg );
Steven Cooremanf7638102021-03-04 15:14:36 +01001220 if( status == PSA_SUCCESS )
1221 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1222
1223 if( status != PSA_ERROR_NOT_SUPPORTED )
1224 return( status );
1225#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001226
1227 /* If software fallback is compiled in, try fallback */
1228#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001229 status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
Steven Cooreman1e582352021-02-18 17:24:37 +01001230 if( status == PSA_SUCCESS )
Steven Cooreman1e582352021-02-18 17:24:37 +01001231 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Steven Cooreman1e582352021-02-18 17:24:37 +01001232
1233 if( status != PSA_ERROR_NOT_SUPPORTED )
1234 return( status );
1235#endif
1236 /* Nothing left to try if we fall through here */
1237 (void) status;
1238 (void) operation;
1239 (void) alg;
1240 return( PSA_ERROR_NOT_SUPPORTED );
1241}
1242
1243psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001244 const psa_hash_operation_t *source_operation,
1245 psa_hash_operation_t *target_operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001246{
Steven Cooreman1e582352021-02-18 17:24:37 +01001247 switch( source_operation->id )
1248 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001249#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1250 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1251 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1252 return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
1253 &target_operation->ctx.mbedtls_ctx ) );
1254#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001255#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001256 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1257 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Ronald Cron7f13fa22021-04-13 12:41:34 +02001258 return( mbedtls_test_transparent_hash_clone(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001259 &source_operation->ctx.test_driver_ctx,
1260 &target_operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001261#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001262 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001263 (void) target_operation;
1264 return( PSA_ERROR_BAD_STATE );
1265 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001266}
1267
1268psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001269 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001270 const uint8_t *input,
1271 size_t input_length )
1272{
Steven Cooreman1e582352021-02-18 17:24:37 +01001273 switch( operation->id )
1274 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001275#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1276 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1277 return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
1278 input, input_length ) );
1279#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001280#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001281 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001282 return( mbedtls_test_transparent_hash_update(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001283 &operation->ctx.test_driver_ctx,
1284 input, input_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001285#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001286 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001287 (void) input;
1288 (void) input_length;
1289 return( PSA_ERROR_BAD_STATE );
1290 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001291}
1292
1293psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001294 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001295 uint8_t *hash,
1296 size_t hash_size,
1297 size_t *hash_length )
1298{
Steven Cooreman1e582352021-02-18 17:24:37 +01001299 switch( operation->id )
1300 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001301#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1302 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1303 return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
1304 hash, hash_size, hash_length ) );
1305#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001306#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001307 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001308 return( mbedtls_test_transparent_hash_finish(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001309 &operation->ctx.test_driver_ctx,
1310 hash, hash_size, hash_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001311#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001312 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001313 (void) hash;
1314 (void) hash_size;
1315 (void) hash_length;
1316 return( PSA_ERROR_BAD_STATE );
1317 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001318}
1319
1320psa_status_t psa_driver_wrapper_hash_abort(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001321 psa_hash_operation_t *operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001322{
Steven Cooreman1e582352021-02-18 17:24:37 +01001323 switch( operation->id )
1324 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001325#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1326 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1327 return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
1328#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001329#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001330 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001331 return( mbedtls_test_transparent_hash_abort(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001332 &operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001333#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001334 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001335 return( PSA_ERROR_BAD_STATE );
1336 }
1337}
1338
Ronald Cronde822812021-03-17 16:08:20 +01001339psa_status_t psa_driver_wrapper_aead_encrypt(
1340 const psa_key_attributes_t *attributes,
1341 const uint8_t *key_buffer, size_t key_buffer_size,
1342 psa_algorithm_t alg,
1343 const uint8_t *nonce, size_t nonce_length,
1344 const uint8_t *additional_data, size_t additional_data_length,
1345 const uint8_t *plaintext, size_t plaintext_length,
1346 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
1347{
1348 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1349 psa_key_location_t location =
1350 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1351
1352 switch( location )
1353 {
1354 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1355 /* Key is stored in the slot in export representation, so
1356 * cycle through all known transparent accelerators */
1357
1358#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1359#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001360 status = mbedtls_test_transparent_aead_encrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001361 attributes, key_buffer, key_buffer_size,
1362 alg,
1363 nonce, nonce_length,
1364 additional_data, additional_data_length,
1365 plaintext, plaintext_length,
1366 ciphertext, ciphertext_size, ciphertext_length );
1367 /* Declared with fallback == true */
1368 if( status != PSA_ERROR_NOT_SUPPORTED )
1369 return( status );
1370#endif /* PSA_CRYPTO_DRIVER_TEST */
1371#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1372
1373 /* Fell through, meaning no accelerator supports this operation */
1374 return( mbedtls_psa_aead_encrypt(
1375 attributes, key_buffer, key_buffer_size,
1376 alg,
1377 nonce, nonce_length,
1378 additional_data, additional_data_length,
1379 plaintext, plaintext_length,
1380 ciphertext, ciphertext_size, ciphertext_length ) );
1381
1382 /* Add cases for opaque driver here */
1383
1384 default:
1385 /* Key is declared with a lifetime not known to us */
1386 (void)status;
1387 return( PSA_ERROR_INVALID_ARGUMENT );
1388 }
1389}
1390
1391psa_status_t psa_driver_wrapper_aead_decrypt(
1392 const psa_key_attributes_t *attributes,
1393 const uint8_t *key_buffer, size_t key_buffer_size,
1394 psa_algorithm_t alg,
1395 const uint8_t *nonce, size_t nonce_length,
1396 const uint8_t *additional_data, size_t additional_data_length,
1397 const uint8_t *ciphertext, size_t ciphertext_length,
1398 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
1399{
1400 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1401 psa_key_location_t location =
1402 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1403
1404 switch( location )
1405 {
1406 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1407 /* Key is stored in the slot in export representation, so
1408 * cycle through all known transparent accelerators */
1409
1410#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1411#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001412 status = mbedtls_test_transparent_aead_decrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001413 attributes, key_buffer, key_buffer_size,
1414 alg,
1415 nonce, nonce_length,
1416 additional_data, additional_data_length,
1417 ciphertext, ciphertext_length,
1418 plaintext, plaintext_size, plaintext_length );
1419 /* Declared with fallback == true */
1420 if( status != PSA_ERROR_NOT_SUPPORTED )
1421 return( status );
1422#endif /* PSA_CRYPTO_DRIVER_TEST */
1423#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1424
1425 /* Fell through, meaning no accelerator supports this operation */
1426 return( mbedtls_psa_aead_decrypt(
1427 attributes, key_buffer, key_buffer_size,
1428 alg,
1429 nonce, nonce_length,
1430 additional_data, additional_data_length,
1431 ciphertext, ciphertext_length,
1432 plaintext, plaintext_size, plaintext_length ) );
1433
1434 /* Add cases for opaque driver here */
1435
1436 default:
1437 /* Key is declared with a lifetime not known to us */
1438 (void)status;
1439 return( PSA_ERROR_INVALID_ARGUMENT );
1440 }
1441}
Steven Cooremand13a70f2021-03-19 15:24:23 +01001442
Paul Elliott6504aa62021-04-20 17:09:36 +01001443psa_status_t psa_driver_wrapper_aead_encrypt_setup(
1444 psa_aead_operation_t *operation,
1445 const psa_key_attributes_t *attributes,
1446 const uint8_t *key_buffer, size_t key_buffer_size,
1447 psa_algorithm_t alg )
1448{
1449 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1450 psa_key_location_t location =
1451 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1452
1453 switch( location )
1454 {
1455 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1456 /* Key is stored in the slot in export representation, so
1457 * cycle through all known transparent accelerators */
1458
1459#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1460#if defined(PSA_CRYPTO_DRIVER_TEST)
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001461 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Paul Elliotta218ceb2021-05-07 15:10:31 +01001462 status = mbedtls_test_transparent_aead_encrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001463 &operation->ctx.transparent_test_driver_ctx,
1464 attributes, key_buffer, key_buffer_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001465 alg );
Paul Elliott6504aa62021-04-20 17:09:36 +01001466
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001467 /* Declared with fallback == true */
Paul Elliott6504aa62021-04-20 17:09:36 +01001468 if( status != PSA_ERROR_NOT_SUPPORTED )
1469 return( status );
1470#endif /* PSA_CRYPTO_DRIVER_TEST */
1471#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1472
1473 /* Fell through, meaning no accelerator supports this operation */
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001474 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Paul Elliott6504aa62021-04-20 17:09:36 +01001475 status = mbedtls_psa_aead_encrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001476 &operation->ctx.mbedtls_ctx, attributes,
Paul Elliott6504aa62021-04-20 17:09:36 +01001477 key_buffer, key_buffer_size,
1478 alg );
1479
Paul Elliott6504aa62021-04-20 17:09:36 +01001480 return( status );
1481
1482 /* Add cases for opaque driver here */
1483
1484 default:
1485 /* Key is declared with a lifetime not known to us */
1486 (void)status;
1487 return( PSA_ERROR_INVALID_ARGUMENT );
1488 }
1489}
1490
1491psa_status_t psa_driver_wrapper_aead_decrypt_setup(
1492 psa_aead_operation_t *operation,
1493 const psa_key_attributes_t *attributes,
1494 const uint8_t *key_buffer, size_t key_buffer_size,
1495 psa_algorithm_t alg )
1496{
1497 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1498 psa_key_location_t location =
1499 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1500
1501 switch( location )
1502 {
1503 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1504 /* Key is stored in the slot in export representation, so
1505 * cycle through all known transparent accelerators */
1506
1507#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1508#if defined(PSA_CRYPTO_DRIVER_TEST)
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001509 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Paul Elliotta218ceb2021-05-07 15:10:31 +01001510 status = mbedtls_test_transparent_aead_decrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001511 &operation->ctx.transparent_test_driver_ctx,
1512 attributes,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001513 key_buffer, key_buffer_size,
1514 alg );
Paul Elliott6504aa62021-04-20 17:09:36 +01001515
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001516 /* Declared with fallback == true */
Paul Elliott6504aa62021-04-20 17:09:36 +01001517 if( status != PSA_ERROR_NOT_SUPPORTED )
1518 return( status );
1519#endif /* PSA_CRYPTO_DRIVER_TEST */
1520#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1521
1522 /* Fell through, meaning no accelerator supports this operation */
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001523 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Paul Elliott6504aa62021-04-20 17:09:36 +01001524 status = mbedtls_psa_aead_decrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001525 &operation->ctx.mbedtls_ctx,
1526 attributes,
Paul Elliott6504aa62021-04-20 17:09:36 +01001527 key_buffer, key_buffer_size,
1528 alg );
1529
Paul Elliott6504aa62021-04-20 17:09:36 +01001530 return( status );
1531
1532 /* Add cases for opaque driver here */
1533
1534 default:
1535 /* Key is declared with a lifetime not known to us */
1536 (void)status;
1537 return( PSA_ERROR_INVALID_ARGUMENT );
1538 }
1539}
1540
Paul Elliott6504aa62021-04-20 17:09:36 +01001541psa_status_t psa_driver_wrapper_aead_set_nonce(
1542 psa_aead_operation_t *operation,
1543 const uint8_t *nonce,
1544 size_t nonce_length )
1545{
1546 switch( operation->id )
1547 {
Paul Elliott5c656cb2021-05-19 14:15:01 +01001548#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001549 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001550 return( mbedtls_psa_aead_set_nonce( &operation->ctx.mbedtls_ctx,
1551 nonce,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001552 nonce_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001553
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001554#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001555
1556#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1557#if defined(PSA_CRYPTO_DRIVER_TEST)
1558 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001559 return( mbedtls_test_transparent_aead_set_nonce(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001560 &operation->ctx.transparent_test_driver_ctx,
1561 nonce, nonce_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001562
1563 /* Add cases for opaque driver here */
1564
1565#endif /* PSA_CRYPTO_DRIVER_TEST */
1566#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1567 }
1568
1569 (void)nonce;
1570 (void)nonce_length;
1571
1572 return( PSA_ERROR_INVALID_ARGUMENT );
1573}
1574
1575psa_status_t psa_driver_wrapper_aead_set_lengths(
1576 psa_aead_operation_t *operation,
1577 size_t ad_length,
1578 size_t plaintext_length )
1579{
1580 switch( operation->id )
1581 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001582#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001583 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001584 return( mbedtls_psa_aead_set_lengths( &operation->ctx.mbedtls_ctx,
1585 ad_length,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001586 plaintext_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001587
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001588#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001589
1590#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1591#if defined(PSA_CRYPTO_DRIVER_TEST)
1592 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001593 return( mbedtls_test_transparent_aead_set_lengths(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001594 &operation->ctx.transparent_test_driver_ctx,
1595 ad_length, plaintext_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001596
1597 /* Add cases for opaque driver here */
1598
1599#endif /* PSA_CRYPTO_DRIVER_TEST */
1600#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1601 }
1602
1603 (void)ad_length;
1604 (void)plaintext_length;
1605
1606 return( PSA_ERROR_INVALID_ARGUMENT );
1607}
1608
1609psa_status_t psa_driver_wrapper_aead_update_ad(
1610 psa_aead_operation_t *operation,
1611 const uint8_t *input,
1612 size_t input_length )
1613{
1614 switch( operation->id )
1615 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001616#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001617 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001618 return( mbedtls_psa_aead_update_ad( &operation->ctx.mbedtls_ctx,
1619 input,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001620 input_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001621
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001622#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001623
1624#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1625#if defined(PSA_CRYPTO_DRIVER_TEST)
1626 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001627 return( mbedtls_test_transparent_aead_update_ad(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001628 &operation->ctx.transparent_test_driver_ctx,
1629 input, input_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001630
1631 /* Add cases for opaque driver here */
1632
1633#endif /* PSA_CRYPTO_DRIVER_TEST */
1634#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1635 }
1636
1637 (void)input;
1638 (void)input_length;
1639
1640 return( PSA_ERROR_INVALID_ARGUMENT );
1641}
1642
1643psa_status_t psa_driver_wrapper_aead_update(
1644 psa_aead_operation_t *operation,
1645 const uint8_t *input,
1646 size_t input_length,
1647 uint8_t *output,
1648 size_t output_size,
1649 size_t *output_length )
1650{
1651 switch( operation->id )
1652 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001653#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001654 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001655 return( mbedtls_psa_aead_update( &operation->ctx.mbedtls_ctx,
1656 input, input_length,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001657 output, output_size,
1658 output_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001659
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001660#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001661
1662#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1663#if defined(PSA_CRYPTO_DRIVER_TEST)
1664 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001665 return( mbedtls_test_transparent_aead_update(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001666 &operation->ctx.transparent_test_driver_ctx,
1667 input, input_length, output, output_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001668 output_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001669
1670 /* Add cases for opaque driver here */
1671
1672#endif /* PSA_CRYPTO_DRIVER_TEST */
1673#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1674 }
1675
1676 (void)input;
1677 (void)input_length;
1678 (void)output;
1679 (void)output_size;
1680 (void)output_length;
1681
1682 return( PSA_ERROR_INVALID_ARGUMENT );
1683}
1684
1685psa_status_t psa_driver_wrapper_aead_finish(
1686 psa_aead_operation_t *operation,
1687 uint8_t *ciphertext,
1688 size_t ciphertext_size,
1689 size_t *ciphertext_length,
1690 uint8_t *tag,
1691 size_t tag_size,
1692 size_t *tag_length )
1693{
1694 switch( operation->id )
1695 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001696#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001697 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001698 return( mbedtls_psa_aead_finish( &operation->ctx.mbedtls_ctx,
1699 ciphertext,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001700 ciphertext_size,
1701 ciphertext_length, tag,
1702 tag_size, tag_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001703
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001704#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001705
1706#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1707#if defined(PSA_CRYPTO_DRIVER_TEST)
1708 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001709 return( mbedtls_test_transparent_aead_finish(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001710 &operation->ctx.transparent_test_driver_ctx,
1711 ciphertext, ciphertext_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001712 ciphertext_length, tag, tag_size, tag_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001713
1714 /* Add cases for opaque driver here */
1715
1716#endif /* PSA_CRYPTO_DRIVER_TEST */
1717#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1718 }
1719
1720 (void)ciphertext;
1721 (void)ciphertext_size;
1722 (void)ciphertext_length;
1723 (void)tag;
1724 (void)tag_size;
1725 (void)tag_length;
1726
1727 return( PSA_ERROR_INVALID_ARGUMENT );
1728}
1729
1730psa_status_t psa_driver_wrapper_aead_verify(
1731 psa_aead_operation_t *operation,
1732 uint8_t *plaintext,
1733 size_t plaintext_size,
1734 size_t *plaintext_length,
1735 const uint8_t *tag,
1736 size_t tag_length )
1737{
1738 switch( operation->id )
1739 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001740#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001741 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliott315628d2021-07-20 18:25:54 +01001742 {
1743 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1744 uint8_t check_tag[PSA_AEAD_TAG_MAX_SIZE];
1745 size_t check_tag_length;
1746
1747 status = mbedtls_psa_aead_finish( &operation->ctx.mbedtls_ctx,
1748 plaintext,
1749 plaintext_size,
1750 plaintext_length,
1751 check_tag,
1752 tag_length,
1753 &check_tag_length );
1754
1755 if( status == PSA_SUCCESS )
1756 {
1757 if( tag_length != check_tag_length ||
1758 mbedtls_psa_safer_memcmp( tag, check_tag, tag_length )
1759 != 0 )
1760 status = PSA_ERROR_INVALID_SIGNATURE;
1761 }
1762
1763 return( status );
1764 }
Paul Elliott6504aa62021-04-20 17:09:36 +01001765
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001766#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001767
1768#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1769#if defined(PSA_CRYPTO_DRIVER_TEST)
1770 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001771 return( mbedtls_test_transparent_aead_verify(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001772 &operation->ctx.transparent_test_driver_ctx,
1773 plaintext, plaintext_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001774 plaintext_length, tag, tag_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001775
1776 /* Add cases for opaque driver here */
1777
1778#endif /* PSA_CRYPTO_DRIVER_TEST */
1779#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1780 }
1781
1782 (void)plaintext;
1783 (void)plaintext_size;
1784 (void)plaintext_length;
1785 (void)tag;
1786 (void)tag_length;
1787
1788 return( PSA_ERROR_INVALID_ARGUMENT );
1789}
1790
1791psa_status_t psa_driver_wrapper_aead_abort(
1792 psa_aead_operation_t *operation )
1793{
1794 switch( operation->id )
1795 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001796#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001797 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001798 return( mbedtls_psa_aead_abort( &operation->ctx.mbedtls_ctx ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001799
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001800#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001801
1802#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1803#if defined(PSA_CRYPTO_DRIVER_TEST)
1804 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001805 return( mbedtls_test_transparent_aead_abort(
1806 &operation->ctx.transparent_test_driver_ctx ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001807
1808 /* Add cases for opaque driver here */
1809
1810#endif /* PSA_CRYPTO_DRIVER_TEST */
1811#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1812 }
1813
1814 return( PSA_ERROR_INVALID_ARGUMENT );
1815}
Steven Cooremand13a70f2021-03-19 15:24:23 +01001816
1817/*
1818 * MAC functions
1819 */
1820psa_status_t psa_driver_wrapper_mac_compute(
1821 const psa_key_attributes_t *attributes,
1822 const uint8_t *key_buffer,
1823 size_t key_buffer_size,
1824 psa_algorithm_t alg,
1825 const uint8_t *input,
1826 size_t input_length,
1827 uint8_t *mac,
1828 size_t mac_size,
1829 size_t *mac_length )
1830{
1831 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1832 psa_key_location_t location =
1833 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1834
1835 switch( location )
1836 {
1837 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1838 /* Key is stored in the slot in export representation, so
1839 * cycle through all known transparent accelerators */
1840#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1841#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001842 status = mbedtls_test_transparent_mac_compute(
Steven Cooremand13a70f2021-03-19 15:24:23 +01001843 attributes, key_buffer, key_buffer_size, alg,
1844 input, input_length,
1845 mac, mac_size, mac_length );
1846 /* Declared with fallback == true */
1847 if( status != PSA_ERROR_NOT_SUPPORTED )
1848 return( status );
1849#endif /* PSA_CRYPTO_DRIVER_TEST */
1850#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1851#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1852 /* Fell through, meaning no accelerator supports this operation */
1853 status = mbedtls_psa_mac_compute(
1854 attributes, key_buffer, key_buffer_size, alg,
1855 input, input_length,
1856 mac, mac_size, mac_length );
1857 if( status != PSA_ERROR_NOT_SUPPORTED )
1858 return( status );
1859#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1860 return( PSA_ERROR_NOT_SUPPORTED );
1861
1862 /* Add cases for opaque driver here */
1863#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1864#if defined(PSA_CRYPTO_DRIVER_TEST)
1865 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001866 status = mbedtls_test_opaque_mac_compute(
Steven Cooremand13a70f2021-03-19 15:24:23 +01001867 attributes, key_buffer, key_buffer_size, alg,
1868 input, input_length,
1869 mac, mac_size, mac_length );
1870 return( status );
1871#endif /* PSA_CRYPTO_DRIVER_TEST */
1872#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1873 default:
1874 /* Key is declared with a lifetime not known to us */
1875 (void) key_buffer;
1876 (void) key_buffer_size;
1877 (void) alg;
1878 (void) input;
1879 (void) input_length;
1880 (void) mac;
1881 (void) mac_size;
1882 (void) mac_length;
1883 (void) status;
1884 return( PSA_ERROR_INVALID_ARGUMENT );
1885 }
1886}
1887
1888psa_status_t psa_driver_wrapper_mac_sign_setup(
1889 psa_mac_operation_t *operation,
1890 const psa_key_attributes_t *attributes,
1891 const uint8_t *key_buffer,
1892 size_t key_buffer_size,
1893 psa_algorithm_t alg )
1894{
1895 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1896 psa_key_location_t location =
1897 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1898
1899 switch( location )
1900 {
1901 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1902 /* Key is stored in the slot in export representation, so
1903 * cycle through all known transparent accelerators */
1904#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1905#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001906 status = mbedtls_test_transparent_mac_sign_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001907 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001908 attributes,
1909 key_buffer, key_buffer_size,
1910 alg );
1911 /* Declared with fallback == true */
1912 if( status == PSA_SUCCESS )
1913 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1914
1915 if( status != PSA_ERROR_NOT_SUPPORTED )
1916 return( status );
1917#endif /* PSA_CRYPTO_DRIVER_TEST */
1918#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1919#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1920 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001921 status = mbedtls_psa_mac_sign_setup( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001922 attributes,
1923 key_buffer, key_buffer_size,
1924 alg );
1925 if( status == PSA_SUCCESS )
1926 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1927
1928 if( status != PSA_ERROR_NOT_SUPPORTED )
1929 return( status );
1930#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1931 return( PSA_ERROR_NOT_SUPPORTED );
1932
1933 /* Add cases for opaque driver here */
1934#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1935#if defined(PSA_CRYPTO_DRIVER_TEST)
1936 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001937 status = mbedtls_test_opaque_mac_sign_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001938 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001939 attributes,
1940 key_buffer, key_buffer_size,
1941 alg );
1942
1943 if( status == PSA_SUCCESS )
1944 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
1945
1946 return( status );
1947#endif /* PSA_CRYPTO_DRIVER_TEST */
1948#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1949 default:
1950 /* Key is declared with a lifetime not known to us */
1951 (void) status;
1952 (void) key_buffer;
1953 (void) key_buffer_size;
1954 (void) alg;
1955 return( PSA_ERROR_INVALID_ARGUMENT );
1956 }
1957}
1958
1959psa_status_t psa_driver_wrapper_mac_verify_setup(
1960 psa_mac_operation_t *operation,
1961 const psa_key_attributes_t *attributes,
1962 const uint8_t *key_buffer,
1963 size_t key_buffer_size,
1964 psa_algorithm_t alg )
1965{
1966 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1967 psa_key_location_t location =
1968 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1969
1970 switch( location )
1971 {
1972 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1973 /* Key is stored in the slot in export representation, so
1974 * cycle through all known transparent accelerators */
1975#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1976#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001977 status = mbedtls_test_transparent_mac_verify_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001978 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001979 attributes,
1980 key_buffer, key_buffer_size,
1981 alg );
1982 /* Declared with fallback == true */
1983 if( status == PSA_SUCCESS )
1984 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1985
1986 if( status != PSA_ERROR_NOT_SUPPORTED )
1987 return( status );
1988#endif /* PSA_CRYPTO_DRIVER_TEST */
1989#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1990#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1991 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001992 status = mbedtls_psa_mac_verify_setup( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001993 attributes,
1994 key_buffer, key_buffer_size,
1995 alg );
1996 if( status == PSA_SUCCESS )
1997 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1998
1999 if( status != PSA_ERROR_NOT_SUPPORTED )
2000 return( status );
2001#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2002 return( PSA_ERROR_NOT_SUPPORTED );
2003
2004 /* Add cases for opaque driver here */
2005#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2006#if defined(PSA_CRYPTO_DRIVER_TEST)
2007 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002008 status = mbedtls_test_opaque_mac_verify_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002009 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002010 attributes,
2011 key_buffer, key_buffer_size,
2012 alg );
2013
2014 if( status == PSA_SUCCESS )
2015 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
2016
2017 return( status );
2018#endif /* PSA_CRYPTO_DRIVER_TEST */
2019#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2020 default:
2021 /* Key is declared with a lifetime not known to us */
2022 (void) status;
2023 (void) key_buffer;
2024 (void) key_buffer_size;
2025 (void) alg;
2026 return( PSA_ERROR_INVALID_ARGUMENT );
2027 }
2028}
2029
2030psa_status_t psa_driver_wrapper_mac_update(
2031 psa_mac_operation_t *operation,
2032 const uint8_t *input,
2033 size_t input_length )
2034{
2035 switch( operation->id )
2036 {
2037#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2038 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002039 return( mbedtls_psa_mac_update( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002040 input, input_length ) );
2041#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2042
2043#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2044#if defined(PSA_CRYPTO_DRIVER_TEST)
2045 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002046 return( mbedtls_test_transparent_mac_update(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002047 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002048 input, input_length ) );
2049
2050 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002051 return( mbedtls_test_opaque_mac_update(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002052 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002053 input, input_length ) );
2054#endif /* PSA_CRYPTO_DRIVER_TEST */
2055#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2056 default:
2057 (void) input;
2058 (void) input_length;
2059 return( PSA_ERROR_INVALID_ARGUMENT );
2060 }
2061}
2062
2063psa_status_t psa_driver_wrapper_mac_sign_finish(
2064 psa_mac_operation_t *operation,
2065 uint8_t *mac,
2066 size_t mac_size,
2067 size_t *mac_length )
2068{
2069 switch( operation->id )
2070 {
2071#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2072 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002073 return( mbedtls_psa_mac_sign_finish( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002074 mac, mac_size, mac_length ) );
2075#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2076
2077#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2078#if defined(PSA_CRYPTO_DRIVER_TEST)
2079 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002080 return( mbedtls_test_transparent_mac_sign_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002081 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002082 mac, mac_size, mac_length ) );
2083
2084 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002085 return( mbedtls_test_opaque_mac_sign_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002086 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002087 mac, mac_size, mac_length ) );
2088#endif /* PSA_CRYPTO_DRIVER_TEST */
2089#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2090 default:
2091 (void) mac;
2092 (void) mac_size;
2093 (void) mac_length;
2094 return( PSA_ERROR_INVALID_ARGUMENT );
2095 }
2096}
2097
2098psa_status_t psa_driver_wrapper_mac_verify_finish(
2099 psa_mac_operation_t *operation,
2100 const uint8_t *mac,
2101 size_t mac_length )
2102{
2103 switch( operation->id )
2104 {
2105#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2106 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002107 return( mbedtls_psa_mac_verify_finish( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002108 mac, mac_length ) );
2109#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2110
2111#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2112#if defined(PSA_CRYPTO_DRIVER_TEST)
2113 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002114 return( mbedtls_test_transparent_mac_verify_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002115 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002116 mac, mac_length ) );
2117
2118 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002119 return( mbedtls_test_opaque_mac_verify_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002120 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002121 mac, mac_length ) );
2122#endif /* PSA_CRYPTO_DRIVER_TEST */
2123#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2124 default:
2125 (void) mac;
2126 (void) mac_length;
2127 return( PSA_ERROR_INVALID_ARGUMENT );
2128 }
2129}
2130
2131psa_status_t psa_driver_wrapper_mac_abort(
2132 psa_mac_operation_t *operation )
2133{
Steven Cooremand13a70f2021-03-19 15:24:23 +01002134 switch( operation->id )
2135 {
2136#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2137 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooremane6804192021-03-19 18:28:56 +01002138 return( mbedtls_psa_mac_abort( &operation->ctx.mbedtls_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002139#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2140
2141#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2142#if defined(PSA_CRYPTO_DRIVER_TEST)
2143 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002144 return( mbedtls_test_transparent_mac_abort(
Steven Cooremane6804192021-03-19 18:28:56 +01002145 &operation->ctx.transparent_test_driver_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002146 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002147 return( mbedtls_test_opaque_mac_abort(
Steven Cooremane6804192021-03-19 18:28:56 +01002148 &operation->ctx.opaque_test_driver_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002149#endif /* PSA_CRYPTO_DRIVER_TEST */
2150#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2151 default:
Steven Cooremane6804192021-03-19 18:28:56 +01002152 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002153 }
Steven Cooremand13a70f2021-03-19 15:24:23 +01002154}
Gilles Peskine1905a242021-04-24 13:19:45 +02002155
2156#endif /* MBEDTLS_PSA_CRYPTO_C */