Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/rsa/TestKeyPairGenerator.java
38855 views
/*1* Copyright (c) 2003, 2018, 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*/2223/**24* @test25* @bug 485696626* @summary Verify that the RSA KeyPairGenerator works27* @author Andreas Sterbenz28* @library ..29* @run main/othervm TestKeyPairGenerator30* @run main/othervm TestKeyPairGenerator sm TestKeyPairGenerator.policy31* @key intermittent randomness32*/3334import java.math.BigInteger;35import java.security.KeyPair;36import java.security.KeyPairGenerator;37import java.security.PrivateKey;38import java.security.Provider;39import java.security.PublicKey;40import java.security.Signature;41import java.security.interfaces.RSAPrivateCrtKey;42import java.security.interfaces.RSAPublicKey;43import java.security.spec.RSAKeyGenParameterSpec;44import java.util.Random;4546public class TestKeyPairGenerator extends PKCS11Test {4748private static Provider provider;4950private static byte[] data;5152private static void testSignature(String algorithm, PrivateKey privateKey,53PublicKey publicKey) throws Exception {54System.out.println("Testing " + algorithm + "...");55Signature s = Signature.getInstance(algorithm, provider);56s.initSign(privateKey);57s.update(data);58byte[] sig = s.sign();59s.initVerify(publicKey);60s.update(data);61boolean result = s.verify(sig);62if (result == false) {63throw new Exception("Verification failed");64}65}6667private static void test(PrivateKey privateKey, PublicKey publicKey) throws Exception {68testSignature("MD2withRSA", privateKey, publicKey);69testSignature("MD5withRSA", privateKey, publicKey);70testSignature("SHA1withRSA", privateKey, publicKey);71testSignature("SHA224withRSA", privateKey, publicKey);72testSignature("SHA256withRSA", privateKey, publicKey);73RSAPublicKey rsaKey = (RSAPublicKey)publicKey;74if (rsaKey.getModulus().bitLength() > 512) {75// for SHA384 and SHA512 the data is too long for 512 bit keys76testSignature("SHA384withRSA", privateKey, publicKey);77testSignature("SHA512withRSA", privateKey, publicKey);78}79}8081// regression test for 486519882private static void testInvalidSignature(KeyPair kp1, KeyPair kp2) throws Exception {83System.out.println("Testing signature with incorrect key...");84Signature sig = Signature.getInstance("MD5withRSA", provider);85sig.initSign(kp1.getPrivate());86byte[] data = new byte[100];87sig.update(data);88byte[] signature = sig.sign();89sig.initVerify(kp1.getPublic());90sig.update(data);91if (sig.verify(signature) == false) {92throw new Exception("verification failed");93}94sig.initVerify(kp2.getPublic());95sig.update(data);96// verify needs to return false and not throw an Exception97if (sig.verify(signature)) {98throw new Exception("verification unexpectedly succeeded");99}100}101102public static void main(String[] args) throws Exception {103main(new TestKeyPairGenerator(), args);104}105106@Override107public void main(Provider p) throws Exception {108long start = System.currentTimeMillis();109provider = p;110data = new byte[2048];111// keypair generation is very slow, test only a few short keys112int[] keyLengths = {512, 512, 1024};113BigInteger[] pubExps = {null, RSAKeyGenParameterSpec.F4, null};114KeyPair[] keyPairs = new KeyPair[3];115new Random().nextBytes(data);116KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", provider);117for (int i = 0; i < keyLengths.length; i++) {118int len = keyLengths[i];119BigInteger exp = pubExps[i];120System.out.println("Generating " + len + " bit keypair...");121if (exp == null) {122kpg.initialize(len);123} else {124kpg.initialize(new RSAKeyGenParameterSpec(len, exp));125}126KeyPair kp = kpg.generateKeyPair();127keyPairs[i] = kp;128RSAPublicKey publicKey = (RSAPublicKey)kp.getPublic();129System.out.println(publicKey);130RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey)kp.getPrivate();131if (publicKey.getModulus().equals(privateKey.getModulus()) == false) {132throw new Exception("Moduli do not match");133}134if (publicKey.getPublicExponent().equals(privateKey.getPublicExponent()) == false) {135throw new Exception("Exponents do not match");136}137int keyLen = publicKey.getModulus().bitLength();138if ((keyLen > len) || (keyLen < len - 1)) {139throw new Exception("Incorrect key length: " + keyLen);140}141if (exp != null) {142if (exp.equals(publicKey.getPublicExponent()) == false) {143throw new Exception("Incorrect exponent");144}145}146test(privateKey, publicKey);147}148testInvalidSignature(keyPairs[0], keyPairs[1]);149testInvalidSignature(keyPairs[0], keyPairs[2]);150long stop = System.currentTimeMillis();151System.out.println("All tests passed (" + (stop - start) + " ms).");152}153}154155156