blob: 54aec932253f15ff66ba5aa7054504740ecc2061 [file] [log] [blame]
Minos Galanakis2c824b42025-03-20 09:28:45 +00001/*
2 * Test driver for hash entry points.
3 */
4/* Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
7
8#include <test/helpers.h>
9
10#if defined(PSA_CRYPTO_DRIVER_TEST)
11#include "psa_crypto_hash.h"
12
13#include "test/drivers/hash.h"
14
15#if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
16#if MBEDTLS_VERSION_MAJOR < 4
17#include "libtestdriver1/library/psa_crypto_hash.h"
18#else
19#include "libtestdriver1/tf-psa-crypto/drivers/builtin/src/psa_crypto_hash.h"
20#endif
21#endif
22
23mbedtls_test_driver_hash_hooks_t
24 mbedtls_test_driver_hash_hooks = MBEDTLS_TEST_DRIVER_HASH_INIT;
25
26psa_status_t mbedtls_test_transparent_hash_compute(
27 psa_algorithm_t alg,
28 const uint8_t *input, size_t input_length,
29 uint8_t *hash, size_t hash_size, size_t *hash_length)
30{
31 mbedtls_test_driver_hash_hooks.hits++;
32
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 {
37#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
38 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
39 mbedtls_test_driver_hash_hooks.driver_status =
40 libtestdriver1_mbedtls_psa_hash_compute(
41 alg, input, input_length,
42 hash, hash_size, hash_length);
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,
47 hash, hash_size, hash_length);
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
57 }
58
59 return mbedtls_test_driver_hash_hooks.driver_status;
60}
61
62psa_status_t mbedtls_test_transparent_hash_setup(
63 mbedtls_transparent_test_driver_hash_operation_t *operation,
64 psa_algorithm_t alg)
65{
66 mbedtls_test_driver_hash_hooks.hits++;
67
68 if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) {
69 mbedtls_test_driver_hash_hooks.driver_status =
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);
76#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
77 mbedtls_test_driver_hash_hooks.driver_status =
78 mbedtls_psa_hash_setup(operation, alg);
79#else
80 (void) operation;
81 (void) alg;
82 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
83#endif
84 }
85
86 return mbedtls_test_driver_hash_hooks.driver_status;
87}
88
89psa_status_t mbedtls_test_transparent_hash_clone(
90 const mbedtls_transparent_test_driver_hash_operation_t *source_operation,
91 mbedtls_transparent_test_driver_hash_operation_t *target_operation)
92{
93 mbedtls_test_driver_hash_hooks.hits++;
94
95 if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) {
96 mbedtls_test_driver_hash_hooks.driver_status =
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);
104#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
105 mbedtls_test_driver_hash_hooks.driver_status =
106 mbedtls_psa_hash_clone(source_operation, target_operation);
107#else
108 (void) source_operation;
109 (void) target_operation;
110 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
111#endif
112 }
113
114 return mbedtls_test_driver_hash_hooks.driver_status;
115}
116
117psa_status_t mbedtls_test_transparent_hash_update(
118 mbedtls_transparent_test_driver_hash_operation_t *operation,
119 const uint8_t *input,
120 size_t input_length)
121{
122 mbedtls_test_driver_hash_hooks.hits++;
123
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 {
128#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
129 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
130 mbedtls_test_driver_hash_hooks.driver_status =
131 libtestdriver1_mbedtls_psa_hash_update(
132 operation, input, input_length);
133#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
134 mbedtls_test_driver_hash_hooks.driver_status =
135 mbedtls_psa_hash_update(operation, input, input_length);
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
142 }
143
144 return mbedtls_test_driver_hash_hooks.driver_status;
145}
146
147psa_status_t mbedtls_test_transparent_hash_finish(
148 mbedtls_transparent_test_driver_hash_operation_t *operation,
149 uint8_t *hash,
150 size_t hash_size,
151 size_t *hash_length)
152{
153 mbedtls_test_driver_hash_hooks.hits++;
154
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 {
159#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
160 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
161 mbedtls_test_driver_hash_hooks.driver_status =
162 libtestdriver1_mbedtls_psa_hash_finish(
163 operation, hash, hash_size, hash_length);
164#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
165 mbedtls_test_driver_hash_hooks.driver_status =
166 mbedtls_psa_hash_finish(operation, hash, hash_size, hash_length);
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
174 }
175
176 return mbedtls_test_driver_hash_hooks.driver_status;
177}
178
179psa_status_t mbedtls_test_transparent_hash_abort(
180 mbedtls_transparent_test_driver_hash_operation_t *operation)
181{
182 mbedtls_test_driver_hash_hooks.hits++;
183
184 if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) {
185 mbedtls_test_driver_hash_hooks.driver_status =
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);
192#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
193 mbedtls_test_driver_hash_hooks.driver_status =
194 mbedtls_psa_hash_abort(operation);
195#else
196 (void) operation;
197 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
198#endif
199 }
200
201 return mbedtls_test_driver_hash_hooks.driver_status;
202}
203#endif /* PSA_CRYPTO_DRIVER_TEST */