| |
1 /* |
| |
2 * A JavaScript implementation of the RSA Data Security, Inc. MD4 Message |
| |
3 * Digest Algorithm, as defined in RFC 1320. |
| |
4 * Version 2.1 Copyright (C) Jerrad Pierce, Paul Johnston 1999 - 2002. |
| |
5 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet |
| |
6 * Distributed under the BSD License |
| |
7 * See http://pajhome.org.uk/crypt/md5 for more info. |
| |
8 */ |
| |
9 |
| |
10 /* |
| |
11 * Configurable variables. You may need to tweak these to be compatible with |
| |
12 * the server-side, but the defaults work in most cases. |
| |
13 */ |
| |
14 var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */ |
| |
15 var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */ |
| |
16 var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */ |
| |
17 |
| |
18 /* |
| |
19 * These are the functions you'll usually want to call |
| |
20 */ |
| |
21 function hexmd4(s){ return binl2hex(coremd4(str2binl(s), s.length * chrsz));} |
| |
22 function b64md4(s){ return binl2b64(coremd4(str2binl(s), s.length * chrsz));} |
| |
23 function strmd4(s){ return binl2str(coremd4(str2binl(s), s.length * chrsz));} |
| |
24 function hexhmacmd4(key, data) { return binl2hex(corehmacmd4(key, data)); } |
| |
25 function b64hmacmd4(key, data) { return binl2b64(corehmacmd4(key, data)); } |
| |
26 function strhmacmd4(key, data) { return binl2str(corehmacmd4(key, data)); } |
| |
27 |
| |
28 /* |
| |
29 * Perform a simple self-test to see if the VM is working |
| |
30 */ |
| |
31 function md4vmtest() |
| |
32 { |
| |
33 return hexmd4("abc") == "a448017aaf21d8525fc10ae87aa6729d"; |
| |
34 } |
| |
35 |
| |
36 /* |
| |
37 * Calculate the MD4 of an array of little-endian words, and a bit length |
| |
38 */ |
| |
39 function coremd4(x, len) |
| |
40 { |
| |
41 /* append padding */ |
| |
42 x[len >> 5] |= 0x80 << (len % 32); |
| |
43 x[(((len + 64) >>> 9) << 4) + 14] = len; |
| |
44 |
| |
45 var a = 1732584193; |
| |
46 var b = -271733879; |
| |
47 var c = -1732584194; |
| |
48 var d = 271733878; |
| |
49 |
| |
50 for(var i = 0; i < x.length; i += 16) |
| |
51 { |
| |
52 var olda = a; |
| |
53 var oldb = b; |
| |
54 var oldc = c; |
| |
55 var oldd = d; |
| |
56 |
| |
57 a = md4ff(a, b, c, d, x[i+ 0], 3 ); |
| |
58 d = md4ff(d, a, b, c, x[i+ 1], 7 ); |
| |
59 c = md4ff(c, d, a, b, x[i+ 2], 11); |
| |
60 b = md4ff(b, c, d, a, x[i+ 3], 19); |
| |
61 a = md4ff(a, b, c, d, x[i+ 4], 3 ); |
| |
62 d = md4ff(d, a, b, c, x[i+ 5], 7 ); |
| |
63 c = md4ff(c, d, a, b, x[i+ 6], 11); |
| |
64 b = md4ff(b, c, d, a, x[i+ 7], 19); |
| |
65 a = md4ff(a, b, c, d, x[i+ 8], 3 ); |
| |
66 d = md4ff(d, a, b, c, x[i+ 9], 7 ); |
| |
67 c = md4ff(c, d, a, b, x[i+10], 11); |
| |
68 b = md4ff(b, c, d, a, x[i+11], 19); |
| |
69 a = md4ff(a, b, c, d, x[i+12], 3 ); |
| |
70 d = md4ff(d, a, b, c, x[i+13], 7 ); |
| |
71 c = md4ff(c, d, a, b, x[i+14], 11); |
| |
72 b = md4ff(b, c, d, a, x[i+15], 19); |
| |
73 |
| |
74 a = md4gg(a, b, c, d, x[i+ 0], 3 ); |
| |
75 d = md4gg(d, a, b, c, x[i+ 4], 5 ); |
| |
76 c = md4gg(c, d, a, b, x[i+ 8], 9 ); |
| |
77 b = md4gg(b, c, d, a, x[i+12], 13); |
| |
78 a = md4gg(a, b, c, d, x[i+ 1], 3 ); |
| |
79 d = md4gg(d, a, b, c, x[i+ 5], 5 ); |
| |
80 c = md4gg(c, d, a, b, x[i+ 9], 9 ); |
| |
81 b = md4gg(b, c, d, a, x[i+13], 13); |
| |
82 a = md4gg(a, b, c, d, x[i+ 2], 3 ); |
| |
83 d = md4gg(d, a, b, c, x[i+ 6], 5 ); |
| |
84 c = md4gg(c, d, a, b, x[i+10], 9 ); |
| |
85 b = md4gg(b, c, d, a, x[i+14], 13); |
| |
86 a = md4gg(a, b, c, d, x[i+ 3], 3 ); |
| |
87 d = md4gg(d, a, b, c, x[i+ 7], 5 ); |
| |
88 c = md4gg(c, d, a, b, x[i+11], 9 ); |
| |
89 b = md4gg(b, c, d, a, x[i+15], 13); |
| |
90 |
| |
91 a = md4hh(a, b, c, d, x[i+ 0], 3 ); |
| |
92 d = md4hh(d, a, b, c, x[i+ 8], 9 ); |
| |
93 c = md4hh(c, d, a, b, x[i+ 4], 11); |
| |
94 b = md4hh(b, c, d, a, x[i+12], 15); |
| |
95 a = md4hh(a, b, c, d, x[i+ 2], 3 ); |
| |
96 d = md4hh(d, a, b, c, x[i+10], 9 ); |
| |
97 c = md4hh(c, d, a, b, x[i+ 6], 11); |
| |
98 b = md4hh(b, c, d, a, x[i+14], 15); |
| |
99 a = md4hh(a, b, c, d, x[i+ 1], 3 ); |
| |
100 d = md4hh(d, a, b, c, x[i+ 9], 9 ); |
| |
101 c = md4hh(c, d, a, b, x[i+ 5], 11); |
| |
102 b = md4hh(b, c, d, a, x[i+13], 15); |
| |
103 a = md4hh(a, b, c, d, x[i+ 3], 3 ); |
| |
104 d = md4hh(d, a, b, c, x[i+11], 9 ); |
| |
105 c = md4hh(c, d, a, b, x[i+ 7], 11); |
| |
106 b = md4hh(b, c, d, a, x[i+15], 15); |
| |
107 |
| |
108 a = safeadd(a, olda); |
| |
109 b = safeadd(b, oldb); |
| |
110 c = safeadd(c, oldc); |
| |
111 d = safeadd(d, oldd); |
| |
112 |
| |
113 } |
| |
114 return Array(a, b, c, d); |
| |
115 |
| |
116 } |
| |
117 |
| |
118 /* |
| |
119 * These functions implement the basic operation for each round of the |
| |
120 * algorithm. |
| |
121 */ |
| |
122 function md4cmn(q, a, b, x, s, t) |
| |
123 { |
| |
124 return safeadd(rol(safeadd(safeadd(a, q), safeadd(x, t)), s), b); |
| |
125 } |
| |
126 function md4ff(a, b, c, d, x, s) |
| |
127 { |
| |
128 return md4cmn((b & c) | ((~b) & d), a, 0, x, s, 0); |
| |
129 } |
| |
130 function md4gg(a, b, c, d, x, s) |
| |
131 { |
| |
132 return md4cmn((b & c) | (b & d) | (c & d), a, 0, x, s, 1518500249); |
| |
133 } |
| |
134 function md4hh(a, b, c, d, x, s) |
| |
135 { |
| |
136 return md4cmn(b ^ c ^ d, a, 0, x, s, 1859775393); |
| |
137 } |
| |
138 |
| |
139 /* |
| |
140 * Calculate the HMAC-MD4, of a key and some data |
| |
141 */ |
| |
142 function corehmacmd4(key, data) |
| |
143 { |
| |
144 var bkey = str2binl(key); |
| |
145 if(bkey.length > 16) bkey = coremd4(bkey, key.length * chrsz); |
| |
146 |
| |
147 var ipad = Array(16), opad = Array(16); |
| |
148 for(var i = 0; i < 16; i++) |
| |
149 { |
| |
150 ipad[i] = bkey[i] ^ 0x36363636; |
| |
151 opad[i] = bkey[i] ^ 0x5C5C5C5C; |
| |
152 } |
| |
153 |
| |
154 var hash = coremd4(ipad.concat(str2binl(data)), 512 + data.length * chrsz); |
| |
155 return coremd4(opad.concat(hash), 512 + 128); |
| |
156 } |
| |
157 |
| |
158 /* |
| |
159 * Add integers, wrapping at 2^32. This uses 16-bit operations internally |
| |
160 * to work around bugs in some JS interpreters. |
| |
161 */ |
| |
162 function safeadd(x, y) |
| |
163 { |
| |
164 var lsw = (x & 0xFFFF) + (y & 0xFFFF); |
| |
165 var msw = (x >> 16) + (y >> 16) + (lsw >> 16); |
| |
166 return (msw << 16) | (lsw & 0xFFFF); |
| |
167 } |
| |
168 |
| |
169 /* |
| |
170 * Bitwise rotate a 32-bit number to the left. |
| |
171 */ |
| |
172 function rol(num, cnt) |
| |
173 { |
| |
174 return (num << cnt) | (num >>> (32 - cnt)); |
| |
175 } |
| |
176 |
| |
177 /* |
| |
178 * Convert a string to an array of little-endian words |
| |
179 * If chrsz is ASCII, characters >255 have their hi-byte silently ignored. |
| |
180 */ |
| |
181 function str2binl(str) |
| |
182 { |
| |
183 var bin = Array(); |
| |
184 var mask = (1 << chrsz) - 1; |
| |
185 for(var i = 0; i < str.length * chrsz; i += chrsz) |
| |
186 bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32); |
| |
187 return bin; |
| |
188 } |
| |
189 |
| |
190 /* |
| |
191 * Convert an array of little-endian words to a string |
| |
192 */ |
| |
193 function binl2str(bin) |
| |
194 { |
| |
195 var str = ""; |
| |
196 var mask = (1 << chrsz) - 1; |
| |
197 for(var i = 0; i < bin.length * 32; i += chrsz) |
| |
198 str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask); |
| |
199 return str; |
| |
200 } |
| |
201 |
| |
202 /* |
| |
203 * Convert an array of little-endian words to a hex string. |
| |
204 */ |
| |
205 function binl2hex(binarray) |
| |
206 { |
| |
207 var hextab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; |
| |
208 var str = ""; |
| |
209 for(var i = 0; i < binarray.length * 4; i++) |
| |
210 { |
| |
211 str += hextab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) + |
| |
212 hextab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF); |
| |
213 } |
| |
214 return str; |
| |
215 } |
| |
216 |
| |
217 /* |
| |
218 * Convert an array of little-endian words to a base-64 string |
| |
219 */ |
| |
220 function binl2b64(binarray) |
| |
221 { |
| |
222 var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| |
223 var str = ""; |
| |
224 for(var i = 0; i < binarray.length * 4; i += 3) |
| |
225 { |
| |
226 var triplet = (((binarray[i >> 2] >> 8 * ( i %4)) & 0xFF) << 16) |
| |
227 | (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 ) |
| |
228 | ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF); |
| |
229 for(var j = 0; j < 4; j++) |
| |
230 { |
| |
231 if(i * 8 + j * 6 > binarray.length * 32) str += b64pad; |
| |
232 else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); |
| |
233 } |
| |
234 } |
| |
235 return str; |
| |
236 } |