summaryrefslogtreecommitdiffstats
path: root/meta/recipes-support/nettle/nettle-3.5.1/CVE-2021-3580_1.patch
blob: ac3a638e72b1fd1dad74fcad8e26cdb7acb411be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
From cd6059aebdd3059fbcf674dddb850b821c13b6c2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se>
Date: Tue, 8 Jun 2021 21:31:39 +0200
Subject: [PATCH 1/2] Change _rsa_sec_compute_root_tr to take a fix input size.

Improves consistency with _rsa_sec_compute_root, and fixes zero-input bug.

(cherry picked from commit 485b5e2820a057e873b1ba812fdb39cae4adf98c)

Upstream-Status: Backport
CVE: CVE-2021-3580 dep#1
Signed-off-by: Armin Kuster <akuster@mvista.com>

---
 ChangeLog                    | 17 +++++++++-
 rsa-decrypt-tr.c             |  7 ++---
 rsa-internal.h               |  4 +--
 rsa-sec-decrypt.c            |  9 ++++--
 rsa-sign-tr.c                | 61 +++++++++++++++++-------------------
 testsuite/rsa-encrypt-test.c | 14 ++++++++-
 6 files changed, 69 insertions(+), 43 deletions(-)

Index: nettle-3.5.1/rsa-decrypt-tr.c
===================================================================
--- nettle-3.5.1.orig/rsa-decrypt-tr.c
+++ nettle-3.5.1/rsa-decrypt-tr.c
@@ -52,14 +52,13 @@ rsa_decrypt_tr(const struct rsa_public_k
   mp_size_t key_limb_size;
   int res;
 
-  key_limb_size = NETTLE_OCTET_SIZE_TO_LIMB_SIZE(key->size);
+  key_limb_size = mpz_size(pub->n);
 
   TMP_GMP_ALLOC (m, key_limb_size);
   TMP_GMP_ALLOC (em, key->size);
+  mpz_limbs_copy(m, gibberish, key_limb_size);
 
-  res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, m,
-				  mpz_limbs_read(gibberish),
-				  mpz_size(gibberish));
+  res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, m, m);
 
   mpn_get_base256 (em, key->size, m, key_limb_size);
 
Index: nettle-3.5.1/rsa-internal.h
===================================================================
--- nettle-3.5.1.orig/rsa-internal.h
+++ nettle-3.5.1/rsa-internal.h
@@ -78,11 +78,11 @@ _rsa_sec_compute_root(const struct rsa_p
                       mp_limb_t *scratch);
 
 /* Safe side-channel silent variant, using RSA blinding, and checking the
- * result after CRT. */
+ * result after CRT. In-place calls, with x == m, is allowed. */
 int
 _rsa_sec_compute_root_tr(const struct rsa_public_key *pub,
 			 const struct rsa_private_key *key,
 			 void *random_ctx, nettle_random_func *random,
-			 mp_limb_t *x, const mp_limb_t *m, size_t mn);
+			 mp_limb_t *x, const mp_limb_t *m);
 
 #endif /* NETTLE_RSA_INTERNAL_H_INCLUDED */
Index: nettle-3.5.1/rsa-sec-decrypt.c
===================================================================
--- nettle-3.5.1.orig/rsa-sec-decrypt.c
+++ nettle-3.5.1/rsa-sec-decrypt.c
@@ -58,9 +58,12 @@ rsa_sec_decrypt(const struct rsa_public_
   TMP_GMP_ALLOC (m, mpz_size(pub->n));
   TMP_GMP_ALLOC (em, key->size);
 
-  res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, m,
-				  mpz_limbs_read(gibberish),
-				  mpz_size(gibberish));
+  /* We need a copy because m can be shorter than key_size,
+   * but _rsa_sec_compute_root_tr expect all inputs to be
+   * normalized to a key_size long buffer length */
+  mpz_limbs_copy(m, gibberish, mpz_size(pub->n));
+
+  res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, m, m);
 
   mpn_get_base256 (em, key->size, m, mpz_size(pub->n));
 
