2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
16 #include <openssl/bio.h>
17 #include <openssl/bn.h>
18 #include <openssl/rand.h>
19 #include <openssl/err.h>
23 #define NUM_BITS (BN_BITS2 * 4)
25 #define BN_print_var(v) bn_print_var(#v, v)
27 static void bn_print_var(const char *var, const BIGNUM *bn)
29 fprintf(stderr, "%s (%3d) = ", var, BN_num_bits(bn));
30 BN_print_fp(stderr, bn);
31 fprintf(stderr, "\n");
35 * Test that r == 0 in test_exp_mod_zero(). Returns one on success,
36 * returns zero and prints debug output otherwise.
38 static int a_is_zero_mod_one(const char *method, const BIGNUM *r,
42 fprintf(stderr, "%s failed:\n", method);
43 fprintf(stderr, "a ** 0 mod 1 = r (should be 0)\n");
52 * test_mod_exp_zero tests that x**0 mod 1 == 0. It returns zero on success.
54 static int test_mod_exp_zero()
56 BIGNUM *a = NULL, *p = NULL, *m = NULL;
58 BN_ULONG one_word = 1;
59 BN_CTX *ctx = BN_CTX_new();
60 int ret = 1, failed = 0;
62 if (!TEST_ptr(m = BN_new())
63 || !TEST_ptr(a = BN_new())
64 || !TEST_ptr(p = BN_new())
65 || !TEST_ptr(r = BN_new()))
72 if (!TEST_true(BN_rand(a, 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)))
75 if (!TEST_true(BN_mod_exp(r, a, p, m, ctx)))
78 if (!TEST_true(a_is_zero_mod_one("BN_mod_exp", r, a)))
81 if (!TEST_true(BN_mod_exp_recp(r, a, p, m, ctx)))
84 if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_recp", r, a)))
87 if (!TEST_true(BN_mod_exp_simple(r, a, p, m, ctx)))
90 if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_simple", r, a)))
93 if (!TEST_true(BN_mod_exp_mont(r, a, p, m, ctx, NULL)))
96 if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont", r, a)))
99 if (!TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL)))
102 if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a)))
106 * A different codepath exists for single word multiplication
107 * in non-constant-time only.
109 if (!TEST_true(BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL)))
112 if (!TEST_true(BN_is_zero(r))) {
113 fprintf(stderr, "BN_mod_exp_mont_word failed:\n");
114 fprintf(stderr, "1 ** 0 mod 1 = r (should be 0)\n");
130 static int test_mod_exp(int round)
135 BIGNUM *r_mont = NULL;
136 BIGNUM *r_mont_const = NULL;
137 BIGNUM *r_recp = NULL;
138 BIGNUM *r_simple = NULL;
143 if (!TEST_ptr(ctx = BN_CTX_new()))
146 if (!TEST_ptr(r_mont = BN_new())
147 || !TEST_ptr(r_mont_const = BN_new())
148 || !TEST_ptr(r_recp = BN_new())
149 || !TEST_ptr(r_simple = BN_new())
150 || !TEST_ptr(a = BN_new())
151 || !TEST_ptr(b = BN_new())
152 || !TEST_ptr(m = BN_new()))
156 c = (c % BN_BITS) - BN_BITS2;
157 BN_rand(a, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
160 c = (c % BN_BITS) - BN_BITS2;
161 BN_rand(b, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
164 c = (c % BN_BITS) - BN_BITS2;
165 BN_rand(m, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD);
167 BN_mod(a, a, m, ctx);
168 BN_mod(b, b, m, ctx);
170 if (!TEST_true(BN_mod_exp_mont(r_mont, a, b, m, ctx, NULL))
171 || !TEST_true(BN_mod_exp_recp(r_recp, a, b, m, ctx))
172 || !TEST_true(BN_mod_exp_simple(r_simple, a, b, m, ctx))
173 || !TEST_true(BN_mod_exp_mont_consttime(r_mont_const, a, b, m, ctx, NULL)))
176 if (TEST_int_eq(BN_cmp(r_simple, r_mont), 0)
177 && TEST_int_eq(BN_cmp(r_simple, r_recp), 0)
178 && TEST_int_eq(BN_cmp(r_simple, r_mont_const), 0)) {
182 if (BN_cmp(r_simple, r_mont) != 0)
183 fprintf(stderr, "simple and mont results differ\n");
184 if (BN_cmp(r_simple, r_mont_const) != 0)
185 fprintf(stderr, "simple and mont const time results differ\n");
186 if (BN_cmp(r_simple, r_recp) != 0)
187 fprintf(stderr, "simple and recp results differ\n");
192 BN_print_var(r_simple);
193 BN_print_var(r_recp);
194 BN_print_var(r_mont);
195 BN_print_var(r_mont_const);
202 BN_free(r_mont_const);
213 void register_tests(void)
215 ADD_TEST(test_mod_exp_zero);
216 ADD_ALL_TESTS(test_mod_exp, 200);