Re: FIPS 140 service indicator revamp

NIIBE Yutaka via Gcrypt-devel <[email protected]>
Newsgroups gmane.comp.encryption.gpg.libgcrypt.devel
Message-ID <[email protected]>
NIIBE Yutaka <[email protected]> wrote:
> I'll continue on gcry_mac_open and gcry_cipher_open API.

Here are the patches for those API.

I'll add tests and continue on gcry_pk_hash_* functions.
--

_______________________________________________
Gcrypt-devel mailing list
[email protected]
https://lists.gnupg.org/mailman/listinfo/gcrypt-devel
0004-fips-mac-Implement-new-FIPS-service-indicator-for-gc.patch (text/x-diff, 3.7 KB)
>From f8c41ebcaaff84c730dc19289fc7bb842771901a Mon Sep 17 00:00:00 2001
From: NIIBE Yutaka <[email protected]>
Date: Fri, 13 Dec 2024 14:25:02 +0900
Subject: [PATCH 4/5] fips,mac: Implement new FIPS service indicator for
 gcry_mac_open.

* src/gcrypt.h.in (GCRY_MAC_FLAG_REJECT_NON_FIPS): New.
* cipher/mac.c (mac_open): Have FLAGS, instead of SECURE.  Reject when
GCRY_MAC_FLAG_REJECT_NON_FIPS, otherwise, mark non compliant.
(_gcry_mac_open): Follow the change.
* src/visibility.c (gcry_mac_open): Add initialization for FIPS
service indicator.
(gcry_mac_setkey): Likewise.  Don't reject but mark.

--

GnuPG-bug-id: 7338
Signed-off-by: NIIBE Yutaka <[email protected]>
---
 cipher/mac.c     | 15 +++++++++++----
 src/gcrypt.h.in  |  3 ++-
 src/visibility.c |  5 +++--
 3 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/cipher/mac.c b/cipher/mac.c
index 128ac53d..0df48fd7 100644
--- a/cipher/mac.c
+++ b/cipher/mac.c
@@ -513,11 +513,13 @@ check_mac_algo (int algorithm)
  * Open a message digest handle for use with algorithm ALGO.
  */
 static gcry_err_code_t
