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