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