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.GraalHotSpotVMConfig.INJECTED_METAACCESS; 28 import static org.graalvm.compiler.hotspot.GraalHotSpotVMConfigBase.INJECTED_INTRINSIC_CONTEXT; 29 import static org.graalvm.compiler.hotspot.HotSpotBackend.DECRYPT; 30 import static org.graalvm.compiler.hotspot.HotSpotBackend.DECRYPT_WITH_ORIGINAL_KEY; 31 import static org.graalvm.compiler.hotspot.HotSpotBackend.ENCRYPT; 32 import static org.graalvm.compiler.nodes.PiNode.piCastNonNull; 33 import static org.graalvm.compiler.nodes.java.InstanceOfNode.doInstanceof; 34 35 import org.graalvm.compiler.api.replacements.ClassSubstitution; 36 import org.graalvm.compiler.api.replacements.Fold; 37 import org.graalvm.compiler.api.replacements.MethodSubstitution; 38 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor; 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.extended.ForeignCallNode; 43 import org.graalvm.compiler.nodes.extended.RawLoadNode; 44 import org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext; 45 import org.graalvm.compiler.replacements.ReplacementsUtil; 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.JavaKind; 52 import jdk.vm.ci.meta.ResolvedJavaType; 53 54 // JaCoCo Exclude 55 56 /** 57 * Substitutions for {@code com.sun.crypto.provider.CipherBlockChaining} methods. 58 */ 59 @ClassSubstitution(className = "com.sun.crypto.provider.CipherBlockChaining", optional = true) 60 public class CipherBlockChainingSubstitutions { 61 62 @Fold 63 static ResolvedJavaType aesCryptType(@Fold.InjectedParameter IntrinsicContext context) { 64 return HotSpotReplacementsUtil.getType(context, "Lcom/sun/crypto/provider/AESCrypt;"); 65 } 66 67 @MethodSubstitution(isStatic = false) 68 static int encrypt(Object rcvr, byte[] in, int inOffset, int inLength, byte[] out, int outOffset) { 69 Object realReceiver = piCastNonNull(rcvr, HotSpotReplacementsUtil.methodHolderClass(INJECTED_INTRINSIC_CONTEXT)); 70 Object embeddedCipher = RawLoadNode.load(realReceiver, embeddedCipherOffset(INJECTED_INTRINSIC_CONTEXT), JavaKind.Object, LocationIdentity.any()); 71 if (doInstanceof(aesCryptType(INJECTED_INTRINSIC_CONTEXT), embeddedCipher)) { 72 Object aesCipher = piCastNonNull(embeddedCipher, aesCryptType(INJECTED_INTRINSIC_CONTEXT)); 73 crypt(realReceiver, in, inOffset, inLength, out, outOffset, aesCipher, true, false); 74 return inLength; 75 } else { 76 return encrypt(realReceiver, in, inOffset, inLength, out, outOffset); 77 } 78 } 79 80 @Fold 81 static long embeddedCipherOffset(@Fold.InjectedParameter IntrinsicContext context) { 82 return HotSpotReplacementsUtil.getFieldOffset(HotSpotReplacementsUtil.getType(context, "Lcom/sun/crypto/provider/FeedbackCipher;"), "embeddedCipher"); 83 } 84 85 @Fold 86 static long rOffset(@Fold.InjectedParameter IntrinsicContext intrinsicContext) { 87 return HotSpotReplacementsUtil.getFieldOffset(HotSpotReplacementsUtil.methodHolderClass(intrinsicContext), "r"); 88 } 89 90 @MethodSubstitution(isStatic = false, value = "implEncrypt") 91 static int implEncrypt(Object rcvr, byte[] in, int inOffset, int inLength, byte[] out, int outOffset) { 92 Object realReceiver = piCastNonNull(rcvr, HotSpotReplacementsUtil.methodHolderClass(INJECTED_INTRINSIC_CONTEXT)); 93 Object embeddedCipher = RawLoadNode.load(realReceiver, embeddedCipherOffset(INJECTED_INTRINSIC_CONTEXT), JavaKind.Object, LocationIdentity.any()); 94 if (doInstanceof(aesCryptType(INJECTED_INTRINSIC_CONTEXT), embeddedCipher)) { 95 Object aesCipher = piCastNonNull(embeddedCipher, aesCryptType(INJECTED_INTRINSIC_CONTEXT)); 96 crypt(realReceiver, in, inOffset, inLength, out, outOffset, aesCipher, true, false); 97 return inLength; 98 } else { 99 return implEncrypt(realReceiver, in, inOffset, inLength, out, outOffset); 100 } 101 } 102 103 @MethodSubstitution(isStatic = false) 104 static int decrypt(Object rcvr, byte[] in, int inOffset, int inLength, byte[] out, int outOffset) { 105 Object realReceiver = piCastNonNull(rcvr, HotSpotReplacementsUtil.methodHolderClass(INJECTED_INTRINSIC_CONTEXT)); 106 Object embeddedCipher = RawLoadNode.load(realReceiver, embeddedCipherOffset(INJECTED_INTRINSIC_CONTEXT), JavaKind.Object, LocationIdentity.any()); 107 if (in != out && doInstanceof(aesCryptType(INJECTED_INTRINSIC_CONTEXT), embeddedCipher)) { 108 Object aesCipher = piCastNonNull(embeddedCipher, aesCryptType(INJECTED_INTRINSIC_CONTEXT)); 109 crypt(realReceiver, in, inOffset, inLength, out, outOffset, aesCipher, false, false); 110 return inLength; 111 } else { 112 return decrypt(realReceiver, in, inOffset, inLength, out, outOffset); 113 } 114 } 115 116 @MethodSubstitution(isStatic = false) 117 static int implDecrypt(Object rcvr, byte[] in, int inOffset, int inLength, byte[] out, int outOffset) { 118 Object realReceiver = piCastNonNull(rcvr, HotSpotReplacementsUtil.methodHolderClass(INJECTED_INTRINSIC_CONTEXT)); 119 Object embeddedCipher = RawLoadNode.load(realReceiver, embeddedCipherOffset(INJECTED_INTRINSIC_CONTEXT), JavaKind.Object, LocationIdentity.any()); 120 if (in != out && doInstanceof(aesCryptType(INJECTED_INTRINSIC_CONTEXT), embeddedCipher)) { 121 Object aesCipher = piCastNonNull(embeddedCipher, aesCryptType(INJECTED_INTRINSIC_CONTEXT)); 122 crypt(realReceiver, in, inOffset, inLength, out, outOffset, aesCipher, false, false); 123 return inLength; 124 } else { 125 return implDecrypt(realReceiver, in, inOffset, inLength, out, outOffset); 126 } 127 } 128 129 /** 130 * Variation for platforms (e.g. SPARC) that need do key expansion in stubs due to compatibility 131 * issues between Java key expansion and hardware crypto instructions. 132 */ 133 @MethodSubstitution(isStatic = false, value = "decrypt") 134 static int decryptWithOriginalKey(Object rcvr, byte[] in, int inOffset, int inLength, byte[] out, int outOffset) { 135 Object realReceiver = piCastNonNull(rcvr, HotSpotReplacementsUtil.methodHolderClass(INJECTED_INTRINSIC_CONTEXT)); 136 Object embeddedCipher = RawLoadNode.load(realReceiver, embeddedCipherOffset(INJECTED_INTRINSIC_CONTEXT), JavaKind.Object, LocationIdentity.any()); 137 if (in != out && doInstanceof(aesCryptType(INJECTED_INTRINSIC_CONTEXT), embeddedCipher)) { 138 Object aesCipher = piCastNonNull(embeddedCipher, aesCryptType(INJECTED_INTRINSIC_CONTEXT)); 139 crypt(realReceiver, in, inOffset, inLength, out, outOffset, aesCipher, false, true); 140 return inLength; 141 } else { 142 return decryptWithOriginalKey(realReceiver, in, inOffset, inLength, out, outOffset); 143 } 144 } 145 146 /** 147 * @see #decryptWithOriginalKey(Object, byte[], int, int, byte[], int) 148 */ 149 @MethodSubstitution(isStatic = false, value = "implDecrypt") 150 static int implDecryptWithOriginalKey(Object rcvr, byte[] in, int inOffset, int inLength, byte[] out, int outOffset) { 151 Object realReceiver = piCastNonNull(rcvr, HotSpotReplacementsUtil.methodHolderClass(INJECTED_INTRINSIC_CONTEXT)); 152 Object embeddedCipher = RawLoadNode.load(realReceiver, embeddedCipherOffset(INJECTED_INTRINSIC_CONTEXT), JavaKind.Object, LocationIdentity.any()); 153 if (in != out && doInstanceof(aesCryptType(INJECTED_INTRINSIC_CONTEXT), embeddedCipher)) { 154 Object aesCipher = piCastNonNull(embeddedCipher, aesCryptType(INJECTED_INTRINSIC_CONTEXT)); 155 crypt(realReceiver, in, inOffset, inLength, out, outOffset, aesCipher, false, true); 156 return inLength; 157 } else { 158 return implDecryptWithOriginalKey(realReceiver, in, inOffset, inLength, out, outOffset); 159 } 160 } 161 162 private static void crypt(Object rcvr, byte[] in, int inOffset, int inLength, byte[] out, int outOffset, Object embeddedCipher, boolean encrypt, boolean withOriginalKey) { 163 AESCryptSubstitutions.checkArgs(in, inOffset, out, outOffset); 164 Object realReceiver = piCastNonNull(rcvr, HotSpotReplacementsUtil.methodHolderClass(INJECTED_INTRINSIC_CONTEXT)); 165 Object aesCipher = piCastNonNull(embeddedCipher, aesCryptType(INJECTED_INTRINSIC_CONTEXT)); 166 Object kObject = RawLoadNode.load(aesCipher, AESCryptSubstitutions.kOffset(INJECTED_INTRINSIC_CONTEXT), JavaKind.Object, LocationIdentity.any()); 167 Object rObject = RawLoadNode.load(realReceiver, rOffset(INJECTED_INTRINSIC_CONTEXT), JavaKind.Object, LocationIdentity.any()); 168 Pointer kAddr = Word.objectToTrackedPointer(kObject).add(ReplacementsUtil.getArrayBaseOffset(INJECTED_METAACCESS, JavaKind.Int)); 169 Pointer rAddr = Word.objectToTrackedPointer(rObject).add(ReplacementsUtil.getArrayBaseOffset(INJECTED_METAACCESS, JavaKind.Byte)); 170 Word inAddr = WordFactory.unsigned(ComputeObjectAddressNode.get(in, ReplacementsUtil.getArrayBaseOffset(INJECTED_METAACCESS, JavaKind.Byte) + inOffset)); 171 Word outAddr = WordFactory.unsigned(ComputeObjectAddressNode.get(out, ReplacementsUtil.getArrayBaseOffset(INJECTED_METAACCESS, JavaKind.Byte) + outOffset)); 172 if (encrypt) { 173 encryptAESCryptStub(ENCRYPT, inAddr, outAddr, kAddr, rAddr, inLength); 174 } else { 175 if (withOriginalKey) { 176 Object lastKeyObject = RawLoadNode.load(aesCipher, AESCryptSubstitutions.lastKeyOffset(INJECTED_INTRINSIC_CONTEXT), JavaKind.Object, LocationIdentity.any()); 177 Pointer lastKeyAddr = Word.objectToTrackedPointer(lastKeyObject).add(ReplacementsUtil.getArrayBaseOffset(INJECTED_METAACCESS, JavaKind.Byte)); 178 decryptAESCryptWithOriginalKeyStub(DECRYPT_WITH_ORIGINAL_KEY, inAddr, outAddr, kAddr, rAddr, inLength, lastKeyAddr); 179 } else { 180 decryptAESCryptStub(DECRYPT, inAddr, outAddr, kAddr, rAddr, inLength); 181 } 182 } 183 } 184 185 @NodeIntrinsic(ForeignCallNode.class) 186 public static native void encryptAESCryptStub(@ConstantNodeParameter ForeignCallDescriptor descriptor, Word in, Word out, Pointer key, Pointer r, int inLength); 187 188 @NodeIntrinsic(ForeignCallNode.class) 189 public static native void decryptAESCryptStub(@ConstantNodeParameter ForeignCallDescriptor descriptor, Word in, Word out, Pointer key, Pointer r, int inLength); 190 191 @NodeIntrinsic(ForeignCallNode.class) 192 public static native void decryptAESCryptWithOriginalKeyStub(@ConstantNodeParameter ForeignCallDescriptor descriptor, Word in, Word out, Pointer key, Pointer r, int inLength, Pointer originalKey); 193 }