forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use SubtleCrypto API on browser DOM scenarios (dotnet#65966)
* Use SubtleCrypto API on browser DOM scenarios * Add sync over async implementation * Address misc feedback and make fixes * Address pinvoke errors * [Attempt] Correct execution of native digest API call at wasm layer * [Fix up] Correct execution of native digest API call at wasm layer * Update src/tests/BuildWasmApps/Wasm.Build.Tests/BuildTestBase.cs * Address feedback and clean up * Re-implement the crypto worker in ts * Address feedback * Revert "Re-implement the crypto worker in ts" This reverts commit 6a74390. * * moved stuff around and renamed it * initialization bit later * Add code to handle errors in worker (particularly on init) * Clean up * Add crypto dll to wasm native project * Add e2e test * Adjust test to reflect lack of SharedArrayBuffer for Chrome in test harness * Enable Chrome test and validate hashed value in tests * fix merge to track assert being renamed to mono_assert Co-authored-by: Eric StJohn <ericstj@microsoft.com> Co-authored-by: pavelsavara <pavel.savara@gmail.com> Co-authored-by: Ankit Jain <radical@gmail.com>
- Loading branch information
1 parent
c5f949e
commit bfbb783
Showing
27 changed files
with
715 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 | ||
{ | ||
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 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 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 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 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 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.