Path: blob/master/test/jdk/sun/security/pkcs11/rsa/TestKeyPairGenerator.java
41154 views
/*1* Copyright (c) 2003, 2017, 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 works (use -Dseed=X to set PRNG seed)27* @author Andreas Sterbenz28* @library ..29* @library /test/lib30* @modules jdk.crypto.cryptoki31* @build jdk.test.lib.RandomFactory32* @run main/othervm -Djava.security.debug=sunpkcs11 TestKeyPairGenerator33* @run main/othervm -Djava.security.manager=allow -Djava.security.debug=sunpkcs11 TestKeyPairGenerator34* sm TestKeyPairGenerator.policy35* @key randomness36*/3738import java.math.BigInteger;39import java.security.KeyPair;40import java.security.KeyPairGenerator;41import java.security.PrivateKey;42import java.security.Provider;43import java.security.PublicKey;44import java.security.Signature;45import java.security.interfaces.RSAPrivateCrtKey;46import java.security.interfaces.RSAPublicKey;47import java.security.spec.RSAKeyGenParameterSpec;48import jdk.test.lib.RandomFactory;4950public class TestKeyPairGenerator extends PKCS11Test {5152private static Provider provider;5354private static byte[] data;5556private static void testSignature(String algorithm, PrivateKey privateKey,57PublicKey publicKey) throws Exception {58System.out.println("Testing " + algorithm + "...");59Signature s = Signature.getInstance(algorithm, provider);60s.initSign(privateKey);61s.update(data);62byte[] sig = s.sign();63s.initVerify(publicKey);64s.update(data);65boolean result = s.verify(sig);66if (result == false) {67throw new Exception("Verification failed");68}69}7071private static void test(PrivateKey privateKey, PublicKey publicKey) throws Exception {72testSignature("MD2withRSA", privateKey, publicKey);73testSignature("MD5withRSA", privateKey, publicKey);74testSignature("SHA1withRSA", privateKey, publicKey);75testSignature("SHA224withRSA", privateKey, publicKey);76testSignature("SHA256withRSA", privateKey, publicKey);77RSAPublicKey rsaKey = (RSAPublicKey)publicKey;78if (rsaKey.getModulus().bitLength() > 512) {79// for SHA384 and SHA512 the data is too long for 512 bit keys80testSignature("SHA384withRSA", privateKey, publicKey);81testSignature("SHA512withRSA", privateKey, publicKey);82}83}8485// regression test for 486519886private static void testInvalidSignature(KeyPair kp1, KeyPair kp2) throws Exception {87System.out.println("Testing signature with incorrect key...");88Signature sig = Signature.getInstance("MD5withRSA", provider);89sig.initSign(kp1.getPrivate());90byte[] data = new byte[100];91sig.update(data);92byte[] signature = sig.sign();93sig.initVerify(kp1.getPublic());94sig.update(data);95if (sig.verify(signature) == false) {96throw new Exception("verification failed");97}98sig.initVerify(kp2.getPublic());99sig.update(data);100// verify needs to return false and not throw an Exception101if (sig.verify(signature)) {102throw new Exception("verification unexpectedly succeeded");103}104}105106public static void main(String[] args) throws Exception {107main(new TestKeyPairGenerator(), args);108}109110@Override111public void main(Provider p) throws Exception {112long start = System.currentTimeMillis();113provider = p;114data = new byte[2048];115// keypair generation is very slow, test only a few short keys116int[] keyLengths = {512, 512, 1024};117BigInteger[] pubExps = {null, RSAKeyGenParameterSpec.F4, null};118KeyPair[] keyPairs = new KeyPair[3];119RandomFactory.getRandom().nextBytes(data);120KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", provider);121for (int i = 0; i < keyLengths.length; i++) {122int len = keyLengths[i];123BigInteger exp = pubExps[i];124System.out.println("Generating " + len + " bit keypair...");125if (exp == null) {126kpg.initialize(len);127} else {128kpg.initialize(new RSAKeyGenParameterSpec(len, exp));129}130KeyPair kp = kpg.generateKeyPair();131keyPairs[i] = kp;132RSAPublicKey publicKey = (RSAPublicKey)kp.getPublic();133System.out.println(publicKey);134RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey)kp.getPrivate();135if (publicKey.getModulus().equals(privateKey.getModulus()) == false) {136throw new Exception("Moduli do not match");137}138if (publicKey.getPublicExponent().equals(privateKey.getPublicExponent()) == false) {139throw new Exception("Exponents do not match");140}141int keyLen = publicKey.getModulus().bitLength();142if ((keyLen > len) || (keyLen < len - 1)) {143throw new Exception("Incorrect key length: " + keyLen);144}145if (exp != null) {146if (exp.equals(publicKey.getPublicExponent()) == false) {147throw new Exception("Incorrect exponent");148}149}150test(privateKey, publicKey);151}152testInvalidSignature(keyPairs[0], keyPairs[1]);153testInvalidSignature(keyPairs[0], keyPairs[2]);154long stop = System.currentTimeMillis();155System.out.println("All tests passed (" + (stop - start) + " ms).");156}157}158159160