Skip to content

Commit 436b335

Browse files
smemeryJamil Nimeh
authored andcommitted
8371450: AES performance improvements for key schedule generation
Reviewed-by: valeriep, jnimeh
1 parent 279f39f commit 436b335

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

src/java.base/share/classes/com/sun/crypto/provider/AES_Crypt.java

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -941,8 +941,9 @@ void init(boolean decrypting, String algorithm, byte[] key)
941941
* Generate the cipher's round keys as outlined in section 5.2 of the spec.
942942
*
943943
* @param key [in] the symmetric key byte array.
944+
* @param rounds [in] the number of rounds for generating the round keys.
944945
*
945-
* @return w the cipher round keys.
946+
* @return the cipher round keys.
946947
*/
947948
private static int[] genRoundKeys(byte[] key, int rounds) {
948949
int wLen = WB * (rounds + 1);
@@ -970,53 +971,58 @@ private static int[] genRoundKeys(byte[] key, int rounds) {
970971
/**
971972
* Generate the inverse cipher round keys.
972973
*
973-
* @return w1 the inverse cipher round keys.
974+
* @param w [in] the targeted word for substitution.
975+
* @param rounds [in] the number of rounds for generating the round keys.
976+
*
977+
* @return the inverse cipher round keys.
974978
*/
975979
private static int[] genInvRoundKeys(int[] w, int rounds) {
976-
int kLen = w.length;;
977-
int[] temp = new int[WB];
978-
int[] dw = new int[kLen];
980+
int[] dw = new int[w.length];
979981

980982
// Intrinsics requires the inverse key expansion to be reverse order
981983
// except for the first and last round key as the first two round keys
982984
// are without a mix column transform.
983985
for (int i = 1; i < rounds; i++) {
984-
System.arraycopy(w, i * WB, temp, 0, WB);
985-
temp[0] = TMI0[temp[0] >>> 24] ^ TMI1[(temp[0] >> 16) & 0xFF]
986-
^ TMI2[(temp[0] >> 8) & 0xFF] ^ TMI3[temp[0] & 0xFF];
987-
temp[1] = TMI0[temp[1] >>> 24] ^ TMI1[(temp[1] >> 16) & 0xFF]
988-
^ TMI2[(temp[1] >> 8) & 0xFF] ^ TMI3[temp[1] & 0xFF];
989-
temp[2] = TMI0[temp[2] >>> 24] ^ TMI1[(temp[2] >> 16) & 0xFF]
990-
^ TMI2[(temp[2] >> 8) & 0xFF] ^ TMI3[temp[2] & 0xFF];
991-
temp[3] = TMI0[temp[3] >>> 24] ^ TMI1[(temp[3] >> 16) & 0xFF]
992-
^ TMI2[(temp[3] >> 8) & 0xFF] ^ TMI3[temp[3] & 0xFF];
993-
System.arraycopy(temp, 0, dw, kLen - (i * WB), WB);
986+
int widx = i * WB;
987+
int idx = w.length - widx;
988+
989+
dw[idx] = TMI0[w[widx] >>> 24] ^ TMI1[(w[widx] >> 16) & 0xFF]
990+
^ TMI2[(w[widx] >> 8) & 0xFF] ^ TMI3[w[widx] & 0xFF];
991+
dw[idx + 1] = TMI0[w[widx + 1] >>> 24]
992+
^ TMI1[(w[widx + 1] >> 16) & 0xFF]
993+
^ TMI2[(w[widx + 1] >> 8) & 0xFF]
994+
^ TMI3[w[widx + 1] & 0xFF];
995+
dw[idx + 2] = TMI0[w[widx + 2] >>> 24]
996+
^ TMI1[(w[widx + 2] >> 16) & 0xFF]
997+
^ TMI2[(w[widx + 2] >> 8) & 0xFF]
998+
^ TMI3[w[widx + 2] & 0xFF];
999+
dw[idx + 3] = TMI0[w[widx + 3] >>> 24]
1000+
^ TMI1[(w[widx + 3] >> 16) & 0xFF]
1001+
^ TMI2[(w[widx + 3] >> 8) & 0xFF]
1002+
^ TMI3[w[widx + 3] & 0xFF];
9941003
}
995-
System.arraycopy(w, kLen - WB, dw, WB, WB);
1004+
System.arraycopy(w, w.length - WB, dw, WB, WB);
9961005
System.arraycopy(w, 0, dw, 0, WB);
997-
Arrays.fill(temp, 0);
9981006

9991007
return dw;
10001008
}
10011009

10021010
/**
1003-
* Subtitute the word as a step of key expansion.
1011+
* Substitute the word as a step of key expansion.
10041012
*
1005-
* @param state [in] the targeted word for substituion.
1006-
* @param sub [in] the substitute table for cipher and inverse cipher.
1013+
* @param word [in] the targeted word for substitution.
10071014
*
10081015
* @return the substituted word.
10091016
*/
10101017
private static int subWord(int word) {
1011-
byte b0 = (byte) (word >>> 24);
1012-
byte b1 = (byte) ((word >> 16) & 0xFF);
1013-
byte b2 = (byte) ((word >> 8) & 0xFF);
1014-
byte b3 = (byte) (word & 0xFF);
1018+
byte b0 = (byte) (word >> 24);
1019+
byte b1 = (byte) (word >> 16);
1020+
byte b2 = (byte) (word >> 8);
10151021

10161022
return ((SBOX[(b0 & 0xF0) >> 4][b0 & 0x0F] & 0xFF) << 24)
10171023
| ((SBOX[(b1 & 0xF0) >> 4][b1 & 0x0F] & 0xFF) << 16)
10181024
| ((SBOX[(b2 & 0xF0) >> 4][b2 & 0x0F] & 0xFF) << 8)
1019-
| (SBOX[(b3 & 0xF0) >> 4][b3 & 0x0F] & 0xFF);
1025+
| (SBOX[(word & 0xF0) >> 4][word & 0x0F] & 0xFF);
10201026
}
10211027

10221028
/**

0 commit comments

Comments
 (0)