Repository navigation
Reduce allocations in managed DataProtection protect/unprotect - #70146
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block approval of the Data Protection allocation-reduction changes.
No open issues 🎉
What changed in this PR
This PR reduces allocations in managed Data Protection protect/unprotect operations while preserving the encrypted payload format.
Changes:
- Reuses one symmetric algorithm instance per encryption and writes directly into the final output array.
- Raises the key-derivation stack buffer threshold to avoid heap allocations for common purpose chains.
- Adds compatibility tests and benchmarks for short and cookie-authentication purpose chains.
| File | Description |
|---|---|
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/SP800_108/SP800_108Tests.cs | Compares key derivation against a reference implementation across buffer thresholds. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/Managed/ManagedAuthenticatedEncryptorTests.cs | Adds known-answer and round-trip coverage with pre- and post-buffers. |
| src/DataProtection/DataProtection/src/SP800_108/ManagedSP800_108_CTR_HMACSHA512.cs | Increases the stack buffer threshold from 128 to 512 bytes. |
| src/DataProtection/DataProtection/src/Managed/ManagedAuthenticatedEncryptor.cs | Reuses the algorithm instance and supports direct encryption into the final array. |
| src/DataProtection/benchmarks/Microsoft.AspNetCore.DataProtection.MicroBenchmarks/Benchmarks/KeyRingProtectorBenchmarks.cs | Adds protect/unprotect allocation benchmarks for representative purpose chains. |
Review effort: Balanced
|
/azp run aspnetcore-ci Rerunning: the only failure is BlazorWebTemplate_ConditionalPasskeyRegistration on Windows (known flaky, 6.35% failure rate per Build Analysis, tracked in #69494). It is unrelated to this change, which only touches the managed (non-CNG) encryptor. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
No pipelines are associated with this pull request. |
|
/azp run aspnetcore-ci 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
No pipelines are associated with this pull request. |
36942b2 to
ec9ec16
Compare
- ManagedAuthenticatedEncryptor: use a single SymmetricAlgorithm instance for sizing and encryption (previously up to 3 per Protect, one never disposed), and implement IOptimizedAuthenticatedEncryptor so the payload is encrypted directly into the final array including the key ring header pre-buffer. - ManagedSP800_108_CTR_HMACSHA512: raise PRF input stackalloc threshold from 128 to 512 bytes. AAD (app discriminator + purposes) routinely pushes the input above 128 bytes, causing a heap allocation per Protect/Unprotect. - Add KeyRingProtectorBenchmarks and tests. Co-authored-by: Copilot <[email protected]>
ec9ec16 to
cd564d7
Compare
|
CI note: across two runs on identical content (36942b2, ec9ec16), every job passed at least once. Run 1 failed only BlazorWebTemplate_ConditionalPasskeyRegistration on Windows (known flaky, #69494), and run 2 failed only the Helix job monitor (Helix passed in run 1). I re-pushed the same commit to rerun CI. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
- Replace Unsafe.AreSame/MemoryMarshal assert with a safe Span.Overlaps check and drop the now-unused usings. - Keep the SP800-108 PRF input stackalloc at 128 bytes; rent from ArrayPool (cleared on return) above that instead of heap-allocating. Co-authored-by: Copilot <[email protected]>
…be rented The 0x00 separator between label and context was never written and relied on a zero-initialized buffer. Pooled arrays are not zeroed, which produced wrong subkeys (seen as cookie auth failures in AuthSamples.FunctionalTests). Write it explicitly and make the reference test leave dirty data in the pool first. Co-authored-by: Copilot <[email protected]>
|
The CI failure on 7448283 (AuthSamples cookie tests on macOS/Linux) was a real bug in that commit and is fixed in 782440c. Switching the >128-byte PRF input from 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
/review |
|
CI is fully green on 782440c, which includes the explicit SP800-108 separator fix: aspnetcore-ci and aspnetcore-quarantined-pr pass on every job, with no reruns needed. The automated review raised no open issues. This is ready for maintainer review. 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
…arks On Windows the default AES-256-CBC/HMACSHA256 key maps to the CNG encryptor, and the benchmark reused the user-profile key ring. Use ManagedAuthenticatedEncryptorConfiguration with a temp key directory so ManagedAuthenticatedEncryptor is measured on every OS. Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
|
I retriggered CI with an empty commit, 29ebe73. CI on 116f518 had one failure: 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
…tor round-trip tests The PRF input contains key derivation material, so don't place it in shared pooled memory. Raise the constant stack buffer to 256 bytes to cover longer AAD (purpose) values and fall back to a new array above that. Add tests that go through AuthenticatedEncryptorExtensions.Encrypt (IOptimizedAuthenticatedEncryptor) and KeyRingBasedDataProtector.Protect/Unprotect with a real managed CBC encryptor, with cases on both sides of the 128- and 256-byte KDF input thresholds. Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
|
✅ No documentation PR was created. Confidence that documentation is needed: 5% No docs needed: internal performance refactoring of managed DataProtection with no observable behavior change. Documentation surfaces considered
|
Reduce allocations in the managed DataProtection Protect/Unprotect path.
Description
ManagedAuthenticatedEncryptornow uses a singleSymmetricAlgorithminstance to both size and perform the encryption. Previously eachProtectcreated up to three, and one of them was never disposed. It also implementsIOptimizedAuthenticatedEncryptor, so the payload is encrypted directly into the final array, including the key-ring header pre-buffer.ManagedSP800_108_CTR_HMACSHA512keeps the 128-byte stack buffer for the PRF input. Above that it now rents fromArrayPool(cleared on return) instead of allocating. Real purpose chains, such as application discriminator plus cookie authentication purposes, exceed 128 bytes, so every Protect and Unprotect used to allocate here.Benchmark (new
KeyRingProtectorBenchmarks, 200 B plaintext, BDN InProcess, 3 interleaved rounds against main, Linux; mean of the round means):Protect is about 6% faster. Unprotect is within run-to-run noise (+0.7 to 0.9%, round spread about 0.1 us). Allocations drop 57-59% on Protect and 24-31% on Unprotect. The Antiforgery token serializer benefits as well: Serialize goes from 1160 to 784 B and Deserialize from 776 to 584 B.
Tests: Microsoft.AspNetCore.DataProtection.Tests (net12.0), 344 passed. The 7 new tests are a KDF comparison against a reference SP800-108 implementation across the stack threshold, and a known-answer encrypt with pre- and post-buffers. net472 builds.
🤖 Posted by ASP.NET Core Perf 2 (AI Agent)