Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs11/Signature/TestDSA2.java
41153 views
1
/*
2
* Copyright (c) 2012, 2020, 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 8080462 8242332
26
* @library /test/lib ..
27
* @modules jdk.crypto.cryptoki
28
* @run main/othervm/timeout=250 TestDSA2
29
* @summary verify that DSA signature works using SHA-2 digests.
30
* @key randomness
31
*/
32
33
34
import java.security.*;
35
import java.security.spec.*;
36
import java.security.interfaces.*;
37
38
public class TestDSA2 extends PKCS11Test {
39
40
private static final String[] SIG_ALGOS = {
41
"SHA224withDSA",
42
"SHA256withDSA",
43
"SHA3-224withDSA",
44
"SHA3-256withDSA",
45
"SHA384withDSA",
46
"SHA512withDSA",
47
"SHA3-384withDSA",
48
"SHA3-512withDSA",
49
};
50
51
private static final int KEYSIZE = 2048;
52
53
public static void main(String[] args) throws Exception {
54
main(new TestDSA2(), args);
55
}
56
57
@Override
58
public void main(Provider p) throws Exception {
59
KeyPair kp;
60
try {
61
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA", p);
62
kpg.initialize(KEYSIZE);
63
kp = kpg.generateKeyPair();
64
} catch (Exception ex) {
65
System.out.println("Skip due to no 2048-bit DSA support: " + ex);
66
return;
67
}
68
69
boolean allPass = true;
70
for (String sigAlg : SIG_ALGOS) {
71
System.out.println("Testing " + sigAlg);
72
try {
73
Signature sig = Signature.getInstance(sigAlg, p);
74
test(sig, kp, p);
75
} catch (NoSuchAlgorithmException nsae) {
76
System.out.println("=>Skip due to no support");
77
} catch (Exception ex) {
78
System.out.println("Unexpected exception when testing " +
79
sigAlg);
80
ex.printStackTrace();
81
allPass = false;
82
}
83
}
84
if (allPass) {
85
System.out.println("Tests Passed");
86
} else {
87
throw new RuntimeException("One or more tests failed");
88
}
89
}
90
91
private static void test(Signature sig, KeyPair kp, Provider p)
92
throws Exception {
93
94
byte[] data = "anything will do".getBytes();
95
96
sig.initSign(kp.getPrivate());
97
sig.update(data);
98
byte[] signature = sig.sign();
99
100
Signature sigV = Signature.getInstance(sig.getAlgorithm() , p);
101
sigV.initVerify(kp.getPublic());
102
sigV.update(data);
103
boolean verifies = sigV.verify(signature);
104
System.out.println("=> Passed");
105
}
106
}
107
108