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 */ |
| 11 | void chacha20_crypt( char *hex_key_string, |
| 12 | char *hex_nonce_string, |
| 13 | int counter, |
| 14 | char *hex_src_string, |
Ronald Cron | 7e51271 | 2020-06-25 11:33:01 +0200 | [diff] [blame] | 15 | char *hex_expected_output_string ) |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 16 | { |
Manuel Pégourié-Gonnard | 528524b | 2018-05-09 11:21:21 +0200 | [diff] [blame] | 17 | unsigned char key_str[32]; /* size set by the standard */ |
| 18 | unsigned char nonce_str[12]; /* size set by the standard */ |
| 19 | unsigned char src_str[375]; /* max size of binary input */ |
Ronald Cron | 7e51271 | 2020-06-25 11:33:01 +0200 | [diff] [blame] | 20 | unsigned char expected_output_str[375]; |
| 21 | unsigned char output[375]; |
| 22 | unsigned char output_string[751]; /* ASCII string representation of output */ |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 23 | size_t key_len; |
| 24 | size_t nonce_len; |
| 25 | size_t src_len; |
Ronald Cron | 7e51271 | 2020-06-25 11:33:01 +0200 | [diff] [blame] | 26 | size_t expected_output_len; |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 27 | mbedtls_chacha20_context ctx; |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 28 | |
Ronald Cron | 7e51271 | 2020-06-25 11:33:01 +0200 | [diff] [blame] | 29 | memset( key_str, 0x00, sizeof( key_str ) ); |
| 30 | memset( nonce_str, 0x00, sizeof( nonce_str ) ); |
| 31 | memset( src_str, 0x00, sizeof( src_str ) ); |
| 32 | memset( output, 0x00, sizeof( output ) ); |
| 33 | memset( output_string, 0x00, sizeof( output_string ) ); |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 34 | |
Ronald Cron | 72d628f | 2020-06-08 17:05:57 +0200 | [diff] [blame] | 35 | key_len = mbedtls_test_unhexify( key_str, hex_key_string ); |
| 36 | nonce_len = mbedtls_test_unhexify( nonce_str, hex_nonce_string ); |
| 37 | src_len = mbedtls_test_unhexify( src_str, hex_src_string ); |
Ronald Cron | 7e51271 | 2020-06-25 11:33:01 +0200 | [diff] [blame] | 38 | expected_output_len = mbedtls_test_unhexify( expected_output_str, |
| 39 | hex_expected_output_string ); |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 40 | |
Ronald Cron | 7e51271 | 2020-06-25 11:33:01 +0200 | [diff] [blame] | 41 | TEST_ASSERT( src_len == expected_output_len ); |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 42 | TEST_ASSERT( key_len == 32U ); |
| 43 | TEST_ASSERT( nonce_len == 12U ); |
| 44 | |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 45 | /* |
| 46 | * Test the integrated API |
| 47 | */ |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 48 | TEST_ASSERT( mbedtls_chacha20_crypt( key_str, nonce_str, counter, src_len, src_str, output ) == 0 ); |
| 49 | |
Ronald Cron | 7e51271 | 2020-06-25 11:33:01 +0200 | [diff] [blame] | 50 | mbedtls_test_hexify( output_string, output, src_len ); |
| 51 | TEST_ASSERT( strcmp( (char*) output_string, hex_expected_output_string ) == 0 ); |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 52 | |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 53 | /* |
| 54 | * Test the streaming API |
| 55 | */ |
| 56 | mbedtls_chacha20_init( &ctx ); |
| 57 | |
| 58 | TEST_ASSERT( mbedtls_chacha20_setkey( &ctx, key_str ) == 0 ); |
| 59 | |
| 60 | TEST_ASSERT( mbedtls_chacha20_starts( &ctx, nonce_str, counter ) == 0 ); |
| 61 | |
| 62 | memset( output, 0x00, sizeof( output ) ); |
| 63 | TEST_ASSERT( mbedtls_chacha20_update( &ctx, src_len, src_str, output ) == 0 ); |
| 64 | |
Ronald Cron | 7e51271 | 2020-06-25 11:33:01 +0200 | [diff] [blame] | 65 | mbedtls_test_hexify( output_string, output, src_len ); |
| 66 | TEST_ASSERT( strcmp( (char*) output_string, |
| 67 | hex_expected_output_string ) == 0 ); |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * Test the streaming API again, piecewise |
| 71 | */ |
| 72 | |
Manuel Pégourié-Gonnard | 1465602 | 2018-05-09 12:51:54 +0200 | [diff] [blame] | 73 | /* Don't free/init the context nor set the key again, |
| 74 | * in order to test that starts() does the right thing. */ |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 75 | TEST_ASSERT( mbedtls_chacha20_starts( &ctx, nonce_str, counter ) == 0 ); |
| 76 | |
| 77 | memset( output, 0x00, sizeof( output ) ); |
| 78 | TEST_ASSERT( mbedtls_chacha20_update( &ctx, 1, src_str, output ) == 0 ); |
| 79 | TEST_ASSERT( mbedtls_chacha20_update( &ctx, src_len - 1, src_str + 1, output + 1 ) == 0 ); |
| 80 | |
Ronald Cron | 7e51271 | 2020-06-25 11:33:01 +0200 | [diff] [blame] | 81 | mbedtls_test_hexify( output_string, output, src_len ); |
| 82 | TEST_ASSERT( strcmp( (char*) output_string, |
| 83 | hex_expected_output_string ) == 0 ); |
Manuel Pégourié-Gonnard | 55c0d09 | 2018-05-09 12:37:58 +0200 | [diff] [blame] | 84 | |
| 85 | mbedtls_chacha20_free( &ctx ); |
Daniel King | 6155cc8 | 2016-05-18 11:51:22 -0300 | [diff] [blame] | 86 | } |
| 87 | /* END_CASE */ |
| 88 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 89 | /* 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] | 90 | void chacha20_bad_params() |
| 91 | { |
| 92 | unsigned char key[32]; |
| 93 | unsigned char nonce[12]; |
| 94 | unsigned char src[1]; |
| 95 | unsigned char dst[1]; |
| 96 | uint32_t counter = 0; |
| 97 | size_t len = sizeof( src ); |
| 98 | mbedtls_chacha20_context ctx; |
| 99 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 100 | TEST_INVALID_PARAM( mbedtls_chacha20_init( NULL ) ); |
| 101 | TEST_VALID_PARAM( mbedtls_chacha20_free( NULL ) ); |
Manuel Pégourié-Gonnard | 2aca236 | 2018-05-10 10:11:42 +0200 | [diff] [blame] | 102 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 103 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 104 | mbedtls_chacha20_setkey( NULL, key ) ); |
| 105 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 106 | mbedtls_chacha20_setkey( &ctx, NULL ) ); |
Manuel Pégourié-Gonnard | 2aca236 | 2018-05-10 10:11:42 +0200 | [diff] [blame] | 107 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 108 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 109 | mbedtls_chacha20_starts( NULL, nonce, counter ) ); |
| 110 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 111 | mbedtls_chacha20_starts( &ctx, NULL, counter ) ); |
Manuel Pégourié-Gonnard | 2aca236 | 2018-05-10 10:11:42 +0200 | [diff] [blame] | 112 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 113 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 114 | mbedtls_chacha20_update( NULL, 0, src, dst ) ); |
| 115 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 116 | mbedtls_chacha20_update( &ctx, len, NULL, dst ) ); |
| 117 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 118 | mbedtls_chacha20_update( &ctx, len, src, NULL ) ); |
Manuel Pégourié-Gonnard | 2aca236 | 2018-05-10 10:11:42 +0200 | [diff] [blame] | 119 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 120 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 121 | mbedtls_chacha20_crypt( NULL, nonce, counter, 0, src, dst ) ); |
| 122 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 123 | mbedtls_chacha20_crypt( key, NULL, counter, 0, src, dst ) ); |
| 124 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 125 | mbedtls_chacha20_crypt( key, nonce, counter, len, NULL, dst ) ); |
| 126 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA, |
| 127 | mbedtls_chacha20_crypt( key, nonce, counter, len, src, NULL ) ); |
Manuel Pégourié-Gonnard | 2aca236 | 2018-05-10 10:11:42 +0200 | [diff] [blame] | 128 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 129 | exit: |
| 130 | return; |
Manuel Pégourié-Gonnard | 2aca236 | 2018-05-10 10:11:42 +0200 | [diff] [blame] | 131 | |
Manuel Pégourié-Gonnard | 2aca236 | 2018-05-10 10:11:42 +0200 | [diff] [blame] | 132 | } |
| 133 | /* END_CASE */ |
| 134 | |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 135 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
| 136 | void chacha20_self_test() |
| 137 | { |
Manuel Pégourié-Gonnard | fce88b2 | 2018-05-09 13:06:12 +0200 | [diff] [blame] | 138 | TEST_ASSERT( mbedtls_chacha20_self_test( 1 ) == 0 ); |
Daniel King | 34b822c | 2016-05-15 17:28:08 -0300 | [diff] [blame] | 139 | } |
Manuel Pégourié-Gonnard | 528524b | 2018-05-09 11:21:21 +0200 | [diff] [blame] | 140 | /* END_CASE */ |