54#if !defined(MBEDTLS_CONFIG_FILE)
57#include MBEDTLS_CONFIG_FILE
60#if defined(MBEDTLS_ECDH_C)
68#define ECDH_VALIDATE_RET( cond ) \
69 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA )
70#define ECDH_VALIDATE( cond ) \
71 MBEDTLS_INTERNAL_VALIDATE( cond )
73#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
80#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
81 return(
ctx->grp.id );
83 return(
ctx->grp_id );
87#if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT)
97 int (*f_rng)(
void *,
unsigned char *,
size_t),
104#if defined(MBEDTLS_ECP_RESTARTABLE)
105 if( rs_ctx ==
NULL || rs_ctx->rsm ==
NULL )
110 f_rng, p_rng, rs_ctx ) );
120 int (*f_rng)(
void *,
unsigned char *,
size_t),
123 ECDH_VALIDATE_RET( grp !=
NULL );
124 ECDH_VALIDATE_RET(
d !=
NULL );
125 ECDH_VALIDATE_RET( Q !=
NULL );
126 ECDH_VALIDATE_RET( f_rng !=
NULL );
127 return( ecdh_gen_public_restartable( grp,
d, Q, f_rng, p_rng,
NULL ) );
131#if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT)
138 int (*f_rng)(
void *,
unsigned char *,
size_t),
148 f_rng, p_rng, rs_ctx ) );
169 int (*f_rng)(
void *,
unsigned char *,
size_t),
172 ECDH_VALIDATE_RET( grp !=
NULL );
173 ECDH_VALIDATE_RET( Q !=
NULL );
174 ECDH_VALIDATE_RET(
d !=
NULL );
175 ECDH_VALIDATE_RET(
z !=
NULL );
176 return( ecdh_compute_shared_restartable( grp,
z, Q,
d,
177 f_rng, p_rng,
NULL ) );
181static void ecdh_init_internal( mbedtls_ecdh_context_mbed *
ctx )
189#if defined(MBEDTLS_ECP_RESTARTABLE)
190 mbedtls_ecp_restart_init( &
ctx->rs );
201#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
202 ecdh_init_internal(
ctx );
209 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
212#if defined(MBEDTLS_ECP_RESTARTABLE)
213 ctx->restart_enabled = 0;
217static int ecdh_setup_internal( mbedtls_ecdh_context_mbed *
ctx,
236 ECDH_VALIDATE_RET(
ctx !=
NULL );
238#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
239 return( ecdh_setup_internal(
ctx, grp_id ) );
245 ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0;
246 ctx->grp_id = grp_id;
247 ecdh_init_internal( &
ctx->
ctx.mbed_ecdh );
248 return( ecdh_setup_internal( &
ctx->
ctx.mbed_ecdh, grp_id ) );
253static void ecdh_free_internal( mbedtls_ecdh_context_mbed *
ctx )
261#if defined(MBEDTLS_ECP_RESTARTABLE)
262 mbedtls_ecp_restart_free( &
ctx->rs );
266#if defined(MBEDTLS_ECP_RESTARTABLE)
274 ctx->restart_enabled = 1;
286#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
290 ecdh_free_internal(
ctx );
294 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
295 ecdh_free_internal( &
ctx->
ctx.mbed_ecdh );
302 ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
307static int ecdh_make_params_internal( mbedtls_ecdh_context_mbed *
ctx,
308 size_t *olen,
int point_format,
309 unsigned char *
buf,
size_t blen,
314 int restart_enabled )
317 size_t grp_len, pt_len;
318#if defined(MBEDTLS_ECP_RESTARTABLE)
322 if(
ctx->grp.pbits == 0 )
325#if defined(MBEDTLS_ECP_RESTARTABLE)
326 if( restart_enabled )
329 (
void) restart_enabled;
333#if defined(MBEDTLS_ECP_RESTARTABLE)
334 if( (
ret = ecdh_gen_public_restartable( &
ctx->grp, &
ctx->d, &
ctx->Q,
335 f_rng, p_rng, rs_ctx ) ) != 0 )
339 f_rng, p_rng ) ) != 0 )
351 &pt_len,
buf, blen ) ) != 0 )
354 *olen = grp_len + pt_len;
366 unsigned char *
buf,
size_t blen,
367 int (*f_rng)(
void *,
unsigned char *,
size_t),
370 int restart_enabled = 0;
371 ECDH_VALIDATE_RET(
ctx !=
NULL );
372 ECDH_VALIDATE_RET( olen !=
NULL );
373 ECDH_VALIDATE_RET(
buf !=
NULL );
374 ECDH_VALIDATE_RET( f_rng !=
NULL );
376#if defined(MBEDTLS_ECP_RESTARTABLE)
377 restart_enabled =
ctx->restart_enabled;
379 (
void) restart_enabled;
382#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
383 return( ecdh_make_params_internal(
ctx, olen,
ctx->point_format,
buf, blen,
384 f_rng, p_rng, restart_enabled ) );
388 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
389 return( ecdh_make_params_internal( &
ctx->
ctx.mbed_ecdh, olen,
390 ctx->point_format,
buf, blen,
399static int ecdh_read_params_internal( mbedtls_ecdh_context_mbed *
ctx,
400 const unsigned char **
buf,
401 const unsigned char *
end )
415 const unsigned char **
buf,
416 const unsigned char *
end )
420 ECDH_VALIDATE_RET(
ctx !=
NULL );
421 ECDH_VALIDATE_RET(
buf !=
NULL );
422 ECDH_VALIDATE_RET( *
buf !=
NULL );
423 ECDH_VALIDATE_RET(
end !=
NULL );
432#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
433 return( ecdh_read_params_internal(
ctx,
buf,
end ) );
437 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
438 return( ecdh_read_params_internal( &
ctx->
ctx.mbed_ecdh,
446static int ecdh_get_params_internal( mbedtls_ecdh_context_mbed *
ctx,
475 ECDH_VALIDATE_RET(
ctx !=
NULL );
476 ECDH_VALIDATE_RET(
key !=
NULL );
492 if( mbedtls_ecdh_grp_id(
ctx ) !=
key->grp.id )
496#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
497 return( ecdh_get_params_internal(
ctx,
key, side ) );
501 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
502 return( ecdh_get_params_internal( &
ctx->
ctx.mbed_ecdh,
510static int ecdh_make_public_internal( mbedtls_ecdh_context_mbed *
ctx,
511 size_t *olen,
int point_format,
512 unsigned char *
buf,
size_t blen,
517 int restart_enabled )
520#if defined(MBEDTLS_ECP_RESTARTABLE)
524 if(
ctx->grp.pbits == 0 )
527#if defined(MBEDTLS_ECP_RESTARTABLE)
528 if( restart_enabled )
531 (
void) restart_enabled;
534#if defined(MBEDTLS_ECP_RESTARTABLE)
535 if( (
ret = ecdh_gen_public_restartable( &
ctx->grp, &
ctx->d, &
ctx->Q,
536 f_rng, p_rng, rs_ctx ) ) != 0 )
540 f_rng, p_rng ) ) != 0 )
552 unsigned char *
buf,
size_t blen,
553 int (*f_rng)(
void *,
unsigned char *,
size_t),
556 int restart_enabled = 0;
557 ECDH_VALIDATE_RET(
ctx !=
NULL );
558 ECDH_VALIDATE_RET( olen !=
NULL );
559 ECDH_VALIDATE_RET(
buf !=
NULL );
560 ECDH_VALIDATE_RET( f_rng !=
NULL );
562#if defined(MBEDTLS_ECP_RESTARTABLE)
563 restart_enabled =
ctx->restart_enabled;
566#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
567 return( ecdh_make_public_internal(
ctx, olen,
ctx->point_format,
buf, blen,
568 f_rng, p_rng, restart_enabled ) );
572 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
573 return( ecdh_make_public_internal( &
ctx->
ctx.mbed_ecdh, olen,
574 ctx->point_format,
buf, blen,
583static int ecdh_read_public_internal( mbedtls_ecdh_context_mbed *
ctx,
584 const unsigned char *
buf,
size_t blen )
587 const unsigned char *
p =
buf;
593 if( (
size_t)(
p -
buf ) != blen )
603 const unsigned char *
buf,
size_t blen )
605 ECDH_VALIDATE_RET(
ctx !=
NULL );
606 ECDH_VALIDATE_RET(
buf !=
NULL );
608#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
609 return( ecdh_read_public_internal(
ctx,
buf, blen ) );
613 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
614 return( ecdh_read_public_internal( &
ctx->
ctx.mbed_ecdh,
622static int ecdh_calc_secret_internal( mbedtls_ecdh_context_mbed *
ctx,
623 size_t *olen,
unsigned char *
buf,
629 int restart_enabled )
632#if defined(MBEDTLS_ECP_RESTARTABLE)
639#if defined(MBEDTLS_ECP_RESTARTABLE)
640 if( restart_enabled )
643 (
void) restart_enabled;
646#if defined(MBEDTLS_ECP_RESTARTABLE)
647 if( (
ret = ecdh_compute_shared_restartable( &
ctx->grp, &
ctx->z, &
ctx->Qp,
648 &
ctx->d, f_rng, p_rng,
655 &
ctx->d, f_rng, p_rng ) ) != 0 )
664 *olen =
ctx->grp.pbits / 8 + ( (
ctx->grp.pbits % 8 ) != 0 );
672 unsigned char *
buf,
size_t blen,
673 int (*f_rng)(
void *,
unsigned char *,
size_t),
676 int restart_enabled = 0;
677 ECDH_VALIDATE_RET(
ctx !=
NULL );
678 ECDH_VALIDATE_RET( olen !=
NULL );
679 ECDH_VALIDATE_RET(
buf !=
NULL );
681#if defined(MBEDTLS_ECP_RESTARTABLE)
682 restart_enabled =
ctx->restart_enabled;
685#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
686 return( ecdh_calc_secret_internal(
ctx, olen,
buf, blen, f_rng, p_rng,
691 case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
692 return( ecdh_calc_secret_internal( &
ctx->
ctx.mbed_ecdh, olen,
buf,
int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
Make a copy of an MPI.
size_t mbedtls_mpi_size(const mbedtls_mpi *X)
Return the total size of an MPI value in bytes.
void mbedtls_mpi_init(mbedtls_mpi *X)
Initialize an MPI context.
#define MBEDTLS_MPI_CHK(f)
void mbedtls_mpi_free(mbedtls_mpi *X)
This function frees the components of an MPI context.
int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf, size_t buflen)
Export an MPI into unsigned big endian binary data of fixed size.
static void cleanup(void)
This file contains ECDH definitions and functions.
int mbedtls_ecdh_setup(mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id)
This function sets up the ECDH context with the information given.
int mbedtls_ecdh_get_params(mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key, mbedtls_ecdh_side side)
This function sets up an ECDH context from an EC key.
int mbedtls_ecdh_gen_public(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function generates an ECDH keypair on an elliptic curve.
int mbedtls_ecdh_make_public(mbedtls_ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function generates a public key and exports it as a TLS ClientKeyExchange payload.
int mbedtls_ecdh_calc_secret(mbedtls_ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function derives and exports the shared secret.
int mbedtls_ecdh_make_params(mbedtls_ecdh_context *ctx, size_t *olen, unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function generates an EC key pair and exports its in the format used in a TLS ServerKeyExchange ...
void mbedtls_ecdh_free(mbedtls_ecdh_context *ctx)
This function frees a context.
void mbedtls_ecdh_init(mbedtls_ecdh_context *ctx)
This function initializes an ECDH context.
int mbedtls_ecdh_read_public(mbedtls_ecdh_context *ctx, const unsigned char *buf, size_t blen)
This function parses and processes the ECDHE payload of a TLS ClientKeyExchange message.
int mbedtls_ecdh_compute_shared(mbedtls_ecp_group *grp, mbedtls_mpi *z, const mbedtls_ecp_point *Q, const mbedtls_mpi *d, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function computes the shared secret.
int mbedtls_ecdh_read_params(mbedtls_ecdh_context *ctx, const unsigned char **buf, const unsigned char *end)
This function parses the ECDHE parameters in a TLS ServerKeyExchange handshake message.
GLenum GLuint GLenum GLsizei const GLchar * buf
Configuration options (set of defines)
#define MBEDTLS_ECP_PF_UNCOMPRESSED
int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt)
This function checks if a point is the point at infinity.
int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp, size_t *olen, unsigned char *buf, size_t blen)
This function exports an elliptic curve as a TLS ECParameters record as defined in RFC 4492,...
void mbedtls_ecp_group_init(mbedtls_ecp_group *grp)
This function initializes an ECP group context without loading any domain parameters.
int mbedtls_ecp_mul_restartable(mbedtls_ecp_group *grp, mbedtls_ecp_point *R, const mbedtls_mpi *m, const mbedtls_ecp_point *P, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, mbedtls_ecp_restart_ctx *rs_ctx)
This function performs multiplication of a point by an integer: R = m * P in a restartable way.
int mbedtls_ecp_group_load(mbedtls_ecp_group *grp, mbedtls_ecp_group_id id)
This function sets up an ECP group context from a standardized set of domain parameters.
int mbedtls_ecp_tls_read_group_id(mbedtls_ecp_group_id *grp, const unsigned char **buf, size_t len)
This function extracts an elliptic curve group ID from a TLS ECParameters record as defined in RFC 44...
void mbedtls_ecp_point_free(mbedtls_ecp_point *pt)
This function frees the components of a point.
int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt, const unsigned char **buf, size_t len)
This function imports a point from a TLS ECPoint record.
void mbedtls_ecp_group_free(mbedtls_ecp_group *grp)
This function frees the components of an ECP group.
int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
This function copies the contents of point Q into point P.
int mbedtls_ecp_gen_privkey(const mbedtls_ecp_group *grp, mbedtls_mpi *d, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function generates a private key.
#define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE
int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt, int format, size_t *olen, unsigned char *buf, size_t blen)
This function exports a point as a TLS ECPoint record defined in RFC 4492, Section 5....
#define MBEDTLS_ERR_ECP_BAD_INPUT_DATA
void mbedtls_ecp_point_init(mbedtls_ecp_point *pt)
This function initializes a point as zero.
The ECDH context structure.
The ECP key-pair structure.
The ECP point structure, in Jacobian coordinates.