From 854fbd7ba27eb89b39646e60e1aed8474474e340 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Mon, 11 Feb 2013 20:28:55 +0100 Subject: [PATCH] Add ecdh_read_params(). --- include/polarssl/ecdh.h | 20 +++++++++++++---- library/ecdh.c | 31 ++++++++++++++++++++++----- tests/suites/test_suite_ecdh.data | 6 ++++++ tests/suites/test_suite_ecdh.function | 25 +++++++++++++++++++++ 4 files changed, 73 insertions(+), 9 deletions(-) diff --git a/include/polarssl/ecdh.h b/include/polarssl/ecdh.h index 992850333..6b8a905aa 100644 --- a/include/polarssl/ecdh.h +++ b/include/polarssl/ecdh.h @@ -105,10 +105,22 @@ void ecdh_free( ecdh_context *ctx ); * * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code */ -int ecdh_make_server_params( ecdh_context *ctx, size_t *olen, - unsigned char *buf, size_t blen, - int (*f_rng)(void *, unsigned char *, size_t), - void *p_rng ); +int ecdh_make_params( ecdh_context *ctx, size_t *olen, + unsigned char *buf, size_t blen, + int (*f_rng)(void *, unsigned char *, size_t), + void *p_rng ); + +/** + * \brief Parse the ServerKeyExhange parameters + * + * \param ctx ECDH context + * \param buf $(start of input buffer) + * \param end one past end of buffer + * + * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code + */ +int ecdh_read_params( ecdh_context *ctx, + const unsigned char **buf, const unsigned char *end ); /** * \brief Checkup routine diff --git a/library/ecdh.c b/library/ecdh.c index 32f044a54..462e0155c 100644 --- a/library/ecdh.c +++ b/library/ecdh.c @@ -104,16 +104,16 @@ void ecdh_free( ecdh_context *ctx ) } /* - * Setup and write the ServerKeyExhange parameters + * Setup and write the ServerKeyExhange parameters (RFC 4492) * struct { * ECParameters curve_params; * ECPoint public; * } ServerECDHParams; */ -int ecdh_make_server_params( ecdh_context *ctx, size_t *olen, - unsigned char *buf, size_t blen, - int (*f_rng)(void *, unsigned char *, size_t), - void *p_rng ) +int ecdh_make_params( ecdh_context *ctx, size_t *olen, + unsigned char *buf, size_t blen, + int (*f_rng)(void *, unsigned char *, size_t), + void *p_rng ) { int ret; size_t grp_len, pt_len; @@ -137,6 +137,27 @@ int ecdh_make_server_params( ecdh_context *ctx, size_t *olen, return 0; } +/* + * Read the ServerKeyExhange parameters (RFC 4492) + * struct { + * ECParameters curve_params; + * ECPoint public; + * } ServerECDHParams; + */ +int ecdh_read_params( ecdh_context *ctx, + const unsigned char **buf, const unsigned char *end ) +{ + int ret; + + if( ( ret = ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 ) + return( ret ); + + if( ( ret = ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) ) + != 0 ) + return( ret ); + + return 0; +} #if defined(POLARSSL_SELF_TEST) diff --git a/tests/suites/test_suite_ecdh.data b/tests/suites/test_suite_ecdh.data index 0926ed55e..47b68fb59 100644 --- a/tests/suites/test_suite_ecdh.data +++ b/tests/suites/test_suite_ecdh.data @@ -21,3 +21,9 @@ ecdh_primitive_testvec:SECP384R1:099F3C7034D4A2C699884D73A375A67F7624EF7C6B3C0F1 ECDH primitive rfc 5903 p521 ecdh_primitive_testvec:SECP521R1:0037ADE9319A89F4DABDB3EF411AACCCA5123C61ACAB57B5393DCE47608172A095AA85A30FE1C2952C6771D937BA9777F5957B2639BAB072462F68C27A57382D4A52:0015417E84DBF28C0AD3C278713349DC7DF153C897A1891BD98BAB4357C9ECBEE1E3BF42E00B8E380AEAE57C2D107564941885942AF5A7F4601723C4195D176CED3E:017CAE20B6641D2EEB695786D8C946146239D099E18E1D5A514C739D7CB4A10AD8A788015AC405D7799DC75E7B7D5B6CF2261A6A7F1507438BF01BEB6CA3926F9582:0145BA99A847AF43793FDD0E872E7CDFA16BE30FDC780F97BCCC3F078380201E9C677D600B343757A3BDBF2A3163E4C2F869CCA7458AA4A4EFFC311F5CB151685EB9:00D0B3975AC4B799F5BEA16D5E13E9AF971D5E9B984C9F39728B5E5739735A219B97C356436ADC6E95BB0352F6BE64A6C2912D4EF2D0433CED2B6171640012D9460F:015C68226383956E3BD066E797B623C27CE0EAC2F551A10C2C724D9852077B87220B6536C5C408A1D2AEBB8E86D678AE49CB57091F4732296579AB44FCD17F0FC56A:01144C7D79AE6956BC8EDB8E7C787C4521CB086FA64407F97894E5E6B2D79B04D1427E73CA4BAA240A34786859810C06B3C715A3A8CC3151F2BEE417996D19F3DDEA + +ECDH exchange #1 +ecdh_exchange:SECP192R1 + +ECDH exchange #2 +ecdh_exchange:SECP521R1 diff --git a/tests/suites/test_suite_ecdh.function b/tests/suites/test_suite_ecdh.function index e8177e7be..837983479 100644 --- a/tests/suites/test_suite_ecdh.function +++ b/tests/suites/test_suite_ecdh.function @@ -78,3 +78,28 @@ ecdh_primitive_testvec:id:dA:xA:yA:dB:xB:yB:z mpi_free( &zA ); mpi_free( &zB ); mpi_free( &check ); } END_CASE + +BEGIN_CASE +ecdh_exchange:id +{ + ecdh_context srv, cli; + unsigned char buf[1000]; + const unsigned char *vbuf; + size_t len; + rnd_pseudo_info rnd_info; + + ecdh_init( &srv ); + ecdh_init( &cli ); + memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) ); + + TEST_ASSERT( ecp_use_known_dp( &srv.grp, POLARSSL_ECP_DP_{id} ) == 0 ); + + memset( buf, 0x00, sizeof( buf ) ); vbuf = buf; + TEST_ASSERT( ecdh_make_params( &srv, &len, buf, 1000, + &rnd_pseudo_rand, &rnd_info ) == 0 ); + TEST_ASSERT( ecdh_read_params( &cli, &vbuf, buf + len ) == 0 ); + + ecdh_free( &srv ); + ecdh_free( &cli ); +} +END_CASE