d1ed09
From 440a178c5aad19050a3d5b5d76881931138af680 Mon Sep 17 00:00:00 2001
d1ed09
From: Colin Walters <walters@verbum.org>
d1ed09
Date: Fri, 7 Jun 2019 18:44:43 +0000
d1ed09
Subject: [PATCH 1/2] ghmac: Split off wrapper functions into ghmac-utils.c
d1ed09
d1ed09
Prep for adding a GnuTLS HMAC implementation; these are just
d1ed09
utility functions that call the "core" API.
d1ed09
---
d1ed09
 glib/Makefile.am   |   1 +
d1ed09
 glib/ghmac-utils.c | 145 +++++++++++++++++++++++++++++++++++++++++++++
d1ed09
 glib/ghmac.c       | 112 ----------------------------------
d1ed09
 glib/meson.build   |   1 +
d1ed09
 4 files changed, 147 insertions(+), 112 deletions(-)
d1ed09
 create mode 100644 glib/ghmac-utils.c
d1ed09
d1ed09
diff --git a/glib/Makefile.am b/glib/Makefile.am
d1ed09
index 8da549c7f..c367b09ad 100644
d1ed09
--- a/glib/Makefile.am
d1ed09
+++ b/glib/Makefile.am
d1ed09
@@ -126,6 +126,7 @@ libglib_2_0_la_SOURCES = 	\
d1ed09
 	ggettext.c		\
d1ed09
 	ghash.c			\
d1ed09
 	ghmac.c			\
d1ed09
+	ghmac-utils.c		\
d1ed09
 	ghook.c			\
d1ed09
 	ghostutils.c		\
d1ed09
 	giochannel.c    	\
d1ed09
diff --git a/glib/ghmac-utils.c b/glib/ghmac-utils.c
d1ed09
new file mode 100644
d1ed09
index 000000000..a17359ff1
d1ed09
--- /dev/null
d1ed09
+++ b/glib/ghmac-utils.c
d1ed09
@@ -0,0 +1,145 @@
d1ed09
+/* ghmac.h - data hashing functions
d1ed09
+ *
d1ed09
+ * Copyright (C) 2011  Collabora Ltd.
d1ed09
+ * Copyright (C) 2019  Red Hat, Inc.
d1ed09
+ *
d1ed09
+ * This library is free software; you can redistribute it and/or
d1ed09
+ * modify it under the terms of the GNU Lesser General Public
d1ed09
+ * License as published by the Free Software Foundation; either
d1ed09
+ * version 2.1 of the License, or (at your option) any later version.
d1ed09
+ *
d1ed09
+ * This library is distributed in the hope that it will be useful,
d1ed09
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
d1ed09
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d1ed09
+ * Lesser General Public License for more details.
d1ed09
+ *
d1ed09
+ * You should have received a copy of the GNU Lesser General Public License
d1ed09
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
d1ed09
+ */
d1ed09
+
d1ed09
+#include "config.h"
d1ed09
+
d1ed09
+#include <string.h>
d1ed09
+
d1ed09
+#include "ghmac.h"
d1ed09
+
d1ed09
+#include "glib/galloca.h"
d1ed09
+#include "gatomic.h"
d1ed09
+#include "gslice.h"
d1ed09
+#include "gmem.h"
d1ed09
+#include "gstrfuncs.h"
d1ed09
+#include "gtestutils.h"
d1ed09
+#include "gtypes.h"
d1ed09
+#include "glibintl.h"
d1ed09
+
d1ed09
+/**
d1ed09
+ * g_compute_hmac_for_data:
d1ed09
+ * @digest_type: a #GChecksumType to use for the HMAC
d1ed09
+ * @key: (array length=key_len): the key to use in the HMAC
d1ed09
+ * @key_len: the length of the key
d1ed09
+ * @data: (array length=length): binary blob to compute the HMAC of
d1ed09
+ * @length: length of @data
d1ed09
+ *
d1ed09
+ * Computes the HMAC for a binary @data of @length. This is a
d1ed09
+ * convenience wrapper for g_hmac_new(), g_hmac_get_string()
d1ed09
+ * and g_hmac_unref().
d1ed09
+ *
d1ed09
+ * The hexadecimal string returned will be in lower case.
d1ed09
+ *
d1ed09
+ * Returns: the HMAC of the binary data as a string in hexadecimal.
d1ed09
+ *   The returned string should be freed with g_free() when done using it.
d1ed09
+ *
d1ed09
+ * Since: 2.30
d1ed09
+ */
d1ed09
+gchar *
d1ed09
+g_compute_hmac_for_data (GChecksumType  digest_type,
d1ed09
+                         const guchar  *key,
d1ed09
+                         gsize          key_len,
d1ed09
+                         const guchar  *data,
d1ed09
+                         gsize          length)
d1ed09
+{
d1ed09
+  GHmac *hmac;
d1ed09
+  gchar *retval;
d1ed09
+
d1ed09
+  g_return_val_if_fail (length == 0 || data != NULL, NULL);
d1ed09
+
d1ed09
+  hmac = g_hmac_new (digest_type, key, key_len);
d1ed09
+  if (!hmac)
d1ed09
+    return NULL;
d1ed09
+
d1ed09
+  g_hmac_update (hmac, data, length);
d1ed09
+  retval = g_strdup (g_hmac_get_string (hmac));
d1ed09
+  g_hmac_unref (hmac);
d1ed09
+
d1ed09
+  return retval;
d1ed09
+}
d1ed09
+
d1ed09
+/**
d1ed09
+ * g_compute_hmac_for_bytes:
d1ed09
+ * @digest_type: a #GChecksumType to use for the HMAC
d1ed09
+ * @key: the key to use in the HMAC
d1ed09
+ * @data: binary blob to compute the HMAC of
d1ed09
+ *
d1ed09
+ * Computes the HMAC for a binary @data. This is a
d1ed09
+ * convenience wrapper for g_hmac_new(), g_hmac_get_string()
d1ed09
+ * and g_hmac_unref().
d1ed09
+ *
d1ed09
+ * The hexadecimal string returned will be in lower case.
d1ed09
+ *
d1ed09
+ * Returns: the HMAC of the binary data as a string in hexadecimal.
d1ed09
+ *   The returned string should be freed with g_free() when done using it.
d1ed09
+ *
d1ed09
+ * Since: 2.50
d1ed09
+ */
d1ed09
+gchar *
d1ed09
+g_compute_hmac_for_bytes (GChecksumType  digest_type,
d1ed09
+                          GBytes        *key,
d1ed09
+                          GBytes        *data)
d1ed09
+{
d1ed09
+  gconstpointer byte_data;
d1ed09
+  gsize length;
d1ed09
+  gconstpointer key_data;
d1ed09
+  gsize key_len;
d1ed09
+
d1ed09
+  g_return_val_if_fail (data != NULL, NULL);
d1ed09
+  g_return_val_if_fail (key != NULL, NULL);
d1ed09
+
d1ed09
+  byte_data = g_bytes_get_data (data, &length);
d1ed09
+  key_data = g_bytes_get_data (key, &key_len);
d1ed09
+  return g_compute_hmac_for_data (digest_type, key_data, key_len, byte_data, length);
d1ed09
+}
d1ed09
+
d1ed09
+
d1ed09
+/**
d1ed09
+ * g_compute_hmac_for_string:
d1ed09
+ * @digest_type: a #GChecksumType to use for the HMAC
d1ed09
+ * @key: (array length=key_len): the key to use in the HMAC
d1ed09
+ * @key_len: the length of the key
d1ed09
+ * @str: the string to compute the HMAC for
d1ed09
+ * @length: the length of the string, or -1 if the string is nul-terminated
d1ed09
+ *
d1ed09
+ * Computes the HMAC for a string.
d1ed09
+ *
d1ed09
+ * The hexadecimal string returned will be in lower case.
d1ed09
+ *
d1ed09
+ * Returns: the HMAC as a hexadecimal string.
d1ed09
+ *     The returned string should be freed with g_free()
d1ed09
+ *     when done using it.
d1ed09
+ *
d1ed09
+ * Since: 2.30
d1ed09
+ */
d1ed09
+gchar *
d1ed09
+g_compute_hmac_for_string (GChecksumType  digest_type,
d1ed09
+                           const guchar  *key,
d1ed09
+                           gsize          key_len,
d1ed09
+                           const gchar   *str,
d1ed09
+                           gssize         length)
d1ed09
+{
d1ed09
+  g_return_val_if_fail (length == 0 || str != NULL, NULL);
d1ed09
+
d1ed09
+  if (length < 0)
d1ed09
+    length = strlen (str);
d1ed09
+
d1ed09
+  return g_compute_hmac_for_data (digest_type, key, key_len,
d1ed09
+                                  (const guchar *) str, length);
d1ed09
+}
d1ed09
diff --git a/glib/ghmac.c b/glib/ghmac.c
d1ed09
index 9b58fd81c..7db38e34a 100644
d1ed09
--- a/glib/ghmac.c
d1ed09
+++ b/glib/ghmac.c
d1ed09
@@ -329,115 +329,3 @@ g_hmac_get_digest (GHmac  *hmac,
d1ed09
   g_checksum_update (hmac->digesto, buffer, len);
d1ed09
   g_checksum_get_digest (hmac->digesto, buffer, digest_len);
d1ed09
 }
