author | bpb |
Wed, 29 Apr 2015 16:34:49 -0700 | |
changeset 30048 | 3424bede284d |
parent 30046 | cf2c86e1819e |
child 30436 | 17827057ef5a |
permissions | -rw-r--r-- |
1826 | 1 |
/* |
29371
6f7f029a6b63
8074460: Always print seeds used in [Splittable]Random instances in java.math tests
bpb
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. |
1826 | 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 |
* |
|
5506 | 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. |
|
1826 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
30048
3424bede284d
8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents:
30046
diff
changeset
|
26 |
* @library /lib/testlibrary/ |
3424bede284d
8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents:
30046
diff
changeset
|
27 |
* @build jdk.testlibrary.* |
3424bede284d
8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents:
30046
diff
changeset
|
28 |
* @run main ModPow65537 |
3424bede284d
8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents:
30046
diff
changeset
|
29 |
* @bug 4891312 8074460 8078672 |
29371
6f7f029a6b63
8074460: Always print seeds used in [Splittable]Random instances in java.math tests
bpb
parents:
5506
diff
changeset
|
30 |
* @summary verify that modPow() not broken by the special case for 65537 (use -Dseed=X to set PRNG seed) |
1826 | 31 |
* @author Andreas Sterbenz |
30046 | 32 |
* @key randomness |
1826 | 33 |
*/ |
34 |
||
35 |
import java.math.BigInteger; |
|
36 |
||
37 |
import java.security.*; |
|
38 |
import java.security.spec.*; |
|
30048
3424bede284d
8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents:
30046
diff
changeset
|
39 |
import java.util.Random; |
1826 | 40 |
|
41 |
public class ModPow65537 { |
|
42 |
||
43 |
public static void main(String[] args) throws Exception { |
|
44 |
// SunRsaSign uses BigInteger internally |
|
45 |
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign"); |
|
46 |
kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(65537))); |
|
47 |
KeyPair kp = kpg.generateKeyPair(); |
|
48 |
testSigning(kp); |
|
49 |
||
50 |
kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(65539))); |
|
51 |
kp = kpg.generateKeyPair(); |
|
52 |
testSigning(kp); |
|
53 |
||
54 |
kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(3))); |
|
55 |
kp = kpg.generateKeyPair(); |
|
56 |
testSigning(kp); |
|
57 |
||
58 |
// basic known answer test |
|
59 |
BigInteger base = new BigInteger("19058071224156864789844466979330892664777520457048234786139035643344145635582"); |
|
60 |
BigInteger mod = new BigInteger("75554098474976067521257305210610421240510163914613117319380559667371251381587"); |
|
61 |
BigInteger exp1 = BigInteger.valueOf(65537); |
|
62 |
BigInteger exp2 = BigInteger.valueOf(75537); |
|
63 |
BigInteger exp3 = new BigInteger("13456870775607312149"); |
|
64 |
||
65 |
BigInteger res1 = new BigInteger("5770048609366563851320890693196148833634112303472168971638730461010114147506"); |
|
66 |
BigInteger res2 = new BigInteger("63446979364051087123350579021875958137036620431381329472348116892915461751531"); |
|
67 |
BigInteger res3 = new BigInteger("39016891919893878823999350081191675846357272199067075794096200770872982089502"); |
|
68 |
||
69 |
if (base.modPow(exp1, mod).equals(res1) == false) { |
|
70 |
throw new Exception("Error using " + exp1); |
|
71 |
} |
|
72 |
if (base.modPow(exp2, mod).equals(res2) == false) { |
|
73 |
throw new Exception("Error using " + exp2); |
|
74 |
} |
|
75 |
if (base.modPow(exp3, mod).equals(res3) == false) { |
|
76 |
throw new Exception("Error using " + exp3); |
|
77 |
} |
|
78 |
||
79 |
System.out.println("Passed"); |
|
80 |
} |
|
81 |
||
82 |
private static void testSigning(KeyPair kp) throws Exception { |
|
83 |
System.out.println(kp.getPublic()); |
|
84 |
byte[] data = new byte[1024]; |
|
30048
3424bede284d
8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents:
30046
diff
changeset
|
85 |
Random random = RandomFactory.getRandom(); |
3424bede284d
8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests
bpb
parents:
30046
diff
changeset
|
86 |
random.nextBytes(data); |
1826 | 87 |
|
88 |
Signature sig = Signature.getInstance("SHA1withRSA", "SunRsaSign"); |
|
89 |
sig.initSign(kp.getPrivate()); |
|
90 |
sig.update(data); |
|
91 |
byte[] sigBytes = sig.sign(); |
|
92 |
||
93 |
sig.initVerify(kp.getPublic()); |
|
94 |
sig.update(data); |
|
95 |
if (sig.verify(sigBytes) == false) { |
|
96 |
throw new Exception("signature verification failed"); |
|
97 |
} |
|
98 |
System.out.println("OK"); |
|
99 |
} |
|
100 |
||
101 |
} |