summaryrefslogtreecommitdiffstats
path: root/crypto/ec/ec_mult.c
diff options
context:
space:
mode:
authorBilly Brumley <bbrumley@gmail.com>2018-06-20 10:56:37 +0300
committerRichard Levitte <levitte@openssl.org>2018-06-21 13:33:42 +0200
commita766aab93a282774e63ba918d0bb1c6680a5f292 (patch)
tree60a0658c4ad8129af5866de1606c91ebb8ff7597 /crypto/ec/ec_mult.c
parent27232cc3385260311e7fd2f6cd78db967cae650d (diff)
[crypto/ec] don't assume points are of order group->order
Reviewed-by: Paul Dale <paul.dale@oracle.com> Reviewed-by: Richard Levitte <levitte@openssl.org> (Merged from https://github.com/openssl/openssl/pull/6535)
Diffstat (limited to 'crypto/ec/ec_mult.c')
-rw-r--r--crypto/ec/ec_mult.c29
1 files changed, 15 insertions, 14 deletions
diff --git a/crypto/ec/ec_mult.c b/crypto/ec/ec_mult.c
index b668e87ff7..c821cb8250 100644
--- a/crypto/ec/ec_mult.c
+++ b/crypto/ec/ec_mult.c
@@ -132,10 +132,11 @@ static int ec_mul_consttime(const EC_GROUP *group, EC_POINT *r,
const BIGNUM *scalar, const EC_POINT *point,
BN_CTX *ctx)
{
- int i, order_bits, group_top, kbit, pbit, Z_is_one;
+ int i, cardinality_bits, group_top, kbit, pbit, Z_is_one;
EC_POINT *s = NULL;
BIGNUM *k = NULL;
BIGNUM *lambda = NULL;
+ BIGNUM *cardinality = NULL;
BN_CTX *new_ctx = NULL;
int ret = 0;
@@ -144,8 +145,6 @@ static int ec_mul_consttime(const EC_GROUP *group, EC_POINT *r,
BN_CTX_start(ctx);
- order_bits = BN_num_bits(group->order);
-
s = EC_POINT_new(group);
if (s == NULL)
goto err;
@@ -160,18 +159,20 @@ static int ec_mul_consttime(const EC_GROUP *group, EC_POINT *r,
EC_POINT_BN_set_flags(s, BN_FLG_CONSTTIME);
+ cardinality = BN_CTX_get(ctx);
lambda = BN_CTX_get(ctx);
k = BN_CTX_get(ctx);
- if (k == NULL)
+ if (k == NULL || !BN_mul(cardinality, group->order, group->cofactor, ctx))
goto err;
/*
- * Group orders are often on a word boundary.
+ * Group cardinalities are often on a word boundary.
* So when we pad the scalar, some timing diff might
* pop if it needs to be expanded due to carries.
* So expand ahead of time.
*/
- group_top = bn_get_top(group->order);
+ cardinality_bits = BN_num_bits(cardinality);
+ group_top = bn_get_top(cardinality);
if ((bn_wexpand(k, group_top + 1) == NULL)
|| (bn_wexpand(lambda, group_top + 1) == NULL))
goto err;
@@ -181,25 +182,25 @@ static int ec_mul_consttime(const EC_GROUP *group, EC_POINT *r,
BN_set_flags(k, BN_FLG_CONSTTIME);
- if ((BN_num_bits(k) > order_bits) || (BN_is_negative(k))) {
+ if ((BN_num_bits(k) > cardinality_bits) || (BN_is_negative(k))) {
/*-
* this is an unusual input, and we don't guarantee
* constant-timeness
*/
- if (!BN_nnmod(k, k, group->order, ctx))
+ if (!BN_nnmod(k, k, cardinality, ctx))
goto err;
}
- if (!BN_add(lambda, k, group->order))
+ if (!BN_add(lambda, k, cardinality))
goto err;
BN_set_flags(lambda, BN_FLG_CONSTTIME);
- if (!BN_add(k, lambda, group->order))
+ if (!BN_add(k, lambda, cardinality))
goto err;
/*
- * lambda := scalar + order
- * k := scalar + 2*order
+ * lambda := scalar + cardinality
+ * k := scalar + 2*cardinality
*/
- kbit = BN_is_bit_set(lambda, order_bits);
+ kbit = BN_is_bit_set(lambda, cardinality_bits);
BN_consttime_swap(kbit, k, lambda, group_top + 1);
group_top = bn_get_top(group->field);
@@ -300,7 +301,7 @@ static int ec_mul_consttime(const EC_GROUP *group, EC_POINT *r,
* This is XOR. pbit tracks the previous bit of k.
*/
- for (i = order_bits - 1; i >= 0; i--) {
+ for (i = cardinality_bits - 1; i >= 0; i--) {
kbit = BN_is_bit_set(k, i) ^ pbit;
EC_POINT_CSWAP(kbit, r, s, group_top, Z_is_one);
if (!EC_POINT_add(group, s, r, s, ctx))