1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
/*
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4856966
* @summary basic test of SHA1withDSA and RawDSA signing/verifying
* @author Andreas Sterbenz
* @library /test/lib ..
* @key randomness
* @modules jdk.crypto.cryptoki
* @run main/othervm TestDSA
* @run main/othervm TestDSA sm
*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.DSAPrivateKeySpec;
import java.security.spec.DSAPublicKeySpec;
import java.util.Random;
public class TestDSA extends PKCS11Test {
// values of the keys we use for the tests
private final static String ps =
"fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669" +
"455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b7" +
"6b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb" +
"83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c7";
private final static String qs =
"9760508f15230bccb292b982a2eb840bf0581cf5";
private final static String gs =
"f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d078267" +
"5159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e1" +
"3c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243b" +
"cca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a";
private final static String xs =
"2952afd9aef9527f9b40d23c8916f7d046028f9d";
private final static String ys =
"b16ddb0f9394c328c983ecf23b20014ace368a1af5728dffbf1162de9ed8ebf6" +
"384f323930e091503035caa797e3674221fc16136240b5474799ede2b7b11313" +
"7574a9c26bcf900940027b4bcd511ef1d1daf2e69c416aebaf3bdf39f02473b9" +
"d963f99414c09d97bb0830d9fbdcf7bb9dad8a2179fcdf296838c4cfab8f4d8f";
private final static BigInteger p = new BigInteger(ps, 16);
private final static BigInteger q = new BigInteger(qs, 16);
private final static BigInteger g = new BigInteger(gs, 16);
private final static BigInteger x = new BigInteger(xs, 16);
private final static BigInteger y = new BigInteger(ys, 16);
// data for test 1, original and SHA-1 hashed
private final static byte[] data1Raw = b("0102030405060708090a0b0c0d0e0f10111213");
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");
// valid signatures of data1. sig1b uses incorrect ASN.1 encoding,
// which we want to accept anyway for compatibility
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");
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");
// data for test 2 (invalid signatures)
private final static byte[] data2Raw = {};
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");
private static void verify(Provider provider, String alg, PublicKey key, byte[] data, byte[] sig, boolean result) throws Exception {
Signature s = Signature.getInstance(alg, provider);
s.initVerify(key);
boolean r;
s.update(data);
r = s.verify(sig);
if (r != result) {
throw new Exception("Result mismatch, actual: " + r);
}
s.update(data);
r = s.verify(sig);
if (r != result) {
throw new Exception("Result mismatch, actual: " + r);
}
System.out.println("Passed");
}
public static void main(String[] args) throws Exception {
main(new TestDSA(), args);
}
@Override
public void main(Provider provider) throws Exception {
long start = System.currentTimeMillis();
System.out.println("Testing provider " + provider + "...");
/*
* Use Solaris SPARC 11.2 or later to avoid an intermittent failure
* when running SunPKCS11-Solaris (8044554)
*/
if (provider.getName().equals("SunPKCS11-Solaris") &&
props.getProperty("os.name").equals("SunOS") &&
props.getProperty("os.arch").equals("sparcv9") &&
props.getProperty("os.version").compareTo("5.11") <= 0 &&
getDistro().compareTo("11.2") < 0) {
System.out.println("SunPKCS11-Solaris provider requires " +
"Solaris SPARC 11.2 or later, skipping");
return;
}
if (provider.getService("Signature", "SHA1withDSA") == null) {
System.out.println("DSA not supported, skipping");
return;
}
KeyFactory kf = KeyFactory.getInstance("DSA", provider);
DSAPrivateKeySpec privSpec = new DSAPrivateKeySpec(x, p, q, g);
DSAPublicKeySpec pubSpec = new DSAPublicKeySpec(y, p, q, g);
PrivateKey privateKey = kf.generatePrivate(privSpec);
PublicKey publicKey = kf.generatePublic(pubSpec);
// verify known-good and known-bad signatures using SHA1withDSA and RawDSA
verify(provider, "SHA1withDSA", publicKey, data1Raw, sig1a, true);
verify(provider, "SHA1withDSA", publicKey, data1Raw, sig1b, true);
verify(provider, "SHA1withDSA", publicKey, data2Raw, sig1a, false);
verify(provider, "SHA1withDSA", publicKey, data2Raw, sig1b, false);
verify(provider, "RawDSA", publicKey, data1SHA, sig1a, true);
verify(provider, "RawDSA", publicKey, data1SHA, sig1b, true);
verify(provider, "RawDSA", publicKey, data2SHA, sig1a, false);
verify(provider, "RawDSA", publicKey, data2SHA, sig1b, false);
testSigning(provider, privateKey, publicKey);
long stop = System.currentTimeMillis();
System.out.println("All tests passed (" + (stop - start) + " ms).");
}
private void testSigning(Provider provider, PrivateKey privateKey,
PublicKey publicKey) throws Exception {
byte[] data = new byte[2048];
new Random().nextBytes(data);
// sign random data using SHA1withDSA and verify using
// SHA1withDSA and RawDSA
Signature s = Signature.getInstance("SHA1withDSA", provider);
s.initSign(privateKey);
s.update(data);
byte[] s1 = s.sign();
s.initVerify(publicKey);
s.update(data);
if (!s.verify(s1)) {
throw new Exception("Sign/verify 1 failed");
}
s = Signature.getInstance("RawDSA", provider);
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] digest = md.digest(data);
s.initVerify(publicKey);
s.update(digest);
if (!s.verify(s1)) {
throw new Exception("Sign/verify 2 failed");
}
// sign random data using RawDSA and verify using
// SHA1withDSA and RawDSA
s.initSign(privateKey);
s.update(digest);
byte[] s2 = s.sign();
s.initVerify(publicKey);
s.update(digest);
if (!s.verify(s2)) {
throw new Exception("Sign/verify 3 failed");
}
s = Signature.getInstance("SHA1withDSA", provider);
s.initVerify(publicKey);
s.update(data);
if (!s.verify(s2)) {
throw new Exception("Sign/verify 4 failed");
}
// test behavior if data of incorrect length is passed
s = Signature.getInstance("RawDSA", provider);
s.initSign(privateKey);
s.update(new byte[8]);
s.update(new byte[64]);
try {
s.sign();
throw new Exception("No error RawDSA signing long data");
} catch (SignatureException e) {
// expected
}
}
private final static char[] hexDigits = "0123456789abcdef".toCharArray();
public static String toString(byte[] b) {
StringBuffer sb = new StringBuffer(b.length * 3);
for (int i = 0; i < b.length; i++) {
int k = b[i] & 0xff;
if (i != 0) {
sb.append(':');
}
sb.append(hexDigits[k >>> 4]);
sb.append(hexDigits[k & 0xf]);
}
return sb.toString();
}
public static byte[] parse(String s) {
try {
int n = s.length();
ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);
StringReader r = new StringReader(s);
while (true) {
int b1 = nextNibble(r);
if (b1 < 0) {
break;
}
int b2 = nextNibble(r);
if (b2 < 0) {
throw new RuntimeException("Invalid string " + s);
}
int b = (b1 << 4) | b2;
out.write(b);
}
return out.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static byte[] b(String s) {
return parse(s);
}
private static int nextNibble(StringReader r) throws IOException {
while (true) {
int ch = r.read();
if (ch == -1) {
return -1;
} else if ((ch >= '0') && (ch <= '9')) {
return ch - '0';
} else if ((ch >= 'a') && (ch <= 'f')) {
return ch - 'a' + 10;
} else if ((ch >= 'A') && (ch <= 'F')) {
return ch - 'A' + 10;
}
}
}
}
|