New function EC_GROUP_check_discriminant().
authorBodo Möller <bodo@openssl.org>
Wed, 20 Mar 2002 10:18:07 +0000 (10:18 +0000)
committerBodo Möller <bodo@openssl.org>
Wed, 20 Mar 2002 10:18:07 +0000 (10:18 +0000)
Restructure implementation of EC_GROUP_check().

Submitted by: Nils Larsch

crypto/ec/Makefile.ssl
crypto/ec/ec.h
crypto/ec/ec_check.c [new file with mode: 0644]
crypto/ec/ec_err.c
crypto/ec/ec_lcl.h
crypto/ec/ec_lib.c
crypto/ec/ecp_mont.c
crypto/ec/ecp_nist.c
crypto/ec/ecp_recp.c
crypto/ec/ecp_smpl.c

index 002841eff20999e4f36a942c07e10345149ba62c..3b5ae1951227426761628b89b8abb0662f690f6c 100644 (file)
@@ -24,10 +24,10 @@ APPS=
 
 LIB=$(TOP)/libcrypto.a
 LIBSRC=        ec_lib.c ecp_smpl.c ecp_mont.c ecp_recp.c ecp_nist.c ec_cvt.c ec_mult.c \
-       ec_err.c ec_curve.c
+       ec_err.c ec_curve.c ec_check.c
 
 LIBOBJ=        ec_lib.o ecp_smpl.o ecp_mont.o ecp_recp.o ecp_nist.o ec_cvt.o ec_mult.o \
-       ec_err.o ec_curve.o
+       ec_err.o ec_curve.o ec_check.o
 
 SRC= $(LIBSRC)
 
@@ -82,6 +82,13 @@ clean:
 
 # DO NOT DELETE THIS LINE -- make depend depends on it.
 
+ec_check.o: ../../include/openssl/bio.h ../../include/openssl/bn.h
+ec_check.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
+ec_check.o: ../../include/openssl/ec.h ../../include/openssl/err.h
+ec_check.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
+ec_check.o: ../../include/openssl/opensslv.h ../../include/openssl/safestack.h
+ec_check.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
+ec_check.o: ec_check.c ec_lcl.h
 ec_curve.o: ../../include/openssl/asn1.h ../../include/openssl/asn1t.h
 ec_curve.o: ../../include/openssl/bio.h ../../include/openssl/bn.h
 ec_curve.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
index f83bacc89b22abf6d4aedeb9e0d87cda29abe0f8..a79112536951e1c2ae35d888e21ee5193523744b 100644 (file)
@@ -127,6 +127,9 @@ int EC_GROUP_get_cofactor(const EC_GROUP *, BIGNUM *cofactor, BN_CTX *);
 
 /* EC_GROUP_check() returns 1 if 'group' defines a valid group, 0 otherwise */
 int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx);
+/* EC_GROUP_check_discriminant() returns 1 if the discriminant of the
+ * elliptic curve is not zero, 0 otherwise */
+int EC_GROUP_check_discriminant(const EC_GROUP *, BN_CTX *);
 
 /* EC_GROUP_new_GFp() calls EC_GROUP_new() and EC_GROUP_set_GFp()
  * after choosing an appropriate EC_METHOD */
@@ -227,7 +230,7 @@ void ERR_load_EC_strings(void);
 #define EC_F_EC_GFP_MONT_FIELD_ENCODE                   134
 #define EC_F_EC_GFP_MONT_FIELD_MUL                      131
 #define EC_F_EC_GFP_MONT_FIELD_SQR                      132
-#define EC_F_EC_GFP_SIMPLE_GROUP_CHECK                  151
+#define EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT     152
 #define EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE_GFP          100
 #define EC_F_EC_GFP_SIMPLE_GROUP_SET_GENERATOR          101
 #define EC_F_EC_GFP_SIMPLE_MAKE_AFFINE                  102
@@ -238,6 +241,7 @@ void ERR_load_EC_strings(void);
 #define EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES_GFP 128
 #define EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP 129
 #define EC_F_EC_GROUP_CHECK                             150
+#define EC_F_EC_GROUP_CHECK_DISCRIMINANT                153
 #define EC_F_EC_GROUP_COPY                              106
 #define EC_F_EC_GROUP_GET0_GENERATOR                    139
 #define EC_F_EC_GROUP_GET_COFACTOR                      140
