Blame SOURCES/libgcrypt-1.10.0-fips-x931.patch

e35e9c
From 06ea5b5332ffdb44a0a394d766be8989bcb6a95c Mon Sep 17 00:00:00 2001
e35e9c
From: Jakub Jelen <jjelen@redhat.com>
e35e9c
Date: Tue, 6 Dec 2022 10:03:47 +0900
e35e9c
Subject: [PATCH] fips,rsa: Prevent usage of X9.31 keygen in FIPS mode.
e35e9c
e35e9c
* cipher/rsa.c (rsa_generate): Do not accept use-x931 or derive-parms
e35e9c
in FIPS mode.
e35e9c
* tests/pubkey.c (get_keys_x931_new): Expect failure in FIPS mode.
e35e9c
(check_run): Skip checking X9.31 keys in FIPS mode.
e35e9c
* doc/gcrypt.texi: Document "test-parms" and clarify some cases around
e35e9c
the X9.31 keygen.
e35e9c
e35e9c
--
e35e9c
e35e9c
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
e35e9c
---
e35e9c
 cipher/rsa.c    |  5 +++++
e35e9c
 doc/gcrypt.texi | 41 ++++++++++++++++++++++++++++++++++++-----
e35e9c
 tests/pubkey.c  | 15 +++++++++++++--
e35e9c
 3 files changed, 54 insertions(+), 7 deletions(-)
e35e9c
e35e9c
diff --git a/cipher/rsa.c b/cipher/rsa.c
e35e9c
index df4af94b..45523e6b 100644
e35e9c
--- a/cipher/rsa.c
e35e9c
+++ b/cipher/rsa.c
e35e9c
@@ -1256,6 +1256,11 @@ rsa_generate (const gcry_sexp_t genparms, gcry_sexp_t *r_skey)
e35e9c
   if (deriveparms || (flags & PUBKEY_FLAG_USE_X931))
e35e9c
     {
e35e9c
       int swapped;
e35e9c
+      if (fips_mode ())
e35e9c
+        {
e35e9c
+          sexp_release (deriveparms);
e35e9c
+          return GPG_ERR_INV_SEXP;
e35e9c
+        }
e35e9c
       ec = generate_x931 (&sk, nbits, evalue, deriveparms, &swapped);
e35e9c
       sexp_release (deriveparms);
e35e9c
       if (!ec && swapped)
e35e9c
diff --git a/doc/gcrypt.texi b/doc/gcrypt.texi
e35e9c
index d0372f3e..e845a4dd 100644
e35e9c
--- a/doc/gcrypt.texi
e35e9c
+++ b/doc/gcrypt.texi
e35e9c
@@ -2699,8 +2699,7 @@ achieve fastest ECC key generation.
e35e9c
 Force the use of the ANSI X9.31 key generation algorithm instead of
e35e9c
 the default algorithm. This flag is only meaningful for RSA key
e35e9c
 generation and usually not required.  Note that this algorithm is
e35e9c
-implicitly used if either @code{derive-parms} is given or Libgcrypt is
e35e9c
-in FIPS mode.
e35e9c
+implicitly used if either @code{derive-parms} is given.
e35e9c
 
e35e9c
 @item use-fips186
e35e9c
 @cindex FIPS 186
e35e9c
@@ -3310,9 +3309,9 @@ This is currently only implemented for RSA and DSA keys.  It is not
e35e9c
 allowed to use this together with a @code{domain} specification.  If
e35e9c
 given, it is used to derive the keys using the given parameters.
e35e9c
 
e35e9c
-If given for an RSA key the X9.31 key generation algorithm is used
e35e9c
-even if libgcrypt is not in FIPS mode.  If given for a DSA key, the
e35e9c
-FIPS 186 algorithm is used even if libgcrypt is not in FIPS mode.
e35e9c
+If given for an RSA key, the X9.31 key generation algorithm is used.
e35e9c
+If given for a DSA key, the FIPS 186 algorithm is used even if
e35e9c
+libgcrypt is not in FIPS mode.
e35e9c
 
e35e9c
 @example
e35e9c
 (genkey
e35e9c
@@ -3342,6 +3341,38 @@ FIPS 186 algorithm is used even if libgcrypt is not in FIPS mode.
e35e9c
       (seed @var{seed-mpi}))))
