85efb08e221f80822e632f1c8cc7dd3178e25175
[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 ;; Keywords: SCRAM-MD5, HMAC-MD5, SASL, IMAP, POP, ACAP
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 ;; This program is implemented from draft-newman-auth-scram-03.txt.
26 ;;
27 ;; It is caller's responsibility to base64-decode challenges and
28 ;; base64-encode responses in IMAP4 AUTHENTICATE command.
29 ;;
30 ;; Passphrase should be longer than 16 bytes. (See RFC 2195)
31 ;;
32 ;; TODO: Provide higher-level (SASL) APIs.
33
34 ;;; Code:
35
36 (require 'hmac-md5)                     ; (hmac-md5 TEXT KEY)
37
38 (defmacro scram-security-info-no-security-layer (security-info)
39   `(eq (logand (aref ,security-info 0) 1) 1))
40 (defmacro scram-security-info-integrity-protection-layer (security-info)
41   `(eq (logand (aref ,security-info 0) 2) 2))
42 (defmacro scram-security-info-buffer-size (security-info)
43   `(let ((ssecinfo ,security-info))
44      (+ (lsh (aref ssecinfo 1) 16)
45         (lsh (aref ssecinfo 2) 8)
46         (aref ssecinfo 3))))
47
48 (defun scram-make-security-info (integrity-protection-layer
49                                  no-security-layer buffer-size)
50   (let ((csecinfo (make-string 4 0)))
51     (when integrity-protection-layer
52       (aset csecinfo 0 2))
53     (if no-security-layer
54         (aset csecinfo 0 (logior (aref csecinfo 0) 1))
55       (aset csecinfo 1
56             (lsh (logand buffer-size (lsh 255 16)) -16))
57       (aset csecinfo 2
58             (lsh (logand buffer-size (lsh 255 8)) -8))
59       (aset csecinfo 3 (logand buffer-size 255)))
60     csecinfo))
61
62 (defun scram-make-unique-nonce ()       ; 8*OCTET, globally unique.
63   ;; For example, concatenated string of process-identifier, system-clock,
64   ;; sequence-number, random-number, and domain-name.
65   (concat "foo" "bar" "baz" "@" (system-name))) ; TEMPORARY
66   
67 (defun scram-xor-string (str1 str2)
68   ;; (length str1) == (length str2) == (length dst) == 16 (in SCRAM-MD5)
69   (let* ((len (length str1))
70          (dst (make-string len 0))
71          (pos 0))
72     (while (< pos len)
73       (aset dst pos (logxor (aref str1 pos) (aref str2 pos)))
74       (setq pos (1+ pos)))
75     dst))
76
77 (defun scram-md5-make-client-msg-1 (authenticate-id &optional authorize-id)
78   "Make an initial client message from AUTHENTICATE-ID and AUTHORIZE-ID.
79 If AUTHORIZE-ID is the same as AUTHENTICATE-ID, it may be omitted."
80   (concat authorize-id "\0" authenticate-id "\0" (scram-make-unique-nonce)))
81
82 (defun scram-md5-parse-server-msg-1 (server-msg-1)
83   "Parse SERVER-MSG-1 and return a list of (SALT SECURITY-INFO SERVICE-ID)."
84   (when (and (> (length server-msg-1) 16)
85              (eq (string-match "[^@]+@[^\0]+\0" server-msg-1 12) 12))
86     (list (substring server-msg-1 0 8)  ; salt
87           (substring server-msg-1 8 12) ; server-security-info
88           (substring server-msg-1       ; service-id
89                      12 (1- (match-end 0))))))
90
91 (defun scram-md5-make-salted-pass (passphrase salt)
92   (unwind-protect
93       (hmac-md5 salt passphrase)
94     ;; immediately erase plaintext passphrase from memory.
95     (fillarray passphrase 0)))
96
97 (defun scram-md5-make-client-key (salted-pass)
98   (md5-binary salted-pass))
99
100 (defun scram-md5-make-client-verifier (client-key)
101   (md5-binary client-key))
102
103 (defun scram-md5-make-shared-key (server-msg-1
104                                   client-msg-1
105                                   client-security-info
106                                   client-verifier)
107   (hmac-md5 (concat server-msg-1 client-msg-1 client-security-info)
108             client-verifier))
109
110 (defun scram-md5-make-client-proof (client-key shared-key)
111   (scram-xor-string client-key shared-key))
112
113 (defun scram-md5-make-client-msg-2 (client-security-info client-proof)
114   (concat client-security-info client-proof))
115
116 (defun scram-md5-authenticate-server (server-msg-1
117                                       server-msg-2
118                                       client-msg-1
119                                       client-security-info
120                                       salt salted-pass)
121   (string= (hmac-md5 (concat client-msg-1 server-msg-1 client-security-info)
122                      (hmac-md5 salt salted-pass))
123            server-msg-2))
124
125 (provide 'scram-md5)
126
127 ;;; scram-md5.el ends here