Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Signature/SignatureTestPSS.java
38855 views
/*1* Copyright (c) 2019, 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 808046230* @summary Generate a RSASSA-PSS signature and verify it using PKCS11 provider31* @library ..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 = { "SHA-224", "SHA-256",43"SHA-384" , "SHA-512" };44private Provider prov;4546/**47* How much times signature updated.48*/49private static final int UPDATE_TIMES_FIFTY = 50;5051/**52* How much times signature initial updated.53*/54private static final int UPDATE_TIMES_HUNDRED = 100;5556public static void main(String[] args) throws Exception {57main(new SignatureTestPSS(), args);58}5960@Override61public void main(Provider p) throws Exception {62Signature sig;63try {64sig = Signature.getInstance(SIGALG, p);65} catch (NoSuchAlgorithmException e) {66System.out.println("Skip testing RSASSA-PSS" +67" due to no support");68return;69}70this.prov = p;71for (int i : KEYSIZES) {72runTest(i);73}74}7576private void runTest(int keySize) throws Exception {77byte[] data = new byte[100];78IntStream.range(0, data.length).forEach(j -> {79data[j] = (byte) j;80});81System.out.println("[KEYSIZE = " + keySize + "]");8283// create a key pair84KeyPair kpair = generateKeys(KEYALG, keySize);85test(DIGESTS, kpair.getPrivate(), kpair.getPublic(), data);86}8788private void test(String[] testAlgs, PrivateKey privKey,89PublicKey pubKey, byte[] data) throws RuntimeException {90// For signature algorithm, create and verify a signature91for (String testAlg : testAlgs) {92try {93checkSignature(data, pubKey, privKey, testAlg);94} catch (NoSuchAlgorithmException | InvalidKeyException |95SignatureException | NoSuchProviderException ex) {96throw new RuntimeException(ex);97} catch (InvalidAlgorithmParameterException ex2) {98System.out.println("Skip test due to " + ex2);99}100};101}102103private KeyPair generateKeys(String keyalg, int size)104throws NoSuchAlgorithmException {105KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyalg, prov);106kpg.initialize(size);107return kpg.generateKeyPair();108}109110private void checkSignature(byte[] data, PublicKey pub,111PrivateKey priv, String mdAlg) throws NoSuchAlgorithmException,112InvalidKeyException, SignatureException, NoSuchProviderException,113InvalidAlgorithmParameterException {114System.out.println("Testing against " + mdAlg);115Signature sig = Signature.getInstance(SIGALG, prov);116AlgorithmParameterSpec params = new PSSParameterSpec(117mdAlg, "MGF1", new MGF1ParameterSpec(mdAlg), 0, 1);118sig.setParameter(params);119sig.initSign(priv);120for (int i = 0; i < UPDATE_TIMES_HUNDRED; i++) {121sig.update(data);122}123byte[] signedData = sig.sign();124125// Make sure signature verifies with original data126// do we need to call sig.setParameter(params) again?127sig.initVerify(pub);128for (int i = 0; i < UPDATE_TIMES_HUNDRED; i++) {129sig.update(data);130}131if (!sig.verify(signedData)) {132throw new RuntimeException("Failed to verify signature");133}134135// Make sure signature does NOT verify when the original data136// has changed137sig.initVerify(pub);138for (int i = 0; i < UPDATE_TIMES_FIFTY; i++) {139sig.update(data);140}141142if (sig.verify(signedData)) {143throw new RuntimeException("Failed to detect bad signature");144}145}146}147148149