blob: 6bf08d9656ec50cde52909aa8cf220eb97dacbc4 [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
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200383/** Get the key buffer size required to store the key material of a key
384 * associated with an opaque driver without storage.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200385 *
Ronald Cron31216282020-12-05 18:47:56 +0100386 * \param[in] attributes The key attributes.
387 * \param[out] key_buffer_size Minimum buffer size to contain the key material
Steven Cooreman56250fd2020-09-04 13:07:15 +0200388 *
389 * \retval #PSA_SUCCESS
Ronald Cron31216282020-12-05 18:47:56 +0100390 * The minimum size for a buffer to contain the key material has been
391 * returned successfully.
392 * \retval #PSA_ERROR_INVALID_ARGUMENT
393 * The size in bits of the key is not valid.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200394 * \retval #PSA_ERROR_NOT_SUPPORTED
Ronald Cron31216282020-12-05 18:47:56 +0100395 * The type and/or the size in bits of the key or the combination of
396 * the two is not supported.
Steven Cooreman56250fd2020-09-04 13:07:15 +0200397 */
Ronald Cron9df74be2020-12-05 19:15:23 +0100398psa_status_t psa_driver_wrapper_get_key_buffer_size(
Ronald Cron31216282020-12-05 18:47:56 +0100399 const psa_key_attributes_t *attributes,
400 size_t *key_buffer_size )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200401{
John Durkop2c618352020-09-22 06:54:01 -0700402 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
403 psa_key_type_t key_type = attributes->core.type;
404 size_t key_bits = attributes->core.bits;
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200405
Ronald Cron31216282020-12-05 18:47:56 +0100406 *key_buffer_size = 0;
John Durkop2c618352020-09-22 06:54:01 -0700407 switch( location )
408 {
John Durkop2c618352020-09-22 06:54:01 -0700409#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100410 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100411#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
412 /* Emulate property 'builtin_key_size' */
413 if( psa_key_id_is_builtin(
414 MBEDTLS_SVC_KEY_ID_GET_KEY_ID(
415 psa_get_key_id( attributes ) ) ) )
416 {
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100417 *key_buffer_size = sizeof( psa_drv_slot_number_t );
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100418 return( PSA_SUCCESS );
419 }
420#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
Ronald Cron7f13fa22021-04-13 12:41:34 +0200421 *key_buffer_size = mbedtls_test_size_function( key_type, key_bits );
Ronald Cron4607c822021-04-14 10:55:34 +0200422 return( ( *key_buffer_size != 0 ) ?
423 PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
John Durkop2c618352020-09-22 06:54:01 -0700424#endif /* PSA_CRYPTO_DRIVER_TEST */
425
426 default:
Ronald Cron9df74be2020-12-05 19:15:23 +0100427 (void)key_type;
428 (void)key_bits;
Steven Cooreman56250fd2020-09-04 13:07:15 +0200429 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200430 }
431}
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200432
Ronald Cron977c2472020-10-13 08:32:21 +0200433psa_status_t psa_driver_wrapper_generate_key(
434 const psa_key_attributes_t *attributes,
435 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Steven Cooreman55ae2172020-07-17 19:46:15 +0200436{
Ronald Cron977c2472020-10-13 08:32:21 +0200437 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
438 psa_key_location_t location =
439 PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime);
440
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200441 /* Try dynamically-registered SE interface first */
442#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
443 const psa_drv_se_t *drv;
444 psa_drv_se_context_t *drv_context;
445
Ronald Cron977c2472020-10-13 08:32:21 +0200446 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200447 {
448 size_t pubkey_length = 0; /* We don't support this feature yet */
449 if( drv->key_management == NULL ||
450 drv->key_management->p_generate == NULL )
451 {
452 /* Key is defined as being in SE, but we have no way to generate it */
Steven Cooreman56250fd2020-09-04 13:07:15 +0200453 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200454 }
455 return( drv->key_management->p_generate(
Ronald Cron977c2472020-10-13 08:32:21 +0200456 drv_context,
457 *( (psa_key_slot_number_t *)key_buffer ),
Ronald Cronea0f8a62020-11-25 17:52:23 +0100458 attributes, NULL, 0, &pubkey_length ) );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200459 }
460#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
461
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200462 switch( location )
463 {
464 case PSA_KEY_LOCATION_LOCAL_STORAGE:
Ronald Cron977c2472020-10-13 08:32:21 +0200465#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200466 /* Transparent drivers are limited to generating asymmetric keys */
Ronald Cron977c2472020-10-13 08:32:21 +0200467 if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) )
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200468 {
Ronald Cron977c2472020-10-13 08:32:21 +0200469 /* Cycle through all known transparent accelerators */
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200470#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200471 status = mbedtls_test_transparent_generate_key(
Ronald Cron977c2472020-10-13 08:32:21 +0200472 attributes, key_buffer, key_buffer_size,
473 key_buffer_length );
474 /* Declared with fallback == true */
475 if( status != PSA_ERROR_NOT_SUPPORTED )
476 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200477#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200478 }
479#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
480
481 /* Software fallback */
482 status = psa_generate_key_internal(
483 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200484 break;
Ronald Cron977c2472020-10-13 08:32:21 +0200485
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200486 /* Add cases for opaque driver here */
Ronald Cron977c2472020-10-13 08:32:21 +0200487#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200488#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100489 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200490 status = mbedtls_test_opaque_generate_key(
Ronald Cron977c2472020-10-13 08:32:21 +0200491 attributes, key_buffer, key_buffer_size, key_buffer_length );
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200492 break;
Steven Cooremanf1720ea2020-07-24 18:41:58 +0200493#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron977c2472020-10-13 08:32:21 +0200494#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
495
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200496 default:
497 /* Key is declared with a lifetime not known to us */
498 status = PSA_ERROR_INVALID_ARGUMENT;
499 break;
500 }
501
Steven Cooreman2a1664c2020-07-20 15:33:08 +0200502 return( status );
Steven Cooreman55ae2172020-07-17 19:46:15 +0200503}
504
Ronald Cron83282872020-11-22 14:02:39 +0100505psa_status_t psa_driver_wrapper_import_key(
506 const psa_key_attributes_t *attributes,
507 const uint8_t *data,
508 size_t data_length,
509 uint8_t *key_buffer,
510 size_t key_buffer_size,
511 size_t *key_buffer_length,
512 size_t *bits )
Steven Cooreman04524762020-10-13 17:43:44 +0200513{
Steven Cooreman04524762020-10-13 17:43:44 +0200514 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronbf33c932020-11-28 18:06:53 +0100515 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
516 psa_get_key_lifetime( attributes ) );
Steven Cooreman04524762020-10-13 17:43:44 +0200517
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100518 /* Try dynamically-registered SE interface first */
519#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
520 const psa_drv_se_t *drv;
521 psa_drv_se_context_t *drv_context;
522
523 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
524 {
525 if( drv->key_management == NULL ||
526 drv->key_management->p_import == NULL )
527 return( PSA_ERROR_NOT_SUPPORTED );
528
529 /* The driver should set the number of key bits, however in
530 * case it doesn't, we initialize bits to an invalid value. */
531 *bits = PSA_MAX_KEY_BITS + 1;
532 status = drv->key_management->p_import(
533 drv_context,
534 *( (psa_key_slot_number_t *)key_buffer ),
535 attributes, data, data_length, bits );
536
537 if( status != PSA_SUCCESS )
538 return( status );
539
540 if( (*bits) > PSA_MAX_KEY_BITS )
541 return( PSA_ERROR_NOT_SUPPORTED );
542
543 return( PSA_SUCCESS );
544 }
545#endif /* PSA_CRYPTO_SE_C */
546
Ronald Cronbf33c932020-11-28 18:06:53 +0100547 switch( location )
548 {
549 case PSA_KEY_LOCATION_LOCAL_STORAGE:
550 /* Key is stored in the slot in export representation, so
551 * cycle through all known transparent accelerators */
552#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
553#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200554 status = mbedtls_test_transparent_import_key(
555 attributes,
556 data, data_length,
557 key_buffer, key_buffer_size,
558 key_buffer_length, bits );
Ronald Cronbf33c932020-11-28 18:06:53 +0100559 /* Declared with fallback == true */
560 if( status != PSA_ERROR_NOT_SUPPORTED )
561 return( status );
562#endif /* PSA_CRYPTO_DRIVER_TEST */
563#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
564 /* Fell through, meaning no accelerator supports this operation */
565 return( psa_import_key_into_slot( attributes,
566 data, data_length,
567 key_buffer, key_buffer_size,
568 key_buffer_length, bits ) );
569
570 default:
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100571 /* Importing a key with external storage in not yet supported.
572 * Return in error indicating that the lifetime is not valid. */
Ronald Cronbf33c932020-11-28 18:06:53 +0100573 (void)status;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +0100574 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cronbf33c932020-11-28 18:06:53 +0100575 }
576
Steven Cooreman04524762020-10-13 17:43:44 +0200577}
578
Ronald Cron67227982020-11-26 15:16:05 +0100579psa_status_t psa_driver_wrapper_export_key(
580 const psa_key_attributes_t *attributes,
581 const uint8_t *key_buffer, size_t key_buffer_size,
582 uint8_t *data, size_t data_size, size_t *data_length )
583
584{
585 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
586 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
587 psa_get_key_lifetime( attributes ) );
588
Ronald Cron152e3f82020-11-26 16:06:41 +0100589 /* Try dynamically-registered SE interface first */
590#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
591 const psa_drv_se_t *drv;
592 psa_drv_se_context_t *drv_context;
593
594 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
595 {
596 if( ( drv->key_management == NULL ) ||
597 ( drv->key_management->p_export == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100598 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100599 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100600 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100601
602 return( drv->key_management->p_export(
603 drv_context,
604 *( (psa_key_slot_number_t *)key_buffer ),
605 data, data_size, data_length ) );
606 }
607#endif /* PSA_CRYPTO_SE_C */
608
Ronald Cron67227982020-11-26 15:16:05 +0100609 switch( location )
610 {
611 case PSA_KEY_LOCATION_LOCAL_STORAGE:
612 return( psa_export_key_internal( attributes,
613 key_buffer,
614 key_buffer_size,
615 data,
616 data_size,
617 data_length ) );
618
619 /* Add cases for opaque driver here */
620#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
621#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100622 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200623 return( mbedtls_test_opaque_export_key( attributes,
624 key_buffer,
625 key_buffer_size,
626 data,
627 data_size,
628 data_length ) );
Ronald Cron67227982020-11-26 15:16:05 +0100629#endif /* PSA_CRYPTO_DRIVER_TEST */
630#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
631 default:
632 /* Key is declared with a lifetime not known to us */
633 return( status );
634 }
635}
636
Ronald Cron84cc9942020-11-25 14:30:05 +0100637psa_status_t psa_driver_wrapper_export_public_key(
638 const psa_key_attributes_t *attributes,
639 const uint8_t *key_buffer, size_t key_buffer_size,
640 uint8_t *data, size_t data_size, size_t *data_length )
641
Steven Cooremanb9b84422020-10-14 14:39:20 +0200642{
Steven Cooremanb9b84422020-10-14 14:39:20 +0200643 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Cron84cc9942020-11-25 14:30:05 +0100644 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
645 psa_get_key_lifetime( attributes ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200646
Ronald Cron152e3f82020-11-26 16:06:41 +0100647 /* Try dynamically-registered SE interface first */
648#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
649 const psa_drv_se_t *drv;
650 psa_drv_se_context_t *drv_context;
651
652 if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) )
653 {
654 if( ( drv->key_management == NULL ) ||
655 ( drv->key_management->p_export_public == NULL ) )
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100656 {
Ronald Cron152e3f82020-11-26 16:06:41 +0100657 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron9cfdf6e2021-01-18 11:58:39 +0100658 }
Ronald Cron152e3f82020-11-26 16:06:41 +0100659
660 return( drv->key_management->p_export_public(
661 drv_context,
662 *( (psa_key_slot_number_t *)key_buffer ),
663 data, data_size, data_length ) );
664 }
665#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
666
Steven Cooremanb9b84422020-10-14 14:39:20 +0200667 switch( location )
668 {
669 case PSA_KEY_LOCATION_LOCAL_STORAGE:
670 /* Key is stored in the slot in export representation, so
671 * cycle through all known transparent accelerators */
Ronald Cron67227982020-11-26 15:16:05 +0100672#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200673#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200674 status = mbedtls_test_transparent_export_public_key(
675 attributes,
676 key_buffer,
677 key_buffer_size,
678 data,
679 data_size,
680 data_length );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200681 /* Declared with fallback == true */
682 if( status != PSA_ERROR_NOT_SUPPORTED )
683 return( status );
684#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100685#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200686 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron67227982020-11-26 15:16:05 +0100687 return( psa_export_public_key_internal( attributes,
688 key_buffer,
689 key_buffer_size,
690 data,
691 data_size,
692 data_length ) );
693
Steven Cooremanb9b84422020-10-14 14:39:20 +0200694 /* Add cases for opaque driver here */
Ronald Cron67227982020-11-26 15:16:05 +0100695#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooremanb9b84422020-10-14 14:39:20 +0200696#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100697 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200698 return( mbedtls_test_opaque_export_public_key( attributes,
699 key_buffer,
700 key_buffer_size,
701 data,
702 data_size,
703 data_length ) );
Steven Cooremanb9b84422020-10-14 14:39:20 +0200704#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron67227982020-11-26 15:16:05 +0100705#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooremanb9b84422020-10-14 14:39:20 +0200706 default:
707 /* Key is declared with a lifetime not known to us */
708 return( status );
709 }
Steven Cooremanb9b84422020-10-14 14:39:20 +0200710}
711
Steven Cooreman6801f082021-02-19 17:21:22 +0100712psa_status_t psa_driver_wrapper_get_builtin_key(
713 psa_drv_slot_number_t slot_number,
714 psa_key_attributes_t *attributes,
715 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
716{
717 psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
718 switch( location )
719 {
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100720#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100721 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200722 return( mbedtls_test_opaque_get_builtin_key(
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100723 slot_number,
724 attributes,
725 key_buffer, key_buffer_size, key_buffer_length ) );
Steven Cooremanf9a55ff2021-02-19 18:04:59 +0100726#endif /* PSA_CRYPTO_DRIVER_TEST */
Steven Cooreman6801f082021-02-19 17:21:22 +0100727 default:
728 (void) slot_number;
729 (void) key_buffer;
730 (void) key_buffer_size;
731 (void) key_buffer_length;
732 return( PSA_ERROR_DOES_NOT_EXIST );
733 }
734}
735
Steven Cooreman37941cb2020-07-28 18:49:51 +0200736/*
737 * Cipher functions
738 */
739psa_status_t psa_driver_wrapper_cipher_encrypt(
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100740 const psa_key_attributes_t *attributes,
741 const uint8_t *key_buffer,
742 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200743 psa_algorithm_t alg,
744 const uint8_t *input,
745 size_t input_length,
746 uint8_t *output,
747 size_t output_size,
748 size_t *output_length )
749{
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100750 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
751 psa_key_location_t location =
752 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200753
754 switch( location )
755 {
756 case PSA_KEY_LOCATION_LOCAL_STORAGE:
757 /* Key is stored in the slot in export representation, so
758 * cycle through all known transparent accelerators */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100759#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200760#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100761 status = mbedtls_test_transparent_cipher_encrypt( attributes,
762 key_buffer,
763 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200764 alg,
765 input,
766 input_length,
767 output,
768 output_size,
769 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200770 /* Declared with fallback == true */
771 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200772 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200773#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100774#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
775
776#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
777 return( mbedtls_psa_cipher_encrypt( attributes,
778 key_buffer,
779 key_buffer_size,
780 alg,
781 input,
782 input_length,
783 output,
784 output_size,
785 output_length ) );
786#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
787
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200788 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100789
Steven Cooreman37941cb2020-07-28 18:49:51 +0200790 /* Add cases for opaque driver here */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100791#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200792#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100793 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100794 return( mbedtls_test_opaque_cipher_encrypt( attributes,
795 key_buffer,
796 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200797 alg,
798 input,
799 input_length,
800 output,
801 output_size,
802 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200803#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100804#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
805
Steven Cooreman37941cb2020-07-28 18:49:51 +0200806 default:
807 /* Key is declared with a lifetime not known to us */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100808 (void)status;
809 (void)key_buffer;
810 (void)key_buffer_size;
811 (void)alg;
812 (void)input;
813 (void)input_length;
814 (void)output;
815 (void)output_size;
816 (void)output_length;
817 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200818 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200819}
820
821psa_status_t psa_driver_wrapper_cipher_decrypt(
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100822 const psa_key_attributes_t *attributes,
823 const uint8_t *key_buffer,
824 size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200825 psa_algorithm_t alg,
826 const uint8_t *input,
827 size_t input_length,
828 uint8_t *output,
829 size_t output_size,
830 size_t *output_length )
831{
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100832 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
833 psa_key_location_t location =
834 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200835
836 switch( location )
837 {
838 case PSA_KEY_LOCATION_LOCAL_STORAGE:
839 /* Key is stored in the slot in export representation, so
840 * cycle through all known transparent accelerators */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100841#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200842#if defined(PSA_CRYPTO_DRIVER_TEST)
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100843 status = mbedtls_test_transparent_cipher_decrypt( attributes,
844 key_buffer,
845 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200846 alg,
847 input,
848 input_length,
849 output,
850 output_size,
851 output_length );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200852 /* Declared with fallback == true */
853 if( status != PSA_ERROR_NOT_SUPPORTED )
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200854 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200855#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100856#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
857
858#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
859 return( mbedtls_psa_cipher_decrypt( attributes,
860 key_buffer,
861 key_buffer_size,
862 alg,
863 input,
864 input_length,
865 output,
866 output_size,
867 output_length ) );
868#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
869
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200870 return( PSA_ERROR_NOT_SUPPORTED );
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100871
Steven Cooreman37941cb2020-07-28 18:49:51 +0200872 /* Add cases for opaque driver here */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100873#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200874#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100875 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100876 return( mbedtls_test_opaque_cipher_decrypt( attributes,
877 key_buffer,
878 key_buffer_size,
Ronald Cron7f13fa22021-04-13 12:41:34 +0200879 alg,
880 input,
881 input_length,
882 output,
883 output_size,
884 output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200885#endif /* PSA_CRYPTO_DRIVER_TEST */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100886#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
887
Steven Cooreman37941cb2020-07-28 18:49:51 +0200888 default:
889 /* Key is declared with a lifetime not known to us */
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100890 (void)status;
891 (void)key_buffer;
892 (void)key_buffer_size;
893 (void)alg;
894 (void)input;
895 (void)input_length;
896 (void)output;
897 (void)output_size;
898 (void)output_length;
899 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200900 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200901}
902
903psa_status_t psa_driver_wrapper_cipher_encrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100904 psa_cipher_operation_t *operation,
905 const psa_key_attributes_t *attributes,
906 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200907 psa_algorithm_t alg )
908{
Ronald Cron0b805592020-12-14 18:08:20 +0100909 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Crona4af55f2020-12-14 14:36:06 +0100910 psa_key_location_t location =
911 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200912
Steven Cooreman37941cb2020-07-28 18:49:51 +0200913 switch( location )
914 {
915 case PSA_KEY_LOCATION_LOCAL_STORAGE:
916 /* Key is stored in the slot in export representation, so
917 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100918#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200919#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200920 status = mbedtls_test_transparent_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100921 &operation->ctx.transparent_test_driver_ctx,
922 attributes,
923 key_buffer,
924 key_buffer_size,
925 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200926 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200927 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100928 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +0200929
Ronald Cron0b805592020-12-14 18:08:20 +0100930 if( status != PSA_ERROR_NOT_SUPPORTED )
931 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200932#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100933#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100934#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200935 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +0100936 status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +0100937 attributes,
938 key_buffer,
939 key_buffer_size,
940 alg );
941 if( status == PSA_SUCCESS )
Ronald Cron6e412a72021-03-10 09:58:47 +0100942 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Ronald Cron49fafa92021-03-10 08:34:23 +0100943
Ronald Cron7b4154d2021-03-19 14:49:41 +0100944 if( status != PSA_ERROR_NOT_SUPPORTED )
945 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100946#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
947 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron0b805592020-12-14 18:08:20 +0100948
Steven Cooreman37941cb2020-07-28 18:49:51 +0200949 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +0100950#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200951#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +0100952 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +0200953 status = mbedtls_test_opaque_cipher_encrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100954 &operation->ctx.opaque_test_driver_ctx,
955 attributes,
956 key_buffer, key_buffer_size,
957 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200958
Steven Cooreman37941cb2020-07-28 18:49:51 +0200959 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +0100960 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +0200961
Steven Cooreman5240e8b2020-09-09 11:51:45 +0200962 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200963#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +0100964#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +0200965 default:
966 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +0100967 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +0100968 (void)key_buffer;
969 (void)key_buffer_size;
970 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +0200971 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200972 }
Steven Cooreman37941cb2020-07-28 18:49:51 +0200973}
974
975psa_status_t psa_driver_wrapper_cipher_decrypt_setup(
Ronald Crona4af55f2020-12-14 14:36:06 +0100976 psa_cipher_operation_t *operation,
977 const psa_key_attributes_t *attributes,
978 const uint8_t *key_buffer, size_t key_buffer_size,
Steven Cooreman37941cb2020-07-28 18:49:51 +0200979 psa_algorithm_t alg )
980{
Steven Cooreman37941cb2020-07-28 18:49:51 +0200981 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crona4af55f2020-12-14 14:36:06 +0100982 psa_key_location_t location =
983 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200984
Steven Cooreman37941cb2020-07-28 18:49:51 +0200985 switch( location )
986 {
987 case PSA_KEY_LOCATION_LOCAL_STORAGE:
988 /* Key is stored in the slot in export representation, so
989 * cycle through all known transparent accelerators */
Ronald Cron0b805592020-12-14 18:08:20 +0100990#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +0200991#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200992 status = mbedtls_test_transparent_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +0100993 &operation->ctx.transparent_test_driver_ctx,
994 attributes,
995 key_buffer,
996 key_buffer_size,
997 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +0200998 /* Declared with fallback == true */
Steven Cooreman150c99b2020-09-09 14:32:44 +0200999 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001000 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Steven Cooreman150c99b2020-09-09 14:32:44 +02001001
Ronald Cron0b805592020-12-14 18:08:20 +01001002 if( status != PSA_ERROR_NOT_SUPPORTED )
1003 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001004#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001005#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001006#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001007 /* Fell through, meaning no accelerator supports this operation */
Ronald Cron6e412a72021-03-10 09:58:47 +01001008 status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001009 attributes,
1010 key_buffer,
1011 key_buffer_size,
1012 alg );
1013 if( status == PSA_SUCCESS )
1014 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1015
1016 return( status );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001017#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1018 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron49fafa92021-03-10 08:34:23 +01001019
Steven Cooreman37941cb2020-07-28 18:49:51 +02001020 /* Add cases for opaque driver here */
Ronald Cron0b805592020-12-14 18:08:20 +01001021#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001022#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremana1ce2f22021-03-18 20:49:29 +01001023 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001024 status = mbedtls_test_opaque_cipher_decrypt_setup(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001025 &operation->ctx.opaque_test_driver_ctx,
1026 attributes,
1027 key_buffer, key_buffer_size,
1028 alg );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001029
Steven Cooreman37941cb2020-07-28 18:49:51 +02001030 if( status == PSA_SUCCESS )
Ronald Cron49fafa92021-03-10 08:34:23 +01001031 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001032
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001033 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001034#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Cron0b805592020-12-14 18:08:20 +01001035#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Steven Cooreman37941cb2020-07-28 18:49:51 +02001036 default:
1037 /* Key is declared with a lifetime not known to us */
Ronald Cron0b805592020-12-14 18:08:20 +01001038 (void)status;
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001039 (void)key_buffer;
1040 (void)key_buffer_size;
1041 (void)alg;
Ronald Cronc45b4af2020-09-29 16:18:05 +02001042 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001043 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001044}
1045
Steven Cooreman37941cb2020-07-28 18:49:51 +02001046psa_status_t psa_driver_wrapper_cipher_set_iv(
Ronald Cron6056fe82020-12-15 13:58:07 +01001047 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001048 const uint8_t *iv,
1049 size_t iv_length )
1050{
Ronald Cron49fafa92021-03-10 08:34:23 +01001051 switch( operation->id )
1052 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001053#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001054 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001055 return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001056 iv,
1057 iv_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001058#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001059
1060#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001061#if defined(PSA_CRYPTO_DRIVER_TEST)
1062 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001063 return( mbedtls_test_transparent_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001064 &operation->ctx.transparent_test_driver_ctx,
1065 iv, iv_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001066
Steven Cooreman37941cb2020-07-28 18:49:51 +02001067 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001068 return( mbedtls_test_opaque_cipher_set_iv(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001069 &operation->ctx.opaque_test_driver_ctx,
1070 iv, iv_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001071#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001072#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001073 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001074
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001075 (void)iv;
1076 (void)iv_length;
1077
Ronald Crondd24c9b2020-12-15 14:10:01 +01001078 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001079}
1080
1081psa_status_t psa_driver_wrapper_cipher_update(
Ronald Cron6056fe82020-12-15 13:58:07 +01001082 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001083 const uint8_t *input,
1084 size_t input_length,
1085 uint8_t *output,
1086 size_t output_size,
1087 size_t *output_length )
1088{
Ronald Cron49fafa92021-03-10 08:34:23 +01001089 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001090 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001091#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001092 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001093 return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001094 input,
1095 input_length,
1096 output,
1097 output_size,
1098 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001099#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
1100
Ronald Cron49fafa92021-03-10 08:34:23 +01001101#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001102#if defined(PSA_CRYPTO_DRIVER_TEST)
1103 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001104 return( mbedtls_test_transparent_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001105 &operation->ctx.transparent_test_driver_ctx,
1106 input, input_length,
1107 output, output_size, output_length ) );
1108
Steven Cooreman37941cb2020-07-28 18:49:51 +02001109 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001110 return( mbedtls_test_opaque_cipher_update(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001111 &operation->ctx.opaque_test_driver_ctx,
1112 input, input_length,
1113 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001114#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001115#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001116 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001117
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001118 (void)input;
1119 (void)input_length;
1120 (void)output;
1121 (void)output_size;
1122 (void)output_length;
1123
Ronald Crondd24c9b2020-12-15 14:10:01 +01001124 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001125}
1126
1127psa_status_t psa_driver_wrapper_cipher_finish(
Ronald Cron6056fe82020-12-15 13:58:07 +01001128 psa_cipher_operation_t *operation,
Steven Cooreman37941cb2020-07-28 18:49:51 +02001129 uint8_t *output,
1130 size_t output_size,
1131 size_t *output_length )
1132{
Ronald Cron49fafa92021-03-10 08:34:23 +01001133 switch( operation->id )
1134 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001135#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001136 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001137 return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx,
Ronald Cron49fafa92021-03-10 08:34:23 +01001138 output,
1139 output_size,
1140 output_length ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001141#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001142
1143#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001144#if defined(PSA_CRYPTO_DRIVER_TEST)
1145 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001146 return( mbedtls_test_transparent_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001147 &operation->ctx.transparent_test_driver_ctx,
1148 output, output_size, output_length ) );
Ronald Cron49fafa92021-03-10 08:34:23 +01001149
Steven Cooreman37941cb2020-07-28 18:49:51 +02001150 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001151 return( mbedtls_test_opaque_cipher_finish(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001152 &operation->ctx.opaque_test_driver_ctx,
1153 output, output_size, output_length ) );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001154#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001155#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001156 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001157
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001158 (void)output;
1159 (void)output_size;
1160 (void)output_length;
1161
Ronald Crondd24c9b2020-12-15 14:10:01 +01001162 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001163}
1164
1165psa_status_t psa_driver_wrapper_cipher_abort(
Ronald Cron6056fe82020-12-15 13:58:07 +01001166 psa_cipher_operation_t *operation )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001167{
Ronald Crondd24c9b2020-12-15 14:10:01 +01001168 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman37941cb2020-07-28 18:49:51 +02001169
Ronald Cron49fafa92021-03-10 08:34:23 +01001170 switch( operation->id )
Steven Cooreman37941cb2020-07-28 18:49:51 +02001171 {
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001172#if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
Ronald Cron49fafa92021-03-10 08:34:23 +01001173 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Ronald Cron6e412a72021-03-10 09:58:47 +01001174 return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) );
Ronald Cron5d9b00d2021-03-10 14:43:20 +01001175#endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
Ronald Cron49fafa92021-03-10 08:34:23 +01001176
1177#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
Steven Cooreman37941cb2020-07-28 18:49:51 +02001178#if defined(PSA_CRYPTO_DRIVER_TEST)
1179 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001180 status = mbedtls_test_transparent_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001181 &operation->ctx.transparent_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001182 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001183 &operation->ctx.transparent_test_driver_ctx,
1184 sizeof( operation->ctx.transparent_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001185 return( status );
Ronald Cron49fafa92021-03-10 08:34:23 +01001186
Steven Cooreman37941cb2020-07-28 18:49:51 +02001187 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001188 status = mbedtls_test_opaque_cipher_abort(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001189 &operation->ctx.opaque_test_driver_ctx );
Steven Cooremancfeea8f2020-09-09 15:09:18 +02001190 mbedtls_platform_zeroize(
Ronald Cron7cb9c3d2021-03-10 12:21:48 +01001191 &operation->ctx.opaque_test_driver_ctx,
1192 sizeof( operation->ctx.opaque_test_driver_ctx ) );
Steven Cooreman5240e8b2020-09-09 11:51:45 +02001193 return( status );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001194#endif /* PSA_CRYPTO_DRIVER_TEST */
Ronald Crondd24c9b2020-12-15 14:10:01 +01001195#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
Ronald Cron49fafa92021-03-10 08:34:23 +01001196 }
Steven Cooreman37941cb2020-07-28 18:49:51 +02001197
Ronald Cron49fafa92021-03-10 08:34:23 +01001198 (void)status;
Ronald Crondd24c9b2020-12-15 14:10:01 +01001199 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman37941cb2020-07-28 18:49:51 +02001200}
1201
Steven Cooreman1e582352021-02-18 17:24:37 +01001202/*
1203 * Hashing functions
1204 */
1205psa_status_t psa_driver_wrapper_hash_compute(
1206 psa_algorithm_t alg,
1207 const uint8_t *input,
1208 size_t input_length,
1209 uint8_t *hash,
1210 size_t hash_size,
1211 size_t *hash_length)
1212{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001213 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001214
1215 /* Try accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001216#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001217 status = mbedtls_test_transparent_hash_compute(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001218 alg, input, input_length, hash, hash_size, hash_length );
Steven Cooremanf7638102021-03-04 15:14:36 +01001219 if( status != PSA_ERROR_NOT_SUPPORTED )
1220 return( status );
1221#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001222
1223 /* If software fallback is compiled in, try fallback */
1224#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1225 status = mbedtls_psa_hash_compute( alg, input, input_length,
1226 hash, hash_size, hash_length );
1227 if( status != PSA_ERROR_NOT_SUPPORTED )
1228 return( status );
1229#endif
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001230 (void) status;
1231 (void) alg;
1232 (void) input;
1233 (void) input_length;
1234 (void) hash;
1235 (void) hash_size;
1236 (void) hash_length;
Steven Cooreman1e582352021-02-18 17:24:37 +01001237
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001238 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman1e582352021-02-18 17:24:37 +01001239}
1240
1241psa_status_t psa_driver_wrapper_hash_setup(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001242 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001243 psa_algorithm_t alg )
1244{
Steven Cooreman0eeb7942021-03-08 12:13:21 +01001245 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman1e582352021-02-18 17:24:37 +01001246
Steven Cooreman1e582352021-02-18 17:24:37 +01001247 /* Try setup on accelerators first */
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001248#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001249 status = mbedtls_test_transparent_hash_setup(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001250 &operation->ctx.test_driver_ctx, alg );
Steven Cooremanf7638102021-03-04 15:14:36 +01001251 if( status == PSA_SUCCESS )
1252 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1253
1254 if( status != PSA_ERROR_NOT_SUPPORTED )
1255 return( status );
1256#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001257
1258 /* If software fallback is compiled in, try fallback */
1259#if defined(MBEDTLS_PSA_BUILTIN_HASH)
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001260 status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg );
Steven Cooreman1e582352021-02-18 17:24:37 +01001261 if( status == PSA_SUCCESS )
Steven Cooreman1e582352021-02-18 17:24:37 +01001262 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
Steven Cooreman1e582352021-02-18 17:24:37 +01001263
1264 if( status != PSA_ERROR_NOT_SUPPORTED )
1265 return( status );
1266#endif
1267 /* Nothing left to try if we fall through here */
1268 (void) status;
1269 (void) operation;
1270 (void) alg;
1271 return( PSA_ERROR_NOT_SUPPORTED );
1272}
1273
1274psa_status_t psa_driver_wrapper_hash_clone(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001275 const psa_hash_operation_t *source_operation,
1276 psa_hash_operation_t *target_operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001277{
Steven Cooreman1e582352021-02-18 17:24:37 +01001278 switch( source_operation->id )
1279 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001280#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1281 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1282 target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1283 return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx,
1284 &target_operation->ctx.mbedtls_ctx ) );
1285#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001286#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001287 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
1288 target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
Ronald Cron7f13fa22021-04-13 12:41:34 +02001289 return( mbedtls_test_transparent_hash_clone(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001290 &source_operation->ctx.test_driver_ctx,
1291 &target_operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001292#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001293 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001294 (void) target_operation;
1295 return( PSA_ERROR_BAD_STATE );
1296 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001297}
1298
1299psa_status_t psa_driver_wrapper_hash_update(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001300 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001301 const uint8_t *input,
1302 size_t input_length )
1303{
Steven Cooreman1e582352021-02-18 17:24:37 +01001304 switch( operation->id )
1305 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001306#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1307 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1308 return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx,
1309 input, input_length ) );
1310#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001311#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001312 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001313 return( mbedtls_test_transparent_hash_update(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001314 &operation->ctx.test_driver_ctx,
1315 input, input_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001316#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001317 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001318 (void) input;
1319 (void) input_length;
1320 return( PSA_ERROR_BAD_STATE );
1321 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001322}
1323
1324psa_status_t psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001325 psa_hash_operation_t *operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01001326 uint8_t *hash,
1327 size_t hash_size,
1328 size_t *hash_length )
1329{
Steven Cooreman1e582352021-02-18 17:24:37 +01001330 switch( operation->id )
1331 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001332#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1333 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1334 return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx,
1335 hash, hash_size, hash_length ) );
1336#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001337#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001338 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001339 return( mbedtls_test_transparent_hash_finish(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001340 &operation->ctx.test_driver_ctx,
1341 hash, hash_size, hash_length ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001342#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001343 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001344 (void) hash;
1345 (void) hash_size;
1346 (void) hash_length;
1347 return( PSA_ERROR_BAD_STATE );
1348 }
Steven Cooreman1e582352021-02-18 17:24:37 +01001349}
1350
1351psa_status_t psa_driver_wrapper_hash_abort(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01001352 psa_hash_operation_t *operation )
Steven Cooreman1e582352021-02-18 17:24:37 +01001353{
Steven Cooreman1e582352021-02-18 17:24:37 +01001354 switch( operation->id )
1355 {
Steven Cooreman5e4c18f2021-03-15 12:26:07 +01001356#if defined(MBEDTLS_PSA_BUILTIN_HASH)
1357 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
1358 return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) );
1359#endif
Steven Cooremanf8e45a42021-03-16 11:07:55 +01001360#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanf7638102021-03-04 15:14:36 +01001361 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Ronald Cron7f13fa22021-04-13 12:41:34 +02001362 return( mbedtls_test_transparent_hash_abort(
Steven Cooreman0f8ffa82021-03-15 11:56:33 +01001363 &operation->ctx.test_driver_ctx ) );
Steven Cooremanf7638102021-03-04 15:14:36 +01001364#endif
Steven Cooreman1e582352021-02-18 17:24:37 +01001365 default:
Steven Cooreman1e582352021-02-18 17:24:37 +01001366 return( PSA_ERROR_BAD_STATE );
1367 }
1368}
1369
Ronald Cronde822812021-03-17 16:08:20 +01001370psa_status_t psa_driver_wrapper_aead_encrypt(
1371 const psa_key_attributes_t *attributes,
1372 const uint8_t *key_buffer, size_t key_buffer_size,
1373 psa_algorithm_t alg,
1374 const uint8_t *nonce, size_t nonce_length,
1375 const uint8_t *additional_data, size_t additional_data_length,
1376 const uint8_t *plaintext, size_t plaintext_length,
1377 uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length )
1378{
1379 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1380 psa_key_location_t location =
1381 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1382
1383 switch( location )
1384 {
1385 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1386 /* Key is stored in the slot in export representation, so
1387 * cycle through all known transparent accelerators */
1388
1389#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1390#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001391 status = mbedtls_test_transparent_aead_encrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001392 attributes, key_buffer, key_buffer_size,
1393 alg,
1394 nonce, nonce_length,
1395 additional_data, additional_data_length,
1396 plaintext, plaintext_length,
1397 ciphertext, ciphertext_size, ciphertext_length );
1398 /* Declared with fallback == true */
1399 if( status != PSA_ERROR_NOT_SUPPORTED )
1400 return( status );
1401#endif /* PSA_CRYPTO_DRIVER_TEST */
1402#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1403
1404 /* Fell through, meaning no accelerator supports this operation */
1405 return( mbedtls_psa_aead_encrypt(
1406 attributes, key_buffer, key_buffer_size,
1407 alg,
1408 nonce, nonce_length,
1409 additional_data, additional_data_length,
1410 plaintext, plaintext_length,
1411 ciphertext, ciphertext_size, ciphertext_length ) );
1412
1413 /* Add cases for opaque driver here */
1414
1415 default:
1416 /* Key is declared with a lifetime not known to us */
1417 (void)status;
1418 return( PSA_ERROR_INVALID_ARGUMENT );
1419 }
1420}
1421
1422psa_status_t psa_driver_wrapper_aead_decrypt(
1423 const psa_key_attributes_t *attributes,
1424 const uint8_t *key_buffer, size_t key_buffer_size,
1425 psa_algorithm_t alg,
1426 const uint8_t *nonce, size_t nonce_length,
1427 const uint8_t *additional_data, size_t additional_data_length,
1428 const uint8_t *ciphertext, size_t ciphertext_length,
1429 uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length )
1430{
1431 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1432 psa_key_location_t location =
1433 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1434
1435 switch( location )
1436 {
1437 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1438 /* Key is stored in the slot in export representation, so
1439 * cycle through all known transparent accelerators */
1440
1441#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1442#if defined(PSA_CRYPTO_DRIVER_TEST)
Ronald Cron7f13fa22021-04-13 12:41:34 +02001443 status = mbedtls_test_transparent_aead_decrypt(
Ronald Cronde822812021-03-17 16:08:20 +01001444 attributes, key_buffer, key_buffer_size,
1445 alg,
1446 nonce, nonce_length,
1447 additional_data, additional_data_length,
1448 ciphertext, ciphertext_length,
1449 plaintext, plaintext_size, plaintext_length );
1450 /* Declared with fallback == true */
1451 if( status != PSA_ERROR_NOT_SUPPORTED )
1452 return( status );
1453#endif /* PSA_CRYPTO_DRIVER_TEST */
1454#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1455
1456 /* Fell through, meaning no accelerator supports this operation */
1457 return( mbedtls_psa_aead_decrypt(
1458 attributes, key_buffer, key_buffer_size,
1459 alg,
1460 nonce, nonce_length,
1461 additional_data, additional_data_length,
1462 ciphertext, ciphertext_length,
1463 plaintext, plaintext_size, plaintext_length ) );
1464
1465 /* Add cases for opaque driver here */
1466
1467 default:
1468 /* Key is declared with a lifetime not known to us */
1469 (void)status;
1470 return( PSA_ERROR_INVALID_ARGUMENT );
1471 }
1472}
Steven Cooremand13a70f2021-03-19 15:24:23 +01001473
1474
1475/*
1476 * MAC functions
1477 */
1478psa_status_t psa_driver_wrapper_mac_compute(
1479 const psa_key_attributes_t *attributes,
1480 const uint8_t *key_buffer,
1481 size_t key_buffer_size,
1482 psa_algorithm_t alg,
1483 const uint8_t *input,
1484 size_t input_length,
1485 uint8_t *mac,
1486 size_t mac_size,
1487 size_t *mac_length )
1488{
1489 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1490 psa_key_location_t location =
1491 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1492
1493 switch( location )
1494 {
1495 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1496 /* Key is stored in the slot in export representation, so
1497 * cycle through all known transparent accelerators */
1498#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1499#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001500 status = mbedtls_test_transparent_mac_compute(
Steven Cooremand13a70f2021-03-19 15:24:23 +01001501 attributes, key_buffer, key_buffer_size, alg,
1502 input, input_length,
1503 mac, mac_size, mac_length );
1504 /* Declared with fallback == true */
1505 if( status != PSA_ERROR_NOT_SUPPORTED )
1506 return( status );
1507#endif /* PSA_CRYPTO_DRIVER_TEST */
1508#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1509#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1510 /* Fell through, meaning no accelerator supports this operation */
1511 status = mbedtls_psa_mac_compute(
1512 attributes, key_buffer, key_buffer_size, alg,
1513 input, input_length,
1514 mac, mac_size, mac_length );
1515 if( status != PSA_ERROR_NOT_SUPPORTED )
1516 return( status );
1517#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1518 return( PSA_ERROR_NOT_SUPPORTED );
1519
1520 /* Add cases for opaque driver here */
1521#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1522#if defined(PSA_CRYPTO_DRIVER_TEST)
1523 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001524 status = mbedtls_test_opaque_mac_compute(
Steven Cooremand13a70f2021-03-19 15:24:23 +01001525 attributes, key_buffer, key_buffer_size, alg,
1526 input, input_length,
1527 mac, mac_size, mac_length );
1528 return( status );
1529#endif /* PSA_CRYPTO_DRIVER_TEST */
1530#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1531 default:
1532 /* Key is declared with a lifetime not known to us */
1533 (void) key_buffer;
1534 (void) key_buffer_size;
1535 (void) alg;
1536 (void) input;
1537 (void) input_length;
1538 (void) mac;
1539 (void) mac_size;
1540 (void) mac_length;
1541 (void) status;
1542 return( PSA_ERROR_INVALID_ARGUMENT );
1543 }
1544}
1545
1546psa_status_t psa_driver_wrapper_mac_sign_setup(
1547 psa_mac_operation_t *operation,
1548 const psa_key_attributes_t *attributes,
1549 const uint8_t *key_buffer,
1550 size_t key_buffer_size,
1551 psa_algorithm_t alg )
1552{
1553 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1554 psa_key_location_t location =
1555 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1556
1557 switch( location )
1558 {
1559 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1560 /* Key is stored in the slot in export representation, so
1561 * cycle through all known transparent accelerators */
1562#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1563#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001564 status = mbedtls_test_transparent_mac_sign_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001565 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001566 attributes,
1567 key_buffer, key_buffer_size,
1568 alg );
1569 /* Declared with fallback == true */
1570 if( status == PSA_SUCCESS )
1571 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1572
1573 if( status != PSA_ERROR_NOT_SUPPORTED )
1574 return( status );
1575#endif /* PSA_CRYPTO_DRIVER_TEST */
1576#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1577#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1578 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001579 status = mbedtls_psa_mac_sign_setup( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001580 attributes,
1581 key_buffer, key_buffer_size,
1582 alg );
1583 if( status == PSA_SUCCESS )
1584 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1585
1586 if( status != PSA_ERROR_NOT_SUPPORTED )
1587 return( status );
1588#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1589 return( PSA_ERROR_NOT_SUPPORTED );
1590
1591 /* Add cases for opaque driver here */
1592#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1593#if defined(PSA_CRYPTO_DRIVER_TEST)
1594 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001595 status = mbedtls_test_opaque_mac_sign_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001596 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001597 attributes,
1598 key_buffer, key_buffer_size,
1599 alg );
1600
1601 if( status == PSA_SUCCESS )
1602 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
1603
1604 return( status );
1605#endif /* PSA_CRYPTO_DRIVER_TEST */
1606#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1607 default:
1608 /* Key is declared with a lifetime not known to us */
1609 (void) status;
1610 (void) key_buffer;
1611 (void) key_buffer_size;
1612 (void) alg;
1613 return( PSA_ERROR_INVALID_ARGUMENT );
1614 }
1615}
1616
1617psa_status_t psa_driver_wrapper_mac_verify_setup(
1618 psa_mac_operation_t *operation,
1619 const psa_key_attributes_t *attributes,
1620 const uint8_t *key_buffer,
1621 size_t key_buffer_size,
1622 psa_algorithm_t alg )
1623{
1624 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1625 psa_key_location_t location =
1626 PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime );
1627
1628 switch( location )
1629 {
1630 case PSA_KEY_LOCATION_LOCAL_STORAGE:
1631 /* Key is stored in the slot in export representation, so
1632 * cycle through all known transparent accelerators */
1633#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1634#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001635 status = mbedtls_test_transparent_mac_verify_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001636 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001637 attributes,
1638 key_buffer, key_buffer_size,
1639 alg );
1640 /* Declared with fallback == true */
1641 if( status == PSA_SUCCESS )
1642 operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID;
1643
1644 if( status != PSA_ERROR_NOT_SUPPORTED )
1645 return( status );
1646#endif /* PSA_CRYPTO_DRIVER_TEST */
1647#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1648#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1649 /* Fell through, meaning no accelerator supports this operation */
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001650 status = mbedtls_psa_mac_verify_setup( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001651 attributes,
1652 key_buffer, key_buffer_size,
1653 alg );
1654 if( status == PSA_SUCCESS )
1655 operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID;
1656
1657 if( status != PSA_ERROR_NOT_SUPPORTED )
1658 return( status );
1659#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1660 return( PSA_ERROR_NOT_SUPPORTED );
1661
1662 /* Add cases for opaque driver here */
1663#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1664#if defined(PSA_CRYPTO_DRIVER_TEST)
1665 case PSA_CRYPTO_TEST_DRIVER_LOCATION:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001666 status = mbedtls_test_opaque_mac_verify_setup(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001667 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001668 attributes,
1669 key_buffer, key_buffer_size,
1670 alg );
1671
1672 if( status == PSA_SUCCESS )
1673 operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID;
1674
1675 return( status );
1676#endif /* PSA_CRYPTO_DRIVER_TEST */
1677#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1678 default:
1679 /* Key is declared with a lifetime not known to us */
1680 (void) status;
1681 (void) key_buffer;
1682 (void) key_buffer_size;
1683 (void) alg;
1684 return( PSA_ERROR_INVALID_ARGUMENT );
1685 }
1686}
1687
1688psa_status_t psa_driver_wrapper_mac_update(
1689 psa_mac_operation_t *operation,
1690 const uint8_t *input,
1691 size_t input_length )
1692{
1693 switch( operation->id )
1694 {
1695#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1696 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001697 return( mbedtls_psa_mac_update( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001698 input, input_length ) );
1699#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1700
1701#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1702#if defined(PSA_CRYPTO_DRIVER_TEST)
1703 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001704 return( mbedtls_test_transparent_mac_update(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001705 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001706 input, input_length ) );
1707
1708 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001709 return( mbedtls_test_opaque_mac_update(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001710 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001711 input, input_length ) );
1712#endif /* PSA_CRYPTO_DRIVER_TEST */
1713#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1714 default:
1715 (void) input;
1716 (void) input_length;
1717 return( PSA_ERROR_INVALID_ARGUMENT );
1718 }
1719}
1720
1721psa_status_t psa_driver_wrapper_mac_sign_finish(
1722 psa_mac_operation_t *operation,
1723 uint8_t *mac,
1724 size_t mac_size,
1725 size_t *mac_length )
1726{
1727 switch( operation->id )
1728 {
1729#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1730 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001731 return( mbedtls_psa_mac_sign_finish( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001732 mac, mac_size, mac_length ) );
1733#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1734
1735#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1736#if defined(PSA_CRYPTO_DRIVER_TEST)
1737 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001738 return( mbedtls_test_transparent_mac_sign_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001739 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001740 mac, mac_size, mac_length ) );
1741
1742 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001743 return( mbedtls_test_opaque_mac_sign_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001744 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001745 mac, mac_size, mac_length ) );
1746#endif /* PSA_CRYPTO_DRIVER_TEST */
1747#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1748 default:
1749 (void) mac;
1750 (void) mac_size;
1751 (void) mac_length;
1752 return( PSA_ERROR_INVALID_ARGUMENT );
1753 }
1754}
1755
1756psa_status_t psa_driver_wrapper_mac_verify_finish(
1757 psa_mac_operation_t *operation,
1758 const uint8_t *mac,
1759 size_t mac_length )
1760{
1761 switch( operation->id )
1762 {
1763#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1764 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001765 return( mbedtls_psa_mac_verify_finish( &operation->ctx.mbedtls_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001766 mac, mac_length ) );
1767#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1768
1769#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1770#if defined(PSA_CRYPTO_DRIVER_TEST)
1771 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001772 return( mbedtls_test_transparent_mac_verify_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001773 &operation->ctx.transparent_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001774 mac, mac_length ) );
1775
1776 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001777 return( mbedtls_test_opaque_mac_verify_finish(
Steven Cooreman77e2cc52021-03-19 17:05:52 +01001778 &operation->ctx.opaque_test_driver_ctx,
Steven Cooremand13a70f2021-03-19 15:24:23 +01001779 mac, mac_length ) );
1780#endif /* PSA_CRYPTO_DRIVER_TEST */
1781#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1782 default:
1783 (void) mac;
1784 (void) mac_length;
1785 return( PSA_ERROR_INVALID_ARGUMENT );
1786 }
1787}
1788
1789psa_status_t psa_driver_wrapper_mac_abort(
1790 psa_mac_operation_t *operation )
1791{
Steven Cooremand13a70f2021-03-19 15:24:23 +01001792 switch( operation->id )
1793 {
1794#if defined(MBEDTLS_PSA_BUILTIN_MAC)
1795 case PSA_CRYPTO_MBED_TLS_DRIVER_ID:
Steven Cooremane6804192021-03-19 18:28:56 +01001796 return( mbedtls_psa_mac_abort( &operation->ctx.mbedtls_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01001797#endif /* MBEDTLS_PSA_BUILTIN_MAC */
1798
1799#if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
1800#if defined(PSA_CRYPTO_DRIVER_TEST)
1801 case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001802 return( mbedtls_test_transparent_mac_abort(
Steven Cooremane6804192021-03-19 18:28:56 +01001803 &operation->ctx.transparent_test_driver_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01001804 case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID:
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001805 return( mbedtls_test_opaque_mac_abort(
Steven Cooremane6804192021-03-19 18:28:56 +01001806 &operation->ctx.opaque_test_driver_ctx ) );
Steven Cooremand13a70f2021-03-19 15:24:23 +01001807#endif /* PSA_CRYPTO_DRIVER_TEST */
1808#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
1809 default:
Steven Cooremane6804192021-03-19 18:28:56 +01001810 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand13a70f2021-03-19 15:24:23 +01001811 }
Steven Cooremand13a70f2021-03-19 15:24:23 +01001812}
Gilles Peskine1905a242021-04-24 13:19:45 +02001813
1814#endif /* MBEDTLS_PSA_CRYPTO_C */