27182
|
1 |
/*
|
37909
|
2 |
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
|
27182
|
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
|
37909
|
26 |
* @bug 7088989 8000415
|
27182
|
27 |
* @summary Ensure the various message digests works correctly
|
30046
|
28 |
* @key randomness
|
27182
|
29 |
*/
|
|
30 |
|
|
31 |
import java.io.*;
|
|
32 |
import java.security.*;
|
|
33 |
import java.security.spec.*;
|
|
34 |
import java.util.*;
|
|
35 |
import javax.crypto.*;
|
|
36 |
import javax.crypto.spec.*;
|
|
37 |
|
|
38 |
public class TestDigest extends UcryptoTest {
|
|
39 |
|
|
40 |
private static final String[] MD_ALGOS = {
|
|
41 |
"MD5",
|
|
42 |
"SHA",
|
37909
|
43 |
"SHA-224",
|
27182
|
44 |
"SHA-256",
|
|
45 |
"SHA-384",
|
37909
|
46 |
"SHA-512",
|
|
47 |
"SHA3-224",
|
|
48 |
"SHA3-256",
|
|
49 |
"SHA3-384",
|
|
50 |
"SHA3-512"
|
27182
|
51 |
};
|
|
52 |
|
|
53 |
public static void main(String[] args) throws Exception {
|
|
54 |
main(new TestDigest(), null);
|
|
55 |
}
|
|
56 |
|
37909
|
57 |
public void doTest(Provider p) throws Exception {
|
27182
|
58 |
boolean testPassed = true;
|
|
59 |
byte[] msg = new byte[200];
|
|
60 |
(new SecureRandom()).nextBytes(msg);
|
|
61 |
String interopProvName = "SUN";
|
|
62 |
|
37909
|
63 |
MessageDigest md, md2;
|
|
64 |
|
27182
|
65 |
for (String a : MD_ALGOS) {
|
37909
|
66 |
System.out.println("Testing " + a);
|
27182
|
67 |
try {
|
37909
|
68 |
md = MessageDigest.getInstance(a, p);
|
|
69 |
} catch (NoSuchAlgorithmException nsae) {
|
|
70 |
System.out.println("=> Skip, unsupported");
|
|
71 |
continue;
|
|
72 |
}
|
|
73 |
try {
|
27182
|
74 |
md2 = MessageDigest.getInstance(a, interopProvName);
|
37909
|
75 |
} catch (NoSuchAlgorithmException nsae) {
|
|
76 |
System.out.println("=> Skip, no interop provider found");
|
|
77 |
continue;
|
|
78 |
}
|
27182
|
79 |
|
37909
|
80 |
// Test Interoperability for update+digest calls
|
|
81 |
for (int i = 0; i < 3; i++) {
|
|
82 |
md.update(msg);
|
|
83 |
byte[] digest = md.digest();
|
|
84 |
md2.update(msg);
|
|
85 |
byte[] digest2 = md2.digest();
|
|
86 |
if (!Arrays.equals(digest, digest2)) {
|
|
87 |
System.out.println("DIFF1 FAILED at iter " + i);
|
|
88 |
testPassed = false;
|
|
89 |
} else {
|
|
90 |
System.out.println("...diff1 test passed");
|
|
91 |
}
|
|
92 |
}
|
27182
|
93 |
|
37909
|
94 |
// Test Interoperability for digest calls
|
|
95 |
md = MessageDigest.getInstance(a, p);
|
|
96 |
md2 = MessageDigest.getInstance(a, interopProvName);
|
27182
|
97 |
|
37909
|
98 |
for (int i = 0; i < 3; i++) {
|
27182
|
99 |
byte[] digest = md.digest();
|
|
100 |
byte[] digest2 = md2.digest();
|
|
101 |
if (!Arrays.equals(digest, digest2)) {
|
37909
|
102 |
System.out.println("DIFF2 FAILED at iter " + i);
|
27182
|
103 |
testPassed = false;
|
37909
|
104 |
} else {
|
|
105 |
System.out.println("...diff2 test passed");
|
27182
|
106 |
}
|
37909
|
107 |
}
|
|
108 |
|
|
109 |
// Test Cloning functionality if supported
|
|
110 |
md = MessageDigest.getInstance(a, p);
|
|
111 |
try {
|
|
112 |
md2 = (MessageDigest) md.clone(); // clone right after construction
|
|
113 |
} catch (CloneNotSupportedException cnse) {
|
|
114 |
System.out.println("...no clone support");
|
|
115 |
continue;
|
|
116 |
}
|
|
117 |
byte[] digest = md.digest();
|
|
118 |
byte[] digest2 = md2.digest();
|
|
119 |
if (!Arrays.equals(digest, digest2)) {
|
|
120 |
System.out.println("DIFF-3.1 FAILED");
|
27182
|
121 |
testPassed = false;
|
37909
|
122 |
} else {
|
|
123 |
System.out.println("...diff3.1 tests passed");
|
|
124 |
}
|
|
125 |
md.update(msg);
|
|
126 |
md2 = (MessageDigest) md.clone(); // clone again after update call
|
|
127 |
digest = md.digest();
|
|
128 |
digest2 = md2.digest();
|
|
129 |
if (!Arrays.equals(digest, digest2)) {
|
|
130 |
System.out.println("DIFF-3.2 FAILED");
|
|
131 |
testPassed = false;
|
|
132 |
} else {
|
|
133 |
System.out.println("...diff3.2 tests passed");
|
|
134 |
}
|
|
135 |
md2 = (MessageDigest) md.clone(); // clone after digest
|
|
136 |
digest = md.digest();
|
|
137 |
digest2 = md2.digest();
|
|
138 |
if (!Arrays.equals(digest, digest2)) {
|
|
139 |
System.out.println("DIFF-3.3 FAILED");
|
|
140 |
testPassed = false;
|
|
141 |
} else {
|
|
142 |
System.out.println("...diff3.3 tests passed");
|
27182
|
143 |
}
|
|
144 |
}
|
|
145 |
if (!testPassed) {
|
|
146 |
throw new RuntimeException("One or more MD test failed!");
|
|
147 |
} else {
|
|
148 |
System.out.println("MD Tests Passed");
|
|
149 |
}
|
|
150 |
}
|
|
151 |
}
|