Skip to content

Commit 2b98128

Browse files
committed
Remove K from AES_Crypt
1 parent 621616a commit 2b98128

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

src/hotspot/share/opto/library_call.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7173,6 +7173,7 @@ Node * LibraryCallKit::field_address_from_object(Node * fromObj, const char * fi
71737173
bool LibraryCallKit::inline_aescrypt_Block(vmIntrinsics::ID id) {
71747174
address stubAddr = nullptr;
71757175
const char *stubName;
7176+
bool is_decrypt = false;
71767177
assert(UseAES, "need AES instruction support");
71777178

71787179
switch(id) {
@@ -7183,6 +7184,7 @@ bool LibraryCallKit::inline_aescrypt_Block(vmIntrinsics::ID id) {
71837184
case vmIntrinsics::_aescrypt_decryptBlock:
71847185
stubAddr = StubRoutines::aescrypt_decryptBlock();
71857186
stubName = "aescrypt_decryptBlock";
7187+
is_decrypt = true;
71867188
break;
71877189
default:
71887190
break;
@@ -7216,7 +7218,7 @@ bool LibraryCallKit::inline_aescrypt_Block(vmIntrinsics::ID id) {
72167218

72177219
// now need to get the start of its expanded key array
72187220
// this requires a newer class file that has this array as littleEndian ints, otherwise we revert to java
7219-
Node* k_start = get_key_start_from_aescrypt_object(aescrypt_object);
7221+
Node* k_start = get_key_start_from_aescrypt_object(aescrypt_object, is_decrypt);
72207222
if (k_start == nullptr) return false;
72217223

72227224
// Call the stub.
@@ -7231,7 +7233,7 @@ bool LibraryCallKit::inline_aescrypt_Block(vmIntrinsics::ID id) {
72317233
bool LibraryCallKit::inline_cipherBlockChaining_AESCrypt(vmIntrinsics::ID id) {
72327234
address stubAddr = nullptr;
72337235
const char *stubName = nullptr;
7234-
7236+
bool is_decrypt= false;
72357237
assert(UseAES, "need AES instruction support");
72367238

72377239
switch(id) {
@@ -7242,6 +7244,7 @@ bool LibraryCallKit::inline_cipherBlockChaining_AESCrypt(vmIntrinsics::ID id) {
72427244
case vmIntrinsics::_cipherBlockChaining_decryptAESCrypt:
72437245
stubAddr = StubRoutines::cipherBlockChaining_decryptAESCrypt();
72447246
stubName = "cipherBlockChaining_decryptAESCrypt";
7247+
is_decrypt = true;
72457248
break;
72467249
default:
72477250
break;
@@ -7295,7 +7298,7 @@ bool LibraryCallKit::inline_cipherBlockChaining_AESCrypt(vmIntrinsics::ID id) {
72957298
aescrypt_object = _gvn.transform(aescrypt_object);
72967299

72977300
// we need to get the start of the aescrypt_object's expanded key array
7298-
Node* k_start = get_key_start_from_aescrypt_object(aescrypt_object);
7301+
Node* k_start = get_key_start_from_aescrypt_object(aescrypt_object, is_decrypt);
72997302
if (k_start == nullptr) return false;
73007303

73017304
// similarly, get the start address of the r vector
@@ -7319,7 +7322,7 @@ bool LibraryCallKit::inline_cipherBlockChaining_AESCrypt(vmIntrinsics::ID id) {
73197322
bool LibraryCallKit::inline_electronicCodeBook_AESCrypt(vmIntrinsics::ID id) {
73207323
address stubAddr = nullptr;
73217324
const char *stubName = nullptr;
7322-
7325+
bool is_decrypt = false;
73237326
assert(UseAES, "need AES instruction support");
73247327

73257328
switch (id) {
@@ -7330,6 +7333,7 @@ bool LibraryCallKit::inline_electronicCodeBook_AESCrypt(vmIntrinsics::ID id) {
73307333
case vmIntrinsics::_electronicCodeBook_decryptAESCrypt:
73317334
stubAddr = StubRoutines::electronicCodeBook_decryptAESCrypt();
73327335
stubName = "electronicCodeBook_decryptAESCrypt";
7336+
is_decrypt = true;
73337337
break;
73347338
default:
73357339
break;
@@ -7381,7 +7385,7 @@ bool LibraryCallKit::inline_electronicCodeBook_AESCrypt(vmIntrinsics::ID id) {
73817385
aescrypt_object = _gvn.transform(aescrypt_object);
73827386

73837387
// we need to get the start of the aescrypt_object's expanded key array
7384-
Node* k_start = get_key_start_from_aescrypt_object(aescrypt_object);
7388+
Node* k_start = get_key_start_from_aescrypt_object(aescrypt_object, is_decrypt);
73857389
if (k_start == nullptr) return false;
73867390

73877391
// Call the stub, passing src_start, dest_start, k_start, r_start and src_len
@@ -7474,17 +7478,17 @@ bool LibraryCallKit::inline_counterMode_AESCrypt(vmIntrinsics::ID id) {
74747478
}
74757479

74767480
//------------------------------get_key_start_from_aescrypt_object-----------------------
7477-
Node* LibraryCallKit::get_key_start_from_aescrypt_object(Node* aescrypt_object) {
7481+
Node* LibraryCallKit::get_key_start_from_aescrypt_object(Node* aescrypt_object, bool is_decrypt) {
74787482
// MixColumns for decryption can be reduced by preprocessing MixColumns with round keys.
74797483
// Intel's extension is based on this optimization and AESCrypt generates round keys by preprocessing MixColumns.
74807484
// However, ppc64 vncipher processes MixColumns and requires the same round keys with encryption.
74817485
// The following platform specific stubs of encryption and decryption use the same round keys.
74827486
#if defined(PPC64) || defined(S390) || defined(RISCV64)
7483-
const char* key_name = "sessionKe";
7487+
bool use_decryption_key = false;
74847488
#else
7485-
const char* key_name = "K";
7489+
bool use_decryption_key = is_decrypt;
74867490
#endif
7487-
Node* objAESCryptKey = load_field_from_object(aescrypt_object, key_name, "[I");
7491+
Node* objAESCryptKey = load_field_from_object(aescrypt_object, use_decryption_key ? "sessionKd" : "sessionKe", "[I");
74887492
assert(objAESCryptKey != nullptr, "wrong version of com.sun.crypto.provider.AES_Crypt");
74897493
if (objAESCryptKey == nullptr) return (Node *) nullptr;
74907494

src/hotspot/share/opto/library_call.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ class LibraryCallKit : public GraphKit {
338338
Node* inline_cipherBlockChaining_AESCrypt_predicate(bool decrypting);
339339
Node* inline_electronicCodeBook_AESCrypt_predicate(bool decrypting);
340340
Node* inline_counterMode_AESCrypt_predicate();
341-
Node* get_key_start_from_aescrypt_object(Node* aescrypt_object);
341+
Node* get_key_start_from_aescrypt_object(Node* aescrypt_object, bool is_decrypt = false);
342342
bool inline_ghash_processBlocks();
343343
bool inline_chacha20Block();
344344
bool inline_kyberNtt();

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,11 @@ final class AES_Crypt extends SymmetricCipher {
5454
private int rounds;
5555
private byte[] prevKey = null;
5656

57-
// Following attributes (sessionKe and K) are specific to Intrinsics, where
58-
// sessionKe is the unprocessed key that is used for PPC64, S390 and
59-
// RISCV64 architectures, whereas K is used for everything else.
57+
// Following attributes are specific to Intrinsics, where sessionKe is the
58+
// unprocessed key that is also used for decryption on PPC64, S390 and
59+
// RISCV64 architectures. Other ones use sessionKd for decryption.
6060
private int[] sessionKe = null; // key for encryption
6161
private int[] sessionKd = null; // preprocessed key for decryption
62-
private int[] K = null; // preprocessed key in case of decryption
6362

6463
// Round constant
6564
private static final int[] RCON = {
@@ -940,9 +939,6 @@ void init(boolean decrypting, String algorithm, byte[] key)
940939
if (sessionKd == null) {
941940
sessionKd = genInvRoundKeys(sessionKe, rounds);
942941
}
943-
K = sessionKd;
944-
} else {
945-
K = sessionKe;
946942
}
947943
}
948944

@@ -1044,6 +1040,7 @@ private static int subWord(int word) {
10441040
*/
10451041
@IntrinsicCandidate
10461042
private void implEncryptBlock(byte[] p, int po, byte[] c, int co) {
1043+
int[] K = sessionKe;
10471044
int ti0, ti1, ti2, ti3;
10481045
int a0, a1, a2, a3;
10491046
int w = K.length - WB;
@@ -1222,6 +1219,7 @@ private void implEncryptBlock(byte[] p, int po, byte[] c, int co) {
12221219
*/
12231220
@IntrinsicCandidate
12241221
private void implDecryptBlock(byte[] c, int co, byte[] p, int po) {
1222+
int[] K = sessionKd;
12251223
int ti0, ti1, ti2, ti3;
12261224
int a0, a1, a2, a3;
12271225

0 commit comments

Comments
 (0)