Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Cipher/TestSymmCiphersNoPad.java
38855 views
/*1* Copyright (c) 2007, 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 4898484 6604496 800128426* @summary basic test for symmetric ciphers with no padding27* @author Valerie Peng28* @library ..29* @key randomness30* @run main/othervm TestSymmCiphersNoPad31* @run main/othervm TestSymmCiphersNoPad sm32*/3334import java.io.ByteArrayInputStream;35import java.io.ByteArrayOutputStream;36import java.io.InputStream;37import java.nio.ByteBuffer;38import java.security.AlgorithmParameters;39import java.security.NoSuchAlgorithmException;40import java.security.Provider;41import java.util.Random;42import javax.crypto.Cipher;43import javax.crypto.CipherInputStream;44import javax.crypto.KeyGenerator;45import javax.crypto.SecretKey;4647public class TestSymmCiphersNoPad extends PKCS11Test {4849private static class CI { // class for holding Cipher Information50String transformation;51String keyAlgo;52int dataSize;5354CI(String transformation, String keyAlgo, int dataSize) {55this.transformation = transformation;56this.keyAlgo = keyAlgo;57this.dataSize = dataSize;58}59}6061private static final CI TEST_LIST[] = {62new CI("ARCFOUR", "ARCFOUR", 400),63new CI("RC4", "RC4", 401),64new CI("DES/CBC/NoPadding", "DES", 400),65new CI("DESede/CBC/NoPadding", "DESede", 160),66new CI("AES/CBC/NoPadding", "AES", 4800),67new CI("Blowfish/CBC/NoPadding", "Blowfish", 24),68new CI("AES/CTR/NoPadding", "AES", 1600),69new CI("AES/CTR/NoPadding", "AES", 65)70};7172private static StringBuffer debugBuf;7374@Override75public void main(Provider p) throws Exception {76boolean status = true;77Random random = new Random();78try {79for (int i = 0; i < TEST_LIST.length; i++) {80CI currTest = TEST_LIST[i];81System.out.println("===" + currTest.transformation + "===");82try {83KeyGenerator kg =84KeyGenerator.getInstance(currTest.keyAlgo, p);85SecretKey key = kg.generateKey();86Cipher c1 = Cipher.getInstance(currTest.transformation, p);87Cipher c2 = Cipher.getInstance(currTest.transformation,88"SunJCE");8990byte[] plainTxt = new byte[currTest.dataSize];91random.nextBytes(plainTxt);92System.out.println("Testing inLen = " + plainTxt.length);9394c2.init(Cipher.ENCRYPT_MODE, key);95AlgorithmParameters params = c2.getParameters();96byte[] answer = c2.doFinal(plainTxt);97test(c1, Cipher.ENCRYPT_MODE, key, params,98plainTxt, answer);99System.out.println("Encryption tests: DONE");100c2.init(Cipher.DECRYPT_MODE, key, params);101byte[] answer2 = c2.doFinal(answer);102test(c1, Cipher.DECRYPT_MODE, key, params,103answer, answer2);104System.out.println("Decryption tests: DONE");105} catch (NoSuchAlgorithmException nsae) {106System.out.println("Skipping unsupported algorithm: " +107nsae);108}109}110} catch (Exception ex) {111// print out debug info when exception is encountered112if (debugBuf != null) {113System.out.println(debugBuf.toString());114}115throw ex;116}117}118119private static void test(Cipher cipher, int mode, SecretKey key,120AlgorithmParameters params,121byte[] in, byte[] answer) throws Exception {122// test setup123debugBuf = new StringBuffer();124cipher.init(mode, key, params);125int outLen = cipher.getOutputSize(in.length);126debugBuf.append("Estimated output size = " + outLen + "\n");127128// test data preparation129ByteBuffer inBuf = ByteBuffer.allocate(in.length);130inBuf.put(in);131inBuf.position(0);132ByteBuffer inDirectBuf = ByteBuffer.allocateDirect(in.length);133inDirectBuf.put(in);134inDirectBuf.position(0);135ByteBuffer outBuf = ByteBuffer.allocate(outLen);136ByteBuffer outDirectBuf = ByteBuffer.allocateDirect(outLen);137138// test#1: byte[] in + byte[] out139debugBuf.append("Test#1:\n");140ByteArrayOutputStream baos = new ByteArrayOutputStream();141byte[] testOut1 = cipher.update(in, 0, 16);142if (testOut1 != null) baos.write(testOut1, 0, testOut1.length);143testOut1 = cipher.doFinal(in, 16, in.length-16);144if (testOut1 != null) baos.write(testOut1, 0, testOut1.length);145testOut1 = baos.toByteArray();146match(testOut1, answer);147148// test#2: Non-direct Buffer in + non-direct Buffer out149debugBuf.append("Test#2:\n");150debugBuf.append("inputBuf: " + inBuf + "\n");151debugBuf.append("outputBuf: " + outBuf + "\n");152cipher.update(inBuf, outBuf);153cipher.doFinal(inBuf, outBuf);154match(outBuf, answer);155156// test#3: Direct Buffer in + direc Buffer out157debugBuf.append("Test#3:\n");158debugBuf.append("(pre) inputBuf: " + inDirectBuf + "\n");159debugBuf.append("(pre) outputBuf: " + outDirectBuf + "\n");160cipher.update(inDirectBuf, outDirectBuf);161cipher.doFinal(inDirectBuf, outDirectBuf);162163debugBuf.append("(post) inputBuf: " + inDirectBuf + "\n");164debugBuf.append("(post) outputBuf: " + outDirectBuf + "\n");165match(outDirectBuf, answer);166167// test#4: Direct Buffer in + non-direct Buffer out168debugBuf.append("Test#4:\n");169inDirectBuf.position(0);170outBuf.position(0);171debugBuf.append("inputBuf: " + inDirectBuf + "\n");172debugBuf.append("outputBuf: " + outBuf + "\n");173cipher.update(inDirectBuf, outBuf);174cipher.doFinal(inDirectBuf, outBuf);175match(outBuf, answer);176177// test#5: Non-direct Buffer in + direct Buffer out178debugBuf.append("Test#5:\n");179inBuf.position(0);180outDirectBuf.position(0);181182debugBuf.append("(pre) inputBuf: " + inBuf + "\n");183debugBuf.append("(pre) outputBuf: " + outDirectBuf + "\n");184185cipher.update(inBuf, outDirectBuf);186cipher.doFinal(inBuf, outDirectBuf);187188debugBuf.append("(post) inputBuf: " + inBuf + "\n");189debugBuf.append("(post) outputBuf: " + outDirectBuf + "\n");190match(outDirectBuf, answer);191192// test#6: Streams193debugBuf.append("Test#6: Streaming\n");194outBuf.position(0);195InputStream stream =196new CipherInputStream(new ByteArrayInputStream(in), cipher);197byte[] data = new byte[1024];198int bytesRead = 0;199try {200while (bytesRead >= 0) {201bytesRead = stream.read(data);202if (bytesRead == -1)203break;204debugBuf.append("bytesRead: " + bytesRead);205debugBuf.append("\toutBuf.position(): " + outBuf.position() +206"\n");207outBuf.put(data, 0 , bytesRead);208}209} catch (Exception ex) {210debugBuf.append("Caught Exception during stream reading\n");211throw ex;212}213match(outBuf, answer);214215debugBuf = null;216}217218private static void match(byte[] b1, byte[] b2) throws Exception {219if (b1.length != b2.length) {220debugBuf.append("got len : " + b1.length + "\n");221debugBuf.append("expect len: " + b2.length + "\n");222throw new Exception("mismatch - different length!\n");223} else {224for (int i = 0; i < b1.length; i++) {225if (b1[i] != b2[i]) {226debugBuf.append("got : " + toString(b1) + "\n");227debugBuf.append("expect: " + toString(b2) + "\n");228throw new Exception("mismatch");229}230}231}232}233234private static void match(ByteBuffer bb, byte[] answer) throws Exception {235byte[] bbTemp = new byte[bb.position()];236bb.position(0);237bb.get(bbTemp, 0, bbTemp.length);238match(bbTemp, answer);239}240241public static void main(String[] args) throws Exception {242main(new TestSymmCiphersNoPad(), args);243}244}245246247