Path: blob/master/test/jdk/com/sun/crypto/provider/CICO/PBEFunc/PBKDF2Wrapper.java
41161 views
/*1* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.security.GeneralSecurityException;24import java.security.Provider;25import java.security.Security;26import javax.crypto.Cipher;27import javax.crypto.SecretKey;28import javax.crypto.SecretKeyFactory;29import javax.crypto.spec.IvParameterSpec;30import javax.crypto.spec.PBEKeySpec;31import javax.crypto.spec.SecretKeySpec;3233/**34* Wrapper class to test a given SecretKeyFactory.PBKDF2 algorithm.35*/36public class PBKDF2Wrapper extends AbstractPBEWrapper {37/**38* Default salt size.39*/40public static final int PBKDF2_SALT_SIZE = 64;4142/**43* Default key length.44*/45public static final int PKDF2_DEFAULT_KEY_LEN = 128;4647/**48* Default transformation.49*/50public static final String CIPHER_TRANSFORMATION = "AES/CBC/PKCS5Padding";5152/**53* Algorithm name.54*/55public static final String KEY_ALGORITHM = "AES";5657/**58* Initialization vector length.59*/60private static final int IV_LENGTH = 16;6162/**63* The buffer with the IV.64*/65private final byte[] iv;6667/**68* PBKDF2Wrapper constructor. Instantiate Cipher using69* "AES/CBC/PKCS5Padding" transformation. Generate a secret key using PKDF270* algorithms given in the "algo" parameter.71*72* @param algo AES-based PBE algorithm.73* @param passwd password phrase.74* @throws GeneralSecurityException all security exceptions are thrown.75*/76public PBKDF2Wrapper(PBEAlgorithm algo, String passwd)77throws GeneralSecurityException {78super(algo, passwd, PBKDF2_SALT_SIZE);79iv = TestUtilities.generateBytes(IV_LENGTH);80}8182/**83* Initiate the Cipher object for PBKDF2 algorithm using given "mode".84*85* @param mode Cipher mode: encrypt or decrypt86* @return Cipher object for PBKDF2 algorithm87* @throws GeneralSecurityException all security exceptions are thrown.88*/89@Override90protected Cipher initCipher(int mode) throws GeneralSecurityException {91Provider provider = Security.getProvider("SunJCE");92if (provider == null) {93throw new RuntimeException("SunJCE provider does not exist.");94}95// Generate secret key96PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(),97salt, DEFAULT_ITERATION, PKDF2_DEFAULT_KEY_LEN);98SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(baseAlgo);99SecretKey key = keyFactory.generateSecret(pbeKeySpec);100101// get Cipher instance102Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION, provider);103cipher.init(mode,104new SecretKeySpec(key.getEncoded(),KEY_ALGORITHM),105new IvParameterSpec(iv));106return cipher;107}108}109110111