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