diff --git a/SOURCES/nettle-3.4.1-powerpc64-ghash-asm.patch b/SOURCES/nettle-3.4.1-powerpc64-ghash-asm.patch index 0b64e71..255adbd 100644 --- a/SOURCES/nettle-3.4.1-powerpc64-ghash-asm.patch +++ b/SOURCES/nettle-3.4.1-powerpc64-ghash-asm.patch @@ -1,7 +1,30 @@ -diff -up ./configure.ac.ghash-asm ./configure.ac ---- ./configure.ac.ghash-asm 2021-06-30 16:04:51.228443240 +0200 -+++ ./configure.ac 2021-06-30 16:04:51.230443230 +0200 -@@ -472,7 +472,7 @@ asm_replace_list="aes-encrypt-internal.a +diff -up ./configure.ac.ghash ./configure.ac +--- ./configure.ac.ghash 2021-07-14 14:11:58.126891572 +0200 ++++ ./configure.ac 2021-07-14 14:11:58.130891552 +0200 +@@ -211,6 +211,22 @@ AC_C_BIGENDIAN([AC_DEFINE([WORDS_BIGENDI + ASM_WORDS_BIGENDIAN=yes], + [ASM_WORDS_BIGENDIAN=no]) + ++AC_CACHE_CHECK([for __builtin_bswap64], ++ nettle_cv_c_builtin_bswap64, ++[AC_TRY_LINK([ ++#include ++],[ ++uint64_t x = 17; ++uint64_t y = __builtin_bswap64(x); ++], ++nettle_cv_c_builtin_bswap64=yes, ++nettle_cv_c_builtin_bswap64=no)]) ++ ++AH_TEMPLATE([HAVE_BUILTIN_BSWAP64], [Define if __builtin_bswap64 is available]) ++if test "x$nettle_cv_c_builtin_bswap64" = "xyes" ; then ++ AC_DEFINE(HAVE_BUILTIN_BSWAP64) ++fi ++ + LSH_GCC_ATTRIBUTES + + # According to Simon Josefsson, looking for uint32_t and friends in +@@ -472,7 +488,7 @@ asm_replace_list="aes-encrypt-internal.a sha3-permute.asm umac-nh.asm umac-nh-n.asm machine.m4" # Assembler files which generate additional object files if they are used. @@ -10,7 +33,7 @@ diff -up ./configure.ac.ghash-asm ./configure.ac aes-encrypt-internal-2.asm aes-decrypt-internal-2.asm memxor-2.asm \ salsa20-core-internal-2.asm sha1-compress-2.asm sha256-compress-2.asm \ sha3-permute-2.asm sha512-compress-2.asm \ -@@ -588,6 +588,10 @@ AH_VERBATIM([HAVE_NATIVE], +@@ -588,6 +604,10 @@ AH_VERBATIM([HAVE_NATIVE], #undef HAVE_NATIVE_ecc_384_redc #undef HAVE_NATIVE_ecc_521_modp #undef HAVE_NATIVE_ecc_521_redc @@ -21,9 +44,9 @@ diff -up ./configure.ac.ghash-asm ./configure.ac #undef HAVE_NATIVE_gcm_hash8 #undef HAVE_NATIVE_salsa20_core #undef HAVE_NATIVE_sha1_compress -diff -up ./ctr16.c.ghash-asm ./ctr16.c ---- ./ctr16.c.ghash-asm 2021-06-30 16:04:51.230443230 +0200 -+++ ./ctr16.c 2021-06-30 16:04:51.230443230 +0200 +diff -up ./ctr16.c.ghash ./ctr16.c +--- ./ctr16.c.ghash 2021-07-14 14:11:58.130891552 +0200 ++++ ./ctr16.c 2021-07-14 14:11:58.130891552 +0200 @@ -0,0 +1,106 @@ +/* ctr16.c + @@ -131,10 +154,10 @@ diff -up ./ctr16.c.ghash-asm ./ctr16.c + } + } +} -diff -up ./ctr.c.ghash-asm ./ctr.c ---- ./ctr.c.ghash-asm 2018-12-04 21:56:05.000000000 +0100 -+++ ./ctr.c 2021-06-30 16:04:51.230443230 +0200 -@@ -41,11 +41,72 @@ +diff -up ./ctr.c.ghash ./ctr.c +--- ./ctr.c.ghash 2018-12-04 21:56:05.000000000 +0100 ++++ ./ctr.c 2021-07-14 14:13:07.714539484 +0200 +@@ -41,11 +41,83 @@ #include "ctr.h" @@ -146,6 +169,17 @@ diff -up ./ctr.c.ghash-asm ./ctr.c -#define NBLOCKS 4 +#define MIN(a,b) (((a) < (b)) ? (a) : (b)) + ++/* The 'u64' member has been added in the public header ++ (nettle-types.h). Check that the alignment is not affected with ++ it using _Static_assert. */ ++union nettle_block16_ ++{ ++ uint8_t b[16]; ++ unsigned long w[16 / sizeof(unsigned long)]; ++}; ++_Static_assert(__alignof(union nettle_block16_) == __alignof(union nettle_block16), ++ "nettle_block16 alignment should be preserved"); ++ +static size_t +ctr_fill (size_t block_size, uint8_t *ctr, size_t length, uint8_t *buffer) +{ @@ -208,7 +242,7 @@ diff -up ./ctr.c.ghash-asm ./ctr.c void ctr_crypt(const void *ctx, nettle_cipher_func *f, -@@ -53,84 +114,64 @@ ctr_crypt(const void *ctx, nettle_cipher +@@ -53,84 +125,64 @@ ctr_crypt(const void *ctx, nettle_cipher size_t length, uint8_t *dst, const uint8_t *src) { @@ -338,9 +372,9 @@ diff -up ./ctr.c.ghash-asm ./ctr.c } } } -diff -up ./ctr-internal.h.ghash-asm ./ctr-internal.h ---- ./ctr-internal.h.ghash-asm 2021-06-30 16:04:51.230443230 +0200 -+++ ./ctr-internal.h 2021-06-30 16:04:51.230443230 +0200 +diff -up ./ctr-internal.h.ghash ./ctr-internal.h +--- ./ctr-internal.h.ghash 2021-07-14 14:11:58.130891552 +0200 ++++ ./ctr-internal.h 2021-07-14 14:11:58.130891552 +0200 @@ -0,0 +1,56 @@ +/* ctr-internal.h + @@ -398,9 +432,9 @@ diff -up ./ctr-internal.h.ghash-asm ./ctr-internal.h + + +#endif /* NETTLE_CTR_INTERNAL_H_INCLUDED */ -diff -up ./fat-ppc.c.ghash-asm ./fat-ppc.c ---- ./fat-ppc.c.ghash-asm 2021-06-30 16:04:51.228443240 +0200 -+++ ./fat-ppc.c 2021-06-30 16:04:51.230443230 +0200 +diff -up ./fat-ppc.c.ghash ./fat-ppc.c +--- ./fat-ppc.c.ghash 2021-07-14 14:11:58.126891572 +0200 ++++ ./fat-ppc.c 2021-07-14 14:11:58.130891552 +0200 @@ -49,6 +49,7 @@ #include "aes-internal.h" @@ -481,9 +515,9 @@ diff -up ./fat-ppc.c.ghash-asm ./fat-ppc.c + size_t length, const uint8_t *data), + (key, x, length, data)) +#endif /* GCM_TABLE_BITS == 8 */ -diff -up ./fat-setup.h.ghash-asm ./fat-setup.h ---- ./fat-setup.h.ghash-asm 2018-12-04 21:56:06.000000000 +0100 -+++ ./fat-setup.h 2021-06-30 16:04:51.230443230 +0200 +diff -up ./fat-setup.h.ghash ./fat-setup.h +--- ./fat-setup.h.ghash 2018-12-04 21:56:06.000000000 +0100 ++++ ./fat-setup.h 2021-07-14 14:11:58.130891552 +0200 @@ -159,6 +159,11 @@ typedef void aes_crypt_internal_func (un size_t length, uint8_t *dst, const uint8_t *src); @@ -496,9 +530,9 @@ diff -up ./fat-setup.h.ghash-asm ./fat-setup.h typedef void *(memxor_func)(void *dst, const void *src, size_t n); typedef void salsa20_core_func (uint32_t *dst, const uint32_t *src, unsigned rounds); -diff -up ./gcm.c.ghash-asm ./gcm.c ---- ./gcm.c.ghash-asm 2018-12-04 21:56:05.000000000 +0100 -+++ ./gcm.c 2021-06-30 16:04:51.231443225 +0200 +diff -up ./gcm.c.ghash ./gcm.c +--- ./gcm.c.ghash 2018-12-04 21:56:05.000000000 +0100 ++++ ./gcm.c 2021-07-14 14:11:58.131891547 +0200 @@ -6,8 +6,9 @@ See also the gcm paper at http://www.cryptobarn.com/papers/gcm-spec.pdf. @@ -847,9 +881,9 @@ diff -up ./gcm.c.ghash-asm ./gcm.c ctx->data_size += length; } -diff -up ./gcm-internal.h.ghash-asm ./gcm-internal.h ---- ./gcm-internal.h.ghash-asm 2021-06-30 16:04:51.231443225 +0200 -+++ ./gcm-internal.h 2021-06-30 16:04:51.231443225 +0200 +diff -up ./gcm-internal.h.ghash ./gcm-internal.h +--- ./gcm-internal.h.ghash 2021-07-14 14:11:58.131891547 +0200 ++++ ./gcm-internal.h 2021-07-14 14:11:58.131891547 +0200 @@ -0,0 +1,54 @@ +/* gcm-internal.h + @@ -905,9 +939,9 @@ diff -up ./gcm-internal.h.ghash-asm ./gcm-internal.h +#endif + +#endif /* NETTLE_GCM_INTERNAL_H_INCLUDED */ -diff -up ./Makefile.in.ghash-asm ./Makefile.in ---- ./Makefile.in.ghash-asm 2021-06-30 16:04:51.227443245 +0200 -+++ ./Makefile.in 2021-06-30 16:04:51.230443230 +0200 +diff -up ./Makefile.in.ghash ./Makefile.in +--- ./Makefile.in.ghash 2021-07-14 14:11:58.124891582 +0200 ++++ ./Makefile.in 2021-07-14 14:11:58.131891547 +0200 @@ -96,7 +96,7 @@ nettle_SOURCES = aes-decrypt-internal.c chacha-crypt.c chacha-core-internal.c \ chacha-poly1305.c chacha-poly1305-meta.c \ @@ -926,9 +960,9 @@ diff -up ./Makefile.in.ghash-asm ./Makefile.in gmp-glue.h ecc-internal.h fat-setup.h \ mini-gmp.h asm.m4 \ nettle.texinfo nettle.info nettle.html nettle.pdf sha-example.c -diff -up ./nettle-types.h.ghash-asm ./nettle-types.h ---- ./nettle-types.h.ghash-asm 2018-12-04 21:56:06.000000000 +0100 -+++ ./nettle-types.h 2021-06-30 16:06:24.608978492 +0200 +diff -up ./nettle-types.h.ghash ./nettle-types.h +--- ./nettle-types.h.ghash 2018-12-04 21:56:06.000000000 +0100 ++++ ./nettle-types.h 2021-07-14 14:11:58.131891547 +0200 @@ -48,6 +48,7 @@ union nettle_block16 { uint8_t b[16]; @@ -937,9 +971,9 @@ diff -up ./nettle-types.h.ghash-asm ./nettle-types.h }; /* Randomness. Used by key generation and dsa signature creation. */ -diff -up ./powerpc64/fat/gcm-hash.asm.ghash-asm ./powerpc64/fat/gcm-hash.asm ---- ./powerpc64/fat/gcm-hash.asm.ghash-asm 2021-06-30 16:04:51.231443225 +0200 -+++ ./powerpc64/fat/gcm-hash.asm 2021-06-30 16:04:51.231443225 +0200 +diff -up ./powerpc64/fat/gcm-hash.asm.ghash ./powerpc64/fat/gcm-hash.asm +--- ./powerpc64/fat/gcm-hash.asm.ghash 2021-07-14 14:11:58.131891547 +0200 ++++ ./powerpc64/fat/gcm-hash.asm 2021-07-14 14:11:58.131891547 +0200 @@ -0,0 +1,39 @@ +C powerpc64/fat/gcm-hash.asm + @@ -980,9 +1014,9 @@ diff -up ./powerpc64/fat/gcm-hash.asm.ghash-asm ./powerpc64/fat/gcm-hash.asm + +define(, <$1_ppc64>) +include_src() -diff -up ./powerpc64/p8/gcm-hash.asm.ghash-asm ./powerpc64/p8/gcm-hash.asm ---- ./powerpc64/p8/gcm-hash.asm.ghash-asm 2021-06-30 16:04:51.231443225 +0200 -+++ ./powerpc64/p8/gcm-hash.asm 2021-06-30 16:04:51.231443225 +0200 +diff -up ./powerpc64/p8/gcm-hash.asm.ghash ./powerpc64/p8/gcm-hash.asm +--- ./powerpc64/p8/gcm-hash.asm.ghash 2021-07-14 14:11:58.131891547 +0200 ++++ ./powerpc64/p8/gcm-hash.asm 2021-07-14 14:11:58.131891547 +0200 @@ -0,0 +1,499 @@ +C powerpc64/p8/gcm-hash.asm + diff --git a/SOURCES/nettle-3.4.1-rsa-decrypt.patch b/SOURCES/nettle-3.4.1-rsa-decrypt.patch new file mode 100644 index 0000000..ecfba91 --- /dev/null +++ b/SOURCES/nettle-3.4.1-rsa-decrypt.patch @@ -0,0 +1,609 @@ +From 5646ca77ee92de0ae33e7d2e0a3383c61a4091ed Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Niels=20M=C3=B6ller?= +Date: Thu, 6 May 2021 21:30:23 +0200 +Subject: [PATCH 1/4] Add check that message length to _pkcs1_sec_decrypt is + valid. + +* pkcs1-sec-decrypt.c (_pkcs1_sec_decrypt): Check that message +length is valid, for given key size. +* testsuite/rsa-sec-decrypt-test.c (test_main): Add test cases for +calls to rsa_sec_decrypt specifying a too large message length. + +(cherry picked from commit 7616541e6eff73353bf682c62e3a68e4fe696707) +--- + ChangeLog | 8 ++++++++ + pkcs1-sec-decrypt.c | 4 +++- + testsuite/rsa-sec-decrypt-test.c | 17 ++++++++++++++++- + 3 files changed, 27 insertions(+), 2 deletions(-) + +diff --git a/ChangeLog b/ChangeLog +index 4c7338a1..7cd0455e 100644 +--- a/ChangeLog ++++ b/ChangeLog +@@ -1,3 +1,11 @@ ++2021-05-06 Niels Möller ++ ++ Bug fixes merged from from 3.7.3 release (starting from 2021-05-06). ++ * pkcs1-sec-decrypt.c (_pkcs1_sec_decrypt): Check that message ++ length is valid, for given key size. ++ * testsuite/rsa-sec-decrypt-test.c (test_main): Add test cases for ++ calls to rsa_sec_decrypt specifying a too large message length. ++ + 2018-12-04 Niels Möller + + * Released nettle-3.4.1. +diff --git a/pkcs1-sec-decrypt.c b/pkcs1-sec-decrypt.c +index 722044b0..02fd07e1 100644 +--- a/pkcs1-sec-decrypt.c ++++ b/pkcs1-sec-decrypt.c +@@ -64,7 +64,9 @@ _pkcs1_sec_decrypt (size_t length, uint8_t *message, + volatile int ok; + size_t i, t; + +- assert (padded_message_length >= length); ++ /* Message independent branch */ ++ if (length + 11 > padded_message_length) ++ return 0; + + t = padded_message_length - length - 1; + +diff --git a/testsuite/rsa-sec-decrypt-test.c b/testsuite/rsa-sec-decrypt-test.c +index 64f0b13c..4a9f301b 100644 +--- a/testsuite/rsa-sec-decrypt-test.c ++++ b/testsuite/rsa-sec-decrypt-test.c +@@ -55,6 +55,7 @@ rsa_decrypt_for_test(const struct rsa_public_key *pub, + #endif + + #define PAYLOAD_SIZE 50 ++#define DECRYPTED_SIZE 256 + void + test_main(void) + { +@@ -63,7 +64,7 @@ test_main(void) + struct knuth_lfib_ctx random_ctx; + + uint8_t plaintext[PAYLOAD_SIZE]; +- uint8_t decrypted[PAYLOAD_SIZE]; ++ uint8_t decrypted[DECRYPTED_SIZE]; + uint8_t verifybad[PAYLOAD_SIZE]; + unsigned n_size = 1024; + mpz_t gibberish; +@@ -98,6 +99,20 @@ test_main(void) + PAYLOAD_SIZE, decrypted, gibberish) == 1); + ASSERT (MEMEQ (PAYLOAD_SIZE, plaintext, decrypted)); + ++ ASSERT (pub.size > 10); ++ ASSERT (pub.size <= DECRYPTED_SIZE); ++ ++ /* Check that too large message length is rejected, largest ++ valid size is pub.size - 11. */ ++ ASSERT (!rsa_decrypt_for_test (&pub, &key, &random_ctx, ++ (nettle_random_func *) knuth_lfib_random, ++ pub.size - 10, decrypted, gibberish)); ++ ++ /* This case used to result in arithmetic underflow and a crash. */ ++ ASSERT (!rsa_decrypt_for_test (&pub, &key, &random_ctx, ++ (nettle_random_func *) knuth_lfib_random, ++ pub.size, decrypted, gibberish)); ++ + /* bad one */ + memcpy(decrypted, verifybad, PAYLOAD_SIZE); + nettle_mpz_random_size(garbage, &random_ctx, +-- +2.31.1 + + +From 743cdf38353f6dd5d3d91eadc769106cfc116301 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Niels=20M=C3=B6ller?= +Date: Tue, 8 Jun 2021 21:30:48 +0200 +Subject: [PATCH 2/4] Fix comment typos. + +(cherry picked from commit 0a714543136de97c7fd34f1c6ac1592dc5036879) +--- + pkcs1-sec-decrypt.c | 4 ++-- + 1 file changed, 2 insertions(+), 2 deletions(-) + +diff --git a/pkcs1-sec-decrypt.c b/pkcs1-sec-decrypt.c +index 02fd07e1..a7f85c2e 100644 +--- a/pkcs1-sec-decrypt.c ++++ b/pkcs1-sec-decrypt.c +@@ -102,8 +102,8 @@ _pkcs1_sec_decrypt_variable(size_t *length, uint8_t *message, + + /* length is discovered in a side-channel silent way. + * not_found goes to 0 when the terminator is found. +- * offset strts at 3 as it includes the terminator and +- * the fomat bytes already */ ++ * offset starts at 3 as it includes the terminator and ++ * the format bytes already */ + offset = 3; + for (i = 2; i < padded_message_length; i++) + { +-- +2.31.1 + + +From dfce46c4540d2abf040073070cff15f9d1708050 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Niels=20M=C3=B6ller?= +Date: Tue, 8 Jun 2021 21:31:39 +0200 +Subject: [PATCH 3/4] 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) +--- + 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(-) + +diff --git a/ChangeLog b/ChangeLog +index 7cd0455e..ae660fc0 100644 +--- a/ChangeLog ++++ b/ChangeLog +@@ -1,6 +1,21 @@ +-2021-05-06 Niels Möller ++2021-05-14 Niels Möller + + Bug fixes merged from from 3.7.3 release (starting from 2021-05-06). ++ * 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 ++ storage, to support in-place operation, x == m. Update all ++ callers. ++ ++ * rsa-decrypt-tr.c (rsa_decrypt_tr): Make zero-padded copy of ++ input, for calling _rsa_sec_compute_root_tr. ++ * rsa-sec-decrypt.c (rsa_sec_decrypt): Likewise. ++ ++ * testsuite/rsa-encrypt-test.c (test_main): Test calling all of ++ rsa_decrypt, rsa_decrypt_tr, and rsa_sec_decrypt with zero input. ++ ++2021-05-06 Niels Möller ++ + * pkcs1-sec-decrypt.c (_pkcs1_sec_decrypt): Check that message + length is valid, for given key size. + * testsuite/rsa-sec-decrypt-test.c (test_main): Add test cases for +diff --git a/rsa-decrypt-tr.c b/rsa-decrypt-tr.c +index 5dfb91b1..c118e852 100644 +--- a/rsa-decrypt-tr.c ++++ b/rsa-decrypt-tr.c +@@ -52,14 +52,13 @@ rsa_decrypt_tr(const struct rsa_public_key *pub, + 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); + +diff --git a/rsa-internal.h b/rsa-internal.h +index bd667bc2..64a7edf6 100644 +--- a/rsa-internal.h ++++ b/rsa-internal.h +@@ -53,12 +53,12 @@ _rsa_sec_compute_root(const struct rsa_private_key *key, + 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); + + /* additional resistance to memory access side-channel attacks. + * Note: message buffer is returned unchanged on error */ +diff --git a/rsa-sec-decrypt.c b/rsa-sec-decrypt.c +index e6a4b267..633a6852 100644 +--- a/rsa-sec-decrypt.c ++++ b/rsa-sec-decrypt.c +@@ -57,9 +57,12 @@ rsa_sec_decrypt(const struct rsa_public_key *pub, + 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)); + +diff --git a/rsa-sign-tr.c b/rsa-sign-tr.c +index 59c9bd07..141a52c7 100644 +--- a/rsa-sign-tr.c ++++ b/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_key *pub, + + /* 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_key *pub, + 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_key *pub, + 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) +@@ -298,7 +296,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); +@@ -306,7 +304,7 @@ _rsa_sec_compute_root_tr(const struct rsa_public_key *pub, + 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, +@@ -320,19 +318,18 @@ _rsa_sec_compute_root_tr(const struct rsa_public_key *pub, + } + + 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); + +@@ -356,17 +353,17 @@ rsa_compute_root_tr(const struct rsa_public_key *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); +diff --git a/testsuite/rsa-encrypt-test.c b/testsuite/rsa-encrypt-test.c +index 87525f78..d3bc374b 100644 +--- a/testsuite/rsa-encrypt-test.c ++++ b/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); + } +- +-- +2.31.1 + + +From f601611b3c315aba373c0ab2ddf24772e88c1b3e Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Niels=20M=C3=B6ller?= +Date: Tue, 8 Jun 2021 21:32:38 +0200 +Subject: [PATCH 4/4] Add input check to rsa_decrypt family of functions. + +(cherry picked from commit 0ad0b5df315665250dfdaa4a1e087f4799edaefe) +--- + ChangeLog | 10 +++++++++- + rsa-decrypt-tr.c | 4 ++++ + rsa-decrypt.c | 10 ++++++++++ + rsa-sec-decrypt.c | 4 ++++ + rsa.h | 5 +++-- + testsuite/rsa-encrypt-test.c | 38 ++++++++++++++++++++++++++++++------ + 6 files changed, 62 insertions(+), 9 deletions(-) + +diff --git a/ChangeLog b/ChangeLog +index ae660fc0..27f022db 100644 +--- a/ChangeLog ++++ b/ChangeLog +@@ -1,6 +1,14 @@ +-2021-05-14 Niels Möller ++2021-05-17 Niels Möller + + 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 ++ + * 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 +diff --git a/rsa-decrypt-tr.c b/rsa-decrypt-tr.c +index c118e852..1ba3d286 100644 +--- a/rsa-decrypt-tr.c ++++ b/rsa-decrypt-tr.c +@@ -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); +diff --git a/rsa-decrypt.c b/rsa-decrypt.c +index 7681439d..540d8baa 100644 +--- a/rsa-decrypt.c ++++ b/rsa-decrypt.c +@@ -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); +diff --git a/rsa-sec-decrypt.c b/rsa-sec-decrypt.c +index 633a6852..53113c69 100644 +--- a/rsa-sec-decrypt.c ++++ b/rsa-sec-decrypt.c +@@ -54,6 +54,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 0aac6a26..54c35688 100644 +--- a/rsa.h ++++ b/rsa.h +@@ -433,13 +433,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, +diff --git a/testsuite/rsa-encrypt-test.c b/testsuite/rsa-encrypt-test.c +index d3bc374b..d1a440f6 100644 +--- a/testsuite/rsa-encrypt-test.c ++++ b/testsuite/rsa-encrypt-test.c +@@ -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); + } +-- +2.31.1 + diff --git a/SPECS/nettle.spec b/SPECS/nettle.spec index b52155f..5bde11d 100644 --- a/SPECS/nettle.spec +++ b/SPECS/nettle.spec @@ -2,7 +2,7 @@ Name: nettle Version: 3.4.1 -Release: 5%{?dist} +Release: 7%{?dist} Summary: A low-level cryptographic library Group: Development/Libraries @@ -17,6 +17,7 @@ Patch2: nettle-3.4.1-enable-intel-cet.patch Patch3: nettle-3.4.1-ecdsa-verify.patch Patch4: nettle-3.4.1-powerpc64-aes-asm.patch Patch5: nettle-3.4.1-powerpc64-ghash-asm.patch +Patch6: nettle-3.4.1-rsa-decrypt.patch BuildRequires: gcc BuildRequires: gmp-devel, m4 @@ -61,6 +62,7 @@ sed 's/ecc-224.c//g' -i Makefile.in %patch3 -p1 %patch4 -p1 %patch5 -p1 +%patch6 -p1 %build autoreconf -ifv @@ -133,6 +135,12 @@ fi %changelog +* Wed Jul 14 2021 Daiki Ueno - 3.4.1-7 +- Backport CVE-2021-3580 from upstream 3.7.3 release (#1967990) + +* Wed Jul 14 2021 Daiki Ueno - 3.4.1-6 +- Enable CTR mode optimization when the block size is 16 + * Wed Jun 30 2021 Daiki Ueno - 3.4.1-5 - Backport powerpc64 optimization patches from upstream (#1855228) Patch from Christopher M. Riedl.