Skip to content

Use keyed HMAC constructors in ManagedAuthenticatedEncryptor - #69859

Merged
DeagleGross merged 2 commits into
dotnet:mainfrom
DeagleGross:deaglegross-keyed-validation-factory-experiment
Oct 16, 2026
Merged

DeagleGross merged 2 commits into
dotnet:mainfrom
DeagleGross:deaglegross-keyed-validation-factory-experiment

Conversation

@DeagleGross

Copy link
Copy Markdown
Member

Follow-up to dotnet/aspnetcore#69789, addressing the remaining instance-based validation paths affected by dotnet/runtime#135759.

Instead of doing creating a HashAlgorithm instance and then setting the Key, we can pass key immediately in the ctor:

// do
_ = new HMACSHA256(key);

// not
var sha256 = new HMACSHA256();
sha256.Key = key;

Proof

Tested on HMACSHA384 which is not covered by one-shot. The code exercises fallback path which uses keyed-constructor.

Environment: RC2 11.0.0-rc.2.26508.107, WSL Ubuntu, verified loaded OpenSSL 3.5.8, BenchmarkDotNet 0.13.0, server GC, 6 warmups and 20 measured iterations

Method Plaintext bytes Code Mean (µs) Error (µs) StdDev (µs) Op/s Gen0 Allocated (B)
ByteArray 5 Existing 16.3914 0.2663 0.3066 61,007.7 0.5798 3,112
ByteArray 5 Updated 13.0134 0.0609 0.0701 76,844.0 0.4272 2,344
PooledWriter 5 Existing 16.4992 0.1655 0.1906 60,609.2 0.5188 2,696
PooledWriter 5 Updated 12.9569 0.0618 0.0711 77,179.2 0.3662 1,928
RefWriter 5 Existing 16.0629 0.1935 0.2229 62,255.4 0.4883 2,632
RefWriter 5 Updated 12.8383 0.0657 0.0757 77,892.1 0.3357 1,864

Fixes #66536

@DeagleGross DeagleGross self-assigned this Oct 15, 2026
Copilot AI balanced review requested due to automatic review settings October 15, 2026 14:10
@DeagleGross DeagleGross added area-dataprotection Includes: DataProtection area-perf Performance infrastructure issues labels Oct 15, 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.

🔵 Needs a closer look

Required review evidence remains incomplete, so the absence of findings does not establish approval readiness.

0 open findings

What changed in this PR

Extends the earlier Data Protection optimization to remaining instance-based HMAC paths, reducing initialization overhead.

Changes:

  • Adds keyed constructors for built-in HMAC algorithms.
  • Routes instance-based validation through the keyed factory while preserving custom-algorithm fallback.
  • Adds payload compatibility and encryption/decryption parity tests.
File Description
src/​DataProtection/​DataProtection/​test/​Microsoft.AspNetCore.DataProtection.Tests/​Managed/​ManagedAuthenticatedEncryptorTests.cs Adds compatibility and keyed-factory tests.
src/​DataProtection/​DataProtection/​src/​Managed/​ManagedAuthenticatedEncryptor.cs Uses keyed factories for instance-based validation.
src/​DataProtection/​DataProtection/​src/​AuthenticatedEncryption/​ManagedAuthenticatedEncryptorFactory.cs Supplies keyed constructors for built-in HMAC types.

🧠 Review effort: Balanced


💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@DeagleGross
DeagleGross merged commit 30e4a52 into dotnet:main Oct 16, 2026
27 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

✅ No documentation PR was created.

Confidence that documentation is needed: 3%

No docs needed: internal performance refactoring in DataProtection (ManagedAuthenticatedEncryptor and its factory) with no public or observable behavior change.

Documentation surfaces considered

  • Conceptual article: Not required — Internal implementation change to HMAC construction; no public API or observable behavior change.
  • Migration guidance: Not required — No user action needed when upgrading.
  • Breaking change: Not required — No compatibility impact; no signals detected.

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 2289deca Deployed Oct 16, 2026 by DeagleGross via conclusion #51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-dataprotection Includes: DataProtection area-perf Performance infrastructure issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Perf regression: antiforgery-generation, antiforgery-validation

3 participants