1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
/*
* Copyright (c) 2012, 2018, 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.
*/
/*
* @test
* @bug 6414899
* @summary Ensure the cloning functionality works.
* @author Valerie Peng
* @library /test/lib ..
* @key randomness
* @modules jdk.crypto.cryptoki
* @run main/othervm TestCloning
* @run main/othervm TestCloning sm
*/
import java.security.MessageDigest;
import java.security.Provider;
import java.util.Arrays;
import java.util.Random;
public class TestCloning extends PKCS11Test {
private static final String[] ALGOS = {
"MD2", "MD5", "SHA1", "SHA-224", "SHA-256", "SHA-384", "SHA-512"
};
public static void main(String[] args) throws Exception {
main(new TestCloning(), args);
}
private static final byte[] data1 = new byte[10];
private static final byte[] data2 = new byte[10*1024];
@Override
public void main(Provider p) throws Exception {
Random r = new Random();
byte[] data1 = new byte[10];
byte[] data2 = new byte[2*1024];
r.nextBytes(data1);
r.nextBytes(data2);
System.out.println("Testing against provider " + p.getName());
for (int i = 0; i < ALGOS.length; i++) {
if (p.getService("MessageDigest", ALGOS[i]) == null) {
System.out.println(ALGOS[i] + " is not supported, skipping");
continue;
} else {
System.out.println("Testing " + ALGOS[i] + " of " + p.getName());
MessageDigest md = MessageDigest.getInstance(ALGOS[i], p);
try {
md = testCloning(md, p);
// repeat the test again after generating digest once
for (int j = 0; j < 10; j++) {
md = testCloning(md, p);
}
} catch (Exception ex) {
if (ALGOS[i] == "MD2" &&
p.getName().equalsIgnoreCase("SunPKCS11-NSS")) {
// known bug in NSS; ignore for now
System.out.println("Ignore Known bug in MD2 of NSS");
continue;
}
throw ex;
}
}
}
}
private static MessageDigest testCloning(MessageDigest mdObj, Provider p)
throws Exception {
// copy#0: clone at state BLANK w/o any data
MessageDigest mdCopy0 = (MessageDigest) mdObj.clone();
// copy#1: clone again at state BUFFERED w/ very short data
mdObj.update(data1);
mdCopy0.update(data1);
MessageDigest mdCopy1 = (MessageDigest) mdObj.clone();
// copy#2: clone again after updating it w/ long data to trigger
// the state into INIT
mdObj.update(data2);
mdCopy0.update(data2);
mdCopy1.update(data2);
MessageDigest mdCopy2 = (MessageDigest) mdObj.clone();
// copy#3: clone again after updating it w/ very short data
mdObj.update(data1);
mdCopy0.update(data1);
mdCopy1.update(data1);
mdCopy2.update(data1);
MessageDigest mdCopy3 = (MessageDigest) mdObj.clone();
// copy#4: clone again after updating it w/ long data
mdObj.update(data2);
mdCopy0.update(data2);
mdCopy1.update(data2);
mdCopy2.update(data2);
mdCopy3.update(data2);
MessageDigest mdCopy4 = (MessageDigest) mdObj.clone();
// check digest equalities
byte[] answer = mdObj.digest();
byte[] result0 = mdCopy0.digest();
byte[] result1 = mdCopy1.digest();
byte[] result2 = mdCopy2.digest();
byte[] result3 = mdCopy3.digest();
byte[] result4 = mdCopy4.digest();
check(answer, result0, "copy0");
check(answer, result1, "copy1");
check(answer, result2, "copy2");
check(answer, result3, "copy3");
check(answer, result4, "copy4");
return mdCopy3;
}
private static void check(byte[] d1, byte[] d2, String copyName)
throws Exception {
if (Arrays.equals(d1, d2) == false) {
throw new RuntimeException(copyName + " digest mismatch!");
}
}
}
|