#include <stdlib.h>
#include <string.h>
-#include "openssl/e_os.h"
+#include "e_os.h"
#include <openssl/bio.h>
#include <openssl/bn.h>
#include <openssl/x509.h>
#include <openssl/err.h>
-#ifdef WINDOWS
+#ifdef OPENSSL_SYS_WINDOWS
#include "../bio/bss_file.c"
#endif
int rand_neg(void);
static int results=0;
-#ifdef NO_STDIO
+#ifdef OPENSSL_NO_STDIO
#define APPS_WIN16
#include "bss_file.c"
#endif
static void message(BIO *out, char *m)
{
fprintf(stderr, "test %s\n", m);
-#if defined(linux) || defined(__FreeBSD__) /* can we use GNU bc features? */
BIO_puts(out, "print \"test ");
BIO_puts(out, m);
BIO_puts(out, "\\n\"\n");
-#endif
}
int main(int argc, char *argv[])
{
BIGNUM a,b,c;
int i;
- int j;
BN_init(&a);
BN_init(&b);
BN_bntest_rand(&b,450+i,0,0);
a.neg=rand_neg();
b.neg=rand_neg();
- if (bp == NULL)
- for (j=0; j<10000; j++)
- BN_add(&c,&a,&b);
BN_add(&c,&a,&b);
if (bp != NULL)
{
{
BIGNUM a,b,c;
int i;
- int j;
BN_init(&a);
BN_init(&b);
a.neg=rand_neg();
b.neg=rand_neg();
}
- if (bp == NULL)
- for (j=0; j<10000; j++)
- BN_sub(&c,&a,&b);
BN_sub(&c,&a,&b);
if (bp != NULL)
{
{
BIGNUM a,b,c,d,e;
int i;
- int j;
BN_init(&a);
BN_init(&b);
BN_bntest_rand(&b,50+3*(i-num1),0,0);
a.neg=rand_neg();
b.neg=rand_neg();
- if (bp == NULL)
- for (j=0; j<100; j++)
- BN_div(&d,&c,&a,&b,ctx);
BN_div(&d,&c,&a,&b,ctx);
if (bp != NULL)
{
BIGNUM a,b,c,d,e;
BN_RECP_CTX recp;
int i;
- int j;
BN_RECP_CTX_init(&recp);
BN_init(&a);
a.neg=rand_neg();
b.neg=rand_neg();
BN_RECP_CTX_set(&recp,&b,ctx);
- if (bp == NULL)
- for (j=0; j<100; j++)
- BN_div_recp(&d,&c,&a,&recp,ctx);
BN_div_recp(&d,&c,&a,&recp,ctx);
if (bp != NULL)
{
{
BIGNUM a,b,c,d,e;
int i;
- int j;
- BN_CTX ctx;
+ BN_CTX *ctx;
- BN_CTX_init(&ctx);
+ ctx = BN_CTX_new();
+ if (ctx == NULL) exit(1);
+
BN_init(&a);
BN_init(&b);
BN_init(&c);
BN_bntest_rand(&b,i-num1,0,0);
a.neg=rand_neg();
b.neg=rand_neg();
- if (bp == NULL)
- for (j=0; j<100; j++)
- BN_mul(&c,&a,&b,&ctx);
- BN_mul(&c,&a,&b,&ctx);
+ BN_mul(&c,&a,&b,ctx);
if (bp != NULL)
{
if (!results)
BN_print(bp,&c);
BIO_puts(bp,"\n");
}
- BN_div(&d,&e,&c,&a,&ctx);
+ BN_div(&d,&e,&c,&a,ctx);
BN_sub(&d,&d,&b);
if(!BN_is_zero(&d) || !BN_is_zero(&e))
{
BN_free(&c);
BN_free(&d);
BN_free(&e);
- BN_CTX_free(&ctx);
+ BN_CTX_free(ctx);
return(1);
}
{
BIGNUM a,c,d,e;
int i;
- int j;
BN_init(&a);
BN_init(&c);
{
BN_bntest_rand(&a,40+i*10,0,0);
a.neg=rand_neg();
- if (bp == NULL)
- for (j=0; j<100; j++)
- BN_sqr(&c,&a,ctx);
BN_sqr(&c,&a,ctx);
if (bp != NULL)
{
BIGNUM a,b,c,d,A,B;
BIGNUM n;
int i;
- int j;
BN_MONT_CTX *mont;
BN_init(&a);
BN_to_montgomery(&A,&a,mont,ctx);
BN_to_montgomery(&B,&b,mont,ctx);
- if (bp == NULL)
- for (j=0; j<100; j++)
- BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
BN_from_montgomery(&A,&c,mont,ctx);/**/
if (bp != NULL)
{
BIGNUM *a,*b,*c,*d,*e;
int i;
- int j;
a=BN_new();
b=BN_new();
BN_bntest_rand(b,450+i*10,0,0); /**/
a->neg=rand_neg();
b->neg=rand_neg();
- if (bp == NULL)
- for (j=0; j<100; j++)
- BN_mod(c,a,b,ctx);/**/
BN_mod(c,a,b,ctx);/**/
if (bp != NULL)
{
int test_mod_mul(BIO *bp, BN_CTX *ctx)
{
BIGNUM *a,*b,*c,*d,*e;
- int i;
+ int i,j;
a=BN_new();
b=BN_new();
d=BN_new();
e=BN_new();
+ for (j=0; j<3; j++) {
BN_bntest_rand(c,1024,0,0); /**/
for (i=0; i<num0; i++)
{
BN_bntest_rand(b,425+i*11,0,0); /**/
a->neg=rand_neg();
b->neg=rand_neg();
- /* if (bp == NULL)
- for (j=0; j<100; j++)
- BN_mod_mul(d,a,b,c,ctx);*/ /**/
-
if (!BN_mod_mul(e,a,b,c,ctx))
{
unsigned long l;
return 0;
}
}
+ }
BN_free(a);
BN_free(b);
BN_free(c);
* works.) */
if (!BN_generate_prime(b, 512, 0, NULL, NULL, genprime_cb, NULL)) goto err;
+ b->neg = rand_neg();
putc('\n', stderr);
for (i = 0; i < num0; i++)
if (!BN_bntest_rand(a, 512, 0, 0)) goto err;
a->neg = rand_neg();
- /* t := (b-1)/2 (note that b is odd) */
+ /* t := (|b|-1)/2 (note that b is odd) */
if (!BN_copy(t, b)) goto err;
+ t->neg = 0;
if (!BN_sub_word(t, 1)) goto err;
if (!BN_rshift1(t, t)) goto err;
/* r := a^t mod b */
- if (!BN_mod_exp(r, a, t, b, ctx)) goto err;
+ b->neg=0;
+
+ if (!BN_mod_exp_recp(r, a, t, b, ctx)) goto err; /* XXX should be BN_mod_exp_recp, but ..._recp triggers a bug that must be fixed */
+ b->neg=1;
if (BN_is_word(r, 1))
legendre = 1;
else
{
if (!BN_add_word(r, 1)) goto err;
- if (0 != BN_cmp(r, b))
+ if (0 != BN_ucmp(r, b))
{
fprintf(stderr, "Legendre symbol computation failed\n");
goto err;
kronecker = BN_kronecker(a, b, ctx);
if (kronecker < -1) goto err;
+ /* we actually need BN_kronecker(a, |b|) */
+ if (a->neg && b->neg)
+ kronecker = -kronecker;
if (legendre != kronecker)
{
if (!BN_generate_prime(p, 256, 0, a, r, genprime_cb, NULL)) goto err;
putc('\n', stderr);
}
+ p->neg = rand_neg();
for (j = 0; j < num2; j++)
{
if (!BN_nnmod(a, a, p, ctx)) goto err;
if (!BN_mod_sqr(a, a, p, ctx)) goto err;
if (!BN_mul(a, a, r, ctx)) goto err;
+ if (rand_neg())
+ if (!BN_sub(a, a, p)) goto err;
if (!BN_mod_sqrt(r, a, p, ctx)) goto err;
if (!BN_mod_sqr(r, r, p, ctx)) goto err;
}
BN_sub(c,a,b);
BN_sub(c,c,b);
- if(!BN_is_zero(c) && !BN_is_one(c))
+ if(!BN_is_zero(c) && !BN_abs_is_word(c, 1))
{
fprintf(stderr,"Right shift one test failed!\n");
return 0;