Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/PBE/TestCipherKeyWrapperPBEKey.java
41161 views
/*1* Copyright (c) 2012, 2016, 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.io.PrintStream;24import java.security.AlgorithmParameters;25import java.security.InvalidKeyException;26import java.security.Key;27import java.security.Provider;28import java.security.Security;29import java.security.spec.AlgorithmParameterSpec;30import java.util.Arrays;31import java.util.Random;32import java.util.StringTokenizer;33import javax.crypto.Cipher;34import javax.crypto.SecretKey;35import javax.crypto.SecretKeyFactory;36import javax.crypto.spec.PBEKeySpec;37import javax.crypto.spec.PBEParameterSpec;3839/**40* @test41* @bug 804178142* @summary Test to see if key wrapper works correctly with PBEKey43* @author Yu-Ching (Valerie) PENG44* @author Bill Situ45* @author Yun Ke46* @run main TestCipherKeyWrapperPBEKey47* @key randomness48*/49public class TestCipherKeyWrapperPBEKey {5051private static final String[] PBEAlgorithms = {52"pbeWithMD5ANDdes",53"PBEWithMD5AndDES/CBC/PKCS5Padding",54"PBEWithMD5AndTripleDES",55"PBEWithMD5AndTripleDES/CBC/PKCS5Padding",56"PBEwithSHA1AndDESede",57"PBEwithSHA1AndDESede/CBC/PKCS5Padding",58"PBEwithSHA1AndRC2_40",59"PBEwithSHA1Andrc2_40/CBC/PKCS5Padding",60"PBEWithSHA1AndRC2_128",61"PBEWithSHA1andRC2_128/CBC/PKCS5Padding",62"PBEWithSHA1AndRC4_40",63"PBEWithsha1AndRC4_40/ECB/NoPadding",64"PBEWithSHA1AndRC4_128",65"pbeWithSHA1AndRC4_128/ECB/NoPadding",66"PBEWithHmacSHA1AndAES_128",67"PBEWithHmacSHA224AndAES_128",68"PBEWithHmacSHA256AndAES_128",69"PBEWithHmacSHA384AndAES_128",70"PBEWithHmacSHA512AndAES_128",71"PBEWithHmacSHA1AndAES_256",72"PBEWithHmacSHA224AndAES_256",73"PBEWithHmacSHA256AndAES_256",74"PBEWithHmacSHA384AndAES_256",75"PBEWithHmacSHA512AndAES_256"76};7778public static void main(String[] args) {7980TestCipherKeyWrapperPBEKey test = new TestCipherKeyWrapperPBEKey();81Provider sunjce = Security.getProvider("SunJCE");8283if (!test.runAll(sunjce, System.out)) {84throw new RuntimeException("One or more tests have failed....");85}86}8788public boolean runAll(Provider p, PrintStream out) {89boolean finalResult = true;9091for (String algorithm : PBEAlgorithms) {92out.println("Running test with " + algorithm + ":");93try {94if (!runTest(p, algorithm, out)) {95finalResult = false;96out.println("STATUS: Failed");97} else {98out.println("STATUS: Passed");99}100} catch (Exception ex) {101finalResult = false;102ex.printStackTrace(out);103out.println("STATUS:Failed");104}105}106107return finalResult;108}109110// Have a generic throws Exception as it can throw many different exceptions111public boolean runTest(Provider p, String algo, PrintStream out)112throws Exception {113114byte[] salt = new byte[8];115int ITERATION_COUNT = 1000;116AlgorithmParameters pbeParams = null;117118String baseAlgo119= new StringTokenizer(algo, "/").nextToken().toUpperCase();120boolean isAES = baseAlgo.contains("AES");121122boolean isUnlimited =123(Cipher.getMaxAllowedKeyLength(algo) == Integer.MAX_VALUE);124125try {126// Initialization127new Random().nextBytes(salt);128AlgorithmParameterSpec aps = new PBEParameterSpec(salt,129ITERATION_COUNT);130SecretKeyFactory skf = SecretKeyFactory.getInstance(baseAlgo, p);131SecretKey key = skf.generateSecret(new PBEKeySpec(132"Secret Key".toCharArray()));133Cipher ci = Cipher.getInstance(algo);134if (isAES) {135ci.init(Cipher.WRAP_MODE, key);136pbeParams = ci.getParameters();137} else {138ci.init(Cipher.WRAP_MODE, key, aps);139}140141byte[] keyWrapper = ci.wrap(key);142if (isAES) {143ci.init(Cipher.UNWRAP_MODE, key, pbeParams);144} else {145ci.init(Cipher.UNWRAP_MODE, key, aps);146}147148Key unwrappedKey = ci.unwrap(keyWrapper, algo, Cipher.SECRET_KEY);149150if ((baseAlgo.endsWith("TRIPLEDES")151|| baseAlgo.endsWith("AES_256")) && !isUnlimited) {152out.print(153"Expected InvalidKeyException not thrown");154return false;155}156157return (Arrays.equals(key.getEncoded(), unwrappedKey.getEncoded()));158159} catch (InvalidKeyException ex) {160161if ((baseAlgo.endsWith("TRIPLEDES")162|| baseAlgo.endsWith("AES_256")) && !isUnlimited) {163out.print(164"Expected InvalidKeyException thrown");165return true;166} else {167throw ex;168}169}170}171}172173174