Skip to content

Commit c921ed6

Browse files
authored
Fixed secret-dependent branch in poly_fromsg (#46)
See pq-crystals/kyber@9b8d306 See https://groups.google.com/a/list.nist.gov/g/pqc-forum/c/hqbtIGFKIpU/m/cnE3pbueBgAJ Signed-off-by: Matthias J. Kannwischer <[email protected]>
1 parent cf8f28e commit c921ed6

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

mlkem/poly.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "reduce.h"
77
#include "cbd.h"
88
#include "symmetric.h"
9+
#include "verify.h"
910

1011
/*************************************************
1112
* Name: poly_compress
@@ -433,16 +434,15 @@ void poly_frombytes_basemul_montgomery(poly *r, const poly *b, const unsigned ch
433434
**************************************************/
434435
void poly_frommsg(poly *r, const uint8_t msg[MLKEM_INDCPA_MSGBYTES]) {
435436
unsigned int i, j;
436-
int16_t mask;
437437

438438
#if (MLKEM_INDCPA_MSGBYTES != MLKEM_N/8)
439439
#error "MLKEM_INDCPA_MSGBYTES must be equal to MLKEM_N/8 bytes!"
440440
#endif
441441

442442
for (i = 0; i < MLKEM_N / 8; i++) {
443443
for (j = 0; j < 8; j++) {
444-
mask = -(int16_t)((msg[i] >> j) & 1);
445-
r->coeffs[8 * i + j] = mask & ((MLKEM_Q + 1) / 2);
444+
r->coeffs[8 * i + j] = 0;
445+
cmov_int16(r->coeffs + 8 * i + j, ((MLKEM_Q + 1) / 2), (msg[i] >> j) & 1);
446446
}
447447
}
448448
}

mlkem/verify.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,19 @@ void cmov(uint8_t *r, const uint8_t *x, size_t len, uint8_t b) {
4646
r[i] ^= b & (r[i] ^ x[i]);
4747
}
4848
}
49+
50+
/*************************************************
51+
* Name: cmov_int16
52+
*
53+
* Description: Copy input v to *r if b is 1, don't modify *r if b is 0.
54+
* Requires b to be in {0,1};
55+
* Runs in constant time.
56+
*
57+
* Arguments: int16_t *r: pointer to output int16_t
58+
* int16_t v: input int16_t
59+
* uint8_t b: Condition bit; has to be in {0,1}
60+
**************************************************/
61+
void cmov_int16(int16_t *r, int16_t v, uint16_t b) {
62+
b = -b;
63+
*r ^= b & ((*r) ^ v);
64+
}

mlkem/verify.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@ int verify(const uint8_t *a, const uint8_t *b, size_t len);
1313
#define cmov MLKEM_NAMESPACE(cmov)
1414
void cmov(uint8_t *r, const uint8_t *x, size_t len, uint8_t b);
1515

16+
#define cmov_int16 MLKEM_NAMESPACE(cmov_int16)
17+
void cmov_int16(int16_t *r, int16_t v, uint16_t b);
18+
1619
#endif

0 commit comments

Comments
 (0)