mirror of
				https://github.com/Ylianst/MeshCentral.git
				synced 2025-03-09 15:40:18 +00:00 
			
		
		
		
	Added RC4/MD4 alternatives for NodeJS v17, #
This commit is contained in:
		
							parent
							
								
									e526e60f72
								
							
						
					
					
						commit
						b77fe3da66
					
				
					 4 changed files with 668 additions and 7 deletions
				
			
		|  | @ -273,7 +273,13 @@ function decodeTargetInfo(targetInfoBuf) { | |||
| function bufToArr(b) { var r = []; for (var i = 0; i < b.length; i++) { r.push(b.readUInt8(i)); } return r; } // For unit testing
 | ||||
| function compareArray(a, b) { if (a.length != b.length) return false; for (var i = 0; i < a.length; i++) { if (a[i] != b[i]) return false; } return true; } // For unit testing
 | ||||
| function toUnicode(str) { return Buffer.from(str, 'ucs2'); } | ||||
| function md4(str) { return crypto.createHash('md4').update(str).digest(); } | ||||
| function md4(buffer) { | ||||
|     try { | ||||
|         return crypto.createHash('md4').update(buffer).digest(); // Built in NodeJS MD4, this does not work starting with NodeJS v17
 | ||||
|     } catch (ex) { | ||||
|         return Buffer.from(require('../security/md4').array(buffer.toString('binary'))); // This is the alternative if NodeJS does not support MD4
 | ||||
|     } | ||||
| } | ||||
| function md5(str) { return crypto.createHash('md5').update(str).digest(); } | ||||
| function hmac_md5(key, data) { return crypto.createHmac('md5', key).update(data).digest(); } | ||||
| function ntowfv2(password, user, domain) { return hmac_md5(md4(toUnicode(password)), toUnicode(user.toUpperCase() + domain)); } | ||||
|  | @ -290,7 +296,20 @@ function compute_response_v2(response_key_nt, response_key_lm, server_challenge, | |||
|     return [nt_challenge_response, lm_challenge_response, session_base_key]; | ||||
| } | ||||
| function kx_key_v2(session_base_key, _lm_challenge_response, _server_challenge) { return session_base_key; } | ||||
| function rc4k(key, data) { return crypto.createCipheriv('rc4', key, null).update(data); } | ||||
| function rc4k(key, data) { return createRC4(key).update(data); } | ||||
| 
 | ||||
| function createRC4(key) { | ||||
|     const obj = {}; | ||||
|     try { | ||||
|         obj.n = crypto.createCipheriv('rc4', key, null); // Built in NodeJS RC4, this does not work starting with NodeJS v17
 | ||||
|         obj.update = function(x) { return obj.n.update(x); } | ||||
|     } catch (ex) { | ||||
|         const RC4 = require('../security/rc4'); // This is the alternative if NodeJS does not support RC4
 | ||||
|         obj.r = new RC4(key.toString('binary')); | ||||
|         obj.update = function (x) { return Buffer.from(obj.r.encrypt(x.toString('binary')), 'hex'); } | ||||
|     } | ||||
|     return obj; | ||||
| } | ||||
| 
 | ||||
| function create_negotiate_message() { | ||||
|     return negotiate_message( | ||||
|  | @ -377,8 +396,8 @@ function build_security_interface(ntlm) { | |||
|         obj.verify_key = sign_key(ntlm.exported_session_key, false); | ||||
|         const client_sealing_key = seal_key(ntlm.exported_session_key, true); | ||||
|         const server_sealing_key = seal_key(ntlm.exported_session_key, false); | ||||
|         obj.encrypt = crypto.createCipheriv('rc4', client_sealing_key, null); | ||||
|         obj.decrypt = crypto.createCipheriv('rc4', server_sealing_key, null); | ||||
|         obj.encrypt = createRC4(client_sealing_key); | ||||
|         obj.decrypt = createRC4(server_sealing_key); | ||||
|     } | ||||
|     obj.seq_num = 0; | ||||
| 
 | ||||
|  | @ -618,7 +637,6 @@ function read_challenge_message(ntlm, derBuffer) { | |||
|     return r; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| function unitTest() { | ||||
|     console.log('--- Starting RDP NLA Unit Tests'); | ||||
| 
 | ||||
|  | @ -669,7 +687,7 @@ function unitTest() { | |||
|     console.log(compareArray(bufToArr(r), [64, 125, 160, 17, 144, 165, 62, 226, 22, 125, 128, 31, 103, 141, 55, 40]) ? "seal_key 2 passed." : "seal_key 2 failed."); | ||||
| 
 | ||||
|     // Test signature function
 | ||||
|     var rc4 = crypto.createCipheriv('rc4', Buffer.from("foo"), null); | ||||
|     var rc4 = createRC4(Buffer.from("foo")); | ||||
|     r = mac(rc4, Buffer.from("bar"), 0, Buffer.from("data")); | ||||
|     console.log(compareArray(bufToArr(r), [1, 0, 0, 0, 77, 211, 144, 84, 51, 242, 202, 176, 0, 0, 0, 0]) ? "Signature passed." : "Signature failed."); | ||||
| 
 | ||||
|  | @ -679,7 +697,7 @@ function unitTest() { | |||
|     console.log(compareArray(bufToArr(buf), [78, 84, 76, 77, 83, 83, 80, 0, 3, 0, 0, 0, 3, 0, 3, 0, 80, 0, 0, 0, 3, 0, 3, 0, 83, 0, 0, 0, 6, 0, 6, 0, 86, 0, 0, 0, 4, 0, 4, 0, 92, 0, 0, 0, 11, 0, 11, 0, 96, 0, 0, 0, 3, 0, 3, 0, 107, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 111, 111, 102, 111, 111, 100, 111, 109, 97, 105, 110, 117, 115, 101, 114, 119, 111, 114, 107, 115, 116, 97, 116, 105, 111, 110, 102, 111, 111]) ? "Challenge message passed." : "Challenge message failed."); | ||||
| 
 | ||||
|     // Test RC4
 | ||||
|     rc4 = crypto.createCipheriv('rc4', Buffer.from("foo"), null); | ||||
|     rc4 = createRC4(Buffer.from("foo")); | ||||
|     r = rc4.update(Buffer.from("bar")); | ||||
|     console.log(compareArray(bufToArr(r), [201, 67, 159]) ? "RC4 1 passed." : "RC4 1 failed."); | ||||
|     r = rc4.update(Buffer.from("bar")); | ||||
|  |  | |||
							
								
								
									
										571
									
								
								rdp/security/md4.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										571
									
								
								rdp/security/md4.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,571 @@ | |||
| /** | ||||
|  * [js-md4]{@link https://github.com/emn178/js-md4}
 | ||||
|  * | ||||
|  * @namespace md4 | ||||
|  * @version 0.3.2 | ||||
|  * @author Yi-Cyuan Chen [emn178@gmail.com] | ||||
|  * @copyright Yi-Cyuan Chen 2015-2027 | ||||
|  * @license MIT | ||||
|  */ | ||||
| /*jslint bitwise: true */ | ||||
| (function () { | ||||
|   'use strict'; | ||||
| 
 | ||||
|   var root = typeof window === 'object' ? window : {}; | ||||
|   var NODE_JS = !root.JS_MD4_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node; | ||||
|   if (NODE_JS) { | ||||
|     root = global; | ||||
|   } | ||||
|   var COMMON_JS = !root.JS_MD4_NO_COMMON_JS && typeof module === 'object' && module.exports; | ||||
|   var AMD = typeof define === 'function' && define.amd; | ||||
|   var ARRAY_BUFFER = !root.JS_MD4_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined'; | ||||
|   var HEX_CHARS = '0123456789abcdef'.split(''); | ||||
|   var EXTRA = [128, 32768, 8388608, -2147483648]; | ||||
|   var SHIFT = [0, 8, 16, 24]; | ||||
|   var OUTPUT_TYPES = ['hex', 'array', 'digest', 'buffer', 'arrayBuffer']; | ||||
| 
 | ||||
|   var blocks = [], buffer8; | ||||
|   if (ARRAY_BUFFER) { | ||||
|     var buffer = new ArrayBuffer(68); | ||||
|     buffer8 = new Uint8Array(buffer); | ||||
|     blocks = new Uint32Array(buffer); | ||||
|   } | ||||
| 
 | ||||
|   /** | ||||
|    * @method hex | ||||
|    * @memberof md4 | ||||
|    * @description Output hash as hex string | ||||
|    * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash | ||||
|    * @returns {String} Hex string | ||||
|    * @example | ||||
|    * md4.hex('The quick brown fox jumps over the lazy dog'); | ||||
|    * // equal to
 | ||||
|    * md4('The quick brown fox jumps over the lazy dog'); | ||||
|    */ | ||||
|   /** | ||||
|    * @method digest | ||||
|    * @memberof md4 | ||||
|    * @description Output hash as bytes array | ||||
|    * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash | ||||
|    * @returns {Array} Bytes array | ||||
|    * @example | ||||
|    * md4.digest('The quick brown fox jumps over the lazy dog'); | ||||
|    */ | ||||
|   /** | ||||
|    * @method array | ||||
|    * @memberof md4 | ||||
|    * @description Output hash as bytes array | ||||
|    * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash | ||||
|    * @returns {Array} Bytes array | ||||
|    * @example | ||||
|    * md4.array('The quick brown fox jumps over the lazy dog'); | ||||
|    */ | ||||
|   /** | ||||
|    * @method buffer | ||||
|    * @memberof md4 | ||||
|    * @description Output hash as ArrayBuffer | ||||
|    * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash | ||||
|    * @returns {ArrayBuffer} ArrayBuffer | ||||
|    * @example | ||||
|    * md4.buffer('The quick brown fox jumps over the lazy dog'); | ||||
|    */ | ||||
|   var createOutputMethod = function (outputType) { | ||||
|     return function(message) { | ||||
|       return new Md4(true).update(message)[outputType](); | ||||
|     } | ||||
|   }; | ||||
| 
 | ||||
|   /** | ||||
|    * @method create | ||||
|    * @memberof md4 | ||||
|    * @description Create Md4 object | ||||
|    * @returns {Md4} MD4 object. | ||||
|    * @example | ||||
|    * var hash = md4.create(); | ||||
|    */ | ||||
|   /** | ||||
|    * @method update | ||||
|    * @memberof md4 | ||||
|    * @description Create and update Md4 object | ||||
|    * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash | ||||
|    * @returns {Md4} MD4 object. | ||||
|    * @example | ||||
|    * var hash = md4.update('The quick brown fox jumps over the lazy dog'); | ||||
|    * // equal to
 | ||||
|    * var hash = md4.create(); | ||||
|    * hash.update('The quick brown fox jumps over the lazy dog'); | ||||
|    */ | ||||
|   var createMethod = function () { | ||||
|     var method = createOutputMethod('hex'); | ||||
|     method.create = function () { | ||||
|       return new Md4(); | ||||
|     }; | ||||
|     method.update = function (message) { | ||||
|       return method.create().update(message); | ||||
|     }; | ||||
|     for (var i = 0; i < OUTPUT_TYPES.length; ++i) { | ||||
|       var type = OUTPUT_TYPES[i]; | ||||
|       method[type] = createOutputMethod(type); | ||||
|     } | ||||
|     return method; | ||||
|   }; | ||||
| 
 | ||||
|   var nodeWrap = function (method) { | ||||
|     var crypto = require('crypto'); | ||||
|     var Buffer = require('buffer').Buffer; | ||||
|     var nodeMethod = function (message) { | ||||
|       if (typeof message === 'string') { | ||||
|         return crypto.createHash('md4').update(message, 'utf8').digest('hex'); | ||||
|       } else if (ARRAY_BUFFER && message instanceof ArrayBuffer) { | ||||
|         message = new Uint8Array(message); | ||||
|       } else if (message.length === undefined) { | ||||
|         return method(message); | ||||
|       } | ||||
|       return crypto.createHash('md4').update(new Buffer(message)).digest('hex'); | ||||
|     }; | ||||
|     return nodeMethod; | ||||
|   }; | ||||
| 
 | ||||
|   /** | ||||
|    * Md4 class | ||||
|    * @class Md4 | ||||
|    * @description This is internal class. | ||||
|    * @see {@link md4.create} | ||||
|    */ | ||||
|   function Md4(sharedMemory) { | ||||
|     if (sharedMemory) { | ||||
|       blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] = | ||||
|       blocks[4] = blocks[5] = blocks[6] = blocks[7] = | ||||
|       blocks[8] = blocks[9] = blocks[10] = blocks[11] = | ||||
|       blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; | ||||
|       this.blocks = blocks; | ||||
|       this.buffer8 = buffer8; | ||||
|     } else { | ||||
|       if (ARRAY_BUFFER) { | ||||
|         var buffer = new ArrayBuffer(68); | ||||
|         this.buffer8 = new Uint8Array(buffer); | ||||
|         this.blocks = new Uint32Array(buffer); | ||||
|       } else { | ||||
|         this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||||
|       } | ||||
|     } | ||||
|     this.h0 = this.h1 = this.h2 = this.h3 = this.start = this.bytes = 0; | ||||
|     this.finalized = this.hashed = false; | ||||
|     this.first = true; | ||||
|   } | ||||
| 
 | ||||
|   /** | ||||
|    * @method update | ||||
|    * @memberof Md4 | ||||
|    * @instance | ||||
|    * @description Update hash | ||||
|    * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash | ||||
|    * @returns {Md4} MD4 object. | ||||
|    * @see {@link md4.update} | ||||
|    */ | ||||
|   Md4.prototype.update = function (message) { | ||||
|     if (this.finalized) { | ||||
|       return; | ||||
|     } | ||||
|     var notString = typeof message !== 'string'; | ||||
|     if (notString && ARRAY_BUFFER && message instanceof ArrayBuffer) { | ||||
|       message = new Uint8Array(message); | ||||
|     } | ||||
|     var code, index = 0, i, length = message.length || 0, blocks = this.blocks; | ||||
|     var buffer8 = this.buffer8; | ||||
| 
 | ||||
|     while (index < length) { | ||||
|       if (this.hashed) { | ||||
|         this.hashed = false; | ||||
|         blocks[0] = blocks[16]; | ||||
|         blocks[16] = blocks[1] = blocks[2] = blocks[3] = | ||||
|         blocks[4] = blocks[5] = blocks[6] = blocks[7] = | ||||
|         blocks[8] = blocks[9] = blocks[10] = blocks[11] = | ||||
|         blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; | ||||
|       } | ||||
| 
 | ||||
|       if (notString) { | ||||
|         if (ARRAY_BUFFER) { | ||||
|           for (i = this.start; index < length && i < 64; ++index) { | ||||
|             buffer8[i++] = message[index]; | ||||
|           } | ||||
|         } else { | ||||
|           for (i = this.start; index < length && i < 64; ++index) { | ||||
|             blocks[i >> 2] |= message[index] << SHIFT[i++ & 3]; | ||||
|           } | ||||
|         } | ||||
|       } else { | ||||
|         if (ARRAY_BUFFER) { | ||||
|           for (i = this.start; index < length && i < 64; ++index) { | ||||
|             code = message.charCodeAt(index); | ||||
|             if (code < 0x80) { | ||||
|               buffer8[i++] = code; | ||||
|             } else if (code < 0x800) { | ||||
|               buffer8[i++] = 0xc0 | (code >> 6); | ||||
|               buffer8[i++] = 0x80 | (code & 0x3f); | ||||
|             } else if (code < 0xd800 || code >= 0xe000) { | ||||
|               buffer8[i++] = 0xe0 | (code >> 12); | ||||
|               buffer8[i++] = 0x80 | ((code >> 6) & 0x3f); | ||||
|               buffer8[i++] = 0x80 | (code & 0x3f); | ||||
|             } else { | ||||
|               code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff)); | ||||
|               buffer8[i++] = 0xf0 | (code >> 18); | ||||
|               buffer8[i++] = 0x80 | ((code >> 12) & 0x3f); | ||||
|               buffer8[i++] = 0x80 | ((code >> 6) & 0x3f); | ||||
|               buffer8[i++] = 0x80 | (code & 0x3f); | ||||
|             } | ||||
|           } | ||||
|         } else { | ||||
|           for (i = this.start; index < length && i < 64; ++index) { | ||||
|             code = message.charCodeAt(index); | ||||
|             if (code < 0x80) { | ||||
|               blocks[i >> 2] |= code << SHIFT[i++ & 3]; | ||||
|             } else if (code < 0x800) { | ||||
|               blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3]; | ||||
|               blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; | ||||
|             } else if (code < 0xd800 || code >= 0xe000) { | ||||
|               blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3]; | ||||
|               blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; | ||||
|               blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; | ||||
|             } else { | ||||
|               code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff)); | ||||
|               blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3]; | ||||
|               blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3]; | ||||
|               blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; | ||||
|               blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; | ||||
|             } | ||||
|           } | ||||
|         } | ||||
|       } | ||||
|       this.lastByteIndex = i; | ||||
|       this.bytes += i - this.start; | ||||
|       if (i >= 64) { | ||||
|         this.start = i - 64; | ||||
|         this.hash(); | ||||
|         this.hashed = true; | ||||
|       } else { | ||||
|         this.start = i; | ||||
|       } | ||||
|     } | ||||
|     return this; | ||||
|   }; | ||||
| 
 | ||||
|   Md4.prototype.finalize = function () { | ||||
|     if (this.finalized) { | ||||
|       return; | ||||
|     } | ||||
|     this.finalized = true; | ||||
|     var blocks = this.blocks, i = this.lastByteIndex; | ||||
|     blocks[i >> 2] |= EXTRA[i & 3]; | ||||
|     if (i >= 56) { | ||||
|       if (!this.hashed) { | ||||
|         this.hash(); | ||||
|       } | ||||
|       blocks[0] = blocks[16]; | ||||
|       blocks[16] = blocks[1] = blocks[2] = blocks[3] = | ||||
|       blocks[4] = blocks[5] = blocks[6] = blocks[7] = | ||||
|       blocks[8] = blocks[9] = blocks[10] = blocks[11] = | ||||
|       blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0; | ||||
|     } | ||||
|     blocks[14] = this.bytes << 3; | ||||
|     this.hash(); | ||||
|   }; | ||||
| 
 | ||||
|   Md4.prototype.hash = function () { | ||||
|     var a, b, c, d, ab, bc, cd, da, blocks = this.blocks; | ||||
| 
 | ||||
|     if (this.first) { | ||||
|       a = blocks[0] - 1; | ||||
|       a = (a << 3) | (a >>> 29); | ||||
|       d = ((a & 0xefcdab89) | (~a & 0x98badcfe)) + blocks[1] + 271733878; | ||||
|       d = (d << 7) | (d >>> 25); | ||||
|       c = ((d & a) | (~d & 0xefcdab89)) + blocks[2] - 1732584194; | ||||
|       c = (c << 11) | (c >>> 21); | ||||
|       b = ((c & d) | (~c & a)) + blocks[3] - 271733879; | ||||
|       b = (b << 19) | (b >>> 13); | ||||
|     } else { | ||||
|       a = this.h0; | ||||
|       b = this.h1; | ||||
|       c = this.h2; | ||||
|       d = this.h3; | ||||
|       a += ((b & c) | (~b & d)) + blocks[0]; | ||||
|       a = (a << 3) | (a >>> 29); | ||||
|       d += ((a & b) | (~a & c)) + blocks[1]; | ||||
|       d = (d << 7) | (d >>> 25); | ||||
|       c += ((d & a) | (~d & b)) + blocks[2]; | ||||
|       c = (c << 11) | (c >>> 21); | ||||
|       b += ((c & d) | (~c & a)) + blocks[3]; | ||||
|       b = (b << 19) | (b >>> 13); | ||||
|     } | ||||
|     a += ((b & c) | (~b & d)) + blocks[4]; | ||||
|     a = (a << 3) | (a >>> 29); | ||||
|     d += ((a & b) | (~a & c)) + blocks[5]; | ||||
|     d = (d << 7) | (d >>> 25); | ||||
|     c += ((d & a) | (~d & b)) + blocks[6]; | ||||
|     c = (c << 11) | (c >>> 21); | ||||
|     b += ((c & d) | (~c & a)) + blocks[7]; | ||||
|     b = (b << 19) | (b >>> 13); | ||||
|     a += ((b & c) | (~b & d)) + blocks[8]; | ||||
|     a = (a << 3) | (a >>> 29); | ||||
|     d += ((a & b) | (~a & c)) + blocks[9]; | ||||
|     d = (d << 7) | (d >>> 25); | ||||
|     c += ((d & a) | (~d & b)) + blocks[10]; | ||||
|     c = (c << 11) | (c >>> 21); | ||||
|     b += ((c & d) | (~c & a)) + blocks[11]; | ||||
|     b = (b << 19) | (b >>> 13); | ||||
|     a += ((b & c) | (~b & d)) + blocks[12]; | ||||
|     a = (a << 3) | (a >>> 29); | ||||
|     d += ((a & b) | (~a & c)) + blocks[13]; | ||||
|     d = (d << 7) | (d >>> 25); | ||||
|     c += ((d & a) | (~d & b)) + blocks[14]; | ||||
|     c = (c << 11) | (c >>> 21); | ||||
|     b += ((c & d) | (~c & a)) + blocks[15]; | ||||
|     b = (b << 19) | (b >>> 13); | ||||
| 
 | ||||
|     bc = b & c; | ||||
|     a += (bc | (b & d) | (c & d)) + blocks[0] + 1518500249; | ||||
|     a = (a << 3) | (a >>> 29); | ||||
|     ab = a & b; | ||||
|     d += (ab | (a & c) | bc) + blocks[4] + 1518500249; | ||||
|     d = (d << 5) | (d >>> 27); | ||||
|     da = d & a; | ||||
|     c += (da | (d & b) | ab) + blocks[8] + 1518500249; | ||||
|     c = (c << 9) | (c >>> 23); | ||||
|     cd = c & d; | ||||
|     b += (cd | (c & a) | da) + blocks[12] + 1518500249; | ||||
|     b = (b << 13) | (b >>> 19); | ||||
|     bc = b & c; | ||||
|     a += (bc | (b & d) | cd) + blocks[1] + 1518500249; | ||||
|     a = (a << 3) | (a >>> 29); | ||||
|     ab = a & b; | ||||
|     d += (ab | (a & c) | bc) + blocks[5] + 1518500249; | ||||
|     d = (d << 5) | (d >>> 27); | ||||
|     da = d & a; | ||||
|     c += (da | (d & b) | ab) + blocks[9] + 1518500249; | ||||
|     c = (c << 9) | (c >>> 23); | ||||
|     cd = c & d; | ||||
|     b += (cd | (c & a) | da) + blocks[13] + 1518500249; | ||||
|     b = (b << 13) | (b >>> 19); | ||||
|     bc = b & c; | ||||
|     a += (bc | (b & d) | cd) + blocks[2] + 1518500249; | ||||
|     a = (a << 3) | (a >>> 29); | ||||
|     ab = a & b; | ||||
|     d += (ab | (a & c) | bc) + blocks[6] + 1518500249; | ||||
|     d = (d << 5) | (d >>> 27); | ||||
|     da = d & a; | ||||
|     c += (da | (d & b) | ab) + blocks[10] + 1518500249; | ||||
|     c = (c << 9) | (c >>> 23); | ||||
|     cd = c & d; | ||||
|     b += (cd | (c & a) | da) + blocks[14] + 1518500249; | ||||
|     b = (b << 13) | (b >>> 19); | ||||
|     bc = b & c; | ||||
|     a += (bc | (b & d) | cd) + blocks[3] + 1518500249; | ||||
|     a = (a << 3) | (a >>> 29); | ||||
|     ab = a & b; | ||||
|     d += (ab | (a & c) | bc) + blocks[7] + 1518500249; | ||||
|     d = (d << 5) | (d >>> 27); | ||||
|     da = d & a; | ||||
|     c += (da | (d & b) | ab) + blocks[11] + 1518500249; | ||||
|     c = (c << 9) | (c >>> 23); | ||||
|     b += ((c & d) | (c & a) | da) + blocks[15] + 1518500249; | ||||
|     b = (b << 13) | (b >>> 19); | ||||
| 
 | ||||
|     bc = b ^ c; | ||||
|     a += (bc ^ d) + blocks[0] + 1859775393; | ||||
|     a = (a << 3) | (a >>> 29); | ||||
|     d += (bc ^ a) + blocks[8] + 1859775393; | ||||
|     d = (d << 9) | (d >>> 23); | ||||
|     da = d ^ a; | ||||
|     c += (da ^ b) + blocks[4] + 1859775393; | ||||
|     c = (c << 11) | (c >>> 21); | ||||
|     b += (da ^ c) + blocks[12] + 1859775393; | ||||
|     b = (b << 15) | (b >>> 17); | ||||
|     bc = b ^ c; | ||||
|     a += (bc ^ d) + blocks[2] + 1859775393; | ||||
|     a = (a << 3) | (a >>> 29); | ||||
|     d += (bc ^ a) + blocks[10] + 1859775393; | ||||
|     d = (d << 9) | (d >>> 23); | ||||
|     da = d ^ a; | ||||
|     c += (da ^ b) + blocks[6] + 1859775393; | ||||
|     c = (c << 11) | (c >>> 21); | ||||
|     b += (da ^ c) + blocks[14] + 1859775393; | ||||
|     b = (b << 15) | (b >>> 17); | ||||
|     bc = b ^ c; | ||||
|     a += (bc ^ d) + blocks[1] + 1859775393; | ||||
|     a = (a << 3) | (a >>> 29); | ||||
|     d += (bc ^ a) + blocks[9] + 1859775393; | ||||
|     d = (d << 9) | (d >>> 23); | ||||
|     da = d ^ a; | ||||
|     c += (da ^ b) + blocks[5] + 1859775393; | ||||
|     c = (c << 11) | (c >>> 21); | ||||
|     b += (da ^ c) + blocks[13] + 1859775393; | ||||
|     b = (b << 15) | (b >>> 17); | ||||
|     bc = b ^ c; | ||||
|     a += (bc ^ d) + blocks[3] + 1859775393; | ||||
|     a = (a << 3) | (a >>> 29); | ||||
|     d += (bc ^ a) + blocks[11] + 1859775393; | ||||
|     d = (d << 9) | (d >>> 23); | ||||
|     da = d ^ a; | ||||
|     c += (da ^ b) + blocks[7] + 1859775393; | ||||
|     c = (c << 11) | (c >>> 21); | ||||
|     b += (da ^ c) + blocks[15] + 1859775393; | ||||
|     b = (b << 15) | (b >>> 17); | ||||
| 
 | ||||
|     if (this.first) { | ||||
|       this.h0 = a + 1732584193 << 0; | ||||
|       this.h1 = b - 271733879 << 0; | ||||
|       this.h2 = c - 1732584194 << 0; | ||||
|       this.h3 = d + 271733878 << 0; | ||||
|       this.first = false; | ||||
|     } else { | ||||
|       this.h0 = this.h0 + a << 0; | ||||
|       this.h1 = this.h1 + b << 0; | ||||
|       this.h2 = this.h2 + c << 0; | ||||
|       this.h3 = this.h3 + d << 0; | ||||
|     } | ||||
|   }; | ||||
| 
 | ||||
