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