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.security.InvalidAlgorithmParameterException;
  25 import java.security.InvalidKeyException;
  26 import java.security.NoSuchAlgorithmException;
  27 import java.security.NoSuchProviderException;
  28 import java.security.spec.AlgorithmParameterSpec;
  29 import java.util.Random;
  30 import javax.crypto.BadPaddingException;
  31 import javax.crypto.Cipher;
  32 import javax.crypto.IllegalBlockSizeException;
  33 import javax.crypto.KeyGenerator;
  34 import javax.crypto.NoSuchPaddingException;
  35 import javax.crypto.SecretKey;
  36 import javax.crypto.ShortBufferException;
  37 import javax.crypto.spec.IvParameterSpec;
  38 
  39 /**
  40  * @test
  41  * @bug 8043836
  42  * @summary Test AES ciphers with different modes and padding schemes (ECB mode
  43  *          doesn't use IV). The test tries 3 different read methods of
  44  *          CipherInputStream.
  45  */
  46 public class Padding {
  47 
  48     private static final String ALGORITHM = "AES";
  49     private static final String PROVIDER = "SunJCE";
  50     private static final String[] MODES = { "ECb", "CbC", "PCBC", "OFB",
  51         "OFB150", "cFB", "CFB7", "cFB8", "cFB16", "cFB24", "cFB32",
  52         "Cfb40", "cfB48", "cfB56", "cfB64", "cfB72", "cfB80", "cfB88",
  53         "cfB96", "cfb104", "cfB112", "cfB120", "OFB8", "OFB16", "OFB24",
  54         "OFB32", "OFB40", "OFB48", "OFB56", "OFB64", "OFB72", "OFB80",
  55         "OFB88", "OFB96", "OFB104", "OFB112", "OFB120", "GCM" };
  56     private static final String PADDING = "PKCS5Padding";
  57     private static final int KEY_LENGTH = 128;
  58 
  59     public static void main(String argv[]) throws Exception {
  60         Padding test = new Padding();
  61         for (String mode : MODES) {
  62             test.runTest(ALGORITHM, mode, PADDING);
  63         }
  64     }
  65 
  66     public void runTest(String algo, String mo, String pad) throws Exception {
  67         Cipher ci = null;
  68         byte[] iv = null;
  69         AlgorithmParameterSpec aps = null;
  70         SecretKey key = null;
  71         try {
  72             Random rdm = new Random();
  73             byte[] plainText;
  74 
  75             ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
  76             KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
  77             kg.init(KEY_LENGTH);
  78             key = kg.generateKey();
  79 
  80             for (int i = 0; i < 15; i++) {
  81                 plainText = new byte[1600 + i + 1];
  82                 rdm.nextBytes(plainText);
  83 
  84                 if (!mo.equalsIgnoreCase("GCM")) {
  85                     ci.init(Cipher.ENCRYPT_MODE, key, aps);
  86                 } else {
  87                     ci.init(Cipher.ENCRYPT_MODE, key);
  88                 }
  89 
  90                 byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
  91                 int offset = ci.update(plainText, 0, plainText.length,
  92                         cipherText, 0);
  93                 ci.doFinal(cipherText, offset);
  94 
  95                 if (!mo.equalsIgnoreCase("ECB")) {
  96                     iv = ci.getIV();
  97                     aps = new IvParameterSpec(iv);
  98                 } else {
  99                     aps = null;
 100                 }
 101 
 102                 if (!mo.equalsIgnoreCase("GCM")) {
 103                     ci.init(Cipher.DECRYPT_MODE, key, aps);
 104                 } else {
 105                     ci.init(Cipher.DECRYPT_MODE, key, ci.getParameters());
 106                 }
 107 
 108                 byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
 109                 int len = ci.doFinal(cipherText, 0, cipherText.length,
 110                         recoveredText);
 111                 byte[] tmp = new byte[len];
 112 
 113                 for (int j = 0; j < len; j++) {
 114                     tmp[j] = recoveredText[j];
 115                 }
 116 
 117                 if (!java.util.Arrays.equals(plainText, tmp)) {
 118                     System.out.println("Original: ");
 119                     dumpBytes(plainText);
 120                     System.out.println("Recovered: ");
 121                     dumpBytes(tmp);
 122                     throw new RuntimeException(
 123                             "Original text is not equal with recovered text, with mode:"
 124                                     + mo);
 125                 }
 126             }
 127         } catch (NoSuchAlgorithmException e) {
 128             //CFB7 and OFB150 are for negative testing
 129             if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("OFB150")) {
 130                 System.out
 131                 .println("Unexpected NoSuchAlgorithmException with mode: "
 132                         + mo);
 133                 throw new RuntimeException("Test failed!");
 134             }
 135         } catch ( NoSuchProviderException | NoSuchPaddingException
 136                 | InvalidKeyException | InvalidAlgorithmParameterException
 137                 | ShortBufferException | IllegalBlockSizeException
 138                 | BadPaddingException e) {
 139             System.out.println("Test failed!");
 140             throw e;
 141         }
 142     }
 143 
 144     private void dumpBytes(byte[] bytes) {
 145         for (byte b : bytes) {
 146             System.out.print(Integer.toHexString(b));
 147         }
 148 
 149         System.out.println();
 150     }
 151 }