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/TestDH.java
38855 views
1
/*
2
* Copyright (c) 2003, 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 4921804 6324825
27
* @summary Verify that DH works properly
28
* @author Andreas Sterbenz
29
* @library ..
30
* @run main/othervm -Djdk.crypto.KeyAgreement.legacyKDF=true TestDH
31
* @run main/othervm -Djdk.crypto.KeyAgreement.legacyKDF=true TestDH sm
32
*/
33
34
import java.security.KeyPair;
35
import java.security.KeyPairGenerator;
36
import java.security.Provider;
37
import java.util.Arrays;
38
import javax.crypto.KeyAgreement;
39
import javax.crypto.SecretKey;
40
41
public class TestDH extends PKCS11Test {
42
43
@Override
44
public void main(Provider p) throws Exception {
45
if (p.getService("KeyAgreement", "DH") == null) {
46
System.out.println("DH not supported, skipping");
47
return;
48
}
49
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", p);
50
kpg.initialize(512);
51
KeyPair kp1 = kpg.generateKeyPair();
52
KeyPair kp2 = kpg.generateKeyPair();
53
54
KeyAgreement ka1, ka2;
55
ka1 = KeyAgreement.getInstance("DH", p);
56
ka1.init(kp1.getPrivate());
57
ka1.doPhase(kp2.getPublic(), true);
58
System.out.println("Derive 1...");
59
byte[] secret1 = ka1.generateSecret();
60
61
ka1.init(kp2.getPrivate());
62
ka1.doPhase(kp1.getPublic(), true);
63
System.out.println("Derive 2...");
64
byte[] secret2 = ka1.generateSecret();
65
66
if (Arrays.equals(secret1, secret2) == false) {
67
throw new Exception("Secrets (1,2) do not match");
68
}
69
70
ka2 = KeyAgreement.getInstance("DH", "SunJCE");
71
ka2.init(kp1.getPrivate());
72
ka2.doPhase(kp2.getPublic(), true);
73
System.out.println("Derive 3...");
74
byte[] secret3 = ka2.generateSecret();
75
76
if (Arrays.equals(secret1, secret3) == false) {
77
throw new Exception("Secrets (1,3) do not match");
78
}
79
80
ka2.init(kp2.getPrivate());
81
ka2.doPhase(kp1.getPublic(), true);
82
System.out.println("Derive 4...");
83
byte[] secret4 = ka2.generateSecret();
84
85
if (Arrays.equals(secret1, secret4) == false) {
86
throw new Exception("Secrets (1,4) do not match");
87
}
88
89
testAlgorithm(ka2, kp2, ka1, kp1, "DES");
90
testAlgorithm(ka2, kp2, ka1, kp1, "DESede");
91
// testAlgorithm(ka2, kp2, ka1, kp1, "AES");
92
// testAlgorithm(ka2, kp2, ka1, kp1, "RC4");
93
testAlgorithm(ka2, kp2, ka1, kp1, "Blowfish");
94
testAlgorithm(ka2, kp2, ka1, kp1, "TlsPremasterSecret");
95
}
96
97
private static void testAlgorithm(KeyAgreement ka1, KeyPair kp1,
98
KeyAgreement ka2, KeyPair kp2, String algorithm) throws Exception {
99
SecretKey key1;
100
101
ka1.init(kp1.getPrivate());
102
ka1.doPhase(kp2.getPublic(), true);
103
System.out.println("Derive " + algorithm + " using SunJCE...");
104
key1 = ka1.generateSecret(algorithm);
105
106
ka2.init(kp1.getPrivate());
107
ka2.doPhase(kp2.getPublic(), true);
108
System.out.println("Derive " + algorithm + " using PKCS#11...");
109
SecretKey key2 = ka2.generateSecret(algorithm);
110
111
byte[] b1 = key1.getEncoded();
112
byte[] b2 = key2.getEncoded();
113
114
if (Arrays.equals(b1, b2) == false) {
115
System.out.println(b1.length + " bytes: " + toString(b1));
116
System.out.println(b2.length + " bytes: " + toString(b2));
117
throw new Exception(algorithm + " secret mismatch");
118
}
119
}
120
121
public static void main(String[] args) throws Exception {
122
main(new TestDH(), args);
123
}
124
125
}
126
127