Commits

cvs2hg  committed 96628be

fixup commit for tag 'sumo-current'

  • Participants
  • Parent commits 912dd17
  • Tags sumo-current

Comments (0)

Files changed (3)

+;;;  idea.el -- block cipher
+
+;; Copyright (C) 1998 Ray Jones
+
+;; Author: Ray Jones, rjones@pobox.com
+;; Keywords: IDEA, oink, cipher, cypher, cryptography
+;; Created: 1998-04-01
+
+;; 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; if not, you can either send email to this
+;; program's maintainer or write to: The Free Software Foundation,
+;; Inc.; 675 Massachusetts Avenue; Cambridge, MA 02139, USA.
+
+;;; Commentary:
+
+;; this code probably isn't as efficient as it could be.
+;; neither am i, though.
+
+;;; Code:
+
+(require 'cl)
+
+;; multiplication mod (2^16)+1, chopped to 16 bits
+;; works by splitting multiplicand into two 8 bit parts
+;; note that an argument of 0 is treated as if it were 2^16
+(defun idea-mul (a b)
+  (if (or (= a 0)
+          (= b 0))
+      (logand (- ?\x10001 a b) ?\xffff)
+
+    ;; split a into 8 bit pieces
+    (let* ((low (logand a ?\xff))
+           (high (ash a -8)))
+
+      ;; multiply low and high parts by b
+      (setq low (* low b))
+      (setq high (* high b))
+
+      ;; add overlapped bits of high and low, store in low
+      (setq low (+ low (ash (logand ?\xff high) 8)))
+      
+      ;; shift high so high and low do not overlap
+      (setq high (+ (ash high -8) (ash low -16)))
+      (setq low (logand low ?\xffff))
+
+      ;; product is now (+ (ash high 16) low)
+      
+      ;; optimized mod operation
+      (setq low (- low high))
+      (if (<= low 0)
+          (logand (+ low ?\x10001) ?\xffff)
+        (logand low ?\xffff)))))
+
+;; multiplicative inverse, mod (2^16)+1 (which is prime)
+;; uses extended Euclid algorithm
+(defun idea-mul-inv (x)
+  (if (= x 0)
+      0
+    ;; calculate am + bn = d, d = greatest common divisor of m,n.
+    ;; if m is prime, then b and n are multiplicative inverses
+    (let ((m ?\x10001)
+          (n x)
+          (a 0)
+          (b 1)
+          (not-done t))
+      (while not-done
+        (let ((r (mod m n))
+              (q (/ m n))
+              (temp b))
+          (if (= r 0)
+              (setq not-done nil)
+            (progn
+              (setq m n)
+              (setq n r)
+              (setq b (- a (* q b)))
+              (setq a temp)))))
+      (if (< b 0)
+          (logand (+ b ?\x10001) ?\xffff)
+        (logand b ?\xffff)))))
+
+
+(defconst *idea-rounds* 8)
+(defconst *idea-subkey-number* 52)
+
+;; generate internal encryption keys from an external key
+(defun idea-encrypt-subkeys (key &optional xor-safe)
+  "generate the IDEA-subkeys from a 128-bit (8 element vector of
+16-bit values).  optional second arg controls XORing with 0x0dae to
+prevent \"weak\" keys from being generated.  return vector of 52
+16-bit numbers."
+
+  ;; sanity check
+  (assert (= (length key) 8) nil
+	  "idea-encrypt-subkeys: first arg must be of length 8")
+
+  (let ((subkeys (make-vector *idea-subkey-number* 0)))
+
+    (dotimes (idx 8)
+      (aset subkeys idx (aref key idx)))
+
+    (do ((idx 8 (1+ idx)))
+	((= idx *idea-subkey-number*))
+
+      (let* ((idx1 (if (= 0 (mod (1+ idx) 8))
+		       (- idx 15)
+		     (- idx 7)))
+	     (idx2 (if (< (mod (+ idx 2) 8) 2)
+		       (- idx 14)
+		     (- idx 6))))
+	(aset subkeys idx (logand ?\xffff
+				  (logior (ash (aref subkeys idx1) 9)
+					  (ash (aref subkeys idx2)
+					       -7))))))
+    (when xor-safe
+      (dotimes (idx *idea-subkey-number*)
+	(aset subkeys idx (logxor ?\x0dae (aref subkeys idx)))))
+
+    subkeys))
+
+(defun idea-decrypt-subkeys (enc-subkeys)
+  "generate IDEA decryptions subkeys from the encryption subkeys"
+  (let ((dec-subkeys (make-vector *idea-subkey-number* 0))
+	(in-idx 0))
+    (flet ((next-subkey ()
+	      (prog1
+		  (aref enc-subkeys in-idx)
+		(incf in-idx))))
+
+      (let ((idx (* 6 *idea-rounds*)))
+	(aset dec-subkeys (+ idx 0) (idea-mul-inv (next-subkey)))
+	(aset dec-subkeys (+ idx 1) (logand ?\xffff (- (next-subkey))))
+	(aset dec-subkeys (+ idx 2) (logand ?\xffff (- (next-subkey))))
+	(aset dec-subkeys (+ idx 3) (idea-mul-inv (next-subkey))))
+
+      (do ((idx (* 6 (1- *idea-rounds*)) (- idx 6)))
+	  ((< idx 0))
+	(aset dec-subkeys (+ idx 4) (next-subkey))
+	(aset dec-subkeys (+ idx 5) (next-subkey))
+	(aset dec-subkeys (+ idx 0) (idea-mul-inv (next-subkey)))
+	
+	(if (= 0 idx)
+	    (progn
+	      (aset dec-subkeys (+ idx 1) (logand ?\xffff (- (next-subkey))))
+	      (aset dec-subkeys (+ idx 2) (logand ?\xffff (- (next-subkey)))))
+	  (progn
+	    (aset dec-subkeys (+ idx 2) (logand ?\xffff (- (next-subkey))))
+	    (aset dec-subkeys (+ idx 1) (logand ?\xffff (- (next-subkey))))))
+	
+	(aset dec-subkeys (+ idx 3) (idea-mul-inv (next-subkey)))))
+    dec-subkeys))
+
+;; encrypt a 64-bit block of data (4 16-bit words), using the subkeys
+;; provided, in place 
+(defun idea-cipher-block (data subkeys)
+  (let ((word0 (aref data 0))
+        (word1 (aref data 1))
+        (word2 (aref data 2))
+        (word3 (aref data 3))
+        (idx 0)
+	(key-idx 0)
+        t1 t2)
+    (flet ((next-subkey ()
+	      (prog1
+		  (aref subkeys key-idx)
+		(incf key-idx))))
+
+      (dotimes (idx *idea-rounds*)
+	(setq word0 (idea-mul word0 (next-subkey)))
+	(setq word1 (logand ?\xffff (+ word1 (next-subkey))))
+	(setq word2 (logand ?\xffff (+ word2 (next-subkey))))
+	(setq word3 (idea-mul word3 (next-subkey)))
+	
+	(setq t2 (idea-mul (logxor word0 word2) 
+			   (next-subkey)))
+	(setq t1 (idea-mul (logand ?\xffff (+ t2 (logxor word1 word3)))
+			   (next-subkey)))
+	(setq t2 (logand ?\xffff (+ t1 t2)))
+	
+	(setq word0 (logxor word0 t1))
+	(setq word3 (logxor word3 t2))
+	
+	(setq t2 (logxor t2 word1))
+	(setq word1 (logxor word2 t1))
+	(setq word2 t2))
+      
+      (setq word0 (idea-mul word0 (next-subkey)))
+      (setq word2 (logand ?\xffff (+ word2 (next-subkey))))
+      (setq word1 (logand ?\xffff (+ word1 (next-subkey))))
+      (setq word3 (idea-mul word3 (next-subkey))))
+      
+    ;; word 1 and 2 are swapped before output
+    (aset data 0 word0)
+    (aset data 1 word2)
+    (aset data 2 word1)
+    (aset data 3 word3)))
+
+(defun idea-cbc-encode (data subkeys &optional IV)
+  "encrypts its first argument, a vector of 16-bit ints, with the keys
+in its second argument, using the IV in the optional third argument
+(which is prepended to the output vector).  if IV is nil, it is taken
+as \[0 0 0 0\] and not prepended to the output.
+returns a vector of 16-bit ints."
+  (let* ((len (length data))
+	 (out-vec (make-vector (if IV (+ len 4) len) 0))
+	 (temp-vec (make-vector 4 0)))
+
+    ;; sanity checks
+    (assert (zerop (mod len 4)) nil
+	    "idea-cbc-encode: length of data must be a multiple of 4")
+    (when IV
+      (assert (= 4 (length IV)) nil 
+	      "idea-cbc-encode: length of IV must be 4"))
+    (assert (= *idea-subkey-number* (length subkeys)) nil
+	    "idea-cbc-encode: there must be %d subkeys"
+	    *idea-subkey-number*)
+
+    ;; write IV into output and temp-vec, if present
+    (when IV
+      (dotimes (idx 4)
+	(aset temp-vec idx (aref IV idx))
+	(aset out-vec idx (aref IV idx))))
+
+    ;; encrypt the rest of the input in CBC mode
+    (do ((in-idx 0 (+ in-idx 4))
+	 (out-idx (if IV 4 0) (+ out-idx 4)))
+	((= in-idx len))
+
+      (dotimes (offset 4)
+	;; XOR the current plaintext block and previous ciphertext
+	;; block (which is in temp-vec) into temp-vec.  (if no IV was
+	;; given, then temp-vec will be all zeroes.)
+	(aset temp-vec offset (logxor (aref data (+ in-idx offset))
+				      (aref temp-vec offset))))
+
+      ;; encrypt temp-vec in place
+      (idea-cipher-block temp-vec subkeys)
+      
+      ;; write temp-vec into the output
+      (dotimes (offset 4)
+        (aset out-vec (+ out-idx offset) (aref temp-vec offset))))
+
+    ;; clean up 
+    (fillarray temp-vec 0)
+
+    out-vec))
+
+(defun idea-cbc-decode (data subkeys &optional no-iv)
+  "decrypts its first argument, a vector of 16-bit ints, with the keys
+in its second argument.  third argument, if non-nil, indicates that
+the data does not have an IV prepended, and that the IV should be
+taken as \[0 0 0 0\].
+returns a vector of 16-bit ints."
+  (let* ((len (length data))
+         (out-vec (make-vector (max 0 (if no-iv len (- len 4))) 0))
+         (temp-vec (make-vector 4 0))
+	 (cur-block (make-vector 4 0))
+	 (prev-block (make-vector 4 0)))
+    
+    ;; sanity checks
+    (assert (zerop (mod len 4)) nil
+	    "idea-cbc-decode: length of data must be a multiple of 4")
+    (unless no-iv
+      (assert (> len 0) nil
+	      "idea-cbc-decode: length of data must be at least 4"))
+    (assert (= *idea-subkey-number* (length subkeys)) nil
+	    "idea-cbc-decode: there must be %d subkeys"
+	    *idea-subkey-number*)
+    
+    ;; set up the feedback block
+    (unless no-iv
+      (dotimes (offset 4)
+	(aset prev-block offset (aref data offset))))
+
+    ;; decrypt the input in CBC mode
+    (do ((in-idx (if no-iv 0 4) (+ in-idx 4))
+	 (out-idx 0 (+ out-idx 4)))
+	((= in-idx len))
+
+      ;; copy the ciphertext block into temp-vec and cur-block
+      (dotimes (offset 4)
+	(let ((x (aref data (+ in-idx offset))))
+	  (aset temp-vec offset x)
+	  (aset cur-block offset x)))
+
+      ;; decrypt temp-vec in place with the keys
+      (idea-cipher-block temp-vec subkeys)
+
+      (dotimes (offset 4)
+	;; XOR the output of the cipher with the previous ciphertext
+	;; block, storing the result in the output
+	(aset out-vec 
+	      (+ out-idx offset)
+	      (logxor (aref temp-vec offset)
+		      (aref prev-block offset)))
+	;; move cur-block into prev-block
+	(aset prev-block offset (aref cur-block offset))))
+
+    ;; clean up
+    (fillarray temp-vec 0)
+    (fillarray prev-block 0)
+    (fillarray cur-block 0)
+
+    out-vec))
+
+(defun idea-package-transform (data key)
+  "perform the idea-cbc package transform on DATA using KEY.
+all arguments are vectors of 16-bit integers.  DATA should be 4*n
+elements long, KEY should be 8 elements.  no IV is used to seed the
+CBC, since the key is (supposedly) random.
+
+package transform is:
+  m <= \(idea-cbc data key\)
+  k <= key
+  k[i%8] <= \(logxor k[i%8] m[i]\) ;; i from 0 to \(length m\)
+  output <= \(append m k\)"
+
+  (let* ((subkeys (idea-encrypt-subkeys key))
+         (enc-data (idea-cbc-encode data subkeys))
+         (enc-len (length enc-data))
+         ;; m is enc-data with the key appended
+         (m-len (+ enc-len 8))
+         (m (make-vector m-len 0))
+         ;; scratch space for the key
+         (k (make-vector 8 0)))
+    
+    ;; copy key into k
+    (dotimes (key-idx 8)
+      (aset k key-idx (aref key key-idx)))
+
+    ;; XOR the k with the blocks of enc-data
+    (do ((idx 0 (1+ idx))
+         (key-idx 0 (% (1+ key-idx) 8)))
+        ((= idx enc-len))
+      (aset k key-idx (logxor (aref k key-idx)
+                              (aref enc-data idx))))
+
+    ;; copy the encoded data into m
+    (dotimes (idx enc-len)
+      (aset m idx (aref enc-data idx)))
+
+    ;; copy k into the final elements of m
+    (dotimes (key-idx 8)
+      (aset m (+ enc-len key-idx) (aref k key-idx)))
+
+    ;; clean up
+    (fillarray enc-data 0)
+    (fillarray subkeys 0)
+    (fillarray k 0)
+    
+    m))
+
+
+(defun idea-package-untransform (data)
+  "reverse the idea-cbc package transform on DATA.  see the
+documentation for idea-package-transform for an explanation of the
+transform that this function undoes."
+
+  ;; decode m, allocate space for encrypted message and key
+  (let* ((len (length data))
+         (enc-len (- len 8))
+         (enc-data (make-vector enc-len 0))
+         (key (make-vector 8 0)))
+    
+    ;; extract enc-data
+    (dotimes (idx enc-len)
+      (aset enc-data idx (aref data idx)))
+
+    ;; extract the XORed key
+    (dotimes (key-idx 8)
+      (aset key key-idx (aref data (+ enc-len key-idx))))
+
+    ;; XOR the key with the blocks of enc-data
+    (do ((idx 0 (1+ idx))
+         (key-idx 0 (% (1+ key-idx) 8)))
+        ((= idx enc-len))
+      (aset key key-idx (logxor (aref key key-idx)
+                                (aref enc-data idx))))
+
+    ;; generate the decryption keys
+    (let* ((enc-subkeys (idea-encrypt-subkeys key))
+           (dec-subkeys (idea-decrypt-subkeys enc-subkeys)))
+      
+      ;; extract the result and clean up
+      (prog1
+	  ;; no IV sent included in package transform
+          (idea-cbc-decode enc-data dec-subkeys t)
+        (fillarray enc-data 0)
+        (fillarray key 0)
+        (fillarray enc-subkeys 0)
+        (fillarray dec-subkeys 0)))))
+
+(provide 'idea)
+;;; sha1-dl.el --- SHA1 Secure Hash Algorithm using DL module.
+
+;; Copyright (C) 1999, 2001  Free Software Foundation, Inc.
+
+;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
+;; Keywords: SHA1, FIPS 180-1
+
+;; 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 'sha1-dl)			; beware of circular dependency.
+(eval-when-compile (require 'sha1))	; sha1-dl-module.
+
+;;; This file is loaded (from "sha1.el") only when sha1-dl-module exists.
+(defvar sha1-dl-handle (dynamic-link sha1-dl-module))
+
+;;; sha1-dl-module provides `sha1-string' and `sha1-binary'.
+(dynamic-call "emacs_sha1_init" sha1-dl-handle)
+
+(defun sha1-region (beg end)
+  (sha1-string (buffer-substring-no-properties beg end)))
+
+(defun sha1 (object &optional beg end)
+  "Return the SHA1 (Secure Hash 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)
+      (sha1-string object)
+    (save-excursion
+      (set-buffer object)
+      (sha1-region (or beg (point-min)) (or end (point-max))))))
+
+(provide 'sha1-dl)
+
+;;; sha1-dl.el ends here
+;;; sha1-el.el --- SHA1 Secure Hash Algorithm in Emacs-Lisp
+
+;; Copyright (C) 1999, 2001, 2003, 2004 Free Software Foundation, Inc.
+
+;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
+;; Keywords: SHA1, FIPS 180-1
+
+;; 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 the definition of SHA-1 in FIPS PUB
+;; 180-1 (Federal Information Processing Standards Publication 180-1),
+;; "Announcing the Standard for SECURE HASH STANDARD".
+;; <URL:http://www.itl.nist.gov/div897/pubs/fip180-1.htm>
+;; (EXCEPTION; two optimizations taken from GnuPG/cipher/sha1.c)
+;;
+;; Test cases from FIPS PUB 180-1.
+;;
+;; (sha1 "abc")
+;; => a9993e364706816aba3e25717850c26c9cd0d89d
+;;
+;; (sha1 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
+;; => 84983e441c3bd26ebaae4aa1f95129e5e54670f1
+;;
+;; (sha1 (make-string 1000000 ?a))
+;; => 34aa973cd4c4daa4f61eeb2bdbad27316534016f
+;;
+;; BUGS:
+;;  * It is assumed that length of input string is less than 2^29 bytes.
+;;  * It is caller's responsibility to make string (or region) unibyte.
+;;
+;; TODO:
+;;  * Rewrite from scratch!
+;;    This version is much faster than Keiichi Suzuki's another sha1.el,
+;;    but it is too dirty.
+
+;;; Code:
+
+(require 'hex-util)
+
+(autoload 'executable-find "executable")
+
+;;;
+;;; external SHA1 function.
+;;;
+
+(defgroup sha1 nil
+  "Elisp interface for SHA1 hash computation."
+  :group 'extensions)
+
+(defcustom sha1-maximum-internal-length 500
+  "*Maximum length of message to use lisp version of SHA1 function.
+If message is longer than this, `sha1-program' is used instead.
+
+If this variable is set to 0, use extarnal program only.
+If this variable is set to nil, use internal function only."
+  :type 'integer
+  :group 'sha1)
+
+(defcustom sha1-program '("sha1sum")
+  "*Name of program to compute SHA1.
+It must be a string \(program name\) or list of strings \(name and its args\)."
+  :type '(repeat string)
+  :group 'sha1)
+
+(defcustom sha1-use-external (condition-case ()
+				 (executable-find (car sha1-program))
+			       (error))
+  "*Use external SHA1 program.
+If this variable is set to nil, use internal function only."
+  :type 'boolean
+  :group 'sha1)
+
+(defun sha1-string-external (string &optional binary)
+  (let (prog args digest default-enable-multibyte-characters)
+    (if (consp sha1-program)
+	(setq prog (car sha1-program)
+	      args (cdr sha1-program))
+      (setq prog sha1-program
+	    args nil))
+    (with-temp-buffer
+      (insert string)
+      (apply (function call-process-region)
+	     (point-min)(point-max)
+	     prog t t nil args)
+      ;; SHA1 is 40 bytes long in hexadecimal form.
+      (setq digest (buffer-substring (point-min)(+ (point-min) 40))))
+    (if binary
+	(decode-hex-string digest)
+      digest)))
+
+(defun sha1-region-external (beg end &optional binary)
+  (sha1-string-external (buffer-substring-no-properties beg end) binary))
+
+;;;
+;;; internal SHA1 function.
+;;;
+
+(eval-when-compile
+  ;; optional second arg of string-to-number is new in v20.
+  (defconst sha1-K0-high 23170)		; (string-to-number "5A82" 16)
+  (defconst sha1-K0-low  31129)		; (string-to-number "7999" 16)
+  (defconst sha1-K1-high 28377)		; (string-to-number "6ED9" 16)
+  (defconst sha1-K1-low  60321)		; (string-to-number "EBA1" 16)
+  (defconst sha1-K2-high 36635)		; (string-to-number "8F1B" 16)
+  (defconst sha1-K2-low  48348)		; (string-to-number "BCDC" 16)
+  (defconst sha1-K3-high 51810)		; (string-to-number "CA62" 16)
+  (defconst sha1-K3-low  49622)		; (string-to-number "C1D6" 16)
+
+;;; original definition of sha1-F0.
+;;; (defmacro sha1-F0 (B C D)
+;;;   (` (logior (logand (, B) (, C))
+;;; 	     (logand (lognot (, B)) (, D)))))
+;;; a little optimization from GnuPG/cipher/sha1.c.
+  (defmacro sha1-F0 (B C D)
+    (` (logxor (, D) (logand (, B) (logxor (, C) (, D))))))
+  (defmacro sha1-F1 (B C D)
+    (` (logxor (, B) (, C) (, D))))
+;;; original definition of sha1-F2.
+;;; (defmacro sha1-F2 (B C D)
+;;;   (` (logior (logand (, B) (, C))
+;;; 	     (logand (, B) (, D))
+;;; 	     (logand (, C) (, D)))))
+;;; a little optimization from GnuPG/cipher/sha1.c.
+  (defmacro sha1-F2 (B C D)
+    (` (logior (logand (, B) (, C))
+	       (logand (, D) (logior (, B) (, C))))))
+  (defmacro sha1-F3 (B C D)
+    (` (logxor (, B) (, C) (, D))))
+
+  (defmacro sha1-S1  (W-high W-low)
+    (` (let ((W-high (, W-high))
+	     (W-low  (, W-low)))
+	 (setq S1W-high (+ (% (* W-high 2) 65536)
+			   (/ W-low (, (/ 65536 2)))))
+	 (setq S1W-low (+ (/ W-high (, (/ 65536 2)))
+			  (% (* W-low 2) 65536))))))
+  (defmacro sha1-S5  (A-high A-low)
+    (` (progn
+	 (setq S5A-high (+ (% (* (, A-high) 32) 65536)
+			   (/ (, A-low) (, (/ 65536 32)))))
+	 (setq S5A-low  (+ (/ (, A-high) (, (/ 65536 32)))
+			   (% (* (, A-low) 32) 65536))))))
+  (defmacro sha1-S30 (B-high B-low)
+    (` (progn
+	 (setq S30B-high (+ (/ (, B-high) 4)
+			    (* (% (, B-low) 4) (, (/ 65536 4)))))
+	 (setq S30B-low  (+ (/ (, B-low) 4)
+			    (* (% (, B-high) 4) (, (/ 65536 4))))))))
+
+  (defmacro sha1-OP (round)
+    (` (progn
+	 (sha1-S5 sha1-A-high sha1-A-low)
+	 (sha1-S30 sha1-B-high sha1-B-low)
+	 (setq sha1-A-low (+ ((, (intern (format "sha1-F%d" round)))
+			      sha1-B-low sha1-C-low sha1-D-low)
+			     sha1-E-low
+			     (, (symbol-value
+				 (intern (format "sha1-K%d-low" round))))
+			     (aref block-low idx)
+			     (progn
+			       (setq sha1-E-low sha1-D-low)
+			       (setq sha1-D-low sha1-C-low)
+			       (setq sha1-C-low S30B-low)
+			       (setq sha1-B-low sha1-A-low)
+			       S5A-low)))
+	 (setq carry (/ sha1-A-low 65536))
+	 (setq sha1-A-low (% sha1-A-low 65536))
+	 (setq sha1-A-high (% (+ ((, (intern (format "sha1-F%d" round)))
+				  sha1-B-high sha1-C-high sha1-D-high)
+				 sha1-E-high
+				 (, (symbol-value
+				     (intern (format "sha1-K%d-high" round))))
+				 (aref block-high idx)
+				 (progn
+				   (setq sha1-E-high sha1-D-high)
+				   (setq sha1-D-high sha1-C-high)
+				   (setq sha1-C-high S30B-high)
+				   (setq sha1-B-high sha1-A-high)
+				   S5A-high)
+				 carry)
+			      65536)))))
+
+  (defmacro sha1-add-to-H (H X)
+    (` (progn
+	 (setq (, (intern (format "sha1-%s-low" H)))
+	       (+ (, (intern (format "sha1-%s-low" H)))
+		  (, (intern (format "sha1-%s-low" X)))))
+	 (setq carry (/ (, (intern (format "sha1-%s-low" H))) 65536))
+	 (setq (, (intern (format "sha1-%s-low" H)))
+	       (% (, (intern (format "sha1-%s-low" H))) 65536))
+	 (setq (, (intern (format "sha1-%s-high" H)))
+	       (% (+ (, (intern (format "sha1-%s-high" H)))
+		     (, (intern (format "sha1-%s-high" X)))
+		     carry)
+		  65536)))))
+  )
+
+;;; buffers (H0 H1 H2 H3 H4).
+(defvar sha1-H0-high)
+(defvar sha1-H0-low)
+(defvar sha1-H1-high)
+(defvar sha1-H1-low)
+(defvar sha1-H2-high)
+(defvar sha1-H2-low)
+(defvar sha1-H3-high)
+(defvar sha1-H3-low)
+(defvar sha1-H4-high)
+(defvar sha1-H4-low)
+
+(defun sha1-block (block-high block-low)
+  (let (;; step (c) --- initialize buffers (A B C D E).
+	(sha1-A-high sha1-H0-high) (sha1-A-low sha1-H0-low)
+	(sha1-B-high sha1-H1-high) (sha1-B-low sha1-H1-low)
+	(sha1-C-high sha1-H2-high) (sha1-C-low sha1-H2-low)
+	(sha1-D-high sha1-H3-high) (sha1-D-low sha1-H3-low)
+	(sha1-E-high sha1-H4-high) (sha1-E-low sha1-H4-low)
+	(idx 16))
+    ;; step (b).
+    (let (;; temporary variables used in sha1-S1 macro.
+	  S1W-high S1W-low)
+      (while (< idx 80)
+	(sha1-S1 (logxor (aref block-high (- idx 3))
+			 (aref block-high (- idx 8))
+			 (aref block-high (- idx 14))
+			 (aref block-high (- idx 16)))
+		 (logxor (aref block-low  (- idx 3))
+			 (aref block-low  (- idx 8))
+			 (aref block-low  (- idx 14))
+			 (aref block-low  (- idx 16))))
+	(aset block-high idx S1W-high)
+	(aset block-low  idx S1W-low)
+	(setq idx (1+ idx))))
+    ;; step (d).
+    (setq idx 0)
+    (let (;; temporary variables used in sha1-OP macro.
+	  S5A-high S5A-low S30B-high S30B-low carry)
+      (while (< idx 20) (sha1-OP 0) (setq idx (1+ idx)))
+      (while (< idx 40) (sha1-OP 1) (setq idx (1+ idx)))
+      (while (< idx 60) (sha1-OP 2) (setq idx (1+ idx)))
+      (while (< idx 80) (sha1-OP 3) (setq idx (1+ idx))))
+    ;; step (e).
+    (let (;; temporary variables used in sha1-add-to-H macro.
+	  carry)
+      (sha1-add-to-H H0 A)
+      (sha1-add-to-H H1 B)
+      (sha1-add-to-H H2 C)
+      (sha1-add-to-H H3 D)
+      (sha1-add-to-H H4 E))))
+
+(defun sha1-binary (string)
+  "Return the SHA1 of STRING in binary form."
+  (let (;; prepare buffers for a block. byte-length of block is 64.
+	;; input block is split into two vectors.
+	;;
+	;; input block: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ...
+	;; block-high:  +-0-+       +-1-+       +-2-+       +-3-+
+	;; block-low:         +-0-+       +-1-+       +-2-+       +-3-+
+	;;
+	;; length of each vector is 80, and elements of each vector are
+	;; 16bit integers.  elements 0x10-0x4F of each vector are
+	;; assigned later in `sha1-block'.
+	(block-high (eval-when-compile (make-vector 80 nil)))
+	(block-low  (eval-when-compile (make-vector 80 nil))))
+    (unwind-protect
+	(let* (;; byte-length of input string.
+	       (len (length string))
+	       (lim (* (/ len 64) 64))
+	       (rem (% len 4))
+	       (idx 0)(pos 0))
+	  ;; initialize buffers (H0 H1 H2 H3 H4).
+	  (setq sha1-H0-high 26437	; (string-to-number "6745" 16)
+		sha1-H0-low  8961	; (string-to-number "2301" 16)
+		sha1-H1-high 61389	; (string-to-number "EFCD" 16)
+		sha1-H1-low  43913	; (string-to-number "AB89" 16)
+		sha1-H2-high 39098	; (string-to-number "98BA" 16)
+		sha1-H2-low  56574	; (string-to-number "DCFE" 16)
+		sha1-H3-high 4146	; (string-to-number "1032" 16)
+		sha1-H3-low  21622	; (string-to-number "5476" 16)
+		sha1-H4-high 50130	; (string-to-number "C3D2" 16)
+		sha1-H4-low  57840)	; (string-to-number "E1F0" 16)
+	  ;; loop for each 64 bytes block.
+	  (while (< pos lim)
+	    ;; step (a).
+	    (setq idx 0)
+	    (while (< idx 16)
+	      (aset block-high idx (+ (* (aref string pos) 256)
+				      (aref string (1+ pos))))
+	      (setq pos (+ pos 2))
+	      (aset block-low  idx (+ (* (aref string pos) 256)
+				      (aref string (1+ pos))))
+	      (setq pos (+ pos 2))
+	      (setq idx (1+ idx)))
+	    (sha1-block block-high block-low))
+	  ;; last block.
+	  (if (prog1
+		  (< (- len lim) 56)
+		(setq lim (- len rem))
+		(setq idx 0)
+		(while (< pos lim)
+		  (aset block-high idx (+ (* (aref string pos) 256)
+					  (aref string (1+ pos))))
+		  (setq pos (+ pos 2))
+		  (aset block-low  idx (+ (* (aref string pos) 256)
+					  (aref string (1+ pos))))
+		  (setq pos (+ pos 2))
+		  (setq idx (1+ idx)))
+		;; this is the last (at most) 32bit word.
+		(cond
+		 ((= rem 3)
+		  (aset block-high idx (+ (* (aref string pos) 256)
+					  (aref string (1+ pos))))
+		  (setq pos (+ pos 2))
+		  (aset block-low  idx (+ (* (aref string pos) 256)
+					  128)))
+		 ((= rem 2)
+		  (aset block-high idx (+ (* (aref string pos) 256)
+					  (aref string (1+ pos))))
+		  (aset block-low  idx 32768))
+		 ((= rem 1)
+		  (aset block-high idx (+ (* (aref string pos) 256)
+					  128))
+		  (aset block-low  idx 0))
+		 (t ;; (= rem 0)
+		  (aset block-high idx 32768)
+		  (aset block-low  idx 0)))
+		(setq idx (1+ idx))
+		(while (< idx 16)
+		  (aset block-high idx 0)
+		  (aset block-low  idx 0)
+		  (setq idx (1+ idx))))
+	      ;; last block has enough room to write the length of string.
+	      (progn
+		;; write bit length of string to last 4 bytes of the block.
+		(aset block-low  15 (* (% len 8192) 8))
+		(setq len (/ len 8192))
+		(aset block-high 15 (% len 65536))
+		;; XXX: It is not practical to compute SHA1 of
+		;;      such a huge message on emacs.
+		;; (setq len (/ len 65536))	; for 64bit emacs.
+		;; (aset block-low  14 (% len 65536))
+		;; (aset block-high 14 (/ len 65536))
+		(sha1-block block-high block-low))
+	    ;; need one more block.
+	    (sha1-block block-high block-low)
+	    (fillarray block-high 0)
+	    (fillarray block-low  0)
+	    ;; write bit length of string to last 4 bytes of the block.
+	    (aset block-low  15 (* (% len 8192) 8))
+	    (setq len (/ len 8192))
+	    (aset block-high 15 (% len 65536))
+	    ;; XXX: It is not practical to compute SHA1 of
+	    ;;      such a huge message on emacs.
+	    ;; (setq len (/ len 65536))		; for 64bit emacs.
+	    ;; (aset block-low  14 (% len 65536))
+	    ;; (aset block-high 14 (/ len 65536))
+	    (sha1-block block-high block-low))
+	  ;; make output string (in binary form).
+	  (let ((result (make-string 20 0)))
+	    (aset result  0 (/ sha1-H0-high 256))
+	    (aset result  1 (% sha1-H0-high 256))
+	    (aset result  2 (/ sha1-H0-low  256))
+	    (aset result  3 (% sha1-H0-low  256))
+	    (aset result  4 (/ sha1-H1-high 256))
+	    (aset result  5 (% sha1-H1-high 256))
+	    (aset result  6 (/ sha1-H1-low  256))
+	    (aset result  7 (% sha1-H1-low  256))
+	    (aset result  8 (/ sha1-H2-high 256))
+	    (aset result  9 (% sha1-H2-high 256))
+	    (aset result 10 (/ sha1-H2-low  256))
+	    (aset result 11 (% sha1-H2-low  256))
+	    (aset result 12 (/ sha1-H3-high 256))
+	    (aset result 13 (% sha1-H3-high 256))
+	    (aset result 14 (/ sha1-H3-low  256))
+	    (aset result 15 (% sha1-H3-low  256))
+	    (aset result 16 (/ sha1-H4-high 256))
+	    (aset result 17 (% sha1-H4-high 256))
+	    (aset result 18 (/ sha1-H4-low  256))
+	    (aset result 19 (% sha1-H4-low  256))
+	    result))
+      ;; do not leave a copy of input string.
+      (fillarray block-high nil)
+      (fillarray block-low  nil))))
+
+(defun sha1-string-internal (string &optional binary)
+  (if binary
+      (sha1-binary string)
+    (encode-hex-string (sha1-binary string))))
+
+(defun sha1-region-internal (beg end &optional binary)
+  (sha1-string-internal (buffer-substring-no-properties beg end) binary))
+
+;;;
+;;; application interface.
+;;;
+
+(defun sha1-region (beg end &optional binary)
+  (if (and sha1-use-external
+	   sha1-maximum-internal-length
+	   (> (abs (- end beg)) sha1-maximum-internal-length))
+      (sha1-region-external beg end binary)
+    (sha1-region-internal beg end binary)))
+
+(defun sha1-string (string &optional binary)
+  (if (and sha1-use-external
+	   sha1-maximum-internal-length
+	   (> (length string) sha1-maximum-internal-length))
+      (sha1-string-external string binary)
+    (sha1-string-internal string binary)))
+
+;;;###autoload
+(defun sha1 (object &optional beg end binary)
+  "Return the SHA1 (Secure Hash 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 BINARY is non-nil, return a string in binary form."
+  (if (stringp object)
+      (sha1-string object binary)
+    (save-excursion
+      (set-buffer object)
+      (sha1-region (or beg (point-min)) (or end (point-max)) binary))))
+
+(provide 'sha1-el)
+
+;;; sha1-el.el ends here