Remove redundant OPENSSL_NO_DEPRECATED suppression
[openssl.git] / crypto / bn / bntest.c
index 06f5954acc39083ea56981b6ccb8730d64ff5026..0fa2504cebc165d58fbb24e11f54e65d535134b2 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");
@@ -262,6 +262,19 @@ int main(int argc, char *argv[])
        message(out,"BN_mod_sqrt");
        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;
@@ -1012,6 +1025,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;
@@ -1819,6 +1906,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;