Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/provider/DSA/TestDSA.java
38853 views
/*1* Copyright (c) 2003, 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 = b("0102030405060708090a0b0c0d0e0f10111213");74private 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");7576// valid signatures of data1. sig1b uses incorrect ASN.1 encoding,77// which we want to accept anyway for compatibility78private 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");79private 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");8081// data for test 2 (invalid signatures)82private final static byte[] data2Raw = {};83private 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");8485private static void verify(Provider provider, String alg, PublicKey key, byte[] data, byte[] sig, boolean result) throws Exception {86Signature s = Signature.getInstance(alg, provider);87s.initVerify(key);88boolean r;89s.update(data);90r = s.verify(sig);91if (r != result) {92throw new Exception("Result mismatch, actual: " + r);93}94s.update(data);95r = s.verify(sig);96if (r != result) {97throw new Exception("Result mismatch, actual: " + r);98}99System.out.println("Passed");100}101102public static void main(String[] args) throws Exception {103long start = System.currentTimeMillis();104105Provider provider = Security.getProvider("SUN");106System.out.println("Testing provider " + provider + "...");107108KeyFactory kf = KeyFactory.getInstance("DSA", provider);109DSAPrivateKeySpec privSpec = new DSAPrivateKeySpec(x, p, q, g);110DSAPublicKeySpec pubSpec = new DSAPublicKeySpec(y, p, q, g);111PrivateKey privateKey = kf.generatePrivate(privSpec);112PublicKey publicKey = kf.generatePublic(pubSpec);113114// verify known-good and known-bad signatures using SHA1withDSA and RawDSA115verify(provider, "SHA1withDSA", publicKey, data1Raw, sig1a, true);116verify(provider, "SHA1withDSA", publicKey, data1Raw, sig1b, true);117verify(provider, "SHA1withDSA", publicKey, data2Raw, sig1a, false);118verify(provider, "SHA1withDSA", publicKey, data2Raw, sig1b, false);119120verify(provider, "RawDSA", publicKey, data1SHA, sig1a, true);121verify(provider, "RawDSA", publicKey, data1SHA, sig1b, true);122verify(provider, "RawDSA", publicKey, data2SHA, sig1a, false);123verify(provider, "RawDSA", publicKey, data2SHA, sig1b, false);124125byte[] data = new byte[2048];126new Random().nextBytes(data);127128// sign random data using SHA1withDSA and verify using129// SHA1withDSA and RawDSA130Signature s = Signature.getInstance("SHA1withDSA", provider);131s.initSign(privateKey);132s.update(data);133byte[] s1 = s.sign();134135s.initVerify(publicKey);136s.update(data);137if (!s.verify(s1)) {138throw new Exception("Sign/verify 1 failed");139}140141s = Signature.getInstance("RawDSA", provider);142MessageDigest md = MessageDigest.getInstance("SHA-1");143byte[] digest = md.digest(data);144s.initVerify(publicKey);145s.update(digest);146if (!s.verify(s1)) {147throw new Exception("Sign/verify 2 failed");148}149150// sign random data using RawDSA and verify using151// SHA1withDSA and RawDSA152s.initSign(privateKey);153s.update(digest);154byte[] s2 = s.sign();155156s.initVerify(publicKey);157s.update(digest);158if (!s.verify(s2)) {159throw new Exception("Sign/verify 3 failed");160}161162s = Signature.getInstance("SHA1withDSA", provider);163s.initVerify(publicKey);164s.update(data);165if (!s.verify(s2)) {166throw new Exception("Sign/verify 4 failed");167}168169// test behavior if data of incorrect length is passed170s = Signature.getInstance("RawDSA", provider);171s.initSign(privateKey);172s.update(new byte[8]);173s.update(new byte[64]);174try {175s.sign();176throw new Exception("No error RawDSA signing long data");177} catch (SignatureException e) {178// expected179}180181long stop = System.currentTimeMillis();182System.out.println("All tests passed (" + (stop - start) + " ms).");183}184185private final static char[] hexDigits = "0123456789abcdef".toCharArray();186187public static String toString(byte[] b) {188StringBuffer sb = new StringBuffer(b.length * 3);189for (int i = 0; i < b.length; i++) {190int k = b[i] & 0xff;191if (i != 0) {192sb.append(':');193}194sb.append(hexDigits[k >>> 4]);195sb.append(hexDigits[k & 0xf]);196}197return sb.toString();198}199200public static byte[] parse(String s) {201try {202int n = s.length();203ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);204StringReader r = new StringReader(s);205while (true) {206int b1 = nextNibble(r);207if (b1 < 0) {208break;209}210int b2 = nextNibble(r);211if (b2 < 0) {212throw new RuntimeException("Invalid string " + s);213}214int b = (b1 << 4) | b2;215out.write(b);216}217return out.toByteArray();218} catch (IOException e) {219throw new RuntimeException(e);220}221}222223public static byte[] b(String s) {224return parse(s);225}226227private static int nextNibble(StringReader r) throws IOException {228while (true) {229int ch = r.read();230if (ch == -1) {231return -1;232} else if ((ch >= '0') && (ch <= '9')) {233return ch - '0';234} else if ((ch >= 'a') && (ch <= 'f')) {235return ch - 'a' + 10;236} else if ((ch >= 'A') && (ch <= 'F')) {237return ch - 'A' + 10;238}239}240}241242}243244245