|
| 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