Path: blob/master/test/jdk/sun/security/pkcs11/Signature/SigInteropPSS2.java
41153 views
/*1* Copyright (c) 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 SigInteropPSS235*/36public class SigInteropPSS2 extends PKCS11Test {3738private static final byte[] MSG =39"Interoperability test between SunRsaSign and SunPKCS11".getBytes();4041private static final String[] DIGESTS = {42"SHA224", "SHA256", "SHA384", "SHA512",43"SHA3-224", "SHA3-256", "SHA3-384", "SHA3-512"44};4546public static void main(String[] args) throws Exception {47main(new SigInteropPSS2(), args);48}4950@Override51public void main(Provider p) throws Exception {5253Signature sigPkcs11;54Signature sigSunRsaSign =55Signature.getInstance("RSASSA-PSS", "SunRsaSign");5657KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);58kpg.initialize(3072);59KeyPair kp = kpg.generateKeyPair();6061for (String digest : DIGESTS) {62try {63sigPkcs11 = Signature.getInstance(digest + "withRSASSA-PSS", p);64} catch (NoSuchAlgorithmException e) {65System.out.println("Skip testing " + digest + "withRSASSA-PSS" +66" due to no support");67continue;68}6970runTest(sigPkcs11, sigSunRsaSign, kp);71}72System.out.println("Test passed");73}7475static void runTest(Signature signer, Signature verifier, KeyPair kp)76throws Exception {77System.out.println("\tSign: " + signer.getProvider().getName());78System.out.println("\tVerify: " + verifier.getProvider().getName());7980signer.initSign(kp.getPrivate());81signer.update(MSG);82byte[] sigBytes = signer.sign();8384AlgorithmParameters signParams = signer.getParameters();85verifier.setParameter(signParams.getParameterSpec86(PSSParameterSpec.class));87verifier.initVerify(kp.getPublic());8889verifier.update(MSG);90boolean isValid = verifier.verify(sigBytes);91if (isValid) {92System.out.println("\tPSS Signature verified");93} else {94throw new RuntimeException("ERROR verifying PSS Signature");95}96}97}9899100