dh.c, rsa.c: use `mpz_powm_sec' for modexps.
authorMark Wooding <mdw@distorted.org.uk>
Wed, 28 Sep 2016 11:01:26 +0000 (12:01 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Wed, 28 Sep 2016 23:47:03 +0000 (00:47 +0100)
commitb6aae8b2364f24edc13c6cdf774bb8c8d282e02f
tree9d11a016a5f3638a2b9eab7fce9fdaded43f58dc
parent889ae0a29d236f8172047ed64a3cf874b4cc77d9
dh.c, rsa.c: use `mpz_powm_sec' for modexps.

This makes an effort to run in constant time.  It's not perfect, but
it's a lot better than nothing.

  * It uses fixed windows from the exponent rather than the traditional
    sliding windows used by `mpz_powm'.  Sliding windows are more
    efficient on low Hamming-weight exponents, which is obviously bad if
    you were hoping for constant-time behaviour.

  * It uses a cache-oblivious algorithm to pick values out of its table
    of small multiples -- i.e., it scans the entire table and plucks out
    the entry it's looking for using bitmasking.  (This is tweakable
    with a macro `WANT_CACHE_SECURITY', but that's hardwired on.)

  * It uses a constant-time conditional subtract to finish up after a
    Montgomery reduction.

On the other hand there are some rough edges.

  * The initial reduction of the exponentiand is done with a non-
    constant-time algorithm.  It /could/ be done a fancy way, using
    Montgomery reduction, but they've not done this yet.

  * I've not looked at the Secnet code to see whether there are other
    timing leaks.

The call in `rsa_sig_check' is safe to leave as the faster `mpz_powm',
since it's verifying a signature which could, in principle, be done by
anyone using only public information.

Signed-off-by: Mark Wooding <mdw@distorted.org.uk>
dh.c
rsa.c