diff --git a/crypto/ec/ec_check.c b/crypto/ec/ec_check.c
new file mode 100644 (file)
index 0000000..f22c564
--- /dev/null
@@ -0,0 +1,122 @@
+/* crypto/ec/ec_check.c */
+/* ====================================================================
+ * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer. 
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. All advertising materials mentioning features or use of this
+ *    software must display the following acknowledgment:
+ *    "This product includes software developed by the OpenSSL Project
+ *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
+ *
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
+ *    endorse or promote products derived from this software without
+ *    prior written permission. For written permission, please contact
+ *    openssl-core@openssl.org.
+ *
+ * 5. Products derived from this software may not be called "OpenSSL"
+ *    nor may "OpenSSL" appear in their names without prior written
+ *    permission of the OpenSSL Project.
+ *
+ * 6. Redistributions of any form whatsoever must retain the following
+ *    acknowledgment:
+ *    "This product includes software developed by the OpenSSL Project
+ *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This product includes cryptographic software written by Eric Young
+ * (eay@cryptsoft.com).  This product includes software written by Tim
+ * Hudson (tjh@cryptsoft.com).
+ *
+ */
+
+#include "ec_lcl.h"
+#include <openssl/err.h>
+
+int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx)
+       {
+       int ret = 0;
+       BIGNUM *order;
+       BN_CTX *new_ctx = NULL;
+       EC_POINT *point = NULL;
+
+       if (ctx == NULL)
+               {
+               ctx = new_ctx = BN_CTX_new();
+               if (ctx == NULL)
+                       {
+                       ECerr(EC_F_EC_GROUP_CHECK, ERR_R_MALLOC_FAILURE);
+                       goto err;
+                       }
+               }
+       BN_CTX_start(ctx);
+       if ((order = BN_CTX_get(ctx)) == NULL) goto err;
+
+       /* check the discriminant */
+       if (!EC_GROUP_check_discriminant(group, ctx))
+               {
+               ECerr(EC_F_EC_GROUP_CHECK, EC_R_DISCRIMINANT_IS_ZERO);
+               goto err;
+               }
+
+       /* check the generator */
+       if (group->generator == NULL)
+               {
+               ECerr(EC_F_EC_GROUP_CHECK, EC_R_UNDEFINED_GENERATOR);
+               goto err;
+               }
+       if (!EC_POINT_is_on_curve(group, group->generator, ctx))
+               {
+               ECerr(EC_F_EC_GROUP_CHECK, EC_R_POINT_IS_NOT_ON_CURVE);
+               goto err;
+               }
+
+       /* check the order of the generator */
+       if ((point = EC_POINT_new(group)) == NULL) goto err;
+       if (!EC_GROUP_get_order(group, order, ctx)) goto err; 
+       if (BN_is_zero(order))
+               {
+               ECerr(EC_F_EC_GROUP_CHECK, EC_R_UNDEFINED_ORDER);
+               goto err;
+               }
+       
+       if (!EC_POINT_mul(group, point, order, NULL, NULL, ctx)) goto err;
+       if (!EC_POINT_is_at_infinity(group, point))
+               {
+               ECerr(EC_F_EC_GROUP_CHECK, EC_R_INVALID_GROUP_ORDER);
+               goto err;
+               }
+
+       ret = 1;
+
+err:
+       BN_CTX_end(ctx);
+       if (new_ctx != NULL)
+               BN_CTX_free(new_ctx);
+       if (point)
+               EC_POINT_free(point);
+       return ret;
+       }
index e78713bd0b51f744af1d51412ac95d8817db59c5..efdee969cea4ddc389aee77acc25c7e3734d122b 100644 (file)
@@ -71,7 +71,7 @@ static ERR_STRING_DATA EC_str_functs[]=
 {ERR_PACK(0,EC_F_EC_GFP_MONT_FIELD_ENCODE,0),  "ec_GFp_mont_field_encode"},
 {ERR_PACK(0,EC_F_EC_GFP_MONT_FIELD_MUL,0),     "ec_GFp_mont_field_mul"},
 {ERR_PACK(0,EC_F_EC_GFP_MONT_FIELD_SQR,0),     "ec_GFp_mont_field_sqr"},
-{ERR_PACK(0,EC_F_EC_GFP_SIMPLE_GROUP_CHECK,0), "ec_GFp_simple_group_check"},
+{ERR_PACK(0,EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT,0),    "ec_GFp_simple_group_check_discriminant"},
 {ERR_PACK(0,EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE_GFP,0), "ec_GFp_simple_group_set_curve_GFp"},
 {ERR_PACK(0,EC_F_EC_GFP_SIMPLE_GROUP_SET_GENERATOR,0), "ec_GFp_simple_group_set_generator"},
 {ERR_PACK(0,EC_F_EC_GFP_SIMPLE_MAKE_AFFINE,0), "ec_GFp_simple_make_affine"},
