Make cipher tests less dependant on padding size
diff --git a/tests/suites/test_suite_cipher.function b/tests/suites/test_suite_cipher.function
index 3024623..765ec73 100644
--- a/tests/suites/test_suite_cipher.function
+++ b/tests/suites/test_suite_cipher.function
@@ -21,7 +21,7 @@
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 @@
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 @@
size_t outlen = 0;
size_t totaloutlen = 0;
- size_t enclen = 0;
memset( key, 0, 32 );
memset( iv , 0, 16 );
@@ -191,60 +167,36 @@
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) );