Skip to content

Commit 27bac22

Browse files
committed
JCE: throw correct InvalidParameterException from Cipher.init() if mode is unsupported or incorrect
1 parent 777a564 commit 27bac22

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/main/java/com/wolfssl/provider/jce/WolfCryptCipher.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.security.Key;
4343
import java.security.NoSuchAlgorithmException;
4444
import java.security.InvalidAlgorithmParameterException;
45+
import java.security.InvalidParameterException;
4546
import java.security.InvalidKeyException;
4647
import java.security.interfaces.RSAPrivateKey;
4748
import java.security.interfaces.RSAPublicKey;
@@ -522,7 +523,7 @@ private void wolfCryptSetDirection(int opmode)
522523
break;
523524

524525
default:
525-
throw new InvalidKeyException(
526+
throw new InvalidParameterException(
526527
"Cipher opmode must be ENCRYPT_MODE or DECRPYT_MODE");
527528
}
528529
}

src/test/java/com/wolfssl/provider/jce/test/WolfCryptCipherTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import java.security.NoSuchAlgorithmException;
6161
import java.security.InvalidKeyException;
6262
import java.security.InvalidAlgorithmParameterException;
63+
import java.security.InvalidParameterException;
6364
import java.security.spec.AlgorithmParameterSpec;
6465
import java.security.AlgorithmParameters;
6566

@@ -191,6 +192,35 @@ public void testGetBlockSize()
191192
}
192193
}
193194

195+
@Test
196+
public void testAesInvalidModeThrowsInvalidParameterException()
197+
throws NoSuchProviderException, NoSuchAlgorithmException,
198+
NoSuchPaddingException, InvalidKeyException,
199+
InvalidAlgorithmParameterException {
200+
201+
Cipher cipher;
202+
203+
try {
204+
cipher = Cipher.getInstance("AES/CBC/NoPadding", jceProvider);
205+
} catch (NoSuchAlgorithmException e) {
206+
/* skip if AES-CBC is not enabled */
207+
return;
208+
}
209+
210+
SecretKeySpec keySpec = new SecretKeySpec(new byte[16], "AES");
211+
IvParameterSpec ivSpec = new IvParameterSpec(new byte[16]);
212+
213+
int invalidMode = 100;
214+
215+
try {
216+
cipher.init(invalidMode, keySpec, ivSpec);
217+
fail("Cipher.init() with invalid mode should throw " +
218+
"InvalidParameterException");
219+
} catch (InvalidParameterException e) {
220+
/* expected */
221+
}
222+
}
223+
194224
@Test
195225
public void testAesCbcNoPadding()
196226
throws NoSuchProviderException, NoSuchAlgorithmException,

0 commit comments

Comments
 (0)