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.Arrays;
  30 import java.util.Random;
  31 import javax.crypto.BadPaddingException;
  32 import javax.crypto.Cipher;
  33 import javax.crypto.IllegalBlockSizeException;
  34 import javax.crypto.KeyGenerator;
  35 import javax.crypto.NoSuchPaddingException;
  36 import javax.crypto.SecretKey;
  37 import javax.crypto.ShortBufferException;
  38 import javax.crypto.spec.IvParameterSpec;
  39 
  40 
  41 /**
  42  * @test
  43  * @bug 8043836
  44  * @summary Test AES ciphers with 4 different modes with NoPadding. Check if
  45  *          data before encryption and after decryption is the same.
  46  * @key randomness
  47  */
  48 
  49 public class CTR {
  50 
  51     private static final String ALGORITHM = "AES";
  52 
  53     private static final String PROVIDER = "SunJCE";
  54 
  55     private static final String[] MODES = {"CTR","CFB24","OFB32","GCM"};
  56 
  57     private static final String PADDING = "NoPadding";
  58 
  59 
  60     private static final int KEY_LENGTH = 128;
  61 
  62     public static void main(String argv[]) throws Exception {
  63         CTR test = new CTR();
  64         for (String mode : MODES) {
  65             test.runTest(ALGORITHM, mode, PADDING);
  66         }
  67     }
  68 
  69 
  70     public void runTest(String algo, String mo, String pad) throws Exception {
  71         Cipher ci = null;
  72         byte[] iv = null;
  73         AlgorithmParameterSpec aps = null;
  74         SecretKey key = null;
  75 
  76         try {
  77             Random rdm = new Random();
  78             byte[] plainText;
  79 
  80             ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
  81             KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
  82             kg.init(KEY_LENGTH);
  83             key = kg.generateKey();
  84 
  85             for (int i = 0; i < 15; i++) {
  86                 plainText = new byte[1600 + i + 1];
  87                 rdm.nextBytes(plainText);
  88 
  89                 if (!mo.equalsIgnoreCase("GCM")) {
  90                     ci.init(Cipher.ENCRYPT_MODE, key, aps);
  91                 } else {
  92                     ci.init(Cipher.ENCRYPT_MODE, key);
  93                 }
  94 
  95                 byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
  96                 int offset = ci.update(plainText, 0, plainText.length,
  97                         cipherText, 0);
  98 
  99                 ci.doFinal(cipherText, offset);
 100 
 101                 if (!mo.equalsIgnoreCase("ECB")) {
 102                     iv = ci.getIV();
 103                     aps = new IvParameterSpec(iv);
 104                 } else {
 105                     aps = null;
 106                 }
 107 
 108                 if (!mo.equalsIgnoreCase("GCM")) {
 109                     ci.init(Cipher.DECRYPT_MODE, key, aps);
 110                 } else {
 111                     ci.init(Cipher.DECRYPT_MODE, key, ci.getParameters());
 112                 }
 113 
 114                 byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
 115                 int len = ci.doFinal(cipherText, 0, cipherText.length,
 116                         recoveredText);
 117                 byte[] tmp = new byte[len];
 118 
 119                 for (int j = 0; j < len; j++) {
 120                     tmp[j] = recoveredText[j];
 121                 }
 122                 Arrays.toString(plainText);
 123                 if (!java.util.Arrays.equals(plainText, tmp)) {
 124                     System.out.println("Original: ");
 125                     dumpBytes(plainText);
 126                     System.out.println("Recovered: ");
 127                     dumpBytes(tmp);
 128                     throw new RuntimeException("Original text is not equal with recovered text, with mode:" + mo);
 129                 }
 130             }
 131         } catch (NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException
 132                 | InvalidKeyException | InvalidAlgorithmParameterException
 133                 | ShortBufferException | IllegalBlockSizeException
 134                 | BadPaddingException e) {
 135             System.out.println("Test failed!");
 136             throw e;
 137         }
 138     }
 139 
 140     private void dumpBytes(byte[] bytes){
 141         for (byte b : bytes){
 142             System.out.print(Integer.toHexString(b));
 143         }
 144         System.out.println();
 145     }
 146 }