Blame SOURCES/nettle-3.4.1-rsa-decrypt.patch

007cfe
From 5646ca77ee92de0ae33e7d2e0a3383c61a4091ed Mon Sep 17 00:00:00 2001
007cfe
From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se>
007cfe
Date: Thu, 6 May 2021 21:30:23 +0200
007cfe
Subject: [PATCH 1/4] Add check that message length to _pkcs1_sec_decrypt is
007cfe
 valid.
007cfe
007cfe
* pkcs1-sec-decrypt.c (_pkcs1_sec_decrypt): Check that message
007cfe
length is valid, for given key size.
007cfe
* testsuite/rsa-sec-decrypt-test.c (test_main): Add test cases for
007cfe
calls to rsa_sec_decrypt specifying a too large message length.
007cfe
007cfe
(cherry picked from commit 7616541e6eff73353bf682c62e3a68e4fe696707)
007cfe
---
007cfe
 ChangeLog                        |  8 ++++++++
007cfe
 pkcs1-sec-decrypt.c              |  4 +++-
007cfe
 testsuite/rsa-sec-decrypt-test.c | 17 ++++++++++++++++-
007cfe
 3 files changed, 27 insertions(+), 2 deletions(-)
007cfe
007cfe
diff --git a/ChangeLog b/ChangeLog
007cfe
index 4c7338a1..7cd0455e 100644
007cfe
--- a/ChangeLog
007cfe
+++ b/ChangeLog
007cfe
@@ -1,3 +1,11 @@
007cfe
+2021-05-06  Niels Möller  <nisse@lysator.liu.se>
007cfe
+
007cfe
+	Bug fixes merged from from 3.7.3 release (starting from 2021-05-06).
007cfe
+	* pkcs1-sec-decrypt.c (_pkcs1_sec_decrypt): Check that message
007cfe
+	length is valid, for given key size.
007cfe
+	* testsuite/rsa-sec-decrypt-test.c (test_main): Add test cases for
007cfe
+	calls to rsa_sec_decrypt specifying a too large message length.
007cfe
+
007cfe
 2018-12-04  Niels Möller  <nisse@lysator.liu.se>
007cfe
 
007cfe
 	* Released nettle-3.4.1.
007cfe
diff --git a/pkcs1-sec-decrypt.c b/pkcs1-sec-decrypt.c
007cfe
index 722044b0..02fd07e1 100644
007cfe
--- a/pkcs1-sec-decrypt.c
007cfe
+++ b/pkcs1-sec-decrypt.c
007cfe
@@ -64,7 +64,9 @@ _pkcs1_sec_decrypt (size_t length, uint8_t *message,
007cfe
   volatile int ok;
007cfe
   size_t i, t;
007cfe
 
007cfe
-  assert (padded_message_length >= length);
007cfe
+  /* Message independent branch */
007cfe
+  if (length + 11 > padded_message_length)
007cfe
+    return 0;
007cfe
 
007cfe
   t = padded_message_length - length - 1;
007cfe
 
007cfe
diff --git a/testsuite/rsa-sec-decrypt-test.c b/testsuite/rsa-sec-decrypt-test.c
007cfe
index 64f0b13c..4a9f301b 100644
007cfe
--- a/testsuite/rsa-sec-decrypt-test.c
007cfe
+++ b/testsuite/rsa-sec-decrypt-test.c
007cfe
@@ -55,6 +55,7 @@ rsa_decrypt_for_test(const struct rsa_public_key *pub,
007cfe
 #endif
007cfe
 
007cfe
 #define PAYLOAD_SIZE 50
007cfe
+#define DECRYPTED_SIZE 256
007cfe
 void
007cfe
 test_main(void)
007cfe
 {
007cfe
@@ -63,7 +64,7 @@ test_main(void)
007cfe
   struct knuth_lfib_ctx random_ctx;
007cfe
 
007cfe
   uint8_t plaintext[PAYLOAD_SIZE];
007cfe
-  uint8_t decrypted[PAYLOAD_SIZE];
007cfe
+  uint8_t decrypted[DECRYPTED_SIZE];
007cfe
   uint8_t verifybad[PAYLOAD_SIZE];
007cfe
   unsigned n_size = 1024;
007cfe
   mpz_t gibberish;
007cfe
@@ -98,6 +99,20 @@ test_main(void)
007cfe
                                     PAYLOAD_SIZE, decrypted, gibberish) == 1);
007cfe
       ASSERT (MEMEQ (PAYLOAD_SIZE, plaintext, decrypted));
007cfe
 
007cfe
+      ASSERT (pub.size > 10);
007cfe
+      ASSERT (pub.size <= DECRYPTED_SIZE);
007cfe
+
007cfe
+      /* Check that too large message length is rejected, largest
007cfe
+	 valid size is pub.size - 11. */
007cfe
+      ASSERT (!rsa_decrypt_for_test (&pub, &key, &random_ctx,
007cfe
+				     (nettle_random_func *) knuth_lfib_random,
007cfe
+				     pub.size - 10, decrypted, gibberish));
007cfe
+
007cfe
+      /* This case used to result in arithmetic underflow and a crash. */
007cfe
+      ASSERT (!rsa_decrypt_for_test (&pub, &key, &random_ctx,
007cfe
+				     (nettle_random_func *) knuth_lfib_random,
007cfe
+				     pub.size, decrypted, gibberish));
007cfe
+
007cfe
       /* bad one */
007cfe
       memcpy(decrypted, verifybad, PAYLOAD_SIZE);
007cfe
       nettle_mpz_random_size(garbage, &random_ctx,
007cfe
-- 
007cfe
2.31.1
007cfe
007cfe
007cfe
From 743cdf38353f6dd5d3d91eadc769106cfc116301 Mon Sep 17 00:00:00 2001
007cfe
From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se>
007cfe
Date: Tue, 8 Jun 2021 21:30:48 +0200
007cfe
Subject: [PATCH 2/4] Fix comment typos.
007cfe
007cfe
(cherry picked from commit 0a714543136de97c7fd34f1c6ac1592dc5036879)
007cfe
---
007cfe
 pkcs1-sec-decrypt.c | 4 ++--
007cfe
 1 file changed, 2 insertions(+), 2 deletions(-)
007cfe
007cfe
diff --git a/pkcs1-sec-decrypt.c b/pkcs1-sec-decrypt.c
007cfe
index 02fd07e1..a7f85c2e 100644
007cfe
--- a/pkcs1-sec-decrypt.c
007cfe
+++ b/pkcs1-sec-decrypt.c
007cfe
@@ -102,8 +102,8 @@ _pkcs1_sec_decrypt_variable(size_t *length, uint8_t *message,
007cfe
 
007cfe
   /* length is discovered in a side-channel silent way.
007cfe
    * not_found goes to 0 when the terminator is found.
007cfe
-   * offset strts at 3 as it includes the terminator and
007cfe
-   * the fomat bytes already */
007cfe
+   * offset starts at 3 as it includes the terminator and
007cfe
+   * the format bytes already */
007cfe
   offset = 3;
007cfe
   for (i = 2; i < padded_message_length; i++)
007cfe
     {
007cfe
-- 
007cfe
2.31.1
007cfe
007cfe
007cfe
From dfce46c4540d2abf040073070cff15f9d1708050 Mon Sep 17 00:00:00 2001
007cfe
From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se>
007cfe
Date: Tue, 8 Jun 2021 21:31:39 +0200
007cfe
Subject: [PATCH 3/4] Change _rsa_sec_compute_root_tr to take a fix input size.
007cfe
007cfe
Improves consistency with _rsa_sec_compute_root, and fixes zero-input bug.
007cfe
007cfe
(cherry picked from commit 485b5e2820a057e873b1ba812fdb39cae4adf98c)
007cfe
---
007cfe
 ChangeLog                    | 17 +++++++++-
007cfe
 rsa-decrypt-tr.c             |  7 ++---
007cfe
 rsa-internal.h               |  4 +--
007cfe
 rsa-sec-decrypt.c            |  9 ++++--
007cfe
 rsa-sign-tr.c                | 61 +++++++++++++++++-------------------
007cfe
 testsuite/rsa-encrypt-test.c | 14 ++++++++-
007cfe
 6 files changed, 69 insertions(+), 43 deletions(-)
007cfe
007cfe
diff --git a/ChangeLog b/ChangeLog
007cfe
index 7cd0455e..ae660fc0 100644
007cfe
--- a/ChangeLog
007cfe
+++ b/ChangeLog
007cfe
@@ -1,6 +1,21 @@
007cfe
-2021-05-06  Niels Möller  <nisse@lysator.liu.se>
007cfe
+2021-05-14  Niels Möller  <nisse@lysator.liu.se>
007cfe
 
007cfe
 	Bug fixes merged from from 3.7.3 release (starting from 2021-05-06).
007cfe
+	* rsa-sign-tr.c (rsa_sec_blind): Delete mn argument.
007cfe
+	(_rsa_sec_compute_root_tr): Delete mn argument, instead require
007cfe
+	that input size matches key size. Rearrange use of temporary
007cfe
+	storage, to support in-place operation, x == m. Update all
007cfe
+	callers.
007cfe
+
007cfe
+	* rsa-decrypt-tr.c (rsa_decrypt_tr): Make zero-padded copy of
007cfe
+	input, for calling _rsa_sec_compute_root_tr.
007cfe
+	* rsa-sec-decrypt.c (rsa_sec_decrypt): Likewise.
007cfe
+
007cfe
+	* testsuite/rsa-encrypt-test.c (test_main): Test calling all of
007cfe
+	rsa_decrypt, rsa_decrypt_tr, and rsa_sec_decrypt with zero input.
007cfe
+
007cfe
+2021-05-06  Niels Möller  <nisse@lysator.liu.se>
007cfe
+
007cfe
 	* pkcs1-sec-decrypt.c (_pkcs1_sec_decrypt): Check that message
007cfe
 	length is valid, for given key size.
007cfe
 	* testsuite/rsa-sec-decrypt-test.c (test_main): Add test cases for
007cfe
diff --git a/rsa-decrypt-tr.c b/rsa-decrypt-tr.c
007cfe
index 5dfb91b1..c118e852 100644
007cfe
--- a/rsa-decrypt-tr.c
007cfe
+++ b/rsa-decrypt-tr.c
007cfe
@@ -52,14 +52,13 @@ rsa_decrypt_tr(const struct rsa_public_key *pub,
007cfe
   mp_size_t key_limb_size;
007cfe
   int res;
007cfe
 
007cfe
-  key_limb_size = NETTLE_OCTET_SIZE_TO_LIMB_SIZE(key->size);
007cfe
+  key_limb_size = mpz_size(pub->n);
007cfe
 
007cfe
   TMP_GMP_ALLOC (m, key_limb_size);
007cfe
   TMP_GMP_ALLOC (em, key->size);
007cfe
+  mpz_limbs_copy(m, gibberish, key_limb_size);
007cfe
 
007cfe
-  res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, m,
007cfe
-				  mpz_limbs_read(gibberish),
007cfe
-				  mpz_size(gibberish));
007cfe
+  res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, m, m);
007cfe
 
007cfe
   mpn_get_base256 (em, key->size, m, key_limb_size);
007cfe
 
007cfe
diff --git a/rsa-internal.h b/rsa-internal.h
007cfe
index bd667bc2..64a7edf6 100644
007cfe
--- a/rsa-internal.h
007cfe
+++ b/rsa-internal.h
007cfe
@@ -53,12 +53,12 @@ _rsa_sec_compute_root(const struct rsa_private_key *key,
007cfe
                       mp_limb_t *scratch);
007cfe
 
007cfe
 /* Safe side-channel silent variant, using RSA blinding, and checking the
007cfe
- * result after CRT. */
007cfe
+ * result after CRT. In-place calls, with x == m, is allowed. */
007cfe
 int
007cfe
 _rsa_sec_compute_root_tr(const struct rsa_public_key *pub,
007cfe
 			 const struct rsa_private_key *key,
007cfe
 			 void *random_ctx, nettle_random_func *random,
007cfe
-			 mp_limb_t *x, const mp_limb_t *m, size_t mn);
007cfe
+			 mp_limb_t *x, const mp_limb_t *m);
007cfe
 
007cfe
 /* additional resistance to memory access side-channel attacks.
007cfe
  * Note: message buffer is returned unchanged on error */
007cfe
diff --git a/rsa-sec-decrypt.c b/rsa-sec-decrypt.c
007cfe
index e6a4b267..633a6852 100644
007cfe
--- a/rsa-sec-decrypt.c
007cfe
+++ b/rsa-sec-decrypt.c
007cfe
@@ -57,9 +57,12 @@ rsa_sec_decrypt(const struct rsa_public_key *pub,
007cfe
   TMP_GMP_ALLOC (m, mpz_size(pub->n));
007cfe
   TMP_GMP_ALLOC (em, key->size);
007cfe
 
007cfe
-  res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, m,
007cfe
-				  mpz_limbs_read(gibberish),
007cfe
-				  mpz_size(gibberish));
007cfe
+  /* We need a copy because m can be shorter than key_size,
007cfe
+   * but _rsa_sec_compute_root_tr expect all inputs to be
007cfe
+   * normalized to a key_size long buffer length */
007cfe
+  mpz_limbs_copy(m, gibberish, mpz_size(pub->n));
007cfe
+
007cfe
+  res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, m, m);
007cfe
 
007cfe
   mpn_get_base256 (em, key->size, m, mpz_size(pub->n));
007cfe
 
007cfe
diff --git a/rsa-sign-tr.c b/rsa-sign-tr.c
007cfe
index 59c9bd07..141a52c7 100644
007cfe
--- a/rsa-sign-tr.c
007cfe
+++ b/rsa-sign-tr.c
007cfe
@@ -131,35 +131,34 @@ int
007cfe
 _rsa_sec_compute_root_tr(const struct rsa_public_key *pub,
007cfe
 			 const struct rsa_private_key *key,
007cfe
 			 void *random_ctx, nettle_random_func *random,
007cfe
-			 mp_limb_t *x, const mp_limb_t *m, size_t mn)
007cfe
+			 mp_limb_t *x, const mp_limb_t *m)
007cfe
 {
007cfe
+  mp_size_t nn;
007cfe
   mpz_t mz;
007cfe
   mpz_t xz;
007cfe
   int res;
007cfe
 
007cfe
-  mpz_init(mz);
007cfe
   mpz_init(xz);
007cfe
 
007cfe
-  mpn_copyi(mpz_limbs_write(mz, mn), m, mn);
007cfe
-  mpz_limbs_finish(mz, mn);
007cfe
+  nn = mpz_size (pub->n);
007cfe
 
007cfe
-  res = rsa_compute_root_tr(pub, key, random_ctx, random, xz, mz);
007cfe
+  res = rsa_compute_root_tr(pub, key, random_ctx, random, xz,
007cfe
+			    mpz_roinit_n(mz, m, nn));
007cfe
 
007cfe
   if (res)
007cfe
-    mpz_limbs_copy(x, xz, mpz_size(pub->n));
007cfe
+    mpz_limbs_copy(x, xz, nn);
007cfe
 
007cfe
-  mpz_clear(mz);
007cfe
   mpz_clear(xz);
007cfe
   return res;
007cfe
 }
