Skip to content

fix: resolve 5 bugs in .NET managed layer#34

Merged
xoofx merged 1 commit intoxoofx:masterfrom
HMBSbige:fix/dotnet-bugs
Mar 12, 2026
Merged

fix: resolve 5 bugs in .NET managed layer#34
xoofx merged 1 commit intoxoofx:masterfrom
HMBSbige:fix/dotnet-bugs

Conversation

@HMBSbige
Copy link
Contributor

Summary

  • Hash.Equals is not constant-time: SequenceCompareTo short-circuits on the first differing byte, making it vulnerable to timing attacks. Replaced with CryptographicOperations.FixedTimeEquals. This contradicts the doc comment which states "constant-time equality checking".
  • Blake3Stream.Read(Span<byte>) hashes wrong data: when the buffer is larger than the data available, the entire buffer (including unread garbage bytes) is hashed instead of only the bytes actually read. Fixed by slicing to length.
  • Blake3Stream.ReadAsync(Memory<byte>) same issue: the async overload has the identical bug. Fixed the same way.
  • Hasher.UpdateWithJoin incorrect empty span handling: ReadOnlySpan<byte> == null is equivalent to checking IsEmpty, which causes UpdateWithJoin to throw ArgumentNullException on empty input. This is inconsistent with Update and UpdateWithJoin<T>, which accept empty spans without throwing. Removed the check.
  • Hasher.Finalize(long, Span<byte>) missing negative offset validation: a negative long is silently cast to a huge ulong. Added ArgumentOutOfRangeException.ThrowIfLessThan(offset, 0).

- use CryptographicOperations.FixedTimeEquals in Hash.Equals for constant-time comparison
- only hash bytes actually read in Blake3Stream.Read(Span<byte>)
- only hash bytes actually read in Blake3Stream.ReadAsync(Memory<byte>)
- remove dead null check on ReadOnlySpan in Hasher.UpdateWithJoin
- validate negative offset in Hasher.Finalize(long, Span<byte>)
@xoofx xoofx merged commit d27b9f5 into xoofx:master Mar 12, 2026
1 check passed
@xoofx
Copy link
Owner

xoofx commented Mar 12, 2026

Thank you!

@HMBSbige HMBSbige deleted the fix/dotnet-bugs branch March 13, 2026 02:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants