comment
[openssl.git] / crypto / bn / bntest.c
index 91563dd22228ad1a314a24c4576f06027f66f7de..443cf420e5ce9cd54a2f420676adf465d1b08db0 100644 (file)
@@ -60,7 +60,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "openssl/e_os.h"
+#include "e_os.h"
 
 #include <openssl/bio.h>
 #include <openssl/bn.h>
@@ -68,7 +68,7 @@
 #include <openssl/x509.h>
 #include <openssl/err.h>
 
-#ifdef WINDOWS
+#ifdef OPENSSL_SYS_WINDOWS
 #include "../bio/bss_file.c"
 #endif
 
@@ -96,7 +96,7 @@ int test_sqrt(BIO *bp,BN_CTX *ctx);
 int rand_neg(void);
 static int results=0;
 
-#ifdef NO_STDIO
+#ifdef OPENSSL_NO_STDIO
 #define APPS_WIN16
 #include "bss_file.c"
 #endif
@@ -109,11 +109,9 @@ static const char rnd_seed[] = "string to make the random number generator think
 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[])
@@ -487,9 +485,11 @@ int test_mul(BIO *bp)
        {
        BIGNUM a,b,c,d,e;
        int i;
-       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);
@@ -507,7 +507,7 @@ int test_mul(BIO *bp)
                        BN_bntest_rand(&b,i-num1,0,0);
                a.neg=rand_neg();
                b.neg=rand_neg();
-               BN_mul(&c,&a,&b,&ctx);
+               BN_mul(&c,&a,&b,ctx);
                if (bp != NULL)
                        {
                        if (!results)
@@ -520,7 +520,7 @@ int test_mul(BIO *bp)
                        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))
                    {
@@ -533,7 +533,7 @@ int test_mul(BIO *bp)
        BN_free(&c);
        BN_free(&d);
        BN_free(&e);
-       BN_CTX_free(&ctx);
+       BN_CTX_free(ctx);
        return(1);
        }
 
@@ -703,7 +703,7 @@ int test_mod(BIO *bp, BN_CTX *ctx)
 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();
@@ -711,6 +711,7 @@ int test_mod_mul(BIO *bp, BN_CTX *ctx)
        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++)
                {
@@ -761,6 +762,7 @@ int test_mod_mul(BIO *bp, BN_CTX *ctx)
                    return 0;
                    }
                }
+       }
        BN_free(a);
        BN_free(b);
        BN_free(c);
@@ -907,6 +909,7 @@ int test_kron(BIO *bp, BN_CTX *ctx)
         * 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++)
@@ -914,12 +917,16 @@ int test_kron(BIO *bp, BN_CTX *ctx)
                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;
@@ -928,7 +935,7 @@ int test_kron(BIO *bp, BN_CTX *ctx)
                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;
@@ -938,6 +945,9 @@ int test_kron(BIO *bp, BN_CTX *ctx)
                
                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)
                        {
@@ -991,6 +1001,7 @@ int test_sqrt(BIO *bp, BN_CTX *ctx)
                        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++)
                        {
@@ -1003,6 +1014,8 @@ int test_sqrt(BIO *bp, BN_CTX *ctx)
                        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;
@@ -1208,7 +1221,7 @@ int test_rshift1(BIO *bp)
                        }
                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;