update.
[elisp/flim.git] / scram-md5.el
1 ;;; scram-md5.el --- Compute SCRAM-MD5.
2
3 ;; Copyright (C) 1999 Shuhei KOBAYASHI
4
5 ;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
6 ;;      Kenichi OKADA <okada@opaopa.org>
7 ;; Keywords: SCRAM-MD5, HMAC-MD5, SASL, IMAP, POP, ACAP
8
9 ;; This file is part of FLIM (Faithful Library about Internet Message).
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or
14 ;; (at your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program; see the file COPYING.  If not, write to
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; This program is implemented from draft-newman-auth-scram-03.txt.
29 ;;
30 ;; It is caller's responsibility to base64-decode challenges and
31 ;; base64-encode responses in IMAP4 AUTHENTICATE command.
32 ;;
33 ;; Passphrase should be longer than 16 bytes. (See RFC 2195)
34
35 ;; Examples.
36 ;;
37 ;; (scram-make-security-info nil t 0)
38 ;; => "^A^@^@^@"
39
40 ;;; Code:
41
42 (require 'hmac-md5)
43 (require 'unique-id)
44
45 (defmacro scram-security-info-no-security-layer (security-info)
46   `(eq (logand (aref ,security-info 0) 1) 1))
47 (defmacro scram-security-info-integrity-protection-layer (security-info)
48   `(eq (logand (aref ,security-info 0) 2) 2))
49 (defmacro scram-security-info-buffer-size (security-info)
50   `(let ((ssecinfo ,security-info))
51      (+ (lsh (aref ssecinfo 1) 16)
52         (lsh (aref ssecinfo 2) 8)
53         (aref ssecinfo 3))))
54
55 (defun scram-make-security-info (integrity-protection-layer
56                                  no-security-layer buffer-size)
57   (let ((csecinfo (make-string 4 0)))
58     (when integrity-protection-layer
59       (aset csecinfo 0 2))
60     (if no-security-layer
61         (aset csecinfo 0 (logior (aref csecinfo 0) 1))
62       (aset csecinfo 1
63             (lsh (logand buffer-size (lsh 255 16)) -16))
64       (aset csecinfo 2
65             (lsh (logand buffer-size (lsh 255 8)) -8))
66       (aset csecinfo 3 (logand buffer-size 255)))
67     csecinfo))
68
69 (defun scram-make-unique-nonce ()       ; 8*OCTET, globally unique.
70   ;; For example, concatenated string of process-identifier, system-clock,
71   ;; sequence-number, random-number, and domain-name.
72   (let (id)
73     (unwind-protect
74         (concat "<" 
75                 (setq id (unique-id-m ".sasl"))
76                 "@" (system-name) ">")
77       (fillarray id 0))))
78
79 (defun scram-xor-string (str1 str2)
80   ;; (length str1) == (length str2) == (length dst) == 16 (in SCRAM-MD5)
81   (let* ((len (length str1))
82          (dst (make-string len 0))
83          (pos 0))
84     (while (< pos len)
85       (aset dst pos (logxor (aref str1 pos) (aref str2 pos)))
86       (setq pos (1+ pos)))
87     dst))
88
89 (defun scram-md5-make-client-msg-1 (authenticate-id &optional authorize-id)
90   "Make an initial client message from AUTHENTICATE-ID and AUTHORIZE-ID.
91 If AUTHORIZE-ID is the same as AUTHENTICATE-ID, it may be omitted."
92   (let (nonce)
93     (unwind-protect
94         (concat authorize-id "\0" authenticate-id "\0" 
95                 (setq nonce (scram-make-unique-nonce)))
96       (fillarray nonce 0))))
97
98 (defun scram-md5-parse-server-msg-1 (server-msg-1)
99   "Parse SERVER-MSG-1 and return a list of (SALT SECURITY-INFO SERVICE-ID)."
100   (when (and (> (length server-msg-1) 16)
101              (eq (string-match "[^@]+@[^\0]+\0" server-msg-1 12) 12))
102     (list (substring server-msg-1 0 8)  ; salt
103           (substring server-msg-1 8 12) ; server-security-info
104           (substring server-msg-1       ; service-id
105                      12 (1- (match-end 0))))))
106
107 (defun scram-md5-make-salted-pass (passphrase salt)
108   (hmac-md5 salt passphrase))
109
110 (defun scram-md5-make-client-key (salted-pass)
111   (md5-binary salted-pass))
112
113 (defun scram-md5-make-client-verifier (client-key)
114   (md5-binary client-key))
115
116 (defun scram-md5-make-shared-key (server-msg-1
117                                   client-msg-1
118                                   client-security-info
119                                   client-verifier)
120   (let (buff)
121     (unwind-protect
122         (hmac-md5
123          (setq buff
124                (concat server-msg-1 client-msg-1 client-security-info))
125          client-verifier)
126       (fillarray buff 0))))
127
128 (defun scram-md5-make-client-proof (client-key shared-key)
129   (scram-xor-string client-key shared-key))
130
131 (defun scram-md5-make-client-msg-2 (client-security-info client-proof)
132   (concat client-security-info client-proof))
133
134 (defun scram-md5-make-server-msg-2 (server-msg-1
135                                     client-msg-1
136                                     client-security-info
137                                     salt salted-pass)
138   (let (buff server-salt)
139     (setq server-salt
140           (hmac-md5 salt salted-pass))
141     (unwind-protect
142         (hmac-md5
143          (setq buff
144                (concat
145                 client-msg-1
146                 server-msg-1
147                 client-security-info))
148          server-salt)
149       (fillarray server-salt 0)
150       (fillarray buff 0))))
151
152 (provide 'scram-md5)
153
154 ;;; scram-md5.el ends here