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