Lines Matching +full:restore +full:- +full:keys

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
386 /* https://tools.ietf.org/id/draft-yang-tls-tls13-sm-suites-05.html */
417 curve_info->grp_id != MBEDTLS_ECP_DP_NONE; in mbedtls_ecp_grp_id_list()
419 ecp_supported_grp_id[i++] = curve_info->grp_id; in mbedtls_ecp_grp_id_list()
437 curve_info->grp_id != MBEDTLS_ECP_DP_NONE; in mbedtls_ecp_curve_info_from_grp_id()
439 if (curve_info->grp_id == grp_id) { in mbedtls_ecp_curve_info_from_grp_id()
455 curve_info->grp_id != MBEDTLS_ECP_DP_NONE; in mbedtls_ecp_curve_info_from_tls_id()
457 if (curve_info->tls_id == tls_id) { in mbedtls_ecp_curve_info_from_tls_id()
477 curve_info->grp_id != MBEDTLS_ECP_DP_NONE; in mbedtls_ecp_curve_info_from_name()
479 if (strcmp(curve_info->name, name) == 0) { in mbedtls_ecp_curve_info_from_name()
492 if (grp->G.X.p == NULL) { in mbedtls_ecp_get_type()
496 if (grp->G.Y.p == NULL) { in mbedtls_ecp_get_type()
508 mbedtls_mpi_init(&pt->X); in mbedtls_ecp_point_init()
509 mbedtls_mpi_init(&pt->Y); in mbedtls_ecp_point_init()
510 mbedtls_mpi_init(&pt->Z); in mbedtls_ecp_point_init()
518 grp->id = MBEDTLS_ECP_DP_NONE; in mbedtls_ecp_group_init()
519 mbedtls_mpi_init(&grp->P); in mbedtls_ecp_group_init()
520 mbedtls_mpi_init(&grp->A); in mbedtls_ecp_group_init()
521 mbedtls_mpi_init(&grp->B); in mbedtls_ecp_group_init()
522 mbedtls_ecp_point_init(&grp->G); in mbedtls_ecp_group_init()
523 mbedtls_mpi_init(&grp->N); in mbedtls_ecp_group_init()
524 grp->pbits = 0; in mbedtls_ecp_group_init()
525 grp->nbits = 0; in mbedtls_ecp_group_init()
526 grp->h = 0; in mbedtls_ecp_group_init()
527 grp->modp = NULL; in mbedtls_ecp_group_init()
528 grp->t_pre = NULL; in mbedtls_ecp_group_init()
529 grp->t_post = NULL; in mbedtls_ecp_group_init()
530 grp->t_data = NULL; in mbedtls_ecp_group_init()
531 grp->T = NULL; in mbedtls_ecp_group_init()
532 grp->T_size = 0; in mbedtls_ecp_group_init()
540 mbedtls_ecp_group_init(&key->grp); in mbedtls_ecp_keypair_init()
541 mbedtls_mpi_init(&key->d); in mbedtls_ecp_keypair_init()
542 mbedtls_ecp_point_init(&key->Q); in mbedtls_ecp_keypair_init()
554 mbedtls_mpi_free(&(pt->X)); in mbedtls_ecp_point_free()
555 mbedtls_mpi_free(&(pt->Y)); in mbedtls_ecp_point_free()
556 mbedtls_mpi_free(&(pt->Z)); in mbedtls_ecp_point_free()
560 * Check that the comb table (grp->T) is static initialized.
565 return grp->T != NULL && grp->T_size == 0; in ecp_group_is_static_comb_table()
583 if (grp->h != 1) { in mbedtls_ecp_group_free()
584 mbedtls_mpi_free(&grp->A); in mbedtls_ecp_group_free()
585 mbedtls_mpi_free(&grp->B); in mbedtls_ecp_group_free()
586 mbedtls_ecp_point_free(&grp->G); in mbedtls_ecp_group_free()
589 mbedtls_mpi_free(&grp->N); in mbedtls_ecp_group_free()
590 mbedtls_mpi_free(&grp->P); in mbedtls_ecp_group_free()
594 if (!ecp_group_is_static_comb_table(grp) && grp->T != NULL) { in mbedtls_ecp_group_free()
595 for (i = 0; i < grp->T_size; i++) { in mbedtls_ecp_group_free()
596 mbedtls_ecp_point_free(&grp->T[i]); in mbedtls_ecp_group_free()
598 mbedtls_free(grp->T); in mbedtls_ecp_group_free()
613 mbedtls_ecp_group_free(&key->grp); in mbedtls_ecp_keypair_free()
614 mbedtls_mpi_free(&key->d); in mbedtls_ecp_keypair_free()
615 mbedtls_ecp_point_free(&key->Q); in mbedtls_ecp_keypair_free()
624 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->X, &Q->X)); in mbedtls_ecp_copy()
625 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->Y, &Q->Y)); in mbedtls_ecp_copy()
626 MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->Z, &Q->Z)); in mbedtls_ecp_copy()
637 return mbedtls_ecp_group_load(dst, src->id); in mbedtls_ecp_group_copy()
646 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->X, 1)); in mbedtls_ecp_set_zero()
647 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Y, 1)); in mbedtls_ecp_set_zero()
648 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 0)); in mbedtls_ecp_set_zero()
659 return mbedtls_mpi_cmp_int(&pt->Z, 0) == 0; in mbedtls_ecp_is_zero()
668 if (mbedtls_mpi_cmp_mpi(&P->X, &Q->X) == 0 && in mbedtls_ecp_point_cmp()
669 mbedtls_mpi_cmp_mpi(&P->Y, &Q->Y) == 0 && in mbedtls_ecp_point_cmp()
670 mbedtls_mpi_cmp_mpi(&P->Z, &Q->Z) == 0) { in mbedtls_ecp_point_cmp()
678 * Import a non-zero point from ASCII strings
684 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P->X, radix, x)); in mbedtls_ecp_point_read_string()
685 MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P->Y, radix, y)); in mbedtls_ecp_point_read_string()
686 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&P->Z, 1)); in mbedtls_ecp_point_read_string()
707 plen = mbedtls_mpi_size(&grp->P); in mbedtls_ecp_point_write_binary()
717 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary_le(&P->X, buf, plen)); in mbedtls_ecp_point_write_binary()
725 if (mbedtls_mpi_cmp_int(&P->Z, 0) == 0) { in mbedtls_ecp_point_write_binary()
744 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->X, buf + 1, plen)); in mbedtls_ecp_point_write_binary()
745 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->Y, buf + 1 + plen, plen)); in mbedtls_ecp_point_write_binary()
753 buf[0] = 0x02 + mbedtls_mpi_get_bit(&P->Y, 0); in mbedtls_ecp_point_write_binary()
754 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->X, buf + 1, plen)); in mbedtls_ecp_point_write_binary()
783 plen = mbedtls_mpi_size(&grp->P); in mbedtls_ecp_point_read_binary()
791 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&pt->X, buf, plen)); in mbedtls_ecp_point_read_binary()
792 mbedtls_mpi_free(&pt->Y); in mbedtls_ecp_point_read_binary()
794 if (grp->id == MBEDTLS_ECP_DP_CURVE25519) { in mbedtls_ecp_point_read_binary()
796 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&pt->X, plen * 8 - 1, 0)); in mbedtls_ecp_point_read_binary()
799 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1)); in mbedtls_ecp_point_read_binary()
816 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&pt->X, buf + 1, plen)); in mbedtls_ecp_point_read_binary()
817 MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1)); in mbedtls_ecp_point_read_binary()
824 return mbedtls_mpi_read_binary(&pt->Y, buf + 1 + plen, plen); in mbedtls_ecp_point_read_binary()
830 return mbedtls_ecp_sw_derive_y(grp, &pt->X, &pt->Y, in mbedtls_ecp_point_read_binary()
845 * opaque point <1..2^8-1>;
862 if (data_len < 1 || data_len > buf_len - 1) { in mbedtls_ecp_tls_read_point()
878 * opaque point <1..2^8-1>;
899 olen, buf + 1, blen - 1)) != 0) { in mbedtls_ecp_tls_write_point()
960 *grp = curve_info->grp_id; in mbedtls_ecp_tls_read_group_id()
972 if ((curve_info = mbedtls_ecp_curve_info_from_grp_id(grp->id)) == NULL) { in mbedtls_ecp_tls_write_group()
992 MBEDTLS_PUT_UINT16_BE(curve_info->tls_id, buf, 0); in mbedtls_ecp_tls_write_group()
998 * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
1007 if (grp->modp == NULL) { in ecp_modp()
1008 return mbedtls_mpi_mod_mpi(N, N, &grp->P); in ecp_modp()
1011 /* N->s < 0 is a much faster test, which fails only if N is 0 */ in ecp_modp()
1012 if ((N->s < 0 && mbedtls_mpi_cmp_int(N, 0) != 0) || in ecp_modp()
1013 mbedtls_mpi_bitlen(N) > 2 * grp->pbits) { in ecp_modp()
1017 MBEDTLS_MPI_CHK(grp->modp(N)); in ecp_modp()
1019 /* N->s < 0 is a much faster test, which fails only if N is 0 */ in ecp_modp()
1020 while (N->s < 0 && mbedtls_mpi_cmp_int(N, 0) != 0) { in ecp_modp()
1021 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(N, N, &grp->P)); in ecp_modp()
1024 while (mbedtls_mpi_cmp_mpi(N, &grp->P) >= 0) { in ecp_modp()
1026 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(N, N, &grp->P)); in ecp_modp()
1034 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
1044 * Reduce a mbedtls_mpi mod p in-place, general case, to use after mbedtls_mpi_mul_mpi
1072 * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
1073 * N->s < 0 is a very fast test, which fails only if N is 0
1077 while ((N)->s < 0 && mbedtls_mpi_cmp_int((N), 0) != 0) \
1078 MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi((N), (N), &grp->P)); \
1095 * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_add_mpi and mbedtls_mpi_mul_int.
1100 while (mbedtls_mpi_cmp_mpi((N), &grp->P) >= 0) \
1101 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs((N), (N), &grp->P))
1180 MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod((dst), (src), &grp->P))
1199 MBEDTLS_MPI_CHK(mbedtls_mpi_random((X), 2, &grp->P, f_rng, p_rng))
1207 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&tmp, &grp->P, (X))); \
1215 ((X)->p != NULL)
1226 * Computes the right-hand side of the Short Weierstrass equation
1238 /* Special case for A = -3 */ in ecp_sw_rhs()
1242 MPI_ECP_ADD(rhs, rhs, &grp->A); in ecp_sw_rhs()
1246 MPI_ECP_ADD(rhs, rhs, &grp->B); in ecp_sw_rhs()
1269 if (mbedtls_mpi_get_bit(&grp->P, 0) != 1 || in mbedtls_ecp_sw_derive_y()
1270 mbedtls_mpi_get_bit(&grp->P, 1) != 1) { in mbedtls_ecp_sw_derive_y()
1283 MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&exp, &grp->P, 1)); in mbedtls_ecp_sw_derive_y()
1286 MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(Y, Y /*y^2*/, &exp, &grp->P, NULL)); in mbedtls_ecp_sw_derive_y()
1294 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(Y, &grp->P, Y)); in mbedtls_ecp_sw_derive_y()
1320 if (MPI_ECP_CMP_INT(&pt->Z, 0) == 0) { in ecp_normalize_jac()
1337 MPI_ECP_INV(&T, &pt->Z); /* T <- 1 / Z */ in ecp_normalize_jac()
1338 MPI_ECP_MUL(&pt->Y, &pt->Y, &T); /* Y' <- Y*T = Y / Z */ in ecp_normalize_jac()
1339 MPI_ECP_SQR(&T, &T); /* T <- T^2 = 1 / Z^2 */ in ecp_normalize_jac()
1340 MPI_ECP_MUL(&pt->X, &pt->X, &T); /* X <- X * T = X / Z^2 */ in ecp_normalize_jac()
1341 MPI_ECP_MUL(&pt->Y, &pt->Y, &T); /* Y'' <- Y' * T = Y / Z^3 */ in ecp_normalize_jac()
1343 MPI_ECP_LSET(&pt->Z, 1); in ecp_normalize_jac()
1362 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
1392 * c[i] = Z_0 * ... * Z_i, i = 0,..,n := T_size-1 in ecp_normalize_jac_many()
1394 MPI_ECP_MOV(&c[0], &T[0]->Z); in ecp_normalize_jac_many()
1396 MPI_ECP_MUL(&c[i], &c[i-1], &T[i]->Z); in ecp_normalize_jac_many()
1402 MPI_ECP_INV(&c[T_size-1], &c[T_size-1]); in ecp_normalize_jac_many()
1404 for (i = T_size - 1;; i--) { in ecp_normalize_jac_many()
1406 * - c[j] = Z_0 * .... * Z_j for j < i, in ecp_normalize_jac_many()
1407 * - c[j] = 1 / (Z_0 * .... * Z_j) for j == i, in ecp_normalize_jac_many()
1410 * - c[i-1] <- c[i] * Z_i in ecp_normalize_jac_many()
1412 * We also derive 1/Z_i = c[i] * c[i-1] for i>0 and use that in ecp_normalize_jac_many()
1419 MPI_ECP_MUL(&t, &c[i], &c[i-1]); in ecp_normalize_jac_many()
1420 MPI_ECP_MUL(&c[i-1], &c[i], &T[i]->Z); in ecp_normalize_jac_many()
1426 MPI_ECP_MUL(&T[i]->Y, &T[i]->Y, &t); in ecp_normalize_jac_many()
1428 MPI_ECP_MUL(&T[i]->X, &T[i]->X, &t); in ecp_normalize_jac_many()
1429 MPI_ECP_MUL(&T[i]->Y, &T[i]->Y, &t); in ecp_normalize_jac_many()
1432 * Post-precessing: reclaim some memory by shrinking coordinates in ecp_normalize_jac_many()
1433 * - not storing Z (always 1) in ecp_normalize_jac_many()
1434 * - shrinking other coordinates, but still keeping the same number of in ecp_normalize_jac_many()
1437 MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(&T[i]->X, grp->P.n)); in ecp_normalize_jac_many()
1438 MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(&T[i]->Y, grp->P.n)); in ecp_normalize_jac_many()
1440 MPI_ECP_LSET(&T[i]->Z, 1); in ecp_normalize_jac_many()
1458 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
1469 MPI_ECP_COND_NEG(&Q->Y, inv); in ecp_safe_invert_jac()
1479 * Based on http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-1998-cmo-2 .
1484 * Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
1487 * 4M + 4S (A == -3)
1509 /* Special case for A = -3 */ in ecp_double_jac()
1511 /* tmp[0] <- M = 3(X + Z^2)(X - Z^2) */ in ecp_double_jac()
1512 MPI_ECP_SQR(&tmp[1], &P->Z); in ecp_double_jac()
1513 MPI_ECP_ADD(&tmp[2], &P->X, &tmp[1]); in ecp_double_jac()
1514 MPI_ECP_SUB(&tmp[3], &P->X, &tmp[1]); in ecp_double_jac()
1518 /* tmp[0] <- M = 3.X^2 + A.Z^4 */ in ecp_double_jac()
1519 MPI_ECP_SQR(&tmp[1], &P->X); in ecp_double_jac()
1523 if (MPI_ECP_CMP_INT(&grp->A, 0) != 0) { in ecp_double_jac()
1525 MPI_ECP_SQR(&tmp[1], &P->Z); in ecp_double_jac()
1527 MPI_ECP_MUL(&tmp[1], &tmp[2], &grp->A); in ecp_double_jac()
1532 /* tmp[1] <- S = 4.X.Y^2 */ in ecp_double_jac()
1533 MPI_ECP_SQR(&tmp[2], &P->Y); in ecp_double_jac()
1535 MPI_ECP_MUL(&tmp[1], &P->X, &tmp[2]); in ecp_double_jac()
1538 /* tmp[3] <- U = 8.Y^4 */ in ecp_double_jac()
1542 /* tmp[2] <- T = M^2 - 2.S */ in ecp_double_jac()
1547 /* tmp[1] <- S = M(S - T) - U */ in ecp_double_jac()
1552 /* tmp[3] <- U = 2.Y.Z */ in ecp_double_jac()
1553 MPI_ECP_MUL(&tmp[3], &P->Y, &P->Z); in ecp_double_jac()
1557 MPI_ECP_MOV(&R->X, &tmp[2]); in ecp_double_jac()
1558 MPI_ECP_MOV(&R->Y, &tmp[1]); in ecp_double_jac()
1559 MPI_ECP_MOV(&R->Z, &tmp[3]); in ecp_double_jac()
1568 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
1575 * Fine-grained aliasing at the level of coordinates is not supported.
1579 * - at each step, P, Q and R are multiples of the base point, the factor
1581 * - Q is an odd multiple of the base point, P an even multiple,
1607 * sure that at the point X,Y,Z are written, {P,Q}->{X,Y,Z} are no in ecp_add_mixed()
1609 mbedtls_mpi * const X = &R->X; in ecp_add_mixed()
1610 mbedtls_mpi * const Y = &R->Y; in ecp_add_mixed()
1611 mbedtls_mpi * const Z = &R->Z; in ecp_add_mixed()
1613 if (!MPI_ECP_VALID(&Q->Z)) { in ecp_add_mixed()
1620 if (MPI_ECP_CMP_INT(&P->Z, 0) == 0) { in ecp_add_mixed()
1624 if (MPI_ECP_CMP_INT(&Q->Z, 0) == 0) { in ecp_add_mixed()
1631 if (MPI_ECP_CMP_INT(&Q->Z, 1) != 0) { in ecp_add_mixed()
1635 MPI_ECP_SQR(&tmp[0], &P->Z); in ecp_add_mixed()
1636 MPI_ECP_MUL(&tmp[1], &tmp[0], &P->Z); in ecp_add_mixed()
1637 MPI_ECP_MUL(&tmp[0], &tmp[0], &Q->X); in ecp_add_mixed()
1638 MPI_ECP_MUL(&tmp[1], &tmp[1], &Q->Y); in ecp_add_mixed()
1639 MPI_ECP_SUB(&tmp[0], &tmp[0], &P->X); in ecp_add_mixed()
1640 MPI_ECP_SUB(&tmp[1], &tmp[1], &P->Y); in ecp_add_mixed()
1653 /* {P,Q}->Z no longer used, so OK to write to Z even if there's aliasing. */ in ecp_add_mixed()
1654 MPI_ECP_MUL(Z, &P->Z, &tmp[0]); in ecp_add_mixed()
1657 MPI_ECP_MUL(&tmp[2], &tmp[2], &P->X); in ecp_add_mixed()
1662 /* {P,Q}->X no longer used, so OK to write to X even if there's aliasing. */ in ecp_add_mixed()
1668 MPI_ECP_MUL(&tmp[3], &tmp[3], &P->Y); in ecp_add_mixed()
1669 /* {P,Q}->Y no longer used, so OK to write to Y even if there's aliasing. */ in ecp_add_mixed()
1680 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
1706 MPI_ECP_MUL(&pt->Z, &pt->Z, &l); in ecp_randomize_jac()
1709 MPI_ECP_MUL(&pt->Y, &pt->Y, &l); in ecp_randomize_jac()
1713 MPI_ECP_MUL(&pt->X, &pt->X, &l); in ecp_randomize_jac()
1716 MPI_ECP_MUL(&pt->Y, &pt->Y, &l); in ecp_randomize_jac()
1739 #define COMB_MAX_PRE (1 << (MBEDTLS_ECP_WINDOW_SIZE - 1))
1753 * - The goal is to compute m*P for some w*d-bit integer m.
1755 * - The basic comb method splits m into the w-bit integers
1756 * x[0] .. x[d-1] where x[i] consists of the bits in m whose
1758 * S[x[0]] + 2 * S[x[1]] + .. + 2^(d-1) S[x[d-1]], where
1759 * S[i_{w-1} .. i_0] := i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + i_0 P.
1761 * - If it happens that, say, x[i+1]=0 (=> S[x[i+1]]=0), one can replace the sum by
1762 * .. + 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]] ..,
1766 * - More generally, even if x[i+1] != 0, we can first transform the sum as
1767 * .. - 2^i S[x[i]] + 2^{i+1} ( S[x[i]] + S[x[i+1]] ) + 2^{i+2} S[x[i+2]] ..,
1771 * S[x'[0]] +- 2 S[x'[1]] +- .. +- 2^{d-1} S[x'[d-1]] + 2^d S[x'[d]]
1776 * - For the sake of compactness, only the seven low-order bits of x[i]
1779 * if s_i == -1;
1782 * - x is an array of size d + 1
1783 * - w is the size, ie number of teeth, of the comb, and must be between
1785 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1812 adjust = 1 - (x[i] & 0x01); in ecp_comb_recode_core()
1813 c |= x[i] & (x[i-1] * adjust); in ecp_comb_recode_core()
1814 x[i] = x[i] ^ (x[i-1] * adjust); in ecp_comb_recode_core()
1815 x[i-1] |= adjust << 7; in ecp_comb_recode_core()
1822 * Assumption: T must be able to hold 2^{w - 1} elements.
1824 * Operation: If i = i_{w-1} ... i_1 is the binary representation of i,
1825 * sets T[i] = i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + P.
1827 * Cost: d(w-1) D + (2^{w-1} - 1) A + 1 N(w-1) + 1 N(2^{w-1} - 1)
1834 * (1) [dbl] Computation of intermediate T[i] for 2-power values of i
1841 * on the window size. Here are operation counts for P-256:
1861 const unsigned char T_size = 1U << (w - 1); in ecp_precompute_comb()
1862 mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1] = { NULL }; in ecp_precompute_comb()
1869 if (rs_ctx != NULL && rs_ctx->rsm != NULL) { in ecp_precompute_comb()
1870 if (rs_ctx->rsm->state == ecp_rsm_pre_dbl) { in ecp_precompute_comb()
1873 if (rs_ctx->rsm->state == ecp_rsm_pre_norm_dbl) { in ecp_precompute_comb()
1876 if (rs_ctx->rsm->state == ecp_rsm_pre_add) { in ecp_precompute_comb()
1879 if (rs_ctx->rsm->state == ecp_rsm_pre_norm_add) { in ecp_precompute_comb()
1888 if (rs_ctx != NULL && rs_ctx->rsm != NULL) { in ecp_precompute_comb()
1889 rs_ctx->rsm->state = ecp_rsm_pre_dbl; in ecp_precompute_comb()
1892 rs_ctx->rsm->i = 0; in ecp_precompute_comb()
1899 * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value) in ecp_precompute_comb()
1904 if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0) { in ecp_precompute_comb()
1905 j = rs_ctx->rsm->i; in ecp_precompute_comb()
1910 for (; j < d * (w - 1); j++) { in ecp_precompute_comb()
1924 if (rs_ctx != NULL && rs_ctx->rsm != NULL) { in ecp_precompute_comb()
1925 rs_ctx->rsm->state = ecp_rsm_pre_norm_dbl; in ecp_precompute_comb()
1942 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV + 6 * j - 2); in ecp_precompute_comb()
1947 if (rs_ctx != NULL && rs_ctx->rsm != NULL) { in ecp_precompute_comb()
1948 rs_ctx->rsm->state = ecp_rsm_pre_add; in ecp_precompute_comb()
1957 MBEDTLS_ECP_BUDGET((T_size - 1) * MBEDTLS_ECP_OPS_ADD); in ecp_precompute_comb()
1961 while (j--) { in ecp_precompute_comb()
1967 if (rs_ctx != NULL && rs_ctx->rsm != NULL) { in ecp_precompute_comb()
1968 rs_ctx->rsm->state = ecp_rsm_pre_norm_add; in ecp_precompute_comb()
1982 MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV + 6 * j - 2); in ecp_precompute_comb()
2000 if (rs_ctx != NULL && rs_ctx->rsm != NULL && in ecp_precompute_comb()
2002 if (rs_ctx->rsm->state == ecp_rsm_pre_dbl) { in ecp_precompute_comb()
2003 rs_ctx->rsm->i = j; in ecp_precompute_comb()
2026 /* Read the whole table to thwart cache-based timing attacks */ in ecp_select_comb()
2028 MPI_ECP_COND_ASSIGN(&R->X, &T[j].X, j == ii); in ecp_select_comb()
2029 MPI_ECP_COND_ASSIGN(&R->Y, &T[j].Y, j == ii); in ecp_select_comb()
2035 MPI_ECP_LSET(&R->Z, 1); in ecp_select_comb()
2067 if (rs_ctx != NULL && rs_ctx->rsm != NULL && in ecp_mul_comb_core()
2068 rs_ctx->rsm->state != ecp_rsm_comb_core) { in ecp_mul_comb_core()
2069 rs_ctx->rsm->i = 0; in ecp_mul_comb_core()
2070 rs_ctx->rsm->state = ecp_rsm_comb_core; in ecp_mul_comb_core()
2074 if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0) { in ecp_mul_comb_core()
2075 /* restore current index (R already pointing to rs_ctx->rsm->R) */ in ecp_mul_comb_core()
2076 i = rs_ctx->rsm->i; in ecp_mul_comb_core()
2080 /* Start with a non-zero point and randomize its coordinates */ in ecp_mul_comb_core()
2090 --i; in ecp_mul_comb_core()
2103 if (rs_ctx != NULL && rs_ctx->rsm != NULL && in ecp_mul_comb_core()
2105 rs_ctx->rsm->i = i; in ecp_mul_comb_core()
2106 /* no need to save R, already pointing to rs_ctx->rsm->R */ in ecp_mul_comb_core()
2114 * Recode the scalar to get constant-time comb multiplication
2117 * this wrapper ensures that by replacing m by N - m if necessary, and
2121 * curves, so N is always odd hence either m or N - m is.
2139 if (mbedtls_mpi_get_bit(&grp->N, 0) != 1) { in ecp_comb_recode_scalar()
2148 MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&mm, &grp->N, m)); in ecp_comb_recode_scalar()
2163 * once the auxiliary table has been pre-computed.
2165 * Scalar recoding may use a parity trick that makes us compute -m * P,
2185 if (rs_ctx != NULL && rs_ctx->rsm != NULL) { in ecp_mul_comb_after_precomp()
2186 RR = &rs_ctx->rsm->R; in ecp_mul_comb_after_precomp()
2188 if (rs_ctx->rsm->state == ecp_rsm_final_norm) { in ecp_mul_comb_after_precomp()
2201 if (rs_ctx != NULL && rs_ctx->rsm != NULL) { in ecp_mul_comb_after_precomp()
2202 rs_ctx->rsm->state = ecp_rsm_final_norm; in ecp_mul_comb_after_precomp()
2210 * scalar [1], and since our MPI implementation isn't constant-flow, in ecp_mul_comb_after_precomp()
2212 * of its input via side-channels [2]. in ecp_mul_comb_after_precomp()
2226 if (rs_ctx != NULL && rs_ctx->rsm != NULL) { in ecp_mul_comb_after_precomp()
2245 * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w ) in ecp_pick_window_size()
2248 w = grp->nbits >= 384 ? 5 : 4; in ecp_pick_window_size()
2251 * If P == G, pre-compute a bit more, since this may be re-used later. in ecp_pick_window_size()
2273 if (w >= grp->nbits) { in ecp_pick_window_size()
2281 * Multiplication using the comb method - for curves in short Weierstrass form
2284 * - managing the restart context if enabled
2285 * - managing the table of precomputed points (passed between the below two
2309 p_eq_g = (MPI_ECP_CMP(&P->Y, &grp->G.Y) == 0 && in ecp_mul_comb()
2310 MPI_ECP_CMP(&P->X, &grp->G.X) == 0); in ecp_mul_comb()
2317 T_size = 1U << (w - 1); in ecp_mul_comb()
2318 d = (grp->nbits + w - 1) / w; in ecp_mul_comb()
2320 /* Pre-computed table: do we have it already for the base point? */ in ecp_mul_comb()
2321 if (p_eq_g && grp->T != NULL) { in ecp_mul_comb()
2323 T = grp->T; in ecp_mul_comb()
2327 /* Pre-computed table: do we have one in progress? complete? */ in ecp_mul_comb()
2328 if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->T != NULL) { in ecp_mul_comb()
2330 T = rs_ctx->rsm->T; in ecp_mul_comb()
2331 rs_ctx->rsm->T = NULL; in ecp_mul_comb()
2332 rs_ctx->rsm->T_size = 0; in ecp_mul_comb()
2335 T_ok = rs_ctx->rsm->state >= ecp_rsm_comb_core; in ecp_mul_comb()
2360 grp->T = T; in ecp_mul_comb()
2361 grp->T_size = T_size; in ecp_mul_comb()
2373 if (T == grp->T) { in ecp_mul_comb()
2379 if (rs_ctx != NULL && rs_ctx->rsm != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS && T != NULL) { in ecp_mul_comb()
2381 rs_ctx->rsm->T_size = T_size; in ecp_mul_comb()
2382 rs_ctx->rsm->T = T; in ecp_mul_comb()
2439 MPI_ECP_INV(&P->Z, &P->Z); in ecp_normalize_mxz()
2440 MPI_ECP_MUL(&P->X, &P->X, &P->Z); in ecp_normalize_mxz()
2441 MPI_ECP_LSET(&P->Z, 1); in ecp_normalize_mxz()
2450 * (X, Z) -> (l X, l Z) for random l
2475 MPI_ECP_MUL(&P->X, &P->X, &l); in ecp_randomize_mxz()
2476 MPI_ECP_MUL(&P->Z, &P->Z, &l); in ecp_randomize_mxz()
2489 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
2492 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
2520 MPI_ECP_ADD(&T[0], &P->X, &P->Z); /* Pp := PX + PZ */ in ecp_double_add_mxz()
2521 MPI_ECP_SUB(&T[1], &P->X, &P->Z); /* Pm := PX - PZ */ in ecp_double_add_mxz()
2522 MPI_ECP_ADD(&T[2], &Q->X, &Q->Z); /* Qp := QX + XZ */ in ecp_double_add_mxz()
2523 MPI_ECP_SUB(&T[3], &Q->X, &Q->Z); /* Qm := QX - QZ */ in ecp_double_add_mxz()
2528 MPI_ECP_MUL(&R->X, &T[0], &T[1]); /* Pp^2 * Pm^2 */ in ecp_double_add_mxz()
2529 MPI_ECP_SUB(&T[0], &T[0], &T[1]); /* Pp^2 - Pm^2 */ in ecp_double_add_mxz()
2530 MPI_ECP_MUL(&R->Z, &grp->A, &T[0]); /* A * (Pp^2 - Pm^2) */ in ecp_double_add_mxz()
2531 MPI_ECP_ADD(&R->Z, &T[1], &R->Z); /* [ A * (Pp^2-Pm^2) ] + Pm^2 */ in ecp_double_add_mxz()
2532 MPI_ECP_ADD(&S->X, &T[3], &T[2]); /* Qm*Pp + Qp*Pm */ in ecp_double_add_mxz()
2533 MPI_ECP_SQR(&S->X, &S->X); /* (Qm*Pp + Qp*Pm)^2 */ in ecp_double_add_mxz()
2534 MPI_ECP_SUB(&S->Z, &T[3], &T[2]); /* Qm*Pp - Qp*Pm */ in ecp_double_add_mxz()
2535 MPI_ECP_SQR(&S->Z, &S->Z); /* (Qm*Pp - Qp*Pm)^2 */ in ecp_double_add_mxz()
2536 MPI_ECP_MUL(&S->Z, d, &S->Z); /* d * ( Qm*Pp - Qp*Pm )^2 */ in ecp_double_add_mxz()
2537 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()
2569 MPI_ECP_MOV(&PX, &P->X); in ecp_mul_mxz()
2573 MPI_ECP_LSET(&R->X, 1); in ecp_mul_mxz()
2574 MPI_ECP_LSET(&R->Z, 0); in ecp_mul_mxz()
2575 mbedtls_mpi_free(&R->Y); in ecp_mul_mxz()
2584 i = grp->nbits + 1; /* one past the (zero-based) required msb for private keys */ in ecp_mul_mxz()
2585 while (i-- > 0) { in ecp_mul_mxz()
2594 MPI_ECP_COND_SWAP(&R->X, &RP.X, b); in ecp_mul_mxz()
2595 MPI_ECP_COND_SWAP(&R->Z, &RP.Z, b); in ecp_mul_mxz()
2597 MPI_ECP_COND_SWAP(&R->X, &RP.X, b); in ecp_mul_mxz()
2598 MPI_ECP_COND_SWAP(&R->Z, &RP.Z, b); in ecp_mul_mxz()
2603 * scalar [1], and since our MPI implementation isn't constant-flow, in ecp_mul_mxz()
2605 * of its input via side-channels [2]. in ecp_mul_mxz()
2641 /* reset ops count for this call if top-level */ in ecp_mul_restartable_internal()
2642 if (rs_ctx != NULL && rs_ctx->depth++ == 0) { in ecp_mul_restartable_internal()
2643 rs_ctx->ops_done = 0; in ecp_mul_restartable_internal()
2657 restarting = (rs_ctx != NULL && rs_ctx->rsm != NULL); in ecp_mul_restartable_internal()
2691 rs_ctx->depth--; in ecp_mul_restartable_internal()
2735 if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0 || in ecp_check_pubkey_sw()
2736 mbedtls_mpi_cmp_int(&pt->Y, 0) < 0 || in ecp_check_pubkey_sw()
2737 mbedtls_mpi_cmp_mpi(&pt->X, &grp->P) >= 0 || in ecp_check_pubkey_sw()
2738 mbedtls_mpi_cmp_mpi(&pt->Y, &grp->P) >= 0) { in ecp_check_pubkey_sw()
2748 MPI_ECP_SQR(&YY, &pt->Y); in ecp_check_pubkey_sw()
2749 MBEDTLS_MPI_CHK(ecp_sw_rhs(grp, &RHS, &pt->X)); in ecp_check_pubkey_sw()
2766 * R = m * P with shortcuts for m == 0, m == 1 and m == -1
2767 * NOT constant-time - ONLY for short Weierstrass!
2785 } else if (mbedtls_mpi_cmp_int(m, -1) == 0) { in mbedtls_ecp_mul_shortcuts()
2788 MPI_ECP_NEG(&R->Y); in mbedtls_ecp_mul_shortcuts()
2802 * NOT constant-time
2828 if (rs_ctx != NULL && rs_ctx->ma != NULL) { in mbedtls_ecp_muladd_restartable()
2830 pmP = &rs_ctx->ma->mP; in mbedtls_ecp_muladd_restartable()
2831 pR = &rs_ctx->ma->R; in mbedtls_ecp_muladd_restartable()
2834 if (rs_ctx->ma->state == ecp_rsma_mul2) { in mbedtls_ecp_muladd_restartable()
2837 if (rs_ctx->ma->state == ecp_rsma_add) { in mbedtls_ecp_muladd_restartable()
2840 if (rs_ctx->ma->state == ecp_rsma_norm) { in mbedtls_ecp_muladd_restartable()
2848 if (rs_ctx != NULL && rs_ctx->ma != NULL) { in mbedtls_ecp_muladd_restartable()
2849 rs_ctx->ma->state = ecp_rsma_mul2; in mbedtls_ecp_muladd_restartable()
2863 if (rs_ctx != NULL && rs_ctx->ma != NULL) { in mbedtls_ecp_muladd_restartable()
2864 rs_ctx->ma->state = ecp_rsma_add; in mbedtls_ecp_muladd_restartable()
2872 if (rs_ctx != NULL && rs_ctx->ma != NULL) { in mbedtls_ecp_muladd_restartable()
2873 rs_ctx->ma->state = ecp_rsma_norm; in mbedtls_ecp_muladd_restartable()
2882 if (rs_ctx != NULL && rs_ctx->ma != NULL) { in mbedtls_ecp_muladd_restartable()
2906 * NOT constant-time
2923 * Constants for the two points other than 0, 1, -1 (mod p) in
2946 * Check that the input point is not one of the low-order points.
2967 * these are 0, 1 and -1. For Curve25519 we check the values less than P in ecp_check_bad_points_mx()
3006 * Check validity of a public key for Montgomery curves with x-only schemes
3013 if (mbedtls_mpi_size(&pt->X) > (grp->nbits + 7) / 8) { in ecp_check_pubkey_mx()
3018 * X must be non-negative. This is normally ensured by the way it's in ecp_check_pubkey_mx()
3020 if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0) { in ecp_check_pubkey_mx()
3024 return ecp_check_bad_points_mx(&pt->X, &grp->P, grp->id); in ecp_check_pubkey_mx()
3035 if (mbedtls_mpi_cmp_int(&pt->Z, 1) != 0) { in mbedtls_ecp_check_pubkey()
3063 mbedtls_mpi_bitlen(d) != grp->nbits + 1) { /* mbedtls_mpi_bitlen is one-based! */ in mbedtls_ecp_check_privkey()
3068 if (grp->nbits == 254 && mbedtls_mpi_get_bit(d, 2) != 0) { in mbedtls_ecp_check_privkey()
3079 mbedtls_mpi_cmp_mpi(d, &grp->N) >= 0) { in mbedtls_ecp_check_privkey()
3101 /* Generate a (high_bit+1)-bit random number by generating just enough in mbedtls_ecp_gen_privkey_mx()
3106 MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(d, 8 * n_random_bytes - high_bit - 1)); in mbedtls_ecp_gen_privkey_mx()
3148 return mbedtls_ecp_gen_privkey_mx(grp->nbits, d, f_rng, p_rng); in mbedtls_ecp_gen_privkey()
3154 return mbedtls_ecp_gen_privkey_sw(&grp->N, d, f_rng, p_rng); in mbedtls_ecp_gen_privkey()
3187 return mbedtls_ecp_gen_keypair_base(grp, &grp->G, d, Q, f_rng, p_rng); in mbedtls_ecp_gen_keypair()
3197 if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) { in mbedtls_ecp_gen_key()
3201 return mbedtls_ecp_gen_keypair(&key->grp, &key->d, &key->Q, f_rng, p_rng); in mbedtls_ecp_gen_key()
3211 if (key->grp.id == MBEDTLS_ECP_DP_NONE) { in mbedtls_ecp_set_public_key()
3213 if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) { in mbedtls_ecp_set_public_key()
3216 } else if (key->grp.id != grp_id) { in mbedtls_ecp_set_public_key()
3220 return mbedtls_ecp_copy(&key->Q, Q); in mbedtls_ecp_set_public_key()
3234 if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) { in mbedtls_ecp_read_key()
3241 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { in mbedtls_ecp_read_key()
3250 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&key->d, buf, buflen)); in mbedtls_ecp_read_key()
3253 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 0, 0)); in mbedtls_ecp_read_key()
3254 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 1, 0)); in mbedtls_ecp_read_key()
3255 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 2, 0)); in mbedtls_ecp_read_key()
3259 mbedtls_mpi_set_bit(&key->d, in mbedtls_ecp_read_key()
3260 ECP_CURVE25519_KEY_SIZE * 8 - 1, 0) in mbedtls_ecp_read_key()
3265 mbedtls_mpi_set_bit(&key->d, in mbedtls_ecp_read_key()
3266 ECP_CURVE25519_KEY_SIZE * 8 - 2, 1) in mbedtls_ecp_read_key()
3273 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&key->d, buf, buflen)); in mbedtls_ecp_read_key()
3276 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 0, 0)); in mbedtls_ecp_read_key()
3277 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 1, 0)); in mbedtls_ecp_read_key()
3281 mbedtls_mpi_set_bit(&key->d, in mbedtls_ecp_read_key()
3282 ECP_CURVE448_KEY_SIZE * 8 - 1, 1) in mbedtls_ecp_read_key()
3288 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { in mbedtls_ecp_read_key()
3289 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&key->d, buf, buflen)); in mbedtls_ecp_read_key()
3294 MBEDTLS_MPI_CHK(mbedtls_ecp_check_privkey(&key->grp, &key->d)); in mbedtls_ecp_read_key()
3300 mbedtls_mpi_free(&key->d); in mbedtls_ecp_read_key()
3316 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { in mbedtls_ecp_write_key()
3317 if (key->grp.id == MBEDTLS_ECP_DP_CURVE25519) { in mbedtls_ecp_write_key()
3322 } else if (key->grp.id == MBEDTLS_ECP_DP_CURVE448) { in mbedtls_ecp_write_key()
3327 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary_le(&key->d, buf, buflen)); in mbedtls_ecp_write_key()
3331 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { in mbedtls_ecp_write_key()
3332 MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&key->d, buf, buflen)); in mbedtls_ecp_write_key()
3345 size_t len = (key->grp.nbits + 7) / 8; in mbedtls_ecp_write_key_ext()
3354 if (key->d.n == 0) { in mbedtls_ecp_write_key_ext()
3359 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { in mbedtls_ecp_write_key_ext()
3360 return mbedtls_mpi_write_binary_le(&key->d, buf, len); in mbedtls_ecp_write_key_ext()
3365 if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { in mbedtls_ecp_write_key_ext()
3366 return mbedtls_mpi_write_binary(&key->d, buf, len); in mbedtls_ecp_write_key_ext()
3381 return mbedtls_ecp_point_write_binary(&key->grp, &key->Q, in mbedtls_ecp_write_public_key()
3388 * Check a public-private key pair
3397 if (pub->grp.id == MBEDTLS_ECP_DP_NONE || in mbedtls_ecp_check_pub_priv()
3398 pub->grp.id != prv->grp.id || in mbedtls_ecp_check_pub_priv()
3399 mbedtls_mpi_cmp_mpi(&pub->Q.X, &prv->Q.X) || in mbedtls_ecp_check_pub_priv()
3400 mbedtls_mpi_cmp_mpi(&pub->Q.Y, &prv->Q.Y) || in mbedtls_ecp_check_pub_priv()
3401 mbedtls_mpi_cmp_mpi(&pub->Q.Z, &prv->Q.Z)) { in mbedtls_ecp_check_pub_priv()
3408 /* mbedtls_ecp_mul() needs a non-const group... */ in mbedtls_ecp_check_pub_priv()
3409 mbedtls_ecp_group_copy(&grp, &prv->grp); in mbedtls_ecp_check_pub_priv()
3412 MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &Q, &prv->d, &prv->grp.G, f_rng, p_rng)); in mbedtls_ecp_check_pub_priv()
3414 if (mbedtls_mpi_cmp_mpi(&Q.X, &prv->Q.X) || in mbedtls_ecp_check_pub_priv()
3415 mbedtls_mpi_cmp_mpi(&Q.Y, &prv->Q.Y) || in mbedtls_ecp_check_pub_priv()
3416 mbedtls_mpi_cmp_mpi(&Q.Z, &prv->Q.Z)) { in mbedtls_ecp_check_pub_priv()
3432 return mbedtls_ecp_mul(&key->grp, &key->Q, &key->d, &key->grp.G, in mbedtls_ecp_keypair_calc_public()
3440 return key->grp.id; in mbedtls_ecp_keypair_get_group_id()
3444 * Export generic key-pair parameters.
3451 if (grp != NULL && (ret = mbedtls_ecp_group_copy(grp, &key->grp)) != 0) { in mbedtls_ecp_export()
3455 if (d != NULL && (ret = mbedtls_mpi_copy(d, &key->d)) != 0) { in mbedtls_ecp_export()
3459 if (Q != NULL && (ret = mbedtls_ecp_copy(Q, &key->Q)) != 0) { in mbedtls_ecp_export()
3470 * PRNG for test - !!!INSECURE NEVER USE IN PRODUCTION!!!
3497 switch (grp->id) { in self_test_adjust_exponent()
3503 /* Move highest bit from 254 to N-1. Setting bit N-1 is in self_test_adjust_exponent()
3504 * necessary to enforce the highest-bit-set constraint. */ in self_test_adjust_exponent()
3506 MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(m, grp->nbits, 1)); in self_test_adjust_exponent()
3507 /* Copy second-highest bit from 253 to N-2. This is not in self_test_adjust_exponent()
3510 mbedtls_mpi_set_bit(m, grp->nbits - 1, in self_test_adjust_exponent()
3516 /* Non-Montgomery curves and Curve25519 need no adjustment. */ in self_test_adjust_exponent()
3596 "FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8C", /* n - 1 */ in mbedtls_ecp_self_test()
3628 MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, mbedtls_ecp_curve_list()->grp_id)); in mbedtls_ecp_self_test()
3670 #error "MBEDTLS_ECP_MONTGOMERY_ENABLED is defined, but no curve is supported for self-test" in mbedtls_ecp_self_test()