Skip to content

Commit bf5b0f9

Browse files
committed
8288985: P11TlsKeyMaterialGenerator should work with ChaCha20-Poly1305
Backport-of: b6bd190d8d10fdb177f9fb100c9f44c9f57a3cb5
1 parent bf74196 commit bf5b0f9

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11SecretKeyFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -69,6 +69,7 @@ final class P11SecretKeyFactory extends SecretKeyFactorySpi {
6969
addKeyType("AES", CKK_AES);
7070
addKeyType("Blowfish", CKK_BLOWFISH);
7171
addKeyType("ChaCha20", CKK_CHACHA20);
72+
addKeyType("ChaCha20-Poly1305", CKK_CHACHA20);
7273

7374
// we don't implement RC2 or IDEA, but we want to be able to generate
7475
// keys for those SSL/TLS ciphersuites.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (c) 2022, Red Hat, Inc.
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 8288985
27+
* @summary Tests that P11TlsKeyMaterialGenerator works with ChaCha20-Poly1305
28+
* @library /test/lib ..
29+
* @modules java.base/sun.security.internal.spec
30+
* jdk.crypto.cryptoki
31+
* @run main/othervm TestKeyMaterialChaCha20
32+
*/
33+
34+
import javax.crypto.KeyGenerator;
35+
import javax.crypto.SecretKey;
36+
import java.security.Provider;
37+
import java.security.NoSuchAlgorithmException;
38+
import sun.security.internal.spec.TlsRsaPremasterSecretParameterSpec;
39+
import sun.security.internal.spec.TlsMasterSecretParameterSpec;
40+
import sun.security.internal.spec.TlsKeyMaterialParameterSpec;
41+
42+
43+
public class TestKeyMaterialChaCha20 extends PKCS11Test {
44+
45+
public static void main(String[] args) throws Exception {
46+
main(new TestKeyMaterialChaCha20(), args);
47+
}
48+
49+
@Override
50+
public void main(Provider provider) throws Exception {
51+
KeyGenerator kg1, kg2, kg3;
52+
try {
53+
kg1 = KeyGenerator.getInstance("SunTlsRsaPremasterSecret", provider);
54+
} catch (Exception e) {
55+
System.out.println("Skipping, SunTlsRsaPremasterSecret KeyGenerator not supported");
56+
return;
57+
}
58+
try {
59+
kg2 = KeyGenerator.getInstance("SunTls12MasterSecret", provider);
60+
} catch (Exception e) {
61+
System.out.println("Skipping, SunTls12MasterSecret KeyGenerator not supported");
62+
return;
63+
}
64+
try {
65+
kg3 = KeyGenerator.getInstance("SunTls12KeyMaterial", provider);
66+
} catch (Exception e) {
67+
System.out.println("Skipping, SunTls12KeyMaterial KeyGenerator not supported");
68+
return;
69+
}
70+
71+
kg1.init(new TlsRsaPremasterSecretParameterSpec(0x0303, 0x0303));
72+
SecretKey preMasterSecret = kg1.generateKey();
73+
74+
TlsMasterSecretParameterSpec spec = new TlsMasterSecretParameterSpec(
75+
preMasterSecret,
76+
3, 3,
77+
new byte[32],
78+
new byte[32],
79+
"SHA-256", 32, 64);
80+
kg2.init(spec);
81+
SecretKey masterSecret = kg2.generateKey();
82+
83+
TlsKeyMaterialParameterSpec params = new TlsKeyMaterialParameterSpec(
84+
masterSecret, 3, 3,
85+
new byte[32],
86+
new byte[32],
87+
"ChaCha20-Poly1305", 32, 32,
88+
12, 0,
89+
"SHA-256", 32, 64);
90+
kg3.init(params);
91+
kg3.generateKey();
92+
}
93+
94+
}

0 commit comments

Comments
 (0)