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