Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/TextLength/DESCipherWrapper.java
41161 views
/*1* Copyright (c) 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 static java.lang.System.out;2425import java.security.InvalidAlgorithmParameterException;26import java.security.InvalidKeyException;27import java.security.NoSuchAlgorithmException;28import java.security.spec.AlgorithmParameterSpec;29import java.util.ArrayList;30import java.util.Arrays;31import java.util.List;32import javax.crypto.BadPaddingException;33import javax.crypto.IllegalBlockSizeException;34import javax.crypto.SecretKey;35import javax.crypto.ShortBufferException;36import javax.crypto.Cipher;37import javax.crypto.KeyGenerator;38import javax.crypto.NoSuchPaddingException;39import javax.crypto.spec.IvParameterSpec;4041/**42* Wrapper class to test a given DES algorithm.43*/44public class DESCipherWrapper {4546private final Cipher ci;47private final byte[] iv;48private final SecretKey key;49private final String algo;50private final String mode;51private final String pad;52private final int keyStrength;53private byte[] resultText = null;5455public DESCipherWrapper(String algo, String mode, String pad)56throws NoSuchAlgorithmException, NoSuchPaddingException {57ci = Cipher.getInstance(algo + "/" + mode + "/" + pad);5859iv = new byte[8];60for (int i = 0; i < 8; i++) {61iv[i] = (byte) (i & 0xff);62}6364KeyGenerator kg = KeyGenerator.getInstance(algo);65key = kg.generateKey();66keyStrength = algo.equalsIgnoreCase("DESede") ? 11267: key.getEncoded().length * 8;6869this.algo = algo;70this.mode = mode;71this.pad = pad;72}7374public byte[] getResult() {75return resultText.clone();76}7778public void execute(int edMode, byte[] inputText)79throws InvalidKeyException, InvalidAlgorithmParameterException,80IllegalBlockSizeException, BadPaddingException,81ShortBufferException, NoSuchAlgorithmException {82AlgorithmParameterSpec aps = null;8384try {85if (!mode.equalsIgnoreCase("ECB")) {86aps = new IvParameterSpec(iv);87}88ci.init(edMode, key, aps);8990// Generate a resultText using a single-part enc/dec91resultText = ci.doFinal(inputText);9293// Generate outputText for each multi-part en/de-cryption94/* Combination #1:95update(byte[], int, int)96doFinal(byte[], int, int)97*/98byte[] part11 = ci.update(inputText, 0, inputText.length);99byte[] part12 = ci.doFinal();100byte[] outputText1 = new byte[part11.length + part12.length];101System.arraycopy(part11, 0, outputText1, 0, part11.length);102System.arraycopy(part12, 0, outputText1, part11.length,103part12.length);104105List<byte[]> outputTexts = new ArrayList<>(4);106outputTexts.add(outputText1);107108/* Combination #2:109update(byte[], int, int)110doFinal(byte[], int, int, byte[], int)111*/112byte[] part21 = ci.update(inputText, 0, inputText.length - 5);113byte[] part22 = new byte[ci.getOutputSize(inputText.length)];114int len2 = ci115.doFinal(inputText, inputText.length - 5, 5, part22, 0);116byte[] outputText2 = new byte[part21.length + len2];117System.arraycopy(part21, 0, outputText2, 0, part21.length);118System.arraycopy(part22, 0, outputText2, part21.length, len2);119120outputTexts.add(outputText2);121122/* Combination #3:123update(byte[], int, int, byte[], int)124doFinal(byte[], int, int)125*/126byte[] part31 = new byte[ci.getOutputSize(inputText.length)];127int len3 = ci.update(inputText, 0, inputText.length - 8, part31, 0);128byte[] part32 = ci.doFinal(inputText, inputText.length - 8, 8);129byte[] outputText3 = new byte[len3 + part32.length];130System.arraycopy(part31, 0, outputText3, 0, len3);131System.arraycopy(part32, 0, outputText3, len3, part32.length);132133outputTexts.add(outputText3);134135/* Combination #4:136update(byte[], int, int, byte[], int)137doFinal(byte[], int, int, byte[], int)138*/139byte[] part41 = new byte[ci.getOutputSize(inputText.length)];140int len4 = ci.update(inputText, 0, inputText.length - 8, part41, 0);141int rest4 = ci.doFinal(inputText, inputText.length - 8, 8, part41,142len4);143byte[] outputText4 = new byte[len4 + rest4];144System.arraycopy(part41, 0, outputText4, 0, outputText4.length);145146outputTexts.add(outputText4);147148// Compare results149for (int k = 0; k < outputTexts.size(); k++) {150if (!Arrays.equals(resultText, outputTexts.get(k))) {151out.println(" Testing: " + algo + "/" + mode + "/" + pad);152throw new RuntimeException(153"Compare value of resultText and combination " + k154+ " are not same. Test failed.");155}156}157if (keyStrength > Cipher.getMaxAllowedKeyLength(algo)) {158throw new RuntimeException(159"Expected exception uncaught, keyStrength "160+ keyStrength);161}162} catch (InvalidKeyException ex) {163if (keyStrength <= Cipher.getMaxAllowedKeyLength(algo)) {164out.println("Unexpected exception in " + algo + "/" + mode165+ "/" + pad + " , KeySize " + keyStrength);166throw ex;167}168out.println("Caught InvalidKeyException as expected");169}170}171}172173174