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