-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable Rfc2898DeriveBytes on Browser WASM (#71768)
* Enable Rfc2898DeriveBytes on Browser WASM Marks the APIs as supported on Browser, and enables Rfc2898 tests on Browser WASM. Use SubtleCrypto deriveBits API to implement one shot Pbkdf2. * Mark HKDF as supported on Browser and enable tests Contributes to #40074
- Loading branch information
Showing
24 changed files
with
250 additions
and
119 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
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.