From 873216c702f625310e664a209854d856b457bb7e Mon Sep 17 00:00:00 2001 From: Thom Wiggers Date: Tue, 16 Apr 2019 13:16:26 +0200 Subject: [PATCH] Fix integer overflow problems in NTRU --- crypto_kem/ntruhps2048509/clean/crypto_sort.c | 2 +- crypto_kem/ntruhps2048509/clean/sample.c | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/crypto_kem/ntruhps2048509/clean/crypto_sort.c b/crypto_kem/ntruhps2048509/clean/crypto_sort.c index 6a9fc5cd..6b12bba3 100644 --- a/crypto_kem/ntruhps2048509/clean/crypto_sort.c +++ b/crypto_kem/ntruhps2048509/clean/crypto_sort.c @@ -8,7 +8,7 @@ #define int32_MINMAX(a,b) \ do { \ int32_t ab = (b) ^ (a); \ - int32_t c = (b) - (a); \ + int32_t c = (int32_t)((int64_t)(b) - (int64_t)(a)); \ c ^= ab & (c ^ (b)); \ c >>= 31; \ c &= ab; \ diff --git a/crypto_kem/ntruhps2048509/clean/sample.c b/crypto_kem/ntruhps2048509/clean/sample.c index 34e5b139..461e15f4 100644 --- a/crypto_kem/ntruhps2048509/clean/sample.c +++ b/crypto_kem/ntruhps2048509/clean/sample.c @@ -25,15 +25,15 @@ void PQCLEAN_NTRUHPS2048509_CLEAN_sample_iid(poly *r, const unsigned char unifor void PQCLEAN_NTRUHPS2048509_CLEAN_sample_fixed_type(poly *r, const unsigned char u[NTRU_SAMPLE_FT_BYTES]) { // Assumes NTRU_SAMPLE_FT_BYTES = ceil(30*(n-1)/8) - int32_t s[NTRU_N - 1]; + uint32_t s[NTRU_N - 1]; int i; // Use 30 bits of u per word for (i = 0; i < (NTRU_N - 1) / 4; i++) { - s[4 * i + 0] = (u[15 * i + 0] << 2) + (u[15 * i + 1] << 10) + (u[15 * i + 2] << 18) + (u[15 * i + 3] << 26); - s[4 * i + 1] = ((u[15 * i + 3] & 0xc0) >> 4) + (u[15 * i + 4] << 4) + (u[15 * i + 5] << 12) + (u[15 * i + 6] << 20) + (u[15 * i + 7] << 28); - s[4 * i + 2] = ((u[15 * i + 7] & 0xf0) >> 2) + (u[15 * i + 8] << 6) + (u[15 * i + 9] << 14) + (u[15 * i + 10] << 22) + (u[15 * i + 11] << 30); - s[4 * i + 3] = (u[15 * i + 11] & 0xfc) + (u[15 * i + 12] << 8) + (u[15 * i + 13] << 15) + (u[15 * i + 14] << 24); + s[4 * i + 0] = (u[15 * i + 0] << 2) + (u[15 * i + 1] << 10) + (u[15 * i + 2] << 18) + ((uint32_t)u[15 * i + 3] << 26); + s[4 * i + 1] = ((u[15 * i + 3] & 0xc0) >> 4) + (u[15 * i + 4] << 4) + (u[15 * i + 5] << 12) + (u[15 * i + 6] << 20) + ((uint32_t)u[15 * i + 7] << 28); + s[4 * i + 2] = ((u[15 * i + 7] & 0xf0) >> 2) + (u[15 * i + 8] << 6) + (u[15 * i + 9] << 14) + (u[15 * i + 10] << 22) + ((uint32_t)u[15 * i + 11] << 30); + s[4 * i + 3] = (u[15 * i + 11] & 0xfc) + (u[15 * i + 12] << 8) + (u[15 * i + 13] << 15) + ((uint32_t)u[15 * i + 14] << 24); } for (i = 0; i < NTRU_WEIGHT / 2; i++) {