Skip to content

Commit 272125f

Browse files
committed
Removed "/KYBER_Q" in poly_compress and polyvec_compress; thanks to Prasanna Ravi and Matthias Kannwischer for pointing out that a DIV instruction could turn into a plaintext-checking oracle
1 parent dda29cc commit 272125f

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

Diff for: ref/poly.c

+13-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ void poly_compress(uint8_t r[KYBER_POLYCOMPRESSEDBYTES], const poly *a)
1919
{
2020
unsigned int i,j;
2121
int16_t u;
22+
uint32_t d0;
2223
uint8_t t[8];
2324

2425
#if (KYBER_POLYCOMPRESSEDBYTES == 128)
@@ -27,7 +28,12 @@ void poly_compress(uint8_t r[KYBER_POLYCOMPRESSEDBYTES], const poly *a)
2728
// map to positive standard representatives
2829
u = a->coeffs[8*i+j];
2930
u += (u >> 15) & KYBER_Q;
30-
t[j] = ((((uint16_t)u << 4) + KYBER_Q/2)/KYBER_Q) & 15;
31+
/* t[j] = ((((uint16_t)u << 4) + KYBER_Q/2)/KYBER_Q) & 15; */
32+
d0 = u << 4;
33+
d0 += 1665;
34+
d0 *= 80635;
35+
d0 >>= 28;
36+
t[j] = d0 & 0xf;
3137
}
3238

3339
r[0] = t[0] | (t[1] << 4);
@@ -42,7 +48,12 @@ void poly_compress(uint8_t r[KYBER_POLYCOMPRESSEDBYTES], const poly *a)
4248
// map to positive standard representatives
4349
u = a->coeffs[8*i+j];
4450
u += (u >> 15) & KYBER_Q;
45-
t[j] = ((((uint32_t)u << 5) + KYBER_Q/2)/KYBER_Q) & 31;
51+
/* t[j] = ((((uint32_t)u << 5) + KYBER_Q/2)/KYBER_Q) & 31; */
52+
d0 = u << 5;
53+
d0 += 1664;
54+
d0 *= 40318;
55+
d0 >>= 27;
56+
t[j] = d0 & 0x1f;
4657
}
4758

4859
r[0] = (t[0] >> 0) | (t[1] << 5);

Diff for: ref/polyvec.c

+15-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
void polyvec_compress(uint8_t r[KYBER_POLYVECCOMPRESSEDBYTES], const polyvec *a)
1616
{
1717
unsigned int i,j,k;
18+
uint64_t d0;
1819

1920
#if (KYBER_POLYVECCOMPRESSEDBYTES == (KYBER_K * 352))
2021
uint16_t t[8];
@@ -23,7 +24,13 @@ void polyvec_compress(uint8_t r[KYBER_POLYVECCOMPRESSEDBYTES], const polyvec *a)
2324
for(k=0;k<8;k++) {
2425
t[k] = a->vec[i].coeffs[8*j+k];
2526
t[k] += ((int16_t)t[k] >> 15) & KYBER_Q;
26-
t[k] = ((((uint32_t)t[k] << 11) + KYBER_Q/2)/KYBER_Q) & 0x7ff;
27+
/* t[k] = ((((uint32_t)t[k] << 11) + KYBER_Q/2)/KYBER_Q) & 0x7ff; */
28+
d0 = t[k];
29+
d0 <<= 11;
30+
d0 += 1664;
31+
d0 *= 645084;
32+
d0 >>= 31;
33+
t[k] = d0 & 0x7ff;
2734
}
2835

2936
r[ 0] = (t[0] >> 0);
@@ -47,7 +54,13 @@ void polyvec_compress(uint8_t r[KYBER_POLYVECCOMPRESSEDBYTES], const polyvec *a)
4754
for(k=0;k<4;k++) {
4855
t[k] = a->vec[i].coeffs[4*j+k];
4956
t[k] += ((int16_t)t[k] >> 15) & KYBER_Q;
50-
t[k] = ((((uint32_t)t[k] << 10) + KYBER_Q/2)/ KYBER_Q) & 0x3ff;
57+
/* t[k] = ((((uint32_t)t[k] << 10) + KYBER_Q/2)/ KYBER_Q) & 0x3ff; */
58+
d0 = t[k];
59+
d0 <<= 10;
60+
d0 += 1665;
61+
d0 *= 1290167;
62+
d0 >>= 32;
63+
t[k] = d0 & 0x3ff;
5164
}
5265

5366
r[0] = (t[0] >> 0);

Diff for: ref/test_kyber.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#define NTESTS 1000
88

9-
static int test_keys()
9+
static int test_keys(void)
1010
{
1111
uint8_t pk[CRYPTO_PUBLICKEYBYTES];
1212
uint8_t sk[CRYPTO_SECRETKEYBYTES];
@@ -31,7 +31,7 @@ static int test_keys()
3131
return 0;
3232
}
3333

34-
static int test_invalid_sk_a()
34+
static int test_invalid_sk_a(void)
3535
{
3636
uint8_t pk[CRYPTO_PUBLICKEYBYTES];
3737
uint8_t sk[CRYPTO_SECRETKEYBYTES];
@@ -59,7 +59,7 @@ static int test_invalid_sk_a()
5959
return 0;
6060
}
6161

62-
static int test_invalid_ciphertext()
62+
static int test_invalid_ciphertext(void)
6363
{
6464
uint8_t pk[CRYPTO_PUBLICKEYBYTES];
6565
uint8_t sk[CRYPTO_SECRETKEYBYTES];

Diff for: ref/test_speed.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
uint64_t t[NTESTS];
1717
uint8_t seed[KYBER_SYMBYTES] = {0};
1818

19-
int main()
19+
int main(void)
2020
{
2121
unsigned int i;
2222
uint8_t pk[CRYPTO_PUBLICKEYBYTES];

Diff for: runtests.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ fi
1111

1212
if [ "$ARCH" = "amd64" -o "$ARCH" = "arm64" ]; then
1313
export CC="clang"
14-
export CFLAGS="-fsanitize=address,undefined ${CFLAGS}"
14+
# export CFLAGS="-fsanitize=address,undefined ${CFLAGS}"
1515
fi
1616

1717
for dir in $DIRS; do
1818
make -j$(nproc) -C $dir
1919
for alg in 512 768 1024 512-90s 768-90s 1024-90s; do
20-
#valgrind --vex-guest-max-insns=25 ./$dir/test_kyber$alg
20+
valgrind --vex-guest-max-insns=25 ./$dir/test_kyber$alg
2121
./$dir/test_kyber$alg &
2222
PID1=$!
2323
./$dir/test_kex$alg &

0 commit comments

Comments
 (0)