emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] scratch/tzz/nettle dee3b49: WIP: GnuTLS: use Fclear_string


From: Teodor Zlatanov
Subject: [Emacs-diffs] scratch/tzz/nettle dee3b49: WIP: GnuTLS: use Fclear_string on IV, KEY, AUTH and adjust tests
Date: Mon, 17 Apr 2017 13:10:27 -0400 (EDT)

branch: scratch/tzz/nettle
commit dee3b49467c6b60cf145839c190a23e0e10521ce
Author: Ted Zlatanov <address@hidden>
Commit: Ted Zlatanov <address@hidden>

    WIP: GnuTLS: use Fclear_string on IV, KEY, AUTH and adjust tests
---
 src/gnutls.c                  | 24 ++++++++++++++++++++++--
 test/lisp/net/gnutls-tests.el | 10 +++++-----
 2 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/src/gnutls.c b/src/gnutls.c
index d12a8ce..96ca9b1 100644
--- a/src/gnutls.c
+++ b/src/gnutls.c
@@ -1811,9 +1811,16 @@ gnutls_symmetric_aead (bool encrypting, 
gnutls_cipher_algorithm_t gca,
                                         SSDATA (storage), &storage_length);
     }
 
+  Fclear_string (key);
+  Fclear_string (iv);
+  if (STRINGP (aead_auth))
+    {
+      Fclear_string (aead_auth);
+    }
+
   if (ret < GNUTLS_E_SUCCESS)
     {
-      memset(SSDATA (storage), 0, storage_length);
+      Fclear_string (storage);
       gnutls_aead_cipher_deinit (acipher);
       const char* str = gnutls_strerror (ret);
       if (!str)
@@ -1825,6 +1832,8 @@ gnutls_symmetric_aead (bool encrypting, 
gnutls_cipher_algorithm_t gca,
 
   gnutls_aead_cipher_deinit (acipher);
 
+  // TODO: switch this to use a resize_string_data() function when
+  // that's provided in the C core, to avoid the extra copy.
   return make_unibyte_string (SSDATA (storage), storage_length);
 #else
   error ("GnuTLS AEAD cipher %ld was invalid or not found", (long) gca);
@@ -1950,9 +1959,12 @@ gnutls_symmetric (bool encrypting, Lisp_Object cipher,
                                     SSDATA (storage), storage_length);
     }
 
+  Fclear_string (key);
+  Fclear_string (iv);
+
   if (ret < GNUTLS_E_SUCCESS)
     {
-      memset(SSDATA (storage), 0, storage_length);
+      Fclear_string (storage);
       gnutls_cipher_deinit (hcipher);
       const char* str = gnutls_strerror (ret);
       if (!str)
@@ -1973,6 +1985,8 @@ DEFUN ("gnutls-symmetric-encrypt", 
Fgnutls_symmetric_encrypt, Sgnutls_symmetric_
 Returns nil on error. INPUT, KEY, and IV should be unibyte
 strings.
 
+IV, KEY, and AEAD_AUTH will be wiped by the function.
+
 The alist of symmetric ciphers can be obtained with `gnutls-ciphers`.
 The CIPHER may be a string or symbol matching a key in that alist, or
 a plist with the `:cipher-id' numeric property, or the number itself.
@@ -1991,6 +2005,8 @@ DEFUN ("gnutls-symmetric-decrypt", 
Fgnutls_symmetric_decrypt, Sgnutls_symmetric_
 Returns nil on error. INPUT, KEY, and IV should be unibyte
 strings. AEAD_AUTH may be a unibyte string or omitted (nil).
 
+IV, KEY, and AEAD_AUTH will be wiped by the function.
+
 The alist of symmetric ciphers can be obtained with `gnutls-ciphers`.
 The CIPHER may be a string or symbol matching a key in that alist, or
 a plist with the `:cipher-id' numeric property, or the number itself.
@@ -2080,6 +2096,8 @@ DEFUN ("gnutls-hash-mac", Fgnutls_hash_mac, 
Sgnutls_hash_mac, 3, 3, 0,
 
 Returns nil on error.  INPUT and KEY should be unibyte strings.
 
+KEY will be wiped by the function.
+
 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
@@ -2145,6 +2163,8 @@ itself. */)
 
   ret = gnutls_hmac (hmac, SSDATA (input), SCHARS (input));
 
+  Fclear_string (key);
+
   if (ret < GNUTLS_E_SUCCESS)
     {
       gnutls_hmac_deinit (hmac, NULL);
diff --git a/test/lisp/net/gnutls-tests.el b/test/lisp/net/gnutls-tests.el
index a71263a..87a149d 100644
--- a/test/lisp/net/gnutls-tests.el
+++ b/test/lisp/net/gnutls-tests.el
@@ -141,7 +141,7 @@
         (let ((plist (cdr (assq mac macs)))
               result)
           (gnutls-tests-message "%s %S" mac plist)
-          (setq result (encode-hex-string (gnutls-hash-mac mac key input)))
+          (setq result (encode-hex-string (gnutls-hash-mac mac (copy-sequence 
key) input)))
           (gnutls-tests-message "%S => result %S" test result)
           (should (string-equal result hash)))))))
 
@@ -185,8 +185,8 @@
                    (key (gnutls-tests-pad-or-trim key (plist-get cplist 
:cipher-keysize)))
                    (input (gnutls-tests-pad-to-multiple input (plist-get 
cplist :cipher-blocksize)))
                    (iv (gnutls-tests-pad-or-trim iv (plist-get cplist 
:cipher-ivsize)))
-                   (data (gnutls-symmetric-encrypt cplist key iv input))
-                   (reverse (gnutls-symmetric-decrypt cplist key iv data)))
+                   (data (gnutls-symmetric-encrypt cplist (copy-sequence key) 
(copy-sequence iv) input))
+                   (reverse (gnutls-symmetric-decrypt cplist (copy-sequence 
key) (copy-sequence iv) data)))
               (gnutls-tests-message "%s %S" cipher cplist)
               (gnutls-tests-message "key %S IV %S input %S => hexdata %S and 
reverse %S" key iv input (encode-hex-string data) reverse)
               (should-not (gnutls-tests-hexstring-equal input data))
@@ -221,8 +221,8 @@
                      (key (gnutls-tests-pad-or-trim key (plist-get cplist 
:cipher-keysize)))
                      (input (gnutls-tests-pad-to-multiple input (plist-get 
cplist :cipher-blocksize)))
                      (iv (gnutls-tests-pad-or-trim iv (plist-get cplist 
:cipher-ivsize)))
-                     (data (gnutls-symmetric-encrypt cplist key iv input auth))
-                     (reverse (gnutls-symmetric-decrypt cplist key iv data 
auth)))
+                     (data (gnutls-symmetric-encrypt cplist (copy-sequence 
key) (copy-sequence iv) input (copy-sequence auth)))
+                     (reverse (gnutls-symmetric-decrypt cplist (copy-sequence 
key) (copy-sequence iv) data (copy-sequence auth))))
                 (gnutls-tests-message "%s %S" cipher cplist)
                 (gnutls-tests-message "key %S IV %S input %S auth %S => 
hexdata %S and reverse %S" key iv input auth (encode-hex-string data) reverse)
                 (should-not (gnutls-tests-hexstring-equal input data))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]