Add SymmetricAlgorithm.SetKey(ROSpan)#113146
Merged
bartonjs merged 6 commits intodotnet:mainfrom Mar 7, 2025
Merged
Conversation
|
Note regarding the |
1 similar comment
|
Note regarding the |
Contributor
There was a problem hiding this comment.
PR Overview
This pull request introduces a new SetKey(ReadOnlySpan key) API for symmetric algorithms. It refactors key management by using a FixedMemoryKeyBox in implementations such as AES and AppleCCCryptor, and it updates tests across AES, RC2, DES, TripleDES, and one-shot paths to verify the new SetKey functionality.
Reviewed Changes
| File | Description |
|---|---|
| src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/AES/AesContractTests.cs | Adds tests checking behavior when setting the key via property assignment and SetKey. |
| src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RC2/RC2CipherTests.cs | Introduces SetKey_Sanity tests to verify key exchange between instances. |
| src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/DES/DESCipherTests.cs | Updates tests to use SetKey for ensuring consistency of key and IV handling. |
| src/libraries/System/Security/Cryptography/src/System/Security/Cryptography/AesImplementation.cs | Refactors key handling with FixedMemoryKeyBox support and overrides Key, KeySize and GenerateKey. |
| src/libraries/System/Security/Cryptography/src/System/Security/Cryptography/AppleCCCryptor.cs | Upgrades key storage to use FixedMemoryKeyBox and disposes the key in Dispose. |
| Other files (AesImplementation.OpenSsl.cs, AesImplementation.Windows.cs, ref/System.Security.Cryptography.cs, etc.) | Update API definitions and implementations to support SetKey overload with ReadOnlySpan. |
Copilot reviewed 25 out of 25 changed files in this pull request and generated no comments.
stephentoub
reviewed
Mar 5, 2025
...libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesImplementation.cs
Outdated
Show resolved
Hide resolved
stephentoub
reviewed
Mar 5, 2025
...libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesImplementation.cs
Show resolved
Hide resolved
stephentoub
approved these changes
Mar 5, 2025
vcsjones
reviewed
Mar 5, 2025
...libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesImplementation.cs
Show resolved
Hide resolved
This was referenced Mar 5, 2025
vcsjones
reviewed
Mar 5, 2025
...ts/System/Security/Cryptography/AlgorithmImplementations/TripleDES/TripleDESContractTests.cs
Show resolved
Hide resolved
...libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesImplementation.cs
Show resolved
Hide resolved
...ibraries/System.Security.Cryptography/src/System/Security/Cryptography/SymmetricAlgorithm.cs
Outdated
Show resolved
Hide resolved
3 tasks
vcsjones
reviewed
Mar 5, 2025
...libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesImplementation.cs
Outdated
Show resolved
Hide resolved
vcsjones
approved these changes
Mar 5, 2025
vcsjones
reviewed
Mar 5, 2025
...libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesImplementation.cs
Outdated
Show resolved
Hide resolved
3 tasks
vcsjones
reviewed
Mar 6, 2025
...stem.Security.Cryptography/src/System/Security/Cryptography/DESCryptoServiceProvider.Unix.cs
Show resolved
Hide resolved
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
This change adds SymmetricAlgorithm.SetKey to set the key of an instance based on a ReadOnlySpan.
The AesImplementation type takes advantage of it by moving the key to a FixedMemoryKeyBox. None of the other algorithms are being changed at this time, mostly because no one should be using them.
AesCng could also change to using FixedMemoryKeyBox, but it's a non-trivial change due to the externalization of state into the CngSymmetricAlgorithmCore struct; and the main intended purpose for the AesCng type is to open a persisted key (where the change wouldn't matter).
Fixes #111154.