Skip to content

Commit

Permalink
ecc: Add mitigation against timing attack.
Browse files Browse the repository at this point in the history
* cipher/ecc-ecdsa.c (_gcry_ecc_ecdsa_sign): Add the order N to K.
* mpi/ec.c (_gcry_mpi_ec_mul_point): Compute with NBITS of P or larger.

CVE-id: CVE-2019-13627
GnuPG-bug-id: 4626
Co-authored-by: Ján Jančár <johny@neuromancer.sk>
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
  • Loading branch information
NIIBE Yutaka and J08nY committed Aug 7, 2019
1 parent 75c2fbc commit b9577f7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 10 additions & 0 deletions cipher/ecc-ecdsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ _gcry_ecc_ecdsa_sign (gcry_mpi_t input, ECC_secret_key *skey,
else
k = _gcry_dsa_gen_k (skey->E.n, GCRY_STRONG_RANDOM);

/* Originally, ECDSA computation requires k where 0 < k < n.
* Here, we add n (the order of curve), to keep k in a
* range: n < k < 2*n, or, addming more n, keep k in a range:
* 2*n < k < 3*n, so that timing difference of the EC
* multiply operation can be small. The result is same.
*/
mpi_add (k, k, skey->E.n);
if (!mpi_test_bit (k, qbits))
mpi_add (k, k, skey->E.n);

_gcry_mpi_ec_mul_point (&I, k, &skey->E.G, ctx);
if (_gcry_mpi_ec_get_affine (x, NULL, &I, ctx))
{
Expand Down
6 changes: 5 additions & 1 deletion mpi/ec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,11 @@ _gcry_mpi_ec_mul_point (mpi_point_t result,
unsigned int nbits;
int j;

nbits = mpi_get_nbits (scalar);
if (mpi_cmp (scalar, ctx->p) >= 0)
nbits = mpi_get_nbits (scalar);
else
nbits = mpi_get_nbits (ctx->p);

if (ctx->model == MPI_EC_WEIERSTRASS)
{
mpi_set_ui (result->x, 1);
Expand Down

0 comments on commit b9577f7

Please sign in to comment.