Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/KeyAgreement/SupportedDHParamGens.java
41155 views
1
/*
2
* Copyright (c) 2016, 2021, 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 8072452 8163498
27
* @summary Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
28
* This test has been split based on lower/higher key sizes in order to
29
* reduce individual execution times and run in parallel
30
* (see SupportedDHParamGensLongKey.java)
31
* @run main/timeout=300 SupportedDHParamGens 512
32
* @run main/timeout=300 SupportedDHParamGens 768
33
* @run main/timeout=300 SupportedDHParamGens 832
34
* @run main/timeout=300 SupportedDHParamGens 1024
35
* @run main/timeout=600 SupportedDHParamGens 2048
36
*/
37
38
import java.math.BigInteger;
39
40
import java.security.*;
41
import javax.crypto.*;
42
import javax.crypto.interfaces.*;
43
import javax.crypto.spec.*;
44
45
public class SupportedDHParamGens {
46
47
public static void main(String[] args) throws Exception {
48
int primeSize = Integer.valueOf(args[0]).intValue();
49
50
System.out.println("Checking " + primeSize + " ...");
51
AlgorithmParameterGenerator apg =
52
AlgorithmParameterGenerator.getInstance("DH", "SunJCE");
53
apg.init(primeSize);
54
AlgorithmParameters ap = apg.generateParameters();
55
DHParameterSpec spec = ap.getParameterSpec(DHParameterSpec.class);
56
57
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", "SunJCE");
58
kpg.initialize(spec);
59
KeyPair kp = kpg.generateKeyPair();
60
checkKeyPair(kp, primeSize);
61
}
62
63
private static void checkKeyPair(KeyPair kp, int pSize) throws Exception {
64
65
DHPrivateKey privateKey = (DHPrivateKey)kp.getPrivate();
66
BigInteger p = privateKey.getParams().getP();
67
if (p.bitLength() != pSize) {
68
throw new Exception(
69
"Invalid modulus size: " + p.bitLength() + "/" + pSize);
70
}
71
72
if (!p.isProbablePrime(128)) {
73
throw new Exception("Good luck, the modulus is composite!");
74
}
75
76
DHPublicKey publicKey = (DHPublicKey)kp.getPublic();
77
p = publicKey.getParams().getP();
78
if (p.bitLength() != pSize) {
79
throw new Exception(
80
"Invalid modulus size: " + p.bitLength() + "/" + pSize);
81
}
82
83
BigInteger leftOpen = BigInteger.ONE;
84
BigInteger rightOpen = p.subtract(BigInteger.ONE);
85
86
BigInteger x = privateKey.getX();
87
if ((x.compareTo(leftOpen) <= 0) ||
88
(x.compareTo(rightOpen) >= 0)) {
89
throw new Exception(
90
"X outside range [2, p - 2]: x: " + x + " p: " + p);
91
}
92
93
BigInteger y = publicKey.getY();
94
if ((y.compareTo(leftOpen) <= 0) ||
95
(y.compareTo(rightOpen) >= 0)) {
96
throw new Exception(
97
"Y outside range [2, p - 2]: x: " + x + " p: " + p);
98
}
99
}
100
}
101
102