Skip to content

Write AES-GCM key modifier and nonce directly into the output buffer - #70193

Merged
DeagleGross merged 1 commit into
mainfrom
perf/dataprotection-gcm-random-alloc
Nov 5, 2026
Merged

DeagleGross merged 1 commit into
mainfrom
perf/dataprotection-gcm-random-alloc

Conversation

@davidfowl

Copy link
Copy Markdown
Member

Write AES-GCM key modifier and nonce directly into the output buffer

Description

AesGcmAuthenticatedEncryptor.Encrypt allocated two small arrays per call, GenRandom(16) for the key modifier and GenRandom(12) for the nonce, then copied them into the output buffer. The span overload IManagedGenRandom.GenRandom(Span<byte>) (backed by RandomNumberGenerator.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.

Scenario main PR main alloc PR alloc
Protect 100 B 7.76 us 7.76 us 560 B 480 B (-14%)
Protect 2 KB 8.41 us 8.37 us 2,504 B 2,424 B
Unprotect 100 B (path untouched) 5.65 us 5.59 us 416 B 416 B

Allocation-only win: two objects fewer per Protect, with no time change.

Security reasoning

  • Byte-identical output: the new known-answer theory 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 span Encrypt outputs are equal, and decrypts the result.
  • Randomness unchanged: same RNG source (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.
  • No public API change, no PublicAPI.*.txt changes, no unsafe code added, no change to validation or exception behavior (still homogenized to 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)

Avoids two small array allocations per Encrypt call.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 31, 2026 16:16
@github-actions github-actions Bot added the area-dataprotection Includes: DataProtection label Oct 31, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@PureWeen

PureWeen commented Nov 1, 2026

Copy link
Copy Markdown
Member

/review

@DeagleGross
DeagleGross merged commit c342e77 into main Nov 5, 2026
29 checks passed
@DeagleGross
DeagleGross deleted the perf/dataprotection-gcm-random-alloc branch November 5, 2026 17:29
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

✅ No documentation PR was created.

Confidence that documentation is needed: 3%

No docs needed: internal refactoring with test additions only.

Documentation surfaces considered

  • Conceptual article: Not required — Internal allocation optimization in AesGcmAuthenticatedEncryptor; no public API or behavior change, output byte-identical.
  • Migration guidance: Not required — No user action needed when upgrading.
  • Breaking change: Not required — No compatibility impact; no API, default, or exception behavior changes.

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — a6193cb3 Deployed Nov 5, 2026 by DeagleGross via conclusion #200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-dataprotection Includes: DataProtection

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants