Prepare for 0.9.8ze-dev
[oweals/openssl.git] / crypto / bn / bntest.c
index d4cb92f84725ea22fbb7c04f1411ea8015792c74..9138c2f15ea76b0192adf120b89e14f81d495c46 100644 (file)
@@ -106,6 +106,7 @@ int test_mont(BIO *bp,BN_CTX *ctx);
 int test_mod(BIO *bp,BN_CTX *ctx);
 int test_mod_mul(BIO *bp,BN_CTX *ctx);
 int test_mod_exp(BIO *bp,BN_CTX *ctx);
+int test_mod_exp_mont_consttime(BIO *bp,BN_CTX *ctx);
 int test_exp(BIO *bp,BN_CTX *ctx);
 int test_gf2m_add(BIO *bp);
 int test_gf2m_mod(BIO *bp);
@@ -183,116 +184,120 @@ int main(int argc, char *argv[])
 
        message(out,"BN_add");
        if (!test_add(out)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_sub");
        if (!test_sub(out)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_lshift1");
        if (!test_lshift1(out)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_lshift (fixed)");
        if (!test_lshift(out,ctx,BN_bin2bn(lst,sizeof(lst)-1,NULL)))
            goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_lshift");
        if (!test_lshift(out,ctx,NULL)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_rshift1");
        if (!test_rshift1(out)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_rshift");
        if (!test_rshift(out,ctx)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_sqr");
        if (!test_sqr(out,ctx)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_mul");
        if (!test_mul(out)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_div");
        if (!test_div(out,ctx)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_div_word");
        if (!test_div_word(out)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_div_recp");
        if (!test_div_recp(out,ctx)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_mod");
        if (!test_mod(out,ctx)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_mod_mul");
        if (!test_mod_mul(out,ctx)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_mont");
        if (!test_mont(out,ctx)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_mod_exp");
        if (!test_mod_exp(out,ctx)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
+
+       message(out,"BN_mod_exp_mont_consttime");
+       if (!test_mod_exp_mont_consttime(out,ctx)) goto err;
+       (void)BIO_flush(out);
 
        message(out,"BN_exp");
        if (!test_exp(out,ctx)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_kronecker");
        if (!test_kron(out,ctx)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_mod_sqrt");
        if (!test_sqrt(out,ctx)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_GF2m_add");
        if (!test_gf2m_add(out)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_GF2m_mod");
        if (!test_gf2m_mod(out)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_GF2m_mod_mul");
        if (!test_gf2m_mod_mul(out,ctx)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_GF2m_mod_sqr");
        if (!test_gf2m_mod_sqr(out,ctx)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_GF2m_mod_inv");
        if (!test_gf2m_mod_inv(out,ctx)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_GF2m_mod_div");
        if (!test_gf2m_mod_div(out,ctx)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_GF2m_mod_exp");
        if (!test_gf2m_mod_exp(out,ctx)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_GF2m_mod_sqrt");
        if (!test_gf2m_mod_sqrt(out,ctx)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        message(out,"BN_GF2m_mod_solve_quad");
        if (!test_gf2m_mod_solve_quad(out,ctx)) goto err;
-       BIO_flush(out);
+       (void)BIO_flush(out);
 
        BN_CTX_free(ctx);
        BIO_free(out);
@@ -302,7 +307,7 @@ int main(int argc, char *argv[])
 err:
        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);
+       (void)BIO_flush(out);
        ERR_load_crypto_strings();
        ERR_print_errors_fp(stderr);
        EXIT(1);
@@ -468,6 +473,22 @@ int test_div(BIO *bp, BN_CTX *ctx)
        return(1);
        }
 
+static void print_word(BIO *bp,BN_ULONG w)
+       {
+#ifdef SIXTY_FOUR_BIT
+       if (sizeof(w) > sizeof(unsigned long))
+               {
+               unsigned long   h=(unsigned long)(w>>32),
+                               l=(unsigned long)(w);
+
+               if (h)  BIO_printf(bp,"%lX%08lX",h,l);
+               else    BIO_printf(bp,"%lX",l);
+               return;
+               }
+#endif
+       BIO_printf(bp,"%lX",w);
+       }
+
 int test_div_word(BIO *bp)
        {
        BIGNUM   a,b;
@@ -494,7 +515,7 @@ int test_div_word(BIO *bp)
                                {
                                BN_print(bp,&a);
                                BIO_puts(bp," / ");
-                               BIO_printf(bp,"%lX",s);
+                               print_word(bp,s);
                                BIO_puts(bp," - ");
                                }
                        BN_print(bp,&b);
@@ -504,10 +525,10 @@ int test_div_word(BIO *bp)
                                {
                                BN_print(bp,&a);
                                BIO_puts(bp," % ");
-                               BIO_printf(bp,"%lX",s);
+                               print_word(bp,s);
                                BIO_puts(bp," - ");
                                }
-                       BIO_printf(bp,"%lX",r);
+                       print_word(bp,r);
                        BIO_puts(bp,"\n");
                        }
                BN_mul_word(&b,s);
@@ -655,44 +676,98 @@ int test_mul(BIO *bp)
 
 int test_sqr(BIO *bp, BN_CTX *ctx)
        {
-       BIGNUM a,c,d,e;
-       int i;
+       BIGNUM *a,*c,*d,*e;
+       int i, ret = 0;
 
-       BN_init(&a);
-       BN_init(&c);
-       BN_init(&d);
-       BN_init(&e);
+       a = BN_new();
+       c = BN_new();
+       d = BN_new();
+       e = BN_new();
+       if (a == NULL || c == NULL || d == NULL || e == NULL)
+               {
+               goto err;
+               }
 
        for (i=0; i<num0; i++)
                {
-               BN_bntest_rand(&a,40+i*10,0,0);
-               a.neg=rand_neg();
-               BN_sqr(&c,&a,ctx);
+               BN_bntest_rand(a,40+i*10,0,0);
+               a->neg=rand_neg();
+               BN_sqr(c,a,ctx);
                if (bp != NULL)
                        {
                        if (!results)
                                {
-                               BN_print(bp,&a);
+                               BN_print(bp,a);
                                BIO_puts(bp," * ");
-                               BN_print(bp,&a);
+                               BN_print(bp,a);
                                BIO_puts(bp," - ");
                                }
-                       BN_print(bp,&c);
+                       BN_print(bp,c);
                        BIO_puts(bp,"\n");
                        }
-               BN_div(&d,&e,&c,&a,ctx);
-               BN_sub(&d,&d,&a);
-               if(!BN_is_zero(&d) || !BN_is_zero(&e))
-                   {
-                   fprintf(stderr,"Square test failed!\n");
-                   return 0;
-                   }
+               BN_div(d,e,c,a,ctx);
+               BN_sub(d,d,a);
+               if(!BN_is_zero(d) || !BN_is_zero(e))
+                       {
+                       fprintf(stderr,"Square test failed!\n");
+                       goto err;
+                       }
                }
-       BN_free(&a);
-       BN_free(&c);
-       BN_free(&d);
-       BN_free(&e);
-       return(1);
+
+       /* Regression test for a BN_sqr overflow bug. */
+       BN_hex2bn(&a,
+               "80000000000000008000000000000001FFFFFFFFFFFFFFFE0000000000000000");
+       BN_sqr(c, a, ctx);
+       if (bp != NULL)
+               {
+               if (!results)
+                       {
+                       BN_print(bp,a);
+                       BIO_puts(bp," * ");
+                       BN_print(bp,a);
+                       BIO_puts(bp," - ");
+                       }
+               BN_print(bp,c);
+               BIO_puts(bp,"\n");
+               }
+       BN_mul(d, a, a, ctx);
+       if (BN_cmp(c, d))
+               {
+               fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "
+                       "different results!\n");
+               goto err;
+               }
+
+       /* Regression test for a BN_sqr overflow bug. */
+       BN_hex2bn(&a,
+               "80000000000000000000000080000001FFFFFFFE000000000000000000000000");
+       BN_sqr(c, a, ctx);
+       if (bp != NULL)
+               {
+               if (!results)
+                       {
+                       BN_print(bp,a);
+                       BIO_puts(bp," * ");
+                       BN_print(bp,a);
+                       BIO_puts(bp," - ");
+                       }
+               BN_print(bp,c);
+               BIO_puts(bp,"\n");
+               }
+       BN_mul(d, a, a, ctx);
+       if (BN_cmp(c, d))
+               {
+               fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "
+                       "different results!\n");
+               goto err;
+               }
+       ret = 1;
+err:
+       if (a != NULL) BN_free(a);
+       if (c != NULL) BN_free(c);
+       if (d != NULL) BN_free(d);
+       if (e != NULL) BN_free(e);
+       return ret;
        }
 
 int test_mont(BIO *bp, BN_CTX *ctx)
@@ -905,6 +980,57 @@ int test_mod_exp(BIO *bp, BN_CTX *ctx)
                BN_bntest_rand(b,2+i,0,0); /**/
 
                if (!BN_mod_exp(d,a,b,c,ctx))
+                       return(0);
+
+               if (bp != NULL)
+                       {
+                       if (!results)
+                               {
+                               BN_print(bp,a);
+                               BIO_puts(bp," ^ ");
+                               BN_print(bp,b);
+                               BIO_puts(bp," % ");
+                               BN_print(bp,c);
+                               BIO_puts(bp," - ");
+                               }
+                       BN_print(bp,d);
+                       BIO_puts(bp,"\n");
+                       }
+               BN_exp(e,a,b,ctx);
+               BN_sub(e,e,d);
+               BN_div(a,b,e,c,ctx);
+               if(!BN_is_zero(b))
+                   {
+                   fprintf(stderr,"Modulo exponentiation test failed!\n");
+                   return 0;
+                   }
+               }
+       BN_free(a);
+       BN_free(b);
+       BN_free(c);
+       BN_free(d);
+       BN_free(e);
+       return(1);
+       }
+
+int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx)
+       {
+       BIGNUM *a,*b,*c,*d,*e;
+       int i;
+
+       a=BN_new();
+       b=BN_new();
+       c=BN_new();
+       d=BN_new();
+       e=BN_new();
+
+       BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */
+       for (i=0; i<num2; i++)
+               {
+               BN_bntest_rand(a,20+i*5,0,0); /**/
+               BN_bntest_rand(b,2+i,0,0); /**/
+
+               if (!BN_mod_exp_mont_consttime(d,a,b,c,ctx,NULL))
                        return(00);
 
                if (bp != NULL)
@@ -955,8 +1081,8 @@ int test_exp(BIO *bp, BN_CTX *ctx)
                BN_bntest_rand(a,20+i*5,0,0); /**/
                BN_bntest_rand(b,2+i,0,0); /**/
 
-               if (!BN_exp(d,a,b,ctx))
-                       return(00);
+               if (BN_exp(d,a,b,ctx) <= 0)
+                       return(0);
 
                if (bp != NULL)
                        {