blob: a613870b3575961cb56ea591cc1d8b41908e9975 [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{
Manuel Pégourié-Gonnard528524b2018-05-09 11:21:21 +020013 unsigned char key_str[32]; /* size set by the standard */
14 unsigned char nonce_str[12]; /* size set by the standard */
15 unsigned char aad_str[12]; /* max size of test data so far */
16 unsigned char input_str[265]; /* max size of binary input/output so far */
17 unsigned char output_str[265];
18 unsigned char output[265];
19 unsigned char mac_str[16]; /* size set by the standard */
20 unsigned char mac[16]; /* size set by the standard */
Daniel Kingb8025c52016-05-17 14:43:01 -030021 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
Manuel Pégourié-Gonnard528524b2018-05-09 11:21:21 +020029 memset( key_str, 0x00, sizeof( key_str ) );
30 memset( nonce_str, 0x00, sizeof( nonce_str ) );
31 memset( aad_str, 0x00, sizeof( aad_str ) );
32 memset( input_str, 0x00, sizeof( input_str ) );
33 memset( output_str, 0x00, sizeof( output_str ) );
34 memset( mac_str, 0x00, sizeof( mac_str ) );
Daniel Kingb8025c52016-05-17 14:43:01 -030035
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é-Gonnard72967712018-05-09 12:22:13 +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, int ret_exp )
Daniel Kingb8025c52016-05-17 14:43:01 -030067{
Manuel Pégourié-Gonnard528524b2018-05-09 11:21:21 +020068 unsigned char key_str[32]; /* size set by the standard */
69 unsigned char nonce_str[12]; /* size set by the standard */
70 unsigned char aad_str[12]; /* max size of test data so far */
71 unsigned char input_str[265]; /* max size of binary input/output so far */
72 unsigned char output_str[265];
73 unsigned char output[265];
74 unsigned char mac_str[16]; /* size set by the standard */
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
Manuel Pégourié-Gonnard528524b2018-05-09 11:21:21 +020084 memset( key_str, 0x00, sizeof( key_str ) );
85 memset( nonce_str, 0x00, sizeof( nonce_str ) );
86 memset( aad_str, 0x00, sizeof( aad_str ) );
87 memset( input_str, 0x00, sizeof( input_str ) );
88 memset( output_str, 0x00, sizeof( output_str ) );
89 memset( mac_str, 0x00, sizeof( mac_str ) );
Daniel Kingb8025c52016-05-17 14:43:01 -030090
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
Manuel Pégourié-Gonnard72967712018-05-09 12:22:13 +0200111 TEST_ASSERT( ret == ret_exp );
112 if( ret_exp == 0 )
113 {
114 TEST_ASSERT( memcmp( output_str, output, output_len ) == 0 );
115 }
Manuel Pégourié-Gonnard346b8d52018-05-07 12:56:36 +0200116
117exit:
118 mbedtls_chachapoly_free( &ctx );
Daniel Kingb8025c52016-05-17 14:43:01 -0300119}
120/* END_CASE */
121
122/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200123void chachapoly_selftest()
Daniel Kingb8025c52016-05-17 14:43:01 -0300124{
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200125 TEST_ASSERT( mbedtls_chachapoly_self_test( 1 ) == 0 );
Daniel Kingb8025c52016-05-17 14:43:01 -0300126}
127/* END_CASE */