1 /*
   2  * Copyright (c) 2003, 2015, 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 ..
  27  * @bug 4891312 8074460
  28  * @summary verify that modPow() not broken by the special case for 65537 (use -Dseed=X to set PRNG seed)
  29  * @author Andreas Sterbenz
  30  */
  31 
  32 import java.math.BigInteger;
  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         RandomSeed rndSeed = new RandomSeed(false);
  82         System.out.println("Random number generator seed = " + rndSeed.getSeed());
  83         rndSeed.getRandom().nextBytes(data);
  84 
  85         Signature sig = Signature.getInstance("SHA1withRSA", "SunRsaSign");
  86         sig.initSign(kp.getPrivate());
  87         sig.update(data);
  88         byte[] sigBytes = sig.sign();
  89 
  90         sig.initVerify(kp.getPublic());
  91         sig.update(data);
  92         if (sig.verify(sigBytes) == false) {
  93             throw new Exception("signature verification failed");
  94         }
  95         System.out.println("OK");
  96     }
  97 
  98 }