1 /* 2 * Copyright (c) 2015, 2017, 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 import static java.lang.System.out; 25 import java.nio.ByteBuffer; 26 import java.security.MessageDigest; 27 import java.security.NoSuchAlgorithmException; 28 import java.security.Security; 29 import jdk.test.lib.RandomFactory; 30 31 /** 32 * @test 33 * @bug 8050371 8156059 34 * @summary Check md.getDigestLength() equal digest output length with various 35 * algorithm/dataLen/(update,digest methods). 36 * @author Kevin Liu 37 * @key randomness 38 * @library /test/lib 39 */ 40 41 public class TestSameLength { 42 43 public static void main(String[] args) throws Exception { 44 TestSameLength test = new TestSameLength(); 45 test.run(); 46 } 47 48 private void run() throws Exception { 49 String[] algorithmArr = { "SHA", "Sha", "SHA-1", "sha-1", "SHA1", 50 "sha1", "MD5", "md5", "SHA-224", "SHA-256", "SHA-384", 51 "SHA-512", "SHA3-224", "SHA3-256", "SHA3-384", "SHA3-512" }; 52 int[] nUpdatesArr = { 0, 1, 2, 3 }; 53 int[] dataLenArr = { 1, 50, 2500, 125000, 6250000 }; 54 55 for (String algorithm : algorithmArr) { 56 for (UpdateMethod update : UpdateMethod.values()) { 57 for (int dataLen : dataLenArr) { 58 if (!runTest(algorithm, dataLen, update)) { 59 throw new RuntimeException( 60 "Test failed at algorithm/dataLen/numUpdate:" 61 + algorithm + "/" + dataLen + "/" 62 + update.toString()); 63 } 64 } 65 } 66 } 67 68 out.println("All " 69 + algorithmArr.length * nUpdatesArr.length * dataLenArr.length 70 + " tests Passed"); 71 } 72 73 private boolean runTest(String algo, long dataLen, UpdateMethod whichUpdate) 74 throws Exception { 75 try { 76 // Do initialization 77 byte[] data = new byte[(int) dataLen]; 78 RandomFactory.getRandom().nextBytes(data); 79 MessageDigest md = MessageDigest.getInstance(algo); 80 int outputLen = md.getDigestLength(); 81 82 // Perform the update using all available/possible update methods 83 whichUpdate.updateDigest(data, md, dataLen); 84 // Get the output 85 byte[] output = md.digest(); 86 87 // Compare input and output 88 return outputLen == output.length; 89 } catch (NoSuchAlgorithmException nae) { 90 if (algo.startsWith("SHA3") && !isSHA3supported()) { 91 return true; 92 } 93 throw nae; 94 } catch (Exception ex) { 95 System.err.println("Testing: " + algo + "/" + dataLen + "/" 96 + whichUpdate.toString() 97 + " failed with unexpected exception"); 98 ex.printStackTrace(); 99 throw ex; 100 } 101 } 102 103 // SHA-3 hash algorithms are only supported by "SUN" provider 104 // and "OracleUcrypto" provider on Solaris 12.0 or later 105 // This method checks if system supports SHA-3 106 private boolean isSHA3supported() { 107 if (Security.getProvider("SUN") != null) { 108 return true; 109 } 110 if (Security.getProvider("OracleUcrypto") != null 111 && "SunOS".equals(System.getProperty("os.name")) 112 && System.getProperty("os.version").compareTo("5.12") >= 0) { 113 return true; 114 } 115 return false; 116 } 117 118 private static enum UpdateMethod { 119 UPDATE_BYTE { 120 @Override 121 public void updateDigest(byte[] data, MessageDigest md, 122 long dataLen) { 123 124 for (int i = 0; i < dataLen; i++) { 125 md.update(data[i]); 126 } 127 } 128 }, 129 130 UPDATE_BUFFER { 131 @Override 132 public void updateDigest(byte[] data, MessageDigest md, 133 long dataLen) { 134 135 md.update(data); 136 } 137 }, 138 139 UPDATE_BUFFER_LEN { 140 @Override 141 public void updateDigest(byte[] data, MessageDigest md, 142 long dataLen) { 143 144 for (int i = 0; i < dataLen; i++) { 145 md.update(data, i, 1); 146 } 147 } 148 }, 149 150 UPDATE_BYTE_BUFFER { 151 @Override 152 public void updateDigest(byte[] data, MessageDigest md, 153 long dataLen) { 154 155 md.update(ByteBuffer.wrap(data)); 156 } 157 }; 158 159 public abstract void updateDigest(byte[] data, MessageDigest md, 160 long dataLen); 161 } 162 }