1 /*
2  * Test driver for AEAD 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_aead.h"
12 #include "psa_crypto_core.h"
13 
14 #include "test/drivers/aead.h"
15 
16 #include "mbedtls/constant_time.h"
17 
18 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1)
19 #include "libtestdriver1/library/psa_crypto_aead.h"
20 #endif
21 
22 mbedtls_test_driver_aead_hooks_t
23     mbedtls_test_driver_aead_hooks = MBEDTLS_TEST_DRIVER_AEAD_INIT;
24 
mbedtls_test_transparent_aead_encrypt(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg,const uint8_t * nonce,size_t nonce_length,const uint8_t * additional_data,size_t additional_data_length,const uint8_t * plaintext,size_t plaintext_length,uint8_t * ciphertext,size_t ciphertext_size,size_t * ciphertext_length)25 psa_status_t mbedtls_test_transparent_aead_encrypt(
26     const psa_key_attributes_t *attributes,
27     const uint8_t *key_buffer, size_t key_buffer_size,
28     psa_algorithm_t alg,
29     const uint8_t *nonce, size_t nonce_length,
30     const uint8_t *additional_data, size_t additional_data_length,
31     const uint8_t *plaintext, size_t plaintext_length,
32     uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length)
33 {
34     mbedtls_test_driver_aead_hooks.hits_encrypt++;
35 
36     if (mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS) {
37         mbedtls_test_driver_aead_hooks.driver_status =
38             mbedtls_test_driver_aead_hooks.forced_status;
39     } else {
40 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
41         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_AEAD)
42         mbedtls_test_driver_aead_hooks.driver_status =
43             libtestdriver1_mbedtls_psa_aead_encrypt(
44                 (const libtestdriver1_psa_key_attributes_t *) attributes,
45                 key_buffer, key_buffer_size,
46                 alg,
47                 nonce, nonce_length,
48                 additional_data, additional_data_length,
49                 plaintext, plaintext_length,
50                 ciphertext, ciphertext_size, ciphertext_length);
51 #elif defined(MBEDTLS_PSA_BUILTIN_AEAD)
52         mbedtls_test_driver_aead_hooks.driver_status =
53             mbedtls_psa_aead_encrypt(
54                 attributes, key_buffer, key_buffer_size,
55                 alg,
56                 nonce, nonce_length,
57                 additional_data, additional_data_length,
58                 plaintext, plaintext_length,
59                 ciphertext, ciphertext_size, ciphertext_length);
60 #else
61         (void) attributes;
62         (void) key_buffer;
63         (void) key_buffer_size;
64         (void) alg;
65         (void) nonce;
66         (void) nonce_length;
67         (void) additional_data;
68         (void) additional_data_length;
69         (void) plaintext;
70         (void) plaintext_length;
71         (void) ciphertext;
72         (void) ciphertext_size;
73         (void) ciphertext_length;
74         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
75 #endif
76     }
77 
78     return mbedtls_test_driver_aead_hooks.driver_status;
79 }
80 
mbedtls_test_transparent_aead_decrypt(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg,const uint8_t * nonce,size_t nonce_length,const uint8_t * additional_data,size_t additional_data_length,const uint8_t * ciphertext,size_t ciphertext_length,uint8_t * plaintext,size_t plaintext_size,size_t * plaintext_length)81 psa_status_t mbedtls_test_transparent_aead_decrypt(
82     const psa_key_attributes_t *attributes,
83     const uint8_t *key_buffer, size_t key_buffer_size,
84     psa_algorithm_t alg,
85     const uint8_t *nonce, size_t nonce_length,
86     const uint8_t *additional_data, size_t additional_data_length,
87     const uint8_t *ciphertext, size_t ciphertext_length,
88     uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length)
89 {
90     mbedtls_test_driver_aead_hooks.hits_decrypt++;
91 
92     if (mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS) {
93         mbedtls_test_driver_aead_hooks.driver_status =
94             mbedtls_test_driver_aead_hooks.forced_status;
95     } else {
96 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
97         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_AEAD)
98         mbedtls_test_driver_aead_hooks.driver_status =
99             libtestdriver1_mbedtls_psa_aead_decrypt(
100                 (const libtestdriver1_psa_key_attributes_t *) attributes,
101                 key_buffer, key_buffer_size,
102                 alg,
103                 nonce, nonce_length,
104                 additional_data, additional_data_length,
105                 ciphertext, ciphertext_length,
106                 plaintext, plaintext_size, plaintext_length);
107 #elif defined(MBEDTLS_PSA_BUILTIN_AEAD)
108         mbedtls_test_driver_aead_hooks.driver_status =
109             mbedtls_psa_aead_decrypt(
110                 attributes, key_buffer, key_buffer_size,
111                 alg,
112                 nonce, nonce_length,
113                 additional_data, additional_data_length,
114                 ciphertext, ciphertext_length,
115                 plaintext, plaintext_size, plaintext_length);
116 #else
117         (void) attributes;
118         (void) key_buffer;
119         (void) key_buffer_size;
120         (void) alg;
121         (void) nonce;
122         (void) nonce_length;
123         (void) additional_data;
124         (void) additional_data_length;
125         (void) ciphertext;
126         (void) ciphertext_length;
127         (void) plaintext;
128         (void) plaintext_size;
129         (void) plaintext_length;
130         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
131 #endif
132     }
133 
134     return mbedtls_test_driver_aead_hooks.driver_status;
135 }
136 
mbedtls_test_transparent_aead_encrypt_setup(mbedtls_transparent_test_driver_aead_operation_t * operation,const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg)137 psa_status_t mbedtls_test_transparent_aead_encrypt_setup(
138     mbedtls_transparent_test_driver_aead_operation_t *operation,
139     const psa_key_attributes_t *attributes,
140     const uint8_t *key_buffer, size_t key_buffer_size,
141     psa_algorithm_t alg)
142 {
143     mbedtls_test_driver_aead_hooks.hits_encrypt_setup++;
144 
145     if (mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS) {
146         mbedtls_test_driver_aead_hooks.driver_status =
147             mbedtls_test_driver_aead_hooks.forced_status;
148     } else {
149 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
150         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_AEAD)
151         mbedtls_test_driver_aead_hooks.driver_status =
152             libtestdriver1_mbedtls_psa_aead_encrypt_setup(operation,
153                                                           (const libtestdriver1_psa_key_attributes_t
154                                                            *) attributes,
155                                                           key_buffer,
156                                                           key_buffer_size, alg);
157 #elif defined(MBEDTLS_PSA_BUILTIN_AEAD)
158         mbedtls_test_driver_aead_hooks.driver_status =
159             mbedtls_psa_aead_encrypt_setup(operation, attributes, key_buffer,
160                                            key_buffer_size, alg);
161 #else
162         (void) operation;
163         (void) attributes;
164         (void) key_buffer;
165         (void) key_buffer_size;
166         (void) alg;
167         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
168 #endif
169     }
170 
171     return mbedtls_test_driver_aead_hooks.driver_status;
172 }
173 
mbedtls_test_transparent_aead_decrypt_setup(mbedtls_transparent_test_driver_aead_operation_t * operation,const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg)174 psa_status_t mbedtls_test_transparent_aead_decrypt_setup(
175     mbedtls_transparent_test_driver_aead_operation_t *operation,
176     const psa_key_attributes_t *attributes,
177     const uint8_t *key_buffer, size_t key_buffer_size,
178     psa_algorithm_t alg)
179 {
180     mbedtls_test_driver_aead_hooks.hits_decrypt_setup++;
181 
182     if (mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS) {
183         mbedtls_test_driver_aead_hooks.driver_status =
184             mbedtls_test_driver_aead_hooks.forced_status;
185     } else {
186 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
187         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_AEAD)
188         mbedtls_test_driver_aead_hooks.driver_status =
189             libtestdriver1_mbedtls_psa_aead_decrypt_setup(operation,
190                                                           (const libtestdriver1_psa_key_attributes_t
191                                                            *) attributes,
192                                                           key_buffer, key_buffer_size, alg);
193 #elif defined(MBEDTLS_PSA_BUILTIN_AEAD)
194         mbedtls_test_driver_aead_hooks.driver_status =
195             mbedtls_psa_aead_decrypt_setup(operation, attributes, key_buffer,
196                                            key_buffer_size, alg);
197 #else
198         (void) operation;
199         (void) attributes;
200         (void) key_buffer;
201         (void) key_buffer_size;
202         (void) alg;
203         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
204 #endif
205     }
206 
207     return mbedtls_test_driver_aead_hooks.driver_status;
208 }
209 
mbedtls_test_transparent_aead_set_nonce(mbedtls_transparent_test_driver_aead_operation_t * operation,const uint8_t * nonce,size_t nonce_length)210 psa_status_t mbedtls_test_transparent_aead_set_nonce(
211     mbedtls_transparent_test_driver_aead_operation_t *operation,
212     const uint8_t *nonce,
213     size_t nonce_length)
214 {
215     mbedtls_test_driver_aead_hooks.hits_set_nonce++;
216 
217     if (mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS) {
218         mbedtls_test_driver_aead_hooks.driver_status =
219             mbedtls_test_driver_aead_hooks.forced_status;
220     } else {
221 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
222         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_AEAD)
223         mbedtls_test_driver_aead_hooks.driver_status =
224             libtestdriver1_mbedtls_psa_aead_set_nonce(operation, nonce, nonce_length);
225 #elif defined(MBEDTLS_PSA_BUILTIN_AEAD)
226         mbedtls_test_driver_aead_hooks.driver_status =
227             mbedtls_psa_aead_set_nonce(operation, nonce, nonce_length);
228 #else
229         (void) operation;
230         (void) nonce;
231         (void) nonce_length;
232         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
233 #endif
234     }
235 
236     return mbedtls_test_driver_aead_hooks.driver_status;
237 }
238 
mbedtls_test_transparent_aead_set_lengths(mbedtls_transparent_test_driver_aead_operation_t * operation,size_t ad_length,size_t plaintext_length)239 psa_status_t mbedtls_test_transparent_aead_set_lengths(
240     mbedtls_transparent_test_driver_aead_operation_t *operation,
241     size_t ad_length,
242     size_t plaintext_length)
243 {
244     mbedtls_test_driver_aead_hooks.hits_set_lengths++;
245 
246     if (mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS) {
247         mbedtls_test_driver_aead_hooks.driver_status =
248             mbedtls_test_driver_aead_hooks.forced_status;
249     } else {
250 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
251         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_AEAD)
252         mbedtls_test_driver_aead_hooks.driver_status =
253             libtestdriver1_mbedtls_psa_aead_set_lengths(operation, ad_length,
254                                                         plaintext_length);
255 #elif defined(MBEDTLS_PSA_BUILTIN_AEAD)
256         mbedtls_test_driver_aead_hooks.driver_status =
257             mbedtls_psa_aead_set_lengths(operation, ad_length,
258                                          plaintext_length);
259 #else
260         (void) operation;
261         (void) ad_length;
262         (void) plaintext_length;
263         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
264 #endif
265     }
266 
267     return mbedtls_test_driver_aead_hooks.driver_status;
268 }
269 
mbedtls_test_transparent_aead_update_ad(mbedtls_transparent_test_driver_aead_operation_t * operation,const uint8_t * input,size_t input_length)270 psa_status_t mbedtls_test_transparent_aead_update_ad(
271     mbedtls_transparent_test_driver_aead_operation_t *operation,
272     const uint8_t *input,
273     size_t input_length)
274 {
275     mbedtls_test_driver_aead_hooks.hits_update_ad++;
276 
277     if (mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS) {
278         mbedtls_test_driver_aead_hooks.driver_status =
279             mbedtls_test_driver_aead_hooks.forced_status;
280     } else {
281 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
282         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_AEAD)
283         mbedtls_test_driver_aead_hooks.driver_status =
284             libtestdriver1_mbedtls_psa_aead_update_ad(operation, input, input_length);
285 #elif defined(MBEDTLS_PSA_BUILTIN_AEAD)
286         mbedtls_test_driver_aead_hooks.driver_status =
287             mbedtls_psa_aead_update_ad(operation, input, input_length);
288 #else
289         (void) operation;
290         (void) input;
291         (void) input_length;
292         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
293 #endif
294     }
295 
296     return mbedtls_test_driver_aead_hooks.driver_status;
297 }
298 
mbedtls_test_transparent_aead_update(mbedtls_transparent_test_driver_aead_operation_t * operation,const uint8_t * input,size_t input_length,uint8_t * output,size_t output_size,size_t * output_length)299 psa_status_t mbedtls_test_transparent_aead_update(
300     mbedtls_transparent_test_driver_aead_operation_t *operation,
301     const uint8_t *input,
302     size_t input_length,
303     uint8_t *output,
304     size_t output_size,
305     size_t *output_length)
306 {
307     mbedtls_test_driver_aead_hooks.hits_update++;
308 
309     if (mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS) {
310         mbedtls_test_driver_aead_hooks.driver_status =
311             mbedtls_test_driver_aead_hooks.forced_status;
312     } else {
313 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
314         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_AEAD)
315         mbedtls_test_driver_aead_hooks.driver_status =
316             libtestdriver1_mbedtls_psa_aead_update(operation, input,
317                                                    input_length, output,
318                                                    output_size, output_length);
319 #elif defined(MBEDTLS_PSA_BUILTIN_AEAD)
320         mbedtls_test_driver_aead_hooks.driver_status =
321             mbedtls_psa_aead_update(operation, input, input_length, output,
322                                     output_size, output_length);
323 #else
324         (void) operation;
325         (void) input;
326         (void) input_length;
327         (void) output;
328         (void) output_size;
329         (void) output_length;
330         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
331 #endif
332     }
333 
334     return mbedtls_test_driver_aead_hooks.driver_status;
335 }
336 
mbedtls_test_transparent_aead_finish(mbedtls_transparent_test_driver_aead_operation_t * operation,uint8_t * ciphertext,size_t ciphertext_size,size_t * ciphertext_length,uint8_t * tag,size_t tag_size,size_t * tag_length)337 psa_status_t mbedtls_test_transparent_aead_finish(
338     mbedtls_transparent_test_driver_aead_operation_t *operation,
339     uint8_t *ciphertext,
340     size_t ciphertext_size,
341     size_t *ciphertext_length,
342     uint8_t *tag,
343     size_t tag_size,
344     size_t *tag_length)
345 {
346     mbedtls_test_driver_aead_hooks.hits_finish++;
347 
348     if (mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS) {
349         mbedtls_test_driver_aead_hooks.driver_status =
350             mbedtls_test_driver_aead_hooks.forced_status;
351     } else {
352 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
353         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_AEAD)
354         mbedtls_test_driver_aead_hooks.driver_status =
355             libtestdriver1_mbedtls_psa_aead_finish(operation, ciphertext,
356                                                    ciphertext_size, ciphertext_length,
357                                                    tag, tag_size, tag_length);
358 #elif defined(MBEDTLS_PSA_BUILTIN_AEAD)
359         mbedtls_test_driver_aead_hooks.driver_status =
360             mbedtls_psa_aead_finish(operation, ciphertext, ciphertext_size,
361                                     ciphertext_length, tag, tag_size,
362                                     tag_length);
363 #else
364         (void) operation;
365         (void) ciphertext;
366         (void) ciphertext_size;
367         (void) ciphertext_length;
368         (void) tag;
369         (void) tag_size;
370         (void) tag_length;
371         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
372 #endif
373     }
374 
375     return mbedtls_test_driver_aead_hooks.driver_status;
376 }
377 
mbedtls_test_transparent_aead_verify(mbedtls_transparent_test_driver_aead_operation_t * operation,uint8_t * plaintext,size_t plaintext_size,size_t * plaintext_length,const uint8_t * tag,size_t tag_length)378 psa_status_t mbedtls_test_transparent_aead_verify(
379     mbedtls_transparent_test_driver_aead_operation_t *operation,
380     uint8_t *plaintext,
381     size_t plaintext_size,
382     size_t *plaintext_length,
383     const uint8_t *tag,
384     size_t tag_length)
385 {
386     mbedtls_test_driver_aead_hooks.hits_verify++;
387 
388     if (mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS) {
389         mbedtls_test_driver_aead_hooks.driver_status =
390             mbedtls_test_driver_aead_hooks.forced_status;
391     } else {
392         uint8_t check_tag[PSA_AEAD_TAG_MAX_SIZE];
393         size_t check_tag_length = 0;
394 
395 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
396         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_AEAD)
397         mbedtls_test_driver_aead_hooks.driver_status =
398             libtestdriver1_mbedtls_psa_aead_finish(operation,
399                                                    plaintext,
400                                                    plaintext_size,
401                                                    plaintext_length,
402                                                    check_tag,
403                                                    sizeof(check_tag),
404                                                    &check_tag_length);
405 #elif defined(MBEDTLS_PSA_BUILTIN_AEAD)
406         mbedtls_test_driver_aead_hooks.driver_status =
407             mbedtls_psa_aead_finish(operation,
408                                     plaintext,
409                                     plaintext_size,
410                                     plaintext_length,
411                                     check_tag,
412                                     sizeof(check_tag),
413                                     &check_tag_length);
414 #else
415         (void) operation;
416         (void) plaintext;
417         (void) plaintext_size;
418         (void) plaintext_length;
419         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
420 #endif
421 
422         if (mbedtls_test_driver_aead_hooks.driver_status == PSA_SUCCESS) {
423             if (tag_length != check_tag_length ||
424                 mbedtls_ct_memcmp(tag, check_tag, tag_length)
425                 != 0) {
426                 mbedtls_test_driver_aead_hooks.driver_status =
427                     PSA_ERROR_INVALID_SIGNATURE;
428             }
429         }
430 
431         mbedtls_platform_zeroize(check_tag, sizeof(check_tag));
432     }
433 
434     return mbedtls_test_driver_aead_hooks.driver_status;
435 }
436 
mbedtls_test_transparent_aead_abort(mbedtls_transparent_test_driver_aead_operation_t * operation)437 psa_status_t mbedtls_test_transparent_aead_abort(
438     mbedtls_transparent_test_driver_aead_operation_t *operation)
439 {
440     mbedtls_test_driver_aead_hooks.hits_abort++;
441 
442     if (mbedtls_test_driver_aead_hooks.forced_status != PSA_SUCCESS) {
443         mbedtls_test_driver_aead_hooks.driver_status =
444             mbedtls_test_driver_aead_hooks.forced_status;
445     } else {
446 #if defined(MBEDTLS_TEST_LIBTESTDRIVER1) && \
447         defined(LIBTESTDRIVER1_MBEDTLS_PSA_BUILTIN_AEAD)
448         mbedtls_test_driver_aead_hooks.driver_status =
449             libtestdriver1_mbedtls_psa_aead_abort(operation);
450 #elif defined(MBEDTLS_PSA_BUILTIN_AEAD)
451         mbedtls_test_driver_aead_hooks.driver_status =
452             mbedtls_psa_aead_abort(operation);
453 #else
454         (void) operation;
455         mbedtls_test_driver_aead_hooks.driver_status = PSA_ERROR_NOT_SUPPORTED;
456 #endif
457     }
458 
459     return mbedtls_test_driver_aead_hooks.driver_status;
460 }
461 
462 #endif /* PSA_CRYPTO_DRIVER_TEST */
463