Skip to content

Commit 85e4a43

Browse files
authored
Merge pull request #143 from cconlon/CipherInitMode
Throw correct InvalidParameterException from Cipher.init() on unsupported mode
2 parents b13f649 + 6f2434a commit 85e4a43

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
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,8 +523,8 @@ private void wolfCryptSetDirection(int opmode)
522523
break;
523524

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

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import java.security.InvalidKeyException;
6262
import java.security.InvalidAlgorithmParameterException;
6363
import java.security.AlgorithmParameters;
64+
import java.security.InvalidParameterException;
6465
import java.security.spec.AlgorithmParameterSpec;
6566
import java.security.AlgorithmParameters;
6667

@@ -194,6 +195,35 @@ public void testGetBlockSize()
194195
}
195196
}
196197

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

0 commit comments

Comments
 (0)