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 |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 9 | #include "mbedtls/config.h" |
| 10 | #else |
| 11 | #include MBEDTLS_CONFIG_FILE |
| 12 | #endif |
| 13 | |
| 14 | #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST) |
| 15 | #include "psa_crypto_hash.h" |
| 16 | |
| 17 | #include "test/drivers/hash.h" |
| 18 | |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame] | 19 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) |
| 20 | #include "libtestdriver1/library/psa_crypto_hash.h" |
| 21 | #endif |
| 22 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 23 | mbedtls_test_driver_hash_hooks_t |
| 24 | mbedtls_test_driver_hash_hooks = MBEDTLS_TEST_DRIVER_HASH_INIT; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 25 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 26 | psa_status_t mbedtls_test_transparent_hash_compute( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 27 | psa_algorithm_t alg, |
| 28 | const uint8_t *input, size_t input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 29 | uint8_t *hash, size_t hash_size, size_t *hash_length) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 30 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 31 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 32 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 33 | if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) { |
| 34 | mbedtls_test_driver_hash_hooks.driver_status = |
| 35 | mbedtls_test_driver_hash_hooks.forced_status; |
| 36 | } else { |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame] | 37 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 38 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 39 | mbedtls_test_driver_hash_hooks.driver_status = |
Ronald Cron | 7b7854e | 2021-03-13 18:19:08 +0100 | [diff] [blame] | 40 | libtestdriver1_mbedtls_psa_hash_compute( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 41 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 42 | hash, hash_size, hash_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 43 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 44 | mbedtls_test_driver_hash_hooks.driver_status = |
| 45 | mbedtls_psa_hash_compute( |
| 46 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 47 | hash, hash_size, hash_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 48 | #else |
| 49 | (void) alg; |
| 50 | (void) input; |
| 51 | (void) input_length; |
| 52 | (void) hash; |
| 53 | (void) hash_size; |
| 54 | (void) hash_length; |
| 55 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 56 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 59 | return mbedtls_test_driver_hash_hooks.driver_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 62 | psa_status_t mbedtls_test_transparent_hash_setup( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 63 | mbedtls_transparent_test_driver_hash_operation_t *operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 64 | psa_algorithm_t alg) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 65 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 66 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 67 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 68 | if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 69 | mbedtls_test_driver_hash_hooks.driver_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 70 | mbedtls_test_driver_hash_hooks.forced_status; |
| 71 | } else { |
| 72 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 73 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH) |
| 74 | mbedtls_test_driver_hash_hooks.driver_status = |
| 75 | libtestdriver1_mbedtls_psa_hash_setup(operation, alg); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 76 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 77 | mbedtls_test_driver_hash_hooks.driver_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 78 | mbedtls_psa_hash_setup(operation, alg); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 79 | #else |
| 80 | (void) operation; |
| 81 | (void) alg; |
| 82 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 83 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 86 | return mbedtls_test_driver_hash_hooks.driver_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 89 | psa_status_t mbedtls_test_transparent_hash_clone( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 90 | const mbedtls_transparent_test_driver_hash_operation_t *source_operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 91 | mbedtls_transparent_test_driver_hash_operation_t *target_operation) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 92 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 93 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 94 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 95 | if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 96 | mbedtls_test_driver_hash_hooks.driver_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 97 | mbedtls_test_driver_hash_hooks.forced_status; |
| 98 | } else { |
| 99 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 100 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH) |
| 101 | mbedtls_test_driver_hash_hooks.driver_status = |
| 102 | libtestdriver1_mbedtls_psa_hash_clone(source_operation, |
| 103 | target_operation); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 104 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 105 | mbedtls_test_driver_hash_hooks.driver_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 106 | mbedtls_psa_hash_clone(source_operation, target_operation); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 107 | #else |
| 108 | (void) source_operation; |
| 109 | (void) target_operation; |
| 110 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 111 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 114 | return mbedtls_test_driver_hash_hooks.driver_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 117 | psa_status_t mbedtls_test_transparent_hash_update( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 118 | mbedtls_transparent_test_driver_hash_operation_t *operation, |
| 119 | const uint8_t *input, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 120 | size_t input_length) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 121 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 122 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 123 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 124 | if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) { |
| 125 | mbedtls_test_driver_hash_hooks.driver_status = |
| 126 | mbedtls_test_driver_hash_hooks.forced_status; |
| 127 | } else { |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame] | 128 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 129 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 130 | mbedtls_test_driver_hash_hooks.driver_status = |
Ronald Cron | 7b7854e | 2021-03-13 18:19:08 +0100 | [diff] [blame] | 131 | libtestdriver1_mbedtls_psa_hash_update( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 132 | operation, input, input_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 133 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 134 | mbedtls_test_driver_hash_hooks.driver_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 135 | mbedtls_psa_hash_update(operation, input, input_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 136 | #else |
| 137 | (void) operation; |
| 138 | (void) input; |
| 139 | (void) input_length; |
| 140 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 141 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 144 | return mbedtls_test_driver_hash_hooks.driver_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 147 | psa_status_t mbedtls_test_transparent_hash_finish( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 148 | mbedtls_transparent_test_driver_hash_operation_t *operation, |
| 149 | uint8_t *hash, |
| 150 | size_t hash_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 151 | size_t *hash_length) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 152 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 153 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 154 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 155 | if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) { |
| 156 | mbedtls_test_driver_hash_hooks.driver_status = |
| 157 | mbedtls_test_driver_hash_hooks.forced_status; |
| 158 | } else { |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame] | 159 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 160 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 161 | mbedtls_test_driver_hash_hooks.driver_status = |
Ronald Cron | 7b7854e | 2021-03-13 18:19:08 +0100 | [diff] [blame] | 162 | libtestdriver1_mbedtls_psa_hash_finish( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 163 | operation, hash, hash_size, hash_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 164 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 165 | mbedtls_test_driver_hash_hooks.driver_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 166 | mbedtls_psa_hash_finish(operation, hash, hash_size, hash_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 167 | #else |
| 168 | (void) operation; |
| 169 | (void) hash; |
| 170 | (void) hash_size; |
| 171 | (void) hash_length; |
| 172 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 173 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 174 | } |
| 175 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 176 | return mbedtls_test_driver_hash_hooks.driver_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 177 | } |
| 178 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 179 | psa_status_t mbedtls_test_transparent_hash_abort( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 180 | mbedtls_transparent_test_driver_hash_operation_t *operation) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 181 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 182 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 183 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 184 | if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 185 | mbedtls_test_driver_hash_hooks.driver_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 186 | mbedtls_test_driver_hash_hooks.forced_status; |
| 187 | } else { |
| 188 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 189 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH) |
| 190 | mbedtls_test_driver_hash_hooks.driver_status = |
| 191 | libtestdriver1_mbedtls_psa_hash_abort(operation); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 192 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 193 | mbedtls_test_driver_hash_hooks.driver_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 194 | mbedtls_psa_hash_abort(operation); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 195 | #else |
| 196 | (void) operation; |
| 197 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 198 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 201 | return mbedtls_test_driver_hash_hooks.driver_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 202 | } |
| 203 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ |