Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/math/BigInteger/ModPow65537.java
38811 views
/*1* Copyright (c) 2003, 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 489131226* @summary verify that modPow() not broken by the special case for 6553727* @author Andreas Sterbenz28* @key randomness29*/3031import java.math.BigInteger;32import java.util.*;3334import java.security.*;35import java.security.spec.*;3637public class ModPow65537 {3839public static void main(String[] args) throws Exception {40// SunRsaSign uses BigInteger internally41KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign");42kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(65537)));43KeyPair kp = kpg.generateKeyPair();44testSigning(kp);4546kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(65539)));47kp = kpg.generateKeyPair();48testSigning(kp);4950kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(3)));51kp = kpg.generateKeyPair();52testSigning(kp);5354// basic known answer test55BigInteger base = new BigInteger("19058071224156864789844466979330892664777520457048234786139035643344145635582");56BigInteger mod = new BigInteger("75554098474976067521257305210610421240510163914613117319380559667371251381587");57BigInteger exp1 = BigInteger.valueOf(65537);58BigInteger exp2 = BigInteger.valueOf(75537);59BigInteger exp3 = new BigInteger("13456870775607312149");6061BigInteger res1 = new BigInteger("5770048609366563851320890693196148833634112303472168971638730461010114147506");62BigInteger res2 = new BigInteger("63446979364051087123350579021875958137036620431381329472348116892915461751531");63BigInteger res3 = new BigInteger("39016891919893878823999350081191675846357272199067075794096200770872982089502");6465if (base.modPow(exp1, mod).equals(res1) == false) {66throw new Exception("Error using " + exp1);67}68if (base.modPow(exp2, mod).equals(res2) == false) {69throw new Exception("Error using " + exp2);70}71if (base.modPow(exp3, mod).equals(res3) == false) {72throw new Exception("Error using " + exp3);73}7475System.out.println("Passed");76}7778private static void testSigning(KeyPair kp) throws Exception {79System.out.println(kp.getPublic());80byte[] data = new byte[1024];81new Random().nextBytes(data);8283Signature sig = Signature.getInstance("SHA1withRSA", "SunRsaSign");84sig.initSign(kp.getPrivate());85sig.update(data);86byte[] sigBytes = sig.sign();8788sig.initVerify(kp.getPublic());89sig.update(data);90if (sig.verify(sigBytes) == false) {91throw new Exception("signature verification failed");92}93System.out.println("OK");94}9596}979899