| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Functions to delegate cryptographic operations to an available | 
|  | 3 | *  and appropriate accelerator. | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 4 | *  Warning: This file will be auto-generated in the future. | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 5 | */ | 
| Steven Cooreman | 2c7b2f8 | 2020-09-02 13:43:46 +0200 | [diff] [blame] | 6 | /*  Copyright The Mbed TLS Contributors | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 7 | *  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 Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 20 | */ | 
|  | 21 |  | 
| Mateusz Starzyk | 2c09c9b | 2021-05-14 22:20:10 +0200 | [diff] [blame] | 22 | #include "common.h" | 
| Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 23 | #include "psa_crypto_aead.h" | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 24 | #include "psa_crypto_cipher.h" | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 25 | #include "psa_crypto_core.h" | 
|  | 26 | #include "psa_crypto_driver_wrappers.h" | 
| Steven Cooreman | 830aff2 | 2021-03-09 09:50:44 +0100 | [diff] [blame] | 27 | #include "psa_crypto_hash.h" | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 28 | #include "psa_crypto_mac.h" | 
| Steven Cooreman | 830aff2 | 2021-03-09 09:50:44 +0100 | [diff] [blame] | 29 |  | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 30 | #include "mbedtls/platform.h" | 
|  | 31 |  | 
| Gilles Peskine | 1905a24 | 2021-04-24 13:19:45 +0200 | [diff] [blame] | 32 | #if defined(MBEDTLS_PSA_CRYPTO_C) | 
|  | 33 |  | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 34 | #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 35 |  | 
|  | 36 | /* Include test driver definition when running tests */ | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 37 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 38 | #ifndef PSA_CRYPTO_DRIVER_PRESENT | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 39 | #define PSA_CRYPTO_DRIVER_PRESENT | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 40 | #endif | 
|  | 41 | #ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 42 | #define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 43 | #endif | 
| Ronald Cron | ce1d8d2 | 2021-04-30 17:00:34 +0200 | [diff] [blame] | 44 | #include "test/drivers/test_driver.h" | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 45 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 46 |  | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 47 | /* Repeat above block for each JSON-declared driver during autogeneration */ | 
| Steven Cooreman | aa87fd0 | 2021-03-15 18:54:03 +0100 | [diff] [blame] | 48 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */ | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 49 |  | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 50 | /* 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 Cooreman | 830aff2 | 2021-03-09 09:50:44 +0100 | [diff] [blame] | 53 | #define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1) | 
|  | 54 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 55 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 56 | #define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2) | 
|  | 57 | #define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 58 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 59 |  | 
|  | 60 | /* Support the 'old' SE interface when asked to */ | 
| Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 61 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 62 | /* 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 Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 65 | #define PSA_CRYPTO_DRIVER_PRESENT | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 66 | #endif | 
| Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 67 | #include "psa_crypto_se.h" | 
|  | 68 | #endif | 
|  | 69 |  | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 70 | /* Start delegation functions */ | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 71 | psa_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-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 83 | 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-arm | 256443e | 2021-05-07 15:16:34 +0200 | [diff] [blame] | 108 | break; | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 109 |  | 
|  | 110 | /* Add cases for opaque driver here */ | 
|  | 111 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
|  | 112 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| gabor-mezei-arm | 2522c0b | 2021-04-27 18:52:51 +0200 | [diff] [blame] | 113 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| gabor-mezei-arm | 256443e | 2021-05-07 15:16:34 +0200 | [diff] [blame] | 114 | status = mbedtls_test_opaque_signature_sign_message( | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 115 | attributes, | 
|  | 116 | key_buffer, | 
|  | 117 | key_buffer_size, | 
|  | 118 | alg, | 
|  | 119 | input, | 
|  | 120 | input_length, | 
|  | 121 | signature, | 
|  | 122 | signature_size, | 
| gabor-mezei-arm | 256443e | 2021-05-07 15:16:34 +0200 | [diff] [blame] | 123 | signature_length ); | 
|  | 124 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 125 | return( status ); | 
|  | 126 | break; | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 127 | #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-arm | d5218df | 2021-05-12 11:12:25 +0200 | [diff] [blame] | 132 | break; | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 133 | } | 
| gabor-mezei-arm | 256443e | 2021-05-07 15:16:34 +0200 | [diff] [blame] | 134 |  | 
|  | 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-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
|  | 146 | psa_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-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 157 | 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-arm | 256443e | 2021-05-07 15:16:34 +0200 | [diff] [blame] | 181 | break; | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 182 |  | 
|  | 183 | /* Add cases for opaque driver here */ | 
|  | 184 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
|  | 185 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| gabor-mezei-arm | 2522c0b | 2021-04-27 18:52:51 +0200 | [diff] [blame] | 186 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 187 | 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-arm | 256443e | 2021-05-07 15:16:34 +0200 | [diff] [blame] | 196 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 197 | return( status ); | 
|  | 198 | break; | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 199 | #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-arm | d5218df | 2021-05-12 11:12:25 +0200 | [diff] [blame] | 204 | break; | 
| gabor-mezei-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 205 | } | 
| gabor-mezei-arm | 256443e | 2021-05-07 15:16:34 +0200 | [diff] [blame] | 206 |  | 
|  | 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-arm | 50eac35 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 215 | } | 
|  | 216 |  | 
| Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 217 | psa_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 Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 222 | { | 
| Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 223 | /* 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 Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 228 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) | 
| Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 229 | { | 
|  | 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 Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 234 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 235 | } | 
| Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 236 | 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 Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 240 | } | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 241 | #endif /* PSA_CRYPTO_SE_C */ | 
| Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 242 |  | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 243 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 244 | psa_key_location_t location = | 
|  | 245 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 246 |  | 
|  | 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 Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 252 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 253 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 254 | status = mbedtls_test_transparent_signature_sign_hash( attributes, | 
| Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 255 | key_buffer, | 
|  | 256 | key_buffer_size, | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 257 | 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 Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 265 | return( status ); | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 266 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 267 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 268 | /* Fell through, meaning no accelerator supports this operation */ | 
| gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 269 | 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 Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 278 |  | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 279 | /* Add cases for opaque driver here */ | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 280 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 281 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 282 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 283 | 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 Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 292 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 293 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 294 | default: | 
|  | 295 | /* Key is declared with a lifetime not known to us */ | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 296 | (void)status; | 
|  | 297 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 298 | } | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 299 | } | 
|  | 300 |  | 
| Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 301 | psa_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 Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 306 | { | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 307 | /* 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 Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 312 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 313 | { | 
|  | 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 Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 318 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 319 | } | 
| Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 320 | 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 Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 324 | } | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 325 | #endif /* PSA_CRYPTO_SE_C */ | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 326 |  | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 327 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 328 | psa_key_location_t location = | 
|  | 329 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 330 |  | 
|  | 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 Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 336 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 337 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 338 | 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 Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 347 | /* Declared with fallback == true */ | 
|  | 348 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 349 | return( status ); | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 350 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 351 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
|  | 352 |  | 
| gabor-mezei-arm | 6883fd2 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 353 | 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 Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 361 |  | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 362 | /* Add cases for opaque driver here */ | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 363 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 364 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 365 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 366 | 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 Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 374 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 375 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 376 | default: | 
|  | 377 | /* Key is declared with a lifetime not known to us */ | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 378 | (void)status; | 
|  | 379 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 380 | } | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 381 | } | 
|  | 382 |  | 
| Steven Cooreman | b938b0b | 2021-04-06 13:08:42 +0200 | [diff] [blame] | 383 | /** Get the key buffer size required to store the key material of a key | 
|  | 384 | *  associated with an opaque driver without storage. | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 385 | * | 
| Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 386 | * \param[in] attributes  The key attributes. | 
|  | 387 | * \param[out] key_buffer_size  Minimum buffer size to contain the key material | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 388 | * | 
|  | 389 | * \retval #PSA_SUCCESS | 
| Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 390 | *         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 Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 394 | * \retval #PSA_ERROR_NOT_SUPPORTED | 
| Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 395 | *         The type and/or the size in bits of the key or the combination of | 
|  | 396 | *         the two is not supported. | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 397 | */ | 
| Ronald Cron | 9df74be | 2020-12-05 19:15:23 +0100 | [diff] [blame] | 398 | psa_status_t psa_driver_wrapper_get_key_buffer_size( | 
| Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 399 | const psa_key_attributes_t *attributes, | 
|  | 400 | size_t *key_buffer_size ) | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 401 | { | 
| John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 402 | 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 Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 405 |  | 
| Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 406 | *key_buffer_size = 0; | 
| John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 407 | switch( location ) | 
|  | 408 | { | 
| John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 409 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 410 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 411 | #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 Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 417 | *key_buffer_size = sizeof( psa_drv_slot_number_t ); | 
| Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 418 | return( PSA_SUCCESS ); | 
|  | 419 | } | 
|  | 420 | #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 421 | *key_buffer_size = mbedtls_test_size_function( key_type, key_bits ); | 
| Ronald Cron | 4607c82 | 2021-04-14 10:55:34 +0200 | [diff] [blame] | 422 | return( ( *key_buffer_size != 0 ) ? | 
|  | 423 | PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED ); | 
| John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 424 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 425 |  | 
|  | 426 | default: | 
| Ronald Cron | 9df74be | 2020-12-05 19:15:23 +0100 | [diff] [blame] | 427 | (void)key_type; | 
|  | 428 | (void)key_bits; | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 429 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 430 | } | 
|  | 431 | } | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 432 |  | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 433 | psa_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 Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 436 | { | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 437 | 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 Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 441 | /* 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 Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 446 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 447 | { | 
|  | 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 Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 453 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 454 | } | 
|  | 455 | return( drv->key_management->p_generate( | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 456 | drv_context, | 
|  | 457 | *( (psa_key_slot_number_t *)key_buffer ), | 
| Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 458 | attributes, NULL, 0, &pubkey_length ) ); | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 459 | } | 
|  | 460 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 461 |  | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 462 | switch( location ) | 
|  | 463 | { | 
|  | 464 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 465 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 466 | /* Transparent drivers are limited to generating asymmetric keys */ | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 467 | if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) ) | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 468 | { | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 469 | /* Cycle through all known transparent accelerators */ | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 470 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 471 | status = mbedtls_test_transparent_generate_key( | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 472 | 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 Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 477 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 478 | } | 
|  | 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 Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 484 | break; | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 485 |  | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 486 | /* Add cases for opaque driver here */ | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 487 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 488 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 489 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 490 | status = mbedtls_test_opaque_generate_key( | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 491 | attributes, key_buffer, key_buffer_size, key_buffer_length ); | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 492 | break; | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 493 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 494 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
|  | 495 |  | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 496 | default: | 
|  | 497 | /* Key is declared with a lifetime not known to us */ | 
|  | 498 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 499 | break; | 
|  | 500 | } | 
|  | 501 |  | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 502 | return( status ); | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 503 | } | 
|  | 504 |  | 
| Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame] | 505 | psa_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 Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 513 | { | 
| Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 514 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 515 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( | 
|  | 516 | psa_get_key_lifetime( attributes ) ); | 
| Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 517 |  | 
| Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 518 | /* 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 Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 547 | 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 Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 554 | status = mbedtls_test_transparent_import_key( | 
|  | 555 | attributes, | 
|  | 556 | data, data_length, | 
|  | 557 | key_buffer, key_buffer_size, | 
|  | 558 | key_buffer_length, bits ); | 
| Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 559 | /* 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 Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 571 | /* Importing a key with external storage in not yet supported. | 
|  | 572 | * Return in error indicating that the lifetime is not valid. */ | 
| Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 573 | (void)status; | 
| Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 574 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 575 | } | 
|  | 576 |  | 
| Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 577 | } | 
|  | 578 |  | 
| Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 579 | psa_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 Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 589 | /* 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 Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 598 | { | 
| Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 599 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 600 | } | 
| Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 601 |  | 
|  | 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 Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 609 | 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 Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 622 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 623 | return( mbedtls_test_opaque_export_key( attributes, | 
|  | 624 | key_buffer, | 
|  | 625 | key_buffer_size, | 
|  | 626 | data, | 
|  | 627 | data_size, | 
|  | 628 | data_length ) ); | 
| Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 629 | #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 Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 637 | psa_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 Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 642 | { | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 643 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; | 
| Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 644 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( | 
|  | 645 | psa_get_key_lifetime( attributes ) ); | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 646 |  | 
| Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 647 | /* 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 Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 656 | { | 
| Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 657 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 658 | } | 
| Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 659 |  | 
|  | 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 Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 667 | 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 Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 672 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 673 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 674 | 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 Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 681 | /* Declared with fallback == true */ | 
|  | 682 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 683 | return( status ); | 
|  | 684 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 685 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 686 | /* Fell through, meaning no accelerator supports this operation */ | 
| Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 687 | return( psa_export_public_key_internal( attributes, | 
|  | 688 | key_buffer, | 
|  | 689 | key_buffer_size, | 
|  | 690 | data, | 
|  | 691 | data_size, | 
|  | 692 | data_length ) ); | 
|  | 693 |  | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 694 | /* Add cases for opaque driver here */ | 
| Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 695 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 696 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 697 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 698 | return( mbedtls_test_opaque_export_public_key( attributes, | 
|  | 699 | key_buffer, | 
|  | 700 | key_buffer_size, | 
|  | 701 | data, | 
|  | 702 | data_size, | 
|  | 703 | data_length ) ); | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 704 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 705 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 706 | default: | 
|  | 707 | /* Key is declared with a lifetime not known to us */ | 
|  | 708 | return( status ); | 
|  | 709 | } | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 710 | } | 
|  | 711 |  | 
| Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 712 | psa_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 Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 720 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 721 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 722 | return( mbedtls_test_opaque_get_builtin_key( | 
| Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 723 | slot_number, | 
|  | 724 | attributes, | 
|  | 725 | key_buffer, key_buffer_size, key_buffer_length ) ); | 
| Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 726 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 727 | 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 Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 736 | /* | 
|  | 737 | * Cipher functions | 
|  | 738 | */ | 
|  | 739 | psa_status_t psa_driver_wrapper_cipher_encrypt( | 
|  | 740 | psa_key_slot_t *slot, | 
|  | 741 | psa_algorithm_t alg, | 
|  | 742 | const uint8_t *input, | 
|  | 743 | size_t input_length, | 
|  | 744 | uint8_t *output, | 
|  | 745 | size_t output_size, | 
|  | 746 | size_t *output_length ) | 
|  | 747 | { | 
|  | 748 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
|  | 749 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 750 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); | 
|  | 751 | psa_key_attributes_t attributes = { | 
|  | 752 | .core = slot->attr | 
|  | 753 | }; | 
|  | 754 |  | 
|  | 755 | switch( location ) | 
|  | 756 | { | 
|  | 757 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
|  | 758 | /* Key is stored in the slot in export representation, so | 
|  | 759 | * cycle through all known transparent accelerators */ | 
|  | 760 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 761 | status = mbedtls_test_transparent_cipher_encrypt( &attributes, | 
|  | 762 | slot->key.data, | 
|  | 763 | slot->key.bytes, | 
|  | 764 | alg, | 
|  | 765 | input, | 
|  | 766 | input_length, | 
|  | 767 | output, | 
|  | 768 | output_size, | 
|  | 769 | output_length ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 770 | /* Declared with fallback == true */ | 
|  | 771 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 772 | return( status ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 773 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 774 | /* Fell through, meaning no accelerator supports this operation */ | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 775 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 776 | /* Add cases for opaque driver here */ | 
|  | 777 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 778 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 779 | return( mbedtls_test_opaque_cipher_encrypt( &attributes, | 
|  | 780 | slot->key.data, | 
|  | 781 | slot->key.bytes, | 
|  | 782 | alg, | 
|  | 783 | input, | 
|  | 784 | input_length, | 
|  | 785 | output, | 
|  | 786 | output_size, | 
|  | 787 | output_length ) ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 788 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 789 | default: | 
|  | 790 | /* Key is declared with a lifetime not known to us */ | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 791 | return( status ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 792 | } | 
|  | 793 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ | 
|  | 794 | (void) slot; | 
|  | 795 | (void) alg; | 
|  | 796 | (void) input; | 
|  | 797 | (void) input_length; | 
|  | 798 | (void) output; | 
|  | 799 | (void) output_size; | 
|  | 800 | (void) output_length; | 
|  | 801 |  | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 802 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 803 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ | 
|  | 804 | } | 
|  | 805 |  | 
|  | 806 | psa_status_t psa_driver_wrapper_cipher_decrypt( | 
|  | 807 | psa_key_slot_t *slot, | 
|  | 808 | psa_algorithm_t alg, | 
|  | 809 | const uint8_t *input, | 
|  | 810 | size_t input_length, | 
|  | 811 | uint8_t *output, | 
|  | 812 | size_t output_size, | 
|  | 813 | size_t *output_length ) | 
|  | 814 | { | 
|  | 815 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
|  | 816 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 817 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); | 
|  | 818 | psa_key_attributes_t attributes = { | 
|  | 819 | .core = slot->attr | 
|  | 820 | }; | 
|  | 821 |  | 
|  | 822 | switch( location ) | 
|  | 823 | { | 
|  | 824 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
|  | 825 | /* Key is stored in the slot in export representation, so | 
|  | 826 | * cycle through all known transparent accelerators */ | 
|  | 827 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 828 | status = mbedtls_test_transparent_cipher_decrypt( &attributes, | 
|  | 829 | slot->key.data, | 
|  | 830 | slot->key.bytes, | 
|  | 831 | alg, | 
|  | 832 | input, | 
|  | 833 | input_length, | 
|  | 834 | output, | 
|  | 835 | output_size, | 
|  | 836 | output_length ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 837 | /* Declared with fallback == true */ | 
|  | 838 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 839 | return( status ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 840 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 841 | /* Fell through, meaning no accelerator supports this operation */ | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 842 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 843 | /* Add cases for opaque driver here */ | 
|  | 844 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 845 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 846 | return( mbedtls_test_opaque_cipher_decrypt( &attributes, | 
|  | 847 | slot->key.data, | 
|  | 848 | slot->key.bytes, | 
|  | 849 | alg, | 
|  | 850 | input, | 
|  | 851 | input_length, | 
|  | 852 | output, | 
|  | 853 | output_size, | 
|  | 854 | output_length ) ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 855 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 856 | default: | 
|  | 857 | /* Key is declared with a lifetime not known to us */ | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 858 | return( status ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 859 | } | 
|  | 860 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ | 
|  | 861 | (void) slot; | 
|  | 862 | (void) alg; | 
|  | 863 | (void) input; | 
|  | 864 | (void) input_length; | 
|  | 865 | (void) output; | 
|  | 866 | (void) output_size; | 
|  | 867 | (void) output_length; | 
|  | 868 |  | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 869 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 870 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ | 
|  | 871 | } | 
|  | 872 |  | 
|  | 873 | psa_status_t psa_driver_wrapper_cipher_encrypt_setup( | 
| Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 874 | psa_cipher_operation_t *operation, | 
|  | 875 | const psa_key_attributes_t *attributes, | 
|  | 876 | const uint8_t *key_buffer, size_t key_buffer_size, | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 877 | psa_algorithm_t alg ) | 
|  | 878 | { | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 879 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 880 | psa_key_location_t location = | 
|  | 881 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 882 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 883 | switch( location ) | 
|  | 884 | { | 
|  | 885 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
|  | 886 | /* Key is stored in the slot in export representation, so | 
|  | 887 | * cycle through all known transparent accelerators */ | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 888 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 889 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 890 | status = mbedtls_test_transparent_cipher_encrypt_setup( | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 891 | &operation->ctx.transparent_test_driver_ctx, | 
|  | 892 | attributes, | 
|  | 893 | key_buffer, | 
|  | 894 | key_buffer_size, | 
|  | 895 | alg ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 896 | /* Declared with fallback == true */ | 
| Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 897 | if( status == PSA_SUCCESS ) | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 898 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; | 
| Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 899 |  | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 900 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 901 | return( status ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 902 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 903 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 904 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 905 | /* Fell through, meaning no accelerator supports this operation */ | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 906 | status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx, | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 907 | attributes, | 
|  | 908 | key_buffer, | 
|  | 909 | key_buffer_size, | 
|  | 910 | alg ); | 
|  | 911 | if( status == PSA_SUCCESS ) | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 912 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 913 |  | 
| Ronald Cron | 7b4154d | 2021-03-19 14:49:41 +0100 | [diff] [blame] | 914 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 915 | return( status ); | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 916 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ | 
|  | 917 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 918 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 919 | /* Add cases for opaque driver here */ | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 920 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 921 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 922 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 923 | status = mbedtls_test_opaque_cipher_encrypt_setup( | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 924 | &operation->ctx.opaque_test_driver_ctx, | 
|  | 925 | attributes, | 
|  | 926 | key_buffer, key_buffer_size, | 
|  | 927 | alg ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 928 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 929 | if( status == PSA_SUCCESS ) | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 930 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 931 |  | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 932 | return( status ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 933 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 934 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 935 | default: | 
|  | 936 | /* Key is declared with a lifetime not known to us */ | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 937 | (void)status; | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 938 | (void)key_buffer; | 
|  | 939 | (void)key_buffer_size; | 
|  | 940 | (void)alg; | 
| Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 941 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 942 | } | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 943 | } | 
|  | 944 |  | 
|  | 945 | psa_status_t psa_driver_wrapper_cipher_decrypt_setup( | 
| Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 946 | psa_cipher_operation_t *operation, | 
|  | 947 | const psa_key_attributes_t *attributes, | 
|  | 948 | const uint8_t *key_buffer, size_t key_buffer_size, | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 949 | psa_algorithm_t alg ) | 
|  | 950 | { | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 951 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; | 
| Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 952 | psa_key_location_t location = | 
|  | 953 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 954 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 955 | switch( location ) | 
|  | 956 | { | 
|  | 957 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
|  | 958 | /* Key is stored in the slot in export representation, so | 
|  | 959 | * cycle through all known transparent accelerators */ | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 960 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 961 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 962 | status = mbedtls_test_transparent_cipher_decrypt_setup( | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 963 | &operation->ctx.transparent_test_driver_ctx, | 
|  | 964 | attributes, | 
|  | 965 | key_buffer, | 
|  | 966 | key_buffer_size, | 
|  | 967 | alg ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 968 | /* Declared with fallback == true */ | 
| Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 969 | if( status == PSA_SUCCESS ) | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 970 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; | 
| Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 971 |  | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 972 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 973 | return( status ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 974 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 975 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 976 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 977 | /* Fell through, meaning no accelerator supports this operation */ | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 978 | status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx, | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 979 | attributes, | 
|  | 980 | key_buffer, | 
|  | 981 | key_buffer_size, | 
|  | 982 | alg ); | 
|  | 983 | if( status == PSA_SUCCESS ) | 
|  | 984 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; | 
|  | 985 |  | 
|  | 986 | return( status ); | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 987 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ | 
|  | 988 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 989 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 990 | /* Add cases for opaque driver here */ | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 991 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 992 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 993 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 994 | status = mbedtls_test_opaque_cipher_decrypt_setup( | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 995 | &operation->ctx.opaque_test_driver_ctx, | 
|  | 996 | attributes, | 
|  | 997 | key_buffer, key_buffer_size, | 
|  | 998 | alg ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 999 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1000 | if( status == PSA_SUCCESS ) | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1001 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1002 |  | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1003 | return( status ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1004 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1005 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1006 | default: | 
|  | 1007 | /* Key is declared with a lifetime not known to us */ | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1008 | (void)status; | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1009 | (void)key_buffer; | 
|  | 1010 | (void)key_buffer_size; | 
|  | 1011 | (void)alg; | 
| Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 1012 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1013 | } | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1014 | } | 
|  | 1015 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1016 | psa_status_t psa_driver_wrapper_cipher_set_iv( | 
| Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 1017 | psa_cipher_operation_t *operation, | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1018 | const uint8_t *iv, | 
|  | 1019 | size_t iv_length ) | 
|  | 1020 | { | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1021 | switch( operation->id ) | 
|  | 1022 | { | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1023 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1024 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 1025 | return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx, | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1026 | iv, | 
|  | 1027 | iv_length ) ); | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1028 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1029 |  | 
|  | 1030 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1031 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 1032 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1033 | return( mbedtls_test_transparent_cipher_set_iv( | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1034 | &operation->ctx.transparent_test_driver_ctx, | 
|  | 1035 | iv, iv_length ) ); | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1036 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1037 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1038 | return( mbedtls_test_opaque_cipher_set_iv( | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1039 | &operation->ctx.opaque_test_driver_ctx, | 
|  | 1040 | iv, iv_length ) ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1041 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1042 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1043 | } | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1044 |  | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1045 | (void)iv; | 
|  | 1046 | (void)iv_length; | 
|  | 1047 |  | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1048 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1049 | } | 
|  | 1050 |  | 
|  | 1051 | psa_status_t psa_driver_wrapper_cipher_update( | 
| Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 1052 | psa_cipher_operation_t *operation, | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1053 | const uint8_t *input, | 
|  | 1054 | size_t input_length, | 
|  | 1055 | uint8_t *output, | 
|  | 1056 | size_t output_size, | 
|  | 1057 | size_t *output_length ) | 
|  | 1058 | { | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1059 | switch( operation->id ) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1060 | { | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1061 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1062 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 1063 | return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx, | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1064 | input, | 
|  | 1065 | input_length, | 
|  | 1066 | output, | 
|  | 1067 | output_size, | 
|  | 1068 | output_length ) ); | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1069 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ | 
|  | 1070 |  | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1071 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1072 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 1073 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1074 | return( mbedtls_test_transparent_cipher_update( | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1075 | &operation->ctx.transparent_test_driver_ctx, | 
|  | 1076 | input, input_length, | 
|  | 1077 | output, output_size, output_length ) ); | 
|  | 1078 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1079 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1080 | return( mbedtls_test_opaque_cipher_update( | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1081 | &operation->ctx.opaque_test_driver_ctx, | 
|  | 1082 | input, input_length, | 
|  | 1083 | output, output_size, output_length ) ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1084 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1085 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1086 | } | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1087 |  | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1088 | (void)input; | 
|  | 1089 | (void)input_length; | 
|  | 1090 | (void)output; | 
|  | 1091 | (void)output_size; | 
|  | 1092 | (void)output_length; | 
|  | 1093 |  | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1094 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1095 | } | 
|  | 1096 |  | 
|  | 1097 | psa_status_t psa_driver_wrapper_cipher_finish( | 
| Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 1098 | psa_cipher_operation_t *operation, | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1099 | uint8_t *output, | 
|  | 1100 | size_t output_size, | 
|  | 1101 | size_t *output_length ) | 
|  | 1102 | { | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1103 | switch( operation->id ) | 
|  | 1104 | { | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1105 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1106 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 1107 | return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx, | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1108 | output, | 
|  | 1109 | output_size, | 
|  | 1110 | output_length ) ); | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1111 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1112 |  | 
|  | 1113 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1114 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 1115 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1116 | return( mbedtls_test_transparent_cipher_finish( | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1117 | &operation->ctx.transparent_test_driver_ctx, | 
|  | 1118 | output, output_size, output_length ) ); | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1119 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1120 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1121 | return( mbedtls_test_opaque_cipher_finish( | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1122 | &operation->ctx.opaque_test_driver_ctx, | 
|  | 1123 | output, output_size, output_length ) ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1124 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1125 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1126 | } | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1127 |  | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1128 | (void)output; | 
|  | 1129 | (void)output_size; | 
|  | 1130 | (void)output_length; | 
|  | 1131 |  | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1132 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1133 | } | 
|  | 1134 |  | 
|  | 1135 | psa_status_t psa_driver_wrapper_cipher_abort( | 
| Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 1136 | psa_cipher_operation_t *operation ) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1137 | { | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1138 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1139 |  | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1140 | switch( operation->id ) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1141 | { | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1142 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1143 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 1144 | return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) ); | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1145 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1146 |  | 
|  | 1147 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1148 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 1149 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1150 | status = mbedtls_test_transparent_cipher_abort( | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1151 | &operation->ctx.transparent_test_driver_ctx ); | 
| Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1152 | mbedtls_platform_zeroize( | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1153 | &operation->ctx.transparent_test_driver_ctx, | 
|  | 1154 | sizeof( operation->ctx.transparent_test_driver_ctx ) ); | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1155 | return( status ); | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1156 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1157 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1158 | status = mbedtls_test_opaque_cipher_abort( | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1159 | &operation->ctx.opaque_test_driver_ctx ); | 
| Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1160 | mbedtls_platform_zeroize( | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1161 | &operation->ctx.opaque_test_driver_ctx, | 
|  | 1162 | sizeof( operation->ctx.opaque_test_driver_ctx ) ); | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1163 | return( status ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1164 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1165 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1166 | } | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1167 |  | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1168 | (void)status; | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1169 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1170 | } | 
|  | 1171 |  | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1172 | /* | 
|  | 1173 | * Hashing functions | 
|  | 1174 | */ | 
|  | 1175 | psa_status_t psa_driver_wrapper_hash_compute( | 
|  | 1176 | psa_algorithm_t alg, | 
|  | 1177 | const uint8_t *input, | 
|  | 1178 | size_t input_length, | 
|  | 1179 | uint8_t *hash, | 
|  | 1180 | size_t hash_size, | 
|  | 1181 | size_t *hash_length) | 
|  | 1182 | { | 
| Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1183 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1184 |  | 
|  | 1185 | /* Try accelerators first */ | 
| Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1186 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1187 | status = mbedtls_test_transparent_hash_compute( | 
| Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1188 | alg, input, input_length, hash, hash_size, hash_length ); | 
| Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1189 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 1190 | return( status ); | 
|  | 1191 | #endif | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1192 |  | 
|  | 1193 | /* If software fallback is compiled in, try fallback */ | 
|  | 1194 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) | 
|  | 1195 | status = mbedtls_psa_hash_compute( alg, input, input_length, | 
|  | 1196 | hash, hash_size, hash_length ); | 
|  | 1197 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 1198 | return( status ); | 
|  | 1199 | #endif | 
| Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1200 | (void) status; | 
|  | 1201 | (void) alg; | 
|  | 1202 | (void) input; | 
|  | 1203 | (void) input_length; | 
|  | 1204 | (void) hash; | 
|  | 1205 | (void) hash_size; | 
|  | 1206 | (void) hash_length; | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1207 |  | 
| Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1208 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1209 | } | 
|  | 1210 |  | 
|  | 1211 | psa_status_t psa_driver_wrapper_hash_setup( | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1212 | psa_hash_operation_t *operation, | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1213 | psa_algorithm_t alg ) | 
|  | 1214 | { | 
| Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1215 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1216 |  | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1217 | /* Try setup on accelerators first */ | 
| Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1218 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1219 | status = mbedtls_test_transparent_hash_setup( | 
| Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1220 | &operation->ctx.test_driver_ctx, alg ); | 
| Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1221 | if( status == PSA_SUCCESS ) | 
|  | 1222 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; | 
|  | 1223 |  | 
|  | 1224 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 1225 | return( status ); | 
|  | 1226 | #endif | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1227 |  | 
|  | 1228 | /* If software fallback is compiled in, try fallback */ | 
|  | 1229 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1230 | status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg ); | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1231 | if( status == PSA_SUCCESS ) | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1232 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1233 |  | 
|  | 1234 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 1235 | return( status ); | 
|  | 1236 | #endif | 
|  | 1237 | /* Nothing left to try if we fall through here */ | 
|  | 1238 | (void) status; | 
|  | 1239 | (void) operation; | 
|  | 1240 | (void) alg; | 
|  | 1241 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 1242 | } | 
|  | 1243 |  | 
|  | 1244 | psa_status_t psa_driver_wrapper_hash_clone( | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1245 | const psa_hash_operation_t *source_operation, | 
|  | 1246 | psa_hash_operation_t *target_operation ) | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1247 | { | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1248 | switch( source_operation->id ) | 
|  | 1249 | { | 
| Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1250 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) | 
|  | 1251 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: | 
|  | 1252 | target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; | 
|  | 1253 | return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx, | 
|  | 1254 | &target_operation->ctx.mbedtls_ctx ) ); | 
|  | 1255 | #endif | 
| Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1256 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1257 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: | 
|  | 1258 | target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1259 | return( mbedtls_test_transparent_hash_clone( | 
| Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1260 | &source_operation->ctx.test_driver_ctx, | 
|  | 1261 | &target_operation->ctx.test_driver_ctx ) ); | 
| Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1262 | #endif | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1263 | default: | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1264 | (void) target_operation; | 
|  | 1265 | return( PSA_ERROR_BAD_STATE ); | 
|  | 1266 | } | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1267 | } | 
|  | 1268 |  | 
|  | 1269 | psa_status_t psa_driver_wrapper_hash_update( | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1270 | psa_hash_operation_t *operation, | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1271 | const uint8_t *input, | 
|  | 1272 | size_t input_length ) | 
|  | 1273 | { | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1274 | switch( operation->id ) | 
|  | 1275 | { | 
| Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1276 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) | 
|  | 1277 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: | 
|  | 1278 | return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx, | 
|  | 1279 | input, input_length ) ); | 
|  | 1280 | #endif | 
| Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1281 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1282 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1283 | return( mbedtls_test_transparent_hash_update( | 
| Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1284 | &operation->ctx.test_driver_ctx, | 
|  | 1285 | input, input_length ) ); | 
| Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1286 | #endif | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1287 | default: | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1288 | (void) input; | 
|  | 1289 | (void) input_length; | 
|  | 1290 | return( PSA_ERROR_BAD_STATE ); | 
|  | 1291 | } | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1292 | } | 
|  | 1293 |  | 
|  | 1294 | psa_status_t psa_driver_wrapper_hash_finish( | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1295 | psa_hash_operation_t *operation, | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1296 | uint8_t *hash, | 
|  | 1297 | size_t hash_size, | 
|  | 1298 | size_t *hash_length ) | 
|  | 1299 | { | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1300 | switch( operation->id ) | 
|  | 1301 | { | 
| Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1302 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) | 
|  | 1303 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: | 
|  | 1304 | return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx, | 
|  | 1305 | hash, hash_size, hash_length ) ); | 
|  | 1306 | #endif | 
| Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1307 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1308 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1309 | return( mbedtls_test_transparent_hash_finish( | 
| Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1310 | &operation->ctx.test_driver_ctx, | 
|  | 1311 | hash, hash_size, hash_length ) ); | 
| Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1312 | #endif | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1313 | default: | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1314 | (void) hash; | 
|  | 1315 | (void) hash_size; | 
|  | 1316 | (void) hash_length; | 
|  | 1317 | return( PSA_ERROR_BAD_STATE ); | 
|  | 1318 | } | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1319 | } | 
|  | 1320 |  | 
|  | 1321 | psa_status_t psa_driver_wrapper_hash_abort( | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1322 | psa_hash_operation_t *operation ) | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1323 | { | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1324 | switch( operation->id ) | 
|  | 1325 | { | 
| Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1326 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) | 
|  | 1327 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: | 
|  | 1328 | return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) ); | 
|  | 1329 | #endif | 
| Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1330 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1331 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1332 | return( mbedtls_test_transparent_hash_abort( | 
| Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1333 | &operation->ctx.test_driver_ctx ) ); | 
| Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1334 | #endif | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1335 | default: | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1336 | return( PSA_ERROR_BAD_STATE ); | 
|  | 1337 | } | 
|  | 1338 | } | 
|  | 1339 |  | 
| Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1340 | psa_status_t psa_driver_wrapper_aead_encrypt( | 
|  | 1341 | const psa_key_attributes_t *attributes, | 
|  | 1342 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 1343 | psa_algorithm_t alg, | 
|  | 1344 | const uint8_t *nonce, size_t nonce_length, | 
|  | 1345 | const uint8_t *additional_data, size_t additional_data_length, | 
|  | 1346 | const uint8_t *plaintext, size_t plaintext_length, | 
|  | 1347 | uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length ) | 
|  | 1348 | { | 
|  | 1349 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 1350 | psa_key_location_t location = | 
|  | 1351 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); | 
|  | 1352 |  | 
|  | 1353 | switch( location ) | 
|  | 1354 | { | 
|  | 1355 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
|  | 1356 | /* Key is stored in the slot in export representation, so | 
|  | 1357 | * cycle through all known transparent accelerators */ | 
|  | 1358 |  | 
|  | 1359 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
|  | 1360 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1361 | status = mbedtls_test_transparent_aead_encrypt( | 
| Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1362 | attributes, key_buffer, key_buffer_size, | 
|  | 1363 | alg, | 
|  | 1364 | nonce, nonce_length, | 
|  | 1365 | additional_data, additional_data_length, | 
|  | 1366 | plaintext, plaintext_length, | 
|  | 1367 | ciphertext, ciphertext_size, ciphertext_length ); | 
|  | 1368 | /* Declared with fallback == true */ | 
|  | 1369 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 1370 | return( status ); | 
|  | 1371 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 1372 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
|  | 1373 |  | 
|  | 1374 | /* Fell through, meaning no accelerator supports this operation */ | 
|  | 1375 | return( mbedtls_psa_aead_encrypt( | 
|  | 1376 | attributes, key_buffer, key_buffer_size, | 
|  | 1377 | alg, | 
|  | 1378 | nonce, nonce_length, | 
|  | 1379 | additional_data, additional_data_length, | 
|  | 1380 | plaintext, plaintext_length, | 
|  | 1381 | ciphertext, ciphertext_size, ciphertext_length ) ); | 
|  | 1382 |  | 
|  | 1383 | /* Add cases for opaque driver here */ | 
|  | 1384 |  | 
|  | 1385 | default: | 
|  | 1386 | /* Key is declared with a lifetime not known to us */ | 
|  | 1387 | (void)status; | 
|  | 1388 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1389 | } | 
|  | 1390 | } | 
|  | 1391 |  | 
|  | 1392 | psa_status_t psa_driver_wrapper_aead_decrypt( | 
|  | 1393 | const psa_key_attributes_t *attributes, | 
|  | 1394 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 1395 | psa_algorithm_t alg, | 
|  | 1396 | const uint8_t *nonce, size_t nonce_length, | 
|  | 1397 | const uint8_t *additional_data, size_t additional_data_length, | 
|  | 1398 | const uint8_t *ciphertext, size_t ciphertext_length, | 
|  | 1399 | uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length ) | 
|  | 1400 | { | 
|  | 1401 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 1402 | psa_key_location_t location = | 
|  | 1403 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); | 
|  | 1404 |  | 
|  | 1405 | switch( location ) | 
|  | 1406 | { | 
|  | 1407 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
|  | 1408 | /* Key is stored in the slot in export representation, so | 
|  | 1409 | * cycle through all known transparent accelerators */ | 
|  | 1410 |  | 
|  | 1411 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
|  | 1412 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Ronald Cron | 7f13fa2 | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1413 | status = mbedtls_test_transparent_aead_decrypt( | 
| Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1414 | attributes, key_buffer, key_buffer_size, | 
|  | 1415 | alg, | 
|  | 1416 | nonce, nonce_length, | 
|  | 1417 | additional_data, additional_data_length, | 
|  | 1418 | ciphertext, ciphertext_length, | 
|  | 1419 | plaintext, plaintext_size, plaintext_length ); | 
|  | 1420 | /* Declared with fallback == true */ | 
|  | 1421 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 1422 | return( status ); | 
|  | 1423 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 1424 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
|  | 1425 |  | 
|  | 1426 | /* Fell through, meaning no accelerator supports this operation */ | 
|  | 1427 | return( mbedtls_psa_aead_decrypt( | 
|  | 1428 | attributes, key_buffer, key_buffer_size, | 
|  | 1429 | alg, | 
|  | 1430 | nonce, nonce_length, | 
|  | 1431 | additional_data, additional_data_length, | 
|  | 1432 | ciphertext, ciphertext_length, | 
|  | 1433 | plaintext, plaintext_size, plaintext_length ) ); | 
|  | 1434 |  | 
|  | 1435 | /* Add cases for opaque driver here */ | 
|  | 1436 |  | 
|  | 1437 | default: | 
|  | 1438 | /* Key is declared with a lifetime not known to us */ | 
|  | 1439 | (void)status; | 
|  | 1440 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1441 | } | 
|  | 1442 | } | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1443 |  | 
|  | 1444 |  | 
|  | 1445 | /* | 
|  | 1446 | * MAC functions | 
|  | 1447 | */ | 
|  | 1448 | psa_status_t psa_driver_wrapper_mac_compute( | 
|  | 1449 | const psa_key_attributes_t *attributes, | 
|  | 1450 | const uint8_t *key_buffer, | 
|  | 1451 | size_t key_buffer_size, | 
|  | 1452 | psa_algorithm_t alg, | 
|  | 1453 | const uint8_t *input, | 
|  | 1454 | size_t input_length, | 
|  | 1455 | uint8_t *mac, | 
|  | 1456 | size_t mac_size, | 
|  | 1457 | size_t *mac_length ) | 
|  | 1458 | { | 
|  | 1459 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 1460 | psa_key_location_t location = | 
|  | 1461 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); | 
|  | 1462 |  | 
|  | 1463 | switch( location ) | 
|  | 1464 | { | 
|  | 1465 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
|  | 1466 | /* Key is stored in the slot in export representation, so | 
|  | 1467 | * cycle through all known transparent accelerators */ | 
|  | 1468 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
|  | 1469 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | c7f0a57 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1470 | status = mbedtls_test_transparent_mac_compute( | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1471 | attributes, key_buffer, key_buffer_size, alg, | 
|  | 1472 | input, input_length, | 
|  | 1473 | mac, mac_size, mac_length ); | 
|  | 1474 | /* Declared with fallback == true */ | 
|  | 1475 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 1476 | return( status ); | 
|  | 1477 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 1478 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
|  | 1479 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) | 
|  | 1480 | /* Fell through, meaning no accelerator supports this operation */ | 
|  | 1481 | status = mbedtls_psa_mac_compute( | 
|  | 1482 | attributes, key_buffer, key_buffer_size, alg, | 
|  | 1483 | input, input_length, | 
|  | 1484 | mac, mac_size, mac_length ); | 
|  | 1485 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 1486 | return( status ); | 
|  | 1487 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ | 
|  | 1488 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 1489 |  | 
|  | 1490 | /* Add cases for opaque driver here */ | 
|  | 1491 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
|  | 1492 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 1493 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Steven Cooreman | c7f0a57 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1494 | status = mbedtls_test_opaque_mac_compute( | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1495 | attributes, key_buffer, key_buffer_size, alg, | 
|  | 1496 | input, input_length, | 
|  | 1497 | mac, mac_size, mac_length ); | 
|  | 1498 | return( status ); | 
|  | 1499 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 1500 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
|  | 1501 | default: | 
|  | 1502 | /* Key is declared with a lifetime not known to us */ | 
|  | 1503 | (void) key_buffer; | 
|  | 1504 | (void) key_buffer_size; | 
|  | 1505 | (void) alg; | 
|  | 1506 | (void) input; | 
|  | 1507 | (void) input_length; | 
|  | 1508 | (void) mac; | 
|  | 1509 | (void) mac_size; | 
|  | 1510 | (void) mac_length; | 
|  | 1511 | (void) status; | 
|  | 1512 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1513 | } | 
|  | 1514 | } | 
|  | 1515 |  | 
|  | 1516 | psa_status_t psa_driver_wrapper_mac_sign_setup( | 
|  | 1517 | psa_mac_operation_t *operation, | 
|  | 1518 | const psa_key_attributes_t *attributes, | 
|  | 1519 | const uint8_t *key_buffer, | 
|  | 1520 | size_t key_buffer_size, | 
|  | 1521 | psa_algorithm_t alg ) | 
|  | 1522 | { | 
|  | 1523 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 1524 | psa_key_location_t location = | 
|  | 1525 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); | 
|  | 1526 |  | 
|  | 1527 | switch( location ) | 
|  | 1528 | { | 
|  | 1529 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
|  | 1530 | /* Key is stored in the slot in export representation, so | 
|  | 1531 | * cycle through all known transparent accelerators */ | 
|  | 1532 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
|  | 1533 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | c7f0a57 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1534 | status = mbedtls_test_transparent_mac_sign_setup( | 
| Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1535 | &operation->ctx.transparent_test_driver_ctx, | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1536 | attributes, | 
|  | 1537 | key_buffer, key_buffer_size, | 
|  | 1538 | alg ); | 
|  | 1539 | /* Declared with fallback == true */ | 
|  | 1540 | if( status == PSA_SUCCESS ) | 
|  | 1541 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; | 
|  | 1542 |  | 
|  | 1543 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 1544 | return( status ); | 
|  | 1545 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 1546 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
|  | 1547 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) | 
|  | 1548 | /* Fell through, meaning no accelerator supports this operation */ | 
| Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1549 | status = mbedtls_psa_mac_sign_setup( &operation->ctx.mbedtls_ctx, | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1550 | attributes, | 
|  | 1551 | key_buffer, key_buffer_size, | 
|  | 1552 | alg ); | 
|  | 1553 | if( status == PSA_SUCCESS ) | 
|  | 1554 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; | 
|  | 1555 |  | 
|  | 1556 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 1557 | return( status ); | 
|  | 1558 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ | 
|  | 1559 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 1560 |  | 
|  | 1561 | /* Add cases for opaque driver here */ | 
|  | 1562 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
|  | 1563 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 1564 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Steven Cooreman | c7f0a57 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1565 | status = mbedtls_test_opaque_mac_sign_setup( | 
| Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1566 | &operation->ctx.opaque_test_driver_ctx, | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1567 | attributes, | 
|  | 1568 | key_buffer, key_buffer_size, | 
|  | 1569 | alg ); | 
|  | 1570 |  | 
|  | 1571 | if( status == PSA_SUCCESS ) | 
|  | 1572 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; | 
|  | 1573 |  | 
|  | 1574 | return( status ); | 
|  | 1575 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 1576 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
|  | 1577 | default: | 
|  | 1578 | /* Key is declared with a lifetime not known to us */ | 
|  | 1579 | (void) status; | 
|  | 1580 | (void) key_buffer; | 
|  | 1581 | (void) key_buffer_size; | 
|  | 1582 | (void) alg; | 
|  | 1583 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1584 | } | 
|  | 1585 | } | 
|  | 1586 |  | 
|  | 1587 | psa_status_t psa_driver_wrapper_mac_verify_setup( | 
|  | 1588 | psa_mac_operation_t *operation, | 
|  | 1589 | const psa_key_attributes_t *attributes, | 
|  | 1590 | const uint8_t *key_buffer, | 
|  | 1591 | size_t key_buffer_size, | 
|  | 1592 | psa_algorithm_t alg ) | 
|  | 1593 | { | 
|  | 1594 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 1595 | psa_key_location_t location = | 
|  | 1596 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); | 
|  | 1597 |  | 
|  | 1598 | switch( location ) | 
|  | 1599 | { | 
|  | 1600 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
|  | 1601 | /* Key is stored in the slot in export representation, so | 
|  | 1602 | * cycle through all known transparent accelerators */ | 
|  | 1603 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
|  | 1604 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | c7f0a57 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1605 | status = mbedtls_test_transparent_mac_verify_setup( | 
| Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1606 | &operation->ctx.transparent_test_driver_ctx, | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1607 | attributes, | 
|  | 1608 | key_buffer, key_buffer_size, | 
|  | 1609 | alg ); | 
|  | 1610 | /* Declared with fallback == true */ | 
|  | 1611 | if( status == PSA_SUCCESS ) | 
|  | 1612 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; | 
|  | 1613 |  | 
|  | 1614 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 1615 | return( status ); | 
|  | 1616 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 1617 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
|  | 1618 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) | 
|  | 1619 | /* Fell through, meaning no accelerator supports this operation */ | 
| Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1620 | status = mbedtls_psa_mac_verify_setup( &operation->ctx.mbedtls_ctx, | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1621 | attributes, | 
|  | 1622 | key_buffer, key_buffer_size, | 
|  | 1623 | alg ); | 
|  | 1624 | if( status == PSA_SUCCESS ) | 
|  | 1625 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; | 
|  | 1626 |  | 
|  | 1627 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 1628 | return( status ); | 
|  | 1629 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ | 
|  | 1630 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 1631 |  | 
|  | 1632 | /* Add cases for opaque driver here */ | 
|  | 1633 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
|  | 1634 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 1635 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Steven Cooreman | c7f0a57 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1636 | status = mbedtls_test_opaque_mac_verify_setup( | 
| Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1637 | &operation->ctx.opaque_test_driver_ctx, | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1638 | attributes, | 
|  | 1639 | key_buffer, key_buffer_size, | 
|  | 1640 | alg ); | 
|  | 1641 |  | 
|  | 1642 | if( status == PSA_SUCCESS ) | 
|  | 1643 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; | 
|  | 1644 |  | 
|  | 1645 | return( status ); | 
|  | 1646 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 1647 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
|  | 1648 | default: | 
|  | 1649 | /* Key is declared with a lifetime not known to us */ | 
|  | 1650 | (void) status; | 
|  | 1651 | (void) key_buffer; | 
|  | 1652 | (void) key_buffer_size; | 
|  | 1653 | (void) alg; | 
|  | 1654 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1655 | } | 
|  | 1656 | } | 
|  | 1657 |  | 
|  | 1658 | psa_status_t psa_driver_wrapper_mac_update( | 
|  | 1659 | psa_mac_operation_t *operation, | 
|  | 1660 | const uint8_t *input, | 
|  | 1661 | size_t input_length ) | 
|  | 1662 | { | 
|  | 1663 | switch( operation->id ) | 
|  | 1664 | { | 
|  | 1665 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) | 
|  | 1666 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: | 
| Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1667 | return( mbedtls_psa_mac_update( &operation->ctx.mbedtls_ctx, | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1668 | input, input_length ) ); | 
|  | 1669 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ | 
|  | 1670 |  | 
|  | 1671 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
|  | 1672 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 1673 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: | 
| Steven Cooreman | c7f0a57 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1674 | return( mbedtls_test_transparent_mac_update( | 
| Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1675 | &operation->ctx.transparent_test_driver_ctx, | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1676 | input, input_length ) ); | 
|  | 1677 |  | 
|  | 1678 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: | 
| Steven Cooreman | c7f0a57 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1679 | return( mbedtls_test_opaque_mac_update( | 
| Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1680 | &operation->ctx.opaque_test_driver_ctx, | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1681 | input, input_length ) ); | 
|  | 1682 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 1683 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
|  | 1684 | default: | 
|  | 1685 | (void) input; | 
|  | 1686 | (void) input_length; | 
|  | 1687 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1688 | } | 
|  | 1689 | } | 
|  | 1690 |  | 
|  | 1691 | psa_status_t psa_driver_wrapper_mac_sign_finish( | 
|  | 1692 | psa_mac_operation_t *operation, | 
|  | 1693 | uint8_t *mac, | 
|  | 1694 | size_t mac_size, | 
|  | 1695 | size_t *mac_length ) | 
|  | 1696 | { | 
|  | 1697 | switch( operation->id ) | 
|  | 1698 | { | 
|  | 1699 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) | 
|  | 1700 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: | 
| Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1701 | return( mbedtls_psa_mac_sign_finish( &operation->ctx.mbedtls_ctx, | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1702 | mac, mac_size, mac_length ) ); | 
|  | 1703 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ | 
|  | 1704 |  | 
|  | 1705 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
|  | 1706 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 1707 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: | 
| Steven Cooreman | c7f0a57 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1708 | return( mbedtls_test_transparent_mac_sign_finish( | 
| Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1709 | &operation->ctx.transparent_test_driver_ctx, | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1710 | mac, mac_size, mac_length ) ); | 
|  | 1711 |  | 
|  | 1712 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: | 
| Steven Cooreman | c7f0a57 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1713 | return( mbedtls_test_opaque_mac_sign_finish( | 
| Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1714 | &operation->ctx.opaque_test_driver_ctx, | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1715 | mac, mac_size, mac_length ) ); | 
|  | 1716 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 1717 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
|  | 1718 | default: | 
|  | 1719 | (void) mac; | 
|  | 1720 | (void) mac_size; | 
|  | 1721 | (void) mac_length; | 
|  | 1722 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1723 | } | 
|  | 1724 | } | 
|  | 1725 |  | 
|  | 1726 | psa_status_t psa_driver_wrapper_mac_verify_finish( | 
|  | 1727 | psa_mac_operation_t *operation, | 
|  | 1728 | const uint8_t *mac, | 
|  | 1729 | size_t mac_length ) | 
|  | 1730 | { | 
|  | 1731 | switch( operation->id ) | 
|  | 1732 | { | 
|  | 1733 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) | 
|  | 1734 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: | 
| Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1735 | return( mbedtls_psa_mac_verify_finish( &operation->ctx.mbedtls_ctx, | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1736 | mac, mac_length ) ); | 
|  | 1737 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ | 
|  | 1738 |  | 
|  | 1739 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
|  | 1740 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 1741 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: | 
| Steven Cooreman | c7f0a57 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1742 | return( mbedtls_test_transparent_mac_verify_finish( | 
| Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1743 | &operation->ctx.transparent_test_driver_ctx, | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1744 | mac, mac_length ) ); | 
|  | 1745 |  | 
|  | 1746 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: | 
| Steven Cooreman | c7f0a57 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1747 | return( mbedtls_test_opaque_mac_verify_finish( | 
| Steven Cooreman | 77e2cc5 | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1748 | &operation->ctx.opaque_test_driver_ctx, | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1749 | mac, mac_length ) ); | 
|  | 1750 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 1751 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
|  | 1752 | default: | 
|  | 1753 | (void) mac; | 
|  | 1754 | (void) mac_length; | 
|  | 1755 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1756 | } | 
|  | 1757 | } | 
|  | 1758 |  | 
|  | 1759 | psa_status_t psa_driver_wrapper_mac_abort( | 
|  | 1760 | psa_mac_operation_t *operation ) | 
|  | 1761 | { | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1762 | switch( operation->id ) | 
|  | 1763 | { | 
|  | 1764 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) | 
|  | 1765 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: | 
| Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 1766 | return( mbedtls_psa_mac_abort( &operation->ctx.mbedtls_ctx ) ); | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 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 Cooreman | c7f0a57 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1772 | return( mbedtls_test_transparent_mac_abort( | 
| Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 1773 | &operation->ctx.transparent_test_driver_ctx ) ); | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1774 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: | 
| Steven Cooreman | c7f0a57 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1775 | return( mbedtls_test_opaque_mac_abort( | 
| Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 1776 | &operation->ctx.opaque_test_driver_ctx ) ); | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1777 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 1778 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
|  | 1779 | default: | 
| Steven Cooreman | e680419 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 1780 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1781 | } | 
| Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1782 | } | 
| Gilles Peskine | 1905a24 | 2021-04-24 13:19:45 +0200 | [diff] [blame] | 1783 |  | 
|  | 1784 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |