blob: 0d59bee405d4d5c4afb6c263fea071f8115091d7 [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 Cronb814bda2021-09-13 14:50:42 +020031#if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
32#include "libtestdriver1/library/psa_crypto_hash.h"
33#endif
34
Ronald Cronc4bc12e2021-04-13 12:41:34 +020035mbedtls_test_driver_hash_hooks_t
36 mbedtls_test_driver_hash_hooks = MBEDTLS_TEST_DRIVER_HASH_INIT;
Ronald Cron0bec41a2021-03-23 09:33:25 +010037
Ronald Cronc4bc12e2021-04-13 12:41:34 +020038psa_status_t mbedtls_test_transparent_hash_compute(
Ronald Cron0bec41a2021-03-23 09:33:25 +010039 psa_algorithm_t alg,
40 const uint8_t *input, size_t input_length,
41 uint8_t *hash, size_t hash_size, size_t *hash_length )
42{
Ronald Cronc4bc12e2021-04-13 12:41:34 +020043 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +010044
Ronald Cronc4bc12e2021-04-13 12:41:34 +020045 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cron0bec41a2021-03-23 09:33:25 +010046 {
Ronald Cronc4bc12e2021-04-13 12:41:34 +020047 mbedtls_test_driver_hash_hooks.driver_status =
48 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +010049 }
50 else
51 {
Ronald Cronb814bda2021-09-13 14:50:42 +020052#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
53 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
Ronald Cronc4bc12e2021-04-13 12:41:34 +020054 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron7b7854e2021-03-13 18:19:08 +010055 libtestdriver1_mbedtls_psa_hash_compute(
Ronald Cron0bec41a2021-03-23 09:33:25 +010056 alg, input, input_length,
57 hash, hash_size, hash_length );
Ronald Cron2091eed2021-04-09 11:09:54 +020058#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
59 mbedtls_test_driver_hash_hooks.driver_status =
60 mbedtls_psa_hash_compute(
61 alg, input, input_length,
62 hash, hash_size, hash_length );
63#else
64 (void) alg;
65 (void) input;
66 (void) input_length;
67 (void) hash;
68 (void) hash_size;
69 (void) hash_length;
70 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
71#endif
Ronald Cron0bec41a2021-03-23 09:33:25 +010072 }
73
Ronald Cronc4bc12e2021-04-13 12:41:34 +020074 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cron0bec41a2021-03-23 09:33:25 +010075}
76
Ronald Cronc4bc12e2021-04-13 12:41:34 +020077psa_status_t mbedtls_test_transparent_hash_setup(
Ronald Cron0bec41a2021-03-23 09:33:25 +010078 mbedtls_transparent_test_driver_hash_operation_t *operation,
79 psa_algorithm_t alg )
80{
Ronald Cronc4bc12e2021-04-13 12:41:34 +020081 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +010082
Ronald Cronc4bc12e2021-04-13 12:41:34 +020083 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cron0bec41a2021-03-23 09:33:25 +010084 {
Ronald Cronc4bc12e2021-04-13 12:41:34 +020085 mbedtls_test_driver_hash_hooks.driver_status =
86 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +010087 }
88 else
89 {
Ronald Cronb814bda2021-09-13 14:50:42 +020090#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
91 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
Ronald Cronc4bc12e2021-04-13 12:41:34 +020092 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron7b7854e2021-03-13 18:19:08 +010093 libtestdriver1_mbedtls_psa_hash_setup( operation, alg );
Ronald Cron2091eed2021-04-09 11:09:54 +020094#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
95 mbedtls_test_driver_hash_hooks.driver_status =
96 mbedtls_psa_hash_setup( operation, alg );
97#else
98 (void) operation;
99 (void) alg;
100 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
101#endif
Ronald Cron0bec41a2021-03-23 09:33:25 +0100102 }
103
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200104 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cron0bec41a2021-03-23 09:33:25 +0100105}
106
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200107psa_status_t mbedtls_test_transparent_hash_clone(
Ronald Cron0bec41a2021-03-23 09:33:25 +0100108 const mbedtls_transparent_test_driver_hash_operation_t *source_operation,
109 mbedtls_transparent_test_driver_hash_operation_t *target_operation )
110{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200111 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100112
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200113 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cron0bec41a2021-03-23 09:33:25 +0100114 {
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200115 mbedtls_test_driver_hash_hooks.driver_status =
116 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100117 }
118 else
119 {
Ronald Cronb814bda2021-09-13 14:50:42 +0200120#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
121 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200122 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron7b7854e2021-03-13 18:19:08 +0100123 libtestdriver1_mbedtls_psa_hash_clone( source_operation,
124 target_operation );
Ronald Cron2091eed2021-04-09 11:09:54 +0200125#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
126 mbedtls_test_driver_hash_hooks.driver_status =
127 mbedtls_psa_hash_clone( source_operation, target_operation );
128#else
129 (void) source_operation;
130 (void) target_operation;
131 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
132#endif
Ronald Cron0bec41a2021-03-23 09:33:25 +0100133 }
134
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200135 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cron0bec41a2021-03-23 09:33:25 +0100136}
137
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200138psa_status_t mbedtls_test_transparent_hash_update(
Ronald Cron0bec41a2021-03-23 09:33:25 +0100139 mbedtls_transparent_test_driver_hash_operation_t *operation,
140 const uint8_t *input,
141 size_t input_length )
142{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200143 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100144
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200145 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cron0bec41a2021-03-23 09:33:25 +0100146 {
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200147 mbedtls_test_driver_hash_hooks.driver_status =
148 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100149 }
150 else
151 {
Ronald Cronb814bda2021-09-13 14:50:42 +0200152#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
153 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200154 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron7b7854e2021-03-13 18:19:08 +0100155 libtestdriver1_mbedtls_psa_hash_update(
Ronald Cron0bec41a2021-03-23 09:33:25 +0100156 operation, input, input_length );
Ronald Cron2091eed2021-04-09 11:09:54 +0200157#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
158 mbedtls_test_driver_hash_hooks.driver_status =
159 mbedtls_psa_hash_update( operation, input, input_length );
160#else
161 (void) operation;
162 (void) input;
163 (void) input_length;
164 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
165#endif
Ronald Cron0bec41a2021-03-23 09:33:25 +0100166 }
167
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200168 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cron0bec41a2021-03-23 09:33:25 +0100169}
170
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200171psa_status_t mbedtls_test_transparent_hash_finish(
Ronald Cron0bec41a2021-03-23 09:33:25 +0100172 mbedtls_transparent_test_driver_hash_operation_t *operation,
173 uint8_t *hash,
174 size_t hash_size,
175 size_t *hash_length )
176{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200177 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100178
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200179 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cron0bec41a2021-03-23 09:33:25 +0100180 {
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200181 mbedtls_test_driver_hash_hooks.driver_status =
182 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100183 }
184 else
185 {
Ronald Cronb814bda2021-09-13 14:50:42 +0200186#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
187 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200188 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron7b7854e2021-03-13 18:19:08 +0100189 libtestdriver1_mbedtls_psa_hash_finish(
Ronald Cron0bec41a2021-03-23 09:33:25 +0100190 operation, hash, hash_size, hash_length );
Ronald Cron2091eed2021-04-09 11:09:54 +0200191#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
192 mbedtls_test_driver_hash_hooks.driver_status =
193 mbedtls_psa_hash_finish( operation, hash, hash_size, hash_length );
194#else
195 (void) operation;
196 (void) hash;
197 (void) hash_size;
198 (void) hash_length;
199 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
200#endif
Ronald Cron0bec41a2021-03-23 09:33:25 +0100201 }
202
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200203 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cron0bec41a2021-03-23 09:33:25 +0100204}
205
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200206psa_status_t mbedtls_test_transparent_hash_abort(
Ronald Cron0bec41a2021-03-23 09:33:25 +0100207 mbedtls_transparent_test_driver_hash_operation_t *operation )
208{
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200209 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100210
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200211 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cron0bec41a2021-03-23 09:33:25 +0100212 {
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200213 mbedtls_test_driver_hash_hooks.driver_status =
214 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cron0bec41a2021-03-23 09:33:25 +0100215 }
216 else
217 {
Ronald Cronb814bda2021-09-13 14:50:42 +0200218#if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
219 defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200220 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cron7b7854e2021-03-13 18:19:08 +0100221 libtestdriver1_mbedtls_psa_hash_abort( operation );
Ronald Cron2091eed2021-04-09 11:09:54 +0200222#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
223 mbedtls_test_driver_hash_hooks.driver_status =
224 mbedtls_psa_hash_abort( operation );
225#else
226 (void) operation;
227 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
228#endif
Ronald Cron0bec41a2021-03-23 09:33:25 +0100229 }
230
Ronald Cronc4bc12e2021-04-13 12:41:34 +0200231 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cron0bec41a2021-03-23 09:33:25 +0100232}
233#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */