blob: e618660968ccb7dc066608c29fe30a2b3d7442da [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 Cronc4bc12e2021-04-13 12:41:34 +020031mbedtls_test_driver_hash_hooks_t
32 mbedtls_test_driver_hash_hooks = MBEDTLS_TEST_DRIVER_HASH_INIT;
Ronald Cron0bec41a2021-03-23 09:33:25 +010033
Ronald Cronc4bc12e2021-04-13 12:41:34 +020034psa_status_t mbedtls_test_transparent_hash_compute(
Ronald Cron0bec41a2021-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 Cronc4bc12e2021-04-13 12:41:34 +020039 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +010040
Ronald Cronc4bc12e2021-04-13 12:41:34 +020041 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cron0bec41a2021-03-23 09:33:25 +010042 {
Ronald Cronc4bc12e2021-04-13 12:41:34 +020043 mbedtls_test_driver_hash_hooks.driver_status =
44 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +010045 }
46 else
47 {
Ronald Cron2091eed2021-04-09 11:09:54 +020048#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
Ronald Cronc4bc12e2021-04-13 12:41:34 +020049 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron0bec41a2021-03-23 09:33:25 +010050 mbedtls_transparent_test_driver_hash_compute(
51 alg, input, input_length,
52 hash, hash_size, hash_length );
Ronald Cron2091eed2021-04-09 11:09:54 +020053#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
54 mbedtls_test_driver_hash_hooks.driver_status =
55 mbedtls_psa_hash_compute(
56 alg, input, input_length,
57 hash, hash_size, hash_length );
58#else
59 (void) alg;
60 (void) input;
61 (void) input_length;
62 (void) hash;
63 (void) hash_size;
64 (void) hash_length;
65 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
66#endif
Ronald Cron0bec41a2021-03-23 09:33:25 +010067 }
68
Ronald Cronc4bc12e2021-04-13 12:41:34 +020069 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cron0bec41a2021-03-23 09:33:25 +010070}
71
Ronald Cronc4bc12e2021-04-13 12:41:34 +020072psa_status_t mbedtls_test_transparent_hash_setup(
Ronald Cron0bec41a2021-03-23 09:33:25 +010073 mbedtls_transparent_test_driver_hash_operation_t *operation,
74 psa_algorithm_t alg )
75{
Ronald Cronc4bc12e2021-04-13 12:41:34 +020076 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +010077
Ronald Cronc4bc12e2021-04-13 12:41:34 +020078 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cron0bec41a2021-03-23 09:33:25 +010079 {
Ronald Cronc4bc12e2021-04-13 12:41:34 +020080 mbedtls_test_driver_hash_hooks.driver_status =
81 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +010082 }
83 else
84 {
Ronald Cron2091eed2021-04-09 11:09:54 +020085#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
Ronald Cronc4bc12e2021-04-13 12:41:34 +020086 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron0bec41a2021-03-23 09:33:25 +010087 mbedtls_transparent_test_driver_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 =
90 mbedtls_psa_hash_setup( operation, alg );
91#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
Ronald Cronc4bc12e2021-04-13 12:41:34 +020098 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,
103 mbedtls_transparent_test_driver_hash_operation_t *target_operation )
104{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200105 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100106
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200107 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cron0bec41a2021-03-23 09:33:25 +0100108 {
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200109 mbedtls_test_driver_hash_hooks.driver_status =
110 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100111 }
112 else
113 {
Ronald Cron2091eed2021-04-09 11:09:54 +0200114#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200115 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron0bec41a2021-03-23 09:33:25 +0100116 mbedtls_transparent_test_driver_hash_clone( source_operation,
117 target_operation );
Ronald Cron2091eed2021-04-09 11:09:54 +0200118#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
119 mbedtls_test_driver_hash_hooks.driver_status =
120 mbedtls_psa_hash_clone( source_operation, target_operation );
121#else
122 (void) source_operation;
123 (void) target_operation;
124 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
125#endif
Ronald Cron0bec41a2021-03-23 09:33:25 +0100126 }
127
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200128 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cron0bec41a2021-03-23 09:33:25 +0100129}
130
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200131psa_status_t mbedtls_test_transparent_hash_update(
Ronald Cron0bec41a2021-03-23 09:33:25 +0100132 mbedtls_transparent_test_driver_hash_operation_t *operation,
133 const uint8_t *input,
134 size_t input_length )
135{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200136 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100137
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200138 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cron0bec41a2021-03-23 09:33:25 +0100139 {
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200140 mbedtls_test_driver_hash_hooks.driver_status =
141 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100142 }
143 else
144 {
Ronald Cron2091eed2021-04-09 11:09:54 +0200145#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200146 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron0bec41a2021-03-23 09:33:25 +0100147 mbedtls_transparent_test_driver_hash_update(
148 operation, input, input_length );
Ronald Cron2091eed2021-04-09 11:09:54 +0200149#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
150 mbedtls_test_driver_hash_hooks.driver_status =
151 mbedtls_psa_hash_update( operation, input, input_length );
152#else
153 (void) operation;
154 (void) input;
155 (void) input_length;
156 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
157#endif
Ronald Cron0bec41a2021-03-23 09:33:25 +0100158 }
159
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200160 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cron0bec41a2021-03-23 09:33:25 +0100161}
162
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200163psa_status_t mbedtls_test_transparent_hash_finish(
Ronald Cron0bec41a2021-03-23 09:33:25 +0100164 mbedtls_transparent_test_driver_hash_operation_t *operation,
165 uint8_t *hash,
166 size_t hash_size,
167 size_t *hash_length )
168{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200169 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100170
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200171 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cron0bec41a2021-03-23 09:33:25 +0100172 {
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200173 mbedtls_test_driver_hash_hooks.driver_status =
174 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100175 }
176 else
177 {
Ronald Cron2091eed2021-04-09 11:09:54 +0200178#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200179 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron0bec41a2021-03-23 09:33:25 +0100180 mbedtls_transparent_test_driver_hash_finish(
181 operation, hash, hash_size, hash_length );
Ronald Cron2091eed2021-04-09 11:09:54 +0200182#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
183 mbedtls_test_driver_hash_hooks.driver_status =
184 mbedtls_psa_hash_finish( operation, hash, hash_size, hash_length );
185#else
186 (void) operation;
187 (void) hash;
188 (void) hash_size;
189 (void) hash_length;
190 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
191#endif
Ronald Cron0bec41a2021-03-23 09:33:25 +0100192 }
193
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200194 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cron0bec41a2021-03-23 09:33:25 +0100195}
196
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200197psa_status_t mbedtls_test_transparent_hash_abort(
Ronald Cron0bec41a2021-03-23 09:33:25 +0100198 mbedtls_transparent_test_driver_hash_operation_t *operation )
199{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200200 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100201
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200202 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cron0bec41a2021-03-23 09:33:25 +0100203 {
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200204 mbedtls_test_driver_hash_hooks.driver_status =
205 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100206 }
207 else
208 {
Ronald Cron2091eed2021-04-09 11:09:54 +0200209#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200210 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron0bec41a2021-03-23 09:33:25 +0100211 mbedtls_transparent_test_driver_hash_abort( operation );
Ronald Cron2091eed2021-04-09 11:09:54 +0200212#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
213 mbedtls_test_driver_hash_hooks.driver_status =
214 mbedtls_psa_hash_abort( operation );
215#else
216 (void) operation;
217 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
218#endif
Ronald Cron0bec41a2021-03-23 09:33:25 +0100219 }
220
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200221 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cron0bec41a2021-03-23 09:33:25 +0100222}
223#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */