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