Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/com/sun/crypto/provider/KeyAgreement/DHKeyAgreementPadding.java
66646 views
1
/*
2
* Copyright (c) 2022, 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 8281628
27
* @library /test/lib
28
* @summary ensure padding bytes are always added when generated secret
29
* is smaller than buffer size.
30
*/
31
32
import javax.crypto.KeyAgreement;
33
import java.security.KeyPair;
34
import java.security.KeyPairGenerator;
35
import java.util.Arrays;
36
import java.util.HexFormat;
37
38
public class DHKeyAgreementPadding {
39
40
public static void main(String[] args) throws Exception {
41
42
byte[] aliceSecret = new byte[80];
43
byte[] bobSecret = new byte[80];
44
45
KeyAgreement alice = KeyAgreement.getInstance("DiffieHellman");
46
KeyAgreement bob = KeyAgreement.getInstance("DiffieHellman");
47
48
// The probability of an error is 0.2% or 1/500. Try more times.
49
for (int i = 0; i < 5000; i++) {
50
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("DiffieHellman");
51
keyPairGen.initialize(512);
52
KeyPair aliceKeyPair = keyPairGen.generateKeyPair();
53
KeyPair bobKeyPair = keyPairGen.generateKeyPair();
54
55
// Different stale data
56
Arrays.fill(aliceSecret, (byte)'a');
57
Arrays.fill(bobSecret, (byte)'b');
58
59
alice.init(aliceKeyPair.getPrivate());
60
alice.doPhase(bobKeyPair.getPublic(), true);
61
int aliceLen = alice.generateSecret(aliceSecret, 0);
62
63
bob.init(bobKeyPair.getPrivate());
64
bob.doPhase(aliceKeyPair.getPublic(), true);
65
int bobLen = bob.generateSecret(bobSecret, 0);
66
67
if (!Arrays.equals(aliceSecret, 0, aliceLen, bobSecret, 0, bobLen)) {
68
System.out.println(HexFormat.ofDelimiter(":").formatHex(aliceSecret, 0, aliceLen));
69
System.out.println(HexFormat.ofDelimiter(":").formatHex(bobSecret, 0, bobLen));
70
throw new RuntimeException("Different secrets observed at runs #" + i);
71
}
72
}
73
}
74
}
75
76