Anonymous avatar Anonymous committed 650afe3

Added SASL library extract from FLIM 1.14.4 packaged in ../flim/.

Comments (0)

Files changed (20)

+# Makefile for XEmacs base lisp code
+
+# This file is part of XEmacs.
+
+# XEmacs is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation; either version 2, or (at your option) any
+# later version.
+
+# XEmacs is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+# for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with XEmacs; see the file COPYING.  If not, write to
+# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+# Boston, MA 02111-1307, USA.
+
+VERSION = 1.1
+AUTHOR_VERSION = 1.14.4
+MAINTAINER = Simon Josefsson <simon@josefsson.org>
+PACKAGE = sasl
+PKG_TYPE = regular
+REQUIRES = 
+CATEGORY = standard
+
+ELCS = hex-util.elc hmac-def.elc hmac-md5.elc  hmac-sha1.elc md4.elc \
+	md5-dl.elc md5-el.elc md5.elc ntlm.elc sasl-cram.elc \
+	sasl-digest.elc sasl-ntlm.elc sasl-scram.elc sasl.elc sha1-dl.elc \
+	sha1-el.elc sha1.elc 
+
+INFO_FILES = sasl.info
+TEXI_FILES = sasl.texi
+
+EXTRA_SOURCES = README
+
+include ../../XEmacs.rules
+
+GENERATED += custom-load.elc
+
+all:: $(ELCS) $(INFO_FILES)
+
+srckit: srckit-std
+
+binkit: binkit-common
+This SASL package was extracted from FLIM
+(ftp://ftp.m17n.org/pub/mule/flim/) release 1.14.4 on 2002-08-02 by
+Simon Josefsson.
+;;; hex-util.el --- Functions to encode/decode hexadecimal string.
+
+;; Copyright (C) 1999, 2001  Free Software Foundation, Inc.
+
+;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
+;; Keywords: data
+
+;; This file is part of FLIM (Faithful Library about Internet Message).
+
+;; This program is free software; you can redistribute it and/or
+;; modify it under the terms of the GNU General Public License as
+;; published by the Free Software Foundation; either version 2, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program; see the file COPYING.  If not, write to
+;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
+
+;;; Commentary:
+
+;;; Code:
+
+(eval-when-compile
+  (defmacro hex-char-to-num (chr)
+    (` (let ((chr (, chr)))
+	 (cond
+	  ((and (<= ?a chr)(<= chr ?f)) (+ (- chr ?a) 10))
+	  ((and (<= ?A chr)(<= chr ?F)) (+ (- chr ?A) 10))
+	  ((and (<= ?0 chr)(<= chr ?9)) (- chr ?0))
+	  (t (error "Invalid hexadecimal digit `%c'" chr))))))
+  (defmacro num-to-hex-char (num)
+    (` (aref "0123456789abcdef" (, num)))))
+
+(defun decode-hex-string (string)
+  "Decode hexadecimal STRING to octet string."
+  (let* ((len (length string))
+	 (dst (make-string (/ len 2) 0))
+	 (idx 0)(pos 0))
+    (while (< pos len)
+;;; logior and lsh are not byte-coded.
+;;;  (aset dst idx (logior (lsh (hex-char-to-num (aref string pos)) 4)
+;;; 			    (hex-char-to-num (aref string (1+ pos)))))
+      (aset dst idx (+ (* (hex-char-to-num (aref string pos)) 16)
+		       (hex-char-to-num (aref string (1+ pos)))))
+      (setq idx (1+ idx)
+            pos (+ 2 pos)))
+    dst))
+
+(defun encode-hex-string (string)
+  "Encode octet STRING to hexadecimal string."
+  (let* ((len (length string))
+	 (dst (make-string (* len 2) 0))
+	 (idx 0)(pos 0))
+    (while (< pos len)
+;;; logand and lsh are not byte-coded.
+;;;  (aset dst idx (num-to-hex-char (logand (lsh (aref string pos) -4) 15)))
+      (aset dst idx (num-to-hex-char (/ (aref string pos) 16)))
+      (setq idx (1+ idx))
+;;;  (aset dst idx (num-to-hex-char (logand (aref string pos) 15)))
+      (aset dst idx (num-to-hex-char (% (aref string pos) 16)))
+      (setq idx (1+ idx)
+            pos (1+ pos)))
+    dst))
+
+(provide 'hex-util)
+
+;;; hex-util.el ends here
+;;; hmac-def.el --- A macro for defining HMAC functions.
+
+;; Copyright (C) 1999, 2001  Free Software Foundation, Inc.
+
+;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
+;; Keywords: HMAC, RFC 2104
+
+;; This file is part of FLIM (Faithful Library about Internet Message).
+
+;; This program is free software; you can redistribute it and/or
+;; modify it under the terms of the GNU General Public License as
+;; published by the Free Software Foundation; either version 2, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program; see the file COPYING.  If not, write to
+;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
+
+;;; Commentary:
+
+;; This program is implemented from RFC 2104,
+;; "HMAC: Keyed-Hashing for Message Authentication".
+
+;;; Code:
+
+(defmacro define-hmac-function (name H B L &optional bit)
+  "Define a function NAME(TEXT KEY) which computes HMAC with function H.
+
+HMAC function is H(KEY XOR opad, H(KEY XOR ipad, TEXT)):
+
+H is a cryptographic hash function, such as SHA1 and MD5, which takes
+a string and return a digest of it (in binary form).
+B is a byte-length of a block size of H. (B=64 for both SHA1 and MD5.)
+L is a byte-length of hash outputs. (L=16 for MD5, L=20 for SHA1.)
+If BIT is non-nil, truncate output to specified bits."
+  (` (defun (, name) (text key)
+       (, (concat "Compute "
+		  (upcase (symbol-name name))
+		  " over TEXT with KEY."))
+       (let ((key-xor-ipad (make-string (, B) ?\x36))
+	     (key-xor-opad (make-string (, B) ?\x5C))
+	     (len (length key))
+	     (pos 0))
+	 (unwind-protect
+	     (progn
+	       ;; if `key' is longer than the block size, apply hash function
+	       ;; to `key' and use the result as a real `key'.
+	       (if (> len (, B))
+		   (setq key ((, H) key)
+			 len (, L)))
+	       (while (< pos len)
+		 (aset key-xor-ipad pos (logxor (aref key pos) ?\x36))
+		 (aset key-xor-opad pos (logxor (aref key pos) ?\x5C))
+		 (setq pos (1+ pos)))
+	       (setq key-xor-ipad (unwind-protect
+				      (concat key-xor-ipad text)
+				    (fillarray key-xor-ipad 0))
+		     key-xor-ipad (unwind-protect
+				      ((, H) key-xor-ipad)
+				    (fillarray key-xor-ipad 0))
+		     key-xor-opad (unwind-protect
+				      (concat key-xor-opad key-xor-ipad)
+				    (fillarray key-xor-opad 0))
+		     key-xor-opad (unwind-protect
+				      ((, H) key-xor-opad)
+				    (fillarray key-xor-opad 0)))
+	       ;; now `key-xor-opad' contains
+	       ;; H(KEY XOR opad, H(KEY XOR ipad, TEXT)).
+	       (, (if (and bit (< (/ bit 8) L))
+		      (` (substring key-xor-opad 0 (, (/ bit 8))))
+		    ;; return a copy of `key-xor-opad'.
+		    (` (concat key-xor-opad)))))
+	   ;; cleanup.
+	   (fillarray key-xor-ipad 0)
+	   (fillarray key-xor-opad 0))))))
+
+(provide 'hmac-def)
+
+;;; hmac-def.el ends here
+;;; hmac-md5.el --- Compute HMAC-MD5.
+
+;; Copyright (C) 1999, 2001  Free Software Foundation, Inc.
+
+;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
+;; Keywords: HMAC, RFC 2104, HMAC-MD5, MD5, KEYED-MD5, CRAM-MD5
+
+;; This file is part of FLIM (Faithful Library about Internet Message).
+
+;; This program is free software; you can redistribute it and/or
+;; modify it under the terms of the GNU General Public License as
+;; published by the Free Software Foundation; either version 2, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program; see the file COPYING.  If not, write to
+;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
+
+;;; Commentary:
+
+;; Test cases from RFC 2202, "Test Cases for HMAC-MD5 and HMAC-SHA-1".
+;;
+;; (encode-hex-string (hmac-md5 "Hi There" (make-string 16 ?\x0b)))
+;;  => "9294727a3638bb1c13f48ef8158bfc9d"
+;;
+;; (encode-hex-string (hmac-md5 "what do ya want for nothing?" "Jefe"))
+;;  => "750c783e6ab0b503eaa86e310a5db738"
+;;
+;; (encode-hex-string (hmac-md5 (make-string 50 ?\xdd) (make-string 16 ?\xaa)))
+;;  => "56be34521d144c88dbb8c733f0e8b3f6"
+;;
+;; (encode-hex-string
+;;  (hmac-md5
+;;   (make-string 50 ?\xcd)
+;;   (decode-hex-string "0102030405060708090a0b0c0d0e0f10111213141516171819")))
+;;  => "697eaf0aca3a3aea3a75164746ffaa79"
+;;
+;; (encode-hex-string
+;;  (hmac-md5 "Test With Truncation" (make-string 16 ?\x0c)))
+;;  => "56461ef2342edc00f9bab995690efd4c"
+;;
+;; (encode-hex-string
+;;  (hmac-md5-96 "Test With Truncation" (make-string 16 ?\x0c)))
+;;  => "56461ef2342edc00f9bab995"
+;;
+;; (encode-hex-string
+;;  (hmac-md5
+;;   "Test Using Larger Than Block-Size Key - Hash Key First"
+;;   (make-string 80 ?\xaa)))
+;;  => "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd"
+;;
+;; (encode-hex-string
+;;  (hmac-md5
+;;   "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
+;;   (make-string 80 ?\xaa)))
+;;  => "6f630fad67cda0ee1fb1f562db3aa53e"
+
+;;; Code:
+
+(eval-when-compile (require 'hmac-def))
+(require 'hex-util)			; (decode-hex-string STRING)
+(require 'md5)				; expects (md5 STRING)
+
+;; To share *.elc files between Emacs w/ and w/o DL patch,
+;; this check must be done at load-time.
+(cond
+ ((fboundp 'md5-binary)
+  ;; do nothing.
+  )
+ ((condition-case nil
+       ;; `md5' of v21 takes 4th arg CODING (and 5th arg NOERROR).
+       (md5 "" nil nil 'binary)		; => "d41d8cd98f00b204e9800998ecf8427e"
+     (wrong-number-of-arguments nil))
+  (defun md5-binary (string)
+    "Return the MD5 of STRING in binary form."
+   (decode-hex-string (md5 string nil nil 'binary))))
+ (t
+  (defun md5-binary (string)
+    "Return the MD5 of STRING in binary form."
+   (decode-hex-string (md5 string)))))
+
+(define-hmac-function hmac-md5 md5-binary 64 16) ; => (hmac-md5 TEXT KEY)
+(define-hmac-function hmac-md5-96 md5-binary 64 16 96)
+
+(provide 'hmac-md5)
+
+;;; hmac-md5.el ends here
+;;; hmac-sha1.el --- Compute HMAC-SHA1.
+
+;; Copyright (C) 1999, 2001  Free Software Foundation, Inc.
+
+;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
+;; Keywords: HMAC, RFC 2104, HMAC-SHA1, SHA1, Cancel-Lock
+
+;; This file is part of FLIM (Faithful Library about Internet Message).
+
+;; This program is free software; you can redistribute it and/or
+;; modify it under the terms of the GNU General Public License as
+;; published by the Free Software Foundation; either version 2, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program; see the file COPYING.  If not, write to
+;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
+
+;;; Commentary:
+
+;; Test cases from RFC 2202, "Test Cases for HMAC-MD5 and HMAC-SHA-1".
+;;
+;; (encode-hex-string (hmac-sha1 "Hi There" (make-string 20 ?\x0b)))
+;;  => "b617318655057264e28bc0b6fb378c8ef146be00"
+;;
+;; (encode-hex-string (hmac-sha1 "what do ya want for nothing?" "Jefe"))
+;;  => "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79"
+;;
+;; (encode-hex-string (hmac-sha1 (make-string 50 ?\xdd) (make-string 20 ?\xaa)))
+;;  => "125d7342b9ac11cd91a39af48aa17b4f63f175d3"
+;;
+;; (encode-hex-string
+;;  (hmac-sha1
+;;   (make-string 50 ?\xcd)
+;;   (decode-hex-string "0102030405060708090a0b0c0d0e0f10111213141516171819")))
+;;  => "4c9007f4026250c6bc8414f9bf50c86c2d7235da"
+;;
+;; (encode-hex-string
+;;  (hmac-sha1 "Test With Truncation" (make-string 20 ?\x0c)))
+;;  => "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04"
+;;
+;; (encode-hex-string
+;;  (hmac-sha1-96 "Test With Truncation" (make-string 20 ?\x0c)))
+;;  => "4c1a03424b55e07fe7f27be1"
+;;
+;; (encode-hex-string
+;;  (hmac-sha1
+;;   "Test Using Larger Than Block-Size Key - Hash Key First"
+;;   (make-string 80 ?\xaa)))
+;;  => "aa4ae5e15272d00e95705637ce8a3b55ed402112"
+;;
+;; (encode-hex-string
+;;  (hmac-sha1
+;;   "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
+;;   (make-string 80 ?\xaa)))
+;;  => "e8e99d0f45237d786d6bbaa7965c7808bbff1a91"
+
+;;; Code:
+
+(eval-when-compile (require 'hmac-def))
+(require 'hex-util)			; (decode-hex-string STRING)
+(require 'sha1)				; expects (sha1 STRING)
+
+;; To share *.elc files between Emacs w/ and w/o DL patch,
+;; this check must be done at load-time.
+(cond
+ ((fboundp 'sha1-binary)
+  ;; do nothing.
+  )
+ (t
+  (defun sha1-binary (string)
+    "Return the SHA1 of STRING in binary form."
+    (decode-hex-string (sha1 string)))))
+
+(define-hmac-function hmac-sha1 sha1-binary 64 20) ; => (hmac-sha1 TEXT KEY)
+(define-hmac-function hmac-sha1-96 sha1-binary 64 20 96)
+
+(provide 'hmac-sha1)
+
+;;; hmac-sha1.el ends here
+;;; md4.el --- MD4 Message Digest Algorithm.
+
+;; Copyright (C) 2001 Taro Kawagishi
+;; Author: Taro Kawagishi <tarok@transpulse.org>
+;; Keywords: MD4
+;; Version: 1.00
+;; Created: February 2001
+
+;; This file is part of FLIM (Faithful Library about Internet Message).
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 2, or (at your option)
+;; any later version.
+;;
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+;;
+;; You should have received a copy of the GNU General Public License
+;; along with this program; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
+
+;;; Code:
+
+;;;
+;;; MD4 hash calculation
+
+(defun print-int32 (int32)
+  "print 32 bits integer in 4 bytes string as little endian"
+  (let ((h (car int32)) (l (cdr int32)))
+    (list (logand l 255) (lsh l -8) (logand h 255) (lsh h -8))))
+
+(defun print-string-hexa (str)
+  "print a string in hexadecimal"
+  (let (out)
+    (mapcar (function (lambda (x) (concat out (format "%x" x)))) str)))
+
+(defvar md4-buffer (make-vector 4 '(0 . 0))
+  "work buffer of four 32-bit integers")
+
+(defun md4 (in n)
+  "Returns the MD4 hash string of 16 bytes long for a string IN of N
+bytes long.  N is required to handle strings containing character 0."
+  (let (m
+	(b (cons 0 (* n 8)))
+	(i 0)
+	(buf (make-string 128 0)) c4)
+    ;; initial values
+    (aset md4-buffer 0 '(26437 . 8961))		;0x67452301
+    (aset md4-buffer 1 '(61389 . 43913))	;0xefcdab89
+    (aset md4-buffer 2 '(39098 . 56574))	;0x98badcfe
+    (aset md4-buffer 3 '(4146 . 21622))		;0x10325476
+
+    ;; process the string in 64 bits chunks
+    (while (> n 64)
+      (setq m (md4-copy64 (substring in 0 64)))
+      (md4-64 m)
+      (setq in (substring in 64))
+      (setq n (- n 64)))
+
+    ;; process the rest of the string (length is now n <= 64)
+    (setq i 0)
+    (while (< i n)
+      (aset buf i (aref in i))
+      (setq i (1+ i)))
+    (aset buf n 128)			;0x80
+    (if (<= n 55)
+	(progn
+	  (setq c4 (md4-pack-int32 b))
+	  (aset buf 56 (aref c4 0))
+	  (aset buf 57 (aref c4 1))
+	  (aset buf 58 (aref c4 2))
+	  (aset buf 59 (aref c4 3))
+	  (setq m (md4-copy64 buf))
+	  (md4-64 m))
+      ;; else
+      (setq c4 (md4-pack-int32 b))
+      (aset buf 120 (aref c4 0))
+      (aset buf 121 (aref c4 1))
+      (aset buf 122 (aref c4 2))
+      (aset buf 123 (aref c4 3))
+      (setq m (md4-copy64 buf))
+      (md4-64 m)
+      (setq m (md4-copy64 (substring buf 64)))
+      (md4-64 m)))
+
+    (concat (md4-pack-int32 (aref md4-buffer 0))
+	    (md4-pack-int32 (aref md4-buffer 1))
+	    (md4-pack-int32 (aref md4-buffer 2))
+	    (md4-pack-int32 (aref md4-buffer 3))))
+
+(defsubst md4-F (x y z) (logior (logand x y) (logand (lognot x) z)))
+(defsubst md4-G (x y z) (logior (logand x y) (logand x z) (logand y z)))
+(defsubst md4-H (x y z) (logxor x y z))
+
+(defmacro md4-make-step (name func)
+  (`
+   (defun (, name) (a b c d xk s ac)
+     (let*
+         ((h1 (+ (car a) ((, func) (car b) (car c) (car d)) (car xk) (car ac)))
+          (l1 (+ (cdr a) ((, func) (cdr b) (cdr c) (cdr d)) (cdr xk) (cdr ac)))
+          (h2 (logand 65535 (+ h1 (lsh l1 -16))))
+          (l2 (logand 65535 l1))
+	  ;; cyclic shift of 32 bits integer
+          (h3 (logand 65535 (if (> s 15)
+                                (+ (lsh h2 (- s 32)) (lsh l2 (- s 16)))
+                              (+ (lsh h2 s) (lsh l2 (- s 16))))))
+          (l3 (logand 65535 (if (> s 15)
+                                (+ (lsh l2 (- s 32)) (lsh h2 (- s 16)))
+                              (+ (lsh l2 s) (lsh h2 (- s 16)))))))
+       (cons h3 l3)))))
+
+(md4-make-step md4-round1 md4-F)
+(md4-make-step md4-round2 md4-G)
+(md4-make-step md4-round3 md4-H)
+
+(defsubst md4-add (x y)
+  "Return 32-bit sum of 32-bit integers X and Y."
+  (let ((h (+ (car x) (car y)))
+	(l (+ (cdr x) (cdr y))))
+    (cons (logand 65535 (+ h (lsh l -16))) (logand 65535 l))))
+
+(defsubst md4-and (x y)
+  (cons (logand (car x) (car y)) (logand (cdr x) (cdr y))))
+
+(defun md4-64 (m)
+  "Calculate md4 of 64 bytes chunk M which is represented as 16 pairs of
+32 bits integers.  The resulting md4 value is placed in md4-buffer."
+  (let ((a (aref md4-buffer 0))
+	(b (aref md4-buffer 1))
+	(c (aref md4-buffer 2))
+	(d (aref md4-buffer 3)))
+    (setq a (md4-round1 a b c d (aref m  0)   3 '(0 . 0))
+	  d (md4-round1 d a b c (aref m  1)   7 '(0 . 0))
+	  c (md4-round1 c d a b (aref m  2)  11 '(0 . 0))
+	  b (md4-round1 b c d a (aref m  3)  19 '(0 . 0))
+	  a (md4-round1 a b c d (aref m  4)   3 '(0 . 0))
+	  d (md4-round1 d a b c (aref m  5)   7 '(0 . 0))
+	  c (md4-round1 c d a b (aref m  6)  11 '(0 . 0))
+	  b (md4-round1 b c d a (aref m  7)  19 '(0 . 0))
+	  a (md4-round1 a b c d (aref m  8)   3 '(0 . 0))
+	  d (md4-round1 d a b c (aref m  9)   7 '(0 . 0))
+	  c (md4-round1 c d a b (aref m 10)  11 '(0 . 0))
+	  b (md4-round1 b c d a (aref m 11)  19 '(0 . 0))
+	  a (md4-round1 a b c d (aref m 12)   3 '(0 . 0))
+	  d (md4-round1 d a b c (aref m 13)   7 '(0 . 0))
+	  c (md4-round1 c d a b (aref m 14)  11 '(0 . 0))
+	  b (md4-round1 b c d a (aref m 15)  19 '(0 . 0))
+
+	  a (md4-round2 a b c d (aref m  0)   3 '(23170 . 31129)) ;0x5A827999
+	  d (md4-round2 d a b c (aref m  4)   5 '(23170 . 31129))
+	  c (md4-round2 c d a b (aref m  8)   9 '(23170 . 31129))
+	  b (md4-round2 b c d a (aref m 12)  13 '(23170 . 31129))
+	  a (md4-round2 a b c d (aref m  1)   3 '(23170 . 31129))
+	  d (md4-round2 d a b c (aref m  5)   5 '(23170 . 31129))
+	  c (md4-round2 c d a b (aref m  9)   9 '(23170 . 31129))
+	  b (md4-round2 b c d a (aref m 13)  13 '(23170 . 31129))
+	  a (md4-round2 a b c d (aref m  2)   3 '(23170 . 31129))
+	  d (md4-round2 d a b c (aref m  6)   5 '(23170 . 31129))
+	  c (md4-round2 c d a b (aref m 10)   9 '(23170 . 31129))
+	  b (md4-round2 b c d a (aref m 14)  13 '(23170 . 31129))
+	  a (md4-round2 a b c d (aref m  3)   3 '(23170 . 31129))
+	  d (md4-round2 d a b c (aref m  7)   5 '(23170 . 31129))
+	  c (md4-round2 c d a b (aref m 11)   9 '(23170 . 31129))
+	  b (md4-round2 b c d a (aref m 15)  13 '(23170 . 31129))
+
+	  a (md4-round3 a b c d (aref m  0)   3 '(28377 . 60321)) ;0x6ED9EBA1
+	  d (md4-round3 d a b c (aref m  8)   9 '(28377 . 60321))
+	  c (md4-round3 c d a b (aref m  4)  11 '(28377 . 60321))
+	  b (md4-round3 b c d a (aref m 12)  15 '(28377 . 60321))
+	  a (md4-round3 a b c d (aref m  2)   3 '(28377 . 60321))
+	  d (md4-round3 d a b c (aref m 10)   9 '(28377 . 60321))
+	  c (md4-round3 c d a b (aref m  6)  11 '(28377 . 60321))
+	  b (md4-round3 b c d a (aref m 14)  15 '(28377 . 60321))
+	  a (md4-round3 a b c d (aref m  1)   3 '(28377 . 60321))
+	  d (md4-round3 d a b c (aref m  9)   9 '(28377 . 60321))
+	  c (md4-round3 c d a b (aref m  5)  11 '(28377 . 60321))
+	  b (md4-round3 b c d a (aref m 13)  15 '(28377 . 60321))
+	  a (md4-round3 a b c d (aref m  3)   3 '(28377 . 60321))
+	  d (md4-round3 d a b c (aref m 11)   9 '(28377 . 60321))
+	  c (md4-round3 c d a b (aref m  7)  11 '(28377 . 60321))
+	  b (md4-round3 b c d a (aref m 15)  15 '(28377 . 60321)))
+
+    (aset md4-buffer 0 (md4-add a (aref md4-buffer 0)))
+    (aset md4-buffer 1 (md4-add b (aref md4-buffer 1)))
+    (aset md4-buffer 2 (md4-add c (aref md4-buffer 2)))
+    (aset md4-buffer 3 (md4-add d (aref md4-buffer 3)))
+    ))
+
+(defun md4-copy64 (seq)
+  "Unpack a 64 bytes string into 16 pairs of 32 bits integers."
+  (let ((int32s (make-vector 16 0)) (i 0) j)
+    (while (< i 16)
+      (setq j (* i 4))
+      (aset int32s i (cons (+ (aref seq (+ j 2)) (lsh (aref seq (+ j 3)) 8))
+			   (+ (aref seq j) (lsh (aref seq (1+ j)) 8))))
+      (setq i (1+ i)))
+    int32s))
+
+;;;
+;;; sub functions
+
+(defun md4-pack-int16 (int16)
+  "Pack 16 bits integer in 2 bytes string as little endian."
+  (let ((str (make-string 2 0)))
+    (aset str 0 (logand int16 255))
+    (aset str 1 (lsh int16 -8))
+    str))
+
+(defun md4-pack-int32 (int32)
+  "Pack 32 bits integer in a 4 bytes string as little endian.  A 32 bits
+integer is represented as a pair of two 16 bits integers (cons high low)."
+  (let ((str (make-string 4 0))
+	(h (car int32)) (l (cdr int32)))
+    (aset str 0 (logand l 255))
+    (aset str 1 (lsh l -8))
+    (aset str 2 (logand h 255))
+    (aset str 3 (lsh h -8))
+    str))
+
+(defun md4-unpack-int16 (str)
+  (if (eq 2 (length str))
+      (+ (lsh (aref str 1) 8) (aref str 0))
+    (error "%s is not 2 bytes long" str)))
+
+(defun md4-unpack-int32 (str)
+  (if (eq 4 (length str))
+      (cons (+ (lsh (aref str 3) 8) (aref str 2))
+	    (+ (lsh (aref str 1) 8) (aref str 0)))
+    (error "%s is not 4 bytes long" str)))
+
+(provide 'md4)
+
+;;; md4.el ends here
+;;; md5-dl.el --- MD5 Message Digest Algorithm using DL module.
+
+;; Copyright (C) 1999, 2001  Free Software Foundation, Inc.
+
+;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
+;; Keywords: MD5, RFC 1321
+
+;; This file is part of FLIM (Faithful Library about Internet Message).
+
+;; This program is free software; you can redistribute it and/or
+;; modify it under the terms of the GNU General Public License as
+;; published by the Free Software Foundation; either version 2, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program; see the file COPYING.  If not, write to
+;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
+
+;;; Commentary:
+
+;;; Code:
+
+(provide 'md5-dl)			; beware of circular dependency.
+(eval-when-compile (require 'md5))	; md5-dl-module.
+
+;;; This file is loaded (from "md5.el") only when md5-dl-module is exists.
+(defvar md5-dl-handle (dynamic-link md5-dl-module))
+
+;;; md5-dl-module provides `md5-string'.
+(dynamic-call "emacs_md5_init" md5-dl-handle)
+
+(defun md5-region (beg end)
+  (md5-string (buffer-substring-no-properties beg end)))
+
+;;; Note that v21 `md5' takes two more args: CODING and NOERROR.
+(defun md5 (object &optional beg end)
+  "Return the MD5 (a secure message digest algorithm) of an object.
+OBJECT is either a string or a buffer.
+Optional arguments BEG and END denote buffer positions for computing the
+hash of a portion of OBJECT."
+  (if (stringp object)
+      (md5-string object)
+    (save-excursion
+      (set-buffer object)
+      (md5-region (or beg (point-min)) (or end (point-max))))))
+
+(provide 'md5-dl)
+
+;;; md5-dl.el ends here
+;;; md5.el -- MD5 Message Digest Algorithm
+;;; Gareth Rees <gdr11@cl.cam.ac.uk>
+
+;; LCD Archive Entry:
+;; md5|Gareth Rees|gdr11@cl.cam.ac.uk|
+;; MD5 cryptographic message digest algorithm|
+;; 13-Nov-95|1.0|~/misc/md5.el.Z|
+
+;;; Details: ------------------------------------------------------------------
+
+;; This is a direct translation into Emacs LISP of the reference C
+;; implementation of the MD5 Message-Digest Algorithm written by RSA
+;; Data Security, Inc.
+;; 
+;; The algorithm takes a message (that is, a string of bytes) and
+;; computes a 16-byte checksum or "digest" for the message.  This digest
+;; is supposed to be cryptographically strong in the sense that if you
+;; are given a 16-byte digest D, then there is no easier way to
+;; construct a message whose digest is D than to exhaustively search the
+;; space of messages.  However, the robustness of the algorithm has not
+;; been proven, and a similar algorithm (MD4) was shown to be unsound,
+;; so treat with caution!
+;; 
+;; The C algorithm uses 32-bit integers; because GNU Emacs
+;; implementations provide 28-bit integers (with 24-bit integers on
+;; versions prior to 19.29), the code represents a 32-bit integer as the
+;; cons of two 16-bit integers.  The most significant word is stored in
+;; the car and the least significant in the cdr.  The algorithm requires
+;; at least 17 bits of integer representation in order to represent the
+;; carry from a 16-bit addition.
+
+;;; Usage: --------------------------------------------------------------------
+
+;; To compute the MD5 Message Digest for a message M (represented as a
+;; string or as a vector of bytes), call
+;; 
+;;   (md5-encode M)
+;; 
+;; which returns the message digest as a vector of 16 bytes.  If you
+;; need to supply the message in pieces M1, M2, ... Mn, then call
+;; 
+;;   (md5-init)
+;;   (md5-update M1)
+;;   (md5-update M2)
+;;   ...
+;;   (md5-update Mn)
+;;   (md5-final)
+
+;;; Copyright and licence: ----------------------------------------------------
+
+;; Copyright (C) 1995, 1996, 1997 by Gareth Rees
+;; Derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm
+;; 
+;; md5.el is free software; you can redistribute it and/or modify it
+;; under the terms of the GNU General Public License as published by the
+;; Free Software Foundation; either version 2, or (at your option) any
+;; later version.
+;; 
+;; md5.el is distributed in the hope that it will be useful, but WITHOUT
+;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+;; FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+;; for more details.
+;; 
+;; The original copyright notice is given below, as required by the
+;; licence for the original code.  This code is distributed under *both*
+;; RSA's original licence and the GNU General Public Licence.  (There
+;; should be no problems, as the former is more liberal than the
+;; latter).
+
+;;; Original copyright notice: ------------------------------------------------
+
+;; Copyright (C) 1990, RSA Data Security, Inc. All rights reserved.
+;;
+;; License to copy and use this software is granted provided that it is
+;; identified as the "RSA Data Security, Inc. MD5 Message- Digest
+;; Algorithm" in all material mentioning or referencing this software or
+;; this function.
+;;
+;; License is also granted to make and use derivative works provided
+;; that such works are identified as "derived from the RSA Data
+;; Security, Inc. MD5 Message-Digest Algorithm" in all material
+;; mentioning or referencing the derived work.
+;;
+;; RSA Data Security, Inc. makes no representations concerning either
+;; the merchantability of this software or the suitability of this
+;; software for any particular purpose.  It is provided "as is" without
+;; express or implied warranty of any kind.
+;;
+;; These notices must be retained in any copies of any part of this
+;; documentation and/or software.
+
+;;; Code: ---------------------------------------------------------------------
+
+(defvar md5-program "md5"
+  "*Program that reads a message on its standard input and writes an
+MD5 digest on its output.")
+
+(defvar md5-maximum-internal-length 4096
+  "*The maximum size of a piece of data that should use the MD5 routines
+written in lisp.  If a message exceeds this, it will be run through an
+external filter for processing.  Also see the `md5-program' variable.
+This variable has no effect if you call the md5-init|update|final
+functions - only used by the `md5' function's simpler interface.")
+
+(defvar md5-bits (make-vector 4 0)
+  "Number of bits handled, modulo 2^64.
+Represented as four 16-bit numbers, least significant first.")
+(defvar md5-buffer (make-vector 4 '(0 . 0))
+  "Scratch buffer (four 32-bit integers).")
+(defvar md5-input (make-vector 64 0)
+  "Input buffer (64 bytes).")
+
+(defun md5-unhex (x)
+  (if (> x ?9)
+      (if (>= x ?a)
+	  (+ 10 (- x ?a))
+	(+ 10 (- x ?A)))
+    (- x ?0)))
+
+(defun md5-encode (message)
+  "Encodes MESSAGE using the MD5 message digest algorithm.
+MESSAGE must be a string or an array of bytes.
+Returns a vector of 16 bytes containing the message digest."
+  (if (or (null md5-maximum-internal-length)
+	   (<= (length message) md5-maximum-internal-length))
+      (progn
+	(md5-init)
+	(md5-update message)
+	(md5-final))
+    (save-excursion
+      (set-buffer (get-buffer-create " *md5-work*"))
+      (erase-buffer)
+      (insert message)
+      (call-process-region (point-min) (point-max)
+			   md5-program
+			   t (current-buffer))
+      ;; MD5 digest is 32 chars long
+      ;; mddriver adds a newline to make neaten output for tty
+      ;; viewing, make sure we leave it behind.
+      (let ((data (buffer-substring (point-min) (+ (point-min) 32)))
+	    (vec (make-vector 16 0))
+	    (ctr 0))
+	(while (< ctr 16)
+	  (aset vec ctr (+ (* 16 (md5-unhex (aref data (* ctr 2))))
+			   (md5-unhex (aref data (1+ (* ctr 2))))))
+	  (setq ctr (1+ ctr)))))))
+
+(defsubst md5-add (x y)
+  "Return 32-bit sum of 32-bit integers X and Y."
+  (let ((m (+ (car x) (car y)))
+        (l (+ (cdr x) (cdr y))))
+    (cons (logand 65535 (+ m (lsh l -16))) (logand l 65535))))
+
+;; FF, GG, HH and II are basic MD5 functions, providing transformations
+;; for rounds 1, 2, 3 and 4 respectively.  Each function follows this
+;; pattern of computation (where ROTATE(x,y) means rotate 32-bit value x
+;; by y bits to the left):
+;; 
+;;   FF(a,b,c,d,x,s,ac) = ROTATE(a + F(b,c,d) + x + ac,s) + b
+;; 
+;; so we use the macro `md5-make-step' to construct each one.  The
+;; helper functions F, G, H and I operate on 16-bit numbers; the full
+;; operation splits its inputs, operates on the halves separately and
+;; then puts the results together.
+
+(defsubst md5-F (x y z) (logior (logand x y) (logand (lognot x) z)))
+(defsubst md5-G (x y z) (logior (logand x z) (logand y (lognot z))))
+(defsubst md5-H (x y z) (logxor x y z))
+(defsubst md5-I (x y z) (logxor y (logior x (logand 65535 (lognot z)))))
+
+(defmacro md5-make-step (name func)
+  (`
+   (defun (, name) (a b c d x s ac)
+     (let*
+         ((m1 (+ (car a) ((, func) (car b) (car c) (car d)) (car x) (car ac)))
+          (l1 (+ (cdr a) ((, func) (cdr b) (cdr c) (cdr d)) (cdr x) (cdr ac)))
+          (m2 (logand 65535 (+ m1 (lsh l1 -16))))
+          (l2 (logand 65535 l1))
+          (m3 (logand 65535 (if (> s 15)
+                                (+ (lsh m2 (- s 32)) (lsh l2 (- s 16)))
+                              (+ (lsh m2 s) (lsh l2 (- s 16))))))
+          (l3 (logand 65535 (if (> s 15)
+                                (+ (lsh l2 (- s 32)) (lsh m2 (- s 16)))
+                              (+ (lsh l2 s) (lsh m2 (- s 16)))))))
+       (md5-add (cons m3 l3) b)))))
+
+(md5-make-step md5-FF md5-F)
+(md5-make-step md5-GG md5-G)
+(md5-make-step md5-HH md5-H)
+(md5-make-step md5-II md5-I)
+
+(defun md5-init ()
+  "Initialise the state of the message-digest routines."
+  (aset md5-bits 0 0)
+  (aset md5-bits 1 0)
+  (aset md5-bits 2 0)
+  (aset md5-bits 3 0)
+  (aset md5-buffer 0 '(26437 .  8961))
+  (aset md5-buffer 1 '(61389 . 43913))
+  (aset md5-buffer 2 '(39098 . 56574))
+  (aset md5-buffer 3 '( 4146 . 21622)))
+
+(defun md5-update (string)
+  "Update the current MD5 state with STRING (an array of bytes)."
+  (let ((len (length string))
+        (i 0)
+        (j 0))
+    (while (< i len)
+      ;; Compute number of bytes modulo 64
+      (setq j (% (/ (aref md5-bits 0) 8) 64))
+
+      ;; Store this byte (truncating to 8 bits to be sure)
+      (aset md5-input j (logand 255 (aref string i)))
+
+      ;; Update number of bits by 8 (modulo 2^64)
+      (let ((c 8) (k 0))
+        (while (and (> c 0) (< k 4))
+          (let ((b (aref md5-bits k)))
+            (aset md5-bits k (logand 65535 (+ b c)))
+            (setq c (if (> b (- 65535 c)) 1 0)
+                  k (1+ k)))))
+
+      ;; Increment number of bytes processed
+      (setq i (1+ i))
+
+      ;; When 64 bytes accumulated, pack them into sixteen 32-bit
+      ;; integers in the array `in' and then tranform them.
+      (if (= j 63)
+          (let ((in (make-vector 16 (cons 0 0)))
+                (k 0)
+                (kk 0))
+            (while (< k 16)
+              (aset in k (md5-pack md5-input kk))
+              (setq k (+ k 1) kk (+ kk 4)))
+            (md5-transform in))))))
+
+(defun md5-pack (array i)
+  "Pack the four bytes at ARRAY reference I to I+3 into a 32-bit integer."
+  (cons (+ (lsh (aref array (+ i 3)) 8) (aref array (+ i 2)))
+        (+ (lsh (aref array (+ i 1)) 8) (aref array (+ i 0)))))
+
+(defun md5-byte (array n b)
+  "Unpack byte B (0 to 3) from Nth member of ARRAY of 32-bit integers."
+  (let ((e (aref array n)))
+    (cond ((eq b 0) (logand 255 (cdr e)))
+          ((eq b 1) (lsh (cdr e) -8))
+          ((eq b 2) (logand 255 (car e)))
+          ((eq b 3) (lsh (car e) -8)))))
+
+(defun md5-final ()
+  (let ((in (make-vector 16 (cons 0 0)))
+        (j 0)
+        (digest (make-vector 16 0))
+        (padding))
+
+    ;; Save the number of bits in the message
+    (aset in 14 (cons (aref md5-bits 1) (aref md5-bits 0)))
+    (aset in 15 (cons (aref md5-bits 3) (aref md5-bits 2)))
+
+    ;; Compute number of bytes modulo 64
+    (setq j (% (/ (aref md5-bits 0) 8) 64))
+
+    ;; Pad out computation to 56 bytes modulo 64
+    (setq padding (make-vector (if (< j 56) (- 56 j) (- 120 j)) 0))
+    (aset padding 0 128)
+    (md5-update padding)
+
+    ;; Append length in bits and transform
+    (let ((k 0) (kk 0))
+      (while (< k 14)
+        (aset in k (md5-pack md5-input kk))
+        (setq k (+ k 1) kk (+ kk 4))))
+    (md5-transform in)
+
+    ;; Store the results in the digest
+    (let ((k 0) (kk 0))
+      (while (< k 4)
+        (aset digest (+ kk 0) (md5-byte md5-buffer k 0))
+        (aset digest (+ kk 1) (md5-byte md5-buffer k 1))
+        (aset digest (+ kk 2) (md5-byte md5-buffer k 2))
+        (aset digest (+ kk 3) (md5-byte md5-buffer k 3))
+        (setq k (+ k 1) kk (+ kk 4))))
+
+    ;; Return digest
+    digest))
+
+;; It says in the RSA source, "Note that if the Mysterious Constants are
+;; arranged backwards in little-endian order and decrypted with the DES
+;; they produce OCCULT MESSAGES!"  Security through obscurity?
+
+(defun md5-transform (in)
+  "Basic MD5 step. Transform md5-buffer based on array IN."
+  (let ((a (aref md5-buffer 0))
+        (b (aref md5-buffer 1))
+        (c (aref md5-buffer 2))
+        (d (aref md5-buffer 3)))
+    (setq
+     a (md5-FF a b c d (aref in  0)  7 '(55146 . 42104))
+     d (md5-FF d a b c (aref in  1) 12 '(59591 . 46934))
+     c (md5-FF c d a b (aref in  2) 17 '( 9248 . 28891))
+     b (md5-FF b c d a (aref in  3) 22 '(49597 . 52974))
+     a (md5-FF a b c d (aref in  4)  7 '(62844 .  4015))
+     d (md5-FF d a b c (aref in  5) 12 '(18311 . 50730))
+     c (md5-FF c d a b (aref in  6) 17 '(43056 . 17939))
+     b (md5-FF b c d a (aref in  7) 22 '(64838 . 38145))
+     a (md5-FF a b c d (aref in  8)  7 '(27008 . 39128))
+     d (md5-FF d a b c (aref in  9) 12 '(35652 . 63407))
+     c (md5-FF c d a b (aref in 10) 17 '(65535 . 23473))
+     b (md5-FF b c d a (aref in 11) 22 '(35164 . 55230))
+     a (md5-FF a b c d (aref in 12)  7 '(27536 .  4386))
+     d (md5-FF d a b c (aref in 13) 12 '(64920 . 29075))
+     c (md5-FF c d a b (aref in 14) 17 '(42617 . 17294))
+     b (md5-FF b c d a (aref in 15) 22 '(18868 .  2081))
+     a (md5-GG a b c d (aref in  1)  5 '(63006 .  9570))
+     d (md5-GG d a b c (aref in  6)  9 '(49216 . 45888))
+     c (md5-GG c d a b (aref in 11) 14 '( 9822 . 23121))
+     b (md5-GG b c d a (aref in  0) 20 '(59830 . 51114))
+     a (md5-GG a b c d (aref in  5)  5 '(54831 .  4189))
+     d (md5-GG d a b c (aref in 10)  9 '(  580 .  5203))
+     c (md5-GG c d a b (aref in 15) 14 '(55457 . 59009))
+     b (md5-GG b c d a (aref in  4) 20 '(59347 . 64456))
+     a (md5-GG a b c d (aref in  9)  5 '( 8673 . 52710))
+     d (md5-GG d a b c (aref in 14)  9 '(49975 .  2006))
+     c (md5-GG c d a b (aref in  3) 14 '(62677 .  3463))
+     b (md5-GG b c d a (aref in  8) 20 '(17754 .  5357))
+     a (md5-GG a b c d (aref in 13)  5 '(43491 . 59653))
+     d (md5-GG d a b c (aref in  2)  9 '(64751 . 41976))
+     c (md5-GG c d a b (aref in  7) 14 '(26479 .   729))
+     b (md5-GG b c d a (aref in 12) 20 '(36138 . 19594))
+     a (md5-HH a b c d (aref in  5)  4 '(65530 . 14658))
+     d (md5-HH d a b c (aref in  8) 11 '(34673 . 63105))
+     c (md5-HH c d a b (aref in 11) 16 '(28061 . 24866))
+     b (md5-HH b c d a (aref in 14) 23 '(64997 . 14348))
+     a (md5-HH a b c d (aref in  1)  4 '(42174 . 59972))
+     d (md5-HH d a b c (aref in  4) 11 '(19422 . 53161))
+     c (md5-HH c d a b (aref in  7) 16 '(63163 . 19296))
+     b (md5-HH b c d a (aref in 10) 23 '(48831 . 48240))
+     a (md5-HH a b c d (aref in 13)  4 '(10395 . 32454))
+     d (md5-HH d a b c (aref in  0) 11 '(60065 . 10234))
+     c (md5-HH c d a b (aref in  3) 16 '(54511 . 12421))
+     b (md5-HH b c d a (aref in  6) 23 '( 1160 .  7429))
+     a (md5-HH a b c d (aref in  9)  4 '(55764 . 53305))
+     d (md5-HH d a b c (aref in 12) 11 '(59099 . 39397))
+     c (md5-HH c d a b (aref in 15) 16 '( 8098 . 31992))
+     b (md5-HH b c d a (aref in  2) 23 '(50348 . 22117))
+     a (md5-II a b c d (aref in  0)  6 '(62505 .  8772))
+     d (md5-II d a b c (aref in  7) 10 '(17194 . 65431))
+     c (md5-II c d a b (aref in 14) 15 '(43924 .  9127))
+     b (md5-II b c d a (aref in  5) 21 '(64659 . 41017))
+     a (md5-II a b c d (aref in 12)  6 '(25947 . 22979))
+     d (md5-II d a b c (aref in  3) 10 '(36620 . 52370))
+     c (md5-II c d a b (aref in 10) 15 '(65519 . 62589))
+     b (md5-II b c d a (aref in  1) 21 '(34180 . 24017))
+     a (md5-II a b c d (aref in  8)  6 '(28584 . 32335))
+     d (md5-II d a b c (aref in 15) 10 '(65068 . 59104))
+     c (md5-II c d a b (aref in  6) 15 '(41729 . 17172))
+     b (md5-II b c d a (aref in 13) 21 '(19976 .  4513))
+     a (md5-II a b c d (aref in  4)  6 '(63315 . 32386))
+     d (md5-II d a b c (aref in 11) 10 '(48442 . 62005))
+     c (md5-II c d a b (aref in  2) 15 '(10967 . 53947))
+     b (md5-II b c d a (aref in  9) 21 '(60294 . 54161)))
+
+     (aset md5-buffer 0 (md5-add (aref md5-buffer 0) a))
+     (aset md5-buffer 1 (md5-add (aref md5-buffer 1) b))
+     (aset md5-buffer 2 (md5-add (aref md5-buffer 2) c))
+     (aset md5-buffer 3 (md5-add (aref md5-buffer 3) d))))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;; Here begins the merger with the XEmacs API and the md5.el from the URL
+;;; package.  Courtesy wmperry@cs.indiana.edu
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+(defun md5 (object &optional start end)
+  "Return the MD5 (a secure message digest algorithm) of an object.
+OBJECT is either a string or a buffer.
+Optional arguments START and END denote buffer positions for computing the
+hash of a portion of OBJECT."
+ (let ((buffer nil))
+    (unwind-protect
+	(save-excursion
+	  (setq buffer (generate-new-buffer " *md5-work*"))
+	  (set-buffer buffer)
+	  (cond
+	   ((bufferp object)
+	    (insert-buffer-substring object start end))
+	   ((stringp object)
+	    (insert (if (or start end)
+			(substring object start end)
+		      object)))
+	   (t nil))
+	  (prog1
+	      (if (or (null md5-maximum-internal-length)
+		      (<= (point-max) md5-maximum-internal-length))
+		  (mapconcat
+		   (function (lambda (node) (format "%02x" node)))
+		   (md5-encode (buffer-string))
+		   "")
+		(call-process-region (point-min) (point-max)
+				     shell-file-name
+				     t buffer nil
+				     shell-command-switch md5-program)
+		;; MD5 digest is 32 chars long
+		;; mddriver adds a newline to make neaten output for tty
+		;; viewing, make sure we leave it behind.
+		(buffer-substring (point-min) (+ (point-min) 32)))
+	    (kill-buffer buffer)))
+      (and buffer (buffer-name buffer) (kill-buffer buffer) nil))))
+
+(provide 'md5-el)
+;;; md5.el --- MD5 Message Digest Algorithm.
+
+;; Copyright (C) 1999, 2001  Free Software Foundation, Inc.
+
+;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
+;; Keywords: MD5, RFC 1321
+
+;; This file is part of FLIM (Faithful Library about Internet Message).
+
+;; This program is free software; you can redistribute it and/or
+;; modify it under the terms of the GNU General Public License as
+;; published by the Free Software Foundation; either version 2, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program; see the file COPYING.  If not, write to
+;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
+
+;;; Commentary:
+
+;; Test cases from RFC 1321.
+;;
+;; (md5 "")
+;; => d41d8cd98f00b204e9800998ecf8427e
+;;
+;; (md5 "a")
+;; => 0cc175b9c0f1b6a831c399e269772661
+;;
+;; (md5 "abc")
+;; => 900150983cd24fb0d6963f7d28e17f72
+;;
+;; (md5 "message digest")
+;; => f96b697d7cb7938d525a2f31aaf161d0
+;;
+;; (md5 "abcdefghijklmnopqrstuvwxyz")
+;; => c3fcd3d76192e4007dfb496cca67e13b
+;;
+;; (md5 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
+;; => d174ab98d277d9f5a5611c2c9f419d9f
+;;
+;; (md5 "12345678901234567890123456789012345678901234567890123456789012345678901234567890")
+;; => 57edf4a22be3c955ac49da2e2107b67a
+
+;;; Code:
+
+(defvar md5-dl-module
+  (cond
+   ((and (fboundp 'md5)
+	 (subrp (symbol-function 'md5)))
+    nil)
+   ((fboundp 'dynamic-link)
+    ;; Should we take care of `dynamic-link-path'?
+    (let ((path (expand-file-name "md5.so" exec-directory)))
+      (if (file-exists-p path)
+	  path
+	nil)))
+   (t
+    nil)))
+
+(cond
+ ((and (fboundp 'md5)
+       (subrp (symbol-function 'md5)))
+  ;; do nothing.
+  )
+ ((and (stringp md5-dl-module)
+       (file-exists-p md5-dl-module))
+  (require 'md5-dl))
+ (t
+  (require 'md5-el)))
+
+(provide 'md5)
+
+;;; md5.el ends here
+;;; ntlm.el --- NTLM (NT LanManager) authentication support
+
+;; Copyright (C) 2001 Taro Kawagishi
+;; Author: Taro Kawagishi <tarok@transpulse.org>
+;; Keywords: NTLM, SASL
+;; Version: 1.00
+;; Created: February 2001
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 2, or (at your option)
+;; any later version.
+;;
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+;;
+;; You should have received a copy of the GNU General Public License
+;; along with this program; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
+
+;;; Commentary:
+
+;; This library is a direct translation of the Samba release 2.2.0
+;; implementation of Windows NT and LanManager compatible password
+;; encryption.
+;; 
+;; Interface functions:
+;; 
+;; ntlm-build-auth-request
+;;   This will return a binary string, which should be used in the
+;;   base64 encoded form and it is the caller's responsibility to encode
+;;   the returned string with base64.
+;;
+;; ntlm-build-auth-response
+;;   It is the caller's responsibility to pass a base64 decoded string
+;;   (which will be a binary string) as the first argument and to
+;;   encode the returned string with base64.  The second argument user
+;;   should be given in user@domain format.
+;; 
+;; ntlm-get-password-hashes
+;;
+;;
+;; NTLM authentication procedure example:
+;;
+;;  1. Open a network connection to the Exchange server at the IMAP port (143)
+;;  2. Receive an opening message such as:
+;;     "* OK Microsoft Exchange IMAP4rev1 server version 5.5.2653.7 (XXXX) ready"
+;;  3. Ask for IMAP server capability by sending "NNN capability"
+;;  4. Receive a capability message such as:
+;;     "* CAPABILITY IMAP4 IMAP4rev1 IDLE LITERAL+ LOGIN-REFERRALS MAILBOX-REFERRALS NAMESPACE AUTH=NTLM"
+;;  5. Ask for NTLM authentication by sending a string
+;;     "NNN authenticate ntlm"
+;;  6. Receive continuation acknowledgment "+"
+;;  7. Send NTLM authentication request generated by 'ntlm-build-auth-request
+;;  8. Receive NTLM challenge string following acknowledgment "+"
+;;  9. Generate response to challenge by 'ntlm-build-auth-response
+;;     (here two hash function values of the user password are encrypted)
+;; 10. Receive authentication completion message such as
+;;     "NNN OK AUTHENTICATE NTLM completed."
+
+;;; Code:
+
+;;; XEmacs (require 'poem)
+(require 'md4)
+
+;;;
+;;; NTLM authentication interface functions
+
+(defun ntlm-build-auth-request (user &optional domain)
+  "Return the NTLM authentication request string for USER and DOMAIN.
+USER is a string representing a user name to be authenticated and
+DOMAIN is a NT domain.  USER can include a NT domain part as in
+user@domain where the string after @ is used as the domain if DOMAIN
+is not given."
+  (interactive)
+  (let ((request-ident (concat "NTLMSSP" (make-string 1 0)))
+	(request-msgType (concat (make-string 1 1) (make-string 3 0)))
+					;0x01 0x00 0x00 0x00
+	(request-flags (concat (make-string 1 7) (make-string 1 178)
+			       (make-string 2 0)))
+					;0x07 0xb2 0x00 0x00
+	lu ld off-d off-u)
+    (when (string-match "@" user)
+      (unless domain
+	(setq domain (substring user (1+ (match-beginning 0)))))
+      (setq user (substring user 0 (match-beginning 0))))
+    ;; set fields offsets within the request struct
+    (setq lu (length user))
+    (setq ld (length domain))
+    (setq off-u 32)			;offset to the string 'user
+    (setq off-d (+ 32 lu))		;offset to the string 'domain
+    ;; pack the request struct in a string
+    (concat request-ident		;8 bytes
+	    request-msgType	;4 bytes
+	    request-flags		;4 bytes
+	    (md4-pack-int16 lu)	;user field, count field
+	    (md4-pack-int16 lu)	;user field, max count field
+	    (md4-pack-int32 (cons 0 off-u)) ;user field, offset field
+	    (md4-pack-int16 ld)	;domain field, count field
+	    (md4-pack-int16 ld)	;domain field, max count field
+	    (md4-pack-int32 (cons 0 off-d)) ;domain field, offset field
+	    user			;bufer field
+	    domain		;bufer field
+	    )))
+
+(defun ntlm-build-auth-response (challenge user password-hashes)
+  "Return the response string to a challenge string CHALLENGE given by
+the NTLM based server for the user USER and the password hash list
+PASSWORD-HASHES.  NTLM uses two hash values which are represented
+by PASSWORD-HASHES.  PASSWORD-HASHES should be a return value of
+ (list (smb-passwd-hash password) (ntlm-md4hash password))"
+  (let* ((rchallenge (string-as-unibyte challenge))
+	 ;; get fields within challenge struct
+	 ;;(ident (substring rchallenge 0 8))	;ident, 8 bytes
+	 ;;(msgType (substring rchallenge 8 12))	;msgType, 4 bytes
+	 (uDomain (substring rchallenge 12 20))	;uDomain, 8 bytes
+	 (flags (substring rchallenge 20 24))	;flags, 4 bytes
+	 (challengeData (substring rchallenge 24 32)) ;challengeData, 8 bytes
+	 uDomain-len uDomain-offs
+	 ;; response struct and its fields
+	 lmRespData			;lmRespData, 24 bytes
+	 ntRespData			;ntRespData, 24 bytes
+	 domain				;ascii domain string
+	 lu ld off-lm off-nt off-d off-u off-w off-s)
+    ;; extract domain string from challenge string
+    (setq uDomain-len (md4-unpack-int16 (substring uDomain 0 2)))
+    (setq uDomain-offs (md4-unpack-int32 (substring uDomain 4 8)))
+    (setq domain
+	  (ntlm-unicode2ascii (substring challenge
+					 (cdr uDomain-offs)
+					 (+ (cdr uDomain-offs) uDomain-len))
+			      (/ uDomain-len 2)))
+    ;; overwrite domain in case user is given in <user>@<domain> format
+    (when (string-match "@" user)
+      (setq domain (substring user (1+ (match-beginning 0))))
+      (setq user (substring user 0 (match-beginning 0))))
+
+    ;; generate response data
+    (setq lmRespData
+	  (smb-owf-encrypt (car password-hashes) challengeData))
+    (setq ntRespData
+	  (smb-owf-encrypt (cadr password-hashes) challengeData))
+
+    ;; get offsets to fields to pack the response struct in a string
+    (setq lu (length user))
+    (setq ld (length domain))
+    (setq off-lm 64)			;offset to string 'lmResponse
+    (setq off-nt (+ 64 24))		;offset to string 'ntResponse
+    (setq off-d (+ 64 48))		;offset to string 'uDomain
+    (setq off-u (+ 64 48 (* 2 ld)))	;offset to string 'uUser
+    (setq off-w (+ 64 48 (* 2 (+ ld lu)))) ;offset to string 'uWks
+    (setq off-s (+ 64 48 (* 2 (+ ld lu lu)))) ;offset to string 'sessionKey
+    ;; pack the response struct in a string
+    (concat "NTLMSSP\0"			;response ident field, 8 bytes
+	    (md4-pack-int32 '(0 . 3))	;response msgType field, 4 bytes
+
+	    ;; lmResponse field, 8 bytes
+	    ;;AddBytes(response,lmResponse,lmRespData,24);
+	    (md4-pack-int16 24)		;len field
+	    (md4-pack-int16 24)		;maxlen field
+	    (md4-pack-int32 (cons 0 off-lm)) ;field offset
+
+	    ;; ntResponse field, 8 bytes
+	    ;;AddBytes(response,ntResponse,ntRespData,24);
+	    (md4-pack-int16 24)		;len field
+	    (md4-pack-int16 24)		;maxlen field
+	    (md4-pack-int32 (cons 0 off-nt)) ;field offset
+
+	    ;; uDomain field, 8 bytes
+	    ;;AddUnicodeString(response,uDomain,domain);
+	    ;;AddBytes(response, uDomain, udomain, 2*ld);
+	    (md4-pack-int16 (* 2 ld))	;len field
+	    (md4-pack-int16 (* 2 ld))	;maxlen field
+	    (md4-pack-int32 (cons 0 off-d)) ;field offset
+
+	    ;; uUser field, 8 bytes
+	    ;;AddUnicodeString(response,uUser,u);
+	    ;;AddBytes(response, uUser, uuser, 2*lu);
+	    (md4-pack-int16 (* 2 lu))	;len field
+	    (md4-pack-int16 (* 2 lu))	;maxlen field
+	    (md4-pack-int32 (cons 0 off-u)) ;field offset
+
+	    ;; uWks field, 8 bytes
+	    ;;AddUnicodeString(response,uWks,u);
+	    (md4-pack-int16 (* 2 lu))	;len field
+	    (md4-pack-int16 (* 2 lu))	;maxlen field
+	    (md4-pack-int32 (cons 0 off-w)) ;field offset
+
+	    ;; sessionKey field, 8 bytes
+	    ;;AddString(response,sessionKey,NULL);
+	    (md4-pack-int16 0)		;len field
+	    (md4-pack-int16 0)		;maxlen field
+	    (md4-pack-int32 (cons 0 (- off-s off-lm))) ;field offset
+
+	    ;; flags field, 4 bytes
+	    flags			;
+
+	    ;; buffer field
+	    lmRespData			;lmResponse, 24 bytes
+	    ntRespData			;ntResponse, 24 bytes
+	    (ntlm-ascii2unicode domain	;unicode domain string, 2*ld bytes
+				(length domain)) ;
+	    (ntlm-ascii2unicode user	;unicode user string, 2*lu bytes
+				(length user)) ;
+	    (ntlm-ascii2unicode user	;unicode user string, 2*lu bytes
+				(length user)) ;
+	    )))
+
+(defun ntlm-get-password-hashes (password)
+  "Return a pair of SMB hash and NT MD4 hash of the given password PASSWORD"
+  (list (smb-passwd-hash password)
+	(ntlm-md4hash password)))
+
+(defun ntlm-ascii2unicode (str len)
+  "Convert an ASCII string into a NT Unicode string, which is
+little-endian utf16."
+  (let ((utf (make-string (* 2 len) 0)) (i 0) val)
+    (while (and (< i len)
+		(not (zerop (setq val (aref str i)))))
+      (aset utf (* 2 i) val)
+      (aset utf (1+ (* 2 i)) 0)
+      (setq i (1+ i)))
+    utf))
+
+(defun ntlm-unicode2ascii (str len)
+  "Extract 7 bits ASCII part of a little endian utf16 string STR of length LEN."
+  (let ((buf (make-string len 0)) (i 0) (j 0))
+    (while (< i len)
+      (aset buf i (logand (aref str j) 127)) ;(string-to-number "7f" 16)
+      (setq i (1+ i)
+	    j (+ 2 j)))
+    buf))
+
+(defun smb-passwd-hash (passwd)
+  "Return the SMB password hash string of 16 bytes long for the given password
+string PASSWD.  PASSWD is truncated to 14 bytes if longer."
+  (let ((len (min (length passwd) 14)))
+    (smbdes-e-p16
+     (concat (substring (upcase passwd) 0 len) ;fill top 14 bytes with passwd
+	     (make-string (- 15 len) 0)))))
+
+(defun smb-owf-encrypt (passwd c8)
+  "Return the response string of 24 bytes long for the given password
+string PASSWD based on the DES encryption.  PASSWD is of at most 14
+bytes long and the challenge string C8 of 8 bytes long."
+  (let ((len (min (length passwd) 16)) p22)
+    (setq p22 (concat (substring passwd 0 len) ;fill top 16 bytes with passwd
+		      (make-string (- 22 len) 0)))
+    (smbdes-e-p24 p22 c8)))
+
+(defun smbdes-e-p24 (p22 c8)
+  "Return a 24 bytes hashed string for a 21 bytes string P22 and a 8 bytes
+string C8."
+  (concat (smbhash c8 p22 t)		;hash first 8 bytes of p22
+	  (smbhash c8 (substring p22 7) t)
+	  (smbhash c8 (substring p22 14) t)))
+
+(defconst smb-sp8 [75 71 83 33 64 35 36 37])
+
+(defun smbdes-e-p16 (p15)
+  "Return a 16 bytes hashed string for a 15 bytes string P15."
+  (concat (smbhash smb-sp8 p15 t)	;hash of first 8 bytes of p15
+	  (smbhash smb-sp8 (substring p15 7) t)	;hash of last 8 bytes of p15
+	  ))
+
+(defun smbhash (in key forw)
+  "Return the hash string of length 8 for a string IN of length 8 and
+a string KEY of length 8.  FORW is t or nil."
+  (let ((out (make-string 8 0))
+	outb				;string of length 64
+	(inb (make-string 64 0))
+	(keyb (make-string 64 0))
+	(key2 (smb-str-to-key key))
+	(i 0) aa)
+    (while (< i 64)
+      (unless (zerop (logand (aref in (/ i 8)) (lsh 1 (- 7 (% i 8)))))
+	(aset inb i 1))
+      (unless (zerop (logand (aref key2 (/ i 8)) (lsh 1 (- 7 (% i 8)))))
+	(aset keyb i 1))
+      (setq i (1+ i)))
+    (setq outb (smb-dohash inb keyb forw))
+    (setq i 0)
+    (while (< i 64)
+      (unless (zerop (aref outb i))
+	(setq aa (aref out (/ i 8)))
+	(aset out (/ i 8)
+	      (logior aa (lsh 1 (- 7 (% i 8))))))
+      (setq i (1+ i)))
+    out))
+
+(defun smb-str-to-key (str)
+  "Return a string of length 8 for the given string STR of length 7."
+  (let ((key (make-string 8 0))
+	(i 7))
+    (aset key 0 (lsh (aref str 0) -1))
+    (aset key 1 (logior
+		 (lsh (logand (aref str 0) 1) 6)
+		 (lsh (aref str 1) -2)))
+    (aset key 2 (logior
+		 (lsh (logand (aref str 1) 3) 5)
+		 (lsh (aref str 2) -3)))
+    (aset key 3 (logior
+		 (lsh (logand (aref str 2) 7) 4)
+		 (lsh (aref str 3) -4)))
+    (aset key 4 (logior
+		 (lsh (logand (aref str 3) 15) 3)
+		 (lsh (aref str 4) -5)))
+    (aset key 5 (logior
+		 (lsh (logand (aref str 4) 31) 2)
+		 (lsh (aref str 5) -6)))
+    (aset key 6 (logior
+		 (lsh (logand (aref str 5) 63) 1)
+		 (lsh (aref str 6) -7)))
+    (aset key 7 (logand (aref str 6) 127))
+    (while (>= i 0)
+      (aset key i (lsh (aref key i) 1))
+      (setq i (1- i)))
+    key))
+
+(defconst smb-perm1 [57 49 41 33 25 17  9
+		     1 58 50 42 34 26 18
+		     10  2 59 51 43 35 27
+		     19 11  3 60 52 44 36
+		     63 55 47 39 31 23 15
+		     7 62 54 46 38 30 22
+		     14  6 61 53 45 37 29
+		     21 13  5 28 20 12  4])
+
+(defconst smb-perm2 [14 17 11 24  1  5
+		     3 28 15  6 21 10
+		     23 19 12  4 26  8
+		     16  7 27 20 13  2
+		     41 52 31 37 47 55
+		     30 40 51 45 33 48
+		     44 49 39 56 34 53
+		     46 42 50 36 29 32])
+
+(defconst smb-perm3 [58 50 42 34 26 18 10  2
+		     60 52 44 36 28 20 12  4
+		     62 54 46 38 30 22 14  6
+		     64 56 48 40 32 24 16  8
+		     57 49 41 33 25 17  9  1
+		     59 51 43 35 27 19 11  3
+		     61 53 45 37 29 21 13  5
+		     63 55 47 39 31 23 15  7])
+
+(defconst smb-perm4 [32  1  2  3  4  5
+		     4  5  6  7  8  9
+		     8  9 10 11 12 13
+		     12 13 14 15 16 17
+		     16 17 18 19 20 21
+		     20 21 22 23 24 25
+		     24 25 26 27 28 29
+		     28 29 30 31 32  1])
+
+(defconst smb-perm5 [16  7 20 21
+		     29 12 28 17
+		     1 15 23 26
+		     5 18 31 10
+		     2  8 24 14
+		     32 27  3  9
+		     19 13 30  6
+		     22 11  4 25])
+
+(defconst smb-perm6 [40  8 48 16 56 24 64 32
+		     39  7 47 15 55 23 63 31
+		     38  6 46 14 54 22 62 30
+		     37  5 45 13 53 21 61 29
+		     36  4 44 12 52 20 60 28
+		     35  3 43 11 51 19 59 27
+		     34  2 42 10 50 18 58 26
+		     33  1 41  9 49 17 57 25])
+
+(defconst smb-sc [1 1 2 2 2 2 2 2 1 2 2 2 2 2 2 1])
+
+(defconst smb-sbox [[[14  4 13  1  2 15 11  8  3 10  6 12  5  9  0  7]
+		     [ 0 15  7  4 14  2 13  1 10  6 12 11  9  5  3  8]
+		     [ 4  1 14  8 13  6  2 11 15 12  9  7  3 10  5  0]
+		     [15 12  8  2  4  9  1  7  5 11  3 14 10  0  6 13]]
+		    [[15  1  8 14  6 11  3  4  9  7  2 13 12  0  5 10]
+		     [ 3 13  4  7 15  2  8 14 12  0  1 10  6  9 11  5]
+		     [ 0 14  7 11 10  4 13  1  5  8 12  6  9  3  2 15]
+		     [13  8 10  1  3 15  4  2 11  6  7 12  0  5 14  9]]
+		    [[10  0  9 14  6  3 15  5  1 13 12  7 11  4  2  8]
+		     [13  7  0  9  3  4  6 10  2  8  5 14 12 11 15  1]
+		     [13  6  4  9  8 15  3  0 11  1  2 12  5 10 14  7]
+		     [ 1 10 13  0  6  9  8  7  4 15 14  3 11  5  2 12]]
+		    [[ 7 13 14  3  0  6  9 10  1  2  8  5 11 12  4 15]
+		     [13  8 11  5  6 15  0  3  4  7  2 12  1 10 14  9]
+		     [10  6  9  0 12 11  7 13 15  1  3 14  5  2  8  4]
+		     [ 3 15  0  6 10  1 13  8  9  4  5 11 12  7  2 14]]
+		    [[ 2 12  4  1  7 10 11  6  8  5  3 15 13  0 14  9]
+		     [14 11  2 12  4  7 13  1  5  0 15 10  3  9  8  6]
+		     [ 4  2  1 11 10 13  7  8 15  9 12  5  6  3  0 14]
+		     [11  8 12  7  1 14  2 13  6 15  0  9 10  4  5  3]]
+		    [[12  1 10 15  9  2  6  8  0 13  3  4 14  7  5 11]
+		     [10 15  4  2  7 12  9  5  6  1 13 14  0 11  3  8]
+		     [ 9 14 15  5  2  8 12  3  7  0  4 10  1 13 11  6]
+		     [ 4  3  2 12  9  5 15 10 11 14  1  7  6  0  8 13]]
+		    [[ 4 11  2 14 15  0  8 13  3 12  9  7  5 10  6  1]
+		     [13  0 11  7  4  9  1 10 14  3  5 12  2 15  8  6]
+		     [ 1  4 11 13 12  3  7 14 10 15  6  8  0  5  9  2]
+		     [ 6 11 13  8  1  4 10  7  9  5  0 15 14  2  3 12]]
+		    [[13  2  8  4  6 15 11  1 10  9  3 14  5  0 12  7]
+		     [ 1 15 13  8 10  3  7  4 12  5  6 11  0 14  9  2]
+		     [ 7 11  4  1  9 12 14  2  0  6 10 13 15  3  5  8]
+		     [ 2  1 14  7  4 10  8 13 15 12  9  0  3  5  6 11]]])
+
+(defsubst string-permute (in perm n)
+  "Return a string of length N for a string IN and a permutation vector
+PERM of size N.  The length of IN should be height of PERM."
+  (let ((i 0) (out (make-string n 0)))
+    (while (< i n)
+      (aset out i (aref in (- (aref perm i) 1)))
+      (setq i (1+ i)))
+    out))
+
+(defsubst string-lshift (str count len)
+  "Return a string by circularly shifting a string STR by COUNT to the left.
+length of STR is LEN."
+  (let ((c (% count len)))
+    (concat (substring str c len) (substring str 0 c))))
+
+(defsubst string-xor (in1 in2 n)
+  "Return exclusive-or of sequences in1 and in2"
+  (let ((w (make-string n 0)) (i 0))
+    (while (< i n)
+      (aset w i (logxor (aref in1 i) (aref in2 i)))
+      (setq i (1+ i)))
+    w))
+
+(defun smb-dohash (in key forw)
+  "Return the hash value for a string IN and a string KEY.
+Length of IN and KEY are 64.  FORW non nill means forward, nil means
+backward."
+  (let (pk1				;string of length 56
+	c				;string of length 28
+	d				;string of length 28
+	cd				;string of length 56
+	(ki (make-vector 16 0))		;vector of string of length 48
+	pd1				;string of length 64
+	l				;string of length 32
+	r				;string of length 32
+	rl				;string of length 64
+	(i 0) (j 0) (k 0))
+    (setq pk1 (string-permute key smb-perm1 56))
+    (setq c (substring pk1 0 28))
+    (setq d (substring pk1 28 56))
+
+    (setq i 0)
+    (while (< i 16)
+      (setq c (string-lshift c (aref smb-sc i) 28))
+      (setq d (string-lshift d (aref smb-sc i) 28))
+      (setq cd (concat (substring c 0 28) (substring d 0 28)))
+      (aset ki i (string-permute cd smb-perm2 48))
+      (setq i (1+ i)))
+
+    (setq pd1 (string-permute in smb-perm3 64))
+
+    (setq l (substring pd1 0 32))
+    (setq r (substring pd1 32 64))
+
+    (setq i 0)
+    (let (er				;string of length 48
+	  erk				;string of length 48
+	  (b (make-vector 8 0))		;vector of strings of length 6
+	  cb				;string of length 32
+	  pcb				;string of length 32
+	  r2				;string of length 32
+	  jj m n bj sbox-jmn)
+      (while (< i 16)
+	(setq er (string-permute r smb-perm4 48))
+	(setq erk (string-xor er
+		       (aref ki (if forw i (- 15 i)))
+		       48))
+	(setq j 0)
+	(while (< j 8)
+	  (setq jj (* 6 j))
+	  (aset b j (substring erk jj (+ jj 6)))
+	  (setq j (1+ j)))
+	(setq j 0)
+	(while (< j 8)
+	  (setq bj (aref b j))
+	  (setq m (logior (lsh (aref bj 0) 1) (aref bj 5)))
+	  (setq n (logior (lsh (aref bj 1) 3)
+			  (lsh (aref bj 2) 2)
+			  (lsh (aref bj 3) 1)
+			  (aref bj 4)))
+	  (setq k 0)
+	  (setq sbox-jmn (aref (aref (aref smb-sbox j) m) n))
+	  (while (< k 4)
+	    (aset bj k
+		  (if (zerop (logand sbox-jmn (lsh 1 (- 3 k))))
+		      0 1))
+	    (setq k (1+ k)))
+	  (setq j (1+ j)))
+
+	(setq j 0)
+	(setq cb nil)
+	(while (< j 8)
+	  (setq cb (concat cb (substring (aref b j) 0 4)))
+	  (setq j (1+ j)))
+
+	(setq pcb (string-permute cb smb-perm5 32))
+	(setq r2 (string-xor l pcb 32))
+	(setq l r)
+	(setq r r2)
+	(setq i (1+ i))))
+    (setq rl (concat r l))
+    (string-permute rl smb-perm6 64)))
+
+(defun ntlm-md4hash (passwd)
+  "Return the 16 bytes MD4 hash of a string PASSWD after converting it
+into a Unicode string.  PASSWD is truncated to 128 bytes if longer."
+  (let (len wpwd)
+    ;; Password cannot be longer than 128 characters
+    (setq len (length passwd))
+    (if (> len 128)
+	(setq len 128))
+    ;; Password must be converted to NT unicode
+    (setq wpwd (ntlm-ascii2unicode passwd len))
+    ;; Calculate length in bytes
+    (setq len (* len 2))
+    (md4 wpwd len)))
+
+(provide 'ntlm)
+
+;;; ntlm.el ends here
+;;; sasl-cram.el --- CRAM-MD5 module for the SASL client framework
+
+;; Copyright (C) 2000 Free Software Foundation, Inc.
+
+;; Author: Daiki Ueno <ueno@unixuser.org>
+;;	Kenichi OKADA <okada@opaopa.org>
+;; Keywords: SASL, CRAM-MD5
+
+;; This file is part of FLIM (Faithful Library about Internet Message).
+
+;; This program is free software; you can redistribute it and/or
+;; modify it under the terms of the GNU General Public License as
+;; published by the Free Software Foundation; either version 2, or (at
+;; your option) any later version.
+
+;; This program is distributed in the hope that it will be useful, but
+;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;; General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
+
+;;; Commentary:
+
+(require 'sasl)
+(require 'hmac-md5)
+
+(defconst sasl-cram-md5-steps
+  '(ignore				;no initial response
+    sasl-cram-md5-response))
+
+(defun sasl-cram-md5-response (client step)
+  (let ((passphrase
+	 (sasl-read-passphrase
+	  (format "CRAM-MD5 passphrase for %s: "
+		  (sasl-client-name client)))))
+    (unwind-protect
+	(concat (sasl-client-name client) " "
+		(encode-hex-string
+		 (hmac-md5 (sasl-step-data step) passphrase)))
+      (fillarray passphrase 0))))
+
+(put 'sasl-cram 'sasl-mechanism
+     (sasl-make-mechanism "CRAM-MD5" sasl-cram-md5-steps))
+
+(provide 'sasl-cram)
+
+;;; sasl-cram.el ends here
+;;; sasl-digest.el --- DIGEST-MD5 module for the SASL client framework
+
+;; Copyright (C) 2000 Free Software Foundation, Inc.
+
+;; Author: Daiki Ueno <ueno@unixuser.org>
+;;	Kenichi OKADA <okada@opaopa.org>
+;; Keywords: SASL, DIGEST-MD5
+
+;; This file is part of FLIM (Faithful Library about Internet Message).
+
+;; This program is free software; you can redistribute it and/or
+;; modify it under the terms of the GNU General Public License as
+;; published by the Free Software Foundation; either version 2, or (at
+;; your option) any later version.
+
+;; This program is distributed in the hope that it will be useful, but
+;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;; General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
+
+;; This program is implemented from draft-leach-digest-sasl-05.txt.
+;;
+;; It is caller's responsibility to base64-decode challenges and
+;; base64-encode responses in IMAP4 AUTHENTICATE command.
+;;
+;; Passphrase should be longer than 16 bytes. (See RFC 2195)
+
+;;; Commentary:
+
+(require 'sasl)
+(require 'hmac-md5)
+
+(defvar sasl-digest-md5-nonce-count 1)
+(defvar sasl-digest-md5-unique-id-function
+  sasl-unique-id-function)
+
+(defvar sasl-digest-md5-syntax-table
+  (let ((table (make-syntax-table)))
+    (modify-syntax-entry ?= "." table)
+    (modify-syntax-entry ?, "." table)
+    table)
+  "A syntax table for parsing digest-challenge attributes.")
+
+(defconst sasl-digest-md5-steps
+  '(ignore				;no initial response
+    sasl-digest-md5-response
+    ignore))				;""
+
+(defun sasl-digest-md5-parse-string (string)
+  "Parse STRING and return a property list.
+The value is a cons cell of the form \(realm nonce qop-options stale maxbuf
+charset algorithm cipher-opts auth-param)."
+  (with-temp-buffer
+    (set-syntax-table sasl-digest-md5-syntax-table)
+    (save-excursion
+      (insert string)
+      (goto-char (point-min))
+      (insert "(")
+      (while (progn (forward-sexp) (not (eobp)))
+	(delete-char 1)
+	(insert " "))
+      (insert ")")
+      (read (point-min-marker)))))
+
+(defun sasl-digest-md5-digest-uri (serv-type host &optional serv-name)
+  (concat serv-type "/" host
+	  (if (and serv-name
+		   (not (string= host serv-name)))
+	      (concat "/" serv-name))))
+
+(defun sasl-digest-md5-cnonce ()
+  (let ((sasl-unique-id-function sasl-digest-md5-unique-id-function))
+    (sasl-unique-id)))
+
+(defun sasl-digest-md5-response-value (username
+				       realm
+				       nonce
+				       cnonce
+				       nonce-count
+				       qop
+				       digest-uri
+				       authzid)
+  (let ((passphrase
+	 (sasl-read-passphrase
+	  (format "DIGEST-MD5 passphrase for %s: "
+		  username))))
+    (unwind-protect
+	(encode-hex-string
+	 (md5-binary
+	  (concat
+	   (encode-hex-string
+	    (md5-binary (concat (md5-binary 
+				 (concat username ":" realm ":" passphrase))
+				":" nonce ":" cnonce
+				(if authzid 
+				    (concat ":" authzid)))))
+	   ":" nonce
+	   ":" (format "%08x" nonce-count) ":" cnonce ":" qop ":"
+	   (encode-hex-string
+	    (md5-binary
+	     (concat "AUTHENTICATE:" digest-uri
+		     (if (member qop '("auth-int" "auth-conf"))
+			 ":00000000000000000000000000000000")))))))
+      (fillarray passphrase 0))))
+
+(defun sasl-digest-md5-response (client step)
+  (let* ((plist
+	  (sasl-digest-md5-parse-string (sasl-step-data step)))
+	 (realm
+	  (or (sasl-client-property client 'realm)
+	      (plist-get plist 'realm))) ;need to check
+	 (nonce-count
+	  (or (sasl-client-property client 'nonce-count)
+	       sasl-digest-md5-nonce-count))
+	 (qop
+	  (or (sasl-client-property client 'qop)
+	      "auth"))
+	 (digest-uri
+	  (sasl-digest-md5-digest-uri
+	   (sasl-client-service client)(sasl-client-server client)))
+	 (cnonce
+	  (or (sasl-client-property client 'cnonce)
+	      (sasl-digest-md5-cnonce))))
+    (sasl-client-set-property client 'nonce-count (1+ nonce-count))
+    (unless (string= qop "auth")
+      (sasl-error (format "Unsupported \"qop-value\": %s" qop)))
+    (concat
+     "username=\"" (sasl-client-name client) "\","
+     "realm=\"" realm "\","
+     "nonce=\"" (plist-get plist 'nonce) "\","
+     "cnonce=\"" cnonce "\","
+     (format "nc=%08x," nonce-count)
+     "digest-uri=\"" digest-uri "\","
+     "qop=" qop ","
+     "response="
+     (sasl-digest-md5-response-value
+      (sasl-client-name client)
+      realm
+      (plist-get plist 'nonce)
+      cnonce
+      nonce-count
+      qop
+      digest-uri
+      (plist-get plist 'authzid)))))
+
+(put 'sasl-digest 'sasl-mechanism
+     (sasl-make-mechanism "DIGEST-MD5" sasl-digest-md5-steps))
+
+(provide 'sasl-digest)
+
+;;; sasl-digest.el ends here
+;;; sasl-ntlm.el --- NTLM (NT Lan Manager) module for the SASL client framework
+
+;; Copyright (C) 2000 Free Software Foundation, Inc.
+
+;; Author: Taro Kawagishi <tarok@transpulse.org>
+;; Keywords: SASL, NTLM
+;; Version: 1.00
+;; Created: February 2001
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 2, or (at your option)
+;; any later version.
+;;
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+;;
+;; You should have received a copy of the GNU General Public License
+;; along with this program; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
+
+;;; Commentary:
+
+;; This is a SASL interface layer for NTLM authentication message
+;; generation by ntlm.el
+
+;;; Code:
+
+(require 'sasl)
+(require 'ntlm)
+
+(defconst sasl-ntlm-steps
+  '(ignore				;nothing to do before making
+    sasl-ntlm-request			;authentication request
+    sasl-ntlm-response)			;response to challenge
+  "A list of functions to be called in sequnece for the NTLM
+authentication steps.  Ther are called by 'sasl-next-step.")
+
+(defun sasl-ntlm-request (client step)
+  "SASL step function to generate a NTLM authentication request to the server.
+Called from 'sasl-next-step.
+CLIENT is a vector [mechanism user service server sasl-client-properties]
+STEP is a vector [<previous step function> <result of previous step function>]"
+  (let ((user (sasl-client-name client)))
+    (ntlm-build-auth-request user)))
+
+(defun sasl-ntlm-response (client step)
+  "SASL step function to generate a NTLM response against the server
+challenge stored in the 2nd element of STEP.  Called from 'sasl-next-step."
+  (let* ((user (sasl-client-name client))
+	 (passphrase
+	  (sasl-read-passphrase (format "NTLM passphrase for %s: " user)))
+	 (challenge (sasl-step-data step)))
+    (ntlm-build-auth-response challenge user
+			      (ntlm-get-password-hashes passphrase))))
+
+(put 'sasl-ntlm 'sasl-mechanism
+     (sasl-make-mechanism "NTLM" sasl-ntlm-steps))
+
+(provide 'sasl-ntlm)
+
+;;; sasl-ntlm.el ends here
+;;; sasl-scram.el --- Compute SCRAM-MD5.
+
+;; Copyright (C) 1999 Shuhei KOBAYASHI
+
+;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
+;;	Kenichi OKADA <okada@opaopa.org>
+;; Keywords: SCRAM-MD5, HMAC-MD5, SASL, IMAP, POP, ACAP
+
+;; This file is part of FLIM (Faithful Library about Internet Message).
+
+;; This program is free software; you can redistribute it and/or
+;; modify it under the terms of the GNU General Public License as
+;; published by the Free Software Foundation; either version 2, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program; see the file COPYING.  If not, write to
+;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
+
+;;; Commentary:
+
+;; This program is implemented from draft-newman-auth-scram-03.txt.
+;;
+;; It is caller's responsibility to base64-decode challenges and
+;; base64-encode responses in IMAP4 AUTHENTICATE command.
+;;
+;; Passphrase should be longer than 16 bytes. (See RFC 2195)
+
+;; Examples.
+;;
+;; (sasl-scram-md5-make-security-info nil t 0)
+;; => "^A^@^@^@"
+;;
+;; (base64-encode-string
+;;  (sasl-scram-md5-make-client-msg-2
+;;   (base64-decode-string "dGVzdHNhbHQBAAAAaW1hcEBlbGVhbm9yLmlubm9zb2Z0LmNvbQBqaGNOWmxSdVBiemlGcCt2TFYrTkN3")
+;;   (base64-decode-string "AGNocmlzADx0NG40UGFiOUhCMEFtL1FMWEI3MmVnQGVsZWFub3IuaW5ub3NvZnQuY29tPg==")
+;;   (sasl-scram-md5-make-salted-pass
+;;    "secret stuff" "testsalt")
+;;   (sasl-scram-md5-make-security-info nil t 0)))
+;; => "AQAAAMg9jU8CeB4KOfk7sUhSQPs="
+;;
+;; (base64-encode-string
+;;  (sasl-scram-md5-make-server-msg-2
+;;   (base64-decode-string "dGVzdHNhbHQBAAAAaW1hcEBlbGVhbm9yLmlubm9zb2Z0LmNvbQBqaGNOWmxSdVBiemlGcCt2TFYrTkN3")
+;;   (base64-decode-string "AGNocmlzADx0NG40UGFiOUhCMEFtL1FMWEI3MmVnQGVsZWFub3IuaW5ub3NvZnQuY29tPg==")
+;;   (sasl-scram-md5-make-security-info nil t 0)
+;;   "testsalt"
+;;   (sasl-scram-md5-make-salted-pass
+;;    "secret stuff" "testsalt")))
+;; => "U0odqYw3B7XIIW0oSz65OQ=="
+
+;;; Code:
+
+(require 'sasl)
+(require 'hmac-md5)
+
+(defvar sasl-scram-md5-unique-id-function
+  sasl-unique-id-function)
+
+(defconst sasl-scram-md5-steps
+  '(ignore				;no initial response
+    sasl-scram-md5-response-1
+    sasl-scram-md5-response-2
+    sasl-scram-md5-authenticate-server))
+
+(defmacro sasl-scram-md5-security-info-no-security-layer (security-info)
+  `(eq (logand (aref ,security-info 0) 1) 1))
+(defmacro sasl-scram-md5-security-info-integrity-protection-layer (security-info)
+  `(eq (logand (aref ,security-info 0) 2) 2))
+(defmacro sasl-scram-md5-security-info-buffer-size (security-info)
+  `(let ((ssecinfo ,security-info))
+     (+ (lsh (aref ssecinfo 1) 16)
+	(lsh (aref ssecinfo 2) 8)
+	(aref ssecinfo 3))))
+
+(defun sasl-scram-md5-make-security-info (integrity-protection-layer
+					  no-security-layer buffer-size)
+  (let ((csecinfo (make-string 4 0)))
+    (when integrity-protection-layer
+      (aset csecinfo 0 2))
+    (if no-security-layer
+	(aset csecinfo 0 (logior (aref csecinfo 0) 1))
+      (aset csecinfo 1
+	    (lsh (logand buffer-size (lsh 255 16)) -16))
+      (aset csecinfo 2
+	    (lsh (logand buffer-size (lsh 255 8)) -8))
+      (aset csecinfo 3 (logand buffer-size 255)))
+    csecinfo))
+
+(defun sasl-scram-md5-make-unique-nonce ()	; 8*OCTET, globally unique.
+  ;; For example, concatenated string of process-identifier, system-clock,
+  ;; sequence-number, random-number, and domain-name.
+  (let* ((sasl-unique-id-function sasl-scram-md5-unique-id-function)
+	 (id (sasl-unique-id)))
+    (unwind-protect
+	(concat "<" id "@" (system-name) ">")
+      (fillarray id 0))))
+
+(defun sasl-scram-md5-xor-string (str1 str2)
+  ;; (length str1) == (length str2) == (length dst) == 16 (in SCRAM-MD5)
+  (let* ((len (length str1))
+         (dst (make-string len 0))
+         (pos 0))
+    (while (< pos len)
+      (aset dst pos (logxor (aref str1 pos) (aref str2 pos)))
+      (setq pos (1+ pos)))
+    dst))
+
+(defun sasl-scram-md5-make-client-msg-1 (authenticate-id &optional authorize-id nonce)
+  "Make an initial client message from AUTHENTICATE-ID and AUTHORIZE-ID.
+If AUTHORIZE-ID is the same as AUTHENTICATE-ID, it may be omitted."
+  (concat authorize-id "\0" authenticate-id "\0"
+	  (or nonce
+	      (sasl-scram-md5-make-unique-nonce))))
+
+(defun sasl-scram-md5-parse-server-msg-1 (server-msg-1)
+  "Parse SERVER-MSG-1 and return a list of (SALT SECURITY-INFO SERVICE-ID)."
+  (if (and (> (length server-msg-1) 16)
+	   (eq (string-match "[^@]+@[^\0]+\0" server-msg-1 12) 12))
+      (list (substring server-msg-1 0 8)	; salt
+	    (substring server-msg-1 8 12)	; server-security-info
+	    (substring server-msg-1	; service-id
+		       12 (1- (match-end 0))))
+    (sasl-error (format "Unexpected response: %s" server-msg-1))))
+
+(defun sasl-scram-md5-server-salt (server-msg-1)
+  (car (sasl-scram-md5-parse-server-msg-1 server-msg-1)))
+
+(defun sasl-scram-md5-make-salted-pass (passphrase salt)
+  (hmac-md5 salt passphrase))
+
+(defun sasl-scram-md5-make-client-key (salted-pass)
+  (md5-binary salted-pass))
+
+(defun sasl-scram-md5-make-client-verifier (client-key)
+  (md5-binary client-key))
+
+(defun sasl-scram-md5-make-shared-key (server-msg-1
+				       client-msg-1
+				       client-security-info
+				       client-verifier)
+  (let (buff)
+    (unwind-protect
+	(hmac-md5
+	 (setq buff
+	       (concat server-msg-1 client-msg-1 client-security-info))
+	 client-verifier)
+      (fillarray buff 0))))
+
+(defun sasl-scram-md5-make-client-proof (client-key shared-key)
+  (sasl-scram-md5-xor-string client-key shared-key))
+
+(defun sasl-scram-md5-make-client-msg-2 (server-msg-1
+					 client-msg-1
+					 salted-pass
+					 client-security-info)
+  (let (client-proof client-key shared-key client-verifier)
+    (setq client-key
+          (sasl-scram-md5-make-client-key salted-pass))
+    (setq client-verifier
+          (sasl-scram-md5-make-client-verifier client-key))
+    (setq shared-key
+          (unwind-protect
+              (sasl-scram-md5-make-shared-key
+               server-msg-1
+               client-msg-1
+               client-security-info
+               client-verifier)
+            (fillarray client-verifier 0)))
+    (setq client-proof
+          (unwind-protect
+              (sasl-scram-md5-make-client-proof
+               client-key shared-key)
+            (fillarray client-key 0)
+            (fillarray shared-key 0)))
+    (unwind-protect
+        (concat
+         client-security-info
+         client-proof)
+      (fillarray client-proof 0))))
+
+(defun sasl-scram-md5-make-server-msg-2 (server-msg-1
+					 client-msg-1
+					 client-security-info
+					 salt salted-pass)
+  (let ((server-salt
+	(hmac-md5 salt salted-pass))
+	buff)
+    (unwind-protect
+	(hmac-md5
+	 (setq buff
+	       (concat
+		client-msg-1
+		server-msg-1
+		client-security-info))
+	 server-salt)
+      (fillarray server-salt 0)
+      (fillarray buff 0))))
+
+(defun sasl-scram-md5-response-1 (client step)
+  (sasl-client-set-property
+   client 'client-msg-1
+   (sasl-scram-md5-make-client-msg-1
+    (sasl-client-name client)
+    (sasl-client-property client 'authorize-id)
+    (sasl-client-property client 'nonce))))
+
+(defun sasl-scram-md5-response-2 (client step)
+  (let* ((server-msg-1
+	  (sasl-client-set-property
+	   client 'server-msg-1
+	   (sasl-step-data step)))
+	 (salted-pass
+	  (sasl-client-set-property
+	   client 'salted-pass
+	   (sasl-scram-md5-make-salted-pass
+	    (sasl-read-passphrase
+	     (format "SCRAM-MD5 passphrase for %s: "
+		     (sasl-client-name client)))
+	    (sasl-scram-md5-server-salt server-msg-1)))))
+    (sasl-client-set-property
+     client 'client-msg-2
+     (sasl-scram-md5-make-client-msg-2
+      server-msg-1
+      (sasl-client-property client 'client-msg-1)
+      salted-pass
+      (or (sasl-client-property client 'client-security-info)
+	  (sasl-scram-md5-make-security-info nil t 0))))))
+
+(defun sasl-scram-md5-authenticate-server (client step)
+  (let ((server-msg-2
+	 (sasl-client-set-property
+	  client 'server-msg-2
+	  (sasl-step-data step)))
+	(server-msg-1
+	 (sasl-client-property client 'server-msg-1)))
+    (if (string= server-msg-2
+		     (sasl-scram-md5-make-server-msg-2
+		      server-msg-1
+		      (sasl-client-property client 'client-msg-1)
+		      (or (sasl-client-property client 'client-security-info)
+			  (sasl-scram-md5-make-security-info nil t 0))
+		      (sasl-scram-md5-server-salt server-msg-1)
+		      (sasl-client-property client 'salted-pass)))
+	" "
+      (sasl-error "SCRAM-MD5:  authenticate server failed."))))
+
+(put 'sasl-scram 'sasl-mechanism
+     (sasl-make-mechanism "SCRAM-MD5" sasl-scram-md5-steps))
+
+(provide 'sasl-scram)
+
+;;; sasl-scram.el ends here
+;;; sasl.el --- SASL client framework
+
+;; Copyright (C) 2000 Free Software Foundation, Inc.
+
+;; Author: Daiki Ueno <ueno@unixuser.org>
+;; Keywords: SASL
+
+;; This file is part of FLIM (Faithful Library about Internet Message).
+
+;; This program is free software; you can redistribute it and/or
+;; modify it under the terms of the GNU General Public License as
+;; published by the Free Software Foundation; either version 2, or (at
+;; your option) any later version.
+
+;; This program is distributed in the hope that it will be useful, but
+;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;; General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
+
+;;; Commentary:
+
+;; This module provides common interface functions to share several
+;; SASL mechanism drivers.  The toplevel is designed to be mostly
+;; compatible with [Java-SASL].
+;;
+;; [SASL] J. Myers, "Simple Authentication and Security Layer (SASL)",
+;;	RFC 2222, October 1997.
+;;
+;; [Java-SASL] R. Weltman & R. Lee, "The Java SASL Application Program
+;;	Interface", draft-weltman-java-sasl-03.txt, March 2000.
+
+;;; Code:
+
+(defvar sasl-mechanisms
+  '("CRAM-MD5" "DIGEST-MD5" "PLAIN" "LOGIN" "ANONYMOUS"
+    "NTLM" "SCRAM-MD5"))
+
+(defvar sasl-mechanism-alist
+  '(("CRAM-MD5" sasl-cram)
+    ("DIGEST-MD5" sasl-digest)
+    ("PLAIN" sasl-plain)
+    ("LOGIN" sasl-login)
+    ("ANONYMOUS" sasl-anonymous)
+    ("NTLM" sasl-ntlm)
+    ("SCRAM-MD5" sasl-scram)))
+
+(defvar sasl-unique-id-function #'sasl-unique-id-function)
+
+(put 'sasl-error 'error-message "SASL error")
+(put 'sasl-error 'error-conditions '(sasl-error error))
+
+(defun sasl-error (datum)
+  (signal 'sasl-error (list datum)))
+
+;;; @ SASL client
+;;;
+
+(defun sasl-make-client (mechanism name service server)
+  "Return a newly allocated SASL client.
+NAME is name of the authorization.  SERVICE is name of the service desired.
+SERVER is the fully qualified host name of the server to authenticate to."
+  (vector mechanism name service server (make-symbol "sasl-client-properties")))
+
+(defun sasl-client-mechanism (client)
+  "Return the authentication mechanism driver of CLIENT."
+  (aref client 0))
+
+(defun sasl-client-name (client)
+  "Return the authorization name of CLIENT, a string."
+  (aref client 1))
+
+(defun sasl-client-service (client)
+  "Return the service name of CLIENT, a string."
+  (aref client 2))
+
+(defun sasl-client-server (client)
+  "Return the server name of CLIENT, a string."
+  (aref client 3))
+
+(defun sasl-client-set-properties (client plist)
+  "Destructively set the properties of CLIENT.
+The second argument PLIST is the new property list."
+  (setplist (aref client 4) plist))
+
+(defun sasl-client-set-property (client property value)
+  "Add the given property/value to CLIENT."
+  (put (aref client 4) property value))
+
+(defun sasl-client-property (client property)
+  "Return the value of the PROPERTY of CLIENT."
+  (get (aref client 4) property))
+
+(defun sasl-client-properties (client)
+  "Return the properties of CLIENT."
+  (symbol-plist (aref client 4)))
+
+;;; @ SASL mechanism
+;;;
+
+(defun sasl-make-mechanism (name steps)
+  "Make an authentication mechanism.
+NAME is a IANA registered SASL mechanism name.
+STEPS is list of continuation function."
+  (vector name
+	  (mapcar
+	   (lambda (step)
+	     (let ((symbol (make-symbol (symbol-name step))))
+	       (fset symbol (symbol-function step))
+	       symbol))