Index: nettle-3.5.1/rsa-sign-tr.c
===================================================================
--- nettle-3.5.1.orig/rsa-sign-tr.c
+++ nettle-3.5.1/rsa-sign-tr.c
@@ -131,35 +131,34 @@ int
 _rsa_sec_compute_root_tr(const struct rsa_public_key *pub,
 			 const struct rsa_private_key *key,
 			 void *random_ctx, nettle_random_func *random,
-			 mp_limb_t *x, const mp_limb_t *m, size_t mn)
+			 mp_limb_t *x, const mp_limb_t *m)
 {
+  mp_size_t nn;
   mpz_t mz;
   mpz_t xz;
   int res;
 
-  mpz_init(mz);
   mpz_init(xz);
 
-  mpn_copyi(mpz_limbs_write(mz, mn), m, mn);
-  mpz_limbs_finish(mz, mn);
+  nn = mpz_size (pub->n);
 
-  res = rsa_compute_root_tr(pub, key, random_ctx, random, xz, mz);
+  res = rsa_compute_root_tr(pub, key, random_ctx, random, xz,
+			    mpz_roinit_n(mz, m, nn));
 
   if (res)
-    mpz_limbs_copy(x, xz, mpz_size(pub->n));
+    mpz_limbs_copy(x, xz, nn);
 
-  mpz_clear(mz);
   mpz_clear(xz);
   return res;
 }
 #else
 /* Blinds m, by computing c = m r^e (mod n), for a random r. Also
-   returns the inverse (ri), for use by rsa_unblind. */
+   returns the inverse (ri), for use by rsa_unblind. Must have c != m,
+   no in-place operation.*/
 static void
 rsa_sec_blind (const struct rsa_public_key *pub,
                void *random_ctx, nettle_random_func *random,
-               mp_limb_t *c, mp_limb_t *ri, const mp_limb_t *m,
-               mp_size_t mn)
+               mp_limb_t *c, mp_limb_t *ri, const mp_limb_t *m)
 {
   const mp_limb_t *ep = mpz_limbs_read (pub->e);
   const mp_limb_t *np = mpz_limbs_read (pub->n);
@@ -177,15 +176,15 @@ rsa_sec_blind (const struct rsa_public_k
 
   /* c = m*(r^e) mod n */
   itch = mpn_sec_powm_itch(nn, ebn, nn);
-  i2 = mpn_sec_mul_itch(nn, mn);
+  i2 = mpn_sec_mul_itch(nn, nn);
   itch = MAX(itch, i2);
-  i2 = mpn_sec_div_r_itch(nn + mn, nn);
+  i2 = mpn_sec_div_r_itch(2*nn, nn);
   itch = MAX(itch, i2);
   i2 = mpn_sec_invert_itch(nn);
   itch = MAX(itch, i2);
 
-  TMP_GMP_ALLOC (tp, nn + mn + itch);
-  scratch = tp + nn + mn;
+  TMP_GMP_ALLOC (tp, 2*nn  + itch);
+  scratch = tp + 2*nn;
 
   /* ri = r^(-1) */
   do
@@ -198,9 +197,8 @@ rsa_sec_blind (const struct rsa_public_k
   while (!mpn_sec_invert (ri, tp, np, nn, 2 * nn * GMP_NUMB_BITS, scratch));
 
   mpn_sec_powm (c, rp, nn, ep, ebn, np, nn, scratch);
-  /* normally mn == nn, but m can be smaller in some cases */
-  mpn_sec_mul (tp, c, nn, m, mn, scratch);
-  mpn_sec_div_r (tp, nn + mn, np, nn, scratch);
+  mpn_sec_mul (tp, c, nn, m, nn, scratch);
+  mpn_sec_div_r (tp, 2*nn, np, nn, scratch);
   mpn_copyi(c, tp, nn);
 
   TMP_GMP_FREE (r);
@@ -208,7 +206,7 @@ rsa_sec_blind (const struct rsa_public_k
   TMP_GMP_FREE (tp);
 }
 
-/* m = c ri mod n */
+/* m = c ri mod n. Allows x == c. */
 static void
 rsa_sec_unblind (const struct rsa_public_key *pub,
                  mp_limb_t *x, mp_limb_t *ri, const mp_limb_t *c)
@@ -299,7 +297,7 @@ int
 _rsa_sec_compute_root_tr(const struct rsa_public_key *pub,
 			 const struct rsa_private_key *key,
 			 void *random_ctx, nettle_random_func *random,
-			 mp_limb_t *x, const mp_limb_t *m, size_t mn)
+			 mp_limb_t *x, const mp_limb_t *m)
 {
   TMP_GMP_DECL (c, mp_limb_t);
   TMP_GMP_DECL (ri, mp_limb_t);
@@ -307,7 +305,7 @@ _rsa_sec_compute_root_tr(const struct rs
   size_t key_limb_size;
   int ret;
 
-  key_limb_size = NETTLE_OCTET_SIZE_TO_LIMB_SIZE(key->size);
+  key_limb_size = mpz_size(pub->n);
 
   /* mpz_powm_sec handles only odd moduli. If p, q or n is even, the
      key is invalid and rejected by rsa_private_key_prepare. However,
@@ -321,19 +319,18 @@ _rsa_sec_compute_root_tr(const struct rs
     }
 
   assert(mpz_size(pub->n) == key_limb_size);
-  assert(mn <= key_limb_size);
 
   TMP_GMP_ALLOC (c, key_limb_size);
   TMP_GMP_ALLOC (ri, key_limb_size);
   TMP_GMP_ALLOC (scratch, _rsa_sec_compute_root_itch(key));
 
-  rsa_sec_blind (pub, random_ctx, random, x, ri, m, mn);
+  rsa_sec_blind (pub, random_ctx, random, c, ri, m);
 
-  _rsa_sec_compute_root(key, c, x, scratch);
+  _rsa_sec_compute_root(key, x, c, scratch);
 
-  ret = rsa_sec_check_root(pub, c, x);
+  ret = rsa_sec_check_root(pub, x, c);
 
-  rsa_sec_unblind(pub, x, ri, c);
+  rsa_sec_unblind(pub, x, ri, x);
 
   cnd_mpn_zero(1 - ret, x, key_limb_size);
 
@@ -357,17 +354,17 @@ rsa_compute_root_tr(const struct rsa_pub
 		    mpz_t x, const mpz_t m)
 {
   TMP_GMP_DECL (l, mp_limb_t);
+  mp_size_t nn = mpz_size(pub->n);
   int res;
 
-  mp_size_t l_size = NETTLE_OCTET_SIZE_TO_LIMB_SIZE(key->size);
-  TMP_GMP_ALLOC (l, l_size);
+  TMP_GMP_ALLOC (l, nn);
+  mpz_limbs_copy(l, m, nn);
 
-  res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, l,
-				  mpz_limbs_read(m), mpz_size(m));
+  res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, l, l);
   if (res) {
-    mp_limb_t *xp = mpz_limbs_write (x, l_size);
-    mpn_copyi (xp, l, l_size);
-    mpz_limbs_finish (x, l_size);
+    mp_limb_t *xp = mpz_limbs_write (x, nn);
+    mpn_copyi (xp, l, nn);
+    mpz_limbs_finish (x, nn);
   }
 
   TMP_GMP_FREE (l);
Index: nettle-3.5.1/testsuite/rsa-encrypt-test.c
===================================================================
--- nettle-3.5.1.orig/testsuite/rsa-encrypt-test.c
+++ nettle-3.5.1/testsuite/rsa-encrypt-test.c
@@ -19,6 +19,7 @@ test_main(void)
   uint8_t after;
 
   mpz_t gibberish;
+  mpz_t zero;
 
   rsa_private_key_init(&key);
   rsa_public_key_init(&pub);
@@ -101,6 +102,17 @@ test_main(void)
   ASSERT(decrypted[decrypted_length] == after);
   ASSERT(decrypted[0] == 'A');
 
+  /* Test zero input. */
+  mpz_init_set_ui (zero, 0);
+  decrypted_length = msg_length;
+  ASSERT(!rsa_decrypt(&key, &decrypted_length, decrypted, zero));
+  ASSERT(!rsa_decrypt_tr(&pub, &key,
+			 &lfib, (nettle_random_func *) knuth_lfib_random,
+			 &decrypted_length, decrypted, zero));
+  ASSERT(!rsa_sec_decrypt(&pub, &key,
+			  &lfib, (nettle_random_func *) knuth_lfib_random,
+			  decrypted_length, decrypted, zero));
+  ASSERT(decrypted_length == msg_length);
 
   /* Test invalid key. */
   mpz_add_ui (key.q, key.q, 2);
@@ -112,6 +124,6 @@ test_main(void)
   rsa_private_key_clear(&key);
   rsa_public_key_clear(&pub);
   mpz_clear(gibberish);
+  mpz_clear(zero);
   free(decrypted);
 }
-