Skip to content

Commit

Permalink
Merge pull request #143 from Patater/chachapoly-empty-buf-test
Browse files Browse the repository at this point in the history
test: Check empty buffer decryption for chachapoly
  • Loading branch information
Patater authored Jun 7, 2019
2 parents 3d94e34 + b013146 commit 47f2de1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion tests/suites/test_suite_cipher.aes.data
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Decrypt empty buffer
depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7
dec_empty_buf:
dec_empty_buf:MBEDTLS_CIPHER_AES_128_CBC

AES-128 CBC - Encrypt and decrypt 0 bytes with PKCS7 padding
depends_on:MBEDTLS_AES_C:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_CIPHER_PADDING_PKCS7
Expand Down
4 changes: 0 additions & 4 deletions tests/suites/test_suite_cipher.chacha20.data
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
Decrypt empty buffer
depends_on:MBEDTLS_CHACHA20_C
dec_empty_buf:

Chacha20 RFC 7539 Test Vector #1
depends_on:MBEDTLS_CHACHA20_C
decrypt_test_vec:MBEDTLS_CIPHER_CHACHA20:-1:"0000000000000000000000000000000000000000000000000000000000000000":"000000000000000000000000":"76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7724e03fb8d84a376a43b8f41518a11cc387b669b2ee6586":"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000":"":"":0:0
Expand Down
2 changes: 1 addition & 1 deletion tests/suites/test_suite_cipher.chachapoly.data
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Decrypt empty buffer
depends_on:MBEDTLS_CHACHAPOLY_C
dec_empty_buf:
dec_empty_buf:MBEDTLS_CIPHER_CHACHA20_POLY1305

ChaCha20+Poly1305 Encrypt and decrypt 0 bytes
depends_on:MBEDTLS_CHACHAPOLY_C
Expand Down
30 changes: 25 additions & 5 deletions tests/suites/test_suite_cipher.function
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ exit:
/* END_CASE */

/* BEGIN_CASE */
void dec_empty_buf( )
void dec_empty_buf( int cipher )
{
unsigned char key[32];
unsigned char iv[16];
Expand All @@ -723,6 +723,8 @@ void dec_empty_buf( )

size_t outlen = 0;

int expected_ret;

memset( key, 0, 32 );
memset( iv , 0, 16 );

Expand All @@ -732,12 +734,15 @@ void dec_empty_buf( )
memset( decbuf, 0, 64 );

/* Initialise context */
cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
cipher_info = mbedtls_cipher_info_from_type( cipher );
TEST_ASSERT( NULL != cipher_info);
TEST_ASSERT( sizeof(key) * 8 >= cipher_info->key_bitlen );

TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );

TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, 128, MBEDTLS_DECRYPT ) );
TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec,
key, cipher_info->key_bitlen,
MBEDTLS_DECRYPT ) );

TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );

Expand All @@ -750,8 +755,23 @@ void dec_empty_buf( )
/* decode 0-byte string */
TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
TEST_ASSERT( 0 == outlen );
TEST_ASSERT( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED == mbedtls_cipher_finish(
&ctx_dec, decbuf + outlen, &outlen ) );

if ( cipher_info->mode == MBEDTLS_MODE_CBC ||
cipher_info->mode == MBEDTLS_MODE_ECB )
{
/* CBC and ECB ciphers need a full block of input. */
expected_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
}
else
{
/* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
* return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
* decrypting an empty buffer. */
expected_ret = 0;
}

TEST_ASSERT( expected_ret == mbedtls_cipher_finish(
&ctx_dec, decbuf + outlen, &outlen ) );
TEST_ASSERT( 0 == outlen );

exit:
Expand Down

0 comments on commit 47f2de1

Please sign in to comment.