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, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 41 | uint8_t *hash, size_t hash_size, size_t *hash_length) |
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.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 44 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 45 | if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) { |
| 46 | mbedtls_test_driver_hash_hooks.driver_status = |
| 47 | mbedtls_test_driver_hash_hooks.forced_status; |
| 48 | } else { |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame] | 49 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 50 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 51 | mbedtls_test_driver_hash_hooks.driver_status = |
Ronald Cron | 7b7854e | 2021-03-13 18:19:08 +0100 | [diff] [blame] | 52 | libtestdriver1_mbedtls_psa_hash_compute( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 53 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 54 | hash, hash_size, hash_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 55 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 56 | mbedtls_test_driver_hash_hooks.driver_status = |
| 57 | mbedtls_psa_hash_compute( |
| 58 | alg, input, input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 59 | hash, hash_size, hash_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 60 | #else |
| 61 | (void) alg; |
| 62 | (void) input; |
| 63 | (void) input_length; |
| 64 | (void) hash; |
| 65 | (void) hash_size; |
| 66 | (void) hash_length; |
| 67 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 68 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 69 | } |
| 70 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 71 | return mbedtls_test_driver_hash_hooks.driver_status; |
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 | psa_status_t mbedtls_test_transparent_hash_setup( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 75 | mbedtls_transparent_test_driver_hash_operation_t *operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 76 | psa_algorithm_t alg) |
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 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 79 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 80 | if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 81 | mbedtls_test_driver_hash_hooks.driver_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 82 | mbedtls_test_driver_hash_hooks.forced_status; |
| 83 | } else { |
| 84 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 85 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH) |
| 86 | mbedtls_test_driver_hash_hooks.driver_status = |
| 87 | libtestdriver1_mbedtls_psa_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 = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 90 | mbedtls_psa_hash_setup(operation, alg); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 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 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [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, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 103 | mbedtls_transparent_test_driver_hash_operation_t *target_operation) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 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 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 107 | if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 108 | mbedtls_test_driver_hash_hooks.driver_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 109 | mbedtls_test_driver_hash_hooks.forced_status; |
| 110 | } else { |
| 111 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 112 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH) |
| 113 | mbedtls_test_driver_hash_hooks.driver_status = |
| 114 | libtestdriver1_mbedtls_psa_hash_clone(source_operation, |
| 115 | target_operation); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 116 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 117 | mbedtls_test_driver_hash_hooks.driver_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 118 | mbedtls_psa_hash_clone(source_operation, target_operation); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 119 | #else |
| 120 | (void) source_operation; |
| 121 | (void) target_operation; |
| 122 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 123 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 126 | return mbedtls_test_driver_hash_hooks.driver_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 129 | psa_status_t mbedtls_test_transparent_hash_update( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 130 | mbedtls_transparent_test_driver_hash_operation_t *operation, |
| 131 | const uint8_t *input, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 132 | size_t input_length) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 133 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 134 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 135 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 136 | if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) { |
| 137 | mbedtls_test_driver_hash_hooks.driver_status = |
| 138 | mbedtls_test_driver_hash_hooks.forced_status; |
| 139 | } else { |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame] | 140 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 141 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 142 | mbedtls_test_driver_hash_hooks.driver_status = |
Ronald Cron | 7b7854e | 2021-03-13 18:19:08 +0100 | [diff] [blame] | 143 | libtestdriver1_mbedtls_psa_hash_update( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 144 | operation, input, input_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 145 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 146 | mbedtls_test_driver_hash_hooks.driver_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 147 | mbedtls_psa_hash_update(operation, input, input_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 148 | #else |
| 149 | (void) operation; |
| 150 | (void) input; |
| 151 | (void) input_length; |
| 152 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 153 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 156 | return mbedtls_test_driver_hash_hooks.driver_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 159 | psa_status_t mbedtls_test_transparent_hash_finish( |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 160 | mbedtls_transparent_test_driver_hash_operation_t *operation, |
| 161 | uint8_t *hash, |
| 162 | size_t hash_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 163 | size_t *hash_length) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 164 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 165 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 166 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 167 | if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) { |
| 168 | mbedtls_test_driver_hash_hooks.driver_status = |
| 169 | mbedtls_test_driver_hash_hooks.forced_status; |
| 170 | } else { |
Ronald Cron | b814bda | 2021-09-13 14:50:42 +0200 | [diff] [blame] | 171 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 172 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH) |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 173 | mbedtls_test_driver_hash_hooks.driver_status = |
Ronald Cron | 7b7854e | 2021-03-13 18:19:08 +0100 | [diff] [blame] | 174 | libtestdriver1_mbedtls_psa_hash_finish( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 175 | operation, hash, hash_size, hash_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 176 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 177 | mbedtls_test_driver_hash_hooks.driver_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 178 | mbedtls_psa_hash_finish(operation, hash, hash_size, hash_length); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 179 | #else |
| 180 | (void) operation; |
| 181 | (void) hash; |
| 182 | (void) hash_size; |
| 183 | (void) hash_length; |
| 184 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 185 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 188 | return mbedtls_test_driver_hash_hooks.driver_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 189 | } |
| 190 | |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 191 | psa_status_t mbedtls_test_transparent_hash_abort( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 192 | mbedtls_transparent_test_driver_hash_operation_t *operation) |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 193 | { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 194 | mbedtls_test_driver_hash_hooks.hits++; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 195 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 196 | if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) { |
Ronald Cron | c4bc12e | 2021-04-13 12:41:34 +0200 | [diff] [blame] | 197 | mbedtls_test_driver_hash_hooks.driver_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 198 | mbedtls_test_driver_hash_hooks.forced_status; |
| 199 | } else { |
| 200 | #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \ |
| 201 | defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH) |
| 202 | mbedtls_test_driver_hash_hooks.driver_status = |
| 203 | libtestdriver1_mbedtls_psa_hash_abort(operation); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 204 | #elif defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 205 | mbedtls_test_driver_hash_hooks.driver_status = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 206 | mbedtls_psa_hash_abort(operation); |
Ronald Cron | 2091eed | 2021-04-09 11:09:54 +0200 | [diff] [blame] | 207 | #else |
| 208 | (void) operation; |
| 209 | mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED; |
| 210 | #endif |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 213 | return mbedtls_test_driver_hash_hooks.driver_status; |
Ronald Cron | 0bec41a | 2021-03-23 09:33:25 +0100 | [diff] [blame] | 214 | } |
| 215 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ |