Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[browser][wasm] Async Hashing and Hashed Message Authentication #43942

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
74ac0ac
[browser][crypto] Initial commit of async crypto AP| surface.
kjpou1 Oct 20, 2020
610444c
Update Algorithms project to include the Interop PAL interface
kjpou1 Oct 20, 2020
166c949
Merge branch 'master' of https://github.com/dotnet/runtime into wasm-…
kjpou1 Oct 21, 2020
0d830ed
Replace PAL interop interface implementation in favor of using the Ja…
kjpou1 Oct 21, 2020
10df2e2
Merge branch 'master' of https://github.com/dotnet/runtime into wasm-…
kjpou1 Oct 22, 2020
43b2e5b
Use async calls for stream hashing when used on browser.
kjpou1 Oct 22, 2020
21bb360
MD5 Not supported by WebCrypto digest API
kjpou1 Oct 22, 2020
0fec2ff
Small cleanup
kjpou1 Oct 22, 2020
03ae22a
Merge branch 'master' of https://github.com/dotnet/runtime into wasm-…
kjpou1 Oct 23, 2020
47ce15b
Initial Support for Authenticated Hashing
kjpou1 Oct 23, 2020
7aacdcc
Merge branch 'master' of https://github.com/dotnet/runtime into wasm-…
kjpou1 Oct 26, 2020
0740c94
Merge branch 'master' of https://github.com/dotnet/runtime into wasm-…
kjpou1 Oct 27, 2020
f10d742
Add support for Authenticated Hashing algorithms HMACSHA256 HMACSHA38…
kjpou1 Oct 27, 2020
60bad46
small cleanup
kjpou1 Oct 27, 2020
05301ca
Add support for ComputeHashAsync for array, start and count.
kjpou1 Oct 27, 2020
d9d9d0b
Merge branch 'master' of https://github.com/dotnet/runtime into wasm-…
kjpou1 Oct 28, 2020
4981dc0
On AnyOS use the non async methods as original code did.
kjpou1 Oct 28, 2020
37dd2c7
Merge branch 'master' of https://github.com/dotnet/runtime into wasm-…
kjpou1 Nov 9, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/libraries/Common/src/Internal/Cryptography/HashProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace Internal.Cryptography
{
Expand Down Expand Up @@ -33,9 +35,11 @@ public void AppendHashData(byte[] data, int offset, int count)

public abstract void AppendHashData(ReadOnlySpan<byte> data);

public abstract Task AppendHashDataAsync(byte[] array, int ibStart, int cbSize, CancellationToken cancellationToken);

// Compute the hash based on the appended data and resets the HashProvider for more hashing.
public abstract int FinalizeHashAndReset(Span<byte> destination);

public abstract Task<int> FinalizeHashAndResetAsync(byte[] destination, CancellationToken cancellationToken);
public abstract int GetCurrentHash(Span<byte> destination);

public byte[] FinalizeHashAndReset()
Expand All @@ -48,6 +52,16 @@ public byte[] FinalizeHashAndReset()
return ret;
}

public async Task<byte[]> FinalizeHashAndResetAsync(CancellationToken cancellationToken)
{
byte[] ret = new byte[HashSizeInBytes];

int written = await FinalizeHashAndResetAsync(ret, cancellationToken).ConfigureAwait(false);
Debug.Assert(written == HashSizeInBytes);

return ret;
}

