e_os.h does not belong with the exported headers. Do not put it there
[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         BN_CTX_init(&ctx);
491         BN_init(&a);
492         BN_init(&b);
493         BN_init(&c);
494         BN_init(&d);
495         BN_init(&e);
496
497         for (i=0; i<num0+num1; i++)
498                 {
499                 if (i <= num1)
500                         {
501                         BN_bntest_rand(&a,100,0,0);
502                         BN_bntest_rand(&b,100,0,0);
503                         }
504                 else
505                         BN_bntest_rand(&b,i-num1,0,0);
506                 a.neg=rand_neg();
507                 b.neg=rand_neg();
508                 BN_mul(&c,&a,&b,&ctx);
509                 if (bp != NULL)
510                         {
511                         if (!results)
512                                 {
513                                 BN_print(bp,&a);
514                                 BIO_puts(bp," * ");
515                                 BN_print(bp,&b);
516                                 BIO_puts(bp," - ");
517                                 }
518                         BN_print(bp,&c);
519                         BIO_puts(bp,"\n");
520                         }
521                 BN_div(&d,&e,&c,&a,&ctx);
522                 BN_sub(&d,&d,&b);
523                 if(!BN_is_zero(&d) || !BN_is_zero(&e))
524                     {
525                     fprintf(stderr,"Multiplication test failed!\n");
526                     return 0;
527                     }
528                 }
529         BN_free(&a);
530         BN_free(&b);
531         BN_free(&c);
532         BN_free(&d);
533         BN_free(&e);
534         BN_CTX_free(&ctx);
535         return(1);
536         }
537
538 int test_sqr(BIO *bp, BN_CTX *ctx)
539         {
540         BIGNUM a,c,d,e;
541         int i;
542
543         BN_init(&a);
544         BN_init(&c);
545         BN_init(&d);
546         BN_init(&e);
547
548         for (i=0; i<num0; i++)
549                 {
550                 BN_bntest_rand(&a,40+i*10,0,0);
551                 a.neg=rand_neg();
552                 BN_sqr(&c,&a,ctx);
553                 if (bp != NULL)
554                         {
555                         if (!results)
556                                 {
557                                 BN_print(bp,&a);
558                                 BIO_puts(bp," * ");
559                                 BN_print(bp,&a);
560                                 BIO_puts(bp," - ");
561                                 }
562                         BN_print(bp,&c);
563                         BIO_puts(bp,"\n");
564                         }
565                 BN_div(&d,&e,&c,&a,ctx);
566                 BN_sub(&d,&d,&a);
567                 if(!BN_is_zero(&d) || !BN_is_zero(&e))
568                     {
569                     fprintf(stderr,"Square test failed!\n");
570                     return 0;
571                     }
572                 }
573         BN_free(&a);
574         BN_free(&c);
575         BN_free(&d);
576         BN_free(&e);
577         return(1);
578         }
579
580 int test_mont(BIO *bp, BN_CTX *ctx)
581         {
582         BIGNUM a,b,c,d,A,B;
583         BIGNUM n;
584         int i;
585         BN_MONT_CTX *mont;
586
587         BN_init(&a);
588         BN_init(&b);
589         BN_init(&c);
590         BN_init(&d);
591         BN_init(&A);
592         BN_init(&B);
593         BN_init(&n);
594
595         mont=BN_MONT_CTX_new();
596
597         BN_bntest_rand(&a,100,0,0); /**/
598         BN_bntest_rand(&b,100,0,0); /**/
599         for (i=0; i<num2; i++)
600                 {
601                 int bits = (200*(i+1))/num2;
602
603                 if (bits == 0)
604                         continue;
605                 BN_bntest_rand(&n,bits,0,1);
606                 BN_MONT_CTX_set(mont,&n,ctx);
607
608                 BN_nnmod(&a,&a,&n,ctx);
609                 BN_nnmod(&b,&b,&n,ctx);
610
611                 BN_to_montgomery(&A,&a,mont,ctx);
612                 BN_to_montgomery(&B,&b,mont,ctx);
613
614                 BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
615                 BN_from_montgomery(&A,&c,mont,ctx);/**/
616                 if (bp != NULL)
617                         {
618                         if (!results)
619                                 {
620 #ifdef undef
621 fprintf(stderr,"%d * %d %% %d\n",
622 BN_num_bits(&a),
623 BN_num_bits(&b),
624 BN_num_bits(mont->N));
625 #endif
626                                 BN_print(bp,&a);
627                                 BIO_puts(bp," * ");
628                                 BN_print(bp,&b);
629                                 BIO_puts(bp," % ");
630                                 BN_print(bp,&(mont->N));
631                                 BIO_puts(bp," - ");
632                                 }
633                         BN_print(bp,&A);
634                         BIO_puts(bp,"\n");
635                         }
636                 BN_mod_mul(&d,&a,&b,&n,ctx);
637                 BN_sub(&d,&d,&A);
638                 if(!BN_is_zero(&d))
639                     {
640                     fprintf(stderr,"Montgomery multiplication test failed!\n");
641                     return 0;
642                     }
643                 }
644         BN_MONT_CTX_free(mont);
645         BN_free(&a);
646         BN_free(&b);
647         BN_free(&c);
648         BN_free(&d);
649         BN_free(&A);
650         BN_free(&B);
651         BN_free(&n);
652         return(1);
653         }
654
655 int test_mod(BIO *bp, BN_CTX *ctx)
656         {
657         BIGNUM *a,*b,*c,*d,*e;
658         int i;
659
660         a=BN_new();
661         b=BN_new();
662         c=BN_new();
663         d=BN_new();
664         e=BN_new();
665
666         BN_bntest_rand(a,1024,0,0); /**/
667         for (i=0; i<num0; i++)
668                 {
669                 BN_bntest_rand(b,450+i*10,0,0); /**/
670                 a->neg=rand_neg();
671                 b->neg=rand_neg();
672                 BN_mod(c,a,b,ctx);/**/
673                 if (bp != NULL)
674                         {
675                         if (!results)
676                                 {
677                                 BN_print(bp,a);
678                                 BIO_puts(bp," % ");
679                                 BN_print(bp,b);
680                                 BIO_puts(bp," - ");
681                                 }
682                         BN_print(bp,c);
683                         BIO_puts(bp,"\n");
684                         }
685                 BN_div(d,e,a,b,ctx);
686                 BN_sub(e,e,c);
687                 if(!BN_is_zero(e))
688                     {
689                     fprintf(stderr,"Modulo test failed!\n");
690                     return 0;
691                     }
692                 }
693         BN_free(a);
694         BN_free(b);
695         BN_free(c);
696         BN_free(d);
697         BN_free(e);
698         return(1);
699         }
700
701 int test_mod_mul(BIO *bp, BN_CTX *ctx)
702         {
703         BIGNUM *a,*b,*c,*d,*e;
704         int i,j;
705
706         a=BN_new();
707         b=BN_new();
708         c=BN_new();
709         d=BN_new();
710         e=BN_new();
711
712         for (j=0; j<3; j++) {
713         BN_bntest_rand(c,1024,0,0); /**/
714         for (i=0; i<num0; i++)
715                 {
716                 BN_bntest_rand(a,475+i*10,0,0); /**/
717                 BN_bntest_rand(b,425+i*11,0,0); /**/
718                 a->neg=rand_neg();
719                 b->neg=rand_neg();
720                 if (!BN_mod_mul(e,a,b,c,ctx))
721                         {
722                         unsigned long l;
723
724                         while ((l=ERR_get_error()))
725                                 fprintf(stderr,"ERROR:%s\n",
726                                         ERR_error_string(l,NULL));
727                         exit(1);
728                         }
729                 if (bp != NULL)
730                         {
731                         if (!results)
732                                 {
733                                 BN_print(bp,a);
734                                 BIO_puts(bp," * ");
735                                 BN_print(bp,b);
736                                 BIO_puts(bp," % ");
737                                 BN_print(bp,c);
738                                 if ((a->neg ^ b->neg) && !BN_is_zero(e))
739                                         {
740                                         /* If  (a*b) % c  is negative,  c  must be added
741                                          * in order to obtain the normalized remainder
742                                          * (new with OpenSSL 0.9.7, previous versions of
743                                          * BN_mod_mul could generate negative results)
744                                          */
745                                         BIO_puts(bp," + ");
746                                         BN_print(bp,c);
747                                         }
748                                 BIO_puts(bp," - ");
749                                 }
750                         BN_print(bp,e);
751                         BIO_puts(bp,"\n");
752                         }
753                 BN_mul(d,a,b,ctx);
754                 BN_sub(d,d,e);
755                 BN_div(a,b,d,c,ctx);
756                 if(!BN_is_zero(b))
757                     {
758                     fprintf(stderr,"Modulo multiply test failed!\n");
759                     ERR_print_errors_fp(stderr);
760                     return 0;
761                     }
762                 }
763         }
764         BN_free(a);
765         BN_free(b);
766         BN_free(c);
767         BN_free(d);
768         BN_free(e);
769         return(1);
770         }
771
772 int test_mod_exp(BIO *bp, BN_CTX *ctx)
773         {
774         BIGNUM *a,*b,*c,*d,*e;
775         int i;
776
777         a=BN_new();
778         b=BN_new();
779         c=BN_new();
780         d=BN_new();
781         e=BN_new();
782
783         BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */
784         for (i=0; i<num2; i++)
785                 {
786                 BN_bntest_rand(a,20+i*5,0,0); /**/
787                 BN_bntest_rand(b,2+i,0,0); /**/
788
789                 if (!BN_mod_exp(d,a,b,c,ctx))
790                         return(00);
791
792                 if (bp != NULL)
793                         {
794                         if (!results)
795                                 {
796                                 BN_print(bp,a);
797                                 BIO_puts(bp," ^ ");
798                                 BN_print(bp,b);
799                                 BIO_puts(bp," % ");
800                                 BN_print(bp,c);
801                                 BIO_puts(bp," - ");
802                                 }
803                         BN_print(bp,d);
804                         BIO_puts(bp,"\n");
805                         }
806                 BN_exp(e,a,b,ctx);
807                 BN_sub(e,e,d);
808                 BN_div(a,b,e,c,ctx);
809                 if(!BN_is_zero(b))
810                     {
811                     fprintf(stderr,"Modulo exponentiation test failed!\n");
812                     return 0;
813                     }
814                 }
815         BN_free(a);
816         BN_free(b);
817         BN_free(c);
818         BN_free(d);
819         BN_free(e);
820         return(1);
821         }
822
823 int test_exp(BIO *bp, BN_CTX *ctx)
824         {
825         BIGNUM *a,*b,*d,*e,*one;
826         int i;
827
828         a=BN_new();
829         b=BN_new();
830         d=BN_new();
831         e=BN_new();
832         one=BN_new();
833         BN_one(one);
834
835         for (i=0; i<num2; i++)
836                 {
837                 BN_bntest_rand(a,20+i*5,0,0); /**/
838                 BN_bntest_rand(b,2+i,0,0); /**/
839
840                 if (!BN_exp(d,a,b,ctx))
841                         return(00);
842
843                 if (bp != NULL)
844                         {
845                         if (!results)
846                                 {
847                                 BN_print(bp,a);
848                                 BIO_puts(bp," ^ ");
849                                 BN_print(bp,b);
850                                 BIO_puts(bp," - ");
851                                 }
852                         BN_print(bp,d);
853                         BIO_puts(bp,"\n");
854                         }
855                 BN_one(e);
856                 for( ; !BN_is_zero(b) ; BN_sub(b,b,one))
857                     BN_mul(e,e,a,ctx);
858                 BN_sub(e,e,d);
859                 if(!BN_is_zero(e))
860                     {
861                     fprintf(stderr,"Exponentiation test failed!\n");
862                     return 0;
863                     }
864                 }
865         BN_free(a);
866         BN_free(b);
867         BN_free(d);
868         BN_free(e);
869         BN_free(one);
870         return(1);
871         }
872
873 static void genprime_cb(int p, int n, void *arg)
874         {
875         char c='*';
876
877         if (p == 0) c='.';
878         if (p == 1) c='+';
879         if (p == 2) c='*';
880         if (p == 3) c='\n';
881         putc(c, stderr);
882         fflush(stderr);
883         (void)n;
884         (void)arg;
885         }
886
887 int test_kron(BIO *bp, BN_CTX *ctx)
888         {
889         BIGNUM *a,*b,*r,*t;
890         int i;
891         int legendre, kronecker;
892         int ret = 0;
893
894         a = BN_new();
895         b = BN_new();
896         r = BN_new();
897         t = BN_new();
898         if (a == NULL || b == NULL || r == NULL || t == NULL) goto err;
899         
900         /* We test BN_kronecker(a, b, ctx) just for  b  odd (Jacobi symbol).
901          * In this case we know that if  b  is prime, then BN_kronecker(a, b, ctx)
902          * is congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol).
903          * So we generate a random prime  b  and compare these values
904          * for a number of random  a's.  (That is, we run the Solovay-Strassen
905          * primality test to confirm that  b  is prime, except that we
906          * don't want to test whether  b  is prime but whether BN_kronecker
907          * works.) */
908
909         if (!BN_generate_prime(b, 512, 0, NULL, NULL, genprime_cb, NULL)) goto err;
910         b->neg = rand_neg();
911         putc('\n', stderr);
912
913         for (i = 0; i < num0; i++)
914                 {
915                 if (!BN_bntest_rand(a, 512, 0, 0)) goto err;
916                 a->neg = rand_neg();
917
918                 /* t := (|b|-1)/2  (note that b is odd) */
919                 if (!BN_copy(t, b)) goto err;
920                 t->neg = 0;
921                 if (!BN_sub_word(t, 1)) goto err;
922                 if (!BN_rshift1(t, t)) goto err;
923                 /* r := a^t mod b */
924                 b->neg=0;
925                 
926                 if (!BN_mod_exp_recp(r, a, t, b, ctx)) goto err; /* XXX should be BN_mod_exp_recp, but ..._recp triggers a bug that must be fixed */
927                 b->neg=1;
928
929                 if (BN_is_word(r, 1))
930                         legendre = 1;
931                 else if (BN_is_zero(r))
932                         legendre = 0;
933                 else
934                         {
935                         if (!BN_add_word(r, 1)) goto err;
936                         if (0 != BN_ucmp(r, b))
937                                 {
938                                 fprintf(stderr, "Legendre symbol computation failed\n");
939                                 goto err;
940                                 }
941                         legendre = -1;
942                         }
943                 
944                 kronecker = BN_kronecker(a, b, ctx);
945                 if (kronecker < -1) goto err;
946                 /* we actually need BN_kronecker(a, |b|) */
947                 if (a->neg && b->neg)
948                         kronecker = -kronecker;
949                 
950                 if (legendre != kronecker)
951                         {
952                         fprintf(stderr, "legendre != kronecker; a = ");
953                         BN_print_fp(stderr, a);
954                         fprintf(stderr, ", b = ");
955                         BN_print_fp(stderr, b);
956                         fprintf(stderr, "\n");
957                         goto err;
958                         }
959
960                 putc('.', stderr);
961                 fflush(stderr);
962                 }
963
964         putc('\n', stderr);
965         fflush(stderr);
966         ret = 1;
967  err:
968         if (a != NULL) BN_free(a);
969         if (b != NULL) BN_free(b);
970         if (r != NULL) BN_free(r);
971         if (t != NULL) BN_free(t);
972         return ret;
973         }
974
975 int test_sqrt(BIO *bp, BN_CTX *ctx)
976         {
977         BIGNUM *a,*p,*r;
978         int i, j;
979         int ret = 0;
980
981         a = BN_new();
982         p = BN_new();
983         r = BN_new();
984         if (a == NULL || p == NULL || r == NULL) goto err;
985         
986         for (i = 0; i < 16; i++)
987                 {
988                 if (i < 8)
989                         {
990                         unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 };
991                         
992                         if (!BN_set_word(p, primes[i])) goto err;
993                         }
994                 else
995                         {
996                         if (!BN_set_word(a, 32)) goto err;
997                         if (!BN_set_word(r, 2*i + 1)) goto err;
998                 
999                         if (!BN_generate_prime(p, 256, 0, a, r, genprime_cb, NULL)) goto err;
1000                         putc('\n', stderr);
1001                         }
1002                 p->neg = rand_neg();
1003
1004                 for (j = 0; j < num2; j++)
1005                         {
1006                         /* construct 'a' such that it is a square modulo p,
1007                          * but in general not a proper square and not reduced modulo p */
1008                         if (!BN_bntest_rand(r, 256, 0, 3)) goto err;
1009                         if (!BN_nnmod(r, r, p, ctx)) goto err;
1010                         if (!BN_mod_sqr(r, r, p, ctx)) goto err;
1011                         if (!BN_bntest_rand(a, 256, 0, 3)) goto err;
1012                         if (!BN_nnmod(a, a, p, ctx)) goto err;
1013                         if (!BN_mod_sqr(a, a, p, ctx)) goto err;
1014                         if (!BN_mul(a, a, r, ctx)) goto err;
1015                         if (rand_neg())
1016                                 if (!BN_sub(a, a, p)) goto err;
1017
1018                         if (!BN_mod_sqrt(r, a, p, ctx)) goto err;
1019                         if (!BN_mod_sqr(r, r, p, ctx)) goto err;
1020
1021                         if (!BN_nnmod(a, a, p, ctx)) goto err;
1022
1023                         if (BN_cmp(a, r) != 0)
1024                                 {
1025                                 fprintf(stderr, "BN_mod_sqrt failed: a = ");
1026                                 BN_print_fp(stderr, a);
1027                                 fprintf(stderr, ", r = ");
1028                                 BN_print_fp(stderr, r);
1029                                 fprintf(stderr, ", p = ");
1030                                 BN_print_fp(stderr, p);
1031                                 fprintf(stderr, "\n");
1032                                 goto err;
1033                                 }
1034
1035                         putc('.', stderr);
1036                         fflush(stderr);
1037                         }
1038                 
1039                 putc('\n', stderr);
1040                 fflush(stderr);
1041                 }
1042         ret = 1;
1043  err:
1044         if (a != NULL) BN_free(a);
1045         if (p != NULL) BN_free(p);
1046         if (r != NULL) BN_free(r);
1047         return ret;
1048         }
1049
1050 int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_)
1051         {
1052         BIGNUM *a,*b,*c,*d;
1053         int i;
1054
1055         b=BN_new();
1056         c=BN_new();
1057         d=BN_new();
1058         BN_one(c);
1059
1060         if(a_)
1061             a=a_;
1062         else
1063             {
1064             a=BN_new();
1065             BN_bntest_rand(a,200,0,0); /**/
1066             a->neg=rand_neg();
1067             }
1068         for (i=0; i<num0; i++)
1069                 {
1070                 BN_lshift(b,a,i+1);
1071                 BN_add(c,c,c);
1072                 if (bp != NULL)
1073                         {
1074                         if (!results)
1075                                 {
1076                                 BN_print(bp,a);
1077                                 BIO_puts(bp," * ");
1078                                 BN_print(bp,c);
1079                                 BIO_puts(bp," - ");
1080                                 }
1081                         BN_print(bp,b);
1082                         BIO_puts(bp,"\n");
1083                         }
1084                 BN_mul(d,a,c,ctx);
1085                 BN_sub(d,d,b);
1086                 if(!BN_is_zero(d))
1087                     {
1088                     fprintf(stderr,"Left shift test failed!\n");
1089                     fprintf(stderr,"a=");
1090                     BN_print_fp(stderr,a);
1091                     fprintf(stderr,"\nb=");
1092                     BN_print_fp(stderr,b);
1093                     fprintf(stderr,"\nc=");
1094                     BN_print_fp(stderr,c);
1095                     fprintf(stderr,"\nd=");
1096                     BN_print_fp(stderr,d);
1097                     fprintf(stderr,"\n");
1098                     return 0;
1099                     }
1100                 }
1101         BN_free(a);
1102         BN_free(b);
1103         BN_free(c);
1104         BN_free(d);
1105         return(1);
1106         }
1107
1108 int test_lshift1(BIO *bp)
1109         {
1110         BIGNUM *a,*b,*c;
1111         int i;
1112
1113         a=BN_new();
1114         b=BN_new();
1115         c=BN_new();
1116
1117         BN_bntest_rand(a,200,0,0); /**/
1118         a->neg=rand_neg();
1119         for (i=0; i<num0; i++)
1120                 {
1121                 BN_lshift1(b,a);
1122                 if (bp != NULL)
1123                         {
1124                         if (!results)
1125                                 {
1126                                 BN_print(bp,a);
1127                                 BIO_puts(bp," * 2");
1128                                 BIO_puts(bp," - ");
1129                                 }
1130                         BN_print(bp,b);
1131                         BIO_puts(bp,"\n");
1132                         }
1133                 BN_add(c,a,a);
1134                 BN_sub(a,b,c);
1135                 if(!BN_is_zero(a))
1136                     {
1137                     fprintf(stderr,"Left shift one test failed!\n");
1138                     return 0;
1139                     }
1140                 
1141                 BN_copy(a,b);
1142                 }
1143         BN_free(a);
1144         BN_free(b);
1145         BN_free(c);
1146         return(1);
1147         }
1148
1149 int test_rshift(BIO *bp,BN_CTX *ctx)
1150         {
1151         BIGNUM *a,*b,*c,*d,*e;
1152         int i;
1153
1154         a=BN_new();
1155         b=BN_new();
1156         c=BN_new();
1157         d=BN_new();
1158         e=BN_new();
1159         BN_one(c);
1160
1161         BN_bntest_rand(a,200,0,0); /**/
1162         a->neg=rand_neg();
1163         for (i=0; i<num0; i++)
1164                 {
1165                 BN_rshift(b,a,i+1);
1166                 BN_add(c,c,c);
1167                 if (bp != NULL)
1168                         {
1169                         if (!results)
1170                                 {
1171                                 BN_print(bp,a);
1172                                 BIO_puts(bp," / ");
1173                                 BN_print(bp,c);
1174                                 BIO_puts(bp," - ");
1175                                 }
1176                         BN_print(bp,b);
1177                         BIO_puts(bp,"\n");
1178                         }
1179                 BN_div(d,e,a,c,ctx);
1180                 BN_sub(d,d,b);
1181                 if(!BN_is_zero(d))
1182                     {
1183                     fprintf(stderr,"Right shift test failed!\n");
1184                     return 0;
1185                     }
1186                 }
1187         BN_free(a);
1188         BN_free(b);
1189         BN_free(c);
1190         BN_free(d);
1191         BN_free(e);
1192         return(1);
1193         }
1194
1195 int test_rshift1(BIO *bp)
1196         {
1197         BIGNUM *a,*b,*c;
1198         int i;
1199
1200         a=BN_new();
1201         b=BN_new();
1202         c=BN_new();
1203
1204         BN_bntest_rand(a,200,0,0); /**/
1205         a->neg=rand_neg();
1206         for (i=0; i<num0; i++)
1207                 {
1208                 BN_rshift1(b,a);
1209                 if (bp != NULL)
1210                         {
1211                         if (!results)
1212                                 {
1213                                 BN_print(bp,a);
1214                                 BIO_puts(bp," / 2");
1215                                 BIO_puts(bp," - ");
1216                                 }
1217                         BN_print(bp,b);
1218                         BIO_puts(bp,"\n");
1219                         }
1220                 BN_sub(c,a,b);
1221                 BN_sub(c,c,b);
1222                 if(!BN_is_zero(c) && !BN_abs_is_word(c, 1))
1223                     {
1224                     fprintf(stderr,"Right shift one test failed!\n");
1225                     return 0;
1226                     }
1227                 BN_copy(a,b);
1228                 }
1229         BN_free(a);
1230         BN_free(b);
1231         BN_free(c);
1232         return(1);
1233         }
1234
1235 int rand_neg(void)
1236         {
1237         static unsigned int neg=0;
1238         static int sign[8]={0,0,0,1,1,0,1,1};
1239
1240         return(sign[(neg++)%8]);
1241         }