Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
| 2 | #include "mbedtls/chacha20.h" |
| 3 | /* END_HEADER */ |
| 4 | |
| 5 | /* BEGIN_DEPENDENCIES |
| 6 | * depends_on:MBEDTLS_CHACHA20_C |
| 7 | * END_DEPENDENCIES |
| 8 | */ |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 9 | |
| 10 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 11 | void chacha20_crypt(data_t *key_str, |
| 12 | data_t *nonce_str, |
| 13 | int counter, |
| 14 | data_t *src_str, |
| 15 | data_t *expected_output_str) |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 16 | { |
Ronald Cron | 7e51271 | 2020-06-25 11:33:01 +0200 | [diff] [blame] | 17 | unsigned char output[375]; |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 18 | mbedtls_chacha20_context ctx; |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 19 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 20 | memset(output, 0x00, sizeof(output)); |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 21 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 22 | TEST_ASSERT(src_str->len == expected_output_str->len); |
| 23 | TEST_ASSERT(key_str->len == 32U); |
| 24 | TEST_ASSERT(nonce_str->len == 12U); |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 26 | /* |
| 27 | * Test the integrated API |
| 28 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 29 | TEST_ASSERT(mbedtls_chacha20_crypt(key_str->x, nonce_str->x, counter, src_str->len, src_str->x, |
| 30 | output) == 0); |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 31 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame^] | 32 | TEST_MEMORY_COMPARE(output, expected_output_str->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 33 | expected_output_str->x, expected_output_str->len); |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 34 | |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 35 | /* |
| 36 | * Test the streaming API |
| 37 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 38 | mbedtls_chacha20_init(&ctx); |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 39 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 40 | TEST_ASSERT(mbedtls_chacha20_setkey(&ctx, key_str->x) == 0); |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 41 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 42 | TEST_ASSERT(mbedtls_chacha20_starts(&ctx, nonce_str->x, counter) == 0); |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 43 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 44 | memset(output, 0x00, sizeof(output)); |
| 45 | TEST_ASSERT(mbedtls_chacha20_update(&ctx, src_str->len, src_str->x, output) == 0); |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 46 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame^] | 47 | TEST_MEMORY_COMPARE(output, expected_output_str->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 48 | expected_output_str->x, expected_output_str->len); |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 49 | |
| 50 | /* |
| 51 | * Test the streaming API again, piecewise |
| 52 | */ |
| 53 | |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 54 | /* Don't free/init the context nor set the key again, |
| 55 | * in order to test that starts() does the right thing. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 56 | TEST_ASSERT(mbedtls_chacha20_starts(&ctx, nonce_str->x, counter) == 0); |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 57 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 58 | memset(output, 0x00, sizeof(output)); |
| 59 | TEST_ASSERT(mbedtls_chacha20_update(&ctx, 1, src_str->x, output) == 0); |
| 60 | TEST_ASSERT(mbedtls_chacha20_update(&ctx, src_str->len - 1, |
| 61 | src_str->x + 1, output + 1) == 0); |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 62 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame^] | 63 | TEST_MEMORY_COMPARE(output, expected_output_str->len, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 64 | expected_output_str->x, expected_output_str->len); |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 65 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | mbedtls_chacha20_free(&ctx); |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 67 | } |
| 68 | /* END_CASE */ |
| 69 | |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 70 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
| 71 | void chacha20_self_test() |
| 72 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 73 | TEST_ASSERT(mbedtls_chacha20_self_test(1) == 0); |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 74 | } |
Manuel Pégourié-Gonnard | 528524b | 2018-05-09 11:21:21 +0200 | [diff] [blame] | 75 | /* END_CASE */ |