-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Enable Rfc2898DeriveBytes on Browser WASM #71768
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 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
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
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
79 changes: 79 additions & 0 deletions
79
...em.Security.Cryptography/src/System/Security/Cryptography/Pbkdf2Implementation.Browser.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,79 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using Internal.Cryptography; | ||
|
||
using SimpleDigest = Interop.BrowserCrypto.SimpleDigest; | ||
|
||
namespace System.Security.Cryptography | ||
{ | ||
internal static partial class Pbkdf2Implementation | ||
{ | ||
public static void Fill( | ||
ReadOnlySpan<byte> password, | ||
ReadOnlySpan<byte> salt, | ||
int iterations, | ||
HashAlgorithmName hashAlgorithmName, | ||
Span<byte> destination) | ||
{ | ||
Debug.Assert(!destination.IsEmpty); | ||
Debug.Assert(hashAlgorithmName.Name is not null); | ||
|
||
if (Interop.BrowserCrypto.CanUseSubtleCrypto) | ||
{ | ||
FillSubtleCrypto(password, salt, iterations, hashAlgorithmName, destination); | ||
} | ||
else | ||
{ | ||
FillManaged(password, salt, iterations, hashAlgorithmName, destination); | ||
} | ||
} | ||
|
||
private static unsafe void FillSubtleCrypto( | ||
ReadOnlySpan<byte> password, | ||
ReadOnlySpan<byte> salt, | ||
int iterations, | ||
HashAlgorithmName hashAlgorithmName, | ||
Span<byte> destination) | ||
{ | ||
(SimpleDigest hashName, _) = SHANativeHashProvider.HashAlgorithmToPal(hashAlgorithmName.Name!); | ||
|
||
fixed (byte* pPassword = password) | ||
fixed (byte* pSalt = salt) | ||
fixed (byte* pDestination = destination) | ||
{ | ||
int result = Interop.BrowserCrypto.DeriveBits( | ||
pPassword, password.Length, | ||
pSalt, salt.Length, | ||
iterations, | ||
hashName, | ||
pDestination, destination.Length); | ||
|
||
if (result != 0) | ||
{ | ||
throw new CryptographicException(SR.Format(SR.Unknown_SubtleCrypto_Error, result)); | ||
} | ||
} | ||
} | ||
|
||
private static void FillManaged( | ||
ReadOnlySpan<byte> password, | ||
ReadOnlySpan<byte> salt, | ||
int iterations, | ||
HashAlgorithmName hashAlgorithmName, | ||
Span<byte> destination) | ||
{ | ||
using (Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes( | ||
password.ToArray(), | ||
salt.ToArray(), | ||
iterations, | ||
hashAlgorithmName, | ||
clearPassword: true)) | ||
{ | ||
byte[] result = deriveBytes.GetBytes(destination.Length); | ||
result.AsSpan().CopyTo(destination); | ||
} | ||
} | ||
} | ||
} |
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.
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.
There's no way to stay in the span universe all the way down? Are we accepting arrays from public entrypoints, passing them around as span, and then ToArray'ing here?
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.
It's possible but we've avoided optimizing the non-one-shot implementation of PBKDF2 when we've talked about it in the past.
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.
Note that this is the way the existing "mobile" platforms are implementing this functionality:
runtime/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/Pbkdf2Implementation.Managed.cs
Lines 27 to 36 in 6a02d5d
which is used by Android and non-macOS AppleCrypto (iOS, tvOS, etc.).
Not without some decent refactoring to
Rfc2898DeriveBytes
in order to make the managed algorithm completely "one shot", and then build the instance basedRfc2898DeriveBytes
methods on top of it.In one overload, yes, and in most overloads no. This method is called from these public end-points:
runtime/src/libraries/System.Security.Cryptography/ref/System.Security.Cryptography.cs
Lines 1734 to 1739 in 6a02d5d
Only the top overload is taking 2
byte[]
s, passing them through as spans, and then ToArray'ing here. The bottom overload takes a string, which turns into UTF8 bytes in either a rented array or stackalloc'd span, and then passed in here.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.
I think it would be simpler to implement a one shot in managed code with IncrementalHash instead of trying to get the streaming PBKDF2 to allocate less. I have a branch with this. Happy to upstream it as a follow up to this.
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.
I think that would be great!