1 /*
   2  * Copyright (c) 2012, 2016, 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 package org.graalvm.compiler.hotspot.replacements;
  24 
  25 import static org.graalvm.compiler.hotspot.HotSpotBackend.DECRYPT_BLOCK;
  26 import static org.graalvm.compiler.hotspot.HotSpotBackend.DECRYPT_BLOCK_WITH_ORIGINAL_KEY;
  27 import static org.graalvm.compiler.hotspot.HotSpotBackend.ENCRYPT_BLOCK;
  28 import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.VERY_SLOW_PATH_PROBABILITY;
  29 import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.probability;
  30 import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider.getArrayBaseOffset;
  31 
  32 import java.lang.reflect.Field;
  33 
  34 import org.graalvm.compiler.api.replacements.ClassSubstitution;
  35 import org.graalvm.compiler.api.replacements.MethodSubstitution;
  36 import org.graalvm.compiler.core.common.LocationIdentity;
  37 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
  38 import org.graalvm.compiler.debug.GraalError;
  39 import org.graalvm.compiler.graph.Node.ConstantNodeParameter;
  40 import org.graalvm.compiler.graph.Node.NodeIntrinsic;
  41 import org.graalvm.compiler.hotspot.nodes.ComputeObjectAddressNode;
  42 import org.graalvm.compiler.nodes.DeoptimizeNode;
  43 import org.graalvm.compiler.nodes.PiNode;
  44 import org.graalvm.compiler.nodes.extended.ForeignCallNode;
  45 import org.graalvm.compiler.nodes.extended.UnsafeLoadNode;
  46 import org.graalvm.compiler.word.Pointer;
  47 import org.graalvm.compiler.word.Word;
  48 
  49 import jdk.vm.ci.meta.DeoptimizationAction;
  50 import jdk.vm.ci.meta.DeoptimizationReason;
  51 import jdk.vm.ci.meta.JavaKind;
  52 
  53 // JaCoCo Exclude
  54 
  55 /**
  56  * Substitutions for {@code com.sun.crypto.provider.AESCrypt} methods.
  57  */
  58 @ClassSubstitution(className = "com.sun.crypto.provider.AESCrypt", optional = true)
  59 public class AESCryptSubstitutions {
  60 
  61     static final long kOffset;
  62     static final long lastKeyOffset;
  63     static final Class<?> AESCryptClass;
  64     static final int AES_BLOCK_SIZE;
  65 
  66     static {
  67         try {
  68             // Need to use the system class loader as com.sun.crypto.provider.AESCrypt
  69             // is normally loaded by the extension class loader which is not delegated
  70             // to by the JVMCI class loader.
  71             ClassLoader cl = ClassLoader.getSystemClassLoader();
  72             AESCryptClass = Class.forName("com.sun.crypto.provider.AESCrypt", true, cl);
  73             kOffset = UnsafeAccess.UNSAFE.objectFieldOffset(AESCryptClass.getDeclaredField("K"));
  74             lastKeyOffset = UnsafeAccess.UNSAFE.objectFieldOffset(AESCryptClass.getDeclaredField("lastKey"));
  75             Field aesBlockSizeField = Class.forName("com.sun.crypto.provider.AESConstants", true, cl).getDeclaredField("AES_BLOCK_SIZE");
  76             aesBlockSizeField.setAccessible(true);
  77             AES_BLOCK_SIZE = aesBlockSizeField.getInt(null);
  78         } catch (Exception ex) {
  79             throw new GraalError(ex);
  80         }
  81     }
  82 
  83     @MethodSubstitution(isStatic = false)
  84     static void encryptBlock(Object rcvr, byte[] in, int inOffset, byte[] out, int outOffset) {
  85         crypt(rcvr, in, inOffset, out, outOffset, true, false);
  86     }
  87 
  88     @MethodSubstitution(isStatic = false)
  89     static void implEncryptBlock(Object rcvr, byte[] in, int inOffset, byte[] out, int outOffset) {
  90         crypt(rcvr, in, inOffset, out, outOffset, true, false);
  91     }
  92 
  93     @MethodSubstitution(isStatic = false)
  94     static void decryptBlock(Object rcvr, byte[] in, int inOffset, byte[] out, int outOffset) {
  95         crypt(rcvr, in, inOffset, out, outOffset, false, false);
  96     }
  97 
  98     @MethodSubstitution(isStatic = false)
  99     static void implDecryptBlock(Object rcvr, byte[] in, int inOffset, byte[] out, int outOffset) {
 100         crypt(rcvr, in, inOffset, out, outOffset, false, false);
 101     }
 102 
 103     /**
 104      * Variation for platforms (e.g. SPARC) that need do key expansion in stubs due to compatibility
 105      * issues between Java key expansion and hardware crypto instructions.
 106      */
 107     @MethodSubstitution(value = "decryptBlock", isStatic = false)
 108     static void decryptBlockWithOriginalKey(Object rcvr, byte[] in, int inOffset, byte[] out, int outOffset) {
 109         crypt(rcvr, in, inOffset, out, outOffset, false, true);
 110     }
 111 
 112     /**
 113      * @see #decryptBlockWithOriginalKey(Object, byte[], int, byte[], int)
 114      */
 115     @MethodSubstitution(value = "implDecryptBlock", isStatic = false)
 116     static void implDecryptBlockWithOriginalKey(Object rcvr, byte[] in, int inOffset, byte[] out, int outOffset) {
 117         crypt(rcvr, in, inOffset, out, outOffset, false, true);
 118     }
 119 
 120     private static void crypt(Object rcvr, byte[] in, int inOffset, byte[] out, int outOffset, boolean encrypt, boolean withOriginalKey) {
 121         checkArgs(in, inOffset, out, outOffset);
 122         Object realReceiver = PiNode.piCastNonNull(rcvr, AESCryptClass);
 123         Object kObject = UnsafeLoadNode.load(realReceiver, kOffset, JavaKind.Object, LocationIdentity.any());
 124         Pointer kAddr = Word.objectToTrackedPointer(kObject).add(getArrayBaseOffset(JavaKind.Int));
 125         Word inAddr = Word.unsigned(ComputeObjectAddressNode.get(in, getArrayBaseOffset(JavaKind.Byte) + inOffset));
 126         Word outAddr = Word.unsigned(ComputeObjectAddressNode.get(out, getArrayBaseOffset(JavaKind.Byte) + outOffset));
 127         if (encrypt) {
 128             encryptBlockStub(ENCRYPT_BLOCK, inAddr, outAddr, kAddr);
 129         } else {
 130             if (withOriginalKey) {
 131                 Object lastKeyObject = UnsafeLoadNode.load(realReceiver, lastKeyOffset, JavaKind.Object, LocationIdentity.any());
 132                 Pointer lastKeyAddr = Word.objectToTrackedPointer(lastKeyObject).add(getArrayBaseOffset(JavaKind.Byte));
 133                 decryptBlockWithOriginalKeyStub(DECRYPT_BLOCK_WITH_ORIGINAL_KEY, inAddr, outAddr, kAddr, lastKeyAddr);
 134             } else {
 135                 decryptBlockStub(DECRYPT_BLOCK, inAddr, outAddr, kAddr);
 136             }
 137         }
 138     }
 139 
 140     /**
 141      * Perform null and array bounds checks for arguments to a cipher operation.
 142      */
 143     static void checkArgs(byte[] in, int inOffset, byte[] out, int outOffset) {
 144         if (probability(VERY_SLOW_PATH_PROBABILITY, inOffset < 0 || in.length - AES_BLOCK_SIZE < inOffset || outOffset < 0 || out.length - AES_BLOCK_SIZE < outOffset)) {
 145             DeoptimizeNode.deopt(DeoptimizationAction.None, DeoptimizationReason.RuntimeConstraint);
 146         }
 147     }
 148 
 149     @NodeIntrinsic(ForeignCallNode.class)
 150     public static native void encryptBlockStub(@ConstantNodeParameter ForeignCallDescriptor descriptor, Word in, Word out, Pointer key);
 151 
 152     @NodeIntrinsic(ForeignCallNode.class)
 153     public static native void decryptBlockStub(@ConstantNodeParameter ForeignCallDescriptor descriptor, Word in, Word out, Pointer key);
 154 
 155     @NodeIntrinsic(ForeignCallNode.class)
 156     public static native void decryptBlockWithOriginalKeyStub(@ConstantNodeParameter ForeignCallDescriptor descriptor, Word in, Word out, Pointer key, Pointer originalKey);
 157 }