1 /* crypto/bn/bntest.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
58 /* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
61 * Portions of the attached software ("Contribution") are developed by
62 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
64 * The Contribution is licensed pursuant to the Eric Young open source
65 * license provided above.
67 * In addition, Sun covenants to all licensees who provide a reciprocal
68 * covenant with respect to their own patents if any, not to sue under
69 * current and future patent claims necessarily infringed by the making,
70 * using, practicing, selling, offering for sale and/or otherwise
71 * disposing of the Contribution as delivered hereunder
72 * (or portions thereof), provided that such covenant shall not apply:
73 * 1) for code that a licensee deletes from the Contribution;
74 * 2) separates from the Contribution; or
75 * 3) for infringements caused by:
76 * i) the modification of the Contribution or
77 * ii) the combination of the Contribution with other software or
78 * devices where such combination causes the infringement.
80 * The binary polynomial arithmetic software is originally written by
81 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
91 #include <openssl/bio.h>
92 #include <openssl/bn.h>
93 #include <openssl/rand.h>
94 #include <openssl/x509.h>
95 #include <openssl/err.h>
97 #ifdef OPENSSL_SYS_WINDOWS
98 #include "../bio/bss_file.c"
101 const int num0 = 100; /* number of tests */
102 const int num1 = 50; /* additional tests for some functions */
103 const int num2 = 5; /* number of tests for slow functions */
105 int test_add(BIO *bp);
106 int test_sub(BIO *bp);
107 int test_lshift1(BIO *bp);
108 int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_);
109 int test_rshift1(BIO *bp);
110 int test_rshift(BIO *bp,BN_CTX *ctx);
111 int test_div(BIO *bp,BN_CTX *ctx);
112 int test_div_recp(BIO *bp,BN_CTX *ctx);
113 int test_mul(BIO *bp);
114 int test_sqr(BIO *bp,BN_CTX *ctx);
115 int test_mont(BIO *bp,BN_CTX *ctx);
116 int test_mod(BIO *bp,BN_CTX *ctx);
117 int test_mod_mul(BIO *bp,BN_CTX *ctx);
118 int test_mod_exp(BIO *bp,BN_CTX *ctx);
119 int test_exp(BIO *bp,BN_CTX *ctx);
120 int test_gf2m_add(BIO *bp);
121 int test_gf2m_mod(BIO *bp);
122 int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx);
123 int test_gf2m_mod_sqr(BIO *bp,BN_CTX *ctx);
124 int test_gf2m_mod_inv(BIO *bp,BN_CTX *ctx);
125 int test_gf2m_mod_div(BIO *bp,BN_CTX *ctx);
126 int test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx);
127 int test_gf2m_mod_sqrt(BIO *bp,BN_CTX *ctx);
128 int test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx);
129 int test_kron(BIO *bp,BN_CTX *ctx);
130 int test_sqrt(BIO *bp,BN_CTX *ctx);
132 static int results=0;
134 #ifdef OPENSSL_NO_STDIO
136 #include "bss_file.c"
139 static unsigned char lst[]="\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9"
140 "\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0";
142 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
144 static void message(BIO *out, char *m)
146 fprintf(stderr, "test %s\n", m);
147 BIO_puts(out, "print \"test ");
149 BIO_puts(out, "\\n\"\n");
152 int main(int argc, char *argv[])
160 RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
166 if (strcmp(*argv,"-results") == 0)
168 else if (strcmp(*argv,"-out") == 0)
170 if (--argc < 1) break;
179 if (ctx == NULL) exit(1);
181 out=BIO_new(BIO_s_file());
182 if (out == NULL) exit(1);
185 BIO_set_fp(out,stdout,BIO_NOCLOSE);
189 if (!BIO_write_filename(out,outfile))
197 BIO_puts(out,"obase=16\nibase=16\n");
199 message(out,"BN_add");
200 if (!test_add(out)) goto err;
203 message(out,"BN_sub");
204 if (!test_sub(out)) goto err;
207 message(out,"BN_lshift1");
208 if (!test_lshift1(out)) goto err;
211 message(out,"BN_lshift (fixed)");
212 if (!test_lshift(out,ctx,BN_bin2bn(lst,sizeof(lst)-1,NULL)))
216 message(out,"BN_lshift");
217 if (!test_lshift(out,ctx,NULL)) goto err;
220 message(out,"BN_rshift1");
221 if (!test_rshift1(out)) goto err;
224 message(out,"BN_rshift");
225 if (!test_rshift(out,ctx)) goto err;
228 message(out,"BN_sqr");
229 if (!test_sqr(out,ctx)) goto err;
232 message(out,"BN_mul");
233 if (!test_mul(out)) goto err;
236 message(out,"BN_div");
237 if (!test_div(out,ctx)) goto err;
240 message(out,"BN_div_recp");
241 if (!test_div_recp(out,ctx)) goto err;
244 message(out,"BN_mod");
245 if (!test_mod(out,ctx)) goto err;
248 message(out,"BN_mod_mul");
249 if (!test_mod_mul(out,ctx)) goto err;
252 message(out,"BN_mont");
253 if (!test_mont(out,ctx)) goto err;
256 message(out,"BN_mod_exp");
257 if (!test_mod_exp(out,ctx)) goto err;
260 message(out,"BN_exp");
261 if (!test_exp(out,ctx)) goto err;
264 message(out,"BN_kronecker");
265 if (!test_kron(out,ctx)) goto err;
268 message(out,"BN_mod_sqrt");
269 if (!test_sqrt(out,ctx)) goto err;
272 message(out,"BN_GF2m_add");
273 if (!test_gf2m_add(out)) goto err;
276 message(out,"BN_GF2m_mod");
277 if (!test_gf2m_mod(out)) goto err;
280 message(out,"BN_GF2m_mod_mul");
281 if (!test_gf2m_mod_mul(out,ctx)) goto err;
284 message(out,"BN_GF2m_mod_sqr");
285 if (!test_gf2m_mod_sqr(out,ctx)) goto err;
288 message(out,"BN_GF2m_mod_inv");
289 if (!test_gf2m_mod_inv(out,ctx)) goto err;
292 message(out,"BN_GF2m_mod_div");
293 if (!test_gf2m_mod_div(out,ctx)) goto err;
296 message(out,"BN_GF2m_mod_exp");
297 if (!test_gf2m_mod_exp(out,ctx)) goto err;
300 message(out,"BN_GF2m_mod_sqrt");
301 if (!test_gf2m_mod_sqrt(out,ctx)) goto err;
304 message(out,"BN_GF2m_mod_solve_quad");
305 if (!test_gf2m_mod_solve_quad(out,ctx)) goto err;
314 BIO_puts(out,"1\n"); /* make sure the Perl script fed by bc notices
315 * the failure, see test_bn in test/Makefile.ssl*/
317 ERR_load_crypto_strings();
318 ERR_print_errors_fp(stderr);
323 int test_add(BIO *bp)
332 BN_bntest_rand(&a,512,0,0);
333 for (i=0; i<num0; i++)
335 BN_bntest_rand(&b,450+i,0,0);
357 fprintf(stderr,"Add test failed!\n");
367 int test_sub(BIO *bp)
376 for (i=0; i<num0+num1; i++)
380 BN_bntest_rand(&a,512,0,0);
382 if (BN_set_bit(&a,i)==0) return(0);
387 BN_bntest_rand(&b,400+i-num1,0,0);
408 fprintf(stderr,"Subtract test failed!\n");
418 int test_div(BIO *bp, BN_CTX *ctx)
429 for (i=0; i<num0+num1; i++)
433 BN_bntest_rand(&a,400,0,0);
439 BN_bntest_rand(&b,50+3*(i-num1),0,0);
442 BN_div(&d,&c,&a,&b,ctx);
465 BN_mul(&e,&d,&b,ctx);
470 fprintf(stderr,"Division test failed!\n");
482 int test_div_recp(BIO *bp, BN_CTX *ctx)
488 BN_RECP_CTX_init(&recp);
495 for (i=0; i<num0+num1; i++)
499 BN_bntest_rand(&a,400,0,0);
505 BN_bntest_rand(&b,50+3*(i-num1),0,0);
508 BN_RECP_CTX_set(&recp,&b,ctx);
509 BN_div_recp(&d,&c,&a,&recp,ctx);
532 BN_mul(&e,&d,&b,ctx);
537 fprintf(stderr,"Reciprocal division test failed!\n");
538 fprintf(stderr,"a=");
539 BN_print_fp(stderr,&a);
540 fprintf(stderr,"\nb=");
541 BN_print_fp(stderr,&b);
542 fprintf(stderr,"\n");
551 BN_RECP_CTX_free(&recp);
555 int test_mul(BIO *bp)
562 if (ctx == NULL) exit(1);
570 for (i=0; i<num0+num1; i++)
574 BN_bntest_rand(&a,100,0,0);
575 BN_bntest_rand(&b,100,0,0);
578 BN_bntest_rand(&b,i-num1,0,0);
581 BN_mul(&c,&a,&b,ctx);
594 BN_div(&d,&e,&c,&a,ctx);
596 if(!BN_is_zero(&d) || !BN_is_zero(&e))
598 fprintf(stderr,"Multiplication test failed!\n");
611 int test_sqr(BIO *bp, BN_CTX *ctx)
621 for (i=0; i<num0; i++)
623 BN_bntest_rand(&a,40+i*10,0,0);
638 BN_div(&d,&e,&c,&a,ctx);
640 if(!BN_is_zero(&d) || !BN_is_zero(&e))
642 fprintf(stderr,"Square test failed!\n");
653 int test_mont(BIO *bp, BN_CTX *ctx)
668 mont=BN_MONT_CTX_new();
670 BN_bntest_rand(&a,100,0,0); /**/
671 BN_bntest_rand(&b,100,0,0); /**/
672 for (i=0; i<num2; i++)
674 int bits = (200*(i+1))/num2;
678 BN_bntest_rand(&n,bits,0,1);
679 BN_MONT_CTX_set(mont,&n,ctx);
681 BN_nnmod(&a,&a,&n,ctx);
682 BN_nnmod(&b,&b,&n,ctx);
684 BN_to_montgomery(&A,&a,mont,ctx);
685 BN_to_montgomery(&B,&b,mont,ctx);
687 BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
688 BN_from_montgomery(&A,&c,mont,ctx);/**/
694 fprintf(stderr,"%d * %d %% %d\n",
697 BN_num_bits(mont->N));
703 BN_print(bp,&(mont->N));
709 BN_mod_mul(&d,&a,&b,&n,ctx);
713 fprintf(stderr,"Montgomery multiplication test failed!\n");
717 BN_MONT_CTX_free(mont);
728 int test_mod(BIO *bp, BN_CTX *ctx)
730 BIGNUM *a,*b,*c,*d,*e;
739 BN_bntest_rand(a,1024,0,0); /**/
740 for (i=0; i<num0; i++)
742 BN_bntest_rand(b,450+i*10,0,0); /**/
745 BN_mod(c,a,b,ctx);/**/
762 fprintf(stderr,"Modulo test failed!\n");
774 int test_mod_mul(BIO *bp, BN_CTX *ctx)
776 BIGNUM *a,*b,*c,*d,*e;
785 for (j=0; j<3; j++) {
786 BN_bntest_rand(c,1024,0,0); /**/
787 for (i=0; i<num0; i++)
789 BN_bntest_rand(a,475+i*10,0,0); /**/
790 BN_bntest_rand(b,425+i*11,0,0); /**/
793 if (!BN_mod_mul(e,a,b,c,ctx))
797 while ((l=ERR_get_error()))
798 fprintf(stderr,"ERROR:%s\n",
799 ERR_error_string(l,NULL));
811 if ((a->neg ^ b->neg) && !BN_is_zero(e))
813 /* If (a*b) % c is negative, c must be added
814 * in order to obtain the normalized remainder
815 * (new with OpenSSL 0.9.7, previous versions of
816 * BN_mod_mul could generate negative results)
831 fprintf(stderr,"Modulo multiply test failed!\n");
832 ERR_print_errors_fp(stderr);
845 int test_mod_exp(BIO *bp, BN_CTX *ctx)
847 BIGNUM *a,*b,*c,*d,*e;
856 BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */
857 for (i=0; i<num2; i++)
859 BN_bntest_rand(a,20+i*5,0,0); /**/
860 BN_bntest_rand(b,2+i,0,0); /**/
862 if (!BN_mod_exp(d,a,b,c,ctx))
884 fprintf(stderr,"Modulo exponentiation test failed!\n");
896 int test_exp(BIO *bp, BN_CTX *ctx)
898 BIGNUM *a,*b,*d,*e,*one;
908 for (i=0; i<num2; i++)
910 BN_bntest_rand(a,20+i*5,0,0); /**/
911 BN_bntest_rand(b,2+i,0,0); /**/
913 if (!BN_exp(d,a,b,ctx))
929 for( ; !BN_is_zero(b) ; BN_sub(b,b,one))
934 fprintf(stderr,"Exponentiation test failed!\n");
946 int test_gf2m_add(BIO *bp)
955 for (i=0; i<num0; i++)
958 BN_copy(&b, BN_value_one());
961 BN_GF2m_add(&c,&a,&b);
962 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
976 /* Test that two added values have the correct parity. */
977 if((BN_is_odd(&a) && BN_is_odd(&c)) || (!BN_is_odd(&a) && !BN_is_odd(&c)))
979 fprintf(stderr,"GF(2^m) addition test (a) failed!\n");
982 BN_GF2m_add(&c,&c,&c);
983 /* Test that c + c = 0. */
986 fprintf(stderr,"GF(2^m) addition test (b) failed!\n");
998 int test_gf2m_mod(BIO *bp)
1000 BIGNUM *a,*b[2],*c,*d,*e;
1002 unsigned int p0[] = {163,7,6,3,0};
1003 unsigned int p1[] = {193,15,0};
1012 BN_GF2m_arr2poly(p0, b[0]);
1013 BN_GF2m_arr2poly(p1, b[1]);
1015 for (i=0; i<num0; i++)
1017 BN_bntest_rand(a, 1024, 0, 0);
1018 for (j=0; j < 2; j++)
1020 BN_GF2m_mod(c, a, b[j]);
1021 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1035 BN_GF2m_add(d, a, c);
1036 BN_GF2m_mod(e, d, b[j]);
1037 /* Test that a + (a mod p) mod p == 0. */
1040 fprintf(stderr,"GF(2^m) modulo test failed!\n");
1056 int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx)
1058 BIGNUM *a,*b[2],*c,*d,*e,*f,*g,*h;
1060 unsigned int p0[] = {163,7,6,3,0};
1061 unsigned int p1[] = {193,15,0};
1073 BN_GF2m_arr2poly(p0, b[0]);
1074 BN_GF2m_arr2poly(p1, b[1]);
1076 for (i=0; i<num0; i++)
1078 BN_bntest_rand(a, 1024, 0, 0);
1079 BN_bntest_rand(c, 1024, 0, 0);
1080 BN_bntest_rand(d, 1024, 0, 0);
1081 for (j=0; j < 2; j++)
1083 BN_GF2m_mod_mul(e, a, c, b[j], ctx);
1084 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1100 BN_GF2m_add(f, a, d);
1101 BN_GF2m_mod_mul(g, f, c, b[j], ctx);
1102 BN_GF2m_mod_mul(h, d, c, b[j], ctx);
1103 BN_GF2m_add(f, e, g);
1104 BN_GF2m_add(f, f, h);
1105 /* Test that (a+d)*c = a*c + d*c. */
1108 fprintf(stderr,"GF(2^m) modular multiplication test failed!\n");
1127 int test_gf2m_mod_sqr(BIO *bp,BN_CTX *ctx)
1129 BIGNUM *a,*b[2],*c,*d;
1131 unsigned int p0[] = {163,7,6,3,0};
1132 unsigned int p1[] = {193,15,0};
1140 BN_GF2m_arr2poly(p0, b[0]);
1141 BN_GF2m_arr2poly(p1, b[1]);
1143 for (i=0; i<num0; i++)
1145 BN_bntest_rand(a, 1024, 0, 0);
1146 for (j=0; j < 2; j++)
1148 BN_GF2m_mod_sqr(c, a, b[j], ctx);
1150 BN_GF2m_mod_mul(d, a, d, b[j], ctx);
1151 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1157 BIO_puts(bp," ^ 2 % ");
1159 BIO_puts(bp, " = ");
1161 BIO_puts(bp,"; a * a = ");
1167 BN_GF2m_add(d, c, d);
1168 /* Test that a*a = a^2. */
1171 fprintf(stderr,"GF(2^m) modular squaring test failed!\n");
1186 int test_gf2m_mod_inv(BIO *bp,BN_CTX *ctx)
1188 BIGNUM *a,*b[2],*c,*d;
1190 unsigned int p0[] = {163,7,6,3,0};
1191 unsigned int p1[] = {193,15,0};
1199 BN_GF2m_arr2poly(p0, b[0]);
1200 BN_GF2m_arr2poly(p1, b[1]);
1202 for (i=0; i<num0; i++)
1204 BN_bntest_rand(a, 512, 0, 0);
1205 for (j=0; j < 2; j++)
1207 BN_GF2m_mod_inv(c, a, b[j], ctx);
1208 BN_GF2m_mod_mul(d, a, c, b[j], ctx);
1209 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1215 BIO_puts(bp, " * ");
1217 BIO_puts(bp," - 1 % ");
1223 /* Test that ((1/a)*a) = 1. */
1226 fprintf(stderr,"GF(2^m) modular inversion test failed!\n");
1241 int test_gf2m_mod_div(BIO *bp,BN_CTX *ctx)
1243 BIGNUM *a,*b[2],*c,*d,*e,*f;
1245 unsigned int p0[] = {163,7,6,3,0};
1246 unsigned int p1[] = {193,15,0};
1256 BN_GF2m_arr2poly(p0, b[0]);
1257 BN_GF2m_arr2poly(p1, b[1]);
1259 for (i=0; i<num0; i++)
1261 BN_bntest_rand(a, 512, 0, 0);
1262 BN_bntest_rand(c, 512, 0, 0);
1263 for (j=0; j < 2; j++)
1265 BN_GF2m_mod_div(d, a, c, b[j], ctx);
1266 BN_GF2m_mod_mul(e, d, c, b[j], ctx);
1267 BN_GF2m_mod_div(f, a, e, b[j], ctx);
1268 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1274 BIO_puts(bp, " = ");
1278 BIO_puts(bp, " % ");
1284 /* Test that ((a/c)*c)/a = 1. */
1287 fprintf(stderr,"GF(2^m) modular division test failed!\n");
1304 int test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx)
1306 BIGNUM *a,*b[2],*c,*d,*e,*f;
1308 unsigned int p0[] = {163,7,6,3,0};
1309 unsigned int p1[] = {193,15,0};
1319 BN_GF2m_arr2poly(p0, b[0]);
1320 BN_GF2m_arr2poly(p1, b[1]);
1322 for (i=0; i<num0; i++)
1324 BN_bntest_rand(a, 512, 0, 0);
1325 BN_bntest_rand(c, 512, 0, 0);
1326 BN_bntest_rand(d, 512, 0, 0);
1327 for (j=0; j < 2; j++)
1329 BN_GF2m_mod_exp(e, a, c, b[j], ctx);
1330 BN_GF2m_mod_exp(f, a, d, b[j], ctx);
1331 BN_GF2m_mod_mul(e, e, f, b[j], ctx);
1333 BN_GF2m_mod_exp(f, a, f, b[j], ctx);
1334 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1340 BIO_puts(bp, " ^ (");
1344 BIO_puts(bp, ") = ");
1346 BIO_puts(bp, "; - ");
1348 BIO_puts(bp, " % ");
1354 BN_GF2m_add(f, e, f);
1355 /* Test that a^(c+d)=a^c*a^d. */
1358 fprintf(stderr,"GF(2^m) modular exponentiation test failed!\n");
1375 int test_gf2m_mod_sqrt(BIO *bp,BN_CTX *ctx)
1377 BIGNUM *a,*b[2],*c,*d,*e,*f;
1379 unsigned int p0[] = {163,7,6,3,0};
1380 unsigned int p1[] = {193,15,0};
1390 BN_GF2m_arr2poly(p0, b[0]);
1391 BN_GF2m_arr2poly(p1, b[1]);
1393 for (i=0; i<num0; i++)
1395 BN_bntest_rand(a, 512, 0, 0);
1396 for (j=0; j < 2; j++)
1398 BN_GF2m_mod(c, a, b[j]);
1399 BN_GF2m_mod_sqrt(d, a, b[j], ctx);
1400 BN_GF2m_mod_sqr(e, d, b[j], ctx);
1401 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1407 BIO_puts(bp, " ^ 2 - ");
1413 BN_GF2m_add(f, c, e);
1414 /* Test that d^2 = a, where d = sqrt(a). */
1417 fprintf(stderr,"GF(2^m) modular square root test failed!\n");
1434 int test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx)
1436 BIGNUM *a,*b[2],*c,*d,*e;
1437 int i, j, s = 0, t, ret = 0;
1438 unsigned int p0[] = {163,7,6,3,0};
1439 unsigned int p1[] = {193,15,0};
1448 BN_GF2m_arr2poly(p0, b[0]);
1449 BN_GF2m_arr2poly(p1, b[1]);
1451 for (i=0; i<num0; i++)
1453 BN_bntest_rand(a, 512, 0, 0);
1454 for (j=0; j < 2; j++)
1456 t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
1460 BN_GF2m_mod_sqr(d, c, b[j], ctx);
1461 BN_GF2m_add(d, c, d);
1462 BN_GF2m_mod(e, a, b[j]);
1463 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1469 BIO_puts(bp, " is root of z^2 + z = ");
1471 BIO_puts(bp, " % ");
1477 BN_GF2m_add(e, e, d);
1478 /* Test that solution of quadratic c satisfies c^2 + c = a. */
1481 fprintf(stderr,"GF(2^m) modular solve quadratic test failed!\n");
1488 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1493 BIO_puts(bp, "There are no roots of z^2 + z = ");
1495 BIO_puts(bp, " % ");
1506 fprintf(stderr,"All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n", num0);
1507 fprintf(stderr,"this is very unlikely and probably indicates an error.\n");
1521 static void genprime_cb(int p, int n, void *arg)
1535 int test_kron(BIO *bp, BN_CTX *ctx)
1539 int legendre, kronecker;
1546 if (a == NULL || b == NULL || r == NULL || t == NULL) goto err;
1548 /* We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol).
1549 * In this case we know that if b is prime, then BN_kronecker(a, b, ctx)
1550 * is congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol).
1551 * So we generate a random prime b and compare these values
1552 * for a number of random a's. (That is, we run the Solovay-Strassen
1553 * primality test to confirm that b is prime, except that we
1554 * don't want to test whether b is prime but whether BN_kronecker
1557 if (!BN_generate_prime(b, 512, 0, NULL, NULL, genprime_cb, NULL)) goto err;
1558 b->neg = rand_neg();
1561 for (i = 0; i < num0; i++)
1563 if (!BN_bntest_rand(a, 512, 0, 0)) goto err;
1564 a->neg = rand_neg();
1566 /* t := (|b|-1)/2 (note that b is odd) */
1567 if (!BN_copy(t, b)) goto err;
1569 if (!BN_sub_word(t, 1)) goto err;
1570 if (!BN_rshift1(t, t)) goto err;
1571 /* r := a^t mod b */
1574 if (!BN_mod_exp_recp(r, a, t, b, ctx)) goto err;
1577 if (BN_is_word(r, 1))
1579 else if (BN_is_zero(r))
1583 if (!BN_add_word(r, 1)) goto err;
1584 if (0 != BN_ucmp(r, b))
1586 fprintf(stderr, "Legendre symbol computation failed\n");
1592 kronecker = BN_kronecker(a, b, ctx);
1593 if (kronecker < -1) goto err;
1594 /* we actually need BN_kronecker(a, |b|) */
1595 if (a->neg && b->neg)
1596 kronecker = -kronecker;
1598 if (legendre != kronecker)
1600 fprintf(stderr, "legendre != kronecker; a = ");
1601 BN_print_fp(stderr, a);
1602 fprintf(stderr, ", b = ");
1603 BN_print_fp(stderr, b);
1604 fprintf(stderr, "\n");
1616 if (a != NULL) BN_free(a);
1617 if (b != NULL) BN_free(b);
1618 if (r != NULL) BN_free(r);
1619 if (t != NULL) BN_free(t);
1623 int test_sqrt(BIO *bp, BN_CTX *ctx)
1632 if (a == NULL || p == NULL || r == NULL) goto err;
1634 for (i = 0; i < 16; i++)
1638 unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 };
1640 if (!BN_set_word(p, primes[i])) goto err;
1644 if (!BN_set_word(a, 32)) goto err;
1645 if (!BN_set_word(r, 2*i + 1)) goto err;
1647 if (!BN_generate_prime(p, 256, 0, a, r, genprime_cb, NULL)) goto err;
1650 p->neg = rand_neg();
1652 for (j = 0; j < num2; j++)
1654 /* construct 'a' such that it is a square modulo p,
1655 * but in general not a proper square and not reduced modulo p */
1656 if (!BN_bntest_rand(r, 256, 0, 3)) goto err;
1657 if (!BN_nnmod(r, r, p, ctx)) goto err;
1658 if (!BN_mod_sqr(r, r, p, ctx)) goto err;
1659 if (!BN_bntest_rand(a, 256, 0, 3)) goto err;
1660 if (!BN_nnmod(a, a, p, ctx)) goto err;
1661 if (!BN_mod_sqr(a, a, p, ctx)) goto err;
1662 if (!BN_mul(a, a, r, ctx)) goto err;
1664 if (!BN_sub(a, a, p)) goto err;
1666 if (!BN_mod_sqrt(r, a, p, ctx)) goto err;
1667 if (!BN_mod_sqr(r, r, p, ctx)) goto err;
1669 if (!BN_nnmod(a, a, p, ctx)) goto err;
1671 if (BN_cmp(a, r) != 0)
1673 fprintf(stderr, "BN_mod_sqrt failed: a = ");
1674 BN_print_fp(stderr, a);
1675 fprintf(stderr, ", r = ");
1676 BN_print_fp(stderr, r);
1677 fprintf(stderr, ", p = ");
1678 BN_print_fp(stderr, p);
1679 fprintf(stderr, "\n");
1692 if (a != NULL) BN_free(a);
1693 if (p != NULL) BN_free(p);
1694 if (r != NULL) BN_free(r);
1698 int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_)
1713 BN_bntest_rand(a,200,0,0); /**/
1716 for (i=0; i<num0; i++)
1736 fprintf(stderr,"Left shift test failed!\n");
1737 fprintf(stderr,"a=");
1738 BN_print_fp(stderr,a);
1739 fprintf(stderr,"\nb=");
1740 BN_print_fp(stderr,b);
1741 fprintf(stderr,"\nc=");
1742 BN_print_fp(stderr,c);
1743 fprintf(stderr,"\nd=");
1744 BN_print_fp(stderr,d);
1745 fprintf(stderr,"\n");
1756 int test_lshift1(BIO *bp)
1765 BN_bntest_rand(a,200,0,0); /**/
1767 for (i=0; i<num0; i++)
1775 BIO_puts(bp," * 2");
1785 fprintf(stderr,"Left shift one test failed!\n");
1797 int test_rshift(BIO *bp,BN_CTX *ctx)
1799 BIGNUM *a,*b,*c,*d,*e;
1809 BN_bntest_rand(a,200,0,0); /**/
1811 for (i=0; i<num0; i++)
1827 BN_div(d,e,a,c,ctx);
1831 fprintf(stderr,"Right shift test failed!\n");
1843 int test_rshift1(BIO *bp)
1852 BN_bntest_rand(a,200,0,0); /**/
1854 for (i=0; i<num0; i++)
1862 BIO_puts(bp," / 2");
1870 if(!BN_is_zero(c) && !BN_abs_is_word(c, 1))
1872 fprintf(stderr,"Right shift one test failed!\n");
1885 static unsigned int neg=0;
1886 static int sign[8]={0,0,0,1,1,0,1,1};
1888 return(sign[(neg++)%8]);