Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/KeyAgreement/DHKeyAgreement3.java
41155 views
1
/*
2
* Copyright (c) 1998, 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 0000000
27
* @summary DHKeyAgreement3
28
* @author Jan Luehe
29
*/
30
31
import java.io.*;
32
import java.math.BigInteger;
33
import java.security.*;
34
import java.security.spec.*;
35
import java.security.interfaces.*;
36
import java.util.HexFormat;
37
import javax.crypto.*;
38
import javax.crypto.spec.*;
39
import javax.crypto.interfaces.*;
40
41
/**
42
* This test utility executes the Diffie-Hellman key agreement protocol
43
* between 3 parties: Alice, Bob, and Carol.
44
*
45
* We use the same 1024 bit prime modulus and base generator that are used by
46
* SKIP.
47
*/
48
49
public class DHKeyAgreement3 {
50
51
// Hex formatter to upper case with ":" delimiter
52
private static final HexFormat HEX_FORMATTER = HexFormat.ofDelimiter(":").withUpperCase();
53
54
private DHKeyAgreement3() {}
55
56
public static void main(String argv[]) throws Exception {
57
DHKeyAgreement3 keyAgree = new DHKeyAgreement3();
58
keyAgree.run();
59
System.out.println("Test Passed");
60
}
61
62
private void run() throws Exception {
63
64
DHParameterSpec dhSkipParamSpec;
65
66
System.err.println("Using SKIP Diffie-Hellman parameters");
67
dhSkipParamSpec = new DHParameterSpec(skip1024Modulus, skip1024Base);
68
69
// Alice creates her own DH key pair
70
System.err.println("ALICE: Generate DH keypair ...");
71
KeyPairGenerator aliceKpairGen = KeyPairGenerator.getInstance("DH", "SunJCE");
72
aliceKpairGen.initialize(dhSkipParamSpec);
73
KeyPair aliceKpair = aliceKpairGen.generateKeyPair();
74
75
// Bob creates his own DH key pair
76
System.err.println("BOB: Generate DH keypair ...");
77
KeyPairGenerator bobKpairGen = KeyPairGenerator.getInstance("DH", "SunJCE");
78
bobKpairGen.initialize(dhSkipParamSpec);
79
KeyPair bobKpair = bobKpairGen.generateKeyPair();
80
81
// Carol creates her own DH key pair
82
System.err.println("CAROL: Generate DH keypair ...");
83
KeyPairGenerator carolKpairGen = KeyPairGenerator.getInstance("DH", "SunJCE");
84
carolKpairGen.initialize(dhSkipParamSpec);
85
KeyPair carolKpair = carolKpairGen.generateKeyPair();
86
87
88
// Alice initialize
89
System.err.println("ALICE: Initialize ...");
90
KeyAgreement aliceKeyAgree = KeyAgreement.getInstance("DH", "SunJCE");
91
aliceKeyAgree.init(aliceKpair.getPrivate());
92
93
// Bob initialize
94
System.err.println("BOB: Initialize ...");
95
KeyAgreement bobKeyAgree = KeyAgreement.getInstance("DH", "SunJCE");
96
bobKeyAgree.init(bobKpair.getPrivate());
97
98
// Carol initialize
99
System.err.println("CAROL: Initialize ...");
100
KeyAgreement carolKeyAgree = KeyAgreement.getInstance("DH", "SunJCE");
101
carolKeyAgree.init(carolKpair.getPrivate());
102
103
104
// Alice uses Carol's public key
105
Key ac = aliceKeyAgree.doPhase(carolKpair.getPublic(), false);
106
107
// Bob uses Alice's public key
108
Key ba = bobKeyAgree.doPhase(aliceKpair.getPublic(), false);
109
110
// Carol uses Bob's public key
111
Key cb = carolKeyAgree.doPhase(bobKpair.getPublic(), false);
112
113
114
// Alice uses Carol's result from above
115
aliceKeyAgree.doPhase(cb, true);
116
117
// Bob uses Alice's result from above
118
bobKeyAgree.doPhase(ac, true);
119
120
// Carol uses Bob's result from above
121
carolKeyAgree.doPhase(ba, true);
122
123
124
// Alice, Bob and Carol compute their secrets
125
byte[] aliceSharedSecret = aliceKeyAgree.generateSecret();
126
int aliceLen = aliceSharedSecret.length;
127
System.out.println("Alice secret: " + HEX_FORMATTER.formatHex(aliceSharedSecret));
128
129
byte[] bobSharedSecret = bobKeyAgree.generateSecret();
130
int bobLen = bobSharedSecret.length;
131
System.out.println("Bob secret: " + HEX_FORMATTER.formatHex(bobSharedSecret));
132
133
byte[] carolSharedSecret = carolKeyAgree.generateSecret();
134
int carolLen = carolSharedSecret.length;
135
System.out.println("Carol secret: " + HEX_FORMATTER.formatHex(carolSharedSecret));
136
137
138
// Compare Alice and Bob
139
if (aliceLen != bobLen) {
140
throw new Exception("Alice and Bob have different lengths");
141
}
142
for (int i=0; i<aliceLen; i++) {
143
if (aliceSharedSecret[i] != bobSharedSecret[i]) {
144
throw new Exception("Alice and Bob differ");
145
}
146
}
147
System.err.println("Alice and Bob are the same");
148
149
// Compare Bob and Carol
150
if (bobLen != carolLen) {
151
throw new Exception("Bob and Carol have different lengths");
152
}
153
for (int i=0; i<bobLen; i++) {
154
if (bobSharedSecret[i] != carolSharedSecret[i]) {
155
throw new Exception("Bob and Carol differ");
156
}
157
}
158
System.err.println("Bob and Carol are the same");
159
}
160
161
162
/*
163
* Converts a byte to hex digit and writes to the supplied buffer
164
*/
165
private void byte2hex(byte b, StringBuffer buf) {
166
char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
167
'9', 'A', 'B', 'C', 'D', 'E', 'F' };
168
int high = ((b & 0xf0) >> 4);
169
int low = (b & 0x0f);
170
buf.append(hexChars[high]);
171
buf.append(hexChars[low]);
172
}
173
174
/*
175
* Prints the usage of this test.
176
*/
177
private void usage() {
178
System.err.print("DHKeyAgreement usage: ");
179
System.err.println("[-gen]");
180
}
181
182
// The 1024 bit Diffie-Hellman modulus values used by SKIP
183
private static final byte skip1024ModulusBytes[] = {
184
(byte)0xF4, (byte)0x88, (byte)0xFD, (byte)0x58,
185
(byte)0x4E, (byte)0x49, (byte)0xDB, (byte)0xCD,
186
(byte)0x20, (byte)0xB4, (byte)0x9D, (byte)0xE4,
187
(byte)0x91, (byte)0x07, (byte)0x36, (byte)0x6B,
188
(byte)0x33, (byte)0x6C, (byte)0x38, (byte)0x0D,
189
(byte)0x45, (byte)0x1D, (byte)0x0F, (byte)0x7C,
190
(byte)0x88, (byte)0xB3, (byte)0x1C, (byte)0x7C,
191
(byte)0x5B, (byte)0x2D, (byte)0x8E, (byte)0xF6,
192
(byte)0xF3, (byte)0xC9, (byte)0x23, (byte)0xC0,
193
(byte)0x43, (byte)0xF0, (byte)0xA5, (byte)0x5B,
194
(byte)0x18, (byte)0x8D, (byte)0x8E, (byte)0xBB,
195
(byte)0x55, (byte)0x8C, (byte)0xB8, (byte)0x5D,
196
(byte)0x38, (byte)0xD3, (byte)0x34, (byte)0xFD,
197
(byte)0x7C, (byte)0x17, (byte)0x57, (byte)0x43,
198
(byte)0xA3, (byte)0x1D, (byte)0x18, (byte)0x6C,
199
(byte)0xDE, (byte)0x33, (byte)0x21, (byte)0x2C,
200
(byte)0xB5, (byte)0x2A, (byte)0xFF, (byte)0x3C,
201
(byte)0xE1, (byte)0xB1, (byte)0x29, (byte)0x40,
202
(byte)0x18, (byte)0x11, (byte)0x8D, (byte)0x7C,
203
(byte)0x84, (byte)0xA7, (byte)0x0A, (byte)0x72,
204
(byte)0xD6, (byte)0x86, (byte)0xC4, (byte)0x03,
205
(byte)0x19, (byte)0xC8, (byte)0x07, (byte)0x29,
206
(byte)0x7A, (byte)0xCA, (byte)0x95, (byte)0x0C,
207
(byte)0xD9, (byte)0x96, (byte)0x9F, (byte)0xAB,
208
(byte)0xD0, (byte)0x0A, (byte)0x50, (byte)0x9B,
209
(byte)0x02, (byte)0x46, (byte)0xD3, (byte)0x08,
210
(byte)0x3D, (byte)0x66, (byte)0xA4, (byte)0x5D,
211
(byte)0x41, (byte)0x9F, (byte)0x9C, (byte)0x7C,
212
(byte)0xBD, (byte)0x89, (byte)0x4B, (byte)0x22,
213
(byte)0x19, (byte)0x26, (byte)0xBA, (byte)0xAB,
214
(byte)0xA2, (byte)0x5E, (byte)0xC3, (byte)0x55,
215
(byte)0xE9, (byte)0x2F, (byte)0x78, (byte)0xC7
216
};
217
218
// The SKIP 1024 bit modulus
219
private static final BigInteger skip1024Modulus
220
= new BigInteger(1, skip1024ModulusBytes);
221
222
// The base used with the SKIP 1024 bit modulus
223
private static final BigInteger skip1024Base = BigInteger.valueOf(2);
224
}
225
226