Lines Matching +full:- +full:m

5  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
11 * SEC1 https://www.secg.org/sec1-v2.pdf
12 * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
13 * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
15 * - https://www.rfc-editor.org/rfc/rfc4492
17 * - https://www.rfc-editor.org/rfc/rfc7748
19 * [Curve25519] https://cr.yp.to/ecdh/curve25519-20060209.pdf
21 * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
23 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
24 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
46 * - The alternative implementation must be activated by
49 * - mbedtls_internal_ecp_free() must \b only be called when the alternative
51 * - mbedtls_internal_ecp_init() must \b not be called when the alternative
53 * - Public functions must not return while the alternative implementation is
55 * - Replaceable functions are guarded by \c MBEDTLS_ECP_XXX_ALT macros and
122 * Restart sub-context for ecp_mul_comb()
141 * Init restart_mul sub-context
145 mbedtls_ecp_point_init(&ctx->R); in ecp_restart_rsm_init()
146 ctx->i = 0; in ecp_restart_rsm_init()
147 ctx->T = NULL; in ecp_restart_rsm_init()
148 ctx->T_size = 0; in ecp_restart_rsm_init()
149 ctx->state = ecp_rsm_init; in ecp_restart_rsm_init()
153 * Free the components of a restart_mul sub-context
163 mbedtls_ecp_point_free(&ctx->R); in ecp_restart_rsm_free()
165 if (ctx->T != NULL) { in ecp_restart_rsm_free()
166 for (i = 0; i < ctx->T_size; i++) { in ecp_restart_rsm_free()
167 mbedtls_ecp_point_free(ctx->T + i); in ecp_restart_rsm_free()
169 mbedtls_free(ctx->T); in ecp_restart_rsm_free()
190 * Init restart_muladd sub-context
194 mbedtls_ecp_point_init(&ctx->mP); in ecp_restart_ma_init()
195 mbedtls_ecp_point_init(&ctx->R); in ecp_restart_ma_init()
196 ctx->state = ecp_rsma_mul1; in ecp_restart_ma_init()
200 * Free the components of a restart_muladd sub-context
208 mbedtls_ecp_point_free(&ctx->mP); in ecp_restart_ma_free()
209 mbedtls_ecp_point_free(&ctx->R); in ecp_restart_ma_free()
219 ctx->ops_done = 0; in mbedtls_ecp_restart_init()
220 ctx->depth = 0; in mbedtls_ecp_restart_init()
221 ctx->rsm = NULL; in mbedtls_ecp_restart_init()
222 ctx->ma = NULL; in mbedtls_ecp_restart_init()
234 ecp_restart_rsm_free(ctx->rsm); in mbedtls_ecp_restart_free()
235 mbedtls_free(ctx->rsm); in mbedtls_ecp_restart_free()
237 ecp_restart_ma_free(ctx->ma); in mbedtls_ecp_restart_free()
238 mbedtls_free(ctx->ma); in mbedtls_ecp_restart_free()
251 /* scale depending on curve size: the chosen reference is 256-bit, in mbedtls_ecp_check_budget()
253 if (grp->pbits >= 512) { in mbedtls_ecp_check_budget()
255 } else if (grp->pbits >= 384) { in mbedtls_ecp_check_budget()
263 if ((rs_ctx->ops_done != 0) && in mbedtls_ecp_check_budget()
264 (rs_ctx->ops_done > ecp_max_ops || in mbedtls_ecp_check_budget()
265 ops > ecp_max_ops - rs_ctx->ops_done)) { in mbedtls_ecp_check_budget()
270 rs_ctx->ops_done += ops; in mbedtls_ecp_check_budget()
276 /* Call this when entering a function that needs its own sub-context */
278 /* reset ops count for this call if top-level */ \
279 if (rs_ctx != NULL && rs_ctx->depth++ == 0) \
280 rs_ctx->ops_done = 0; \
282 /* set up our own sub-context if needed */ \
284 rs_ctx != NULL && rs_ctx->SUB == NULL) \
286 rs_ctx->SUB = mbedtls_calloc(1, sizeof(*rs_ctx->SUB)); \
287 if (rs_ctx->SUB == NULL) \
290 ecp_restart_## SUB ##_init(rs_ctx->SUB); \
294 /* Call this when leaving a function that needs its own sub-context */
296 /* clear our sub-context when not in progress (done or error) */ \
297 if (rs_ctx != NULL && rs_ctx->SUB != NULL && \
300 ecp_restart_## SUB ##_free(rs_ctx->SUB); \
301 mbedtls_free(rs_ctx->SUB); \
302 rs_ctx->SUB = NULL; \
306 rs_ctx->depth--; \
319 while (size--) { in mpi_init_many()
326 while (size--) { in mpi_free_many()
334 * - internal ID
335 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2, RFC 8446 sec. 4.2.7)
336 * - size in bits
337 * - readable name
413 curve_info->grp_id != MBEDTLS_ECP_DP_NONE; in mbedtls_ecp_grp_id_list()
415 ecp_supported_grp_id[i++] = curve_info->grp_id; in mbedtls_ecp_grp_id_list()
433 curve_info->grp_id != MBEDTLS_ECP_DP_NONE; in mbedtls_ecp_curve_info_from_grp_id()
435 if (curve_info->grp_id == grp_id) { in mbedtls_ecp_curve_info_from_grp_id()
451 curve_info->grp_id != MBEDTLS_ECP_DP_NONE; in mbedtls_ecp_curve_info_from_tls_id()
453 if (curve_info->tls_id == tls_id) { in mbedtls_ecp_curve_info_from_tls_id()
473 curve_info->grp_id != MBEDTLS_ECP_DP_NONE; in mbedtls_ecp_curve_info_from_name()
475 if (strcmp(curve_info->name, name) == 0) { in mbedtls_ecp_curve_info_from_name()
488 if (grp->G.X.p == NULL) { in mbedtls_ecp_get_type()
492 if (grp->G.Y.p == NULL) { in mbedtls_ecp_get_type()
504 mbedtls_mpi_init(&pt->X); in mbedtls_ecp_point_init()
505 mbedtls_mpi_init(&pt->Y); in mbedtls_ecp_point_init()
506 mbedtls_mpi_init(&pt->Z); in mbedtls_ecp_point_init()
514 grp->id = MBEDTLS_ECP_DP_NONE; in mbedtls_ecp_group_init()
515 mbedtls_mpi_init(&grp->P); in mbedtls_ecp_group_init()
516 mbedtls_mpi_init(&grp->A); in mbedtls_ecp_group_init()
517 mbedtls_mpi_init(&grp->B); in mbedtls_ecp_group_init()
518 mbedtls_ecp_point_init(&grp->G); in mbedtls_ecp_group_init()
519 mbedtls_mpi_init(&grp->N); in mbedtls_ecp_group_init()
520 grp->pbits = 0; in mbedtls_ecp_group_init()
521 grp->nbits = 0; in mbedtls_ecp_group_init()
522 grp->h = 0; in mbedtls_ecp_group_init()
523 grp->modp = NULL; in mbedtls_ecp_group_init()
524 grp->t_pre = NULL; in mbedtls_ecp_group_init()
525 grp->t_post = NULL; in mbedtls_ecp_group_init()
526 grp->t_data = NULL; in mbedtls_ecp_group_init()
527 grp->T = NULL; in mbedtls_ecp_group_init()
528 grp->T_size = 0; in mbedtls_ecp_group_init()
536 mbedtls_ecp_group_init(&key->grp); in mbedtls_ecp_keypair_init()
537 mbedtls_mpi_init(&key->d); in mbedtls_ecp_keypair_init()
538 mbedtls_ecp_point_init(&key->Q); in mbedtls_ecp_keypair_init()
550 mbedtls_mpi_free(&(pt->X)); in mbedtls_ecp_point_free()
551 mbedtls_mpi_free(&(pt->Y)); in mbedtls_ecp_point_free()
552 mbedtls_mpi_free(&(pt->Z)); in mbedtls_ecp_point_free()
556 * Check that the comb table (grp->T) is static initialized.
561 return grp->T != NULL && grp->T_size == 0; in ecp_group_is_static_comb_table()
579 if (grp->h != 1) { in mbedtls_ecp_group_free()
580 mbedtls_mpi_free(&grp->A); in mbedtls_ecp_group_free()
581 mbedtls_mpi_free(&grp->B); in mbedtls_ecp_group_free()
582 mbedtls_ecp_point_free(&grp->G); in mbedtls_ecp_group_free()
585 mbedtls_mpi_free(&grp->N); in mbedtls_ecp_group_free()
586 mbedtls_mpi_free(&grp->P); in mbedtls_ecp_group_free()
590 if (!ecp_group_is_static_comb_table(grp) && grp->T != NULL) { in mbedtls_ecp_group_free()
591 for (i = 0; i < grp->T_size; i++) { in mbedtls_ecp_group_free()
592 mbedtls_ecp_point_free(&grp->T[i]); in mbedtls_ecp_group_free()
594 mbedtls_free(grp->T); in mbedtls_ecp_group_free()
609 mbedtls_ecp_group_free(&key->grp); in mbedtls_ecp_keypair_free()
610 mbedtls_mpi_free(&key->d); in mbedtls_ecp_keypair_free()
611 mbedtls_ecp_point_free(&key->Q); in mbedtls_ecp_keypair_free()
620 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->X, &Q->X)); in mbedtls_ecp_copy()
621 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->Y, &Q->Y)); in mbedtls_ecp_copy()
622 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->Z, &Q->Z)); in mbedtls_ecp_copy()
633 return mbedtls_ecp_group_load(dst, src->id); in mbedtls_ecp_group_copy()
642 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->X, 1)); in mbedtls_ecp_set_zero()
643 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Y, 1)); in mbedtls_ecp_set_zero()
644 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 0)); in mbedtls_ecp_set_zero()
655 return mbedtls_mpi_cmp_int(&pt->Z, 0) == 0; in mbedtls_ecp_is_zero()
664 if (mbedtls_mpi_cmp_mpi(&P->X, &Q->X) == 0 && in mbedtls_ecp_point_cmp()
665 mbedtls_mpi_cmp_mpi(&P->Y, &Q->Y) == 0 && in mbedtls_ecp_point_cmp()
666 mbedtls_mpi_cmp_mpi(&P->Z, &Q->Z) == 0) { in mbedtls_ecp_point_cmp()
674 * Import a non-zero point from ASCII strings
680 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P->X, radix, x)); in mbedtls_ecp_point_read_string()
681 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P->Y, radix, y)); in mbedtls_ecp_point_read_string()
682 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&P->Z, 1)); in mbedtls_ecp_point_read_string()
703 plen = mbedtls_mpi_size(&grp->P); in mbedtls_ecp_point_write_binary()
713 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary_le(&P->X, buf, plen)); in mbedtls_ecp_point_write_binary()
721 if (mbedtls_mpi_cmp_int(&P->Z, 0) == 0) { in mbedtls_ecp_point_write_binary()
740 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->X, buf + 1, plen)); in mbedtls_ecp_point_write_binary()
741 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->Y, buf + 1 + plen, plen)); in mbedtls_ecp_point_write_binary()
749 buf[0] = 0x02 + mbedtls_mpi_get_bit(&P->Y, 0); in mbedtls_ecp_point_write_binary()
750 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->X, buf + 1, plen)); in mbedtls_ecp_point_write_binary()
779 plen = mbedtls_mpi_size(&grp->P); in mbedtls_ecp_point_read_binary()
787 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&pt->X, buf, plen)); in mbedtls_ecp_point_read_binary()
788 mbedtls_mpi_free(&pt->Y); in mbedtls_ecp_point_read_binary()
790 if (grp->id == MBEDTLS_ECP_DP_CURVE25519) { in mbedtls_ecp_point_read_binary()
792 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&pt->X, plen * 8 - 1, 0)); in mbedtls_ecp_point_read_binary()
795 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1)); in mbedtls_ecp_point_read_binary()
812 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&pt->X, buf + 1, plen)); in mbedtls_ecp_point_read_binary()
813 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1)); in mbedtls_ecp_point_read_binary()
820 return mbedtls_mpi_read_binary(&pt->Y, buf + 1 + plen, plen); in mbedtls_ecp_point_read_binary()
826 return mbedtls_ecp_sw_derive_y(grp, &pt->X, &pt->Y, in mbedtls_ecp_point_read_binary()
841 * opaque point <1..2^8-1>;
858 if (data_len < 1 || data_len > buf_len - 1) { in mbedtls_ecp_tls_read_point()
874 * opaque point <1..2^8-1>;
895 olen, buf + 1, blen - 1)) != 0) { in mbedtls_ecp_tls_write_point()
956 *grp = curve_info->grp_id; in mbedtls_ecp_tls_read_group_id()
968 if ((curve_info = mbedtls_ecp_curve_info_from_grp_id(grp->id)) == NULL) { in mbedtls_ecp_tls_write_group()
988 MBEDTLS_PUT_UINT16_BE(curve_info->tls_id, buf, 0); in mbedtls_ecp_tls_write_group()
994 * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
1003 if (grp->modp == NULL) { in ecp_modp()
1004 return mbedtls_mpi_mod_mpi(N, N, &grp->P); in ecp_modp()
1007 /* N->s < 0 is a much faster test, which fails only if N is 0 */ in ecp_modp()
1008 if ((N->s < 0 && mbedtls_mpi_cmp_int(N, 0) != 0) || in ecp_modp()
1009 mbedtls_mpi_bitlen(N) > 2 * grp->pbits) { in ecp_modp()
1013 MBEDTLS_MPI_CHK(grp->modp(N)); in ecp_modp()
1015 /* N->s < 0 is a much faster test, which fails only if N is 0 */ in ecp_modp()
1016 while (N->s < 0 && mbedtls_mpi_cmp_int(N, 0) != 0) { in ecp_modp()
1017 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(N, N, &grp->P)); in ecp_modp()
1020 while (mbedtls_mpi_cmp_mpi(N, &grp->P) >= 0) { in ecp_modp()
1022 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(N, N, &grp->P)); in ecp_modp()
1030 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
1040 * Reduce a mbedtls_mpi mod p in-place, general case, to use after mbedtls_mpi_mul_mpi
1068 * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
1069 * N->s < 0 is a very fast test, which fails only if N is 0
1073 while ((N)->s < 0 && mbedtls_mpi_cmp_int((N), 0) != 0) \
1074 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi((N), (N), &grp->P)); \
1091 * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_add_mpi and mbedtls_mpi_mul_int.
1096 while (mbedtls_mpi_cmp_mpi((N), &grp->P) >= 0) \
1097 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs((N), (N), &grp->P))
1176 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod((dst), (src), &grp->P))
1195 MBEDTLS_MPI_CHK(mbedtls_mpi_random((X), 2, &grp->P, f_rng, p_rng))
1203 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&tmp, &grp->P, (X))); \
1211 ((X)->p != NULL)
1222 * Computes the right-hand side of the Short Weierstrass equation
1234 /* Special case for A = -3 */ in ecp_sw_rhs()
1238 MPI_ECP_ADD(rhs, rhs, &grp->A); in ecp_sw_rhs()
1242 MPI_ECP_ADD(rhs, rhs, &grp->B); in ecp_sw_rhs()
1265 if (mbedtls_mpi_get_bit(&grp->P, 0) != 1 || in mbedtls_ecp_sw_derive_y()
1266 mbedtls_mpi_get_bit(&grp->P, 1) != 1) { in mbedtls_ecp_sw_derive_y()
1279 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&exp, &grp->P, 1)); in mbedtls_ecp_sw_derive_y()
1282 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(Y, Y /*y^2*/, &exp, &grp->P, NULL)); in mbedtls_ecp_sw_derive_y()
1290 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(Y, &grp->P, Y)); in mbedtls_ecp_sw_derive_y()
1312 * Cost: 1N := 1I + 3M + 1S
1316 if (MPI_ECP_CMP_INT(&pt->Z, 0) == 0) { in ecp_normalize_jac()
1333 MPI_ECP_INV(&T, &pt->Z); /* T <- 1 / Z */ in ecp_normalize_jac()
1334 MPI_ECP_MUL(&pt->Y, &pt->Y, &T); /* Y' <- Y*T = Y / Z */ in ecp_normalize_jac()
1335 MPI_ECP_SQR(&T, &T); /* T <- T^2 = 1 / Z^2 */ in ecp_normalize_jac()
1336 MPI_ECP_MUL(&pt->X, &pt->X, &T); /* X <- X * T = X / Z^2 */ in ecp_normalize_jac()
1337 MPI_ECP_MUL(&pt->Y, &pt->Y, &T); /* Y'' <- Y' * T = Y / Z^3 */ in ecp_normalize_jac()
1339 MPI_ECP_LSET(&pt->Z, 1); in ecp_normalize_jac()
1358 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
1388 * c[i] = Z_0 * ... * Z_i, i = 0,..,n := T_size-1 in ecp_normalize_jac_many()
1390 MPI_ECP_MOV(&c[0], &T[0]->Z); in ecp_normalize_jac_many()
1392 MPI_ECP_MUL(&c[i], &c[i-1], &T[i]->Z); in ecp_normalize_jac_many()
1398 MPI_ECP_INV(&c[T_size-1], &c[T_size-1]); in ecp_normalize_jac_many()
1400 for (i = T_size - 1;; i--) { in ecp_normalize_jac_many()
1402 * - c[j] = Z_0 * .... * Z_j for j < i, in ecp_normalize_jac_many()
1403 * - c[j] = 1 / (Z_0 * .... * Z_j) for j == i, in ecp_normalize_jac_many()
1406 * - c[i-1] <- c[i] * Z_i in ecp_normalize_jac_many()
1408 * We also derive 1/Z_i = c[i] * c[i-1] for i>0 and use that in ecp_normalize_jac_many()
1415 MPI_ECP_MUL(&t, &c[i], &c[i-1]); in ecp_normalize_jac_many()
1416 MPI_ECP_MUL(&c[i-1], &c[i], &T[i]->Z); in ecp_normalize_jac_many()
1422 MPI_ECP_MUL(&T[i]->Y, &T[i]->Y, &t); in ecp_normalize_jac_many()
1424 MPI_ECP_MUL(&T[i]->X, &T[i]->X, &t); in ecp_normalize_jac_many()
1425 MPI_ECP_MUL(&T[i]->Y, &T[i]->Y, &t); in ecp_normalize_jac_many()
1428 * Post-precessing: reclaim some memory by shrinking coordinates in ecp_normalize_jac_many()
1429 * - not storing Z (always 1) in ecp_normalize_jac_many()
1430 * - shrinking other coordinates, but still keeping the same number of in ecp_normalize_jac_many()
1433 MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(&T[i]->X, grp->P.n)); in ecp_normalize_jac_many()
1434 MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(&T[i]->Y, grp->P.n)); in ecp_normalize_jac_many()
1436 MPI_ECP_LSET(&T[i]->Z, 1); in ecp_normalize_jac_many()
1454 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
1465 MPI_ECP_COND_NEG(&Q->Y, inv); in ecp_safe_invert_jac()
1475 * Based on http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-1998-cmo-2 .
1480 * Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
1482 * Cost: 1D := 3M + 4S (A == 0)
1483 * 4M + 4S (A == -3)
1484 * 3M + 6S + 1a otherwise
1505 /* Special case for A = -3 */ in ecp_double_jac()
1507 /* tmp[0] <- M = 3(X + Z^2)(X - Z^2) */ in ecp_double_jac()
1508 MPI_ECP_SQR(&tmp[1], &P->Z); in ecp_double_jac()
1509 MPI_ECP_ADD(&tmp[2], &P->X, &tmp[1]); in ecp_double_jac()
1510 MPI_ECP_SUB(&tmp[3], &P->X, &tmp[1]); in ecp_double_jac()
1514 /* tmp[0] <- M = 3.X^2 + A.Z^4 */ in ecp_double_jac()
1515 MPI_ECP_SQR(&tmp[1], &P->X); in ecp_double_jac()
1519 if (MPI_ECP_CMP_INT(&grp->A, 0) != 0) { in ecp_double_jac()
1520 /* M += A.Z^4 */ in ecp_double_jac()
1521 MPI_ECP_SQR(&tmp[1], &P->Z); in ecp_double_jac()
1523 MPI_ECP_MUL(&tmp[1], &tmp[2], &grp->A); in ecp_double_jac()
1528 /* tmp[1] <- S = 4.X.Y^2 */ in ecp_double_jac()
1529 MPI_ECP_SQR(&tmp[2], &P->Y); in ecp_double_jac()
1531 MPI_ECP_MUL(&tmp[1], &P->X, &tmp[2]); in ecp_double_jac()
1534 /* tmp[3] <- U = 8.Y^4 */ in ecp_double_jac()
1538 /* tmp[2] <- T = M^2 - 2.S */ in ecp_double_jac()
1543 /* tmp[1] <- S = M(S - T) - U */ in ecp_double_jac()
1548 /* tmp[3] <- U = 2.Y.Z */ in ecp_double_jac()
1549 MPI_ECP_MUL(&tmp[3], &P->Y, &P->Z); in ecp_double_jac()
1553 MPI_ECP_MOV(&R->X, &tmp[2]); in ecp_double_jac()
1554 MPI_ECP_MOV(&R->Y, &tmp[1]); in ecp_double_jac()
1555 MPI_ECP_MOV(&R->Z, &tmp[3]); in ecp_double_jac()
1564 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
1571 * Fine-grained aliasing at the level of coordinates is not supported.
1575 * - at each step, P, Q and R are multiples of the base point, the factor
1577 * - Q is an odd multiple of the base point, P an even multiple,
1581 * Cost: 1A := 8M + 3S
1603 * sure that at the point X,Y,Z are written, {P,Q}->{X,Y,Z} are no in ecp_add_mixed()
1605 mbedtls_mpi * const X = &R->X; in ecp_add_mixed()
1606 mbedtls_mpi * const Y = &R->Y; in ecp_add_mixed()
1607 mbedtls_mpi * const Z = &R->Z; in ecp_add_mixed()
1609 if (!MPI_ECP_VALID(&Q->Z)) { in ecp_add_mixed()
1616 if (MPI_ECP_CMP_INT(&P->Z, 0) == 0) { in ecp_add_mixed()
1620 if (MPI_ECP_CMP_INT(&Q->Z, 0) == 0) { in ecp_add_mixed()
1627 if (MPI_ECP_CMP_INT(&Q->Z, 1) != 0) { in ecp_add_mixed()
1631 MPI_ECP_SQR(&tmp[0], &P->Z); in ecp_add_mixed()
1632 MPI_ECP_MUL(&tmp[1], &tmp[0], &P->Z); in ecp_add_mixed()
1633 MPI_ECP_MUL(&tmp[0], &tmp[0], &Q->X); in ecp_add_mixed()
1634 MPI_ECP_MUL(&tmp[1], &tmp[1], &Q->Y); in ecp_add_mixed()
1635 MPI_ECP_SUB(&tmp[0], &tmp[0], &P->X); in ecp_add_mixed()
1636 MPI_ECP_SUB(&tmp[1], &tmp[1], &P->Y); in ecp_add_mixed()
1649 /* {P,Q}->Z no longer used, so OK to write to Z even if there's aliasing. */ in ecp_add_mixed()
1650 MPI_ECP_MUL(Z, &P->Z, &tmp[0]); in ecp_add_mixed()
1653 MPI_ECP_MUL(&tmp[2], &tmp[2], &P->X); in ecp_add_mixed()
1658 /* {P,Q}->X no longer used, so OK to write to X even if there's aliasing. */ in ecp_add_mixed()
1664 MPI_ECP_MUL(&tmp[3], &tmp[3], &P->Y); in ecp_add_mixed()
1665 /* {P,Q}->Y no longer used, so OK to write to Y even if there's aliasing. */ in ecp_add_mixed()
1676 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
1702 MPI_ECP_MUL(&pt->Z, &pt->Z, &l); in ecp_randomize_jac()
1705 MPI_ECP_MUL(&pt->Y, &pt->Y, &l); in ecp_randomize_jac()
1709 MPI_ECP_MUL(&pt->X, &pt->X, &l); in ecp_randomize_jac()
1712 MPI_ECP_MUL(&pt->Y, &pt->Y, &l); in ecp_randomize_jac()
1735 #define COMB_MAX_PRE (1 << (MBEDTLS_ECP_WINDOW_SIZE - 1))
1738 * Compute the representation of m that will be used with our comb method.
1749 * - The goal is to compute m*P for some w*d-bit integer m.
1751 * - The basic comb method splits m into the w-bit integers
1752 * x[0] .. x[d-1] where x[i] consists of the bits in m whose
1753 * index has residue i modulo d, and computes m * P as
1754 * S[x[0]] + 2 * S[x[1]] + .. + 2^(d-1) S[x[d-1]], where
1755 * S[i_{w-1} .. i_0] := i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + i_0 P.
1757 * - If it happens that, say, x[i+1]=0 (=> S[x[i+1]]=0), one can replace the sum by
1758 * .. + 2^{i-1} S[x[i-1]] - 2^i S[x[i]] + 2^{i+1} S[x[i]] + 2^{i+2} S[x[i+2]] ..,
1762 * - More generally, even if x[i+1] != 0, we can first transform the sum as
1763 * .. - 2^i S[x[i]] + 2^{i+1} ( S[x[i]] + S[x[i+1]] ) + 2^{i+2} S[x[i+2]] ..,
1767 * S[x'[0]] +- 2 S[x'[1]] +- .. +- 2^{d-1} S[x'[d-1]] + 2^d S[x'[d]]
1772 * - For the sake of compactness, only the seven low-order bits of x[i]
1775 * if s_i == -1;
1778 * - x is an array of size d + 1
1779 * - w is the size, ie number of teeth, of the comb, and must be between
1781 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1785 unsigned char w, const mbedtls_mpi *m) in ecp_comb_recode_core() argument
1795 x[i] |= mbedtls_mpi_get_bit(m, i + d * j) << j; in ecp_comb_recode_core()
1808 adjust = 1 - (x[i] & 0x01); in ecp_comb_recode_core()
1809 c |= x[i] & (x[i-1] * adjust); in ecp_comb_recode_core()
1810 x[i] = x[i] ^ (x[i-1] * adjust); in ecp_comb_recode_core()
1811 x[i-1] |= adjust << 7; in ecp_comb_recode_core()
1818 * Assumption: T must be able to hold 2^{w - 1} elements.
1820 * Operation: If i = i_{w-1} ... i_1 is the binary representation of i,
1821 * sets T[i] = i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + P.
1823 * Cost: d(w-1) D + (2^{w-1} - 1) A + 1 N(w-1) + 1 N(2^{w-1} - 1)
1830 * (1) [dbl] Computation of intermediate T[i] for 2-power values of i
1837 * on the window size. Here are operation counts for P-256:
1857 const unsigned char T_size = 1U << (w - 1); in ecp_precompute_comb()
1858 mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1] = { NULL }; in ecp_precompute_comb()
1865 if (rs_ctx != NULL && rs_ctx->rsm != NULL) { in ecp_precompute_comb()
1866 if (rs_ctx->rsm->state == ecp_rsm_pre_dbl) { in ecp_precompute_comb()
1869 if (rs_ctx->rsm->state == ecp_rsm_pre_norm_dbl) { in ecp_precompute_comb()
1872 if (rs_ctx->rsm->state == ecp_rsm_pre_add) { in ecp_precompute_comb()
1875 if (rs_ctx->rsm->state == ecp_rsm_pre_norm_add) { in ecp_precompute_comb()
1884 if (rs_ctx != NULL && rs_ctx->rsm != NULL) { in ecp_precompute_comb()
1885 rs_ctx->rsm->state = ecp_rsm_pre_dbl; in ecp_precompute_comb()
1888 rs_ctx->rsm->i = 0; in ecp_precompute_comb()
1895 * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value) in ecp_precompute_comb()
1900 if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0) { in ecp_precompute_comb()
1901 j = rs_ctx->rsm->i; in ecp_precompute_comb()
1906 for (; j < d * (w - 1); j++) { in ecp_precompute_comb()
1920 if (rs_ctx != NULL && rs_ctx->rsm != NULL) { in ecp_precompute_comb()
1921 rs_ctx->rsm->state = ecp_rsm_pre_norm_dbl; in ecp_precompute_comb()
1938 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV + 6 * j - 2); in ecp_precompute_comb()
1943 if (rs_ctx != NULL && rs_ctx->rsm != NULL) { in ecp_precompute_comb()
1944 rs_ctx->rsm->state = ecp_rsm_pre_add; in ecp_precompute_comb()
1953 MBEDTLS_ECP_BUDGET((T_size - 1) * MBEDTLS_ECP_OPS_ADD); in ecp_precompute_comb()
1957 while (j--) { in ecp_precompute_comb()
1963 if (rs_ctx != NULL && rs_ctx->rsm != NULL) { in ecp_precompute_comb()
1964 rs_ctx->rsm->state = ecp_rsm_pre_norm_add; in ecp_precompute_comb()
1978 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV + 6 * j - 2); in ecp_precompute_comb()
1996 if (rs_ctx != NULL && rs_ctx->rsm != NULL && in ecp_precompute_comb()
1998 if (rs_ctx->rsm->state == ecp_rsm_pre_dbl) { in ecp_precompute_comb()
1999 rs_ctx->rsm->i = j; in ecp_precompute_comb()
2022 /* Read the whole table to thwart cache-based timing attacks */ in ecp_select_comb()
2024 MPI_ECP_COND_ASSIGN(&R->X, &T[j].X, j == ii); in ecp_select_comb()
2025 MPI_ECP_COND_ASSIGN(&R->Y, &T[j].Y, j == ii); in ecp_select_comb()
2031 MPI_ECP_LSET(&R->Z, 1); in ecp_select_comb()
2063 if (rs_ctx != NULL && rs_ctx->rsm != NULL && in ecp_mul_comb_core()
2064 rs_ctx->rsm->state != ecp_rsm_comb_core) { in ecp_mul_comb_core()
2065 rs_ctx->rsm->i = 0; in ecp_mul_comb_core()
2066 rs_ctx->rsm->state = ecp_rsm_comb_core; in ecp_mul_comb_core()
2070 if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0) { in ecp_mul_comb_core()
2071 /* restore current index (R already pointing to rs_ctx->rsm->R) */ in ecp_mul_comb_core()
2072 i = rs_ctx->rsm->i; in ecp_mul_comb_core()
2076 /* Start with a non-zero point and randomize its coordinates */ in ecp_mul_comb_core()
2086 --i; in ecp_mul_comb_core()
2099 if (rs_ctx != NULL && rs_ctx->rsm != NULL && in ecp_mul_comb_core()
2101 rs_ctx->rsm->i = i; in ecp_mul_comb_core()
2102 /* no need to save R, already pointing to rs_ctx->rsm->R */ in ecp_mul_comb_core()
2110 * Recode the scalar to get constant-time comb multiplication
2113 * this wrapper ensures that by replacing m by N - m if necessary, and
2117 * curves, so N is always odd hence either m or N - m is.
2122 const mbedtls_mpi *m, in ecp_comb_recode_scalar() argument
2129 mbedtls_mpi M, mm; in ecp_comb_recode_scalar() local
2131 mbedtls_mpi_init(&M); in ecp_comb_recode_scalar()
2135 if (mbedtls_mpi_get_bit(&grp->N, 0) != 1) { in ecp_comb_recode_scalar()
2140 *parity_trick = (mbedtls_mpi_get_bit(m, 0) == 0); in ecp_comb_recode_scalar()
2143 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&M, m)); in ecp_comb_recode_scalar()
2144 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&mm, &grp->N, m)); in ecp_comb_recode_scalar()
2145 MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(&M, &mm, *parity_trick)); in ecp_comb_recode_scalar()
2148 ecp_comb_recode_core(k, d, w, &M); in ecp_comb_recode_scalar()
2152 mbedtls_mpi_free(&M); in ecp_comb_recode_scalar()
2159 * once the auxiliary table has been pre-computed.
2161 * Scalar recoding may use a parity trick that makes us compute -m * P,
2162 * if that is the case we'll need to recover m * P at the end.
2166 const mbedtls_mpi *m, in ecp_mul_comb_after_precomp() argument
2181 if (rs_ctx != NULL && rs_ctx->rsm != NULL) { in ecp_mul_comb_after_precomp()
2182 RR = &rs_ctx->rsm->R; in ecp_mul_comb_after_precomp()
2184 if (rs_ctx->rsm->state == ecp_rsm_final_norm) { in ecp_mul_comb_after_precomp()
2190 MBEDTLS_MPI_CHK(ecp_comb_recode_scalar(grp, m, k, d, w, in ecp_mul_comb_after_precomp()
2197 if (rs_ctx != NULL && rs_ctx->rsm != NULL) { in ecp_mul_comb_after_precomp()
2198 rs_ctx->rsm->state = ecp_rsm_final_norm; in ecp_mul_comb_after_precomp()
2206 * scalar [1], and since our MPI implementation isn't constant-flow, in ecp_mul_comb_after_precomp()
2208 * of its input via side-channels [2]. in ecp_mul_comb_after_precomp()
2222 if (rs_ctx != NULL && rs_ctx->rsm != NULL) { in ecp_mul_comb_after_precomp()
2241 * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w ) in ecp_pick_window_size()
2242 * (see costs of the various parts, with 1S = 1M) in ecp_pick_window_size()
2244 w = grp->nbits >= 384 ? 5 : 4; in ecp_pick_window_size()
2247 * If P == G, pre-compute a bit more, since this may be re-used later. in ecp_pick_window_size()
2269 if (w >= grp->nbits) { in ecp_pick_window_size()
2277 * Multiplication using the comb method - for curves in short Weierstrass form
2280 * - managing the restart context if enabled
2281 * - managing the table of precomputed points (passed between the below two
2290 const mbedtls_mpi *m, const mbedtls_ecp_point *P, in ecp_mul_comb() argument
2305 p_eq_g = (MPI_ECP_CMP(&P->Y, &grp->G.Y) == 0 && in ecp_mul_comb()
2306 MPI_ECP_CMP(&P->X, &grp->G.X) == 0); in ecp_mul_comb()
2313 T_size = 1U << (w - 1); in ecp_mul_comb()
2314 d = (grp->nbits + w - 1) / w; in ecp_mul_comb()
2316 /* Pre-computed table: do we have it already for the base point? */ in ecp_mul_comb()
2317 if (p_eq_g && grp->T != NULL) { in ecp_mul_comb()
2319 T = grp->T; in ecp_mul_comb()
2323 /* Pre-computed table: do we have one in progress? complete? */ in ecp_mul_comb()
2324 if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->T != NULL) { in ecp_mul_comb()
2326 T = rs_ctx->rsm->T; in ecp_mul_comb()
2327 rs_ctx->rsm->T = NULL; in ecp_mul_comb()
2328 rs_ctx->rsm->T_size = 0; in ecp_mul_comb()
2331 T_ok = rs_ctx->rsm->state >= ecp_rsm_comb_core; in ecp_mul_comb()
2356 grp->T = T; in ecp_mul_comb()
2357 grp->T_size = T_size; in ecp_mul_comb()
2362 MBEDTLS_MPI_CHK(ecp_mul_comb_after_precomp(grp, R, m, in ecp_mul_comb()
2369 if (T == grp->T) { in ecp_mul_comb()
2375 if (rs_ctx != NULL && rs_ctx->rsm != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS && T != NULL) { in ecp_mul_comb()
2377 rs_ctx->rsm->T_size = T_size; in ecp_mul_comb()
2378 rs_ctx->rsm->T = T; in ecp_mul_comb()
2421 * Cost: 1M + 1I
2435 MPI_ECP_INV(&P->Z, &P->Z); in ecp_normalize_mxz()
2436 MPI_ECP_MUL(&P->X, &P->X, &P->Z); in ecp_normalize_mxz()
2437 MPI_ECP_LSET(&P->Z, 1); in ecp_normalize_mxz()
2446 * (X, Z) -> (l X, l Z) for random l
2450 * Cost: 2M
2471 MPI_ECP_MUL(&P->X, &P->X, &l); in ecp_randomize_mxz()
2472 MPI_ECP_MUL(&P->Z, &P->Z, &l); in ecp_randomize_mxz()
2485 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
2488 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
2497 * Cost: 5M + 4S
2516 MPI_ECP_ADD(&T[0], &P->X, &P->Z); /* Pp := PX + PZ */ in ecp_double_add_mxz()
2517 MPI_ECP_SUB(&T[1], &P->X, &P->Z); /* Pm := PX - PZ */ in ecp_double_add_mxz()
2518 MPI_ECP_ADD(&T[2], &Q->X, &Q->Z); /* Qp := QX + XZ */ in ecp_double_add_mxz()
2519 MPI_ECP_SUB(&T[3], &Q->X, &Q->Z); /* Qm := QX - QZ */ in ecp_double_add_mxz()
2524 MPI_ECP_MUL(&R->X, &T[0], &T[1]); /* Pp^2 * Pm^2 */ in ecp_double_add_mxz()
2525 MPI_ECP_SUB(&T[0], &T[0], &T[1]); /* Pp^2 - Pm^2 */ in ecp_double_add_mxz()
2526 MPI_ECP_MUL(&R->Z, &grp->A, &T[0]); /* A * (Pp^2 - Pm^2) */ in ecp_double_add_mxz()
2527 MPI_ECP_ADD(&R->Z, &T[1], &R->Z); /* [ A * (Pp^2-Pm^2) ] + Pm^2 */ in ecp_double_add_mxz()
2528 MPI_ECP_ADD(&S->X, &T[3], &T[2]); /* Qm*Pp + Qp*Pm */ in ecp_double_add_mxz()
2529 MPI_ECP_SQR(&S->X, &S->X); /* (Qm*Pp + Qp*Pm)^2 */ in ecp_double_add_mxz()
2530 MPI_ECP_SUB(&S->Z, &T[3], &T[2]); /* Qm*Pp - Qp*Pm */ in ecp_double_add_mxz()
2531 MPI_ECP_SQR(&S->Z, &S->Z); /* (Qm*Pp - Qp*Pm)^2 */ in ecp_double_add_mxz()
2532 MPI_ECP_MUL(&S->Z, d, &S->Z); /* d * ( Qm*Pp - Qp*Pm )^2 */ in ecp_double_add_mxz()
2533 MPI_ECP_MUL(&R->Z, &T[0], &R->Z); /* [A*(Pp^2-Pm^2)+Pm^2]*(Pp^2-Pm^2) */ in ecp_double_add_mxz()
2546 const mbedtls_mpi *m, const mbedtls_ecp_point *P, in ecp_mul_mxz() argument
2565 MPI_ECP_MOV(&PX, &P->X); in ecp_mul_mxz()
2569 MPI_ECP_LSET(&R->X, 1); in ecp_mul_mxz()
2570 MPI_ECP_LSET(&R->Z, 0); in ecp_mul_mxz()
2571 mbedtls_mpi_free(&R->Y); in ecp_mul_mxz()
2580 i = grp->nbits + 1; /* one past the (zero-based) required msb for private keys */ in ecp_mul_mxz()
2581 while (i-- > 0) { in ecp_mul_mxz()
2582 b = mbedtls_mpi_get_bit(m, i); in ecp_mul_mxz()
2590 MPI_ECP_COND_SWAP(&R->X, &RP.X, b); in ecp_mul_mxz()
2591 MPI_ECP_COND_SWAP(&R->Z, &RP.Z, b); in ecp_mul_mxz()
2593 MPI_ECP_COND_SWAP(&R->X, &RP.X, b); in ecp_mul_mxz()
2594 MPI_ECP_COND_SWAP(&R->Z, &RP.Z, b); in ecp_mul_mxz()
2599 * scalar [1], and since our MPI implementation isn't constant-flow, in ecp_mul_mxz()
2601 * of its input via side-channels [2]. in ecp_mul_mxz()
2621 * Restartable multiplication R = m * P
2627 const mbedtls_mpi *m, const mbedtls_ecp_point *P, in ecp_mul_restartable_internal() argument
2637 /* reset ops count for this call if top-level */ in ecp_mul_restartable_internal()
2638 if (rs_ctx != NULL && rs_ctx->depth++ == 0) { in ecp_mul_restartable_internal()
2639 rs_ctx->ops_done = 0; in ecp_mul_restartable_internal()
2653 restarting = (rs_ctx != NULL && rs_ctx->rsm != NULL); in ecp_mul_restartable_internal()
2661 MBEDTLS_MPI_CHK(mbedtls_ecp_check_privkey(grp, m)); in ecp_mul_restartable_internal()
2668 MBEDTLS_MPI_CHK(ecp_mul_mxz(grp, R, m, P, f_rng, p_rng)); in ecp_mul_restartable_internal()
2673 MBEDTLS_MPI_CHK(ecp_mul_comb(grp, R, m, P, f_rng, p_rng, rs_ctx)); in ecp_mul_restartable_internal()
2687 rs_ctx->depth--; in ecp_mul_restartable_internal()
2695 * Restartable multiplication R = m * P
2698 const mbedtls_mpi *m, const mbedtls_ecp_point *P, in mbedtls_ecp_mul_restartable() argument
2706 return ecp_mul_restartable_internal(grp, R, m, P, f_rng, p_rng, rs_ctx); in mbedtls_ecp_mul_restartable()
2710 * Multiplication R = m * P
2713 const mbedtls_mpi *m, const mbedtls_ecp_point *P, in mbedtls_ecp_mul() argument
2716 return mbedtls_ecp_mul_restartable(grp, R, m, P, f_rng, p_rng, NULL); in mbedtls_ecp_mul()
2731 if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0 || in ecp_check_pubkey_sw()
2732 mbedtls_mpi_cmp_int(&pt->Y, 0) < 0 || in ecp_check_pubkey_sw()
2733 mbedtls_mpi_cmp_mpi(&pt->X, &grp->P) >= 0 || in ecp_check_pubkey_sw()
2734 mbedtls_mpi_cmp_mpi(&pt->Y, &grp->P) >= 0) { in ecp_check_pubkey_sw()
2744 MPI_ECP_SQR(&YY, &pt->Y); in ecp_check_pubkey_sw()
2745 MBEDTLS_MPI_CHK(ecp_sw_rhs(grp, &RHS, &pt->X)); in ecp_check_pubkey_sw()
2762 * R = m * P with shortcuts for m == 0, m == 1 and m == -1
2763 * NOT constant-time - ONLY for short Weierstrass!
2767 const mbedtls_mpi *m, in mbedtls_ecp_mul_shortcuts() argument
2775 if (mbedtls_mpi_cmp_int(m, 0) == 0) { in mbedtls_ecp_mul_shortcuts()
2778 } else if (mbedtls_mpi_cmp_int(m, 1) == 0) { in mbedtls_ecp_mul_shortcuts()
2781 } else if (mbedtls_mpi_cmp_int(m, -1) == 0) { in mbedtls_ecp_mul_shortcuts()
2784 MPI_ECP_NEG(&R->Y); in mbedtls_ecp_mul_shortcuts()
2786 MBEDTLS_MPI_CHK(ecp_mul_restartable_internal(grp, R, m, P, in mbedtls_ecp_mul_shortcuts()
2798 * NOT constant-time
2802 const mbedtls_mpi *m, const mbedtls_ecp_point *P, in mbedtls_ecp_muladd_restartable() argument
2824 if (rs_ctx != NULL && rs_ctx->ma != NULL) { in mbedtls_ecp_muladd_restartable()
2826 pmP = &rs_ctx->ma->mP; in mbedtls_ecp_muladd_restartable()
2827 pR = &rs_ctx->ma->R; in mbedtls_ecp_muladd_restartable()
2830 if (rs_ctx->ma->state == ecp_rsma_mul2) { in mbedtls_ecp_muladd_restartable()
2833 if (rs_ctx->ma->state == ecp_rsma_add) { in mbedtls_ecp_muladd_restartable()
2836 if (rs_ctx->ma->state == ecp_rsma_norm) { in mbedtls_ecp_muladd_restartable()
2842 MBEDTLS_MPI_CHK(mbedtls_ecp_mul_shortcuts(grp, pmP, m, P, rs_ctx)); in mbedtls_ecp_muladd_restartable()
2844 if (rs_ctx != NULL && rs_ctx->ma != NULL) { in mbedtls_ecp_muladd_restartable()
2845 rs_ctx->ma->state = ecp_rsma_mul2; in mbedtls_ecp_muladd_restartable()
2859 if (rs_ctx != NULL && rs_ctx->ma != NULL) { in mbedtls_ecp_muladd_restartable()
2860 rs_ctx->ma->state = ecp_rsma_add; in mbedtls_ecp_muladd_restartable()
2868 if (rs_ctx != NULL && rs_ctx->ma != NULL) { in mbedtls_ecp_muladd_restartable()
2869 rs_ctx->ma->state = ecp_rsma_norm; in mbedtls_ecp_muladd_restartable()
2878 if (rs_ctx != NULL && rs_ctx->ma != NULL) { in mbedtls_ecp_muladd_restartable()
2902 * NOT constant-time
2905 const mbedtls_mpi *m, const mbedtls_ecp_point *P, in mbedtls_ecp_muladd() argument
2908 return mbedtls_ecp_muladd_restartable(grp, R, m, P, n, Q, NULL); in mbedtls_ecp_muladd()
2919 * Constants for the two points other than 0, 1, -1 (mod p) in
2942 * Check that the input point is not one of the low-order points.
2963 * these are 0, 1 and -1. For Curve25519 we check the values less than P in ecp_check_bad_points_mx()
3002 * Check validity of a public key for Montgomery curves with x-only schemes
3009 if (mbedtls_mpi_size(&pt->X) > (grp->nbits + 7) / 8) { in ecp_check_pubkey_mx()
3014 * X must be non-negative. This is normally ensured by the way it's in ecp_check_pubkey_mx()
3016 if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0) { in ecp_check_pubkey_mx()
3020 return ecp_check_bad_points_mx(&pt->X, &grp->P, grp->id); in ecp_check_pubkey_mx()
3031 if (mbedtls_mpi_cmp_int(&pt->Z, 1) != 0) { in mbedtls_ecp_check_pubkey()
3059 mbedtls_mpi_bitlen(d) - 1 != grp->nbits) { /* mbedtls_mpi_bitlen is one-based! */ in mbedtls_ecp_check_privkey()
3064 if (grp->nbits == 254 && mbedtls_mpi_get_bit(d, 2) != 0) { in mbedtls_ecp_check_privkey()
3075 mbedtls_mpi_cmp_mpi(d, &grp->N) >= 0) { in mbedtls_ecp_check_privkey()
3097 /* Generate a (high_bit+1)-bit random number by generating just enough in mbedtls_ecp_gen_privkey_mx()
3102 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(d, 8 * n_random_bytes - high_bit - 1)); in mbedtls_ecp_gen_privkey_mx()
3144 return mbedtls_ecp_gen_privkey_mx(grp->nbits, d, f_rng, p_rng); in mbedtls_ecp_gen_privkey()
3150 return mbedtls_ecp_gen_privkey_sw(&grp->N, d, f_rng, p_rng); in mbedtls_ecp_gen_privkey()
3183 return mbedtls_ecp_gen_keypair_base(grp, &grp->G, d, Q, f_rng, p_rng); in mbedtls_ecp_gen_keypair()
3193 if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) { in mbedtls_ecp_gen_key()
3197 return mbedtls_ecp_gen_keypair(&key->grp, &key->d, &key->Q, f_rng, p_rng); in mbedtls_ecp_gen_key()
3207 if (key->grp.id == MBEDTLS_ECP_DP_NONE) { in mbedtls_ecp_set_public_key()
3209 if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) { in mbedtls_ecp_set_public_key()
3212 } else if (key->grp.id != grp_id) { in mbedtls_ecp_set_public_key()
3216 return mbedtls_ecp_copy(&key->Q, Q); in mbedtls_ecp_set_public_key()
3230 if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) { in mbedtls_ecp_read_key()
3237 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { in mbedtls_ecp_read_key()
3246 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&key->d, buf, buflen)); in mbedtls_ecp_read_key()
3249 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 0, 0)); in mbedtls_ecp_read_key()
3250 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 1, 0)); in mbedtls_ecp_read_key()
3251 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 2, 0)); in mbedtls_ecp_read_key()
3255 mbedtls_mpi_set_bit(&key->d, in mbedtls_ecp_read_key()
3256 ECP_CURVE25519_KEY_SIZE * 8 - 1, 0) in mbedtls_ecp_read_key()
3261 mbedtls_mpi_set_bit(&key->d, in mbedtls_ecp_read_key()
3262 ECP_CURVE25519_KEY_SIZE * 8 - 2, 1) in mbedtls_ecp_read_key()
3269 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&key->d, buf, buflen)); in mbedtls_ecp_read_key()
3272 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 0, 0)); in mbedtls_ecp_read_key()
3273 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 1, 0)); in mbedtls_ecp_read_key()
3277 mbedtls_mpi_set_bit(&key->d, in mbedtls_ecp_read_key()
3278 ECP_CURVE448_KEY_SIZE * 8 - 1, 1) in mbedtls_ecp_read_key()
3284 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { in mbedtls_ecp_read_key()
3285 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&key->d, buf, buflen)); in mbedtls_ecp_read_key()
3290 MBEDTLS_MPI_CHK(mbedtls_ecp_check_privkey(&key->grp, &key->d)); in mbedtls_ecp_read_key()
3296 mbedtls_mpi_free(&key->d); in mbedtls_ecp_read_key()
3312 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { in mbedtls_ecp_write_key()
3313 if (key->grp.id == MBEDTLS_ECP_DP_CURVE25519) { in mbedtls_ecp_write_key()
3318 } else if (key->grp.id == MBEDTLS_ECP_DP_CURVE448) { in mbedtls_ecp_write_key()
3323 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary_le(&key->d, buf, buflen)); in mbedtls_ecp_write_key()
3327 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { in mbedtls_ecp_write_key()
3328 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&key->d, buf, buflen)); in mbedtls_ecp_write_key()
3341 size_t len = (key->grp.nbits + 7) / 8; in mbedtls_ecp_write_key_ext()
3350 if (key->d.n == 0) { in mbedtls_ecp_write_key_ext()
3355 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { in mbedtls_ecp_write_key_ext()
3356 return mbedtls_mpi_write_binary_le(&key->d, buf, len); in mbedtls_ecp_write_key_ext()
3361 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { in mbedtls_ecp_write_key_ext()
3362 return mbedtls_mpi_write_binary(&key->d, buf, len); in mbedtls_ecp_write_key_ext()
3377 return mbedtls_ecp_point_write_binary(&key->grp, &key->Q, in mbedtls_ecp_write_public_key()
3384 * Check a public-private key pair
3393 if (pub->grp.id == MBEDTLS_ECP_DP_NONE || in mbedtls_ecp_check_pub_priv()
3394 pub->grp.id != prv->grp.id || in mbedtls_ecp_check_pub_priv()
3395 mbedtls_mpi_cmp_mpi(&pub->Q.X, &prv->Q.X) || in mbedtls_ecp_check_pub_priv()
3396 mbedtls_mpi_cmp_mpi(&pub->Q.Y, &prv->Q.Y) || in mbedtls_ecp_check_pub_priv()
3397 mbedtls_mpi_cmp_mpi(&pub->Q.Z, &prv->Q.Z)) { in mbedtls_ecp_check_pub_priv()
3404 /* mbedtls_ecp_mul() needs a non-const group... */ in mbedtls_ecp_check_pub_priv()
3405 mbedtls_ecp_group_copy(&grp, &prv->grp); in mbedtls_ecp_check_pub_priv()
3408 MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &Q, &prv->d, &prv->grp.G, f_rng, p_rng)); in mbedtls_ecp_check_pub_priv()
3410 if (mbedtls_mpi_cmp_mpi(&Q.X, &prv->Q.X) || in mbedtls_ecp_check_pub_priv()
3411 mbedtls_mpi_cmp_mpi(&Q.Y, &prv->Q.Y) || in mbedtls_ecp_check_pub_priv()
3412 mbedtls_mpi_cmp_mpi(&Q.Z, &prv->Q.Z)) { in mbedtls_ecp_check_pub_priv()
3428 return mbedtls_ecp_mul(&key->grp, &key->Q, &key->d, &key->grp.G, in mbedtls_ecp_keypair_calc_public()
3436 return key->grp.id; in mbedtls_ecp_keypair_get_group_id()
3440 * Export generic key-pair parameters.
3447 if (grp != NULL && (ret = mbedtls_ecp_group_copy(grp, &key->grp)) != 0) { in mbedtls_ecp_export()
3451 if (d != NULL && (ret = mbedtls_mpi_copy(d, &key->d)) != 0) { in mbedtls_ecp_export()
3455 if (Q != NULL && (ret = mbedtls_ecp_copy(Q, &key->Q)) != 0) { in mbedtls_ecp_export()
3466 * PRNG for test - !!!INSECURE NEVER USE IN PRODUCTION!!!
3490 mbedtls_mpi *m) in self_test_adjust_exponent() argument
3493 switch (grp->id) { in self_test_adjust_exponent()
3499 /* Move highest bit from 254 to N-1. Setting bit N-1 is in self_test_adjust_exponent()
3500 * necessary to enforce the highest-bit-set constraint. */ in self_test_adjust_exponent()
3501 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(m, 254, 0)); in self_test_adjust_exponent()
3502 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(m, grp->nbits, 1)); in self_test_adjust_exponent()
3503 /* Copy second-highest bit from 253 to N-2. This is not in self_test_adjust_exponent()
3506 mbedtls_mpi_set_bit(m, grp->nbits - 1, in self_test_adjust_exponent()
3507 mbedtls_mpi_get_bit(m, 253))); in self_test_adjust_exponent()
3512 /* Non-Montgomery curves and Curve25519 need no adjustment. */ in self_test_adjust_exponent()
3514 (void) m; in self_test_adjust_exponent()
3521 /* Calculate R = m.P for each m in exponents. Check that the number of
3522 * basic operations doesn't depend on the value of m. */
3526 mbedtls_mpi *m, in self_test_point() argument
3538 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(m, 16, exponents[0])); in self_test_point()
3539 MBEDTLS_MPI_CHK(self_test_adjust_exponent(grp, m)); in self_test_point()
3540 MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, R, m, P, self_test_rng, NULL)); in self_test_point()
3550 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(m, 16, exponents[i])); in self_test_point()
3551 MBEDTLS_MPI_CHK(self_test_adjust_exponent(grp, m)); in self_test_point()
3552 MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, R, m, P, self_test_rng, NULL)); in self_test_point()
3583 mbedtls_mpi m; in mbedtls_ecp_self_test() local
3592 "FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8C", /* n - 1 */ in mbedtls_ecp_self_test()
3617 mbedtls_mpi_init(&m); in mbedtls_ecp_self_test()
3624 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, mbedtls_ecp_curve_list()->grp_id)); in mbedtls_ecp_self_test()
3631 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&m, 2)); in mbedtls_ecp_self_test()
3632 MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &P, &m, &grp.G, self_test_rng, NULL)); in mbedtls_ecp_self_test()
3634 &grp, &R, &m, &grp.G, in mbedtls_ecp_self_test()
3646 &grp, &R, &m, &P, in mbedtls_ecp_self_test()
3666 #error "MBEDTLS_ECP_MONTGOMERY_ENABLED is defined, but no curve is supported for self-test" in mbedtls_ecp_self_test()
3669 &grp, &R, &m, &grp.G, in mbedtls_ecp_self_test()
3686 mbedtls_mpi_free(&m); in mbedtls_ecp_self_test()