1 /*
   2  * Copyright (c) 2019, 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 8209951
  27  * @summary SIGBUS in com.sun.crypto.provider.CipherBlockChaining
  28  * @run main/othervm/timeout=300 -Xbatch
  29  *      compiler.codegen.aes.TestCipherBlockChainingEncrypt
  30  */
  31 
  32 package compiler.codegen.aes;
  33 
  34 import java.io.PrintStream;
  35 import java.security.*;
  36 import java.util.Random;
  37 import java.lang.reflect.Method;
  38 import javax.crypto.Cipher;
  39 import javax.crypto.SecretKey;
  40 import javax.crypto.SecretKeyFactory;
  41 import javax.crypto.spec.PBEKeySpec;
  42 
  43 public class TestCipherBlockChainingEncrypt {
  44     private static String algorithm = "PBEWithHmacSHA1AndAES_256";
  45     private static final String PBEPASS = "Hush, it's supposed to be a secret!";
  46 
  47     private static final int INPUT_LENGTH = 800;
  48     private static final int[] OFFSETS = {0};
  49     private static final int NUM_PAD_BYTES = 8;
  50     private static final int PBKDF2_ADD_PAD_BYTES = 8;
  51 
  52     private static SecretKey key;
  53     private static Cipher ci;
  54 
  55     public static void main(String[] args) throws Exception {
  56      for(int i=0; i<5_000; i++) {
  57         if (!(new TestCipherBlockChainingEncrypt().test(args))) {
  58             throw new RuntimeException("TestCipherBlockChainingEncrypt test failed");
  59        }
  60      }
  61    }
  62 
  63     public boolean test(String[] args) throws Exception {
  64         boolean result = true;
  65 
  66         Provider p = Security.getProvider("SunJCE");
  67         ci = Cipher.getInstance(algorithm, p);
  68         key = SecretKeyFactory.getInstance(algorithm, p).generateSecret(
  69                         new PBEKeySpec(PBEPASS.toCharArray()));
  70 
  71         // generate input data
  72         byte[] inputText = new byte[INPUT_LENGTH + NUM_PAD_BYTES
  73                 + PBKDF2_ADD_PAD_BYTES];
  74         new Random().nextBytes(inputText);
  75 
  76         try {
  77             // Encrypt
  78             execute(Cipher.ENCRYPT_MODE,
  79                     inputText,
  80                     0,
  81                     INPUT_LENGTH);
  82 
  83             // PBKDF2 required 16 byte padding
  84             int padLength = NUM_PAD_BYTES + PBKDF2_ADD_PAD_BYTES;
  85 
  86             // Decrypt
  87             // Note: inputText is implicitly padded by the above encrypt
  88             // operation so decrypt operation can safely proceed
  89             execute(Cipher.DECRYPT_MODE,
  90                     inputText,
  91                     0,
  92                     INPUT_LENGTH + padLength);
  93 
  94         } catch (Exception ex) {
  95             ex.printStackTrace(System.out);
  96             result = false;
  97         }
  98         return result;
  99     }
 100 
 101     private void execute(int edMode, byte[] inputText, int offset, int len) {
 102         try {
 103             // init Cipher
 104             if (Cipher.ENCRYPT_MODE == edMode) {
 105                 ci.init(Cipher.ENCRYPT_MODE, this.key);
 106             } else {
 107                 ci.init(Cipher.DECRYPT_MODE, this.key, ci.getParameters());
 108             }
 109 
 110             // First, generate the cipherText at an allocated buffer
 111             byte[] outputText = ci.doFinal(inputText, offset, len);
 112 
 113             // Second, generate cipherText again at the same buffer of plainText
 114             int myoff = offset / 2;
 115             int off = ci.update(inputText, offset, len, inputText, myoff);
 116             ci.doFinal(inputText, myoff + off);
 117 
 118             // Compare to see whether the two results are the same or not
 119             boolean e = equalsBlock(inputText, myoff, outputText, 0,
 120                     outputText.length);
 121         } catch (Exception ex) {
 122                 System.out.println("Got unexpected exception for " + algorithm);
 123                 ex.printStackTrace(System.out);
 124         }
 125     }
 126 
 127     private boolean equalsBlock(byte[] b1, int off1,
 128             byte[] b2, int off2, int len) {
 129         for (int i = off1, j = off2, k = 0; k < len; i++, j++, k++) {
 130             if (b1[i] != b2[j]) {
 131                 return false;
 132             }
 133         }
 134         return true;
 135     }
 136 }