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