annotate lisp/net/hmac-def.el @ 93881:c0fe31fb3833

($(lisp)/mh-e/mh-loaddefs.el): Make it depend on mh-e/*.el rather than subdirs.el. It introduces an ugly circular dependency, tho.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Tue, 08 Apr 2008 22:12:12 +0000
parents b9e8ab94c460
children 1e3a407766b9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
86795
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
1 ;;; hmac-def.el --- A macro for defining HMAC functions.
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
2
87665
b9e8ab94c460 Add 2008 to copyright years.
Glenn Morris <rgm@gnu.org>
parents: 86795
diff changeset
3 ;; Copyright (C) 1999, 2001, 2007, 2008 Free Software Foundation, Inc.
86795
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
4
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
5 ;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
6 ;; Keywords: HMAC, RFC 2104
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
7
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
8 ;; This file is part of GNU Emacs.
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
9
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
11 ;; it under the terms of the GNU General Public License as published by
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
12 ;; the Free Software Foundation; either version 3, or (at your option)
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
13 ;; any later version.
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
14
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
15 ;; GNU Emacs is distributed in the hope that it will be useful,
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
18 ;; GNU General Public License for more details.
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
19
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
20 ;; You should have received a copy of the GNU General Public License
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
23 ;; Boston, MA 02110-1301, USA.
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
24
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
25 ;;; Commentary:
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
26
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
27 ;; This program is implemented from RFC 2104,
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
28 ;; "HMAC: Keyed-Hashing for Message Authentication".
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
29
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
30 ;;; Code:
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
31
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
32 (defmacro define-hmac-function (name H B L &optional bit)
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
33 "Define a function NAME(TEXT KEY) which computes HMAC with function H.
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
34
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
35 HMAC function is H(KEY XOR opad, H(KEY XOR ipad, TEXT)):
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
36
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
37 H is a cryptographic hash function, such as SHA1 and MD5, which takes
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
38 a string and return a digest of it (in binary form).
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
39 B is a byte-length of a block size of H. (B=64 for both SHA1 and MD5.)
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
40 L is a byte-length of hash outputs. (L=16 for MD5, L=20 for SHA1.)
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
41 If BIT is non-nil, truncate output to specified bits."
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
42 `(defun ,name (text key)
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
43 ,(concat "Compute "
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
44 (upcase (symbol-name name))
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
45 " over TEXT with KEY.")
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
46 (let ((key-xor-ipad (make-string ,B ?\x36))
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
47 (key-xor-opad (make-string ,B ?\x5C))
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
48 (len (length key))
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
49 (pos 0))
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
50 (unwind-protect
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
51 (progn
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
52 ;; if `key' is longer than the block size, apply hash function
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
53 ;; to `key' and use the result as a real `key'.
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
54 (if (> len ,B)
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
55 (setq key (,H key)
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
56 len ,L))
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
57 (while (< pos len)
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
58 (aset key-xor-ipad pos (logxor (aref key pos) ?\x36))
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
59 (aset key-xor-opad pos (logxor (aref key pos) ?\x5C))
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
60 (setq pos (1+ pos)))
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
61 (setq key-xor-ipad (unwind-protect
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
62 (concat key-xor-ipad text)
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
63 (fillarray key-xor-ipad 0))
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
64 key-xor-ipad (unwind-protect
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
65 (,H key-xor-ipad)
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
66 (fillarray key-xor-ipad 0))
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
67 key-xor-opad (unwind-protect
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
68 (concat key-xor-opad key-xor-ipad)
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
69 (fillarray key-xor-opad 0))
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
70 key-xor-opad (unwind-protect
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
71 (,H key-xor-opad)
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
72 (fillarray key-xor-opad 0)))
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
73 ;; now `key-xor-opad' contains
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
74 ;; H(KEY XOR opad, H(KEY XOR ipad, TEXT)).
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
75 ,(if (and bit (< (/ bit 8) L))
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
76 `(substring key-xor-opad 0 ,(/ bit 8))
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
77 ;; return a copy of `key-xor-opad'.
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
78 `(concat key-xor-opad)))
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
79 ;; cleanup.
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
80 (fillarray key-xor-ipad 0)
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
81 (fillarray key-xor-opad 0)))))
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
82
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
83 (provide 'hmac-def)
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
84
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
85 ;;; arch-tag: 645adcef-b835-4900-a10a-11f636c982b9
6cd3c68dd6ab Move here from ../gnus.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
86 ;;; hmac-def.el ends here