Skip to content

Commit 75b1e4c

Browse files
peterdettmansipa
authored andcommitted
Reduce side channels from single-bit reads
1 parent 08dbb45 commit 75b1e4c

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/ecmult_gen_impl.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,15 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
131131
* ((block*COMB_TEETH + tooth)*COMB_SPACING + comb_off) of recoded. */
132132
uint32_t bits = 0, sign, abs, index, tooth;
133133
for (tooth = 0; tooth < COMB_TEETH; ++tooth) {
134-
uint32_t bit = (recoded[bit_pos >> 5] >> (bit_pos & 0x1f)) & 1;
135-
bits |= bit << tooth;
134+
/* Instead of reading individual bits here to construct bits, build up
135+
* the result by xoring shifted reads together. In every iteration, one
136+
* additional bit is made correct, starting at the bottom. The bits
137+
* above that contain junk. This reduces leakage from single bits. See
138+
* https://www.usenix.org/system/files/conference/usenixsecurity18/sec18-alam.pdf
139+
*/
140+
uint32_t bitdata = recoded[bit_pos >> 5] >> (bit_pos & 0x1f);
141+
bits &= ~(1 << tooth);
142+
bits ^= bitdata << tooth;
136143
bit_pos += COMB_SPACING;
137144
}
138145

0 commit comments

Comments
 (0)