diff --git a/library/chachapoly.c b/library/chachapoly.c index 9ca21b39a..fd05886fb 100644 --- a/library/chachapoly.c +++ b/library/chachapoly.c @@ -202,7 +202,7 @@ int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx, const unsigned char *input, unsigned char *output ) { - if ( ( ctx == NULL ) || ( input == NULL ) || ( output == NULL ) ) + if ( ctx == NULL ) { return( MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); } @@ -339,6 +339,9 @@ int mbedtls_chachapoly_auth_decrypt( mbedtls_chachapoly_context *ctx, size_t i; int diff; + if( tag == NULL ) + return( MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + if( ( ret = mbedtls_chachapoly_crypt_and_tag( ctx, MBEDTLS_CHACHAPOLY_DECRYPT, length, nonce, aad, aad_len, input, output, check_tag ) ) != 0 ) diff --git a/tests/suites/test_suite_chachapoly.data b/tests/suites/test_suite_chachapoly.data index f0b4a0de6..b0eedea27 100644 --- a/tests/suites/test_suite_chachapoly.data +++ b/tests/suites/test_suite_chachapoly.data @@ -1,27 +1,24 @@ ChaCha20-Poly1305 RFC 7539 Example and Test Vector (Encrypt) -depends_on:MBEDTLS_CHACHAPOLY_C mbedtls_chachapoly_enc:"808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f":"070000004041424344454647":"50515253c0c1c2c3c4c5c6c7":"4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e":"d31a8d34648e60db7b86afbc53ef7ec2a4aded51296e08fea9e2b5a736ee62d63dbea45e8ca9671282fafb69da92728b1a71de0a9e060b2905d6a5b67ecd3b3692ddbd7f2d778b8c9803aee328091b58fab324e4fad675945585808b4831d7bc3ff4def08e4b7a9de576d26586cec64b6116":"1ae10b594f09e26a7e902ecbd0600691" ChaCha20-Poly1305 RFC 7539 Example and Test Vector (Decrypt) -depends_on:MBEDTLS_CHACHAPOLY_C mbedtls_chachapoly_dec:"808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f":"070000004041424344454647":"50515253c0c1c2c3c4c5c6c7":"d31a8d34648e60db7b86afbc53ef7ec2a4aded51296e08fea9e2b5a736ee62d63dbea45e8ca9671282fafb69da92728b1a71de0a9e060b2905d6a5b67ecd3b3692ddbd7f2d778b8c9803aee328091b58fab324e4fad675945585808b4831d7bc3ff4def08e4b7a9de576d26586cec64b6116":"4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e":"1ae10b594f09e26a7e902ecbd0600691":0 ChaCha20-Poly1305 RFC 7539 Example and Test Vector (Decrypt, not authentic) -depends_on:MBEDTLS_CHACHAPOLY_C mbedtls_chachapoly_dec:"808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f":"070000004041424344454647":"50515253c0c1c2c3c4c5c6c7":"d31a8d34648e60db7b86afbc53ef7ec2a4aded51296e08fea9e2b5a736ee62d63dbea45e8ca9671282fafb69da92728b1a71de0a9e060b2905d6a5b67ecd3b3692ddbd7f2d778b8c9803aee328091b58fab324e4fad675945585808b4831d7bc3ff4def08e4b7a9de576d26586cec64b6116":"4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e":"1ae10b594f09e26a7e902ecbd0600690":MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED ChaCha20-Poly1305 RFC 7539 Test Vector #1 (Encrypt) -depends_on:MBEDTLS_CHACHAPOLY_C mbedtls_chachapoly_enc:"1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0":"000000000102030405060708":"f33388860000000000004e91":"496e7465726e65742d4472616674732061726520647261667420646f63756d656e74732076616c696420666f722061206d6178696d756d206f6620736978206d6f6e74687320616e64206d617920626520757064617465642c207265706c616365642c206f72206f62736f6c65746564206279206f7468657220646f63756d656e747320617420616e792074696d652e20497420697320696e617070726f70726961746520746f2075736520496e7465726e65742d447261667473206173207265666572656e6365206d6174657269616c206f7220746f2063697465207468656d206f74686572207468616e206173202fe2809c776f726b20696e2070726f67726573732e2fe2809d":"64a0861575861af460f062c79be643bd5e805cfd345cf389f108670ac76c8cb24c6cfc18755d43eea09ee94e382d26b0bdb7b73c321b0100d4f03b7f355894cf332f830e710b97ce98c8a84abd0b948114ad176e008d33bd60f982b1ff37c8559797a06ef4f0ef61c186324e2b3506383606907b6a7c02b0f9f6157b53c867e4b9166c767b804d46a59b5216cde7a4e99040c5a40433225ee282a1b0a06c523eaf4534d7f83fa1155b0047718cbc546a0d072b04b3564eea1b422273f548271a0bb2316053fa76991955ebd63159434ecebb4e466dae5a1073a6727627097a1049e617d91d361094fa68f0ff77987130305beaba2eda04df997b714d6c6f2c29a6ad5cb4022b02709b":"eead9d67890cbb22392336fea1851f38" ChaCha20-Poly1305 RFC 7539 Test Vector #1 (Decrypt) -depends_on:MBEDTLS_CHACHAPOLY_C mbedtls_chachapoly_dec:"1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0":"000000000102030405060708":"f33388860000000000004e91":"64a0861575861af460f062c79be643bd5e805cfd345cf389f108670ac76c8cb24c6cfc18755d43eea09ee94e382d26b0bdb7b73c321b0100d4f03b7f355894cf332f830e710b97ce98c8a84abd0b948114ad176e008d33bd60f982b1ff37c8559797a06ef4f0ef61c186324e2b3506383606907b6a7c02b0f9f6157b53c867e4b9166c767b804d46a59b5216cde7a4e99040c5a40433225ee282a1b0a06c523eaf4534d7f83fa1155b0047718cbc546a0d072b04b3564eea1b422273f548271a0bb2316053fa76991955ebd63159434ecebb4e466dae5a1073a6727627097a1049e617d91d361094fa68f0ff77987130305beaba2eda04df997b714d6c6f2c29a6ad5cb4022b02709b":"496e7465726e65742d4472616674732061726520647261667420646f63756d656e74732076616c696420666f722061206d6178696d756d206f6620736978206d6f6e74687320616e64206d617920626520757064617465642c207265706c616365642c206f72206f62736f6c65746564206279206f7468657220646f63756d656e747320617420616e792074696d652e20497420697320696e617070726f70726961746520746f2075736520496e7465726e65742d447261667473206173207265666572656e6365206d6174657269616c206f7220746f2063697465207468656d206f74686572207468616e206173202fe2809c776f726b20696e2070726f67726573732e2fe2809d":"eead9d67890cbb22392336fea1851f38":0 ChaCha20-Poly1305 RFC 7539 Test Vector #1 (Decrypt, not authentic) -depends_on:MBEDTLS_CHACHAPOLY_C mbedtls_chachapoly_dec:"1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0":"000000000102030405060708":"f33388860000000000004e91":"64a0861575861af460f062c79be643bd5e805cfd345cf389f108670ac76c8cb24c6cfc18755d43eea09ee94e382d26b0bdb7b73c321b0100d4f03b7f355894cf332f830e710b97ce98c8a84abd0b948114ad176e008d33bd60f982b1ff37c8559797a06ef4f0ef61c186324e2b3506383606907b6a7c02b0f9f6157b53c867e4b9166c767b804d46a59b5216cde7a4e99040c5a40433225ee282a1b0a06c523eaf4534d7f83fa1155b0047718cbc546a0d072b04b3564eea1b422273f548271a0bb2316053fa76991955ebd63159434ecebb4e466dae5a1073a6727627097a1049e617d91d361094fa68f0ff77987130305beaba2eda04df997b714d6c6f2c29a6ad5cb4022b02709b":"496e7465726e65742d4472616674732061726520647261667420646f63756d656e74732076616c696420666f722061206d6178696d756d206f6620736978206d6f6e74687320616e64206d617920626520757064617465642c207265706c616365642c206f72206f62736f6c65746564206279206f7468657220646f63756d656e747320617420616e792074696d652e20497420697320696e617070726f70726961746520746f2075736520496e7465726e65742d447261667473206173207265666572656e6365206d6174657269616c206f7220746f2063697465207468656d206f74686572207468616e206173202fe2809c776f726b20696e2070726f67726573732e2fe2809d":"fead9d67890cbb22392336fea1851f38":MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED +ChaCha20-Poly1305 Parameter Validation +chachapoly_bad_params: + ChaCha20-Poly1305 Selftest -depends_on:MBEDTLS_CHACHAPOLY_C:MBEDTLS_SELF_TEST +depends_on:MBEDTLS_SELF_TEST chachapoly_selftest: diff --git a/tests/suites/test_suite_chachapoly.function b/tests/suites/test_suite_chachapoly.function index a613870b3..3f8145a54 100644 --- a/tests/suites/test_suite_chachapoly.function +++ b/tests/suites/test_suite_chachapoly.function @@ -46,13 +46,13 @@ void mbedtls_chachapoly_enc( char *hex_key_string, char *hex_nonce_string, char mbedtls_chachapoly_init( &ctx ); - mbedtls_chachapoly_setkey( &ctx, key_str ); + TEST_ASSERT( mbedtls_chachapoly_setkey( &ctx, key_str ) == 0 ); - mbedtls_chachapoly_crypt_and_tag( &ctx, + TEST_ASSERT( mbedtls_chachapoly_crypt_and_tag( &ctx, MBEDTLS_CHACHAPOLY_ENCRYPT, input_len, nonce_str, aad_str, aad_len, - input_str, output, mac ); + input_str, output, mac ) == 0 ); TEST_ASSERT( memcmp( output_str, output, output_len ) == 0 ); TEST_ASSERT( memcmp( mac_str, mac, 16U ) == 0 ); @@ -101,7 +101,7 @@ void mbedtls_chachapoly_dec( char *hex_key_string, char *hex_nonce_string, char mbedtls_chachapoly_init( &ctx ); - mbedtls_chachapoly_setkey( &ctx, key_str ); + TEST_ASSERT( mbedtls_chachapoly_setkey( &ctx, key_str ) == 0 ); ret = mbedtls_chachapoly_auth_decrypt( &ctx, input_len, nonce_str, @@ -119,6 +119,155 @@ exit: } /* END_CASE */ +/* BEGIN_CASE */ +void chachapoly_bad_params() +{ + unsigned char key[32]; + unsigned char nonce[12]; + unsigned char aad[1]; + unsigned char input[1]; + unsigned char output[1]; + unsigned char mac[16]; + size_t input_len = sizeof( input ); + size_t aad_len = sizeof( aad ); + mbedtls_chachapoly_context ctx; + + memset( key, 0x00, sizeof( key ) ); + memset( nonce, 0x00, sizeof( nonce ) ); + memset( aad, 0x00, sizeof( aad ) ); + memset( input, 0x00, sizeof( input ) ); + memset( output, 0x00, sizeof( output ) ); + memset( mac, 0x00, sizeof( mac ) ); + + mbedtls_chachapoly_init( NULL ); + mbedtls_chachapoly_free( NULL ); + + mbedtls_chachapoly_init( &ctx ); + + TEST_ASSERT( mbedtls_chachapoly_setkey( NULL, key ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_chachapoly_setkey( &ctx, NULL ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + + TEST_ASSERT( mbedtls_chachapoly_crypt_and_tag( NULL, + MBEDTLS_CHACHAPOLY_ENCRYPT, + 0, nonce, + aad, 0, + input, output, mac ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_chachapoly_crypt_and_tag( &ctx, + MBEDTLS_CHACHAPOLY_ENCRYPT, + 0, NULL, + aad, 0, + input, output, mac ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_chachapoly_crypt_and_tag( &ctx, + MBEDTLS_CHACHAPOLY_ENCRYPT, + 0, nonce, + NULL, aad_len, + input, output, mac ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_chachapoly_crypt_and_tag( &ctx, + MBEDTLS_CHACHAPOLY_ENCRYPT, + input_len, nonce, + aad, 0, + NULL, output, mac ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_chachapoly_crypt_and_tag( &ctx, + MBEDTLS_CHACHAPOLY_ENCRYPT, + input_len, nonce, + aad, 0, + input, NULL, mac ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_chachapoly_crypt_and_tag( &ctx, + MBEDTLS_CHACHAPOLY_ENCRYPT, + 0, nonce, + aad, 0, + input, output, NULL ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + + TEST_ASSERT( mbedtls_chachapoly_auth_decrypt( NULL, + 0, nonce, + aad, 0, + mac, input, output ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_chachapoly_auth_decrypt( &ctx, + 0, NULL, + aad, 0, + mac, input, output ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_chachapoly_auth_decrypt( &ctx, + 0, nonce, + NULL, aad_len, + mac, input, output ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_chachapoly_auth_decrypt( &ctx, + 0, nonce, + aad, 0, + NULL, input, output ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_chachapoly_auth_decrypt( &ctx, + input_len, nonce, + aad, 0, + mac, NULL, output ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_chachapoly_auth_decrypt( &ctx, + input_len, nonce, + aad, 0, + mac, input, NULL ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + + TEST_ASSERT( mbedtls_chachapoly_crypt_and_tag( &ctx, + MBEDTLS_CHACHAPOLY_ENCRYPT, + 0, nonce, + aad, aad_len, + NULL, NULL, mac ) + == 0 ); + TEST_ASSERT( mbedtls_chachapoly_auth_decrypt( &ctx, + 0, nonce, + aad, aad_len, + mac, NULL, NULL ) + == 0 ); + + TEST_ASSERT( mbedtls_chachapoly_crypt_and_tag( &ctx, + MBEDTLS_CHACHAPOLY_ENCRYPT, + input_len, nonce, + NULL, 0, + input, output, mac ) + == 0 ); + TEST_ASSERT( mbedtls_chachapoly_auth_decrypt( &ctx, + input_len, nonce, + NULL, 0, + mac, input, output ) + == 0 ); + + TEST_ASSERT( mbedtls_chachapoly_starts( NULL, nonce, MBEDTLS_CHACHAPOLY_ENCRYPT ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_chachapoly_starts( &ctx, NULL, MBEDTLS_CHACHAPOLY_ENCRYPT ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + + TEST_ASSERT( mbedtls_chachapoly_update_aad( NULL, aad, aad_len ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_chachapoly_update_aad( &ctx, NULL, aad_len ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + + TEST_ASSERT( mbedtls_chachapoly_update( NULL, input_len, input, output ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_chachapoly_update( &ctx, input_len, NULL, output ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_chachapoly_update( &ctx, input_len, input, NULL ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + + TEST_ASSERT( mbedtls_chachapoly_finish( NULL, mac ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_chachapoly_finish( &ctx, NULL ) + == MBEDTLS_ERR_CHACHAPOLY_BAD_INPUT_DATA ); + +exit: + mbedtls_chachapoly_free( &ctx ); +} +/* END_CASE */ + /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ void chachapoly_selftest() {