809N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
809N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
809N/A *
809N/A * This code is free software; you can redistribute it and/or modify it
809N/A * under the terms of the GNU General Public License version 2 only, as
809N/A * published by the Free Software Foundation.
809N/A *
809N/A * This code is distributed in the hope that it will be useful, but WITHOUT
809N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
809N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
809N/A * version 2 for more details (a copy is included in the LICENSE file that
809N/A * accompanied this code).
809N/A *
809N/A * You should have received a copy of the GNU General Public License version
809N/A * 2 along with this work; if not, write to the Free Software Foundation,
809N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
809N/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.
809N/A */
809N/A
809N/A/*
809N/A * @test
809N/A * @bug 4891312
809N/A * @summary verify that modPow() not broken by the special case for 65537
809N/A * @author Andreas Sterbenz
809N/A */
809N/A
809N/Aimport java.math.BigInteger;
809N/Aimport java.util.*;
809N/A
809N/Aimport java.security.*;
809N/Aimport java.security.spec.*;
809N/A
809N/Apublic class ModPow65537 {
809N/A
809N/A public static void main(String[] args) throws Exception {
809N/A // SunRsaSign uses BigInteger internally
809N/A KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign");
809N/A kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(65537)));
809N/A KeyPair kp = kpg.generateKeyPair();
809N/A testSigning(kp);
809N/A
809N/A kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(65539)));
809N/A kp = kpg.generateKeyPair();
809N/A testSigning(kp);
809N/A
809N/A kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(3)));
809N/A kp = kpg.generateKeyPair();
809N/A testSigning(kp);
809N/A
809N/A // basic known answer test
809N/A BigInteger base = new BigInteger("19058071224156864789844466979330892664777520457048234786139035643344145635582");
809N/A BigInteger mod = new BigInteger("75554098474976067521257305210610421240510163914613117319380559667371251381587");
809N/A BigInteger exp1 = BigInteger.valueOf(65537);
809N/A BigInteger exp2 = BigInteger.valueOf(75537);
809N/A BigInteger exp3 = new BigInteger("13456870775607312149");
809N/A
809N/A BigInteger res1 = new BigInteger("5770048609366563851320890693196148833634112303472168971638730461010114147506");
809N/A BigInteger res2 = new BigInteger("63446979364051087123350579021875958137036620431381329472348116892915461751531");
809N/A BigInteger res3 = new BigInteger("39016891919893878823999350081191675846357272199067075794096200770872982089502");
809N/A
809N/A if (base.modPow(exp1, mod).equals(res1) == false) {
809N/A throw new Exception("Error using " + exp1);
809N/A }
809N/A if (base.modPow(exp2, mod).equals(res2) == false) {
809N/A throw new Exception("Error using " + exp2);
809N/A }
809N/A if (base.modPow(exp3, mod).equals(res3) == false) {
809N/A throw new Exception("Error using " + exp3);
809N/A }
809N/A
809N/A System.out.println("Passed");
809N/A }
809N/A
809N/A private static void testSigning(KeyPair kp) throws Exception {
809N/A System.out.println(kp.getPublic());
809N/A byte[] data = new byte[1024];
809N/A new Random().nextBytes(data);
809N/A
809N/A Signature sig = Signature.getInstance("SHA1withRSA", "SunRsaSign");
809N/A sig.initSign(kp.getPrivate());
809N/A sig.update(data);
809N/A byte[] sigBytes = sig.sign();
809N/A
809N/A sig.initVerify(kp.getPublic());
809N/A sig.update(data);
809N/A if (sig.verify(sigBytes) == false) {
809N/A throw new Exception("signature verification failed");
809N/A }
809N/A System.out.println("OK");
809N/A }
809N/A
809N/A}