Skip to content

Commit

Permalink
TKSS-420: SM4Parameters::engineGetParameterSpec throws NullPointerExc…
Browse files Browse the repository at this point in the history
…eption
  • Loading branch information
johnshajiang committed Sep 28, 2023
1 parent 98495e6 commit 06dd8dd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,24 @@ protected void engineInit(byte[] encoded, String decodingMethod)
protected <T extends AlgorithmParameterSpec> T engineGetParameterSpec(
Class<T> paramSpec) throws InvalidParameterSpecException {
if (IvParameterSpec.class.isAssignableFrom(paramSpec)) {
try {
decode();
} catch (IOException e) {
throw new InvalidParameterSpecException(
"Decode parameters failed: " + e.getMessage());
if (iv == null) {
try {
decode();
} catch (IOException e) {
throw new InvalidParameterSpecException(
"Decode parameters failed: " + e.getMessage());
}
}

return paramSpec.cast(new IvParameterSpec(iv));
} else if (GCMParameterSpec.class.isAssignableFrom(paramSpec)) {
try {
gcmDecode();
} catch (IOException e) {
throw new InvalidParameterSpecException(
"Decode GCM parameters failed: " + e.getMessage());
if (iv == null) {
try {
gcmDecode();
} catch (IOException e) {
throw new InvalidParameterSpecException(
"Decode GCM parameters failed: " + e.getMessage());
}
}

if (tagLen == SM4_GCM_TAG_LEN) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.AlgorithmParameters;
import java.security.spec.InvalidParameterSpecException;
Expand All @@ -39,11 +41,15 @@
*/
public class SM4ParametersTest {

private static final byte[] KEY = toBytes("0123456789abcdef0123456789abcdef");
private static final byte[] IV = "0123456789abcdef".getBytes(StandardCharsets.UTF_8);
private static final byte[] GCM_IV = "0123456789ab".getBytes(StandardCharsets.UTF_8);
private static final byte[] ENCODED_PARAMS = toBytes("041030313233343536373839616263646566");
private static final byte[] GCM_ENCODED_PARAMS = toBytes("3011040c303132333435363738396162020110");

private static final byte[] MESSAGE = toBytes(
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef");

@BeforeAll
public static void setup() {
TestUtils.addProviders();
Expand Down Expand Up @@ -91,4 +97,18 @@ public void testDecodeFailed() throws Exception {
InvalidParameterSpecException.class,
() -> gcmParams.getParameterSpec(GCMParameterSpec.class));
}

@Test
public void testGetParameters() throws Exception {
Cipher encrypter = Cipher.getInstance("SM4/CBC/NoPadding");
encrypter.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY, "SM4"));
AlgorithmParameters params = encrypter.getParameters();
byte[] cipertext = encrypter.doFinal(MESSAGE);

Cipher decrypter = Cipher.getInstance("SM4/CBC/NoPadding");
decrypter.init(Cipher.DECRYPT_MODE, new SecretKeySpec(KEY, "SM4"), params);
byte[] cleartext = decrypter.doFinal(cipertext);

Assertions.assertArrayEquals(MESSAGE, cleartext);
}
}

0 comments on commit 06dd8dd

Please sign in to comment.