Skip to content

Commit

Permalink
blake2b_state may now be unaligned
Browse files Browse the repository at this point in the history
  • Loading branch information
ektrah committed Jan 20, 2019
1 parent a2d7295 commit 5b3e39a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 84 deletions.
64 changes: 19 additions & 45 deletions src/Cryptography/Blake2b.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,17 @@ internal unsafe override bool FinalizeAndVerifyCore(
Debug.Assert(hash.Length >= crypto_generichash_blake2b_BYTES_MIN);
Debug.Assert(hash.Length <= crypto_generichash_blake2b_BYTES_MAX);

byte* buffer = stackalloc byte[63 + Unsafe.SizeOf<crypto_generichash_blake2b_state>()];
crypto_generichash_blake2b_state* state_ = Align64(buffer);
*state_ = state.blake2b;

byte* temp = stackalloc byte[hash.Length];

int error = crypto_generichash_blake2b_final(
state_,
temp,
(UIntPtr)hash.Length);

Debug.Assert(error == 0);
fixed (crypto_generichash_blake2b_state* state_ = &state.blake2b)
{
int error = crypto_generichash_blake2b_final(
state_,
temp,
(UIntPtr)hash.Length);

state.blake2b = *state_;
Debug.Assert(error == 0);
}

fixed (byte* @out = hash)
{
Expand All @@ -83,10 +80,7 @@ internal unsafe override void FinalizeCore(
Debug.Assert(hash.Length >= crypto_generichash_blake2b_BYTES_MIN);
Debug.Assert(hash.Length <= crypto_generichash_blake2b_BYTES_MAX);

byte* buffer = stackalloc byte[63 + Unsafe.SizeOf<crypto_generichash_blake2b_state>()];
crypto_generichash_blake2b_state* state_ = Align64(buffer);
*state_ = state.blake2b;

fixed (crypto_generichash_blake2b_state* state_ = &state.blake2b)
fixed (byte* @out = hash)
{
int error = crypto_generichash_blake2b_final(
Expand All @@ -96,8 +90,6 @@ internal unsafe override void FinalizeCore(

Debug.Assert(error == 0);
}

state.blake2b = *state_;
}

internal unsafe override void InitializeCore(
Expand All @@ -106,28 +98,23 @@ internal unsafe override void InitializeCore(
Debug.Assert(HashSize >= crypto_generichash_blake2b_BYTES_MIN);
Debug.Assert(HashSize <= crypto_generichash_blake2b_BYTES_MAX);

byte* buffer = stackalloc byte[63 + Unsafe.SizeOf<crypto_generichash_blake2b_state>()];
crypto_generichash_blake2b_state* state_ = Align64(buffer);

int error = crypto_generichash_blake2b_init(
state_,
null,
UIntPtr.Zero,
(UIntPtr)HashSize);

Debug.Assert(error == 0);
fixed (crypto_generichash_blake2b_state* state_ = &state.blake2b)
{
int error = crypto_generichash_blake2b_init(
state_,
null,
UIntPtr.Zero,
(UIntPtr)HashSize);

state.blake2b = *state_;
Debug.Assert(error == 0);
}
}

internal unsafe override void UpdateCore(
ref IncrementalHashState state,
ReadOnlySpan<byte> data)
{
byte* buffer = stackalloc byte[63 + Unsafe.SizeOf<crypto_generichash_blake2b_state>()];
crypto_generichash_blake2b_state* state_ = Align64(buffer);
*state_ = state.blake2b;

fixed (crypto_generichash_blake2b_state* state_ = &state.blake2b)
fixed (byte* @in = data)
{
int error = crypto_generichash_blake2b_update(
Expand All @@ -137,8 +124,6 @@ internal unsafe override void UpdateCore(

Debug.Assert(error == 0);
}

state.blake2b = *state_;
}

private protected unsafe override void HashCore(
Expand Down Expand Up @@ -191,17 +176,6 @@ private protected unsafe override bool VerifyCore(
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe crypto_generichash_blake2b_state* Align64(byte* value)
{
unchecked
{
return sizeof(byte*) == sizeof(uint)
? (crypto_generichash_blake2b_state*)(((uint)value + 63u) & ~63u)
: (crypto_generichash_blake2b_state*)(((ulong)value + 63ul) & ~63ul);
}
}

private static void SelfTest()
{
if ((crypto_generichash_blake2b_bytes() != (UIntPtr)crypto_generichash_blake2b_BYTES) ||
Expand Down
50 changes: 11 additions & 39 deletions src/Cryptography/Blake2bMac.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,17 @@ internal unsafe override bool FinalizeAndVerifyCore(
Debug.Assert(mac.Length >= crypto_generichash_blake2b_BYTES_MIN);
Debug.Assert(mac.Length <= crypto_generichash_blake2b_BYTES_MAX);

byte* buffer = stackalloc byte[63 + Unsafe.SizeOf<crypto_generichash_blake2b_state>()];
crypto_generichash_blake2b_state* state_ = Align64(buffer);
*state_ = state.blake2b;

byte* temp = stackalloc byte[mac.Length];

int error = crypto_generichash_blake2b_final(
state_,
temp,
(UIntPtr)mac.Length);

Debug.Assert(error == 0);
fixed (crypto_generichash_blake2b_state* state_ = &state.blake2b)
{
int error = crypto_generichash_blake2b_final(
state_,
temp,
(UIntPtr)mac.Length);

state.blake2b = *state_;
Debug.Assert(error == 0);
}

fixed (byte* @out = mac)
{
Expand All @@ -114,10 +111,7 @@ internal unsafe override void FinalizeCore(
Debug.Assert(mac.Length >= crypto_generichash_blake2b_BYTES_MIN);
Debug.Assert(mac.Length <= crypto_generichash_blake2b_BYTES_MAX);

byte* buffer = stackalloc byte[63 + Unsafe.SizeOf<crypto_generichash_blake2b_state>()];
crypto_generichash_blake2b_state* state_ = Align64(buffer);
*state_ = state.blake2b;

fixed (crypto_generichash_blake2b_state* state_ = &state.blake2b)
fixed (byte* @out = mac)
{
int error = crypto_generichash_blake2b_final(
Expand All @@ -127,8 +121,6 @@ internal unsafe override void FinalizeCore(

Debug.Assert(error == 0);
}

state.blake2b = *state_;
}

internal override int GetSeedSize()
Expand All @@ -145,9 +137,7 @@ internal unsafe override void InitializeCore(
Debug.Assert(MacSize >= crypto_generichash_blake2b_BYTES_MIN);
Debug.Assert(MacSize <= crypto_generichash_blake2b_BYTES_MAX);

byte* buffer = stackalloc byte[63 + Unsafe.SizeOf<crypto_generichash_blake2b_state>()];
crypto_generichash_blake2b_state* state_ = Align64(buffer);

fixed (crypto_generichash_blake2b_state* state_ = &state.blake2b)
fixed (byte* k = key)
{
int error = crypto_generichash_blake2b_init(
Expand All @@ -158,8 +148,6 @@ internal unsafe override void InitializeCore(

Debug.Assert(error == 0);
}

state.blake2b = *state_;
}

internal override bool TryExportKey(
Expand Down Expand Up @@ -204,10 +192,7 @@ internal unsafe override void UpdateCore(
ref IncrementalMacState state,
ReadOnlySpan<byte> data)
{
byte* buffer = stackalloc byte[63 + Unsafe.SizeOf<crypto_generichash_blake2b_state>()];
crypto_generichash_blake2b_state* state_ = Align64(buffer);
*state_ = state.blake2b;

fixed (crypto_generichash_blake2b_state* state_ = &state.blake2b)
fixed (byte* @in = data)
{
int error = crypto_generichash_blake2b_update(
Expand All @@ -217,8 +202,6 @@ internal unsafe override void UpdateCore(

Debug.Assert(error == 0);
}

state.blake2b = *state_;
}

private protected unsafe override void MacCore(
Expand Down Expand Up @@ -279,17 +262,6 @@ private protected unsafe override bool VerifyCore(
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe crypto_generichash_blake2b_state* Align64(byte* value)
{
unchecked
{
return sizeof(byte*) == sizeof(uint)
? (crypto_generichash_blake2b_state*)(((uint)value + 63u) & ~63u)
: (crypto_generichash_blake2b_state*)(((ulong)value + 63ul) & ~63ul);
}
}

private static void SelfTest()
{
if ((crypto_generichash_blake2b_bytes() != (UIntPtr)crypto_generichash_blake2b_BYTES) ||
Expand Down

0 comments on commit 5b3e39a

Please sign in to comment.