0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4856966
0N/A * @summary Verify that the RSA KeyPairGenerator works
0N/A * @author Andreas Sterbenz
0N/A * @library ..
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/Aimport java.math.BigInteger;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.security.interfaces.*;
0N/Aimport java.security.spec.*;
0N/A
0N/Apublic class TestKeyPairGenerator extends PKCS11Test {
0N/A
0N/A private static Provider provider;
0N/A
0N/A private static byte[] data;
0N/A
0N/A private static void testSignature(String algorithm, PrivateKey privateKey, PublicKey publicKey) throws Exception {
0N/A System.out.println("Testing " + algorithm + "...");
0N/A Signature s = Signature.getInstance(algorithm, provider);
0N/A s.initSign(privateKey);
0N/A s.update(data);
0N/A byte[] sig = s.sign();
0N/A s.initVerify(publicKey);
0N/A s.update(data);
0N/A boolean result = s.verify(sig);
0N/A if (result == false) {
0N/A throw new Exception("Verification failed");
0N/A }
0N/A }
0N/A
0N/A private static void test(PrivateKey privateKey, PublicKey publicKey) throws Exception {
0N/A testSignature("MD2withRSA", privateKey, publicKey);
0N/A testSignature("MD5withRSA", privateKey, publicKey);
0N/A testSignature("SHA1withRSA", privateKey, publicKey);
0N/A testSignature("SHA256withRSA", privateKey, publicKey);
0N/A RSAPublicKey rsaKey = (RSAPublicKey)publicKey;
0N/A if (rsaKey.getModulus().bitLength() > 512) {
0N/A // for SHA384 and SHA512 the data is too long for 512 bit keys
0N/A testSignature("SHA384withRSA", privateKey, publicKey);
0N/A testSignature("SHA512withRSA", privateKey, publicKey);
0N/A }
0N/A }
0N/A
0N/A // regression test for 4865198
0N/A private static void testInvalidSignature(KeyPair kp1, KeyPair kp2) throws Exception {
0N/A System.out.println("Testing signature with incorrect key...");
0N/A Signature sig = Signature.getInstance("MD5withRSA", provider);
0N/A sig.initSign(kp1.getPrivate());
0N/A byte[] data = new byte[100];
0N/A sig.update(data);
0N/A byte[] signature = sig.sign();
0N/A sig.initVerify(kp1.getPublic());
0N/A sig.update(data);
0N/A if (sig.verify(signature) == false) {
0N/A throw new Exception("verification failed");
0N/A }
0N/A sig.initVerify(kp2.getPublic());
0N/A sig.update(data);
0N/A // verify needs to return false and not throw an Exception
0N/A if (sig.verify(signature)) {
0N/A throw new Exception("verification unexpectedly succeeded");
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A main(new TestKeyPairGenerator());
0N/A }
0N/A
0N/A public void main(Provider p) throws Exception {
0N/A long start = System.currentTimeMillis();
0N/A provider = p;
0N/A data = new byte[2048];
0N/A // keypair generation is very slow, test only a few short keys
0N/A int[] keyLengths = {512, 512, 1024};
0N/A BigInteger[] pubExps = {null, BigInteger.valueOf(3), null};
0N/A KeyPair[] keyPairs = new KeyPair[3];
0N/A new Random().nextBytes(data);
0N/A KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", provider);
0N/A for (int i = 0; i < keyLengths.length; i++) {
0N/A int len = keyLengths[i];
0N/A BigInteger exp = pubExps[i];
0N/A System.out.println("Generating " + len + " bit keypair...");
0N/A if (exp == null) {
0N/A kpg.initialize(len);
0N/A } else {
0N/A kpg.initialize(new RSAKeyGenParameterSpec(len, exp));
0N/A }
0N/A KeyPair kp = kpg.generateKeyPair();
0N/A keyPairs[i] = kp;
0N/A RSAPublicKey publicKey = (RSAPublicKey)kp.getPublic();
0N/A System.out.println(publicKey);
0N/A RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey)kp.getPrivate();
0N/A if (publicKey.getModulus().equals(privateKey.getModulus()) == false) {
0N/A throw new Exception("Moduli do not match");
0N/A }
0N/A if (publicKey.getPublicExponent().equals(privateKey.getPublicExponent()) == false) {
0N/A throw new Exception("Exponents do not match");
0N/A }
0N/A int keyLen = publicKey.getModulus().bitLength();
0N/A if ((keyLen > len) || (keyLen < len - 1)) {
0N/A throw new Exception("Incorrect key length: " + keyLen);
0N/A }
0N/A if (exp != null) {
0N/A if (exp.equals(publicKey.getPublicExponent()) == false) {
0N/A throw new Exception("Incorrect exponent");
0N/A }
0N/A }
0N/A test(privateKey, publicKey);
0N/A }
0N/A testInvalidSignature(keyPairs[0], keyPairs[1]);
0N/A testInvalidSignature(keyPairs[0], keyPairs[2]);
0N/A long stop = System.currentTimeMillis();
0N/A System.out.println("All tests passed (" + (stop - start) + " ms).");
0N/A }
0N/A}