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/pkcs11/Signature/TestDSA.java
38855 views
1
/*
2
* Copyright (c) 2003, 2016, 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 4856966
27
* @summary basic test of SHA1withDSA and RawDSA signing/verifying
28
* @author Andreas Sterbenz
29
* @library ..
30
* @key randomness
31
* @run main/othervm TestDSA
32
* @run main/othervm TestDSA sm
33
*/
34
35
import java.io.ByteArrayOutputStream;
36
import java.io.IOException;
37
import java.io.StringReader;
38
import java.math.BigInteger;
39
import java.security.KeyFactory;
40
import java.security.MessageDigest;
41
import java.security.PrivateKey;
42
import java.security.Provider;
43
import java.security.PublicKey;
44
import java.security.Signature;
45
import java.security.SignatureException;
46
import java.security.spec.DSAPrivateKeySpec;
47
import java.security.spec.DSAPublicKeySpec;
48
import java.util.Random;
49
50
public class TestDSA extends PKCS11Test {
51
52
// values of the keys we use for the tests
53
54
private final static String ps =
55
"fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669" +
56
"455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b7" +
57
"6b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb" +
58
"83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c7";
59
60
private final static String qs =
61
"9760508f15230bccb292b982a2eb840bf0581cf5";
62
63
private final static String gs =
64
"f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d078267" +
65
"5159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e1" +
66
"3c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243b" +
67
"cca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a";
68
69
private final static String xs =
70
"2952afd9aef9527f9b40d23c8916f7d046028f9d";
71
72
private final static String ys =
73
"b16ddb0f9394c328c983ecf23b20014ace368a1af5728dffbf1162de9ed8ebf6" +
74
"384f323930e091503035caa797e3674221fc16136240b5474799ede2b7b11313" +
75
"7574a9c26bcf900940027b4bcd511ef1d1daf2e69c416aebaf3bdf39f02473b9" +
76
"d963f99414c09d97bb0830d9fbdcf7bb9dad8a2179fcdf296838c4cfab8f4d8f";
77
78
private final static BigInteger p = new BigInteger(ps, 16);
79
private final static BigInteger q = new BigInteger(qs, 16);
80
private final static BigInteger g = new BigInteger(gs, 16);
81
private final static BigInteger x = new BigInteger(xs, 16);
82
private final static BigInteger y = new BigInteger(ys, 16);
83
84
// data for test 1, original and SHA-1 hashed
85
private final static byte[] data1Raw = b("0102030405060708090a0b0c0d0e0f10111213");
86
private final static byte[] data1SHA = b("00:e2:5f:c9:1c:8f:d6:8c:6a:dc:c6:bd:f0:46:60:5e:a2:cd:8d:ad");
87
88
// valid signatures of data1. sig1b uses incorrect ASN.1 encoding,
89
// which we want to accept anyway for compatibility
90
private final static byte[] sig1a = b("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");
91
private final static byte[] sig1b = b("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");
92
93
// data for test 2 (invalid signatures)
94
private final static byte[] data2Raw = {};
95
private final static byte[] data2SHA = b("da:39:a3:ee:5e:6b:4b:0d:32:55:bf:ef:95:60:18:90:af:d8:07:09");
96
97
private static void verify(Provider provider, String alg, PublicKey key, byte[] data, byte[] sig, boolean result) throws Exception {
98
Signature s = Signature.getInstance(alg, provider);
99
s.initVerify(key);
100
boolean r;
101
s.update(data);
102
r = s.verify(sig);
103
if (r != result) {
104
throw new Exception("Result mismatch, actual: " + r);
105
}
106
s.update(data);
107
r = s.verify(sig);
108
if (r != result) {
109
throw new Exception("Result mismatch, actual: " + r);
110
}
111
System.out.println("Passed");
112
}
113
114
public static void main(String[] args) throws Exception {
115
main(new TestDSA(), args);
116
}
117
118
@Override
119
public void main(Provider provider) throws Exception {
120
long start = System.currentTimeMillis();
121
122
System.out.println("Testing provider " + provider + "...");
123
124
if (provider.getService("Signature", "SHA1withDSA") == null) {
125
System.out.println("DSA not supported, skipping");
126
return;
127
}
128
129
KeyFactory kf = KeyFactory.getInstance("DSA", provider);
130
DSAPrivateKeySpec privSpec = new DSAPrivateKeySpec(x, p, q, g);
131
DSAPublicKeySpec pubSpec = new DSAPublicKeySpec(y, p, q, g);
132
PrivateKey privateKey = kf.generatePrivate(privSpec);
133
PublicKey publicKey = kf.generatePublic(pubSpec);
134
135
// verify known-good and known-bad signatures using SHA1withDSA and RawDSA
136
verify(provider, "SHA1withDSA", publicKey, data1Raw, sig1a, true);
137
verify(provider, "SHA1withDSA", publicKey, data1Raw, sig1b, true);
138
verify(provider, "SHA1withDSA", publicKey, data2Raw, sig1a, false);
139
verify(provider, "SHA1withDSA", publicKey, data2Raw, sig1b, false);
140
141
verify(provider, "RawDSA", publicKey, data1SHA, sig1a, true);
142
verify(provider, "RawDSA", publicKey, data1SHA, sig1b, true);
143
verify(provider, "RawDSA", publicKey, data2SHA, sig1a, false);
144
verify(provider, "RawDSA", publicKey, data2SHA, sig1b, false);
145
146
testSigning(provider, privateKey, publicKey);
147
148
long stop = System.currentTimeMillis();
149
System.out.println("All tests passed (" + (stop - start) + " ms).");
150
}
151
152
private void testSigning(Provider provider, PrivateKey privateKey,
153
PublicKey publicKey) throws Exception {
154
byte[] data = new byte[2048];
155
new Random().nextBytes(data);
156
157
// sign random data using SHA1withDSA and verify using
158
// SHA1withDSA and RawDSA
159
Signature s = Signature.getInstance("SHA1withDSA", provider);
160
s.initSign(privateKey);
161
s.update(data);
162
byte[] s1 = s.sign();
163
164
s.initVerify(publicKey);
165
s.update(data);
166
if (!s.verify(s1)) {
167
throw new Exception("Sign/verify 1 failed");
168
}
169
170
s = Signature.getInstance("RawDSA", provider);
171
MessageDigest md = MessageDigest.getInstance("SHA-1");
172
byte[] digest = md.digest(data);
173
s.initVerify(publicKey);
174
s.update(digest);
175
if (!s.verify(s1)) {
176
throw new Exception("Sign/verify 2 failed");
177
}
178
179
// sign random data using RawDSA and verify using
180
// SHA1withDSA and RawDSA
181
s.initSign(privateKey);
182
s.update(digest);
183
byte[] s2 = s.sign();
184
185
s.initVerify(publicKey);
186
s.update(digest);
187
if (!s.verify(s2)) {
188
throw new Exception("Sign/verify 3 failed");
189
}
190
191
s = Signature.getInstance("SHA1withDSA", provider);
192
s.initVerify(publicKey);
193
s.update(data);
194
if (!s.verify(s2)) {
195
throw new Exception("Sign/verify 4 failed");
196
}
197
198
// test behavior if data of incorrect length is passed
199
s = Signature.getInstance("RawDSA", provider);
200
s.initSign(privateKey);
201
s.update(new byte[8]);
202
s.update(new byte[64]);
203
try {
204
s.sign();
205
throw new Exception("No error RawDSA signing long data");
206
} catch (SignatureException e) {
207
// expected
208
}
209
}
210
211
private final static char[] hexDigits = "0123456789abcdef".toCharArray();
212
213
public static String toString(byte[] b) {
214
StringBuffer sb = new StringBuffer(b.length * 3);
215
for (int i = 0; i < b.length; i++) {
216
int k = b[i] & 0xff;
217
if (i != 0) {
218
sb.append(':');
219
}
220
sb.append(hexDigits[k >>> 4]);
221
sb.append(hexDigits[k & 0xf]);
222
}
223
return sb.toString();
224
}
225
226
public static byte[] parse(String s) {
227
try {
228
int n = s.length();
229
ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);
230
StringReader r = new StringReader(s);
231
while (true) {
232
int b1 = nextNibble(r);
233
if (b1 < 0) {
234
break;
235
}
236
int b2 = nextNibble(r);
237
if (b2 < 0) {
238
throw new RuntimeException("Invalid string " + s);
239
}
240
int b = (b1 << 4) | b2;
241
out.write(b);
242
}
243
return out.toByteArray();
244
} catch (IOException e) {
245
throw new RuntimeException(e);
246
}
247
}
248
249
public static byte[] b(String s) {
250
return parse(s);
251
}
252
253
private static int nextNibble(StringReader r) throws IOException {
254
while (true) {
255
int ch = r.read();
256
if (ch == -1) {
257
return -1;
258
} else if ((ch >= '0') && (ch <= '9')) {
259
return ch - '0';
260
} else if ((ch >= 'a') && (ch <= 'f')) {
261
return ch - 'a' + 10;
262
} else if ((ch >= 'A') && (ch <= 'F')) {
263
return ch - 'A' + 10;
264
}
265
}
266
}
267
268
}
269
270