|   /** | ||||
|    * @method hex | ||||
|    * @memberof Md4 | ||||
|    * @instance | ||||
|    * @description Output hash as hex string | ||||
|    * @returns {String} Hex string | ||||
|    * @see {@link md4.hex} | ||||
|    * @example | ||||
|    * hash.hex(); | ||||
|    */ | ||||
|   Md4.prototype.hex = function () { | ||||
|     this.finalize(); | ||||
| 
 | ||||
|     var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3; | ||||
| 
 | ||||
|     return HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] + | ||||
|       HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] + | ||||
|       HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] + | ||||
|       HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] + | ||||
|       HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] + | ||||
|       HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] + | ||||
|       HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] + | ||||
|       HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] + | ||||
|       HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] + | ||||
|       HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] + | ||||
|       HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] + | ||||
|       HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] + | ||||
|       HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] + | ||||
|       HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] + | ||||
|       HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] + | ||||
|       HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F]; | ||||
|   }; | ||||
| 
 | ||||
|   /** | ||||
|    * @method toString | ||||
|    * @memberof Md4 | ||||
|    * @instance | ||||
|    * @description Output hash as hex string | ||||
|    * @returns {String} Hex string | ||||
|    * @see {@link md4.hex} | ||||
|    * @example | ||||
|    * hash.toString(); | ||||
|    */ | ||||
|   Md4.prototype.toString = Md4.prototype.hex; | ||||
| 
 | ||||
