Path: blob/master/test/jdk/javax/crypto/SecretKeyFactory/SecKFTranslateTest.java
41149 views
/*1* Copyright (c) 2003, 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.InvalidAlgorithmParameterException;24import java.security.InvalidKeyException;25import java.security.NoSuchAlgorithmException;26import java.security.NoSuchProviderException;27import java.security.SecureRandom;28import java.security.spec.AlgorithmParameterSpec;29import java.security.spec.InvalidKeySpecException;30import java.util.Arrays;31import java.util.Random;3233import javax.crypto.BadPaddingException;34import javax.crypto.Cipher;35import javax.crypto.IllegalBlockSizeException;36import javax.crypto.NoSuchPaddingException;37import javax.crypto.SecretKey;38import javax.crypto.SecretKeyFactory;39import javax.crypto.ShortBufferException;40import javax.crypto.spec.PBEKeySpec;41import javax.crypto.spec.PBEParameterSpec;42import javax.security.auth.DestroyFailedException;4344/*45* @test46* @bug 804882047* @summary The test verifies SecretKey values should remain the same after48* translation with SecretKeyFactory.translateKey().49*/5051public class SecKFTranslateTest {52private static final String SUN_JCE = "SunJCE";5354public static void main(String[] args) throws Exception {5556SecKFTranslateTest test = new SecKFTranslateTest();57test.run();58}5960private void run() throws Exception {6162for (Algorithm algorithm : Algorithm.values()) {63runTest(algorithm);64}65}6667private void runTest(Algorithm algo) throws NoSuchAlgorithmException,68NoSuchProviderException, InvalidKeyException,69InvalidKeySpecException, NoSuchPaddingException,70InvalidAlgorithmParameterException, ShortBufferException,71IllegalBlockSizeException, BadPaddingException {72AlgorithmParameterSpec[] aps = new AlgorithmParameterSpec[1];73byte[] plainText = new byte[800];7475SecretKey key1 = algo.intSecurityKey(aps);76Random random = new Random();77// Initialization78SecretKeyFactory skf = SecretKeyFactory.getInstance(algo.toString(),79SUN_JCE);8081random.nextBytes(plainText);82Cipher ci = Cipher.getInstance(algo.toString(), SUN_JCE);83// Encryption84ci.init(Cipher.ENCRYPT_MODE, key1, aps[0]);85byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];86int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);87ci.doFinal(cipherText, offset);88// translate key89SecretKey key2 = skf.translateKey(key1);9091// Decryption92ci.init(Cipher.DECRYPT_MODE, key2, aps[0]);93byte[] recoveredText = new byte[ci.getOutputSize(plainText.length)];94ci.doFinal(cipherText, 0, cipherText.length, recoveredText);9596// Comparison97if (!Arrays.equals(plainText, recoveredText)) {98System.out.println("Key1:" + new String(key1.getEncoded())99+ " Key2:" + new String(key2.getEncoded()));100throw new RuntimeException("Testing translate key failed with "101+ algo);102}103104}105}106107class MyOwnSecKey implements SecretKey {108109private static final String DEFAULT_ALGO = "PBEWithMD5AndDES";110private final byte[] key;111private final String algorithm;112private final int keySize;113114public MyOwnSecKey(byte[] key1, int offset, String algo)115throws InvalidKeyException {116algorithm = algo;117if (algo.equalsIgnoreCase("DES")) {118keySize = 8;119} else if (algo.equalsIgnoreCase("DESede")) {120keySize = 24;121} else {122throw new InvalidKeyException(123"Inappropriate key format and algorithm");124}125126if (key1 == null || key1.length - offset < keySize) {127throw new InvalidKeyException("Wrong key size");128}129key = new byte[keySize];130System.arraycopy(key, offset, key, 0, keySize);131}132133public MyOwnSecKey(PBEKeySpec ks) throws InvalidKeySpecException {134algorithm = DEFAULT_ALGO;135key = new String(ks.getPassword()).getBytes();136keySize = key.length;137}138139@Override140public String getAlgorithm() {141return algorithm;142}143144@Override145public String getFormat() {146return "RAW";147}148149@Override150public byte[] getEncoded() {151byte[] copy = new byte[keySize];152System.arraycopy(key, 0, copy, 0, keySize);153return copy;154}155156@Override157public void destroy() throws DestroyFailedException {158}159160@Override161public boolean isDestroyed() {162return false;163}164}165166enum Algorithm {167DES {168@Override169SecretKey intSecurityKey(AlgorithmParameterSpec[] spec)170throws InvalidKeyException {171int keyLength = 8;172byte[] keyVal = new byte[keyLength];173new SecureRandom().nextBytes(keyVal);174SecretKey key1 = new MyOwnSecKey(keyVal, 0, this.toString());175return key1;176}177},178DESEDE {179@Override180SecretKey intSecurityKey(AlgorithmParameterSpec[] spec)181throws InvalidKeyException {182int keyLength = 24;183byte[] keyVal = new byte[keyLength];184new SecureRandom().nextBytes(keyVal);185SecretKey key1 = new MyOwnSecKey(keyVal, 0, this.toString());186return key1;187}188},189PBEWithMD5ANDdes {190@Override191SecretKey intSecurityKey(AlgorithmParameterSpec[] spec)192throws InvalidKeySpecException {193byte[] salt = new byte[8];194int iterCnt = 6;195new Random().nextBytes(salt);196spec[0] = new PBEParameterSpec(salt, iterCnt);197PBEKeySpec pbeKS = new PBEKeySpec(198new String("So far so good").toCharArray());199SecretKey key1 = new MyOwnSecKey(pbeKS);200return key1;201}202};203abstract SecretKey intSecurityKey(AlgorithmParameterSpec[] spec)204throws InvalidKeyException, InvalidKeySpecException;205}206207208