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