1 ;;; scram-md5.el --- Compute SCRAM-MD5.
3 ;; Copyright (C) 1999 Shuhei KOBAYASHI
5 ;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
6 ;; Keywords: SCRAM-MD5, HMAC-MD5, SASL, IMAP, POP, ACAP
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.
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.
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.
25 ;; This program is implemented from draft-newman-auth-scram-03.txt.
27 ;; It is caller's responsibility to base64-decode challenges and
28 ;; base64-encode responses in IMAP4 AUTHENTICATE command.
30 ;; Passphrase should be longer than 16 bytes. (See RFC 2195)
32 ;; TODO: Provide higher-level (SASL) APIs.
36 (require 'hmac-md5) ; (hmac-md5 TEXT KEY)
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)
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
54 (aset csecinfo 0 (logior (aref csecinfo 0) 1))
56 (lsh (logand buffer-size (lsh 255 16)) -16))
58 (lsh (logand buffer-size (lsh 255 8)) -8))
59 (aset csecinfo 3 (logand buffer-size 255)))
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
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))
73 (aset dst pos (logxor (aref str1 pos) (aref str2 pos)))
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)))
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))))))
91 (defun scram-md5-make-salted-pass (passphrase salt)
93 (hmac-md5 salt passphrase)
94 ;; immediately erase plaintext passphrase from memory.
95 (fillarray passphrase 0)))
97 (defun scram-md5-make-client-key (salted-pass)
98 (md5-binary salted-pass))
100 (defun scram-md5-make-client-verifier (client-key)
101 (md5-binary client-key))
103 (defun scram-md5-make-shared-key (server-msg-1
107 (hmac-md5 (concat server-msg-1 client-msg-1 client-security-info)
110 (defun scram-md5-make-client-proof (client-key shared-key)
111 (scram-xor-string client-key shared-key))
113 (defun scram-md5-make-client-msg-2 (client-security-info client-proof)
114 (concat client-security-info client-proof))
116 (defun scram-md5-authenticate-server (server-msg-1
121 (string= (hmac-md5 (concat client-msg-1 server-msg-1 client-security-info)
122 (hmac-md5 salt salted-pass))
127 ;;; scram-md5.el ends here