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