From 13724765b27b0456a144df9c3f2bd6a30c4cb35e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Sun, 10 Feb 2013 15:01:54 +0100 Subject: [PATCH] Add ecdh_make_server_params (untested yet) --- include/polarssl/ecdh.h | 30 +++++++++++++++++++++++++----- library/ecdh.c | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/include/polarssl/ecdh.h b/include/polarssl/ecdh.h index 2f1e51dd4..992850333 100644 --- a/include/polarssl/ecdh.h +++ b/include/polarssl/ecdh.h @@ -34,11 +34,12 @@ */ typedef struct { - ecp_group grp; /*!< ellipitic curve used */ - mpi d; /*!< our secret value */ - ecp_point Q; /*!< our public value */ - ecp_point Qp; /*!< peer's public value */ - mpi z; /*!< shared secret */ + ecp_group grp; /*!< ellipitic curve used */ + mpi d; /*!< our secret value */ + ecp_point Q; /*!< our public value */ + ecp_point Qp; /*!< peer's public value */ + mpi z; /*!< shared secret */ + int point_format; /*!< format for point export */ } ecdh_context; @@ -90,6 +91,25 @@ void ecdh_init( ecdh_context *ctx ); */ void ecdh_free( ecdh_context *ctx ); +/** + * \brief Setup and write the ServerKeyExhange parameters + * + * \param ctx ECDH context + * \param buf destination buffer + * \param olen number of chars written + * \param f_rng RNG function + * \param p_rng RNG parameter + * + * \note This function assumes that ctx->grp has already been + * properly set (for example using ecp_use_known_dp). + * + * \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 ); + /** * \brief Checkup routine * diff --git a/library/ecdh.c b/library/ecdh.c index 0e2cfdf5b..32f044a54 100644 --- a/library/ecdh.c +++ b/library/ecdh.c @@ -85,9 +85,9 @@ void ecdh_init( ecdh_context *ctx ) ecp_point_init( &ctx->Q ); ecp_point_init( &ctx->Qp ); mpi_init ( &ctx->z ); + ctx->point_format = POLARSSL_ECP_PF_UNCOMPRESSED; } - /* * Free context */ @@ -103,6 +103,40 @@ void ecdh_free( ecdh_context *ctx ) mpi_free ( &ctx->z ); } +/* + * Setup and write the ServerKeyExhange parameters + * 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 ret; + size_t grp_len, pt_len; + + if( ( ret = ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) ) + != 0 ) + return( ret ); + + if( ( ret = ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) ) + != 0 ) + return( ret ); + + buf += grp_len; + blen -= grp_len; + + if( ( ret = ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format, + &pt_len, buf, blen ) ) != 0 ) + return( ret ); + + *olen = grp_len + pt_len; + return 0; +} + #if defined(POLARSSL_SELF_TEST)