1 /* 2 * ***** BEGIN LICENSE BLOCK ***** 3 * Zimbra Collaboration Suite Web Client 4 * Copyright (C) 2005, 2006, 2007, 2009, 2010, 2013, 2014, 2016 Synacor, Inc. 5 * 6 * The contents of this file are subject to the Common Public Attribution License Version 1.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at: https://www.zimbra.com/license 9 * The License is based on the Mozilla Public License Version 1.1 but Sections 14 and 15 10 * have been added to cover use of software over a computer network and provide for limited attribution 11 * for the Original Developer. In addition, Exhibit A has been modified to be consistent with Exhibit B. 12 * 13 * Software distributed under the License is distributed on an "AS IS" basis, 14 * WITHOUT WARRANTY OF ANY KIND, either express or implied. 15 * See the License for the specific language governing rights and limitations under the License. 16 * The Original Code is Zimbra Open Source Web Client. 17 * The Initial Developer of the Original Code is Zimbra, Inc. All rights to the Original Code were 18 * transferred by Zimbra, Inc. to Synacor, Inc. on September 14, 2015. 19 * 20 * All portions of the code are Copyright (C) 2005, 2006, 2007, 2009, 2010, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved. 21 * ***** END LICENSE BLOCK ***** 22 */ 23 24 25 /* 26 * Based on code by Paul Johnston: 27 * 28 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message 29 * Digest Algorithm, as defined in RFC 1321. 30 * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002. 31 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet 32 * Distributed under the BSD License 33 * See http://pajhome.org.uk/crypt/md5 for more info. 34 */ 35 36 // Namespace for common functions 37 // Members are initialized at the end of this file 38 // Do NOT instantiate. Call class members like this: 39 // 40 // var md5_hash = AjxMD5.hex_md5('foo bar'); 41 // 42 // See the available methods at EOF (look for "this"). 43 var AjxMD5 = function() { 44 45 /* 46 * Configurable variables. You may need to tweak these to be compatible with 47 * the server-side, but the defaults work in most cases. 48 */ 49 var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */ 50 var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */ 51 var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */ 52 53 /* 54 * Add integers, wrapping at 2^32. Uses 16-bit operations internally 55 * to work around bugs in some JS interpreters. 56 */ 57 function safe_add(x, y) { 58 var lsw = (x & 0xFFFF) + (y & 0xFFFF); 59 var msw = (x >> 16) + (y >> 16) + (lsw >> 16); 60 return (msw << 16) | (lsw & 0xFFFF); 61 }; 62 63 /* 64 * Bitwise rotate a 32-bit number to the left. 65 */ 66 function bit_rol(num, cnt) { 67 return (num << cnt) | (num >>> (32 - cnt)); 68 }; 69 70 /* 71 * These functions implement the four basic operations the algorithm uses. 72 */ 73 function md5_cmn(q, a, b, x, s, t) { 74 return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b); 75 }; 76 77 function md5_ff(a, b, c, d, x, s, t) { 78 return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t); 79 }; 80 81 function md5_gg(a, b, c, d, x, s, t) { 82 return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t); 83 }; 84 85 function md5_hh(a, b, c, d, x, s, t) { 86 return md5_cmn(b ^ c ^ d, a, b, x, s, t); 87 }; 88 89 function md5_ii(a, b, c, d, x, s, t) { 90 return md5_cmn(c ^ (b | (~d)), a, b, x, s, t); 91 }; 92 93 /* 94 * Calculate the MD5 of an array of little-endian words, and a bit length 95 */ 96 function core_md5(x, len) { 97 /* append padding */ 98 x[len >> 5] |= 0x80 << ((len) % 32); 99 x[(((len + 64) >>> 9) << 4) + 14] = len; 100 101 var a = 1732584193; 102 var b = -271733879; 103 var c = -1732584194; 104 var d = 271733878; 105 106 for (var i = 0; i < x.length; i += 16) { 107 var olda = a; 108 var oldb = b; 109 var oldc = c; 110 var oldd = d; 111 112 a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936); 113 d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586); 114 c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819); 115 b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330); 116 a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897); 117 d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426); 118 c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341); 119 b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983); 120 a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416); 121 d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417); 122 c = md5_ff(c, d, a, b, x[i+10], 17, -42063); 123 b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162); 124 a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682); 125 d = md5_ff(d, a, b, c, x[i+13], 12, -40341101); 126 c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290); 127 b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329); 128 129 a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510); 130 d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632); 131 c = md5_gg(c, d, a, b, x[i+11], 14, 643717713); 132 b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302); 133 a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691); 134 d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083); 135 c = md5_gg(c, d, a, b, x[i+15], 14, -660478335); 136 b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848); 137 a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438); 138 d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690); 139 c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961); 140 b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501); 141 a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467); 142 d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784); 143 c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473); 144 b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734); 145 146 a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558); 147 d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463); 148 c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562); 149 b = md5_hh(b, c, d, a, x[i+14], 23, -35309556); 150 a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060); 151 d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353); 152 c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632); 153 b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640); 154 a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174); 155 d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222); 156 c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979); 157 b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189); 158 a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487); 159 d = md5_hh(d, a, b, c, x[i+12], 11, -421815835); 160 c = md5_hh(c, d, a, b, x[i+15], 16, 530742520); 161 b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651); 162 163 a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844); 164 d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415); 165 c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905); 166 b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055); 167 a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571); 168 d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606); 169 c = md5_ii(c, d, a, b, x[i+10], 15, -1051523); 170 b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799); 171 a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359); 172 d = md5_ii(d, a, b, c, x[i+15], 10, -30611744); 173 c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380); 174 b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649); 175 a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070); 176 d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379); 177 c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259); 178 b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551); 179 180 a = safe_add(a, olda); 181 b = safe_add(b, oldb); 182 c = safe_add(c, oldc); 183 d = safe_add(d, oldd); 184 } 185 return Array(a, b, c, d); 186 }; 187 188 /* 189 * Convert a string to an array of little-endian words 190 * If chrsz is ASCII, characters >255 have their hi-byte silently ignored. 191 */ 192 function str2binl(str) { 193 var bin = Array(); 194 var mask = (1 << chrsz) - 1; 195 for (var i = 0; i < str.length * chrsz; i += chrsz) 196 bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32); 197 return bin; 198 }; 199 200 /* 201 * Calculate the HMAC-MD5, of a key and some data 202 */ 203 function core_hmac_md5(key, data) { 204 var bkey = str2binl(key); 205 if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz); 206 207 var ipad = Array(16), opad = Array(16); 208 for (var i = 0; i < 16; i++) { 209 ipad[i] = bkey[i] ^ 0x36363636; 210 opad[i] = bkey[i] ^ 0x5C5C5C5C; 211 } 212 213 var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz); 214 return core_md5(opad.concat(hash), 512 + 128); 215 }; 216 217 /* 218 * Convert an array of little-endian words to a string 219 */ 220 function binl2str(bin) { 221 var str = ""; 222 var mask = (1 << chrsz) - 1; 223 for (var i = 0; i < bin.length * 32; i += chrsz) 224 str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask); 225 return str; 226 }; 227 228 /* 229 * Convert an array of little-endian words to a hex string. 230 */ 231 function binl2hex(binarray) { 232 var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; 233 var str = ""; 234 for (var i = 0; i < binarray.length * 4; i++) { 235 str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) + 236 hex_tab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF); 237 } 238 return str; 239 }; 240 241 /* 242 * Convert an array of little-endian words to a base-64 string 243 */ 244 function binl2b64(binarray) { 245 var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 246 var str = ""; 247 for (var i = 0; i < binarray.length * 4; i += 3) { 248 var triplet = (((binarray[i >> 2] >> 8 * ( i %4)) & 0xFF) << 16) 249 | (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 ) 250 | ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF); 251 for (var j = 0; j < 4; j++) { 252 if(i * 8 + j * 6 > binarray.length * 32) str += b64pad; 253 else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); 254 } 255 } 256 return str; 257 }; 258 259 /* 260 * These are the functions you'll usually want to call 261 * They take string arguments and return either hex or base-64 encoded strings 262 */ 263 function hex_md5(s) { return binl2hex(core_md5(str2binl(s), s.length * chrsz)); }; 264 function b64_md5(s) { return binl2b64(core_md5(str2binl(s), s.length * chrsz)); }; 265 function str_md5(s) { return binl2str(core_md5(str2binl(s), s.length * chrsz)); }; 266 function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); }; 267 function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); }; 268 function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); }; 269 270 /* 271 * Perform a simple self-test to see if the VM is working 272 */ 273 function md5_vm_test() { 274 return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72"; 275 }; 276 277 // initialize global stuff 278 this.hex_md5 = hex_md5; 279 this.b64_md5 = b64_md5; 280 this.str_md5 = str_md5; 281 this.hex_hmac = hex_hmac_md5; 282 this.b64_hmac = b64_hmac_md5; 283 this.str_hmac = str_hmac_md5; 284 this.md5_vm_test = md5_vm_test; 285 }; 286 287 AjxMD5 = new AjxMD5; 288