Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/AES/TestAESCipher.java
66649 views
/*1* Copyright (c) 2014, 2021, 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.HexFormat;29import javax.crypto.BadPaddingException;30import javax.crypto.Cipher;31import javax.crypto.IllegalBlockSizeException;32import javax.crypto.NoSuchPaddingException;33import javax.crypto.SecretKey;34import javax.crypto.ShortBufferException;35import javax.crypto.spec.GCMParameterSpec;36import javax.crypto.spec.IvParameterSpec;37import javax.crypto.spec.SecretKeySpec;3839/**40* @test41* @bug 804383642* @summary Test AES ciphers with different modes and padding schemes (ECB mode43* doesn't use IV).44* @author Liwen Wang45* @author Parag Salvi46*/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 = 16;59static byte[] plainText = new byte[128];60static byte[] key = new byte[KEY_LENGTH];6162public static void main(String argv[]) throws Exception {63TestAESCipher test = new TestAESCipher();64for (String mode : MODES) {65int padKinds = 1;66if (mode.equalsIgnoreCase("ECB") || mode.equalsIgnoreCase("PCBC")67|| mode.equalsIgnoreCase("CBC")) {68padKinds = PADDING.length;69}7071for (int k = 0; k < padKinds; k++) {72test.runTest(ALGORITHM, mode, PADDING[k]);73}74}75}767778public void runTest(String algo, String mo, String pad) throws Exception {79Cipher ci;80System.out.println("Testing " + algo + "/" + mo + "/" + pad);8182byte[] iv = new byte[16];83AlgorithmParameterSpec aps = new GCMParameterSpec(128, iv);84SecretKey key = new SecretKeySpec(this.key, 0, KEY_LENGTH,"AES");8586try {87// Initialization88ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);8990// encrypt91if (mo.equalsIgnoreCase("GCM")) {92ci.init(Cipher.ENCRYPT_MODE, key, aps);93} else if (mo.equalsIgnoreCase("ECB")) {94ci.init(Cipher.ENCRYPT_MODE, key, (AlgorithmParameterSpec)null);95} else {96ci.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));97}9899byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];100int offset = ci.update(plainText, 0, plainText.length, cipherText,1010);102ci.doFinal(cipherText, offset);103104if (!mo.equalsIgnoreCase("ECB")) {105iv = ci.getIV();106aps = new IvParameterSpec(iv);107} else {108aps = null;109}110111if (!mo.equalsIgnoreCase("GCM")) {112ci.init(Cipher.DECRYPT_MODE, key, aps);113} else {114ci.init(Cipher.DECRYPT_MODE, key, ci.getParameters());115}116117byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];118int len = ci.doFinal(cipherText, 0, cipherText.length,119recoveredText);120121// Comparison122if (!java.util.Arrays.equals(plainText, 0 , plainText.length,123recoveredText, 0, len)) {124System.out.println("Original: ");125System.out.println(HexFormat.of().formatHex(plainText));126System.out.println("Recovered: ");127System.out.println(HexFormat.of().128formatHex(recoveredText, 0, len));129throw new RuntimeException("Original text is not equal with " +130"recovered text, with mode:" + 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" +137" mode: " + 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}148}149150151