blob: 9cfb707697362c5d5a9ffaf4c872cd83fd3ef73d [file] [log] [blame]
Ronald Cron0bec41a2021-03-23 09:33:25 +01001/*
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 Cronb814bda2021-09-13 14:50:42 +020031#if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
32#include "libtestdriver1/library/psa_crypto_hash.h"
33#endif
34
Ronald Cronc4bc12e2021-04-13 12:41:34 +020035mbedtls_test_driver_hash_hooks_t
36 mbedtls_test_driver_hash_hooks = MBEDTLS_TEST_DRIVER_HASH_INIT;
Ronald Cron0bec41a2021-03-23 09:33:25 +010037
Ronald Cronc4bc12e2021-04-13 12:41:34 +020038psa_status_t mbedtls_test_transparent_hash_compute(
Ronald Cron0bec41a2021-03-23 09:33:25 +010039 psa_algorithm_t alg,
40 const uint8_t *input, size_t input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010041 uint8_t *hash, size_t hash_size, size_t *hash_length)
Ronald Cron0bec41a2021-03-23 09:33:25 +010042{
Ronald Cronc4bc12e2021-04-13 12:41:34 +020043 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +010044
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045 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 Cronb814bda2021-09-13 14:50:42 +020049#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010050 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
Ronald Cronc4bc12e2021-04-13 12:41:34 +020051 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron7b7854e2021-03-13 18:19:08 +010052 libtestdriver1_mbedtls_psa_hash_compute(
Ronald Cron0bec41a2021-03-23 09:33:25 +010053 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010054 hash, hash_size, hash_length);
Ronald Cron2091eed2021-04-09 11:09:54 +020055#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 Peskine1b6c09a2023-01-11 14:52:35 +010059 hash, hash_size, hash_length);
Ronald Cron2091eed2021-04-09 11:09:54 +020060#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 Cron0bec41a2021-03-23 09:33:25 +010069 }
70
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071 return mbedtls_test_driver_hash_hooks.driver_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +010072}
73
Ronald Cronc4bc12e2021-04-13 12:41:34 +020074psa_status_t mbedtls_test_transparent_hash_setup(
Ronald Cron0bec41a2021-03-23 09:33:25 +010075 mbedtls_transparent_test_driver_hash_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 psa_algorithm_t alg)
Ronald Cron0bec41a2021-03-23 09:33:25 +010077{
Ronald Cronc4bc12e2021-04-13 12:41:34 +020078 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +010079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) {
Ronald Cronc4bc12e2021-04-13 12:41:34 +020081 mbedtls_test_driver_hash_hooks.driver_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082 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 Cron2091eed2021-04-09 11:09:54 +020088#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
89 mbedtls_test_driver_hash_hooks.driver_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 mbedtls_psa_hash_setup(operation, alg);
Ronald Cron2091eed2021-04-09 11:09:54 +020091#else
92 (void) operation;
93 (void) alg;
94 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
95#endif
Ronald Cron0bec41a2021-03-23 09:33:25 +010096 }
97
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 return mbedtls_test_driver_hash_hooks.driver_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +010099}
100
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200101psa_status_t mbedtls_test_transparent_hash_clone(
Ronald Cron0bec41a2021-03-23 09:33:25 +0100102 const mbedtls_transparent_test_driver_hash_operation_t *source_operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 mbedtls_transparent_test_driver_hash_operation_t *target_operation)
Ronald Cron0bec41a2021-03-23 09:33:25 +0100104{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200105 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107 if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) {
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200108 mbedtls_test_driver_hash_hooks.driver_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 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 Cron2091eed2021-04-09 11:09:54 +0200116#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
117 mbedtls_test_driver_hash_hooks.driver_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 mbedtls_psa_hash_clone(source_operation, target_operation);
Ronald Cron2091eed2021-04-09 11:09:54 +0200119#else
120 (void) source_operation;
121 (void) target_operation;
122 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
123#endif
Ronald Cron0bec41a2021-03-23 09:33:25 +0100124 }
125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 return mbedtls_test_driver_hash_hooks.driver_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100127}
128
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200129psa_status_t mbedtls_test_transparent_hash_update(
Ronald Cron0bec41a2021-03-23 09:33:25 +0100130 mbedtls_transparent_test_driver_hash_operation_t *operation,
131 const uint8_t *input,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 size_t input_length)
Ronald Cron0bec41a2021-03-23 09:33:25 +0100133{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200134 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 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 Cronb814bda2021-09-13 14:50:42 +0200140#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200142 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron7b7854e2021-03-13 18:19:08 +0100143 libtestdriver1_mbedtls_psa_hash_update(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 operation, input, input_length);
Ronald Cron2091eed2021-04-09 11:09:54 +0200145#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
146 mbedtls_test_driver_hash_hooks.driver_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 mbedtls_psa_hash_update(operation, input, input_length);
Ronald Cron2091eed2021-04-09 11:09:54 +0200148#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 Cron0bec41a2021-03-23 09:33:25 +0100154 }
155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 return mbedtls_test_driver_hash_hooks.driver_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100157}
158
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200159psa_status_t mbedtls_test_transparent_hash_finish(
Ronald Cron0bec41a2021-03-23 09:33:25 +0100160 mbedtls_transparent_test_driver_hash_operation_t *operation,
161 uint8_t *hash,
162 size_t hash_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163 size_t *hash_length)
Ronald Cron0bec41a2021-03-23 09:33:25 +0100164{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200165 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 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 Cronb814bda2021-09-13 14:50:42 +0200171#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200173 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron7b7854e2021-03-13 18:19:08 +0100174 libtestdriver1_mbedtls_psa_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175 operation, hash, hash_size, hash_length);
Ronald Cron2091eed2021-04-09 11:09:54 +0200176#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
177 mbedtls_test_driver_hash_hooks.driver_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 mbedtls_psa_hash_finish(operation, hash, hash_size, hash_length);
Ronald Cron2091eed2021-04-09 11:09:54 +0200179#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 Cron0bec41a2021-03-23 09:33:25 +0100186 }
187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100188 return mbedtls_test_driver_hash_hooks.driver_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100189}
190
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200191psa_status_t mbedtls_test_transparent_hash_abort(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 mbedtls_transparent_test_driver_hash_operation_t *operation)
Ronald Cron0bec41a2021-03-23 09:33:25 +0100193{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200194 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) {
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200197 mbedtls_test_driver_hash_hooks.driver_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198 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 Cron2091eed2021-04-09 11:09:54 +0200204#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
205 mbedtls_test_driver_hash_hooks.driver_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 mbedtls_psa_hash_abort(operation);
Ronald Cron2091eed2021-04-09 11:09:54 +0200207#else
208 (void) operation;
209 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
210#endif
Ronald Cron0bec41a2021-03-23 09:33:25 +0100211 }
212
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213 return mbedtls_test_driver_hash_hooks.driver_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100214}
215#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */