aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGlenn Morris2007-11-28 08:06:36 +0000
committerGlenn Morris2007-11-28 08:06:36 +0000
commit0cd1fac26800b1588e901b360e314ec938db8981 (patch)
tree66a144427234a2dbfe288a3cc5bd7b336d01a84f
parent4e856ec712c1f5a0111ab7480a091b629cad382a (diff)
downloademacs-0cd1fac26800b1588e901b360e314ec938db8981.tar.gz
emacs-0cd1fac26800b1588e901b360e314ec938db8981.zip
Move here from ../gnus.
-rw-r--r--lisp/net/hmac-def.el86
-rw-r--r--lisp/net/hmac-md5.el85
2 files changed, 171 insertions, 0 deletions
diff --git a/lisp/net/hmac-def.el b/lisp/net/hmac-def.el
new file mode 100644
index 00000000000..bfff7282adf
--- /dev/null
+++ b/lisp/net/hmac-def.el
@@ -0,0 +1,86 @@
1;;; hmac-def.el --- A macro for defining HMAC functions.
2
3;; Copyright (C) 1999, 2001, 2007 Free Software Foundation, Inc.
4
5;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
6;; Keywords: HMAC, RFC 2104
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 3, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301, USA.
24
25;;; Commentary:
26
27;; This program is implemented from RFC 2104,
28;; "HMAC: Keyed-Hashing for Message Authentication".
29
30;;; Code:
31
32(defmacro define-hmac-function (name H B L &optional bit)
33 "Define a function NAME(TEXT KEY) which computes HMAC with function H.
34
35HMAC function is H(KEY XOR opad, H(KEY XOR ipad, TEXT)):
36
37H is a cryptographic hash function, such as SHA1 and MD5, which takes
38a string and return a digest of it (in binary form).
39B is a byte-length of a block size of H. (B=64 for both SHA1 and MD5.)
40L is a byte-length of hash outputs. (L=16 for MD5, L=20 for SHA1.)
41If BIT is non-nil, truncate output to specified bits."
42 `(defun ,name (text key)
43 ,(concat "Compute "
44 (upcase (symbol-name name))
45 " over TEXT with KEY.")
46 (let ((key-xor-ipad (make-string ,B ?\x36))
47 (key-xor-opad (make-string ,B ?\x5C))
48 (len (length key))
49 (pos 0))
50 (unwind-protect
51 (progn
52 ;; if `key' is longer than the block size, apply hash function
53 ;; to `key' and use the result as a real `key'.
54 (if (> len ,B)
55 (setq key (,H key)
56 len ,L))
57 (while (< pos len)
58 (aset key-xor-ipad pos (logxor (aref key pos) ?\x36))
59 (aset key-xor-opad pos (logxor (aref key pos) ?\x5C))
60 (setq pos (1+ pos)))
61 (setq key-xor-ipad (unwind-protect
62 (concat key-xor-ipad text)
63 (fillarray key-xor-ipad 0))
64 key-xor-ipad (unwind-protect
65 (,H key-xor-ipad)
66 (fillarray key-xor-ipad 0))
67 key-xor-opad (unwind-protect
68 (concat key-xor-opad key-xor-ipad)
69 (fillarray key-xor-opad 0))
70 key-xor-opad (unwind-protect
71 (,H key-xor-opad)
72 (fillarray key-xor-opad 0)))
73 ;; now `key-xor-opad' contains
74 ;; H(KEY XOR opad, H(KEY XOR ipad, TEXT)).
75 ,(if (and bit (< (/ bit 8) L))
76 `(substring key-xor-opad 0 ,(/ bit 8))
77 ;; return a copy of `key-xor-opad'.
78 `(concat key-xor-opad)))
79 ;; cleanup.
80 (fillarray key-xor-ipad 0)
81 (fillarray key-xor-opad 0)))))
82
83(provide 'hmac-def)
84
85;;; arch-tag: 645adcef-b835-4900-a10a-11f636c982b9
86;;; hmac-def.el ends here
diff --git a/lisp/net/hmac-md5.el b/lisp/net/hmac-md5.el
new file mode 100644
index 00000000000..186708446f0
--- /dev/null
+++ b/lisp/net/hmac-md5.el
@@ -0,0 +1,85 @@
1;;; hmac-md5.el --- Compute HMAC-MD5.
2
3;; Copyright (C) 1999, 2001, 2007 Free Software Foundation, Inc.
4
5;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
6;; Keywords: HMAC, RFC 2104, HMAC-MD5, MD5, KEYED-MD5, CRAM-MD5
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 3, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301, USA.
24
25;;; Commentary:
26
27;; Test cases from RFC 2202, "Test Cases for HMAC-MD5 and HMAC-SHA-1".
28;;
29;; (encode-hex-string (hmac-md5 "Hi There" (make-string 16 ?\x0b)))
30;; => "9294727a3638bb1c13f48ef8158bfc9d"
31;;
32;; (encode-hex-string (hmac-md5 "what do ya want for nothing?" "Jefe"))
33;; => "750c783e6ab0b503eaa86e310a5db738"
34;;
35;; (encode-hex-string (hmac-md5 (make-string 50 ?\xdd) (make-string 16 ?\xaa)))
36;; => "56be34521d144c88dbb8c733f0e8b3f6"
37;;
38;; (encode-hex-string
39;; (hmac-md5
40;; (make-string 50 ?\xcd)
41;; (decode-hex-string "0102030405060708090a0b0c0d0e0f10111213141516171819")))
42;; => "697eaf0aca3a3aea3a75164746ffaa79"
43;;
44;; (encode-hex-string
45;; (hmac-md5 "Test With Truncation" (make-string 16 ?\x0c)))
46;; => "56461ef2342edc00f9bab995690efd4c"
47;;
48;; (encode-hex-string
49;; (hmac-md5-96 "Test With Truncation" (make-string 16 ?\x0c)))
50;; => "56461ef2342edc00f9bab995"
51;;
52;; (encode-hex-string
53;; (hmac-md5
54;; "Test Using Larger Than Block-Size Key - Hash Key First"
55;; (make-string 80 ?\xaa)))
56;; => "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd"
57;;
58;; (encode-hex-string
59;; (hmac-md5
60;; "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
61;; (make-string 80 ?\xaa)))
62;; => "6f630fad67cda0ee1fb1f562db3aa53e"
63
64;;; Code:
65
66(eval-when-compile (require 'hmac-def))
67(require 'hex-util) ; (decode-hex-string STRING)
68(require 'md5) ; expects (md5 STRING)
69
70(defun md5-binary (string)
71 "Return the MD5 of STRING in binary form."
72 (if (condition-case nil
73 ;; `md5' of v21 takes 4th arg CODING (and 5th arg NOERROR).
74 (md5 "" nil nil 'binary) ; => "d41d8cd98f00b204e9800998ecf8427e"
75 (wrong-number-of-arguments nil))
76 (decode-hex-string (md5 string nil nil 'binary))
77 (decode-hex-string (md5 string))))
78
79(define-hmac-function hmac-md5 md5-binary 64 16) ; => (hmac-md5 TEXT KEY)
80(define-hmac-function hmac-md5-96 md5-binary 64 16 96)
81
82(provide 'hmac-md5)
83
84;;; arch-tag: 0ab3f4f6-3d4b-4167-a9fa-635b7fed7f27
85;;; hmac-md5.el ends here