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