p≡p for Java
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

521 lines
18 KiB

  1. diff --git a/nettle/ChangeLog b/nettle/ChangeLog
  2. index bb169e86..4787cff6 100644
  3. --- a/nettle/ChangeLog
  4. +++ b/nettle/ChangeLog
  5. @@ -1,3 +1,39 @@
  6. +2021-05-22 Niels Möller <nisse@lysator.liu.se>
  7. +
  8. + * configure.ac: Bump package version, to 3.7.3.
  9. + (LIBNETTLE_MINOR): Bump minor number, to 8.4.
  10. + (LIBHOGWEED_MINOR): Bump minor number, to 6.4.
  11. +
  12. +2021-05-17 Niels Möller <nisse@lysator.liu.se>
  13. +
  14. + * rsa-decrypt-tr.c (rsa_decrypt_tr): Check up-front that input is
  15. + in range.
  16. + * rsa-sec-decrypt.c (rsa_sec_decrypt): Likewise.
  17. + * rsa-decrypt.c (rsa_decrypt): Likewise.
  18. + * testsuite/rsa-encrypt-test.c (test_main): Add tests with input > n.
  19. +
  20. +2021-05-14 Niels Möller <nisse@lysator.liu.se>
  21. +
  22. + * rsa-sign-tr.c (rsa_sec_blind): Delete mn argument.
  23. + (_rsa_sec_compute_root_tr): Delete mn argument, instead require
  24. + that input size matches key size. Rearrange use of temporary
  25. + storage, to support in-place operation, x == m. Update all
  26. + callers.
  27. +
  28. + * rsa-decrypt-tr.c (rsa_decrypt_tr): Make zero-padded copy of
  29. + input, for calling _rsa_sec_compute_root_tr.
  30. + * rsa-sec-decrypt.c (rsa_sec_decrypt): Likewise.
  31. +
  32. + * testsuite/rsa-encrypt-test.c (test_main): Test calling all of
  33. + rsa_decrypt, rsa_decrypt_tr, and rsa_sec_decrypt with zero input.
  34. +
  35. +2021-05-06 Niels Möller <nisse@lysator.liu.se>
  36. +
  37. + * pkcs1-sec-decrypt.c (_pkcs1_sec_decrypt): Check that message
  38. + length is valid, for given key size.
  39. + * testsuite/rsa-sec-decrypt-test.c (test_main): Add test cases for
  40. + calls to rsa_sec_decrypt specifying a too large message length.
  41. +
  42. 2021-03-21 Niels Möller <nisse@lysator.liu.se>
  43. * NEWS: NEWS entries for 3.7.2.
  44. diff --git a/nettle/NEWS b/nettle/NEWS
  45. index 897527c9..277ef383 100644
  46. --- a/nettle/NEWS
  47. +++ b/nettle/NEWS
  48. @@ -1,3 +1,40 @@
  49. +NEWS for the Nettle 3.7.3 release
  50. +
  51. + This is bugfix release, fixing bugs that could make the RSA
  52. + decryption functions crash on invalid inputs.
  53. +
  54. + Upgrading to the new version is strongly recommended. For
  55. + applications that want to support older versions of Nettle,
  56. + the bug can be worked around by adding a check that the RSA
  57. + ciphertext is in the range 0 < ciphertext < n, before
  58. + attempting to decrypt it.
  59. +
  60. + Thanks to Justus Winter for reporting these problems.
  61. +
  62. + The new version is intended to be fully source and binary
  63. + compatible with Nettle-3.6. The shared library names are
  64. + libnettle.so.8.4 and libhogweed.so.6.4, with sonames
  65. + libnettle.so.8 and libhogweed.so.6.
  66. +
  67. + Bug fixes:
  68. +
  69. + * Fix crash for zero input to rsa_sec_decrypt and
  70. + rsa_decrypt_tr. Potential denial of service vector.
  71. +
  72. + * Ensure that all of rsa_decrypt_tr and rsa_sec_decrypt return
  73. + failure for out of range inputs, instead of either crashing,
  74. + or silently reducing input modulo n. Potential denial of
  75. + service vector.
  76. +
  77. + * Ensure that all of rsa_decrypt returns failure for out of
  78. + range inputs, instead of silently reducing input modulo n.
  79. +
  80. + * Ensure that rsa_sec_decrypt returns failure if the message
  81. + size is too large for the given key. Unlike the other bugs,
  82. + this would typically be triggered by invalid local
  83. + configuration, rather than by processing untrusted remote
  84. + data.
  85. +
  86. NEWS for the Nettle 3.7.2 release
  87. This is a bugfix release, fixing a bug in ECDSA signature
  88. diff --git a/nettle/configure.ac b/nettle/configure.ac
  89. index 9b2c153b..f4d9e905 100644
  90. --- a/nettle/configure.ac
  91. +++ b/nettle/configure.ac
  92. @@ -2,7 +2,7 @@ dnl -*- mode: shell-script; sh-indentation: 2; -*-
  93. dnl Process this file with autoconf to produce a configure script.
  94. -AC_INIT([nettle], [3.7.2], [nettle-bugs@lists.lysator.liu.se])
  95. +AC_INIT([nettle], [3.7.3], [nettle-bugs@lists.lysator.liu.se])
  96. AC_PREREQ(2.61)
  97. AC_CONFIG_SRCDIR([arcfour.c])
  98. # Needed to stop autoconf from looking for files in parent directories.
  99. @@ -11,10 +11,10 @@ AC_CONFIG_AUX_DIR([.])
  100. AC_CONFIG_HEADER([config.h])
  101. LIBNETTLE_MAJOR=8
  102. -LIBNETTLE_MINOR=3
  103. +LIBNETTLE_MINOR=4
  104. LIBHOGWEED_MAJOR=6
  105. -LIBHOGWEED_MINOR=3
  106. +LIBHOGWEED_MINOR=4
  107. dnl Note double square brackets, for extra m4 quoting.
  108. MAJOR_VERSION=`echo $PACKAGE_VERSION | sed 's/^\([[^.]]*\)\..*/\1/'`
  109. diff --git a/nettle/pkcs1-sec-decrypt.c b/nettle/pkcs1-sec-decrypt.c
  110. index 4f13080e..942a2bd3 100644
  111. --- a/nettle/pkcs1-sec-decrypt.c
  112. +++ b/nettle/pkcs1-sec-decrypt.c
  113. @@ -63,7 +63,9 @@ _pkcs1_sec_decrypt (size_t length, uint8_t *message,
  114. volatile int ok;
  115. size_t i, t;
  116. - assert (padded_message_length >= length);
  117. + /* Message independent branch */
  118. + if (length + 11 > padded_message_length)
  119. + return 0;
  120. t = padded_message_length - length - 1;
  121. @@ -99,8 +101,8 @@ _pkcs1_sec_decrypt_variable(size_t *length, uint8_t *message,
  122. /* length is discovered in a side-channel silent way.
  123. * not_found goes to 0 when the terminator is found.
  124. - * offset strts at 3 as it includes the terminator and
  125. - * the fomat bytes already */
  126. + * offset starts at 3 as it includes the terminator and
  127. + * the format bytes already */
  128. offset = 3;
  129. for (i = 2; i < padded_message_length; i++)
  130. {
  131. diff --git a/nettle/rsa-decrypt-tr.c b/nettle/rsa-decrypt-tr.c
  132. index 0224c0b7..4a9e9d74 100644
  133. --- a/nettle/rsa-decrypt-tr.c
  134. +++ b/nettle/rsa-decrypt-tr.c
  135. @@ -52,14 +52,17 @@ rsa_decrypt_tr(const struct rsa_public_key *pub,
  136. mp_size_t key_limb_size;
  137. int res;
  138. - key_limb_size = NETTLE_OCTET_SIZE_TO_LIMB_SIZE(key->size);
  139. + /* First check that input is in range. */
  140. + if (mpz_sgn (gibberish) < 0 || mpz_cmp (gibberish, pub->n) >= 0)
  141. + return 0;
  142. +
  143. + key_limb_size = mpz_size(pub->n);
  144. TMP_GMP_ALLOC (m, key_limb_size);
  145. TMP_GMP_ALLOC (em, key->size);
  146. + mpz_limbs_copy(m, gibberish, key_limb_size);
  147. - res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, m,
  148. - mpz_limbs_read(gibberish),
  149. - mpz_size(gibberish));
  150. + res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, m, m);
  151. mpn_get_base256 (em, key->size, m, key_limb_size);
  152. diff --git a/nettle/rsa-decrypt.c b/nettle/rsa-decrypt.c
  153. index 7681439d..540d8baa 100644
  154. --- a/nettle/rsa-decrypt.c
  155. +++ b/nettle/rsa-decrypt.c
  156. @@ -48,6 +48,16 @@ rsa_decrypt(const struct rsa_private_key *key,
  157. int res;
  158. mpz_init(m);
  159. +
  160. + /* First check that input is in range. Since we don't have the
  161. + public key available here, we need to reconstruct n. */
  162. + mpz_mul (m, key->p, key->q);
  163. + if (mpz_sgn (gibberish) < 0 || mpz_cmp (gibberish, m) >= 0)
  164. + {
  165. + mpz_clear (m);
  166. + return 0;
  167. + }
  168. +
  169. rsa_compute_root(key, m, gibberish);
  170. res = pkcs1_decrypt (key->size, m, length, message);
  171. diff --git a/nettle/rsa-internal.h b/nettle/rsa-internal.h
  172. index b828e451..f66a7df0 100644
  173. --- a/nettle/rsa-internal.h
  174. +++ b/nettle/rsa-internal.h
  175. @@ -78,11 +78,11 @@ _rsa_sec_compute_root(const struct rsa_private_key *key,
  176. mp_limb_t *scratch);
  177. /* Safe side-channel silent variant, using RSA blinding, and checking the
  178. - * result after CRT. */
  179. + * result after CRT. In-place calls, with x == m, is allowed. */
  180. int
  181. _rsa_sec_compute_root_tr(const struct rsa_public_key *pub,
  182. const struct rsa_private_key *key,
  183. void *random_ctx, nettle_random_func *random,
  184. - mp_limb_t *x, const mp_limb_t *m, size_t mn);
  185. + mp_limb_t *x, const mp_limb_t *m);
  186. #endif /* NETTLE_RSA_INTERNAL_H_INCLUDED */
  187. diff --git a/nettle/rsa-sec-decrypt.c b/nettle/rsa-sec-decrypt.c
  188. index 6866e7c8..4c98958d 100644
  189. --- a/nettle/rsa-sec-decrypt.c
  190. +++ b/nettle/rsa-sec-decrypt.c
  191. @@ -55,12 +55,19 @@ rsa_sec_decrypt(const struct rsa_public_key *pub,
  192. TMP_GMP_DECL (em, uint8_t);
  193. int res;
  194. + /* First check that input is in range. */
  195. + if (mpz_sgn (gibberish) < 0 || mpz_cmp (gibberish, pub->n) >= 0)
  196. + return 0;
  197. +
  198. TMP_GMP_ALLOC (m, mpz_size(pub->n));
  199. TMP_GMP_ALLOC (em, key->size);
  200. - res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, m,
  201. - mpz_limbs_read(gibberish),
  202. - mpz_size(gibberish));
  203. + /* We need a copy because m can be shorter than key_size,
  204. + * but _rsa_sec_compute_root_tr expect all inputs to be
  205. + * normalized to a key_size long buffer length */
  206. + mpz_limbs_copy(m, gibberish, mpz_size(pub->n));
  207. +
  208. + res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, m, m);
  209. mpn_get_base256 (em, key->size, m, mpz_size(pub->n));
  210. diff --git a/nettle/rsa-sign-tr.c b/nettle/rsa-sign-tr.c
  211. index f824c4ca..9e137c7a 100644
  212. --- a/nettle/rsa-sign-tr.c
  213. +++ b/nettle/rsa-sign-tr.c
  214. @@ -131,35 +131,34 @@ int
  215. _rsa_sec_compute_root_tr(const struct rsa_public_key *pub,
  216. const struct rsa_private_key *key,
  217. void *random_ctx, nettle_random_func *random,
  218. - mp_limb_t *x, const mp_limb_t *m, size_t mn)
  219. + mp_limb_t *x, const mp_limb_t *m)
  220. {
  221. + mp_size_t nn;
  222. mpz_t mz;
  223. mpz_t xz;
  224. int res;
  225. - mpz_init(mz);
  226. mpz_init(xz);
  227. - mpn_copyi(mpz_limbs_write(mz, mn), m, mn);
  228. - mpz_limbs_finish(mz, mn);
  229. + nn = mpz_size (pub->n);
  230. - res = rsa_compute_root_tr(pub, key, random_ctx, random, xz, mz);
  231. + res = rsa_compute_root_tr(pub, key, random_ctx, random, xz,
  232. + mpz_roinit_n(mz, m, nn));
  233. if (res)
  234. - mpz_limbs_copy(x, xz, mpz_size(pub->n));
  235. + mpz_limbs_copy(x, xz, nn);
  236. - mpz_clear(mz);
  237. mpz_clear(xz);
  238. return res;
  239. }
  240. #else
  241. /* Blinds m, by computing c = m r^e (mod n), for a random r. Also
  242. - returns the inverse (ri), for use by rsa_unblind. */
  243. + returns the inverse (ri), for use by rsa_unblind. Must have c != m,
  244. + no in-place operation.*/
  245. static void
  246. rsa_sec_blind (const struct rsa_public_key *pub,
  247. void *random_ctx, nettle_random_func *random,
  248. - mp_limb_t *c, mp_limb_t *ri, const mp_limb_t *m,
  249. - mp_size_t mn)
  250. + mp_limb_t *c, mp_limb_t *ri, const mp_limb_t *m)
  251. {
  252. const mp_limb_t *ep = mpz_limbs_read (pub->e);
  253. const mp_limb_t *np = mpz_limbs_read (pub->n);
  254. @@ -177,15 +176,15 @@ rsa_sec_blind (const struct rsa_public_key *pub,
  255. /* c = m*(r^e) mod n */
  256. itch = mpn_sec_powm_itch(nn, ebn, nn);
  257. - i2 = mpn_sec_mul_itch(nn, mn);
  258. + i2 = mpn_sec_mul_itch(nn, nn);
  259. itch = MAX(itch, i2);
  260. - i2 = mpn_sec_div_r_itch(nn + mn, nn);
  261. + i2 = mpn_sec_div_r_itch(2*nn, nn);
  262. itch = MAX(itch, i2);
  263. i2 = mpn_sec_invert_itch(nn);
  264. itch = MAX(itch, i2);
  265. - TMP_GMP_ALLOC (tp, nn + mn + itch);
  266. - scratch = tp + nn + mn;
  267. + TMP_GMP_ALLOC (tp, 2*nn + itch);
  268. + scratch = tp + 2*nn;
  269. /* ri = r^(-1) */
  270. do
  271. @@ -198,9 +197,8 @@ rsa_sec_blind (const struct rsa_public_key *pub,
  272. while (!mpn_sec_invert (ri, tp, np, nn, 2 * nn * GMP_NUMB_BITS, scratch));
  273. mpn_sec_powm (c, rp, nn, ep, ebn, np, nn, scratch);
  274. - /* normally mn == nn, but m can be smaller in some cases */
  275. - mpn_sec_mul (tp, c, nn, m, mn, scratch);
  276. - mpn_sec_div_r (tp, nn + mn, np, nn, scratch);
  277. + mpn_sec_mul (tp, c, nn, m, nn, scratch);
  278. + mpn_sec_div_r (tp, 2*nn, np, nn, scratch);
  279. mpn_copyi(c, tp, nn);
  280. TMP_GMP_FREE (r);
  281. @@ -208,7 +206,7 @@ rsa_sec_blind (const struct rsa_public_key *pub,
  282. TMP_GMP_FREE (tp);
  283. }
  284. -/* m = c ri mod n */
  285. +/* m = c ri mod n. Allows x == c. */
  286. static void
  287. rsa_sec_unblind (const struct rsa_public_key *pub,
  288. mp_limb_t *x, mp_limb_t *ri, const mp_limb_t *c)
  289. @@ -299,7 +297,7 @@ int
  290. _rsa_sec_compute_root_tr(const struct rsa_public_key *pub,
  291. const struct rsa_private_key *key,
  292. void *random_ctx, nettle_random_func *random,
  293. - mp_limb_t *x, const mp_limb_t *m, size_t mn)
  294. + mp_limb_t *x, const mp_limb_t *m)
  295. {
  296. TMP_GMP_DECL (c, mp_limb_t);
  297. TMP_GMP_DECL (ri, mp_limb_t);
  298. @@ -307,7 +305,7 @@ _rsa_sec_compute_root_tr(const struct rsa_public_key *pub,
  299. size_t key_limb_size;
  300. int ret;
  301. - key_limb_size = NETTLE_OCTET_SIZE_TO_LIMB_SIZE(key->size);
  302. + key_limb_size = mpz_size(pub->n);
  303. /* mpz_powm_sec handles only odd moduli. If p, q or n is even, the
  304. key is invalid and rejected by rsa_private_key_prepare. However,
  305. @@ -321,19 +319,18 @@ _rsa_sec_compute_root_tr(const struct rsa_public_key *pub,
  306. }
  307. assert(mpz_size(pub->n) == key_limb_size);
  308. - assert(mn <= key_limb_size);
  309. TMP_GMP_ALLOC (c, key_limb_size);
  310. TMP_GMP_ALLOC (ri, key_limb_size);
  311. TMP_GMP_ALLOC (scratch, _rsa_sec_compute_root_itch(key));
  312. - rsa_sec_blind (pub, random_ctx, random, x, ri, m, mn);
  313. + rsa_sec_blind (pub, random_ctx, random, c, ri, m);
  314. - _rsa_sec_compute_root(key, c, x, scratch);
  315. + _rsa_sec_compute_root(key, x, c, scratch);
  316. - ret = rsa_sec_check_root(pub, c, x);
  317. + ret = rsa_sec_check_root(pub, x, c);
  318. - rsa_sec_unblind(pub, x, ri, c);
  319. + rsa_sec_unblind(pub, x, ri, x);
  320. cnd_mpn_zero(1 - ret, x, key_limb_size);
  321. @@ -357,17 +354,17 @@ rsa_compute_root_tr(const struct rsa_public_key *pub,
  322. mpz_t x, const mpz_t m)
  323. {
  324. TMP_GMP_DECL (l, mp_limb_t);
  325. + mp_size_t nn = mpz_size(pub->n);
  326. int res;
  327. - mp_size_t l_size = NETTLE_OCTET_SIZE_TO_LIMB_SIZE(key->size);
  328. - TMP_GMP_ALLOC (l, l_size);
  329. + TMP_GMP_ALLOC (l, nn);
  330. + mpz_limbs_copy(l, m, nn);
  331. - res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, l,
  332. - mpz_limbs_read(m), mpz_size(m));
  333. + res = _rsa_sec_compute_root_tr (pub, key, random_ctx, random, l, l);
  334. if (res) {
  335. - mp_limb_t *xp = mpz_limbs_write (x, l_size);
  336. - mpn_copyi (xp, l, l_size);
  337. - mpz_limbs_finish (x, l_size);
  338. + mp_limb_t *xp = mpz_limbs_write (x, nn);
  339. + mpn_copyi (xp, l, nn);
  340. + mpz_limbs_finish (x, nn);
  341. }
  342. TMP_GMP_FREE (l);
  343. diff --git a/nettle/rsa.h b/nettle/rsa.h
  344. index 3b10155f..2dd35a2d 100644
  345. --- a/nettle/rsa.h
  346. +++ b/nettle/rsa.h
  347. @@ -428,13 +428,14 @@ rsa_sec_decrypt(const struct rsa_public_key *pub,
  348. size_t length, uint8_t *message,
  349. const mpz_t gibberish);
  350. -/* Compute x, the e:th root of m. Calling it with x == m is allowed. */
  351. +/* Compute x, the e:th root of m. Calling it with x == m is allowed.
  352. + It is required that 0 <= m < n. */
  353. void
  354. rsa_compute_root(const struct rsa_private_key *key,
  355. mpz_t x, const mpz_t m);
  356. /* Safer variant, using RSA blinding, and checking the result after
  357. - CRT. */
  358. + CRT. It is required that 0 <= m < n. */
  359. int
  360. rsa_compute_root_tr(const struct rsa_public_key *pub,
  361. const struct rsa_private_key *key,
  362. diff --git a/nettle/testsuite/rsa-encrypt-test.c b/nettle/testsuite/rsa-encrypt-test.c
  363. index 87525f78..d1a440f6 100644
  364. --- a/nettle/testsuite/rsa-encrypt-test.c
  365. +++ b/nettle/testsuite/rsa-encrypt-test.c
  366. @@ -19,10 +19,12 @@ test_main(void)
  367. uint8_t after;
  368. mpz_t gibberish;
  369. + mpz_t bad_input;
  370. rsa_private_key_init(&key);
  371. rsa_public_key_init(&pub);
  372. mpz_init(gibberish);
  373. + mpz_init(bad_input);
  374. knuth_lfib_init(&lfib, 17);
  375. @@ -101,6 +103,42 @@ test_main(void)
  376. ASSERT(decrypted[decrypted_length] == after);
  377. ASSERT(decrypted[0] == 'A');
  378. + /* Test zero input. */
  379. + mpz_set_ui (bad_input, 0);
  380. + decrypted_length = msg_length;
  381. + ASSERT(!rsa_decrypt(&key, &decrypted_length, decrypted, bad_input));
  382. + ASSERT(!rsa_decrypt_tr(&pub, &key,
  383. + &lfib, (nettle_random_func *) knuth_lfib_random,
  384. + &decrypted_length, decrypted, bad_input));
  385. + ASSERT(!rsa_sec_decrypt(&pub, &key,
  386. + &lfib, (nettle_random_func *) knuth_lfib_random,
  387. + decrypted_length, decrypted, bad_input));
  388. + ASSERT(decrypted_length == msg_length);
  389. +
  390. + /* Test input that is slightly larger than n */
  391. + mpz_add(bad_input, gibberish, pub.n);
  392. + decrypted_length = msg_length;
  393. + ASSERT(!rsa_decrypt(&key, &decrypted_length, decrypted, bad_input));
  394. + ASSERT(!rsa_decrypt_tr(&pub, &key,
  395. + &lfib, (nettle_random_func *) knuth_lfib_random,
  396. + &decrypted_length, decrypted, bad_input));
  397. + ASSERT(!rsa_sec_decrypt(&pub, &key,
  398. + &lfib, (nettle_random_func *) knuth_lfib_random,
  399. + decrypted_length, decrypted, bad_input));
  400. + ASSERT(decrypted_length == msg_length);
  401. +
  402. + /* Test input that is considerably larger than n */
  403. + mpz_mul_2exp (bad_input, pub.n, 100);
  404. + mpz_add (bad_input, bad_input, gibberish);
  405. + decrypted_length = msg_length;
  406. + ASSERT(!rsa_decrypt(&key, &decrypted_length, decrypted, bad_input));
  407. + ASSERT(!rsa_decrypt_tr(&pub, &key,
  408. + &lfib, (nettle_random_func *) knuth_lfib_random,
  409. + &decrypted_length, decrypted, bad_input));
  410. + ASSERT(!rsa_sec_decrypt(&pub, &key,
  411. + &lfib, (nettle_random_func *) knuth_lfib_random,
  412. + decrypted_length, decrypted, bad_input));
  413. + ASSERT(decrypted_length == msg_length);
  414. /* Test invalid key. */
  415. mpz_add_ui (key.q, key.q, 2);
  416. @@ -112,6 +150,6 @@ test_main(void)
  417. rsa_private_key_clear(&key);
  418. rsa_public_key_clear(&pub);
  419. mpz_clear(gibberish);
  420. + mpz_clear(bad_input);
  421. free(decrypted);
  422. }
  423. -
  424. diff --git a/nettle/testsuite/rsa-sec-decrypt-test.c b/nettle/testsuite/rsa-sec-decrypt-test.c
  425. index fb0ed3a1..3419322e 100644
  426. --- a/nettle/testsuite/rsa-sec-decrypt-test.c
  427. +++ b/nettle/testsuite/rsa-sec-decrypt-test.c
  428. @@ -55,6 +55,7 @@ rsa_decrypt_for_test(const struct rsa_public_key *pub,
  429. #endif
  430. #define PAYLOAD_SIZE 50
  431. +#define DECRYPTED_SIZE 256
  432. void
  433. test_main(void)
  434. {
  435. @@ -63,7 +64,7 @@ test_main(void)
  436. struct knuth_lfib_ctx random_ctx;
  437. uint8_t plaintext[PAYLOAD_SIZE];
  438. - uint8_t decrypted[PAYLOAD_SIZE];
  439. + uint8_t decrypted[DECRYPTED_SIZE];
  440. uint8_t verifybad[PAYLOAD_SIZE];
  441. unsigned n_size = 1024;
  442. mpz_t gibberish;
  443. @@ -99,6 +100,20 @@ test_main(void)
  444. PAYLOAD_SIZE, decrypted, gibberish) == 1);
  445. ASSERT (MEMEQ (PAYLOAD_SIZE, plaintext, decrypted));
  446. + ASSERT (pub.size > 10);
  447. + ASSERT (pub.size <= DECRYPTED_SIZE);
  448. +
  449. + /* Check that too large message length is rejected, largest
  450. + valid size is pub.size - 11. */
  451. + ASSERT (!rsa_decrypt_for_test (&pub, &key, &random_ctx,
  452. + (nettle_random_func *) knuth_lfib_random,
  453. + pub.size - 10, decrypted, gibberish));
  454. +
  455. + /* This case used to result in arithmetic underflow and a crash. */
  456. + ASSERT (!rsa_decrypt_for_test (&pub, &key, &random_ctx,
  457. + (nettle_random_func *) knuth_lfib_random,
  458. + pub.size, decrypted, gibberish));
  459. +
  460. /* bad one */
  461. memcpy(decrypted, verifybad, PAYLOAD_SIZE);
  462. nettle_mpz_random_size(garbage, &random_ctx,