Extend test coverage of CMAC

Expands tests to include NULL tests and successive calls to
mbedtls_cipher_cmac_update() and mbedtls_cipher_cmac_reset().
This commit is contained in:
Simon Butcher 2016-10-10 21:41:03 +01:00
parent c61351df72
commit c3db62fd51
2 changed files with 309 additions and 3 deletions

View file

@ -1,6 +1,9 @@
CMAC self test
mbedtls_cmac_self_test:
CMAC null arguments
mbedtls_cmac_null_args:
CMAC init #1 AES-128: OK
depends_on:MBEDTLS_AES_C
mbedtls_cmac_setkey:MBEDTLS_CIPHER_AES_128_ECB:128:0
@ -21,7 +24,41 @@ CMAC init #5 AES-224: bad key size
depends_on:MBEDTLS_AES_C
mbedtls_cmac_setkey:MBEDTLS_CIPHER_ID_AES:224:MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
CMAC init #6 Camellia: wrong cipher
CMAC init #6 AES-0: bad key size
depends_on:MBEDTLS_AES_C
mbedtls_cmac_setkey:MBEDTLS_CIPHER_ID_AES:0:MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
CMAC init #7 Camellia: wrong cipher
depends_on:MBEDTLS_CAMELLIA_C
mbedtls_cmac_setkey:MBEDTLS_CIPHER_ID_CAMELLIA:128:MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
CMAC Single Blocks #1 - Empty block, no updates
mbedtls_cmac_multiple_blocks:MBEDTLS_CIPHER_AES_128_ECB:"2b7e151628aed2a6abf7158809cf4f3c":128:16:"":-1:"":-1:"":-1:"":-1:"bb1d6929e95937287fa37d129b756746"
CMAC Single Blocks #2 - Single 16 byte block
mbedtls_cmac_multiple_blocks:MBEDTLS_CIPHER_AES_128_ECB:"2b7e151628aed2a6abf7158809cf4f3c":128:16:"6bc1bee22e409f96e93d7e117393172a":16:"":-1:"":-1:"":-1:"070a16b46b4d4144f79bdd9dd04a287c"
CMAC Single Blocks #3 - Single 64 byte block
mbedtls_cmac_multiple_blocks:MBEDTLS_CIPHER_AES_128_ECB:"2b7e151628aed2a6abf7158809cf4f3c":128:16:"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710":64:"":-1:"":-1:"":-1:"51f0bebf7e3b9d92fc49741779363cfe"
CMAC Multiple Blocks #1 - Multiple 8 byte blocks
mbedtls_cmac_multiple_blocks:MBEDTLS_CIPHER_AES_128_ECB:"2b7e151628aed2a6abf7158809cf4f3c":128:16:"6bc1bee22e409f96":8:"e93d7e117393172a":8:"":-1:"":-1:"070a16b46b4d4144f79bdd9dd04a287c"
CMAC Multiple Blocks #2 - Multiple 16 byte blocks
mbedtls_cmac_multiple_blocks:MBEDTLS_CIPHER_AES_128_ECB:"2b7e151628aed2a6abf7158809cf4f3c":128:16:"6bc1bee22e409f96e93d7e117393172a":16:"ae2d8a571e03ac9c9eb76fac45af8e51":16:"30c81c46a35ce411e5fbc1191a0a52ef":16:"f69f2445df4f9b17ad2b417be66c3710":16:"51f0bebf7e3b9d92fc49741779363cfe"
CMAC Multiple Blocks #3 - Multiple variable sized blocks
mbedtls_cmac_multiple_blocks:MBEDTLS_CIPHER_AES_128_ECB:"2b7e151628aed2a6abf7158809cf4f3c":128:16:"6bc1bee22e409f96":8:"e93d7e117393172aae2d8a571e03ac9c":16:"9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52ef":24:"f69f2445df4f9b17ad2b417be66c3710":16:"51f0bebf7e3b9d92fc49741779363cfe"
CMAC Multiple Blocks #4 - Multiple 8 byte blocks with gaps
mbedtls_cmac_multiple_blocks:MBEDTLS_CIPHER_AES_128_ECB:"2b7e151628aed2a6abf7158809cf4f3c":128:16:"":0:"6bc1bee22e409f96":8:"":0:"e93d7e117393172a":8:"070a16b46b4d4144f79bdd9dd04a287c"
CMAC Multiple Operations, same key #1 - Empty, empty
mbedtls_cmac_multiple_operations_same_key:MBEDTLS_CIPHER_AES_192_ECB:"8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b":192:16:"":-1:"":-1:"":-1:"d17ddf46adaacde531cac483de7a9367":"":-1:"":-1:"":-1:"d17ddf46adaacde531cac483de7a9367"
CMAC Multiple Operations, same key #2 - Empty, 64 byte block
mbedtls_cmac_multiple_operations_same_key:MBEDTLS_CIPHER_AES_192_ECB:"8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b":192:16:"":-1:"":-1:"":-1:"d17ddf46adaacde531cac483de7a9367":"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710":64:"":-1:"":-1:"a1d5df0eed790f794d77589659f39a11"
CMAC Multiple Operations, same key #3 - variable byte blocks
mbedtls_cmac_multiple_operations_same_key:MBEDTLS_CIPHER_AES_192_ECB:"8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b":192:16:"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51":32:"30c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710":32:"":-1:"a1d5df0eed790f794d77589659f39a11":"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51":32:"30c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710":32:"":-1:"a1d5df0eed790f794d77589659f39a11"

