blob: 1c02407b7a740c29bd9303beaac5a723f6c72ea8 [file] [log] [blame]
Paul Bakker89e80c92012-03-20 13:50:09 +00001BEGIN_HEADER
2#include <polarssl/gcm.h>
3END_HEADER
4
5BEGIN_DEPENDENCIES
6depends_on:POLARSSL_GCM_C
7END_DEPENDENCIES
8
9BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +020010gcm_encrypt_and_tag:hex_key_string:hex_src_string:hex_iv_string:hex_add_string:hex_dst_string:#tag_len_bits:hex_tag_string:#init_result
Paul Bakker89e80c92012-03-20 13:50:09 +000011{
12 unsigned char key_str[128];
13 unsigned char src_str[128];
14 unsigned char dst_str[257];
15 unsigned char iv_str[128];
16 unsigned char add_str[128];
17 unsigned char tag_str[128];
18 unsigned char output[128];
19 unsigned char tag_output[16];
20 gcm_context ctx;
21 unsigned int key_len;
Paul Bakkerdbd443d2013-08-16 13:38:47 +020022 size_t pt_len, iv_len, add_len, tag_len = {tag_len_bits} / 8;
Paul Bakker89e80c92012-03-20 13:50:09 +000023
24 memset(key_str, 0x00, 128);
25 memset(src_str, 0x00, 128);
Paul Bakker68b6d882012-09-08 14:04:13 +000026 memset(dst_str, 0x00, 257);
Paul Bakker89e80c92012-03-20 13:50:09 +000027 memset(iv_str, 0x00, 128);
28 memset(add_str, 0x00, 128);
29 memset(tag_str, 0x00, 128);
30 memset(output, 0x00, 128);
31 memset(tag_output, 0x00, 16);
32
33 key_len = unhexify( key_str, {hex_key_string} );
34 pt_len = unhexify( src_str, {hex_src_string} );
35 iv_len = unhexify( iv_str, {hex_iv_string} );
36 add_len = unhexify( add_str, {hex_add_string} );
37
38 TEST_ASSERT( gcm_init( &ctx, key_str, key_len * 8 ) == {init_result} );
39 if( {init_result} == 0 )
40 {
41 TEST_ASSERT( gcm_crypt_and_tag( &ctx, GCM_ENCRYPT, pt_len, iv_str, iv_len, add_str, add_len, src_str, output, tag_len, tag_output ) == 0 );
42 hexify( dst_str, output, pt_len );
43 hexify( tag_str, tag_output, tag_len );
44
45 TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 );
46 TEST_ASSERT( strcmp( (char *) tag_str, {hex_tag_string} ) == 0 );
47 }
48}
49END_CASE
50
51BEGIN_CASE
Paul Bakkerdbd443d2013-08-16 13:38:47 +020052gcm_decrypt_and_verify:hex_key_string:hex_src_string:hex_iv_string:hex_add_string:#tag_len_bits:hex_tag_string:pt_result:#init_result
Paul Bakker89e80c92012-03-20 13:50:09 +000053{
54 unsigned char key_str[128];
55 unsigned char src_str[128];
56 unsigned char dst_str[257];
57 unsigned char iv_str[128];
58 unsigned char add_str[128];
59 unsigned char tag_str[128];
60 unsigned char output[128];
61 gcm_context ctx;
62 unsigned int key_len;
Paul Bakkerdbd443d2013-08-16 13:38:47 +020063 size_t pt_len, iv_len, add_len, tag_len = {tag_len_bits} / 8;
Paul Bakker89e80c92012-03-20 13:50:09 +000064 int ret;
65
66 memset(key_str, 0x00, 128);
67 memset(src_str, 0x00, 128);
Paul Bakker68b6d882012-09-08 14:04:13 +000068 memset(dst_str, 0x00, 257);
Paul Bakker89e80c92012-03-20 13:50:09 +000069 memset(iv_str, 0x00, 128);
70 memset(add_str, 0x00, 128);
71 memset(tag_str, 0x00, 128);
72 memset(output, 0x00, 128);
73
74 key_len = unhexify( key_str, {hex_key_string} );
75 pt_len = unhexify( src_str, {hex_src_string} );
76 iv_len = unhexify( iv_str, {hex_iv_string} );
77 add_len = unhexify( add_str, {hex_add_string} );
78 unhexify( tag_str, {hex_tag_string} );
79
80 TEST_ASSERT( gcm_init( &ctx, key_str, key_len * 8 ) == {init_result} );
81 if( {init_result} == 0 )
82 {
83 ret = gcm_auth_decrypt( &ctx, pt_len, iv_str, iv_len, add_str, add_len, tag_str, tag_len, src_str, output );
84
85 if( strcmp( "FAIL", {pt_result} ) == 0 )
86 {
87 TEST_ASSERT( ret == POLARSSL_ERR_GCM_AUTH_FAILED );
88 }
89 else
90 {
91 hexify( dst_str, output, pt_len );
92
93 TEST_ASSERT( strcmp( (char *) dst_str, {pt_result} ) == 0 );
94 }
95 }
96}
97END_CASE
98
99BEGIN_CASE
100gcm_selftest:
101{
102 TEST_ASSERT( gcm_self_test( 0 ) == 0 );
103}
104END_CASE