Lines Matching refs:ctx
30 const mbedtls_ecdh_context *ctx) in mbedtls_ecdh_grp_id() argument
33 return ctx->grp.id; in mbedtls_ecdh_grp_id()
35 return ctx->grp_id; in mbedtls_ecdh_grp_id()
134 static void ecdh_init_internal(mbedtls_ecdh_context_mbed *ctx) in ecdh_init_internal() argument
136 mbedtls_ecp_group_init(&ctx->grp); in ecdh_init_internal()
137 mbedtls_mpi_init(&ctx->d); in ecdh_init_internal()
138 mbedtls_ecp_point_init(&ctx->Q); in ecdh_init_internal()
139 mbedtls_ecp_point_init(&ctx->Qp); in ecdh_init_internal()
140 mbedtls_mpi_init(&ctx->z); in ecdh_init_internal()
143 mbedtls_ecp_restart_init(&ctx->rs); in ecdh_init_internal()
147 mbedtls_ecp_group_id mbedtls_ecdh_get_grp_id(mbedtls_ecdh_context *ctx) in mbedtls_ecdh_get_grp_id() argument
150 return ctx->MBEDTLS_PRIVATE(grp).id; in mbedtls_ecdh_get_grp_id()
152 return ctx->MBEDTLS_PRIVATE(grp_id); in mbedtls_ecdh_get_grp_id()
159 void mbedtls_ecdh_init(mbedtls_ecdh_context *ctx) in mbedtls_ecdh_init() argument
162 ecdh_init_internal(ctx); in mbedtls_ecdh_init()
163 mbedtls_ecp_point_init(&ctx->Vi); in mbedtls_ecdh_init()
164 mbedtls_ecp_point_init(&ctx->Vf); in mbedtls_ecdh_init()
165 mbedtls_mpi_init(&ctx->_d); in mbedtls_ecdh_init()
167 memset(ctx, 0, sizeof(mbedtls_ecdh_context)); in mbedtls_ecdh_init()
169 ctx->var = MBEDTLS_ECDH_VARIANT_NONE; in mbedtls_ecdh_init()
171 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED; in mbedtls_ecdh_init()
173 ctx->restart_enabled = 0; in mbedtls_ecdh_init()
177 static int ecdh_setup_internal(mbedtls_ecdh_context_mbed *ctx, in ecdh_setup_internal() argument
182 ret = mbedtls_ecp_group_load(&ctx->grp, grp_id); in ecdh_setup_internal()
193 int mbedtls_ecdh_setup(mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id) in mbedtls_ecdh_setup() argument
196 return ecdh_setup_internal(ctx, grp_id); in mbedtls_ecdh_setup()
201 ctx->point_format = MBEDTLS_ECP_PF_COMPRESSED; in mbedtls_ecdh_setup()
202 ctx->var = MBEDTLS_ECDH_VARIANT_EVEREST; in mbedtls_ecdh_setup()
203 ctx->grp_id = grp_id; in mbedtls_ecdh_setup()
204 return mbedtls_everest_setup(&ctx->ctx.everest_ecdh, grp_id); in mbedtls_ecdh_setup()
207 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED; in mbedtls_ecdh_setup()
208 ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0; in mbedtls_ecdh_setup()
209 ctx->grp_id = grp_id; in mbedtls_ecdh_setup()
210 ecdh_init_internal(&ctx->ctx.mbed_ecdh); in mbedtls_ecdh_setup()
211 return ecdh_setup_internal(&ctx->ctx.mbed_ecdh, grp_id); in mbedtls_ecdh_setup()
216 static void ecdh_free_internal(mbedtls_ecdh_context_mbed *ctx) in ecdh_free_internal() argument
218 mbedtls_ecp_group_free(&ctx->grp); in ecdh_free_internal()
219 mbedtls_mpi_free(&ctx->d); in ecdh_free_internal()
220 mbedtls_ecp_point_free(&ctx->Q); in ecdh_free_internal()
221 mbedtls_ecp_point_free(&ctx->Qp); in ecdh_free_internal()
222 mbedtls_mpi_free(&ctx->z); in ecdh_free_internal()
225 mbedtls_ecp_restart_free(&ctx->rs); in ecdh_free_internal()
233 void mbedtls_ecdh_enable_restart(mbedtls_ecdh_context *ctx) in mbedtls_ecdh_enable_restart() argument
235 ctx->restart_enabled = 1; in mbedtls_ecdh_enable_restart()
242 void mbedtls_ecdh_free(mbedtls_ecdh_context *ctx) in mbedtls_ecdh_free() argument
244 if (ctx == NULL) { in mbedtls_ecdh_free()
249 mbedtls_ecp_point_free(&ctx->Vi); in mbedtls_ecdh_free()
250 mbedtls_ecp_point_free(&ctx->Vf); in mbedtls_ecdh_free()
251 mbedtls_mpi_free(&ctx->_d); in mbedtls_ecdh_free()
252 ecdh_free_internal(ctx); in mbedtls_ecdh_free()
254 switch (ctx->var) { in mbedtls_ecdh_free()
257 mbedtls_everest_free(&ctx->ctx.everest_ecdh); in mbedtls_ecdh_free()
261 ecdh_free_internal(&ctx->ctx.mbed_ecdh); in mbedtls_ecdh_free()
267 ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED; in mbedtls_ecdh_free()
268 ctx->var = MBEDTLS_ECDH_VARIANT_NONE; in mbedtls_ecdh_free()
269 ctx->grp_id = MBEDTLS_ECP_DP_NONE; in mbedtls_ecdh_free()
273 static int ecdh_make_params_internal(mbedtls_ecdh_context_mbed *ctx, in ecdh_make_params_internal() argument
288 if (ctx->grp.pbits == 0) { in ecdh_make_params_internal()
294 rs_ctx = &ctx->rs; in ecdh_make_params_internal()
302 if ((ret = ecdh_gen_public_restartable(&ctx->grp, &ctx->d, &ctx->Q, in ecdh_make_params_internal()
307 if ((ret = mbedtls_ecdh_gen_public(&ctx->grp, &ctx->d, &ctx->Q, in ecdh_make_params_internal()
313 if ((ret = mbedtls_ecp_tls_write_group(&ctx->grp, &grp_len, buf, in ecdh_make_params_internal()
321 if ((ret = mbedtls_ecp_tls_write_point(&ctx->grp, &ctx->Q, point_format, in ecdh_make_params_internal()
337 int mbedtls_ecdh_make_params(mbedtls_ecdh_context *ctx, size_t *olen, in mbedtls_ecdh_make_params() argument
344 restart_enabled = ctx->restart_enabled; in mbedtls_ecdh_make_params()
350 return ecdh_make_params_internal(ctx, olen, ctx->point_format, buf, blen, in mbedtls_ecdh_make_params()
353 switch (ctx->var) { in mbedtls_ecdh_make_params()
356 return mbedtls_everest_make_params(&ctx->ctx.everest_ecdh, olen, in mbedtls_ecdh_make_params()
360 return ecdh_make_params_internal(&ctx->ctx.mbed_ecdh, olen, in mbedtls_ecdh_make_params()
361 ctx->point_format, buf, blen, in mbedtls_ecdh_make_params()
370 static int ecdh_read_params_internal(mbedtls_ecdh_context_mbed *ctx, in ecdh_read_params_internal() argument
374 return mbedtls_ecp_tls_read_point(&ctx->grp, &ctx->Qp, buf, in ecdh_read_params_internal()
385 int mbedtls_ecdh_read_params(mbedtls_ecdh_context *ctx, in mbedtls_ecdh_read_params() argument
396 if ((ret = mbedtls_ecdh_setup(ctx, grp_id)) != 0) { in mbedtls_ecdh_read_params()
401 return ecdh_read_params_internal(ctx, buf, end); in mbedtls_ecdh_read_params()
403 switch (ctx->var) { in mbedtls_ecdh_read_params()
406 return mbedtls_everest_read_params(&ctx->ctx.everest_ecdh, in mbedtls_ecdh_read_params()
410 return ecdh_read_params_internal(&ctx->ctx.mbed_ecdh, in mbedtls_ecdh_read_params()
418 static int ecdh_get_params_internal(mbedtls_ecdh_context_mbed *ctx, in ecdh_get_params_internal() argument
426 return mbedtls_ecp_copy(&ctx->Qp, &key->Q); in ecdh_get_params_internal()
434 if ((ret = mbedtls_ecp_copy(&ctx->Q, &key->Q)) != 0 || in ecdh_get_params_internal()
435 (ret = mbedtls_mpi_copy(&ctx->d, &key->d)) != 0) { in ecdh_get_params_internal()
445 int mbedtls_ecdh_get_params(mbedtls_ecdh_context *ctx, in mbedtls_ecdh_get_params() argument
454 if (mbedtls_ecdh_grp_id(ctx) == MBEDTLS_ECP_DP_NONE) { in mbedtls_ecdh_get_params()
457 if ((ret = mbedtls_ecdh_setup(ctx, key->grp.id)) != 0) { in mbedtls_ecdh_get_params()
464 if (mbedtls_ecdh_grp_id(ctx) != key->grp.id) { in mbedtls_ecdh_get_params()
470 return ecdh_get_params_internal(ctx, key, side); in mbedtls_ecdh_get_params()
472 switch (ctx->var) { in mbedtls_ecdh_get_params()
479 return mbedtls_everest_get_params(&ctx->ctx.everest_ecdh, in mbedtls_ecdh_get_params()
484 return ecdh_get_params_internal(&ctx->ctx.mbed_ecdh, in mbedtls_ecdh_get_params()
492 static int ecdh_make_public_internal(mbedtls_ecdh_context_mbed *ctx, in ecdh_make_public_internal() argument
506 if (ctx->grp.pbits == 0) { in ecdh_make_public_internal()
512 rs_ctx = &ctx->rs; in ecdh_make_public_internal()
519 if ((ret = ecdh_gen_public_restartable(&ctx->grp, &ctx->d, &ctx->Q, in ecdh_make_public_internal()
524 if ((ret = mbedtls_ecdh_gen_public(&ctx->grp, &ctx->d, &ctx->Q, in ecdh_make_public_internal()
530 return mbedtls_ecp_tls_write_point(&ctx->grp, &ctx->Q, point_format, olen, in ecdh_make_public_internal()
537 int mbedtls_ecdh_make_public(mbedtls_ecdh_context *ctx, size_t *olen, in mbedtls_ecdh_make_public() argument
544 restart_enabled = ctx->restart_enabled; in mbedtls_ecdh_make_public()
548 return ecdh_make_public_internal(ctx, olen, ctx->point_format, buf, blen, in mbedtls_ecdh_make_public()
551 switch (ctx->var) { in mbedtls_ecdh_make_public()
554 return mbedtls_everest_make_public(&ctx->ctx.everest_ecdh, olen, in mbedtls_ecdh_make_public()
558 return ecdh_make_public_internal(&ctx->ctx.mbed_ecdh, olen, in mbedtls_ecdh_make_public()
559 ctx->point_format, buf, blen, in mbedtls_ecdh_make_public()
568 static int ecdh_read_public_internal(mbedtls_ecdh_context_mbed *ctx, in ecdh_read_public_internal() argument
574 if ((ret = mbedtls_ecp_tls_read_point(&ctx->grp, &ctx->Qp, &p, in ecdh_read_public_internal()
589 int mbedtls_ecdh_read_public(mbedtls_ecdh_context *ctx, in mbedtls_ecdh_read_public() argument
593 return ecdh_read_public_internal(ctx, buf, blen); in mbedtls_ecdh_read_public()
595 switch (ctx->var) { in mbedtls_ecdh_read_public()
598 return mbedtls_everest_read_public(&ctx->ctx.everest_ecdh, in mbedtls_ecdh_read_public()
602 return ecdh_read_public_internal(&ctx->ctx.mbed_ecdh, in mbedtls_ecdh_read_public()
610 static int ecdh_calc_secret_internal(mbedtls_ecdh_context_mbed *ctx, in ecdh_calc_secret_internal() argument
624 if (ctx == NULL || ctx->grp.pbits == 0) { in ecdh_calc_secret_internal()
630 rs_ctx = &ctx->rs; in ecdh_calc_secret_internal()
637 if ((ret = ecdh_compute_shared_restartable(&ctx->grp, &ctx->z, &ctx->Qp, in ecdh_calc_secret_internal()
638 &ctx->d, f_rng, p_rng, in ecdh_calc_secret_internal()
643 if ((ret = mbedtls_ecdh_compute_shared(&ctx->grp, &ctx->z, &ctx->Qp, in ecdh_calc_secret_internal()
644 &ctx->d, f_rng, p_rng)) != 0) { in ecdh_calc_secret_internal()
649 if (mbedtls_mpi_size(&ctx->z) > blen) { in ecdh_calc_secret_internal()
653 *olen = ctx->grp.pbits / 8 + ((ctx->grp.pbits % 8) != 0); in ecdh_calc_secret_internal()
655 if (mbedtls_ecp_get_type(&ctx->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { in ecdh_calc_secret_internal()
656 return mbedtls_mpi_write_binary_le(&ctx->z, buf, *olen); in ecdh_calc_secret_internal()
659 return mbedtls_mpi_write_binary(&ctx->z, buf, *olen); in ecdh_calc_secret_internal()
665 int mbedtls_ecdh_calc_secret(mbedtls_ecdh_context *ctx, size_t *olen, in mbedtls_ecdh_calc_secret() argument
672 restart_enabled = ctx->restart_enabled; in mbedtls_ecdh_calc_secret()
676 return ecdh_calc_secret_internal(ctx, olen, buf, blen, f_rng, p_rng, in mbedtls_ecdh_calc_secret()
679 switch (ctx->var) { in mbedtls_ecdh_calc_secret()
682 return mbedtls_everest_calc_secret(&ctx->ctx.everest_ecdh, olen, in mbedtls_ecdh_calc_secret()
686 return ecdh_calc_secret_internal(&ctx->ctx.mbed_ecdh, olen, buf, in mbedtls_ecdh_calc_secret()