test/jdk/com/sun/crypto/provider/Cipher/PBE/PKCS12Cipher.java
author coffeys
Thu, 23 Aug 2018 11:37:14 +0100
changeset 51504 c9a3e3cac9c7
parent 47216 71c04702a3d5
permissions -rw-r--r--
8209129: Further improvements to cipher buffer management Reviewed-by: weijun, igerasim
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 5506
diff changeset
    26
 * @bug 4893959 6383200
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 5506
diff changeset
    27
 * @summary basic test for PBEWithSHA1AndDESede, PBEWithSHA1AndRC2_40/128
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 5506
diff changeset
    28
 *          and PBEWithSHA1AndRC4_40/128
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Valerie Peng
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 14405
diff changeset
    30
 * @key randomness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.crypto.interfaces.PBEKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class PKCS12Cipher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static void runTest(String alg, byte[] plaintext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                                char[] password, Provider p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        Cipher cipher = Cipher.getInstance(alg, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        AlgorithmParameters pbeParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        SecretKey key = keyFac.generateSecret(pbeKeySpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        cipher.init(Cipher.ENCRYPT_MODE, key, pbeParams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        byte[] enc1 = cipher.doFinal(plaintext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        byte[] enc2 = cipher.doFinal(plaintext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        if (Arrays.equals(enc1, enc2) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            throw new Exception("Re-encryption test failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        pbeParams = cipher.getParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        cipher.init(Cipher.DECRYPT_MODE, key, pbeParams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        byte[] dec = cipher.doFinal(enc1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (Arrays.equals(plaintext, dec) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            throw new Exception("decryption test for " + alg + " failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        PBEParameterSpec spec = (PBEParameterSpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            pbeParams.getParameterSpec(PBEParameterSpec.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        PBEKey key2 = new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            MyPBEKey(password, spec.getSalt(), spec.getIterationCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        cipher.init(Cipher.DECRYPT_MODE, key2, pbeParams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        byte[] dec2 = cipher.doFinal(enc1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (Arrays.equals(dec2, dec) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            throw new Exception("Re-decryption test#1 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        cipher.init(Cipher.DECRYPT_MODE, key2, (AlgorithmParameters) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        byte[] dec3 = cipher.doFinal(enc1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (Arrays.equals(dec3, dec) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new Exception("Re-decryption test#2 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        System.out.println("passed: " + alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static void main(String[] argv) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        byte[] input = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        new SecureRandom().nextBytes(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        char[] PASSWD = { 'p','a','s','s','w','o','r','d' };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        long start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        Provider p = Security.getProvider("SunJCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        System.out.println("Testing provider " + p.getName() + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        runTest("PBEWithSHA1AndDESede", input, PASSWD, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        runTest("PBEWithSHA1AndRC2_40", input, PASSWD, p);
14405
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 5506
diff changeset
    91
        runTest("PBEWithSHA1AndRC2_128", input, PASSWD, p);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 5506
diff changeset
    92
        runTest("PBEWithSHA1AndRC4_40", input, PASSWD, p);
e7fff80005c1 6383200: PBE: need new algorithm support in password based encryption
vinnie
parents: 5506
diff changeset
    93
        runTest("PBEWithSHA1AndRC4_128", input, PASSWD, p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        System.out.println("All tests passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        long stop = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        System.out.println("Done (" + (stop - start) + " ms).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
class MyPBEKey implements PBEKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    char[] passwd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    byte[] salt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    int iCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    MyPBEKey(char[] passwd, byte[] salt, int iCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this.passwd = passwd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this.salt = salt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        this.iCount = iCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
51504
c9a3e3cac9c7 8209129: Further improvements to cipher buffer management
coffeys
parents: 47216
diff changeset
   109
    public char[] getPassword() { return passwd.clone(); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public byte[] getSalt() { return salt; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public int getIterationCount() { return iCount; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public String getAlgorithm() { return "PBE"; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public String getFormat() { return "RAW"; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public byte[] getEncoded() { return null; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
}