Added RC4/MD4 alternatives for NodeJS v17, #
Ylian Saint-Hilaire committed
May 1, 2022 at 02:02 UTC
b77fe3da661fc3ca03ffa66eeb2ca82873934b58
4 files changed
+668
-7
MeshCentralServer.njsproj
+2
@@ -211,6 +211,8 @@
211
<Compile Include="rdp\protocol\x224.js" />
212
<Compile Include="rdp\security\index.js" />
213
<Compile Include="rdp\security\jsbn.js" />
214
+ <Compile Include="rdp\security\md4.js" />
215
+ <Compile Include="rdp\security\rc4.js" />
216
<Compile Include="rdp\security\rsa.js" />
217
<Compile Include="rdp\security\x509.js" />
218
<Compile Include="swarmserver.js" />
rdp/protocol/nla.js
+25
-7
@@ -273,7 +273,13 @@ function decodeTargetInfo(targetInfoBuf) {
273
function bufToArr(b) { var r = []; for (var i = 0; i < b.length; i++) { r.push(b.readUInt8(i)); } return r; } // For unit testing
274
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
275
function toUnicode(str) { return Buffer.from(str, 'ucs2'); }
276
-function md4(str) { return crypto.createHash('md4').update(str).digest(); }
276
+function md4(buffer) {
277
+ try {
278
+ return crypto.createHash('md4').update(buffer).digest(); // Built in NodeJS MD4, this does not work starting with NodeJS v17
279
+ } catch (ex) {
280
+ return Buffer.from(require('../security/md4').array(buffer.toString('binary'))); // This is the alternative if NodeJS does not support MD4
281
+ }
282
+}
283
function md5(str) { return crypto.createHash('md5').update(str).digest(); }
284
function hmac_md5(key, data) { return crypto.createHmac('md5', key).update(data).digest(); }
285
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,
296
return [nt_challenge_response, lm_challenge_response, session_base_key];
297
}
298
function kx_key_v2(session_base_key, _lm_challenge_response, _server_challenge) { return session_base_key; }
293
-function rc4k(key, data) { return crypto.createCipheriv('rc4', key, null).update(data); }
299
+function rc4k(key, data) { return createRC4(key).update(data); }
300
+
301
+function createRC4(key) {
302
+ const obj = {};
303
+ try {
304
+ obj.n = crypto.createCipheriv('rc4', key, null); // Built in NodeJS RC4, this does not work starting with NodeJS v17
305
+ obj.update = function(x) { return obj.n.update(x); }
306
+ } catch (ex) {
307
+ const RC4 = require('../security/rc4'); // This is the alternative if NodeJS does not support RC4
308
+ obj.r = new RC4(key.toString('binary'));
309
+ obj.update = function (x) { return Buffer.from(obj.r.encrypt(x.toString('binary')), 'hex'); }
310
+ }
311
+ return obj;
312
+}
313
314
function create_negotiate_message() {
315
return negotiate_message(
@@ -377,8 +396,8 @@ function build_security_interface(ntlm) {
396
obj.verify_key = sign_key(ntlm.exported_session_key, false);
397
const client_sealing_key = seal_key(ntlm.exported_session_key, true);
398
const server_sealing_key = seal_key(ntlm.exported_session_key, false);
380
- obj.encrypt = crypto.createCipheriv('rc4', client_sealing_key, null);
381
- obj.decrypt = crypto.createCipheriv('rc4', server_sealing_key, null);
399
+ obj.encrypt = createRC4(client_sealing_key);
400
+ obj.decrypt = createRC4(server_sealing_key);
401
}
402
obj.seq_num = 0;
403
@@ -618,7 +637,6 @@ function read_challenge_message(ntlm, derBuffer) {
637
return r;
638
}
639
621
-
640
function unitTest() {
641
console.log('--- Starting RDP NLA Unit Tests');
642
@@ -669,7 +687,7 @@ function unitTest() {
687
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.");
688
689
// Test signature function
672
- var rc4 = crypto.createCipheriv('rc4', Buffer.from("foo"), null);
690
+ var rc4 = createRC4(Buffer.from("foo"));
691
r = mac(rc4, Buffer.from("bar"), 0, Buffer.from("data"));
692
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.");
693
@@ -679,7 +697,7 @@ function unitTest() {
697
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.");
698
699
// Test RC4
682
- rc4 = crypto.createCipheriv('rc4', Buffer.from("foo"), null);
700
+ rc4 = createRC4(Buffer.from("foo"));
701
r = rc4.update(Buffer.from("bar"));
702
console.log(compareArray(bufToArr(r), [201, 67, 159]) ? "RC4 1 passed." : "RC4 1 failed.");
703
r = rc4.update(Buffer.from("bar"));
rdp/security/md4.js
new
+571
@@ -0,0 +1,571 @@
1
+/**
2
+ * [js-md4]{@link https://github.com/emn178/js-md4}
3
+ *
4
+ * @namespace md4
5
+ * @version 0.3.2
6
+ * @author Yi-Cyuan Chen [emn178@gmail.com]
7
+ * @copyright Yi-Cyuan Chen 2015-2027
8
+ * @license MIT
9
+ */
10
+/*jslint bitwise: true */
11
+(function () {
12
+ 'use strict';
13
+
14
+ var root = typeof window === 'object' ? window : {};
15
+ var NODE_JS = !root.JS_MD4_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
16
+ if (NODE_JS) {
17
+ root = global;
18
+ }
19
+ var COMMON_JS = !root.JS_MD4_NO_COMMON_JS && typeof module === 'object' && module.exports;
20
+ var AMD = typeof define === 'function' && define.amd;
21
+ var ARRAY_BUFFER = !root.JS_MD4_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
22
+ var HEX_CHARS = '0123456789abcdef'.split('');
23
+ var EXTRA = [128, 32768, 8388608, -2147483648];
24
+ var SHIFT = [0, 8, 16, 24];
25
+ var OUTPUT_TYPES = ['hex', 'array', 'digest', 'buffer', 'arrayBuffer'];
26
+
27
+ var blocks = [], buffer8;
28
+ if (ARRAY_BUFFER) {
29
+ var buffer = new ArrayBuffer(68);
30
+ buffer8 = new Uint8Array(buffer);
31
+ blocks = new Uint32Array(buffer);
32
+ }
33
+
34
+ /**
35
+ * @method hex
36
+ * @memberof md4
37
+ * @description Output hash as hex string
38
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
39
+ * @returns {String} Hex string
40
+ * @example
41
+ * md4.hex('The quick brown fox jumps over the lazy dog');
42
+ * // equal to
43
+ * md4('The quick brown fox jumps over the lazy dog');
44
+ */
45
+ /**
46
+ * @method digest
47
+ * @memberof md4
48
+ * @description Output hash as bytes array
49
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
50
+ * @returns {Array} Bytes array
51
+ * @example
52
+ * md4.digest('The quick brown fox jumps over the lazy dog');
53
+ */
54
+ /**
55
+ * @method array
56
+ * @memberof md4
57
+ * @description Output hash as bytes array
58
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
59
+ * @returns {Array} Bytes array
60
+ * @example
61
+ * md4.array('The quick brown fox jumps over the lazy dog');
62
+ */
63
+ /**
64
+ * @method buffer
65
+ * @memberof md4
66
+ * @description Output hash as ArrayBuffer
67
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
68
+ * @returns {ArrayBuffer} ArrayBuffer
69
+ * @example
70
+ * md4.buffer('The quick brown fox jumps over the lazy dog');
71
+ */
72
+ var createOutputMethod = function (outputType) {
73
+ return function(message) {
74
+ return new Md4(true).update(message)[outputType]();
75
+ }
76
+ };
77
+
78
+ /**
79
+ * @method create
80
+ * @memberof md4
81
+ * @description Create Md4 object
82
+ * @returns {Md4} MD4 object.
83
+ * @example
84
+ * var hash = md4.create();
85
+ */
86
+ /**
87
+ * @method update
88
+ * @memberof md4
89
+ * @description Create and update Md4 object
90
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
91
+ * @returns {Md4} MD4 object.
92
+ * @example
93
+ * var hash = md4.update('The quick brown fox jumps over the lazy dog');
94
+ * // equal to
95
+ * var hash = md4.create();
96
+ * hash.update('The quick brown fox jumps over the lazy dog');
97
+ */
98
+ var createMethod = function () {
99
+ var method = createOutputMethod('hex');
100
+ method.create = function () {
101
+ return new Md4();
102
+ };
103
+ method.update = function (message) {
104
+ return method.create().update(message);
105
+ };
106
+ for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
107
+ var type = OUTPUT_TYPES[i];
108
+ method[type] = createOutputMethod(type);
109
+ }
110
+ return method;
111
+ };
112
+
113
+ var nodeWrap = function (method) {
114
+ var crypto = require('crypto');
115
+ var Buffer = require('buffer').Buffer;
116
+ var nodeMethod = function (message) {
117
+ if (typeof message === 'string') {
118
+ return crypto.createHash('md4').update(message, 'utf8').digest('hex');
119
+ } else if (ARRAY_BUFFER && message instanceof ArrayBuffer) {
120
+ message = new Uint8Array(message);
121
+ } else if (message.length === undefined) {
122
+ return method(message);
123
+ }
124
+ return crypto.createHash('md4').update(new Buffer(message)).digest('hex');
125
+ };
126
+ return nodeMethod;
127
+ };
128
+
129
+ /**
130
+ * Md4 class
131
+ * @class Md4
132
+ * @description This is internal class.
133
+ * @see {@link md4.create}
134
+ */
135
+ function Md4(sharedMemory) {
136
+ if (sharedMemory) {
137
+ blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
138
+ blocks[4] = blocks[5] = blocks[6] = blocks[7] =
139
+ blocks[8] = blocks[9] = blocks[10] = blocks[11] =
140
+ blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
141
+ this.blocks = blocks;
142
+ this.buffer8 = buffer8;
143
+ } else {
144
+ if (ARRAY_BUFFER) {
145
+ var buffer = new ArrayBuffer(68);
146
+ this.buffer8 = new Uint8Array(buffer);
147
+ this.blocks = new Uint32Array(buffer);
148
+ } else {
149
+ this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
150
+ }
151
+ }
152
+ this.h0 = this.h1 = this.h2 = this.h3 = this.start = this.bytes = 0;
153
+ this.finalized = this.hashed = false;
154
+ this.first = true;
155
+ }
156
+
157
+ /**
158
+ * @method update
159
+ * @memberof Md4
160
+ * @instance
161
+ * @description Update hash
162
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
163
+ * @returns {Md4} MD4 object.
164
+ * @see {@link md4.update}
165
+ */
166
+ Md4.prototype.update = function (message) {
167
+ if (this.finalized) {
168
+ return;
169
+ }
170
+ var notString = typeof message !== 'string';
171
+ if (notString && ARRAY_BUFFER && message instanceof ArrayBuffer) {
172
+ message = new Uint8Array(message);
173
+ }
174
+ var code, index = 0, i, length = message.length || 0, blocks = this.blocks;
175
+ var buffer8 = this.buffer8;
176
+
177
+ while (index < length) {
178
+ if (this.hashed) {
179
+ this.hashed = false;
180
+ blocks[0] = blocks[16];
181
+ blocks[16] = blocks[1] = blocks[2] = blocks[3] =
182
+ blocks[4] = blocks[5] = blocks[6] = blocks[7] =
183
+ blocks[8] = blocks[9] = blocks[10] = blocks[11] =
184
+ blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
185
+ }
186
+
187
+ if (notString) {
188
+ if (ARRAY_BUFFER) {
189
+ for (i = this.start; index < length && i < 64; ++index) {
190
+ buffer8[i++] = message[index];
191
+ }
192
+ } else {
193
+ for (i = this.start; index < length && i < 64; ++index) {
194
+ blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
195
+ }
196
+ }
197
+ } else {
198
+ if (ARRAY_BUFFER) {
199
+ for (i = this.start; index < length && i < 64; ++index) {
200
+ code = message.charCodeAt(index);
201
+ if (code < 0x80) {
202
+ buffer8[i++] = code;
203
+ } else if (code < 0x800) {
204
+ buffer8[i++] = 0xc0 | (code >> 6);
205
+ buffer8[i++] = 0x80 | (code & 0x3f);
206
+ } else if (code < 0xd800 || code >= 0xe000) {
207
+ buffer8[i++] = 0xe0 | (code >> 12);
208
+ buffer8[i++] = 0x80 | ((code >> 6) & 0x3f);
209
+ buffer8[i++] = 0x80 | (code & 0x3f);
210
+ } else {
211
+ code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
212
+ buffer8[i++] = 0xf0 | (code >> 18);
213
+ buffer8[i++] = 0x80 | ((code >> 12) & 0x3f);
214
+ buffer8[i++] = 0x80 | ((code >> 6) & 0x3f);
215
+ buffer8[i++] = 0x80 | (code & 0x3f);
216
+ }
217
+ }
218
+ } else {
219
+ for (i = this.start; index < length && i < 64; ++index) {
220
+ code = message.charCodeAt(index);
221
+ if (code < 0x80) {
222
+ blocks[i >> 2] |= code << SHIFT[i++ & 3];
223
+ } else if (code < 0x800) {
224
+ blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
225
+ blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
226
+ } else if (code < 0xd800 || code >= 0xe000) {
227
+ blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
228
+ blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
229
+ blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
230
+ } else {
231
+ code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
232
+ blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
233
+ blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
234
+ blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
235
+ blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
236
+ }
237
+ }
238
+ }
239
+ }
240
+ this.lastByteIndex = i;
241
+ this.bytes += i - this.start;
242
+ if (i >= 64) {
243
+ this.start = i - 64;
244
+ this.hash();
245
+ this.hashed = true;
246
+ } else {
247
+ this.start = i;
248
+ }
249
+ }
250
+ return this;
251
+ };
252
+
253
+ Md4.prototype.finalize = function () {
254
+ if (this.finalized) {
255
+ return;
256
+ }
257
+ this.finalized = true;
258
+ var blocks = this.blocks, i = this.lastByteIndex;
259
+ blocks[i >> 2] |= EXTRA[i & 3];
260
+ if (i >= 56) {
261
+ if (!this.hashed) {
262
+ this.hash();
263
+ }
264
+ blocks[0] = blocks[16];
265
+ blocks[16] = blocks[1] = blocks[2] = blocks[3] =
266
+ blocks[4] = blocks[5] = blocks[6] = blocks[7] =
267
+ blocks[8] = blocks[9] = blocks[10] = blocks[11] =
268
+ blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
269
+ }
270
+ blocks[14] = this.bytes << 3;
271
+ this.hash();
272
+ };
273
+
274
+ Md4.prototype.hash = function () {
275
+ var a, b, c, d, ab, bc, cd, da, blocks = this.blocks;
276
+
277
+ if (this.first) {
278
+ a = blocks[0] - 1;
279
+ a = (a << 3) | (a >>> 29);
280
+ d = ((a & 0xefcdab89) | (~a & 0x98badcfe)) + blocks[1] + 271733878;
281
+ d = (d << 7) | (d >>> 25);
282
+ c = ((d & a) | (~d & 0xefcdab89)) + blocks[2] - 1732584194;
283
+ c = (c << 11) | (c >>> 21);
284
+ b = ((c & d) | (~c & a)) + blocks[3] - 271733879;
285
+ b = (b << 19) | (b >>> 13);
286
+ } else {
287
+ a = this.h0;
288
+ b = this.h1;
289
+ c = this.h2;
290
+ d = this.h3;
291
+ a += ((b & c) | (~b & d)) + blocks[0];
292
+ a = (a << 3) | (a >>> 29);
293
+ d += ((a & b) | (~a & c)) + blocks[1];
294
+ d = (d << 7) | (d >>> 25);
295
+ c += ((d & a) | (~d & b)) + blocks[2];
296
+ c = (c << 11) | (c >>> 21);
297
+ b += ((c & d) | (~c & a)) + blocks[3];
298
+ b = (b << 19) | (b >>> 13);
299
+ }
300
+ a += ((b & c) | (~b & d)) + blocks[4];
301
+ a = (a << 3) | (a >>> 29);
302
+ d += ((a & b) | (~a & c)) + blocks[5];
303
+ d = (d << 7) | (d >>> 25);
304
+ c += ((d & a) | (~d & b)) + blocks[6];
305
+ c = (c << 11) | (c >>> 21);
306
+ b += ((c & d) | (~c & a)) + blocks[7];
307
+ b = (b << 19) | (b >>> 13);
308
+ a += ((b & c) | (~b & d)) + blocks[8];
309
+ a = (a << 3) | (a >>> 29);
310
+ d += ((a & b) | (~a & c)) + blocks[9];
311
+ d = (d << 7) | (d >>> 25);
312
+ c += ((d & a) | (~d & b)) + blocks[10];
313
+ c = (c << 11) | (c >>> 21);
314
+ b += ((c & d) | (~c & a)) + blocks[11];
315
+ b = (b << 19) | (b >>> 13);
316
+ a += ((b & c) | (~b & d)) + blocks[12];
317
+ a = (a << 3) | (a >>> 29);
318
+ d += ((a & b) | (~a & c)) + blocks[13];
319
+ d = (d << 7) | (d >>> 25);
320
+ c += ((d & a) | (~d & b)) + blocks[14];
321
+ c = (c << 11) | (c >>> 21);
322
+ b += ((c & d) | (~c & a)) + blocks[15];
323
+ b = (b << 19) | (b >>> 13);
324
+
325
+ bc = b & c;
326
+ a += (bc | (b & d) | (c & d)) + blocks[0] + 1518500249;
327
+ a = (a << 3) | (a >>> 29);
328
+ ab = a & b;
329
+ d += (ab | (a & c) | bc) + blocks[4] + 1518500249;
330
+ d = (d << 5) | (d >>> 27);
331
+ da = d & a;
332
+ c += (da | (d & b) | ab) + blocks[8] + 1518500249;
333
+ c = (c << 9) | (c >>> 23);
334
+ cd = c & d;
335
+ b += (cd | (c & a) | da) + blocks[12] + 1518500249;
336
+ b = (b << 13) | (b >>> 19);
337
+ bc = b & c;
338
+ a += (bc | (b & d) | cd) + blocks[1] + 1518500249;
339
+ a = (a << 3) | (a >>> 29);
340
+ ab = a & b;
341
+ d += (ab | (a & c) | bc) + blocks[5] + 1518500249;
342
+ d = (d << 5) | (d >>> 27);
343
+ da = d & a;
344
+ c += (da | (d & b) | ab) + blocks[9] + 1518500249;
345
+ c = (c << 9) | (c >>> 23);
346
+ cd = c & d;
347
+ b += (cd | (c & a) | da) + blocks[13] + 1518500249;
348
+ b = (b << 13) | (b >>> 19);
349
+ bc = b & c;
350
+ a += (bc | (b & d) | cd) + blocks[2] + 1518500249;
351
+ a = (a << 3) | (a >>> 29);
352
+ ab = a & b;
353
+ d += (ab | (a & c) | bc) + blocks[6] + 1518500249;
354
+ d = (d << 5) | (d >>> 27);
355
+ da = d & a;
356
+ c += (da | (d & b) | ab) + blocks[10] + 1518500249;
357
+ c = (c << 9) | (c >>> 23);
358
+ cd = c & d;
359
+ b += (cd | (c & a) | da) + blocks[14] + 1518500249;
360
+ b = (b << 13) | (b >>> 19);
361
+ bc = b & c;
362
+ a += (bc | (b & d) | cd) + blocks[3] + 1518500249;
363
+ a = (a << 3) | (a >>> 29);
364
+ ab = a & b;
365
+ d += (ab | (a & c) | bc) + blocks[7] + 1518500249;
366
+ d = (d << 5) | (d >>> 27);
367
+ da = d & a;
368
+ c += (da | (d & b) | ab) + blocks[11] + 1518500249;
369
+ c = (c << 9) | (c >>> 23);
370
+ b += ((c & d) | (c & a) | da) + blocks[15] + 1518500249;
371
+ b = (b << 13) | (b >>> 19);
372
+
373
+ bc = b ^ c;
374
+ a += (bc ^ d) + blocks[0] + 1859775393;
375
+ a = (a << 3) | (a >>> 29);
376
+ d += (bc ^ a) + blocks[8] + 1859775393;
377
+ d = (d << 9) | (d >>> 23);
378
+ da = d ^ a;
379
+ c += (da ^ b) + blocks[4] + 1859775393;
380
+ c = (c << 11) | (c >>> 21);
381
+ b += (da ^ c) + blocks[12] + 1859775393;
382
+ b = (b << 15) | (b >>> 17);
383
+ bc = b ^ c;
384
+ a += (bc ^ d) + blocks[2] + 1859775393;
385
+ a = (a << 3) | (a >>> 29);
386
+ d += (bc ^ a) + blocks[10] + 1859775393;
387
+ d = (d << 9) | (d >>> 23);
388
+ da = d ^ a;
389
+ c += (da ^ b) + blocks[6] + 1859775393;
390
+ c = (c << 11) | (c >>> 21);
391
+ b += (da ^ c) + blocks[14] + 1859775393;
392
+ b = (b << 15) | (b >>> 17);
393
+ bc = b ^ c;
394
+ a += (bc ^ d) + blocks[1] + 1859775393;
395
+ a = (a << 3) | (a >>> 29);
396
+ d += (bc ^ a) + blocks[9] + 1859775393;
397
+ d = (d << 9) | (d >>> 23);
398
+ da = d ^ a;
399
+ c += (da ^ b) + blocks[5] + 1859775393;
400
+ c = (c << 11) | (c >>> 21);
401
+ b += (da ^ c) + blocks[13] + 1859775393;
402
+ b = (b << 15) | (b >>> 17);
403
+ bc = b ^ c;
404
+ a += (bc ^ d) + blocks[3] + 1859775393;
405
+ a = (a << 3) | (a >>> 29);
406
+ d += (bc ^ a) + blocks[11] + 1859775393;
407
+ d = (d << 9) | (d >>> 23);
408
+ da = d ^ a;
409
+ c += (da ^ b) + blocks[7] + 1859775393;
410
+ c = (c << 11) | (c >>> 21);
411
+ b += (da ^ c) + blocks[15] + 1859775393;
412
+ b = (b << 15) | (b >>> 17);
413
+
414
+ if (this.first) {
415
+ this.h0 = a + 1732584193 << 0;
416
+ this.h1 = b - 271733879 << 0;
417
+ this.h2 = c - 1732584194 << 0;
418
+ this.h3 = d + 271733878 << 0;
419
+ this.first = false;
420
+ } else {
421
+ this.h0 = this.h0 + a << 0;
422
+ this.h1 = this.h1 + b << 0;
423
+ this.h2 = this.h2 + c << 0;
424
+ this.h3 = this.h3 + d << 0;
425
+ }
426
+ };
427
+
428
+ /**
429
+ * @method hex
430
+ * @memberof Md4
431
+ * @instance
432
+ * @description Output hash as hex string
433
+ * @returns {String} Hex string
434
+ * @see {@link md4.hex}
435
+ * @example
436
+ * hash.hex();
437
+ */
438
+ Md4.prototype.hex = function () {
439
+ this.finalize();
440
+
441
+ var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3;
442
+
443
+ return HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
444
+ HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] +
445
+ HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] +
446
+ HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] +
447
+ HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
448
+ HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] +
449
+ HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] +
450
+ HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] +
451
+ HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
452
+ HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] +
453
+ HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] +
454
+ HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] +
455
+ HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
456
+ HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] +
457
+ HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] +
458
+ HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F];
459
+ };
460
+
461
+ /**
462
+ * @method toString
463
+ * @memberof Md4
464
+ * @instance
465
+ * @description Output hash as hex string
466
+ * @returns {String} Hex string
467
+ * @see {@link md4.hex}
468
+ * @example
469
+ * hash.toString();
470
+ */
471
+ Md4.prototype.toString = Md4.prototype.hex;
472
+
473
+ /**
474
+ * @method digest
475
+ * @memberof Md4
476
+ * @instance
477
+ * @description Output hash as bytes array
478
+ * @returns {Array} Bytes array
479
+ * @see {@link md4.digest}
480
+ * @example
481
+ * hash.digest();
482
+ */
483
+ Md4.prototype.digest = function() {
484
+ this.finalize();
485
+
486
+ var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3;
487
+ return [
488
+ h0 & 0xFF, (h0 >> 8) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 24) & 0xFF,
489
+ h1 & 0xFF, (h1 >> 8) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 24) & 0xFF,
490
+ h2 & 0xFF, (h2 >> 8) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 24) & 0xFF,
491
+ h3 & 0xFF, (h3 >> 8) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 24) & 0xFF
492
+ ];
493
+ };
494
+
495
+ /**
496
+ * @method array
497
+ * @memberof Md4
498
+ * @instance
499
+ * @description Output hash as bytes array
500
+ * @returns {Array} Bytes array
501
+ * @see {@link md4.array}
502
+ * @example
503
+ * hash.array();
504
+ */
505
+ Md4.prototype.array = Md4.prototype.digest;
506
+
507
+ /**
508
+ * @method arrayBuffer
509
+ * @memberof Md4
510
+ * @instance
511
+ * @description Output hash as ArrayBuffer
512
+ * @returns {ArrayBuffer} ArrayBuffer
513
+ * @see {@link md4.arrayBuffer}
514
+ * @example
515
+ * hash.arrayBuffer();
516
+ */
517
+ Md4.prototype.arrayBuffer = function() {
518
+ this.finalize();
519
+
520
+ var buffer = new ArrayBuffer(16);
521
+ var blocks = new Uint32Array(buffer);
522
+ blocks[0] = this.h0;
523
+ blocks[1] = this.h1;
524
+ blocks[2] = this.h2;
525
+ blocks[3] = this.h3;
526
+ return buffer;
527
+ };
528
+
529
+ /**
530
+ * @method buffer
531
+ * @deprecated This maybe confuse with Buffer in node.js. Please use arrayBuffer instead.
532
+ * @memberof Md4
533
+ * @instance
534
+ * @description Output hash as ArrayBuffer
535
+ * @returns {ArrayBuffer} ArrayBuffer
536
+ * @see {@link md4.buffer}
537
+ * @example
538
+ * hash.buffer();
539
+ */
540
+ Md4.prototype.buffer = Md4.prototype.arrayBuffer;
541
+
542
+ var exports = createMethod();
543
+
544
+ if (COMMON_JS) {
545
+ module.exports = exports;
546
+ } else {
547
+ /**
548
+ * @method md4
549
+ * @description MD4 hash function, export to global in browsers.
550
+ * @param {String|Array|Uint8Array|ArrayBuffer} message message to hash
551
+ * @returns {String} md4 hashes
552
+ * @example
553
+ * md4(''); // 31d6cfe0d16ae931b73c59d7e0c089c0
554
+ * md4('The quick brown fox jumps over the lazy dog'); // 1bee69a46ba811185c194762abaeae90
555
+ * md4('The quick brown fox jumps over the lazy dog.'); // 2812c6c7136898c51f6f6739ad08750e
556
+ *
557
+ * // It also supports UTF-8 encoding
558
+ * md4('中文'); // 223088bf7bd45a16436b15360c5fc5a0
559
+ *
560
+ * // It also supports byte `Array`, `Uint8Array`, `ArrayBuffer`
561
+ * md4([]); // 31d6cfe0d16ae931b73c59d7e0c089c0
562
+ * md4(new Uint8Array([])); // 31d6cfe0d16ae931b73c59d7e0c089c0
563
+ */
564
+ root.md4 = exports;
565
+ if (AMD) {
566
+ define(function () {
567
+ return exports;
568
+ });
569
+ }
570
+ }
571
+})();
rdp/security/rc4.js
new
+70
@@ -0,0 +1,70 @@
1
+
2
+function RC4(key) {
3
+ this.privateKey = keySetup(key);
4
+ this.byteStream = byteStreamGenerator(this.privateKey.slice(0));
5
+}
6
+
7
+/**
8
+ * Converts the text into an array of the characters numeric Unicode values
9
+ * @param {String} text, the text to convert
10
+ * @return {Array} the array of Unicode values
11
+ */
12
+function convert(text) {
13
+ var codes = [];
14
+ for (var i = 0, ii = text.length; i < ii; i++) { codes.push(text.charCodeAt(i)); }
15
+ return codes;
16
+}
17
+
18
+/**
19
+ * Sets up the key to use with the byte stream
20
+ * @param {String} key, The key that you want to use
21
+ * @return {Array}, the key stream which with be used in the byteStreamGenerator
22
+ */
23
+function keySetup(key) {
24
+ var K = [...Array(256).keys()], j = 0, key = convert(key);
25
+ for (var i = 0, ii = K.length; i < ii; i++) {
26
+ j = (j + K[i] + key[i % key.length]) % 256;
27
+ [K[i], K[j]] = [K[j], K[i]];
28
+ }
29
+ return K;
30
+}
31
+
32
+/**
33
+ * byteStreamGenerator uses ES6 generators which will be 'XOR-ed' to encrypt and decrypt
34
+ * @param {Array} K, the array generated from the keySetup
35
+ * @yield {Integer}, the current value which will be 'XOR-ed' to encrypt or decrypt
36
+ */
37
+var byteStreamGenerator = function* (K) {
38
+ var i = 0, j = 0;
39
+ while (true) {
40
+ i = (i + 1) % 256;
41
+ j = (j + K[i]) % 256;
42
+ [K[i], K[j]] = [K[j], K[i]];
43
+ yield (K[(K[i] + K[j]) % 256]);
44
+ }
45
+}
46
+
47
+/**
48
+ * Encrypts the input text
49
+ * @param {String} input, the text to encrypt
50
+ * @return {String}, the encrypted text
51
+ */
52
+RC4.prototype.encrypt = function (input) {
53
+ var outputText = '';
54
+ for (var i = 0, ii = input.length; i < ii; i++) { outputText += ('00' + (input.charCodeAt(i) ^ this.byteStream.next().value).toString(16)).substr(-2); }
55
+ return outputText;
56
+}
57
+
58
+/**
59
+ * Decrypts the input text
60
+ * @param {String} input, the text to decrypt
61
+ * @return {String}, the decrypted text (if the same key was used)
62
+ */
63
+RC4.prototype.decrypt = function (input) {
64
+ var outputText = '';
65
+ input = input.match(/[a-z0-9]{2}/gi);
66
+ for (var i = 0, ii = input.length; i < ii; i++) { outputText += String.fromCharCode((parseInt(input[i], 16) ^ byteStream.next().value)); }
67
+ return outputText;
68
+}
69
+
70
+module.exports = RC4;
\ No newline at end of file