- {
- if (group->meth->group_get_order == 0)
- {
- ECerr(EC_F_EC_GROUP_GET_ORDER, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
- return 0;
- }
- return group->meth->group_get_order(group, order, ctx);
- }
-
-
-int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, BN_CTX *ctx)
- {
- if (group->meth->group_get_cofactor == 0)
- {
- ECerr(EC_F_EC_GROUP_GET_COFACTOR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
- return 0;
- }
- return group->meth->group_get_cofactor(group, cofactor, ctx);
- }
-
-
-int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx)
- {
- if (group->meth->group_check == 0)
- {
- ECerr(EC_F_EC_GROUP_CHECK, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
- return 0;
- }
- return group->meth->group_check(group, ctx);
- }
-
-
-void EC_GROUP_set_nid(EC_GROUP *group, int nid)
- {
- group->nid = nid;
- }
-
-
-int EC_GROUP_get_nid(const EC_GROUP *group)
- {
- return group->nid;
- }
-
-
-/* this has 'package' visibility */
-int EC_GROUP_set_extra_data(EC_GROUP *group, void *extra_data, void *(*extra_data_dup_func)(void *),
- void (*extra_data_free_func)(void *), void (*extra_data_clear_free_func)(void *))
- {
- if ((group->extra_data != NULL)
- || (group->extra_data_dup_func != 0)
- || (group->extra_data_free_func != 0)
- || (group->extra_data_clear_free_func != 0))
- {
- ECerr(EC_F_EC_GROUP_SET_EXTRA_DATA, EC_R_SLOT_FULL);
- return 0;
- }
-
- group->extra_data = extra_data;
- group->extra_data_dup_func = extra_data_dup_func;
- group->extra_data_free_func = extra_data_free_func;
- group->extra_data_clear_free_func = extra_data_clear_free_func;
- return 1;
- }
-
-
-/* this has 'package' visibility */
-void *EC_GROUP_get_extra_data(const EC_GROUP *group, void *(*extra_data_dup_func)(void *),
- void (*extra_data_free_func)(void *), void (*extra_data_clear_free_func)(void *))
- {
- if ((group->extra_data_dup_func != extra_data_dup_func)
- || (group->extra_data_free_func != extra_data_free_func)
- || (group->extra_data_clear_free_func != extra_data_clear_free_func))
- {
- ECerr(EC_F_EC_GROUP_GET_EXTRA_DATA, EC_R_NO_SUCH_EXTRA_DATA);
- return NULL;
- }
-
- return group->extra_data;
- }
-
-
-/* this has 'package' visibility */
-void EC_GROUP_free_extra_data(EC_GROUP *group)
- {
- if (group->extra_data_free_func)
- group->extra_data_free_func(group->extra_data);
- group->extra_data = NULL;
- group->extra_data_dup_func = 0;
- group->extra_data_free_func = 0;
- group->extra_data_clear_free_func = 0;
- }
-
-
-/* this has 'package' visibility */
-void EC_GROUP_clear_free_extra_data(EC_GROUP *group)
- {
- if (group->extra_data_clear_free_func)
- group->extra_data_clear_free_func(group->extra_data);
- else if (group->extra_data_free_func)
- group->extra_data_free_func(group->extra_data);
- group->extra_data = NULL;
- group->extra_data_dup_func = 0;
- group->extra_data_free_func = 0;
- group->extra_data_clear_free_func = 0;
- }
-
+{
+ if (group->order == NULL)
+ return 0;
+ if (!BN_copy(order, group->order))
+ return 0;
+
+ return !BN_is_zero(order);
+}
+
+const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
+{
+ return group->order;
+}
+
+int EC_GROUP_order_bits(const EC_GROUP *group)
+{
+ OPENSSL_assert(group->meth->group_order_bits != NULL);
+ return group->meth->group_order_bits(group);
+}
+
+int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
+ BN_CTX *ctx)
+{
+
+ if (group->cofactor == NULL)
+ return 0;
+ if (!BN_copy(cofactor, group->cofactor))
+ return 0;
+
+ return !BN_is_zero(group->cofactor);
+}
+
+const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
+{
+ return group->cofactor;
+}
+
+void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
+{
+ group->curve_name = nid;
+}
+
+int EC_GROUP_get_curve_name(const EC_GROUP *group)
+{
+ return group->curve_name;
+}
+
+void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
+{
+ group->asn1_flag = flag;
+}
+
+int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
+{
+ return group->asn1_flag;
+}
+
+void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
+ point_conversion_form_t form)
+{
+ group->asn1_form = form;
+}
+
+point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
+ *group)
+{
+ return group->asn1_form;
+}
+
+size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
+{
+ OPENSSL_free(group->seed);
+ group->seed = NULL;
+ group->seed_len = 0;
+
+ if (!len || !p)
+ return 1;
+
+ if ((group->seed = OPENSSL_malloc(len)) == NULL)
+ return 0;
+ memcpy(group->seed, p, len);
+ group->seed_len = len;
+
+ return len;
+}
+
+unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
+{
+ return group->seed;
+}
+
+size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
+{
+ return group->seed_len;
+}
+
+int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
+ const BIGNUM *b, BN_CTX *ctx)
+{
+ if (group->meth->group_set_curve == 0) {
+ ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
+ return 0;
+ }
+ return group->meth->group_set_curve(group, p, a, b, ctx);
+}
+
+int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
+ BIGNUM *b, BN_CTX *ctx)
+{
+ if (group->meth->group_get_curve == 0) {
+ ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
+ return 0;
+ }
+ return group->meth->group_get_curve(group, p, a, b, ctx);
+}
+
+#ifndef OPENSSL_NO_EC2M
+int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
+ const BIGNUM *b, BN_CTX *ctx)
+{
+ if (group->meth->group_set_curve == 0) {
+ ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M,
+ ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
+ return 0;
+ }
+ return group->meth->group_set_curve(group, p, a, b, ctx);
+}
+
+int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
+ BIGNUM *b, BN_CTX *ctx)
+{
+ if (group->meth->group_get_curve == 0) {
+ ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M,
+ ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
+ return 0;
+ }
+ return group->meth->group_get_curve(group, p, a, b, ctx);
+}
+#endif
+
+int EC_GROUP_get_degree(const EC_GROUP *group)
+{
+ if (group->meth->group_get_degree == 0) {
+ ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
+ return 0;
+ }
+ return group->meth->group_get_degree(group);
+}
+
+int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
+{
+ if (group->meth->group_check_discriminant == 0) {
+ ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
+ ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
+ return 0;
+ }
+ return group->meth->group_check_discriminant(group, ctx);
+}
+
+int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
+{
+ int r = 0;
+ BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
+ BN_CTX *ctx_new = NULL;
+
+ /* compare the field types */
+ if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
+ EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
+ return 1;
+ /* compare the curve name (if present in both) */
+ if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
+ EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
+ return 1;
+ if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
+ return 0;
+
+ if (ctx == NULL)
+ ctx_new = ctx = BN_CTX_new();
+ if (ctx == NULL)
+ return -1;
+
+ BN_CTX_start(ctx);
+ a1 = BN_CTX_get(ctx);
+ a2 = BN_CTX_get(ctx);
+ a3 = BN_CTX_get(ctx);
+ b1 = BN_CTX_get(ctx);
+ b2 = BN_CTX_get(ctx);
+ b3 = BN_CTX_get(ctx);
+ if (b3 == NULL) {
+ BN_CTX_end(ctx);
+ BN_CTX_free(ctx_new);
+ return -1;
+ }
+
+ /*
+ * XXX This approach assumes that the external representation of curves
+ * over the same field type is the same.
+ */
+ if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
+ !b->meth->group_get_curve(b, b1, b2, b3, ctx))
+ r = 1;
+
+ if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
+ r = 1;
+
+ /* XXX EC_POINT_cmp() assumes that the methods are equal */
+ if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
+ EC_GROUP_get0_generator(b), ctx))
+ r = 1;
+
+ if (!r) {
+ const BIGNUM *ao, *bo, *ac, *bc;
+ /* compare the order and cofactor */
+ ao = EC_GROUP_get0_order(a);
+ bo = EC_GROUP_get0_order(b);
+ ac = EC_GROUP_get0_cofactor(a);
+ bc = EC_GROUP_get0_cofactor(b);
+ if (ao == NULL || bo == NULL) {
+ BN_CTX_end(ctx);
+ BN_CTX_free(ctx_new);
+ return -1;
+ }
+ if (BN_cmp(ao, bo) || BN_cmp(ac, bc))
+ r = 1;
+ }
+
+ BN_CTX_end(ctx);
+ BN_CTX_free(ctx_new);
+
+ return r;
+}