• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <test/helpers.h>
21 
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 
27 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
28 #include "libtestdriver1/library/psa_crypto_hash.h"
29 #endif
30 
31 mbedtls_test_driver_hash_hooks_t
32     mbedtls_test_driver_hash_hooks = MBEDTLS_TEST_DRIVER_HASH_INIT;
33 
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)34 psa_status_t mbedtls_test_transparent_hash_compute(
35     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 {
39     mbedtls_test_driver_hash_hooks.hits++;
40 
41     if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
42     {
43          mbedtls_test_driver_hash_hooks.driver_status =
44              mbedtls_test_driver_hash_hooks.forced_status;
45     }
46     else
47     {
48 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
49     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
50         mbedtls_test_driver_hash_hooks.driver_status =
51             libtestdriver1_mbedtls_psa_hash_compute(
52                 alg, input, input_length,
53                 hash, hash_size, hash_length );
54 #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
68     }
69 
70     return( mbedtls_test_driver_hash_hooks.driver_status );
71 }
72 
mbedtls_test_transparent_hash_setup(mbedtls_transparent_test_driver_hash_operation_t * operation,psa_algorithm_t alg)73 psa_status_t mbedtls_test_transparent_hash_setup(
74     mbedtls_transparent_test_driver_hash_operation_t *operation,
75     psa_algorithm_t alg )
76 {
77     mbedtls_test_driver_hash_hooks.hits++;
78 
79     if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
80     {
81          mbedtls_test_driver_hash_hooks.driver_status =
82              mbedtls_test_driver_hash_hooks.forced_status;
83     }
84     else
85     {
86 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
87     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
88         mbedtls_test_driver_hash_hooks.driver_status =
89             libtestdriver1_mbedtls_psa_hash_setup( operation, alg );
90 #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
98     }
99 
100     return( mbedtls_test_driver_hash_hooks.driver_status );
101 }
102 
mbedtls_test_transparent_hash_clone(const mbedtls_transparent_test_driver_hash_operation_t * source_operation,mbedtls_transparent_test_driver_hash_operation_t * target_operation)103 psa_status_t mbedtls_test_transparent_hash_clone(
104     const mbedtls_transparent_test_driver_hash_operation_t *source_operation,
105     mbedtls_transparent_test_driver_hash_operation_t *target_operation )
106 {
107     mbedtls_test_driver_hash_hooks.hits++;
108 
109     if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
110     {
111          mbedtls_test_driver_hash_hooks.driver_status =
112              mbedtls_test_driver_hash_hooks.forced_status;
113     }
114     else
115     {
116 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
117     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
118         mbedtls_test_driver_hash_hooks.driver_status =
119             libtestdriver1_mbedtls_psa_hash_clone( source_operation,
120                                                    target_operation );
121 #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
129     }
130 
131     return( mbedtls_test_driver_hash_hooks.driver_status );
132 }
133 
mbedtls_test_transparent_hash_update(mbedtls_transparent_test_driver_hash_operation_t * operation,const uint8_t * input,size_t input_length)134 psa_status_t mbedtls_test_transparent_hash_update(
135     mbedtls_transparent_test_driver_hash_operation_t *operation,
136     const uint8_t *input,
137     size_t input_length )
138 {
139     mbedtls_test_driver_hash_hooks.hits++;
140 
141     if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
142     {
143          mbedtls_test_driver_hash_hooks.driver_status =
144              mbedtls_test_driver_hash_hooks.forced_status;
145     }
146     else
147     {
148 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
149     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
150         mbedtls_test_driver_hash_hooks.driver_status =
151             libtestdriver1_mbedtls_psa_hash_update(
152                 operation, input, input_length );
153 #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
162     }
163 
164     return( mbedtls_test_driver_hash_hooks.driver_status );
165 }
166 
mbedtls_test_transparent_hash_finish(mbedtls_transparent_test_driver_hash_operation_t * operation,uint8_t * hash,size_t hash_size,size_t * hash_length)167 psa_status_t mbedtls_test_transparent_hash_finish(
168     mbedtls_transparent_test_driver_hash_operation_t *operation,
169     uint8_t *hash,
170     size_t hash_size,
171     size_t *hash_length )
172 {
173     mbedtls_test_driver_hash_hooks.hits++;
174 
175     if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
176     {
177          mbedtls_test_driver_hash_hooks.driver_status =
178              mbedtls_test_driver_hash_hooks.forced_status;
179     }
180     else
181     {
182 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
183     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
184         mbedtls_test_driver_hash_hooks.driver_status =
185             libtestdriver1_mbedtls_psa_hash_finish(
186                 operation, hash, hash_size, hash_length );
187 #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
197     }
198 
199     return( mbedtls_test_driver_hash_hooks.driver_status );
200 }
201 
mbedtls_test_transparent_hash_abort(mbedtls_transparent_test_driver_hash_operation_t * operation)202 psa_status_t mbedtls_test_transparent_hash_abort(
203     mbedtls_transparent_test_driver_hash_operation_t *operation )
204 {
205     mbedtls_test_driver_hash_hooks.hits++;
206 
207     if( mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS )
208     {
209          mbedtls_test_driver_hash_hooks.driver_status =
210              mbedtls_test_driver_hash_hooks.forced_status;
211     }
212     else
213     {
214 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
215     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
216         mbedtls_test_driver_hash_hooks.driver_status =
217             libtestdriver1_mbedtls_psa_hash_abort( operation );
218 #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
225     }
226 
227     return( mbedtls_test_driver_hash_hooks.driver_status );
228 }
229 #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */
230