public bool TryFinalizeHashAndReset(Span<byte> destination, out int bytesWritten)
{
if (destination.Length < HashSizeInBytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using NTSTATUS = Interop.BCrypt.NTSTATUS;
using BCryptOpenAlgorithmProviderFlags = Interop.BCrypt.BCryptOpenAlgorithmProviderFlags;
using BCryptCreateHashFlags = Interop.BCrypt.BCryptCreateHashFlags;
using System.Threading;
using System.Threading.Tasks;

namespace Internal.Cryptography
{
Expand Down Expand Up @@ -68,7 +70,7 @@ public sealed override unsafe void AppendHashData(ReadOnlySpan<byte> source)
throw Interop.BCrypt.CreateCryptographicException(ntStatus);
}
}

public override Task AppendHashDataAsync(byte[] array, int ibStart, int cbSize, CancellationToken cancellationToken) => throw new NotImplementedException();
public override int FinalizeHashAndReset(Span<byte> destination)
{
Debug.Assert(destination.Length >= _hashSize);
Expand All @@ -84,6 +86,8 @@ public override int FinalizeHashAndReset(Span<byte> destination)
return _hashSize;
}

public override Task<int> FinalizeHashAndResetAsync(byte[] destination, CancellationToken cancellationToken) => throw new NotImplementedException();

public override int GetCurrentHash(Span<byte> destination)
{
Debug.Assert(destination.Length >= _hashSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,12 @@ public HMACMD5(byte[] key) { }
protected override void Dispose(bool disposing) { }
protected override void HashCore(byte[] rgb, int ib, int cb) { }
protected override void HashCore(System.ReadOnlySpan<byte> source) { }
protected override System.Threading.Tasks.Task HashCoreAsync(byte[] rgb, int ib, int cb, System.Threading.CancellationToken cancellationToken) { throw null; }
protected override byte[] HashFinal() { throw null; }
protected override System.Threading.Tasks.Task<byte[]> HashFinalAsync(System.Threading.CancellationToken cancellationToken) { throw null; }
public override void Initialize() { }
protected override bool TryHashFinal(System.Span<byte> destination, out int bytesWritten) { throw null; }
}
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public partial class HMACSHA1 : System.Security.Cryptography.HMAC
{
public HMACSHA1() { }
Expand All @@ -442,11 +443,12 @@ public HMACSHA1(byte[] key, bool useManagedSha1) { }
protected override void Dispose(bool disposing) { }
protected override void HashCore(byte[] rgb, int ib, int cb) { }
protected override void HashCore(System.ReadOnlySpan<byte> source) { }
protected override System.Threading.Tasks.Task HashCoreAsync(byte[] rgb, int ib, int cb, System.Threading.CancellationToken cancellationToken) { throw null; }
protected override byte[] HashFinal() { throw null; }
protected override System.Threading.Tasks.Task<byte[]> HashFinalAsync(System.Threading.CancellationToken cancellationToken) { throw null; }
public override void Initialize() { }
protected override bool TryHashFinal(System.Span<byte> destination, out int bytesWritten) { throw null; }
}
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public partial class HMACSHA256 : System.Security.Cryptography.HMAC
{
public HMACSHA256() { }
Expand All @@ -455,11 +457,12 @@ public HMACSHA256(byte[] key) { }
protected override void Dispose(bool disposing) { }
protected override void HashCore(byte[] rgb, int ib, int cb) { }
protected override void HashCore(System.ReadOnlySpan<byte> source) { }
protected override System.Threading.Tasks.Task HashCoreAsync(byte[] rgb, int ib, int cb, System.Threading.CancellationToken cancellationToken) { throw null; }
protected override byte[] HashFinal() { throw null; }
protected override System.Threading.Tasks.Task<byte[]> HashFinalAsync(System.Threading.CancellationToken cancellationToken) { throw null; }
public override void Initialize() { }
protected override bool TryHashFinal(System.Span<byte> destination, out int bytesWritten) { throw null; }
}
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public partial class HMACSHA384 : System.Security.Cryptography.HMAC
{
public HMACSHA384() { }
Expand All @@ -469,11 +472,12 @@ public HMACSHA384(byte[] key) { }
protected override void Dispose(bool disposing) { }
protected override void HashCore(byte[] rgb, int ib, int cb) { }
protected override void HashCore(System.ReadOnlySpan<byte> source) { }
protected override System.Threading.Tasks.Task HashCoreAsync(byte[] rgb, int ib, int cb, System.Threading.CancellationToken cancellationToken) { throw null; }
protected override byte[] HashFinal() { throw null; }
protected override System.Threading.Tasks.Task<byte[]> HashFinalAsync(System.Threading.CancellationToken cancellationToken) { throw null; }
public override void Initialize() { }
protected override bool TryHashFinal(System.Span<byte> destination, out int bytesWritten) { throw null; }
}
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public partial class HMACSHA512 : System.Security.Cryptography.HMAC
{
public HMACSHA512() { }
Expand All @@ -483,7 +487,9 @@ public HMACSHA512(byte[] key) { }
protected override void Dispose(bool disposing) { }
protected override void HashCore(byte[] rgb, int ib, int cb) { }
protected override void HashCore(System.ReadOnlySpan<byte> source) { }
protected override System.Threading.Tasks.Task HashCoreAsync(byte[] rgb, int ib, int cb, System.Threading.CancellationToken cancellationToken) { throw null; }
protected override byte[] HashFinal() { throw null; }
protected override System.Threading.Tasks.Task<byte[]> HashFinalAsync(System.Threading.CancellationToken cancellationToken) { throw null; }
public override void Initialize() { }
protected override bool TryHashFinal(System.Span<byte> destination, out int bytesWritten) { throw null; }
}
Expand Down Expand Up @@ -781,9 +787,12 @@ protected SHA1() { }
public static new System.Security.Cryptography.SHA1 Create() { throw null; }
public static new System.Security.Cryptography.SHA1? Create(string hashName) { throw null; }
public static byte[] HashData(byte[] source) { throw null; }
public static System.Threading.Tasks.Task<byte[]> HashDataAsync(byte[] source, System.Threading.CancellationToken cancellationToken = default) { throw null; }
public static System.Threading.Tasks.Task<int> HashDataAsync(byte[] source, byte[] destination, System.Threading.CancellationToken cancellationToken = default) { throw null; }
public static byte[] HashData(System.ReadOnlySpan<byte> source) { throw null; }
public static int HashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination) { throw null; }
public static bool TryHashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination, out int bytesWritten) { throw null; }
public static System.Threading.Tasks.Task<(bool IsSuccess, int BytesWritten)> TryHashDataAsync(byte[] source, byte[] destination, System.Threading.CancellationToken cancellationToken = default) { throw null; }
}
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public sealed partial class SHA1Managed : System.Security.Cryptography.SHA1
Expand All @@ -792,7 +801,9 @@ public SHA1Managed() { }
protected sealed override void Dispose(bool disposing) { }
protected sealed override void HashCore(byte[] array, int ibStart, int cbSize) { }
protected sealed override void HashCore(System.ReadOnlySpan<byte> source) { }
protected sealed override System.Threading.Tasks.Task HashCoreAsync(byte[] array, int ibStart, int cbSize, System.Threading.CancellationToken cancellationToken) { throw null; }
protected sealed override byte[] HashFinal() { throw null; }
protected sealed override System.Threading.Tasks.Task<byte[]> HashFinalAsync(System.Threading.CancellationToken cancellationToken) { throw null; }
public sealed override void Initialize() { }
protected sealed override bool TryHashFinal(System.Span<byte> destination, out int bytesWritten) { throw null; }
}
Expand All @@ -802,9 +813,12 @@ protected SHA256() { }
public static new System.Security.Cryptography.SHA256 Create() { throw null; }
public static new System.Security.Cryptography.SHA256? Create(string hashName) { throw null; }
public static byte[] HashData(byte[] source) { throw null; }
public static System.Threading.Tasks.Task<byte[]> HashDataAsync(byte[] source, System.Threading.CancellationToken cancellationToken = default) { throw null; }
public static System.Threading.Tasks.Task<int> HashDataAsync(byte[] source, byte[] destination, System.Threading.CancellationToken cancellationToken = default) { throw null; }
public static byte[] HashData(System.ReadOnlySpan<byte> source) { throw null; }
public static int HashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination) { throw null; }
public static bool TryHashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination, out int bytesWritten) { throw null; }
public static System.Threading.Tasks.Task<(bool IsSuccess, int BytesWritten)> TryHashDataAsync(byte[] source, byte[] destination, System.Threading.CancellationToken cancellationToken = default) { throw null; }
}
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public sealed partial class SHA256Managed : System.Security.Cryptography.SHA256
Expand All @@ -813,7 +827,9 @@ public SHA256Managed() { }
protected sealed override void Dispose(bool disposing) { }
protected sealed override void HashCore(byte[] array, int ibStart, int cbSize) { }
protected sealed override void HashCore(System.ReadOnlySpan<byte> source) { }
protected sealed override System.Threading.Tasks.Task HashCoreAsync(byte[] array, int ibStart, int cbSize, System.Threading.CancellationToken cancellationToken) { throw null; }
protected sealed override byte[] HashFinal() { throw null; }
protected sealed override System.Threading.Tasks.Task<byte[]> HashFinalAsync(System.Threading.CancellationToken cancellationToken) { throw null; }
public sealed override void Initialize() { }
protected sealed override bool TryHashFinal(System.Span<byte> destination, out int bytesWritten) { throw null; }
}
Expand All @@ -823,9 +839,13 @@ protected SHA384() { }
public static new System.Security.Cryptography.SHA384 Create() { throw null; }
public static new System.Security.Cryptography.SHA384? Create(string hashName) { throw null; }
public static byte[] HashData(byte[] source) { throw null; }
public static System.Threading.Tasks.Task<byte[]> HashDataAsync(byte[] source, System.Threading.CancellationToken cancellationToken = default) { throw null; }
public static System.Threading.Tasks.Task<int> HashDataAsync(byte[] source, byte[] destination, System.Threading.CancellationToken cancellationToken = default) { throw null; }
public static byte[] HashData(System.ReadOnlySpan<byte> source) { throw null; }
public static int HashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination) { throw null; }
public static bool TryHashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination, out int bytesWritten) { throw null; }
public static System.Threading.Tasks.Task<(bool IsSuccess, int BytesWritten)> TryHashDataAsync(byte[] source, byte[] destination, System.Threading.CancellationToken cancellationToken = default) { throw null; }

}
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public sealed partial class SHA384Managed : System.Security.Cryptography.SHA384
Expand All @@ -834,7 +854,9 @@ public SHA384Managed() { }
protected sealed override void Dispose(bool disposing) { }
protected sealed override void HashCore(byte[] array, int ibStart, int cbSize) { }
protected sealed override void HashCore(System.ReadOnlySpan<byte> source) { }
protected sealed override System.Threading.Tasks.Task HashCoreAsync(byte[] array, int ibStart, int cbSize, System.Threading.CancellationToken cancellationToken) { throw null; }
protected sealed override byte[] HashFinal() { throw null; }
protected sealed override System.Threading.Tasks.Task<byte[]> HashFinalAsync(System.Threading.CancellationToken cancellationToken) { throw null; }
public sealed override void Initialize() { }
protected sealed override bool TryHashFinal(System.Span<byte> destination, out int bytesWritten) { throw null; }
}
Expand All @@ -844,9 +866,13 @@ protected SHA512() { }
public static new System.Security.Cryptography.SHA512 Create() { throw null; }
public static new System.Security.Cryptography.SHA512? Create(string hashName) { throw null; }
public static byte[] HashData(byte[] source) { throw null; }
public static System.Threading.Tasks.Task<byte[]> HashDataAsync(byte[] source, System.Threading.CancellationToken cancellationToken = default) { throw null; }
public static System.Threading.Tasks.Task<int> HashDataAsync(byte[] source, byte[] destination, System.Threading.CancellationToken cancellationToken = default) { throw null; }
public static byte[] HashData(System.ReadOnlySpan<byte> source) { throw null; }
public static int HashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination) { throw null; }
public static bool TryHashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination, out int bytesWritten) { throw null; }
public static System.Threading.Tasks.Task<(bool IsSuccess, int BytesWritten)> TryHashDataAsync(byte[] source, byte[] destination, System.Threading.CancellationToken cancellationToken = default) { throw null; }

}
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public sealed partial class SHA512Managed : System.Security.Cryptography.SHA512
Expand All @@ -855,7 +881,9 @@ public SHA512Managed() { }
protected sealed override void Dispose(bool disposing) { }
protected sealed override void HashCore(byte[] array, int ibStart, int cbSize) { }
protected sealed override void HashCore(System.ReadOnlySpan<byte> source) { }
protected sealed override System.Threading.Tasks.Task HashCoreAsync(byte[] array, int ibStart, int cbSize, System.Threading.CancellationToken cancellationToken) { throw null; }
protected sealed override byte[] HashFinal() { throw null; }
protected sealed override System.Threading.Tasks.Task<byte[]> HashFinalAsync(System.Threading.CancellationToken cancellationToken) { throw null; }
public sealed override void Initialize() { }
protected sealed override bool TryHashFinal(System.Span<byte> destination, out int bytesWritten) { throw null; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ T:System.Security.Cryptography.SHA512
T:System.Security.Cryptography.SHA1Managed
T:System.Security.Cryptography.SHA256Managed
T:System.Security.Cryptography.SHA384Managed
T:System.Security.Cryptography.SHA512Managed
T:System.Security.Cryptography.SHA512Managed
T:System.Security.Cryptography.HMACSHA1
T:System.Security.Cryptography.HMACSHA256
T:System.Security.Cryptography.HMACSHA384
T:System.Security.Cryptography.HMACSHA512
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;

namespace Internal.Cryptography
{
Expand Down Expand Up @@ -81,6 +83,9 @@ public void ChangeKey(byte[] key)
public void AppendHashData(byte[] data, int offset, int count) =>
_hMacProvider.AppendHashData(data, offset, count);

public Task AppendHashDataAsync(byte[] data, int offset, int count, CancellationToken cancellationToken) =>
_hMacProvider.AppendHashDataAsync(data, offset, count, cancellationToken);

public void AppendHashData(ReadOnlySpan<byte> source) =>
_hMacProvider.AppendHashData(source);

Expand All @@ -91,6 +96,12 @@ public byte[] FinalizeHashAndReset() =>
public int FinalizeHashAndReset(Span<byte> destination) =>
_hMacProvider.FinalizeHashAndReset(destination);

public Task<byte[]> FinalizeHashAndResetAsync(CancellationToken cancellationToken) =>
_hMacProvider.FinalizeHashAndResetAsync(cancellationToken);

public Task<int> FinalizeHashAndResetAsync(byte[] destination, CancellationToken cancellationToken) =>
_hMacProvider.FinalizeHashAndResetAsync(destination, cancellationToken);

public bool TryFinalizeHashAndReset(Span<byte> destination, out int bytesWritten) =>
_hMacProvider.TryFinalizeHashAndReset(destination, out bytesWritten);

Expand Down
Loading