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