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