diff --git a/include/mbedtls/ecjpake.h b/include/mbedtls/ecjpake.h index 57f4b15e4..0b20ff6dd 100644 --- a/include/mbedtls/ecjpake.h +++ b/include/mbedtls/ecjpake.h @@ -30,6 +30,52 @@ extern "C" { #endif +typedef struct +{ + const mbedtls_md_info_t *md_info; /**< Hash to use */ + mbedtls_ecp_group grp; /**< Elliptic curve */ + + mbedtls_ecp_point X1; /**< Public key one */ + mbedtls_ecp_point X2; /**< Public key two */ + mbedtls_ecp_point X3; /**< Public key three */ + mbedtls_ecp_point X4; /**< Public key four */ + + mbedtls_mpi xa; /**< Our first secret (x1 or x3) */ + mbedtls_mpi xb; /**< Our second secret (x2 or x4) */ +} mbedtls_ecjpake_context; + +/* + * \brief Initialize a context + * (just makes it ready for setup() or free()). + * + * \param ctx context to initialize + */ +void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx ); + +/* + * \brief Free a context's content + * + * \param ctx context to free + */ +void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx ); + +/* + * \brief Set up a context for use + * + * \note Currently the only values for hash/curve allowed by the + * standard are MBEDTLS_MD_SHA256/MBEDTLS_ECP_DP_SECP256R1. + * + * \param ctx context to set up + * \param hash hash function to use (MBEDTLS_MD_XXX) + * \param curve elliptic curve identifier (MBEDTLS_ECP_DP_XXX) + * + * \return 0 if successfull, + * a negative error code otherwise + */ +int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx, + mbedtls_md_type_t hash, + mbedtls_ecp_group_id curve ); + #if defined(MBEDTLS_SELF_TEST) /** * \brief Checkup routine diff --git a/library/ecjpake.c b/library/ecjpake.c index a61c146eb..678112c57 100644 --- a/library/ecjpake.c +++ b/library/ecjpake.c @@ -36,6 +36,64 @@ #include +/* + * Initialize context + */ +void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx ) +{ + if( ctx == NULL ) + return; + + ctx->md_info = NULL; + mbedtls_ecp_group_init( &ctx->grp ); + + mbedtls_ecp_point_init( &ctx->X1 ); + mbedtls_ecp_point_init( &ctx->X2 ); + mbedtls_ecp_point_init( &ctx->X3 ); + mbedtls_ecp_point_init( &ctx->X4 ); + + mbedtls_mpi_init( &ctx->xa ); + mbedtls_mpi_init( &ctx->xb ); +} + +/* + * Free context + */ +void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx ) +{ + if( ctx == NULL ) + return; + + ctx->md_info = NULL; + mbedtls_ecp_group_free( &ctx->grp ); + + mbedtls_ecp_point_free( &ctx->X1 ); + mbedtls_ecp_point_free( &ctx->X2 ); + mbedtls_ecp_point_free( &ctx->X3 ); + mbedtls_ecp_point_free( &ctx->X4 ); + + mbedtls_mpi_free( &ctx->xa ); + mbedtls_mpi_free( &ctx->xb ); +} + +/* + * Setup context + */ +int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx, + mbedtls_md_type_t hash, + mbedtls_ecp_group_id curve ) +{ + int ret; + + if( ( ctx->md_info = mbedtls_md_info_from_type( hash ) ) == NULL ) + return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); + + if( ( ret = mbedtls_ecp_group_load( &ctx->grp, curve ) ) != 0 ) + return( ret ); + + return( 0 ); +} + /* * Write a point plus its length to a buffer */