1 /*
   2  * Copyright (c) 2014, 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 import java.io.ByteArrayInputStream;
  25 import java.io.ByteArrayOutputStream;
  26 import java.io.IOException;
  27 import java.security.InvalidAlgorithmParameterException;
  28 import java.security.InvalidKeyException;
  29 import java.security.NoSuchAlgorithmException;
  30 import java.security.NoSuchProviderException;
  31 import java.security.spec.AlgorithmParameterSpec;
  32 import java.util.Random;
  33 import javax.crypto.Cipher;
  34 import javax.crypto.CipherInputStream;
  35 import javax.crypto.CipherOutputStream;
  36 import javax.crypto.KeyGenerator;
  37 import javax.crypto.NoSuchPaddingException;
  38 import javax.crypto.SecretKey;
  39 import javax.crypto.spec.IvParameterSpec;
  40 
  41 /**
  42  * @test
  43  * @bug 8043836
  44  * @summary Test AES ciphers with different modes and padding schemes (ECB mode
  45  *          doesn't use IV). The test tries 3 different read methods of
  46  *          CipherInputStream.
  47  */
  48 public class CICO {
  49     private static final String ALGORITHM = "aEs";
  50     private static final String[] MODES = { "PCBC", "ECb", "cbC", "cFB",
  51         "cFB24", "cFB32", "Cfb40", "CFB72", "OfB", "OfB20", "OfB48",
  52         "OfB56", "OFB64", "OFB112", "CFB112", "pCbC" };
  53     private static final String[] PADDING = { "noPadding", "pkcs5padding" };
  54     private static final String PROVIDER = "SunJCE";
  55     private static final int NREADS = 3;
  56     private static final int KEY_LENGTH = 128;
  57 
  58     private final byte[] plainText = new byte[1600000];
  59 
  60 
  61     public static void main(String argv[]) throws Exception {
  62         CICO test = new CICO();
  63         for (String mode : MODES) {
  64             for (String pad : PADDING) {
  65                 for (int m = 0; m < NREADS; m++) {
  66                     test.runTest(ALGORITHM, mode, pad, m);
  67                 }
  68             }
  69         }
  70     }
  71 
  72     public void runTest(String algo, String mo, String pad, int whichRead) throws Exception {
  73         Cipher ci1 = null;
  74         Cipher ci2 = null;
  75         byte[] iv = null;
  76         AlgorithmParameterSpec aps = null;
  77         SecretKey key = null;
  78 
  79         try {
  80             // Do initialization
  81             Random rdm = new Random();
  82             rdm.nextBytes(plainText);
  83             KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
  84             if (!kg.getAlgorithm().equals(algo)) {
  85                 throw new RuntimeException("Unexpected algorithm <"
  86                         + kg.getAlgorithm() + ">, expected value is <" + algo
  87                         + ">");
  88             }
  89 
  90             kg.init(KEY_LENGTH);
  91             key = kg.generateKey();
  92 
  93             ci1 = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
  94 
  95             if (mo.equalsIgnoreCase("ECB")) {
  96                 ci1.init(Cipher.ENCRYPT_MODE, key);
  97             } else {
  98                 ci1.init(Cipher.ENCRYPT_MODE, key, aps);
  99             }
 100 
 101             if (!mo.equalsIgnoreCase("ECB")) {
 102                 iv = ci1.getIV();
 103                 aps = new IvParameterSpec(iv);
 104             } else {
 105                 aps = null;
 106             }
 107 
 108             ci2 = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
 109             if (mo.equalsIgnoreCase("ECB")) {
 110                 ci2.init(Cipher.DECRYPT_MODE, key);
 111             } else {
 112                 ci2.init(Cipher.DECRYPT_MODE, key, aps);
 113             }
 114 
 115             ByteArrayInputStream baInput = new ByteArrayInputStream(plainText);
 116             ByteArrayOutputStream baOutput = new ByteArrayOutputStream();
 117             try (CipherInputStream ciInput = new CipherInputStream(baInput, ci1);
 118                     CipherOutputStream ciOutput = new CipherOutputStream(
 119                             baOutput, ci2)) {
 120                 // According to specification, CipherInputStream does not support the
 121                 // mark and reset methods
 122                 if (ciInput.markSupported()) {
 123                     throw new RuntimeException(
 124                             "CipherInputStream unexpectedly supports the mark and reset methods");
 125                 }
 126 
 127                 // Read from the input and write to the output using 2 types
 128                 // of buffering : byte[] and int
 129                 switch (whichRead) {
 130                 case 0:
 131                     int buffer0 = ciInput.read();
 132                     while (buffer0 != -1) {
 133                         ciOutput.write(buffer0);
 134                         buffer0 = ciInput.read();
 135                     }
 136                     break;
 137                 case 1:
 138                     byte[] buffer1 = new byte[20];
 139                     int len1 = ciInput.read(buffer1);
 140                     while (len1 != -1) {
 141                         ciOutput.write(buffer1, 0, len1);
 142                         len1 = ciInput.read(buffer1);
 143                     }
 144                     break;
 145                 case NREADS - 1:
 146                     byte[] buffer2 = new byte[ci1
 147                                               .getOutputSize(plainText.length)];
 148                     int offset2 = 0;
 149                     int len2 = 0;
 150                     while (len2 != -1) {
 151                         len2 = ciInput.read(buffer2, offset2, buffer2.length
 152                                 - offset2);
 153                         offset2 += len2;
 154                     }
 155                     ciOutput.write(buffer2, 0, buffer2.length);
 156                     break;
 157                 }
 158             }
 159 
 160             // Get the output
 161             byte[] recoveredText = new byte[baOutput.size()];
 162             recoveredText = baOutput.toByteArray();
 163             if (!java.util.Arrays.equals(plainText, recoveredText)) {
 164                 throw new RuntimeException(
 165                         "Original text is not equal with recovered text, with "
 166                                 + algo + "/" + mo + "/" + pad + "/" + whichRead);
 167             }
 168 
 169             // Compare input and output
 170 
 171         } catch (NoSuchAlgorithmException e) {
 172             //OFB20 is for negative testing
 173             if (!mo.equalsIgnoreCase("OFB20")) {
 174                 System.out.println("Unexpected NoSuchAlgorithmException with "
 175                         + algo + "/" + mo + "/" + pad + "/" + whichRead);
 176                 throw new RuntimeException("Test failed!");
 177             }
 178         } catch (IOException | NoSuchProviderException | NoSuchPaddingException
 179                 | InvalidKeyException | InvalidAlgorithmParameterException e) {
 180             System.out.println("Unexpected Exception with "
 181                     + algo + "/" + mo + "/" + pad + "/" + whichRead);
 182             System.out.println("Test failed!");
 183             throw e;
 184         }
 185     }
 186 }