Path: blob/master/test/jdk/sun/security/pkcs11/Signature/SignatureTestPSS2.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*/22import java.security.*;23import java.security.interfaces.*;24import java.security.spec.*;25import java.util.stream.IntStream;2627/**28* @test29* @bug 8244154 824233230* @summary Generate a <digest>withRSASSA-PSS signature and verify it using31* PKCS11 provider32* @library /test/lib ..33* @modules jdk.crypto.cryptoki34* @run main SignatureTestPSS235*/36public class SignatureTestPSS2 extends PKCS11Test {3738// PKCS11 does not support RSASSA-PSS keys yet39private static final String KEYALG = "RSA";40private static final String[] SIGALGS = {41"SHA224withRSASSA-PSS", "SHA256withRSASSA-PSS",42"SHA384withRSASSA-PSS", "SHA512withRSASSA-PSS",43"SHA3-224withRSASSA-PSS", "SHA3-256withRSASSA-PSS",44"SHA3-384withRSASSA-PSS", "SHA3-512withRSASSA-PSS"45};4647private static final int[] KEYSIZES = { 2048, 3072 };4849/**50* How much times signature updated.51*/52private static final int UPDATE_TIMES = 2;5354public static void main(String[] args) throws Exception {55main(new SignatureTestPSS2(), args);56}5758@Override59public void main(Provider p) throws Exception {60for (String sa : SIGALGS) {61Signature sig;62try {63sig = Signature.getInstance(sa, p);64} catch (NoSuchAlgorithmException e) {65System.out.println("Skip testing " + sa +66" due to no support");67return;68}69for (int i : KEYSIZES) {70runTest(sig, i);71}72}73}7475private static void runTest(Signature s, int keySize) throws Exception {76byte[] data = new byte[100];77IntStream.range(0, data.length).forEach(j -> {78data[j] = (byte) j;79});80System.out.println("[KEYSIZE = " + keySize + "]");8182// create a key pair83KeyPair kpair = generateKeys(KEYALG, keySize, s.getProvider());84test(s, kpair.getPrivate(), kpair.getPublic(), data);85}8687private static void test(Signature sig, PrivateKey privKey,88PublicKey pubKey, byte[] data) throws RuntimeException {89// For signature algorithm, create and verify a signature90try {91checkSignature(sig, privKey, pubKey, data);92} catch (NoSuchAlgorithmException | InvalidKeyException |93SignatureException | NoSuchProviderException ex) {94throw new RuntimeException(ex);95} catch (InvalidAlgorithmParameterException ex2) {96System.out.println("Skip test due to " + ex2);97}98}99100private static KeyPair generateKeys(String keyalg, int size, Provider p)101throws NoSuchAlgorithmException {102KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyalg, p);103kpg.initialize(size);104return kpg.generateKeyPair();105}106107private static void checkSignature(Signature sig, PrivateKey priv,108PublicKey pub, byte[] data) throws NoSuchAlgorithmException,109InvalidKeyException, SignatureException, NoSuchProviderException,110InvalidAlgorithmParameterException {111System.out.println("Testing against " + sig.getAlgorithm());112sig.initSign(priv);113for (int i = 0; i < UPDATE_TIMES; i++) {114sig.update(data);115}116byte[] signedData = sig.sign();117118// Make sure signature verifies with original data119// do we need to call sig.setParameter(params) again?120sig.initVerify(pub);121for (int i = 0; i < UPDATE_TIMES; i++) {122sig.update(data);123}124if (!sig.verify(signedData)) {125throw new RuntimeException("Failed to verify signature");126}127128// Make sure signature does NOT verify when the original data129// has changed130sig.initVerify(pub);131for (int i = 0; i < UPDATE_TIMES + 1; i++) {132sig.update(data);133}134135if (sig.verify(signedData)) {136throw new RuntimeException("Failed to detect bad signature");137}138}139}140141142