Blame SOURCES/libgcrypt-1.8.5-elgamal-blinding.patch

5788e9
commit e8b7f10be275bcedb5fc05ed4837a89bfd605c61
5788e9
Author: NIIBE Yutaka <gniibe@fsij.org>
5788e9
Date:   Tue Apr 13 10:00:00 2021 +0900
5788e9
5788e9
    cipher: Hardening ElGamal by introducing exponent blinding too.
5788e9
    
5788e9
    * cipher/elgamal.c (do_encrypt): Also do exponent blinding.
5788e9
    
5788e9
    --
5788e9
    
5788e9
    Base blinding had been introduced with USE_BLINDING.  This patch add
5788e9
    exponent blinding as well to mitigate side-channel attack on mpi_powm.
5788e9
    
5788e9
    GnuPG-bug-id: 5328
5788e9
    Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
5788e9
5788e9
diff --git a/cipher/elgamal.c b/cipher/elgamal.c
5788e9
index 4eb52d62..9835122f 100644
5788e9
--- a/cipher/elgamal.c
5788e9
+++ b/cipher/elgamal.c
5788e9
@@ -522,8 +522,9 @@ do_encrypt(gcry_mpi_t a, gcry_mpi_t b, gcry_mpi_t input, ELG_public_key *pkey )
5788e9
 static void
5788e9
 decrypt (gcry_mpi_t output, gcry_mpi_t a, gcry_mpi_t b, ELG_secret_key *skey )
5788e9
 {
5788e9
-  gcry_mpi_t t1, t2, r;
5788e9
+  gcry_mpi_t t1, t2, r, r1, h;
5788e9
   unsigned int nbits = mpi_get_nbits (skey->p);
5788e9
+  gcry_mpi_t x_blind;
5788e9
 
5788e9
   mpi_normalize (a);
5788e9
   mpi_normalize (b);
5788e9
@@ -534,20 +535,33 @@ decrypt (gcry_mpi_t output, gcry_mpi_t a, gcry_mpi_t b, ELG_secret_key *skey )
5788e9
 
5788e9
   t2 = mpi_snew (nbits);
5788e9
   r  = mpi_new (nbits);
5788e9
+  r1 = mpi_new (nbits);
5788e9
+  h  = mpi_new (nbits);
5788e9
+  x_blind = mpi_snew (nbits);
5788e9
 
5788e9
   /* We need a random number of about the prime size.  The random
5788e9
      number merely needs to be unpredictable; thus we use level 0.  */
5788e9
   _gcry_mpi_randomize (r, nbits, GCRY_WEAK_RANDOM);
5788e9
 
5788e9
+  /* Also, exponent blinding: x_blind = x + (p-1)*r1 */
5788e9
+  _gcry_mpi_randomize (r1, nbits, GCRY_WEAK_RANDOM);
5788e9
+  mpi_set_highbit (r1, nbits - 1);
5788e9
+  mpi_sub_ui (h, skey->p, 1);
5788e9
+  mpi_mul (x_blind, h, r1);
5788e9
+  mpi_add (x_blind, skey->x, x_blind);
5788e9
+
5788e9
   /* t1 = r^x mod p */
5788e9
-  mpi_powm (t1, r, skey->x, skey->p);
5788e9
+  mpi_powm (t1, r, x_blind, skey->p);
5788e9
   /* t2 = (a * r)^-x mod p */
5788e9
   mpi_mulm (t2, a, r, skey->p);
5788e9
-  mpi_powm (t2, t2, skey->x, skey->p);
5788e9
+  mpi_powm (t2, t2, x_blind, skey->p);
5788e9
   mpi_invm (t2, t2, skey->p);
5788e9
   /* t1 = (t1 * t2) mod p*/
5788e9
   mpi_mulm (t1, t1, t2, skey->p);
5788e9
 
5788e9
+  mpi_free (x_blind);
5788e9
+  mpi_free (h);
5788e9
+  mpi_free (r1);
5788e9
   mpi_free (r);
5788e9
   mpi_free (t2);
5788e9