Path: blob/master/test/jdk/sun/security/pkcs11/Signature/TestDSA2.java
41153 views
/*1* Copyright (c) 2012, 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*/22/*23* @test24* @bug 8080462 824233225* @library /test/lib ..26* @modules jdk.crypto.cryptoki27* @run main/othervm/timeout=250 TestDSA228* @summary verify that DSA signature works using SHA-2 digests.29* @key randomness30*/313233import java.security.*;34import java.security.spec.*;35import java.security.interfaces.*;3637public class TestDSA2 extends PKCS11Test {3839private static final String[] SIG_ALGOS = {40"SHA224withDSA",41"SHA256withDSA",42"SHA3-224withDSA",43"SHA3-256withDSA",44"SHA384withDSA",45"SHA512withDSA",46"SHA3-384withDSA",47"SHA3-512withDSA",48};4950private static final int KEYSIZE = 2048;5152public static void main(String[] args) throws Exception {53main(new TestDSA2(), args);54}5556@Override57public void main(Provider p) throws Exception {58KeyPair kp;59try {60KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA", p);61kpg.initialize(KEYSIZE);62kp = kpg.generateKeyPair();63} catch (Exception ex) {64System.out.println("Skip due to no 2048-bit DSA support: " + ex);65return;66}6768boolean allPass = true;69for (String sigAlg : SIG_ALGOS) {70System.out.println("Testing " + sigAlg);71try {72Signature sig = Signature.getInstance(sigAlg, p);73test(sig, kp, p);74} catch (NoSuchAlgorithmException nsae) {75System.out.println("=>Skip due to no support");76} catch (Exception ex) {77System.out.println("Unexpected exception when testing " +78sigAlg);79ex.printStackTrace();80allPass = false;81}82}83if (allPass) {84System.out.println("Tests Passed");85} else {86throw new RuntimeException("One or more tests failed");87}88}8990private static void test(Signature sig, KeyPair kp, Provider p)91throws Exception {9293byte[] data = "anything will do".getBytes();9495sig.initSign(kp.getPrivate());96sig.update(data);97byte[] signature = sig.sign();9899Signature sigV = Signature.getInstance(sig.getAlgorithm() , p);100sigV.initVerify(kp.getPublic());101sigV.update(data);102boolean verifies = sigV.verify(signature);103System.out.println("=> Passed");104}105}106107108