aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorTed Zlatanov2017-07-14 11:04:19 -0400
committerTed Zlatanov2017-07-14 11:06:16 -0400
commit583995c62dd424775dda33d5134ce04bee2ae685 (patch)
tree732251c7c468b20a70d20578b778946cf49f77fe /test
parent0f3cc0b8245dfd7a9f6fcc95ec148be03fde8931 (diff)
downloademacs-583995c62dd424775dda33d5134ce04bee2ae685.tar.gz
emacs-583995c62dd424775dda33d5134ce04bee2ae685.zip
GnuTLS HMAC and symmetric cipher support
* etc/NEWS: Add news for new feature. * doc/lispref/text.texi (GnuTLS Cryptography): Add documentation. * configure.ac: Add macros HAVE_GNUTLS3_DIGEST, HAVE_GNUTLS3_CIPHER, HAVE_GNUTLS3_AEAD, HAVE_GNUTLS3_HMAC. * src/fns.c (Fsecure_hash_algorithms): Add function to list supported `secure-hash' algorithms. (extract_data_from_object): Add data extraction function that can operate on buffers and strings. (secure_hash): Use it. (Fsecure_hash): Mention `secure-hash-algorithms'. * src/gnutls.h: Include gnutls/crypto.h. * src/gnutls.c (Fgnutls_ciphers, gnutls_symmetric_aead) (gnutls_symmetric, Fgnutls_symmetric_encrypt, Fgnutls_symmetric_decrypt) (Fgnutls_macs, Fgnutls_digests, Fgnutls_hash_mac, Fgnutls_hash_digest) (Fgnutls_available_p): Implement GnuTLS cryptographic integration. * test/lisp/net/gnutls-tests.el: Add tests.
Diffstat (limited to 'test')
-rw-r--r--test/lisp/net/gnutls-tests.el290
1 files changed, 290 insertions, 0 deletions
diff --git a/test/lisp/net/gnutls-tests.el b/test/lisp/net/gnutls-tests.el
new file mode 100644
index 00000000000..7cef8c1ff10
--- /dev/null
+++ b/test/lisp/net/gnutls-tests.el
@@ -0,0 +1,290 @@
1;;; gnutls-tests.el --- Test suite for gnutls.el
2
3;; Copyright (C) 2017 Free Software Foundation, Inc.
4
5;; Author: Ted Zlatanov <tzz@lifelogs.com>
6
7;; This program is free software; you can redistribute it and/or modify
8;; it under the terms of the GNU General Public License as published by
9;; the Free Software Foundation, either version 3 of the License, or
10;; (at your option) any later version.
11
12;; This program is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;; GNU General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
18;; along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20;;; Commentary:
21
22;; Run this with `GNUTLS_TEST_VERBOSE=1' to get verbose debugging.
23
24;;; Code:
25
26(require 'ert)
27(require 'cl)
28(require 'gnutls)
29(require 'hex-util)
30
31(defvar gnutls-tests-message-prefix "")
32
33(defsubst gnutls-tests-message (format-string &rest args)
34 (when (getenv "GNUTLS_TEST_VERBOSE")
35 (apply #'message (concat "gnutls-tests: " gnutls-tests-message-prefix format-string) args)))
36
37;; Minor convenience to see strings more easily (without binary data).
38(defsubst gnutls-tests-hexstring-equal (a b)
39 (and (stringp a) (stringp b) (string-equal (encode-hex-string a) (encode-hex-string b))))
40
41(defvar gnutls-tests-internal-macs-upcased
42 (mapcar (lambda (sym) (cons sym (intern (upcase (symbol-name sym)))))
43 (secure-hash-algorithms)))
44
45(defvar gnutls-tests-tested-macs
46 (remove-duplicates
47 (append (mapcar 'cdr gnutls-tests-internal-macs-upcased)
48 (mapcar 'car (gnutls-macs)))))
49
50(defvar gnutls-tests-tested-digests
51 (remove-duplicates
52 (append (mapcar 'cdr gnutls-tests-internal-macs-upcased)
53 (mapcar 'car (gnutls-digests)))))
54
55(defvar gnutls-tests-tested-ciphers
56 (remove-duplicates
57 ; these cause FPEs or SEGVs
58 (remove-if (lambda (e) (memq e '(ARCFOUR-128)))
59 (mapcar 'car (gnutls-ciphers)))))
60
61(defvar gnutls-tests-mondo-strings
62 (list
63 ""
64 "some data"
65 "lots and lots of data lots and lots of data lots and lots of data lots and lots of data lots and lots of data lots and lots of data lots and lots of data lots and lots of data lots and lots of data lots and lots of data lots and lots of data lots and lots of data lots and lots of data lots and lots of data lots and lots of data lots and lots of data lots and lots of data lots and lots of data lots and lots of data lots and lots of data lots and lots of data "
66 "data and more data to go over the block limit!"
67 "data and more data to go over the block limit"
68 (format "some random data %d%d" (random) (random))))
69
70(ert-deftest test-gnutls-000-availability ()
71 "Test the GnuTLS hashes and ciphers availability."
72 (skip-unless (memq 'gnutls3 (gnutls-available-p)))
73 (setq gnutls-tests-message-prefix "availability: ")
74 (should (> (length gnutls-tests-internal-macs-upcased) 5))
75 (let ((macs (gnutls-macs))
76 (digests (gnutls-digests))
77 (ciphers (gnutls-ciphers)))
78 (dolist (mac gnutls-tests-tested-macs)
79 (let ((plist (cdr (assq mac macs))))
80 (gnutls-tests-message "MAC %s %S" mac plist)
81 (dolist (prop '(:mac-algorithm-id :mac-algorithm-length :mac-algorithm-keysize :mac-algorithm-noncesize))
82 (should (plist-get plist prop)))
83 (should (eq 'gnutls-mac-algorithm (plist-get plist :type)))))
84 (dolist (digest gnutls-tests-tested-digests)
85 (let ((plist (cdr (assq digest digests))))
86 (gnutls-tests-message "digest %s %S" digest plist)
87 (dolist (prop '(:digest-algorithm-id :digest-algorithm-length))
88 (should (plist-get plist prop)))
89 (should (eq 'gnutls-digest-algorithm (plist-get plist :type)))))
90 (dolist (cipher gnutls-tests-tested-ciphers)
91 (let ((plist (cdr (assq cipher ciphers))))
92 (gnutls-tests-message "cipher %s %S" cipher plist)
93 (dolist (prop '(:cipher-id :cipher-blocksize :cipher-keysize :cipher-ivsize))
94 (should (plist-get plist prop)))
95 (should (eq 'gnutls-symmetric-cipher (plist-get plist :type)))))))
96
97(ert-deftest test-gnutls-000-data-extractions ()
98 "Test the GnuTLS data extractions against the built-in `secure-hash'."
99 (skip-unless (memq 'digests (gnutls-available-p)))
100 (setq gnutls-tests-message-prefix "data extraction: ")
101 (dolist (input gnutls-tests-mondo-strings)
102 ;; Test buffer extraction
103 (with-temp-buffer
104 (insert input)
105 (insert "not ASCII: не e английски")
106 (dolist (step '(0 1 2 3 4 5))
107 (let ((spec (list (current-buffer) ; a buffer spec
108 (point-min)
109 (max (point-min) (- step (point-max)))))
110 (spec2 (list (buffer-string) ; a string spec
111 (point-min)
112 (max (point-min) (- step (point-max))))))
113 (should (gnutls-tests-hexstring-equal
114 (gnutls-hash-digest 'MD5 spec)
115 (apply 'secure-hash 'md5 (append spec '(t)))))
116 (should (gnutls-tests-hexstring-equal
117 (gnutls-hash-digest 'MD5 spec2)
118 (apply 'secure-hash 'md5 (append spec2 '(t))))))))))
119
120(ert-deftest test-gnutls-001-hashes-internal-digests ()
121 "Test the GnuTLS hash digests against the built-in `secure-hash'."
122 (skip-unless (memq 'digests (gnutls-available-p)))
123 (setq gnutls-tests-message-prefix "digest internal verification: ")
124 (let ((macs (gnutls-macs)))
125 (dolist (mcell gnutls-tests-internal-macs-upcased)
126 (let ((plist (cdr (assq (cdr mcell) macs))))
127 (gnutls-tests-message "Checking digest MAC %S %S" mcell plist)
128 (dolist (input gnutls-tests-mondo-strings)
129 ;; Test buffer extraction
130 (with-temp-buffer
131 (insert input)
132 (should (gnutls-tests-hexstring-equal
133 (gnutls-hash-digest (cdr mcell) (current-buffer))
134 (secure-hash (car mcell) (current-buffer) nil nil t))))
135 (should (gnutls-tests-hexstring-equal
136 (gnutls-hash-digest (cdr mcell) input)
137 (secure-hash (car mcell) input nil nil t))))))))
138
139(ert-deftest test-gnutls-002-hashes-digests ()
140 "Test some GnuTLS hash digests against pre-defined outputs."
141 (skip-unless (memq 'digests (gnutls-available-p)))
142 (setq gnutls-tests-message-prefix "digest external verification: ")
143 (let ((macs (gnutls-macs)))
144 (dolist (test '(("57edf4a22be3c955ac49da2e2107b67a" "12345678901234567890123456789012345678901234567890123456789012345678901234567890" MD5)
145 ("d174ab98d277d9f5a5611c2c9f419d9f" "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" MD5)
146 ("c3fcd3d76192e4007dfb496cca67e13b" "abcdefghijklmnopqrstuvwxyz" MD5)
147 ("f96b697d7cb7938d525a2f31aaf161d0" "message digest" MD5)
148 ("900150983cd24fb0d6963f7d28e17f72" "abc" MD5)
149 ("0cc175b9c0f1b6a831c399e269772661" "a" MD5)
150 ("a9993e364706816aba3e25717850c26c9cd0d89d" "abc" SHA1)
151 ("a9993e364706816aba3e25717850c26c9cd0d89d" "abc" "SHA1"))) ; check string ID for digest
152 (destructuring-bind (hash input mac) test
153 (let ((plist (cdr (assq mac macs)))
154 result resultb)
155 (gnutls-tests-message "%s %S" mac plist)
156 (setq result (encode-hex-string (gnutls-hash-digest mac input)))
157 (gnutls-tests-message "%S => result %S" test result)
158 (should (string-equal result hash))
159 ;; Test buffer extraction
160 (with-temp-buffer
161 (insert input)
162 (setq resultb (encode-hex-string (gnutls-hash-digest mac (current-buffer))))
163 (gnutls-tests-message "%S => result from buffer %S" test resultb)
164 (should (string-equal resultb hash))))))))
165
166(ert-deftest test-gnutls-003-hashes-hmacs ()
167 "Test some predefined GnuTLS HMAC outputs for SHA256."
168 (skip-unless (memq 'macs (gnutls-available-p)))
169 (setq gnutls-tests-message-prefix "HMAC verification: ")
170 (let ((macs (gnutls-macs)))
171 (dolist (test '(("f5c5021e60d9686fef3bb0414275fe4163bece61d9a95fec7a273746a437b986" "hello\n" "test" SHA256)
172 ("46b75292b81002fd873e89c532a1b8545d6efc9822ee938feba6de2723161a67" "more and more data goes into a file to exceed the buffer size" "test" SHA256)
173 ("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)
174 ("4bc830005783a73b8112f4bd5f4aa5f92e05b51e9b55c0cd6f9a7bee48371def" "more and more data goes into a file to exceed the buffer size" "" "SHA256") ; check string ID for HMAC
175 ("4bc830005783a73b8112f4bd5f4aa5f92e05b51e9b55c0cd6f9a7bee48371def" "more and more data goes into a file to exceed the buffer size" "" SHA256)))
176 (destructuring-bind (hash input key mac) test
177 (let ((plist (cdr (assq mac macs)))
178 result)
179 (gnutls-tests-message "%s %S" mac plist)
180 (setq result (encode-hex-string (gnutls-hash-mac mac (copy-sequence key) input)))
181 (gnutls-tests-message "%S => result %S" test result)
182 (should (string-equal result hash)))))))
183
184
185(defun gnutls-tests-pad-or-trim (s exact)
186 "Pad or trim string S to EXACT numeric size."
187 (if (and (consp s) (eq 'iv-auto (nth 0 s)))
188 s
189 (let ((e (number-to-string exact)))
190 (format (concat "%" e "." e "s") s))))
191
192(defun gnutls-tests-pad-to-multiple (s blocksize)
193 "Pad string S to BLOCKSIZE numeric size."
194 (let* ((e (if (string= s "")
195 blocksize
196 (* blocksize (ceiling (length s) blocksize))))
197 (out (concat s (make-string (- e (length s)) ? ))))
198 ;; (gnutls-tests-message "padding %S to length %d for blocksize %d: => %S" s e blocksize out)
199 out))
200
201;; ;;; Testing from the command line:
202;; ;;; echo e36a9d13c15a6df23a59a6337d6132b8f7cd5283cb4784b81141b52343a18e5f5e5ee8f5553c23167409dd222478bc30 | perl -lne 'print pack "H*", $_' | openssl enc -aes-128-ctr -d -nosalt -K 6d796b657932 -iv 696e697432 | od -x
203(ert-deftest test-gnutls-004-symmetric-ciphers ()
204 "Test the GnuTLS symmetric ciphers"
205 (skip-unless (memq 'ciphers (gnutls-available-p)))
206 (setq gnutls-tests-message-prefix "symmetric cipher verification: ")
207 ;; we expect at least 10 ciphers
208 (should (> (length (gnutls-ciphers)) 10))
209 (let ((keys '("mykey" "mykey2"))
210 (inputs gnutls-tests-mondo-strings)
211 (ivs '("" "-abc123-" "init" "ini2"))
212 (ciphers (remove-if
213 (lambda (c) (plist-get (cdr (assq c (gnutls-ciphers)))
214 :cipher-aead-capable))
215 gnutls-tests-tested-ciphers)))
216
217 (dolist (cipher ciphers)
218 (dolist (iv ivs)
219 (dolist (input inputs)
220 (dolist (key keys)
221 (gnutls-tests-message "%S, starting key %S IV %S input %S" (assq cipher (gnutls-ciphers)) key iv input)
222 (let* ((cplist (cdr (assq cipher (gnutls-ciphers))))
223 (key (gnutls-tests-pad-or-trim key (plist-get cplist :cipher-keysize)))
224 (input (gnutls-tests-pad-to-multiple input (plist-get cplist :cipher-blocksize)))
225 (iv (gnutls-tests-pad-or-trim iv (plist-get cplist :cipher-ivsize)))
226 (output (gnutls-symmetric-encrypt cplist (copy-sequence key) iv input))
227 (data (nth 0 output))
228 (actual-iv (nth 1 output))
229 (reverse-output (gnutls-symmetric-decrypt cplist (copy-sequence key) actual-iv data))
230 (reverse (nth 0 reverse-output)))
231 (gnutls-tests-message "%s %S" cipher cplist)
232 (gnutls-tests-message "key %S IV %S input %S => hexdata %S and reverse %S" key iv input (encode-hex-string data) reverse)
233 (should-not (gnutls-tests-hexstring-equal input data))
234 (should-not (gnutls-tests-hexstring-equal data reverse))
235 (should (gnutls-tests-hexstring-equal input reverse)))))))))
236
237(ert-deftest test-gnutls-005-aead-ciphers ()
238 "Test the GnuTLS AEAD ciphers"
239 (skip-unless (memq 'AEAD-ciphers (gnutls-available-p)))
240 (setq gnutls-tests-message-prefix "AEAD verification: ")
241 (let ((keys '("mykey" "mykey2"))
242 (inputs gnutls-tests-mondo-strings)
243 (ivs '("" "-abc123-" "init" "ini2"))
244 (auths '(nil
245 ""
246 "auth data"
247 "auth and auth of data auth and auth of data auth and auth of data auth and auth of data auth and auth of data auth and auth of data auth and auth of data auth and auth of data auth and auth of data auth and auth of data auth and auth of data auth and auth of data auth and auth of data auth and auth of data auth and auth of data auth and auth of data auth and auth of data auth and auth of data auth and auth of data auth and auth of data auth and auth of data "
248 "AUTH data and more data to go over the block limit!"
249 "AUTH data and more data to go over the block limit"))
250 (ciphers (remove-if
251 (lambda (c) (or (null (plist-get (cdr (assq c (gnutls-ciphers)))
252 :cipher-aead-capable))))
253 gnutls-tests-tested-ciphers))
254 actual-ivlist)
255
256 (dolist (cipher ciphers)
257 (dolist (input inputs)
258 (dolist (auth auths)
259 (dolist (key keys)
260 (let* ((cplist (cdr (assq cipher (gnutls-ciphers))))
261 (key (gnutls-tests-pad-or-trim key (plist-get cplist :cipher-keysize)))
262 (input (gnutls-tests-pad-to-multiple input (plist-get cplist :cipher-blocksize)))
263 (ivsize (plist-get cplist :cipher-ivsize)))
264 (should (>= ivsize 12)) ; as per the RFC
265 (dolist (iv (append ivs (list (list 'iv-auto ivsize))))
266
267 (gnutls-tests-message "%S, starting key %S IV %S input %S auth %S" (assq cipher (gnutls-ciphers)) key iv input auth)
268 (let* ((iv (gnutls-tests-pad-or-trim iv (plist-get cplist :cipher-ivsize)))
269 (output (gnutls-symmetric-encrypt cplist (copy-sequence key) iv input (copy-sequence auth)))
270 (data (nth 0 output))
271 (actual-iv (nth 1 output))
272 (reverse-output (gnutls-symmetric-decrypt cplist (copy-sequence key) actual-iv data auth))
273 (reverse (nth 0 reverse-output)))
274 ;; GNUTLS_RND_NONCE should be good enough to ensure this.
275 (should-not (member (secure-hash 'sha384 actual-iv 0 ivsize) actual-ivlist))
276 (cond
277 ((stringp iv)
278 (should (equal iv actual-iv)))
279 ((consp iv)
280 (push (secure-hash 'sha384 actual-iv 0 ivsize) actual-ivlist)
281 (gnutls-tests-message "IV list length: %d" (length actual-ivlist))))
282
283 (gnutls-tests-message "%s %S" cipher cplist)
284 (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)
285 (should-not (gnutls-tests-hexstring-equal input data))
286 (should-not (gnutls-tests-hexstring-equal data reverse))
287 (should (gnutls-tests-hexstring-equal input reverse)))))))))))
288
289(provide 'gnutls-tests)
290;;; gnutls-tests.el ends here