mirror of
https://github.com/openssl/openssl.git
synced 2025-01-18 13:44:20 +08:00
New function EC_GROUP_check_discriminant().
Restructure implementation of EC_GROUP_check(). Submitted by: Nils Larsch
This commit is contained in:
parent
11c26ecf81
commit
17d6bb8158
@ -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
|
||||
|
@ -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
|
||||
|
122
crypto/ec/ec_check.c
Normal file
122
crypto/ec/ec_check.c
Normal file
@ -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;
|
||||
}
|
@ -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"},
|
||||
|
@ -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 *);
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user