74 #ifdef OPENSSL_NO_DEPRECATED
75 #undef OPENSSL_NO_DEPRECATED
123 static int results=0;
125 static unsigned char lst[]=
"\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9"
126 "\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0";
128 static const char rnd_seed[] =
"string to make the random number generator think it has entropy";
130 static void message(
BIO *out,
char *m)
132 fprintf(stderr,
"test %s\n", m);
138 int main(
int argc,
char *argv[])
152 if (strcmp(*argv,
"-results") == 0)
154 else if (strcmp(*argv,
"-out") == 0)
156 if (--argc < 1)
break;
165 if (ctx == NULL)
EXIT(1);
168 if (out == NULL)
EXIT(1);
183 BIO_puts(out,
"obase=16\nibase=16\n");
185 message(out,
"BN_add");
189 message(out,
"BN_sub");
193 message(out,
"BN_lshift1");
197 message(out,
"BN_lshift (fixed)");
202 message(out,
"BN_lshift");
206 message(out,
"BN_rshift1");
210 message(out,
"BN_rshift");
214 message(out,
"BN_sqr");
218 message(out,
"BN_mul");
222 message(out,
"BN_div");
226 message(out,
"BN_div_word");
230 message(out,
"BN_div_recp");
234 message(out,
"BN_mod");
238 message(out,
"BN_mod_mul");
242 message(out,
"BN_mont");
246 message(out,
"BN_mod_exp");
250 message(out,
"BN_mod_exp_mont_consttime");
254 message(out,
"BN_exp");
258 message(out,
"BN_kronecker");
262 message(out,
"BN_mod_sqrt");
265 #ifndef OPENSSL_NO_EC2M
266 message(out,
"BN_GF2m_add");
270 message(out,
"BN_GF2m_mod");
274 message(out,
"BN_GF2m_mod_mul");
278 message(out,
"BN_GF2m_mod_sqr");
282 message(out,
"BN_GF2m_mod_inv");
286 message(out,
"BN_GF2m_mod_div");
290 message(out,
"BN_GF2m_mod_exp");
294 message(out,
"BN_GF2m_mod_sqrt");
298 message(out,
"BN_GF2m_mod_solve_quad");
327 for (i=0; i<
num0; i++)
351 fprintf(stderr,
"Add test failed!\n");
402 fprintf(stderr,
"Subtract test failed!\n");
464 fprintf(stderr,
"Division test failed!\n");
476 static void print_word(
BIO *
bp,BN_ULONG w)
478 #ifdef SIXTY_FOUR_BIT
479 if (
sizeof(w) >
sizeof(
unsigned long))
481 unsigned long h=(
unsigned long)(w>>32),
482 l=(
unsigned long)(w);
501 for (i=0; i<
num0; i++)
539 fprintf(stderr,
"Division (word) test failed!\n");
603 fprintf(stderr,
"Reciprocal division test failed!\n");
604 fprintf(stderr,
"a=");
606 fprintf(stderr,
"\nb=");
608 fprintf(stderr,
"\n");
628 if (ctx == NULL)
EXIT(1);
664 fprintf(stderr,
"Multiplication test failed!\n");
687 for (i=0; i<
num0; i++)
708 fprintf(stderr,
"Square test failed!\n");
740 for (i=0; i<
num2; i++)
762 fprintf(stderr,
"%d * %d %% %d\n",
781 fprintf(stderr,
"Montgomery multiplication test failed!\n");
808 for (i=0; i<
num0; i++)
830 fprintf(stderr,
"Modulo test failed!\n");
853 for (j=0; j<3; j++) {
855 for (i=0; i<
num0; i++)
866 fprintf(stderr,
"ERROR:%s\n",
899 fprintf(stderr,
"Modulo multiply test failed!\n");
925 for (i=0; i<
num2; i++)
952 fprintf(stderr,
"Modulo exponentiation test failed!\n");
976 for (i=0; i<
num2; i++)
1003 fprintf(stderr,
"Modulo exponentiation test failed!\n");
1027 for (i=0; i<
num2; i++)
1032 if (
BN_exp(d,a,b,ctx) <= 0)
1053 fprintf(stderr,
"Exponentiation test failed!\n");
1064 #ifndef OPENSSL_NO_EC2M
1074 for (i=0; i<
num0; i++)
1098 fprintf(stderr,
"GF(2^m) addition test (a) failed!\n");
1105 fprintf(stderr,
"GF(2^m) addition test (b) failed!\n");
1121 int p0[] = {163,7,6,3,0,-1};
1122 int p1[] = {193,15,0,-1};
1134 for (i=0; i<
num0; i++)
1137 for (j=0; j < 2; j++)
1159 fprintf(stderr,
"GF(2^m) modulo test failed!\n");
1179 int p0[] = {163,7,6,3,0,-1};
1180 int p1[] = {193,15,0,-1};
1195 for (i=0; i<
num0; i++)
1200 for (j=0; j < 2; j++)
1227 fprintf(stderr,
"GF(2^m) modular multiplication test failed!\n");
1250 int p0[] = {163,7,6,3,0,-1};
1251 int p1[] = {193,15,0,-1};
1262 for (i=0; i<
num0; i++)
1265 for (j=0; j < 2; j++)
1290 fprintf(stderr,
"GF(2^m) modular squaring test failed!\n");
1309 int p0[] = {163,7,6,3,0,-1};
1310 int p1[] = {193,15,0,-1};
1321 for (i=0; i<
num0; i++)
1324 for (j=0; j < 2; j++)
1345 fprintf(stderr,
"GF(2^m) modular inversion test failed!\n");
1364 int p0[] = {163,7,6,3,0,-1};
1365 int p1[] = {193,15,0,-1};
1378 for (i=0; i<
num0; i++)
1382 for (j=0; j < 2; j++)
1406 fprintf(stderr,
"GF(2^m) modular division test failed!\n");
1427 int p0[] = {163,7,6,3,0,-1};
1428 int p1[] = {193,15,0,-1};
1441 for (i=0; i<
num0; i++)
1446 for (j=0; j < 2; j++)
1477 fprintf(stderr,
"GF(2^m) modular exponentiation test failed!\n");
1498 int p0[] = {163,7,6,3,0,-1};
1499 int p1[] = {193,15,0,-1};
1512 for (i=0; i<
num0; i++)
1515 for (j=0; j < 2; j++)
1536 fprintf(stderr,
"GF(2^m) modular square root test failed!\n");
1556 int i, j, s = 0,
t, ret = 0;
1557 int p0[] = {163,7,6,3,0,-1};
1558 int p1[] = {193,15,0,-1};
1570 for (i=0; i<
num0; i++)
1573 for (j=0; j < 2; j++)
1588 BIO_puts(bp,
" is root of z^2 + z = ");
1600 fprintf(stderr,
"GF(2^m) modular solve quadratic test failed!\n");
1612 BIO_puts(bp,
"There are no roots of z^2 + z = ");
1625 fprintf(stderr,
"All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n", num0);
1626 fprintf(stderr,
"this is very unlikely and probably indicates an error.\n");
1658 int legendre, kronecker;
1665 if (a == NULL || b == NULL || r == NULL || t == NULL)
goto err;
1682 for (i = 0; i <
num0; i++)
1707 fprintf(stderr,
"Legendre symbol computation failed\n");
1714 if (kronecker < -1)
goto err;
1717 kronecker = -kronecker;
1719 if (legendre != kronecker)
1721 fprintf(stderr,
"legendre != kronecker; a = ");
1723 fprintf(stderr,
", b = ");
1725 fprintf(stderr,
"\n");
1754 if (a == NULL || p == NULL || r == NULL)
goto err;
1758 for (i = 0; i < 16; i++)
1762 unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 };
1776 for (j = 0; j <
num2; j++)
1781 if (!
BN_nnmod(r, r, p, ctx))
goto err;
1784 if (!
BN_nnmod(a, a, p, ctx))
goto err;
1786 if (!
BN_mul(a, a, r, ctx))
goto err;
1788 if (!
BN_sub(a, a, p))
goto err;
1793 if (!
BN_nnmod(a, a, p, ctx))
goto err;
1797 fprintf(stderr,
"BN_mod_sqrt failed: a = ");
1799 fprintf(stderr,
", r = ");
1801 fprintf(stderr,
", p = ");
1803 fprintf(stderr,
"\n");
1840 for (i=0; i<
num0; i++)
1860 fprintf(stderr,
"Left shift test failed!\n");
1861 fprintf(stderr,
"a=");
1863 fprintf(stderr,
"\nb=");
1865 fprintf(stderr,
"\nc=");
1867 fprintf(stderr,
"\nd=");
1869 fprintf(stderr,
"\n");
1891 for (i=0; i<
num0; i++)
1909 fprintf(stderr,
"Left shift one test failed!\n");
1935 for (i=0; i<
num0; i++)
1955 fprintf(stderr,
"Right shift test failed!\n");
1978 for (i=0; i<
num0; i++)
1996 fprintf(stderr,
"Right shift one test failed!\n");
2009 static unsigned int neg=0;
2010 static int sign[8]={0,0,0,1,1,0,1,1};
2012 return(sign[(neg++)%8]);