]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
Add input check to rsa_decrypt family of functions.
authorNiels Möller <nisse@lysator.liu.se>
Tue, 8 Jun 2021 19:32:38 +0000 (21:32 +0200)
committerNiels Möller <nisse@lysator.liu.se>
Tue, 8 Jun 2021 19:32:38 +0000 (21:32 +0200)
(cherry picked from commit 0ad0b5df315665250dfdaa4a1e087f4799edaefe)

ChangeLog
rsa-decrypt-tr.c
rsa-decrypt.c
rsa-sec-decrypt.c
rsa.h
testsuite/rsa-encrypt-test.c

index e87ee81f66ee7fbb30799d7d1d42242d0adc2a62..eadfc36aef45093b6cd2bf26e09092c9f06041f2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,14 @@
-2021-05-14  Niels Möller  <nisse@lysator.liu.se>
+2021-05-17  Niels Möller  <nisse@lysator.liu.se>
 
        Bug fixes merged from from 3.7.3 release (starting from 2021-05-06).
+       * rsa-decrypt-tr.c (rsa_decrypt_tr): Check up-front that input is
+       in range.
+       * rsa-sec-decrypt.c (rsa_sec_decrypt): Likewise.
+       * rsa-decrypt.c (rsa_decrypt): Likewise.
+       * testsuite/rsa-encrypt-test.c (test_main): Add tests with input > n.
+
+2021-05-14  Niels Möller  <nisse@lysator.liu.se>
+
        * rsa-sign-tr.c (rsa_sec_blind): Delete mn argument.
        (_rsa_sec_compute_root_tr): Delete mn argument, instead require
        that input size matches key size. Rearrange use of temporary
