1 ;;; scram-md5.el --- Compute SCRAM-MD5.
3 ;; Copyright (C) 1999 Shuhei KOBAYASHI
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
9 ;; This file is part of FLIM (Faithful Library about Internet Message).
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.
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.
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.
28 ;; This program is implemented from draft-newman-auth-scram-03.txt.
30 ;; It is caller's responsibility to base64-decode challenges and
31 ;; base64-encode responses in IMAP4 AUTHENTICATE command.
33 ;; Passphrase should be longer than 16 bytes. (See RFC 2195)
37 ;; (scram-make-security-info nil t 0)
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)
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
61 (aset csecinfo 0 (logior (aref csecinfo 0) 1))
63 (lsh (logand buffer-size (lsh 255 16)) -16))
65 (lsh (logand buffer-size (lsh 255 8)) -8))
66 (aset csecinfo 3 (logand buffer-size 255)))
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.
75 (setq id (unique-id-m ".sasl"))
76 "@" (system-name) ">")
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))
85 (aset dst pos (logxor (aref str1 pos) (aref str2 pos)))
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."
94 (concat authorize-id "\0" authenticate-id "\0"
95 (setq nonce (scram-make-unique-nonce)))
96 (fillarray nonce 0))))
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))))))
107 (defun scram-md5-make-salted-pass (passphrase salt)
108 (hmac-md5 salt passphrase))
110 (defun scram-md5-make-client-key (salted-pass)
111 (md5-binary salted-pass))
113 (defun scram-md5-make-client-verifier (client-key)
114 (md5-binary client-key))
116 (defun scram-md5-make-shared-key (server-msg-1
124 (concat server-msg-1 client-msg-1 client-security-info))
126 (fillarray buff 0))))
128 (defun scram-md5-make-client-proof (client-key shared-key)
129 (scram-xor-string client-key shared-key))
131 (defun scram-md5-make-client-msg-2 (client-security-info client-proof)
132 (concat client-security-info client-proof))
134 (defun scram-md5-make-server-msg-2 (server-msg-1
138 (let (buff server-salt)
140 (hmac-md5 salt salted-pass))
147 client-security-info))
149 (fillarray server-salt 0)
150 (fillarray buff 0))))
154 ;;; scram-md5.el ends here