include/des.js: trim some fat.

DES is just used once during authentication and is not performance
sensitive so we save some space by generating and/or removing some
lookup tables. Also, shorten some very frequently used variables.

Shaves off about 100 lines.
This commit is contained in:
Joel Martin 2010-09-30 11:17:52 -05:00
parent 5d0e1ca643
commit 2db6580933
2 changed files with 190 additions and 285 deletions

View File

@ -7,9 +7,7 @@
* -------------------------------------------------------------------------
*
* This DES class has been extracted from package Acme.Crypto for use in VNC.
* The bytebit[] array has been reversed so that the most significant bit
* in each byte of the key is ignored, not the least significant. Also the
* unnecessary odd parity code has been removed.
* The unnecessary odd parity code has been removed.
*
* These changes are:
* Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
@ -80,289 +78,196 @@
"use strict";
/*jslint white: false, bitwise: false, plusplus: false */
var DES = {
function DES(passwd) {
// Tables, permutations, S-boxes, etc.
bytebit : [0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80],
bigbyte : [ 0x800000, 0x400000, 0x200000, 0x100000,
0x080000, 0x040000, 0x020000, 0x010000, 0x008000, 0x004000,
0x002000, 0x001000, 0x000800, 0x000400, 0x000200, 0x000100,
0x000080, 0x000040, 0x000020, 0x000010, 0x000008, 0x000004,
0x000002, 0x000001],
pc1 : [56,48,40,32,24,16, 8, 0,57,49,41,33,25,17, 9, 1,
58,50,42,34,26,18,10, 2,59,51,43,35,62,54,46,38,
30,22,14,6, 61,53,45,37,29,21,13, 5,60,52,44,36,
28,20,12, 4,27,19,11, 3],
pc2 : [13,16,10,23, 0, 4, 2,27,14, 5,20, 9,22,18,11, 3,
// Tables, permutations, S-boxes, etc.
var PC2 = [13,16,10,23, 0, 4, 2,27,14, 5,20, 9,22,18,11, 3,
25, 7,15, 6,26,19,12, 1,40,51,30,36,46,54,29,39,
50,44,32,47,43,48,38,55,33,52,45,41,49,35,28,31 ],
totrot : [ 1, 2, 4, 6, 8,10,12,14,15,17,19,21,23,25,27,28],
SP1 : [ 0x01010400, 0x00000000, 0x00010000,
0x01010404, 0x01010004, 0x00010404, 0x00000004, 0x00010000,
0x00000400, 0x01010400, 0x01010404, 0x00000400, 0x01000404,
0x01010004, 0x01000000, 0x00000004, 0x00000404, 0x01000400,
0x01000400, 0x00010400, 0x00010400, 0x01010000, 0x01010000,
0x01000404, 0x00010004, 0x01000004, 0x01000004, 0x00010004,
0x00000000, 0x00000404, 0x00010404, 0x01000000, 0x00010000,
0x01010404, 0x00000004, 0x01010000, 0x01010400, 0x01000000,
0x01000000, 0x00000400, 0x01010004, 0x00010000, 0x00010400,
0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404,
0x01010404, 0x00010004, 0x01010000, 0x01000404, 0x01000004,
0x00000404, 0x00010404, 0x01010400, 0x00000404, 0x01000400,
0x01000400, 0x00000000, 0x00010004, 0x00010400, 0x00000000,
0x01010004],
SP2 : [ 0x80108020, 0x80008000, 0x00008000,
0x00108020, 0x00100000, 0x00000020, 0x80100020, 0x80008020,
0x80000020, 0x80108020, 0x80108000, 0x80000000, 0x80008000,
0x00100000, 0x00000020, 0x80100020, 0x00108000, 0x00100020,
0x80008020, 0x00000000, 0x80000000, 0x00008000, 0x00108020,
0x80100000, 0x00100020, 0x80000020, 0x00000000, 0x00108000,
0x00008020, 0x80108000, 0x80100000, 0x00008020, 0x00000000,
0x00108020, 0x80100020, 0x00100000, 0x80008020, 0x80100000,
0x80108000, 0x00008000, 0x80100000, 0x80008000, 0x00000020,
0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000,
0x00008020, 0x80108000, 0x00100000, 0x80000020, 0x00100020,
0x80008020, 0x80000020, 0x00100020, 0x00108000, 0x00000000,
0x80008000, 0x00008020, 0x80000000, 0x80100020, 0x80108020,
0x00108000],
SP3 : [ 0x00000208, 0x08020200, 0x00000000,
0x08020008, 0x08000200, 0x00000000, 0x00020208, 0x08000200,
0x00020008, 0x08000008, 0x08000008, 0x00020000, 0x08020208,
0x00020008, 0x08020000, 0x00000208, 0x08000000, 0x00000008,
0x08020200, 0x00000200, 0x00020200, 0x08020000, 0x08020008,
0x00020208, 0x08000208, 0x00020200, 0x00020000, 0x08000208,
0x00000008, 0x08020208, 0x00000200, 0x08000000, 0x08020200,
0x08000000, 0x00020008, 0x00000208, 0x00020000, 0x08020200,
0x08000200, 0x00000000, 0x00000200, 0x00020008, 0x08020208,
0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008,
0x08000208, 0x00020000, 0x08000000, 0x08020208, 0x00000008,
0x00020208, 0x00020200, 0x08000008, 0x08020000, 0x08000208,
0x00000208, 0x08020000, 0x00020208, 0x00000008, 0x08020008,
0x00020200],
SP4 : [ 0x00802001, 0x00002081, 0x00002081,
0x00000080, 0x00802080, 0x00800081, 0x00800001, 0x00002001,
0x00000000, 0x00802000, 0x00802000, 0x00802081, 0x00000081,
0x00000000, 0x00800080, 0x00800001, 0x00000001, 0x00002000,
0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002001,
0x00002080, 0x00800081, 0x00000001, 0x00002080, 0x00800080,
0x00002000, 0x00802080, 0x00802081, 0x00000081, 0x00800080,
0x00800001, 0x00802000, 0x00802081, 0x00000081, 0x00000000,
0x00000000, 0x00802000, 0x00002080, 0x00800080, 0x00800081,
0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080,
0x00802081, 0x00000081, 0x00000001, 0x00002000, 0x00800001,
0x00002001, 0x00802080, 0x00800081, 0x00002001, 0x00002080,
0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002000,
0x00802080],
SP5 : [ 0x00000100, 0x02080100, 0x02080000,
0x42000100, 0x00080000, 0x00000100, 0x40000000, 0x02080000,
0x40080100, 0x00080000, 0x02000100, 0x40080100, 0x42000100,
0x42080000, 0x00080100, 0x40000000, 0x02000000, 0x40080000,
0x40080000, 0x00000000, 0x40000100, 0x42080100, 0x42080100,
0x02000100, 0x42080000, 0x40000100, 0x00000000, 0x42000000,
0x02080100, 0x02000000, 0x42000000, 0x00080100, 0x00080000,
0x42000100, 0x00000100, 0x02000000, 0x40000000, 0x02080000,
0x42000100, 0x40080100, 0x02000100, 0x40000000, 0x42080000,
0x02080100, 0x40080100, 0x00000100, 0x02000000, 0x42080000,
0x42080100, 0x00080100, 0x42000000, 0x42080100, 0x02080000,
0x00000000, 0x40080000, 0x42000000, 0x00080100, 0x02000100,
0x40000100, 0x00080000, 0x00000000, 0x40080000, 0x02080100,
0x40000100],
SP6 : [ 0x20000010, 0x20400000, 0x00004000,
0x20404010, 0x20400000, 0x00000010, 0x20404010, 0x00400000,
0x20004000, 0x00404010, 0x00400000, 0x20000010, 0x00400010,
0x20004000, 0x20000000, 0x00004010, 0x00000000, 0x00400010,
0x20004010, 0x00004000, 0x00404000, 0x20004010, 0x00000010,
0x20400010, 0x20400010, 0x00000000, 0x00404010, 0x20404000,
0x00004010, 0x00404000, 0x20404000, 0x20000000, 0x20004000,
0x00000010, 0x20400010, 0x00404000, 0x20404010, 0x00400000,
0x00004010, 0x20000010, 0x00400000, 0x20004000, 0x20000000,
0x00004010, 0x20000010, 0x20404010, 0x00404000, 0x20400000,
0x00404010, 0x20404000, 0x00000000, 0x20400010, 0x00000010,
0x00004000, 0x20400000, 0x00404010, 0x00004000, 0x00400010,
0x20004010, 0x00000000, 0x20404000, 0x20000000, 0x00400010,
0x20004010],
SP7 : [ 0x00200000, 0x04200002, 0x04000802,
0x00000000, 0x00000800, 0x04000802, 0x00200802, 0x04200800,
0x04200802, 0x00200000, 0x00000000, 0x04000002, 0x00000002,
0x04000000, 0x04200002, 0x00000802, 0x04000800, 0x00200802,
0x00200002, 0x04000800, 0x04000002, 0x04200000, 0x04200800,
0x00200002, 0x04200000, 0x00000800, 0x00000802, 0x04200802,
0x00200800, 0x00000002, 0x04000000, 0x00200800, 0x04000000,
0x00200800, 0x00200000, 0x04000802, 0x04000802, 0x04200002,
0x04200002, 0x00000002, 0x00200002, 0x04000000, 0x04000800,
0x00200000, 0x04200800, 0x00000802, 0x00200802, 0x04200800,
0x00000802, 0x04000002, 0x04200802, 0x04200000, 0x00200800,
0x00000000, 0x00000002, 0x04200802, 0x00000000, 0x00200802,
0x04200000, 0x00000800, 0x04000002, 0x04000800, 0x00000800,
0x00200002],
SP8 : [ 0x10001040, 0x00001000, 0x00040000,
0x10041040, 0x10000000, 0x10001040, 0x00000040, 0x10000000,
0x00040040, 0x10040000, 0x10041040, 0x00041000, 0x10041000,
0x00041040, 0x00001000, 0x00000040, 0x10040000, 0x10000040,
0x10001000, 0x00001040, 0x00041000, 0x00040040, 0x10040040,
0x10041000, 0x00001040, 0x00000000, 0x00000000, 0x10040040,
0x10000040, 0x10001000, 0x00041040, 0x00040000, 0x00041040,
0x00040000, 0x10041000, 0x00001000, 0x00000040, 0x10040040,
0x00001000, 0x00041040, 0x10001000, 0x00000040, 0x10000040,
0x10040000, 0x10040040, 0x10000000, 0x00040000, 0x10001040,
0x00000000, 0x10041040, 0x00040040, 0x10000040, 0x10040000,
0x10001000, 0x10001040, 0x00000000, 0x10041040, 0x00041000,
0x00041000, 0x00001040, 0x00001040, 0x00040040, 0x10000000,
0x10041000],
totrot = [ 1, 2, 4, 6, 8,10,12,14,15,17,19,21,23,25,27,28],
z = 0x0, a,b,c,d,e,f, SP1,SP2,SP3,SP4,SP5,SP6,SP7,SP8,
keys = [];
keys : [],
a=1<<16; b=1<<24; c=a|b; d=1<<2; e=1<<10; f=d|e;
SP1 = [c|e,z|z,a|z,c|f,c|d,a|f,z|d,a|z,z|e,c|e,c|f,z|e,b|f,c|d,b|z,z|d,
z|f,b|e,b|e,a|e,a|e,c|z,c|z,b|f,a|d,b|d,b|d,a|d,z|z,z|f,a|f,b|z,
a|z,c|f,z|d,c|z,c|e,b|z,b|z,z|e,c|d,a|z,a|e,b|d,z|e,z|d,b|f,a|f,
c|f,a|d,c|z,b|f,b|d,z|f,a|f,c|e,z|f,b|e,b|e,z|z,a|d,a|e,z|z,c|d];
a=1<<20; b=1<<31; c=a|b; d=1<<5; e=1<<15; f=d|e;
SP2 = [c|f,b|e,z|e,a|f,a|z,z|d,c|d,b|f,b|d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f,z|f,a|d,b|z,c|e];
// Set the key.
setKeys : function(keyBlock) {
var i, j, l, m, n, pc1m = [], pcr = [], kn = [],
raw0, raw1, rawi, KnLi;
// Set the key.
function setKeys(keyBlock) {
var i, j, l, m, n, o, pc1m = [], pcr = [], kn = [],
raw0, raw1, rawi, KnLi;
for (j = 0; j < 56; ++j) {
l = DES.pc1[j];
m = l & 0x7;
pc1m[j] = ((keyBlock[l >>> 3] & DES.bytebit[m]) !== 0) ? 1: 0;
}
for (i = 0; i < 16; ++i) {
m = i << 1;
n = m + 1;
kn[m] = kn[n] = 0;
for (j = 0; j < 28; ++j) {
l = j + DES.totrot[i];
if (l < 28) {
pcr[j] = pc1m[l];
} else {
pcr[j] = pc1m[l - 28];
}
}
for (j = 28; j < 56; ++j) {
l = j + DES.totrot[i];
if (l < 56) {
pcr[j] = pc1m[l];
} else {
pcr[j] = pc1m[l - 28];
}
}
for (j = 0; j < 24; ++j) {
if (pcr[DES.pc2[j]] !== 0) {
kn[m] |= DES.bigbyte[j];
}
if (pcr[DES.pc2[j + 24]] !== 0) {
kn[n] |= DES.bigbyte[j];
}
}
}
// cookey
for (i = 0, rawi = 0, KnLi = 0; i < 16; ++i) {
raw0 = kn[rawi++];
raw1 = kn[rawi++];
DES.keys[KnLi] = (raw0 & 0x00fc0000) << 6;
DES.keys[KnLi] |= (raw0 & 0x00000fc0) << 10;
DES.keys[KnLi] |= (raw1 & 0x00fc0000) >>> 10;
DES.keys[KnLi] |= (raw1 & 0x00000fc0) >>> 6;
++KnLi;
DES.keys[KnLi] = (raw0 & 0x0003f000) << 12;
DES.keys[KnLi] |= (raw0 & 0x0000003f) << 16;
DES.keys[KnLi] |= (raw1 & 0x0003f000) >>> 4;
DES.keys[KnLi] |= (raw1 & 0x0000003f);
++KnLi;
}
},
// Encrypt 8 bytes of text starting at offset
encrypt8: function(text, offset) {
var i, b = text, o, outInts =[],
fval, work, right, leftt, round, keysi = 0;
// Squash 8 bytes down to 2 ints
o = offset;
leftt = b[o++]<<24 | b[o++]<<16 | b[o++]<<8 | b[o++];
right = b[o++]<<24 | b[o++]<<16 | b[o++]<<8 | b[o++];
work = ((leftt >>> 4) ^ right) & 0x0f0f0f0f;
right ^= work;
leftt ^= (work << 4);
work = ((leftt >>> 16) ^ right) & 0x0000ffff;
right ^= work;
leftt ^= (work << 16);
work = ((right >>> 2) ^ leftt) & 0x33333333;
leftt ^= work;
right ^= (work << 2);
work = ((right >>> 8) ^ leftt) & 0x00ff00ff;
leftt ^= work;
right ^= (work << 8);
right = (right << 1) | ((right >>> 31) & 1);
work = (leftt ^ right) & 0xaaaaaaaa;
leftt ^= work;
right ^= work;
leftt = (leftt << 1) | ((leftt >>> 31) & 1);
for (round = 0; round < 8; ++round) {
work = (right << 28) | (right >>> 4);
work ^= DES.keys[keysi++];
fval = DES.SP7[work & 0x0000003f];
fval |= DES.SP5[(work >>> 8) & 0x0000003f];
fval |= DES.SP3[(work >>> 16) & 0x0000003f];
fval |= DES.SP1[(work >>> 24) & 0x0000003f];
work = right ^ DES.keys[keysi++];
fval |= DES.SP8[work & 0x0000003f];
fval |= DES.SP6[(work >>> 8) & 0x0000003f];
fval |= DES.SP4[(work >>> 16) & 0x0000003f];
fval |= DES.SP2[(work >>> 24) & 0x0000003f];
leftt ^= fval;
work = (leftt << 28) | (leftt >>> 4);
work ^= DES.keys[keysi++];
fval = DES.SP7[work & 0x0000003f];
fval |= DES.SP5[(work >>> 8) & 0x0000003f];
fval |= DES.SP3[(work >>> 16) & 0x0000003f];
fval |= DES.SP1[(work >>> 24) & 0x0000003f];
work = leftt ^ DES.keys[keysi++];
fval |= DES.SP8[work & 0x0000003f];
fval |= DES.SP6[(work >>> 8) & 0x0000003f];
fval |= DES.SP4[(work >>> 16) & 0x0000003f];
fval |= DES.SP2[(work >>> 24) & 0x0000003f];
right ^= fval;
}
right = (right << 31) | (right >>> 1);
work = (leftt ^ right) & 0xaaaaaaaa;
leftt ^= work;
right ^= work;
leftt = (leftt << 31) | (leftt >>> 1);
work = ((leftt >>> 8) ^ right) & 0x00ff00ff;
right ^= work;
leftt ^= (work << 8);
work = ((leftt >>> 2) ^ right) & 0x33333333;
right ^= work;
leftt ^= (work << 2);
work = ((right >>> 16) ^ leftt) & 0x0000ffff;
leftt ^= work;
right ^= (work << 16);
work = ((right >>> 4) ^ leftt) & 0x0f0f0f0f;
leftt ^= work;
right ^= (work << 4);
outInts[0] = right;
outInts[1] = leftt;
// Spread ints to bytes
o = offset;
for (i = 0; i < 8; i++) {
b[o+i] = (outInts[i>>>2] >>> (8*(3 - (i%4)))) % 256;
if (b[o+i] < 0) { b[o+i] += 256; } // unsigned
}
},
// Encrypt 16 bytes of text using passwd as key
encrypt: function(passwd, text) {
var i, cipher = text.slice();
DES.setKeys(passwd);
DES.encrypt8(cipher, 0);
DES.encrypt8(cipher, 8);
return cipher;
for (j = 0, l = 56; j < 56; ++j, l-=8) {
l += l<-5 ? 65 : l<-3 ? 31 : l<-1 ? 63 : l===27 ? 35 : 0; // PC1
m = l & 0x7;
pc1m[j] = ((keyBlock[l >>> 3] & (1<<m)) !== 0) ? 1: 0;
}
};
for (i = 0; i < 16; ++i) {
m = i << 1;
n = m + 1;
kn[m] = kn[n] = 0;
for (o=28; o<59; o+=28) {
for (j = o-28; j < o; ++j) {
l = j + totrot[i];
if (l < o) {
pcr[j] = pc1m[l];
} else {
pcr[j] = pc1m[l - 28];
}
}
}
for (j = 0; j < 24; ++j) {
if (pcr[PC2[j]] !== 0) {
kn[m] |= 1<<(23-j);
}
if (pcr[PC2[j + 24]] !== 0) {
kn[n] |= 1<<(23-j);
}
}
}
// cookey
for (i = 0, rawi = 0, KnLi = 0; i < 16; ++i) {
raw0 = kn[rawi++];
raw1 = kn[rawi++];
keys[KnLi] = (raw0 & 0x00fc0000) << 6;
keys[KnLi] |= (raw0 & 0x00000fc0) << 10;
keys[KnLi] |= (raw1 & 0x00fc0000) >>> 10;
keys[KnLi] |= (raw1 & 0x00000fc0) >>> 6;
++KnLi;
keys[KnLi] = (raw0 & 0x0003f000) << 12;
keys[KnLi] |= (raw0 & 0x0000003f) << 16;
keys[KnLi] |= (raw1 & 0x0003f000) >>> 4;
keys[KnLi] |= (raw1 & 0x0000003f);
++KnLi;
}
}
// Encrypt 8 bytes of text
function enc8(text) {
var i = 0, b = text.slice(), fval, keysi = 0,
l, r, x; // left, right, accumulator
// Squash 8 bytes to 2 ints
l = b[i++]<<24 | b[i++]<<16 | b[i++]<<8 | b[i++];
r = b[i++]<<24 | b[i++]<<16 | b[i++]<<8 | b[i++];
x = ((l >>> 4) ^ r) & 0x0f0f0f0f;
r ^= x;
l ^= (x << 4);
x = ((l >>> 16) ^ r) & 0x0000ffff;
r ^= x;
l ^= (x << 16);
x = ((r >>> 2) ^ l) & 0x33333333;
l ^= x;
r ^= (x << 2);
x = ((r >>> 8) ^ l) & 0x00ff00ff;
l ^= x;
r ^= (x << 8);
r = (r << 1) | ((r >>> 31) & 1);
x = (l ^ r) & 0xaaaaaaaa;
l ^= x;
r ^= x;
l = (l << 1) | ((l >>> 31) & 1);
for (i = 0; i < 8; ++i) {
x = (r << 28) | (r >>> 4);
x ^= keys[keysi++];
fval = SP7[x & 0x3f];
fval |= SP5[(x >>> 8) & 0x3f];
fval |= SP3[(x >>> 16) & 0x3f];
fval |= SP1[(x >>> 24) & 0x3f];
x = r ^ keys[keysi++];
fval |= SP8[x & 0x3f];
fval |= SP6[(x >>> 8) & 0x3f];
fval |= SP4[(x >>> 16) & 0x3f];
fval |= SP2[(x >>> 24) & 0x3f];
l ^= fval;
x = (l << 28) | (l >>> 4);
x ^= keys[keysi++];
fval = SP7[x & 0x3f];
fval |= SP5[(x >>> 8) & 0x3f];
fval |= SP3[(x >>> 16) & 0x3f];
fval |= SP1[(x >>> 24) & 0x3f];
x = l ^ keys[keysi++];
fval |= SP8[x & 0x0000003f];
fval |= SP6[(x >>> 8) & 0x3f];
fval |= SP4[(x >>> 16) & 0x3f];
fval |= SP2[(x >>> 24) & 0x3f];
r ^= fval;
}
r = (r << 31) | (r >>> 1);
x = (l ^ r) & 0xaaaaaaaa;
l ^= x;
r ^= x;
l = (l << 31) | (l >>> 1);
x = ((l >>> 8) ^ r) & 0x00ff00ff;
r ^= x;
l ^= (x << 8);
x = ((l >>> 2) ^ r) & 0x33333333;
r ^= x;
l ^= (x << 2);
x = ((r >>> 16) ^ l) & 0x0000ffff;
l ^= x;
r ^= (x << 16);
x = ((r >>> 4) ^ l) & 0x0f0f0f0f;
l ^= x;
r ^= (x << 4);
// Spread ints to bytes
x = [r, l];
for (i = 0; i < 8; i++) {
b[i] = (x[i>>>2] >>> (8*(3 - (i%4)))) % 256;
if (b[i] < 0) { b[i] += 256; } // unsigned
}
return b;
}
// Encrypt 16 bytes of text using passwd as key
function encrypt(t) {
return enc8(t.slice(0,8)).concat(enc8(t.slice(8,16)));
}
setKeys(passwd); // Setup keys
return {'encrypt': encrypt}; // Public interface
} // function DES

View File

@ -313,7 +313,7 @@ init_vars = function() {
mouse_arr = [];
// Clear the per connection encoding stats
for (i=0; i < encodings.length; i+=1) {
for (var i=0; i < encodings.length; i+=1) {
encStats[encodings[i][1]][0] = 0;
}
};
@ -602,11 +602,11 @@ function send_string(str) {
}
function genDES(password, challenge) {
var i, passwd = [];
var i, passwd = [], des;
for (i=0; i < password.length; i += 1) {
passwd.push(password.charCodeAt(i));
}
return DES.encrypt(passwd, challenge);
return (new DES(passwd)).encrypt(challenge);
}
function flushClient() {