Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/math/BigInteger/ModPow65537.java
38811 views
1
/*
2
* Copyright (c) 2003, 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
* @bug 4891312
27
* @summary verify that modPow() not broken by the special case for 65537
28
* @author Andreas Sterbenz
29
* @key randomness
30
*/
31
32
import java.math.BigInteger;
33
import java.util.*;
34
35
import java.security.*;
36
import java.security.spec.*;
37
38
public class ModPow65537 {
39
40
public static void main(String[] args) throws Exception {
41
// SunRsaSign uses BigInteger internally
42
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign");
43
kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(65537)));
44
KeyPair kp = kpg.generateKeyPair();
45
testSigning(kp);
46
47
kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(65539)));
48
kp = kpg.generateKeyPair();
49
testSigning(kp);
50
51
kpg.initialize(new RSAKeyGenParameterSpec(512, BigInteger.valueOf(3)));
52
kp = kpg.generateKeyPair();
53
testSigning(kp);
54
55
// basic known answer test
56
BigInteger base = new BigInteger("19058071224156864789844466979330892664777520457048234786139035643344145635582");
57
BigInteger mod = new BigInteger("75554098474976067521257305210610421240510163914613117319380559667371251381587");
58
BigInteger exp1 = BigInteger.valueOf(65537);
59
BigInteger exp2 = BigInteger.valueOf(75537);
60
BigInteger exp3 = new BigInteger("13456870775607312149");
61
62
BigInteger res1 = new BigInteger("5770048609366563851320890693196148833634112303472168971638730461010114147506");
63
BigInteger res2 = new BigInteger("63446979364051087123350579021875958137036620431381329472348116892915461751531");
64
BigInteger res3 = new BigInteger("39016891919893878823999350081191675846357272199067075794096200770872982089502");
65
66
if (base.modPow(exp1, mod).equals(res1) == false) {
67
throw new Exception("Error using " + exp1);
68
}
69
if (base.modPow(exp2, mod).equals(res2) == false) {
70
throw new Exception("Error using " + exp2);
71
}
72
if (base.modPow(exp3, mod).equals(res3) == false) {
73
throw new Exception("Error using " + exp3);
74
}
75
76
System.out.println("Passed");
77
}
78
79
private static void testSigning(KeyPair kp) throws Exception {
80
System.out.println(kp.getPublic());
81
byte[] data = new byte[1024];
82
new Random().nextBytes(data);
83
84
Signature sig = Signature.getInstance("SHA1withRSA", "SunRsaSign");
85
sig.initSign(kp.getPrivate());
86
sig.update(data);
87
byte[] sigBytes = sig.sign();
88
89
sig.initVerify(kp.getPublic());
90
sig.update(data);
91
if (sig.verify(sigBytes) == false) {
92
throw new Exception("signature verification failed");
93
}
94
System.out.println("OK");
95
}
96
97
}
98
99