1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
64 #include <openssl/bio.h>
65 #include <openssl/bn.h>
66 #include <openssl/rand.h>
67 #include <openssl/err.h>
69 #define NUM_BITS (BN_BITS2 * 4)
71 static const char rnd_seed[] =
72 "string to make the random number generator think it has entropy";
75 * Test that r == 0 in test_exp_mod_zero(). Returns one on success,
76 * returns zero and prints debug output otherwise.
78 static int a_is_zero_mod_one(const char *method, const BIGNUM *r,
81 fprintf(stderr, "%s failed:\n", method);
82 fprintf(stderr, "a ** 0 mod 1 = r (should be 0)\n");
83 fprintf(stderr, "a = ");
84 BN_print_fp(stderr, a);
85 fprintf(stderr, "\nr = ");
86 BN_print_fp(stderr, r);
87 fprintf(stderr, "\n");
94 * test_exp_mod_zero tests that x**0 mod 1 == 0. It returns zero on success.
96 static int test_exp_mod_zero()
98 BIGNUM *a = NULL, *p = NULL, *m = NULL;
100 BN_ULONG one_word = 1;
101 BN_CTX *ctx = BN_CTX_new();
102 int ret = 1, failed = 0;
123 if (!BN_rand(a, 1024, 0, 0))
126 if (!BN_mod_exp(r, a, p, m, ctx))
129 if (!a_is_zero_mod_one("BN_mod_exp", r, a))
132 if (!BN_mod_exp_recp(r, a, p, m, ctx))
135 if (!a_is_zero_mod_one("BN_mod_exp_recp", r, a))
138 if (!BN_mod_exp_simple(r, a, p, m, ctx))
141 if (!a_is_zero_mod_one("BN_mod_exp_simple", r, a))
144 if (!BN_mod_exp_mont(r, a, p, m, ctx, NULL))
147 if (!a_is_zero_mod_one("BN_mod_exp_mont", r, a))
150 if (!BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL)) {
154 if (!a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a))
158 * A different codepath exists for single word multiplication
159 * in non-constant-time only.
161 if (!BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL))
164 if (!BN_is_zero(r)) {
165 fprintf(stderr, "BN_mod_exp_mont_word failed:\n");
166 fprintf(stderr, "1 ** 0 mod 1 = r (should be 0)\n");
167 fprintf(stderr, "r = ");
168 BN_print_fp(stderr, r);
169 fprintf(stderr, "\n");
185 int main(int argc, char *argv[])
191 BIGNUM *r_mont, *r_mont_const, *r_recp, *r_simple, *a, *b, *m;
193 RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_rand may fail, and we
194 * don't even check its return
195 * value (which we should) */
201 r_mont_const = BN_new();
207 if ((r_mont == NULL) || (r_recp == NULL) || (a == NULL) || (b == NULL))
210 out = BIO_new(BIO_s_file());
214 BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
216 for (i = 0; i < 200; i++) {
218 c = (c % BN_BITS) - BN_BITS2;
219 BN_rand(a, NUM_BITS + c, 0, 0);
222 c = (c % BN_BITS) - BN_BITS2;
223 BN_rand(b, NUM_BITS + c, 0, 0);
226 c = (c % BN_BITS) - BN_BITS2;
227 BN_rand(m, NUM_BITS + c, 0, 1);
229 BN_mod(a, a, m, ctx);
230 BN_mod(b, b, m, ctx);
232 ret = BN_mod_exp_mont(r_mont, a, b, m, ctx, NULL);
234 printf("BN_mod_exp_mont() problems\n");
235 ERR_print_errors(out);
239 ret = BN_mod_exp_recp(r_recp, a, b, m, ctx);
241 printf("BN_mod_exp_recp() problems\n");
242 ERR_print_errors(out);
246 ret = BN_mod_exp_simple(r_simple, a, b, m, ctx);
248 printf("BN_mod_exp_simple() problems\n");
249 ERR_print_errors(out);
253 ret = BN_mod_exp_mont_consttime(r_mont_const, a, b, m, ctx, NULL);
255 printf("BN_mod_exp_mont_consttime() problems\n");
256 ERR_print_errors(out);
260 if (BN_cmp(r_simple, r_mont) == 0
261 && BN_cmp(r_simple, r_recp) == 0
262 && BN_cmp(r_simple, r_mont_const) == 0) {
266 if (BN_cmp(r_simple, r_mont) != 0)
267 printf("\nsimple and mont results differ\n");
268 if (BN_cmp(r_simple, r_mont_const) != 0)
269 printf("\nsimple and mont const time results differ\n");
270 if (BN_cmp(r_simple, r_recp) != 0)
271 printf("\nsimple and recp results differ\n");
273 printf("a (%3d) = ", BN_num_bits(a));
275 printf("\nb (%3d) = ", BN_num_bits(b));
277 printf("\nm (%3d) = ", BN_num_bits(m));
279 printf("\nsimple =");
280 BN_print(out, r_simple);
282 BN_print(out, r_recp);
284 BN_print(out, r_mont);
285 printf("\nmont_ct =");
286 BN_print(out, r_mont_const);
292 BN_free(r_mont_const);
300 if (test_exp_mod_zero() != 0)
303 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
304 if (CRYPTO_mem_leaks(out) <= 0)
314 ERR_print_errors(out);