86795
|
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
|
|
35 HMAC function is H(KEY XOR opad, H(KEY XOR ipad, TEXT)):
|
|
36
|
|
37 H is a cryptographic hash function, such as SHA1 and MD5, which takes
|
|
38 a string and return a digest of it (in binary form).
|
|
39 B is a byte-length of a block size of H. (B=64 for both SHA1 and MD5.)
|
|
40 L is a byte-length of hash outputs. (L=16 for MD5, L=20 for SHA1.)
|
|
41 If 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
|