@@ -82,6 +82,7 @@ static ERR_STRING_DATA EC_str_functs[]=
 {ERR_PACK(0,EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES_GFP,0),    "ec_GFp_simple_point_set_affine_coordinates_GFp"},
 {ERR_PACK(0,EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP,0),      "ec_GFp_simple_set_compressed_coordinates_GFp"},
 {ERR_PACK(0,EC_F_EC_GROUP_CHECK,0),    "EC_GROUP_check"},
+{ERR_PACK(0,EC_F_EC_GROUP_CHECK_DISCRIMINANT,0),       "EC_GROUP_check_discriminant"},
 {ERR_PACK(0,EC_F_EC_GROUP_COPY,0),     "EC_GROUP_copy"},
 {ERR_PACK(0,EC_F_EC_GROUP_GET0_GENERATOR,0),   "EC_GROUP_get0_generator"},
 {ERR_PACK(0,EC_F_EC_GROUP_GET_COFACTOR,0),     "EC_GROUP_get_cofactor"},
index 87feb39dea740627b22a35ad278ddf9ab32ff23f..ece0de852cd00528c36d51c20f31432a1f2215ae 100644 (file)
@@ -83,7 +83,7 @@ struct ec_method_st {
        int (*group_get_cofactor)(const EC_GROUP *, BIGNUM *cofactor, BN_CTX *);
 
        /* used by EC_GROUP_check: */
-       int (*group_check)(const EC_GROUP *, BN_CTX *);
+       int (*group_check_discriminant)(const EC_GROUP *, BN_CTX *);
 
        /* used by EC_POINT_new, EC_POINT_free, EC_POINT_clear_free, EC_POINT_copy: */
        int (*point_init)(EC_POINT *);
@@ -218,7 +218,7 @@ int ec_GFp_simple_group_set_generator(EC_GROUP *, const EC_POINT *generator,
 EC_POINT *ec_GFp_simple_group_get0_generator(const EC_GROUP *);
 int ec_GFp_simple_group_get_order(const EC_GROUP *, BIGNUM *order, BN_CTX *);
 int ec_GFp_simple_group_get_cofactor(const EC_GROUP *, BIGNUM *cofactor, BN_CTX *);
-int ec_GFp_simple_group_check(const EC_GROUP *, BN_CTX *);
+int ec_GFp_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
 int ec_GFp_simple_point_init(EC_POINT *);
 void ec_GFp_simple_point_finish(EC_POINT *);
 void ec_GFp_simple_point_clear_finish(EC_POINT *);
index 1a6aceed820895c5ab0079371c9e65fd5de9064b..a5153cd4bf5982e2b698d51d8a4a83ec4af60f00 100644 (file)
@@ -237,14 +237,14 @@ int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, BN_CTX *ctx)
        }
 
 
