Skip to content

Commit 3d90274

Browse files
peterdettmansipa
authored andcommitted
Reduce side channels from single-bit reads
1 parent 1464b93 commit 3d90274

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

0 commit comments

Comments
 (0)