-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Use SubtleCrypto API on browser DOM scenarios #65966
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
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
6035e71
Use SubtleCrypto API on browser DOM scenarios
layomia 2f99382
Add sync over async implementation
layomia f7e68f9
Address misc feedback and make fixes
layomia f1653c5
Address pinvoke errors
layomia d1da46a
[Attempt] Correct execution of native digest API call at wasm layer
layomia c23607f
[Fix up] Correct execution of native digest API call at wasm layer
layomia 9cf4875
Update src/tests/BuildWasmApps/Wasm.Build.Tests/BuildTestBase.cs
ericstj 807a566
Address feedback and clean up
layomia 6a74390
Re-implement the crypto worker in ts
layomia 0f85951
Address feedback
layomia 72e47b8
Revert "Re-implement the crypto worker in ts"
layomia cf7cf37
* moved stuff around and renamed it
pavelsavara 3f14a3a
Add code to handle errors in worker (particularly on init)
layomia 0f11bca
Clean up
layomia 7d964be
Add crypto dll to wasm native project
layomia 797220e
Merge remote-tracking branch 'upstream/main' into WasmCrypto
layomia c7901c2
Add e2e test
layomia e7f89f5
Adjust test to reflect lack of SharedArrayBuffer for Chrome in test h…
layomia a22037b
Enable Chrome test and validate hashed value in tests
layomia 3b71f3e
Merge remote-tracking branch 'upstream/main' into WasmCrypto
layomia 7b1a026
Merge branch 'main' into WasmCrypto
ericstj c2ec92f
fix merge to track assert being renamed to mono_assert
radical File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/libraries/Common/src/Interop/Browser/Interop.Libraries.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| internal static partial class Interop | ||
| { | ||
| internal static partial class Libraries | ||
| { | ||
| // Shims | ||
| internal const string SystemNative = "libSystem.Native"; | ||
| internal const string CryptoNative = "libSystem.Security.Cryptography.Native.Browser"; | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
...c/Interop/Browser/System.Security.Cryptography.Native.Browser/Interop.SimpleDigestHash.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Diagnostics; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| internal static partial class Interop | ||
| { | ||
| internal static partial class BrowserCrypto | ||
| { | ||
| // These values are also defined in the pal_crypto_webworker header file, and utilized in the dotnet-crypto-worker in the wasm runtime. | ||
| internal enum SimpleDigest | ||
| { | ||
| Sha1, | ||
| Sha256, | ||
| Sha384, | ||
| Sha512, | ||
| }; | ||
|
|
||
| [LibraryImport(Libraries.CryptoNative, EntryPoint = "SystemCryptoNativeBrowser_CanUseSimpleDigestHash")] | ||
| internal static partial int CanUseSimpleDigestHash(); | ||
|
|
||
| [LibraryImport(Libraries.CryptoNative, EntryPoint = "SystemCryptoNativeBrowser_SimpleDigestHash")] | ||
| internal static unsafe partial int SimpleDigestHash( | ||
| SimpleDigest hash, | ||
| byte* input_buffer, | ||
| int input_len, | ||
| byte* output_buffer, | ||
| int output_len); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
....Security.Cryptography/src/System/Security/Cryptography/SHAHashProvider.Browser.Native.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Diagnostics; | ||
| using System.Security.Cryptography; | ||
|
|
||
| using SimpleDigest = Interop.BrowserCrypto.SimpleDigest; | ||
|
|
||
| namespace Internal.Cryptography | ||
layomia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| internal sealed class SHANativeHashProvider : HashProvider | ||
| { | ||
| private readonly int _hashSizeInBytes; | ||
| private readonly SimpleDigest _impl; | ||
| private MemoryStream? _buffer; | ||
|
|
||
| public SHANativeHashProvider(string hashAlgorithmId) | ||
| { | ||
| Debug.Assert(HashProviderDispenser.CanUseSubtleCryptoImpl); | ||
|
|
||
| switch (hashAlgorithmId) | ||
| { | ||
| case HashAlgorithmNames.SHA1: | ||
| _impl = SimpleDigest.Sha1; | ||
| _hashSizeInBytes = 20; | ||
| break; | ||
| case HashAlgorithmNames.SHA256: | ||
| _impl = SimpleDigest.Sha256; | ||
| _hashSizeInBytes = 32; | ||
| break; | ||
| case HashAlgorithmNames.SHA384: | ||
| _impl = SimpleDigest.Sha384; | ||
| _hashSizeInBytes = 48; | ||
| break; | ||
| case HashAlgorithmNames.SHA512: | ||
| _impl = SimpleDigest.Sha512; | ||
| _hashSizeInBytes = 64; | ||
| break; | ||
| default: | ||
| throw new CryptographicException(SR.Format(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithmId)); | ||
| } | ||
| } | ||
|
|
||
| public override void AppendHashData(ReadOnlySpan<byte> data) | ||
| { | ||
| _buffer ??= new MemoryStream(1000); | ||
| _buffer.Write(data); | ||
| } | ||
|
|
||
| public override int FinalizeHashAndReset(Span<byte> destination) | ||
| { | ||
| GetCurrentHash(destination); | ||
| _buffer = null; | ||
|
|
||
| return _hashSizeInBytes; | ||
| } | ||
|
|
||
| public override int GetCurrentHash(Span<byte> destination) | ||
| { | ||
| Debug.Assert(destination.Length >= _hashSizeInBytes); | ||
|
|
||
| byte[] srcArray = Array.Empty<byte>(); | ||
| int srcLength = 0; | ||
| if (_buffer != null) | ||
| { | ||
| srcArray = _buffer.GetBuffer(); | ||
| srcLength = (int)_buffer.Length; | ||
| } | ||
|
|
||
| unsafe | ||
| { | ||
| fixed (byte* src = srcArray) | ||
| fixed (byte* dest = destination) | ||
| { | ||
| int res = Interop.BrowserCrypto.SimpleDigestHash(_impl, src, srcLength, dest, destination.Length); | ||
| Debug.Assert(res != 0); | ||
| } | ||
| } | ||
|
|
||
| return _hashSizeInBytes; | ||
| } | ||
|
|
||
| public override int HashSizeInBytes => _hashSizeInBytes; | ||
|
|
||
| public override void Dispose(bool disposing) | ||
| { | ||
| } | ||
|
|
||
| public override void Reset() | ||
| { | ||
| _buffer = null; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.