Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 2 | #include "mbedtls/chachapoly.h" |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 3 | /* END_HEADER */ |
| 4 | |
| 5 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 6 | * depends_on:MBEDTLS_CHACHAPOLY_C |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 7 | * END_DEPENDENCIES |
| 8 | */ |
| 9 | |
| 10 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 11 | void mbedtls_chachapoly_enc( char *hex_key_string, char *hex_nonce_string, char *hex_aad_string, char *hex_input_string, char *hex_output_string, char *hex_mac_string ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 12 | { |
| 13 | unsigned char key_str[32]; |
| 14 | unsigned char nonce_str[12]; |
| 15 | unsigned char aad_str[10000]; |
| 16 | unsigned char input_str[10000]; |
| 17 | unsigned char output_str[10000]; |
| 18 | unsigned char mac_str[16]; |
| 19 | unsigned char output[10000]; |
| 20 | unsigned char mac[16]; |
| 21 | size_t input_len; |
| 22 | size_t output_len; |
| 23 | size_t aad_len; |
| 24 | size_t key_len; |
| 25 | size_t nonce_len; |
| 26 | size_t mac_len; |
| 27 | |
| 28 | memset( key_str, 0x00, 32 ); |
| 29 | memset( nonce_str, 0x00, 12 ); |
| 30 | memset( aad_str, 0x00, 10000 ); |
| 31 | memset( input_str, 0x00, 10000 ); |
| 32 | memset( output_str, 0x00, 10000 ); |
| 33 | memset( mac_str, 0x00, 16 ); |
| 34 | |
| 35 | aad_len = unhexify( aad_str, hex_aad_string ); |
| 36 | input_len = unhexify( input_str, hex_input_string ); |
| 37 | output_len = unhexify( output_str, hex_output_string ); |
| 38 | key_len = unhexify( key_str, hex_key_string ); |
| 39 | nonce_len = unhexify( nonce_str, hex_nonce_string ); |
| 40 | mac_len = unhexify( mac_str, hex_mac_string ); |
| 41 | |
| 42 | TEST_ASSERT( key_len == 32 ); |
| 43 | TEST_ASSERT( nonce_len == 12 ); |
| 44 | TEST_ASSERT( mac_len == 16 ); |
| 45 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 46 | mbedtls_chachapoly_crypt_and_mac( key_str, nonce_str, |
| 47 | MBEDTLS_CHACHAPOLY_ENCRYPT, |
| 48 | aad_len, aad_str, |
| 49 | input_len, input_str, output, |
| 50 | mac ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 51 | |
| 52 | TEST_ASSERT( memcmp( output_str, output, output_len ) == 0 ); |
| 53 | TEST_ASSERT( memcmp( mac_str, mac, 16U ) == 0 ); |
| 54 | } |
| 55 | /* END_CASE */ |
| 56 | |
| 57 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 58 | void mbedtls_chachapoly_dec( char *hex_key_string, char *hex_nonce_string, char *hex_aad_string, char *hex_input_string, char *hex_output_string, char *hex_mac_string ) |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 59 | { |
| 60 | unsigned char key_str[32]; |
| 61 | unsigned char nonce_str[12]; |
| 62 | unsigned char aad_str[10000]; |
| 63 | unsigned char input_str[10000]; |
| 64 | unsigned char output_str[10000]; |
| 65 | unsigned char mac_str[16]; |
| 66 | unsigned char output[10000]; |
| 67 | unsigned char mac[16]; |
| 68 | size_t input_len; |
| 69 | size_t output_len; |
| 70 | size_t aad_len; |
| 71 | size_t key_len; |
| 72 | size_t nonce_len; |
| 73 | size_t mac_len; |
| 74 | |
| 75 | memset( key_str, 0x00, 32 ); |
| 76 | memset( nonce_str, 0x00, 12 ); |
| 77 | memset( aad_str, 0x00, 10000 ); |
| 78 | memset( input_str, 0x00, 10000 ); |
| 79 | memset( output_str, 0x00, 10000 ); |
| 80 | memset( mac_str, 0x00, 16 ); |
| 81 | |
| 82 | aad_len = unhexify( aad_str, hex_aad_string ); |
| 83 | input_len = unhexify( input_str, hex_input_string ); |
| 84 | output_len = unhexify( output_str, hex_output_string ); |
| 85 | key_len = unhexify( key_str, hex_key_string ); |
| 86 | nonce_len = unhexify( nonce_str, hex_nonce_string ); |
| 87 | mac_len = unhexify( mac_str, hex_mac_string ); |
| 88 | |
| 89 | TEST_ASSERT( key_len == 32 ); |
| 90 | TEST_ASSERT( nonce_len == 12 ); |
| 91 | TEST_ASSERT( mac_len == 16 ); |
| 92 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 93 | mbedtls_chachapoly_crypt_and_mac( key_str, nonce_str, |
| 94 | MBEDTLS_CHACHAPOLY_DECRYPT, |
| 95 | aad_len, aad_str, |
| 96 | input_len, input_str, output, |
| 97 | mac ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 98 | |
| 99 | TEST_ASSERT( memcmp( output_str, output, output_len ) == 0 ); |
| 100 | TEST_ASSERT( memcmp( mac_str, mac, 16U ) == 0 ); |
| 101 | } |
| 102 | /* END_CASE */ |
| 103 | |
| 104 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 105 | void chachapoly_selftest() |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 106 | { |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame^] | 107 | TEST_ASSERT( mbedtls_chachapoly_self_test( 1 ) == 0 ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 108 | } |
| 109 | /* END_CASE */ |