Lines Matching refs:ctx
98 void mbedtls_dhm_init(mbedtls_dhm_context *ctx) in mbedtls_dhm_init() argument
100 memset(ctx, 0, sizeof(mbedtls_dhm_context)); in mbedtls_dhm_init()
103 size_t mbedtls_dhm_get_bitlen(const mbedtls_dhm_context *ctx) in mbedtls_dhm_get_bitlen() argument
105 return mbedtls_mpi_bitlen(&ctx->P); in mbedtls_dhm_get_bitlen()
108 size_t mbedtls_dhm_get_len(const mbedtls_dhm_context *ctx) in mbedtls_dhm_get_len() argument
110 return mbedtls_mpi_size(&ctx->P); in mbedtls_dhm_get_len()
113 int mbedtls_dhm_get_value(const mbedtls_dhm_context *ctx, in mbedtls_dhm_get_value() argument
120 src = &ctx->P; in mbedtls_dhm_get_value()
123 src = &ctx->G; in mbedtls_dhm_get_value()
126 src = &ctx->X; in mbedtls_dhm_get_value()
129 src = &ctx->GX; in mbedtls_dhm_get_value()
132 src = &ctx->GY; in mbedtls_dhm_get_value()
135 src = &ctx->K; in mbedtls_dhm_get_value()
146 int mbedtls_dhm_read_params(mbedtls_dhm_context *ctx, in mbedtls_dhm_read_params() argument
152 if ((ret = dhm_read_bignum(&ctx->P, p, end)) != 0 || in mbedtls_dhm_read_params()
153 (ret = dhm_read_bignum(&ctx->G, p, end)) != 0 || in mbedtls_dhm_read_params()
154 (ret = dhm_read_bignum(&ctx->GY, p, end)) != 0) { in mbedtls_dhm_read_params()
158 if ((ret = dhm_check_range(&ctx->GY, &ctx->P)) != 0) { in mbedtls_dhm_read_params()
180 static int dhm_make_common(mbedtls_dhm_context *ctx, int x_size, in dhm_make_common() argument
186 if (mbedtls_mpi_cmp_int(&ctx->P, 0) == 0) { in dhm_make_common()
193 if ((unsigned) x_size < mbedtls_mpi_size(&ctx->P)) { in dhm_make_common()
194 MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&ctx->X, x_size, f_rng, p_rng)); in dhm_make_common()
197 ret = dhm_random_below(&ctx->X, &ctx->P, f_rng, p_rng); in dhm_make_common()
209 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&ctx->GX, &ctx->G, &ctx->X, in dhm_make_common()
210 &ctx->P, &ctx->RP)); in dhm_make_common()
212 if ((ret = dhm_check_range(&ctx->GX, &ctx->P)) != 0) { in dhm_make_common()
223 int mbedtls_dhm_make_params(mbedtls_dhm_context *ctx, int x_size, in mbedtls_dhm_make_params() argument
232 ret = dhm_make_common(ctx, x_size, f_rng, p_rng); in mbedtls_dhm_make_params()
251 n1 = mbedtls_mpi_size(&ctx->P); in mbedtls_dhm_make_params()
252 n2 = mbedtls_mpi_size(&ctx->G); in mbedtls_dhm_make_params()
253 n3 = mbedtls_mpi_size(&ctx->GX); in mbedtls_dhm_make_params()
256 DHM_MPI_EXPORT(&ctx->P, n1); in mbedtls_dhm_make_params()
257 DHM_MPI_EXPORT(&ctx->G, n2); in mbedtls_dhm_make_params()
258 DHM_MPI_EXPORT(&ctx->GX, n3); in mbedtls_dhm_make_params()
272 int mbedtls_dhm_set_group(mbedtls_dhm_context *ctx, in mbedtls_dhm_set_group() argument
278 if ((ret = mbedtls_mpi_copy(&ctx->P, P)) != 0 || in mbedtls_dhm_set_group()
279 (ret = mbedtls_mpi_copy(&ctx->G, G)) != 0) { in mbedtls_dhm_set_group()
289 int mbedtls_dhm_read_public(mbedtls_dhm_context *ctx, in mbedtls_dhm_read_public() argument
294 if (ilen < 1 || ilen > mbedtls_dhm_get_len(ctx)) { in mbedtls_dhm_read_public()
298 if ((ret = mbedtls_mpi_read_binary(&ctx->GY, input, ilen)) != 0) { in mbedtls_dhm_read_public()
308 int mbedtls_dhm_make_public(mbedtls_dhm_context *ctx, int x_size, in mbedtls_dhm_make_public() argument
315 if (olen < 1 || olen > mbedtls_dhm_get_len(ctx)) { in mbedtls_dhm_make_public()
319 ret = dhm_make_common(ctx, x_size, f_rng, p_rng); in mbedtls_dhm_make_public()
327 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->GX, output, olen)); in mbedtls_dhm_make_public()
343 static int dhm_update_blinding(mbedtls_dhm_context *ctx, in dhm_update_blinding() argument
355 if (mbedtls_mpi_cmp_mpi(&ctx->X, &ctx->pX) != 0) { in dhm_update_blinding()
356 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&ctx->pX, &ctx->X)); in dhm_update_blinding()
357 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->Vi, 1)); in dhm_update_blinding()
358 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->Vf, 1)); in dhm_update_blinding()
367 if (mbedtls_mpi_cmp_int(&ctx->Vi, 1) != 0) { in dhm_update_blinding()
368 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi)); in dhm_update_blinding()
369 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->P)); in dhm_update_blinding()
371 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf)); in dhm_update_blinding()
372 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->P)); in dhm_update_blinding()
382 MBEDTLS_MPI_CHK(dhm_random_below(&ctx->Vi, &ctx->P, f_rng, p_rng)); in dhm_update_blinding()
387 MBEDTLS_MPI_CHK(dhm_random_below(&R, &ctx->P, f_rng, p_rng)); in dhm_update_blinding()
388 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vi, &R)); in dhm_update_blinding()
389 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->P)); in dhm_update_blinding()
390 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->Vf, &ctx->Vf, &ctx->P)); in dhm_update_blinding()
391 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &R)); in dhm_update_blinding()
392 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->P)); in dhm_update_blinding()
394 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP)); in dhm_update_blinding()
405 int mbedtls_dhm_calc_secret(mbedtls_dhm_context *ctx, in mbedtls_dhm_calc_secret() argument
417 if (output_size < mbedtls_dhm_get_len(ctx)) { in mbedtls_dhm_calc_secret()
421 if ((ret = dhm_check_range(&ctx->GY, &ctx->P)) != 0) { in mbedtls_dhm_calc_secret()
428 MBEDTLS_MPI_CHK(dhm_update_blinding(ctx, f_rng, p_rng)); in mbedtls_dhm_calc_secret()
429 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&GYb, &ctx->GY, &ctx->Vi)); in mbedtls_dhm_calc_secret()
430 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&GYb, &GYb, &ctx->P)); in mbedtls_dhm_calc_secret()
433 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&ctx->K, &GYb, &ctx->X, in mbedtls_dhm_calc_secret()
434 &ctx->P, &ctx->RP)); in mbedtls_dhm_calc_secret()
437 MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->K, &ctx->K, &ctx->Vf)); in mbedtls_dhm_calc_secret()
438 MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->K, &ctx->K, &ctx->P)); in mbedtls_dhm_calc_secret()
442 *olen = mbedtls_mpi_size(&ctx->K); in mbedtls_dhm_calc_secret()
443 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->K, output, *olen)); in mbedtls_dhm_calc_secret()
458 void mbedtls_dhm_free(mbedtls_dhm_context *ctx) in mbedtls_dhm_free() argument
460 if (ctx == NULL) { in mbedtls_dhm_free()
464 mbedtls_mpi_free(&ctx->pX); in mbedtls_dhm_free()
465 mbedtls_mpi_free(&ctx->Vf); in mbedtls_dhm_free()
466 mbedtls_mpi_free(&ctx->Vi); in mbedtls_dhm_free()
467 mbedtls_mpi_free(&ctx->RP); in mbedtls_dhm_free()
468 mbedtls_mpi_free(&ctx->K); in mbedtls_dhm_free()
469 mbedtls_mpi_free(&ctx->GY); in mbedtls_dhm_free()
470 mbedtls_mpi_free(&ctx->GX); in mbedtls_dhm_free()
471 mbedtls_mpi_free(&ctx->X); in mbedtls_dhm_free()
472 mbedtls_mpi_free(&ctx->G); in mbedtls_dhm_free()
473 mbedtls_mpi_free(&ctx->P); in mbedtls_dhm_free()
475 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_dhm_context)); in mbedtls_dhm_free()