bntest.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
* This package is an SSL implementation written
* by Eric Young (eay@cryptsoft.com).
* The implementation was written so as to conform with Netscapes SSL.
*
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
*
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* "This product includes cryptographic software written by
* Eric Young (eay@cryptsoft.com)"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
* 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
*
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
* [including the GNU Public Licence.]
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "e_os.h"
int rand_neg(void);
static int results=0;
static unsigned char lst[]="\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9"
"\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0";
static const char rnd_seed[] = "string to make the random number generator think it has entropy";
{
}
{
results = 0;
argc--;
argv++;
while (argc >= 1)
{
results=1;
{
if (--argc < 1) break;
}
argc--;
argv++;
}
ctx=BN_CTX_new();
{
}
else
{
{
EXIT(1);
}
}
if (!results)
goto err;
/**/
EXIT(0);
err:
* the failure, see test_bn in test/Makefile.ssl*/
EXIT(1);
return(1);
}
{
BIGNUM a,b,c;
int i;
BN_init(&a);
BN_init(&b);
BN_init(&c);
BN_bntest_rand(&a,512,0,0);
for (i=0; i<num0; i++)
{
BN_bntest_rand(&b,450+i,0,0);
BN_add(&c,&a,&b);
{
if (!results)
{
}
}
BN_add(&c,&c,&b);
BN_add(&c,&c,&a);
if(!BN_is_zero(&c))
{
return 0;
}
}
BN_free(&a);
BN_free(&b);
BN_free(&c);
return(1);
}
{
BIGNUM a,b,c;
int i;
BN_init(&a);
BN_init(&b);
BN_init(&c);
{
if (i < num1)
{
BN_bntest_rand(&a,512,0,0);
BN_copy(&b,&a);
if (BN_set_bit(&a,i)==0) return(0);
BN_add_word(&b,i);
}
else
{
}
BN_sub(&c,&a,&b);
{
if (!results)
{
}
}
BN_add(&c,&c,&b);
BN_sub(&c,&c,&a);
if(!BN_is_zero(&c))
{
return 0;
}
}
BN_free(&a);
BN_free(&b);
BN_free(&c);
return(1);
}
{
BIGNUM a,b,c,d,e;
int i;
BN_init(&a);
BN_init(&b);
BN_init(&c);
BN_init(&d);
BN_init(&e);
{
if (i < num1)
{
BN_bntest_rand(&a,400,0,0);
BN_copy(&b,&a);
BN_lshift(&a,&a,i);
BN_add_word(&a,i);
}
else
{
if (!results)
{
}
if (!results)
{
}
}
BN_add(&d,&e,&c);
BN_sub(&d,&d,&a);
if(!BN_is_zero(&d))
{
return 0;
}
}
BN_free(&a);
BN_free(&b);
BN_free(&c);
BN_free(&d);
BN_free(&e);
return(1);
}
{
BIGNUM a,b,c,d,e;
int i;
BN_init(&a);
BN_init(&b);
BN_init(&c);
BN_init(&d);
BN_init(&e);
{
if (i < num1)
{
BN_bntest_rand(&a,400,0,0);
BN_copy(&b,&a);
BN_lshift(&a,&a,i);
BN_add_word(&a,i);
}
else
{
if (!results)
{
}
if (!results)
{
}
}
BN_add(&d,&e,&c);
BN_sub(&d,&d,&a);
if(!BN_is_zero(&d))
{
BN_print_fp(stderr,&a);
BN_print_fp(stderr,&b);
return 0;
}
}
BN_free(&a);
BN_free(&b);
BN_free(&c);
BN_free(&d);
BN_free(&e);
return(1);
}
{
BIGNUM a,b,c,d,e;
int i;
ctx = BN_CTX_new();
BN_init(&a);
BN_init(&b);
BN_init(&c);
BN_init(&d);
BN_init(&e);
{
if (i <= num1)
{
BN_bntest_rand(&a,100,0,0);
BN_bntest_rand(&b,100,0,0);
}
else
BN_bntest_rand(&b,i-num1,0,0);
{
if (!results)
{
}
}
BN_sub(&d,&d,&b);
if(!BN_is_zero(&d) || !BN_is_zero(&e))
{
return 0;
}
}
BN_free(&a);
BN_free(&b);
BN_free(&c);
BN_free(&d);
BN_free(&e);
return(1);
}
{
BIGNUM a,c,d,e;
int i;
BN_init(&a);
BN_init(&c);
BN_init(&d);
BN_init(&e);
for (i=0; i<num0; i++)
{
{
if (!results)
{
}
}
BN_sub(&d,&d,&a);
if(!BN_is_zero(&d) || !BN_is_zero(&e))
{
return 0;
}
}
BN_free(&a);
BN_free(&c);
BN_free(&d);
BN_free(&e);
return(1);
}
{
BIGNUM a,b,c,d,A,B;
BIGNUM n;
int i;
BN_init(&a);
BN_init(&b);
BN_init(&c);
BN_init(&d);
BN_init(&A);
BN_init(&B);
BN_init(&n);
for (i=0; i<num2; i++)
{
if (bits == 0)
continue;
{
if (!results)
{
#ifdef undef
BN_num_bits(&a),
BN_num_bits(&b),
BN_num_bits(mont->N));
#endif
}
}
BN_mod_mul(&d,&a,&b,&n,ctx);
BN_sub(&d,&d,&A);
if(!BN_is_zero(&d))
{
return 0;
}
}
BN_free(&a);
BN_free(&b);
BN_free(&c);
BN_free(&d);
BN_free(&A);
BN_free(&B);
BN_free(&n);
return(1);
}
{
BIGNUM *a,*b,*c,*d,*e;
int i;
a=BN_new();
b=BN_new();
c=BN_new();
d=BN_new();
e=BN_new();
for (i=0; i<num0; i++)
{
{
if (!results)
{
}
}
BN_sub(e,e,c);
if(!BN_is_zero(e))
{
return 0;
}
}
BN_free(a);
BN_free(b);
BN_free(c);
BN_free(d);
BN_free(e);
return(1);
}
{
BIGNUM *a,*b,*c,*d,*e;
int i,j;
a=BN_new();
b=BN_new();
c=BN_new();
d=BN_new();
e=BN_new();
for (j=0; j<3; j++) {
for (i=0; i<num0; i++)
{
if (!BN_mod_mul(e,a,b,c,ctx))
{
unsigned long l;
while ((l=ERR_get_error()))
ERR_error_string(l,NULL));
EXIT(1);
}
{
if (!results)
{
{
/* If (a*b) % c is negative, c must be added
* in order to obtain the normalized remainder
* (new with OpenSSL 0.9.7, previous versions of
* BN_mod_mul could generate negative results)
*/
}
}
}
BN_sub(d,d,e);
if(!BN_is_zero(b))
{
return 0;
}
}
}
BN_free(a);
BN_free(b);
BN_free(c);
BN_free(d);
BN_free(e);
return(1);
}
{
BIGNUM *a,*b,*c,*d,*e;
int i;
a=BN_new();
b=BN_new();
c=BN_new();
d=BN_new();
e=BN_new();
for (i=0; i<num2; i++)
{
if (!BN_mod_exp(d,a,b,c,ctx))
return(00);
{
if (!results)
{
}
}
BN_sub(e,e,d);
if(!BN_is_zero(b))
{
return 0;
}
}
BN_free(a);
BN_free(b);
BN_free(c);
BN_free(d);
BN_free(e);
return(1);
}
{
int i;
a=BN_new();
b=BN_new();
d=BN_new();
e=BN_new();
for (i=0; i<num2; i++)
{
return(00);
{
if (!results)
{
}
}
BN_one(e);
BN_sub(e,e,d);
if(!BN_is_zero(e))
{
return 0;
}
}
BN_free(a);
BN_free(b);
BN_free(d);
BN_free(e);
return(1);
}
static void genprime_cb(int p, int n, void *arg)
{
char c='*';
if (p == 0) c='.';
if (p == 1) c='+';
if (p == 2) c='*';
if (p == 3) c='\n';
(void)n;
(void)arg;
}
{
BIGNUM *a,*b,*r,*t;
int i;
int ret = 0;
a = BN_new();
b = BN_new();
r = BN_new();
t = BN_new();
/* We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol).
* In this case we know that if b is prime, then BN_kronecker(a, b, ctx)
* is congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol).
* So we generate a random prime b and compare these values
* for a number of random a's. (That is, we run the Solovay-Strassen
* primality test to confirm that b is prime, except that we
* don't want to test whether b is prime but whether BN_kronecker
* works.) */
for (i = 0; i < num0; i++)
{
/* t := (|b|-1)/2 (note that b is odd) */
t->neg = 0;
if (!BN_rshift1(t, t)) goto err;
/* r := a^t mod b */
b->neg=0;
b->neg=1;
if (BN_is_word(r, 1))
legendre = 1;
else if (BN_is_zero(r))
legendre = 0;
else
{
if (0 != BN_ucmp(r, b))
{
goto err;
}
legendre = -1;
}
/* we actually need BN_kronecker(a, |b|) */
{
BN_print_fp(stderr, a);
BN_print_fp(stderr, b);
goto err;
}
}
ret = 1;
err:
return ret;
}
{
BIGNUM *a,*p,*r;
int i, j;
int ret = 0;
a = BN_new();
p = BN_new();
r = BN_new();
for (i = 0; i < 16; i++)
{
if (i < 8)
{
}
else
{
}
for (j = 0; j < num2; j++)
{
/* construct 'a' such that it is a square modulo p,
* but in general not a proper square and not reduced modulo p */
if (rand_neg())
if (BN_cmp(a, r) != 0)
{
BN_print_fp(stderr, a);
BN_print_fp(stderr, r);
BN_print_fp(stderr, p);
goto err;
}
}
}
ret = 1;
err:
return ret;
}
{
BIGNUM *a,*b,*c,*d;
int i;
b=BN_new();
c=BN_new();
d=BN_new();
BN_one(c);
if(a_)
a=a_;
else
{
a=BN_new();
}
for (i=0; i<num0; i++)
{
BN_lshift(b,a,i+1);
BN_add(c,c,c);
{
if (!results)
{
}
}
BN_sub(d,d,b);
if(!BN_is_zero(d))
{
BN_print_fp(stderr,a);
BN_print_fp(stderr,b);
BN_print_fp(stderr,c);
BN_print_fp(stderr,d);
return 0;
}
}
BN_free(a);
BN_free(b);
BN_free(c);
BN_free(d);
return(1);
}
{
BIGNUM *a,*b,*c;
int i;
a=BN_new();
b=BN_new();
c=BN_new();
for (i=0; i<num0; i++)
{
BN_lshift1(b,a);
{
if (!results)
{
}
}
BN_add(c,a,a);
BN_sub(a,b,c);
if(!BN_is_zero(a))
{
return 0;
}
BN_copy(a,b);
}
BN_free(a);
BN_free(b);
BN_free(c);
return(1);
}
{
BIGNUM *a,*b,*c,*d,*e;
int i;
a=BN_new();
b=BN_new();
c=BN_new();
d=BN_new();
e=BN_new();
BN_one(c);
for (i=0; i<num0; i++)
{
BN_rshift(b,a,i+1);
BN_add(c,c,c);
{
if (!results)
{
}
}
BN_sub(d,d,b);
if(!BN_is_zero(d))
{
return 0;
}
}
BN_free(a);
BN_free(b);
BN_free(c);
BN_free(d);
BN_free(e);
return(1);
}
{
BIGNUM *a,*b,*c;
int i;
a=BN_new();
b=BN_new();
c=BN_new();
for (i=0; i<num0; i++)
{
BN_rshift1(b,a);
{
if (!results)
{
}
}
BN_sub(c,a,b);
BN_sub(c,c,b);
{
return 0;
}
BN_copy(a,b);
}
BN_free(a);
BN_free(b);
BN_free(c);
return(1);
}
int rand_neg(void)
{
static unsigned int neg=0;
}