Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/provider/DSA/TestDSA.java
41154 views
1
/*
2
* Copyright (c) 2003, 2021, 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
/**
25
* @test
26
* @bug 4815057 4839277
27
* @summary basic test of SHA1withDSA and RawDSA signing/verifying
28
* @author Andreas Sterbenz
29
* @key randomness
30
*/
31
32
import java.io.*;
33
import java.util.*;
34
import java.math.BigInteger;
35
36
import java.security.*;
37
import java.security.spec.*;
38
39
public class TestDSA {
40
41
// values of the keys we use for the tests
42
43
private final static String ps =
44
"fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669" +
45
"455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b7" +
46
"6b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb" +
47
"83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c7";
48
49
private final static String qs =
50
"9760508f15230bccb292b982a2eb840bf0581cf5";
51
52
private final static String gs =
53
"f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d078267" +
54
"5159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e1" +
55
"3c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243b" +
56
"cca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a";
57
58
private final static String xs =
59
"2952afd9aef9527f9b40d23c8916f7d046028f9d";
60
61
private final static String ys =
62
"b16ddb0f9394c328c983ecf23b20014ace368a1af5728dffbf1162de9ed8ebf6" +
63
"384f323930e091503035caa797e3674221fc16136240b5474799ede2b7b11313" +
64
"7574a9c26bcf900940027b4bcd511ef1d1daf2e69c416aebaf3bdf39f02473b9" +
65
"d963f99414c09d97bb0830d9fbdcf7bb9dad8a2179fcdf296838c4cfab8f4d8f";
66
67
private final static BigInteger p = new BigInteger(ps, 16);
68
private final static BigInteger q = new BigInteger(qs, 16);
69
private final static BigInteger g = new BigInteger(gs, 16);
70
private final static BigInteger x = new BigInteger(xs, 16);
71
private final static BigInteger y = new BigInteger(ys, 16);
72
73
// data for test 1, original and SHA-1 hashed
74
private final static byte[] data1Raw = HexFormat.of()
75
.parseHex("0102030405060708090a0b0c0d0e0f10111213");
76
private final static byte[] data1SHA = HexFormat.ofDelimiter(":")
77
.parseHex("00:e2:5f:c9:1c:8f:d6:8c:6a:dc:c6:bd:f0:46:60:5e:a2:cd:8d:ad");
78
79
// valid signatures of data1. sig1b uses incorrect ASN.1 encoding,
80
// which we want to accept anyway for compatibility
81
private final static byte[] sig1a = HexFormat.ofDelimiter(":")
82
.parseHex("30:2d:02:14:53:06:3f:7d:ec:48:3c:99:17:9a:2c:a9:4d:e8:00:da:70:fb:35:d7:02:15:00:92:6a:39:6b:15:63:2f:e7:32:90:35:bf:af:47:55:e7:ff:33:a5:13");
83
private final static byte[] sig1b = HexFormat.ofDelimiter(":")
84
.parseHex("30:2c:02:14:53:06:3f:7d:ec:48:3c:99:17:9a:2c:a9:4d:e8:00:da:70:fb:35:d7:02:14:92:6a:39:6b:15:63:2f:e7:32:90:35:bf:af:47:55:e7:ff:33:a5:13");
85
86
// data for test 2 (invalid signatures)
87
private final static byte[] data2Raw = {};
88
private final static byte[] data2SHA = HexFormat.ofDelimiter(":")
89
.parseHex("da:39:a3:ee:5e:6b:4b:0d:32:55:bf:ef:95:60:18:90:af:d8:07:09");
90
91
private static void verify(Provider provider, String alg, PublicKey key, byte[] data, byte[] sig, boolean result) throws Exception {
92
Signature s = Signature.getInstance(alg, provider);
93
s.initVerify(key);
94
boolean r;
95
s.update(data);
96
r = s.verify(sig);
97
if (r != result) {
98
throw new Exception("Result mismatch, actual: " + r);
99
}
100
s.update(data);
101
r = s.verify(sig);
102
if (r != result) {
103
throw new Exception("Result mismatch, actual: " + r);
104
}
105
System.out.println("Passed");
106
}
107
108
public static void main(String[] args) throws Exception {
109
long start = System.currentTimeMillis();
110
111
Provider provider = Security.getProvider("SUN");
112
System.out.println("Testing provider " + provider + "...");
113
114
KeyFactory kf = KeyFactory.getInstance("DSA", provider);
115
DSAPrivateKeySpec privSpec = new DSAPrivateKeySpec(x, p, q, g);
116
DSAPublicKeySpec pubSpec = new DSAPublicKeySpec(y, p, q, g);
117
PrivateKey privateKey = kf.generatePrivate(privSpec);
118
PublicKey publicKey = kf.generatePublic(pubSpec);
119
120
// verify known-good and known-bad signatures using SHA1withDSA and RawDSA
121
verify(provider, "SHA1withDSA", publicKey, data1Raw, sig1a, true);
122
verify(provider, "SHA1withDSA", publicKey, data1Raw, sig1b, true);
123
verify(provider, "SHA1withDSA", publicKey, data2Raw, sig1a, false);
124
verify(provider, "SHA1withDSA", publicKey, data2Raw, sig1b, false);
125
126
verify(provider, "RawDSA", publicKey, data1SHA, sig1a, true);
127
verify(provider, "RawDSA", publicKey, data1SHA, sig1b, true);
128
verify(provider, "RawDSA", publicKey, data2SHA, sig1a, false);
129
verify(provider, "RawDSA", publicKey, data2SHA, sig1b, false);
130
131
byte[] data = new byte[2048];
132
new Random().nextBytes(data);
133
134
// sign random data using SHA1withDSA and verify using
135
// SHA1withDSA and RawDSA
136
Signature s = Signature.getInstance("SHA1withDSA", provider);
137
s.initSign(privateKey);
138
s.update(data);
139
byte[] s1 = s.sign();
140
141
s.initVerify(publicKey);
142
s.update(data);
143
if (!s.verify(s1)) {
144
throw new Exception("Sign/verify 1 failed");
145
}
146
147
s = Signature.getInstance("RawDSA", provider);
148
MessageDigest md = MessageDigest.getInstance("SHA-1");
149
byte[] digest = md.digest(data);
150
s.initVerify(publicKey);
151
s.update(digest);
152
if (!s.verify(s1)) {
153
throw new Exception("Sign/verify 2 failed");
154
}
155
156
// sign random data using RawDSA and verify using
157
// SHA1withDSA and RawDSA
158
s.initSign(privateKey);
159
s.update(digest);
160
byte[] s2 = s.sign();
161
162
s.initVerify(publicKey);
163
s.update(digest);
164
if (!s.verify(s2)) {
165
throw new Exception("Sign/verify 3 failed");
166
}
167
168
s = Signature.getInstance("SHA1withDSA", provider);
169
s.initVerify(publicKey);
170
s.update(data);
171
if (!s.verify(s2)) {
172
throw new Exception("Sign/verify 4 failed");
173
}
174
175
// test behavior if data of incorrect length is passed
176
s = Signature.getInstance("RawDSA", provider);
177
s.initSign(privateKey);
178
s.update(new byte[8]);
179
s.update(new byte[64]);
180
try {
181
s.sign();
182
throw new Exception("No error RawDSA signing long data");
183
} catch (SignatureException e) {
184
// expected
185
}
186
187
long stop = System.currentTimeMillis();
188
System.out.println("All tests passed (" + (stop - start) + " ms).");
189
}
190
}
191
192