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