Path: blob/master/test/jdk/sun/security/pkcs11/Signature/SignatureTestPSS.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*/22import java.security.*;23import java.security.interfaces.*;24import java.security.spec.*;25import java.util.stream.IntStream;2627/**28* @test29* @bug 8080462 8226651 824233230* @summary Generate a RSASSA-PSS signature and verify it using PKCS11 provider31* @library /test/lib ..32* @modules jdk.crypto.cryptoki33* @run main SignatureTestPSS34*/35public class SignatureTestPSS extends PKCS11Test {3637// PKCS11 does not support RSASSA-PSS keys yet38private static final String KEYALG = "RSA";39private static final String SIGALG = "RSASSA-PSS";4041private static final int[] KEYSIZES = { 2048, 3072 };42private static final String[] DIGESTS = {43"SHA-224", "SHA-256", "SHA-384" , "SHA-512",44"SHA3-224", "SHA3-256", "SHA3-384" , "SHA3-512",45};46private Provider prov;4748/**49* How much times signature updated.50*/51private static final int UPDATE_TIMES_FIFTY = 50;5253/**54* How much times signature initial updated.55*/56private static final int UPDATE_TIMES_HUNDRED = 100;5758public static void main(String[] args) throws Exception {59main(new SignatureTestPSS(), args);60}6162@Override63public void main(Provider p) throws Exception {64Signature sig;65try {66sig = Signature.getInstance(SIGALG, p);67} catch (NoSuchAlgorithmException e) {68System.out.println("Skip testing RSASSA-PSS" +69" due to no support");70return;71}72this.prov = p;73for (int i : KEYSIZES) {74runTest(i);75}76}7778private void runTest(int keySize) throws Exception {79byte[] data = new byte[100];80IntStream.range(0, data.length).forEach(j -> {81data[j] = (byte) j;82});83System.out.println("[KEYSIZE = " + keySize + "]");8485// create a key pair86KeyPair kpair = generateKeys(KEYALG, keySize);87test(DIGESTS, kpair.getPrivate(), kpair.getPublic(), data);88}8990private void test(String[] digestAlgs, PrivateKey privKey,91PublicKey pubKey, byte[] data) throws RuntimeException {92// For signature algorithm, create and verify a signature93for (String hash : digestAlgs) {94for (String mgfHash : digestAlgs) {95try {96checkSignature(data, pubKey, privKey, hash, mgfHash);97} catch (NoSuchAlgorithmException | InvalidKeyException |98SignatureException | NoSuchProviderException ex) {99throw new RuntimeException(ex);100} catch (InvalidAlgorithmParameterException ex2) {101System.out.println("Skip test due to " + ex2);102}103}104};105}106107private KeyPair generateKeys(String keyalg, int size)108throws NoSuchAlgorithmException {109KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyalg, prov);110kpg.initialize(size);111return kpg.generateKeyPair();112}113114private void checkSignature(byte[] data, PublicKey pub,115PrivateKey priv, String hash, String mgfHash)116throws NoSuchAlgorithmException, InvalidKeyException,117SignatureException, NoSuchProviderException,118InvalidAlgorithmParameterException {119120String testName = hash + " and MGF1_" + mgfHash;121// only test RSASSA-PSS signature against the supplied hash/mgfHash122// if they are supported; otherwise PKCS11 library will throw123// CKR_MECHANISM_PARAM_INVALID at Signature.initXXX calls124try {125MessageDigest md = MessageDigest.getInstance(hash, prov);126if (!hash.equalsIgnoreCase(mgfHash)) {127md = MessageDigest.getInstance(mgfHash, prov);128}129} catch (NoSuchAlgorithmException nsae) {130System.out.println("Skip testing " + hash + "/" + mgfHash);131return;132}133134System.out.println("Testing against " + testName);135Signature sig = Signature.getInstance(SIGALG, prov);136AlgorithmParameterSpec params = new PSSParameterSpec(137hash, "MGF1", new MGF1ParameterSpec(mgfHash), 0, 1);138sig.setParameter(params);139sig.initSign(priv);140for (int i = 0; i < UPDATE_TIMES_HUNDRED; i++) {141sig.update(data);142}143byte[] signedData = sig.sign();144145// Make sure signature verifies with original data146// do we need to call sig.setParameter(params) again?147sig.initVerify(pub);148for (int i = 0; i < UPDATE_TIMES_HUNDRED; i++) {149sig.update(data);150}151if (!sig.verify(signedData)) {152throw new RuntimeException("Failed to verify signature");153}154155// Make sure signature does NOT verify when the original data156// has changed157sig.initVerify(pub);158for (int i = 0; i < UPDATE_TIMES_FIFTY; i++) {159sig.update(data);160}161162if (sig.verify(signedData)) {163throw new RuntimeException("Failed to detect bad signature");164}165}166}167168169