| 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 |  | 
| Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 22 | #include "psa_crypto_aead.h" | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 23 | #include "psa_crypto_cipher.h" | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 24 | #include "psa_crypto_core.h" | 
|  | 25 | #include "psa_crypto_driver_wrappers.h" | 
| Steven Cooreman | 830aff2 | 2021-03-09 09:50:44 +0100 | [diff] [blame] | 26 | #include "psa_crypto_hash.h" | 
|  | 27 |  | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 28 | #include "mbedtls/platform.h" | 
|  | 29 |  | 
|  | 30 | #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 31 |  | 
|  | 32 | /* Include test driver definition when running tests */ | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 33 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 34 | #ifndef PSA_CRYPTO_DRIVER_PRESENT | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 35 | #define PSA_CRYPTO_DRIVER_PRESENT | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 36 | #endif | 
|  | 37 | #ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 38 | #define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 39 | #endif | 
| Steven Cooreman | 0d7c64d | 2020-09-07 16:17:55 +0200 | [diff] [blame] | 40 | #include "test/drivers/test_driver.h" | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 41 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 42 |  | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 43 | /* Repeat above block for each JSON-declared driver during autogeneration */ | 
| Steven Cooreman | aa87fd0 | 2021-03-15 18:54:03 +0100 | [diff] [blame] | 44 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS */ | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 45 |  | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 46 | /* Auto-generated values depending on which drivers are registered. | 
|  | 47 | * ID 0 is reserved for unallocated operations. | 
|  | 48 | * ID 1 is reserved for the Mbed TLS software driver. */ | 
| Steven Cooreman | 830aff2 | 2021-03-09 09:50:44 +0100 | [diff] [blame] | 49 | #define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1) | 
|  | 50 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 51 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 52 | #define PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID (2) | 
|  | 53 | #define PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID (3) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 54 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 55 |  | 
|  | 56 | /* Support the 'old' SE interface when asked to */ | 
| Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 57 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 58 | /* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style | 
|  | 59 | * SE driver is present, to avoid unused argument errors at compile time. */ | 
|  | 60 | #ifndef PSA_CRYPTO_DRIVER_PRESENT | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 61 | #define PSA_CRYPTO_DRIVER_PRESENT | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 62 | #endif | 
| Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 63 | #include "psa_crypto_se.h" | 
|  | 64 | #endif | 
|  | 65 |  | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 66 | /* Start delegation functions */ | 
| Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 67 | psa_status_t psa_driver_wrapper_sign_hash( | 
|  | 68 | const psa_key_attributes_t *attributes, | 
|  | 69 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 70 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, | 
|  | 71 | uint8_t *signature, size_t signature_size, size_t *signature_length ) | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 72 | { | 
| Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 73 | /* Try dynamically-registered SE interface first */ | 
|  | 74 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 75 | const psa_drv_se_t *drv; | 
|  | 76 | psa_drv_se_context_t *drv_context; | 
|  | 77 |  | 
| Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 78 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) | 
| Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 79 | { | 
|  | 80 | if( drv->asymmetric == NULL || | 
|  | 81 | drv->asymmetric->p_sign == NULL ) | 
|  | 82 | { | 
|  | 83 | /* 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] | 84 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 85 | } | 
| Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 86 | return( drv->asymmetric->p_sign( | 
|  | 87 | drv_context, *( (psa_key_slot_number_t *)key_buffer ), | 
|  | 88 | alg, hash, hash_length, | 
|  | 89 | signature, signature_size, signature_length ) ); | 
| Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 90 | } | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 91 | #endif /* PSA_CRYPTO_SE_C */ | 
| Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 92 |  | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 93 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 94 | psa_key_location_t location = | 
|  | 95 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 96 |  | 
|  | 97 | switch( location ) | 
|  | 98 | { | 
|  | 99 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
|  | 100 | /* Key is stored in the slot in export representation, so | 
|  | 101 | * cycle through all known transparent accelerators */ | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 102 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 103 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 104 | status = test_transparent_signature_sign_hash( attributes, | 
|  | 105 | key_buffer, | 
|  | 106 | key_buffer_size, | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 107 | alg, | 
|  | 108 | hash, | 
|  | 109 | hash_length, | 
|  | 110 | signature, | 
|  | 111 | signature_size, | 
|  | 112 | signature_length ); | 
|  | 113 | /* Declared with fallback == true */ | 
|  | 114 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 115 | return( status ); | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 116 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 117 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 118 | /* Fell through, meaning no accelerator supports this operation */ | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 119 | return( psa_sign_hash_internal( attributes, | 
|  | 120 | key_buffer, | 
|  | 121 | key_buffer_size, | 
|  | 122 | alg, | 
|  | 123 | hash, | 
|  | 124 | hash_length, | 
|  | 125 | signature, | 
|  | 126 | signature_size, | 
|  | 127 | signature_length ) ); | 
|  | 128 |  | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 129 | /* Add cases for opaque driver here */ | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 130 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 131 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 132 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 133 | return( test_opaque_signature_sign_hash( attributes, | 
|  | 134 | key_buffer, | 
|  | 135 | key_buffer_size, | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 136 | alg, | 
|  | 137 | hash, | 
|  | 138 | hash_length, | 
|  | 139 | signature, | 
|  | 140 | signature_size, | 
|  | 141 | signature_length ) ); | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 142 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 143 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 144 | default: | 
|  | 145 | /* Key is declared with a lifetime not known to us */ | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 146 | (void)status; | 
|  | 147 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 148 | } | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 149 | } | 
|  | 150 |  | 
| Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 151 | psa_status_t psa_driver_wrapper_verify_hash( | 
|  | 152 | const psa_key_attributes_t *attributes, | 
|  | 153 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 154 | psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, | 
|  | 155 | const uint8_t *signature, size_t signature_length ) | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 156 | { | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 157 | /* Try dynamically-registered SE interface first */ | 
|  | 158 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 159 | const psa_drv_se_t *drv; | 
|  | 160 | psa_drv_se_context_t *drv_context; | 
|  | 161 |  | 
| Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 162 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 163 | { | 
|  | 164 | if( drv->asymmetric == NULL || | 
|  | 165 | drv->asymmetric->p_verify == NULL ) | 
|  | 166 | { | 
|  | 167 | /* 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] | 168 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 169 | } | 
| Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 170 | return( drv->asymmetric->p_verify( | 
|  | 171 | drv_context, *( (psa_key_slot_number_t *)key_buffer ), | 
|  | 172 | alg, hash, hash_length, | 
|  | 173 | signature, signature_length ) ); | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 174 | } | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 175 | #endif /* PSA_CRYPTO_SE_C */ | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 176 |  | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 177 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 178 | psa_key_location_t location = | 
|  | 179 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 180 |  | 
|  | 181 | switch( location ) | 
|  | 182 | { | 
|  | 183 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
|  | 184 | /* Key is stored in the slot in export representation, so | 
|  | 185 | * cycle through all known transparent accelerators */ | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 186 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 187 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 188 | status = test_transparent_signature_verify_hash( attributes, | 
|  | 189 | key_buffer, | 
|  | 190 | key_buffer_size, | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 191 | alg, | 
|  | 192 | hash, | 
|  | 193 | hash_length, | 
|  | 194 | signature, | 
|  | 195 | signature_length ); | 
|  | 196 | /* Declared with fallback == true */ | 
|  | 197 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 198 | return( status ); | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 199 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 200 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
|  | 201 |  | 
|  | 202 | return( psa_verify_hash_internal( attributes, | 
|  | 203 | key_buffer, | 
|  | 204 | key_buffer_size, | 
|  | 205 | alg, | 
|  | 206 | hash, | 
|  | 207 | hash_length, | 
|  | 208 | signature, | 
|  | 209 | signature_length ) ); | 
|  | 210 |  | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 211 | /* Add cases for opaque driver here */ | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 212 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 213 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 214 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Ronald Cron | 9f17aa4 | 2020-12-08 17:07:25 +0100 | [diff] [blame] | 215 | return( test_opaque_signature_verify_hash( attributes, | 
|  | 216 | key_buffer, | 
|  | 217 | key_buffer_size, | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 218 | alg, | 
|  | 219 | hash, | 
|  | 220 | hash_length, | 
|  | 221 | signature, | 
|  | 222 | signature_length ) ); | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 223 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 224 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 225 | default: | 
|  | 226 | /* Key is declared with a lifetime not known to us */ | 
| Ronald Cron | fce9df2 | 2020-12-08 18:06:03 +0100 | [diff] [blame] | 227 | (void)status; | 
|  | 228 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 229 | } | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 230 | } | 
|  | 231 |  | 
| Steven Cooreman | b938b0b | 2021-04-06 13:08:42 +0200 | [diff] [blame] | 232 | /** Get the key buffer size required to store the key material of a key | 
|  | 233 | *  associated with an opaque driver without storage. | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 234 | * | 
| Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 235 | * \param[in] attributes  The key attributes. | 
|  | 236 | * \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] | 237 | * | 
|  | 238 | * \retval #PSA_SUCCESS | 
| Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 239 | *         The minimum size for a buffer to contain the key material has been | 
|  | 240 | *         returned successfully. | 
|  | 241 | * \retval #PSA_ERROR_INVALID_ARGUMENT | 
|  | 242 | *         The size in bits of the key is not valid. | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 243 | * \retval #PSA_ERROR_NOT_SUPPORTED | 
| Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 244 | *         The type and/or the size in bits of the key or the combination of | 
|  | 245 | *         the two is not supported. | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 246 | */ | 
| Ronald Cron | 9df74be | 2020-12-05 19:15:23 +0100 | [diff] [blame] | 247 | psa_status_t psa_driver_wrapper_get_key_buffer_size( | 
| Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 248 | const psa_key_attributes_t *attributes, | 
|  | 249 | size_t *key_buffer_size ) | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 250 | { | 
| John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 251 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); | 
|  | 252 | psa_key_type_t key_type = attributes->core.type; | 
|  | 253 | size_t key_bits = attributes->core.bits; | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 254 |  | 
| Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 255 | *key_buffer_size = 0; | 
| John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 256 | switch( location ) | 
|  | 257 | { | 
| John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 258 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 259 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 260 | #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS) | 
|  | 261 | /* Emulate property 'builtin_key_size' */ | 
|  | 262 | if( psa_key_id_is_builtin( | 
|  | 263 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( | 
|  | 264 | psa_get_key_id( attributes ) ) ) ) | 
|  | 265 | { | 
| Steven Cooreman | 203bcbb | 2021-03-18 17:17:40 +0100 | [diff] [blame] | 266 | *key_buffer_size = sizeof( psa_drv_slot_number_t ); | 
| Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 267 | return( PSA_SUCCESS ); | 
|  | 268 | } | 
|  | 269 | #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */ | 
| John Durkop | ac93e3b | 2020-10-16 06:48:55 -0700 | [diff] [blame] | 270 | #ifdef TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION | 
| Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 271 | *key_buffer_size = test_size_function( key_type, key_bits ); | 
| John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 272 | return( PSA_SUCCESS ); | 
|  | 273 | #else /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */ | 
|  | 274 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) ) | 
|  | 275 | { | 
| Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 276 | int public_key_overhead = | 
|  | 277 | ( ( TEST_DRIVER_KEY_CONTEXT_STORE_PUBLIC_KEY == 1 ) ? | 
|  | 278 | PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) : 0 ); | 
|  | 279 | *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE | 
| John Durkop | 135ce69 | 2020-10-19 07:12:28 -0700 | [diff] [blame] | 280 | + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE | 
|  | 281 | + public_key_overhead; | 
|  | 282 | } | 
| Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 283 | else if( PSA_KEY_TYPE_IS_PUBLIC_KEY( key_type ) ) | 
| John Durkop | 135ce69 | 2020-10-19 07:12:28 -0700 | [diff] [blame] | 284 | { | 
| Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 285 | *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE | 
| John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 286 | + TEST_DRIVER_KEY_CONTEXT_PUBLIC_KEY_SIZE; | 
|  | 287 | } | 
| John Durkop | 135ce69 | 2020-10-19 07:12:28 -0700 | [diff] [blame] | 288 | else if ( !PSA_KEY_TYPE_IS_KEY_PAIR( key_type ) && | 
| Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 289 | !PSA_KEY_TYPE_IS_PUBLIC_KEY ( key_type ) ) | 
| John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 290 | { | 
| Ronald Cron | 3121628 | 2020-12-05 18:47:56 +0100 | [diff] [blame] | 291 | *key_buffer_size = TEST_DRIVER_KEY_CONTEXT_BASE_SIZE | 
| John Durkop | 2c61835 | 2020-09-22 06:54:01 -0700 | [diff] [blame] | 292 | + TEST_DRIVER_KEY_CONTEXT_SYMMETRIC_FACTOR | 
|  | 293 | * ( ( key_bits + 7 ) / 8 ); | 
|  | 294 | } | 
|  | 295 | else | 
|  | 296 | { | 
|  | 297 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 298 | } | 
|  | 299 | return( PSA_SUCCESS ); | 
|  | 300 | #endif /* TEST_DRIVER_KEY_CONTEXT_SIZE_FUNCTION */ | 
|  | 301 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 302 |  | 
|  | 303 | default: | 
| Ronald Cron | 9df74be | 2020-12-05 19:15:23 +0100 | [diff] [blame] | 304 | (void)key_type; | 
|  | 305 | (void)key_bits; | 
| Steven Cooreman | 56250fd | 2020-09-04 13:07:15 +0200 | [diff] [blame] | 306 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 307 | } | 
|  | 308 | } | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 309 |  | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 310 | psa_status_t psa_driver_wrapper_generate_key( | 
|  | 311 | const psa_key_attributes_t *attributes, | 
|  | 312 | 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] | 313 | { | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 314 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 315 | psa_key_location_t location = | 
|  | 316 | PSA_KEY_LIFETIME_GET_LOCATION(attributes->core.lifetime); | 
|  | 317 |  | 
| 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 |  | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 323 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 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 | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 333 | drv_context, | 
|  | 334 | *( (psa_key_slot_number_t *)key_buffer ), | 
| Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 335 | attributes, NULL, 0, &pubkey_length ) ); | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 336 | } | 
|  | 337 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 338 |  | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 339 | switch( location ) | 
|  | 340 | { | 
|  | 341 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 342 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 343 | /* Transparent drivers are limited to generating asymmetric keys */ | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 344 | if( PSA_KEY_TYPE_IS_ASYMMETRIC( attributes->core.type ) ) | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 345 | { | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 346 | /* Cycle through all known transparent accelerators */ | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 347 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 348 | status = test_transparent_generate_key( | 
|  | 349 | attributes, key_buffer, key_buffer_size, | 
|  | 350 | key_buffer_length ); | 
|  | 351 | /* Declared with fallback == true */ | 
|  | 352 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 353 | break; | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 354 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 355 | } | 
|  | 356 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
|  | 357 |  | 
|  | 358 | /* Software fallback */ | 
|  | 359 | status = psa_generate_key_internal( | 
|  | 360 | attributes, key_buffer, key_buffer_size, key_buffer_length ); | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 361 | break; | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 362 |  | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 363 | /* Add cases for opaque driver here */ | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 364 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 365 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 366 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 367 | status = test_opaque_generate_key( | 
|  | 368 | attributes, key_buffer, key_buffer_size, key_buffer_length ); | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 369 | break; | 
| Steven Cooreman | f1720ea | 2020-07-24 18:41:58 +0200 | [diff] [blame] | 370 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | 977c247 | 2020-10-13 08:32:21 +0200 | [diff] [blame] | 371 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
|  | 372 |  | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 373 | default: | 
|  | 374 | /* Key is declared with a lifetime not known to us */ | 
|  | 375 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 376 | break; | 
|  | 377 | } | 
|  | 378 |  | 
| Steven Cooreman | 2a1664c | 2020-07-20 15:33:08 +0200 | [diff] [blame] | 379 | return( status ); | 
| Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame] | 380 | } | 
|  | 381 |  | 
| Ronald Cron | 8328287 | 2020-11-22 14:02:39 +0100 | [diff] [blame] | 382 | psa_status_t psa_driver_wrapper_import_key( | 
|  | 383 | const psa_key_attributes_t *attributes, | 
|  | 384 | const uint8_t *data, | 
|  | 385 | size_t data_length, | 
|  | 386 | uint8_t *key_buffer, | 
|  | 387 | size_t key_buffer_size, | 
|  | 388 | size_t *key_buffer_length, | 
|  | 389 | size_t *bits ) | 
| Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 390 | { | 
| Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 391 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 392 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( | 
|  | 393 | psa_get_key_lifetime( attributes ) ); | 
| Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 394 |  | 
| Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 395 | /* Try dynamically-registered SE interface first */ | 
|  | 396 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 397 | const psa_drv_se_t *drv; | 
|  | 398 | psa_drv_se_context_t *drv_context; | 
|  | 399 |  | 
|  | 400 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) | 
|  | 401 | { | 
|  | 402 | if( drv->key_management == NULL || | 
|  | 403 | drv->key_management->p_import == NULL ) | 
|  | 404 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 405 |  | 
|  | 406 | /* The driver should set the number of key bits, however in | 
|  | 407 | * case it doesn't, we initialize bits to an invalid value. */ | 
|  | 408 | *bits = PSA_MAX_KEY_BITS + 1; | 
|  | 409 | status = drv->key_management->p_import( | 
|  | 410 | drv_context, | 
|  | 411 | *( (psa_key_slot_number_t *)key_buffer ), | 
|  | 412 | attributes, data, data_length, bits ); | 
|  | 413 |  | 
|  | 414 | if( status != PSA_SUCCESS ) | 
|  | 415 | return( status ); | 
|  | 416 |  | 
|  | 417 | if( (*bits) > PSA_MAX_KEY_BITS ) | 
|  | 418 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 419 |  | 
|  | 420 | return( PSA_SUCCESS ); | 
|  | 421 | } | 
|  | 422 | #endif /* PSA_CRYPTO_SE_C */ | 
|  | 423 |  | 
| Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 424 | switch( location ) | 
|  | 425 | { | 
|  | 426 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
|  | 427 | /* Key is stored in the slot in export representation, so | 
|  | 428 | * cycle through all known transparent accelerators */ | 
|  | 429 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
|  | 430 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 431 | status = test_transparent_import_key( attributes, | 
|  | 432 | data, data_length, | 
|  | 433 | key_buffer, key_buffer_size, | 
|  | 434 | key_buffer_length, bits ); | 
|  | 435 | /* Declared with fallback == true */ | 
|  | 436 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 437 | return( status ); | 
|  | 438 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 439 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
|  | 440 | /* Fell through, meaning no accelerator supports this operation */ | 
|  | 441 | return( psa_import_key_into_slot( attributes, | 
|  | 442 | data, data_length, | 
|  | 443 | key_buffer, key_buffer_size, | 
|  | 444 | key_buffer_length, bits ) ); | 
|  | 445 |  | 
|  | 446 | default: | 
| Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 447 | /* Importing a key with external storage in not yet supported. | 
|  | 448 | * Return in error indicating that the lifetime is not valid. */ | 
| Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 449 | (void)status; | 
| Ronald Cron | fb2ed5b | 2020-11-30 12:11:01 +0100 | [diff] [blame] | 450 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Ronald Cron | bf33c93 | 2020-11-28 18:06:53 +0100 | [diff] [blame] | 451 | } | 
|  | 452 |  | 
| Steven Cooreman | 0452476 | 2020-10-13 17:43:44 +0200 | [diff] [blame] | 453 | } | 
|  | 454 |  | 
| Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 455 | psa_status_t psa_driver_wrapper_export_key( | 
|  | 456 | const psa_key_attributes_t *attributes, | 
|  | 457 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 458 | uint8_t *data, size_t data_size, size_t *data_length ) | 
|  | 459 |  | 
|  | 460 | { | 
|  | 461 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 462 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( | 
|  | 463 | psa_get_key_lifetime( attributes ) ); | 
|  | 464 |  | 
| Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 465 | /* Try dynamically-registered SE interface first */ | 
|  | 466 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 467 | const psa_drv_se_t *drv; | 
|  | 468 | psa_drv_se_context_t *drv_context; | 
|  | 469 |  | 
|  | 470 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) | 
|  | 471 | { | 
|  | 472 | if( ( drv->key_management == NULL   ) || | 
|  | 473 | ( drv->key_management->p_export == NULL ) ) | 
| Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 474 | { | 
| Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 475 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 476 | } | 
| Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 477 |  | 
|  | 478 | return( drv->key_management->p_export( | 
|  | 479 | drv_context, | 
|  | 480 | *( (psa_key_slot_number_t *)key_buffer ), | 
|  | 481 | data, data_size, data_length ) ); | 
|  | 482 | } | 
|  | 483 | #endif /* PSA_CRYPTO_SE_C */ | 
|  | 484 |  | 
| Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 485 | switch( location ) | 
|  | 486 | { | 
|  | 487 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
|  | 488 | return( psa_export_key_internal( attributes, | 
|  | 489 | key_buffer, | 
|  | 490 | key_buffer_size, | 
|  | 491 | data, | 
|  | 492 | data_size, | 
|  | 493 | data_length ) ); | 
|  | 494 |  | 
|  | 495 | /* Add cases for opaque driver here */ | 
|  | 496 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
|  | 497 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 498 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 499 | return( test_opaque_export_key( attributes, | 
|  | 500 | key_buffer, | 
|  | 501 | key_buffer_size, | 
|  | 502 | data, | 
|  | 503 | data_size, | 
|  | 504 | data_length ) ); | 
|  | 505 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 506 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
|  | 507 | default: | 
|  | 508 | /* Key is declared with a lifetime not known to us */ | 
|  | 509 | return( status ); | 
|  | 510 | } | 
|  | 511 | } | 
|  | 512 |  | 
| Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 513 | psa_status_t psa_driver_wrapper_export_public_key( | 
|  | 514 | const psa_key_attributes_t *attributes, | 
|  | 515 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 516 | uint8_t *data, size_t data_size, size_t *data_length ) | 
|  | 517 |  | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 518 | { | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 519 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; | 
| Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 520 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( | 
|  | 521 | psa_get_key_lifetime( attributes ) ); | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 522 |  | 
| Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 523 | /* Try dynamically-registered SE interface first */ | 
|  | 524 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) | 
|  | 525 | const psa_drv_se_t *drv; | 
|  | 526 | psa_drv_se_context_t *drv_context; | 
|  | 527 |  | 
|  | 528 | if( psa_get_se_driver( attributes->core.lifetime, &drv, &drv_context ) ) | 
|  | 529 | { | 
|  | 530 | if( ( drv->key_management == NULL ) || | 
|  | 531 | ( drv->key_management->p_export_public == NULL ) ) | 
| Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 532 | { | 
| Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 533 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Ronald Cron | 9cfdf6e | 2021-01-18 11:58:39 +0100 | [diff] [blame] | 534 | } | 
| Ronald Cron | 152e3f8 | 2020-11-26 16:06:41 +0100 | [diff] [blame] | 535 |  | 
|  | 536 | return( drv->key_management->p_export_public( | 
|  | 537 | drv_context, | 
|  | 538 | *( (psa_key_slot_number_t *)key_buffer ), | 
|  | 539 | data, data_size, data_length ) ); | 
|  | 540 | } | 
|  | 541 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ | 
|  | 542 |  | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 543 | switch( location ) | 
|  | 544 | { | 
|  | 545 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
|  | 546 | /* Key is stored in the slot in export representation, so | 
|  | 547 | * cycle through all known transparent accelerators */ | 
| Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 548 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 549 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 550 | status = test_transparent_export_public_key( attributes, | 
|  | 551 | key_buffer, | 
|  | 552 | key_buffer_size, | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 553 | data, | 
|  | 554 | data_size, | 
|  | 555 | data_length ); | 
|  | 556 | /* Declared with fallback == true */ | 
|  | 557 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 558 | return( status ); | 
|  | 559 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 560 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 561 | /* Fell through, meaning no accelerator supports this operation */ | 
| Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 562 | return( psa_export_public_key_internal( attributes, | 
|  | 563 | key_buffer, | 
|  | 564 | key_buffer_size, | 
|  | 565 | data, | 
|  | 566 | data_size, | 
|  | 567 | data_length ) ); | 
|  | 568 |  | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 569 | /* Add cases for opaque driver here */ | 
| Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 570 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 571 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 572 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Ronald Cron | 84cc994 | 2020-11-25 14:30:05 +0100 | [diff] [blame] | 573 | return( test_opaque_export_public_key( attributes, | 
|  | 574 | key_buffer, | 
|  | 575 | key_buffer_size, | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 576 | data, | 
|  | 577 | data_size, | 
|  | 578 | data_length ) ); | 
|  | 579 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | 6722798 | 2020-11-26 15:16:05 +0100 | [diff] [blame] | 580 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 581 | default: | 
|  | 582 | /* Key is declared with a lifetime not known to us */ | 
|  | 583 | return( status ); | 
|  | 584 | } | 
| Steven Cooreman | b9b8442 | 2020-10-14 14:39:20 +0200 | [diff] [blame] | 585 | } | 
|  | 586 |  | 
| Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 587 | psa_status_t psa_driver_wrapper_get_builtin_key( | 
|  | 588 | psa_drv_slot_number_t slot_number, | 
|  | 589 | psa_key_attributes_t *attributes, | 
|  | 590 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length ) | 
|  | 591 | { | 
|  | 592 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); | 
|  | 593 | switch( location ) | 
|  | 594 | { | 
| Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 595 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 596 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 597 | return( test_opaque_get_builtin_key( | 
|  | 598 | slot_number, | 
|  | 599 | attributes, | 
|  | 600 | key_buffer, key_buffer_size, key_buffer_length ) ); | 
| Steven Cooreman | f9a55ff | 2021-02-19 18:04:59 +0100 | [diff] [blame] | 601 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Steven Cooreman | 6801f08 | 2021-02-19 17:21:22 +0100 | [diff] [blame] | 602 | default: | 
|  | 603 | (void) slot_number; | 
|  | 604 | (void) key_buffer; | 
|  | 605 | (void) key_buffer_size; | 
|  | 606 | (void) key_buffer_length; | 
|  | 607 | return( PSA_ERROR_DOES_NOT_EXIST ); | 
|  | 608 | } | 
|  | 609 | } | 
|  | 610 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 611 | /* | 
|  | 612 | * Cipher functions | 
|  | 613 | */ | 
|  | 614 | psa_status_t psa_driver_wrapper_cipher_encrypt( | 
|  | 615 | psa_key_slot_t *slot, | 
|  | 616 | psa_algorithm_t alg, | 
|  | 617 | const uint8_t *input, | 
|  | 618 | size_t input_length, | 
|  | 619 | uint8_t *output, | 
|  | 620 | size_t output_size, | 
|  | 621 | size_t *output_length ) | 
|  | 622 | { | 
|  | 623 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
|  | 624 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 625 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); | 
|  | 626 | psa_key_attributes_t attributes = { | 
|  | 627 | .core = slot->attr | 
|  | 628 | }; | 
|  | 629 |  | 
|  | 630 | switch( location ) | 
|  | 631 | { | 
|  | 632 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
|  | 633 | /* Key is stored in the slot in export representation, so | 
|  | 634 | * cycle through all known transparent accelerators */ | 
|  | 635 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 636 | status = test_transparent_cipher_encrypt( &attributes, | 
| Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 637 | slot->key.data, | 
|  | 638 | slot->key.bytes, | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 639 | alg, | 
|  | 640 | input, | 
|  | 641 | input_length, | 
|  | 642 | output, | 
|  | 643 | output_size, | 
|  | 644 | output_length ); | 
|  | 645 | /* Declared with fallback == true */ | 
|  | 646 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 647 | return( status ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 648 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 649 | /* Fell through, meaning no accelerator supports this operation */ | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 650 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 651 | /* Add cases for opaque driver here */ | 
|  | 652 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 653 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 654 | return( test_opaque_cipher_encrypt( &attributes, | 
| Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 655 | slot->key.data, | 
|  | 656 | slot->key.bytes, | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 657 | alg, | 
|  | 658 | input, | 
|  | 659 | input_length, | 
|  | 660 | output, | 
|  | 661 | output_size, | 
|  | 662 | output_length ) ); | 
|  | 663 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 664 | default: | 
|  | 665 | /* Key is declared with a lifetime not known to us */ | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 666 | return( status ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 667 | } | 
|  | 668 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ | 
|  | 669 | (void) slot; | 
|  | 670 | (void) alg; | 
|  | 671 | (void) input; | 
|  | 672 | (void) input_length; | 
|  | 673 | (void) output; | 
|  | 674 | (void) output_size; | 
|  | 675 | (void) output_length; | 
|  | 676 |  | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 677 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 678 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ | 
|  | 679 | } | 
|  | 680 |  | 
|  | 681 | psa_status_t psa_driver_wrapper_cipher_decrypt( | 
|  | 682 | psa_key_slot_t *slot, | 
|  | 683 | psa_algorithm_t alg, | 
|  | 684 | const uint8_t *input, | 
|  | 685 | size_t input_length, | 
|  | 686 | uint8_t *output, | 
|  | 687 | size_t output_size, | 
|  | 688 | size_t *output_length ) | 
|  | 689 | { | 
|  | 690 | #if defined(PSA_CRYPTO_DRIVER_PRESENT) && defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
|  | 691 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 692 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); | 
|  | 693 | psa_key_attributes_t attributes = { | 
|  | 694 | .core = slot->attr | 
|  | 695 | }; | 
|  | 696 |  | 
|  | 697 | switch( location ) | 
|  | 698 | { | 
|  | 699 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
|  | 700 | /* Key is stored in the slot in export representation, so | 
|  | 701 | * cycle through all known transparent accelerators */ | 
|  | 702 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 703 | status = test_transparent_cipher_decrypt( &attributes, | 
| Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 704 | slot->key.data, | 
|  | 705 | slot->key.bytes, | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 706 | alg, | 
|  | 707 | input, | 
|  | 708 | input_length, | 
|  | 709 | output, | 
|  | 710 | output_size, | 
|  | 711 | output_length ); | 
|  | 712 | /* Declared with fallback == true */ | 
|  | 713 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 714 | return( status ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 715 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 716 | /* Fell through, meaning no accelerator supports this operation */ | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 717 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 718 | /* Add cases for opaque driver here */ | 
|  | 719 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 720 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 721 | return( test_opaque_cipher_decrypt( &attributes, | 
| Ronald Cron | ea0f8a6 | 2020-11-25 17:52:23 +0100 | [diff] [blame] | 722 | slot->key.data, | 
|  | 723 | slot->key.bytes, | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 724 | alg, | 
|  | 725 | input, | 
|  | 726 | input_length, | 
|  | 727 | output, | 
|  | 728 | output_size, | 
|  | 729 | output_length ) ); | 
|  | 730 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 731 | default: | 
|  | 732 | /* Key is declared with a lifetime not known to us */ | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 733 | return( status ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 734 | } | 
|  | 735 | #else /* PSA_CRYPTO_DRIVER_PRESENT */ | 
|  | 736 | (void) slot; | 
|  | 737 | (void) alg; | 
|  | 738 | (void) input; | 
|  | 739 | (void) input_length; | 
|  | 740 | (void) output; | 
|  | 741 | (void) output_size; | 
|  | 742 | (void) output_length; | 
|  | 743 |  | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 744 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 745 | #endif /* PSA_CRYPTO_DRIVER_PRESENT */ | 
|  | 746 | } | 
|  | 747 |  | 
|  | 748 | psa_status_t psa_driver_wrapper_cipher_encrypt_setup( | 
| Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 749 | psa_cipher_operation_t *operation, | 
|  | 750 | const psa_key_attributes_t *attributes, | 
|  | 751 | const uint8_t *key_buffer, size_t key_buffer_size, | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 752 | psa_algorithm_t alg ) | 
|  | 753 | { | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 754 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 755 | psa_key_location_t location = | 
|  | 756 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 757 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 758 | switch( location ) | 
|  | 759 | { | 
|  | 760 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
|  | 761 | /* Key is stored in the slot in export representation, so | 
|  | 762 | * cycle through all known transparent accelerators */ | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 763 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 764 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 765 | status = test_transparent_cipher_encrypt_setup( | 
|  | 766 | &operation->ctx.transparent_test_driver_ctx, | 
|  | 767 | attributes, | 
|  | 768 | key_buffer, | 
|  | 769 | key_buffer_size, | 
|  | 770 | alg ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 771 | /* Declared with fallback == true */ | 
| Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 772 | if( status == PSA_SUCCESS ) | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 773 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; | 
| Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 774 |  | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 775 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 776 | return( status ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 777 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 778 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 779 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 780 | /* Fell through, meaning no accelerator supports this operation */ | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 781 | status = mbedtls_psa_cipher_encrypt_setup( &operation->ctx.mbedtls_ctx, | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 782 | attributes, | 
|  | 783 | key_buffer, | 
|  | 784 | key_buffer_size, | 
|  | 785 | alg ); | 
|  | 786 | if( status == PSA_SUCCESS ) | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 787 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 788 |  | 
| Ronald Cron | 7b4154d | 2021-03-19 14:49:41 +0100 | [diff] [blame] | 789 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 790 | return( status ); | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 791 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ | 
|  | 792 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 793 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 794 | /* Add cases for opaque driver here */ | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 795 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 796 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 797 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 798 | status = test_opaque_cipher_encrypt_setup( | 
|  | 799 | &operation->ctx.opaque_test_driver_ctx, | 
|  | 800 | attributes, | 
|  | 801 | key_buffer, key_buffer_size, | 
|  | 802 | alg ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 803 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 804 | if( status == PSA_SUCCESS ) | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 805 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 806 |  | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 807 | return( status ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 808 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 809 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 810 | default: | 
|  | 811 | /* Key is declared with a lifetime not known to us */ | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 812 | (void)status; | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 813 | (void)key_buffer; | 
|  | 814 | (void)key_buffer_size; | 
|  | 815 | (void)alg; | 
| Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 816 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 817 | } | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 818 | } | 
|  | 819 |  | 
|  | 820 | psa_status_t psa_driver_wrapper_cipher_decrypt_setup( | 
| Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 821 | psa_cipher_operation_t *operation, | 
|  | 822 | const psa_key_attributes_t *attributes, | 
|  | 823 | const uint8_t *key_buffer, size_t key_buffer_size, | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 824 | psa_algorithm_t alg ) | 
|  | 825 | { | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 826 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; | 
| Ronald Cron | a4af55f | 2020-12-14 14:36:06 +0100 | [diff] [blame] | 827 | psa_key_location_t location = | 
|  | 828 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 829 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 830 | switch( location ) | 
|  | 831 | { | 
|  | 832 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
|  | 833 | /* Key is stored in the slot in export representation, so | 
|  | 834 | * cycle through all known transparent accelerators */ | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 835 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 836 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 837 | status = test_transparent_cipher_decrypt_setup( | 
|  | 838 | &operation->ctx.transparent_test_driver_ctx, | 
|  | 839 | attributes, | 
|  | 840 | key_buffer, | 
|  | 841 | key_buffer_size, | 
|  | 842 | alg ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 843 | /* Declared with fallback == true */ | 
| Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 844 | if( status == PSA_SUCCESS ) | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 845 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; | 
| Steven Cooreman | 150c99b | 2020-09-09 14:32:44 +0200 | [diff] [blame] | 846 |  | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 847 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 848 | return( status ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 849 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 850 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 851 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 852 | /* Fell through, meaning no accelerator supports this operation */ | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 853 | status = mbedtls_psa_cipher_decrypt_setup( &operation->ctx.mbedtls_ctx, | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 854 | attributes, | 
|  | 855 | key_buffer, | 
|  | 856 | key_buffer_size, | 
|  | 857 | alg ); | 
|  | 858 | if( status == PSA_SUCCESS ) | 
|  | 859 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; | 
|  | 860 |  | 
|  | 861 | return( status ); | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 862 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ | 
|  | 863 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 864 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 865 | /* Add cases for opaque driver here */ | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 866 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 867 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | a1ce2f2 | 2021-03-18 20:49:29 +0100 | [diff] [blame] | 868 | case PSA_CRYPTO_TEST_DRIVER_LOCATION: | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 869 | status = test_opaque_cipher_decrypt_setup( | 
|  | 870 | &operation->ctx.opaque_test_driver_ctx, | 
|  | 871 | attributes, | 
|  | 872 | key_buffer, key_buffer_size, | 
|  | 873 | alg ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 874 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 875 | if( status == PSA_SUCCESS ) | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 876 | operation->id = PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID; | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 877 |  | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 878 | return( status ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 879 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 880 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 881 | default: | 
|  | 882 | /* Key is declared with a lifetime not known to us */ | 
| Ronald Cron | 0b80559 | 2020-12-14 18:08:20 +0100 | [diff] [blame] | 883 | (void)status; | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 884 | (void)key_buffer; | 
|  | 885 | (void)key_buffer_size; | 
|  | 886 | (void)alg; | 
| Ronald Cron | c45b4af | 2020-09-29 16:18:05 +0200 | [diff] [blame] | 887 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 888 | } | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 889 | } | 
|  | 890 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 891 | psa_status_t psa_driver_wrapper_cipher_set_iv( | 
| Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 892 | psa_cipher_operation_t *operation, | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 893 | const uint8_t *iv, | 
|  | 894 | size_t iv_length ) | 
|  | 895 | { | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 896 | switch( operation->id ) | 
|  | 897 | { | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 898 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 899 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 900 | return( mbedtls_psa_cipher_set_iv( &operation->ctx.mbedtls_ctx, | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 901 | iv, | 
|  | 902 | iv_length ) ); | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 903 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 904 |  | 
|  | 905 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 906 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 907 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 908 | return( test_transparent_cipher_set_iv( | 
|  | 909 | &operation->ctx.transparent_test_driver_ctx, | 
|  | 910 | iv, iv_length ) ); | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 911 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 912 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 913 | return( test_opaque_cipher_set_iv( | 
|  | 914 | &operation->ctx.opaque_test_driver_ctx, | 
|  | 915 | iv, iv_length ) ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 916 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 917 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 918 | } | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 919 |  | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 920 | (void)iv; | 
|  | 921 | (void)iv_length; | 
|  | 922 |  | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 923 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 924 | } | 
|  | 925 |  | 
|  | 926 | psa_status_t psa_driver_wrapper_cipher_update( | 
| Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 927 | psa_cipher_operation_t *operation, | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 928 | const uint8_t *input, | 
|  | 929 | size_t input_length, | 
|  | 930 | uint8_t *output, | 
|  | 931 | size_t output_size, | 
|  | 932 | size_t *output_length ) | 
|  | 933 | { | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 934 | switch( operation->id ) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 935 | { | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 936 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 937 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 938 | return( mbedtls_psa_cipher_update( &operation->ctx.mbedtls_ctx, | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 939 | input, | 
|  | 940 | input_length, | 
|  | 941 | output, | 
|  | 942 | output_size, | 
|  | 943 | output_length ) ); | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 944 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ | 
|  | 945 |  | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 946 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 947 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 948 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 949 | return( test_transparent_cipher_update( | 
|  | 950 | &operation->ctx.transparent_test_driver_ctx, | 
|  | 951 | input, input_length, | 
|  | 952 | output, output_size, output_length ) ); | 
|  | 953 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 954 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 955 | return( test_opaque_cipher_update( | 
|  | 956 | &operation->ctx.opaque_test_driver_ctx, | 
|  | 957 | input, input_length, | 
|  | 958 | output, output_size, output_length ) ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 959 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 960 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 961 | } | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 962 |  | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 963 | (void)input; | 
|  | 964 | (void)input_length; | 
|  | 965 | (void)output; | 
|  | 966 | (void)output_size; | 
|  | 967 | (void)output_length; | 
|  | 968 |  | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 969 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 970 | } | 
|  | 971 |  | 
|  | 972 | psa_status_t psa_driver_wrapper_cipher_finish( | 
| Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 973 | psa_cipher_operation_t *operation, | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 974 | uint8_t *output, | 
|  | 975 | size_t output_size, | 
|  | 976 | size_t *output_length ) | 
|  | 977 | { | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 978 | switch( operation->id ) | 
|  | 979 | { | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 980 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 981 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 982 | return( mbedtls_psa_cipher_finish( &operation->ctx.mbedtls_ctx, | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 983 | output, | 
|  | 984 | output_size, | 
|  | 985 | output_length ) ); | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 986 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 987 |  | 
|  | 988 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 989 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 990 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 991 | return( test_transparent_cipher_finish( | 
|  | 992 | &operation->ctx.transparent_test_driver_ctx, | 
|  | 993 | output, output_size, output_length ) ); | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 994 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 995 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 996 | return( test_opaque_cipher_finish( | 
|  | 997 | &operation->ctx.opaque_test_driver_ctx, | 
|  | 998 | output, output_size, output_length ) ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 999 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1000 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1001 | } | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1002 |  | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1003 | (void)output; | 
|  | 1004 | (void)output_size; | 
|  | 1005 | (void)output_length; | 
|  | 1006 |  | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1007 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1008 | } | 
|  | 1009 |  | 
|  | 1010 | psa_status_t psa_driver_wrapper_cipher_abort( | 
| Ronald Cron | 6056fe8 | 2020-12-15 13:58:07 +0100 | [diff] [blame] | 1011 | psa_cipher_operation_t *operation ) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1012 | { | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1013 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1014 |  | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1015 | switch( operation->id ) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1016 | { | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1017 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1018 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 1019 | return( mbedtls_psa_cipher_abort( &operation->ctx.mbedtls_ctx ) ); | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 1020 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1021 |  | 
|  | 1022 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1023 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 1024 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1025 | status = test_transparent_cipher_abort( | 
|  | 1026 | &operation->ctx.transparent_test_driver_ctx ); | 
| Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1027 | mbedtls_platform_zeroize( | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1028 | &operation->ctx.transparent_test_driver_ctx, | 
|  | 1029 | sizeof( operation->ctx.transparent_test_driver_ctx ) ); | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1030 | return( status ); | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1031 |  | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1032 | case PSA_CRYPTO_OPAQUE_TEST_DRIVER_ID: | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1033 | status = test_opaque_cipher_abort( | 
|  | 1034 | &operation->ctx.opaque_test_driver_ctx ); | 
| Steven Cooreman | cfeea8f | 2020-09-09 15:09:18 +0200 | [diff] [blame] | 1035 | mbedtls_platform_zeroize( | 
| Ronald Cron | 7cb9c3d | 2021-03-10 12:21:48 +0100 | [diff] [blame] | 1036 | &operation->ctx.opaque_test_driver_ctx, | 
|  | 1037 | sizeof( operation->ctx.opaque_test_driver_ctx ) ); | 
| Steven Cooreman | 5240e8b | 2020-09-09 11:51:45 +0200 | [diff] [blame] | 1038 | return( status ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1039 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1040 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1041 | } | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1042 |  | 
| Ronald Cron | 49fafa9 | 2021-03-10 08:34:23 +0100 | [diff] [blame] | 1043 | (void)status; | 
| Ronald Cron | dd24c9b | 2020-12-15 14:10:01 +0100 | [diff] [blame] | 1044 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
| Steven Cooreman | 37941cb | 2020-07-28 18:49:51 +0200 | [diff] [blame] | 1045 | } | 
|  | 1046 |  | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1047 | /* | 
|  | 1048 | * Hashing functions | 
|  | 1049 | */ | 
|  | 1050 | psa_status_t psa_driver_wrapper_hash_compute( | 
|  | 1051 | psa_algorithm_t alg, | 
|  | 1052 | const uint8_t *input, | 
|  | 1053 | size_t input_length, | 
|  | 1054 | uint8_t *hash, | 
|  | 1055 | size_t hash_size, | 
|  | 1056 | size_t *hash_length) | 
|  | 1057 | { | 
| Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1058 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1059 |  | 
|  | 1060 | /* Try accelerators first */ | 
| Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1061 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1062 | status = mbedtls_transparent_test_driver_hash_compute( | 
|  | 1063 | alg, input, input_length, hash, hash_size, hash_length ); | 
| Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1064 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 1065 | return( status ); | 
|  | 1066 | #endif | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1067 |  | 
|  | 1068 | /* If software fallback is compiled in, try fallback */ | 
|  | 1069 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) | 
|  | 1070 | status = mbedtls_psa_hash_compute( alg, input, input_length, | 
|  | 1071 | hash, hash_size, hash_length ); | 
|  | 1072 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 1073 | return( status ); | 
|  | 1074 | #endif | 
| Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1075 | (void) status; | 
|  | 1076 | (void) alg; | 
|  | 1077 | (void) input; | 
|  | 1078 | (void) input_length; | 
|  | 1079 | (void) hash; | 
|  | 1080 | (void) hash_size; | 
|  | 1081 | (void) hash_length; | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1082 |  | 
| Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1083 | return( PSA_ERROR_NOT_SUPPORTED ); | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1084 | } | 
|  | 1085 |  | 
|  | 1086 | psa_status_t psa_driver_wrapper_hash_setup( | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1087 | psa_hash_operation_t *operation, | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1088 | psa_algorithm_t alg ) | 
|  | 1089 | { | 
| Steven Cooreman | 0eeb794 | 2021-03-08 12:13:21 +0100 | [diff] [blame] | 1090 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1091 |  | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1092 | /* Try setup on accelerators first */ | 
| Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1093 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1094 | status = mbedtls_transparent_test_driver_hash_setup( | 
|  | 1095 | &operation->ctx.test_driver_ctx, alg ); | 
| Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1096 | if( status == PSA_SUCCESS ) | 
|  | 1097 | operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; | 
|  | 1098 |  | 
|  | 1099 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 1100 | return( status ); | 
|  | 1101 | #endif | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1102 |  | 
|  | 1103 | /* If software fallback is compiled in, try fallback */ | 
|  | 1104 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1105 | status = mbedtls_psa_hash_setup( &operation->ctx.mbedtls_ctx, alg ); | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1106 | if( status == PSA_SUCCESS ) | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1107 | operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1108 |  | 
|  | 1109 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 1110 | return( status ); | 
|  | 1111 | #endif | 
|  | 1112 | /* Nothing left to try if we fall through here */ | 
|  | 1113 | (void) status; | 
|  | 1114 | (void) operation; | 
|  | 1115 | (void) alg; | 
|  | 1116 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 1117 | } | 
|  | 1118 |  | 
|  | 1119 | psa_status_t psa_driver_wrapper_hash_clone( | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1120 | const psa_hash_operation_t *source_operation, | 
|  | 1121 | psa_hash_operation_t *target_operation ) | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1122 | { | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1123 | switch( source_operation->id ) | 
|  | 1124 | { | 
| Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1125 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) | 
|  | 1126 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: | 
|  | 1127 | target_operation->id = PSA_CRYPTO_MBED_TLS_DRIVER_ID; | 
|  | 1128 | return( mbedtls_psa_hash_clone( &source_operation->ctx.mbedtls_ctx, | 
|  | 1129 | &target_operation->ctx.mbedtls_ctx ) ); | 
|  | 1130 | #endif | 
| Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1131 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1132 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: | 
|  | 1133 | target_operation->id = PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID; | 
| Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1134 | return( mbedtls_transparent_test_driver_hash_clone( | 
|  | 1135 | &source_operation->ctx.test_driver_ctx, | 
|  | 1136 | &target_operation->ctx.test_driver_ctx ) ); | 
| Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1137 | #endif | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1138 | default: | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1139 | (void) target_operation; | 
|  | 1140 | return( PSA_ERROR_BAD_STATE ); | 
|  | 1141 | } | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1142 | } | 
|  | 1143 |  | 
|  | 1144 | psa_status_t psa_driver_wrapper_hash_update( | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1145 | psa_hash_operation_t *operation, | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1146 | const uint8_t *input, | 
|  | 1147 | size_t input_length ) | 
|  | 1148 | { | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1149 | switch( operation->id ) | 
|  | 1150 | { | 
| Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1151 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) | 
|  | 1152 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: | 
|  | 1153 | return( mbedtls_psa_hash_update( &operation->ctx.mbedtls_ctx, | 
|  | 1154 | input, input_length ) ); | 
|  | 1155 | #endif | 
| Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1156 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1157 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: | 
| Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1158 | return( mbedtls_transparent_test_driver_hash_update( | 
|  | 1159 | &operation->ctx.test_driver_ctx, | 
|  | 1160 | input, input_length ) ); | 
| Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1161 | #endif | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1162 | default: | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1163 | (void) input; | 
|  | 1164 | (void) input_length; | 
|  | 1165 | return( PSA_ERROR_BAD_STATE ); | 
|  | 1166 | } | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1167 | } | 
|  | 1168 |  | 
|  | 1169 | psa_status_t psa_driver_wrapper_hash_finish( | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1170 | psa_hash_operation_t *operation, | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1171 | uint8_t *hash, | 
|  | 1172 | size_t hash_size, | 
|  | 1173 | size_t *hash_length ) | 
|  | 1174 | { | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1175 | switch( operation->id ) | 
|  | 1176 | { | 
| Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1177 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) | 
|  | 1178 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: | 
|  | 1179 | return( mbedtls_psa_hash_finish( &operation->ctx.mbedtls_ctx, | 
|  | 1180 | hash, hash_size, hash_length ) ); | 
|  | 1181 | #endif | 
| Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1182 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1183 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: | 
| Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1184 | return( mbedtls_transparent_test_driver_hash_finish( | 
|  | 1185 | &operation->ctx.test_driver_ctx, | 
|  | 1186 | hash, hash_size, hash_length ) ); | 
| Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1187 | #endif | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1188 | default: | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1189 | (void) hash; | 
|  | 1190 | (void) hash_size; | 
|  | 1191 | (void) hash_length; | 
|  | 1192 | return( PSA_ERROR_BAD_STATE ); | 
|  | 1193 | } | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1194 | } | 
|  | 1195 |  | 
|  | 1196 | psa_status_t psa_driver_wrapper_hash_abort( | 
| Steven Cooreman | dbf8ced | 2021-03-04 13:01:18 +0100 | [diff] [blame] | 1197 | psa_hash_operation_t *operation ) | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1198 | { | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1199 | switch( operation->id ) | 
|  | 1200 | { | 
| Steven Cooreman | 5e4c18f | 2021-03-15 12:26:07 +0100 | [diff] [blame] | 1201 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) | 
|  | 1202 | case PSA_CRYPTO_MBED_TLS_DRIVER_ID: | 
|  | 1203 | return( mbedtls_psa_hash_abort( &operation->ctx.mbedtls_ctx ) ); | 
|  | 1204 | #endif | 
| Steven Cooreman | f8e45a4 | 2021-03-16 11:07:55 +0100 | [diff] [blame] | 1205 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
| Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1206 | case PSA_CRYPTO_TRANSPARENT_TEST_DRIVER_ID: | 
| Steven Cooreman | 0f8ffa8 | 2021-03-15 11:56:33 +0100 | [diff] [blame] | 1207 | return( mbedtls_transparent_test_driver_hash_abort( | 
|  | 1208 | &operation->ctx.test_driver_ctx ) ); | 
| Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 1209 | #endif | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1210 | default: | 
| Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 1211 | return( PSA_ERROR_BAD_STATE ); | 
|  | 1212 | } | 
|  | 1213 | } | 
|  | 1214 |  | 
| Ronald Cron | de82281 | 2021-03-17 16:08:20 +0100 | [diff] [blame] | 1215 | psa_status_t psa_driver_wrapper_aead_encrypt( | 
|  | 1216 | const psa_key_attributes_t *attributes, | 
|  | 1217 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 1218 | psa_algorithm_t alg, | 
|  | 1219 | const uint8_t *nonce, size_t nonce_length, | 
|  | 1220 | const uint8_t *additional_data, size_t additional_data_length, | 
|  | 1221 | const uint8_t *plaintext, size_t plaintext_length, | 
|  | 1222 | uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length ) | 
|  | 1223 | { | 
|  | 1224 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 1225 | psa_key_location_t location = | 
|  | 1226 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); | 
|  | 1227 |  | 
|  | 1228 | switch( location ) | 
|  | 1229 | { | 
|  | 1230 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
|  | 1231 | /* Key is stored in the slot in export representation, so | 
|  | 1232 | * cycle through all known transparent accelerators */ | 
|  | 1233 |  | 
|  | 1234 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
|  | 1235 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 1236 | status = test_transparent_aead_encrypt( | 
|  | 1237 | attributes, key_buffer, key_buffer_size, | 
|  | 1238 | alg, | 
|  | 1239 | nonce, nonce_length, | 
|  | 1240 | additional_data, additional_data_length, | 
|  | 1241 | plaintext, plaintext_length, | 
|  | 1242 | ciphertext, ciphertext_size, ciphertext_length ); | 
|  | 1243 | /* Declared with fallback == true */ | 
|  | 1244 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 1245 | return( status ); | 
|  | 1246 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 1247 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
|  | 1248 |  | 
|  | 1249 | /* Fell through, meaning no accelerator supports this operation */ | 
|  | 1250 | return( mbedtls_psa_aead_encrypt( | 
|  | 1251 | attributes, key_buffer, key_buffer_size, | 
|  | 1252 | alg, | 
|  | 1253 | nonce, nonce_length, | 
|  | 1254 | additional_data, additional_data_length, | 
|  | 1255 | plaintext, plaintext_length, | 
|  | 1256 | ciphertext, ciphertext_size, ciphertext_length ) ); | 
|  | 1257 |  | 
|  | 1258 | /* Add cases for opaque driver here */ | 
|  | 1259 |  | 
|  | 1260 | default: | 
|  | 1261 | /* Key is declared with a lifetime not known to us */ | 
|  | 1262 | (void)status; | 
|  | 1263 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1264 | } | 
|  | 1265 | } | 
|  | 1266 |  | 
|  | 1267 | psa_status_t psa_driver_wrapper_aead_decrypt( | 
|  | 1268 | const psa_key_attributes_t *attributes, | 
|  | 1269 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 1270 | psa_algorithm_t alg, | 
|  | 1271 | const uint8_t *nonce, size_t nonce_length, | 
|  | 1272 | const uint8_t *additional_data, size_t additional_data_length, | 
|  | 1273 | const uint8_t *ciphertext, size_t ciphertext_length, | 
|  | 1274 | uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length ) | 
|  | 1275 | { | 
|  | 1276 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 1277 | psa_key_location_t location = | 
|  | 1278 | PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ); | 
|  | 1279 |  | 
|  | 1280 | switch( location ) | 
|  | 1281 | { | 
|  | 1282 | case PSA_KEY_LOCATION_LOCAL_STORAGE: | 
|  | 1283 | /* Key is stored in the slot in export representation, so | 
|  | 1284 | * cycle through all known transparent accelerators */ | 
|  | 1285 |  | 
|  | 1286 | #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) | 
|  | 1287 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 1288 | status = test_transparent_aead_decrypt( | 
|  | 1289 | attributes, key_buffer, key_buffer_size, | 
|  | 1290 | alg, | 
|  | 1291 | nonce, nonce_length, | 
|  | 1292 | additional_data, additional_data_length, | 
|  | 1293 | ciphertext, ciphertext_length, | 
|  | 1294 | plaintext, plaintext_size, plaintext_length ); | 
|  | 1295 | /* Declared with fallback == true */ | 
|  | 1296 | if( status != PSA_ERROR_NOT_SUPPORTED ) | 
|  | 1297 | return( status ); | 
|  | 1298 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 1299 | #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ | 
|  | 1300 |  | 
|  | 1301 | /* Fell through, meaning no accelerator supports this operation */ | 
|  | 1302 | return( mbedtls_psa_aead_decrypt( | 
|  | 1303 | attributes, key_buffer, key_buffer_size, | 
|  | 1304 | alg, | 
|  | 1305 | nonce, nonce_length, | 
|  | 1306 | additional_data, additional_data_length, | 
|  | 1307 | ciphertext, ciphertext_length, | 
|  | 1308 | plaintext, plaintext_size, plaintext_length ) ); | 
|  | 1309 |  | 
|  | 1310 | /* Add cases for opaque driver here */ | 
|  | 1311 |  | 
|  | 1312 | default: | 
|  | 1313 | /* Key is declared with a lifetime not known to us */ | 
|  | 1314 | (void)status; | 
|  | 1315 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 1316 | } | 
|  | 1317 | } | 
| Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 1318 | /* End of automatically generated file. */ |