1 /* 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 6414899 27 * @summary Ensure the cloning functionality works. 28 * @author Valerie Peng 29 * @library .. 30 * @key randomness 31 */ 32 33 import java.util.*; 34 35 import java.security.*; 36 37 public class TestCloning extends PKCS11Test { 38 39 private static final String[] ALGOS = { 40 "MD2", "MD5", "SHA1", "SHA-224", "SHA-256", "SHA-384", "SHA-512" 41 }; 42 43 public static void main(String[] args) throws Exception { 44 main(new TestCloning()); 45 } 46 47 private static final byte[] data1 = new byte[10]; 48 private static final byte[] data2 = new byte[10*1024]; 49 50 51 public void main(Provider p) throws Exception { 52 Random r = new Random(); 53 byte[] data1 = new byte[10]; 54 byte[] data2 = new byte[2*1024]; 55 r.nextBytes(data1); 56 r.nextBytes(data2); 57 System.out.println("Testing against provider " + p.getName()); 58 for (int i = 0; i < ALGOS.length; i++) { 59 if (p.getService("MessageDigest", ALGOS[i]) == null) { 60 System.out.println(ALGOS[i] + " is not supported, skipping"); 61 continue; 62 } else { 63 System.out.println("Testing " + ALGOS[i] + " of " + p.getName()); 64 MessageDigest md = MessageDigest.getInstance(ALGOS[i], p); 65 try { 66 md = testCloning(md, p); 67 // repeat the test again after generating digest once 68 for (int j = 0; j < 10; j++) { 69 md = testCloning(md, p); 70 } 71 } catch (Exception ex) { 72 if (ALGOS[i] == "MD2" && 73 p.getName().equalsIgnoreCase("SunPKCS11-NSS")) { 74 // known bug in NSS; ignore for now 75 System.out.println("Ignore Known bug in MD2 of NSS"); 76 continue; 77 } 78 throw ex; 79 } 80 } 81 } 82 } 83 84 private static MessageDigest testCloning(MessageDigest mdObj, Provider p) 85 throws Exception { 86 87 // copy#0: clone at state BLANK w/o any data 88 MessageDigest mdCopy0 = (MessageDigest) mdObj.clone(); 89 90 // copy#1: clone again at state BUFFERED w/ very short data 91 mdObj.update(data1); 92 mdCopy0.update(data1); 93 MessageDigest mdCopy1 = (MessageDigest) mdObj.clone(); 94 95 // copy#2: clone again after updating it w/ long data to trigger 96 // the state into INIT 97 mdObj.update(data2); 98 mdCopy0.update(data2); 99 mdCopy1.update(data2); 100 MessageDigest mdCopy2 = (MessageDigest) mdObj.clone(); 101 102 // copy#3: clone again after updating it w/ very short data 103 mdObj.update(data1); 104 mdCopy0.update(data1); 105 mdCopy1.update(data1); 106 mdCopy2.update(data1); 107 MessageDigest mdCopy3 = (MessageDigest) mdObj.clone(); 108 109 // copy#4: clone again after updating it w/ long data 110 mdObj.update(data2); 111 mdCopy0.update(data2); 112 mdCopy1.update(data2); 113 mdCopy2.update(data2); 114 mdCopy3.update(data2); 115 MessageDigest mdCopy4 = (MessageDigest) mdObj.clone(); 116 117 // check digest equalities 118 byte[] answer = mdObj.digest(); 119 byte[] result0 = mdCopy0.digest(); 120 byte[] result1 = mdCopy1.digest(); 121 byte[] result2 = mdCopy2.digest(); 122 byte[] result3 = mdCopy3.digest(); 123 byte[] result4 = mdCopy4.digest(); 124 125 126 check(answer, result0, "copy0"); 127 check(answer, result1, "copy1"); 128 check(answer, result2, "copy2"); 129 check(answer, result3, "copy3"); 130 check(answer, result4, "copy4"); 131 132 return mdCopy3; 133 } 134 135 private static void check(byte[] d1, byte[] d2, String copyName) 136 throws Exception { 137 if (Arrays.equals(d1, d2) == false) { 138 throw new RuntimeException(copyName + " digest mismatch!"); 139 } 140 } 141 } 142