clang on Linux x86_64 complains about unreachable code.
[openssl.git] / crypto / bn / bntest.c
1 /* crypto/bn/bntest.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 /* ====================================================================
59  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60  *
61  * Portions of the attached software ("Contribution") are developed by
62  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63  *
64  * The Contribution is licensed pursuant to the Eric Young open source
65  * license provided above.
66  *
67  * The binary polynomial arithmetic software is originally written by
68  * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
69  *
70  */
71
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75
76 #include "e_os.h"
77
78 #include <openssl/bio.h>
79 #include <openssl/bn.h>
80 #include <openssl/rand.h>
81 #include <openssl/x509.h>
82 #include <openssl/err.h>
83
84 #include "../crypto/bn/bn_lcl.h"
85
86 const int num0 = 100;           /* number of tests */
87 const int num1 = 50;            /* additional tests for some functions */
88 const int num2 = 5;             /* number of tests for slow functions */
89
90 int test_add(BIO *bp);
91 int test_sub(BIO *bp);
92 int test_lshift1(BIO *bp);
93 int test_lshift(BIO *bp, BN_CTX *ctx, BIGNUM *a_);
94 int test_rshift1(BIO *bp);
95 int test_rshift(BIO *bp, BN_CTX *ctx);
96 int test_div(BIO *bp, BN_CTX *ctx);
97 int test_div_word(BIO *bp);
98 int test_div_recp(BIO *bp, BN_CTX *ctx);
99 int test_mul(BIO *bp);
100 int test_sqr(BIO *bp, BN_CTX *ctx);
101 int test_mont(BIO *bp, BN_CTX *ctx);
102 int test_mod(BIO *bp, BN_CTX *ctx);
103 int test_mod_mul(BIO *bp, BN_CTX *ctx);
104 int test_mod_exp(BIO *bp, BN_CTX *ctx);
105 int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx);
106 int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx);
107 int test_exp(BIO *bp, BN_CTX *ctx);
108 int test_gf2m_add(BIO *bp);
109 int test_gf2m_mod(BIO *bp);
110 int test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx);
111 int test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx);
112 int test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx);
113 int test_gf2m_mod_div(BIO *bp, BN_CTX *ctx);
114 int test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx);
115 int test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx);
116 int test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx);
117 int test_kron(BIO *bp, BN_CTX *ctx);
118 int test_sqrt(BIO *bp, BN_CTX *ctx);
119 int test_small_prime(BIO *bp, BN_CTX *ctx);
120 int test_probable_prime_coprime(BIO *bp, BN_CTX *ctx);
121 int rand_neg(void);
122 static int results = 0;
123
124 static unsigned char lst[] =
125     "\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9"
126     "\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0";
127
128 static const char rnd_seed[] =
129     "string to make the random number generator think it has entropy";
130
131 static void message(BIO *out, char *m)
132 {
133     fprintf(stderr, "test %s\n", m);
134     BIO_puts(out, "print \"test ");
135     BIO_puts(out, m);
136     BIO_puts(out, "\\n\"\n");
137 }
138
139 int main(int argc, char *argv[])
140 {
141     BN_CTX *ctx;
142     BIO *out;
143     char *outfile = NULL;
144
145     results = 0;
146
147     RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
148
149     argc--;
150     argv++;
151     while (argc >= 1) {
152         if (strcmp(*argv, "-results") == 0)
153             results = 1;
154         else if (strcmp(*argv, "-out") == 0) {
155             if (--argc < 1)
156                 break;
157             outfile = *(++argv);
158         }
159         argc--;
160         argv++;
161     }
162
163     ctx = BN_CTX_new();
164     if (ctx == NULL)
165         EXIT(1);
166
167     out = BIO_new(BIO_s_file());
168     if (out == NULL)
169         EXIT(1);
170     if (outfile == NULL) {
171         BIO_set_fp(out, stdout, BIO_NOCLOSE);
172     } else {
173         if (!BIO_write_filename(out, outfile)) {
174             perror(outfile);
175             EXIT(1);
176         }
177     }
178
179     if (!results)
180         BIO_puts(out, "obase=16\nibase=16\n");
181
182     message(out, "BN_add");
183     if (!test_add(out))
184         goto err;
185     (void)BIO_flush(out);
186
187     message(out, "BN_sub");
188     if (!test_sub(out))
189         goto err;
190     (void)BIO_flush(out);
191
192     message(out, "BN_lshift1");
193     if (!test_lshift1(out))
194         goto err;
195     (void)BIO_flush(out);
196
197     message(out, "BN_lshift (fixed)");
198     if (!test_lshift(out, ctx, BN_bin2bn(lst, sizeof(lst) - 1, NULL)))
199         goto err;
200     (void)BIO_flush(out);
201
202     message(out, "BN_lshift");
203     if (!test_lshift(out, ctx, NULL))
204         goto err;
205     (void)BIO_flush(out);
206
207     message(out, "BN_rshift1");
208     if (!test_rshift1(out))
209         goto err;
210     (void)BIO_flush(out);
211
212     message(out, "BN_rshift");
213     if (!test_rshift(out, ctx))
214         goto err;
215     (void)BIO_flush(out);
216
217     message(out, "BN_sqr");
218     if (!test_sqr(out, ctx))
219         goto err;
220     (void)BIO_flush(out);
221
222     message(out, "BN_mul");
223     if (!test_mul(out))
224         goto err;
225     (void)BIO_flush(out);
226
227     message(out, "BN_div");
228     if (!test_div(out, ctx))
229         goto err;
230     (void)BIO_flush(out);
231
232     message(out, "BN_div_word");
233     if (!test_div_word(out))
234         goto err;
235     (void)BIO_flush(out);
236
237     message(out, "BN_div_recp");
238     if (!test_div_recp(out, ctx))
239         goto err;
240     (void)BIO_flush(out);
241
242     message(out, "BN_mod");
243     if (!test_mod(out, ctx))
244         goto err;
245     (void)BIO_flush(out);
246
247     message(out, "BN_mod_mul");
248     if (!test_mod_mul(out, ctx))
249         goto err;
250     (void)BIO_flush(out);
251
252     message(out, "BN_mont");
253     if (!test_mont(out, ctx))
254         goto err;
255     (void)BIO_flush(out);
256
257     message(out, "BN_mod_exp");
258     if (!test_mod_exp(out, ctx))
259         goto err;
260     (void)BIO_flush(out);
261
262     message(out, "BN_mod_exp_mont_consttime");
263     if (!test_mod_exp_mont_consttime(out, ctx))
264         goto err;
265     if (!test_mod_exp_mont5(out, ctx))
266         goto err;
267     (void)BIO_flush(out);
268
269     message(out, "BN_exp");
270     if (!test_exp(out, ctx))
271         goto err;
272     (void)BIO_flush(out);
273
274     message(out, "BN_kronecker");
275     if (!test_kron(out, ctx))
276         goto err;
277     (void)BIO_flush(out);
278
279     message(out, "BN_mod_sqrt");
280     if (!test_sqrt(out, ctx))
281         goto err;
282     (void)BIO_flush(out);
283
284     message(out, "Small prime generation");
285     if (!test_small_prime(out, ctx))
286         goto err;
287     (void)BIO_flush(out);
288
289 #ifdef OPENSSL_SYS_WIN32
290     message(out, "Probable prime generation with coprimes disabled");
291 #else
292     message(out, "Probable prime generation with coprimes");
293     if (!test_probable_prime_coprime(out, ctx))
294         goto err;
295 #endif
296     (void)BIO_flush(out);
297
298 #ifndef OPENSSL_NO_EC2M
299     message(out, "BN_GF2m_add");
300     if (!test_gf2m_add(out))
301         goto err;
302     (void)BIO_flush(out);
303
304     message(out, "BN_GF2m_mod");
305     if (!test_gf2m_mod(out))
306         goto err;
307     (void)BIO_flush(out);
308
309     message(out, "BN_GF2m_mod_mul");
310     if (!test_gf2m_mod_mul(out, ctx))
311         goto err;
312     (void)BIO_flush(out);
313
314     message(out, "BN_GF2m_mod_sqr");
315     if (!test_gf2m_mod_sqr(out, ctx))
316         goto err;
317     (void)BIO_flush(out);
318
319     message(out, "BN_GF2m_mod_inv");
320     if (!test_gf2m_mod_inv(out, ctx))
321         goto err;
322     (void)BIO_flush(out);
323
324     message(out, "BN_GF2m_mod_div");
325     if (!test_gf2m_mod_div(out, ctx))
326         goto err;
327     (void)BIO_flush(out);
328
329     message(out, "BN_GF2m_mod_exp");
330     if (!test_gf2m_mod_exp(out, ctx))
331         goto err;
332     (void)BIO_flush(out);
333
334     message(out, "BN_GF2m_mod_sqrt");
335     if (!test_gf2m_mod_sqrt(out, ctx))
336         goto err;
337     (void)BIO_flush(out);
338
339     message(out, "BN_GF2m_mod_solve_quad");
340     if (!test_gf2m_mod_solve_quad(out, ctx))
341         goto err;
342     (void)BIO_flush(out);
343 #endif
344     BN_CTX_free(ctx);
345     BIO_free(out);
346
347     EXIT(0);
348  err:
349     BIO_puts(out, "1\n");       /* make sure the Perl script fed by bc
350                                  * notices the failure, see test_bn in
351                                  * test/Makefile.ssl */
352     (void)BIO_flush(out);
353     ERR_load_crypto_strings();
354     ERR_print_errors_fp(stderr);
355     EXIT(1);
356 }
357
358 int test_add(BIO *bp)
359 {
360     BIGNUM *a, *b, *c;
361     int i;
362
363     a = BN_new();
364     b = BN_new();
365     c = BN_new();
366
367     BN_bntest_rand(a, 512, 0, 0);
368     for (i = 0; i < num0; i++) {
369         BN_bntest_rand(b, 450 + i, 0, 0);
370         a->neg = rand_neg();
371         b->neg = rand_neg();
372         BN_add(c, a, b);
373         if (bp != NULL) {
374             if (!results) {
375                 BN_print(bp, a);
376                 BIO_puts(bp, " + ");
377                 BN_print(bp, b);
378                 BIO_puts(bp, " - ");
379             }
380             BN_print(bp, c);
381             BIO_puts(bp, "\n");
382         }
383         a->neg = !a->neg;
384         b->neg = !b->neg;
385         BN_add(c, c, b);
386         BN_add(c, c, a);
387         if (!BN_is_zero(c)) {
388             fprintf(stderr, "Add test failed!\n");
389             return 0;
390         }
391     }
392     BN_free(a);
393     BN_free(b);
394     BN_free(c);
395     return (1);
396 }
397
398 int test_sub(BIO *bp)
399 {
400     BIGNUM *a, *b, *c;
401     int i;
402
403     a = BN_new();
404     b = BN_new();
405     c = BN_new();
406
407     for (i = 0; i < num0 + num1; i++) {
408         if (i < num1) {
409             BN_bntest_rand(a, 512, 0, 0);
410             BN_copy(b, a);
411             if (BN_set_bit(a, i) == 0)
412                 return (0);
413             BN_add_word(b, i);
414         } else {
415             BN_bntest_rand(b, 400 + i - num1, 0, 0);
416             a->neg = rand_neg();
417             b->neg = rand_neg();
418         }
419         BN_sub(c, a, b);
420         if (bp != NULL) {
421             if (!results) {
422                 BN_print(bp, a);
423                 BIO_puts(bp, " - ");
424                 BN_print(bp, b);
425                 BIO_puts(bp, " - ");
426             }
427             BN_print(bp, c);
428             BIO_puts(bp, "\n");
429         }
430         BN_add(c, c, b);
431         BN_sub(c, c, a);
432         if (!BN_is_zero(c)) {
433             fprintf(stderr, "Subtract test failed!\n");
434             return 0;
435         }
436     }
437     BN_free(a);
438     BN_free(b);
439     BN_free(c);
440     return (1);
441 }
442
443 int test_div(BIO *bp, BN_CTX *ctx)
444 {
445     BIGNUM *a, *b, *c, *d, *e;
446     int i;
447
448     a = BN_new();
449     b = BN_new();
450     c = BN_new();
451     d = BN_new();
452     e = BN_new();
453
454     for (i = 0; i < num0 + num1; i++) {
455         if (i < num1) {
456             BN_bntest_rand(a, 400, 0, 0);
457             BN_copy(b, a);
458             BN_lshift(a, a, i);
459             BN_add_word(a, i);
460         } else
461             BN_bntest_rand(b, 50 + 3 * (i - num1), 0, 0);
462         a->neg = rand_neg();
463         b->neg = rand_neg();
464         BN_div(d, c, a, b, ctx);
465         if (bp != NULL) {
466             if (!results) {
467                 BN_print(bp, a);
468                 BIO_puts(bp, " / ");
469                 BN_print(bp, b);
470                 BIO_puts(bp, " - ");
471             }
472             BN_print(bp, d);
473             BIO_puts(bp, "\n");
474
475             if (!results) {
476                 BN_print(bp, a);
477                 BIO_puts(bp, " % ");
478                 BN_print(bp, b);
479                 BIO_puts(bp, " - ");
480             }
481             BN_print(bp, c);
482             BIO_puts(bp, "\n");
483         }
484         BN_mul(e, d, b, ctx);
485         BN_add(d, e, c);
486         BN_sub(d, d, a);
487         if (!BN_is_zero(d)) {
488             fprintf(stderr, "Division test failed!\n");
489             return 0;
490         }
491     }
492     BN_free(a);
493     BN_free(b);
494     BN_free(c);
495     BN_free(d);
496     BN_free(e);
497     return (1);
498 }
499
500 static void print_word(BIO *bp, BN_ULONG w)
501 {
502 #ifdef SIXTY_FOUR_BIT
503     if (sizeof(w) > sizeof(unsigned long)) {
504         unsigned long h = (unsigned long)(w >> 32), l = (unsigned long)(w);
505
506         if (h)
507             BIO_printf(bp, "%lX%08lX", h, l);
508         else
509             BIO_printf(bp, "%lX", l);
510         return;
511     }
512 #endif
513     BIO_printf(bp, BN_HEX_FMT1, w);
514 }
515
516 int test_div_word(BIO *bp)
517 {
518     BIGNUM *a, *b;
519     BN_ULONG r, s;
520     int i;
521
522     a = BN_new();
523     b = BN_new();
524
525     for (i = 0; i < num0; i++) {
526         do {
527             BN_bntest_rand(a, 512, -1, 0);
528             BN_bntest_rand(b, BN_BITS2, -1, 0);
529             s = b->d[0];
530         } while (!s);
531
532         BN_copy(b, a);
533         r = BN_div_word(b, s);
534
535         if (bp != NULL) {
536             if (!results) {
537                 BN_print(bp, a);
538                 BIO_puts(bp, " / ");
539                 print_word(bp, s);
540                 BIO_puts(bp, " - ");
541             }
542             BN_print(bp, b);
543             BIO_puts(bp, "\n");
544
545             if (!results) {
546                 BN_print(bp, a);
547                 BIO_puts(bp, " % ");
548                 print_word(bp, s);
549                 BIO_puts(bp, " - ");
550             }
551             print_word(bp, r);
552             BIO_puts(bp, "\n");
553         }
554         BN_mul_word(b, s);
555         BN_add_word(b, r);
556         BN_sub(b, a, b);
557         if (!BN_is_zero(b)) {
558             fprintf(stderr, "Division (word) test failed!\n");
559             return 0;
560         }
561     }
562     BN_free(a);
563     BN_free(b);
564     return (1);
565 }
566
567 int test_div_recp(BIO *bp, BN_CTX *ctx)
568 {
569     BIGNUM *a, *b, *c, *d, *e;
570     BN_RECP_CTX *recp;
571     int i;
572
573     recp = BN_RECP_CTX_new();
574     a = BN_new();
575     b = BN_new();
576     c = BN_new();
577     d = BN_new();
578     e = BN_new();
579
580     for (i = 0; i < num0 + num1; i++) {
581         if (i < num1) {
582             BN_bntest_rand(a, 400, 0, 0);
583             BN_copy(b, a);
584             BN_lshift(a, a, i);
585             BN_add_word(a, i);
586         } else
587             BN_bntest_rand(b, 50 + 3 * (i - num1), 0, 0);
588         a->neg = rand_neg();
589         b->neg = rand_neg();
590         BN_RECP_CTX_set(recp, b, ctx);
591         BN_div_recp(d, c, a, recp, ctx);
592         if (bp != NULL) {
593             if (!results) {
594                 BN_print(bp, a);
595                 BIO_puts(bp, " / ");
596                 BN_print(bp, b);
597                 BIO_puts(bp, " - ");
598             }
599             BN_print(bp, d);
600             BIO_puts(bp, "\n");
601
602             if (!results) {
603                 BN_print(bp, a);
604                 BIO_puts(bp, " % ");
605                 BN_print(bp, b);
606                 BIO_puts(bp, " - ");
607             }
608             BN_print(bp, c);
609             BIO_puts(bp, "\n");
610         }
611         BN_mul(e, d, b, ctx);
612         BN_add(d, e, c);
613         BN_sub(d, d, a);
614         if (!BN_is_zero(d)) {
615             fprintf(stderr, "Reciprocal division test failed!\n");
616             fprintf(stderr, "a=");
617             BN_print_fp(stderr, a);
618             fprintf(stderr, "\nb=");
619             BN_print_fp(stderr, b);
620             fprintf(stderr, "\n");
621             return 0;
622         }
623     }
624     BN_free(a);
625     BN_free(b);
626     BN_free(c);
627     BN_free(d);
628     BN_free(e);
629     BN_RECP_CTX_free(recp);
630     return (1);
631 }
632
633 int test_mul(BIO *bp)
634 {
635     BIGNUM *a, *b, *c, *d, *e;
636     int i;
637     BN_CTX *ctx;
638
639     ctx = BN_CTX_new();
640     if (ctx == NULL)
641         EXIT(1);
642
643     a = BN_new();
644     b = BN_new();
645     c = BN_new();
646     d = BN_new();
647     e = BN_new();
648
649     for (i = 0; i < num0 + num1; i++) {
650         if (i <= num1) {
651             BN_bntest_rand(a, 100, 0, 0);
652             BN_bntest_rand(b, 100, 0, 0);
653         } else
654             BN_bntest_rand(b, i - num1, 0, 0);
655         a->neg = rand_neg();
656         b->neg = rand_neg();
657         BN_mul(c, a, b, ctx);
658         if (bp != NULL) {
659             if (!results) {
660                 BN_print(bp, a);
661                 BIO_puts(bp, " * ");
662                 BN_print(bp, b);
663                 BIO_puts(bp, " - ");
664             }
665             BN_print(bp, c);
666             BIO_puts(bp, "\n");
667         }
668         BN_div(d, e, c, a, ctx);
669         BN_sub(d, d, b);
670         if (!BN_is_zero(d) || !BN_is_zero(e)) {
671             fprintf(stderr, "Multiplication test failed!\n");
672             return 0;
673         }
674     }
675     BN_free(a);
676     BN_free(b);
677     BN_free(c);
678     BN_free(d);
679     BN_free(e);
680     BN_CTX_free(ctx);
681     return (1);
682 }
683
684 int test_sqr(BIO *bp, BN_CTX *ctx)
685 {
686     BIGNUM *a, *c, *d, *e;
687     int i, ret = 0;
688
689     a = BN_new();
690     c = BN_new();
691     d = BN_new();
692     e = BN_new();
693     if (a == NULL || c == NULL || d == NULL || e == NULL) {
694         goto err;
695     }
696
697     for (i = 0; i < num0; i++) {
698         BN_bntest_rand(a, 40 + i * 10, 0, 0);
699         a->neg = rand_neg();
700         BN_sqr(c, a, ctx);
701         if (bp != NULL) {
702             if (!results) {
703                 BN_print(bp, a);
704                 BIO_puts(bp, " * ");
705                 BN_print(bp, a);
706                 BIO_puts(bp, " - ");
707             }
708             BN_print(bp, c);
709             BIO_puts(bp, "\n");
710         }
711         BN_div(d, e, c, a, ctx);
712         BN_sub(d, d, a);
713         if (!BN_is_zero(d) || !BN_is_zero(e)) {
714             fprintf(stderr, "Square test failed!\n");
715             goto err;
716         }
717     }
718
719     /* Regression test for a BN_sqr overflow bug. */
720     BN_hex2bn(&a,
721               "80000000000000008000000000000001"
722               "FFFFFFFFFFFFFFFE0000000000000000");
723     BN_sqr(c, a, ctx);
724     if (bp != NULL) {
725         if (!results) {
726             BN_print(bp, a);
727             BIO_puts(bp, " * ");
728             BN_print(bp, a);
729             BIO_puts(bp, " - ");
730         }
731         BN_print(bp, c);
732         BIO_puts(bp, "\n");
733     }
734     BN_mul(d, a, a, ctx);
735     if (BN_cmp(c, d)) {
736         fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "
737                 "different results!\n");
738         goto err;
739     }
740
741     /* Regression test for a BN_sqr overflow bug. */
742     BN_hex2bn(&a,
743               "80000000000000000000000080000001"
744               "FFFFFFFE000000000000000000000000");
745     BN_sqr(c, a, ctx);
746     if (bp != NULL) {
747         if (!results) {
748             BN_print(bp, a);
749             BIO_puts(bp, " * ");
750             BN_print(bp, a);
751             BIO_puts(bp, " - ");
752         }
753         BN_print(bp, c);
754         BIO_puts(bp, "\n");
755     }
756     BN_mul(d, a, a, ctx);
757     if (BN_cmp(c, d)) {
758         fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "
759                 "different results!\n");
760         goto err;
761     }
762     ret = 1;
763  err:
764     if (a != NULL)
765         BN_free(a);
766     if (c != NULL)
767         BN_free(c);
768     if (d != NULL)
769         BN_free(d);
770     if (e != NULL)
771         BN_free(e);
772     return ret;
773 }
774
775 int test_mont(BIO *bp, BN_CTX *ctx)
776 {
777     BIGNUM *a, *b, *c, *d, *A, *B;
778     BIGNUM *n;
779     int i;
780     BN_MONT_CTX *mont;
781
782     a = BN_new();
783     b = BN_new();
784     c = BN_new();
785     d = BN_new();
786     A = BN_new();
787     B = BN_new();
788     n = BN_new();
789
790     mont = BN_MONT_CTX_new();
791     if (mont == NULL)
792         return 0;
793
794     BN_bntest_rand(a, 100, 0, 0);
795     BN_bntest_rand(b, 100, 0, 0);
796     for (i = 0; i < num2; i++) {
797         int bits = (200 * (i + 1)) / num2;
798
799         if (bits == 0)
800             continue;
801         BN_bntest_rand(n, bits, 0, 1);
802         BN_MONT_CTX_set(mont, n, ctx);
803
804         BN_nnmod(a, a, n, ctx);
805         BN_nnmod(b, b, n, ctx);
806
807         BN_to_montgomery(A, a, mont, ctx);
808         BN_to_montgomery(B, b, mont, ctx);
809
810         BN_mod_mul_montgomery(c, A, B, mont, ctx);
811         BN_from_montgomery(A, c, mont, ctx);
812         if (bp != NULL) {
813             if (!results) {
814                 BN_print(bp, a);
815                 BIO_puts(bp, " * ");
816                 BN_print(bp, b);
817                 BIO_puts(bp, " % ");
818                 BN_print(bp, &mont->N);
819                 BIO_puts(bp, " - ");
820             }
821             BN_print(bp, A);
822             BIO_puts(bp, "\n");
823         }
824         BN_mod_mul(d, a, b, n, ctx);
825         BN_sub(d, d, A);
826         if (!BN_is_zero(d)) {
827             fprintf(stderr, "Montgomery multiplication test failed!\n");
828             return 0;
829         }
830     }
831     BN_MONT_CTX_free(mont);
832     BN_free(a);
833     BN_free(b);
834     BN_free(c);
835     BN_free(d);
836     BN_free(A);
837     BN_free(B);
838     BN_free(n);
839     return (1);
840 }
841
842 int test_mod(BIO *bp, BN_CTX *ctx)
843 {
844     BIGNUM *a, *b, *c, *d, *e;
845     int i;
846
847     a = BN_new();
848     b = BN_new();
849     c = BN_new();
850     d = BN_new();
851     e = BN_new();
852
853     BN_bntest_rand(a, 1024, 0, 0);
854     for (i = 0; i < num0; i++) {
855         BN_bntest_rand(b, 450 + i * 10, 0, 0);
856         a->neg = rand_neg();
857         b->neg = rand_neg();
858         BN_mod(c, a, b, ctx);
859         if (bp != NULL) {
860             if (!results) {
861                 BN_print(bp, a);
862                 BIO_puts(bp, " % ");
863                 BN_print(bp, b);
864                 BIO_puts(bp, " - ");
865             }
866             BN_print(bp, c);
867             BIO_puts(bp, "\n");
868         }
869         BN_div(d, e, a, b, ctx);
870         BN_sub(e, e, c);
871         if (!BN_is_zero(e)) {
872             fprintf(stderr, "Modulo test failed!\n");
873             return 0;
874         }
875     }
876     BN_free(a);
877     BN_free(b);
878     BN_free(c);
879     BN_free(d);
880     BN_free(e);
881     return (1);
882 }
883
884 int test_mod_mul(BIO *bp, BN_CTX *ctx)
885 {
886     BIGNUM *a, *b, *c, *d, *e;
887     int i, j;
888
889     a = BN_new();
890     b = BN_new();
891     c = BN_new();
892     d = BN_new();
893     e = BN_new();
894
895     for (j = 0; j < 3; j++) {
896         BN_bntest_rand(c, 1024, 0, 0);
897         for (i = 0; i < num0; i++) {
898             BN_bntest_rand(a, 475 + i * 10, 0, 0);
899             BN_bntest_rand(b, 425 + i * 11, 0, 0);
900             a->neg = rand_neg();
901             b->neg = rand_neg();
902             if (!BN_mod_mul(e, a, b, c, ctx)) {
903                 unsigned long l;
904
905                 while ((l = ERR_get_error()))
906                     fprintf(stderr, "ERROR:%s\n", ERR_error_string(l, NULL));
907                 EXIT(1);
908             }
909             if (bp != NULL) {
910                 if (!results) {
911                     BN_print(bp, a);
912                     BIO_puts(bp, " * ");
913                     BN_print(bp, b);
914                     BIO_puts(bp, " % ");
915                     BN_print(bp, c);
916                     if ((a->neg ^ b->neg) && !BN_is_zero(e)) {
917                         /*
918                          * If (a*b) % c is negative, c must be added in order
919                          * to obtain the normalized remainder (new with
920                          * OpenSSL 0.9.7, previous versions of BN_mod_mul
921                          * could generate negative results)
922                          */
923                         BIO_puts(bp, " + ");
924                         BN_print(bp, c);
925                     }
926                     BIO_puts(bp, " - ");
927                 }
928                 BN_print(bp, e);
929                 BIO_puts(bp, "\n");
930             }
931             BN_mul(d, a, b, ctx);
932             BN_sub(d, d, e);
933             BN_div(a, b, d, c, ctx);
934             if (!BN_is_zero(b)) {
935                 fprintf(stderr, "Modulo multiply test failed!\n");
936                 ERR_print_errors_fp(stderr);
937                 return 0;
938             }
939         }
940     }
941     BN_free(a);
942     BN_free(b);
943     BN_free(c);
944     BN_free(d);
945     BN_free(e);
946     return (1);
947 }
948
949 int test_mod_exp(BIO *bp, BN_CTX *ctx)
950 {
951     BIGNUM *a, *b, *c, *d, *e;
952     int i;
953
954     a = BN_new();
955     b = BN_new();
956     c = BN_new();
957     d = BN_new();
958     e = BN_new();
959
960     BN_bntest_rand(c, 30, 0, 1); /* must be odd for montgomery */
961     for (i = 0; i < num2; i++) {
962         BN_bntest_rand(a, 20 + i * 5, 0, 0);
963         BN_bntest_rand(b, 2 + i, 0, 0);
964
965         if (!BN_mod_exp(d, a, b, c, ctx))
966             return (0);
967
968         if (bp != NULL) {
969             if (!results) {
970                 BN_print(bp, a);
971                 BIO_puts(bp, " ^ ");
972                 BN_print(bp, b);
973                 BIO_puts(bp, " % ");
974                 BN_print(bp, c);
975                 BIO_puts(bp, " - ");
976             }
977             BN_print(bp, d);
978             BIO_puts(bp, "\n");
979         }
980         BN_exp(e, a, b, ctx);
981         BN_sub(e, e, d);
982         BN_div(a, b, e, c, ctx);
983         if (!BN_is_zero(b)) {
984             fprintf(stderr, "Modulo exponentiation test failed!\n");
985             return 0;
986         }
987     }
988     BN_free(a);
989     BN_free(b);
990     BN_free(c);
991     BN_free(d);
992     BN_free(e);
993     return (1);
994 }
995
996 int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx)
997 {
998     BIGNUM *a, *b, *c, *d, *e;
999     int i;
1000
1001     a = BN_new();
1002     b = BN_new();
1003     c = BN_new();
1004     d = BN_new();
1005     e = BN_new();
1006
1007     BN_bntest_rand(c, 30, 0, 1); /* must be odd for montgomery */
1008     for (i = 0; i < num2; i++) {
1009         BN_bntest_rand(a, 20 + i * 5, 0, 0);
1010         BN_bntest_rand(b, 2 + i, 0, 0);
1011
1012         if (!BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL))
1013             return (00);
1014
1015         if (bp != NULL) {
1016             if (!results) {
1017                 BN_print(bp, a);
1018                 BIO_puts(bp, " ^ ");
1019                 BN_print(bp, b);
1020                 BIO_puts(bp, " % ");
1021                 BN_print(bp, c);
1022                 BIO_puts(bp, " - ");
1023             }
1024             BN_print(bp, d);
1025             BIO_puts(bp, "\n");
1026         }
1027         BN_exp(e, a, b, ctx);
1028         BN_sub(e, e, d);
1029         BN_div(a, b, e, c, ctx);
1030         if (!BN_is_zero(b)) {
1031             fprintf(stderr, "Modulo exponentiation test failed!\n");
1032             return 0;
1033         }
1034     }
1035     BN_free(a);
1036     BN_free(b);
1037     BN_free(c);
1038     BN_free(d);
1039     BN_free(e);
1040     return (1);
1041 }
1042
1043 /*
1044  * Test constant-time modular exponentiation with 1024-bit inputs, which on
1045  * x86_64 cause a different code branch to be taken.
1046  */
1047 int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx)
1048 {
1049     BIGNUM *a, *p, *m, *d, *e;
1050
1051     BN_MONT_CTX *mont;
1052
1053     a = BN_new();
1054     p = BN_new();
1055     m = BN_new();
1056     d = BN_new();
1057     e = BN_new();
1058
1059     mont = BN_MONT_CTX_new();
1060
1061     BN_bntest_rand(m, 1024, 0, 1); /* must be odd for montgomery */
1062     /* Zero exponent */
1063     BN_bntest_rand(a, 1024, 0, 0);
1064     BN_zero(p);
1065     if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
1066         return 0;
1067     if (!BN_is_one(d)) {
1068         fprintf(stderr, "Modular exponentiation test failed!\n");
1069         return 0;
1070     }
1071     /* Zero input */
1072     BN_bntest_rand(p, 1024, 0, 0);
1073     BN_zero(a);
1074     if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
1075         return 0;
1076     if (!BN_is_zero(d)) {
1077         fprintf(stderr, "Modular exponentiation test failed!\n");
1078         return 0;
1079     }
1080     /*
1081      * Craft an input whose Montgomery representation is 1, i.e., shorter
1082      * than the modulus m, in order to test the const time precomputation
1083      * scattering/gathering.
1084      */
1085     BN_one(a);
1086     BN_MONT_CTX_set(mont, m, ctx);
1087     if (!BN_from_montgomery(e, a, mont, ctx))
1088         return 0;
1089     if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
1090         return 0;
1091     if (!BN_mod_exp_simple(a, e, p, m, ctx))
1092         return 0;
1093     if (BN_cmp(a, d) != 0) {
1094         fprintf(stderr, "Modular exponentiation test failed!\n");
1095         return 0;
1096     }
1097     /* Finally, some regular test vectors. */
1098     BN_bntest_rand(e, 1024, 0, 0);
1099     if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
1100         return 0;
1101     if (!BN_mod_exp_simple(a, e, p, m, ctx))
1102         return 0;
1103     if (BN_cmp(a, d) != 0) {
1104         fprintf(stderr, "Modular exponentiation test failed!\n");
1105         return 0;
1106     }
1107     BN_free(a);
1108     BN_free(p);
1109     BN_free(m);
1110     BN_free(d);
1111     BN_free(e);
1112     return (1);
1113 }
1114
1115 int test_exp(BIO *bp, BN_CTX *ctx)
1116 {
1117     BIGNUM *a, *b, *d, *e, *one;
1118     int i;
1119
1120     a = BN_new();
1121     b = BN_new();
1122     d = BN_new();
1123     e = BN_new();
1124     one = BN_new();
1125     BN_one(one);
1126
1127     for (i = 0; i < num2; i++) {
1128         BN_bntest_rand(a, 20 + i * 5, 0, 0);
1129         BN_bntest_rand(b, 2 + i, 0, 0);
1130
1131         if (BN_exp(d, a, b, ctx) <= 0)
1132             return (0);
1133
1134         if (bp != NULL) {
1135             if (!results) {
1136                 BN_print(bp, a);
1137                 BIO_puts(bp, " ^ ");
1138                 BN_print(bp, b);
1139                 BIO_puts(bp, " - ");
1140             }
1141             BN_print(bp, d);
1142             BIO_puts(bp, "\n");
1143         }
1144         BN_one(e);
1145         for (; !BN_is_zero(b); BN_sub(b, b, one))
1146             BN_mul(e, e, a, ctx);
1147         BN_sub(e, e, d);
1148         if (!BN_is_zero(e)) {
1149             fprintf(stderr, "Exponentiation test failed!\n");
1150             return 0;
1151         }
1152     }
1153     BN_free(a);
1154     BN_free(b);
1155     BN_free(d);
1156     BN_free(e);
1157     BN_free(one);
1158     return (1);
1159 }
1160
1161 #ifndef OPENSSL_NO_EC2M
1162 int test_gf2m_add(BIO *bp)
1163 {
1164     BIGNUM *a, *b, *c;
1165     int i, ret = 0;
1166
1167     a = BN_new();
1168     b = BN_new();
1169     c = BN_new();
1170
1171     for (i = 0; i < num0; i++) {
1172         BN_rand(a, 512, 0, 0);
1173         BN_copy(b, BN_value_one());
1174         a->neg = rand_neg();
1175         b->neg = rand_neg();
1176         BN_GF2m_add(c, a, b);
1177 # if 0                          /* make test uses ouput in bc but bc can't
1178                                  * handle GF(2^m) arithmetic */
1179         if (bp != NULL) {
1180             if (!results) {
1181                 BN_print(bp, a);
1182                 BIO_puts(bp, " ^ ");
1183                 BN_print(bp, b);
1184                 BIO_puts(bp, " = ");
1185             }
1186             BN_print(bp, c);
1187             BIO_puts(bp, "\n");
1188         }
1189 # endif
1190         /* Test that two added values have the correct parity. */
1191         if ((BN_is_odd(a) && BN_is_odd(c))
1192             || (!BN_is_odd(a) && !BN_is_odd(c))) {
1193             fprintf(stderr, "GF(2^m) addition test (a) failed!\n");
1194             goto err;
1195         }
1196         BN_GF2m_add(c, c, c);
1197         /* Test that c + c = 0. */
1198         if (!BN_is_zero(c)) {
1199             fprintf(stderr, "GF(2^m) addition test (b) failed!\n");
1200             goto err;
1201         }
1202     }
1203     ret = 1;
1204  err:
1205     BN_free(a);
1206     BN_free(b);
1207     BN_free(c);
1208     return ret;
1209 }
1210
1211 int test_gf2m_mod(BIO *bp)
1212 {
1213     BIGNUM *a, *b[2], *c, *d, *e;
1214     int i, j, ret = 0;
1215     int p0[] = { 163, 7, 6, 3, 0, -1 };
1216     int p1[] = { 193, 15, 0, -1 };
1217
1218     a = BN_new();
1219     b[0] = BN_new();
1220     b[1] = BN_new();
1221     c = BN_new();
1222     d = BN_new();
1223     e = BN_new();
1224
1225     BN_GF2m_arr2poly(p0, b[0]);
1226     BN_GF2m_arr2poly(p1, b[1]);
1227
1228     for (i = 0; i < num0; i++) {
1229         BN_bntest_rand(a, 1024, 0, 0);
1230         for (j = 0; j < 2; j++) {
1231             BN_GF2m_mod(c, a, b[j]);
1232 # if 0                          /* make test uses ouput in bc but bc can't
1233                                  * handle GF(2^m) arithmetic */
1234             if (bp != NULL) {
1235                 if (!results) {
1236                     BN_print(bp, a);
1237                     BIO_puts(bp, " % ");
1238                     BN_print(bp, b[j]);
1239                     BIO_puts(bp, " - ");
1240                     BN_print(bp, c);
1241                     BIO_puts(bp, "\n");
1242                 }
1243             }
1244 # endif
1245             BN_GF2m_add(d, a, c);
1246             BN_GF2m_mod(e, d, b[j]);
1247             /* Test that a + (a mod p) mod p == 0. */
1248             if (!BN_is_zero(e)) {
1249                 fprintf(stderr, "GF(2^m) modulo test failed!\n");
1250                 goto err;
1251             }
1252         }
1253     }
1254     ret = 1;
1255  err:
1256     BN_free(a);
1257     BN_free(b[0]);
1258     BN_free(b[1]);
1259     BN_free(c);
1260     BN_free(d);
1261     BN_free(e);
1262     return ret;
1263 }
1264
1265 int test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx)
1266 {
1267     BIGNUM *a, *b[2], *c, *d, *e, *f, *g, *h;
1268     int i, j, ret = 0;
1269     int p0[] = { 163, 7, 6, 3, 0, -1 };
1270     int p1[] = { 193, 15, 0, -1 };
1271
1272     a = BN_new();
1273     b[0] = BN_new();
1274     b[1] = BN_new();
1275     c = BN_new();
1276     d = BN_new();
1277     e = BN_new();
1278     f = BN_new();
1279     g = BN_new();
1280     h = BN_new();
1281
1282     BN_GF2m_arr2poly(p0, b[0]);
1283     BN_GF2m_arr2poly(p1, b[1]);
1284
1285     for (i = 0; i < num0; i++) {
1286         BN_bntest_rand(a, 1024, 0, 0);
1287         BN_bntest_rand(c, 1024, 0, 0);
1288         BN_bntest_rand(d, 1024, 0, 0);
1289         for (j = 0; j < 2; j++) {
1290             BN_GF2m_mod_mul(e, a, c, b[j], ctx);
1291 # if 0                          /* make test uses ouput in bc but bc can't
1292                                  * handle GF(2^m) arithmetic */
1293             if (bp != NULL) {
1294                 if (!results) {
1295                     BN_print(bp, a);
1296                     BIO_puts(bp, " * ");
1297                     BN_print(bp, c);
1298                     BIO_puts(bp, " % ");
1299                     BN_print(bp, b[j]);
1300                     BIO_puts(bp, " - ");
1301                     BN_print(bp, e);
1302                     BIO_puts(bp, "\n");
1303                 }
1304             }
1305 # endif
1306             BN_GF2m_add(f, a, d);
1307             BN_GF2m_mod_mul(g, f, c, b[j], ctx);
1308             BN_GF2m_mod_mul(h, d, c, b[j], ctx);
1309             BN_GF2m_add(f, e, g);
1310             BN_GF2m_add(f, f, h);
1311             /* Test that (a+d)*c = a*c + d*c. */
1312             if (!BN_is_zero(f)) {
1313                 fprintf(stderr,
1314                         "GF(2^m) modular multiplication test failed!\n");
1315                 goto err;
1316             }
1317         }
1318     }
1319     ret = 1;
1320  err:
1321     BN_free(a);
1322     BN_free(b[0]);
1323     BN_free(b[1]);
1324     BN_free(c);
1325     BN_free(d);
1326     BN_free(e);
1327     BN_free(f);
1328     BN_free(g);
1329     BN_free(h);
1330     return ret;
1331 }
1332
1333 int test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx)
1334 {
1335     BIGNUM *a, *b[2], *c, *d;
1336     int i, j, ret = 0;
1337     int p0[] = { 163, 7, 6, 3, 0, -1 };
1338     int p1[] = { 193, 15, 0, -1 };
1339
1340     a = BN_new();
1341     b[0] = BN_new();
1342     b[1] = BN_new();
1343     c = BN_new();
1344     d = BN_new();
1345
1346     BN_GF2m_arr2poly(p0, b[0]);
1347     BN_GF2m_arr2poly(p1, b[1]);
1348
1349     for (i = 0; i < num0; i++) {
1350         BN_bntest_rand(a, 1024, 0, 0);
1351         for (j = 0; j < 2; j++) {
1352             BN_GF2m_mod_sqr(c, a, b[j], ctx);
1353             BN_copy(d, a);
1354             BN_GF2m_mod_mul(d, a, d, b[j], ctx);
1355 # if 0                          /* make test uses ouput in bc but bc can't
1356                                  * handle GF(2^m) arithmetic */
1357             if (bp != NULL) {
1358                 if (!results) {
1359                     BN_print(bp, a);
1360                     BIO_puts(bp, " ^ 2 % ");
1361                     BN_print(bp, b[j]);
1362                     BIO_puts(bp, " = ");
1363                     BN_print(bp, c);
1364                     BIO_puts(bp, "; a * a = ");
1365                     BN_print(bp, d);
1366                     BIO_puts(bp, "\n");
1367                 }
1368             }
1369 # endif
1370             BN_GF2m_add(d, c, d);
1371             /* Test that a*a = a^2. */
1372             if (!BN_is_zero(d)) {
1373                 fprintf(stderr, "GF(2^m) modular squaring test failed!\n");
1374                 goto err;
1375             }
1376         }
1377     }
1378     ret = 1;
1379  err:
1380     BN_free(a);
1381     BN_free(b[0]);
1382     BN_free(b[1]);
1383     BN_free(c);
1384     BN_free(d);
1385     return ret;
1386 }
1387
1388 int test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx)
1389 {
1390     BIGNUM *a, *b[2], *c, *d;
1391     int i, j, ret = 0;
1392     int p0[] = { 163, 7, 6, 3, 0, -1 };
1393     int p1[] = { 193, 15, 0, -1 };
1394
1395     a = BN_new();
1396     b[0] = BN_new();
1397     b[1] = BN_new();
1398     c = BN_new();
1399     d = BN_new();
1400
1401     BN_GF2m_arr2poly(p0, b[0]);
1402     BN_GF2m_arr2poly(p1, b[1]);
1403
1404     for (i = 0; i < num0; i++) {
1405         BN_bntest_rand(a, 512, 0, 0);
1406         for (j = 0; j < 2; j++) {
1407             BN_GF2m_mod_inv(c, a, b[j], ctx);
1408             BN_GF2m_mod_mul(d, a, c, b[j], ctx);
1409 # if 0                          /* make test uses ouput in bc but bc can't
1410                                  * handle GF(2^m) arithmetic */
1411             if (bp != NULL) {
1412                 if (!results) {
1413                     BN_print(bp, a);
1414                     BIO_puts(bp, " * ");
1415                     BN_print(bp, c);
1416                     BIO_puts(bp, " - 1 % ");
1417                     BN_print(bp, b[j]);
1418                     BIO_puts(bp, "\n");
1419                 }
1420             }
1421 # endif
1422             /* Test that ((1/a)*a) = 1. */
1423             if (!BN_is_one(d)) {
1424                 fprintf(stderr, "GF(2^m) modular inversion test failed!\n");
1425                 goto err;
1426             }
1427         }
1428     }
1429     ret = 1;
1430  err:
1431     BN_free(a);
1432     BN_free(b[0]);
1433     BN_free(b[1]);
1434     BN_free(c);
1435     BN_free(d);
1436     return ret;
1437 }
1438
1439 int test_gf2m_mod_div(BIO *bp, BN_CTX *ctx)
1440 {
1441     BIGNUM *a, *b[2], *c, *d, *e, *f;
1442     int i, j, ret = 0;
1443     int p0[] = { 163, 7, 6, 3, 0, -1 };
1444     int p1[] = { 193, 15, 0, -1 };
1445
1446     a = BN_new();
1447     b[0] = BN_new();
1448     b[1] = BN_new();
1449     c = BN_new();
1450     d = BN_new();
1451     e = BN_new();
1452     f = BN_new();
1453
1454     BN_GF2m_arr2poly(p0, b[0]);
1455     BN_GF2m_arr2poly(p1, b[1]);
1456
1457     for (i = 0; i < num0; i++) {
1458         BN_bntest_rand(a, 512, 0, 0);
1459         BN_bntest_rand(c, 512, 0, 0);
1460         for (j = 0; j < 2; j++) {
1461             BN_GF2m_mod_div(d, a, c, b[j], ctx);
1462             BN_GF2m_mod_mul(e, d, c, b[j], ctx);
1463             BN_GF2m_mod_div(f, a, e, b[j], ctx);
1464 # if 0                          /* make test uses ouput in bc but bc can't
1465                                  * handle GF(2^m) arithmetic */
1466             if (bp != NULL) {
1467                 if (!results) {
1468                     BN_print(bp, a);
1469                     BIO_puts(bp, " = ");
1470                     BN_print(bp, c);
1471                     BIO_puts(bp, " * ");
1472                     BN_print(bp, d);
1473                     BIO_puts(bp, " % ");
1474                     BN_print(bp, b[j]);
1475                     BIO_puts(bp, "\n");
1476                 }
1477             }
1478 # endif
1479             /* Test that ((a/c)*c)/a = 1. */
1480             if (!BN_is_one(f)) {
1481                 fprintf(stderr, "GF(2^m) modular division test failed!\n");
1482                 goto err;
1483             }
1484         }
1485     }
1486     ret = 1;
1487  err:
1488     BN_free(a);
1489     BN_free(b[0]);
1490     BN_free(b[1]);
1491     BN_free(c);
1492     BN_free(d);
1493     BN_free(e);
1494     BN_free(f);
1495     return ret;
1496 }
1497
1498 int test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx)
1499 {
1500     BIGNUM *a, *b[2], *c, *d, *e, *f;
1501     int i, j, ret = 0;
1502     int p0[] = { 163, 7, 6, 3, 0, -1 };
1503     int p1[] = { 193, 15, 0, -1 };
1504
1505     a = BN_new();
1506     b[0] = BN_new();
1507     b[1] = BN_new();
1508     c = BN_new();
1509     d = BN_new();
1510     e = BN_new();
1511     f = BN_new();
1512
1513     BN_GF2m_arr2poly(p0, b[0]);
1514     BN_GF2m_arr2poly(p1, b[1]);
1515
1516     for (i = 0; i < num0; i++) {
1517         BN_bntest_rand(a, 512, 0, 0);
1518         BN_bntest_rand(c, 512, 0, 0);
1519         BN_bntest_rand(d, 512, 0, 0);
1520         for (j = 0; j < 2; j++) {
1521             BN_GF2m_mod_exp(e, a, c, b[j], ctx);
1522             BN_GF2m_mod_exp(f, a, d, b[j], ctx);
1523             BN_GF2m_mod_mul(e, e, f, b[j], ctx);
1524             BN_add(f, c, d);
1525             BN_GF2m_mod_exp(f, a, f, b[j], ctx);
1526 # if 0                          /* make test uses ouput in bc but bc can't
1527                                  * handle GF(2^m) arithmetic */
1528             if (bp != NULL) {
1529                 if (!results) {
1530                     BN_print(bp, a);
1531                     BIO_puts(bp, " ^ (");
1532                     BN_print(bp, c);
1533                     BIO_puts(bp, " + ");
1534                     BN_print(bp, d);
1535                     BIO_puts(bp, ") = ");
1536                     BN_print(bp, e);
1537                     BIO_puts(bp, "; - ");
1538                     BN_print(bp, f);
1539                     BIO_puts(bp, " % ");
1540                     BN_print(bp, b[j]);
1541                     BIO_puts(bp, "\n");
1542                 }
1543             }
1544 # endif
1545             BN_GF2m_add(f, e, f);
1546             /* Test that a^(c+d)=a^c*a^d. */
1547             if (!BN_is_zero(f)) {
1548                 fprintf(stderr,
1549                         "GF(2^m) modular exponentiation test failed!\n");
1550                 goto err;
1551             }
1552         }
1553     }
1554     ret = 1;
1555  err:
1556     BN_free(a);
1557     BN_free(b[0]);
1558     BN_free(b[1]);
1559     BN_free(c);
1560     BN_free(d);
1561     BN_free(e);
1562     BN_free(f);
1563     return ret;
1564 }
1565
1566 int test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx)
1567 {
1568     BIGNUM *a, *b[2], *c, *d, *e, *f;
1569     int i, j, ret = 0;
1570     int p0[] = { 163, 7, 6, 3, 0, -1 };
1571     int p1[] = { 193, 15, 0, -1 };
1572
1573     a = BN_new();
1574     b[0] = BN_new();
1575     b[1] = BN_new();
1576     c = BN_new();
1577     d = BN_new();
1578     e = BN_new();
1579     f = BN_new();
1580
1581     BN_GF2m_arr2poly(p0, b[0]);
1582     BN_GF2m_arr2poly(p1, b[1]);
1583
1584     for (i = 0; i < num0; i++) {
1585         BN_bntest_rand(a, 512, 0, 0);
1586         for (j = 0; j < 2; j++) {
1587             BN_GF2m_mod(c, a, b[j]);
1588             BN_GF2m_mod_sqrt(d, a, b[j], ctx);
1589             BN_GF2m_mod_sqr(e, d, b[j], ctx);
1590 # if 0                          /* make test uses ouput in bc but bc can't
1591                                  * handle GF(2^m) arithmetic */
1592             if (bp != NULL) {
1593                 if (!results) {
1594                     BN_print(bp, d);
1595                     BIO_puts(bp, " ^ 2 - ");
1596                     BN_print(bp, a);
1597                     BIO_puts(bp, "\n");
1598                 }
1599             }
1600 # endif
1601             BN_GF2m_add(f, c, e);
1602             /* Test that d^2 = a, where d = sqrt(a). */
1603             if (!BN_is_zero(f)) {
1604                 fprintf(stderr, "GF(2^m) modular square root test failed!\n");
1605                 goto err;
1606             }
1607         }
1608     }
1609     ret = 1;
1610  err:
1611     BN_free(a);
1612     BN_free(b[0]);
1613     BN_free(b[1]);
1614     BN_free(c);
1615     BN_free(d);
1616     BN_free(e);
1617     BN_free(f);
1618     return ret;
1619 }
1620
1621 int test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx)
1622 {
1623     BIGNUM *a, *b[2], *c, *d, *e;
1624     int i, j, s = 0, t, ret = 0;
1625     int p0[] = { 163, 7, 6, 3, 0, -1 };
1626     int p1[] = { 193, 15, 0, -1 };
1627
1628     a = BN_new();
1629     b[0] = BN_new();
1630     b[1] = BN_new();
1631     c = BN_new();
1632     d = BN_new();
1633     e = BN_new();
1634
1635     BN_GF2m_arr2poly(p0, b[0]);
1636     BN_GF2m_arr2poly(p1, b[1]);
1637
1638     for (i = 0; i < num0; i++) {
1639         BN_bntest_rand(a, 512, 0, 0);
1640         for (j = 0; j < 2; j++) {
1641             t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
1642             if (t) {
1643                 s++;
1644                 BN_GF2m_mod_sqr(d, c, b[j], ctx);
1645                 BN_GF2m_add(d, c, d);
1646                 BN_GF2m_mod(e, a, b[j]);
1647 # if 0                          /* make test uses ouput in bc but bc can't
1648                                  * handle GF(2^m) arithmetic */
1649                 if (bp != NULL) {
1650                     if (!results) {
1651                         BN_print(bp, c);
1652                         BIO_puts(bp, " is root of z^2 + z = ");
1653                         BN_print(bp, a);
1654                         BIO_puts(bp, " % ");
1655                         BN_print(bp, b[j]);
1656                         BIO_puts(bp, "\n");
1657                     }
1658                 }
1659 # endif
1660                 BN_GF2m_add(e, e, d);
1661                 /*
1662                  * Test that solution of quadratic c satisfies c^2 + c = a.
1663                  */
1664                 if (!BN_is_zero(e)) {
1665                     fprintf(stderr,
1666                             "GF(2^m) modular solve quadratic test failed!\n");
1667                     goto err;
1668                 }
1669
1670             } else {
1671 # if 0                          /* make test uses ouput in bc but bc can't
1672                                  * handle GF(2^m) arithmetic */
1673                 if (bp != NULL) {
1674                     if (!results) {
1675                         BIO_puts(bp, "There are no roots of z^2 + z = ");
1676                         BN_print(bp, a);
1677                         BIO_puts(bp, " % ");
1678                         BN_print(bp, b[j]);
1679                         BIO_puts(bp, "\n");
1680                     }
1681                 }
1682 # endif
1683             }
1684         }
1685     }
1686     if (s == 0) {
1687         fprintf(stderr,
1688                 "All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n",
1689                 num0);
1690         fprintf(stderr,
1691                 "this is very unlikely and probably indicates an error.\n");
1692         goto err;
1693     }
1694     ret = 1;
1695  err:
1696     BN_free(a);
1697     BN_free(b[0]);
1698     BN_free(b[1]);
1699     BN_free(c);
1700     BN_free(d);
1701     BN_free(e);
1702     return ret;
1703 }
1704 #endif
1705 static int genprime_cb(int p, int n, BN_GENCB *arg)
1706 {
1707     char c = '*';
1708
1709     if (p == 0)
1710         c = '.';
1711     if (p == 1)
1712         c = '+';
1713     if (p == 2)
1714         c = '*';
1715     if (p == 3)
1716         c = '\n';
1717     putc(c, stderr);
1718     fflush(stderr);
1719     return 1;
1720 }
1721
1722 int test_kron(BIO *bp, BN_CTX *ctx)
1723 {
1724     BN_GENCB cb;
1725     BIGNUM *a, *b, *r, *t;
1726     int i;
1727     int legendre, kronecker;
1728     int ret = 0;
1729
1730     a = BN_new();
1731     b = BN_new();
1732     r = BN_new();
1733     t = BN_new();
1734     if (a == NULL || b == NULL || r == NULL || t == NULL)
1735         goto err;
1736
1737     BN_GENCB_set(&cb, genprime_cb, NULL);
1738
1739     /*
1740      * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
1741      * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
1742      * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
1743      * generate a random prime b and compare these values for a number of
1744      * random a's.  (That is, we run the Solovay-Strassen primality test to
1745      * confirm that b is prime, except that we don't want to test whether b
1746      * is prime but whether BN_kronecker works.)
1747      */
1748
1749     if (!BN_generate_prime_ex(b, 512, 0, NULL, NULL, &cb))
1750         goto err;
1751     b->neg = rand_neg();
1752     putc('\n', stderr);
1753
1754     for (i = 0; i < num0; i++) {
1755         if (!BN_bntest_rand(a, 512, 0, 0))
1756             goto err;
1757         a->neg = rand_neg();
1758
1759         /* t := (|b|-1)/2  (note that b is odd) */
1760         if (!BN_copy(t, b))
1761             goto err;
1762         t->neg = 0;
1763         if (!BN_sub_word(t, 1))
1764             goto err;
1765         if (!BN_rshift1(t, t))
1766             goto err;
1767         /* r := a^t mod b */
1768         b->neg = 0;
1769
1770         if (!BN_mod_exp_recp(r, a, t, b, ctx))
1771             goto err;
1772         b->neg = 1;
1773
1774         if (BN_is_word(r, 1))
1775             legendre = 1;
1776         else if (BN_is_zero(r))
1777             legendre = 0;
1778         else {
1779             if (!BN_add_word(r, 1))
1780                 goto err;
1781             if (0 != BN_ucmp(r, b)) {
1782                 fprintf(stderr, "Legendre symbol computation failed\n");
1783                 goto err;
1784             }
1785             legendre = -1;
1786         }
1787
1788         kronecker = BN_kronecker(a, b, ctx);
1789         if (kronecker < -1)
1790             goto err;
1791         /* we actually need BN_kronecker(a, |b|) */
1792         if (a->neg && b->neg)
1793             kronecker = -kronecker;
1794
1795         if (legendre != kronecker) {
1796             fprintf(stderr, "legendre != kronecker; a = ");
1797             BN_print_fp(stderr, a);
1798             fprintf(stderr, ", b = ");
1799             BN_print_fp(stderr, b);
1800             fprintf(stderr, "\n");
1801             goto err;
1802         }
1803
1804         putc('.', stderr);
1805         fflush(stderr);
1806     }
1807
1808     putc('\n', stderr);
1809     fflush(stderr);
1810     ret = 1;
1811  err:
1812     if (a != NULL)
1813         BN_free(a);
1814     if (b != NULL)
1815         BN_free(b);
1816     if (r != NULL)
1817         BN_free(r);
1818     if (t != NULL)
1819         BN_free(t);
1820     return ret;
1821 }
1822
1823 int test_sqrt(BIO *bp, BN_CTX *ctx)
1824 {
1825     BN_GENCB cb;
1826     BIGNUM *a, *p, *r;
1827     int i, j;
1828     int ret = 0;
1829
1830     a = BN_new();
1831     p = BN_new();
1832     r = BN_new();
1833     if (a == NULL || p == NULL || r == NULL)
1834         goto err;
1835
1836     BN_GENCB_set(&cb, genprime_cb, NULL);
1837
1838     for (i = 0; i < 16; i++) {
1839         if (i < 8) {
1840             unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 };
1841
1842             if (!BN_set_word(p, primes[i]))
1843                 goto err;
1844         } else {
1845             if (!BN_set_word(a, 32))
1846                 goto err;
1847             if (!BN_set_word(r, 2 * i + 1))
1848                 goto err;
1849
1850             if (!BN_generate_prime_ex(p, 256, 0, a, r, &cb))
1851                 goto err;
1852             putc('\n', stderr);
1853         }
1854         p->neg = rand_neg();
1855
1856         for (j = 0; j < num2; j++) {
1857             /*
1858              * construct 'a' such that it is a square modulo p, but in
1859              * general not a proper square and not reduced modulo p
1860              */
1861             if (!BN_bntest_rand(r, 256, 0, 3))
1862                 goto err;
1863             if (!BN_nnmod(r, r, p, ctx))
1864                 goto err;
1865             if (!BN_mod_sqr(r, r, p, ctx))
1866                 goto err;
1867             if (!BN_bntest_rand(a, 256, 0, 3))
1868                 goto err;
1869             if (!BN_nnmod(a, a, p, ctx))
1870                 goto err;
1871             if (!BN_mod_sqr(a, a, p, ctx))
1872                 goto err;
1873             if (!BN_mul(a, a, r, ctx))
1874                 goto err;
1875             if (rand_neg())
1876                 if (!BN_sub(a, a, p))
1877                     goto err;
1878
1879             if (!BN_mod_sqrt(r, a, p, ctx))
1880                 goto err;
1881             if (!BN_mod_sqr(r, r, p, ctx))
1882                 goto err;
1883
1884             if (!BN_nnmod(a, a, p, ctx))
1885                 goto err;
1886
1887             if (BN_cmp(a, r) != 0) {
1888                 fprintf(stderr, "BN_mod_sqrt failed: a = ");
1889                 BN_print_fp(stderr, a);
1890                 fprintf(stderr, ", r = ");
1891                 BN_print_fp(stderr, r);
1892                 fprintf(stderr, ", p = ");
1893                 BN_print_fp(stderr, p);
1894                 fprintf(stderr, "\n");
1895                 goto err;
1896             }
1897
1898             putc('.', stderr);
1899             fflush(stderr);
1900         }
1901
1902         putc('\n', stderr);
1903         fflush(stderr);
1904     }
1905     ret = 1;
1906  err:
1907     if (a != NULL)
1908         BN_free(a);
1909     if (p != NULL)
1910         BN_free(p);
1911     if (r != NULL)
1912         BN_free(r);
1913     return ret;
1914 }
1915
1916 int test_small_prime(BIO *bp, BN_CTX *ctx)
1917 {
1918     static const int bits = 10;
1919     int ret = 0;
1920     BIGNUM *r;
1921
1922     r = BN_new();
1923     if (!BN_generate_prime_ex(r, bits, 0, NULL, NULL, NULL))
1924         goto err;
1925     if (BN_num_bits(r) != bits) {
1926         BIO_printf(bp, "Expected %d bit prime, got %d bit number\n", bits,
1927                    BN_num_bits(r));
1928         goto err;
1929     }
1930
1931     ret = 1;
1932
1933  err:
1934     BN_clear_free(r);
1935     return ret;
1936 }
1937
1938 #ifndef OPENSSL_SYS_WIN32
1939 int test_probable_prime_coprime(BIO *bp, BN_CTX *ctx)
1940 {
1941     int i, j, ret = 0;
1942     BIGNUM *r;
1943     BN_ULONG primes[5] = { 2, 3, 5, 7, 11 };
1944
1945     r = BN_new();
1946
1947     for (i = 0; i < 1000; i++) {
1948         if (!bn_probable_prime_dh_coprime(r, 1024, ctx))
1949             goto err;
1950
1951         for (j = 0; j < 5; j++) {
1952             if (BN_mod_word(r, primes[j]) == 0) {
1953                 BIO_printf(bp, "Number generated is not coprime to %ld:\n",
1954                            primes[j]);
1955                 BN_print_fp(stdout, r);
1956                 BIO_printf(bp, "\n");
1957                 goto err;
1958             }
1959         }
1960     }
1961
1962     ret = 1;
1963
1964  err:
1965     BN_clear_free(r);
1966     return ret;
1967 }
1968 #endif
1969 int test_lshift(BIO *bp, BN_CTX *ctx, BIGNUM *a_)
1970 {
1971     BIGNUM *a, *b, *c, *d;
1972     int i;
1973
1974     b = BN_new();
1975     c = BN_new();
1976     d = BN_new();
1977     BN_one(c);
1978
1979     if (a_)
1980         a = a_;
1981     else {
1982         a = BN_new();
1983         BN_bntest_rand(a, 200, 0, 0);
1984         a->neg = rand_neg();
1985     }
1986     for (i = 0; i < num0; i++) {
1987         BN_lshift(b, a, i + 1);
1988         BN_add(c, c, c);
1989         if (bp != NULL) {
1990             if (!results) {
1991                 BN_print(bp, a);
1992                 BIO_puts(bp, " * ");
1993                 BN_print(bp, c);
1994                 BIO_puts(bp, " - ");
1995             }
1996             BN_print(bp, b);
1997             BIO_puts(bp, "\n");
1998         }
1999         BN_mul(d, a, c, ctx);
2000         BN_sub(d, d, b);
2001         if (!BN_is_zero(d)) {
2002             fprintf(stderr, "Left shift test failed!\n");
2003             fprintf(stderr, "a=");
2004             BN_print_fp(stderr, a);
2005             fprintf(stderr, "\nb=");
2006             BN_print_fp(stderr, b);
2007             fprintf(stderr, "\nc=");
2008             BN_print_fp(stderr, c);
2009             fprintf(stderr, "\nd=");
2010             BN_print_fp(stderr, d);
2011             fprintf(stderr, "\n");
2012             return 0;
2013         }
2014     }
2015     BN_free(a);
2016     BN_free(b);
2017     BN_free(c);
2018     BN_free(d);
2019     return (1);
2020 }
2021
2022 int test_lshift1(BIO *bp)
2023 {
2024     BIGNUM *a, *b, *c;
2025     int i;
2026
2027     a = BN_new();
2028     b = BN_new();
2029     c = BN_new();
2030
2031     BN_bntest_rand(a, 200, 0, 0);
2032     a->neg = rand_neg();
2033     for (i = 0; i < num0; i++) {
2034         BN_lshift1(b, a);
2035         if (bp != NULL) {
2036             if (!results) {
2037                 BN_print(bp, a);
2038                 BIO_puts(bp, " * 2");
2039                 BIO_puts(bp, " - ");
2040             }
2041             BN_print(bp, b);
2042             BIO_puts(bp, "\n");
2043         }
2044         BN_add(c, a, a);
2045         BN_sub(a, b, c);
2046         if (!BN_is_zero(a)) {
2047             fprintf(stderr, "Left shift one test failed!\n");
2048             return 0;
2049         }
2050
2051         BN_copy(a, b);
2052     }
2053     BN_free(a);
2054     BN_free(b);
2055     BN_free(c);
2056     return (1);
2057 }
2058
2059 int test_rshift(BIO *bp, BN_CTX *ctx)
2060 {
2061     BIGNUM *a, *b, *c, *d, *e;
2062     int i;
2063
2064     a = BN_new();
2065     b = BN_new();
2066     c = BN_new();
2067     d = BN_new();
2068     e = BN_new();
2069     BN_one(c);
2070
2071     BN_bntest_rand(a, 200, 0, 0);
2072     a->neg = rand_neg();
2073     for (i = 0; i < num0; i++) {
2074         BN_rshift(b, a, i + 1);
2075         BN_add(c, c, c);
2076         if (bp != NULL) {
2077             if (!results) {
2078                 BN_print(bp, a);
2079                 BIO_puts(bp, " / ");
2080                 BN_print(bp, c);
2081                 BIO_puts(bp, " - ");
2082             }
2083             BN_print(bp, b);
2084             BIO_puts(bp, "\n");
2085         }
2086         BN_div(d, e, a, c, ctx);
2087         BN_sub(d, d, b);
2088         if (!BN_is_zero(d)) {
2089             fprintf(stderr, "Right shift test failed!\n");
2090             return 0;
2091         }
2092     }
2093     BN_free(a);
2094     BN_free(b);
2095     BN_free(c);
2096     BN_free(d);
2097     BN_free(e);
2098     return (1);
2099 }
2100
2101 int test_rshift1(BIO *bp)
2102 {
2103     BIGNUM *a, *b, *c;
2104     int i;
2105
2106     a = BN_new();
2107     b = BN_new();
2108     c = BN_new();
2109
2110     BN_bntest_rand(a, 200, 0, 0);
2111     a->neg = rand_neg();
2112     for (i = 0; i < num0; i++) {
2113         BN_rshift1(b, a);
2114         if (bp != NULL) {
2115             if (!results) {
2116                 BN_print(bp, a);
2117                 BIO_puts(bp, " / 2");
2118                 BIO_puts(bp, " - ");
2119             }
2120             BN_print(bp, b);
2121             BIO_puts(bp, "\n");
2122         }
2123         BN_sub(c, a, b);
2124         BN_sub(c, c, b);
2125         if (!BN_is_zero(c) && !BN_abs_is_word(c, 1)) {
2126             fprintf(stderr, "Right shift one test failed!\n");
2127             return 0;
2128         }
2129         BN_copy(a, b);
2130     }
2131     BN_free(a);
2132     BN_free(b);
2133     BN_free(c);
2134     return (1);
2135 }
2136
2137 int rand_neg(void)
2138 {
2139     static unsigned int neg = 0;
2140     static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
2141
2142     return (sign[(neg++) % 8]);
2143 }