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