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         mbedtls_test_driver_hash_hooks.driver_status =
43             mbedtls_test_driver_hash_hooks.forced_status;
44     } else {
45 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
46         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
47         mbedtls_test_driver_hash_hooks.driver_status =
48             libtestdriver1_mbedtls_psa_hash_compute(
49                 alg, input, input_length,
50                 hash, hash_size, hash_length);
51 #elif defined(MBEDTLS_PSA_BUILTIN_HASH)
52         mbedtls_test_driver_hash_hooks.driver_status =
53             mbedtls_psa_hash_compute(
54                 alg, input, input_length,
55                 hash, hash_size, hash_length);
56 #else
57         (void) alg;
58         (void) input;
59         (void) input_length;
60         (void) hash;
61         (void) hash_size;
62         (void) hash_length;
63         mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
64 #endif
65     }
66 
67     return mbedtls_test_driver_hash_hooks.driver_status;
68 }
69 
mbedtls_test_transparent_hash_setup(mbedtls_transparent_test_driver_hash_operation_t * operation,psa_algorithm_t alg)70 psa_status_t mbedtls_test_transparent_hash_setup(
71     mbedtls_transparent_test_driver_hash_operation_t *operation,
72     psa_algorithm_t alg)
73 {
74     mbedtls_test_driver_hash_hooks.hits++;
75 
76     if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) {
77         mbedtls_test_driver_hash_hooks.driver_status =
78             mbedtls_test_driver_hash_hooks.forced_status;
79     } else {
80 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
81         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
82         mbedtls_test_driver_hash_hooks.driver_status =
83             libtestdriver1_mbedtls_psa_hash_setup(operation, alg);
84 #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
92     }
93 
94     return mbedtls_test_driver_hash_hooks.driver_status;
95 }
96 
mbedtls_test_transparent_hash_clone(const mbedtls_transparent_test_driver_hash_operation_t * source_operation,mbedtls_transparent_test_driver_hash_operation_t * target_operation)97 psa_status_t mbedtls_test_transparent_hash_clone(
98     const mbedtls_transparent_test_driver_hash_operation_t *source_operation,
99     mbedtls_transparent_test_driver_hash_operation_t *target_operation)
100 {
101     mbedtls_test_driver_hash_hooks.hits++;
102 
103     if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) {
104         mbedtls_test_driver_hash_hooks.driver_status =
105             mbedtls_test_driver_hash_hooks.forced_status;
106     } else {
107 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
108         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
109         mbedtls_test_driver_hash_hooks.driver_status =
110             libtestdriver1_mbedtls_psa_hash_clone(source_operation,
111                                                   target_operation);
112 #elif defined(MBEDTLS_PSA_BUILTIN_HASH)
113         mbedtls_test_driver_hash_hooks.driver_status =
114             mbedtls_psa_hash_clone(source_operation, target_operation);
115 #else
116         (void) source_operation;
117         (void) target_operation;
118         mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
119 #endif
120     }
121 
122     return mbedtls_test_driver_hash_hooks.driver_status;
123 }
124 
mbedtls_test_transparent_hash_update(mbedtls_transparent_test_driver_hash_operation_t * operation,const uint8_t * input,size_t input_length)125 psa_status_t mbedtls_test_transparent_hash_update(
126     mbedtls_transparent_test_driver_hash_operation_t *operation,
127     const uint8_t *input,
128     size_t input_length)
129 {
130     mbedtls_test_driver_hash_hooks.hits++;
131 
132     if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) {
133         mbedtls_test_driver_hash_hooks.driver_status =
134             mbedtls_test_driver_hash_hooks.forced_status;
135     } else {
136 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
137         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
138         mbedtls_test_driver_hash_hooks.driver_status =
139             libtestdriver1_mbedtls_psa_hash_update(
140                 operation, input, input_length);
141 #elif defined(MBEDTLS_PSA_BUILTIN_HASH)
142         mbedtls_test_driver_hash_hooks.driver_status =
143             mbedtls_psa_hash_update(operation, input, input_length);
144 #else
145         (void) operation;
146         (void) input;
147         (void) input_length;
148         mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
149 #endif
150     }
151 
152     return mbedtls_test_driver_hash_hooks.driver_status;
153 }
154 
mbedtls_test_transparent_hash_finish(mbedtls_transparent_test_driver_hash_operation_t * operation,uint8_t * hash,size_t hash_size,size_t * hash_length)155 psa_status_t mbedtls_test_transparent_hash_finish(
156     mbedtls_transparent_test_driver_hash_operation_t *operation,
157     uint8_t *hash,
158     size_t hash_size,
159     size_t *hash_length)
160 {
161     mbedtls_test_driver_hash_hooks.hits++;
162 
163     if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) {
164         mbedtls_test_driver_hash_hooks.driver_status =
165             mbedtls_test_driver_hash_hooks.forced_status;
166     } else {
167 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
168         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
169         mbedtls_test_driver_hash_hooks.driver_status =
170             libtestdriver1_mbedtls_psa_hash_finish(
171                 operation, hash, hash_size, hash_length);
172 #elif defined(MBEDTLS_PSA_BUILTIN_HASH)
173         mbedtls_test_driver_hash_hooks.driver_status =
174             mbedtls_psa_hash_finish(operation, hash, hash_size, hash_length);
175 #else
176         (void) operation;
177         (void) hash;
178         (void) hash_size;
179         (void) hash_length;
180         mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
181 #endif
182     }
183 
184     return mbedtls_test_driver_hash_hooks.driver_status;
185 }
186 
mbedtls_test_transparent_hash_abort(mbedtls_transparent_test_driver_hash_operation_t * operation)187 psa_status_t mbedtls_test_transparent_hash_abort(
188     mbedtls_transparent_test_driver_hash_operation_t *operation)
189 {
190     mbedtls_test_driver_hash_hooks.hits++;
191 
192     if (mbedtls_test_driver_hash_hooks.forced_status != PSA_SUCCESS) {
193         mbedtls_test_driver_hash_hooks.driver_status =
194             mbedtls_test_driver_hash_hooks.forced_status;
195     } else {
196 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
197         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_HASH)
198         mbedtls_test_driver_hash_hooks.driver_status =
199             libtestdriver1_mbedtls_psa_hash_abort(operation);
200 #elif defined(MBEDTLS_PSA_BUILTIN_HASH)
201         mbedtls_test_driver_hash_hooks.driver_status =
202             mbedtls_psa_hash_abort(operation);
203 #else
204         (void) operation;
205         mbedtls_test_driver_hash_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
206 #endif
207     }
208 
209     return mbedtls_test_driver_hash_hooks.driver_status;
210 }
211 #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */
212