Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/AES/TestAESCipher.java
41161 views
/*1* Copyright (c) 2014, 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.spec.AlgorithmParameterSpec;28import java.util.Random;29import javax.crypto.BadPaddingException;30import javax.crypto.Cipher;31import javax.crypto.IllegalBlockSizeException;32import javax.crypto.KeyGenerator;33import javax.crypto.NoSuchPaddingException;34import javax.crypto.SecretKey;35import javax.crypto.ShortBufferException;36import javax.crypto.spec.IvParameterSpec;3738/**39* @test40* @bug 804383641* @summary Test AES ciphers with different modes and padding schemes (ECB mode42* doesn't use IV).43* @author Liwen Wang44* @author Parag Salvi45* @key randomness46*/47public class TestAESCipher {4849private static final String ALGORITHM = "AES";50private static final String PROVIDER = "SunJCE";51private static final String[] MODES = { "ECb", "CbC", "CTR", "PCBC", "OFB",52"OFB150", "cFB", "CFB7", "cFB8", "cFB16", "cFB24", "cFB32",53"Cfb40", "cfB48", "cfB56", "cfB64", "cfB72", "cfB80", "cfB88",54"cfB96", "cfb104", "cfB112", "cfB120", "OFB8", "OFB16", "OFB24",55"OFB32", "OFB40", "OFB48", "OFB56", "OFB64", "OFB72", "OFB80",56"OFB88", "OFB96", "OFB104", "OFB112", "OFB120", "GCM" };57private static final String[] PADDING = { "NoPadding", "PKCS5Padding" };58private static final int KEY_LENGTH = 128;5960public static void main(String argv[]) throws Exception {61TestAESCipher test = new TestAESCipher();62for (String mode : MODES) {63int padKinds = 1;64if (mode.equalsIgnoreCase("ECB") || mode.equalsIgnoreCase("PCBC")65|| mode.equalsIgnoreCase("CBC")) {66padKinds = PADDING.length;67}6869for (int k = 0; k < padKinds; k++) {70test.runTest(ALGORITHM, mode, PADDING[k]);71}72}73}7475public void runTest(String algo, String mo, String pad) throws Exception {76Cipher ci = null;77byte[] iv = null;78AlgorithmParameterSpec aps = null;79SecretKey key = null;80try {81// Initialization82Random rdm = new Random();83byte[] plainText = new byte[128];84rdm.nextBytes(plainText);8586ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);87KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);88kg.init(KEY_LENGTH);89key = kg.generateKey();9091// encrypt92if (!mo.equalsIgnoreCase("GCM")) {93ci.init(Cipher.ENCRYPT_MODE, key, aps);94} else {95ci.init(Cipher.ENCRYPT_MODE, key);96}9798byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];99int offset = ci.update(plainText, 0, plainText.length, cipherText,1000);101ci.doFinal(cipherText, offset);102103if (!mo.equalsIgnoreCase("ECB")) {104iv = ci.getIV();105aps = new IvParameterSpec(iv);106} else {107aps = null;108}109110if (!mo.equalsIgnoreCase("GCM")) {111ci.init(Cipher.DECRYPT_MODE, key, aps);112} else {113ci.init(Cipher.DECRYPT_MODE, key, ci.getParameters());114}115116byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];117int len = ci.doFinal(cipherText, 0, cipherText.length,118recoveredText);119byte[] tmp = new byte[len];120System.arraycopy(recoveredText, 0, tmp, 0, len);121122// Comparison123if (!java.util.Arrays.equals(plainText, tmp)) {124System.out.println("Original: ");125dumpBytes(plainText);126System.out.println("Recovered: ");127dumpBytes(tmp);128throw new RuntimeException(129"Original text is not equal with recovered text, with mode:"130+ mo);131}132133} catch (NoSuchAlgorithmException e) {134//CFB7 and OFB150 are for negative testing135if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("OFB150")) {136System.out.println("Unexpected NoSuchAlgorithmException with mode: "137+ mo);138throw new RuntimeException("Test failed!");139}140} catch ( NoSuchProviderException | NoSuchPaddingException141| InvalidKeyException | InvalidAlgorithmParameterException142| ShortBufferException | IllegalBlockSizeException143| BadPaddingException e) {144System.out.println("Test failed!");145throw e;146}147}148149private void dumpBytes(byte[] bytes) {150for (byte b : bytes) {151System.out.print(Integer.toHexString(b));152}153154System.out.println();155}156}157158159