Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Test driver for hash entry points. |
| 3 | */ |
| 4 | /* Copyright The Mbed TLS Contributors |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
| 20 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 21 | #include "mbedtls/config.h" |
| 22 | #else |
| 23 | #include MBEDTLS_CONFIG_FILE |
| 24 | #endif |
| 25 | |
| 26 | #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST) |
| 27 | #include "psa_crypto_hash.h" |
| 28 | |
| 29 | #include "test/drivers/hash.h" |
| 30 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 31 | mbedtls_test_driver_hash_hooks_t |
| 32 | mbedtls_test_driver_hash_hooks = MBEDTLS_TEST_DRIVER_HASH_INIT; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 33 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 34 | psa_status_t mbedtls_test_transparent_hash_compute( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 35 | psa_algorithm_t alg, |
| 36 | const uint8_t *input, size_t input_length, |
| 37 | uint8_t *hash, size_t hash_size, size_t *hash_length ) |
| 38 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 39 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 40 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 41 | if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS ) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 42 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 43 | mbedtls_test_driver_hash_hooks.driver_status = |
| 44 | mbedtls_test_driver_hash_hooks.forced_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 45 | } |
| 46 | else |
| 47 | { |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 48 | #if defined(MBEDTLS_PSA_CRYPTO_CONFIG) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 49 | mbedtls_test_driver_hash_hooks.driver_status = |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 50 | mbedtls_transparent_test_driver_hash_compute( |
| 51 | alg, input, input_length, |
| 52 | hash, hash_size, hash_length ); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 53 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 54 | mbedtls_test_driver_hash_hooks.driver_status = |
| 55 | mbedtls_psa_hash_compute( |
| 56 | alg, input, input_length, |
| 57 | hash, hash_size, hash_length ); |
| 58 | #else |
| 59 | (void) alg; |
| 60 | (void) input; |
| 61 | (void) input_length; |
| 62 | (void) hash; |
| 63 | (void) hash_size; |
| 64 | (void) hash_length; |
| 65 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 66 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 69 | return( mbedtls_test_driver_hash_hooks.driver_status ); |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 72 | psa_status_t mbedtls_test_transparent_hash_setup( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 73 | mbedtls_transparent_test_driver_hash_operation_t *operation, |
| 74 | psa_algorithm_t alg ) |
| 75 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 76 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 77 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 78 | if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS ) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 79 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 80 | mbedtls_test_driver_hash_hooks.driver_status = |
| 81 | mbedtls_test_driver_hash_hooks.forced_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 82 | } |
| 83 | else |
| 84 | { |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 85 | #if defined(MBEDTLS_PSA_CRYPTO_CONFIG) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 86 | mbedtls_test_driver_hash_hooks.driver_status = |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 87 | mbedtls_transparent_test_driver_hash_setup( operation, alg ); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 88 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 89 | mbedtls_test_driver_hash_hooks.driver_status = |
| 90 | mbedtls_psa_hash_setup( operation, alg ); |
| 91 | #else |
| 92 | (void) operation; |
| 93 | (void) alg; |
| 94 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 95 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 98 | return( mbedtls_test_driver_hash_hooks.driver_status ); |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 101 | psa_status_t mbedtls_test_transparent_hash_clone( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 102 | const mbedtls_transparent_test_driver_hash_operation_t *source_operation, |
| 103 | mbedtls_transparent_test_driver_hash_operation_t *target_operation ) |
| 104 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 105 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 106 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 107 | if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS ) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 108 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 109 | mbedtls_test_driver_hash_hooks.driver_status = |
| 110 | mbedtls_test_driver_hash_hooks.forced_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 111 | } |
| 112 | else |
| 113 | { |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 114 | #if defined(MBEDTLS_PSA_CRYPTO_CONFIG) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 115 | mbedtls_test_driver_hash_hooks.driver_status = |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 116 | mbedtls_transparent_test_driver_hash_clone( source_operation, |
| 117 | target_operation ); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 118 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 119 | mbedtls_test_driver_hash_hooks.driver_status = |
| 120 | mbedtls_psa_hash_clone( source_operation, target_operation ); |
| 121 | #else |
| 122 | (void) source_operation; |
| 123 | (void) target_operation; |
| 124 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 125 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 128 | return( mbedtls_test_driver_hash_hooks.driver_status ); |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 131 | psa_status_t mbedtls_test_transparent_hash_update( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 132 | mbedtls_transparent_test_driver_hash_operation_t *operation, |
| 133 | const uint8_t *input, |
| 134 | size_t input_length ) |
| 135 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 136 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 137 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 138 | if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS ) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 139 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 140 | mbedtls_test_driver_hash_hooks.driver_status = |
| 141 | mbedtls_test_driver_hash_hooks.forced_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 142 | } |
| 143 | else |
| 144 | { |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 145 | #if defined(MBEDTLS_PSA_CRYPTO_CONFIG) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 146 | mbedtls_test_driver_hash_hooks.driver_status = |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 147 | mbedtls_transparent_test_driver_hash_update( |
| 148 | operation, input, input_length ); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 149 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 150 | mbedtls_test_driver_hash_hooks.driver_status = |
| 151 | mbedtls_psa_hash_update( operation, input, input_length ); |
| 152 | #else |
| 153 | (void) operation; |
| 154 | (void) input; |
| 155 | (void) input_length; |
| 156 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 157 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 160 | return( mbedtls_test_driver_hash_hooks.driver_status ); |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 163 | psa_status_t mbedtls_test_transparent_hash_finish( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 164 | mbedtls_transparent_test_driver_hash_operation_t *operation, |
| 165 | uint8_t *hash, |
| 166 | size_t hash_size, |
| 167 | size_t *hash_length ) |
| 168 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 169 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 170 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 171 | if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS ) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 172 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 173 | mbedtls_test_driver_hash_hooks.driver_status = |
| 174 | mbedtls_test_driver_hash_hooks.forced_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 175 | } |
| 176 | else |
| 177 | { |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 178 | #if defined(MBEDTLS_PSA_CRYPTO_CONFIG) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 179 | mbedtls_test_driver_hash_hooks.driver_status = |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 180 | mbedtls_transparent_test_driver_hash_finish( |
| 181 | operation, hash, hash_size, hash_length ); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 182 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 183 | mbedtls_test_driver_hash_hooks.driver_status = |
| 184 | mbedtls_psa_hash_finish( operation, hash, hash_size, hash_length ); |
| 185 | #else |
| 186 | (void) operation; |
| 187 | (void) hash; |
| 188 | (void) hash_size; |
| 189 | (void) hash_length; |
| 190 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 191 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 194 | return( mbedtls_test_driver_hash_hooks.driver_status ); |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 197 | psa_status_t mbedtls_test_transparent_hash_abort( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 198 | mbedtls_transparent_test_driver_hash_operation_t *operation ) |
| 199 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 200 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 201 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 202 | if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS ) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 203 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 204 | mbedtls_test_driver_hash_hooks.driver_status = |
| 205 | mbedtls_test_driver_hash_hooks.forced_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 206 | } |
| 207 | else |
| 208 | { |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 209 | #if defined(MBEDTLS_PSA_CRYPTO_CONFIG) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 210 | mbedtls_test_driver_hash_hooks.driver_status = |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 211 | mbedtls_transparent_test_driver_hash_abort( operation ); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 212 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 213 | mbedtls_test_driver_hash_hooks.driver_status = |
| 214 | mbedtls_psa_hash_abort( operation ); |
| 215 | #else |
| 216 | (void) operation; |
| 217 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 218 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 219 | } |
| 220 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 221 | return( mbedtls_test_driver_hash_hooks.driver_status ); |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 222 | } |
| 223 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ |