diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-02-02 10:01:55 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-02-02 10:01:55 +0700 |
commit | 113ceb51ccc7eaf1a1373d0211bb7bb8a71e81a3 (patch) | |
tree | ebe540fbbc60bad0b1c94d516e599036dafae7c7 | |
parent | Refactoring in Crypto.IO (diff) | |
download | BouncyCastle.NET-ed25519-113ceb51ccc7eaf1a1373d0211bb7bb8a71e81a3.tar.xz |
Refactoring in Crypto.Paddings
-rw-r--r-- | crypto/src/crypto/paddings/ISO10126d2Padding.cs | 37 | ||||
-rw-r--r-- | crypto/src/crypto/paddings/ISO7816d4Padding.cs | 27 | ||||
-rw-r--r-- | crypto/src/crypto/paddings/Pkcs7Padding.cs | 23 | ||||
-rw-r--r-- | crypto/src/crypto/paddings/TbcPadding.cs | 34 | ||||
-rw-r--r-- | crypto/src/crypto/paddings/X923Padding.cs | 39 | ||||
-rw-r--r-- | crypto/src/crypto/paddings/ZeroBytePadding.cs | 21 |
6 files changed, 40 insertions, 141 deletions
diff --git a/crypto/src/crypto/paddings/ISO10126d2Padding.cs b/crypto/src/crypto/paddings/ISO10126d2Padding.cs index 21e007f1b..168b7ecc9 100644 --- a/crypto/src/crypto/paddings/ISO10126d2Padding.cs +++ b/crypto/src/crypto/paddings/ISO10126d2Padding.cs @@ -4,42 +4,25 @@ using Org.BouncyCastle.Security; namespace Org.BouncyCastle.Crypto.Paddings { - - /** - * A padder that adds ISO10126-2 padding to a block. - */ - public class ISO10126d2Padding: IBlockCipherPadding + /// <summary>A padder that adds ISO10126-2 padding to a block.</summary> + public class ISO10126d2Padding + : IBlockCipherPadding { - private SecureRandom random; + private SecureRandom m_random = null; - /** - * Initialise the padder. - * - * @param random a SecureRandom if available. - */ - public void Init( - SecureRandom random) - //throws ArgumentException + public void Init(SecureRandom random) { - this.random = CryptoServicesRegistrar.GetSecureRandom(random); + m_random = CryptoServicesRegistrar.GetSecureRandom(random); } - /** - * Return the name of the algorithm the cipher implements. - * - * @return the name of the algorithm the cipher implements. - */ - public string PaddingName - { - get { return "ISO10126-2"; } - } + public string PaddingName => "ISO10126-2"; public int AddPadding(byte[] input, int inOff) { int count = input.Length - inOff; if (count > 1) { - random.NextBytes(input, inOff, count - 1); + m_random.NextBytes(input, inOff, count - 1); } input[input.Length - 1] = (byte)count; @@ -52,7 +35,7 @@ namespace Org.BouncyCastle.Crypto.Paddings int count = block.Length - position; if (count > 1) { - random.NextBytes(block[position..(block.Length - 1)]); + m_random.NextBytes(block[position..(block.Length - 1)]); } block[block.Length - 1] = (byte)count; @@ -62,7 +45,7 @@ namespace Org.BouncyCastle.Crypto.Paddings public int PadCount(byte[] input) { - int count = input[input.Length -1]; + int count = input[input.Length - 1]; int position = input.Length - count; int failed = (position | (count - 1)) >> 31; diff --git a/crypto/src/crypto/paddings/ISO7816d4Padding.cs b/crypto/src/crypto/paddings/ISO7816d4Padding.cs index 7b1834626..987b69439 100644 --- a/crypto/src/crypto/paddings/ISO7816d4Padding.cs +++ b/crypto/src/crypto/paddings/ISO7816d4Padding.cs @@ -1,37 +1,22 @@ using System; -using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Security; namespace Org.BouncyCastle.Crypto.Paddings { - /** - * A padder that adds the padding according to the scheme referenced in - * ISO 7814-4 - scheme 2 from ISO 9797-1. The first byte is 0x80, rest is 0x00 - */ + /// <summary> + /// A padder that adds the padding according to the scheme referenced in ISO 7814-4 - scheme 2 from ISO 9797-1. + /// The first byte is 0x80, rest is 0x00 + /// </summary> public class ISO7816d4Padding : IBlockCipherPadding { - /** - * Initialise the padder. - * - * @param random - a SecureRandom if available. - */ - public void Init( - SecureRandom random) + public void Init(SecureRandom random) { // nothing to do. } - /** - * Return the name of the algorithm the padder implements. - * - * @return the name of the algorithm the padder implements. - */ - public string PaddingName - { - get { return "ISO7816-4"; } - } + public string PaddingName => "ISO7816-4"; public int AddPadding(byte[] input, int inOff) { diff --git a/crypto/src/crypto/paddings/Pkcs7Padding.cs b/crypto/src/crypto/paddings/Pkcs7Padding.cs index 46d97c9eb..61355af34 100644 --- a/crypto/src/crypto/paddings/Pkcs7Padding.cs +++ b/crypto/src/crypto/paddings/Pkcs7Padding.cs @@ -1,36 +1,19 @@ using System; -using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Security; namespace Org.BouncyCastle.Crypto.Paddings { - /** - * A padder that adds Pkcs7/Pkcs5 padding to a block. - */ + /// <summary>A padder that adds PKCS7/PKCS5 padding to a block.</summary> public class Pkcs7Padding : IBlockCipherPadding { - /** - * Initialise the padder. - * - * @param random - a SecureRandom if available. - */ - public void Init( - SecureRandom random) + public void Init(SecureRandom random) { // nothing to do. } - /** - * Return the name of the algorithm the cipher implements. - * - * @return the name of the algorithm the cipher implements. - */ - public string PaddingName - { - get { return "PKCS7"; } - } + public string PaddingName => "PKCS7"; public int AddPadding(byte[] input, int inOff) { diff --git a/crypto/src/crypto/paddings/TbcPadding.cs b/crypto/src/crypto/paddings/TbcPadding.cs index b54c5f4d0..aeefa59a8 100644 --- a/crypto/src/crypto/paddings/TbcPadding.cs +++ b/crypto/src/crypto/paddings/TbcPadding.cs @@ -4,38 +4,18 @@ using Org.BouncyCastle.Security; namespace Org.BouncyCastle.Crypto.Paddings { - - /// <summary> A padder that adds Trailing-Bit-Compliment padding to a block. - /// <p> - /// This padding pads the block out compliment of the last bit - /// of the plain text. - /// </p> - /// </summary> + /// <summary> A padder that adds Trailing-Bit-Compliment padding to a block.</summary> + /// <remarks>This padding pads the block out compliment of the last bit of the plain text.</remarks> public class TbcPadding : IBlockCipherPadding { - /// <summary> Return the name of the algorithm the cipher implements.</summary> - /// <returns> the name of the algorithm the cipher implements. - /// </returns> - public string PaddingName - { - get { return "TBC"; } - } - - /// <summary> Initialise the padder.</summary> - /// <param name="random">- a SecureRandom if available. - /// </param> public virtual void Init(SecureRandom random) { // nothing to do. } - /// <summary> add the pad bytes to the passed in block, returning the number of bytes added.</summary> - /// <remarks> - /// This assumes that the last block of plain text is always passed to it inside <paramref name="input"/>. - /// i.e. if <paramref name="inOff"/> is zero, indicating the padding will fill the entire block,the value of - /// <paramref name="input"/> should be the same as the last block of plain text. - /// </remarks> + public string PaddingName => "TBC"; + public virtual int AddPadding(byte[] input, int inOff) { int count = input.Length - inOff; @@ -51,12 +31,6 @@ namespace Org.BouncyCastle.Crypto.Paddings } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - /// <summary> add the pad bytes to the passed in block, returning the number of bytes added.</summary> - /// <remarks> - /// This assumes that the last block of plain text is always passed to it inside <paramref name="block"/>. - /// i.e. if <paramref name="position"/> is zero, indicating the padding will fill the entire block,the value of - /// <paramref name="block"/> should be the same as the last block of plain text. - /// </remarks> public virtual int AddPadding(Span<byte> block, int position) { byte lastByte = position > 0 ? block[position - 1] : block[block.Length - 1]; diff --git a/crypto/src/crypto/paddings/X923Padding.cs b/crypto/src/crypto/paddings/X923Padding.cs index 12338aa04..24cc54255 100644 --- a/crypto/src/crypto/paddings/X923Padding.cs +++ b/crypto/src/crypto/paddings/X923Padding.cs @@ -5,48 +5,35 @@ using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Crypto.Paddings { - /** - * A padder that adds X9.23 padding to a block - if a SecureRandom is - * passed in random padding is assumed, otherwise padding with zeros is used. - */ + /// <summary> + /// A padder that adds X9.23 padding to a block - if a SecureRandom is passed in random padding is assumed, + /// otherwise padding with zeros is used. + /// </summary> public class X923Padding : IBlockCipherPadding { - private SecureRandom random; + private SecureRandom m_random = null; - /** - * Initialise the padder. - * - * @param random a SecureRandom if one is available. - */ - public void Init( - SecureRandom random) + public void Init(SecureRandom random) { - this.random = random; + // NOTE: If random is null, zero padding is used + m_random = random; } - /** - * Return the name of the algorithm the cipher implements. - * - * @return the name of the algorithm the cipher implements. - */ - public string PaddingName - { - get { return "X9.23"; } - } + public string PaddingName => "X9.23"; public int AddPadding(byte[] input, int inOff) { int count = input.Length - inOff; if (count > 1) { - if (random == null) + if (m_random == null) { Arrays.Fill(input, inOff, input.Length - 1, 0x00); } else { - random.NextBytes(input, inOff, count - 1); + m_random.NextBytes(input, inOff, count - 1); } } input[input.Length - 1] = (byte)count; @@ -60,13 +47,13 @@ namespace Org.BouncyCastle.Crypto.Paddings if (count > 1) { var body = block[position..(block.Length - 1)]; - if (random == null) + if (m_random == null) { body.Fill(0x00); } else { - random.NextBytes(body); + m_random.NextBytes(body); } } block[block.Length - 1] = (byte)count; diff --git a/crypto/src/crypto/paddings/ZeroBytePadding.cs b/crypto/src/crypto/paddings/ZeroBytePadding.cs index 910fe7154..09b1a73ae 100644 --- a/crypto/src/crypto/paddings/ZeroBytePadding.cs +++ b/crypto/src/crypto/paddings/ZeroBytePadding.cs @@ -4,25 +4,12 @@ using Org.BouncyCastle.Security; namespace Org.BouncyCastle.Crypto.Paddings { - - /// <summary> A padder that adds Null byte padding to a block.</summary> - public class ZeroBytePadding : IBlockCipherPadding + /// <summary> A padder that adds zero byte padding to a block.</summary> + public class ZeroBytePadding + : IBlockCipherPadding { - /// <summary> Return the name of the algorithm the cipher implements. - /// - /// </summary> - /// <returns> the name of the algorithm the cipher implements. - /// </returns> - public string PaddingName - { - get { return "ZeroBytePadding"; } - } + public string PaddingName => "ZeroBytePadding"; - /// <summary> Initialise the padder. - /// - /// </summary> - /// <param name="random">- a SecureRandom if available. - /// </param> public void Init(SecureRandom random) { // nothing to do. |