149N/A/*
3645N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
149N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
149N/A *
320N/A * This code is free software; you can redistribute it and/or modify it
320N/A * under the terms of the GNU General Public License version 2 only, as
149N/A * published by the Free Software Foundation.
149N/A *
320N/A * This code is distributed in the hope that it will be useful, but WITHOUT
320N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
320N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
320N/A * version 2 for more details (a copy is included in the LICENSE file that
149N/A * accompanied this code).
149N/A *
320N/A * You should have received a copy of the GNU General Public License version
320N/A * 2 along with this work; if not, write to the Free Software Foundation,
149N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
149N/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.
149N/A */
149N/A
149N/A/**
149N/A * @test %I% %E%
3645N/A * @bug 4898461 6604496
149N/A * @summary basic test for symmetric ciphers with padding
149N/A * @author Valerie Peng
149N/A * @library ..
149N/A */
149N/Aimport java.io.*;
149N/Aimport java.nio.*;
149N/Aimport java.util.*;
149N/A
149N/Aimport java.security.*;
149N/Aimport java.security.spec.AlgorithmParameterSpec;
149N/A
149N/Aimport javax.crypto.*;
149N/Aimport javax.crypto.spec.IvParameterSpec;
149N/A
149N/Apublic class TestSymmCiphers extends PKCS11Test {
149N/A
149N/A private static class CI { // class for holding Cipher Information
149N/A
149N/A String transformation;
149N/A String keyAlgo;
149N/A int dataSize;
149N/A
149N/A CI(String transformation, String keyAlgo, int dataSize) {
149N/A this.transformation = transformation;
149N/A this.keyAlgo = keyAlgo;
149N/A this.dataSize = dataSize;
149N/A }
149N/A }
149N/A private static final CI[] TEST_LIST = {
149N/A new CI("ARCFOUR", "ARCFOUR", 400),
149N/A new CI("RC4", "RC4", 401),
149N/A new CI("DES/CBC/NoPadding", "DES", 400),
149N/A new CI("DESede/CBC/NoPadding", "DESede", 160),
149N/A new CI("AES/CBC/NoPadding", "AES", 4800),
149N/A new CI("Blowfish/CBC/NoPadding", "Blowfish", 24),
149N/A new CI("DES/cbc/PKCS5Padding", "DES", 6401),
149N/A new CI("DESede/CBC/PKCS5Padding", "DESede", 402),
149N/A new CI("AES/CBC/PKCS5Padding", "AES", 30),
149N/A new CI("Blowfish/CBC/PKCS5Padding", "Blowfish", 19),
149N/A new CI("DES/ECB/NoPadding", "DES", 400),
149N/A new CI("DESede/ECB/NoPadding", "DESede", 160),
149N/A new CI("AES/ECB/NoPadding", "AES", 4800),
149N/A new CI("DES/ECB/PKCS5Padding", "DES", 32),
149N/A new CI("DES/ECB/PKCS5Padding", "DES", 6400),
149N/A new CI("DESede/ECB/PKCS5Padding", "DESede", 400),
149N/A new CI("AES/ECB/PKCS5Padding", "AES", 64),
3645N/A
149N/A new CI("DES", "DES", 6400),
149N/A new CI("DESede", "DESede", 408),
3645N/A new CI("AES", "AES", 128),
3645N/A
3645N/A new CI("AES/CTR/NoPadding", "AES", 3200)
3645N/A
149N/A };
149N/A private static StringBuffer debugBuf = new StringBuffer();
149N/A
149N/A public void main(Provider p) throws Exception {
149N/A // NSS reports CKR_DEVICE_ERROR when the data passed to
149N/A // its EncryptUpdate/DecryptUpdate is not multiple of blocks
149N/A int firstBlkSize = 16;
149N/A boolean status = true;
149N/A Random random = new Random();
149N/A try {
149N/A for (int i = 0; i < TEST_LIST.length; i++) {
149N/A CI currTest = TEST_LIST[i];
149N/A System.out.println("===" + currTest.transformation + "===");
149N/A try {
149N/A KeyGenerator kg =
149N/A KeyGenerator.getInstance(currTest.keyAlgo, p);
149N/A SecretKey key = kg.generateKey();
149N/A Cipher c1 = Cipher.getInstance(currTest.transformation, p);
149N/A Cipher c2 = Cipher.getInstance(currTest.transformation,
149N/A "SunJCE");
149N/A
149N/A byte[] plainTxt = new byte[currTest.dataSize];
149N/A random.nextBytes(plainTxt);
149N/A System.out.println("Testing inLen = " + plainTxt.length);
149N/A
149N/A c2.init(Cipher.ENCRYPT_MODE, key);
149N/A AlgorithmParameters params = c2.getParameters();
149N/A byte[] answer = c2.doFinal(plainTxt);
149N/A System.out.println("Encryption tests: START");
149N/A test(c1, Cipher.ENCRYPT_MODE, key, params, firstBlkSize,
149N/A plainTxt, answer);
149N/A System.out.println("Encryption tests: DONE");
149N/A c2.init(Cipher.DECRYPT_MODE, key, params);
149N/A byte[] answer2 = c2.doFinal(answer);
149N/A System.out.println("Decryption tests: START");
149N/A test(c1, Cipher.DECRYPT_MODE, key, params, firstBlkSize,
149N/A answer, answer2);
149N/A System.out.println("Decryption tests: DONE");
149N/A } catch (NoSuchAlgorithmException nsae) {
149N/A System.out.println("Skipping unsupported algorithm: " +
149N/A nsae);
149N/A }
149N/A }
149N/A } catch (Exception ex) {
149N/A // print out debug info when exception is encountered
149N/A if (debugBuf != null) {
149N/A System.out.println(debugBuf.toString());
149N/A debugBuf = new StringBuffer();
149N/A }
149N/A throw ex;
149N/A }
149N/A }
149N/A
149N/A private static void test(Cipher cipher, int mode, SecretKey key,
149N/A AlgorithmParameters params, int firstBlkSize,
149N/A byte[] in, byte[] answer) throws Exception {
149N/A // test setup
149N/A long startTime, endTime;
149N/A cipher.init(mode, key, params);
149N/A int outLen = cipher.getOutputSize(in.length);
149N/A //debugOut("Estimated output size = " + outLen + "\n");
149N/A
149N/A // test data preparation
149N/A ByteBuffer inBuf = ByteBuffer.allocate(in.length);
149N/A inBuf.put(in);
149N/A inBuf.position(0);
149N/A ByteBuffer inDirectBuf = ByteBuffer.allocateDirect(in.length);
149N/A inDirectBuf.put(in);
149N/A inDirectBuf.position(0);
149N/A ByteBuffer outBuf = ByteBuffer.allocate(outLen);
149N/A ByteBuffer outDirectBuf = ByteBuffer.allocateDirect(outLen);
149N/A
149N/A // test#1: byte[] in + byte[] out
149N/A //debugOut("Test#1:\n");
149N/A
149N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
149N/A
149N/A startTime = System.nanoTime();
149N/A byte[] temp = cipher.update(in, 0, firstBlkSize);
149N/A if (temp != null && temp.length > 0) {
149N/A baos.write(temp, 0, temp.length);
149N/A }
149N/A temp = cipher.doFinal(in, firstBlkSize, in.length - firstBlkSize);
149N/A if (temp != null && temp.length > 0) {
149N/A baos.write(temp, 0, temp.length);
149N/A }
149N/A byte[] testOut1 = baos.toByteArray();
149N/A endTime = System.nanoTime();
149N/A perfOut("stream InBuf + stream OutBuf: " +
149N/A (endTime - startTime));
149N/A match(testOut1, answer);
149N/A
149N/A // test#2: Non-direct Buffer in + non-direct Buffer out
149N/A //debugOut("Test#2:\n");
149N/A //debugOut("inputBuf: " + inBuf + "\n");
149N/A //debugOut("outputBuf: " + outBuf + "\n");
149N/A
149N/A startTime = System.nanoTime();
149N/A cipher.update(inBuf, outBuf);
149N/A cipher.doFinal(inBuf, outBuf);
149N/A endTime = System.nanoTime();
149N/A perfOut("non-direct InBuf + non-direct OutBuf: " +
149N/A (endTime - startTime));
149N/A match(outBuf, answer);
149N/A
149N/A // test#3: Direct Buffer in + direc Buffer out
149N/A //debugOut("Test#3:\n");
149N/A //debugOut("(pre) inputBuf: " + inDirectBuf + "\n");
149N/A //debugOut("(pre) outputBuf: " + outDirectBuf + "\n");
149N/A
149N/A startTime = System.nanoTime();
149N/A cipher.update(inDirectBuf, outDirectBuf);
149N/A cipher.doFinal(inDirectBuf, outDirectBuf);
149N/A endTime = System.nanoTime();
149N/A perfOut("direct InBuf + direct OutBuf: " +
149N/A (endTime - startTime));
149N/A
149N/A //debugOut("(post) inputBuf: " + inDirectBuf + "\n");
149N/A //debugOut("(post) outputBuf: " + outDirectBuf + "\n");
149N/A match(outDirectBuf, answer);
149N/A
149N/A // test#4: Direct Buffer in + non-direct Buffer out
149N/A //debugOut("Test#4:\n");
149N/A inDirectBuf.position(0);
149N/A outBuf.position(0);
149N/A //debugOut("inputBuf: " + inDirectBuf + "\n");
149N/A //debugOut("outputBuf: " + outBuf + "\n");
149N/A
149N/A startTime = System.nanoTime();
149N/A cipher.update(inDirectBuf, outBuf);
149N/A cipher.doFinal(inDirectBuf, outBuf);
149N/A endTime = System.nanoTime();
149N/A perfOut("direct InBuf + non-direct OutBuf: " +
149N/A (endTime - startTime));
149N/A match(outBuf, answer);
149N/A
149N/A // test#5: Non-direct Buffer in + direct Buffer out
149N/A //debugOut("Test#5:\n");
149N/A inBuf.position(0);
149N/A outDirectBuf.position(0);
149N/A
149N/A //debugOut("(pre) inputBuf: " + inBuf + "\n");
149N/A //debugOut("(pre) outputBuf: " + outDirectBuf + "\n");
149N/A
149N/A startTime = System.nanoTime();
149N/A cipher.update(inBuf, outDirectBuf);
149N/A cipher.doFinal(inBuf, outDirectBuf);
149N/A endTime = System.nanoTime();
149N/A perfOut("non-direct InBuf + direct OutBuf: " +
149N/A (endTime - startTime));
149N/A
149N/A //debugOut("(post) inputBuf: " + inBuf + "\n");
149N/A //debugOut("(post) outputBuf: " + outDirectBuf + "\n");
149N/A match(outDirectBuf, answer);
149N/A
149N/A debugBuf = null;
149N/A }
149N/A
149N/A private static void perfOut(String msg) {
149N/A if (debugBuf != null) {
149N/A debugBuf.append("PERF>" + msg);
149N/A }
149N/A }
149N/A
149N/A private static void debugOut(String msg) {
149N/A if (debugBuf != null) {
149N/A debugBuf.append(msg);
149N/A }
149N/A }
149N/A
149N/A private static void match(byte[] b1, byte[] b2) throws Exception {
149N/A if (b1.length != b2.length) {
149N/A debugOut("got len : " + b1.length + "\n");
149N/A debugOut("expect len: " + b2.length + "\n");
149N/A throw new Exception("mismatch - different length! got: " + b1.length + ", expect: " + b2.length + "\n");
149N/A } else {
149N/A for (int i = 0; i < b1.length; i++) {
149N/A if (b1[i] != b2[i]) {
149N/A debugOut("got : " + toString(b1) + "\n");
149N/A debugOut("expect: " + toString(b2) + "\n");
149N/A throw new Exception("mismatch");
149N/A }
149N/A }
149N/A }
149N/A }
149N/A
149N/A private static void match(ByteBuffer bb, byte[] answer) throws Exception {
149N/A byte[] bbTemp = new byte[bb.position()];
149N/A bb.position(0);
149N/A bb.get(bbTemp, 0, bbTemp.length);
149N/A match(bbTemp, answer);
149N/A }
149N/A
149N/A public static void main(String[] args) throws Exception {
149N/A main(new TestSymmCiphers());
149N/A }
149N/A}