Path: blob/master/test/jdk/sun/security/provider/DSA/TestDSA.java
41154 views
/*1* Copyright (c) 2003, 2021, 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 4815057 483927726* @summary basic test of SHA1withDSA and RawDSA signing/verifying27* @author Andreas Sterbenz28* @key randomness29*/3031import java.io.*;32import java.util.*;33import java.math.BigInteger;3435import java.security.*;36import java.security.spec.*;3738public class TestDSA {3940// values of the keys we use for the tests4142private final static String ps =43"fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669" +44"455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b7" +45"6b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb" +46"83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c7";4748private final static String qs =49"9760508f15230bccb292b982a2eb840bf0581cf5";5051private final static String gs =52"f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d078267" +53"5159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e1" +54"3c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243b" +55"cca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a";5657private final static String xs =58"2952afd9aef9527f9b40d23c8916f7d046028f9d";5960private final static String ys =61"b16ddb0f9394c328c983ecf23b20014ace368a1af5728dffbf1162de9ed8ebf6" +62"384f323930e091503035caa797e3674221fc16136240b5474799ede2b7b11313" +63"7574a9c26bcf900940027b4bcd511ef1d1daf2e69c416aebaf3bdf39f02473b9" +64"d963f99414c09d97bb0830d9fbdcf7bb9dad8a2179fcdf296838c4cfab8f4d8f";6566private final static BigInteger p = new BigInteger(ps, 16);67private final static BigInteger q = new BigInteger(qs, 16);68private final static BigInteger g = new BigInteger(gs, 16);69private final static BigInteger x = new BigInteger(xs, 16);70private final static BigInteger y = new BigInteger(ys, 16);7172// data for test 1, original and SHA-1 hashed73private final static byte[] data1Raw = HexFormat.of()74.parseHex("0102030405060708090a0b0c0d0e0f10111213");75private final static byte[] data1SHA = HexFormat.ofDelimiter(":")76.parseHex("00:e2:5f:c9:1c:8f:d6:8c:6a:dc:c6:bd:f0:46:60:5e:a2:cd:8d:ad");7778// valid signatures of data1. sig1b uses incorrect ASN.1 encoding,79// which we want to accept anyway for compatibility80private final static byte[] sig1a = HexFormat.ofDelimiter(":")81.parseHex("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");82private final static byte[] sig1b = HexFormat.ofDelimiter(":")83.parseHex("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");8485// data for test 2 (invalid signatures)86private final static byte[] data2Raw = {};87private final static byte[] data2SHA = HexFormat.ofDelimiter(":")88.parseHex("da:39:a3:ee:5e:6b:4b:0d:32:55:bf:ef:95:60:18:90:af:d8:07:09");8990private static void verify(Provider provider, String alg, PublicKey key, byte[] data, byte[] sig, boolean result) throws Exception {91Signature s = Signature.getInstance(alg, provider);92s.initVerify(key);93boolean r;94s.update(data);95r = s.verify(sig);96if (r != result) {97throw new Exception("Result mismatch, actual: " + r);98}99s.update(data);100r = s.verify(sig);101if (r != result) {102throw new Exception("Result mismatch, actual: " + r);103}104System.out.println("Passed");105}106107public static void main(String[] args) throws Exception {108long start = System.currentTimeMillis();109110Provider provider = Security.getProvider("SUN");111System.out.println("Testing provider " + provider + "...");112113KeyFactory kf = KeyFactory.getInstance("DSA", provider);114DSAPrivateKeySpec privSpec = new DSAPrivateKeySpec(x, p, q, g);115DSAPublicKeySpec pubSpec = new DSAPublicKeySpec(y, p, q, g);116PrivateKey privateKey = kf.generatePrivate(privSpec);117PublicKey publicKey = kf.generatePublic(pubSpec);118119// verify known-good and known-bad signatures using SHA1withDSA and RawDSA120verify(provider, "SHA1withDSA", publicKey, data1Raw, sig1a, true);121verify(provider, "SHA1withDSA", publicKey, data1Raw, sig1b, true);122verify(provider, "SHA1withDSA", publicKey, data2Raw, sig1a, false);123verify(provider, "SHA1withDSA", publicKey, data2Raw, sig1b, false);124125verify(provider, "RawDSA", publicKey, data1SHA, sig1a, true);126verify(provider, "RawDSA", publicKey, data1SHA, sig1b, true);127verify(provider, "RawDSA", publicKey, data2SHA, sig1a, false);128verify(provider, "RawDSA", publicKey, data2SHA, sig1b, false);129130byte[] data = new byte[2048];131new Random().nextBytes(data);132133// sign random data using SHA1withDSA and verify using134// SHA1withDSA and RawDSA135Signature s = Signature.getInstance("SHA1withDSA", provider);136s.initSign(privateKey);137s.update(data);138byte[] s1 = s.sign();139140s.initVerify(publicKey);141s.update(data);142if (!s.verify(s1)) {143throw new Exception("Sign/verify 1 failed");144}145146s = Signature.getInstance("RawDSA", provider);147MessageDigest md = MessageDigest.getInstance("SHA-1");148byte[] digest = md.digest(data);149s.initVerify(publicKey);150s.update(digest);151if (!s.verify(s1)) {152throw new Exception("Sign/verify 2 failed");153}154155// sign random data using RawDSA and verify using156// SHA1withDSA and RawDSA157s.initSign(privateKey);158s.update(digest);159byte[] s2 = s.sign();160161s.initVerify(publicKey);162s.update(digest);163if (!s.verify(s2)) {164throw new Exception("Sign/verify 3 failed");165}166167s = Signature.getInstance("SHA1withDSA", provider);168s.initVerify(publicKey);169s.update(data);170if (!s.verify(s2)) {171throw new Exception("Sign/verify 4 failed");172}173174// test behavior if data of incorrect length is passed175s = Signature.getInstance("RawDSA", provider);176s.initSign(privateKey);177s.update(new byte[8]);178s.update(new byte[64]);179try {180s.sign();181throw new Exception("No error RawDSA signing long data");182} catch (SignatureException e) {183// expected184}185186long stop = System.currentTimeMillis();187System.out.println("All tests passed (" + (stop - start) + " ms).");188}189}190191192