View file

@ -15,6 +15,87 @@ void mbedtls_cmac_self_test( )
}
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_cmac_null_args( )
{
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,
test_data, 16,
test_output ) ==
MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
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 );
}
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_cmac_setkey( int cipher_type, int key_size,
int result )
@ -30,8 +111,196 @@ void mbedtls_cmac_setkey( int cipher_type, int key_size,
TEST_ASSERT( ( cipher_info = mbedtls_cipher_info_from_type( cipher_type ) )
!= NULL );
TEST_ASSERT( result == mbedtls_cipher_cmac( cipher_info, key, key_size,
buf, 16, tmp ) );
TEST_ASSERT( ( result == mbedtls_cipher_cmac( cipher_info, key, key_size,
buf, 16, tmp ) ) != 0 );
}
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_cmac_multiple_blocks( int cipher_type,
char *key_string, int keybits,
int block_size,
char *block1_string, int block1_len,
char *block2_string, int block2_len,
char *block3_string, int block3_len,
char *block4_string, int block4_len,
char *expected_result_string )
{
unsigned char key[100];
unsigned char block1[100];
unsigned char block2[100];
unsigned char block3[100];
unsigned char block4[100];
unsigned char expected_result[100];
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 */
unhexify( key, key_string );
unhexify( block1, block1_string );
unhexify( block2, block2_string );
unhexify( block3, block3_string );
unhexify( block4, block4_string );
unhexify( expected_result, expected_result_string );
/* 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 );
mbedtls_cipher_init( &ctx );
TEST_ASSERT( mbedtls_cipher_setup( &ctx, cipher_info ) == 0 );
TEST_ASSERT( mbedtls_cipher_cmac_starts( &ctx,
(const unsigned char*)key,
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,
(unsigned char*)block1,
block1_len ) == 0);
if( block2_len >= 0 )
TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
(unsigned char*)block2,
block2_len ) == 0);
if( block3_len >= 0 )
TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
(unsigned char*)block3,
block3_len ) == 0);
if( block4_len >= 0 )
TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
(unsigned char*)block4,
block4_len ) == 0);
TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, output ) == 0 );
TEST_ASSERT( memcmp( output, expected_result, block_size ) == 0 );
mbedtls_cipher_free( &ctx );
}
/* END_CASE */
/* BEGIN_CASE */
void mbedtls_cmac_multiple_operations_same_key( int cipher_type,
char *key_string, int keybits,
int block_size,
char *block_a1_string, int block_a1_len,
char *block_a2_string, int block_a2_len,
char *block_a3_string, int block_a3_len,
char *expected_result_a_string,
char *block_b1_string, int block_b1_len,
char *block_b2_string, int block_b2_len,
char *block_b3_string, int block_b3_len,
char *expected_result_b_string )
{
unsigned char key[100];
unsigned char block_a1[100];
unsigned char block_a2[100];
unsigned char block_a3[100];
unsigned char block_b1[100];
unsigned char block_b2[100];
unsigned char block_b3[100];
unsigned char expected_result_a[100], expected_result_b[100];
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 */
unhexify( key, key_string );
unhexify( block_a1, block_a1_string );
unhexify( block_a2, block_a2_string );
unhexify( block_a3, block_a3_string );
unhexify( block_b1, block_b1_string );
unhexify( block_b2, block_b2_string );
unhexify( block_b3, block_b3_string );
unhexify( expected_result_a, expected_result_a_string );
unhexify( expected_result_b, expected_result_b_string );
/* 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 );
mbedtls_cipher_init( &ctx );
TEST_ASSERT( mbedtls_cipher_setup( &ctx, cipher_info ) == 0 );
TEST_ASSERT( mbedtls_cipher_cmac_starts( &ctx,
(const unsigned char*)key,
keybits ) == 0 );
/* Sequence A */
/* Multiple partial and complete blocks. A negative length means skip the
* update operation */
if( block_a1_len >= 0)
TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
(unsigned char*)block_a1,
block_a1_len ) == 0);
if( block_a2_len >= 0 )
TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
(unsigned char*)block_a2,
block_a2_len ) == 0);
if( block_a3_len >= 0 )
TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
(unsigned char*)block_a3,
block_a3_len ) == 0);
TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, output ) == 0 );
TEST_ASSERT( memcmp( output, expected_result_a, block_size ) == 0 );
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,
(unsigned char*)block_b1,
block_b1_len ) == 0);
if( block_b2_len >= 0 )
TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
(unsigned char*)block_b2,
block_b2_len ) == 0);
if( block_b3_len >= 0 )
TEST_ASSERT( mbedtls_cipher_cmac_update( &ctx,
(unsigned char*)block_b3,
block_b3_len ) == 0);
TEST_ASSERT( mbedtls_cipher_cmac_finish( &ctx, output ) == 0 );
TEST_ASSERT( memcmp( output, expected_result_b, block_size ) == 0 );
mbedtls_cipher_free( &ctx );
}
/* END_CASE */