blob: c9cc1774e88032cd4d19903b491e438edac0a411 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Paul Bakker89e80c92012-03-20 13:50:09 +00002#include <polarssl/gcm.h>
Paul Bakker33b43f12013-08-20 11:48:36 +02003/* END_HEADER */
Paul Bakker89e80c92012-03-20 13:50:09 +00004
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* BEGIN_DEPENDENCIES
6 * depends_on:POLARSSL_GCM_C
7 * END_DEPENDENCIES
8 */
Paul Bakker89e80c92012-03-20 13:50:09 +00009
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
11void gcm_encrypt_and_tag( char *hex_key_string, char *hex_src_string,
12 char *hex_iv_string, char *hex_add_string,
13 char *hex_dst_string, int tag_len_bits,
14 char *hex_tag_string, int init_result )
Paul Bakker89e80c92012-03-20 13:50:09 +000015{
16 unsigned char key_str[128];
17 unsigned char src_str[128];
18 unsigned char dst_str[257];
19 unsigned char iv_str[128];
20 unsigned char add_str[128];
21 unsigned char tag_str[128];
22 unsigned char output[128];
23 unsigned char tag_output[16];
24 gcm_context ctx;
25 unsigned int key_len;
Paul Bakker33b43f12013-08-20 11:48:36 +020026 size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
Paul Bakker89e80c92012-03-20 13:50:09 +000027
28 memset(key_str, 0x00, 128);
29 memset(src_str, 0x00, 128);
Paul Bakker68b6d882012-09-08 14:04:13 +000030 memset(dst_str, 0x00, 257);
Paul Bakker89e80c92012-03-20 13:50:09 +000031 memset(iv_str, 0x00, 128);
32 memset(add_str, 0x00, 128);
33 memset(tag_str, 0x00, 128);
34 memset(output, 0x00, 128);
35 memset(tag_output, 0x00, 16);
36
Paul Bakker33b43f12013-08-20 11:48:36 +020037 key_len = unhexify( key_str, hex_key_string );
38 pt_len = unhexify( src_str, hex_src_string );
39 iv_len = unhexify( iv_str, hex_iv_string );
40 add_len = unhexify( add_str, hex_add_string );
Paul Bakker89e80c92012-03-20 13:50:09 +000041
Paul Bakker43aff2a2013-09-09 00:10:27 +020042 TEST_ASSERT( gcm_init( &ctx, POLARSSL_CIPHER_ID_AES, key_str, key_len * 8 ) == init_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020043 if( init_result == 0 )
Paul Bakker89e80c92012-03-20 13:50:09 +000044 {
45 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 );
46 hexify( dst_str, output, pt_len );
47 hexify( tag_str, tag_output, tag_len );
48
Paul Bakker33b43f12013-08-20 11:48:36 +020049 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
50 TEST_ASSERT( strcmp( (char *) tag_str, hex_tag_string ) == 0 );
Paul Bakker89e80c92012-03-20 13:50:09 +000051 }
52}
Paul Bakker33b43f12013-08-20 11:48:36 +020053/* END_CASE */
Paul Bakker89e80c92012-03-20 13:50:09 +000054
Paul Bakker33b43f12013-08-20 11:48:36 +020055/* BEGIN_CASE */
56void gcm_decrypt_and_verify( char *hex_key_string, char *hex_src_string,
57 char *hex_iv_string, char *hex_add_string,
58 int tag_len_bits, char *hex_tag_string,
59 char *pt_result, int init_result )
Paul Bakker89e80c92012-03-20 13:50:09 +000060{
61 unsigned char key_str[128];
62 unsigned char src_str[128];
63 unsigned char dst_str[257];
64 unsigned char iv_str[128];
65 unsigned char add_str[128];
66 unsigned char tag_str[128];
67 unsigned char output[128];
68 gcm_context ctx;
69 unsigned int key_len;
Paul Bakker33b43f12013-08-20 11:48:36 +020070 size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
Paul Bakker89e80c92012-03-20 13:50:09 +000071 int ret;
72
73 memset(key_str, 0x00, 128);
74 memset(src_str, 0x00, 128);
Paul Bakker68b6d882012-09-08 14:04:13 +000075 memset(dst_str, 0x00, 257);
Paul Bakker89e80c92012-03-20 13:50:09 +000076 memset(iv_str, 0x00, 128);
77 memset(add_str, 0x00, 128);
78 memset(tag_str, 0x00, 128);
79 memset(output, 0x00, 128);
80
Paul Bakker33b43f12013-08-20 11:48:36 +020081 key_len = unhexify( key_str, hex_key_string );
82 pt_len = unhexify( src_str, hex_src_string );
83 iv_len = unhexify( iv_str, hex_iv_string );
84 add_len = unhexify( add_str, hex_add_string );
85 unhexify( tag_str, hex_tag_string );
Paul Bakker89e80c92012-03-20 13:50:09 +000086
Paul Bakker43aff2a2013-09-09 00:10:27 +020087 TEST_ASSERT( gcm_init( &ctx, POLARSSL_CIPHER_ID_AES, key_str, key_len * 8 ) == init_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020088 if( init_result == 0 )
Paul Bakker89e80c92012-03-20 13:50:09 +000089 {
90 ret = gcm_auth_decrypt( &ctx, pt_len, iv_str, iv_len, add_str, add_len, tag_str, tag_len, src_str, output );
91
Paul Bakker33b43f12013-08-20 11:48:36 +020092 if( strcmp( "FAIL", pt_result ) == 0 )
Paul Bakker89e80c92012-03-20 13:50:09 +000093 {
94 TEST_ASSERT( ret == POLARSSL_ERR_GCM_AUTH_FAILED );
95 }
96 else
97 {
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +020098 TEST_ASSERT( ret == 0 );
Paul Bakker89e80c92012-03-20 13:50:09 +000099 hexify( dst_str, output, pt_len );
100
Paul Bakker33b43f12013-08-20 11:48:36 +0200101 TEST_ASSERT( strcmp( (char *) dst_str, pt_result ) == 0 );
Paul Bakker89e80c92012-03-20 13:50:09 +0000102 }
103 }
104}
Paul Bakker33b43f12013-08-20 11:48:36 +0200105/* END_CASE */
Paul Bakker89e80c92012-03-20 13:50:09 +0000106
Paul Bakker33b43f12013-08-20 11:48:36 +0200107/* BEGIN_CASE */
108void gcm_selftest()
Paul Bakker89e80c92012-03-20 13:50:09 +0000109{
110 TEST_ASSERT( gcm_self_test( 0 ) == 0 );
111}
Paul Bakker33b43f12013-08-20 11:48:36 +0200112/* END_CASE */