blob: 69af107809fc26b8e66296e9a2c9f66a4952191e [file] [log] [blame]
Steven Cooremanc7f0a572021-04-29 21:10:11 +02001/*
2 * Test driver for MAC 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_mac.h"
28
29#include "test/drivers/mac.h"
30
Steven Cooremanae3ec522021-05-10 11:18:20 +020031mbedtls_test_driver_mac_hooks_t mbedtls_test_driver_mac_hooks =
32 MBEDTLS_TEST_DRIVER_MAC_INIT;
Steven Cooremanc7f0a572021-04-29 21:10:11 +020033
34psa_status_t mbedtls_test_transparent_mac_compute(
35 const psa_key_attributes_t *attributes,
36 const uint8_t *key_buffer,
37 size_t key_buffer_size,
38 psa_algorithm_t alg,
39 const uint8_t *input,
40 size_t input_length,
41 uint8_t *mac,
42 size_t mac_size,
43 size_t *mac_length )
44{
Steven Cooremanae3ec522021-05-10 11:18:20 +020045 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooremanc7f0a572021-04-29 21:10:11 +020046
Steven Cooremanae3ec522021-05-10 11:18:20 +020047 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooremanc7f0a572021-04-29 21:10:11 +020048 {
Steven Cooremanae3ec522021-05-10 11:18:20 +020049 mbedtls_test_driver_mac_hooks.driver_status =
50 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooremanc7f0a572021-04-29 21:10:11 +020051 }
52 else
53 {
Steven Cooremanae3ec522021-05-10 11:18:20 +020054 mbedtls_test_driver_mac_hooks.driver_status =
Steven Cooremanc7f0a572021-04-29 21:10:11 +020055 mbedtls_transparent_test_driver_mac_compute(
56 attributes, key_buffer, key_buffer_size, alg,
57 input, input_length,
58 mac, mac_size, mac_length );
59 }
60
Steven Cooremanae3ec522021-05-10 11:18:20 +020061 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooremanc7f0a572021-04-29 21:10:11 +020062}
63
64psa_status_t mbedtls_test_transparent_mac_sign_setup(
65 mbedtls_transparent_test_driver_mac_operation_t *operation,
66 const psa_key_attributes_t *attributes,
67 const uint8_t *key_buffer,
68 size_t key_buffer_size,
69 psa_algorithm_t alg )
70{
Steven Cooremanae3ec522021-05-10 11:18:20 +020071 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooremanc7f0a572021-04-29 21:10:11 +020072
Steven Cooremanae3ec522021-05-10 11:18:20 +020073 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooremanc7f0a572021-04-29 21:10:11 +020074 {
Steven Cooremanae3ec522021-05-10 11:18:20 +020075 mbedtls_test_driver_mac_hooks.driver_status =
76 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooremanc7f0a572021-04-29 21:10:11 +020077 }
78 else
79 {
Steven Cooremanae3ec522021-05-10 11:18:20 +020080 mbedtls_test_driver_mac_hooks.driver_status =
Steven Cooremanc7f0a572021-04-29 21:10:11 +020081 mbedtls_transparent_test_driver_mac_sign_setup(
82 operation, attributes, key_buffer, key_buffer_size, alg );
83 }
84
Steven Cooremanae3ec522021-05-10 11:18:20 +020085 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooremanc7f0a572021-04-29 21:10:11 +020086}
87
88psa_status_t mbedtls_test_transparent_mac_verify_setup(
89 mbedtls_transparent_test_driver_mac_operation_t *operation,
90 const psa_key_attributes_t *attributes,
91 const uint8_t *key_buffer,
92 size_t key_buffer_size,
93 psa_algorithm_t alg )
94{
Steven Cooremanae3ec522021-05-10 11:18:20 +020095 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooremanc7f0a572021-04-29 21:10:11 +020096
Steven Cooremanae3ec522021-05-10 11:18:20 +020097 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooremanc7f0a572021-04-29 21:10:11 +020098 {
Steven Cooremanae3ec522021-05-10 11:18:20 +020099 mbedtls_test_driver_mac_hooks.driver_status =
100 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200101 }
102 else
103 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200104 mbedtls_test_driver_mac_hooks.driver_status =
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200105 mbedtls_transparent_test_driver_mac_verify_setup(
106 operation, attributes, key_buffer, key_buffer_size, alg );
107 }
108
Steven Cooremanae3ec522021-05-10 11:18:20 +0200109 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200110}
111
112psa_status_t mbedtls_test_transparent_mac_update(
113 mbedtls_transparent_test_driver_mac_operation_t *operation,
114 const uint8_t *input,
115 size_t input_length )
116{
Steven Cooremanae3ec522021-05-10 11:18:20 +0200117 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200118
Steven Cooremanae3ec522021-05-10 11:18:20 +0200119 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200120 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200121 mbedtls_test_driver_mac_hooks.driver_status =
122 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200123 }
124 else
125 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200126 mbedtls_test_driver_mac_hooks.driver_status =
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200127 mbedtls_transparent_test_driver_mac_update(
128 operation, input, input_length );
129 }
130
Steven Cooremanae3ec522021-05-10 11:18:20 +0200131 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200132}
133
134psa_status_t mbedtls_test_transparent_mac_sign_finish(
135 mbedtls_transparent_test_driver_mac_operation_t *operation,
136 uint8_t *mac,
137 size_t mac_size,
138 size_t *mac_length )
139{
Steven Cooremanae3ec522021-05-10 11:18:20 +0200140 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200141
Steven Cooremanae3ec522021-05-10 11:18:20 +0200142 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200143 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200144 mbedtls_test_driver_mac_hooks.driver_status =
145 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200146 }
147 else
148 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200149 mbedtls_test_driver_mac_hooks.driver_status =
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200150 mbedtls_transparent_test_driver_mac_sign_finish(
151 operation, mac, mac_size, mac_length );
152 }
153
Steven Cooremanae3ec522021-05-10 11:18:20 +0200154 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200155}
156
157psa_status_t mbedtls_test_transparent_mac_verify_finish(
158 mbedtls_transparent_test_driver_mac_operation_t *operation,
159 const uint8_t *mac,
160 size_t mac_length )
161{
Steven Cooremanae3ec522021-05-10 11:18:20 +0200162 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200163
Steven Cooremanae3ec522021-05-10 11:18:20 +0200164 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200165 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200166 mbedtls_test_driver_mac_hooks.driver_status =
167 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200168 }
169 else
170 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200171 mbedtls_test_driver_mac_hooks.driver_status =
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200172 mbedtls_transparent_test_driver_mac_verify_finish(
173 operation, mac, mac_length );
174 }
175
Steven Cooremanae3ec522021-05-10 11:18:20 +0200176 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200177}
178
179psa_status_t mbedtls_test_transparent_mac_abort(
180 mbedtls_transparent_test_driver_mac_operation_t *operation )
181{
Steven Cooremanae3ec522021-05-10 11:18:20 +0200182 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200183
Steven Cooremanae3ec522021-05-10 11:18:20 +0200184 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200185 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200186 mbedtls_test_driver_mac_hooks.driver_status =
187 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200188 }
189 else
190 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200191 mbedtls_test_driver_mac_hooks.driver_status =
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200192 mbedtls_transparent_test_driver_mac_abort( operation );
193 }
194
Steven Cooremanae3ec522021-05-10 11:18:20 +0200195 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200196}
197
198psa_status_t mbedtls_test_opaque_mac_compute(
199 const psa_key_attributes_t *attributes,
200 const uint8_t *key_buffer,
201 size_t key_buffer_size,
202 psa_algorithm_t alg,
203 const uint8_t *input,
204 size_t input_length,
205 uint8_t *mac,
206 size_t mac_size,
207 size_t *mac_length )
208{
Steven Cooremanae3ec522021-05-10 11:18:20 +0200209 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200210
Steven Cooremanae3ec522021-05-10 11:18:20 +0200211 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200212 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200213 mbedtls_test_driver_mac_hooks.driver_status =
214 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200215 }
216 else
217 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200218 mbedtls_test_driver_mac_hooks.driver_status =
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200219 mbedtls_opaque_test_driver_mac_compute(
220 attributes, key_buffer, key_buffer_size, alg,
221 input, input_length,
222 mac, mac_size, mac_length );
223 }
224
Steven Cooremanae3ec522021-05-10 11:18:20 +0200225 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200226}
227
228psa_status_t mbedtls_test_opaque_mac_sign_setup(
229 mbedtls_opaque_test_driver_mac_operation_t *operation,
230 const psa_key_attributes_t *attributes,
231 const uint8_t *key_buffer,
232 size_t key_buffer_size,
233 psa_algorithm_t alg )
234{
Steven Cooremanae3ec522021-05-10 11:18:20 +0200235 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200236
Steven Cooremanae3ec522021-05-10 11:18:20 +0200237 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200238 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200239 mbedtls_test_driver_mac_hooks.driver_status =
240 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200241 }
242 else
243 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200244 mbedtls_test_driver_mac_hooks.driver_status =
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200245 mbedtls_opaque_test_driver_mac_sign_setup(
246 operation, attributes, key_buffer, key_buffer_size, alg );
247 }
248
Steven Cooremanae3ec522021-05-10 11:18:20 +0200249 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200250}
251
252psa_status_t mbedtls_test_opaque_mac_verify_setup(
253 mbedtls_opaque_test_driver_mac_operation_t *operation,
254 const psa_key_attributes_t *attributes,
255 const uint8_t *key_buffer,
256 size_t key_buffer_size,
257 psa_algorithm_t alg )
258{
Steven Cooremanae3ec522021-05-10 11:18:20 +0200259 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200260
Steven Cooremanae3ec522021-05-10 11:18:20 +0200261 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200262 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200263 mbedtls_test_driver_mac_hooks.driver_status =
264 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200265 }
266 else
267 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200268 mbedtls_test_driver_mac_hooks.driver_status =
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200269 mbedtls_opaque_test_driver_mac_verify_setup(
270 operation, attributes, key_buffer, key_buffer_size, alg );
271 }
272
Steven Cooremanae3ec522021-05-10 11:18:20 +0200273 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200274}
275
276psa_status_t mbedtls_test_opaque_mac_update(
277 mbedtls_opaque_test_driver_mac_operation_t *operation,
278 const uint8_t *input,
279 size_t input_length )
280{
Steven Cooremanae3ec522021-05-10 11:18:20 +0200281 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200282
Steven Cooremanae3ec522021-05-10 11:18:20 +0200283 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200284 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200285 mbedtls_test_driver_mac_hooks.driver_status =
286 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200287 }
288 else
289 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200290 mbedtls_test_driver_mac_hooks.driver_status =
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200291 mbedtls_opaque_test_driver_mac_update(
292 operation, input, input_length );
293 }
294
Steven Cooremanae3ec522021-05-10 11:18:20 +0200295 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200296}
297
298psa_status_t mbedtls_test_opaque_mac_sign_finish(
299 mbedtls_opaque_test_driver_mac_operation_t *operation,
300 uint8_t *mac,
301 size_t mac_size,
302 size_t *mac_length )
303{
Steven Cooremanae3ec522021-05-10 11:18:20 +0200304 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200305
Steven Cooremanae3ec522021-05-10 11:18:20 +0200306 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200307 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200308 mbedtls_test_driver_mac_hooks.driver_status =
309 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200310 }
311 else
312 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200313 mbedtls_test_driver_mac_hooks.driver_status =
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200314 mbedtls_opaque_test_driver_mac_sign_finish(
315 operation, mac, mac_size, mac_length );
316 }
317
Steven Cooremanae3ec522021-05-10 11:18:20 +0200318 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200319}
320
321psa_status_t mbedtls_test_opaque_mac_verify_finish(
322 mbedtls_opaque_test_driver_mac_operation_t *operation,
323 const uint8_t *mac,
324 size_t mac_length )
325{
Steven Cooremanae3ec522021-05-10 11:18:20 +0200326 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200327
Steven Cooremanae3ec522021-05-10 11:18:20 +0200328 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200329 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200330 mbedtls_test_driver_mac_hooks.driver_status =
331 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200332 }
333 else
334 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200335 mbedtls_test_driver_mac_hooks.driver_status =
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200336 mbedtls_opaque_test_driver_mac_verify_finish(
337 operation, mac, mac_length );
338 }
339
Steven Cooremanae3ec522021-05-10 11:18:20 +0200340 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200341}
342
343psa_status_t mbedtls_test_opaque_mac_abort(
344 mbedtls_opaque_test_driver_mac_operation_t *operation )
345{
Steven Cooremanae3ec522021-05-10 11:18:20 +0200346 mbedtls_test_driver_mac_hooks.hits++;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200347
Steven Cooremanae3ec522021-05-10 11:18:20 +0200348 if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200349 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200350 mbedtls_test_driver_mac_hooks.driver_status =
351 mbedtls_test_driver_mac_hooks.forced_status;
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200352 }
353 else
354 {
Steven Cooremanae3ec522021-05-10 11:18:20 +0200355 mbedtls_test_driver_mac_hooks.driver_status =
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200356 mbedtls_opaque_test_driver_mac_abort( operation );
357 }
358
Steven Cooremanae3ec522021-05-10 11:18:20 +0200359 return( mbedtls_test_driver_mac_hooks.driver_status );
Steven Cooremanc7f0a572021-04-29 21:10:11 +0200360}
361
362#endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */