unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#65826: [PATCH] Add nonce support for gnutls-hash-mac
@ 2023-09-08 15:58 SK Kim
  2023-09-08 16:18 ` Stefan Kangas
  0 siblings, 1 reply; 6+ messages in thread
From: SK Kim @ 2023-09-08 15:58 UTC (permalink / raw)
  To: 65826


[-- Attachment #1.1: Type: text/plain, Size: 629 bytes --]

Hi,

As far as I understood, currently `gnutls-hash-mac' does not support nonce
input, so there is no way to properly hash with some MAC algorithms which
require nonce. (e.g AES-GMAC-128)
So I suggest adding an optional argument NONCE to `gnutls-hash-mac' to
support MAC algorithms with nonce.

What I have tested after applying the attached patch are as below.
1. AES-GMC-128/192/256 works correctly.
2. NONCE does not affect SHA256/SHA512 hash results, even if presented.

Since NONCE is added as an optional argument, I believe it will not even
affect existing code using the 'gnutls-hash-mac' function.

Thanks.

Seungki Kim

[-- Attachment #1.2: Type: text/html, Size: 783 bytes --]

[-- Attachment #2: 0001-add-nonce-support-for-gnutls-hash-mac.patch --]
[-- Type: text/x-patch, Size: 2079 bytes --]

diff --git a/src/gnutls.c b/src/gnutls.c
index e3f1093d977..26dd17e673c 100644
--- a/src/gnutls.c
+++ b/src/gnutls.c
@@ -2740,7 +2740,7 @@ DEFUN ("gnutls-digests", Fgnutls_digests, Sgnutls_digests, 0, 0, 0,
   return digest_algorithms;
 }
 
-DEFUN ("gnutls-hash-mac", Fgnutls_hash_mac, Sgnutls_hash_mac, 3, 3, 0,
+DEFUN ("gnutls-hash-mac", Fgnutls_hash_mac, Sgnutls_hash_mac, 3, 4, 0,
        doc: /* Hash INPUT with HASH-METHOD and KEY into a unibyte string.
 
 Return nil on error.
@@ -2752,11 +2752,16 @@ DEFUN ("gnutls-hash-mac", Fgnutls_hash_mac, Sgnutls_hash_mac, 3, 3, 0,
 The INPUT can also be specified as a buffer or string or in other
 ways.
 
+The NONCE can also be specified as a buffer or string or in other
+ways. If MAC algorithm does not require nonce, the optional argument
+NONCE is ignored even if presented.
+
+
 The alist of MAC algorithms can be obtained with `gnutls-macs'.  The
 HASH-METHOD may be a string or symbol matching a key in that alist, or
 a plist with the `:mac-algorithm-id' numeric property, or the number
 itself. */)
-  (Lisp_Object hash_method, Lisp_Object key, Lisp_Object input)
+  (Lisp_Object hash_method, Lisp_Object key, Lisp_Object input, Lisp_Object nonce)
 {
   if (BUFFERP (input) || STRINGP (input))
     input = list1 (input);
@@ -2813,6 +2818,23 @@ DEFUN ("gnutls-hash-mac", Fgnutls_hash_mac, Sgnutls_hash_mac, 3, 3, 0,
     error ("GnuTLS MAC %s initialization failed: %s",
 	   gnutls_mac_get_name (gma), emacs_gnutls_strerror (ret));
 
+  if (!NILP (nonce))
+    {
+      if (BUFFERP (nonce) || STRINGP (nonce))
+        nonce = list1 (nonce);
+
+      CHECK_CONS (nonce);
+
+      ptrdiff_t nstart_byte, nend_byte;
+      const char *ndata
+        = extract_data_from_object (nonce, &nstart_byte, &nend_byte);
+      if (ndata == NULL)
+        error ("GnuTLS MAC nonce extraction failed");
+
+      gnutls_hmac_set_nonce (hmac,
+			     ndata + nstart_byte, nend_byte - nstart_byte);
+    }
+
   ptrdiff_t istart_byte, iend_byte;
   const char *idata
     = extract_data_from_object (input, &istart_byte, &iend_byte);

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-09-10 11:06 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-08 15:58 bug#65826: [PATCH] Add nonce support for gnutls-hash-mac SK Kim
2023-09-08 16:18 ` Stefan Kangas
2023-09-08 16:33   ` Seungki Kim
2023-09-08 16:41     ` Stefan Kangas
2023-09-09  5:50       ` Seungki Kim
2023-09-10 11:06         ` Ted Zlatanov

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).