# HG changeset patch # User Doug Simon # Date 1358974588 -3600 # Node ID aee6ce29fb177e7751b9052db94896f2d3882395 # Parent a77f22f2759ddc1e35203a169c39857129bd529c factored out common code in AES crypto substitutions diff -r a77f22f2759d -r aee6ce29fb17 graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/snippets/AESCryptSubstitutions.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/snippets/AESCryptSubstitutions.java Wed Jan 23 17:40:14 2013 +0100 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/snippets/AESCryptSubstitutions.java Wed Jan 23 21:56:28 2013 +0100 @@ -61,64 +61,66 @@ @MethodSubstitution(isStatic = false) static void encryptBlock(Object rcvr, byte[] in, int inOffset, byte[] out, int outOffset) { - Word kAddr = Word.fromObject(rcvr).readWord(Word.unsigned(kOffset)).add(arrayBaseOffset(Kind.Byte)); - Word inAddr = Word.unsigned(GetObjectAddressNode.get(in) + arrayBaseOffset(Kind.Byte) + inOffset); - Word outAddr = Word.unsigned(GetObjectAddressNode.get(out) + arrayBaseOffset(Kind.Byte) + outOffset); - EncryptBlockStubCall.call(inAddr, outAddr, kAddr); + crypt(rcvr, in, inOffset, out, outOffset, true); } @MethodSubstitution(isStatic = false) static void decryptBlock(Object rcvr, byte[] in, int inOffset, byte[] out, int outOffset) { - Word kAddr = Word.unsigned(GetObjectAddressNode.get(rcvr)).readWord(Word.unsigned(kOffset)).add(arrayBaseOffset(Kind.Byte)); + crypt(rcvr, in, inOffset, out, outOffset, false); + } + + private static void crypt(Object rcvr, byte[] in, int inOffset, byte[] out, int outOffset, boolean encrypt) { + Word kAddr = Word.fromObject(rcvr).readWord(Word.unsigned(kOffset)).add(arrayBaseOffset(Kind.Byte)); Word inAddr = Word.unsigned(GetObjectAddressNode.get(in) + arrayBaseOffset(Kind.Byte) + inOffset); Word outAddr = Word.unsigned(GetObjectAddressNode.get(out) + arrayBaseOffset(Kind.Byte) + outOffset); - DecryptBlockStubCall.call(inAddr, outAddr, kAddr); + if (encrypt) { + EncryptBlockStubCall.call(inAddr, outAddr, kAddr); + } else { + DecryptBlockStubCall.call(inAddr, outAddr, kAddr); + } } - public static class EncryptBlockStubCall extends FixedWithNextNode implements LIRGenLowerable { + abstract static class CryptBlockStubCall extends FixedWithNextNode implements LIRGenLowerable { @Input private final ValueNode in; @Input private final ValueNode out; @Input private final ValueNode key; - public static final Descriptor ENCRYPT_BLOCK = new Descriptor("encrypt_block", false, void.class, Word.class, Word.class, Word.class); + private final Descriptor descriptor; - public EncryptBlockStubCall(ValueNode in, ValueNode out, ValueNode key) { + public CryptBlockStubCall(ValueNode in, ValueNode out, ValueNode key, Descriptor descriptor) { super(StampFactory.forVoid()); this.in = in; this.out = out; this.key = key; + this.descriptor = descriptor; } @Override public void generate(LIRGenerator gen) { - RuntimeCallTarget stub = gen.getRuntime().lookupRuntimeCall(ENCRYPT_BLOCK); + RuntimeCallTarget stub = gen.getRuntime().lookupRuntimeCall(descriptor); gen.emitCall(stub, stub.getCallingConvention(), false, gen.operand(in), gen.operand(out), gen.operand(key)); } + } + + public static class EncryptBlockStubCall extends CryptBlockStubCall { + + public static final Descriptor ENCRYPT_BLOCK = new Descriptor("encrypt_block", false, void.class, Word.class, Word.class, Word.class); + + public EncryptBlockStubCall(ValueNode in, ValueNode out, ValueNode key) { + super(in, out, key, ENCRYPT_BLOCK); + } @NodeIntrinsic public static native void call(Word in, Word out, Word key); } - public static class DecryptBlockStubCall extends FixedWithNextNode implements LIRGenLowerable { - - @Input private final ValueNode in; - @Input private final ValueNode out; - @Input private final ValueNode key; + public static class DecryptBlockStubCall extends CryptBlockStubCall { public static final Descriptor DECRYPT_BLOCK = new Descriptor("decrypt_block", false, void.class, Word.class, Word.class, Word.class); public DecryptBlockStubCall(ValueNode in, ValueNode out, ValueNode key) { - super(StampFactory.forVoid()); - this.in = in; - this.out = out; - this.key = key; - } - - @Override - public void generate(LIRGenerator gen) { - RuntimeCallTarget stub = gen.getRuntime().lookupRuntimeCall(DECRYPT_BLOCK); - gen.emitCall(stub, stub.getCallingConvention(), false, gen.operand(in), gen.operand(out), gen.operand(key)); + super(in, out, key, DECRYPT_BLOCK); } @NodeIntrinsic diff -r a77f22f2759d -r aee6ce29fb17 graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/snippets/CipherBlockChainingSubstitutions.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/snippets/CipherBlockChainingSubstitutions.java Wed Jan 23 17:40:14 2013 +0100 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/snippets/CipherBlockChainingSubstitutions.java Wed Jan 23 21:56:28 2013 +0100 @@ -68,31 +68,36 @@ static void encrypt(Object rcvr, byte[] in, int inOffset, int inLength, byte[] out, int outOffset) { Object embeddedCipher = Word.fromObject(rcvr).readObject(Word.unsigned(embeddedCipherOffset)); if (getAESCryptClass().isInstance(embeddedCipher)) { - Word kAddr = Word.fromObject(embeddedCipher).readWord(Word.unsigned(AESCryptSubstitutions.kOffset)).add(arrayBaseOffset(Kind.Byte)); - Word rAddr = Word.unsigned(GetObjectAddressNode.get(rcvr)).readWord(Word.unsigned(rOffset)).add(arrayBaseOffset(Kind.Byte)); - Word inAddr = Word.unsigned(GetObjectAddressNode.get(in) + arrayBaseOffset(Kind.Byte) + inOffset); - Word outAddr = Word.unsigned(GetObjectAddressNode.get(out) + arrayBaseOffset(Kind.Byte) + outOffset); - EncryptAESCryptStubCall.call(inAddr, outAddr, kAddr, rAddr, inLength); + crypt(rcvr, in, inOffset, inLength, out, outOffset, embeddedCipher, true); } else { encrypt(rcvr, in, inOffset, inLength, out, outOffset); } } + private static void crypt(Object rcvr, byte[] in, int inOffset, int inLength, byte[] out, int outOffset, Object embeddedCipher, boolean encrypt) { + Word kAddr = Word.fromObject(embeddedCipher).readWord(Word.unsigned(AESCryptSubstitutions.kOffset)).add(arrayBaseOffset(Kind.Byte)); + Word rAddr = Word.unsigned(GetObjectAddressNode.get(rcvr)).readWord(Word.unsigned(rOffset)).add(arrayBaseOffset(Kind.Byte)); + Word inAddr = Word.unsigned(GetObjectAddressNode.get(in) + arrayBaseOffset(Kind.Byte) + inOffset); + Word outAddr = Word.unsigned(GetObjectAddressNode.get(out) + arrayBaseOffset(Kind.Byte) + outOffset); + if (encrypt) { + EncryptAESCryptStubCall.call(inAddr, outAddr, kAddr, rAddr, inLength); + } else { + DecryptAESCryptStubCall.call(inAddr, outAddr, kAddr, rAddr, inLength); + } + + } + @MethodSubstitution(isStatic = false) static void decrypt(Object rcvr, byte[] in, int inOffset, int inLength, byte[] out, int outOffset) { Object embeddedCipher = Word.fromObject(rcvr).readObject(Word.unsigned(embeddedCipherOffset)); if (in != out && getAESCryptClass().isInstance(embeddedCipher)) { - Word kAddr = Word.fromObject(embeddedCipher).readWord(Word.unsigned(AESCryptSubstitutions.kOffset)).add(arrayBaseOffset(Kind.Byte)); - Word rAddr = Word.unsigned(GetObjectAddressNode.get(rcvr)).readWord(Word.unsigned(rOffset)).add(arrayBaseOffset(Kind.Byte)); - Word inAddr = Word.unsigned(GetObjectAddressNode.get(in) + arrayBaseOffset(Kind.Byte) + inOffset); - Word outAddr = Word.unsigned(GetObjectAddressNode.get(out) + arrayBaseOffset(Kind.Byte) + outOffset); - DecryptAESCryptStubCall.call(inAddr, outAddr, kAddr, rAddr, inLength); + crypt(rcvr, in, inOffset, inLength, out, outOffset, embeddedCipher, false); } else { decrypt(rcvr, in, inOffset, inLength, out, outOffset); } } - public static class EncryptAESCryptStubCall extends FixedWithNextNode implements LIRGenLowerable { + abstract static class AESCryptStubCall extends FixedWithNextNode implements LIRGenLowerable { @Input private final ValueNode in; @Input private final ValueNode out; @@ -100,50 +105,43 @@ @Input private final ValueNode r; @Input private final ValueNode inLength; - public static final Descriptor ENCRYPT = new Descriptor("encrypt", false, void.class, Word.class, Word.class, Word.class, Word.class, int.class); + private final Descriptor descriptor; - public EncryptAESCryptStubCall(ValueNode in, ValueNode out, ValueNode key, ValueNode r, ValueNode inLength) { + public AESCryptStubCall(ValueNode in, ValueNode out, ValueNode key, ValueNode r, ValueNode inLength, Descriptor descriptor) { super(StampFactory.forVoid()); this.in = in; this.out = out; this.key = key; this.r = r; this.inLength = inLength; + this.descriptor = descriptor; } @Override public void generate(LIRGenerator gen) { - RuntimeCallTarget stub = gen.getRuntime().lookupRuntimeCall(ENCRYPT); + RuntimeCallTarget stub = gen.getRuntime().lookupRuntimeCall(descriptor); gen.emitCall(stub, stub.getCallingConvention(), false, gen.operand(in), gen.operand(out), gen.operand(key), gen.operand(r), gen.operand(inLength)); } + } + + public static class EncryptAESCryptStubCall extends AESCryptStubCall { + + public static final Descriptor ENCRYPT = new Descriptor("encrypt", false, void.class, Word.class, Word.class, Word.class, Word.class, int.class); + + public EncryptAESCryptStubCall(ValueNode in, ValueNode out, ValueNode key, ValueNode r, ValueNode inLength) { + super(in, out, key, r, inLength, ENCRYPT); + } @NodeIntrinsic public static native void call(Word in, Word out, Word key, Word r, int inLength); } - public static class DecryptAESCryptStubCall extends FixedWithNextNode implements LIRGenLowerable { - - @Input private final ValueNode in; - @Input private final ValueNode out; - @Input private final ValueNode key; - @Input private final ValueNode r; - @Input private final ValueNode inLength; + public static class DecryptAESCryptStubCall extends AESCryptStubCall { public static final Descriptor DECRYPT = new Descriptor("decrypt", false, void.class, Word.class, Word.class, Word.class, Word.class, int.class); public DecryptAESCryptStubCall(ValueNode in, ValueNode out, ValueNode key, ValueNode r, ValueNode inLength) { - super(StampFactory.forVoid()); - this.in = in; - this.out = out; - this.key = key; - this.r = r; - this.inLength = inLength; - } - - @Override - public void generate(LIRGenerator gen) { - RuntimeCallTarget stub = gen.getRuntime().lookupRuntimeCall(DECRYPT); - gen.emitCall(stub, stub.getCallingConvention(), false, gen.operand(in), gen.operand(out), gen.operand(key), gen.operand(r), gen.operand(inLength)); + super(in, out, key, r, inLength, DECRYPT); } @NodeIntrinsic