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