|
|
a07c00 |
diff -up openssl-1.0.2k/crypto/bn/bn_lib.c.ecc-ladder openssl-1.0.2k/crypto/bn/bn_lib.c
|
|
|
a07c00 |
--- openssl-1.0.2k/crypto/bn/bn_lib.c.ecc-ladder 2019-02-06 12:58:50.575844123 +0100
|
|
|
a07c00 |
+++ openssl-1.0.2k/crypto/bn/bn_lib.c 2019-02-08 10:48:53.529291777 +0100
|
|
|
a07c00 |
@@ -877,6 +877,38 @@ void BN_consttime_swap(BN_ULONG conditio
|
|
|
a07c00 |
a->top ^= t;
|
|
|
a07c00 |
b->top ^= t;
|
|
|
a07c00 |
|
|
|
a07c00 |
+ t = (a->neg ^ b->neg) & condition;
|
|
|
a07c00 |
+ a->neg ^= t;
|
|
|
a07c00 |
+ b->neg ^= t;
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ /*-
|
|
|
a07c00 |
+ * BN_FLG_STATIC_DATA: indicates that data may not be written to. Intention
|
|
|
a07c00 |
+ * is actually to treat it as it's read-only data, and some (if not most)
|
|
|
a07c00 |
+ * of it does reside in read-only segment. In other words observation of
|
|
|
a07c00 |
+ * BN_FLG_STATIC_DATA in BN_consttime_swap should be treated as fatal
|
|
|
a07c00 |
+ * condition. It would either cause SEGV or effectively cause data
|
|
|
a07c00 |
+ * corruption.
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * BN_FLG_MALLOCED: refers to BN structure itself, and hence must be
|
|
|
a07c00 |
+ * preserved.
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * BN_FLG_SECURE: must be preserved, because it determines how x->d was
|
|
|
a07c00 |
+ * allocated and hence how to free it.
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * BN_FLG_CONSTTIME: sufficient to mask and swap
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * BN_FLG_FIXED_TOP: indicates that we haven't called bn_correct_top() on
|
|
|
a07c00 |
+ * the data, so the d array may be padded with additional 0 values (i.e.
|
|
|
a07c00 |
+ * top could be greater than the minimal value that it could be). We should
|
|
|
a07c00 |
+ * be swapping it
|
|
|
a07c00 |
+ */
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+#define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition;
|
|
|
a07c00 |
+ a->flags ^= t;
|
|
|
a07c00 |
+ b->flags ^= t;
|
|
|
a07c00 |
+
|
|
|
a07c00 |
#define BN_CONSTTIME_SWAP(ind) \
|
|
|
a07c00 |
do { \
|
|
|
a07c00 |
t = (a->d[ind] ^ b->d[ind]) & condition; \
|
|
|
a07c00 |
diff -up openssl-1.0.2k/crypto/ec/ec_mult.c.ecc-ladder openssl-1.0.2k/crypto/ec/ec_mult.c
|
|
|
a07c00 |
--- openssl-1.0.2k/crypto/ec/ec_mult.c.ecc-ladder 2017-01-26 14:22:03.000000000 +0100
|
|
|
a07c00 |
+++ openssl-1.0.2k/crypto/ec/ec_mult.c 2019-02-08 10:48:53.531291744 +0100
|
|
|
a07c00 |
@@ -306,6 +306,224 @@ static signed char *compute_wNAF(const B
|
|
|
a07c00 |
return r;
|
|
|
a07c00 |
}
|
|
|
a07c00 |
|
|
|
a07c00 |
+#define EC_POINT_BN_set_flags(P, flags) do { \
|
|
|
a07c00 |
+ BN_set_flags(&(P)->X, (flags)); \
|
|
|
a07c00 |
+ BN_set_flags(&(P)->Y, (flags)); \
|
|
|
a07c00 |
+ BN_set_flags(&(P)->Z, (flags)); \
|
|
|
a07c00 |
+} while(0)
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+/*-
|
|
|
a07c00 |
+ * This functions computes (in constant time) a point multiplication over the
|
|
|
a07c00 |
+ * EC group.
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * At a high level, it is Montgomery ladder with conditional swaps.
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * It performs either a fixed scalar point multiplication
|
|
|
a07c00 |
+ * (scalar * generator)
|
|
|
a07c00 |
+ * when point is NULL, or a generic scalar point multiplication
|
|
|
a07c00 |
+ * (scalar * point)
|
|
|
a07c00 |
+ * when point is not NULL.
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * scalar should be in the range [0,n) otherwise all constant time bets are off.
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * NB: This says nothing about EC_POINT_add and EC_POINT_dbl,
|
|
|
a07c00 |
+ * which of course are not constant time themselves.
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * The product is stored in r.
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * Returns 1 on success, 0 otherwise.
|
|
|
a07c00 |
+ */
|
|
|
a07c00 |
+static int ec_mul_consttime(const EC_GROUP *group, EC_POINT *r,
|
|
|
a07c00 |
+ const BIGNUM *scalar, const EC_POINT *point,
|
|
|
a07c00 |
+ BN_CTX *ctx)
|
|
|
a07c00 |
+{
|
|
|
a07c00 |
+ int i, cardinality_bits, group_top, kbit, pbit, Z_is_one;
|
|
|
a07c00 |
+ EC_POINT *s = NULL;
|
|
|
a07c00 |
+ BIGNUM *k = NULL;
|
|
|
a07c00 |
+ BIGNUM *lambda = NULL;
|
|
|
a07c00 |
+ BIGNUM *cardinality = NULL;
|
|
|
a07c00 |
+ BN_CTX *new_ctx = NULL;
|
|
|
a07c00 |
+ int ret = 0;
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ if (ctx == NULL && (ctx = new_ctx = BN_CTX_new()) == NULL)
|
|
|
a07c00 |
+ return 0;
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ BN_CTX_start(ctx);
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ s = EC_POINT_new(group);
|
|
|
a07c00 |
+ if (s == NULL)
|
|
|
a07c00 |
+ goto err;
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ if (point == NULL) {
|
|
|
a07c00 |
+ if (!EC_POINT_copy(s, group->generator))
|
|
|
a07c00 |
+ goto err;
|
|
|
a07c00 |
+ } else {
|
|
|
a07c00 |
+ if (!EC_POINT_copy(s, point))
|
|
|
a07c00 |
+ goto err;
|
|
|
a07c00 |
+ }
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ EC_POINT_BN_set_flags(s, BN_FLG_CONSTTIME);
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ cardinality = BN_CTX_get(ctx);
|
|
|
a07c00 |
+ lambda = BN_CTX_get(ctx);
|
|
|
a07c00 |
+ k = BN_CTX_get(ctx);
|
|
|
a07c00 |
+ if (k == NULL || !BN_mul(cardinality, &group->order, &group->cofactor, ctx))
|
|
|
a07c00 |
+ goto err;
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ /*
|
|
|
a07c00 |
+ * Group cardinalities are often on a word boundary.
|
|
|
a07c00 |
+ * So when we pad the scalar, some timing diff might
|
|
|
a07c00 |
+ * pop if it needs to be expanded due to carries.
|
|
|
a07c00 |
+ * So expand ahead of time.
|
|
|
a07c00 |
+ */
|
|
|
a07c00 |
+ cardinality_bits = BN_num_bits(cardinality);
|
|
|
a07c00 |
+ group_top = cardinality->top;
|
|
|
a07c00 |
+ if ((bn_wexpand(k, group_top + 2) == NULL)
|
|
|
a07c00 |
+ || (bn_wexpand(lambda, group_top + 2) == NULL))
|
|
|
a07c00 |
+ goto err;
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ if (!BN_copy(k, scalar))
|
|
|
a07c00 |
+ goto err;
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ BN_set_flags(k, BN_FLG_CONSTTIME);
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ if ((BN_num_bits(k) > cardinality_bits) || (BN_is_negative(k))) {
|
|
|
a07c00 |
+ /*-
|
|
|
a07c00 |
+ * this is an unusual input, and we don't guarantee
|
|
|
a07c00 |
+ * constant-timeness
|
|
|
a07c00 |
+ */
|
|
|
a07c00 |
+ if (!BN_nnmod(k, k, cardinality, ctx))
|
|
|
a07c00 |
+ goto err;
|
|
|
a07c00 |
+ }
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ if (!BN_add(lambda, k, cardinality))
|
|
|
a07c00 |
+ goto err;
|
|
|
a07c00 |
+ BN_set_flags(lambda, BN_FLG_CONSTTIME);
|
|
|
a07c00 |
+ if (!BN_add(k, lambda, cardinality))
|
|
|
a07c00 |
+ goto err;
|
|
|
a07c00 |
+ /*
|
|
|
a07c00 |
+ * lambda := scalar + cardinality
|
|
|
a07c00 |
+ * k := scalar + 2*cardinality
|
|
|
a07c00 |
+ */
|
|
|
a07c00 |
+ kbit = BN_is_bit_set(lambda, cardinality_bits);
|
|
|
a07c00 |
+ BN_consttime_swap(kbit, k, lambda, group_top + 2);
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ group_top = group->field.top;
|
|
|
a07c00 |
+ if ((bn_wexpand(&s->X, group_top) == NULL)
|
|
|
a07c00 |
+ || (bn_wexpand(&s->Y, group_top) == NULL)
|
|
|
a07c00 |
+ || (bn_wexpand(&s->Z, group_top) == NULL)
|
|
|
a07c00 |
+ || (bn_wexpand(&r->X, group_top) == NULL)
|
|
|
a07c00 |
+ || (bn_wexpand(&r->Y, group_top) == NULL)
|
|
|
a07c00 |
+ || (bn_wexpand(&r->Z, group_top) == NULL))
|
|
|
a07c00 |
+ goto err;
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ /* top bit is a 1, in a fixed pos */
|
|
|
a07c00 |
+ if (!EC_POINT_copy(r, s))
|
|
|
a07c00 |
+ goto err;
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ EC_POINT_BN_set_flags(r, BN_FLG_CONSTTIME);
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ if (!EC_POINT_dbl(group, s, s, ctx))
|
|
|
a07c00 |
+ goto err;
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ pbit = 0;
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+#define EC_POINT_CSWAP(c, a, b, w, t) do { \
|
|
|
a07c00 |
+ BN_consttime_swap(c, &(a)->X, &(b)->X, w); \
|
|
|
a07c00 |
+ BN_consttime_swap(c, &(a)->Y, &(b)->Y, w); \
|
|
|
a07c00 |
+ BN_consttime_swap(c, &(a)->Z, &(b)->Z, w); \
|
|
|
a07c00 |
+ t = ((a)->Z_is_one ^ (b)->Z_is_one) & (c); \
|
|
|
a07c00 |
+ (a)->Z_is_one ^= (t); \
|
|
|
a07c00 |
+ (b)->Z_is_one ^= (t); \
|
|
|
a07c00 |
+} while(0)
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ /*-
|
|
|
a07c00 |
+ * The ladder step, with branches, is
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * k[i] == 0: S = add(R, S), R = dbl(R)
|
|
|
a07c00 |
+ * k[i] == 1: R = add(S, R), S = dbl(S)
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * Swapping R, S conditionally on k[i] leaves you with state
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * k[i] == 0: T, U = R, S
|
|
|
a07c00 |
+ * k[i] == 1: T, U = S, R
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * Then perform the ECC ops.
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * U = add(T, U)
|
|
|
a07c00 |
+ * T = dbl(T)
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * Which leaves you with state
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * k[i] == 0: U = add(R, S), T = dbl(R)
|
|
|
a07c00 |
+ * k[i] == 1: U = add(S, R), T = dbl(S)
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * Swapping T, U conditionally on k[i] leaves you with state
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * k[i] == 0: R, S = T, U
|
|
|
a07c00 |
+ * k[i] == 1: R, S = U, T
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * Which leaves you with state
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * k[i] == 0: S = add(R, S), R = dbl(R)
|
|
|
a07c00 |
+ * k[i] == 1: R = add(S, R), S = dbl(S)
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * So we get the same logic, but instead of a branch it's a
|
|
|
a07c00 |
+ * conditional swap, followed by ECC ops, then another conditional swap.
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * Optimization: The end of iteration i and start of i-1 looks like
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * ...
|
|
|
a07c00 |
+ * CSWAP(k[i], R, S)
|
|
|
a07c00 |
+ * ECC
|
|
|
a07c00 |
+ * CSWAP(k[i], R, S)
|
|
|
a07c00 |
+ * (next iteration)
|
|
|
a07c00 |
+ * CSWAP(k[i-1], R, S)
|
|
|
a07c00 |
+ * ECC
|
|
|
a07c00 |
+ * CSWAP(k[i-1], R, S)
|
|
|
a07c00 |
+ * ...
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * So instead of two contiguous swaps, you can merge the condition
|
|
|
a07c00 |
+ * bits and do a single swap.
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * k[i] k[i-1] Outcome
|
|
|
a07c00 |
+ * 0 0 No Swap
|
|
|
a07c00 |
+ * 0 1 Swap
|
|
|
a07c00 |
+ * 1 0 Swap
|
|
|
a07c00 |
+ * 1 1 No Swap
|
|
|
a07c00 |
+ *
|
|
|
a07c00 |
+ * This is XOR. pbit tracks the previous bit of k.
|
|
|
a07c00 |
+ */
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ for (i = cardinality_bits - 1; i >= 0; i--) {
|
|
|
a07c00 |
+ kbit = BN_is_bit_set(k, i) ^ pbit;
|
|
|
a07c00 |
+ EC_POINT_CSWAP(kbit, r, s, group_top, Z_is_one);
|
|
|
a07c00 |
+ if (!EC_POINT_add(group, s, r, s, ctx))
|
|
|
a07c00 |
+ goto err;
|
|
|
a07c00 |
+ if (!EC_POINT_dbl(group, r, r, ctx))
|
|
|
a07c00 |
+ goto err;
|
|
|
a07c00 |
+ /*
|
|
|
a07c00 |
+ * pbit logic merges this cswap with that of the
|
|
|
a07c00 |
+ * next iteration
|
|
|
a07c00 |
+ */
|
|
|
a07c00 |
+ pbit ^= kbit;
|
|
|
a07c00 |
+ }
|
|
|
a07c00 |
+ /* one final cswap to move the right value into r */
|
|
|
a07c00 |
+ EC_POINT_CSWAP(pbit, r, s, group_top, Z_is_one);
|
|
|
a07c00 |
+#undef EC_POINT_CSWAP
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ ret = 1;
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ err:
|
|
|
a07c00 |
+ EC_POINT_free(s);
|
|
|
a07c00 |
+ BN_CTX_end(ctx);
|
|
|
a07c00 |
+ BN_CTX_free(new_ctx);
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+ return ret;
|
|
|
a07c00 |
+}
|
|
|
a07c00 |
+
|
|
|
a07c00 |
+#undef EC_POINT_BN_set_flags
|
|
|
a07c00 |
+
|
|
|
a07c00 |
/*
|
|
|
a07c00 |
* TODO: table should be optimised for the wNAF-based implementation,
|
|
|
a07c00 |
* sometimes smaller windows will give better performance (thus the
|
|
|
a07c00 |
@@ -365,6 +583,34 @@ int ec_wNAF_mul(const EC_GROUP *group, E
|
|
|
a07c00 |
return EC_POINT_set_to_infinity(group, r);
|
|
|
a07c00 |
}
|
|
|
a07c00 |
|
|
|
a07c00 |
+ if (!BN_is_zero(&group->order) && !BN_is_zero(&group->cofactor)) {
|
|
|
a07c00 |
+ /*-
|
|
|
a07c00 |
+ * Handle the common cases where the scalar is secret, enforcing a constant
|
|
|
a07c00 |
+ * time scalar multiplication algorithm.
|
|
|
a07c00 |
+ */
|
|
|
a07c00 |
+ if ((scalar != NULL) && (num == 0)) {
|
|
|
a07c00 |
+ /*-
|
|
|
a07c00 |
+ * In this case we want to compute scalar * GeneratorPoint: this
|
|
|
a07c00 |
+ * codepath is reached most prominently by (ephemeral) key generation
|
|
|
a07c00 |
+ * of EC cryptosystems (i.e. ECDSA keygen and sign setup, ECDH
|
|
|
a07c00 |
+ * keygen/first half), where the scalar is always secret. This is why
|
|
|
a07c00 |
+ * we ignore if BN_FLG_CONSTTIME is actually set and we always call the
|
|
|
a07c00 |
+ * constant time version.
|
|
|
a07c00 |
+ */
|
|
|
a07c00 |
+ return ec_mul_consttime(group, r, scalar, NULL, ctx);
|
|
|
a07c00 |
+ }
|
|
|
a07c00 |
+ if ((scalar == NULL) && (num == 1)) {
|
|
|
a07c00 |
+ /*-
|
|
|
a07c00 |
+ * In this case we want to compute scalar * GenericPoint: this codepath
|
|
|
a07c00 |
+ * is reached most prominently by the second half of ECDH, where the
|
|
|
a07c00 |
+ * secret scalar is multiplied by the peer's public point. To protect
|
|
|
a07c00 |
+ * the secret scalar, we ignore if BN_FLG_CONSTTIME is actually set and
|
|
|
a07c00 |
+ * we always call the constant time version.
|
|
|
a07c00 |
+ */
|
|
|
a07c00 |
+ return ec_mul_consttime(group, r, scalars[0], points[0], ctx);
|
|
|
a07c00 |
+ }
|
|
|
a07c00 |
+ }
|
|
|
a07c00 |
+
|
|
|
a07c00 |
for (i = 0; i < num; i++) {
|
|
|
a07c00 |
if (group->meth != points[i]->meth) {
|
|
|
a07c00 |
ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
|