Path: blob/master/test/jdk/java/math/BigInteger/ModPow65537.java
41149 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* @library /test/lib26* @build jdk.test.lib.RandomFactory27* @run main ModPow6553728* @bug 4891312 8074460 807867229* @summary verify that modPow() not broken by the special case for 65537 (use -Dseed=X to set PRNG seed)30* @author Andreas Sterbenz31* @key randomness32*/3334import java.math.BigInteger;3536import java.security.*;37import java.security.spec.*;38import java.util.Random;39import jdk.test.lib.RandomFactory;4041public class ModPow65537 {4243public static void main(String[] args) throws Exception {44// SunRsaSign uses BigInteger internally45KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign");46kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(65537)));47KeyPair kp = kpg.generateKeyPair();48testSigning(kp);4950kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(65539)));51kp = kpg.generateKeyPair();52testSigning(kp);5354kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(3)));55kp = kpg.generateKeyPair();56testSigning(kp);5758// basic known answer test59BigInteger base = new BigInteger("19058071224156864789844466979330892664777520457048234786139035643344145635582");60BigInteger mod = new BigInteger("75554098474976067521257305210610421240510163914613117319380559667371251381587");61BigInteger exp1 = BigInteger.valueOf(65537);62BigInteger exp2 = BigInteger.valueOf(75537);63BigInteger exp3 = new BigInteger("13456870775607312149");6465BigInteger res1 = new BigInteger("5770048609366563851320890693196148833634112303472168971638730461010114147506");66BigInteger res2 = new BigInteger("63446979364051087123350579021875958137036620431381329472348116892915461751531");67BigInteger res3 = new BigInteger("39016891919893878823999350081191675846357272199067075794096200770872982089502");6869if (base.modPow(exp1, mod).equals(res1) == false) {70throw new Exception("Error using " + exp1);71}72if (base.modPow(exp2, mod).equals(res2) == false) {73throw new Exception("Error using " + exp2);74}75if (base.modPow(exp3, mod).equals(res3) == false) {76throw new Exception("Error using " + exp3);77}7879System.out.println("Passed");80}8182private static void testSigning(KeyPair kp) throws Exception {83System.out.println(kp.getPublic());84byte[] data = new byte[1024];85Random random = RandomFactory.getRandom();86random.nextBytes(data);8788Signature sig = Signature.getInstance("SHA1withRSA", "SunRsaSign");89sig.initSign(kp.getPrivate());90sig.update(data);91byte[] sigBytes = sig.sign();9293sig.initVerify(kp.getPublic());94sig.update(data);95if (sig.verify(sigBytes) == false) {96throw new Exception("signature verification failed");97}98System.out.println("OK");99}100101}102103104