[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emacs-diffs] scratch/tzz/nettle 4f9b6dc: WIP: GnuTLS tests: use symbols
From: |
Teodor Zlatanov |
Subject: |
[Emacs-diffs] scratch/tzz/nettle 4f9b6dc: WIP: GnuTLS tests: use symbols; some small code review fixes from Eli |
Date: |
Sat, 15 Apr 2017 10:26:35 -0400 (EDT) |
branch: scratch/tzz/nettle
commit 4f9b6dc4812cf2a73ec0f8681a7c251326e4c9b2
Author: Ted Zlatanov <address@hidden>
Commit: Ted Zlatanov <address@hidden>
WIP: GnuTLS tests: use symbols; some small code review fixes from Eli
---
src/gnutls.c | 80 +++++++++++++++++++++--------------------
test/lisp/net/gnutls-tests.el | 84 ++++++++++++++++++++++---------------------
2 files changed, 84 insertions(+), 80 deletions(-)
diff --git a/src/gnutls.c b/src/gnutls.c
index 7bb1ec1..d12a8ce 100644
--- a/src/gnutls.c
+++ b/src/gnutls.c
@@ -1698,20 +1698,6 @@ This function may also return `gnutls-e-again', or
#ifdef HAVE_GNUTLS3
-/*
-TODO list:
-doc strings
-additions to the manual and NEWS
-maybe cache gnutls-macs and gnutls-ciphers
-
-EZ> Here and elsewhere, the size of the result is known in advance, so I
-EZ> would avoid allocating a scratch buffer and then copying its data
-EZ> (inside make_unibyte_string) into a newly-allocated string. Instead,
-EZ> use make_uninit_string, and then pass its string data pointer to the
-EZ> algorithm that produces the digest.
-
-*/
-
DEFUN ("gnutls-ciphers", Fgnutls_ciphers, Sgnutls_ciphers, 0, 0, 0,
doc: /* Return alist of GnuTLS symmetric cipher descriptions as plists.
The alist key is the cipher name. */)
@@ -1725,8 +1711,8 @@ The alist key is the cipher name. */)
const gnutls_cipher_algorithm_t gca = gciphers[pos];
Lisp_Object cp = listn (CONSTYPE_HEAP, 15,
- // The string description of the cipher ID
- build_unibyte_string (gnutls_cipher_get_name
(gca)),
+ // A symbol representing the cipher
+ intern (gnutls_cipher_get_name (gca)),
// The internally meaningful cipher ID
QCcipher_id,
make_number (gca),
@@ -1864,7 +1850,12 @@ gnutls_symmetric (bool encrypting, Lisp_Object cipher,
Lisp_Object info = Qnil;
if (STRINGP (cipher))
{
- info = XCDR (Fassoc (cipher, Fgnutls_ciphers ()));
+ cipher = intern (SSDATA (cipher));
+ }
+
+ if (SYMBOLP (cipher))
+ {
+ info = XCDR (Fassq (cipher, Fgnutls_ciphers ()));
}
else if (INTEGERP (cipher))
{
@@ -1982,12 +1973,12 @@ DEFUN ("gnutls-symmetric-encrypt",
Fgnutls_symmetric_encrypt, Sgnutls_symmetric_
Returns nil on error. INPUT, KEY, and IV should be unibyte
strings.
-The alist of symmetric ciphers can be obtained with `gnutls-ciphers`. The
-CIPHER may be a string matching a key in that alist, or a plist
-with the `:cipher-id' numeric property, or the number itself.
+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.
AEAD ciphers: these ciphers will have a `gnutls-ciphers' entry with
-:cipher-aead-capable set to t. AEAD_AUTH can be a unibyte string for
+:cipher-aead-capable set to t. AEAD_AUTH can be a unibyte string for
these AEAD ciphers, and it may be omitted (nil) as well. */)
(Lisp_Object cipher, Lisp_Object key, Lisp_Object iv, Lisp_Object input,
Lisp_Object aead_auth)
{
@@ -2000,12 +1991,12 @@ 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).
-The alist of symmetric ciphers can be obtained with `gnutls-ciphers`. The
-CIPHER may be a string matching a key in that alist, or a plist
-with the `:cipher-id' numeric property, or the number itself.
+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.
AEAD ciphers: these ciphers will have a `gnutls-ciphers' entry with
-:cipher-aead-capable set to t. AEAD_AUTH can be a unibyte string for
+:cipher-aead-capable set to t. AEAD_AUTH can be a unibyte string for
these AEAD ciphers, and it may be omitted (nil) as well. */)
(Lisp_Object cipher, Lisp_Object key, Lisp_Object iv, Lisp_Object input,
Lisp_Object aead_auth)
{
@@ -2028,8 +2019,8 @@ the mac-algorithm method name. */)
const char* name = gnutls_mac_get_name (gma);
Lisp_Object mp = listn (CONSTYPE_HEAP, 11,
- // The string description of the mac-algorithm
ID.
- build_unibyte_string (name),
+ // A symbol representing the mac-algorithm.
+ intern (name),
// The internally meaningful mac-algorithm ID.
QCmac_algorithm_id,
make_number (gma),
@@ -2067,8 +2058,8 @@ the digest-algorithm method name. */)
const char* name = gnutls_digest_get_name (gda);
Lisp_Object mp = listn (CONSTYPE_HEAP, 7,
- // The string description of the
digest-algorithm ID.
- build_unibyte_string (name),
+ // A symbol representing the digest-algorithm.
+ intern (name),
// The internally meaningful digest-algorithm ID.
QCdigest_algorithm_id,
make_number (gda),
@@ -2087,11 +2078,12 @@ the digest-algorithm method name. */)
DEFUN ("gnutls-hash-mac", Fgnutls_hash_mac, Sgnutls_hash_mac, 3, 3, 0,
doc: /* Hash INPUT string with HASH-METHOD and KEY string into a
unibyte string.
-Returns nil on error. INPUT and KEY should be unibyte strings.
+Returns nil on error. INPUT and KEY should be unibyte strings.
-The alist of MAC algorithms can be obtained with `gnutls-macs`. The
-HASH-METHOD may be a string matching a key in that alist, or a plist
-with the `:mac-algorithm-id' numeric property, or the number itself. */)
+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)
{
CHECK_STRING (input);
@@ -2104,7 +2096,12 @@ with the `:mac-algorithm-id' numeric property, or the
number itself. */)
Lisp_Object info = Qnil;
if (STRINGP (hash_method))
{
- info = XCDR (Fassoc (hash_method, Fgnutls_macs ()));
+ hash_method = intern (SSDATA (hash_method));
+ }
+
+ if (SYMBOLP (hash_method))
+ {
+ info = XCDR (Fassq (hash_method, Fgnutls_macs ()));
}
else if (INTEGERP (hash_method))
{
@@ -2169,12 +2166,12 @@ with the `:mac-algorithm-id' numeric property, or the
number itself. */)
DEFUN ("gnutls-hash-digest", Fgnutls_hash_digest, Sgnutls_hash_digest, 2, 2, 0,
doc: /* Digest INPUT string with DIGEST-METHOD into a unibyte string.
-Returns nil on error. INPUT should be a unibyte string.
+Returns nil on error. INPUT should be a unibyte string.
The alist of digest algorithms can be obtained with `gnutls-digests`.
-The DIGEST-METHOD may be a string matching a key in that alist, or
-a plist with the `:digest-algorithm-id' numeric property, or the number
-itself. */)
+The DIGEST-METHOD may be a string or symbol matching a key in that
+alist, or a plist with the `:digest-algorithm-id' numeric property, or
+the number itself. */)
(Lisp_Object digest_method, Lisp_Object input)
{
CHECK_STRING (input);
@@ -2186,7 +2183,12 @@ itself. */)
Lisp_Object info = Qnil;
if (STRINGP (digest_method))
{
- info = XCDR (Fassoc (digest_method, Fgnutls_digests ()));
+ digest_method = intern (SSDATA (digest_method));
+ }
+
+ if (SYMBOLP (digest_method))
+ {
+ info = XCDR (Fassq (digest_method, Fgnutls_digests ()));
}
else if (INTEGERP (digest_method))
{
diff --git a/test/lisp/net/gnutls-tests.el b/test/lisp/net/gnutls-tests.el
index 6665487..a71263a 100644
--- a/test/lisp/net/gnutls-tests.el
+++ b/test/lisp/net/gnutls-tests.el
@@ -40,18 +40,18 @@
(defvar gnutls-tests-tested-macs
(remove-duplicates
- (append '("MD5" "SHA1" "SHA224" "SHA256" "SHA384" "SHA512")
+ (append '(MD5 SHA1 SHA224 SHA256 SHA384 SHA512)
(mapcar 'car (gnutls-macs)))))
(defvar gnutls-tests-tested-digests
(remove-duplicates
- (append '("MD5" "SHA1" "SHA224" "SHA256" "SHA384" "SHA512")
+ (append '(MD5 SHA1 SHA224 SHA256 SHA384 SHA512)
(mapcar 'car (gnutls-digests)))))
(defvar gnutls-tests-tested-ciphers
(remove-duplicates
; these cause FPEs or SEGVs
- (remove-if (lambda (e) (member e '("ARCFOUR-128")))
+ (remove-if (lambda (e) (memq e '(ARCFOUR-128)))
(mapcar 'car (gnutls-ciphers)))))
(defvar gnutls-tests-mondo-strings
@@ -70,21 +70,21 @@
(let ((macs (gnutls-macs))
(digests (gnutls-digests))
(ciphers (gnutls-ciphers)))
- (dolist (name gnutls-tests-tested-macs)
- (let ((plist (cdr (assoc name macs))))
- (gnutls-tests-message "MAC %s %S" name plist)
+ (dolist (mac gnutls-tests-tested-macs)
+ (let ((plist (cdr (assq mac macs))))
+ (gnutls-tests-message "MAC %s %S" mac plist)
(dolist (prop '(:mac-algorithm-id :mac-algorithm-length
:mac-algorithm-keysize :mac-algorithm-noncesize))
(should (plist-get plist prop)))
(should (eq 'gnutls-mac-algorithm (plist-get plist :type)))))
- (dolist (name gnutls-tests-tested-digests)
- (let ((plist (cdr (assoc name digests))))
- (gnutls-tests-message "digest %s %S" name plist)
+ (dolist (digest gnutls-tests-tested-digests)
+ (let ((plist (cdr (assq digest digests))))
+ (gnutls-tests-message "digest %s %S" digest plist)
(dolist (prop '(:digest-algorithm-id :digest-algorithm-length))
(should (plist-get plist prop)))
(should (eq 'gnutls-digest-algorithm (plist-get plist :type)))))
- (dolist (name gnutls-tests-tested-ciphers)
- (let ((plist (cdr (assoc name ciphers))))
- (gnutls-tests-message "cipher %s %S" name plist)
+ (dolist (cipher gnutls-tests-tested-ciphers)
+ (let ((plist (cdr (assq cipher ciphers))))
+ (gnutls-tests-message "cipher %s %S" cipher plist)
(dolist (prop '(:cipher-id :cipher-blocksize :cipher-keysize
:cipher-ivsize))
(should (plist-get plist prop)))
(should (eq 'gnutls-symmetric-cipher (plist-get plist :type)))))))
@@ -98,12 +98,12 @@
;; `secure-hash'. Unfortunately this list can't be obtained
;; programmatically.
(dolist (sym '(md5 sha1 sha224 sha256 sha384 sha512))
- (let* ((name (upcase (symbol-name sym)))
- (plist (cdr (assoc name macs))))
- (gnutls-tests-message "Checking digest MAC %s %S" name plist)
+ (let* ((mac (intern (upcase (symbol-name sym))))
+ (plist (cdr (assq mac macs))))
+ (gnutls-tests-message "Checking digest MAC %s %S" mac plist)
(dolist (input gnutls-tests-mondo-strings)
(should (gnutls-tests-hexstring-equal
- (gnutls-hash-digest name input)
+ (gnutls-hash-digest mac input)
(secure-hash sym input nil nil t))))))))
(ert-deftest test-gnutls-002-hashes-digests ()
@@ -111,18 +111,19 @@
(skip-unless (gnutls-available-p))
(setq gnutls-tests-message-prefix "digest external verification: ")
(let ((macs (gnutls-macs)))
- (dolist (test '(("57edf4a22be3c955ac49da2e2107b67a"
"12345678901234567890123456789012345678901234567890123456789012345678901234567890"
"MD5")
- ("d174ab98d277d9f5a5611c2c9f419d9f"
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" "MD5")
- ("c3fcd3d76192e4007dfb496cca67e13b"
"abcdefghijklmnopqrstuvwxyz" "MD5")
- ("f96b697d7cb7938d525a2f31aaf161d0" "message digest" "MD5")
- ("900150983cd24fb0d6963f7d28e17f72" "abc" "MD5")
- ("0cc175b9c0f1b6a831c399e269772661" "a" "MD5")
- ("a9993e364706816aba3e25717850c26c9cd0d89d" "abc" "SHA1")))
- (destructuring-bind (hash input name) test
- (let ((plist (cdr (assoc name macs)))
+ (dolist (test '(("57edf4a22be3c955ac49da2e2107b67a"
"12345678901234567890123456789012345678901234567890123456789012345678901234567890"
MD5)
+ ("d174ab98d277d9f5a5611c2c9f419d9f"
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" MD5)
+ ("c3fcd3d76192e4007dfb496cca67e13b"
"abcdefghijklmnopqrstuvwxyz" MD5)
+ ("f96b697d7cb7938d525a2f31aaf161d0" "message digest" MD5)
+ ("900150983cd24fb0d6963f7d28e17f72" "abc" MD5)
+ ("0cc175b9c0f1b6a831c399e269772661" "a" MD5)
+ ("a9993e364706816aba3e25717850c26c9cd0d89d" "abc" SHA1)
+ ("a9993e364706816aba3e25717850c26c9cd0d89d" "abc"
"SHA1"))) ; check string ID for digest
+ (destructuring-bind (hash input mac) test
+ (let ((plist (cdr (assq mac macs)))
result)
- (gnutls-tests-message "%s %S" name plist)
- (setq result (encode-hex-string (gnutls-hash-digest name input)))
+ (gnutls-tests-message "%s %S" mac plist)
+ (setq result (encode-hex-string (gnutls-hash-digest mac input)))
(gnutls-tests-message "%S => result %S" test result)
(should (string-equal result hash)))))))
@@ -131,15 +132,16 @@
(skip-unless (gnutls-available-p))
(setq gnutls-tests-message-prefix "HMAC verification: ")
(let ((macs (gnutls-macs)))
- (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")))
- (destructuring-bind (hash input key name) test
- (let ((plist (cdr (assoc name macs)))
+ (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)))
+ (destructuring-bind (hash input key mac) test
+ (let ((plist (cdr (assq mac macs)))
result)
- (gnutls-tests-message "%s %S" name plist)
- (setq result (encode-hex-string (gnutls-hash-mac name key input)))
+ (gnutls-tests-message "%s %S" mac plist)
+ (setq result (encode-hex-string (gnutls-hash-mac mac key input)))
(gnutls-tests-message "%S => result %S" test result)
(should (string-equal result hash)))))))
@@ -170,7 +172,7 @@
(inputs gnutls-tests-mondo-strings)
(ivs '("" "-abc123-" "init" "ini2"))
(ciphers (remove-if
- (lambda (c) (plist-get (cdr (assoc c (gnutls-ciphers)))
+ (lambda (c) (plist-get (cdr (assq c (gnutls-ciphers)))
:cipher-aead-capable))
gnutls-tests-tested-ciphers)))
@@ -178,8 +180,8 @@
(dolist (iv ivs)
(dolist (input inputs)
(dolist (key keys)
- (gnutls-tests-message "%S, starting key %S IV %S input %S" (assoc
cipher (gnutls-ciphers)) key iv input)
- (let* ((cplist (cdr (assoc cipher (gnutls-ciphers))))
+ (gnutls-tests-message "%S, starting key %S IV %S input %S" (assq
cipher (gnutls-ciphers)) key iv input)
+ (let* ((cplist (cdr (assq cipher (gnutls-ciphers))))
(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)))
@@ -205,7 +207,7 @@
"AUTH data and more data to go over the block limit!"
"AUTH data and more data to go over the block limit"))
(ciphers (remove-if
- (lambda (c) (or (null (plist-get (cdr (assoc c
(gnutls-ciphers)))
+ (lambda (c) (or (null (plist-get (cdr (assq c
(gnutls-ciphers)))
:cipher-aead-capable))))
gnutls-tests-tested-ciphers)))
@@ -214,8 +216,8 @@
(dolist (input inputs)
(dolist (auth auths)
(dolist (key keys)
- (gnutls-tests-message "%S, starting key %S IV %S input %S auth
%S" (assoc cipher (gnutls-ciphers)) key iv input auth)
- (let* ((cplist (cdr (assoc cipher (gnutls-ciphers))))
+ (gnutls-tests-message "%S, starting key %S IV %S input %S auth
%S" (assq cipher (gnutls-ciphers)) key iv input auth)
+ (let* ((cplist (cdr (assq cipher (gnutls-ciphers))))
(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)))
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Emacs-diffs] scratch/tzz/nettle 4f9b6dc: WIP: GnuTLS tests: use symbols; some small code review fixes from Eli,
Teodor Zlatanov <=