|   /** | ||||
|    * @method digest | ||||
|    * @memberof Md4 | ||||
|    * @instance | ||||
|    * @description Output hash as bytes array | ||||
|    * @returns {Array} Bytes array | ||||
|    * @see {@link md4.digest} | ||||
|    * @example | ||||
|    * hash.digest(); | ||||
|    */ | ||||
|   Md4.prototype.digest = function() { | ||||
|     this.finalize(); | ||||
| 
 | ||||
|     var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3; | ||||
|     return [ | ||||
|       h0 & 0xFF, (h0 >> 8) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 24) & 0xFF, | ||||
|       h1 & 0xFF, (h1 >> 8) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 24) & 0xFF, | ||||
|       h2 & 0xFF, (h2 >> 8) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 24) & 0xFF, | ||||
|       h3 & 0xFF, (h3 >> 8) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 24) & 0xFF | ||||
|     ]; | ||||
|   }; | ||||
| 
 | ||||
|   /** | ||||
|    * @method array | ||||
|    * @memberof Md4 | ||||
|    * @instance | ||||
|    * @description Output hash as bytes array | ||||
|    * @returns {Array} Bytes array | ||||
|    * @see {@link md4.array} | ||||
|    * @example | ||||
|    * hash.array(); | ||||
|    */ | ||||
|   Md4.prototype.array = Md4.prototype.digest; | ||||
| 
 | ||||
