Fix for CVE-2014-3570 (with minor bn_asm.c revamp).
[openssl.git] / crypto / bn / bntest.c
index 14990bc4ff10489925e76031ad8d3af6d63cc6db..869ae05a901d51ee69ad4d181337ba1d8c34e7a4 100644 (file)
  *
  */
 
-/* Until the key-gen callbacks are modified to use newer prototypes, we allow
- * deprecated functions for openssl-internal code */
-#ifdef OPENSSL_NO_DEPRECATED
-#undef OPENSSL_NO_DEPRECATED
-#endif
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -87,6 +81,8 @@
 #include <openssl/x509.h>
 #include <openssl/err.h>
 
+#include "../crypto/bn/bn_lcl.h"
+
 const int num0 = 100; /* number of tests */
 const int num1 = 50;  /* additional tests for some functions */
 const int num2 = 5;   /* number of tests for slow functions */
@@ -107,6 +103,7 @@ 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_mod_exp_mont5(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);
@@ -119,6 +116,8 @@ int test_gf2m_mod_sqrt(BIO *bp,BN_CTX *ctx);
 int test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx);
 int test_kron(BIO *bp,BN_CTX *ctx);
 int test_sqrt(BIO *bp,BN_CTX *ctx);
+int test_small_prime(BIO *bp,BN_CTX *ctx);
+int test_probable_prime_coprime(BIO *bp,BN_CTX *ctx);
 int rand_neg(void);
 static int results=0;
 
@@ -249,6 +248,7 @@ int main(int argc, char *argv[])
 
        message(out,"BN_mod_exp_mont_consttime");
        if (!test_mod_exp_mont_consttime(out,ctx)) goto err;
+       if (!test_mod_exp_mont5(out,ctx)) goto err;
        (void)BIO_flush(out);
 
        message(out,"BN_exp");
@@ -263,6 +263,19 @@ int main(int argc, char *argv[])
        if (!test_sqrt(out,ctx)) goto err;
        (void)BIO_flush(out);
 
+       message(out,"Small prime generation");
+       if (!test_small_prime(out,ctx)) goto err;
+       (void)BIO_flush(out);
+
+#ifdef OPENSSL_SYS_WIN32
+       message(out,"Probable prime generation with coprimes disabled");
+#else
+       message(out,"Probable prime generation with coprimes");
+       if (!test_probable_prime_coprime(out,ctx)) goto err;
+#endif
+       (void)BIO_flush(out);
+
+#ifndef OPENSSL_NO_EC2M
        message(out,"BN_GF2m_add");
        if (!test_gf2m_add(out)) goto err;
        (void)BIO_flush(out);
@@ -298,7 +311,7 @@ int main(int argc, char *argv[])
        message(out,"BN_GF2m_mod_solve_quad");
        if (!test_gf2m_mod_solve_quad(out,ctx)) goto err;
        (void)BIO_flush(out);
-
+#endif
        BN_CTX_free(ctx);
        BIO_free(out);
 
@@ -676,44 +689,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)
@@ -1012,6 +1079,80 @@ int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx)
        return(1);
        }
 
