91563dd22228ad1a314a24c4576f06027f66f7de
[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
259         BN_init(&a);
260         BN_init(&b);
261         BN_init(&c);
262
263         BN_bntest_rand(&a,512,0,0);
264         for (i=0; i<num0; i++)
265                 {
266                 BN_bntest_rand(&b,450+i,0,0);
267                 a.neg=rand_neg();
268                 b.neg=rand_neg();
269                 BN_add(&c,&a,&b);
270                 if (bp != NULL)
271                         {
272                         if (!results)
273                                 {
274                                 BN_print(bp,&a);
275                                 BIO_puts(bp," + ");
276                                 BN_print(bp,&b);
277                                 BIO_puts(bp," - ");
278                                 }
279                         BN_print(bp,&c);
280                         BIO_puts(bp,"\n");
281                         }
282                 a.neg=!a.neg;
283                 b.neg=!b.neg;
284                 BN_add(&c,&c,&b);
285                 BN_add(&c,&c,&a);
286                 if(!BN_is_zero(&c))
287                     {
288                     fprintf(stderr,"Add test failed!\n");
289                     return 0;
290                     }
291                 }
292         BN_free(&a);
293         BN_free(&b);
294         BN_free(&c);
295         return(1);
296         }
297
298 int test_sub(BIO *bp)
299         {
300         BIGNUM a,b,c;
301         int i;
302
303         BN_init(&a);
304         BN_init(&b);
305         BN_init(&c);
306
307         for (i=0; i<num0+num1; i++)
308                 {
309                 if (i < num1)
310                         {
311                         BN_bntest_rand(&a,512,0,0);
312                         BN_copy(&b,&a);
313                         if (BN_set_bit(&a,i)==0) return(0);
314                         BN_add_word(&b,i);
315                         }
316                 else
317                         {
318                         BN_bntest_rand(&b,400+i-num1,0,0);
319                         a.neg=rand_neg();
320                         b.neg=rand_neg();
321                         }
322                 BN_sub(&c,&a,&b);
323                 if (bp != NULL)
324                         {
325                         if (!results)
326                                 {
327                                 BN_print(bp,&a);
328                                 BIO_puts(bp," - ");
329                                 BN_print(bp,&b);
330                                 BIO_puts(bp," - ");
331                                 }
332                         BN_print(bp,&c);
333                         BIO_puts(bp,"\n");
334                         }
335                 BN_add(&c,&c,&b);
336                 BN_sub(&c,&c,&a);
337                 if(!BN_is_zero(&c))
338                     {
339                     fprintf(stderr,"Subtract test failed!\n");
340                     return 0;
341                     }
342                 }
343         BN_free(&a);
344         BN_free(&b);
345         BN_free(&c);
346         return(1);
347         }
348
349 int test_div(BIO *bp, BN_CTX *ctx)
350         {
351         BIGNUM a,b,c,d,e;
352         int i;
353
354         BN_init(&a);
355         BN_init(&b);
356         BN_init(&c);
357         BN_init(&d);
358         BN_init(&e);
359
360         for (i=0; i<num0+num1; i++)
361                 {
362                 if (i < num1)
363                         {
364                         BN_bntest_rand(&a,400,0,0);
365                         BN_copy(&b,&a);
366                         BN_lshift(&a,&a,i);
367                         BN_add_word(&a,i);
368                         }
369                 else
370                         BN_bntest_rand(&b,50+3*(i-num1),0,0);
371                 a.neg=rand_neg();
372                 b.neg=rand_neg();
373                 BN_div(&d,&c,&a,&b,ctx);
374                 if (bp != NULL)
375                         {
376                         if (!results)
377                                 {
378                                 BN_print(bp,&a);
379                                 BIO_puts(bp," / ");
380                                 BN_print(bp,&b);
381                                 BIO_puts(bp," - ");
382                                 }
383                         BN_print(bp,&d);
384                         BIO_puts(bp,"\n");
385
386                         if (!results)
387                                 {
388                                 BN_print(bp,&a);
389                                 BIO_puts(bp," % ");
390                                 BN_print(bp,&b);
391                                 BIO_puts(bp," - ");
392                                 }
393                         BN_print(bp,&c);
394                         BIO_puts(bp,"\n");
395                         }
396                 BN_mul(&e,&d,&b,ctx);
397                 BN_add(&d,&e,&c);
398                 BN_sub(&d,&d,&a);
399                 if(!BN_is_zero(&d))
400                     {
401                     fprintf(stderr,"Division test failed!\n");
402                     return 0;
403                     }
404                 }
405         BN_free(&a);
406         BN_free(&b);
407         BN_free(&c);
408         BN_free(&d);
409         BN_free(&e);
410         return(1);
411         }
412
413 int test_div_recp(BIO *bp, BN_CTX *ctx)
414         {
415         BIGNUM a,b,c,d,e;
416         BN_RECP_CTX recp;
417         int i;
418
419         BN_RECP_CTX_init(&recp);
420         BN_init(&a);
421         BN_init(&b);
422         BN_init(&c);
423         BN_init(&d);
424         BN_init(&e);
425
426         for (i=0; i<num0+num1; i++)
427                 {
428                 if (i < num1)
429                         {
430                         BN_bntest_rand(&a,400,0,0);
431                         BN_copy(&b,&a);
432                         BN_lshift(&a,&a,i);
433                         BN_add_word(&a,i);
434                         }
435                 else
436                         BN_bntest_rand(&b,50+3*(i-num1),0,0);
437                 a.neg=rand_neg();
438                 b.neg=rand_neg();
439                 BN_RECP_CTX_set(&recp,&b,ctx);
440                 BN_div_recp(&d,&c,&a,&recp,ctx);
441                 if (bp != NULL)
442                         {
443                         if (!results)
444                                 {
445                                 BN_print(bp,&a);
446                                 BIO_puts(bp," / ");
447                                 BN_print(bp,&b);
448                                 BIO_puts(bp," - ");
449                                 }
450                         BN_print(bp,&d);
451                         BIO_puts(bp,"\n");
452
453                         if (!results)
454                                 {
455                                 BN_print(bp,&a);
456                                 BIO_puts(bp," % ");
457                                 BN_print(bp,&b);
458                                 BIO_puts(bp," - ");
459                                 }
460                         BN_print(bp,&c);
461                         BIO_puts(bp,"\n");
462                         }
463                 BN_mul(&e,&d,&b,ctx);
464                 BN_add(&d,&e,&c);
465                 BN_sub(&d,&d,&a);
466                 if(!BN_is_zero(&d))
467                     {
468                     fprintf(stderr,"Reciprocal division test failed!\n");
469                     fprintf(stderr,"a=");
470                     BN_print_fp(stderr,&a);
471                     fprintf(stderr,"\nb=");
472                     BN_print_fp(stderr,&b);
473                     fprintf(stderr,"\n");
474                     return 0;
475                     }
476                 }
477         BN_free(&a);
478         BN_free(&b);
479         BN_free(&c);
480         BN_free(&d);
481         BN_free(&e);
482         BN_RECP_CTX_free(&recp);
483         return(1);
484         }
485
486 int test_mul(BIO *bp)
487         {
488         BIGNUM a,b,c,d,e;
489         int i;
490         BN_CTX ctx;
491
492         BN_CTX_init(&ctx);
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;
707
708         a=BN_new();
709         b=BN_new();
710         c=BN_new();
711         d=BN_new();
712         e=BN_new();
713
714         BN_bntest_rand(c,1024,0,0); /**/
715         for (i=0; i<num0; i++)
716                 {
717                 BN_bntest_rand(a,475+i*10,0,0); /**/
718                 BN_bntest_rand(b,425+i*11,0,0); /**/
719                 a->neg=rand_neg();
720                 b->neg=rand_neg();
721                 if (!BN_mod_mul(e,a,b,c,ctx))
722                         {
723                         unsigned long l;
724
725                         while ((l=ERR_get_error()))
726                                 fprintf(stderr,"ERROR:%s\n",
727                                         ERR_error_string(l,NULL));
728                         exit(1);
729                         }
730                 if (bp != NULL)
731                         {
732                         if (!results)
733                                 {
734                                 BN_print(bp,a);
735                                 BIO_puts(bp," * ");
736                                 BN_print(bp,b);
737                                 BIO_puts(bp," % ");
738                                 BN_print(bp,c);
739                                 if ((a->neg ^ b->neg) && !BN_is_zero(e))
740                                         {
741                                         /* If  (a*b) % c  is negative,  c  must be added
742                                          * in order to obtain the normalized remainder
743                                          * (new with OpenSSL 0.9.7, previous versions of
744                                          * BN_mod_mul could generate negative results)
745                                          */
746                                         BIO_puts(bp," + ");
747                                         BN_print(bp,c);
748                                         }
749                                 BIO_puts(bp," - ");
750                                 }
751                         BN_print(bp,e);
752                         BIO_puts(bp,"\n");
753                         }
754                 BN_mul(d,a,b,ctx);
755                 BN_sub(d,d,e);
756                 BN_div(a,b,d,c,ctx);
757                 if(!BN_is_zero(b))
758                     {
759                     fprintf(stderr,"Modulo multiply test failed!\n");
760                     ERR_print_errors_fp(stderr);
761                     return 0;
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         putc('\n', stderr);
911
912         for (i = 0; i < num0; i++)
913                 {
914                 if (!BN_bntest_rand(a, 512, 0, 0)) goto err;
915                 a->neg = rand_neg();
916
917                 /* t := (b-1)/2  (note that b is odd) */
918                 if (!BN_copy(t, b)) goto err;
919                 if (!BN_sub_word(t, 1)) goto err;
920                 if (!BN_rshift1(t, t)) goto err;
921                 /* r := a^t mod b */
922                 if (!BN_mod_exp(r, a, t, b, ctx)) goto err;
923
924                 if (BN_is_word(r, 1))
925                         legendre = 1;
926                 else if (BN_is_zero(r))
927                         legendre = 0;
928                 else
929                         {
930                         if (!BN_add_word(r, 1)) goto err;
931                         if (0 != BN_cmp(r, b))
932                                 {
933                                 fprintf(stderr, "Legendre symbol computation failed\n");
934                                 goto err;
935                                 }
936                         legendre = -1;
937                         }
938                 
939                 kronecker = BN_kronecker(a, b, ctx);
940                 if (kronecker < -1) goto err;
941                 
942                 if (legendre != kronecker)
943                         {
944                         fprintf(stderr, "legendre != kronecker; a = ");
945                         BN_print_fp(stderr, a);
946                         fprintf(stderr, ", b = ");
947                         BN_print_fp(stderr, b);
948                         fprintf(stderr, "\n");
949                         goto err;
950                         }
951
952                 putc('.', stderr);
953                 fflush(stderr);
954                 }
955
956         putc('\n', stderr);
957         fflush(stderr);
958         ret = 1;
959  err:
960         if (a != NULL) BN_free(a);
961         if (b != NULL) BN_free(b);
962         if (r != NULL) BN_free(r);
963         if (t != NULL) BN_free(t);
964         return ret;
965         }
966
967 int test_sqrt(BIO *bp, BN_CTX *ctx)
968         {
969         BIGNUM *a,*p,*r;
970         int i, j;
971         int ret = 0;
972
973         a = BN_new();
974         p = BN_new();
975         r = BN_new();
976         if (a == NULL || p == NULL || r == NULL) goto err;
977         
978         for (i = 0; i < 16; i++)
979                 {
980                 if (i < 8)
981                         {
982                         unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 };
983                         
984                         if (!BN_set_word(p, primes[i])) goto err;
985                         }
986                 else
987                         {
988                         if (!BN_set_word(a, 32)) goto err;
989                         if (!BN_set_word(r, 2*i + 1)) goto err;
990                 
991                         if (!BN_generate_prime(p, 256, 0, a, r, genprime_cb, NULL)) goto err;
992                         putc('\n', stderr);
993                         }
994
995                 for (j = 0; j < num2; j++)
996                         {
997                         /* construct 'a' such that it is a square modulo p,
998                          * but in general not a proper square and not reduced modulo p */
999                         if (!BN_bntest_rand(r, 256, 0, 3)) goto err;
1000                         if (!BN_nnmod(r, r, p, ctx)) goto err;
1001                         if (!BN_mod_sqr(r, r, p, ctx)) goto err;
1002                         if (!BN_bntest_rand(a, 256, 0, 3)) goto err;
1003                         if (!BN_nnmod(a, a, p, ctx)) goto err;
1004                         if (!BN_mod_sqr(a, a, p, ctx)) goto err;
1005                         if (!BN_mul(a, a, r, ctx)) goto err;
1006
1007                         if (!BN_mod_sqrt(r, a, p, ctx)) goto err;
1008                         if (!BN_mod_sqr(r, r, p, ctx)) goto err;
1009
1010                         if (!BN_nnmod(a, a, p, ctx)) goto err;
1011
1012                         if (BN_cmp(a, r) != 0)
1013                                 {
1014                                 fprintf(stderr, "BN_mod_sqrt failed: a = ");
1015                                 BN_print_fp(stderr, a);
1016                                 fprintf(stderr, ", r = ");
1017                                 BN_print_fp(stderr, r);
1018                                 fprintf(stderr, ", p = ");
1019                                 BN_print_fp(stderr, p);
1020                                 fprintf(stderr, "\n");
1021                                 goto err;
1022                                 }
1023
1024                         putc('.', stderr);
1025                         fflush(stderr);
1026                         }
1027                 
1028                 putc('\n', stderr);
1029                 fflush(stderr);
1030                 }
1031         ret = 1;
1032  err:
1033         if (a != NULL) BN_free(a);
1034         if (p != NULL) BN_free(p);
1035         if (r != NULL) BN_free(r);
1036         return ret;
1037         }
1038
1039 int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_)
1040         {
1041         BIGNUM *a,*b,*c,*d;
1042         int i;
1043
1044         b=BN_new();
1045         c=BN_new();
1046         d=BN_new();
1047         BN_one(c);
1048
1049         if(a_)
1050             a=a_;
1051         else
1052             {
1053             a=BN_new();
1054             BN_bntest_rand(a,200,0,0); /**/
1055             a->neg=rand_neg();
1056             }
1057         for (i=0; i<num0; i++)
1058                 {
1059                 BN_lshift(b,a,i+1);
1060                 BN_add(c,c,c);
1061                 if (bp != NULL)
1062                         {
1063                         if (!results)
1064                                 {
1065                                 BN_print(bp,a);
1066                                 BIO_puts(bp," * ");
1067                                 BN_print(bp,c);
1068                                 BIO_puts(bp," - ");
1069                                 }
1070                         BN_print(bp,b);
1071                         BIO_puts(bp,"\n");
1072                         }
1073                 BN_mul(d,a,c,ctx);
1074                 BN_sub(d,d,b);
1075                 if(!BN_is_zero(d))
1076                     {
1077                     fprintf(stderr,"Left shift test failed!\n");
1078                     fprintf(stderr,"a=");
1079                     BN_print_fp(stderr,a);
1080                     fprintf(stderr,"\nb=");
1081                     BN_print_fp(stderr,b);
1082                     fprintf(stderr,"\nc=");
1083                     BN_print_fp(stderr,c);
1084                     fprintf(stderr,"\nd=");
1085                     BN_print_fp(stderr,d);
1086                     fprintf(stderr,"\n");
1087                     return 0;
1088                     }
1089                 }
1090         BN_free(a);
1091         BN_free(b);
1092         BN_free(c);
1093         BN_free(d);
1094         return(1);
1095         }
1096
1097 int test_lshift1(BIO *bp)
1098         {
1099         BIGNUM *a,*b,*c;
1100         int i;
1101
1102         a=BN_new();
1103         b=BN_new();
1104         c=BN_new();
1105
1106         BN_bntest_rand(a,200,0,0); /**/
1107         a->neg=rand_neg();
1108         for (i=0; i<num0; i++)
1109                 {
1110                 BN_lshift1(b,a);
1111                 if (bp != NULL)
1112                         {
1113                         if (!results)
1114                                 {
1115                                 BN_print(bp,a);
1116                                 BIO_puts(bp," * 2");
1117                                 BIO_puts(bp," - ");
1118                                 }
1119                         BN_print(bp,b);
1120                         BIO_puts(bp,"\n");
1121                         }
1122                 BN_add(c,a,a);
1123                 BN_sub(a,b,c);
1124                 if(!BN_is_zero(a))
1125                     {
1126                     fprintf(stderr,"Left shift one test failed!\n");
1127                     return 0;
1128                     }
1129                 
1130                 BN_copy(a,b);
1131                 }
1132         BN_free(a);
1133         BN_free(b);
1134         BN_free(c);
1135         return(1);
1136         }
1137
1138 int test_rshift(BIO *bp,BN_CTX *ctx)
1139         {
1140         BIGNUM *a,*b,*c,*d,*e;
1141         int i;
1142
1143         a=BN_new();
1144         b=BN_new();
1145         c=BN_new();
1146         d=BN_new();
1147         e=BN_new();
1148         BN_one(c);
1149
1150         BN_bntest_rand(a,200,0,0); /**/
1151         a->neg=rand_neg();
1152         for (i=0; i<num0; i++)
1153                 {
1154                 BN_rshift(b,a,i+1);
1155                 BN_add(c,c,c);
1156                 if (bp != NULL)
1157                         {
1158                         if (!results)
1159                                 {
1160                                 BN_print(bp,a);
1161                                 BIO_puts(bp," / ");
1162                                 BN_print(bp,c);
1163                                 BIO_puts(bp," - ");
1164                                 }
1165                         BN_print(bp,b);
1166                         BIO_puts(bp,"\n");
1167                         }
1168                 BN_div(d,e,a,c,ctx);
1169                 BN_sub(d,d,b);
1170                 if(!BN_is_zero(d))
1171                     {
1172                     fprintf(stderr,"Right shift test failed!\n");
1173                     return 0;
1174                     }
1175                 }
1176         BN_free(a);
1177         BN_free(b);
1178         BN_free(c);
1179         BN_free(d);
1180         BN_free(e);
1181         return(1);
1182         }
1183
1184 int test_rshift1(BIO *bp)
1185         {
1186         BIGNUM *a,*b,*c;
1187         int i;
1188
1189         a=BN_new();
1190         b=BN_new();
1191         c=BN_new();
1192
1193         BN_bntest_rand(a,200,0,0); /**/
1194         a->neg=rand_neg();
1195         for (i=0; i<num0; i++)
1196                 {
1197                 BN_rshift1(b,a);
1198                 if (bp != NULL)
1199                         {
1200                         if (!results)
1201                                 {
1202                                 BN_print(bp,a);
1203                                 BIO_puts(bp," / 2");
1204                                 BIO_puts(bp," - ");
1205                                 }
1206                         BN_print(bp,b);
1207                         BIO_puts(bp,"\n");
1208                         }
1209                 BN_sub(c,a,b);
1210                 BN_sub(c,c,b);
1211                 if(!BN_is_zero(c) && !BN_is_one(c))
1212                     {
1213                     fprintf(stderr,"Right shift one test failed!\n");
1214                     return 0;
1215                     }
1216                 BN_copy(a,b);
1217                 }
1218         BN_free(a);
1219         BN_free(b);
1220         BN_free(c);
1221         return(1);
1222         }
1223
1224 int rand_neg(void)
1225         {
1226         static unsigned int neg=0;
1227         static int sign[8]={0,0,0,1,1,0,1,1};
1228
1229         return(sign[(neg++)%8]);
1230         }