Skip to content

Commit

Permalink
Add common interface for padding (#420)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalkwst authored Oct 17, 2023
1 parent 60d4de0 commit 7de8efb
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 10 deletions.
12 changes: 11 additions & 1 deletion Algorithms.Tests/Crypto/Paddings/Pkcs7PaddingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,20 @@ public void RemovePadding_WhenInvalidPadding_ShouldThrowArgumentException()
.WithMessage("Invalid padding");
}

[Test]
public void GetPaddingCount_WhenArrayIsNull_ShouldThrowArgumentNullException()
{
var padding = new Pkcs7Padding(DefaultBlockSize);

Action act = () => padding.GetPaddingCount(null!);

act.Should().Throw<ArgumentNullException>();
}

[Test]
public void GetPaddingCount_WhenInputArrayIsValid_ShouldReturnCorrectPaddingCount()
{
var paddingSize = 5;
const int paddingSize = 5;
var size32Input = new byte[32];

for (var i = 0; i < paddingSize; i++)
Expand Down
6 changes: 3 additions & 3 deletions Algorithms.Tests/Crypto/Paddings/TbcPaddingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void GetPaddingBytes_WhenCalledWithPaddedData_ShouldReturnCorrectPaddingC
var paddedData = new byte[] { 0x01, 0x02, 0x03, 0xff, 0xff };
const int expectedPaddingCount = 2;

var result = padding.GetPaddingBytes(paddedData);
var result = padding.GetPaddingCount(paddedData);

result.Should().Be(expectedPaddingCount);
}
Expand All @@ -138,7 +138,7 @@ public void GetPaddingBytes_WhenCalledWithUnpaddedData_ShouldReturnZero()
{
var unpaddedData = new byte[] { 0x01, 0x02, 0x03 };

Action action = () => padding.GetPaddingBytes(unpaddedData);
Action action = () => padding.GetPaddingCount(unpaddedData);

action.Should().Throw<ArgumentException>()
.WithMessage("No padding found");
Expand All @@ -149,7 +149,7 @@ public void GetPaddingBytes_WhenCalledWithEmptyArray_ShouldReturnZero()
{
var emptyData = Array.Empty<byte>();

Action action = () => padding.GetPaddingBytes(emptyData);
Action action = () => padding.GetPaddingCount(emptyData);

action.Should().Throw<ArgumentException>()
.WithMessage("No padding found.");
Expand Down
39 changes: 39 additions & 0 deletions Algorithms/Crypto/Paddings/IBlockCipherPadding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;

namespace Algorithms.Crypto.Paddings;

/// <summary>
/// A common interface that all block cipher padding schemes should follow.
/// </summary>
public interface IBlockCipherPadding
{
/// <summary>
/// Adds padding bytes to the end of the given block of the data and returns the number of bytes that were added.
/// </summary>
/// <param name="inputData">The input data array that needs padding.</param>
/// <param name="inputOffset">The offset in the input array where the padding should start.</param>
/// <returns>The number of bytes added.</returns>
/// <remarks>
/// This method expects that the input parameter <paramref name="inputData"/> contains the last block of plain text
/// that needs to be padded. This means that the value of <paramref name="inputData"/> has to have the same value as
/// the last block of plain text. The reason for this is that some modes such as the <see cref="TbcPadding"/> base the
/// padding value on the last byte of the plain text.
/// </remarks>
public int AddPadding(byte[] inputData, int inputOffset);

/// <summary>
/// Removes the padding bytes from the given block of data and returns the original data as a new array.
/// </summary>
/// <param name="inputData">The input data array containing the padding.</param>
/// <returns>The input data without the padding as a new byte array.</returns>
/// <exception cref="ArgumentException">Thrown when the input data has invalid padding.</exception>
public byte[] RemovePadding(byte[] inputData);

/// <summary>
/// Gets the number of padding bytes in the input data.
/// </summary>
/// <param name="input">The input data array that has padding.</param>
/// <returns>The number of padding bytes in the input data.</returns>
/// <exception cref="ArgumentException">Thrown when the input data has invalid padding.</exception>
public int GetPaddingCount(byte[] input);
}
2 changes: 1 addition & 1 deletion Algorithms/Crypto/Paddings/Iso10126D2Padding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Algorithms.Crypto.Paddings;
/// the end of the data.
/// </para>
/// </summary>
public class Iso10126D2Padding
public class Iso10126D2Padding : IBlockCipherPadding
{
/// <summary>
/// Adds random padding to the input data array to make it a multiple of the block size according to the
Expand Down
2 changes: 1 addition & 1 deletion Algorithms/Crypto/Paddings/Iso7816D4Padding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Algorithms.Crypto.Paddings;
/// depend on any specific character encoding or representation.
/// </para>
/// </summary>
public class Iso7816D4Padding
public class Iso7816D4Padding : IBlockCipherPadding
{
/// <summary>
/// Adds padding to the input data according to the ISO 7816-4 standard.
Expand Down
2 changes: 1 addition & 1 deletion Algorithms/Crypto/Paddings/Pkcs7Padding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Algorithms.Crypto.Paddings;
/// padding, such as AES.
/// </para>
/// </summary>
public class Pkcs7Padding
public class Pkcs7Padding : IBlockCipherPadding
{
private readonly int blockSize;

Expand Down
4 changes: 2 additions & 2 deletions Algorithms/Crypto/Paddings/TbcPadding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Algorithms.Crypto.Paddings;
/// The padding bytes are added at the end of the data block until the desired length is reached.
/// </para>
/// </summary>
public class TbcPadding
public class TbcPadding : IBlockCipherPadding
{
/// <summary>
/// Adds padding to the input array according to the TBC standard.
Expand Down Expand Up @@ -121,7 +121,7 @@ public byte[] RemovePadding(byte[] input)
/// avoid branching. If the input array is not padded or has an invalid padding, the method may return incorrect
/// results.
/// </remarks>
public int GetPaddingBytes(byte[] input)
public int GetPaddingCount(byte[] input)
{
var length = input.Length;

Expand Down
2 changes: 1 addition & 1 deletion Algorithms/Crypto/Paddings/X932Padding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Algorithms.Crypto.Paddings;
/// bytes.
/// </para>
/// </summary>
public class X932Padding
public class X932Padding : IBlockCipherPadding
{
private readonly bool useRandomPadding;

Expand Down

0 comments on commit 7de8efb

Please sign in to comment.