|
@@ -5,6 +5,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
|
|
|
|
using Renci.SshNet.Security.Cryptography;
|
|
using Renci.SshNet.Security.Cryptography;
|
|
|
using Renci.SshNet.Security.Cryptography.Ciphers;
|
|
using Renci.SshNet.Security.Cryptography.Ciphers;
|
|
|
|
|
+using Renci.SshNet.Security.Cryptography.Ciphers.Modes;
|
|
|
using Renci.SshNet.Security.Cryptography.Ciphers.Paddings;
|
|
using Renci.SshNet.Security.Cryptography.Ciphers.Paddings;
|
|
|
using Renci.SshNet.Tests.Common;
|
|
using Renci.SshNet.Tests.Common;
|
|
|
|
|
|
|
@@ -14,7 +15,7 @@ namespace Renci.SshNet.Tests.Classes.Security.Cryptography
|
|
|
public class BlockCipherTest : TestBase
|
|
public class BlockCipherTest : TestBase
|
|
|
{
|
|
{
|
|
|
[TestMethod]
|
|
[TestMethod]
|
|
|
- public void EncryptShouldTakeIntoAccountPaddingForLengthOfOutputBufferPassedToEncryptBlock()
|
|
|
|
|
|
|
+ public void EncryptShouldTakeIntoAccountPaddingForLengthOfInputBufferPassedToEncryptBlock_InputNotDivisible()
|
|
|
{
|
|
{
|
|
|
var input = new byte[] { 0x2c, 0x1a, 0x05, 0x00, 0x68 };
|
|
var input = new byte[] { 0x2c, 0x1a, 0x05, 0x00, 0x68 };
|
|
|
var output = new byte[] { 0x0a, 0x00, 0x03, 0x02, 0x06, 0x08, 0x07, 0x05 };
|
|
var output = new byte[] { 0x0a, 0x00, 0x03, 0x02, 0x06, 0x08, 0x07, 0x05 };
|
|
@@ -23,7 +24,7 @@ namespace Renci.SshNet.Tests.Classes.Security.Cryptography
|
|
|
{
|
|
{
|
|
|
EncryptBlockDelegate = (inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset) =>
|
|
EncryptBlockDelegate = (inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset) =>
|
|
|
{
|
|
{
|
|
|
- Assert.AreEqual(8, outputBuffer.Length);
|
|
|
|
|
|
|
+ Assert.AreEqual(8, inputBuffer.Length);
|
|
|
Buffer.BlockCopy(output, 0, outputBuffer, 0, output.Length);
|
|
Buffer.BlockCopy(output, 0, outputBuffer, 0, output.Length);
|
|
|
return inputBuffer.Length;
|
|
return inputBuffer.Length;
|
|
|
}
|
|
}
|
|
@@ -35,17 +36,61 @@ namespace Renci.SshNet.Tests.Classes.Security.Cryptography
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
|
[TestMethod]
|
|
|
- public void DecryptShouldTakeIntoAccountPaddingForLengthOfOutputBufferPassedToDecryptBlock()
|
|
|
|
|
|
|
+ public void EncryptShouldTakeIntoAccountPaddingForLengthOfInputBufferPassedToEncryptBlock_InputDivisible()
|
|
|
{
|
|
{
|
|
|
- var input = new byte[] { 0x2c, 0x1a, 0x05, 0x00, 0x68 };
|
|
|
|
|
|
|
+ var input = new byte[0];
|
|
|
var output = new byte[] { 0x0a, 0x00, 0x03, 0x02, 0x06, 0x08, 0x07, 0x05 };
|
|
var output = new byte[] { 0x0a, 0x00, 0x03, 0x02, 0x06, 0x08, 0x07, 0x05 };
|
|
|
var key = new byte[] { 0x17, 0x78, 0x56, 0xe1, 0x3e, 0xbd, 0x3e, 0x50, 0x1d, 0x79, 0x3f, 0x0f, 0x55, 0x37, 0x45, 0x54 };
|
|
var key = new byte[] { 0x17, 0x78, 0x56, 0xe1, 0x3e, 0xbd, 0x3e, 0x50, 0x1d, 0x79, 0x3f, 0x0f, 0x55, 0x37, 0x45, 0x54 };
|
|
|
var blockCipher = new BlockCipherStub(key, 8, null, new PKCS7Padding())
|
|
var blockCipher = new BlockCipherStub(key, 8, null, new PKCS7Padding())
|
|
|
|
|
+ {
|
|
|
|
|
+ EncryptBlockDelegate = (inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset) =>
|
|
|
|
|
+ {
|
|
|
|
|
+ Assert.AreEqual(8, inputBuffer.Length);
|
|
|
|
|
+ Buffer.BlockCopy(output, 0, outputBuffer, 0, output.Length);
|
|
|
|
|
+ return inputBuffer.Length;
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ var actual = blockCipher.Encrypt(input);
|
|
|
|
|
+
|
|
|
|
|
+ Assert.IsTrue(output.SequenceEqual(actual));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ [TestMethod]
|
|
|
|
|
+ public void EncryptShouldTakeIntoAccountManualPaddingForLengthOfInputBufferPassedToDecryptBlockAndUnPaddingForTheFinalOutput_CFB()
|
|
|
|
|
+ {
|
|
|
|
|
+ var input = new byte[] { 0x0a, 0x00, 0x03, 0x02, 0x06 };
|
|
|
|
|
+ var output = new byte[] { 0x2c, 0x1a, 0x05, 0x00, 0x68 };
|
|
|
|
|
+ var key = new byte[] { 0x17, 0x78, 0x56, 0xe1, 0x3e, 0xbd, 0x3e, 0x50, 0x1d, 0x79, 0x3f, 0x0f, 0x55, 0x37, 0x45, 0x54 };
|
|
|
|
|
+ var blockCipher = new BlockCipherStub(key, 8, new CfbCipherModeStub(new byte[8]), null)
|
|
|
|
|
+ {
|
|
|
|
|
+ EncryptBlockDelegate = (inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset) =>
|
|
|
|
|
+ {
|
|
|
|
|
+ Assert.AreEqual(8, inputBuffer.Length);
|
|
|
|
|
+ Buffer.BlockCopy(output, 0, outputBuffer, 0, output.Length);
|
|
|
|
|
+ return inputBuffer.Length;
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ var actual = blockCipher.Encrypt(input);
|
|
|
|
|
+
|
|
|
|
|
+ Assert.IsTrue(output.SequenceEqual(actual));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ [TestMethod]
|
|
|
|
|
+ public void DecryptShouldTakeIntoAccountUnPaddingForTheFinalOutput()
|
|
|
|
|
+ {
|
|
|
|
|
+ var input = new byte[] { 0x0a, 0x00, 0x03, 0x02, 0x06, 0x08, 0x07, 0x05 };
|
|
|
|
|
+ var output = new byte[] { 0x2c, 0x1a, 0x05, 0x00, 0x68 };
|
|
|
|
|
+ var padding = new byte[] { 0x03, 0x03, 0x03 };
|
|
|
|
|
+ var key = new byte[] { 0x17, 0x78, 0x56, 0xe1, 0x3e, 0xbd, 0x3e, 0x50, 0x1d, 0x79, 0x3f, 0x0f, 0x55, 0x37, 0x45, 0x54 };
|
|
|
|
|
+ var blockCipher = new BlockCipherStub(key, 8, null, new PKCS7Padding())
|
|
|
{
|
|
{
|
|
|
DecryptBlockDelegate = (inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset) =>
|
|
DecryptBlockDelegate = (inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset) =>
|
|
|
{
|
|
{
|
|
|
Assert.AreEqual(8, outputBuffer.Length);
|
|
Assert.AreEqual(8, outputBuffer.Length);
|
|
|
Buffer.BlockCopy(output, 0, outputBuffer, 0, output.Length);
|
|
Buffer.BlockCopy(output, 0, outputBuffer, 0, output.Length);
|
|
|
|
|
+ Buffer.BlockCopy(padding, 0, outputBuffer, output.Length, padding.Length);
|
|
|
return inputBuffer.Length;
|
|
return inputBuffer.Length;
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
@@ -55,6 +100,27 @@ namespace Renci.SshNet.Tests.Classes.Security.Cryptography
|
|
|
Assert.IsTrue(output.SequenceEqual(actual));
|
|
Assert.IsTrue(output.SequenceEqual(actual));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ [TestMethod]
|
|
|
|
|
+ public void DecryptShouldTakeIntoAccountManualPaddingForLengthOfInputBufferPassedToDecryptBlockAndUnPaddingForTheFinalOutput_CFB()
|
|
|
|
|
+ {
|
|
|
|
|
+ var input = new byte[] { 0x0a, 0x00, 0x03, 0x02, 0x06 };
|
|
|
|
|
+ var output = new byte[] { 0x2c, 0x1a, 0x05, 0x00, 0x68 };
|
|
|
|
|
+ var key = new byte[] { 0x17, 0x78, 0x56, 0xe1, 0x3e, 0xbd, 0x3e, 0x50, 0x1d, 0x79, 0x3f, 0x0f, 0x55, 0x37, 0x45, 0x54 };
|
|
|
|
|
+ var blockCipher = new BlockCipherStub(key, 8, new CfbCipherModeStub(new byte[8]), null)
|
|
|
|
|
+ {
|
|
|
|
|
+ DecryptBlockDelegate = (inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset) =>
|
|
|
|
|
+ {
|
|
|
|
|
+ Assert.AreEqual(8, inputBuffer.Length);
|
|
|
|
|
+ Buffer.BlockCopy(output, 0, outputBuffer, 0, output.Length);
|
|
|
|
|
+ return inputBuffer.Length;
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ var actual = blockCipher.Decrypt(input);
|
|
|
|
|
+
|
|
|
|
|
+ Assert.IsTrue(output.SequenceEqual(actual));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
|
|
|
private class BlockCipherStub : BlockCipher
|
|
private class BlockCipherStub : BlockCipher
|
|
|
{
|
|
{
|
|
@@ -75,5 +141,23 @@ namespace Renci.SshNet.Tests.Classes.Security.Cryptography
|
|
|
return DecryptBlockDelegate(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
|
|
return DecryptBlockDelegate(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ private class CfbCipherModeStub : CfbCipherMode
|
|
|
|
|
+ {
|
|
|
|
|
+ public CfbCipherModeStub(byte[] iv)
|
|
|
|
|
+ : base(iv)
|
|
|
|
|
+ {
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public override int EncryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
|
|
|
|
|
+ {
|
|
|
|
|
+ return Cipher.EncryptBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public override int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
|
|
|
|
|
+ {
|
|
|
|
|
+ return Cipher.DecryptBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|