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