Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/AES/Padding.java
41161 views
/*1* Copyright (c) 2014, 2019, 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). The test tries 3 different read methods of43* CipherInputStream.44* @key randomness45*/46public class Padding {4748private static final String ALGORITHM = "AES";49private static final String PROVIDER = "SunJCE";50private static final String[] MODES_PKCS5PAD = {51"ECb", "CbC", "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" };57private static final String[] MODES_NOPAD = { "CTR", "CTS", "GCM" };5859private static final int KEY_LENGTH = 128;6061public static void main(String argv[]) throws Exception {62Padding test = new Padding();63for (String mode : MODES_PKCS5PAD) {64test.runTest(ALGORITHM, mode, "PKCS5Padding");65}66for (String mode : MODES_NOPAD) {67test.runTest(ALGORITHM, mode, "NoPadding");68}69}7071public void runTest(String algo, String mo, String pad) throws Exception {72Cipher ci = null;73byte[] iv = null;74AlgorithmParameterSpec aps = null;75SecretKey key = null;76try {77Random rdm = new Random();78byte[] plainText;7980ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);81KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);82kg.init(KEY_LENGTH);83key = kg.generateKey();8485for (int i = 0; i < 15; i++) {86plainText = new byte[1600 + i + 1];87rdm.nextBytes(plainText);8889if (!mo.equalsIgnoreCase("GCM")) {90ci.init(Cipher.ENCRYPT_MODE, key, aps);91} else {92ci.init(Cipher.ENCRYPT_MODE, key);93}9495byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];96int offset = ci.update(plainText, 0, plainText.length,97cipherText, 0);98ci.doFinal(cipherText, offset);99if (!mo.equalsIgnoreCase("ECB")) {100iv = ci.getIV();101aps = new IvParameterSpec(iv);102} else {103aps = null;104}105106if (!mo.equalsIgnoreCase("GCM")) {107ci.init(Cipher.DECRYPT_MODE, key, aps);108} else {109ci.init(Cipher.DECRYPT_MODE, key, ci.getParameters());110}111112byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];113int len = ci.doFinal(cipherText, 0, cipherText.length,114recoveredText);115byte[] tmp = new byte[len];116117for (int j = 0; j < len; j++) {118tmp[j] = recoveredText[j];119}120121if (!java.util.Arrays.equals(plainText, tmp)) {122System.out.println("Original: ");123dumpBytes(plainText);124System.out.println("Recovered: ");125dumpBytes(tmp);126throw new RuntimeException(127"Original text is not equal with recovered text, with mode:"128+ mo);129}130}131} catch (NoSuchAlgorithmException e) {132//CFB7 and OFB150 are for negative testing133if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("OFB150")) {134System.out135.println("Unexpected NoSuchAlgorithmException with mode: "136+ mo);137throw new RuntimeException("Test failed!");138}139} catch ( NoSuchProviderException | NoSuchPaddingException140| InvalidKeyException | InvalidAlgorithmParameterException141| ShortBufferException | IllegalBlockSizeException142| BadPaddingException e) {143System.out.println("Test failed!");144throw e;145}146}147148private void dumpBytes(byte[] bytes) {149for (byte b : bytes) {150System.out.print(Integer.toHexString(b));151}152153System.out.println();154}155}156157158