Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Functions to delegate cryptographic operations to an available |
| 3 | * and appropriate accelerator. |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 4 | * Warning: This file will be auto-generated in the future. |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 5 | */ |
Steven Cooreman | 2c7b2f8 | 2020-09-02 13:43:46 +0200 | [diff] [blame] | 6 | /* Copyright The Mbed TLS Contributors |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: Apache-2.0 |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | * not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "psa_crypto_core.h" |
| 23 | #include "psa_crypto_driver_wrappers.h" |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 24 | #include "mbedtls/platform.h" |
| 25 | |
| 26 | #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 27 | |
| 28 | /* Include test driver definition when running tests */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 29 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 30 | #ifndef PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 31 | #define PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 32 | #endif |
| 33 | #ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 34 | #define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 35 | #endif |
Steven Cooreman | 0d7c64d | 2020-09-07 16:17:55 +0200 | [diff] [blame] | 36 | #include "test/drivers/test_driver.h" |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 37 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 38 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 39 | /* Repeat above block for each JSON-declared driver during autogeneration */ |
| 40 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 41 | /* Auto-generated values depending on which drivers are registered. ID 0 is |
| 42 | * reserved for unallocated operations. */ |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 43 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 44 | #define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (1) |
| 45 | #define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (2) |
| 46 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 47 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */ |
| 48 | |
| 49 | /* Support the 'old' SE interface when asked to */ |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 50 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 51 | /* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style |
| 52 | * SE driver is present, to avoid unused argument errors at compile time. */ |
| 53 | #ifndef PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 54 | #define PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 55 | #endif |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 56 | #include "psa_crypto_se.h" |
| 57 | #endif |
| 58 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 59 | /* Start delegation functions */ |
| 60 | psa_status_t psa_driver_wrapper_sign_hash( psa_key_slot_t *slot, |
| 61 | psa_algorithm_t alg, |
| 62 | const uint8_t *hash, |
| 63 | size_t hash_length, |
| 64 | uint8_t *signature, |
| 65 | size_t signature_size, |
| 66 | size_t *signature_length ) |
| 67 | { |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 68 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 69 | /* Try dynamically-registered SE interface first */ |
| 70 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 71 | const psa_drv_se_t *drv; |
| 72 | psa_drv_se_context_t *drv_context; |
| 73 | |
| 74 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 75 | { |
| 76 | if( drv->asymmetric == NULL || |
| 77 | drv->asymmetric->p_sign == NULL ) |
| 78 | { |
| 79 | /* Key is defined in SE, but we have no way to exercise it */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 80 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 81 | } |
| 82 | return( drv->asymmetric->p_sign( drv_context, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 83 | psa_key_slot_get_slot_number( slot ), |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 84 | alg, |
| 85 | hash, hash_length, |
| 86 | signature, signature_size, |
| 87 | signature_length ) ); |
| 88 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 89 | #endif /* PSA_CRYPTO_SE_C */ |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 90 | |
| 91 | /* Then try accelerator API */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 92 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 93 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 94 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 95 | psa_key_attributes_t attributes = { |
| 96 | .core = slot->attr |
| 97 | }; |
| 98 | |
| 99 | switch( location ) |
| 100 | { |
| 101 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 102 | /* Key is stored in the slot in export representation, so |
| 103 | * cycle through all known transparent accelerators */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 104 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 105 | status = test_transparent_signature_sign_hash( &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 106 | slot->key.data, |
| 107 | slot->key.bytes, |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 108 | alg, |
| 109 | hash, |
| 110 | hash_length, |
| 111 | signature, |
| 112 | signature_size, |
| 113 | signature_length ); |
| 114 | /* Declared with fallback == true */ |
| 115 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 116 | return( status ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 117 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 118 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 119 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 120 | /* Add cases for opaque driver here */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 121 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 122 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 123 | return( test_opaque_signature_sign_hash( &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 124 | slot->key.data, |
| 125 | slot->key.bytes, |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 126 | alg, |
| 127 | hash, |
| 128 | hash_length, |
| 129 | signature, |
| 130 | signature_size, |
| 131 | signature_length ) ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 132 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 133 | default: |
| 134 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 135 | return( status ); |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 136 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 137 | #else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 138 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 139 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 140 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 141 | (void)slot; |
| 142 | (void)alg; |
| 143 | (void)hash; |
| 144 | (void)hash_length; |
| 145 | (void)signature; |
| 146 | (void)signature_size; |
| 147 | (void)signature_length; |
| 148 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 149 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 150 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 153 | psa_status_t psa_driver_wrapper_verify_hash( psa_key_slot_t *slot, |
| 154 | psa_algorithm_t alg, |
| 155 | const uint8_t *hash, |
| 156 | size_t hash_length, |
| 157 | const uint8_t *signature, |
| 158 | size_t signature_length ) |
| 159 | { |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 160 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 161 | /* Try dynamically-registered SE interface first */ |
| 162 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 163 | const psa_drv_se_t *drv; |
| 164 | psa_drv_se_context_t *drv_context; |
| 165 | |
| 166 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 167 | { |
| 168 | if( drv->asymmetric == NULL || |
| 169 | drv->asymmetric->p_verify == NULL ) |
| 170 | { |
| 171 | /* Key is defined in SE, but we have no way to exercise it */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 172 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 173 | } |
| 174 | return( drv->asymmetric->p_verify( drv_context, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 175 | psa_key_slot_get_slot_number( slot ), |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 176 | alg, |
| 177 | hash, hash_length, |
| 178 | signature, signature_length ) ); |
| 179 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 180 | #endif /* PSA_CRYPTO_SE_C */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 181 | |
| 182 | /* Then try accelerator API */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 183 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 184 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 185 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 186 | psa_key_attributes_t attributes = { |
| 187 | .core = slot->attr |
| 188 | }; |
| 189 | |
| 190 | switch( location ) |
| 191 | { |
| 192 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 193 | /* Key is stored in the slot in export representation, so |
| 194 | * cycle through all known transparent accelerators */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 195 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 196 | status = test_transparent_signature_verify_hash( &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 197 | slot->key.data, |
| 198 | slot->key.bytes, |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 199 | alg, |
| 200 | hash, |
| 201 | hash_length, |
| 202 | signature, |
| 203 | signature_length ); |
| 204 | /* Declared with fallback == true */ |
| 205 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 206 | return( status ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 207 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 208 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 209 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 210 | /* Add cases for opaque driver here */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 211 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 212 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 213 | return( test_opaque_signature_verify_hash( &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 214 | slot->key.data, |
| 215 | slot->key.bytes, |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 216 | alg, |
| 217 | hash, |
| 218 | hash_length, |
| 219 | signature, |
| 220 | signature_length ) ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 221 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 222 | default: |
| 223 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 224 | return( status ); |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 225 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 226 | #else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 227 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 228 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 229 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 230 | (void)slot; |
| 231 | (void)alg; |
| 232 | (void)hash; |
| 233 | (void)hash_length; |
| 234 | (void)signature; |
| 235 | (void)signature_length; |
| 236 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 237 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 238 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 241 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 242 | /** Calculate the size to allocate for buffering a key with given attributes. |
| 243 | * |
| 244 | * This function provides a way to get the expected size for storing a key with |
| 245 | * the given attributes. This will be the size of the export representation for |
| 246 | * cleartext keys, and a driver-defined size for keys stored by opaque drivers. |
| 247 | * |
| 248 | * \param[in] attributes The key attribute structure of the key to store. |
| 249 | * \param[out] expected_size On success, a byte size large enough to contain |
| 250 | * the declared key. |
| 251 | * |
| 252 | * \retval #PSA_SUCCESS |
| 253 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 254 | */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 255 | static psa_status_t get_expected_key_size( const psa_key_attributes_t *attributes, |
| 256 | size_t *expected_size ) |
| 257 | { |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 258 | size_t buffer_size = 0; |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 259 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); |
| 260 | psa_key_type_t key_type = attributes->core.type; |
| 261 | size_t key_bits = attributes->core.bits; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 262 | |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 263 | switch( location ) |
| 264 | { |
| 265 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 266 | buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ); |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 267 | |
| 268 | if( buffer_size == 0 ) |
| 269 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 270 | |
| 271 | *expected_size = buffer_size; |
| 272 | return( PSA_SUCCESS ); |
| 273 | |
| 274 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 275 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
John Durkop | ac93e3b | 2020-10-16 06:48:55 -0700 | [diff] [blame] | 276 | #ifdef TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 277 | *expected_size = test_size_function( key_type, key_bits ); |
| 278 | return( PSA_SUCCESS ); |
| 279 | #else /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */ |
| 280 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) ) |
| 281 | { |
John Durkop | 135ce69 | 2020-10-19 07:12:28 -0700 | [diff] [blame] | 282 | int public_key_overhead = ( ( TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 ) ? |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 283 | PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) : 0 ); |
John Durkop | 135ce69 | 2020-10-19 07:12:28 -0700 | [diff] [blame] | 284 | *expected_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE |
| 285 | + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE |
| 286 | + public_key_overhead; |
| 287 | } |
| 288 | else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( attributes->core.type ) ) |
| 289 | { |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 290 | *expected_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE |
| 291 | + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE; |
| 292 | } |
John Durkop | 135ce69 | 2020-10-19 07:12:28 -0700 | [diff] [blame] | 293 | else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) && |
| 294 | !PSA_KEY_TYPE_IS_PUBLIC_KEY ( attributes->core.type ) ) |
John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 295 | { |
| 296 | *expected_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE |
| 297 | + TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR |
| 298 | * ( ( key_bits + 7 ) / 8 ); |
| 299 | } |
| 300 | else |
| 301 | { |
| 302 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 303 | } |
| 304 | return( PSA_SUCCESS ); |
| 305 | #endif /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */ |
| 306 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 307 | |
| 308 | default: |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 309 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 310 | } |
| 311 | } |
John Durkop | 135ce69 | 2020-10-19 07:12:28 -0700 | [diff] [blame] | 312 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 313 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 314 | psa_status_t psa_driver_wrapper_generate_key( const psa_key_attributes_t *attributes, |
| 315 | psa_key_slot_t *slot ) |
| 316 | { |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 317 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 318 | /* Try dynamically-registered SE interface first */ |
| 319 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 320 | const psa_drv_se_t *drv; |
| 321 | psa_drv_se_context_t *drv_context; |
| 322 | |
| 323 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 324 | { |
| 325 | size_t pubkey_length = 0; /* We don't support this feature yet */ |
| 326 | if( drv->key_management == NULL || |
| 327 | drv->key_management->p_generate == NULL ) |
| 328 | { |
| 329 | /* Key is defined as being in SE, but we have no way to generate it */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 330 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 331 | } |
| 332 | return( drv->key_management->p_generate( |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 333 | drv_context, psa_key_slot_get_slot_number( slot ), |
| 334 | attributes, NULL, 0, &pubkey_length ) ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 335 | } |
| 336 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 337 | |
| 338 | /* Then try accelerator API */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 339 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 340 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 341 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 342 | size_t export_size = 0; |
| 343 | |
| 344 | status = get_expected_key_size( attributes, &export_size ); |
| 345 | if( status != PSA_SUCCESS ) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 346 | return( status ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 347 | |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 348 | slot->key.data = mbedtls_calloc(1, export_size); |
| 349 | if( slot->key.data == NULL ) |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 350 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 351 | slot->key.bytes = export_size; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 352 | |
| 353 | switch( location ) |
| 354 | { |
| 355 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 356 | /* Key is stored in the slot in export representation, so |
| 357 | * cycle through all known transparent accelerators */ |
| 358 | |
| 359 | /* Transparent drivers are limited to generating asymmetric keys */ |
| 360 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) ) |
| 361 | { |
| 362 | status = PSA_ERROR_NOT_SUPPORTED; |
| 363 | break; |
| 364 | } |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 365 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 366 | status = test_transparent_generate_key( attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 367 | slot->key.data, |
| 368 | slot->key.bytes, |
| 369 | &slot->key.bytes ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 370 | /* Declared with fallback == true */ |
| 371 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 372 | break; |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 373 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 374 | /* Fell through, meaning no accelerator supports this operation */ |
| 375 | status = PSA_ERROR_NOT_SUPPORTED; |
| 376 | break; |
| 377 | /* Add cases for opaque driver here */ |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 378 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 379 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 380 | status = test_opaque_generate_key( attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 381 | slot->key.data, |
| 382 | slot->key.bytes, |
| 383 | &slot->key.bytes ); |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 384 | break; |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 385 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 386 | default: |
| 387 | /* Key is declared with a lifetime not known to us */ |
| 388 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 389 | break; |
| 390 | } |
| 391 | |
| 392 | if( status != PSA_SUCCESS ) |
| 393 | { |
| 394 | /* free allocated buffer */ |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 395 | mbedtls_free( slot->key.data ); |
| 396 | slot->key.data = NULL; |
| 397 | slot->key.bytes = 0; |
Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | return( status ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 401 | #else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 402 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 403 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 404 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 405 | (void) attributes; |
| 406 | (void) slot; |
| 407 | |
Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 408 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 409 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 410 | } |
| 411 | |
Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame] | 412 | psa_status_t psa_driver_wrapper_import_key( |
| 413 | const psa_key_attributes_t *attributes, |
| 414 | const uint8_t *data, |
| 415 | size_t data_length, |
| 416 | uint8_t *key_buffer, |
| 417 | size_t key_buffer_size, |
| 418 | size_t *key_buffer_length, |
| 419 | size_t *bits ) |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 420 | { |
| 421 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 422 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 423 | /* Try accelerators in turn */ |
| 424 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame] | 425 | status = test_transparent_import_key( attributes, |
| 426 | data, data_length, |
| 427 | key_buffer, key_buffer_size, |
| 428 | key_buffer_length, bits ); |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 429 | /* Declared with fallback == true */ |
| 430 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 431 | return( status ); |
| 432 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 433 | |
| 434 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 435 | #else /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 436 | (void) attributes; |
| 437 | (void) data; |
| 438 | (void) data_length; |
Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame] | 439 | (void) key_buffer; |
| 440 | (void) key_buffer_size; |
| 441 | (void) key_buffer_length; |
Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 442 | (void) bits; |
| 443 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 444 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 445 | } |
| 446 | |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 447 | psa_status_t psa_driver_wrapper_export_key( |
| 448 | const psa_key_attributes_t *attributes, |
| 449 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 450 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 451 | |
| 452 | { |
| 453 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 454 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( |
| 455 | psa_get_key_lifetime( attributes ) ); |
| 456 | |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame^] | 457 | /* Try dynamically-registered SE interface first */ |
| 458 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 459 | const psa_drv_se_t *drv; |
| 460 | psa_drv_se_context_t *drv_context; |
| 461 | |
| 462 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 463 | { |
| 464 | if( ( drv->key_management == NULL ) || |
| 465 | ( drv->key_management->p_export == NULL ) ) |
| 466 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 467 | |
| 468 | return( drv->key_management->p_export( |
| 469 | drv_context, |
| 470 | *( (psa_key_slot_number_t *)key_buffer ), |
| 471 | data, data_size, data_length ) ); |
| 472 | } |
| 473 | #endif /* PSA_CRYPTO_SE_C */ |
| 474 | |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 475 | switch( location ) |
| 476 | { |
| 477 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 478 | return( psa_export_key_internal( attributes, |
| 479 | key_buffer, |
| 480 | key_buffer_size, |
| 481 | data, |
| 482 | data_size, |
| 483 | data_length ) ); |
| 484 | |
| 485 | /* Add cases for opaque driver here */ |
| 486 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 487 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 488 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
| 489 | return( test_opaque_export_key( attributes, |
| 490 | key_buffer, |
| 491 | key_buffer_size, |
| 492 | data, |
| 493 | data_size, |
| 494 | data_length ) ); |
| 495 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 496 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 497 | default: |
| 498 | /* Key is declared with a lifetime not known to us */ |
| 499 | return( status ); |
| 500 | } |
| 501 | } |
| 502 | |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 503 | psa_status_t psa_driver_wrapper_export_public_key( |
| 504 | const psa_key_attributes_t *attributes, |
| 505 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 506 | uint8_t *data, size_t data_size, size_t *data_length ) |
| 507 | |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 508 | { |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 509 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 510 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( |
| 511 | psa_get_key_lifetime( attributes ) ); |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 512 | |
Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame^] | 513 | /* Try dynamically-registered SE interface first */ |
| 514 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 515 | const psa_drv_se_t *drv; |
| 516 | psa_drv_se_context_t *drv_context; |
| 517 | |
| 518 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) |
| 519 | { |
| 520 | if( ( drv->key_management == NULL ) || |
| 521 | ( drv->key_management->p_export_public == NULL ) ) |
| 522 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 523 | |
| 524 | return( drv->key_management->p_export_public( |
| 525 | drv_context, |
| 526 | *( (psa_key_slot_number_t *)key_buffer ), |
| 527 | data, data_size, data_length ) ); |
| 528 | } |
| 529 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 530 | |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 531 | switch( location ) |
| 532 | { |
| 533 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 534 | /* Key is stored in the slot in export representation, so |
| 535 | * cycle through all known transparent accelerators */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 536 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 537 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 538 | status = test_transparent_export_public_key( attributes, |
| 539 | key_buffer, |
| 540 | key_buffer_size, |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 541 | data, |
| 542 | data_size, |
| 543 | data_length ); |
| 544 | /* Declared with fallback == true */ |
| 545 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 546 | return( status ); |
| 547 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 548 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 549 | /* Fell through, meaning no accelerator supports this operation */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 550 | return( psa_export_public_key_internal( attributes, |
| 551 | key_buffer, |
| 552 | key_buffer_size, |
| 553 | data, |
| 554 | data_size, |
| 555 | data_length ) ); |
| 556 | |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 557 | /* Add cases for opaque driver here */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 558 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 559 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 560 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 561 | return( test_opaque_export_public_key( attributes, |
| 562 | key_buffer, |
| 563 | key_buffer_size, |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 564 | data, |
| 565 | data_size, |
| 566 | data_length ) ); |
| 567 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 568 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 569 | default: |
| 570 | /* Key is declared with a lifetime not known to us */ |
| 571 | return( status ); |
| 572 | } |
Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 573 | } |
| 574 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 575 | /* |
| 576 | * Cipher functions |
| 577 | */ |
| 578 | psa_status_t psa_driver_wrapper_cipher_encrypt( |
| 579 | psa_key_slot_t *slot, |
| 580 | psa_algorithm_t alg, |
| 581 | const uint8_t *input, |
| 582 | size_t input_length, |
| 583 | uint8_t *output, |
| 584 | size_t output_size, |
| 585 | size_t *output_length ) |
| 586 | { |
| 587 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 588 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 589 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 590 | psa_key_attributes_t attributes = { |
| 591 | .core = slot->attr |
| 592 | }; |
| 593 | |
| 594 | switch( location ) |
| 595 | { |
| 596 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 597 | /* Key is stored in the slot in export representation, so |
| 598 | * cycle through all known transparent accelerators */ |
| 599 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 600 | status = test_transparent_cipher_encrypt( &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 601 | slot->key.data, |
| 602 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 603 | alg, |
| 604 | input, |
| 605 | input_length, |
| 606 | output, |
| 607 | output_size, |
| 608 | output_length ); |
| 609 | /* Declared with fallback == true */ |
| 610 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 611 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 612 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 613 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 614 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 615 | /* Add cases for opaque driver here */ |
| 616 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 617 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
| 618 | return( test_opaque_cipher_encrypt( &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 619 | slot->key.data, |
| 620 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 621 | alg, |
| 622 | input, |
| 623 | input_length, |
| 624 | output, |
| 625 | output_size, |
| 626 | output_length ) ); |
| 627 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 628 | default: |
| 629 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 630 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 631 | } |
| 632 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 633 | (void) slot; |
| 634 | (void) alg; |
| 635 | (void) input; |
| 636 | (void) input_length; |
| 637 | (void) output; |
| 638 | (void) output_size; |
| 639 | (void) output_length; |
| 640 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 641 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 642 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 643 | } |
| 644 | |
| 645 | psa_status_t psa_driver_wrapper_cipher_decrypt( |
| 646 | psa_key_slot_t *slot, |
| 647 | psa_algorithm_t alg, |
| 648 | const uint8_t *input, |
| 649 | size_t input_length, |
| 650 | uint8_t *output, |
| 651 | size_t output_size, |
| 652 | size_t *output_length ) |
| 653 | { |
| 654 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 655 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 656 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 657 | psa_key_attributes_t attributes = { |
| 658 | .core = slot->attr |
| 659 | }; |
| 660 | |
| 661 | switch( location ) |
| 662 | { |
| 663 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 664 | /* Key is stored in the slot in export representation, so |
| 665 | * cycle through all known transparent accelerators */ |
| 666 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 667 | status = test_transparent_cipher_decrypt( &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 668 | slot->key.data, |
| 669 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 670 | alg, |
| 671 | input, |
| 672 | input_length, |
| 673 | output, |
| 674 | output_size, |
| 675 | output_length ); |
| 676 | /* Declared with fallback == true */ |
| 677 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 678 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 679 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 680 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 681 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 682 | /* Add cases for opaque driver here */ |
| 683 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 684 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
| 685 | return( test_opaque_cipher_decrypt( &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 686 | slot->key.data, |
| 687 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 688 | alg, |
| 689 | input, |
| 690 | input_length, |
| 691 | output, |
| 692 | output_size, |
| 693 | output_length ) ); |
| 694 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 695 | default: |
| 696 | /* Key is declared with a lifetime not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 697 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 698 | } |
| 699 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 700 | (void) slot; |
| 701 | (void) alg; |
| 702 | (void) input; |
| 703 | (void) input_length; |
| 704 | (void) output; |
| 705 | (void) output_size; |
| 706 | (void) output_length; |
| 707 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 708 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 709 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 710 | } |
| 711 | |
| 712 | psa_status_t psa_driver_wrapper_cipher_encrypt_setup( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 713 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 714 | psa_key_slot_t *slot, |
| 715 | psa_algorithm_t alg ) |
| 716 | { |
| 717 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 718 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 719 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 720 | psa_key_attributes_t attributes = { |
| 721 | .core = slot->attr |
| 722 | }; |
| 723 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 724 | switch( location ) |
| 725 | { |
| 726 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 727 | /* Key is stored in the slot in export representation, so |
| 728 | * cycle through all known transparent accelerators */ |
| 729 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 730 | operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) ); |
| 731 | if( operation->ctx == NULL ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 732 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 733 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 734 | status = test_transparent_cipher_encrypt_setup( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 735 | &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 736 | slot->key.data, |
| 737 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 738 | alg ); |
| 739 | /* Declared with fallback == true */ |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 740 | if( status == PSA_SUCCESS ) |
| 741 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 742 | else |
| 743 | { |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 744 | mbedtls_platform_zeroize( |
| 745 | operation->ctx, |
| 746 | sizeof( test_transparent_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 747 | mbedtls_free( operation->ctx ); |
| 748 | operation->ctx = NULL; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 749 | } |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 750 | |
| 751 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 752 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 753 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 754 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 755 | /* Add cases for opaque driver here */ |
| 756 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 757 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 758 | operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) ); |
| 759 | if( operation->ctx == NULL ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 760 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 761 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 762 | status = test_opaque_cipher_encrypt_setup( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 763 | &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 764 | slot->key.data, |
| 765 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 766 | alg ); |
| 767 | if( status == PSA_SUCCESS ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 768 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 769 | else |
| 770 | { |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 771 | mbedtls_platform_zeroize( |
| 772 | operation->ctx, |
| 773 | sizeof( test_opaque_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 774 | mbedtls_free( operation->ctx ); |
| 775 | operation->ctx = NULL; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 776 | } |
| 777 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 778 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 779 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 780 | default: |
| 781 | /* Key is declared with a lifetime not known to us */ |
John Durkop | 714e3a1 | 2020-09-29 22:07:04 -0700 | [diff] [blame] | 782 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 783 | } |
| 784 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 785 | (void)slot; |
| 786 | (void)alg; |
| 787 | (void)operation; |
| 788 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 789 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 790 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 791 | } |
| 792 | |
| 793 | psa_status_t psa_driver_wrapper_cipher_decrypt_setup( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 794 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 795 | psa_key_slot_t *slot, |
| 796 | psa_algorithm_t alg ) |
| 797 | { |
| 798 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 799 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 800 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 801 | psa_key_attributes_t attributes = { |
| 802 | .core = slot->attr |
| 803 | }; |
| 804 | |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 805 | switch( location ) |
| 806 | { |
| 807 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 808 | /* Key is stored in the slot in export representation, so |
| 809 | * cycle through all known transparent accelerators */ |
| 810 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 811 | operation->ctx = mbedtls_calloc( 1, sizeof(test_transparent_cipher_operation_t) ); |
| 812 | if( operation->ctx == NULL ) |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 813 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 814 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 815 | status = test_transparent_cipher_decrypt_setup( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 816 | &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 817 | slot->key.data, |
| 818 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 819 | alg ); |
| 820 | /* Declared with fallback == true */ |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 821 | if( status == PSA_SUCCESS ) |
| 822 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 823 | else |
| 824 | { |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 825 | mbedtls_platform_zeroize( |
| 826 | operation->ctx, |
| 827 | sizeof( test_transparent_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 828 | mbedtls_free( operation->ctx ); |
| 829 | operation->ctx = NULL; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 830 | } |
Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 831 | |
| 832 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 833 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 834 | /* Fell through, meaning no accelerator supports this operation */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 835 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 836 | /* Add cases for opaque driver here */ |
| 837 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 838 | case PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 839 | operation->ctx = mbedtls_calloc( 1, sizeof(test_opaque_cipher_operation_t) ); |
| 840 | if( operation->ctx == NULL ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 841 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 842 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 843 | status = test_opaque_cipher_decrypt_setup( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 844 | &attributes, |
Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 845 | slot->key.data, |
| 846 | slot->key.bytes, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 847 | alg ); |
| 848 | if( status == PSA_SUCCESS ) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 849 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 850 | else |
| 851 | { |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 852 | mbedtls_platform_zeroize( |
| 853 | operation->ctx, |
| 854 | sizeof( test_opaque_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 855 | mbedtls_free( operation->ctx ); |
| 856 | operation->ctx = NULL; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 857 | } |
| 858 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 859 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 860 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 861 | default: |
| 862 | /* Key is declared with a lifetime not known to us */ |
John Durkop | 814dca7 | 2020-10-05 06:31:12 -0700 | [diff] [blame] | 863 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 864 | } |
| 865 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 866 | (void)slot; |
| 867 | (void)alg; |
| 868 | (void)operation; |
| 869 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 870 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 871 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 872 | } |
| 873 | |
| 874 | psa_status_t psa_driver_wrapper_cipher_generate_iv( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 875 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 876 | uint8_t *iv, |
| 877 | size_t iv_size, |
| 878 | size_t *iv_length ) |
| 879 | { |
| 880 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 881 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 882 | { |
| 883 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 884 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 885 | return( test_transparent_cipher_generate_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 886 | iv, |
| 887 | iv_size, |
| 888 | iv_length ) ); |
| 889 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 890 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 891 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 892 | return( test_opaque_cipher_generate_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 893 | iv, |
| 894 | iv_size, |
| 895 | iv_length ) ); |
| 896 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 897 | default: |
| 898 | /* Key is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 899 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 900 | } |
| 901 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 902 | (void) operation; |
| 903 | (void) iv; |
| 904 | (void) iv_size; |
| 905 | (void) iv_length; |
| 906 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 907 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 908 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 909 | } |
| 910 | |
| 911 | psa_status_t psa_driver_wrapper_cipher_set_iv( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 912 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 913 | const uint8_t *iv, |
| 914 | size_t iv_length ) |
| 915 | { |
| 916 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 917 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 918 | { |
| 919 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 920 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 921 | return( test_transparent_cipher_set_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 922 | iv, |
| 923 | iv_length ) ); |
| 924 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 925 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 926 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 927 | return( test_opaque_cipher_set_iv( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 928 | iv, |
| 929 | iv_length ) ); |
| 930 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 931 | default: |
| 932 | /* Key is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 933 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 934 | } |
| 935 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 936 | (void) operation; |
| 937 | (void) iv; |
| 938 | (void) iv_length; |
| 939 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 940 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 941 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 942 | } |
| 943 | |
| 944 | psa_status_t psa_driver_wrapper_cipher_update( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 945 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 946 | const uint8_t *input, |
| 947 | size_t input_length, |
| 948 | uint8_t *output, |
| 949 | size_t output_size, |
| 950 | size_t *output_length ) |
| 951 | { |
| 952 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 953 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 954 | { |
| 955 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 956 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 957 | return( test_transparent_cipher_update( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 958 | input, |
| 959 | input_length, |
| 960 | output, |
| 961 | output_size, |
| 962 | output_length ) ); |
| 963 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 964 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 965 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 966 | return( test_opaque_cipher_update( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 967 | input, |
| 968 | input_length, |
| 969 | output, |
| 970 | output_size, |
| 971 | output_length ) ); |
| 972 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 973 | default: |
| 974 | /* Key is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 975 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 976 | } |
| 977 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 978 | (void) operation; |
| 979 | (void) input; |
| 980 | (void) input_length; |
| 981 | (void) output; |
| 982 | (void) output_length; |
| 983 | (void) output_size; |
| 984 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 985 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 986 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 987 | } |
| 988 | |
| 989 | psa_status_t psa_driver_wrapper_cipher_finish( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 990 | psa_operation_driver_context_t *operation, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 991 | uint8_t *output, |
| 992 | size_t output_size, |
| 993 | size_t *output_length ) |
| 994 | { |
| 995 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 996 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 997 | { |
| 998 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 999 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1000 | return( test_transparent_cipher_finish( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1001 | output, |
| 1002 | output_size, |
| 1003 | output_length ) ); |
| 1004 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1005 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1006 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1007 | return( test_opaque_cipher_finish( operation->ctx, |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1008 | output, |
| 1009 | output_size, |
| 1010 | output_length ) ); |
| 1011 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1012 | default: |
| 1013 | /* Key is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1014 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1015 | } |
| 1016 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1017 | (void) operation; |
| 1018 | (void) output; |
| 1019 | (void) output_size; |
| 1020 | (void) output_length; |
| 1021 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1022 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1023 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1024 | } |
| 1025 | |
| 1026 | psa_status_t psa_driver_wrapper_cipher_abort( |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1027 | psa_operation_driver_context_t *operation ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1028 | { |
| 1029 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 1030 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1031 | |
| 1032 | /* The object has (apparently) been initialized but it is not in use. It's |
| 1033 | * ok to call abort on such an object, and there's nothing to do. */ |
| 1034 | if( operation->ctx == NULL && operation->id == 0 ) |
| 1035 | return( PSA_SUCCESS ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1036 | |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1037 | switch( operation->id ) |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1038 | { |
| 1039 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1040 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1041 | status = test_transparent_cipher_abort( operation->ctx ); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1042 | mbedtls_platform_zeroize( |
| 1043 | operation->ctx, |
| 1044 | sizeof( test_transparent_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1045 | mbedtls_free( operation->ctx ); |
| 1046 | operation->ctx = NULL; |
| 1047 | operation->id = 0; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1048 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1049 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1050 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1051 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 1052 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1053 | status = test_opaque_cipher_abort( operation->ctx ); |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1054 | mbedtls_platform_zeroize( |
| 1055 | operation->ctx, |
| 1056 | sizeof( test_opaque_cipher_operation_t ) ); |
Steven Cooreman | fb81aa5 | 2020-09-09 12:01:43 +0200 | [diff] [blame] | 1057 | mbedtls_free( operation->ctx ); |
| 1058 | operation->ctx = NULL; |
Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1059 | operation->id = 0; |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1060 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1061 | return( status ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1062 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 1063 | default: |
| 1064 | /* Operation is attached to a driver not known to us */ |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1065 | return( PSA_ERROR_BAD_STATE ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1066 | } |
| 1067 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1068 | (void)operation; |
| 1069 | |
Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1070 | return( PSA_ERROR_NOT_SUPPORTED ); |
Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1071 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ |
| 1072 | } |
| 1073 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 1074 | /* End of automatically generated file. */ |