-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Use crypto.subtle for HMAC on Browser WASM #70745
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
3 commits
Select commit
Hold shift + click to select a range
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
22 changes: 22 additions & 0 deletions
22
...es/Common/src/Interop/Browser/System.Security.Cryptography.Native.Browser/Interop.Sign.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,22 @@ | ||
| // 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 | ||
| { | ||
| [LibraryImport(Libraries.CryptoNative, EntryPoint = "SystemCryptoNativeBrowser_Sign")] | ||
| internal static unsafe partial int Sign( | ||
| SimpleDigest hashAlgorithm, | ||
| byte* key_buffer, | ||
| int key_len, | ||
| 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
99 changes: 99 additions & 0 deletions
99
...Security.Cryptography/src/System/Security/Cryptography/HMACHashProvider.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,99 @@ | ||
| // 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 System.Security.Cryptography | ||
| { | ||
| internal sealed class HMACNativeHashProvider : HashProvider | ||
| { | ||
| private readonly int _hashSizeInBytes; | ||
| private readonly SimpleDigest _hashAlgorithm; | ||
| private readonly byte[] _key; | ||
| private MemoryStream? _buffer; | ||
|
|
||
| public HMACNativeHashProvider(string hashAlgorithmId, ReadOnlySpan<byte> key) | ||
| { | ||
| Debug.Assert(HashProviderDispenser.CanUseSubtleCryptoImpl); | ||
|
|
||
| (_hashAlgorithm, _hashSizeInBytes) = SHANativeHashProvider.HashAlgorithmToPal(hashAlgorithmId); | ||
| _key = key.ToArray(); | ||
| } | ||
|
|
||
| public override void AppendHashData(ReadOnlySpan<byte> data) | ||
| { | ||
| _buffer ??= new MemoryStream(1000); | ||
| _buffer.Write(data); | ||
| } | ||
|
|
||
| public override int FinalizeHashAndReset(Span<byte> destination) | ||
| { | ||
| int written = GetCurrentHash(destination); | ||
| _buffer = null; | ||
|
|
||
| return written; | ||
| } | ||
|
|
||
| public override int GetCurrentHash(Span<byte> destination) | ||
| { | ||
| Debug.Assert(destination.Length >= _hashSizeInBytes); | ||
eerhardt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| byte[] srcArray = Array.Empty<byte>(); | ||
| int srcLength = 0; | ||
| if (_buffer != null) | ||
| { | ||
| srcArray = _buffer.GetBuffer(); | ||
| srcLength = (int)_buffer.Length; | ||
| } | ||
|
|
||
| unsafe | ||
| { | ||
| fixed (byte* key = _key) | ||
| fixed (byte* src = srcArray) | ||
| fixed (byte* dest = destination) | ||
| { | ||
| int res = Interop.BrowserCrypto.Sign(_hashAlgorithm, key, _key.Length, src, srcLength, dest, destination.Length); | ||
| Debug.Assert(res != 0); | ||
| } | ||
| } | ||
|
|
||
| return _hashSizeInBytes; | ||
| } | ||
|
|
||
| public static unsafe int MacDataOneShot(string hashAlgorithmId, ReadOnlySpan<byte> key, ReadOnlySpan<byte> data, Span<byte> destination) | ||
| { | ||
| (SimpleDigest hashName, int hashSizeInBytes) = SHANativeHashProvider.HashAlgorithmToPal(hashAlgorithmId); | ||
| Debug.Assert(destination.Length >= hashSizeInBytes); | ||
|
|
||
| fixed (byte* k = key) | ||
| fixed (byte* src = data) | ||
| fixed (byte* dest = destination) | ||
| { | ||
| int res = Interop.BrowserCrypto.Sign(hashName, k, key.Length, src, data.Length, dest, destination.Length); | ||
| Debug.Assert(res != 0); | ||
eerhardt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return hashSizeInBytes; | ||
| } | ||
|
|
||
| public override int HashSizeInBytes => _hashSizeInBytes; | ||
|
|
||
| public override void Dispose(bool disposing) | ||
| { | ||
| if (disposing) | ||
| { | ||
| CryptographicOperations.ZeroMemory(_key); | ||
| } | ||
| } | ||
|
|
||
| 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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this buffering all of the data that's used to compute the hash?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. There is no other way with SubtleCrypto, currently. The browser-provided implementations are one-shots. You need to give it all of the data up-front. See w3c/webcrypto#73
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like it could be a real pit of failure, especially if we may sometimes use SubtleCrypto and sometimes use the managed implementation based on configuration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A performance pit of feature because of memory usage? Or something else?
From my understanding, it is based on the browser's capabilities and even the settings/headers of the page it is hosted in. See https://developer.chrome.com/blog/enabling-shared-array-buffer/ for when we can use SharedArrayBuffer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, note that this is how the SHA algos are implemented as well in #65966.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Memory, and then any knock-on effects from that, e.g. exceptions that result from the memory pressure (do the wasm environments enforce any kind of limits on working set size)? I'm just imagining someone downloading a gig of data to hash, expecting it to just stream through, but instead having the whole thing be buffered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WASM is 32-bit, currently, so you have 4 GB of address space to work with, at most. In practice, v8 (Chromium)'s limit is more like 2 GB: https://v8.dev/blog/4gb-wasm-memory. The post is a few years old but I am not aware of that needle moving since then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reality is even more complicated WebAssembly/design#1397 and because of that it can definitely make sense to do the buffering on the js side rather than in managed but I expect most of the uses on this would look more like https://en.wikipedia.org/wiki/JSON_Web_Token than a large binary