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 package test.java.math.BigInteger; 36 37 import java.math.BigInteger; 38 39 import java.security.*; 40 import java.security.spec.*; 41 import java.util.Random; 42 import jdk.test.lib.RandomFactory; 43 44 public class ModPow65537 { 45 main(String[] args)46 public static void main(String[] args) throws Exception { 47 // BEGIN Android-removed: Test fails on Android. 48 // java.security.NoSuchProviderException: no such provider: SunRsaSign. 49 /* 50 // SunRsaSign uses BigInteger internally 51 KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign"); 52 kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(65537))); 53 KeyPair kp = kpg.generateKeyPair(); 54 testSigning(kp); 55 56 kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(65539))); 57 kp = kpg.generateKeyPair(); 58 testSigning(kp); 59 60 kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(3))); 61 kp = kpg.generateKeyPair(); 62 testSigning(kp); 63 64 // basic known answer test 65 BigInteger base = new 66 BigInteger("19058071224156864789844466979330892664777520457048234786139035643344145635582"); 67 BigInteger mod = new 68 BigInteger("75554098474976067521257305210610421240510163914613117319380559667371251381587"); 69 BigInteger exp1 = BigInteger.valueOf(65537); 70 BigInteger exp2 = BigInteger.valueOf(75537); 71 BigInteger exp3 = new BigInteger("13456870775607312149"); 72 73 BigInteger res1 = new 74 BigInteger("5770048609366563851320890693196148833634112303472168971638730461010114147506"); 75 BigInteger res2 = new 76 BigInteger("63446979364051087123350579021875958137036620431381329472348116892915461751531"); 77 BigInteger res3 = new 78 BigInteger("39016891919893878823999350081191675846357272199067075794096200770872982089502"); 79 80 if (base.modPow(exp1, mod).equals(res1) == false) { 81 throw new Exception("Error using " + exp1); 82 } 83 if (base.modPow(exp2, mod).equals(res2) == false) { 84 throw new Exception("Error using " + exp2); 85 } 86 if (base.modPow(exp3, mod).equals(res3) == false) { 87 throw new Exception("Error using " + exp3); 88 } 89 90 System.out.println("Passed"); 91 */ 92 // END Android-removed: Test fails on Android. 93 } 94 95 // BEGIN Android-removed: Test fails on Android. 96 // java.security.NoSuchProviderException: no such provider: SunRsaSign. 97 /* 98 private static void testSigning(KeyPair kp) throws Exception { 99 System.out.println(kp.getPublic()); 100 byte[] data = new byte[1024]; 101 Random random = RandomFactory.getRandom(); 102 random.nextBytes(data); 103 104 Signature sig = Signature.getInstance("SHA1withRSA", "SunRsaSign"); 105 sig.initSign(kp.getPrivate()); 106 sig.update(data); 107 byte[] sigBytes = sig.sign(); 108 109 sig.initVerify(kp.getPublic()); 110 sig.update(data); 111 if (sig.verify(sigBytes) == false) { 112 throw new Exception("signature verification failed"); 113 } 114 System.out.println("OK"); 115 } 116 */ 117 } 118