1 /*
2  * Test driver for MAC entry points.
3  */
4 /*  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6  */
7 
8 #include <test/helpers.h>
9 
10 #if defined(PSA_CRYPTO_DRIVER_TEST)
11 #include "psa_crypto_mac.h"
12 
13 #include "test/drivers/mac.h"
14 
15 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
16 #include "libtestdriver1/library/psa_crypto_mac.h"
17 #endif
18 
19 mbedtls_test_driver_mac_hooks_t mbedtls_test_driver_mac_hooks =
20     MBEDTLS_TEST_DRIVER_MAC_INIT;
21 
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)22 psa_status_t mbedtls_test_transparent_mac_compute(
23     const psa_key_attributes_t *attributes,
24     const uint8_t *key_buffer,
25     size_t key_buffer_size,
26     psa_algorithm_t alg,
27     const uint8_t *input,
28     size_t input_length,
29     uint8_t *mac,
30     size_t mac_size,
31     size_t *mac_length)
32 {
33     mbedtls_test_driver_mac_hooks.hits++;
34 
35     if (mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS) {
36         mbedtls_test_driver_mac_hooks.driver_status =
37             mbedtls_test_driver_mac_hooks.forced_status;
38     } else {
39 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
40         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_MAC)
41         mbedtls_test_driver_mac_hooks.driver_status =
42             libtestdriver1_mbedtls_psa_mac_compute(
43                 (const libtestdriver1_psa_key_attributes_t *) attributes,
44                 key_buffer, key_buffer_size, alg,
45                 input, input_length,
46                 mac, mac_size, mac_length);
47 #elif defined(MBEDTLS_PSA_BUILTIN_MAC)
48         mbedtls_test_driver_mac_hooks.driver_status =
49             mbedtls_psa_mac_compute(
50                 attributes, key_buffer, key_buffer_size, alg,
51                 input, input_length,
52                 mac, mac_size, mac_length);
53 #else
54         (void) attributes;
55         (void) key_buffer;
56         (void) key_buffer_size;
57         (void) alg;
58         (void) input;
59         (void) input_length;
60         (void) mac;
61         (void) mac_size;
62         (void) mac_length;
63         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
64 #endif
65     }
66 
67     return mbedtls_test_driver_mac_hooks.driver_status;
68 }
69 
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)70 psa_status_t mbedtls_test_transparent_mac_sign_setup(
71     mbedtls_transparent_test_driver_mac_operation_t *operation,
72     const psa_key_attributes_t *attributes,
73     const uint8_t *key_buffer,
74     size_t key_buffer_size,
75     psa_algorithm_t alg)
76 {
77     mbedtls_test_driver_mac_hooks.hits++;
78 
79     if (mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS) {
80         mbedtls_test_driver_mac_hooks.driver_status =
81             mbedtls_test_driver_mac_hooks.forced_status;
82     } else {
83 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
84         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_MAC)
85         mbedtls_test_driver_mac_hooks.driver_status =
86             libtestdriver1_mbedtls_psa_mac_sign_setup(
87                 operation,
88                 (const libtestdriver1_psa_key_attributes_t *) attributes,
89                 key_buffer, key_buffer_size, alg);
90 #elif defined(MBEDTLS_PSA_BUILTIN_MAC)
91         mbedtls_test_driver_mac_hooks.driver_status =
92             mbedtls_psa_mac_sign_setup(
93                 operation, attributes, key_buffer, key_buffer_size, alg);
94 #else
95         (void) operation;
96         (void) attributes;
97         (void) key_buffer;
98         (void) key_buffer_size;
99         (void) alg;
100         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
101 #endif
102     }
103 
104     return mbedtls_test_driver_mac_hooks.driver_status;
105 }
106 
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)107 psa_status_t mbedtls_test_transparent_mac_verify_setup(
108     mbedtls_transparent_test_driver_mac_operation_t *operation,
109     const psa_key_attributes_t *attributes,
110     const uint8_t *key_buffer,
111     size_t key_buffer_size,
112     psa_algorithm_t alg)
113 {
114     mbedtls_test_driver_mac_hooks.hits++;
115 
116     if (mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS) {
117         mbedtls_test_driver_mac_hooks.driver_status =
118             mbedtls_test_driver_mac_hooks.forced_status;
119     } else {
120 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
121         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_MAC)
122         mbedtls_test_driver_mac_hooks.driver_status =
123             libtestdriver1_mbedtls_psa_mac_verify_setup(
124                 operation,
125                 (const libtestdriver1_psa_key_attributes_t *) attributes,
126                 key_buffer, key_buffer_size, alg);
127 #elif defined(MBEDTLS_PSA_BUILTIN_MAC)
128         mbedtls_test_driver_mac_hooks.driver_status =
129             mbedtls_psa_mac_verify_setup(
130                 operation, attributes, key_buffer, key_buffer_size, alg);
131 #else
132         (void) operation;
133         (void) attributes;
134         (void) key_buffer;
135         (void) key_buffer_size;
136         (void) alg;
137         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
138 #endif
139     }
140 
141     return mbedtls_test_driver_mac_hooks.driver_status;
142 }
143 
mbedtls_test_transparent_mac_update(mbedtls_transparent_test_driver_mac_operation_t * operation,const uint8_t * input,size_t input_length)144 psa_status_t mbedtls_test_transparent_mac_update(
145     mbedtls_transparent_test_driver_mac_operation_t *operation,
146     const uint8_t *input,
147     size_t input_length)
148 {
149     mbedtls_test_driver_mac_hooks.hits++;
150 
151     if (mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS) {
152         mbedtls_test_driver_mac_hooks.driver_status =
153             mbedtls_test_driver_mac_hooks.forced_status;
154     } else {
155 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
156         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_MAC)
157         mbedtls_test_driver_mac_hooks.driver_status =
158             libtestdriver1_mbedtls_psa_mac_update(
159                 operation, input, input_length);
160 #elif defined(MBEDTLS_PSA_BUILTIN_MAC)
161         mbedtls_test_driver_mac_hooks.driver_status =
162             mbedtls_psa_mac_update(
163                 operation, input, input_length);
164 #else
165         (void) operation;
166         (void) input;
167         (void) input_length;
168         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
169 #endif
170     }
171 
172     return mbedtls_test_driver_mac_hooks.driver_status;
173 }
174 
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)175 psa_status_t mbedtls_test_transparent_mac_sign_finish(
176     mbedtls_transparent_test_driver_mac_operation_t *operation,
177     uint8_t *mac,
178     size_t mac_size,
179     size_t *mac_length)
180 {
181     mbedtls_test_driver_mac_hooks.hits++;
182 
183     if (mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS) {
184         mbedtls_test_driver_mac_hooks.driver_status =
185             mbedtls_test_driver_mac_hooks.forced_status;
186     } else {
187 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
188         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_MAC)
189         mbedtls_test_driver_mac_hooks.driver_status =
190             libtestdriver1_mbedtls_psa_mac_sign_finish(
191                 operation, mac, mac_size, mac_length);
192 #elif defined(MBEDTLS_PSA_BUILTIN_MAC)
193         mbedtls_test_driver_mac_hooks.driver_status =
194             mbedtls_psa_mac_sign_finish(
195                 operation, mac, mac_size, mac_length);
196 #else
197         (void) operation;
198         (void) mac;
199         (void) mac_size;
200         (void) mac_length;
201         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
202 #endif
203     }
204 
205     return mbedtls_test_driver_mac_hooks.driver_status;
206 }
207 
mbedtls_test_transparent_mac_verify_finish(mbedtls_transparent_test_driver_mac_operation_t * operation,const uint8_t * mac,size_t mac_length)208 psa_status_t mbedtls_test_transparent_mac_verify_finish(
209     mbedtls_transparent_test_driver_mac_operation_t *operation,
210     const uint8_t *mac,
211     size_t mac_length)
212 {
213     mbedtls_test_driver_mac_hooks.hits++;
214 
215     if (mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS) {
216         mbedtls_test_driver_mac_hooks.driver_status =
217             mbedtls_test_driver_mac_hooks.forced_status;
218     } else {
219 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
220         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_MAC)
221         mbedtls_test_driver_mac_hooks.driver_status =
222             libtestdriver1_mbedtls_psa_mac_verify_finish(
223                 operation, mac, mac_length);
224 #elif defined(MBEDTLS_PSA_BUILTIN_MAC)
225         mbedtls_test_driver_mac_hooks.driver_status =
226             mbedtls_psa_mac_verify_finish(
227                 operation, mac, mac_length);
228 #else
229         (void) operation;
230         (void) mac;
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_abort(mbedtls_transparent_test_driver_mac_operation_t * operation)239 psa_status_t mbedtls_test_transparent_mac_abort(
240     mbedtls_transparent_test_driver_mac_operation_t *operation)
241 {
242     mbedtls_test_driver_mac_hooks.hits++;
243 
244     if (mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS) {
245         mbedtls_test_driver_mac_hooks.driver_status =
246             mbedtls_test_driver_mac_hooks.forced_status;
247     } else {
248 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
249         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_MAC)
250         mbedtls_test_driver_mac_hooks.driver_status =
251             libtestdriver1_mbedtls_psa_mac_abort(operation);
252 #elif defined(MBEDTLS_PSA_BUILTIN_MAC)
253         mbedtls_test_driver_mac_hooks.driver_status =
254             mbedtls_psa_mac_abort(operation);
255 #else
256         (void) operation;
257         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
258 #endif
259     }
260 
261     return mbedtls_test_driver_mac_hooks.driver_status;
262 }
263 
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)264 psa_status_t mbedtls_test_opaque_mac_compute(
265     const psa_key_attributes_t *attributes,
266     const uint8_t *key_buffer,
267     size_t key_buffer_size,
268     psa_algorithm_t alg,
269     const uint8_t *input,
270     size_t input_length,
271     uint8_t *mac,
272     size_t mac_size,
273     size_t *mac_length)
274 {
275     mbedtls_test_driver_mac_hooks.hits++;
276 
277     if (mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS) {
278         mbedtls_test_driver_mac_hooks.driver_status =
279             mbedtls_test_driver_mac_hooks.forced_status;
280     } else {
281         (void) attributes;
282         (void) key_buffer;
283         (void) key_buffer_size;
284         (void) alg;
285         (void) input;
286         (void) input_length;
287         (void) mac;
288         (void) mac_size;
289         (void) mac_length;
290         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
291     }
292 
293     return mbedtls_test_driver_mac_hooks.driver_status;
294 }
295 
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)296 psa_status_t mbedtls_test_opaque_mac_sign_setup(
297     mbedtls_opaque_test_driver_mac_operation_t *operation,
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 {
303     mbedtls_test_driver_mac_hooks.hits++;
304 
305     if (mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS) {
306         mbedtls_test_driver_mac_hooks.driver_status =
307             mbedtls_test_driver_mac_hooks.forced_status;
308     } else {
309         (void) operation;
310         (void) attributes;
311         (void) key_buffer;
312         (void) key_buffer_size;
313         (void) alg;
314         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
315     }
316 
317     return mbedtls_test_driver_mac_hooks.driver_status;
318 }
319 
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)320 psa_status_t mbedtls_test_opaque_mac_verify_setup(
321     mbedtls_opaque_test_driver_mac_operation_t *operation,
322     const psa_key_attributes_t *attributes,
323     const uint8_t *key_buffer,
324     size_t key_buffer_size,
325     psa_algorithm_t alg)
326 {
327     mbedtls_test_driver_mac_hooks.hits++;
328 
329     if (mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS) {
330         mbedtls_test_driver_mac_hooks.driver_status =
331             mbedtls_test_driver_mac_hooks.forced_status;
332     } else {
333         (void) operation;
334         (void) attributes;
335         (void) key_buffer;
336         (void) key_buffer_size;
337         (void) alg;
338         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
339     }
340 
341     return mbedtls_test_driver_mac_hooks.driver_status;
342 }
343 
mbedtls_test_opaque_mac_update(mbedtls_opaque_test_driver_mac_operation_t * operation,const uint8_t * input,size_t input_length)344 psa_status_t mbedtls_test_opaque_mac_update(
345     mbedtls_opaque_test_driver_mac_operation_t *operation,
346     const uint8_t *input,
347     size_t input_length)
348 {
349     mbedtls_test_driver_mac_hooks.hits++;
350 
351     if (mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS) {
352         mbedtls_test_driver_mac_hooks.driver_status =
353             mbedtls_test_driver_mac_hooks.forced_status;
354     } else {
355         (void) operation;
356         (void) input;
357         (void) input_length;
358         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
359     }
360 
361     return mbedtls_test_driver_mac_hooks.driver_status;
362 }
363 
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)364 psa_status_t mbedtls_test_opaque_mac_sign_finish(
365     mbedtls_opaque_test_driver_mac_operation_t *operation,
366     uint8_t *mac,
367     size_t mac_size,
368     size_t *mac_length)
369 {
370     mbedtls_test_driver_mac_hooks.hits++;
371 
372     if (mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS) {
373         mbedtls_test_driver_mac_hooks.driver_status =
374             mbedtls_test_driver_mac_hooks.forced_status;
375     } else {
376         (void) operation;
377         (void) mac;
378         (void) mac_size;
379         (void) mac_length;
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_verify_finish(mbedtls_opaque_test_driver_mac_operation_t * operation,const uint8_t * mac,size_t mac_length)386 psa_status_t mbedtls_test_opaque_mac_verify_finish(
387     mbedtls_opaque_test_driver_mac_operation_t *operation,
388     const uint8_t *mac,
389     size_t mac_length)
390 {
391     mbedtls_test_driver_mac_hooks.hits++;
392 
393     if (mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS) {
394         mbedtls_test_driver_mac_hooks.driver_status =
395             mbedtls_test_driver_mac_hooks.forced_status;
396     } else {
397         (void) operation;
398         (void) mac;
399         (void) mac_length;
400         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
401     }
402 
403     return mbedtls_test_driver_mac_hooks.driver_status;
404 }
405 
mbedtls_test_opaque_mac_abort(mbedtls_opaque_test_driver_mac_operation_t * operation)406 psa_status_t mbedtls_test_opaque_mac_abort(
407     mbedtls_opaque_test_driver_mac_operation_t *operation)
408 {
409     mbedtls_test_driver_mac_hooks.hits++;
410 
411     if (mbedtls_test_driver_mac_hooks.forced_status != PSA_SUCCESS) {
412         mbedtls_test_driver_mac_hooks.driver_status =
413             mbedtls_test_driver_mac_hooks.forced_status;
414     } else {
415         (void) operation;
416         mbedtls_test_driver_mac_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
417     }
418 
419     return mbedtls_test_driver_mac_hooks.driver_status;
420 }
421 
422 #endif /* PSA_CRYPTO_DRIVER_TEST */
423