65 #if defined( INC_ALL )
72 #pragma warning( disable: 4267 )
75 #if defined( USE_ECDH ) || defined( USE_ECDSA )
79 #define NID_X9_62_prime_field 406
80 #define NID_X9_62_characteristic_two_field 407
85 NID_X9_62_prime_field,
170 if (!
BN_copy(&dest->
a, &src->
a))
return 0;
171 if (!
BN_copy(&dest->
b, &src->
b))
return 0;
202 if (tmp_a == NULL)
goto err;
209 if (!
BN_nnmod(tmp_a, a, p, ctx))
goto err;
213 if (!
BN_copy(&group->
a, tmp_a))
goto err;
216 if (!
BN_nnmod(&group->
b, b, p, ctx))
goto err;
244 if (a != NULL || b != NULL)
267 if (!
BN_copy(a, &group->
a))
goto err;
271 if (!
BN_copy(b, &group->
b))
goto err;
294 BIGNUM *a,*b,*order,*tmp_1,*tmp_2;
313 if (order == NULL)
goto err;
322 if (!
BN_copy(a, &group->
a))
goto err;
323 if (!
BN_copy(b, &group->
b))
goto err;
336 if (!
BN_mod_mul(tmp_2, tmp_1, a, p, ctx))
goto err;
337 if (!
BN_lshift(tmp_1, tmp_2, 2))
goto err;
344 if (!
BN_mod_add(a, tmp_1, tmp_2, p, ctx))
goto err;
388 if (!
BN_copy(&dest->
X, &src->
X))
return 0;
389 if (!
BN_copy(&dest->
Y, &src->
Y))
return 0;
390 if (!
BN_copy(&dest->
Z, &src->
Z))
return 0;
497 if (!
BN_copy(x, &point->
X))
goto err;
501 if (!
BN_copy(y, &point->
Y))
goto err;
505 if (!
BN_copy(z, &point->
Z))
goto err;
521 if (x == NULL || y == NULL)
536 BIGNUM *Z, *Z_1, *Z_2, *Z_3;
558 if (Z_3 == NULL)
goto err;
589 if (!
BN_copy(x, &point->
X))
goto err;
593 if (!
BN_copy(y, &point->
Y))
goto err;
618 if (!group->
meth->
field_mul(group, x, &point->
X, Z_2, ctx))
goto err;
626 if (!group->
meth->
field_mul(group, Z_3, Z_2, Z_1, ctx))
goto err;
634 if (!group->
meth->
field_mul(group, y, &point->
Y, Z_3, ctx))
goto err;
652 BIGNUM *tmp1, *tmp2, *x, *y;
665 y_bit = (y_bit != 0);
672 if (y == NULL)
goto err;
685 if (!group->
meth->
field_mul(group, tmp1, tmp2, x_, ctx))
goto err;
710 if (!group->
meth->
field_mul(group, tmp2, &group->
a, x, ctx))
goto err;
746 if (kron == -2)
goto err;
776 unsigned char *buf,
size_t len,
BN_CTX *ctx)
782 size_t field_len, i, skip;
832 if (y == NULL)
goto err;
844 if (skip > field_len)
856 if (i != 1 + field_len)
865 if (skip > field_len)
902 const unsigned char *buf,
size_t len,
BN_CTX *ctx)
908 size_t field_len, enc_len;
962 if (y == NULL)
goto err;
964 if (!
BN_bin2bn(buf + 1, field_len, x))
goto err;
977 if (!
BN_bin2bn(buf + 1 + field_len, field_len, y))
goto err;
1005 if (new_ctx != NULL)
1014 int (*field_sqr)(
const EC_GROUP *, BIGNUM *,
const BIGNUM *,
BN_CTX *);
1017 BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;
1046 if (n6 == NULL)
goto end;
1063 if (!field_sqr(group, n0, &b->
Z, ctx))
goto end;
1064 if (!field_mul(group, n1, &a->
X, n0, ctx))
goto end;
1067 if (!field_mul(group, n0, n0, &b->
Z, ctx))
goto end;
1068 if (!field_mul(group, n2, &a->
Y, n0, ctx))
goto end;
1082 if (!field_sqr(group, n0, &a->
Z, ctx))
goto end;
1083 if (!field_mul(group, n3, &b->
X, n0, ctx))
goto end;
1086 if (!field_mul(group, n0, n0, &a->
Z, ctx))
goto end;
1087 if (!field_mul(group, n4, &b->
Y, n0, ctx))
goto end;
1131 {
if (!
BN_copy(n0, &b->
Z))
goto end; }
1133 {
if (!
BN_copy(n0, &a->
Z))
goto end; }
1135 {
if (!field_mul(group, n0, &a->
Z, &b->
Z, ctx))
goto end; }
1136 if (!field_mul(group, &r->
Z, n0, n5, ctx))
goto end;
1142 if (!field_sqr(group, n0, n6, ctx))
goto end;
1143 if (!field_sqr(group, n4, n5, ctx))
goto end;
1144 if (!field_mul(group, n3, n1, n4, ctx))
goto end;
1154 if (!field_mul(group, n0, n0, n6, ctx))
goto end;
1155 if (!field_mul(group, n5, n4, n5, ctx))
goto end;
1156 if (!field_mul(group, n1, n2, n5, ctx))
goto end;
1159 if (!
BN_add(n0, n0, p))
goto end;
1169 if (new_ctx != NULL)
1177 int (*field_mul)(
const EC_GROUP *, BIGNUM *,
const BIGNUM *,
const BIGNUM *,
BN_CTX *);
1178 int (*field_sqr)(
const EC_GROUP *, BIGNUM *,
const BIGNUM *,
BN_CTX *);
1181 BIGNUM *n0, *n1, *n2, *n3;
1207 if (n3 == NULL)
goto err;
1217 if (!field_sqr(group, n0, &a->
X, ctx))
goto err;
1225 if (!field_sqr(group, n1, &a->
Z, ctx))
goto err;
1228 if (!field_mul(group, n1, n0, n2, ctx))
goto err;
1236 if (!field_sqr(group, n0, &a->
X, ctx))
goto err;
1239 if (!field_sqr(group, n1, &a->
Z, ctx))
goto err;
1240 if (!field_sqr(group, n1, n1, ctx))
goto err;
1241 if (!field_mul(group, n1, n1, &group->
a, ctx))
goto err;
1253 if (!field_mul(group, n0, &a->
Y, &a->
Z, ctx))
goto err;
1260 if (!field_sqr(group, n3, &a->
Y, ctx))
goto err;
1261 if (!field_mul(group, n2, &a->
X, n3, ctx))
goto err;
1267 if (!field_sqr(group, &r->
X, n1, ctx))
goto err;
1272 if (!field_sqr(group, n0, n3, ctx))
goto err;
1278 if (!field_mul(group, n0, n1, n0, ctx))
goto err;
1286 if (new_ctx != NULL)
1310 int (*field_mul)(
const EC_GROUP *, BIGNUM *,
const BIGNUM *,
const BIGNUM *,
BN_CTX *);
1311 int (*field_sqr)(
const EC_GROUP *, BIGNUM *,
const BIGNUM *,
BN_CTX *);
1314 BIGNUM *rh, *tmp, *Z4, *Z6;
1336 if (Z6 == NULL)
goto err;
1348 if (!field_sqr(group, rh, &point->
X, ctx))
goto err;
1352 if (!field_sqr(group, tmp, &point->
Z, ctx))
goto err;
1353 if (!field_sqr(group, Z4, tmp, ctx))
goto err;
1354 if (!field_mul(group, Z6, Z4, tmp, ctx))
goto err;
1362 if (!field_mul(group, rh, rh, &point->
X, ctx))
goto err;
1366 if (!field_mul(group, tmp, Z4, &group->
a, ctx))
goto err;
1368 if (!field_mul(group, rh, rh, &point->
X, ctx))
goto err;
1372 if (!field_mul(group, tmp, &group->
b, Z6, ctx))
goto err;
1381 if (!field_mul(group, rh, rh, &point->
X, ctx))
goto err;
1387 if (!field_sqr(group, tmp, &point->
Y, ctx))
goto err;
1389 ret = (0 ==
BN_ucmp(tmp, rh));
1393 if (new_ctx != NULL)
1407 int (*field_mul)(
const EC_GROUP *, BIGNUM *,
const BIGNUM *,
const BIGNUM *,
BN_CTX *);
1408 int (*field_sqr)(
const EC_GROUP *, BIGNUM *,
const BIGNUM *,
BN_CTX *);
1410 BIGNUM *tmp1, *tmp2, *Za23, *Zb23;
1411 const BIGNUM *tmp1_, *tmp2_;
1439 if (Zb23 == NULL)
goto end;
1449 if (!field_sqr(group, Zb23, &b->
Z, ctx))
goto end;
1450 if (!field_mul(group, tmp1, &a->
X, Zb23, ctx))
goto end;
1457 if (!field_sqr(group, Za23, &a->
Z, ctx))
goto end;
1458 if (!field_mul(group, tmp2, &b->
X, Za23, ctx))
goto end;
1465 if (
BN_cmp(tmp1_, tmp2_) != 0)
1474 if (!field_mul(group, Zb23, Zb23, &b->
Z, ctx))
goto end;
1475 if (!field_mul(group, tmp1, &a->
Y, Zb23, ctx))
goto end;
1482 if (!field_mul(group, Za23, Za23, &a->
Z, ctx))
goto end;
1483 if (!field_mul(group, tmp2, &b->
Y, Za23, ctx))
goto end;
1490 if (
BN_cmp(tmp1_, tmp2_) != 0)
1501 if (new_ctx != NULL)
1526 if (y == NULL)
goto err;
1540 if (new_ctx != NULL)
1549 BIGNUM *tmp0, *tmp1;
1551 BIGNUM **heap = NULL;
1568 if (tmp0 == NULL || tmp1 == NULL)
goto err;
1582 heap =
clBnAlloc(
"ec_GFp_simple_points_make_affine", pow2 *
sizeof heap[0]);
1583 if (heap == NULL)
goto err;
1600 for (i = pow2/2 - 1; i > 0; i--)
1602 for (i = 0; i < num; i++)
1603 heap[pow2/2 + i] = &points[i]->Z;
1604 for (i = pow2/2 + num; i < pow2; i++)
1608 for (i = pow2/2 - 1; i > 0; i--)
1611 if (heap[i] == NULL)
goto err;
1613 if (heap[2*i] != NULL)
1615 if ((heap[2*i + 1] == NULL) ||
BN_is_zero(heap[2*i + 1]))
1617 if (!
BN_copy(heap[i], heap[2*i]))
goto err;
1623 if (!
BN_copy(heap[i], heap[2*i + 1]))
goto err;
1628 heap[2*i], heap[2*i + 1], ctx))
goto err;
1653 for (i = 2; i < pow2/2 + num; i += 2)
1656 if ((heap[i + 1] != NULL) && !
BN_is_zero(heap[i + 1]))
1658 if (!group->
meth->
field_mul(group, tmp0, heap[i/2], heap[i + 1], ctx))
goto err;
1659 if (!group->
meth->
field_mul(group, tmp1, heap[i/2], heap[i], ctx))
goto err;
1660 if (!
BN_copy(heap[i], tmp0))
goto err;
1661 if (!
BN_copy(heap[i + 1], tmp1))
goto err;
1665 if (!
BN_copy(heap[i], heap[i/2]))
goto err;
1670 for (i = 0; i < num; i++)
1679 if (!group->
meth->
field_mul(group, &p->
X, &p->
X, tmp1, ctx))
goto err;
1681 if (!group->
meth->
field_mul(group, tmp1, tmp1, &p->
Z, ctx))
goto err;
1682 if (!group->
meth->
field_mul(group, &p->
Y, &p->
Y, tmp1, ctx))
goto err;
1700 if (new_ctx != NULL)
1705 for (i = pow2/2 - 1; i > 0; i--)
1707 if (heap[i] != NULL)