Skip to content

Commit aeb53e6

Browse files
author
Sibabrata Sahoo
committed
8308711: Develop additional Tests for KEM implementation
Reviewed-by: weijun
1 parent dcd9590 commit aeb53e6

File tree

3 files changed

+542
-0
lines changed

3 files changed

+542
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2023, 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 8297878
27+
* @summary Generate large number of Secret Keys using same KEM
28+
* @library /test/lib
29+
* @run main/othervm -Djava.security.egd=file:/dev/urandom GenLargeNumberOfKeys
30+
*/
31+
import java.security.KeyPair;
32+
import java.security.KeyPairGenerator;
33+
import java.util.Arrays;
34+
import javax.crypto.KEM;
35+
import javax.crypto.SecretKey;
36+
37+
import java.security.spec.ECGenParameterSpec;
38+
import jdk.test.lib.Asserts;
39+
40+
public class GenLargeNumberOfKeys {
41+
42+
private static final int COUNT = 1000;
43+
44+
public static void main(String[] args) throws Exception {
45+
KEM kem = KEM.getInstance("DHKEM");
46+
testAlgo(kem, "X448", null);
47+
testAlgo(kem, "EC", "secp521r1");
48+
}
49+
50+
private static void testAlgo(KEM kem, String algo, String curveId) throws Exception {
51+
KeyPair kp = genKeyPair(algo, curveId);
52+
KEM.Encapsulator e = kem.newEncapsulator(kp.getPublic());
53+
KEM.Decapsulator d = kem.newDecapsulator(kp.getPrivate());
54+
for (int i = 0; i < COUNT; i++) {
55+
test(e, d);
56+
}
57+
System.out.println(algo + ": test Successful");
58+
}
59+
60+
private static KeyPair genKeyPair(String algo, String curveId) throws Exception {
61+
KeyPairGenerator kpg = KeyPairGenerator.getInstance(algo);
62+
if (curveId != null) {
63+
kpg.initialize(new ECGenParameterSpec(curveId));
64+
}
65+
return kpg.generateKeyPair();
66+
}
67+
68+
private static void test(KEM.Encapsulator e, KEM.Decapsulator d)
69+
throws Exception {
70+
KEM.Encapsulated enc = e.encapsulate();
71+
SecretKey sk = enc.key();
72+
Asserts.assertEQ(d.encapsulationSize(), enc.encapsulation().length);
73+
Asserts.assertEQ(d.secretSize(), enc.key().getEncoded().length);
74+
Asserts.assertTrue(Arrays.equals(d.decapsulate(enc.encapsulation()).getEncoded(),
75+
sk.getEncoded()));
76+
// Repeated calls to encapsulation() on Encapsulated object don't change anything
77+
Asserts.assertTrue(Arrays.equals(d.decapsulate(enc.encapsulation()).getEncoded(),
78+
sk.getEncoded()));
79+
Asserts.assertTrue(Arrays.equals(d.decapsulate(enc.encapsulation()).getEncoded(),
80+
enc.key().getEncoded()));
81+
}
82+
83+
}
+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Copyright (c) 2023, 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 8297878
27+
* @summary KEM using KeyPair generated through SunPKCS11 provider using NSS
28+
* @library /test/lib ../../../sun/security/pkcs11
29+
*/
30+
import java.security.KeyPair;
31+
import java.security.KeyPairGenerator;
32+
import java.security.NoSuchAlgorithmException;
33+
import java.security.Provider;
34+
import java.security.PublicKey;
35+
import java.security.SecureRandom;
36+
import java.security.spec.ECGenParameterSpec;
37+
import java.util.Arrays;
38+
import javax.crypto.KEM;
39+
import javax.crypto.SecretKey;
40+
import jdk.test.lib.Asserts;
41+
42+
public class KemInterop extends PKCS11Test {
43+
44+
public static void main(String[] args) throws Exception {
45+
main(new KemInterop(), args);
46+
}
47+
48+
@Override
49+
public void main(Provider p) throws Exception {
50+
test("EC", "secp256r1", p);
51+
test("EC", "secp384r1", p);
52+
test("EC", "secp521r1", p);
53+
test("X25519", null, p);
54+
test("X448", null, p);
55+
test("XDH", null, p);
56+
}
57+
58+
private static void test(String algo, String curveId, Provider p) throws Exception {
59+
60+
@FunctionalInterface
61+
interface KemTest<KP, SR> {
62+
63+
void test(KP kp, SR scr);
64+
}
65+
66+
KemTest<KeyPair, SecureRandom> kemWithPKCSKeys = (kpr, scr) -> {
67+
try {
68+
KEM kem = KEM.getInstance("DHKEM", "SunJCE");
69+
KEM.Encapsulator encT = encapsulator(kem, kpr.getPublic(), scr);
70+
KEM.Encapsulator encT1 = encapsulator(kem, kpr.getPublic(), scr);
71+
KEM.Encapsulated enc = encT.encapsulate();
72+
KEM.Encapsulated enc1 = encT.encapsulate();
73+
KEM.Encapsulated enc2 = encT1.encapsulate();
74+
75+
Asserts.assertTrue(Arrays.equals(enc.key().getEncoded(), enc.key().getEncoded()));
76+
Asserts.assertTrue(Arrays.equals(enc.encapsulation(), enc.encapsulation()));
77+
78+
Asserts.assertFalse(Arrays.equals(enc.key().getEncoded(), enc1.key().getEncoded()));
79+
Asserts.assertFalse(Arrays.equals(enc.encapsulation(), enc1.encapsulation()));
80+
81+
Asserts.assertFalse(Arrays.equals(enc.key().getEncoded(), enc2.key().getEncoded()));
82+
Asserts.assertFalse(Arrays.equals(enc.encapsulation(), enc2.encapsulation()));
83+
84+
KEM.Decapsulator decT = kem.newDecapsulator(kpr.getPrivate());
85+
SecretKey dsk = decT.decapsulate(enc.encapsulation());
86+
Asserts.assertTrue(Arrays.equals(dsk.getEncoded(), enc.key().getEncoded()));
87+
88+
Asserts.assertEQ(encT.encapsulationSize(), enc.encapsulation().length);
89+
Asserts.assertEQ(encT.encapsulationSize(), decT.encapsulationSize());
90+
Asserts.assertEQ(encT.secretSize(), enc.key().getEncoded().length);
91+
Asserts.assertEQ(encT.secretSize(), decT.secretSize());
92+
Asserts.assertEQ(decT.secretSize(), dsk.getEncoded().length);
93+
Asserts.assertEQ(decT.secretSize(),
94+
decT.decapsulate(enc.encapsulation()).getEncoded().length);
95+
Asserts.assertEQ(decT.decapsulate(enc.encapsulation()).getEncoded().length,
96+
enc.key().getEncoded().length);
97+
98+
KEM.Encapsulated enc3 = encT.encapsulate(0, encT.secretSize(), "AES");
99+
KEM.Decapsulator decT1 = kem.newDecapsulator(kpr.getPrivate());
100+
SecretKey dsk1 = decT1.decapsulate(enc3.encapsulation(), 0, decT1.secretSize(), "AES");
101+
Asserts.assertTrue(Arrays.equals(dsk1.getEncoded(), enc3.key().getEncoded()));
102+
} catch (Exception e) {
103+
throw new RuntimeException(e);
104+
}
105+
System.out.println("Success");
106+
};
107+
108+
KeyPair kp = null;
109+
SecureRandom sr = null;
110+
try {
111+
System.out.print("Algo-" + algo + ":" + "Curve-" + curveId + ":");
112+
KeyPairGenerator kpg = KeyPairGenerator.getInstance(algo, p);
113+
try {
114+
sr = SecureRandom.getInstance("PKCS11");
115+
} catch (NoSuchAlgorithmException e) {
116+
// Get default SecureRandom incase PKCS11 not found
117+
sr = new SecureRandom();
118+
}
119+
if (curveId != null) {
120+
kpg.initialize(new ECGenParameterSpec(curveId), sr);
121+
}
122+
kp = kpg.generateKeyPair();
123+
} catch (NoSuchAlgorithmException e) {
124+
System.out.println("Unsupported. Test execution Ignored");
125+
return;
126+
}
127+
128+
kemWithPKCSKeys.test(kp, null);
129+
kemWithPKCSKeys.test(kp, sr);
130+
}
131+
132+
private static KEM.Encapsulator encapsulator(KEM kem, PublicKey pk, SecureRandom sr)
133+
throws Exception {
134+
if (sr == null) {
135+
return kem.newEncapsulator(pk);
136+
} else {
137+
return kem.newEncapsulator(pk, sr);
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)