2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/gcm.h"
|
2021-04-13 13:02:03 +02:00
|
|
|
|
|
|
|
/* Use the multipart interface to process the encrypted data in two parts
|
|
|
|
* and check that the output matches the expected output.
|
|
|
|
* The context must have been set up with the key. */
|
|
|
|
static int check_multipart( mbedtls_gcm_context *ctx,
|
|
|
|
int mode,
|
|
|
|
const data_t *iv,
|
|
|
|
const data_t *add,
|
|
|
|
const data_t *input,
|
|
|
|
const data_t *expected_output,
|
|
|
|
const data_t *tag,
|
2021-06-15 15:29:48 +02:00
|
|
|
size_t n1,
|
|
|
|
size_t n1_add)
|
2021-04-13 13:02:03 +02:00
|
|
|
{
|
|
|
|
int ok = 0;
|
|
|
|
uint8_t *output = NULL;
|
|
|
|
size_t n2 = input->len - n1;
|
2021-05-26 14:26:48 +02:00
|
|
|
size_t n2_add = add->len - n1_add;
|
2021-04-15 17:22:35 +02:00
|
|
|
size_t olen;
|
2021-04-13 13:02:03 +02:00
|
|
|
|
|
|
|
/* Sanity checks on the test data */
|
|
|
|
TEST_ASSERT( n1 <= input->len );
|
2021-05-26 14:26:48 +02:00
|
|
|
TEST_ASSERT( n1_add <= add->len );
|
2021-04-13 13:02:03 +02:00
|
|
|
TEST_EQUAL( input->len, expected_output->len );
|
|
|
|
|
|
|
|
TEST_EQUAL( 0, mbedtls_gcm_starts( ctx, mode,
|
2021-04-15 18:32:23 +02:00
|
|
|
iv->x, iv->len ) );
|
2021-05-26 14:26:48 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_gcm_update_ad( ctx, add->x, n1_add ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_gcm_update_ad( ctx, add->x + n1_add, n2_add ) );
|
2021-04-13 13:02:03 +02:00
|
|
|
|
|
|
|
/* Allocate a tight buffer for each update call. This way, if the function
|
|
|
|
* tries to write beyond the advertised required buffer size, this will
|
|
|
|
* count as an overflow for memory sanitizers and static checkers. */
|
|
|
|
ASSERT_ALLOC( output, n1 );
|
2021-04-15 17:22:35 +02:00
|
|
|
olen = 0xdeadbeef;
|
|
|
|
TEST_EQUAL( 0, mbedtls_gcm_update( ctx, input->x, n1, output, n1, &olen ) );
|
|
|
|
TEST_EQUAL( n1, olen );
|
|
|
|
ASSERT_COMPARE( output, olen, expected_output->x, n1 );
|
2021-04-13 13:02:03 +02:00
|
|
|
mbedtls_free( output );
|
|
|
|
output = NULL;
|
|
|
|
|
|
|
|
ASSERT_ALLOC( output, n2 );
|
2021-04-15 17:22:35 +02:00
|
|
|
olen = 0xdeadbeef;
|
|
|
|
TEST_EQUAL( 0, mbedtls_gcm_update( ctx, input->x + n1, n2, output, n2, &olen ) );
|
|
|
|
TEST_EQUAL( n2, olen );
|
|
|
|
ASSERT_COMPARE( output, olen, expected_output->x + n1, n2 );
|
2021-04-13 13:02:03 +02:00
|
|
|
mbedtls_free( output );
|
|
|
|
output = NULL;
|
|
|
|
|
|
|
|
ASSERT_ALLOC( output, tag->len );
|
2021-06-23 21:51:32 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_gcm_finish( ctx, NULL, 0, &olen, output, tag->len ) );
|
|
|
|
TEST_EQUAL( 0, olen );
|
2021-04-13 13:02:03 +02:00
|
|
|
ASSERT_COMPARE( output, tag->len, tag->x, tag->len );
|
|
|
|
mbedtls_free( output );
|
|
|
|
output = NULL;
|
|
|
|
|
|
|
|
ok = 1;
|
|
|
|
exit:
|
|
|
|
mbedtls_free( output );
|
|
|
|
return( ok );
|
|
|
|
}
|
|
|
|
|
2021-06-16 11:04:07 +02:00
|
|
|
static void check_cipher_with_empty_ad( mbedtls_gcm_context *ctx,
|
|
|
|
int mode,
|
|
|
|
const data_t *iv,
|
|
|
|
const data_t *input,
|
|
|
|
const data_t *expected_output,
|
|
|
|
const data_t *tag,
|
|
|
|
size_t ad_update_count)
|
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
uint8_t *output = NULL;
|
|
|
|
size_t olen;
|
|
|
|
|
|
|
|
/* Sanity checks on the test data */
|
|
|
|
TEST_EQUAL( input->len, expected_output->len );
|
|
|
|
|
|
|
|
TEST_EQUAL( 0, mbedtls_gcm_starts( ctx, mode,
|
|
|
|
iv->x, iv->len ) );
|
|
|
|
|
2021-06-22 11:12:28 +02:00
|
|
|
for( n = 0; n < ad_update_count; n++ )
|
2021-06-16 11:04:07 +02:00
|
|
|
{
|
|
|
|
TEST_EQUAL( 0, mbedtls_gcm_update_ad( ctx, NULL, 0 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a tight buffer for each update call. This way, if the function
|
|
|
|
* tries to write beyond the advertised required buffer size, this will
|
|
|
|
* count as an overflow for memory sanitizers and static checkers. */
|
|
|
|
ASSERT_ALLOC( output, input->len );
|
|
|
|
olen = 0xdeadbeef;
|
|
|
|
TEST_EQUAL( 0, mbedtls_gcm_update( ctx, input->x, input->len, output, input->len, &olen ) );
|
|
|
|
TEST_EQUAL( input->len, olen );
|
|
|
|
ASSERT_COMPARE( output, olen, expected_output->x, input->len );
|
|
|
|
mbedtls_free( output );
|
|
|
|
output = NULL;
|
|
|
|
|
|
|
|
ASSERT_ALLOC( output, tag->len );
|
2021-06-23 21:51:32 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_gcm_finish( ctx, NULL, 0, &olen, output, tag->len ) );
|
|
|
|
TEST_EQUAL( 0, olen );
|
2021-06-16 11:04:07 +02:00
|
|
|
ASSERT_COMPARE( output, tag->len, tag->x, tag->len );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free( output );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void check_empty_cipher_with_ad( mbedtls_gcm_context *ctx,
|
|
|
|
int mode,
|
|
|
|
const data_t *iv,
|
|
|
|
const data_t *add,
|
|
|
|
const data_t *tag,
|
|
|
|
size_t cipher_update_count)
|
|
|
|
{
|
|
|
|
size_t olen;
|
|
|
|
size_t n;
|
|
|
|
uint8_t* output_tag = NULL;
|
|
|
|
|
|
|
|
TEST_EQUAL( 0, mbedtls_gcm_starts( ctx, mode, iv->x, iv->len ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_gcm_update_ad( ctx, add->x, add->len ) );
|
|
|
|
|
2021-06-22 11:12:28 +02:00
|
|
|
for( n = 0; n < cipher_update_count; n++ )
|
2021-06-16 11:04:07 +02:00
|
|
|
{
|
|
|
|
olen = 0xdeadbeef;
|
|
|
|
TEST_EQUAL( 0, mbedtls_gcm_update( ctx, NULL, 0, NULL, 0, &olen ) );
|
|
|
|
TEST_EQUAL( 0, olen );
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_ALLOC( output_tag, tag->len );
|
2021-06-23 21:51:32 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_gcm_finish( ctx, NULL, 0, &olen,
|
|
|
|
output_tag, tag->len ) );
|
|
|
|
TEST_EQUAL( 0, olen );
|
2021-06-16 11:04:07 +02:00
|
|
|
ASSERT_COMPARE( output_tag, tag->len, tag->x, tag->len );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free( output_tag );
|
|
|
|
}
|
|
|
|
|
2021-06-18 00:06:52 +02:00
|
|
|
static void check_no_cipher_no_ad( mbedtls_gcm_context *ctx,
|
|
|
|
int mode,
|
|
|
|
const data_t *iv,
|
|
|
|
const data_t *tag )
|
|
|
|
{
|
|
|
|
uint8_t *output = NULL;
|
2021-06-23 21:51:32 +02:00
|
|
|
size_t olen = 0;
|
2021-06-18 00:06:52 +02:00
|
|
|
|
|
|
|
TEST_EQUAL( 0, mbedtls_gcm_starts( ctx, mode,
|
|
|
|
iv->x, iv->len ) );
|
|
|
|
ASSERT_ALLOC( output, tag->len );
|
2021-06-23 21:51:32 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_gcm_finish( ctx, NULL, 0, &olen, output, tag->len ) );
|
|
|
|
TEST_EQUAL( 0, olen );
|
2021-06-18 00:06:52 +02:00
|
|
|
ASSERT_COMPARE( output, tag->len, tag->x, tag->len );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free( output );
|
|
|
|
}
|
|
|
|
|
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,
|
2018-06-29 12:05:32 +02:00
|
|
|
data_t *key_str, data_t *src_str,
|
|
|
|
data_t *iv_str, data_t *add_str,
|
2016-12-16 15:15:56 +01:00
|
|
|
int tag_len_bits, int gcm_result )
|
|
|
|
{
|
|
|
|
unsigned char output[128];
|
|
|
|
unsigned char tag_output[16];
|
|
|
|
mbedtls_gcm_context ctx;
|
2017-08-02 18:33:54 +02:00
|
|
|
size_t tag_len = tag_len_bits / 8;
|
2016-12-16 15:15:56 +01:00
|
|
|
|
|
|
|
mbedtls_gcm_init( &ctx );
|
|
|
|
|
|
|
|
memset( output, 0x00, sizeof( output ) );
|
|
|
|
memset( tag_output, 0x00, sizeof( tag_output ) );
|
2018-03-13 16:22:58 +01:00
|
|
|
|
2017-08-02 18:33:54 +02: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 15:15:56 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_gcm_free( &ctx );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2018-06-29 12:05:32 +02:00
|
|
|
void gcm_encrypt_and_tag( int cipher_id, data_t * key_str,
|
|
|
|
data_t * src_str, data_t * iv_str,
|
2020-06-26 14:33:03 +02:00
|
|
|
data_t * add_str, data_t * dst,
|
|
|
|
int tag_len_bits, data_t * tag,
|
2017-06-09 05:32:58 +02:00
|
|
|
int init_result )
|
2012-03-20 14:50:09 +01:00
|
|
|
{
|
|
|
|
unsigned char output[128];
|
|
|
|
unsigned char tag_output[16];
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_gcm_context ctx;
|
2017-05-30 15:23:15 +02:00
|
|
|
size_t tag_len = tag_len_bits / 8;
|
2021-04-13 13:02:03 +02:00
|
|
|
size_t n1;
|
2021-06-15 15:29:48 +02:00
|
|
|
size_t n1_add;
|
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(output, 0x00, 128);
|
|
|
|
memset(tag_output, 0x00, 16);
|
|
|
|
|
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == init_result );
|
2013-08-20 11:48:36 +02:00
|
|
|
if( init_result == 0 )
|
2012-03-20 14:50:09 +01:00
|
|
|
{
|
2017-06-09 05:32:58 +02: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 14:50:09 +01:00
|
|
|
|
2021-04-13 13:02:03 +02:00
|
|
|
ASSERT_COMPARE( output, src_str->len, dst->x, dst->len );
|
|
|
|
ASSERT_COMPARE( tag_output, tag_len, tag->x, tag->len );
|
|
|
|
|
2021-04-13 15:58:27 +02:00
|
|
|
for( n1 = 0; n1 <= src_str->len; n1 += 1 )
|
2021-04-13 13:02:03 +02:00
|
|
|
{
|
2021-06-15 15:29:48 +02:00
|
|
|
for( n1_add = 0; n1_add <= add_str->len; n1_add += 1 )
|
|
|
|
{
|
2021-06-17 11:40:52 +02:00
|
|
|
mbedtls_test_set_step( n1 * 10000 + n1_add );
|
2021-06-15 15:29:48 +02:00
|
|
|
if( !check_multipart( &ctx, MBEDTLS_GCM_ENCRYPT,
|
|
|
|
iv_str, add_str, src_str,
|
|
|
|
dst, tag,
|
|
|
|
n1, n1_add ) )
|
|
|
|
goto exit;
|
|
|
|
}
|
2021-04-13 13:02:03 +02:00
|
|
|
}
|
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 */
|
2018-06-29 12:05:32 +02: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 14:50:09 +01:00
|
|
|
{
|
|
|
|
unsigned char output[128];
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_gcm_context ctx;
|
2012-03-20 14:50:09 +01:00
|
|
|
int ret;
|
2017-05-30 15:23:15 +02:00
|
|
|
size_t tag_len = tag_len_bits / 8;
|
2021-04-13 13:02:03 +02:00
|
|
|
size_t n1;
|
2021-06-15 15:29:48 +02:00
|
|
|
size_t n1_add;
|
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(output, 0x00, 128);
|
|
|
|
|
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == init_result );
|
2013-08-20 11:48:36 +02:00
|
|
|
if( init_result == 0 )
|
2012-03-20 14:50:09 +01:00
|
|
|
{
|
2017-06-09 05:32:58 +02: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 14:50:09 +01:00
|
|
|
|
2017-05-31 21:46:35 +02:00
|
|
|
if( strcmp( "FAIL", 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 );
|
2021-04-13 13:02:03 +02:00
|
|
|
ASSERT_COMPARE( output, src_str->len, pt_result->x, pt_result->len );
|
|
|
|
|
2021-04-13 15:58:27 +02:00
|
|
|
for( n1 = 0; n1 <= src_str->len; n1 += 1 )
|
2021-04-13 13:02:03 +02:00
|
|
|
{
|
2021-06-15 15:29:48 +02:00
|
|
|
for( n1_add = 0; n1_add <= add_str->len; n1_add += 1 )
|
|
|
|
{
|
2021-06-17 11:40:52 +02:00
|
|
|
mbedtls_test_set_step( n1 * 10000 + n1_add );
|
2021-06-15 15:29:48 +02:00
|
|
|
if( !check_multipart( &ctx, MBEDTLS_GCM_DECRYPT,
|
|
|
|
iv_str, add_str, src_str,
|
|
|
|
pt_result, tag_str,
|
|
|
|
n1, n1_add ) )
|
|
|
|
goto exit;
|
|
|
|
}
|
2021-04-13 13:02:03 +02:00
|
|
|
}
|
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
|
|
|
|
2021-06-16 11:04:07 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void gcm_decrypt_and_verify_empty_cipher( int cipher_id,
|
|
|
|
data_t * key_str,
|
|
|
|
data_t * iv_str,
|
|
|
|
data_t * add_str,
|
|
|
|
data_t * tag_str,
|
2021-06-17 11:50:26 +02:00
|
|
|
int cipher_update_calls )
|
2021-06-16 11:04:07 +02:00
|
|
|
{
|
|
|
|
mbedtls_gcm_context ctx;
|
|
|
|
|
|
|
|
mbedtls_gcm_init( &ctx );
|
|
|
|
|
2021-06-17 11:50:26 +02:00
|
|
|
TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == 0 );
|
|
|
|
check_empty_cipher_with_ad( &ctx, MBEDTLS_GCM_DECRYPT,
|
|
|
|
iv_str, add_str, tag_str,
|
|
|
|
cipher_update_calls );
|
2021-06-16 11:04:07 +02:00
|
|
|
|
|
|
|
mbedtls_gcm_free( &ctx );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void gcm_decrypt_and_verify_empty_ad( int cipher_id,
|
|
|
|
data_t * key_str,
|
|
|
|
data_t * iv_str,
|
|
|
|
data_t * src_str,
|
|
|
|
data_t * tag_str,
|
|
|
|
data_t * pt_result,
|
2021-06-17 11:50:26 +02:00
|
|
|
int ad_update_calls )
|
2021-06-16 11:04:07 +02:00
|
|
|
{
|
|
|
|
mbedtls_gcm_context ctx;
|
|
|
|
|
|
|
|
mbedtls_gcm_init( &ctx );
|
|
|
|
|
2021-06-17 11:50:26 +02:00
|
|
|
TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == 0 );
|
|
|
|
check_cipher_with_empty_ad( &ctx, MBEDTLS_GCM_DECRYPT,
|
|
|
|
iv_str, src_str, pt_result, tag_str,
|
|
|
|
ad_update_calls );
|
2021-06-16 11:04:07 +02:00
|
|
|
|
|
|
|
mbedtls_gcm_free( &ctx );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-06-18 00:06:52 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void gcm_decrypt_and_verify_no_ad_no_cipher( int cipher_id,
|
|
|
|
data_t * key_str,
|
|
|
|
data_t * iv_str,
|
|
|
|
data_t * tag_str )
|
|
|
|
{
|
|
|
|
mbedtls_gcm_context ctx;
|
|
|
|
|
|
|
|
mbedtls_gcm_init( &ctx );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == 0 );
|
|
|
|
check_no_cipher_no_ad( &ctx, MBEDTLS_GCM_DECRYPT,
|
|
|
|
iv_str, tag_str );
|
|
|
|
|
|
|
|
mbedtls_gcm_free( &ctx );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-06-16 11:04:07 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void gcm_encrypt_and_tag_empty_cipher( int cipher_id,
|
|
|
|
data_t * key_str,
|
|
|
|
data_t * iv_str,
|
|
|
|
data_t * add_str,
|
|
|
|
data_t * tag_str,
|
2021-06-17 11:50:26 +02:00
|
|
|
int cipher_update_calls )
|
2021-06-16 11:04:07 +02:00
|
|
|
{
|
|
|
|
mbedtls_gcm_context ctx;
|
|
|
|
|
|
|
|
mbedtls_gcm_init( &ctx );
|
|
|
|
|
2021-06-17 11:50:26 +02:00
|
|
|
TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == 0 );
|
|
|
|
check_empty_cipher_with_ad( &ctx, MBEDTLS_GCM_ENCRYPT,
|
|
|
|
iv_str, add_str, tag_str,
|
|
|
|
cipher_update_calls );
|
2021-06-16 11:04:07 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_gcm_free( &ctx );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void gcm_encrypt_and_tag_empty_ad( int cipher_id,
|
|
|
|
data_t * key_str,
|
|
|
|
data_t * iv_str,
|
|
|
|
data_t * src_str,
|
|
|
|
data_t * dst,
|
|
|
|
data_t * tag_str,
|
2021-06-17 11:50:26 +02:00
|
|
|
int ad_update_calls )
|
2021-06-16 11:04:07 +02:00
|
|
|
{
|
|
|
|
mbedtls_gcm_context ctx;
|
|
|
|
|
|
|
|
mbedtls_gcm_init( &ctx );
|
|
|
|
|
2021-06-17 11:50:26 +02:00
|
|
|
TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == 0 );
|
|
|
|
check_cipher_with_empty_ad( &ctx, MBEDTLS_GCM_ENCRYPT,
|
|
|
|
iv_str, src_str, dst, tag_str,
|
|
|
|
ad_update_calls );
|
2021-06-16 11:04:07 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_gcm_free( &ctx );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-06-18 00:06:52 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void gcm_encrypt_and_verify_no_ad_no_cipher( int cipher_id,
|
|
|
|
data_t * key_str,
|
|
|
|
data_t * iv_str,
|
|
|
|
data_t * tag_str )
|
|
|
|
{
|
|
|
|
mbedtls_gcm_context ctx;
|
|
|
|
|
|
|
|
mbedtls_gcm_init( &ctx );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == 0 );
|
|
|
|
check_no_cipher_no_ad( &ctx, MBEDTLS_GCM_ENCRYPT,
|
|
|
|
iv_str, tag_str );
|
|
|
|
|
|
|
|
mbedtls_gcm_free( &ctx );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-05-25 15:15:57 +02:00
|
|
|
/* BEGIN_CASE depends_on:NOT_DEFINED */
|
2019-01-31 14:20:20 +01:00
|
|
|
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;
|
2021-05-21 08:50:00 +02:00
|
|
|
int invalid_bitlen = 1;
|
2019-01-31 14:20:20 +01:00
|
|
|
|
|
|
|
mbedtls_gcm_init( &ctx );
|
|
|
|
|
|
|
|
/* mbedtls_gcm_setkey */
|
2021-05-21 08:50:00 +02:00
|
|
|
TEST_EQUAL(
|
2019-01-31 14:20:20 +01:00
|
|
|
MBEDTLS_ERR_GCM_BAD_INPUT,
|
|
|
|
mbedtls_gcm_setkey( &ctx, valid_cipher, valid_buffer, invalid_bitlen ) );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_gcm_free( &ctx );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
|
2017-05-30 15:23:15 +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 */
|