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

* bug#65826: [PATCH] Add nonce support for gnutls-hash-mac
  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
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Kangas @ 2023-09-08 16:18 UTC (permalink / raw)
  To: SK Kim, 65826

SK Kim <tttuuu888@gmail.com> writes:

> 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.

Sounds useful.  Could you add unit tests for this, though?  In
particular, it would be good to test your number 1 and 2 above.





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

* bug#65826: [PATCH] Add nonce support for gnutls-hash-mac
  2023-09-08 16:18 ` Stefan Kangas
@ 2023-09-08 16:33   ` Seungki Kim
  2023-09-08 16:41     ` Stefan Kangas
  0 siblings, 1 reply; 6+ messages in thread
From: Seungki Kim @ 2023-09-08 16:33 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: 65826

[-- Attachment #1: Type: text/plain, Size: 1209 bytes --]

Thank you for your good suggestion.

It seems like test/lisp/net/gnutls-tests.el would be the right place to add
unit tests for my patch.(Please let me know if it is not)
I will update the patch adding unit tests on this thread as soon as it is
ready.

Thanks.

2023년 9월 9일 (토) 오전 1:18, Stefan Kangas <stefankangas@gmail.com>님이 작성:

> SK Kim <tttuuu888@gmail.com> writes:
>
> > 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.
>
> Sounds useful.  Could you add unit tests for this, though?  In
> particular, it would be good to test your number 1 and 2 above.
>

[-- Attachment #2: Type: text/html, Size: 1805 bytes --]

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

* bug#65826: [PATCH] Add nonce support for gnutls-hash-mac
  2023-09-08 16:33   ` Seungki Kim
@ 2023-09-08 16:41     ` Stefan Kangas
  2023-09-09  5:50       ` Seungki Kim
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Kangas @ 2023-09-08 16:41 UTC (permalink / raw)
  To: Seungki Kim; +Cc: Ted Zlatanov, 65826

Seungki Kim <tttuuu888@gmail.com> writes:

> It seems like test/lisp/net/gnutls-tests.el would be the right place to add
> unit tests for my patch.(Please let me know if it is not)

It looks like the right place, yes.

> I will update the patch adding unit tests on this thread as soon as it is
> ready.

Great, thank you.

I'm also copying in Ted Zlatanov in case he has any comments.





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

