Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/Cipher/AES/Padding.java
38889 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). 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 = { "ECb", "CbC", "PCBC", "OFB",51"OFB150", "cFB", "CFB7", "cFB8", "cFB16", "cFB24", "cFB32",52"Cfb40", "cfB48", "cfB56", "cfB64", "cfB72", "cfB80", "cfB88",53"cfB96", "cfb104", "cfB112", "cfB120", "OFB8", "OFB16", "OFB24",54"OFB32", "OFB40", "OFB48", "OFB56", "OFB64", "OFB72", "OFB80",55"OFB88", "OFB96", "OFB104", "OFB112", "OFB120", "GCM" };56private static final String PADDING = "PKCS5Padding";57private static final int KEY_LENGTH = 128;5859public static void main(String argv[]) throws Exception {60Padding test = new Padding();61for (String mode : MODES) {62test.runTest(ALGORITHM, mode, PADDING);63}64}6566public void runTest(String algo, String mo, String pad) throws Exception {67Cipher ci = null;68byte[] iv = null;69AlgorithmParameterSpec aps = null;70SecretKey key = null;71try {72Random rdm = new Random();73byte[] plainText;7475ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);76KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);77kg.init(KEY_LENGTH);78key = kg.generateKey();7980for (int i = 0; i < 15; i++) {81plainText = new byte[1600 + i + 1];82rdm.nextBytes(plainText);8384if (!mo.equalsIgnoreCase("GCM")) {85ci.init(Cipher.ENCRYPT_MODE, key, aps);86} else {87ci.init(Cipher.ENCRYPT_MODE, key);88}8990byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];91int offset = ci.update(plainText, 0, plainText.length,92cipherText, 0);93ci.doFinal(cipherText, offset);9495if (!mo.equalsIgnoreCase("ECB")) {96iv = ci.getIV();97aps = new IvParameterSpec(iv);98} else {99aps = null;100}101102if (!mo.equalsIgnoreCase("GCM")) {103ci.init(Cipher.DECRYPT_MODE, key, aps);104} else {105ci.init(Cipher.DECRYPT_MODE, key, ci.getParameters());106}107108byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];109int len = ci.doFinal(cipherText, 0, cipherText.length,110recoveredText);111byte[] tmp = new byte[len];112113for (int j = 0; j < len; j++) {114tmp[j] = recoveredText[j];115}116117if (!java.util.Arrays.equals(plainText, tmp)) {118System.out.println("Original: ");119dumpBytes(plainText);120System.out.println("Recovered: ");121dumpBytes(tmp);122throw new RuntimeException(123"Original text is not equal with recovered text, with mode:"124+ mo);125}126}127} catch (NoSuchAlgorithmException e) {128//CFB7 and OFB150 are for negative testing129if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("OFB150")) {130System.out131.println("Unexpected NoSuchAlgorithmException with mode: "132+ mo);133throw new RuntimeException("Test failed!");134}135} catch ( NoSuchProviderException | NoSuchPaddingException136| InvalidKeyException | InvalidAlgorithmParameterException137| ShortBufferException | IllegalBlockSizeException138| BadPaddingException e) {139System.out.println("Test failed!");140throw e;141}142}143144private void dumpBytes(byte[] bytes) {145for (byte b : bytes) {146System.out.print(Integer.toHexString(b));147}148149System.out.println();150}151}152153154