blob: c0e799c196ac367600a6b089f5cda9335cc7b2be [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 */
Ron Eldor5a21fd62016-12-16 16:15:56 +020011void gcm_bad_parameters( int cipher_id, int direction,
12 char *hex_key_string, char *hex_src_string,
13 char *hex_iv_string, char *hex_add_string,
14 int tag_len_bits, int gcm_result )
15{
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 mbedtls_gcm_context ctx;
25 unsigned int key_len;
26 size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
27
28 mbedtls_gcm_init( &ctx );
29
30 memset( key_str, 0x00, sizeof( key_str ) );
31 memset( src_str, 0x00, sizeof( src_str ) );
32 memset( dst_str, 0x00, sizeof( dst_str ) );
33 memset( iv_str, 0x00, sizeof( iv_str ) );
34 memset( add_str, 0x00, sizeof( add_str ) );
35 memset( tag_str, 0x00, sizeof( tag_str ) );
36 memset( output, 0x00, sizeof( output ) );
37 memset( tag_output, 0x00, sizeof( tag_output ) );
Darryl Green11999bb2018-03-13 15:22:58 +000038
Ron Eldor5a21fd62016-12-16 16:15:56 +020039 key_len = unhexify( key_str, hex_key_string );
40 pt_len = unhexify( src_str, hex_src_string );
41 iv_len = unhexify( iv_str, hex_iv_string );
42 add_len = unhexify( add_str, hex_add_string );
43
44 TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == 0 );
45 TEST_ASSERT( mbedtls_gcm_crypt_and_tag( &ctx, direction, pt_len, iv_str, iv_len,
46 add_str, add_len, src_str, output, tag_len, tag_output ) == gcm_result );
47
48exit:
49 mbedtls_gcm_free( &ctx );
50}
51/* END_CASE */
52
53/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +010054void gcm_encrypt_and_tag( int cipher_id, HexParam_t * key_str,
55 HexParam_t * src_str, HexParam_t * iv_str,
56 HexParam_t * add_str, HexParam_t * hex_dst_string,
57 int tag_len_bits, HexParam_t * hex_tag_string,
58 int init_result )
Paul Bakker89e80c92012-03-20 13:50:09 +000059{
Paul Bakker89e80c92012-03-20 13:50:09 +000060 unsigned char output[128];
61 unsigned char tag_output[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 mbedtls_gcm_context ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +010063 size_t tag_len = tag_len_bits / 8;
Paul Bakker89e80c92012-03-20 13:50:09 +000064
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +020065 mbedtls_gcm_init( &ctx );
66
Paul Bakker89e80c92012-03-20 13:50:09 +000067 memset(output, 0x00, 128);
68 memset(tag_output, 0x00, 16);
69
Paul Bakker89e80c92012-03-20 13:50:09 +000070
Azim Khand30ca132017-06-09 04:32:58 +010071 TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == init_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020072 if( init_result == 0 )
Paul Bakker89e80c92012-03-20 13:50:09 +000073 {
Azim Khand30ca132017-06-09 04:32:58 +010074 TEST_ASSERT( mbedtls_gcm_crypt_and_tag( &ctx, MBEDTLS_GCM_ENCRYPT, src_str->len, iv_str->x, iv_str->len, add_str->x, add_str->len, src_str->x, output, tag_len, tag_output ) == 0 );
Paul Bakker89e80c92012-03-20 13:50:09 +000075
Azim Khand30ca132017-06-09 04:32:58 +010076 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
77 TEST_ASSERT( hexcmp( tag_output, hex_tag_string->x, tag_len, hex_tag_string->len ) == 0 );
Paul Bakker89e80c92012-03-20 13:50:09 +000078 }
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +020079
Paul Bakkerbd51b262014-07-10 15:26:12 +020080exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 mbedtls_gcm_free( &ctx );
Paul Bakker89e80c92012-03-20 13:50:09 +000082}
Paul Bakker33b43f12013-08-20 11:48:36 +020083/* END_CASE */
Paul Bakker89e80c92012-03-20 13:50:09 +000084
Paul Bakker33b43f12013-08-20 11:48:36 +020085/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +010086void gcm_decrypt_and_verify( int cipher_id, HexParam_t * key_str,
87 HexParam_t * src_str, HexParam_t * iv_str,
88 HexParam_t * add_str, int tag_len_bits,
89 HexParam_t * tag_str, char * result,
90 HexParam_t * pt_result, int init_result )
Paul Bakker89e80c92012-03-20 13:50:09 +000091{
Paul Bakker89e80c92012-03-20 13:50:09 +000092 unsigned char output[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 mbedtls_gcm_context ctx;
Paul Bakker89e80c92012-03-20 13:50:09 +000094 int ret;
Azim Khanf1aaec92017-05-30 14:23:15 +010095 size_t tag_len = tag_len_bits / 8;
Paul Bakker89e80c92012-03-20 13:50:09 +000096
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +020097 mbedtls_gcm_init( &ctx );
98
Paul Bakker89e80c92012-03-20 13:50:09 +000099 memset(output, 0x00, 128);
100
Paul Bakker89e80c92012-03-20 13:50:09 +0000101
Azim Khand30ca132017-06-09 04:32:58 +0100102 TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == init_result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200103 if( init_result == 0 )
Paul Bakker89e80c92012-03-20 13:50:09 +0000104 {
Azim Khand30ca132017-06-09 04:32:58 +0100105 ret = mbedtls_gcm_auth_decrypt( &ctx, src_str->len, iv_str->x, iv_str->len, add_str->x, add_str->len, tag_str->x, tag_len, src_str->x, output );
Paul Bakker89e80c92012-03-20 13:50:09 +0000106
Azim Khan46c9b1f2017-05-31 20:46:35 +0100107 if( strcmp( "FAIL", result ) == 0 )
Paul Bakker89e80c92012-03-20 13:50:09 +0000108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 TEST_ASSERT( ret == MBEDTLS_ERR_GCM_AUTH_FAILED );
Paul Bakker89e80c92012-03-20 13:50:09 +0000110 }
111 else
112 {
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +0200113 TEST_ASSERT( ret == 0 );
Paul Bakker89e80c92012-03-20 13:50:09 +0000114
Azim Khand30ca132017-06-09 04:32:58 +0100115 TEST_ASSERT( hexcmp( output, pt_result->x, src_str->len, pt_result->len ) == 0 );
Paul Bakker89e80c92012-03-20 13:50:09 +0000116 }
117 }
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200118
Paul Bakkerbd51b262014-07-10 15:26:12 +0200119exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_gcm_free( &ctx );
Paul Bakker89e80c92012-03-20 13:50:09 +0000121}
Paul Bakker33b43f12013-08-20 11:48:36 +0200122/* END_CASE */
Paul Bakker89e80c92012-03-20 13:50:09 +0000123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100125void gcm_selftest( )
Paul Bakker89e80c92012-03-20 13:50:09 +0000126{
Andres AG93012e82016-09-09 09:10:28 +0100127 TEST_ASSERT( mbedtls_gcm_self_test( 1 ) == 0 );
Paul Bakker89e80c92012-03-20 13:50:09 +0000128}
Paul Bakker33b43f12013-08-20 11:48:36 +0200129/* END_CASE */