diff --git a/crypto/src/crypto/BufferedBlockCipher.cs b/crypto/src/crypto/BufferedBlockCipher.cs index 405f3f3d1..2957b36cb 100644 --- a/crypto/src/crypto/BufferedBlockCipher.cs +++ b/crypto/src/crypto/BufferedBlockCipher.cs @@ -7,41 +7,47 @@ namespace Org.BouncyCastle.Crypto { - /** - * A wrapper class that allows block ciphers to be used to process data in - * a piecemeal fashion. The BufferedBlockCipher outputs a block only when the - * buffer is full and more data is being added, or on a doFinal. - *

- * Note: in the case where the underlying cipher is either a CFB cipher or an - * OFB one the last block may not be a multiple of the block size. - *

- */ + /// + /// A wrapper class that allows block ciphers to be used to process data in a piecemeal fashion. + /// + /// + /// The outputs a block only when the buffer is full and more data is being added, or on a DoFinal. + /// In the case where the underlying cipher is a stream-oriented mode (like CFB or OFB), the last block may not be a multiple of the block size. + /// public class BufferedBlockCipher : BufferedCipherBase { + /// The buffer where data is stored before being processed by the cipher. internal byte[] buf; + /// The current position in the buffer. internal int bufOff; + /// True if initialised for encryption, false for decryption. internal bool forEncryption; + /// The underlying cipher mode we are wrapping. internal IBlockCipherMode m_cipherMode; - /** - * constructor for subclasses - */ + /// + /// Constructor for subclasses. + /// protected BufferedBlockCipher() { } + /// + /// Basic constructor. + /// + /// The underlying block cipher. public BufferedBlockCipher(IBlockCipher cipher) : this(EcbBlockCipher.GetBlockCipherMode(cipher)) { } - /** - * Create a buffered block cipher without padding. - * - * @param cipher the underlying block cipher this buffering object wraps. - * false otherwise. - */ + /// + /// Create a buffered block cipher without padding. + /// + /// The underlying block cipher mode this buffering object wraps. + /// If is null. + /// If the block size is non-positive. public BufferedBlockCipher(IBlockCipherMode cipherMode) { if (cipherMode == null) @@ -56,18 +62,18 @@ public BufferedBlockCipher(IBlockCipherMode cipherMode) bufOff = 0; } + /// + /// The algorithm name for the underlying cipher. + /// + /// The name of the underlying algorithm. public override string AlgorithmName => m_cipherMode.AlgorithmName; - /** - * initialise the cipher. - * - * @param forEncryption if true the cipher is initialised for - * encryption, if false for decryption. - * @param param the key and other data required by the cipher. - * @exception ArgumentException if the parameters argument is - * inappropriate. - */ - // Note: This doubles as the Init in the event that this cipher is being used as an IWrapper + /// + /// Initialise the cipher. + /// + /// If true the cipher is initialised for encryption, if false for decryption. + /// The key and other data required by the cipher. + /// If the parameters argument is inappropriate. public override void Init(bool forEncryption, ICipherParameters parameters) { this.forEncryption = forEncryption; @@ -80,35 +86,32 @@ public override void Init(bool forEncryption, ICipherParameters parameters) m_cipherMode.Init(forEncryption, parameters); } - /** - * return the blocksize for the underlying cipher. - * - * @return the blocksize for the underlying cipher. - */ + /// + /// Return the block size for the underlying cipher. + /// + /// The block size in bytes. public override int GetBlockSize() => m_cipherMode.GetBlockSize(); - /** - * return the size of the output buffer required for an update plus a - * doFinal with an input of len bytes. - * - * @param len the length of the input. - * @return the space required to accommodate a call to update and doFinal - * with len bytes of input. - */ - // Note: Can assume IsPartialBlockOkay is true for purposes of this calculation + /// + /// Return the size of the output buffer required for an update plus a DoFinal with an input of bytes. + /// + /// The length of the input. + /// The space required to accommodate a call to update and DoFinal with bytes of input. public override int GetOutputSize(int length) => bufOff + length; - /** - * return the size of the output buffer required for an update - * an input of len bytes. - * - * @param len the length of the input. - * @return the space required to accommodate a call to update - * with len bytes of input. - */ + /// + /// Return the size of the output buffer required for an update with an input of bytes. + /// + /// The length of the input. + /// The space required to accommodate a call to update with bytes of input. public override int GetUpdateOutputSize(int length) => GetFullBlocksSize(totalSize: bufOff + length, blockSize: buf.Length); + /// + /// Process a single byte, returning the produced output. + /// + /// The input byte. + /// A byte array containing the produced output, or null if no output is produced. public override byte[] ProcessByte(byte input) { int updateOutputSize = GetUpdateOutputSize(1); @@ -123,16 +126,15 @@ public override byte[] ProcessByte(byte input) return output; } - /** - * process a single byte, producing an output block if necessary. - * - * @param in the input byte. - * @param out the space for any output that might be produced. - * @param outOff the offset from which the output will be copied. - * @return the number of output bytes copied to out. - * @exception DataLengthException if there isn't enough space in out. - * @exception InvalidOperationException if the cipher isn't initialised. - */ + /// + /// Process a single byte, producing an output block if necessary. + /// + /// The input byte. + /// The buffer for any output that might be produced. + /// The offset from which the output will be copied. + /// The number of output bytes copied to . + /// If there isn't enough space in . + /// If the cipher isn't initialised. public override int ProcessByte(byte input, byte[] output, int outOff) { #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER @@ -153,6 +155,14 @@ public override int ProcessByte(byte input, byte[] output, int outOff) } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Process a single byte, producing an output block if necessary. + /// + /// The input byte. + /// The span for any output that might be produced. + /// The number of output bytes copied to . + /// If there isn't enough space in . + /// If the cipher isn't initialised. public override int ProcessByte(byte input, Span output) { buf[bufOff++] = input; @@ -169,6 +179,14 @@ public override int ProcessByte(byte input, Span output) } #endif + /// + /// Process an array of bytes, returning the produced output. + /// + /// The input byte array. + /// The offset at which the input data starts. + /// The number of bytes to be processed. + /// A byte array containing the produced output, or null if no output is produced. + /// If is null. public override byte[] ProcessBytes(byte[] input, int inOff, int length) { if (input == null) @@ -188,18 +206,17 @@ public override byte[] ProcessBytes(byte[] input, int inOff, int length) return output; } - /** - * process an array of bytes, producing output if necessary. - * - * @param in the input byte array. - * @param inOff the offset at which the input data starts. - * @param len the number of bytes to be copied out of the input array. - * @param out the space for any output that might be produced. - * @param outOff the offset from which the output will be copied. - * @return the number of output bytes copied to out. - * @exception DataLengthException if there isn't enough space in out. - * @exception InvalidOperationException if the cipher isn't initialised. - */ + /// + /// Process an array of bytes, producing output if necessary. + /// + /// The input byte array. + /// The offset at which the input data starts. + /// The number of bytes to be copied out of the input array. + /// The buffer for any output that might be produced. + /// The offset from which the output will be copied. + /// The number of output bytes copied to . + /// If there isn't enough space in . + /// If the cipher isn't initialised. public override int ProcessBytes(byte[] input, int inOff, int length, byte[] output, int outOff) { if (length < 1) @@ -253,6 +270,14 @@ public override int ProcessBytes(byte[] input, int inOff, int length, byte[] out } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Process a span of bytes, producing output if necessary. + /// + /// The input span. + /// The output span. + /// The number of output bytes produced. + /// If there isn't enough space in . + /// If the cipher isn't initialised. public override int ProcessBytes(ReadOnlySpan input, Span output) { int resultLen = 0; @@ -292,6 +317,11 @@ public override int ProcessBytes(ReadOnlySpan input, Span output) } #endif + /// + /// Process any remaining bytes in the buffer, returning the produced output. + /// + /// A byte array containing the produced output. + /// If the padding is corrupted. public override byte[] DoFinal() { int outputSize = GetOutputSize(0); @@ -310,6 +340,15 @@ public override byte[] DoFinal() return output; } + /// + /// Process an array of bytes plus any remaining bytes in the buffer, returning the produced output. + /// + /// The input byte array. + /// The offset at which the input data starts. + /// The number of bytes to be processed. + /// A byte array containing the produced output. + /// If is null. + /// If the padding is corrupted. public override byte[] DoFinal(byte[] input, int inOff, int inLen) { if (input == null) @@ -333,20 +372,15 @@ public override byte[] DoFinal(byte[] input, int inOff, int inLen) return output; } - /** - * Process the last block in the buffer. - * - * @param out the array the block currently being held is copied into. - * @param outOff the offset at which the copying starts. - * @return the number of output bytes copied to out. - * @exception DataLengthException if there is insufficient space in out for - * the output, or the input is not block size aligned and should be. - * @exception InvalidOperationException if the underlying cipher is not - * initialised. - * @exception InvalidCipherTextException if padding is expected and not found. - * @exception DataLengthException if the input is not block size - * aligned. - */ + /// + /// Process the last block in the buffer. + /// + /// The array the block currently being held is copied into. + /// The offset at which the copying starts. + /// The number of output bytes copied to . + /// If there is insufficient space in , or the input is not block size aligned. + /// If the underlying cipher is not initialised. + /// If padding is expected and not found. public override int DoFinal(byte[] output, int outOff) { #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER @@ -374,6 +408,14 @@ public override int DoFinal(byte[] output, int outOff) } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Process any remaining bytes in the buffer, producing output if necessary. + /// + /// The output span. + /// The number of output bytes produced. + /// If there isn't enough space in , or the input is not block size aligned. + /// If the underlying cipher is not initialised. + /// If padding is expected and not found. public override int DoFinal(Span output) { try @@ -397,10 +439,12 @@ public override int DoFinal(Span output) } #endif - /** - * Reset the buffer and cipher. After resetting the object is in the same - * state as it was after the last init (if there was one). - */ + /// + /// Reset the buffer and cipher. + /// + /// + /// After resetting, the object is in the same state as it was after the last init (if there was one). + /// public override void Reset() { Array.Clear(buf, 0, buf.Length); diff --git a/crypto/src/crypto/modes/CfbBlockCipher.cs b/crypto/src/crypto/modes/CfbBlockCipher.cs index cdb17dd3c..71f0aa951 100644 --- a/crypto/src/crypto/modes/CfbBlockCipher.cs +++ b/crypto/src/crypto/modes/CfbBlockCipher.cs @@ -4,11 +4,11 @@ namespace Org.BouncyCastle.Crypto.Modes { - /** - * implements a Cipher-FeedBack (CFB) mode on top of a simple cipher. - */ + /// + /// Implements a Cipher-FeedBack (CFB) mode on top of a simple block cipher. + /// public class CfbBlockCipher - : IBlockCipherMode + : IBlockCipherMode { private byte[] IV; private byte[] cfbV; @@ -18,13 +18,11 @@ public class CfbBlockCipher private readonly int blockSize; private readonly IBlockCipher cipher; - /** - * Basic constructor. - * - * @param cipher the block cipher to be used as the basis of the - * feedback mode. - * @param blockSize the block size in bits (note: a multiple of 8) - */ + /// + /// Basic constructor. + /// + /// The block cipher to be used as the basis of the feedback mode. + /// The block size in bits (must be a multiple of 8). public CfbBlockCipher( IBlockCipher cipher, int bitBlockSize) @@ -38,24 +36,18 @@ public CfbBlockCipher( this.cfbV = new byte[cipher.GetBlockSize()]; this.cfbOutV = new byte[cipher.GetBlockSize()]; } - /** - * return the underlying block cipher that we are wrapping. - * - * @return the underlying block cipher that we are wrapping. - */ + /// + /// The underlying block cipher that we are wrapping. + /// + /// The underlying block cipher that we are wrapping. public IBlockCipher UnderlyingCipher => cipher; - /** - * Initialise the cipher and, possibly, the initialisation vector (IV). - * If an IV isn't passed as part of the parameter, the IV will be all zeros. - * An IV which is too short is handled in FIPS compliant fashion. - * - * @param forEncryption if true the cipher is initialised for - * encryption, if false for decryption. - * @param param the key and other data required by the cipher. - * @exception ArgumentException if the parameters argument is - * inappropriate. - */ + /// + /// Initialise the cipher and, possibly, the initialisation vector (IV). + /// + /// If true the cipher is initialised for encryption, if false for decryption. + /// The key and other data required by the cipher. + /// If the parameters argument is inappropriate. public void Init( bool forEncryption, ICipherParameters parameters) @@ -79,32 +71,40 @@ public void Init( } } - /** - * return the algorithm name and mode. - * - * @return the name of the underlying algorithm followed by "/CFB" - * and the block size in bits. - */ + /// + /// The algorithm name and mode. + /// + /// The name of the underlying algorithm followed by "/CFB" and the block size in bits. public string AlgorithmName { get { return cipher.AlgorithmName + "/CFB" + (blockSize * 8); } } - public bool IsPartialBlockOkay - { - get { return true; } - } + /// + /// Indicates whether partial blocks are okay for this mode. + /// + public bool IsPartialBlockOkay + { + get { return true; } + } - /** - * return the block size we are operating at. - * - * @return the block size we are operating at (in bytes). - */ + /// + /// Return the block size we are operating at. + /// + /// The block size we are operating at (in bytes). public int GetBlockSize() { return blockSize; } + /// + /// Process a block of data. + /// + /// The input buffer. + /// The offset into the input buffer. + /// The output buffer. + /// The offset into the output buffer. + /// The number of bytes processed. public int ProcessBlock(byte[] input, int inOff, byte[] output, int outOff) { #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER @@ -119,6 +119,12 @@ public int ProcessBlock(byte[] input, int inOff, byte[] output, int outOff) } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Process a block of data using Spans. + /// + /// The input span. + /// The output span. + /// The number of bytes processed. public int ProcessBlock(ReadOnlySpan input, Span output) { return encrypting @@ -213,10 +219,9 @@ private int DecryptBlock(byte[] input, int inOff, byte[] outBytes, int outOff) } #endif - /** - * reset the chaining vector back to the IV and reset the underlying - * cipher. - */ + /// + /// Reset the chaining vector back to the IV and reset the underlying cipher. + /// public void Reset() { Array.Copy(IV, 0, cfbV, 0, IV.Length); diff --git a/crypto/src/crypto/modes/OfbBlockCipher.cs b/crypto/src/crypto/modes/OfbBlockCipher.cs index 9bf4c25c7..62b8c3b36 100644 --- a/crypto/src/crypto/modes/OfbBlockCipher.cs +++ b/crypto/src/crypto/modes/OfbBlockCipher.cs @@ -4,11 +4,11 @@ namespace Org.BouncyCastle.Crypto.Modes { - /** - * implements a Output-FeedBack (OFB) mode on top of a simple cipher. - */ + /// + /// Implements an Output-FeedBack (OFB) mode on top of a simple block cipher. + /// public class OfbBlockCipher - : IBlockCipherMode + : IBlockCipherMode { private byte[] IV; private byte[] ofbV; @@ -17,13 +17,11 @@ public class OfbBlockCipher private readonly int blockSize; private readonly IBlockCipher cipher; - /** - * Basic constructor. - * - * @param cipher the block cipher to be used as the basis of the - * feedback mode. - * @param blockSize the block size in bits (note: a multiple of 8) - */ + /// + /// Basic constructor. + /// + /// The block cipher to be used as the basis of the feedback mode. + /// The block size in bits (must be a multiple of 8). public OfbBlockCipher( IBlockCipher cipher, int blockSize) @@ -36,24 +34,18 @@ public OfbBlockCipher( this.ofbOutV = new byte[cipher.GetBlockSize()]; } - /** - * return the underlying block cipher that we are wrapping. - * - * @return the underlying block cipher that we are wrapping. - */ + /// + /// The underlying block cipher that we are wrapping. + /// + /// The underlying block cipher that we are wrapping. public IBlockCipher UnderlyingCipher => cipher; - /** - * Initialise the cipher and, possibly, the initialisation vector (IV). - * If an IV isn't passed as part of the parameter, the IV will be all zeros. - * An IV which is too short is handled in FIPS compliant fashion. - * - * @param forEncryption if true the cipher is initialised for - * encryption, if false for decryption. - * @param param the key and other data required by the cipher. - * @exception ArgumentException if the parameters argument is - * inappropriate. - */ + /// + /// Initialise the cipher and, possibly, the initialisation vector (IV). + /// + /// Ignored by this OFB mode. + /// The key and other data required by the cipher (ParametersWithIV). + /// If the parameters argument is inappropriate. public void Init( bool forEncryption, //ignored by this OFB mode ICipherParameters parameters) @@ -88,32 +80,40 @@ public void Init( } } - /** - * return the algorithm name and mode. - * - * @return the name of the underlying algorithm followed by "/OFB" - * and the block size in bits - */ + /// + /// The algorithm name and mode. + /// + /// The name of the underlying algorithm followed by "/OFB" and the block size in bits. public string AlgorithmName { get { return cipher.AlgorithmName + "/OFB" + (blockSize * 8); } } - public bool IsPartialBlockOkay - { - get { return true; } - } + /// + /// Indicates whether partial blocks are okay for this mode. + /// + public bool IsPartialBlockOkay + { + get { return true; } + } - /** - * return the block size we are operating at (in bytes). - * - * @return the block size we are operating at (in bytes). - */ + /// + /// Return the block size we are operating at. + /// + /// The block size we are operating at (in bytes). public int GetBlockSize() { return blockSize; } + /// + /// Process a block of data. + /// + /// The input buffer. + /// The offset into the input buffer. + /// The output buffer. + /// The offset into the output buffer. + /// The number of bytes processed. public int ProcessBlock(byte[] input, int inOff, byte[] output, int outOff) { Check.DataLength(input, inOff, blockSize, "input buffer too short"); @@ -140,6 +140,12 @@ public int ProcessBlock(byte[] input, int inOff, byte[] output, int outOff) } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Process a block of data using Spans. + /// + /// The input span. + /// The output span. + /// The number of bytes processed. public int ProcessBlock(ReadOnlySpan input, Span output) { Check.DataLength(input, blockSize, "input buffer too short"); @@ -166,10 +172,9 @@ public int ProcessBlock(ReadOnlySpan input, Span output) } #endif - /** - * reset the feedback vector back to the IV and reset the underlying - * cipher. - */ + /// + /// Reset the feedback vector back to the IV and reset the underlying cipher. + /// public void Reset() { Array.Copy(IV, 0, ofbV, 0, IV.Length); diff --git a/crypto/src/crypto/modes/SicBlockCipher.cs b/crypto/src/crypto/modes/SicBlockCipher.cs index ee204c18c..beb175b02 100644 --- a/crypto/src/crypto/modes/SicBlockCipher.cs +++ b/crypto/src/crypto/modes/SicBlockCipher.cs @@ -6,10 +6,12 @@ namespace Org.BouncyCastle.Crypto.Modes { - /** - * Implements the Segmented Integer Counter (SIC) mode on top of a simple - * block cipher. - */ + /// + /// Implements the Segmented Integer Counter (SIC) mode on top of a simple block cipher. + /// + /// + /// This mode is also known as CTR mode. + /// public class SicBlockCipher : IBlockCipherMode { @@ -19,11 +21,10 @@ public class SicBlockCipher private readonly byte[] counterOut; private byte[] IV; - /** - * Basic constructor. - * - * @param c the block cipher to be used. - */ + /// + /// Basic constructor. + /// + /// The block cipher to be used. public SicBlockCipher(IBlockCipher cipher) { this.cipher = cipher; @@ -33,13 +34,18 @@ public SicBlockCipher(IBlockCipher cipher) this.IV = new byte[blockSize]; } - /** - * return the underlying block cipher that we are wrapping. - * - * @return the underlying block cipher that we are wrapping. - */ + /// + /// The underlying block cipher that we are wrapping. + /// + /// The underlying block cipher that we are wrapping. public IBlockCipher UnderlyingCipher => cipher; + /// + /// Initialise the cipher and, possibly, the initialisation vector (IV). + /// + /// Ignored by CTR mode. + /// The key and other data required by the cipher (ParametersWithIV). + /// If the parameters argument is inappropriate. public virtual void Init( bool forEncryption, //ignored by this CTR mode ICipherParameters parameters) @@ -65,21 +71,40 @@ public virtual void Init( } } + /// + /// The algorithm name and mode. + /// + /// The name of the underlying algorithm followed by "/SIC". public virtual string AlgorithmName { get { return cipher.AlgorithmName + "/SIC"; } } + /// + /// Indicates whether partial blocks are okay for this mode. + /// public virtual bool IsPartialBlockOkay { get { return true; } } + /// + /// Return the block size of the underlying cipher. + /// + /// The block size of the underlying cipher (in bytes). public virtual int GetBlockSize() { return cipher.GetBlockSize(); } + /// + /// Process a block of data. + /// + /// The input buffer. + /// The offset into the input buffer. + /// The output buffer. + /// The offset into the output buffer. + /// The number of bytes processed. public virtual int ProcessBlock(byte[] input, int inOff, byte[] output, int outOff) { cipher.ProcessBlock(counter, 0, counterOut, 0); @@ -102,6 +127,12 @@ public virtual int ProcessBlock(byte[] input, int inOff, byte[] output, int outO } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Process a block of data using Spans. + /// + /// The input span. + /// The output span. + /// The number of bytes processed. public virtual int ProcessBlock(ReadOnlySpan input, Span output) { cipher.ProcessBlock(counter, 0, counterOut, 0); @@ -124,6 +155,9 @@ public virtual int ProcessBlock(ReadOnlySpan input, Span output) } #endif + /// + /// Reset the chaining vector back to the IV and reset the underlying cipher. + /// public virtual void Reset() { Arrays.Fill(counter, (byte)0); diff --git a/crypto/src/crypto/paddings/ISO7816d4Padding.cs b/crypto/src/crypto/paddings/ISO7816d4Padding.cs index 987b69439..62a5de255 100644 --- a/crypto/src/crypto/paddings/ISO7816d4Padding.cs +++ b/crypto/src/crypto/paddings/ISO7816d4Padding.cs @@ -5,19 +5,38 @@ 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 7816-4 - scheme 2 from ISO 9797-1. /// + /// + /// The first byte is 0x80, the rest are 0x00. + /// public class ISO7816d4Padding : IBlockCipherPadding { + /// + /// Initialise the padder. + /// + /// A source of randomness (ignored for ISO7816-4). + /// + /// For this padding scheme, the parameter is ignored. + /// public void Init(SecureRandom random) { // nothing to do. } + /// + /// The algorithm name for the padding. + /// + /// The string "ISO7816-4". public string PaddingName => "ISO7816-4"; + /// + /// Add padding to a given block. + /// + /// The array containing the data to be padded. + /// The offset into the input array where padding should start. + /// The number of bytes of padding added. public int AddPadding(byte[] input, int inOff) { int count = input.Length - inOff; @@ -32,6 +51,12 @@ public int AddPadding(byte[] input, int inOff) } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Add padding to a given block using Spans. + /// + /// The span to be padded. + /// The position where padding should start. + /// The number of bytes of padding added. public int AddPadding(Span block, int position) { int count = block.Length - position; @@ -41,6 +66,12 @@ public int AddPadding(Span block, int position) } #endif + /// + /// Return the number of pad bytes found in the passed in block. + /// + /// The array containing the padded data. + /// The number of pad bytes. + /// If the padding is corrupted. public int PadCount(byte[] input) { int position = -1, still00Mask = -1; @@ -60,6 +91,12 @@ public int PadCount(byte[] input) } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Return the number of pad bytes found in the passed in block using Spans. + /// + /// The read-only span containing the padded data. + /// The number of pad bytes. + /// If the padding is corrupted. public int PadCount(ReadOnlySpan block) { int position = -1, still00Mask = -1; diff --git a/crypto/src/crypto/paddings/Pkcs7Padding.cs b/crypto/src/crypto/paddings/Pkcs7Padding.cs index 61355af34..e4fd84891 100644 --- a/crypto/src/crypto/paddings/Pkcs7Padding.cs +++ b/crypto/src/crypto/paddings/Pkcs7Padding.cs @@ -4,17 +4,36 @@ 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. + /// + /// A source of randomness (ignored for PKCS7). + /// + /// For this padding scheme, the parameter is ignored. + /// public void Init(SecureRandom random) { // nothing to do. } + /// + /// The algorithm name for the padding. + /// + /// The string "PKCS7". public string PaddingName => "PKCS7"; + /// + /// Add padding to a given block. + /// + /// The array containing the data to be padded. + /// The offset into the input array where padding should start. + /// The number of bytes of padding added. public int AddPadding(byte[] input, int inOff) { int count = input.Length - inOff; @@ -29,6 +48,12 @@ public int AddPadding(byte[] input, int inOff) } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Add padding to a given block using Spans. + /// + /// The span to be padded. + /// The position where padding should start. + /// The number of bytes of padding added. public int AddPadding(Span block, int position) { int count = block.Length - position; @@ -38,6 +63,12 @@ public int AddPadding(Span block, int position) } #endif + /// + /// Return the number of pad bytes found in the passed in block. + /// + /// The array containing the padded data. + /// The number of pad bytes. + /// If the padding is corrupted. public int PadCount(byte[] input) { byte padValue = input[input.Length - 1]; @@ -56,6 +87,12 @@ public int PadCount(byte[] input) } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Return the number of pad bytes found in the passed in block using Spans. + /// + /// The read-only span containing the padded data. + /// The number of pad bytes. + /// If the padding is corrupted. public int PadCount(ReadOnlySpan block) { byte padValue = block[block.Length - 1]; diff --git a/crypto/src/crypto/paddings/X923Padding.cs b/crypto/src/crypto/paddings/X923Padding.cs index 24cc54255..671b8254a 100644 --- a/crypto/src/crypto/paddings/X923Padding.cs +++ b/crypto/src/crypto/paddings/X923Padding.cs @@ -6,22 +6,40 @@ 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 is passed in, random padding is used; otherwise, the block is padded with zeros. + /// public class X923Padding - : IBlockCipherPadding + : IBlockCipherPadding { private SecureRandom m_random = null; + /// + /// Initialise the padder. + /// + /// A source of randomness. + /// + /// If is null, zero padding is used; otherwise, the block is padded with random bytes. + /// public void Init(SecureRandom random) { - // NOTE: If random is null, zero padding is used m_random = random; } + /// + /// The algorithm name for the padding. + /// + /// The string "X9.23". public string PaddingName => "X9.23"; + /// + /// Add padding to a given block. + /// + /// The array containing the data to be padded. + /// The offset into the input array where padding should start. + /// The number of bytes of padding added. public int AddPadding(byte[] input, int inOff) { int count = input.Length - inOff; @@ -41,6 +59,12 @@ public int AddPadding(byte[] input, int inOff) } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Add padding to a given block using Spans. + /// + /// The span to be padded. + /// The position where padding should start. + /// The number of bytes of padding added. public int AddPadding(Span block, int position) { int count = block.Length - position; @@ -61,6 +85,12 @@ public int AddPadding(Span block, int position) } #endif + /// + /// Return the number of pad bytes found in the passed in block. + /// + /// The array containing the padded data. + /// The number of pad bytes. + /// If the padding is corrupted. public int PadCount(byte[] input) { int count = input[input.Length - 1]; @@ -74,6 +104,12 @@ public int PadCount(byte[] input) } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Return the number of pad bytes found in the passed in block using Spans. + /// + /// The read-only span containing the padded data. + /// The number of pad bytes. + /// If the padding is corrupted. public int PadCount(ReadOnlySpan block) { int count = block[block.Length - 1]; diff --git a/crypto/src/crypto/parameters/ParametersWithID.cs b/crypto/src/crypto/parameters/ParametersWithID.cs index b21bd60e1..4fef378ad 100644 --- a/crypto/src/crypto/parameters/ParametersWithID.cs +++ b/crypto/src/crypto/parameters/ParametersWithID.cs @@ -1,13 +1,26 @@ -using System; +using System; using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Crypto.Parameters { + /// + /// Wrapper class for parameters which include an Identity (ID). + /// public class ParametersWithID : ICipherParameters { #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Create a new instance using a span action. + /// + /// The type of the state object. + /// The base parameters. + /// The length of the ID in bytes. + /// The state object for the action. + /// The action to initialize the ID. + /// A new . + /// If is null. public static ParametersWithID Create(ICipherParameters parameters, int idLength, TState state, System.Buffers.SpanAction action) { @@ -23,6 +36,11 @@ public static ParametersWithID Create(ICipherParameters parameters, int private readonly ICipherParameters m_parameters; private readonly byte[] m_id; + /// + /// Basic constructor. + /// + /// The base parameters (may be null for key reuse). + /// The identity bytes. public ParametersWithID(ICipherParameters parameters, byte[] id) { // NOTE: 'parameters' may be null to imply key re-use @@ -30,6 +48,13 @@ public ParametersWithID(ICipherParameters parameters, byte[] id) m_id = Arrays.CopyBuffer(id); } + /// + /// Constructor with offset and length for ID. + /// + /// The base parameters (may be null for key reuse). + /// The identity byte array. + /// The offset into the ID array. + /// The length of the ID to use. public ParametersWithID(ICipherParameters parameters, byte[] id, int idOff, int idLen) { // NOTE: 'parameters' may be null to imply key re-use @@ -38,15 +63,23 @@ public ParametersWithID(ICipherParameters parameters, byte[] id, int idOff, int } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Constructor using Span for ID. + /// + /// The base parameters (may be null for key reuse). + /// The identity span. public ParametersWithID(ICipherParameters parameters, ReadOnlySpan id) { // NOTE: 'parameters' may be null to imply key re-use m_parameters = parameters; m_id = id.ToArray(); } -#endif -#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Initialise the ID with a given length. + /// + /// The base parameters. + /// The length of the ID in bytes. private ParametersWithID(ICipherParameters parameters, int idLength) { // NOTE: 'parameters' may be null to imply key re-use @@ -55,16 +88,37 @@ private ParametersWithID(ICipherParameters parameters, int idLength) } #endif + /// + /// Copy the ID into a segment of a destination buffer. + /// + /// The destination buffer. + /// The offset into the destination buffer. + /// The length to copy. public void CopyIDTo(byte[] buf, int off, int len) => Arrays.CopyBufferToSegment(m_id, buf, off, len); + /// + /// Return a copy of the ID. + /// + /// A new array containing the ID bytes. public byte[] GetID() => Arrays.InternalCopyBuffer(m_id); + /// + /// Return the length of the ID in bytes. + /// + /// The length of the ID in bytes. public int IDLength => m_id.Length; #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Allows internal access to the ID as a . + /// internal ReadOnlySpan InternalID => m_id; #endif + /// + /// Return the base parameters associated with this ID. + /// + /// The parameters wrapped by this ID. public ICipherParameters Parameters => m_parameters; } } diff --git a/crypto/src/crypto/parameters/ParametersWithIV.cs b/crypto/src/crypto/parameters/ParametersWithIV.cs index 93dbcf855..8a7f58de9 100644 --- a/crypto/src/crypto/parameters/ParametersWithIV.cs +++ b/crypto/src/crypto/parameters/ParametersWithIV.cs @@ -4,18 +4,30 @@ namespace Org.BouncyCastle.Crypto.Parameters { + /// + /// Wrapper class for parameters which include an Initialisation Vector (IV). + /// public class ParametersWithIV : ICipherParameters { #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - // TODO[api] 'parameter' -> 'parameters' - public static ParametersWithIV Create(ICipherParameters parameter, int ivLength, TState state, + /// + /// Create a new instance using a span action. + /// + /// The type of the state object. + /// The base parameters. + /// The length of the IV in bytes. + /// The state object for the action. + /// The action to initialize the IV. + /// A new . + /// If is null. + public static ParametersWithIV Create(ICipherParameters parameters, int ivLength, TState state, System.Buffers.SpanAction action) { if (action == null) throw new ArgumentNullException(nameof(action)); - ParametersWithIV result = new ParametersWithIV(parameter, ivLength); + ParametersWithIV result = new ParametersWithIV(parameters, ivLength); action(result.m_iv, state); return result; } @@ -24,6 +36,11 @@ public static ParametersWithIV Create(ICipherParameters parameter, int i private readonly ICipherParameters m_parameters; private readonly byte[] m_iv; + /// + /// Basic constructor. + /// + /// The base parameters (may be null for key reuse). + /// The initialization vector. public ParametersWithIV(ICipherParameters parameters, byte[] iv) { // NOTE: 'parameters' may be null to imply key re-use @@ -31,6 +48,13 @@ public ParametersWithIV(ICipherParameters parameters, byte[] iv) m_iv = Arrays.CopyBuffer(iv); } + /// + /// Constructor with offset and length for IV. + /// + /// The base parameters (may be null for key reuse). + /// The initialization vector array. + /// The offset into the IV array. + /// The length of the IV to use. public ParametersWithIV(ICipherParameters parameters, byte[] iv, int ivOff, int ivLen) { // NOTE: 'parameters' may be null to imply key re-use @@ -39,15 +63,23 @@ public ParametersWithIV(ICipherParameters parameters, byte[] iv, int ivOff, int } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Constructor using Span for IV. + /// + /// The base parameters (may be null for key reuse). + /// The initialization vector span. public ParametersWithIV(ICipherParameters parameters, ReadOnlySpan iv) { // NOTE: 'parameters' may be null to imply key re-use m_parameters = parameters; m_iv = iv.ToArray(); } -#endif -#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Initialise the IV with a given length. + /// + /// The base parameters. + /// The length of the IV in bytes. private ParametersWithIV(ICipherParameters parameters, int ivLength) { // NOTE: 'parameters' may be null to imply key re-use @@ -56,16 +88,37 @@ private ParametersWithIV(ICipherParameters parameters, int ivLength) } #endif + /// + /// Copy the IV into a segment of a destination buffer. + /// + /// The destination buffer. + /// The offset into the destination buffer. + /// The length to copy. public void CopyIVTo(byte[] buf, int off, int len) => Arrays.CopyBufferToSegment(m_iv, buf, off, len); + /// + /// Return a copy of the IV. + /// + /// A new array containing the IV bytes. public byte[] GetIV() => Arrays.InternalCopyBuffer(m_iv); #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Allows internal access to the IV as a . + /// internal ReadOnlySpan InternalIV => m_iv; #endif + /// + /// Return the length of the IV in bytes. + /// + /// The length of the IV in bytes. public int IVLength => m_iv.Length; + /// + /// Return the base parameters (e.g., KeyParameter) associated with this IV. + /// + /// The parameters wrapped by this IV. public ICipherParameters Parameters => m_parameters; } } diff --git a/crypto/src/crypto/parameters/ParametersWithRandom.cs b/crypto/src/crypto/parameters/ParametersWithRandom.cs index 53f0fa34c..2a2f92d2e 100644 --- a/crypto/src/crypto/parameters/ParametersWithRandom.cs +++ b/crypto/src/crypto/parameters/ParametersWithRandom.cs @@ -4,25 +4,46 @@ namespace Org.BouncyCastle.Crypto.Parameters { + /// + /// Wrapper class for parameters which include a source of randomness (SecureRandom). + /// public class ParametersWithRandom : ICipherParameters { private readonly ICipherParameters m_parameters; private readonly SecureRandom m_random; + /// + /// Constructor using the default secure random from . + /// + /// The base parameters. public ParametersWithRandom(ICipherParameters parameters) : this(parameters, CryptoServicesRegistrar.GetSecureRandom()) { } + /// + /// Basic constructor. + /// + /// The base parameters. + /// The source of randomness. + /// If or is null. public ParametersWithRandom(ICipherParameters parameters, SecureRandom random) { m_parameters = parameters ?? throw new ArgumentNullException(nameof(parameters)); m_random = random ?? throw new ArgumentNullException(nameof(random)); } + /// + /// Return the base parameters associated with this randomness. + /// + /// The parameters wrapped by this source of randomness. public ICipherParameters Parameters => m_parameters; + /// + /// Return the source of randomness. + /// + /// The source of randomness. public SecureRandom Random => m_random; } } diff --git a/crypto/src/crypto/parameters/ParametersWithSBox.cs b/crypto/src/crypto/parameters/ParametersWithSBox.cs index 4acbb8e36..66be64b43 100644 --- a/crypto/src/crypto/parameters/ParametersWithSBox.cs +++ b/crypto/src/crypto/parameters/ParametersWithSBox.cs @@ -4,10 +4,23 @@ namespace Org.BouncyCastle.Crypto.Parameters { + /// + /// Wrapper class for parameters which include a substitution box (S-Box). + /// public class ParametersWithSBox : ICipherParameters { #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Create a new instance using a span action. + /// + /// The type of the state object. + /// The base parameters. + /// The length of the S-Box in bytes. + /// The state object for the action. + /// The action to initialize the S-Box. + /// A new . + /// If is null. public static ParametersWithSBox Create(ICipherParameters parameters, int sBoxLength, TState state, System.Buffers.SpanAction action) { @@ -23,6 +36,11 @@ public static ParametersWithSBox Create(ICipherParameters parameters, in private readonly ICipherParameters m_parameters; private readonly byte[] m_sBox; + /// + /// Basic constructor. + /// + /// The base parameters (may be null for key reuse). + /// The S-Box bytes. public ParametersWithSBox(ICipherParameters parameters, byte[] sBox) { // NOTE: 'parameters' may be null to imply key re-use @@ -30,6 +48,13 @@ public ParametersWithSBox(ICipherParameters parameters, byte[] sBox) m_sBox = Arrays.CopyBuffer(sBox); } + /// + /// Constructor with offset and length for S-Box. + /// + /// The base parameters (may be null for key reuse). + /// The S-Box byte array. + /// The offset into the S-Box array. + /// The length of the S-Box to use. public ParametersWithSBox(ICipherParameters parameters, byte[] sBox, int sBoxOff, int sBoxLen) { // NOTE: 'parameters' may be null to imply key re-use @@ -38,15 +63,23 @@ public ParametersWithSBox(ICipherParameters parameters, byte[] sBox, int sBoxOff } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Constructor using Span for S-Box. + /// + /// The base parameters (may be null for key reuse). + /// The S-Box span. public ParametersWithSBox(ICipherParameters parameters, ReadOnlySpan sBox) { // NOTE: 'parameters' may be null to imply key re-use m_parameters = parameters; m_sBox = sBox.ToArray(); } -#endif -#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Initialise the S-Box with a given length. + /// + /// The base parameters. + /// The length of the S-Box in bytes. private ParametersWithSBox(ICipherParameters parameters, int sBoxLength) { // NOTE: 'parameters' may be null to imply key re-use @@ -55,16 +88,37 @@ private ParametersWithSBox(ICipherParameters parameters, int sBoxLength) } #endif + /// + /// Copy the S-Box into a segment of a destination buffer. + /// + /// The destination buffer. + /// The offset into the destination buffer. + /// The length to copy. public void CopySBoxTo(byte[] buf, int off, int len) => Arrays.CopyBufferToSegment(m_sBox, buf, off, len); + /// + /// Return a copy of the S-Box. + /// + /// A new array containing the S-Box bytes. public byte[] GetSBox() => Arrays.InternalCopyBuffer(m_sBox); #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Allows internal access to the S-Box as a . + /// internal ReadOnlySpan InternalSBox => m_sBox; #endif + /// + /// Return the base parameters associated with this S-Box. + /// + /// The parameters wrapped by this S-Box. public ICipherParameters Parameters => m_parameters; + /// + /// Return the length of the S-Box in bytes. + /// + /// The length of the S-Box in bytes. public int SBoxLength => m_sBox.Length; } } diff --git a/crypto/src/crypto/parameters/ParametersWithSalt.cs b/crypto/src/crypto/parameters/ParametersWithSalt.cs index dee4369e3..8f9dc7cd6 100644 --- a/crypto/src/crypto/parameters/ParametersWithSalt.cs +++ b/crypto/src/crypto/parameters/ParametersWithSalt.cs @@ -4,11 +4,23 @@ namespace Org.BouncyCastle.Crypto.Parameters { - /// Cipher parameters with a fixed salt value associated with them. + /// + /// Wrapper class for parameters which include a salt value. + /// public class ParametersWithSalt : ICipherParameters { #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Create a new instance using a span action. + /// + /// The type of the state object. + /// The base parameters. + /// The length of the salt in bytes. + /// The state object for the action. + /// The action to initialize the salt. + /// A new . + /// If is null. public static ParametersWithSalt Create(ICipherParameters parameters, int saltLength, TState state, System.Buffers.SpanAction action) { @@ -24,6 +36,11 @@ public static ParametersWithSalt Create(ICipherParameters parameters, in private readonly ICipherParameters m_parameters; private readonly byte[] m_salt; + /// + /// Basic constructor. + /// + /// The base parameters (may be null for key reuse). + /// The salt bytes. public ParametersWithSalt(ICipherParameters parameters, byte[] salt) { // NOTE: 'parameters' may be null to imply key re-use @@ -31,6 +48,13 @@ public ParametersWithSalt(ICipherParameters parameters, byte[] salt) m_salt = Arrays.CopyBuffer(salt); } + /// + /// Constructor with offset and length for salt. + /// + /// The base parameters (may be null for key reuse). + /// The salt byte array. + /// The offset into the salt array. + /// The length of the salt to use. public ParametersWithSalt(ICipherParameters parameters, byte[] salt, int saltOff, int saltLen) { // NOTE: 'parameters' may be null to imply key re-use @@ -39,15 +63,23 @@ public ParametersWithSalt(ICipherParameters parameters, byte[] salt, int saltOff } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Constructor using Span for salt. + /// + /// The base parameters (may be null for key reuse). + /// The salt span. public ParametersWithSalt(ICipherParameters parameters, ReadOnlySpan salt) { // NOTE: 'parameters' may be null to imply key re-use m_parameters = parameters; m_salt = salt.ToArray(); } -#endif -#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Initialise the salt with a given length. + /// + /// The base parameters. + /// The length of the salt in bytes. private ParametersWithSalt(ICipherParameters parameters, int saltLength) { // NOTE: 'parameters' may be null to imply key re-use @@ -56,16 +88,37 @@ private ParametersWithSalt(ICipherParameters parameters, int saltLength) } #endif + /// + /// Copy the salt into a segment of a destination buffer. + /// + /// The destination buffer. + /// The offset into the destination buffer. + /// The length to copy. public void CopySaltTo(byte[] buf, int off, int len) => Arrays.CopyBufferToSegment(m_salt, buf, off, len); + /// + /// Return a copy of the salt. + /// + /// A new array containing the salt bytes. public byte[] GetSalt() => Arrays.InternalCopyBuffer(m_salt); #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + /// Allows internal access to the salt as a . + /// internal ReadOnlySpan InternalSalt => m_salt; #endif + /// + /// Return the base parameters associated with this salt. + /// + /// The parameters wrapped by this salt. public ICipherParameters Parameters => m_parameters; + /// + /// Return the length of the salt in bytes. + /// + /// The length of the salt in bytes. public int SaltLength => m_salt.Length; } }