0N/A/*
2362N/A * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4856966
0N/A * @summary basic test of SHA1withDSA and RawDSA signing/verifying
0N/A * @author Andreas Sterbenz
0N/A * @library ..
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/Aimport java.math.BigInteger;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.security.spec.*;
0N/A
0N/Apublic class TestDSA extends PKCS11Test {
0N/A
0N/A // values of the keys we use for the tests
0N/A
0N/A private final static String ps =
0N/A "fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669" +
0N/A "455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b7" +
0N/A "6b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb" +
0N/A "83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c7";
0N/A
0N/A private final static String qs =
0N/A "9760508f15230bccb292b982a2eb840bf0581cf5";
0N/A
0N/A private final static String gs =
0N/A "f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d078267" +
0N/A "5159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e1" +
0N/A "3c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243b" +
0N/A "cca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a";
0N/A
0N/A private final static String xs =
0N/A "2952afd9aef9527f9b40d23c8916f7d046028f9d";
0N/A
0N/A private final static String ys =
0N/A "b16ddb0f9394c328c983ecf23b20014ace368a1af5728dffbf1162de9ed8ebf6" +
0N/A "384f323930e091503035caa797e3674221fc16136240b5474799ede2b7b11313" +
0N/A "7574a9c26bcf900940027b4bcd511ef1d1daf2e69c416aebaf3bdf39f02473b9" +
0N/A "d963f99414c09d97bb0830d9fbdcf7bb9dad8a2179fcdf296838c4cfab8f4d8f";
0N/A
0N/A private final static BigInteger p = new BigInteger(ps, 16);
0N/A private final static BigInteger q = new BigInteger(qs, 16);
0N/A private final static BigInteger g = new BigInteger(gs, 16);
0N/A private final static BigInteger x = new BigInteger(xs, 16);
0N/A private final static BigInteger y = new BigInteger(ys, 16);
0N/A
0N/A // data for test 1, original and SHA-1 hashed
0N/A private final static byte[] data1Raw = b("0102030405060708090a0b0c0d0e0f10111213");
0N/A 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");
0N/A
0N/A // valid signatures of data1. sig1b uses incorrect ASN.1 encoding,
0N/A // which we want to accept anyway for compatibility
0N/A 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");
0N/A 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");
0N/A
0N/A // data for test 2 (invalid signatures)
0N/A private final static byte[] data2Raw = {};
0N/A 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");
0N/A
0N/A private static void verify(Provider provider, String alg, PublicKey key, byte[] data, byte[] sig, boolean result) throws Exception {
0N/A Signature s = Signature.getInstance(alg, provider);
0N/A s.initVerify(key);
0N/A boolean r;
0N/A s.update(data);
0N/A r = s.verify(sig);
0N/A if (r != result) {
0N/A throw new Exception("Result mismatch, actual: " + r);
0N/A }
0N/A s.update(data);
0N/A r = s.verify(sig);
0N/A if (r != result) {
0N/A throw new Exception("Result mismatch, actual: " + r);
0N/A }
0N/A System.out.println("Passed");
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A main(new TestDSA());
0N/A }
0N/A
0N/A public void main(Provider provider) throws Exception {
0N/A long start = System.currentTimeMillis();
0N/A
0N/A System.out.println("Testing provider " + provider + "...");
0N/A
0N/A if (provider.getService("Signature", "SHA1withDSA") == null) {
0N/A System.out.println("DSA not supported, skipping");
0N/A return;
0N/A }
0N/A
0N/A KeyFactory kf = KeyFactory.getInstance("DSA", provider);
0N/A DSAPrivateKeySpec privSpec = new DSAPrivateKeySpec(x, p, q, g);
0N/A DSAPublicKeySpec pubSpec = new DSAPublicKeySpec(y, p, q, g);
0N/A PrivateKey privateKey = kf.generatePrivate(privSpec);
0N/A PublicKey publicKey = kf.generatePublic(pubSpec);
0N/A
0N/A // verify known-good and known-bad signatures using SHA1withDSA and RawDSA
0N/A verify(provider, "SHA1withDSA", publicKey, data1Raw, sig1a, true);
0N/A verify(provider, "SHA1withDSA", publicKey, data1Raw, sig1b, true);
0N/A verify(provider, "SHA1withDSA", publicKey, data2Raw, sig1a, false);
0N/A verify(provider, "SHA1withDSA", publicKey, data2Raw, sig1b, false);
0N/A
0N/A verify(provider, "RawDSA", publicKey, data1SHA, sig1a, true);
0N/A verify(provider, "RawDSA", publicKey, data1SHA, sig1b, true);
0N/A verify(provider, "RawDSA", publicKey, data2SHA, sig1a, false);
0N/A verify(provider, "RawDSA", publicKey, data2SHA, sig1b, false);
0N/A
0N/A testSigning(provider, privateKey, publicKey);
0N/A
0N/A long stop = System.currentTimeMillis();
0N/A System.out.println("All tests passed (" + (stop - start) + " ms).");
0N/A }
0N/A
0N/A private void testSigning(Provider provider, PrivateKey privateKey,
0N/A PublicKey publicKey) throws Exception {
0N/A byte[] data = new byte[2048];
0N/A new Random().nextBytes(data);
0N/A
0N/A // sign random data using SHA1withDSA and verify using
0N/A // SHA1withDSA and RawDSA
0N/A Signature s = Signature.getInstance("SHA1withDSA", provider);
0N/A s.initSign(privateKey);
0N/A s.update(data);
0N/A byte[] s1 = s.sign();
0N/A
0N/A s.initVerify(publicKey);
0N/A s.update(data);
0N/A if (!s.verify(s1)) {
0N/A throw new Exception("Sign/verify 1 failed");
0N/A }
0N/A
0N/A s = Signature.getInstance("RawDSA", provider);
0N/A MessageDigest md = MessageDigest.getInstance("SHA-1");
0N/A byte[] digest = md.digest(data);
0N/A s.initVerify(publicKey);
0N/A s.update(digest);
0N/A if (!s.verify(s1)) {
0N/A throw new Exception("Sign/verify 2 failed");
0N/A }
0N/A
0N/A // sign random data using RawDSA and verify using
0N/A // SHA1withDSA and RawDSA
0N/A s.initSign(privateKey);
0N/A s.update(digest);
0N/A byte[] s2 = s.sign();
0N/A
0N/A s.initVerify(publicKey);
0N/A s.update(digest);
0N/A if (!s.verify(s2)) {
0N/A throw new Exception("Sign/verify 3 failed");
0N/A }
0N/A
0N/A s = Signature.getInstance("SHA1withDSA", provider);
0N/A s.initVerify(publicKey);
0N/A s.update(data);
0N/A if (!s.verify(s2)) {
0N/A throw new Exception("Sign/verify 4 failed");
0N/A }
0N/A
0N/A // test behavior if data of incorrect length is passed
0N/A s = Signature.getInstance("RawDSA", provider);
0N/A s.initSign(privateKey);
0N/A s.update(new byte[8]);
0N/A s.update(new byte[64]);
0N/A try {
0N/A s.sign();
0N/A throw new Exception("No error RawDSA signing long data");
0N/A } catch (SignatureException e) {
0N/A // expected
0N/A }
0N/A }
0N/A
0N/A private final static char[] hexDigits = "0123456789abcdef".toCharArray();
0N/A
0N/A public static String toString(byte[] b) {
0N/A StringBuffer sb = new StringBuffer(b.length * 3);
0N/A for (int i = 0; i < b.length; i++) {
0N/A int k = b[i] & 0xff;
0N/A if (i != 0) {
0N/A sb.append(':');
0N/A }
0N/A sb.append(hexDigits[k >>> 4]);
0N/A sb.append(hexDigits[k & 0xf]);
0N/A }
0N/A return sb.toString();
0N/A }
0N/A
0N/A public static byte[] parse(String s) {
0N/A try {
0N/A int n = s.length();
0N/A ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);
0N/A StringReader r = new StringReader(s);
0N/A while (true) {
0N/A int b1 = nextNibble(r);
0N/A if (b1 < 0) {
0N/A break;
0N/A }
0N/A int b2 = nextNibble(r);
0N/A if (b2 < 0) {
0N/A throw new RuntimeException("Invalid string " + s);
0N/A }
0N/A int b = (b1 << 4) | b2;
0N/A out.write(b);
0N/A }
0N/A return out.toByteArray();
0N/A } catch (IOException e) {
0N/A throw new RuntimeException(e);
0N/A }
0N/A }
0N/A
0N/A public static byte[] b(String s) {
0N/A return parse(s);
0N/A }
0N/A
0N/A private static int nextNibble(StringReader r) throws IOException {
0N/A while (true) {
0N/A int ch = r.read();
0N/A if (ch == -1) {
0N/A return -1;
0N/A } else if ((ch >= '0') && (ch <= '9')) {
0N/A return ch - '0';
0N/A } else if ((ch >= 'a') && (ch <= 'f')) {
0N/A return ch - 'a' + 10;
0N/A } else if ((ch >= 'A') && (ch <= 'F')) {
0N/A return ch - 'A' + 10;
0N/A }
0N/A }
0N/A }
0N/A
0N/A}