UNPKG

6.53 kBJavaScriptView Raw
1var uint_max = Math.pow(2, 32);
2function fixup_uint32(x) {
3 var ret, x_pos;
4 ret = x > uint_max || x < 0 ? (x_pos = Math.abs(x) % uint_max, x < 0 ? uint_max - x_pos : x_pos) : x;
5 return ret;
6}
7function scrub_vec(v) {
8 var i, _i, _ref;
9 for (i = _i = 0, _ref = v.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
10 v[i] = 0;
11 }
12 return false;
13}
14
15function Global() {
16 var i;
17 this.SBOX = [];
18 this.INV_SBOX = [];
19 this.SUB_MIX = (function() {
20 var _i, _results;
21 _results = [];
22 for (i = _i = 0; _i < 4; i = ++_i) {
23 _results.push([]);
24 }
25 return _results;
26 })();
27 this.INV_SUB_MIX = (function() {
28 var _i, _results;
29 _results = [];
30 for (i = _i = 0; _i < 4; i = ++_i) {
31 _results.push([]);
32 }
33 return _results;
34 })();
35 this.init();
36 this.RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36];
37}
38
39Global.prototype.init = function() {
40 var d, i, sx, t, x, x2, x4, x8, xi, _i;
41 d = (function() {
42 var _i, _results;
43 _results = [];
44 for (i = _i = 0; _i < 256; i = ++_i) {
45 if (i < 128) {
46 _results.push(i << 1);
47 } else {
48 _results.push((i << 1) ^ 0x11b);
49 }
50 }
51 return _results;
52 })();
53 x = 0;
54 xi = 0;
55 for (i = _i = 0; _i < 256; i = ++_i) {
56 sx = xi ^ (xi << 1) ^ (xi << 2) ^ (xi << 3) ^ (xi << 4);
57 sx = (sx >>> 8) ^ (sx & 0xff) ^ 0x63;
58 this.SBOX[x] = sx;
59 this.INV_SBOX[sx] = x;
60 x2 = d[x];
61 x4 = d[x2];
62 x8 = d[x4];
63 t = (d[sx] * 0x101) ^ (sx * 0x1010100);
64 this.SUB_MIX[0][x] = (t << 24) | (t >>> 8);
65 this.SUB_MIX[1][x] = (t << 16) | (t >>> 16);
66 this.SUB_MIX[2][x] = (t << 8) | (t >>> 24);
67 this.SUB_MIX[3][x] = t;
68 t = (x8 * 0x1010101) ^ (x4 * 0x10001) ^ (x2 * 0x101) ^ (x * 0x1010100);
69 this.INV_SUB_MIX[0][sx] = (t << 24) | (t >>> 8);
70 this.INV_SUB_MIX[1][sx] = (t << 16) | (t >>> 16);
71 this.INV_SUB_MIX[2][sx] = (t << 8) | (t >>> 24);
72 this.INV_SUB_MIX[3][sx] = t;
73 if (x === 0) {
74 x = xi = 1;
75 } else {
76 x = x2 ^ d[d[d[x8 ^ x2]]];
77 xi ^= d[d[xi]];
78 }
79 }
80 return true;
81};
82
83var G = new Global();
84
85
86AES.blockSize = 4 * 4;
87
88AES.prototype.blockSize = AES.blockSize;
89
90AES.keySize = 256 / 8;
91
92AES.prototype.keySize = AES.keySize;
93
94AES.ivSize = AES.blockSize;
95
96AES.prototype.ivSize = AES.ivSize;
97
98 function bufferToArray(buf) {
99 var len = buf.length/4;
100 var out = new Array(len);
101 var i = -1;
102 while (++i < len) {
103 out[i] = buf.readUInt32BE(i * 4);
104 }
105 return out;
106 }
107function AES(key) {
108 this._key = bufferToArray(key);
109 this._doReset();
110}
111
112AES.prototype._doReset = function() {
113 var invKsRow, keySize, keyWords, ksRow, ksRows, t, _i, _j;
114 keyWords = this._key;
115 keySize = keyWords.length;
116 this._nRounds = keySize + 6;
117 ksRows = (this._nRounds + 1) * 4;
118 this._keySchedule = [];
119 for (ksRow = _i = 0; 0 <= ksRows ? _i < ksRows : _i > ksRows; ksRow = 0 <= ksRows ? ++_i : --_i) {
120 this._keySchedule[ksRow] = ksRow < keySize ? keyWords[ksRow] : (t = this._keySchedule[ksRow - 1], (ksRow % keySize) === 0 ? (t = (t << 8) | (t >>> 24), t = (G.SBOX[t >>> 24] << 24) | (G.SBOX[(t >>> 16) & 0xff] << 16) | (G.SBOX[(t >>> 8) & 0xff] << 8) | G.SBOX[t & 0xff], t ^= G.RCON[(ksRow / keySize) | 0] << 24) : keySize > 6 && ksRow % keySize === 4 ? t = (G.SBOX[t >>> 24] << 24) | (G.SBOX[(t >>> 16) & 0xff] << 16) | (G.SBOX[(t >>> 8) & 0xff] << 8) | G.SBOX[t & 0xff] : void 0, this._keySchedule[ksRow - keySize] ^ t);
121 }
122 this._invKeySchedule = [];
123 for (invKsRow = _j = 0; 0 <= ksRows ? _j < ksRows : _j > ksRows; invKsRow = 0 <= ksRows ? ++_j : --_j) {
124 ksRow = ksRows - invKsRow;
125 t = this._keySchedule[ksRow - (invKsRow % 4 ? 0 : 4)];
126 this._invKeySchedule[invKsRow] = invKsRow < 4 || ksRow <= 4 ? t : G.INV_SUB_MIX[0][G.SBOX[t >>> 24]] ^ G.INV_SUB_MIX[1][G.SBOX[(t >>> 16) & 0xff]] ^ G.INV_SUB_MIX[2][G.SBOX[(t >>> 8) & 0xff]] ^ G.INV_SUB_MIX[3][G.SBOX[t & 0xff]];
127 }
128 return true;
129};
130
131AES.prototype.encryptBlock = function(M) {
132 M = bufferToArray(new Buffer(M));
133 var out = this._doCryptBlock(M, this._keySchedule, G.SUB_MIX, G.SBOX);
134 var buf = new Buffer(16);
135 buf.writeUInt32BE(out[0], 0);
136 buf.writeUInt32BE(out[1], 4);
137 buf.writeUInt32BE(out[2], 8);
138 buf.writeUInt32BE(out[3], 12);
139 return buf;
140};
141
142AES.prototype.decryptBlock = function(M) {
143 M = bufferToArray(new Buffer(M));
144 var temp = [M[3], M[1]];
145 M[1] = temp[0];
146 M[3] = temp[1];
147 var out = this._doCryptBlock(M, this._invKeySchedule, G.INV_SUB_MIX, G.INV_SBOX);
148 var buf = new Buffer(16);
149 buf.writeUInt32BE(out[0], 0);
150 buf.writeUInt32BE(out[3], 4);
151 buf.writeUInt32BE(out[2], 8);
152 buf.writeUInt32BE(out[1], 12);
153 return buf;
154};
155
156AES.prototype.scrub = function() {
157 scrub_vec(this._keySchedule);
158 scrub_vec(this._invKeySchedule);
159 scrub_vec(this._key);
160};
161
162AES.prototype._doCryptBlock = function(M, keySchedule, SUB_MIX, SBOX) {
163 var ksRow, round, s0, s1, s2, s3, t0, t1, t2, t3, _i, _ref;
164
165 s0 = M[0] ^ keySchedule[0];
166 s1 = M[1] ^ keySchedule[1];
167 s2 = M[2] ^ keySchedule[2];
168 s3 = M[3] ^ keySchedule[3];
169 ksRow = 4;
170 for (round = _i = 1, _ref = this._nRounds; 1 <= _ref ? _i < _ref : _i > _ref; round = 1 <= _ref ? ++_i : --_i) {
171 t0 = SUB_MIX[0][s0 >>> 24] ^ SUB_MIX[1][(s1 >>> 16) & 0xff] ^ SUB_MIX[2][(s2 >>> 8) & 0xff] ^ SUB_MIX[3][s3 & 0xff] ^ keySchedule[ksRow++];
172 t1 = SUB_MIX[0][s1 >>> 24] ^ SUB_MIX[1][(s2 >>> 16) & 0xff] ^ SUB_MIX[2][(s3 >>> 8) & 0xff] ^ SUB_MIX[3][s0 & 0xff] ^ keySchedule[ksRow++];
173 t2 = SUB_MIX[0][s2 >>> 24] ^ SUB_MIX[1][(s3 >>> 16) & 0xff] ^ SUB_MIX[2][(s0 >>> 8) & 0xff] ^ SUB_MIX[3][s1 & 0xff] ^ keySchedule[ksRow++];
174 t3 = SUB_MIX[0][s3 >>> 24] ^ SUB_MIX[1][(s0 >>> 16) & 0xff] ^ SUB_MIX[2][(s1 >>> 8) & 0xff] ^ SUB_MIX[3][s2 & 0xff] ^ keySchedule[ksRow++];
175 s0 = t0;
176 s1 = t1;
177 s2 = t2;
178 s3 = t3;
179 }
180 t0 = ((SBOX[s0 >>> 24] << 24) | (SBOX[(s1 >>> 16) & 0xff] << 16) | (SBOX[(s2 >>> 8) & 0xff] << 8) | SBOX[s3 & 0xff]) ^ keySchedule[ksRow++];
181 t1 = ((SBOX[s1 >>> 24] << 24) | (SBOX[(s2 >>> 16) & 0xff] << 16) | (SBOX[(s3 >>> 8) & 0xff] << 8) | SBOX[s0 & 0xff]) ^ keySchedule[ksRow++];
182 t2 = ((SBOX[s2 >>> 24] << 24) | (SBOX[(s3 >>> 16) & 0xff] << 16) | (SBOX[(s0 >>> 8) & 0xff] << 8) | SBOX[s1 & 0xff]) ^ keySchedule[ksRow++];
183 t3 = ((SBOX[s3 >>> 24] << 24) | (SBOX[(s0 >>> 16) & 0xff] << 16) | (SBOX[(s1 >>> 8) & 0xff] << 8) | SBOX[s2 & 0xff]) ^ keySchedule[ksRow++];
184 return [
185 fixup_uint32(t0),
186 fixup_uint32(t1),
187 fixup_uint32(t2),
188 fixup_uint32(t3)
189 ];
190
191};
192
193
194
195
196 exports.AES = AES;
\No newline at end of file