changeset 86795:6cd3c68dd6ab

Move here from ../gnus.
author Glenn Morris <rgm@gnu.org>
date Wed, 28 Nov 2007 08:06:36 +0000
parents c142065479b2
children b2b7cce3571a
files lisp/net/hmac-def.el lisp/net/hmac-md5.el
diffstat 2 files changed, 171 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lisp/net/hmac-def.el	Wed Nov 28 08:06:36 2007 +0000
@@ -0,0 +1,86 @@
+;;; hmac-def.el --- A macro for defining HMAC functions.
+
+;; Copyright (C) 1999, 2001, 2007  Free Software Foundation, Inc.
+
+;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
+;; Keywords: HMAC, RFC 2104
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 3, or (at your option)
+;; any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
+
+;;; Commentary:
+
+;; This program is implemented from RFC 2104,
+;; "HMAC: Keyed-Hashing for Message Authentication".
+
+;;; Code:
+
+(defmacro define-hmac-function (name H B L &optional bit)
+  "Define a function NAME(TEXT KEY) which computes HMAC with function H.
+
+HMAC function is H(KEY XOR opad, H(KEY XOR ipad, TEXT)):
+
+H is a cryptographic hash function, such as SHA1 and MD5, which takes
+a string and return a digest of it (in binary form).
+B is a byte-length of a block size of H. (B=64 for both SHA1 and MD5.)
+L is a byte-length of hash outputs. (L=16 for MD5, L=20 for SHA1.)
+If BIT is non-nil, truncate output to specified bits."
+  `(defun ,name (text key)
+     ,(concat "Compute "
+	      (upcase (symbol-name name))
+	      " over TEXT with KEY.")
+     (let ((key-xor-ipad (make-string ,B ?\x36))
+	   (key-xor-opad (make-string ,B ?\x5C))
+	   (len (length key))
+	   (pos 0))
+       (unwind-protect
+	   (progn
+	     ;; if `key' is longer than the block size, apply hash function
+	     ;; to `key' and use the result as a real `key'.
+	     (if (> len ,B)
+		 (setq key (,H key)
+		       len ,L))
+	     (while (< pos len)
+	       (aset key-xor-ipad pos (logxor (aref key pos) ?\x36))
+	       (aset key-xor-opad pos (logxor (aref key pos) ?\x5C))
+	       (setq pos (1+ pos)))
+	     (setq key-xor-ipad (unwind-protect
+				    (concat key-xor-ipad text)
+				  (fillarray key-xor-ipad 0))
+		   key-xor-ipad (unwind-protect
+				    (,H key-xor-ipad)
+				  (fillarray key-xor-ipad 0))
+		   key-xor-opad (unwind-protect
+				    (concat key-xor-opad key-xor-ipad)
+				  (fillarray key-xor-opad 0))
+		   key-xor-opad (unwind-protect
+				    (,H key-xor-opad)
+				  (fillarray key-xor-opad 0)))
+	     ;; now `key-xor-opad' contains
+	     ;; H(KEY XOR opad, H(KEY XOR ipad, TEXT)).
+	     ,(if (and bit (< (/ bit 8) L))
+		  `(substring key-xor-opad 0 ,(/ bit 8))
+		;; return a copy of `key-xor-opad'.
+		`(concat key-xor-opad)))
+	 ;; cleanup.
+	 (fillarray key-xor-ipad 0)
+	 (fillarray key-xor-opad 0)))))
+
+(provide 'hmac-def)
+
+;;; arch-tag: 645adcef-b835-4900-a10a-11f636c982b9
+;;; hmac-def.el ends here
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lisp/net/hmac-md5.el	Wed Nov 28 08:06:36 2007 +0000
@@ -0,0 +1,85 @@
+;;; hmac-md5.el --- Compute HMAC-MD5.
+
+;; Copyright (C) 1999, 2001, 2007  Free Software Foundation, Inc.
+
+;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
+;; Keywords: HMAC, RFC 2104, HMAC-MD5, MD5, KEYED-MD5, CRAM-MD5
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 3, or (at your option)
+;; any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
+
+;;; Commentary:
+
+;; Test cases from RFC 2202, "Test Cases for HMAC-MD5 and HMAC-SHA-1".
+;;
+;; (encode-hex-string (hmac-md5 "Hi There" (make-string 16 ?\x0b)))
+;;  => "9294727a3638bb1c13f48ef8158bfc9d"
+;;
+;; (encode-hex-string (hmac-md5 "what do ya want for nothing?" "Jefe"))
+;;  => "750c783e6ab0b503eaa86e310a5db738"
+;;
+;; (encode-hex-string (hmac-md5 (make-string 50 ?\xdd) (make-string 16 ?\xaa)))
+;;  => "56be34521d144c88dbb8c733f0e8b3f6"
+;;
+;; (encode-hex-string
+;;  (hmac-md5
+;;   (make-string 50 ?\xcd)
+;;   (decode-hex-string "0102030405060708090a0b0c0d0e0f10111213141516171819")))
+;;  => "697eaf0aca3a3aea3a75164746ffaa79"
+;;
+;; (encode-hex-string
+;;  (hmac-md5 "Test With Truncation" (make-string 16 ?\x0c)))
+;;  => "56461ef2342edc00f9bab995690efd4c"
+;;
+;; (encode-hex-string
+;;  (hmac-md5-96 "Test With Truncation" (make-string 16 ?\x0c)))
+;;  => "56461ef2342edc00f9bab995"
+;;
+;; (encode-hex-string
+;;  (hmac-md5
+;;   "Test Using Larger Than Block-Size Key - Hash Key First"
+;;   (make-string 80 ?\xaa)))
+;;  => "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd"
+;;
+;; (encode-hex-string
+;;  (hmac-md5
+;;   "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
+;;   (make-string 80 ?\xaa)))
+;;  => "6f630fad67cda0ee1fb1f562db3aa53e"
+
+;;; Code:
+
+(eval-when-compile (require 'hmac-def))
+(require 'hex-util)			; (decode-hex-string STRING)
+(require 'md5)				; expects (md5 STRING)
+
+(defun md5-binary (string)
+  "Return the MD5 of STRING in binary form."
+  (if (condition-case nil
+	  ;; `md5' of v21 takes 4th arg CODING (and 5th arg NOERROR).
+	  (md5 "" nil nil 'binary)	; => "d41d8cd98f00b204e9800998ecf8427e"
+	(wrong-number-of-arguments nil))
+      (decode-hex-string (md5 string nil nil 'binary))
+    (decode-hex-string (md5 string))))
+
+(define-hmac-function hmac-md5 md5-binary 64 16) ; => (hmac-md5 TEXT KEY)
+(define-hmac-function hmac-md5-96 md5-binary 64 16 96)
+
+(provide 'hmac-md5)
+
+;;; arch-tag: 0ab3f4f6-3d4b-4167-a9fa-635b7fed7f27
+;;; hmac-md5.el ends here