|   /** | ||||
|    * @method arrayBuffer | ||||
|    * @memberof Md4 | ||||
|    * @instance | ||||
|    * @description Output hash as ArrayBuffer | ||||
|    * @returns {ArrayBuffer} ArrayBuffer | ||||
|    * @see {@link md4.arrayBuffer} | ||||
|    * @example | ||||
|    * hash.arrayBuffer(); | ||||
|    */ | ||||
|   Md4.prototype.arrayBuffer = function() { | ||||
|     this.finalize(); | ||||
| 
 | ||||
|     var buffer = new ArrayBuffer(16); | ||||
|     var blocks = new Uint32Array(buffer); | ||||
|     blocks[0] = this.h0; | ||||
|     blocks[1] = this.h1; | ||||
|     blocks[2] = this.h2; | ||||
|     blocks[3] = this.h3; | ||||
|     return buffer; | ||||
|   }; | ||||
| 
 | ||||
|   /** | ||||
|    * @method buffer | ||||
|    * @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead. | ||||
|    * @memberof Md4 | ||||
|    * @instance | ||||
|    * @description Output hash as ArrayBuffer | ||||
|    * @returns {ArrayBuffer} ArrayBuffer | ||||
|    * @see {@link md4.buffer} | ||||
|    * @example | ||||
|    * hash.buffer(); | ||||
|    */ | ||||
|   Md4.prototype.buffer = Md4.prototype.arrayBuffer; | ||||
| 
 | ||||
