Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/KeyAgreement/KeyAgreementTest.java
41149 views
1
/*
2
* Copyright (c) 2018, 2020, 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 4936763 8184359 8205476 8226307
27
* @summary KeyAgreement Test with all supported algorithms from JCE.
28
* Arguments order <KeyExchangeAlgorithm> <KeyGenAlgorithm> <Provider>
29
* It removes com/sun/crypto/provider/KeyAgreement/DHGenSecretKey.java
30
* as the same functionality for DiffieHellman is covered along with
31
* this test file was covered before with JDK-4936763.
32
* @run main/othervm -Djdk.crypto.KeyAgreement.legacyKDF=true KeyAgreementTest
33
* DiffieHellman DH SunJCE
34
* @run main KeyAgreementTest ECDH EC SunEC
35
* @run main KeyAgreementTest XDH XDH SunEC
36
*/
37
import java.security.KeyPair;
38
import java.security.KeyPairGenerator;
39
import java.security.spec.NamedParameterSpec;
40
import java.security.spec.AlgorithmParameterSpec;
41
import java.security.spec.ECGenParameterSpec;
42
import java.util.ArrayList;
43
import java.util.Arrays;
44
import java.util.List;
45
import javax.crypto.KeyAgreement;
46
import javax.crypto.spec.DHGenParameterSpec;
47
48
public class KeyAgreementTest {
49
50
public static void main(String[] args) throws Exception {
51
52
String kaAlgo = args[0];
53
String kpgAlgo = args[1];
54
String provider = args[2];
55
System.out.println("Testing " + kaAlgo);
56
AlgoSpec aSpec = AlgoSpec.valueOf(AlgoSpec.class, kaAlgo);
57
List<AlgorithmParameterSpec> specs = aSpec.getAlgorithmParameterSpecs();
58
for (AlgorithmParameterSpec spec : specs) {
59
testKeyAgreement(provider, kaAlgo, kpgAlgo, spec);
60
}
61
}
62
63
/**
64
* Generate AlgorithmParameterSpec using all possible supported curve for
65
* KeyExchangeAlgorithm.
66
*/
67
private enum AlgoSpec {
68
// EC curve supported for KeyGeneration can found between intersection
69
// of curves define in
70
// "java.base/share/classes/sun/security/util/CurveDB.java"
71
72
ECDH("secp256r1", "secp384r1", "secp521r1"),
73
XDH("X25519", "X448", "x25519"),
74
// There is no curve for DiffieHellman
75
DiffieHellman(new String[]{});
76
77
private final List<AlgorithmParameterSpec> specs = new ArrayList<>();
78
79
private AlgoSpec(String... curves) {
80
// Generate AlgorithmParameterSpec for each KeyExchangeAlgorithm
81
for (String crv : curves) {
82
switch (this.name()) {
83
case "ECDH":
84
specs.add(new ECGenParameterSpec(crv));
85
break;
86
case "XDH":
87
specs.add(new NamedParameterSpec(crv));
88
break;
89
case "DiffieHellman":
90
specs.add(new DHGenParameterSpec(512, 64));
91
break;
92
default:
93
throw new RuntimeException("Invalid Algo name "
94
+ this.name());
95
}
96
}
97
}
98
99
public List<AlgorithmParameterSpec> getAlgorithmParameterSpecs() {
100
return this.specs;
101
}
102
}
103
104
/**
105
* Perform KeyAgreement operation
106
*/
107
private static void testKeyAgreement(String provider, String kaAlgo,
108
String kpgAlgo, AlgorithmParameterSpec spec) throws Exception {
109
110
KeyPairGenerator kpg = KeyPairGenerator.getInstance(kpgAlgo, provider);
111
kpg.initialize(spec);
112
if (spec instanceof ECGenParameterSpec) {
113
System.out.println("Testing curve: " +
114
((ECGenParameterSpec)spec).getName());
115
} else if (spec instanceof NamedParameterSpec) {
116
System.out.println("Testing curve: " +
117
((NamedParameterSpec)spec).getName());
118
}
119
KeyPair kp1 = kpg.generateKeyPair();
120
KeyPair kp2 = kpg.generateKeyPair();
121
122
// Uses KeyAgreement based on Provider search order.
123
KeyAgreement ka1 = KeyAgreement.getInstance(kaAlgo);
124
ka1.init(kp1.getPrivate());
125
ka1.doPhase(kp2.getPublic(), true);
126
byte[] secret1 = ka1.generateSecret();
127
128
// Uses SunJCE provider
129
KeyAgreement ka2 = KeyAgreement.getInstance(kaAlgo, provider);
130
ka2.init(kp2.getPrivate());
131
ka2.doPhase(kp1.getPublic(), true);
132
// Keeping the legacy generateSecret method for DiffieHellman as it was
133
// defined in removed Test file from JDK-4936763,
134
// com/sun/crypto/provider/KeyAgreement/DHGenSecretKey.java.
135
byte[] secret2 = "DiffieHellman".equals(kaAlgo)
136
? ka2.generateSecret("AES").getEncoded() : ka2.generateSecret();
137
138
// With related keypairs, each provider should generate same
139
// KeyAgreement secret.
140
if (!Arrays.equals(secret1, secret2)) {
141
throw new Exception("KeyAgreement secret mismatch.");
142
}
143
144
// ensure that a new secret cannot be produced before the next doPhase
145
try {
146
ka2.generateSecret();
147
throw new RuntimeException("state not reset");
148
} catch (IllegalStateException ex) {
149
// this is expected
150
}
151
152
// calling doPhase and then generateSecret should succeed
153
ka2.doPhase(kp1.getPublic(), true);
154
ka2.generateSecret();
155
}
156
}
157
158