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 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 22 | #include "psa_crypto_aead.h" |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 23 | #include "psa_crypto_cipher.h" |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 24 | #include "psa_crypto_core.h" |
| 25 | #include "psa_crypto_driver_wrappers.h" |
Steven Cooreman | 830aff2 | 2021-03-09 09:50:44 +0100 | [diff] [blame] | 26 | #include "psa_crypto_hash.h" |
| 27 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 28 | #include "mbedtls/platform.h" |
| 29 | |
| 30 | #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 31 | |
| 32 | /* Include test driver definition when running tests */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 33 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 34 | #ifndef PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 35 | #define PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 36 | #endif |
| 37 | #ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 38 | #define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 39 | #endif |
Ronald Cron | 05ee58d | 2021-04-30 17:00:34 +0200 | [diff] [blame] | 40 | #include "test/drivers/test_driver.h" |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 41 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 42 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 43 | /* Repeat above block for each JSON-declared driver during autogeneration */ |
Steven Cooreman | aa87fd0 | 2021-03-15 18:54:03 +0100 | [diff] [blame] | 44 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 45 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 46 | /* Auto-generated values depending on which drivers are registered. |
| 47 | * ID 0 is reserved for unallocated operations. |
| 48 | * ID 1 is reserved for the Mbed TLS software driver. */ |
Steven Cooreman | 830aff2 | 2021-03-09 09:50:44 +0100 | [diff] [blame] | 49 | #define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1) |
| 50 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 51 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 52 | #define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2) |
| 53 | #define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 54 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 55 | |
| 56 | /* Support the 'old' SE interface when asked to */ |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 57 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 58 | /* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style |
| 59 | * SE driver is present, to avoid unused argument errors at compile time. */ |
| 60 | #ifndef PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 61 | #define PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 62 | #endif |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 63 | #include "psa_crypto_se.h" |
| 64 | #endif |
| 65 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 66 | /* Start delegation functions */ |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame^] | 67 | psa_status_t psa_driver_wrapper_sign_message( |
| 68 | const psa_key_attributes_t *attributes, |
| 69 | const uint8_t *key_buffer, |
| 70 | size_t key_buffer_size, |
| 71 | psa_algorithm_t alg, |
| 72 | const uint8_t *input, |
| 73 | size_t input_length, |
| 74 | uint8_t *signature, |
| 75 | size_t signature_size, |
| 76 | size_t *signature_length ) |
| 77 | { |
| 78 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 79 | |
| 80 | /* Try dynamically-registered SE interface first */ |
| 81 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 82 | const psa_drv_se_t *drv; |
| 83 | psa_drv_se_context_t *drv_context; |
| 84 | |
| 85 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 86 | { |
| 87 | if( drv->asymmetric == NULL || |
| 88 | drv->asymmetric->p_sign == NULL ) |
| 89 | { |
| 90 | /* Key is defined in SE, but we have no way to exercise it */ |
| 91 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 92 | } |
| 93 | |
| 94 | size_t hash_length; |
| 95 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 96 | |
| 97 | status = psa_driver_wrapper_hash_compute( PSA_ALG_SIGN_GET_HASH( alg ), |
| 98 | input, input_length, |
| 99 | hash, sizeof( hash ), |
| 100 | &hash_length ); |
| 101 | |
| 102 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 103 | return( status ); |
| 104 | |
| 105 | return( drv->asymmetric->p_sign( |
| 106 | drv_context, *( (psa_key_slot_number_t *)key_buffer ), |
| 107 | alg, hash, hash_length, |
| 108 | signature, signature_size, signature_length ) ); |
| 109 | } |
| 110 | #endif /* PSA_CRYPTO_SE_C */ |
| 111 | |
| 112 | psa_key_location_t location = |
| 113 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 114 | |
| 115 | switch( location ) |
| 116 | { |
| 117 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 118 | /* Key is stored in the slot in export representation, so |
| 119 | * cycle through all known transparent accelerators */ |
| 120 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 121 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 122 | status = mbedtls_test_transparent_signature_sign_message( |
| 123 | attributes, |
| 124 | key_buffer, |
| 125 | key_buffer_size, |
| 126 | alg, |
| 127 | input, |
| 128 | input_length, |
| 129 | signature, |
| 130 | signature_size, |
| 131 | signature_length ); |
| 132 | /* Declared with fallback == true */ |
| 133 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 134 | return( status ); |
| 135 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 136 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 137 | /* Fell through, meaning no accelerator supports this operation */ |
| 138 | return( psa_sign_message_internal( attributes, |
| 139 | key_buffer, |
| 140 | key_buffer_size, |
| 141 | alg, |
| 142 | input, |
| 143 | input_length, |
| 144 | signature, |
| 145 | signature_size, |
| 146 | signature_length ) ); |
| 147 | |
| 148 | /* Add cases for opaque driver here */ |
| 149 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 150 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 151 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
| 152 | return( mbedtls_test_opaque_signature_sign_message( |
| 153 | attributes, |
| 154 | key_buffer, |
| 155 | key_buffer_size, |
| 156 | alg, |
| 157 | input, |
| 158 | input_length, |
| 159 | signature, |
| 160 | signature_size, |
| 161 | signature_length ) ); |
| 162 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 163 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 164 | default: |
| 165 | /* Key is declared with a lifetime not known to us */ |
| 166 | (void)status; |
| 167 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | psa_status_t psa_driver_wrapper_verify_message( |
| 172 | const psa_key_attributes_t *attributes, |
| 173 | const uint8_t *key_buffer, |
| 174 | size_t key_buffer_size, |
| 175 | psa_algorithm_t alg, |
| 176 | const uint8_t *input, |
| 177 | size_t input_length, |
| 178 | const uint8_t *signature, |
| 179 | size_t signature_length ) |
| 180 | { |
| 181 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 182 | |
| 183 | /* Try dynamically-registered SE interface first */ |
| 184 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 185 | const psa_drv_se_t *drv; |
| 186 | psa_drv_se_context_t *drv_context; |
| 187 | |
| 188 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 189 | { |
| 190 | if( drv->asymmetric == NULL || |
| 191 | drv->asymmetric->p_verify == NULL ) |
| 192 | { |
| 193 | /* Key is defined in SE, but we have no way to exercise it */ |
| 194 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 195 | } |
| 196 | |
| 197 | size_t hash_length; |
| 198 | uint8_t hash[PSA_HASH_MAX_SIZE]; |
| 199 | |
| 200 | status = psa_driver_wrapper_hash_compute( PSA_ALG_SIGN_GET_HASH( alg ), |
| 201 | input, input_length, |
| 202 | hash, sizeof( hash ), |
| 203 | &hash_length ); |
| 204 | |
| 205 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 206 | return( status ); |
| 207 | |
| 208 | return( drv->asymmetric->p_verify( |
| 209 | drv_context, *( (psa_key_slot_number_t *)key_buffer ), |
| 210 | alg, hash, hash_length, |
| 211 | signature, signature_length ) ); |
| 212 | } |
| 213 | #endif /* PSA_CRYPTO_SE_C */ |
| 214 | |
| 215 | psa_key_location_t location = |
| 216 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 217 | |
| 218 | switch( location ) |
| 219 | { |
| 220 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 221 | /* Key is stored in the slot in export representation, so |
| 222 | * cycle through all known transparent accelerators */ |
| 223 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 224 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 225 | status = mbedtls_test_transparent_signature_verify_message( |
| 226 | attributes, |
| 227 | key_buffer, |
| 228 | key_buffer_size, |
| 229 | alg, |
| 230 | input, |
| 231 | input_length, |
| 232 | signature, |
| 233 | signature_length ); |
| 234 | /* Declared with fallback == true */ |
| 235 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 236 | return( status ); |
| 237 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 238 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 239 | |
| 240 | return( psa_verify_message_internal( attributes, |
| 241 | key_buffer, |
| 242 | key_buffer_size, |
| 243 | alg, |
| 244 | input, |
| 245 | input_length, |
| 246 | signature, |
| 247 | signature_length ) ); |
| 248 | |
| 249 | /* Add cases for opaque driver here */ |
| 250 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 251 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 252 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
| 253 | return( mbedtls_test_opaque_signature_verify_message( |
| 254 | attributes, |
| 255 | key_buffer, |
| 256 | key_buffer_size, |
| 257 | alg, |
| 258 | input, |
| 259 | input_length, |
| 260 | signature, |
| 261 | signature_length ) ); |
| 262 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 263 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 264 | default: |
| 265 | /* Key is declared with a lifetime not known to us */ |
| 266 | (void)status; |
| 267 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 268 | } |
| 269 | } |
| 270 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 271 | psa_status_t psa_driver_wrapper_sign_hash( |
| 272 | const psa_key_attributes_t *attributes, |
| 273 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 274 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 275 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 276 | { |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 277 | /* Try dynamically-registered SE interface first */ |
| 278 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 279 | const psa_drv_se_t *drv; |
| 280 | psa_drv_se_context_t *drv_context; |
| 281 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 282 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 283 | { |
| 284 | if( drv->asymmetric == NULL || |
| 285 | drv->asymmetric->p_sign == NULL ) |
| 286 | { |
| 287 | /* 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] | 288 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 289 | } |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 290 | return( drv->asymmetric->p_sign( |
| 291 | drv_context, *( (psa_key_slot_number_t *)key_buffer ), |
| 292 | alg, hash, hash_length, |
| 293 | signature, signature_size, signature_length ) ); |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 294 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 295 | #endif /* PSA_CRYPTO_SE_C */ |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 296 | |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 297 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 298 | psa_key_location_t location = |
| 299 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 300 | |
| 301 | switch( location ) |
| 302 | { |
| 303 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 304 | /* Key is stored in the slot in export representation, so |
| 305 | * cycle through all known transparent accelerators */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 306 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 307 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 308 | status = mbedtls_test_transparent_signature_sign_hash( attributes, |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 309 | key_buffer, |
| 310 | key_buffer_size, |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 311 | alg, |
| 312 | hash, |
| 313 | hash_length, |
| 314 | signature, |
| 315 | signature_size, |
| 316 | signature_length ); |
| 317 | /* Declared with fallback == true */ |
| 318 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 319 | return( status ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 320 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 321 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 322 | /* Fell through, meaning no accelerator supports this operation */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 323 | return( psa_sign_hash_internal( attributes, |
| 324 | key_buffer, |
| 325 | key_buffer_size, |
| 326 | alg, |
| 327 | hash, |
| 328 | hash_length, |
| 329 | signature, |
| 330 | signature_size, |
| 331 | signature_length ) ); |
| 332 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 333 | /* Add cases for opaque driver here */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 334 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 335 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 336 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 337 | return( mbedtls_test_opaque_signature_sign_hash( attributes, |
| 338 | key_buffer, |
| 339 | key_buffer_size, |
| 340 | alg, |
| 341 | hash, |
| 342 | hash_length, |
| 343 | signature, |
| 344 | signature_size, |
| 345 | signature_length ) ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 346 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 347 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 348 | default: |
| 349 | /* Key is declared with a lifetime not known to us */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 350 | (void)status; |
| 351 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 352 | } |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 353 | } |
| 354 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 355 | psa_status_t psa_driver_wrapper_verify_hash( |
| 356 | const psa_key_attributes_t *attributes, |
| 357 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 358 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 359 | const uint8_t *signature, size_t signature_length ) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 360 | { |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 361 | /* Try dynamically-registered SE interface first */ |
| 362 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 363 | const psa_drv_se_t *drv; |
| 364 | psa_drv_se_context_t *drv_context; |
| 365 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 366 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 367 | { |
| 368 | if( drv->asymmetric == NULL || |
| 369 | drv->asymmetric->p_verify == NULL ) |
| 370 | { |
| 371 | /* 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] | 372 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 373 | } |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 374 | return( drv->asymmetric->p_verify( |
| 375 | drv_context, *( (psa_key_slot_number_t *)key_buffer ), |
| 376 | alg, hash, hash_length, |
| 377 | signature, signature_length ) ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 378 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 379 | #endif /* PSA_CRYPTO_SE_C */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 380 | |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 381 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 382 | psa_key_location_t location = |
| 383 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 384 | |
| 385 | switch( location ) |
| 386 | { |
| 387 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 388 | /* Key is stored in the slot in export representation, so |
| 389 | * cycle through all known transparent accelerators */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 390 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 391 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 392 | status = mbedtls_test_transparent_signature_verify_hash( |
| 393 | attributes, |
| 394 | key_buffer, |
| 395 | key_buffer_size, |
| 396 | alg, |
| 397 | hash, |
| 398 | hash_length, |
| 399 | signature, |
| 400 | signature_length ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 401 | /* Declared with fallback == true */ |
| 402 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 403 | return( status ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 404 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 405 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 406 | |
| 407 | return( psa_verify_hash_internal( attributes, |
| 408 | key_buffer, |
| 409 | key_buffer_size, |
| 410 | alg, |
| 411 | hash, |
| 412 | hash_length, |
| 413 | signature, |
| 414 | signature_length ) ); |
| 415 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 416 | /* Add cases for opaque driver here */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 417 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 418 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 419 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 420 | return( mbedtls_test_opaque_signature_verify_hash( attributes, |
| 421 | key_buffer, |
| 422 | key_buffer_size, |
| 423 | alg, |
| 424 | hash, |
| 425 | hash_length, |
| 426 | signature, |
| 427 | signature_length ) ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 428 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 429 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 430 | default: |
| 431 | /* Key is declared with a lifetime not known to us */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 432 | (void)status; |
| 433 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 434 | } |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 435 | } |
| 436 | |
Steven Cooreman | b938b0b | 2021-04-06 13:08:42 +0200 | [diff] [blame] | 437 | /** Get the key buffer size required to store the key material of a key |
| 438 | * associated with an opaque driver without storage. |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 439 | * |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 440 | * \param[in] attributes The key attributes. |
| 441 | * \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] | 442 | * |
| 443 | * \retval #PSA_SUCCESS |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 444 | * The minimum size for a buffer to contain the key material has been |
| 445 | * returned successfully. |
| 446 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 447 | * The size in bits of the key is not valid. |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 448 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 449 | * The type and/or the size in bits of the key or the combination of |
| 450 | * the two is not supported. |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 451 | */ |
Ronald Cron | 9df74be | 2020-12-05 19:15:23 +0100 | [diff] [blame] | 452 | psa_status_t psa_driver_wrapper_get_key_buffer_size( |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 453 | const psa_key_attributes_t *attributes, |
| 454 | size_t *key_buffer_size ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 455 | { |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 456 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 457 | psa_key_type_t key_type = attributes->core.type; |
| 458 | size_t key_bits = attributes->core.bits; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 459 | |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 460 | *key_buffer_size = 0; |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 461 | switch( location ) |
| 462 | { |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 463 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 464 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 465 | #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS) |
| 466 | /* Emulate property 'builtin_key_size' */ |
| 467 | if( psa_key_id_is_builtin( |
| 468 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( |
| 469 | psa_get_key_id( attributes ) ) ) ) |
| 470 | { |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 471 | *key_buffer_size = sizeof( psa_drv_slot_number_t ); |
Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 472 | return( PSA_SUCCESS ); |
| 473 | } |
| 474 | #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 475 | *key_buffer_size = mbedtls_test_size_function( key_type, key_bits ); |
Ronald Cron | b0737da | 2021-04-14 10:55:34 +0200 | [diff] [blame] | 476 | return( ( *key_buffer_size != 0 ) ? |
| 477 | PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 478 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 479 | |
| 480 | default: |
Ronald Cron | 9df74be | 2020-12-05 19:15:23 +0100 | [diff] [blame] | 481 | (void)key_type; |
| 482 | (void)key_bits; |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 483 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 484 | } |
| 485 | } |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 486 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 487 | psa_status_t psa_driver_wrapper_generate_key( |
| 488 | const psa_key_attributes_t *attributes, |
| 489 | 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] | 490 | { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 491 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 492 | psa_key_location_t location = |
| 493 | PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime); |
| 494 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 495 | /* Try dynamically-registered SE interface first */ |
| 496 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 497 | const psa_drv_se_t *drv; |
| 498 | psa_drv_se_context_t *drv_context; |
| 499 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 500 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 501 | { |
| 502 | size_t pubkey_length = 0; /* We don't support this feature yet */ |
| 503 | if( drv->key_management == NULL || |
| 504 | drv->key_management->p_generate == NULL ) |
| 505 | { |
| 506 | /* 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] | 507 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 508 | } |
| 509 | return( drv->key_management->p_generate( |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 510 | drv_context, |
| 511 | *( (psa_key_slot_number_t *)key_buffer ), |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 512 | attributes, NULL, 0, &pubkey_length ) ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 513 | } |
| 514 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 515 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 516 | switch( location ) |
| 517 | { |
| 518 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 519 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 520 | /* Transparent drivers are limited to generating asymmetric keys */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 521 | if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 522 | { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 523 | /* Cycle through all known transparent accelerators */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 524 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 525 | status = mbedtls_test_transparent_generate_key( |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 526 | attributes, key_buffer, key_buffer_size, |
| 527 | key_buffer_length ); |
| 528 | /* Declared with fallback == true */ |
| 529 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 530 | break; |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 531 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 532 | } |
| 533 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 534 | |
| 535 | /* Software fallback */ |
| 536 | status = psa_generate_key_internal( |
| 537 | attributes, key_buffer, key_buffer_size, key_buffer_length ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 538 | break; |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 539 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 540 | /* Add cases for opaque driver here */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 541 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 542 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 543 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 544 | status = mbedtls_test_opaque_generate_key( |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 545 | attributes, key_buffer, key_buffer_size, key_buffer_length ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 546 | break; |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 547 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 548 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 549 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 550 | default: |
| 551 | /* Key is declared with a lifetime not known to us */ |
| 552 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 553 | break; |
| 554 | } |
| 555 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 556 | return( status ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 557 | } |
| 558 | |
Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame] | 559 | psa_status_t psa_driver_wrapper_import_key( |
| 560 | const psa_key_attributes_t *attributes, |
| 561 | const uint8_t *data, |
| 562 | size_t data_length, |
| 563 | uint8_t *key_buffer, |
| 564 | size_t key_buffer_size, |
| 565 | size_t *key_buffer_length, |
| 566 | size_t *bits ) |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 567 | { |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 568 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 569 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( |
| 570 | psa_get_key_lifetime( attributes ) ); |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 571 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 572 | /* Try dynamically-registered SE interface first */ |
| 573 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 574 | const psa_drv_se_t *drv; |
| 575 | psa_drv_se_context_t *drv_context; |
| 576 | |
| 577 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 578 | { |
| 579 | if( drv->key_management == NULL || |
| 580 | drv->key_management->p_import == NULL ) |
| 581 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 582 | |
| 583 | /* The driver should set the number of key bits, however in |
| 584 | * case it doesn't, we initialize bits to an invalid value. */ |
| 585 | *bits = PSA_MAX_KEY_BITS + 1; |
| 586 | status = drv->key_management->p_import( |
| 587 | drv_context, |
| 588 | *( (psa_key_slot_number_t *)key_buffer ), |
| 589 | attributes, data, data_length, bits ); |
| 590 | |
| 591 | if( status != PSA_SUCCESS ) |
| 592 | return( status ); |
| 593 | |
| 594 | if( (*bits) > PSA_MAX_KEY_BITS ) |
| 595 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 596 | |
| 597 | return( PSA_SUCCESS ); |
| 598 | } |
| 599 | #endif /* PSA_CRYPTO_SE_C */ |
| 600 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 601 | switch( location ) |
| 602 | { |
| 603 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 604 | /* Key is stored in the slot in export representation, so |
| 605 | * cycle through all known transparent accelerators */ |
| 606 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 607 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 608 | status = mbedtls_test_transparent_import_key( |
| 609 | attributes, |
| 610 | data, data_length, |
| 611 | key_buffer, key_buffer_size, |
| 612 | key_buffer_length, bits ); |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 613 | /* Declared with fallback == true */ |
| 614 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 615 | return( status ); |
| 616 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 617 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 618 | /* Fell through, meaning no accelerator supports this operation */ |
| 619 | return( psa_import_key_into_slot( attributes, |
| 620 | data, data_length, |
| 621 | key_buffer, key_buffer_size, |
| 622 | key_buffer_length, bits ) ); |
| 623 | |
| 624 | default: |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 625 | /* Importing a key with external storage in not yet supported. |
| 626 | * Return in error indicating that the lifetime is not valid. */ |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 627 | (void)status; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 628 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 629 | } |
| 630 | |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 631 | } |
| 632 | |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 633 | psa_status_t psa_driver_wrapper_export_key( |
| 634 | const psa_key_attributes_t *attributes, |
| 635 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 636 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 637 | |
| 638 | { |
| 639 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 640 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( |
| 641 | psa_get_key_lifetime( attributes ) ); |
| 642 | |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 643 | /* Try dynamically-registered SE interface first */ |
| 644 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 645 | const psa_drv_se_t *drv; |
| 646 | psa_drv_se_context_t *drv_context; |
| 647 | |
| 648 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 649 | { |
| 650 | if( ( drv->key_management == NULL ) || |
| 651 | ( drv->key_management->p_export == NULL ) ) |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 652 | { |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 653 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 654 | } |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 655 | |
| 656 | return( drv->key_management->p_export( |
| 657 | drv_context, |
| 658 | *( (psa_key_slot_number_t *)key_buffer ), |
| 659 | data, data_size, data_length ) ); |
| 660 | } |
| 661 | #endif /* PSA_CRYPTO_SE_C */ |
| 662 | |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 663 | switch( location ) |
| 664 | { |
| 665 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 666 | return( psa_export_key_internal( attributes, |
| 667 | key_buffer, |
| 668 | key_buffer_size, |
| 669 | data, |
| 670 | data_size, |
| 671 | data_length ) ); |
| 672 | |
| 673 | /* Add cases for opaque driver here */ |
| 674 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 675 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 676 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 677 | return( mbedtls_test_opaque_export_key( attributes, |
| 678 | key_buffer, |
| 679 | key_buffer_size, |
| 680 | data, |
| 681 | data_size, |
| 682 | data_length ) ); |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 683 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 684 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 685 | default: |
| 686 | /* Key is declared with a lifetime not known to us */ |
| 687 | return( status ); |
| 688 | } |
| 689 | } |
| 690 | |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 691 | psa_status_t psa_driver_wrapper_export_public_key( |
| 692 | const psa_key_attributes_t *attributes, |
| 693 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 694 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 695 | |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 696 | { |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 697 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 698 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( |
| 699 | psa_get_key_lifetime( attributes ) ); |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 700 | |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 701 | /* Try dynamically-registered SE interface first */ |
| 702 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 703 | const psa_drv_se_t *drv; |
| 704 | psa_drv_se_context_t *drv_context; |
| 705 | |
| 706 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 707 | { |
| 708 | if( ( drv->key_management == NULL ) || |
| 709 | ( drv->key_management->p_export_public == NULL ) ) |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 710 | { |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 711 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 712 | } |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 713 | |
| 714 | return( drv->key_management->p_export_public( |
| 715 | drv_context, |
| 716 | *( (psa_key_slot_number_t *)key_buffer ), |
| 717 | data, data_size, data_length ) ); |
| 718 | } |
| 719 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 720 | |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 721 | switch( location ) |
| 722 | { |
| 723 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 724 | /* Key is stored in the slot in export representation, so |
| 725 | * cycle through all known transparent accelerators */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 726 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 727 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 728 | status = mbedtls_test_transparent_export_public_key( |
| 729 | attributes, |
| 730 | key_buffer, |
| 731 | key_buffer_size, |
| 732 | data, |
| 733 | data_size, |
| 734 | data_length ); |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 735 | /* Declared with fallback == true */ |
| 736 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 737 | return( status ); |
| 738 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 739 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 740 | /* Fell through, meaning no accelerator supports this operation */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 741 | return( psa_export_public_key_internal( attributes, |
| 742 | key_buffer, |
| 743 | key_buffer_size, |
| 744 | data, |
| 745 | data_size, |
| 746 | data_length ) ); |
| 747 | |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 748 | /* Add cases for opaque driver here */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 749 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 750 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 751 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 752 | return( mbedtls_test_opaque_export_public_key( attributes, |
| 753 | key_buffer, |
| 754 | key_buffer_size, |
| 755 | data, |
| 756 | data_size, |
| 757 | data_length ) ); |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 758 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 759 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 760 | default: |
| 761 | /* Key is declared with a lifetime not known to us */ |
| 762 | return( status ); |
| 763 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 764 | } |
| 765 | |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 766 | psa_status_t psa_driver_wrapper_get_builtin_key( |
| 767 | psa_drv_slot_number_t slot_number, |
| 768 | psa_key_attributes_t *attributes, |
| 769 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length ) |
| 770 | { |
| 771 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 772 | switch( location ) |
| 773 | { |
Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 774 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 775 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 776 | return( mbedtls_test_opaque_get_builtin_key( |
Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 777 | slot_number, |
| 778 | attributes, |
| 779 | key_buffer, key_buffer_size, key_buffer_length ) ); |
Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 780 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 781 | default: |
| 782 | (void) slot_number; |
| 783 | (void) key_buffer; |
| 784 | (void) key_buffer_size; |
| 785 | (void) key_buffer_length; |
| 786 | return( PSA_ERROR_DOES_NOT_EXIST ); |
| 787 | } |
| 788 | } |
| 789 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 790 | /* |
| 791 | * Cipher functions |
| 792 | */ |
| 793 | psa_status_t psa_driver_wrapper_cipher_encrypt( |
| 794 | psa_key_slot_t *slot, |
| 795 | psa_algorithm_t alg, |
| 796 | const uint8_t *input, |
| 797 | size_t input_length, |
| 798 | uint8_t *output, |
| 799 | size_t output_size, |
| 800 | size_t *output_length ) |
| 801 | { |
| 802 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 803 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 804 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 805 | psa_key_attributes_t attributes = { |
| 806 | .core = slot->attr |
| 807 | }; |
| 808 | |
| 809 | switch( location ) |
| 810 | { |
| 811 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 812 | /* Key is stored in the slot in export representation, so |
| 813 | * cycle through all known transparent accelerators */ |
| 814 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 815 | status = mbedtls_test_transparent_cipher_encrypt( &attributes, |
| 816 | slot->key.data, |
| 817 | slot->key.bytes, |
| 818 | alg, |
| 819 | input, |
| 820 | input_length, |
| 821 | output, |
| 822 | output_size, |
| 823 | output_length ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 824 | /* Declared with fallback == true */ |
| 825 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 826 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 827 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 828 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 829 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 830 | /* Add cases for opaque driver here */ |
| 831 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 832 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 833 | return( mbedtls_test_opaque_cipher_encrypt( &attributes, |
| 834 | slot->key.data, |
| 835 | slot->key.bytes, |
| 836 | alg, |
| 837 | input, |
| 838 | input_length, |
| 839 | output, |
| 840 | output_size, |
| 841 | output_length ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 842 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 843 | default: |
| 844 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 845 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 846 | } |
| 847 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 848 | (void) slot; |
| 849 | (void) alg; |
| 850 | (void) input; |
| 851 | (void) input_length; |
| 852 | (void) output; |
| 853 | (void) output_size; |
| 854 | (void) output_length; |
| 855 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 856 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 857 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 858 | } |
| 859 | |
| 860 | psa_status_t psa_driver_wrapper_cipher_decrypt( |
| 861 | psa_key_slot_t *slot, |
| 862 | psa_algorithm_t alg, |
| 863 | const uint8_t *input, |
| 864 | size_t input_length, |
| 865 | uint8_t *output, |
| 866 | size_t output_size, |
| 867 | size_t *output_length ) |
| 868 | { |
| 869 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 870 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 871 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 872 | psa_key_attributes_t attributes = { |
| 873 | .core = slot->attr |
| 874 | }; |
| 875 | |
| 876 | switch( location ) |
| 877 | { |
| 878 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 879 | /* Key is stored in the slot in export representation, so |
| 880 | * cycle through all known transparent accelerators */ |
| 881 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 882 | status = mbedtls_test_transparent_cipher_decrypt( &attributes, |
| 883 | slot->key.data, |
| 884 | slot->key.bytes, |
| 885 | alg, |
| 886 | input, |
| 887 | input_length, |
| 888 | output, |
| 889 | output_size, |
| 890 | output_length ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 891 | /* Declared with fallback == true */ |
| 892 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 893 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 894 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 895 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 896 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 897 | /* Add cases for opaque driver here */ |
| 898 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 899 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 900 | return( mbedtls_test_opaque_cipher_decrypt( &attributes, |
| 901 | slot->key.data, |
| 902 | slot->key.bytes, |
| 903 | alg, |
| 904 | input, |
| 905 | input_length, |
| 906 | output, |
| 907 | output_size, |
| 908 | output_length ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 909 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 910 | default: |
| 911 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 912 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 913 | } |
| 914 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 915 | (void) slot; |
| 916 | (void) alg; |
| 917 | (void) input; |
| 918 | (void) input_length; |
| 919 | (void) output; |
| 920 | (void) output_size; |
| 921 | (void) output_length; |
| 922 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 923 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 924 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 925 | } |
| 926 | |
| 927 | psa_status_t psa_driver_wrapper_cipher_encrypt_setup( |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 928 | psa_cipher_operation_t *operation, |
| 929 | const psa_key_attributes_t *attributes, |
| 930 | const uint8_t *key_buffer, size_t key_buffer_size, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 931 | psa_algorithm_t alg ) |
| 932 | { |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 933 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 934 | psa_key_location_t location = |
| 935 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 936 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 937 | switch( location ) |
| 938 | { |
| 939 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 940 | /* Key is stored in the slot in export representation, so |
| 941 | * cycle through all known transparent accelerators */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 942 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 943 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 944 | status = mbedtls_test_transparent_cipher_encrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 945 | &operation->ctx.transparent_test_driver_ctx, |
| 946 | attributes, |
| 947 | key_buffer, |
| 948 | key_buffer_size, |
| 949 | alg ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 950 | /* Declared with fallback == true */ |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 951 | if( status == PSA_SUCCESS ) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 952 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 953 | |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 954 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 955 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 956 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 957 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 958 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 959 | /* Fell through, meaning no accelerator supports this operation */ |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 960 | status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 961 | attributes, |
| 962 | key_buffer, |
| 963 | key_buffer_size, |
| 964 | alg ); |
| 965 | if( status == PSA_SUCCESS ) |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 966 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 967 | |
Ronald Cron | 7b4154d | 2021-03-19 14:49:41 +0100 | [diff] [blame] | 968 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 969 | return( status ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 970 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
| 971 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 972 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 973 | /* Add cases for opaque driver here */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 974 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 975 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 976 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 977 | status = mbedtls_test_opaque_cipher_encrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 978 | &operation->ctx.opaque_test_driver_ctx, |
| 979 | attributes, |
| 980 | key_buffer, key_buffer_size, |
| 981 | alg ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 982 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 983 | if( status == PSA_SUCCESS ) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 984 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 985 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 986 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 987 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 988 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 989 | default: |
| 990 | /* Key is declared with a lifetime not known to us */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 991 | (void)status; |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 992 | (void)key_buffer; |
| 993 | (void)key_buffer_size; |
| 994 | (void)alg; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 995 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 996 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | psa_status_t psa_driver_wrapper_cipher_decrypt_setup( |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 1000 | psa_cipher_operation_t *operation, |
| 1001 | const psa_key_attributes_t *attributes, |
| 1002 | const uint8_t *key_buffer, size_t key_buffer_size, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1003 | psa_algorithm_t alg ) |
| 1004 | { |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1005 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 1006 | psa_key_location_t location = |
| 1007 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1008 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1009 | switch( location ) |
| 1010 | { |
| 1011 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1012 | /* Key is stored in the slot in export representation, so |
| 1013 | * cycle through all known transparent accelerators */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1014 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1015 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1016 | status = mbedtls_test_transparent_cipher_decrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1017 | &operation->ctx.transparent_test_driver_ctx, |
| 1018 | attributes, |
| 1019 | key_buffer, |
| 1020 | key_buffer_size, |
| 1021 | alg ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1022 | /* Declared with fallback == true */ |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 1023 | if( status == PSA_SUCCESS ) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1024 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 1025 | |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1026 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1027 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1028 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1029 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1030 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1031 | /* Fell through, meaning no accelerator supports this operation */ |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 1032 | status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1033 | attributes, |
| 1034 | key_buffer, |
| 1035 | key_buffer_size, |
| 1036 | alg ); |
| 1037 | if( status == PSA_SUCCESS ) |
| 1038 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
| 1039 | |
| 1040 | return( status ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1041 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
| 1042 | return( PSA_ERROR_NOT_SUPPORTED ); |
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 | /* Add cases for opaque driver here */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1045 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1046 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 1047 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1048 | status = mbedtls_test_opaque_cipher_decrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1049 | &operation->ctx.opaque_test_driver_ctx, |
| 1050 | attributes, |
| 1051 | key_buffer, key_buffer_size, |
| 1052 | alg ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1053 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1054 | if( status == PSA_SUCCESS ) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1055 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1056 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1057 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1058 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1059 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1060 | default: |
| 1061 | /* Key is declared with a lifetime not known to us */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1062 | (void)status; |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1063 | (void)key_buffer; |
| 1064 | (void)key_buffer_size; |
| 1065 | (void)alg; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 1066 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1067 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1068 | } |
| 1069 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1070 | psa_status_t psa_driver_wrapper_cipher_set_iv( |
Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 1071 | psa_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1072 | const uint8_t *iv, |
| 1073 | size_t iv_length ) |
| 1074 | { |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1075 | switch( operation->id ) |
| 1076 | { |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1077 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1078 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 1079 | return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1080 | iv, |
| 1081 | iv_length ) ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1082 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1083 | |
| 1084 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1085 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1086 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1087 | return( mbedtls_test_transparent_cipher_set_iv( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1088 | &operation->ctx.transparent_test_driver_ctx, |
| 1089 | iv, iv_length ) ); |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1090 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1091 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1092 | return( mbedtls_test_opaque_cipher_set_iv( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1093 | &operation->ctx.opaque_test_driver_ctx, |
| 1094 | iv, iv_length ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1095 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1096 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1097 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1098 | |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1099 | (void)iv; |
| 1100 | (void)iv_length; |
| 1101 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1102 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | psa_status_t psa_driver_wrapper_cipher_update( |
Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 1106 | psa_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1107 | const uint8_t *input, |
| 1108 | size_t input_length, |
| 1109 | uint8_t *output, |
| 1110 | size_t output_size, |
| 1111 | size_t *output_length ) |
| 1112 | { |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1113 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1114 | { |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1115 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1116 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 1117 | return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1118 | input, |
| 1119 | input_length, |
| 1120 | output, |
| 1121 | output_size, |
| 1122 | output_length ) ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1123 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
| 1124 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1125 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1126 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1127 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1128 | return( mbedtls_test_transparent_cipher_update( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1129 | &operation->ctx.transparent_test_driver_ctx, |
| 1130 | input, input_length, |
| 1131 | output, output_size, output_length ) ); |
| 1132 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1133 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1134 | return( mbedtls_test_opaque_cipher_update( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1135 | &operation->ctx.opaque_test_driver_ctx, |
| 1136 | input, input_length, |
| 1137 | output, output_size, output_length ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1138 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1139 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1140 | } |
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 | (void)input; |
| 1143 | (void)input_length; |
| 1144 | (void)output; |
| 1145 | (void)output_size; |
| 1146 | (void)output_length; |
| 1147 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1148 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | psa_status_t psa_driver_wrapper_cipher_finish( |
Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 1152 | psa_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1153 | uint8_t *output, |
| 1154 | size_t output_size, |
| 1155 | size_t *output_length ) |
| 1156 | { |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1157 | switch( operation->id ) |
| 1158 | { |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1159 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1160 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 1161 | return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1162 | output, |
| 1163 | output_size, |
| 1164 | output_length ) ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1165 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1166 | |
| 1167 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1168 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1169 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1170 | return( mbedtls_test_transparent_cipher_finish( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1171 | &operation->ctx.transparent_test_driver_ctx, |
| 1172 | output, output_size, output_length ) ); |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1173 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1174 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1175 | return( mbedtls_test_opaque_cipher_finish( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1176 | &operation->ctx.opaque_test_driver_ctx, |
| 1177 | output, output_size, output_length ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1178 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1179 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1180 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1181 | |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1182 | (void)output; |
| 1183 | (void)output_size; |
| 1184 | (void)output_length; |
| 1185 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1186 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | psa_status_t psa_driver_wrapper_cipher_abort( |
Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 1190 | psa_cipher_operation_t *operation ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1191 | { |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1192 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1193 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1194 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1195 | { |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1196 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1197 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 1198 | return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1199 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1200 | |
| 1201 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1202 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1203 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1204 | status = mbedtls_test_transparent_cipher_abort( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1205 | &operation->ctx.transparent_test_driver_ctx ); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1206 | mbedtls_platform_zeroize( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1207 | &operation->ctx.transparent_test_driver_ctx, |
| 1208 | sizeof( operation->ctx.transparent_test_driver_ctx ) ); |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1209 | return( status ); |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1210 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1211 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1212 | status = mbedtls_test_opaque_cipher_abort( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1213 | &operation->ctx.opaque_test_driver_ctx ); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1214 | mbedtls_platform_zeroize( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1215 | &operation->ctx.opaque_test_driver_ctx, |
| 1216 | sizeof( operation->ctx.opaque_test_driver_ctx ) ); |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1217 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1218 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1219 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1220 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1221 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1222 | (void)status; |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1223 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1224 | } |
| 1225 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1226 | /* |
| 1227 | * Hashing functions |
| 1228 | */ |
| 1229 | psa_status_t psa_driver_wrapper_hash_compute( |
| 1230 | psa_algorithm_t alg, |
| 1231 | const uint8_t *input, |
| 1232 | size_t input_length, |
| 1233 | uint8_t *hash, |
| 1234 | size_t hash_size, |
| 1235 | size_t *hash_length) |
| 1236 | { |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1237 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1238 | |
| 1239 | /* Try accelerators first */ |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1240 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1241 | status = mbedtls_test_transparent_hash_compute( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1242 | alg, input, input_length, hash, hash_size, hash_length ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1243 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1244 | return( status ); |
| 1245 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1246 | |
| 1247 | /* If software fallback is compiled in, try fallback */ |
| 1248 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1249 | status = mbedtls_psa_hash_compute( alg, input, input_length, |
| 1250 | hash, hash_size, hash_length ); |
| 1251 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1252 | return( status ); |
| 1253 | #endif |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1254 | (void) status; |
| 1255 | (void) alg; |
| 1256 | (void) input; |
| 1257 | (void) input_length; |
| 1258 | (void) hash; |
| 1259 | (void) hash_size; |
| 1260 | (void) hash_length; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1261 | |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1262 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | psa_status_t psa_driver_wrapper_hash_setup( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1266 | psa_hash_operation_t *operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1267 | psa_algorithm_t alg ) |
| 1268 | { |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1269 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1270 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1271 | /* Try setup on accelerators first */ |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1272 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1273 | status = mbedtls_test_transparent_hash_setup( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1274 | &operation->ctx.test_driver_ctx, alg ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1275 | if( status == PSA_SUCCESS ) |
| 1276 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
| 1277 | |
| 1278 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1279 | return( status ); |
| 1280 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1281 | |
| 1282 | /* If software fallback is compiled in, try fallback */ |
| 1283 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1284 | status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg ); |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1285 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1286 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1287 | |
| 1288 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1289 | return( status ); |
| 1290 | #endif |
| 1291 | /* Nothing left to try if we fall through here */ |
| 1292 | (void) status; |
| 1293 | (void) operation; |
| 1294 | (void) alg; |
| 1295 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1296 | } |
| 1297 | |
| 1298 | psa_status_t psa_driver_wrapper_hash_clone( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1299 | const psa_hash_operation_t *source_operation, |
| 1300 | psa_hash_operation_t *target_operation ) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1301 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1302 | switch( source_operation->id ) |
| 1303 | { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1304 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1305 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
| 1306 | target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
| 1307 | return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx, |
| 1308 | &target_operation->ctx.mbedtls_ctx ) ); |
| 1309 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1310 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1311 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
| 1312 | target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1313 | return( mbedtls_test_transparent_hash_clone( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1314 | &source_operation->ctx.test_driver_ctx, |
| 1315 | &target_operation->ctx.test_driver_ctx ) ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1316 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1317 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1318 | (void) target_operation; |
| 1319 | return( PSA_ERROR_BAD_STATE ); |
| 1320 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | psa_status_t psa_driver_wrapper_hash_update( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1324 | psa_hash_operation_t *operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1325 | const uint8_t *input, |
| 1326 | size_t input_length ) |
| 1327 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1328 | switch( operation->id ) |
| 1329 | { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1330 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1331 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
| 1332 | return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx, |
| 1333 | input, input_length ) ); |
| 1334 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1335 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1336 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1337 | return( mbedtls_test_transparent_hash_update( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1338 | &operation->ctx.test_driver_ctx, |
| 1339 | input, input_length ) ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1340 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1341 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1342 | (void) input; |
| 1343 | (void) input_length; |
| 1344 | return( PSA_ERROR_BAD_STATE ); |
| 1345 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | psa_status_t psa_driver_wrapper_hash_finish( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1349 | psa_hash_operation_t *operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1350 | uint8_t *hash, |
| 1351 | size_t hash_size, |
| 1352 | size_t *hash_length ) |
| 1353 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1354 | switch( operation->id ) |
| 1355 | { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1356 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1357 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
| 1358 | return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx, |
| 1359 | hash, hash_size, hash_length ) ); |
| 1360 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1361 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1362 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1363 | return( mbedtls_test_transparent_hash_finish( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1364 | &operation->ctx.test_driver_ctx, |
| 1365 | hash, hash_size, hash_length ) ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1366 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1367 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1368 | (void) hash; |
| 1369 | (void) hash_size; |
| 1370 | (void) hash_length; |
| 1371 | return( PSA_ERROR_BAD_STATE ); |
| 1372 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | psa_status_t psa_driver_wrapper_hash_abort( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1376 | psa_hash_operation_t *operation ) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1377 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1378 | switch( operation->id ) |
| 1379 | { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1380 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1381 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
| 1382 | return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) ); |
| 1383 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1384 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1385 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1386 | return( mbedtls_test_transparent_hash_abort( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1387 | &operation->ctx.test_driver_ctx ) ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1388 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1389 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1390 | return( PSA_ERROR_BAD_STATE ); |
| 1391 | } |
| 1392 | } |
| 1393 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1394 | psa_status_t psa_driver_wrapper_aead_encrypt( |
| 1395 | const psa_key_attributes_t *attributes, |
| 1396 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1397 | psa_algorithm_t alg, |
| 1398 | const uint8_t *nonce, size_t nonce_length, |
| 1399 | const uint8_t *additional_data, size_t additional_data_length, |
| 1400 | const uint8_t *plaintext, size_t plaintext_length, |
| 1401 | uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length ) |
| 1402 | { |
| 1403 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1404 | psa_key_location_t location = |
| 1405 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 1406 | |
| 1407 | switch( location ) |
| 1408 | { |
| 1409 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1410 | /* Key is stored in the slot in export representation, so |
| 1411 | * cycle through all known transparent accelerators */ |
| 1412 | |
| 1413 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1414 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1415 | status = mbedtls_test_transparent_aead_encrypt( |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1416 | attributes, key_buffer, key_buffer_size, |
| 1417 | alg, |
| 1418 | nonce, nonce_length, |
| 1419 | additional_data, additional_data_length, |
| 1420 | plaintext, plaintext_length, |
| 1421 | ciphertext, ciphertext_size, ciphertext_length ); |
| 1422 | /* Declared with fallback == true */ |
| 1423 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1424 | return( status ); |
| 1425 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1426 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1427 | |
| 1428 | /* Fell through, meaning no accelerator supports this operation */ |
| 1429 | return( mbedtls_psa_aead_encrypt( |
| 1430 | attributes, key_buffer, key_buffer_size, |
| 1431 | alg, |
| 1432 | nonce, nonce_length, |
| 1433 | additional_data, additional_data_length, |
| 1434 | plaintext, plaintext_length, |
| 1435 | ciphertext, ciphertext_size, ciphertext_length ) ); |
| 1436 | |
| 1437 | /* Add cases for opaque driver here */ |
| 1438 | |
| 1439 | default: |
| 1440 | /* Key is declared with a lifetime not known to us */ |
| 1441 | (void)status; |
| 1442 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | psa_status_t psa_driver_wrapper_aead_decrypt( |
| 1447 | const psa_key_attributes_t *attributes, |
| 1448 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1449 | psa_algorithm_t alg, |
| 1450 | const uint8_t *nonce, size_t nonce_length, |
| 1451 | const uint8_t *additional_data, size_t additional_data_length, |
| 1452 | const uint8_t *ciphertext, size_t ciphertext_length, |
| 1453 | uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length ) |
| 1454 | { |
| 1455 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1456 | psa_key_location_t location = |
| 1457 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 1458 | |
| 1459 | switch( location ) |
| 1460 | { |
| 1461 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1462 | /* Key is stored in the slot in export representation, so |
| 1463 | * cycle through all known transparent accelerators */ |
| 1464 | |
| 1465 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1466 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1467 | status = mbedtls_test_transparent_aead_decrypt( |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1468 | attributes, key_buffer, key_buffer_size, |
| 1469 | alg, |
| 1470 | nonce, nonce_length, |
| 1471 | additional_data, additional_data_length, |
| 1472 | ciphertext, ciphertext_length, |
| 1473 | plaintext, plaintext_size, plaintext_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 | |
| 1480 | /* Fell through, meaning no accelerator supports this operation */ |
| 1481 | return( mbedtls_psa_aead_decrypt( |
| 1482 | attributes, key_buffer, key_buffer_size, |
| 1483 | alg, |
| 1484 | nonce, nonce_length, |
| 1485 | additional_data, additional_data_length, |
| 1486 | ciphertext, ciphertext_length, |
| 1487 | plaintext, plaintext_size, plaintext_length ) ); |
| 1488 | |
| 1489 | /* Add cases for opaque driver here */ |
| 1490 | |
| 1491 | default: |
| 1492 | /* Key is declared with a lifetime not known to us */ |
| 1493 | (void)status; |
| 1494 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1495 | } |
| 1496 | } |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 1497 | /* End of automatically generated file. */ |