blob: e03e7f06bb37c412fd9ebe4a9c17b1b04c96994f [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Ronald Cron0bec41a2021-03-23 09:33:25 +01006 */
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 Cronb814bda2021-09-13 14:50:42 +020019#if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
20#include "libtestdriver1/library/psa_crypto_hash.h"
21#endif
22
Ronald Cronc4bc12e2021-04-13 12:41:34 +020023mbedtls_test_driver_hash_hooks_t
24 mbedtls_test_driver_hash_hooks = MBEDTLS_TEST_DRIVER_HASH_INIT;
Ronald Cron0bec41a2021-03-23 09:33:25 +010025
Ronald Cronc4bc12e2021-04-13 12:41:34 +020026psa_status_t mbedtls_test_transparent_hash_compute(
Ronald Cron0bec41a2021-03-23 09:33:25 +010027 psa_algorithm_t alg,
28 const uint8_t *input, size_t input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010029 uint8_t *hash, size_t hash_size, size_t *hash_length)
Ronald Cron0bec41a2021-03-23 09:33:25 +010030{
Ronald Cronc4bc12e2021-04-13 12:41:34 +020031 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +010032
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010033 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 Cronb814bda2021-09-13 14:50:42 +020037#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010038 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
Ronald Cronc4bc12e2021-04-13 12:41:34 +020039 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron7b7854e2021-03-13 18:19:08 +010040 libtestdriver1_mbedtls_psa_hash_compute(
Ronald Cron0bec41a2021-03-23 09:33:25 +010041 alg, input, input_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010042 hash, hash_size, hash_length);
Ronald Cron2091eed2021-04-09 11:09:54 +020043#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 Peskine1b6c09a2023-01-11 14:52:35 +010047 hash, hash_size, hash_length);
Ronald Cron2091eed2021-04-09 11:09:54 +020048#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 Cron0bec41a2021-03-23 09:33:25 +010057 }
58
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010059 return mbedtls_test_driver_hash_hooks.driver_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +010060}
61
Ronald Cronc4bc12e2021-04-13 12:41:34 +020062psa_status_t mbedtls_test_transparent_hash_setup(
Ronald Cron0bec41a2021-03-23 09:33:25 +010063 mbedtls_transparent_test_driver_hash_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064 psa_algorithm_t alg)
Ronald Cron0bec41a2021-03-23 09:33:25 +010065{
Ronald Cronc4bc12e2021-04-13 12:41:34 +020066 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +010067
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010068 if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) {
Ronald Cronc4bc12e2021-04-13 12:41:34 +020069 mbedtls_test_driver_hash_hooks.driver_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010070 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 Cron2091eed2021-04-09 11:09:54 +020076#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
77 mbedtls_test_driver_hash_hooks.driver_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010078 mbedtls_psa_hash_setup(operation, alg);
Ronald Cron2091eed2021-04-09 11:09:54 +020079#else
80 (void) operation;
81 (void) alg;
82 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
83#endif
Ronald Cron0bec41a2021-03-23 09:33:25 +010084 }
85
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 return mbedtls_test_driver_hash_hooks.driver_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +010087}
88
Ronald Cronc4bc12e2021-04-13 12:41:34 +020089psa_status_t mbedtls_test_transparent_hash_clone(
Ronald Cron0bec41a2021-03-23 09:33:25 +010090 const mbedtls_transparent_test_driver_hash_operation_t *source_operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 mbedtls_transparent_test_driver_hash_operation_t *target_operation)
Ronald Cron0bec41a2021-03-23 09:33:25 +010092{
Ronald Cronc4bc12e2021-04-13 12:41:34 +020093 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +010094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095 if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) {
Ronald Cronc4bc12e2021-04-13 12:41:34 +020096 mbedtls_test_driver_hash_hooks.driver_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097 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 Cron2091eed2021-04-09 11:09:54 +0200104#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
105 mbedtls_test_driver_hash_hooks.driver_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 mbedtls_psa_hash_clone(source_operation, target_operation);
Ronald Cron2091eed2021-04-09 11:09:54 +0200107#else
108 (void) source_operation;
109 (void) target_operation;
110 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
111#endif
Ronald Cron0bec41a2021-03-23 09:33:25 +0100112 }
113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114 return mbedtls_test_driver_hash_hooks.driver_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100115}
116
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200117psa_status_t mbedtls_test_transparent_hash_update(
Ronald Cron0bec41a2021-03-23 09:33:25 +0100118 mbedtls_transparent_test_driver_hash_operation_t *operation,
119 const uint8_t *input,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 size_t input_length)
Ronald Cron0bec41a2021-03-23 09:33:25 +0100121{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200122 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 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 Cronb814bda2021-09-13 14:50:42 +0200128#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100129 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200130 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron7b7854e2021-03-13 18:19:08 +0100131 libtestdriver1_mbedtls_psa_hash_update(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 operation, input, input_length);
Ronald Cron2091eed2021-04-09 11:09:54 +0200133#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
134 mbedtls_test_driver_hash_hooks.driver_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 mbedtls_psa_hash_update(operation, input, input_length);
Ronald Cron2091eed2021-04-09 11:09:54 +0200136#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 Cron0bec41a2021-03-23 09:33:25 +0100142 }
143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 return mbedtls_test_driver_hash_hooks.driver_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100145}
146
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200147psa_status_t mbedtls_test_transparent_hash_finish(
Ronald Cron0bec41a2021-03-23 09:33:25 +0100148 mbedtls_transparent_test_driver_hash_operation_t *operation,
149 uint8_t *hash,
150 size_t hash_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 size_t *hash_length)
Ronald Cron0bec41a2021-03-23 09:33:25 +0100152{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200153 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 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 Cronb814bda2021-09-13 14:50:42 +0200159#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200161 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron7b7854e2021-03-13 18:19:08 +0100162 libtestdriver1_mbedtls_psa_hash_finish(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163 operation, hash, hash_size, hash_length);
Ronald Cron2091eed2021-04-09 11:09:54 +0200164#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
165 mbedtls_test_driver_hash_hooks.driver_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166 mbedtls_psa_hash_finish(operation, hash, hash_size, hash_length);
Ronald Cron2091eed2021-04-09 11:09:54 +0200167#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 Cron0bec41a2021-03-23 09:33:25 +0100174 }
175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176 return mbedtls_test_driver_hash_hooks.driver_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100177}
178
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200179psa_status_t mbedtls_test_transparent_hash_abort(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180 mbedtls_transparent_test_driver_hash_operation_t *operation)
Ronald Cron0bec41a2021-03-23 09:33:25 +0100181{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200182 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100183
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) {
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200185 mbedtls_test_driver_hash_hooks.driver_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 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 Cron2091eed2021-04-09 11:09:54 +0200192#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
193 mbedtls_test_driver_hash_hooks.driver_status =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 mbedtls_psa_hash_abort(operation);
Ronald Cron2091eed2021-04-09 11:09:54 +0200195#else
196 (void) operation;
197 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
198#endif
Ronald Cron0bec41a2021-03-23 09:33:25 +0100199 }
200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100201 return mbedtls_test_driver_hash_hooks.driver_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100202}
203#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */