-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
One Shot ECB #52510
One Shot ECB #52510
Conversation
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
Tagging subscribers to this area: @bartonjs, @vcsjones, @krwq, @GrabYourPitchforks Issue DetailsThis is a work-in progress for ECB one shots. Remaining tasks:
Implementation notes so far:
|
The CNG types were calculating their padding size based on an instance method that used the Mode property instead of the actual mode being used. Most of the time, this will just end up being whatever the block size of the algorithm is going to be. However, in the case of CFB8, that is not true. Even if the algorithm instance is in CFB8 mode, the EncryptEcb and DecryptEcb one-shots should continue to function as ECB mode.
src/libraries/Common/src/Internal/Cryptography/UniversalCryptoDecryptor.cs
Outdated
Show resolved
Hide resolved
The overlapping buffer check was renting a buffer every time unless the input and output overlap exactly. We only need to rent when they do overlap partially, or no overlapping at all.
@bartonjs This is not yet complete but, as time permits for you, this is probably nearing close to done minus the remaining tasks above and could be reviewed at least for design / approach. |
src/libraries/Common/src/Internal/Cryptography/UniversalCryptoDecryptor.cs
Outdated
Show resolved
Hide resolved
|
||
[Theory] | ||
[MemberData(nameof(EcbTestCases))] | ||
public static void EcbRoundtrip(byte[] plaintext, byte[] ciphertext, PaddingMode padding) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder (genuine curiosity, not presupposing an answer) if doing subclass for ICryptoTransform vs OneShot would be goodness like with the places we use subclassing for Array vs Span (e.g. RSA)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I follow, can you expand on that a bit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have a bunch of tests that are encrypting or decrypting AES (and 3DES, etc) via the CreateEncryptor/CreateDecryptor methods.
- Move the data verification tests to a class different than the property validators and such (assuming they aren't already).
- Make all the methods instance methods, instead of static methods.
- Make them call (e.g.)
protected abstract byte[] Decrypt(Aes cipher, ReadOnlySpan<byte> iv, ReadOnlySpan<byte> ciphertext, PaddingMode paddingMode, CipherMode blockMode, int feedbackSize);
- Make N derived types, AesCipherTests_CryptoTransform, AesCipherTests_OneShot, (AesCipherTests_CryptoTransform_Chunked, AesCipherTests_CryptoTransform_OneByteAtATime, etc)
- Each derived type writes its version of Encrypt/Decrypt, and now all of the same scenario/theory/whatever data is being tried in all the input models.
Comparative example:
Lines 8 to 20 in d71182a
public sealed class SignVerify_Span : SignVerify | |
{ | |
protected override byte[] SignData(RSA rsa, byte[] data, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding) => | |
TryWithOutputArray(dest => rsa.TrySignData(data, dest, hashAlgorithm, padding, out int bytesWritten) ? (true, bytesWritten) : (false, 0)); | |
protected override byte[] SignHash(RSA rsa, byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding) => | |
TryWithOutputArray(dest => rsa.TrySignHash(hash, dest, hashAlgorithm, padding, out int bytesWritten) ? (true, bytesWritten) : (false, 0)); | |
protected override bool VerifyData(RSA rsa, byte[] data, byte[] signature, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding) => | |
rsa.VerifyData((ReadOnlySpan<byte>)data, (ReadOnlySpan<byte>)signature, hashAlgorithm, padding); | |
protected override bool VerifyHash(RSA rsa, byte[] hash, byte[] signature, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding) => | |
rsa.VerifyHash((ReadOnlySpan<byte>)hash, (ReadOnlySpan<byte>)signature, hashAlgorithm, padding); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(OneShot can also then use OneShot_InPlace, OneShot_ForwardOverlapped, OneShot_NegativeOverlapped, etc)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I typically shy away from that approach, mostly as a matter of preference and more confidence that we were not losing any existing test coverage mistakenly through a refactoring.
I see the desire though. One of my remaining check items is some test work. I'll noodle with it to see if turns in to something I don't dislike.
...on/tests/System/Security/Cryptography/AlgorithmImplementations/AES/AesCipherTests.OneShot.cs
Show resolved
Hide resolved
We're only a couple of weeks away from the target platform complete date. What's left for the ECB part, and do you need help? I don't want to have ECB and CBC split across a release boundary, so we either need to finish this off relatively soon or push it out to 7. |
I think the "musts" that are remaining are XML docs and a few more test cases to make me feel better about failure scenarios. I can commit to getting ECB in shape by end of the week (Sunday if not sooner) and it should be relatively straight forward to do the work for CBC/CFB. |
@bartonjs this should be in a reviewable state now, caveated on the things in the top post are not done where they are not checked off. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a whole heap'o periods, a few missing <returns>
es, and two pieces of technical feedback.
...stem.Security.Cryptography.Primitives/src/System/Security/Cryptography/SymmetricAlgorithm.cs
Outdated
Show resolved
Hide resolved
...stem.Security.Cryptography.Primitives/src/System/Security/Cryptography/SymmetricAlgorithm.cs
Outdated
Show resolved
Hide resolved
...stem.Security.Cryptography.Primitives/src/System/Security/Cryptography/SymmetricAlgorithm.cs
Outdated
Show resolved
Hide resolved
...stem.Security.Cryptography.Primitives/src/System/Security/Cryptography/SymmetricAlgorithm.cs
Outdated
Show resolved
Hide resolved
...stem.Security.Cryptography.Primitives/src/System/Security/Cryptography/SymmetricAlgorithm.cs
Outdated
Show resolved
Hide resolved
...stem.Security.Cryptography.Primitives/src/System/Security/Cryptography/SymmetricAlgorithm.cs
Outdated
Show resolved
Hide resolved
...stem.Security.Cryptography.Primitives/src/System/Security/Cryptography/SymmetricAlgorithm.cs
Outdated
Show resolved
Hide resolved
...stem.Security.Cryptography.Primitives/src/System/Security/Cryptography/SymmetricAlgorithm.cs
Outdated
Show resolved
Hide resolved
...stem.Security.Cryptography.Primitives/src/System/Security/Cryptography/SymmetricAlgorithm.cs
Outdated
Show resolved
Hide resolved
...stem.Security.Cryptography.Primitives/src/System/Security/Cryptography/SymmetricAlgorithm.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Jeremy Barton <jbarton@microsoft.com>
@bartonjs sincere thank you for the proofing and taking the time to put all of the suggestions in. Will address the rest of the feedback in the next day or two. |
src/libraries/Common/src/Internal/Cryptography/UniversalCryptoDecryptor.cs
Outdated
Show resolved
Hide resolved
...stem.Security.Cryptography.Primitives/src/System/Security/Cryptography/SymmetricAlgorithm.cs
Outdated
Show resolved
Hide resolved
I came into this late, but it looks great! Feeling kinda guilty that the only comments I can offer are stylistic nits. |
@bartonjs I think I got all your feedback applied consistently.
There will be plenty more opportunities to say, "Kevin... what on earth are you doing??" |
Woohoo! Thanks, @vcsjones. |
It's done — Frodo Will get CBC / CFB in ASAP. |
This is a work-in progress for ECB one shots. Remaining tasks:
SymmetricAlgorithm
throws aNotImplementedException
. Ideally for existing inherited classes, they can fallback to the transform methodsKey
property for example)AesCng
,TripleDESCng
, etc. is not implemented.Implementation notes so far:
AesCryptoServiceProvider
,AesManaged
, andRijndaelManaged
will not get a custom implementation since they are being obsoleted.My intention is to implement ECB "fully" and get it reviewed, then it can more or less serve as a pattern for CBC and CFB.
Contributes to #2406