2016-10-05 15:13:31 +02:00
|
|
|
/* BEGIN_HEADER */
|
|
|
|
#include "mbedtls/cipher.h"
|
|
|
|
#include "mbedtls/cmac.h"
|
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:MBEDTLS_CMAC_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
|
2017-05-30 15:23:15 +02:00
|
|
|
void mbedtls_cmac_self_test( )
|
2016-10-05 15:13:31 +02:00
|
|
|
{
|
|
|
|
TEST_ASSERT( mbedtls_cmac_self_test( 1 ) == 0 );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2016-10-10 22:41:03 +02:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 15:23:15 +02:00
|
|
|
void mbedtls_cmac_null_args( )
|
2016-10-10 22:41:03 +02:00
|
|
|
{
|
|
|
|
mbedtls_cipher_context_t ctx;
|
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
|
|
|
unsigned char test_key[MBEDTLS_CIPHER_BLKSIZE_MAX];
|
|
|
|
unsigned char test_data[MBEDTLS_CIPHER_BLKSIZE_MAX];
|
|
|
|
unsigned char test_output[MBEDTLS_CIPHER_BLKSIZE_MAX];
|
|
|
|
|
|
|
|
mbedtls_cipher_init( &ctx );
|
|
|
|
|
|
|
|
/* Test NULL cipher info */
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx, test_data, 16 ) ==
|
|
|
|
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
|
|
|
|
TEST_ASSERT( mbedtls_cipher_setup( &ctx, cipher_info ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_starts( NULL, test_key, 128 ) ==
|
|
|
|
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_starts( &ctx, NULL, 128 ) ==
|
|
|
|
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_update( NULL, test_data, 16 ) ==
|
|
|
|
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx, NULL, 16 ) ==
|
|
|
|
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_finish( NULL, test_output ) ==
|
|
|
|
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, NULL ) ==
|
|
|
|
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_reset( NULL ) ==
|
|
|
|
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac( NULL,
|
|
|
|
test_key, 128,
|
|
|
|
test_data, 16,
|
|
|
|
test_output ) ==
|
|
|
|
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac( cipher_info,
|
|
|
|
NULL, 128,
|
|
|
|
test_data, 16,
|
|
|
|
test_output ) ==
|
|
|
|
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac( cipher_info,
|
|
|
|
test_key, 128,
|
|
|
|
NULL, 16,
|
|
|
|
test_output ) ==
|
|
|
|
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac( cipher_info,
|
|
|
|
test_key, 128,
|
|
|
|
test_data, 16,
|
|
|
|
NULL ) ==
|
|
|
|
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_aes_cmac_prf_128( NULL, 16,
|
2016-10-11 13:05:51 +02:00
|
|
|
test_data, 16,
|
|
|
|
test_output ) ==
|
|
|
|
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2016-10-10 22:41:03 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_aes_cmac_prf_128( test_key, 16,
|
|
|
|
NULL, 16,
|
|
|
|
test_output ) ==
|
|
|
|
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_aes_cmac_prf_128( test_key, 16,
|
|
|
|
test_data, 16,
|
|
|
|
NULL ) ==
|
|
|
|
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
2016-10-11 16:41:40 +02:00
|
|
|
exit:
|
|
|
|
mbedtls_cipher_free( &ctx );
|
2016-10-10 22:41:03 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2016-10-05 15:13:31 +02:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 15:23:15 +02:00
|
|
|
void mbedtls_cmac_setkey( int cipher_type, int key_size, int result )
|
2016-10-05 15:13:31 +02:00
|
|
|
{
|
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
|
|
|
unsigned char key[32];
|
|
|
|
unsigned char buf[16];
|
|
|
|
unsigned char tmp[16];
|
|
|
|
|
|
|
|
memset( key, 0x2A, sizeof( key ) );
|
|
|
|
TEST_ASSERT( (unsigned) key_size <= 8 * sizeof( key ) );
|
|
|
|
|
|
|
|
TEST_ASSERT( ( cipher_info = mbedtls_cipher_info_from_type( cipher_type ) )
|
|
|
|
!= NULL );
|
|
|
|
|
2016-10-12 11:00:42 +02:00
|
|
|
memset( buf, 0x2A, sizeof( buf ) );
|
2016-10-10 22:41:03 +02:00
|
|
|
TEST_ASSERT( ( result == mbedtls_cipher_cmac( cipher_info, key, key_size,
|
|
|
|
buf, 16, tmp ) ) != 0 );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-06-29 12:05:32 +02:00
|
|
|
void mbedtls_cmac_multiple_blocks( int cipher_type, data_t * key,
|
2017-06-09 05:32:58 +02:00
|
|
|
int keybits, int block_size,
|
2018-06-29 12:05:32 +02:00
|
|
|
data_t * block1, int block1_len,
|
|
|
|
data_t * block2, int block2_len,
|
|
|
|
data_t * block3, int block3_len,
|
|
|
|
data_t * block4, int block4_len,
|
|
|
|
data_t * expected_result )
|
2016-10-10 22:41:03 +02:00
|
|
|
{
|
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
|
|
|
mbedtls_cipher_context_t ctx;
|
|
|
|
unsigned char output[MBEDTLS_CIPHER_BLKSIZE_MAX];
|
|
|
|
|
|
|
|
/* Convert the test parameters to binary data */
|
|
|
|
|
2016-10-11 16:41:40 +02:00
|
|
|
mbedtls_cipher_init( &ctx );
|
|
|
|
|
2016-10-10 22:41:03 +02:00
|
|
|
/* Validate the test inputs */
|
|
|
|
TEST_ASSERT( block1_len <= 100 );
|
|
|
|
TEST_ASSERT( block2_len <= 100 );
|
|
|
|
TEST_ASSERT( block3_len <= 100 );
|
|
|
|
TEST_ASSERT( block4_len <= 100 );
|
|
|
|
|
|
|
|
/* Set up */
|
|
|
|
TEST_ASSERT( ( cipher_info = mbedtls_cipher_info_from_type( cipher_type ) )
|
|
|
|
!= NULL );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_cipher_setup( &ctx, cipher_info ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_starts( &ctx,
|
2017-06-09 05:32:58 +02:00
|
|
|
(const unsigned char*)key->x,
|
2016-10-10 22:41:03 +02:00
|
|
|
keybits ) == 0 );
|
|
|
|
|
|
|
|
/* Multiple partial and complete blocks. A negative length means skip the
|
|
|
|
* update operation */
|
|
|
|
if( block1_len >= 0)
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
|
2017-06-09 05:32:58 +02:00
|
|
|
(unsigned char*)block1->x,
|
2016-10-10 22:41:03 +02:00
|
|
|
block1_len ) == 0);
|
|
|
|
|
|
|
|
if( block2_len >= 0 )
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
|
2017-06-09 05:32:58 +02:00
|
|
|
(unsigned char*)block2->x,
|
2016-10-10 22:41:03 +02:00
|
|
|
block2_len ) == 0);
|
|
|
|
|
|
|
|
if( block3_len >= 0 )
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
|
2017-06-09 05:32:58 +02:00
|
|
|
(unsigned char*)block3->x,
|
2016-10-10 22:41:03 +02:00
|
|
|
block3_len ) == 0);
|
|
|
|
|
|
|
|
if( block4_len >= 0 )
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
|
2017-06-09 05:32:58 +02:00
|
|
|
(unsigned char*)block4->x,
|
2016-10-10 22:41:03 +02:00
|
|
|
block4_len ) == 0);
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, output ) == 0 );
|
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT( memcmp( output, expected_result->x, block_size ) == 0 );
|
2016-10-10 22:41:03 +02:00
|
|
|
|
2016-10-11 13:05:51 +02:00
|
|
|
exit:
|
2016-10-10 22:41:03 +02:00
|
|
|
mbedtls_cipher_free( &ctx );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mbedtls_cmac_multiple_operations_same_key( int cipher_type,
|
2018-06-29 12:05:32 +02:00
|
|
|
data_t * key, int keybits,
|
2017-05-30 15:23:15 +02:00
|
|
|
int block_size,
|
2018-06-29 12:05:32 +02:00
|
|
|
data_t * block_a1,
|
2017-05-30 15:23:15 +02:00
|
|
|
int block_a1_len,
|
2018-06-29 12:05:32 +02:00
|
|
|
data_t * block_a2,
|
2017-05-30 15:23:15 +02:00
|
|
|
int block_a2_len,
|
2018-06-29 12:05:32 +02:00
|
|
|
data_t * block_a3,
|
2017-05-30 15:23:15 +02:00
|
|
|
int block_a3_len,
|
2018-06-29 12:05:32 +02:00
|
|
|
data_t * expected_result_a,
|
|
|
|
data_t * block_b1,
|
2017-05-30 15:23:15 +02:00
|
|
|
int block_b1_len,
|
2018-06-29 12:05:32 +02:00
|
|
|
data_t * block_b2,
|
2017-05-30 15:23:15 +02:00
|
|
|
int block_b2_len,
|
2018-06-29 12:05:32 +02:00
|
|
|
data_t * block_b3,
|
2017-05-30 15:23:15 +02:00
|
|
|
int block_b3_len,
|
2018-06-29 12:05:32 +02:00
|
|
|
data_t * expected_result_b
|
2017-05-30 15:23:15 +02:00
|
|
|
)
|
2016-10-10 22:41:03 +02:00
|
|
|
{
|
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
|
|
|
mbedtls_cipher_context_t ctx;
|
|
|
|
unsigned char output[MBEDTLS_CIPHER_BLKSIZE_MAX];
|
|
|
|
|
|
|
|
/* Convert the test parameters to binary data */
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-10-11 16:41:40 +02:00
|
|
|
mbedtls_cipher_init( &ctx );
|
|
|
|
|
2016-10-10 22:41:03 +02:00
|
|
|
/* Validate the test inputs */
|
|
|
|
TEST_ASSERT( block_a1_len <= 100 );
|
|
|
|
TEST_ASSERT( block_a2_len <= 100 );
|
|
|
|
TEST_ASSERT( block_a3_len <= 100 );
|
|
|
|
|
|
|
|
TEST_ASSERT( block_b1_len <= 100 );
|
|
|
|
TEST_ASSERT( block_b2_len <= 100 );
|
|
|
|
TEST_ASSERT( block_b3_len <= 100 );
|
|
|
|
|
|
|
|
/* Set up */
|
|
|
|
TEST_ASSERT( ( cipher_info = mbedtls_cipher_info_from_type( cipher_type ) )
|
|
|
|
!= NULL );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_cipher_setup( &ctx, cipher_info ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_starts( &ctx,
|
2017-06-09 05:32:58 +02:00
|
|
|
(const unsigned char*)key->x,
|
2016-10-10 22:41:03 +02:00
|
|
|
keybits ) == 0 );
|
|
|
|
|
|
|
|
/* Sequence A */
|
|
|
|
|
|
|
|
/* Multiple partial and complete blocks. A negative length means skip the
|
|
|
|
* update operation */
|
2016-10-11 13:05:51 +02:00
|
|
|
if( block_a1_len >= 0 )
|
2016-10-10 22:41:03 +02:00
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
|
2017-06-09 05:32:58 +02:00
|
|
|
(unsigned char*)block_a1->x,
|
2016-10-10 22:41:03 +02:00
|
|
|
block_a1_len ) == 0);
|
|
|
|
|
|
|
|
if( block_a2_len >= 0 )
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
|
2017-06-09 05:32:58 +02:00
|
|
|
(unsigned char*)block_a2->x,
|
2016-10-10 22:41:03 +02:00
|
|
|
block_a2_len ) == 0);
|
|
|
|
|
|
|
|
if( block_a3_len >= 0 )
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
|
2017-06-09 05:32:58 +02:00
|
|
|
(unsigned char*)block_a3->x,
|
2016-10-10 22:41:03 +02:00
|
|
|
block_a3_len ) == 0);
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, output ) == 0 );
|
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT( memcmp( output, expected_result_a->x, block_size ) == 0 );
|
2016-10-10 22:41:03 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_reset( &ctx ) == 0 );
|
|
|
|
|
|
|
|
/* Sequence B */
|
|
|
|
|
|
|
|
/* Multiple partial and complete blocks. A negative length means skip the
|
|
|
|
* update operation */
|
|
|
|
if( block_b1_len >= 0)
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
|
2017-06-09 05:32:58 +02:00
|
|
|
(unsigned char*)block_b1->x,
|
2016-10-10 22:41:03 +02:00
|
|
|
block_b1_len ) == 0);
|
|
|
|
|
|
|
|
if( block_b2_len >= 0 )
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
|
2017-06-09 05:32:58 +02:00
|
|
|
(unsigned char*)block_b2->x,
|
2016-10-10 22:41:03 +02:00
|
|
|
block_b2_len ) == 0);
|
|
|
|
|
|
|
|
if( block_b3_len >= 0 )
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
|
2017-06-09 05:32:58 +02:00
|
|
|
(unsigned char*)block_b3->x,
|
2016-10-10 22:41:03 +02:00
|
|
|
block_b3_len ) == 0);
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, output ) == 0 );
|
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT( memcmp( output, expected_result_b->x, block_size ) == 0 );
|
2016-10-10 22:41:03 +02:00
|
|
|
|
2016-10-11 13:05:51 +02:00
|
|
|
exit:
|
2016-10-10 22:41:03 +02:00
|
|
|
mbedtls_cipher_free( &ctx );
|
2016-10-05 15:13:31 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|