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