jdk/test/com/sun/crypto/provider/CICO/PBEFunc/PBKDF2Wrapper.java
changeset 32766 2322e6c2a032
equal deleted inserted replaced
32765:b65c2f5d4d01 32766:2322e6c2a032
       
     1 /*
       
     2  * Copyright (c) 2007, 2015, 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 import java.security.GeneralSecurityException;
       
    25 import java.security.Provider;
       
    26 import java.security.Security;
       
    27 import javax.crypto.Cipher;
       
    28 import javax.crypto.SecretKey;
       
    29 import javax.crypto.SecretKeyFactory;
       
    30 import javax.crypto.spec.IvParameterSpec;
       
    31 import javax.crypto.spec.PBEKeySpec;
       
    32 import javax.crypto.spec.SecretKeySpec;
       
    33 
       
    34 /**
       
    35  * Wrapper class to test a given SecretKeyFactory.PBKDF2 algorithm.
       
    36  */
       
    37 public class PBKDF2Wrapper extends AbstractPBEWrapper {
       
    38     /**
       
    39      * Default salt size.
       
    40      */
       
    41     public static final int PBKDF2_SALT_SIZE = 64;
       
    42 
       
    43     /**
       
    44      * Default key length.
       
    45      */
       
    46     public static final int PKDF2_DEFAULT_KEY_LEN = 128;
       
    47 
       
    48     /**
       
    49      * Default transformation.
       
    50      */
       
    51     public static final String CIPHER_TRANSFORMATION = "AES/CBC/PKCS5Padding";
       
    52 
       
    53     /**
       
    54      * Algorithm name.
       
    55      */
       
    56     public static final String KEY_ALGORITHM = "AES";
       
    57 
       
    58     /**
       
    59      * Initialization vector length.
       
    60      */
       
    61     private static final int IV_LENGTH = 16;
       
    62 
       
    63     /**
       
    64      * The buffer with the IV.
       
    65      */
       
    66     private final byte[] iv;
       
    67 
       
    68     /**
       
    69      * PBKDF2Wrapper constructor. Instantiate Cipher using
       
    70      * "AES/CBC/PKCS5Padding" transformation. Generate a secret key using PKDF2
       
    71      * algorithms given in the "algo" parameter.
       
    72      *
       
    73      * @param algo AES-based PBE algorithm.
       
    74      * @param passwd password phrase.
       
    75      * @throws GeneralSecurityException all security exceptions are thrown.
       
    76      */
       
    77     public PBKDF2Wrapper(PBEAlgorithm algo, String passwd)
       
    78             throws GeneralSecurityException {
       
    79         super(algo, passwd, PBKDF2_SALT_SIZE);
       
    80         iv = TestUtilities.generateBytes(IV_LENGTH);
       
    81     }
       
    82 
       
    83     /**
       
    84      * Initiate the Cipher object for PBKDF2 algorithm using given "mode".
       
    85      *
       
    86      * @param mode Cipher mode: encrypt or decrypt
       
    87      * @return Cipher object for PBKDF2 algorithm
       
    88      * @throws GeneralSecurityException all security exceptions are thrown.
       
    89      */
       
    90     @Override
       
    91     protected Cipher initCipher(int mode) throws GeneralSecurityException {
       
    92         Provider provider = Security.getProvider("SunJCE");
       
    93         if (provider == null) {
       
    94             throw new RuntimeException("SunJCE provider does not exist.");
       
    95         }
       
    96         // Generate secret key
       
    97         PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(),
       
    98                 salt, DEFAULT_ITERATION, PKDF2_DEFAULT_KEY_LEN);
       
    99         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(baseAlgo);
       
   100         SecretKey key = keyFactory.generateSecret(pbeKeySpec);
       
   101 
       
   102         // get Cipher instance
       
   103         Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION, provider);
       
   104         cipher.init(mode,
       
   105                 new SecretKeySpec(key.getEncoded(),KEY_ALGORITHM),
       
   106                 new IvParameterSpec(iv));
       
   107         return cipher;
       
   108     }
       
   109 }