Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Signature/TestDSA.java
38855 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 485696626* @summary basic test of SHA1withDSA and RawDSA signing/verifying27* @author Andreas Sterbenz28* @library ..29* @key randomness30* @run main/othervm TestDSA31* @run main/othervm TestDSA sm32*/3334import java.io.ByteArrayOutputStream;35import java.io.IOException;36import java.io.StringReader;37import java.math.BigInteger;38import java.security.KeyFactory;39import java.security.MessageDigest;40import java.security.PrivateKey;41import java.security.Provider;42import java.security.PublicKey;43import java.security.Signature;44import java.security.SignatureException;45import java.security.spec.DSAPrivateKeySpec;46import java.security.spec.DSAPublicKeySpec;47import java.util.Random;4849public class TestDSA extends PKCS11Test {5051// values of the keys we use for the tests5253private final static String ps =54"fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669" +55"455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b7" +56"6b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb" +57"83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c7";5859private final static String qs =60"9760508f15230bccb292b982a2eb840bf0581cf5";6162private final static String gs =63"f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d078267" +64"5159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e1" +65"3c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243b" +66"cca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a";6768private final static String xs =69"2952afd9aef9527f9b40d23c8916f7d046028f9d";7071private final static String ys =72"b16ddb0f9394c328c983ecf23b20014ace368a1af5728dffbf1162de9ed8ebf6" +73"384f323930e091503035caa797e3674221fc16136240b5474799ede2b7b11313" +74"7574a9c26bcf900940027b4bcd511ef1d1daf2e69c416aebaf3bdf39f02473b9" +75"d963f99414c09d97bb0830d9fbdcf7bb9dad8a2179fcdf296838c4cfab8f4d8f";7677private final static BigInteger p = new BigInteger(ps, 16);78private final static BigInteger q = new BigInteger(qs, 16);79private final static BigInteger g = new BigInteger(gs, 16);80private final static BigInteger x = new BigInteger(xs, 16);81private final static BigInteger y = new BigInteger(ys, 16);8283// data for test 1, original and SHA-1 hashed84private final static byte[] data1Raw = b("0102030405060708090a0b0c0d0e0f10111213");85private 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");8687// valid signatures of data1. sig1b uses incorrect ASN.1 encoding,88// which we want to accept anyway for compatibility89private 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");90private 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");9192// data for test 2 (invalid signatures)93private final static byte[] data2Raw = {};94private 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");9596private static void verify(Provider provider, String alg, PublicKey key, byte[] data, byte[] sig, boolean result) throws Exception {97Signature s = Signature.getInstance(alg, provider);98s.initVerify(key);99boolean r;100s.update(data);101r = s.verify(sig);102if (r != result) {103throw new Exception("Result mismatch, actual: " + r);104}105s.update(data);106r = s.verify(sig);107if (r != result) {108throw new Exception("Result mismatch, actual: " + r);109}110System.out.println("Passed");111}112113public static void main(String[] args) throws Exception {114main(new TestDSA(), args);115}116117@Override118public void main(Provider provider) throws Exception {119long start = System.currentTimeMillis();120121System.out.println("Testing provider " + provider + "...");122123if (provider.getService("Signature", "SHA1withDSA") == null) {124System.out.println("DSA not supported, skipping");125return;126}127128KeyFactory kf = KeyFactory.getInstance("DSA", provider);129DSAPrivateKeySpec privSpec = new DSAPrivateKeySpec(x, p, q, g);130DSAPublicKeySpec pubSpec = new DSAPublicKeySpec(y, p, q, g);131PrivateKey privateKey = kf.generatePrivate(privSpec);132PublicKey publicKey = kf.generatePublic(pubSpec);133134// verify known-good and known-bad signatures using SHA1withDSA and RawDSA135verify(provider, "SHA1withDSA", publicKey, data1Raw, sig1a, true);136verify(provider, "SHA1withDSA", publicKey, data1Raw, sig1b, true);137verify(provider, "SHA1withDSA", publicKey, data2Raw, sig1a, false);138verify(provider, "SHA1withDSA", publicKey, data2Raw, sig1b, false);139140verify(provider, "RawDSA", publicKey, data1SHA, sig1a, true);141verify(provider, "RawDSA", publicKey, data1SHA, sig1b, true);142verify(provider, "RawDSA", publicKey, data2SHA, sig1a, false);143verify(provider, "RawDSA", publicKey, data2SHA, sig1b, false);144145testSigning(provider, privateKey, publicKey);146147long stop = System.currentTimeMillis();148System.out.println("All tests passed (" + (stop - start) + " ms).");149}150151private void testSigning(Provider provider, PrivateKey privateKey,152PublicKey publicKey) throws Exception {153byte[] data = new byte[2048];154new Random().nextBytes(data);155156// sign random data using SHA1withDSA and verify using157// SHA1withDSA and RawDSA158Signature s = Signature.getInstance("SHA1withDSA", provider);159s.initSign(privateKey);160s.update(data);161byte[] s1 = s.sign();162163s.initVerify(publicKey);164s.update(data);165if (!s.verify(s1)) {166throw new Exception("Sign/verify 1 failed");167}168169s = Signature.getInstance("RawDSA", provider);170MessageDigest md = MessageDigest.getInstance("SHA-1");171byte[] digest = md.digest(data);172s.initVerify(publicKey);173s.update(digest);174if (!s.verify(s1)) {175throw new Exception("Sign/verify 2 failed");176}177178// sign random data using RawDSA and verify using179// SHA1withDSA and RawDSA180s.initSign(privateKey);181s.update(digest);182byte[] s2 = s.sign();183184s.initVerify(publicKey);185s.update(digest);186if (!s.verify(s2)) {187throw new Exception("Sign/verify 3 failed");188}189190s = Signature.getInstance("SHA1withDSA", provider);191s.initVerify(publicKey);192s.update(data);193if (!s.verify(s2)) {194throw new Exception("Sign/verify 4 failed");195}196197// test behavior if data of incorrect length is passed198s = Signature.getInstance("RawDSA", provider);199s.initSign(privateKey);200s.update(new byte[8]);201s.update(new byte[64]);202try {203s.sign();204throw new Exception("No error RawDSA signing long data");205} catch (SignatureException e) {206// expected207}208}209210private final static char[] hexDigits = "0123456789abcdef".toCharArray();211212public static String toString(byte[] b) {213StringBuffer sb = new StringBuffer(b.length * 3);214for (int i = 0; i < b.length; i++) {215int k = b[i] & 0xff;216if (i != 0) {217sb.append(':');218}219sb.append(hexDigits[k >>> 4]);220sb.append(hexDigits[k & 0xf]);221}222return sb.toString();223}224225public static byte[] parse(String s) {226try {227int n = s.length();228ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);229StringReader r = new StringReader(s);230while (true) {231int b1 = nextNibble(r);232if (b1 < 0) {233break;234}235int b2 = nextNibble(r);236if (b2 < 0) {237throw new RuntimeException("Invalid string " + s);238}239int b = (b1 << 4) | b2;240out.write(b);241}242return out.toByteArray();243} catch (IOException e) {244throw new RuntimeException(e);245}246}247248public static byte[] b(String s) {249return parse(s);250}251252private static int nextNibble(StringReader r) throws IOException {253while (true) {254int ch = r.read();255if (ch == -1) {256return -1;257} else if ((ch >= '0') && (ch <= '9')) {258return ch - '0';259} else if ((ch >= 'a') && (ch <= 'f')) {260return ch - 'a' + 10;261} else if ((ch >= 'A') && (ch <= 'F')) {262return ch - 'A' + 10;263}264}265}266267}268269270