From 29371caa9c29fd6cfdfb238d98f53fda59e2e8a7 Mon Sep 17 00:00:00 2001 From: vnugent Date: Wed, 29 Nov 2023 00:15:28 -0500 Subject: immutable fbm clients, websocket abstractions, prep for monocypher/argon2 bindings --- lib/Hashing.Portable/src/Argon2/VnArgon2.cs | 37 +++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 7 deletions(-) (limited to 'lib/Hashing.Portable') diff --git a/lib/Hashing.Portable/src/Argon2/VnArgon2.cs b/lib/Hashing.Portable/src/Argon2/VnArgon2.cs index c07a02e..9b27194 100644 --- a/lib/Hashing.Portable/src/Argon2/VnArgon2.cs +++ b/lib/Hashing.Portable/src/Argon2/VnArgon2.cs @@ -133,7 +133,7 @@ namespace VNLib.Hashing using IMemoryHandle buffer = PwHeap.Alloc(saltbytes + passBytes); Span saltBuffer = buffer.AsSpan(0, saltbytes); - Span passBuffer = buffer.AsSpan(passBytes); + Span passBuffer = buffer.AsSpan(saltbytes, passBytes); //Encode salt with span the same size of the salt _ = LocEncoding.GetBytes(salt, saltBuffer); @@ -142,7 +142,12 @@ namespace VNLib.Hashing _ = LocEncoding.GetBytes(password, passBuffer); //Hash - return Hash2id(lib, passBuffer, saltBuffer, secret, in costParams, hashLen); + string result = Hash2id(lib, passBuffer, saltBuffer, secret, in costParams, hashLen); + + //Zero buffer + MemoryUtil.InitializeBlock(ref buffer.GetReference(), buffer.GetIntLength()); + + return result; } /// @@ -176,7 +181,12 @@ namespace VNLib.Hashing _ = LocEncoding.GetBytes(password, pwdHandle.Span); //Hash - return Hash2id(lib, pwdHandle.Span, salt, secret, in costParams, hashLen); + string result = Hash2id(lib, pwdHandle.Span, salt, secret, in costParams, hashLen); + + //Zero buffer + MemoryUtil.InitializeBlock(ref pwdHandle.GetReference(), pwdHandle.GetIntLength()); + + return result; } /// @@ -213,6 +223,9 @@ namespace VNLib.Hashing //encode salt salts = Convert.ToBase64String(salt); + //Zero buffer + MemoryUtil.InitializeBlock(ref hashHandle.GetReference(), hashHandle.GetIntLength()); + //Encode salt in base64 return $"${ID_MODE}$v={(int)Argon2Version.Version13},m={costParams.MemoryCost},t={costParams.TimeCost},p={costParams.Parallelism},s={salts}${hash}"; } @@ -347,7 +360,12 @@ namespace VNLib.Hashing //encode password bytes rawPassLen = LocEncoding.GetBytes(rawPass, rawPassBuf); //Verify password - return Verify2id(lib, rawPassBuf[..rawPassLen], saltBuf, secret, passBuf, in costParams); + bool result = Verify2id(lib, rawPassBuf[..rawPassLen], saltBuf, secret, passBuf, in costParams); + + //Zero buffer + MemoryUtil.InitializeBlock(ref rawBufferHandle.GetReference(), rawBufferHandle.GetIntLength()); + + return result; } /// @@ -410,12 +428,17 @@ namespace VNLib.Hashing context->outptr = outputPtr.Pointer; context->outlen = (uint)outputHandle.Length; //Hash - Argon2_ErrorCodes result = (Argon2_ErrorCodes)lib.Argon2Hash((IntPtr)context); + Argon2_ErrorCodes argResult = (Argon2_ErrorCodes)lib.Argon2Hash((IntPtr)context); //Throw an excpetion if an error ocurred - ThrowOnArgonErr(result); + ThrowOnArgonErr(argResult); } //Return the comparison - return CryptographicOperations.FixedTimeEquals(outputHandle.Span, hashBytes); + bool result = CryptographicOperations.FixedTimeEquals(outputHandle.Span, hashBytes); + + //Zero buffer + MemoryUtil.InitializeBlock(ref outputHandle.GetReference(), outputHandle.GetIntLength()); + + return result; } private static void ThrowOnArgonErr(Argon2_ErrorCodes result) -- cgit