blob: b205c4ce01e6608f6bf4e896d83e4b4e8fee6fcd [file] [log] [blame]
Daniel Kingb8025c52016-05-17 14:43:01 -03001/* BEGIN_HEADER */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002#include "mbedtls/chachapoly.h"
Daniel Kingb8025c52016-05-17 14:43:01 -03003/* END_HEADER */
4
5/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02006 * depends_on:MBEDTLS_CHACHAPOLY_C
Daniel Kingb8025c52016-05-17 14:43:01 -03007 * END_DEPENDENCIES
8 */
9
10/* BEGIN_CASE */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020011void 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 Kingb8025c52016-05-17 14:43:01 -030012{
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é-Gonnard346b8d52018-05-07 12:56:36 +020027 mbedtls_chachapoly_context ctx;
Daniel Kingb8025c52016-05-17 14:43:01 -030028
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é-Gonnard346b8d52018-05-07 12:56:36 +020047 mbedtls_chachapoly_init( &ctx );
48
49 mbedtls_chachapoly_setkey( &ctx, key_str );
50
51 mbedtls_chachapoly_crypt_and_tag( &ctx,
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020052 MBEDTLS_CHACHAPOLY_ENCRYPT,
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +020053 input_len, nonce_str,
54 aad_str, aad_len,
55 input_str, output, mac );
Daniel Kingb8025c52016-05-17 14:43:01 -030056
57 TEST_ASSERT( memcmp( output_str, output, output_len ) == 0 );
58 TEST_ASSERT( memcmp( mac_str, mac, 16U ) == 0 );
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +020059
60exit:
61 mbedtls_chachapoly_free( &ctx );
Daniel Kingb8025c52016-05-17 14:43:01 -030062}
63/* END_CASE */
64
65/* BEGIN_CASE */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020066void 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 Kingb8025c52016-05-17 14:43:01 -030067{
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 Kingb8025c52016-05-17 14:43:01 -030075 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é-Gonnard346b8d52018-05-07 12:56:36 +020081 int ret;
82 mbedtls_chachapoly_context ctx;
Daniel Kingb8025c52016-05-17 14:43:01 -030083
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é-Gonnard346b8d52018-05-07 12:56:36 +0200102 mbedtls_chachapoly_init( &ctx );
Daniel Kingb8025c52016-05-17 14:43:01 -0300103
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200104 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 Kingb8025c52016-05-17 14:43:01 -0300112 TEST_ASSERT( memcmp( output_str, output, output_len ) == 0 );
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200113
114exit:
115 mbedtls_chachapoly_free( &ctx );
Daniel Kingb8025c52016-05-17 14:43:01 -0300116}
117/* END_CASE */
118
119/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200120void chachapoly_selftest()
Daniel Kingb8025c52016-05-17 14:43:01 -0300121{
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200122 TEST_ASSERT( mbedtls_chachapoly_self_test( 1 ) == 0 );
Daniel Kingb8025c52016-05-17 14:43:01 -0300123}
124/* END_CASE */