Some platforms provide getcontext() but it does not work
[openssl.git] / test / exptest.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
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.
7  *
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).
14  *
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.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
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)"
39  *
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
50  * SUCH DAMAGE.
51  *
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.]
56  */
57
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61
62 #include "../e_os.h"
63
64 #include <openssl/bio.h>
65 #include <openssl/bn.h>
66 #include <openssl/rand.h>
67 #include <openssl/err.h>
68
69 #define NUM_BITS        (BN_BITS2 * 4)
70
71 static const char rnd_seed[] =
72     "string to make the random number generator think it has entropy";
73
74 /*
75  * Test that r == 0 in test_exp_mod_zero(). Returns one on success,
76  * returns zero and prints debug output otherwise.
77  */
78 static int a_is_zero_mod_one(const char *method, const BIGNUM *r,
79                              const BIGNUM *a) {
80     if (!BN_is_zero(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");
88         return 0;
89     }
90     return 1;
91 }
92
93 /*
94  * test_exp_mod_zero tests that x**0 mod 1 == 0. It returns zero on success.
95  */
96 static int test_exp_mod_zero()
97 {
98     BIGNUM *a = NULL, *p = NULL, *m = NULL;
99     BIGNUM *r = NULL;
100     BN_ULONG one_word = 1;
101     BN_CTX *ctx = BN_CTX_new();
102     int ret = 1, failed = 0;
103
104     m = BN_new();
105     if (!m)
106         goto err;
107     BN_one(m);
108
109     a = BN_new();
110     if (!a)
111         goto err;
112     BN_one(a);
113
114     p = BN_new();
115     if (!p)
116         goto err;
117     BN_zero(p);
118
119     r = BN_new();
120     if (!r)
121         goto err;
122
123     if (!BN_rand(a, 1024, 0, 0))
124         goto err;
125
126     if (!BN_mod_exp(r, a, p, m, ctx))
127         goto err;
128
129     if (!a_is_zero_mod_one("BN_mod_exp", r, a))
130         failed = 1;
131
132     if (!BN_mod_exp_recp(r, a, p, m, ctx))
133         goto err;
134
135     if (!a_is_zero_mod_one("BN_mod_exp_recp", r, a))
136         failed = 1;
137
138     if (!BN_mod_exp_simple(r, a, p, m, ctx))
139         goto err;
140
141     if (!a_is_zero_mod_one("BN_mod_exp_simple", r, a))
142         failed = 1;
143
144     if (!BN_mod_exp_mont(r, a, p, m, ctx, NULL))
145         goto err;
146
147     if (!a_is_zero_mod_one("BN_mod_exp_mont", r, a))
148         failed = 1;
149
150     if (!BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL)) {
151         goto err;
152     }
153
154     if (!a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a))
155         failed = 1;
156
157     /*
158      * A different codepath exists for single word multiplication
159      * in non-constant-time only.
160      */
161     if (!BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL))
162         goto err;
163
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");
170         return 0;
171     }
172
173     ret = failed;
174
175  err:
176     BN_free(r);
177     BN_free(a);
178     BN_free(p);
179     BN_free(m);
180     BN_CTX_free(ctx);
181
182     return ret;
183 }
184
185 int main(int argc, char *argv[])
186 {
187     BN_CTX *ctx;
188     BIO *out = NULL;
189     int i, ret;
190     unsigned char c;
191     BIGNUM *r_mont, *r_mont_const, *r_recp, *r_simple, *a, *b, *m;
192
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) */
196
197     ctx = BN_CTX_new();
198     if (ctx == NULL)
199         EXIT(1);
200     r_mont = BN_new();
201     r_mont_const = BN_new();
202     r_recp = BN_new();
203     r_simple = BN_new();
204     a = BN_new();
205     b = BN_new();
206     m = BN_new();
207     if ((r_mont == NULL) || (r_recp == NULL) || (a == NULL) || (b == NULL))
208         goto err;
209
210     out = BIO_new(BIO_s_file());
211
212     if (out == NULL)
213         EXIT(1);
214     BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
215
216     for (i = 0; i < 200; i++) {
217         RAND_bytes(&c, 1);
218         c = (c % BN_BITS) - BN_BITS2;
219         BN_rand(a, NUM_BITS + c, 0, 0);
220
221         RAND_bytes(&c, 1);
222         c = (c % BN_BITS) - BN_BITS2;
223         BN_rand(b, NUM_BITS + c, 0, 0);
224
225         RAND_bytes(&c, 1);
226         c = (c % BN_BITS) - BN_BITS2;
227         BN_rand(m, NUM_BITS + c, 0, 1);
228
229         BN_mod(a, a, m, ctx);
230         BN_mod(b, b, m, ctx);
231
232         ret = BN_mod_exp_mont(r_mont, a, b, m, ctx, NULL);
233         if (ret <= 0) {
234             printf("BN_mod_exp_mont() problems\n");
235             ERR_print_errors(out);
236             EXIT(1);
237         }
238
239         ret = BN_mod_exp_recp(r_recp, a, b, m, ctx);
240         if (ret <= 0) {
241             printf("BN_mod_exp_recp() problems\n");
242             ERR_print_errors(out);
243             EXIT(1);
244         }
245
246         ret = BN_mod_exp_simple(r_simple, a, b, m, ctx);
247         if (ret <= 0) {
248             printf("BN_mod_exp_simple() problems\n");
249             ERR_print_errors(out);
250             EXIT(1);
251         }
252
253         ret = BN_mod_exp_mont_consttime(r_mont_const, a, b, m, ctx, NULL);
254         if (ret <= 0) {
255             printf("BN_mod_exp_mont_consttime() problems\n");
256             ERR_print_errors(out);
257             EXIT(1);
258         }
259
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) {
263             printf(".");
264             fflush(stdout);
265         } else {
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");
272
273             printf("a (%3d) = ", BN_num_bits(a));
274             BN_print(out, a);
275             printf("\nb (%3d) = ", BN_num_bits(b));
276             BN_print(out, b);
277             printf("\nm (%3d) = ", BN_num_bits(m));
278             BN_print(out, m);
279             printf("\nsimple   =");
280             BN_print(out, r_simple);
281             printf("\nrecp     =");
282             BN_print(out, r_recp);
283             printf("\nmont     =");
284             BN_print(out, r_mont);
285             printf("\nmont_ct  =");
286             BN_print(out, r_mont_const);
287             printf("\n");
288             EXIT(1);
289         }
290     }
291     BN_free(r_mont);
292     BN_free(r_mont_const);
293     BN_free(r_recp);
294     BN_free(r_simple);
295     BN_free(a);
296     BN_free(b);
297     BN_free(m);
298     BN_CTX_free(ctx);
299
300     if (test_exp_mod_zero() != 0)
301         goto err;
302
303 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
304     if (CRYPTO_mem_leaks(out) <= 0)
305         goto err;
306 #endif
307     BIO_free(out);
308     printf("\n");
309
310     printf("done\n");
311
312     EXIT(0);
313  err:
314     ERR_print_errors(out);
315 #ifdef OPENSSL_SYS_NETWARE
316     printf("ERROR\n");
317 #endif
318     EXIT(1);
319 }