Undo previous commit, which was an accident.
[openssl.git] / crypto / bn / bntest.c
index 9276a079d0e096ee09d40e1967510c8aa2b38da3..0ade0c00120f9b999fc62c3bcb2931a17999b464 100644 (file)
@@ -107,7 +107,7 @@ 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? */
+#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");
@@ -165,68 +165,68 @@ int main(int argc, char *argv[])
 
        message(out,"BN_add");
        if (!test_add(out)) goto err;
-       fflush(stdout);
+       BIO_flush(out);
 
        message(out,"BN_sub");
        if (!test_sub(out)) goto err;
-       fflush(stdout);
+       BIO_flush(out);
 
        message(out,"BN_lshift1");
        if (!test_lshift1(out)) goto err;
-       fflush(stdout);
+       BIO_flush(out);
 
        message(out,"BN_lshift (fixed)");
        if (!test_lshift(out,ctx,BN_bin2bn(lst,sizeof(lst)-1,NULL)))
            goto err;
-       fflush(stdout);
+       BIO_flush(out);
 
        message(out,"BN_lshift");
        if (!test_lshift(out,ctx,NULL)) goto err;
-       fflush(stdout);
+       BIO_flush(out);
 
        message(out,"BN_rshift1");
        if (!test_rshift1(out)) goto err;
-       fflush(stdout);
+       BIO_flush(out);
 
        message(out,"BN_rshift");
        if (!test_rshift(out,ctx)) goto err;
-       fflush(stdout);
+       BIO_flush(out);
 
        message(out,"BN_sqr");
        if (!test_sqr(out,ctx)) goto err;
-       fflush(stdout);
+       BIO_flush(out);
 
        message(out,"BN_mul");
        if (!test_mul(out)) goto err;
-       fflush(stdout);
+       BIO_flush(out);
 
        message(out,"BN_div");
        if (!test_div(out,ctx)) goto err;
-       fflush(stdout);
+       BIO_flush(out);
 
        message(out,"BN_div_recp");
        if (!test_div_recp(out,ctx)) goto err;
-       fflush(stdout);
+       BIO_flush(out);
 
        message(out,"BN_mod");
        if (!test_mod(out,ctx)) goto err;
-       fflush(stdout);
+       BIO_flush(out);
 
        message(out,"BN_mod_mul");
        if (!test_mod_mul(out,ctx)) goto err;
-       fflush(stdout);
+       BIO_flush(out);
 
        message(out,"BN_mont");
        if (!test_mont(out,ctx)) goto err;
-       fflush(stdout);
+       BIO_flush(out);
 
        message(out,"BN_mod_exp");
        if (!test_mod_exp(out,ctx)) goto err;
-       fflush(stdout);
+       BIO_flush(out);
 
        message(out,"BN_exp");
        if (!test_exp(out,ctx)) goto err;
-       fflush(stdout);
+       BIO_flush(out);
 
        BN_CTX_free(ctx);
        BIO_free(out);
@@ -234,7 +234,9 @@ int main(int argc, char *argv[])
 /**/
        exit(0);
 err:
-       BIO_puts(out,"1\n"); /* make sure bc fails if we are piping to it */
+       BIO_puts(out,"1\n"); /* make sure the Perl script fed by bc notices
+                             * the failure, see test_bn in test/Makefile.ssl*/
+       BIO_flush(out);
        ERR_load_crypto_strings();
        ERR_print_errors_fp(stderr);
        exit(1);
@@ -505,7 +507,7 @@ int test_mul(BIO *bp)
 
        for (i=0; i<num0+num1; i++)
                {
-               if (i < num1)
+               if (i <= num1)
                        {
                        BN_rand(&a,100,0,0);
                        BN_rand(&b,100,0,0);
@@ -615,7 +617,7 @@ int test_mont(BIO *bp, BN_CTX *ctx)
        BN_rand(&b,100,0,0); /**/
        for (i=0; i<num2; i++)
                {
-               int bits = (100%BN_BITS2+1)*BN_BITS2*i*BN_BITS2;
+               int bits = (200*(i+1))/num2;
 
                if (bits == 0)
                        continue;
@@ -759,6 +761,16 @@ int test_mod_mul(BIO *bp, BN_CTX *ctx)
                                BN_print(bp,b);
                                BIO_puts(bp," % ");
                                BN_print(bp,c);
+                               if ((a->neg ^ b->neg) && !BN_is_zero(e))
+                                       {
+                                       /* 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)
+                                        */
+                                       BIO_puts(bp," + ");
+                                       BN_print(bp,c);
+                                       }
                                BIO_puts(bp," - ");
                                }
                        BN_print(bp,e);
@@ -770,6 +782,7 @@ int test_mod_mul(BIO *bp, BN_CTX *ctx)
                if(!BN_is_zero(b))
                    {
                    fprintf(stderr,"Modulo multiply test failed!\n");
+                   ERR_print_errors_fp(stderr);
                    return 0;
                    }
                }