Merge `deisui-1_14_0-1'.
[elisp/flim.git] / sha1-el.el
1 ;;; sha1-el.el --- SHA1 Secure Hash Algorithm in Emacs-Lisp.
2
3 ;; Copyright (C) 1999 Shuhei KOBAYASHI
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:
32 ;;  * Two optimizations taken from GnuPG/cipher/sha1.c.
33 ;;
34 ;; BUGS:
35 ;;  * It is assumed that length of input string is less than 2^29 bytes.
36 ;;  * It is caller's responsibility to make string (or region) unibyte.
37
38 ;;; Code:
39
40 (require 'hex-util)
41
42 ;;;
43 ;;; external SHA1 function.
44 ;;;
45
46 (defvar sha1-maximum-internal-length 500
47   "*Maximum length of message to use lisp version of SHA1 function.
48 If message is longer than this, `sha1-program' is used instead.
49
50 If this variable is set to 0, use extarnal program only.
51 If this variable is set to nil, use internal function only.")
52
53 (defvar sha1-program '("openssl" "sha1")
54   "*Name of program to compute SHA1.
55 It must be a string \(program name\) or list of strings \(name and its args\).")
56
57 (defun sha1-string-external (string)
58   ;; `with-temp-buffer' is new in v20, so we do not use it.
59   (save-excursion
60     (let (buffer)
61       (unwind-protect
62           (let (prog args)
63             (if (consp sha1-program)
64                 (setq prog (car sha1-program)
65                       args (cdr sha1-program))
66               (setq prog sha1-program
67                     args nil))
68             (setq buffer (set-buffer
69                           (generate-new-buffer " *sha1 external*")))
70             (insert string)
71             (apply (function call-process-region)
72                    (point-min)(point-max)
73                    prog t t nil args)
74             ;; SHA1 is 40 bytes long in hexadecimal form.
75             (buffer-substring (point-min)(+ (point-min) 40)))
76         (and buffer
77              (buffer-name buffer)
78              (kill-buffer buffer))))))
79
80 (defun sha1-region-external (beg end)
81   (sha1-string-external (buffer-substring-no-properties beg end)))
82
83 ;;;
84 ;;; internal SHA1 function.
85 ;;;
86
87 (eval-when-compile
88   ;; optional second arg of string-to-number is new in v20.
89   (defconst sha1-K0-high 23170)         ; (string-to-number "5A82" 16)
90   (defconst sha1-K0-low  31129)         ; (string-to-number "7999" 16)
91   (defconst sha1-K1-high 28377)         ; (string-to-number "6ED9" 16)
92   (defconst sha1-K1-low  60321)         ; (string-to-number "EBA1" 16)
93   (defconst sha1-K2-high 36635)         ; (string-to-number "8F1B" 16)
94   (defconst sha1-K2-low  48348)         ; (string-to-number "BCDC" 16)
95   (defconst sha1-K3-high 51810)         ; (string-to-number "CA62" 16)
96   (defconst sha1-K3-low  49622)         ; (string-to-number "C1D6" 16)
97
98 ;;; original definition of sha1-F0.
99 ;;; (defmacro sha1-F0 (B C D)
100 ;;;   (` (logior (logand (, B) (, C))
101 ;;;          (logand (lognot (, B)) (, D)))))
102 ;;; a little optimization from GnuPG/cipher/sha1.c.
103   (defmacro sha1-F0 (B C D)
104     (` (logxor (, D) (logand (, B) (logxor (, C) (, D))))))
105   (defmacro sha1-F1 (B C D)
106     (` (logxor (, B) (, C) (, D))))
107 ;;; original definition of sha1-F2.
108 ;;; (defmacro sha1-F2 (B C D)
109 ;;;   (` (logior (logand (, B) (, C))
110 ;;;          (logand (, B) (, D))
111 ;;;          (logand (, C) (, D)))))
112 ;;; a little optimization from GnuPG/cipher/sha1.c.
113   (defmacro sha1-F2 (B C D)
114     (` (logior (logand (, B) (, C))
115                (logand (, D) (logior (, B) (, C))))))
116   (defmacro sha1-F3 (B C D)
117     (` (logxor (, B) (, C) (, D))))
118
119   (defmacro sha1-S1  (W-high W-low)
120     (` (let ((W-high (, W-high))
121              (W-low  (, W-low)))
122          (setq S1W-high (+ (% (* W-high 2) 65536)
123                            (/ W-low (, (/ 65536 2)))))
124          (setq S1W-low (+ (/ W-high (, (/ 65536 2)))
125                           (% (* W-low 2) 65536))))))
126   (defmacro sha1-S5  (A-high A-low)
127     (` (progn
128          (setq S5A-high (+ (% (* (, A-high) 32) 65536)
129                            (/ (, A-low) (, (/ 65536 32)))))
130          (setq S5A-low  (+ (/ (, A-high) (, (/ 65536 32)))
131                            (% (* (, A-low) 32) 65536))))))
132   (defmacro sha1-S30 (B-high B-low)
133     (` (progn
134          (setq S30B-high (+ (/ (, B-high) 4)
135                             (* (% (, B-low) 4) (, (/ 65536 4)))))
136          (setq S30B-low  (+ (/ (, B-low) 4)
137                             (* (% (, B-high) 4) (, (/ 65536 4))))))))
138
139   (defmacro sha1-OP (round)
140     (` (progn
141          (sha1-S5 sha1-A-high sha1-A-low)
142          (sha1-S30 sha1-B-high sha1-B-low)
143          (setq sha1-A-low (+ ((, (intern (format "sha1-F%d" round)))
144                               sha1-B-low sha1-C-low sha1-D-low)
145                              sha1-E-low
146                              (, (symbol-value
147                                  (intern (format "sha1-K%d-low" round))))
148                              (aref block-low idx)
149                              (progn
150                                (setq sha1-E-low sha1-D-low)
151                                (setq sha1-D-low sha1-C-low)
152                                (setq sha1-C-low S30B-low)
153                                (setq sha1-B-low sha1-A-low)
154                                S5A-low)))
155          (setq carry (/ sha1-A-low 65536))
156          (setq sha1-A-low (% sha1-A-low 65536))
157          (setq sha1-A-high (% (+ ((, (intern (format "sha1-F%d" round)))
158                                   sha1-B-high sha1-C-high sha1-D-high)
159                                  sha1-E-high
160                                  (, (symbol-value
161                                      (intern (format "sha1-K%d-high" round))))
162                                  (aref block-high idx)
163                                  (progn
164                                    (setq sha1-E-high sha1-D-high)
165                                    (setq sha1-D-high sha1-C-high)
166                                    (setq sha1-C-high S30B-high)
167                                    (setq sha1-B-high sha1-A-high)
168                                    S5A-high)
169                                  carry)
170                               65536)))))
171
172   (defmacro sha1-add-to-H (H X)
173     (` (progn
174          (setq (, (intern (format "sha1-%s-low" H)))
175                (+ (, (intern (format "sha1-%s-low" H)))
176                   (, (intern (format "sha1-%s-low" X)))))
177          (setq carry (/ (, (intern (format "sha1-%s-low" H))) 65536))
178          (setq (, (intern (format "sha1-%s-low" H)))
179                (% (, (intern (format "sha1-%s-low" H))) 65536))
180          (setq (, (intern (format "sha1-%s-high" H)))
181                (% (+ (, (intern (format "sha1-%s-high" H)))
182                      (, (intern (format "sha1-%s-high" X)))
183                      carry)
184                   65536)))))
185   )
186
187 ;;; buffers (H0 H1 H2 H3 H4).
188 (defvar sha1-H0-high)
189 (defvar sha1-H0-low)
190 (defvar sha1-H1-high)
191 (defvar sha1-H1-low)
192 (defvar sha1-H2-high)
193 (defvar sha1-H2-low)
194 (defvar sha1-H3-high)
195 (defvar sha1-H3-low)
196 (defvar sha1-H4-high)
197 (defvar sha1-H4-low)
198
199 (defun sha1-block (block-high block-low)
200   (let (;; step (c) --- initialize buffers (A B C D E).
201         (sha1-A-high sha1-H0-high) (sha1-A-low sha1-H0-low)
202         (sha1-B-high sha1-H1-high) (sha1-B-low sha1-H1-low)
203         (sha1-C-high sha1-H2-high) (sha1-C-low sha1-H2-low)
204         (sha1-D-high sha1-H3-high) (sha1-D-low sha1-H3-low)
205         (sha1-E-high sha1-H4-high) (sha1-E-low sha1-H4-low)
206         (idx 16))
207     ;; step (b).
208     (let (;; temporary variables used in sha1-S1 macro.
209           S1W-high S1W-low)
210       (while (< idx 80)
211         (sha1-S1 (logxor (aref block-high (- idx 3))
212                          (aref block-high (- idx 8))
213                          (aref block-high (- idx 14))
214                          (aref block-high (- idx 16)))
215                  (logxor (aref block-low  (- idx 3))
216                          (aref block-low  (- idx 8))
217                          (aref block-low  (- idx 14))
218                          (aref block-low  (- idx 16))))
219         (aset block-high idx S1W-high)
220         (aset block-low  idx S1W-low)
221         (setq idx (1+ idx))))
222     ;; step (d).
223     (setq idx 0)
224     (let (;; temporary variables used in sha1-OP macro.
225           S5A-high S5A-low S30B-high S30B-low carry)
226       (while (< idx 20) (sha1-OP 0) (setq idx (1+ idx)))
227       (while (< idx 40) (sha1-OP 1) (setq idx (1+ idx)))
228       (while (< idx 60) (sha1-OP 2) (setq idx (1+ idx)))
229       (while (< idx 80) (sha1-OP 3) (setq idx (1+ idx))))
230     ;; step (e).
231     (let (;; temporary variables used in sha1-add-to-H macro.
232           carry)
233       (sha1-add-to-H H0 A)
234       (sha1-add-to-H H1 B)
235       (sha1-add-to-H H2 C)
236       (sha1-add-to-H H3 D)
237       (sha1-add-to-H H4 E))))
238
239 (defun sha1-binary (string)
240   "Return the SHA1 of STRING in binary form."
241   (let (;; prepare buffers for a block. byte-length of block is 64.
242         ;; input block is split into two vectors.
243         ;;
244         ;; input block: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ...
245         ;; block-high:  +-0-+       +-1-+       +-2-+       +-3-+
246         ;; block-low:         +-0-+       +-1-+       +-2-+       +-3-+
247         ;;
248         ;; length of each vector is 80, and elements of each vector are
249         ;; 16bit integers.  elements 0x10-0x4F of each vector are
250         ;; assigned later in `sha1-block'.
251         (block-high (eval-when-compile (make-vector 80 nil)))
252         (block-low  (eval-when-compile (make-vector 80 nil))))
253     (unwind-protect
254         (let* (;; byte-length of input string.
255                (len (length string))
256                (lim (* (/ len 64) 64))
257                (rem (% len 4))
258                (idx 0)(pos 0))
259           ;; initialize buffers (H0 H1 H2 H3 H4).
260           (setq sha1-H0-high 26437      ; (string-to-number "6745" 16)
261                 sha1-H0-low  8961       ; (string-to-number "2301" 16)
262                 sha1-H1-high 61389      ; (string-to-number "EFCD" 16)
263                 sha1-H1-low  43913      ; (string-to-number "AB89" 16)
264                 sha1-H2-high 39098      ; (string-to-number "98BA" 16)
265                 sha1-H2-low  56574      ; (string-to-number "DCFE" 16)
266                 sha1-H3-high 4146       ; (string-to-number "1032" 16)
267                 sha1-H3-low  21622      ; (string-to-number "5476" 16)
268                 sha1-H4-high 50130      ; (string-to-number "C3D2" 16)
269                 sha1-H4-low  57840)     ; (string-to-number "E1F0" 16)
270           ;; loop for each 64 bytes block.
271           (while (< pos lim)
272             ;; step (a).
273             (setq idx 0)
274             (while (< idx 16)
275               (aset block-high idx (+ (* (aref string pos) 256)
276                                       (aref string (1+ pos))))
277               (setq pos (+ pos 2))
278               (aset block-low  idx (+ (* (aref string pos) 256)
279                                       (aref string (1+ pos))))
280               (setq pos (+ pos 2))
281               (setq idx (1+ idx)))
282             (sha1-block block-high block-low))
283           ;; last block.
284           (if (prog1
285                   (< (- len lim) 56)
286                 (setq lim (- len rem))
287                 (setq idx 0)
288                 (while (< pos lim)
289                   (aset block-high idx (+ (* (aref string pos) 256)
290                                           (aref string (1+ pos))))
291                   (setq pos (+ pos 2))
292                   (aset block-low  idx (+ (* (aref string pos) 256)
293                                           (aref string (1+ pos))))
294                   (setq pos (+ pos 2))
295                   (setq idx (1+ idx)))
296                 ;; this is the last (at most) 32bit word.
297                 (cond
298                  ((= rem 3)
299                   (aset block-high idx (+ (* (aref string pos) 256)
300                                           (aref string (1+ pos))))
301                   (setq pos (+ pos 2))
302                   (aset block-low  idx (+ (* (aref string pos) 256)
303                                           128)))
304                  ((= rem 2)
305                   (aset block-high idx (+ (* (aref string pos) 256)
306                                           (aref string (1+ pos))))
307                   (aset block-low  idx 32768))
308                  ((= rem 1)
309                   (aset block-high idx (+ (* (aref string pos) 256)
310                                           128))
311                   (aset block-low  idx 0))
312                  (t ;; (= rem 0)
313                   (aset block-high idx 32768)
314                   (aset block-low  idx 0)))
315                 (setq idx (1+ idx))
316                 (while (< idx 16)
317                   (aset block-high idx 0)
318                   (aset block-low  idx 0)
319                   (setq idx (1+ idx))))
320               ;; last block has enough room to write the length of string.
321               (progn
322                 ;; write bit length of string to last 4 bytes of the block.
323                 (aset block-low  15 (* (% len 8192) 8))
324                 (setq len (/ len 8192))
325                 (aset block-high 15 (% len 65536))
326                 ;; XXX: It is not practical to compute SHA1 of
327                 ;;      such a huge message on emacs.
328                 ;; (setq len (/ len 65536))     ; for 64bit emacs.
329                 ;; (aset block-low  14 (% len 65536))
330                 ;; (aset block-high 14 (/ len 65536))
331                 (sha1-block block-high block-low))
332             ;; need one more block.
333             (sha1-block block-high block-low)
334             (fillarray block-high 0)
335             (fillarray block-low  0)
336             ;; write bit length of string to last 4 bytes of the block.
337             (aset block-low  15 (* (% len 8192) 8))
338             (setq len (/ len 8192))
339             (aset block-high 15 (% len 65536))
340             ;; XXX: It is not practical to compute SHA1 of
341             ;;      such a huge message on emacs.
342             ;; (setq len (/ len 65536))         ; for 64bit emacs.
343             ;; (aset block-low  14 (% len 65536))
344             ;; (aset block-high 14 (/ len 65536))
345             (sha1-block block-high block-low))
346           ;; make output string (in binary form).
347           (let ((result (make-string 20 0)))
348             (aset result  0 (/ sha1-H0-high 256))
349             (aset result  1 (% sha1-H0-high 256))
350             (aset result  2 (/ sha1-H0-low  256))
351             (aset result  3 (% sha1-H0-low  256))
352             (aset result  4 (/ sha1-H1-high 256))
353             (aset result  5 (% sha1-H1-high 256))
354             (aset result  6 (/ sha1-H1-low  256))
355             (aset result  7 (% sha1-H1-low  256))
356             (aset result  8 (/ sha1-H2-high 256))
357             (aset result  9 (% sha1-H2-high 256))
358             (aset result 10 (/ sha1-H2-low  256))
359             (aset result 11 (% sha1-H2-low  256))
360             (aset result 12 (/ sha1-H3-high 256))
361             (aset result 13 (% sha1-H3-high 256))
362             (aset result 14 (/ sha1-H3-low  256))
363             (aset result 15 (% sha1-H3-low  256))
364             (aset result 16 (/ sha1-H4-high 256))
365             (aset result 17 (% sha1-H4-high 256))
366             (aset result 18 (/ sha1-H4-low  256))
367             (aset result 19 (% sha1-H4-low  256))
368             result))
369       ;; do not leave a copy of input string.
370       (fillarray block-high nil)
371       (fillarray block-low  nil))))
372
373 (defun sha1-string-internal (string)
374   (encode-hex-string (sha1-binary string)))
375
376 (defun sha1-region-internal (beg end)
377   (sha1-string-internal (buffer-substring-no-properties beg end)))
378
379 ;;;
380 ;;; application interface.
381 ;;;
382
383 (defun sha1-region (beg end)
384   (if (and sha1-maximum-internal-length
385            (> (abs (- end beg)) sha1-maximum-internal-length))
386       (sha1-region-external beg end)
387     (sha1-region-internal beg end)))
388
389 (defun sha1-string (string)
390   (if (and sha1-maximum-internal-length
391            (> (length string) sha1-maximum-internal-length))
392       (sha1-string-external string)
393     (sha1-string-internal string)))
394
395 (defun sha1 (object &optional beg end)
396   "Return the SHA1 (Secure Hash Algorithm) of an object.
397 OBJECT is either a string or a buffer.
398 Optional arguments BEG and END denote buffer positions for computing the
399 hash of a portion of OBJECT."
400   (if (stringp object)
401       (sha1-string object)
402     (save-excursion
403       (set-buffer object)
404       (sha1-region (or beg (point-min)) (or end (point-max))))))
405
406 (provide 'sha1-el)
407
408 ;;; sha1-el.el ends here