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