0N/A/*
2362N/A * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 0000000 6296075
0N/A * @summary PaddingTest
0N/A * @author Jan Luehe
0N/A */
0N/Aimport java.io.*;
0N/Aimport java.security.*;
0N/Aimport java.security.spec.*;
0N/Aimport javax.crypto.*;
0N/Aimport javax.crypto.spec.*;
0N/Aimport com.sun.crypto.provider.*;
0N/A
0N/Apublic class PaddingTest {
0N/A
0N/A Cipher cipher;
0N/A IvParameterSpec params = null;
0N/A SecretKey cipherKey = null;
0N/A String pinfile = null;
0N/A String cfile = null;
0N/A String poutfile = null;
0N/A
0N/A public static byte[] key = {
0N/A (byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,
0N/A (byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef
0N/A };
0N/A
0N/A public static byte[] key3 = {
0N/A (byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,
0N/A (byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef,
0N/A (byte)0xf0,(byte)0xe1,(byte)0xd2,(byte)0xc3,
0N/A (byte)0xb4,(byte)0xa5,(byte)0x96,(byte)0x87,
0N/A (byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98,
0N/A (byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10};
0N/A
0N/A public static byte[] iv = {
0N/A (byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98,
0N/A (byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10};
0N/A
0N/A static String[] crypts = {"DES", "DESede"};
0N/A static String[] modes = {"ECB", "CBC", "CFB", "OFB", "PCBC"};
0N/A static String[] paddings = {"PKCS5Padding", "NoPadding"};
0N/A static int numFiles = 11;
0N/A static final String currDir = System.getProperty("test.src", ".");
0N/A static String dataDir = currDir + "/inputData/";
0N/A
0N/A private String padding = null;
0N/A
0N/A public static void main(String argv[]) throws Exception {
0N/A PaddingTest pt = new PaddingTest();
0N/A pt.run();
0N/A }
0N/A
0N/A public PaddingTest() {
0N/A }
0N/A
0N/A public void run() throws Exception {
0N/A
0N/A for (int l=0; l<numFiles; l++) {
0N/A pinfile = new String(dataDir + "plain" + l + ".txt");
0N/A for (int i=0; i<crypts.length; i++) {
0N/A for (int j=0; j<modes.length; j++) {
0N/A for (int k=0; k<paddings.length; k++) {
0N/A System.out.println
0N/A ("===============================");
0N/A System.out.println
0N/A (crypts[i]+" "+modes[j]+" " + paddings[k]+ " " +
0N/A "plain" + l + " test");
0N/A cfile = new String
0N/A ("c" + l + "_" +
0N/A crypts[i] + "_" +
0N/A modes[j] + "_" +
0N/A paddings[k] + ".bin");
0N/A poutfile = new String
0N/A ("p" + l +
0N/A "_" + crypts[i] + modes[j] + paddings[k] + ".txt");
0N/A
0N/A init(crypts[i], modes[j], paddings[k]);
0N/A padding = paddings[k];
0N/A runTest();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void init(String crypt, String mode, String padding)
0N/A throws Exception {
0N/A
0N/A SunJCE jce = new SunJCE();
0N/A Security.addProvider(jce);
0N/A
0N/A KeySpec desKeySpec = null;
0N/A SecretKeyFactory factory = null;
0N/A
0N/A StringBuffer cipherName = new StringBuffer(crypt);
0N/A if (mode.length() != 0)
0N/A cipherName.append("/" + mode);
0N/A if (padding.length() != 0)
0N/A cipherName.append("/" + padding);
0N/A
0N/A cipher = Cipher.getInstance(cipherName.toString());
0N/A if (crypt.endsWith("ede")) {
0N/A desKeySpec = new DESedeKeySpec(key3);
0N/A factory = SecretKeyFactory.getInstance("DESede", "SunJCE");
0N/A } else {
0N/A desKeySpec = new DESKeySpec(key);
0N/A factory = SecretKeyFactory.getInstance("DES", "SunJCE");
0N/A }
0N/A
0N/A // retrieve the cipher key
0N/A cipherKey = factory.generateSecret(desKeySpec);
0N/A
0N/A // retrieve iv
0N/A if (!mode.equals("ECB"))
0N/A params = new IvParameterSpec(iv);
0N/A else
0N/A params = null;
0N/A }
0N/A
0N/A public void runTest() throws Exception {
0N/A
0N/A int bufferLen = 512;
0N/A byte[] input = new byte[bufferLen];
0N/A int len;
0N/A int totalInputLen = 0;
0N/A
0N/A BufferedInputStream pin = null;
0N/A BufferedOutputStream cout = null;
0N/A BufferedInputStream cin = null;
0N/A BufferedOutputStream pout = null;
0N/A
0N/A try {
0N/A pin = new BufferedInputStream(new FileInputStream(pinfile));
0N/A cout = new BufferedOutputStream(new FileOutputStream(cfile));
0N/A cipher.init(Cipher.ENCRYPT_MODE, cipherKey, params);
0N/A
0N/A while ((len = pin.read(input, 0, bufferLen)) > 0) {
0N/A totalInputLen += len;
0N/A byte[] output = cipher.update(input, 0, len);
0N/A cout.write(output, 0, output.length);
0N/A cout.flush();
0N/A }
0N/A
0N/A len = cipher.getOutputSize(0);
0N/A
0N/A byte[] out = new byte[len];
0N/A len = cipher.doFinal(out, 0);
0N/A cout.write(out, 0, len);
0N/A cout.flush();
0N/A
0N/A cin = new BufferedInputStream(new FileInputStream(cfile));
0N/A pout = new BufferedOutputStream(new FileOutputStream(poutfile));
0N/A cipher.init(Cipher.DECRYPT_MODE, cipherKey, params);
0N/A
0N/A byte[] output = null;
0N/A while ((len = cin.read(input, 0, bufferLen)) > 0) {
0N/A output = cipher.update(input, 0, len);
0N/A pout.write(output, 0, output.length);
0N/A pout.flush();
0N/A }
0N/A
0N/A len = cipher.getOutputSize(0);
0N/A out = new byte[len];
0N/A len = cipher.doFinal(out, 0);
0N/A pout.write(out, 0, len);
0N/A pout.flush();
0N/A
0N/A Process child = Runtime.getRuntime().exec
0N/A ("diff " + pinfile + " " + poutfile);
0N/A InputStream in = child.getInputStream();
0N/A byte[] data = new byte[64];
0N/A
0N/A while((len = in.read(data)) != -1)
0N/A System.out.write(data, 0, len);
0N/A in.close();
0N/A child.waitFor();
0N/A System.out.println("child exited with " + child.exitValue());
0N/A }
0N/A catch (IllegalBlockSizeException ex) {
0N/A if ((totalInputLen % 8 != 0) && (padding.equals("NoPadding")))
0N/A return;
0N/A else {
0N/A System.out.println("Test failed!");
0N/A throw ex;
0N/A }
0N/A }
0N/A finally {
0N/A try {
0N/A if (pin != null)
0N/A pin.close();
0N/A if (pout != null)
0N/A pout.close();
0N/A if (cin != null)
0N/A cin.close();
0N/A if (cout != null)
0N/A cout.close();
0N/A }
0N/A catch (IOException e) {
0N/A e.printStackTrace();
0N/A return;
0N/A }
0N/A }
0N/A }
0N/A
0N/A}