|
|
@@ -1,402 +1,402 @@
|
|
1
|
|
-(function(){
|
|
2
|
|
-
|
|
3
|
|
-var C = (typeof window === 'undefined') ? require('./Crypto').Crypto : window.Crypto;
|
|
4
|
|
-
|
|
5
|
|
-// Shortcuts
|
|
6
|
|
-var util = C.util,
|
|
7
|
|
- charenc = C.charenc,
|
|
8
|
|
- UTF8 = charenc.UTF8;
|
|
9
|
|
-
|
|
10
|
|
-// Precomputed SBOX
|
|
11
|
|
-var SBOX = [ 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5,
|
|
12
|
|
- 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
|
|
13
|
|
- 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
|
|
14
|
|
- 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
|
|
15
|
|
- 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc,
|
|
16
|
|
- 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
|
|
17
|
|
- 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a,
|
|
18
|
|
- 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
|
|
19
|
|
- 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
|
|
20
|
|
- 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
|
|
21
|
|
- 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b,
|
|
22
|
|
- 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
|
|
23
|
|
- 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85,
|
|
24
|
|
- 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 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
43
|
|
-
|
|
44
|
|
-// Compute inverse SBOX lookup table
|
|
45
|
|
-for (var INVSBOX = [], i = 0; i < 256; i++) INVSBOX[SBOX[i]] = i;
|
|
46
|
|
-
|
|
47
|
|
-// Compute mulitplication in GF(2^8) lookup tables
|
|
48
|
|
-var MULT2 = [],
|
|
49
|
|
- MULT3 = [],
|
|
50
|
|
- MULT9 = [],
|
|
51
|
|
- MULTB = [],
|
|
52
|
|
- MULTD = [],
|
|
53
|
|
- MULTE = [];
|
|
54
|
|
-
|
|
55
|
|
-function xtime(a, b) {
|
|
56
|
|
- for (var result = 0, i = 0; i < 8; i++) {
|
|
57
|
|
- if (b & 1) result ^= a;
|
|
58
|
|
- var hiBitSet = a & 0x80;
|
|
59
|
|
- a = (a << 1) & 0xFF;
|
|
60
|
|
- if (hiBitSet) a ^= 0x1b;
|
|
61
|
|
- b >>>= 1;
|
|
62
|
|
- }
|
|
63
|
|
- return result;
|
|
64
|
|
-}
|
|
65
|
|
-
|
|
66
|
|
-for (var i = 0; i < 256; i++) {
|
|
67
|
|
- MULT2[i] = xtime(i,2);
|
|
68
|
|
- MULT3[i] = xtime(i,3);
|
|
69
|
|
- MULT9[i] = xtime(i,9);
|
|
70
|
|
- MULTB[i] = xtime(i,0xB);
|
|
71
|
|
- MULTD[i] = xtime(i,0xD);
|
|
72
|
|
- MULTE[i] = xtime(i,0xE);
|
|
73
|
|
-}
|
|
74
|
|
-
|
|
75
|
|
-// Precomputed RCon lookup
|
|
76
|
|
-var RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36];
|
|
77
|
|
-
|
|
78
|
|
-// Inner state
|
|
79
|
|
-var state = [[], [], [], []],
|
|
80
|
|
- keylength,
|
|
81
|
|
- nrounds,
|
|
82
|
|
- keyschedule;
|
|
83
|
|
-
|
|
84
|
|
-var AES = C.AES = {
|
|
85
|
|
-
|
|
86
|
|
- /**
|
|
87
|
|
- * Public API
|
|
88
|
|
- */
|
|
89
|
|
-
|
|
90
|
|
- encrypt: function (message, password, options) {
|
|
91
|
|
-
|
|
92
|
|
- options = options || {};
|
|
93
|
|
-
|
|
94
|
|
- // Determine mode
|
|
95
|
|
- var mode = options.mode || new C.mode.OFB;
|
|
96
|
|
-
|
|
97
|
|
- // Allow mode to override options
|
|
98
|
|
- if (mode.fixOptions) mode.fixOptions(options);
|
|
99
|
|
-
|
|
100
|
|
- var
|
|
101
|
|
-
|
|
102
|
|
- // Convert to bytes if message is a string
|
|
103
|
|
- m = (
|
|
104
|
|
- message.constructor == String ?
|
|
105
|
|
- UTF8.stringToBytes(message) :
|
|
106
|
|
- message
|
|
107
|
|
- ),
|
|
108
|
|
-
|
|
109
|
|
- // Generate random IV
|
|
110
|
|
- iv = options.iv || util.randomBytes(AES._blocksize * 4),
|
|
111
|
|
-
|
|
112
|
|
- // Generate key
|
|
113
|
|
- k = (
|
|
114
|
|
- password.constructor == String ?
|
|
115
|
|
- // Derive key from passphrase
|
|
116
|
|
- C.PBKDF2(password, iv, 32, { asBytes: true }) :
|
|
117
|
|
- // else, assume byte array representing cryptographic key
|
|
118
|
|
- password
|
|
119
|
|
- );
|
|
120
|
|
-
|
|
121
|
|
- // Encrypt
|
|
122
|
|
- AES._init(k);
|
|
123
|
|
- mode.encrypt(AES, m, iv);
|
|
124
|
|
-
|
|
125
|
|
- // Return ciphertext
|
|
126
|
|
- m = options.iv ? m : iv.concat(m);
|
|
127
|
|
- return (options && options.asBytes) ? m : util.bytesToBase64(m);
|
|
128
|
|
-
|
|
129
|
|
- },
|
|
130
|
|
-
|
|
131
|
|
- decrypt: function (ciphertext, password, options) {
|
|
132
|
|
-
|
|
133
|
|
- options = options || {};
|
|
134
|
|
-
|
|
135
|
|
- // Determine mode
|
|
136
|
|
- var mode = options.mode || new C.mode.OFB;
|
|
137
|
|
-
|
|
138
|
|
- // Allow mode to override options
|
|
139
|
|
- if (mode.fixOptions) mode.fixOptions(options);
|
|
140
|
|
-
|
|
141
|
|
- var
|
|
142
|
|
-
|
|
143
|
|
- // Convert to bytes if ciphertext is a string
|
|
144
|
|
- c = (
|
|
145
|
|
- ciphertext.constructor == String ?
|
|
146
|
|
- util.base64ToBytes(ciphertext):
|
|
147
|
|
- ciphertext
|
|
148
|
|
- ),
|
|
149
|
|
-
|
|
150
|
|
- // Separate IV and message
|
|
151
|
|
- iv = options.iv || c.splice(0, AES._blocksize * 4),
|
|
152
|
|
-
|
|
153
|
|
- // Generate key
|
|
154
|
|
- k = (
|
|
155
|
|
- password.constructor == String ?
|
|
156
|
|
- // Derive key from passphrase
|
|
157
|
|
- C.PBKDF2(password, iv, 32, { asBytes: true }) :
|
|
158
|
|
- // else, assume byte array representing cryptographic key
|
|
159
|
|
- password
|
|
160
|
|
- );
|
|
161
|
|
-
|
|
162
|
|
- // Decrypt
|
|
163
|
|
- AES._init(k);
|
|
164
|
|
- mode.decrypt(AES, c, iv);
|
|
165
|
|
-
|
|
166
|
|
- // Return plaintext
|
|
167
|
|
- return (options && options.asBytes) ? c : UTF8.bytesToString(c);
|
|
168
|
|
-
|
|
169
|
|
- },
|
|
170
|
|
-
|
|
171
|
|
-
|
|
172
|
|
- /**
|
|
173
|
|
- * Package private methods and properties
|
|
174
|
|
- */
|
|
175
|
|
-
|
|
176
|
|
- _blocksize: 4,
|
|
177
|
|
-
|
|
178
|
|
- _encryptblock: function (m, offset) {
|
|
179
|
|
-
|
|
180
|
|
- // Set input
|
|
181
|
|
- for (var row = 0; row < AES._blocksize; row++) {
|
|
182
|
|
- for (var col = 0; col < 4; col++)
|
|
183
|
|
- state[row][col] = m[offset + col * 4 + row];
|
|
184
|
|
- }
|
|
185
|
|
-
|
|
186
|
|
- // Add round key
|
|
187
|
|
- for (var row = 0; row < 4; row++) {
|
|
188
|
|
- for (var col = 0; col < 4; col++)
|
|
189
|
|
- state[row][col] ^= keyschedule[col][row];
|
|
190
|
|
- }
|
|
191
|
|
-
|
|
192
|
|
- for (var round = 1; round < nrounds; round++) {
|
|
193
|
|
-
|
|
194
|
|
- // Sub bytes
|
|
195
|
|
- for (var row = 0; row < 4; row++) {
|
|
196
|
|
- for (var col = 0; col < 4; col++)
|
|
197
|
|
- state[row][col] = SBOX[state[row][col]];
|
|
198
|
|
- }
|
|
199
|
|
-
|
|
200
|
|
- // Shift rows
|
|
201
|
|
- state[1].push(state[1].shift());
|
|
202
|
|
- state[2].push(state[2].shift());
|
|
203
|
|
- state[2].push(state[2].shift());
|
|
204
|
|
- state[3].unshift(state[3].pop());
|
|
205
|
|
-
|
|
206
|
|
- // Mix columns
|
|
207
|
|
- for (var col = 0; col < 4; col++) {
|
|
208
|
|
-
|
|
209
|
|
- var s0 = state[0][col],
|
|
210
|
|
- s1 = state[1][col],
|
|
211
|
|
- s2 = state[2][col],
|
|
212
|
|
- s3 = state[3][col];
|
|
213
|
|
-
|
|
214
|
|
- state[0][col] = MULT2[s0] ^ MULT3[s1] ^ s2 ^ s3;
|
|
215
|
|
- state[1][col] = s0 ^ MULT2[s1] ^ MULT3[s2] ^ s3;
|
|
216
|
|
- state[2][col] = s0 ^ s1 ^ MULT2[s2] ^ MULT3[s3];
|
|
217
|
|
- state[3][col] = MULT3[s0] ^ s1 ^ s2 ^ MULT2[s3];
|
|
218
|
|
-
|
|
219
|
|
- }
|
|
220
|
|
-
|
|
221
|
|
- // Add round key
|
|
222
|
|
- for (var row = 0; row < 4; row++) {
|
|
223
|
|
- for (var col = 0; col < 4; col++)
|
|
224
|
|
- state[row][col] ^= keyschedule[round * 4 + col][row];
|
|
225
|
|
- }
|
|
226
|
|
-
|
|
227
|
|
- }
|
|
228
|
|
-
|
|
229
|
|
- // Sub bytes
|
|
230
|
|
- for (var row = 0; row < 4; row++) {
|
|
231
|
|
- for (var col = 0; col < 4; col++)
|
|
232
|
|
- state[row][col] = SBOX[state[row][col]];
|
|
233
|
|
- }
|
|
234
|
|
-
|
|
235
|
|
- // Shift rows
|
|
236
|
|
- state[1].push(state[1].shift());
|
|
237
|
|
- state[2].push(state[2].shift());
|
|
238
|
|
- state[2].push(state[2].shift());
|
|
239
|
|
- state[3].unshift(state[3].pop());
|
|
240
|
|
-
|
|
241
|
|
- // Add round key
|
|
242
|
|
- for (var row = 0; row < 4; row++) {
|
|
243
|
|
- for (var col = 0; col < 4; col++)
|
|
244
|
|
- state[row][col] ^= keyschedule[nrounds * 4 + col][row];
|
|
245
|
|
- }
|
|
246
|
|
-
|
|
247
|
|
- // Set output
|
|
248
|
|
- for (var row = 0; row < AES._blocksize; row++) {
|
|
249
|
|
- for (var col = 0; col < 4; col++)
|
|
250
|
|
- m[offset + col * 4 + row] = state[row][col];
|
|
251
|
|
- }
|
|
252
|
|
-
|
|
253
|
|
- },
|
|
254
|
|
-
|
|
255
|
|
- _decryptblock: function (c, offset) {
|
|
256
|
|
-
|
|
257
|
|
- // Set input
|
|
258
|
|
- for (var row = 0; row < AES._blocksize; row++) {
|
|
259
|
|
- for (var col = 0; col < 4; col++)
|
|
260
|
|
- state[row][col] = c[offset + col * 4 + row];
|
|
261
|
|
- }
|
|
262
|
|
-
|
|
263
|
|
- // Add round key
|
|
264
|
|
- for (var row = 0; row < 4; row++) {
|
|
265
|
|
- for (var col = 0; col < 4; col++)
|
|
266
|
|
- state[row][col] ^= keyschedule[nrounds * 4 + col][row];
|
|
267
|
|
- }
|
|
268
|
|
-
|
|
269
|
|
- for (var round = 1; round < nrounds; round++) {
|
|
270
|
|
-
|
|
271
|
|
- // Inv shift rows
|
|
272
|
|
- state[1].unshift(state[1].pop());
|
|
273
|
|
- state[2].push(state[2].shift());
|
|
274
|
|
- state[2].push(state[2].shift());
|
|
275
|
|
- state[3].push(state[3].shift());
|
|
276
|
|
-
|
|
277
|
|
- // Inv sub bytes
|
|
278
|
|
- for (var row = 0; row < 4; row++) {
|
|
279
|
|
- for (var col = 0; col < 4; col++)
|
|
280
|
|
- state[row][col] = INVSBOX[state[row][col]];
|
|
281
|
|
- }
|
|
282
|
|
-
|
|
283
|
|
- // Add round key
|
|
284
|
|
- for (var row = 0; row < 4; row++) {
|
|
285
|
|
- for (var col = 0; col < 4; col++)
|
|
286
|
|
- state[row][col] ^= keyschedule[(nrounds - round) * 4 + col][row];
|
|
287
|
|
- }
|
|
288
|
|
-
|
|
289
|
|
- // Inv mix columns
|
|
290
|
|
- for (var col = 0; col < 4; col++) {
|
|
291
|
|
-
|
|
292
|
|
- var s0 = state[0][col],
|
|
293
|
|
- s1 = state[1][col],
|
|
294
|
|
- s2 = state[2][col],
|
|
295
|
|
- s3 = state[3][col];
|
|
296
|
|
-
|
|
297
|
|
- state[0][col] = MULTE[s0] ^ MULTB[s1] ^ MULTD[s2] ^ MULT9[s3];
|
|
298
|
|
- state[1][col] = MULT9[s0] ^ MULTE[s1] ^ MULTB[s2] ^ MULTD[s3];
|
|
299
|
|
- state[2][col] = MULTD[s0] ^ MULT9[s1] ^ MULTE[s2] ^ MULTB[s3];
|
|
300
|
|
- state[3][col] = MULTB[s0] ^ MULTD[s1] ^ MULT9[s2] ^ MULTE[s3];
|
|
301
|
|
-
|
|
302
|
|
- }
|
|
303
|
|
-
|
|
304
|
|
- }
|
|
305
|
|
-
|
|
306
|
|
- // Inv shift rows
|
|
307
|
|
- state[1].unshift(state[1].pop());
|
|
308
|
|
- state[2].push(state[2].shift());
|
|
309
|
|
- state[2].push(state[2].shift());
|
|
310
|
|
- state[3].push(state[3].shift());
|
|
311
|
|
-
|
|
312
|
|
- // Inv sub bytes
|
|
313
|
|
- for (var row = 0; row < 4; row++) {
|
|
314
|
|
- for (var col = 0; col < 4; col++)
|
|
315
|
|
- state[row][col] = INVSBOX[state[row][col]];
|
|
316
|
|
- }
|
|
317
|
|
-
|
|
318
|
|
- // Add round key
|
|
319
|
|
- for (var row = 0; row < 4; row++) {
|
|
320
|
|
- for (var col = 0; col < 4; col++)
|
|
321
|
|
- state[row][col] ^= keyschedule[col][row];
|
|
322
|
|
- }
|
|
323
|
|
-
|
|
324
|
|
- // Set output
|
|
325
|
|
- for (var row = 0; row < AES._blocksize; row++) {
|
|
326
|
|
- for (var col = 0; col < 4; col++)
|
|
327
|
|
- c[offset + col * 4 + row] = state[row][col];
|
|
328
|
|
- }
|
|
329
|
|
-
|
|
330
|
|
- },
|
|
331
|
|
-
|
|
332
|
|
-
|
|
333
|
|
- /**
|
|
334
|
|
- * Private methods
|
|
335
|
|
- */
|
|
336
|
|
-
|
|
337
|
|
- _init: function (k) {
|
|
338
|
|
- keylength = k.length / 4;
|
|
339
|
|
- nrounds = keylength + 6;
|
|
340
|
|
- AES._keyexpansion(k);
|
|
341
|
|
- },
|
|
342
|
|
-
|
|
343
|
|
- // Generate a key schedule
|
|
344
|
|
- _keyexpansion: function (k) {
|
|
345
|
|
-
|
|
346
|
|
- keyschedule = [];
|
|
347
|
|
-
|
|
348
|
|
- for (var row = 0; row < keylength; row++) {
|
|
349
|
|
- keyschedule[row] = [
|
|
350
|
|
- k[row * 4],
|
|
351
|
|
- k[row * 4 + 1],
|
|
352
|
|
- k[row * 4 + 2],
|
|
353
|
|
- k[row * 4 + 3]
|
|
354
|
|
- ];
|
|
355
|
|
- }
|
|
356
|
|
-
|
|
357
|
|
- for (var row = keylength; row < AES._blocksize * (nrounds + 1); row++) {
|
|
358
|
|
-
|
|
359
|
|
- var temp = [
|
|
360
|
|
- keyschedule[row - 1][0],
|
|
361
|
|
- keyschedule[row - 1][1],
|
|
362
|
|
- keyschedule[row - 1][2],
|
|
363
|
|
- keyschedule[row - 1][3]
|
|
364
|
|
- ];
|
|
365
|
|
-
|
|
366
|
|
- if (row % keylength == 0) {
|
|
367
|
|
-
|
|
368
|
|
- // Rot word
|
|
369
|
|
- temp.push(temp.shift());
|
|
370
|
|
-
|
|
371
|
|
- // Sub word
|
|
372
|
|
- temp[0] = SBOX[temp[0]];
|
|
373
|
|
- temp[1] = SBOX[temp[1]];
|
|
374
|
|
- temp[2] = SBOX[temp[2]];
|
|
375
|
|
- temp[3] = SBOX[temp[3]];
|
|
376
|
|
-
|
|
377
|
|
- temp[0] ^= RCON[row / keylength];
|
|
378
|
|
-
|
|
379
|
|
- } else if (keylength > 6 && row % keylength == 4) {
|
|
380
|
|
-
|
|
381
|
|
- // Sub word
|
|
382
|
|
- temp[0] = SBOX[temp[0]];
|
|
383
|
|
- temp[1] = SBOX[temp[1]];
|
|
384
|
|
- temp[2] = SBOX[temp[2]];
|
|
385
|
|
- temp[3] = SBOX[temp[3]];
|
|
386
|
|
-
|
|
387
|
|
- }
|
|
388
|
|
-
|
|
389
|
|
- keyschedule[row] = [
|
|
390
|
|
- keyschedule[row - keylength][0] ^ temp[0],
|
|
391
|
|
- keyschedule[row - keylength][1] ^ temp[1],
|
|
392
|
|
- keyschedule[row - keylength][2] ^ temp[2],
|
|
393
|
|
- keyschedule[row - keylength][3] ^ temp[3]
|
|
394
|
|
- ];
|
|
395
|
|
-
|
|
396
|
|
- }
|
|
397
|
|
-
|
|
398
|
|
- }
|
|
399
|
|
-
|
|
400
|
|
-};
|
|
401
|
|
-
|
|
402
|
|
-})();
|
|
|
1
|
+(function(){
|
|
|
2
|
+
|
|
|
3
|
+var C = (typeof window === 'undefined') ? require('./Crypto').Crypto : window.Crypto;
|
|
|
4
|
+
|
|
|
5
|
+// Shortcuts
|
|
|
6
|
+var util = C.util,
|
|
|
7
|
+ charenc = C.charenc,
|
|
|
8
|
+ UTF8 = charenc.UTF8;
|
|
|
9
|
+
|
|
|
10
|
+// Precomputed SBOX
|
|
|
11
|
+var SBOX = [ 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5,
|
|
|
12
|
+ 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
|
|
|
13
|
+ 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
|
|
|
14
|
+ 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
|
|
|
15
|
+ 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc,
|
|
|
16
|
+ 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34
|
+ 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
|
|
|
35
|
+ 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6,
|
|
|
36
|
+ 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
|
|
|
37
|
+ 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
|
|
|
38
|
+ 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
|
|
|
39
|
+ 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94,
|
|
|
40
|
+ 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
|
|
|
41
|
+ 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68,
|
|
|
42
|
+ 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 ];
|
|
|
43
|
+
|
|
|
44
|
+// Compute inverse SBOX lookup table
|
|
|
45
|
+for (var INVSBOX = [], i = 0; i < 256; i++) INVSBOX[SBOX[i]] = i;
|
|
|
46
|
+
|
|
|
47
|
+// Compute mulitplication in GF(2^8) lookup tables
|
|
|
48
|
+var MULT2 = [],
|
|
|
49
|
+ MULT3 = [],
|
|
|
50
|
+ MULT9 = [],
|
|
|
51
|
+ MULTB = [],
|
|
|
52
|
+ MULTD = [],
|
|
|
53
|
+ MULTE = [];
|
|
|
54
|
+
|
|
|
55
|
+function xtime(a, b) {
|
|
|
56
|
+ for (var result = 0, i = 0; i < 8; i++) {
|
|
|
57
|
+ if (b & 1) result ^= a;
|
|
|
58
|
+ var hiBitSet = a & 0x80;
|
|
|
59
|
+ a = (a << 1) & 0xFF;
|
|
|
60
|
+ if (hiBitSet) a ^= 0x1b;
|
|
|
61
|
+ b >>>= 1;
|
|
|
62
|
+ }
|
|
|
63
|
+ return result;
|
|
|
64
|
+}
|
|
|
65
|
+
|
|
|
66
|
+for (var i = 0; i < 256; i++) {
|
|
|
67
|
+ MULT2[i] = xtime(i,2);
|
|
|
68
|
+ MULT3[i] = xtime(i,3);
|
|
|
69
|
+ MULT9[i] = xtime(i,9);
|
|
|
70
|
+ MULTB[i] = xtime(i,0xB);
|
|
|
71
|
+ MULTD[i] = xtime(i,0xD);
|
|
|
72
|
+ MULTE[i] = xtime(i,0xE);
|
|
|
73
|
+}
|
|
|
74
|
+
|
|
|
75
|
+// Precomputed RCon lookup
|
|
|
76
|
+var RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36];
|
|
|
77
|
+
|
|
|
78
|
+// Inner state
|
|
|
79
|
+var state = [[], [], [], []],
|
|
|
80
|
+ keylength,
|
|
|
81
|
+ nrounds,
|
|
|
82
|
+ keyschedule;
|
|
|
83
|
+
|
|
|
84
|
+var AES = C.AES = {
|
|
|
85
|
+
|
|
|
86
|
+ /**
|
|
|
87
|
+ * Public API
|
|
|
88
|
+ */
|
|
|
89
|
+
|
|
|
90
|
+ encrypt: function (message, password, options) {
|
|
|
91
|
+
|
|
|
92
|
+ options = options || {};
|
|
|
93
|
+
|
|
|
94
|
+ // Determine mode
|
|
|
95
|
+ var mode = options.mode || new C.mode.OFB;
|
|
|
96
|
+
|
|
|
97
|
+ // Allow mode to override options
|
|
|
98
|
+ if (mode.fixOptions) mode.fixOptions(options);
|
|
|
99
|
+
|
|
|
100
|
+ var
|
|
|
101
|
+
|
|
|
102
|
+ // Convert to bytes if message is a string
|
|
|
103
|
+ m = (
|
|
|
104
|
+ message.constructor == String ?
|
|
|
105
|
+ UTF8.stringToBytes(message) :
|
|
|
106
|
+ message
|
|
|
107
|
+ ),
|
|
|
108
|
+
|
|
|
109
|
+ // Generate random IV
|
|
|
110
|
+ iv = options.iv || util.randomBytes(AES._blocksize * 4),
|
|
|
111
|
+
|
|
|
112
|
+ // Generate key
|
|
|
113
|
+ k = (
|
|
|
114
|
+ password.constructor == String ?
|
|
|
115
|
+ // Derive key from passphrase
|
|
|
116
|
+ C.PBKDF2(password, iv, 32, { asBytes: true }) :
|
|
|
117
|
+ // else, assume byte array representing cryptographic key
|
|
|
118
|
+ password
|
|
|
119
|
+ );
|
|
|
120
|
+
|
|
|
121
|
+ // Encrypt
|
|
|
122
|
+ AES._init(k);
|
|
|
123
|
+ mode.encrypt(AES, m, iv);
|
|
|
124
|
+
|
|
|
125
|
+ // Return ciphertext
|
|
|
126
|
+ m = options.iv ? m : iv.concat(m);
|
|
|
127
|
+ return (options && options.asBytes) ? m : util.bytesToBase64(m);
|
|
|
128
|
+
|
|
|
129
|
+ },
|
|
|
130
|
+
|
|
|
131
|
+ decrypt: function (ciphertext, password, options) {
|
|
|
132
|
+
|
|
|
133
|
+ options = options || {};
|
|
|
134
|
+
|
|
|
135
|
+ // Determine mode
|
|
|
136
|
+ var mode = options.mode || new C.mode.OFB;
|
|
|
137
|
+
|
|
|
138
|
+ // Allow mode to override options
|
|
|
139
|
+ if (mode.fixOptions) mode.fixOptions(options);
|
|
|
140
|
+
|
|
|
141
|
+ var
|
|
|
142
|
+
|
|
|
143
|
+ // Convert to bytes if ciphertext is a string
|
|
|
144
|
+ c = (
|
|
|
145
|
+ ciphertext.constructor == String ?
|
|
|
146
|
+ util.base64ToBytes(ciphertext):
|
|
|
147
|
+ ciphertext
|
|
|
148
|
+ ),
|
|
|
149
|
+
|
|
|
150
|
+ // Separate IV and message
|
|
|
151
|
+ iv = options.iv || c.splice(0, AES._blocksize * 4),
|
|
|
152
|
+
|
|
|
153
|
+ // Generate key
|
|
|
154
|
+ k = (
|
|
|
155
|
+ password.constructor == String ?
|
|
|
156
|
+ // Derive key from passphrase
|
|
|
157
|
+ C.PBKDF2(password, iv, 32, { asBytes: true }) :
|
|
|
158
|
+ // else, assume byte array representing cryptographic key
|
|
|
159
|
+ password
|
|
|
160
|
+ );
|
|
|
161
|
+
|
|
|
162
|
+ // Decrypt
|
|
|
163
|
+ AES._init(k);
|
|
|
164
|
+ mode.decrypt(AES, c, iv);
|
|
|
165
|
+
|
|
|
166
|
+ // Return plaintext
|
|
|
167
|
+ return (options && options.asBytes) ? c : UTF8.bytesToString(c);
|
|
|
168
|
+
|
|
|
169
|
+ },
|
|
|
170
|
+
|
|
|
171
|
+
|
|
|
172
|
+ /**
|
|
|
173
|
+ * Package private methods and properties
|
|
|
174
|
+ */
|
|
|
175
|
+
|
|
|
176
|
+ _blocksize: 4,
|
|
|
177
|
+
|
|
|
178
|
+ _encryptblock: function (m, offset) {
|
|
|
179
|
+
|
|
|
180
|
+ // Set input
|
|
|
181
|
+ for (var row = 0; row < AES._blocksize; row++) {
|
|
|
182
|
+ for (var col = 0; col < 4; col++)
|
|
|
183
|
+ state[row][col] = m[offset + col * 4 + row];
|
|
|
184
|
+ }
|
|
|
185
|
+
|
|
|
186
|
+ // Add round key
|
|
|
187
|
+ for (var row = 0; row < 4; row++) {
|
|
|
188
|
+ for (var col = 0; col < 4; col++)
|
|
|
189
|
+ state[row][col] ^= keyschedule[col][row];
|
|
|
190
|
+ }
|
|
|
191
|
+
|
|
|
192
|
+ for (var round = 1; round < nrounds; round++) {
|
|
|
193
|
+
|
|
|
194
|
+ // Sub bytes
|
|
|
195
|
+ for (var row = 0; row < 4; row++) {
|
|
|
196
|
+ for (var col = 0; col < 4; col++)
|
|
|
197
|
+ state[row][col] = SBOX[state[row][col]];
|
|
|
198
|
+ }
|
|
|
199
|
+
|
|
|
200
|
+ // Shift rows
|
|
|
201
|
+ state[1].push(state[1].shift());
|
|
|
202
|
+ state[2].push(state[2].shift());
|
|
|
203
|
+ state[2].push(state[2].shift());
|
|
|
204
|
+ state[3].unshift(state[3].pop());
|
|
|
205
|
+
|
|
|
206
|
+ // Mix columns
|
|
|
207
|
+ for (var col = 0; col < 4; col++) {
|
|
|
208
|
+
|
|
|
209
|
+ var s0 = state[0][col],
|
|
|
210
|
+ s1 = state[1][col],
|
|
|
211
|
+ s2 = state[2][col],
|
|
|
212
|
+ s3 = state[3][col];
|
|
|
213
|
+
|
|
|
214
|
+ state[0][col] = MULT2[s0] ^ MULT3[s1] ^ s2 ^ s3;
|
|
|
215
|
+ state[1][col] = s0 ^ MULT2[s1] ^ MULT3[s2] ^ s3;
|
|
|
216
|
+ state[2][col] = s0 ^ s1 ^ MULT2[s2] ^ MULT3[s3];
|
|
|
217
|
+ state[3][col] = MULT3[s0] ^ s1 ^ s2 ^ MULT2[s3];
|
|
|
218
|
+
|
|
|
219
|
+ }
|
|
|
220
|
+
|
|
|
221
|
+ // Add round key
|
|
|
222
|
+ for (var row = 0; row < 4; row++) {
|
|
|
223
|
+ for (var col = 0; col < 4; col++)
|
|
|
224
|
+ state[row][col] ^= keyschedule[round * 4 + col][row];
|
|
|
225
|
+ }
|
|
|
226
|
+
|
|
|
227
|
+ }
|
|
|
228
|
+
|
|
|
229
|
+ // Sub bytes
|
|
|
230
|
+ for (var row = 0; row < 4; row++) {
|
|
|
231
|
+ for (var col = 0; col < 4; col++)
|
|
|
232
|
+ state[row][col] = SBOX[state[row][col]];
|
|
|
233
|
+ }
|
|
|
234
|
+
|
|
|
235
|
+ // Shift rows
|
|
|
236
|
+ state[1].push(state[1].shift());
|
|
|
237
|
+ state[2].push(state[2].shift());
|
|
|
238
|
+ state[2].push(state[2].shift());
|
|
|
239
|
+ state[3].unshift(state[3].pop());
|
|
|
240
|
+
|
|
|
241
|
+ // Add round key
|
|
|
242
|
+ for (var row = 0; row < 4; row++) {
|
|
|
243
|
+ for (var col = 0; col < 4; col++)
|
|
|
244
|
+ state[row][col] ^= keyschedule[nrounds * 4 + col][row];
|
|
|
245
|
+ }
|
|
|
246
|
+
|
|
|
247
|
+ // Set output
|
|
|
248
|
+ for (var row = 0; row < AES._blocksize; row++) {
|
|
|
249
|
+ for (var col = 0; col < 4; col++)
|
|
|
250
|
+ m[offset + col * 4 + row] = state[row][col];
|
|
|
251
|
+ }
|
|
|
252
|
+
|
|
|
253
|
+ },
|
|
|
254
|
+
|
|
|
255
|
+ _decryptblock: function (c, offset) {
|
|
|
256
|
+
|
|
|
257
|
+ // Set input
|
|
|
258
|
+ for (var row = 0; row < AES._blocksize; row++) {
|
|
|
259
|
+ for (var col = 0; col < 4; col++)
|
|
|
260
|
+ state[row][col] = c[offset + col * 4 + row];
|
|
|
261
|
+ }
|
|
|
262
|
+
|
|
|
263
|
+ // Add round key
|
|
|
264
|
+ for (var row = 0; row < 4; row++) {
|
|
|
265
|
+ for (var col = 0; col < 4; col++)
|
|
|
266
|
+ state[row][col] ^= keyschedule[nrounds * 4 + col][row];
|
|
|
267
|
+ }
|
|
|
268
|
+
|
|
|
269
|
+ for (var round = 1; round < nrounds; round++) {
|
|
|
270
|
+
|
|
|
271
|
+ // Inv shift rows
|
|
|
272
|
+ state[1].unshift(state[1].pop());
|
|
|
273
|
+ state[2].push(state[2].shift());
|
|
|
274
|
+ state[2].push(state[2].shift());
|
|
|
275
|
+ state[3].push(state[3].shift());
|
|
|
276
|
+
|
|
|
277
|
+ // Inv sub bytes
|
|
|
278
|
+ for (var row = 0; row < 4; row++) {
|
|
|
279
|
+ for (var col = 0; col < 4; col++)
|
|
|
280
|
+ state[row][col] = INVSBOX[state[row][col]];
|
|
|
281
|
+ }
|
|
|
282
|
+
|
|
|
283
|
+ // Add round key
|
|
|
284
|
+ for (var row = 0; row < 4; row++) {
|
|
|
285
|
+ for (var col = 0; col < 4; col++)
|
|
|
286
|
+ state[row][col] ^= keyschedule[(nrounds - round) * 4 + col][row];
|
|
|
287
|
+ }
|
|
|
288
|
+
|
|
|
289
|
+ // Inv mix columns
|
|
|
290
|
+ for (var col = 0; col < 4; col++) {
|
|
|
291
|
+
|
|
|
292
|
+ var s0 = state[0][col],
|
|
|
293
|
+ s1 = state[1][col],
|
|
|
294
|
+ s2 = state[2][col],
|
|
|
295
|
+ s3 = state[3][col];
|
|
|
296
|
+
|
|
|
297
|
+ state[0][col] = MULTE[s0] ^ MULTB[s1] ^ MULTD[s2] ^ MULT9[s3];
|
|
|
298
|
+ state[1][col] = MULT9[s0] ^ MULTE[s1] ^ MULTB[s2] ^ MULTD[s3];
|
|
|
299
|
+ state[2][col] = MULTD[s0] ^ MULT9[s1] ^ MULTE[s2] ^ MULTB[s3];
|
|
|
300
|
+ state[3][col] = MULTB[s0] ^ MULTD[s1] ^ MULT9[s2] ^ MULTE[s3];
|
|
|
301
|
+
|
|
|
302
|
+ }
|
|
|
303
|
+
|
|
|
304
|
+ }
|
|
|
305
|
+
|
|
|
306
|
+ // Inv shift rows
|
|
|
307
|
+ state[1].unshift(state[1].pop());
|
|
|
308
|
+ state[2].push(state[2].shift());
|
|
|
309
|
+ state[2].push(state[2].shift());
|
|
|
310
|
+ state[3].push(state[3].shift());
|
|
|
311
|
+
|
|
|
312
|
+ // Inv sub bytes
|
|
|
313
|
+ for (var row = 0; row < 4; row++) {
|
|
|
314
|
+ for (var col = 0; col < 4; col++)
|
|
|
315
|
+ state[row][col] = INVSBOX[state[row][col]];
|
|
|
316
|
+ }
|
|
|
317
|
+
|
|
|
318
|
+ // Add round key
|
|
|
319
|
+ for (var row = 0; row < 4; row++) {
|
|
|
320
|
+ for (var col = 0; col < 4; col++)
|
|
|
321
|
+ state[row][col] ^= keyschedule[col][row];
|
|
|
322
|
+ }
|
|
|
323
|
+
|
|
|
324
|
+ // Set output
|
|
|
325
|
+ for (var row = 0; row < AES._blocksize; row++) {
|
|
|
326
|
+ for (var col = 0; col < 4; col++)
|
|
|
327
|
+ c[offset + col * 4 + row] = state[row][col];
|
|
|
328
|
+ }
|
|
|
329
|
+
|
|
|
330
|
+ },
|
|
|
331
|
+
|
|
|
332
|
+
|
|
|
333
|
+ /**
|
|
|
334
|
+ * Private methods
|
|
|
335
|
+ */
|
|
|
336
|
+
|
|
|
337
|
+ _init: function (k) {
|
|
|
338
|
+ keylength = k.length / 4;
|
|
|
339
|
+ nrounds = keylength + 6;
|
|
|
340
|
+ AES._keyexpansion(k);
|
|
|
341
|
+ },
|
|
|
342
|
+
|
|
|
343
|
+ // Generate a key schedule
|
|
|
344
|
+ _keyexpansion: function (k) {
|
|
|
345
|
+
|
|
|
346
|
+ keyschedule = [];
|
|
|
347
|
+
|
|
|
348
|
+ for (var row = 0; row < keylength; row++) {
|
|
|
349
|
+ keyschedule[row] = [
|
|
|
350
|
+ k[row * 4],
|
|
|
351
|
+ k[row * 4 + 1],
|
|
|
352
|
+ k[row * 4 + 2],
|
|
|
353
|
+ k[row * 4 + 3]
|
|
|
354
|
+ ];
|
|
|
355
|
+ }
|
|
|
356
|
+
|
|
|
357
|
+ for (var row = keylength; row < AES._blocksize * (nrounds + 1); row++) {
|
|
|
358
|
+
|
|
|
359
|
+ var temp = [
|
|
|
360
|
+ keyschedule[row - 1][0],
|
|
|
361
|
+ keyschedule[row - 1][1],
|
|
|
362
|
+ keyschedule[row - 1][2],
|
|
|
363
|
+ keyschedule[row - 1][3]
|
|
|
364
|
+ ];
|
|
|
365
|
+
|
|
|
366
|
+ if (row % keylength == 0) {
|
|
|
367
|
+
|
|
|
368
|
+ // Rot word
|
|
|
369
|
+ temp.push(temp.shift());
|
|
|
370
|
+
|
|
|
371
|
+ // Sub word
|
|
|
372
|
+ temp[0] = SBOX[temp[0]];
|
|
|
373
|
+ temp[1] = SBOX[temp[1]];
|
|
|
374
|
+ temp[2] = SBOX[temp[2]];
|
|
|
375
|
+ temp[3] = SBOX[temp[3]];
|
|
|
376
|
+
|
|
|
377
|
+ temp[0] ^= RCON[row / keylength];
|
|
|
378
|
+
|
|
|
379
|
+ } else if (keylength > 6 && row % keylength == 4) {
|
|
|
380
|
+
|
|
|
381
|
+ // Sub word
|
|
|
382
|
+ temp[0] = SBOX[temp[0]];
|
|
|
383
|
+ temp[1] = SBOX[temp[1]];
|
|
|
384
|
+ temp[2] = SBOX[temp[2]];
|
|
|
385
|
+ temp[3] = SBOX[temp[3]];
|
|
|
386
|
+
|
|
|
387
|
+ }
|
|
|
388
|
+
|
|
|
389
|
+ keyschedule[row] = [
|
|
|
390
|
+ keyschedule[row - keylength][0] ^ temp[0],
|
|
|
391
|
+ keyschedule[row - keylength][1] ^ temp[1],
|
|
|
392
|
+ keyschedule[row - keylength][2] ^ temp[2],
|
|
|
393
|
+ keyschedule[row - keylength][3] ^ temp[3]
|
|
|
394
|
+ ];
|
|
|
395
|
+
|
|
|
396
|
+ }
|
|
|
397
|
+
|
|
|
398
|
+ }
|
|
|
399
|
+
|
|
|
400
|
+};
|
|
|
401
|
+
|
|
|
402
|
+})();
|