feat: adjustments to Argon2 settings
All checks were successful
Test if Server can be built / build-server (push) Successful in 26s

This commit is contained in:
Timothy Schenk 2023-08-14 20:00:56 +02:00
parent fd47ba2db0
commit 63c29f21df
3 changed files with 15 additions and 26 deletions

View file

@ -26,28 +26,28 @@ public class LoginHandler : IPacketHandler<LoginInfoPacket>
public async Task HandleAsync(LoginInfoPacket packet, TcpSession session) public async Task HandleAsync(LoginInfoPacket packet, TcpSession session)
{ {
var loginResponseReason = LoginResponseReason.Error; LoginResponseReason loginResponseReason;
this.logger.LogInformation("Login data: Username {Username} & Password {Password}", packet.Username, this.logger.LogInformation("Login data: Username {Username} & Password {Password}", packet.Username,
packet.Password); packet.Password);
var account = this.wonderkingContext.Accounts.FirstOrDefault(a => a.Username == packet.Username); var account = this.wonderkingContext.Accounts.FirstOrDefault(a => a.Username == packet.Username);
// https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#argon2id // https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Chea1t_Sheet.html#argon2id
// "Use Argon2id with a minimum configuration of 19 MiB of memory, an iteration count of 2, and 1 degree of parallelism." // "Use Argon2id with a minimum configuration of 19 MiB of memory, an iteration count of 2, and 1 degree of parallelism."
var argon2Id = new Argon2id(Encoding.ASCII.GetBytes(packet.Password)); var argon2Id = new Argon2id(Encoding.ASCII.GetBytes(packet.Password));
argon2Id.MemorySize = 1024 * 40; argon2Id.MemorySize = 1024 * 19;
argon2Id.Iterations = 4; argon2Id.Iterations = 2;
argon2Id.DegreeOfParallelism = 2; argon2Id.DegreeOfParallelism = 1;
if (account == null) if (account == null)
{ {
if (this.configuration.GetSection("Testing").GetValue<bool>("CreateAccountOnLogin")) if (this.configuration.GetSection("Testing").GetValue<bool>("CreateAccountOnLogin"))
{ {
argon2Id.Salt = RandomNumberGenerator.GetBytes(128); argon2Id.Salt = RandomNumberGenerator.GetBytes(16);
var finalAccount = var finalAccount =
await this.wonderkingContext.Accounts.AddAsync(new Account(packet.Username, Array.Empty<byte>(), "", await this.wonderkingContext.Accounts.AddAsync(new Account(packet.Username, Array.Empty<byte>(), "",
0, argon2Id.Salt)); 0, argon2Id.Salt));
await this.wonderkingContext.SaveChangesAsync(); await this.wonderkingContext.SaveChangesAsync();
argon2Id.AssociatedData = finalAccount.Entity.Id.ToByteArray(); argon2Id.AssociatedData = finalAccount.Entity.Id.ToByteArray();
finalAccount.Entity.Password = await argon2Id.GetBytesAsync(128); finalAccount.Entity.Password = await argon2Id.GetBytesAsync(16);
this.wonderkingContext.Accounts.Update(finalAccount.Entity); this.wonderkingContext.Accounts.Update(finalAccount.Entity);
loginResponseReason = LoginResponseReason.Ok; loginResponseReason = LoginResponseReason.Ok;
await this.wonderkingContext.SaveChangesAsync(); await this.wonderkingContext.SaveChangesAsync();
@ -63,7 +63,7 @@ public class LoginHandler : IPacketHandler<LoginInfoPacket>
{ {
argon2Id.Salt = account.Salt; argon2Id.Salt = account.Salt;
argon2Id.AssociatedData = account.Id.ToByteArray(); argon2Id.AssociatedData = account.Id.ToByteArray();
var tempPasswordBytes = await argon2Id.GetBytesAsync(128); var tempPasswordBytes = await argon2Id.GetBytesAsync(16);
loginResponseReason = tempPasswordBytes.SequenceEqual(account.Password) loginResponseReason = tempPasswordBytes.SequenceEqual(account.Password)
? LoginResponseReason.Ok ? LoginResponseReason.Ok
: LoginResponseReason.WrongPassword; : LoginResponseReason.WrongPassword;

View file

@ -4,10 +4,11 @@
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>warnings</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<RootNamespace>Server</RootNamespace> <RootNamespace>Server</RootNamespace>
<LangVersion>default</LangVersion> <LangVersion>default</LangVersion>
<ServerGarbageCollection>true</ServerGarbageCollection>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@ -64,18 +65,6 @@
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Roslynator.Analyzers" Version="4.4.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Roslynator.CodeAnalysis.Analyzers" Version="4.4.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Roslynator.Formatting.Analyzers" Version="4.4.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0"/> <PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0"/>
</ItemGroup> </ItemGroup>

View file

@ -58,9 +58,9 @@ public class PacketDistributorService : IHostedService
private Dictionary<OperationCode, Type> GetPacketsWithId(Assembly executingAssembly) private Dictionary<OperationCode, Type> GetPacketsWithId(Assembly executingAssembly)
{ {
var packetsWithId = executingAssembly.GetTypes().AsParallel() var packetsWithId = executingAssembly.GetTypes().AsParallel()
.Where(type => type.GetCustomAttribute<PacketIdAttribute>() != null && type.HasInterface(typeof(IPacket)) && .Where(type => type.HasInterface(typeof(IPacket)) && !type.IsInterface && !type.IsAbstract)
!type.IsInterface) .Where(type => type.GetCustomAttribute<PacketIdAttribute>() != null)
.ToDictionary(packet => packet.GetCustomAttribute<PacketIdAttribute>()!.Code); .ToDictionary(type => type.GetCustomAttribute<PacketIdAttribute>().Code);
if (packetsWithId is not { Count: 0 }) if (packetsWithId is not { Count: 0 })
{ {
packetsWithId.AsParallel().ForAll(packet => packetsWithId.AsParallel().ForAll(packet =>
@ -81,8 +81,8 @@ public class PacketDistributorService : IHostedService
t is { IsClass: true, IsAbstract: false } && t t is { IsClass: true, IsAbstract: false } && t
.GetInterfaces().Any(i => .GetInterfaces().Any(i =>
i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPacketHandler<>))).ToDictionary(type => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPacketHandler<>))).ToDictionary(type =>
type.GetInterfaces().First(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IPacketHandler<>)) type.GetInterfaces().First(t =>t is { IsGenericType: true} && t.GetGenericTypeDefinition() == typeof(IPacketHandler<>))
.GetGenericArguments()[0].GetCustomAttribute<PacketIdAttribute>().Code); .GetGenericArguments().First().GetCustomAttribute<PacketIdAttribute>()!.Code);
if (packetHandlersWithId is not { Count: 0 }) if (packetHandlersWithId is not { Count: 0 })
{ {