-mac_open (gcry_mac_hd_t * hd, int algo, int secure, gcry_ctx_t ctx)
+mac_open (gcry_mac_hd_t * hd, int algo, unsigned int flags, gcry_ctx_t ctx)
 {
   const gcry_mac_spec_t *spec;
   gcry_err_code_t err;
   gcry_mac_hd_t h;
+  int secure = !!(flags & GCRY_MAC_FLAG_SECURE);
+  int reject_non_fips = !!(flags & GCRY_MAC_FLAG_REJECT_NON_FIPS);
 
   spec = spec_from_algo (algo);
   if (!spec)
@@ -525,7 +527,12 @@ mac_open (gcry_mac_hd_t * hd, int algo, int secure, gcry_ctx_t ctx)
   else if (spec->flags.disabled)
     return GPG_ERR_MAC_ALGO;
   else if (!spec->flags.fips && fips_mode ())
-    return GPG_ERR_MAC_ALGO;
+    {
+      if (reject_non_fips)
+        return GPG_ERR_MAC_ALGO;
+      else
+        fips_service_indicator_mark_non_compliant ();
+    }
   else if (!spec->ops)
     return GPG_ERR_MAC_ALGO;
   else if (!spec->ops->open || !spec->ops->write || !spec->ops->setkey ||
@@ -643,10 +650,10 @@ _gcry_mac_open (gcry_mac_hd_t * h, int algo, unsigned int flags,
   gcry_err_code_t rc;
   gcry_mac_hd_t hd = NULL;
 
-  if ((flags & ~GCRY_MAC_FLAG_SECURE))
+  if ((flags & ~(GCRY_MAC_FLAG_SECURE | GCRY_MAC_FLAG_REJECT_NON_FIPS)))
     rc = GPG_ERR_INV_ARG;
   else
-    rc = mac_open (&hd, algo, !!(flags & GCRY_MAC_FLAG_SECURE), ctx);
+    rc = mac_open (&hd, algo, flags, ctx);
 
   *h = rc ? NULL : hd;
   return rc;
diff --git a/src/gcrypt.h.in b/src/gcrypt.h.in
index 96bf88f6..2a378639 100644
--- a/src/gcrypt.h.in
+++ b/src/gcrypt.h.in
@@ -1560,7 +1560,8 @@ enum gcry_mac_algos
 /* Flags used with the open function.  */
 enum gcry_mac_flags
   {
-    GCRY_MAC_FLAG_SECURE = 1   /* Allocate all buffers in "secure" memory.  */
+    GCRY_MAC_FLAG_SECURE = 1,  /* Allocate all buffers in "secure" memory.  */
+    GCRY_MAC_FLAG_REJECT_NON_FIPS = 2   /* Reject non-FIPS-compliant algo.  */
   };
 
 /* Create a MAC handle for algorithm ALGO.  FLAGS may be given as an bitwise OR
diff --git a/src/visibility.c b/src/visibility.c
index 44b05eb2..7699f14f 100644
--- a/src/visibility.c
+++ b/src/visibility.c
@@ -946,7 +946,7 @@ gcry_mac_open (gcry_mac_hd_t *handle, int algo, unsigned int flags,
       *handle = NULL;
       return gpg_error (fips_not_operational ());
     }
-
+  fips_service_indicator_init ();
   return gpg_error (_gcry_mac_open (handle, algo, flags, ctx));
 }
 
@@ -962,8 +962,9 @@ gcry_mac_setkey (gcry_mac_hd_t hd, const void *key, size_t keylen)
   if (!fips_is_operational ())
     return gpg_error (fips_not_operational ());
 
+  fips_service_indicator_init ();
   if (fips_mode () && keylen < 14)
-    return GPG_ERR_INV_VALUE;
+    fips_service_indicator_mark_non_compliant ();
 
   return gpg_error (_gcry_mac_setkey (hd, key, keylen));
 }
-- 
2.39.5
0005-fips-cipher-Implement-new-FIPS-service-indicator-for.patch (text/x-diff, 3.9 KB)
>From bdf16a38fe5040507a0d7fa56a607b8816dfbec6 Mon Sep 17 00:00:00 2001
From: NIIBE Yutaka <[email protected]>
Date: Fri, 13 Dec 2024 14:40:53 +0900
Subject: [PATCH 5/5] fips,cipher: Implement new FIPS service indicator for
 cipher_open.

* src/gcrypt.h.in (GCRY_CIPHER_FLAG_REJECT_NON_FIPS): New.
* cipher/cipher.c (_gcry_cipher_open_internal): Don't reject
but mark the service indicator in FIPS mode.
(cipher_setkey): Likewise.
* src/visibility.c (gcry_cipher_open): Initialize the service
indicator.
(gcry_cipher_setkey): Likewise.

--

GnuPG-bug-id: 7338
Signed-off-by: NIIBE Yutaka <[email protected]>
---
 cipher/cipher.c  | 23 +++++++++++++++++++----
 src/gcrypt.h.in  |  3 ++-
 src/visibility.c |  4 ++--
 3 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/cipher/cipher.c b/cipher/cipher.c
index 898bb58f..7ffacf05 100644
--- a/cipher/cipher.c
+++ b/cipher/cipher.c
@@ -509,7 +509,8 @@ gcry_err_code_t
 _gcry_cipher_open_internal (gcry_cipher_hd_t *handle,
 			    int algo, int mode, unsigned int flags)
 {
-  int secure = (flags & GCRY_CIPHER_SECURE);
+  int secure = !!(flags & GCRY_CIPHER_SECURE);
+  int reject_non_fips = !!(flags & GCRY_CIPHER_FLAG_REJECT_NON_FIPS);
   gcry_cipher_spec_t *spec;
   gcry_cipher_hd_t h = NULL;
   gcry_err_code_t err;
@@ -524,7 +525,15 @@ _gcry_cipher_open_internal (gcry_cipher_hd_t *handle,
   else if (spec->flags.disabled)
     err = GPG_ERR_CIPHER_ALGO;
   else if (!spec->flags.fips && fips_mode ())
-    err = GPG_ERR_CIPHER_ALGO;
+    {
+      if (reject_non_fips)
+        err = GPG_ERR_CIPHER_ALGO;
+      else
+        {
+          fips_service_indicator_mark_non_compliant ();
+          err = 0;
+        }
+    }
   else
     err = 0;
 
@@ -535,7 +544,8 @@ _gcry_cipher_open_internal (gcry_cipher_hd_t *handle,
 		     | GCRY_CIPHER_ENABLE_SYNC
 		     | GCRY_CIPHER_CBC_CTS
 		     | GCRY_CIPHER_CBC_MAC
-                     | GCRY_CIPHER_EXTENDED))
+                     | GCRY_CIPHER_EXTENDED
+                     | GCRY_CIPHER_FLAG_REJECT_NON_FIPS))
 	  || ((flags & GCRY_CIPHER_CBC_CTS) && (flags & GCRY_CIPHER_CBC_MAC))))
     err = GPG_ERR_CIPHER_ALGO;
 
@@ -765,7 +775,12 @@ cipher_setkey (gcry_cipher_hd_t c, byte *key, size_t keylen)
 	     See "Implementation Guidance for FIPS 140-2, A.9 XTS-AES
 	     Key Generation Requirements" for details.  */
 	  if (buf_eq_const (key, key + keylen, keylen))
-	    return GPG_ERR_WEAK_KEY;
+            {
+              if ((c->flags & GCRY_CIPHER_FLAG_REJECT_NON_FIPS))
+                return GPG_ERR_WEAK_KEY;
+              else
+                fips_service_indicator_mark_non_compliant ();
+            }
 	}
     }
   else if (c->mode == GCRY_CIPHER_MODE_SIV)
diff --git a/src/gcrypt.h.in b/src/gcrypt.h.in
index 2a378639..2ed9914b 100644
--- a/src/gcrypt.h.in
+++ b/src/gcrypt.h.in
@@ -992,7 +992,8 @@ enum gcry_cipher_flags
     GCRY_CIPHER_ENABLE_SYNC = 2,  /* Enable CFB sync mode. */
     GCRY_CIPHER_CBC_CTS     = 4,  /* Enable CBC cipher text stealing (CTS). */
     GCRY_CIPHER_CBC_MAC     = 8,  /* Enable CBC message auth. code (MAC).  */
-    GCRY_CIPHER_EXTENDED    = 16  /* Enable extended AES-WRAP.  */
+    GCRY_CIPHER_EXTENDED    = 16, /* Enable extended AES-WRAP.  */
+    GCRY_CIPHER_FLAG_REJECT_NON_FIPS = 32 /* Reject non-FIPS-compliant algo.  */
   };
 
 /* Methods used for AEAD IV generation. */
diff --git a/src/visibility.c b/src/visibility.c
index 7699f14f..d219f1a6 100644
--- a/src/visibility.c
+++ b/src/visibility.c
@@ -736,7 +736,7 @@ gcry_cipher_open (gcry_cipher_hd_t *handle,
       *handle = NULL;
       return gpg_error (fips_not_operational ());
     }
-
+  fips_service_indicator_init ();
   return gpg_error (_gcry_cipher_open (handle, algo, mode, flags));
 }
 
@@ -751,7 +751,7 @@ gcry_cipher_setkey (gcry_cipher_hd_t hd, const void *key, size_t keylen)
 {
   if (!fips_is_operational ())
     return gpg_error (fips_not_operational ());
-
+  fips_service_indicator_init ();
   return gcry_error (_gcry_cipher_setkey (hd, key, keylen));
 }
 
-- 
2.39.5
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.