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 */ |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 11 | void chacha20_crypt( data_t *key_str, |
| 12 | data_t *nonce_str, |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 13 | int counter, |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 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 | |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 20 | memset( output, 0x00, sizeof( output ) ); |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 21 | |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [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 | */ |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 29 | TEST_ASSERT( mbedtls_chacha20_crypt( key_str->x, nonce_str->x, counter, src_str->len, src_str->x, output ) == 0 ); |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 30 | |
Ronald Cron | 55d97f2 | 2020-06-26 17:00:30 +0200 | [diff] [blame] | 31 | TEST_ASSERT( !memcmp( output, expected_output_str->x, |
| 32 | expected_output_str->len ) ); |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 33 | |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 34 | /* |
| 35 | * Test the streaming API |
| 36 | */ |
| 37 | mbedtls_chacha20_init( &ctx ); |
| 38 | |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 39 | TEST_ASSERT( mbedtls_chacha20_setkey( &ctx, key_str->x ) == 0 ); |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 40 | |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 41 | 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] | 42 | |
| 43 | memset( output, 0x00, sizeof( output ) ); |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 44 | 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] | 45 | |
Ronald Cron | 55d97f2 | 2020-06-26 17:00:30 +0200 | [diff] [blame] | 46 | TEST_ASSERT( !memcmp( output, expected_output_str->x, |
| 47 | expected_output_str->len ) ); |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * Test the streaming API again, piecewise |
| 51 | */ |
| 52 | |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 53 | /* Don't free/init the context nor set the key again, |
| 54 | * in order to test that starts() does the right thing. */ |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 55 | 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] | 56 | |
| 57 | memset( output, 0x00, sizeof( output ) ); |
Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 58 | TEST_ASSERT( mbedtls_chacha20_update( &ctx, 1, src_str->x, output ) == 0 ); |
| 59 | TEST_ASSERT( mbedtls_chacha20_update( &ctx, src_str->len - 1, |
| 60 | src_str->x + 1, output + 1 ) == 0 ); |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 61 | |
Ronald Cron | 55d97f2 | 2020-06-26 17:00:30 +0200 | [diff] [blame] | 62 | TEST_ASSERT( !memcmp( output, expected_output_str->x, |
| 63 | expected_output_str->len ) ); |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 64 | |
| 65 | mbedtls_chacha20_free( &ctx ); |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 66 | } |
| 67 | /* END_CASE */ |
| 68 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 69 | /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */ |
Manuel Pégourié-Gonnard | 2aca236 | 2018-05-10 10:11:42 +0200 | [diff] [blame] | 70 | void chacha20_bad_params() |
| 71 | { |
| 72 | unsigned char key[32]; |
| 73 | unsigned char nonce[12]; |
| 74 | unsigned char src[1]; |
| 75 | unsigned char dst[1]; |
| 76 | uint32_t counter = 0; |
| 77 | size_t len = sizeof( src ); |
| 78 | mbedtls_chacha20_context ctx; |
| 79 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 80 | TEST_INVALID_PARAM( mbedtls_chacha20_init( NULL ) ); |
| 81 | TEST_VALID_PARAM( mbedtls_chacha20_free( NULL ) ); |
Manuel Pégourié-Gonnard | 2aca236 | 2018-05-10 10:11:42 +0200 | [diff] [blame] | 82 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 83 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 84 | mbedtls_chacha20_setkey( NULL, key ) ); |
| 85 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 86 | mbedtls_chacha20_setkey( &ctx, NULL ) ); |
Manuel Pégourié-Gonnard | 2aca236 | 2018-05-10 10:11:42 +0200 | [diff] [blame] | 87 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 88 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 89 | mbedtls_chacha20_starts( NULL, nonce, counter ) ); |
| 90 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 91 | mbedtls_chacha20_starts( &ctx, NULL, counter ) ); |
Manuel Pégourié-Gonnard | 2aca236 | 2018-05-10 10:11:42 +0200 | [diff] [blame] | 92 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 93 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 94 | mbedtls_chacha20_update( NULL, 0, src, dst ) ); |
| 95 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 96 | mbedtls_chacha20_update( &ctx, len, NULL, dst ) ); |
| 97 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 98 | mbedtls_chacha20_update( &ctx, len, src, NULL ) ); |
Manuel Pégourié-Gonnard | 2aca236 | 2018-05-10 10:11:42 +0200 | [diff] [blame] | 99 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 100 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 101 | mbedtls_chacha20_crypt( NULL, nonce, counter, 0, src, dst ) ); |
| 102 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 103 | mbedtls_chacha20_crypt( key, NULL, counter, 0, src, dst ) ); |
| 104 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 105 | mbedtls_chacha20_crypt( key, nonce, counter, len, NULL, dst ) ); |
| 106 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 107 | mbedtls_chacha20_crypt( key, nonce, counter, len, src, NULL ) ); |
Manuel Pégourié-Gonnard | 2aca236 | 2018-05-10 10:11:42 +0200 | [diff] [blame] | 108 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 109 | exit: |
| 110 | return; |
Manuel Pégourié-Gonnard | 2aca236 | 2018-05-10 10:11:42 +0200 | [diff] [blame] | 111 | |
Manuel Pégourié-Gonnard | 2aca236 | 2018-05-10 10:11:42 +0200 | [diff] [blame] | 112 | } |
| 113 | /* END_CASE */ |
| 114 | |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 115 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
| 116 | void chacha20_self_test() |
| 117 | { |
Manuel Pégourié-Gonnard | fce88b2 | 2018-05-09 13:06:12 +0200 | [diff] [blame] | 118 | TEST_ASSERT( mbedtls_chacha20_self_test( 1 ) == 0 ); |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 119 | } |
Manuel Pégourié-Gonnard | 528524b | 2018-05-09 11:21:21 +0200 | [diff] [blame] | 120 | /* END_CASE */ |