Lines Matching refs:group
88 void EC_pre_comp_free(EC_GROUP *group)
90 switch (group->pre_comp_type) {
95 EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
100 EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
103 EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
106 EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
115 EC_ec_pre_comp_free(group->pre_comp.ec);
118 group->pre_comp.ec = NULL;
121 void EC_GROUP_free(EC_GROUP *group)
123 if (!group)
126 if (group->meth->group_finish != 0)
127 group->meth->group_finish(group);
129 EC_pre_comp_free(group);
130 BN_MONT_CTX_free(group->mont_data);
131 EC_POINT_free(group->generator);
132 BN_free(group->order);
133 BN_free(group->cofactor);
134 OPENSSL_free(group->seed);
135 OPENSSL_free(group->propq);
136 OPENSSL_free(group);
140 void EC_GROUP_clear_free(EC_GROUP *group)
142 if (!group)
145 if (group->meth->group_clear_finish != 0)
146 group->meth->group_clear_finish(group);
147 else if (group->meth->group_finish != 0)
148 group->meth->group_finish(group);
150 EC_pre_comp_free(group);
151 BN_MONT_CTX_free(group->mont_data);
152 EC_POINT_clear_free(group->generator);
153 BN_clear_free(group->order);
154 BN_clear_free(group->cofactor);
155 OPENSSL_clear_free(group->seed, group->seed_len);
156 OPENSSL_clear_free(group, sizeof(*group));
289 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
291 return group->meth;
315 static int ec_guess_cofactor(EC_GROUP *group) {
324 if (BN_num_bits(group->order) <= (BN_num_bits(group->field) + 1) / 2 + 3) {
326 BN_zero(group->cofactor);
331 if ((ctx = BN_CTX_new_ex(group->libctx)) == NULL)
339 if (group->meth->field_type == NID_X9_62_characteristic_two_field) {
341 if (!BN_set_bit(q, BN_num_bits(group->field) - 1))
344 if (!BN_copy(q, group->field))
349 if (!BN_rshift1(group->cofactor, group->order) /* n/2 */
350 || !BN_add(group->cofactor, group->cofactor, q) /* q + n/2 */
352 || !BN_add(group->cofactor, group->cofactor, BN_value_one())
354 || !BN_div(group->cofactor, NULL, group->cofactor, group->order, ctx))
363 int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
371 /* require group->field >= 1 */
372 if (group->field == NULL || BN_is_zero(group->field)
373 || BN_is_negative(group->field)) {
384 || BN_num_bits(order) > BN_num_bits(group->field) + 1) {
399 if (group->generator == NULL) {
400 group->generator = EC_POINT_new(group);
401 if (group->generator == NULL)
404 if (!EC_POINT_copy(group->generator, generator))
407 if (!BN_copy(group->order, order))
412 if (!BN_copy(group->cofactor, cofactor))
414 } else if (!ec_guess_cofactor(group)) {
415 BN_zero(group->cofactor);
422 * |group->mont_data| will be NULL in this case.
424 if (BN_is_odd(group->order)) {
425 return ec_precompute_mont_data(group);
428 BN_MONT_CTX_free(group->mont_data);
429 group->mont_data = NULL;
433 const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
435 return group->generator;
438 BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
440 return group->mont_data;
443 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
445 if (group->order == NULL)
447 if (!BN_copy(order, group->order))
453 const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
455 return group->order;
458 int EC_GROUP_order_bits(const EC_GROUP *group)
460 return group->meth->group_order_bits(group);
463 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
467 if (group->cofactor == NULL)
469 if (!BN_copy(cofactor, group->cofactor))
472 return !BN_is_zero(group->cofactor);
475 const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
477 return group->cofactor;
480 void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
482 group->curve_name = nid;
483 group->asn1_flag =
489 int EC_GROUP_get_curve_name(const EC_GROUP *group)
491 return group->curve_name;
494 const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group)
496 return group->field;
499 int EC_GROUP_get_field_type(const EC_GROUP *group)
501 return group->meth->field_type;
504 void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
506 group->asn1_flag = flag;
509 int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
511 return group->asn1_flag;
514 void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
517 group->asn1_form = form;
521 *group)
523 return group->asn1_form;
526 size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
528 OPENSSL_free(group->seed);
529 group->seed = NULL;
530 group->seed_len = 0;
535 if ((group->seed = OPENSSL_malloc(len)) == NULL) {
539 memcpy(group->seed, p, len);
540 group->seed_len = len;
545 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
547 return group->seed;
550 size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
552 return group->seed_len;
555 int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
558 if (group->meth->group_set_curve == 0) {
562 return group->meth->group_set_curve(group, p, a, b, ctx);
565 int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
568 if (group->meth->group_get_curve == NULL) {
572 return group->meth->group_get_curve(group, p, a, b, ctx);
576 int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
579 return EC_GROUP_set_curve(group, p, a, b, ctx);
582 int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
585 return EC_GROUP_get_curve(group, p, a, b, ctx);
589 int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
592 return EC_GROUP_set_curve(group, p, a, b, ctx);
595 int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
598 return EC_GROUP_get_curve(group, p, a, b, ctx);
603 int EC_GROUP_get_degree(const EC_GROUP *group)
605 if (group->meth->group_get_degree == 0) {
609 return group->meth->group_get_degree(group);
612 int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
614 if (group->meth->group_check_discriminant == 0) {
618 return group->meth->group_check_discriminant(group, ctx);
715 EC_POINT *EC_POINT_new(const EC_GROUP *group)
719 if (group == NULL) {
723 if (group->meth->point_init == NULL) {
734 ret->meth = group->meth;
735 ret->curve_name = group->curve_name;
785 EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
793 t = EC_POINT_new(group);
811 int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
813 if (group->meth->point_set_to_infinity == 0) {
817 if (group->meth != point->meth) {
821 return group->meth->point_set_to_infinity(group, point);
825 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
830 if (group->meth->field_type != NID_X9_62_prime_field) {
834 if (!ec_point_is_compat(point, group)) {
838 return ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(group, point,
842 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
847 if (group->meth->field_type != NID_X9_62_prime_field) {
851 if (!ec_point_is_compat(point, group)) {
855 return ossl_ec_GFp_simple_get_Jprojective_coordinates_GFp(group, point,
860 int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
864 if (group->meth->point_set_affine_coordinates == NULL) {
868 if (!ec_point_is_compat(point, group)) {
872 if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
875 if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
883 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
887 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
891 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
895 return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
900 int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
904 if (group->meth->point_get_affine_coordinates == NULL) {
908 if (!ec_point_is_compat(point, group)) {
912 if (EC_POINT_is_at_infinity(group, point)) {
916 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
920 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
924 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
928 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
932 return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
937 int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
940 if (group->meth->add == 0) {
944 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)
945 || !ec_point_is_compat(b, group)) {
949 return group->meth->add(group, r, a, b, ctx);
952 int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
955 if (group->meth->dbl == 0) {
959 if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
963 return group->meth->dbl(group, r, a, ctx);
966 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
968 if (group->meth->invert == 0) {
972 if (!ec_point_is_compat(a, group)) {
976 return group->meth->invert(group, a, ctx);
979 int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
981 if (group->meth->is_at_infinity == 0) {
985 if (!ec_point_is_compat(point, group)) {
989 return group->meth->is_at_infinity(group, point);
999 int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
1002 if (group->meth->is_on_curve == 0) {
1006 if (!ec_point_is_compat(point, group)) {
1010 return group->meth->is_on_curve(group, point, ctx);
1013 int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
1016 if (group->meth->point_cmp == 0) {
1020 if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
1024 return group->meth->point_cmp(group, a, b, ctx);
1028 int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1030 if (group->meth->make_affine == 0) {
1034 if (!ec_point_is_compat(point, group)) {
1038 return group->meth->make_affine(group, point, ctx);
1041 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
1046 if (group->meth->points_make_affine == 0) {
1051 if (!ec_point_is_compat(points[i], group)) {
1056 return group->meth->points_make_affine(group, num, points, ctx);
1061 * Functions for point multiplication. If group->meth->mul is 0, we use the
1067 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
1077 if (!ec_point_is_compat(r, group)) {
1083 return EC_POINT_set_to_infinity(group, r);
1086 if (!ec_point_is_compat(points[i], group)) {
1101 if (group->meth->mul != NULL)
1102 ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1105 ret = ossl_ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
1114 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1123 if (!ec_point_is_compat(r, group)
1124 || (point != NULL && !ec_point_is_compat(point, group))) {
1130 return EC_POINT_set_to_infinity(group, r);
1142 if (group->meth->mul != NULL)
1143 ret = group->meth->mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
1146 ret = ossl_ec_wNAF_mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
1155 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
1157 if (group->meth->mul == 0)
1159 return ossl_ec_wNAF_precompute_mult(group, ctx);
1161 if (group->meth->precompute_mult != 0)
1162 return group->meth->precompute_mult(group, ctx);
1167 int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
1169 if (group->meth->mul == 0)
1171 return ossl_ec_wNAF_have_precompute_mult(group);
1173 if (group->meth->have_precompute_mult != 0)
1174 return group->meth->have_precompute_mult(group);
1182 * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
1185 static int ec_precompute_mont_data(EC_GROUP *group)
1187 BN_CTX *ctx = BN_CTX_new_ex(group->libctx);
1190 BN_MONT_CTX_free(group->mont_data);
1191 group->mont_data = NULL;
1196 group->mont_data = BN_MONT_CTX_new();
1197 if (group->mont_data == NULL)
1200 if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
1201 BN_MONT_CTX_free(group->mont_data);
1202 group->mont_data = NULL;
1226 int ossl_ec_group_simple_order_bits(const EC_GROUP *group)
1228 if (group->order == NULL)
1230 return BN_num_bits(group->order);
1233 static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,
1242 if (group->mont_data == NULL)
1262 if (!BN_sub(e, group->order, e))
1268 if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data))
1282 * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
1283 * - When group->order is even, this function returns an error.
1284 * - When group->order is otherwise composite, the correctness
1286 * - When x is outside the range [1, group->order), the correctness
1289 * range [1, group->order).
1294 int ossl_ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
1297 if (group->meth->field_inverse_mod_ord != NULL)
1298 return group->meth->field_inverse_mod_ord(group, res, x, ctx);
1300 return ec_field_inverse_mod_ord(group, res, x, ctx);
1313 int ossl_ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
1316 if (group->meth->blind_coordinates == NULL)
1319 return group->meth->blind_coordinates(group, p, ctx);
1322 int EC_GROUP_get_basis_type(const EC_GROUP *group)
1326 if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field)
1330 /* Find the last non-zero element of group->poly[] */
1332 i < (int)OSSL_NELEM(group->poly) && group->poly[i] != 0;
1346 int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
1348 if (group == NULL)
1351 if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field
1352 || !((group->poly[0] != 0) && (group->poly[1] != 0)
1353 && (group->poly[2] == 0))) {
1359 *k = group->poly[1];
1364 int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
1367 if (group == NULL)
1370 if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field
1371 || !((group->poly[0] != 0) && (group->poly[1] != 0)
1372 && (group->poly[2] != 0) && (group->poly[3] != 0)
1373 && (group->poly[4] == 0))) {
1379 *k1 = group->poly[3];
1381 *k2 = group->poly[2];
1383 *k3 = group->poly[1];
1391 * Check if the explicit parameters group matches any built-in curves.
1393 * We create a copy of the group just built, so that we can remove optional
1401 static EC_GROUP *ec_group_explicit_to_named(const EC_GROUP *group,
1409 const EC_POINT *point = EC_GROUP_get0_generator(group);
1410 const BIGNUM *order = EC_GROUP_get0_order(group);
1411 int no_seed = (EC_GROUP_get0_seed(group) == NULL);
1413 if ((dup = EC_GROUP_dup(group)) == NULL
1424 * parameters with one created from a named group.
1449 * sure it is not added to the returned group.
1461 ret_group = (EC_GROUP *)group;
1502 int ossl_ec_group_set_params(EC_GROUP *group, const OSSL_PARAM params[])
1513 EC_GROUP_set_point_conversion_form(group, format);
1522 EC_GROUP_set_asn1_flag(group, encoding_flag);
1529 || !EC_GROUP_set_seed(group, p->data, p->data_size)) {
1541 EC_GROUP *group = NULL;
1556 /* This is the simple named group case */
1561 if ((group = group_new_from_name(ptmp, libctx, propq)) == NULL)
1563 if (!ossl_ec_group_set_params(group, params)) {
1564 EC_GROUP_free(group);
1572 EC_GROUP_free(group);
1575 group->decoded_from_explicit_params = decoded > 0;
1576 return group;
1645 group = EC_GROUP_new_curve_GFp(p, a, b, bnctx);
1652 group = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
1653 if (group != NULL) {
1654 field_bits = EC_GROUP_get_degree(group);
1663 if (group == NULL) {
1675 if (!EC_GROUP_set_seed(group, ptmp->data, ptmp->data_size))
1687 if ((point = EC_POINT_new(group)) == NULL)
1689 EC_GROUP_set_point_conversion_form(group,
1691 if (!EC_POINT_oct2point(group, point, buf, ptmp->data_size, bnctx)) {
1716 if (!EC_GROUP_set_generator(group, point, order, cofactor)) {
1721 named_group = ec_group_explicit_to_named(group, libctx, propq, bnctx);
1726 if (named_group == group) {
1728 * If we did not find a named group then the encoding should be explicit
1741 EC_GROUP_set_asn1_flag(group, OPENSSL_EC_EXPLICIT_CURVE);
1743 EC_GROUP_free(group);
1744 group = named_group;
1746 /* We've imported the group from explicit parameters, set it so. */
1747 group->decoded_from_explicit_params = 1;
1751 EC_GROUP_free(group);
1752 group = NULL;
1758 return group;