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. |
| 4 | * Warning: auto-generated file. |
| 5 | */ |
| 6 | /* Copyright (C) 2020, ARM Limited, All Rights Reserved |
| 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. |
| 20 | * |
| 21 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 22 | */ |
| 23 | |
| 24 | #include "psa_crypto_core.h" |
| 25 | #include "psa_crypto_driver_wrappers.h" |
| 26 | |
| 27 | /* Include test driver definition when running tests */ |
| 28 | #if defined(MBEDTLS_TEST_HOOKS) |
| 29 | #undef MBEDTLS_PSA_CRYPTO_DRIVER_PRESENT |
| 30 | #define MBEDTLS_PSA_CRYPTO_DRIVER_PRESENT |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 31 | #undef MBEDTLS_PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT |
| 32 | #define MBEDTLS_PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 33 | #include "drivers/test_driver.h" |
| 34 | #endif |
| 35 | |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 36 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 37 | #undef MBEDTLS_PSA_CRYPTO_DRIVER_PRESENT |
| 38 | #define MBEDTLS_PSA_CRYPTO_DRIVER_PRESENT |
| 39 | #include "psa_crypto_se.h" |
| 40 | #endif |
| 41 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 42 | /* Include driver definition file for each registered driver */ |
| 43 | |
| 44 | /* Start delegation functions */ |
| 45 | psa_status_t psa_driver_wrapper_sign_hash( psa_key_slot_t *slot, |
| 46 | psa_algorithm_t alg, |
| 47 | const uint8_t *hash, |
| 48 | size_t hash_length, |
| 49 | uint8_t *signature, |
| 50 | size_t signature_size, |
| 51 | size_t *signature_length ) |
| 52 | { |
| 53 | #if defined(MBEDTLS_PSA_CRYPTO_DRIVER_PRESENT) |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 54 | /* Try dynamically-registered SE interface first */ |
| 55 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 56 | const psa_drv_se_t *drv; |
| 57 | psa_drv_se_context_t *drv_context; |
| 58 | |
| 59 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 60 | { |
| 61 | if( drv->asymmetric == NULL || |
| 62 | drv->asymmetric->p_sign == NULL ) |
| 63 | { |
| 64 | /* Key is defined in SE, but we have no way to exercise it */ |
| 65 | return PSA_ERROR_INVALID_ARGUMENT; |
| 66 | } |
| 67 | return( drv->asymmetric->p_sign( drv_context, |
| 68 | slot->data.se.slot_number, |
| 69 | alg, |
| 70 | hash, hash_length, |
| 71 | signature, signature_size, |
| 72 | signature_length ) ); |
| 73 | } |
| 74 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 75 | |
| 76 | /* Then try accelerator API */ |
| 77 | #if defined(MBEDTLS_PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 78 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 79 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 80 | psa_key_attributes_t attributes = { |
| 81 | .core = slot->attr |
| 82 | }; |
| 83 | |
| 84 | switch( location ) |
| 85 | { |
| 86 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 87 | /* Key is stored in the slot in export representation, so |
| 88 | * cycle through all known transparent accelerators */ |
| 89 | #if defined(MBEDTLS_TEST_HOOKS) |
| 90 | status = test_transparent_signature_sign_hash( &attributes, |
| 91 | slot->data.key.data, |
| 92 | slot->data.key.bytes, |
| 93 | alg, |
| 94 | hash, |
| 95 | hash_length, |
| 96 | signature, |
| 97 | signature_size, |
| 98 | signature_length ); |
| 99 | /* Declared with fallback == true */ |
| 100 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 101 | return status; |
| 102 | #endif /* MBEDTLS_TEST_HOOKS */ |
| 103 | /* Fell through, meaning no accelerator supports this operation */ |
| 104 | return PSA_ERROR_NOT_SUPPORTED; |
| 105 | /* Add cases for opaque driver here */ |
| 106 | #if defined(MBEDTLS_TEST_HOOKS) |
| 107 | case MBEDTLS_PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
| 108 | return( test_opaque_signature_sign_hash( &attributes, |
| 109 | slot->data.key.data, |
| 110 | slot->data.key.bytes, |
| 111 | alg, |
| 112 | hash, |
| 113 | hash_length, |
| 114 | signature, |
| 115 | signature_size, |
| 116 | signature_length ) ); |
| 117 | #endif /* MBEDTLS_TEST_HOOKS */ |
| 118 | default: |
| 119 | /* Key is declared with a lifetime not known to us */ |
| 120 | return status; |
| 121 | } |
Steven Cooreman | 7a25057 | 2020-07-17 16:43:05 +0200 | [diff] [blame] | 122 | #else /* MBEDTLS_PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 123 | return PSA_ERROR_NOT_SUPPORTED; |
| 124 | #endif /* MBEDTLS_PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 125 | #else /* MBEDTLS_PSA_CRYPTO_DRIVER_PRESENT */ |
| 126 | (void)slot; |
| 127 | (void)alg; |
| 128 | (void)hash; |
| 129 | (void)hash_length; |
| 130 | (void)signature; |
| 131 | (void)signature_size; |
| 132 | (void)signature_length; |
| 133 | |
| 134 | return PSA_ERROR_NOT_SUPPORTED; |
| 135 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVER_PRESENT */ |
| 136 | } |
| 137 | |
Steven Cooreman | 55ae217 | 2020-07-17 19:46:15 +0200 | [diff] [blame^] | 138 | psa_status_t psa_driver_wrapper_verify_hash( psa_key_slot_t *slot, |
| 139 | psa_algorithm_t alg, |
| 140 | const uint8_t *hash, |
| 141 | size_t hash_length, |
| 142 | const uint8_t *signature, |
| 143 | size_t signature_length ) |
| 144 | { |
| 145 | #if defined(MBEDTLS_PSA_CRYPTO_DRIVER_PRESENT) |
| 146 | /* Try dynamically-registered SE interface first */ |
| 147 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 148 | const psa_drv_se_t *drv; |
| 149 | psa_drv_se_context_t *drv_context; |
| 150 | |
| 151 | if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) ) |
| 152 | { |
| 153 | if( drv->asymmetric == NULL || |
| 154 | drv->asymmetric->p_verify == NULL ) |
| 155 | { |
| 156 | /* Key is defined in SE, but we have no way to exercise it */ |
| 157 | return PSA_ERROR_INVALID_ARGUMENT; |
| 158 | } |
| 159 | return( drv->asymmetric->p_verify( drv_context, |
| 160 | slot->data.se.slot_number, |
| 161 | alg, |
| 162 | hash, hash_length, |
| 163 | signature, signature_length ) ); |
| 164 | } |
| 165 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 166 | |
| 167 | /* Then try accelerator API */ |
| 168 | #if defined(MBEDTLS_PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT) |
| 169 | psa_status_t status = PSA_ERROR_INVALID_ARGUMENT; |
| 170 | psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime); |
| 171 | psa_key_attributes_t attributes = { |
| 172 | .core = slot->attr |
| 173 | }; |
| 174 | |
| 175 | switch( location ) |
| 176 | { |
| 177 | case PSA_KEY_LOCATION_LOCAL_STORAGE: |
| 178 | /* Key is stored in the slot in export representation, so |
| 179 | * cycle through all known transparent accelerators */ |
| 180 | #if defined(MBEDTLS_TEST_HOOKS) |
| 181 | status = test_transparent_signature_verify_hash( &attributes, |
| 182 | slot->data.key.data, |
| 183 | slot->data.key.bytes, |
| 184 | alg, |
| 185 | hash, |
| 186 | hash_length, |
| 187 | signature, |
| 188 | signature_length ); |
| 189 | /* Declared with fallback == true */ |
| 190 | if( status != PSA_ERROR_NOT_SUPPORTED ) |
| 191 | return status; |
| 192 | #endif /* MBEDTLS_TEST_HOOKS */ |
| 193 | /* Fell through, meaning no accelerator supports this operation */ |
| 194 | return PSA_ERROR_NOT_SUPPORTED; |
| 195 | /* Add cases for opaque driver here */ |
| 196 | #if defined(MBEDTLS_TEST_HOOKS) |
| 197 | case MBEDTLS_PSA_CRYPTO_TEST_DRIVER_LIFETIME: |
| 198 | return( test_opaque_signature_verify_hash( &attributes, |
| 199 | slot->data.key.data, |
| 200 | slot->data.key.bytes, |
| 201 | alg, |
| 202 | hash, |
| 203 | hash_length, |
| 204 | signature, |
| 205 | signature_length ) ); |
| 206 | #endif /* MBEDTLS_TEST_HOOKS */ |
| 207 | default: |
| 208 | /* Key is declared with a lifetime not known to us */ |
| 209 | return status; |
| 210 | } |
| 211 | #else /* MBEDTLS_PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 212 | return PSA_ERROR_NOT_SUPPORTED; |
| 213 | #endif /* MBEDTLS_PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */ |
| 214 | #else /* MBEDTLS_PSA_CRYPTO_DRIVER_PRESENT */ |
| 215 | (void)slot; |
| 216 | (void)alg; |
| 217 | (void)hash; |
| 218 | (void)hash_length; |
| 219 | (void)signature; |
| 220 | (void)signature_length; |
| 221 | |
| 222 | return PSA_ERROR_NOT_SUPPORTED; |
| 223 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVER_PRESENT */ |
| 224 | } |
| 225 | |
| 226 | psa_status_t psa_driver_wrapper_generate_key( const psa_key_attributes_t *attributes, |
| 227 | psa_key_slot_t *slot ) |
| 228 | { |
| 229 | (void) attributes; |
| 230 | (void) slot; |
| 231 | |
| 232 | return PSA_ERROR_NOT_SUPPORTED; |
| 233 | } |
| 234 | |
Steven Cooreman | cd84cb4 | 2020-07-16 20:28:36 +0200 | [diff] [blame] | 235 | /* End of automatically generated file. */ |