diff --git a/scripts/copy_from_upstream/copy_from_upstream.yml b/scripts/copy_from_upstream/copy_from_upstream.yml index 4b7f0c1dc8..7d6b4228df 100644 --- a/scripts/copy_from_upstream/copy_from_upstream.yml +++ b/scripts/copy_from_upstream/copy_from_upstream.yml @@ -8,7 +8,7 @@ upstreams: sig_meta_path: 'crypto_sign/{pqclean_scheme}/META.yml' kem_scheme_path: 'crypto_kem/{pqclean_scheme}' sig_scheme_path: 'crypto_sign/{pqclean_scheme}' - patches: [pqclean-dilithium-arm-randomized-signing.patch, pqclean-kyber-armneon-shake-fixes.patch, pqclean-kyber-armneon-768-1024-fixes.patch] + patches: [pqclean-dilithium-arm-randomized-signing.patch, pqclean-kyber-armneon-shake-fixes.patch, pqclean-kyber-armneon-768-1024-fixes.patch, pqclean-kyber-armneon-variable-timing-fix.patch] ignore: pqclean_sphincs-shake-256s-simple_aarch64, pqclean_sphincs-shake-256s-simple_aarch64, pqclean_sphincs-shake-256f-simple_aarch64, pqclean_sphincs-shake-192s-simple_aarch64, pqclean_sphincs-shake-192f-simple_aarch64, pqclean_sphincs-shake-128s-simple_aarch64, pqclean_sphincs-shake-128f-simple_aarch64 - name: pqclean diff --git a/scripts/copy_from_upstream/patches/pqclean-kyber-armneon-variable-timing-fix.patch b/scripts/copy_from_upstream/patches/pqclean-kyber-armneon-variable-timing-fix.patch new file mode 100644 index 0000000000..071c58e243 --- /dev/null +++ b/scripts/copy_from_upstream/patches/pqclean-kyber-armneon-variable-timing-fix.patch @@ -0,0 +1,81 @@ +diff --git a/crypto_kem/kyber1024/aarch64/poly.c b/crypto_kem/kyber1024/aarch64/poly.c +index 1dfa52c..02e010b 100644 +--- a/crypto_kem/kyber1024/aarch64/poly.c ++++ b/crypto_kem/kyber1024/aarch64/poly.c +@@ -207,14 +207,19 @@ void poly_frommsg(int16_t r[KYBER_N], const uint8_t msg[KYBER_INDCPA_MSGBYTES]) + **************************************************/ + void poly_tomsg(uint8_t msg[KYBER_INDCPA_MSGBYTES], const int16_t a[KYBER_N]) { + unsigned int i, j; +- uint16_t t; ++ uint32_t t; + + for (i = 0; i < KYBER_N / 8; i++) { + msg[i] = 0; + for (j = 0; j < 8; j++) { + t = a[8 * i + j]; +- t += ((int16_t)t >> 15) & KYBER_Q; +- t = (((t << 1) + KYBER_Q / 2) / KYBER_Q) & 1; ++ // t += ((int16_t)t >> 15) & KYBER_Q; ++ // t = (((t << 1) + KYBER_Q/2)/KYBER_Q) & 1; ++ t <<= 1; ++ t += 1665; ++ t *= 80635; ++ t >>= 28; ++ t &= 1; + msg[i] |= t << j; + } + } +diff --git a/crypto_kem/kyber512/aarch64/poly.c b/crypto_kem/kyber512/aarch64/poly.c +index dffc655..fcfcedd 100644 +--- a/crypto_kem/kyber512/aarch64/poly.c ++++ b/crypto_kem/kyber512/aarch64/poly.c +@@ -194,14 +194,19 @@ void poly_frommsg(int16_t r[KYBER_N], const uint8_t msg[KYBER_INDCPA_MSGBYTES]) + **************************************************/ + void poly_tomsg(uint8_t msg[KYBER_INDCPA_MSGBYTES], const int16_t a[KYBER_N]) { + unsigned int i, j; +- uint16_t t; ++ uint32_t t; + + for (i = 0; i < KYBER_N / 8; i++) { + msg[i] = 0; + for (j = 0; j < 8; j++) { + t = a[8 * i + j]; +- t += ((int16_t)t >> 15) & KYBER_Q; +- t = (((t << 1) + KYBER_Q / 2) / KYBER_Q) & 1; ++ // t += ((int16_t)t >> 15) & KYBER_Q; ++ // t = (((t << 1) + KYBER_Q/2)/KYBER_Q) & 1; ++ t <<= 1; ++ t += 1665; ++ t *= 80635; ++ t >>= 28; ++ t &= 1; + msg[i] |= t << j; + } + } +diff --git a/crypto_kem/kyber768/aarch64/poly.c b/crypto_kem/kyber768/aarch64/poly.c +index dffc655..fcfcedd 100644 +--- a/crypto_kem/kyber768/aarch64/poly.c ++++ b/crypto_kem/kyber768/aarch64/poly.c +@@ -194,14 +194,19 @@ void poly_frommsg(int16_t r[KYBER_N], const uint8_t msg[KYBER_INDCPA_MSGBYTES]) + **************************************************/ + void poly_tomsg(uint8_t msg[KYBER_INDCPA_MSGBYTES], const int16_t a[KYBER_N]) { + unsigned int i, j; +- uint16_t t; ++ uint32_t t; + + for (i = 0; i < KYBER_N / 8; i++) { + msg[i] = 0; + for (j = 0; j < 8; j++) { + t = a[8 * i + j]; +- t += ((int16_t)t >> 15) & KYBER_Q; +- t = (((t << 1) + KYBER_Q / 2) / KYBER_Q) & 1; ++ // t += ((int16_t)t >> 15) & KYBER_Q; ++ // t = (((t << 1) + KYBER_Q/2)/KYBER_Q) & 1; ++ t <<= 1; ++ t += 1665; ++ t *= 80635; ++ t >>= 28; ++ t &= 1; + msg[i] |= t << j; + } + } diff --git a/src/kem/kyber/oldpqclean_kyber1024_aarch64/poly.c b/src/kem/kyber/oldpqclean_kyber1024_aarch64/poly.c index 1dfa52ce51..02e010b3d5 100644 --- a/src/kem/kyber/oldpqclean_kyber1024_aarch64/poly.c +++ b/src/kem/kyber/oldpqclean_kyber1024_aarch64/poly.c @@ -207,14 +207,19 @@ void poly_frommsg(int16_t r[KYBER_N], const uint8_t msg[KYBER_INDCPA_MSGBYTES]) **************************************************/ void poly_tomsg(uint8_t msg[KYBER_INDCPA_MSGBYTES], const int16_t a[KYBER_N]) { unsigned int i, j; - uint16_t t; + uint32_t t; for (i = 0; i < KYBER_N / 8; i++) { msg[i] = 0; for (j = 0; j < 8; j++) { t = a[8 * i + j]; - t += ((int16_t)t >> 15) & KYBER_Q; - t = (((t << 1) + KYBER_Q / 2) / KYBER_Q) & 1; + // t += ((int16_t)t >> 15) & KYBER_Q; + // t = (((t << 1) + KYBER_Q/2)/KYBER_Q) & 1; + t <<= 1; + t += 1665; + t *= 80635; + t >>= 28; + t &= 1; msg[i] |= t << j; } } diff --git a/src/kem/kyber/oldpqclean_kyber512_aarch64/poly.c b/src/kem/kyber/oldpqclean_kyber512_aarch64/poly.c index dffc655913..fcfceddd83 100644 --- a/src/kem/kyber/oldpqclean_kyber512_aarch64/poly.c +++ b/src/kem/kyber/oldpqclean_kyber512_aarch64/poly.c @@ -194,14 +194,19 @@ void poly_frommsg(int16_t r[KYBER_N], const uint8_t msg[KYBER_INDCPA_MSGBYTES]) **************************************************/ void poly_tomsg(uint8_t msg[KYBER_INDCPA_MSGBYTES], const int16_t a[KYBER_N]) { unsigned int i, j; - uint16_t t; + uint32_t t; for (i = 0; i < KYBER_N / 8; i++) { msg[i] = 0; for (j = 0; j < 8; j++) { t = a[8 * i + j]; - t += ((int16_t)t >> 15) & KYBER_Q; - t = (((t << 1) + KYBER_Q / 2) / KYBER_Q) & 1; + // t += ((int16_t)t >> 15) & KYBER_Q; + // t = (((t << 1) + KYBER_Q/2)/KYBER_Q) & 1; + t <<= 1; + t += 1665; + t *= 80635; + t >>= 28; + t &= 1; msg[i] |= t << j; } } diff --git a/src/kem/kyber/oldpqclean_kyber768_aarch64/poly.c b/src/kem/kyber/oldpqclean_kyber768_aarch64/poly.c index dffc655913..fcfceddd83 100644 --- a/src/kem/kyber/oldpqclean_kyber768_aarch64/poly.c +++ b/src/kem/kyber/oldpqclean_kyber768_aarch64/poly.c @@ -194,14 +194,19 @@ void poly_frommsg(int16_t r[KYBER_N], const uint8_t msg[KYBER_INDCPA_MSGBYTES]) **************************************************/ void poly_tomsg(uint8_t msg[KYBER_INDCPA_MSGBYTES], const int16_t a[KYBER_N]) { unsigned int i, j; - uint16_t t; + uint32_t t; for (i = 0; i < KYBER_N / 8; i++) { msg[i] = 0; for (j = 0; j < 8; j++) { t = a[8 * i + j]; - t += ((int16_t)t >> 15) & KYBER_Q; - t = (((t << 1) + KYBER_Q / 2) / KYBER_Q) & 1; + // t += ((int16_t)t >> 15) & KYBER_Q; + // t = (((t << 1) + KYBER_Q/2)/KYBER_Q) & 1; + t <<= 1; + t += 1665; + t *= 80635; + t >>= 28; + t &= 1; msg[i] |= t << j; } }