Path: blob/master/test/jdk/sun/security/pkcs11/Cipher/TestSymmCiphers.java
41152 views
/*1* Copyright (c) 2008, 2018, 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*/2223/*24* @test25* @bug 4898461 660449626* @summary basic test for symmetric ciphers with padding27* @author Valerie Peng28* @library /test/lib ..29* @key randomness30* @modules jdk.crypto.cryptoki31* @run main/othervm TestSymmCiphers32* @run main/othervm -Djava.security.manager=allow TestSymmCiphers sm33*/3435import java.io.ByteArrayOutputStream;36import java.nio.ByteBuffer;37import java.security.AlgorithmParameters;38import java.security.NoSuchAlgorithmException;39import java.security.Provider;40import java.util.Random;41import javax.crypto.Cipher;42import javax.crypto.KeyGenerator;43import javax.crypto.SecretKey;4445public class TestSymmCiphers extends PKCS11Test {4647private static class CI { // class for holding Cipher Information4849String transformation;50String keyAlgo;51int dataSize;5253CI(String transformation, String keyAlgo, int dataSize) {54this.transformation = transformation;55this.keyAlgo = keyAlgo;56this.dataSize = dataSize;57}58}59private static final CI[] TEST_LIST = {60new CI("ARCFOUR", "ARCFOUR", 400),61new CI("RC4", "RC4", 401),62new CI("DES/CBC/NoPadding", "DES", 400),63new CI("DESede/CBC/NoPadding", "DESede", 160),64new CI("AES/CBC/NoPadding", "AES", 4800),65new CI("Blowfish/CBC/NoPadding", "Blowfish", 24),66new CI("DES/cbc/PKCS5Padding", "DES", 6401),67new CI("DESede/CBC/PKCS5Padding", "DESede", 402),68new CI("AES/CBC/PKCS5Padding", "AES", 30),69new CI("Blowfish/CBC/PKCS5Padding", "Blowfish", 19),70new CI("DES/ECB/NoPadding", "DES", 400),71new CI("DESede/ECB/NoPadding", "DESede", 160),72new CI("AES/ECB/NoPadding", "AES", 4800),73new CI("DES/ECB/PKCS5Padding", "DES", 32),74new CI("DES/ECB/PKCS5Padding", "DES", 6400),75new CI("DESede/ECB/PKCS5Padding", "DESede", 400),76new CI("AES/ECB/PKCS5Padding", "AES", 64),7778new CI("DES", "DES", 6400),79new CI("DESede", "DESede", 408),80new CI("AES", "AES", 128),8182new CI("AES/CTR/NoPadding", "AES", 3200)8384};85private static StringBuffer debugBuf = new StringBuffer();8687@Override88public void main(Provider p) throws Exception {89// NSS reports CKR_DEVICE_ERROR when the data passed to90// its EncryptUpdate/DecryptUpdate is not multiple of blocks91int firstBlkSize = 16;92boolean status = true;93Random random = new Random();94try {95for (int i = 0; i < TEST_LIST.length; i++) {96CI currTest = TEST_LIST[i];97System.out.println("===" + currTest.transformation + "===");98try {99KeyGenerator kg =100KeyGenerator.getInstance(currTest.keyAlgo, p);101SecretKey key = kg.generateKey();102Cipher c1 = Cipher.getInstance(currTest.transformation, p);103Cipher c2 = Cipher.getInstance(currTest.transformation,104"SunJCE");105106byte[] plainTxt = new byte[currTest.dataSize];107random.nextBytes(plainTxt);108System.out.println("Testing inLen = " + plainTxt.length);109110c2.init(Cipher.ENCRYPT_MODE, key);111AlgorithmParameters params = c2.getParameters();112byte[] answer = c2.doFinal(plainTxt);113System.out.println("Encryption tests: START");114test(c1, Cipher.ENCRYPT_MODE, key, params, firstBlkSize,115plainTxt, answer);116System.out.println("Encryption tests: DONE");117c2.init(Cipher.DECRYPT_MODE, key, params);118byte[] answer2 = c2.doFinal(answer);119System.out.println("Decryption tests: START");120test(c1, Cipher.DECRYPT_MODE, key, params, firstBlkSize,121answer, answer2);122System.out.println("Decryption tests: DONE");123} catch (NoSuchAlgorithmException nsae) {124System.out.println("Skipping unsupported algorithm: " +125nsae);126}127}128} catch (Exception ex) {129// print out debug info when exception is encountered130if (debugBuf != null) {131System.out.println(debugBuf.toString());132debugBuf = new StringBuffer();133}134throw ex;135}136}137138private static void test(Cipher cipher, int mode, SecretKey key,139AlgorithmParameters params, int firstBlkSize,140byte[] in, byte[] answer) throws Exception {141// test setup142long startTime, endTime;143cipher.init(mode, key, params);144int outLen = cipher.getOutputSize(in.length);145//debugOut("Estimated output size = " + outLen + "\n");146147// test data preparation148ByteBuffer inBuf = ByteBuffer.allocate(in.length);149inBuf.put(in);150inBuf.position(0);151ByteBuffer inDirectBuf = ByteBuffer.allocateDirect(in.length);152inDirectBuf.put(in);153inDirectBuf.position(0);154ByteBuffer outBuf = ByteBuffer.allocate(outLen);155ByteBuffer outDirectBuf = ByteBuffer.allocateDirect(outLen);156157// test#1: byte[] in + byte[] out158//debugOut("Test#1:\n");159160ByteArrayOutputStream baos = new ByteArrayOutputStream();161162startTime = System.nanoTime();163byte[] temp = cipher.update(in, 0, firstBlkSize);164if (temp != null && temp.length > 0) {165baos.write(temp, 0, temp.length);166}167temp = cipher.doFinal(in, firstBlkSize, in.length - firstBlkSize);168if (temp != null && temp.length > 0) {169baos.write(temp, 0, temp.length);170}171byte[] testOut1 = baos.toByteArray();172endTime = System.nanoTime();173perfOut("stream InBuf + stream OutBuf: " +174(endTime - startTime));175match(testOut1, answer);176177// test#2: Non-direct Buffer in + non-direct Buffer out178//debugOut("Test#2:\n");179//debugOut("inputBuf: " + inBuf + "\n");180//debugOut("outputBuf: " + outBuf + "\n");181182startTime = System.nanoTime();183cipher.update(inBuf, outBuf);184cipher.doFinal(inBuf, outBuf);185endTime = System.nanoTime();186perfOut("non-direct InBuf + non-direct OutBuf: " +187(endTime - startTime));188match(outBuf, answer);189190// test#3: Direct Buffer in + direc Buffer out191//debugOut("Test#3:\n");192//debugOut("(pre) inputBuf: " + inDirectBuf + "\n");193//debugOut("(pre) outputBuf: " + outDirectBuf + "\n");194195startTime = System.nanoTime();196cipher.update(inDirectBuf, outDirectBuf);197cipher.doFinal(inDirectBuf, outDirectBuf);198endTime = System.nanoTime();199perfOut("direct InBuf + direct OutBuf: " +200(endTime - startTime));201202//debugOut("(post) inputBuf: " + inDirectBuf + "\n");203//debugOut("(post) outputBuf: " + outDirectBuf + "\n");204match(outDirectBuf, answer);205206// test#4: Direct Buffer in + non-direct Buffer out207//debugOut("Test#4:\n");208inDirectBuf.position(0);209outBuf.position(0);210//debugOut("inputBuf: " + inDirectBuf + "\n");211//debugOut("outputBuf: " + outBuf + "\n");212213startTime = System.nanoTime();214cipher.update(inDirectBuf, outBuf);215cipher.doFinal(inDirectBuf, outBuf);216endTime = System.nanoTime();217perfOut("direct InBuf + non-direct OutBuf: " +218(endTime - startTime));219match(outBuf, answer);220221// test#5: Non-direct Buffer in + direct Buffer out222//debugOut("Test#5:\n");223inBuf.position(0);224outDirectBuf.position(0);225226//debugOut("(pre) inputBuf: " + inBuf + "\n");227//debugOut("(pre) outputBuf: " + outDirectBuf + "\n");228229startTime = System.nanoTime();230cipher.update(inBuf, outDirectBuf);231cipher.doFinal(inBuf, outDirectBuf);232endTime = System.nanoTime();233perfOut("non-direct InBuf + direct OutBuf: " +234(endTime - startTime));235236//debugOut("(post) inputBuf: " + inBuf + "\n");237//debugOut("(post) outputBuf: " + outDirectBuf + "\n");238match(outDirectBuf, answer);239240debugBuf = null;241}242243private static void perfOut(String msg) {244if (debugBuf != null) {245debugBuf.append("PERF>" + msg);246}247}248249private static void debugOut(String msg) {250if (debugBuf != null) {251debugBuf.append(msg);252}253}254255private static void match(byte[] b1, byte[] b2) throws Exception {256if (b1.length != b2.length) {257debugOut("got len : " + b1.length + "\n");258debugOut("expect len: " + b2.length + "\n");259throw new Exception("mismatch - different length! got: " + b1.length + ", expect: " + b2.length + "\n");260} else {261for (int i = 0; i < b1.length; i++) {262if (b1[i] != b2[i]) {263debugOut("got : " + toString(b1) + "\n");264debugOut("expect: " + toString(b2) + "\n");265throw new Exception("mismatch");266}267}268}269}270271private static void match(ByteBuffer bb, byte[] answer) throws Exception {272byte[] bbTemp = new byte[bb.position()];273bb.position(0);274bb.get(bbTemp, 0, bbTemp.length);275match(bbTemp, answer);276}277278public static void main(String[] args) throws Exception {279main(new TestSymmCiphers(), args);280}281}282283284