This commit is contained in:
2026-06-15 18:18:16 +08:00
parent 97c9fba14e
commit 2b9f134e5f
4164 changed files with 386922 additions and 79 deletions

View File

@@ -0,0 +1,3 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e483ada875227e2498f60f353f72564e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,47 @@
#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>Block cipher padders are expected to conform to this interface.</summary>
public interface IBlockCipherPadding
{
/// <summary>Initialise the padder.</summary>
/// <param name="random">A source of randomness, if any required.</param>
void Init(SecureRandom random);
/// <summary>The name of the algorithm this padder implements.</summary>
string PaddingName { get; }
/// <summary>Add padding to the passed in block.</summary>
/// <param name="input">the block to add padding to.</param>
/// <param name="inOff">the offset into the block the padding is to start at.</param>
/// <returns>the number of bytes of padding added.</returns>
int AddPadding(byte[] input, int inOff);
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
/// <summary>Add padding to the passed in block.</summary>
/// <param name="block">the block to add padding to.</param>
/// <param name="position">the offset into the block the padding is to start at.</param>
/// <returns>the number of bytes of padding added.</returns>
int AddPadding(Span<byte> block, int position);
#endif
/// <summary>Determine the length of padding present in the passed in block.</summary>
/// <param name="input">the block to check padding for.</param>
/// <returns>the number of bytes of padding present.</returns>
int PadCount(byte[] input);
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
/// <summary>Determine the length of padding present in the passed in block.</summary>
/// <param name="block">the block to check padding for.</param>
/// <returns>the number of bytes of padding present.</returns>
int PadCount(ReadOnlySpan<byte> block);
#endif
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e0fc6a16df807e345a633a1bb6708455
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,93 @@
#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
{
/**
* A padder that adds ISO10126-2 padding to a block.
*/
public class ISO10126d2Padding: IBlockCipherPadding
{
private SecureRandom random;
/**
* Initialise the padder.
*
* @param random a SecureRandom if available.
*/
public void Init(
SecureRandom random)
//throws ArgumentException
{
this.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 int AddPadding(byte[] input, int inOff)
{
int count = input.Length - inOff;
if (count > 1)
{
random.NextBytes(input, inOff, count - 1);
}
input[input.Length - 1] = (byte)count;
return count;
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
public int AddPadding(Span<byte> block, int position)
{
int count = block.Length - position;
if (count > 1)
{
random.NextBytes(block[position..(block.Length - 1)]);
}
block[block.Length - 1] = (byte)count;
return count;
}
#endif
public int PadCount(byte[] input)
{
int count = input[input.Length -1];
int position = input.Length - count;
int failed = (position | (count - 1)) >> 31;
if (failed != 0)
throw new InvalidCipherTextException("pad block corrupted");
return count;
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
public int PadCount(ReadOnlySpan<byte> block)
{
int count = block[block.Length - 1];
int position = block.Length - count;
int failed = (position | (count - 1)) >> 31;
if (failed != 0)
throw new InvalidCipherTextException("pad block corrupted");
return count;
}
#endif
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fe1558dfd56858f43a505b6d9c5f3a33
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,101 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
#pragma warning disable
using System;
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
namespace Best.HTTP.SecureProtocol.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
*/
public class ISO7816d4Padding
: IBlockCipherPadding
{
/**
* Initialise the padder.
*
* @param random - a SecureRandom if available.
*/
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 int AddPadding(byte[] input, int inOff)
{
int count = input.Length - inOff;
input[inOff]= 0x80;
while (++inOff < input.Length)
{
input[inOff] = 0x00;
}
return count;
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
public int AddPadding(Span<byte> block, int position)
{
int count = block.Length - position;
block[position++] = 0x80;
block[position..].Fill(0x00);
return count;
}
#endif
public int PadCount(byte[] input)
{
int position = -1, still00Mask = -1;
int i = input.Length;
while (--i >= 0)
{
int next = input[i];
int match00Mask = ((next ^ 0x00) - 1) >> 31;
int match80Mask = ((next ^ 0x80) - 1) >> 31;
position ^= (i ^ position) & still00Mask & match80Mask;
still00Mask &= match00Mask;
}
if (position < 0)
throw new InvalidCipherTextException("pad block corrupted");
return input.Length - position;
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
public int PadCount(ReadOnlySpan<byte> block)
{
int position = -1, still00Mask = -1;
int i = block.Length;
while (--i >= 0)
{
int next = block[i];
int match00Mask = ((next ^ 0x00) - 1) >> 31;
int match80Mask = ((next ^ 0x80) - 1) >> 31;
position ^= (i ^ position) & still00Mask & match80Mask;
still00Mask &= match00Mask;
}
if (position < 0)
throw new InvalidCipherTextException("pad block corrupted");
return block.Length - position;
}
#endif
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 929db33ee95478f4d82801656a2be80d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,388 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
#pragma warning disable
using System;
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Modes;
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Paddings
{
/**
* A wrapper class that allows block ciphers to be used to process data in
* a piecemeal fashion with padding. The PaddedBufferedBlockCipher
* outputs a block only when the buffer is full and more data is being added,
* or on a doFinal (unless the current block in the buffer is a pad block).
* The default padding mechanism used is the one outlined in Pkcs5/Pkcs7.
*/
public class PaddedBufferedBlockCipher
: BufferedBlockCipher
{
private readonly IBlockCipherPadding padding;
public PaddedBufferedBlockCipher(IBlockCipher cipher, IBlockCipherPadding padding)
: this(EcbBlockCipher.GetBlockCipherMode(cipher), padding)
{
}
/**
* Create a buffered block cipher with the desired padding.
*
* @param cipher the underlying block cipher this buffering object wraps.
* @param padding the padding type.
*/
public PaddedBufferedBlockCipher(IBlockCipherMode cipherMode, IBlockCipherPadding padding)
{
m_cipherMode = cipherMode;
this.padding = padding;
buf = new byte[m_cipherMode.GetBlockSize()];
bufOff = 0;
}
/**
* Create a buffered block cipher Pkcs7 padding
*
* @param cipher the underlying block cipher this buffering object wraps.
*/
public PaddedBufferedBlockCipher(IBlockCipherMode cipherMode)
: this(cipherMode, new Pkcs7Padding())
{
}
/**
* 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.
*/
public override void Init(bool forEncryption, ICipherParameters parameters)
{
this.forEncryption = forEncryption;
SecureRandom initRandom = null;
if (parameters is ParametersWithRandom withRandom)
{
initRandom = withRandom.Random;
parameters = withRandom.Parameters;
}
Reset();
padding.Init(initRandom);
m_cipherMode.Init(forEncryption, parameters);
}
/**
* return the minimum 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.
*/
public override int GetOutputSize(
int length)
{
int total = length + bufOff;
int leftOver = total % buf.Length;
if (leftOver == 0)
{
if (forEncryption)
{
return total + buf.Length;
}
return total;
}
return total - leftOver + buf.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.
*/
public override int GetUpdateOutputSize(
int length)
{
int total = length + bufOff;
int leftOver = total % buf.Length;
if (leftOver == 0)
{
return total - buf.Length;
}
return total - leftOver;
}
/**
* 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.
*/
public override int ProcessByte(byte input, byte[] output, int outOff)
{
int resultLen = 0;
if (bufOff == buf.Length)
{
resultLen = m_cipherMode.ProcessBlock(buf, 0, output, outOff);
bufOff = 0;
}
buf[bufOff++] = input;
return resultLen;
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
public override int ProcessByte(byte input, Span<byte> output)
{
int resultLen = 0;
if (bufOff == buf.Length)
{
resultLen = m_cipherMode.ProcessBlock(buf, output);
bufOff = 0;
}
buf[bufOff++] = input;
return resultLen;
}
#endif
/**
* 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.
*/
public override int ProcessBytes(byte[] input, int inOff, int length, byte[] output, int outOff)
{
if (length < 0)
throw new ArgumentException("Can't have a negative input length!");
int blockSize = GetBlockSize();
int outLength = GetUpdateOutputSize(length);
if (outLength > 0)
{
Check.OutputLength(output, outOff, outLength, "output buffer too short");
}
int resultLen = 0;
int gapLen = buf.Length - bufOff;
if (length > gapLen)
{
Array.Copy(input, inOff, buf, bufOff, gapLen);
resultLen = m_cipherMode.ProcessBlock(buf, 0, output, outOff);
bufOff = 0;
length -= gapLen;
inOff += gapLen;
while (length > buf.Length)
{
resultLen += m_cipherMode.ProcessBlock(input, inOff, output, outOff + resultLen);
length -= blockSize;
inOff += blockSize;
}
}
Array.Copy(input, inOff, buf, bufOff, length);
bufOff += length;
return resultLen;
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
public override int ProcessBytes(ReadOnlySpan<byte> input, Span<byte> output)
{
int blockSize = GetBlockSize();
int outLength = GetUpdateOutputSize(input.Length);
if (outLength > 0)
{
Check.OutputLength(output, outLength, "output buffer too short");
}
int resultLen = 0;
int gapLen = buf.Length - bufOff;
if (input.Length > gapLen)
{
input[..gapLen].CopyTo(buf.AsSpan(bufOff));
resultLen = m_cipherMode.ProcessBlock(buf, output);
bufOff = 0;
input = input[gapLen..];
while (input.Length > buf.Length)
{
resultLen += m_cipherMode.ProcessBlock(input, output[resultLen..]);
input = input[blockSize..];
}
}
input.CopyTo(buf.AsSpan(bufOff));
bufOff += input.Length;
return resultLen;
}
#endif
/**
* Process the last block in the buffer. If the buffer is currently
* full and padding needs to be added a call to doFinal will produce
* 2 * GetBlockSize() bytes.
*
* @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 we are decrypting and the input is not block size aligned.
* @exception InvalidOperationException if the underlying cipher is not
* initialised.
* @exception InvalidCipherTextException if padding is expected and not found.
*/
public override int DoFinal(byte[] output, int outOff)
{
int blockSize = m_cipherMode.GetBlockSize();
int resultLen = 0;
if (forEncryption)
{
if (bufOff == blockSize)
{
if ((outOff + 2 * blockSize) > output.Length)
{
Reset();
throw new OutputLengthException("output buffer too short");
}
resultLen = m_cipherMode.ProcessBlock(buf, 0, output, outOff);
bufOff = 0;
}
padding.AddPadding(buf, bufOff);
resultLen += m_cipherMode.ProcessBlock(buf, 0, output, outOff + resultLen);
Reset();
}
else
{
if (bufOff == blockSize)
{
resultLen = m_cipherMode.ProcessBlock(buf, 0, buf, 0);
bufOff = 0;
}
else
{
Reset();
throw new DataLengthException("last block incomplete in decryption");
}
try
{
resultLen -= padding.PadCount(buf);
Array.Copy(buf, 0, output, outOff, resultLen);
}
finally
{
Reset();
}
}
return resultLen;
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
public override int DoFinal(Span<byte> output)
{
int blockSize = m_cipherMode.GetBlockSize();
int resultLen = 0;
if (forEncryption)
{
if (bufOff == blockSize)
{
if ((2 * blockSize) > output.Length)
{
Reset();
throw new OutputLengthException("output buffer too short");
}
resultLen = m_cipherMode.ProcessBlock(buf, output);
bufOff = 0;
}
padding.AddPadding(buf, bufOff);
resultLen += m_cipherMode.ProcessBlock(buf, output[resultLen..]);
Reset();
}
else
{
if (bufOff != blockSize)
{
Reset();
throw new DataLengthException("last block incomplete in decryption");
}
resultLen = m_cipherMode.ProcessBlock(buf, buf);
bufOff = 0;
try
{
resultLen -= padding.PadCount(buf);
buf.AsSpan(0, resultLen).CopyTo(output);
}
finally
{
Reset();
}
}
return resultLen;
}
#endif
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 14b87b703dc44394c9429b41e7d5fe27
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,98 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
#pragma warning disable
using System;
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Paddings
{
/**
* 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)
{
// 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 int AddPadding(byte[] input, int inOff)
{
int count = input.Length - inOff;
byte padValue = (byte)count;
while (inOff < input.Length)
{
input[inOff++] = padValue;
}
return count;
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
public int AddPadding(Span<byte> block, int position)
{
int count = block.Length - position;
byte padValue = (byte)count;
block[position..].Fill(padValue);
return count;
}
#endif
public int PadCount(byte[] input)
{
byte padValue = input[input.Length - 1];
int count = padValue;
int position = input.Length - count;
int failed = (position | (count - 1)) >> 31;
for (int i = 0; i < input.Length; ++i)
{
failed |= (input[i] ^ padValue) & ~((i - position) >> 31);
}
if (failed != 0)
throw new InvalidCipherTextException("pad block corrupted");
return count;
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
public int PadCount(ReadOnlySpan<byte> block)
{
byte padValue = block[block.Length - 1];
int count = padValue;
int position = block.Length - count;
int failed = (position | (count - 1)) >> 31;
for (int i = 0; i < block.Length; ++i)
{
failed |= (block[i] ^ padValue) & ~((i - position) >> 31);
}
if (failed != 0)
throw new InvalidCipherTextException("pad block corrupted");
return count;
}
#endif
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9a8a5b6cb1eae8a4ebf73109d8a79ebc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,105 @@
#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

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2e31faf0c613de74baf79100a792b443
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,107 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
#pragma warning disable
using System;
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
namespace Best.HTTP.SecureProtocol.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.
*/
public class X923Padding
: IBlockCipherPadding
{
private SecureRandom random;
/**
* Initialise the padder.
*
* @param random a SecureRandom if one is available.
*/
public void Init(
SecureRandom random)
{
this.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 int AddPadding(byte[] input, int inOff)
{
int count = input.Length - inOff;
if (count > 1)
{
if (random == null)
{
Arrays.Fill(input, inOff, input.Length - 1, 0x00);
}
else
{
random.NextBytes(input, inOff, count - 1);
}
}
input[input.Length - 1] = (byte)count;
return count;
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
public int AddPadding(Span<byte> block, int position)
{
int count = block.Length - position;
if (count > 1)
{
var body = block[position..(block.Length - 1)];
if (random == null)
{
body.Fill(0x00);
}
else
{
random.NextBytes(body);
}
}
block[block.Length - 1] = (byte)count;
return count;
}
#endif
public int PadCount(byte[] input)
{
int count = input[input.Length - 1];
int position = input.Length - count;
int failed = (position | (count - 1)) >> 31;
if (failed != 0)
throw new InvalidCipherTextException("pad block corrupted");
return count;
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
public int PadCount(ReadOnlySpan<byte> block)
{
int count = block[block.Length - 1];
int position = block.Length - count;
int failed = (position | (count - 1)) >> 31;
if (failed != 0)
throw new InvalidCipherTextException("pad block corrupted");
return count;
}
#endif
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 11591317706d1fd4088d96933bcd7c48
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,86 @@
#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 Null 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"; }
}
/// <summary> Initialise the padder.
///
/// </summary>
/// <param name="random">- a SecureRandom if available.
/// </param>
public void Init(SecureRandom random)
{
// nothing to do.
}
public int AddPadding(byte[] input, int inOff)
{
int added = input.Length - inOff;
while (inOff < input.Length)
{
input[inOff++] = 0x00;
}
return added;
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
public int AddPadding(Span<byte> block, int position)
{
int count = block.Length - position;
block[position..].Fill(0x00);
return count;
}
#endif
public int PadCount(byte[] input)
{
int count = 0, still00Mask = -1;
int i = input.Length;
while (--i >= 0)
{
int next = input[i];
int match00Mask = ((next ^ 0x00) - 1) >> 31;
still00Mask &= match00Mask;
count -= still00Mask;
}
return count;
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
public int PadCount(ReadOnlySpan<byte> block)
{
int count = 0, still00Mask = -1;
int i = block.Length;
while (--i >= 0)
{
int next = block[i];
int match00Mask = ((next ^ 0x00) - 1) >> 31;
still00Mask &= match00Mask;
count -= still00Mask;
}
return count;
}
#endif
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d7f58f8fba737414894011d0f9a4e0de
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: