add all
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Agreement.Kdf
|
||||
{
|
||||
/// <summary>Generator for Concatenation Key Derivation Function defined in NIST SP 800-56A, Sect 5.8.1</summary>
|
||||
public sealed class ConcatenationKdfGenerator
|
||||
: IDerivationFunction
|
||||
{
|
||||
private readonly IDigest m_digest;
|
||||
private readonly int m_hLen;
|
||||
|
||||
private byte[] m_buffer;
|
||||
|
||||
/// <param name="digest">the digest to be used as the source of generated bytes</param>
|
||||
public ConcatenationKdfGenerator(IDigest digest)
|
||||
{
|
||||
m_digest = digest;
|
||||
m_hLen = digest.GetDigestSize();
|
||||
}
|
||||
|
||||
public void Init(IDerivationParameters param)
|
||||
{
|
||||
if (!(param is KdfParameters kdfParameters))
|
||||
throw new ArgumentException("KDF parameters required for ConcatenationKdfGenerator");
|
||||
|
||||
byte[] sharedSecret = kdfParameters.GetSharedSecret();
|
||||
byte[] otherInfo = kdfParameters.GetIV();
|
||||
|
||||
m_buffer = new byte[4 + sharedSecret.Length + otherInfo.Length + m_hLen];
|
||||
sharedSecret.CopyTo(m_buffer, 4);
|
||||
otherInfo.CopyTo(m_buffer, 4 + sharedSecret.Length);
|
||||
}
|
||||
|
||||
/// <summary>the underlying digest.</summary>
|
||||
public IDigest Digest => m_digest;
|
||||
|
||||
/// <summary>Fill <c>len</c> bytes of the output buffer with bytes generated from the derivation function.
|
||||
/// </summary>
|
||||
public int GenerateBytes(byte[] output, int outOff, int length)
|
||||
{
|
||||
Check.OutputLength(output, outOff, length, "output buffer too small");
|
||||
|
||||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
|
||||
return GenerateBytes(output.AsSpan(outOff, length));
|
||||
#else
|
||||
int hashPos = m_buffer.Length - m_hLen;
|
||||
uint counter = 1;
|
||||
|
||||
m_digest.Reset();
|
||||
|
||||
int end = outOff + length;
|
||||
int limit = end - m_hLen;
|
||||
|
||||
while (outOff <= limit)
|
||||
{
|
||||
Pack.UInt32_To_BE(counter++, m_buffer, 0);
|
||||
|
||||
m_digest.BlockUpdate(m_buffer, 0, hashPos);
|
||||
m_digest.DoFinal(output, outOff);
|
||||
|
||||
outOff += m_hLen;
|
||||
}
|
||||
|
||||
if (outOff < end)
|
||||
{
|
||||
Pack.UInt32_To_BE(counter, m_buffer, 0);
|
||||
|
||||
m_digest.BlockUpdate(m_buffer, 0, hashPos);
|
||||
m_digest.DoFinal(m_buffer, hashPos);
|
||||
|
||||
Array.Copy(m_buffer, hashPos, output, outOff, end - outOff);
|
||||
}
|
||||
|
||||
return length;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
|
||||
public int GenerateBytes(Span<byte> output)
|
||||
{
|
||||
int hashPos = m_buffer.Length - m_hLen;
|
||||
uint counter = 1;
|
||||
|
||||
m_digest.Reset();
|
||||
|
||||
int pos = 0, length = output.Length, limit = length - m_hLen;
|
||||
|
||||
while (pos <= limit)
|
||||
{
|
||||
Pack.UInt32_To_BE(counter++, m_buffer.AsSpan());
|
||||
|
||||
m_digest.BlockUpdate(m_buffer.AsSpan(0, hashPos));
|
||||
m_digest.DoFinal(output[pos..]);
|
||||
|
||||
pos += m_hLen;
|
||||
}
|
||||
|
||||
if (pos < length)
|
||||
{
|
||||
Pack.UInt32_To_BE(counter, m_buffer.AsSpan());
|
||||
|
||||
m_digest.BlockUpdate(m_buffer.AsSpan(0, hashPos));
|
||||
m_digest.DoFinal(m_buffer.AsSpan(hashPos));
|
||||
m_buffer.AsSpan(hashPos, length - pos).CopyTo(output[pos..]);
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e8557a9f5b3111342a056dc6d2f6a673
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,61 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Agreement.Kdf
|
||||
{
|
||||
public class DHKdfParameters
|
||||
: IDerivationParameters
|
||||
{
|
||||
private readonly DerObjectIdentifier algorithm;
|
||||
private readonly int keySize;
|
||||
private readonly byte[] z;
|
||||
private readonly byte[] extraInfo;
|
||||
|
||||
public DHKdfParameters(
|
||||
DerObjectIdentifier algorithm,
|
||||
int keySize,
|
||||
byte[] z)
|
||||
: this(algorithm, keySize, z, null)
|
||||
{
|
||||
}
|
||||
|
||||
public DHKdfParameters(
|
||||
DerObjectIdentifier algorithm,
|
||||
int keySize,
|
||||
byte[] z,
|
||||
byte[] extraInfo)
|
||||
{
|
||||
this.algorithm = algorithm;
|
||||
this.keySize = keySize;
|
||||
this.z = z; // TODO Clone?
|
||||
this.extraInfo = extraInfo;
|
||||
}
|
||||
|
||||
public DerObjectIdentifier Algorithm
|
||||
{
|
||||
get { return algorithm; }
|
||||
}
|
||||
|
||||
public int KeySize
|
||||
{
|
||||
get { return keySize; }
|
||||
}
|
||||
|
||||
public byte[] GetZ()
|
||||
{
|
||||
// TODO Clone?
|
||||
return z;
|
||||
}
|
||||
|
||||
public byte[] GetExtraInfo()
|
||||
{
|
||||
// TODO Clone?
|
||||
return extraInfo;
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 63ca39658cd060b499a676c8c3fbfd2a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
174
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/crypto/agreement/kdf/DHKekGenerator.cs
vendored
Normal file
174
Packages/com.tivadar.best.http/Runtime/3rdParty/BouncyCastle/crypto/agreement/kdf/DHKekGenerator.cs
vendored
Normal file
@@ -0,0 +1,174 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.IO;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Agreement.Kdf
|
||||
{
|
||||
/**
|
||||
* RFC 2631 Diffie-hellman KEK derivation function.
|
||||
*/
|
||||
public sealed class DHKekGenerator
|
||||
: IDerivationFunction
|
||||
{
|
||||
private readonly IDigest m_digest;
|
||||
|
||||
private DerObjectIdentifier algorithm;
|
||||
private int keySize;
|
||||
private byte[] z;
|
||||
private byte[] partyAInfo;
|
||||
|
||||
public DHKekGenerator(IDigest digest)
|
||||
{
|
||||
m_digest = digest;
|
||||
}
|
||||
|
||||
public void Init(IDerivationParameters param)
|
||||
{
|
||||
DHKdfParameters parameters = (DHKdfParameters)param;
|
||||
|
||||
this.algorithm = parameters.Algorithm;
|
||||
this.keySize = parameters.KeySize;
|
||||
this.z = parameters.GetZ(); // TODO Clone?
|
||||
this.partyAInfo = parameters.GetExtraInfo(); // TODO Clone?
|
||||
}
|
||||
|
||||
public IDigest Digest => m_digest;
|
||||
|
||||
public int GenerateBytes(byte[] outBytes, int outOff, int length)
|
||||
{
|
||||
Check.OutputLength(outBytes, outOff, length, "output buffer too small");
|
||||
|
||||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
|
||||
return GenerateBytes(outBytes.AsSpan(outOff, length));
|
||||
#else
|
||||
long oBytes = length;
|
||||
int digestSize = m_digest.GetDigestSize();
|
||||
|
||||
//
|
||||
// this is at odds with the standard implementation, the
|
||||
// maximum value should be hBits * (2^32 - 1) where hBits
|
||||
// is the digest output size in bits. We can't have an
|
||||
// array with a long index at the moment...
|
||||
//
|
||||
if (oBytes > ((2L << 32) - 1))
|
||||
throw new ArgumentException("Output length too large");
|
||||
|
||||
int cThreshold = (int)((oBytes + digestSize - 1) / digestSize);
|
||||
|
||||
byte[] dig = new byte[digestSize];
|
||||
|
||||
uint counter = 1;
|
||||
|
||||
for (int i = 0; i < cThreshold; i++)
|
||||
{
|
||||
// KeySpecificInfo
|
||||
DerSequence keyInfo = new DerSequence(algorithm, new DerOctetString(Pack.UInt32_To_BE(counter)));
|
||||
|
||||
// OtherInfo
|
||||
Asn1EncodableVector v1 = new Asn1EncodableVector(keyInfo);
|
||||
|
||||
if (partyAInfo != null)
|
||||
{
|
||||
v1.Add(new DerTaggedObject(true, 0, new DerOctetString(partyAInfo)));
|
||||
}
|
||||
|
||||
v1.Add(new DerTaggedObject(true, 2, new DerOctetString(Pack.UInt32_To_BE((uint)keySize))));
|
||||
|
||||
byte[] other = new DerSequence(v1).GetDerEncoded();
|
||||
|
||||
m_digest.BlockUpdate(z, 0, z.Length);
|
||||
m_digest.BlockUpdate(other, 0, other.Length);
|
||||
m_digest.DoFinal(dig, 0);
|
||||
|
||||
if (length > digestSize)
|
||||
{
|
||||
Array.Copy(dig, 0, outBytes, outOff, digestSize);
|
||||
outOff += digestSize;
|
||||
length -= digestSize;
|
||||
}
|
||||
else
|
||||
{
|
||||
Array.Copy(dig, 0, outBytes, outOff, length);
|
||||
}
|
||||
|
||||
counter++;
|
||||
}
|
||||
|
||||
m_digest.Reset();
|
||||
|
||||
return (int)oBytes;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
|
||||
public int GenerateBytes(Span<byte> output)
|
||||
{
|
||||
long oBytes = output.Length;
|
||||
int digestSize = m_digest.GetDigestSize();
|
||||
|
||||
//
|
||||
// this is at odds with the standard implementation, the
|
||||
// maximum value should be hBits * (2^32 - 1) where hBits
|
||||
// is the digest output size in bits. We can't have an
|
||||
// array with a long index at the moment...
|
||||
//
|
||||
if (oBytes > ((2L << 32) - 1))
|
||||
throw new ArgumentException("Output length too large");
|
||||
|
||||
int cThreshold = (int)((oBytes + digestSize - 1) / digestSize);
|
||||
|
||||
Span<byte> dig = digestSize <= 128
|
||||
? stackalloc byte[digestSize]
|
||||
: new byte[digestSize];
|
||||
|
||||
uint counter = 1;
|
||||
|
||||
for (int i = 0; i < cThreshold; i++)
|
||||
{
|
||||
// KeySpecificInfo
|
||||
DerSequence keyInfo = new DerSequence(algorithm, new DerOctetString(Pack.UInt32_To_BE(counter)));
|
||||
|
||||
// OtherInfo
|
||||
Asn1EncodableVector v1 = new Asn1EncodableVector(keyInfo);
|
||||
|
||||
if (partyAInfo != null)
|
||||
{
|
||||
v1.Add(new DerTaggedObject(true, 0, new DerOctetString(partyAInfo)));
|
||||
}
|
||||
|
||||
v1.Add(new DerTaggedObject(true, 2, new DerOctetString(Pack.UInt32_To_BE((uint)keySize))));
|
||||
|
||||
byte[] other = new DerSequence(v1).GetDerEncoded();
|
||||
|
||||
m_digest.BlockUpdate(z);
|
||||
m_digest.BlockUpdate(other);
|
||||
m_digest.DoFinal(dig);
|
||||
|
||||
int remaining = output.Length;
|
||||
if (remaining > digestSize)
|
||||
{
|
||||
dig.CopyTo(output);
|
||||
output = output[digestSize..];
|
||||
}
|
||||
else
|
||||
{
|
||||
dig[..remaining].CopyTo(output);
|
||||
}
|
||||
|
||||
counter++;
|
||||
}
|
||||
|
||||
m_digest.Reset();
|
||||
|
||||
return (int)oBytes;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 60c48eaaa5f539640a6ffb1923dc4cb7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,77 @@
|
||||
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
|
||||
#pragma warning disable
|
||||
using System;
|
||||
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Generators;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
|
||||
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
|
||||
|
||||
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Agreement.Kdf
|
||||
{
|
||||
/**
|
||||
* X9.63 based key derivation function for ECDH CMS.
|
||||
*/
|
||||
public sealed class ECDHKekGenerator
|
||||
: IDerivationFunction
|
||||
{
|
||||
private readonly IDerivationFunction m_kdf;
|
||||
|
||||
private DerObjectIdentifier algorithm;
|
||||
private int keySize;
|
||||
private byte[] z;
|
||||
|
||||
public ECDHKekGenerator(IDigest digest)
|
||||
{
|
||||
m_kdf = new Kdf2BytesGenerator(digest);
|
||||
}
|
||||
|
||||
public void Init(IDerivationParameters param)
|
||||
{
|
||||
DHKdfParameters parameters = (DHKdfParameters)param;
|
||||
|
||||
this.algorithm = parameters.Algorithm;
|
||||
this.keySize = parameters.KeySize;
|
||||
this.z = parameters.GetZ(); // TODO Clone?
|
||||
}
|
||||
|
||||
public IDigest Digest => m_kdf.Digest;
|
||||
|
||||
public int GenerateBytes(byte[] outBytes, int outOff, int length)
|
||||
{
|
||||
Check.OutputLength(outBytes, outOff, length, "output buffer too small");
|
||||
|
||||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
|
||||
return GenerateBytes(outBytes.AsSpan(outOff, length));
|
||||
#else
|
||||
// TODO Create an ASN.1 class for this (RFC3278)
|
||||
// ECC-CMS-SharedInfo
|
||||
DerSequence s = new DerSequence(
|
||||
new AlgorithmIdentifier(algorithm, DerNull.Instance),
|
||||
new DerTaggedObject(true, 2, new DerOctetString(Pack.UInt32_To_BE((uint)keySize))));
|
||||
|
||||
m_kdf.Init(new KdfParameters(z, s.GetDerEncoded()));
|
||||
|
||||
return m_kdf.GenerateBytes(outBytes, outOff, length);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
|
||||
public int GenerateBytes(Span<byte> output)
|
||||
{
|
||||
// TODO Create an ASN.1 class for this (RFC3278)
|
||||
// ECC-CMS-SharedInfo
|
||||
DerSequence s = new DerSequence(
|
||||
new AlgorithmIdentifier(algorithm, DerNull.Instance),
|
||||
new DerTaggedObject(true, 2, new DerOctetString(Pack.UInt32_To_BE((uint)keySize))));
|
||||
|
||||
m_kdf.Init(new KdfParameters(z, s.GetDerEncoded()));
|
||||
|
||||
return m_kdf.GenerateBytes(output);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#pragma warning restore
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f8a010d841cddb4448bc3f9784248d3d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user