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 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 278 | #endif /* 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 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 362 | #endif /* 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 | } |
| 582 | #endif /* PSA_CRYPTO_SE_C */ |
| 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 | } |
| 644 | #endif /* PSA_CRYPTO_SE_C */ |
| 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, |
| 781 | const uint8_t *input, |
| 782 | size_t input_length, |
| 783 | uint8_t *output, |
| 784 | size_t output_size, |
| 785 | size_t *output_length ) |
| 786 | { |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 787 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 788 | psa_key_location_t location = |
| 789 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 790 | |
| 791 | switch( location ) |
| 792 | { |
| 793 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 794 | /* Key is stored in the slot in export representation, so |
| 795 | * cycle through all known transparent accelerators */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 796 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 797 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 798 | status = mbedtls_test_transparent_cipher_encrypt( attributes, |
| 799 | key_buffer, |
| 800 | key_buffer_size, |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 801 | alg, |
| 802 | input, |
| 803 | input_length, |
| 804 | output, |
| 805 | output_size, |
| 806 | output_length ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 807 | /* Declared with fallback == true */ |
| 808 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 809 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 810 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 811 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 812 | |
| 813 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
| 814 | return( mbedtls_psa_cipher_encrypt( attributes, |
| 815 | key_buffer, |
| 816 | key_buffer_size, |
| 817 | alg, |
| 818 | input, |
| 819 | input_length, |
| 820 | output, |
| 821 | output_size, |
| 822 | output_length ) ); |
gabor-mezei-arm | d52c8dc | 2021-06-25 15:43:32 +0200 | [diff] [blame] | 823 | #else |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 824 | return( PSA_ERROR_NOT_SUPPORTED ); |
gabor-mezei-arm | d52c8dc | 2021-06-25 15:43:32 +0200 | [diff] [blame] | 825 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 826 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 827 | /* Add cases for opaque driver here */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 828 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 829 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 830 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 831 | return( mbedtls_test_opaque_cipher_encrypt( attributes, |
| 832 | key_buffer, |
| 833 | key_buffer_size, |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 834 | alg, |
| 835 | input, |
| 836 | input_length, |
| 837 | output, |
| 838 | output_size, |
| 839 | output_length ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 840 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 841 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 842 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 843 | default: |
| 844 | /* Key is declared with a lifetime not known to us */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 845 | (void)status; |
| 846 | (void)key_buffer; |
| 847 | (void)key_buffer_size; |
| 848 | (void)alg; |
| 849 | (void)input; |
| 850 | (void)input_length; |
| 851 | (void)output; |
| 852 | (void)output_size; |
| 853 | (void)output_length; |
| 854 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 855 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | psa_status_t psa_driver_wrapper_cipher_decrypt( |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 859 | const psa_key_attributes_t *attributes, |
| 860 | const uint8_t *key_buffer, |
| 861 | size_t key_buffer_size, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 862 | psa_algorithm_t alg, |
| 863 | const uint8_t *input, |
| 864 | size_t input_length, |
| 865 | uint8_t *output, |
| 866 | size_t output_size, |
| 867 | size_t *output_length ) |
| 868 | { |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 869 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 870 | psa_key_location_t location = |
| 871 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 872 | |
| 873 | switch( location ) |
| 874 | { |
| 875 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 876 | /* Key is stored in the slot in export representation, so |
| 877 | * cycle through all known transparent accelerators */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 878 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 879 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 880 | status = mbedtls_test_transparent_cipher_decrypt( attributes, |
| 881 | key_buffer, |
| 882 | key_buffer_size, |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 883 | alg, |
| 884 | input, |
| 885 | input_length, |
| 886 | output, |
| 887 | output_size, |
| 888 | output_length ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 889 | /* Declared with fallback == true */ |
| 890 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 891 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 892 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 893 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 894 | |
| 895 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
| 896 | return( mbedtls_psa_cipher_decrypt( attributes, |
| 897 | key_buffer, |
| 898 | key_buffer_size, |
| 899 | alg, |
| 900 | input, |
| 901 | input_length, |
| 902 | output, |
| 903 | output_size, |
| 904 | output_length ) ); |
gabor-mezei-arm | d52c8dc | 2021-06-25 15:43:32 +0200 | [diff] [blame] | 905 | #else |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 906 | return( PSA_ERROR_NOT_SUPPORTED ); |
gabor-mezei-arm | d52c8dc | 2021-06-25 15:43:32 +0200 | [diff] [blame] | 907 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 908 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 909 | /* Add cases for opaque driver here */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 910 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 911 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 912 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 913 | return( mbedtls_test_opaque_cipher_decrypt( attributes, |
| 914 | key_buffer, |
| 915 | key_buffer_size, |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 916 | alg, |
| 917 | input, |
| 918 | input_length, |
| 919 | output, |
| 920 | output_size, |
| 921 | output_length ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 922 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 923 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 924 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 925 | default: |
| 926 | /* Key is declared with a lifetime not known to us */ |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 927 | (void)status; |
| 928 | (void)key_buffer; |
| 929 | (void)key_buffer_size; |
| 930 | (void)alg; |
| 931 | (void)input; |
| 932 | (void)input_length; |
| 933 | (void)output; |
| 934 | (void)output_size; |
| 935 | (void)output_length; |
| 936 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 937 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | psa_status_t psa_driver_wrapper_cipher_encrypt_setup( |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 941 | psa_cipher_operation_t *operation, |
| 942 | const psa_key_attributes_t *attributes, |
| 943 | const uint8_t *key_buffer, size_t key_buffer_size, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 944 | psa_algorithm_t alg ) |
| 945 | { |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 946 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 947 | psa_key_location_t location = |
| 948 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 949 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 950 | switch( location ) |
| 951 | { |
| 952 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 953 | /* Key is stored in the slot in export representation, so |
| 954 | * cycle through all known transparent accelerators */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 955 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 956 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 957 | status = mbedtls_test_transparent_cipher_encrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 958 | &operation->ctx.transparent_test_driver_ctx, |
| 959 | attributes, |
| 960 | key_buffer, |
| 961 | key_buffer_size, |
| 962 | alg ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 963 | /* Declared with fallback == true */ |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 964 | if( status == PSA_SUCCESS ) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 965 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 966 | |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 967 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 968 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 969 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 970 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 971 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 972 | /* Fell through, meaning no accelerator supports this operation */ |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 973 | status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 974 | attributes, |
| 975 | key_buffer, |
| 976 | key_buffer_size, |
| 977 | alg ); |
| 978 | if( status == PSA_SUCCESS ) |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 979 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 980 | |
Ronald Cron | 7b4154d | 2021-03-19 14:49:41 +0100 | [diff] [blame] | 981 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 982 | return( status ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 983 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
| 984 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 985 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 986 | /* Add cases for opaque driver here */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 987 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 988 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 989 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 990 | status = mbedtls_test_opaque_cipher_encrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 991 | &operation->ctx.opaque_test_driver_ctx, |
| 992 | attributes, |
| 993 | key_buffer, key_buffer_size, |
| 994 | alg ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 995 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 996 | if( status == PSA_SUCCESS ) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 997 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 998 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 999 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1000 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1001 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1002 | default: |
| 1003 | /* Key is declared with a lifetime not known to us */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1004 | (void)status; |
Ronald Cron | 485559e | 2021-04-28 14:29:00 +0200 | [diff] [blame] | 1005 | (void)operation; |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1006 | (void)key_buffer; |
| 1007 | (void)key_buffer_size; |
| 1008 | (void)alg; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 1009 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1010 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | psa_status_t psa_driver_wrapper_cipher_decrypt_setup( |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 1014 | psa_cipher_operation_t *operation, |
| 1015 | const psa_key_attributes_t *attributes, |
| 1016 | const uint8_t *key_buffer, size_t key_buffer_size, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1017 | psa_algorithm_t alg ) |
| 1018 | { |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1019 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 1020 | psa_key_location_t location = |
| 1021 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1022 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1023 | switch( location ) |
| 1024 | { |
| 1025 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1026 | /* Key is stored in the slot in export representation, so |
| 1027 | * cycle through all known transparent accelerators */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1028 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1029 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1030 | status = mbedtls_test_transparent_cipher_decrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1031 | &operation->ctx.transparent_test_driver_ctx, |
| 1032 | attributes, |
| 1033 | key_buffer, |
| 1034 | key_buffer_size, |
| 1035 | alg ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1036 | /* Declared with fallback == true */ |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 1037 | if( status == PSA_SUCCESS ) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1038 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 1039 | |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1040 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1041 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1042 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1043 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1044 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1045 | /* Fell through, meaning no accelerator supports this operation */ |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 1046 | status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1047 | attributes, |
| 1048 | key_buffer, |
| 1049 | key_buffer_size, |
| 1050 | alg ); |
| 1051 | if( status == PSA_SUCCESS ) |
| 1052 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
| 1053 | |
| 1054 | return( status ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1055 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
| 1056 | return( PSA_ERROR_NOT_SUPPORTED ); |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1057 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1058 | /* Add cases for opaque driver here */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1059 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1060 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 1061 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1062 | status = mbedtls_test_opaque_cipher_decrypt_setup( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1063 | &operation->ctx.opaque_test_driver_ctx, |
| 1064 | attributes, |
| 1065 | key_buffer, key_buffer_size, |
| 1066 | alg ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1067 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1068 | if( status == PSA_SUCCESS ) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1069 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1070 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1071 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1072 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1073 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1074 | default: |
| 1075 | /* Key is declared with a lifetime not known to us */ |
Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 1076 | (void)status; |
Ronald Cron | 485559e | 2021-04-28 14:29:00 +0200 | [diff] [blame] | 1077 | (void)operation; |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1078 | (void)key_buffer; |
| 1079 | (void)key_buffer_size; |
| 1080 | (void)alg; |
Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 1081 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1082 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1083 | } |
| 1084 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1085 | psa_status_t psa_driver_wrapper_cipher_set_iv( |
Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 1086 | psa_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1087 | const uint8_t *iv, |
| 1088 | size_t iv_length ) |
| 1089 | { |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1090 | switch( operation->id ) |
| 1091 | { |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1092 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1093 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 1094 | return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1095 | iv, |
| 1096 | iv_length ) ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1097 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1098 | |
| 1099 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1100 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1101 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1102 | return( mbedtls_test_transparent_cipher_set_iv( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1103 | &operation->ctx.transparent_test_driver_ctx, |
| 1104 | iv, iv_length ) ); |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1105 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1106 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1107 | return( mbedtls_test_opaque_cipher_set_iv( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1108 | &operation->ctx.opaque_test_driver_ctx, |
| 1109 | iv, iv_length ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1110 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1111 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1112 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1113 | |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1114 | (void)iv; |
| 1115 | (void)iv_length; |
| 1116 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1117 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | psa_status_t psa_driver_wrapper_cipher_update( |
Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 1121 | psa_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1122 | const uint8_t *input, |
| 1123 | size_t input_length, |
| 1124 | uint8_t *output, |
| 1125 | size_t output_size, |
| 1126 | size_t *output_length ) |
| 1127 | { |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1128 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1129 | { |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1130 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1131 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 1132 | return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1133 | input, |
| 1134 | input_length, |
| 1135 | output, |
| 1136 | output_size, |
| 1137 | output_length ) ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1138 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
| 1139 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1140 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1141 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1142 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1143 | return( mbedtls_test_transparent_cipher_update( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1144 | &operation->ctx.transparent_test_driver_ctx, |
| 1145 | input, input_length, |
| 1146 | output, output_size, output_length ) ); |
| 1147 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1148 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1149 | return( mbedtls_test_opaque_cipher_update( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1150 | &operation->ctx.opaque_test_driver_ctx, |
| 1151 | input, input_length, |
| 1152 | output, output_size, output_length ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1153 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1154 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1155 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1156 | |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1157 | (void)input; |
| 1158 | (void)input_length; |
| 1159 | (void)output; |
| 1160 | (void)output_size; |
| 1161 | (void)output_length; |
| 1162 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1163 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | psa_status_t psa_driver_wrapper_cipher_finish( |
Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 1167 | psa_cipher_operation_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1168 | uint8_t *output, |
| 1169 | size_t output_size, |
| 1170 | size_t *output_length ) |
| 1171 | { |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1172 | switch( operation->id ) |
| 1173 | { |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1174 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1175 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 1176 | return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx, |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1177 | output, |
| 1178 | output_size, |
| 1179 | output_length ) ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1180 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1181 | |
| 1182 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1183 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1184 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1185 | return( mbedtls_test_transparent_cipher_finish( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1186 | &operation->ctx.transparent_test_driver_ctx, |
| 1187 | output, output_size, output_length ) ); |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1188 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1189 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1190 | return( mbedtls_test_opaque_cipher_finish( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1191 | &operation->ctx.opaque_test_driver_ctx, |
| 1192 | output, output_size, output_length ) ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1193 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1194 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1195 | } |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1196 | |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1197 | (void)output; |
| 1198 | (void)output_size; |
| 1199 | (void)output_length; |
| 1200 | |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1201 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | psa_status_t psa_driver_wrapper_cipher_abort( |
Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 1205 | psa_cipher_operation_t *operation ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1206 | { |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1207 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1208 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1209 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1210 | { |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1211 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1212 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 1213 | return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) ); |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1214 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1215 | |
| 1216 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1217 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1218 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1219 | status = mbedtls_test_transparent_cipher_abort( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1220 | &operation->ctx.transparent_test_driver_ctx ); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1221 | mbedtls_platform_zeroize( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1222 | &operation->ctx.transparent_test_driver_ctx, |
| 1223 | sizeof( operation->ctx.transparent_test_driver_ctx ) ); |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1224 | return( status ); |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1225 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1226 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1227 | status = mbedtls_test_opaque_cipher_abort( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1228 | &operation->ctx.opaque_test_driver_ctx ); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1229 | mbedtls_platform_zeroize( |
Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1230 | &operation->ctx.opaque_test_driver_ctx, |
| 1231 | sizeof( operation->ctx.opaque_test_driver_ctx ) ); |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1232 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1233 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1234 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
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 | |
Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1237 | (void)status; |
Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1238 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1239 | } |
| 1240 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1241 | /* |
| 1242 | * Hashing functions |
| 1243 | */ |
| 1244 | psa_status_t psa_driver_wrapper_hash_compute( |
| 1245 | psa_algorithm_t alg, |
| 1246 | const uint8_t *input, |
| 1247 | size_t input_length, |
| 1248 | uint8_t *hash, |
| 1249 | size_t hash_size, |
| 1250 | size_t *hash_length) |
| 1251 | { |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1252 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1253 | |
| 1254 | /* Try accelerators first */ |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1255 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1256 | status = mbedtls_test_transparent_hash_compute( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1257 | alg, input, input_length, hash, hash_size, hash_length ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1258 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1259 | return( status ); |
| 1260 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1261 | |
| 1262 | /* If software fallback is compiled in, try fallback */ |
| 1263 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1264 | status = mbedtls_psa_hash_compute( alg, input, input_length, |
| 1265 | hash, hash_size, hash_length ); |
| 1266 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1267 | return( status ); |
| 1268 | #endif |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1269 | (void) status; |
| 1270 | (void) alg; |
| 1271 | (void) input; |
| 1272 | (void) input_length; |
| 1273 | (void) hash; |
| 1274 | (void) hash_size; |
| 1275 | (void) hash_length; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1276 | |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1277 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | psa_status_t psa_driver_wrapper_hash_setup( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1281 | psa_hash_operation_t *operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1282 | psa_algorithm_t alg ) |
| 1283 | { |
Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1284 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1285 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1286 | /* Try setup on accelerators first */ |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1287 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1288 | status = mbedtls_test_transparent_hash_setup( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1289 | &operation->ctx.test_driver_ctx, alg ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1290 | if( status == PSA_SUCCESS ) |
| 1291 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
| 1292 | |
| 1293 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1294 | return( status ); |
| 1295 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1296 | |
| 1297 | /* If software fallback is compiled in, try fallback */ |
| 1298 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1299 | status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg ); |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1300 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1301 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1302 | |
| 1303 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1304 | return( status ); |
| 1305 | #endif |
| 1306 | /* Nothing left to try if we fall through here */ |
| 1307 | (void) status; |
| 1308 | (void) operation; |
| 1309 | (void) alg; |
| 1310 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1311 | } |
| 1312 | |
| 1313 | psa_status_t psa_driver_wrapper_hash_clone( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1314 | const psa_hash_operation_t *source_operation, |
| 1315 | psa_hash_operation_t *target_operation ) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1316 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1317 | switch( source_operation->id ) |
| 1318 | { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1319 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1320 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
| 1321 | target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
| 1322 | return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx, |
| 1323 | &target_operation->ctx.mbedtls_ctx ) ); |
| 1324 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1325 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1326 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
| 1327 | target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1328 | return( mbedtls_test_transparent_hash_clone( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1329 | &source_operation->ctx.test_driver_ctx, |
| 1330 | &target_operation->ctx.test_driver_ctx ) ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1331 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1332 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1333 | (void) target_operation; |
| 1334 | return( PSA_ERROR_BAD_STATE ); |
| 1335 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | psa_status_t psa_driver_wrapper_hash_update( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1339 | psa_hash_operation_t *operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1340 | const uint8_t *input, |
| 1341 | size_t input_length ) |
| 1342 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1343 | switch( operation->id ) |
| 1344 | { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1345 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1346 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
| 1347 | return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx, |
| 1348 | input, input_length ) ); |
| 1349 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1350 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1351 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1352 | return( mbedtls_test_transparent_hash_update( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1353 | &operation->ctx.test_driver_ctx, |
| 1354 | input, input_length ) ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1355 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1356 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1357 | (void) input; |
| 1358 | (void) input_length; |
| 1359 | return( PSA_ERROR_BAD_STATE ); |
| 1360 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | psa_status_t psa_driver_wrapper_hash_finish( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1364 | psa_hash_operation_t *operation, |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1365 | uint8_t *hash, |
| 1366 | size_t hash_size, |
| 1367 | size_t *hash_length ) |
| 1368 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1369 | switch( operation->id ) |
| 1370 | { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1371 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1372 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
| 1373 | return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx, |
| 1374 | hash, hash_size, hash_length ) ); |
| 1375 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1376 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1377 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1378 | return( mbedtls_test_transparent_hash_finish( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1379 | &operation->ctx.test_driver_ctx, |
| 1380 | hash, hash_size, hash_length ) ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1381 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1382 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1383 | (void) hash; |
| 1384 | (void) hash_size; |
| 1385 | (void) hash_length; |
| 1386 | return( PSA_ERROR_BAD_STATE ); |
| 1387 | } |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | psa_status_t psa_driver_wrapper_hash_abort( |
Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1391 | psa_hash_operation_t *operation ) |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1392 | { |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1393 | switch( operation->id ) |
| 1394 | { |
Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1395 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 1396 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
| 1397 | return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) ); |
| 1398 | #endif |
Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1399 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1400 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1401 | return( mbedtls_test_transparent_hash_abort( |
Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1402 | &operation->ctx.test_driver_ctx ) ); |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1403 | #endif |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1404 | default: |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1405 | return( PSA_ERROR_BAD_STATE ); |
| 1406 | } |
| 1407 | } |
| 1408 | |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1409 | psa_status_t psa_driver_wrapper_aead_encrypt( |
| 1410 | const psa_key_attributes_t *attributes, |
| 1411 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1412 | psa_algorithm_t alg, |
| 1413 | const uint8_t *nonce, size_t nonce_length, |
| 1414 | const uint8_t *additional_data, size_t additional_data_length, |
| 1415 | const uint8_t *plaintext, size_t plaintext_length, |
| 1416 | uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length ) |
| 1417 | { |
| 1418 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1419 | psa_key_location_t location = |
| 1420 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 1421 | |
| 1422 | switch( location ) |
| 1423 | { |
| 1424 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1425 | /* Key is stored in the slot in export representation, so |
| 1426 | * cycle through all known transparent accelerators */ |
| 1427 | |
| 1428 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1429 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1430 | status = mbedtls_test_transparent_aead_encrypt( |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1431 | attributes, key_buffer, key_buffer_size, |
| 1432 | alg, |
| 1433 | nonce, nonce_length, |
| 1434 | additional_data, additional_data_length, |
| 1435 | plaintext, plaintext_length, |
| 1436 | ciphertext, ciphertext_size, ciphertext_length ); |
| 1437 | /* Declared with fallback == true */ |
| 1438 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1439 | return( status ); |
| 1440 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1441 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1442 | |
| 1443 | /* Fell through, meaning no accelerator supports this operation */ |
| 1444 | return( mbedtls_psa_aead_encrypt( |
| 1445 | attributes, key_buffer, key_buffer_size, |
| 1446 | alg, |
| 1447 | nonce, nonce_length, |
| 1448 | additional_data, additional_data_length, |
| 1449 | plaintext, plaintext_length, |
| 1450 | ciphertext, ciphertext_size, ciphertext_length ) ); |
| 1451 | |
| 1452 | /* Add cases for opaque driver here */ |
| 1453 | |
| 1454 | default: |
| 1455 | /* Key is declared with a lifetime not known to us */ |
| 1456 | (void)status; |
| 1457 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | psa_status_t psa_driver_wrapper_aead_decrypt( |
| 1462 | const psa_key_attributes_t *attributes, |
| 1463 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 1464 | psa_algorithm_t alg, |
| 1465 | const uint8_t *nonce, size_t nonce_length, |
| 1466 | const uint8_t *additional_data, size_t additional_data_length, |
| 1467 | const uint8_t *ciphertext, size_t ciphertext_length, |
| 1468 | uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length ) |
| 1469 | { |
| 1470 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1471 | psa_key_location_t location = |
| 1472 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 1473 | |
| 1474 | switch( location ) |
| 1475 | { |
| 1476 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1477 | /* Key is stored in the slot in export representation, so |
| 1478 | * cycle through all known transparent accelerators */ |
| 1479 | |
| 1480 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1481 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 1482 | status = mbedtls_test_transparent_aead_decrypt( |
Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1483 | attributes, key_buffer, key_buffer_size, |
| 1484 | alg, |
| 1485 | nonce, nonce_length, |
| 1486 | additional_data, additional_data_length, |
| 1487 | ciphertext, ciphertext_length, |
| 1488 | plaintext, plaintext_size, plaintext_length ); |
| 1489 | /* Declared with fallback == true */ |
| 1490 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1491 | return( status ); |
| 1492 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1493 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1494 | |
| 1495 | /* Fell through, meaning no accelerator supports this operation */ |
| 1496 | return( mbedtls_psa_aead_decrypt( |
| 1497 | attributes, key_buffer, key_buffer_size, |
| 1498 | alg, |
| 1499 | nonce, nonce_length, |
| 1500 | additional_data, additional_data_length, |
| 1501 | ciphertext, ciphertext_length, |
| 1502 | plaintext, plaintext_size, plaintext_length ) ); |
| 1503 | |
| 1504 | /* Add cases for opaque driver here */ |
| 1505 | |
| 1506 | default: |
| 1507 | /* Key is declared with a lifetime not known to us */ |
| 1508 | (void)status; |
| 1509 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1510 | } |
| 1511 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1512 | |
| 1513 | |
| 1514 | /* |
| 1515 | * MAC functions |
| 1516 | */ |
| 1517 | psa_status_t psa_driver_wrapper_mac_compute( |
| 1518 | const psa_key_attributes_t *attributes, |
| 1519 | const uint8_t *key_buffer, |
| 1520 | size_t key_buffer_size, |
| 1521 | psa_algorithm_t alg, |
| 1522 | const uint8_t *input, |
| 1523 | size_t input_length, |
| 1524 | uint8_t *mac, |
| 1525 | size_t mac_size, |
| 1526 | size_t *mac_length ) |
| 1527 | { |
| 1528 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1529 | psa_key_location_t location = |
| 1530 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 1531 | |
| 1532 | switch( location ) |
| 1533 | { |
| 1534 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1535 | /* Key is stored in the slot in export representation, so |
| 1536 | * cycle through all known transparent accelerators */ |
| 1537 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1538 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1539 | status = mbedtls_test_transparent_mac_compute( |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1540 | attributes, key_buffer, key_buffer_size, alg, |
| 1541 | input, input_length, |
| 1542 | mac, mac_size, mac_length ); |
| 1543 | /* Declared with fallback == true */ |
| 1544 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1545 | return( status ); |
| 1546 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1547 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1548 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1549 | /* Fell through, meaning no accelerator supports this operation */ |
| 1550 | status = mbedtls_psa_mac_compute( |
| 1551 | attributes, key_buffer, key_buffer_size, alg, |
| 1552 | input, input_length, |
| 1553 | mac, mac_size, mac_length ); |
| 1554 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1555 | return( status ); |
| 1556 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1557 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1558 | |
| 1559 | /* Add cases for opaque driver here */ |
| 1560 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1561 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1562 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1563 | status = mbedtls_test_opaque_mac_compute( |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1564 | attributes, key_buffer, key_buffer_size, alg, |
| 1565 | input, input_length, |
| 1566 | mac, mac_size, mac_length ); |
| 1567 | return( status ); |
| 1568 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1569 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1570 | default: |
| 1571 | /* Key is declared with a lifetime not known to us */ |
| 1572 | (void) key_buffer; |
| 1573 | (void) key_buffer_size; |
| 1574 | (void) alg; |
| 1575 | (void) input; |
| 1576 | (void) input_length; |
| 1577 | (void) mac; |
| 1578 | (void) mac_size; |
| 1579 | (void) mac_length; |
| 1580 | (void) status; |
| 1581 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | psa_status_t psa_driver_wrapper_mac_sign_setup( |
| 1586 | psa_mac_operation_t *operation, |
| 1587 | const psa_key_attributes_t *attributes, |
| 1588 | const uint8_t *key_buffer, |
| 1589 | size_t key_buffer_size, |
| 1590 | psa_algorithm_t alg ) |
| 1591 | { |
| 1592 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1593 | psa_key_location_t location = |
| 1594 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 1595 | |
| 1596 | switch( location ) |
| 1597 | { |
| 1598 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1599 | /* Key is stored in the slot in export representation, so |
| 1600 | * cycle through all known transparent accelerators */ |
| 1601 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1602 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1603 | status = mbedtls_test_transparent_mac_sign_setup( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1604 | &operation->ctx.transparent_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1605 | attributes, |
| 1606 | key_buffer, key_buffer_size, |
| 1607 | alg ); |
| 1608 | /* Declared with fallback == true */ |
| 1609 | if( status == PSA_SUCCESS ) |
| 1610 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
| 1611 | |
| 1612 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1613 | return( status ); |
| 1614 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1615 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1616 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1617 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1618 | status = mbedtls_psa_mac_sign_setup( &operation->ctx.mbedtls_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1619 | attributes, |
| 1620 | key_buffer, key_buffer_size, |
| 1621 | alg ); |
| 1622 | if( status == PSA_SUCCESS ) |
| 1623 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
| 1624 | |
| 1625 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1626 | return( status ); |
| 1627 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1628 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1629 | |
| 1630 | /* Add cases for opaque driver here */ |
| 1631 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1632 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1633 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1634 | status = mbedtls_test_opaque_mac_sign_setup( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1635 | &operation->ctx.opaque_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1636 | attributes, |
| 1637 | key_buffer, key_buffer_size, |
| 1638 | alg ); |
| 1639 | |
| 1640 | if( status == PSA_SUCCESS ) |
| 1641 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
| 1642 | |
| 1643 | return( status ); |
| 1644 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1645 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1646 | default: |
| 1647 | /* Key is declared with a lifetime not known to us */ |
| 1648 | (void) status; |
Ronald Cron | 485559e | 2021-04-28 14:29:00 +0200 | [diff] [blame] | 1649 | (void) operation; |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1650 | (void) key_buffer; |
| 1651 | (void) key_buffer_size; |
| 1652 | (void) alg; |
| 1653 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1654 | } |
| 1655 | } |
| 1656 | |
| 1657 | psa_status_t psa_driver_wrapper_mac_verify_setup( |
| 1658 | psa_mac_operation_t *operation, |
| 1659 | const psa_key_attributes_t *attributes, |
| 1660 | const uint8_t *key_buffer, |
| 1661 | size_t key_buffer_size, |
| 1662 | psa_algorithm_t alg ) |
| 1663 | { |
| 1664 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1665 | psa_key_location_t location = |
| 1666 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 1667 | |
| 1668 | switch( location ) |
| 1669 | { |
| 1670 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 1671 | /* Key is stored in the slot in export representation, so |
| 1672 | * cycle through all known transparent accelerators */ |
| 1673 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1674 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1675 | status = mbedtls_test_transparent_mac_verify_setup( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1676 | &operation->ctx.transparent_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1677 | attributes, |
| 1678 | key_buffer, key_buffer_size, |
| 1679 | alg ); |
| 1680 | /* Declared with fallback == true */ |
| 1681 | if( status == PSA_SUCCESS ) |
| 1682 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
| 1683 | |
| 1684 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1685 | return( status ); |
| 1686 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1687 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1688 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1689 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1690 | status = mbedtls_psa_mac_verify_setup( &operation->ctx.mbedtls_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1691 | attributes, |
| 1692 | key_buffer, key_buffer_size, |
| 1693 | alg ); |
| 1694 | if( status == PSA_SUCCESS ) |
| 1695 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; |
| 1696 | |
| 1697 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 1698 | return( status ); |
| 1699 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1700 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 1701 | |
| 1702 | /* Add cases for opaque driver here */ |
| 1703 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1704 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1705 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1706 | status = mbedtls_test_opaque_mac_verify_setup( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1707 | &operation->ctx.opaque_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1708 | attributes, |
| 1709 | key_buffer, key_buffer_size, |
| 1710 | alg ); |
| 1711 | |
| 1712 | if( status == PSA_SUCCESS ) |
| 1713 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
| 1714 | |
| 1715 | return( status ); |
| 1716 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1717 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1718 | default: |
| 1719 | /* Key is declared with a lifetime not known to us */ |
| 1720 | (void) status; |
Ronald Cron | 485559e | 2021-04-28 14:29:00 +0200 | [diff] [blame] | 1721 | (void) operation; |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1722 | (void) key_buffer; |
| 1723 | (void) key_buffer_size; |
| 1724 | (void) alg; |
| 1725 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1726 | } |
| 1727 | } |
| 1728 | |
| 1729 | psa_status_t psa_driver_wrapper_mac_update( |
| 1730 | psa_mac_operation_t *operation, |
| 1731 | const uint8_t *input, |
| 1732 | size_t input_length ) |
| 1733 | { |
| 1734 | switch( operation->id ) |
| 1735 | { |
| 1736 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1737 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1738 | return( mbedtls_psa_mac_update( &operation->ctx.mbedtls_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1739 | input, input_length ) ); |
| 1740 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1741 | |
| 1742 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1743 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1744 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1745 | return( mbedtls_test_transparent_mac_update( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1746 | &operation->ctx.transparent_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1747 | input, input_length ) ); |
| 1748 | |
| 1749 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1750 | return( mbedtls_test_opaque_mac_update( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1751 | &operation->ctx.opaque_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1752 | input, input_length ) ); |
| 1753 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1754 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1755 | default: |
| 1756 | (void) input; |
| 1757 | (void) input_length; |
| 1758 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1759 | } |
| 1760 | } |
| 1761 | |
| 1762 | psa_status_t psa_driver_wrapper_mac_sign_finish( |
| 1763 | psa_mac_operation_t *operation, |
| 1764 | uint8_t *mac, |
| 1765 | size_t mac_size, |
| 1766 | size_t *mac_length ) |
| 1767 | { |
| 1768 | switch( operation->id ) |
| 1769 | { |
| 1770 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1771 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1772 | return( mbedtls_psa_mac_sign_finish( &operation->ctx.mbedtls_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1773 | mac, mac_size, mac_length ) ); |
| 1774 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1775 | |
| 1776 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1777 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1778 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1779 | return( mbedtls_test_transparent_mac_sign_finish( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1780 | &operation->ctx.transparent_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1781 | mac, mac_size, mac_length ) ); |
| 1782 | |
| 1783 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1784 | return( mbedtls_test_opaque_mac_sign_finish( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1785 | &operation->ctx.opaque_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1786 | mac, mac_size, mac_length ) ); |
| 1787 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1788 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1789 | default: |
| 1790 | (void) mac; |
| 1791 | (void) mac_size; |
| 1792 | (void) mac_length; |
| 1793 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | psa_status_t psa_driver_wrapper_mac_verify_finish( |
| 1798 | psa_mac_operation_t *operation, |
| 1799 | const uint8_t *mac, |
| 1800 | size_t mac_length ) |
| 1801 | { |
| 1802 | switch( operation->id ) |
| 1803 | { |
| 1804 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1805 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1806 | return( mbedtls_psa_mac_verify_finish( &operation->ctx.mbedtls_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1807 | mac, mac_length ) ); |
| 1808 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1809 | |
| 1810 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1811 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1812 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1813 | return( mbedtls_test_transparent_mac_verify_finish( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1814 | &operation->ctx.transparent_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1815 | mac, mac_length ) ); |
| 1816 | |
| 1817 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1818 | return( mbedtls_test_opaque_mac_verify_finish( |
Steven Cooreman | 6e3c2cb | 2021-03-19 17:05:52 +0100 | [diff] [blame] | 1819 | &operation->ctx.opaque_test_driver_ctx, |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1820 | mac, mac_length ) ); |
| 1821 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1822 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1823 | default: |
| 1824 | (void) mac; |
| 1825 | (void) mac_length; |
| 1826 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 1827 | } |
| 1828 | } |
| 1829 | |
| 1830 | psa_status_t psa_driver_wrapper_mac_abort( |
| 1831 | psa_mac_operation_t *operation ) |
| 1832 | { |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1833 | switch( operation->id ) |
| 1834 | { |
| 1835 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 1836 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 1837 | return( mbedtls_psa_mac_abort( &operation->ctx.mbedtls_ctx ) ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1838 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 1839 | |
| 1840 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1841 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1842 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1843 | return( mbedtls_test_transparent_mac_abort( |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 1844 | &operation->ctx.transparent_test_driver_ctx ) ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1845 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | 2d9a3f9 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1846 | return( mbedtls_test_opaque_mac_abort( |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 1847 | &operation->ctx.opaque_test_driver_ctx ) ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1848 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1849 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 1850 | default: |
Steven Cooreman | 0789783 | 2021-03-19 18:28:56 +0100 | [diff] [blame] | 1851 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1852 | } |
Steven Cooreman | 896d51e | 2021-03-19 15:24:23 +0100 | [diff] [blame] | 1853 | } |
Gilles Peskine | ad0e012 | 2021-04-24 13:19:45 +0200 | [diff] [blame] | 1854 | |
| 1855 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |