ntlm.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. ;;; ntlm.el --- NTLM (NT LanManager) authentication support
  2. ;; Copyright (C) 2001, 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: Taro Kawagishi <tarok@transpulse.org>
  4. ;; Keywords: NTLM, SASL
  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. ;;; Commentary:
  19. ;; This library is a direct translation of the Samba release 2.2.0
  20. ;; implementation of Windows NT and LanManager compatible password
  21. ;; encryption.
  22. ;;
  23. ;; Interface functions:
  24. ;;
  25. ;; ntlm-build-auth-request
  26. ;; This will return a binary string, which should be used in the
  27. ;; base64 encoded form and it is the caller's responsibility to encode
  28. ;; the returned string with base64.
  29. ;;
  30. ;; ntlm-build-auth-response
  31. ;; It is the caller's responsibility to pass a base64 decoded string
  32. ;; (which will be a binary string) as the first argument and to
  33. ;; encode the returned string with base64. The second argument user
  34. ;; should be given in user@domain format.
  35. ;;
  36. ;; ntlm-get-password-hashes
  37. ;;
  38. ;;
  39. ;; NTLM authentication procedure example:
  40. ;;
  41. ;; 1. Open a network connection to the Exchange server at the IMAP port (143)
  42. ;; 2. Receive an opening message such as:
  43. ;; "* OK Microsoft Exchange IMAP4rev1 server version 5.5.2653.7 (XXXX) ready"
  44. ;; 3. Ask for IMAP server capability by sending "NNN capability"
  45. ;; 4. Receive a capability message such as:
  46. ;; "* CAPABILITY IMAP4 IMAP4rev1 IDLE LITERAL+ LOGIN-REFERRALS MAILBOX-REFERRALS NAMESPACE AUTH=NTLM"
  47. ;; 5. Ask for NTLM authentication by sending a string
  48. ;; "NNN authenticate ntlm"
  49. ;; 6. Receive continuation acknowledgment "+"
  50. ;; 7. Send NTLM authentication request generated by 'ntlm-build-auth-request
  51. ;; 8. Receive NTLM challenge string following acknowledgment "+"
  52. ;; 9. Generate response to challenge by 'ntlm-build-auth-response
  53. ;; (here two hash function values of the user password are encrypted)
  54. ;; 10. Receive authentication completion message such as
  55. ;; "NNN OK AUTHENTICATE NTLM completed."
  56. ;;; Code:
  57. (require 'md4)
  58. ;;;
  59. ;;; NTLM authentication interface functions
  60. (defun ntlm-build-auth-request (user &optional domain)
  61. "Return the NTLM authentication request string for USER and DOMAIN.
  62. USER is a string representing a user name to be authenticated and
  63. DOMAIN is a NT domain. USER can include a NT domain part as in
  64. user@domain where the string after @ is used as the domain if DOMAIN
  65. is not given."
  66. (interactive)
  67. (let ((request-ident (concat "NTLMSSP" (make-string 1 0)))
  68. (request-msgType (concat (make-string 1 1) (make-string 3 0)))
  69. ;0x01 0x00 0x00 0x00
  70. (request-flags (concat (make-string 1 7) (make-string 1 178)
  71. (make-string 2 0)))
  72. ;0x07 0xb2 0x00 0x00
  73. lu ld off-d off-u)
  74. (when (string-match "@" user)
  75. (unless domain
  76. (setq domain (substring user (1+ (match-beginning 0)))))
  77. (setq user (substring user 0 (match-beginning 0))))
  78. ;; set fields offsets within the request struct
  79. (setq lu (length user))
  80. (setq ld (length domain))
  81. (setq off-u 32) ;offset to the string 'user
  82. (setq off-d (+ 32 lu)) ;offset to the string 'domain
  83. ;; pack the request struct in a string
  84. (concat request-ident ;8 bytes
  85. request-msgType ;4 bytes
  86. request-flags ;4 bytes
  87. (md4-pack-int16 lu) ;user field, count field
  88. (md4-pack-int16 lu) ;user field, max count field
  89. (md4-pack-int32 (cons 0 off-u)) ;user field, offset field
  90. (md4-pack-int16 ld) ;domain field, count field
  91. (md4-pack-int16 ld) ;domain field, max count field
  92. (md4-pack-int32 (cons 0 off-d)) ;domain field, offset field
  93. user ;buffer field
  94. domain ;buffer field
  95. )))
  96. (eval-when-compile
  97. (defmacro ntlm-string-as-unibyte (string)
  98. (if (fboundp 'string-as-unibyte)
  99. `(string-as-unibyte ,string)
  100. string)))
  101. (defun ntlm-build-auth-response (challenge user password-hashes)
  102. "Return the response string to a challenge string CHALLENGE given by
  103. the NTLM based server for the user USER and the password hash list
  104. PASSWORD-HASHES. NTLM uses two hash values which are represented
  105. by PASSWORD-HASHES. PASSWORD-HASHES should be a return value of
  106. (list (ntlm-smb-passwd-hash password) (ntlm-md4hash password))"
  107. (let* ((rchallenge (ntlm-string-as-unibyte challenge))
  108. ;; get fields within challenge struct
  109. ;;(ident (substring rchallenge 0 8)) ;ident, 8 bytes
  110. ;;(msgType (substring rchallenge 8 12)) ;msgType, 4 bytes
  111. (uDomain (substring rchallenge 12 20)) ;uDomain, 8 bytes
  112. (flags (substring rchallenge 20 24)) ;flags, 4 bytes
  113. (challengeData (substring rchallenge 24 32)) ;challengeData, 8 bytes
  114. uDomain-len uDomain-offs
  115. ;; response struct and its fields
  116. lmRespData ;lmRespData, 24 bytes
  117. ntRespData ;ntRespData, 24 bytes
  118. domain ;ascii domain string
  119. lu ld off-lm off-nt off-d off-u off-w off-s)
  120. ;; extract domain string from challenge string
  121. (setq uDomain-len (md4-unpack-int16 (substring uDomain 0 2)))
  122. (setq uDomain-offs (md4-unpack-int32 (substring uDomain 4 8)))
  123. (setq domain
  124. (ntlm-unicode2ascii (substring challenge
  125. (cdr uDomain-offs)
  126. (+ (cdr uDomain-offs) uDomain-len))
  127. (/ uDomain-len 2)))
  128. ;; overwrite domain in case user is given in <user>@<domain> format
  129. (when (string-match "@" user)
  130. (setq domain (substring user (1+ (match-beginning 0))))
  131. (setq user (substring user 0 (match-beginning 0))))
  132. ;; generate response data
  133. (setq lmRespData
  134. (ntlm-smb-owf-encrypt (car password-hashes) challengeData))
  135. (setq ntRespData
  136. (ntlm-smb-owf-encrypt (cadr password-hashes) challengeData))
  137. ;; get offsets to fields to pack the response struct in a string
  138. (setq lu (length user))
  139. (setq ld (length domain))
  140. (setq off-lm 64) ;offset to string 'lmResponse
  141. (setq off-nt (+ 64 24)) ;offset to string 'ntResponse
  142. (setq off-d (+ 64 48)) ;offset to string 'uDomain
  143. (setq off-u (+ 64 48 (* 2 ld))) ;offset to string 'uUser
  144. (setq off-w (+ 64 48 (* 2 (+ ld lu)))) ;offset to string 'uWks
  145. (setq off-s (+ 64 48 (* 2 (+ ld lu lu)))) ;offset to string 'sessionKey
  146. ;; pack the response struct in a string
  147. (concat "NTLMSSP\0" ;response ident field, 8 bytes
  148. (md4-pack-int32 '(0 . 3)) ;response msgType field, 4 bytes
  149. ;; lmResponse field, 8 bytes
  150. ;;AddBytes(response,lmResponse,lmRespData,24);
  151. (md4-pack-int16 24) ;len field
  152. (md4-pack-int16 24) ;maxlen field
  153. (md4-pack-int32 (cons 0 off-lm)) ;field offset
  154. ;; ntResponse field, 8 bytes
  155. ;;AddBytes(response,ntResponse,ntRespData,24);
  156. (md4-pack-int16 24) ;len field
  157. (md4-pack-int16 24) ;maxlen field
  158. (md4-pack-int32 (cons 0 off-nt)) ;field offset
  159. ;; uDomain field, 8 bytes
  160. ;;AddUnicodeString(response,uDomain,domain);
  161. ;;AddBytes(response, uDomain, udomain, 2*ld);
  162. (md4-pack-int16 (* 2 ld)) ;len field
  163. (md4-pack-int16 (* 2 ld)) ;maxlen field
  164. (md4-pack-int32 (cons 0 off-d)) ;field offset
  165. ;; uUser field, 8 bytes
  166. ;;AddUnicodeString(response,uUser,u);
  167. ;;AddBytes(response, uUser, uuser, 2*lu);
  168. (md4-pack-int16 (* 2 lu)) ;len field
  169. (md4-pack-int16 (* 2 lu)) ;maxlen field
  170. (md4-pack-int32 (cons 0 off-u)) ;field offset
  171. ;; uWks field, 8 bytes
  172. ;;AddUnicodeString(response,uWks,u);
  173. (md4-pack-int16 (* 2 lu)) ;len field
  174. (md4-pack-int16 (* 2 lu)) ;maxlen field
  175. (md4-pack-int32 (cons 0 off-w)) ;field offset
  176. ;; sessionKey field, 8 bytes
  177. ;;AddString(response,sessionKey,NULL);
  178. (md4-pack-int16 0) ;len field
  179. (md4-pack-int16 0) ;maxlen field
  180. (md4-pack-int32 (cons 0 (- off-s off-lm))) ;field offset
  181. ;; flags field, 4 bytes
  182. flags ;
  183. ;; buffer field
  184. lmRespData ;lmResponse, 24 bytes
  185. ntRespData ;ntResponse, 24 bytes
  186. (ntlm-ascii2unicode domain ;Unicode domain string, 2*ld bytes
  187. (length domain)) ;
  188. (ntlm-ascii2unicode user ;Unicode user string, 2*lu bytes
  189. (length user)) ;
  190. (ntlm-ascii2unicode user ;Unicode user string, 2*lu bytes
  191. (length user)) ;
  192. )))
  193. (defun ntlm-get-password-hashes (password)
  194. "Return a pair of SMB hash and NT MD4 hash of the given password PASSWORD."
  195. (list (ntlm-smb-passwd-hash password)
  196. (ntlm-md4hash password)))
  197. (defun ntlm-ascii2unicode (str len)
  198. "Convert an ASCII string into a NT Unicode string, which is
  199. little-endian utf16."
  200. (let ((utf (make-string (* 2 len) 0)) (i 0) val)
  201. (while (and (< i len)
  202. (not (zerop (setq val (aref str i)))))
  203. (aset utf (* 2 i) val)
  204. (aset utf (1+ (* 2 i)) 0)
  205. (setq i (1+ i)))
  206. utf))
  207. (defun ntlm-unicode2ascii (str len)
  208. "Extract 7 bits ASCII part of a little endian utf16 string STR of length LEN."
  209. (let ((buf (make-string len 0)) (i 0) (j 0))
  210. (while (< i len)
  211. (aset buf i (logand (aref str j) 127)) ;(string-to-number "7f" 16)
  212. (setq i (1+ i)
  213. j (+ 2 j)))
  214. buf))
  215. (defun ntlm-smb-passwd-hash (passwd)
  216. "Return the SMB password hash string of 16 bytes long for the given password
  217. string PASSWD. PASSWD is truncated to 14 bytes if longer."
  218. (let ((len (min (length passwd) 14)))
  219. (ntlm-smb-des-e-p16
  220. (concat (substring (upcase passwd) 0 len) ;fill top 14 bytes with passwd
  221. (make-string (- 15 len) 0)))))
  222. (defun ntlm-smb-owf-encrypt (passwd c8)
  223. "Return the response string of 24 bytes long for the given password
  224. string PASSWD based on the DES encryption. PASSWD is of at most 14
  225. bytes long and the challenge string C8 of 8 bytes long."
  226. (let ((len (min (length passwd) 16)) p22)
  227. (setq p22 (concat (substring passwd 0 len) ;fill top 16 bytes with passwd
  228. (make-string (- 22 len) 0)))
  229. (ntlm-smb-des-e-p24 p22 c8)))
  230. (defun ntlm-smb-des-e-p24 (p22 c8)
  231. "Return a 24 bytes hashed string for a 21 bytes string P22 and a 8 bytes
  232. string C8."
  233. (concat (ntlm-smb-hash c8 p22 t) ;hash first 8 bytes of p22
  234. (ntlm-smb-hash c8 (substring p22 7) t)
  235. (ntlm-smb-hash c8 (substring p22 14) t)))
  236. (defconst ntlm-smb-sp8 [75 71 83 33 64 35 36 37])
  237. (defun ntlm-smb-des-e-p16 (p15)
  238. "Return a 16 bytes hashed string for a 15 bytes string P15."
  239. (concat (ntlm-smb-hash ntlm-smb-sp8 p15 t) ;hash of first 8 bytes of p15
  240. (ntlm-smb-hash ntlm-smb-sp8 ;hash of last 8 bytes of p15
  241. (substring p15 7) t)))
  242. (defun ntlm-smb-hash (in key forw)
  243. "Return the hash string of length 8 for a string IN of length 8 and
  244. a string KEY of length 8. FORW is t or nil."
  245. (let ((out (make-string 8 0))
  246. outb ;string of length 64
  247. (inb (make-string 64 0))
  248. (keyb (make-string 64 0))
  249. (key2 (ntlm-smb-str-to-key key))
  250. (i 0) aa)
  251. (while (< i 64)
  252. (unless (zerop (logand (aref in (/ i 8)) (lsh 1 (- 7 (% i 8)))))
  253. (aset inb i 1))
  254. (unless (zerop (logand (aref key2 (/ i 8)) (lsh 1 (- 7 (% i 8)))))
  255. (aset keyb i 1))
  256. (setq i (1+ i)))
  257. (setq outb (ntlm-smb-dohash inb keyb forw))
  258. (setq i 0)
  259. (while (< i 64)
  260. (unless (zerop (aref outb i))
  261. (setq aa (aref out (/ i 8)))
  262. (aset out (/ i 8)
  263. (logior aa (lsh 1 (- 7 (% i 8))))))
  264. (setq i (1+ i)))
  265. out))
  266. (defun ntlm-smb-str-to-key (str)
  267. "Return a string of length 8 for the given string STR of length 7."
  268. (let ((key (make-string 8 0))
  269. (i 7))
  270. (aset key 0 (lsh (aref str 0) -1))
  271. (aset key 1 (logior
  272. (lsh (logand (aref str 0) 1) 6)
  273. (lsh (aref str 1) -2)))
  274. (aset key 2 (logior
  275. (lsh (logand (aref str 1) 3) 5)
  276. (lsh (aref str 2) -3)))
  277. (aset key 3 (logior
  278. (lsh (logand (aref str 2) 7) 4)
  279. (lsh (aref str 3) -4)))
  280. (aset key 4 (logior
  281. (lsh (logand (aref str 3) 15) 3)
  282. (lsh (aref str 4) -5)))
  283. (aset key 5 (logior
  284. (lsh (logand (aref str 4) 31) 2)
  285. (lsh (aref str 5) -6)))
  286. (aset key 6 (logior
  287. (lsh (logand (aref str 5) 63) 1)
  288. (lsh (aref str 6) -7)))
  289. (aset key 7 (logand (aref str 6) 127))
  290. (while (>= i 0)
  291. (aset key i (lsh (aref key i) 1))
  292. (setq i (1- i)))
  293. key))
  294. (defconst ntlm-smb-perm1 [57 49 41 33 25 17 9
  295. 1 58 50 42 34 26 18
  296. 10 2 59 51 43 35 27
  297. 19 11 3 60 52 44 36
  298. 63 55 47 39 31 23 15
  299. 7 62 54 46 38 30 22
  300. 14 6 61 53 45 37 29
  301. 21 13 5 28 20 12 4])
  302. (defconst ntlm-smb-perm2 [14 17 11 24 1 5
  303. 3 28 15 6 21 10
  304. 23 19 12 4 26 8
  305. 16 7 27 20 13 2
  306. 41 52 31 37 47 55
  307. 30 40 51 45 33 48
  308. 44 49 39 56 34 53
  309. 46 42 50 36 29 32])
  310. (defconst ntlm-smb-perm3 [58 50 42 34 26 18 10 2
  311. 60 52 44 36 28 20 12 4
  312. 62 54 46 38 30 22 14 6
  313. 64 56 48 40 32 24 16 8
  314. 57 49 41 33 25 17 9 1
  315. 59 51 43 35 27 19 11 3
  316. 61 53 45 37 29 21 13 5
  317. 63 55 47 39 31 23 15 7])
  318. (defconst ntlm-smb-perm4 [32 1 2 3 4 5
  319. 4 5 6 7 8 9
  320. 8 9 10 11 12 13
  321. 12 13 14 15 16 17
  322. 16 17 18 19 20 21
  323. 20 21 22 23 24 25
  324. 24 25 26 27 28 29
  325. 28 29 30 31 32 1])
  326. (defconst ntlm-smb-perm5 [16 7 20 21
  327. 29 12 28 17
  328. 1 15 23 26
  329. 5 18 31 10
  330. 2 8 24 14
  331. 32 27 3 9
  332. 19 13 30 6
  333. 22 11 4 25])
  334. (defconst ntlm-smb-perm6 [40 8 48 16 56 24 64 32
  335. 39 7 47 15 55 23 63 31
  336. 38 6 46 14 54 22 62 30
  337. 37 5 45 13 53 21 61 29
  338. 36 4 44 12 52 20 60 28
  339. 35 3 43 11 51 19 59 27
  340. 34 2 42 10 50 18 58 26
  341. 33 1 41 9 49 17 57 25])
  342. (defconst ntlm-smb-sc [1 1 2 2 2 2 2 2 1 2 2 2 2 2 2 1])
  343. (defconst ntlm-smb-sbox [[[14 4 13 1 2 15 11 8 3 10 6 12 5 9 0 7]
  344. [ 0 15 7 4 14 2 13 1 10 6 12 11 9 5 3 8]
  345. [ 4 1 14 8 13 6 2 11 15 12 9 7 3 10 5 0]
  346. [15 12 8 2 4 9 1 7 5 11 3 14 10 0 6 13]]
  347. [[15 1 8 14 6 11 3 4 9 7 2 13 12 0 5 10]
  348. [ 3 13 4 7 15 2 8 14 12 0 1 10 6 9 11 5]
  349. [ 0 14 7 11 10 4 13 1 5 8 12 6 9 3 2 15]
  350. [13 8 10 1 3 15 4 2 11 6 7 12 0 5 14 9]]
  351. [[10 0 9 14 6 3 15 5 1 13 12 7 11 4 2 8]
  352. [13 7 0 9 3 4 6 10 2 8 5 14 12 11 15 1]
  353. [13 6 4 9 8 15 3 0 11 1 2 12 5 10 14 7]
  354. [ 1 10 13 0 6 9 8 7 4 15 14 3 11 5 2 12]]
  355. [[ 7 13 14 3 0 6 9 10 1 2 8 5 11 12 4 15]
  356. [13 8 11 5 6 15 0 3 4 7 2 12 1 10 14 9]
  357. [10 6 9 0 12 11 7 13 15 1 3 14 5 2 8 4]
  358. [ 3 15 0 6 10 1 13 8 9 4 5 11 12 7 2 14]]
  359. [[ 2 12 4 1 7 10 11 6 8 5 3 15 13 0 14 9]
  360. [14 11 2 12 4 7 13 1 5 0 15 10 3 9 8 6]
  361. [ 4 2 1 11 10 13 7 8 15 9 12 5 6 3 0 14]
  362. [11 8 12 7 1 14 2 13 6 15 0 9 10 4 5 3]]
  363. [[12 1 10 15 9 2 6 8 0 13 3 4 14 7 5 11]
  364. [10 15 4 2 7 12 9 5 6 1 13 14 0 11 3 8]
  365. [ 9 14 15 5 2 8 12 3 7 0 4 10 1 13 11 6]
  366. [ 4 3 2 12 9 5 15 10 11 14 1 7 6 0 8 13]]
  367. [[ 4 11 2 14 15 0 8 13 3 12 9 7 5 10 6 1]
  368. [13 0 11 7 4 9 1 10 14 3 5 12 2 15 8 6]
  369. [ 1 4 11 13 12 3 7 14 10 15 6 8 0 5 9 2]
  370. [ 6 11 13 8 1 4 10 7 9 5 0 15 14 2 3 12]]
  371. [[13 2 8 4 6 15 11 1 10 9 3 14 5 0 12 7]
  372. [ 1 15 13 8 10 3 7 4 12 5 6 11 0 14 9 2]
  373. [ 7 11 4 1 9 12 14 2 0 6 10 13 15 3 5 8]
  374. [ 2 1 14 7 4 10 8 13 15 12 9 0 3 5 6 11]]])
  375. (defsubst ntlm-string-permute (in perm n)
  376. "Return a string of length N for a string IN and a permutation vector
  377. PERM of size N. The length of IN should be height of PERM."
  378. (let ((i 0) (out (make-string n 0)))
  379. (while (< i n)
  380. (aset out i (aref in (- (aref perm i) 1)))
  381. (setq i (1+ i)))
  382. out))
  383. (defsubst ntlm-string-lshift (str count len)
  384. "Return a string by circularly shifting a string STR by COUNT to the left.
  385. length of STR is LEN."
  386. (let ((c (% count len)))
  387. (concat (substring str c len) (substring str 0 c))))
  388. (defsubst ntlm-string-xor (in1 in2 n)
  389. "Return exclusive-or of sequences in1 and in2"
  390. (let ((w (make-string n 0)) (i 0))
  391. (while (< i n)
  392. (aset w i (logxor (aref in1 i) (aref in2 i)))
  393. (setq i (1+ i)))
  394. w))
  395. (defun ntlm-smb-dohash (in key forw)
  396. "Return the hash value for a string IN and a string KEY.
  397. Length of IN and KEY are 64. FORW non-nil means forward, nil means
  398. backward."
  399. (let (pk1 ;string of length 56
  400. c ;string of length 28
  401. d ;string of length 28
  402. cd ;string of length 56
  403. (ki (make-vector 16 0)) ;vector of string of length 48
  404. pd1 ;string of length 64
  405. l ;string of length 32
  406. r ;string of length 32
  407. rl ;string of length 64
  408. (i 0) (j 0) (k 0))
  409. (setq pk1 (ntlm-string-permute key ntlm-smb-perm1 56))
  410. (setq c (substring pk1 0 28))
  411. (setq d (substring pk1 28 56))
  412. (setq i 0)
  413. (while (< i 16)
  414. (setq c (ntlm-string-lshift c (aref ntlm-smb-sc i) 28))
  415. (setq d (ntlm-string-lshift d (aref ntlm-smb-sc i) 28))
  416. (setq cd (concat (substring c 0 28) (substring d 0 28)))
  417. (aset ki i (ntlm-string-permute cd ntlm-smb-perm2 48))
  418. (setq i (1+ i)))
  419. (setq pd1 (ntlm-string-permute in ntlm-smb-perm3 64))
  420. (setq l (substring pd1 0 32))
  421. (setq r (substring pd1 32 64))
  422. (setq i 0)
  423. (let (er ;string of length 48
  424. erk ;string of length 48
  425. (b (make-vector 8 0)) ;vector of strings of length 6
  426. cb ;string of length 32
  427. pcb ;string of length 32
  428. r2 ;string of length 32
  429. jj m n bj sbox-jmn)
  430. (while (< i 16)
  431. (setq er (ntlm-string-permute r ntlm-smb-perm4 48))
  432. (setq erk (ntlm-string-xor er
  433. (aref ki (if forw i (- 15 i)))
  434. 48))
  435. (setq j 0)
  436. (while (< j 8)
  437. (setq jj (* 6 j))
  438. (aset b j (substring erk jj (+ jj 6)))
  439. (setq j (1+ j)))
  440. (setq j 0)
  441. (while (< j 8)
  442. (setq bj (aref b j))
  443. (setq m (logior (lsh (aref bj 0) 1) (aref bj 5)))
  444. (setq n (logior (lsh (aref bj 1) 3)
  445. (lsh (aref bj 2) 2)
  446. (lsh (aref bj 3) 1)
  447. (aref bj 4)))
  448. (setq k 0)
  449. (setq sbox-jmn (aref (aref (aref ntlm-smb-sbox j) m) n))
  450. (while (< k 4)
  451. (aset bj k
  452. (if (zerop (logand sbox-jmn (lsh 1 (- 3 k))))
  453. 0 1))
  454. (setq k (1+ k)))
  455. (setq j (1+ j)))
  456. (setq j 0)
  457. (setq cb nil)
  458. (while (< j 8)
  459. (setq cb (concat cb (substring (aref b j) 0 4)))
  460. (setq j (1+ j)))
  461. (setq pcb (ntlm-string-permute cb ntlm-smb-perm5 32))
  462. (setq r2 (ntlm-string-xor l pcb 32))
  463. (setq l r)
  464. (setq r r2)
  465. (setq i (1+ i))))
  466. (setq rl (concat r l))
  467. (ntlm-string-permute rl ntlm-smb-perm6 64)))
  468. (defun ntlm-md4hash (passwd)
  469. "Return the 16 bytes MD4 hash of a string PASSWD after converting it
  470. into a Unicode string. PASSWD is truncated to 128 bytes if longer."
  471. (let (len wpwd)
  472. ;; Password cannot be longer than 128 characters
  473. (setq len (length passwd))
  474. (if (> len 128)
  475. (setq len 128))
  476. ;; Password must be converted to NT Unicode
  477. (setq wpwd (ntlm-ascii2unicode passwd len))
  478. ;; Calculate length in bytes
  479. (setq len (* len 2))
  480. (md4 wpwd len)))
  481. (provide 'ntlm)
  482. ;;; ntlm.el ends here