BN_mod_exp problems ...
[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;
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         if (a == NULL || b == NULL || r == NULL) goto err;
936         
937         /* We test BN_kronecker(a, b, ctx) just for  b  odd (Jacobi symbol).
938          * In this case we know that if  b  is prime, then BN_kronecker(a, b, ctx)
939          * is congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol).
940          * So we generate a random prime  b  and compare these values
941          * for a number of random  a's.  (That is, we run the Solovay-Strassen
942          * primality test to confirm that  b  is prime, except that we
943          * don't want to test whether  b  is prime but whether BN_kronecker
944          * works.) */
945
946         if (!BN_generate_prime(b, 512, 0, NULL, NULL, genprime_cb, NULL)) goto err;
947         putc('\n', stderr);
948
949         for (i = 0; i < num0; i++)
950                 {
951                 if (!BN_rand(a, 512, 0, 0)) goto err;
952                 a->neg = rand_neg();
953
954                 /* r := (b-1)/2  (note that b is odd) */
955                 if (!BN_copy(r, b)) goto err;
956                 if (!BN_sub_word(r, 1)) goto err;
957                 if (!BN_rshift1(r, r)) goto err;
958                 /* r := a^r mod b */
959 #if 0 /* These three variants should produce the same result, but with
960        * BN_mod_exp_recp or BN_mod_exp_simple, the test fails with
961        * the "Legendre symbol computation failed" error.
962        * (Platform: debug-solaris-sparcv9-gcc)
963        */
964                 if (!BN_mod_exp(r, a, r, b, ctx)) goto err;
965 #elsif 0
966                 if (!BN_mod_exp_recp(r, a, r, b, ctx)) goto err;
967 #else
968                 if (!BN_mod_exp_simple(r, a, r, b, ctx)) goto err;
969 #endif
970
971                 if (BN_is_word(r, 1))
972                         legendre = 1;
973                 else
974                         {
975                         if (!BN_add_word(r, 1)) goto err;
976                         if (0 != BN_cmp(r, b))
977                                 {
978                                 fprintf(stderr, "Legendre symbol computation failed\n");
979                                 goto err;
980                                 }
981                         legendre = -1;
982                         }
983
984                 kronecker = BN_kronecker(a, b, ctx);
985                 if (kronecker < -1) goto err;
986                 
987                 if (legendre != kronecker)
988                         {
989                         fprintf(stderr, "legendre != kronecker; a = ");
990                         BN_print_fp(stderr, a);
991                         fprintf(stderr, ", a = ");
992                         BN_print_fp(stderr, b);
993                         fprintf(stderr, "\n");
994                         goto err;
995                         }
996
997                 putc('.', stderr);
998                 fflush(stderr);
999                 }
1000
1001         putc('\n', stderr);
1002         fflush(stderr);
1003         ret = 1;
1004  err:
1005         if (a != NULL) BN_free(a);
1006         if (b != NULL) BN_free(b);
1007         if (r != NULL) BN_free(r);
1008         return ret;
1009         }
1010
1011 int test_sqrt(BIO *bp, BN_CTX *ctx)
1012         {
1013         BIGNUM *a,*p,*r;
1014         int i, j;
1015         int ret = 0;
1016
1017         a = BN_new();
1018         p = BN_new();
1019         r = BN_new();
1020         if (a == NULL || p == NULL || r == NULL) goto err;
1021         
1022         for (i = 0; i < 16; i++)
1023                 {
1024                 if (i < 8)
1025                         {
1026                         unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 };
1027                         
1028                         if (!BN_set_word(p, primes[i])) goto err;
1029                         }
1030                 else
1031                         {
1032                         if (!BN_set_word(a, 32)) goto err;
1033                         if (!BN_set_word(r, 2*i + 1)) goto err;
1034                 
1035                         if (!BN_generate_prime(p, 256, 0, a, r, genprime_cb, NULL)) goto err;
1036                         putc('\n', stderr);
1037                         }
1038
1039                 for (j = 0; j < num2; j++)
1040                         {
1041                         /* construct 'a' such that it is a square modulo p,
1042                          * but in general not a proper square and not reduced modulo p */
1043                         if (!BN_rand(r, 256, 0, 3)) goto err;
1044                         if (!BN_nnmod(r, r, p, ctx)) goto err;
1045                         if (!BN_mod_sqr(r, r, p, ctx)) goto err;
1046                         if (!BN_rand(a, 256, 0, 3)) goto err;
1047                         if (!BN_nnmod(a, a, p, ctx)) goto err;
1048                         if (!BN_mod_sqr(a, a, p, ctx)) goto err;
1049                         if (!BN_mul(a, a, r, ctx)) goto err;
1050
1051                         if (!BN_mod_sqrt(r, a, p, ctx)) goto err;
1052                         if (!BN_mod_sqr(r, r, p, ctx)) goto err;
1053
1054                         if (!BN_nnmod(a, a, p, ctx)) goto err;
1055
1056                         if (BN_cmp(a, r) != 0)
1057                                 {
1058                                 fprintf(stderr, "BN_mod_sqrt failed: a = ");
1059                                 BN_print_fp(stderr, a);
1060                                 fprintf(stderr, ", r = ");
1061                                 BN_print_fp(stderr, r);
1062                                 fprintf(stderr, ", p = ");
1063                                 BN_print_fp(stderr, p);
1064                                 fprintf(stderr, "\n");
1065                                 goto err;
1066                                 }
1067
1068                         putc('.', stderr);
1069                         fflush(stderr);
1070                         }
1071                 
1072                 putc('\n', stderr);
1073                 fflush(stderr);
1074                 }
1075         ret = 1;
1076  err:
1077         if (a != NULL) BN_free(a);
1078         if (p != NULL) BN_free(p);
1079         if (r != NULL) BN_free(r);
1080         return ret;
1081         }
1082
1083 int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_)
1084         {
1085         BIGNUM *a,*b,*c,*d;
1086         int i;
1087
1088         b=BN_new();
1089         c=BN_new();
1090         d=BN_new();
1091         BN_one(c);
1092
1093         if(a_)
1094             a=a_;
1095         else
1096             {
1097             a=BN_new();
1098             BN_rand(a,200,0,0); /**/
1099             a->neg=rand_neg();
1100             }
1101         for (i=0; i<num0; i++)
1102                 {
1103                 BN_lshift(b,a,i+1);
1104                 BN_add(c,c,c);
1105                 if (bp != NULL)
1106                         {
1107                         if (!results)
1108                                 {
1109                                 BN_print(bp,a);
1110                                 BIO_puts(bp," * ");
1111                                 BN_print(bp,c);
1112                                 BIO_puts(bp," - ");
1113                                 }
1114                         BN_print(bp,b);
1115                         BIO_puts(bp,"\n");
1116                         }
1117                 BN_mul(d,a,c,ctx);
1118                 BN_sub(d,d,b);
1119                 if(!BN_is_zero(d))
1120                     {
1121                     fprintf(stderr,"Left shift test failed!\n");
1122                     fprintf(stderr,"a=");
1123                     BN_print_fp(stderr,a);
1124                     fprintf(stderr,"\nb=");
1125                     BN_print_fp(stderr,b);
1126                     fprintf(stderr,"\nc=");
1127                     BN_print_fp(stderr,c);
1128                     fprintf(stderr,"\nd=");
1129                     BN_print_fp(stderr,d);
1130                     fprintf(stderr,"\n");
1131                     return 0;
1132                     }
1133                 }
1134         BN_free(a);
1135         BN_free(b);
1136         BN_free(c);
1137         BN_free(d);
1138         return(1);
1139         }
1140
1141 int test_lshift1(BIO *bp)
1142         {
1143         BIGNUM *a,*b,*c;
1144         int i;
1145
1146         a=BN_new();
1147         b=BN_new();
1148         c=BN_new();
1149
1150         BN_rand(a,200,0,0); /**/
1151         a->neg=rand_neg();
1152         for (i=0; i<num0; i++)
1153                 {
1154                 BN_lshift1(b,a);
1155                 if (bp != NULL)
1156                         {
1157                         if (!results)
1158                                 {
1159                                 BN_print(bp,a);
1160                                 BIO_puts(bp," * 2");
1161                                 BIO_puts(bp," - ");
1162                                 }
1163                         BN_print(bp,b);
1164                         BIO_puts(bp,"\n");
1165                         }
1166                 BN_add(c,a,a);
1167                 BN_sub(a,b,c);
1168                 if(!BN_is_zero(a))
1169                     {
1170                     fprintf(stderr,"Left shift one test failed!\n");
1171                     return 0;
1172                     }
1173                 
1174                 BN_copy(a,b);
1175                 }
1176         BN_free(a);
1177         BN_free(b);
1178         BN_free(c);
1179         return(1);
1180         }
1181
1182 int test_rshift(BIO *bp,BN_CTX *ctx)
1183         {
1184         BIGNUM *a,*b,*c,*d,*e;
1185         int i;
1186
1187         a=BN_new();
1188         b=BN_new();
1189         c=BN_new();
1190         d=BN_new();
1191         e=BN_new();
1192         BN_one(c);
1193
1194         BN_rand(a,200,0,0); /**/
1195         a->neg=rand_neg();
1196         for (i=0; i<num0; i++)
1197                 {
1198                 BN_rshift(b,a,i+1);
1199                 BN_add(c,c,c);
1200                 if (bp != NULL)
1201                         {
1202                         if (!results)
1203                                 {
1204                                 BN_print(bp,a);
1205                                 BIO_puts(bp," / ");
1206                                 BN_print(bp,c);
1207                                 BIO_puts(bp," - ");
1208                                 }
1209                         BN_print(bp,b);
1210                         BIO_puts(bp,"\n");
1211                         }
1212                 BN_div(d,e,a,c,ctx);
1213                 BN_sub(d,d,b);
1214                 if(!BN_is_zero(d))
1215                     {
1216                     fprintf(stderr,"Right shift test failed!\n");
1217                     return 0;
1218                     }
1219                 }
1220         BN_free(a);
1221         BN_free(b);
1222         BN_free(c);
1223         BN_free(d);
1224         BN_free(e);
1225         return(1);
1226         }
1227
1228 int test_rshift1(BIO *bp)
1229         {
1230         BIGNUM *a,*b,*c;
1231         int i;
1232
1233         a=BN_new();
1234         b=BN_new();
1235         c=BN_new();
1236
1237         BN_rand(a,200,0,0); /**/
1238         a->neg=rand_neg();
1239         for (i=0; i<num0; i++)
1240                 {
1241                 BN_rshift1(b,a);
1242                 if (bp != NULL)
1243                         {
1244                         if (!results)
1245                                 {
1246                                 BN_print(bp,a);
1247                                 BIO_puts(bp," / 2");
1248                                 BIO_puts(bp," - ");
1249                                 }
1250                         BN_print(bp,b);
1251                         BIO_puts(bp,"\n");
1252                         }
1253                 BN_sub(c,a,b);
1254                 BN_sub(c,c,b);
1255                 if(!BN_is_zero(c) && !BN_is_one(c))
1256                     {
1257                     fprintf(stderr,"Right shift one test failed!\n");
1258                     return 0;
1259                     }
1260                 BN_copy(a,b);
1261                 }
1262         BN_free(a);
1263         BN_free(b);
1264         BN_free(c);
1265         return(1);
1266         }
1267
1268 int rand_neg(void)
1269         {
1270         static unsigned int neg=0;
1271         static int sign[8]={0,0,0,1,1,0,1,1};
1272
1273         return(sign[(neg++)%8]);
1274         }