e35e9c
 @end example
e35e9c
 
e35e9c
+@item test-parms @var{list}
e35e9c
+This is currently only implemented for RSA keys. If given, the
e35e9c
+libgcrypt will not generate parameter, but tests whether the p,q is
e35e9c
+probably prime. Returns key with zeroes.
e35e9c
+
e35e9c
+The FIPS key generation algorithm is used even if libgcrypt is not
e35e9c
+in FIPS mode.
e35e9c
+
e35e9c
+@example
e35e9c
+(genkey
e35e9c
+  (rsa
e35e9c
+    (nbits 4:1024)
e35e9c
+    (rsa-use-e 1:3)
e35e9c
+    (test-parms
e35e9c
+      (e "65537")
e35e9c
+      (p #00bbccabcee15d343944a47e492d4b1f4de79633e2
e35e9c
+          0cbb46f7d2d6813392a807ad048cf77528edd19f77
e35e9c
+          e7453f25173b9dcb70423afa2037aae147b81a33d5
e35e9c
+          41fc58f875eff1e852ab55e2e09a3debfbc151b3b0
e35e9c
+          d17fef6f74d81fca14fbae531418e211ef818592af
e35e9c
+          70de5cec3b92795cc3578572bf456099cd8727150e
e35e9c
+          523261#)
e35e9c
+      (q #00ca87ecf2883f4ed00a9ec65abdeba81d28edbfcc
e35e9c
+          34ecc563d587f166b52d42bfbe22bbc095b0b8426a
e35e9c
+          2f8bbc55baaa8859b42cbc376ed3067db3ef7b135b
e35e9c
+          63481322911ebbd7014db83aa051e0ca2dbf302b75
e35e9c
+          cd37f2ae8df90e134226e92f6353a284b28bb30af0
e35e9c
+          bbf925b345b955328379866ebac11d55bc80fe84f1
e35e9c
+          05d415#)
e35e9c
+
e35e9c
+@end example
e35e9c
+
e35e9c
 
e35e9c
 @item flags @var{flaglist}
e35e9c
 This is preferred way to define flags.  @var{flaglist} may contain any
e35e9c
diff --git a/tests/pubkey.c b/tests/pubkey.c
e35e9c
index bc44f3a5..2669b41a 100644
e35e9c
--- a/tests/pubkey.c
e35e9c
+++ b/tests/pubkey.c
e35e9c
@@ -430,7 +430,17 @@ get_keys_x931_new (gcry_sexp_t *pkey, gcry_sexp_t *skey)
e35e9c
   rc = gcry_pk_genkey (&key, key_spec);
e35e9c
   gcry_sexp_release (key_spec);
e35e9c
   if (rc)
e35e9c
-    die ("error generating RSA key: %s\n", gcry_strerror (rc));
e35e9c
+    {
e35e9c
+      if (in_fips_mode)
e35e9c
+        {
e35e9c
+          if (verbose)
e35e9c
+            fprintf (stderr, "The X9.31 RSA keygen is not available in FIPS modee.\n");
e35e9c
+          return;
e35e9c
+        }
e35e9c
+      die ("error generating RSA key: %s\n", gcry_strerror (rc));
e35e9c
+    }
e35e9c
+  else if (in_fips_mode)
e35e9c
+    die ("generating X9.31 RSA key unexpected worked in FIPS mode\n");
e35e9c
 
e35e9c
   if (verbose > 1)
e35e9c
     show_sexp ("generated RSA (X9.31) key:\n", key);
e35e9c
@@ -777,7 +787,8 @@ check_run (void)
e35e9c
   if (verbose)
e35e9c
     fprintf (stderr, "Checking generated RSA key (X9.31).\n");
e35e9c
   get_keys_x931_new (&pkey, &skey);
e35e9c
-  check_keys (pkey, skey, 800, 0);
e35e9c
+  if (!in_fips_mode)
e35e9c
+    check_keys (pkey, skey, 800, 0);
e35e9c
   gcry_sexp_release (pkey);
e35e9c
   gcry_sexp_release (skey);
e35e9c
   pkey = skey = NULL;
e35e9c
-- 
e35e9c
2.39.0
e35e9c