blob: b3d212a5095eed9583b0715a83d1abb385a61f8a [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,
Azim Khan317efe82017-08-02 17:33:54 +010012 HexParam_t *key_str, HexParam_t *src_str,
13 HexParam_t *iv_str, HexParam_t *add_str,
Ron Eldor5a21fd62016-12-16 16:15:56 +020014 int tag_len_bits, int gcm_result )
15{
Ron Eldor5a21fd62016-12-16 16:15:56 +020016 unsigned char output[128];
17 unsigned char tag_output[16];
18 mbedtls_gcm_context ctx;
Azim Khan317efe82017-08-02 17:33:54 +010019 size_t tag_len = tag_len_bits / 8;
Ron Eldor5a21fd62016-12-16 16:15:56 +020020
21 mbedtls_gcm_init( &ctx );
22
Ron Eldor5a21fd62016-12-16 16:15:56 +020023 memset( output, 0x00, sizeof( output ) );
24 memset( tag_output, 0x00, sizeof( tag_output ) );
Darryl Green11999bb2018-03-13 15:22:58 +000025
Azim Khan317efe82017-08-02 17:33:54 +010026 TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == 0 );
27 TEST_ASSERT( mbedtls_gcm_crypt_and_tag( &ctx, direction, src_str->len, iv_str->x, iv_str->len,
28 add_str->x, add_str->len, src_str->x, output, tag_len, tag_output ) == gcm_result );
Ron Eldor5a21fd62016-12-16 16:15:56 +020029
30exit:
31 mbedtls_gcm_free( &ctx );
32}
33/* END_CASE */
34
35/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +010036void gcm_encrypt_and_tag( int cipher_id, HexParam_t * key_str,
37 HexParam_t * src_str, HexParam_t * iv_str,
38 HexParam_t * add_str, HexParam_t * hex_dst_string,
39 int tag_len_bits, HexParam_t * hex_tag_string,
40 int init_result )
Paul Bakker89e80c92012-03-20 13:50:09 +000041{
Paul Bakker89e80c92012-03-20 13:50:09 +000042 unsigned char output[128];
43 unsigned char tag_output[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 mbedtls_gcm_context ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +010045 size_t tag_len = tag_len_bits / 8;
Paul Bakker89e80c92012-03-20 13:50:09 +000046
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +020047 mbedtls_gcm_init( &ctx );
48
Paul Bakker89e80c92012-03-20 13:50:09 +000049 memset(output, 0x00, 128);
50 memset(tag_output, 0x00, 16);
51
Paul Bakker89e80c92012-03-20 13:50:09 +000052
Azim Khand30ca132017-06-09 04:32:58 +010053 TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == init_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020054 if( init_result == 0 )
Paul Bakker89e80c92012-03-20 13:50:09 +000055 {
Azim Khand30ca132017-06-09 04:32:58 +010056 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 +000057
Azim Khand30ca132017-06-09 04:32:58 +010058 TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
59 TEST_ASSERT( hexcmp( tag_output, hex_tag_string->x, tag_len, hex_tag_string->len ) == 0 );
Paul Bakker89e80c92012-03-20 13:50:09 +000060 }
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +020061
Paul Bakkerbd51b262014-07-10 15:26:12 +020062exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063 mbedtls_gcm_free( &ctx );
Paul Bakker89e80c92012-03-20 13:50:09 +000064}
Paul Bakker33b43f12013-08-20 11:48:36 +020065/* END_CASE */
Paul Bakker89e80c92012-03-20 13:50:09 +000066
Paul Bakker33b43f12013-08-20 11:48:36 +020067/* BEGIN_CASE */
Azim Khand30ca132017-06-09 04:32:58 +010068void gcm_decrypt_and_verify( int cipher_id, HexParam_t * key_str,
69 HexParam_t * src_str, HexParam_t * iv_str,
70 HexParam_t * add_str, int tag_len_bits,
71 HexParam_t * tag_str, char * result,
72 HexParam_t * pt_result, int init_result )
Paul Bakker89e80c92012-03-20 13:50:09 +000073{
Paul Bakker89e80c92012-03-20 13:50:09 +000074 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 int ret;
Azim Khanf1aaec92017-05-30 14:23:15 +010077 size_t tag_len = tag_len_bits / 8;
Paul Bakker89e80c92012-03-20 13:50:09 +000078
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +020079 mbedtls_gcm_init( &ctx );
80
Paul Bakker89e80c92012-03-20 13:50:09 +000081 memset(output, 0x00, 128);
82
Paul Bakker89e80c92012-03-20 13:50:09 +000083
Azim Khand30ca132017-06-09 04:32:58 +010084 TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == init_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020085 if( init_result == 0 )
Paul Bakker89e80c92012-03-20 13:50:09 +000086 {
Azim Khand30ca132017-06-09 04:32:58 +010087 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 +000088
Azim Khan46c9b1f2017-05-31 20:46:35 +010089 if( strcmp( "FAIL", result ) == 0 )
Paul Bakker89e80c92012-03-20 13:50:09 +000090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 TEST_ASSERT( ret == MBEDTLS_ERR_GCM_AUTH_FAILED );
Paul Bakker89e80c92012-03-20 13:50:09 +000092 }
93 else
94 {
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +020095 TEST_ASSERT( ret == 0 );
Paul Bakker89e80c92012-03-20 13:50:09 +000096
Azim Khand30ca132017-06-09 04:32:58 +010097 TEST_ASSERT( hexcmp( output, pt_result->x, src_str->len, pt_result->len ) == 0 );
Paul Bakker89e80c92012-03-20 13:50:09 +000098 }
99 }
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200100
Paul Bakkerbd51b262014-07-10 15:26:12 +0200101exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_gcm_free( &ctx );
Paul Bakker89e80c92012-03-20 13:50:09 +0000103}
Paul Bakker33b43f12013-08-20 11:48:36 +0200104/* END_CASE */
Paul Bakker89e80c92012-03-20 13:50:09 +0000105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100107void gcm_selftest( )
Paul Bakker89e80c92012-03-20 13:50:09 +0000108{
Andres AG93012e82016-09-09 09:10:28 +0100109 TEST_ASSERT( mbedtls_gcm_self_test( 1 ) == 0 );
Paul Bakker89e80c92012-03-20 13:50:09 +0000110}
Paul Bakker33b43f12013-08-20 11:48:36 +0200111/* END_CASE */