blob: 25574177dda0d32eb5f2b3bf320e19d9ef7b3cbe [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 Cron7f13fa22021-04-13 12:41:34 +020027mbedtls_test_driver_hash_hooks_t
28 mbedtls_test_driver_hash_hooks = MBEDTLS_TEST_DRIVER_HASH_INIT;
Ronald Cronfa036c82021-03-23 09:33:25 +010029
Ronald Cron7f13fa22021-04-13 12:41:34 +020030psa_status_t mbedtls_test_transparent_hash_compute(
Ronald Cronfa036c82021-03-23 09:33:25 +010031 psa_algorithm_t alg,
32 const uint8_t *input, size_t input_length,
33 uint8_t *hash, size_t hash_size, size_t *hash_length )
34{
Ronald Cron7f13fa22021-04-13 12:41:34 +020035 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +010036
Ronald Cron7f13fa22021-04-13 12:41:34 +020037 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +010038 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020039 mbedtls_test_driver_hash_hooks.driver_status =
40 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +010041 }
42 else
43 {
Ronald Cron73c9d9e2021-04-09 11:09:54 +020044#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
Ronald Cron7f13fa22021-04-13 12:41:34 +020045 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +010046 mbedtls_transparent_test_driver_hash_compute(
47 alg, input, input_length,
48 hash, hash_size, hash_length );
Ronald Cron73c9d9e2021-04-09 11:09:54 +020049#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
50 mbedtls_test_driver_hash_hooks.driver_status =
51 mbedtls_psa_hash_compute(
52 alg, input, input_length,
53 hash, hash_size, hash_length );
54#else
55 (void) alg;
56 (void) input;
57 (void) input_length;
58 (void) hash;
59 (void) hash_size;
60 (void) hash_length;
61 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
62#endif
Ronald Cronfa036c82021-03-23 09:33:25 +010063 }
64
Ronald Cron7f13fa22021-04-13 12:41:34 +020065 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +010066}
67
Ronald Cron7f13fa22021-04-13 12:41:34 +020068psa_status_t mbedtls_test_transparent_hash_setup(
Ronald Cronfa036c82021-03-23 09:33:25 +010069 mbedtls_transparent_test_driver_hash_operation_t *operation,
70 psa_algorithm_t alg )
71{
Ronald Cron7f13fa22021-04-13 12:41:34 +020072 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +010073
Ronald Cron7f13fa22021-04-13 12:41:34 +020074 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +010075 {
Ronald Cron7f13fa22021-04-13 12:41:34 +020076 mbedtls_test_driver_hash_hooks.driver_status =
77 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +010078 }
79 else
80 {
Ronald Cron73c9d9e2021-04-09 11:09:54 +020081#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
Ronald Cron7f13fa22021-04-13 12:41:34 +020082 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +010083 mbedtls_transparent_test_driver_hash_setup( operation, alg );
Ronald Cron73c9d9e2021-04-09 11:09:54 +020084#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
85 mbedtls_test_driver_hash_hooks.driver_status =
86 mbedtls_psa_hash_setup( operation, alg );
87#else
88 (void) operation;
89 (void) alg;
90 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
91#endif
Ronald Cronfa036c82021-03-23 09:33:25 +010092 }
93
Ronald Cron7f13fa22021-04-13 12:41:34 +020094 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +010095}
96
Ronald Cron7f13fa22021-04-13 12:41:34 +020097psa_status_t mbedtls_test_transparent_hash_clone(
Ronald Cronfa036c82021-03-23 09:33:25 +010098 const mbedtls_transparent_test_driver_hash_operation_t *source_operation,
99 mbedtls_transparent_test_driver_hash_operation_t *target_operation )
100{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200101 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +0100102
Ronald Cron7f13fa22021-04-13 12:41:34 +0200103 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +0100104 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200105 mbedtls_test_driver_hash_hooks.driver_status =
106 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +0100107 }
108 else
109 {
Ronald Cron73c9d9e2021-04-09 11:09:54 +0200110#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200111 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +0100112 mbedtls_transparent_test_driver_hash_clone( source_operation,
113 target_operation );
Ronald Cron73c9d9e2021-04-09 11:09:54 +0200114#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
115 mbedtls_test_driver_hash_hooks.driver_status =
116 mbedtls_psa_hash_clone( source_operation, target_operation );
117#else
118 (void) source_operation;
119 (void) target_operation;
120 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
121#endif
Ronald Cronfa036c82021-03-23 09:33:25 +0100122 }
123
Ronald Cron7f13fa22021-04-13 12:41:34 +0200124 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +0100125}
126
Ronald Cron7f13fa22021-04-13 12:41:34 +0200127psa_status_t mbedtls_test_transparent_hash_update(
Ronald Cronfa036c82021-03-23 09:33:25 +0100128 mbedtls_transparent_test_driver_hash_operation_t *operation,
129 const uint8_t *input,
130 size_t input_length )
131{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200132 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +0100133
Ronald Cron7f13fa22021-04-13 12:41:34 +0200134 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +0100135 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200136 mbedtls_test_driver_hash_hooks.driver_status =
137 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +0100138 }
139 else
140 {
Ronald Cron73c9d9e2021-04-09 11:09:54 +0200141#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200142 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +0100143 mbedtls_transparent_test_driver_hash_update(
144 operation, input, input_length );
Ronald Cron73c9d9e2021-04-09 11:09:54 +0200145#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
146 mbedtls_test_driver_hash_hooks.driver_status =
147 mbedtls_psa_hash_update( operation, input, input_length );
148#else
149 (void) operation;
150 (void) input;
151 (void) input_length;
152 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
153#endif
Ronald Cronfa036c82021-03-23 09:33:25 +0100154 }
155
Ronald Cron7f13fa22021-04-13 12:41:34 +0200156 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +0100157}
158
Ronald Cron7f13fa22021-04-13 12:41:34 +0200159psa_status_t mbedtls_test_transparent_hash_finish(
Ronald Cronfa036c82021-03-23 09:33:25 +0100160 mbedtls_transparent_test_driver_hash_operation_t *operation,
161 uint8_t *hash,
162 size_t hash_size,
163 size_t *hash_length )
164{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200165 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +0100166
Ronald Cron7f13fa22021-04-13 12:41:34 +0200167 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +0100168 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200169 mbedtls_test_driver_hash_hooks.driver_status =
170 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +0100171 }
172 else
173 {
Ronald Cron73c9d9e2021-04-09 11:09:54 +0200174#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200175 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +0100176 mbedtls_transparent_test_driver_hash_finish(
177 operation, hash, hash_size, hash_length );
Ronald Cron73c9d9e2021-04-09 11:09:54 +0200178#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
179 mbedtls_test_driver_hash_hooks.driver_status =
180 mbedtls_psa_hash_finish( operation, hash, hash_size, hash_length );
181#else
182 (void) operation;
183 (void) hash;
184 (void) hash_size;
185 (void) hash_length;
186 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
187#endif
Ronald Cronfa036c82021-03-23 09:33:25 +0100188 }
189
Ronald Cron7f13fa22021-04-13 12:41:34 +0200190 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +0100191}
192
Ronald Cron7f13fa22021-04-13 12:41:34 +0200193psa_status_t mbedtls_test_transparent_hash_abort(
Ronald Cronfa036c82021-03-23 09:33:25 +0100194 mbedtls_transparent_test_driver_hash_operation_t *operation )
195{
Ronald Cron7f13fa22021-04-13 12:41:34 +0200196 mbedtls_test_driver_hash_hooks.hits++;
Ronald Cronfa036c82021-03-23 09:33:25 +0100197
Ronald Cron7f13fa22021-04-13 12:41:34 +0200198 if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
Ronald Cronfa036c82021-03-23 09:33:25 +0100199 {
Ronald Cron7f13fa22021-04-13 12:41:34 +0200200 mbedtls_test_driver_hash_hooks.driver_status =
201 mbedtls_test_driver_hash_hooks.forced_status;
Ronald Cronfa036c82021-03-23 09:33:25 +0100202 }
203 else
204 {
Ronald Cron73c9d9e2021-04-09 11:09:54 +0200205#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
Ronald Cron7f13fa22021-04-13 12:41:34 +0200206 mbedtls_test_driver_hash_hooks.driver_status =
Ronald Cronfa036c82021-03-23 09:33:25 +0100207 mbedtls_transparent_test_driver_hash_abort( operation );
Ronald Cron73c9d9e2021-04-09 11:09:54 +0200208#elif defined(MBEDTLS_PSA_BUILTIN_HASH)
209 mbedtls_test_driver_hash_hooks.driver_status =
210 mbedtls_psa_hash_abort( operation );
211#else
212 (void) operation;
213 mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
214#endif
Ronald Cronfa036c82021-03-23 09:33:25 +0100215 }
216
Ronald Cron7f13fa22021-04-13 12:41:34 +0200217 return( mbedtls_test_driver_hash_hooks.driver_status );
Ronald Cronfa036c82021-03-23 09:33:25 +0100218}
219#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */