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