1 /*
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 #include <test/helpers.h>
21 
22 #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST)
23 #include "psa_crypto_mac.h"
24 
25 #include "test/drivers/mac.h"
26 
27 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
28 #include "libtestdriver1/library/psa_crypto_mac.h"
29 #endif
30 
31 mbedtls_test_driver_mac_hooks_t mbedtls_test_driver_mac_hooks =
32     MBEDTLS_TEST_DRIVER_MAC_INIT;
33 
mbedtls_test_transparent_mac_compute(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg,const uint8_t * input,size_t input_length,uint8_t * mac,size_t mac_size,size_t * mac_length)34 psa_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 {
45     mbedtls_test_driver_mac_hooks.hits++;
46 
47     if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
48     {
49          mbedtls_test_driver_mac_hooks.driver_status =
50              mbedtls_test_driver_mac_hooks.forced_status;
51     }
52     else
53     {
54 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
55     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_MAC)
56         mbedtls_test_driver_mac_hooks.driver_status =
57             libtestdriver1_mbedtls_psa_mac_compute(
58                 (const libtestdriver1_psa_key_attributes_t *)attributes,
59                 key_buffer, key_buffer_size, alg,
60                 input, input_length,
61                 mac, mac_size, mac_length );
62 #elif defined(MBEDTLS_PSA_BUILTIN_MAC)
63         mbedtls_test_driver_mac_hooks.driver_status =
64             mbedtls_psa_mac_compute(
65                 attributes, key_buffer, key_buffer_size, alg,
66                 input, input_length,
67                 mac, mac_size, mac_length );
68 #else
69         (void) attributes;
70         (void) key_buffer;
71         (void) key_buffer_size;
72         (void) alg;
73         (void) input;
74         (void) input_length;
75         (void) mac;
76         (void) mac_size;
77         (void) mac_length;
78         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
79 #endif
80     }
81 
82     return( mbedtls_test_driver_mac_hooks.driver_status );
83 }
84 
mbedtls_test_transparent_mac_sign_setup(mbedtls_transparent_test_driver_mac_operation_t * operation,const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg)85 psa_status_t mbedtls_test_transparent_mac_sign_setup(
86     mbedtls_transparent_test_driver_mac_operation_t *operation,
87     const psa_key_attributes_t *attributes,
88     const uint8_t *key_buffer,
89     size_t key_buffer_size,
90     psa_algorithm_t alg )
91 {
92     mbedtls_test_driver_mac_hooks.hits++;
93 
94     if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
95     {
96          mbedtls_test_driver_mac_hooks.driver_status =
97              mbedtls_test_driver_mac_hooks.forced_status;
98     }
99     else
100     {
101 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
102     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_MAC)
103         mbedtls_test_driver_mac_hooks.driver_status =
104             libtestdriver1_mbedtls_psa_mac_sign_setup(
105                 operation,
106                 (const libtestdriver1_psa_key_attributes_t *)attributes,
107                 key_buffer, key_buffer_size, alg );
108 #elif defined(MBEDTLS_PSA_BUILTIN_MAC)
109         mbedtls_test_driver_mac_hooks.driver_status =
110             mbedtls_psa_mac_sign_setup(
111                 operation, attributes, key_buffer, key_buffer_size, alg );
112 #else
113         (void) operation;
114         (void) attributes;
115         (void) key_buffer;
116         (void) key_buffer_size;
117         (void) alg;
118         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
119 #endif
120     }
121 
122     return( mbedtls_test_driver_mac_hooks.driver_status );
123 }
124 
mbedtls_test_transparent_mac_verify_setup(mbedtls_transparent_test_driver_mac_operation_t * operation,const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg)125 psa_status_t mbedtls_test_transparent_mac_verify_setup(
126     mbedtls_transparent_test_driver_mac_operation_t *operation,
127     const psa_key_attributes_t *attributes,
128     const uint8_t *key_buffer,
129     size_t key_buffer_size,
130     psa_algorithm_t alg )
131 {
132     mbedtls_test_driver_mac_hooks.hits++;
133 
134     if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
135     {
136          mbedtls_test_driver_mac_hooks.driver_status =
137              mbedtls_test_driver_mac_hooks.forced_status;
138     }
139     else
140     {
141 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
142     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_MAC)
143         mbedtls_test_driver_mac_hooks.driver_status =
144             libtestdriver1_mbedtls_psa_mac_verify_setup(
145                 operation,
146                 (const libtestdriver1_psa_key_attributes_t *)attributes,
147                 key_buffer, key_buffer_size, alg );
148 #elif defined(MBEDTLS_PSA_BUILTIN_MAC)
149         mbedtls_test_driver_mac_hooks.driver_status =
150             mbedtls_psa_mac_verify_setup(
151                 operation, attributes, key_buffer, key_buffer_size, alg );
152 #else
153         (void) operation;
154         (void) attributes;
155         (void) key_buffer;
156         (void) key_buffer_size;
157         (void) alg;
158         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
159 #endif
160     }
161 
162     return( mbedtls_test_driver_mac_hooks.driver_status );
163 }
164 
mbedtls_test_transparent_mac_update(mbedtls_transparent_test_driver_mac_operation_t * operation,const uint8_t * input,size_t input_length)165 psa_status_t mbedtls_test_transparent_mac_update(
166     mbedtls_transparent_test_driver_mac_operation_t *operation,
167     const uint8_t *input,
168     size_t input_length )
169 {
170     mbedtls_test_driver_mac_hooks.hits++;
171 
172     if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
173     {
174          mbedtls_test_driver_mac_hooks.driver_status =
175              mbedtls_test_driver_mac_hooks.forced_status;
176     }
177     else
178     {
179 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
180     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_MAC)
181         mbedtls_test_driver_mac_hooks.driver_status =
182             libtestdriver1_mbedtls_psa_mac_update(
183                 operation, input, input_length );
184 #elif defined(MBEDTLS_PSA_BUILTIN_MAC)
185         mbedtls_test_driver_mac_hooks.driver_status =
186             mbedtls_psa_mac_update(
187                 operation, input, input_length );
188 #else
189         (void) operation;
190         (void) input;
191         (void) input_length;
192         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
193 #endif
194     }
195 
196     return( mbedtls_test_driver_mac_hooks.driver_status );
197 }
198 
mbedtls_test_transparent_mac_sign_finish(mbedtls_transparent_test_driver_mac_operation_t * operation,uint8_t * mac,size_t mac_size,size_t * mac_length)199 psa_status_t mbedtls_test_transparent_mac_sign_finish(
200     mbedtls_transparent_test_driver_mac_operation_t *operation,
201     uint8_t *mac,
202     size_t mac_size,
203     size_t *mac_length )
204 {
205     mbedtls_test_driver_mac_hooks.hits++;
206 
207     if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
208     {
209          mbedtls_test_driver_mac_hooks.driver_status =
210              mbedtls_test_driver_mac_hooks.forced_status;
211     }
212     else
213     {
214 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
215     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_MAC)
216         mbedtls_test_driver_mac_hooks.driver_status =
217             libtestdriver1_mbedtls_psa_mac_sign_finish(
218                 operation, mac, mac_size, mac_length );
219 #elif defined(MBEDTLS_PSA_BUILTIN_MAC)
220         mbedtls_test_driver_mac_hooks.driver_status =
221             mbedtls_psa_mac_sign_finish(
222                 operation, mac, mac_size, mac_length );
223 #else
224         (void) operation;
225         (void) mac;
226         (void) mac_size;
227         (void) mac_length;
228         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
229 #endif
230     }
231 
232     return( mbedtls_test_driver_mac_hooks.driver_status );
233 }
234 
mbedtls_test_transparent_mac_verify_finish(mbedtls_transparent_test_driver_mac_operation_t * operation,const uint8_t * mac,size_t mac_length)235 psa_status_t mbedtls_test_transparent_mac_verify_finish(
236     mbedtls_transparent_test_driver_mac_operation_t *operation,
237     const uint8_t *mac,
238     size_t mac_length )
239 {
240     mbedtls_test_driver_mac_hooks.hits++;
241 
242     if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
243     {
244          mbedtls_test_driver_mac_hooks.driver_status =
245              mbedtls_test_driver_mac_hooks.forced_status;
246     }
247     else
248     {
249 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
250     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_MAC)
251         mbedtls_test_driver_mac_hooks.driver_status =
252             libtestdriver1_mbedtls_psa_mac_verify_finish(
253                 operation, mac, mac_length );
254 #elif defined(MBEDTLS_PSA_BUILTIN_MAC)
255         mbedtls_test_driver_mac_hooks.driver_status =
256             mbedtls_psa_mac_verify_finish(
257                 operation, mac, mac_length );
258 #else
259         (void) operation;
260         (void) mac;
261         (void) mac_length;
262         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
263 #endif
264     }
265 
266     return( mbedtls_test_driver_mac_hooks.driver_status );
267 }
268 
mbedtls_test_transparent_mac_abort(mbedtls_transparent_test_driver_mac_operation_t * operation)269 psa_status_t mbedtls_test_transparent_mac_abort(
270     mbedtls_transparent_test_driver_mac_operation_t *operation )
271 {
272     mbedtls_test_driver_mac_hooks.hits++;
273 
274     if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
275     {
276          mbedtls_test_driver_mac_hooks.driver_status =
277              mbedtls_test_driver_mac_hooks.forced_status;
278     }
279     else
280     {
281 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
282     defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_MAC)
283         mbedtls_test_driver_mac_hooks.driver_status =
284             libtestdriver1_mbedtls_psa_mac_abort( operation );
285 #elif defined(MBEDTLS_PSA_BUILTIN_MAC)
286         mbedtls_test_driver_mac_hooks.driver_status =
287             mbedtls_psa_mac_abort( operation );
288 #else
289         (void) operation;
290         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
291 #endif
292     }
293 
294     return( mbedtls_test_driver_mac_hooks.driver_status );
295 }
296 
mbedtls_test_opaque_mac_compute(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg,const uint8_t * input,size_t input_length,uint8_t * mac,size_t mac_size,size_t * mac_length)297 psa_status_t mbedtls_test_opaque_mac_compute(
298     const psa_key_attributes_t *attributes,
299     const uint8_t *key_buffer,
300     size_t key_buffer_size,
301     psa_algorithm_t alg,
302     const uint8_t *input,
303     size_t input_length,
304     uint8_t *mac,
305     size_t mac_size,
306     size_t *mac_length )
307 {
308     mbedtls_test_driver_mac_hooks.hits++;
309 
310     if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
311     {
312          mbedtls_test_driver_mac_hooks.driver_status =
313              mbedtls_test_driver_mac_hooks.forced_status;
314     }
315     else
316     {
317         (void) attributes;
318         (void) key_buffer;
319         (void) key_buffer_size;
320         (void) alg;
321         (void) input;
322         (void) input_length;
323         (void) mac;
324         (void) mac_size;
325         (void) mac_length;
326         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
327     }
328 
329     return( mbedtls_test_driver_mac_hooks.driver_status );
330 }
331 
mbedtls_test_opaque_mac_sign_setup(mbedtls_opaque_test_driver_mac_operation_t * operation,const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg)332 psa_status_t mbedtls_test_opaque_mac_sign_setup(
333     mbedtls_opaque_test_driver_mac_operation_t *operation,
334     const psa_key_attributes_t *attributes,
335     const uint8_t *key_buffer,
336     size_t key_buffer_size,
337     psa_algorithm_t alg )
338 {
339     mbedtls_test_driver_mac_hooks.hits++;
340 
341     if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
342     {
343          mbedtls_test_driver_mac_hooks.driver_status =
344              mbedtls_test_driver_mac_hooks.forced_status;
345     }
346     else
347     {
348         (void) operation;
349         (void) attributes;
350         (void) key_buffer;
351         (void) key_buffer_size;
352         (void) alg;
353         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
354     }
355 
356     return( mbedtls_test_driver_mac_hooks.driver_status );
357 }
358 
mbedtls_test_opaque_mac_verify_setup(mbedtls_opaque_test_driver_mac_operation_t * operation,const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg)359 psa_status_t mbedtls_test_opaque_mac_verify_setup(
360     mbedtls_opaque_test_driver_mac_operation_t *operation,
361     const psa_key_attributes_t *attributes,
362     const uint8_t *key_buffer,
363     size_t key_buffer_size,
364     psa_algorithm_t alg )
365 {
366     mbedtls_test_driver_mac_hooks.hits++;
367 
368     if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
369     {
370          mbedtls_test_driver_mac_hooks.driver_status =
371              mbedtls_test_driver_mac_hooks.forced_status;
372     }
373     else
374     {
375         (void) operation;
376         (void) attributes;
377         (void) key_buffer;
378         (void) key_buffer_size;
379         (void) alg;
380         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
381     }
382 
383     return( mbedtls_test_driver_mac_hooks.driver_status );
384 }
385 
mbedtls_test_opaque_mac_update(mbedtls_opaque_test_driver_mac_operation_t * operation,const uint8_t * input,size_t input_length)386 psa_status_t mbedtls_test_opaque_mac_update(
387     mbedtls_opaque_test_driver_mac_operation_t *operation,
388     const uint8_t *input,
389     size_t input_length )
390 {
391     mbedtls_test_driver_mac_hooks.hits++;
392 
393     if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
394     {
395          mbedtls_test_driver_mac_hooks.driver_status =
396              mbedtls_test_driver_mac_hooks.forced_status;
397     }
398     else
399     {
400         (void) operation;
401         (void) input;
402         (void) input_length;
403         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
404     }
405 
406     return( mbedtls_test_driver_mac_hooks.driver_status );
407 }
408 
mbedtls_test_opaque_mac_sign_finish(mbedtls_opaque_test_driver_mac_operation_t * operation,uint8_t * mac,size_t mac_size,size_t * mac_length)409 psa_status_t mbedtls_test_opaque_mac_sign_finish(
410     mbedtls_opaque_test_driver_mac_operation_t *operation,
411     uint8_t *mac,
412     size_t mac_size,
413     size_t *mac_length )
414 {
415     mbedtls_test_driver_mac_hooks.hits++;
416 
417     if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
418     {
419          mbedtls_test_driver_mac_hooks.driver_status =
420              mbedtls_test_driver_mac_hooks.forced_status;
421     }
422     else
423     {
424         (void) operation;
425         (void) mac;
426         (void) mac_size;
427         (void) mac_length;
428         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
429     }
430 
431     return( mbedtls_test_driver_mac_hooks.driver_status );
432 }
433 
mbedtls_test_opaque_mac_verify_finish(mbedtls_opaque_test_driver_mac_operation_t * operation,const uint8_t * mac,size_t mac_length)434 psa_status_t mbedtls_test_opaque_mac_verify_finish(
435     mbedtls_opaque_test_driver_mac_operation_t *operation,
436     const uint8_t *mac,
437     size_t mac_length )
438 {
439     mbedtls_test_driver_mac_hooks.hits++;
440 
441     if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
442     {
443          mbedtls_test_driver_mac_hooks.driver_status =
444              mbedtls_test_driver_mac_hooks.forced_status;
445     }
446     else
447     {
448         (void) operation;
449         (void) mac;
450         (void) mac_length;
451         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
452     }
453 
454     return( mbedtls_test_driver_mac_hooks.driver_status );
455 }
456 
mbedtls_test_opaque_mac_abort(mbedtls_opaque_test_driver_mac_operation_t * operation)457 psa_status_t mbedtls_test_opaque_mac_abort(
458     mbedtls_opaque_test_driver_mac_operation_t *operation )
459 {
460     mbedtls_test_driver_mac_hooks.hits++;
461 
462     if( mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS )
463     {
464          mbedtls_test_driver_mac_hooks.driver_status =
465              mbedtls_test_driver_mac_hooks.forced_status;
466     }
467     else
468     {
469         (void) operation;
470         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
471     }
472 
473     return( mbedtls_test_driver_mac_hooks.driver_status );
474 }
475 
476 #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */
477