Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/AES/CICO.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.io.ByteArrayInputStream;24import java.io.ByteArrayOutputStream;25import java.io.IOException;26import java.security.InvalidAlgorithmParameterException;27import java.security.InvalidKeyException;28import java.security.NoSuchAlgorithmException;29import java.security.NoSuchProviderException;30import java.security.spec.AlgorithmParameterSpec;31import java.util.Random;32import javax.crypto.Cipher;33import javax.crypto.CipherInputStream;34import javax.crypto.CipherOutputStream;35import javax.crypto.KeyGenerator;36import javax.crypto.NoSuchPaddingException;37import javax.crypto.SecretKey;38import javax.crypto.spec.IvParameterSpec;3940/**41* @test42* @bug 804383643* @summary Test AES ciphers with different modes and padding schemes (ECB mode44* doesn't use IV). The test tries 3 different read methods of45* CipherInputStream.46* @key randomness47*/48public class CICO {49private static final String ALGORITHM = "aEs";50private static final String[] MODES = { "PCBC", "ECb", "cbC", "cFB",51"cFB24", "cFB32", "Cfb40", "CFB72", "OfB", "OfB20", "OfB48",52"OfB56", "OFB64", "OFB112", "CFB112", "pCbC" };53private static final String[] PADDING = { "noPadding", "pkcs5padding" };54private static final String PROVIDER = "SunJCE";55private static final int NREADS = 3;56private static final int KEY_LENGTH = 128;5758private final byte[] plainText = new byte[1600000];596061public static void main(String argv[]) throws Exception {62CICO test = new CICO();63for (String mode : MODES) {64for (String pad : PADDING) {65for (int m = 0; m < NREADS; m++) {66test.runTest(ALGORITHM, mode, pad, m);67}68}69}70}7172public void runTest(String algo, String mo, String pad, int whichRead) throws Exception {73Cipher ci1 = null;74Cipher ci2 = null;75byte[] iv = null;76AlgorithmParameterSpec aps = null;77SecretKey key = null;7879try {80// Do initialization81Random rdm = new Random();82rdm.nextBytes(plainText);83KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);84if (!kg.getAlgorithm().equals(algo)) {85throw new RuntimeException("Unexpected algorithm <"86+ kg.getAlgorithm() + ">, expected value is <" + algo87+ ">");88}8990kg.init(KEY_LENGTH);91key = kg.generateKey();9293ci1 = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);9495if (mo.equalsIgnoreCase("ECB")) {96ci1.init(Cipher.ENCRYPT_MODE, key);97} else {98ci1.init(Cipher.ENCRYPT_MODE, key, aps);99}100101if (!mo.equalsIgnoreCase("ECB")) {102iv = ci1.getIV();103aps = new IvParameterSpec(iv);104} else {105aps = null;106}107108ci2 = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);109if (mo.equalsIgnoreCase("ECB")) {110ci2.init(Cipher.DECRYPT_MODE, key);111} else {112ci2.init(Cipher.DECRYPT_MODE, key, aps);113}114115ByteArrayInputStream baInput = new ByteArrayInputStream(plainText);116ByteArrayOutputStream baOutput = new ByteArrayOutputStream();117try (CipherInputStream ciInput = new CipherInputStream(baInput, ci1);118CipherOutputStream ciOutput = new CipherOutputStream(119baOutput, ci2)) {120// According to specification, CipherInputStream does not support the121// mark and reset methods122if (ciInput.markSupported()) {123throw new RuntimeException(124"CipherInputStream unexpectedly supports the mark and reset methods");125}126127// Read from the input and write to the output using 2 types128// of buffering : byte[] and int129switch (whichRead) {130case 0:131int buffer0 = ciInput.read();132while (buffer0 != -1) {133ciOutput.write(buffer0);134buffer0 = ciInput.read();135}136break;137case 1:138byte[] buffer1 = new byte[20];139int len1 = ciInput.read(buffer1);140while (len1 != -1) {141ciOutput.write(buffer1, 0, len1);142len1 = ciInput.read(buffer1);143}144break;145case NREADS - 1:146byte[] buffer2 = new byte[ci1147.getOutputSize(plainText.length)];148int offset2 = 0;149int len2 = 0;150while (len2 != -1) {151len2 = ciInput.read(buffer2, offset2, buffer2.length152- offset2);153offset2 += len2;154}155ciOutput.write(buffer2, 0, buffer2.length);156break;157}158}159160// Get the output161byte[] recoveredText = new byte[baOutput.size()];162recoveredText = baOutput.toByteArray();163if (!java.util.Arrays.equals(plainText, recoveredText)) {164throw new RuntimeException(165"Original text is not equal with recovered text, with "166+ algo + "/" + mo + "/" + pad + "/" + whichRead);167}168169// Compare input and output170171} catch (NoSuchAlgorithmException e) {172//OFB20 is for negative testing173if (!mo.equalsIgnoreCase("OFB20")) {174System.out.println("Unexpected NoSuchAlgorithmException with "175+ algo + "/" + mo + "/" + pad + "/" + whichRead);176throw new RuntimeException("Test failed!");177}178} catch (IOException | NoSuchProviderException | NoSuchPaddingException179| InvalidKeyException | InvalidAlgorithmParameterException e) {180System.out.println("Unexpected Exception with "181+ algo + "/" + mo + "/" + pad + "/" + whichRead);182System.out.println("Test failed!");183throw e;184}185}186}187188189