From f957a1dd5b902655ec601802e9145fcaf7b459d8 Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Sun, 24 May 2020 19:13:19 -0700 Subject: [PATCH] Cleans up Argon2 (#141) --- Projects/Argon2/Argon2.cs | 4 +-- Projects/Argon2/Argon2.csproj | 5 ++- Projects/Argon2/Argon2.nuspec | 2 +- Projects/Argon2/Argon2Error.cs | 1 + Projects/Argon2/Argon2Exception.cs | 2 +- Projects/Argon2/Argon2PasswordHasher.cs | 33 ++++--------------- Projects/Argon2/HashMetadata.cs | 4 +-- Projects/Server/Utilities/RandomProviders.cs | 1 - .../Security/Argon2PasswordProtection.cs | 4 +-- 9 files changed, 17 insertions(+), 39 deletions(-) diff --git a/Projects/Argon2/Argon2.cs b/Projects/Argon2/Argon2.cs index 5745dc49e..dba748953 100644 --- a/Projects/Argon2/Argon2.cs +++ b/Projects/Argon2/Argon2.cs @@ -102,10 +102,10 @@ namespace System.Security.Cryptography ); [DllImport("libargon2", EntryPoint = "argon2_verify")] - internal static extern unsafe Argon2Error crypto_argon2_verify(in byte encoded, in byte pwd, int pwdlen, int type); + internal static extern Argon2Error crypto_argon2_verify(in byte encoded, in byte pwd, int pwdlen, int type); [DllImport("libargon2", EntryPoint = "decode_string")] - internal static extern unsafe Argon2Error crypto_decode_string(Argon2Context ctx, in byte str, int type); + internal static extern Argon2Error crypto_decode_string(Argon2Context ctx, in byte str, int type); } } } diff --git a/Projects/Argon2/Argon2.csproj b/Projects/Argon2/Argon2.csproj index ac800bb99..383c40594 100644 --- a/Projects/Argon2/Argon2.csproj +++ b/Projects/Argon2/Argon2.csproj @@ -2,15 +2,14 @@ win-x64;linux-x64;osx-x64 1.1.8 - Server + System.Security.Cryptography Argon2.Bindings x64 8.0 netcoreapp3.1 true - true false - false + true Debug;Release;Analyze 1.1.8 x64 diff --git a/Projects/Argon2/Argon2.nuspec b/Projects/Argon2/Argon2.nuspec index b28f90859..bf03a1491 100644 --- a/Projects/Argon2/Argon2.nuspec +++ b/Projects/Argon2/Argon2.nuspec @@ -2,7 +2,7 @@ Argon2.Bindings - 1.1.6 + 1.1.8 Kamron Batman Kamron Batman MIT diff --git a/Projects/Argon2/Argon2Error.cs b/Projects/Argon2/Argon2Error.cs index 42ebcca6e..bdb645236 100644 --- a/Projects/Argon2/Argon2Error.cs +++ b/Projects/Argon2/Argon2Error.cs @@ -69,6 +69,7 @@ /// This will not be returned from the C# PasswordHasher wrapper /// DECODING_FAIL = -32, + /// /// Unable to create the number of threads requested /// diff --git a/Projects/Argon2/Argon2Exception.cs b/Projects/Argon2/Argon2Exception.cs index c65a414cb..b7a7a2ad6 100644 --- a/Projects/Argon2/Argon2Exception.cs +++ b/Projects/Argon2/Argon2Exception.cs @@ -13,6 +13,6 @@ /// Which method the Argon2Exception originated from /// The error returned from the Argon2 library /// - public Argon2Exception(string action, Argon2Error error) : base(string.Format("Error during Argon2 {0}: ({1}) {2}", action, (int)error, error)) {} + public Argon2Exception(string action, Argon2Error error) : base($"Error during Argon2 {action}: ({(int)error}) {error}") {} } } diff --git a/Projects/Argon2/Argon2PasswordHasher.cs b/Projects/Argon2/Argon2PasswordHasher.cs index 3970207e6..6f72e482f 100644 --- a/Projects/Argon2/Argon2PasswordHasher.cs +++ b/Projects/Argon2/Argon2PasswordHasher.cs @@ -1,6 +1,5 @@ using System.Runtime.InteropServices; using System.Text; -// using System.Text.RegularExpressions; namespace System.Security.Cryptography { @@ -10,10 +9,7 @@ namespace System.Security.Cryptography /// public class Argon2PasswordHasher { - private static readonly RNGCryptoServiceProvider Rng = new RNGCryptoServiceProvider(); - - // private static readonly Regex HashRegex = new Regex(@"^\$argon2([di])\$v=(\d+)$m=(\d+),t=(\d+),p=(\d+)\$([A-Za-z0-9+/=]+)\$([A-Za-z0-9+/=]*)$", RegexOptions.Compiled); - + private static RandomNumberGenerator m_Rng; /// /// How many iterations of the Argon2 hash to perform @@ -48,13 +44,17 @@ namespace System.Security.Cryptography /// public Encoding StringEncoding { get; set; } + /// + /// Randomizer used to generate salts + /// + public RandomNumberGenerator Rng { get; set; } /// /// Initialize the Argon2 PasswordHasher with default performance and algorithm settings based upon the environment the hashing will be used in. /// You should perform your own profiling to determine what the parameters should be for your specific usage; however, this attempts to provide /// some reasonable defaults. /// - public Argon2PasswordHasher() + public Argon2PasswordHasher(RandomNumberGenerator rng = null) { TimeCost = 3; MemoryCost = 8192; @@ -62,28 +62,9 @@ namespace System.Security.Cryptography ArgonType = Argon2Type.Argon2i; HashLength = 32; StringEncoding = Encoding.UTF8; + Rng = rng ?? (m_Rng ??= new RNGCryptoServiceProvider()); } - - /// - /// Initialize the Argon2 PasswordHasher with the performance and algorithm settings to use while hashing - /// How many iterations of the Argon2 hash to perform (default: 3, must be at least 1) - /// How much memory to use while hashing in kibibytes (KiB) (default: 8192 KiB [8 MiB], must be at least 8 KiB) - /// How many threads to use while hashing (default: 1, must be at least 1) - /// The type of Argon2 hashing algorithm to use (Independent [default] or Dependent) - /// The length of the resulting hash in bytes (default: 32) - /// - public Argon2PasswordHasher(uint timeCost = 3, uint memoryCost = 8192, uint parallelism = 1, Argon2Type argonType = Argon2Type.Argon2i, uint hashLength = 32) - { - TimeCost = timeCost; - MemoryCost = memoryCost; - Parallelism = parallelism; - ArgonType = argonType; - HashLength = hashLength; - StringEncoding = Encoding.UTF8; - } - - /// /// Hash the password using Argon2 with a cryptographically-secure, random, 16-byte salt. /// This is the only overload of the Hash method that the typical user will need to use for password storage. The other overloads are provided for interoperability purposes. diff --git a/Projects/Argon2/HashMetadata.cs b/Projects/Argon2/HashMetadata.cs index 4ff264952..a08995353 100644 --- a/Projects/Argon2/HashMetadata.cs +++ b/Projects/Argon2/HashMetadata.cs @@ -1,6 +1,4 @@ -using System; - -namespace System.Security.Cryptography +namespace System.Security.Cryptography { /// /// HashMetadata represents the information stored in the encoded Argon2 format diff --git a/Projects/Server/Utilities/RandomProviders.cs b/Projects/Server/Utilities/RandomProviders.cs index b866d2214..80f696bdf 100644 --- a/Projects/Server/Utilities/RandomProviders.cs +++ b/Projects/Server/Utilities/RandomProviders.cs @@ -19,7 +19,6 @@ *************************************************************************/ using System; -using System.Security.Cryptography; namespace Server { diff --git a/Projects/UOContent/Accounting/Security/Argon2PasswordProtection.cs b/Projects/UOContent/Accounting/Security/Argon2PasswordProtection.cs index 2b99b0cd3..b79d5cafa 100644 --- a/Projects/UOContent/Accounting/Security/Argon2PasswordProtection.cs +++ b/Projects/UOContent/Accounting/Security/Argon2PasswordProtection.cs @@ -3,7 +3,7 @@ * Copyright (C) 2019-2020 - ModernUO Development Team * * Email: hi@modernuo.com * * File: Argon2PasswordProtection.cs * - * Created: 2020/05/01 - Updated: 2020/05/02 * + * Created: 2020/05/01 - Updated: 2020/05/24 * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -26,7 +26,7 @@ namespace Server.Accounting.Security public class Argon2PasswordProtection : IPasswordProtection { public static IPasswordProtection Instance = new Argon2PasswordProtection(); - private Argon2PasswordHasher m_PasswordHasher = new Argon2PasswordHasher(); + private Argon2PasswordHasher m_PasswordHasher = new Argon2PasswordHasher(RandomProviders.SecureProvider as RandomNumberGenerator); public string EncryptPassword(string plainPassword) => m_PasswordHasher.Hash(plainPassword);