Provide partial support for fragmented DTLS ClientHellos
[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     ERR_load_BN_strings();
198
199     ctx = BN_CTX_new();
200     if (ctx == NULL)
201         EXIT(1);
202     r_mont = BN_new();
203     r_mont_const = BN_new();
204     r_recp = BN_new();
205     r_simple = BN_new();
206     a = BN_new();
207     b = BN_new();
208     m = BN_new();
209     if ((r_mont == NULL) || (r_recp == NULL) || (a == NULL) || (b == NULL))
210         goto err;
211
212     out = BIO_new(BIO_s_file());
213
214     if (out == NULL)
215         EXIT(1);
216     BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
217
218     for (i = 0; i < 200; i++) {
219         RAND_bytes(&c, 1);
220         c = (c % BN_BITS) - BN_BITS2;
221         BN_rand(a, NUM_BITS + c, 0, 0);
222
223         RAND_bytes(&c, 1);
224         c = (c % BN_BITS) - BN_BITS2;
225         BN_rand(b, NUM_BITS + c, 0, 0);
226
227         RAND_bytes(&c, 1);
228         c = (c % BN_BITS) - BN_BITS2;
229         BN_rand(m, NUM_BITS + c, 0, 1);
230
231         BN_mod(a, a, m, ctx);
232         BN_mod(b, b, m, ctx);
233
234         ret = BN_mod_exp_mont(r_mont, a, b, m, ctx, NULL);
235         if (ret <= 0) {
236             printf("BN_mod_exp_mont() problems\n");
237             ERR_print_errors(out);
238             EXIT(1);
239         }
240
241         ret = BN_mod_exp_recp(r_recp, a, b, m, ctx);
242         if (ret <= 0) {
243             printf("BN_mod_exp_recp() problems\n");
244             ERR_print_errors(out);
245             EXIT(1);
246         }
247
248         ret = BN_mod_exp_simple(r_simple, a, b, m, ctx);
249         if (ret <= 0) {
250             printf("BN_mod_exp_simple() problems\n");
251             ERR_print_errors(out);
252             EXIT(1);
253         }
254
255         ret = BN_mod_exp_mont_consttime(r_mont_const, a, b, m, ctx, NULL);
256         if (ret <= 0) {
257             printf("BN_mod_exp_mont_consttime() problems\n");
258             ERR_print_errors(out);
259             EXIT(1);
260         }
261
262         if (BN_cmp(r_simple, r_mont) == 0
263             && BN_cmp(r_simple, r_recp) == 0
264             && BN_cmp(r_simple, r_mont_const) == 0) {
265             printf(".");
266             fflush(stdout);
267         } else {
268             if (BN_cmp(r_simple, r_mont) != 0)
269                 printf("\nsimple and mont results differ\n");
270             if (BN_cmp(r_simple, r_mont_const) != 0)
271                 printf("\nsimple and mont const time results differ\n");
272             if (BN_cmp(r_simple, r_recp) != 0)
273                 printf("\nsimple and recp results differ\n");
274
275             printf("a (%3d) = ", BN_num_bits(a));
276             BN_print(out, a);
277             printf("\nb (%3d) = ", BN_num_bits(b));
278             BN_print(out, b);
279             printf("\nm (%3d) = ", BN_num_bits(m));
280             BN_print(out, m);
281             printf("\nsimple   =");
282             BN_print(out, r_simple);
283             printf("\nrecp     =");
284             BN_print(out, r_recp);
285             printf("\nmont     =");
286             BN_print(out, r_mont);
287             printf("\nmont_ct  =");
288             BN_print(out, r_mont_const);
289             printf("\n");
290             EXIT(1);
291         }
292     }
293     BN_free(r_mont);
294     BN_free(r_mont_const);
295     BN_free(r_recp);
296     BN_free(r_simple);
297     BN_free(a);
298     BN_free(b);
299     BN_free(m);
300     BN_CTX_free(ctx);
301     ERR_remove_thread_state(NULL);
302 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
303     if (CRYPTO_mem_leaks(out) <= 0)
304         goto err;
305 #endif
306     BIO_free(out);
307     printf("\n");
308
309     if (test_exp_mod_zero() != 0)
310         goto err;
311
312     printf("done\n");
313
314     EXIT(0);
315  err:
316     ERR_load_crypto_strings();
317     ERR_print_errors(out);
318 #ifdef OPENSSL_SYS_NETWARE
319     printf("ERROR\n");
320 #endif
321     EXIT(1);
322 }