1 /*
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 
31 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
32 #include "libtestdriver1/library/psa_crypto_hash.h"
33 #endif
34 
35 mbedtls_test_driver_hash_hooks_t
36     mbedtls_test_driver_hash_hooks = MBEDTLS_TEST_DRIVER_HASH_INIT;
37 
mbedtls_test_transparent_hash_compute(psa_algorithm_t alg,const uint8_t * input,size_t input_length,uint8_t * hash,size_t hash_size,size_t * hash_length)38 psa_status_t mbedtls_test_transparent_hash_compute(
39     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 {
43     mbedtls_test_driver_hash_hooks.hits++;
44 
45     if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
46     {
47          mbedtls_test_driver_hash_hooks.driver_status =
48              mbedtls_test_driver_hash_hooks.forced_status;
49     }
50     else
51     {
52 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
53     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
54         mbedtls_test_driver_hash_hooks.driver_status =
55             libtestdriver1_mbedtls_psa_hash_compute(
56                 alg, input, input_length,
57                 hash, hash_size, hash_length );
58 #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
72     }
73 
74     return( mbedtls_test_driver_hash_hooks.driver_status );
75 }
76 
mbedtls_test_transparent_hash_setup(mbedtls_transparent_test_driver_hash_operation_t * operation,psa_algorithm_t alg)77 psa_status_t mbedtls_test_transparent_hash_setup(
78     mbedtls_transparent_test_driver_hash_operation_t *operation,
79     psa_algorithm_t alg )
80 {
81     mbedtls_test_driver_hash_hooks.hits++;
82 
83     if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
84     {
85          mbedtls_test_driver_hash_hooks.driver_status =
86              mbedtls_test_driver_hash_hooks.forced_status;
87     }
88     else
89     {
90 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
91     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
92         mbedtls_test_driver_hash_hooks.driver_status =
93             libtestdriver1_mbedtls_psa_hash_setup( operation, alg );
94 #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
102     }
103 
104     return( mbedtls_test_driver_hash_hooks.driver_status );
105 }
106 
mbedtls_test_transparent_hash_clone(const mbedtls_transparent_test_driver_hash_operation_t * source_operation,mbedtls_transparent_test_driver_hash_operation_t * target_operation)107 psa_status_t mbedtls_test_transparent_hash_clone(
108     const mbedtls_transparent_test_driver_hash_operation_t *source_operation,
109     mbedtls_transparent_test_driver_hash_operation_t *target_operation )
110 {
111     mbedtls_test_driver_hash_hooks.hits++;
112 
113     if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
114     {
115          mbedtls_test_driver_hash_hooks.driver_status =
116              mbedtls_test_driver_hash_hooks.forced_status;
117     }
118     else
119     {
120 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
121     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
122         mbedtls_test_driver_hash_hooks.driver_status =
123             libtestdriver1_mbedtls_psa_hash_clone( source_operation,
124                                                    target_operation );
125 #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
133     }
134 
135     return( mbedtls_test_driver_hash_hooks.driver_status );
136 }
137 
mbedtls_test_transparent_hash_update(mbedtls_transparent_test_driver_hash_operation_t * operation,const uint8_t * input,size_t input_length)138 psa_status_t mbedtls_test_transparent_hash_update(
139     mbedtls_transparent_test_driver_hash_operation_t *operation,
140     const uint8_t *input,
141     size_t input_length )
142 {
143     mbedtls_test_driver_hash_hooks.hits++;
144 
145     if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
146     {
147          mbedtls_test_driver_hash_hooks.driver_status =
148              mbedtls_test_driver_hash_hooks.forced_status;
149     }
150     else
151     {
152 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
153     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
154         mbedtls_test_driver_hash_hooks.driver_status =
155             libtestdriver1_mbedtls_psa_hash_update(
156                 operation, input, input_length );
157 #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
166     }
167 
168     return( mbedtls_test_driver_hash_hooks.driver_status );
169 }
170 
mbedtls_test_transparent_hash_finish(mbedtls_transparent_test_driver_hash_operation_t * operation,uint8_t * hash,size_t hash_size,size_t * hash_length)171 psa_status_t mbedtls_test_transparent_hash_finish(
172     mbedtls_transparent_test_driver_hash_operation_t *operation,
173     uint8_t *hash,
174     size_t hash_size,
175     size_t *hash_length )
176 {
177     mbedtls_test_driver_hash_hooks.hits++;
178 
179     if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
180     {
181          mbedtls_test_driver_hash_hooks.driver_status =
182              mbedtls_test_driver_hash_hooks.forced_status;
183     }
184     else
185     {
186 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
187     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
188         mbedtls_test_driver_hash_hooks.driver_status =
189             libtestdriver1_mbedtls_psa_hash_finish(
190                 operation, hash, hash_size, hash_length );
191 #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
201     }
202 
203     return( mbedtls_test_driver_hash_hooks.driver_status );
204 }
205 
mbedtls_test_transparent_hash_abort(mbedtls_transparent_test_driver_hash_operation_t * operation)206 psa_status_t mbedtls_test_transparent_hash_abort(
207     mbedtls_transparent_test_driver_hash_operation_t *operation )
208 {
209     mbedtls_test_driver_hash_hooks.hits++;
210 
211     if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
212     {
213          mbedtls_test_driver_hash_hooks.driver_status =
214              mbedtls_test_driver_hash_hooks.forced_status;
215     }
216     else
217     {
218 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
219     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
220         mbedtls_test_driver_hash_hooks.driver_status =
221             libtestdriver1_mbedtls_psa_hash_abort( operation );
222 #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
229     }
230 
231     return( mbedtls_test_driver_hash_hooks.driver_status );
232 }
233 #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */
234