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