Make cipher tests less dependant on padding size

This commit is contained in:
Manuel Pégourié-Gonnard 2013-07-25 15:26:54 +02:00 committed by Paul Bakker
parent ac56a1aec4
commit 725680ffd2

View file

@ -21,7 +21,7 @@ enc_dec_buf:cipher_id:cipher_string:key_len:length:
unsigned char decbuf[64]; unsigned char decbuf[64];
size_t outlen = 0; size_t outlen = 0;
size_t enclen = 0; size_t total_len = 0;
memset( key, 0, 32 ); memset( key, 0, 32 );
memset( iv , 0, 16 ); memset( iv , 0, 16 );
@ -48,59 +48,36 @@ enc_dec_buf:cipher_id:cipher_string:key_len:length:
TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) ); TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) );
TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv ) ); TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv ) );
if( POLARSSL_MODE_CBC == cipher_info->mode )
{
enclen = cipher_get_block_size( &ctx_enc )
* ( 1 + length / cipher_get_block_size( &ctx_enc ) );
}
else
{
enclen = length;
}
/* encode length number of bytes from inbuf */ /* encode length number of bytes from inbuf */
TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
if( POLARSSL_MODE_CBC == cipher_info->mode ) total_len = outlen;
{
TEST_ASSERT( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); TEST_ASSERT( total_len == length ||
} ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
else total_len < length &&
{ total_len + cipher_get_block_size( &ctx_enc ) > length ) );
TEST_ASSERT( outlen == enclen );
}
TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
if( POLARSSL_MODE_CBC == cipher_info->mode ) total_len += outlen;
{
TEST_ASSERT( outlen == cipher_get_block_size ( &ctx_enc ) );
}
else
{
TEST_ASSERT( outlen == 0 );
}
TEST_ASSERT( total_len == length ||
( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
total_len > length &&
total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
/* decode the previously encoded string */ /* decode the previously encoded string */
TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
if( POLARSSL_MODE_CBC == cipher_info->mode ) total_len = outlen;
{
TEST_ASSERT( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); TEST_ASSERT( total_len == length ||
} ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
else total_len < length &&
{ total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
TEST_ASSERT( enclen == outlen );
}
TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
if( POLARSSL_MODE_CBC == cipher_info->mode ) total_len += outlen;
{
TEST_ASSERT( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
}
else
{
TEST_ASSERT( outlen == 0 );
}
TEST_ASSERT( total_len == length );
TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) ); TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
@ -166,7 +143,6 @@ enc_dec_buf_multipart:cipher_id:key_len:first_length:second_length:
size_t outlen = 0; size_t outlen = 0;
size_t totaloutlen = 0; size_t totaloutlen = 0;
size_t enclen = 0;
memset( key, 0, 32 ); memset( key, 0, 32 );
memset( iv , 0, 16 ); memset( iv , 0, 16 );
@ -191,60 +167,36 @@ enc_dec_buf_multipart:cipher_id:key_len:first_length:second_length:
TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) ); TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) );
TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv ) ); TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv ) );
if( POLARSSL_MODE_CBC == cipher_info->mode )
{
enclen = cipher_get_block_size(&ctx_enc )
* ( 1 + length / cipher_get_block_size( &ctx_enc ) );
}
else
{
enclen = length;
}
/* encode length number of bytes from inbuf */ /* encode length number of bytes from inbuf */
TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
totaloutlen = outlen; totaloutlen = outlen;
TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
totaloutlen += outlen; totaloutlen += outlen;
if( POLARSSL_MODE_CBC == cipher_info->mode ) TEST_ASSERT( totaloutlen == length ||
{ ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
TEST_ASSERT( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); totaloutlen < length &&
} totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
else
{
TEST_ASSERT( totaloutlen == enclen );
}
TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
totaloutlen += outlen; totaloutlen += outlen;
if( POLARSSL_MODE_CBC == cipher_info->mode ) TEST_ASSERT( totaloutlen == length ||
{ ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
TEST_ASSERT( outlen == cipher_get_block_size ( &ctx_enc ) ); totaloutlen > length &&
} totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
else
{
TEST_ASSERT( outlen == 0 );
}
/* decode the previously encoded string */ /* decode the previously encoded string */
TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
if( POLARSSL_MODE_CBC == cipher_info->mode ) totaloutlen = outlen;
{
TEST_ASSERT( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
}
else
{
TEST_ASSERT( enclen == outlen );
}
TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
if( POLARSSL_MODE_CBC == cipher_info->mode )
{
TEST_ASSERT( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
}
else
{
TEST_ASSERT( outlen == 0 );
}
TEST_ASSERT( totaloutlen == length ||
( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
totaloutlen < length &&
totaloutlen + cipher_get_block_size( &ctx_dec ) > length ) );
TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
totaloutlen += outlen;
TEST_ASSERT( totaloutlen == length );
TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) ); TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );