blob: 4bbb61c3d46265d3b72195b3b085d15321b2aa7b [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 Cron5d9b00d2021-03-10 14:43:20 +01001062 (void)key_buffer;
1063 (void)key_buffer_size;
1064 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +02001065 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001066 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001067}
1068
1069psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +01001070 psa_cipher_operation_t *operation,
1071 const psa_key_attributes_t *attributes,
1072 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001073 psa_algorithm_t alg )
1074{
Steven Cooreman37941cb2020-07-28 18:49:51 +02001075 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crona4af55f2020-12-14 14:36:06 +01001076 psa_key_location_t location =
1077 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001078
Steven Cooreman37941cb2020-07-28 18:49:51 +02001079 switch( location )
1080 {
1081 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1082 /* Key is stored in the slot in export representation, so
1083 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +01001084#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001085#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001086 status = mbedtls_test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001087 &operation->ctx.transparent_test_driver_ctx,
1088 attributes,
1089 key_buffer,
1090 key_buffer_size,
1091 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001092 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +02001093 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001094 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +02001095
Ronald Cron0b805592020-12-14 18:08:20 +01001096 if( status != PSA_ERROR_NOT_SUPPORTED )
1097 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001098#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001099#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001100#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001101 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +01001102 status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001103 attributes,
1104 key_buffer,
1105 key_buffer_size,
1106 alg );
1107 if( status == PSA_SUCCESS )
1108 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1109
1110 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001111#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1112 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron49fafa92021-03-10 08:34:23 +01001113
Steven Cooreman37941cb2020-07-28 18:49:51 +02001114 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +01001115#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001116#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +01001117 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001118 status = mbedtls_test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001119 &operation->ctx.opaque_test_driver_ctx,
1120 attributes,
1121 key_buffer, key_buffer_size,
1122 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001123
Steven Cooreman37941cb2020-07-28 18:49:51 +02001124 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001125 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001126
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001127 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001128#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001129#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +02001130 default:
1131 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +01001132 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001133 (void)key_buffer;
1134 (void)key_buffer_size;
1135 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +02001136 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001137 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001138}
1139
Steven Cooreman37941cb2020-07-28 18:49:51 +02001140psa_status_t psa_driver_wrapper_cipher_set_iv(
Ronald Cron6056fe82020-12-15 13:58:07 +01001141 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001142 const uint8_t *iv,
1143 size_t iv_length )
1144{
Ronald Cron49fafa92021-03-10 08:34:23 +01001145 switch( operation->id )
1146 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001147#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001148 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001149 return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001150 iv,
1151 iv_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001152#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001153
1154#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001155#if defined(PSA_CRYPTO_DRIVER_TEST)
1156 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001157 return( mbedtls_test_transparent_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001158 &operation->ctx.transparent_test_driver_ctx,
1159 iv, iv_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001160
Steven Cooreman37941cb2020-07-28 18:49:51 +02001161 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001162 return( mbedtls_test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001163 &operation->ctx.opaque_test_driver_ctx,
1164 iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001165#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001166#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001167 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001168
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001169 (void)iv;
1170 (void)iv_length;
1171
Ronald Crondd24c9b2020-12-15 14:10:01 +01001172 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001173}
1174
1175psa_status_t psa_driver_wrapper_cipher_update(
Ronald Cron6056fe82020-12-15 13:58:07 +01001176 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001177 const uint8_t *input,
1178 size_t input_length,
1179 uint8_t *output,
1180 size_t output_size,
1181 size_t *output_length )
1182{
Ronald Cron49fafa92021-03-10 08:34:23 +01001183 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001184 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001185#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001186 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001187 return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001188 input,
1189 input_length,
1190 output,
1191 output_size,
1192 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001193#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1194
Ronald Cron49fafa92021-03-10 08:34:23 +01001195#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001196#if defined(PSA_CRYPTO_DRIVER_TEST)
1197 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001198 return( mbedtls_test_transparent_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001199 &operation->ctx.transparent_test_driver_ctx,
1200 input, input_length,
1201 output, output_size, output_length ) );
1202
Steven Cooreman37941cb2020-07-28 18:49:51 +02001203 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001204 return( mbedtls_test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001205 &operation->ctx.opaque_test_driver_ctx,
1206 input, input_length,
1207 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001208#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001209#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001210 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001211
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001212 (void)input;
1213 (void)input_length;
1214 (void)output;
1215 (void)output_size;
1216 (void)output_length;
1217
Ronald Crondd24c9b2020-12-15 14:10:01 +01001218 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001219}
1220
1221psa_status_t psa_driver_wrapper_cipher_finish(
Ronald Cron6056fe82020-12-15 13:58:07 +01001222 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001223 uint8_t *output,
1224 size_t output_size,
1225 size_t *output_length )
1226{
Ronald Cron49fafa92021-03-10 08:34:23 +01001227 switch( operation->id )
1228 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001229#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001230 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001231 return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001232 output,
1233 output_size,
1234 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001235#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001236
1237#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001238#if defined(PSA_CRYPTO_DRIVER_TEST)
1239 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001240 return( mbedtls_test_transparent_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001241 &operation->ctx.transparent_test_driver_ctx,
1242 output, output_size, output_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001243
Steven Cooreman37941cb2020-07-28 18:49:51 +02001244 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001245 return( mbedtls_test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001246 &operation->ctx.opaque_test_driver_ctx,
1247 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001248#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001249#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001250 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001251
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001252 (void)output;
1253 (void)output_size;
1254 (void)output_length;
1255
Ronald Crondd24c9b2020-12-15 14:10:01 +01001256 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001257}
1258
1259psa_status_t psa_driver_wrapper_cipher_abort(
Ronald Cron6056fe82020-12-15 13:58:07 +01001260 psa_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001261{
Ronald Crondd24c9b2020-12-15 14:10:01 +01001262 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001263
Ronald Cron49fafa92021-03-10 08:34:23 +01001264 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001265 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001266#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001267 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001268 return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001269#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +01001270
1271#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001272#if defined(PSA_CRYPTO_DRIVER_TEST)
1273 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001274 status = mbedtls_test_transparent_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001275 &operation->ctx.transparent_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001276 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001277 &operation->ctx.transparent_test_driver_ctx,
1278 sizeof( operation->ctx.transparent_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001279 return( status );
Ronald Cron49fafa92021-03-10 08:34:23 +01001280
Steven Cooreman37941cb2020-07-28 18:49:51 +02001281 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001282 status = mbedtls_test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001283 &operation->ctx.opaque_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001284 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001285 &operation->ctx.opaque_test_driver_ctx,
1286 sizeof( operation->ctx.opaque_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001287 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001288#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001289#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001290 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001291
Ronald Cron49fafa92021-03-10 08:34:23 +01001292 (void)status;
Ronald Crondd24c9b2020-12-15 14:10:01 +01001293 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001294}
1295
Steven Cooreman1e582352021-02-18 17:24:37 +01001296/*
1297 * Hashing functions
1298 */
1299psa_status_t psa_driver_wrapper_hash_compute(
1300 psa_algorithm_t alg,
1301 const uint8_t *input,
1302 size_t input_length,
1303 uint8_t *hash,
1304 size_t hash_size,
1305 size_t *hash_length)
1306{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001307 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001308
1309 /* Try accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001310#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001311 status = mbedtls_test_transparent_hash_compute(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001312 alg, input, input_length, hash, hash_size, hash_length );
Steven Cooremanf7638102021-03-04 15:14:36 +01001313 if( status != PSA_ERROR_NOT_SUPPORTED )
1314 return( status );
1315#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001316
1317 /* If software fallback is compiled in, try fallback */
1318#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1319 status = mbedtls_psa_hash_compute( alg, input, input_length,
1320 hash, hash_size, hash_length );
1321 if( status != PSA_ERROR_NOT_SUPPORTED )
1322 return( status );
1323#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001324 (void) status;
1325 (void) alg;
1326 (void) input;
1327 (void) input_length;
1328 (void) hash;
1329 (void) hash_size;
1330 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001331
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001332 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman1e582352021-02-18 17:24:37 +01001333}
1334
1335psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001336 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001337 psa_algorithm_t alg )
1338{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001339 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001340
Steven Cooreman1e582352021-02-18 17:24:37 +01001341 /* Try setup on accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001342#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001343 status = mbedtls_test_transparent_hash_setup(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001344 &operation->ctx.test_driver_ctx, alg );
Steven Cooremanf7638102021-03-04 15:14:36 +01001345 if( status == PSA_SUCCESS )
1346 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1347
1348 if( status != PSA_ERROR_NOT_SUPPORTED )
1349 return( status );
1350#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001351
1352 /* If software fallback is compiled in, try fallback */
1353#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001354 status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
Steven Cooreman1e582352021-02-18 17:24:37 +01001355 if( status == PSA_SUCCESS )
Steven Cooreman1e582352021-02-18 17:24:37 +01001356 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Steven Cooreman1e582352021-02-18 17:24:37 +01001357
1358 if( status != PSA_ERROR_NOT_SUPPORTED )
1359 return( status );
1360#endif
1361 /* Nothing left to try if we fall through here */
1362 (void) status;
1363 (void) operation;
1364 (void) alg;
1365 return( PSA_ERROR_NOT_SUPPORTED );
1366}
1367
1368psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001369 const psa_hash_operation_t *source_operation,
1370 psa_hash_operation_t *target_operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001371{
Steven Cooreman1e582352021-02-18 17:24:37 +01001372 switch( source_operation->id )
1373 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001374#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1375 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1376 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1377 return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
1378 &target_operation->ctx.mbedtls_ctx ) );
1379#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001380#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001381 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1382 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Ronald Cron7f13fa22021-04-13 12:41:34 +02001383 return( mbedtls_test_transparent_hash_clone(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001384 &source_operation->ctx.test_driver_ctx,
1385 &target_operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001386#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001387 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001388 (void) target_operation;
1389 return( PSA_ERROR_BAD_STATE );
1390 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001391}
1392
1393psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001394 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001395 const uint8_t *input,
1396 size_t input_length )
1397{
Steven Cooreman1e582352021-02-18 17:24:37 +01001398 switch( operation->id )
1399 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001400#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1401 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1402 return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
1403 input, input_length ) );
1404#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001405#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001406 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001407 return( mbedtls_test_transparent_hash_update(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001408 &operation->ctx.test_driver_ctx,
1409 input, input_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001410#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001411 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001412 (void) input;
1413 (void) input_length;
1414 return( PSA_ERROR_BAD_STATE );
1415 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001416}
1417
1418psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001419 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001420 uint8_t *hash,
1421 size_t hash_size,
1422 size_t *hash_length )
1423{
Steven Cooreman1e582352021-02-18 17:24:37 +01001424 switch( operation->id )
1425 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001426#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1427 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1428 return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
1429 hash, hash_size, hash_length ) );
1430#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001431#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001432 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001433 return( mbedtls_test_transparent_hash_finish(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001434 &operation->ctx.test_driver_ctx,
1435 hash, hash_size, hash_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001436#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001437 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001438 (void) hash;
1439 (void) hash_size;
1440 (void) hash_length;
1441 return( PSA_ERROR_BAD_STATE );
1442 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001443}
1444
1445psa_status_t psa_driver_wrapper_hash_abort(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001446 psa_hash_operation_t *operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001447{
Steven Cooreman1e582352021-02-18 17:24:37 +01001448 switch( operation->id )
1449 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001450#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1451 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1452 return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
1453#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001454#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001455 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001456 return( mbedtls_test_transparent_hash_abort(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001457 &operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001458#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001459 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001460 return( PSA_ERROR_BAD_STATE );
1461 }
1462}
1463
Ronald Cronde822812021-03-17 16:08:20 +01001464psa_status_t psa_driver_wrapper_aead_encrypt(
1465 const psa_key_attributes_t *attributes,
1466 const uint8_t *key_buffer, size_t key_buffer_size,
1467 psa_algorithm_t alg,
1468 const uint8_t *nonce, size_t nonce_length,
1469 const uint8_t *additional_data, size_t additional_data_length,
1470 const uint8_t *plaintext, size_t plaintext_length,
1471 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
1472{
1473 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1474 psa_key_location_t location =
1475 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1476
1477 switch( location )
1478 {
1479 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1480 /* Key is stored in the slot in export representation, so
1481 * cycle through all known transparent accelerators */
1482
1483#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1484#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001485 status = mbedtls_test_transparent_aead_encrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001486 attributes, key_buffer, key_buffer_size,
1487 alg,
1488 nonce, nonce_length,
1489 additional_data, additional_data_length,
1490 plaintext, plaintext_length,
1491 ciphertext, ciphertext_size, ciphertext_length );
1492 /* Declared with fallback == true */
1493 if( status != PSA_ERROR_NOT_SUPPORTED )
1494 return( status );
1495#endif /* PSA_CRYPTO_DRIVER_TEST */
1496#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1497
1498 /* Fell through, meaning no accelerator supports this operation */
1499 return( mbedtls_psa_aead_encrypt(
1500 attributes, key_buffer, key_buffer_size,
1501 alg,
1502 nonce, nonce_length,
1503 additional_data, additional_data_length,
1504 plaintext, plaintext_length,
1505 ciphertext, ciphertext_size, ciphertext_length ) );
1506
1507 /* Add cases for opaque driver here */
1508
1509 default:
1510 /* Key is declared with a lifetime not known to us */
1511 (void)status;
1512 return( PSA_ERROR_INVALID_ARGUMENT );
1513 }
1514}
1515
1516psa_status_t psa_driver_wrapper_aead_decrypt(
1517 const psa_key_attributes_t *attributes,
1518 const uint8_t *key_buffer, size_t key_buffer_size,
1519 psa_algorithm_t alg,
1520 const uint8_t *nonce, size_t nonce_length,
1521 const uint8_t *additional_data, size_t additional_data_length,
1522 const uint8_t *ciphertext, size_t ciphertext_length,
1523 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
1524{
1525 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1526 psa_key_location_t location =
1527 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1528
1529 switch( location )
1530 {
1531 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1532 /* Key is stored in the slot in export representation, so
1533 * cycle through all known transparent accelerators */
1534
1535#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1536#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001537 status = mbedtls_test_transparent_aead_decrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001538 attributes, key_buffer, key_buffer_size,
1539 alg,
1540 nonce, nonce_length,
1541 additional_data, additional_data_length,
1542 ciphertext, ciphertext_length,
1543 plaintext, plaintext_size, plaintext_length );
1544 /* Declared with fallback == true */
1545 if( status != PSA_ERROR_NOT_SUPPORTED )
1546 return( status );
1547#endif /* PSA_CRYPTO_DRIVER_TEST */
1548#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1549
1550 /* Fell through, meaning no accelerator supports this operation */
1551 return( mbedtls_psa_aead_decrypt(
1552 attributes, key_buffer, key_buffer_size,
1553 alg,
1554 nonce, nonce_length,
1555 additional_data, additional_data_length,
1556 ciphertext, ciphertext_length,
1557 plaintext, plaintext_size, plaintext_length ) );
1558
1559 /* Add cases for opaque driver here */
1560
1561 default:
1562 /* Key is declared with a lifetime not known to us */
1563 (void)status;
1564 return( PSA_ERROR_INVALID_ARGUMENT );
1565 }
1566}
Steven Cooremand13a70f2021-03-19 15:24:23 +01001567
Paul Elliott6504aa62021-04-20 17:09:36 +01001568psa_status_t psa_driver_wrapper_aead_encrypt_setup(
1569 psa_aead_operation_t *operation,
1570 const psa_key_attributes_t *attributes,
1571 const uint8_t *key_buffer, size_t key_buffer_size,
1572 psa_algorithm_t alg )
1573{
1574 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1575 psa_key_location_t location =
1576 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1577
1578 switch( location )
1579 {
1580 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1581 /* Key is stored in the slot in export representation, so
1582 * cycle through all known transparent accelerators */
1583
1584#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1585#if defined(PSA_CRYPTO_DRIVER_TEST)
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001586 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Paul Elliotta218ceb2021-05-07 15:10:31 +01001587 status = mbedtls_test_transparent_aead_encrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001588 &operation->ctx.transparent_test_driver_ctx,
1589 attributes, key_buffer, key_buffer_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001590 alg );
Paul Elliott6504aa62021-04-20 17:09:36 +01001591
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001592 /* Declared with fallback == true */
Paul Elliott6504aa62021-04-20 17:09:36 +01001593 if( status != PSA_ERROR_NOT_SUPPORTED )
1594 return( status );
1595#endif /* PSA_CRYPTO_DRIVER_TEST */
1596#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1597
1598 /* Fell through, meaning no accelerator supports this operation */
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001599 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Paul Elliott6504aa62021-04-20 17:09:36 +01001600 status = mbedtls_psa_aead_encrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001601 &operation->ctx.mbedtls_ctx, attributes,
Paul Elliott6504aa62021-04-20 17:09:36 +01001602 key_buffer, key_buffer_size,
1603 alg );
1604
Paul Elliott6504aa62021-04-20 17:09:36 +01001605 return( status );
1606
1607 /* Add cases for opaque driver here */
1608
1609 default:
1610 /* Key is declared with a lifetime not known to us */
1611 (void)status;
1612 return( PSA_ERROR_INVALID_ARGUMENT );
1613 }
1614}
1615
1616psa_status_t psa_driver_wrapper_aead_decrypt_setup(
1617 psa_aead_operation_t *operation,
1618 const psa_key_attributes_t *attributes,
1619 const uint8_t *key_buffer, size_t key_buffer_size,
1620 psa_algorithm_t alg )
1621{
1622 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1623 psa_key_location_t location =
1624 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1625
1626 switch( location )
1627 {
1628 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1629 /* Key is stored in the slot in export representation, so
1630 * cycle through all known transparent accelerators */
1631
1632#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1633#if defined(PSA_CRYPTO_DRIVER_TEST)
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001634 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Paul Elliotta218ceb2021-05-07 15:10:31 +01001635 status = mbedtls_test_transparent_aead_decrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001636 &operation->ctx.transparent_test_driver_ctx,
Paul Elliott5d3a3c32021-08-19 18:34:41 +01001637 attributes,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001638 key_buffer, key_buffer_size,
1639 alg );
Paul Elliott6504aa62021-04-20 17:09:36 +01001640
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001641 /* Declared with fallback == true */
Paul Elliott6504aa62021-04-20 17:09:36 +01001642 if( status != PSA_ERROR_NOT_SUPPORTED )
1643 return( status );
1644#endif /* PSA_CRYPTO_DRIVER_TEST */
1645#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1646
1647 /* Fell through, meaning no accelerator supports this operation */
Paul Elliottd7ab9f12021-06-23 09:52:19 +01001648 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Paul Elliott6504aa62021-04-20 17:09:36 +01001649 status = mbedtls_psa_aead_decrypt_setup(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001650 &operation->ctx.mbedtls_ctx,
Paul Elliott5d3a3c32021-08-19 18:34:41 +01001651 attributes,
Paul Elliott6504aa62021-04-20 17:09:36 +01001652 key_buffer, key_buffer_size,
1653 alg );
1654
Paul Elliott6504aa62021-04-20 17:09:36 +01001655 return( status );
1656
1657 /* Add cases for opaque driver here */
1658
1659 default:
1660 /* Key is declared with a lifetime not known to us */
1661 (void)status;
1662 return( PSA_ERROR_INVALID_ARGUMENT );
1663 }
1664}
1665
Paul Elliott6504aa62021-04-20 17:09:36 +01001666psa_status_t psa_driver_wrapper_aead_set_nonce(
1667 psa_aead_operation_t *operation,
1668 const uint8_t *nonce,
1669 size_t nonce_length )
1670{
1671 switch( operation->id )
1672 {
Paul Elliott5c656cb2021-05-19 14:15:01 +01001673#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001674 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001675 return( mbedtls_psa_aead_set_nonce( &operation->ctx.mbedtls_ctx,
1676 nonce,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001677 nonce_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001678
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001679#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001680
1681#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1682#if defined(PSA_CRYPTO_DRIVER_TEST)
1683 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001684 return( mbedtls_test_transparent_aead_set_nonce(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001685 &operation->ctx.transparent_test_driver_ctx,
1686 nonce, nonce_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001687
1688 /* Add cases for opaque driver here */
1689
1690#endif /* PSA_CRYPTO_DRIVER_TEST */
1691#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1692 }
1693
1694 (void)nonce;
1695 (void)nonce_length;
1696
1697 return( PSA_ERROR_INVALID_ARGUMENT );
1698}
1699
1700psa_status_t psa_driver_wrapper_aead_set_lengths(
1701 psa_aead_operation_t *operation,
1702 size_t ad_length,
1703 size_t plaintext_length )
1704{
1705 switch( operation->id )
1706 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001707#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001708 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliott325d3742021-09-27 17:56:28 +01001709 /* No mbedtls_psa_aead_set_lengths, everything is done in PSA
1710 * Core. */
1711 return( PSA_SUCCESS );
Paul Elliott6504aa62021-04-20 17:09:36 +01001712
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001713#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001714
1715#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1716#if defined(PSA_CRYPTO_DRIVER_TEST)
1717 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001718 return( mbedtls_test_transparent_aead_set_lengths(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001719 &operation->ctx.transparent_test_driver_ctx,
1720 ad_length, plaintext_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001721
1722 /* Add cases for opaque driver here */
1723
1724#endif /* PSA_CRYPTO_DRIVER_TEST */
1725#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1726 }
1727
1728 (void)ad_length;
1729 (void)plaintext_length;
1730
1731 return( PSA_ERROR_INVALID_ARGUMENT );
1732}
1733
1734psa_status_t psa_driver_wrapper_aead_update_ad(
1735 psa_aead_operation_t *operation,
1736 const uint8_t *input,
1737 size_t input_length )
1738{
1739 switch( operation->id )
1740 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001741#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001742 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001743 return( mbedtls_psa_aead_update_ad( &operation->ctx.mbedtls_ctx,
1744 input,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001745 input_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001746
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001747#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001748
1749#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1750#if defined(PSA_CRYPTO_DRIVER_TEST)
1751 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001752 return( mbedtls_test_transparent_aead_update_ad(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001753 &operation->ctx.transparent_test_driver_ctx,
1754 input, input_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001755
1756 /* Add cases for opaque driver here */
1757
1758#endif /* PSA_CRYPTO_DRIVER_TEST */
1759#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1760 }
1761
1762 (void)input;
1763 (void)input_length;
1764
1765 return( PSA_ERROR_INVALID_ARGUMENT );
1766}
1767
1768psa_status_t psa_driver_wrapper_aead_update(
1769 psa_aead_operation_t *operation,
1770 const uint8_t *input,
1771 size_t input_length,
1772 uint8_t *output,
1773 size_t output_size,
1774 size_t *output_length )
1775{
1776 switch( operation->id )
1777 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001778#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001779 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001780 return( mbedtls_psa_aead_update( &operation->ctx.mbedtls_ctx,
1781 input, input_length,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001782 output, output_size,
1783 output_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001784
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001785#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001786
1787#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1788#if defined(PSA_CRYPTO_DRIVER_TEST)
1789 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001790 return( mbedtls_test_transparent_aead_update(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001791 &operation->ctx.transparent_test_driver_ctx,
1792 input, input_length, output, output_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001793 output_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001794
1795 /* Add cases for opaque driver here */
1796
1797#endif /* PSA_CRYPTO_DRIVER_TEST */
1798#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1799 }
1800
1801 (void)input;
1802 (void)input_length;
1803 (void)output;
1804 (void)output_size;
1805 (void)output_length;
1806
1807 return( PSA_ERROR_INVALID_ARGUMENT );
1808}
1809
1810psa_status_t psa_driver_wrapper_aead_finish(
1811 psa_aead_operation_t *operation,
1812 uint8_t *ciphertext,
1813 size_t ciphertext_size,
1814 size_t *ciphertext_length,
1815 uint8_t *tag,
1816 size_t tag_size,
1817 size_t *tag_length )
1818{
1819 switch( operation->id )
1820 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001821#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001822 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001823 return( mbedtls_psa_aead_finish( &operation->ctx.mbedtls_ctx,
1824 ciphertext,
Paul Elliotta218ceb2021-05-07 15:10:31 +01001825 ciphertext_size,
1826 ciphertext_length, tag,
1827 tag_size, tag_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001828
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001829#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001830
1831#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1832#if defined(PSA_CRYPTO_DRIVER_TEST)
1833 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001834 return( mbedtls_test_transparent_aead_finish(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001835 &operation->ctx.transparent_test_driver_ctx,
1836 ciphertext, ciphertext_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001837 ciphertext_length, tag, tag_size, tag_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001838
1839 /* Add cases for opaque driver here */
1840
1841#endif /* PSA_CRYPTO_DRIVER_TEST */
1842#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1843 }
1844
1845 (void)ciphertext;
1846 (void)ciphertext_size;
1847 (void)ciphertext_length;
1848 (void)tag;
1849 (void)tag_size;
1850 (void)tag_length;
1851
1852 return( PSA_ERROR_INVALID_ARGUMENT );
1853}
1854
1855psa_status_t psa_driver_wrapper_aead_verify(
1856 psa_aead_operation_t *operation,
1857 uint8_t *plaintext,
1858 size_t plaintext_size,
1859 size_t *plaintext_length,
1860 const uint8_t *tag,
1861 size_t tag_length )
1862{
1863 switch( operation->id )
1864 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001865#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001866 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliott315628d2021-07-20 18:25:54 +01001867 {
1868 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1869 uint8_t check_tag[PSA_AEAD_TAG_MAX_SIZE];
1870 size_t check_tag_length;
1871
1872 status = mbedtls_psa_aead_finish( &operation->ctx.mbedtls_ctx,
1873 plaintext,
1874 plaintext_size,
1875 plaintext_length,
1876 check_tag,
Paul Elliottb183d562021-09-13 19:02:57 +01001877 sizeof( check_tag ),
Paul Elliott315628d2021-07-20 18:25:54 +01001878 &check_tag_length );
1879
1880 if( status == PSA_SUCCESS )
1881 {
1882 if( tag_length != check_tag_length ||
1883 mbedtls_psa_safer_memcmp( tag, check_tag, tag_length )
1884 != 0 )
1885 status = PSA_ERROR_INVALID_SIGNATURE;
1886 }
1887
Paul Elliott06b6b8c2021-09-13 19:02:04 +01001888 mbedtls_platform_zeroize( check_tag, sizeof( check_tag ) );
1889
Paul Elliott315628d2021-07-20 18:25:54 +01001890 return( status );
1891 }
Paul Elliott6504aa62021-04-20 17:09:36 +01001892
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001893#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001894
1895#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1896#if defined(PSA_CRYPTO_DRIVER_TEST)
1897 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliotta218ceb2021-05-07 15:10:31 +01001898 return( mbedtls_test_transparent_aead_verify(
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001899 &operation->ctx.transparent_test_driver_ctx,
1900 plaintext, plaintext_size,
Paul Elliott4bbe82b2021-04-27 12:11:56 +01001901 plaintext_length, tag, tag_length ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001902
1903 /* Add cases for opaque driver here */
1904
1905#endif /* PSA_CRYPTO_DRIVER_TEST */
1906#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1907 }
1908
1909 (void)plaintext;
1910 (void)plaintext_size;
1911 (void)plaintext_length;
1912 (void)tag;
1913 (void)tag_length;
1914
1915 return( PSA_ERROR_INVALID_ARGUMENT );
1916}
1917
1918psa_status_t psa_driver_wrapper_aead_abort(
1919 psa_aead_operation_t *operation )
1920{
1921 switch( operation->id )
1922 {
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001923#if defined(MBEDTLS_PSA_BUILTIN_AEAD)
Paul Elliott6504aa62021-04-20 17:09:36 +01001924 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001925 return( mbedtls_psa_aead_abort( &operation->ctx.mbedtls_ctx ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001926
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001927#endif /* MBEDTLS_PSA_BUILTIN_AEAD */
Paul Elliott6504aa62021-04-20 17:09:36 +01001928
1929#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1930#if defined(PSA_CRYPTO_DRIVER_TEST)
1931 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Paul Elliottcbbde5f2021-05-10 18:19:46 +01001932 return( mbedtls_test_transparent_aead_abort(
1933 &operation->ctx.transparent_test_driver_ctx ) );
Paul Elliott6504aa62021-04-20 17:09:36 +01001934
1935 /* Add cases for opaque driver here */
1936
1937#endif /* PSA_CRYPTO_DRIVER_TEST */
1938#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1939 }
1940
1941 return( PSA_ERROR_INVALID_ARGUMENT );
1942}
Steven Cooremand13a70f2021-03-19 15:24:23 +01001943
1944/*
1945 * MAC functions
1946 */
1947psa_status_t psa_driver_wrapper_mac_compute(
1948 const psa_key_attributes_t *attributes,
1949 const uint8_t *key_buffer,
1950 size_t key_buffer_size,
1951 psa_algorithm_t alg,
1952 const uint8_t *input,
1953 size_t input_length,
1954 uint8_t *mac,
1955 size_t mac_size,
1956 size_t *mac_length )
1957{
1958 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1959 psa_key_location_t location =
1960 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1961
1962 switch( location )
1963 {
1964 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1965 /* Key is stored in the slot in export representation, so
1966 * cycle through all known transparent accelerators */
1967#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1968#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001969 status = mbedtls_test_transparent_mac_compute(
Steven Cooremand13a70f2021-03-19 15:24:23 +01001970 attributes, key_buffer, key_buffer_size, alg,
1971 input, input_length,
1972 mac, mac_size, mac_length );
1973 /* Declared with fallback == true */
1974 if( status != PSA_ERROR_NOT_SUPPORTED )
1975 return( status );
1976#endif /* PSA_CRYPTO_DRIVER_TEST */
1977#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1978#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1979 /* Fell through, meaning no accelerator supports this operation */
1980 status = mbedtls_psa_mac_compute(
1981 attributes, key_buffer, key_buffer_size, alg,
1982 input, input_length,
1983 mac, mac_size, mac_length );
1984 if( status != PSA_ERROR_NOT_SUPPORTED )
1985 return( status );
1986#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1987 return( PSA_ERROR_NOT_SUPPORTED );
1988
1989 /* Add cases for opaque driver here */
1990#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1991#if defined(PSA_CRYPTO_DRIVER_TEST)
1992 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001993 status = mbedtls_test_opaque_mac_compute(
Steven Cooremand13a70f2021-03-19 15:24:23 +01001994 attributes, key_buffer, key_buffer_size, alg,
1995 input, input_length,
1996 mac, mac_size, mac_length );
1997 return( status );
1998#endif /* PSA_CRYPTO_DRIVER_TEST */
1999#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2000 default:
2001 /* Key is declared with a lifetime not known to us */
2002 (void) key_buffer;
2003 (void) key_buffer_size;
2004 (void) alg;
2005 (void) input;
2006 (void) input_length;
2007 (void) mac;
2008 (void) mac_size;
2009 (void) mac_length;
2010 (void) status;
2011 return( PSA_ERROR_INVALID_ARGUMENT );
2012 }
2013}
2014
2015psa_status_t psa_driver_wrapper_mac_sign_setup(
2016 psa_mac_operation_t *operation,
2017 const psa_key_attributes_t *attributes,
2018 const uint8_t *key_buffer,
2019 size_t key_buffer_size,
2020 psa_algorithm_t alg )
2021{
2022 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2023 psa_key_location_t location =
2024 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
2025
2026 switch( location )
2027 {
2028 case PSA_KEY_LOCATION_LOCAL_STORAGE:
2029 /* Key is stored in the slot in export representation, so
2030 * cycle through all known transparent accelerators */
2031#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2032#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002033 status = mbedtls_test_transparent_mac_sign_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002034 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002035 attributes,
2036 key_buffer, key_buffer_size,
2037 alg );
2038 /* Declared with fallback == true */
2039 if( status == PSA_SUCCESS )
2040 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
2041
2042 if( status != PSA_ERROR_NOT_SUPPORTED )
2043 return( status );
2044#endif /* PSA_CRYPTO_DRIVER_TEST */
2045#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2046#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2047 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002048 status = mbedtls_psa_mac_sign_setup( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002049 attributes,
2050 key_buffer, key_buffer_size,
2051 alg );
2052 if( status == PSA_SUCCESS )
2053 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
2054
2055 if( status != PSA_ERROR_NOT_SUPPORTED )
2056 return( status );
2057#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2058 return( PSA_ERROR_NOT_SUPPORTED );
2059
2060 /* Add cases for opaque driver here */
2061#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2062#if defined(PSA_CRYPTO_DRIVER_TEST)
2063 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002064 status = mbedtls_test_opaque_mac_sign_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002065 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002066 attributes,
2067 key_buffer, key_buffer_size,
2068 alg );
2069
2070 if( status == PSA_SUCCESS )
2071 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
2072
2073 return( status );
2074#endif /* PSA_CRYPTO_DRIVER_TEST */
2075#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2076 default:
2077 /* Key is declared with a lifetime not known to us */
2078 (void) status;
2079 (void) key_buffer;
2080 (void) key_buffer_size;
2081 (void) alg;
2082 return( PSA_ERROR_INVALID_ARGUMENT );
2083 }
2084}
2085
2086psa_status_t psa_driver_wrapper_mac_verify_setup(
2087 psa_mac_operation_t *operation,
2088 const psa_key_attributes_t *attributes,
2089 const uint8_t *key_buffer,
2090 size_t key_buffer_size,
2091 psa_algorithm_t alg )
2092{
2093 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2094 psa_key_location_t location =
2095 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
2096
2097 switch( location )
2098 {
2099 case PSA_KEY_LOCATION_LOCAL_STORAGE:
2100 /* Key is stored in the slot in export representation, so
2101 * cycle through all known transparent accelerators */
2102#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2103#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002104 status = mbedtls_test_transparent_mac_verify_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002105 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002106 attributes,
2107 key_buffer, key_buffer_size,
2108 alg );
2109 /* Declared with fallback == true */
2110 if( status == PSA_SUCCESS )
2111 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
2112
2113 if( status != PSA_ERROR_NOT_SUPPORTED )
2114 return( status );
2115#endif /* PSA_CRYPTO_DRIVER_TEST */
2116#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2117#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2118 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002119 status = mbedtls_psa_mac_verify_setup( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002120 attributes,
2121 key_buffer, key_buffer_size,
2122 alg );
2123 if( status == PSA_SUCCESS )
2124 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
2125
2126 if( status != PSA_ERROR_NOT_SUPPORTED )
2127 return( status );
2128#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2129 return( PSA_ERROR_NOT_SUPPORTED );
2130
2131 /* Add cases for opaque driver here */
2132#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2133#if defined(PSA_CRYPTO_DRIVER_TEST)
2134 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002135 status = mbedtls_test_opaque_mac_verify_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002136 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002137 attributes,
2138 key_buffer, key_buffer_size,
2139 alg );
2140
2141 if( status == PSA_SUCCESS )
2142 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
2143
2144 return( status );
2145#endif /* PSA_CRYPTO_DRIVER_TEST */
2146#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2147 default:
2148 /* Key is declared with a lifetime not known to us */
2149 (void) status;
2150 (void) key_buffer;
2151 (void) key_buffer_size;
2152 (void) alg;
2153 return( PSA_ERROR_INVALID_ARGUMENT );
2154 }
2155}
2156
2157psa_status_t psa_driver_wrapper_mac_update(
2158 psa_mac_operation_t *operation,
2159 const uint8_t *input,
2160 size_t input_length )
2161{
2162 switch( operation->id )
2163 {
2164#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2165 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002166 return( mbedtls_psa_mac_update( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002167 input, input_length ) );
2168#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2169
2170#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2171#if defined(PSA_CRYPTO_DRIVER_TEST)
2172 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002173 return( mbedtls_test_transparent_mac_update(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002174 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002175 input, input_length ) );
2176
2177 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002178 return( mbedtls_test_opaque_mac_update(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002179 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002180 input, input_length ) );
2181#endif /* PSA_CRYPTO_DRIVER_TEST */
2182#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2183 default:
2184 (void) input;
2185 (void) input_length;
2186 return( PSA_ERROR_INVALID_ARGUMENT );
2187 }
2188}
2189
2190psa_status_t psa_driver_wrapper_mac_sign_finish(
2191 psa_mac_operation_t *operation,
2192 uint8_t *mac,
2193 size_t mac_size,
2194 size_t *mac_length )
2195{
2196 switch( operation->id )
2197 {
2198#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2199 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002200 return( mbedtls_psa_mac_sign_finish( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002201 mac, mac_size, mac_length ) );
2202#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2203
2204#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2205#if defined(PSA_CRYPTO_DRIVER_TEST)
2206 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002207 return( mbedtls_test_transparent_mac_sign_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002208 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002209 mac, mac_size, mac_length ) );
2210
2211 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002212 return( mbedtls_test_opaque_mac_sign_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002213 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002214 mac, mac_size, mac_length ) );
2215#endif /* PSA_CRYPTO_DRIVER_TEST */
2216#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2217 default:
2218 (void) mac;
2219 (void) mac_size;
2220 (void) mac_length;
2221 return( PSA_ERROR_INVALID_ARGUMENT );
2222 }
2223}
2224
2225psa_status_t psa_driver_wrapper_mac_verify_finish(
2226 psa_mac_operation_t *operation,
2227 const uint8_t *mac,
2228 size_t mac_length )
2229{
2230 switch( operation->id )
2231 {
2232#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2233 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002234 return( mbedtls_psa_mac_verify_finish( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002235 mac, mac_length ) );
2236#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2237
2238#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2239#if defined(PSA_CRYPTO_DRIVER_TEST)
2240 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002241 return( mbedtls_test_transparent_mac_verify_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002242 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002243 mac, mac_length ) );
2244
2245 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002246 return( mbedtls_test_opaque_mac_verify_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01002247 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01002248 mac, mac_length ) );
2249#endif /* PSA_CRYPTO_DRIVER_TEST */
2250#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2251 default:
2252 (void) mac;
2253 (void) mac_length;
2254 return( PSA_ERROR_INVALID_ARGUMENT );
2255 }
2256}
2257
2258psa_status_t psa_driver_wrapper_mac_abort(
2259 psa_mac_operation_t *operation )
2260{
Steven Cooremand13a70f2021-03-19 15:24:23 +01002261 switch( operation->id )
2262 {
2263#if defined(MBEDTLS_PSA_BUILTIN_MAC)
2264 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooremane6804192021-03-19 18:28:56 +01002265 return( mbedtls_psa_mac_abort( &operation->ctx.mbedtls_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002266#endif /* MBEDTLS_PSA_BUILTIN_MAC */
2267
2268#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
2269#if defined(PSA_CRYPTO_DRIVER_TEST)
2270 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002271 return( mbedtls_test_transparent_mac_abort(
Steven Cooremane6804192021-03-19 18:28:56 +01002272 &operation->ctx.transparent_test_driver_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002273 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02002274 return( mbedtls_test_opaque_mac_abort(
Steven Cooremane6804192021-03-19 18:28:56 +01002275 &operation->ctx.opaque_test_driver_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002276#endif /* PSA_CRYPTO_DRIVER_TEST */
2277#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
2278 default:
Steven Cooremane6804192021-03-19 18:28:56 +01002279 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand13a70f2021-03-19 15:24:23 +01002280 }
Steven Cooremand13a70f2021-03-19 15:24:23 +01002281}
Gilles Peskine1905a242021-04-24 13:19:45 +02002282
2283#endif /* MBEDTLS_PSA_CRYPTO_C */