From 550c20fcf955b94e36762072935682415fde9660 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 10 May 2018 10:12:36 +0200 Subject: [PATCH] poly1305: add test for parameter validation Also fix two validation bugs found while adding the tests. Also handle test dependencies the right way while at it. --- library/poly1305.c | 4 +- tests/suites/test_suite_poly1305.data | 17 ++----- tests/suites/test_suite_poly1305.function | 55 +++++++++++++++++++++-- 3 files changed, 58 insertions(+), 18 deletions(-) diff --git a/library/poly1305.c b/library/poly1305.c index 542a85004..0aa453356 100644 --- a/library/poly1305.c +++ b/library/poly1305.c @@ -259,7 +259,7 @@ void mbedtls_poly1305_free( mbedtls_poly1305_context *ctx ) int mbedtls_poly1305_starts( mbedtls_poly1305_context *ctx, const unsigned char key[32] ) { - if ( ctx == NULL ) + if ( ctx == NULL || key == NULL ) { return( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); } @@ -417,7 +417,7 @@ int mbedtls_poly1305_mac( const unsigned char key[32], cleanup: mbedtls_poly1305_free( &ctx ); - return( 0 ); + return( result ); } #endif /* MBEDTLS_POLY1305_ALT */ diff --git a/tests/suites/test_suite_poly1305.data b/tests/suites/test_suite_poly1305.data index f259e848b..13912e997 100644 --- a/tests/suites/test_suite_poly1305.data +++ b/tests/suites/test_suite_poly1305.data @@ -1,51 +1,42 @@ Poly1305 RFC 7539 Example And Test Vector -depends_on:MBEDTLS_POLY1305_C mbedtls_poly1305:"85d6be7857556d337f4452fe42d506a80103808afb0db2fd4abff6af4149f51b":"a8061dc1305136c6c22b8baf0c0127a9":"43727970746f6772617068696320466f72756d2052657365617263682047726f7570" Poly1305 RFC 7539 Test Vector #1 -depends_on:MBEDTLS_POLY1305_C mbedtls_poly1305:"0000000000000000000000000000000000000000000000000000000000000000":"00000000000000000000000000000000":"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" Poly1305 RFC 7539 Test Vector #2 -depends_on:MBEDTLS_POLY1305_C mbedtls_poly1305:"0000000000000000000000000000000036e5f6b5c5e06070f0efca96227a863e":"36e5f6b5c5e06070f0efca96227a863e":"416e79207375626d697373696f6e20746f20746865204945544620696e74656e6465642062792074686520436f6e7472696275746f7220666f72207075626c69636174696f6e20617320616c6c206f722070617274206f6620616e204945544620496e7465726e65742d4472616674206f722052464320616e6420616e792073746174656d656e74206d6164652077697468696e2074686520636f6e74657874206f6620616e204945544620616374697669747920697320636f6e7369646572656420616e20224945544620436f6e747269627574696f6e222e20537563682073746174656d656e747320696e636c756465206f72616c2073746174656d656e747320696e20494554462073657373696f6e732c2061732077656c6c206173207772697474656e20616e6420656c656374726f6e696320636f6d6d756e69636174696f6e73206d61646520617420616e792074696d65206f7220706c6163652c207768696368206172652061646472657373656420746f" Poly1305 RFC 7539 Test Vector #3 -depends_on:MBEDTLS_POLY1305_C mbedtls_poly1305:"36e5f6b5c5e06070f0efca96227a863e00000000000000000000000000000000":"f3477e7cd95417af89a6b8794c310cf0":"416e79207375626d697373696f6e20746f20746865204945544620696e74656e6465642062792074686520436f6e7472696275746f7220666f72207075626c69636174696f6e20617320616c6c206f722070617274206f6620616e204945544620496e7465726e65742d4472616674206f722052464320616e6420616e792073746174656d656e74206d6164652077697468696e2074686520636f6e74657874206f6620616e204945544620616374697669747920697320636f6e7369646572656420616e20224945544620436f6e747269627574696f6e222e20537563682073746174656d656e747320696e636c756465206f72616c2073746174656d656e747320696e20494554462073657373696f6e732c2061732077656c6c206173207772697474656e20616e6420656c656374726f6e696320636f6d6d756e69636174696f6e73206d61646520617420616e792074696d65206f7220706c6163652c207768696368206172652061646472657373656420746f" Poly1305 RFC 7539 Test Vector #4 -depends_on:MBEDTLS_POLY1305_C mbedtls_poly1305:"1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0":"4541669a7eaaee61e708dc7cbcc5eb62":"2754776173206272696c6c69672c20616e642074686520736c6974687920746f7665730a446964206779726520616e642067696d626c6520696e2074686520776162653a0a416c6c206d696d737920776572652074686520626f726f676f7665732c0a416e6420746865206d6f6d65207261746873206f757467726162652e" Poly1305 RFC 7539 Test Vector #5 -depends_on:MBEDTLS_POLY1305_C mbedtls_poly1305:"0200000000000000000000000000000000000000000000000000000000000000":"03000000000000000000000000000000":"ffffffffffffffffffffffffffffffff" Poly1305 RFC 7539 Test Vector #6 -depends_on:MBEDTLS_POLY1305_C mbedtls_poly1305:"02000000000000000000000000000000ffffffffffffffffffffffffffffffff":"03000000000000000000000000000000":"02000000000000000000000000000000" Poly1305 RFC 7539 Test Vector #7 -depends_on:MBEDTLS_POLY1305_C mbedtls_poly1305:"0100000000000000000000000000000000000000000000000000000000000000":"05000000000000000000000000000000":"fffffffffffffffffffffffffffffffff0ffffffffffffffffffffffffffffff11000000000000000000000000000000" Poly1305 RFC 7539 Test Vector #8 -depends_on:MBEDTLS_POLY1305_C mbedtls_poly1305:"0100000000000000000000000000000000000000000000000000000000000000":"00000000000000000000000000000000":"fffffffffffffffffffffffffffffffffbfefefefefefefefefefefefefefefe01010101010101010101010101010101" Poly1305 RFC 7539 Test Vector #9 -depends_on:MBEDTLS_POLY1305_C mbedtls_poly1305:"0200000000000000000000000000000000000000000000000000000000000000":"faffffffffffffffffffffffffffffff":"fdffffffffffffffffffffffffffffff" Poly1305 RFC 7539 Test Vector #10 -depends_on:MBEDTLS_POLY1305_C mbedtls_poly1305:"0100000000000000040000000000000000000000000000000000000000000000":"14000000000000005500000000000000":"e33594d7505e43b900000000000000003394d7505e4379cd01000000000000000000000000000000000000000000000001000000000000000000000000000000" Poly1305 RFC 7539 Test Vector #11 -depends_on:MBEDTLS_POLY1305_C mbedtls_poly1305:"0100000000000000040000000000000000000000000000000000000000000000":"13000000000000000000000000000000":"e33594d7505e43b900000000000000003394d7505e4379cd010000000000000000000000000000000000000000000000" +Poly1305 Parameter validation +poly1305_bad_params: + Poly1305 Selftest -depends_on:MBEDTLS_SELF_TEST:MBEDTLS_POLY1305_C +depends_on:MBEDTLS_SELF_TEST poly1305_selftest: diff --git a/tests/suites/test_suite_poly1305.function b/tests/suites/test_suite_poly1305.function index 682eb05aa..c5e7989fe 100644 --- a/tests/suites/test_suite_poly1305.function +++ b/tests/suites/test_suite_poly1305.function @@ -3,7 +3,12 @@ #include /* END_HEADER */ -/* BEGIN_CASE depends_on:MBEDTLS_POLY1305_C */ +/* BEGIN_DEPENDENCIES + * depends_on:MBEDTLS_POLY1305_C + * END_DEPENDENCIES + */ + +/* BEGIN_CASE */ void mbedtls_poly1305( char *hex_key_string, char *hex_mac_string, char *hex_src_string ) { unsigned char src_str[375]; /* max size of binary input */ @@ -24,7 +29,7 @@ void mbedtls_poly1305( char *hex_key_string, char *hex_mac_string, char *hex_src /* * Test the integrated API */ - mbedtls_poly1305_mac( key, src_str, src_len, mac ); + TEST_ASSERT( mbedtls_poly1305_mac( key, src_str, src_len, mac ) == 0 ); hexify( mac_str, mac, 16 ); TEST_ASSERT( strcmp( (char *) mac_str, hex_mac_string ) == 0 ); @@ -63,7 +68,51 @@ void mbedtls_poly1305( char *hex_key_string, char *hex_mac_string, char *hex_src } /* END_CASE */ -/* BEGIN_CASE depends_on:MBEDTLS_POLY1305_C:MBEDTLS_SELF_TEST */ +/* BEGIN_CASE */ +void poly1305_bad_params() +{ + unsigned char src[1]; + unsigned char key[32]; + unsigned char mac[16]; + size_t src_len = sizeof( src ); + mbedtls_poly1305_context ctx; + + mbedtls_poly1305_init( NULL ); + mbedtls_poly1305_free( NULL ); + + mbedtls_poly1305_init( &ctx ); + + TEST_ASSERT( mbedtls_poly1305_starts( NULL, key ) + == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_poly1305_starts( &ctx, NULL ) + == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); + + TEST_ASSERT( mbedtls_poly1305_update( NULL, src, 0 ) + == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_poly1305_update( &ctx, NULL, src_len ) + == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_poly1305_update( &ctx, NULL, 0 ) + == 0 ); + + TEST_ASSERT( mbedtls_poly1305_finish( NULL, mac ) + == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_poly1305_finish( &ctx, NULL ) + == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); + + TEST_ASSERT( mbedtls_poly1305_mac( NULL, src, 0, mac ) + == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_poly1305_mac( key, NULL, src_len, mac ) + == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_poly1305_mac( key, src, 0, NULL ) + == MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); + TEST_ASSERT( mbedtls_poly1305_mac( key, NULL, 0, mac ) + == 0 ); + + mbedtls_poly1305_free( &ctx ); +} +/* END_CASE */ + +/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ void poly1305_selftest() { TEST_ASSERT( mbedtls_poly1305_self_test( 1 ) == 0 );