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