Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/rsa/pss/SignatureTest2.java
38853 views
/*1* Copyright (c) 2018, 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.RSAPrivateKey;24import java.security.interfaces.RSAPublicKey;25import java.security.spec.*;26import java.util.Arrays;27import java.util.stream.IntStream;28import static javax.crypto.Cipher.PRIVATE_KEY;29import static javax.crypto.Cipher.PUBLIC_KEY;3031/**32* @test33* @bug 8146293 823844834* @summary Create a signature for RSASSA-PSS and get its signed data.35* re-initiate the signature with the public key. The signature36* can be verified by acquired signed data.37* @run main SignatureTest2 76838* @run main SignatureTest2 102439* @run main SignatureTest2 102540* @run main SignatureTest2 204841* @run main SignatureTest2 204942* @run main/timeout=240 SignatureTest2 409643*/44public class SignatureTest2 {45/**46* ALGORITHM name, fixed as RSA.47*/48private static final String KEYALG = "RSASSA-PSS";4950/**51* JDK default RSA Provider.52*/53private static final String PROVIDER = "SunRsaSign";5455/**56* How much times signature updated.57*/58private static final int UPDATE_TIMES_TWO = 2;5960/**61* How much times signature initial updated.62*/63private static final int UPDATE_TIMES_TEN = 10;6465/**66* Digest algorithms to test w/ RSASSA-PSS signature algorithms67*/68private static final String[] DIGEST_ALG = {69"SHA-1", "SHA-224", "SHA-256", "SHA-384",70"SHA-512", "SHA-512/224", "SHA-512/256"71};7273private static final String SIG_ALG = "RSASSA-PSS";7475private static PSSParameterSpec genPSSParameter(String digestAlgo,76int digestLen, int keySize) {77// pick a salt length based on the key length and digestAlgo78int saltLength = keySize/8 - digestLen - 2;79if (saltLength < 0) {80System.out.println("keysize: " + keySize/8 + ", digestLen: " + digestLen);81return null;82}83return new PSSParameterSpec(digestAlgo, "MGF1",84new MGF1ParameterSpec(digestAlgo), saltLength, 1);85}8687public static void main(String[] args) throws Exception {88final int testSize = Integer.parseInt(args[0]);8990byte[] data = new byte[100];91IntStream.range(0, data.length).forEach(j -> {92data[j] = (byte) j;93});9495// create a key pair96KeyPair kpair = generateKeys(KEYALG, testSize);97Key[] privs = manipulateKey(PRIVATE_KEY, kpair.getPrivate());98Key[] pubs = manipulateKey(PUBLIC_KEY, kpair.getPublic());99100// For messsage digest algorithm, create and verify a RSASSA-PSS signature101Arrays.stream(privs).forEach(priv102-> Arrays.stream(pubs).forEach(pub103-> Arrays.stream(DIGEST_ALG).forEach(testAlg -> {104checkSignature(data, (PublicKey) pub, (PrivateKey) priv,105testAlg, testSize);106}107)));108109}110111private static KeyPair generateKeys(String keyalg, int size)112throws NoSuchAlgorithmException {113KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyalg);114kpg.initialize(size);115return kpg.generateKeyPair();116}117118private static Key[] manipulateKey(int type, Key key)119throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {120KeyFactory kf = KeyFactory.getInstance(KEYALG, PROVIDER);121122switch (type) {123case PUBLIC_KEY:124return new Key[]{125kf.generatePublic(kf.getKeySpec(key, RSAPublicKeySpec.class)),126kf.generatePublic(new X509EncodedKeySpec(key.getEncoded())),127kf.generatePublic(new RSAPublicKeySpec(128((RSAPublicKey) key).getModulus(),129((RSAPublicKey) key).getPublicExponent()))130};131case PRIVATE_KEY:132return new Key[]{133kf.generatePrivate(kf.getKeySpec(key,134RSAPrivateKeySpec.class)),135kf.generatePrivate(new PKCS8EncodedKeySpec(136key.getEncoded())),137kf.generatePrivate(new RSAPrivateKeySpec(((RSAPrivateKey) key).getModulus(),138((RSAPrivateKey) key).getPrivateExponent()))139};140}141throw new RuntimeException("We shouldn't reach here");142}143144private static void checkSignature(byte[] data, PublicKey pub,145PrivateKey priv, String digestAlg, int keySize) throws RuntimeException {146try {147Signature sig = Signature.getInstance(SIG_ALG, PROVIDER);148int digestLen = MessageDigest.getInstance(digestAlg).getDigestLength();149PSSParameterSpec params = genPSSParameter(digestAlg, digestLen, keySize);150if (params == null) {151System.out.println("Skip test due to short key size");152return;153}154sig.setParameter(params);155sig.initSign(priv);156for (int i = 0; i < UPDATE_TIMES_TEN; i++) {157sig.update(data);158}159byte[] signedDataTen = sig.sign();160161// Make sure signature can be generated without re-init162sig.update(data);163byte[] signedDataOne = sig.sign();164165// Make sure signature verifies with original data166System.out.println("Verify using params " + sig.getParameters());167sig.initVerify(pub);168sig.setParameter(params);169for (int i = 0; i < UPDATE_TIMES_TEN; i++) {170sig.update(data);171}172if (!sig.verify(signedDataTen)) {173throw new RuntimeException("Signature verification test#1 failed w/ "174+ digestAlg);175}176177// Make sure signature can verify without re-init178sig.update(data);179if (!sig.verify(signedDataOne)) {180throw new RuntimeException("Signature verification test#2 failed w/ "181+ digestAlg);182}183184// Make sure signature does NOT verify when the original data185// has changed186for (int i = 0; i < UPDATE_TIMES_TWO; i++) {187sig.update(data);188}189190if (sig.verify(signedDataOne)) {191throw new RuntimeException("Bad signature accepted w/ "192+ digestAlg);193}194} catch (NoSuchAlgorithmException | InvalidKeyException |195SignatureException | NoSuchProviderException |196InvalidAlgorithmParameterException e) {197e.printStackTrace();198throw new RuntimeException(e);199}200}201}202203204