Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/AES/TestSameBuffer.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). The test tries 3 different read methods of43* CipherInputStream.44* @key randomness45*/46public class TestSameBuffer {4748private static final String ALGORITHM = "Rijndael";49private static final String PROVIDER = "SunJCE";50private static final String[] MODES = { "ECb", "CbC", "OFB", "CFB150",51"cFB", "CFB7", " cFB8", "cFB16", "cFB24", "cFB32", "Cfb40",52"cfB48", " cfB56", "cfB64", "cfB72", "cfB80", "cfB88", "cfB96",53"cfb104", "cfB112", "cfB120" };54private static final String PADDING = "NoPadding";55private static final int KEY_LENGTH = 128;5657public static void main(String argv[]) throws Exception {58TestSameBuffer test = new TestSameBuffer();59for (String mode : MODES) {60test.runTest(ALGORITHM, mode, PADDING);61}62}6364public void runTest(String algo, String mo, String pad) throws Exception {65Cipher ci = null;66byte[] iv = null;67AlgorithmParameterSpec aps = null;68SecretKey key = null;69try {70// Initialization71Random rdm = new Random();72byte[] plainText = new byte[128];73rdm.nextBytes(plainText);7475// keep the plain text76byte[] tmpText = new byte[plainText.length];77for (int i = 0; i < plainText.length; i++) {78tmpText[i] = plainText[i];79}8081ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);8283KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);84kg.init(KEY_LENGTH);85key = kg.generateKey();8687// encrypt88ci.init(Cipher.ENCRYPT_MODE, key);89int offset = ci90.update(plainText, 0, plainText.length, plainText, 0);91ci.doFinal(plainText, offset);9293if (!mo.equalsIgnoreCase("ECB")) {94iv = ci.getIV();95aps = new IvParameterSpec(iv);96} else {97aps = null;98}99100ci.init(Cipher.DECRYPT_MODE, key, aps);101byte[] recoveredText = new byte[ci.getOutputSize(plainText.length)];102ci.doFinal(plainText, 0, plainText.length, recoveredText);103104// Comparison105if (!java.util.Arrays.equals(tmpText, recoveredText)) {106System.out.println("Original: ");107dumpBytes(plainText);108System.out.println("Recovered: ");109dumpBytes(recoveredText);110throw new RuntimeException(111"Original text is not equal with recovered text, with mode:"112+ mo);113}114115} catch (NoSuchAlgorithmException e) {116//CFB7 and CFB150 are for negative testing117if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("CFB150")) {118System.out.println("Unexpected NoSuchAlgorithmException with mode: "119+ mo);120throw new RuntimeException("Test failed!");121}122} catch (NoSuchProviderException | NoSuchPaddingException123| InvalidKeyException | InvalidAlgorithmParameterException124| ShortBufferException | IllegalBlockSizeException125| BadPaddingException e) {126System.out.println("Test failed!");127throw e;128}129}130131private void dumpBytes(byte[] bytes) {132for (byte b : bytes) {133System.out.print(Integer.toHexString(b));134}135136System.out.println();137}138}139140141