AFAICS lst1 stands for "lshift test" not "list".
[openssl.git] / crypto / bn / bntest.c
index 562de76f1bd14e0866dae7fc04a160a7e75c4546..78fd3253839023a7b75e5a7cc22db249aa8fe6ea 100644 (file)
@@ -75,7 +75,7 @@
 int test_add(BIO *bp);
 int test_sub(BIO *bp);
 int test_lshift1(BIO *bp);
 int test_add(BIO *bp);
 int test_sub(BIO *bp);
 int test_lshift1(BIO *bp);
-int test_lshift(BIO *bp,BN_CTX *ctx);
+int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_);
 int test_rshift1(BIO *bp);
 int test_rshift(BIO *bp,BN_CTX *ctx);
 int test_div(BIO *bp,BN_CTX *ctx);
 int test_rshift1(BIO *bp);
 int test_rshift(BIO *bp,BN_CTX *ctx);
 int test_div(BIO *bp,BN_CTX *ctx);
@@ -95,13 +95,22 @@ static int results=0;
 #include "bss_file.c"
 #endif
 
 #include "bss_file.c"
 #endif
 
+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";
+
 int main(int argc, char *argv[])
        {
        BN_CTX *ctx;
        BIO *out;
        char *outfile=NULL;
 
 int main(int argc, char *argv[])
        {
        BN_CTX *ctx;
        BIO *out;
        char *outfile=NULL;
 
-       srand((unsigned int)time(NULL));
+       results = 0;
+
+       RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_rand may fail, and we don't
+                                              * even check its return value
+                                              * (which we should) */
 
        argc--;
        argv++;
 
        argc--;
        argv++;
@@ -152,8 +161,13 @@ int main(int argc, char *argv[])
        if (!test_lshift1(out)) goto err;
        fflush(stdout);
 
        if (!test_lshift1(out)) goto err;
        fflush(stdout);
 
+       fprintf(stderr,"test BN_lshift (fixed)\n");
+       if (!test_lshift(out,ctx,BN_bin2bn(lst,sizeof(lst)-1,NULL)))
+           goto err;
+       fflush(stdout);
+
        fprintf(stderr,"test BN_lshift\n");
        fprintf(stderr,"test BN_lshift\n");
-       if (!test_lshift(out,ctx)) goto err;
+       if (!test_lshift(out,ctx,NULL)) goto err;
        fflush(stdout);
 
        fprintf(stderr,"test BN_rshift1\n");
        fflush(stdout);
 
        fprintf(stderr,"test BN_rshift1\n");
@@ -201,9 +215,13 @@ int main(int argc, char *argv[])
        if (!test_exp(out,ctx)) goto err;
        fflush(stdout);
 
        if (!test_exp(out,ctx)) goto err;
        fflush(stdout);
 
+       BN_CTX_free(ctx);
+       BIO_free(out);
+
 /**/
        exit(0);
 err:
 /**/
        exit(0);
 err:
+       BIO_puts(out,"1\n"); /* make sure bc fails if we are piping to it */
        ERR_load_crypto_strings();
        ERR_print_errors(out);
        exit(1);
        ERR_load_crypto_strings();
        ERR_print_errors(out);
        exit(1);
@@ -815,19 +833,24 @@ int test_exp(BIO *bp, BN_CTX *ctx)
        return(1);
        }
 
        return(1);
        }
 
-int test_lshift(BIO *bp,BN_CTX *ctx)
+int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_)
        {
        BIGNUM *a,*b,*c,*d;
        int i;
 
        {
        BIGNUM *a,*b,*c,*d;
        int i;
 
-       a=BN_new();
        b=BN_new();
        c=BN_new();
        d=BN_new();
        BN_one(c);
 
        b=BN_new();
        c=BN_new();
        d=BN_new();
        BN_one(c);
 
-       BN_rand(a,200,0,0); /**/
-       a->neg=rand_neg();
+       if(a_)
+           a=a_;
+       else
+           {
+           a=BN_new();
+           BN_rand(a,200,0,0); /**/
+           a->neg=rand_neg();
+           }
        for (i=0; i<70; i++)
                {
                BN_lshift(b,a,i+1);
        for (i=0; i<70; i++)
                {
                BN_lshift(b,a,i+1);
@@ -849,6 +872,15 @@ int test_lshift(BIO *bp,BN_CTX *ctx)
                if(!BN_is_zero(d))
                    {
                    BIO_puts(bp,"Left shift test failed!\n");
                if(!BN_is_zero(d))
                    {
                    BIO_puts(bp,"Left shift test failed!\n");
+                   BIO_puts(bp,"a=");
+                   BN_print(bp,a);
+                   BIO_puts(bp,"\nb=");
+                   BN_print(bp,b);
+                   BIO_puts(bp,"\nc=");
+                   BN_print(bp,c);
+                   BIO_puts(bp,"\nd=");
+                   BN_print(bp,d);
+                   BIO_puts(bp,"\n");
                    return 0;
                    }
                }
                    return 0;
                    }
                }