007cfe
 #else
007cfe
 /* Blinds m, by computing c = m r^e (mod n), for a random r. Also
007cfe
-   returns the inverse (ri), for use by rsa_unblind. */
007cfe
+   returns the inverse (ri), for use by rsa_unblind. Must have c != m,
007cfe
+   no in-place operation.*/
007cfe
 static void
007cfe
 rsa_sec_blind (const struct rsa_public_key *pub,
007cfe
                void *random_ctx, nettle_random_func *random,
007cfe
-               mp_limb_t *c, mp_limb_t *ri, const mp_limb_t *m,
007cfe
-               mp_size_t mn)
007cfe
+               mp_limb_t *c, mp_limb_t *ri, const mp_limb_t *m)
007cfe
 {
007cfe
   const mp_limb_t *ep = mpz_limbs_read (pub->e);
007cfe
   const mp_limb_t *np = mpz_limbs_read (pub->n);
007cfe
@@ -177,15 +176,15 @@ rsa_sec_blind (const struct rsa_public_key *pub,
007cfe
 
007cfe
   /* c = m*(r^e) mod n */
007cfe
   itch = mpn_sec_powm_itch(nn, ebn, nn);
007cfe
-  i2 = mpn_sec_mul_itch(nn, mn);
007cfe
+  i2 = mpn_sec_mul_itch(nn, nn);
007cfe
   itch = MAX(itch, i2);
007cfe
-  i2 = mpn_sec_div_r_itch(nn + mn, nn);
007cfe
+  i2 = mpn_sec_div_r_itch(2*nn, nn);
007cfe
   itch = MAX(itch, i2);
007cfe
   i2 = mpn_sec_invert_itch(nn);
007cfe
   itch = MAX(itch, i2);
007cfe
 
007cfe
-  TMP_GMP_ALLOC (tp, nn + mn + itch);
007cfe
-  scratch = tp + nn + mn;
007cfe
+  TMP_GMP_ALLOC (tp, 2*nn  + itch);
007cfe
+  scratch = tp + 2*nn;
007cfe
 
007cfe
   /* ri = r^(-1) */
007cfe
   do
007cfe
@@ -198,9 +197,8 @@ rsa_sec_blind (const struct rsa_public_key *pub,
007cfe
   while (!mpn_sec_invert (ri, tp, np, nn, 2 * nn * GMP_NUMB_BITS, scratch));
007cfe
 
007cfe
   mpn_sec_powm (c, rp, nn, ep, ebn, np, nn, scratch);
007cfe
-  /* normally mn == nn, but m can be smaller in some cases */
007cfe
-  mpn_sec_mul (tp, c, nn, m, mn, scratch);
007cfe
-  mpn_sec_div_r (tp, nn + mn, np, nn, scratch);
007cfe
+  mpn_sec_mul (tp, c, nn, m, nn, scratch);
007cfe
+  mpn_sec_div_r (tp, 2*nn, np, nn, scratch);
007cfe
   mpn_copyi(c, tp, nn);
007cfe
 
007cfe
   TMP_GMP_FREE (r);
007cfe
@@ -208,7 +206,7 @@ rsa_sec_blind (const struct rsa_public_key *pub,
007cfe
   TMP_GMP_FREE (tp);
007cfe
 }
007cfe
 
007cfe
-/* m = c ri mod n */
007cfe
+/* m = c ri mod n. Allows x == c. */
007cfe
 static void
007cfe
 rsa_sec_unblind (const struct rsa_public_key *pub,
007cfe
                  mp_limb_t *x, mp_limb_t *ri, const mp_limb_t *c)
007cfe
@@ -298,7 +296,7 @@ int
007cfe
 _rsa_sec_compute_root_tr(const struct rsa_public_key *pub,
007cfe
 			 const struct rsa_private_key *key,
007cfe
 			 void *random_ctx, nettle_random_func *random,
007cfe
-			 mp_limb_t *x, const mp_limb_t *m, size_t mn)
007cfe
+			 mp_limb_t *x, const mp_limb_t *m)
007cfe
 {
007cfe
   TMP_GMP_DECL (c, mp_limb_t);
007cfe
   TMP_GMP_DECL (ri, mp_limb_t);
007cfe
@@ -306,7 +304,7 @@ _rsa_sec_compute_root_tr(const struct rsa_public_key *pub,
007cfe
   size_t key_limb_size;
007cfe
   int ret;
007cfe
 
007cfe
-  key_limb_size = NETTLE_OCTET_SIZE_TO_LIMB_SIZE(key->size);
007cfe
+  key_limb_size = mpz_size(pub->n);
007cfe
 
007cfe
   /* mpz_powm_sec handles only odd moduli. If p, q or n is even, the
007cfe
      key is invalid and rejected by rsa_private_key_prepare. However,
007cfe
@@ -320,19 +318,18 @@ _rsa_sec_compute_root_tr(const struct rsa_public_key *pub,
007cfe
     }
007cfe
 
007cfe
   assert(mpz_size(pub->n) == key_limb_size);
007cfe
-  assert(mn <= key_limb_size);
007cfe
 
007cfe
   TMP_GMP_ALLOC (c, key_limb_size);
007cfe
   TMP_GMP_ALLOC (ri, key_limb_size);
007cfe
   TMP_GMP_ALLOC (scratch, _rsa_sec_compute_root_itch(key));
007cfe
 
007cfe
-  rsa_sec_blind (pub, random_ctx, random, x, ri, m, mn);
007cfe
+  rsa_sec_blind (pub, random_ctx, random, c, ri, m);
007cfe
 
007cfe
-  _rsa_sec_compute_root(key, c, x, scratch);
007cfe
+  _rsa_sec_compute_root(key, x, c, scratch);
007cfe
 
007cfe
-  ret = rsa_sec_check_root(pub, c, x);
007cfe
+  ret = rsa_sec_check_root(pub, x, c);
007cfe
 
007cfe
-  rsa_sec_unblind(pub, x, ri, c);
007cfe
+  rsa_sec_unblind(pub, x, ri, x);
007cfe
 
007cfe
   cnd_mpn_zero(1 - ret, x, key_limb_size);
007cfe
 
007cfe
@@ -356,17 +353,17 @@ rsa_compute_root_tr(const struct rsa_public_key *pub,
007cfe
 		    mpz_t x, const mpz_t m)
007cfe
 {
007cfe
   TMP_GMP_DECL (l, mp_limb_t);
007cfe
+  mp_size_t nn = mpz_size(pub->n);
007cfe
   int res;
007cfe
 
007cfe
-  mp_size_t l_size = NETTLE_OCTET_SIZE_TO_LIMB_SIZE(key->size);
007cfe
-  TMP_GMP_ALLOC (l, l_size);
007cfe
+  TMP_GMP_ALLOC (l, nn);
007cfe
+  mpz_limbs_copy(l, m, nn);
007cfe
 
007cfe
-  res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, l,
007cfe
-				  mpz_limbs_read(m), mpz_size(m));
007cfe
+  res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, l, l);
007cfe
   if (res) {
007cfe
-    mp_limb_t *xp = mpz_limbs_write (x, l_size);
007cfe
-    mpn_copyi (xp, l, l_size);
007cfe
-    mpz_limbs_finish (x, l_size);
007cfe
+    mp_limb_t *xp = mpz_limbs_write (x, nn);
007cfe
+    mpn_copyi (xp, l, nn);
007cfe
+    mpz_limbs_finish (x, nn);
007cfe
   }
007cfe
 
007cfe
   TMP_GMP_FREE (l);
007cfe
diff --git a/testsuite/rsa-encrypt-test.c b/testsuite/rsa-encrypt-test.c
007cfe
index 87525f78..d3bc374b 100644
007cfe
--- a/testsuite/rsa-encrypt-test.c
007cfe
+++ b/testsuite/rsa-encrypt-test.c
007cfe
@@ -19,6 +19,7 @@ test_main(void)
007cfe
   uint8_t after;
007cfe
 
007cfe
   mpz_t gibberish;
007cfe
+  mpz_t zero;
007cfe
 
007cfe
   rsa_private_key_init(&key);
007cfe
   rsa_public_key_init(&pub;;
007cfe
@@ -101,6 +102,17 @@ test_main(void)
007cfe
   ASSERT(decrypted[decrypted_length] == after);
007cfe
   ASSERT(decrypted[0] == 'A');
007cfe
 
007cfe
+  /* Test zero input. */
007cfe
+  mpz_init_set_ui (zero, 0);
007cfe
+  decrypted_length = msg_length;
007cfe
+  ASSERT(!rsa_decrypt(&key, &decrypted_length, decrypted, zero));
007cfe
+  ASSERT(!rsa_decrypt_tr(&pub, &key,
007cfe
+			 &lfib, (nettle_random_func *) knuth_lfib_random,
007cfe
+			 &decrypted_length, decrypted, zero));
007cfe
+  ASSERT(!rsa_sec_decrypt(&pub, &key,
007cfe
+			  &lfib, (nettle_random_func *) knuth_lfib_random,
007cfe
+			  decrypted_length, decrypted, zero));
007cfe
+  ASSERT(decrypted_length == msg_length);
007cfe
 
007cfe
   /* Test invalid key. */
007cfe
   mpz_add_ui (key.q, key.q, 2);
007cfe
@@ -112,6 +124,6 @@ test_main(void)
007cfe
   rsa_private_key_clear(&key);
007cfe
   rsa_public_key_clear(&pub;;
007cfe
   mpz_clear(gibberish);
007cfe
+  mpz_clear(zero);
007cfe
   free(decrypted);
007cfe
 }
007cfe
-  
007cfe
-- 
007cfe
2.31.1
007cfe
007cfe
007cfe
From f601611b3c315aba373c0ab2ddf24772e88c1b3e Mon Sep 17 00:00:00 2001
007cfe
From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se>
007cfe
Date: Tue, 8 Jun 2021 21:32:38 +0200
007cfe
Subject: [PATCH 4/4] Add input check to rsa_decrypt family of functions.
007cfe
007cfe
(cherry picked from commit 0ad0b5df315665250dfdaa4a1e087f4799edaefe)
007cfe
---
007cfe
 ChangeLog                    | 10 +++++++++-
007cfe
 rsa-decrypt-tr.c             |  4 ++++
007cfe
 rsa-decrypt.c                | 10 ++++++++++
007cfe
 rsa-sec-decrypt.c            |  4 ++++
007cfe
 rsa.h                        |  5 +++--
007cfe
 testsuite/rsa-encrypt-test.c | 38 ++++++++++++++++++++++++++++++------
007cfe
 6 files changed, 62 insertions(+), 9 deletions(-)
007cfe
007cfe
diff --git a/ChangeLog b/ChangeLog
007cfe
index ae660fc0..27f022db 100644
007cfe
--- a/ChangeLog
007cfe
+++ b/ChangeLog
007cfe
@@ -1,6 +1,14 @@
007cfe
-2021-05-14  Niels Möller  <nisse@lysator.liu.se>
007cfe
+2021-05-17  Niels Möller  <nisse@lysator.liu.se>
007cfe
 
007cfe
 	Bug fixes merged from from 3.7.3 release (starting from 2021-05-06).
007cfe
+	* rsa-decrypt-tr.c (rsa_decrypt_tr): Check up-front that input is
007cfe
+	in range.
007cfe
+	* rsa-sec-decrypt.c (rsa_sec_decrypt): Likewise.
007cfe
+	* rsa-decrypt.c (rsa_decrypt): Likewise.
007cfe
+	* testsuite/rsa-encrypt-test.c (test_main): Add tests with input > n.
007cfe
+
007cfe
+2021-05-14  Niels Möller  <nisse@lysator.liu.se>
007cfe
+
007cfe
 	* rsa-sign-tr.c (rsa_sec_blind): Delete mn argument.
007cfe
 	(_rsa_sec_compute_root_tr): Delete mn argument, instead require
007cfe
 	that input size matches key size. Rearrange use of temporary
007cfe
diff --git a/rsa-decrypt-tr.c b/rsa-decrypt-tr.c
007cfe
index c118e852..1ba3d286 100644
007cfe
--- a/rsa-decrypt-tr.c
007cfe
+++ b/rsa-decrypt-tr.c
007cfe
@@ -52,6 +52,10 @@ rsa_decrypt_tr(const struct rsa_public_key *pub,
007cfe
   mp_size_t key_limb_size;
007cfe
   int res;
007cfe
 
007cfe
+  /* First check that input is in range. */
007cfe
+  if (mpz_sgn (gibberish) < 0 || mpz_cmp (gibberish, pub->n) >= 0)
007cfe
+    return 0;
007cfe
+
007cfe
   key_limb_size = mpz_size(pub->n);
007cfe
 
007cfe
   TMP_GMP_ALLOC (m, key_limb_size);
007cfe
diff --git a/rsa-decrypt.c b/rsa-decrypt.c
007cfe
index 7681439d..540d8baa 100644
007cfe
--- a/rsa-decrypt.c
007cfe
+++ b/rsa-decrypt.c
007cfe
@@ -48,6 +48,16 @@ rsa_decrypt(const struct rsa_private_key *key,
007cfe
   int res;
007cfe
 
007cfe
   mpz_init(m);
007cfe
+
007cfe
+  /* First check that input is in range. Since we don't have the
007cfe
+     public key available here, we need to reconstruct n. */
007cfe
+  mpz_mul (m, key->p, key->q);
007cfe
+  if (mpz_sgn (gibberish) < 0 || mpz_cmp (gibberish, m) >= 0)
007cfe
+    {
007cfe
+      mpz_clear (m);
007cfe
+      return 0;
007cfe
+    }
007cfe
+
007cfe
   rsa_compute_root(key, m, gibberish);
007cfe
 
007cfe
   res = pkcs1_decrypt (key->size, m, length, message);
007cfe
diff --git a/rsa-sec-decrypt.c b/rsa-sec-decrypt.c
007cfe
index 633a6852..53113c69 100644
007cfe
--- a/rsa-sec-decrypt.c
007cfe
+++ b/rsa-sec-decrypt.c
007cfe
@@ -54,6 +54,10 @@ rsa_sec_decrypt(const struct rsa_public_key *pub,
007cfe
   TMP_GMP_DECL (em, uint8_t);
007cfe
   int res;
007cfe
 
007cfe
+  /* First check that input is in range. */
007cfe
+  if (mpz_sgn (gibberish) < 0 || mpz_cmp (gibberish, pub->n) >= 0)
007cfe
+    return 0;
007cfe
+
007cfe
   TMP_GMP_ALLOC (m, mpz_size(pub->n));
007cfe
   TMP_GMP_ALLOC (em, key->size);
007cfe
 
007cfe
diff --git a/rsa.h b/rsa.h
007cfe
index 0aac6a26..54c35688 100644
007cfe
--- a/rsa.h
007cfe
+++ b/rsa.h
007cfe
@@ -433,13 +433,14 @@ rsa_sec_decrypt(const struct rsa_public_key *pub,
007cfe
 	        size_t length, uint8_t *message,
007cfe
 	        const mpz_t gibberish);
007cfe
 
007cfe
-/* Compute x, the e:th root of m. Calling it with x == m is allowed. */
007cfe
+/* Compute x, the e:th root of m. Calling it with x == m is allowed.
007cfe
+   It is required that 0 <= m < n. */
007cfe
 void
007cfe
 rsa_compute_root(const struct rsa_private_key *key,
007cfe
 		 mpz_t x, const mpz_t m);
007cfe
 
007cfe
 /* Safer variant, using RSA blinding, and checking the result after
007cfe
-   CRT. */
007cfe
+   CRT. It is required that 0 <= m < n. */
007cfe
 int
007cfe
 rsa_compute_root_tr(const struct rsa_public_key *pub,
007cfe
 		    const struct rsa_private_key *key,
007cfe
diff --git a/testsuite/rsa-encrypt-test.c b/testsuite/rsa-encrypt-test.c
007cfe
index d3bc374b..d1a440f6 100644
007cfe
--- a/testsuite/rsa-encrypt-test.c
007cfe
+++ b/testsuite/rsa-encrypt-test.c
007cfe
@@ -19,11 +19,12 @@ test_main(void)
007cfe
   uint8_t after;
007cfe
 
007cfe
   mpz_t gibberish;
007cfe
-  mpz_t zero;
007cfe
+  mpz_t bad_input;
007cfe
 
007cfe
   rsa_private_key_init(&key);
007cfe
   rsa_public_key_init(&pub;;
007cfe
   mpz_init(gibberish);
007cfe
+  mpz_init(bad_input);
007cfe
 
007cfe
   knuth_lfib_init(&lfib, 17);
007cfe
   
007cfe
@@ -103,15 +104,40 @@ test_main(void)
007cfe
   ASSERT(decrypted[0] == 'A');
007cfe
 
007cfe
   /* Test zero input. */
007cfe
-  mpz_init_set_ui (zero, 0);
007cfe
+  mpz_set_ui (bad_input, 0);
007cfe
   decrypted_length = msg_length;
007cfe
-  ASSERT(!rsa_decrypt(&key, &decrypted_length, decrypted, zero));
007cfe
+  ASSERT(!rsa_decrypt(&key, &decrypted_length, decrypted, bad_input));
007cfe
   ASSERT(!rsa_decrypt_tr(&pub, &key,
007cfe
 			 &lfib, (nettle_random_func *) knuth_lfib_random,
007cfe
-			 &decrypted_length, decrypted, zero));
007cfe
+			 &decrypted_length, decrypted, bad_input));
007cfe
   ASSERT(!rsa_sec_decrypt(&pub, &key,
007cfe
 			  &lfib, (nettle_random_func *) knuth_lfib_random,
007cfe
-			  decrypted_length, decrypted, zero));
007cfe
+			  decrypted_length, decrypted, bad_input));
007cfe
+  ASSERT(decrypted_length == msg_length);
007cfe
+
007cfe
+  /* Test input that is slightly larger than n */
007cfe
+  mpz_add(bad_input, gibberish, pub.n);
007cfe
+  decrypted_length = msg_length;
007cfe
+  ASSERT(!rsa_decrypt(&key, &decrypted_length, decrypted, bad_input));
007cfe
+  ASSERT(!rsa_decrypt_tr(&pub, &key,
007cfe
+			 &lfib, (nettle_random_func *) knuth_lfib_random,
007cfe
+			 &decrypted_length, decrypted, bad_input));
007cfe
+  ASSERT(!rsa_sec_decrypt(&pub, &key,
007cfe
+			  &lfib, (nettle_random_func *) knuth_lfib_random,
007cfe
+			  decrypted_length, decrypted, bad_input));
007cfe
+  ASSERT(decrypted_length == msg_length);
007cfe
+
007cfe
+  /* Test input that is considerably larger than n */
007cfe
+  mpz_mul_2exp (bad_input, pub.n, 100);
007cfe
+  mpz_add (bad_input, bad_input, gibberish);
007cfe
+  decrypted_length = msg_length;
007cfe
+  ASSERT(!rsa_decrypt(&key, &decrypted_length, decrypted, bad_input));
007cfe
+  ASSERT(!rsa_decrypt_tr(&pub, &key,
007cfe
+			 &lfib, (nettle_random_func *) knuth_lfib_random,
007cfe
+			 &decrypted_length, decrypted, bad_input));
007cfe
+  ASSERT(!rsa_sec_decrypt(&pub, &key,
007cfe
+			  &lfib, (nettle_random_func *) knuth_lfib_random,
007cfe
+			  decrypted_length, decrypted, bad_input));
007cfe
   ASSERT(decrypted_length == msg_length);
007cfe
 
007cfe
   /* Test invalid key. */
007cfe
@@ -124,6 +150,6 @@ test_main(void)
007cfe
   rsa_private_key_clear(&key);
007cfe
   rsa_public_key_clear(&pub;;
007cfe
   mpz_clear(gibberish);
007cfe
-  mpz_clear(zero);
007cfe
+  mpz_clear(bad_input);
007cfe
   free(decrypted);
007cfe
 }
007cfe
-- 
007cfe
2.31.1
007cfe