Path: blob/master/test/jdk/com/sun/crypto/provider/CICO/CICODESFuncTest.java
41155 views
/*1* Copyright (c) 2007, 2015, 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*/22import static java.lang.System.out;2324import java.io.ByteArrayInputStream;25import java.io.ByteArrayOutputStream;26import java.io.IOException;27import java.security.GeneralSecurityException;28import java.security.NoSuchAlgorithmException;29import java.security.Provider;30import java.security.Security;31import java.security.spec.AlgorithmParameterSpec;32import java.util.Arrays;33import javax.crypto.CipherInputStream;34import javax.crypto.CipherOutputStream;35import javax.crypto.SecretKey;36import javax.crypto.Cipher;37import javax.crypto.KeyGenerator;38import javax.crypto.spec.IvParameterSpec;3940/*41* @test42* @bug 804860443* @summary to verify cipherInputStream and cipherInputStream cipher function44* @run main CICODESFuncTest45*/46public class CICODESFuncTest {47/**48* Algorithms name.49*/50private static final String[] ALGORITHMS = { "DES", "DESede", "Blowfish" };51private static final String[] MODES = { "ECB", "CBC", "CFB", "CFB24",52"CFB32", "CFB40", "CFB72", "OFB", "OFB20", "OFB48", "OFB56",53"OFB64", "PCBC" };54/**55* Padding mode.56*/57private static final String[] PADDINGS = { "noPadding", "pkcs5padding" };58/**59* Plain text length.60*/61private static final int TEXT_LENGTH = 80;62/**63* Initialization vector length.64*/65private static final int IV_LENGTH = 8;6667public static void main(String[] args) throws Exception {68Provider provider = Security.getProvider("SunJCE");69if (provider == null) {70throw new RuntimeException("SunJCE provider does not exist.");71}72for (String algorithm : ALGORITHMS) {73for (String mode : MODES) {74// We only test noPadding and pkcs5padding for CFB72, OFB20, ECB75// PCBC and CBC. Otherwise test noPadding only.76int padKinds = 1;77if (mode.equalsIgnoreCase("CFB72")78|| mode.equalsIgnoreCase("OFB20")79|| mode.equalsIgnoreCase("ECB")80|| mode.equalsIgnoreCase("PCBC")81|| mode.equalsIgnoreCase("CBC")) {82padKinds = PADDINGS.length;83}84// PKCS5padding is meaningful only for ECB, CBC, PCBC85for (int k = 0; k < padKinds; k++) {86for (ReadModel readMode : ReadModel.values()) {87runTest(provider, algorithm, mode, PADDINGS[k], readMode);88}89}90}91}92}9394private static void runTest(Provider p, String algo, String mo, String pad,95ReadModel whichRead) throws GeneralSecurityException, IOException {96// Do initialization97byte[] plainText = TestUtilities.generateBytes(TEXT_LENGTH);98byte[] iv = TestUtilities.generateBytes(IV_LENGTH);99AlgorithmParameterSpec aps = new IvParameterSpec(iv);100try {101KeyGenerator kg = KeyGenerator.getInstance(algo, p);102out.println(algo + "/" + mo + "/" + pad + "/" + whichRead);103SecretKey key = kg.generateKey();104Cipher ci1 = Cipher.getInstance(algo + "/" + mo + "/" + pad, p);105if ("CFB72".equalsIgnoreCase(mo) || "OFB20".equalsIgnoreCase(mo)) {106throw new RuntimeException(107"NoSuchAlgorithmException not throw when mode"108+ " is CFB72 or OFB20");109}110Cipher ci2 = Cipher.getInstance(algo + "/" + mo + "/" + pad, p);111if ("ECB".equalsIgnoreCase(mo)) {112ci1.init(Cipher.ENCRYPT_MODE, key);113ci2.init(Cipher.DECRYPT_MODE, key);114} else {115ci1.init(Cipher.ENCRYPT_MODE, key, aps);116ci2.init(Cipher.DECRYPT_MODE, key, aps);117}118ByteArrayOutputStream baOutput = new ByteArrayOutputStream();119try (CipherInputStream cInput120= new CipherInputStream(121new ByteArrayInputStream(plainText), ci1);122CipherOutputStream ciOutput123= new CipherOutputStream(baOutput, ci2);) {124// Read from the input and write to the output using 2 types125// of buffering : byte[] and int126whichRead.read(cInput, ciOutput, ci1, plainText.length);127}128// Verify input and output are same.129if (!Arrays.equals(plainText, baOutput.toByteArray())) {130throw new RuntimeException("Test failed due to compare fail ");131}132} catch (NoSuchAlgorithmException nsaEx) {133if ("CFB72".equalsIgnoreCase(mo) || "OFB20".equalsIgnoreCase(mo)) {134out.println("NoSuchAlgorithmException is expected for CFB72 and OFB20");135} else {136throw new RuntimeException("Unexpected exception testing: "137+ algo + "/" + mo + "/" + pad + "/" + whichRead, nsaEx);138}139}140}141}142143144