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" |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 27 | #include "psa_crypto_mac.h" |
Steven Cooreman | 830aff2 | 2021-03-09 09:50:44 +0100 | [diff] [blame] | 28 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 29 | #include "mbedtls/platform.h" |
| 30 | |
Gilles Peskine | ad0e012 | 2021-04-24 13:19:45 +0200 | [diff] [blame] | 31 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 32 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 33 | #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 34 | |
| 35 | /* Include test driver definition when running tests */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 36 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 37 | #ifndef PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 38 | #define PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 39 | #endif |
| 40 | #ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 41 | #define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 42 | #endif |
Ronald Cron | 05ee58d | 2021-04-30 17:00:34 +0200 | [diff] [blame] | 43 | #include "test/drivers/test_driver.h" |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 44 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 45 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 46 | /* Repeat above block for each JSON-declared driver during autogeneration */ |
Steven Cooreman | aa87fd0 | 2021-03-15 18:54:03 +0100 | [diff] [blame] | 47 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 48 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 49 | /* Auto-generated values depending on which drivers are registered. |
| 50 | * ID 0 is reserved for unallocated operations. |
| 51 | * ID 1 is reserved for the Mbed TLS software driver. */ |
Steven Cooreman | 830aff2 | 2021-03-09 09:50:44 +0100 | [diff] [blame] | 52 | #define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1) |
| 53 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 54 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 55 | #define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2) |
| 56 | #define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 57 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 58 | |
| 59 | /* Support the 'old' SE interface when asked to */ |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 60 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 61 | /* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style |
| 62 | * SE driver is present, to avoid unused argument errors at compile time. */ |
| 63 | #ifndef PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 64 | #define PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 65 | #endif |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 66 | #include "psa_crypto_se.h" |
| 67 | #endif |
| 68 | |
Ronald Cron | 088d5d0 | 2021-04-10 16:57:30 +0200 | [diff] [blame] | 69 | psa_status_t psa_driver_wrapper_init( void ) |
| 70 | { |
| 71 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 72 | |
| 73 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 74 | status = psa_init_all_se_drivers( ); |
| 75 | if( status != PSA_SUCCESS ) |
| 76 | return( status ); |
| 77 | #endif |
| 78 | |
| 79 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 80 | status = mbedtls_test_transparent_init( ); |
| 81 | if( status != PSA_SUCCESS ) |
| 82 | return( status ); |
| 83 | |
| 84 | status = mbedtls_test_opaque_init( ); |
| 85 | if( status != PSA_SUCCESS ) |
| 86 | return( status ); |
| 87 | #endif |
| 88 | |
| 89 | (void) status; |
| 90 | return( PSA_SUCCESS ); |
| 91 | } |
| 92 | |
| 93 | void psa_driver_wrapper_free( void ) |
| 94 | { |
| 95 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 96 | /* Unregister all secure element drivers, so that we restart from |
| 97 | * a pristine state. */ |
| 98 | psa_unregister_all_se_drivers( ); |
| 99 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 100 | |
| 101 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 102 | mbedtls_test_transparent_free( ); |
| 103 | mbedtls_test_opaque_free( ); |
| 104 | #endif |
| 105 | } |
| 106 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 107 | /* Start delegation functions */ |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 108 | psa_status_t psa_driver_wrapper_sign_message( |
| 109 | const psa_key_attributes_t *attributes, |
| 110 | const uint8_t *key_buffer, |
| 111 | size_t key_buffer_size, |
| 112 | psa_algorithm_t alg, |
| 113 | const uint8_t *input, |
| 114 | size_t input_length, |
| 115 | uint8_t *signature, |
| 116 | size_t signature_size, |
| 117 | size_t *signature_length ) |
| 118 | { |
| 119 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 120 | psa_key_location_t location = |
| 121 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 122 | |
| 123 | switch( location ) |
| 124 | { |
| 125 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 126 | /* Key is stored in the slot in export representation, so |
| 127 | * cycle through all known transparent accelerators */ |
| 128 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 129 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 130 | status = mbedtls_test_transparent_signature_sign_message( |
| 131 | attributes, |
| 132 | key_buffer, |
| 133 | key_buffer_size, |
| 134 | alg, |
| 135 | input, |
| 136 | input_length, |
| 137 | signature, |
| 138 | signature_size, |
| 139 | signature_length ); |
| 140 | /* Declared with fallback == true */ |
| 141 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 142 | return( status ); |
| 143 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 144 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
gabor-mezei-arm | a7b9b20 | 2021-05-07 15:16:34 +0200 | [diff] [blame] | 145 | break; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 146 | |
| 147 | /* Add cases for opaque driver here */ |
| 148 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 149 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
gabor-mezei-arm | 77588fb | 2021-04-27 18:52:51 +0200 | [diff] [blame] | 150 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
gabor-mezei-arm | a7b9b20 | 2021-05-07 15:16:34 +0200 | [diff] [blame] | 151 | status = mbedtls_test_opaque_signature_sign_message( |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 152 | attributes, |
| 153 | key_buffer, |
| 154 | key_buffer_size, |
| 155 | alg, |
| 156 | input, |
| 157 | input_length, |
| 158 | signature, |
| 159 | signature_size, |
gabor-mezei-arm | a7b9b20 | 2021-05-07 15:16:34 +0200 | [diff] [blame] | 160 | signature_length ); |
| 161 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 162 | return( status ); |
| 163 | break; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 164 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 165 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 166 | default: |
| 167 | /* Key is declared with a lifetime not known to us */ |
| 168 | (void)status; |
gabor-mezei-arm | f25c976 | 2021-05-12 11:12:25 +0200 | [diff] [blame] | 169 | break; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 170 | } |
gabor-mezei-arm | a7b9b20 | 2021-05-07 15:16:34 +0200 | [diff] [blame] | 171 | |
| 172 | return( psa_sign_message_builtin( attributes, |
| 173 | key_buffer, |
| 174 | key_buffer_size, |
| 175 | alg, |
| 176 | input, |
| 177 | input_length, |
| 178 | signature, |
| 179 | signature_size, |
| 180 | signature_length ) ); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | psa_status_t psa_driver_wrapper_verify_message( |
| 184 | const psa_key_attributes_t *attributes, |
| 185 | const uint8_t *key_buffer, |
| 186 | size_t key_buffer_size, |
| 187 | psa_algorithm_t alg, |
| 188 | const uint8_t *input, |
| 189 | size_t input_length, |
| 190 | const uint8_t *signature, |
| 191 | size_t signature_length ) |
| 192 | { |
| 193 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 194 | psa_key_location_t location = |
| 195 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 196 | |
| 197 | switch( location ) |
| 198 | { |
| 199 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 200 | /* Key is stored in the slot in export representation, so |
| 201 | * cycle through all known transparent accelerators */ |
| 202 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 203 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 204 | status = mbedtls_test_transparent_signature_verify_message( |
| 205 | attributes, |
| 206 | key_buffer, |
| 207 | key_buffer_size, |
| 208 | alg, |
| 209 | input, |
| 210 | input_length, |
| 211 | signature, |
| 212 | signature_length ); |
| 213 | /* Declared with fallback == true */ |
| 214 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 215 | return( status ); |
| 216 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 217 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
gabor-mezei-arm | a7b9b20 | 2021-05-07 15:16:34 +0200 | [diff] [blame] | 218 | break; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 219 | |
| 220 | /* Add cases for opaque driver here */ |
| 221 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 222 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
gabor-mezei-arm | 77588fb | 2021-04-27 18:52:51 +0200 | [diff] [blame] | 223 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 224 | return( mbedtls_test_opaque_signature_verify_message( |
| 225 | attributes, |
| 226 | key_buffer, |
| 227 | key_buffer_size, |
| 228 | alg, |
| 229 | input, |
| 230 | input_length, |
| 231 | signature, |
| 232 | signature_length ) ); |
gabor-mezei-arm | a7b9b20 | 2021-05-07 15:16:34 +0200 | [diff] [blame] | 233 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 234 | return( status ); |
| 235 | break; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 236 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 237 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 238 | default: |
| 239 | /* Key is declared with a lifetime not known to us */ |
| 240 | (void)status; |
gabor-mezei-arm | f25c976 | 2021-05-12 11:12:25 +0200 | [diff] [blame] | 241 | break; |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 242 | } |
gabor-mezei-arm | a7b9b20 | 2021-05-07 15:16:34 +0200 | [diff] [blame] | 243 | |
| 244 | return( psa_verify_message_builtin( attributes, |
| 245 | key_buffer, |
| 246 | key_buffer_size, |
| 247 | alg, |
| 248 | input, |
| 249 | input_length, |
| 250 | signature, |
| 251 | signature_length ) ); |
gabor-mezei-arm | c53f4f6 | 2021-04-22 11:32:19 +0200 | [diff] [blame] | 252 | } |
| 253 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 254 | psa_status_t psa_driver_wrapper_sign_hash( |
| 255 | const psa_key_attributes_t *attributes, |
| 256 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 257 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 258 | uint8_t *signature, size_t signature_size, size_t *signature_length ) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 259 | { |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 260 | /* Try dynamically-registered SE interface first */ |
| 261 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 262 | const psa_drv_se_t *drv; |
| 263 | psa_drv_se_context_t *drv_context; |
| 264 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 265 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 266 | { |
| 267 | if( drv->asymmetric == NULL || |
| 268 | drv->asymmetric->p_sign == NULL ) |
| 269 | { |
| 270 | /* 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] | 271 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 272 | } |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 273 | return( drv->asymmetric->p_sign( |
| 274 | drv_context, *( (psa_key_slot_number_t *)key_buffer ), |
| 275 | alg, hash, hash_length, |
| 276 | signature, signature_size, signature_length ) ); |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 277 | } |
Pengyu Lv | f513197 | 2022-11-08 18:17:00 +0800 | [diff] [blame^] | 278 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 279 | |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 280 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 281 | psa_key_location_t location = |
| 282 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 283 | |
| 284 | switch( location ) |
| 285 | { |
| 286 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 287 | /* Key is stored in the slot in export representation, so |
| 288 | * cycle through all known transparent accelerators */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 289 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 290 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 291 | status = mbedtls_test_transparent_signature_sign_hash( attributes, |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 292 | key_buffer, |
| 293 | key_buffer_size, |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 294 | alg, |
| 295 | hash, |
| 296 | hash_length, |
| 297 | signature, |
| 298 | signature_size, |
| 299 | signature_length ); |
| 300 | /* Declared with fallback == true */ |
| 301 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 302 | return( status ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 303 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 304 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 305 | /* Fell through, meaning no accelerator supports this operation */ |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 306 | return( psa_sign_hash_builtin( attributes, |
| 307 | key_buffer, |
| 308 | key_buffer_size, |
| 309 | alg, |
| 310 | hash, |
| 311 | hash_length, |
| 312 | signature, |
| 313 | signature_size, |
| 314 | signature_length ) ); |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 315 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 316 | /* Add cases for opaque driver here */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 317 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 318 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 319 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 320 | return( mbedtls_test_opaque_signature_sign_hash( attributes, |
| 321 | key_buffer, |
| 322 | key_buffer_size, |
| 323 | alg, |
| 324 | hash, |
| 325 | hash_length, |
| 326 | signature, |
| 327 | signature_size, |
| 328 | signature_length ) ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 329 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 330 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 331 | default: |
| 332 | /* Key is declared with a lifetime not known to us */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 333 | (void)status; |
| 334 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 335 | } |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 336 | } |
| 337 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 338 | psa_status_t psa_driver_wrapper_verify_hash( |
| 339 | const psa_key_attributes_t *attributes, |
| 340 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 341 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, |
| 342 | const uint8_t *signature, size_t signature_length ) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 343 | { |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 344 | /* Try dynamically-registered SE interface first */ |
| 345 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 346 | const psa_drv_se_t *drv; |
| 347 | psa_drv_se_context_t *drv_context; |
| 348 | |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 349 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 350 | { |
| 351 | if( drv->asymmetric == NULL || |
| 352 | drv->asymmetric->p_verify == NULL ) |
| 353 | { |
| 354 | /* 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] | 355 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 356 | } |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 357 | return( drv->asymmetric->p_verify( |
| 358 | drv_context, *( (psa_key_slot_number_t *)key_buffer ), |
| 359 | alg, hash, hash_length, |
| 360 | signature, signature_length ) ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 361 | } |
Pengyu Lv | f513197 | 2022-11-08 18:17:00 +0800 | [diff] [blame^] | 362 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 363 | |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 364 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 365 | psa_key_location_t location = |
| 366 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 367 | |
| 368 | switch( location ) |
| 369 | { |
| 370 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 371 | /* Key is stored in the slot in export representation, so |
| 372 | * cycle through all known transparent accelerators */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 373 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 374 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 375 | status = mbedtls_test_transparent_signature_verify_hash( |
| 376 | attributes, |
| 377 | key_buffer, |
| 378 | key_buffer_size, |
| 379 | alg, |
| 380 | hash, |
| 381 | hash_length, |
| 382 | signature, |
| 383 | signature_length ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 384 | /* Declared with fallback == true */ |
| 385 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 386 | return( status ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 387 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 388 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 389 | |
gabor-mezei-arm | f3c5c86 | 2021-05-05 14:18:36 +0200 | [diff] [blame] | 390 | return( psa_verify_hash_builtin( attributes, |
| 391 | key_buffer, |
| 392 | key_buffer_size, |
| 393 | alg, |
| 394 | hash, |
| 395 | hash_length, |
| 396 | signature, |
| 397 | signature_length ) ); |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 398 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 399 | /* Add cases for opaque driver here */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 400 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 401 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 402 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 403 | return( mbedtls_test_opaque_signature_verify_hash( attributes, |
| 404 | key_buffer, |
| 405 | key_buffer_size, |
| 406 | alg, |
| 407 | hash, |
| 408 | hash_length, |
| 409 | signature, |
| 410 | signature_length ) ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 411 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 412 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 413 | default: |
| 414 | /* Key is declared with a lifetime not known to us */ |
Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 415 | (void)status; |
| 416 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 417 | } |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 418 | } |
| 419 | |
Steven Cooreman | b938b0b | 2021-04-06 13:08:42 +0200 | [diff] [blame] | 420 | /** Get the key buffer size required to store the key material of a key |
| 421 | * associated with an opaque driver without storage. |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 422 | * |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 423 | * \param[in] attributes The key attributes. |
| 424 | * \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] | 425 | * |
| 426 | * \retval #PSA_SUCCESS |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 427 | * The minimum size for a buffer to contain the key material has been |
| 428 | * returned successfully. |
| 429 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 430 | * The size in bits of the key is not valid. |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 431 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 432 | * The type and/or the size in bits of the key or the combination of |
| 433 | * the two is not supported. |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 434 | */ |
Ronald Cron | 9df74be | 2020-12-05 19:15:23 +0100 | [diff] [blame] | 435 | psa_status_t psa_driver_wrapper_get_key_buffer_size( |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 436 | const psa_key_attributes_t *attributes, |
| 437 | size_t *key_buffer_size ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 438 | { |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 439 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 440 | psa_key_type_t key_type = attributes->core.type; |
| 441 | size_t key_bits = attributes->core.bits; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 442 | |
Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 443 | *key_buffer_size = 0; |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 444 | switch( location ) |
| 445 | { |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 446 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 447 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 448 | #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS) |
| 449 | /* Emulate property 'builtin_key_size' */ |
| 450 | if( psa_key_id_is_builtin( |
| 451 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( |
| 452 | psa_get_key_id( attributes ) ) ) ) |
| 453 | { |
Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 454 | *key_buffer_size = sizeof( psa_drv_slot_number_t ); |
Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 455 | return( PSA_SUCCESS ); |
| 456 | } |
| 457 | #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 458 | *key_buffer_size = mbedtls_test_size_function( key_type, key_bits ); |
Ronald Cron | b0737da | 2021-04-14 10:55:34 +0200 | [diff] [blame] | 459 | return( ( *key_buffer_size != 0 ) ? |
| 460 | PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED ); |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 461 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 462 | |
| 463 | default: |
Ronald Cron | 9df74be | 2020-12-05 19:15:23 +0100 | [diff] [blame] | 464 | (void)key_type; |
| 465 | (void)key_bits; |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 466 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 467 | } |
| 468 | } |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 469 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 470 | psa_status_t psa_driver_wrapper_generate_key( |
| 471 | const psa_key_attributes_t *attributes, |
| 472 | 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] | 473 | { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 474 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 475 | psa_key_location_t location = |
| 476 | PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime); |
| 477 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 478 | /* Try dynamically-registered SE interface first */ |
| 479 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 480 | const psa_drv_se_t *drv; |
| 481 | psa_drv_se_context_t *drv_context; |
| 482 | |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 483 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 484 | { |
| 485 | size_t pubkey_length = 0; /* We don't support this feature yet */ |
| 486 | if( drv->key_management == NULL || |
| 487 | drv->key_management->p_generate == NULL ) |
| 488 | { |
| 489 | /* 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] | 490 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 491 | } |
| 492 | return( drv->key_management->p_generate( |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 493 | drv_context, |
| 494 | *( (psa_key_slot_number_t *)key_buffer ), |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 495 | attributes, NULL, 0, &pubkey_length ) ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 496 | } |
| 497 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 498 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 499 | switch( location ) |
| 500 | { |
| 501 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 502 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 503 | /* Transparent drivers are limited to generating asymmetric keys */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 504 | if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) ) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 505 | { |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 506 | /* Cycle through all known transparent accelerators */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 507 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 508 | status = mbedtls_test_transparent_generate_key( |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 509 | attributes, key_buffer, key_buffer_size, |
| 510 | key_buffer_length ); |
| 511 | /* Declared with fallback == true */ |
| 512 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 513 | break; |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 514 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 515 | } |
| 516 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 517 | |
| 518 | /* Software fallback */ |
| 519 | status = psa_generate_key_internal( |
| 520 | attributes, key_buffer, key_buffer_size, key_buffer_length ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 521 | break; |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 522 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 523 | /* Add cases for opaque driver here */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 524 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 525 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 526 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 527 | status = mbedtls_test_opaque_generate_key( |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 528 | attributes, key_buffer, key_buffer_size, key_buffer_length ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 529 | break; |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 530 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 531 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 532 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 533 | default: |
| 534 | /* Key is declared with a lifetime not known to us */ |
| 535 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 536 | break; |
| 537 | } |
| 538 | |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 539 | return( status ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 540 | } |
| 541 | |
Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame] | 542 | psa_status_t psa_driver_wrapper_import_key( |
| 543 | const psa_key_attributes_t *attributes, |
| 544 | const uint8_t *data, |
| 545 | size_t data_length, |
| 546 | uint8_t *key_buffer, |
| 547 | size_t key_buffer_size, |
| 548 | size_t *key_buffer_length, |
| 549 | size_t *bits ) |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 550 | { |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 551 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 552 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( |
| 553 | psa_get_key_lifetime( attributes ) ); |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 554 | |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 555 | /* Try dynamically-registered SE interface first */ |
| 556 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 557 | const psa_drv_se_t *drv; |
| 558 | psa_drv_se_context_t *drv_context; |
| 559 | |
| 560 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 561 | { |
| 562 | if( drv->key_management == NULL || |
| 563 | drv->key_management->p_import == NULL ) |
| 564 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 565 | |
| 566 | /* The driver should set the number of key bits, however in |
| 567 | * case it doesn't, we initialize bits to an invalid value. */ |
| 568 | *bits = PSA_MAX_KEY_BITS + 1; |
| 569 | status = drv->key_management->p_import( |
| 570 | drv_context, |
| 571 | *( (psa_key_slot_number_t *)key_buffer ), |
| 572 | attributes, data, data_length, bits ); |
| 573 | |
| 574 | if( status != PSA_SUCCESS ) |
| 575 | return( status ); |
| 576 | |
| 577 | if( (*bits) > PSA_MAX_KEY_BITS ) |
| 578 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 579 | |
| 580 | return( PSA_SUCCESS ); |
| 581 | } |
Pengyu Lv | f513197 | 2022-11-08 18:17:00 +0800 | [diff] [blame^] | 582 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 583 | |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 584 | switch( location ) |
| 585 | { |
| 586 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 587 | /* Key is stored in the slot in export representation, so |
| 588 | * cycle through all known transparent accelerators */ |
| 589 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 590 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 591 | status = mbedtls_test_transparent_import_key( |
| 592 | attributes, |
| 593 | data, data_length, |
| 594 | key_buffer, key_buffer_size, |
| 595 | key_buffer_length, bits ); |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 596 | /* Declared with fallback == true */ |
| 597 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 598 | return( status ); |
| 599 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 600 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 601 | /* Fell through, meaning no accelerator supports this operation */ |
| 602 | return( psa_import_key_into_slot( attributes, |
| 603 | data, data_length, |
| 604 | key_buffer, key_buffer_size, |
| 605 | key_buffer_length, bits ) ); |
| 606 | |
| 607 | default: |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 608 | /* Importing a key with external storage in not yet supported. |
| 609 | * Return in error indicating that the lifetime is not valid. */ |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 610 | (void)status; |
Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 611 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 612 | } |
| 613 | |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 614 | } |
| 615 | |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 616 | psa_status_t psa_driver_wrapper_export_key( |
| 617 | const psa_key_attributes_t *attributes, |
| 618 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 619 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 620 | |
| 621 | { |
| 622 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 623 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( |
| 624 | psa_get_key_lifetime( attributes ) ); |
| 625 | |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 626 | /* Try dynamically-registered SE interface first */ |
| 627 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 628 | const psa_drv_se_t *drv; |
| 629 | psa_drv_se_context_t *drv_context; |
| 630 | |
| 631 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 632 | { |
| 633 | if( ( drv->key_management == NULL ) || |
| 634 | ( drv->key_management->p_export == NULL ) ) |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 635 | { |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 636 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 637 | } |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 638 | |
| 639 | return( drv->key_management->p_export( |
| 640 | drv_context, |
| 641 | *( (psa_key_slot_number_t *)key_buffer ), |
| 642 | data, data_size, data_length ) ); |
| 643 | } |
Pengyu Lv | f513197 | 2022-11-08 18:17:00 +0800 | [diff] [blame^] | 644 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 645 | |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 646 | switch( location ) |
| 647 | { |
| 648 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 649 | return( psa_export_key_internal( attributes, |
| 650 | key_buffer, |
| 651 | key_buffer_size, |
| 652 | data, |
| 653 | data_size, |
| 654 | data_length ) ); |
| 655 | |
| 656 | /* Add cases for opaque driver here */ |
| 657 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 658 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 659 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 660 | return( mbedtls_test_opaque_export_key( attributes, |
| 661 | key_buffer, |
| 662 | key_buffer_size, |
| 663 | data, |
| 664 | data_size, |
| 665 | data_length ) ); |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 666 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 667 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 668 | default: |
| 669 | /* Key is declared with a lifetime not known to us */ |
| 670 | return( status ); |
| 671 | } |
| 672 | } |
| 673 | |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 674 | psa_status_t psa_driver_wrapper_export_public_key( |
| 675 | const psa_key_attributes_t *attributes, |
| 676 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 677 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 678 | |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 679 | { |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 680 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 681 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( |
| 682 | psa_get_key_lifetime( attributes ) ); |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 683 | |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 684 | /* Try dynamically-registered SE interface first */ |
| 685 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 686 | const psa_drv_se_t *drv; |
| 687 | psa_drv_se_context_t *drv_context; |
| 688 | |
| 689 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 690 | { |
| 691 | if( ( drv->key_management == NULL ) || |
| 692 | ( drv->key_management->p_export_public == NULL ) ) |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 693 | { |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 694 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 695 | } |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 696 | |
| 697 | return( drv->key_management->p_export_public( |
| 698 | drv_context, |
| 699 | *( (psa_key_slot_number_t *)key_buffer ), |
| 700 | data, data_size, data_length ) ); |
| 701 | } |
| 702 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 703 | |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 704 | switch( location ) |
| 705 | { |
| 706 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 707 | /* Key is stored in the slot in export representation, so |
| 708 | * cycle through all known transparent accelerators */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 709 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 710 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 711 | status = mbedtls_test_transparent_export_public_key( |
| 712 | attributes, |
| 713 | key_buffer, |
| 714 | key_buffer_size, |
| 715 | data, |
| 716 | data_size, |
| 717 | data_length ); |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 718 | /* Declared with fallback == true */ |
| 719 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 720 | return( status ); |
| 721 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 722 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 723 | /* Fell through, meaning no accelerator supports this operation */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 724 | return( psa_export_public_key_internal( attributes, |
| 725 | key_buffer, |
| 726 | key_buffer_size, |
| 727 | data, |
| 728 | data_size, |
| 729 | data_length ) ); |
| 730 | |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 731 | /* Add cases for opaque driver here */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 732 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 733 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 734 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 735 | return( mbedtls_test_opaque_export_public_key( attributes, |
| 736 | key_buffer, |
| 737 | key_buffer_size, |
| 738 | data, |
| 739 | data_size, |
| 740 | data_length ) ); |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 741 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 742 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 743 | default: |
| 744 | /* Key is declared with a lifetime not known to us */ |
| 745 | return( status ); |
| 746 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 747 | } |
| 748 | |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 749 | psa_status_t psa_driver_wrapper_get_builtin_key( |
| 750 | psa_drv_slot_number_t slot_number, |
| 751 | psa_key_attributes_t *attributes, |
| 752 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length ) |
| 753 | { |
| 754 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 755 | switch( location ) |
| 756 | { |
Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 757 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 758 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 759 | return( mbedtls_test_opaque_get_builtin_key( |
Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 760 | slot_number, |
| 761 | attributes, |
| 762 | key_buffer, key_buffer_size, key_buffer_length ) ); |
Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 763 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 764 | default: |
| 765 | (void) slot_number; |
| 766 | (void) key_buffer; |
| 767 | (void) key_buffer_size; |
| 768 | (void) key_buffer_length; |
| 769 | return( PSA_ERROR_DOES_NOT_EXIST ); |
| 770 | } |
| 771 | } |
| 772 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 773 | /* |
| 774 | * Cipher functions |
| 775 | */ |
| 776 | psa_status_t psa_driver_wrapper_cipher_encrypt( |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 777 | const psa_key_attributes_t *attributes, |
| 778 | const uint8_t *key_buffer, |
| 779 | size_t key_buffer_size, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 780 | psa_algorithm_t alg, |
Ronald Cron | a833169 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 781 | const uint8_t *iv, |
| 782 | size_t iv_length, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 783 | const uint8_t *input, |
| 784 | size_t input_length, |
| 785 | uint8_t *output, |
| 786 | size_t output_size, |
| 787 | size_t *output_length ) |
| 788 | { |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 789 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 790 | psa_key_location_t location = |
| 791 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 792 | |
| 793 | switch( location ) |
| 794 | { |
| 795 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 796 | /* Key is stored in the slot in export representation, so |
| 797 | * cycle through all known transparent accelerators */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 798 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 799 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 800 | status = mbedtls_test_transparent_cipher_encrypt( attributes, |
| 801 | key_buffer, |
| 802 | key_buffer_size, |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 803 | alg, |
Ronald Cron | a833169 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 804 | iv, |
| 805 | iv_length, |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 806 | input, |
| 807 | input_length, |
| 808 | output, |
| 809 | output_size, |
| 810 | output_length ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 811 | /* Declared with fallback == true */ |
| 812 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 813 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 814 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 815 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 816 | |
| 817 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
| 818 | return( mbedtls_psa_cipher_encrypt( attributes, |
| 819 | key_buffer, |
| 820 | key_buffer_size, |
| 821 | alg, |
Ronald Cron | a833169 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 822 | iv, |
| 823 | iv_length, |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 824 | input, |
| 825 | input_length, |
| 826 | output, |
| 827 | output_size, |
| 828 | output_length ) ); |
gabor-mezei-arm | d52c8dc | 2021-06-25 15:43:32 +0200 | [diff] [blame] | 829 | #else |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 830 | return( PSA_ERROR_NOT_SUPPORTED ); |
gabor-mezei-arm | d52c8dc | 2021-06-25 15:43:32 +0200 | [diff] [blame] | 831 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 832 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 833 | /* Add cases for opaque driver here */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 834 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 835 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 836 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 837 | return( mbedtls_test_opaque_cipher_encrypt( attributes, |
| 838 | key_buffer, |
| 839 | key_buffer_size, |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 840 | alg, |
Ronald Cron | a833169 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 841 | iv, |
| 842 | iv_length, |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 843 | input, |
| 844 | input_length, |
| 845 | output, |
| 846 | output_size, |
| 847 | output_length ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 848 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 849 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 850 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 851 | default: |
| 852 | /* Key is declared with a lifetime not known to us */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 853 | (void)status; |
| 854 | (void)key_buffer; |
| 855 | (void)key_buffer_size; |
| 856 | (void)alg; |
Gilles Peskine | 03efa0b | 2022-04-12 17:08:34 +0200 | [diff] [blame] | 857 | (void)iv; |
| 858 | (void)iv_length; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 859 | (void)input; |
| 860 | (void)input_length; |
| 861 | (void)output; |
| 862 | (void)output_size; |
| 863 | (void)output_length; |
| 864 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 865 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | psa_status_t psa_driver_wrapper_cipher_decrypt( |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 869 | const psa_key_attributes_t *attributes, |
| 870 | const uint8_t *key_buffer, |
| 871 | size_t key_buffer_size, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 872 | psa_algorithm_t alg, |
| 873 | const uint8_t *input, |
| 874 | size_t input_length, |
| 875 | uint8_t *output, |
| 876 | size_t output_size, |
| 877 | size_t *output_length ) |
| 878 | { |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 879 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 880 | psa_key_location_t location = |
| 881 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 882 | |
| 883 | switch( location ) |
| 884 | { |
| 885 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 886 | /* Key is stored in the slot in export representation, so |
| 887 | * cycle through all known transparent accelerators */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 888 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 889 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 890 | status = mbedtls_test_transparent_cipher_decrypt( attributes, |
| 891 | key_buffer, |
| 892 | key_buffer_size, |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 893 | alg, |
| 894 | input, |
| 895 | input_length, |
| 896 | output, |
| 897 | output_size, |
| 898 | output_length ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 899 | /* Declared with fallback == true */ |
| 900 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 901 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 902 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 903 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 904 | |
| 905 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
| 906 | return( mbedtls_psa_cipher_decrypt( attributes, |
| 907 | key_buffer, |
| 908 | key_buffer_size, |
| 909 | alg, |
| 910 | input, |
| 911 | input_length, |
| 912 | output, |
| 913 | output_size, |
| 914 | output_length ) ); |
gabor-mezei-arm | d52c8dc | 2021-06-25 15:43:32 +0200 | [diff] [blame] | 915 | #else |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 916 | return( PSA_ERROR_NOT_SUPPORTED ); |
gabor-mezei-arm | d52c8dc | 2021-06-25 15:43:32 +0200 | [diff] [blame] | 917 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 918 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 919 | /* Add cases for opaque driver here */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 920 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 921 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 922 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 923 | return( mbedtls_test_opaque_cipher_decrypt( attributes, |
| 924 | key_buffer, |
| 925 | key_buffer_size, |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 926 | alg, |
| 927 | input, |
| 928 | input_length, |
| 929 | output, |
| 930 | output_size, |
| 931 | output_length ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 932 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 933 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 934 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 935 | default: |
| 936 | /* Key is declared with a lifetime not known to us */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 937 | (void)status; |
| 938 | (void)key_buffer; |
| 939 | (void)key_buffer_size; |
| 940 | (void)alg; |
| 941 | (void)input; |
| 942 | (void)input_length; |
| 943 | (void)output; |
| 944 | (void)output_size; |
| 945 | (void)output_length; |
| 946 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 947 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | psa_status_t psa_driver_wrapper_cipher_encrypt_setup( |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 951 | psa_cipher_operation_t *operation, |
| 952 | const psa_key_attributes_t *attributes, |
| 953 | const uint8_t *key_buffer, size_t key_buffer_size, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 954 | psa_algorithm_t alg ) |
| 955 | { |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 956 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 957 | psa_key_location_t location = |
| 958 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 959 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 960 | switch( location ) |
| 961 | { |
| 962 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 963 | /* Key is stored in the slot in export representation, so |
| 964 | * cycle through all known transparent accelerators */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 965 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 966 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 967 | status = mbedtls_test_transparent_cipher_encrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 968 | &operation->ctx.transparent_test_driver_ctx, |
| 969 | attributes, |
| 970 | key_buffer, |
| 971 | key_buffer_size, |
| 972 | alg ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 973 | /* Declared with fallback == true */ |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 974 | if( status == PSA_SUCCESS ) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 975 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 976 | |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 977 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 978 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 979 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 980 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 981 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 982 | /* Fell through, meaning no accelerator supports this operation */ |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 983 | status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 984 | attributes, |
| 985 | key_buffer, |
| 986 | key_buffer_size, |
| 987 | alg ); |
| 988 | if( status == PSA_SUCCESS ) |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 989 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 990 | |
Ronald Cron | 7b4154d | 2021-03-19 14:49:41 +0100 | [diff] [blame] | 991 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 992 | return( status ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 993 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
| 994 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 995 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 996 | /* Add cases for opaque driver here */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 997 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 998 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 999 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1000 | status = mbedtls_test_opaque_cipher_encrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1001 | &operation->ctx.opaque_test_driver_ctx, |
| 1002 | attributes, |
| 1003 | key_buffer, key_buffer_size, |
| 1004 | alg ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1005 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1006 | if( status == PSA_SUCCESS ) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1007 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1008 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1009 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1010 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1011 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1012 | default: |
| 1013 | /* Key is declared with a lifetime not known to us */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1014 | (void)status; |
Ronald Cron | 485559e | 2021-04-28 14:29:00 +0200 | [diff] [blame] | 1015 | (void)operation; |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1016 | (void)key_buffer; |
| 1017 | (void)key_buffer_size; |
| 1018 | (void)alg; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 1019 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1020 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | psa_status_t psa_driver_wrapper_cipher_decrypt_setup( |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 1024 | psa_cipher_operation_t *operation, |
| 1025 | const psa_key_attributes_t *attributes, |
| 1026 | const uint8_t *key_buffer, size_t key_buffer_size, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1027 | psa_algorithm_t alg ) |
| 1028 | { |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1029 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 1030 | psa_key_location_t location = |
| 1031 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1032 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1033 | switch( location ) |
| 1034 | { |
| 1035 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1036 | /* Key is stored in the slot in export representation, so |
| 1037 | * cycle through all known transparent accelerators */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1038 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1039 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1040 | status = mbedtls_test_transparent_cipher_decrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1041 | &operation->ctx.transparent_test_driver_ctx, |
| 1042 | attributes, |
| 1043 | key_buffer, |
| 1044 | key_buffer_size, |
| 1045 | alg ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1046 | /* Declared with fallback == true */ |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 1047 | if( status == PSA_SUCCESS ) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1048 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 1049 | |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1050 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1051 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1052 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1053 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1054 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1055 | /* Fell through, meaning no accelerator supports this operation */ |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 1056 | status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1057 | attributes, |
| 1058 | key_buffer, |
| 1059 | key_buffer_size, |
| 1060 | alg ); |
| 1061 | if( status == PSA_SUCCESS ) |
| 1062 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
| 1063 | |
| 1064 | return( status ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1065 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
| 1066 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1067 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1068 | /* Add cases for opaque driver here */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1069 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1070 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 1071 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1072 | status = mbedtls_test_opaque_cipher_decrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1073 | &operation->ctx.opaque_test_driver_ctx, |
| 1074 | attributes, |
| 1075 | key_buffer, key_buffer_size, |
| 1076 | alg ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1077 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1078 | if( status == PSA_SUCCESS ) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1079 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1080 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1081 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1082 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1083 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1084 | default: |
| 1085 | /* Key is declared with a lifetime not known to us */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1086 | (void)status; |
Ronald Cron | 485559e | 2021-04-28 14:29:00 +0200 | [diff] [blame] | 1087 | (void)operation; |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1088 | (void)key_buffer; |
| 1089 | (void)key_buffer_size; |
| 1090 | (void)alg; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 1091 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1092 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1093 | } |
| 1094 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1095 | psa_status_t psa_driver_wrapper_cipher_set_iv( |
Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 1096 | psa_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1097 | const uint8_t *iv, |
| 1098 | size_t iv_length ) |
| 1099 | { |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1100 | switch( operation->id ) |
| 1101 | { |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1102 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1103 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 1104 | return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1105 | iv, |
| 1106 | iv_length ) ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1107 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1108 | |
| 1109 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1110 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1111 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1112 | return( mbedtls_test_transparent_cipher_set_iv( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1113 | &operation->ctx.transparent_test_driver_ctx, |
| 1114 | iv, iv_length ) ); |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1115 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1116 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1117 | return( mbedtls_test_opaque_cipher_set_iv( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1118 | &operation->ctx.opaque_test_driver_ctx, |
| 1119 | iv, iv_length ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1120 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1121 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1122 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1123 | |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1124 | (void)iv; |
| 1125 | (void)iv_length; |
| 1126 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1127 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | psa_status_t psa_driver_wrapper_cipher_update( |
Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 1131 | psa_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1132 | const uint8_t *input, |
| 1133 | size_t input_length, |
| 1134 | uint8_t *output, |
| 1135 | size_t output_size, |
| 1136 | size_t *output_length ) |
| 1137 | { |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1138 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1139 | { |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1140 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1141 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 1142 | return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1143 | input, |
| 1144 | input_length, |
| 1145 | output, |
| 1146 | output_size, |
| 1147 | output_length ) ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1148 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
| 1149 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1150 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1151 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1152 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1153 | return( mbedtls_test_transparent_cipher_update( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1154 | &operation->ctx.transparent_test_driver_ctx, |
| 1155 | input, input_length, |
| 1156 | output, output_size, output_length ) ); |
| 1157 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1158 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1159 | return( mbedtls_test_opaque_cipher_update( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1160 | &operation->ctx.opaque_test_driver_ctx, |
| 1161 | input, input_length, |
| 1162 | output, output_size, output_length ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1163 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1164 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1165 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1166 | |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1167 | (void)input; |
| 1168 | (void)input_length; |
| 1169 | (void)output; |
| 1170 | (void)output_size; |
| 1171 | (void)output_length; |
| 1172 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1173 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | psa_status_t psa_driver_wrapper_cipher_finish( |
Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 1177 | psa_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1178 | uint8_t *output, |
| 1179 | size_t output_size, |
| 1180 | size_t *output_length ) |
| 1181 | { |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1182 | switch( operation->id ) |
| 1183 | { |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1184 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1185 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 1186 | return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1187 | output, |
| 1188 | output_size, |
| 1189 | output_length ) ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1190 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1191 | |
| 1192 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1193 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1194 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1195 | return( mbedtls_test_transparent_cipher_finish( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1196 | &operation->ctx.transparent_test_driver_ctx, |
| 1197 | output, output_size, output_length ) ); |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1198 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1199 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1200 | return( mbedtls_test_opaque_cipher_finish( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1201 | &operation->ctx.opaque_test_driver_ctx, |
| 1202 | output, output_size, output_length ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1203 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1204 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1205 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1206 | |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1207 | (void)output; |
| 1208 | (void)output_size; |
| 1209 | (void)output_length; |
| 1210 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1211 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | psa_status_t psa_driver_wrapper_cipher_abort( |
Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 1215 | psa_cipher_operation_t *operation ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1216 | { |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1217 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1218 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1219 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1220 | { |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1221 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1222 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 1223 | return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1224 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1225 | |
| 1226 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1227 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1228 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1229 | status = mbedtls_test_transparent_cipher_abort( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1230 | &operation->ctx.transparent_test_driver_ctx ); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1231 | mbedtls_platform_zeroize( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1232 | &operation->ctx.transparent_test_driver_ctx, |
| 1233 | sizeof( operation->ctx.transparent_test_driver_ctx ) ); |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1234 | return( status ); |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1235 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1236 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1237 | status = mbedtls_test_opaque_cipher_abort( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1238 | &operation->ctx.opaque_test_driver_ctx ); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1239 | mbedtls_platform_zeroize( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1240 | &operation->ctx.opaque_test_driver_ctx, |
| 1241 | sizeof( operation->ctx.opaque_test_driver_ctx ) ); |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1242 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1243 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1244 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1245 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1246 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1247 | (void)status; |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1248 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1249 | } |
| 1250 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1251 | /* |
| 1252 | * Hashing functions |
| 1253 | */ |
| 1254 | psa_status_t psa_driver_wrapper_hash_compute( |
| 1255 | psa_algorithm_t alg, |
| 1256 | const uint8_t *input, |
| 1257 | size_t input_length, |
| 1258 | uint8_t *hash, |
| 1259 | size_t hash_size, |
| 1260 | size_t *hash_length) |
| 1261 | { |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1262 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1263 | |
| 1264 | /* Try accelerators first */ |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1265 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1266 | status = mbedtls_test_transparent_hash_compute( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1267 | alg, input, input_length, hash, hash_size, hash_length ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1268 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1269 | return( status ); |
| 1270 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1271 | |
| 1272 | /* If software fallback is compiled in, try fallback */ |
| 1273 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1274 | status = mbedtls_psa_hash_compute( alg, input, input_length, |
| 1275 | hash, hash_size, hash_length ); |
| 1276 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1277 | return( status ); |
| 1278 | #endif |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1279 | (void) status; |
| 1280 | (void) alg; |
| 1281 | (void) input; |
| 1282 | (void) input_length; |
| 1283 | (void) hash; |
| 1284 | (void) hash_size; |
| 1285 | (void) hash_length; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1286 | |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1287 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | psa_status_t psa_driver_wrapper_hash_setup( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1291 | psa_hash_operation_t *operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1292 | psa_algorithm_t alg ) |
| 1293 | { |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1294 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1295 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1296 | /* Try setup on accelerators first */ |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1297 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1298 | status = mbedtls_test_transparent_hash_setup( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1299 | &operation->ctx.test_driver_ctx, alg ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1300 | if( status == PSA_SUCCESS ) |
| 1301 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
| 1302 | |
| 1303 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1304 | return( status ); |
| 1305 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1306 | |
| 1307 | /* If software fallback is compiled in, try fallback */ |
| 1308 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1309 | status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg ); |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1310 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1311 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1312 | |
| 1313 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1314 | return( status ); |
| 1315 | #endif |
| 1316 | /* Nothing left to try if we fall through here */ |
| 1317 | (void) status; |
| 1318 | (void) operation; |
| 1319 | (void) alg; |
| 1320 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1321 | } |
| 1322 | |
| 1323 | psa_status_t psa_driver_wrapper_hash_clone( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1324 | const psa_hash_operation_t *source_operation, |
| 1325 | psa_hash_operation_t *target_operation ) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1326 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1327 | switch( source_operation->id ) |
| 1328 | { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1329 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1330 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
| 1331 | target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
| 1332 | return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx, |
| 1333 | &target_operation->ctx.mbedtls_ctx ) ); |
| 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: |
| 1337 | target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1338 | return( mbedtls_test_transparent_hash_clone( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1339 | &source_operation->ctx.test_driver_ctx, |
| 1340 | &target_operation->ctx.test_driver_ctx ) ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1341 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1342 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1343 | (void) target_operation; |
| 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_update( |
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 | const uint8_t *input, |
| 1351 | size_t input_length ) |
| 1352 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1353 | switch( operation->id ) |
| 1354 | { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1355 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1356 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
| 1357 | return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx, |
| 1358 | input, input_length ) ); |
| 1359 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1360 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1361 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1362 | return( mbedtls_test_transparent_hash_update( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1363 | &operation->ctx.test_driver_ctx, |
| 1364 | input, input_length ) ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1365 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1366 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1367 | (void) input; |
| 1368 | (void) input_length; |
| 1369 | return( PSA_ERROR_BAD_STATE ); |
| 1370 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | psa_status_t psa_driver_wrapper_hash_finish( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1374 | psa_hash_operation_t *operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1375 | uint8_t *hash, |
| 1376 | size_t hash_size, |
| 1377 | size_t *hash_length ) |
| 1378 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1379 | switch( operation->id ) |
| 1380 | { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1381 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1382 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
| 1383 | return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx, |
| 1384 | hash, hash_size, hash_length ) ); |
| 1385 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1386 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1387 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1388 | return( mbedtls_test_transparent_hash_finish( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1389 | &operation->ctx.test_driver_ctx, |
| 1390 | hash, hash_size, hash_length ) ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1391 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1392 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1393 | (void) hash; |
| 1394 | (void) hash_size; |
| 1395 | (void) hash_length; |
| 1396 | return( PSA_ERROR_BAD_STATE ); |
| 1397 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | psa_status_t psa_driver_wrapper_hash_abort( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1401 | psa_hash_operation_t *operation ) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1402 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1403 | switch( operation->id ) |
| 1404 | { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1405 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1406 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
| 1407 | return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) ); |
| 1408 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1409 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1410 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1411 | return( mbedtls_test_transparent_hash_abort( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1412 | &operation->ctx.test_driver_ctx ) ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1413 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1414 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1415 | return( PSA_ERROR_BAD_STATE ); |
| 1416 | } |
| 1417 | } |
| 1418 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1419 | psa_status_t psa_driver_wrapper_aead_encrypt( |
| 1420 | const psa_key_attributes_t *attributes, |
| 1421 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1422 | psa_algorithm_t alg, |
| 1423 | const uint8_t *nonce, size_t nonce_length, |
| 1424 | const uint8_t *additional_data, size_t additional_data_length, |
| 1425 | const uint8_t *plaintext, size_t plaintext_length, |
| 1426 | uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length ) |
| 1427 | { |
| 1428 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1429 | psa_key_location_t location = |
| 1430 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 1431 | |
| 1432 | switch( location ) |
| 1433 | { |
| 1434 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1435 | /* Key is stored in the slot in export representation, so |
| 1436 | * cycle through all known transparent accelerators */ |
| 1437 | |
| 1438 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1439 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1440 | status = mbedtls_test_transparent_aead_encrypt( |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1441 | attributes, key_buffer, key_buffer_size, |
| 1442 | alg, |
| 1443 | nonce, nonce_length, |
| 1444 | additional_data, additional_data_length, |
| 1445 | plaintext, plaintext_length, |
| 1446 | ciphertext, ciphertext_size, ciphertext_length ); |
| 1447 | /* Declared with fallback == true */ |
| 1448 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1449 | return( status ); |
| 1450 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1451 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1452 | |
| 1453 | /* Fell through, meaning no accelerator supports this operation */ |
| 1454 | return( mbedtls_psa_aead_encrypt( |
| 1455 | attributes, key_buffer, key_buffer_size, |
| 1456 | alg, |
| 1457 | nonce, nonce_length, |
| 1458 | additional_data, additional_data_length, |
| 1459 | plaintext, plaintext_length, |
| 1460 | ciphertext, ciphertext_size, ciphertext_length ) ); |
| 1461 | |
| 1462 | /* Add cases for opaque driver here */ |
| 1463 | |
| 1464 | default: |
| 1465 | /* Key is declared with a lifetime not known to us */ |
| 1466 | (void)status; |
| 1467 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | psa_status_t psa_driver_wrapper_aead_decrypt( |
| 1472 | const psa_key_attributes_t *attributes, |
| 1473 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1474 | psa_algorithm_t alg, |
| 1475 | const uint8_t *nonce, size_t nonce_length, |
| 1476 | const uint8_t *additional_data, size_t additional_data_length, |
| 1477 | const uint8_t *ciphertext, size_t ciphertext_length, |
| 1478 | uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length ) |
| 1479 | { |
| 1480 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1481 | psa_key_location_t location = |
| 1482 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 1483 | |
| 1484 | switch( location ) |
| 1485 | { |
| 1486 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1487 | /* Key is stored in the slot in export representation, so |
| 1488 | * cycle through all known transparent accelerators */ |
| 1489 | |
| 1490 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1491 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1492 | status = mbedtls_test_transparent_aead_decrypt( |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1493 | attributes, key_buffer, key_buffer_size, |
| 1494 | alg, |
| 1495 | nonce, nonce_length, |
| 1496 | additional_data, additional_data_length, |
| 1497 | ciphertext, ciphertext_length, |
| 1498 | plaintext, plaintext_size, plaintext_length ); |
| 1499 | /* Declared with fallback == true */ |
| 1500 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1501 | return( status ); |
| 1502 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1503 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1504 | |
| 1505 | /* Fell through, meaning no accelerator supports this operation */ |
| 1506 | return( mbedtls_psa_aead_decrypt( |
| 1507 | attributes, key_buffer, key_buffer_size, |
| 1508 | alg, |
| 1509 | nonce, nonce_length, |
| 1510 | additional_data, additional_data_length, |
| 1511 | ciphertext, ciphertext_length, |
| 1512 | plaintext, plaintext_size, plaintext_length ) ); |
| 1513 | |
| 1514 | /* Add cases for opaque driver here */ |
| 1515 | |
| 1516 | default: |
| 1517 | /* Key is declared with a lifetime not known to us */ |
| 1518 | (void)status; |
| 1519 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1520 | } |
| 1521 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1522 | |
| 1523 | |
| 1524 | /* |
| 1525 | * MAC functions |
| 1526 | */ |
| 1527 | psa_status_t psa_driver_wrapper_mac_compute( |
| 1528 | const psa_key_attributes_t *attributes, |
| 1529 | const uint8_t *key_buffer, |
| 1530 | size_t key_buffer_size, |
| 1531 | psa_algorithm_t alg, |
| 1532 | const uint8_t *input, |
| 1533 | size_t input_length, |
| 1534 | uint8_t *mac, |
| 1535 | size_t mac_size, |
| 1536 | size_t *mac_length ) |
| 1537 | { |
| 1538 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1539 | psa_key_location_t location = |
| 1540 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 1541 | |
| 1542 | switch( location ) |
| 1543 | { |
| 1544 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1545 | /* Key is stored in the slot in export representation, so |
| 1546 | * cycle through all known transparent accelerators */ |
| 1547 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1548 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1549 | status = mbedtls_test_transparent_mac_compute( |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1550 | attributes, key_buffer, key_buffer_size, alg, |
| 1551 | input, input_length, |
| 1552 | mac, mac_size, mac_length ); |
| 1553 | /* Declared with fallback == true */ |
| 1554 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1555 | return( status ); |
| 1556 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1557 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1558 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1559 | /* Fell through, meaning no accelerator supports this operation */ |
| 1560 | status = mbedtls_psa_mac_compute( |
| 1561 | attributes, key_buffer, key_buffer_size, alg, |
| 1562 | input, input_length, |
| 1563 | mac, mac_size, mac_length ); |
| 1564 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1565 | return( status ); |
| 1566 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1567 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1568 | |
| 1569 | /* Add cases for opaque driver here */ |
| 1570 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1571 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1572 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1573 | status = mbedtls_test_opaque_mac_compute( |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1574 | attributes, key_buffer, key_buffer_size, alg, |
| 1575 | input, input_length, |
| 1576 | mac, mac_size, mac_length ); |
| 1577 | return( status ); |
| 1578 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1579 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1580 | default: |
| 1581 | /* Key is declared with a lifetime not known to us */ |
| 1582 | (void) key_buffer; |
| 1583 | (void) key_buffer_size; |
| 1584 | (void) alg; |
| 1585 | (void) input; |
| 1586 | (void) input_length; |
| 1587 | (void) mac; |
| 1588 | (void) mac_size; |
| 1589 | (void) mac_length; |
| 1590 | (void) status; |
| 1591 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | psa_status_t psa_driver_wrapper_mac_sign_setup( |
| 1596 | psa_mac_operation_t *operation, |
| 1597 | const psa_key_attributes_t *attributes, |
| 1598 | const uint8_t *key_buffer, |
| 1599 | size_t key_buffer_size, |
| 1600 | psa_algorithm_t alg ) |
| 1601 | { |
| 1602 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1603 | psa_key_location_t location = |
| 1604 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 1605 | |
| 1606 | switch( location ) |
| 1607 | { |
| 1608 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1609 | /* Key is stored in the slot in export representation, so |
| 1610 | * cycle through all known transparent accelerators */ |
| 1611 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1612 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1613 | status = mbedtls_test_transparent_mac_sign_setup( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1614 | &operation->ctx.transparent_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1615 | attributes, |
| 1616 | key_buffer, key_buffer_size, |
| 1617 | alg ); |
| 1618 | /* Declared with fallback == true */ |
| 1619 | if( status == PSA_SUCCESS ) |
| 1620 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
| 1621 | |
| 1622 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1623 | return( status ); |
| 1624 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1625 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1626 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1627 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1628 | status = mbedtls_psa_mac_sign_setup( &operation->ctx.mbedtls_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1629 | attributes, |
| 1630 | key_buffer, key_buffer_size, |
| 1631 | alg ); |
| 1632 | if( status == PSA_SUCCESS ) |
| 1633 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
| 1634 | |
| 1635 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1636 | return( status ); |
| 1637 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1638 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1639 | |
| 1640 | /* Add cases for opaque driver here */ |
| 1641 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1642 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1643 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1644 | status = mbedtls_test_opaque_mac_sign_setup( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1645 | &operation->ctx.opaque_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1646 | attributes, |
| 1647 | key_buffer, key_buffer_size, |
| 1648 | alg ); |
| 1649 | |
| 1650 | if( status == PSA_SUCCESS ) |
| 1651 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
| 1652 | |
| 1653 | return( status ); |
| 1654 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1655 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1656 | default: |
| 1657 | /* Key is declared with a lifetime not known to us */ |
| 1658 | (void) status; |
Ronald Cron | 485559e | 2021-04-28 14:29:00 +0200 | [diff] [blame] | 1659 | (void) operation; |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1660 | (void) key_buffer; |
| 1661 | (void) key_buffer_size; |
| 1662 | (void) alg; |
| 1663 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | psa_status_t psa_driver_wrapper_mac_verify_setup( |
| 1668 | psa_mac_operation_t *operation, |
| 1669 | const psa_key_attributes_t *attributes, |
| 1670 | const uint8_t *key_buffer, |
| 1671 | size_t key_buffer_size, |
| 1672 | psa_algorithm_t alg ) |
| 1673 | { |
| 1674 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1675 | psa_key_location_t location = |
| 1676 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 1677 | |
| 1678 | switch( location ) |
| 1679 | { |
| 1680 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1681 | /* Key is stored in the slot in export representation, so |
| 1682 | * cycle through all known transparent accelerators */ |
| 1683 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1684 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1685 | status = mbedtls_test_transparent_mac_verify_setup( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1686 | &operation->ctx.transparent_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1687 | attributes, |
| 1688 | key_buffer, key_buffer_size, |
| 1689 | alg ); |
| 1690 | /* Declared with fallback == true */ |
| 1691 | if( status == PSA_SUCCESS ) |
| 1692 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
| 1693 | |
| 1694 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1695 | return( status ); |
| 1696 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1697 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1698 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1699 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1700 | status = mbedtls_psa_mac_verify_setup( &operation->ctx.mbedtls_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1701 | attributes, |
| 1702 | key_buffer, key_buffer_size, |
| 1703 | alg ); |
| 1704 | if( status == PSA_SUCCESS ) |
| 1705 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
| 1706 | |
| 1707 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1708 | return( status ); |
| 1709 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1710 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1711 | |
| 1712 | /* Add cases for opaque driver here */ |
| 1713 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1714 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1715 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1716 | status = mbedtls_test_opaque_mac_verify_setup( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1717 | &operation->ctx.opaque_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1718 | attributes, |
| 1719 | key_buffer, key_buffer_size, |
| 1720 | alg ); |
| 1721 | |
| 1722 | if( status == PSA_SUCCESS ) |
| 1723 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
| 1724 | |
| 1725 | return( status ); |
| 1726 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1727 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1728 | default: |
| 1729 | /* Key is declared with a lifetime not known to us */ |
| 1730 | (void) status; |
Ronald Cron | 485559e | 2021-04-28 14:29:00 +0200 | [diff] [blame] | 1731 | (void) operation; |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1732 | (void) key_buffer; |
| 1733 | (void) key_buffer_size; |
| 1734 | (void) alg; |
| 1735 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | psa_status_t psa_driver_wrapper_mac_update( |
| 1740 | psa_mac_operation_t *operation, |
| 1741 | const uint8_t *input, |
| 1742 | size_t input_length ) |
| 1743 | { |
| 1744 | switch( operation->id ) |
| 1745 | { |
| 1746 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1747 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1748 | return( mbedtls_psa_mac_update( &operation->ctx.mbedtls_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1749 | input, input_length ) ); |
| 1750 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1751 | |
| 1752 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1753 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1754 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1755 | return( mbedtls_test_transparent_mac_update( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1756 | &operation->ctx.transparent_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1757 | input, input_length ) ); |
| 1758 | |
| 1759 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1760 | return( mbedtls_test_opaque_mac_update( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1761 | &operation->ctx.opaque_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1762 | input, input_length ) ); |
| 1763 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1764 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1765 | default: |
| 1766 | (void) input; |
| 1767 | (void) input_length; |
| 1768 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1769 | } |
| 1770 | } |
| 1771 | |
| 1772 | psa_status_t psa_driver_wrapper_mac_sign_finish( |
| 1773 | psa_mac_operation_t *operation, |
| 1774 | uint8_t *mac, |
| 1775 | size_t mac_size, |
| 1776 | size_t *mac_length ) |
| 1777 | { |
| 1778 | switch( operation->id ) |
| 1779 | { |
| 1780 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1781 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1782 | return( mbedtls_psa_mac_sign_finish( &operation->ctx.mbedtls_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1783 | mac, mac_size, mac_length ) ); |
| 1784 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1785 | |
| 1786 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1787 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1788 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1789 | return( mbedtls_test_transparent_mac_sign_finish( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1790 | &operation->ctx.transparent_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1791 | mac, mac_size, mac_length ) ); |
| 1792 | |
| 1793 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1794 | return( mbedtls_test_opaque_mac_sign_finish( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1795 | &operation->ctx.opaque_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1796 | mac, mac_size, mac_length ) ); |
| 1797 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1798 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1799 | default: |
| 1800 | (void) mac; |
| 1801 | (void) mac_size; |
| 1802 | (void) mac_length; |
| 1803 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1804 | } |
| 1805 | } |
| 1806 | |
| 1807 | psa_status_t psa_driver_wrapper_mac_verify_finish( |
| 1808 | psa_mac_operation_t *operation, |
| 1809 | const uint8_t *mac, |
| 1810 | size_t mac_length ) |
| 1811 | { |
| 1812 | switch( operation->id ) |
| 1813 | { |
| 1814 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1815 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1816 | return( mbedtls_psa_mac_verify_finish( &operation->ctx.mbedtls_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1817 | mac, mac_length ) ); |
| 1818 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1819 | |
| 1820 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1821 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1822 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1823 | return( mbedtls_test_transparent_mac_verify_finish( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1824 | &operation->ctx.transparent_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1825 | mac, mac_length ) ); |
| 1826 | |
| 1827 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1828 | return( mbedtls_test_opaque_mac_verify_finish( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1829 | &operation->ctx.opaque_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1830 | mac, mac_length ) ); |
| 1831 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1832 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1833 | default: |
| 1834 | (void) mac; |
| 1835 | (void) mac_length; |
| 1836 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1837 | } |
| 1838 | } |
| 1839 | |
| 1840 | psa_status_t psa_driver_wrapper_mac_abort( |
| 1841 | psa_mac_operation_t *operation ) |
| 1842 | { |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1843 | switch( operation->id ) |
| 1844 | { |
| 1845 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1846 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 1847 | return( mbedtls_psa_mac_abort( &operation->ctx.mbedtls_ctx ) ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1848 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1849 | |
| 1850 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1851 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1852 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1853 | return( mbedtls_test_transparent_mac_abort( |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 1854 | &operation->ctx.transparent_test_driver_ctx ) ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1855 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1856 | return( mbedtls_test_opaque_mac_abort( |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 1857 | &operation->ctx.opaque_test_driver_ctx ) ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1858 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1859 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1860 | default: |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 1861 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1862 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1863 | } |
Gilles Peskine | ad0e012 | 2021-04-24 13:19:45 +0200 | [diff] [blame] | 1864 | |
| 1865 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |