mbedtls/tests/suites/test_suite_gcm.function

136 lines
4.4 KiB
Text
Raw Normal View History

/* BEGIN_HEADER */
2015-03-09 17:05:11 +00:00
#include "mbedtls/gcm.h"
/* END_HEADER */
/* BEGIN_DEPENDENCIES
* depends_on:MBEDTLS_GCM_C
* END_DEPENDENCIES
*/
/* 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 15:22:58 +00: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 */
/* BEGIN_CASE */
2017-05-30 13:23:15 +00:00
void gcm_encrypt_and_tag( int cipher_id, uint8_t * key_str, uint32_t key_len,
uint8_t * src_str, uint32_t pt_len,
uint8_t * iv_str, uint32_t iv_len,
uint8_t * add_str, uint32_t add_len,
uint8_t * hex_dst_string,
uint32_t hex_dst_string_len, int tag_len_bits,
uint8_t * hex_tag_string,
uint32_t hex_tag_string_len, int init_result )
{
unsigned char output[128];
unsigned char tag_output[16];
mbedtls_gcm_context ctx;
2017-05-30 13:23:15 +00:00
size_t tag_len = tag_len_bits / 8;
mbedtls_gcm_init( &ctx );
memset(output, 0x00, 128);
memset(tag_output, 0x00, 16);
TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == init_result );
if( init_result == 0 )
{
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 );
2017-05-30 13:23:15 +00:00
TEST_ASSERT( hexcmp( output, hex_dst_string, pt_len, hex_dst_string_len ) == 0 );
TEST_ASSERT( hexcmp( tag_output, hex_tag_string, tag_len, hex_tag_string_len ) == 0 );
}
exit:
mbedtls_gcm_free( &ctx );
}
/* END_CASE */
/* BEGIN_CASE */
2017-05-30 13:23:15 +00:00
void gcm_decrypt_and_verify( int cipher_id, uint8_t * key_str,
uint32_t key_len, uint8_t * src_str,
uint32_t pt_len, uint8_t * iv_str,
uint32_t iv_len, uint8_t * add_str,
uint32_t add_len, int tag_len_bits,
uint8_t * tag_str, uint32_t tag_str_len,
uint8_t * pt_result, uint32_t pt_result_len,
int init_result )
{
unsigned char output[128];
mbedtls_gcm_context ctx;
int ret;
2017-05-30 13:23:15 +00:00
size_t tag_len = tag_len_bits / 8;
mbedtls_gcm_init( &ctx );
memset(output, 0x00, 128);
TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == init_result );
if( init_result == 0 )
{
ret = mbedtls_gcm_auth_decrypt( &ctx, pt_len, iv_str, iv_len, add_str, add_len, tag_str, tag_len, src_str, output );
if( strcmp( "FAIL", pt_result ) == 0 )
{
TEST_ASSERT( ret == MBEDTLS_ERR_GCM_AUTH_FAILED );
}
else
{
2013-09-03 18:17:35 +00:00
TEST_ASSERT( ret == 0 );
2017-05-30 13:23:15 +00:00
TEST_ASSERT( hexcmp( output, pt_result, pt_len, pt_result_len ) == 0 );
}
}
exit:
mbedtls_gcm_free( &ctx );
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
2017-05-30 13:23:15 +00:00
void gcm_selftest( )
{
TEST_ASSERT( mbedtls_gcm_self_test( 1 ) == 0 );
}
/* END_CASE */