Blame SOURCES/jdk8288985-rh2006351-chacha20-poly1305.patch

7b63c2
commit 8b3c31751693bc7de9487cb3a17daccd6c6f75f2
7b63c2
Author: duke <duke@openjdk.org>
7b63c2
Date:   Sat Jul 30 22:24:04 2022 +0000
7b63c2
7b63c2
    Backport b6bd190d8d10fdb177f9fb100c9f44c9f57a3cb5
7b63c2
7b63c2
diff --git a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11SecretKeyFactory.java b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11SecretKeyFactory.java
7b63c2
index 96db38828a1..8d1b8ccb0ae 100644
7b63c2
--- a/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11SecretKeyFactory.java
7b63c2
+++ b/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11SecretKeyFactory.java
7b63c2
@@ -1,5 +1,5 @@
7b63c2
 /*
7b63c2
- * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
7b63c2
+ * Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
7b63c2
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7b63c2
  *
7b63c2
  * This code is free software; you can redistribute it and/or modify it
7b63c2
@@ -69,6 +69,7 @@ final class P11SecretKeyFactory extends SecretKeyFactorySpi {
7b63c2
         addKeyType("AES",      CKK_AES);
7b63c2
         addKeyType("Blowfish", CKK_BLOWFISH);
7b63c2
         addKeyType("ChaCha20", CKK_CHACHA20);
7b63c2
+        addKeyType("ChaCha20-Poly1305", CKK_CHACHA20);
7b63c2
 
7b63c2
         // we don't implement RC2 or IDEA, but we want to be able to generate
7b63c2
         // keys for those SSL/TLS ciphersuites.
7b63c2
diff --git a/test/jdk/sun/security/pkcs11/tls/TestKeyMaterialChaCha20.java b/test/jdk/sun/security/pkcs11/tls/TestKeyMaterialChaCha20.java
7b63c2
new file mode 100644
7b63c2
index 00000000000..51471fca65a
7b63c2
--- /dev/null
7b63c2
+++ b/test/jdk/sun/security/pkcs11/tls/TestKeyMaterialChaCha20.java
7b63c2
@@ -0,0 +1,94 @@
7b63c2
+/*
7b63c2
+ * Copyright (c) 2022, Red Hat, Inc.
7b63c2
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7b63c2
+ *
7b63c2
+ * This code is free software; you can redistribute it and/or modify it
7b63c2
+ * under the terms of the GNU General Public License version 2 only, as
7b63c2
+ * published by the Free Software Foundation.
7b63c2
+ *
7b63c2
+ * This code is distributed in the hope that it will be useful, but WITHOUT
7b63c2
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7b63c2
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7b63c2
+ * version 2 for more details (a copy is included in the LICENSE file that
7b63c2
+ * accompanied this code).
7b63c2
+ *
7b63c2
+ * You should have received a copy of the GNU General Public License version
7b63c2
+ * 2 along with this work; if not, write to the Free Software Foundation,
7b63c2
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7b63c2
+ *
7b63c2
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7b63c2
+ * or visit www.oracle.com if you need additional information or have any
7b63c2
+ * questions.
7b63c2
+ */
7b63c2
+
7b63c2
+/*
7b63c2
+ * @test
7b63c2
+ * @bug 8288985
7b63c2
+ * @summary Tests that P11TlsKeyMaterialGenerator works with ChaCha20-Poly1305
7b63c2
+ * @library /test/lib ..
7b63c2
+ * @modules java.base/sun.security.internal.spec
7b63c2
+ *          jdk.crypto.cryptoki
7b63c2
+ * @run main/othervm TestKeyMaterialChaCha20
7b63c2
+ */
7b63c2
+
7b63c2
+import javax.crypto.KeyGenerator;
7b63c2
+import javax.crypto.SecretKey;
7b63c2
+import java.security.Provider;
7b63c2
+import java.security.NoSuchAlgorithmException;
7b63c2
+import sun.security.internal.spec.TlsRsaPremasterSecretParameterSpec;
7b63c2
+import sun.security.internal.spec.TlsMasterSecretParameterSpec;
7b63c2
+import sun.security.internal.spec.TlsKeyMaterialParameterSpec;
7b63c2
+
7b63c2
+
7b63c2
+public class TestKeyMaterialChaCha20 extends PKCS11Test {
7b63c2
+
7b63c2
+    public static void main(String[] args) throws Exception {
7b63c2
+        main(new TestKeyMaterialChaCha20(), args);
7b63c2
+    }
7b63c2
+
7b63c2
+    @Override
7b63c2
+    public void main(Provider provider) throws Exception {
7b63c2
+        KeyGenerator kg1, kg2, kg3;
7b63c2
+        try {
7b63c2
+            kg1 = KeyGenerator.getInstance("SunTlsRsaPremasterSecret", provider);
7b63c2
+        } catch (Exception e) {
7b63c2
+            System.out.println("Skipping, SunTlsRsaPremasterSecret KeyGenerator not supported");
7b63c2
+            return;
7b63c2
+        }
7b63c2
+        try {
7b63c2
+            kg2 = KeyGenerator.getInstance("SunTls12MasterSecret", provider);
7b63c2
+        } catch (Exception e) {
7b63c2
+            System.out.println("Skipping, SunTls12MasterSecret KeyGenerator not supported");
7b63c2
+            return;
7b63c2
+        }
7b63c2
+        try {
7b63c2
+            kg3 = KeyGenerator.getInstance("SunTls12KeyMaterial", provider);
7b63c2
+        } catch (Exception e) {
7b63c2
+            System.out.println("Skipping, SunTls12KeyMaterial KeyGenerator not supported");
7b63c2
+            return;
7b63c2
+        }
7b63c2
+
7b63c2
+        kg1.init(new TlsRsaPremasterSecretParameterSpec(0x0303, 0x0303));
7b63c2
+        SecretKey preMasterSecret = kg1.generateKey();
7b63c2
+
7b63c2
+        TlsMasterSecretParameterSpec spec = new TlsMasterSecretParameterSpec(
7b63c2
+            preMasterSecret,
7b63c2
+            3, 3,
7b63c2
+            new byte[32],
7b63c2
+            new byte[32],
7b63c2
+            "SHA-256", 32, 64);
7b63c2
+        kg2.init(spec);
7b63c2
+        SecretKey masterSecret = kg2.generateKey();
7b63c2
+
7b63c2
+        TlsKeyMaterialParameterSpec params = new TlsKeyMaterialParameterSpec(
7b63c2
+            masterSecret, 3, 3,
7b63c2
+            new byte[32],
7b63c2
+            new byte[32],
7b63c2
+            "ChaCha20-Poly1305", 32, 32,
7b63c2
+            12, 0,
7b63c2
+            "SHA-256", 32, 64);
7b63c2
+        kg3.init(params);
7b63c2
+        kg3.generateKey();
7b63c2
+    }
7b63c2
+
7b63c2
+}