Path: blob/master/test/jdk/sun/security/pkcs11/Signature/TestDSA.java
41153 views
/*1* Copyright (c) 2003, 2020, 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 /test/lib ..29* @key randomness30* @modules jdk.crypto.cryptoki31* @run main/othervm TestDSA32* @run main/othervm -Djava.security.manager=allow TestDSA sm33*/3435import java.io.ByteArrayOutputStream;36import java.io.IOException;37import java.io.StringReader;38import java.math.BigInteger;39import java.security.KeyFactory;40import java.security.MessageDigest;41import java.security.PrivateKey;42import java.security.Provider;43import java.security.PublicKey;44import java.security.Signature;45import java.security.SignatureException;46import java.security.spec.DSAPrivateKeySpec;47import java.security.spec.DSAPublicKeySpec;48import java.util.Random;4950public class TestDSA extends PKCS11Test {5152// values of the keys we use for the tests5354private final static String ps =55"fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669" +56"455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b7" +57"6b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb" +58"83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c7";5960private final static String qs =61"9760508f15230bccb292b982a2eb840bf0581cf5";6263private final static String gs =64"f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d078267" +65"5159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e1" +66"3c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243b" +67"cca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a";6869private final static String xs =70"2952afd9aef9527f9b40d23c8916f7d046028f9d";7172private final static String ys =73"b16ddb0f9394c328c983ecf23b20014ace368a1af5728dffbf1162de9ed8ebf6" +74"384f323930e091503035caa797e3674221fc16136240b5474799ede2b7b11313" +75"7574a9c26bcf900940027b4bcd511ef1d1daf2e69c416aebaf3bdf39f02473b9" +76"d963f99414c09d97bb0830d9fbdcf7bb9dad8a2179fcdf296838c4cfab8f4d8f";7778private final static BigInteger p = new BigInteger(ps, 16);79private final static BigInteger q = new BigInteger(qs, 16);80private final static BigInteger g = new BigInteger(gs, 16);81private final static BigInteger x = new BigInteger(xs, 16);82private final static BigInteger y = new BigInteger(ys, 16);8384// data for test 1, original and SHA-1 hashed85private final static byte[] data1Raw = b("0102030405060708090a0b0c0d0e0f10111213");86private 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");8788// valid signatures of data1. sig1b uses incorrect ASN.1 encoding,89// which we want to accept anyway for compatibility90private 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");91private 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");9293// data for test 2 (invalid signatures)94private final static byte[] data2Raw = {};95private 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");9697private static void verify(Provider provider, String alg, PublicKey key, byte[] data, byte[] sig, boolean result) throws Exception {98Signature s = Signature.getInstance(alg, provider);99s.initVerify(key);100boolean r;101s.update(data);102r = s.verify(sig);103if (r != result) {104throw new Exception("Result mismatch, actual: " + r);105}106s.update(data);107r = s.verify(sig);108if (r != result) {109throw new Exception("Result mismatch, actual: " + r);110}111System.out.println("Passed");112}113114public static void main(String[] args) throws Exception {115main(new TestDSA(), args);116}117118@Override119public void main(Provider provider) throws Exception {120long start = System.currentTimeMillis();121122System.out.println("Testing provider " + provider + "...");123124if (provider.getService("Signature", "SHA1withDSA") == null) {125System.out.println("DSA not supported, skipping");126return;127}128129KeyFactory kf = KeyFactory.getInstance("DSA", provider);130DSAPrivateKeySpec privSpec = new DSAPrivateKeySpec(x, p, q, g);131DSAPublicKeySpec pubSpec = new DSAPublicKeySpec(y, p, q, g);132PrivateKey privateKey = kf.generatePrivate(privSpec);133PublicKey publicKey = kf.generatePublic(pubSpec);134135// verify known-good and known-bad signatures using SHA1withDSA and RawDSA136verify(provider, "SHA1withDSA", publicKey, data1Raw, sig1a, true);137verify(provider, "SHA1withDSA", publicKey, data1Raw, sig1b, true);138verify(provider, "SHA1withDSA", publicKey, data2Raw, sig1a, false);139verify(provider, "SHA1withDSA", publicKey, data2Raw, sig1b, false);140141verify(provider, "RawDSA", publicKey, data1SHA, sig1a, true);142verify(provider, "RawDSA", publicKey, data1SHA, sig1b, true);143verify(provider, "RawDSA", publicKey, data2SHA, sig1a, false);144verify(provider, "RawDSA", publicKey, data2SHA, sig1b, false);145146testSigning(provider, privateKey, publicKey);147148long stop = System.currentTimeMillis();149System.out.println("All tests passed (" + (stop - start) + " ms).");150}151152private void testSigning(Provider provider, PrivateKey privateKey,153PublicKey publicKey) throws Exception {154byte[] data = new byte[2048];155new Random().nextBytes(data);156157// sign random data using SHA1withDSA and verify using158// SHA1withDSA and RawDSA159Signature s = Signature.getInstance("SHA1withDSA", provider);160s.initSign(privateKey);161s.update(data);162byte[] s1 = s.sign();163164s.initVerify(publicKey);165s.update(data);166if (!s.verify(s1)) {167throw new Exception("Sign/verify 1 failed");168}169170s = Signature.getInstance("RawDSA", provider);171MessageDigest md = MessageDigest.getInstance("SHA-1");172byte[] digest = md.digest(data);173s.initVerify(publicKey);174s.update(digest);175if (!s.verify(s1)) {176throw new Exception("Sign/verify 2 failed");177}178179// sign random data using RawDSA and verify using180// SHA1withDSA and RawDSA181s.initSign(privateKey);182s.update(digest);183byte[] s2 = s.sign();184185s.initVerify(publicKey);186s.update(digest);187if (!s.verify(s2)) {188throw new Exception("Sign/verify 3 failed");189}190191s = Signature.getInstance("SHA1withDSA", provider);192s.initVerify(publicKey);193s.update(data);194if (!s.verify(s2)) {195throw new Exception("Sign/verify 4 failed");196}197198// test behavior if data of incorrect length is passed199s = Signature.getInstance("RawDSA", provider);200s.initSign(privateKey);201s.update(new byte[8]);202s.update(new byte[64]);203try {204s.sign();205throw new Exception("No error RawDSA signing long data");206} catch (SignatureException e) {207// expected208}209}210211private final static char[] hexDigits = "0123456789abcdef".toCharArray();212213public static String toString(byte[] b) {214StringBuffer sb = new StringBuffer(b.length * 3);215for (int i = 0; i < b.length; i++) {216int k = b[i] & 0xff;217if (i != 0) {218sb.append(':');219}220sb.append(hexDigits[k >>> 4]);221sb.append(hexDigits[k & 0xf]);222}223return sb.toString();224}225226public static byte[] parse(String s) {227try {228int n = s.length();229ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);230StringReader r = new StringReader(s);231while (true) {232int b1 = nextNibble(r);233if (b1 < 0) {234break;235}236int b2 = nextNibble(r);237if (b2 < 0) {238throw new RuntimeException("Invalid string " + s);239}240int b = (b1 << 4) | b2;241out.write(b);242}243return out.toByteArray();244} catch (IOException e) {245throw new RuntimeException(e);246}247}248249public static byte[] b(String s) {250return parse(s);251}252253private static int nextNibble(StringReader r) throws IOException {254while (true) {255int ch = r.read();256if (ch == -1) {257return -1;258} else if ((ch >= '0') && (ch <= '9')) {259return ch - '0';260} else if ((ch >= 'a') && (ch <= 'f')) {261return ch - 'a' + 10;262} else if ((ch >= 'A') && (ch <= 'F')) {263return ch - 'A' + 10;264}265}266}267268}269270271