-int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx)
+int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
        {
-       if (group->meth->group_check == 0)
+       if (group->meth->group_check_discriminant == 0)
                {
-               ECerr(EC_F_EC_GROUP_CHECK, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
+               ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
                return 0;
                }
-       return group->meth->group_check(group, ctx);
+       return group->meth->group_check_discriminant(group, ctx);
        }
 
 
index 1d02661555d58b6dabc07c7b485442b584be5d48..bad0a1361477a5bbc79f66881bfdfe73bdefc6f5 100644 (file)
@@ -71,7 +71,7 @@ const EC_METHOD *EC_GFp_mont_method(void)
                ec_GFp_simple_group_get0_generator,
                ec_GFp_simple_group_get_order,
                ec_GFp_simple_group_get_cofactor,
-               ec_GFp_simple_group_check,
+               ec_GFp_simple_group_check_discriminant,
                ec_GFp_simple_point_init,
                ec_GFp_simple_point_finish,
                ec_GFp_simple_point_clear_finish,
index 4d92220670857f5f3eebd3224e673945462da453..4e37c33cf3d2c73e13eaaf023788cc3ed823ca48 100644 (file)
@@ -69,7 +69,7 @@ const EC_METHOD *EC_GFp_nist_method(void)
                ec_GFp_simple_group_get0_generator,
                ec_GFp_simple_group_get_order,
                ec_GFp_simple_group_get_cofactor,
-               ec_GFp_simple_group_check,
+               ec_GFp_simple_group_check_discriminant,
                ec_GFp_simple_point_init,
                ec_GFp_simple_point_finish,
                ec_GFp_simple_point_clear_finish,
index b251c921ce3df2e566c04861b1ca1bb6678081e6..54d3b83ec49b97b6781c0bf8e427c2d45cd9ceb9 100644 (file)
@@ -69,7 +69,7 @@ const EC_METHOD *EC_GFp_recp_method(void)
                ec_GFp_simple_group_get0_generator,
                ec_GFp_simple_group_get_order,
                ec_GFp_simple_group_get_cofactor,
-               ec_GFp_simple_group_check,
+               ec_GFp_simple_group_check_discriminant,
                ec_GFp_simple_point_init,
                ec_GFp_simple_point_finish,
                ec_GFp_simple_point_clear_finish,
index 8e062dc95126e9c5e610855d4fb004f898172dc8..911a4e4760eb93434d2a4ce429e6d71f15a1cbcb 100644 (file)
@@ -73,7 +73,7 @@ const EC_METHOD *EC_GFp_simple_method(void)
                ec_GFp_simple_group_get0_generator,
                ec_GFp_simple_group_get_order,
                ec_GFp_simple_group_get_cofactor,
-               ec_GFp_simple_group_check,
+               ec_GFp_simple_group_check_discriminant,
                ec_GFp_simple_point_init,
                ec_GFp_simple_point_finish,
                ec_GFp_simple_point_clear_finish,
@@ -339,20 +339,19 @@ int ec_GFp_simple_group_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, BN
        }
 
 
-int ec_GFp_simple_group_check(const EC_GROUP *group, BN_CTX *ctx)
+int ec_GFp_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
        {
        int ret = 0;
        BIGNUM *a,*b,*order,*tmp_1,*tmp_2;
        const BIGNUM *p = &group->field;
        BN_CTX *new_ctx = NULL;
-       EC_POINT *point = NULL;
 
        if (ctx == NULL)
                {
                ctx = new_ctx = BN_CTX_new();
                if (ctx == NULL)
                        {
-                       ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK, ERR_R_MALLOC_FAILURE);
+                       ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT, ERR_R_MALLOC_FAILURE);
                        goto err;
                        }
                }
@@ -380,11 +379,7 @@ int ec_GFp_simple_group_check(const EC_GROUP *group, BN_CTX *ctx)
          * 0 =< a, b < p */
        if (BN_is_zero(a))
                {
-               if (BN_is_zero(b))
-                       {
-                       ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK, EC_R_DISCRIMINANT_IS_ZERO);
-                       goto err;
-                       }
+               if (BN_is_zero(b)) goto err;
                }
        else if (!BN_is_zero(b))
                {
@@ -398,49 +393,14 @@ int ec_GFp_simple_group_check(const EC_GROUP *group, BN_CTX *ctx)
                /* tmp_2 = 27*b^2 */
 
                if (!BN_mod_add(a, tmp_1, tmp_2, p, ctx)) goto err;
-               if (BN_is_zero(a))
-                       {
-                       ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK, EC_R_DISCRIMINANT_IS_ZERO);
-                       goto err;
-                       }
+               if (BN_is_zero(a)) goto err;
                }
-       
-       /* check the generator */
-       if (group->generator == NULL)
-               {
-               ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK, EC_R_UNDEFINED_GENERATOR);
-               goto err;
-               }
-       if (!ec_GFp_simple_is_on_curve(group, group->generator, ctx))
-               {
-               ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK, EC_R_POINT_IS_NOT_ON_CURVE);
-               goto err;
-               }
-
-       /* check the order of the generator */
-       if ((point = EC_POINT_new(group)) == NULL) goto err;
-       if (!EC_GROUP_get_order(group, order, ctx)) goto err; 
-       if (BN_is_zero(order))
-               {
-               ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK, EC_R_UNDEFINED_ORDER);
-               goto err;
-               }
-       
-       if (!EC_POINT_mul(group, point, order, NULL, NULL, ctx)) goto err;
-       if (!EC_POINT_is_at_infinity(group, point))
-               {
-               ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK, EC_R_INVALID_GROUP_ORDER);
-               goto err;
-               }
-
        ret = 1;
 
 err:
        BN_CTX_end(ctx);
        if (new_ctx != NULL)
                BN_CTX_free(new_ctx);
-       if (point)
-               EC_POINT_free(point);
        return ret;
        }