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