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; |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 27 | mbedtls_chachapoly_context ctx; |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 28 | |
| 29 | memset( key_str, 0x00, 32 ); |
| 30 | memset( nonce_str, 0x00, 12 ); |
| 31 | memset( aad_str, 0x00, 10000 ); |
| 32 | memset( input_str, 0x00, 10000 ); |
| 33 | memset( output_str, 0x00, 10000 ); |
| 34 | memset( mac_str, 0x00, 16 ); |
| 35 | |
| 36 | aad_len = unhexify( aad_str, hex_aad_string ); |
| 37 | input_len = unhexify( input_str, hex_input_string ); |
| 38 | output_len = unhexify( output_str, hex_output_string ); |
| 39 | key_len = unhexify( key_str, hex_key_string ); |
| 40 | nonce_len = unhexify( nonce_str, hex_nonce_string ); |
| 41 | mac_len = unhexify( mac_str, hex_mac_string ); |
| 42 | |
| 43 | TEST_ASSERT( key_len == 32 ); |
| 44 | TEST_ASSERT( nonce_len == 12 ); |
| 45 | TEST_ASSERT( mac_len == 16 ); |
| 46 | |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 47 | mbedtls_chachapoly_init( &ctx ); |
| 48 | |
| 49 | mbedtls_chachapoly_setkey( &ctx, key_str ); |
| 50 | |
| 51 | mbedtls_chachapoly_crypt_and_tag( &ctx, |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 52 | MBEDTLS_CHACHAPOLY_ENCRYPT, |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 53 | input_len, nonce_str, |
| 54 | aad_str, aad_len, |
| 55 | input_str, output, mac ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 56 | |
| 57 | TEST_ASSERT( memcmp( output_str, output, output_len ) == 0 ); |
| 58 | TEST_ASSERT( memcmp( mac_str, mac, 16U ) == 0 ); |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 59 | |
| 60 | exit: |
| 61 | mbedtls_chachapoly_free( &ctx ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 62 | } |
| 63 | /* END_CASE */ |
| 64 | |
| 65 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 66 | 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] | 67 | { |
| 68 | unsigned char key_str[32]; |
| 69 | unsigned char nonce_str[12]; |
| 70 | unsigned char aad_str[10000]; |
| 71 | unsigned char input_str[10000]; |
| 72 | unsigned char output_str[10000]; |
| 73 | unsigned char mac_str[16]; |
| 74 | unsigned char output[10000]; |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 75 | size_t input_len; |
| 76 | size_t output_len; |
| 77 | size_t aad_len; |
| 78 | size_t key_len; |
| 79 | size_t nonce_len; |
| 80 | size_t mac_len; |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 81 | int ret; |
| 82 | mbedtls_chachapoly_context ctx; |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 83 | |
| 84 | memset( key_str, 0x00, 32 ); |
| 85 | memset( nonce_str, 0x00, 12 ); |
| 86 | memset( aad_str, 0x00, 10000 ); |
| 87 | memset( input_str, 0x00, 10000 ); |
| 88 | memset( output_str, 0x00, 10000 ); |
| 89 | memset( mac_str, 0x00, 16 ); |
| 90 | |
| 91 | aad_len = unhexify( aad_str, hex_aad_string ); |
| 92 | input_len = unhexify( input_str, hex_input_string ); |
| 93 | output_len = unhexify( output_str, hex_output_string ); |
| 94 | key_len = unhexify( key_str, hex_key_string ); |
| 95 | nonce_len = unhexify( nonce_str, hex_nonce_string ); |
| 96 | mac_len = unhexify( mac_str, hex_mac_string ); |
| 97 | |
| 98 | TEST_ASSERT( key_len == 32 ); |
| 99 | TEST_ASSERT( nonce_len == 12 ); |
| 100 | TEST_ASSERT( mac_len == 16 ); |
| 101 | |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 102 | mbedtls_chachapoly_init( &ctx ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 103 | |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 104 | mbedtls_chachapoly_setkey( &ctx, key_str ); |
| 105 | |
| 106 | ret = mbedtls_chachapoly_auth_decrypt( &ctx, |
| 107 | input_len, nonce_str, |
| 108 | aad_str, aad_len, |
| 109 | mac_str, input_str, output ); |
| 110 | |
| 111 | TEST_ASSERT( ret == 0 ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 112 | TEST_ASSERT( memcmp( output_str, output, output_len ) == 0 ); |
Manuel Pégourié-Gonnard | 346b8d5 | 2018-05-07 12:56:36 +0200 | [diff] [blame] | 113 | |
| 114 | exit: |
| 115 | mbedtls_chachapoly_free( &ctx ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 116 | } |
| 117 | /* END_CASE */ |
| 118 | |
| 119 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 120 | void chachapoly_selftest() |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 121 | { |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 122 | TEST_ASSERT( mbedtls_chachapoly_self_test( 1 ) == 0 ); |
Daniel King | b8025c5 | 2016-05-17 14:43:01 -0300 | [diff] [blame] | 123 | } |
| 124 | /* END_CASE */ |