PR: 1835
[openssl.git] / crypto / bn / bntest.c
index 9169cc88138448248326c8c94b39214163f42d47..14990bc4ff10489925e76031ad8d3af6d63cc6db 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);
@@ -481,7 +486,7 @@ static void print_word(BIO *bp,BN_ULONG w)
                return;
                }
 #endif
-       BIO_printf(bp,"%lX",w);
+       BIO_printf(bp,BN_HEX_FMT1,w);
        }
 
 int test_div_word(BIO *bp)
@@ -727,6 +732,8 @@ int test_mont(BIO *bp, BN_CTX *ctx)
        BN_init(&n);
 
        mont=BN_MONT_CTX_new();
+       if (mont == NULL)
+               return 0;
 
        BN_bntest_rand(&a,100,0,0); /**/
        BN_bntest_rand(&b,100,0,0); /**/
@@ -921,6 +928,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)
@@ -972,7 +1030,7 @@ int test_exp(BIO *bp, BN_CTX *ctx)
                BN_bntest_rand(b,2+i,0,0); /**/
 
                if (!BN_exp(d,a,b,ctx))
-                       return(00);
+                       return(0);
 
                if (bp != NULL)
                        {
@@ -1060,8 +1118,8 @@ int test_gf2m_mod(BIO *bp)
        {
        BIGNUM *a,*b[2],*c,*d,*e;
        int i, j, ret = 0;
-       unsigned int p0[] = {163,7,6,3,0};
-       unsigned int p1[] = {193,15,0};
+       int p0[] = {163,7,6,3,0,-1};
+       int p1[] = {193,15,0,-1};
 
        a=BN_new();
        b[0]=BN_new();
@@ -1118,8 +1176,8 @@ int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx)
        {
        BIGNUM *a,*b[2],*c,*d,*e,*f,*g,*h;
        int i, j, ret = 0;
-       unsigned int p0[] = {163,7,6,3,0};
-       unsigned int p1[] = {193,15,0};
+       int p0[] = {163,7,6,3,0,-1};
+       int p1[] = {193,15,0,-1};
 
        a=BN_new();
        b[0]=BN_new();
@@ -1189,8 +1247,8 @@ int test_gf2m_mod_sqr(BIO *bp,BN_CTX *ctx)
        {
        BIGNUM *a,*b[2],*c,*d;
        int i, j, ret = 0;
-       unsigned int p0[] = {163,7,6,3,0};
-       unsigned int p1[] = {193,15,0};
+       int p0[] = {163,7,6,3,0,-1};
+       int p1[] = {193,15,0,-1};
 
        a=BN_new();
        b[0]=BN_new();
@@ -1248,8 +1306,8 @@ int test_gf2m_mod_inv(BIO *bp,BN_CTX *ctx)
        {
        BIGNUM *a,*b[2],*c,*d;
        int i, j, ret = 0;
-       unsigned int p0[] = {163,7,6,3,0};
-       unsigned int p1[] = {193,15,0};
+       int p0[] = {163,7,6,3,0,-1};
+       int p1[] = {193,15,0,-1};
 
        a=BN_new();
        b[0]=BN_new();
@@ -1303,8 +1361,8 @@ int test_gf2m_mod_div(BIO *bp,BN_CTX *ctx)
        {
        BIGNUM *a,*b[2],*c,*d,*e,*f;
        int i, j, ret = 0;
-       unsigned int p0[] = {163,7,6,3,0};
-       unsigned int p1[] = {193,15,0};
+       int p0[] = {163,7,6,3,0,-1};
+       int p1[] = {193,15,0,-1};
 
        a=BN_new();
        b[0]=BN_new();
@@ -1366,8 +1424,8 @@ int test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx)
        {
        BIGNUM *a,*b[2],*c,*d,*e,*f;
        int i, j, ret = 0;
-       unsigned int p0[] = {163,7,6,3,0};
-       unsigned int p1[] = {193,15,0};
+       int p0[] = {163,7,6,3,0,-1};
+       int p1[] = {193,15,0,-1};
 
        a=BN_new();
        b[0]=BN_new();
@@ -1437,8 +1495,8 @@ int test_gf2m_mod_sqrt(BIO *bp,BN_CTX *ctx)
        {
        BIGNUM *a,*b[2],*c,*d,*e,*f;
        int i, j, ret = 0;
-       unsigned int p0[] = {163,7,6,3,0};
-       unsigned int p1[] = {193,15,0};
+       int p0[] = {163,7,6,3,0,-1};
+       int p1[] = {193,15,0,-1};
 
        a=BN_new();
        b[0]=BN_new();
@@ -1496,8 +1554,8 @@ int test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx)
        {
        BIGNUM *a,*b[2],*c,*d,*e;
        int i, j, s = 0, t, ret = 0;
-       unsigned int p0[] = {163,7,6,3,0};
-       unsigned int p1[] = {193,15,0};
+       int p0[] = {163,7,6,3,0,-1};
+       int p1[] = {193,15,0,-1};
 
        a=BN_new();
        b[0]=BN_new();