Repository navigation
Write AES-GCM key modifier and nonce directly into the output buffer - #70193
Merged
Merged
Conversation
Avoids two small array allocations per Encrypt call. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the AES-GCM allocation-reduction change.
No open issues 🎉
What changed in this PR
Reduces per-call allocations in DataProtection’s managed AES-GCM encryptor by writing random bytes directly into the output buffer.
Changes:
- Removes two temporary arrays and their copies while preserving random-generation order and sizes.
- Adds known-answer tests for 0-, 100-, and 2,000-byte payloads, checking expected hashes, array/span output equality, and decryption.
| File | Description |
|---|---|
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/Aes/AesAuthenticatedEncryptorTests.cs | Adds deterministic payload compatibility and round-trip tests. |
| src/DataProtection/DataProtection/src/Managed/AesGcmAuthenticatedEncryptor.cs | Generates the key modifier and nonce directly into their output-buffer slices. |
Review effort: Balanced
Member
|
/review |
DeagleGross
approved these changes
Nov 5, 2026
DeagleGross
temporarily deployed
to
copilot-pat-pool
November 5, 2026 17:29 — with
GitHub Actions
Inactive
DeagleGross
temporarily deployed
to
copilot-pat-pool
November 5, 2026 17:29 — with
GitHub Actions
Inactive
DeagleGross
temporarily deployed
to
copilot-pat-pool
November 5, 2026 17:30 — with
GitHub Actions
Inactive
DeagleGross
temporarily deployed
to
copilot-pat-pool
November 5, 2026 17:32 — with
GitHub Actions
Inactive
DeagleGross
temporarily deployed
to
copilot-pat-pool
November 5, 2026 17:33 — with
GitHub Actions
Inactive
DeagleGross
temporarily deployed
to
copilot-pat-pool
November 5, 2026 17:34 — with
GitHub Actions
Inactive
Contributor
|
✅ No documentation PR was created. Confidence that documentation is needed: 3% No docs needed: internal refactoring with test additions only. Documentation surfaces considered
|
DeagleGross
temporarily deployed
to
copilot-pat-pool
November 5, 2026 17:34 — with
GitHub Actions
Inactive
This branch was previously deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Write AES-GCM key modifier and nonce directly into the output buffer
Description
AesGcmAuthenticatedEncryptor.Encryptallocated two small arrays per call,GenRandom(16)for the key modifier andGenRandom(12)for the nonce, then copied them into the output buffer. The span overloadIManagedGenRandom.GenRandom(Span<byte>)(backed byRandomNumberGenerator.Fill) now writes them directly into their final positions in the output buffer. The derived-key context reads the key modifier from that same slice. The RNG calls happen in the same order with the same sizes.The managed AES-GCM encryptor is used on Linux/macOS when AES-GCM is configured. Found by profiling (EventPipe allocation sampling): these arrays were about 14% of GCM Protect allocations.
Benchmarks
Linux x64, 2 cores, BenchmarkDotNet in-process, 3 interleaved rounds vs main (median). AES-256-GCM, cookie purpose chain, cached protector.
Allocation-only win: two objects fewer per Protect, with no time change.
Security reasoning
Encrypt_AesGcm_KnownAnswer(0/100/2000 B) asserts the SHA-256 of the payload produced with a deterministic RNG. The expected hashes were generated by running the same test against main's encryptor. It also asserts that the byte[] and spanEncryptoutputs are equal, and decrypts the result.RandomNumberGenerator.Fill), same sizes, same call order. The key modifier and nonce are public parts of the payload, not secrets, so writing them straight into the output exposes nothing new. Key-derivation buffers and their clearing are untouched.CryptographicException).Overlap
#70146 changes the managed CBC encryptor (
ManagedAuthenticatedEncryptor) and the SP800-108 KDF buffer. This PR touches only the AES-GCM encryptor, so there is no overlap.Tests
Microsoft.AspNetCore.DataProtection.Tests: 340 passed, including 3 new known-answer cases.🤖 Posted by ASP.NET Core Perf 2 (AI Agent)