jdk/test/com/sun/crypto/provider/Cipher/PBE/CheckPBEKeySize.java
author vinnie
Mon, 07 Mar 2016 14:52:36 +0000
changeset 36248 29aaf8a0d7a9
child 42338 a60f280f803c
permissions -rw-r--r--
8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36248
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
     1
/*
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
     4
 *
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
     7
 * published by the Free Software Foundation.
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
     8
 *
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    13
 * accompanied this code).
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    14
 *
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    18
 *
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    21
 * questions.
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    22
 */
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    23
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    24
/**
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    25
 * @test
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    26
 * @bug 8151149
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    27
 * @modules java.base/com.sun.crypto.provider
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    28
 */
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    29
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    30
import java.lang.reflect.*;
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    31
import java.security.*;
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    32
import javax.crypto.*;
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    33
import javax.crypto.spec.*;
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    34
import com.sun.crypto.provider.*;
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    35
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    36
public class CheckPBEKeySize {
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    37
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    38
    private static final String ALGO = "PBEWithSHA1AndDESede";
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    39
    private static final int KEYSIZE = 112; // Triple DES effective key size
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    40
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    41
    public static final void main(String[] args) throws Exception {
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    42
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    43
        // Generate a PBE key
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    44
        SecretKeyFactory skFac = SecretKeyFactory.getInstance("PBE");
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    45
        SecretKey skey =
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    46
            skFac.generateSecret(new PBEKeySpec("test123".toCharArray()));
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    47
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    48
        // Initialize the PBE cipher
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    49
        Cipher cipher = Cipher.getInstance(ALGO);
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    50
        cipher.init(Cipher.ENCRYPT_MODE, skey);
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    51
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    52
        // Permit access to the Cipher.spi field (a CipherSpi object)
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    53
        Field spi = Cipher.class.getDeclaredField("spi");
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    54
        spi.setAccessible(true);
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    55
        Object value = spi.get(cipher);
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    56
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    57
        // Permit access to the CipherSpi.engineGetKeySize method
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    58
        Method engineGetKeySize =
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    59
            PKCS12PBECipherCore$PBEWithSHA1AndDESede.class
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    60
                .getDeclaredMethod("engineGetKeySize", Key.class);
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    61
        engineGetKeySize.setAccessible(true);
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    62
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    63
        // Check the key size
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    64
        int keySize = (int) engineGetKeySize.invoke(value, skey);
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    65
        if (keySize == KEYSIZE) {
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    66
            System.out.println(ALGO + ".engineGetKeySize returns " + keySize +
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    67
                " bits, as expected");
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    68
            System.out.println("OK");
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    69
        } else {
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    70
            throw new Exception("ERROR: " + ALGO + " key size is incorrect");
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    71
        }
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    72
    }
29aaf8a0d7a9 8151149: CipherSpi implementation of PBEWithSHA1AndDESede returns key size in bytes
vinnie
parents:
diff changeset
    73
}