Path: blob/master/test/jdk/sun/security/provider/DSA/TestDSA2.java
41154 views
/*1* Copyright (c) 2012, 2015, 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*/22/*23* @test24* @bug 7044060 804296725* @run main/othervm/timeout=250 TestDSA226* @summary verify that DSA signature works using SHA and SHA-224 and27* SHA-256 digests.28* @key randomness29*/303132import java.security.*;33import java.security.spec.*;34import java.security.interfaces.*;3536public class TestDSA2 {3738// NOTE: need to explictly specify provider since the more39// preferred provider SunPKCS11 provider only supports up40// 1024 bits.41private static final String PROV = "SUN";4243private static final String[] SIG_ALGOS = {44"NONEwithDSA",45"SHA1withDSA",46"SHA224withDSA",47"SHA256withDSA",48"NONEwithDSAinP1363Format",49"SHA1withDSAinP1363Format",50"SHA224withDSAinP1363Format",51"SHA256withDSAinP1363Format"52};5354private static final int[] KEYSIZES = {551024, 204856};5758public static void main(String[] args) throws Exception {59boolean[] expectedToPass = { true, true, true, true,60true, true, true, true };61test(1024, expectedToPass);62boolean[] expectedToPass2 = { true, false, true, true,63true, false, true, true };64test(2048, expectedToPass2);65}6667private static void test(int keySize, boolean[] testStatus)68throws Exception {69// Raw DSA requires the data to be exactly 20 bytes long. Use a70// 20-byte array for these tests so that the NONEwithDSA* algorithms71// don't complain.72byte[] data = "12345678901234567890".getBytes();73System.out.println("Test against key size: " + keySize);7475KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", PROV);76keyGen.initialize(keySize, new SecureRandom());77KeyPair pair = keyGen.generateKeyPair();7879if (testStatus.length != SIG_ALGOS.length) {80throw new RuntimeException("TestError: incorrect status array!");81}82for (int i = 0; i < SIG_ALGOS.length; i++) {83Signature dsa = Signature.getInstance(SIG_ALGOS[i], PROV);84try {85dsa.initSign(pair.getPrivate());86dsa.update(data);87byte[] sig = dsa.sign();88dsa.initVerify(pair.getPublic());89dsa.update(data);90boolean verifies = dsa.verify(sig);91if (verifies == testStatus[i]) {92System.out.println(SIG_ALGOS[i] + ": Passed");93} else {94System.out.println(SIG_ALGOS[i] + ": should " +95(testStatus[i]? "pass":"fail"));96throw new RuntimeException(SIG_ALGOS[i] + ": Unexpected Test result!");9798}99} catch (Exception ex) {100if (testStatus[i]) {101ex.printStackTrace();102throw new RuntimeException(SIG_ALGOS[i] + ": Unexpected exception " + ex);103} else {104System.out.println(SIG_ALGOS[i] + ": Passed, expected " + ex);105}106}107}108}109}110111112