summaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authorAdam Langley <agl@chromium.org>2013-04-23 12:13:51 -0400
committerEmilia Kasper <emilia@openssl.org>2014-09-04 16:07:39 +0200
commit45d129511ff0b43be9a4271133c9ee22658ff07e (patch)
treed0fed3d841527f5d8f1949346f552c3ceb81a848 /crypto
parent0976adac8fb4e7d336b0edcd24d1bcd3e62abcf3 (diff)
Ensure that x**0 mod 1 = 0.
(cherry picked from commit 2b0180c37fa6ffc48ee40caa831ca398b828e680) Reviewed-by: Ben Laurie <ben@openssl.org>
Diffstat (limited to 'crypto')
-rw-r--r--crypto/bn/bn_exp.c9
-rw-r--r--crypto/bn/exptest.c45
2 files changed, 52 insertions, 2 deletions
diff --git a/crypto/bn/bn_exp.c b/crypto/bn/bn_exp.c
index d9b6c737fc..2e1e4e7fd8 100644
--- a/crypto/bn/bn_exp.c
+++ b/crypto/bn/bn_exp.c
@@ -767,7 +767,14 @@ int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,
bits = BN_num_bits(p);
if (bits == 0)
{
- ret = BN_one(rr);
+ /* x**0 mod 1 is still zero. */
+ if (BN_is_one(m))
+ {
+ ret = 1;
+ BN_zero(rr);
+ }
+ else
+ ret = BN_one(rr);
return ret;
}
if (a == 0)
diff --git a/crypto/bn/exptest.c b/crypto/bn/exptest.c
index f598a07cf5..44a90e2c84 100644
--- a/crypto/bn/exptest.c
+++ b/crypto/bn/exptest.c
@@ -71,6 +71,43 @@
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;
@@ -190,7 +227,13 @@ int main(int argc, char *argv[])
ERR_remove_state(0);
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();