Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Cipher/TestSymmCiphers.java
38855 views
/*1* Copyright (c) 2008, 2016, 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 ..29* @key randomness30* @run main/othervm TestSymmCiphers31* @run main/othervm TestSymmCiphers sm32*/3334import java.io.ByteArrayOutputStream;35import java.nio.ByteBuffer;36import java.security.AlgorithmParameters;37import java.security.NoSuchAlgorithmException;38import java.security.Provider;39import java.util.Random;40import javax.crypto.Cipher;41import javax.crypto.KeyGenerator;42import javax.crypto.SecretKey;4344public class TestSymmCiphers extends PKCS11Test {4546private static class CI { // class for holding Cipher Information4748String transformation;49String keyAlgo;50int dataSize;5152CI(String transformation, String keyAlgo, int dataSize) {53this.transformation = transformation;54this.keyAlgo = keyAlgo;55this.dataSize = dataSize;56}57}58private static final CI[] TEST_LIST = {59new CI("ARCFOUR", "ARCFOUR", 400),60new CI("RC4", "RC4", 401),61new CI("DES/CBC/NoPadding", "DES", 400),62new CI("DESede/CBC/NoPadding", "DESede", 160),63new CI("AES/CBC/NoPadding", "AES", 4800),64new CI("Blowfish/CBC/NoPadding", "Blowfish", 24),65new CI("DES/cbc/PKCS5Padding", "DES", 6401),66new CI("DESede/CBC/PKCS5Padding", "DESede", 402),67new CI("AES/CBC/PKCS5Padding", "AES", 30),68new CI("Blowfish/CBC/PKCS5Padding", "Blowfish", 19),69new CI("DES/ECB/NoPadding", "DES", 400),70new CI("DESede/ECB/NoPadding", "DESede", 160),71new CI("AES/ECB/NoPadding", "AES", 4800),72new CI("DES/ECB/PKCS5Padding", "DES", 32),73new CI("DES/ECB/PKCS5Padding", "DES", 6400),74new CI("DESede/ECB/PKCS5Padding", "DESede", 400),75new CI("AES/ECB/PKCS5Padding", "AES", 64),7677new CI("DES", "DES", 6400),78new CI("DESede", "DESede", 408),79new CI("AES", "AES", 128),8081new CI("AES/CTR/NoPadding", "AES", 3200)8283};84private static StringBuffer debugBuf = new StringBuffer();8586@Override87public void main(Provider p) throws Exception {88// NSS reports CKR_DEVICE_ERROR when the data passed to89// its EncryptUpdate/DecryptUpdate is not multiple of blocks90int firstBlkSize = 16;91boolean status = true;92Random random = new Random();93try {94for (int i = 0; i < TEST_LIST.length; i++) {95CI currTest = TEST_LIST[i];96System.out.println("===" + currTest.transformation + "===");97try {98KeyGenerator kg =99KeyGenerator.getInstance(currTest.keyAlgo, p);100SecretKey key = kg.generateKey();101Cipher c1 = Cipher.getInstance(currTest.transformation, p);102Cipher c2 = Cipher.getInstance(currTest.transformation,103"SunJCE");104105byte[] plainTxt = new byte[currTest.dataSize];106random.nextBytes(plainTxt);107System.out.println("Testing inLen = " + plainTxt.length);108109c2.init(Cipher.ENCRYPT_MODE, key);110AlgorithmParameters params = c2.getParameters();111byte[] answer = c2.doFinal(plainTxt);112System.out.println("Encryption tests: START");113test(c1, Cipher.ENCRYPT_MODE, key, params, firstBlkSize,114plainTxt, answer);115System.out.println("Encryption tests: DONE");116c2.init(Cipher.DECRYPT_MODE, key, params);117byte[] answer2 = c2.doFinal(answer);118System.out.println("Decryption tests: START");119test(c1, Cipher.DECRYPT_MODE, key, params, firstBlkSize,120answer, answer2);121System.out.println("Decryption tests: DONE");122} catch (NoSuchAlgorithmException nsae) {123System.out.println("Skipping unsupported algorithm: " +124nsae);125}126}127} catch (Exception ex) {128// print out debug info when exception is encountered129if (debugBuf != null) {130System.out.println(debugBuf.toString());131debugBuf = new StringBuffer();132}133throw ex;134}135}136137private static void test(Cipher cipher, int mode, SecretKey key,138AlgorithmParameters params, int firstBlkSize,139byte[] in, byte[] answer) throws Exception {140// test setup141long startTime, endTime;142cipher.init(mode, key, params);143int outLen = cipher.getOutputSize(in.length);144//debugOut("Estimated output size = " + outLen + "\n");145146// test data preparation147ByteBuffer inBuf = ByteBuffer.allocate(in.length);148inBuf.put(in);149inBuf.position(0);150ByteBuffer inDirectBuf = ByteBuffer.allocateDirect(in.length);151inDirectBuf.put(in);152inDirectBuf.position(0);153ByteBuffer outBuf = ByteBuffer.allocate(outLen);154ByteBuffer outDirectBuf = ByteBuffer.allocateDirect(outLen);155156// test#1: byte[] in + byte[] out157//debugOut("Test#1:\n");158159ByteArrayOutputStream baos = new ByteArrayOutputStream();160161startTime = System.nanoTime();162byte[] temp = cipher.update(in, 0, firstBlkSize);163if (temp != null && temp.length > 0) {164baos.write(temp, 0, temp.length);165}166temp = cipher.doFinal(in, firstBlkSize, in.length - firstBlkSize);167if (temp != null && temp.length > 0) {168baos.write(temp, 0, temp.length);169}170byte[] testOut1 = baos.toByteArray();171endTime = System.nanoTime();172perfOut("stream InBuf + stream OutBuf: " +173(endTime - startTime));174match(testOut1, answer);175176// test#2: Non-direct Buffer in + non-direct Buffer out177//debugOut("Test#2:\n");178//debugOut("inputBuf: " + inBuf + "\n");179//debugOut("outputBuf: " + outBuf + "\n");180181startTime = System.nanoTime();182cipher.update(inBuf, outBuf);183cipher.doFinal(inBuf, outBuf);184endTime = System.nanoTime();185perfOut("non-direct InBuf + non-direct OutBuf: " +186(endTime - startTime));187match(outBuf, answer);188189// test#3: Direct Buffer in + direc Buffer out190//debugOut("Test#3:\n");191//debugOut("(pre) inputBuf: " + inDirectBuf + "\n");192//debugOut("(pre) outputBuf: " + outDirectBuf + "\n");193194startTime = System.nanoTime();195cipher.update(inDirectBuf, outDirectBuf);196cipher.doFinal(inDirectBuf, outDirectBuf);197endTime = System.nanoTime();198perfOut("direct InBuf + direct OutBuf: " +199(endTime - startTime));200201//debugOut("(post) inputBuf: " + inDirectBuf + "\n");202//debugOut("(post) outputBuf: " + outDirectBuf + "\n");203match(outDirectBuf, answer);204205// test#4: Direct Buffer in + non-direct Buffer out206//debugOut("Test#4:\n");207inDirectBuf.position(0);208outBuf.position(0);209//debugOut("inputBuf: " + inDirectBuf + "\n");210//debugOut("outputBuf: " + outBuf + "\n");211212startTime = System.nanoTime();213cipher.update(inDirectBuf, outBuf);214cipher.doFinal(inDirectBuf, outBuf);215endTime = System.nanoTime();216perfOut("direct InBuf + non-direct OutBuf: " +217(endTime - startTime));218match(outBuf, answer);219220// test#5: Non-direct Buffer in + direct Buffer out221//debugOut("Test#5:\n");222inBuf.position(0);223outDirectBuf.position(0);224225//debugOut("(pre) inputBuf: " + inBuf + "\n");226//debugOut("(pre) outputBuf: " + outDirectBuf + "\n");227228startTime = System.nanoTime();229cipher.update(inBuf, outDirectBuf);230cipher.doFinal(inBuf, outDirectBuf);231endTime = System.nanoTime();232perfOut("non-direct InBuf + direct OutBuf: " +233(endTime - startTime));234235//debugOut("(post) inputBuf: " + inBuf + "\n");236//debugOut("(post) outputBuf: " + outDirectBuf + "\n");237match(outDirectBuf, answer);238239debugBuf = null;240}241242private static void perfOut(String msg) {243if (debugBuf != null) {244debugBuf.append("PERF>" + msg);245}246}247248private static void debugOut(String msg) {249if (debugBuf != null) {250debugBuf.append(msg);251}252}253254private static void match(byte[] b1, byte[] b2) throws Exception {255if (b1.length != b2.length) {256debugOut("got len : " + b1.length + "\n");257debugOut("expect len: " + b2.length + "\n");258throw new Exception("mismatch - different length! got: " + b1.length + ", expect: " + b2.length + "\n");259} else {260for (int i = 0; i < b1.length; i++) {261if (b1[i] != b2[i]) {262debugOut("got : " + toString(b1) + "\n");263debugOut("expect: " + toString(b2) + "\n");264throw new Exception("mismatch");265}266}267}268}269270private static void match(ByteBuffer bb, byte[] answer) throws Exception {271byte[] bbTemp = new byte[bb.position()];272bb.position(0);273bb.get(bbTemp, 0, bbTemp.length);274match(bbTemp, answer);275}276277public static void main(String[] args) throws Exception {278main(new TestSymmCiphers(), args);279}280}281282283