2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/gcm.h"
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_HEADER */
|
2012-03-20 14:50:09 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 12:49:31 +02:00
|
|
|
* depends_on:MBEDTLS_GCM_C
|
2013-08-20 11:48:36 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2012-03-20 14:50:09 +01:00
|
|
|
|
2016-12-16 15:15:56 +01:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void gcm_bad_parameters( int cipher_id, int direction,
|
|
|
|
char *hex_key_string, char *hex_src_string,
|
|
|
|
char *hex_iv_string, char *hex_add_string,
|
|
|
|
int tag_len_bits, int gcm_result )
|
|
|
|
{
|
|
|
|
unsigned char key_str[128];
|
|
|
|
unsigned char src_str[128];
|
|
|
|
unsigned char dst_str[257];
|
|
|
|
unsigned char iv_str[128];
|
|
|
|
unsigned char add_str[128];
|
|
|
|
unsigned char tag_str[128];
|
|
|
|
unsigned char output[128];
|
|
|
|
unsigned char tag_output[16];
|
|
|
|
mbedtls_gcm_context ctx;
|
|
|
|
unsigned int key_len;
|
|
|
|
size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
|
|
|
|
|
|
|
|
mbedtls_gcm_init( &ctx );
|
|
|
|
|
|
|
|
memset( key_str, 0x00, sizeof( key_str ) );
|
|
|
|
memset( src_str, 0x00, sizeof( src_str ) );
|
|
|
|
memset( dst_str, 0x00, sizeof( dst_str ) );
|
|
|
|
memset( iv_str, 0x00, sizeof( iv_str ) );
|
|
|
|
memset( add_str, 0x00, sizeof( add_str ) );
|
|
|
|
memset( tag_str, 0x00, sizeof( tag_str ) );
|
|
|
|
memset( output, 0x00, sizeof( output ) );
|
|
|
|
memset( tag_output, 0x00, sizeof( tag_output ) );
|
2018-03-13 16:22:58 +01:00
|
|
|
|
2016-12-16 15:15:56 +01:00
|
|
|
key_len = unhexify( key_str, hex_key_string );
|
|
|
|
pt_len = unhexify( src_str, hex_src_string );
|
|
|
|
iv_len = unhexify( iv_str, hex_iv_string );
|
|
|
|
add_len = unhexify( add_str, hex_add_string );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_gcm_crypt_and_tag( &ctx, direction, pt_len, iv_str, iv_len,
|
|
|
|
add_str, add_len, src_str, output, tag_len, tag_output ) == gcm_result );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_gcm_free( &ctx );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2013-10-24 12:06:54 +02:00
|
|
|
void gcm_encrypt_and_tag( int cipher_id,
|
|
|
|
char *hex_key_string, char *hex_src_string,
|
2013-08-20 11:48:36 +02:00
|
|
|
char *hex_iv_string, char *hex_add_string,
|
|
|
|
char *hex_dst_string, int tag_len_bits,
|
|
|
|
char *hex_tag_string, int init_result )
|
2012-03-20 14:50:09 +01:00
|
|
|
{
|
|
|
|
unsigned char key_str[128];
|
|
|
|
unsigned char src_str[128];
|
|
|
|
unsigned char dst_str[257];
|
|
|
|
unsigned char iv_str[128];
|
|
|
|
unsigned char add_str[128];
|
|
|
|
unsigned char tag_str[128];
|
|
|
|
unsigned char output[128];
|
|
|
|
unsigned char tag_output[16];
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_gcm_context ctx;
|
2012-03-20 14:50:09 +01:00
|
|
|
unsigned int key_len;
|
2013-08-20 11:48:36 +02:00
|
|
|
size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
|
2012-03-20 14:50:09 +01:00
|
|
|
|
2015-04-28 21:42:17 +02:00
|
|
|
mbedtls_gcm_init( &ctx );
|
|
|
|
|
2012-03-20 14:50:09 +01:00
|
|
|
memset(key_str, 0x00, 128);
|
|
|
|
memset(src_str, 0x00, 128);
|
2012-09-08 16:04:13 +02:00
|
|
|
memset(dst_str, 0x00, 257);
|
2012-03-20 14:50:09 +01:00
|
|
|
memset(iv_str, 0x00, 128);
|
|
|
|
memset(add_str, 0x00, 128);
|
|
|
|
memset(tag_str, 0x00, 128);
|
|
|
|
memset(output, 0x00, 128);
|
|
|
|
memset(tag_output, 0x00, 16);
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
key_len = unhexify( key_str, hex_key_string );
|
|
|
|
pt_len = unhexify( src_str, hex_src_string );
|
|
|
|
iv_len = unhexify( iv_str, hex_iv_string );
|
|
|
|
add_len = unhexify( add_str, hex_add_string );
|
2012-03-20 14:50:09 +01:00
|
|
|
|
2015-04-28 21:42:17 +02:00
|
|
|
TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == init_result );
|
2013-08-20 11:48:36 +02:00
|
|
|
if( init_result == 0 )
|
2012-03-20 14:50:09 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
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 );
|
2012-03-20 14:50:09 +01:00
|
|
|
hexify( dst_str, output, pt_len );
|
|
|
|
hexify( tag_str, tag_output, tag_len );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
|
|
|
|
TEST_ASSERT( strcmp( (char *) tag_str, hex_tag_string ) == 0 );
|
2012-03-20 14:50:09 +01:00
|
|
|
}
|
2013-09-13 13:45:58 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_gcm_free( &ctx );
|
2012-03-20 14:50:09 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2012-03-20 14:50:09 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2013-10-24 12:06:54 +02:00
|
|
|
void gcm_decrypt_and_verify( int cipher_id,
|
|
|
|
char *hex_key_string, char *hex_src_string,
|
2013-08-20 11:48:36 +02:00
|
|
|
char *hex_iv_string, char *hex_add_string,
|
|
|
|
int tag_len_bits, char *hex_tag_string,
|
|
|
|
char *pt_result, int init_result )
|
2012-03-20 14:50:09 +01:00
|
|
|
{
|
|
|
|
unsigned char key_str[128];
|
|
|
|
unsigned char src_str[128];
|
|
|
|
unsigned char dst_str[257];
|
|
|
|
unsigned char iv_str[128];
|
|
|
|
unsigned char add_str[128];
|
|
|
|
unsigned char tag_str[128];
|
|
|
|
unsigned char output[128];
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_gcm_context ctx;
|
2012-03-20 14:50:09 +01:00
|
|
|
unsigned int key_len;
|
2013-08-20 11:48:36 +02:00
|
|
|
size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
|
2012-03-20 14:50:09 +01:00
|
|
|
int ret;
|
|
|
|
|
2015-04-28 21:42:17 +02:00
|
|
|
mbedtls_gcm_init( &ctx );
|
|
|
|
|
2012-03-20 14:50:09 +01:00
|
|
|
memset(key_str, 0x00, 128);
|
|
|
|
memset(src_str, 0x00, 128);
|
2012-09-08 16:04:13 +02:00
|
|
|
memset(dst_str, 0x00, 257);
|
2012-03-20 14:50:09 +01:00
|
|
|
memset(iv_str, 0x00, 128);
|
|
|
|
memset(add_str, 0x00, 128);
|
|
|
|
memset(tag_str, 0x00, 128);
|
|
|
|
memset(output, 0x00, 128);
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
key_len = unhexify( key_str, hex_key_string );
|
|
|
|
pt_len = unhexify( src_str, hex_src_string );
|
|
|
|
iv_len = unhexify( iv_str, hex_iv_string );
|
|
|
|
add_len = unhexify( add_str, hex_add_string );
|
|
|
|
unhexify( tag_str, hex_tag_string );
|
2012-03-20 14:50:09 +01:00
|
|
|
|
2015-04-28 21:42:17 +02:00
|
|
|
TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == init_result );
|
2013-08-20 11:48:36 +02:00
|
|
|
if( init_result == 0 )
|
2012-03-20 14:50:09 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_gcm_auth_decrypt( &ctx, pt_len, iv_str, iv_len, add_str, add_len, tag_str, tag_len, src_str, output );
|
2012-03-20 14:50:09 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( strcmp( "FAIL", pt_result ) == 0 )
|
2012-03-20 14:50:09 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( ret == MBEDTLS_ERR_GCM_AUTH_FAILED );
|
2012-03-20 14:50:09 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-03 20:17:35 +02:00
|
|
|
TEST_ASSERT( ret == 0 );
|
2012-03-20 14:50:09 +01:00
|
|
|
hexify( dst_str, output, pt_len );
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcmp( (char *) dst_str, pt_result ) == 0 );
|
2012-03-20 14:50:09 +01:00
|
|
|
}
|
|
|
|
}
|
2013-09-13 13:45:58 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_gcm_free( &ctx );
|
2012-03-20 14:50:09 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2012-03-20 14:50:09 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
|
2013-08-20 11:48:36 +02:00
|
|
|
void gcm_selftest()
|
2012-03-20 14:50:09 +01:00
|
|
|
{
|
2016-09-09 10:10:28 +02:00
|
|
|
TEST_ASSERT( mbedtls_gcm_self_test( 1 ) == 0 );
|
2012-03-20 14:50:09 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|