Add arch taglines
[gnus] / lisp / sha1-el.el
1 ;;; sha1-el.el --- SHA1 Secure Hash Algorithm in Emacs-Lisp
2
3 ;; Copyright (C) 1999, 2001, 2003, 2004 Free Software Foundation, Inc.
4
5 ;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
6 ;; Keywords: SHA1, FIPS 180-1
7
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or
13 ;; (at your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This program is implemented from the definition of SHA-1 in FIPS PUB
28 ;; 180-1 (Federal Information Processing Standards Publication 180-1),
29 ;; "Announcing the Standard for SECURE HASH STANDARD".
30 ;; <URL:http://www.itl.nist.gov/div897/pubs/fip180-1.htm>
31 ;; (EXCEPTION; two optimizations taken from GnuPG/cipher/sha1.c)
32 ;;
33 ;; Test cases from FIPS PUB 180-1.
34 ;;
35 ;; (sha1 "abc")
36 ;; => a9993e364706816aba3e25717850c26c9cd0d89d
37 ;;
38 ;; (sha1 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
39 ;; => 84983e441c3bd26ebaae4aa1f95129e5e54670f1
40 ;;
41 ;; (sha1 (make-string 1000000 ?a))
42 ;; => 34aa973cd4c4daa4f61eeb2bdbad27316534016f
43 ;;
44 ;; BUGS:
45 ;;  * It is assumed that length of input string is less than 2^29 bytes.
46 ;;  * It is caller's responsibility to make string (or region) unibyte.
47 ;;
48 ;; TODO:
49 ;;  * Rewrite from scratch!
50 ;;    This version is much faster than Keiichi Suzuki's another sha1.el,
51 ;;    but it is too dirty.
52
53 ;;; Code:
54
55 (require 'hex-util)
56
57 ;;;
58 ;;; external SHA1 function.
59 ;;;
60
61 (defgroup sha1 nil
62   "Elisp interface for SHA1 hash computation."
63   :group 'extensions)
64
65 (defcustom sha1-maximum-internal-length 500
66   "*Maximum length of message to use Lisp version of SHA1 function.
67 If message is longer than this, `sha1-program' is used instead.
68
69 If this variable is set to 0, use external program only.
70 If this variable is set to nil, use internal function only."
71   :type 'integer
72   :group 'sha1)
73
74 (defcustom sha1-program '("sha1sum")
75   "*Name of program to compute SHA1.
76 It must be a string \(program name\) or list of strings \(name and its args\)."
77   :type '(repeat string)
78   :group 'sha1)
79
80 (defcustom sha1-use-external (condition-case ()
81                                  (executable-find (car sha1-program))
82                                (error))
83   "*Use external SHA1 program.
84 If this variable is set to nil, use internal function only."
85   :type 'boolean
86   :group 'sha1)
87
88 (defun sha1-string-external (string &optional binary)
89   (let (prog args digest default-enable-multibyte-characters)
90     (if (consp sha1-program)
91         (setq prog (car sha1-program)
92               args (cdr sha1-program))
93       (setq prog sha1-program
94             args nil))
95     (with-temp-buffer
96       (insert string)
97       (apply (function call-process-region)
98              (point-min)(point-max)
99              prog t t nil args)
100       ;; SHA1 is 40 bytes long in hexadecimal form.
101       (setq digest (buffer-substring (point-min)(+ (point-min) 40))))
102     (if binary
103         (decode-hex-string digest)
104       digest)))
105
106 (defun sha1-region-external (beg end &optional binary)
107   (sha1-string-external (buffer-substring-no-properties beg end) binary))
108
109 ;;;
110 ;;; internal SHA1 function.
111 ;;;
112
113 (eval-when-compile
114   ;; optional second arg of string-to-number is new in v20.
115   (defconst sha1-K0-high 23170)         ; (string-to-number "5A82" 16)
116   (defconst sha1-K0-low  31129)         ; (string-to-number "7999" 16)
117   (defconst sha1-K1-high 28377)         ; (string-to-number "6ED9" 16)
118   (defconst sha1-K1-low  60321)         ; (string-to-number "EBA1" 16)
119   (defconst sha1-K2-high 36635)         ; (string-to-number "8F1B" 16)
120   (defconst sha1-K2-low  48348)         ; (string-to-number "BCDC" 16)
121   (defconst sha1-K3-high 51810)         ; (string-to-number "CA62" 16)
122   (defconst sha1-K3-low  49622)         ; (string-to-number "C1D6" 16)
123
124 ;;; original definition of sha1-F0.
125 ;;; (defmacro sha1-F0 (B C D)
126 ;;;   (` (logior (logand (, B) (, C))
127 ;;;          (logand (lognot (, B)) (, D)))))
128 ;;; a little optimization from GnuPG/cipher/sha1.c.
129   (defmacro sha1-F0 (B C D)
130     (` (logxor (, D) (logand (, B) (logxor (, C) (, D))))))
131   (defmacro sha1-F1 (B C D)
132     (` (logxor (, B) (, C) (, D))))
133 ;;; original definition of sha1-F2.
134 ;;; (defmacro sha1-F2 (B C D)
135 ;;;   (` (logior (logand (, B) (, C))
136 ;;;          (logand (, B) (, D))
137 ;;;          (logand (, C) (, D)))))
138 ;;; a little optimization from GnuPG/cipher/sha1.c.
139   (defmacro sha1-F2 (B C D)
140     (` (logior (logand (, B) (, C))
141                (logand (, D) (logior (, B) (, C))))))
142   (defmacro sha1-F3 (B C D)
143     (` (logxor (, B) (, C) (, D))))
144
145   (defmacro sha1-S1  (W-high W-low)
146     (` (let ((W-high (, W-high))
147              (W-low  (, W-low)))
148          (setq S1W-high (+ (% (* W-high 2) 65536)
149                            (/ W-low (, (/ 65536 2)))))
150          (setq S1W-low (+ (/ W-high (, (/ 65536 2)))
151                           (% (* W-low 2) 65536))))))
152   (defmacro sha1-S5  (A-high A-low)
153     (` (progn
154          (setq S5A-high (+ (% (* (, A-high) 32) 65536)
155                            (/ (, A-low) (, (/ 65536 32)))))
156          (setq S5A-low  (+ (/ (, A-high) (, (/ 65536 32)))
157                            (% (* (, A-low) 32) 65536))))))
158   (defmacro sha1-S30 (B-high B-low)
159     (` (progn
160          (setq S30B-high (+ (/ (, B-high) 4)
161                             (* (% (, B-low) 4) (, (/ 65536 4)))))
162          (setq S30B-low  (+ (/ (, B-low) 4)
163                             (* (% (, B-high) 4) (, (/ 65536 4))))))))
164
165   (defmacro sha1-OP (round)
166     (` (progn
167          (sha1-S5 sha1-A-high sha1-A-low)
168          (sha1-S30 sha1-B-high sha1-B-low)
169          (setq sha1-A-low (+ ((, (intern (format "sha1-F%d" round)))
170                               sha1-B-low sha1-C-low sha1-D-low)
171                              sha1-E-low
172                              (, (symbol-value
173                                  (intern (format "sha1-K%d-low" round))))
174                              (aref block-low idx)
175                              (progn
176                                (setq sha1-E-low sha1-D-low)
177                                (setq sha1-D-low sha1-C-low)
178                                (setq sha1-C-low S30B-low)
179                                (setq sha1-B-low sha1-A-low)
180                                S5A-low)))
181          (setq carry (/ sha1-A-low 65536))
182          (setq sha1-A-low (% sha1-A-low 65536))
183          (setq sha1-A-high (% (+ ((, (intern (format "sha1-F%d" round)))
184                                   sha1-B-high sha1-C-high sha1-D-high)
185                                  sha1-E-high
186                                  (, (symbol-value
187                                      (intern (format "sha1-K%d-high" round))))
188                                  (aref block-high idx)
189                                  (progn
190                                    (setq sha1-E-high sha1-D-high)
191                                    (setq sha1-D-high sha1-C-high)
192                                    (setq sha1-C-high S30B-high)
193                                    (setq sha1-B-high sha1-A-high)
194                                    S5A-high)
195                                  carry)
196                               65536)))))
197
198   (defmacro sha1-add-to-H (H X)
199     (` (progn
200          (setq (, (intern (format "sha1-%s-low" H)))
201                (+ (, (intern (format "sha1-%s-low" H)))
202                   (, (intern (format "sha1-%s-low" X)))))
203          (setq carry (/ (, (intern (format "sha1-%s-low" H))) 65536))
204          (setq (, (intern (format "sha1-%s-low" H)))
205                (% (, (intern (format "sha1-%s-low" H))) 65536))
206          (setq (, (intern (format "sha1-%s-high" H)))
207                (% (+ (, (intern (format "sha1-%s-high" H)))
208                      (, (intern (format "sha1-%s-high" X)))
209                      carry)
210                   65536)))))
211   )
212
213 ;;; buffers (H0 H1 H2 H3 H4).
214 (defvar sha1-H0-high)
215 (defvar sha1-H0-low)
216 (defvar sha1-H1-high)
217 (defvar sha1-H1-low)
218 (defvar sha1-H2-high)
219 (defvar sha1-H2-low)
220 (defvar sha1-H3-high)
221 (defvar sha1-H3-low)
222 (defvar sha1-H4-high)
223 (defvar sha1-H4-low)
224
225 (defun sha1-block (block-high block-low)
226   (let (;; step (c) --- initialize buffers (A B C D E).
227         (sha1-A-high sha1-H0-high) (sha1-A-low sha1-H0-low)
228         (sha1-B-high sha1-H1-high) (sha1-B-low sha1-H1-low)
229         (sha1-C-high sha1-H2-high) (sha1-C-low sha1-H2-low)
230         (sha1-D-high sha1-H3-high) (sha1-D-low sha1-H3-low)
231         (sha1-E-high sha1-H4-high) (sha1-E-low sha1-H4-low)
232         (idx 16))
233     ;; step (b).
234     (let (;; temporary variables used in sha1-S1 macro.
235           S1W-high S1W-low)
236       (while (< idx 80)
237         (sha1-S1 (logxor (aref block-high (- idx 3))
238                          (aref block-high (- idx 8))
239                          (aref block-high (- idx 14))
240                          (aref block-high (- idx 16)))
241                  (logxor (aref block-low  (- idx 3))
242                          (aref block-low  (- idx 8))
243                          (aref block-low  (- idx 14))
244                          (aref block-low  (- idx 16))))
245         (aset block-high idx S1W-high)
246         (aset block-low  idx S1W-low)
247         (setq idx (1+ idx))))
248     ;; step (d).
249     (setq idx 0)
250     (let (;; temporary variables used in sha1-OP macro.
251           S5A-high S5A-low S30B-high S30B-low carry)
252       (while (< idx 20) (sha1-OP 0) (setq idx (1+ idx)))
253       (while (< idx 40) (sha1-OP 1) (setq idx (1+ idx)))
254       (while (< idx 60) (sha1-OP 2) (setq idx (1+ idx)))
255       (while (< idx 80) (sha1-OP 3) (setq idx (1+ idx))))
256     ;; step (e).
257     (let (;; temporary variables used in sha1-add-to-H macro.
258           carry)
259       (sha1-add-to-H H0 A)
260       (sha1-add-to-H H1 B)
261       (sha1-add-to-H H2 C)
262       (sha1-add-to-H H3 D)
263       (sha1-add-to-H H4 E))))
264
265 (defun sha1-binary (string)
266   "Return the SHA1 of STRING in binary form."
267   (let (;; prepare buffers for a block. byte-length of block is 64.
268         ;; input block is split into two vectors.
269         ;;
270         ;; input block: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ...
271         ;; block-high:  +-0-+       +-1-+       +-2-+       +-3-+
272         ;; block-low:         +-0-+       +-1-+       +-2-+       +-3-+
273         ;;
274         ;; length of each vector is 80, and elements of each vector are
275         ;; 16bit integers.  elements 0x10-0x4F of each vector are
276         ;; assigned later in `sha1-block'.
277         (block-high (eval-when-compile (make-vector 80 nil)))
278         (block-low  (eval-when-compile (make-vector 80 nil))))
279     (unwind-protect
280         (let* (;; byte-length of input string.
281                (len (length string))
282                (lim (* (/ len 64) 64))
283                (rem (% len 4))
284                (idx 0)(pos 0))
285           ;; initialize buffers (H0 H1 H2 H3 H4).
286           (setq sha1-H0-high 26437      ; (string-to-number "6745" 16)
287                 sha1-H0-low  8961       ; (string-to-number "2301" 16)
288                 sha1-H1-high 61389      ; (string-to-number "EFCD" 16)
289                 sha1-H1-low  43913      ; (string-to-number "AB89" 16)
290                 sha1-H2-high 39098      ; (string-to-number "98BA" 16)
291                 sha1-H2-low  56574      ; (string-to-number "DCFE" 16)
292                 sha1-H3-high 4146       ; (string-to-number "1032" 16)
293                 sha1-H3-low  21622      ; (string-to-number "5476" 16)
294                 sha1-H4-high 50130      ; (string-to-number "C3D2" 16)
295                 sha1-H4-low  57840)     ; (string-to-number "E1F0" 16)
296           ;; loop for each 64 bytes block.
297           (while (< pos lim)
298             ;; step (a).
299             (setq idx 0)
300             (while (< idx 16)
301               (aset block-high idx (+ (* (aref string pos) 256)
302                                       (aref string (1+ pos))))
303               (setq pos (+ pos 2))
304               (aset block-low  idx (+ (* (aref string pos) 256)
305                                       (aref string (1+ pos))))
306               (setq pos (+ pos 2))
307               (setq idx (1+ idx)))
308             (sha1-block block-high block-low))
309           ;; last block.
310           (if (prog1
311                   (< (- len lim) 56)
312                 (setq lim (- len rem))
313                 (setq idx 0)
314                 (while (< pos lim)
315                   (aset block-high idx (+ (* (aref string pos) 256)
316                                           (aref string (1+ pos))))
317                   (setq pos (+ pos 2))
318                   (aset block-low  idx (+ (* (aref string pos) 256)
319                                           (aref string (1+ pos))))
320                   (setq pos (+ pos 2))
321                   (setq idx (1+ idx)))
322                 ;; this is the last (at most) 32bit word.
323                 (cond
324                  ((= rem 3)
325                   (aset block-high idx (+ (* (aref string pos) 256)
326                                           (aref string (1+ pos))))
327                   (setq pos (+ pos 2))
328                   (aset block-low  idx (+ (* (aref string pos) 256)
329                                           128)))
330                  ((= rem 2)
331                   (aset block-high idx (+ (* (aref string pos) 256)
332                                           (aref string (1+ pos))))
333                   (aset block-low  idx 32768))
334                  ((= rem 1)
335                   (aset block-high idx (+ (* (aref string pos) 256)
336                                           128))
337                   (aset block-low  idx 0))
338                  (t ;; (= rem 0)
339                   (aset block-high idx 32768)
340                   (aset block-low  idx 0)))
341                 (setq idx (1+ idx))
342                 (while (< idx 16)
343                   (aset block-high idx 0)
344                   (aset block-low  idx 0)
345                   (setq idx (1+ idx))))
346               ;; last block has enough room to write the length of string.
347               (progn
348                 ;; write bit length of string to last 4 bytes of the block.
349                 (aset block-low  15 (* (% len 8192) 8))
350                 (setq len (/ len 8192))
351                 (aset block-high 15 (% len 65536))
352                 ;; XXX: It is not practical to compute SHA1 of
353                 ;;      such a huge message on emacs.
354                 ;; (setq len (/ len 65536))     ; for 64bit emacs.
355                 ;; (aset block-low  14 (% len 65536))
356                 ;; (aset block-high 14 (/ len 65536))
357                 (sha1-block block-high block-low))
358             ;; need one more block.
359             (sha1-block block-high block-low)
360             (fillarray block-high 0)
361             (fillarray block-low  0)
362             ;; write bit length of string to last 4 bytes of the block.
363             (aset block-low  15 (* (% len 8192) 8))
364             (setq len (/ len 8192))
365             (aset block-high 15 (% len 65536))
366             ;; XXX: It is not practical to compute SHA1 of
367             ;;      such a huge message on emacs.
368             ;; (setq len (/ len 65536))         ; for 64bit emacs.
369             ;; (aset block-low  14 (% len 65536))
370             ;; (aset block-high 14 (/ len 65536))
371             (sha1-block block-high block-low))
372           ;; make output string (in binary form).
373           (let ((result (make-string 20 0)))
374             (aset result  0 (/ sha1-H0-high 256))
375             (aset result  1 (% sha1-H0-high 256))
376             (aset result  2 (/ sha1-H0-low  256))
377             (aset result  3 (% sha1-H0-low  256))
378             (aset result  4 (/ sha1-H1-high 256))
379             (aset result  5 (% sha1-H1-high 256))
380             (aset result  6 (/ sha1-H1-low  256))
381             (aset result  7 (% sha1-H1-low  256))
382             (aset result  8 (/ sha1-H2-high 256))
383             (aset result  9 (% sha1-H2-high 256))
384             (aset result 10 (/ sha1-H2-low  256))
385             (aset result 11 (% sha1-H2-low  256))
386             (aset result 12 (/ sha1-H3-high 256))
387             (aset result 13 (% sha1-H3-high 256))
388             (aset result 14 (/ sha1-H3-low  256))
389             (aset result 15 (% sha1-H3-low  256))
390             (aset result 16 (/ sha1-H4-high 256))
391             (aset result 17 (% sha1-H4-high 256))
392             (aset result 18 (/ sha1-H4-low  256))
393             (aset result 19 (% sha1-H4-low  256))
394             result))
395       ;; do not leave a copy of input string.
396       (fillarray block-high nil)
397       (fillarray block-low  nil))))
398
399 (defun sha1-string-internal (string &optional binary)
400   (if binary
401       (sha1-binary string)
402     (encode-hex-string (sha1-binary string))))
403
404 (defun sha1-region-internal (beg end &optional binary)
405   (sha1-string-internal (buffer-substring-no-properties beg end) binary))
406
407 ;;;
408 ;;; application interface.
409 ;;;
410
411 (defun sha1-region (beg end &optional binary)
412   (if (and sha1-use-external
413            sha1-maximum-internal-length
414            (> (abs (- end beg)) sha1-maximum-internal-length))
415       (sha1-region-external beg end binary)
416     (sha1-region-internal beg end binary)))
417
418 (defun sha1-string (string &optional binary)
419   (if (and sha1-use-external
420            sha1-maximum-internal-length
421            (> (length string) sha1-maximum-internal-length))
422       (sha1-string-external string binary)
423     (sha1-string-internal string binary)))
424
425 ;;;###autoload
426 (defun sha1 (object &optional beg end binary)
427   "Return the SHA1 (Secure Hash Algorithm) of an object.
428 OBJECT is either a string or a buffer.
429 Optional arguments BEG and END denote buffer positions for computing the
430 hash of a portion of OBJECT.
431 If BINARY is non-nil, return a string in binary form."
432   (if (stringp object)
433       (sha1-string object binary)
434     (save-excursion
435       (set-buffer object)
436       (sha1-region (or beg (point-min)) (or end (point-max)) binary))))
437
438 (provide 'sha1-el)
439
440 ;;; arch-tag: c0f9abd0-ffc1-4557-aac6-ece7f2d4c901
441 ;;; sha1-el.el ends here