Simplify ec_group_st on the assumption it is used for GF(p) only.
Change-Id: I90e8f9ce7b996471daed129794eb1b0fa80a27cc
Reviewed-on: https://boringssl-review.googlesource.com/4272
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/ec/ec_montgomery.c b/crypto/ec/ec_montgomery.c
index 0e5120c..f152b13 100644
--- a/crypto/ec/ec_montgomery.c
+++ b/crypto/ec/ec_montgomery.c
@@ -121,61 +121,61 @@
int ok;
ok = ec_GFp_simple_group_init(group);
- group->field_data1 = NULL;
- group->field_data2 = NULL;
+ group->mont = NULL;
+ group->one = NULL;
return ok;
}
void ec_GFp_mont_group_finish(EC_GROUP *group) {
- if (group->field_data1 != NULL) {
- BN_MONT_CTX_free(group->field_data1);
- group->field_data1 = NULL;
+ if (group->mont != NULL) {
+ BN_MONT_CTX_free(group->mont);
+ group->mont = NULL;
}
- if (group->field_data2 != NULL) {
- BN_free(group->field_data2);
- group->field_data2 = NULL;
+ if (group->one != NULL) {
+ BN_free(group->one);
+ group->one = NULL;
}
ec_GFp_simple_group_finish(group);
}
void ec_GFp_mont_group_clear_finish(EC_GROUP *group) {
- if (group->field_data1 != NULL) {
- BN_MONT_CTX_free(group->field_data1);
- group->field_data1 = NULL;
+ if (group->mont != NULL) {
+ BN_MONT_CTX_free(group->mont);
+ group->mont = NULL;
}
- if (group->field_data2 != NULL) {
- BN_clear_free(group->field_data2);
- group->field_data2 = NULL;
+ if (group->one != NULL) {
+ BN_clear_free(group->one);
+ group->one = NULL;
}
ec_GFp_simple_group_clear_finish(group);
}
int ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src) {
- if (dest->field_data1 != NULL) {
- BN_MONT_CTX_free(dest->field_data1);
- dest->field_data1 = NULL;
+ if (dest->mont != NULL) {
+ BN_MONT_CTX_free(dest->mont);
+ dest->mont = NULL;
}
- if (dest->field_data2 != NULL) {
- BN_clear_free(dest->field_data2);
- dest->field_data2 = NULL;
+ if (dest->one != NULL) {
+ BN_clear_free(dest->one);
+ dest->one = NULL;
}
if (!ec_GFp_simple_group_copy(dest, src)) {
return 0;
}
- if (src->field_data1 != NULL) {
- dest->field_data1 = BN_MONT_CTX_new();
- if (dest->field_data1 == NULL) {
+ if (src->mont != NULL) {
+ dest->mont = BN_MONT_CTX_new();
+ if (dest->mont == NULL) {
return 0;
}
- if (!BN_MONT_CTX_copy(dest->field_data1, src->field_data1)) {
+ if (!BN_MONT_CTX_copy(dest->mont, src->mont)) {
goto err;
}
}
- if (src->field_data2 != NULL) {
- dest->field_data2 = BN_dup(src->field_data2);
- if (dest->field_data2 == NULL) {
+ if (src->one != NULL) {
+ dest->one = BN_dup(src->one);
+ if (dest->one == NULL) {
goto err;
}
}
@@ -183,9 +183,9 @@
return 1;
err:
- if (dest->field_data1 != NULL) {
- BN_MONT_CTX_free(dest->field_data1);
- dest->field_data1 = NULL;
+ if (dest->mont != NULL) {
+ BN_MONT_CTX_free(dest->mont);
+ dest->mont = NULL;
}
return 0;
}
@@ -197,13 +197,13 @@
BIGNUM *one = NULL;
int ret = 0;
- if (group->field_data1 != NULL) {
- BN_MONT_CTX_free(group->field_data1);
- group->field_data1 = NULL;
+ if (group->mont != NULL) {
+ BN_MONT_CTX_free(group->mont);
+ group->mont = NULL;
}
- if (group->field_data2 != NULL) {
- BN_free(group->field_data2);
- group->field_data2 = NULL;
+ if (group->one != NULL) {
+ BN_free(group->one);
+ group->one = NULL;
}
if (ctx == NULL) {
@@ -226,18 +226,18 @@
goto err;
}
- group->field_data1 = mont;
+ group->mont = mont;
mont = NULL;
- group->field_data2 = one;
+ group->one = one;
one = NULL;
ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
if (!ret) {
- BN_MONT_CTX_free(group->field_data1);
- group->field_data1 = NULL;
- BN_free(group->field_data2);
- group->field_data2 = NULL;
+ BN_MONT_CTX_free(group->mont);
+ group->mont = NULL;
+ BN_free(group->one);
+ group->one = NULL;
}
err:
@@ -255,52 +255,52 @@
int ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
const BIGNUM *b, BN_CTX *ctx) {
- if (group->field_data1 == NULL) {
+ if (group->mont == NULL) {
OPENSSL_PUT_ERROR(EC, ec_GFp_mont_field_mul, EC_R_NOT_INITIALIZED);
return 0;
}
- return BN_mod_mul_montgomery(r, a, b, group->field_data1, ctx);
+ return BN_mod_mul_montgomery(r, a, b, group->mont, ctx);
}
int ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
BN_CTX *ctx) {
- if (group->field_data1 == NULL) {
+ if (group->mont == NULL) {
OPENSSL_PUT_ERROR(EC, ec_GFp_mont_field_sqr, EC_R_NOT_INITIALIZED);
return 0;
}
- return BN_mod_mul_montgomery(r, a, a, group->field_data1, ctx);
+ return BN_mod_mul_montgomery(r, a, a, group->mont, ctx);
}
int ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
BN_CTX *ctx) {
- if (group->field_data1 == NULL) {
+ if (group->mont == NULL) {
OPENSSL_PUT_ERROR(EC, ec_GFp_mont_field_encode, EC_R_NOT_INITIALIZED);
return 0;
}
- return BN_to_montgomery(r, a, (BN_MONT_CTX *)group->field_data1, ctx);
+ return BN_to_montgomery(r, a, group->mont, ctx);
}
int ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
BN_CTX *ctx) {
- if (group->field_data1 == NULL) {
+ if (group->mont == NULL) {
OPENSSL_PUT_ERROR(EC, ec_GFp_mont_field_decode, EC_R_NOT_INITIALIZED);
return 0;
}
- return BN_from_montgomery(r, a, group->field_data1, ctx);
+ return BN_from_montgomery(r, a, group->mont, ctx);
}
int ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r,
BN_CTX *ctx) {
- if (group->field_data2 == NULL) {
+ if (group->one == NULL) {
OPENSSL_PUT_ERROR(EC, ec_GFp_mont_field_set_to_one, EC_R_NOT_INITIALIZED);
return 0;
}
- if (!BN_copy(r, group->field_data2)) {
+ if (!BN_copy(r, group->one)) {
return 0;
}
return 1;
diff --git a/crypto/ec/internal.h b/crypto/ec/internal.h
index 17f63ae..50ad5b6 100644
--- a/crypto/ec/internal.h
+++ b/crypto/ec/internal.h
@@ -205,35 +205,14 @@
/* The following members are handled by the method functions,
* even if they appear generic */
- BIGNUM field; /* Field specification.
- * For curves over GF(p), this is the modulus;
- * for curves over GF(2^m), this is the
- * irreducible polynomial defining the field. */
+ BIGNUM field; /* For curves over GF(p), this is the modulus. */
- int poly[6]; /* Field specification for curves over GF(2^m).
- * The irreducible f(t) is then of the form:
- * t^poly[0] + t^poly[1] + ... + t^poly[k]
- * where m = poly[0] > poly[1] > ... > poly[k] = 0.
- * The array is terminated with poly[k+1]=-1.
- * All elliptic curve irreducibles have at most 5
- * non-zero terms. */
-
- BIGNUM a, b; /* Curve coefficients.
- * (Here the assumption is that BIGNUMs can be used
- * or abused for all kinds of fields, not just GF(p).)
- * For characteristic > 3, the curve is defined
- * by a Weierstrass equation of the form
- * y^2 = x^3 + a*x + b.
- * For characteristic 2, the curve is defined by
- * an equation of the form
- * y^2 + x*y = x^3 + a*x^2 + b. */
+ BIGNUM a, b; /* Curve coefficients. */
int a_is_minus3; /* enable optimized point arithmetics for special case */
- void *field_data1; /* method-specific (e.g., Montgomery structure) */
- void *field_data2; /* method-specific */
- int (*field_mod_func)(BIGNUM *, const BIGNUM *, const BIGNUM *,
- BN_CTX *); /* method-specific */
+ BN_MONT_CTX *mont; /* Montgomery structure. */
+ BIGNUM *one; /* The value one */
} /* EC_GROUP */;
struct ec_point_st {