test_mod_mul is useful, let's run it more often.
[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 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62
63 #include "openssl/e_os.h"
64
65 #include <openssl/bio.h>
66 #include <openssl/bn.h>
67 #include <openssl/rand.h>
68 #include <openssl/x509.h>
69 #include <openssl/err.h>
70
71 #ifdef WINDOWS
72 #include "../bio/bss_file.c"
73 #endif
74
75 const int num0 = 100; /* number of tests */
76 const int num1 = 50;  /* additional tests for some functions */
77 const int num2 = 5;   /* number of tests for slow functions */
78
79 int test_add(BIO *bp);
80 int test_sub(BIO *bp);
81 int test_lshift1(BIO *bp);
82 int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_);
83 int test_rshift1(BIO *bp);
84 int test_rshift(BIO *bp,BN_CTX *ctx);
85 int test_div(BIO *bp,BN_CTX *ctx);
86 int test_div_recp(BIO *bp,BN_CTX *ctx);
87 int test_mul(BIO *bp);
88 int test_sqr(BIO *bp,BN_CTX *ctx);
89 int test_mont(BIO *bp,BN_CTX *ctx);
90 int test_mod(BIO *bp,BN_CTX *ctx);
91 int test_mod_mul(BIO *bp,BN_CTX *ctx);
92 int test_mod_exp(BIO *bp,BN_CTX *ctx);
93 int test_exp(BIO *bp,BN_CTX *ctx);
94 int test_kron(BIO *bp,BN_CTX *ctx);
95 int test_sqrt(BIO *bp,BN_CTX *ctx);
96 int rand_neg(void);
97 static int results=0;
98
99 #ifdef NO_STDIO
100 #define APPS_WIN16
101 #include "bss_file.c"
102 #endif
103
104 static unsigned char lst[]="\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9"
105 "\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0";
106
107 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
108
109 static void message(BIO *out, char *m)
110         {
111         fprintf(stderr, "test %s\n", m);
112 #if defined(linux) || defined(__FreeBSD__) /* can we use GNU bc features? */
113         BIO_puts(out, "print \"test ");
114         BIO_puts(out, m);
115         BIO_puts(out, "\\n\"\n");
116 #endif
117         }
118
119 int main(int argc, char *argv[])
120         {
121         BN_CTX *ctx;
122         BIO *out;
123         char *outfile=NULL;
124
125         results = 0;
126
127         RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
128
129         argc--;
130         argv++;
131         while (argc >= 1)
132                 {
133                 if (strcmp(*argv,"-results") == 0)
134                         results=1;
135                 else if (strcmp(*argv,"-out") == 0)
136                         {
137                         if (--argc < 1) break;
138                         outfile= *(++argv);
139                         }
140                 argc--;
141                 argv++;
142                 }
143
144
145         ctx=BN_CTX_new();
146         if (ctx == NULL) exit(1);
147
148         out=BIO_new(BIO_s_file());
149         if (out == NULL) exit(1);
150         if (outfile == NULL)
151                 {
152                 BIO_set_fp(out,stdout,BIO_NOCLOSE);
153                 }
154         else
155                 {
156                 if (!BIO_write_filename(out,outfile))
157                         {
158                         perror(outfile);
159                         exit(1);
160                         }
161                 }
162
163         if (!results)
164                 BIO_puts(out,"obase=16\nibase=16\n");
165
166         message(out,"BN_add");
167         if (!test_add(out)) goto err;
168         BIO_flush(out);
169
170         message(out,"BN_sub");
171         if (!test_sub(out)) goto err;
172         BIO_flush(out);
173
174         message(out,"BN_lshift1");
175         if (!test_lshift1(out)) goto err;
176         BIO_flush(out);
177
178         message(out,"BN_lshift (fixed)");
179         if (!test_lshift(out,ctx,BN_bin2bn(lst,sizeof(lst)-1,NULL)))
180             goto err;
181         BIO_flush(out);
182
183         message(out,"BN_lshift");
184         if (!test_lshift(out,ctx,NULL)) goto err;
185         BIO_flush(out);
186
187         message(out,"BN_rshift1");
188         if (!test_rshift1(out)) goto err;
189         BIO_flush(out);
190
191         message(out,"BN_rshift");
192         if (!test_rshift(out,ctx)) goto err;
193         BIO_flush(out);
194
195         message(out,"BN_sqr");
196         if (!test_sqr(out,ctx)) goto err;
197         BIO_flush(out);
198
199         message(out,"BN_mul");
200         if (!test_mul(out)) goto err;
201         BIO_flush(out);
202
203         message(out,"BN_div");
204         if (!test_div(out,ctx)) goto err;
205         BIO_flush(out);
206
207         message(out,"BN_div_recp");
208         if (!test_div_recp(out,ctx)) goto err;
209         BIO_flush(out);
210
211         message(out,"BN_mod");
212         if (!test_mod(out,ctx)) goto err;
213         BIO_flush(out);
214
215         message(out,"BN_mod_mul");
216         if (!test_mod_mul(out,ctx)) goto err;
217         BIO_flush(out);
218
219         message(out,"BN_mont");
220         if (!test_mont(out,ctx)) goto err;
221         BIO_flush(out);
222
223         message(out,"BN_mod_exp");
224         if (!test_mod_exp(out,ctx)) goto err;
225         BIO_flush(out);
226
227         message(out,"BN_exp");
228         if (!test_exp(out,ctx)) goto err;
229         BIO_flush(out);
230
231         message(out,"BN_kronecker");
232         if (!test_kron(out,ctx)) goto err;
233         BIO_flush(out);
234
235         message(out,"BN_mod_sqrt");
236         if (!test_sqrt(out,ctx)) goto err;
237         BIO_flush(out);
238
239         BN_CTX_free(ctx);
240         BIO_free(out);
241
242 /**/
243         exit(0);
244 err:
245         BIO_puts(out,"1\n"); /* make sure the Perl script fed by bc notices
246                               * the failure, see test_bn in test/Makefile.ssl*/
247         BIO_flush(out);
248         ERR_load_crypto_strings();
249         ERR_print_errors_fp(stderr);
250         exit(1);
251         return(1);
252         }
253
254 int test_add(BIO *bp)
255         {
256         BIGNUM a,b,c;
257         int i;
258
259         BN_init(&a);
260         BN_init(&b);
261         BN_init(&c);
262
263         BN_bntest_rand(&a,512,0,0);
264         for (i=0; i<num0; i++)
265                 {
266                 BN_bntest_rand(&b,450+i,0,0);
267                 a.neg=rand_neg();
268                 b.neg=rand_neg();
269                 BN_add(&c,&a,&b);
270                 if (bp != NULL)
271                         {
272                         if (!results)
273                                 {
274                                 BN_print(bp,&a);
275                                 BIO_puts(bp," + ");
276                                 BN_print(bp,&b);
277                                 BIO_puts(bp," - ");
278                                 }
279                         BN_print(bp,&c);
280                         BIO_puts(bp,"\n");
281                         }
282                 a.neg=!a.neg;
283                 b.neg=!b.neg;
284                 BN_add(&c,&c,&b);
285                 BN_add(&c,&c,&a);
286                 if(!BN_is_zero(&c))
287                     {
288                     fprintf(stderr,"Add test failed!\n");
289                     return 0;
290                     }
291                 }
292         BN_free(&a);
293         BN_free(&b);
294         BN_free(&c);
295         return(1);
296         }
297
298 int test_sub(BIO *bp)
299         {
300         BIGNUM a,b,c;
301         int i;
302
303         BN_init(&a);
304         BN_init(&b);
305         BN_init(&c);
306
307         for (i=0; i<num0+num1; i++)
308                 {
309                 if (i < num1)
310                         {
311                         BN_bntest_rand(&a,512,0,0);
312                         BN_copy(&b,&a);
313                         if (BN_set_bit(&a,i)==0) return(0);
314                         BN_add_word(&b,i);
315                         }
316                 else
317                         {
318                         BN_bntest_rand(&b,400+i-num1,0,0);
319                         a.neg=rand_neg();
320                         b.neg=rand_neg();
321                         }
322                 BN_sub(&c,&a,&b);
323                 if (bp != NULL)
324                         {
325                         if (!results)
326                                 {
327                                 BN_print(bp,&a);
328                                 BIO_puts(bp," - ");
329                                 BN_print(bp,&b);
330                                 BIO_puts(bp," - ");
331                                 }
332                         BN_print(bp,&c);
333                         BIO_puts(bp,"\n");
334                         }
335                 BN_add(&c,&c,&b);
336                 BN_sub(&c,&c,&a);
337                 if(!BN_is_zero(&c))
338                     {
339                     fprintf(stderr,"Subtract test failed!\n");
340                     return 0;
341                     }
342                 }
343         BN_free(&a);
344         BN_free(&b);
345         BN_free(&c);
346         return(1);
347         }
348
349 int test_div(BIO *bp, BN_CTX *ctx)
350         {
351         BIGNUM a,b,c,d,e;
352         int i;
353
354         BN_init(&a);
355         BN_init(&b);
356         BN_init(&c);
357         BN_init(&d);
358         BN_init(&e);
359
360         for (i=0; i<num0+num1; i++)
361                 {
362                 if (i < num1)
363                         {
364                         BN_bntest_rand(&a,400,0,0);
365                         BN_copy(&b,&a);
366                         BN_lshift(&a,&a,i);
367                         BN_add_word(&a,i);
368                         }
369                 else
370                         BN_bntest_rand(&b,50+3*(i-num1),0,0);
371                 a.neg=rand_neg();
372                 b.neg=rand_neg();
373                 BN_div(&d,&c,&a,&b,ctx);
374                 if (bp != NULL)
375                         {
376                         if (!results)
377                                 {
378                                 BN_print(bp,&a);
379                                 BIO_puts(bp," / ");
380                                 BN_print(bp,&b);
381                                 BIO_puts(bp," - ");
382                                 }
383                         BN_print(bp,&d);
384                         BIO_puts(bp,"\n");
385
386                         if (!results)
387                                 {
388                                 BN_print(bp,&a);
389                                 BIO_puts(bp," % ");
390                                 BN_print(bp,&b);
391                                 BIO_puts(bp," - ");
392                                 }
393                         BN_print(bp,&c);
394                         BIO_puts(bp,"\n");
395                         }
396                 BN_mul(&e,&d,&b,ctx);
397                 BN_add(&d,&e,&c);
398                 BN_sub(&d,&d,&a);
399                 if(!BN_is_zero(&d))
400                     {
401                     fprintf(stderr,"Division test failed!\n");
402                     return 0;
403                     }
404                 }
405         BN_free(&a);
406         BN_free(&b);
407         BN_free(&c);
408         BN_free(&d);
409         BN_free(&e);
410         return(1);
411         }
412
413 int test_div_recp(BIO *bp, BN_CTX *ctx)
414         {
415         BIGNUM a,b,c,d,e;
416         BN_RECP_CTX recp;
417         int i;
418
419         BN_RECP_CTX_init(&recp);
420         BN_init(&a);
421         BN_init(&b);
422         BN_init(&c);
423         BN_init(&d);
424         BN_init(&e);
425
426         for (i=0; i<num0+num1; i++)
427                 {
428                 if (i < num1)
429                         {
430                         BN_bntest_rand(&a,400,0,0);
431                         BN_copy(&b,&a);
432                         BN_lshift(&a,&a,i);
433                         BN_add_word(&a,i);
434                         }
435                 else
436                         BN_bntest_rand(&b,50+3*(i-num1),0,0);
437                 a.neg=rand_neg();
438                 b.neg=rand_neg();
439                 BN_RECP_CTX_set(&recp,&b,ctx);
440                 BN_div_recp(&d,&c,&a,&recp,ctx);
441                 if (bp != NULL)
442                         {
443                         if (!results)
444                                 {
445                                 BN_print(bp,&a);
446                                 BIO_puts(bp," / ");
447                                 BN_print(bp,&b);
448                                 BIO_puts(bp," - ");
449                                 }
450                         BN_print(bp,&d);
451                         BIO_puts(bp,"\n");
452
453                         if (!results)
454                                 {
455                                 BN_print(bp,&a);
456                                 BIO_puts(bp," % ");
457                                 BN_print(bp,&b);
458                                 BIO_puts(bp," - ");
459                                 }
460                         BN_print(bp,&c);
461                         BIO_puts(bp,"\n");
462                         }
463                 BN_mul(&e,&d,&b,ctx);
464                 BN_add(&d,&e,&c);
465                 BN_sub(&d,&d,&a);
466                 if(!BN_is_zero(&d))
467                     {
468                     fprintf(stderr,"Reciprocal division test failed!\n");
469                     fprintf(stderr,"a=");
470                     BN_print_fp(stderr,&a);
471                     fprintf(stderr,"\nb=");
472                     BN_print_fp(stderr,&b);
473                     fprintf(stderr,"\n");
474                     return 0;
475                     }
476                 }
477         BN_free(&a);
478         BN_free(&b);
479         BN_free(&c);
480         BN_free(&d);
481         BN_free(&e);
482         BN_RECP_CTX_free(&recp);
483         return(1);
484         }
485
486 int test_mul(BIO *bp)
487         {
488         BIGNUM a,b,c,d,e;
489         int i;
490         BN_CTX ctx;
491
492         BN_CTX_init(&ctx);
493         BN_init(&a);
494         BN_init(&b);
495         BN_init(&c);
496         BN_init(&d);
497         BN_init(&e);
498
499         for (i=0; i<num0+num1; i++)
500                 {
501                 if (i <= num1)
502                         {
503                         BN_bntest_rand(&a,100,0,0);
504                         BN_bntest_rand(&b,100,0,0);
505                         }
506                 else
507                         BN_bntest_rand(&b,i-num1,0,0);
508                 a.neg=rand_neg();
509                 b.neg=rand_neg();
510                 BN_mul(&c,&a,&b,&ctx);
511                 if (bp != NULL)
512                         {
513                         if (!results)
514                                 {
515                                 BN_print(bp,&a);
516                                 BIO_puts(bp," * ");
517                                 BN_print(bp,&b);
518                                 BIO_puts(bp," - ");
519                                 }
520                         BN_print(bp,&c);
521                         BIO_puts(bp,"\n");
522                         }
523                 BN_div(&d,&e,&c,&a,&ctx);
524                 BN_sub(&d,&d,&b);
525                 if(!BN_is_zero(&d) || !BN_is_zero(&e))
526                     {
527                     fprintf(stderr,"Multiplication test failed!\n");
528                     return 0;
529                     }
530                 }
531         BN_free(&a);
532         BN_free(&b);
533         BN_free(&c);
534         BN_free(&d);
535         BN_free(&e);
536         BN_CTX_free(&ctx);
537         return(1);
538         }
539
540 int test_sqr(BIO *bp, BN_CTX *ctx)
541         {
542         BIGNUM a,c,d,e;
543         int i;
544
545         BN_init(&a);
546         BN_init(&c);
547         BN_init(&d);
548         BN_init(&e);
549
550         for (i=0; i<num0; i++)
551                 {
552                 BN_bntest_rand(&a,40+i*10,0,0);
553                 a.neg=rand_neg();
554                 BN_sqr(&c,&a,ctx);
555                 if (bp != NULL)
556                         {
557                         if (!results)
558                                 {
559                                 BN_print(bp,&a);
560                                 BIO_puts(bp," * ");
561                                 BN_print(bp,&a);
562                                 BIO_puts(bp," - ");
563                                 }
564                         BN_print(bp,&c);
565                         BIO_puts(bp,"\n");
566                         }
567                 BN_div(&d,&e,&c,&a,ctx);
568                 BN_sub(&d,&d,&a);
569                 if(!BN_is_zero(&d) || !BN_is_zero(&e))
570                     {
571                     fprintf(stderr,"Square test failed!\n");
572                     return 0;
573                     }
574                 }
575         BN_free(&a);
576         BN_free(&c);
577         BN_free(&d);
578         BN_free(&e);
579         return(1);
580         }
581
582 int test_mont(BIO *bp, BN_CTX *ctx)
583         {
584         BIGNUM a,b,c,d,A,B;
585         BIGNUM n;
586         int i;
587         BN_MONT_CTX *mont;
588
589         BN_init(&a);
590         BN_init(&b);
591         BN_init(&c);
592         BN_init(&d);
593         BN_init(&A);
594         BN_init(&B);
595         BN_init(&n);
596
597         mont=BN_MONT_CTX_new();
598
599         BN_bntest_rand(&a,100,0,0); /**/
600         BN_bntest_rand(&b,100,0,0); /**/
601         for (i=0; i<num2; i++)
602                 {
603                 int bits = (200*(i+1))/num2;
604
605                 if (bits == 0)
606                         continue;
607                 BN_bntest_rand(&n,bits,0,1);
608                 BN_MONT_CTX_set(mont,&n,ctx);
609
610                 BN_nnmod(&a,&a,&n,ctx);
611                 BN_nnmod(&b,&b,&n,ctx);
612
613                 BN_to_montgomery(&A,&a,mont,ctx);
614                 BN_to_montgomery(&B,&b,mont,ctx);
615
616                 BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
617                 BN_from_montgomery(&A,&c,mont,ctx);/**/
618                 if (bp != NULL)
619                         {
620                         if (!results)
621                                 {
622 #ifdef undef
623 fprintf(stderr,"%d * %d %% %d\n",
624 BN_num_bits(&a),
625 BN_num_bits(&b),
626 BN_num_bits(mont->N));
627 #endif
628                                 BN_print(bp,&a);
629                                 BIO_puts(bp," * ");
630                                 BN_print(bp,&b);
631                                 BIO_puts(bp," % ");
632                                 BN_print(bp,&(mont->N));
633                                 BIO_puts(bp," - ");
634                                 }
635                         BN_print(bp,&A);
636                         BIO_puts(bp,"\n");
637                         }
638                 BN_mod_mul(&d,&a,&b,&n,ctx);
639                 BN_sub(&d,&d,&A);
640                 if(!BN_is_zero(&d))
641                     {
642                     fprintf(stderr,"Montgomery multiplication test failed!\n");
643                     return 0;
644                     }
645                 }
646         BN_MONT_CTX_free(mont);
647         BN_free(&a);
648         BN_free(&b);
649         BN_free(&c);
650         BN_free(&d);
651         BN_free(&A);
652         BN_free(&B);
653         BN_free(&n);
654         return(1);
655         }
656
657 int test_mod(BIO *bp, BN_CTX *ctx)
658         {
659         BIGNUM *a,*b,*c,*d,*e;
660         int i;
661
662         a=BN_new();
663         b=BN_new();
664         c=BN_new();
665         d=BN_new();
666         e=BN_new();
667
668         BN_bntest_rand(a,1024,0,0); /**/
669         for (i=0; i<num0; i++)
670                 {
671                 BN_bntest_rand(b,450+i*10,0,0); /**/
672                 a->neg=rand_neg();
673                 b->neg=rand_neg();
674                 BN_mod(c,a,b,ctx);/**/
675                 if (bp != NULL)
676                         {
677                         if (!results)
678                                 {
679                                 BN_print(bp,a);
680                                 BIO_puts(bp," % ");
681                                 BN_print(bp,b);
682                                 BIO_puts(bp," - ");
683                                 }
684                         BN_print(bp,c);
685                         BIO_puts(bp,"\n");
686                         }
687                 BN_div(d,e,a,b,ctx);
688                 BN_sub(e,e,c);
689                 if(!BN_is_zero(e))
690                     {
691                     fprintf(stderr,"Modulo test failed!\n");
692                     return 0;
693                     }
694                 }
695         BN_free(a);
696         BN_free(b);
697         BN_free(c);
698         BN_free(d);
699         BN_free(e);
700         return(1);
701         }
702
703 int test_mod_mul(BIO *bp, BN_CTX *ctx)
704         {
705         BIGNUM *a,*b,*c,*d,*e;
706         int i,j;
707
708         a=BN_new();
709         b=BN_new();
710         c=BN_new();
711         d=BN_new();
712         e=BN_new();
713
714         for (j=0; j<3; j++) {
715         BN_bntest_rand(c,1024,0,0); /**/
716         for (i=0; i<num0; i++)
717                 {
718                 BN_bntest_rand(a,475+i*10,0,0); /**/
719                 BN_bntest_rand(b,425+i*11,0,0); /**/
720                 a->neg=rand_neg();
721                 b->neg=rand_neg();
722                 if (!BN_mod_mul(e,a,b,c,ctx))
723                         {
724                         unsigned long l;
725
726                         while ((l=ERR_get_error()))
727                                 fprintf(stderr,"ERROR:%s\n",
728                                         ERR_error_string(l,NULL));
729                         exit(1);
730                         }
731                 if (bp != NULL)
732                         {
733                         if (!results)
734                                 {
735                                 BN_print(bp,a);
736                                 BIO_puts(bp," * ");
737                                 BN_print(bp,b);
738                                 BIO_puts(bp," % ");
739                                 BN_print(bp,c);
740                                 if ((a->neg ^ b->neg) && !BN_is_zero(e))
741                                         {
742                                         /* If  (a*b) % c  is negative,  c  must be added
743                                          * in order to obtain the normalized remainder
744                                          * (new with OpenSSL 0.9.7, previous versions of
745                                          * BN_mod_mul could generate negative results)
746                                          */
747                                         BIO_puts(bp," + ");
748                                         BN_print(bp,c);
749                                         }
750                                 BIO_puts(bp," - ");
751                                 }
752                         BN_print(bp,e);
753                         BIO_puts(bp,"\n");
754                         }
755                 BN_mul(d,a,b,ctx);
756                 BN_sub(d,d,e);
757                 BN_div(a,b,d,c,ctx);
758                 if(!BN_is_zero(b))
759                     {
760                     fprintf(stderr,"Modulo multiply test failed!\n");
761                     ERR_print_errors_fp(stderr);
762                     return 0;
763                     }
764                 }
765         }
766         BN_free(a);
767         BN_free(b);
768         BN_free(c);
769         BN_free(d);
770         BN_free(e);
771         return(1);
772         }
773
774 int test_mod_exp(BIO *bp, BN_CTX *ctx)
775         {
776         BIGNUM *a,*b,*c,*d,*e;
777         int i;
778
779         a=BN_new();
780         b=BN_new();
781         c=BN_new();
782         d=BN_new();
783         e=BN_new();
784
785         BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */
786         for (i=0; i<num2; i++)
787                 {
788                 BN_bntest_rand(a,20+i*5,0,0); /**/
789                 BN_bntest_rand(b,2+i,0,0); /**/
790
791                 if (!BN_mod_exp(d,a,b,c,ctx))
792                         return(00);
793
794                 if (bp != NULL)
795                         {
796                         if (!results)
797                                 {
798                                 BN_print(bp,a);
799                                 BIO_puts(bp," ^ ");
800                                 BN_print(bp,b);
801                                 BIO_puts(bp," % ");
802                                 BN_print(bp,c);
803                                 BIO_puts(bp," - ");
804                                 }
805                         BN_print(bp,d);
806                         BIO_puts(bp,"\n");
807                         }
808                 BN_exp(e,a,b,ctx);
809                 BN_sub(e,e,d);
810                 BN_div(a,b,e,c,ctx);
811                 if(!BN_is_zero(b))
812                     {
813                     fprintf(stderr,"Modulo exponentiation test failed!\n");
814                     return 0;
815                     }
816                 }
817         BN_free(a);
818         BN_free(b);
819         BN_free(c);
820         BN_free(d);
821         BN_free(e);
822         return(1);
823         }
824
825 int test_exp(BIO *bp, BN_CTX *ctx)
826         {
827         BIGNUM *a,*b,*d,*e,*one;
828         int i;
829
830         a=BN_new();
831         b=BN_new();
832         d=BN_new();
833         e=BN_new();
834         one=BN_new();
835         BN_one(one);
836
837         for (i=0; i<num2; i++)
838                 {
839                 BN_bntest_rand(a,20+i*5,0,0); /**/
840                 BN_bntest_rand(b,2+i,0,0); /**/
841
842                 if (!BN_exp(d,a,b,ctx))
843                         return(00);
844
845                 if (bp != NULL)
846                         {
847                         if (!results)
848                                 {
849                                 BN_print(bp,a);
850                                 BIO_puts(bp," ^ ");
851                                 BN_print(bp,b);
852                                 BIO_puts(bp," - ");
853                                 }
854                         BN_print(bp,d);
855                         BIO_puts(bp,"\n");
856                         }
857                 BN_one(e);
858                 for( ; !BN_is_zero(b) ; BN_sub(b,b,one))
859                     BN_mul(e,e,a,ctx);
860                 BN_sub(e,e,d);
861                 if(!BN_is_zero(e))
862                     {
863                     fprintf(stderr,"Exponentiation test failed!\n");
864                     return 0;
865                     }
866                 }
867         BN_free(a);
868         BN_free(b);
869         BN_free(d);
870         BN_free(e);
871         BN_free(one);
872         return(1);
873         }
874
875 static void genprime_cb(int p, int n, void *arg)
876         {
877         char c='*';
878
879         if (p == 0) c='.';
880         if (p == 1) c='+';
881         if (p == 2) c='*';
882         if (p == 3) c='\n';
883         putc(c, stderr);
884         fflush(stderr);
885         (void)n;
886         (void)arg;
887         }
888
889 int test_kron(BIO *bp, BN_CTX *ctx)
890         {
891         BIGNUM *a,*b,*r,*t;
892         int i;
893         int legendre, kronecker;
894         int ret = 0;
895
896         a = BN_new();
897         b = BN_new();
898         r = BN_new();
899         t = BN_new();
900         if (a == NULL || b == NULL || r == NULL || t == NULL) goto err;
901         
902         /* We test BN_kronecker(a, b, ctx) just for  b  odd (Jacobi symbol).
903          * In this case we know that if  b  is prime, then BN_kronecker(a, b, ctx)
904          * is congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol).
905          * So we generate a random prime  b  and compare these values
906          * for a number of random  a's.  (That is, we run the Solovay-Strassen
907          * primality test to confirm that  b  is prime, except that we
908          * don't want to test whether  b  is prime but whether BN_kronecker
909          * works.) */
910
911         if (!BN_generate_prime(b, 512, 0, NULL, NULL, genprime_cb, NULL)) goto err;
912         putc('\n', stderr);
913
914         for (i = 0; i < num0; i++)
915                 {
916                 if (!BN_bntest_rand(a, 512, 0, 0)) goto err;
917                 a->neg = rand_neg();
918
919                 /* t := (b-1)/2  (note that b is odd) */
920                 if (!BN_copy(t, b)) goto err;
921                 if (!BN_sub_word(t, 1)) goto err;
922                 if (!BN_rshift1(t, t)) goto err;
923                 /* r := a^t mod b */
924                 if (!BN_mod_exp(r, a, t, b, ctx)) goto err;
925
926                 if (BN_is_word(r, 1))
927                         legendre = 1;
928                 else if (BN_is_zero(r))
929                         legendre = 0;
930                 else
931                         {
932                         if (!BN_add_word(r, 1)) goto err;
933                         if (0 != BN_cmp(r, b))
934                                 {
935                                 fprintf(stderr, "Legendre symbol computation failed\n");
936                                 goto err;
937                                 }
938                         legendre = -1;
939                         }
940                 
941                 kronecker = BN_kronecker(a, b, ctx);
942                 if (kronecker < -1) goto err;
943                 
944                 if (legendre != kronecker)
945                         {
946                         fprintf(stderr, "legendre != kronecker; a = ");
947                         BN_print_fp(stderr, a);
948                         fprintf(stderr, ", b = ");
949                         BN_print_fp(stderr, b);
950                         fprintf(stderr, "\n");
951                         goto err;
952                         }
953
954                 putc('.', stderr);
955                 fflush(stderr);
956                 }
957
958         putc('\n', stderr);
959         fflush(stderr);
960         ret = 1;
961  err:
962         if (a != NULL) BN_free(a);
963         if (b != NULL) BN_free(b);
964         if (r != NULL) BN_free(r);
965         if (t != NULL) BN_free(t);
966         return ret;
967         }
968
969 int test_sqrt(BIO *bp, BN_CTX *ctx)
970         {
971         BIGNUM *a,*p,*r;
972         int i, j;
973         int ret = 0;
974
975         a = BN_new();
976         p = BN_new();
977         r = BN_new();
978         if (a == NULL || p == NULL || r == NULL) goto err;
979         
980         for (i = 0; i < 16; i++)
981                 {
982                 if (i < 8)
983                         {
984                         unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 };
985                         
986                         if (!BN_set_word(p, primes[i])) goto err;
987                         }
988                 else
989                         {
990                         if (!BN_set_word(a, 32)) goto err;
991                         if (!BN_set_word(r, 2*i + 1)) goto err;
992                 
993                         if (!BN_generate_prime(p, 256, 0, a, r, genprime_cb, NULL)) goto err;
994                         putc('\n', stderr);
995                         }
996
997                 for (j = 0; j < num2; j++)
998                         {
999                         /* construct 'a' such that it is a square modulo p,
1000                          * but in general not a proper square and not reduced modulo p */
1001                         if (!BN_bntest_rand(r, 256, 0, 3)) goto err;
1002                         if (!BN_nnmod(r, r, p, ctx)) goto err;
1003                         if (!BN_mod_sqr(r, r, p, ctx)) goto err;
1004                         if (!BN_bntest_rand(a, 256, 0, 3)) goto err;
1005                         if (!BN_nnmod(a, a, p, ctx)) goto err;
1006                         if (!BN_mod_sqr(a, a, p, ctx)) goto err;
1007                         if (!BN_mul(a, a, r, ctx)) goto err;
1008
1009                         if (!BN_mod_sqrt(r, a, p, ctx)) goto err;
1010                         if (!BN_mod_sqr(r, r, p, ctx)) goto err;
1011
1012                         if (!BN_nnmod(a, a, p, ctx)) goto err;
1013
1014                         if (BN_cmp(a, r) != 0)
1015                                 {
1016                                 fprintf(stderr, "BN_mod_sqrt failed: a = ");
1017                                 BN_print_fp(stderr, a);
1018                                 fprintf(stderr, ", r = ");
1019                                 BN_print_fp(stderr, r);
1020                                 fprintf(stderr, ", p = ");
1021                                 BN_print_fp(stderr, p);
1022                                 fprintf(stderr, "\n");
1023                                 goto err;
1024                                 }
1025
1026                         putc('.', stderr);
1027                         fflush(stderr);
1028                         }
1029                 
1030                 putc('\n', stderr);
1031                 fflush(stderr);
1032                 }
1033         ret = 1;
1034  err:
1035         if (a != NULL) BN_free(a);
1036         if (p != NULL) BN_free(p);
1037         if (r != NULL) BN_free(r);
1038         return ret;
1039         }
1040
1041 int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_)
1042         {
1043         BIGNUM *a,*b,*c,*d;
1044         int i;
1045
1046         b=BN_new();
1047         c=BN_new();
1048         d=BN_new();
1049         BN_one(c);
1050
1051         if(a_)
1052             a=a_;
1053         else
1054             {
1055             a=BN_new();
1056             BN_bntest_rand(a,200,0,0); /**/
1057             a->neg=rand_neg();
1058             }
1059         for (i=0; i<num0; i++)
1060                 {
1061                 BN_lshift(b,a,i+1);
1062                 BN_add(c,c,c);
1063                 if (bp != NULL)
1064                         {
1065                         if (!results)
1066                                 {
1067                                 BN_print(bp,a);
1068                                 BIO_puts(bp," * ");
1069                                 BN_print(bp,c);
1070                                 BIO_puts(bp," - ");
1071                                 }
1072                         BN_print(bp,b);
1073                         BIO_puts(bp,"\n");
1074                         }
1075                 BN_mul(d,a,c,ctx);
1076                 BN_sub(d,d,b);
1077                 if(!BN_is_zero(d))
1078                     {
1079                     fprintf(stderr,"Left shift test failed!\n");
1080                     fprintf(stderr,"a=");
1081                     BN_print_fp(stderr,a);
1082                     fprintf(stderr,"\nb=");
1083                     BN_print_fp(stderr,b);
1084                     fprintf(stderr,"\nc=");
1085                     BN_print_fp(stderr,c);
1086                     fprintf(stderr,"\nd=");
1087                     BN_print_fp(stderr,d);
1088                     fprintf(stderr,"\n");
1089                     return 0;
1090                     }
1091                 }
1092         BN_free(a);
1093         BN_free(b);
1094         BN_free(c);
1095         BN_free(d);
1096         return(1);
1097         }
1098
1099 int test_lshift1(BIO *bp)
1100         {
1101         BIGNUM *a,*b,*c;
1102         int i;
1103
1104         a=BN_new();
1105         b=BN_new();
1106         c=BN_new();
1107
1108         BN_bntest_rand(a,200,0,0); /**/
1109         a->neg=rand_neg();
1110         for (i=0; i<num0; i++)
1111                 {
1112                 BN_lshift1(b,a);
1113                 if (bp != NULL)
1114                         {
1115                         if (!results)
1116                                 {
1117                                 BN_print(bp,a);
1118                                 BIO_puts(bp," * 2");
1119                                 BIO_puts(bp," - ");
1120                                 }
1121                         BN_print(bp,b);
1122                         BIO_puts(bp,"\n");
1123                         }
1124                 BN_add(c,a,a);
1125                 BN_sub(a,b,c);
1126                 if(!BN_is_zero(a))
1127                     {
1128                     fprintf(stderr,"Left shift one test failed!\n");
1129                     return 0;
1130                     }
1131                 
1132                 BN_copy(a,b);
1133                 }
1134         BN_free(a);
1135         BN_free(b);
1136         BN_free(c);
1137         return(1);
1138         }
1139
1140 int test_rshift(BIO *bp,BN_CTX *ctx)
1141         {
1142         BIGNUM *a,*b,*c,*d,*e;
1143         int i;
1144
1145         a=BN_new();
1146         b=BN_new();
1147         c=BN_new();
1148         d=BN_new();
1149         e=BN_new();
1150         BN_one(c);
1151
1152         BN_bntest_rand(a,200,0,0); /**/
1153         a->neg=rand_neg();
1154         for (i=0; i<num0; i++)
1155                 {
1156                 BN_rshift(b,a,i+1);
1157                 BN_add(c,c,c);
1158                 if (bp != NULL)
1159                         {
1160                         if (!results)
1161                                 {
1162                                 BN_print(bp,a);
1163                                 BIO_puts(bp," / ");
1164                                 BN_print(bp,c);
1165                                 BIO_puts(bp," - ");
1166                                 }
1167                         BN_print(bp,b);
1168                         BIO_puts(bp,"\n");
1169                         }
1170                 BN_div(d,e,a,c,ctx);
1171                 BN_sub(d,d,b);
1172                 if(!BN_is_zero(d))
1173                     {
1174                     fprintf(stderr,"Right shift test failed!\n");
1175                     return 0;
1176                     }
1177                 }
1178         BN_free(a);
1179         BN_free(b);
1180         BN_free(c);
1181         BN_free(d);
1182         BN_free(e);
1183         return(1);
1184         }
1185
1186 int test_rshift1(BIO *bp)
1187         {
1188         BIGNUM *a,*b,*c;
1189         int i;
1190
1191         a=BN_new();
1192         b=BN_new();
1193         c=BN_new();
1194
1195         BN_bntest_rand(a,200,0,0); /**/
1196         a->neg=rand_neg();
1197         for (i=0; i<num0; i++)
1198                 {
1199                 BN_rshift1(b,a);
1200                 if (bp != NULL)
1201                         {
1202                         if (!results)
1203                                 {
1204                                 BN_print(bp,a);
1205                                 BIO_puts(bp," / 2");
1206                                 BIO_puts(bp," - ");
1207                                 }
1208                         BN_print(bp,b);
1209                         BIO_puts(bp,"\n");
1210                         }
1211                 BN_sub(c,a,b);
1212                 BN_sub(c,c,b);
1213                 if(!BN_is_zero(c) && !BN_is_one(c))
1214                     {
1215                     fprintf(stderr,"Right shift one test failed!\n");
1216                     return 0;
1217                     }
1218                 BN_copy(a,b);
1219                 }
1220         BN_free(a);
1221         BN_free(b);
1222         BN_free(c);
1223         return(1);
1224         }
1225
1226 int rand_neg(void)
1227         {
1228         static unsigned int neg=0;
1229         static int sign[8]={0,0,0,1,1,0,1,1};
1230
1231         return(sign[(neg++)%8]);
1232         }