Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/provider/DSA/TestDSA2.java
38853 views
1
/*
2
* Copyright (c) 2012, 2015, 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
* @test
25
* @bug 7044060
26
* @run main/othervm/timeout=250 TestDSA2
27
* @summary verify that DSA signature works using SHA and SHA-224 and SHA-256 digests.
28
* @key randomness
29
*/
30
31
32
import java.security.*;
33
import java.security.spec.*;
34
import java.security.interfaces.*;
35
36
public class TestDSA2 {
37
38
// NOTE: need to explictly specify provider since the more
39
// preferred provider SunPKCS11 provider only supports up
40
// 1024 bits.
41
private static final String PROV = "SUN";
42
43
private static final String[] SIG_ALGOS = {
44
"SHA1withDSA", "SHA224withDSA", "SHA256withDSA"
45
};
46
47
private static final int[] KEYSIZES = {
48
1024, 2048
49
};
50
51
public static void main(String[] args) throws Exception {
52
boolean[] expectedToPass = { true, true, true };
53
test(1024, expectedToPass);
54
boolean[] expectedToPass2 = { false, true, true };
55
test(2048, expectedToPass2);
56
}
57
58
private static void test(int keySize, boolean[] testStatus)
59
throws Exception {
60
byte[] data = "1234567890".getBytes();
61
System.out.println("Test against key size: " + keySize);
62
63
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", PROV);
64
keyGen.initialize(keySize, new SecureRandom());
65
KeyPair pair = keyGen.generateKeyPair();
66
67
if (testStatus.length != SIG_ALGOS.length) {
68
throw new RuntimeException("TestError: incorrect status array!");
69
}
70
for (int i = 0; i < SIG_ALGOS.length; i++) {
71
Signature dsa = Signature.getInstance(SIG_ALGOS[i], PROV);
72
try {
73
dsa.initSign(pair.getPrivate());
74
dsa.update(data);
75
byte[] sig = dsa.sign();
76
dsa.initVerify(pair.getPublic());
77
dsa.update(data);
78
boolean verifies = dsa.verify(sig);
79
if (verifies == testStatus[i]) {
80
System.out.println(SIG_ALGOS[i] + ": Passed");
81
} else {
82
System.out.println(SIG_ALGOS[i] + ": should " +
83
(testStatus[i]? "pass":"fail"));
84
throw new RuntimeException(SIG_ALGOS[i] + ": Unexpected Test result!");
85
86
}
87
} catch (Exception ex) {
88
if (testStatus[i]) {
89
ex.printStackTrace();
90
throw new RuntimeException(SIG_ALGOS[i] + ": Unexpected exception " + ex);
91
} else {
92
System.out.println(SIG_ALGOS[i] + ": Passed, expected " + ex);
93
}
94
}
95
}
96
}
97
}
98
99