Skip to content

Commit 03f8b9e

Browse files
Ensure AES related tests are using a deterministic provider (#172)
The AES base test cases were reviewed to ensure that the tests are testing a desired provider. Currently there are a few tests missing the provider argument during algorithm instantiation. In this case algorithms tested may or may not match the expected provider used in the constructor of a test depending upon what happens to be first in the JCE provider list at the time of execution. Signed-off-by: Jason Katonica <katonica@us.ibm.com>
1 parent c1a04fa commit 03f8b9e

File tree

4 files changed

+14
-15
lines changed

4 files changed

+14
-15
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public BaseTestAESGCMBufferIV(String providerName) {
2626

2727
protected void setUp() throws Exception {
2828
keySpec = new SecretKeySpec(new byte[16], "AES");
29-
cipher = Cipher.getInstance("AES/GCM/NoPadding");
29+
cipher = Cipher.getInstance("AES/GCM/NoPadding", providerName);
3030
plaintext = new byte[51];
3131
}
3232

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -358,18 +358,18 @@ private void do_cbc_readAllIllegalBlockSize() throws Exception {
358358
}
359359

360360
/* Generic method to create encrypted text */
361-
static byte[] encryptedText(String mode, int length) throws Exception {
361+
byte[] encryptedText(String mode, int length) throws Exception {
362362
return encryptedText(mode, new byte[length]);
363363
}
364364

365365
/* Generic method to create encrypted text */
366-
static byte[] encryptedText(String mode, byte[] pt) throws Exception {
366+
byte[] encryptedText(String mode, byte[] pt) throws Exception {
367367
Cipher c;
368368
if (mode.compareTo("GCM") == 0) {
369-
c = Cipher.getInstance("AES/GCM/NoPadding", "OpenJCEPlus");
369+
c = Cipher.getInstance("AES/GCM/NoPadding", providerName);
370370
c.init(Cipher.ENCRYPT_MODE, key, gcmspec);
371371
} else if (mode.compareTo("CBC") == 0) {
372-
c = Cipher.getInstance("AES/CBC/NoPadding", "OpenJCEPlus");
372+
c = Cipher.getInstance("AES/CBC/NoPadding", providerName);
373373
c.init(Cipher.ENCRYPT_MODE, key, iv);
374374
} else {
375375
return null;
@@ -379,19 +379,19 @@ static byte[] encryptedText(String mode, byte[] pt) throws Exception {
379379
}
380380

381381
/* Generic method to get a properly setup CipherInputStream */
382-
static CipherInputStream getStream(String mode, byte[] ct) throws Exception {
382+
CipherInputStream getStream(String mode, byte[] ct) throws Exception {
383383
return getStream(mode, ct, ct.length);
384384
}
385385

386386
/* Generic method to get a properly setup CipherInputStream */
387-
static CipherInputStream getStream(String mode, byte[] ct, int length) throws Exception {
387+
CipherInputStream getStream(String mode, byte[] ct, int length) throws Exception {
388388
Cipher c;
389389

390390
if (mode.compareTo("GCM") == 0) {
391-
c = Cipher.getInstance("AES/GCM/NoPadding", "OpenJCEPlus");
391+
c = Cipher.getInstance("AES/GCM/NoPadding", providerName);
392392
c.init(Cipher.DECRYPT_MODE, key, gcmspec);
393393
} else if (mode.compareTo("CBC") == 0) {
394-
c = Cipher.getInstance("AES/CBC/NoPadding", "OpenJCEPlus");
394+
c = Cipher.getInstance("AES/CBC/NoPadding", providerName);
395395
c.init(Cipher.DECRYPT_MODE, key, iv);
396396
} else {
397397
return null;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
public class BaseTestAESGCMNonExpanding extends BaseTest {
3232

3333
private static final String ALGORITHM = "AES";
34-
private static final String PROVIDER = "OpenJCEPlus"; //"SunJCE";
3534
private static final String[] MODES = {"GCM"};
3635
private static final String PADDING = "NoPadding";
3736
protected int specifiedKeySize = 128;
@@ -64,9 +63,9 @@ public void doTest(String algo, String mo, String pad) throws Exception {
6463
byte[] plainText = new byte[128];
6564
rdm.nextBytes(plainText);
6665

67-
ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
66+
ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, providerName);
6867

69-
KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
68+
KeyGenerator kg = KeyGenerator.getInstance(algo, providerName);
7069
kg.init(specifiedKeySize);
7170
key = kg.generateKey();
7271

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@ public void testCallUpdateFailsSameKeyIV() throws Exception {
12041204

12051205
public void testMultipleUpdateWithoutAllocatingExternalBuffer19() throws Exception {
12061206

1207-
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
1207+
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES", providerName);
12081208
keyGenerator.init(16 * 8);
12091209

12101210
// Generate Key
@@ -1239,7 +1239,7 @@ public byte[] doMultipleUpdateWithoutAllocatingExternalBufferEncrypt(SecretKey k
12391239
// Get Cipher Instance
12401240
Cipher cipher = null;
12411241
try {
1242-
cipher = Cipher.getInstance("AES/GCM/NoPadding");
1242+
cipher = Cipher.getInstance("AES/GCM/NoPadding", providerName);
12431243

12441244
// Create SecretKeySpec
12451245
SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");
@@ -1287,7 +1287,7 @@ public byte[] doMultipleUpdateWithoutAllocatingExternalBufferEncrypt(SecretKey k
12871287
public byte[] doMultipleUpdateWithoutAllocatingExternalBufferDecrypt(byte[] cipherText,
12881288
SecretKey key, byte[] IV) throws Exception {
12891289
// Get Cipher Instance
1290-
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
1290+
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", providerName);
12911291

12921292
// Create SecretKeySpec
12931293
SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");

0 commit comments

Comments
 (0)