init commit
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class AeadParameters : ICipherParameters
|
||||
{
|
||||
private readonly byte[] associatedText;
|
||||
|
||||
private readonly byte[] nonce;
|
||||
|
||||
private readonly KeyParameter key;
|
||||
|
||||
private readonly int macSize;
|
||||
|
||||
public virtual KeyParameter Key => key;
|
||||
|
||||
public virtual int MacSize => macSize;
|
||||
|
||||
public AeadParameters(KeyParameter key, int macSize, byte[] nonce)
|
||||
: this(key, macSize, nonce, null)
|
||||
{
|
||||
}
|
||||
|
||||
public AeadParameters(KeyParameter key, int macSize, byte[] nonce, byte[] associatedText)
|
||||
{
|
||||
this.key = key;
|
||||
this.nonce = nonce;
|
||||
this.macSize = macSize;
|
||||
this.associatedText = associatedText;
|
||||
}
|
||||
|
||||
public virtual byte[] GetAssociatedText()
|
||||
{
|
||||
return associatedText;
|
||||
}
|
||||
|
||||
public virtual byte[] GetNonce()
|
||||
{
|
||||
return nonce;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
[Obsolete("Use AeadParameters")]
|
||||
public class CcmParameters : AeadParameters
|
||||
{
|
||||
public CcmParameters(KeyParameter key, int macSize, byte[] nonce, byte[] associatedText)
|
||||
: base(key, macSize, nonce, associatedText)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using Org.BouncyCastle.Security;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class DHKeyGenerationParameters : KeyGenerationParameters
|
||||
{
|
||||
private readonly DHParameters parameters;
|
||||
|
||||
public DHParameters Parameters => parameters;
|
||||
|
||||
public DHKeyGenerationParameters(SecureRandom random, DHParameters parameters)
|
||||
: base(random, GetStrength(parameters))
|
||||
{
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
internal static int GetStrength(DHParameters parameters)
|
||||
{
|
||||
if (parameters.L == 0)
|
||||
{
|
||||
return parameters.P.BitLength;
|
||||
}
|
||||
return parameters.L;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Asn1.Pkcs;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class DHKeyParameters : AsymmetricKeyParameter
|
||||
{
|
||||
private readonly DHParameters parameters;
|
||||
|
||||
private readonly DerObjectIdentifier algorithmOid;
|
||||
|
||||
public DHParameters Parameters => parameters;
|
||||
|
||||
public DerObjectIdentifier AlgorithmOid => algorithmOid;
|
||||
|
||||
protected DHKeyParameters(bool isPrivate, DHParameters parameters)
|
||||
: this(isPrivate, parameters, PkcsObjectIdentifiers.DhKeyAgreement)
|
||||
{
|
||||
}
|
||||
|
||||
protected DHKeyParameters(bool isPrivate, DHParameters parameters, DerObjectIdentifier algorithmOid)
|
||||
: base(isPrivate)
|
||||
{
|
||||
this.parameters = parameters;
|
||||
this.algorithmOid = algorithmOid;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is DHKeyParameters other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Equals(other);
|
||||
}
|
||||
|
||||
protected bool Equals(DHKeyParameters other)
|
||||
{
|
||||
if (object.Equals(parameters, other.parameters))
|
||||
{
|
||||
return Equals((AsymmetricKeyParameter)other);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
int num = base.GetHashCode();
|
||||
if (parameters != null)
|
||||
{
|
||||
num ^= parameters.GetHashCode();
|
||||
}
|
||||
return num;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Math;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class DHParameters : ICipherParameters
|
||||
{
|
||||
private const int DefaultMinimumLength = 160;
|
||||
|
||||
private readonly BigInteger p;
|
||||
|
||||
private readonly BigInteger g;
|
||||
|
||||
private readonly BigInteger q;
|
||||
|
||||
private readonly BigInteger j;
|
||||
|
||||
private readonly int m;
|
||||
|
||||
private readonly int l;
|
||||
|
||||
private readonly DHValidationParameters validation;
|
||||
|
||||
public BigInteger P => p;
|
||||
|
||||
public BigInteger G => g;
|
||||
|
||||
public BigInteger Q => q;
|
||||
|
||||
public BigInteger J => j;
|
||||
|
||||
public int M => m;
|
||||
|
||||
public int L => l;
|
||||
|
||||
public DHValidationParameters ValidationParameters => validation;
|
||||
|
||||
private static int GetDefaultMParam(int lParam)
|
||||
{
|
||||
if (lParam == 0)
|
||||
{
|
||||
return 160;
|
||||
}
|
||||
return System.Math.Min(lParam, 160);
|
||||
}
|
||||
|
||||
public DHParameters(BigInteger p, BigInteger g)
|
||||
: this(p, g, null, 0)
|
||||
{
|
||||
}
|
||||
|
||||
public DHParameters(BigInteger p, BigInteger g, BigInteger q)
|
||||
: this(p, g, q, 0)
|
||||
{
|
||||
}
|
||||
|
||||
public DHParameters(BigInteger p, BigInteger g, BigInteger q, int l)
|
||||
: this(p, g, q, GetDefaultMParam(l), l, null, null)
|
||||
{
|
||||
}
|
||||
|
||||
public DHParameters(BigInteger p, BigInteger g, BigInteger q, int m, int l)
|
||||
: this(p, g, q, m, l, null, null)
|
||||
{
|
||||
}
|
||||
|
||||
public DHParameters(BigInteger p, BigInteger g, BigInteger q, BigInteger j, DHValidationParameters validation)
|
||||
: this(p, g, q, 160, 0, j, validation)
|
||||
{
|
||||
}
|
||||
|
||||
public DHParameters(BigInteger p, BigInteger g, BigInteger q, int m, int l, BigInteger j, DHValidationParameters validation)
|
||||
{
|
||||
if (p == null)
|
||||
{
|
||||
throw new ArgumentNullException("p");
|
||||
}
|
||||
if (g == null)
|
||||
{
|
||||
throw new ArgumentNullException("g");
|
||||
}
|
||||
if (!p.TestBit(0))
|
||||
{
|
||||
throw new ArgumentException("field must be an odd prime", "p");
|
||||
}
|
||||
if (g.CompareTo(BigInteger.Two) < 0 || g.CompareTo(p.Subtract(BigInteger.Two)) > 0)
|
||||
{
|
||||
throw new ArgumentException("generator must in the range [2, p - 2]", "g");
|
||||
}
|
||||
if (q != null && q.BitLength >= p.BitLength)
|
||||
{
|
||||
throw new ArgumentException("q too big to be a factor of (p-1)", "q");
|
||||
}
|
||||
if (m >= p.BitLength)
|
||||
{
|
||||
throw new ArgumentException("m value must be < bitlength of p", "m");
|
||||
}
|
||||
if (l != 0)
|
||||
{
|
||||
if (l >= p.BitLength)
|
||||
{
|
||||
throw new ArgumentException("when l value specified, it must be less than bitlength(p)", "l");
|
||||
}
|
||||
if (l < m)
|
||||
{
|
||||
throw new ArgumentException("when l value specified, it may not be less than m value", "l");
|
||||
}
|
||||
}
|
||||
if (j != null && j.CompareTo(BigInteger.Two) < 0)
|
||||
{
|
||||
throw new ArgumentException("subgroup factor must be >= 2", "j");
|
||||
}
|
||||
this.p = p;
|
||||
this.g = g;
|
||||
this.q = q;
|
||||
this.m = m;
|
||||
this.l = l;
|
||||
this.j = j;
|
||||
this.validation = validation;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is DHParameters other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Equals(other);
|
||||
}
|
||||
|
||||
protected virtual bool Equals(DHParameters other)
|
||||
{
|
||||
if (p.Equals(other.p) && g.Equals(other.g))
|
||||
{
|
||||
return object.Equals(q, other.q);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
int num = p.GetHashCode() ^ g.GetHashCode();
|
||||
if (q != null)
|
||||
{
|
||||
num ^= q.GetHashCode();
|
||||
}
|
||||
return num;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Math;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class DHPrivateKeyParameters : DHKeyParameters
|
||||
{
|
||||
private readonly BigInteger x;
|
||||
|
||||
public BigInteger X => x;
|
||||
|
||||
public DHPrivateKeyParameters(BigInteger x, DHParameters parameters)
|
||||
: base(isPrivate: true, parameters)
|
||||
{
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public DHPrivateKeyParameters(BigInteger x, DHParameters parameters, DerObjectIdentifier algorithmOid)
|
||||
: base(isPrivate: true, parameters, algorithmOid)
|
||||
{
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is DHPrivateKeyParameters other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Equals(other);
|
||||
}
|
||||
|
||||
protected bool Equals(DHPrivateKeyParameters other)
|
||||
{
|
||||
if (x.Equals(other.x))
|
||||
{
|
||||
return Equals((DHKeyParameters)other);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return x.GetHashCode() ^ base.GetHashCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Math;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class DHPublicKeyParameters : DHKeyParameters
|
||||
{
|
||||
private readonly BigInteger y;
|
||||
|
||||
public virtual BigInteger Y => y;
|
||||
|
||||
private static BigInteger Validate(BigInteger y, DHParameters dhParams)
|
||||
{
|
||||
if (y == null)
|
||||
{
|
||||
throw new ArgumentNullException("y");
|
||||
}
|
||||
if (y.CompareTo(BigInteger.Two) < 0 || y.CompareTo(dhParams.P.Subtract(BigInteger.Two)) > 0)
|
||||
{
|
||||
throw new ArgumentException("invalid DH public key", "y");
|
||||
}
|
||||
if (dhParams.Q != null && !y.ModPow(dhParams.Q, dhParams.P).Equals(BigInteger.One))
|
||||
{
|
||||
throw new ArgumentException("y value does not appear to be in correct group", "y");
|
||||
}
|
||||
return y;
|
||||
}
|
||||
|
||||
public DHPublicKeyParameters(BigInteger y, DHParameters parameters)
|
||||
: base(isPrivate: false, parameters)
|
||||
{
|
||||
this.y = Validate(y, parameters);
|
||||
}
|
||||
|
||||
public DHPublicKeyParameters(BigInteger y, DHParameters parameters, DerObjectIdentifier algorithmOid)
|
||||
: base(isPrivate: false, parameters, algorithmOid)
|
||||
{
|
||||
this.y = Validate(y, parameters);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is DHPublicKeyParameters other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Equals(other);
|
||||
}
|
||||
|
||||
protected bool Equals(DHPublicKeyParameters other)
|
||||
{
|
||||
if (y.Equals(other.y))
|
||||
{
|
||||
return Equals((DHKeyParameters)other);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return y.GetHashCode() ^ base.GetHashCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class DHValidationParameters
|
||||
{
|
||||
private readonly byte[] seed;
|
||||
|
||||
private readonly int counter;
|
||||
|
||||
public int Counter => counter;
|
||||
|
||||
public DHValidationParameters(byte[] seed, int counter)
|
||||
{
|
||||
if (seed == null)
|
||||
{
|
||||
throw new ArgumentNullException("seed");
|
||||
}
|
||||
this.seed = (byte[])seed.Clone();
|
||||
this.counter = counter;
|
||||
}
|
||||
|
||||
public byte[] GetSeed()
|
||||
{
|
||||
return (byte[])seed.Clone();
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is DHValidationParameters other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Equals(other);
|
||||
}
|
||||
|
||||
protected bool Equals(DHValidationParameters other)
|
||||
{
|
||||
if (counter == other.counter)
|
||||
{
|
||||
return Arrays.AreEqual(seed, other.seed);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
int num = counter;
|
||||
return num.GetHashCode() ^ Arrays.GetHashCode(seed);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class DesEdeParameters : DesParameters
|
||||
{
|
||||
public const int DesEdeKeyLength = 24;
|
||||
|
||||
private static byte[] FixKey(byte[] key, int keyOff, int keyLen)
|
||||
{
|
||||
byte[] array = new byte[24];
|
||||
switch (keyLen)
|
||||
{
|
||||
case 16:
|
||||
Array.Copy(key, keyOff, array, 0, 16);
|
||||
Array.Copy(key, keyOff, array, 16, 8);
|
||||
break;
|
||||
case 24:
|
||||
Array.Copy(key, keyOff, array, 0, 24);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("Bad length for DESede key: " + keyLen, "keyLen");
|
||||
}
|
||||
if (IsWeakKey(array))
|
||||
{
|
||||
throw new ArgumentException("attempt to create weak DESede key");
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public DesEdeParameters(byte[] key)
|
||||
: base(FixKey(key, 0, key.Length))
|
||||
{
|
||||
}
|
||||
|
||||
public DesEdeParameters(byte[] key, int keyOff, int keyLen)
|
||||
: base(FixKey(key, keyOff, keyLen))
|
||||
{
|
||||
}
|
||||
|
||||
public static bool IsWeakKey(byte[] key, int offset, int length)
|
||||
{
|
||||
for (int i = offset; i < length; i += 8)
|
||||
{
|
||||
if (DesParameters.IsWeakKey(key, i))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public new static bool IsWeakKey(byte[] key, int offset)
|
||||
{
|
||||
return IsWeakKey(key, offset, key.Length - offset);
|
||||
}
|
||||
|
||||
public new static bool IsWeakKey(byte[] key)
|
||||
{
|
||||
return IsWeakKey(key, 0, key.Length);
|
||||
}
|
||||
|
||||
public static bool IsRealEdeKey(byte[] key, int offset)
|
||||
{
|
||||
if (key.Length != 16)
|
||||
{
|
||||
return IsReal3Key(key, offset);
|
||||
}
|
||||
return IsReal2Key(key, offset);
|
||||
}
|
||||
|
||||
public static bool IsReal2Key(byte[] key, int offset)
|
||||
{
|
||||
bool flag = false;
|
||||
for (int i = offset; i != offset + 8; i++)
|
||||
{
|
||||
flag |= key[i] != key[i + 8];
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
public static bool IsReal3Key(byte[] key, int offset)
|
||||
{
|
||||
bool flag = false;
|
||||
bool flag2 = false;
|
||||
bool flag3 = false;
|
||||
for (int i = offset; i != offset + 8; i++)
|
||||
{
|
||||
flag |= key[i] != key[i + 8];
|
||||
flag2 |= key[i] != key[i + 16];
|
||||
flag3 |= key[i + 8] != key[i + 16];
|
||||
}
|
||||
if (flag && flag2)
|
||||
{
|
||||
return flag3;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
using System;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class DesParameters : KeyParameter
|
||||
{
|
||||
public const int DesKeyLength = 8;
|
||||
|
||||
private const int N_DES_WEAK_KEYS = 16;
|
||||
|
||||
private static readonly byte[] DES_weak_keys = new byte[128]
|
||||
{
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 31, 31,
|
||||
31, 31, 14, 14, 14, 14, 224, 224, 224, 224,
|
||||
241, 241, 241, 241, 254, 254, 254, 254, 254, 254,
|
||||
254, 254, 1, 254, 1, 254, 1, 254, 1, 254,
|
||||
31, 224, 31, 224, 14, 241, 14, 241, 1, 224,
|
||||
1, 224, 1, 241, 1, 241, 31, 254, 31, 254,
|
||||
14, 254, 14, 254, 1, 31, 1, 31, 1, 14,
|
||||
1, 14, 224, 254, 224, 254, 241, 254, 241, 254,
|
||||
254, 1, 254, 1, 254, 1, 254, 1, 224, 31,
|
||||
224, 31, 241, 14, 241, 14, 224, 1, 224, 1,
|
||||
241, 1, 241, 1, 254, 31, 254, 31, 254, 14,
|
||||
254, 14, 31, 1, 31, 1, 14, 1, 14, 1,
|
||||
254, 224, 254, 224, 254, 241, 254, 241
|
||||
};
|
||||
|
||||
public DesParameters(byte[] key)
|
||||
: base(key)
|
||||
{
|
||||
if (IsWeakKey(key))
|
||||
{
|
||||
throw new ArgumentException("attempt to create weak DES key");
|
||||
}
|
||||
}
|
||||
|
||||
public DesParameters(byte[] key, int keyOff, int keyLen)
|
||||
: base(key, keyOff, keyLen)
|
||||
{
|
||||
if (IsWeakKey(key, keyOff))
|
||||
{
|
||||
throw new ArgumentException("attempt to create weak DES key");
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsWeakKey(byte[] key, int offset)
|
||||
{
|
||||
if (key.Length - offset < 8)
|
||||
{
|
||||
throw new ArgumentException("key material too short.");
|
||||
}
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
bool flag = false;
|
||||
for (int j = 0; j < 8; j++)
|
||||
{
|
||||
if (key[j + offset] != DES_weak_keys[i * 8 + j])
|
||||
{
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!flag)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool IsWeakKey(byte[] key)
|
||||
{
|
||||
return IsWeakKey(key, 0);
|
||||
}
|
||||
|
||||
public static byte SetOddParity(byte b)
|
||||
{
|
||||
uint num = (uint)(b ^ 1);
|
||||
num ^= num >> 4;
|
||||
num ^= num >> 2;
|
||||
num ^= num >> 1;
|
||||
num &= 1;
|
||||
return (byte)(b ^ num);
|
||||
}
|
||||
|
||||
public static void SetOddParity(byte[] bytes)
|
||||
{
|
||||
for (int i = 0; i < bytes.Length; i++)
|
||||
{
|
||||
bytes[i] = SetOddParity(bytes[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetOddParity(byte[] bytes, int off, int len)
|
||||
{
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
bytes[off + i] = SetOddParity(bytes[off + i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using Org.BouncyCastle.Security;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class DsaKeyGenerationParameters : KeyGenerationParameters
|
||||
{
|
||||
private readonly DsaParameters parameters;
|
||||
|
||||
public DsaParameters Parameters => parameters;
|
||||
|
||||
public DsaKeyGenerationParameters(SecureRandom random, DsaParameters parameters)
|
||||
: base(random, parameters.P.BitLength - 1)
|
||||
{
|
||||
this.parameters = parameters;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public abstract class DsaKeyParameters : AsymmetricKeyParameter
|
||||
{
|
||||
private readonly DsaParameters parameters;
|
||||
|
||||
public DsaParameters Parameters => parameters;
|
||||
|
||||
protected DsaKeyParameters(bool isPrivate, DsaParameters parameters)
|
||||
: base(isPrivate)
|
||||
{
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is DsaKeyParameters other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Equals(other);
|
||||
}
|
||||
|
||||
protected bool Equals(DsaKeyParameters other)
|
||||
{
|
||||
if (object.Equals(parameters, other.parameters))
|
||||
{
|
||||
return Equals((AsymmetricKeyParameter)other);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
int num = base.GetHashCode();
|
||||
if (parameters != null)
|
||||
{
|
||||
num ^= parameters.GetHashCode();
|
||||
}
|
||||
return num;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Org.BouncyCastle.Security;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class DsaParameterGenerationParameters
|
||||
{
|
||||
public const int DigitalSignatureUsage = 1;
|
||||
|
||||
public const int KeyEstablishmentUsage = 2;
|
||||
|
||||
private readonly int l;
|
||||
|
||||
private readonly int n;
|
||||
|
||||
private readonly int certainty;
|
||||
|
||||
private readonly SecureRandom random;
|
||||
|
||||
private readonly int usageIndex;
|
||||
|
||||
public virtual int L => l;
|
||||
|
||||
public virtual int N => n;
|
||||
|
||||
public virtual int UsageIndex => usageIndex;
|
||||
|
||||
public virtual int Certainty => certainty;
|
||||
|
||||
public virtual SecureRandom Random => random;
|
||||
|
||||
public DsaParameterGenerationParameters(int L, int N, int certainty, SecureRandom random)
|
||||
: this(L, N, certainty, random, -1)
|
||||
{
|
||||
}
|
||||
|
||||
public DsaParameterGenerationParameters(int L, int N, int certainty, SecureRandom random, int usageIndex)
|
||||
{
|
||||
l = L;
|
||||
n = N;
|
||||
this.certainty = certainty;
|
||||
this.random = random;
|
||||
this.usageIndex = usageIndex;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Math;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class DsaParameters : ICipherParameters
|
||||
{
|
||||
private readonly BigInteger p;
|
||||
|
||||
private readonly BigInteger q;
|
||||
|
||||
private readonly BigInteger g;
|
||||
|
||||
private readonly DsaValidationParameters validation;
|
||||
|
||||
public BigInteger P => p;
|
||||
|
||||
public BigInteger Q => q;
|
||||
|
||||
public BigInteger G => g;
|
||||
|
||||
public DsaValidationParameters ValidationParameters => validation;
|
||||
|
||||
public DsaParameters(BigInteger p, BigInteger q, BigInteger g)
|
||||
: this(p, q, g, null)
|
||||
{
|
||||
}
|
||||
|
||||
public DsaParameters(BigInteger p, BigInteger q, BigInteger g, DsaValidationParameters parameters)
|
||||
{
|
||||
if (p == null)
|
||||
{
|
||||
throw new ArgumentNullException("p");
|
||||
}
|
||||
if (q == null)
|
||||
{
|
||||
throw new ArgumentNullException("q");
|
||||
}
|
||||
if (g == null)
|
||||
{
|
||||
throw new ArgumentNullException("g");
|
||||
}
|
||||
this.p = p;
|
||||
this.q = q;
|
||||
this.g = g;
|
||||
validation = parameters;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is DsaParameters other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Equals(other);
|
||||
}
|
||||
|
||||
protected bool Equals(DsaParameters other)
|
||||
{
|
||||
if (p.Equals(other.p) && q.Equals(other.q))
|
||||
{
|
||||
return g.Equals(other.g);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return p.GetHashCode() ^ q.GetHashCode() ^ g.GetHashCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Math;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class DsaPrivateKeyParameters : DsaKeyParameters
|
||||
{
|
||||
private readonly BigInteger x;
|
||||
|
||||
public BigInteger X => x;
|
||||
|
||||
public DsaPrivateKeyParameters(BigInteger x, DsaParameters parameters)
|
||||
: base(isPrivate: true, parameters)
|
||||
{
|
||||
if (x == null)
|
||||
{
|
||||
throw new ArgumentNullException("x");
|
||||
}
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is DsaPrivateKeyParameters other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Equals(other);
|
||||
}
|
||||
|
||||
protected bool Equals(DsaPrivateKeyParameters other)
|
||||
{
|
||||
if (x.Equals(other.x))
|
||||
{
|
||||
return Equals((DsaKeyParameters)other);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return x.GetHashCode() ^ base.GetHashCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Math;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class DsaPublicKeyParameters : DsaKeyParameters
|
||||
{
|
||||
private readonly BigInteger y;
|
||||
|
||||
public BigInteger Y => y;
|
||||
|
||||
private static BigInteger Validate(BigInteger y, DsaParameters parameters)
|
||||
{
|
||||
if (parameters != null && (y.CompareTo(BigInteger.Two) < 0 || y.CompareTo(parameters.P.Subtract(BigInteger.Two)) > 0 || !y.ModPow(parameters.Q, parameters.P).Equals(BigInteger.One)))
|
||||
{
|
||||
throw new ArgumentException("y value does not appear to be in correct group");
|
||||
}
|
||||
return y;
|
||||
}
|
||||
|
||||
public DsaPublicKeyParameters(BigInteger y, DsaParameters parameters)
|
||||
: base(isPrivate: false, parameters)
|
||||
{
|
||||
if (y == null)
|
||||
{
|
||||
throw new ArgumentNullException("y");
|
||||
}
|
||||
this.y = Validate(y, parameters);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is DsaPublicKeyParameters other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Equals(other);
|
||||
}
|
||||
|
||||
protected bool Equals(DsaPublicKeyParameters other)
|
||||
{
|
||||
if (y.Equals(other.y))
|
||||
{
|
||||
return Equals((DsaKeyParameters)other);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return y.GetHashCode() ^ base.GetHashCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class DsaValidationParameters
|
||||
{
|
||||
private readonly byte[] seed;
|
||||
|
||||
private readonly int counter;
|
||||
|
||||
private readonly int usageIndex;
|
||||
|
||||
public virtual int Counter => counter;
|
||||
|
||||
public virtual int UsageIndex => usageIndex;
|
||||
|
||||
public DsaValidationParameters(byte[] seed, int counter)
|
||||
: this(seed, counter, -1)
|
||||
{
|
||||
}
|
||||
|
||||
public DsaValidationParameters(byte[] seed, int counter, int usageIndex)
|
||||
{
|
||||
if (seed == null)
|
||||
{
|
||||
throw new ArgumentNullException("seed");
|
||||
}
|
||||
this.seed = (byte[])seed.Clone();
|
||||
this.counter = counter;
|
||||
this.usageIndex = usageIndex;
|
||||
}
|
||||
|
||||
public virtual byte[] GetSeed()
|
||||
{
|
||||
return (byte[])seed.Clone();
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is DsaValidationParameters other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Equals(other);
|
||||
}
|
||||
|
||||
protected virtual bool Equals(DsaValidationParameters other)
|
||||
{
|
||||
if (counter == other.counter)
|
||||
{
|
||||
return Arrays.AreEqual(seed, other.seed);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
int num = counter;
|
||||
return num.GetHashCode() ^ Arrays.GetHashCode(seed);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Math;
|
||||
using Org.BouncyCastle.Math.EC;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class ECDomainParameters
|
||||
{
|
||||
internal ECCurve curve;
|
||||
|
||||
internal byte[] seed;
|
||||
|
||||
internal ECPoint g;
|
||||
|
||||
internal BigInteger n;
|
||||
|
||||
internal BigInteger h;
|
||||
|
||||
internal BigInteger hInv;
|
||||
|
||||
public ECCurve Curve => curve;
|
||||
|
||||
public ECPoint G => g;
|
||||
|
||||
public BigInteger N => n;
|
||||
|
||||
public BigInteger H => h;
|
||||
|
||||
public BigInteger HInv
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (this)
|
||||
{
|
||||
if (hInv == null)
|
||||
{
|
||||
hInv = h.ModInverse(n);
|
||||
}
|
||||
return hInv;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ECDomainParameters(ECCurve curve, ECPoint g, BigInteger n)
|
||||
: this(curve, g, n, BigInteger.One, null)
|
||||
{
|
||||
}
|
||||
|
||||
public ECDomainParameters(ECCurve curve, ECPoint g, BigInteger n, BigInteger h)
|
||||
: this(curve, g, n, h, null)
|
||||
{
|
||||
}
|
||||
|
||||
public ECDomainParameters(ECCurve curve, ECPoint g, BigInteger n, BigInteger h, byte[] seed)
|
||||
{
|
||||
if (curve == null)
|
||||
{
|
||||
throw new ArgumentNullException("curve");
|
||||
}
|
||||
if (g == null)
|
||||
{
|
||||
throw new ArgumentNullException("g");
|
||||
}
|
||||
if (n == null)
|
||||
{
|
||||
throw new ArgumentNullException("n");
|
||||
}
|
||||
this.curve = curve;
|
||||
this.g = Validate(curve, g);
|
||||
this.n = n;
|
||||
this.h = h;
|
||||
this.seed = Arrays.Clone(seed);
|
||||
}
|
||||
|
||||
public byte[] GetSeed()
|
||||
{
|
||||
return Arrays.Clone(seed);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is ECDomainParameters other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Equals(other);
|
||||
}
|
||||
|
||||
protected virtual bool Equals(ECDomainParameters other)
|
||||
{
|
||||
if (curve.Equals(other.curve) && g.Equals(other.g) && n.Equals(other.n))
|
||||
{
|
||||
return h.Equals(other.h);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
int hashCode = curve.GetHashCode();
|
||||
hashCode *= 37;
|
||||
hashCode ^= g.GetHashCode();
|
||||
hashCode *= 37;
|
||||
hashCode ^= n.GetHashCode();
|
||||
hashCode *= 37;
|
||||
return hashCode ^ h.GetHashCode();
|
||||
}
|
||||
|
||||
internal static ECPoint Validate(ECCurve c, ECPoint q)
|
||||
{
|
||||
if (q == null)
|
||||
{
|
||||
throw new ArgumentException("Point has null value", "q");
|
||||
}
|
||||
q = ECAlgorithms.ImportPoint(c, q).Normalize();
|
||||
if (q.IsInfinity)
|
||||
{
|
||||
throw new ArgumentException("Point at infinity", "q");
|
||||
}
|
||||
if (!q.IsValid())
|
||||
{
|
||||
throw new ArgumentException("Point not on curve", "q");
|
||||
}
|
||||
return q;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using Org.BouncyCastle.Asn1;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class ECGost3410Parameters : ECNamedDomainParameters
|
||||
{
|
||||
private readonly DerObjectIdentifier _publicKeyParamSet;
|
||||
|
||||
private readonly DerObjectIdentifier _digestParamSet;
|
||||
|
||||
private readonly DerObjectIdentifier _encryptionParamSet;
|
||||
|
||||
public DerObjectIdentifier PublicKeyParamSet => _publicKeyParamSet;
|
||||
|
||||
public DerObjectIdentifier DigestParamSet => _digestParamSet;
|
||||
|
||||
public DerObjectIdentifier EncryptionParamSet => _encryptionParamSet;
|
||||
|
||||
public ECGost3410Parameters(ECNamedDomainParameters dp, DerObjectIdentifier publicKeyParamSet, DerObjectIdentifier digestParamSet, DerObjectIdentifier encryptionParamSet)
|
||||
: base(dp.Name, dp.Curve, dp.G, dp.N, dp.H, dp.GetSeed())
|
||||
{
|
||||
_publicKeyParamSet = publicKeyParamSet;
|
||||
_digestParamSet = digestParamSet;
|
||||
_encryptionParamSet = encryptionParamSet;
|
||||
}
|
||||
|
||||
public ECGost3410Parameters(ECDomainParameters dp, DerObjectIdentifier publicKeyParamSet, DerObjectIdentifier digestParamSet, DerObjectIdentifier encryptionParamSet)
|
||||
: base(publicKeyParamSet, dp.Curve, dp.G, dp.N, dp.H, dp.GetSeed())
|
||||
{
|
||||
_publicKeyParamSet = publicKeyParamSet;
|
||||
_digestParamSet = digestParamSet;
|
||||
_encryptionParamSet = encryptionParamSet;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Security;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class ECKeyGenerationParameters : KeyGenerationParameters
|
||||
{
|
||||
private readonly ECDomainParameters domainParams;
|
||||
|
||||
private readonly DerObjectIdentifier publicKeyParamSet;
|
||||
|
||||
public ECDomainParameters DomainParameters => domainParams;
|
||||
|
||||
public DerObjectIdentifier PublicKeyParamSet => publicKeyParamSet;
|
||||
|
||||
public ECKeyGenerationParameters(ECDomainParameters domainParameters, SecureRandom random)
|
||||
: base(random, domainParameters.N.BitLength)
|
||||
{
|
||||
domainParams = domainParameters;
|
||||
}
|
||||
|
||||
public ECKeyGenerationParameters(DerObjectIdentifier publicKeyParamSet, SecureRandom random)
|
||||
: this(ECKeyParameters.LookupParameters(publicKeyParamSet), random)
|
||||
{
|
||||
this.publicKeyParamSet = publicKeyParamSet;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Asn1.CryptoPro;
|
||||
using Org.BouncyCastle.Asn1.X9;
|
||||
using Org.BouncyCastle.Crypto.Generators;
|
||||
using Org.BouncyCastle.Security;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public abstract class ECKeyParameters : AsymmetricKeyParameter
|
||||
{
|
||||
private static readonly string[] algorithms = new string[6] { "EC", "ECDSA", "ECDH", "ECDHC", "ECGOST3410", "ECMQV" };
|
||||
|
||||
private readonly string algorithm;
|
||||
|
||||
private readonly ECDomainParameters parameters;
|
||||
|
||||
private readonly DerObjectIdentifier publicKeyParamSet;
|
||||
|
||||
public string AlgorithmName => algorithm;
|
||||
|
||||
public ECDomainParameters Parameters => parameters;
|
||||
|
||||
public DerObjectIdentifier PublicKeyParamSet => publicKeyParamSet;
|
||||
|
||||
protected ECKeyParameters(string algorithm, bool isPrivate, ECDomainParameters parameters)
|
||||
: base(isPrivate)
|
||||
{
|
||||
if (algorithm == null)
|
||||
{
|
||||
throw new ArgumentNullException("algorithm");
|
||||
}
|
||||
if (parameters == null)
|
||||
{
|
||||
throw new ArgumentNullException("parameters");
|
||||
}
|
||||
this.algorithm = VerifyAlgorithmName(algorithm);
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
protected ECKeyParameters(string algorithm, bool isPrivate, DerObjectIdentifier publicKeyParamSet)
|
||||
: base(isPrivate)
|
||||
{
|
||||
if (algorithm == null)
|
||||
{
|
||||
throw new ArgumentNullException("algorithm");
|
||||
}
|
||||
if (publicKeyParamSet == null)
|
||||
{
|
||||
throw new ArgumentNullException("publicKeyParamSet");
|
||||
}
|
||||
this.algorithm = VerifyAlgorithmName(algorithm);
|
||||
parameters = LookupParameters(publicKeyParamSet);
|
||||
this.publicKeyParamSet = publicKeyParamSet;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is ECDomainParameters obj2))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Equals(obj2);
|
||||
}
|
||||
|
||||
protected bool Equals(ECKeyParameters other)
|
||||
{
|
||||
if (parameters.Equals(other.parameters))
|
||||
{
|
||||
return Equals((AsymmetricKeyParameter)other);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return parameters.GetHashCode() ^ base.GetHashCode();
|
||||
}
|
||||
|
||||
internal ECKeyGenerationParameters CreateKeyGenerationParameters(SecureRandom random)
|
||||
{
|
||||
if (publicKeyParamSet != null)
|
||||
{
|
||||
return new ECKeyGenerationParameters(publicKeyParamSet, random);
|
||||
}
|
||||
return new ECKeyGenerationParameters(parameters, random);
|
||||
}
|
||||
|
||||
internal static string VerifyAlgorithmName(string algorithm)
|
||||
{
|
||||
string result = Platform.ToUpperInvariant(algorithm);
|
||||
if (Array.IndexOf((Array)algorithms, (object?)algorithm, 0, algorithms.Length) < 0)
|
||||
{
|
||||
throw new ArgumentException("unrecognised algorithm: " + algorithm, "algorithm");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static ECDomainParameters LookupParameters(DerObjectIdentifier publicKeyParamSet)
|
||||
{
|
||||
if (publicKeyParamSet == null)
|
||||
{
|
||||
throw new ArgumentNullException("publicKeyParamSet");
|
||||
}
|
||||
ECDomainParameters eCDomainParameters = ECGost3410NamedCurves.GetByOid(publicKeyParamSet);
|
||||
if (eCDomainParameters == null)
|
||||
{
|
||||
X9ECParameters x9ECParameters = ECKeyPairGenerator.FindECCurveByOid(publicKeyParamSet);
|
||||
if (x9ECParameters == null)
|
||||
{
|
||||
throw new ArgumentException("OID is not a valid public key parameter set", "publicKeyParamSet");
|
||||
}
|
||||
eCDomainParameters = new ECDomainParameters(x9ECParameters.Curve, x9ECParameters.G, x9ECParameters.N, x9ECParameters.H, x9ECParameters.GetSeed());
|
||||
}
|
||||
return eCDomainParameters;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Math;
|
||||
using Org.BouncyCastle.Math.EC;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class ECNamedDomainParameters : ECDomainParameters
|
||||
{
|
||||
private readonly DerObjectIdentifier name;
|
||||
|
||||
public DerObjectIdentifier Name => name;
|
||||
|
||||
public ECNamedDomainParameters(DerObjectIdentifier name, ECDomainParameters dp)
|
||||
: this(name, dp.curve, dp.g, dp.n, dp.h, dp.seed)
|
||||
{
|
||||
}
|
||||
|
||||
public ECNamedDomainParameters(DerObjectIdentifier name, ECCurve curve, ECPoint g, BigInteger n)
|
||||
: base(curve, g, n)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public ECNamedDomainParameters(DerObjectIdentifier name, ECCurve curve, ECPoint g, BigInteger n, BigInteger h)
|
||||
: base(curve, g, n, h)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public ECNamedDomainParameters(DerObjectIdentifier name, ECCurve curve, ECPoint g, BigInteger n, BigInteger h, byte[] seed)
|
||||
: base(curve, g, n, h, seed)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Math;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class ECPrivateKeyParameters : ECKeyParameters
|
||||
{
|
||||
private readonly BigInteger d;
|
||||
|
||||
public BigInteger D => d;
|
||||
|
||||
public ECPrivateKeyParameters(BigInteger d, ECDomainParameters parameters)
|
||||
: this("EC", d, parameters)
|
||||
{
|
||||
}
|
||||
|
||||
[Obsolete("Use version with explicit 'algorithm' parameter")]
|
||||
public ECPrivateKeyParameters(BigInteger d, DerObjectIdentifier publicKeyParamSet)
|
||||
: base("ECGOST3410", isPrivate: true, publicKeyParamSet)
|
||||
{
|
||||
if (d == null)
|
||||
{
|
||||
throw new ArgumentNullException("d");
|
||||
}
|
||||
this.d = d;
|
||||
}
|
||||
|
||||
public ECPrivateKeyParameters(string algorithm, BigInteger d, ECDomainParameters parameters)
|
||||
: base(algorithm, isPrivate: true, parameters)
|
||||
{
|
||||
if (d == null)
|
||||
{
|
||||
throw new ArgumentNullException("d");
|
||||
}
|
||||
this.d = d;
|
||||
}
|
||||
|
||||
public ECPrivateKeyParameters(string algorithm, BigInteger d, DerObjectIdentifier publicKeyParamSet)
|
||||
: base(algorithm, isPrivate: true, publicKeyParamSet)
|
||||
{
|
||||
if (d == null)
|
||||
{
|
||||
throw new ArgumentNullException("d");
|
||||
}
|
||||
this.d = d;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is ECPrivateKeyParameters other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Equals(other);
|
||||
}
|
||||
|
||||
protected bool Equals(ECPrivateKeyParameters other)
|
||||
{
|
||||
if (d.Equals(other.d))
|
||||
{
|
||||
return Equals((ECKeyParameters)other);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return d.GetHashCode() ^ base.GetHashCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Math.EC;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class ECPublicKeyParameters : ECKeyParameters
|
||||
{
|
||||
private readonly ECPoint q;
|
||||
|
||||
public ECPoint Q => q;
|
||||
|
||||
public ECPublicKeyParameters(ECPoint q, ECDomainParameters parameters)
|
||||
: this("EC", q, parameters)
|
||||
{
|
||||
}
|
||||
|
||||
[Obsolete("Use version with explicit 'algorithm' parameter")]
|
||||
public ECPublicKeyParameters(ECPoint q, DerObjectIdentifier publicKeyParamSet)
|
||||
: base("ECGOST3410", isPrivate: false, publicKeyParamSet)
|
||||
{
|
||||
if (q == null)
|
||||
{
|
||||
throw new ArgumentNullException("q");
|
||||
}
|
||||
this.q = ECDomainParameters.Validate(base.Parameters.Curve, q);
|
||||
}
|
||||
|
||||
public ECPublicKeyParameters(string algorithm, ECPoint q, ECDomainParameters parameters)
|
||||
: base(algorithm, isPrivate: false, parameters)
|
||||
{
|
||||
if (q == null)
|
||||
{
|
||||
throw new ArgumentNullException("q");
|
||||
}
|
||||
this.q = ECDomainParameters.Validate(base.Parameters.Curve, q);
|
||||
}
|
||||
|
||||
public ECPublicKeyParameters(string algorithm, ECPoint q, DerObjectIdentifier publicKeyParamSet)
|
||||
: base(algorithm, isPrivate: false, publicKeyParamSet)
|
||||
{
|
||||
if (q == null)
|
||||
{
|
||||
throw new ArgumentNullException("q");
|
||||
}
|
||||
this.q = ECDomainParameters.Validate(base.Parameters.Curve, q);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is ECPublicKeyParameters other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Equals(other);
|
||||
}
|
||||
|
||||
protected bool Equals(ECPublicKeyParameters other)
|
||||
{
|
||||
if (q.Equals(other.q))
|
||||
{
|
||||
return Equals((ECKeyParameters)other);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return q.GetHashCode() ^ base.GetHashCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using Org.BouncyCastle.Security;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class Ed25519KeyGenerationParameters : KeyGenerationParameters
|
||||
{
|
||||
public Ed25519KeyGenerationParameters(SecureRandom random)
|
||||
: base(random, 256)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Math.EC.Rfc8032;
|
||||
using Org.BouncyCastle.Security;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
using Org.BouncyCastle.Utilities.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public sealed class Ed25519PrivateKeyParameters : AsymmetricKeyParameter
|
||||
{
|
||||
public static readonly int KeySize = Ed25519.SecretKeySize;
|
||||
|
||||
public static readonly int SignatureSize = Ed25519.SignatureSize;
|
||||
|
||||
private readonly byte[] data = new byte[KeySize];
|
||||
|
||||
public Ed25519PrivateKeyParameters(SecureRandom random)
|
||||
: base(privateKey: true)
|
||||
{
|
||||
Ed25519.GeneratePrivateKey(random, data);
|
||||
}
|
||||
|
||||
public Ed25519PrivateKeyParameters(byte[] buf, int off)
|
||||
: base(privateKey: true)
|
||||
{
|
||||
Array.Copy(buf, off, data, 0, KeySize);
|
||||
}
|
||||
|
||||
public Ed25519PrivateKeyParameters(Stream input)
|
||||
: base(privateKey: true)
|
||||
{
|
||||
if (KeySize != Streams.ReadFully(input, data))
|
||||
{
|
||||
throw new EndOfStreamException("EOF encountered in middle of Ed25519 private key");
|
||||
}
|
||||
}
|
||||
|
||||
public void Encode(byte[] buf, int off)
|
||||
{
|
||||
Array.Copy(data, 0, buf, off, KeySize);
|
||||
}
|
||||
|
||||
public byte[] GetEncoded()
|
||||
{
|
||||
return Arrays.Clone(data);
|
||||
}
|
||||
|
||||
public Ed25519PublicKeyParameters GeneratePublicKey()
|
||||
{
|
||||
byte[] array = new byte[Ed25519.PublicKeySize];
|
||||
Ed25519.GeneratePublicKey(data, 0, array, 0);
|
||||
return new Ed25519PublicKeyParameters(array, 0);
|
||||
}
|
||||
|
||||
public void Sign(Ed25519.Algorithm algorithm, Ed25519PublicKeyParameters publicKey, byte[] ctx, byte[] msg, int msgOff, int msgLen, byte[] sig, int sigOff)
|
||||
{
|
||||
byte[] array = new byte[Ed25519.PublicKeySize];
|
||||
if (publicKey == null)
|
||||
{
|
||||
Ed25519.GeneratePublicKey(data, 0, array, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
publicKey.Encode(array, 0);
|
||||
}
|
||||
switch (algorithm)
|
||||
{
|
||||
case Ed25519.Algorithm.Ed25519:
|
||||
if (ctx != null)
|
||||
{
|
||||
throw new ArgumentException("ctx");
|
||||
}
|
||||
Ed25519.Sign(data, 0, array, 0, msg, msgOff, msgLen, sig, sigOff);
|
||||
break;
|
||||
case Ed25519.Algorithm.Ed25519ctx:
|
||||
Ed25519.Sign(data, 0, array, 0, ctx, msg, msgOff, msgLen, sig, sigOff);
|
||||
break;
|
||||
case Ed25519.Algorithm.Ed25519ph:
|
||||
if (Ed25519.PrehashSize != msgLen)
|
||||
{
|
||||
throw new ArgumentException("msgLen");
|
||||
}
|
||||
Ed25519.SignPrehash(data, 0, array, 0, ctx, msg, msgOff, sig, sigOff);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("algorithm");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Math.EC.Rfc8032;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
using Org.BouncyCastle.Utilities.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public sealed class Ed25519PublicKeyParameters : AsymmetricKeyParameter
|
||||
{
|
||||
public static readonly int KeySize = Ed25519.PublicKeySize;
|
||||
|
||||
private readonly byte[] data = new byte[KeySize];
|
||||
|
||||
public Ed25519PublicKeyParameters(byte[] buf, int off)
|
||||
: base(privateKey: false)
|
||||
{
|
||||
Array.Copy(buf, off, data, 0, KeySize);
|
||||
}
|
||||
|
||||
public Ed25519PublicKeyParameters(Stream input)
|
||||
: base(privateKey: false)
|
||||
{
|
||||
if (KeySize != Streams.ReadFully(input, data))
|
||||
{
|
||||
throw new EndOfStreamException("EOF encountered in middle of Ed25519 public key");
|
||||
}
|
||||
}
|
||||
|
||||
public void Encode(byte[] buf, int off)
|
||||
{
|
||||
Array.Copy(data, 0, buf, off, KeySize);
|
||||
}
|
||||
|
||||
public byte[] GetEncoded()
|
||||
{
|
||||
return Arrays.Clone(data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using Org.BouncyCastle.Security;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class Ed448KeyGenerationParameters : KeyGenerationParameters
|
||||
{
|
||||
public Ed448KeyGenerationParameters(SecureRandom random)
|
||||
: base(random, 448)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Math.EC.Rfc8032;
|
||||
using Org.BouncyCastle.Security;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
using Org.BouncyCastle.Utilities.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public sealed class Ed448PrivateKeyParameters : AsymmetricKeyParameter
|
||||
{
|
||||
public static readonly int KeySize = Ed448.SecretKeySize;
|
||||
|
||||
public static readonly int SignatureSize = Ed448.SignatureSize;
|
||||
|
||||
private readonly byte[] data = new byte[KeySize];
|
||||
|
||||
public Ed448PrivateKeyParameters(SecureRandom random)
|
||||
: base(privateKey: true)
|
||||
{
|
||||
Ed448.GeneratePrivateKey(random, data);
|
||||
}
|
||||
|
||||
public Ed448PrivateKeyParameters(byte[] buf, int off)
|
||||
: base(privateKey: true)
|
||||
{
|
||||
Array.Copy(buf, off, data, 0, KeySize);
|
||||
}
|
||||
|
||||
public Ed448PrivateKeyParameters(Stream input)
|
||||
: base(privateKey: true)
|
||||
{
|
||||
if (KeySize != Streams.ReadFully(input, data))
|
||||
{
|
||||
throw new EndOfStreamException("EOF encountered in middle of Ed448 private key");
|
||||
}
|
||||
}
|
||||
|
||||
public void Encode(byte[] buf, int off)
|
||||
{
|
||||
Array.Copy(data, 0, buf, off, KeySize);
|
||||
}
|
||||
|
||||
public byte[] GetEncoded()
|
||||
{
|
||||
return Arrays.Clone(data);
|
||||
}
|
||||
|
||||
public Ed448PublicKeyParameters GeneratePublicKey()
|
||||
{
|
||||
byte[] array = new byte[Ed448.PublicKeySize];
|
||||
Ed448.GeneratePublicKey(data, 0, array, 0);
|
||||
return new Ed448PublicKeyParameters(array, 0);
|
||||
}
|
||||
|
||||
public void Sign(Ed448.Algorithm algorithm, Ed448PublicKeyParameters publicKey, byte[] ctx, byte[] msg, int msgOff, int msgLen, byte[] sig, int sigOff)
|
||||
{
|
||||
byte[] array = new byte[Ed448.PublicKeySize];
|
||||
if (publicKey == null)
|
||||
{
|
||||
Ed448.GeneratePublicKey(data, 0, array, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
publicKey.Encode(array, 0);
|
||||
}
|
||||
switch (algorithm)
|
||||
{
|
||||
case Ed448.Algorithm.Ed448:
|
||||
Ed448.Sign(data, 0, array, 0, ctx, msg, msgOff, msgLen, sig, sigOff);
|
||||
break;
|
||||
case Ed448.Algorithm.Ed448ph:
|
||||
if (Ed448.PrehashSize != msgLen)
|
||||
{
|
||||
throw new ArgumentException("msgLen");
|
||||
}
|
||||
Ed448.SignPrehash(data, 0, array, 0, ctx, msg, msgOff, sig, sigOff);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("algorithm");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Math.EC.Rfc8032;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
using Org.BouncyCastle.Utilities.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public sealed class Ed448PublicKeyParameters : AsymmetricKeyParameter
|
||||
{
|
||||
public static readonly int KeySize = Ed448.PublicKeySize;
|
||||
|
||||
private readonly byte[] data = new byte[KeySize];
|
||||
|
||||
public Ed448PublicKeyParameters(byte[] buf, int off)
|
||||
: base(privateKey: false)
|
||||
{
|
||||
Array.Copy(buf, off, data, 0, KeySize);
|
||||
}
|
||||
|
||||
public Ed448PublicKeyParameters(Stream input)
|
||||
: base(privateKey: false)
|
||||
{
|
||||
if (KeySize != Streams.ReadFully(input, data))
|
||||
{
|
||||
throw new EndOfStreamException("EOF encountered in middle of Ed448 public key");
|
||||
}
|
||||
}
|
||||
|
||||
public void Encode(byte[] buf, int off)
|
||||
{
|
||||
Array.Copy(data, 0, buf, off, KeySize);
|
||||
}
|
||||
|
||||
public byte[] GetEncoded()
|
||||
{
|
||||
return Arrays.Clone(data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using Org.BouncyCastle.Security;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class ElGamalKeyGenerationParameters : KeyGenerationParameters
|
||||
{
|
||||
private readonly ElGamalParameters parameters;
|
||||
|
||||
public ElGamalParameters Parameters => parameters;
|
||||
|
||||
public ElGamalKeyGenerationParameters(SecureRandom random, ElGamalParameters parameters)
|
||||
: base(random, GetStrength(parameters))
|
||||
{
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
internal static int GetStrength(ElGamalParameters parameters)
|
||||
{
|
||||
if (parameters.L == 0)
|
||||
{
|
||||
return parameters.P.BitLength;
|
||||
}
|
||||
return parameters.L;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class ElGamalKeyParameters : AsymmetricKeyParameter
|
||||
{
|
||||
private readonly ElGamalParameters parameters;
|
||||
|
||||
public ElGamalParameters Parameters => parameters;
|
||||
|
||||
protected ElGamalKeyParameters(bool isPrivate, ElGamalParameters parameters)
|
||||
: base(isPrivate)
|
||||
{
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is ElGamalKeyParameters other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Equals(other);
|
||||
}
|
||||
|
||||
protected bool Equals(ElGamalKeyParameters other)
|
||||
{
|
||||
if (object.Equals(parameters, other.parameters))
|
||||
{
|
||||
return Equals((AsymmetricKeyParameter)other);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
int num = base.GetHashCode();
|
||||
if (parameters != null)
|
||||
{
|
||||
num ^= parameters.GetHashCode();
|
||||
}
|
||||
return num;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Math;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class ElGamalParameters : ICipherParameters
|
||||
{
|
||||
private readonly BigInteger p;
|
||||
|
||||
private readonly BigInteger g;
|
||||
|
||||
private readonly int l;
|
||||
|
||||
public BigInteger P => p;
|
||||
|
||||
public BigInteger G => g;
|
||||
|
||||
public int L => l;
|
||||
|
||||
public ElGamalParameters(BigInteger p, BigInteger g)
|
||||
: this(p, g, 0)
|
||||
{
|
||||
}
|
||||
|
||||
public ElGamalParameters(BigInteger p, BigInteger g, int l)
|
||||
{
|
||||
if (p == null)
|
||||
{
|
||||
throw new ArgumentNullException("p");
|
||||
}
|
||||
if (g == null)
|
||||
{
|
||||
throw new ArgumentNullException("g");
|
||||
}
|
||||
this.p = p;
|
||||
this.g = g;
|
||||
this.l = l;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is ElGamalParameters other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Equals(other);
|
||||
}
|
||||
|
||||
protected bool Equals(ElGamalParameters other)
|
||||
{
|
||||
if (p.Equals(other.p) && g.Equals(other.g))
|
||||
{
|
||||
return l == other.l;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return p.GetHashCode() ^ g.GetHashCode() ^ l;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Math;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class ElGamalPrivateKeyParameters : ElGamalKeyParameters
|
||||
{
|
||||
private readonly BigInteger x;
|
||||
|
||||
public BigInteger X => x;
|
||||
|
||||
public ElGamalPrivateKeyParameters(BigInteger x, ElGamalParameters parameters)
|
||||
: base(isPrivate: true, parameters)
|
||||
{
|
||||
if (x == null)
|
||||
{
|
||||
throw new ArgumentNullException("x");
|
||||
}
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is ElGamalPrivateKeyParameters other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Equals(other);
|
||||
}
|
||||
|
||||
protected bool Equals(ElGamalPrivateKeyParameters other)
|
||||
{
|
||||
if (other.x.Equals(x))
|
||||
{
|
||||
return Equals((ElGamalKeyParameters)other);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return x.GetHashCode() ^ base.GetHashCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Math;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class ElGamalPublicKeyParameters : ElGamalKeyParameters
|
||||
{
|
||||
private readonly BigInteger y;
|
||||
|
||||
public BigInteger Y => y;
|
||||
|
||||
public ElGamalPublicKeyParameters(BigInteger y, ElGamalParameters parameters)
|
||||
: base(isPrivate: false, parameters)
|
||||
{
|
||||
if (y == null)
|
||||
{
|
||||
throw new ArgumentNullException("y");
|
||||
}
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is ElGamalPublicKeyParameters other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Equals(other);
|
||||
}
|
||||
|
||||
protected bool Equals(ElGamalPublicKeyParameters other)
|
||||
{
|
||||
if (y.Equals(other.y))
|
||||
{
|
||||
return Equals((ElGamalKeyParameters)other);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return y.GetHashCode() ^ base.GetHashCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Asn1.CryptoPro;
|
||||
using Org.BouncyCastle.Security;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class Gost3410KeyGenerationParameters : KeyGenerationParameters
|
||||
{
|
||||
private readonly Gost3410Parameters parameters;
|
||||
|
||||
private readonly DerObjectIdentifier publicKeyParamSet;
|
||||
|
||||
public Gost3410Parameters Parameters => parameters;
|
||||
|
||||
public DerObjectIdentifier PublicKeyParamSet => publicKeyParamSet;
|
||||
|
||||
public Gost3410KeyGenerationParameters(SecureRandom random, Gost3410Parameters parameters)
|
||||
: base(random, parameters.P.BitLength - 1)
|
||||
{
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public Gost3410KeyGenerationParameters(SecureRandom random, DerObjectIdentifier publicKeyParamSet)
|
||||
: this(random, LookupParameters(publicKeyParamSet))
|
||||
{
|
||||
this.publicKeyParamSet = publicKeyParamSet;
|
||||
}
|
||||
|
||||
private static Gost3410Parameters LookupParameters(DerObjectIdentifier publicKeyParamSet)
|
||||
{
|
||||
if (publicKeyParamSet == null)
|
||||
{
|
||||
throw new ArgumentNullException("publicKeyParamSet");
|
||||
}
|
||||
Gost3410ParamSetParameters byOid = Gost3410NamedParameters.GetByOid(publicKeyParamSet);
|
||||
if (byOid == null)
|
||||
{
|
||||
throw new ArgumentException("OID is not a valid CryptoPro public key parameter set", "publicKeyParamSet");
|
||||
}
|
||||
return new Gost3410Parameters(byOid.P, byOid.Q, byOid.A);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Asn1.CryptoPro;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public abstract class Gost3410KeyParameters : AsymmetricKeyParameter
|
||||
{
|
||||
private readonly Gost3410Parameters parameters;
|
||||
|
||||
private readonly DerObjectIdentifier publicKeyParamSet;
|
||||
|
||||
public Gost3410Parameters Parameters => parameters;
|
||||
|
||||
public DerObjectIdentifier PublicKeyParamSet => publicKeyParamSet;
|
||||
|
||||
protected Gost3410KeyParameters(bool isPrivate, Gost3410Parameters parameters)
|
||||
: base(isPrivate)
|
||||
{
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
protected Gost3410KeyParameters(bool isPrivate, DerObjectIdentifier publicKeyParamSet)
|
||||
: base(isPrivate)
|
||||
{
|
||||
parameters = LookupParameters(publicKeyParamSet);
|
||||
this.publicKeyParamSet = publicKeyParamSet;
|
||||
}
|
||||
|
||||
private static Gost3410Parameters LookupParameters(DerObjectIdentifier publicKeyParamSet)
|
||||
{
|
||||
if (publicKeyParamSet == null)
|
||||
{
|
||||
throw new ArgumentNullException("publicKeyParamSet");
|
||||
}
|
||||
Gost3410ParamSetParameters byOid = Gost3410NamedParameters.GetByOid(publicKeyParamSet);
|
||||
if (byOid == null)
|
||||
{
|
||||
throw new ArgumentException("OID is not a valid CryptoPro public key parameter set", "publicKeyParamSet");
|
||||
}
|
||||
return new Gost3410Parameters(byOid.P, byOid.Q, byOid.A);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Math;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class Gost3410Parameters : ICipherParameters
|
||||
{
|
||||
private readonly BigInteger p;
|
||||
|
||||
private readonly BigInteger q;
|
||||
|
||||
private readonly BigInteger a;
|
||||
|
||||
private readonly Gost3410ValidationParameters validation;
|
||||
|
||||
public BigInteger P => p;
|
||||
|
||||
public BigInteger Q => q;
|
||||
|
||||
public BigInteger A => a;
|
||||
|
||||
public Gost3410ValidationParameters ValidationParameters => validation;
|
||||
|
||||
public Gost3410Parameters(BigInteger p, BigInteger q, BigInteger a)
|
||||
: this(p, q, a, null)
|
||||
{
|
||||
}
|
||||
|
||||
public Gost3410Parameters(BigInteger p, BigInteger q, BigInteger a, Gost3410ValidationParameters validation)
|
||||
{
|
||||
if (p == null)
|
||||
{
|
||||
throw new ArgumentNullException("p");
|
||||
}
|
||||
if (q == null)
|
||||
{
|
||||
throw new ArgumentNullException("q");
|
||||
}
|
||||
if (a == null)
|
||||
{
|
||||
throw new ArgumentNullException("a");
|
||||
}
|
||||
this.p = p;
|
||||
this.q = q;
|
||||
this.a = a;
|
||||
this.validation = validation;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is Gost3410Parameters other))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Equals(other);
|
||||
}
|
||||
|
||||
protected bool Equals(Gost3410Parameters other)
|
||||
{
|
||||
if (p.Equals(other.p) && q.Equals(other.q))
|
||||
{
|
||||
return a.Equals(other.a);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return p.GetHashCode() ^ q.GetHashCode() ^ a.GetHashCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Math;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class Gost3410PrivateKeyParameters : Gost3410KeyParameters
|
||||
{
|
||||
private readonly BigInteger x;
|
||||
|
||||
public BigInteger X => x;
|
||||
|
||||
public Gost3410PrivateKeyParameters(BigInteger x, Gost3410Parameters parameters)
|
||||
: base(isPrivate: true, parameters)
|
||||
{
|
||||
if (x.SignValue < 1 || x.BitLength > 256 || x.CompareTo(base.Parameters.Q) >= 0)
|
||||
{
|
||||
throw new ArgumentException("Invalid x for GOST3410 private key", "x");
|
||||
}
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public Gost3410PrivateKeyParameters(BigInteger x, DerObjectIdentifier publicKeyParamSet)
|
||||
: base(isPrivate: true, publicKeyParamSet)
|
||||
{
|
||||
if (x.SignValue < 1 || x.BitLength > 256 || x.CompareTo(base.Parameters.Q) >= 0)
|
||||
{
|
||||
throw new ArgumentException("Invalid x for GOST3410 private key", "x");
|
||||
}
|
||||
this.x = x;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1;
|
||||
using Org.BouncyCastle.Math;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class Gost3410PublicKeyParameters : Gost3410KeyParameters
|
||||
{
|
||||
private readonly BigInteger y;
|
||||
|
||||
public BigInteger Y => y;
|
||||
|
||||
public Gost3410PublicKeyParameters(BigInteger y, Gost3410Parameters parameters)
|
||||
: base(isPrivate: false, parameters)
|
||||
{
|
||||
if (y.SignValue < 1 || y.CompareTo(base.Parameters.P) >= 0)
|
||||
{
|
||||
throw new ArgumentException("Invalid y for GOST3410 public key", "y");
|
||||
}
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public Gost3410PublicKeyParameters(BigInteger y, DerObjectIdentifier publicKeyParamSet)
|
||||
: base(isPrivate: false, publicKeyParamSet)
|
||||
{
|
||||
if (y.SignValue < 1 || y.CompareTo(base.Parameters.P) >= 0)
|
||||
{
|
||||
throw new ArgumentException("Invalid y for GOST3410 public key", "y");
|
||||
}
|
||||
this.y = y;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class Gost3410ValidationParameters
|
||||
{
|
||||
private int x0;
|
||||
|
||||
private int c;
|
||||
|
||||
private long x0L;
|
||||
|
||||
private long cL;
|
||||
|
||||
public int C => c;
|
||||
|
||||
public int X0 => x0;
|
||||
|
||||
public long CL => cL;
|
||||
|
||||
public long X0L => x0L;
|
||||
|
||||
public Gost3410ValidationParameters(int x0, int c)
|
||||
{
|
||||
this.x0 = x0;
|
||||
this.c = c;
|
||||
}
|
||||
|
||||
public Gost3410ValidationParameters(long x0L, long cL)
|
||||
{
|
||||
this.x0L = x0L;
|
||||
this.cL = cL;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj is Gost3410ValidationParameters gost3410ValidationParameters && gost3410ValidationParameters.c == c && gost3410ValidationParameters.x0 == x0 && gost3410ValidationParameters.cL == cL)
|
||||
{
|
||||
return gost3410ValidationParameters.x0L == x0L;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return c.GetHashCode() ^ x0.GetHashCode() ^ cL.GetHashCode() ^ x0L.GetHashCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class HkdfParameters : IDerivationParameters
|
||||
{
|
||||
private readonly byte[] ikm;
|
||||
|
||||
private readonly bool skipExpand;
|
||||
|
||||
private readonly byte[] salt;
|
||||
|
||||
private readonly byte[] info;
|
||||
|
||||
public virtual bool SkipExtract => skipExpand;
|
||||
|
||||
private HkdfParameters(byte[] ikm, bool skip, byte[] salt, byte[] info)
|
||||
{
|
||||
if (ikm == null)
|
||||
{
|
||||
throw new ArgumentNullException("ikm");
|
||||
}
|
||||
this.ikm = Arrays.Clone(ikm);
|
||||
skipExpand = skip;
|
||||
if (salt == null || salt.Length == 0)
|
||||
{
|
||||
this.salt = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.salt = Arrays.Clone(salt);
|
||||
}
|
||||
if (info == null)
|
||||
{
|
||||
this.info = new byte[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
this.info = Arrays.Clone(info);
|
||||
}
|
||||
}
|
||||
|
||||
public HkdfParameters(byte[] ikm, byte[] salt, byte[] info)
|
||||
: this(ikm, skip: false, salt, info)
|
||||
{
|
||||
}
|
||||
|
||||
public static HkdfParameters SkipExtractParameters(byte[] ikm, byte[] info)
|
||||
{
|
||||
return new HkdfParameters(ikm, skip: true, null, info);
|
||||
}
|
||||
|
||||
public static HkdfParameters DefaultParameters(byte[] ikm)
|
||||
{
|
||||
return new HkdfParameters(ikm, skip: false, null, null);
|
||||
}
|
||||
|
||||
public virtual byte[] GetIkm()
|
||||
{
|
||||
return Arrays.Clone(ikm);
|
||||
}
|
||||
|
||||
public virtual byte[] GetSalt()
|
||||
{
|
||||
return Arrays.Clone(salt);
|
||||
}
|
||||
|
||||
public virtual byte[] GetInfo()
|
||||
{
|
||||
return Arrays.Clone(info);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class IesParameters : ICipherParameters
|
||||
{
|
||||
private byte[] derivation;
|
||||
|
||||
private byte[] encoding;
|
||||
|
||||
private int macKeySize;
|
||||
|
||||
public int MacKeySize => macKeySize;
|
||||
|
||||
public IesParameters(byte[] derivation, byte[] encoding, int macKeySize)
|
||||
{
|
||||
this.derivation = derivation;
|
||||
this.encoding = encoding;
|
||||
this.macKeySize = macKeySize;
|
||||
}
|
||||
|
||||
public byte[] GetDerivationV()
|
||||
{
|
||||
return derivation;
|
||||
}
|
||||
|
||||
public byte[] GetEncodingV()
|
||||
{
|
||||
return encoding;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class IesWithCipherParameters : IesParameters
|
||||
{
|
||||
private int cipherKeySize;
|
||||
|
||||
public int CipherKeySize => cipherKeySize;
|
||||
|
||||
public IesWithCipherParameters(byte[] derivation, byte[] encoding, int macKeySize, int cipherKeySize)
|
||||
: base(derivation, encoding, macKeySize)
|
||||
{
|
||||
this.cipherKeySize = cipherKeySize;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class Iso18033KdfParameters : IDerivationParameters
|
||||
{
|
||||
private byte[] seed;
|
||||
|
||||
public Iso18033KdfParameters(byte[] seed)
|
||||
{
|
||||
this.seed = seed;
|
||||
}
|
||||
|
||||
public byte[] GetSeed()
|
||||
{
|
||||
return seed;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class KdfParameters : IDerivationParameters
|
||||
{
|
||||
private byte[] iv;
|
||||
|
||||
private byte[] shared;
|
||||
|
||||
public KdfParameters(byte[] shared, byte[] iv)
|
||||
{
|
||||
this.shared = shared;
|
||||
this.iv = iv;
|
||||
}
|
||||
|
||||
public byte[] GetSharedSecret()
|
||||
{
|
||||
return shared;
|
||||
}
|
||||
|
||||
public byte[] GetIV()
|
||||
{
|
||||
return iv;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class KeyParameter : ICipherParameters
|
||||
{
|
||||
private readonly byte[] key;
|
||||
|
||||
public KeyParameter(byte[] key)
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
throw new ArgumentNullException("key");
|
||||
}
|
||||
this.key = (byte[])key.Clone();
|
||||
}
|
||||
|
||||
public KeyParameter(byte[] key, int keyOff, int keyLen)
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
throw new ArgumentNullException("key");
|
||||
}
|
||||
if (keyOff < 0 || keyOff > key.Length)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("keyOff");
|
||||
}
|
||||
if (keyLen < 0 || keyLen > key.Length - keyOff)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("keyLen");
|
||||
}
|
||||
this.key = new byte[keyLen];
|
||||
Array.Copy(key, keyOff, this.key, 0, keyLen);
|
||||
}
|
||||
|
||||
public byte[] GetKey()
|
||||
{
|
||||
return (byte[])key.Clone();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class MgfParameters : IDerivationParameters
|
||||
{
|
||||
private readonly byte[] seed;
|
||||
|
||||
public MgfParameters(byte[] seed)
|
||||
: this(seed, 0, seed.Length)
|
||||
{
|
||||
}
|
||||
|
||||
public MgfParameters(byte[] seed, int off, int len)
|
||||
{
|
||||
this.seed = new byte[len];
|
||||
Array.Copy(seed, off, this.seed, 0, len);
|
||||
}
|
||||
|
||||
public byte[] GetSeed()
|
||||
{
|
||||
return (byte[])seed.Clone();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class MqvPrivateParameters : ICipherParameters
|
||||
{
|
||||
private readonly ECPrivateKeyParameters staticPrivateKey;
|
||||
|
||||
private readonly ECPrivateKeyParameters ephemeralPrivateKey;
|
||||
|
||||
private readonly ECPublicKeyParameters ephemeralPublicKey;
|
||||
|
||||
public virtual ECPrivateKeyParameters StaticPrivateKey => staticPrivateKey;
|
||||
|
||||
public virtual ECPrivateKeyParameters EphemeralPrivateKey => ephemeralPrivateKey;
|
||||
|
||||
public virtual ECPublicKeyParameters EphemeralPublicKey => ephemeralPublicKey;
|
||||
|
||||
public MqvPrivateParameters(ECPrivateKeyParameters staticPrivateKey, ECPrivateKeyParameters ephemeralPrivateKey)
|
||||
: this(staticPrivateKey, ephemeralPrivateKey, null)
|
||||
{
|
||||
}
|
||||
|
||||
public MqvPrivateParameters(ECPrivateKeyParameters staticPrivateKey, ECPrivateKeyParameters ephemeralPrivateKey, ECPublicKeyParameters ephemeralPublicKey)
|
||||
{
|
||||
if (staticPrivateKey == null)
|
||||
{
|
||||
throw new ArgumentNullException("staticPrivateKey");
|
||||
}
|
||||
if (ephemeralPrivateKey == null)
|
||||
{
|
||||
throw new ArgumentNullException("ephemeralPrivateKey");
|
||||
}
|
||||
ECDomainParameters parameters = staticPrivateKey.Parameters;
|
||||
if (!parameters.Equals(ephemeralPrivateKey.Parameters))
|
||||
{
|
||||
throw new ArgumentException("Static and ephemeral private keys have different domain parameters");
|
||||
}
|
||||
if (ephemeralPublicKey == null)
|
||||
{
|
||||
ephemeralPublicKey = new ECPublicKeyParameters(parameters.G.Multiply(ephemeralPrivateKey.D), parameters);
|
||||
}
|
||||
else if (!parameters.Equals(ephemeralPublicKey.Parameters))
|
||||
{
|
||||
throw new ArgumentException("Ephemeral public key has different domain parameters");
|
||||
}
|
||||
this.staticPrivateKey = staticPrivateKey;
|
||||
this.ephemeralPrivateKey = ephemeralPrivateKey;
|
||||
this.ephemeralPublicKey = ephemeralPublicKey;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class MqvPublicParameters : ICipherParameters
|
||||
{
|
||||
private readonly ECPublicKeyParameters staticPublicKey;
|
||||
|
||||
private readonly ECPublicKeyParameters ephemeralPublicKey;
|
||||
|
||||
public virtual ECPublicKeyParameters StaticPublicKey => staticPublicKey;
|
||||
|
||||
public virtual ECPublicKeyParameters EphemeralPublicKey => ephemeralPublicKey;
|
||||
|
||||
public MqvPublicParameters(ECPublicKeyParameters staticPublicKey, ECPublicKeyParameters ephemeralPublicKey)
|
||||
{
|
||||
if (staticPublicKey == null)
|
||||
{
|
||||
throw new ArgumentNullException("staticPublicKey");
|
||||
}
|
||||
if (ephemeralPublicKey == null)
|
||||
{
|
||||
throw new ArgumentNullException("ephemeralPublicKey");
|
||||
}
|
||||
if (!staticPublicKey.Parameters.Equals(ephemeralPublicKey.Parameters))
|
||||
{
|
||||
throw new ArgumentException("Static and ephemeral public keys have different domain parameters");
|
||||
}
|
||||
this.staticPublicKey = staticPublicKey;
|
||||
this.ephemeralPublicKey = ephemeralPublicKey;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Security;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class NaccacheSternKeyGenerationParameters : KeyGenerationParameters
|
||||
{
|
||||
private readonly int certainty;
|
||||
|
||||
private readonly int countSmallPrimes;
|
||||
|
||||
public int Certainty => certainty;
|
||||
|
||||
public int CountSmallPrimes => countSmallPrimes;
|
||||
|
||||
[Obsolete("Remove: always false")]
|
||||
public bool IsDebug => false;
|
||||
|
||||
public NaccacheSternKeyGenerationParameters(SecureRandom random, int strength, int certainty, int countSmallPrimes)
|
||||
: base(random, strength)
|
||||
{
|
||||
if (countSmallPrimes % 2 == 1)
|
||||
{
|
||||
throw new ArgumentException("countSmallPrimes must be a multiple of 2");
|
||||
}
|
||||
if (countSmallPrimes < 30)
|
||||
{
|
||||
throw new ArgumentException("countSmallPrimes must be >= 30 for security reasons");
|
||||
}
|
||||
this.certainty = certainty;
|
||||
this.countSmallPrimes = countSmallPrimes;
|
||||
}
|
||||
|
||||
[Obsolete("Use version without 'debug' parameter")]
|
||||
public NaccacheSternKeyGenerationParameters(SecureRandom random, int strength, int certainty, int countSmallPrimes, bool debug)
|
||||
: this(random, strength, certainty, countSmallPrimes)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Org.BouncyCastle.Math;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class NaccacheSternKeyParameters : AsymmetricKeyParameter
|
||||
{
|
||||
private readonly BigInteger g;
|
||||
|
||||
private readonly BigInteger n;
|
||||
|
||||
private readonly int lowerSigmaBound;
|
||||
|
||||
public BigInteger G => g;
|
||||
|
||||
public int LowerSigmaBound => lowerSigmaBound;
|
||||
|
||||
public BigInteger Modulus => n;
|
||||
|
||||
public NaccacheSternKeyParameters(bool privateKey, BigInteger g, BigInteger n, int lowerSigmaBound)
|
||||
: base(privateKey)
|
||||
{
|
||||
this.g = g;
|
||||
this.n = n;
|
||||
this.lowerSigmaBound = lowerSigmaBound;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Org.BouncyCastle.Math;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class NaccacheSternPrivateKeyParameters : NaccacheSternKeyParameters
|
||||
{
|
||||
private readonly BigInteger phiN;
|
||||
|
||||
private readonly IList smallPrimes;
|
||||
|
||||
public BigInteger PhiN => phiN;
|
||||
|
||||
[Obsolete("Use 'SmallPrimesList' instead")]
|
||||
public ArrayList SmallPrimes => new ArrayList(smallPrimes);
|
||||
|
||||
public IList SmallPrimesList => smallPrimes;
|
||||
|
||||
[Obsolete]
|
||||
public NaccacheSternPrivateKeyParameters(BigInteger g, BigInteger n, int lowerSigmaBound, ArrayList smallPrimes, BigInteger phiN)
|
||||
: base(privateKey: true, g, n, lowerSigmaBound)
|
||||
{
|
||||
this.smallPrimes = smallPrimes;
|
||||
this.phiN = phiN;
|
||||
}
|
||||
|
||||
public NaccacheSternPrivateKeyParameters(BigInteger g, BigInteger n, int lowerSigmaBound, IList smallPrimes, BigInteger phiN)
|
||||
: base(privateKey: true, g, n, lowerSigmaBound)
|
||||
{
|
||||
this.smallPrimes = smallPrimes;
|
||||
this.phiN = phiN;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class ParametersWithID : ICipherParameters
|
||||
{
|
||||
private readonly ICipherParameters parameters;
|
||||
|
||||
private readonly byte[] id;
|
||||
|
||||
public ICipherParameters Parameters => parameters;
|
||||
|
||||
public ParametersWithID(ICipherParameters parameters, byte[] id)
|
||||
: this(parameters, id, 0, id.Length)
|
||||
{
|
||||
}
|
||||
|
||||
public ParametersWithID(ICipherParameters parameters, byte[] id, int idOff, int idLen)
|
||||
{
|
||||
this.parameters = parameters;
|
||||
this.id = Arrays.CopyOfRange(id, idOff, idOff + idLen);
|
||||
}
|
||||
|
||||
public byte[] GetID()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class ParametersWithIV : ICipherParameters
|
||||
{
|
||||
private readonly ICipherParameters parameters;
|
||||
|
||||
private readonly byte[] iv;
|
||||
|
||||
public ICipherParameters Parameters => parameters;
|
||||
|
||||
public ParametersWithIV(ICipherParameters parameters, byte[] iv)
|
||||
: this(parameters, iv, 0, iv.Length)
|
||||
{
|
||||
}
|
||||
|
||||
public ParametersWithIV(ICipherParameters parameters, byte[] iv, int ivOff, int ivLen)
|
||||
{
|
||||
if (iv == null)
|
||||
{
|
||||
throw new ArgumentNullException("iv");
|
||||
}
|
||||
this.parameters = parameters;
|
||||
this.iv = Arrays.CopyOfRange(iv, ivOff, ivOff + ivLen);
|
||||
}
|
||||
|
||||
public byte[] GetIV()
|
||||
{
|
||||
return (byte[])iv.Clone();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Security;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class ParametersWithRandom : ICipherParameters
|
||||
{
|
||||
private readonly ICipherParameters parameters;
|
||||
|
||||
private readonly SecureRandom random;
|
||||
|
||||
public SecureRandom Random => random;
|
||||
|
||||
public ICipherParameters Parameters => parameters;
|
||||
|
||||
public ParametersWithRandom(ICipherParameters parameters, SecureRandom random)
|
||||
{
|
||||
if (parameters == null)
|
||||
{
|
||||
throw new ArgumentNullException("parameters");
|
||||
}
|
||||
if (random == null)
|
||||
{
|
||||
throw new ArgumentNullException("random");
|
||||
}
|
||||
this.parameters = parameters;
|
||||
this.random = random;
|
||||
}
|
||||
|
||||
public ParametersWithRandom(ICipherParameters parameters)
|
||||
: this(parameters, new SecureRandom())
|
||||
{
|
||||
}
|
||||
|
||||
[Obsolete("Use Random property instead")]
|
||||
public SecureRandom GetRandom()
|
||||
{
|
||||
return Random;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class ParametersWithSBox : ICipherParameters
|
||||
{
|
||||
private ICipherParameters parameters;
|
||||
|
||||
private byte[] sBox;
|
||||
|
||||
public ICipherParameters Parameters => parameters;
|
||||
|
||||
public ParametersWithSBox(ICipherParameters parameters, byte[] sBox)
|
||||
{
|
||||
this.parameters = parameters;
|
||||
this.sBox = sBox;
|
||||
}
|
||||
|
||||
public byte[] GetSBox()
|
||||
{
|
||||
return sBox;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class ParametersWithSalt : ICipherParameters
|
||||
{
|
||||
private byte[] salt;
|
||||
|
||||
private ICipherParameters parameters;
|
||||
|
||||
public ICipherParameters Parameters => parameters;
|
||||
|
||||
public ParametersWithSalt(ICipherParameters parameters, byte[] salt)
|
||||
: this(parameters, salt, 0, salt.Length)
|
||||
{
|
||||
}
|
||||
|
||||
public ParametersWithSalt(ICipherParameters parameters, byte[] salt, int saltOff, int saltLen)
|
||||
{
|
||||
this.salt = new byte[saltLen];
|
||||
this.parameters = parameters;
|
||||
Array.Copy(salt, saltOff, this.salt, 0, saltLen);
|
||||
}
|
||||
|
||||
public byte[] GetSalt()
|
||||
{
|
||||
return salt;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class RC2Parameters : KeyParameter
|
||||
{
|
||||
private readonly int bits;
|
||||
|
||||
public int EffectiveKeyBits => bits;
|
||||
|
||||
public RC2Parameters(byte[] key)
|
||||
: this(key, (key.Length > 128) ? 1024 : (key.Length * 8))
|
||||
{
|
||||
}
|
||||
|
||||
public RC2Parameters(byte[] key, int keyOff, int keyLen)
|
||||
: this(key, keyOff, keyLen, (keyLen > 128) ? 1024 : (keyLen * 8))
|
||||
{
|
||||
}
|
||||
|
||||
public RC2Parameters(byte[] key, int bits)
|
||||
: base(key)
|
||||
{
|
||||
this.bits = bits;
|
||||
}
|
||||
|
||||
public RC2Parameters(byte[] key, int keyOff, int keyLen, int bits)
|
||||
: base(key, keyOff, keyLen)
|
||||
{
|
||||
this.bits = bits;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class RC5Parameters : KeyParameter
|
||||
{
|
||||
private readonly int rounds;
|
||||
|
||||
public int Rounds => rounds;
|
||||
|
||||
public RC5Parameters(byte[] key, int rounds)
|
||||
: base(key)
|
||||
{
|
||||
if (key.Length > 255)
|
||||
{
|
||||
throw new ArgumentException("RC5 key length can be no greater than 255");
|
||||
}
|
||||
this.rounds = rounds;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Math;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class RsaBlindingParameters : ICipherParameters
|
||||
{
|
||||
private readonly RsaKeyParameters publicKey;
|
||||
|
||||
private readonly BigInteger blindingFactor;
|
||||
|
||||
public RsaKeyParameters PublicKey => publicKey;
|
||||
|
||||
public BigInteger BlindingFactor => blindingFactor;
|
||||
|
||||
public RsaBlindingParameters(RsaKeyParameters publicKey, BigInteger blindingFactor)
|
||||
{
|
||||
if (publicKey.IsPrivate)
|
||||
{
|
||||
throw new ArgumentException("RSA parameters should be for a public key");
|
||||
}
|
||||
this.publicKey = publicKey;
|
||||
this.blindingFactor = blindingFactor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using Org.BouncyCastle.Math;
|
||||
using Org.BouncyCastle.Security;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class RsaKeyGenerationParameters : KeyGenerationParameters
|
||||
{
|
||||
private readonly BigInteger publicExponent;
|
||||
|
||||
private readonly int certainty;
|
||||
|
||||
public BigInteger PublicExponent => publicExponent;
|
||||
|
||||
public int Certainty => certainty;
|
||||
|
||||
public RsaKeyGenerationParameters(BigInteger publicExponent, SecureRandom random, int strength, int certainty)
|
||||
: base(random, strength)
|
||||
{
|
||||
this.publicExponent = publicExponent;
|
||||
this.certainty = certainty;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (!(obj is RsaKeyGenerationParameters rsaKeyGenerationParameters))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (certainty == rsaKeyGenerationParameters.certainty)
|
||||
{
|
||||
return publicExponent.Equals(rsaKeyGenerationParameters.publicExponent);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
int num = certainty;
|
||||
return num.GetHashCode() ^ publicExponent.GetHashCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Math;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class RsaKeyParameters : AsymmetricKeyParameter
|
||||
{
|
||||
private static BigInteger SmallPrimesProduct = new BigInteger("8138E8A0FCF3A4E84A771D40FD305D7F4AA59306D7251DE54D98AF8FE95729A1F73D893FA424CD2EDC8636A6C3285E022B0E3866A565AE8108EED8591CD4FE8D2CE86165A978D719EBF647F362D33FCA29CD179FB42401CBAF3DF0C614056F9C8F3CFD51E474AFB6BC6974F78DB8ABA8E9E517FDED658591AB7502BD41849462F", 16);
|
||||
|
||||
private readonly BigInteger modulus;
|
||||
|
||||
private readonly BigInteger exponent;
|
||||
|
||||
public BigInteger Modulus => modulus;
|
||||
|
||||
public BigInteger Exponent => exponent;
|
||||
|
||||
private static BigInteger Validate(BigInteger modulus)
|
||||
{
|
||||
if ((modulus.IntValue & 1) == 0)
|
||||
{
|
||||
throw new ArgumentException("RSA modulus is even", "modulus");
|
||||
}
|
||||
if (!modulus.Gcd(SmallPrimesProduct).Equals(BigInteger.One))
|
||||
{
|
||||
throw new ArgumentException("RSA modulus has a small prime factor");
|
||||
}
|
||||
return modulus;
|
||||
}
|
||||
|
||||
public RsaKeyParameters(bool isPrivate, BigInteger modulus, BigInteger exponent)
|
||||
: base(isPrivate)
|
||||
{
|
||||
if (modulus == null)
|
||||
{
|
||||
throw new ArgumentNullException("modulus");
|
||||
}
|
||||
if (exponent == null)
|
||||
{
|
||||
throw new ArgumentNullException("exponent");
|
||||
}
|
||||
if (modulus.SignValue <= 0)
|
||||
{
|
||||
throw new ArgumentException("Not a valid RSA modulus", "modulus");
|
||||
}
|
||||
if (exponent.SignValue <= 0)
|
||||
{
|
||||
throw new ArgumentException("Not a valid RSA exponent", "exponent");
|
||||
}
|
||||
if (!isPrivate && (exponent.IntValue & 1) == 0)
|
||||
{
|
||||
throw new ArgumentException("RSA publicExponent is even", "exponent");
|
||||
}
|
||||
this.modulus = Validate(modulus);
|
||||
this.exponent = exponent;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (!(obj is RsaKeyParameters rsaKeyParameters))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (rsaKeyParameters.IsPrivate == base.IsPrivate && rsaKeyParameters.Modulus.Equals(modulus))
|
||||
{
|
||||
return rsaKeyParameters.Exponent.Equals(exponent);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return modulus.GetHashCode() ^ exponent.GetHashCode() ^ base.IsPrivate.GetHashCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Asn1.Pkcs;
|
||||
using Org.BouncyCastle.Math;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class RsaPrivateCrtKeyParameters : RsaKeyParameters
|
||||
{
|
||||
private readonly BigInteger e;
|
||||
|
||||
private readonly BigInteger p;
|
||||
|
||||
private readonly BigInteger q;
|
||||
|
||||
private readonly BigInteger dP;
|
||||
|
||||
private readonly BigInteger dQ;
|
||||
|
||||
private readonly BigInteger qInv;
|
||||
|
||||
public BigInteger PublicExponent => e;
|
||||
|
||||
public BigInteger P => p;
|
||||
|
||||
public BigInteger Q => q;
|
||||
|
||||
public BigInteger DP => dP;
|
||||
|
||||
public BigInteger DQ => dQ;
|
||||
|
||||
public BigInteger QInv => qInv;
|
||||
|
||||
public RsaPrivateCrtKeyParameters(BigInteger modulus, BigInteger publicExponent, BigInteger privateExponent, BigInteger p, BigInteger q, BigInteger dP, BigInteger dQ, BigInteger qInv)
|
||||
: base(isPrivate: true, modulus, privateExponent)
|
||||
{
|
||||
ValidateValue(publicExponent, "publicExponent", "exponent");
|
||||
ValidateValue(p, "p", "P value");
|
||||
ValidateValue(q, "q", "Q value");
|
||||
ValidateValue(dP, "dP", "DP value");
|
||||
ValidateValue(dQ, "dQ", "DQ value");
|
||||
ValidateValue(qInv, "qInv", "InverseQ value");
|
||||
e = publicExponent;
|
||||
this.p = p;
|
||||
this.q = q;
|
||||
this.dP = dP;
|
||||
this.dQ = dQ;
|
||||
this.qInv = qInv;
|
||||
}
|
||||
|
||||
public RsaPrivateCrtKeyParameters(RsaPrivateKeyStructure rsaPrivateKey)
|
||||
: this(rsaPrivateKey.Modulus, rsaPrivateKey.PublicExponent, rsaPrivateKey.PrivateExponent, rsaPrivateKey.Prime1, rsaPrivateKey.Prime2, rsaPrivateKey.Exponent1, rsaPrivateKey.Exponent2, rsaPrivateKey.Coefficient)
|
||||
{
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!(obj is RsaPrivateCrtKeyParameters rsaPrivateCrtKeyParameters))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (rsaPrivateCrtKeyParameters.DP.Equals(dP) && rsaPrivateCrtKeyParameters.DQ.Equals(dQ) && rsaPrivateCrtKeyParameters.Exponent.Equals(base.Exponent) && rsaPrivateCrtKeyParameters.Modulus.Equals(base.Modulus) && rsaPrivateCrtKeyParameters.P.Equals(p) && rsaPrivateCrtKeyParameters.Q.Equals(q) && rsaPrivateCrtKeyParameters.PublicExponent.Equals(e))
|
||||
{
|
||||
return rsaPrivateCrtKeyParameters.QInv.Equals(qInv);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return DP.GetHashCode() ^ DQ.GetHashCode() ^ base.Exponent.GetHashCode() ^ base.Modulus.GetHashCode() ^ P.GetHashCode() ^ Q.GetHashCode() ^ PublicExponent.GetHashCode() ^ QInv.GetHashCode();
|
||||
}
|
||||
|
||||
private static void ValidateValue(BigInteger x, string name, string desc)
|
||||
{
|
||||
if (x == null)
|
||||
{
|
||||
throw new ArgumentNullException(name);
|
||||
}
|
||||
if (x.SignValue <= 0)
|
||||
{
|
||||
throw new ArgumentException("Not a valid RSA " + desc, name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Math.EC;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class SM2KeyExchangePrivateParameters : ICipherParameters
|
||||
{
|
||||
private readonly bool mInitiator;
|
||||
|
||||
private readonly ECPrivateKeyParameters mStaticPrivateKey;
|
||||
|
||||
private readonly ECPoint mStaticPublicPoint;
|
||||
|
||||
private readonly ECPrivateKeyParameters mEphemeralPrivateKey;
|
||||
|
||||
private readonly ECPoint mEphemeralPublicPoint;
|
||||
|
||||
public virtual bool IsInitiator => mInitiator;
|
||||
|
||||
public virtual ECPrivateKeyParameters StaticPrivateKey => mStaticPrivateKey;
|
||||
|
||||
public virtual ECPoint StaticPublicPoint => mStaticPublicPoint;
|
||||
|
||||
public virtual ECPrivateKeyParameters EphemeralPrivateKey => mEphemeralPrivateKey;
|
||||
|
||||
public virtual ECPoint EphemeralPublicPoint => mEphemeralPublicPoint;
|
||||
|
||||
public SM2KeyExchangePrivateParameters(bool initiator, ECPrivateKeyParameters staticPrivateKey, ECPrivateKeyParameters ephemeralPrivateKey)
|
||||
{
|
||||
if (staticPrivateKey == null)
|
||||
{
|
||||
throw new ArgumentNullException("staticPrivateKey");
|
||||
}
|
||||
if (ephemeralPrivateKey == null)
|
||||
{
|
||||
throw new ArgumentNullException("ephemeralPrivateKey");
|
||||
}
|
||||
ECDomainParameters parameters = staticPrivateKey.Parameters;
|
||||
if (!parameters.Equals(ephemeralPrivateKey.Parameters))
|
||||
{
|
||||
throw new ArgumentException("Static and ephemeral private keys have different domain parameters");
|
||||
}
|
||||
mInitiator = initiator;
|
||||
mStaticPrivateKey = staticPrivateKey;
|
||||
mStaticPublicPoint = parameters.G.Multiply(staticPrivateKey.D).Normalize();
|
||||
mEphemeralPrivateKey = ephemeralPrivateKey;
|
||||
mEphemeralPublicPoint = parameters.G.Multiply(ephemeralPrivateKey.D).Normalize();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class SM2KeyExchangePublicParameters : ICipherParameters
|
||||
{
|
||||
private readonly ECPublicKeyParameters mStaticPublicKey;
|
||||
|
||||
private readonly ECPublicKeyParameters mEphemeralPublicKey;
|
||||
|
||||
public virtual ECPublicKeyParameters StaticPublicKey => mStaticPublicKey;
|
||||
|
||||
public virtual ECPublicKeyParameters EphemeralPublicKey => mEphemeralPublicKey;
|
||||
|
||||
public SM2KeyExchangePublicParameters(ECPublicKeyParameters staticPublicKey, ECPublicKeyParameters ephemeralPublicKey)
|
||||
{
|
||||
if (staticPublicKey == null)
|
||||
{
|
||||
throw new ArgumentNullException("staticPublicKey");
|
||||
}
|
||||
if (ephemeralPublicKey == null)
|
||||
{
|
||||
throw new ArgumentNullException("ephemeralPublicKey");
|
||||
}
|
||||
if (!staticPublicKey.Parameters.Equals(ephemeralPublicKey.Parameters))
|
||||
{
|
||||
throw new ArgumentException("Static and ephemeral public keys have different domain parameters");
|
||||
}
|
||||
mStaticPublicKey = staticPublicKey;
|
||||
mEphemeralPublicKey = ephemeralPublicKey;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class SkeinParameters : ICipherParameters
|
||||
{
|
||||
public class Builder
|
||||
{
|
||||
private IDictionary parameters = Platform.CreateHashtable();
|
||||
|
||||
public Builder()
|
||||
{
|
||||
}
|
||||
|
||||
public Builder(IDictionary paramsMap)
|
||||
{
|
||||
IEnumerator enumerator = paramsMap.Keys.GetEnumerator();
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
int num = (int)enumerator.Current;
|
||||
parameters.Add(num, paramsMap[num]);
|
||||
}
|
||||
}
|
||||
|
||||
public Builder(SkeinParameters parameters)
|
||||
{
|
||||
IEnumerator enumerator = parameters.parameters.Keys.GetEnumerator();
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
int num = (int)enumerator.Current;
|
||||
this.parameters.Add(num, parameters.parameters[num]);
|
||||
}
|
||||
}
|
||||
|
||||
public Builder Set(int type, byte[] value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
throw new ArgumentException("Parameter value must not be null.");
|
||||
}
|
||||
switch (type)
|
||||
{
|
||||
default:
|
||||
throw new ArgumentException("Parameter types must be in the range 0,5..47,49..62.");
|
||||
case 0:
|
||||
case 5:
|
||||
case 6:
|
||||
case 7:
|
||||
case 8:
|
||||
case 9:
|
||||
case 10:
|
||||
case 11:
|
||||
case 12:
|
||||
case 13:
|
||||
case 14:
|
||||
case 15:
|
||||
case 16:
|
||||
case 17:
|
||||
case 18:
|
||||
case 19:
|
||||
case 20:
|
||||
case 21:
|
||||
case 22:
|
||||
case 23:
|
||||
case 24:
|
||||
case 25:
|
||||
case 26:
|
||||
case 27:
|
||||
case 28:
|
||||
case 29:
|
||||
case 30:
|
||||
case 31:
|
||||
case 32:
|
||||
case 33:
|
||||
case 34:
|
||||
case 35:
|
||||
case 36:
|
||||
case 37:
|
||||
case 38:
|
||||
case 39:
|
||||
case 40:
|
||||
case 41:
|
||||
case 42:
|
||||
case 43:
|
||||
case 44:
|
||||
case 45:
|
||||
case 46:
|
||||
case 47:
|
||||
case 49:
|
||||
case 50:
|
||||
case 51:
|
||||
case 52:
|
||||
case 53:
|
||||
case 54:
|
||||
case 55:
|
||||
case 56:
|
||||
case 57:
|
||||
case 58:
|
||||
case 59:
|
||||
case 60:
|
||||
case 61:
|
||||
case 62:
|
||||
if (type == 4)
|
||||
{
|
||||
throw new ArgumentException("Parameter type " + 4 + " is reserved for internal use.");
|
||||
}
|
||||
parameters.Add(type, value);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public Builder SetKey(byte[] key)
|
||||
{
|
||||
return Set(0, key);
|
||||
}
|
||||
|
||||
public Builder SetPersonalisation(byte[] personalisation)
|
||||
{
|
||||
return Set(8, personalisation);
|
||||
}
|
||||
|
||||
public Builder SetPersonalisation(DateTime date, string emailAddress, string distinguisher)
|
||||
{
|
||||
try
|
||||
{
|
||||
MemoryStream memoryStream = new MemoryStream();
|
||||
StreamWriter streamWriter = new StreamWriter(memoryStream, Encoding.UTF8);
|
||||
streamWriter.Write(date.ToString("YYYYMMDD", CultureInfo.InvariantCulture));
|
||||
streamWriter.Write(" ");
|
||||
streamWriter.Write(emailAddress);
|
||||
streamWriter.Write(" ");
|
||||
streamWriter.Write(distinguisher);
|
||||
Platform.Dispose(streamWriter);
|
||||
return Set(8, memoryStream.ToArray());
|
||||
}
|
||||
catch (IOException innerException)
|
||||
{
|
||||
throw new InvalidOperationException("Byte I/O failed.", innerException);
|
||||
}
|
||||
}
|
||||
|
||||
public Builder SetPublicKey(byte[] publicKey)
|
||||
{
|
||||
return Set(12, publicKey);
|
||||
}
|
||||
|
||||
public Builder SetKeyIdentifier(byte[] keyIdentifier)
|
||||
{
|
||||
return Set(16, keyIdentifier);
|
||||
}
|
||||
|
||||
public Builder SetNonce(byte[] nonce)
|
||||
{
|
||||
return Set(20, nonce);
|
||||
}
|
||||
|
||||
public SkeinParameters Build()
|
||||
{
|
||||
return new SkeinParameters(parameters);
|
||||
}
|
||||
}
|
||||
|
||||
public const int PARAM_TYPE_KEY = 0;
|
||||
|
||||
public const int PARAM_TYPE_CONFIG = 4;
|
||||
|
||||
public const int PARAM_TYPE_PERSONALISATION = 8;
|
||||
|
||||
public const int PARAM_TYPE_PUBLIC_KEY = 12;
|
||||
|
||||
public const int PARAM_TYPE_KEY_IDENTIFIER = 16;
|
||||
|
||||
public const int PARAM_TYPE_NONCE = 20;
|
||||
|
||||
public const int PARAM_TYPE_MESSAGE = 48;
|
||||
|
||||
public const int PARAM_TYPE_OUTPUT = 63;
|
||||
|
||||
private IDictionary parameters;
|
||||
|
||||
public SkeinParameters()
|
||||
: this(Platform.CreateHashtable())
|
||||
{
|
||||
}
|
||||
|
||||
private SkeinParameters(IDictionary parameters)
|
||||
{
|
||||
this.parameters = parameters;
|
||||
}
|
||||
|
||||
public IDictionary GetParameters()
|
||||
{
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public byte[] GetKey()
|
||||
{
|
||||
return (byte[])parameters[0];
|
||||
}
|
||||
|
||||
public byte[] GetPersonalisation()
|
||||
{
|
||||
return (byte[])parameters[8];
|
||||
}
|
||||
|
||||
public byte[] GetPublicKey()
|
||||
{
|
||||
return (byte[])parameters[12];
|
||||
}
|
||||
|
||||
public byte[] GetKeyIdentifier()
|
||||
{
|
||||
return (byte[])parameters[16];
|
||||
}
|
||||
|
||||
public byte[] GetNonce()
|
||||
{
|
||||
return (byte[])parameters[20];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using Org.BouncyCastle.Math;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public sealed class Srp6GroupParameters
|
||||
{
|
||||
private readonly BigInteger n;
|
||||
|
||||
private readonly BigInteger g;
|
||||
|
||||
public BigInteger G => g;
|
||||
|
||||
public BigInteger N => n;
|
||||
|
||||
public Srp6GroupParameters(BigInteger N, BigInteger g)
|
||||
{
|
||||
n = N;
|
||||
this.g = g;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using Org.BouncyCastle.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class TweakableBlockCipherParameters : ICipherParameters
|
||||
{
|
||||
private readonly byte[] tweak;
|
||||
|
||||
private readonly KeyParameter key;
|
||||
|
||||
public KeyParameter Key => key;
|
||||
|
||||
public byte[] Tweak => tweak;
|
||||
|
||||
public TweakableBlockCipherParameters(KeyParameter key, byte[] tweak)
|
||||
{
|
||||
this.key = key;
|
||||
this.tweak = Arrays.Clone(tweak);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using Org.BouncyCastle.Security;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class X25519KeyGenerationParameters : KeyGenerationParameters
|
||||
{
|
||||
public X25519KeyGenerationParameters(SecureRandom random)
|
||||
: base(random, 255)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Math.EC.Rfc7748;
|
||||
using Org.BouncyCastle.Security;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
using Org.BouncyCastle.Utilities.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public sealed class X25519PrivateKeyParameters : AsymmetricKeyParameter
|
||||
{
|
||||
public static readonly int KeySize = 32;
|
||||
|
||||
public static readonly int SecretSize = 32;
|
||||
|
||||
private readonly byte[] data = new byte[KeySize];
|
||||
|
||||
public X25519PrivateKeyParameters(SecureRandom random)
|
||||
: base(privateKey: true)
|
||||
{
|
||||
X25519.GeneratePrivateKey(random, data);
|
||||
}
|
||||
|
||||
public X25519PrivateKeyParameters(byte[] buf, int off)
|
||||
: base(privateKey: true)
|
||||
{
|
||||
Array.Copy(buf, off, data, 0, KeySize);
|
||||
}
|
||||
|
||||
public X25519PrivateKeyParameters(Stream input)
|
||||
: base(privateKey: true)
|
||||
{
|
||||
if (KeySize != Streams.ReadFully(input, data))
|
||||
{
|
||||
throw new EndOfStreamException("EOF encountered in middle of X25519 private key");
|
||||
}
|
||||
}
|
||||
|
||||
public void Encode(byte[] buf, int off)
|
||||
{
|
||||
Array.Copy(data, 0, buf, off, KeySize);
|
||||
}
|
||||
|
||||
public byte[] GetEncoded()
|
||||
{
|
||||
return Arrays.Clone(data);
|
||||
}
|
||||
|
||||
public X25519PublicKeyParameters GeneratePublicKey()
|
||||
{
|
||||
byte[] array = new byte[32];
|
||||
X25519.GeneratePublicKey(data, 0, array, 0);
|
||||
return new X25519PublicKeyParameters(array, 0);
|
||||
}
|
||||
|
||||
public void GenerateSecret(X25519PublicKeyParameters publicKey, byte[] buf, int off)
|
||||
{
|
||||
byte[] array = new byte[32];
|
||||
publicKey.Encode(array, 0);
|
||||
if (!X25519.CalculateAgreement(data, 0, array, 0, buf, off))
|
||||
{
|
||||
throw new InvalidOperationException("X25519 agreement failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
using Org.BouncyCastle.Utilities.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public sealed class X25519PublicKeyParameters : AsymmetricKeyParameter
|
||||
{
|
||||
public static readonly int KeySize = 32;
|
||||
|
||||
private readonly byte[] data = new byte[KeySize];
|
||||
|
||||
public X25519PublicKeyParameters(byte[] buf, int off)
|
||||
: base(privateKey: false)
|
||||
{
|
||||
Array.Copy(buf, off, data, 0, KeySize);
|
||||
}
|
||||
|
||||
public X25519PublicKeyParameters(Stream input)
|
||||
: base(privateKey: false)
|
||||
{
|
||||
if (KeySize != Streams.ReadFully(input, data))
|
||||
{
|
||||
throw new EndOfStreamException("EOF encountered in middle of X25519 public key");
|
||||
}
|
||||
}
|
||||
|
||||
public void Encode(byte[] buf, int off)
|
||||
{
|
||||
Array.Copy(data, 0, buf, off, KeySize);
|
||||
}
|
||||
|
||||
public byte[] GetEncoded()
|
||||
{
|
||||
return Arrays.Clone(data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using Org.BouncyCastle.Security;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public class X448KeyGenerationParameters : KeyGenerationParameters
|
||||
{
|
||||
public X448KeyGenerationParameters(SecureRandom random)
|
||||
: base(random, 448)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Math.EC.Rfc7748;
|
||||
using Org.BouncyCastle.Security;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
using Org.BouncyCastle.Utilities.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public sealed class X448PrivateKeyParameters : AsymmetricKeyParameter
|
||||
{
|
||||
public static readonly int KeySize = 56;
|
||||
|
||||
public static readonly int SecretSize = 56;
|
||||
|
||||
private readonly byte[] data = new byte[KeySize];
|
||||
|
||||
public X448PrivateKeyParameters(SecureRandom random)
|
||||
: base(privateKey: true)
|
||||
{
|
||||
X448.GeneratePrivateKey(random, data);
|
||||
}
|
||||
|
||||
public X448PrivateKeyParameters(byte[] buf, int off)
|
||||
: base(privateKey: true)
|
||||
{
|
||||
Array.Copy(buf, off, data, 0, KeySize);
|
||||
}
|
||||
|
||||
public X448PrivateKeyParameters(Stream input)
|
||||
: base(privateKey: true)
|
||||
{
|
||||
if (KeySize != Streams.ReadFully(input, data))
|
||||
{
|
||||
throw new EndOfStreamException("EOF encountered in middle of X448 private key");
|
||||
}
|
||||
}
|
||||
|
||||
public void Encode(byte[] buf, int off)
|
||||
{
|
||||
Array.Copy(data, 0, buf, off, KeySize);
|
||||
}
|
||||
|
||||
public byte[] GetEncoded()
|
||||
{
|
||||
return Arrays.Clone(data);
|
||||
}
|
||||
|
||||
public X448PublicKeyParameters GeneratePublicKey()
|
||||
{
|
||||
byte[] array = new byte[56];
|
||||
X448.GeneratePublicKey(data, 0, array, 0);
|
||||
return new X448PublicKeyParameters(array, 0);
|
||||
}
|
||||
|
||||
public void GenerateSecret(X448PublicKeyParameters publicKey, byte[] buf, int off)
|
||||
{
|
||||
byte[] array = new byte[56];
|
||||
publicKey.Encode(array, 0);
|
||||
if (!X448.CalculateAgreement(data, 0, array, 0, buf, off))
|
||||
{
|
||||
throw new InvalidOperationException("X448 agreement failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Utilities;
|
||||
using Org.BouncyCastle.Utilities.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
public sealed class X448PublicKeyParameters : AsymmetricKeyParameter
|
||||
{
|
||||
public static readonly int KeySize = 56;
|
||||
|
||||
private readonly byte[] data = new byte[KeySize];
|
||||
|
||||
public X448PublicKeyParameters(byte[] buf, int off)
|
||||
: base(privateKey: false)
|
||||
{
|
||||
Array.Copy(buf, off, data, 0, KeySize);
|
||||
}
|
||||
|
||||
public X448PublicKeyParameters(Stream input)
|
||||
: base(privateKey: false)
|
||||
{
|
||||
if (KeySize != Streams.ReadFully(input, data))
|
||||
{
|
||||
throw new EndOfStreamException("EOF encountered in middle of X448 public key");
|
||||
}
|
||||
}
|
||||
|
||||
public void Encode(byte[] buf, int off)
|
||||
{
|
||||
Array.Copy(data, 0, buf, off, KeySize);
|
||||
}
|
||||
|
||||
public byte[] GetEncoded()
|
||||
{
|
||||
return Arrays.Clone(data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user