Repository navigation
Cache AES one-shot cipher handles - #129996
Merged
Merged
Conversation
Reuse ECB and CBC lite ciphers for repeated AES one-shot operations while guarding one-shot and key mutation paths with ConcurrencyBlock. Clear cached ciphers when the key changes or the instance is disposed. Co-authored-by: Copilot <[email protected]>
Contributor
|
Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security |
vcsjones
commented
Jun 29, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the internal AesImplementation used by Aes.Create() to cache and reuse ILiteSymmetricCipher handles for AES one-shot ECB/CBC encrypt/decrypt operations, and introduces ConcurrencyBlock to reject concurrent use on the same Aes instance.
Changes:
- Cache ECB/CBC one-shot cipher handles (
_encrypt*/_decrypt*) and reuse them viaReset(iv)(GetOrCreateCachedLiteCipher). - Add
ConcurrencyBlockto serialize one-shot operations and key mutation (KeySize,SetKeyCore, one-shot cores), plus cache clearing on key changes/dispose. - Refactor key initialization to generate key material directly in
GetKey().
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AesImplementation.cs | Adds cached one-shot cipher handle reuse and concurrency blocking; refactors key initialization and cache invalidation. |
Copilot's findings
- Files reviewed: 1/1 changed files
- Comments generated: 2
bartonjs
reviewed
Jul 2, 2026
bartonjs
reviewed
Jul 2, 2026
bartonjs
approved these changes
Jul 6, 2026
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 caches the handles used by the AES algorithm's one-shot handles so that repeated calls to
{Encrypt,Decrypt}{Cbc,Ecb}can re-use their handles. This means any subsequent calls to these methods don't have to re-create the key handle and do key expansion.Because the previous implementations were more or less thread-safe, but not intentionally, we add
ConcurrencyBlockhere. We do this prevent accidental misuse of these APIs concurrently. Even though they look "safe" because they are one-shots and don't mutate state, that is not the case with the underlying platform's implementations on each handle.For example, in OpenSSL, it internally maintains the previous block of data during CBC processing because it needs to chain the blocks. Using the cipher concurrently is a sure way to lead to data corruption.
We do not do this for CFB because 1. CFB is a less common code and 2. the feedback size is part of the handle on some platforms. That means we would need a cached handle per operation, per feedback size. That makes the
Aesclass bigger with no clear benefit. We also do not do this for 3DES/DES/RC2 because these algorithms should no longer be used on any performance critical path.Summarizing the benchmarks:
"Single-use" calls to the one-shots will have a small allocation regression. This regression is a fixed amount and not dependent on the input size of the data. Because the
Aesclass itself is bigger now to hold these fields, allocating it increased its size by ~40 bytes.The use of
ConcurrencyBlockintroduces minimal overhead and appears to be within noise.Any additional calls to the methods that have been cached will see big improvements. Both in wall-clock time since the handle is being re-used, and allocations are down because they do not allocate the handles. In the right circumstances they are even allocation free.
Benchmark code: https://gist.github.com/vcsjones/f6f571224aff928d7e13d6b7ac3d404e
Benchmark results: