Skip to content

Conversation

@vcsjones
Copy link
Member

@vcsjones vcsjones commented Jul 2, 2021

This is a refactoring of the existing one-shot cryptography for the up-coming CBC/CFB implementations. The goal of the refactoring was to reduce allocations and do less work that the one shots don't need to do.

  1. The universal transform one-shot is static for the Implementation symmetric ciphers, so we don't allocation a universal transform.
  2. Skipping Reset on finalize since the one shot is not going to get used another time.
  3. Permits passing the IV as a ReadOnlySpan<byte> where possible.

The CNG implementations (AesCng and friends) still uses the existing one-shots on the Universal transforms. I would like to remove that in a potential follow up. Then we can remove the one-shots from the universal transform.

@ghost ghost added the area-System.Security label Jul 2, 2021
@ghost
Copy link

ghost commented Jul 2, 2021

Tagging subscribers to this area: @bartonjs, @vcsjones, @krwq, @GrabYourPitchforks
See info in area-owners.md if you want to be subscribed.

Issue Details

This is a refactoring of the existing one-shot cryptography for the up-coming CBC/CFB implementations. The goal of the refactoring was to reduce allocations and do less work that the one shots don't need to do.

  1. The universal transform one-shot is static for the Implementation symmetric ciphers, so we don't allocation a universal transform.
  2. Skipping Reset on finalize since the one shot is not going to get used another time.
  3. Permits passing the IV as a ReadOnlySpan<byte> where possible.

The CNG implementations (AesCng and friends) still uses the existing one-shots on the Universal transforms. I would like to remove that in a potential follow up. Then we can remove the one-shots from the universal transform.

Author: vcsjones
Assignees: -
Labels:

area-System.Security

Milestone: -

@vcsjones vcsjones changed the title Refactor one-shot symmetric one-shots Refactor symmetric one-shots Jul 2, 2021
@vcsjones
Copy link
Member Author

vcsjones commented Jul 2, 2021

There is still some clean up and other ways to improve but this was getting to be a big change. In particular, the way CFB feedback size is handled is a little quirky and deserves its own PR that would be easier to review in isolation.

@vcsjones vcsjones marked this pull request as ready for review July 2, 2021 18:59
using System.Diagnostics;
using System.Security.Cryptography;

namespace Internal.Cryptography
Copy link
Member

Choose a reason for hiding this comment

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

I want to get all the stuff in Internal\Cryptography out (it should just be S.S.Cryptography). So please use System.Security.Cryptography

Copy link
Member Author

Choose a reason for hiding this comment

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

Should the file move elsewhere or just fix the namespace?


namespace Internal.Cryptography
{
internal interface ILiteSymmetricCipher : IDisposable
Copy link
Member

Choose a reason for hiding this comment

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

Lite as a prefix seems odd.

  • ISpanSymmetricCipher
  • ISymmetricCipherLite
  • ISymmetricCipherSlim


namespace Internal.Cryptography
{
internal interface ILiteSymmetricCipher : IDisposable
Copy link
Member

Choose a reason for hiding this comment

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

Do we need an interface here? Any reason it can't be done on BasicSymmetricCipher?

Copy link
Member Author

Choose a reason for hiding this comment

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

Any reason it can't be done on BasicSymmetricCipher?

BasicSymmetricCipher has a property for the IV which is one of the things I was trying to avoid. We could just have a null IV, but I was trying to be super careful about the readability of this. One could assume "no IV means it is ECB" when really what we were intending was a CBC with no captured IV since it is not reusable.

The exception to this is the BCrypt implementation. It has no "Lite" implementation because we are responsible for passing in the IV for each call. So the BCrypt implementation just implements the interface without extracting the functionality.

using System.Diagnostics;
using System.Security.Cryptography;

namespace Internal.Cryptography
Copy link
Member

Choose a reason for hiding this comment

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

Same general "don't add to this bad namespace, please" (unless it's just splitting a class into partials)

@bartonjs
Copy link
Member

bartonjs commented Jul 3, 2021

This is... pretty big. (Bigger than I have time to finish reviewing today).

What's the cost if we don't do this for this release? Worst case, the CFB implementations could be

using (xform = CreateTransform(CFB))
{
    xform.TransformFinalBlock(...);
    I assume that now we need to copy stuff?
}

which gets the API out the door and then we can factor stuff out in late August to get cleaner for 7.

Or if it's just "oh, it takes longer than 10 minutes" then I can give it the longer than 10 minutes next week :)

@vcsjones
Copy link
Member Author

vcsjones commented Jul 3, 2021

What's the cost if we don't do this for this release?

Nothing extremely important. It just nudges us closer to an "ideal" state for the one shots in terms of allocation and not doing unnecessary work.

Agree I let this one run away from me a bit. I'll close this out and keep the branch so it can re-evaluated when you're accepting things for 7.0.

@vcsjones vcsjones closed this Jul 3, 2021
@vcsjones vcsjones mentioned this pull request Jul 5, 2021
1 task
@ghost ghost locked as resolved and limited conversation to collaborators Aug 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants