unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: SK Kim <tttuuu888@gmail.com>
To: 65826@debbugs.gnu.org
Subject: bug#65826: [PATCH] Add nonce support for gnutls-hash-mac
Date: Sat, 9 Sep 2023 00:58:52 +0900	[thread overview]
Message-ID: <CAKYNWwQN08JFCpedr+tAT3-0VkfYhQuys25Ku3enh8A0jVkPeA@mail.gmail.com> (raw)


[-- 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);

             reply	other threads:[~2023-09-08 15:58 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-08 15:58 SK Kim [this message]
2023-09-08 16:18 ` bug#65826: [PATCH] Add nonce support for gnutls-hash-mac 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAKYNWwQN08JFCpedr+tAT3-0VkfYhQuys25Ku3enh8A0jVkPeA@mail.gmail.com \
    --to=tttuuu888@gmail.com \
    --cc=65826@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).