Skip to content
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

Merged
merged 40 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
4e831fd
ECB decrypt skeleton
vcsjones May 6, 2021
0192aeb
Add AES ECB decrypt one-shot
vcsjones May 6, 2021
3648777
Simple test
vcsjones May 6, 2021
ba0391e
Add ECB decrypt tests
vcsjones May 6, 2021
21e3760
Decrypt in to buffer unless no padding
vcsjones May 6, 2021
7505b6a
Implement ECB encryption
vcsjones May 7, 2021
ef12105
Add more ECB encrypt tests
vcsjones May 7, 2021
7a808fd
Add tests for zero length buffers
vcsjones May 7, 2021
835cf76
Fixup ECB tests
vcsjones May 7, 2021
7a72745
Add 3DES and tests.
vcsjones May 7, 2021
425f6c8
Implement DES ECB
vcsjones May 8, 2021
5fd05ca
Implement RC2 one shot
vcsjones May 8, 2021
b205986
Fix overlapping buffers in CNG
vcsjones May 8, 2021
0768841
Allow-list one shot ECB for not overridding
vcsjones May 9, 2021
bf32394
Permit overlapping buffers on MacOS
vcsjones May 9, 2021
5993181
Get non-persisted keys working for CNG one shots
vcsjones May 13, 2021
7d0ea8d
Fix padding calculation for CNG types.
vcsjones May 13, 2021
9dbd999
Fix overlapping buffer check on CNG.
vcsjones May 13, 2021
a853ccd
AES/3DES persisted CNG key implementation and tests
vcsjones May 14, 2021
1df32b7
Fix indentation
vcsjones May 14, 2021
720018b
Fix white space
vcsjones May 14, 2021
b32fd8a
Merge remote-tracking branch 'ms/main' into 2406-one-shot-ecb
vcsjones Jun 7, 2021
b1c6ea3
Some code review feedback and WIP on documentation.
vcsjones Jun 8, 2021
02d68cb
Merge remote-tracking branch 'upstream/main' into 2406-one-shot-ecb
vcsjones Jun 14, 2021
ccdecda
Fix build
vcsjones Jun 14, 2021
500b829
Fix compilation, again
vcsjones Jun 14, 2021
2d43ec7
Remove reset. This does not hold over any cipher data
vcsjones Jun 14, 2021
3f0c165
Use pool for decrypt buffer
vcsjones Jun 14, 2021
16ff27f
Add missing empty line.
vcsjones Jun 16, 2021
b315f93
Fix whitespace
vcsjones Jun 23, 2021
03b5564
Finish XML documentation
vcsjones Jun 23, 2021
1ebb11c
Fixup exceptions
vcsjones Jun 23, 2021
23fe2a6
Fix handling core implementation that indicates it wrote more bytes
vcsjones Jun 24, 2021
1c65eec
Test and fix for overflow
vcsjones Jun 24, 2021
f20f7aa
Merge remote-tracking branch 'ms/main' into 2406-one-shot-ecb
vcsjones Jun 24, 2021
0d76e80
Fix grammar and punctuation
vcsjones Jun 25, 2021
33c649d
Remove unnecessary early return.
vcsjones Jun 25, 2021
0e3b568
Let Array.Resize do the work of creating a new array
vcsjones Jun 25, 2021
d017f9d
Simplify throwing exceptions for incorrect encryption length
vcsjones Jun 25, 2021
18f2478
Apply feedback consistently
vcsjones Jun 26, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplify throwing exceptions for incorrect encryption length
  • Loading branch information
vcsjones committed Jun 25, 2021
commit d017f9dc907324eebc6c3958f523e9f24181376f
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@
<data name="Cryptography_PlaintextTooLarge" xml:space="preserve">
<value>The specified plaintext size is too large.</value>
</data>
<data name="Cryptography_EncryptedIncorrectPadding" xml:space="preserve">
<value>TryEncryptEcbCore unexpectedly produced a ciphertext which has the incorrect amount of padding.</value>
<data name="Cryptography_EncryptedIncorrectLength" xml:space="preserve">
<value>{0} unexpectedly produced a ciphertext with the incorrect length.</value>
</data>
<data name="NotSupported_SubclassOverride" xml:space="preserve">
<value>Method not supported. Derived class must override.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,8 @@ public byte[] DecryptEcb(ReadOnlySpan<byte> ciphertext, PaddingMode paddingMode)

// Array.Resize will no-op if the array does not need to be resized.
Array.Resize(ref decryptBuffer, written);
return decryptBuffer;
}
return decryptBuffer;
}

/// <summary>
/// Decrypts data into the specified buffer, using ECB mode with the specified padding mode.
Expand Down Expand Up @@ -598,21 +598,15 @@ public byte[] EncryptEcb(ReadOnlySpan<byte> plaintext, PaddingMode paddingMode)
// We expect most if not all uses to encrypt to exactly the ciphertextLength
byte[] buffer = GC.AllocateUninitializedArray<byte>(ciphertextLength);

if (!TryEncryptEcbCore(plaintext, buffer, paddingMode, out int written))
if (!TryEncryptEcbCore(plaintext, buffer, paddingMode, out int written) ||
written != ciphertextLength)
{
// This means a user-derived imiplementation added more padding than we expected.
throw new CryptographicException(SR.Argument_DestinationTooShort);
}

if (written != ciphertextLength)
{
// This should not happen, but could if a derived class's implementation of core
// This means a user-derived imiplementation added more padding than we expected or
// did something non-standard (encrypt to a partial block). This can't happen for
// multiple padding blocks since the buffer would have been too small in the first
// place. It doesn't make sense to try and support partial block encryption, likely
// something went very wrong. So throw.

throw new CryptographicException(SR.Cryptography_EncryptedIncorrectPadding);
throw new CryptographicException(SR.Format(SR.Cryptography_EncryptedIncorrectLength, nameof(TryEncryptEcbCore)));
}

return buffer;
Expand Down