/*
- * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
*out = (int)word;
st = 1;
-err:
+ err:
BN_free(ret);
return st;
}
return 0;
}
-
/*
* Return a "random" flag for if a BN should be negated.
*/
return sign[(neg++) % 8];
}
-
static int test_swap(void)
{
BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
BN_bntest_rand(b, 1024, 1, 0);
BN_copy(c, a);
BN_copy(d, b);
- top = BN_num_bits(a)/BN_BITS2;
+ top = BN_num_bits(a) / BN_BITS2;
/* regular swap */
BN_swap(a, b);
goto err;
}
st = 1;
-err:
+ err:
BN_free(a);
BN_free(b);
BN_free(c);
return st;
}
-
static int test_div_recip(void)
{
BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
goto err;
}
st = 1;
-err:
+ err:
BN_free(a);
BN_free(b);
BN_free(c);
return st;
}
-
static int test_mod(void)
{
BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
goto err;
}
st = 1;
-err:
+ err:
BN_free(a);
BN_free(b);
BN_free(c);
st = 1;
-err:
+ err:
BN_MONT_CTX_free(mont);
BN_free(a);
BN_free(p);
}
st = 1;
-err:
+ err:
BN_free(a);
BN_free(b);
BN_free(sum);
goto err;
st = 1;
-err:
+ err:
BN_free(a);
BN_free(lshift1);
BN_free(zero);
goto err;
st = 1;
-err:
+ err:
BN_free(a);
BN_free(lshift);
BN_free(ret);
}
st = 1;
-err:
+ err:
BN_free(a);
BN_free(rshift);
BN_free(ret);
#endif
st = 1;
-err:
+ err:
BN_free(a);
BN_free(square);
BN_free(zero);
goto err;
st = 1;
-err:
+ err:
BN_free(a);
BN_free(b);
BN_free(product);
}
st = 1;
-err:
+ err:
BN_free(a);
BN_free(b);
BN_free(quotient);
}
st = 1;
-err:
+ err:
BN_free(a);
BN_free(b);
BN_free(m);
goto err;
st = 1;
-err:
+ err:
BN_free(a);
BN_free(b);
BN_free(c);
goto err;
st = 1;
-err:
+ err:
BN_free(a);
BN_free(e);
BN_free(exp);
goto err;
st = 1;
-err:
+ err:
BN_free(a);
BN_free(p);
BN_free(mod_sqrt);
/* Test a random numbers at various byte lengths. */
for (size_t bytes = 128 - 7; bytes <= 128; bytes++) {
-#define TOP_BIT_ON 0
-#define BOTTOM_BIT_NOTOUCH 0
+# define TOP_BIT_ON 0
+# define BOTTOM_BIT_NOTOUCH 0
if (!TEST_true(BN_rand(n, bytes * 8, TOP_BIT_ON, BOTTOM_BIT_NOTOUCH)))
goto err;
if (!TEST_int_eq(BN_num_bytes(n),A) bytes
}
st = 1;
-err:
+ err:
BN_free(n);
return st;
#else
goto err;
st = 1;
-err:
+ err:
BN_free(bn);
return st;
}
goto err;
st = 1;
-err:
+ err:
BN_free(bn);
return st;
}
goto err;
st = 1;
-err:
+ err:
BN_free(bn);
return st;
}
BN_free(bn2);
st = 1;
-err:
+ err:
BN_free(bn);
return st;
}
goto err;
st = 1;
-err:
+ err:
BN_free(bn);
return st;
}
goto err;
st = 1;
-err:
+ err:
BN_free(a);
BN_free(b);
BN_free(c);
ERR_clear_error();
if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
- zero, ctx, NULL)))
+ zero, ctx, NULL)))
goto err;
ERR_clear_error();
ERR_clear_error();
if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
- b, ctx, NULL)))
+ b, ctx, NULL)))
goto err;
ERR_clear_error();
st = 1;
-err:
+ err:
BN_free(a);
BN_free(b);
BN_free(zero);
goto err;
st = 1;
-err:
+ err:
BN_free(zero);
BN_free(a);
BN_free(r);
}
ret = 1;
-err:
+ err:
BN_free(r);
BN_free(a);
BN_free(p);
goto err;
st = 1;
-err:
+ err:
BN_free(r);
return st;
}
}
ret = 1;
-err:
+ err:
BN_free(r);
return ret;
}
}
ret = 1;
-err:
+ err:
BN_free(r);
return ret;
}
+static int test_ctx_set_ct_flag(BN_CTX *c)
+{
+ int st = 0;
+ size_t i;
+ BIGNUM *b[15];
+
+ BN_CTX_start(c);
+ for (i = 0; i < OSSL_NELEM(b); i++) {
+ if (!TEST_ptr(b[i] = BN_CTX_get(c)))
+ goto err;
+ if (i % 2 == 1)
+ BN_set_flags(b[i], BN_FLG_CONSTTIME);
+ }
+
+ st = 1;
+ err:
+ BN_CTX_end(c);
+ return st;
+}
+
+static int test_ctx_check_ct_flag(BN_CTX *c)
+{
+ int st = 0;
+ size_t i;
+ BIGNUM *b[30];
+
+ BN_CTX_start(c);
+ for (i = 0; i < OSSL_NELEM(b); i++) {
+ if (!TEST_ptr(b[i] = BN_CTX_get(c)))
+ goto err;
+ if (!TEST_false(BN_get_flags(b[i], BN_FLG_CONSTTIME)))
+ goto err;
+ }
+
+ st = 1;
+ err:
+ BN_CTX_end(c);
+ return st;
+}
+
+static int test_ctx_consttime_flag(void)
+{
+ /*-
+ * The constant-time flag should not "leak" among BN_CTX frames:
+ *
+ * - test_ctx_set_ct_flag() starts a frame in the given BN_CTX and
+ * sets the BN_FLG_CONSTTIME flag on some of the BIGNUMs obtained
+ * from the frame before ending it.
+ * - test_ctx_check_ct_flag() then starts a new frame and gets a
+ * number of BIGNUMs from it. In absence of leaks, none of the
+ * BIGNUMs in the new frame should have BN_FLG_CONSTTIME set.
+ *
+ * In actual BN_CTX usage inside libcrypto the leak could happen at
+ * any depth level in the BN_CTX stack, with varying results
+ * depending on the patterns of sibling trees of nested function
+ * calls sharing the same BN_CTX object, and the effect of
+ * unintended BN_FLG_CONSTTIME on the called BN_* functions.
+ *
+ * This simple unit test abstracts away this complexity and verifies
+ * that the leak does not happen between two sibling functions
+ * sharing the same BN_CTX object at the same level of nesting.
+ *
+ */
+ BN_CTX *nctx = NULL;
+ BN_CTX *sctx = NULL;
+ size_t i = 0;
+ int st = 0;
+
+ if (!TEST_ptr(nctx = BN_CTX_new())
+ || !TEST_ptr(sctx = BN_CTX_secure_new()))
+ goto err;
+
+ for (i = 0; i < 2; i++) {
+ BN_CTX *c = i == 0 ? nctx : sctx;
+ if (!TEST_true(test_ctx_set_ct_flag(c))
+ || !TEST_true(test_ctx_check_ct_flag(c)))
+ goto err;
+ }
+
+ st = 1;
+ err:
+ BN_CTX_free(nctx);
+ BN_CTX_free(sctx);
+ return st;
+}
+
static int file_test_run(STANZA *s)
{
static const FILETEST filetests[] = {
ADD_TEST(test_expmodone);
ADD_TEST(test_smallprime);
ADD_TEST(test_swap);
+ ADD_TEST(test_ctx_consttime_flag);
#ifndef OPENSSL_NO_EC2M
ADD_TEST(test_gf2m_add);
ADD_TEST(test_gf2m_mod);