Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/AESCrypt.java
41161 views
1
/*
2
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
/* $Id: Rijndael.java,v 1.6 2000/02/10 01:31:41 gelderen Exp $
27
*
28
* Copyright (C) 1995-2000 The Cryptix Foundation Limited.
29
* All rights reserved.
30
*
31
* Use, modification, copying and distribution of this softwareas is subject
32
* the terms and conditions of the Cryptix General Licence. You should have
33
* received a copy of the Cryptix General Licence along with this library;
34
* if not, you can download a copy from http://www.cryptix.org/ .
35
*/
36
37
package com.sun.crypto.provider;
38
39
import java.security.InvalidKeyException;
40
import java.security.MessageDigest;
41
import java.util.Arrays;
42
43
import jdk.internal.vm.annotation.IntrinsicCandidate;
44
45
/**
46
* Rijndael --pronounced Reindaal-- is a symmetric cipher with a 128-bit
47
* block size and variable key-size (128-, 192- and 256-bit).
48
* <p>
49
* Rijndael was designed by <a href="mailto:[email protected]">Vincent
50
* Rijmen</a> and <a href="mailto:[email protected]">Joan Daemen</a>.
51
*/
52
final class AESCrypt extends SymmetricCipher implements AESConstants
53
{
54
private boolean ROUNDS_12 = false;
55
private boolean ROUNDS_14 = false;
56
57
/** Session and Sub keys */
58
private int[][] sessionK = null;
59
private int[] K = null;
60
61
/** Cipher encryption/decryption key */
62
// skip re-generating Session and Sub keys if the cipher key is
63
// the same
64
private byte[] lastKey = null;
65
66
/** ROUNDS * 4 */
67
private int limit = 0;
68
69
AESCrypt() {
70
// empty
71
}
72
73
/**
74
* Returns this cipher's block size.
75
*
76
* @return this cipher's block size
77
*/
78
int getBlockSize() {
79
return AES_BLOCK_SIZE;
80
}
81
82
void init(boolean decrypting, String algorithm, byte[] key)
83
throws InvalidKeyException {
84
if (!algorithm.equalsIgnoreCase("AES")
85
&& !algorithm.equalsIgnoreCase("Rijndael")) {
86
throw new InvalidKeyException
87
("Wrong algorithm: AES or Rijndael required");
88
}
89
if (!isKeySizeValid(key.length)) {
90
throw new InvalidKeyException("Invalid AES key length: " +
91
key.length + " bytes");
92
}
93
94
if (!MessageDigest.isEqual(key, lastKey)) {
95
// re-generate session key 'sessionK' when cipher key changes
96
makeSessionKey(key);
97
if (lastKey != null) {
98
Arrays.fill(lastKey, (byte)0);
99
}
100
lastKey = key.clone(); // save cipher key
101
}
102
103
// set sub key to the corresponding session Key
104
this.K = sessionK[(decrypting? 1:0)];
105
}
106
107
/**
108
* Expand an int[(ROUNDS+1)][4] into int[(ROUNDS+1)*4].
109
* For decryption round keys, need to rotate right by 4 ints.
110
* @param kr The round keys for encryption or decryption.
111
* @param decrypting True if 'kr' is for decryption and false otherwise.
112
*/
113
private static final int[] expandToSubKey(int[][] kr, boolean decrypting) {
114
int total = kr.length;
115
int[] expK = new int[total*4];
116
if (decrypting) {
117
// decrypting, rotate right by 4 ints
118
// i.e. i==0
119
for(int j=0; j<4; j++) {
120
expK[j] = kr[total-1][j];
121
}
122
for(int i=1; i<total; i++) {
123
for(int j=0; j<4; j++) {
124
expK[i*4 + j] = kr[i-1][j];
125
}
126
}
127
} else {
128
// encrypting, straight expansion
129
for(int i=0; i<total; i++) {
130
for(int j=0; j<4; j++) {
131
expK[i*4 + j] = kr[i][j];
132
}
133
}
134
}
135
return expK;
136
}
137
138
private static int[]
139
alog = new int[256],
140
log = new int[256];
141
142
private static final byte[]
143
S = new byte[256],
144
Si = new byte[256];
145
146
private static final int[]
147
T1 = new int[256],
148
T2 = new int[256],
149
T3 = new int[256],
150
T4 = new int[256],
151
T5 = new int[256],
152
T6 = new int[256],
153
T7 = new int[256],
154
T8 = new int[256];
155
156
private static final int[]
157
U1 = new int[256],
158
U2 = new int[256],
159
U3 = new int[256],
160
U4 = new int[256];
161
162
private static final byte[] rcon = new byte[30];
163
164
165
// Static code - to intialise S-boxes and T-boxes
166
static
167
{
168
int ROOT = 0x11B;
169
int i, j = 0;
170
171
//
172
// produce log and alog tables, needed for multiplying in the
173
// field GF(2^m) (generator = 3)
174
//
175
alog[0] = 1;
176
for (i = 1; i < 256; i++)
177
{
178
j = (alog[i-1] << 1) ^ alog[i-1];
179
if ((j & 0x100) != 0) {
180
j ^= ROOT;
181
}
182
alog[i] = j;
183
}
184
for (i = 1; i < 255; i++) {
185
log[alog[i]] = i;
186
}
187
byte[][] A = new byte[][]
188
{
189
{1, 1, 1, 1, 1, 0, 0, 0},
190
{0, 1, 1, 1, 1, 1, 0, 0},
191
{0, 0, 1, 1, 1, 1, 1, 0},
192
{0, 0, 0, 1, 1, 1, 1, 1},
193
{1, 0, 0, 0, 1, 1, 1, 1},
194
{1, 1, 0, 0, 0, 1, 1, 1},
195
{1, 1, 1, 0, 0, 0, 1, 1},
196
{1, 1, 1, 1, 0, 0, 0, 1}
197
};
198
byte[] B = new byte[] { 0, 1, 1, 0, 0, 0, 1, 1};
199
200
//
201
// substitution box based on F^{-1}(x)
202
//
203
int t;
204
byte[][] box = new byte[256][8];
205
box[1][7] = 1;
206
for (i = 2; i < 256; i++) {
207
j = alog[255 - log[i]];
208
for (t = 0; t < 8; t++) {
209
box[i][t] = (byte)((j >>> (7 - t)) & 0x01);
210
}
211
}
212
//
213
// affine transform: box[i] <- B + A*box[i]
214
//
215
byte[][] cox = new byte[256][8];
216
for (i = 0; i < 256; i++) {
217
for (t = 0; t < 8; t++) {
218
cox[i][t] = B[t];
219
for (j = 0; j < 8; j++) {
220
cox[i][t] ^= A[t][j] * box[i][j];
221
}
222
}
223
}
224
//
225
// S-boxes and inverse S-boxes
226
//
227
for (i = 0; i < 256; i++) {
228
S[i] = (byte)(cox[i][0] << 7);
229
for (t = 1; t < 8; t++) {
230
S[i] ^= cox[i][t] << (7-t);
231
}
232
Si[S[i] & 0xFF] = (byte) i;
233
}
234
//
235
// T-boxes
236
//
237
byte[][] G = new byte[][] {
238
{2, 1, 1, 3},
239
{3, 2, 1, 1},
240
{1, 3, 2, 1},
241
{1, 1, 3, 2}
242
};
243
byte[][] AA = new byte[4][8];
244
for (i = 0; i < 4; i++) {
245
for (j = 0; j < 4; j++) AA[i][j] = G[i][j];
246
AA[i][i+4] = 1;
247
}
248
byte pivot, tmp;
249
byte[][] iG = new byte[4][4];
250
for (i = 0; i < 4; i++) {
251
pivot = AA[i][i];
252
if (pivot == 0) {
253
t = i + 1;
254
while ((AA[t][i] == 0) && (t < 4)) {
255
t++;
256
}
257
if (t == 4) {
258
throw new RuntimeException("G matrix is not invertible");
259
}
260
else {
261
for (j = 0; j < 8; j++) {
262
tmp = AA[i][j];
263
AA[i][j] = AA[t][j];
264
AA[t][j] = tmp;
265
}
266
pivot = AA[i][i];
267
}
268
}
269
for (j = 0; j < 8; j++) {
270
if (AA[i][j] != 0) {
271
AA[i][j] = (byte)
272
alog[(255 + log[AA[i][j] & 0xFF] - log[pivot & 0xFF])
273
% 255];
274
}
275
}
276
for (t = 0; t < 4; t++) {
277
if (i != t) {
278
for (j = i+1; j < 8; j++) {
279
AA[t][j] ^= mul(AA[i][j], AA[t][i]);
280
}
281
AA[t][i] = 0;
282
}
283
}
284
}
285
for (i = 0; i < 4; i++) {
286
for (j = 0; j < 4; j++) {
287
iG[i][j] = AA[i][j + 4];
288
}
289
}
290
291
int s;
292
for (t = 0; t < 256; t++) {
293
s = S[t];
294
T1[t] = mul4(s, G[0]);
295
T2[t] = mul4(s, G[1]);
296
T3[t] = mul4(s, G[2]);
297
T4[t] = mul4(s, G[3]);
298
299
s = Si[t];
300
T5[t] = mul4(s, iG[0]);
301
T6[t] = mul4(s, iG[1]);
302
T7[t] = mul4(s, iG[2]);
303
T8[t] = mul4(s, iG[3]);
304
305
U1[t] = mul4(t, iG[0]);
306
U2[t] = mul4(t, iG[1]);
307
U3[t] = mul4(t, iG[2]);
308
U4[t] = mul4(t, iG[3]);
309
}
310
//
311
// round constants
312
//
313
rcon[0] = 1;
314
int r = 1;
315
for (t = 1; t < 30; t++) {
316
r = mul(2, r);
317
rcon[t] = (byte) r;
318
}
319
log = null;
320
alog = null;
321
}
322
323
// multiply two elements of GF(2^m)
324
private static final int mul (int a, int b) {
325
return (a != 0 && b != 0) ?
326
alog[(log[a & 0xFF] + log[b & 0xFF]) % 255] :
327
0;
328
}
329
330
// convenience method used in generating Transposition boxes
331
private static final int mul4 (int a, byte[] b) {
332
if (a == 0) return 0;
333
a = log[a & 0xFF];
334
int a0 = (b[0] != 0) ? alog[(a + log[b[0] & 0xFF]) % 255] & 0xFF : 0;
335
int a1 = (b[1] != 0) ? alog[(a + log[b[1] & 0xFF]) % 255] & 0xFF : 0;
336
int a2 = (b[2] != 0) ? alog[(a + log[b[2] & 0xFF]) % 255] & 0xFF : 0;
337
int a3 = (b[3] != 0) ? alog[(a + log[b[3] & 0xFF]) % 255] & 0xFF : 0;
338
return a0 << 24 | a1 << 16 | a2 << 8 | a3;
339
}
340
341
// check if the specified length (in bytes) is a valid keysize for AES
342
static final boolean isKeySizeValid(int len) {
343
for (int i = 0; i < AES_KEYSIZES.length; i++) {
344
if (len == AES_KEYSIZES[i]) {
345
return true;
346
}
347
}
348
return false;
349
}
350
351
/**
352
* Encrypt exactly one block of plaintext.
353
*/
354
void encryptBlock(byte[] in, int inOffset,
355
byte[] out, int outOffset) {
356
// Array bound checks are done in caller code, i.e.
357
// FeedbackCipher.encrypt/decrypt(...) to improve performance.
358
implEncryptBlock(in, inOffset, out, outOffset);
359
}
360
361
// Encryption operation. Possibly replaced with a compiler intrinsic.
362
@IntrinsicCandidate
363
private void implEncryptBlock(byte[] in, int inOffset,
364
byte[] out, int outOffset)
365
{
366
int keyOffset = 0;
367
int t0 = ((in[inOffset++] ) << 24 |
368
(in[inOffset++] & 0xFF) << 16 |
369
(in[inOffset++] & 0xFF) << 8 |
370
(in[inOffset++] & 0xFF) ) ^ K[keyOffset++];
371
int t1 = ((in[inOffset++] ) << 24 |
372
(in[inOffset++] & 0xFF) << 16 |
373
(in[inOffset++] & 0xFF) << 8 |
374
(in[inOffset++] & 0xFF) ) ^ K[keyOffset++];
375
int t2 = ((in[inOffset++] ) << 24 |
376
(in[inOffset++] & 0xFF) << 16 |
377
(in[inOffset++] & 0xFF) << 8 |
378
(in[inOffset++] & 0xFF) ) ^ K[keyOffset++];
379
int t3 = ((in[inOffset++] ) << 24 |
380
(in[inOffset++] & 0xFF) << 16 |
381
(in[inOffset++] & 0xFF) << 8 |
382
(in[inOffset++] & 0xFF) ) ^ K[keyOffset++];
383
384
// apply round transforms
385
while( keyOffset < limit )
386
{
387
int a0, a1, a2;
388
a0 = T1[(t0 >>> 24) ] ^
389
T2[(t1 >>> 16) & 0xFF] ^
390
T3[(t2 >>> 8) & 0xFF] ^
391
T4[(t3 ) & 0xFF] ^ K[keyOffset++];
392
a1 = T1[(t1 >>> 24) ] ^
393
T2[(t2 >>> 16) & 0xFF] ^
394
T3[(t3 >>> 8) & 0xFF] ^
395
T4[(t0 ) & 0xFF] ^ K[keyOffset++];
396
a2 = T1[(t2 >>> 24) ] ^
397
T2[(t3 >>> 16) & 0xFF] ^
398
T3[(t0 >>> 8) & 0xFF] ^
399
T4[(t1 ) & 0xFF] ^ K[keyOffset++];
400
t3 = T1[(t3 >>> 24) ] ^
401
T2[(t0 >>> 16) & 0xFF] ^
402
T3[(t1 >>> 8) & 0xFF] ^
403
T4[(t2 ) & 0xFF] ^ K[keyOffset++];
404
t0 = a0; t1 = a1; t2 = a2;
405
}
406
407
// last round is special
408
int tt = K[keyOffset++];
409
out[outOffset++] = (byte)(S[(t0 >>> 24) ] ^ (tt >>> 24));
410
out[outOffset++] = (byte)(S[(t1 >>> 16) & 0xFF] ^ (tt >>> 16));
411
out[outOffset++] = (byte)(S[(t2 >>> 8) & 0xFF] ^ (tt >>> 8));
412
out[outOffset++] = (byte)(S[(t3 ) & 0xFF] ^ (tt ));
413
tt = K[keyOffset++];
414
out[outOffset++] = (byte)(S[(t1 >>> 24) ] ^ (tt >>> 24));
415
out[outOffset++] = (byte)(S[(t2 >>> 16) & 0xFF] ^ (tt >>> 16));
416
out[outOffset++] = (byte)(S[(t3 >>> 8) & 0xFF] ^ (tt >>> 8));
417
out[outOffset++] = (byte)(S[(t0 ) & 0xFF] ^ (tt ));
418
tt = K[keyOffset++];
419
out[outOffset++] = (byte)(S[(t2 >>> 24) ] ^ (tt >>> 24));
420
out[outOffset++] = (byte)(S[(t3 >>> 16) & 0xFF] ^ (tt >>> 16));
421
out[outOffset++] = (byte)(S[(t0 >>> 8) & 0xFF] ^ (tt >>> 8));
422
out[outOffset++] = (byte)(S[(t1 ) & 0xFF] ^ (tt ));
423
tt = K[keyOffset++];
424
out[outOffset++] = (byte)(S[(t3 >>> 24) ] ^ (tt >>> 24));
425
out[outOffset++] = (byte)(S[(t0 >>> 16) & 0xFF] ^ (tt >>> 16));
426
out[outOffset++] = (byte)(S[(t1 >>> 8) & 0xFF] ^ (tt >>> 8));
427
out[outOffset ] = (byte)(S[(t2 ) & 0xFF] ^ (tt ));
428
}
429
430
/**
431
* Decrypt exactly one block of plaintext.
432
*/
433
void decryptBlock(byte[] in, int inOffset,
434
byte[] out, int outOffset) {
435
// Array bound checks are done in caller code, i.e.
436
// FeedbackCipher.encrypt/decrypt(...) to improve performance.
437
implDecryptBlock(in, inOffset, out, outOffset);
438
}
439
440
// Decrypt operation. Possibly replaced with a compiler intrinsic.
441
@IntrinsicCandidate
442
private void implDecryptBlock(byte[] in, int inOffset,
443
byte[] out, int outOffset)
444
{
445
int keyOffset = 4;
446
int t0 = ((in[inOffset++] ) << 24 |
447
(in[inOffset++] & 0xFF) << 16 |
448
(in[inOffset++] & 0xFF) << 8 |
449
(in[inOffset++] & 0xFF) ) ^ K[keyOffset++];
450
int t1 = ((in[inOffset++] ) << 24 |
451
(in[inOffset++] & 0xFF) << 16 |
452
(in[inOffset++] & 0xFF) << 8 |
453
(in[inOffset++] & 0xFF) ) ^ K[keyOffset++];
454
int t2 = ((in[inOffset++] ) << 24 |
455
(in[inOffset++] & 0xFF) << 16 |
456
(in[inOffset++] & 0xFF) << 8 |
457
(in[inOffset++] & 0xFF) ) ^ K[keyOffset++];
458
int t3 = ((in[inOffset++] ) << 24 |
459
(in[inOffset++] & 0xFF) << 16 |
460
(in[inOffset++] & 0xFF) << 8 |
461
(in[inOffset ] & 0xFF) ) ^ K[keyOffset++];
462
463
int a0, a1, a2;
464
if(ROUNDS_12)
465
{
466
a0 = T5[(t0>>>24) ] ^ T6[(t3>>>16)&0xFF] ^
467
T7[(t2>>> 8)&0xFF] ^ T8[(t1 )&0xFF] ^ K[keyOffset++];
468
a1 = T5[(t1>>>24) ] ^ T6[(t0>>>16)&0xFF] ^
469
T7[(t3>>> 8)&0xFF] ^ T8[(t2 )&0xFF] ^ K[keyOffset++];
470
a2 = T5[(t2>>>24) ] ^ T6[(t1>>>16)&0xFF] ^
471
T7[(t0>>> 8)&0xFF] ^ T8[(t3 )&0xFF] ^ K[keyOffset++];
472
t3 = T5[(t3>>>24) ] ^ T6[(t2>>>16)&0xFF] ^
473
T7[(t1>>> 8)&0xFF] ^ T8[(t0 )&0xFF] ^ K[keyOffset++];
474
t0 = T5[(a0>>>24) ] ^ T6[(t3>>>16)&0xFF] ^
475
T7[(a2>>> 8)&0xFF] ^ T8[(a1 )&0xFF] ^ K[keyOffset++];
476
t1 = T5[(a1>>>24) ] ^ T6[(a0>>>16)&0xFF] ^
477
T7[(t3>>> 8)&0xFF] ^ T8[(a2 )&0xFF] ^ K[keyOffset++];
478
t2 = T5[(a2>>>24) ] ^ T6[(a1>>>16)&0xFF] ^
479
T7[(a0>>> 8)&0xFF] ^ T8[(t3 )&0xFF] ^ K[keyOffset++];
480
t3 = T5[(t3>>>24) ] ^ T6[(a2>>>16)&0xFF] ^
481
T7[(a1>>> 8)&0xFF] ^ T8[(a0 )&0xFF] ^ K[keyOffset++];
482
483
if(ROUNDS_14)
484
{
485
a0 = T5[(t0>>>24) ] ^ T6[(t3>>>16)&0xFF] ^
486
T7[(t2>>> 8)&0xFF] ^ T8[(t1 )&0xFF] ^ K[keyOffset++];
487
a1 = T5[(t1>>>24) ] ^ T6[(t0>>>16)&0xFF] ^
488
T7[(t3>>> 8)&0xFF] ^ T8[(t2 )&0xFF] ^ K[keyOffset++];
489
a2 = T5[(t2>>>24) ] ^ T6[(t1>>>16)&0xFF] ^
490
T7[(t0>>> 8)&0xFF] ^ T8[(t3 )&0xFF] ^ K[keyOffset++];
491
t3 = T5[(t3>>>24) ] ^ T6[(t2>>>16)&0xFF] ^
492
T7[(t1>>> 8)&0xFF] ^ T8[(t0 )&0xFF] ^ K[keyOffset++];
493
t0 = T5[(a0>>>24) ] ^ T6[(t3>>>16)&0xFF] ^
494
T7[(a2>>> 8)&0xFF] ^ T8[(a1 )&0xFF] ^ K[keyOffset++];
495
t1 = T5[(a1>>>24) ] ^ T6[(a0>>>16)&0xFF] ^
496
T7[(t3>>> 8)&0xFF] ^ T8[(a2 )&0xFF] ^ K[keyOffset++];
497
t2 = T5[(a2>>>24) ] ^ T6[(a1>>>16)&0xFF] ^
498
T7[(a0>>> 8)&0xFF] ^ T8[(t3 )&0xFF] ^ K[keyOffset++];
499
t3 = T5[(t3>>>24) ] ^ T6[(a2>>>16)&0xFF] ^
500
T7[(a1>>> 8)&0xFF] ^ T8[(a0 )&0xFF] ^ K[keyOffset++];
501
}
502
}
503
a0 = T5[(t0>>>24) ] ^ T6[(t3>>>16)&0xFF] ^
504
T7[(t2>>> 8)&0xFF] ^ T8[(t1 )&0xFF] ^ K[keyOffset++];
505
a1 = T5[(t1>>>24) ] ^ T6[(t0>>>16)&0xFF] ^
506
T7[(t3>>> 8)&0xFF] ^ T8[(t2 )&0xFF] ^ K[keyOffset++];
507
a2 = T5[(t2>>>24) ] ^ T6[(t1>>>16)&0xFF] ^
508
T7[(t0>>> 8)&0xFF] ^ T8[(t3 )&0xFF] ^ K[keyOffset++];
509
t3 = T5[(t3>>>24) ] ^ T6[(t2>>>16)&0xFF] ^
510
T7[(t1>>> 8)&0xFF] ^ T8[(t0 )&0xFF] ^ K[keyOffset++];
511
t0 = T5[(a0>>>24) ] ^ T6[(t3>>>16)&0xFF] ^
512
T7[(a2>>> 8)&0xFF] ^ T8[(a1 )&0xFF] ^ K[keyOffset++];
513
t1 = T5[(a1>>>24) ] ^ T6[(a0>>>16)&0xFF] ^
514
T7[(t3>>> 8)&0xFF] ^ T8[(a2 )&0xFF] ^ K[keyOffset++];
515
t2 = T5[(a2>>>24) ] ^ T6[(a1>>>16)&0xFF] ^
516
T7[(a0>>> 8)&0xFF] ^ T8[(t3 )&0xFF] ^ K[keyOffset++];
517
t3 = T5[(t3>>>24) ] ^ T6[(a2>>>16)&0xFF] ^
518
T7[(a1>>> 8)&0xFF] ^ T8[(a0 )&0xFF] ^ K[keyOffset++];
519
a0 = T5[(t0>>>24) ] ^ T6[(t3>>>16)&0xFF] ^
520
T7[(t2>>> 8)&0xFF] ^ T8[(t1 )&0xFF] ^ K[keyOffset++];
521
a1 = T5[(t1>>>24) ] ^ T6[(t0>>>16)&0xFF] ^
522
T7[(t3>>> 8)&0xFF] ^ T8[(t2 )&0xFF] ^ K[keyOffset++];
523
a2 = T5[(t2>>>24) ] ^ T6[(t1>>>16)&0xFF] ^
524
T7[(t0>>> 8)&0xFF] ^ T8[(t3 )&0xFF] ^ K[keyOffset++];
525
t3 = T5[(t3>>>24) ] ^ T6[(t2>>>16)&0xFF] ^
526
T7[(t1>>> 8)&0xFF] ^ T8[(t0 )&0xFF] ^ K[keyOffset++];
527
t0 = T5[(a0>>>24) ] ^ T6[(t3>>>16)&0xFF] ^
528
T7[(a2>>> 8)&0xFF] ^ T8[(a1 )&0xFF] ^ K[keyOffset++];
529
t1 = T5[(a1>>>24) ] ^ T6[(a0>>>16)&0xFF] ^
530
T7[(t3>>> 8)&0xFF] ^ T8[(a2 )&0xFF] ^ K[keyOffset++];
531
t2 = T5[(a2>>>24) ] ^ T6[(a1>>>16)&0xFF] ^
532
T7[(a0>>> 8)&0xFF] ^ T8[(t3 )&0xFF] ^ K[keyOffset++];
533
t3 = T5[(t3>>>24) ] ^ T6[(a2>>>16)&0xFF] ^
534
T7[(a1>>> 8)&0xFF] ^ T8[(a0 )&0xFF] ^ K[keyOffset++];
535
a0 = T5[(t0>>>24) ] ^ T6[(t3>>>16)&0xFF] ^
536
T7[(t2>>> 8)&0xFF] ^ T8[(t1 )&0xFF] ^ K[keyOffset++];
537
a1 = T5[(t1>>>24) ] ^ T6[(t0>>>16)&0xFF] ^
538
T7[(t3>>> 8)&0xFF] ^ T8[(t2 )&0xFF] ^ K[keyOffset++];
539
a2 = T5[(t2>>>24) ] ^ T6[(t1>>>16)&0xFF] ^
540
T7[(t0>>> 8)&0xFF] ^ T8[(t3 )&0xFF] ^ K[keyOffset++];
541
t3 = T5[(t3>>>24) ] ^ T6[(t2>>>16)&0xFF] ^
542
T7[(t1>>> 8)&0xFF] ^ T8[(t0 )&0xFF] ^ K[keyOffset++];
543
t0 = T5[(a0>>>24) ] ^ T6[(t3>>>16)&0xFF] ^
544
T7[(a2>>> 8)&0xFF] ^ T8[(a1 )&0xFF] ^ K[keyOffset++];
545
t1 = T5[(a1>>>24) ] ^ T6[(a0>>>16)&0xFF] ^
546
T7[(t3>>> 8)&0xFF] ^ T8[(a2 )&0xFF] ^ K[keyOffset++];
547
t2 = T5[(a2>>>24) ] ^ T6[(a1>>>16)&0xFF] ^
548
T7[(a0>>> 8)&0xFF] ^ T8[(t3 )&0xFF] ^ K[keyOffset++];
549
t3 = T5[(t3>>>24) ] ^ T6[(a2>>>16)&0xFF] ^
550
T7[(a1>>> 8)&0xFF] ^ T8[(a0 )&0xFF] ^ K[keyOffset++];
551
a0 = T5[(t0>>>24) ] ^ T6[(t3>>>16)&0xFF] ^
552
T7[(t2>>> 8)&0xFF] ^ T8[(t1 )&0xFF] ^ K[keyOffset++];
553
a1 = T5[(t1>>>24) ] ^ T6[(t0>>>16)&0xFF] ^
554
T7[(t3>>> 8)&0xFF] ^ T8[(t2 )&0xFF] ^ K[keyOffset++];
555
a2 = T5[(t2>>>24) ] ^ T6[(t1>>>16)&0xFF] ^
556
T7[(t0>>> 8)&0xFF] ^ T8[(t3 )&0xFF] ^ K[keyOffset++];
557
t3 = T5[(t3>>>24) ] ^ T6[(t2>>>16)&0xFF] ^
558
T7[(t1>>> 8)&0xFF] ^ T8[(t0 )&0xFF] ^ K[keyOffset++];
559
t0 = T5[(a0>>>24) ] ^ T6[(t3>>>16)&0xFF] ^
560
T7[(a2>>> 8)&0xFF] ^ T8[(a1 )&0xFF] ^ K[keyOffset++];
561
t1 = T5[(a1>>>24) ] ^ T6[(a0>>>16)&0xFF] ^
562
T7[(t3>>> 8)&0xFF] ^ T8[(a2 )&0xFF] ^ K[keyOffset++];
563
t2 = T5[(a2>>>24) ] ^ T6[(a1>>>16)&0xFF] ^
564
T7[(a0>>> 8)&0xFF] ^ T8[(t3 )&0xFF] ^ K[keyOffset++];
565
t3 = T5[(t3>>>24) ] ^ T6[(a2>>>16)&0xFF] ^
566
T7[(a1>>> 8)&0xFF] ^ T8[(a0 )&0xFF] ^ K[keyOffset++];
567
a0 = T5[(t0>>>24) ] ^ T6[(t3>>>16)&0xFF] ^
568
T7[(t2>>> 8)&0xFF] ^ T8[(t1 )&0xFF] ^ K[keyOffset++];
569
a1 = T5[(t1>>>24) ] ^ T6[(t0>>>16)&0xFF] ^
570
T7[(t3>>> 8)&0xFF] ^ T8[(t2 )&0xFF] ^ K[keyOffset++];
571
a2 = T5[(t2>>>24) ] ^ T6[(t1>>>16)&0xFF] ^
572
T7[(t0>>> 8)&0xFF] ^ T8[(t3 )&0xFF] ^ K[keyOffset++];
573
t3 = T5[(t3>>>24) ] ^ T6[(t2>>>16)&0xFF] ^
574
T7[(t1>>> 8)&0xFF] ^ T8[(t0 )&0xFF] ^ K[keyOffset++];
575
576
t1 = K[0];
577
out[outOffset++] = (byte)(Si[(a0 >>> 24) ] ^ (t1 >>> 24));
578
out[outOffset++] = (byte)(Si[(t3 >>> 16) & 0xFF] ^ (t1 >>> 16));
579
out[outOffset++] = (byte)(Si[(a2 >>> 8) & 0xFF] ^ (t1 >>> 8));
580
out[outOffset++] = (byte)(Si[(a1 ) & 0xFF] ^ (t1 ));
581
t1 = K[1];
582
out[outOffset++] = (byte)(Si[(a1 >>> 24) ] ^ (t1 >>> 24));
583
out[outOffset++] = (byte)(Si[(a0 >>> 16) & 0xFF] ^ (t1 >>> 16));
584
out[outOffset++] = (byte)(Si[(t3 >>> 8) & 0xFF] ^ (t1 >>> 8));
585
out[outOffset++] = (byte)(Si[(a2 ) & 0xFF] ^ (t1 ));
586
t1 = K[2];
587
out[outOffset++] = (byte)(Si[(a2 >>> 24) ] ^ (t1 >>> 24));
588
out[outOffset++] = (byte)(Si[(a1 >>> 16) & 0xFF] ^ (t1 >>> 16));
589
out[outOffset++] = (byte)(Si[(a0 >>> 8) & 0xFF] ^ (t1 >>> 8));
590
out[outOffset++] = (byte)(Si[(t3 ) & 0xFF] ^ (t1 ));
591
t1 = K[3];
592
out[outOffset++] = (byte)(Si[(t3 >>> 24) ] ^ (t1 >>> 24));
593
out[outOffset++] = (byte)(Si[(a2 >>> 16) & 0xFF] ^ (t1 >>> 16));
594
out[outOffset++] = (byte)(Si[(a1 >>> 8) & 0xFF] ^ (t1 >>> 8));
595
out[outOffset ] = (byte)(Si[(a0 ) & 0xFF] ^ (t1 ));
596
}
597
598
/**
599
* Expand a user-supplied key material into a session key.
600
*
601
* @param k The 128/192/256-bit cipher key to use.
602
* @exception InvalidKeyException If the key is invalid.
603
*/
604
private void makeSessionKey(byte[] k) throws InvalidKeyException {
605
if (k == null) {
606
throw new InvalidKeyException("Empty key");
607
}
608
if (!isKeySizeValid(k.length)) {
609
throw new InvalidKeyException("Invalid AES key length: " +
610
k.length + " bytes");
611
}
612
int ROUNDS = getRounds(k.length);
613
int ROUND_KEY_COUNT = (ROUNDS + 1) * 4;
614
615
int BC = 4;
616
int[][] Ke = new int[ROUNDS + 1][4]; // encryption round keys
617
int[][] Kd = new int[ROUNDS + 1][4]; // decryption round keys
618
619
int KC = k.length/4; // keylen in 32-bit elements
620
621
int[] tk = new int[KC];
622
int i, j;
623
624
// copy user material bytes into temporary ints
625
for (i = 0, j = 0; i < KC; i++, j+=4) {
626
tk[i] = (k[j] ) << 24 |
627
(k[j+1] & 0xFF) << 16 |
628
(k[j+2] & 0xFF) << 8 |
629
(k[j+3] & 0xFF);
630
}
631
632
// copy values into round key arrays
633
int t = 0;
634
for (j = 0; (j < KC) && (t < ROUND_KEY_COUNT); j++, t++) {
635
Ke[t / 4][t % 4] = tk[j];
636
Kd[ROUNDS - (t / 4)][t % 4] = tk[j];
637
}
638
int tt, rconpointer = 0;
639
while (t < ROUND_KEY_COUNT) {
640
// extrapolate using phi (the round key evolution function)
641
tt = tk[KC - 1];
642
tk[0] ^= (S[(tt >>> 16) & 0xFF] ) << 24 ^
643
(S[(tt >>> 8) & 0xFF] & 0xFF) << 16 ^
644
(S[(tt ) & 0xFF] & 0xFF) << 8 ^
645
(S[(tt >>> 24) ] & 0xFF) ^
646
(rcon[rconpointer++] ) << 24;
647
if (KC != 8)
648
for (i = 1, j = 0; i < KC; i++, j++) tk[i] ^= tk[j];
649
else {
650
for (i = 1, j = 0; i < KC / 2; i++, j++) tk[i] ^= tk[j];
651
tt = tk[KC / 2 - 1];
652
tk[KC / 2] ^= (S[(tt ) & 0xFF] & 0xFF) ^
653
(S[(tt >>> 8) & 0xFF] & 0xFF) << 8 ^
654
(S[(tt >>> 16) & 0xFF] & 0xFF) << 16 ^
655
(S[(tt >>> 24) ] ) << 24;
656
for (j = KC / 2, i = j + 1; i < KC; i++, j++) tk[i] ^= tk[j];
657
}
658
// copy values into round key arrays
659
for (j = 0; (j < KC) && (t < ROUND_KEY_COUNT); j++, t++) {
660
Ke[t / 4][t % 4] = tk[j];
661
Kd[ROUNDS - (t / 4)][t % 4] = tk[j];
662
}
663
}
664
for (int r = 1; r < ROUNDS; r++) {
665
// inverse MixColumn where needed
666
for (j = 0; j < BC; j++) {
667
tt = Kd[r][j];
668
Kd[r][j] = U1[(tt >>> 24) & 0xFF] ^
669
U2[(tt >>> 16) & 0xFF] ^
670
U3[(tt >>> 8) & 0xFF] ^
671
U4[ tt & 0xFF];
672
}
673
}
674
675
// assemble the encryption (Ke) and decryption (Kd) round keys
676
// and expand them into arrays of ints.
677
int[] expandedKe = expandToSubKey(Ke, false); // decrypting==false
678
int[] expandedKd = expandToSubKey(Kd, true); // decrypting==true
679
Arrays.fill(tk, 0);
680
for (int[] ia: Ke) {
681
Arrays.fill(ia, 0);
682
}
683
for (int[] ia: Kd) {
684
Arrays.fill(ia, 0);
685
}
686
ROUNDS_12 = (ROUNDS>=12);
687
ROUNDS_14 = (ROUNDS==14);
688
limit = ROUNDS*4;
689
690
// store the expanded sub keys into 'sessionK'
691
if (sessionK != null) {
692
// erase the previous values in sessionK
693
Arrays.fill(sessionK[0], 0);
694
Arrays.fill(sessionK[1], 0);
695
}
696
sessionK = new int[][] { expandedKe, expandedKd };
697
}
698
699
700
/**
701
* Return The number of rounds for a given Rijndael keysize.
702
*
703
* @param keySize The size of the user key material in bytes.
704
* MUST be one of (16, 24, 32).
705
* @return The number of rounds.
706
*/
707
private static int getRounds(int keySize) {
708
return (keySize >> 2) + 6;
709
}
710
}
711
712