8048607: Test key generation of DES and DESEDE
Reviewed-by: xuelei
This commit is contained in:
parent
e9f8462593
commit
48d9e1f35e
jdk/test
@ -27,39 +27,52 @@
|
||||
|
||||
public class TestUtility {
|
||||
|
||||
private static final String digits = "0123456789abcdef";
|
||||
private static final String DIGITS = "0123456789abcdef";
|
||||
|
||||
public TestUtility() {
|
||||
private TestUtility() {
|
||||
|
||||
}
|
||||
|
||||
public static String hexDump(byte[] bytes) {
|
||||
|
||||
StringBuffer buf = new StringBuffer (bytes.length * 2);
|
||||
int i;
|
||||
StringBuilder buf = new StringBuilder(bytes.length * 2);
|
||||
int i;
|
||||
|
||||
buf.append (" "); // four spaces
|
||||
buf.append(" "); // four spaces
|
||||
for (i = 0; i < bytes.length; i++) {
|
||||
buf.append (digits.charAt ((bytes[i] >> 4) & 0x0f));
|
||||
buf.append (digits.charAt (bytes[i] & 0x0f));
|
||||
if (((i + 1) % 32) == 0) {
|
||||
if ((i + 1) != bytes.length)
|
||||
buf.append ("\n "); // line after four words
|
||||
} else if (((i + 1) % 4) == 0)
|
||||
buf.append (' '); // space between words
|
||||
buf.append(DIGITS.charAt(bytes[i] >> 4 & 0x0f));
|
||||
buf.append(DIGITS.charAt(bytes[i] & 0x0f));
|
||||
if ((i + 1) % 32 == 0) {
|
||||
if (i + 1 != bytes.length) {
|
||||
buf.append("\n "); // line after four words
|
||||
}
|
||||
} else if ((i + 1) % 4 == 0) {
|
||||
buf.append(' '); // space between words
|
||||
}
|
||||
}
|
||||
return buf.toString ();
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public static String hexDump(byte[] bytes, int index) {
|
||||
StringBuilder buf = new StringBuilder(bytes.length * 2);
|
||||
int i;
|
||||
|
||||
buf.append(" "); // four spaces
|
||||
buf.append(DIGITS.charAt(bytes[index] >> 4 & 0x0f));
|
||||
buf.append(DIGITS.charAt(bytes[index] & 0x0f));
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public static boolean equalsBlock(byte[] b1, byte[] b2) {
|
||||
|
||||
if (b1.length != b2.length)
|
||||
if (b1.length != b2.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i=0; i<b1.length; i++) {
|
||||
if (b1[i] != b2[i])
|
||||
for (int i = 0; i < b1.length; i++) {
|
||||
if (b1[i] != b2[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -67,9 +80,10 @@ public class TestUtility {
|
||||
|
||||
public static boolean equalsBlock(byte[] b1, byte[] b2, int len) {
|
||||
|
||||
for (int i=0; i<len; i++) {
|
||||
if (b1[i] != b2[i])
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (b1[i] != b2[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
121
jdk/test/javax/crypto/KeyGenerator/TestKGParity.java
Normal file
121
jdk/test/javax/crypto/KeyGenerator/TestKGParity.java
Normal file
@ -0,0 +1,121 @@
|
||||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.lang.String;
|
||||
import java.lang.System;
|
||||
import java.security.Provider;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.Security;
|
||||
import javax.crypto.KeyGenerator;
|
||||
import static java.lang.System.out;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8048607
|
||||
* @compile ../../../com/sun/crypto/provider/Cipher/DES/TestUtility.java
|
||||
* @summary Test key generation of DES and DESEDE
|
||||
*/
|
||||
public class TestKGParity {
|
||||
|
||||
private static final String[] ALGORITHM_ARR = {
|
||||
"deS", "DesEDE"
|
||||
};
|
||||
|
||||
public static void main(String argv[]) throws Exception {
|
||||
|
||||
TestKGParity test = new TestKGParity();
|
||||
test.run();
|
||||
}
|
||||
|
||||
private void run() throws Exception {
|
||||
Provider[] providers = Security.getProviders();
|
||||
for (Provider p : providers) {
|
||||
String prvName = p.getName();
|
||||
if (prvName.startsWith("SunJCE")
|
||||
|| prvName.startsWith("SunPKCS11-")) {
|
||||
for (String algorithm : ALGORITHM_ARR) {
|
||||
if (!runTest(p, algorithm)) {
|
||||
throw new RuntimeException(
|
||||
"Test failed with provider/algorithm:"
|
||||
+ p.getName() + "/" + algorithm);
|
||||
} else {
|
||||
out.println("Test passed with provider/algorithm:"
|
||||
+ p.getName() + "/" + algorithm);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean runTest(Provider p, String algo) throws Exception {
|
||||
byte[] keyValue = null;
|
||||
try {
|
||||
// Initialization
|
||||
SecureRandom sRdm = new SecureRandom();
|
||||
KeyGenerator kg = KeyGenerator.getInstance(algo, p);
|
||||
kg.init(sRdm);
|
||||
|
||||
// Generate a SecretKey and retrieve its value
|
||||
keyValue = kg.generateKey().getEncoded();
|
||||
|
||||
// Verify its parity in the unit of byte
|
||||
for (int i = 0; i < keyValue.length; i++) {
|
||||
if (!checkParity(keyValue[i])) {
|
||||
out.println("Testing: "
|
||||
+ p.getName()
|
||||
+ "/"
|
||||
+ algo
|
||||
+ " failed when verify its parity in the unit of byte:"
|
||||
+ TestUtility.hexDump(keyValue, i));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
out.println("Testing: " + p.getName() + "/" + algo
|
||||
+ " failed with unexpected exception");
|
||||
ex.printStackTrace();
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean checkParity(byte keyByte) {
|
||||
boolean even = false;
|
||||
byte[] PARITY_BIT_MASK = {
|
||||
(byte) 0x40, (byte) 0x20, (byte) 0x10, (byte) 0x08,
|
||||
(byte) 0x04, (byte) 0x02, (byte) 0x01
|
||||
};
|
||||
|
||||
for (int i = 0; i < 7; i++) {
|
||||
if ((keyByte & PARITY_BIT_MASK[i]) > 0) {
|
||||
even = !even;
|
||||
}
|
||||
}
|
||||
if (keyByte < 0) {
|
||||
even = !even;
|
||||
}
|
||||
|
||||
return even;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user