123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- #pragma warning disable
- using System;
- using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
- namespace Best.HTTP.SecureProtocol.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>
- 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 virtual int AddPadding(byte[] input, int inOff)
- {
- int count = input.Length - inOff;
- byte lastByte = inOff > 0 ? input[inOff - 1] : input[input.Length - 1];
- byte padValue = (byte)((lastByte & 1) - 1);
- while (inOff < input.Length)
- {
- input[inOff++] = padValue;
- }
- return count;
- }
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
- /// <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];
- byte padValue = (byte)((lastByte & 1) - 1);
- var padding = block[position..];
- padding.Fill(padValue);
- return padding.Length;
- }
- #endif
- public virtual int PadCount(byte[] input)
- {
- int i = input.Length;
- int code = input[--i], count = 1, countingMask = -1;
- while (--i >= 0)
- {
- int next = input[i];
- int matchMask = ((next ^ code) - 1) >> 31;
- countingMask &= matchMask;
- count -= countingMask;
- }
- return count;
- }
- #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
- public virtual int PadCount(ReadOnlySpan<byte> block)
- {
- int i = block.Length;
- int code = block[--i], count = 1, countingMask = -1;
- while (--i >= 0)
- {
- int next = block[i];
- int matchMask = ((next ^ code) - 1) >> 31;
- countingMask &= matchMask;
- count -= countingMask;
- }
- return count;
- }
- #endif
- }
- }
- #pragma warning restore
- #endif
|