1ebfedea0SLionel Sambuc /* crypto/bn/exptest.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc * All rights reserved.
4ebfedea0SLionel Sambuc *
5ebfedea0SLionel Sambuc * This package is an SSL implementation written
6ebfedea0SLionel Sambuc * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc * the following conditions are aheared to. The following conditions
11ebfedea0SLionel Sambuc * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13ebfedea0SLionel Sambuc * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc * the code are not to be removed.
18ebfedea0SLionel Sambuc * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc * are met:
26ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc * must display the following acknowledgement:
33ebfedea0SLionel Sambuc * "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc * Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc * The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc * being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc * the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc *
41ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc * SUCH DAMAGE.
52ebfedea0SLionel Sambuc *
53ebfedea0SLionel Sambuc * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc * derivative of this code cannot be changed. i.e. this code cannot simply be
55ebfedea0SLionel Sambuc * copied and put under another distribution licence
56ebfedea0SLionel Sambuc * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc */
58ebfedea0SLionel Sambuc
59ebfedea0SLionel Sambuc #include <stdio.h>
60ebfedea0SLionel Sambuc #include <stdlib.h>
61ebfedea0SLionel Sambuc #include <string.h>
62ebfedea0SLionel Sambuc
63ebfedea0SLionel Sambuc #include "../e_os.h"
64ebfedea0SLionel Sambuc
65ebfedea0SLionel Sambuc #include <openssl/bio.h>
66ebfedea0SLionel Sambuc #include <openssl/bn.h>
67ebfedea0SLionel Sambuc #include <openssl/rand.h>
68ebfedea0SLionel Sambuc #include <openssl/err.h>
69ebfedea0SLionel Sambuc
70ebfedea0SLionel Sambuc #define NUM_BITS (BN_BITS*2)
71ebfedea0SLionel Sambuc
72*0a6a1f1dSLionel Sambuc static const char rnd_seed[] =
73*0a6a1f1dSLionel Sambuc "string to make the random number generator think it has entropy";
74*0a6a1f1dSLionel Sambuc
75*0a6a1f1dSLionel Sambuc /*
76*0a6a1f1dSLionel Sambuc * test_exp_mod_zero tests that x**0 mod 1 == 0. It returns zero on success.
77*0a6a1f1dSLionel Sambuc */
test_exp_mod_zero(void)78*0a6a1f1dSLionel Sambuc static int test_exp_mod_zero(void)
79*0a6a1f1dSLionel Sambuc {
80*0a6a1f1dSLionel Sambuc BIGNUM a, p, m;
81*0a6a1f1dSLionel Sambuc BIGNUM r;
82*0a6a1f1dSLionel Sambuc BN_CTX *ctx = BN_CTX_new();
83*0a6a1f1dSLionel Sambuc int ret = 1;
84*0a6a1f1dSLionel Sambuc
85*0a6a1f1dSLionel Sambuc BN_init(&m);
86*0a6a1f1dSLionel Sambuc BN_one(&m);
87*0a6a1f1dSLionel Sambuc
88*0a6a1f1dSLionel Sambuc BN_init(&a);
89*0a6a1f1dSLionel Sambuc BN_one(&a);
90*0a6a1f1dSLionel Sambuc
91*0a6a1f1dSLionel Sambuc BN_init(&p);
92*0a6a1f1dSLionel Sambuc BN_zero(&p);
93*0a6a1f1dSLionel Sambuc
94*0a6a1f1dSLionel Sambuc BN_init(&r);
95*0a6a1f1dSLionel Sambuc BN_mod_exp(&r, &a, &p, &m, ctx);
96*0a6a1f1dSLionel Sambuc BN_CTX_free(ctx);
97*0a6a1f1dSLionel Sambuc
98*0a6a1f1dSLionel Sambuc if (BN_is_zero(&r))
99*0a6a1f1dSLionel Sambuc ret = 0;
100*0a6a1f1dSLionel Sambuc else {
101*0a6a1f1dSLionel Sambuc printf("1**0 mod 1 = ");
102*0a6a1f1dSLionel Sambuc BN_print_fp(stdout, &r);
103*0a6a1f1dSLionel Sambuc printf(", should be 0\n");
104*0a6a1f1dSLionel Sambuc }
105*0a6a1f1dSLionel Sambuc
106*0a6a1f1dSLionel Sambuc BN_free(&r);
107*0a6a1f1dSLionel Sambuc BN_free(&a);
108*0a6a1f1dSLionel Sambuc BN_free(&p);
109*0a6a1f1dSLionel Sambuc BN_free(&m);
110*0a6a1f1dSLionel Sambuc
111*0a6a1f1dSLionel Sambuc return ret;
112*0a6a1f1dSLionel Sambuc }
113ebfedea0SLionel Sambuc
main(int argc,char * argv[])114ebfedea0SLionel Sambuc int main(int argc, char *argv[])
115ebfedea0SLionel Sambuc {
116ebfedea0SLionel Sambuc BN_CTX *ctx;
117ebfedea0SLionel Sambuc BIO *out = NULL;
118ebfedea0SLionel Sambuc int i, ret;
119ebfedea0SLionel Sambuc unsigned char c;
120ebfedea0SLionel Sambuc BIGNUM *r_mont, *r_mont_const, *r_recp, *r_simple, *a, *b, *m;
121ebfedea0SLionel Sambuc
122*0a6a1f1dSLionel Sambuc RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_rand may fail, and we
123*0a6a1f1dSLionel Sambuc * don't even check its return
124*0a6a1f1dSLionel Sambuc * value (which we should) */
125ebfedea0SLionel Sambuc
126ebfedea0SLionel Sambuc ERR_load_BN_strings();
127ebfedea0SLionel Sambuc
128ebfedea0SLionel Sambuc ctx = BN_CTX_new();
129*0a6a1f1dSLionel Sambuc if (ctx == NULL)
130*0a6a1f1dSLionel Sambuc EXIT(1);
131ebfedea0SLionel Sambuc r_mont = BN_new();
132ebfedea0SLionel Sambuc r_mont_const = BN_new();
133ebfedea0SLionel Sambuc r_recp = BN_new();
134ebfedea0SLionel Sambuc r_simple = BN_new();
135ebfedea0SLionel Sambuc a = BN_new();
136ebfedea0SLionel Sambuc b = BN_new();
137ebfedea0SLionel Sambuc m = BN_new();
138*0a6a1f1dSLionel Sambuc if ((r_mont == NULL) || (r_recp == NULL) || (a == NULL) || (b == NULL))
139ebfedea0SLionel Sambuc goto err;
140ebfedea0SLionel Sambuc
141ebfedea0SLionel Sambuc out = BIO_new(BIO_s_file());
142ebfedea0SLionel Sambuc
143*0a6a1f1dSLionel Sambuc if (out == NULL)
144*0a6a1f1dSLionel Sambuc EXIT(1);
145ebfedea0SLionel Sambuc BIO_set_fp(out, stdout, BIO_NOCLOSE);
146ebfedea0SLionel Sambuc
147*0a6a1f1dSLionel Sambuc for (i = 0; i < 200; i++) {
148ebfedea0SLionel Sambuc RAND_bytes(&c, 1);
149ebfedea0SLionel Sambuc c = (c % BN_BITS) - BN_BITS2;
150ebfedea0SLionel Sambuc BN_rand(a, NUM_BITS + c, 0, 0);
151ebfedea0SLionel Sambuc
152ebfedea0SLionel Sambuc RAND_bytes(&c, 1);
153ebfedea0SLionel Sambuc c = (c % BN_BITS) - BN_BITS2;
154ebfedea0SLionel Sambuc BN_rand(b, NUM_BITS + c, 0, 0);
155ebfedea0SLionel Sambuc
156ebfedea0SLionel Sambuc RAND_bytes(&c, 1);
157ebfedea0SLionel Sambuc c = (c % BN_BITS) - BN_BITS2;
158ebfedea0SLionel Sambuc BN_rand(m, NUM_BITS + c, 0, 1);
159ebfedea0SLionel Sambuc
160ebfedea0SLionel Sambuc BN_mod(a, a, m, ctx);
161ebfedea0SLionel Sambuc BN_mod(b, b, m, ctx);
162ebfedea0SLionel Sambuc
163ebfedea0SLionel Sambuc ret = BN_mod_exp_mont(r_mont, a, b, m, ctx, NULL);
164*0a6a1f1dSLionel Sambuc if (ret <= 0) {
165ebfedea0SLionel Sambuc printf("BN_mod_exp_mont() problems\n");
166ebfedea0SLionel Sambuc ERR_print_errors(out);
167ebfedea0SLionel Sambuc EXIT(1);
168ebfedea0SLionel Sambuc }
169ebfedea0SLionel Sambuc
170ebfedea0SLionel Sambuc ret = BN_mod_exp_recp(r_recp, a, b, m, ctx);
171*0a6a1f1dSLionel Sambuc if (ret <= 0) {
172ebfedea0SLionel Sambuc printf("BN_mod_exp_recp() problems\n");
173ebfedea0SLionel Sambuc ERR_print_errors(out);
174ebfedea0SLionel Sambuc EXIT(1);
175ebfedea0SLionel Sambuc }
176ebfedea0SLionel Sambuc
177ebfedea0SLionel Sambuc ret = BN_mod_exp_simple(r_simple, a, b, m, ctx);
178*0a6a1f1dSLionel Sambuc if (ret <= 0) {
179ebfedea0SLionel Sambuc printf("BN_mod_exp_simple() problems\n");
180ebfedea0SLionel Sambuc ERR_print_errors(out);
181ebfedea0SLionel Sambuc EXIT(1);
182ebfedea0SLionel Sambuc }
183ebfedea0SLionel Sambuc
184ebfedea0SLionel Sambuc ret = BN_mod_exp_mont_consttime(r_mont_const, a, b, m, ctx, NULL);
185*0a6a1f1dSLionel Sambuc if (ret <= 0) {
186ebfedea0SLionel Sambuc printf("BN_mod_exp_mont_consttime() problems\n");
187ebfedea0SLionel Sambuc ERR_print_errors(out);
188ebfedea0SLionel Sambuc EXIT(1);
189ebfedea0SLionel Sambuc }
190ebfedea0SLionel Sambuc
191ebfedea0SLionel Sambuc if (BN_cmp(r_simple, r_mont) == 0
192ebfedea0SLionel Sambuc && BN_cmp(r_simple, r_recp) == 0
193*0a6a1f1dSLionel Sambuc && BN_cmp(r_simple, r_mont_const) == 0) {
194ebfedea0SLionel Sambuc printf(".");
195ebfedea0SLionel Sambuc fflush(stdout);
196*0a6a1f1dSLionel Sambuc } else {
197ebfedea0SLionel Sambuc if (BN_cmp(r_simple, r_mont) != 0)
198ebfedea0SLionel Sambuc printf("\nsimple and mont results differ\n");
199ebfedea0SLionel Sambuc if (BN_cmp(r_simple, r_mont_const) != 0)
200ebfedea0SLionel Sambuc printf("\nsimple and mont const time results differ\n");
201ebfedea0SLionel Sambuc if (BN_cmp(r_simple, r_recp) != 0)
202ebfedea0SLionel Sambuc printf("\nsimple and recp results differ\n");
203ebfedea0SLionel Sambuc
204*0a6a1f1dSLionel Sambuc printf("a (%3d) = ", BN_num_bits(a));
205*0a6a1f1dSLionel Sambuc BN_print(out, a);
206*0a6a1f1dSLionel Sambuc printf("\nb (%3d) = ", BN_num_bits(b));
207*0a6a1f1dSLionel Sambuc BN_print(out, b);
208*0a6a1f1dSLionel Sambuc printf("\nm (%3d) = ", BN_num_bits(m));
209*0a6a1f1dSLionel Sambuc BN_print(out, m);
210*0a6a1f1dSLionel Sambuc printf("\nsimple =");
211*0a6a1f1dSLionel Sambuc BN_print(out, r_simple);
212*0a6a1f1dSLionel Sambuc printf("\nrecp =");
213*0a6a1f1dSLionel Sambuc BN_print(out, r_recp);
214*0a6a1f1dSLionel Sambuc printf("\nmont =");
215*0a6a1f1dSLionel Sambuc BN_print(out, r_mont);
216*0a6a1f1dSLionel Sambuc printf("\nmont_ct =");
217*0a6a1f1dSLionel Sambuc BN_print(out, r_mont_const);
218ebfedea0SLionel Sambuc printf("\n");
219ebfedea0SLionel Sambuc EXIT(1);
220ebfedea0SLionel Sambuc }
221ebfedea0SLionel Sambuc }
222ebfedea0SLionel Sambuc BN_free(r_mont);
223ebfedea0SLionel Sambuc BN_free(r_mont_const);
224ebfedea0SLionel Sambuc BN_free(r_recp);
225ebfedea0SLionel Sambuc BN_free(r_simple);
226ebfedea0SLionel Sambuc BN_free(a);
227ebfedea0SLionel Sambuc BN_free(b);
228ebfedea0SLionel Sambuc BN_free(m);
229ebfedea0SLionel Sambuc BN_CTX_free(ctx);
230ebfedea0SLionel Sambuc ERR_remove_thread_state(NULL);
231ebfedea0SLionel Sambuc CRYPTO_mem_leaks(out);
232ebfedea0SLionel Sambuc BIO_free(out);
233*0a6a1f1dSLionel Sambuc printf("\n");
234*0a6a1f1dSLionel Sambuc
235*0a6a1f1dSLionel Sambuc if (test_exp_mod_zero() != 0)
236*0a6a1f1dSLionel Sambuc goto err;
237*0a6a1f1dSLionel Sambuc
238ebfedea0SLionel Sambuc printf("done\n");
239*0a6a1f1dSLionel Sambuc
240ebfedea0SLionel Sambuc EXIT(0);
241ebfedea0SLionel Sambuc err:
242ebfedea0SLionel Sambuc ERR_load_crypto_strings();
243ebfedea0SLionel Sambuc ERR_print_errors(out);
244ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_NETWARE
245ebfedea0SLionel Sambuc printf("ERROR\n");
246ebfedea0SLionel Sambuc #endif
247ebfedea0SLionel Sambuc EXIT(1);
248ebfedea0SLionel Sambuc return (1);
249ebfedea0SLionel Sambuc }
250