8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
authorvinnie
Mon, 07 Mar 2016 14:52:36 +0000
changeset 36248 29aaf8a0d7a9
parent 36247 ca6922c2d951
child 36250 b1831ac3db35
8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes Reviewed-by: xuelei
jdk/src/java.base/share/classes/com/sun/crypto/provider/PKCS12PBECipherCore.java
jdk/test/com/sun/crypto/provider/Cipher/PBE/CheckPBEKeySize.java
--- a/jdk/src/java.base/share/classes/com/sun/crypto/provider/PKCS12PBECipherCore.java	Sat Mar 05 10:34:06 2016 +0800
+++ b/jdk/src/java.base/share/classes/com/sun/crypto/provider/PKCS12PBECipherCore.java	Mon Mar 07 14:52:36 2016 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -50,6 +50,7 @@
     private CipherCore cipher;
     private int blockSize;
     private int keySize;
+    private int keyLength; // in bits
     private String algo = null;
     private String pbeAlgo = null;
     private byte[] salt = null;
@@ -166,16 +167,18 @@
         throws NoSuchAlgorithmException {
 
         algo = symmCipherAlg;
+        keyLength = defKeySize * 8;
         if (algo.equals("RC4")) {
-            pbeAlgo = "PBEWithSHA1AndRC4_" + defKeySize * 8;
+            pbeAlgo = "PBEWithSHA1AndRC4_" + keyLength;
         } else {
             SymmetricCipher symmCipher = null;
             if (algo.equals("DESede")) {
                 symmCipher = new DESedeCrypt();
                 pbeAlgo = "PBEWithSHA1AndDESede";
+                keyLength = 112; // effective key length
             } else if (algo.equals("RC2")) {
                 symmCipher = new RC2Crypt();
-                pbeAlgo = "PBEWithSHA1AndRC2_" + defKeySize * 8;
+                pbeAlgo = "PBEWithSHA1AndRC2_" + keyLength;
             } else {
                 throw new NoSuchAlgorithmException("No Cipher implementation " +
                        "for PBEWithSHA1And" + algo);
@@ -406,7 +409,7 @@
     }
 
     int implGetKeySize(Key key) throws InvalidKeyException {
-        return keySize;
+        return keyLength;
     }
 
     byte[] implWrap(Key key) throws IllegalBlockSizeException,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/com/sun/crypto/provider/Cipher/PBE/CheckPBEKeySize.java	Mon Mar 07 14:52:36 2016 +0000
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug 8151149
+ * @modules java.base/com.sun.crypto.provider
+ */
+
+import java.lang.reflect.*;
+import java.security.*;
+import javax.crypto.*;
+import javax.crypto.spec.*;
+import com.sun.crypto.provider.*;
+
+public class CheckPBEKeySize {
+
+    private static final String ALGO = "PBEWithSHA1AndDESede";
+    private static final int KEYSIZE = 112; // Triple DES effective key size
+
+    public static final void main(String[] args) throws Exception {
+
+        // Generate a PBE key
+        SecretKeyFactory skFac = SecretKeyFactory.getInstance("PBE");
+        SecretKey skey =
+            skFac.generateSecret(new PBEKeySpec("test123".toCharArray()));
+
+        // Initialize the PBE cipher
+        Cipher cipher = Cipher.getInstance(ALGO);
+        cipher.init(Cipher.ENCRYPT_MODE, skey);
+
+        // Permit access to the Cipher.spi field (a CipherSpi object)
+        Field spi = Cipher.class.getDeclaredField("spi");
+        spi.setAccessible(true);
+        Object value = spi.get(cipher);
+
+        // Permit access to the CipherSpi.engineGetKeySize method
+        Method engineGetKeySize =
+            PKCS12PBECipherCore$PBEWithSHA1AndDESede.class
+                .getDeclaredMethod("engineGetKeySize", Key.class);
+        engineGetKeySize.setAccessible(true);
+
+        // Check the key size
+        int keySize = (int) engineGetKeySize.invoke(value, skey);
+        if (keySize == KEYSIZE) {
+            System.out.println(ALGO + ".engineGetKeySize returns " + keySize +
+                " bits, as expected");
+            System.out.println("OK");
+        } else {
+            throw new Exception("ERROR: " + ALGO + " key size is incorrect");
+        }
+    }
+}