1/* BEGIN_HEADER */
2#include "mbedtls/gcm.h"
3/* END_HEADER */
4
5/* BEGIN_DEPENDENCIES
6 * depends_on:MBEDTLS_GCM_C
7 * END_DEPENDENCIES
8 */
9
10/* BEGIN_CASE */
11void gcm_encrypt_and_tag( int cipher_id,
12                          char *hex_key_string, char *hex_src_string,
13                          char *hex_iv_string, char *hex_add_string,
14                          char *hex_dst_string, int tag_len_bits,
15                          char *hex_tag_string, int  init_result )
16{
17    unsigned char key_str[128];
18    unsigned char src_str[128];
19    unsigned char dst_str[257];
20    unsigned char iv_str[128];
21    unsigned char add_str[128];
22    unsigned char tag_str[128];
23    unsigned char output[128];
24    unsigned char tag_output[16];
25    mbedtls_gcm_context ctx;
26    unsigned int key_len;
27    size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
28
29    mbedtls_gcm_init( &ctx );
30
31    memset(key_str, 0x00, 128);
32    memset(src_str, 0x00, 128);
33    memset(dst_str, 0x00, 257);
34    memset(iv_str, 0x00, 128);
35    memset(add_str, 0x00, 128);
36    memset(tag_str, 0x00, 128);
37    memset(output, 0x00, 128);
38    memset(tag_output, 0x00, 16);
39
40    key_len = unhexify( key_str, hex_key_string );
41    pt_len = unhexify( src_str, hex_src_string );
42    iv_len = unhexify( iv_str, hex_iv_string );
43    add_len = unhexify( add_str, hex_add_string );
44
45    TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == init_result );
46    if( init_result == 0 )
47    {
48        TEST_ASSERT( mbedtls_gcm_crypt_and_tag( &ctx, MBEDTLS_GCM_ENCRYPT, pt_len, iv_str, iv_len, add_str, add_len, src_str, output, tag_len, tag_output ) == 0 );
49        hexify( dst_str, output, pt_len );
50        hexify( tag_str, tag_output, tag_len );
51
52        TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
53        TEST_ASSERT( strcmp( (char *) tag_str, hex_tag_string ) == 0 );
54    }
55
56exit:
57    mbedtls_gcm_free( &ctx );
58}
59/* END_CASE */
60
61/* BEGIN_CASE */
62void gcm_decrypt_and_verify( int cipher_id,
63                             char *hex_key_string, char *hex_src_string,
64                             char *hex_iv_string, char *hex_add_string,
65                             int tag_len_bits, char *hex_tag_string,
66                             char *pt_result, int init_result )
67{
68    unsigned char key_str[128];
69    unsigned char src_str[128];
70    unsigned char dst_str[257];
71    unsigned char iv_str[128];
72    unsigned char add_str[128];
73    unsigned char tag_str[128];
74    unsigned char output[128];
75    mbedtls_gcm_context ctx;
76    unsigned int key_len;
77    size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
78    int ret;
79
80    mbedtls_gcm_init( &ctx );
81
82    memset(key_str, 0x00, 128);
83    memset(src_str, 0x00, 128);
84    memset(dst_str, 0x00, 257);
85    memset(iv_str, 0x00, 128);
86    memset(add_str, 0x00, 128);
87    memset(tag_str, 0x00, 128);
88    memset(output, 0x00, 128);
89
90    key_len = unhexify( key_str, hex_key_string );
91    pt_len = unhexify( src_str, hex_src_string );
92    iv_len = unhexify( iv_str, hex_iv_string );
93    add_len = unhexify( add_str, hex_add_string );
94    unhexify( tag_str, hex_tag_string );
95
96    TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == init_result );
97    if( init_result == 0 )
98    {
99        ret = mbedtls_gcm_auth_decrypt( &ctx, pt_len, iv_str, iv_len, add_str, add_len, tag_str, tag_len, src_str, output );
100
101        if( strcmp( "FAIL", pt_result ) == 0 )
102        {
103            TEST_ASSERT( ret == MBEDTLS_ERR_GCM_AUTH_FAILED );
104        }
105        else
106        {
107            TEST_ASSERT( ret == 0 );
108            hexify( dst_str, output, pt_len );
109
110            TEST_ASSERT( strcmp( (char *) dst_str, pt_result ) == 0 );
111        }
112    }
113
114exit:
115    mbedtls_gcm_free( &ctx );
116}
117/* END_CASE */
118
119/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
120void gcm_selftest()
121{
122    TEST_ASSERT( mbedtls_gcm_self_test( 1 ) == 0 );
123}
124/* END_CASE */
125