Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AliveDevil committed Jan 7, 2025
1 parent 63bdfaf commit 3da3113
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#if NETCOREAPP3_0_OR_GREATER
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;

Expand All @@ -12,21 +13,21 @@ partial class X86
{
public static bool IsSupported => Aes.IsSupported && Ssse3.IsSupported && Sse2.IsSupported;

private static Vector128<byte> LoadKey(ReadOnlySpan<byte> key, int offset, Vector128<byte>? shuffle_mask = null)
private static Vector128<byte> LoadKey(ReadOnlySpan<int> key, nuint offset, Vector128<byte>? shuffle_mask = null)
{
#if NET8_0_OR_GREATER
var xmmdest = Vector128.Create(key[offset..]);
var xmmdest = Vector128.LoadUnsafe(in Unsafe.As<int, byte>(ref MemoryMarshal.GetReference(key)), offset);
shuffle_mask ??= Vector128.Create(KeyShuffleMask).AsByte();
#else
var xmmdest = Vector128Polyfill.Create(key[offset..]);
var xmmdest = Vector128Polyfill.LoadUnsafe(in Unsafe.As<int, byte>(ref MemoryMarshal.GetReference(key)), offset);
shuffle_mask ??= Vector128Polyfill.Create(KeyShuffleMask).AsByte();
#endif
Ssse3.Shuffle(xmmdest, shuffle_mask.Value);
return xmmdest;

return Ssse3.Shuffle(xmmdest, shuffle_mask.Value);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void EncryptBlock(ReadOnlySpan<byte> @in, Span<byte> @out, ReadOnlySpan<byte> K)
public static void EncryptBlock(ReadOnlySpan<byte> @in, Span<byte> @out, ReadOnlySpan<int> K)
{
// ext\openjdk\hotspot\src\cpu\x86\vm\stubGenerator_x86_32.cpp:2189-2277
Vector128<byte> xmm_temp1, xmm_temp2, xmm_temp3, xmm_temp4;
Expand All @@ -39,7 +40,7 @@ public static void EncryptBlock(ReadOnlySpan<byte> @in, Span<byte> @out, ReadOnl
#endif

xmm_temp1 = LoadKey(K, 0x00, xmm_key_shuf_mask);
Sse2.Xor(xmm_result, xmm_temp1);
xmm_result = Sse2.Xor(xmm_result, xmm_temp1);

xmm_temp1 = LoadKey(K, 0x10, xmm_key_shuf_mask);
xmm_temp2 = LoadKey(K, 0x20, xmm_key_shuf_mask);
Expand All @@ -64,18 +65,16 @@ public static void EncryptBlock(ReadOnlySpan<byte> @in, Span<byte> @out, ReadOnl
xmm_temp1 = LoadKey(K, 0x90, xmm_key_shuf_mask);
xmm_temp2 = LoadKey(K, 0xa0, xmm_key_shuf_mask);

/*
* ext\openjdk\hotspot\src\cpu\x86\vm\stubGenerator_x86_32.cpp:2215,2216
*/
if (K.Length != 11)
// ext\openjdk\hotspot\src\cpu\x86\vm\stubGenerator_x86_32.cpp:2215,2216
if (K.Length != 44)
{
xmm_result = Aes.Encrypt(xmm_result, xmm_temp1);
xmm_result = Aes.Encrypt(xmm_result, xmm_temp2);

xmm_temp1 = LoadKey(K, 0xb0, xmm_key_shuf_mask);
xmm_temp2 = LoadKey(K, 0xc0, xmm_key_shuf_mask);

if (K.Length != 13)
if (K.Length != 52)
{
xmm_result = Aes.Encrypt(xmm_result, xmm_temp1);
xmm_result = Aes.Encrypt(xmm_result, xmm_temp2);
Expand All @@ -91,7 +90,7 @@ public static void EncryptBlock(ReadOnlySpan<byte> @in, Span<byte> @out, ReadOnl
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void DecryptBlock(ReadOnlySpan<byte> @in, Span<byte> @out, ReadOnlySpan<byte> K)
public static void DecryptBlock(ReadOnlySpan<byte> @in, Span<byte> @out, ReadOnlySpan<int> K)
{
// ext\openjdk\hotspot\src\cpu\x86\vm\stubGenerator_x86_32.cpp:3125-3210
Vector128<byte> xmm_temp1, xmm_temp2, xmm_temp3, xmm_temp4;
Expand Down Expand Up @@ -127,15 +126,15 @@ public static void DecryptBlock(ReadOnlySpan<byte> @in, Span<byte> @out, ReadOnl
xmm_temp2 = LoadKey(K, 0xa0, xmm_key_shuf_mask);
xmm_temp3 = LoadKey(K, 0x00, xmm_key_shuf_mask);

if (K.Length != 11)
if (K.Length != 44)
{
xmm_result = Aes.Decrypt(xmm_result, xmm_temp1);
xmm_result = Aes.Decrypt(xmm_result, xmm_temp2);

xmm_temp1 = LoadKey(K, 0xb0, xmm_key_shuf_mask);
xmm_temp2 = LoadKey(K, 0xc0, xmm_key_shuf_mask);

if (K.Length != 13)
if (K.Length != 52)
{
xmm_result = Aes.Decrypt(xmm_result, xmm_temp1);
xmm_result = Aes.Decrypt(xmm_result, xmm_temp2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static bool EncryptBlock(byte[] @in, int inOffset, byte[] @out, int outOf
#if NETCOREAPP3_0_OR_GREATER
if (X86.IsSupported)
{
X86.EncryptBlock(@in.AsSpan(inOffset), @out.AsSpan(outOffset), MemoryMarshal.AsBytes((ReadOnlySpan<int>)K));
X86.EncryptBlock(@in.AsSpan(inOffset), @out.AsSpan(outOffset), K);
return true;
}
#endif
Expand All @@ -28,7 +28,7 @@ public static bool DecryptBlock(byte[] @in, int inOffset, byte[] @out, int outOf
#if NETCOREAPP3_0_OR_GREATER
if (X86.IsSupported)
{
X86.DecryptBlock(@in.AsSpan(inOffset), @out.AsSpan(outOffset), MemoryMarshal.AsBytes((ReadOnlySpan<int>)K));
X86.DecryptBlock(@in.AsSpan(inOffset), @out.AsSpan(outOffset), K);
return true;
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ namespace IKVM.Java.Externs.com.sun.crypto.provider;
internal static class Vector128Polyfill
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector128<T> Create<T>(ReadOnlySpan<T> values) where T : unmanaged
public static Vector128<T> LoadUnsafe<T>(ref readonly T source, nuint elementOffset) where T : struct
{
// Use with caution.
// Supports blittable primitives only.
ref byte address = ref Unsafe.As<T, byte>(ref Unsafe.Add(ref Unsafe.AsRef(in source), (nint)elementOffset));
return Unsafe.ReadUnaligned<Vector128<T>>(ref address);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector128<T> Create<T>(ReadOnlySpan<T> values) where T : struct
{
if (values.Length < Vector128<T>.Count)
{
Expand All @@ -20,7 +29,7 @@ public static Vector128<T> Create<T>(ReadOnlySpan<T> values) where T : unmanaged
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CopyTo<T>(this Vector128<T> vector, Span<T> destination) where T : unmanaged
public static void CopyTo<T>(this Vector128<T> vector, Span<T> destination) where T : struct
{
if (destination.Length < Vector128<T>.Count)
{
Expand Down

0 comments on commit 3da3113

Please sign in to comment.