2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/gcm.h"
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_HEADER */
|
2012-03-20 13:50:09 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 10:49:31 +00:00
|
|
|
* depends_on:MBEDTLS_GCM_C
|
2013-08-20 09:48:36 +00:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2012-03-20 13:50:09 +00:00
|
|
|
|
2016-12-16 14:15:56 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void gcm_bad_parameters( int cipher_id, int direction,
|
2018-06-29 10:05:32 +00:00
|
|
|
data_t *key_str, data_t *src_str,
|
|
|
|
data_t *iv_str, data_t *add_str,
|
2016-12-16 14:15:56 +00:00
|
|
|
int tag_len_bits, int gcm_result )
|
|
|
|
{
|
|
|
|
unsigned char output[128];
|
|
|
|
unsigned char tag_output[16];
|
|
|
|
mbedtls_gcm_context ctx;
|
2017-08-02 16:33:54 +00:00
|
|
|
size_t tag_len = tag_len_bits / 8;
|
2016-12-16 14:15:56 +00:00
|
|
|
|
|
|
|
mbedtls_gcm_init( &ctx );
|
|
|
|
|
|
|
|
memset( output, 0x00, sizeof( output ) );
|
|
|
|
memset( tag_output, 0x00, sizeof( tag_output ) );
|
2018-03-13 15:22:58 +00:00
|
|
|
|
2017-08-02 16:33:54 +00:00
|
|
|
TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_gcm_crypt_and_tag( &ctx, direction, src_str->len, iv_str->x, iv_str->len,
|
|
|
|
add_str->x, add_str->len, src_str->x, output, tag_len, tag_output ) == gcm_result );
|
2016-12-16 14:15:56 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_gcm_free( &ctx );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
2018-06-29 10:05:32 +00:00
|
|
|
void gcm_encrypt_and_tag( int cipher_id, data_t * key_str,
|
|
|
|
data_t * src_str, data_t * iv_str,
|
2020-06-26 12:33:03 +00:00
|
|
|
data_t * add_str, data_t * dst,
|
|
|
|
int tag_len_bits, data_t * tag,
|
2017-06-09 03:32:58 +00:00
|
|
|
int init_result )
|
2012-03-20 13:50:09 +00:00
|
|
|
{
|
|
|
|
unsigned char output[128];
|
|
|
|
unsigned char tag_output[16];
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_gcm_context ctx;
|
2017-05-30 13:23:15 +00:00
|
|
|
size_t tag_len = tag_len_bits / 8;
|
2012-03-20 13:50:09 +00:00
|
|
|
|
2015-04-28 19:42:17 +00:00
|
|
|
mbedtls_gcm_init( &ctx );
|
|
|
|
|
2012-03-20 13:50:09 +00:00
|
|
|
memset(output, 0x00, 128);
|
|
|
|
memset(tag_output, 0x00, 16);
|
|
|
|
|
|
|
|
|
2017-06-09 03:32:58 +00:00
|
|
|
TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == init_result );
|
2013-08-20 09:48:36 +00:00
|
|
|
if( init_result == 0 )
|
2012-03-20 13:50:09 +00:00
|
|
|
{
|
2017-06-09 03:32:58 +00:00
|
|
|
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 );
|
2012-03-20 13:50:09 +00:00
|
|
|
|
2020-06-26 12:33:03 +00:00
|
|
|
TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x,
|
|
|
|
src_str->len, dst->len ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_hexcmp( tag_output, tag->x,
|
|
|
|
tag_len, tag->len ) == 0 );
|
2012-03-20 13:50:09 +00:00
|
|
|
}
|
2013-09-13 11:45:58 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_gcm_free( &ctx );
|
2012-03-20 13:50:09 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2012-03-20 13:50:09 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
2018-06-29 10:05:32 +00:00
|
|
|
void gcm_decrypt_and_verify( int cipher_id, data_t * key_str,
|
|
|
|
data_t * src_str, data_t * iv_str,
|
|
|
|
data_t * add_str, int tag_len_bits,
|
|
|
|
data_t * tag_str, char * result,
|
|
|
|
data_t * pt_result, int init_result )
|
2012-03-20 13:50:09 +00:00
|
|
|
{
|
|
|
|
unsigned char output[128];
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_gcm_context ctx;
|
2012-03-20 13:50:09 +00:00
|
|
|
int ret;
|
2017-05-30 13:23:15 +00:00
|
|
|
size_t tag_len = tag_len_bits / 8;
|
2012-03-20 13:50:09 +00:00
|
|
|
|
2015-04-28 19:42:17 +00:00
|
|
|
mbedtls_gcm_init( &ctx );
|
|
|
|
|
2012-03-20 13:50:09 +00:00
|
|
|
memset(output, 0x00, 128);
|
|
|
|
|
|
|
|
|
2017-06-09 03:32:58 +00:00
|
|
|
TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == init_result );
|
2013-08-20 09:48:36 +00:00
|
|
|
if( init_result == 0 )
|
2012-03-20 13:50:09 +00:00
|
|
|
{
|
2017-06-09 03:32:58 +00:00
|
|
|
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 );
|
2012-03-20 13:50:09 +00:00
|
|
|
|
2017-05-31 19:46:35 +00:00
|
|
|
if( strcmp( "FAIL", result ) == 0 )
|
2012-03-20 13:50:09 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( ret == MBEDTLS_ERR_GCM_AUTH_FAILED );
|
2012-03-20 13:50:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-03 18:17:35 +00:00
|
|
|
TEST_ASSERT( ret == 0 );
|
2012-03-20 13:50:09 +00:00
|
|
|
|
2020-06-10 09:42:32 +00:00
|
|
|
TEST_ASSERT( mbedtls_test_hexcmp( output, pt_result->x,
|
|
|
|
src_str->len,
|
|
|
|
pt_result->len ) == 0 );
|
2012-03-20 13:50:09 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-13 11:45:58 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_gcm_free( &ctx );
|
2012-03-20 13:50:09 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2012-03-20 13:50:09 +00:00
|
|
|
|
2018-12-12 13:21:59 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
|
|
|
|
void gcm_invalid_param( )
|
|
|
|
{
|
|
|
|
mbedtls_gcm_context ctx;
|
|
|
|
unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
|
|
|
|
mbedtls_cipher_id_t valid_cipher = MBEDTLS_CIPHER_ID_AES;
|
|
|
|
int valid_mode = MBEDTLS_GCM_ENCRYPT;
|
|
|
|
int valid_len = sizeof(valid_buffer);
|
|
|
|
int valid_bitlen = 128, invalid_bitlen = 1;
|
|
|
|
|
|
|
|
mbedtls_gcm_init( &ctx );
|
|
|
|
|
|
|
|
/* mbedtls_gcm_init() */
|
|
|
|
TEST_INVALID_PARAM( mbedtls_gcm_init( NULL ) );
|
|
|
|
|
|
|
|
/* mbedtls_gcm_setkey */
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_setkey( NULL, valid_cipher, valid_buffer, valid_bitlen ) );
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_setkey( &ctx, valid_cipher, NULL, valid_bitlen ) );
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_setkey( &ctx, valid_cipher, valid_buffer, invalid_bitlen ) );
|
|
|
|
|
|
|
|
/* mbedtls_gcm_crypt_and_tag() */
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_crypt_and_tag( NULL, valid_mode, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
valid_buffer, valid_buffer,
|
|
|
|
valid_len, valid_buffer ) );
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_crypt_and_tag( &ctx, valid_mode, valid_len,
|
|
|
|
NULL, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
valid_buffer, valid_buffer,
|
|
|
|
valid_len, valid_buffer ) );
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_crypt_and_tag( &ctx, valid_mode, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
NULL, valid_len,
|
|
|
|
valid_buffer, valid_buffer,
|
|
|
|
valid_len, valid_buffer ) );
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_crypt_and_tag( &ctx, valid_mode, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
NULL, valid_buffer,
|
|
|
|
valid_len, valid_buffer ) );
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_crypt_and_tag( &ctx, valid_mode, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
valid_buffer, NULL,
|
|
|
|
valid_len, valid_buffer ) );
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_crypt_and_tag( &ctx, valid_mode, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
valid_buffer, valid_buffer,
|
|
|
|
valid_len, NULL ) );
|
|
|
|
|
|
|
|
/* mbedtls_gcm_auth_decrypt() */
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_auth_decrypt( NULL, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
valid_buffer, valid_buffer) );
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_auth_decrypt( &ctx, valid_len,
|
|
|
|
NULL, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
valid_buffer, valid_buffer) );
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_auth_decrypt( &ctx, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
NULL, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
valid_buffer, valid_buffer) );
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_auth_decrypt( &ctx, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
NULL, valid_len,
|
|
|
|
valid_buffer, valid_buffer) );
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_auth_decrypt( &ctx, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
NULL, valid_buffer) );
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_auth_decrypt( &ctx, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
valid_buffer, NULL) );
|
|
|
|
|
|
|
|
/* mbedtls_gcm_starts() */
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_starts( NULL, valid_mode,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
valid_buffer, valid_len ) );
|
|
|
|
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_starts( &ctx, valid_mode,
|
|
|
|
NULL, valid_len,
|
|
|
|
valid_buffer, valid_len ) );
|
|
|
|
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_starts( &ctx, valid_mode,
|
|
|
|
valid_buffer, valid_len,
|
|
|
|
NULL, valid_len ) );
|
|
|
|
|
|
|
|
/* mbedtls_gcm_update() */
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_update( NULL, valid_len,
|
|
|
|
valid_buffer, valid_buffer ) );
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_update( &ctx, valid_len,
|
|
|
|
NULL, valid_buffer ) );
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_update( &ctx, valid_len,
|
|
|
|
valid_buffer, NULL ) );
|
|
|
|
|
|
|
|
/* mbedtls_gcm_finish() */
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_finish( NULL, valid_buffer, valid_len ) );
|
|
|
|
TEST_INVALID_PARAM_RET(
|
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_finish( &ctx, NULL, valid_len ) );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_gcm_free( &ctx );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-12-12 14:51:26 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void gcm_valid_param( )
|
|
|
|
{
|
|
|
|
TEST_VALID_PARAM( mbedtls_gcm_free( NULL ) );
|
|
|
|
exit:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
|
2017-05-30 13:23:15 +00:00
|
|
|
void gcm_selftest( )
|
2012-03-20 13:50:09 +00:00
|
|
|
{
|
2016-09-09 08:10:28 +00:00
|
|
|
TEST_ASSERT( mbedtls_gcm_self_test( 1 ) == 0 );
|
2012-03-20 13:50:09 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|