Skip to content

Commit 79ff9a3

Browse files
committed
Encrypt should take into account padding for length of inputBuffer passed to EncryptBlock if padding is specified, no matter input is divisible or not.
1 parent 56d5254 commit 79ff9a3

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

src/Renci.SshNet/Security/Cryptography/BlockCipher.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,17 @@ protected BlockCipher(byte[] key, byte blockSize, CipherMode mode, CipherPadding
7575
/// </returns>
7676
public override byte[] Encrypt(byte[] input, int offset, int length)
7777
{
78-
if (length % _blockSize > 0)
78+
if (_padding is not null)
7979
{
80-
if (_padding is null)
81-
{
82-
throw new ArgumentException(string.Format("The data block size is incorrect for {0}.", GetType().Name), "data");
83-
}
84-
8580
var paddingLength = _blockSize - (length % _blockSize);
8681
input = _padding.Pad(input, offset, length, paddingLength);
8782
length += paddingLength;
8883
offset = 0;
8984
}
85+
else if (length % _blockSize > 0)
86+
{
87+
throw new ArgumentException(string.Format("The data block size is incorrect for {0}.", GetType().Name), "data");
88+
}
9089

9190
var output = new byte[length];
9291
var writtenBytes = 0;

test/Renci.SshNet.Tests/Classes/Security/Cryptography/BlockCipherTest.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Renci.SshNet.Tests.Classes.Security.Cryptography
1414
public class BlockCipherTest : TestBase
1515
{
1616
[TestMethod]
17-
public void EncryptShouldTakeIntoAccountPaddingForLengthOfOutputBufferPassedToEncryptBlock()
17+
public void EncryptShouldTakeIntoAccountPaddingForLengthOfInputBufferPassedToEncryptBlock_InputNotDivisible()
1818
{
1919
var input = new byte[] { 0x2c, 0x1a, 0x05, 0x00, 0x68 };
2020
var output = new byte[] { 0x0a, 0x00, 0x03, 0x02, 0x06, 0x08, 0x07, 0x05 };
@@ -23,7 +23,7 @@ public void EncryptShouldTakeIntoAccountPaddingForLengthOfOutputBufferPassedToEn
2323
{
2424
EncryptBlockDelegate = (inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset) =>
2525
{
26-
Assert.AreEqual(8, outputBuffer.Length);
26+
Assert.AreEqual(8, inputBuffer.Length);
2727
Buffer.BlockCopy(output, 0, outputBuffer, 0, output.Length);
2828
return inputBuffer.Length;
2929
}
@@ -34,6 +34,27 @@ public void EncryptShouldTakeIntoAccountPaddingForLengthOfOutputBufferPassedToEn
3434
Assert.IsTrue(output.SequenceEqual(actual));
3535
}
3636

37+
[TestMethod]
38+
public void EncryptShouldTakeIntoAccountPaddingForLengthOfInputBufferPassedToEncryptBlock_InputDivisible()
39+
{
40+
var input = new byte[0];
41+
var output = new byte[] { 0x0a, 0x00, 0x03, 0x02, 0x06, 0x08, 0x07, 0x05 };
42+
var key = new byte[] { 0x17, 0x78, 0x56, 0xe1, 0x3e, 0xbd, 0x3e, 0x50, 0x1d, 0x79, 0x3f, 0x0f, 0x55, 0x37, 0x45, 0x54 };
43+
var blockCipher = new BlockCipherStub(key, 8, null, new PKCS7Padding())
44+
{
45+
EncryptBlockDelegate = (inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset) =>
46+
{
47+
Assert.AreEqual(8, inputBuffer.Length);
48+
Buffer.BlockCopy(output, 0, outputBuffer, 0, output.Length);
49+
return inputBuffer.Length;
50+
}
51+
};
52+
53+
var actual = blockCipher.Encrypt(input);
54+
55+
Assert.IsTrue(output.SequenceEqual(actual));
56+
}
57+
3758
[TestMethod]
3859
public void DecryptShouldTakeIntoAccountPaddingForLengthOfOutputBufferPassedToDecryptBlock()
3960
{

0 commit comments

Comments
 (0)