1 /* 2 * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @library /test/lib 27 * @build jdk.test.lib.RandomFactory 28 * @run main ModPow65537 29 * @bug 4891312 8074460 8078672 30 * @summary verify that modPow() not broken by the special case for 65537 (use -Dseed=X to set PRNG seed) 31 * @author Andreas Sterbenz 32 * @key randomness 33 */ 34 35 import java.math.BigInteger; 36 37 import java.security.*; 38 import java.security.spec.*; 39 import java.util.Random; 40 import jdk.test.lib.RandomFactory; 41 42 public class ModPow65537 { 43 44 public static void main(String[] args) throws Exception { 45 // SunRsaSign uses BigInteger internally 46 KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign"); 47 kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(65537))); 48 KeyPair kp = kpg.generateKeyPair(); 49 testSigning(kp); 50 51 kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(65539))); 52 kp = kpg.generateKeyPair(); 53 testSigning(kp); 54 55 kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(3))); 56 kp = kpg.generateKeyPair(); 57 testSigning(kp); 58 59 // basic known answer test 60 BigInteger base = new BigInteger("19058071224156864789844466979330892664777520457048234786139035643344145635582"); 61 BigInteger mod = new BigInteger("75554098474976067521257305210610421240510163914613117319380559667371251381587"); 62 BigInteger exp1 = BigInteger.valueOf(65537); 63 BigInteger exp2 = BigInteger.valueOf(75537); 64 BigInteger exp3 = new BigInteger("13456870775607312149"); 65 66 BigInteger res1 = new BigInteger("5770048609366563851320890693196148833634112303472168971638730461010114147506"); 67 BigInteger res2 = new BigInteger("63446979364051087123350579021875958137036620431381329472348116892915461751531"); 68 BigInteger res3 = new BigInteger("39016891919893878823999350081191675846357272199067075794096200770872982089502"); 69 70 if (base.modPow(exp1, mod).equals(res1) == false) { 71 throw new Exception("Error using " + exp1); 72 } 73 if (base.modPow(exp2, mod).equals(res2) == false) { 74 throw new Exception("Error using " + exp2); 75 } 76 if (base.modPow(exp3, mod).equals(res3) == false) { 77 throw new Exception("Error using " + exp3); 78 } 79 80 System.out.println("Passed"); 81 } 82 83 private static void testSigning(KeyPair kp) throws Exception { 84 System.out.println(kp.getPublic()); 85 byte[] data = new byte[1024]; 86 Random random = RandomFactory.getRandom(); 87 random.nextBytes(data); 88 89 Signature sig = Signature.getInstance("SHA1withRSA", "SunRsaSign"); 90 sig.initSign(kp.getPrivate()); 91 sig.update(data); 92 byte[] sigBytes = sig.sign(); 93 94 sig.initVerify(kp.getPublic()); 95 sig.update(data); 96 if (sig.verify(sigBytes) == false) { 97 throw new Exception("signature verification failed"); 98 } 99 System.out.println("OK"); 100 } 101 102 }