Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/provider/DSA/TestDSA2.java
38853 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 704406025* @run main/othervm/timeout=250 TestDSA226* @summary verify that DSA signature works using SHA and SHA-224 and SHA-256 digests.27* @key randomness28*/293031import java.security.*;32import java.security.spec.*;33import java.security.interfaces.*;3435public class TestDSA2 {3637// NOTE: need to explictly specify provider since the more38// preferred provider SunPKCS11 provider only supports up39// 1024 bits.40private static final String PROV = "SUN";4142private static final String[] SIG_ALGOS = {43"SHA1withDSA", "SHA224withDSA", "SHA256withDSA"44};4546private static final int[] KEYSIZES = {471024, 204848};4950public static void main(String[] args) throws Exception {51boolean[] expectedToPass = { true, true, true };52test(1024, expectedToPass);53boolean[] expectedToPass2 = { false, true, true };54test(2048, expectedToPass2);55}5657private static void test(int keySize, boolean[] testStatus)58throws Exception {59byte[] data = "1234567890".getBytes();60System.out.println("Test against key size: " + keySize);6162KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", PROV);63keyGen.initialize(keySize, new SecureRandom());64KeyPair pair = keyGen.generateKeyPair();6566if (testStatus.length != SIG_ALGOS.length) {67throw new RuntimeException("TestError: incorrect status array!");68}69for (int i = 0; i < SIG_ALGOS.length; i++) {70Signature dsa = Signature.getInstance(SIG_ALGOS[i], PROV);71try {72dsa.initSign(pair.getPrivate());73dsa.update(data);74byte[] sig = dsa.sign();75dsa.initVerify(pair.getPublic());76dsa.update(data);77boolean verifies = dsa.verify(sig);78if (verifies == testStatus[i]) {79System.out.println(SIG_ALGOS[i] + ": Passed");80} else {81System.out.println(SIG_ALGOS[i] + ": should " +82(testStatus[i]? "pass":"fail"));83throw new RuntimeException(SIG_ALGOS[i] + ": Unexpected Test result!");8485}86} catch (Exception ex) {87if (testStatus[i]) {88ex.printStackTrace();89throw new RuntimeException(SIG_ALGOS[i] + ": Unexpected exception " + ex);90} else {91System.out.println(SIG_ALGOS[i] + ": Passed, expected " + ex);92}93}94}95}96}979899