Ensure that x**0 mod 1 = 0.
[openssl.git] / crypto / bn / exptest.c
index f598a07cf5c9919faacdaf095ba905ae338aa2b0..5fa02a1229e88c54d4f29c14eef2f166059b52da 100644 (file)
 
 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;
@@ -163,7 +200,7 @@ 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) != 0)
+                       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");
@@ -187,10 +224,16 @@ int main(int argc, char *argv[])
        BN_free(b);
        BN_free(m);
        BN_CTX_free(ctx);
-       ERR_remove_state(0);
+       ERR_remove_thread_state(NULL);
        CRYPTO_mem_leaks(out);
        BIO_free(out);
-       printf(" done\n");
+       printf("\n");
+
+       if (test_exp_mod_zero() != 0)
+               goto err;
+
+       printf("done\n");
+
        EXIT(0);
 err:
        ERR_load_crypto_strings();