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