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/TestInterop.java
38855 views
1
/*
2
* Copyright (c) 2012, 2016, 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 7146728
27
* @summary Interop test for DH with secret that has a leading 0x00 byte
28
* @library ..
29
* @run main/othervm TestInterop
30
* @run main/othervm TestInterop sm
31
*/
32
import java.math.BigInteger;
33
import java.security.KeyFactory;
34
import java.security.PrivateKey;
35
import java.security.Provider;
36
import java.security.PublicKey;
37
import java.util.Arrays;
38
import javax.crypto.KeyAgreement;
39
import javax.crypto.spec.DHPrivateKeySpec;
40
import javax.crypto.spec.DHPublicKeySpec;
41
42
public class TestInterop extends PKCS11Test {
43
44
private final static BigInteger p = new BigInteger
45
("171718397966129586011229151993178480901904202533705695869569760169920539"
46
+ "80807543778874708672297590042574075430109846864794139516459381007417046"
47
+ "27996080624930219892858374168155487210358743785481212360509485282294161"
48
+ "39585571568998066586304075565145536350296006867635076744949977849997684"
49
+ "222020336013226588207303");
50
51
private final static BigInteger g = new BigInteger("2");
52
53
private final static BigInteger ya = new BigInteger
54
("687709211571508809414670982463565909269384277848448625781941269577397703"
55
+ "73675199968849153119146758339814638228795348558483510369322822476757204"
56
+ "22158455966026517829008713407587339322132253724742557954802911059639161"
57
+ "24827916158465757962384625410294483756242900146397201260757102085985457"
58
+ "09397033481077351036224");
59
60
private final static BigInteger xa = new BigInteger
61
("104917367119952955556289227181599819745346393858545449202252025137706135"
62
+ "98100778613457655440586438263591136003106529323555991109623536177695714"
63
+ "66884181531401472902830508361532232717792847436112280721439936797741371"
64
+ "245140912614191507");
65
66
private final static BigInteger yb = new BigInteger
67
("163887874871842952463100699681506173424091615364591742415764095471629919"
68
+ "08421025296419917755446931473037086355546823601999684501737493240373415"
69
+ "65608293667837249198973539289354492348897732633852665609611113031379864"
70
+ "58514616034107537409230452318065341748503347627733368519091332060477528"
71
+ "173423377887175351037810");
72
73
private final static BigInteger xb = new BigInteger
74
("127757517533485947079959908591028646859165238853082197617179368337276371"
75
+ "51601819447716934542027725311863797141734616730248519214531856941516613"
76
+ "30313414180008978013330410484011186019824874948204261839391153650949864"
77
+ "429505597086564709");
78
79
@Override
80
public void main(Provider prov) throws Exception {
81
if (prov.getService("KeyAgreement", "DH") == null) {
82
System.out.println("DH not supported, skipping");
83
return;
84
}
85
try {
86
System.out.println("testing generateSecret()");
87
88
DHPublicKeySpec publicSpec;
89
DHPrivateKeySpec privateSpec;
90
KeyFactory kf = KeyFactory.getInstance("DH");
91
KeyAgreement ka = KeyAgreement.getInstance("DH", prov);
92
KeyAgreement kbSunJCE = KeyAgreement.getInstance("DH", "SunJCE");
93
DHPrivateKeySpec privSpecA = new DHPrivateKeySpec(xa, p, g);
94
DHPublicKeySpec pubSpecA = new DHPublicKeySpec(ya, p, g);
95
PrivateKey privA = kf.generatePrivate(privSpecA);
96
PublicKey pubA = kf.generatePublic(pubSpecA);
97
98
DHPrivateKeySpec privSpecB = new DHPrivateKeySpec(xb, p, g);
99
DHPublicKeySpec pubSpecB = new DHPublicKeySpec(yb, p, g);
100
PrivateKey privB = kf.generatePrivate(privSpecB);
101
PublicKey pubB = kf.generatePublic(pubSpecB);
102
103
ka.init(privA);
104
ka.doPhase(pubB, true);
105
byte[] n1 = ka.generateSecret();
106
107
kbSunJCE.init(privB);
108
kbSunJCE.doPhase(pubA, true);
109
byte[] n2 = kbSunJCE.generateSecret();
110
111
if (Arrays.equals(n1, n2) == false) {
112
throw new Exception("values mismatch!");
113
} else {
114
System.out.println("values: same");
115
}
116
117
System.out.println("testing generateSecret(byte[], int)");
118
byte[] n3 = new byte[n1.length];
119
ka.init(privB);
120
ka.doPhase(pubA, true);
121
int n3Len = ka.generateSecret(n3, 0);
122
if (n3Len != n3.length) {
123
throw new Exception("PKCS11 Length mismatch!");
124
} else System.out.println("PKCS11 Length: ok");
125
byte[] n4 = new byte[n2.length];
126
kbSunJCE.init(privA);
127
kbSunJCE.doPhase(pubB, true);
128
int n4Len = kbSunJCE.generateSecret(n4, 0);
129
if (n4Len != n4.length) {
130
throw new Exception("SunJCE Length mismatch!");
131
} else System.out.println("SunJCE Length: ok");
132
133
if (Arrays.equals(n3, n4) == false) {
134
throw new Exception("values mismatch! ");
135
} else {
136
System.out.println("values: same");
137
}
138
} catch (Exception ex) {
139
System.out.println("Unexpected ex: " + ex);
140
ex.printStackTrace();
141
throw ex;
142
}
143
}
144
145
public static void main(String[] args) throws Exception {
146
main(new TestInterop(), args);
147
}
148
}
149
150