Ensure that x**0 mod 1 = 0.
[openssl.git] / crypto / bn / exptest.c
index b6c7b18a3a82022e330ae1ec8e093ef233f23b5d..5fa02a1229e88c54d4f29c14eef2f166059b52da 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+
+#include "../e_os.h"
+
 #include <openssl/bio.h>
 #include <openssl/bn.h>
 #include <openssl/rand.h>
 #include <openssl/err.h>
-#ifdef WINDOWS
-#include "../bio/bss_file.c"
-#endif
 
 #define NUM_BITS       (BN_BITS*2)
 
 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
 
+/* test_exp_mod_zero tests that x**0 mod 1 == 0. It returns zero on success. */
+static int test_exp_mod_zero() {
+       BIGNUM a, p, m;
+       BIGNUM r;
+       BN_CTX *ctx = BN_CTX_new();
+       int ret = 1;
+
+       BN_init(&m);
+       BN_one(&m);
+
+       BN_init(&a);
+       BN_one(&a);
+
+       BN_init(&p);
+       BN_zero(&p);
+
+       BN_init(&r);
+       BN_mod_exp(&r, &a, &p, &m, ctx);
+       BN_CTX_free(ctx);
+
+       if (BN_is_zero(&r))
+               ret = 0;
+       else
+               {
+               printf("1**0 mod 1 = ");
+               BN_print_fp(stdout, &r);
+               printf(", should be 0\n");
+               }
+
+       BN_free(&r);
+       BN_free(&a);
+       BN_free(&p);
+       BN_free(&m);
+
+       return ret;
+}
+
 int main(int argc, char *argv[])
        {
        BN_CTX *ctx;
        BIO *out=NULL;
        int i,ret;
        unsigned char c;
-       BIGNUM *r_mont,*r_recp,*r_simple,*a,*b,*m;
+       BIGNUM *r_mont,*r_mont_const,*r_recp,*r_simple,*a,*b,*m;
 
        RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_rand may fail, and we don't
                                               * even check its return value
@@ -86,8 +123,9 @@ int main(int argc, char *argv[])
        ERR_load_BN_strings();
 
        ctx=BN_CTX_new();
-       if (ctx == NULL) exit(1);
+       if (ctx == NULL) EXIT(1);
        r_mont=BN_new();
+       r_mont_const=BN_new();
        r_recp=BN_new();
        r_simple=BN_new();
        a=BN_new();
@@ -99,7 +137,7 @@ int main(int argc, char *argv[])
 
        out=BIO_new(BIO_s_file());
 
-       if (out == NULL) exit(1);
+       if (out == NULL) EXIT(1);
        BIO_set_fp(out,stdout,BIO_NOCLOSE);
 
        for (i=0; i<200; i++)
@@ -124,7 +162,7 @@ int main(int argc, char *argv[])
                        {
                        printf("BN_mod_exp_mont() problems\n");
                        ERR_print_errors(out);
-                       exit(1);
+                       EXIT(1);
                        }
 
                ret=BN_mod_exp_recp(r_recp,a,b,m,ctx);
@@ -132,7 +170,7 @@ int main(int argc, char *argv[])
                        {
                        printf("BN_mod_exp_recp() problems\n");
                        ERR_print_errors(out);
-                       exit(1);
+                       EXIT(1);
                        }
 
                ret=BN_mod_exp_simple(r_simple,a,b,m,ctx);
@@ -140,11 +178,20 @@ int main(int argc, char *argv[])
                        {
                        printf("BN_mod_exp_simple() problems\n");
                        ERR_print_errors(out);
-                       exit(1);
+                       EXIT(1);
+                       }
+
+               ret=BN_mod_exp_mont_consttime(r_mont_const,a,b,m,ctx,NULL);
+               if (ret <= 0)
+                       {
+                       printf("BN_mod_exp_mont_consttime() problems\n");
+                       ERR_print_errors(out);
+                       EXIT(1);
                        }
 
                if (BN_cmp(r_simple, r_mont) == 0
-                   && BN_cmp(r_simple,r_recp) == 0)
+                   && BN_cmp(r_simple,r_recp) == 0
+                       && BN_cmp(r_simple,r_mont_const) == 0)
                        {
                        printf(".");
                        fflush(stdout);
@@ -153,6 +200,8 @@ int main(int argc, char *argv[])
                        {
                        if (BN_cmp(r_simple,r_mont) != 0)
                                printf("\nsimple and mont results differ\n");
+                       if (BN_cmp(r_simple,r_mont_const) != 0)
+                               printf("\nsimple and mont const time results differ\n");
                        if (BN_cmp(r_simple,r_recp) != 0)
                                printf("\nsimple and recp results differ\n");
 
@@ -162,25 +211,37 @@ int main(int argc, char *argv[])
                        printf("\nsimple   ="); BN_print(out,r_simple);
                        printf("\nrecp     ="); BN_print(out,r_recp);
                        printf("\nmont     ="); BN_print(out,r_mont);
+                       printf("\nmont_ct  ="); BN_print(out,r_mont_const);
                        printf("\n");
-                       exit(1);
+                       EXIT(1);
                        }
                }
        BN_free(r_mont);
+       BN_free(r_mont_const);
        BN_free(r_recp);
        BN_free(r_simple);
        BN_free(a);
        BN_free(b);
        BN_free(m);
        BN_CTX_free(ctx);
+       ERR_remove_thread_state(NULL);
        CRYPTO_mem_leaks(out);
        BIO_free(out);
-       printf(" done\n");
-       exit(0);
+       printf("\n");
+
+       if (test_exp_mod_zero() != 0)
+               goto err;
+
+       printf("done\n");
+
+       EXIT(0);
 err:
        ERR_load_crypto_strings();
        ERR_print_errors(out);
-       exit(1);
+#ifdef OPENSSL_SYS_NETWARE
+    printf("ERROR\n");
+#endif
+       EXIT(1);
        return(1);
        }