|   var exports = createMethod(); | ||||
| 
 | ||||
|   if (COMMON_JS) { | ||||
|     module.exports = exports; | ||||
|   } else { | ||||
|     /** | ||||
|      * @method md4 | ||||
|      * @description MD4 hash function, export to global in browsers. | ||||
|      * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash | ||||
|      * @returns {String} md4 hashes | ||||
|      * @example | ||||
|      * md4(''); // 31d6cfe0d16ae931b73c59d7e0c089c0
 | ||||
|      * md4('The quick brown fox jumps over the lazy dog'); // 1bee69a46ba811185c194762abaeae90
 | ||||
|      * md4('The quick brown fox jumps over the lazy dog.'); // 2812c6c7136898c51f6f6739ad08750e
 | ||||
|      * | ||||
|      * // It also supports UTF-8 encoding
 | ||||
|      * md4('中文'); // 223088bf7bd45a16436b15360c5fc5a0
 | ||||
|      * | ||||
|      * // It also supports byte `Array`, `Uint8Array`, `ArrayBuffer`
 | ||||
|      * md4([]); // 31d6cfe0d16ae931b73c59d7e0c089c0
 | ||||
|      * md4(new Uint8Array([])); // 31d6cfe0d16ae931b73c59d7e0c089c0
 | ||||
|      */ | ||||
|     root.md4 = exports; | ||||
|     if (AMD) { | ||||
|       define(function () { | ||||
|         return exports; | ||||
|       }); | ||||
|     } | ||||
|   } | ||||
| })(); | ||||
							
								
								
									
										70
									
								
								rdp/security/rc4.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								rdp/security/rc4.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,70 @@ | |||
