Path: blob/master/test/jdk/sun/security/rsa/KeySizeTest.java
41149 views
/*1* Copyright (c) 2015, 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*/2223import java.security.KeyFactory;24import java.security.KeyPair;25import java.security.KeyPairGenerator;26import java.security.PrivateKey;27import java.security.PublicKey;28import java.security.interfaces.RSAKey;29import java.security.interfaces.RSAPrivateKey;30import java.security.interfaces.RSAPublicKey;31import java.security.spec.RSAPrivateKeySpec;32import java.security.spec.RSAPublicKeySpec;3334/**35* @test36* @bug 804419937* @summary test if the private and public key size are the same as what is set38* through KeyPairGenerator.39* @run main KeySizeTest 512 1040* @run main KeySizeTest 768 1041* @run main KeySizeTest 1024 1042* @run main KeySizeTest 2048 543* @run main KeySizeTest 4096 144*/45public class KeySizeTest {4647/**48* ALGORITHM name, fixed as RSA.49*/50private static final String KEYALG = "RSA";5152/**53* JDK default RSA Provider.54*/55private static final String PROVIDER_NAME = "SunRsaSign";5657public static void main(String[] args) throws Exception {58int iKeyPairSize = Integer.parseInt(args[0]);59int maxLoopCnt = Integer.parseInt(args[1]);6061int failCount = 0;62KeyPairGenerator keyPairGen63= KeyPairGenerator.getInstance(KEYALG, PROVIDER_NAME);64keyPairGen.initialize(iKeyPairSize);65// Generate RSA keypair66KeyPair keyPair = keyPairGen.generateKeyPair();6768// Get priavte and public keys69PrivateKey privateKey = keyPair.getPrivate();70PublicKey publicKey = keyPair.getPublic();71try {72if (!sizeTest(keyPair)) {73failCount++;74}75} catch (Exception ex) {76ex.printStackTrace(System.err);77failCount++;78}7980for (int iCnt = 0; iCnt < maxLoopCnt; iCnt++) {8182// Get keysize (modulus) of keys83KeyFactory keyFact = KeyFactory.getInstance(KEYALG, PROVIDER_NAME);8485// Comparing binary length.86RSAPrivateKeySpec privateKeySpec87= (RSAPrivateKeySpec) keyFact.getKeySpec(privateKey,88RSAPrivateKeySpec.class);89int iPrivateKeySize = privateKeySpec.getModulus().bitLength();9091RSAPublicKeySpec publicKeySpec92= (RSAPublicKeySpec) keyFact.getKeySpec(publicKey,93RSAPublicKeySpec.class);94int iPublicKeySize = publicKeySpec.getModulus().bitLength();9596if ((iKeyPairSize != iPublicKeySize) || (iKeyPairSize != iPrivateKeySize)) {97System.err.println("iKeyPairSize : " + iKeyPairSize);98System.err.println("Generated a " + iPrivateKeySize99+ " bit RSA private key");100System.err.println("Generated a " + iPublicKeySize101+ " bit RSA public key");102failCount++;103}104}105106if (failCount > 0) {107throw new RuntimeException("There are " + failCount + " tests failed.");108}109}110111/**112* @param kpair test key pair.113* @return true if test passed. false if test failed.114*/115private static boolean sizeTest(KeyPair kpair) {116RSAPrivateKey priv = (RSAPrivateKey) kpair.getPrivate();117RSAPublicKey pub = (RSAPublicKey) kpair.getPublic();118119// test the getModulus method120if ((priv instanceof RSAKey) && (pub instanceof RSAKey)) {121if (!priv.getModulus().equals(pub.getModulus())) {122System.err.println("priv.getModulus() = " + priv.getModulus());123System.err.println("pub.getModulus() = " + pub.getModulus());124return false;125}126}127return true;128}129}130131132