From ad3de364fd73aaa066e550a3b947bd2776d175b7 Mon Sep 17 00:00:00 2001 From: drieseng Date: Sun, 4 Sep 2016 20:52:23 +0200 Subject: [PATCH] Added Pad overloads taking offset and length. --- .../Ciphers/Paddings/PKCS5PaddingTest.cs | 73 +++++++++++------- .../Ciphers/Paddings/PKCS7PaddingTest.cs | 75 ++++++++++++------- .../Cryptography/Ciphers/CipherPadding.cs | 44 +++++++++-- .../Ciphers/Paddings/PKCS5Padding.cs | 46 ++++++------ .../Ciphers/Paddings/PKCS7Padding.cs | 49 ++++++------ 5 files changed, 182 insertions(+), 105 deletions(-) diff --git a/src/Renci.SshNet.Tests/Classes/Security/Cryptography/Ciphers/Paddings/PKCS5PaddingTest.cs b/src/Renci.SshNet.Tests/Classes/Security/Cryptography/Ciphers/Paddings/PKCS5PaddingTest.cs index 7adb19fe..814062ad 100644 --- a/src/Renci.SshNet.Tests/Classes/Security/Cryptography/Ciphers/Paddings/PKCS5PaddingTest.cs +++ b/src/Renci.SshNet.Tests/Classes/Security/Cryptography/Ciphers/Paddings/PKCS5PaddingTest.cs @@ -1,42 +1,61 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Renci.SshNet.Security.Cryptography.Ciphers.Paddings; -using Renci.SshNet.Tests.Common; namespace Renci.SshNet.Tests.Classes.Security.Cryptography.Ciphers.Paddings { - /// - ///This is a test class for PKCS5PaddingTest and is intended - ///to contain all PKCS5PaddingTest Unit Tests - /// [TestClass] - public class PKCS5PaddingTest : TestBase + public class PKCS5PaddingTest { - /// - ///A test for Pad - /// - [TestMethod] - [Ignore] // placeholder for actual test - public void PadTest() + private PKCS5Padding _padding; + + [TestInitialize] + public void SetUp() { - PKCS5Padding target = new PKCS5Padding(); // TODO: Initialize to an appropriate value - int blockSize = 0; // TODO: Initialize to an appropriate value - byte[] input = null; // TODO: Initialize to an appropriate value - byte[] expected = null; // TODO: Initialize to an appropriate value - byte[] actual; - actual = target.Pad(blockSize, input); - Assert.AreEqual(expected, actual); - Assert.Inconclusive("Verify the correctness of this test method."); + _padding = new PKCS5Padding(); } - /// - ///A test for PKCS5Padding Constructor - /// [TestMethod] - [Ignore] // placeholder - public void PKCS5PaddingConstructorTest() + public void Pad_BlockSizeAndInput_LessThanBlockSize() { - PKCS5Padding target = new PKCS5Padding(); - Assert.Inconclusive("TODO: Implement code to verify target"); + var input = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05}; + var expected = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x03, 0x03, 0x03}; + + var actual = _padding.Pad(8, input); + + Assert.IsTrue(expected.IsEqualTo(actual)); + } + + [TestMethod] + public void Pad_BlockSizeAndInput_MoreThanBlockSizeButNoMultipleOfBlockSize() + { + var input = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }; + var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07 }; + + var actual = _padding.Pad(8, input); + + Assert.IsTrue(expected.IsEqualTo(actual)); + } + + [TestMethod] + public void Pad_BlockSizeAndInputAndOffsetAndLength_LessThanBlockSize() + { + var input = new byte[] { 0x0f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; + var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x03, 0x03, 0x03 }; + + var actual = _padding.Pad(8, input, 1, input.Length - 2); + + Assert.IsTrue(expected.IsEqualTo(actual)); + } + + [TestMethod] + public void Pad_BlockSizeAndInputAndOffsetAndLength_MoreThanBlockSizeButNoMultipleOfBlockSize() + { + var input = new byte[] { 0x0f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 }; + var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07 }; + + var actual = _padding.Pad(8, input, 1, input.Length - 2); + + Assert.IsTrue(expected.IsEqualTo(actual)); } } } diff --git a/src/Renci.SshNet.Tests/Classes/Security/Cryptography/Ciphers/Paddings/PKCS7PaddingTest.cs b/src/Renci.SshNet.Tests/Classes/Security/Cryptography/Ciphers/Paddings/PKCS7PaddingTest.cs index ed048f6b..a04efc16 100644 --- a/src/Renci.SshNet.Tests/Classes/Security/Cryptography/Ciphers/Paddings/PKCS7PaddingTest.cs +++ b/src/Renci.SshNet.Tests/Classes/Security/Cryptography/Ciphers/Paddings/PKCS7PaddingTest.cs @@ -1,42 +1,61 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Renci.SshNet.Security.Cryptography.Ciphers.Paddings; -using Renci.SshNet.Tests.Common; namespace Renci.SshNet.Tests.Classes.Security.Cryptography.Ciphers.Paddings { - /// - ///This is a test class for PKCS7PaddingTest and is intended - ///to contain all PKCS7PaddingTest Unit Tests - /// - [TestClass()] - public class PKCS7PaddingTest : TestBase + [TestClass] + public class PKCS7PaddingTest { - /// - ///A test for Pad - /// - [TestMethod] - [Ignore] // placeholder for actual test - public void PadTest() + private PKCS7Padding _padding; + + [TestInitialize] + public void SetUp() { - PKCS7Padding target = new PKCS7Padding(); // TODO: Initialize to an appropriate value - int blockSize = 0; // TODO: Initialize to an appropriate value - byte[] input = null; // TODO: Initialize to an appropriate value - byte[] expected = null; // TODO: Initialize to an appropriate value - byte[] actual; - actual = target.Pad(blockSize, input); - Assert.AreEqual(expected, actual); - Assert.Inconclusive("Verify the correctness of this test method."); + _padding = new PKCS7Padding(); } - /// - ///A test for PKCS7Padding Constructor - /// [TestMethod] - [Ignore] // placeholder - public void PKCS7PaddingConstructorTest() + public void Pad_BlockSizeAndInput_LessThanBlockSize() { - PKCS7Padding target = new PKCS7Padding(); - Assert.Inconclusive("TODO: Implement code to verify target"); + var input = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 }; + var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x03, 0x03, 0x03 }; + + var actual = _padding.Pad(8, input); + + Assert.IsTrue(expected.IsEqualTo(actual)); + } + + [TestMethod] + public void Pad_BlockSizeAndInput_MoreThanBlockSizeButNoMultipleOfBlockSize() + { + var input = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }; + var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07 }; + + var actual = _padding.Pad(8, input); + + Assert.IsTrue(expected.IsEqualTo(actual)); + } + + [TestMethod] + public void Pad_BlockSizeAndInputAndOffsetAndLength_LessThanBlockSize() + { + var input = new byte[] { 0x0f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; + var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x03, 0x03, 0x03 }; + + var actual = _padding.Pad(8, input, 1, input.Length - 2); + + Assert.IsTrue(expected.IsEqualTo(actual)); + } + + [TestMethod] + public void Pad_BlockSizeAndInputAndOffsetAndLength_MoreThanBlockSizeButNoMultipleOfBlockSize() + { + var input = new byte[] { 0x0f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 }; + var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07 }; + + var actual = _padding.Pad(8, input, 1, input.Length - 2); + + Assert.IsTrue(expected.IsEqualTo(actual)); } } } diff --git a/src/Renci.SshNet/Security/Cryptography/Ciphers/CipherPadding.cs b/src/Renci.SshNet/Security/Cryptography/Ciphers/CipherPadding.cs index 2295300d..5013865c 100644 --- a/src/Renci.SshNet/Security/Cryptography/Ciphers/CipherPadding.cs +++ b/src/Renci.SshNet/Security/Cryptography/Ciphers/CipherPadding.cs @@ -6,21 +6,53 @@ public abstract class CipherPadding { /// - /// Pads specified input to match block size. + /// Pads the specified input to match the block size. /// /// Size of the block. /// The input. - /// Padded data array. - public abstract byte[] Pad(int blockSize, byte[] input); + /// + /// Padded data array. + /// + public byte[] Pad(int blockSize, byte[] input) + { + return Pad(blockSize, input, 0, input.Length); + } /// - /// Pads specified input with a given number of bytes to match the block size. + /// Pads the specified input to match the block size. /// + /// Size of the block. /// The input. - /// The number of bytes to pad the input with. + /// The zero-based offset in at which the data to pad starts. + /// The number of bytes in to take into account. /// /// The padded data array. /// - public abstract byte[] Pad(byte[] input, int length); + public abstract byte[] Pad(int blockSize, byte[] input, int offset, int length); + + /// + /// Pads the specified input with a given number of bytes. + /// + /// The input. + /// The number of bytes to pad the input with. + /// + /// The padded data array. + /// + public byte[] Pad(byte[] input, int paddinglength) + { + return Pad(input, 0, input.Length, paddinglength); + } + + /// + /// Pads the specified input with a given number of bytes. + /// + /// The input. + /// The zero-based offset in at which the data to pad starts. + /// The number of bytes in to take into account. + /// The number of bytes to pad the input with. + /// + /// The padded data array. + /// + public abstract byte[] Pad(byte[] input, int offset, int length, int paddinglength); } } diff --git a/src/Renci.SshNet/Security/Cryptography/Ciphers/Paddings/PKCS5Padding.cs b/src/Renci.SshNet/Security/Cryptography/Ciphers/Paddings/PKCS5Padding.cs index 30e89e32..70d94c20 100644 --- a/src/Renci.SshNet/Security/Cryptography/Ciphers/Paddings/PKCS5Padding.cs +++ b/src/Renci.SshNet/Security/Cryptography/Ciphers/Paddings/PKCS5Padding.cs @@ -8,34 +8,38 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers.Paddings public class PKCS5Padding : CipherPadding { /// - /// Transforms the specified input. + /// Pads the specified input to match the block size. /// - /// Size of the block. + /// The size of the block. /// The input. - /// - /// Padded data array. - /// - public override byte[] Pad(int blockSize, byte[] input) - { - var numOfPaddedBytes = blockSize - (input.Length % blockSize); - return Pad(input, numOfPaddedBytes); - } - - /// - /// Pads specified input with a given number of bytes to match the block size. - /// - /// The input. - /// The number of bytes to pad the input with. + /// The zero-based offset in at which the data to pad starts. + /// The number of bytes in to take into account. /// /// The padded data array. /// - public override byte[] Pad(byte[] input, int length) + public override byte[] Pad(int blockSize, byte[] input, int offset, int length) { - var output = new byte[input.Length + length]; - Buffer.BlockCopy(input, 0, output, 0, input.Length); - for (var i = 0; i < length; i++) + var numOfPaddedBytes = blockSize - (length % blockSize); + return Pad(input, offset, length, numOfPaddedBytes); + } + + /// + /// Pads the specified input with a given number of bytes. + /// + /// The input. + /// The zero-based offset in at which the data to pad starts. + /// The number of bytes in to take into account. + /// The number of bytes to pad the input with. + /// + /// The padded data array. + /// + public override byte[] Pad(byte[] input, int offset, int length, int paddinglength) + { + var output = new byte[length + paddinglength]; + Buffer.BlockCopy(input, offset, output, 0, length); + for (var i = 0; i < paddinglength; i++) { - output[input.Length + i] = (byte) length; + output[length + i] = (byte) paddinglength; } return output; } diff --git a/src/Renci.SshNet/Security/Cryptography/Ciphers/Paddings/PKCS7Padding.cs b/src/Renci.SshNet/Security/Cryptography/Ciphers/Paddings/PKCS7Padding.cs index 8455b929..2623d8fd 100644 --- a/src/Renci.SshNet/Security/Cryptography/Ciphers/Paddings/PKCS7Padding.cs +++ b/src/Renci.SshNet/Security/Cryptography/Ciphers/Paddings/PKCS7Padding.cs @@ -8,36 +8,39 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers.Paddings public class PKCS7Padding : CipherPadding { /// - /// Transforms the specified input. + /// Pads the specified input to match the block size. /// - /// Size of the block. + /// The size of the block. /// The input. - /// - /// Padded data array. - /// - public override byte[] Pad(int blockSize, byte[] input) - { - var numOfPaddedBytes = blockSize - (input.Length % blockSize); - return Pad(input, numOfPaddedBytes); - } - - /// - /// Pads specified input with a given number of bytes to match the block size. - /// - /// The input. - /// The number of bytes to pad the input with. + /// The zero-based offset in at which the data to pad starts. + /// The number of bytes in to take into account. /// /// The padded data array. /// - public override byte[] Pad(byte[] input, int length) + public override byte[] Pad(int blockSize, byte[] input, int offset, int length) { - var output = new byte[input.Length + length]; - Buffer.BlockCopy(input, 0, output, 0, input.Length); - for (var i = 0; i < length; i++) - { - output[input.Length + i] = (byte) length; - } + var numOfPaddedBytes = blockSize - (length % blockSize); + return Pad(input, offset, length, numOfPaddedBytes); + } + /// + /// Pads the specified input with a given number of bytes. + /// + /// The input. + /// The zero-based offset in at which the data to pad starts. + /// The number of bytes in to take into account. + /// The number of bytes to pad the input with. + /// + /// The padded data array. + /// + public override byte[] Pad(byte[] input, int offset, int length, int paddinglength) + { + var output = new byte[length + paddinglength]; + Buffer.BlockCopy(input, offset, output, 0, length); + for (var i = 0; i < paddinglength; i++) + { + output[length + i] = (byte) paddinglength; + } return output; } }