| 
 | ||||
| function RC4(key) { | ||||
|     this.privateKey = keySetup(key); | ||||
|     this.byteStream = byteStreamGenerator(this.privateKey.slice(0)); | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * Converts the text into an array of the characters numeric Unicode values | ||||
|  * @param  {String} text, the text to convert | ||||
|  * @return {Array} the array of Unicode values | ||||
|  */ | ||||
| function convert(text) { | ||||
|     var codes = []; | ||||
|     for (var i = 0, ii = text.length; i < ii; i++) { codes.push(text.charCodeAt(i)); } | ||||
|     return codes; | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * Sets up the key to use with the byte stream | ||||
|  * @param  {String} key, The key that you want to use | ||||
|  * @return {Array}, the key stream which with be used in the byteStreamGenerator | ||||
|  */ | ||||
| function keySetup(key) { | ||||
|     var K = [...Array(256).keys()], j = 0, key = convert(key); | ||||
|     for (var i = 0, ii = K.length; i < ii; i++) { | ||||
|         j = (j + K[i] + key[i % key.length]) % 256; | ||||
|         [K[i], K[j]] = [K[j], K[i]]; | ||||
|     } | ||||
|     return K; | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * byteStreamGenerator uses ES6 generators which will be 'XOR-ed' to encrypt and decrypt | ||||
|  * @param {Array} K, the array generated from the keySetup | ||||
|  * @yield {Integer}, the current value which will be 'XOR-ed' to encrypt or decrypt | ||||
|  */ | ||||
| var byteStreamGenerator = function* (K) { | ||||
|     var i = 0, j = 0; | ||||
|     while (true) { | ||||
|         i = (i + 1) % 256; | ||||
|         j = (j + K[i]) % 256; | ||||
|         [K[i], K[j]] = [K[j], K[i]]; | ||||
|         yield (K[(K[i] + K[j]) % 256]); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * Encrypts the input text | ||||
|  * @param  {String} input, the text to encrypt | ||||
|  * @return {String}, the encrypted text | ||||
|  */ | ||||
| RC4.prototype.encrypt = function (input) { | ||||
|     var outputText = ''; | ||||
|     for (var i = 0, ii = input.length; i < ii; i++) { outputText += ('00' + (input.charCodeAt(i) ^ this.byteStream.next().value).toString(16)).substr(-2); } | ||||
|     return outputText; | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * Decrypts the input text | ||||
|  * @param  {String} input, the text to decrypt | ||||
|  * @return {String}, the decrypted text (if the same key was used) | ||||
|  */ | ||||
| RC4.prototype.decrypt = function (input) { | ||||
|     var outputText = ''; | ||||
|     input = input.match(/[a-z0-9]{2}/gi); | ||||
|     for (var i = 0, ii = input.length; i < ii; i++) { outputText += String.fromCharCode((parseInt(input[i], 16) ^ byteStream.next().value)); } | ||||
|     return outputText; | ||||
| } | ||||
| 
 | ||||
| module.exports = RC4; | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue