blob: 9d841dc12aee7b87dd3e2adb6d8126054ac652f1 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006 * depends_on:MBEDTLS_GCM_C
Paul Bakker33b43f12013-08-20 11:48:36 +02007 * END_DEPENDENCIES
8 */
Paul Bakker89e80c92012-03-20 13:50:09 +00009
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
Manuel Pégourié-Gonnard083d6682013-10-24 12:06:54 +020011void gcm_encrypt_and_tag( int cipher_id,
12 char *hex_key_string, char *hex_src_string,
Paul Bakker33b43f12013-08-20 11:48:36 +020013 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 )
Paul Bakker89e80c92012-03-20 13:50:09 +000016{
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];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025 mbedtls_gcm_context ctx;
Paul Bakker89e80c92012-03-20 13:50:09 +000026 unsigned int key_len;
Paul Bakker33b43f12013-08-20 11:48:36 +020027 size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
Paul Bakker89e80c92012-03-20 13:50:09 +000028
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +020029 mbedtls_gcm_init( &ctx );
30
Paul Bakker89e80c92012-03-20 13:50:09 +000031 memset(key_str, 0x00, 128);
32 memset(src_str, 0x00, 128);
Paul Bakker68b6d882012-09-08 14:04:13 +000033 memset(dst_str, 0x00, 257);
Paul Bakker89e80c92012-03-20 13:50:09 +000034 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
Paul Bakker33b43f12013-08-20 11:48:36 +020040 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 );
Paul Bakker89e80c92012-03-20 13:50:09 +000044
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +020045 TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == init_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020046 if( init_result == 0 )
Paul Bakker89e80c92012-03-20 13:50:09 +000047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 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 );
Paul Bakker89e80c92012-03-20 13:50:09 +000049 hexify( dst_str, output, pt_len );
50 hexify( tag_str, tag_output, tag_len );
51
Paul Bakker33b43f12013-08-20 11:48:36 +020052 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
53 TEST_ASSERT( strcmp( (char *) tag_str, hex_tag_string ) == 0 );
Paul Bakker89e80c92012-03-20 13:50:09 +000054 }
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +020055
Paul Bakkerbd51b262014-07-10 15:26:12 +020056exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 mbedtls_gcm_free( &ctx );
Paul Bakker89e80c92012-03-20 13:50:09 +000058}
Paul Bakker33b43f12013-08-20 11:48:36 +020059/* END_CASE */
Paul Bakker89e80c92012-03-20 13:50:09 +000060
Paul Bakker33b43f12013-08-20 11:48:36 +020061/* BEGIN_CASE */
Manuel Pégourié-Gonnard083d6682013-10-24 12:06:54 +020062void gcm_decrypt_and_verify( int cipher_id,
63 char *hex_key_string, char *hex_src_string,
Paul Bakker33b43f12013-08-20 11:48:36 +020064 char *hex_iv_string, char *hex_add_string,
65 int tag_len_bits, char *hex_tag_string,
66 char *pt_result, int init_result )
Paul Bakker89e80c92012-03-20 13:50:09 +000067{
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];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 mbedtls_gcm_context ctx;
Paul Bakker89e80c92012-03-20 13:50:09 +000076 unsigned int key_len;
Paul Bakker33b43f12013-08-20 11:48:36 +020077 size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
Paul Bakker89e80c92012-03-20 13:50:09 +000078 int ret;
79
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +020080 mbedtls_gcm_init( &ctx );
81
Paul Bakker89e80c92012-03-20 13:50:09 +000082 memset(key_str, 0x00, 128);
83 memset(src_str, 0x00, 128);
Paul Bakker68b6d882012-09-08 14:04:13 +000084 memset(dst_str, 0x00, 257);
Paul Bakker89e80c92012-03-20 13:50:09 +000085 memset(iv_str, 0x00, 128);
86 memset(add_str, 0x00, 128);
87 memset(tag_str, 0x00, 128);
88 memset(output, 0x00, 128);
89
Paul Bakker33b43f12013-08-20 11:48:36 +020090 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 );
Paul Bakker89e80c92012-03-20 13:50:09 +000095
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +020096 TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == init_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020097 if( init_result == 0 )
Paul Bakker89e80c92012-03-20 13:50:09 +000098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 ret = mbedtls_gcm_auth_decrypt( &ctx, pt_len, iv_str, iv_len, add_str, add_len, tag_str, tag_len, src_str, output );
Paul Bakker89e80c92012-03-20 13:50:09 +0000100
Paul Bakker33b43f12013-08-20 11:48:36 +0200101 if( strcmp( "FAIL", pt_result ) == 0 )
Paul Bakker89e80c92012-03-20 13:50:09 +0000102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 TEST_ASSERT( ret == MBEDTLS_ERR_GCM_AUTH_FAILED );
Paul Bakker89e80c92012-03-20 13:50:09 +0000104 }
105 else
106 {
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +0200107 TEST_ASSERT( ret == 0 );
Paul Bakker89e80c92012-03-20 13:50:09 +0000108 hexify( dst_str, output, pt_len );
109
Paul Bakker33b43f12013-08-20 11:48:36 +0200110 TEST_ASSERT( strcmp( (char *) dst_str, pt_result ) == 0 );
Paul Bakker89e80c92012-03-20 13:50:09 +0000111 }
112 }
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200113
Paul Bakkerbd51b262014-07-10 15:26:12 +0200114exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_gcm_free( &ctx );
Paul Bakker89e80c92012-03-20 13:50:09 +0000116}
Paul Bakker33b43f12013-08-20 11:48:36 +0200117/* END_CASE */
Paul Bakker89e80c92012-03-20 13:50:09 +0000118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200120void gcm_selftest()
Paul Bakker89e80c92012-03-20 13:50:09 +0000121{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 TEST_ASSERT( mbedtls_gcm_self_test( 0 ) == 0 );
Paul Bakker89e80c92012-03-20 13:50:09 +0000123}
Paul Bakker33b43f12013-08-20 11:48:36 +0200124/* END_CASE */