From 113ceb51ccc7eaf1a1373d0211bb7bb8a71e81a3 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 2 Feb 2023 10:01:55 +0700 Subject: Refactoring in Crypto.Paddings --- crypto/src/crypto/paddings/ISO10126d2Padding.cs | 37 +++++++---------------- crypto/src/crypto/paddings/ISO7816d4Padding.cs | 27 ++++------------- crypto/src/crypto/paddings/Pkcs7Padding.cs | 23 ++------------- crypto/src/crypto/paddings/TbcPadding.cs | 34 +++------------------ crypto/src/crypto/paddings/X923Padding.cs | 39 +++++++++---------------- 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 + /// A padder that adds ISO10126-2 padding to a block. + 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 - */ + /// + /// 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 + /// 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. - */ + /// A padder that adds PKCS7/PKCS5 padding to a block. 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 { - - /// A padder that adds Trailing-Bit-Compliment padding to a block. - ///

- /// This padding pads the block out compliment of the last bit - /// of the plain text. - ///

- ///
+ /// A padder that adds Trailing-Bit-Compliment padding to a block. + /// This padding pads the block out compliment of the last bit of the plain text. public class TbcPadding : IBlockCipherPadding { - /// Return the name of the algorithm the cipher implements. - /// the name of the algorithm the cipher implements. - /// - public string PaddingName - { - get { return "TBC"; } - } - - /// Initialise the padder. - /// - a SecureRandom if available. - /// public virtual void Init(SecureRandom random) { // nothing to do. } - /// add the pad bytes to the passed in block, returning the number of bytes added. - /// - /// This assumes that the last block of plain text is always passed to it inside . - /// i.e. if is zero, indicating the padding will fill the entire block,the value of - /// should be the same as the last block of plain text. - /// + 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 - /// add the pad bytes to the passed in block, returning the number of bytes added. - /// - /// This assumes that the last block of plain text is always passed to it inside . - /// i.e. if is zero, indicating the padding will fill the entire block,the value of - /// should be the same as the last block of plain text. - /// public virtual int AddPadding(Span 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. - */ + /// + /// 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. + /// 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 { - - /// A padder that adds Null byte padding to a block. - public class ZeroBytePadding : IBlockCipherPadding + /// A padder that adds zero byte padding to a block. + public class ZeroBytePadding + : IBlockCipherPadding { - /// Return the name of the algorithm the cipher implements. - /// - /// - /// the name of the algorithm the cipher implements. - /// - public string PaddingName - { - get { return "ZeroBytePadding"; } - } + public string PaddingName => "ZeroBytePadding"; - /// Initialise the padder. - /// - /// - /// - a SecureRandom if available. - /// public void Init(SecureRandom random) { // nothing to do. -- cgit 1.4.1