d1ed09
-
d1ed09
-/**
d1ed09
- * g_compute_hmac_for_data:
d1ed09
- * @digest_type: a #GChecksumType to use for the HMAC
d1ed09
- * @key: (array length=key_len): the key to use in the HMAC
d1ed09
- * @key_len: the length of the key
d1ed09
- * @data: (array length=length): binary blob to compute the HMAC of
d1ed09
- * @length: length of @data
d1ed09
- *
d1ed09
- * Computes the HMAC for a binary @data of @length. This is a
d1ed09
- * convenience wrapper for g_hmac_new(), g_hmac_get_string()
d1ed09
- * and g_hmac_unref().
d1ed09
- *
d1ed09
- * The hexadecimal string returned will be in lower case.
d1ed09
- *
d1ed09
- * Returns: the HMAC of the binary data as a string in hexadecimal.
d1ed09
- *   The returned string should be freed with g_free() when done using it.
d1ed09
- *
d1ed09
- * Since: 2.30
d1ed09
- */
d1ed09
-gchar *
d1ed09
-g_compute_hmac_for_data (GChecksumType  digest_type,
d1ed09
-                         const guchar  *key,
d1ed09
-                         gsize          key_len,
d1ed09
-                         const guchar  *data,
d1ed09
-                         gsize          length)
d1ed09
-{
d1ed09
-  GHmac *hmac;
d1ed09
-  gchar *retval;
d1ed09
-
d1ed09
-  g_return_val_if_fail (length == 0 || data != NULL, NULL);
d1ed09
-
d1ed09
-  hmac = g_hmac_new (digest_type, key, key_len);
d1ed09
-  if (!hmac)
d1ed09
-    return NULL;
d1ed09
-
d1ed09
-  g_hmac_update (hmac, data, length);
d1ed09
-  retval = g_strdup (g_hmac_get_string (hmac));
d1ed09
-  g_hmac_unref (hmac);
d1ed09
-
d1ed09
-  return retval;
d1ed09
-}
d1ed09
-
d1ed09
-/**
d1ed09
- * g_compute_hmac_for_bytes:
d1ed09
- * @digest_type: a #GChecksumType to use for the HMAC
d1ed09
- * @key: the key to use in the HMAC
d1ed09
- * @data: binary blob to compute the HMAC of
d1ed09
- *
d1ed09
- * Computes the HMAC for a binary @data. This is a
d1ed09
- * convenience wrapper for g_hmac_new(), g_hmac_get_string()
d1ed09
- * and g_hmac_unref().
d1ed09
- *
d1ed09
- * The hexadecimal string returned will be in lower case.
d1ed09
- *
d1ed09
- * Returns: the HMAC of the binary data as a string in hexadecimal.
d1ed09
- *   The returned string should be freed with g_free() when done using it.
d1ed09
- *
d1ed09
- * Since: 2.50
d1ed09
- */
d1ed09
-gchar *
d1ed09
-g_compute_hmac_for_bytes (GChecksumType  digest_type,
d1ed09
-                          GBytes        *key,
d1ed09
-                          GBytes        *data)
d1ed09
-{
d1ed09
-  gconstpointer byte_data;
d1ed09
-  gsize length;
d1ed09
-  gconstpointer key_data;
d1ed09
-  gsize key_len;
d1ed09
-
d1ed09
-  g_return_val_if_fail (data != NULL, NULL);
d1ed09
-  g_return_val_if_fail (key != NULL, NULL);
d1ed09
-
d1ed09
-  byte_data = g_bytes_get_data (data, &length);
d1ed09
-  key_data = g_bytes_get_data (key, &key_len);
d1ed09
-  return g_compute_hmac_for_data (digest_type, key_data, key_len, byte_data, length);
d1ed09
-}
d1ed09
-
d1ed09
-
d1ed09
-/**
d1ed09
- * g_compute_hmac_for_string:
d1ed09
- * @digest_type: a #GChecksumType to use for the HMAC
d1ed09
- * @key: (array length=key_len): the key to use in the HMAC
d1ed09
- * @key_len: the length of the key
d1ed09
- * @str: the string to compute the HMAC for
d1ed09
- * @length: the length of the string, or -1 if the string is nul-terminated
d1ed09
- *
d1ed09
- * Computes the HMAC for a string.
d1ed09
- *
d1ed09
- * The hexadecimal string returned will be in lower case.
d1ed09
- *
d1ed09
- * Returns: the HMAC as a hexadecimal string.
d1ed09
- *     The returned string should be freed with g_free()
d1ed09
- *     when done using it.
d1ed09
- *
d1ed09
- * Since: 2.30
d1ed09
- */
d1ed09
-gchar *
d1ed09
-g_compute_hmac_for_string (GChecksumType  digest_type,
d1ed09
-                           const guchar  *key,
d1ed09
-                           gsize          key_len,
d1ed09
-                           const gchar   *str,
d1ed09
-                           gssize         length)
d1ed09
-{
d1ed09
-  g_return_val_if_fail (length == 0 || str != NULL, NULL);
d1ed09
-
d1ed09
-  if (length < 0)
d1ed09
-    length = strlen (str);
d1ed09
-
d1ed09
-  return g_compute_hmac_for_data (digest_type, key, key_len,
d1ed09
-                                  (const guchar *) str, length);
d1ed09
-}
d1ed09
diff --git a/glib/meson.build b/glib/meson.build
d1ed09
index 9df77b6f9..c7f28b5b6 100644
d1ed09
--- a/glib/meson.build
d1ed09
+++ b/glib/meson.build
d1ed09
@@ -138,6 +138,7 @@ glib_sources = files(
d1ed09
   'ggettext.c',
d1ed09
   'ghash.c',
d1ed09
   'ghmac.c',
d1ed09
+  'ghmac-utils.c',
d1ed09
   'ghook.c',
d1ed09
   'ghostutils.c',
d1ed09
   'giochannel.c',
d1ed09
-- 
d1ed09
2.21.0
d1ed09
d1ed09
d1ed09
From 423355787ba9133b310c0b72708024b1428d7d14 Mon Sep 17 00:00:00 2001
d1ed09
From: Colin Walters <walters@verbum.org>
d1ed09
Date: Fri, 7 Jun 2019 19:36:54 +0000
d1ed09
Subject: [PATCH 2/2] Add a gnutls backend for GHmac
d1ed09
d1ed09
For RHEL we want apps to use FIPS-certified crypto libraries,
d1ed09
and HMAC apparently counts as "keyed" and hence needs to
d1ed09
be validated.
d1ed09
d1ed09
Bug: https://bugzilla.redhat.com/show_bug.cgi?id=1630260
d1ed09
Replaces: https://gitlab.gnome.org/GNOME/glib/merge_requests/897
d1ed09
d1ed09
This is a build-time option that backs the GHmac API with GnuTLS.
d1ed09
Most distributors ship glib-networking built with GnuTLS, and
d1ed09
most apps use glib-networking, so this isn't a net-new library
d1ed09
in most cases.
d1ed09
d1ed09
However, a fun wrinkle is that the GnuTLS HMAC API doesn't expose
d1ed09
the necessary bits to implement `g_hmac_copy()`; OpenSSL does.
d1ed09
I chose to just make that abort for now since I didn't find
d1ed09
apps using it.
d1ed09
---
d1ed09
 glib/Makefile.am        |   9 ++-
d1ed09
 glib/gchecksum.c        |   9 +--
d1ed09
 glib/gchecksumprivate.h |  32 +++++++++
d1ed09
 glib/ghmac-gnutls.c     | 151 ++++++++++++++++++++++++++++++++++++++++
d1ed09
 glib/ghmac.c            |   1 +
d1ed09
 glib/meson.build        |  10 ++-
d1ed09
 glib/tests/hmac.c       |   6 ++
d1ed09
 meson.build             |   7 ++
d1ed09
 meson_options.txt       |   5 ++
d1ed09
 9 files changed, 221 insertions(+), 9 deletions(-)
d1ed09
 create mode 100644 glib/gchecksumprivate.h
d1ed09
 create mode 100644 glib/ghmac-gnutls.c
d1ed09
d1ed09
diff --git a/glib/Makefile.am b/glib/Makefile.am
d1ed09
index c367b09ad..b0a721ad0 100644
d1ed09
--- a/glib/Makefile.am
d1ed09
+++ b/glib/Makefile.am
d1ed09
@@ -125,7 +125,7 @@ libglib_2_0_la_SOURCES = 	\
d1ed09
 	gfileutils.c		\
d1ed09
 	ggettext.c		\
d1ed09
 	ghash.c			\
d1ed09
-	ghmac.c			\
d1ed09
+	ghmac-gnutls.c		\
d1ed09
 	ghmac-utils.c		\
d1ed09
 	ghook.c			\
d1ed09
 	ghostutils.c		\
d1ed09
@@ -352,11 +352,14 @@ pcre_lib = pcre/libpcre.la
d1ed09
 pcre_inc =
d1ed09
 endif
d1ed09
 
d1ed09
-libglib_2_0_la_CFLAGS = $(AM_CFLAGS) $(GLIB_HIDDEN_VISIBILITY_CFLAGS) $(LIBSYSTEMD_CFLAGS)
d1ed09
+gnutls_libs = $(shell pkg-config --libs gnutls)
d1ed09
+gnutls_cflags = $(shell pkg-config --cflags gnutls)
d1ed09
+
d1ed09
+libglib_2_0_la_CFLAGS = $(AM_CFLAGS) $(GLIB_HIDDEN_VISIBILITY_CFLAGS) $(LIBSYSTEMD_CFLAGS) $(gnutls_cflags)
d1ed09
 libglib_2_0_la_LIBADD = libcharset/libcharset.la $(printf_la) @GIO@ @GSPAWN@ @PLATFORMDEP@ @ICONV_LIBS@ @G_LIBS_EXTRA@ $(pcre_lib) $(G_THREAD_LIBS_EXTRA) $(G_THREAD_LIBS_FOR_GTHREAD) $(LIBSYSTEMD_LIBS)
d1ed09
 libglib_2_0_la_DEPENDENCIES = libcharset/libcharset.la $(printf_la) @GIO@ @GSPAWN@ @PLATFORMDEP@ $(glib_win32_res) $(glib_def)
d1ed09
 
d1ed09
-libglib_2_0_la_LDFLAGS = $(GLIB_LINK_FLAGS) \
d1ed09
+libglib_2_0_la_LDFLAGS = $(GLIB_LINK_FLAGS) $(gnutls_libs) \
d1ed09
 	 $(glib_win32_res_ldflag) \
d1ed09
 	-version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE) \
d1ed09
 	-export-dynamic $(no_undefined)
d1ed09
diff --git a/glib/gchecksum.c b/glib/gchecksum.c
d1ed09
index 40b1d50e2..2f59d4a66 100644
d1ed09
--- a/glib/gchecksum.c
d1ed09
+++ b/glib/gchecksum.c
d1ed09
@@ -20,7 +20,7 @@
d1ed09
 
d1ed09
 #include <string.h>
d1ed09
 
d1ed09
-#include "gchecksum.h"
d1ed09
+#include "gchecksumprivate.h"
d1ed09
 
d1ed09
 #include "gslice.h"
d1ed09
 #include "gmem.h"
d1ed09
@@ -173,9 +173,9 @@ sha_byte_reverse (guint32 *buffer,
d1ed09
 }
d1ed09
 #endif /* G_BYTE_ORDER == G_BIG_ENDIAN */
d1ed09
 
d1ed09
-static gchar *
d1ed09
-digest_to_string (guint8 *digest,
d1ed09
-                  gsize   digest_len)
d1ed09
+gchar *
d1ed09
+gchecksum_digest_to_string (guint8 *digest,
d1ed09
+                            gsize   digest_len)
d1ed09
 {
d1ed09
   gint len = digest_len * 2;
d1ed09
   gint i;
d1ed09
@@ -195,6 +195,7 @@ digest_to_string (guint8 *digest,
d1ed09
 
d1ed09
   return retval;
d1ed09
 }
d1ed09
+#define digest_to_string gchecksum_digest_to_string
d1ed09
 
d1ed09
 /*
d1ed09
  * MD5 Checksum
d1ed09
diff --git a/glib/gchecksumprivate.h b/glib/gchecksumprivate.h
d1ed09
new file mode 100644
d1ed09
index 000000000..86c7a3b61
d1ed09
--- /dev/null
d1ed09
+++ b/glib/gchecksumprivate.h
d1ed09
@@ -0,0 +1,32 @@
d1ed09
+/* gstdioprivate.h - Private GLib stdio functions
d1ed09
+ *
d1ed09
+ * Copyright 2017 Руслан Ижбулатов
d1ed09
+ *
d1ed09
+ * This library is free software; you can redistribute it and/or
d1ed09
+ * modify it under the terms of the GNU Lesser General Public
d1ed09
+ * License as published by the Free Software Foundation; either
d1ed09
+ * version 2.1 of the License, or (at your option) any later version.
d1ed09
+ *
d1ed09
+ * This library is distributed in the hope that it will be useful,
d1ed09
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
d1ed09
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d1ed09
+ * Lesser General Public License for more details.
d1ed09
+ *
d1ed09
+ * You should have received a copy of the GNU Lesser General Public License
d1ed09
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
d1ed09
+ */
d1ed09
+
d1ed09
+#ifndef __G_CHECKSUMPRIVATE_H__
d1ed09
+#define __G_CHECKSUMPRIVATE_H__
d1ed09
+
d1ed09
+#include "gchecksum.h"
d1ed09
+
d1ed09
+G_BEGIN_DECLS
d1ed09
+
d1ed09
+gchar *
d1ed09
+gchecksum_digest_to_string (guint8 *digest,
d1ed09
+                            gsize   digest_len);
d1ed09
+
d1ed09
+G_END_DECLS
d1ed09
+
d1ed09
+#endif
d1ed09
\ No newline at end of file
d1ed09
diff --git a/glib/ghmac-gnutls.c b/glib/ghmac-gnutls.c
d1ed09
new file mode 100644
d1ed09
index 000000000..3b4dfb872
d1ed09
--- /dev/null
d1ed09
+++ b/glib/ghmac-gnutls.c
645344
@@ -0,0 +1,160 @@
d1ed09
+/* ghmac.h - data hashing functions
d1ed09
+ *
d1ed09
+ * Copyright (C) 2011  Collabora Ltd.
d1ed09
+ * Copyright (C) 2019  Red Hat, Inc.
d1ed09
+ *
d1ed09
+ * This library is free software; you can redistribute it and/or
d1ed09
+ * modify it under the terms of the GNU Lesser General Public
d1ed09
+ * License as published by the Free Software Foundation; either
d1ed09
+ * version 2.1 of the License, or (at your option) any later version.
d1ed09
+ *
d1ed09
+ * This library is distributed in the hope that it will be useful,
d1ed09
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
d1ed09
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d1ed09
+ * Lesser General Public License for more details.
d1ed09
+ *
d1ed09
+ * You should have received a copy of the GNU Lesser General Public License
d1ed09
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
d1ed09
+ */
d1ed09
+
d1ed09
+#include "config.h"
d1ed09
+
d1ed09
+#include <string.h>
d1ed09
+#include <gnutls/crypto.h>
d1ed09
+
d1ed09
+#include "ghmac.h"
d1ed09
+
d1ed09
+#include "glib/galloca.h"
d1ed09
+#include "gatomic.h"
d1ed09
+#include "gslice.h"
d1ed09
+#include "gmem.h"
d1ed09
+#include "gstrfuncs.h"
d1ed09
+#include "gchecksumprivate.h"
d1ed09
+#include "gtestutils.h"
d1ed09
+#include "gtypes.h"
d1ed09
+#include "glibintl.h"
d1ed09
+
d1ed09
+struct _GHmac
d1ed09
+{
d1ed09
+  int ref_count;
d1ed09
+  GChecksumType digest_type;
d1ed09
+  gnutls_hmac_hd_t hmac;
d1ed09
+  gchar *digest_str;
d1ed09
+};
d1ed09
+
d1ed09
+GHmac *
d1ed09
+g_hmac_new (GChecksumType  digest_type,
d1ed09
+            const guchar  *key,
d1ed09
+            gsize          key_len)
d1ed09
+{
d1ed09
+  gnutls_mac_algorithm_t algo;
d1ed09
+  GHmac *hmac = g_slice_new0 (GHmac);
d1ed09
+  hmac->ref_count = 1;
d1ed09
+  hmac->digest_type = digest_type;  
d1ed09
+
d1ed09
+  switch (digest_type)
d1ed09
+    {
d1ed09
+    case G_CHECKSUM_MD5:
d1ed09
+      algo = GNUTLS_MAC_MD5;
d1ed09
+      break;
d1ed09
+    case G_CHECKSUM_SHA1:
d1ed09
+      algo = GNUTLS_MAC_SHA1;
d1ed09
+      break;
d1ed09
+    case G_CHECKSUM_SHA256:
d1ed09
+      algo = GNUTLS_MAC_SHA256;
d1ed09
+      break;
d1ed09
+    case G_CHECKSUM_SHA384:
d1ed09
+      algo = GNUTLS_MAC_SHA384;
d1ed09
+      break;
d1ed09
+    case G_CHECKSUM_SHA512:
d1ed09
+      algo = GNUTLS_MAC_SHA512;
d1ed09
+      break;
d1ed09
+    default:
d1ed09
+      g_return_val_if_reached (NULL);
d1ed09
+    }
d1ed09
+
d1ed09
+  gnutls_hmac_init (&hmac->hmac, algo, key, key_len);
d1ed09
+
d1ed09
+  return hmac;
d1ed09
+}
d1ed09
+
d1ed09
+GHmac *
d1ed09
+g_hmac_copy (const GHmac *hmac)
d1ed09
+{
645344
+  GHmac *copy;
645344
+
645344
+  g_return_val_if_fail (hmac != NULL, NULL);
645344
+
645344
+  copy = g_slice_new0 (GHmac);
645344
+  copy->ref_count = 1;
645344
+  copy->digest_type = hmac->digest_type;
645344
+  copy->hmac = gnutls_hmac_copy (hmac->hmac);
645344
+
645344
+  return copy;
d1ed09
+}
d1ed09
+
d1ed09
+GHmac *
d1ed09
+g_hmac_ref (GHmac *hmac)
d1ed09
+{
d1ed09
+  g_return_val_if_fail (hmac != NULL, NULL);
d1ed09
+
d1ed09
+  g_atomic_int_inc (&hmac->ref_count);
d1ed09
+
d1ed09
+  return hmac;
d1ed09
+}
d1ed09
+
d1ed09
+void
d1ed09
+g_hmac_unref (GHmac *hmac)
d1ed09
+{
d1ed09
+  g_return_if_fail (hmac != NULL);
d1ed09
+
d1ed09
+  if (g_atomic_int_dec_and_test (&hmac->ref_count))
d1ed09
+    {
d1ed09
+      gnutls_hmac_deinit (hmac->hmac, NULL);
d1ed09
+      g_free (hmac->digest_str);
d1ed09
+      g_slice_free (GHmac, hmac);
d1ed09
+    }
d1ed09
+}
d1ed09
+
d1ed09
+
d1ed09
+void
d1ed09
+g_hmac_update (GHmac        *hmac,
d1ed09
+               const guchar *data,
d1ed09
+               gssize        length)
d1ed09
+{
d1ed09
+  g_return_if_fail (hmac != NULL);
d1ed09
+  g_return_if_fail (length == 0 || data != NULL);
d1ed09
+
d1ed09
+  gnutls_hmac (hmac->hmac, data, length);
d1ed09
+}
d1ed09
+
d1ed09
+const gchar *
d1ed09
+g_hmac_get_string (GHmac *hmac)
d1ed09
+{
d1ed09
+  guint8 *buffer;
d1ed09
+  gsize digest_len;
d1ed09
+
d1ed09
+  g_return_val_if_fail (hmac != NULL, NULL);
d1ed09
+
d1ed09
+  if (hmac->digest_str)
d1ed09
+    return hmac->digest_str;
d1ed09
+
d1ed09
+  digest_len = g_checksum_type_get_length (hmac->digest_type);
d1ed09
+  buffer = g_alloca (digest_len);
d1ed09
+
d1ed09
+  gnutls_hmac_output (hmac->hmac, buffer);
d1ed09
+  hmac->digest_str = gchecksum_digest_to_string (buffer, digest_len);
d1ed09
+  return hmac->digest_str;
d1ed09
+}
d1ed09
+
d1ed09
+
d1ed09
+void
d1ed09
+g_hmac_get_digest (GHmac  *hmac,
d1ed09
+                   guint8 *buffer,
d1ed09
+                   gsize  *digest_len)
d1ed09
+{
d1ed09
+  g_return_if_fail (hmac != NULL);
d1ed09
+
d1ed09
+  gnutls_hmac_output (hmac->hmac, buffer);
d1ed09
+  *digest_len = g_checksum_type_get_length (hmac->digest_type);
d1ed09
+}
d1ed09
diff --git a/glib/ghmac.c b/glib/ghmac.c
d1ed09
index 7db38e34a..b12eb07c4 100644
d1ed09
--- a/glib/ghmac.c
d1ed09
+++ b/glib/ghmac.c
d1ed09
@@ -33,6 +33,7 @@
d1ed09
 #include "gtypes.h"
d1ed09
 #include "glibintl.h"
d1ed09
 
d1ed09
+#error "build configuration error"
d1ed09
 
d1ed09
 /**
d1ed09
  * SECTION:hmac
d1ed09
diff --git a/glib/meson.build b/glib/meson.build
d1ed09
index c7f28b5b6..a2f9da81c 100644
d1ed09
--- a/glib/meson.build
d1ed09
+++ b/glib/meson.build
d1ed09
@@ -137,7 +137,6 @@ glib_sources = files(
d1ed09
   'gfileutils.c',
d1ed09
   'ggettext.c',
d1ed09
   'ghash.c',
d1ed09
-  'ghmac.c',
d1ed09
   'ghmac-utils.c',
d1ed09
   'ghook.c',
d1ed09
   'ghostutils.c',
d1ed09
@@ -185,6 +184,7 @@ glib_sources = files(
d1ed09
   'gunidecomp.c',
d1ed09
   'gurifuncs.c',
d1ed09
   'gutils.c',
d1ed09
+  'gchecksumprivate.h',
d1ed09
   'guuid.c',
d1ed09
   'gvariant.c',
d1ed09
   'gvariant-core.c',
d1ed09
@@ -222,6 +222,12 @@ else
d1ed09
   glib_dtrace_hdr = []
d1ed09
 endif
d1ed09
 
d1ed09
+if get_option('gnutls')
d1ed09
+  glib_sources += files('ghmac-gnutls.c')
d1ed09
+else
d1ed09
+  glib_sources += files('ghmac.c')
d1ed09
+endif
d1ed09
+
d1ed09
 pcre_static_args = []
d1ed09
 
d1ed09
 if use_pcre_static_flag
d1ed09
@@ -238,7 +244,7 @@ libglib = library('glib-2.0',
d1ed09
   link_args : platform_ldflags + noseh_link_args,
d1ed09
   include_directories : configinc,
d1ed09
   link_with : [charset_lib, gnulib_lib],
d1ed09
-  dependencies : [pcre, thread_dep, libintl, librt] + libiconv + platform_deps,
d1ed09
+  dependencies : [pcre, thread_dep, libintl, librt] + libiconv + platform_deps + libgnutls_dep,
d1ed09
   c_args : ['-DG_LOG_DOMAIN="GLib"', '-DGLIB_COMPILATION'] + pcre_static_args + glib_hidden_visibility_args
d1ed09
 )
d1ed09
 
d1ed09
diff --git a/meson.build b/meson.build
d1ed09
index 0cefee51d..81b16b004 100644
d1ed09
--- a/meson.build
d1ed09
+++ b/meson.build
d1ed09
@@ -1596,6 +1596,13 @@ if host_system == 'linux' and get_option('libmount')
d1ed09
   libmount_dep = [dependency('mount', version : '>=2.23', required : true)]
d1ed09
 endif
d1ed09
 
d1ed09
+# gnutls is used optionally by ghmac
d1ed09
+libgnutls_dep = []
d1ed09
+if get_option('gnutls')
645344
+  libgnutls_dep = [dependency('gnutls', version : '>=3.6.9', required : true)]
d1ed09
+  glib_conf.set('HAVE_GNUTLS', 1)
d1ed09
+endif
d1ed09
+
d1ed09
 if host_system == 'windows'
d1ed09
   winsock2 = cc.find_library('ws2_32')
d1ed09
 endif
d1ed09
diff --git a/meson_options.txt b/meson_options.txt
d1ed09
index 4504c6858..d18c42a36 100644
d1ed09
--- a/meson_options.txt
d1ed09
+++ b/meson_options.txt
d1ed09
@@ -34,6 +34,11 @@ option('libmount',
d1ed09
        value : true,
d1ed09
        description : 'build with libmount support')
d1ed09
 
d1ed09
+option('gnutls',
d1ed09
+       type : 'boolean',
d1ed09
+       value : false,
d1ed09
+       description : 'build with gnutls support')
d1ed09
+
d1ed09
 option('internal_pcre',
d1ed09
        type : 'boolean',
d1ed09
        value : false,
d1ed09
-- 
d1ed09
2.21.0
d1ed09