md4.el 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. ;;; md4.el --- MD4 Message Digest Algorithm.
  2. ;; Copyright (C) 2001, 2004, 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: Taro Kawagishi <tarok@transpulse.org>
  4. ;; Keywords: MD4
  5. ;; Version: 1.00
  6. ;; Created: February 2001
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Code:
  19. ;;;
  20. ;;; MD4 hash calculation
  21. (defvar md4-buffer (make-vector 4 '(0 . 0))
  22. "Work buffer of four 32-bit integers.")
  23. (defun md4 (in n)
  24. "Return the MD4 hash for a string IN of length N bytes.
  25. The returned hash is 16 bytes long. N is required to handle
  26. strings containing the character 0."
  27. (let (m
  28. (b (cons 0 (* n 8)))
  29. (i 0)
  30. (buf (make-string 128 0)) c4)
  31. ;; initial values
  32. (aset md4-buffer 0 '(26437 . 8961)) ;0x67452301
  33. (aset md4-buffer 1 '(61389 . 43913)) ;0xefcdab89
  34. (aset md4-buffer 2 '(39098 . 56574)) ;0x98badcfe
  35. (aset md4-buffer 3 '(4146 . 21622)) ;0x10325476
  36. ;; process the string in 64 bits chunks
  37. (while (> n 64)
  38. (setq m (md4-copy64 (substring in 0 64)))
  39. (md4-64 m)
  40. (setq in (substring in 64))
  41. (setq n (- n 64)))
  42. ;; process the rest of the string (length is now n <= 64)
  43. (setq i 0)
  44. (while (< i n)
  45. (aset buf i (aref in i))
  46. (setq i (1+ i)))
  47. (aset buf n 128) ;0x80
  48. (if (<= n 55)
  49. (progn
  50. (setq c4 (md4-pack-int32 b))
  51. (aset buf 56 (aref c4 0))
  52. (aset buf 57 (aref c4 1))
  53. (aset buf 58 (aref c4 2))
  54. (aset buf 59 (aref c4 3))
  55. (setq m (md4-copy64 buf))
  56. (md4-64 m))
  57. ;; else
  58. (setq c4 (md4-pack-int32 b))
  59. (aset buf 120 (aref c4 0))
  60. (aset buf 121 (aref c4 1))
  61. (aset buf 122 (aref c4 2))
  62. (aset buf 123 (aref c4 3))
  63. (setq m (md4-copy64 buf))
  64. (md4-64 m)
  65. (setq m (md4-copy64 (substring buf 64)))
  66. (md4-64 m)))
  67. (concat (md4-pack-int32 (aref md4-buffer 0))
  68. (md4-pack-int32 (aref md4-buffer 1))
  69. (md4-pack-int32 (aref md4-buffer 2))
  70. (md4-pack-int32 (aref md4-buffer 3))))
  71. (defsubst md4-F (x y z) (logior (logand x y) (logand (lognot x) z)))
  72. (defsubst md4-G (x y z) (logior (logand x y) (logand x z) (logand y z)))
  73. (defsubst md4-H (x y z) (logxor x y z))
  74. (defmacro md4-make-step (name func)
  75. `(defun ,name (a b c d xk s ac)
  76. (let*
  77. ((h1 (+ (car a) (,func (car b) (car c) (car d)) (car xk) (car ac)))
  78. (l1 (+ (cdr a) (,func (cdr b) (cdr c) (cdr d)) (cdr xk) (cdr ac)))
  79. (h2 (logand 65535 (+ h1 (lsh l1 -16))))
  80. (l2 (logand 65535 l1))
  81. ;; cyclic shift of 32 bits integer
  82. (h3 (logand 65535 (if (> s 15)
  83. (+ (lsh h2 (- s 32)) (lsh l2 (- s 16)))
  84. (+ (lsh h2 s) (lsh l2 (- s 16))))))
  85. (l3 (logand 65535 (if (> s 15)
  86. (+ (lsh l2 (- s 32)) (lsh h2 (- s 16)))
  87. (+ (lsh l2 s) (lsh h2 (- s 16)))))))
  88. (cons h3 l3))))
  89. (md4-make-step md4-round1 md4-F)
  90. (md4-make-step md4-round2 md4-G)
  91. (md4-make-step md4-round3 md4-H)
  92. (defsubst md4-add (x y)
  93. "Return 32-bit sum of 32-bit integers X and Y."
  94. (let ((h (+ (car x) (car y)))
  95. (l (+ (cdr x) (cdr y))))
  96. (cons (logand 65535 (+ h (lsh l -16))) (logand 65535 l))))
  97. (defsubst md4-and (x y)
  98. (cons (logand (car x) (car y)) (logand (cdr x) (cdr y))))
  99. (defun md4-64 (m)
  100. "Calculate MD4 hash of M.
  101. M is a 64-bytes chunk, represented as 16 pairs of 32-bit integers.
  102. The resulting MD4 value is placed in `md4-buffer'."
  103. (let ((a (aref md4-buffer 0))
  104. (b (aref md4-buffer 1))
  105. (c (aref md4-buffer 2))
  106. (d (aref md4-buffer 3)))
  107. (setq a (md4-round1 a b c d (aref m 0) 3 '(0 . 0))
  108. d (md4-round1 d a b c (aref m 1) 7 '(0 . 0))
  109. c (md4-round1 c d a b (aref m 2) 11 '(0 . 0))
  110. b (md4-round1 b c d a (aref m 3) 19 '(0 . 0))
  111. a (md4-round1 a b c d (aref m 4) 3 '(0 . 0))
  112. d (md4-round1 d a b c (aref m 5) 7 '(0 . 0))
  113. c (md4-round1 c d a b (aref m 6) 11 '(0 . 0))
  114. b (md4-round1 b c d a (aref m 7) 19 '(0 . 0))
  115. a (md4-round1 a b c d (aref m 8) 3 '(0 . 0))
  116. d (md4-round1 d a b c (aref m 9) 7 '(0 . 0))
  117. c (md4-round1 c d a b (aref m 10) 11 '(0 . 0))
  118. b (md4-round1 b c d a (aref m 11) 19 '(0 . 0))
  119. a (md4-round1 a b c d (aref m 12) 3 '(0 . 0))
  120. d (md4-round1 d a b c (aref m 13) 7 '(0 . 0))
  121. c (md4-round1 c d a b (aref m 14) 11 '(0 . 0))
  122. b (md4-round1 b c d a (aref m 15) 19 '(0 . 0))
  123. a (md4-round2 a b c d (aref m 0) 3 '(23170 . 31129)) ;0x5A827999
  124. d (md4-round2 d a b c (aref m 4) 5 '(23170 . 31129))
  125. c (md4-round2 c d a b (aref m 8) 9 '(23170 . 31129))
  126. b (md4-round2 b c d a (aref m 12) 13 '(23170 . 31129))
  127. a (md4-round2 a b c d (aref m 1) 3 '(23170 . 31129))
  128. d (md4-round2 d a b c (aref m 5) 5 '(23170 . 31129))
  129. c (md4-round2 c d a b (aref m 9) 9 '(23170 . 31129))
  130. b (md4-round2 b c d a (aref m 13) 13 '(23170 . 31129))
  131. a (md4-round2 a b c d (aref m 2) 3 '(23170 . 31129))
  132. d (md4-round2 d a b c (aref m 6) 5 '(23170 . 31129))
  133. c (md4-round2 c d a b (aref m 10) 9 '(23170 . 31129))
  134. b (md4-round2 b c d a (aref m 14) 13 '(23170 . 31129))
  135. a (md4-round2 a b c d (aref m 3) 3 '(23170 . 31129))
  136. d (md4-round2 d a b c (aref m 7) 5 '(23170 . 31129))
  137. c (md4-round2 c d a b (aref m 11) 9 '(23170 . 31129))
  138. b (md4-round2 b c d a (aref m 15) 13 '(23170 . 31129))
  139. a (md4-round3 a b c d (aref m 0) 3 '(28377 . 60321)) ;0x6ED9EBA1
  140. d (md4-round3 d a b c (aref m 8) 9 '(28377 . 60321))
  141. c (md4-round3 c d a b (aref m 4) 11 '(28377 . 60321))
  142. b (md4-round3 b c d a (aref m 12) 15 '(28377 . 60321))
  143. a (md4-round3 a b c d (aref m 2) 3 '(28377 . 60321))
  144. d (md4-round3 d a b c (aref m 10) 9 '(28377 . 60321))
  145. c (md4-round3 c d a b (aref m 6) 11 '(28377 . 60321))
  146. b (md4-round3 b c d a (aref m 14) 15 '(28377 . 60321))
  147. a (md4-round3 a b c d (aref m 1) 3 '(28377 . 60321))
  148. d (md4-round3 d a b c (aref m 9) 9 '(28377 . 60321))
  149. c (md4-round3 c d a b (aref m 5) 11 '(28377 . 60321))
  150. b (md4-round3 b c d a (aref m 13) 15 '(28377 . 60321))
  151. a (md4-round3 a b c d (aref m 3) 3 '(28377 . 60321))
  152. d (md4-round3 d a b c (aref m 11) 9 '(28377 . 60321))
  153. c (md4-round3 c d a b (aref m 7) 11 '(28377 . 60321))
  154. b (md4-round3 b c d a (aref m 15) 15 '(28377 . 60321)))
  155. (aset md4-buffer 0 (md4-add a (aref md4-buffer 0)))
  156. (aset md4-buffer 1 (md4-add b (aref md4-buffer 1)))
  157. (aset md4-buffer 2 (md4-add c (aref md4-buffer 2)))
  158. (aset md4-buffer 3 (md4-add d (aref md4-buffer 3)))
  159. ))
  160. (defun md4-copy64 (seq)
  161. "Unpack a 64 bytes string into 16 pairs of 32 bits integers."
  162. (let ((int32s (make-vector 16 0)) (i 0) j)
  163. (while (< i 16)
  164. (setq j (* i 4))
  165. (aset int32s i (cons (+ (aref seq (+ j 2)) (lsh (aref seq (+ j 3)) 8))
  166. (+ (aref seq j) (lsh (aref seq (1+ j)) 8))))
  167. (setq i (1+ i)))
  168. int32s))
  169. ;;;
  170. ;;; sub functions
  171. (defun md4-pack-int16 (int16)
  172. "Pack 16 bits integer in 2 bytes string as little endian."
  173. (let ((str (make-string 2 0)))
  174. (aset str 0 (logand int16 255))
  175. (aset str 1 (lsh int16 -8))
  176. str))
  177. (defun md4-pack-int32 (int32)
  178. "Pack 32 bits integer in a 4 bytes string as little endian.
  179. A 32 bits integer is represented as a pair of two 16 bits
  180. integers (cons high low)."
  181. (let ((str (make-string 4 0))
  182. (h (car int32)) (l (cdr int32)))
  183. (aset str 0 (logand l 255))
  184. (aset str 1 (lsh l -8))
  185. (aset str 2 (logand h 255))
  186. (aset str 3 (lsh h -8))
  187. str))
  188. (defun md4-unpack-int16 (str)
  189. (if (eq 2 (length str))
  190. (+ (lsh (aref str 1) 8) (aref str 0))
  191. (error "%s is not 2 bytes long" str)))
  192. (defun md4-unpack-int32 (str)
  193. (if (eq 4 (length str))
  194. (cons (+ (lsh (aref str 3) 8) (aref str 2))
  195. (+ (lsh (aref str 1) 8) (aref str 0)))
  196. (error "%s is not 4 bytes long" str)))
  197. (provide 'md4)
  198. ;;; md4.el ends here