1 /*
   2  * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
   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 4893959 6383200
  27  * @summary basic test for PBEWithSHA1AndDESede, PBEWithSHA1AndRC2_40/128
  28  *          and PBEWithSHA1AndRC4_40/128
  29  * @author Valerie Peng

  30  */
  31 
  32 import java.io.*;
  33 import java.util.*;
  34 import java.security.*;
  35 import javax.crypto.*;
  36 import javax.crypto.spec.*;
  37 import javax.crypto.interfaces.PBEKey;
  38 
  39 public class PKCS12Cipher {
  40 
  41     private static void runTest(String alg, byte[] plaintext,
  42                                 char[] password, Provider p)
  43         throws Exception {
  44         Cipher cipher = Cipher.getInstance(alg, p);
  45         PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
  46         SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
  47         AlgorithmParameters pbeParams = null;
  48         SecretKey key = keyFac.generateSecret(pbeKeySpec);
  49         cipher.init(Cipher.ENCRYPT_MODE, key, pbeParams);
  50         byte[] enc1 = cipher.doFinal(plaintext);
  51         byte[] enc2 = cipher.doFinal(plaintext);
  52         if (Arrays.equals(enc1, enc2) == false) {
  53             throw new Exception("Re-encryption test failed");
  54         }
  55         pbeParams = cipher.getParameters();
  56         cipher.init(Cipher.DECRYPT_MODE, key, pbeParams);
  57         byte[] dec = cipher.doFinal(enc1);
  58         if (Arrays.equals(plaintext, dec) == false) {
  59             throw new Exception("decryption test for " + alg + " failed");
  60         }
  61 
  62         PBEParameterSpec spec = (PBEParameterSpec)
  63             pbeParams.getParameterSpec(PBEParameterSpec.class);
  64         PBEKey key2 = new
  65             MyPBEKey(password, spec.getSalt(), spec.getIterationCount());
  66         cipher.init(Cipher.DECRYPT_MODE, key2, pbeParams);
  67         byte[] dec2 = cipher.doFinal(enc1);
  68         if (Arrays.equals(dec2, dec) == false) {
  69             throw new Exception("Re-decryption test#1 failed");
  70         }
  71 
  72         cipher.init(Cipher.DECRYPT_MODE, key2, (AlgorithmParameters) null);
  73         byte[] dec3 = cipher.doFinal(enc1);
  74         if (Arrays.equals(dec3, dec) == false) {
  75             throw new Exception("Re-decryption test#2 failed");
  76         }
  77 
  78         System.out.println("passed: " + alg);
  79     }
  80 
  81     public static void main(String[] argv) throws Exception {
  82         byte[] input = new byte[1024];
  83         new SecureRandom().nextBytes(input);
  84         char[] PASSWD = { 'p','a','s','s','w','o','r','d' };
  85         long start = System.currentTimeMillis();
  86         Provider p = Security.getProvider("SunJCE");
  87         System.out.println("Testing provider " + p.getName() + "...");
  88         runTest("PBEWithSHA1AndDESede", input, PASSWD, p);
  89         runTest("PBEWithSHA1AndRC2_40", input, PASSWD, p);
  90         runTest("PBEWithSHA1AndRC2_128", input, PASSWD, p);
  91         runTest("PBEWithSHA1AndRC4_40", input, PASSWD, p);
  92         runTest("PBEWithSHA1AndRC4_128", input, PASSWD, p);
  93         System.out.println("All tests passed");
  94         long stop = System.currentTimeMillis();
  95         System.out.println("Done (" + (stop - start) + " ms).");
  96     }
  97 }
  98 
  99 class MyPBEKey implements PBEKey {
 100     char[] passwd;
 101     byte[] salt;
 102     int iCount;
 103     MyPBEKey(char[] passwd, byte[] salt, int iCount) {
 104         this.passwd = passwd;
 105         this.salt = salt;
 106         this.iCount = iCount;
 107     }
 108     public char[] getPassword() { return passwd; }
 109     public byte[] getSalt() { return salt; }
 110     public int getIterationCount() { return iCount; }
 111     public String getAlgorithm() { return "PBE"; }
 112     public String getFormat() { return "RAW"; }
 113     public byte[] getEncoded() { return null; }
 114 }
--- EOF ---