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