init commit
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Crypto.Parameters;
|
||||
using Org.BouncyCastle.Crypto.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Agreement.Kdf;
|
||||
|
||||
public class ConcatenationKdfGenerator : IDerivationFunction
|
||||
{
|
||||
private readonly IDigest mDigest;
|
||||
|
||||
private byte[] mShared;
|
||||
|
||||
private byte[] mOtherInfo;
|
||||
|
||||
private int mHLen;
|
||||
|
||||
public virtual IDigest Digest => mDigest;
|
||||
|
||||
public ConcatenationKdfGenerator(IDigest digest)
|
||||
{
|
||||
mDigest = digest;
|
||||
mHLen = digest.GetDigestSize();
|
||||
}
|
||||
|
||||
public virtual void Init(IDerivationParameters param)
|
||||
{
|
||||
if (!(param is KdfParameters))
|
||||
{
|
||||
throw new ArgumentException("KDF parameters required for ConcatenationKdfGenerator");
|
||||
}
|
||||
KdfParameters kdfParameters = (KdfParameters)param;
|
||||
mShared = kdfParameters.GetSharedSecret();
|
||||
mOtherInfo = kdfParameters.GetIV();
|
||||
}
|
||||
|
||||
public virtual int GenerateBytes(byte[] outBytes, int outOff, int len)
|
||||
{
|
||||
if (outBytes.Length - len < outOff)
|
||||
{
|
||||
throw new DataLengthException("output buffer too small");
|
||||
}
|
||||
byte[] array = new byte[mHLen];
|
||||
byte[] array2 = new byte[4];
|
||||
uint n = 1u;
|
||||
int num = 0;
|
||||
mDigest.Reset();
|
||||
if (len > mHLen)
|
||||
{
|
||||
do
|
||||
{
|
||||
Pack.UInt32_To_BE(n, array2);
|
||||
mDigest.BlockUpdate(array2, 0, array2.Length);
|
||||
mDigest.BlockUpdate(mShared, 0, mShared.Length);
|
||||
mDigest.BlockUpdate(mOtherInfo, 0, mOtherInfo.Length);
|
||||
mDigest.DoFinal(array, 0);
|
||||
Array.Copy(array, 0, outBytes, outOff + num, mHLen);
|
||||
num += mHLen;
|
||||
}
|
||||
while (n++ < len / mHLen);
|
||||
}
|
||||
if (num < len)
|
||||
{
|
||||
Pack.UInt32_To_BE(n, array2);
|
||||
mDigest.BlockUpdate(array2, 0, array2.Length);
|
||||
mDigest.BlockUpdate(mShared, 0, mShared.Length);
|
||||
mDigest.BlockUpdate(mOtherInfo, 0, mOtherInfo.Length);
|
||||
mDigest.DoFinal(array, 0);
|
||||
Array.Copy(array, 0, outBytes, outOff + num, len - num);
|
||||
}
|
||||
return len;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using Org.BouncyCastle.Asn1;
|
||||
|
||||
namespace 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 DerObjectIdentifier Algorithm => algorithm;
|
||||
|
||||
public int KeySize => keySize;
|
||||
|
||||
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;
|
||||
this.extraInfo = extraInfo;
|
||||
}
|
||||
|
||||
public byte[] GetZ()
|
||||
{
|
||||
return z;
|
||||
}
|
||||
|
||||
public byte[] GetExtraInfo()
|
||||
{
|
||||
return extraInfo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Crypto.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Agreement.Kdf;
|
||||
|
||||
public class DHKekGenerator : IDerivationFunction
|
||||
{
|
||||
private readonly IDigest digest;
|
||||
|
||||
private DerObjectIdentifier algorithm;
|
||||
|
||||
private int keySize;
|
||||
|
||||
private byte[] z;
|
||||
|
||||
private byte[] partyAInfo;
|
||||
|
||||
public virtual IDigest Digest => digest;
|
||||
|
||||
public DHKekGenerator(IDigest digest)
|
||||
{
|
||||
this.digest = digest;
|
||||
}
|
||||
|
||||
public virtual void Init(IDerivationParameters param)
|
||||
{
|
||||
DHKdfParameters dHKdfParameters = (DHKdfParameters)param;
|
||||
algorithm = dHKdfParameters.Algorithm;
|
||||
keySize = dHKdfParameters.KeySize;
|
||||
z = dHKdfParameters.GetZ();
|
||||
partyAInfo = dHKdfParameters.GetExtraInfo();
|
||||
}
|
||||
|
||||
public virtual int GenerateBytes(byte[] outBytes, int outOff, int len)
|
||||
{
|
||||
if (outBytes.Length - len < outOff)
|
||||
{
|
||||
throw new DataLengthException("output buffer too small");
|
||||
}
|
||||
long num = len;
|
||||
int digestSize = digest.GetDigestSize();
|
||||
if (num > 8589934591L)
|
||||
{
|
||||
throw new ArgumentException("Output length too large");
|
||||
}
|
||||
int num2 = (int)((num + digestSize - 1) / digestSize);
|
||||
byte[] array = new byte[digest.GetDigestSize()];
|
||||
uint num3 = 1u;
|
||||
for (int i = 0; i < num2; i++)
|
||||
{
|
||||
digest.BlockUpdate(z, 0, z.Length);
|
||||
DerSequence derSequence = new DerSequence(algorithm, new DerOctetString(Pack.UInt32_To_BE(num3)));
|
||||
Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(derSequence);
|
||||
if (partyAInfo != null)
|
||||
{
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: true, 0, new DerOctetString(partyAInfo)));
|
||||
}
|
||||
asn1EncodableVector.Add(new DerTaggedObject(explicitly: true, 2, new DerOctetString(Pack.UInt32_To_BE((uint)keySize))));
|
||||
byte[] derEncoded = new DerSequence(asn1EncodableVector).GetDerEncoded();
|
||||
digest.BlockUpdate(derEncoded, 0, derEncoded.Length);
|
||||
digest.DoFinal(array, 0);
|
||||
if (len > digestSize)
|
||||
{
|
||||
Array.Copy(array, 0, outBytes, outOff, digestSize);
|
||||
outOff += digestSize;
|
||||
len -= digestSize;
|
||||
}
|
||||
else
|
||||
{
|
||||
Array.Copy(array, 0, outBytes, outOff, len);
|
||||
}
|
||||
num3++;
|
||||
}
|
||||
digest.Reset();
|
||||
return (int)num;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Asn1.X509;
|
||||
using Org.BouncyCastle.Crypto.Generators;
|
||||
using Org.BouncyCastle.Crypto.Parameters;
|
||||
using Org.BouncyCastle.Crypto.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Agreement.Kdf;
|
||||
|
||||
public class ECDHKekGenerator : IDerivationFunction
|
||||
{
|
||||
private readonly IDerivationFunction kdf;
|
||||
|
||||
private DerObjectIdentifier algorithm;
|
||||
|
||||
private int keySize;
|
||||
|
||||
private byte[] z;
|
||||
|
||||
public virtual IDigest Digest => kdf.Digest;
|
||||
|
||||
public ECDHKekGenerator(IDigest digest)
|
||||
{
|
||||
kdf = new Kdf2BytesGenerator(digest);
|
||||
}
|
||||
|
||||
public virtual void Init(IDerivationParameters param)
|
||||
{
|
||||
DHKdfParameters dHKdfParameters = (DHKdfParameters)param;
|
||||
algorithm = dHKdfParameters.Algorithm;
|
||||
keySize = dHKdfParameters.KeySize;
|
||||
z = dHKdfParameters.GetZ();
|
||||
}
|
||||
|
||||
public virtual int GenerateBytes(byte[] outBytes, int outOff, int len)
|
||||
{
|
||||
DerSequence derSequence = new DerSequence(new AlgorithmIdentifier(algorithm, DerNull.Instance), new DerTaggedObject(explicitly: true, 2, new DerOctetString(Pack.UInt32_To_BE((uint)keySize))));
|
||||
kdf.Init(new KdfParameters(z, derSequence.GetDerEncoded()));
|
||||
return kdf.GenerateBytes(outBytes, outOff, len);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user