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