blob: 44e0e805910d78975e48d84f5afa7260b3df4c36 [file] [log] [blame]
Ronald Cronfa036c82021-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
Mateusz Starzykb4a01292021-05-27 14:49:25 +020020#include <test/helpers.h>
Ronald Cronfa036c82021-03-23 09:33:25 +010021
22#if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST)
23#include "psa_crypto_hash.h"
24
25#include "test/drivers/hash.h"
26
Ronald Cron7975fae2021-09-13 14:50:42 +020027#if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
28#include "libtestdriver1/library/psa_crypto_hash.h"
29#endif
30
Ronald Cron7f13fa22021-04-13 12:41:34 +020031mbedtls_test_driver_hash_hooks_t
32 mbedtls_test_driver_hash_hooks = MBEDTLS_TEST_DRIVER_HASH_INIT;
Ronald Cronfa036c82021-03-23 09:33:25 +010033
Ronald Cron7f13fa22021-04-13 12:41:34 +020034psa_status_t mbedtls_test_transparent_hash_compute(
Ronald Cronfa036c82021-03-23 09:33:25 +010035 psa_algorithm_t alg,
36 const uint8_t *input, size_t input_length,
37 uint8_t *hash, size_t hash_size, size_t *hash_length )
38{
Ronald Cron7f13fa22021-04-13 12:41:34 +020039 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +010040
Ronald Cron7f13fa22021-04-13 12:41:34 +020041 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +010042 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020043 mbedtls_test_driver_hash_hooks.driver_status =
44 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +010045 }
46 else
47 {
Ronald Cron7975fae2021-09-13 14:50:42 +020048#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
49 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
Ronald Cron7f13fa22021-04-13 12:41:34 +020050 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron40170d92021-03-13 18:19:08 +010051 libtestdriver1_mbedtls_psa_hash_compute(
Ronald Cronfa036c82021-03-23 09:33:25 +010052 alg, input, input_length,
53 hash, hash_size, hash_length );
Ronald Cron73c9d9e2021-04-09 11:09:54 +020054#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
55 mbedtls_test_driver_hash_hooks.driver_status =
56 mbedtls_psa_hash_compute(
57 alg, input, input_length,
58 hash, hash_size, hash_length );
59#else
60 (void) alg;
61 (void) input;
62 (void) input_length;
63 (void) hash;
64 (void) hash_size;
65 (void) hash_length;
66 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
67#endif
Ronald Cronfa036c82021-03-23 09:33:25 +010068 }
69
Ronald Cron7f13fa22021-04-13 12:41:34 +020070 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +010071}
72
Ronald Cron7f13fa22021-04-13 12:41:34 +020073psa_status_t mbedtls_test_transparent_hash_setup(
Ronald Cronfa036c82021-03-23 09:33:25 +010074 mbedtls_transparent_test_driver_hash_operation_t *operation,
75 psa_algorithm_t alg )
76{
Ronald Cron7f13fa22021-04-13 12:41:34 +020077 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +010078
Ronald Cron7f13fa22021-04-13 12:41:34 +020079 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +010080 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020081 mbedtls_test_driver_hash_hooks.driver_status =
82 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +010083 }
84 else
85 {
Ronald Cron7975fae2021-09-13 14:50:42 +020086#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
87 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
Ronald Cron7f13fa22021-04-13 12:41:34 +020088 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron40170d92021-03-13 18:19:08 +010089 libtestdriver1_mbedtls_psa_hash_setup( operation, alg );
Ronald Cron73c9d9e2021-04-09 11:09:54 +020090#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
91 mbedtls_test_driver_hash_hooks.driver_status =
92 mbedtls_psa_hash_setup( operation, alg );
93#else
94 (void) operation;
95 (void) alg;
96 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
97#endif
Ronald Cronfa036c82021-03-23 09:33:25 +010098 }
99
Ronald Cron7f13fa22021-04-13 12:41:34 +0200100 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +0100101}
102
Ronald Cron7f13fa22021-04-13 12:41:34 +0200103psa_status_t mbedtls_test_transparent_hash_clone(
Ronald Cronfa036c82021-03-23 09:33:25 +0100104 const mbedtls_transparent_test_driver_hash_operation_t *source_operation,
105 mbedtls_transparent_test_driver_hash_operation_t *target_operation )
106{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200107 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +0100108
Ronald Cron7f13fa22021-04-13 12:41:34 +0200109 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +0100110 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200111 mbedtls_test_driver_hash_hooks.driver_status =
112 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +0100113 }
114 else
115 {
Ronald Cron7975fae2021-09-13 14:50:42 +0200116#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
117 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200118 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron40170d92021-03-13 18:19:08 +0100119 libtestdriver1_mbedtls_psa_hash_clone( source_operation,
120 target_operation );
Ronald Cron73c9d9e2021-04-09 11:09:54 +0200121#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
122 mbedtls_test_driver_hash_hooks.driver_status =
123 mbedtls_psa_hash_clone( source_operation, target_operation );
124#else
125 (void) source_operation;
126 (void) target_operation;
127 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
128#endif
Ronald Cronfa036c82021-03-23 09:33:25 +0100129 }
130
Ronald Cron7f13fa22021-04-13 12:41:34 +0200131 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +0100132}
133
Ronald Cron7f13fa22021-04-13 12:41:34 +0200134psa_status_t mbedtls_test_transparent_hash_update(
Ronald Cronfa036c82021-03-23 09:33:25 +0100135 mbedtls_transparent_test_driver_hash_operation_t *operation,
136 const uint8_t *input,
137 size_t input_length )
138{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200139 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +0100140
Ronald Cron7f13fa22021-04-13 12:41:34 +0200141 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +0100142 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200143 mbedtls_test_driver_hash_hooks.driver_status =
144 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +0100145 }
146 else
147 {
Ronald Cron7975fae2021-09-13 14:50:42 +0200148#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
149 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200150 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron40170d92021-03-13 18:19:08 +0100151 libtestdriver1_mbedtls_psa_hash_update(
Ronald Cronfa036c82021-03-23 09:33:25 +0100152 operation, input, input_length );
Ronald Cron73c9d9e2021-04-09 11:09:54 +0200153#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
154 mbedtls_test_driver_hash_hooks.driver_status =
155 mbedtls_psa_hash_update( operation, input, input_length );
156#else
157 (void) operation;
158 (void) input;
159 (void) input_length;
160 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
161#endif
Ronald Cronfa036c82021-03-23 09:33:25 +0100162 }
163
Ronald Cron7f13fa22021-04-13 12:41:34 +0200164 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +0100165}
166
Ronald Cron7f13fa22021-04-13 12:41:34 +0200167psa_status_t mbedtls_test_transparent_hash_finish(
Ronald Cronfa036c82021-03-23 09:33:25 +0100168 mbedtls_transparent_test_driver_hash_operation_t *operation,
169 uint8_t *hash,
170 size_t hash_size,
171 size_t *hash_length )
172{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200173 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +0100174
Ronald Cron7f13fa22021-04-13 12:41:34 +0200175 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +0100176 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200177 mbedtls_test_driver_hash_hooks.driver_status =
178 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +0100179 }
180 else
181 {
Ronald Cron7975fae2021-09-13 14:50:42 +0200182#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
183 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200184 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron40170d92021-03-13 18:19:08 +0100185 libtestdriver1_mbedtls_psa_hash_finish(
Ronald Cronfa036c82021-03-23 09:33:25 +0100186 operation, hash, hash_size, hash_length );
Ronald Cron73c9d9e2021-04-09 11:09:54 +0200187#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
188 mbedtls_test_driver_hash_hooks.driver_status =
189 mbedtls_psa_hash_finish( operation, hash, hash_size, hash_length );
190#else
191 (void) operation;
192 (void) hash;
193 (void) hash_size;
194 (void) hash_length;
195 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
196#endif
Ronald Cronfa036c82021-03-23 09:33:25 +0100197 }
198
Ronald Cron7f13fa22021-04-13 12:41:34 +0200199 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +0100200}
201
Ronald Cron7f13fa22021-04-13 12:41:34 +0200202psa_status_t mbedtls_test_transparent_hash_abort(
Ronald Cronfa036c82021-03-23 09:33:25 +0100203 mbedtls_transparent_test_driver_hash_operation_t *operation )
204{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200205 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +0100206
Ronald Cron7f13fa22021-04-13 12:41:34 +0200207 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +0100208 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200209 mbedtls_test_driver_hash_hooks.driver_status =
210 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +0100211 }
212 else
213 {
Ronald Cron7975fae2021-09-13 14:50:42 +0200214#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
215 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200216 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron40170d92021-03-13 18:19:08 +0100217 libtestdriver1_mbedtls_psa_hash_abort( operation );
Ronald Cron73c9d9e2021-04-09 11:09:54 +0200218#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
219 mbedtls_test_driver_hash_hooks.driver_status =
220 mbedtls_psa_hash_abort( operation );
221#else
222 (void) operation;
223 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
224#endif
Ronald Cronfa036c82021-03-23 09:33:25 +0100225 }
226
Ronald Cron7f13fa22021-04-13 12:41:34 +0200227 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +0100228}
229#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */