Updates Argon2 to use Spans/Stackalloc (#140)

This commit is contained in:
Kamron Batman 2020-05-24 18:45:30 -07:00 • committed by GitHub
parent 2770ab1d50
commit 5d51e97c52
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 115 additions and 230 deletions

View file

@ -1,127 +1,111 @@
using System.Runtime.InteropServices;
using System.Text;
namespace Server.Accounting.Security
namespace System.Security.Cryptography
{
internal interface IArgon2
{
public Argon2Error Hash(uint t_cost, uint m_cost, uint parallelism,
byte[] pwd,
byte[] salt,
byte[] hash,
byte[] encoded,
ReadOnlySpan<byte> pwd,
ReadOnlySpan<byte> salt,
Span<byte> hash,
Span<byte> encoded,
int type, int version);
Argon2Error Verify(byte[] encoded, byte[] pwd, int pwdlen, int type);
Argon2Error Verify(ReadOnlySpan<byte> encoded, ReadOnlySpan<byte> pwd, int pwdlen, int type);
// TODO: Change str to use ReadOnlySpan<char> then convert to pointer later
Argon2Error Decode(Argon2Context ctx, string str, int type);
Argon2Error Decode(Argon2Context ctx, ReadOnlySpan<byte> str, int type);
}
internal static class Argon2
{
internal static readonly IArgon2 Library;
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);
static Argon2()
{
if (RuntimeUtility.IsUnix)
Library = new UnixArgon2();
else
Library = new WindowsArgon2();
}
internal static readonly IArgon2 Library =
IsLinux || IsFreeBSD || IsDarwin ? (IArgon2)new UnixArgon2() : new WindowsArgon2();
}
internal class WindowsArgon2 : IArgon2
{
public Argon2Error Hash(uint t_cost, uint m_cost, uint parallelism,
byte[] pwd,
byte[] salt,
byte[] hash,
byte[] encoded,
ReadOnlySpan<byte> pwd,
ReadOnlySpan<byte> salt,
Span<byte> hash,
Span<byte> encoded,
int type, int version) =>
NativeMethods.crypto_argon2_hash(t_cost, m_cost, parallelism,
pwd, pwd.Length,
salt, salt.Length,
hash, hash.Length,
encoded, encoded.Length,
SafeNativeMethods.crypto_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(byte[] encoded, byte[] pwd, int pwdlen, int type) =>
NativeMethods.crypto_argon2_verify(encoded, pwd, pwdlen, type);
public Argon2Error Verify(ReadOnlySpan<byte> encoded, ReadOnlySpan<byte> pwd, int pwdlen, int type) =>
SafeNativeMethods.crypto_argon2_verify(in encoded.GetPinnableReference(), in pwd.GetPinnableReference(), pwdlen, type);
public Argon2Error Decode(Argon2Context ctx, string str, int type)
{
byte[] bytes = new byte[str.Length];
Encoding.ASCII.GetBytes(str, bytes);
public Argon2Error Decode(Argon2Context ctx, ReadOnlySpan<byte> str, int type) =>
SafeNativeMethods.crypto_decode_string(ctx, in str.GetPinnableReference(), type);
// TODO: Use pointers instead
return NativeMethods.crypto_decode_string(ctx, bytes, type);
}
internal static class NativeMethods
internal static class SafeNativeMethods
{
[DllImport("argon2.dll", EntryPoint = "crypto_argon2_hash", CallingConvention = CallingConvention.Cdecl)]
internal static extern Argon2Error crypto_argon2_hash(uint t_cost, uint m_cost, uint parallelism,
byte[] pwd, int pwdlen,
byte[] salt, int saltlen,
byte[] hash, int hashlen,
byte[] encoded, int encodedlen,
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.dll", EntryPoint = "crypto_argon2_verify", CallingConvention = CallingConvention.Cdecl)]
internal static extern Argon2Error crypto_argon2_verify(byte[] encoded, byte[] pwd, int pwdlen, int type);
internal static extern Argon2Error crypto_argon2_verify(in byte encoded, in byte pwd, int pwdlen, int type);
[DllImport("argon2.dll", EntryPoint = "crypto_decode_string", CallingConvention = CallingConvention.Cdecl)]
internal static extern Argon2Error crypto_decode_string(Argon2Context ctx, byte[] str, int type);
internal static extern Argon2Error crypto_decode_string(Argon2Context ctx, in byte str, int type);
}
}
internal class UnixArgon2 : IArgon2
{
public Argon2Error Hash(uint t_cost, uint m_cost, uint parallelism,
byte[] pwd,
byte[] salt,
byte[] hash,
byte[] encoded,
ReadOnlySpan<byte> pwd,
ReadOnlySpan<byte> salt,
Span<byte> hash,
Span<byte> encoded,
int type, int version) =>
NativeMethods.crypto_argon2_hash(t_cost, m_cost, parallelism,
pwd, pwd.Length,
salt, salt.Length,
hash, hash.Length,
encoded, encoded.Length,
SafeNativeMethods.crypto_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(byte[] encoded, byte[] pwd, int pwdlen, int type) =>
NativeMethods.crypto_argon2_verify(encoded, pwd, pwdlen, type);
public Argon2Error Verify(ReadOnlySpan<byte> encoded, ReadOnlySpan<byte> pwd, int pwdlen, int type) =>
SafeNativeMethods.crypto_argon2_verify(in encoded.GetPinnableReference(), in pwd.GetPinnableReference(), pwdlen, type);
public Argon2Error Decode(Argon2Context ctx, string str, int type)
{
byte[] bytes = new byte[str.Length];
Encoding.ASCII.GetBytes(str, bytes);
public Argon2Error Decode(Argon2Context ctx, ReadOnlySpan<byte> str, int type) =>
SafeNativeMethods.crypto_decode_string(ctx, in str.GetPinnableReference(), type);
// TODO: Use pointers instead
return NativeMethods.crypto_decode_string(ctx, bytes, type);
}
internal static class NativeMethods
internal static class SafeNativeMethods
{
[DllImport("libargon2", EntryPoint = "argon2_hash")]
internal static extern Argon2Error crypto_argon2_hash(uint t_cost, uint m_cost, uint parallelism,
byte[] pwd, int pwdlen,
byte[] salt, int saltlen,
byte[] hash, int hashlen,
byte[] encoded, int encodedlen,
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("libargon2", EntryPoint = "argon2_verify")]
internal static extern Argon2Error crypto_argon2_verify(byte[] encoded, byte[] pwd, int pwdlen, int type);
internal static extern unsafe Argon2Error crypto_argon2_verify(in byte encoded, in byte pwd, int pwdlen, int type);
[DllImport("libargon2", EntryPoint = "decode_string")]
internal static extern Argon2Error crypto_decode_string(Argon2Context ctx, byte[] str, int type);
internal static extern unsafe Argon2Error crypto_decode_string(Argon2Context ctx, in byte str, int type);
}
}
}