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 * The binary polynomial arithmetic software is originally written by
68 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
72 /* Until the key-gen callbacks are modified to use newer prototypes, we allow
73 * deprecated functions for openssl-internal code */
74 #ifdef OPENSSL_NO_DEPRECATED
75 #undef OPENSSL_NO_DEPRECATED
84 #include <openssl/bio.h>
85 #include <openssl/bn.h>
86 #include <openssl/rand.h>
87 #include <openssl/x509.h>
88 #include <openssl/err.h>
90 const int num0 = 100; /* number of tests */
91 const int num1 = 50; /* additional tests for some functions */
92 const int num2 = 5; /* number of tests for slow functions */
94 int test_add(BIO *bp);
95 int test_sub(BIO *bp);
96 int test_lshift1(BIO *bp);
97 int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_);
98 int test_rshift1(BIO *bp);
99 int test_rshift(BIO *bp,BN_CTX *ctx);
100 int test_div(BIO *bp,BN_CTX *ctx);
101 int test_div_word(BIO *bp);
102 int test_div_recp(BIO *bp,BN_CTX *ctx);
103 int test_mul(BIO *bp);
104 int test_sqr(BIO *bp,BN_CTX *ctx);
105 int test_mont(BIO *bp,BN_CTX *ctx);
106 int test_mod(BIO *bp,BN_CTX *ctx);
107 int test_mod_mul(BIO *bp,BN_CTX *ctx);
108 int test_mod_exp(BIO *bp,BN_CTX *ctx);
109 int test_mod_exp_mont_consttime(BIO *bp,BN_CTX *ctx);
110 int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx);
111 int test_exp(BIO *bp,BN_CTX *ctx);
112 int test_gf2m_add(BIO *bp);
113 int test_gf2m_mod(BIO *bp);
114 int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx);
115 int test_gf2m_mod_sqr(BIO *bp,BN_CTX *ctx);
116 int test_gf2m_mod_inv(BIO *bp,BN_CTX *ctx);
117 int test_gf2m_mod_div(BIO *bp,BN_CTX *ctx);
118 int test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx);
119 int test_gf2m_mod_sqrt(BIO *bp,BN_CTX *ctx);
120 int test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx);
121 int test_kron(BIO *bp,BN_CTX *ctx);
122 int test_sqrt(BIO *bp,BN_CTX *ctx);
123 int test_small_prime(BIO *bp,BN_CTX *ctx);
125 static int results=0;
127 static unsigned char lst[]="\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9"
128 "\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0";
130 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
132 static void message(BIO *out, char *m)
134 fprintf(stderr, "test %s\n", m);
135 BIO_puts(out, "print \"test ");
137 BIO_puts(out, "\\n\"\n");
140 int main(int argc, char *argv[])
148 RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
154 if (strcmp(*argv,"-results") == 0)
156 else if (strcmp(*argv,"-out") == 0)
158 if (--argc < 1) break;
167 if (ctx == NULL) EXIT(1);
169 out=BIO_new(BIO_s_file());
170 if (out == NULL) EXIT(1);
173 BIO_set_fp(out,stdout,BIO_NOCLOSE);
177 if (!BIO_write_filename(out,outfile))
185 BIO_puts(out,"obase=16\nibase=16\n");
187 message(out,"BN_add");
188 if (!test_add(out)) goto err;
189 (void)BIO_flush(out);
191 message(out,"BN_sub");
192 if (!test_sub(out)) goto err;
193 (void)BIO_flush(out);
195 message(out,"BN_lshift1");
196 if (!test_lshift1(out)) goto err;
197 (void)BIO_flush(out);
199 message(out,"BN_lshift (fixed)");
200 if (!test_lshift(out,ctx,BN_bin2bn(lst,sizeof(lst)-1,NULL)))
202 (void)BIO_flush(out);
204 message(out,"BN_lshift");
205 if (!test_lshift(out,ctx,NULL)) goto err;
206 (void)BIO_flush(out);
208 message(out,"BN_rshift1");
209 if (!test_rshift1(out)) goto err;
210 (void)BIO_flush(out);
212 message(out,"BN_rshift");
213 if (!test_rshift(out,ctx)) goto err;
214 (void)BIO_flush(out);
216 message(out,"BN_sqr");
217 if (!test_sqr(out,ctx)) goto err;
218 (void)BIO_flush(out);
220 message(out,"BN_mul");
221 if (!test_mul(out)) goto err;
222 (void)BIO_flush(out);
224 message(out,"BN_div");
225 if (!test_div(out,ctx)) goto err;
226 (void)BIO_flush(out);
228 message(out,"BN_div_word");
229 if (!test_div_word(out)) goto err;
230 (void)BIO_flush(out);
232 message(out,"BN_div_recp");
233 if (!test_div_recp(out,ctx)) goto err;
234 (void)BIO_flush(out);
236 message(out,"BN_mod");
237 if (!test_mod(out,ctx)) goto err;
238 (void)BIO_flush(out);
240 message(out,"BN_mod_mul");
241 if (!test_mod_mul(out,ctx)) goto err;
242 (void)BIO_flush(out);
244 message(out,"BN_mont");
245 if (!test_mont(out,ctx)) goto err;
246 (void)BIO_flush(out);
248 message(out,"BN_mod_exp");
249 if (!test_mod_exp(out,ctx)) goto err;
250 (void)BIO_flush(out);
252 message(out,"BN_mod_exp_mont_consttime");
253 if (!test_mod_exp_mont_consttime(out,ctx)) goto err;
254 if (!test_mod_exp_mont5(out,ctx)) goto err;
255 (void)BIO_flush(out);
257 message(out,"BN_exp");
258 if (!test_exp(out,ctx)) goto err;
259 (void)BIO_flush(out);
261 message(out,"BN_kronecker");
262 if (!test_kron(out,ctx)) goto err;
263 (void)BIO_flush(out);
265 message(out,"BN_mod_sqrt");
266 if (!test_sqrt(out,ctx)) goto err;
267 (void)BIO_flush(out);
269 message(out,"Small prime generation");
270 if (!test_small_prime(out,ctx)) goto err;
271 (void)BIO_flush(out);
273 #ifndef OPENSSL_NO_EC2M
274 message(out,"BN_GF2m_add");
275 if (!test_gf2m_add(out)) goto err;
276 (void)BIO_flush(out);
278 message(out,"BN_GF2m_mod");
279 if (!test_gf2m_mod(out)) goto err;
280 (void)BIO_flush(out);
282 message(out,"BN_GF2m_mod_mul");
283 if (!test_gf2m_mod_mul(out,ctx)) goto err;
284 (void)BIO_flush(out);
286 message(out,"BN_GF2m_mod_sqr");
287 if (!test_gf2m_mod_sqr(out,ctx)) goto err;
288 (void)BIO_flush(out);
290 message(out,"BN_GF2m_mod_inv");
291 if (!test_gf2m_mod_inv(out,ctx)) goto err;
292 (void)BIO_flush(out);
294 message(out,"BN_GF2m_mod_div");
295 if (!test_gf2m_mod_div(out,ctx)) goto err;
296 (void)BIO_flush(out);
298 message(out,"BN_GF2m_mod_exp");
299 if (!test_gf2m_mod_exp(out,ctx)) goto err;
300 (void)BIO_flush(out);
302 message(out,"BN_GF2m_mod_sqrt");
303 if (!test_gf2m_mod_sqrt(out,ctx)) goto err;
304 (void)BIO_flush(out);
306 message(out,"BN_GF2m_mod_solve_quad");
307 if (!test_gf2m_mod_solve_quad(out,ctx)) goto err;
308 (void)BIO_flush(out);
316 BIO_puts(out,"1\n"); /* make sure the Perl script fed by bc notices
317 * the failure, see test_bn in test/Makefile.ssl*/
318 (void)BIO_flush(out);
319 ERR_load_crypto_strings();
320 ERR_print_errors_fp(stderr);
325 int test_add(BIO *bp)
334 BN_bntest_rand(&a,512,0,0);
335 for (i=0; i<num0; i++)
337 BN_bntest_rand(&b,450+i,0,0);
359 fprintf(stderr,"Add test failed!\n");
369 int test_sub(BIO *bp)
378 for (i=0; i<num0+num1; i++)
382 BN_bntest_rand(&a,512,0,0);
384 if (BN_set_bit(&a,i)==0) return(0);
389 BN_bntest_rand(&b,400+i-num1,0,0);
410 fprintf(stderr,"Subtract test failed!\n");
420 int test_div(BIO *bp, BN_CTX *ctx)
431 for (i=0; i<num0+num1; i++)
435 BN_bntest_rand(&a,400,0,0);
441 BN_bntest_rand(&b,50+3*(i-num1),0,0);
444 BN_div(&d,&c,&a,&b,ctx);
467 BN_mul(&e,&d,&b,ctx);
472 fprintf(stderr,"Division test failed!\n");
484 static void print_word(BIO *bp,BN_ULONG w)
486 #ifdef SIXTY_FOUR_BIT
487 if (sizeof(w) > sizeof(unsigned long))
489 unsigned long h=(unsigned long)(w>>32),
490 l=(unsigned long)(w);
492 if (h) BIO_printf(bp,"%lX%08lX",h,l);
493 else BIO_printf(bp,"%lX",l);
497 BIO_printf(bp,BN_HEX_FMT1,w);
500 int test_div_word(BIO *bp)
509 for (i=0; i<num0; i++)
512 BN_bntest_rand(&a,512,-1,0);
513 BN_bntest_rand(&b,BN_BITS2,-1,0);
518 r = BN_div_word(&b, s);
547 fprintf(stderr,"Division (word) test failed!\n");
556 int test_div_recp(BIO *bp, BN_CTX *ctx)
562 BN_RECP_CTX_init(&recp);
569 for (i=0; i<num0+num1; i++)
573 BN_bntest_rand(&a,400,0,0);
579 BN_bntest_rand(&b,50+3*(i-num1),0,0);
582 BN_RECP_CTX_set(&recp,&b,ctx);
583 BN_div_recp(&d,&c,&a,&recp,ctx);
606 BN_mul(&e,&d,&b,ctx);
611 fprintf(stderr,"Reciprocal division test failed!\n");
612 fprintf(stderr,"a=");
613 BN_print_fp(stderr,&a);
614 fprintf(stderr,"\nb=");
615 BN_print_fp(stderr,&b);
616 fprintf(stderr,"\n");
625 BN_RECP_CTX_free(&recp);
629 int test_mul(BIO *bp)
636 if (ctx == NULL) EXIT(1);
644 for (i=0; i<num0+num1; i++)
648 BN_bntest_rand(&a,100,0,0);
649 BN_bntest_rand(&b,100,0,0);
652 BN_bntest_rand(&b,i-num1,0,0);
655 BN_mul(&c,&a,&b,ctx);
668 BN_div(&d,&e,&c,&a,ctx);
670 if(!BN_is_zero(&d) || !BN_is_zero(&e))
672 fprintf(stderr,"Multiplication test failed!\n");
685 int test_sqr(BIO *bp, BN_CTX *ctx)
695 for (i=0; i<num0; i++)
697 BN_bntest_rand(&a,40+i*10,0,0);
712 BN_div(&d,&e,&c,&a,ctx);
714 if(!BN_is_zero(&d) || !BN_is_zero(&e))
716 fprintf(stderr,"Square test failed!\n");
727 int test_mont(BIO *bp, BN_CTX *ctx)
742 mont=BN_MONT_CTX_new();
746 BN_bntest_rand(&a,100,0,0); /**/
747 BN_bntest_rand(&b,100,0,0); /**/
748 for (i=0; i<num2; i++)
750 int bits = (200*(i+1))/num2;
754 BN_bntest_rand(&n,bits,0,1);
755 BN_MONT_CTX_set(mont,&n,ctx);
757 BN_nnmod(&a,&a,&n,ctx);
758 BN_nnmod(&b,&b,&n,ctx);
760 BN_to_montgomery(&A,&a,mont,ctx);
761 BN_to_montgomery(&B,&b,mont,ctx);
763 BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
764 BN_from_montgomery(&A,&c,mont,ctx);/**/
770 fprintf(stderr,"%d * %d %% %d\n",
773 BN_num_bits(mont->N));
779 BN_print(bp,&(mont->N));
785 BN_mod_mul(&d,&a,&b,&n,ctx);
789 fprintf(stderr,"Montgomery multiplication test failed!\n");
793 BN_MONT_CTX_free(mont);
804 int test_mod(BIO *bp, BN_CTX *ctx)
806 BIGNUM *a,*b,*c,*d,*e;
815 BN_bntest_rand(a,1024,0,0); /**/
816 for (i=0; i<num0; i++)
818 BN_bntest_rand(b,450+i*10,0,0); /**/
821 BN_mod(c,a,b,ctx);/**/
838 fprintf(stderr,"Modulo test failed!\n");
850 int test_mod_mul(BIO *bp, BN_CTX *ctx)
852 BIGNUM *a,*b,*c,*d,*e;
861 for (j=0; j<3; j++) {
862 BN_bntest_rand(c,1024,0,0); /**/
863 for (i=0; i<num0; i++)
865 BN_bntest_rand(a,475+i*10,0,0); /**/
866 BN_bntest_rand(b,425+i*11,0,0); /**/
869 if (!BN_mod_mul(e,a,b,c,ctx))
873 while ((l=ERR_get_error()))
874 fprintf(stderr,"ERROR:%s\n",
875 ERR_error_string(l,NULL));
887 if ((a->neg ^ b->neg) && !BN_is_zero(e))
889 /* If (a*b) % c is negative, c must be added
890 * in order to obtain the normalized remainder
891 * (new with OpenSSL 0.9.7, previous versions of
892 * BN_mod_mul could generate negative results)
907 fprintf(stderr,"Modulo multiply test failed!\n");
908 ERR_print_errors_fp(stderr);
921 int test_mod_exp(BIO *bp, BN_CTX *ctx)
923 BIGNUM *a,*b,*c,*d,*e;
932 BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */
933 for (i=0; i<num2; i++)
935 BN_bntest_rand(a,20+i*5,0,0); /**/
936 BN_bntest_rand(b,2+i,0,0); /**/
938 if (!BN_mod_exp(d,a,b,c,ctx))
960 fprintf(stderr,"Modulo exponentiation test failed!\n");
972 int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx)
974 BIGNUM *a,*b,*c,*d,*e;
983 BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */
984 for (i=0; i<num2; i++)
986 BN_bntest_rand(a,20+i*5,0,0); /**/
987 BN_bntest_rand(b,2+i,0,0); /**/
989 if (!BN_mod_exp_mont_consttime(d,a,b,c,ctx,NULL))
1008 BN_div(a,b,e,c,ctx);
1011 fprintf(stderr,"Modulo exponentiation test failed!\n");
1023 /* Test constant-time modular exponentiation with 1024-bit inputs,
1024 * which on x86_64 cause a different code branch to be taken.
1026 int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx)
1028 BIGNUM *a,*p,*m,*d,*e;
1038 mont = BN_MONT_CTX_new();
1040 BN_bntest_rand(m,1024,0,1); /* must be odd for montgomery */
1042 BN_bntest_rand(a,1024,0,0);
1044 if(!BN_mod_exp_mont_consttime(d,a,p,m,ctx,NULL))
1048 fprintf(stderr, "Modular exponentiation test failed!\n");
1052 BN_bntest_rand(p,1024,0,0);
1054 if(!BN_mod_exp_mont_consttime(d,a,p,m,ctx,NULL))
1058 fprintf(stderr, "Modular exponentiation test failed!\n");
1061 /* Craft an input whose Montgomery representation is 1,
1062 * i.e., shorter than the modulus m, in order to test
1063 * the const time precomputation scattering/gathering.
1066 BN_MONT_CTX_set(mont,m,ctx);
1067 if(!BN_from_montgomery(e,a,mont,ctx))
1069 if(!BN_mod_exp_mont_consttime(d,e,p,m,ctx,NULL))
1071 if(!BN_mod_exp_simple(a,e,p,m,ctx))
1073 if(BN_cmp(a,d) != 0)
1075 fprintf(stderr,"Modular exponentiation test failed!\n");
1078 /* Finally, some regular test vectors. */
1079 BN_bntest_rand(e,1024,0,0);
1080 if(!BN_mod_exp_mont_consttime(d,e,p,m,ctx,NULL))
1082 if(!BN_mod_exp_simple(a,e,p,m,ctx))
1084 if(BN_cmp(a,d) != 0)
1086 fprintf(stderr,"Modular exponentiation test failed!\n");
1097 int test_exp(BIO *bp, BN_CTX *ctx)
1099 BIGNUM *a,*b,*d,*e,*one;
1109 for (i=0; i<num2; i++)
1111 BN_bntest_rand(a,20+i*5,0,0); /**/
1112 BN_bntest_rand(b,2+i,0,0); /**/
1114 if (BN_exp(d,a,b,ctx) <= 0)
1130 for( ; !BN_is_zero(b) ; BN_sub(b,b,one))
1135 fprintf(stderr,"Exponentiation test failed!\n");
1146 #ifndef OPENSSL_NO_EC2M
1147 int test_gf2m_add(BIO *bp)
1156 for (i=0; i<num0; i++)
1158 BN_rand(&a,512,0,0);
1159 BN_copy(&b, BN_value_one());
1162 BN_GF2m_add(&c,&a,&b);
1163 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1177 /* Test that two added values have the correct parity. */
1178 if((BN_is_odd(&a) && BN_is_odd(&c)) || (!BN_is_odd(&a) && !BN_is_odd(&c)))
1180 fprintf(stderr,"GF(2^m) addition test (a) failed!\n");
1183 BN_GF2m_add(&c,&c,&c);
1184 /* Test that c + c = 0. */
1187 fprintf(stderr,"GF(2^m) addition test (b) failed!\n");
1199 int test_gf2m_mod(BIO *bp)
1201 BIGNUM *a,*b[2],*c,*d,*e;
1203 int p0[] = {163,7,6,3,0,-1};
1204 int p1[] = {193,15,0,-1};
1213 BN_GF2m_arr2poly(p0, b[0]);
1214 BN_GF2m_arr2poly(p1, b[1]);
1216 for (i=0; i<num0; i++)
1218 BN_bntest_rand(a, 1024, 0, 0);
1219 for (j=0; j < 2; j++)
1221 BN_GF2m_mod(c, a, b[j]);
1222 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1236 BN_GF2m_add(d, a, c);
1237 BN_GF2m_mod(e, d, b[j]);
1238 /* Test that a + (a mod p) mod p == 0. */
1241 fprintf(stderr,"GF(2^m) modulo test failed!\n");
1257 int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx)
1259 BIGNUM *a,*b[2],*c,*d,*e,*f,*g,*h;
1261 int p0[] = {163,7,6,3,0,-1};
1262 int p1[] = {193,15,0,-1};
1274 BN_GF2m_arr2poly(p0, b[0]);
1275 BN_GF2m_arr2poly(p1, b[1]);
1277 for (i=0; i<num0; i++)
1279 BN_bntest_rand(a, 1024, 0, 0);
1280 BN_bntest_rand(c, 1024, 0, 0);
1281 BN_bntest_rand(d, 1024, 0, 0);
1282 for (j=0; j < 2; j++)
1284 BN_GF2m_mod_mul(e, a, c, b[j], ctx);
1285 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1301 BN_GF2m_add(f, a, d);
1302 BN_GF2m_mod_mul(g, f, c, b[j], ctx);
1303 BN_GF2m_mod_mul(h, d, c, b[j], ctx);
1304 BN_GF2m_add(f, e, g);
1305 BN_GF2m_add(f, f, h);
1306 /* Test that (a+d)*c = a*c + d*c. */
1309 fprintf(stderr,"GF(2^m) modular multiplication test failed!\n");
1328 int test_gf2m_mod_sqr(BIO *bp,BN_CTX *ctx)
1330 BIGNUM *a,*b[2],*c,*d;
1332 int p0[] = {163,7,6,3,0,-1};
1333 int p1[] = {193,15,0,-1};
1341 BN_GF2m_arr2poly(p0, b[0]);
1342 BN_GF2m_arr2poly(p1, b[1]);
1344 for (i=0; i<num0; i++)
1346 BN_bntest_rand(a, 1024, 0, 0);
1347 for (j=0; j < 2; j++)
1349 BN_GF2m_mod_sqr(c, a, b[j], ctx);
1351 BN_GF2m_mod_mul(d, a, d, b[j], ctx);
1352 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1358 BIO_puts(bp," ^ 2 % ");
1360 BIO_puts(bp, " = ");
1362 BIO_puts(bp,"; a * a = ");
1368 BN_GF2m_add(d, c, d);
1369 /* Test that a*a = a^2. */
1372 fprintf(stderr,"GF(2^m) modular squaring test failed!\n");
1387 int test_gf2m_mod_inv(BIO *bp,BN_CTX *ctx)
1389 BIGNUM *a,*b[2],*c,*d;
1391 int p0[] = {163,7,6,3,0,-1};
1392 int p1[] = {193,15,0,-1};
1400 BN_GF2m_arr2poly(p0, b[0]);
1401 BN_GF2m_arr2poly(p1, b[1]);
1403 for (i=0; i<num0; i++)
1405 BN_bntest_rand(a, 512, 0, 0);
1406 for (j=0; j < 2; j++)
1408 BN_GF2m_mod_inv(c, a, b[j], ctx);
1409 BN_GF2m_mod_mul(d, a, c, b[j], ctx);
1410 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1416 BIO_puts(bp, " * ");
1418 BIO_puts(bp," - 1 % ");
1424 /* Test that ((1/a)*a) = 1. */
1427 fprintf(stderr,"GF(2^m) modular inversion test failed!\n");
1442 int test_gf2m_mod_div(BIO *bp,BN_CTX *ctx)
1444 BIGNUM *a,*b[2],*c,*d,*e,*f;
1446 int p0[] = {163,7,6,3,0,-1};
1447 int p1[] = {193,15,0,-1};
1457 BN_GF2m_arr2poly(p0, b[0]);
1458 BN_GF2m_arr2poly(p1, b[1]);
1460 for (i=0; i<num0; i++)
1462 BN_bntest_rand(a, 512, 0, 0);
1463 BN_bntest_rand(c, 512, 0, 0);
1464 for (j=0; j < 2; j++)
1466 BN_GF2m_mod_div(d, a, c, b[j], ctx);
1467 BN_GF2m_mod_mul(e, d, c, b[j], ctx);
1468 BN_GF2m_mod_div(f, a, e, b[j], ctx);
1469 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1475 BIO_puts(bp, " = ");
1479 BIO_puts(bp, " % ");
1485 /* Test that ((a/c)*c)/a = 1. */
1488 fprintf(stderr,"GF(2^m) modular division test failed!\n");
1505 int test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx)
1507 BIGNUM *a,*b[2],*c,*d,*e,*f;
1509 int p0[] = {163,7,6,3,0,-1};
1510 int p1[] = {193,15,0,-1};
1520 BN_GF2m_arr2poly(p0, b[0]);
1521 BN_GF2m_arr2poly(p1, b[1]);
1523 for (i=0; i<num0; i++)
1525 BN_bntest_rand(a, 512, 0, 0);
1526 BN_bntest_rand(c, 512, 0, 0);
1527 BN_bntest_rand(d, 512, 0, 0);
1528 for (j=0; j < 2; j++)
1530 BN_GF2m_mod_exp(e, a, c, b[j], ctx);
1531 BN_GF2m_mod_exp(f, a, d, b[j], ctx);
1532 BN_GF2m_mod_mul(e, e, f, b[j], ctx);
1534 BN_GF2m_mod_exp(f, a, f, b[j], ctx);
1535 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1541 BIO_puts(bp, " ^ (");
1545 BIO_puts(bp, ") = ");
1547 BIO_puts(bp, "; - ");
1549 BIO_puts(bp, " % ");
1555 BN_GF2m_add(f, e, f);
1556 /* Test that a^(c+d)=a^c*a^d. */
1559 fprintf(stderr,"GF(2^m) modular exponentiation test failed!\n");
1576 int test_gf2m_mod_sqrt(BIO *bp,BN_CTX *ctx)
1578 BIGNUM *a,*b[2],*c,*d,*e,*f;
1580 int p0[] = {163,7,6,3,0,-1};
1581 int p1[] = {193,15,0,-1};
1591 BN_GF2m_arr2poly(p0, b[0]);
1592 BN_GF2m_arr2poly(p1, b[1]);
1594 for (i=0; i<num0; i++)
1596 BN_bntest_rand(a, 512, 0, 0);
1597 for (j=0; j < 2; j++)
1599 BN_GF2m_mod(c, a, b[j]);
1600 BN_GF2m_mod_sqrt(d, a, b[j], ctx);
1601 BN_GF2m_mod_sqr(e, d, b[j], ctx);
1602 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1608 BIO_puts(bp, " ^ 2 - ");
1614 BN_GF2m_add(f, c, e);
1615 /* Test that d^2 = a, where d = sqrt(a). */
1618 fprintf(stderr,"GF(2^m) modular square root test failed!\n");
1635 int test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx)
1637 BIGNUM *a,*b[2],*c,*d,*e;
1638 int i, j, s = 0, t, ret = 0;
1639 int p0[] = {163,7,6,3,0,-1};
1640 int p1[] = {193,15,0,-1};
1649 BN_GF2m_arr2poly(p0, b[0]);
1650 BN_GF2m_arr2poly(p1, b[1]);
1652 for (i=0; i<num0; i++)
1654 BN_bntest_rand(a, 512, 0, 0);
1655 for (j=0; j < 2; j++)
1657 t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
1661 BN_GF2m_mod_sqr(d, c, b[j], ctx);
1662 BN_GF2m_add(d, c, d);
1663 BN_GF2m_mod(e, a, b[j]);
1664 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1670 BIO_puts(bp, " is root of z^2 + z = ");
1672 BIO_puts(bp, " % ");
1678 BN_GF2m_add(e, e, d);
1679 /* Test that solution of quadratic c satisfies c^2 + c = a. */
1682 fprintf(stderr,"GF(2^m) modular solve quadratic test failed!\n");
1689 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1694 BIO_puts(bp, "There are no roots of z^2 + z = ");
1696 BIO_puts(bp, " % ");
1707 fprintf(stderr,"All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n", num0);
1708 fprintf(stderr,"this is very unlikely and probably indicates an error.\n");
1722 static int genprime_cb(int p, int n, BN_GENCB *arg)
1735 int test_kron(BIO *bp, BN_CTX *ctx)
1740 int legendre, kronecker;
1747 if (a == NULL || b == NULL || r == NULL || t == NULL) goto err;
1749 BN_GENCB_set(&cb, genprime_cb, NULL);
1751 /* We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol).
1752 * In this case we know that if b is prime, then BN_kronecker(a, b, ctx)
1753 * is congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol).
1754 * So we generate a random prime b and compare these values
1755 * for a number of random a's. (That is, we run the Solovay-Strassen
1756 * primality test to confirm that b is prime, except that we
1757 * don't want to test whether b is prime but whether BN_kronecker
1760 if (!BN_generate_prime_ex(b, 512, 0, NULL, NULL, &cb)) goto err;
1761 b->neg = rand_neg();
1764 for (i = 0; i < num0; i++)
1766 if (!BN_bntest_rand(a, 512, 0, 0)) goto err;
1767 a->neg = rand_neg();
1769 /* t := (|b|-1)/2 (note that b is odd) */
1770 if (!BN_copy(t, b)) goto err;
1772 if (!BN_sub_word(t, 1)) goto err;
1773 if (!BN_rshift1(t, t)) goto err;
1774 /* r := a^t mod b */
1777 if (!BN_mod_exp_recp(r, a, t, b, ctx)) goto err;
1780 if (BN_is_word(r, 1))
1782 else if (BN_is_zero(r))
1786 if (!BN_add_word(r, 1)) goto err;
1787 if (0 != BN_ucmp(r, b))
1789 fprintf(stderr, "Legendre symbol computation failed\n");
1795 kronecker = BN_kronecker(a, b, ctx);
1796 if (kronecker < -1) goto err;
1797 /* we actually need BN_kronecker(a, |b|) */
1798 if (a->neg && b->neg)
1799 kronecker = -kronecker;
1801 if (legendre != kronecker)
1803 fprintf(stderr, "legendre != kronecker; a = ");
1804 BN_print_fp(stderr, a);
1805 fprintf(stderr, ", b = ");
1806 BN_print_fp(stderr, b);
1807 fprintf(stderr, "\n");
1819 if (a != NULL) BN_free(a);
1820 if (b != NULL) BN_free(b);
1821 if (r != NULL) BN_free(r);
1822 if (t != NULL) BN_free(t);
1826 int test_sqrt(BIO *bp, BN_CTX *ctx)
1836 if (a == NULL || p == NULL || r == NULL) goto err;
1838 BN_GENCB_set(&cb, genprime_cb, NULL);
1840 for (i = 0; i < 16; i++)
1844 unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 };
1846 if (!BN_set_word(p, primes[i])) goto err;
1850 if (!BN_set_word(a, 32)) goto err;
1851 if (!BN_set_word(r, 2*i + 1)) goto err;
1853 if (!BN_generate_prime_ex(p, 256, 0, a, r, &cb)) goto err;
1856 p->neg = rand_neg();
1858 for (j = 0; j < num2; j++)
1860 /* construct 'a' such that it is a square modulo p,
1861 * but in general not a proper square and not reduced modulo p */
1862 if (!BN_bntest_rand(r, 256, 0, 3)) goto err;
1863 if (!BN_nnmod(r, r, p, ctx)) goto err;
1864 if (!BN_mod_sqr(r, r, p, ctx)) goto err;
1865 if (!BN_bntest_rand(a, 256, 0, 3)) goto err;
1866 if (!BN_nnmod(a, a, p, ctx)) goto err;
1867 if (!BN_mod_sqr(a, a, p, ctx)) goto err;
1868 if (!BN_mul(a, a, r, ctx)) goto err;
1870 if (!BN_sub(a, a, p)) goto err;
1872 if (!BN_mod_sqrt(r, a, p, ctx)) goto err;
1873 if (!BN_mod_sqr(r, r, p, ctx)) goto err;
1875 if (!BN_nnmod(a, a, p, ctx)) goto err;
1877 if (BN_cmp(a, r) != 0)
1879 fprintf(stderr, "BN_mod_sqrt failed: a = ");
1880 BN_print_fp(stderr, a);
1881 fprintf(stderr, ", r = ");
1882 BN_print_fp(stderr, r);
1883 fprintf(stderr, ", p = ");
1884 BN_print_fp(stderr, p);
1885 fprintf(stderr, "\n");
1898 if (a != NULL) BN_free(a);
1899 if (p != NULL) BN_free(p);
1900 if (r != NULL) BN_free(r);
1904 int test_small_prime(BIO *bp,BN_CTX *ctx)
1906 static const int bits = 10;
1911 if (!BN_generate_prime_ex(&r, bits, 0, NULL, NULL, NULL))
1913 if (BN_num_bits(&r) != bits)
1915 BIO_printf(bp, "Expected %d bit prime, got %d bit number\n", bits, BN_num_bits(&r));
1926 int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_)
1941 BN_bntest_rand(a,200,0,0); /**/
1944 for (i=0; i<num0; i++)
1964 fprintf(stderr,"Left shift test failed!\n");
1965 fprintf(stderr,"a=");
1966 BN_print_fp(stderr,a);
1967 fprintf(stderr,"\nb=");
1968 BN_print_fp(stderr,b);
1969 fprintf(stderr,"\nc=");
1970 BN_print_fp(stderr,c);
1971 fprintf(stderr,"\nd=");
1972 BN_print_fp(stderr,d);
1973 fprintf(stderr,"\n");
1984 int test_lshift1(BIO *bp)
1993 BN_bntest_rand(a,200,0,0); /**/
1995 for (i=0; i<num0; i++)
2003 BIO_puts(bp," * 2");
2013 fprintf(stderr,"Left shift one test failed!\n");
2025 int test_rshift(BIO *bp,BN_CTX *ctx)
2027 BIGNUM *a,*b,*c,*d,*e;
2037 BN_bntest_rand(a,200,0,0); /**/
2039 for (i=0; i<num0; i++)
2055 BN_div(d,e,a,c,ctx);
2059 fprintf(stderr,"Right shift test failed!\n");
2071 int test_rshift1(BIO *bp)
2080 BN_bntest_rand(a,200,0,0); /**/
2082 for (i=0; i<num0; i++)
2090 BIO_puts(bp," / 2");
2098 if(!BN_is_zero(c) && !BN_abs_is_word(c, 1))
2100 fprintf(stderr,"Right shift one test failed!\n");
2113 static unsigned int neg=0;
2114 static int sign[8]={0,0,0,1,1,0,1,1};
2116 return(sign[(neg++)%8]);