From 725680ffd29f1f8efbe039fc505ab3711c35a37e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 25 Jul 2013 15:26:54 +0200 Subject: [PATCH] Make cipher tests less dependant on padding size --- tests/suites/test_suite_cipher.function | 130 ++++++++---------------- 1 file changed, 41 insertions(+), 89 deletions(-) diff --git a/tests/suites/test_suite_cipher.function b/tests/suites/test_suite_cipher.function index 3024623dc..765ec7380 100644 --- a/tests/suites/test_suite_cipher.function +++ b/tests/suites/test_suite_cipher.function @@ -21,7 +21,7 @@ enc_dec_buf:cipher_id:cipher_string:key_len:length: unsigned char decbuf[64]; size_t outlen = 0; - size_t enclen = 0; + size_t total_len = 0; memset( key, 0, 32 ); 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_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 */ TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); - if( POLARSSL_MODE_CBC == cipher_info->mode ) - { - TEST_ASSERT( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); - } - else - { - TEST_ASSERT( outlen == enclen ); - } + total_len = outlen; + + TEST_ASSERT( total_len == length || + ( total_len % cipher_get_block_size( &ctx_enc ) == 0 && + total_len < length && + total_len + cipher_get_block_size( &ctx_enc ) > length ) ); TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); - if( POLARSSL_MODE_CBC == cipher_info->mode ) - { - TEST_ASSERT( outlen == cipher_get_block_size ( &ctx_enc ) ); - } - else - { - TEST_ASSERT( outlen == 0 ); - } + total_len += outlen; + 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 */ - TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); - if( POLARSSL_MODE_CBC == cipher_info->mode ) - { - TEST_ASSERT( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); - } - else - { - TEST_ASSERT( enclen == outlen ); - } + TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) ); + total_len = outlen; + + TEST_ASSERT( total_len == length || + ( total_len % cipher_get_block_size( &ctx_dec ) == 0 && + total_len < length && + total_len + cipher_get_block_size( &ctx_dec ) >= length ) ); 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 ); - } + total_len += outlen; + TEST_ASSERT( total_len == 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 totaloutlen = 0; - size_t enclen = 0; memset( key, 0, 32 ); 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_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 */ TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); totaloutlen = outlen; TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); totaloutlen += outlen; - if( POLARSSL_MODE_CBC == cipher_info->mode ) - { - TEST_ASSERT( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); - } - else - { - TEST_ASSERT( totaloutlen == enclen ); - } + TEST_ASSERT( totaloutlen == length || + ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 && + totaloutlen < length && + totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) ); + TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); totaloutlen += outlen; - if( POLARSSL_MODE_CBC == cipher_info->mode ) - { - TEST_ASSERT( outlen == cipher_get_block_size ( &ctx_enc ) ); - } - else - { - TEST_ASSERT( outlen == 0 ); - } + TEST_ASSERT( totaloutlen == length || + ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 && + totaloutlen > length && + totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) ); /* decode the previously encoded string */ - TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); - if( POLARSSL_MODE_CBC == cipher_info->mode ) - { - TEST_ASSERT( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); - } - else - { - TEST_ASSERT( enclen == outlen ); - } + TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) ); + totaloutlen = outlen; + + 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 ) ); - if( POLARSSL_MODE_CBC == cipher_info->mode ) - { - TEST_ASSERT( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); - } - else - { - TEST_ASSERT( outlen == 0 ); - } - + totaloutlen += outlen; + + TEST_ASSERT( totaloutlen == length ); TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );