cc59fefafa31c45465edf9d0e2f923a69210c8d2
[elisp/flim.git] / hmac-def.el
1 ;;; hmac-def.el --- Functions/macros for defining HMAC functions.
2
3 ;; Copyright (C) 1999 Shuhei KOBAYASHI
4
5 ;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
6 ;; Keywords: HMAC, RFC 2104
7
8 ;; This program is free software; you can redistribute it and/or
9 ;; modify it under the terms of the GNU General Public License as
10 ;; published by the Free Software Foundation; either version 2, or
11 ;; (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program; see the file COPYING.  If not, write to
20 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;; See RFC 2104, "HMAC: Keyed-Hashing for Message Authentication"
26 ;; for definition of HMAC.
27
28 ;;; Code:
29
30 (require 'hmac-util)
31
32 (defmacro hmac-unhex-string-macro (string length)
33   (let* ((len (eval length))
34          (dst (make-string (/ len 2) 0)))
35     `(let ((str ,string)
36            (dst ,dst)
37            (idx 0)(pos 0))
38        (while (< pos ,len)
39          (aset dst idx (+ (* (hmac-hex-to-int (aref str pos)) 16)
40                           (hmac-hex-to-int (aref str (1+ pos)))))
41          (setq idx (1+ idx)
42                pos (+ 2 pos)))
43        dst)))
44
45 ;; Note that H, B, and L will be evaluated multiple times.  They are
46 ;; usually constants, so I don't want to bother to bind them locally.
47 (defmacro define-hmac-function (name H B L &optional bit)
48   "Define a function NAME which computes HMAC with hash function H.
49
50 HMAC function is H\(KEY XOR opad, H\(KEY XOR ipad, TEXT\)\):
51
52 H is a cryptographic hash function, such as SHA1 and MD5, which takes
53 a string and return a digest of it \(in hexadecimal form\).
54 B is a byte-length of a block size of H. \(B=64 for both SHA1 and MD5.\)
55 L is a byte-length of hash outputs. \(L=16 for MD5, L=20 for SHA1.\)
56 If BIT is non-nil, truncate output to specified bits."
57   `(defun ,name (text key)
58      ,(concat "Compute " (upcase (symbol-name name)) " over TEXT with KEY.")
59      (let ((key-xor-ipad (make-string ,B ?\x36))
60            (key-xor-opad (make-string ,B ?\x5C))
61            (len (length key))
62            (pos 0))
63        (when (> len ,B)
64          (setq key (hmac-unhex-string-macro (,H key) ,(* L 2)))
65          (setq len ,L))
66        (while (< pos len)
67          (aset key-xor-ipad pos (logxor (aref key pos) ?\x36))
68          (aset key-xor-opad pos (logxor (aref key pos) ?\x5C))
69          (setq pos (1+ pos)))
70        ;; If outer `hmac-unhex-string-macro' is removed, return value
71        ;; will be in hexadecimal form.  It is useful for test.
72        ,(if (and bit (< (/ bit 8) L))
73             `(substring
74               (hmac-unhex-string-macro
75                (,H
76                 (concat key-xor-opad
77                         (hmac-unhex-string-macro
78                          (,H (concat key-xor-ipad text))
79                          ,(* L 2))))
80                ,(* L 2))
81               0 ,(/ bit 8))
82           `(hmac-unhex-string-macro
83             (,H
84              (concat key-xor-opad
85                      (hmac-unhex-string-macro
86                       (,H (concat key-xor-ipad text))
87                       ,(* L 2))))
88             ,(* L 2))))))
89
90 (provide 'hmac-def)
91
92 ;;; hmac-def.el ends here.