From 3fec220a33bf86c201074f5da86da385c698b01a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Sat, 29 Mar 2014 16:42:38 +0100 Subject: [PATCH] Add test for dhm_parse_dhmfile --- ChangeLog | 1 + library/dhm.c | 2 ++ tests/data_files/dhparams.pem | 5 +++++ tests/suites/test_suite_dhm.data | 3 +++ tests/suites/test_suite_dhm.function | 23 +++++++++++++++++++++++ 5 files changed, 34 insertions(+) create mode 100644 tests/data_files/dhparams.pem diff --git a/ChangeLog b/ChangeLog index b9d44d94f..01c9c876b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -21,6 +21,7 @@ Bugfix (found by Gergely Budai) * oid_get_numeric_string() used to truncate the output without returning an error if the output buffer was just 1 byte too small. + * dhm_parse_dhm() (hence dhm_parse_dhmfile()) did not set dhm->len. = PolarSSL 1.3.5 released on 2014-03-26 Features diff --git a/library/dhm.c b/library/dhm.c index 8bc9f0e34..406404aea 100644 --- a/library/dhm.c +++ b/library/dhm.c @@ -460,6 +460,8 @@ int dhm_parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen ret = 0; + dhm->len = mpi_size( &dhm->P ); + exit: #if defined(POLARSSL_PEM_PARSE_C) pem_free( &pem ); diff --git a/tests/data_files/dhparams.pem b/tests/data_files/dhparams.pem new file mode 100644 index 000000000..7ce05f0c0 --- /dev/null +++ b/tests/data_files/dhparams.pem @@ -0,0 +1,5 @@ +-----BEGIN DH PARAMETERS----- +MIGHAoGBAJ419DBEOgmQTzo5qXl5fQcN9TN455wkOL7052HzxxRVMyhYmwQcgJvh +1sa18fyfR9OiVEMYglOpkqVoGLN7qd5aQNNi5W7/C+VBdHTBJcGZJyyP5B3qcz32 +9mLJKudlVudV0Qxk5qUJaPZ/xupz0NyoVpviuiBOI1gNi8ovSXWzAgEC +-----END DH PARAMETERS----- diff --git a/tests/suites/test_suite_dhm.data b/tests/suites/test_suite_dhm.data index aecbfc5c9..57db3dbc9 100644 --- a/tests/suites/test_suite_dhm.data +++ b/tests/suites/test_suite_dhm.data @@ -7,5 +7,8 @@ dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622" Diffie-Hellman full exchange #3 dhm_do_dhm:10:"93450983094850938450983409623982317398171298719873918739182739712938719287391879381271":10:"9345098309485093845098340962223981329819812792137312973297123912791271" +Diffie-Hallman load parameters from file +dhm_file:"data_files/dhparams.pem":"9e35f430443a09904f3a39a979797d070df53378e79c2438bef4e761f3c714553328589b041c809be1d6c6b5f1fc9f47d3a25443188253a992a56818b37ba9de5a40d362e56eff0be5417474c125c199272c8fe41dea733df6f662c92ae76556e755d10c64e6a50968f67fc6ea73d0dca8569be2ba204e23580d8bca2f4975b3":"02":128 + Diffie-Hellman selftest dhm_selftest: diff --git a/tests/suites/test_suite_dhm.function b/tests/suites/test_suite_dhm.function index d66998ffe..b0df9fdbb 100644 --- a/tests/suites/test_suite_dhm.function +++ b/tests/suites/test_suite_dhm.function @@ -97,6 +97,29 @@ void dhm_do_dhm( int radix_P, char *input_P, } /* END_CASE */ +/* BEGIN_CASE depends_on:POLARSSL_FS_IO */ +void dhm_file( char *filename, char *p, char *g, int len ) +{ + dhm_context ctx; + mpi P, G; + + memset( &ctx, 0, sizeof ctx ); + mpi_init( &P ); mpi_init( &G ); + + TEST_ASSERT( mpi_read_string( &P, 16, p ) == 0 ); + TEST_ASSERT( mpi_read_string( &G, 16, g ) == 0 ); + + TEST_ASSERT( dhm_parse_dhmfile( &ctx, filename ) == 0 ); + + TEST_ASSERT( ctx.len == (size_t) len ); + TEST_ASSERT( mpi_cmp_mpi( &ctx.P, &P ) == 0 ); + TEST_ASSERT( mpi_cmp_mpi( &ctx.G, &G ) == 0 ); + + mpi_free( &P ); mpi_free( &G ); + dhm_free( &ctx ); +} +/* END_CASE */ + /* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */ void dhm_selftest() {