Blame SOURCES/libgcrypt-1.9.3-CVE-2021-40528.patch

9925f7
commit 3462280f2e23e16adf3ed5176e0f2413d8861320
9925f7
Author: NIIBE Yutaka <gniibe@fsij.org>
9925f7
Date:   Fri May 21 11:15:07 2021 +0900
9925f7
9925f7
    cipher: Fix ElGamal encryption for other implementations.
9925f7
    
9925f7
    * cipher/elgamal.c (gen_k): Remove support of smaller K.
9925f7
    (do_encrypt): Never use smaller K.
9925f7
    (sign): Folllow the change of gen_k.
9925f7
    
9925f7
    --
9925f7
    
9925f7
    Cherry-pick master commit of:
9925f7
            632d80ef30e13de6926d503aa697f92b5dbfbc5e
9925f7
    
9925f7
    This change basically reverts encryption changes in two commits:
9925f7
    
9925f7
            74386120dad6b3da62db37f7044267c8ef34689b
9925f7
            78531373a342aeb847950f404343a05e36022065
9925f7
    
9925f7
    Use of smaller K for ephemeral key in ElGamal encryption is only good,
9925f7
    when we can guarantee that recipient's key is generated by our
9925f7
    implementation (or compatible).
9925f7
    
9925f7
    For detail, please see:
9925f7
    
9925f7
        Luca De Feo, Bertram Poettering, Alessandro Sorniotti,
9925f7
        "On the (in)security of ElGamal in OpenPGP";
9925f7
        in the proceedings of  CCS'2021.
9925f7
    
9925f7
    CVE-id: CVE-2021-33560
9925f7
    GnuPG-bug-id: 5328
9925f7
    Suggested-by: Luca De Feo, Bertram Poettering, Alessandro Sorniotti
9925f7
    Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
9925f7
9925f7
diff --git a/cipher/elgamal.c b/cipher/elgamal.c
9925f7
index 9835122f..eead4502 100644
9925f7
--- a/cipher/elgamal.c
9925f7
+++ b/cipher/elgamal.c
9925f7
@@ -66,7 +66,7 @@ static const char *elg_names[] =
9925f7
 
9925f7
 
9925f7
 static int test_keys (ELG_secret_key *sk, unsigned int nbits, int nodie);
9925f7
-static gcry_mpi_t gen_k (gcry_mpi_t p, int small_k);
9925f7
+static gcry_mpi_t gen_k (gcry_mpi_t p);
9925f7
 static gcry_err_code_t generate (ELG_secret_key *sk, unsigned nbits,
9925f7
                                  gcry_mpi_t **factors);
9925f7
 static int  check_secret_key (ELG_secret_key *sk);
9925f7
@@ -189,11 +189,10 @@ test_keys ( ELG_secret_key *sk, unsigned int nbits, int nodie )
9925f7
 
9925f7
 /****************
9925f7
  * Generate a random secret exponent k from prime p, so that k is
9925f7
- * relatively prime to p-1.  With SMALL_K set, k will be selected for
9925f7
- * better encryption performance - this must never be used signing!
9925f7
+ * relatively prime to p-1.
9925f7
  */
9925f7
 static gcry_mpi_t
9925f7
-gen_k( gcry_mpi_t p, int small_k )
9925f7
+gen_k( gcry_mpi_t p )
9925f7
 {
9925f7
   gcry_mpi_t k = mpi_alloc_secure( 0 );
9925f7
   gcry_mpi_t temp = mpi_alloc( mpi_get_nlimbs(p) );
9925f7
@@ -202,18 +201,7 @@ gen_k( gcry_mpi_t p, int small_k )
9925f7
   unsigned int nbits, nbytes;
9925f7
   char *rndbuf = NULL;
9925f7
 
9925f7
-  if (small_k)
9925f7
-    {
9925f7
-      /* Using a k much lesser than p is sufficient for encryption and
9925f7
-       * it greatly improves the encryption performance.  We use
9925f7
-       * Wiener's table and add a large safety margin. */
9925f7
-      nbits = wiener_map( orig_nbits ) * 3 / 2;
9925f7
-      if( nbits >= orig_nbits )
9925f7
-        BUG();
9925f7
-    }
9925f7
-  else
9925f7
-    nbits = orig_nbits;
9925f7
-
9925f7
+  nbits = orig_nbits;
9925f7
 
9925f7
   nbytes = (nbits+7)/8;
9925f7
   if( DBG_CIPHER )
9925f7
@@ -492,7 +480,7 @@ do_encrypt(gcry_mpi_t a, gcry_mpi_t b, gcry_mpi_t input, ELG_public_key *pkey )
9925f7
    * error code.
9925f7
    */
9925f7
 
9925f7
-  k = gen_k( pkey->p, 1 );
9925f7
+  k = gen_k( pkey->p );
9925f7
   mpi_powm (a, pkey->g, k, pkey->p);
9925f7
 
9925f7
   /* b = (y^k * input) mod p
9925f7
@@ -608,7 +596,7 @@ sign(gcry_mpi_t a, gcry_mpi_t b, gcry_mpi_t input, ELG_secret_key *skey )
9925f7
     *
9925f7
     */
9925f7
     mpi_sub_ui(p_1, p_1, 1);
9925f7
-    k = gen_k( skey->p, 0 /* no small K ! */ );
9925f7
+    k = gen_k( skey->p );
9925f7
     mpi_powm( a, skey->g, k, skey->p );
9925f7
     mpi_mul(t, skey->x, a );
9925f7
     mpi_subm(t, input, t, p_1 );