diff --git a/Projects/Argon2/Argon2.cs b/Projects/Argon2/Argon2.cs index c1e546b4c..0a9c53ca6 100644 --- a/Projects/Argon2/Argon2.cs +++ b/Projects/Argon2/Argon2.cs @@ -19,7 +19,17 @@ namespace System.Security.Cryptography internal static class Argon2 { - internal static Argon2Error Hash(uint t_cost, uint m_cost, uint parallelism, + internal static readonly bool IsDarwin = RuntimeInformation.IsOSPlatform(OSPlatform.OSX); + internal static readonly bool IsFreeBSD = RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD); + internal static readonly bool IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); + internal static readonly bool IsUnix = IsLinux || IsDarwin || IsFreeBSD; + + internal static readonly IArgon2 Library = IsUnix ? (IArgon2)new UnixArgon2() : new WindowsArgon2(); + } + + internal class WindowsArgon2 : IArgon2 + { + public Argon2Error Hash(uint t_cost, uint m_cost, uint parallelism, ReadOnlySpan pwd, ReadOnlySpan salt, Span hash, @@ -33,15 +43,15 @@ namespace System.Security.Cryptography type, version ); - internal static Argon2Error Verify(ReadOnlySpan encoded, ReadOnlySpan pwd, int pwdlen, int type) => + public Argon2Error Verify(ReadOnlySpan encoded, ReadOnlySpan pwd, int pwdlen, int type) => SafeNativeMethods.argon2_verify(in encoded.GetPinnableReference(), in pwd.GetPinnableReference(), pwdlen, type); - internal static Argon2Error Decode(Argon2Context ctx, ReadOnlySpan str, int type) => + public Argon2Error Decode(Argon2Context ctx, ReadOnlySpan str, int type) => SafeNativeMethods.decode_string(ctx, in str.GetPinnableReference(), type); internal static class SafeNativeMethods { - [DllImport("libargon2", EntryPoint = "argon2_hash")] + [DllImport("argon2", EntryPoint = "argon2_hash", CallingConvention = CallingConvention.Cdecl)] internal static extern Argon2Error argon2_hash(uint t_cost, uint m_cost, uint parallelism, in byte pwd, int pwdlen, in byte salt, int saltlen, @@ -50,10 +60,51 @@ namespace System.Security.Cryptography int type, int version ); - [DllImport("libargon2", EntryPoint = "argon2_verify")] + [DllImport("argon2", EntryPoint = "argon2_verify", CallingConvention = CallingConvention.Cdecl)] internal static extern Argon2Error argon2_verify(in byte encoded, in byte pwd, int pwdlen, int type); - [DllImport("libargon2", EntryPoint = "decode_string")] + [DllImport("argon2", EntryPoint = "decode_string", CallingConvention = CallingConvention.Cdecl)] + internal static extern Argon2Error decode_string(Argon2Context ctx, in byte str, int type); + } + } + + internal class UnixArgon2 : IArgon2 + { + public Argon2Error Hash(uint t_cost, uint m_cost, uint parallelism, + ReadOnlySpan pwd, + ReadOnlySpan salt, + Span hash, + Span encoded, + int type, int version) => + SafeNativeMethods.argon2_hash(t_cost, m_cost, parallelism, + in pwd.GetPinnableReference(), pwd.Length, + in salt.GetPinnableReference(), salt.Length, + ref hash.GetPinnableReference(), hash.Length, + ref encoded.GetPinnableReference(), encoded.Length, + type, version + ); + + public Argon2Error Verify(ReadOnlySpan encoded, ReadOnlySpan pwd, int pwdlen, int type) => + SafeNativeMethods.argon2_verify(in encoded.GetPinnableReference(), in pwd.GetPinnableReference(), pwdlen, type); + + public Argon2Error Decode(Argon2Context ctx, ReadOnlySpan str, int type) => + SafeNativeMethods.decode_string(ctx, in str.GetPinnableReference(), type); + + internal static class SafeNativeMethods + { + [DllImport("argon2", EntryPoint = "argon2_hash")] + internal static extern Argon2Error argon2_hash(uint t_cost, uint m_cost, uint parallelism, + in byte pwd, int pwdlen, + in byte salt, int saltlen, + ref byte hash, int hashlen, + ref byte encoded, int encodedlen, + int type, int version + ); + + [DllImport("argon2", EntryPoint = "argon2_verify")] + internal static extern Argon2Error argon2_verify(in byte encoded, in byte pwd, int pwdlen, int type); + + [DllImport("argon2", EntryPoint = "decode_string")] internal static extern Argon2Error decode_string(Argon2Context ctx, in byte str, int type); } } diff --git a/Projects/Argon2/Argon2.csproj b/Projects/Argon2/Argon2.csproj index 706954214..a978e0240 100644 --- a/Projects/Argon2/Argon2.csproj +++ b/Projects/Argon2/Argon2.csproj @@ -1,7 +1,7 @@  win-x64;linux-x64;osx-x64 - 1.2.0 + 1.2.5 System.Security.Cryptography Argon2.Bindings x64 @@ -11,7 +11,7 @@ false true Debug;Release;Analyze - 1.2.0 + 1.2.5 x64 @@ -20,11 +20,6 @@ runtimes/win-x64/native PreserveNewest - - true - runtimes/osx-x64/native - PreserveNewest - true runtimes/linux-x64/native diff --git a/Projects/Argon2/Argon2PasswordHasher.cs b/Projects/Argon2/Argon2PasswordHasher.cs index 65ac501be..6f72e482f 100644 --- a/Projects/Argon2/Argon2PasswordHasher.cs +++ b/Projects/Argon2/Argon2PasswordHasher.cs @@ -94,7 +94,7 @@ namespace System.Security.Cryptography Span passwordBytes = stackalloc byte[StringEncoding.GetByteCount(password)]; StringEncoding.GetBytes(password, passwordBytes); - var result = Argon2.Hash( + var result = Argon2.Library.Hash( TimeCost, MemoryCost, Parallelism, @@ -128,7 +128,7 @@ namespace System.Security.Cryptography Span passwordBytes = stackalloc byte[StringEncoding.GetByteCount(password)]; StringEncoding.GetBytes(password, passwordBytes); - var result = Argon2.Hash( + var result = Argon2.Library.Hash( TimeCost, MemoryCost, Parallelism, @@ -172,7 +172,7 @@ namespace System.Security.Cryptography /// public bool Verify(ReadOnlySpan expectedHash, ReadOnlySpan password) { - var result = Argon2.Verify(expectedHash, password, password.Length, (int)ArgonType); + var result = Argon2.Library.Verify(expectedHash, password, password.Length, (int)ArgonType); if (result == Argon2Error.OK || result == Argon2Error.VERIFY_MISMATCH || result == Argon2Error.DECODING_FAIL) return result == Argon2Error.OK; @@ -246,7 +246,7 @@ namespace System.Security.Cryptography Span bytes = stackalloc byte[formattedHash.Length]; Encoding.ASCII.GetBytes(formattedHash, bytes); - var result = Argon2.Decode(context, bytes, (int)type); + var result = Argon2.Library.Decode(context, bytes, (int)type); if (result != Argon2Error.OK) return null; diff --git a/Projects/Argon2/runtimes/linux-x64/native/libargon2.so b/Projects/Argon2/runtimes/linux-x64/native/libargon2.so index 5303d8d75..1097b8dcd 100755 Binary files a/Projects/Argon2/runtimes/linux-x64/native/libargon2.so and b/Projects/Argon2/runtimes/linux-x64/native/libargon2.so differ diff --git a/Projects/Argon2/runtimes/osx-x64/native/libargon2.dylib b/Projects/Argon2/runtimes/osx-x64/native/libargon2.dylib index 1c0cc3953..05d380b71 100755 Binary files a/Projects/Argon2/runtimes/osx-x64/native/libargon2.dylib and b/Projects/Argon2/runtimes/osx-x64/native/libargon2.dylib differ diff --git a/Projects/Argon2/runtimes/win-x64/native/libargon2.dll b/Projects/Argon2/runtimes/win-x64/native/libargon2.dll old mode 100644 new mode 100755 index 6a46dd60d..e76fc5a4d Binary files a/Projects/Argon2/runtimes/win-x64/native/libargon2.dll and b/Projects/Argon2/runtimes/win-x64/native/libargon2.dll differ diff --git a/Projects/UOContent/UOContent.csproj b/Projects/UOContent/UOContent.csproj index 974b49649..1a6f5093d 100644 --- a/Projects/UOContent/UOContent.csproj +++ b/Projects/UOContent/UOContent.csproj @@ -80,7 +80,7 @@ - +