blob: d6e5778dff2c6e5e5242c6a20bd16b8c6f77fc94 [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,
838 const uint8_t *input,
839 size_t input_length,
840 uint8_t *output,
841 size_t output_size,
842 size_t *output_length )
843{
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100844 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
845 psa_key_location_t location =
846 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200847
848 switch( location )
849 {
850 case PSA_KEY_LOCATION_LOCAL_STORAGE:
851 /* Key is stored in the slot in export representation, so
852 * cycle through all known transparent accelerators */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100853#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200854#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100855 status = mbedtls_test_transparent_cipher_encrypt( attributes,
856 key_buffer,
857 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200858 alg,
859 input,
860 input_length,
861 output,
862 output_size,
863 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200864 /* Declared with fallback == true */
865 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200866 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200867#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100868#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
869
870#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
871 return( mbedtls_psa_cipher_encrypt( attributes,
872 key_buffer,
873 key_buffer_size,
874 alg,
875 input,
876 input_length,
877 output,
878 output_size,
879 output_length ) );
gabor-mezei-arm0a93b662021-06-25 15:43:32 +0200880#else
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200881 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-arm0a93b662021-06-25 15:43:32 +0200882#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100883
Steven Cooreman37941cb2020-07-28 18:49:51 +0200884 /* Add cases for opaque driver here */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100885#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200886#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100887 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100888 return( mbedtls_test_opaque_cipher_encrypt( attributes,
889 key_buffer,
890 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200891 alg,
892 input,
893 input_length,
894 output,
895 output_size,
896 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200897#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100898#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
899
Steven Cooreman37941cb2020-07-28 18:49:51 +0200900 default:
901 /* Key is declared with a lifetime not known to us */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100902 (void)status;
903 (void)key_buffer;
904 (void)key_buffer_size;
905 (void)alg;
906 (void)input;
907 (void)input_length;
908 (void)output;
909 (void)output_size;
910 (void)output_length;
911 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200912 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200913}
914
915psa_status_t psa_driver_wrapper_cipher_decrypt(
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100916 const psa_key_attributes_t *attributes,
917 const uint8_t *key_buffer,
918 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200919 psa_algorithm_t alg,
920 const uint8_t *input,
921 size_t input_length,
922 uint8_t *output,
923 size_t output_size,
924 size_t *output_length )
925{
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100926 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
927 psa_key_location_t location =
928 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200929
930 switch( location )
931 {
932 case PSA_KEY_LOCATION_LOCAL_STORAGE:
933 /* Key is stored in the slot in export representation, so
934 * cycle through all known transparent accelerators */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100935#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200936#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100937 status = mbedtls_test_transparent_cipher_decrypt( attributes,
938 key_buffer,
939 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200940 alg,
941 input,
942 input_length,
943 output,
944 output_size,
945 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200946 /* Declared with fallback == true */
947 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200948 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200949#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100950#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
951
952#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
953 return( mbedtls_psa_cipher_decrypt( attributes,
954 key_buffer,
955 key_buffer_size,
956 alg,
957 input,
958 input_length,
959 output,
960 output_size,
961 output_length ) );
gabor-mezei-arm0a93b662021-06-25 15:43:32 +0200962#else
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200963 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-arm0a93b662021-06-25 15:43:32 +0200964#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100965
Steven Cooreman37941cb2020-07-28 18:49:51 +0200966 /* Add cases for opaque driver here */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100967#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200968#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100969 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100970 return( mbedtls_test_opaque_cipher_decrypt( attributes,
971 key_buffer,
972 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200973 alg,
974 input,
975 input_length,
976 output,
977 output_size,
978 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200979#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100980#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
981
Steven Cooreman37941cb2020-07-28 18:49:51 +0200982 default:
983 /* Key is declared with a lifetime not known to us */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100984 (void)status;
985 (void)key_buffer;
986 (void)key_buffer_size;
987 (void)alg;
988 (void)input;
989 (void)input_length;
990 (void)output;
991 (void)output_size;
992 (void)output_length;
993 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200994 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200995}
996
997psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100998 psa_cipher_operation_t *operation,
999 const psa_key_attributes_t *attributes,
1000 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001001 psa_algorithm_t alg )
1002{
Ronald Cron0b805592020-12-14 18:08:20 +01001003 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona4af55f2020-12-14 14:36:06 +01001004 psa_key_location_t location =
1005 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001006
Steven Cooreman37941cb2020-07-28 18:49:51 +02001007 switch( location )
1008 {
1009 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1010 /* Key is stored in the slot in export representation, so
1011 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +01001012#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001013#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001014 status = mbedtls_test_transparent_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001015 &operation->ctx.transparent_test_driver_ctx,
1016 attributes,
1017 key_buffer,
1018 key_buffer_size,
1019 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001020 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +02001021 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001022 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +02001023
Ronald Cron0b805592020-12-14 18:08:20 +01001024 if( status != PSA_ERROR_NOT_SUPPORTED )
1025 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001026#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001027#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001028#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001029 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +01001030 status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001031 attributes,
1032 key_buffer,
1033 key_buffer_size,
1034 alg );
1035 if( status == PSA_SUCCESS )
Ronald Cron6e412a72021-03-10 09:58:47 +01001036 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Ronald Cron49fafa92021-03-10 08:34:23 +01001037
Ronald Cron7b4154d2021-03-19 14:49:41 +01001038 if( status != PSA_ERROR_NOT_SUPPORTED )
1039 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001040#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1041 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron0b805592020-12-14 18:08:20 +01001042
Steven Cooreman37941cb2020-07-28 18:49:51 +02001043 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +01001044#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001045#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +01001046 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001047 status = mbedtls_test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001048 &operation->ctx.opaque_test_driver_ctx,
1049 attributes,
1050 key_buffer, key_buffer_size,
1051 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001052
Steven Cooreman37941cb2020-07-28 18:49:51 +02001053 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001054 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001055
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001056 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001057#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001058#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +02001059 default:
1060 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +01001061 (void)status;
Ronald Cron7a55deb2021-04-28 14:29:00 +02001062 (void)operation;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001063 (void)key_buffer;
1064 (void)key_buffer_size;
1065 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +02001066 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001067 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001068}
1069
1070psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +01001071 psa_cipher_operation_t *operation,
1072 const psa_key_attributes_t *attributes,
1073 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001074 psa_algorithm_t alg )
1075{
Steven Cooreman37941cb2020-07-28 18:49:51 +02001076 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crona4af55f2020-12-14 14:36:06 +01001077 psa_key_location_t location =
1078 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001079
Steven Cooreman37941cb2020-07-28 18:49:51 +02001080 switch( location )
1081 {
1082 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1083 /* Key is stored in the slot in export representation, so
1084 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +01001085#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001086#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001087 status = mbedtls_test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001088 &operation->ctx.transparent_test_driver_ctx,
1089 attributes,
1090 key_buffer,
1091 key_buffer_size,
1092 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001093 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +02001094 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001095 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +02001096
Ronald Cron0b805592020-12-14 18:08:20 +01001097 if( status != PSA_ERROR_NOT_SUPPORTED )
1098 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001099#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001100#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001101#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001102 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +01001103 status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001104 attributes,
1105 key_buffer,
1106 key_buffer_size,
1107 alg );
1108 if( status == PSA_SUCCESS )
1109 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1110
1111 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001112#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1113 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron49fafa92021-03-10 08:34:23 +01001114
Steven Cooreman37941cb2020-07-28 18:49:51 +02001115 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +01001116#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001117#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +01001118 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001119 status = mbedtls_test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001120 &operation->ctx.opaque_test_driver_ctx,
1121 attributes,
1122 key_buffer, key_buffer_size,
1123 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001124
Steven Cooreman37941cb2020-07-28 18:49:51 +02001125 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001126 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001127
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001128 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001129#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001130#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +02001131 default:
1132 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +01001133 (void)status;
Ronald Cron17006702021-12-03 15:25:24 +01001134 (void)operation;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001135 (void)key_buffer;
1136 (void)key_buffer_size;
1137 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +02001138 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001139 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001140}
1141
Steven Cooreman37941cb2020-07-28 18:49:51 +02001142psa_status_t psa_driver_wrapper_cipher_set_iv(
Ronald Cron6056fe82020-12-15 13:58:07 +01001143 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001144 const uint8_t *iv,
1145 size_t iv_length )
1146{
Ronald Cron49fafa92021-03-10 08:34:23 +01001147 switch( operation->id )
1148 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001149#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001150 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001151 return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001152 iv,
1153 iv_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001154#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001155
1156#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001157#if defined(PSA_CRYPTO_DRIVER_TEST)
1158 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001159 return( mbedtls_test_transparent_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001160 &operation->ctx.transparent_test_driver_ctx,
1161 iv, iv_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001162
Steven Cooreman37941cb2020-07-28 18:49:51 +02001163 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001164 return( mbedtls_test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001165 &operation->ctx.opaque_test_driver_ctx,
1166 iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001167#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001168#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001169 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001170
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001171 (void)iv;
1172 (void)iv_length;
1173
Ronald Crondd24c9b2020-12-15 14:10:01 +01001174 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001175}
1176
1177psa_status_t psa_driver_wrapper_cipher_update(
Ronald Cron6056fe82020-12-15 13:58:07 +01001178 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001179 const uint8_t *input,
1180 size_t input_length,
1181 uint8_t *output,
1182 size_t output_size,
1183 size_t *output_length )
1184{
Ronald Cron49fafa92021-03-10 08:34:23 +01001185 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001186 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001187#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001188 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001189 return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001190 input,
1191 input_length,
1192 output,
1193 output_size,
1194 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001195#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1196
Ronald Cron49fafa92021-03-10 08:34:23 +01001197#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001198#if defined(PSA_CRYPTO_DRIVER_TEST)
1199 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001200 return( mbedtls_test_transparent_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001201 &operation->ctx.transparent_test_driver_ctx,
1202 input, input_length,
1203 output, output_size, output_length ) );
1204
Steven Cooreman37941cb2020-07-28 18:49:51 +02001205 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001206 return( mbedtls_test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001207 &operation->ctx.opaque_test_driver_ctx,
1208 input, input_length,
1209 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001210#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001211#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001212 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001213
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001214 (void)input;
1215 (void)input_length;
1216 (void)output;
1217 (void)output_size;
1218 (void)output_length;
1219
Ronald Crondd24c9b2020-12-15 14:10:01 +01001220 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001221}
1222
1223psa_status_t psa_driver_wrapper_cipher_finish(
Ronald Cron6056fe82020-12-15 13:58:07 +01001224 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001225 uint8_t *output,
1226 size_t output_size,
1227 size_t *output_length )
1228{
Ronald Cron49fafa92021-03-10 08:34:23 +01001229 switch( operation->id )
1230 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001231#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001232 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001233 return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001234 output,
1235 output_size,
1236 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001237#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001238
1239#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001240#if defined(PSA_CRYPTO_DRIVER_TEST)
1241 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001242 return( mbedtls_test_transparent_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001243 &operation->ctx.transparent_test_driver_ctx,
1244 output, output_size, output_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001245
Steven Cooreman37941cb2020-07-28 18:49:51 +02001246 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001247 return( mbedtls_test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001248 &operation->ctx.opaque_test_driver_ctx,
1249 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001250#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001251#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001252 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001253
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001254 (void)output;
1255 (void)output_size;
1256 (void)output_length;
1257
Ronald Crondd24c9b2020-12-15 14:10:01 +01001258 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001259}
1260
1261psa_status_t psa_driver_wrapper_cipher_abort(
Ronald Cron6056fe82020-12-15 13:58:07 +01001262 psa_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001263{
Ronald Crondd24c9b2020-12-15 14:10:01 +01001264 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001265
Ronald Cron49fafa92021-03-10 08:34:23 +01001266 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001267 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001268#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001269 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001270 return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001271#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +01001272
1273#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001274#if defined(PSA_CRYPTO_DRIVER_TEST)
1275 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001276 status = mbedtls_test_transparent_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001277 &operation->ctx.transparent_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001278 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001279 &operation->ctx.transparent_test_driver_ctx,
1280 sizeof( operation->ctx.transparent_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001281 return( status );
Ronald Cron49fafa92021-03-10 08:34:23 +01001282
Steven Cooreman37941cb2020-07-28 18:49:51 +02001283 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001284 status = mbedtls_test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001285 &operation->ctx.opaque_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001286 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001287 &operation->ctx.opaque_test_driver_ctx,
1288 sizeof( operation->ctx.opaque_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001289 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001290#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001291#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001292 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001293
Ronald Cron49fafa92021-03-10 08:34:23 +01001294 (void)status;
Ronald Crondd24c9b2020-12-15 14:10:01 +01001295 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001296}
1297
Steven Cooreman1e582352021-02-18 17:24:37 +01001298/*
1299 * Hashing functions
1300 */
1301psa_status_t psa_driver_wrapper_hash_compute(
1302 psa_algorithm_t alg,
1303 const uint8_t *input,
1304 size_t input_length,
1305 uint8_t *hash,
1306 size_t hash_size,
1307 size_t *hash_length)
1308{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001309 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001310
1311 /* Try accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001312#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001313 status = mbedtls_test_transparent_hash_compute(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001314 alg, input, input_length, hash, hash_size, hash_length );
Steven Cooremanf7638102021-03-04 15:14:36 +01001315 if( status != PSA_ERROR_NOT_SUPPORTED )
1316 return( status );
1317#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001318
1319 /* If software fallback is compiled in, try fallback */
1320#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1321 status = mbedtls_psa_hash_compute( alg, input, input_length,
1322 hash, hash_size, hash_length );
1323 if( status != PSA_ERROR_NOT_SUPPORTED )
1324 return( status );
1325#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001326 (void) status;
1327 (void) alg;
1328 (void) input;
1329 (void) input_length;
1330 (void) hash;
1331 (void) hash_size;
1332 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001333
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001334 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman1e582352021-02-18 17:24:37 +01001335}
1336
1337psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001338 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001339 psa_algorithm_t alg )
1340{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001341 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001342
Steven Cooreman1e582352021-02-18 17:24:37 +01001343 /* Try setup on accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001344#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001345 status = mbedtls_test_transparent_hash_setup(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001346 &operation->ctx.test_driver_ctx, alg );
Steven Cooremanf7638102021-03-04 15:14:36 +01001347 if( status == PSA_SUCCESS )
1348 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1349
1350 if( status != PSA_ERROR_NOT_SUPPORTED )
1351 return( status );
1352#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001353
1354 /* If software fallback is compiled in, try fallback */
1355#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001356 status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
Steven Cooreman1e582352021-02-18 17:24:37 +01001357 if( status == PSA_SUCCESS )
Steven Cooreman1e582352021-02-18 17:24:37 +01001358 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Steven Cooreman1e582352021-02-18 17:24:37 +01001359
1360 if( status != PSA_ERROR_NOT_SUPPORTED )
1361 return( status );
1362#endif
1363 /* Nothing left to try if we fall through here */
1364 (void) status;
1365 (void) operation;
1366 (void) alg;
1367 return( PSA_ERROR_NOT_SUPPORTED );
1368}
1369
1370psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001371 const psa_hash_operation_t *source_operation,
1372 psa_hash_operation_t *target_operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001373{
Steven Cooreman1e582352021-02-18 17:24:37 +01001374 switch( source_operation->id )
1375 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001376#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1377 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1378 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1379 return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
1380 &target_operation->ctx.mbedtls_ctx ) );
1381#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001382#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001383 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1384 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Ronald Cron7f13fa22021-04-13 12:41:34 +02001385 return( mbedtls_test_transparent_hash_clone(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001386 &source_operation->ctx.test_driver_ctx,
1387 &target_operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001388#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001389 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001390 (void) target_operation;
1391 return( PSA_ERROR_BAD_STATE );
1392 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001393}
1394
1395psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001396 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001397 const uint8_t *input,
1398 size_t input_length )
1399{
Steven Cooreman1e582352021-02-18 17:24:37 +01001400 switch( operation->id )
1401 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001402#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1403 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1404 return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
1405 input, input_length ) );
1406#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001407#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001408 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001409 return( mbedtls_test_transparent_hash_update(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001410 &operation->ctx.test_driver_ctx,
1411 input, input_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001412#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001413 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001414 (void) input;
1415 (void) input_length;
1416 return( PSA_ERROR_BAD_STATE );
1417 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001418}
1419
1420psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001421 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001422 uint8_t *hash,
1423 size_t hash_size,
1424 size_t *hash_length )
1425{
Steven Cooreman1e582352021-02-18 17:24:37 +01001426 switch( operation->id )
1427 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001428#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1429 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1430 return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
1431 hash, hash_size, hash_length ) );
1432#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001433#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001434 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001435 return( mbedtls_test_transparent_hash_finish(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001436 &operation->ctx.test_driver_ctx,
1437 hash, hash_size, hash_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001438#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001439 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001440 (void) hash;
1441 (void) hash_size;
1442 (void) hash_length;
1443 return( PSA_ERROR_BAD_STATE );
1444 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001445}
1446
1447psa_status_t psa_driver_wrapper_hash_abort(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001448 psa_hash_operation_t *operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001449{
Steven Cooreman1e582352021-02-18 17:24:37 +01001450 switch( operation->id )
1451 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001452#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1453 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1454 return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
1455#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001456#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001457 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001458 return( mbedtls_test_transparent_hash_abort(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001459 &operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001460#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001461 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001462 return( PSA_ERROR_BAD_STATE );
1463 }
1464}
1465
Ronald Cronde822812021-03-17 16:08:20 +01001466psa_status_t psa_driver_wrapper_aead_encrypt(
1467 const psa_key_attributes_t *attributes,
1468 const uint8_t *key_buffer, size_t key_buffer_size,
1469 psa_algorithm_t alg,
1470 const uint8_t *nonce, size_t nonce_length,
1471 const uint8_t *additional_data, size_t additional_data_length,
1472 const uint8_t *plaintext, size_t plaintext_length,
1473 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
1474{
1475 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1476 psa_key_location_t location =
1477 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1478
1479 switch( location )
1480 {
1481 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1482 /* Key is stored in the slot in export representation, so
1483 * cycle through all known transparent accelerators */
1484
1485#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1486#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001487 status = mbedtls_test_transparent_aead_encrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001488 attributes, key_buffer, key_buffer_size,
1489 alg,
1490 nonce, nonce_length,
1491 additional_data, additional_data_length,
1492 plaintext, plaintext_length,
1493 ciphertext, ciphertext_size, ciphertext_length );
1494 /* Declared with fallback == true */
1495 if( status != PSA_ERROR_NOT_SUPPORTED )
1496 return( status );
1497#endif /* PSA_CRYPTO_DRIVER_TEST */
1498#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1499
1500 /* Fell through, meaning no accelerator supports this operation */
1501 return( mbedtls_psa_aead_encrypt(
1502 attributes, key_buffer, key_buffer_size,
1503 alg,
1504 nonce, nonce_length,
1505 additional_data, additional_data_length,
1506 plaintext, plaintext_length,
1507 ciphertext, ciphertext_size, ciphertext_length ) );
1508
1509 /* Add cases for opaque driver here */
1510
1511 default:
1512 /* Key is declared with a lifetime not known to us */
1513 (void)status;
1514 return( PSA_ERROR_INVALID_ARGUMENT );
1515 }
1516}
1517
1518psa_status_t psa_driver_wrapper_aead_decrypt(
1519 const psa_key_attributes_t *attributes,
1520 const uint8_t *key_buffer, size_t key_buffer_size,
1521 psa_algorithm_t alg,
1522 const uint8_t *nonce, size_t nonce_length,
1523 const uint8_t *additional_data, size_t additional_data_length,
1524 const uint8_t *ciphertext, size_t ciphertext_length,
1525 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
1526{
1527 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1528 psa_key_location_t location =
1529 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1530
1531 switch( location )
1532 {
1533 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1534 /* Key is stored in the slot in export representation, so
1535 * cycle through all known transparent accelerators */
1536
1537#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1538#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001539 status = mbedtls_test_transparent_aead_decrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001540 attributes, key_buffer, key_buffer_size,
1541 alg,
1542 nonce, nonce_length,
1543 additional_data, additional_data_length,
1544 ciphertext, ciphertext_length,
1545 plaintext, plaintext_size, plaintext_length );
1546 /* Declared with fallback == true */
1547 if( status != PSA_ERROR_NOT_SUPPORTED )
1548 return( status );
1549#endif /* PSA_CRYPTO_DRIVER_TEST */
1550#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1551
1552 /* Fell through, meaning no accelerator supports this operation */
1553 return( mbedtls_psa_aead_decrypt(
1554 attributes, key_buffer, key_buffer_size,
1555 alg,
1556 nonce, nonce_length,
1557 additional_data, additional_data_length,
1558 ciphertext, ciphertext_length,
1559 plaintext, plaintext_size, plaintext_length ) );
1560
1561 /* Add cases for opaque driver here */
1562
1563 default:
1564 /* Key is declared with a lifetime not known to us */
1565 (void)status;
1566 return( PSA_ERROR_INVALID_ARGUMENT );
1567 }
1568}
Steven Cooremand13a70f2021-03-19 15:24:23 +01001569
Paul Elliott6504aa62021-04-20 17:09:36 +01001570psa_status_t psa_driver_wrapper_aead_encrypt_setup(
1571 psa_aead_operation_t *operation,
1572 const psa_key_attributes_t *attributes,
1573 const uint8_t *key_buffer, size_t key_buffer_size,
1574 psa_algorithm_t alg )
1575{
1576 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1577 psa_key_location_t location =
1578 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1579
1580 switch( location )
1581 {
1582 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1583 /* Key is stored in the slot in export representation, so
1584 * cycle through all known transparent accelerators */
1585
1586#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1587#if defined(PSA_CRYPTO_DRIVER_TEST)
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001588 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Paul Elliotta218ceb2021-05-07 15:10:31 +01001589 status = mbedtls_test_transparent_aead_encrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001590 &operation->ctx.transparent_test_driver_ctx,
1591 attributes, key_buffer, key_buffer_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001592 alg );
Paul Elliott6504aa62021-04-20 17:09:36 +01001593
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001594 /* Declared with fallback == true */
Paul Elliott6504aa62021-04-20 17:09:36 +01001595 if( status != PSA_ERROR_NOT_SUPPORTED )
1596 return( status );
1597#endif /* PSA_CRYPTO_DRIVER_TEST */
1598#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1599
1600 /* Fell through, meaning no accelerator supports this operation */
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001601 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Paul Elliott6504aa62021-04-20 17:09:36 +01001602 status = mbedtls_psa_aead_encrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001603 &operation->ctx.mbedtls_ctx, attributes,
Paul Elliott6504aa62021-04-20 17:09:36 +01001604 key_buffer, key_buffer_size,
1605 alg );
1606
Paul Elliott6504aa62021-04-20 17:09:36 +01001607 return( status );
1608
1609 /* Add cases for opaque driver here */
1610
1611 default:
1612 /* Key is declared with a lifetime not known to us */
1613 (void)status;
1614 return( PSA_ERROR_INVALID_ARGUMENT );
1615 }
1616}
1617
1618psa_status_t psa_driver_wrapper_aead_decrypt_setup(
1619 psa_aead_operation_t *operation,
1620 const psa_key_attributes_t *attributes,
1621 const uint8_t *key_buffer, size_t key_buffer_size,
1622 psa_algorithm_t alg )
1623{
1624 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1625 psa_key_location_t location =
1626 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1627
1628 switch( location )
1629 {
1630 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1631 /* Key is stored in the slot in export representation, so
1632 * cycle through all known transparent accelerators */
1633
1634#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1635#if defined(PSA_CRYPTO_DRIVER_TEST)
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001636 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Paul Elliotta218ceb2021-05-07 15:10:31 +01001637 status = mbedtls_test_transparent_aead_decrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001638 &operation->ctx.transparent_test_driver_ctx,
Paul Elliott5d3a3c32021-08-19 18:34:41 +01001639 attributes,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001640 key_buffer, key_buffer_size,
1641 alg );
Paul Elliott6504aa62021-04-20 17:09:36 +01001642
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001643 /* Declared with fallback == true */
Paul Elliott6504aa62021-04-20 17:09:36 +01001644 if( status != PSA_ERROR_NOT_SUPPORTED )
1645 return( status );
1646#endif /* PSA_CRYPTO_DRIVER_TEST */
1647#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1648
1649 /* Fell through, meaning no accelerator supports this operation */
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001650 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Paul Elliott6504aa62021-04-20 17:09:36 +01001651 status = mbedtls_psa_aead_decrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001652 &operation->ctx.mbedtls_ctx,
Paul Elliott5d3a3c32021-08-19 18:34:41 +01001653 attributes,
Paul Elliott6504aa62021-04-20 17:09:36 +01001654 key_buffer, key_buffer_size,
1655 alg );
1656
Paul Elliott6504aa62021-04-20 17:09:36 +01001657 return( status );
1658
1659 /* Add cases for opaque driver here */
1660
1661 default:
1662 /* Key is declared with a lifetime not known to us */
1663 (void)status;
1664 return( PSA_ERROR_INVALID_ARGUMENT );
1665 }
1666}
1667
Paul Elliott6504aa62021-04-20 17:09:36 +01001668psa_status_t psa_driver_wrapper_aead_set_nonce(
1669 psa_aead_operation_t *operation,
1670 const uint8_t *nonce,
1671 size_t nonce_length )
1672{
1673 switch( operation->id )
1674 {
Paul Elliott5c656cb2021-05-19 14:15:01 +01001675#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001676 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001677 return( mbedtls_psa_aead_set_nonce( &operation->ctx.mbedtls_ctx,
1678 nonce,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001679 nonce_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001680
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001681#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001682
1683#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1684#if defined(PSA_CRYPTO_DRIVER_TEST)
1685 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001686 return( mbedtls_test_transparent_aead_set_nonce(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001687 &operation->ctx.transparent_test_driver_ctx,
1688 nonce, nonce_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001689
1690 /* Add cases for opaque driver here */
1691
1692#endif /* PSA_CRYPTO_DRIVER_TEST */
1693#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1694 }
1695
1696 (void)nonce;
1697 (void)nonce_length;
1698
1699 return( PSA_ERROR_INVALID_ARGUMENT );
1700}
1701
1702psa_status_t psa_driver_wrapper_aead_set_lengths(
1703 psa_aead_operation_t *operation,
1704 size_t ad_length,
1705 size_t plaintext_length )
1706{
1707 switch( operation->id )
1708 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001709#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001710 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottdff6c5d2021-09-28 11:00:20 +01001711 return( mbedtls_psa_aead_set_lengths( &operation->ctx.mbedtls_ctx,
1712 ad_length,
1713 plaintext_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001714
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001715#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001716
1717#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1718#if defined(PSA_CRYPTO_DRIVER_TEST)
1719 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001720 return( mbedtls_test_transparent_aead_set_lengths(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001721 &operation->ctx.transparent_test_driver_ctx,
1722 ad_length, plaintext_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001723
1724 /* Add cases for opaque driver here */
1725
1726#endif /* PSA_CRYPTO_DRIVER_TEST */
1727#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1728 }
1729
1730 (void)ad_length;
1731 (void)plaintext_length;
1732
1733 return( PSA_ERROR_INVALID_ARGUMENT );
1734}
1735
1736psa_status_t psa_driver_wrapper_aead_update_ad(
1737 psa_aead_operation_t *operation,
1738 const uint8_t *input,
1739 size_t input_length )
1740{
1741 switch( operation->id )
1742 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001743#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001744 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001745 return( mbedtls_psa_aead_update_ad( &operation->ctx.mbedtls_ctx,
1746 input,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001747 input_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001748
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001749#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001750
1751#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1752#if defined(PSA_CRYPTO_DRIVER_TEST)
1753 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001754 return( mbedtls_test_transparent_aead_update_ad(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001755 &operation->ctx.transparent_test_driver_ctx,
1756 input, input_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001757
1758 /* Add cases for opaque driver here */
1759
1760#endif /* PSA_CRYPTO_DRIVER_TEST */
1761#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1762 }
1763
1764 (void)input;
1765 (void)input_length;
1766
1767 return( PSA_ERROR_INVALID_ARGUMENT );
1768}
1769
1770psa_status_t psa_driver_wrapper_aead_update(
1771 psa_aead_operation_t *operation,
1772 const uint8_t *input,
1773 size_t input_length,
1774 uint8_t *output,
1775 size_t output_size,
1776 size_t *output_length )
1777{
1778 switch( operation->id )
1779 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001780#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001781 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001782 return( mbedtls_psa_aead_update( &operation->ctx.mbedtls_ctx,
1783 input, input_length,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001784 output, output_size,
1785 output_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001786
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001787#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001788
1789#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1790#if defined(PSA_CRYPTO_DRIVER_TEST)
1791 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001792 return( mbedtls_test_transparent_aead_update(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001793 &operation->ctx.transparent_test_driver_ctx,
1794 input, input_length, output, output_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001795 output_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001796
1797 /* Add cases for opaque driver here */
1798
1799#endif /* PSA_CRYPTO_DRIVER_TEST */
1800#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1801 }
1802
1803 (void)input;
1804 (void)input_length;
1805 (void)output;
1806 (void)output_size;
1807 (void)output_length;
1808
1809 return( PSA_ERROR_INVALID_ARGUMENT );
1810}
1811
1812psa_status_t psa_driver_wrapper_aead_finish(
1813 psa_aead_operation_t *operation,
1814 uint8_t *ciphertext,
1815 size_t ciphertext_size,
1816 size_t *ciphertext_length,
1817 uint8_t *tag,
1818 size_t tag_size,
1819 size_t *tag_length )
1820{
1821 switch( operation->id )
1822 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001823#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001824 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001825 return( mbedtls_psa_aead_finish( &operation->ctx.mbedtls_ctx,
1826 ciphertext,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001827 ciphertext_size,
1828 ciphertext_length, tag,
1829 tag_size, tag_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001830
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001831#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001832
1833#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1834#if defined(PSA_CRYPTO_DRIVER_TEST)
1835 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001836 return( mbedtls_test_transparent_aead_finish(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001837 &operation->ctx.transparent_test_driver_ctx,
1838 ciphertext, ciphertext_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001839 ciphertext_length, tag, tag_size, tag_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001840
1841 /* Add cases for opaque driver here */
1842
1843#endif /* PSA_CRYPTO_DRIVER_TEST */
1844#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1845 }
1846
1847 (void)ciphertext;
1848 (void)ciphertext_size;
1849 (void)ciphertext_length;
1850 (void)tag;
1851 (void)tag_size;
1852 (void)tag_length;
1853
1854 return( PSA_ERROR_INVALID_ARGUMENT );
1855}
1856
1857psa_status_t psa_driver_wrapper_aead_verify(
1858 psa_aead_operation_t *operation,
1859 uint8_t *plaintext,
1860 size_t plaintext_size,
1861 size_t *plaintext_length,
1862 const uint8_t *tag,
1863 size_t tag_length )
1864{
1865 switch( operation->id )
1866 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001867#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001868 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliott315628d2021-07-20 18:25:54 +01001869 {
1870 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1871 uint8_t check_tag[PSA_AEAD_TAG_MAX_SIZE];
1872 size_t check_tag_length;
1873
1874 status = mbedtls_psa_aead_finish( &operation->ctx.mbedtls_ctx,
1875 plaintext,
1876 plaintext_size,
1877 plaintext_length,
1878 check_tag,
Paul Elliottb183d562021-09-13 19:02:57 +01001879 sizeof( check_tag ),
Paul Elliott315628d2021-07-20 18:25:54 +01001880 &check_tag_length );
1881
1882 if( status == PSA_SUCCESS )
1883 {
1884 if( tag_length != check_tag_length ||
1885 mbedtls_psa_safer_memcmp( tag, check_tag, tag_length )
1886 != 0 )
1887 status = PSA_ERROR_INVALID_SIGNATURE;
1888 }
1889
Paul Elliott06b6b8c2021-09-13 19:02:04 +01001890 mbedtls_platform_zeroize( check_tag, sizeof( check_tag ) );
1891
Paul Elliott315628d2021-07-20 18:25:54 +01001892 return( status );
1893 }
Paul Elliott6504aa62021-04-20 17:09:36 +01001894
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001895#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001896
1897#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1898#if defined(PSA_CRYPTO_DRIVER_TEST)
1899 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001900 return( mbedtls_test_transparent_aead_verify(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001901 &operation->ctx.transparent_test_driver_ctx,
1902 plaintext, plaintext_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001903 plaintext_length, tag, tag_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001904
1905 /* Add cases for opaque driver here */
1906
1907#endif /* PSA_CRYPTO_DRIVER_TEST */
1908#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1909 }
1910
1911 (void)plaintext;
1912 (void)plaintext_size;
1913 (void)plaintext_length;
1914 (void)tag;
1915 (void)tag_length;
1916
1917 return( PSA_ERROR_INVALID_ARGUMENT );
1918}
1919
1920psa_status_t psa_driver_wrapper_aead_abort(
1921 psa_aead_operation_t *operation )
1922{
1923 switch( operation->id )
1924 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001925#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001926 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001927 return( mbedtls_psa_aead_abort( &operation->ctx.mbedtls_ctx ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001928
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001929#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001930
1931#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1932#if defined(PSA_CRYPTO_DRIVER_TEST)
1933 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001934 return( mbedtls_test_transparent_aead_abort(
1935 &operation->ctx.transparent_test_driver_ctx ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001936
1937 /* Add cases for opaque driver here */
1938
1939#endif /* PSA_CRYPTO_DRIVER_TEST */
1940#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1941 }
1942
1943 return( PSA_ERROR_INVALID_ARGUMENT );
1944}
Steven Cooremand13a70f2021-03-19 15:24:23 +01001945
1946/*
1947 * MAC functions
1948 */
1949psa_status_t psa_driver_wrapper_mac_compute(
1950 const psa_key_attributes_t *attributes,
1951 const uint8_t *key_buffer,
1952 size_t key_buffer_size,
1953 psa_algorithm_t alg,
1954 const uint8_t *input,
1955 size_t input_length,
1956 uint8_t *mac,
1957 size_t mac_size,
1958 size_t *mac_length )
1959{
1960 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1961 psa_key_location_t location =
1962 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1963
1964 switch( location )
1965 {
1966 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1967 /* Key is stored in the slot in export representation, so
1968 * cycle through all known transparent accelerators */
1969#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1970#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001971 status = mbedtls_test_transparent_mac_compute(
Steven Cooremand13a70f2021-03-19 15:24:23 +01001972 attributes, key_buffer, key_buffer_size, alg,
1973 input, input_length,
1974 mac, mac_size, mac_length );
1975 /* Declared with fallback == true */
1976 if( status != PSA_ERROR_NOT_SUPPORTED )
1977 return( status );
1978#endif /* PSA_CRYPTO_DRIVER_TEST */
1979#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1980#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1981 /* Fell through, meaning no accelerator supports this operation */
1982 status = mbedtls_psa_mac_compute(
1983 attributes, key_buffer, key_buffer_size, alg,
1984 input, input_length,
1985 mac, mac_size, mac_length );
1986 if( status != PSA_ERROR_NOT_SUPPORTED )
1987 return( status );
1988#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1989 return( PSA_ERROR_NOT_SUPPORTED );
1990
1991 /* Add cases for opaque driver here */
1992#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1993#if defined(PSA_CRYPTO_DRIVER_TEST)
1994 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001995 status = mbedtls_test_opaque_mac_compute(
Steven Cooremand13a70f2021-03-19 15:24:23 +01001996 attributes, key_buffer, key_buffer_size, alg,
1997 input, input_length,
1998 mac, mac_size, mac_length );
1999 return( status );
2000#endif /* PSA_CRYPTO_DRIVER_TEST */
2001#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2002 default:
2003 /* Key is declared with a lifetime not known to us */
2004 (void) key_buffer;
2005 (void) key_buffer_size;
2006 (void) alg;
2007 (void) input;
2008 (void) input_length;
2009 (void) mac;
2010 (void) mac_size;
2011 (void) mac_length;
2012 (void) status;
2013 return( PSA_ERROR_INVALID_ARGUMENT );
2014 }
2015}
2016
2017psa_status_t psa_driver_wrapper_mac_sign_setup(
2018 psa_mac_operation_t *operation,
2019 const psa_key_attributes_t *attributes,
2020 const uint8_t *key_buffer,
2021 size_t key_buffer_size,
2022 psa_algorithm_t alg )
2023{
2024 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2025 psa_key_location_t location =
2026 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
2027
2028 switch( location )
2029 {
2030 case PSA_KEY_LOCATION_LOCAL_STORAGE:
2031 /* Key is stored in the slot in export representation, so
2032 * cycle through all known transparent accelerators */
2033#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2034#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002035 status = mbedtls_test_transparent_mac_sign_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002036 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002037 attributes,
2038 key_buffer, key_buffer_size,
2039 alg );
2040 /* Declared with fallback == true */
2041 if( status == PSA_SUCCESS )
2042 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
2043
2044 if( status != PSA_ERROR_NOT_SUPPORTED )
2045 return( status );
2046#endif /* PSA_CRYPTO_DRIVER_TEST */
2047#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2048#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2049 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002050 status = mbedtls_psa_mac_sign_setup( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002051 attributes,
2052 key_buffer, key_buffer_size,
2053 alg );
2054 if( status == PSA_SUCCESS )
2055 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
2056
2057 if( status != PSA_ERROR_NOT_SUPPORTED )
2058 return( status );
2059#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2060 return( PSA_ERROR_NOT_SUPPORTED );
2061
2062 /* Add cases for opaque driver here */
2063#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2064#if defined(PSA_CRYPTO_DRIVER_TEST)
2065 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002066 status = mbedtls_test_opaque_mac_sign_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002067 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002068 attributes,
2069 key_buffer, key_buffer_size,
2070 alg );
2071
2072 if( status == PSA_SUCCESS )
2073 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
2074
2075 return( status );
2076#endif /* PSA_CRYPTO_DRIVER_TEST */
2077#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2078 default:
2079 /* Key is declared with a lifetime not known to us */
2080 (void) status;
Ronald Cron7a55deb2021-04-28 14:29:00 +02002081 (void) operation;
Steven Cooremand13a70f2021-03-19 15:24:23 +01002082 (void) key_buffer;
2083 (void) key_buffer_size;
2084 (void) alg;
2085 return( PSA_ERROR_INVALID_ARGUMENT );
2086 }
2087}
2088
2089psa_status_t psa_driver_wrapper_mac_verify_setup(
2090 psa_mac_operation_t *operation,
2091 const psa_key_attributes_t *attributes,
2092 const uint8_t *key_buffer,
2093 size_t key_buffer_size,
2094 psa_algorithm_t alg )
2095{
2096 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2097 psa_key_location_t location =
2098 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
2099
2100 switch( location )
2101 {
2102 case PSA_KEY_LOCATION_LOCAL_STORAGE:
2103 /* Key is stored in the slot in export representation, so
2104 * cycle through all known transparent accelerators */
2105#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2106#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002107 status = mbedtls_test_transparent_mac_verify_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002108 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002109 attributes,
2110 key_buffer, key_buffer_size,
2111 alg );
2112 /* Declared with fallback == true */
2113 if( status == PSA_SUCCESS )
2114 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
2115
2116 if( status != PSA_ERROR_NOT_SUPPORTED )
2117 return( status );
2118#endif /* PSA_CRYPTO_DRIVER_TEST */
2119#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2120#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2121 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002122 status = mbedtls_psa_mac_verify_setup( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002123 attributes,
2124 key_buffer, key_buffer_size,
2125 alg );
2126 if( status == PSA_SUCCESS )
2127 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
2128
2129 if( status != PSA_ERROR_NOT_SUPPORTED )
2130 return( status );
2131#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2132 return( PSA_ERROR_NOT_SUPPORTED );
2133
2134 /* Add cases for opaque driver here */
2135#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2136#if defined(PSA_CRYPTO_DRIVER_TEST)
2137 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002138 status = mbedtls_test_opaque_mac_verify_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002139 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002140 attributes,
2141 key_buffer, key_buffer_size,
2142 alg );
2143
2144 if( status == PSA_SUCCESS )
2145 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
2146
2147 return( status );
2148#endif /* PSA_CRYPTO_DRIVER_TEST */
2149#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2150 default:
2151 /* Key is declared with a lifetime not known to us */
2152 (void) status;
Ronald Cron7a55deb2021-04-28 14:29:00 +02002153 (void) operation;
Steven Cooremand13a70f2021-03-19 15:24:23 +01002154 (void) key_buffer;
2155 (void) key_buffer_size;
2156 (void) alg;
2157 return( PSA_ERROR_INVALID_ARGUMENT );
2158 }
2159}
2160
2161psa_status_t psa_driver_wrapper_mac_update(
2162 psa_mac_operation_t *operation,
2163 const uint8_t *input,
2164 size_t input_length )
2165{
2166 switch( operation->id )
2167 {
2168#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2169 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002170 return( mbedtls_psa_mac_update( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002171 input, input_length ) );
2172#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2173
2174#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2175#if defined(PSA_CRYPTO_DRIVER_TEST)
2176 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002177 return( mbedtls_test_transparent_mac_update(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002178 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002179 input, input_length ) );
2180
2181 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002182 return( mbedtls_test_opaque_mac_update(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002183 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002184 input, input_length ) );
2185#endif /* PSA_CRYPTO_DRIVER_TEST */
2186#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2187 default:
2188 (void) input;
2189 (void) input_length;
2190 return( PSA_ERROR_INVALID_ARGUMENT );
2191 }
2192}
2193
2194psa_status_t psa_driver_wrapper_mac_sign_finish(
2195 psa_mac_operation_t *operation,
2196 uint8_t *mac,
2197 size_t mac_size,
2198 size_t *mac_length )
2199{
2200 switch( operation->id )
2201 {
2202#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2203 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002204 return( mbedtls_psa_mac_sign_finish( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002205 mac, mac_size, mac_length ) );
2206#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2207
2208#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2209#if defined(PSA_CRYPTO_DRIVER_TEST)
2210 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002211 return( mbedtls_test_transparent_mac_sign_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002212 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002213 mac, mac_size, mac_length ) );
2214
2215 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002216 return( mbedtls_test_opaque_mac_sign_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002217 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002218 mac, mac_size, mac_length ) );
2219#endif /* PSA_CRYPTO_DRIVER_TEST */
2220#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2221 default:
2222 (void) mac;
2223 (void) mac_size;
2224 (void) mac_length;
2225 return( PSA_ERROR_INVALID_ARGUMENT );
2226 }
2227}
2228
2229psa_status_t psa_driver_wrapper_mac_verify_finish(
2230 psa_mac_operation_t *operation,
2231 const uint8_t *mac,
2232 size_t mac_length )
2233{
2234 switch( operation->id )
2235 {
2236#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2237 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002238 return( mbedtls_psa_mac_verify_finish( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002239 mac, mac_length ) );
2240#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2241
2242#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2243#if defined(PSA_CRYPTO_DRIVER_TEST)
2244 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002245 return( mbedtls_test_transparent_mac_verify_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002246 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002247 mac, mac_length ) );
2248
2249 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002250 return( mbedtls_test_opaque_mac_verify_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002251 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002252 mac, mac_length ) );
2253#endif /* PSA_CRYPTO_DRIVER_TEST */
2254#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2255 default:
2256 (void) mac;
2257 (void) mac_length;
2258 return( PSA_ERROR_INVALID_ARGUMENT );
2259 }
2260}
2261
2262psa_status_t psa_driver_wrapper_mac_abort(
2263 psa_mac_operation_t *operation )
2264{
Steven Cooremand13a70f2021-03-19 15:24:23 +01002265 switch( operation->id )
2266 {
2267#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2268 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooremane6804192021-03-19 18:28:56 +01002269 return( mbedtls_psa_mac_abort( &operation->ctx.mbedtls_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002270#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2271
2272#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2273#if defined(PSA_CRYPTO_DRIVER_TEST)
2274 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002275 return( mbedtls_test_transparent_mac_abort(
Steven Cooremane6804192021-03-19 18:28:56 +01002276 &operation->ctx.transparent_test_driver_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002277 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002278 return( mbedtls_test_opaque_mac_abort(
Steven Cooremane6804192021-03-19 18:28:56 +01002279 &operation->ctx.opaque_test_driver_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002280#endif /* PSA_CRYPTO_DRIVER_TEST */
2281#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2282 default:
Steven Cooremane6804192021-03-19 18:28:56 +01002283 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002284 }
Steven Cooremand13a70f2021-03-19 15:24:23 +01002285}
Gilles Peskine1905a242021-04-24 13:19:45 +02002286
2287#endif /* MBEDTLS_PSA_CRYPTO_C */