Skip to content

Commit 5132a17

Browse files
authored
Merge pull request #7613 from SparkiDev/kyber_fixes_2
Kyber: fix kyber_from_msg()
2 parents b7d0c25 + df44fac commit 5132a17

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

wolfcrypt/src/wc_kyber.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@
5959

6060
/******************************************************************************/
6161

62+
/* Declare variable to make compiler not optimize code in kyber_from_msg(). */
63+
volatile sword16 kyber_opt_blocker = 0;
64+
65+
/******************************************************************************/
66+
6267
/**
6368
* Initialize the Kyber key.
6469
*

wolfcrypt/src/wc_kyber_poly.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434

3535
#ifdef WOLFSSL_WC_KYBER
3636

37+
/* Declared in wc_kyber.c to stop compiler optimizer from simplifying. */
38+
extern volatile sword16 kyber_opt_blocker;
39+
3740
#ifdef USE_INTEL_SPEEDUP
3841
static word32 cpuid_flags = 0;
3942
#endif
@@ -2773,14 +2776,17 @@ void kyber_decompress_5(sword16* p, const unsigned char* b)
27732776
/* Convert bit from byte to 0 or (KYBER_Q + 1) / 2.
27742777
*
27752778
* Constant time implementation.
2779+
* XOR in kyber_opt_blocker to ensure optimizer doesn't know what will be ANDed
2780+
* with KYBER_Q_1_HALF and can't optimize to non-constant time code.
27762781
*
27772782
* @param [out] p Polynomial to hold converted value.
27782783
* @param [in] msg Message to get bit from byte from.
27792784
* @param [in] i Index of byte from message.
27802785
* @param [in] j Index of bit in byte.
27812786
*/
27822787
#define FROM_MSG_BIT(p, msg, i, j) \
2783-
p[8 * (i) + (j)] = ((sword16)0 - (sword16)(((msg)[i] >> (j)) & 1)) & KYBER_Q_1_HALF
2788+
(p)[8 * (i) + (j)] = (((sword16)0 - (sword16)(((msg)[i] >> (j)) & 1)) ^ \
2789+
kyber_opt_blocker) & KYBER_Q_1_HALF
27842790

27852791
/* Convert message to polynomial.
27862792
*

0 commit comments

Comments
 (0)