OpenCryptoProject / JCMathLib

Implementation of mathematical operations with big numbers and elliptic curve points for smart cards with JavaCard platform.
MIT License
85 stars 29 forks source link

`mult_rsa_trick`: make the add carry bit resolution constant-time #26

Closed lzaoral closed 2 years ago

lzaoral commented 2 years ago

Unfortunately, Java does not support mixing booleans and bytes as operands of bitwise operators. Therefore, the add overload used in mult_rsa_trick was modified to return the byte with the carry bit itself.

Of course, this is at the expense of readability due to the unavoidable casts of intermediate values to shorts or bytes. On the other hand, the timing difference between the executions with or without the usage of the carry bit if branch was measurable.

The change was tested on JCOP3 J3H145 using the enclosed test suite.

Detected-by: JCProfilerNext

Measurements without this patch:

without

Measurements with this patch:

with

/cc @petrs

EDIT: use a similar binsize in both histograms for consistency

lzaoral commented 2 years ago

@dufkan You're welcome and I'm glad you like it!