blob: a506e2ae8949f75ff120ed13c8455e5b10c3f7e2 [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
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +020022#include "common.h"
Ronald Cronde822812021-03-17 16:08:20 +010023#include "psa_crypto_aead.h"
Ronald Cron0b805592020-12-14 18:08:20 +010024#include "psa_crypto_cipher.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020025#include "psa_crypto_core.h"
26#include "psa_crypto_driver_wrappers.h"
Steven Cooreman830aff22021-03-09 09:50:44 +010027#include "psa_crypto_hash.h"
Steven Cooremand13a70f2021-03-19 15:24:23 +010028#include "psa_crypto_mac.h"
Steven Cooreman830aff22021-03-09 09:50:44 +010029
Steven Cooreman2a1664c2020-07-20 15:33:08 +020030#include "mbedtls/platform.h"
31
Gilles Peskine1905a242021-04-24 13:19:45 +020032#if defined(MBEDTLS_PSA_CRYPTO_C)
33
Steven Cooreman2a1664c2020-07-20 15:33:08 +020034#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS)
Steven Cooremancd84cb42020-07-16 20:28:36 +020035
36/* Include test driver definition when running tests */
Steven Cooremanf1720ea2020-07-24 18:41:58 +020037#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman56250fd2020-09-04 13:07:15 +020038#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020039#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020040#endif
41#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020042#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020043#endif
Ronald Cronce1d8d22021-04-30 17:00:34 +020044#include "test/drivers/test_driver.h"
Steven Cooremanf1720ea2020-07-24 18:41:58 +020045#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooremancd84cb42020-07-16 20:28:36 +020046
Steven Cooreman56250fd2020-09-04 13:07:15 +020047/* Repeat above block for each JSON-declared driver during autogeneration */
Steven Cooremanaa87fd02021-03-15 18:54:03 +010048#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */
Steven Cooreman56250fd2020-09-04 13:07:15 +020049
Steven Cooreman1e582352021-02-18 17:24:37 +010050/* Auto-generated values depending on which drivers are registered.
51 * ID 0 is reserved for unallocated operations.
52 * ID 1 is reserved for the Mbed TLS software driver. */
Steven Cooreman830aff22021-03-09 09:50:44 +010053#define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
54
Steven Cooreman37941cb2020-07-28 18:49:51 +020055#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooreman1e582352021-02-18 17:24:37 +010056#define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2)
57#define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3)
Steven Cooreman37941cb2020-07-28 18:49:51 +020058#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman2a1664c2020-07-20 15:33:08 +020059
60/* Support the 'old' SE interface when asked to */
Steven Cooreman7a250572020-07-17 16:43:05 +020061#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Steven Cooreman56250fd2020-09-04 13:07:15 +020062/* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
63 * SE driver is present, to avoid unused argument errors at compile time. */
64#ifndef PSA_CRYPTO_DRIVER_PRESENT
Steven Cooremanf1720ea2020-07-24 18:41:58 +020065#define PSA_CRYPTO_DRIVER_PRESENT
Steven Cooreman56250fd2020-09-04 13:07:15 +020066#endif
Steven Cooreman7a250572020-07-17 16:43:05 +020067#include "psa_crypto_se.h"
68#endif
69
Steven Cooremancd84cb42020-07-16 20:28:36 +020070/* Start delegation functions */
gabor-mezei-arm50eac352021-04-22 11:32:19 +020071psa_status_t psa_driver_wrapper_sign_message(
72 const psa_key_attributes_t *attributes,
73 const uint8_t *key_buffer,
74 size_t key_buffer_size,
75 psa_algorithm_t alg,
76 const uint8_t *input,
77 size_t input_length,
78 uint8_t *signature,
79 size_t signature_size,
80 size_t *signature_length )
81{
82 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +020083 psa_key_location_t location =
84 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
85
86 switch( location )
87 {
88 case PSA_KEY_LOCATION_LOCAL_STORAGE:
89 /* Key is stored in the slot in export representation, so
90 * cycle through all known transparent accelerators */
91#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
92#if defined(PSA_CRYPTO_DRIVER_TEST)
93 status = mbedtls_test_transparent_signature_sign_message(
94 attributes,
95 key_buffer,
96 key_buffer_size,
97 alg,
98 input,
99 input_length,
100 signature,
101 signature_size,
102 signature_length );
103 /* Declared with fallback == true */
104 if( status != PSA_ERROR_NOT_SUPPORTED )
105 return( status );
106#endif /* PSA_CRYPTO_DRIVER_TEST */
107#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200108 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200109
110 /* Add cases for opaque driver here */
111#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
112#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arm2522c0b2021-04-27 18:52:51 +0200113 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200114 status = mbedtls_test_opaque_signature_sign_message(
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200115 attributes,
116 key_buffer,
117 key_buffer_size,
118 alg,
119 input,
120 input_length,
121 signature,
122 signature_size,
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200123 signature_length );
124 if( status != PSA_ERROR_NOT_SUPPORTED )
125 return( status );
126 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200127#endif /* PSA_CRYPTO_DRIVER_TEST */
128#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
129 default:
130 /* Key is declared with a lifetime not known to us */
131 (void)status;
gabor-mezei-armd5218df2021-05-12 11:12:25 +0200132 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200133 }
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200134
135 return( psa_sign_message_builtin( attributes,
136 key_buffer,
137 key_buffer_size,
138 alg,
139 input,
140 input_length,
141 signature,
142 signature_size,
143 signature_length ) );
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200144}
145
146psa_status_t psa_driver_wrapper_verify_message(
147 const psa_key_attributes_t *attributes,
148 const uint8_t *key_buffer,
149 size_t key_buffer_size,
150 psa_algorithm_t alg,
151 const uint8_t *input,
152 size_t input_length,
153 const uint8_t *signature,
154 size_t signature_length )
155{
156 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200157 psa_key_location_t location =
158 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
159
160 switch( location )
161 {
162 case PSA_KEY_LOCATION_LOCAL_STORAGE:
163 /* Key is stored in the slot in export representation, so
164 * cycle through all known transparent accelerators */
165#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
166#if defined(PSA_CRYPTO_DRIVER_TEST)
167 status = mbedtls_test_transparent_signature_verify_message(
168 attributes,
169 key_buffer,
170 key_buffer_size,
171 alg,
172 input,
173 input_length,
174 signature,
175 signature_length );
176 /* Declared with fallback == true */
177 if( status != PSA_ERROR_NOT_SUPPORTED )
178 return( status );
179#endif /* PSA_CRYPTO_DRIVER_TEST */
180#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200181 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200182
183 /* Add cases for opaque driver here */
184#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
185#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arm2522c0b2021-04-27 18:52:51 +0200186 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200187 return( mbedtls_test_opaque_signature_verify_message(
188 attributes,
189 key_buffer,
190 key_buffer_size,
191 alg,
192 input,
193 input_length,
194 signature,
195 signature_length ) );
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200196 if( status != PSA_ERROR_NOT_SUPPORTED )
197 return( status );
198 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200199#endif /* PSA_CRYPTO_DRIVER_TEST */
200#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
201 default:
202 /* Key is declared with a lifetime not known to us */
203 (void)status;
gabor-mezei-armd5218df2021-05-12 11:12:25 +0200204 break;
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200205 }
gabor-mezei-arm256443e2021-05-07 15:16:34 +0200206
207 return( psa_verify_message_builtin( attributes,
208 key_buffer,
209 key_buffer_size,
210 alg,
211 input,
212 input_length,
213 signature,
214 signature_length ) );
gabor-mezei-arm50eac352021-04-22 11:32:19 +0200215}
216
Ronald Cron9f17aa42020-12-08 17:07:25 +0100217psa_status_t psa_driver_wrapper_sign_hash(
218 const psa_key_attributes_t *attributes,
219 const uint8_t *key_buffer, size_t key_buffer_size,
220 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
221 uint8_t *signature, size_t signature_size, size_t *signature_length )
Steven Cooremancd84cb42020-07-16 20:28:36 +0200222{
Steven Cooreman7a250572020-07-17 16:43:05 +0200223 /* Try dynamically-registered SE interface first */
224#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
225 const psa_drv_se_t *drv;
226 psa_drv_se_context_t *drv_context;
227
Ronald Cron9f17aa42020-12-08 17:07:25 +0100228 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman7a250572020-07-17 16:43:05 +0200229 {
230 if( drv->asymmetric == NULL ||
231 drv->asymmetric->p_sign == NULL )
232 {
233 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200234 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman7a250572020-07-17 16:43:05 +0200235 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100236 return( drv->asymmetric->p_sign(
237 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
238 alg, hash, hash_length,
239 signature, signature_size, signature_length ) );
Steven Cooreman7a250572020-07-17 16:43:05 +0200240 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200241#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman7a250572020-07-17 16:43:05 +0200242
Ronald Cronfce9df22020-12-08 18:06:03 +0100243 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100244 psa_key_location_t location =
245 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200246
247 switch( location )
248 {
249 case PSA_KEY_LOCATION_LOCAL_STORAGE:
250 /* Key is stored in the slot in export representation, so
251 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100252#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200253#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200254 status = mbedtls_test_transparent_signature_sign_hash( attributes,
Ronald Cron9f17aa42020-12-08 17:07:25 +0100255 key_buffer,
256 key_buffer_size,
Steven Cooremancd84cb42020-07-16 20:28:36 +0200257 alg,
258 hash,
259 hash_length,
260 signature,
261 signature_size,
262 signature_length );
263 /* Declared with fallback == true */
264 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200265 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200266#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100267#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200268 /* Fell through, meaning no accelerator supports this operation */
gabor-mezei-arm6883fd22021-05-05 14:18:36 +0200269 return( psa_sign_hash_builtin( attributes,
270 key_buffer,
271 key_buffer_size,
272 alg,
273 hash,
274 hash_length,
275 signature,
276 signature_size,
277 signature_length ) );
Ronald Cronfce9df22020-12-08 18:06:03 +0100278
Steven Cooremancd84cb42020-07-16 20:28:36 +0200279 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100280#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200281#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100282 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200283 return( mbedtls_test_opaque_signature_sign_hash( attributes,
284 key_buffer,
285 key_buffer_size,
286 alg,
287 hash,
288 hash_length,
289 signature,
290 signature_size,
291 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200292#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100293#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremancd84cb42020-07-16 20:28:36 +0200294 default:
295 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100296 (void)status;
297 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremancd84cb42020-07-16 20:28:36 +0200298 }
Steven Cooremancd84cb42020-07-16 20:28:36 +0200299}
300
Ronald Cron9f17aa42020-12-08 17:07:25 +0100301psa_status_t psa_driver_wrapper_verify_hash(
302 const psa_key_attributes_t *attributes,
303 const uint8_t *key_buffer, size_t key_buffer_size,
304 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
305 const uint8_t *signature, size_t signature_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200306{
Steven Cooreman55ae2172020-07-17 19:46:15 +0200307 /* Try dynamically-registered SE interface first */
308#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
309 const psa_drv_se_t *drv;
310 psa_drv_se_context_t *drv_context;
311
Ronald Cron9f17aa42020-12-08 17:07:25 +0100312 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200313 {
314 if( drv->asymmetric == NULL ||
315 drv->asymmetric->p_verify == NULL )
316 {
317 /* Key is defined in SE, but we have no way to exercise it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200318 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200319 }
Ronald Cron9f17aa42020-12-08 17:07:25 +0100320 return( drv->asymmetric->p_verify(
321 drv_context, *( (psa_key_slot_number_t *)key_buffer ),
322 alg, hash, hash_length,
323 signature, signature_length ) );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200324 }
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200325#endif /* PSA_CRYPTO_SE_C */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200326
Ronald Cronfce9df22020-12-08 18:06:03 +0100327 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron9f17aa42020-12-08 17:07:25 +0100328 psa_key_location_t location =
329 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200330
331 switch( location )
332 {
333 case PSA_KEY_LOCATION_LOCAL_STORAGE:
334 /* Key is stored in the slot in export representation, so
335 * cycle through all known transparent accelerators */
Ronald Cronfce9df22020-12-08 18:06:03 +0100336#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200337#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200338 status = mbedtls_test_transparent_signature_verify_hash(
339 attributes,
340 key_buffer,
341 key_buffer_size,
342 alg,
343 hash,
344 hash_length,
345 signature,
346 signature_length );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200347 /* Declared with fallback == true */
348 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman56250fd2020-09-04 13:07:15 +0200349 return( status );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200350#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100351#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
352
gabor-mezei-arm6883fd22021-05-05 14:18:36 +0200353 return( psa_verify_hash_builtin( attributes,
354 key_buffer,
355 key_buffer_size,
356 alg,
357 hash,
358 hash_length,
359 signature,
360 signature_length ) );
Ronald Cronfce9df22020-12-08 18:06:03 +0100361
Steven Cooreman55ae2172020-07-17 19:46:15 +0200362 /* Add cases for opaque driver here */
Ronald Cronfce9df22020-12-08 18:06:03 +0100363#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200364#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100365 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200366 return( mbedtls_test_opaque_signature_verify_hash( attributes,
367 key_buffer,
368 key_buffer_size,
369 alg,
370 hash,
371 hash_length,
372 signature,
373 signature_length ) );
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200374#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cronfce9df22020-12-08 18:06:03 +0100375#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman55ae2172020-07-17 19:46:15 +0200376 default:
377 /* Key is declared with a lifetime not known to us */
Ronald Cronfce9df22020-12-08 18:06:03 +0100378 (void)status;
379 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200380 }
Steven Cooreman55ae2172020-07-17 19:46:15 +0200381}
382
Archana449608b2021-09-08 15:36:05 +0530383/** Calculate the key buffer size required to store the key material of a key
Archanad8a83dc2021-06-14 10:04:16 +0530384 * associated with an opaque driver from input key data.
385 *
Archanad8a83dc2021-06-14 10:04:16 +0530386 * \param[in] attributes The key attributes
387 * \param[in] data The input key data.
388 * \param[in] data_length The input data length.
389 * \param[out] key_buffer_size Minimum buffer size to contain the key material.
390 *
391 * \retval #PSA_SUCCESS
392 * \retval #PSA_ERROR_INVALID_ARGUMENT
393 * \retval #PSA_ERROR_NOT_SUPPORTED
394 */
395psa_status_t psa_driver_wrapper_get_key_buffer_size_from_key_data(
396 const psa_key_attributes_t *attributes,
397 const uint8_t *data,
398 size_t data_length,
399 size_t *key_buffer_size )
400{
Archana449608b2021-09-08 15:36:05 +0530401 psa_key_location_t location =
402 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Archanad8a83dc2021-06-14 10:04:16 +0530403 psa_key_type_t key_type = attributes->core.type;
404
405 *key_buffer_size = 0;
406 switch( location )
407 {
408#if defined(PSA_CRYPTO_DRIVER_TEST)
409 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
410 *key_buffer_size = mbedtls_test_opaque_size_function( key_type,
411 PSA_BYTES_TO_BITS( data_length ) );
412 return( ( *key_buffer_size != 0 ) ?
413 PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
414#endif /* PSA_CRYPTO_DRIVER_TEST */
415
416 default:
417 (void)key_type;
418 (void)data;
419 (void)data_length;
420 return( PSA_ERROR_INVALID_ARGUMENT );
421 }
422}
423
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200424/** Get the key buffer size required to store the key material of a key
Archanad8a83dc2021-06-14 10:04:16 +0530425 * associated with an opaque driver.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200426 *
Ronald Cron31216282020-12-05 18:47:56 +0100427 * \param[in] attributes The key attributes.
428 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200429 *
430 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100431 * The minimum size for a buffer to contain the key material has been
432 * returned successfully.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200433 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100434 * The type and/or the size in bits of the key or the combination of
435 * the two is not supported.
Archanad8a83dc2021-06-14 10:04:16 +0530436 * \retval #PSA_ERROR_INVALID_ARGUMENT
437 * The key is declared with a lifetime not known to us.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200438 */
Ronald Cron9df74be2020-12-05 19:15:23 +0100439psa_status_t psa_driver_wrapper_get_key_buffer_size(
Ronald Cron31216282020-12-05 18:47:56 +0100440 const psa_key_attributes_t *attributes,
441 size_t *key_buffer_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200442{
John Durkop2c618352020-09-22 06:54:01 -0700443 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
444 psa_key_type_t key_type = attributes->core.type;
445 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200446
Ronald Cron31216282020-12-05 18:47:56 +0100447 *key_buffer_size = 0;
John Durkop2c618352020-09-22 06:54:01 -0700448 switch( location )
449 {
John Durkop2c618352020-09-22 06:54:01 -0700450#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100451 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100452#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
453 /* Emulate property 'builtin_key_size' */
454 if( psa_key_id_is_builtin(
455 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(
456 psa_get_key_id( attributes ) ) ) )
457 {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100458 *key_buffer_size = sizeof( psa_drv_slot_number_t );
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100459 return( PSA_SUCCESS );
460 }
461#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Archana449608b2021-09-08 15:36:05 +0530462 *key_buffer_size = mbedtls_test_opaque_size_function( key_type,
463 key_bits );
Ronald Cron4607c822021-04-14 10:55:34 +0200464 return( ( *key_buffer_size != 0 ) ?
465 PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
John Durkop2c618352020-09-22 06:54:01 -0700466#endif /* PSA_CRYPTO_DRIVER_TEST */
467
468 default:
Ronald Cron9df74be2020-12-05 19:15:23 +0100469 (void)key_type;
470 (void)key_bits;
Archanad8a83dc2021-06-14 10:04:16 +0530471 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200472 }
473}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200474
Ronald Cron977c2472020-10-13 08:32:21 +0200475psa_status_t psa_driver_wrapper_generate_key(
476 const psa_key_attributes_t *attributes,
477 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200478{
Ronald Cron977c2472020-10-13 08:32:21 +0200479 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
480 psa_key_location_t location =
481 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
482
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200483 /* Try dynamically-registered SE interface first */
484#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
485 const psa_drv_se_t *drv;
486 psa_drv_se_context_t *drv_context;
487
Ronald Cron977c2472020-10-13 08:32:21 +0200488 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200489 {
490 size_t pubkey_length = 0; /* We don't support this feature yet */
491 if( drv->key_management == NULL ||
492 drv->key_management->p_generate == NULL )
493 {
494 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200495 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200496 }
497 return( drv->key_management->p_generate(
Ronald Cron977c2472020-10-13 08:32:21 +0200498 drv_context,
499 *( (psa_key_slot_number_t *)key_buffer ),
Ronald Cronea0f8a62020-11-25 17:52:23 +0100500 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200501 }
502#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
503
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200504 switch( location )
505 {
506 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Ronald Cron977c2472020-10-13 08:32:21 +0200507#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200508 /* Transparent drivers are limited to generating asymmetric keys */
Ronald Cron977c2472020-10-13 08:32:21 +0200509 if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200510 {
Ronald Cron977c2472020-10-13 08:32:21 +0200511 /* Cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200512#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200513 status = mbedtls_test_transparent_generate_key(
Ronald Cron977c2472020-10-13 08:32:21 +0200514 attributes, key_buffer, key_buffer_size,
515 key_buffer_length );
516 /* Declared with fallback == true */
517 if( status != PSA_ERROR_NOT_SUPPORTED )
518 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200519#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200520 }
521#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
522
523 /* Software fallback */
524 status = psa_generate_key_internal(
525 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200526 break;
Ronald Cron977c2472020-10-13 08:32:21 +0200527
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200528 /* Add cases for opaque driver here */
Ronald Cron977c2472020-10-13 08:32:21 +0200529#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200530#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100531 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200532 status = mbedtls_test_opaque_generate_key(
Ronald Cron977c2472020-10-13 08:32:21 +0200533 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200534 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200535#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200536#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
537
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200538 default:
539 /* Key is declared with a lifetime not known to us */
540 status = PSA_ERROR_INVALID_ARGUMENT;
541 break;
542 }
543
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200544 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200545}
546
Ronald Cron83282872020-11-22 14:02:39 +0100547psa_status_t psa_driver_wrapper_import_key(
548 const psa_key_attributes_t *attributes,
549 const uint8_t *data,
550 size_t data_length,
551 uint8_t *key_buffer,
552 size_t key_buffer_size,
553 size_t *key_buffer_length,
554 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200555{
Steven Cooreman04524762020-10-13 17:43:44 +0200556 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100557 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
558 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200559
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100560 /* Try dynamically-registered SE interface first */
561#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
562 const psa_drv_se_t *drv;
563 psa_drv_se_context_t *drv_context;
564
565 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
566 {
567 if( drv->key_management == NULL ||
568 drv->key_management->p_import == NULL )
569 return( PSA_ERROR_NOT_SUPPORTED );
570
571 /* The driver should set the number of key bits, however in
572 * case it doesn't, we initialize bits to an invalid value. */
573 *bits = PSA_MAX_KEY_BITS + 1;
574 status = drv->key_management->p_import(
575 drv_context,
576 *( (psa_key_slot_number_t *)key_buffer ),
577 attributes, data, data_length, bits );
578
579 if( status != PSA_SUCCESS )
580 return( status );
581
582 if( (*bits) > PSA_MAX_KEY_BITS )
583 return( PSA_ERROR_NOT_SUPPORTED );
584
585 return( PSA_SUCCESS );
586 }
587#endif /* PSA_CRYPTO_SE_C */
588
Ronald Cronbf33c932020-11-28 18:06:53 +0100589 switch( location )
590 {
591 case PSA_KEY_LOCATION_LOCAL_STORAGE:
592 /* Key is stored in the slot in export representation, so
593 * cycle through all known transparent accelerators */
594#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
595#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200596 status = mbedtls_test_transparent_import_key(
597 attributes,
598 data, data_length,
599 key_buffer, key_buffer_size,
600 key_buffer_length, bits );
Ronald Cronbf33c932020-11-28 18:06:53 +0100601 /* Declared with fallback == true */
602 if( status != PSA_ERROR_NOT_SUPPORTED )
603 return( status );
604#endif /* PSA_CRYPTO_DRIVER_TEST */
605#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
606 /* Fell through, meaning no accelerator supports this operation */
607 return( psa_import_key_into_slot( attributes,
608 data, data_length,
609 key_buffer, key_buffer_size,
610 key_buffer_length, bits ) );
Archana4d7ae1d2021-07-07 02:50:22 +0530611 /* Add cases for opaque driver here */
612#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
613#if defined(PSA_CRYPTO_DRIVER_TEST)
614 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
615 return( mbedtls_test_opaque_import_key(
616 attributes,
617 data, data_length,
618 key_buffer, key_buffer_size,
619 key_buffer_length, bits ) );
620#endif /* PSA_CRYPTO_DRIVER_TEST */
621#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cronbf33c932020-11-28 18:06:53 +0100622 default:
Ronald Cronbf33c932020-11-28 18:06:53 +0100623 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100624 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100625 }
626
Steven Cooreman04524762020-10-13 17:43:44 +0200627}
628
Ronald Cron67227982020-11-26 15:16:05 +0100629psa_status_t psa_driver_wrapper_export_key(
630 const psa_key_attributes_t *attributes,
631 const uint8_t *key_buffer, size_t key_buffer_size,
632 uint8_t *data, size_t data_size, size_t *data_length )
633
634{
635 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
636 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
637 psa_get_key_lifetime( attributes ) );
638
Ronald Cron152e3f82020-11-26 16:06:41 +0100639 /* Try dynamically-registered SE interface first */
640#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
641 const psa_drv_se_t *drv;
642 psa_drv_se_context_t *drv_context;
643
644 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
645 {
646 if( ( drv->key_management == NULL ) ||
647 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100648 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100649 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100650 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100651
652 return( drv->key_management->p_export(
653 drv_context,
654 *( (psa_key_slot_number_t *)key_buffer ),
655 data, data_size, data_length ) );
656 }
657#endif /* PSA_CRYPTO_SE_C */
658
Ronald Cron67227982020-11-26 15:16:05 +0100659 switch( location )
660 {
661 case PSA_KEY_LOCATION_LOCAL_STORAGE:
662 return( psa_export_key_internal( attributes,
663 key_buffer,
664 key_buffer_size,
665 data,
666 data_size,
667 data_length ) );
668
669 /* Add cases for opaque driver here */
670#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
671#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100672 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200673 return( mbedtls_test_opaque_export_key( attributes,
674 key_buffer,
675 key_buffer_size,
676 data,
677 data_size,
678 data_length ) );
Ronald Cron67227982020-11-26 15:16:05 +0100679#endif /* PSA_CRYPTO_DRIVER_TEST */
680#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
681 default:
682 /* Key is declared with a lifetime not known to us */
683 return( status );
684 }
685}
686
Ronald Cron84cc9942020-11-25 14:30:05 +0100687psa_status_t psa_driver_wrapper_export_public_key(
688 const psa_key_attributes_t *attributes,
689 const uint8_t *key_buffer, size_t key_buffer_size,
690 uint8_t *data, size_t data_size, size_t *data_length )
691
Steven Cooremanb9b84422020-10-14 14:39:20 +0200692{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200693 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100694 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
695 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200696
Ronald Cron152e3f82020-11-26 16:06:41 +0100697 /* Try dynamically-registered SE interface first */
698#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
699 const psa_drv_se_t *drv;
700 psa_drv_se_context_t *drv_context;
701
702 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
703 {
704 if( ( drv->key_management == NULL ) ||
705 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100706 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100707 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100708 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100709
710 return( drv->key_management->p_export_public(
711 drv_context,
712 *( (psa_key_slot_number_t *)key_buffer ),
713 data, data_size, data_length ) );
714 }
715#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
716
Steven Cooremanb9b84422020-10-14 14:39:20 +0200717 switch( location )
718 {
719 case PSA_KEY_LOCATION_LOCAL_STORAGE:
720 /* Key is stored in the slot in export representation, so
721 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100722#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200723#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200724 status = mbedtls_test_transparent_export_public_key(
725 attributes,
726 key_buffer,
727 key_buffer_size,
728 data,
729 data_size,
730 data_length );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200731 /* Declared with fallback == true */
732 if( status != PSA_ERROR_NOT_SUPPORTED )
733 return( status );
734#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100735#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200736 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100737 return( psa_export_public_key_internal( attributes,
738 key_buffer,
739 key_buffer_size,
740 data,
741 data_size,
742 data_length ) );
743
Steven Cooremanb9b84422020-10-14 14:39:20 +0200744 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100745#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200746#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100747 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200748 return( mbedtls_test_opaque_export_public_key( attributes,
749 key_buffer,
750 key_buffer_size,
751 data,
752 data_size,
753 data_length ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200754#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100755#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200756 default:
757 /* Key is declared with a lifetime not known to us */
758 return( status );
759 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200760}
761
Steven Cooreman6801f082021-02-19 17:21:22 +0100762psa_status_t psa_driver_wrapper_get_builtin_key(
763 psa_drv_slot_number_t slot_number,
764 psa_key_attributes_t *attributes,
765 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
766{
767 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
768 switch( location )
769 {
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100770#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100771 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200772 return( mbedtls_test_opaque_get_builtin_key(
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100773 slot_number,
774 attributes,
775 key_buffer, key_buffer_size, key_buffer_length ) );
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100776#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman6801f082021-02-19 17:21:22 +0100777 default:
778 (void) slot_number;
779 (void) key_buffer;
780 (void) key_buffer_size;
781 (void) key_buffer_length;
782 return( PSA_ERROR_DOES_NOT_EXIST );
783 }
784}
785
Archana8a180362021-07-05 02:18:48 +0530786psa_status_t psa_driver_wrapper_copy_key(
787 psa_key_attributes_t *attributes,
Archana9d17bf42021-09-10 06:22:44 +0530788 const uint8_t *source_key, size_t source_key_length,
Archana449608b2021-09-08 15:36:05 +0530789 uint8_t *target_key_buffer, size_t target_key_buffer_size,
790 size_t *target_key_buffer_length )
Archana8a180362021-07-05 02:18:48 +0530791{
Archana449608b2021-09-08 15:36:05 +0530792 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
793 psa_key_location_t location =
794 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Archana8a180362021-07-05 02:18:48 +0530795
Archana9d17bf42021-09-10 06:22:44 +0530796#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
797 const psa_drv_se_t *drv;
798 psa_drv_se_context_t *drv_context;
799
800 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
801 {
802 /* Copying to a secure element is not implemented yet. */
803 return( PSA_ERROR_NOT_SUPPORTED );
804 }
805#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
806
Archana8a180362021-07-05 02:18:48 +0530807 switch( location )
808 {
809#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
810#if defined(PSA_CRYPTO_DRIVER_TEST)
811 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
812 return( mbedtls_test_opaque_copy_key( attributes, source_key,
Archana9d17bf42021-09-10 06:22:44 +0530813 source_key_length,
Archana8a180362021-07-05 02:18:48 +0530814 target_key_buffer,
Archana449608b2021-09-08 15:36:05 +0530815 target_key_buffer_size,
816 target_key_buffer_length) );
Archana8a180362021-07-05 02:18:48 +0530817#endif /* PSA_CRYPTO_DRIVER_TEST */
818#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
819 default:
820 (void)source_key;
Archana9d17bf42021-09-10 06:22:44 +0530821 (void)source_key_length;
Archana8a180362021-07-05 02:18:48 +0530822 (void)target_key_buffer;
Archana449608b2021-09-08 15:36:05 +0530823 (void)target_key_buffer_size;
824 (void)target_key_buffer_length;
Archana8a180362021-07-05 02:18:48 +0530825 status = PSA_ERROR_INVALID_ARGUMENT;
826 }
827 return( status );
828}
829
Steven Cooreman37941cb2020-07-28 18:49:51 +0200830/*
831 * Cipher functions
832 */
833psa_status_t psa_driver_wrapper_cipher_encrypt(
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100834 const psa_key_attributes_t *attributes,
835 const uint8_t *key_buffer,
836 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200837 psa_algorithm_t alg,
Ronald Cron9b674282021-07-09 09:19:35 +0200838 const uint8_t *iv,
839 size_t iv_length,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200840 const uint8_t *input,
841 size_t input_length,
842 uint8_t *output,
843 size_t output_size,
844 size_t *output_length )
845{
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100846 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
847 psa_key_location_t location =
848 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200849
850 switch( location )
851 {
852 case PSA_KEY_LOCATION_LOCAL_STORAGE:
853 /* Key is stored in the slot in export representation, so
854 * cycle through all known transparent accelerators */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100855#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200856#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100857 status = mbedtls_test_transparent_cipher_encrypt( attributes,
858 key_buffer,
859 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200860 alg,
Ronald Cron9b674282021-07-09 09:19:35 +0200861 iv,
862 iv_length,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200863 input,
864 input_length,
865 output,
866 output_size,
867 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200868 /* Declared with fallback == true */
869 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200870 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200871#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100872#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
873
874#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
875 return( mbedtls_psa_cipher_encrypt( attributes,
876 key_buffer,
877 key_buffer_size,
878 alg,
Ronald Cron9b674282021-07-09 09:19:35 +0200879 iv,
880 iv_length,
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100881 input,
882 input_length,
883 output,
884 output_size,
885 output_length ) );
gabor-mezei-arm0a93b662021-06-25 15:43:32 +0200886#else
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200887 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-arm0a93b662021-06-25 15:43:32 +0200888#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100889
Steven Cooreman37941cb2020-07-28 18:49:51 +0200890 /* Add cases for opaque driver here */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100891#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200892#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100893 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100894 return( mbedtls_test_opaque_cipher_encrypt( attributes,
895 key_buffer,
896 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200897 alg,
Ronald Cron9b674282021-07-09 09:19:35 +0200898 iv,
899 iv_length,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200900 input,
901 input_length,
902 output,
903 output_size,
904 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200905#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100906#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
907
Steven Cooreman37941cb2020-07-28 18:49:51 +0200908 default:
909 /* Key is declared with a lifetime not known to us */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100910 (void)status;
911 (void)key_buffer;
912 (void)key_buffer_size;
913 (void)alg;
914 (void)input;
915 (void)input_length;
916 (void)output;
917 (void)output_size;
918 (void)output_length;
919 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200920 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200921}
922
923psa_status_t psa_driver_wrapper_cipher_decrypt(
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100924 const psa_key_attributes_t *attributes,
925 const uint8_t *key_buffer,
926 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200927 psa_algorithm_t alg,
928 const uint8_t *input,
929 size_t input_length,
930 uint8_t *output,
931 size_t output_size,
932 size_t *output_length )
933{
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100934 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
935 psa_key_location_t location =
936 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200937
938 switch( location )
939 {
940 case PSA_KEY_LOCATION_LOCAL_STORAGE:
941 /* Key is stored in the slot in export representation, so
942 * cycle through all known transparent accelerators */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100943#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200944#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100945 status = mbedtls_test_transparent_cipher_decrypt( attributes,
946 key_buffer,
947 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200948 alg,
949 input,
950 input_length,
951 output,
952 output_size,
953 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200954 /* Declared with fallback == true */
955 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200956 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200957#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100958#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
959
960#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
961 return( mbedtls_psa_cipher_decrypt( attributes,
962 key_buffer,
963 key_buffer_size,
964 alg,
965 input,
966 input_length,
967 output,
968 output_size,
969 output_length ) );
gabor-mezei-arm0a93b662021-06-25 15:43:32 +0200970#else
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200971 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-arm0a93b662021-06-25 15:43:32 +0200972#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100973
Steven Cooreman37941cb2020-07-28 18:49:51 +0200974 /* Add cases for opaque driver here */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100975#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200976#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100977 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100978 return( mbedtls_test_opaque_cipher_decrypt( attributes,
979 key_buffer,
980 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200981 alg,
982 input,
983 input_length,
984 output,
985 output_size,
986 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200987#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100988#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
989
Steven Cooreman37941cb2020-07-28 18:49:51 +0200990 default:
991 /* Key is declared with a lifetime not known to us */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100992 (void)status;
993 (void)key_buffer;
994 (void)key_buffer_size;
995 (void)alg;
996 (void)input;
997 (void)input_length;
998 (void)output;
999 (void)output_size;
1000 (void)output_length;
1001 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001002 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001003}
1004
1005psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +01001006 psa_cipher_operation_t *operation,
1007 const psa_key_attributes_t *attributes,
1008 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001009 psa_algorithm_t alg )
1010{
Ronald Cron0b805592020-12-14 18:08:20 +01001011 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona4af55f2020-12-14 14:36:06 +01001012 psa_key_location_t location =
1013 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001014
Steven Cooreman37941cb2020-07-28 18:49:51 +02001015 switch( location )
1016 {
1017 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1018 /* Key is stored in the slot in export representation, so
1019 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +01001020#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001021#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001022 status = mbedtls_test_transparent_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001023 &operation->ctx.transparent_test_driver_ctx,
1024 attributes,
1025 key_buffer,
1026 key_buffer_size,
1027 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001028 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +02001029 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001030 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +02001031
Ronald Cron0b805592020-12-14 18:08:20 +01001032 if( status != PSA_ERROR_NOT_SUPPORTED )
1033 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001034#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001035#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001036#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001037 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +01001038 status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001039 attributes,
1040 key_buffer,
1041 key_buffer_size,
1042 alg );
1043 if( status == PSA_SUCCESS )
Ronald Cron6e412a72021-03-10 09:58:47 +01001044 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Ronald Cron49fafa92021-03-10 08:34:23 +01001045
Ronald Cron7b4154d2021-03-19 14:49:41 +01001046 if( status != PSA_ERROR_NOT_SUPPORTED )
1047 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001048#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1049 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron0b805592020-12-14 18:08:20 +01001050
Steven Cooreman37941cb2020-07-28 18:49:51 +02001051 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +01001052#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001053#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +01001054 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001055 status = mbedtls_test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001056 &operation->ctx.opaque_test_driver_ctx,
1057 attributes,
1058 key_buffer, key_buffer_size,
1059 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001060
Steven Cooreman37941cb2020-07-28 18:49:51 +02001061 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001062 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001063
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001064 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001065#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001066#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +02001067 default:
1068 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +01001069 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001070 (void)key_buffer;
1071 (void)key_buffer_size;
1072 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +02001073 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001074 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001075}
1076
1077psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +01001078 psa_cipher_operation_t *operation,
1079 const psa_key_attributes_t *attributes,
1080 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001081 psa_algorithm_t alg )
1082{
Steven Cooreman37941cb2020-07-28 18:49:51 +02001083 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crona4af55f2020-12-14 14:36:06 +01001084 psa_key_location_t location =
1085 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001086
Steven Cooreman37941cb2020-07-28 18:49:51 +02001087 switch( location )
1088 {
1089 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1090 /* Key is stored in the slot in export representation, so
1091 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +01001092#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001093#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001094 status = mbedtls_test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001095 &operation->ctx.transparent_test_driver_ctx,
1096 attributes,
1097 key_buffer,
1098 key_buffer_size,
1099 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001100 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +02001101 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001102 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +02001103
Ronald Cron0b805592020-12-14 18:08:20 +01001104 if( status != PSA_ERROR_NOT_SUPPORTED )
1105 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001106#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001107#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001108#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001109 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +01001110 status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001111 attributes,
1112 key_buffer,
1113 key_buffer_size,
1114 alg );
1115 if( status == PSA_SUCCESS )
1116 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1117
1118 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001119#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1120 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron49fafa92021-03-10 08:34:23 +01001121
Steven Cooreman37941cb2020-07-28 18:49:51 +02001122 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +01001123#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001124#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +01001125 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001126 status = mbedtls_test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001127 &operation->ctx.opaque_test_driver_ctx,
1128 attributes,
1129 key_buffer, key_buffer_size,
1130 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001131
Steven Cooreman37941cb2020-07-28 18:49:51 +02001132 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001133 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001134
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001135 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001136#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001137#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +02001138 default:
1139 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +01001140 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001141 (void)key_buffer;
1142 (void)key_buffer_size;
1143 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +02001144 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001145 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001146}
1147
Steven Cooreman37941cb2020-07-28 18:49:51 +02001148psa_status_t psa_driver_wrapper_cipher_set_iv(
Ronald Cron6056fe82020-12-15 13:58:07 +01001149 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001150 const uint8_t *iv,
1151 size_t iv_length )
1152{
Ronald Cron49fafa92021-03-10 08:34:23 +01001153 switch( operation->id )
1154 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001155#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001156 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001157 return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001158 iv,
1159 iv_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001160#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001161
1162#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001163#if defined(PSA_CRYPTO_DRIVER_TEST)
1164 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001165 return( mbedtls_test_transparent_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001166 &operation->ctx.transparent_test_driver_ctx,
1167 iv, iv_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001168
Steven Cooreman37941cb2020-07-28 18:49:51 +02001169 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001170 return( mbedtls_test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001171 &operation->ctx.opaque_test_driver_ctx,
1172 iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001173#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001174#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001175 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001176
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001177 (void)iv;
1178 (void)iv_length;
1179
Ronald Crondd24c9b2020-12-15 14:10:01 +01001180 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001181}
1182
1183psa_status_t psa_driver_wrapper_cipher_update(
Ronald Cron6056fe82020-12-15 13:58:07 +01001184 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001185 const uint8_t *input,
1186 size_t input_length,
1187 uint8_t *output,
1188 size_t output_size,
1189 size_t *output_length )
1190{
Ronald Cron49fafa92021-03-10 08:34:23 +01001191 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001192 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001193#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001194 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001195 return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001196 input,
1197 input_length,
1198 output,
1199 output_size,
1200 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001201#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1202
Ronald Cron49fafa92021-03-10 08:34:23 +01001203#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001204#if defined(PSA_CRYPTO_DRIVER_TEST)
1205 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001206 return( mbedtls_test_transparent_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001207 &operation->ctx.transparent_test_driver_ctx,
1208 input, input_length,
1209 output, output_size, output_length ) );
1210
Steven Cooreman37941cb2020-07-28 18:49:51 +02001211 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001212 return( mbedtls_test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001213 &operation->ctx.opaque_test_driver_ctx,
1214 input, input_length,
1215 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001216#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001217#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001218 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001219
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001220 (void)input;
1221 (void)input_length;
1222 (void)output;
1223 (void)output_size;
1224 (void)output_length;
1225
Ronald Crondd24c9b2020-12-15 14:10:01 +01001226 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001227}
1228
1229psa_status_t psa_driver_wrapper_cipher_finish(
Ronald Cron6056fe82020-12-15 13:58:07 +01001230 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001231 uint8_t *output,
1232 size_t output_size,
1233 size_t *output_length )
1234{
Ronald Cron49fafa92021-03-10 08:34:23 +01001235 switch( operation->id )
1236 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001237#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001238 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001239 return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001240 output,
1241 output_size,
1242 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001243#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001244
1245#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001246#if defined(PSA_CRYPTO_DRIVER_TEST)
1247 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001248 return( mbedtls_test_transparent_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001249 &operation->ctx.transparent_test_driver_ctx,
1250 output, output_size, output_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001251
Steven Cooreman37941cb2020-07-28 18:49:51 +02001252 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001253 return( mbedtls_test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001254 &operation->ctx.opaque_test_driver_ctx,
1255 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001256#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001257#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001258 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001259
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001260 (void)output;
1261 (void)output_size;
1262 (void)output_length;
1263
Ronald Crondd24c9b2020-12-15 14:10:01 +01001264 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001265}
1266
1267psa_status_t psa_driver_wrapper_cipher_abort(
Ronald Cron6056fe82020-12-15 13:58:07 +01001268 psa_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001269{
Ronald Crondd24c9b2020-12-15 14:10:01 +01001270 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001271
Ronald Cron49fafa92021-03-10 08:34:23 +01001272 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001273 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001274#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001275 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001276 return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001277#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +01001278
1279#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001280#if defined(PSA_CRYPTO_DRIVER_TEST)
1281 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001282 status = mbedtls_test_transparent_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001283 &operation->ctx.transparent_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001284 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001285 &operation->ctx.transparent_test_driver_ctx,
1286 sizeof( operation->ctx.transparent_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001287 return( status );
Ronald Cron49fafa92021-03-10 08:34:23 +01001288
Steven Cooreman37941cb2020-07-28 18:49:51 +02001289 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001290 status = mbedtls_test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001291 &operation->ctx.opaque_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001292 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001293 &operation->ctx.opaque_test_driver_ctx,
1294 sizeof( operation->ctx.opaque_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001295 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001296#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001297#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001298 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001299
Ronald Cron49fafa92021-03-10 08:34:23 +01001300 (void)status;
Ronald Crondd24c9b2020-12-15 14:10:01 +01001301 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001302}
1303
Steven Cooreman1e582352021-02-18 17:24:37 +01001304/*
1305 * Hashing functions
1306 */
1307psa_status_t psa_driver_wrapper_hash_compute(
1308 psa_algorithm_t alg,
1309 const uint8_t *input,
1310 size_t input_length,
1311 uint8_t *hash,
1312 size_t hash_size,
1313 size_t *hash_length)
1314{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001315 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001316
1317 /* Try accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001318#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001319 status = mbedtls_test_transparent_hash_compute(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001320 alg, input, input_length, hash, hash_size, hash_length );
Steven Cooremanf7638102021-03-04 15:14:36 +01001321 if( status != PSA_ERROR_NOT_SUPPORTED )
1322 return( status );
1323#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001324
1325 /* If software fallback is compiled in, try fallback */
1326#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1327 status = mbedtls_psa_hash_compute( alg, input, input_length,
1328 hash, hash_size, hash_length );
1329 if( status != PSA_ERROR_NOT_SUPPORTED )
1330 return( status );
1331#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001332 (void) status;
1333 (void) alg;
1334 (void) input;
1335 (void) input_length;
1336 (void) hash;
1337 (void) hash_size;
1338 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001339
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001340 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman1e582352021-02-18 17:24:37 +01001341}
1342
1343psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001344 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001345 psa_algorithm_t alg )
1346{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001347 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001348
Steven Cooreman1e582352021-02-18 17:24:37 +01001349 /* Try setup on accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001350#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001351 status = mbedtls_test_transparent_hash_setup(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001352 &operation->ctx.test_driver_ctx, alg );
Steven Cooremanf7638102021-03-04 15:14:36 +01001353 if( status == PSA_SUCCESS )
1354 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1355
1356 if( status != PSA_ERROR_NOT_SUPPORTED )
1357 return( status );
1358#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001359
1360 /* If software fallback is compiled in, try fallback */
1361#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001362 status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
Steven Cooreman1e582352021-02-18 17:24:37 +01001363 if( status == PSA_SUCCESS )
Steven Cooreman1e582352021-02-18 17:24:37 +01001364 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Steven Cooreman1e582352021-02-18 17:24:37 +01001365
1366 if( status != PSA_ERROR_NOT_SUPPORTED )
1367 return( status );
1368#endif
1369 /* Nothing left to try if we fall through here */
1370 (void) status;
1371 (void) operation;
1372 (void) alg;
1373 return( PSA_ERROR_NOT_SUPPORTED );
1374}
1375
1376psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001377 const psa_hash_operation_t *source_operation,
1378 psa_hash_operation_t *target_operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001379{
Steven Cooreman1e582352021-02-18 17:24:37 +01001380 switch( source_operation->id )
1381 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001382#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1383 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1384 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1385 return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
1386 &target_operation->ctx.mbedtls_ctx ) );
1387#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001388#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001389 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1390 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Ronald Cron7f13fa22021-04-13 12:41:34 +02001391 return( mbedtls_test_transparent_hash_clone(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001392 &source_operation->ctx.test_driver_ctx,
1393 &target_operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001394#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001395 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001396 (void) target_operation;
1397 return( PSA_ERROR_BAD_STATE );
1398 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001399}
1400
1401psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001402 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001403 const uint8_t *input,
1404 size_t input_length )
1405{
Steven Cooreman1e582352021-02-18 17:24:37 +01001406 switch( operation->id )
1407 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001408#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1409 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1410 return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
1411 input, input_length ) );
1412#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001413#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001414 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001415 return( mbedtls_test_transparent_hash_update(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001416 &operation->ctx.test_driver_ctx,
1417 input, input_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001418#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001419 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001420 (void) input;
1421 (void) input_length;
1422 return( PSA_ERROR_BAD_STATE );
1423 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001424}
1425
1426psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001427 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001428 uint8_t *hash,
1429 size_t hash_size,
1430 size_t *hash_length )
1431{
Steven Cooreman1e582352021-02-18 17:24:37 +01001432 switch( operation->id )
1433 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001434#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1435 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1436 return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
1437 hash, hash_size, hash_length ) );
1438#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001439#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001440 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001441 return( mbedtls_test_transparent_hash_finish(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001442 &operation->ctx.test_driver_ctx,
1443 hash, hash_size, hash_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001444#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001445 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001446 (void) hash;
1447 (void) hash_size;
1448 (void) hash_length;
1449 return( PSA_ERROR_BAD_STATE );
1450 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001451}
1452
1453psa_status_t psa_driver_wrapper_hash_abort(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001454 psa_hash_operation_t *operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001455{
Steven Cooreman1e582352021-02-18 17:24:37 +01001456 switch( operation->id )
1457 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001458#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1459 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1460 return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
1461#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001462#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001463 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001464 return( mbedtls_test_transparent_hash_abort(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001465 &operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001466#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001467 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001468 return( PSA_ERROR_BAD_STATE );
1469 }
1470}
1471
Ronald Cronde822812021-03-17 16:08:20 +01001472psa_status_t psa_driver_wrapper_aead_encrypt(
1473 const psa_key_attributes_t *attributes,
1474 const uint8_t *key_buffer, size_t key_buffer_size,
1475 psa_algorithm_t alg,
1476 const uint8_t *nonce, size_t nonce_length,
1477 const uint8_t *additional_data, size_t additional_data_length,
1478 const uint8_t *plaintext, size_t plaintext_length,
1479 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
1480{
1481 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1482 psa_key_location_t location =
1483 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1484
1485 switch( location )
1486 {
1487 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1488 /* Key is stored in the slot in export representation, so
1489 * cycle through all known transparent accelerators */
1490
1491#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1492#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001493 status = mbedtls_test_transparent_aead_encrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001494 attributes, key_buffer, key_buffer_size,
1495 alg,
1496 nonce, nonce_length,
1497 additional_data, additional_data_length,
1498 plaintext, plaintext_length,
1499 ciphertext, ciphertext_size, ciphertext_length );
1500 /* Declared with fallback == true */
1501 if( status != PSA_ERROR_NOT_SUPPORTED )
1502 return( status );
1503#endif /* PSA_CRYPTO_DRIVER_TEST */
1504#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1505
1506 /* Fell through, meaning no accelerator supports this operation */
1507 return( mbedtls_psa_aead_encrypt(
1508 attributes, key_buffer, key_buffer_size,
1509 alg,
1510 nonce, nonce_length,
1511 additional_data, additional_data_length,
1512 plaintext, plaintext_length,
1513 ciphertext, ciphertext_size, ciphertext_length ) );
1514
1515 /* Add cases for opaque driver here */
1516
1517 default:
1518 /* Key is declared with a lifetime not known to us */
1519 (void)status;
1520 return( PSA_ERROR_INVALID_ARGUMENT );
1521 }
1522}
1523
1524psa_status_t psa_driver_wrapper_aead_decrypt(
1525 const psa_key_attributes_t *attributes,
1526 const uint8_t *key_buffer, size_t key_buffer_size,
1527 psa_algorithm_t alg,
1528 const uint8_t *nonce, size_t nonce_length,
1529 const uint8_t *additional_data, size_t additional_data_length,
1530 const uint8_t *ciphertext, size_t ciphertext_length,
1531 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
1532{
1533 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1534 psa_key_location_t location =
1535 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1536
1537 switch( location )
1538 {
1539 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1540 /* Key is stored in the slot in export representation, so
1541 * cycle through all known transparent accelerators */
1542
1543#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1544#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001545 status = mbedtls_test_transparent_aead_decrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001546 attributes, key_buffer, key_buffer_size,
1547 alg,
1548 nonce, nonce_length,
1549 additional_data, additional_data_length,
1550 ciphertext, ciphertext_length,
1551 plaintext, plaintext_size, plaintext_length );
1552 /* Declared with fallback == true */
1553 if( status != PSA_ERROR_NOT_SUPPORTED )
1554 return( status );
1555#endif /* PSA_CRYPTO_DRIVER_TEST */
1556#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1557
1558 /* Fell through, meaning no accelerator supports this operation */
1559 return( mbedtls_psa_aead_decrypt(
1560 attributes, key_buffer, key_buffer_size,
1561 alg,
1562 nonce, nonce_length,
1563 additional_data, additional_data_length,
1564 ciphertext, ciphertext_length,
1565 plaintext, plaintext_size, plaintext_length ) );
1566
1567 /* Add cases for opaque driver here */
1568
1569 default:
1570 /* Key is declared with a lifetime not known to us */
1571 (void)status;
1572 return( PSA_ERROR_INVALID_ARGUMENT );
1573 }
1574}
Steven Cooremand13a70f2021-03-19 15:24:23 +01001575
Paul Elliott6504aa62021-04-20 17:09:36 +01001576psa_status_t psa_driver_wrapper_aead_encrypt_setup(
1577 psa_aead_operation_t *operation,
1578 const psa_key_attributes_t *attributes,
1579 const uint8_t *key_buffer, size_t key_buffer_size,
1580 psa_algorithm_t alg )
1581{
1582 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1583 psa_key_location_t location =
1584 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1585
1586 switch( location )
1587 {
1588 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1589 /* Key is stored in the slot in export representation, so
1590 * cycle through all known transparent accelerators */
1591
1592#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1593#if defined(PSA_CRYPTO_DRIVER_TEST)
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001594 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Paul Elliotta218ceb2021-05-07 15:10:31 +01001595 status = mbedtls_test_transparent_aead_encrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001596 &operation->ctx.transparent_test_driver_ctx,
1597 attributes, key_buffer, key_buffer_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001598 alg );
Paul Elliott6504aa62021-04-20 17:09:36 +01001599
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001600 /* Declared with fallback == true */
Paul Elliott6504aa62021-04-20 17:09:36 +01001601 if( status != PSA_ERROR_NOT_SUPPORTED )
1602 return( status );
1603#endif /* PSA_CRYPTO_DRIVER_TEST */
1604#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1605
1606 /* Fell through, meaning no accelerator supports this operation */
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001607 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Paul Elliott6504aa62021-04-20 17:09:36 +01001608 status = mbedtls_psa_aead_encrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001609 &operation->ctx.mbedtls_ctx, attributes,
Paul Elliott6504aa62021-04-20 17:09:36 +01001610 key_buffer, key_buffer_size,
1611 alg );
1612
Paul Elliott6504aa62021-04-20 17:09:36 +01001613 return( status );
1614
1615 /* Add cases for opaque driver here */
1616
1617 default:
1618 /* Key is declared with a lifetime not known to us */
1619 (void)status;
1620 return( PSA_ERROR_INVALID_ARGUMENT );
1621 }
1622}
1623
1624psa_status_t psa_driver_wrapper_aead_decrypt_setup(
1625 psa_aead_operation_t *operation,
1626 const psa_key_attributes_t *attributes,
1627 const uint8_t *key_buffer, size_t key_buffer_size,
1628 psa_algorithm_t alg )
1629{
1630 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1631 psa_key_location_t location =
1632 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1633
1634 switch( location )
1635 {
1636 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1637 /* Key is stored in the slot in export representation, so
1638 * cycle through all known transparent accelerators */
1639
1640#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1641#if defined(PSA_CRYPTO_DRIVER_TEST)
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001642 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Paul Elliotta218ceb2021-05-07 15:10:31 +01001643 status = mbedtls_test_transparent_aead_decrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001644 &operation->ctx.transparent_test_driver_ctx,
Paul Elliott5d3a3c32021-08-19 18:34:41 +01001645 attributes,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001646 key_buffer, key_buffer_size,
1647 alg );
Paul Elliott6504aa62021-04-20 17:09:36 +01001648
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001649 /* Declared with fallback == true */
Paul Elliott6504aa62021-04-20 17:09:36 +01001650 if( status != PSA_ERROR_NOT_SUPPORTED )
1651 return( status );
1652#endif /* PSA_CRYPTO_DRIVER_TEST */
1653#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1654
1655 /* Fell through, meaning no accelerator supports this operation */
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001656 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Paul Elliott6504aa62021-04-20 17:09:36 +01001657 status = mbedtls_psa_aead_decrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001658 &operation->ctx.mbedtls_ctx,
Paul Elliott5d3a3c32021-08-19 18:34:41 +01001659 attributes,
Paul Elliott6504aa62021-04-20 17:09:36 +01001660 key_buffer, key_buffer_size,
1661 alg );
1662
Paul Elliott6504aa62021-04-20 17:09:36 +01001663 return( status );
1664
1665 /* Add cases for opaque driver here */
1666
1667 default:
1668 /* Key is declared with a lifetime not known to us */
1669 (void)status;
1670 return( PSA_ERROR_INVALID_ARGUMENT );
1671 }
1672}
1673
Paul Elliott6504aa62021-04-20 17:09:36 +01001674psa_status_t psa_driver_wrapper_aead_set_nonce(
1675 psa_aead_operation_t *operation,
1676 const uint8_t *nonce,
1677 size_t nonce_length )
1678{
1679 switch( operation->id )
1680 {
Paul Elliott5c656cb2021-05-19 14:15:01 +01001681#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001682 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001683 return( mbedtls_psa_aead_set_nonce( &operation->ctx.mbedtls_ctx,
1684 nonce,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001685 nonce_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001686
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001687#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001688
1689#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1690#if defined(PSA_CRYPTO_DRIVER_TEST)
1691 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001692 return( mbedtls_test_transparent_aead_set_nonce(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001693 &operation->ctx.transparent_test_driver_ctx,
1694 nonce, nonce_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001695
1696 /* Add cases for opaque driver here */
1697
1698#endif /* PSA_CRYPTO_DRIVER_TEST */
1699#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1700 }
1701
1702 (void)nonce;
1703 (void)nonce_length;
1704
1705 return( PSA_ERROR_INVALID_ARGUMENT );
1706}
1707
1708psa_status_t psa_driver_wrapper_aead_set_lengths(
1709 psa_aead_operation_t *operation,
1710 size_t ad_length,
1711 size_t plaintext_length )
1712{
1713 switch( operation->id )
1714 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001715#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001716 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottdff6c5d2021-09-28 11:00:20 +01001717 return( mbedtls_psa_aead_set_lengths( &operation->ctx.mbedtls_ctx,
1718 ad_length,
1719 plaintext_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001720
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001721#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001722
1723#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1724#if defined(PSA_CRYPTO_DRIVER_TEST)
1725 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001726 return( mbedtls_test_transparent_aead_set_lengths(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001727 &operation->ctx.transparent_test_driver_ctx,
1728 ad_length, plaintext_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001729
1730 /* Add cases for opaque driver here */
1731
1732#endif /* PSA_CRYPTO_DRIVER_TEST */
1733#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1734 }
1735
1736 (void)ad_length;
1737 (void)plaintext_length;
1738
1739 return( PSA_ERROR_INVALID_ARGUMENT );
1740}
1741
1742psa_status_t psa_driver_wrapper_aead_update_ad(
1743 psa_aead_operation_t *operation,
1744 const uint8_t *input,
1745 size_t input_length )
1746{
1747 switch( operation->id )
1748 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001749#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001750 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001751 return( mbedtls_psa_aead_update_ad( &operation->ctx.mbedtls_ctx,
1752 input,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001753 input_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001754
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001755#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001756
1757#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1758#if defined(PSA_CRYPTO_DRIVER_TEST)
1759 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001760 return( mbedtls_test_transparent_aead_update_ad(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001761 &operation->ctx.transparent_test_driver_ctx,
1762 input, input_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001763
1764 /* Add cases for opaque driver here */
1765
1766#endif /* PSA_CRYPTO_DRIVER_TEST */
1767#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1768 }
1769
1770 (void)input;
1771 (void)input_length;
1772
1773 return( PSA_ERROR_INVALID_ARGUMENT );
1774}
1775
1776psa_status_t psa_driver_wrapper_aead_update(
1777 psa_aead_operation_t *operation,
1778 const uint8_t *input,
1779 size_t input_length,
1780 uint8_t *output,
1781 size_t output_size,
1782 size_t *output_length )
1783{
1784 switch( operation->id )
1785 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001786#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001787 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001788 return( mbedtls_psa_aead_update( &operation->ctx.mbedtls_ctx,
1789 input, input_length,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001790 output, output_size,
1791 output_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001792
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001793#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001794
1795#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1796#if defined(PSA_CRYPTO_DRIVER_TEST)
1797 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001798 return( mbedtls_test_transparent_aead_update(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001799 &operation->ctx.transparent_test_driver_ctx,
1800 input, input_length, output, output_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001801 output_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001802
1803 /* Add cases for opaque driver here */
1804
1805#endif /* PSA_CRYPTO_DRIVER_TEST */
1806#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1807 }
1808
1809 (void)input;
1810 (void)input_length;
1811 (void)output;
1812 (void)output_size;
1813 (void)output_length;
1814
1815 return( PSA_ERROR_INVALID_ARGUMENT );
1816}
1817
1818psa_status_t psa_driver_wrapper_aead_finish(
1819 psa_aead_operation_t *operation,
1820 uint8_t *ciphertext,
1821 size_t ciphertext_size,
1822 size_t *ciphertext_length,
1823 uint8_t *tag,
1824 size_t tag_size,
1825 size_t *tag_length )
1826{
1827 switch( operation->id )
1828 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001829#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001830 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001831 return( mbedtls_psa_aead_finish( &operation->ctx.mbedtls_ctx,
1832 ciphertext,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001833 ciphertext_size,
1834 ciphertext_length, tag,
1835 tag_size, tag_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001836
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001837#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001838
1839#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1840#if defined(PSA_CRYPTO_DRIVER_TEST)
1841 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001842 return( mbedtls_test_transparent_aead_finish(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001843 &operation->ctx.transparent_test_driver_ctx,
1844 ciphertext, ciphertext_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001845 ciphertext_length, tag, tag_size, tag_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001846
1847 /* Add cases for opaque driver here */
1848
1849#endif /* PSA_CRYPTO_DRIVER_TEST */
1850#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1851 }
1852
1853 (void)ciphertext;
1854 (void)ciphertext_size;
1855 (void)ciphertext_length;
1856 (void)tag;
1857 (void)tag_size;
1858 (void)tag_length;
1859
1860 return( PSA_ERROR_INVALID_ARGUMENT );
1861}
1862
1863psa_status_t psa_driver_wrapper_aead_verify(
1864 psa_aead_operation_t *operation,
1865 uint8_t *plaintext,
1866 size_t plaintext_size,
1867 size_t *plaintext_length,
1868 const uint8_t *tag,
1869 size_t tag_length )
1870{
1871 switch( operation->id )
1872 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001873#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001874 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliott315628d2021-07-20 18:25:54 +01001875 {
1876 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1877 uint8_t check_tag[PSA_AEAD_TAG_MAX_SIZE];
1878 size_t check_tag_length;
1879
1880 status = mbedtls_psa_aead_finish( &operation->ctx.mbedtls_ctx,
1881 plaintext,
1882 plaintext_size,
1883 plaintext_length,
1884 check_tag,
Paul Elliottb183d562021-09-13 19:02:57 +01001885 sizeof( check_tag ),
Paul Elliott315628d2021-07-20 18:25:54 +01001886 &check_tag_length );
1887
1888 if( status == PSA_SUCCESS )
1889 {
1890 if( tag_length != check_tag_length ||
1891 mbedtls_psa_safer_memcmp( tag, check_tag, tag_length )
1892 != 0 )
1893 status = PSA_ERROR_INVALID_SIGNATURE;
1894 }
1895
Paul Elliott06b6b8c2021-09-13 19:02:04 +01001896 mbedtls_platform_zeroize( check_tag, sizeof( check_tag ) );
1897
Paul Elliott315628d2021-07-20 18:25:54 +01001898 return( status );
1899 }
Paul Elliott6504aa62021-04-20 17:09:36 +01001900
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001901#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001902
1903#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1904#if defined(PSA_CRYPTO_DRIVER_TEST)
1905 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001906 return( mbedtls_test_transparent_aead_verify(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001907 &operation->ctx.transparent_test_driver_ctx,
1908 plaintext, plaintext_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001909 plaintext_length, tag, tag_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001910
1911 /* Add cases for opaque driver here */
1912
1913#endif /* PSA_CRYPTO_DRIVER_TEST */
1914#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1915 }
1916
1917 (void)plaintext;
1918 (void)plaintext_size;
1919 (void)plaintext_length;
1920 (void)tag;
1921 (void)tag_length;
1922
1923 return( PSA_ERROR_INVALID_ARGUMENT );
1924}
1925
1926psa_status_t psa_driver_wrapper_aead_abort(
1927 psa_aead_operation_t *operation )
1928{
1929 switch( operation->id )
1930 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001931#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001932 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001933 return( mbedtls_psa_aead_abort( &operation->ctx.mbedtls_ctx ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001934
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001935#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001936
1937#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1938#if defined(PSA_CRYPTO_DRIVER_TEST)
1939 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001940 return( mbedtls_test_transparent_aead_abort(
1941 &operation->ctx.transparent_test_driver_ctx ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001942
1943 /* Add cases for opaque driver here */
1944
1945#endif /* PSA_CRYPTO_DRIVER_TEST */
1946#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1947 }
1948
1949 return( PSA_ERROR_INVALID_ARGUMENT );
1950}
Steven Cooremand13a70f2021-03-19 15:24:23 +01001951
1952/*
1953 * MAC functions
1954 */
1955psa_status_t psa_driver_wrapper_mac_compute(
1956 const psa_key_attributes_t *attributes,
1957 const uint8_t *key_buffer,
1958 size_t key_buffer_size,
1959 psa_algorithm_t alg,
1960 const uint8_t *input,
1961 size_t input_length,
1962 uint8_t *mac,
1963 size_t mac_size,
1964 size_t *mac_length )
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_compute(
Steven Cooremand13a70f2021-03-19 15:24:23 +01001978 attributes, key_buffer, key_buffer_size, alg,
1979 input, input_length,
1980 mac, mac_size, mac_length );
1981 /* Declared with fallback == true */
1982 if( status != PSA_ERROR_NOT_SUPPORTED )
1983 return( status );
1984#endif /* PSA_CRYPTO_DRIVER_TEST */
1985#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1986#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1987 /* Fell through, meaning no accelerator supports this operation */
1988 status = mbedtls_psa_mac_compute(
1989 attributes, key_buffer, key_buffer_size, alg,
1990 input, input_length,
1991 mac, mac_size, mac_length );
1992 if( status != PSA_ERROR_NOT_SUPPORTED )
1993 return( status );
1994#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1995 return( PSA_ERROR_NOT_SUPPORTED );
1996
1997 /* Add cases for opaque driver here */
1998#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1999#if defined(PSA_CRYPTO_DRIVER_TEST)
2000 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002001 status = mbedtls_test_opaque_mac_compute(
Steven Cooremand13a70f2021-03-19 15:24:23 +01002002 attributes, key_buffer, key_buffer_size, alg,
2003 input, input_length,
2004 mac, mac_size, mac_length );
2005 return( status );
2006#endif /* PSA_CRYPTO_DRIVER_TEST */
2007#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2008 default:
2009 /* Key is declared with a lifetime not known to us */
2010 (void) key_buffer;
2011 (void) key_buffer_size;
2012 (void) alg;
2013 (void) input;
2014 (void) input_length;
2015 (void) mac;
2016 (void) mac_size;
2017 (void) mac_length;
2018 (void) status;
2019 return( PSA_ERROR_INVALID_ARGUMENT );
2020 }
2021}
2022
2023psa_status_t psa_driver_wrapper_mac_sign_setup(
2024 psa_mac_operation_t *operation,
2025 const psa_key_attributes_t *attributes,
2026 const uint8_t *key_buffer,
2027 size_t key_buffer_size,
2028 psa_algorithm_t alg )
2029{
2030 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2031 psa_key_location_t location =
2032 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
2033
2034 switch( location )
2035 {
2036 case PSA_KEY_LOCATION_LOCAL_STORAGE:
2037 /* Key is stored in the slot in export representation, so
2038 * cycle through all known transparent accelerators */
2039#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2040#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002041 status = mbedtls_test_transparent_mac_sign_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002042 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002043 attributes,
2044 key_buffer, key_buffer_size,
2045 alg );
2046 /* Declared with fallback == true */
2047 if( status == PSA_SUCCESS )
2048 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
2049
2050 if( status != PSA_ERROR_NOT_SUPPORTED )
2051 return( status );
2052#endif /* PSA_CRYPTO_DRIVER_TEST */
2053#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2054#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2055 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002056 status = mbedtls_psa_mac_sign_setup( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002057 attributes,
2058 key_buffer, key_buffer_size,
2059 alg );
2060 if( status == PSA_SUCCESS )
2061 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
2062
2063 if( status != PSA_ERROR_NOT_SUPPORTED )
2064 return( status );
2065#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2066 return( PSA_ERROR_NOT_SUPPORTED );
2067
2068 /* Add cases for opaque driver here */
2069#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2070#if defined(PSA_CRYPTO_DRIVER_TEST)
2071 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002072 status = mbedtls_test_opaque_mac_sign_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002073 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002074 attributes,
2075 key_buffer, key_buffer_size,
2076 alg );
2077
2078 if( status == PSA_SUCCESS )
2079 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
2080
2081 return( status );
2082#endif /* PSA_CRYPTO_DRIVER_TEST */
2083#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2084 default:
2085 /* Key is declared with a lifetime not known to us */
2086 (void) status;
2087 (void) key_buffer;
2088 (void) key_buffer_size;
2089 (void) alg;
2090 return( PSA_ERROR_INVALID_ARGUMENT );
2091 }
2092}
2093
2094psa_status_t psa_driver_wrapper_mac_verify_setup(
2095 psa_mac_operation_t *operation,
2096 const psa_key_attributes_t *attributes,
2097 const uint8_t *key_buffer,
2098 size_t key_buffer_size,
2099 psa_algorithm_t alg )
2100{
2101 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2102 psa_key_location_t location =
2103 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
2104
2105 switch( location )
2106 {
2107 case PSA_KEY_LOCATION_LOCAL_STORAGE:
2108 /* Key is stored in the slot in export representation, so
2109 * cycle through all known transparent accelerators */
2110#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2111#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002112 status = mbedtls_test_transparent_mac_verify_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002113 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002114 attributes,
2115 key_buffer, key_buffer_size,
2116 alg );
2117 /* Declared with fallback == true */
2118 if( status == PSA_SUCCESS )
2119 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
2120
2121 if( status != PSA_ERROR_NOT_SUPPORTED )
2122 return( status );
2123#endif /* PSA_CRYPTO_DRIVER_TEST */
2124#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2125#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2126 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002127 status = mbedtls_psa_mac_verify_setup( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002128 attributes,
2129 key_buffer, key_buffer_size,
2130 alg );
2131 if( status == PSA_SUCCESS )
2132 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
2133
2134 if( status != PSA_ERROR_NOT_SUPPORTED )
2135 return( status );
2136#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2137 return( PSA_ERROR_NOT_SUPPORTED );
2138
2139 /* Add cases for opaque driver here */
2140#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2141#if defined(PSA_CRYPTO_DRIVER_TEST)
2142 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002143 status = mbedtls_test_opaque_mac_verify_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002144 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002145 attributes,
2146 key_buffer, key_buffer_size,
2147 alg );
2148
2149 if( status == PSA_SUCCESS )
2150 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
2151
2152 return( status );
2153#endif /* PSA_CRYPTO_DRIVER_TEST */
2154#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2155 default:
2156 /* Key is declared with a lifetime not known to us */
2157 (void) status;
2158 (void) key_buffer;
2159 (void) key_buffer_size;
2160 (void) alg;
2161 return( PSA_ERROR_INVALID_ARGUMENT );
2162 }
2163}
2164
2165psa_status_t psa_driver_wrapper_mac_update(
2166 psa_mac_operation_t *operation,
2167 const uint8_t *input,
2168 size_t input_length )
2169{
2170 switch( operation->id )
2171 {
2172#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2173 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002174 return( mbedtls_psa_mac_update( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002175 input, input_length ) );
2176#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2177
2178#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2179#if defined(PSA_CRYPTO_DRIVER_TEST)
2180 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002181 return( mbedtls_test_transparent_mac_update(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002182 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002183 input, input_length ) );
2184
2185 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002186 return( mbedtls_test_opaque_mac_update(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002187 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002188 input, input_length ) );
2189#endif /* PSA_CRYPTO_DRIVER_TEST */
2190#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2191 default:
2192 (void) input;
2193 (void) input_length;
2194 return( PSA_ERROR_INVALID_ARGUMENT );
2195 }
2196}
2197
2198psa_status_t psa_driver_wrapper_mac_sign_finish(
2199 psa_mac_operation_t *operation,
2200 uint8_t *mac,
2201 size_t mac_size,
2202 size_t *mac_length )
2203{
2204 switch( operation->id )
2205 {
2206#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2207 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002208 return( mbedtls_psa_mac_sign_finish( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002209 mac, mac_size, mac_length ) );
2210#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2211
2212#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2213#if defined(PSA_CRYPTO_DRIVER_TEST)
2214 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002215 return( mbedtls_test_transparent_mac_sign_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002216 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002217 mac, mac_size, mac_length ) );
2218
2219 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002220 return( mbedtls_test_opaque_mac_sign_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002221 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002222 mac, mac_size, mac_length ) );
2223#endif /* PSA_CRYPTO_DRIVER_TEST */
2224#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2225 default:
2226 (void) mac;
2227 (void) mac_size;
2228 (void) mac_length;
2229 return( PSA_ERROR_INVALID_ARGUMENT );
2230 }
2231}
2232
2233psa_status_t psa_driver_wrapper_mac_verify_finish(
2234 psa_mac_operation_t *operation,
2235 const uint8_t *mac,
2236 size_t mac_length )
2237{
2238 switch( operation->id )
2239 {
2240#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2241 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002242 return( mbedtls_psa_mac_verify_finish( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002243 mac, mac_length ) );
2244#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2245
2246#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2247#if defined(PSA_CRYPTO_DRIVER_TEST)
2248 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002249 return( mbedtls_test_transparent_mac_verify_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002250 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002251 mac, mac_length ) );
2252
2253 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002254 return( mbedtls_test_opaque_mac_verify_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002255 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002256 mac, mac_length ) );
2257#endif /* PSA_CRYPTO_DRIVER_TEST */
2258#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2259 default:
2260 (void) mac;
2261 (void) mac_length;
2262 return( PSA_ERROR_INVALID_ARGUMENT );
2263 }
2264}
2265
2266psa_status_t psa_driver_wrapper_mac_abort(
2267 psa_mac_operation_t *operation )
2268{
Steven Cooremand13a70f2021-03-19 15:24:23 +01002269 switch( operation->id )
2270 {
2271#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2272 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooremane6804192021-03-19 18:28:56 +01002273 return( mbedtls_psa_mac_abort( &operation->ctx.mbedtls_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002274#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2275
2276#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2277#if defined(PSA_CRYPTO_DRIVER_TEST)
2278 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002279 return( mbedtls_test_transparent_mac_abort(
Steven Cooremane6804192021-03-19 18:28:56 +01002280 &operation->ctx.transparent_test_driver_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002281 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002282 return( mbedtls_test_opaque_mac_abort(
Steven Cooremane6804192021-03-19 18:28:56 +01002283 &operation->ctx.opaque_test_driver_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002284#endif /* PSA_CRYPTO_DRIVER_TEST */
2285#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2286 default:
Steven Cooremane6804192021-03-19 18:28:56 +01002287 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002288 }
Steven Cooremand13a70f2021-03-19 15:24:23 +01002289}
Gilles Peskine1905a242021-04-24 13:19:45 +02002290
2291#endif /* MBEDTLS_PSA_CRYPTO_C */