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