-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
perf: improve ManagedAuthenticatedEncryptor Decrypt() and Encrypt() flow #59424
Open
DeagleGross
wants to merge
18
commits into
dotnet:main
Choose a base branch
from
DeagleGross:dmkorolev/dataprotection/lin-perf-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+692
−145
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
e47b41d
a bit of improvement
DeagleGross bb6d3c0
another buffer.blockCopy removal
DeagleGross 384c3d5
use `DecryptCbc` instead?
DeagleGross 90bf754
tests and remove another array
DeagleGross a37b0d5
fix the slicing of IV
DeagleGross 268ee44
slice correctly!
DeagleGross eb6cb1b
try with encrypt
DeagleGross d090882
finish encrypt
DeagleGross 6ec76d3
use same overload in decrypt()
DeagleGross 49eab48
dont allocate for prf-output as well
DeagleGross 4ba9cc2
address PR comments
DeagleGross 919d003
remove spaces
DeagleGross 87d118b
prfInput and prfOutput rented
DeagleGross 2524a15
correctHash as rented
DeagleGross ec70b6e
more details
DeagleGross 0d30a5b
use static `TryHashData` for specific hash implementation
DeagleGross bab196a
prettify
DeagleGross e96ad0c
dont rent (only stackalloc or allocate new byte array) + address othe…
DeagleGross 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
23 changes: 23 additions & 0 deletions
23
src/DataProtection/DataProtection/src/Internal/DataProtectionPool.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,23 @@ | ||
// 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.Buffers; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.AspNetCore.DataProtection.Internal; | ||
|
||
/// <summary> | ||
/// Used for pooling secret data (e.g. Protect()/Unprotect() flow). | ||
/// Main goal is not to intersect with the <see cref="ArrayPool{T}.Shared"/> | ||
/// </summary> | ||
internal static class DataProtectionPool | ||
{ | ||
private static readonly ArrayPool<byte> _pool = ArrayPool<byte>.Create(); | ||
|
||
public static byte[] Rent(int length) => _pool.Rent(length); | ||
public static void Return(byte[] array, bool clearArray = false) => _pool.Return(array, clearArray); | ||
} |
6 changes: 6 additions & 0 deletions
6
src/DataProtection/DataProtection/src/Managed/IManagedGenRandom.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 |
---|---|---|
@@ -1,9 +1,15 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
|
||
namespace Microsoft.AspNetCore.DataProtection.Managed; | ||
|
||
internal interface IManagedGenRandom | ||
{ | ||
byte[] GenRandom(int numBytes); | ||
|
||
#if NET10_0_OR_GREATER | ||
void GenRandom(Span<byte> target); | ||
#endif | ||
} |
537 changes: 392 additions & 145 deletions
537
src/DataProtection/DataProtection/src/Managed/ManagedAuthenticatedEncryptor.cs
Large diffs are not rendered by default.
Oops, something went wrong.
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
28 changes: 28 additions & 0 deletions
28
src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/E2ETests.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,28 @@ | ||
// 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.Collections.Generic; | ||
using System.Text; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.AspNetCore.DataProtection.Tests; | ||
public class E2ETests | ||
{ | ||
[Fact] | ||
public void ProtectAndUnprotect_ForSampleAntiforgeryToken() | ||
{ | ||
const string sampleToken = "CfDJ8H5oH_fp1QNBmvs-OWXxsVoV30hrXeI4-PI4p1VZytjsgd0DTstMdtTZbFtm2dKHvsBlDCv7TiEWKztZf8fb48pUgBgUE2SeYV3eOUXvSfNWU0D8SmHLy5KEnwKKkZKqudDhCnjQSIU7mhDliJJN1e4"; | ||
|
||
var dataProtector = GetServiceCollectionBuiltDataProtector(); | ||
var encrypted = dataProtector.Protect(sampleToken); | ||
var decrypted = dataProtector.Unprotect(encrypted); | ||
Assert.Equal(sampleToken, decrypted); | ||
} | ||
|
||
private static IDataProtector GetServiceCollectionBuiltDataProtector(string purpose = "samplePurpose") | ||
=> new ServiceCollection() | ||
.AddDataProtection() | ||
.Services.BuildServiceProvider() | ||
.GetDataProtector(purpose); | ||
} |
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.
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.
.NET already has SP800108 in the box. We should probably just use the one that is built-in to .NET if it is available.
https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.sp800108hmaccounterkdf