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