Skip to content

Commit 3f672c8

Browse files
authored
Enhancements to AES-GCM Tests and Cipher Initialization (#497)
This update addresses multiple issues related to the AES-GCM cipher. AES context creation is now validated to prevent returning a NULL context. Using a NULL context in subsequent API calls was previously causing crashes. The ICC_AES_GCM_CTX_ctrl API has been removed, as it was deprecated and no longer needed. AES-GCM test cases were updated to eliminate unintended use of static variables in multi-threaded scenarios. This issue could lead to unpredictable behavior due to concurrent reads and writes to shared fields. BaseTestAESGCMUpdate now uses StringBuilder instead of string concatenation, reducing memory pressure and improving efficiency. Signed-off-by: Tao Liu <tao.liu@ibm.com>
1 parent 14d04ad commit 3f672c8

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

src/main/native/GCM.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright IBM Corp. 2023, 2024
2+
* Copyright IBM Corp. 2023, 2025
33
*
44
* This code is free software; you can redistribute it and/or modify it
55
* under the terms provided by IBM in the LICENSE file that accompanied
@@ -1680,26 +1680,22 @@ Java_com_ibm_crypto_plus_provider_ock_NativeInterface_create_1GCM_1context(
16801680
static const char* functionName = "NativeInterface.create_GCM_context";
16811681
ICC_CTX* ockCtx = (ICC_CTX*)((intptr_t)ockContextId);
16821682
ICC_AES_GCM_CTX* gcmCtx = NULL;
1683-
int rc = 0;
16841683

16851684
if (debug) {
16861685
gslogFunctionEntry(functionName);
16871686
}
1688-
if (debug) {
1689-
gslogFunctionExit(functionName);
1690-
}
16911687
gcmCtx = ICC_AES_GCM_CTX_new(ockCtx);
1692-
rc = ICC_AES_GCM_CTX_ctrl(ockCtx, gcmCtx, ICC_AES_GCM_CTRL_TLS13, 0, NULL);
1693-
if (rc != ICC_OSSL_SUCCESS) {
1688+
if (gcmCtx == NULL) {
16941689
#ifdef DEBUG_GCM_DETAIL
16951690
if (debug) {
1696-
gslogMessage("ICC_AES_GCM_CTX_ctrl failed rc = %d\n", rc);
1691+
gslogMessage("ICC_AES_GCM_CTX_new failed to create a new context.");
16971692
}
16981693
#endif
1699-
if (gcmCtx != NULL) {
1700-
ICC_AES_GCM_CTX_free(ockCtx, gcmCtx);
1701-
}
1702-
gcmCtx = NULL;
1694+
throwOCKException(env, 0,
1695+
"ICC_AES_GCM_CTX_new failed to create a new context.");
1696+
}
1697+
if (debug) {
1698+
gslogFunctionExit(functionName);
17031699
}
17041700
return (jlong)gcmCtx;
17051701
}

src/test/java/ibm/jceplus/junit/base/BaseTestAESGCMUpdate.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright IBM Corp. 2023, 2024
2+
* Copyright IBM Corp. 2023, 2025
33
*
44
* This code is free software; you can redistribute it and/or modify it
55
* under the terms provided by IBM in the LICENSE file that accompanied
@@ -17,6 +17,7 @@
1717
import java.io.IOException;
1818
import java.lang.reflect.Method;
1919
import java.nio.ByteBuffer;
20+
import java.nio.charset.StandardCharsets;
2021
import java.nio.file.Files;
2122
import java.nio.file.Paths;
2223
import java.security.AlgorithmParameters;
@@ -82,7 +83,7 @@ public void setUp() throws Exception {
8283
key = aesKeyGen.generateKey();
8384
}
8485

85-
static String[] plainTextStrArray = {"a", "ab", "abc", "abcd", "abcde", "abcdef", "abcdefg",
86+
String[] plainTextStrArray = {"a", "ab", "abc", "abcd", "abcde", "abcdef", "abcdefg",
8687
"abcdefgh", "abcdefghi", "abcdefghi", "abcdefghij", "abcdefghijk", "abcdefghijkl",
8788
"abcdefghijklm", "abcdefghijklmn", "abcdefghijklmno", "abcdefghijklmnop",
8889
"abcdefghijklmnopq", "abcdefghijklmnopqr", "abcdefghijklmnopqrs",
@@ -93,7 +94,7 @@ public void setUp() throws Exception {
9394
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyza",
9495
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0123456789"};
9596

96-
static String[] plainTextStrArray1 = {
97+
String[] plainTextStrArray1 = {
9798
//"abcdefghijklmnopqrstuvwxyz0123456789012345678901234",
9899
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyza01234",
99100
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa012345678901234"};
@@ -182,15 +183,16 @@ public void testCaseWithLongString2() throws Exception {
182183
byte[] myAAD = "aaaaaaaaa".getBytes();
183184

184185
GCMParameterSpec ivSpec = new GCMParameterSpec(GCM_TAG_LENGTH * Byte.SIZE, iv);
186+
185187
for (int keysizeloop = 1; keysizeloop < 3; keysizeloop++) {
186-
String myStr = "";
187-
for (int i = 0; i < 118999;) {
188-
myStr = myStr + "a";
188+
StringBuilder myStr = new StringBuilder();
189+
SecretKey key16 = new SecretKeySpec(new byte[16 * keysizeloop], "AES"); // key is 16 zero bytes
189190

190-
byte[] plainTextBytes = myStr.getBytes("UTF-8");
191+
for (int i = 0; i < 118999;) {
192+
myStr.append("a");
191193

194+
byte[] plainTextBytes = myStr.toString().getBytes(StandardCharsets.UTF_8);
192195

193-
SecretKey key16 = new SecretKeySpec(new byte[16 * keysizeloop], "AES"); // key is 16 zero bytes
194196
byte[] encryptedText = dotestWithString(Cipher.ENCRYPT_MODE, key16, myAAD,
195197
plainTextBytes, ivSpec);
196198
byte[] decryptedText = dotestWithString(Cipher.DECRYPT_MODE, key16, myAAD,
@@ -767,14 +769,14 @@ public void testWithMultipleDataUpdate9() throws Exception {
767769

768770
GCMParameterSpec ivSpec = new GCMParameterSpec(GCM_TAG_LENGTH * Byte.SIZE, iv);
769771
for (int keysizeloop = 1; keysizeloop < 3; keysizeloop++) {
770-
String myStr = "";
772+
StringBuilder myStr = new StringBuilder();
771773
for (int i = 0; i < 250; i++) {
772-
myStr = myStr + "a";
774+
myStr.append("a");
773775
}
774776
for (int i = 250; i < 118999;) {
775-
myStr = myStr + "a";
777+
myStr.append("a");
776778
int numTimes = 7;
777-
byte[] plainTextBytes = myStr.getBytes("UTF-8");
779+
byte[] plainTextBytes = myStr.toString().getBytes(StandardCharsets.UTF_8);
778780
SecretKey key = new SecretKeySpec(new byte[16 * keysizeloop], "AES"); // key is 16 zero bytes
779781
byte[] encryptedText = doTestWithMultipleDataUpdate(Cipher.ENCRYPT_MODE, key, myAAD,
780782
plainTextBytes, ivSpec, numTimes);

src/test/java/ibm/jceplus/junit/base/BaseTestAESGCMUpdateInteropBC.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright IBM Corp. 2023, 2024
2+
* Copyright IBM Corp. 2023, 2025
33
*
44
* This code is free software; you can redistribute it and/or modify it
55
* under the terms provided by IBM in the LICENSE file that accompanied
@@ -37,7 +37,7 @@ public class BaseTestAESGCMUpdateInteropBC extends BaseTestJunit5Interop {
3737
//protected Method methodGCMParameterSpecSetAAD = null;
3838
protected int specifiedKeySize = 0;
3939

40-
static String[] plainTextStrArray = {"a", "ab", "abc", "abcd", "abcde", "abcdef", "abcdefg",
40+
String[] plainTextStrArray = {"a", "ab", "abc", "abcd", "abcde", "abcdef", "abcdefg",
4141
"abcdefgh", "abcdefghi", "abcdefghi", "abcdefghij", "abcdefghijk", "abcdefghijkl",
4242
"abcdefghijklm", "abcdefghijklmn", "abcdefghijklmno", "abcdefghijklmnop",
4343
"abcdefghijklmnopq", "abcdefghijklmnopqr", "abcdefghijklmnopqrs",
@@ -48,7 +48,7 @@ public class BaseTestAESGCMUpdateInteropBC extends BaseTestJunit5Interop {
4848
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyza",
4949
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0123456789"};
5050

51-
static String[] plainTextStrArray1 = {
51+
String[] plainTextStrArray1 = {
5252
//"abcdefghijklmnopqrstuvwxyz0123456789012345678901234",
5353
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyza01234",
5454
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa012345678901234"};

0 commit comments

Comments
 (0)