* bug#65826: [PATCH] Add nonce support for gnutls-hash-mac
  2023-09-08 16:41     ` Stefan Kangas
@ 2023-09-09  5:50       ` Seungki Kim
  2023-09-10 11:06         ` Ted Zlatanov
  0 siblings, 1 reply; 6+ messages in thread
From: Seungki Kim @ 2023-09-09  5:50 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: Ted Zlatanov, 65826


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

I would like to update the patch to add more unit tests covering
SHA256/512, AES-GMAC-128/192/256 with nonce arguments.
GMAC test vectors are from NIST CAVP SP 800-38D.
I tried to keep the existing format as much as possible while only adding
additional unit tests.
Thanks.

2023년 9월 9일 (토) 오전 1:41, Stefan Kangas <stefankangas@gmail.com>님이 작성:

> Seungki Kim <tttuuu888@gmail.com> writes:
>
> > It seems like test/lisp/net/gnutls-tests.el would be the right place to
> add
> > unit tests for my patch.(Please let me know if it is not)
>
> It looks like the right place, yes.
>
> > I will update the patch adding unit tests on this thread as soon as it is
> > ready.
>
> Great, thank you.
>
> I'm also copying in Ted Zlatanov in case he has any comments.
>

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

[-- Attachment #2: 0002-add-unit-tests-for-gnutls-hash-mac.patch --]
[-- Type: text/x-patch, Size: 8228 bytes --]

diff --git a/test/lisp/net/gnutls-tests.el b/test/lisp/net/gnutls-tests.el
index cb911577385..0f2f39aad67 100644
--- a/test/lisp/net/gnutls-tests.el
+++ b/test/lisp/net/gnutls-tests.el
@@ -181,16 +181,46 @@ test-gnutls-003-hashes-hmacs
   (skip-unless (memq 'macs (gnutls-available-p)))
   (setq gnutls-tests-message-prefix "HMAC verification: ")
   (let ((macs (gnutls-macs)))
-    (dolist (test '(("f5c5021e60d9686fef3bb0414275fe4163bece61d9a95fec7a273746a437b986" "hello\n" "test" SHA256)
+    (dolist (test `(("f5c5021e60d9686fef3bb0414275fe4163bece61d9a95fec7a273746a437b986" "hello\n" "test" SHA256)
                     ("46b75292b81002fd873e89c532a1b8545d6efc9822ee938feba6de2723161a67" "more and more data goes into a file to exceed the buffer size" "test" SHA256)
                     ("81568ba71fa2c5f33cc84bf362466988f98eba3735479100b4e8908acad87ac4" "more and more data goes into a file to exceed the buffer size" "very long key goes here to exceed the key size" SHA256)
                     ("4bc830005783a73b8112f4bd5f4aa5f92e05b51e9b55c0cd6f9a7bee48371def" "more and more data goes into a file to exceed the buffer size" "" "SHA256") ; check string ID for HMAC
-                    ("4bc830005783a73b8112f4bd5f4aa5f92e05b51e9b55c0cd6f9a7bee48371def" "more and more data goes into a file to exceed the buffer size" "" SHA256)))
-      (pcase-let ((`(,hash ,input ,key ,mac) test))
+                    ("4bc830005783a73b8112f4bd5f4aa5f92e05b51e9b55c0cd6f9a7bee48371def" "more and more data goes into a file to exceed the buffer size" "" SHA256)
+                    ("f5c5021e60d9686fef3bb0414275fe4163bece61d9a95fec7a273746a437b986" "hello\n" "test" SHA256 "nonce")
+                    ("46b75292b81002fd873e89c532a1b8545d6efc9822ee938feba6de2723161a67" "more and more data goes into a file to exceed the buffer size" "test" SHA256 "nonce should not affect result")
+                    ("81568ba71fa2c5f33cc84bf362466988f98eba3735479100b4e8908acad87ac4" "more and more data goes into a file to exceed the buffer size" "very long key goes here to exceed the key size" SHA256 "nonce should not affect result")
+                    ("4bc830005783a73b8112f4bd5f4aa5f92e05b51e9b55c0cd6f9a7bee48371def" "more and more data goes into a file to exceed the buffer size" "" "SHA256" "nonce should not affect result") ; check string ID for HMAC
+                    ("4bc830005783a73b8112f4bd5f4aa5f92e05b51e9b55c0cd6f9a7bee48371def" "more and more data goes into a file to exceed the buffer size" "" SHA256 "nonce should not affect result")
+                    ("5300130ed12a31956a2da19bb2b292817955382c25dbe7c04d85e95b7c6db642c15fb7df3a4a235f85c8e15c8107ab43c909f1ebf6a0458992943d3f7fc90637" "hello\n" "test" SHA512)
+                    ("5b2b459e89ecd204beb959dc6214a4c4b7da20bfc4c8adacf9615bddd0b2b8d2e950409e4703de11f02e50852edfb3cfbdaf8795e0ebf0ad50cf15e29150d234" "more and more data goes into a file to exceed the buffer size" "test" SHA512)
+                    ("04d903bacae136010b21cefda8eb930b0016b2854994ff891e1f728c4b6f38e433481308c5713ea9137b003ade1f509bb96180ef5b6db5f5c82336245954674c" "more and more data goes into a file to exceed the buffer size" "very long key goes here to exceed the key size" SHA512)
+                    ("5d6e0645c03fa26e5e55f6e7a2c8dc66927f4a5b93b4fe6f17188941364b689cba09fe088d9a74a7dff5a2a1f104aa87e3b8db6b4eb16739c635893ea1aa5752" "more and more data goes into a file to exceed the buffer size" "" "SHA512") ; check string ID for HMAC
+                    ("5d6e0645c03fa26e5e55f6e7a2c8dc66927f4a5b93b4fe6f17188941364b689cba09fe088d9a74a7dff5a2a1f104aa87e3b8db6b4eb16739c635893ea1aa5752" "more and more data goes into a file to exceed the buffer size" "" SHA512)
+                    ("5300130ed12a31956a2da19bb2b292817955382c25dbe7c04d85e95b7c6db642c15fb7df3a4a235f85c8e15c8107ab43c909f1ebf6a0458992943d3f7fc90637" "hello\n" "test" SHA512 "nonce")
+                    ("5b2b459e89ecd204beb959dc6214a4c4b7da20bfc4c8adacf9615bddd0b2b8d2e950409e4703de11f02e50852edfb3cfbdaf8795e0ebf0ad50cf15e29150d234" "more and more data goes into a file to exceed the buffer size" "test" SHA512 "nonce should not affect result")
+                    ("04d903bacae136010b21cefda8eb930b0016b2854994ff891e1f728c4b6f38e433481308c5713ea9137b003ade1f509bb96180ef5b6db5f5c82336245954674c" "more and more data goes into a file to exceed the buffer size" "very long key goes here to exceed the key size" SHA512 "nonce should not affect result")
+                    ("5d6e0645c03fa26e5e55f6e7a2c8dc66927f4a5b93b4fe6f17188941364b689cba09fe088d9a74a7dff5a2a1f104aa87e3b8db6b4eb16739c635893ea1aa5752" "more and more data goes into a file to exceed the buffer size" "" "SHA512" "nonce should not affect result") ; check string ID for HMAC
+                    ("5d6e0645c03fa26e5e55f6e7a2c8dc66927f4a5b93b4fe6f17188941364b689cba09fe088d9a74a7dff5a2a1f104aa87e3b8db6b4eb16739c635893ea1aa5752" "more and more data goes into a file to exceed the buffer size" "" SHA512 "nonce should not affect result")
+
+                    ;; GMAC tests (from NIST)
+                    ("250327c674aaf477aef2675748cf6971" "" ,(decode-hex-string "11754cd72aec309bf52f7687212e8957") AES-GMAC-128 ,(decode-hex-string "3c819d9a9bed087615030b65"))
+                    ("250327c674aaf477aef2675748cf6971" "" ,(decode-hex-string "11754cd72aec309bf52f7687212e8957") "AES-GMAC-128" ,(decode-hex-string "3c819d9a9bed087615030b65"))
+                    ("209fcc8d3675ed938e9c7166709dd946" ,(decode-hex-string "7a43ec1d9c0a5a78a0b16533a6213cab") ,(decode-hex-string "77be63708971c4e240d1cb79e8d77feb") AES-GMAC-128 ,(decode-hex-string "e0e00f19fed7ba0136a797f3"))
+                    ("209fcc8d3675ed938e9c7166709dd946" ,(decode-hex-string "7a43ec1d9c0a5a78a0b16533a6213cab") ,(decode-hex-string "77be63708971c4e240d1cb79e8d77feb") "AES-GMAC-128" ,(decode-hex-string "e0e00f19fed7ba0136a797f3"))
+                    ("f149e2b5f0adaa9842ca5f45b768a8fc" "" ,(decode-hex-string "aa740abfadcda779220d3b406c5d7ec09a77fe9d94104539") AES-GMAC-192 ,(decode-hex-string "ab2265b4c168955561f04315"))
+                    ("f149e2b5f0adaa9842ca5f45b768a8fc" "" ,(decode-hex-string "aa740abfadcda779220d3b406c5d7ec09a77fe9d94104539") "AES-GMAC-192" ,(decode-hex-string "ab2265b4c168955561f04315"))
+                    ("204bdb1bd62154bf08922aaa54eed705" ,(decode-hex-string "8b5c124bef6e2f0fe4d8c95cd5fa4cf1") ,(decode-hex-string "41c5da8667ef725220ffe39ae0ac590ac9fca729ab60ada0") AES-GMAC-192 ,(decode-hex-string "05ad13a5e2c2ab667e1a6fbc"))
+                    ("204bdb1bd62154bf08922aaa54eed705" ,(decode-hex-string "8b5c124bef6e2f0fe4d8c95cd5fa4cf1") ,(decode-hex-string "41c5da8667ef725220ffe39ae0ac590ac9fca729ab60ada0") "AES-GMAC-192" ,(decode-hex-string "05ad13a5e2c2ab667e1a6fbc"))
+                    ("bdc1ac884d332457a1d2664f168c76f0" "" ,(decode-hex-string "b52c505a37d78eda5dd34f20c22540ea1b58963cf8e5bf8ffa85f9f2492505b4") AES-GMAC-256 ,(decode-hex-string "516c33929df5a3284ff463d7"))
+                    ("bdc1ac884d332457a1d2664f168c76f0" "" ,(decode-hex-string "b52c505a37d78eda5dd34f20c22540ea1b58963cf8e5bf8ffa85f9f2492505b4") "AES-GMAC-256" ,(decode-hex-string "516c33929df5a3284ff463d7"))
+                    ("3e5d486aa2e30b22e040b85723a06e76" ,(decode-hex-string "b96baa8c1c75a671bfb2d08d06be5f36") ,(decode-hex-string "78dc4e0aaf52d935c3c01eea57428f00ca1fd475f5da86a49c8dd73d68c8e223") AES-GMAC-256 ,(decode-hex-string "d79cf22d504cc793c3fb6c8a"))
+                    ("3e5d486aa2e30b22e040b85723a06e76" ,(decode-hex-string "b96baa8c1c75a671bfb2d08d06be5f36") ,(decode-hex-string "78dc4e0aaf52d935c3c01eea57428f00ca1fd475f5da86a49c8dd73d68c8e223") "AES-GMAC-256" ,(decode-hex-string "d79cf22d504cc793c3fb6c8a"))))
+
+      (pcase-let ((`(,hash ,input ,key ,mac ,nonce) test))
         (let ((plist (cdr (assq mac macs)))
               result)
           (gnutls-tests-message "%s %S" mac plist)
-          (setq result (encode-hex-string (gnutls-hash-mac mac (copy-sequence key) input)))
+          (setq result (encode-hex-string (gnutls-hash-mac mac (copy-sequence key) input nonce)))
           (gnutls-tests-message "%S => result %S" test result)
           (should (string-equal result hash)))))))
 

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

* bug#65826: [PATCH] Add nonce support for gnutls-hash-mac
  2023-09-09  5:50       ` Seungki Kim
@ 2023-09-10 11:06         ` Ted Zlatanov
  0 siblings, 0 replies; 6+ messages in thread
From: Ted Zlatanov @ 2023-09-10 11:06 UTC (permalink / raw)
  To: Seungki Kim; +Cc: Stefan Kangas, 65826

On Sat, 9 Sep 2023 14:50:02 +0900 Seungki Kim <tttuuu888@gmail.com> wrote: 

SK> I would like to update the patch to add more unit tests covering
SK> SHA256/512, AES-GMAC-128/192/256 with nonce arguments.
SK> GMAC test vectors are from NIST CAVP SP 800-38D.
SK> I tried to keep the existing format as much as possible while only adding
SK> additional unit tests.

Wow, it's really good to add this test coverage.

Thank you for working on it.
Ted






^ permalink raw reply	[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).