index 927a8915746617984814ae1f3f1ff1c1a7d32467..4a9e9d74da51b30963d22e6d4a3023fc5a28ba99 100644 (file)
@@ -52,6 +52,10 @@ rsa_decrypt_tr(const struct rsa_public_key *pub,
   mp_size_t key_limb_size;
   int res;
 
+  /* First check that input is in range. */
+  if (mpz_sgn (gibberish) < 0 || mpz_cmp (gibberish, pub->n) >= 0)
+    return 0;
+
   key_limb_size = mpz_size(pub->n);
 
   TMP_GMP_ALLOC (m, key_limb_size);
index 7681439d2004eb2958d15ea66199dc6475e84ef2..540d8baa4068993f539ffa83778bd3f3a259e99d 100644 (file)
@@ -48,6 +48,16 @@ rsa_decrypt(const struct rsa_private_key *key,
   int res;
 
   mpz_init(m);
+
+  /* First check that input is in range. Since we don't have the
+     public key available here, we need to reconstruct n. */
+  mpz_mul (m, key->p, key->q);
+  if (mpz_sgn (gibberish) < 0 || mpz_cmp (gibberish, m) >= 0)
+    {
+      mpz_clear (m);
+      return 0;
+    }
+
   rsa_compute_root(key, m, gibberish);
 
   res = pkcs1_decrypt (key->size, m, length, message);
index fc4757a001957aff6e11096af9cfcfd09ebad1dd..4c98958dd52863dfc2682dfa60532bd4980f44d8 100644 (file)
@@ -55,6 +55,10 @@ rsa_sec_decrypt(const struct rsa_public_key *pub,
   TMP_GMP_DECL (em, uint8_t);
   int res;
 
+  /* First check that input is in range. */
+  if (mpz_sgn (gibberish) < 0 || mpz_cmp (gibberish, pub->n) >= 0)
+    return 0;
+
   TMP_GMP_ALLOC (m, mpz_size(pub->n));
   TMP_GMP_ALLOC (em, key->size);
 
diff --git a/rsa.h b/rsa.h
index 3b10155ffb92b40aa86f60a847eeda29b724d9a4..2dd35a2d34edb53b05635d5712fcf14557373fea 100644 (file)
--- a/rsa.h
+++ b/rsa.h
@@ -428,13 +428,14 @@ rsa_sec_decrypt(const struct rsa_public_key *pub,
                size_t length, uint8_t *message,
                const mpz_t gibberish);
 
-/* Compute x, the e:th root of m. Calling it with x == m is allowed. */
+/* Compute x, the e:th root of m. Calling it with x == m is allowed.
+   It is required that 0 <= m < n. */
 void
 rsa_compute_root(const struct rsa_private_key *key,
                 mpz_t x, const mpz_t m);
 
 /* Safer variant, using RSA blinding, and checking the result after
-   CRT. */
+   CRT. It is required that 0 <= m < n. */
 int
 rsa_compute_root_tr(const struct rsa_public_key *pub,
                    const struct rsa_private_key *key,
index d3bc374b9ce40a1d45fe5332d3e90a849418c60b..d1a440f695a2b947746779df5456e44e8f1851c8 100644 (file)
@@ -19,11 +19,12 @@ test_main(void)
   uint8_t after;
 
   mpz_t gibberish;
-  mpz_t zero;
+  mpz_t bad_input;
 
   rsa_private_key_init(&key);
   rsa_public_key_init(&pub);
   mpz_init(gibberish);
+  mpz_init(bad_input);
 
   knuth_lfib_init(&lfib, 17);
   
@@ -103,15 +104,40 @@ test_main(void)
   ASSERT(decrypted[0] == 'A');
 
   /* Test zero input. */
-  mpz_init_set_ui (zero, 0);
+  mpz_set_ui (bad_input, 0);
   decrypted_length = msg_length;
-  ASSERT(!rsa_decrypt(&key, &decrypted_length, decrypted, zero));
+  ASSERT(!rsa_decrypt(&key, &decrypted_length, decrypted, bad_input));
   ASSERT(!rsa_decrypt_tr(&pub, &key,
                         &lfib, (nettle_random_func *) knuth_lfib_random,
-                        &decrypted_length, decrypted, zero));
+                        &decrypted_length, decrypted, bad_input));
   ASSERT(!rsa_sec_decrypt(&pub, &key,
                          &lfib, (nettle_random_func *) knuth_lfib_random,
-                         decrypted_length, decrypted, zero));
+                         decrypted_length, decrypted, bad_input));
+  ASSERT(decrypted_length == msg_length);
+
+  /* Test input that is slightly larger than n */
+  mpz_add(bad_input, gibberish, pub.n);
+  decrypted_length = msg_length;
+  ASSERT(!rsa_decrypt(&key, &decrypted_length, decrypted, bad_input));
+  ASSERT(!rsa_decrypt_tr(&pub, &key,
+                        &lfib, (nettle_random_func *) knuth_lfib_random,
+                        &decrypted_length, decrypted, bad_input));
+  ASSERT(!rsa_sec_decrypt(&pub, &key,
+                         &lfib, (nettle_random_func *) knuth_lfib_random,
+                         decrypted_length, decrypted, bad_input));
+  ASSERT(decrypted_length == msg_length);
+
+  /* Test input that is considerably larger than n */
+  mpz_mul_2exp (bad_input, pub.n, 100);
+  mpz_add (bad_input, bad_input, gibberish);
+  decrypted_length = msg_length;
+  ASSERT(!rsa_decrypt(&key, &decrypted_length, decrypted, bad_input));
+  ASSERT(!rsa_decrypt_tr(&pub, &key,
+                        &lfib, (nettle_random_func *) knuth_lfib_random,
+                        &decrypted_length, decrypted, bad_input));
+  ASSERT(!rsa_sec_decrypt(&pub, &key,
+                         &lfib, (nettle_random_func *) knuth_lfib_random,
+                         decrypted_length, decrypted, bad_input));
   ASSERT(decrypted_length == msg_length);
 
   /* Test invalid key. */
@@ -124,6 +150,6 @@ test_main(void)
   rsa_private_key_clear(&key);
   rsa_public_key_clear(&pub);
   mpz_clear(gibberish);
-  mpz_clear(zero);
+  mpz_clear(bad_input);
   free(decrypted);
 }