1 /*
   2  * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 4898461 6604496
  27  * @summary basic test for symmetric ciphers with padding
  28  * @author Valerie Peng
  29  * @library ..
  30  * @key randomness
  31  */
  32 import java.io.*;
  33 import java.nio.*;
  34 import java.util.*;
  35 
  36 import java.security.*;
  37 import java.security.spec.AlgorithmParameterSpec;
  38 
  39 import javax.crypto.*;
  40 import javax.crypto.spec.IvParameterSpec;
  41 
  42 public class TestSymmCiphers extends PKCS11Test {
  43 
  44     private static class CI { // class for holding Cipher Information
  45 
  46         String transformation;
  47         String keyAlgo;
  48         int dataSize;
  49 
  50         CI(String transformation, String keyAlgo, int dataSize) {
  51             this.transformation = transformation;
  52             this.keyAlgo = keyAlgo;
  53             this.dataSize = dataSize;
  54         }
  55     }
  56     private static final CI[] TEST_LIST = {
  57         new CI("ARCFOUR", "ARCFOUR", 400),
  58         new CI("RC4", "RC4", 401),
  59         new CI("DES/CBC/NoPadding", "DES", 400),
  60         new CI("DESede/CBC/NoPadding", "DESede", 160),
  61         new CI("AES/CBC/NoPadding", "AES", 4800),
  62         new CI("Blowfish/CBC/NoPadding", "Blowfish", 24),
  63         new CI("DES/cbc/PKCS5Padding", "DES", 6401),
  64         new CI("DESede/CBC/PKCS5Padding", "DESede", 402),
  65         new CI("AES/CBC/PKCS5Padding", "AES", 30),
  66         new CI("Blowfish/CBC/PKCS5Padding", "Blowfish", 19),
  67         new CI("DES/ECB/NoPadding", "DES", 400),
  68         new CI("DESede/ECB/NoPadding", "DESede", 160),
  69         new CI("AES/ECB/NoPadding", "AES", 4800),
  70         new CI("DES/ECB/PKCS5Padding", "DES", 32),
  71         new CI("DES/ECB/PKCS5Padding", "DES", 6400),
  72         new CI("DESede/ECB/PKCS5Padding", "DESede", 400),
  73         new CI("AES/ECB/PKCS5Padding", "AES", 64),
  74 
  75         new CI("DES", "DES", 6400),
  76         new CI("DESede", "DESede", 408),
  77         new CI("AES", "AES", 128),
  78 
  79         new CI("AES/CTR/NoPadding", "AES", 3200)
  80 
  81     };
  82     private static StringBuffer debugBuf = new StringBuffer();
  83 
  84     public void main(Provider p) throws Exception {
  85         // NSS reports CKR_DEVICE_ERROR when the data passed to
  86         // its EncryptUpdate/DecryptUpdate is not multiple of blocks
  87         int firstBlkSize = 16;
  88         boolean status = true;
  89         Random random = new Random();
  90         try {
  91             for (int i = 0; i < TEST_LIST.length; i++) {
  92                 CI currTest = TEST_LIST[i];
  93                 System.out.println("===" + currTest.transformation + "===");
  94                 try {
  95                     KeyGenerator kg =
  96                             KeyGenerator.getInstance(currTest.keyAlgo, p);
  97                     SecretKey key = kg.generateKey();
  98                     Cipher c1 = Cipher.getInstance(currTest.transformation, p);
  99                     Cipher c2 = Cipher.getInstance(currTest.transformation,
 100                             "SunJCE");
 101 
 102                     byte[] plainTxt = new byte[currTest.dataSize];
 103                     random.nextBytes(plainTxt);
 104                     System.out.println("Testing inLen = " + plainTxt.length);
 105 
 106                     c2.init(Cipher.ENCRYPT_MODE, key);
 107                     AlgorithmParameters params = c2.getParameters();
 108                     byte[] answer = c2.doFinal(plainTxt);
 109                     System.out.println("Encryption tests: START");
 110                     test(c1, Cipher.ENCRYPT_MODE, key, params, firstBlkSize,
 111                             plainTxt, answer);
 112                     System.out.println("Encryption tests: DONE");
 113                     c2.init(Cipher.DECRYPT_MODE, key, params);
 114                     byte[] answer2 = c2.doFinal(answer);
 115                     System.out.println("Decryption tests: START");
 116                     test(c1, Cipher.DECRYPT_MODE, key, params, firstBlkSize,
 117                             answer, answer2);
 118                     System.out.println("Decryption tests: DONE");
 119                 } catch (NoSuchAlgorithmException nsae) {
 120                     System.out.println("Skipping unsupported algorithm: " +
 121                             nsae);
 122                 }
 123             }
 124         } catch (Exception ex) {
 125             // print out debug info when exception is encountered
 126             if (debugBuf != null) {
 127                 System.out.println(debugBuf.toString());
 128                 debugBuf = new StringBuffer();
 129             }
 130             throw ex;
 131         }
 132     }
 133 
 134     private static void test(Cipher cipher, int mode, SecretKey key,
 135             AlgorithmParameters params, int firstBlkSize,
 136             byte[] in, byte[] answer) throws Exception {
 137         // test setup
 138         long startTime, endTime;
 139         cipher.init(mode, key, params);
 140         int outLen = cipher.getOutputSize(in.length);
 141         //debugOut("Estimated output size = " + outLen + "\n");
 142 
 143         // test data preparation
 144         ByteBuffer inBuf = ByteBuffer.allocate(in.length);
 145         inBuf.put(in);
 146         inBuf.position(0);
 147         ByteBuffer inDirectBuf = ByteBuffer.allocateDirect(in.length);
 148         inDirectBuf.put(in);
 149         inDirectBuf.position(0);
 150         ByteBuffer outBuf = ByteBuffer.allocate(outLen);
 151         ByteBuffer outDirectBuf = ByteBuffer.allocateDirect(outLen);
 152 
 153         // test#1: byte[] in + byte[] out
 154         //debugOut("Test#1:\n");
 155 
 156         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 157 
 158         startTime = System.nanoTime();
 159         byte[] temp = cipher.update(in, 0, firstBlkSize);
 160         if (temp != null && temp.length > 0) {
 161             baos.write(temp, 0, temp.length);
 162         }
 163         temp = cipher.doFinal(in, firstBlkSize, in.length - firstBlkSize);
 164         if (temp != null && temp.length > 0) {
 165             baos.write(temp, 0, temp.length);
 166         }
 167         byte[] testOut1 = baos.toByteArray();
 168         endTime = System.nanoTime();
 169         perfOut("stream InBuf + stream OutBuf: " +
 170                 (endTime - startTime));
 171         match(testOut1, answer);
 172 
 173         // test#2: Non-direct Buffer in + non-direct Buffer out
 174         //debugOut("Test#2:\n");
 175         //debugOut("inputBuf: " + inBuf + "\n");
 176         //debugOut("outputBuf: " + outBuf + "\n");
 177 
 178         startTime = System.nanoTime();
 179         cipher.update(inBuf, outBuf);
 180         cipher.doFinal(inBuf, outBuf);
 181         endTime = System.nanoTime();
 182         perfOut("non-direct InBuf + non-direct OutBuf: " +
 183                 (endTime - startTime));
 184         match(outBuf, answer);
 185 
 186         // test#3: Direct Buffer in + direc Buffer out
 187         //debugOut("Test#3:\n");
 188         //debugOut("(pre) inputBuf: " + inDirectBuf + "\n");
 189         //debugOut("(pre) outputBuf: " + outDirectBuf + "\n");
 190 
 191         startTime = System.nanoTime();
 192         cipher.update(inDirectBuf, outDirectBuf);
 193         cipher.doFinal(inDirectBuf, outDirectBuf);
 194         endTime = System.nanoTime();
 195         perfOut("direct InBuf + direct OutBuf: " +
 196                 (endTime - startTime));
 197 
 198         //debugOut("(post) inputBuf: " + inDirectBuf + "\n");
 199         //debugOut("(post) outputBuf: " + outDirectBuf + "\n");
 200         match(outDirectBuf, answer);
 201 
 202         // test#4: Direct Buffer in + non-direct Buffer out
 203         //debugOut("Test#4:\n");
 204         inDirectBuf.position(0);
 205         outBuf.position(0);
 206         //debugOut("inputBuf: " + inDirectBuf + "\n");
 207         //debugOut("outputBuf: " + outBuf + "\n");
 208 
 209         startTime = System.nanoTime();
 210         cipher.update(inDirectBuf, outBuf);
 211         cipher.doFinal(inDirectBuf, outBuf);
 212         endTime = System.nanoTime();
 213         perfOut("direct InBuf + non-direct OutBuf: " +
 214                 (endTime - startTime));
 215         match(outBuf, answer);
 216 
 217         // test#5: Non-direct Buffer in + direct Buffer out
 218         //debugOut("Test#5:\n");
 219         inBuf.position(0);
 220         outDirectBuf.position(0);
 221 
 222         //debugOut("(pre) inputBuf: " + inBuf + "\n");
 223         //debugOut("(pre) outputBuf: " + outDirectBuf + "\n");
 224 
 225         startTime = System.nanoTime();
 226         cipher.update(inBuf, outDirectBuf);
 227         cipher.doFinal(inBuf, outDirectBuf);
 228         endTime = System.nanoTime();
 229         perfOut("non-direct InBuf + direct OutBuf: " +
 230                 (endTime - startTime));
 231 
 232         //debugOut("(post) inputBuf: " + inBuf + "\n");
 233         //debugOut("(post) outputBuf: " + outDirectBuf + "\n");
 234         match(outDirectBuf, answer);
 235 
 236         debugBuf = null;
 237     }
 238 
 239     private static void perfOut(String msg) {
 240         if (debugBuf != null) {
 241             debugBuf.append("PERF>" + msg);
 242         }
 243     }
 244 
 245     private static void debugOut(String msg) {
 246         if (debugBuf != null) {
 247             debugBuf.append(msg);
 248         }
 249     }
 250 
 251     private static void match(byte[] b1, byte[] b2) throws Exception {
 252         if (b1.length != b2.length) {
 253             debugOut("got len   : " + b1.length + "\n");
 254             debugOut("expect len: " + b2.length + "\n");
 255             throw new Exception("mismatch - different length! got: " + b1.length + ", expect: " + b2.length + "\n");
 256         } else {
 257             for (int i = 0; i < b1.length; i++) {
 258                 if (b1[i] != b2[i]) {
 259                     debugOut("got   : " + toString(b1) + "\n");
 260                     debugOut("expect: " + toString(b2) + "\n");
 261                     throw new Exception("mismatch");
 262                 }
 263             }
 264         }
 265     }
 266 
 267     private static void match(ByteBuffer bb, byte[] answer) throws Exception {
 268         byte[] bbTemp = new byte[bb.position()];
 269         bb.position(0);
 270         bb.get(bbTemp, 0, bbTemp.length);
 271         match(bbTemp, answer);
 272     }
 273 
 274     public static void main(String[] args) throws Exception {
 275         main(new TestSymmCiphers());
 276     }
 277 }