Skip to content

Commit

Permalink
remove ctor; revert tests; remove unused _iv member
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob-Hague committed Nov 13, 2023
1 parent 799a5c5 commit 36cea67
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 392 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public partial class AesCipher
private sealed class CtrImpl : BlockCipher, IDisposable
{
private readonly Aes _aes;
private readonly byte[] _iv;
private readonly uint[] _packedIV;
private readonly ICryptoTransform _encryptor;

Expand All @@ -28,7 +27,6 @@ public CtrImpl(
_aes = aes;
_encryptor = aes.CreateEncryptor();

_iv = iv;
_packedIV = GetPackedIV(iv);
}

Expand Down
14 changes: 0 additions & 14 deletions src/Renci.SshNet/Security/Cryptography/Ciphers/AesCipher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,6 @@ public sealed partial class AesCipher : BlockCipher, IDisposable
{
private readonly BlockCipher _impl;

/// <summary>
/// Initializes a new instance of the <see cref="AesCipher"/> class.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="mode">The mode.</param>
/// <param name="padding">The padding.</param>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
public AesCipher(byte[] key, CipherMode mode, CipherPadding padding)
: base(key, 16, mode, padding)
{
_impl = new BlockImpl(key, mode, padding);
}

/// <summary>
/// Initializes a new instance of the <see cref="AesCipher"/> class.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
// expected encrypted values, and also verifies those values against the .NET
// BCL implementation as an extra validation before generating the tests.

Dictionary<string, (string, string, CipherMode?)> modes = new()
Dictionary<string, (string, CipherMode?)> modes = new()
{
["ecb"] = ("mode: null", "iv: null, AesCipherMode.ECB", CipherMode.ECB),
["cbc"] = ("new CbcCipherMode((byte[])iv.Clone())", "(byte[])iv.Clone(), AesCipherMode.CBC", CipherMode.CBC),
["cfb"] = ("new CfbCipherMode((byte[])iv.Clone())", "(byte[])iv.Clone(), AesCipherMode.CFB", CipherMode.CFB),
["ctr"] = ("new CtrCipherMode((byte[])iv.Clone())", "(byte[])iv.Clone(), AesCipherMode.CTR", null),
["ofb"] = ("new OfbCipherMode((byte[])iv.Clone())", "(byte[])iv.Clone(), AesCipherMode.OFB", CipherMode.OFB),
["ecb"] = ("iv: null, AesCipherMode.ECB", CipherMode.ECB),
["cbc"] = ("(byte[])iv.Clone(), AesCipherMode.CBC", CipherMode.CBC),
["cfb"] = ("(byte[])iv.Clone(), AesCipherMode.CFB", CipherMode.CFB),
["ctr"] = ("(byte[])iv.Clone(), AesCipherMode.CTR", null),
["ofb"] = ("(byte[])iv.Clone(), AesCipherMode.OFB", CipherMode.OFB),
};

Random random = new(123);

using IndentedTextWriter tw = new(Console.Out);

foreach ((string mode, (string ctor1Code, string ctor2Code, CipherMode? bclMode)) in modes)
foreach ((string mode, (string modeCode, CipherMode? bclMode)) in modes)
{
foreach (int keySize in new int[] { 128, 192, 256 })
{
Expand Down Expand Up @@ -90,7 +90,8 @@ foreach ((string mode, (string ctor1Code, string ctor2Code, CipherMode? bclMode)
tw.WriteLine($"// {openSslCmd} | hd"); // pipe to hexdump
WriteBytes(expected);
tw.WriteLine();
tw.WriteLine($"var actual = new AesCipher(key, {ctor1Code}, padding: null).Encrypt(input);");
tw.WriteLine($"var actual = new AesCipher(key, {modeCode}, pkcs7Padding: false).Encrypt(input);");
tw.WriteLine();
tw.WriteLine($"CollectionAssert.AreEqual(expected, actual);");

if (bclMode is not null and not CipherMode.OFB)
Expand All @@ -116,14 +117,9 @@ foreach ((string mode, (string ctor1Code, string ctor2Code, CipherMode? bclMode)
}

tw.WriteLine();
tw.WriteLine($"var decrypted = new AesCipher(key, {ctor1Code}, padding: null).Decrypt(actual);");
tw.WriteLine($"CollectionAssert.AreEqual(input, decrypted);");
tw.WriteLine();
tw.WriteLine($"var actual2 = new AesCipher(key, {ctor2Code}, pkcs7Padding: false).Encrypt(input);");
tw.WriteLine($"CollectionAssert.AreEqual(expected, actual2);");
tw.WriteLine($"var decrypted = new AesCipher(key, {modeCode}, pkcs7Padding: false).Decrypt(actual);");
tw.WriteLine();
tw.WriteLine($"var decrypted2 = new AesCipher(key, {ctor2Code}, pkcs7Padding: false).Decrypt(actual);");
tw.WriteLine($"CollectionAssert.AreEqual(input, decrypted2);");
tw.WriteLine($"CollectionAssert.AreEqual(input, decrypted);");

tw.Indent--;
tw.WriteLine("}");
Expand Down
Loading

0 comments on commit 36cea67

Please sign in to comment.