Eliminate warning induced by http://cvs.openssl.org/chngview?cn=14690 and
[openssl.git] / crypto / bn / bntest.c
index 0d37dcff4d89b79ae9e00fa11f008faac7d442e0..b79685ee42318faf69b6c588fb16a546a9056ba2 100644 (file)
@@ -98,6 +98,7 @@ 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_div_word(BIO *bp);
 int test_div_recp(BIO *bp,BN_CTX *ctx);
 int test_mul(BIO *bp);
 int test_sqr(BIO *bp,BN_CTX *ctx);
@@ -105,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);
@@ -221,6 +223,10 @@ int main(int argc, char *argv[])
        if (!test_div(out,ctx)) goto err;
        BIO_flush(out);
 
+       message(out,"BN_div_word");
+       if (!test_div_word(out)) goto err;
+       BIO_flush(out);
+
        message(out,"BN_div_recp");
        if (!test_div_recp(out,ctx)) goto err;
        BIO_flush(out);
@@ -241,6 +247,10 @@ int main(int argc, char *argv[])
        if (!test_mod_exp(out,ctx)) goto err;
        BIO_flush(out);
 
+       message(out,"BN_mod_exp_mont_consttime");
+       if (!test_mod_exp_mont_consttime(out,ctx)) goto err;
+       BIO_flush(out);
+
        message(out,"BN_exp");
        if (!test_exp(out,ctx)) goto err;
        BIO_flush(out);
@@ -463,6 +473,78 @@ 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,BN_HEX_FMT1,w);
+       }
+
+int test_div_word(BIO *bp)
+       {
+       BIGNUM   a,b;
+       BN_ULONG r,s;
+       int i;
+
+       BN_init(&a);
+       BN_init(&b);
+
+       for (i=0; i<num0; i++)
+               {
+               do {
+                       BN_bntest_rand(&a,512,-1,0);
+                       BN_bntest_rand(&b,BN_BITS2,-1,0);
+                       s = b.d[0];
+               } while (!s);
+
+               BN_copy(&b, &a);
+               r = BN_div_word(&b, s);
+
+               if (bp != NULL)
+                       {
+                       if (!results)
+                               {
+                               BN_print(bp,&a);
+                               BIO_puts(bp," / ");
+                               print_word(bp,s);
+                               BIO_puts(bp," - ");
+                               }
+                       BN_print(bp,&b);
+                       BIO_puts(bp,"\n");
+
+                       if (!results)
+                               {
+                               BN_print(bp,&a);
+                               BIO_puts(bp," % ");
+                               print_word(bp,s);
+                               BIO_puts(bp," - ");
+                               }
+                       print_word(bp,r);
+                       BIO_puts(bp,"\n");
+                       }
+               BN_mul_word(&b,s);
+               BN_add_word(&b,r);
+               BN_sub(&b,&a,&b);
+               if(!BN_is_zero(&b))
+                   {
+                   fprintf(stderr,"Division (word) test failed!\n");
+                   return 0;
+                   }
+               }
+       BN_free(&a);
+       BN_free(&b);
+       return(1);
+       }
+
 int test_div_recp(BIO *bp, BN_CTX *ctx)
        {
        BIGNUM a,b,c,d,e;
@@ -877,6 +959,57 @@ int test_mod_exp(BIO *bp, BN_CTX *ctx)
        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)
+                       {
+                       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_exp(BIO *bp, BN_CTX *ctx)
        {
        BIGNUM *a,*b,*d,*e,*one;