1 /* 2 * Copyright 2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4891312 27 * @summary verify that modPow() not broken by the special case for 65537 28 * @author Andreas Sterbenz 29 */ 30 31 import java.math.BigInteger; 32 import java.util.*; 33 34 import java.security.*; 35 import java.security.spec.*; 36 37 public class ModPow65537 { 38 39 public static void main(String[] args) throws Exception { 40 // SunRsaSign uses BigInteger internally 41 KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign"); 42 kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(65537))); 43 KeyPair kp = kpg.generateKeyPair(); 44 testSigning(kp); 45 46 kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(65539))); 47 kp = kpg.generateKeyPair(); 48 testSigning(kp); 49 50 kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(3))); 51 kp = kpg.generateKeyPair(); 52 testSigning(kp); 53 54 // basic known answer test 55 BigInteger base = new BigInteger("19058071224156864789844466979330892664777520457048234786139035643344145635582"); 56 BigInteger mod = new BigInteger("75554098474976067521257305210610421240510163914613117319380559667371251381587"); 57 BigInteger exp1 = BigInteger.valueOf(65537); 58 BigInteger exp2 = BigInteger.valueOf(75537); 59 BigInteger exp3 = new BigInteger("13456870775607312149"); 60 61 BigInteger res1 = new BigInteger("5770048609366563851320890693196148833634112303472168971638730461010114147506"); 62 BigInteger res2 = new BigInteger("63446979364051087123350579021875958137036620431381329472348116892915461751531"); 63 BigInteger res3 = new BigInteger("39016891919893878823999350081191675846357272199067075794096200770872982089502"); 64 65 if (base.modPow(exp1, mod).equals(res1) == false) { 66 throw new Exception("Error using " + exp1); 67 } 68 if (base.modPow(exp2, mod).equals(res2) == false) { 69 throw new Exception("Error using " + exp2); 70 } 71 if (base.modPow(exp3, mod).equals(res3) == false) { 72 throw new Exception("Error using " + exp3); 73 } 74 75 System.out.println("Passed"); 76 } 77 78 private static void testSigning(KeyPair kp) throws Exception { 79 System.out.println(kp.getPublic()); 80 byte[] data = new byte[1024]; 81 new Random().nextBytes(data); 82 83 Signature sig = Signature.getInstance("SHA1withRSA", "SunRsaSign"); 84 sig.initSign(kp.getPrivate()); 85 sig.update(data); 86 byte[] sigBytes = sig.sign(); 87 88 sig.initVerify(kp.getPublic()); 89 sig.update(data); 90 if (sig.verify(sigBytes) == false) { 91 throw new Exception("signature verification failed"); 92 } 93 System.out.println("OK"); 94 } 95 96 }