+/* Test constant-time modular exponentiation with 1024-bit inputs,
+ * which on x86_64 cause a different code branch to be taken.
+ */
+int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx)
+       {
+       BIGNUM *a,*p,*m,*d,*e;
+
+       BN_MONT_CTX *mont;
+
+       a=BN_new();
+       p=BN_new();
+       m=BN_new();
+       d=BN_new();
+       e=BN_new();
+
+       mont = BN_MONT_CTX_new();
+
+       BN_bntest_rand(m,1024,0,1); /* must be odd for montgomery */
+       /* Zero exponent */
+       BN_bntest_rand(a,1024,0,0);
+       BN_zero(p);
+       if(!BN_mod_exp_mont_consttime(d,a,p,m,ctx,NULL))
+               return 0;
+       if(!BN_is_one(d))
+               {
+               fprintf(stderr, "Modular exponentiation test failed!\n");
+               return 0;
+               }
+       /* Zero input */
+       BN_bntest_rand(p,1024,0,0);
+       BN_zero(a);
+       if(!BN_mod_exp_mont_consttime(d,a,p,m,ctx,NULL))
+               return 0;
+       if(!BN_is_zero(d))
+               {
+               fprintf(stderr, "Modular exponentiation test failed!\n");
+               return 0;
+               }
+       /* Craft an input whose Montgomery representation is 1,
+        * i.e., shorter than the modulus m, in order to test
+        * the const time precomputation scattering/gathering.
+        */
+       BN_one(a);
+       BN_MONT_CTX_set(mont,m,ctx);
+       if(!BN_from_montgomery(e,a,mont,ctx))
+               return 0;
+       if(!BN_mod_exp_mont_consttime(d,e,p,m,ctx,NULL))
+               return 0;
+       if(!BN_mod_exp_simple(a,e,p,m,ctx))
+               return 0;
+       if(BN_cmp(a,d) != 0)
+               {
+               fprintf(stderr,"Modular exponentiation test failed!\n");
+               return 0;
+               }
+       /* Finally, some regular test vectors. */
+       BN_bntest_rand(e,1024,0,0);
+       if(!BN_mod_exp_mont_consttime(d,e,p,m,ctx,NULL))
+               return 0;
+       if(!BN_mod_exp_simple(a,e,p,m,ctx))
+               return 0;
+       if(BN_cmp(a,d) != 0)
+               {
+               fprintf(stderr,"Modular exponentiation test failed!\n");
+               return 0;
+               }
+       BN_free(a);
+       BN_free(p);
+       BN_free(m);
+       BN_free(d);
+       BN_free(e);
+       return(1);
+       }
+
 int test_exp(BIO *bp, BN_CTX *ctx)
        {
        BIGNUM *a,*b,*d,*e,*one;
@@ -1029,7 +1170,7 @@ 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))
+               if (BN_exp(d,a,b,ctx) <= 0)
                        return(0);
 
                if (bp != NULL)
@@ -1061,7 +1202,7 @@ int test_exp(BIO *bp, BN_CTX *ctx)
        BN_free(one);
        return(1);
        }
-
+#ifndef OPENSSL_NO_EC2M
 int test_gf2m_add(BIO *bp)
        {
        BIGNUM a,b,c;
@@ -1636,7 +1777,7 @@ int test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx)
        BN_free(e);
        return ret;
        }
-
+#endif
 static int genprime_cb(int p, int n, BN_GENCB *arg)
        {
        char c='*';
@@ -1819,6 +1960,59 @@ int test_sqrt(BIO *bp, BN_CTX *ctx)
        return ret;
        }
 
+int test_small_prime(BIO *bp,BN_CTX *ctx)
+       {
+       static const int bits = 10;
+       int ret = 0;
+       BIGNUM r;
+
+       BN_init(&r);
+       if (!BN_generate_prime_ex(&r, bits, 0, NULL, NULL, NULL))
+               goto err;
+       if (BN_num_bits(&r) != bits)
+               {
+               BIO_printf(bp, "Expected %d bit prime, got %d bit number\n", bits, BN_num_bits(&r));
+               goto err;
+               }
+
+       ret = 1;
+
+err:
+       BN_clear(&r);
+       return ret;
+       }
+#ifndef OPENSSL_SYS_WIN32
+int test_probable_prime_coprime(BIO *bp, BN_CTX *ctx)
+       {
+       int i, j, ret = 0;
+       BIGNUM r;
+       BN_ULONG primes[5] = { 2, 3, 5, 7, 11 };
+
+       BN_init(&r);
+       
+       for (i = 0; i < 1000; i++)
+               {
+               if (!bn_probable_prime_dh_coprime(&r, 1024, ctx)) goto err;
+               
+               for (j = 0; j < 5; j++)
+                       {
+                       if (BN_mod_word(&r, primes[j]) == 0)
+                               {
+                               BIO_printf(bp, "Number generated is not coprime to %ld:\n", primes[j]);
+                               BN_print_fp(stdout, &r);
+                               BIO_printf(bp, "\n");
+                               goto err;
+                               }
+                       }
+               }
+
+       ret = 1;
+
+err:
+       BN_clear(&r);
+       return ret;
+       }
+#endif
 int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_)
        {
        BIGNUM *a,*b,*c,*d;