summaryrefslogtreecommitdiffstats
path: root/test/bntest.c
diff options
context:
space:
mode:
authorMatt Caswell <matt@openssl.org>2018-05-24 16:13:43 +0100
committerMatt Caswell <matt@openssl.org>2018-05-29 16:43:18 +0100
commitadf652436a42a5132e708f8003b7621647f0a404 (patch)
tree6ea9cff5f6831523dcf2f4abda368146645caa81 /test/bntest.c
parent4aa5b725d549b3ebc3a4f2f1c44e44a11f68752b (diff)
Test that a ^ 0 mod -1 is always 0
Check all functions that do this. Reviewed-by: Rich Salz <rsalz@openssl.org> (Merged from https://github.com/openssl/openssl/pull/6355)
Diffstat (limited to 'test/bntest.c')
-rw-r--r--test/bntest.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/test/bntest.c b/test/bntest.c
index 629707ad16..35587780f3 100644
--- a/test/bntest.c
+++ b/test/bntest.c
@@ -2063,6 +2063,53 @@ err:
return st;
}
+static int test_expmodone(void)
+{
+ int ret = 0, i;
+ BIGNUM *r = BN_new();
+ BIGNUM *a = BN_new();
+ BIGNUM *p = BN_new();
+ BIGNUM *m = BN_new();
+
+ if (!TEST_ptr(r)
+ || !TEST_ptr(a)
+ || !TEST_ptr(p)
+ || !TEST_ptr(p)
+ || !TEST_ptr(m)
+ || !TEST_true(BN_set_word(a, 1))
+ || !TEST_true(BN_set_word(p, 0))
+ || !TEST_true(BN_set_word(m, 1)))
+ goto err;
+
+ /* Calculate r = 1 ^ 0 mod 1, and check the result is always 0 */
+ for (i = 0; i < 2; i++) {
+ if (!TEST_true(BN_mod_exp(r, a, p, m, NULL))
+ || !TEST_BN_eq_zero(r)
+ || !TEST_true(BN_mod_exp_mont(r, a, p, m, NULL, NULL))
+ || !TEST_BN_eq_zero(r)
+ || !TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, NULL, NULL))
+ || !TEST_BN_eq_zero(r)
+ || !TEST_true(BN_mod_exp_mont_word(r, 1, p, m, NULL, NULL))
+ || !TEST_BN_eq_zero(r)
+ || !TEST_true(BN_mod_exp_simple(r, a, p, m, NULL))
+ || !TEST_BN_eq_zero(r)
+ || !TEST_true(BN_mod_exp_recp(r, a, p, m, NULL))
+ || !TEST_BN_eq_zero(r))
+ goto err;
+ /* Repeat for r = 1 ^ 0 mod -1 */
+ if (i == 0)
+ BN_set_negative(m, 1);
+ }
+
+ ret = 1;
+err:
+ BN_free(r);
+ BN_free(a);
+ BN_free(p);
+ BN_free(m);
+ return ret;
+}
+
static int test_smallprime(void)
{
static const int kBits = 10;
@@ -2189,6 +2236,7 @@ int setup_tests(void)
ADD_TEST(test_negzero);
ADD_TEST(test_badmod);
ADD_TEST(test_expmodzero);
+ ADD_TEST(test_expmodone);
ADD_TEST(test_smallprime);
ADD_TEST(test_swap);
#ifndef OPENSSL_NO_EC2M