blob: 5fba955477e6903efd5ba6556fdefe9052424cde [file] [log] [blame]
Steven Cooremancd84cb42020-07-16 20:28:36 +02001/*
2 * Functions to delegate cryptographic operations to an available
3 * and appropriate accelerator.
Steven Cooreman56250fd2020-09-04 13:07:15 +02004 * Warning: This file will be auto-generated in the future.
Steven Cooremancd84cb42020-07-16 20:28:36 +02005 */
Steven Cooreman2c7b2f82020-09-02 13:43:46 +02006/* Copyright The Mbed TLS Contributors
Steven Cooremancd84cb42020-07-16 20:28:36 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Steven Cooremancd84cb42020-07-16 20:28:36 +020020 */
21
Ronald Cronde822812021-03-17 16:08:20 +010022#include "psa_crypto_aead.h"
Ronald Cron0b805592020-12-14 18:08:20 +010023#include "psa_crypto_cipher.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020024#include "psa_crypto_core.h"
25#include "psa_crypto_driver_wrappers.h"
Steven Cooreman830aff22021-03-09 09:50:44 +010026#include "psa_crypto_hash.h"
Steven Cooreman896d51e2021-03-19 15:24:23 +010027#include "psa_crypto_mac.h"
Steven Cooreman830aff22021-03-09 09:50:44 +010028
Steven Cooreman2a1664c2020-07-20 15:33:08 +020029#include "mbedtls/platform.h"
30
Gilles Peskinead0e0122021-04-24 13:19:45 +020031#if defined(MBEDTLS_PSA_CRYPTO_C)
32
Steven Cooreman2a1664c2020-07-20 15:33:08 +020033#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
Steven Cooremancd84cb42020-07-16 20:28:36 +020034
35/* Include test driver definition when running tests */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020036#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman56250fd2020-09-04 13:07:15 +020037#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020038#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020039#endif
40#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020041#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020042#endif
Ronald Cron05ee58d2021-04-30 17:00:34 +020043#include "test/drivers/test_driver.h"
Steven Cooremanf1720ea2020-07-24 18:41:58 +020044#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +020045
Steven Cooreman56250fd2020-09-04 13:07:15 +020046/* Repeat above block for each JSON-declared driver during autogeneration */
Steven Cooremanaa87fd02021-03-15 18:54:03 +010047#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Steven Cooreman56250fd2020-09-04 13:07:15 +020048
Steven Cooreman1e582352021-02-18 17:24:37 +010049/* Auto-generated values depending on which drivers are registered.
50 * ID 0 is reserved for unallocated operations.
51 * ID 1 is reserved for the Mbed TLS software driver. */
Steven Cooreman830aff22021-03-09 09:50:44 +010052#define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
53
Steven Cooreman37941cb2020-07-28 18:49:51 +020054#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman1e582352021-02-18 17:24:37 +010055#define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2)
56#define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3)
Steven Cooreman37941cb2020-07-28 18:49:51 +020057#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +020058
59/* Support the 'old' SE interface when asked to */
Steven Cooreman7a250572020-07-17 16:43:05 +020060#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman56250fd2020-09-04 13:07:15 +020061/* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
62 * SE driver is present, to avoid unused argument errors at compile time. */
63#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020064#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020065#endif
Steven Cooreman7a250572020-07-17 16:43:05 +020066#include "psa_crypto_se.h"
67#endif
68
Steven Cooremancd84cb42020-07-16 20:28:36 +020069/* Start delegation functions */
gabor-mezei-armc53f4f62021-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-armc53f4f62021-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-arma7b9b202021-05-07 15:16:34 +0200107 break;
gabor-mezei-armc53f4f62021-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-arm77588fb2021-04-27 18:52:51 +0200112 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arma7b9b202021-05-07 15:16:34 +0200113 status = mbedtls_test_opaque_signature_sign_message(
gabor-mezei-armc53f4f62021-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-arma7b9b202021-05-07 15:16:34 +0200122 signature_length );
123 if( status != PSA_ERROR_NOT_SUPPORTED )
124 return( status );
125 break;
gabor-mezei-armc53f4f62021-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-armf25c9762021-05-12 11:12:25 +0200131 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200132 }
gabor-mezei-arma7b9b202021-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-armc53f4f62021-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-armc53f4f62021-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-arma7b9b202021-05-07 15:16:34 +0200180 break;
gabor-mezei-armc53f4f62021-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-arm77588fb2021-04-27 18:52:51 +0200185 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-armc53f4f62021-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-arma7b9b202021-05-07 15:16:34 +0200195 if( status != PSA_ERROR_NOT_SUPPORTED )
196 return( status );
197 break;
gabor-mezei-armc53f4f62021-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-armf25c9762021-05-12 11:12:25 +0200203 break;
gabor-mezei-armc53f4f62021-04-22 11:32:19 +0200204 }
gabor-mezei-arma7b9b202021-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-armc53f4f62021-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 Cronc4bc12e2021-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-armf3c5c862021-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 Cronc4bc12e2021-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 Cronc4bc12e2021-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-armf3c5c862021-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 Cronc4bc12e2021-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 Cronc4bc12e2021-04-13 12:41:34 +0200420 *key_buffer_size = mbedtls_test_size_function( key_type, key_bits );
Ronald Cronb0737da2021-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 Cronc4bc12e2021-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 Cronc4bc12e2021-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 Cronc4bc12e2021-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 Cronc4bc12e2021-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 Cronc4bc12e2021-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 Cronc4bc12e2021-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 Cronc4bc12e2021-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(
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100739 const psa_key_attributes_t *attributes,
740 const uint8_t *key_buffer,
741 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200742 psa_algorithm_t alg,
Ronald Crona8331692021-07-09 09:19:35 +0200743 const uint8_t *iv,
744 size_t iv_length,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200745 const uint8_t *input,
746 size_t input_length,
747 uint8_t *output,
748 size_t output_size,
749 size_t *output_length )
750{
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100751 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
752 psa_key_location_t location =
753 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200754
755 switch( location )
756 {
757 case PSA_KEY_LOCATION_LOCAL_STORAGE:
758 /* Key is stored in the slot in export representation, so
759 * cycle through all known transparent accelerators */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100760#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200761#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100762 status = mbedtls_test_transparent_cipher_encrypt( attributes,
763 key_buffer,
764 key_buffer_size,
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200765 alg,
Ronald Crona8331692021-07-09 09:19:35 +0200766 iv,
767 iv_length,
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200768 input,
769 input_length,
770 output,
771 output_size,
772 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200773 /* Declared with fallback == true */
774 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200775 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200776#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100777#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
778
779#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
780 return( mbedtls_psa_cipher_encrypt( attributes,
781 key_buffer,
782 key_buffer_size,
783 alg,
Ronald Crona8331692021-07-09 09:19:35 +0200784 iv,
785 iv_length,
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100786 input,
787 input_length,
788 output,
789 output_size,
790 output_length ) );
gabor-mezei-armd52c8dc2021-06-25 15:43:32 +0200791#else
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200792 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-armd52c8dc2021-06-25 15:43:32 +0200793#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100794
Steven Cooreman37941cb2020-07-28 18:49:51 +0200795 /* Add cases for opaque driver here */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100796#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200797#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100798 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100799 return( mbedtls_test_opaque_cipher_encrypt( attributes,
800 key_buffer,
801 key_buffer_size,
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200802 alg,
Ronald Crona8331692021-07-09 09:19:35 +0200803 iv,
804 iv_length,
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200805 input,
806 input_length,
807 output,
808 output_size,
809 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200810#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100811#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
812
Steven Cooreman37941cb2020-07-28 18:49:51 +0200813 default:
814 /* Key is declared with a lifetime not known to us */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100815 (void)status;
816 (void)key_buffer;
817 (void)key_buffer_size;
818 (void)alg;
819 (void)input;
820 (void)input_length;
821 (void)output;
822 (void)output_size;
823 (void)output_length;
824 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200825 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200826}
827
828psa_status_t psa_driver_wrapper_cipher_decrypt(
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100829 const psa_key_attributes_t *attributes,
830 const uint8_t *key_buffer,
831 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200832 psa_algorithm_t alg,
833 const uint8_t *input,
834 size_t input_length,
835 uint8_t *output,
836 size_t output_size,
837 size_t *output_length )
838{
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100839 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
840 psa_key_location_t location =
841 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200842
843 switch( location )
844 {
845 case PSA_KEY_LOCATION_LOCAL_STORAGE:
846 /* Key is stored in the slot in export representation, so
847 * cycle through all known transparent accelerators */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100848#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200849#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100850 status = mbedtls_test_transparent_cipher_decrypt( attributes,
851 key_buffer,
852 key_buffer_size,
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200853 alg,
854 input,
855 input_length,
856 output,
857 output_size,
858 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200859 /* Declared with fallback == true */
860 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200861 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200862#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100863#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
864
865#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
866 return( mbedtls_psa_cipher_decrypt( attributes,
867 key_buffer,
868 key_buffer_size,
869 alg,
870 input,
871 input_length,
872 output,
873 output_size,
874 output_length ) );
gabor-mezei-armd52c8dc2021-06-25 15:43:32 +0200875#else
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200876 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-armd52c8dc2021-06-25 15:43:32 +0200877#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100878
Steven Cooreman37941cb2020-07-28 18:49:51 +0200879 /* Add cases for opaque driver here */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100880#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200881#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100882 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100883 return( mbedtls_test_opaque_cipher_decrypt( attributes,
884 key_buffer,
885 key_buffer_size,
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200886 alg,
887 input,
888 input_length,
889 output,
890 output_size,
891 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200892#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100893#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
894
Steven Cooreman37941cb2020-07-28 18:49:51 +0200895 default:
896 /* Key is declared with a lifetime not known to us */
gabor-mezei-armfa990b52021-03-25 11:17:10 +0100897 (void)status;
898 (void)key_buffer;
899 (void)key_buffer_size;
900 (void)alg;
901 (void)input;
902 (void)input_length;
903 (void)output;
904 (void)output_size;
905 (void)output_length;
906 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200907 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200908}
909
910psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100911 psa_cipher_operation_t *operation,
912 const psa_key_attributes_t *attributes,
913 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200914 psa_algorithm_t alg )
915{
Ronald Cron0b805592020-12-14 18:08:20 +0100916 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona4af55f2020-12-14 14:36:06 +0100917 psa_key_location_t location =
918 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200919
Steven Cooreman37941cb2020-07-28 18:49:51 +0200920 switch( location )
921 {
922 case PSA_KEY_LOCATION_LOCAL_STORAGE:
923 /* Key is stored in the slot in export representation, so
924 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100925#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200926#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200927 status = mbedtls_test_transparent_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100928 &operation->ctx.transparent_test_driver_ctx,
929 attributes,
930 key_buffer,
931 key_buffer_size,
932 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200933 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200934 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100935 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +0200936
Ronald Cron0b805592020-12-14 18:08:20 +0100937 if( status != PSA_ERROR_NOT_SUPPORTED )
938 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200939#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100940#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100941#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200942 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +0100943 status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100944 attributes,
945 key_buffer,
946 key_buffer_size,
947 alg );
948 if( status == PSA_SUCCESS )
Ronald Cron6e412a72021-03-10 09:58:47 +0100949 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Ronald Cron49fafa92021-03-10 08:34:23 +0100950
Ronald Cron7b4154d2021-03-19 14:49:41 +0100951 if( status != PSA_ERROR_NOT_SUPPORTED )
952 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100953#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
954 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron0b805592020-12-14 18:08:20 +0100955
Steven Cooreman37941cb2020-07-28 18:49:51 +0200956 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100957#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200958#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100959 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200960 status = mbedtls_test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100961 &operation->ctx.opaque_test_driver_ctx,
962 attributes,
963 key_buffer, key_buffer_size,
964 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200965
Steven Cooreman37941cb2020-07-28 18:49:51 +0200966 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100967 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200968
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200969 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200970#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100971#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +0200972 default:
973 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +0100974 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100975 (void)key_buffer;
976 (void)key_buffer_size;
977 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +0200978 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200979 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200980}
981
982psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100983 psa_cipher_operation_t *operation,
984 const psa_key_attributes_t *attributes,
985 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200986 psa_algorithm_t alg )
987{
Steven Cooreman37941cb2020-07-28 18:49:51 +0200988 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crona4af55f2020-12-14 14:36:06 +0100989 psa_key_location_t location =
990 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200991
Steven Cooreman37941cb2020-07-28 18:49:51 +0200992 switch( location )
993 {
994 case PSA_KEY_LOCATION_LOCAL_STORAGE:
995 /* Key is stored in the slot in export representation, so
996 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100997#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200998#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200999 status = mbedtls_test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001000 &operation->ctx.transparent_test_driver_ctx,
1001 attributes,
1002 key_buffer,
1003 key_buffer_size,
1004 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001005 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +02001006 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001007 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +02001008
Ronald Cron0b805592020-12-14 18:08:20 +01001009 if( status != PSA_ERROR_NOT_SUPPORTED )
1010 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001011#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001012#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001013#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001014 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +01001015 status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001016 attributes,
1017 key_buffer,
1018 key_buffer_size,
1019 alg );
1020 if( status == PSA_SUCCESS )
1021 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1022
1023 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001024#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1025 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron49fafa92021-03-10 08:34:23 +01001026
Steven Cooreman37941cb2020-07-28 18:49:51 +02001027 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +01001028#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001029#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +01001030 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001031 status = mbedtls_test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001032 &operation->ctx.opaque_test_driver_ctx,
1033 attributes,
1034 key_buffer, key_buffer_size,
1035 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001036
Steven Cooreman37941cb2020-07-28 18:49:51 +02001037 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001038 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001039
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001040 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001041#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001042#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +02001043 default:
1044 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +01001045 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001046 (void)key_buffer;
1047 (void)key_buffer_size;
1048 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +02001049 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001050 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001051}
1052
Steven Cooreman37941cb2020-07-28 18:49:51 +02001053psa_status_t psa_driver_wrapper_cipher_set_iv(
Ronald Cron6056fe82020-12-15 13:58:07 +01001054 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001055 const uint8_t *iv,
1056 size_t iv_length )
1057{
Ronald Cron49fafa92021-03-10 08:34:23 +01001058 switch( operation->id )
1059 {
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_set_iv( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001063 iv,
1064 iv_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001065#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001066
1067#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001068#if defined(PSA_CRYPTO_DRIVER_TEST)
1069 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001070 return( mbedtls_test_transparent_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001071 &operation->ctx.transparent_test_driver_ctx,
1072 iv, iv_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001073
Steven Cooreman37941cb2020-07-28 18:49:51 +02001074 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001075 return( mbedtls_test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001076 &operation->ctx.opaque_test_driver_ctx,
1077 iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001078#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001079#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001080 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001081
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001082 (void)iv;
1083 (void)iv_length;
1084
Ronald Crondd24c9b2020-12-15 14:10:01 +01001085 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001086}
1087
1088psa_status_t psa_driver_wrapper_cipher_update(
Ronald Cron6056fe82020-12-15 13:58:07 +01001089 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001090 const uint8_t *input,
1091 size_t input_length,
1092 uint8_t *output,
1093 size_t output_size,
1094 size_t *output_length )
1095{
Ronald Cron49fafa92021-03-10 08:34:23 +01001096 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001097 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001098#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001099 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001100 return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001101 input,
1102 input_length,
1103 output,
1104 output_size,
1105 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001106#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1107
Ronald Cron49fafa92021-03-10 08:34:23 +01001108#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001109#if defined(PSA_CRYPTO_DRIVER_TEST)
1110 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001111 return( mbedtls_test_transparent_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001112 &operation->ctx.transparent_test_driver_ctx,
1113 input, input_length,
1114 output, output_size, output_length ) );
1115
Steven Cooreman37941cb2020-07-28 18:49:51 +02001116 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001117 return( mbedtls_test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001118 &operation->ctx.opaque_test_driver_ctx,
1119 input, input_length,
1120 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001121#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001122#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001123 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001124
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001125 (void)input;
1126 (void)input_length;
1127 (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_finish(
Ronald Cron6056fe82020-12-15 13:58:07 +01001135 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001136 uint8_t *output,
1137 size_t output_size,
1138 size_t *output_length )
1139{
Ronald Cron49fafa92021-03-10 08:34:23 +01001140 switch( operation->id )
1141 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001142#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001143 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001144 return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001145 output,
1146 output_size,
1147 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001148#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001149
1150#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001151#if defined(PSA_CRYPTO_DRIVER_TEST)
1152 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001153 return( mbedtls_test_transparent_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001154 &operation->ctx.transparent_test_driver_ctx,
1155 output, output_size, output_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001156
Steven Cooreman37941cb2020-07-28 18:49:51 +02001157 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001158 return( mbedtls_test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001159 &operation->ctx.opaque_test_driver_ctx,
1160 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001161#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001162#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001163 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001164
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001165 (void)output;
1166 (void)output_size;
1167 (void)output_length;
1168
Ronald Crondd24c9b2020-12-15 14:10:01 +01001169 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001170}
1171
1172psa_status_t psa_driver_wrapper_cipher_abort(
Ronald Cron6056fe82020-12-15 13:58:07 +01001173 psa_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001174{
Ronald Crondd24c9b2020-12-15 14:10:01 +01001175 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001176
Ronald Cron49fafa92021-03-10 08:34:23 +01001177 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001178 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001179#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001180 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001181 return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001182#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +01001183
1184#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001185#if defined(PSA_CRYPTO_DRIVER_TEST)
1186 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001187 status = mbedtls_test_transparent_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001188 &operation->ctx.transparent_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001189 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001190 &operation->ctx.transparent_test_driver_ctx,
1191 sizeof( operation->ctx.transparent_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001192 return( status );
Ronald Cron49fafa92021-03-10 08:34:23 +01001193
Steven Cooreman37941cb2020-07-28 18:49:51 +02001194 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001195 status = mbedtls_test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001196 &operation->ctx.opaque_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001197 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001198 &operation->ctx.opaque_test_driver_ctx,
1199 sizeof( operation->ctx.opaque_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001200 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001201#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001202#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001203 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001204
Ronald Cron49fafa92021-03-10 08:34:23 +01001205 (void)status;
Ronald Crondd24c9b2020-12-15 14:10:01 +01001206 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001207}
1208
Steven Cooreman1e582352021-02-18 17:24:37 +01001209/*
1210 * Hashing functions
1211 */
1212psa_status_t psa_driver_wrapper_hash_compute(
1213 psa_algorithm_t alg,
1214 const uint8_t *input,
1215 size_t input_length,
1216 uint8_t *hash,
1217 size_t hash_size,
1218 size_t *hash_length)
1219{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001220 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001221
1222 /* Try accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001223#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001224 status = mbedtls_test_transparent_hash_compute(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001225 alg, input, input_length, hash, hash_size, hash_length );
Steven Cooremanf7638102021-03-04 15:14:36 +01001226 if( status != PSA_ERROR_NOT_SUPPORTED )
1227 return( status );
1228#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001229
1230 /* If software fallback is compiled in, try fallback */
1231#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1232 status = mbedtls_psa_hash_compute( alg, input, input_length,
1233 hash, hash_size, hash_length );
1234 if( status != PSA_ERROR_NOT_SUPPORTED )
1235 return( status );
1236#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001237 (void) status;
1238 (void) alg;
1239 (void) input;
1240 (void) input_length;
1241 (void) hash;
1242 (void) hash_size;
1243 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001244
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001245 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman1e582352021-02-18 17:24:37 +01001246}
1247
1248psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001249 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001250 psa_algorithm_t alg )
1251{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001252 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001253
Steven Cooreman1e582352021-02-18 17:24:37 +01001254 /* Try setup on accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001255#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001256 status = mbedtls_test_transparent_hash_setup(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001257 &operation->ctx.test_driver_ctx, alg );
Steven Cooremanf7638102021-03-04 15:14:36 +01001258 if( status == PSA_SUCCESS )
1259 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1260
1261 if( status != PSA_ERROR_NOT_SUPPORTED )
1262 return( status );
1263#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001264
1265 /* If software fallback is compiled in, try fallback */
1266#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001267 status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
Steven Cooreman1e582352021-02-18 17:24:37 +01001268 if( status == PSA_SUCCESS )
Steven Cooreman1e582352021-02-18 17:24:37 +01001269 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Steven Cooreman1e582352021-02-18 17:24:37 +01001270
1271 if( status != PSA_ERROR_NOT_SUPPORTED )
1272 return( status );
1273#endif
1274 /* Nothing left to try if we fall through here */
1275 (void) status;
1276 (void) operation;
1277 (void) alg;
1278 return( PSA_ERROR_NOT_SUPPORTED );
1279}
1280
1281psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001282 const psa_hash_operation_t *source_operation,
1283 psa_hash_operation_t *target_operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001284{
Steven Cooreman1e582352021-02-18 17:24:37 +01001285 switch( source_operation->id )
1286 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001287#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1288 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1289 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1290 return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
1291 &target_operation->ctx.mbedtls_ctx ) );
1292#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001293#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001294 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1295 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001296 return( mbedtls_test_transparent_hash_clone(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001297 &source_operation->ctx.test_driver_ctx,
1298 &target_operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001299#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001300 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001301 (void) target_operation;
1302 return( PSA_ERROR_BAD_STATE );
1303 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001304}
1305
1306psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001307 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001308 const uint8_t *input,
1309 size_t input_length )
1310{
Steven Cooreman1e582352021-02-18 17:24:37 +01001311 switch( operation->id )
1312 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001313#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1314 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1315 return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
1316 input, input_length ) );
1317#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001318#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001319 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001320 return( mbedtls_test_transparent_hash_update(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001321 &operation->ctx.test_driver_ctx,
1322 input, input_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001323#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001324 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001325 (void) input;
1326 (void) input_length;
1327 return( PSA_ERROR_BAD_STATE );
1328 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001329}
1330
1331psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001332 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001333 uint8_t *hash,
1334 size_t hash_size,
1335 size_t *hash_length )
1336{
Steven Cooreman1e582352021-02-18 17:24:37 +01001337 switch( operation->id )
1338 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001339#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1340 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1341 return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
1342 hash, hash_size, hash_length ) );
1343#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001344#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001345 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001346 return( mbedtls_test_transparent_hash_finish(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001347 &operation->ctx.test_driver_ctx,
1348 hash, hash_size, hash_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001349#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001350 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001351 (void) hash;
1352 (void) hash_size;
1353 (void) hash_length;
1354 return( PSA_ERROR_BAD_STATE );
1355 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001356}
1357
1358psa_status_t psa_driver_wrapper_hash_abort(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001359 psa_hash_operation_t *operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001360{
Steven Cooreman1e582352021-02-18 17:24:37 +01001361 switch( operation->id )
1362 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001363#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1364 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1365 return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
1366#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001367#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001368 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001369 return( mbedtls_test_transparent_hash_abort(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001370 &operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001371#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001372 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001373 return( PSA_ERROR_BAD_STATE );
1374 }
1375}
1376
Ronald Cronde822812021-03-17 16:08:20 +01001377psa_status_t psa_driver_wrapper_aead_encrypt(
1378 const psa_key_attributes_t *attributes,
1379 const uint8_t *key_buffer, size_t key_buffer_size,
1380 psa_algorithm_t alg,
1381 const uint8_t *nonce, size_t nonce_length,
1382 const uint8_t *additional_data, size_t additional_data_length,
1383 const uint8_t *plaintext, size_t plaintext_length,
1384 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
1385{
1386 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1387 psa_key_location_t location =
1388 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1389
1390 switch( location )
1391 {
1392 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1393 /* Key is stored in the slot in export representation, so
1394 * cycle through all known transparent accelerators */
1395
1396#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1397#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001398 status = mbedtls_test_transparent_aead_encrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001399 attributes, key_buffer, key_buffer_size,
1400 alg,
1401 nonce, nonce_length,
1402 additional_data, additional_data_length,
1403 plaintext, plaintext_length,
1404 ciphertext, ciphertext_size, ciphertext_length );
1405 /* Declared with fallback == true */
1406 if( status != PSA_ERROR_NOT_SUPPORTED )
1407 return( status );
1408#endif /* PSA_CRYPTO_DRIVER_TEST */
1409#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1410
1411 /* Fell through, meaning no accelerator supports this operation */
1412 return( mbedtls_psa_aead_encrypt(
1413 attributes, key_buffer, key_buffer_size,
1414 alg,
1415 nonce, nonce_length,
1416 additional_data, additional_data_length,
1417 plaintext, plaintext_length,
1418 ciphertext, ciphertext_size, ciphertext_length ) );
1419
1420 /* Add cases for opaque driver here */
1421
1422 default:
1423 /* Key is declared with a lifetime not known to us */
1424 (void)status;
1425 return( PSA_ERROR_INVALID_ARGUMENT );
1426 }
1427}
1428
1429psa_status_t psa_driver_wrapper_aead_decrypt(
1430 const psa_key_attributes_t *attributes,
1431 const uint8_t *key_buffer, size_t key_buffer_size,
1432 psa_algorithm_t alg,
1433 const uint8_t *nonce, size_t nonce_length,
1434 const uint8_t *additional_data, size_t additional_data_length,
1435 const uint8_t *ciphertext, size_t ciphertext_length,
1436 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
1437{
1438 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1439 psa_key_location_t location =
1440 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1441
1442 switch( location )
1443 {
1444 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1445 /* Key is stored in the slot in export representation, so
1446 * cycle through all known transparent accelerators */
1447
1448#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1449#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cronc4bc12e2021-04-13 12:41:34 +02001450 status = mbedtls_test_transparent_aead_decrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001451 attributes, key_buffer, key_buffer_size,
1452 alg,
1453 nonce, nonce_length,
1454 additional_data, additional_data_length,
1455 ciphertext, ciphertext_length,
1456 plaintext, plaintext_size, plaintext_length );
1457 /* Declared with fallback == true */
1458 if( status != PSA_ERROR_NOT_SUPPORTED )
1459 return( status );
1460#endif /* PSA_CRYPTO_DRIVER_TEST */
1461#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1462
1463 /* Fell through, meaning no accelerator supports this operation */
1464 return( mbedtls_psa_aead_decrypt(
1465 attributes, key_buffer, key_buffer_size,
1466 alg,
1467 nonce, nonce_length,
1468 additional_data, additional_data_length,
1469 ciphertext, ciphertext_length,
1470 plaintext, plaintext_size, plaintext_length ) );
1471
1472 /* Add cases for opaque driver here */
1473
1474 default:
1475 /* Key is declared with a lifetime not known to us */
1476 (void)status;
1477 return( PSA_ERROR_INVALID_ARGUMENT );
1478 }
1479}
Steven Cooreman896d51e2021-03-19 15:24:23 +01001480
1481
1482/*
1483 * MAC functions
1484 */
1485psa_status_t psa_driver_wrapper_mac_compute(
1486 const psa_key_attributes_t *attributes,
1487 const uint8_t *key_buffer,
1488 size_t key_buffer_size,
1489 psa_algorithm_t alg,
1490 const uint8_t *input,
1491 size_t input_length,
1492 uint8_t *mac,
1493 size_t mac_size,
1494 size_t *mac_length )
1495{
1496 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1497 psa_key_location_t location =
1498 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1499
1500 switch( location )
1501 {
1502 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1503 /* Key is stored in the slot in export representation, so
1504 * cycle through all known transparent accelerators */
1505#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1506#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001507 status = mbedtls_test_transparent_mac_compute(
Steven Cooreman896d51e2021-03-19 15:24:23 +01001508 attributes, key_buffer, key_buffer_size, alg,
1509 input, input_length,
1510 mac, mac_size, mac_length );
1511 /* Declared with fallback == true */
1512 if( status != PSA_ERROR_NOT_SUPPORTED )
1513 return( status );
1514#endif /* PSA_CRYPTO_DRIVER_TEST */
1515#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1516#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1517 /* Fell through, meaning no accelerator supports this operation */
1518 status = mbedtls_psa_mac_compute(
1519 attributes, key_buffer, key_buffer_size, alg,
1520 input, input_length,
1521 mac, mac_size, mac_length );
1522 if( status != PSA_ERROR_NOT_SUPPORTED )
1523 return( status );
1524#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1525 return( PSA_ERROR_NOT_SUPPORTED );
1526
1527 /* Add cases for opaque driver here */
1528#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1529#if defined(PSA_CRYPTO_DRIVER_TEST)
1530 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001531 status = mbedtls_test_opaque_mac_compute(
Steven Cooreman896d51e2021-03-19 15:24:23 +01001532 attributes, key_buffer, key_buffer_size, alg,
1533 input, input_length,
1534 mac, mac_size, mac_length );
1535 return( status );
1536#endif /* PSA_CRYPTO_DRIVER_TEST */
1537#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1538 default:
1539 /* Key is declared with a lifetime not known to us */
1540 (void) key_buffer;
1541 (void) key_buffer_size;
1542 (void) alg;
1543 (void) input;
1544 (void) input_length;
1545 (void) mac;
1546 (void) mac_size;
1547 (void) mac_length;
1548 (void) status;
1549 return( PSA_ERROR_INVALID_ARGUMENT );
1550 }
1551}
1552
1553psa_status_t psa_driver_wrapper_mac_sign_setup(
1554 psa_mac_operation_t *operation,
1555 const psa_key_attributes_t *attributes,
1556 const uint8_t *key_buffer,
1557 size_t key_buffer_size,
1558 psa_algorithm_t alg )
1559{
1560 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1561 psa_key_location_t location =
1562 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1563
1564 switch( location )
1565 {
1566 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1567 /* Key is stored in the slot in export representation, so
1568 * cycle through all known transparent accelerators */
1569#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1570#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001571 status = mbedtls_test_transparent_mac_sign_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001572 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001573 attributes,
1574 key_buffer, key_buffer_size,
1575 alg );
1576 /* Declared with fallback == true */
1577 if( status == PSA_SUCCESS )
1578 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1579
1580 if( status != PSA_ERROR_NOT_SUPPORTED )
1581 return( status );
1582#endif /* PSA_CRYPTO_DRIVER_TEST */
1583#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1584#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1585 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001586 status = mbedtls_psa_mac_sign_setup( &operation->ctx.mbedtls_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001587 attributes,
1588 key_buffer, key_buffer_size,
1589 alg );
1590 if( status == PSA_SUCCESS )
1591 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1592
1593 if( status != PSA_ERROR_NOT_SUPPORTED )
1594 return( status );
1595#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1596 return( PSA_ERROR_NOT_SUPPORTED );
1597
1598 /* Add cases for opaque driver here */
1599#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1600#if defined(PSA_CRYPTO_DRIVER_TEST)
1601 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001602 status = mbedtls_test_opaque_mac_sign_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001603 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001604 attributes,
1605 key_buffer, key_buffer_size,
1606 alg );
1607
1608 if( status == PSA_SUCCESS )
1609 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
1610
1611 return( status );
1612#endif /* PSA_CRYPTO_DRIVER_TEST */
1613#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1614 default:
1615 /* Key is declared with a lifetime not known to us */
1616 (void) status;
1617 (void) key_buffer;
1618 (void) key_buffer_size;
1619 (void) alg;
1620 return( PSA_ERROR_INVALID_ARGUMENT );
1621 }
1622}
1623
1624psa_status_t psa_driver_wrapper_mac_verify_setup(
1625 psa_mac_operation_t *operation,
1626 const psa_key_attributes_t *attributes,
1627 const uint8_t *key_buffer,
1628 size_t key_buffer_size,
1629 psa_algorithm_t alg )
1630{
1631 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1632 psa_key_location_t location =
1633 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1634
1635 switch( location )
1636 {
1637 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1638 /* Key is stored in the slot in export representation, so
1639 * cycle through all known transparent accelerators */
1640#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1641#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001642 status = mbedtls_test_transparent_mac_verify_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001643 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001644 attributes,
1645 key_buffer, key_buffer_size,
1646 alg );
1647 /* Declared with fallback == true */
1648 if( status == PSA_SUCCESS )
1649 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1650
1651 if( status != PSA_ERROR_NOT_SUPPORTED )
1652 return( status );
1653#endif /* PSA_CRYPTO_DRIVER_TEST */
1654#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1655#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1656 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001657 status = mbedtls_psa_mac_verify_setup( &operation->ctx.mbedtls_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001658 attributes,
1659 key_buffer, key_buffer_size,
1660 alg );
1661 if( status == PSA_SUCCESS )
1662 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1663
1664 if( status != PSA_ERROR_NOT_SUPPORTED )
1665 return( status );
1666#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1667 return( PSA_ERROR_NOT_SUPPORTED );
1668
1669 /* Add cases for opaque driver here */
1670#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1671#if defined(PSA_CRYPTO_DRIVER_TEST)
1672 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001673 status = mbedtls_test_opaque_mac_verify_setup(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001674 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001675 attributes,
1676 key_buffer, key_buffer_size,
1677 alg );
1678
1679 if( status == PSA_SUCCESS )
1680 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
1681
1682 return( status );
1683#endif /* PSA_CRYPTO_DRIVER_TEST */
1684#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1685 default:
1686 /* Key is declared with a lifetime not known to us */
1687 (void) status;
1688 (void) key_buffer;
1689 (void) key_buffer_size;
1690 (void) alg;
1691 return( PSA_ERROR_INVALID_ARGUMENT );
1692 }
1693}
1694
1695psa_status_t psa_driver_wrapper_mac_update(
1696 psa_mac_operation_t *operation,
1697 const uint8_t *input,
1698 size_t input_length )
1699{
1700 switch( operation->id )
1701 {
1702#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1703 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001704 return( mbedtls_psa_mac_update( &operation->ctx.mbedtls_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001705 input, input_length ) );
1706#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1707
1708#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1709#if defined(PSA_CRYPTO_DRIVER_TEST)
1710 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001711 return( mbedtls_test_transparent_mac_update(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001712 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001713 input, input_length ) );
1714
1715 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001716 return( mbedtls_test_opaque_mac_update(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001717 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001718 input, input_length ) );
1719#endif /* PSA_CRYPTO_DRIVER_TEST */
1720#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1721 default:
1722 (void) input;
1723 (void) input_length;
1724 return( PSA_ERROR_INVALID_ARGUMENT );
1725 }
1726}
1727
1728psa_status_t psa_driver_wrapper_mac_sign_finish(
1729 psa_mac_operation_t *operation,
1730 uint8_t *mac,
1731 size_t mac_size,
1732 size_t *mac_length )
1733{
1734 switch( operation->id )
1735 {
1736#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1737 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001738 return( mbedtls_psa_mac_sign_finish( &operation->ctx.mbedtls_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001739 mac, mac_size, mac_length ) );
1740#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1741
1742#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1743#if defined(PSA_CRYPTO_DRIVER_TEST)
1744 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001745 return( mbedtls_test_transparent_mac_sign_finish(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001746 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001747 mac, mac_size, mac_length ) );
1748
1749 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001750 return( mbedtls_test_opaque_mac_sign_finish(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001751 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001752 mac, mac_size, mac_length ) );
1753#endif /* PSA_CRYPTO_DRIVER_TEST */
1754#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1755 default:
1756 (void) mac;
1757 (void) mac_size;
1758 (void) mac_length;
1759 return( PSA_ERROR_INVALID_ARGUMENT );
1760 }
1761}
1762
1763psa_status_t psa_driver_wrapper_mac_verify_finish(
1764 psa_mac_operation_t *operation,
1765 const uint8_t *mac,
1766 size_t mac_length )
1767{
1768 switch( operation->id )
1769 {
1770#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1771 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001772 return( mbedtls_psa_mac_verify_finish( &operation->ctx.mbedtls_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001773 mac, mac_length ) );
1774#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1775
1776#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1777#if defined(PSA_CRYPTO_DRIVER_TEST)
1778 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001779 return( mbedtls_test_transparent_mac_verify_finish(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001780 &operation->ctx.transparent_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001781 mac, mac_length ) );
1782
1783 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001784 return( mbedtls_test_opaque_mac_verify_finish(
Steven Cooreman6e3c2cb2021-03-19 17:05:52 +01001785 &operation->ctx.opaque_test_driver_ctx,
Steven Cooreman896d51e2021-03-19 15:24:23 +01001786 mac, mac_length ) );
1787#endif /* PSA_CRYPTO_DRIVER_TEST */
1788#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1789 default:
1790 (void) mac;
1791 (void) mac_length;
1792 return( PSA_ERROR_INVALID_ARGUMENT );
1793 }
1794}
1795
1796psa_status_t psa_driver_wrapper_mac_abort(
1797 psa_mac_operation_t *operation )
1798{
Steven Cooreman896d51e2021-03-19 15:24:23 +01001799 switch( operation->id )
1800 {
1801#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1802 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman07897832021-03-19 18:28:56 +01001803 return( mbedtls_psa_mac_abort( &operation->ctx.mbedtls_ctx ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +01001804#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1805
1806#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1807#if defined(PSA_CRYPTO_DRIVER_TEST)
1808 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001809 return( mbedtls_test_transparent_mac_abort(
Steven Cooreman07897832021-03-19 18:28:56 +01001810 &operation->ctx.transparent_test_driver_ctx ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +01001811 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooreman2d9a3f92021-04-29 21:10:11 +02001812 return( mbedtls_test_opaque_mac_abort(
Steven Cooreman07897832021-03-19 18:28:56 +01001813 &operation->ctx.opaque_test_driver_ctx ) );
Steven Cooreman896d51e2021-03-19 15:24:23 +01001814#endif /* PSA_CRYPTO_DRIVER_TEST */
1815#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1816 default:
Steven Cooreman07897832021-03-19 18:28:56 +01001817 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman896d51e2021-03-19 15:24:23 +01001818 }
Steven Cooreman896d51e2021-03-19 15:24:23 +01001819}
Gilles Peskinead0e0122021-04-24 13:19:45 +02001820
1821#endif /* MBEDTLS_PSA_CRYPTO_C */