[PATCH] cipher:rsa: Fix the dead-code of stronger_key_check.

NIIBE Yutaka via Gcrypt-devel <[email protected]> Wed, 18 Mar 2026 14:55:51 +0900
Newsgroups gmane.comp.encryption.gpg.libgcrypt.devel
Message-ID <9a4f7395d4c784d3faba93d4baa97c2d9b5b185f.1773813207.git.gniibe@fsij.org>
This is a multi-part message in MIME format.
--------------2.47.3
Content-Type: text/plain; charset=UTF-8; format=fixed
Content-Transfer-Encoding: 8bit


* cipher/rsa.c (check_secret_key): Rename from stronger_key_check
to be enabled with ENABLE_STRONGER_CHECK.

--

GnuPG-bug-id: 8171
Signed-off-by: NIIBE Yutaka <[email protected]>
---
 cipher/rsa.c | 52 ++++++++++++++++++++++++++++++++++------------------
 1 file changed, 34 insertions(+), 18 deletions(-)


--------------2.47.3
Content-Type: text/x-patch; name="0001-cipher-rsa-Fix-the-dead-code-of-stronger_key_check.patch"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="0001-cipher-rsa-Fix-the-dead-code-of-stronger_key_check.patch"

diff --git a/cipher/rsa.c b/cipher/rsa.c
index a501d7d8..44a44bdc 100644
--- a/cipher/rsa.c
+++ b/cipher/rsa.c
@@ -960,6 +960,9 @@ generate_x931 (RSA_secret_key *sk, unsigned int nbits, unsigned long e_value,
 }
 
 
+/* Uncomment following line to enable stronger check. */
+/* #define ENABLE_STRONGER_CHECK 1 */
+#ifndef ENABLE_STRONGER_CHECK
 /****************
  * Test whether the secret key is valid.
  * Returns: true if this is a valid key.
@@ -975,7 +978,7 @@ check_secret_key( RSA_secret_key *sk )
   mpi_free(temp);
   return !rc;
 }
-
+#endif
 
 
 /****************
@@ -999,66 +1002,79 @@ public(gcry_mpi_t output, gcry_mpi_t input, RSA_public_key *pkey )
     mpi_powm( output, input, pkey->e, pkey->n );
 }
 
-#if 0
-static void
-stronger_key_check ( RSA_secret_key *skey )
+#ifdef ENABLE_STRONGER_CHECK
+/****************
+ * Test whether the secret key is valid.
+ * Returns: true if this is a valid key.
+ */
+static int
+check_secret_key ( RSA_secret_key *skey )
 {
   gcry_mpi_t t = mpi_alloc_secure ( 0 );
   gcry_mpi_t t1 = mpi_alloc_secure ( 0 );
   gcry_mpi_t t2 = mpi_alloc_secure ( 0 );
   gcry_mpi_t phi = mpi_alloc_secure ( 0 );
+  int rc = 1;
 
   /* check that n == p * q */
   mpi_mul( t, skey->p, skey->q);
   if (mpi_cmp( t, skey->n) )
-    log_info ( "RSA Oops: n != p * q\n" );
+    {
+      rc = 0;
+      goto leave;
+    }
 
   /* check that p is less than q */
   if( mpi_cmp( skey->p, skey->q ) > 0 )
     {
-      log_info ("RSA Oops: p >= q - fixed\n");
-      _gcry_mpi_swap ( skey->p, skey->q);
+      rc = 0;
+      goto leave;
     }
 
     /* check that e divides neither p-1 nor q-1 */
     mpi_sub_ui(t, skey->p, 1 );
     mpi_fdiv_r(t, t, skey->e );
     if ( !mpi_cmp_ui( t, 0) )
-        log_info ( "RSA Oops: e divides p-1\n" );
+      {
+        rc = 0;
+        goto leave;
+      }
     mpi_sub_ui(t, skey->q, 1 );
     mpi_fdiv_r(t, t, skey->e );
     if ( !mpi_cmp_ui( t, 0) )
-        log_info ( "RSA Oops: e divides q-1\n" );
+      {
+        rc = 0;
+        goto leave;
+      }
 
     /* check that d is correct */
     mpi_sub_ui( t1, skey->p, 1 );
     mpi_sub_ui( t2, skey->q, 1 );
     mpi_mul( phi, t1, t2 );
-    gcry_mpi_gcd(t, t1, t2);
+    _gcry_mpi_gcd(t, t1, t2);
     mpi_fdiv_q(t, phi, t);
     mpi_invm(t, skey->e, t );
     if ( mpi_cmp(t, skey->d ) )
       {
-        log_info ( "RSA Oops: d is wrong - fixed\n");
-        mpi_set (skey->d, t);
-        log_printmpi ("  fixed d", skey->d);
+        rc = 0;
+        goto leave;
       }
 
     /* check for correctness of u */
     mpi_invm(t, skey->p, skey->q );
     if ( mpi_cmp(t, skey->u ) )
       {
-        log_info ( "RSA Oops: u is wrong - fixed\n");
-        mpi_set (skey->u, t);
-        log_printmpi ("  fixed u", skey->u);
+        rc = 0;
+        goto leave;
       }
 
-    log_info ( "RSA secret key check finished\n");
-
+ leave:
     mpi_free (t);
     mpi_free (t1);
     mpi_free (t2);
     mpi_free (phi);
+
+    return rc;
 }
 #endif
 

--------------2.47.3
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Gcrypt-devel mailing list
[email protected]
https://lists.gnupg.org/mailman/listinfo/gcrypt-devel

--------------2.47.3--