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 
main(String[] args)45     public static void main(String[] args) throws Exception {
46         TestSameLength test = new TestSameLength();
47         test.run();
48     }
49 
run()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 
runTest(String algo, long dataLen, UpdateMethod whichUpdate)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     // and "OracleUcrypto" provider on Solaris 12.0 or later
107     // This method checks if system supports SHA-3
isSHA3supported()108     private boolean isSHA3supported() {
109         if (Security.getProvider("SUN") != null) {
110             return true;
111         }
112         if (Security.getProvider("OracleUcrypto") != null
113                 && "SunOS".equals(System.getProperty("os.name"))
114                 && System.getProperty("os.version").compareTo("5.12") >= 0) {
115             return true;
116         }
117         return false;
118     }
119 
120     private static enum UpdateMethod {
121         UPDATE_BYTE {
122             @Override
updateDigest(byte[] data, MessageDigest md, long dataLen)123             public void updateDigest(byte[] data, MessageDigest md,
124                     long dataLen) {
125 
126                 for (int i = 0; i < dataLen; i++) {
127                     md.update(data[i]);
128                 }
129             }
130         },
131 
132         UPDATE_BUFFER {
133             @Override
updateDigest(byte[] data, MessageDigest md, long dataLen)134             public void updateDigest(byte[] data, MessageDigest md,
135                     long dataLen) {
136 
137                 md.update(data);
138             }
139         },
140 
141         UPDATE_BUFFER_LEN {
142             @Override
updateDigest(byte[] data, MessageDigest md, long dataLen)143             public void updateDigest(byte[] data, MessageDigest md,
144                     long dataLen) {
145 
146                 for (int i = 0; i < dataLen; i++) {
147                     md.update(data, i, 1);
148                 }
149             }
150         },
151 
152         UPDATE_BYTE_BUFFER {
153             @Override
updateDigest(byte[] data, MessageDigest md, long dataLen)154             public void updateDigest(byte[] data, MessageDigest md,
155                     long dataLen) {
156 
157                 md.update(ByteBuffer.wrap(data));
158             }
159         };
160 
updateDigest(byte[] data, MessageDigest md, long dataLen)161         public abstract void updateDigest(byte[] data, MessageDigest md,
162                 long dataLen);
163     }
164 }
165