Path: blob/master/test/jdk/sun/security/pkcs11/Signature/SigInteropPSS.java
41153 views
/*1* Copyright (c) 2019, 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*/2223import java.security.*;24import java.security.spec.*;25import java.security.interfaces.*;2627/*28* @test29* @bug 8080462 8226651 824233230* @summary testing interoperability of PSS signatures of PKCS11 provider31* against SunRsaSign provider32* @library /test/lib ..33* @modules jdk.crypto.cryptoki34* @run main/othervm SigInteropPSS35*/36public class SigInteropPSS extends PKCS11Test {3738private static final byte[] MSG =39"Interoperability test between SunRsaSign and SunPKCS11".getBytes();4041private static final String[] DIGESTS = {42"SHA-224", "SHA-256", "SHA-384", "SHA-512"43};4445public static void main(String[] args) throws Exception {46main(new SigInteropPSS(), args);47}4849@Override50public void main(Provider p) throws Exception {51Signature sigPkcs11;52try {53sigPkcs11 = Signature.getInstance("RSASSA-PSS", p);54} catch (NoSuchAlgorithmException e) {55System.out.println("Skip testing RSASSA-PSS" +56" due to no support");57return;58}5960Signature sigSunRsaSign =61Signature.getInstance("RSASSA-PSS", "SunRsaSign");6263KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);64kpg.initialize(3072);65KeyPair kp = kpg.generateKeyPair();6667runTest(sigSunRsaSign, sigPkcs11, kp);68runTest(sigPkcs11, sigSunRsaSign, kp);6970System.out.println("Test passed");71}7273static void runTest(Signature signer, Signature verifier, KeyPair kp)74throws Exception {75System.out.println("\tSign using " + signer.getProvider().getName());76System.out.println("\tVerify using " + verifier.getProvider().getName());7778for (String hash : DIGESTS) {79for (String mgfHash : DIGESTS) {80System.out.println("\tDigest = " + hash);81System.out.println("\tMGF = MGF1_" + mgfHash);8283PSSParameterSpec params = new PSSParameterSpec(hash, "MGF1",84new MGF1ParameterSpec(mgfHash), 0, 1);8586signer.setParameter(params);87signer.initSign(kp.getPrivate());88verifier.setParameter(params);89verifier.initVerify(kp.getPublic());9091signer.update(MSG);92byte[] sigBytes = signer.sign();93verifier.update(MSG);94boolean isValid = verifier.verify(sigBytes);95if (isValid) {96System.out.println("\tPSS Signature verified");97} else {98throw new RuntimeException("ERROR verifying PSS Signature");99}100}101}102}103}104105106