Path: blob/master/test/jdk/sun/security/pkcs11/Cipher/TestSymmCiphersNoPad.java
41152 views
/*1* Copyright (c) 2007, 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 4898484 6604496 800128426* @summary basic test for symmetric ciphers with no padding27* @author Valerie Peng28* @library /test/lib ..29* @key randomness30* @modules jdk.crypto.cryptoki31* @run main/othervm TestSymmCiphersNoPad32* @run main/othervm -Djava.security.manager=allow TestSymmCiphersNoPad sm33*/3435import java.io.ByteArrayInputStream;36import java.io.ByteArrayOutputStream;37import java.io.InputStream;38import java.nio.ByteBuffer;39import java.security.AlgorithmParameters;40import java.security.NoSuchAlgorithmException;41import java.security.Provider;42import java.util.Random;43import javax.crypto.Cipher;44import javax.crypto.CipherInputStream;45import javax.crypto.KeyGenerator;46import javax.crypto.SecretKey;4748public class TestSymmCiphersNoPad extends PKCS11Test {4950private static class CI { // class for holding Cipher Information51String transformation;52String keyAlgo;53int dataSize;5455CI(String transformation, String keyAlgo, int dataSize) {56this.transformation = transformation;57this.keyAlgo = keyAlgo;58this.dataSize = dataSize;59}60}6162private static final CI TEST_LIST[] = {63new CI("ARCFOUR", "ARCFOUR", 400),64new CI("RC4", "RC4", 401),65new CI("DES/CBC/NoPadding", "DES", 400),66new CI("DESede/CBC/NoPadding", "DESede", 160),67new CI("AES/CBC/NoPadding", "AES", 4800),68new CI("Blowfish/CBC/NoPadding", "Blowfish", 24),69new CI("AES/CTR/NoPadding", "AES", 1600),70new CI("AES/CTR/NoPadding", "AES", 65)71};7273private static StringBuffer debugBuf;7475@Override76public void main(Provider p) throws Exception {77boolean status = true;78Random random = new Random();79try {80for (int i = 0; i < TEST_LIST.length; i++) {81CI currTest = TEST_LIST[i];82System.out.println("===" + currTest.transformation + "===");83try {84KeyGenerator kg =85KeyGenerator.getInstance(currTest.keyAlgo, p);86SecretKey key = kg.generateKey();87Cipher c1 = Cipher.getInstance(currTest.transformation, p);88Cipher c2 = Cipher.getInstance(currTest.transformation,89"SunJCE");9091byte[] plainTxt = new byte[currTest.dataSize];92random.nextBytes(plainTxt);93System.out.println("Testing inLen = " + plainTxt.length);9495c2.init(Cipher.ENCRYPT_MODE, key);96AlgorithmParameters params = c2.getParameters();97byte[] answer = c2.doFinal(plainTxt);98test(c1, Cipher.ENCRYPT_MODE, key, params,99plainTxt, answer);100System.out.println("Encryption tests: DONE");101c2.init(Cipher.DECRYPT_MODE, key, params);102byte[] answer2 = c2.doFinal(answer);103test(c1, Cipher.DECRYPT_MODE, key, params,104answer, answer2);105System.out.println("Decryption tests: DONE");106} catch (NoSuchAlgorithmException nsae) {107System.out.println("Skipping unsupported algorithm: " +108nsae);109}110}111} catch (Exception ex) {112// print out debug info when exception is encountered113if (debugBuf != null) {114System.out.println(debugBuf.toString());115}116throw ex;117}118}119120private static void test(Cipher cipher, int mode, SecretKey key,121AlgorithmParameters params,122byte[] in, byte[] answer) throws Exception {123// test setup124debugBuf = new StringBuffer();125cipher.init(mode, key, params);126int outLen = cipher.getOutputSize(in.length);127debugBuf.append("Estimated output size = " + outLen + "\n");128129// test data preparation130ByteBuffer inBuf = ByteBuffer.allocate(in.length);131inBuf.put(in);132inBuf.position(0);133ByteBuffer inDirectBuf = ByteBuffer.allocateDirect(in.length);134inDirectBuf.put(in);135inDirectBuf.position(0);136ByteBuffer outBuf = ByteBuffer.allocate(outLen);137ByteBuffer outDirectBuf = ByteBuffer.allocateDirect(outLen);138139// test#1: byte[] in + byte[] out140debugBuf.append("Test#1:\n");141ByteArrayOutputStream baos = new ByteArrayOutputStream();142byte[] testOut1 = cipher.update(in, 0, 16);143if (testOut1 != null) baos.write(testOut1, 0, testOut1.length);144testOut1 = cipher.doFinal(in, 16, in.length-16);145if (testOut1 != null) baos.write(testOut1, 0, testOut1.length);146testOut1 = baos.toByteArray();147match(testOut1, answer);148149// test#2: Non-direct Buffer in + non-direct Buffer out150debugBuf.append("Test#2:\n");151debugBuf.append("inputBuf: " + inBuf + "\n");152debugBuf.append("outputBuf: " + outBuf + "\n");153cipher.update(inBuf, outBuf);154cipher.doFinal(inBuf, outBuf);155match(outBuf, answer);156157// test#3: Direct Buffer in + direc Buffer out158debugBuf.append("Test#3:\n");159debugBuf.append("(pre) inputBuf: " + inDirectBuf + "\n");160debugBuf.append("(pre) outputBuf: " + outDirectBuf + "\n");161cipher.update(inDirectBuf, outDirectBuf);162cipher.doFinal(inDirectBuf, outDirectBuf);163164debugBuf.append("(post) inputBuf: " + inDirectBuf + "\n");165debugBuf.append("(post) outputBuf: " + outDirectBuf + "\n");166match(outDirectBuf, answer);167168// test#4: Direct Buffer in + non-direct Buffer out169debugBuf.append("Test#4:\n");170inDirectBuf.position(0);171outBuf.position(0);172debugBuf.append("inputBuf: " + inDirectBuf + "\n");173debugBuf.append("outputBuf: " + outBuf + "\n");174cipher.update(inDirectBuf, outBuf);175cipher.doFinal(inDirectBuf, outBuf);176match(outBuf, answer);177178// test#5: Non-direct Buffer in + direct Buffer out179debugBuf.append("Test#5:\n");180inBuf.position(0);181outDirectBuf.position(0);182183debugBuf.append("(pre) inputBuf: " + inBuf + "\n");184debugBuf.append("(pre) outputBuf: " + outDirectBuf + "\n");185186cipher.update(inBuf, outDirectBuf);187cipher.doFinal(inBuf, outDirectBuf);188189debugBuf.append("(post) inputBuf: " + inBuf + "\n");190debugBuf.append("(post) outputBuf: " + outDirectBuf + "\n");191match(outDirectBuf, answer);192193// test#6: Streams194debugBuf.append("Test#6: Streaming\n");195outBuf.position(0);196InputStream stream =197new CipherInputStream(new ByteArrayInputStream(in), cipher);198byte[] data = new byte[1024];199int bytesRead = 0;200try {201while (bytesRead >= 0) {202bytesRead = stream.read(data);203if (bytesRead == -1)204break;205debugBuf.append("bytesRead: " + bytesRead);206debugBuf.append("\toutBuf.position(): " + outBuf.position() +207"\n");208outBuf.put(data, 0 , bytesRead);209}210} catch (Exception ex) {211debugBuf.append("Caught Exception during stream reading\n");212throw ex;213}214match(outBuf, answer);215216debugBuf = null;217}218219private static void match(byte[] b1, byte[] b2) throws Exception {220if (b1.length != b2.length) {221debugBuf.append("got len : " + b1.length + "\n");222debugBuf.append("expect len: " + b2.length + "\n");223throw new Exception("mismatch - different length!\n");224} else {225for (int i = 0; i < b1.length; i++) {226if (b1[i] != b2[i]) {227debugBuf.append("got : " + toString(b1) + "\n");228debugBuf.append("expect: " + toString(b2) + "\n");229throw new Exception("mismatch");230}231}232}233}234235private static void match(ByteBuffer bb, byte[] answer) throws Exception {236byte[] bbTemp = new byte[bb.position()];237bb.position(0);238bb.get(bbTemp, 0, bbTemp.length);239match(bbTemp, answer);240}241242public static void main(String[] args) throws Exception {243main(new TestSymmCiphersNoPad(), args);244}245}246247248