2 * Copyright 1995-2016 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>
21 #define NUM_BITS (BN_BITS2 * 4)
23 static const char rnd_seed[] =
24 "string to make the random number generator think it has entropy";
27 * Test that r == 0 in test_exp_mod_zero(). Returns one on success,
28 * returns zero and prints debug output otherwise.
30 static int a_is_zero_mod_one(const char *method, const BIGNUM *r,
33 fprintf(stderr, "%s failed:\n", method);
34 fprintf(stderr, "a ** 0 mod 1 = r (should be 0)\n");
35 fprintf(stderr, "a = ");
36 BN_print_fp(stderr, a);
37 fprintf(stderr, "\nr = ");
38 BN_print_fp(stderr, r);
39 fprintf(stderr, "\n");
46 * test_exp_mod_zero tests that x**0 mod 1 == 0. It returns zero on success.
48 static int test_exp_mod_zero()
50 BIGNUM *a = NULL, *p = NULL, *m = NULL;
52 BN_ULONG one_word = 1;
53 BN_CTX *ctx = BN_CTX_new();
54 int ret = 1, failed = 0;
75 if (!BN_rand(a, 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
78 if (!BN_mod_exp(r, a, p, m, ctx))
81 if (!a_is_zero_mod_one("BN_mod_exp", r, a))
84 if (!BN_mod_exp_recp(r, a, p, m, ctx))
87 if (!a_is_zero_mod_one("BN_mod_exp_recp", r, a))
90 if (!BN_mod_exp_simple(r, a, p, m, ctx))
93 if (!a_is_zero_mod_one("BN_mod_exp_simple", r, a))
96 if (!BN_mod_exp_mont(r, a, p, m, ctx, NULL))
99 if (!a_is_zero_mod_one("BN_mod_exp_mont", r, a))
102 if (!BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL)) {
106 if (!a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a))
110 * A different codepath exists for single word multiplication
111 * in non-constant-time only.
113 if (!BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL))
116 if (!BN_is_zero(r)) {
117 fprintf(stderr, "BN_mod_exp_mont_word failed:\n");
118 fprintf(stderr, "1 ** 0 mod 1 = r (should be 0)\n");
119 fprintf(stderr, "r = ");
120 BN_print_fp(stderr, r);
121 fprintf(stderr, "\n");
137 int main(int argc, char *argv[])
143 BIGNUM *r_mont, *r_mont_const, *r_recp, *r_simple, *a, *b, *m;
145 RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_rand may fail, and we
146 * don't even check its return
147 * value (which we should) */
153 r_mont_const = BN_new();
159 if ((r_mont == NULL) || (r_recp == NULL) || (a == NULL) || (b == NULL))
162 out = BIO_new(BIO_s_file());
166 BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
168 for (i = 0; i < 200; i++) {
170 c = (c % BN_BITS) - BN_BITS2;
171 BN_rand(a, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
174 c = (c % BN_BITS) - BN_BITS2;
175 BN_rand(b, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
178 c = (c % BN_BITS) - BN_BITS2;
179 BN_rand(m, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD);
181 BN_mod(a, a, m, ctx);
182 BN_mod(b, b, m, ctx);
184 ret = BN_mod_exp_mont(r_mont, a, b, m, ctx, NULL);
186 printf("BN_mod_exp_mont() problems\n");
187 ERR_print_errors(out);
191 ret = BN_mod_exp_recp(r_recp, a, b, m, ctx);
193 printf("BN_mod_exp_recp() problems\n");
194 ERR_print_errors(out);
198 ret = BN_mod_exp_simple(r_simple, a, b, m, ctx);
200 printf("BN_mod_exp_simple() problems\n");
201 ERR_print_errors(out);
205 ret = BN_mod_exp_mont_consttime(r_mont_const, a, b, m, ctx, NULL);
207 printf("BN_mod_exp_mont_consttime() problems\n");
208 ERR_print_errors(out);
212 if (BN_cmp(r_simple, r_mont) == 0
213 && BN_cmp(r_simple, r_recp) == 0
214 && BN_cmp(r_simple, r_mont_const) == 0) {
218 if (BN_cmp(r_simple, r_mont) != 0)
219 printf("\nsimple and mont results differ\n");
220 if (BN_cmp(r_simple, r_mont_const) != 0)
221 printf("\nsimple and mont const time results differ\n");
222 if (BN_cmp(r_simple, r_recp) != 0)
223 printf("\nsimple and recp results differ\n");
225 printf("a (%3d) = ", BN_num_bits(a));
227 printf("\nb (%3d) = ", BN_num_bits(b));
229 printf("\nm (%3d) = ", BN_num_bits(m));
231 printf("\nsimple =");
232 BN_print(out, r_simple);
234 BN_print(out, r_recp);
236 BN_print(out, r_mont);
237 printf("\nmont_ct =");
238 BN_print(out, r_mont_const);
244 BN_free(r_mont_const);
252 if (test_exp_mod_zero() != 0)
255 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
256 if (CRYPTO_mem_leaks(out) <= 0)
266 ERR_print_errors(out);