Skip to content

Fix ManagedAuthenticatedEncryptor calculations#65890

Open
BrennanConroy wants to merge 1 commit intomainfrom
brecon/iv
Open

Fix ManagedAuthenticatedEncryptor calculations#65890
BrennanConroy wants to merge 1 commit intomainfrom
brecon/iv

Conversation

@BrennanConroy
Copy link
Member

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).

Copilot AI review requested due to automatic review settings March 20, 2026 17:34
@github-actions github-actions bot added the area-dataprotection Includes: DataProtection label Mar 20, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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-NET TFM path) to compute and validate the MAC over the correct payload segment and compare against the actual computed hash.
  • Multi-target Microsoft.AspNetCore.DataProtection.Tests for both $(DefaultNetCoreTargetFramework) and $(DefaultNetFxTargetFramework).
  • Adjust tests for NETFX compatibility (explicit using directives, some #if NET guards, 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.

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
Copy link
Member Author

Choose a reason for hiding this comment

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

Main change is here.

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.

ManagedAuthenticatedEncryptor is broken on NETFX

2 participants