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