Fix ManagedAuthenticatedEncryptor calculations#65890
Open
BrennanConroy wants to merge 1 commit intomainfrom
Open
Fix ManagedAuthenticatedEncryptor calculations#65890BrennanConroy wants to merge 1 commit intomainfrom
BrennanConroy wants to merge 1 commit intomainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a .NET Framework-specific integrity-check bug in ManagedAuthenticatedEncryptor by correcting the MAC computation range and comparison, and expands DataProtection test coverage by adding a .NET Framework target to the main test project (with accompanying test adjustments for cross-TFM compatibility).
Changes:
- Fix
ManagedAuthenticatedEncryptor(non-NETTFM path) to compute and validate the MAC over the correct payload segment and compare against the actual computed hash. - Multi-target
Microsoft.AspNetCore.DataProtection.Testsfor both$(DefaultNetCoreTargetFramework)and$(DefaultNetFxTargetFramework). - Adjust tests for NETFX compatibility (explicit
usingdirectives, some#if NETguards, and Span-based assertions converted where necessary).
Reviewed changes
Copilot reviewed 55 out of 55 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/DataProtection/DataProtection/src/Managed/ManagedAuthenticatedEncryptor.cs | Fixes MAC calculation/validation logic for non-NET TFMs and aligns buffer nullability usage. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/Microsoft.AspNetCore.DataProtection.Tests.csproj | Adds NETFX target and conditions framework-specific references. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/Managed/ManagedAuthenticatedEncryptorTests.cs | Updates assertions/guards to run under NETFX and keep Span-only tests under #if NET. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/KeyManagement/KeyRingBasedDataProtectorTests.cs | Updates Span-based assertions and gates Span-only helper usage under #if NET. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/Internal/RoundtripEncryptionHelpers.cs | Restricts Span-based helper to #if NET to avoid NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/Internal/KeyManagementOptionsPostSetupTest.cs | Replaces Directory.CreateTempSubdirectory() usage for broader TFM compatibility. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/Repositories/FileSystemXmlRepositoryTests.cs | Gates UnixFileMode test under #if NET and adjusts Guid parsing / usings for NETFX builds. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/Repositories/RegistryXmlRepositoryTests.cs | Removes culture-based Guid.Parse overload usage for broader TFM compatibility. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/Aes/AesAuthenticatedEncryptorTests.cs | Limits Span/TryEncryptDecrypt tests to #if NET. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/Cng/CbcAuthenticatedEncryptorTests.cs | Adjusts Span assertions and Base64 conversion to avoid Span overloads not available on NETFX. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/Cng/GcmAuthenticatedEncryptorTests.cs | Adjusts Span assertions and gates Span-only tests under #if NET. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/HostingTests.cs | Wraps file in #if NET to avoid NETFX-incompatible hosting test surface. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/ActivatorTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/ContainerUtilsTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/DataProtectionUtilityExtensionsTests.cs | Adds/reorders usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/EphemeralDataProtectionProviderTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/RegistryPolicyResolverTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/SP800_108/SP800_108Tests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/SecretAssert.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/SecretTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/ServiceCollectionTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/StringLoggerFactory.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/TypeForwardingActivatorTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/XmlAssert.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/AuthenticatedEncryption/CngCbcAuthenticatedEncryptorFactoryTest.cs | Normalizes header encoding and adds explicit Xunit using. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/AuthenticatedEncryption/CngGcmAuthenticatedEncryptorFactoryTest.cs | Normalizes header encoding and adds explicit Xunit using. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/AuthenticatedEncryption/ManagedAuthenticatedEncryptorFactoryTest.cs | Normalizes header encoding and adds explicit Xunit using. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/AuthenticatedEncryption/ConfigurationModel/AuthenticatedEncryptorDescriptorDeserializerTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/AuthenticatedEncryption/ConfigurationModel/AuthenticatedEncryptorDescriptorTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/AuthenticatedEncryption/ConfigurationModel/CngCbcAuthenticatedEncryptorConfigurationTests.cs | Adds explicit Xunit using for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/AuthenticatedEncryption/ConfigurationModel/CngCbcAuthenticatedEncryptorDescriptorDeserializerTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/AuthenticatedEncryption/ConfigurationModel/CngCbcAuthenticatedEncryptorDescriptorTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/AuthenticatedEncryption/ConfigurationModel/CngGcmAuthenticatedEncryptorConfigurationTests.cs | Adds explicit Xunit using for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/AuthenticatedEncryption/ConfigurationModel/CngGcmAuthenticatedEncryptorDescriptorDeserializerTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/AuthenticatedEncryption/ConfigurationModel/CngGcmAuthenticatedEncryptorDescriptorTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/AuthenticatedEncryption/ConfigurationModel/ManagedAuthenticatedEncryptorConfigurationTests.cs | Adds explicit Xunit using for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/AuthenticatedEncryption/ConfigurationModel/ManagedAuthenticatedEncryptorDescriptorDeserializerTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/AuthenticatedEncryption/ConfigurationModel/ManagedAuthenticatedEncryptorDescriptorTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/KeyManagement/AdditionalAuthenticatedDataTemplateTests.cs | Adds explicit Xunit using for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/KeyManagement/CacheableKeyRingTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/KeyManagement/DefaultKeyResolverTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/KeyManagement/DeferredKeyTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/KeyManagement/KeyEscrowServiceProviderExtensionsTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/KeyManagement/KeyRingProviderTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/KeyManagement/KeyRingTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/KeyManagement/KeyTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/KeyManagement/XmlKeyManagerTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/Repositories/EphemeralXmlRepositoryTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/XmlEncryption/CertificateXmlEncryptionTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/XmlEncryption/DpapiNGXmlEncryptionTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/XmlEncryption/DpapiXmlEncryptionTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/XmlEncryption/EncryptedXmlDecryptorTests.cs | Normalizes header encoding and adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/XmlEncryption/NullXmlEncryptionTests.cs | Adds explicit Xunit using for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/XmlEncryption/XmlEncryptionExtensionsTests.cs | Adds explicit usings for NETFX compilation. |
| src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/Internal/KeyManagementOptionsSetupTest.cs | Normalizes header encoding and adds explicit usings for NETFX compilation. |
BrennanConroy
commented
Mar 20, 2026
| Span<byte> correctHash = hashSize <= 128 | ||
| ? stackalloc byte[128].Slice(0, hashSize) | ||
| : (correctHashArray = new byte[hashSize]); | ||
| // if validationSubkey is stackalloc'ed, there is no way we avoid an alloc here |
Member
Author
There was a problem hiding this comment.
Main change is here.
This was referenced Mar 20, 2026
Open
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.
Fixes #65889
Also, adds netfx to the main DataProtection tests so we get the missing coverage that would have caught this. (7 or 8 tests failed before fixing the bug after adding netfx to the test project).