Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/KeyAgreement/SupportedDHKeys.java
38855 views
1
/*
2
* Copyright (c) 2016, 2017, 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
27
* @summary Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
28
* @library ..
29
* @run main/othervm SupportedDHKeys
30
*/
31
32
import java.math.BigInteger;
33
34
import java.security.*;
35
import javax.crypto.*;
36
import javax.crypto.interfaces.*;
37
import javax.crypto.spec.*;
38
39
public class SupportedDHKeys extends PKCS11Test {
40
41
/*
42
* Sizes and values for various lengths.
43
*/
44
private enum SupportedKeySize {
45
dhp512(512), dhp768(768), dhp832(832),
46
dhp1024(1024), dhp1536(1536), dhp2048(2048);
47
48
// the underlying pkcs11 may not support the following sizes yet
49
//
50
// dhp3072(3072), dhp4096(4096), dhp6144(6144),
51
// dhp8192(8192);
52
53
final int primeSize;
54
55
SupportedKeySize(int primeSize) {
56
this.primeSize = primeSize;
57
}
58
}
59
60
@Override
61
public void main(Provider provider) throws Exception {
62
if (provider.getService("KeyPairGenerator", "DiffieHellman") == null) {
63
System.out.println("No support of DH KeyPairGenerator, skipping");
64
return;
65
}
66
67
for (SupportedKeySize keySize : SupportedKeySize.values()) {
68
System.out.println("Checking " + keySize.primeSize + " ...");
69
KeyPairGenerator kpg =
70
KeyPairGenerator.getInstance("DiffieHellman", provider);
71
kpg.initialize(keySize.primeSize);
72
KeyPair kp = kpg.generateKeyPair();
73
checkKeyPair(kp, keySize.primeSize, provider);
74
75
DHPublicKey publicKey = (DHPublicKey)kp.getPublic();
76
BigInteger p = publicKey.getParams().getP();
77
BigInteger g = publicKey.getParams().getG();
78
kpg.initialize(new DHParameterSpec(p, g));
79
kp = kpg.generateKeyPair();
80
checkKeyPair(kp, keySize.primeSize, provider);
81
}
82
}
83
84
private static void checkKeyPair(KeyPair kp, int pSize,
85
Provider provider) throws Exception {
86
87
DHPrivateKey privateKey = (DHPrivateKey)kp.getPrivate();
88
BigInteger p = privateKey.getParams().getP();
89
if (p.bitLength() != pSize) {
90
throw new Exception(
91
"Invalid modulus size: " + p.bitLength() + "/" + pSize);
92
}
93
94
// System.out.println("P(" + pSize + "): " + p.toString());
95
if (!p.isProbablePrime(128)) {
96
throw new Exception("Good luck, the modulus is composite!");
97
}
98
99
DHPublicKey publicKey = (DHPublicKey)kp.getPublic();
100
p = publicKey.getParams().getP();
101
if (p.bitLength() != pSize) {
102
throw new Exception(
103
"Invalid modulus size: " + p.bitLength() + "/" + pSize);
104
}
105
106
BigInteger leftOpen = BigInteger.ONE;
107
BigInteger rightOpen = p.subtract(BigInteger.ONE);
108
109
// ignore the private key range checking on Solaris at present
110
if (provider.getName().equals("SunPKCS11-Solaris") &&
111
!System.getProperty("os.name").equals("SunOS")) {
112
BigInteger x = privateKey.getX();
113
if ((x.compareTo(leftOpen) <= 0) ||
114
(x.compareTo(rightOpen) >= 0)) {
115
throw new Exception(
116
"X outside range [2, p - 2]: x: " + x + " p: " + p);
117
}
118
}
119
120
BigInteger y = publicKey.getY();
121
if ((y.compareTo(leftOpen) <= 0) ||
122
(y.compareTo(rightOpen) >= 0)) {
123
throw new Exception(
124
"Y outside range [2, p - 2]: y: " + y + " p: " + p);
125
}
126
}
127
128
public static void main(String[] args) throws Exception {
129
main(new SupportedDHKeys());
130
}
131
}
132
133