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 | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame^] | 31 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) |
| 32 | #include "libtestdriver1/library/psa_crypto_hash.h" |
| 33 | #endif |
| 34 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 35 | mbedtls_test_driver_hash_hooks_t |
| 36 | mbedtls_test_driver_hash_hooks = MBEDTLS_TEST_DRIVER_HASH_INIT; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 37 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 38 | psa_status_t mbedtls_test_transparent_hash_compute( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 39 | psa_algorithm_t alg, |
| 40 | const uint8_t *input, size_t input_length, |
| 41 | uint8_t *hash, size_t hash_size, size_t *hash_length ) |
| 42 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 43 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 44 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 45 | if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS ) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 46 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 47 | mbedtls_test_driver_hash_hooks.driver_status = |
| 48 | mbedtls_test_driver_hash_hooks.forced_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 49 | } |
| 50 | else |
| 51 | { |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame^] | 52 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 53 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 54 | mbedtls_test_driver_hash_hooks.driver_status = |
Ronald Cron | 7b7854e | 2021-03-13 18:19:08 +0100 | [diff] [blame] | 55 | libtestdriver1_mbedtls_psa_hash_compute( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 56 | alg, input, input_length, |
| 57 | hash, hash_size, hash_length ); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 58 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 59 | mbedtls_test_driver_hash_hooks.driver_status = |
| 60 | mbedtls_psa_hash_compute( |
| 61 | alg, input, input_length, |
| 62 | hash, hash_size, hash_length ); |
| 63 | #else |
| 64 | (void) alg; |
| 65 | (void) input; |
| 66 | (void) input_length; |
| 67 | (void) hash; |
| 68 | (void) hash_size; |
| 69 | (void) hash_length; |
| 70 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 71 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 74 | return( mbedtls_test_driver_hash_hooks.driver_status ); |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 77 | psa_status_t mbedtls_test_transparent_hash_setup( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 78 | mbedtls_transparent_test_driver_hash_operation_t *operation, |
| 79 | psa_algorithm_t alg ) |
| 80 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 81 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 82 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 83 | if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS ) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 84 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 85 | mbedtls_test_driver_hash_hooks.driver_status = |
| 86 | mbedtls_test_driver_hash_hooks.forced_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 87 | } |
| 88 | else |
| 89 | { |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame^] | 90 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 91 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 92 | mbedtls_test_driver_hash_hooks.driver_status = |
Ronald Cron | 7b7854e | 2021-03-13 18:19:08 +0100 | [diff] [blame] | 93 | libtestdriver1_mbedtls_psa_hash_setup( operation, alg ); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 94 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 95 | mbedtls_test_driver_hash_hooks.driver_status = |
| 96 | mbedtls_psa_hash_setup( operation, alg ); |
| 97 | #else |
| 98 | (void) operation; |
| 99 | (void) alg; |
| 100 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 101 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 104 | return( mbedtls_test_driver_hash_hooks.driver_status ); |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 107 | psa_status_t mbedtls_test_transparent_hash_clone( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 108 | const mbedtls_transparent_test_driver_hash_operation_t *source_operation, |
| 109 | mbedtls_transparent_test_driver_hash_operation_t *target_operation ) |
| 110 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 111 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 112 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 113 | if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS ) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 114 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 115 | mbedtls_test_driver_hash_hooks.driver_status = |
| 116 | mbedtls_test_driver_hash_hooks.forced_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 117 | } |
| 118 | else |
| 119 | { |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame^] | 120 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 121 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 122 | mbedtls_test_driver_hash_hooks.driver_status = |
Ronald Cron | 7b7854e | 2021-03-13 18:19:08 +0100 | [diff] [blame] | 123 | libtestdriver1_mbedtls_psa_hash_clone( source_operation, |
| 124 | target_operation ); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 125 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 126 | mbedtls_test_driver_hash_hooks.driver_status = |
| 127 | mbedtls_psa_hash_clone( source_operation, target_operation ); |
| 128 | #else |
| 129 | (void) source_operation; |
| 130 | (void) target_operation; |
| 131 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 132 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 135 | return( mbedtls_test_driver_hash_hooks.driver_status ); |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 138 | psa_status_t mbedtls_test_transparent_hash_update( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 139 | mbedtls_transparent_test_driver_hash_operation_t *operation, |
| 140 | const uint8_t *input, |
| 141 | size_t input_length ) |
| 142 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 143 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 144 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 145 | if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS ) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 146 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 147 | mbedtls_test_driver_hash_hooks.driver_status = |
| 148 | mbedtls_test_driver_hash_hooks.forced_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 149 | } |
| 150 | else |
| 151 | { |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame^] | 152 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 153 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 154 | mbedtls_test_driver_hash_hooks.driver_status = |
Ronald Cron | 7b7854e | 2021-03-13 18:19:08 +0100 | [diff] [blame] | 155 | libtestdriver1_mbedtls_psa_hash_update( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 156 | operation, input, input_length ); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 157 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 158 | mbedtls_test_driver_hash_hooks.driver_status = |
| 159 | mbedtls_psa_hash_update( operation, input, input_length ); |
| 160 | #else |
| 161 | (void) operation; |
| 162 | (void) input; |
| 163 | (void) input_length; |
| 164 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 165 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 168 | return( mbedtls_test_driver_hash_hooks.driver_status ); |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 171 | psa_status_t mbedtls_test_transparent_hash_finish( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 172 | mbedtls_transparent_test_driver_hash_operation_t *operation, |
| 173 | uint8_t *hash, |
| 174 | size_t hash_size, |
| 175 | size_t *hash_length ) |
| 176 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 177 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 178 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 179 | if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS ) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 180 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 181 | mbedtls_test_driver_hash_hooks.driver_status = |
| 182 | mbedtls_test_driver_hash_hooks.forced_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 183 | } |
| 184 | else |
| 185 | { |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame^] | 186 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 187 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 188 | mbedtls_test_driver_hash_hooks.driver_status = |
Ronald Cron | 7b7854e | 2021-03-13 18:19:08 +0100 | [diff] [blame] | 189 | libtestdriver1_mbedtls_psa_hash_finish( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 190 | operation, hash, hash_size, hash_length ); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 191 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 192 | mbedtls_test_driver_hash_hooks.driver_status = |
| 193 | mbedtls_psa_hash_finish( operation, hash, hash_size, hash_length ); |
| 194 | #else |
| 195 | (void) operation; |
| 196 | (void) hash; |
| 197 | (void) hash_size; |
| 198 | (void) hash_length; |
| 199 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 200 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 203 | return( mbedtls_test_driver_hash_hooks.driver_status ); |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 206 | psa_status_t mbedtls_test_transparent_hash_abort( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 207 | mbedtls_transparent_test_driver_hash_operation_t *operation ) |
| 208 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 209 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 210 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 211 | if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS ) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 212 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 213 | mbedtls_test_driver_hash_hooks.driver_status = |
| 214 | mbedtls_test_driver_hash_hooks.forced_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 215 | } |
| 216 | else |
| 217 | { |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame^] | 218 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 219 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 220 | mbedtls_test_driver_hash_hooks.driver_status = |
Ronald Cron | 7b7854e | 2021-03-13 18:19:08 +0100 | [diff] [blame] | 221 | libtestdriver1_mbedtls_psa_hash_abort( operation ); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 222 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 223 | mbedtls_test_driver_hash_hooks.driver_status = |
| 224 | mbedtls_psa_hash_abort( operation ); |
| 225 | #else |
| 226 | (void) operation; |
| 227 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 228 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 231 | return( mbedtls_test_driver_hash_hooks.driver_status ); |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 232 | } |
| 233 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ |