提交 767c1f0a 编写于 作者: V valeriep

8170245: [TEST_BUG] Cipher tests fail when running with unlimited policy

Summary: Updated the failed cipher tests to work under unlimited policy
Reviewed-by: xuelei
上级 338be0a0
/* /*
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -150,19 +150,8 @@ public class Dynamic { ...@@ -150,19 +150,8 @@ public class Dynamic {
int offset = ci.update(plainText, 0, plainText.length, cipherText, int offset = ci.update(plainText, 0, plainText.length, cipherText,
0); 0);
ci.doFinal(cipherText, offset); ci.doFinal(cipherText, offset);
ci.init(Cipher.DECRYPT_MODE, key, ci.getParameters());
if (!mo.equalsIgnoreCase("ECB")) {
iv = ci.getIV();
aps = new IvParameterSpec(iv);
} else {
aps = null;
}
if (!mo.equalsIgnoreCase("GCM")) {
ci.init(Cipher.DECRYPT_MODE, key, aps);
} else {
ci.init(Cipher.DECRYPT_MODE, key, ci.getParameters());
}
byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)]; byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
int len = ci.doFinal(cipherText, 0, cipherText.length, int len = ci.doFinal(cipherText, 0, cipherText.length,
recoveredText); recoveredText);
...@@ -174,12 +163,14 @@ public class Dynamic { ...@@ -174,12 +163,14 @@ public class Dynamic {
result = Arrays.equals(plainText, tmp); result = Arrays.equals(plainText, tmp);
} catch (NoSuchAlgorithmException nsaEx) { } catch (NoSuchAlgorithmException nsaEx) {
nsaEx.printStackTrace();
// CFB7 and OFB150 are negative test,SunJCE not support this // CFB7 and OFB150 are negative test,SunJCE not support this
// algorithm // algorithm
result = mo.equalsIgnoreCase("CFB7") result = mo.equalsIgnoreCase("CFB7")
|| mo.equalsIgnoreCase("OFB150"); || mo.equalsIgnoreCase("OFB150");
if (!result) {
// only report unexpected exception
nsaEx.printStackTrace();
}
} }
return result; return result;
} }
......
/* /*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -42,7 +42,7 @@ public class TestCipherBlowfish extends TestCipher { ...@@ -42,7 +42,7 @@ public class TestCipherBlowfish extends TestCipher {
"OFB", "OFB8", "OFB16", "OFB24", "OFB32", "OFB40", "OFB48", "OFB56", "OFB", "OFB8", "OFB16", "OFB24", "OFB32", "OFB40", "OFB48", "OFB56",
"OFB64"}, "OFB64"},
new String[]{"NoPaDDing", "PKCS5Padding"}, new String[]{"NoPaDDing", "PKCS5Padding"},
true); 32, 448);
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
......
/* /*
* Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
import java.io.PrintStream; import java.io.PrintStream;
import java.security.AlgorithmParameters; import java.security.AlgorithmParameters;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Provider; import java.security.Provider;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory; import javax.crypto.SecretKeyFactory;
...@@ -69,6 +70,15 @@ public class AESPBEWrapper extends PBEWrapper { ...@@ -69,6 +70,15 @@ public class AESPBEWrapper extends PBEWrapper {
*/ */
@Override @Override
public boolean execute(int edMode, byte[] inputText, int offset, int len) { public boolean execute(int edMode, byte[] inputText, int offset, int len) {
boolean isUnlimited;
try {
isUnlimited =
(Cipher.getMaxAllowedKeyLength(this.algo) == Integer.MAX_VALUE);
} catch (NoSuchAlgorithmException nsae) {
out.println("Got unexpected exception for " + this.algo);
nsae.printStackTrace(out);
return false;
}
try { try {
// init Cipher // init Cipher
if (Cipher.ENCRYPT_MODE == edMode) { if (Cipher.ENCRYPT_MODE == edMode) {
...@@ -78,6 +88,11 @@ public class AESPBEWrapper extends PBEWrapper { ...@@ -78,6 +88,11 @@ public class AESPBEWrapper extends PBEWrapper {
ci.init(Cipher.DECRYPT_MODE, this.key, pbeParams); ci.init(Cipher.DECRYPT_MODE, this.key, pbeParams);
} }
if (this.algo.endsWith("AES_256") && !isUnlimited) {
out.print("Expected exception not thrown for " + this.algo);
return false;
}
// First, generate the cipherText at an allocated buffer // First, generate the cipherText at an allocated buffer
byte[] outputText = ci.doFinal(inputText, offset, len); byte[] outputText = ci.doFinal(inputText, offset, len);
...@@ -86,29 +101,19 @@ public class AESPBEWrapper extends PBEWrapper { ...@@ -86,29 +101,19 @@ public class AESPBEWrapper extends PBEWrapper {
int off = ci.update(inputText, offset, len, inputText, myoff); int off = ci.update(inputText, offset, len, inputText, myoff);
ci.doFinal(inputText, myoff + off); ci.doFinal(inputText, myoff + off);
if (this.algo.endsWith("AES_256")) {
out.print("Expected exception uncaught, "
+ "keyStrength > 128 within " + this.algo);
return false;
}
// Compare to see whether the two results are the same or not // Compare to see whether the two results are the same or not
return equalsBlock(inputText, myoff, outputText, 0, return equalsBlock(inputText, myoff, outputText, 0,
outputText.length); outputText.length);
} catch (Exception ex) { } catch (Exception ex) {
if ((ex instanceof InvalidKeyException) if ((ex instanceof InvalidKeyException)
&& this.algo.endsWith("AES_256")) { && this.algo.endsWith("AES_256") && !isUnlimited) {
out.println("Expected InvalidKeyException exception: " out.println("Expected InvalidKeyException thrown");
+ ex.getMessage());
return true; return true;
} else {
out.println("Got unexpected exception for " + algo);
ex.printStackTrace(out);
return false;
} }
out.println("Catch unexpected exception within " + algo);
ex.printStackTrace(out);
return false;
} }
} }
} }
/* /*
* Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -32,6 +32,7 @@ import java.security.spec.AlgorithmParameterSpec; ...@@ -32,6 +32,7 @@ import java.security.spec.AlgorithmParameterSpec;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Provider; import java.security.Provider;
import java.io.PrintStream; import java.io.PrintStream;
...@@ -64,9 +65,25 @@ public class PBECipherWrapper extends PBEWrapper { ...@@ -64,9 +65,25 @@ public class PBECipherWrapper extends PBEWrapper {
StringTokenizer st = new StringTokenizer(algo, "/"); StringTokenizer st = new StringTokenizer(algo, "/");
String baseAlgo = st.nextToken().toUpperCase(); String baseAlgo = st.nextToken().toUpperCase();
boolean isUnlimited;
try {
isUnlimited =
(Cipher.getMaxAllowedKeyLength(this.algo) == Integer.MAX_VALUE);
} catch (NoSuchAlgorithmException nsae) {
out.println("Got unexpected exception for " + this.algo);
nsae.printStackTrace(out);
return false;
}
// Perform encryption or decryption depends on the specified edMode // Perform encryption or decryption depends on the specified edMode
try { try {
ci.init(edMode, key, aps); ci.init(edMode, key, aps);
if ((baseAlgo.endsWith("TRIPLEDES")
|| baseAlgo.endsWith("AES_256")) && !isUnlimited) {
out.print("Expected InvalidKeyException not thrown: "
+ this.algo);
return false;
}
// First, generate the cipherText at an allocated buffer // First, generate the cipherText at an allocated buffer
byte[] outputText = ci.doFinal(inputText, offset, len); byte[] outputText = ci.doFinal(inputText, offset, len);
...@@ -78,33 +95,24 @@ public class PBECipherWrapper extends PBEWrapper { ...@@ -78,33 +95,24 @@ public class PBECipherWrapper extends PBEWrapper {
ci.doFinal(inputText, myoff + off); ci.doFinal(inputText, myoff + off);
if (baseAlgo.endsWith("TRIPLEDES")
|| baseAlgo.endsWith("AES_256")) {
out.print("Expected exception uncaught,"
+ "keyStrength > 128 within " + this.algo);
return false;
}
// Compare to see whether the two results are the same or not // Compare to see whether the two results are the same or not
boolean result = equalsBlock(inputText, myoff, outputText, 0, boolean result = equalsBlock(inputText, myoff, outputText, 0,
outputText.length); outputText.length);
return result; return result;
} catch (Exception ex) { } catch (Exception ex) {
if ((ex instanceof InvalidKeyException) if ((ex instanceof InvalidKeyException) &&
&& (baseAlgo.endsWith("TRIPLEDES") (baseAlgo.endsWith("TRIPLEDES")
|| baseAlgo.endsWith("AES_256"))) { || baseAlgo.endsWith("AES_256")) &&
out.println("Expected InvalidKeyException exception: " !isUnlimited) {
+ ex.getMessage()); out.println("Expected InvalidKeyException thrown for "
+ algo);
return true; return true;
} else {
out.println("Got unexpected exception for " + algo);
ex.printStackTrace(out);
return false;
} }
out.println("Catch unexpected exception within " + algo);
ex.printStackTrace(out);
return false;
} }
} }
} }
/* /*
* Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -119,6 +119,9 @@ public class TestCipherKeyWrapperPBEKey { ...@@ -119,6 +119,9 @@ public class TestCipherKeyWrapperPBEKey {
= new StringTokenizer(algo, "/").nextToken().toUpperCase(); = new StringTokenizer(algo, "/").nextToken().toUpperCase();
boolean isAES = baseAlgo.contains("AES"); boolean isAES = baseAlgo.contains("AES");
boolean isUnlimited =
(Cipher.getMaxAllowedKeyLength(algo) == Integer.MAX_VALUE);
try { try {
// Initialization // Initialization
new Random().nextBytes(salt); new Random().nextBytes(salt);
...@@ -128,7 +131,6 @@ public class TestCipherKeyWrapperPBEKey { ...@@ -128,7 +131,6 @@ public class TestCipherKeyWrapperPBEKey {
SecretKey key = skf.generateSecret(new PBEKeySpec( SecretKey key = skf.generateSecret(new PBEKeySpec(
"Secret Key".toCharArray())); "Secret Key".toCharArray()));
Cipher ci = Cipher.getInstance(algo); Cipher ci = Cipher.getInstance(algo);
if (isAES) { if (isAES) {
ci.init(Cipher.WRAP_MODE, key); ci.init(Cipher.WRAP_MODE, key);
pbeParams = ci.getParameters(); pbeParams = ci.getParameters();
...@@ -145,10 +147,10 @@ public class TestCipherKeyWrapperPBEKey { ...@@ -145,10 +147,10 @@ public class TestCipherKeyWrapperPBEKey {
Key unwrappedKey = ci.unwrap(keyWrapper, algo, Cipher.SECRET_KEY); Key unwrappedKey = ci.unwrap(keyWrapper, algo, Cipher.SECRET_KEY);
if (baseAlgo.endsWith("TRIPLEDES") if ((baseAlgo.endsWith("TRIPLEDES")
|| baseAlgo.endsWith("AES_256")) { || baseAlgo.endsWith("AES_256")) && !isUnlimited) {
out.print( out.print(
"InvalidKeyException not thrown when keyStrength > 128"); "Expected InvalidKeyException not thrown");
return false; return false;
} }
...@@ -157,8 +159,9 @@ public class TestCipherKeyWrapperPBEKey { ...@@ -157,8 +159,9 @@ public class TestCipherKeyWrapperPBEKey {
} catch (InvalidKeyException ex) { } catch (InvalidKeyException ex) {
if ((baseAlgo.endsWith("TRIPLEDES") if ((baseAlgo.endsWith("TRIPLEDES")
|| baseAlgo.endsWith("AES_256"))) { || baseAlgo.endsWith("AES_256")) && !isUnlimited) {
out.println("Expected InvalidKeyException, keyStrength > 128"); out.print(
"Expected InvalidKeyException thrown");
return true; return true;
} else { } else {
throw ex; throw ex;
......
/* /*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -79,6 +79,9 @@ public class TestCipherPBE { ...@@ -79,6 +79,9 @@ public class TestCipherPBE {
out.println("=> Testing: " + algorithm); out.println("=> Testing: " + algorithm);
boolean isUnlimited =
(Cipher.getMaxAllowedKeyLength(algorithm) == Integer.MAX_VALUE);
try { try {
// Initialization // Initialization
AlgorithmParameterSpec algoParamSpec AlgorithmParameterSpec algoParamSpec
...@@ -98,9 +101,9 @@ public class TestCipherPBE { ...@@ -98,9 +101,9 @@ public class TestCipherPBE {
ci.init(Cipher.DECRYPT_MODE, secretKey, algoParamSpec); ci.init(Cipher.DECRYPT_MODE, secretKey, algoParamSpec);
byte[] recoveredText = ci.doFinal(cipherText); byte[] recoveredText = ci.doFinal(cipherText);
if (algorithm.contains("TripleDES")) { if (algorithm.contains("TripleDES") && !isUnlimited) {
throw new RuntimeException( throw new RuntimeException(
"Expected InvalidKeyException exception uncaugh"); "Expected InvalidKeyException not thrown");
} }
// Comparison // Comparison
...@@ -110,8 +113,8 @@ public class TestCipherPBE { ...@@ -110,8 +113,8 @@ public class TestCipherPBE {
} }
out.println("Test Passed."); out.println("Test Passed.");
} catch (InvalidKeyException ex) { } catch (InvalidKeyException ex) {
if (algorithm.contains("TripleDES")) { if (algorithm.contains("TripleDES") && !isUnlimited) {
out.println("Expected InvalidKeyException raised"); out.println("Expected InvalidKeyException thrown");
} else { } else {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
......
...@@ -49,14 +49,13 @@ public abstract class TestCipher { ...@@ -49,14 +49,13 @@ public abstract class TestCipher {
private final String[] MODES; private final String[] MODES;
private final String[] PADDINGS; private final String[] PADDINGS;
/* Used to test cipher with different key strengths /* Used to test variable-key-length ciphers:
Key size tested is increment of KEYCUTTER from MINIMUM_KEY_SIZE to Key size tested is increment of KEYCUTTER from minKeySize
maximum allowed keysize. to min(maxKeySize, Cipher.getMaxAllowedKeyLength(algo)).
DES/DESede/Blowfish work with currently selected key sizes.
*/ */
private final int variousKeySize;
private final int KEYCUTTER = 8; private final int KEYCUTTER = 8;
private final int MINIMUM_KEY_SIZE = 32; private final int minKeySize;
private final int maxKeySize;
// Used to assert that Encryption/Decryption works with same buffer // Used to assert that Encryption/Decryption works with same buffer
// TEXT_LEN is multiple of blocks in order to work against ciphers w/ NoPadding // TEXT_LEN is multiple of blocks in order to work against ciphers w/ NoPadding
...@@ -68,23 +67,28 @@ public abstract class TestCipher { ...@@ -68,23 +67,28 @@ public abstract class TestCipher {
private final byte[] IV; private final byte[] IV;
private final byte[] INPUT_TEXT; private final byte[] INPUT_TEXT;
// for variable-key-length ciphers
TestCipher(String algo, String[] modes, String[] paddings, TestCipher(String algo, String[] modes, String[] paddings,
boolean keyStrength) throws NoSuchAlgorithmException { int minKeySize, int maxKeySize) throws NoSuchAlgorithmException {
ALGORITHM = algo; ALGORITHM = algo;
MODES = modes; MODES = modes;
PADDINGS = paddings; PADDINGS = paddings;
this.variousKeySize this.minKeySize = minKeySize;
= keyStrength ? Cipher.getMaxAllowedKeyLength(ALGORITHM) : 0; int maxAllowedKeySize = Cipher.getMaxAllowedKeyLength(ALGORITHM);
if (maxKeySize > maxAllowedKeySize) {
maxKeySize = maxAllowedKeySize;
}
this.maxKeySize = maxKeySize;
IV = generateBytes(8); IV = generateBytes(8);
INPUT_TEXT = generateBytes(TEXT_LEN + PAD_BYTES + ENC_OFFSET); INPUT_TEXT = generateBytes(TEXT_LEN + PAD_BYTES + ENC_OFFSET);
} }
// for fixed-key-length ciphers
TestCipher(String algo, String[] modes, String[] paddings) { TestCipher(String algo, String[] modes, String[] paddings) {
ALGORITHM = algo; ALGORITHM = algo;
MODES = modes; MODES = modes;
PADDINGS = paddings; PADDINGS = paddings;
variousKeySize = 0; this.minKeySize = this.maxKeySize = 0;
IV = generateBytes(8); IV = generateBytes(8);
INPUT_TEXT = generateBytes(TEXT_LEN + PAD_BYTES + ENC_OFFSET); INPUT_TEXT = generateBytes(TEXT_LEN + PAD_BYTES + ENC_OFFSET);
...@@ -98,8 +102,8 @@ public abstract class TestCipher { ...@@ -98,8 +102,8 @@ public abstract class TestCipher {
return bytes; return bytes;
} }
private boolean isKeyStrenthSupported() { private boolean isMultipleKeyLengthSupported() {
return (variousKeySize != 0); return (maxKeySize != minKeySize);
} }
public void runAll() throws InvalidKeyException, public void runAll() throws InvalidKeyException,
...@@ -110,11 +114,11 @@ public abstract class TestCipher { ...@@ -110,11 +114,11 @@ public abstract class TestCipher {
for (String mode : MODES) { for (String mode : MODES) {
for (String padding : PADDINGS) { for (String padding : PADDINGS) {
if (!isKeyStrenthSupported()) { if (!isMultipleKeyLengthSupported()) {
runTest(mode, padding, 0); runTest(mode, padding, minKeySize);
} else { } else {
int keySize = variousKeySize; int keySize = maxKeySize;
while (keySize >= MINIMUM_KEY_SIZE) { while (keySize >= minKeySize) {
out.println("With Key Strength: " + keySize); out.println("With Key Strength: " + keySize);
runTest(mode, padding, keySize); runTest(mode, padding, keySize);
keySize -= KEYCUTTER; keySize -= KEYCUTTER;
...@@ -139,6 +143,7 @@ public abstract class TestCipher { ...@@ -139,6 +143,7 @@ public abstract class TestCipher {
if (keySize != 0) { if (keySize != 0) {
kg.init(keySize); kg.init(keySize);
} }
SecretKey key = kg.generateKey(); SecretKey key = kg.generateKey();
SecretKeySpec skeySpec = new SecretKeySpec(key.getEncoded(), ALGORITHM); SecretKeySpec skeySpec = new SecretKeySpec(key.getEncoded(), ALGORITHM);
...@@ -150,7 +155,6 @@ public abstract class TestCipher { ...@@ -150,7 +155,6 @@ public abstract class TestCipher {
} }
// Encryption // Encryption
byte[] plainText = INPUT_TEXT.clone(); byte[] plainText = INPUT_TEXT.clone();
// Generate cipher and save to separate buffer // Generate cipher and save to separate buffer
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册