Skip to content

Commit 80ba4c5

Browse files
peterdettmansipa
authored andcommitted
Reduce side channels from single-bit reads
1 parent c727086 commit 80ba4c5

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
@@ -152,8 +152,15 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
152152
* ((block*COMB_TEETH + tooth)*COMB_SPACING + comb_off) of recoded. */
153153
uint32_t bits = 0, sign, abs, index, tooth;
154154
for (tooth = 0; tooth < COMB_TEETH; ++tooth) {
155-
uint32_t bit = (recoded[bit_pos >> 5] >> (bit_pos & 0x1f)) & 1;
156-
bits |= bit << tooth;
155+
/* Instead of reading individual bits here to construct bits, build up
156+
* the result by xoring shifted reads together. In every iteration, one
157+
* additional bit is made correct, starting at the bottom. The bits
158+
* above that contain junk. This reduces leakage from single bits. See
159+
* https://www.usenix.org/system/files/conference/usenixsecurity18/sec18-alam.pdf
160+
*/
161+
uint32_t bitdata = recoded[bit_pos >> 5] >> (bit_pos & 0x1f);
162+
bits &= ~(1 << tooth);
163+
bits ^= bitdata << tooth;
157164
bit_pos += COMB_SPACING;
158165
}
159166

0 commit comments

Comments
 (0)