Lines Matching refs:ctx

88 static void point_resize(MPI_POINT p, struct mpi_ec_ctx *ctx)
90 size_t nlimbs = ctx->p->nlimbs;
97 if (ctx->model != MPI_EC_MONTGOMERY) {
104 struct mpi_ec_ctx *ctx)
107 if (ctx->model != MPI_EC_MONTGOMERY)
122 static void ec_addm(MPI w, MPI u, MPI v, struct mpi_ec_ctx *ctx)
125 ec_mod(w, ctx);
136 static void ec_mulm(MPI w, MPI u, MPI v, struct mpi_ec_ctx *ctx)
139 ec_mod(w, ctx);
143 static void ec_mul2(MPI w, MPI u, struct mpi_ec_ctx *ctx)
146 ec_mod(w, ctx);
150 struct mpi_ec_ctx *ctx)
152 mpi_powm(w, b, e, ctx->p);
157 * ec_powm(B, B, mpi_const(MPI_C_TWO), ctx);
160 static void ec_pow2(MPI w, const MPI b, struct mpi_ec_ctx *ctx)
163 /* mpi_powm(w, b, mpi_const(MPI_C_TWO), ctx->p); */
164 ec_mulm(w, b, b, ctx);
168 * ec_powm(B, B, mpi_const(MPI_C_THREE), ctx);
171 static void ec_pow3(MPI w, const MPI b, struct mpi_ec_ctx *ctx)
173 mpi_powm(w, b, mpi_const(MPI_C_THREE), ctx->p);
176 static void ec_invm(MPI x, MPI a, struct mpi_ec_ctx *ctx)
178 if (!mpi_invm(x, a, ctx->p))
199 static void ec_addm_25519(MPI w, MPI u, MPI v, struct mpi_ec_ctx *ctx)
215 borrow = mpihelp_sub_n(wp, wp, ctx->p->d, wsize);
216 mpih_set_cond(n, ctx->p->d, wsize, (borrow != 0UL));
221 static void ec_subm_25519(MPI w, MPI u, MPI v, struct mpi_ec_ctx *ctx)
237 mpih_set_cond(n, ctx->p->d, wsize, (borrow != 0UL));
242 static void ec_mulm_25519(MPI w, MPI u, MPI v, struct mpi_ec_ctx *ctx)
251 (void)ctx;
286 cy = mpihelp_sub_n(wp, wp, ctx->p->d, wsize);
287 mpih_set_cond(m, ctx->p->d, wsize, (cy != 0UL));
291 static void ec_mul2_25519(MPI w, MPI u, struct mpi_ec_ctx *ctx)
293 ec_addm_25519(w, u, u, ctx);
296 static void ec_pow2_25519(MPI w, const MPI b, struct mpi_ec_ctx *ctx)
298 ec_mulm_25519(w, b, b, ctx);
306 static void ec_addm_448(MPI w, MPI u, MPI v, struct mpi_ec_ctx *ctx)
322 mpih_set_cond(n, ctx->p->d, wsize, (cy != 0UL));
326 static void ec_subm_448(MPI w, MPI u, MPI v, struct mpi_ec_ctx *ctx)
342 mpih_set_cond(n, ctx->p->d, wsize, (borrow != 0UL));
346 static void ec_mulm_448(MPI w, MPI u, MPI v, struct mpi_ec_ctx *ctx)
438 cy = mpihelp_sub_n(wp, wp, ctx->p->d, wsize);
439 mpih_set_cond(n, ctx->p->d, wsize, (cy != 0UL));
443 static void ec_mul2_448(MPI w, MPI u, struct mpi_ec_ctx *ctx)
445 ec_addm_448(w, u, u, ctx);
448 static void ec_pow2_448(MPI w, const MPI b, struct mpi_ec_ctx *ctx)
450 ec_mulm_448(w, b, b, ctx);
457 void (*addm)(MPI w, MPI u, MPI v, struct mpi_ec_ctx *ctx);
458 void (*subm)(MPI w, MPI u, MPI v, struct mpi_ec_ctx *ctx);
459 void (*mulm)(MPI w, MPI u, MPI v, struct mpi_ec_ctx *ctx);
460 void (*mul2)(MPI w, MPI u, struct mpi_ec_ctx *ctx);
461 void (*pow2)(MPI w, const MPI b, struct mpi_ec_ctx *ctx);
564 void mpi_ec_init(struct mpi_ec_ctx *ctx, enum gcry_mpi_ec_models model,
576 ctx->model = model;
577 ctx->dialect = dialect;
578 ctx->flags = flags;
580 ctx->nbits = 256;
582 ctx->nbits = mpi_get_nbits(p);
583 ctx->p = mpi_copy(p);
584 ctx->a = mpi_copy(a);
585 ctx->b = mpi_copy(b);
587 ctx->d = NULL;
588 ctx->t.two_inv_p = NULL;
590 ctx->t.p_barrett = use_barrett > 0 ? mpi_barrett_init(ctx->p, 0) : NULL;
592 mpi_ec_get_reset(ctx);
597 int match_p = !mpi_cmp(ctx->p, p_candidate);
604 for (j = 0; i < DIM(ctx->t.scratch) && bad_points_table[i][j]; j++)
605 ctx->t.scratch[j] = mpi_scanval(bad_points_table[i][j]);
609 for (i = 0; i < DIM(ctx->t.scratch); i++)
610 ctx->t.scratch[i] = mpi_alloc_like(ctx->p);
613 ctx->addm = ec_addm;
614 ctx->subm = ec_subm;
615 ctx->mulm = ec_mulm;
616 ctx->mul2 = ec_mul2;
617 ctx->pow2 = ec_pow2;
627 ctx->addm = field_table[i].addm;
628 ctx->subm = field_table[i].subm;
629 ctx->mulm = field_table[i].mulm;
630 ctx->mul2 = field_table[i].mul2;
631 ctx->pow2 = field_table[i].pow2;
634 mpi_resize(ctx->a, ctx->p->nlimbs);
635 ctx->a->nlimbs = ctx->p->nlimbs;
637 mpi_resize(ctx->b, ctx->p->nlimbs);
638 ctx->b->nlimbs = ctx->p->nlimbs;
640 for (i = 0; i < DIM(ctx->t.scratch) && ctx->t.scratch[i]; i++)
641 ctx->t.scratch[i]->nlimbs = ctx->p->nlimbs;
651 void mpi_ec_deinit(struct mpi_ec_ctx *ctx)
655 mpi_barrett_free(ctx->t.p_barrett);
658 mpi_free(ctx->p);
659 mpi_free(ctx->a);
660 mpi_free(ctx->b);
661 mpi_point_release(ctx->G);
662 mpi_free(ctx->n);
665 mpi_point_release(ctx->Q);
666 mpi_free(ctx->d);
669 mpi_free(ctx->t.two_inv_p);
671 for (i = 0; i < DIM(ctx->t.scratch); i++)
672 mpi_free(ctx->t.scratch[i]);
681 int mpi_ec_get_affine(MPI x, MPI y, MPI_POINT point, struct mpi_ec_ctx *ctx)
686 switch (ctx->model) {
693 ec_invm(z1, point->z, ctx); /* z1 = z^(-1) mod p */
694 ec_mulm(z2, z1, z1, ctx); /* z2 = z^(-2) mod p */
697 ec_mulm(x, point->x, z2, ctx);
701 ec_mulm(z3, z2, z1, ctx); /* z3 = z^(-3) mod p */
702 ec_mulm(y, point->y, z3, ctx);
729 ec_invm(z, point->z, ctx);
731 mpi_resize(z, ctx->p->nlimbs);
732 z->nlimbs = ctx->p->nlimbs;
735 mpi_resize(x, ctx->p->nlimbs);
736 x->nlimbs = ctx->p->nlimbs;
737 ctx->mulm(x, point->x, z, ctx);
740 mpi_resize(y, ctx->p->nlimbs);
741 y->nlimbs = ctx->p->nlimbs;
742 ctx->mulm(y, point->y, z, ctx);
757 MPI_POINT point, struct mpi_ec_ctx *ctx)
762 #define t1 (ctx->t.scratch[0])
763 #define t2 (ctx->t.scratch[1])
764 #define t3 (ctx->t.scratch[2])
765 #define l1 (ctx->t.scratch[3])
766 #define l2 (ctx->t.scratch[4])
767 #define l3 (ctx->t.scratch[5])
775 if (ec_get_a_is_pminus3(ctx)) {
780 ec_pow2(t1, point->z, ctx);
781 ec_subm(l1, point->x, t1, ctx);
782 ec_mulm(l1, l1, mpi_const(MPI_C_THREE), ctx);
783 ec_addm(t2, point->x, t1, ctx);
784 ec_mulm(l1, l1, t2, ctx);
789 ec_pow2(l1, point->x, ctx);
790 ec_mulm(l1, l1, mpi_const(MPI_C_THREE), ctx);
791 ec_powm(t1, point->z, mpi_const(MPI_C_FOUR), ctx);
792 ec_mulm(t1, t1, ctx->a, ctx);
793 ec_addm(l1, l1, t1, ctx);
796 ec_mulm(z3, point->y, point->z, ctx);
797 ec_mul2(z3, z3, ctx);
801 ec_pow2(t2, point->y, ctx);
802 ec_mulm(l2, t2, point->x, ctx);
803 ec_mulm(l2, l2, mpi_const(MPI_C_FOUR), ctx);
807 ec_pow2(x3, l1, ctx);
808 ec_mul2(t1, l2, ctx);
809 ec_subm(x3, x3, t1, ctx);
813 ec_pow2(t2, t2, ctx);
814 ec_mulm(l3, t2, mpi_const(MPI_C_EIGHT), ctx);
817 ec_subm(y3, l2, x3, ctx);
818 ec_mulm(y3, y3, l1, ctx);
819 ec_subm(y3, y3, l3, ctx);
835 MPI_POINT point, struct mpi_ec_ctx *ctx)
839 (void)ctx;
846 MPI_POINT point, struct mpi_ec_ctx *ctx)
854 #define B (ctx->t.scratch[0])
855 #define C (ctx->t.scratch[1])
856 #define D (ctx->t.scratch[2])
857 #define E (ctx->t.scratch[3])
858 #define F (ctx->t.scratch[4])
859 #define H (ctx->t.scratch[5])
860 #define J (ctx->t.scratch[6])
865 ctx->addm(B, X1, Y1, ctx);
866 ctx->pow2(B, B, ctx);
870 ctx->pow2(C, X1, ctx);
871 ctx->pow2(D, Y1, ctx);
874 if (ctx->dialect == ECC_DIALECT_ED25519)
875 ctx->subm(E, ctx->p, C, ctx);
877 ctx->mulm(E, ctx->a, C, ctx);
880 ctx->addm(F, E, D, ctx);
883 ctx->pow2(H, Z1, ctx);
886 ctx->mul2(J, H, ctx);
887 ctx->subm(J, F, J, ctx);
890 ctx->subm(X3, B, C, ctx);
891 ctx->subm(X3, X3, D, ctx);
892 ctx->mulm(X3, X3, J, ctx);
895 ctx->subm(Y3, E, D, ctx);
896 ctx->mulm(Y3, Y3, F, ctx);
899 ctx->mulm(Z3, F, J, ctx);
918 mpi_ec_dup_point(MPI_POINT result, MPI_POINT point, struct mpi_ec_ctx *ctx)
920 switch (ctx->model) {
922 dup_point_weierstrass(result, point, ctx);
925 dup_point_montgomery(result, point, ctx);
928 dup_point_edwards(result, point, ctx);
936 struct mpi_ec_ctx *ctx)
947 #define l1 (ctx->t.scratch[0])
948 #define l2 (ctx->t.scratch[1])
949 #define l3 (ctx->t.scratch[2])
950 #define l4 (ctx->t.scratch[3])
951 #define l5 (ctx->t.scratch[4])
952 #define l6 (ctx->t.scratch[5])
953 #define l7 (ctx->t.scratch[6])
954 #define l8 (ctx->t.scratch[7])
955 #define l9 (ctx->t.scratch[8])
956 #define t1 (ctx->t.scratch[9])
957 #define t2 (ctx->t.scratch[10])
961 mpi_ec_dup_point(result, p1, ctx);
981 ec_pow2(l1, z2, ctx);
982 ec_mulm(l1, l1, x1, ctx);
987 ec_pow2(l2, z1, ctx);
988 ec_mulm(l2, l2, x2, ctx);
991 ec_subm(l3, l1, l2, ctx);
993 ec_powm(l4, z2, mpi_const(MPI_C_THREE), ctx);
994 ec_mulm(l4, l4, y1, ctx);
996 ec_powm(l5, z1, mpi_const(MPI_C_THREE), ctx);
997 ec_mulm(l5, l5, y2, ctx);
999 ec_subm(l6, l4, l5, ctx);
1004 mpi_ec_dup_point(result, p1, ctx);
1013 ec_addm(l7, l1, l2, ctx);
1015 ec_addm(l8, l4, l5, ctx);
1017 ec_mulm(z3, z1, z2, ctx);
1018 ec_mulm(z3, z3, l3, ctx);
1020 ec_pow2(t1, l6, ctx);
1021 ec_pow2(t2, l3, ctx);
1022 ec_mulm(t2, t2, l7, ctx);
1023 ec_subm(x3, t1, t2, ctx);
1025 ec_mul2(t1, x3, ctx);
1026 ec_subm(l9, t2, t1, ctx);
1028 ec_mulm(l9, l9, l6, ctx);
1029 ec_powm(t1, l3, mpi_const(MPI_C_THREE), ctx); /* fixme: Use saved value*/
1030 ec_mulm(t1, t1, l8, ctx);
1031 ec_subm(y3, l9, t1, ctx);
1032 ec_mulm(y3, y3, ec_get_two_inv_p(ctx), ctx);
1061 struct mpi_ec_ctx *ctx)
1066 (void)ctx;
1074 struct mpi_ec_ctx *ctx)
1085 #define A (ctx->t.scratch[0])
1086 #define B (ctx->t.scratch[1])
1087 #define C (ctx->t.scratch[2])
1088 #define D (ctx->t.scratch[3])
1089 #define E (ctx->t.scratch[4])
1090 #define F (ctx->t.scratch[5])
1091 #define G (ctx->t.scratch[6])
1092 #define tmp (ctx->t.scratch[7])
1094 point_resize(result, ctx);
1099 ctx->mulm(A, Z1, Z2, ctx);
1102 ctx->pow2(B, A, ctx);
1105 ctx->mulm(C, X1, X2, ctx);
1108 ctx->mulm(D, Y1, Y2, ctx);
1111 ctx->mulm(E, ctx->b, C, ctx);
1112 ctx->mulm(E, E, D, ctx);
1115 ctx->subm(F, B, E, ctx);
1118 ctx->addm(G, B, E, ctx);
1121 ctx->addm(tmp, X1, Y1, ctx);
1122 ctx->addm(X3, X2, Y2, ctx);
1123 ctx->mulm(X3, X3, tmp, ctx);
1124 ctx->subm(X3, X3, C, ctx);
1125 ctx->subm(X3, X3, D, ctx);
1126 ctx->mulm(X3, X3, F, ctx);
1127 ctx->mulm(X3, X3, A, ctx);
1130 if (ctx->dialect == ECC_DIALECT_ED25519) {
1131 ctx->addm(Y3, D, C, ctx);
1133 ctx->mulm(Y3, ctx->a, C, ctx);
1134 ctx->subm(Y3, D, Y3, ctx);
1136 ctx->mulm(Y3, Y3, G, ctx);
1137 ctx->mulm(Y3, Y3, A, ctx);
1140 ctx->mulm(Z3, F, G, ctx);
1168 struct mpi_ec_ctx *ctx)
1170 ctx->addm(sum->x, p2->x, p2->z, ctx);
1171 ctx->subm(p2->z, p2->x, p2->z, ctx);
1172 ctx->addm(prd->x, p1->x, p1->z, ctx);
1173 ctx->subm(p1->z, p1->x, p1->z, ctx);
1174 ctx->mulm(p2->x, p1->z, sum->x, ctx);
1175 ctx->mulm(p2->z, prd->x, p2->z, ctx);
1176 ctx->pow2(p1->x, prd->x, ctx);
1177 ctx->pow2(p1->z, p1->z, ctx);
1178 ctx->addm(sum->x, p2->x, p2->z, ctx);
1179 ctx->subm(p2->z, p2->x, p2->z, ctx);
1180 ctx->mulm(prd->x, p1->x, p1->z, ctx);
1181 ctx->subm(p1->z, p1->x, p1->z, ctx);
1182 ctx->pow2(sum->x, sum->x, ctx);
1183 ctx->pow2(sum->z, p2->z, ctx);
1184 ctx->mulm(prd->z, p1->z, ctx->a, ctx); /* CTX->A: (a-2)/4 */
1185 ctx->mulm(sum->z, sum->z, dif_x, ctx);
1186 ctx->addm(prd->z, p1->x, prd->z, ctx);
1187 ctx->mulm(prd->z, prd->z, p1->z, ctx);
1193 struct mpi_ec_ctx *ctx)
1195 switch (ctx->model) {
1197 add_points_weierstrass(result, p1, p2, ctx);
1200 add_points_montgomery(result, p1, p2, ctx);
1203 add_points_edwards(result, p1, p2, ctx);
1215 struct mpi_ec_ctx *ctx)
1221 if (ctx->model == MPI_EC_EDWARDS) {
1232 if (mpi_cmp(scalar, ctx->p) >= 0)
1235 nbits = mpi_get_nbits(ctx->p);
1240 point_resize(point, ctx);
1242 point_resize(result, ctx);
1243 point_resize(point, ctx);
1246 mpi_ec_dup_point(result, result, ctx);
1248 mpi_ec_add_points(result, result, point, ctx);
1251 } else if (ctx->model == MPI_EC_MONTGOMERY) {
1274 point_resize(&p1, ctx);
1275 point_resize(&p2, ctx);
1276 point_resize(&p1_, ctx);
1277 point_resize(&p2_, ctx);
1279 mpi_resize(point->x, ctx->p->nlimbs);
1280 point->x->nlimbs = ctx->p->nlimbs;
1291 point_swap_cond(q1, q2, sw, ctx);
1292 montgomery_ladder(prd, sum, q1, q2, point->x, ctx);
1293 point_swap_cond(prd, sum, sw, ctx);
1300 point_swap_cond(&p1, &p1_, sw, ctx);
1309 ec_invm(z1, p1.z, ctx);
1310 ec_mulm(result->x, p1.x, z1, ctx);
1322 x1 = mpi_alloc_like(ctx->p);
1323 y1 = mpi_alloc_like(ctx->p);
1324 h = mpi_alloc_like(ctx->p);
1330 ec_invm(yy, yy, ctx);
1339 z2 = mpi_alloc_like(ctx->p);
1340 z3 = mpi_alloc_like(ctx->p);
1341 ec_mulm(z2, point->z, point->z, ctx);
1342 ec_mulm(z3, point->z, z2, ctx);
1343 ec_invm(z2, z2, ctx);
1344 ec_mulm(x1, point->x, z2, ctx);
1345 ec_invm(z3, z3, ctx);
1346 ec_mulm(y1, yy, z3, ctx);
1378 ec_subm(p1inv.y, ctx->p, p1inv.y, ctx);
1381 mpi_ec_dup_point(result, result, ctx);
1384 mpi_ec_add_points(result, &p2, &p1, ctx);
1388 mpi_ec_add_points(result, &p2, &p1inv, ctx);
1401 int mpi_ec_curve_point(MPI_POINT point, struct mpi_ec_ctx *ctx)
1413 if (mpi_cmpabs(point->x, ctx->p) >= 0)
1415 if (mpi_cmpabs(point->y, ctx->p) >= 0)
1417 if (mpi_cmpabs(point->z, ctx->p) >= 0)
1420 switch (ctx->model) {
1425 if (mpi_ec_get_affine(x, y, point, ctx))
1431 ec_pow2(y, y, ctx);
1433 ec_pow3(xxx, x, ctx);
1434 ec_mulm(w, ctx->a, x, ctx);
1435 ec_addm(w, w, ctx->b, ctx);
1436 ec_addm(w, w, xxx, ctx);
1449 if (mpi_ec_get_affine(x, NULL, point, ctx))
1457 ec_mulm(w, ctx->a, mpi_const(MPI_C_FOUR), ctx);
1458 ec_addm(w, w, mpi_const(MPI_C_TWO), ctx);
1459 ec_mulm(w, w, x, ctx);
1460 ec_pow2(xx, x, ctx);
1461 ec_addm(w, w, xx, ctx);
1462 ec_addm(w, w, mpi_const(MPI_C_ONE), ctx);
1463 ec_mulm(w, w, x, ctx);
1464 ec_mulm(w, w, ctx->b, ctx);
1468 ec_subm(p_minus1, ctx->p, mpi_const(MPI_C_ONE), ctx);
1470 ec_powm(w, w, p_minus1, ctx);
1479 if (mpi_ec_get_affine(x, y, point, ctx))
1482 mpi_resize(w, ctx->p->nlimbs);
1483 w->nlimbs = ctx->p->nlimbs;
1486 ctx->pow2(x, x, ctx);
1487 ctx->pow2(y, y, ctx);
1488 if (ctx->dialect == ECC_DIALECT_ED25519)
1489 ctx->subm(w, ctx->p, x, ctx);
1491 ctx->mulm(w, ctx->a, x, ctx);
1492 ctx->addm(w, w, y, ctx);
1493 ctx->mulm(x, x, y, ctx);
1494 ctx->mulm(x, x, ctx->b, ctx);
1495 ctx->subm(w, w, x, ctx);