Initial Commit

This commit is contained in:
2023-06-21 12:46:23 -04:00
commit c70248a520
1352 changed files with 336780 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
using System;
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;
/**
* Base constructor.
*
* @param key key to be used by underlying cipher
* @param macSize macSize in bits
* @param nonce nonce to be used
* @param associatedText associated text, if any
*/
public AeadParameters(
KeyParameter key,
int macSize,
byte[] nonce,
byte[] associatedText)
{
this.key = key;
this.nonce = nonce;
this.macSize = macSize;
this.associatedText = associatedText;
}
public virtual KeyParameter Key
{
get { return key; }
}
public virtual int MacSize
{
get { return macSize; }
}
public virtual byte[] GetAssociatedText()
{
return associatedText;
}
public virtual byte[] GetNonce()
{
return nonce;
}
}
}

View File

@@ -0,0 +1,25 @@
using System;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class CcmParameters
: AeadParameters
{
/**
* Base constructor.
*
* @param key key to be used by underlying cipher
* @param macSize macSize in bits
* @param nonce nonce to be used
* @param associatedText associated text, if any
*/
public CcmParameters(
KeyParameter key,
int macSize,
byte[] nonce,
byte[] associatedText)
: base(key, macSize, nonce, associatedText)
{
}
}
}

View File

@@ -0,0 +1,25 @@
using System;
using Org.BouncyCastle.Security;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class DHKeyGenerationParameters
: KeyGenerationParameters
{
private readonly DHParameters parameters;
public DHKeyGenerationParameters(
SecureRandom random,
DHParameters parameters)
: base(random, parameters.P.BitLength)
{
this.parameters = parameters;
}
public DHParameters Parameters
{
get { return parameters; }
}
}
}

View File

@@ -0,0 +1,59 @@
using System;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class DHKeyParameters
: AsymmetricKeyParameter
{
private readonly DHParameters parameters;
protected DHKeyParameters(
bool isPrivate,
DHParameters parameters)
: base(isPrivate)
{
// TODO Should we allow parameters to be null?
this.parameters = parameters;
}
public DHParameters Parameters
{
get { return parameters; }
}
public override bool Equals(
object obj)
{
if (obj == this)
return true;
DHKeyParameters other = obj as DHKeyParameters;
if (other == null)
return false;
return Equals(other);
}
protected bool Equals(
DHKeyParameters other)
{
return Platform.Equals(parameters, other.parameters)
&& base.Equals(other);
}
public override int GetHashCode()
{
int hc = base.GetHashCode();
if (parameters != null)
{
hc ^= parameters.GetHashCode();
}
return hc;
}
}
}

View File

@@ -0,0 +1,178 @@
using System;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class DHParameters
: ICipherParameters
{
private const int DefaultMinimumLength = 160;
private readonly BigInteger p, g, q, j;
private readonly int m, l;
private readonly DHValidationParameters validation;
private static int GetDefaultM(
BigInteger p,
int l)
{
int effectiveL = l != 0 ? l : p.BitLength - 1;
return System.Math.Min(DefaultMinimumLength, effectiveL);
// return DefaultMinimumLength;
}
public DHParameters(
BigInteger p,
BigInteger g)
: this(p, g, null, GetDefaultM(p, 0), 0, null, null)
{
}
public DHParameters(
BigInteger p,
BigInteger g,
BigInteger q)
: this(p, g, q, GetDefaultM(p, 0), 0, null, null)
{
}
public DHParameters(
BigInteger p,
BigInteger g,
BigInteger q,
int l)
: this(p, g, q, GetDefaultM(p, 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, GetDefaultM(p, 0), 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 (m >= p.BitLength)
throw new ArgumentException("m value must be < bitlength of p", "m");
if (l != 0 && l < m)
throw new ArgumentException("l value must be >= m, or zero", "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 BigInteger P
{
get { return p; }
}
public BigInteger G
{
get { return g; }
}
public BigInteger Q
{
get { return q; }
}
public BigInteger J
{
get { return j; }
}
/// <summary>The minimum bitlength of the private value.</summary>
public int M
{
get { return m; }
}
/// <summary>The bitlength of the private value.</summary>
/// <remarks>If zero, bitLength(p) - 1 will be used.</remarks>
public int L
{
get { return l; }
}
public DHValidationParameters ValidationParameters
{
get { return validation; }
}
public override bool Equals(
object obj)
{
if (obj == this)
return true;
DHParameters other = obj as DHParameters;
if (other == null)
return false;
return Equals(other);
}
protected bool Equals(
DHParameters other)
{
return p.Equals(other.p)
&& g.Equals(other.g)
&& Platform.Equals(q, other.q);
}
public override int GetHashCode()
{
int hc = p.GetHashCode() ^ g.GetHashCode();
if (q != null)
{
hc ^= q.GetHashCode();
}
return hc;
}
}
}

View File

@@ -0,0 +1,50 @@
using System;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class DHPrivateKeyParameters
: DHKeyParameters
{
private readonly BigInteger x;
public DHPrivateKeyParameters(
BigInteger x,
DHParameters parameters)
: base(true, parameters)
{
this.x = x;
}
public BigInteger X
{
get { return x; }
}
public override bool Equals(
object obj)
{
if (obj == this)
return true;
DHPrivateKeyParameters other = obj as DHPrivateKeyParameters;
if (other == null)
return false;
return Equals(other);
}
protected bool Equals(
DHPrivateKeyParameters other)
{
return x.Equals(other.x) && base.Equals(other);
}
public override int GetHashCode()
{
return x.GetHashCode() ^ base.GetHashCode();
}
}
}

View File

@@ -0,0 +1,53 @@
using System;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class DHPublicKeyParameters
: DHKeyParameters
{
private readonly BigInteger y;
public DHPublicKeyParameters(
BigInteger y,
DHParameters parameters)
: base(false, parameters)
{
if (y == null)
throw new ArgumentNullException("y");
this.y = y;
}
public BigInteger Y
{
get { return y; }
}
public override bool Equals(
object obj)
{
if (obj == this)
return true;
DHPublicKeyParameters other = obj as DHPublicKeyParameters;
if (other == null)
return false;
return Equals(other);
}
protected bool Equals(
DHPublicKeyParameters other)
{
return y.Equals(other.y) && base.Equals(other);
}
public override int GetHashCode()
{
return y.GetHashCode() ^ base.GetHashCode();
}
}
}

View File

@@ -0,0 +1,59 @@
using System;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class DHValidationParameters
{
private readonly byte[] seed;
private readonly int 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 int Counter
{
get { return counter; }
}
public override bool Equals(
object obj)
{
if (obj == this)
return true;
DHValidationParameters other = obj as DHValidationParameters;
if (other == null)
return false;
return Equals(other);
}
protected bool Equals(
DHValidationParameters other)
{
return counter == other.counter
&& Arrays.AreEqual(this.seed, other.seed);
}
public override int GetHashCode()
{
return counter.GetHashCode() ^ Arrays.GetHashCode(seed);
}
}
}

View File

@@ -0,0 +1,95 @@
using System;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class DesEdeParameters
: DesParameters
{
/*
* DES-EDE Key length in bytes.
*/
public const int DesEdeKeyLength = 24;
private static byte[] FixKey(
byte[] key,
int keyOff,
int keyLen)
{
byte[] tmp = new byte[24];
switch (keyLen)
{
case 16:
Array.Copy(key, keyOff, tmp, 0, 16);
Array.Copy(key, keyOff, tmp, 16, 8);
break;
case 24:
Array.Copy(key, keyOff, tmp, 0, 24);
break;
default:
throw new ArgumentException("Bad length for DESede key: " + keyLen, "keyLen");
}
if (IsWeakKey(tmp))
throw new ArgumentException("attempt to create weak DESede key");
return tmp;
}
public DesEdeParameters(
byte[] key)
: base(FixKey(key, 0, key.Length))
{
}
public DesEdeParameters(
byte[] key,
int keyOff,
int keyLen)
: base(FixKey(key, keyOff, keyLen))
{
}
/**
* return true if the passed in key is a DES-EDE weak key.
*
* @param key bytes making up the key
* @param offset offset into the byte array the key starts at
* @param length number of bytes making up the key
*/
public static bool IsWeakKey(
byte[] key,
int offset,
int length)
{
for (int i = offset; i < length; i += DesKeyLength)
{
if (DesParameters.IsWeakKey(key, i))
{
return true;
}
}
return false;
}
/**
* return true if the passed in key is a DES-EDE weak key.
*
* @param key bytes making up the key
* @param offset offset into the byte array the key starts at
*/
public static new bool IsWeakKey(
byte[] key,
int offset)
{
return IsWeakKey(key, offset, key.Length - offset);
}
public static new bool IsWeakKey(
byte[] key)
{
return IsWeakKey(key, 0, key.Length);
}
}
}

View File

@@ -0,0 +1,130 @@
using System;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class DesParameters
: KeyParameter
{
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");
}
/*
* DES Key Length in bytes.
*/
public const int DesKeyLength = 8;
/*
* Table of weak and semi-weak keys taken from Schneier pp281
*/
private const int N_DES_WEAK_KEYS = 16;
private static readonly byte[] DES_weak_keys =
{
/* weak keys */
(byte)0x01,(byte)0x01,(byte)0x01,(byte)0x01, (byte)0x01,(byte)0x01,(byte)0x01,(byte)0x01,
(byte)0x1f,(byte)0x1f,(byte)0x1f,(byte)0x1f, (byte)0x0e,(byte)0x0e,(byte)0x0e,(byte)0x0e,
(byte)0xe0,(byte)0xe0,(byte)0xe0,(byte)0xe0, (byte)0xf1,(byte)0xf1,(byte)0xf1,(byte)0xf1,
(byte)0xfe,(byte)0xfe,(byte)0xfe,(byte)0xfe, (byte)0xfe,(byte)0xfe,(byte)0xfe,(byte)0xfe,
/* semi-weak keys */
(byte)0x01,(byte)0xfe,(byte)0x01,(byte)0xfe, (byte)0x01,(byte)0xfe,(byte)0x01,(byte)0xfe,
(byte)0x1f,(byte)0xe0,(byte)0x1f,(byte)0xe0, (byte)0x0e,(byte)0xf1,(byte)0x0e,(byte)0xf1,
(byte)0x01,(byte)0xe0,(byte)0x01,(byte)0xe0, (byte)0x01,(byte)0xf1,(byte)0x01,(byte)0xf1,
(byte)0x1f,(byte)0xfe,(byte)0x1f,(byte)0xfe, (byte)0x0e,(byte)0xfe,(byte)0x0e,(byte)0xfe,
(byte)0x01,(byte)0x1f,(byte)0x01,(byte)0x1f, (byte)0x01,(byte)0x0e,(byte)0x01,(byte)0x0e,
(byte)0xe0,(byte)0xfe,(byte)0xe0,(byte)0xfe, (byte)0xf1,(byte)0xfe,(byte)0xf1,(byte)0xfe,
(byte)0xfe,(byte)0x01,(byte)0xfe,(byte)0x01, (byte)0xfe,(byte)0x01,(byte)0xfe,(byte)0x01,
(byte)0xe0,(byte)0x1f,(byte)0xe0,(byte)0x1f, (byte)0xf1,(byte)0x0e,(byte)0xf1,(byte)0x0e,
(byte)0xe0,(byte)0x01,(byte)0xe0,(byte)0x01, (byte)0xf1,(byte)0x01,(byte)0xf1,(byte)0x01,
(byte)0xfe,(byte)0x1f,(byte)0xfe,(byte)0x1f, (byte)0xfe,(byte)0x0e,(byte)0xfe,(byte)0x0e,
(byte)0x1f,(byte)0x01,(byte)0x1f,(byte)0x01, (byte)0x0e,(byte)0x01,(byte)0x0e,(byte)0x01,
(byte)0xfe,(byte)0xe0,(byte)0xfe,(byte)0xe0, (byte)0xfe,(byte)0xf1,(byte)0xfe,(byte)0xf1
};
/**
* DES has 16 weak keys. This method will check
* if the given DES key material is weak or semi-weak.
* Key material that is too short is regarded as weak.
* <p>
* See <a href="http://www.counterpane.com/applied.html">"Applied
* Cryptography"</a> by Bruce Schneier for more information.
* </p>
* @return true if the given DES key material is weak or semi-weak,
* false otherwise.
*/
public static bool IsWeakKey(
byte[] key,
int offset)
{
if (key.Length - offset < DesKeyLength)
throw new ArgumentException("key material too short.");
//nextkey:
for (int i = 0; i < N_DES_WEAK_KEYS; i++)
{
bool unmatch = false;
for (int j = 0; j < DesKeyLength; j++)
{
if (key[j + offset] != DES_weak_keys[i * DesKeyLength + j])
{
//continue nextkey;
unmatch = true;
break;
}
}
if (!unmatch)
{
return true;
}
}
return false;
}
public static bool IsWeakKey(
byte[] key)
{
return IsWeakKey(key, 0);
}
/**
* DES Keys use the LSB as the odd parity bit. This can
* be used to check for corrupt keys.
*
* @param bytes the byte array to set the parity on.
*/
public static void SetOddParity(
byte[] bytes)
{
for (int i = 0; i < bytes.Length; i++)
{
int b = bytes[i];
bytes[i] = (byte)((b & 0xfe) |
((((b >> 1) ^
(b >> 2) ^
(b >> 3) ^
(b >> 4) ^
(b >> 5) ^
(b >> 6) ^
(b >> 7)) ^ 0x01) & 0x01));
}
}
}
}

View File

@@ -0,0 +1,26 @@
using System;
using Org.BouncyCastle.Security;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class DsaKeyGenerationParameters
: KeyGenerationParameters
{
private readonly DsaParameters parameters;
public DsaKeyGenerationParameters(
SecureRandom random,
DsaParameters parameters)
: base(random, parameters.P.BitLength - 1)
{
this.parameters = parameters;
}
public DsaParameters Parameters
{
get { return parameters; }
}
}
}

View File

@@ -0,0 +1,59 @@
using System;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class DsaKeyParameters
: AsymmetricKeyParameter
{
private readonly DsaParameters parameters;
public DsaKeyParameters(
bool isPrivate,
DsaParameters parameters)
: base(isPrivate)
{
// Note: parameters may be null
this.parameters = parameters;
}
public DsaParameters Parameters
{
get { return parameters; }
}
public override bool Equals(
object obj)
{
if (obj == this)
return true;
DsaKeyParameters other = obj as DsaKeyParameters;
if (other == null)
return false;
return Equals(other);
}
protected bool Equals(
DsaKeyParameters other)
{
return Platform.Equals(parameters, other.parameters)
&& base.Equals(other);
}
public override int GetHashCode()
{
int hc = base.GetHashCode();
if (parameters != null)
{
hc ^= parameters.GetHashCode();
}
return hc;
}
}
}

View File

@@ -0,0 +1,85 @@
using System;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class DsaParameters
: ICipherParameters
{
private readonly BigInteger p, q , g;
private readonly DsaValidationParameters 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;
this.validation = parameters;
}
public BigInteger P
{
get { return p; }
}
public BigInteger Q
{
get { return q; }
}
public BigInteger G
{
get { return g; }
}
public DsaValidationParameters ValidationParameters
{
get { return validation; }
}
public override bool Equals(
object obj)
{
if (obj == this)
return true;
DsaParameters other = obj as DsaParameters;
if (other == null)
return false;
return Equals(other);
}
protected bool Equals(
DsaParameters other)
{
return p.Equals(other.p) && q.Equals(other.q) && g.Equals(other.g);
}
public override int GetHashCode()
{
return p.GetHashCode() ^ q.GetHashCode() ^ g.GetHashCode();
}
}
}

View File

@@ -0,0 +1,53 @@
using System;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class DsaPrivateKeyParameters
: DsaKeyParameters
{
private readonly BigInteger x;
public DsaPrivateKeyParameters(
BigInteger x,
DsaParameters parameters)
: base(true, parameters)
{
if (x == null)
throw new ArgumentNullException("x");
this.x = x;
}
public BigInteger X
{
get { return x; }
}
public override bool Equals(
object obj)
{
if (obj == this)
return true;
DsaPrivateKeyParameters other = obj as DsaPrivateKeyParameters;
if (other == null)
return false;
return Equals(other);
}
protected bool Equals(
DsaPrivateKeyParameters other)
{
return x.Equals(other.x) && base.Equals(other);
}
public override int GetHashCode()
{
return x.GetHashCode() ^ base.GetHashCode();
}
}
}

View File

@@ -0,0 +1,52 @@
using System;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class DsaPublicKeyParameters
: DsaKeyParameters
{
private readonly BigInteger y;
public DsaPublicKeyParameters(
BigInteger y,
DsaParameters parameters)
: base(false, parameters)
{
if (y == null)
throw new ArgumentNullException("y");
this.y = y;
}
public BigInteger Y
{
get { return y; }
}
public override bool Equals(object obj)
{
if (obj == this)
return true;
DsaPublicKeyParameters other = obj as DsaPublicKeyParameters;
if (other == null)
return false;
return Equals(other);
}
protected bool Equals(
DsaPublicKeyParameters other)
{
return y.Equals(other.y) && base.Equals(other);
}
public override int GetHashCode()
{
return y.GetHashCode() ^ base.GetHashCode();
}
}
}

View File

@@ -0,0 +1,59 @@
using System;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class DsaValidationParameters
{
private readonly byte[] seed;
private readonly int counter;
public DsaValidationParameters(
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 int Counter
{
get { return counter; }
}
public override bool Equals(
object obj)
{
if (obj == this)
return true;
DsaValidationParameters other = obj as DsaValidationParameters;
if (other == null)
return false;
return Equals(other);
}
protected bool Equals(
DsaValidationParameters other)
{
return counter == other.counter
&& Arrays.AreEqual(seed, other.seed);
}
public override int GetHashCode()
{
return counter.GetHashCode() ^ Arrays.GetHashCode(seed);
}
}
}

View File

@@ -0,0 +1,116 @@
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;
public ECDomainParameters(
ECCurve curve,
ECPoint g,
BigInteger n)
: this(curve, g, n, BigInteger.One)
{
}
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");
if (h == null)
throw new ArgumentNullException("h");
this.curve = curve;
this.g = g;
this.n = n;
this.h = h;
this.seed = Arrays.Clone(seed);
}
public ECCurve Curve
{
get { return curve; }
}
public ECPoint G
{
get { return g; }
}
public BigInteger N
{
get { return n; }
}
public BigInteger H
{
get { return h; }
}
public byte[] GetSeed()
{
return Arrays.Clone(seed);
}
public override bool Equals(
object obj)
{
if (obj == this)
return true;
ECDomainParameters other = obj as ECDomainParameters;
if (other == null)
return false;
return Equals(other);
}
protected bool Equals(
ECDomainParameters other)
{
return curve.Equals(other.curve)
&& g.Equals(other.g)
&& n.Equals(other.n)
&& h.Equals(other.h)
&& Arrays.AreEqual(seed, other.seed);
}
public override int GetHashCode()
{
return curve.GetHashCode()
^ g.GetHashCode()
^ n.GetHashCode()
^ h.GetHashCode()
^ Arrays.GetHashCode(seed);
}
}
}

View File

@@ -0,0 +1,55 @@
using System;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.CryptoPro;
using Org.BouncyCastle.Security;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class ECKeyGenerationParameters
: KeyGenerationParameters
{
private readonly ECDomainParameters domainParams;
private readonly DerObjectIdentifier publicKeyParamSet;
public ECKeyGenerationParameters(
ECDomainParameters domainParameters,
SecureRandom random)
: base(random, domainParameters.N.BitLength)
{
this.domainParams = domainParameters;
}
public ECKeyGenerationParameters(
DerObjectIdentifier publicKeyParamSet,
SecureRandom random)
: this(LookupParameters(publicKeyParamSet), random)
{
this.publicKeyParamSet = publicKeyParamSet;
}
public ECDomainParameters DomainParameters
{
get { return domainParams; }
}
public DerObjectIdentifier PublicKeyParamSet
{
get { return publicKeyParamSet; }
}
private static ECDomainParameters LookupParameters(
DerObjectIdentifier publicKeyParamSet)
{
if (publicKeyParamSet == null)
throw new ArgumentNullException("publicKeyParamSet");
ECDomainParameters p = ECGost3410NamedCurves.GetByOid(publicKeyParamSet);
if (p == null)
throw new ArgumentException("OID is not a valid CryptoPro public key parameter set", "publicKeyParamSet");
return p;
}
}
}

View File

@@ -0,0 +1,121 @@
using System;
using System.Globalization;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.CryptoPro;
namespace Org.BouncyCastle.Crypto.Parameters
{
public abstract class ECKeyParameters
: AsymmetricKeyParameter
{
private readonly string algorithm;
private readonly ECDomainParameters parameters;
private readonly DerObjectIdentifier 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);
this.parameters = LookupParameters(publicKeyParamSet);
this.publicKeyParamSet = publicKeyParamSet;
}
public string AlgorithmName
{
get { return algorithm; }
}
public ECDomainParameters Parameters
{
get { return parameters; }
}
public DerObjectIdentifier PublicKeyParamSet
{
get { return publicKeyParamSet; }
}
public override bool Equals(
object obj)
{
if (obj == this)
return true;
ECDomainParameters other = obj as ECDomainParameters;
if (other == null)
return false;
return Equals(other);
}
protected bool Equals(
ECKeyParameters other)
{
return parameters.Equals(other.parameters) && base.Equals(other);
}
public override int GetHashCode()
{
return parameters.GetHashCode() ^ base.GetHashCode();
}
private string VerifyAlgorithmName(
string algorithm)
{
string upper = algorithm.ToUpper(CultureInfo.InvariantCulture);
switch (upper)
{
case "EC":
case "ECDSA":
case "ECGOST3410":
case "ECDH":
case "ECDHC":
break;
default:
throw new ArgumentException("unrecognised algorithm: " + algorithm, "algorithm");
}
return upper;
}
private static ECDomainParameters LookupParameters(
DerObjectIdentifier publicKeyParamSet)
{
if (publicKeyParamSet == null)
throw new ArgumentNullException("publicKeyParamSet");
ECDomainParameters p = ECGost3410NamedCurves.GetByOid(publicKeyParamSet);
if (p == null)
throw new ArgumentException("OID is not a valid CryptoPro public key parameter set", "publicKeyParamSet");
return p;
}
}
}

View File

@@ -0,0 +1,74 @@
using System;
using System.Globalization;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class ECPrivateKeyParameters
: ECKeyParameters
{
private readonly BigInteger d;
public ECPrivateKeyParameters(
BigInteger d,
ECDomainParameters parameters)
: this("EC", d, parameters)
{
}
public ECPrivateKeyParameters(
BigInteger d,
DerObjectIdentifier publicKeyParamSet)
: base("ECGOST3410", true, publicKeyParamSet)
{
if (d == null)
throw new ArgumentNullException("d");
this.d = d;
}
public ECPrivateKeyParameters(
string algorithm,
BigInteger d,
ECDomainParameters parameters)
: base(algorithm, true, parameters)
{
if (d == null)
throw new ArgumentNullException("d");
this.d = d;
}
public BigInteger D
{
get { return d; }
}
public override bool Equals(
object obj)
{
if (obj == this)
return true;
ECPrivateKeyParameters other = obj as ECPrivateKeyParameters;
if (other == null)
return false;
return Equals(other);
}
protected bool Equals(
ECPrivateKeyParameters other)
{
return d.Equals(other.d) && base.Equals(other);
}
public override int GetHashCode()
{
return d.GetHashCode() ^ base.GetHashCode();
}
}
}

View File

@@ -0,0 +1,73 @@
using System;
using System.Globalization;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Math.EC;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class ECPublicKeyParameters
: ECKeyParameters
{
private readonly ECPoint q;
public ECPublicKeyParameters(
ECPoint q,
ECDomainParameters parameters)
: this("EC", q, parameters)
{
}
public ECPublicKeyParameters(
ECPoint q,
DerObjectIdentifier publicKeyParamSet)
: base("ECGOST3410", false, publicKeyParamSet)
{
if (q == null)
throw new ArgumentNullException("q");
this.q = q;
}
public ECPublicKeyParameters(
string algorithm,
ECPoint q,
ECDomainParameters parameters)
: base(algorithm, false, parameters)
{
if (q == null)
throw new ArgumentNullException("q");
this.q = q;
}
public ECPoint Q
{
get { return q; }
}
public override bool Equals(object obj)
{
if (obj == this)
return true;
ECPublicKeyParameters other = obj as ECPublicKeyParameters;
if (other == null)
return false;
return Equals(other);
}
protected bool Equals(
ECPublicKeyParameters other)
{
return q.Equals(other.q) && base.Equals(other);
}
public override int GetHashCode()
{
return q.GetHashCode() ^ base.GetHashCode();
}
}
}

View File

@@ -0,0 +1,25 @@
using System;
using Org.BouncyCastle.Security;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class ElGamalKeyGenerationParameters
: KeyGenerationParameters
{
private readonly ElGamalParameters parameters;
public ElGamalKeyGenerationParameters(
SecureRandom random,
ElGamalParameters parameters)
: base(random, parameters.P.BitLength)
{
this.parameters = parameters;
}
public ElGamalParameters Parameters
{
get { return parameters; }
}
}
}

View File

@@ -0,0 +1,59 @@
using System;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class ElGamalKeyParameters
: AsymmetricKeyParameter
{
private readonly ElGamalParameters parameters;
protected ElGamalKeyParameters(
bool isPrivate,
ElGamalParameters parameters)
: base(isPrivate)
{
// TODO Should we allow 'parameters' to be null?
this.parameters = parameters;
}
public ElGamalParameters Parameters
{
get { return parameters; }
}
public override bool Equals(
object obj)
{
if (obj == this)
return true;
ElGamalKeyParameters other = obj as ElGamalKeyParameters;
if (other == null)
return false;
return Equals(other);
}
protected bool Equals(
ElGamalKeyParameters other)
{
return Platform.Equals(parameters, other.parameters)
&& base.Equals(other);
}
public override int GetHashCode()
{
int hc = base.GetHashCode();
if (parameters != null)
{
hc ^= parameters.GetHashCode();
}
return hc;
}
}
}

View File

@@ -0,0 +1,81 @@
using System;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class ElGamalParameters
: ICipherParameters
{
private readonly BigInteger p, g;
private readonly int 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 BigInteger P
{
get { return p; }
}
/**
* return the generator - g
*/
public BigInteger G
{
get { return g; }
}
/**
* return private value limit - l
*/
public int L
{
get { return l; }
}
public override bool Equals(
object obj)
{
if (obj == this)
return true;
ElGamalParameters other = obj as ElGamalParameters;
if (other == null)
return false;
return Equals(other);
}
protected bool Equals(
ElGamalParameters other)
{
return p.Equals(other.p) && g.Equals(other.g) && l == other.l;
}
public override int GetHashCode()
{
return p.GetHashCode() ^ g.GetHashCode() ^ l;
}
}
}

View File

@@ -0,0 +1,53 @@
using System;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class ElGamalPrivateKeyParameters
: ElGamalKeyParameters
{
private readonly BigInteger x;
public ElGamalPrivateKeyParameters(
BigInteger x,
ElGamalParameters parameters)
: base(true, parameters)
{
if (x == null)
throw new ArgumentNullException("x");
this.x = x;
}
public BigInteger X
{
get { return x; }
}
public override bool Equals(
object obj)
{
if (obj == this)
return true;
ElGamalPrivateKeyParameters other = obj as ElGamalPrivateKeyParameters;
if (other == null)
return false;
return Equals(other);
}
protected bool Equals(
ElGamalPrivateKeyParameters other)
{
return other.x.Equals(x) && base.Equals(other);
}
public override int GetHashCode()
{
return x.GetHashCode() ^ base.GetHashCode();
}
}
}

View File

@@ -0,0 +1,53 @@
using System;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class ElGamalPublicKeyParameters
: ElGamalKeyParameters
{
private readonly BigInteger y;
public ElGamalPublicKeyParameters(
BigInteger y,
ElGamalParameters parameters)
: base(false, parameters)
{
if (y == null)
throw new ArgumentNullException("y");
this.y = y;
}
public BigInteger Y
{
get { return y; }
}
public override bool Equals(
object obj)
{
if (obj == this)
return true;
ElGamalPublicKeyParameters other = obj as ElGamalPublicKeyParameters;
if (other == null)
return false;
return Equals(other);
}
protected bool Equals(
ElGamalPublicKeyParameters other)
{
return y.Equals(other.y) && base.Equals(other);
}
public override int GetHashCode()
{
return y.GetHashCode() ^ base.GetHashCode();
}
}
}

View File

@@ -0,0 +1,55 @@
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 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;
}
public Gost3410Parameters Parameters
{
get { return parameters; }
}
public DerObjectIdentifier PublicKeyParamSet
{
get { return publicKeyParamSet; }
}
private static Gost3410Parameters LookupParameters(
DerObjectIdentifier publicKeyParamSet)
{
if (publicKeyParamSet == null)
throw new ArgumentNullException("publicKeyParamSet");
Gost3410ParamSetParameters p = Gost3410NamedParameters.GetByOid(publicKeyParamSet);
if (p == null)
throw new ArgumentException("OID is not a valid CryptoPro public key parameter set", "publicKeyParamSet");
return new Gost3410Parameters(p.P, p.Q, p.A);
}
}
}

View File

@@ -0,0 +1,58 @@
using System;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.CryptoPro;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Crypto.Parameters
{
public abstract class Gost3410KeyParameters
: AsymmetricKeyParameter
{
private readonly Gost3410Parameters parameters;
private readonly DerObjectIdentifier publicKeyParamSet;
protected Gost3410KeyParameters(
bool isPrivate,
Gost3410Parameters parameters)
: base(isPrivate)
{
this.parameters = parameters;
}
protected Gost3410KeyParameters(
bool isPrivate,
DerObjectIdentifier publicKeyParamSet)
: base(isPrivate)
{
this.parameters = LookupParameters(publicKeyParamSet);
this.publicKeyParamSet = publicKeyParamSet;
}
public Gost3410Parameters Parameters
{
get { return parameters; }
}
public DerObjectIdentifier PublicKeyParamSet
{
get { return publicKeyParamSet; }
}
// TODO Implement Equals/GetHashCode
private static Gost3410Parameters LookupParameters(
DerObjectIdentifier publicKeyParamSet)
{
if (publicKeyParamSet == null)
throw new ArgumentNullException("publicKeyParamSet");
Gost3410ParamSetParameters p = Gost3410NamedParameters.GetByOid(publicKeyParamSet);
if (p == null)
throw new ArgumentException("OID is not a valid CryptoPro public key parameter set", "publicKeyParamSet");
return new Gost3410Parameters(p.P, p.Q, p.A);
}
}
}

View File

@@ -0,0 +1,86 @@
using System;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class Gost3410Parameters
: ICipherParameters
{
private readonly BigInteger p, q, a;
private readonly Gost3410ValidationParameters 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 BigInteger P
{
get { return p; }
}
public BigInteger Q
{
get { return q; }
}
public BigInteger A
{
get { return a; }
}
public Gost3410ValidationParameters ValidationParameters
{
get { return validation; }
}
public override bool Equals(
object obj)
{
if (obj == this)
return true;
Gost3410Parameters other = obj as Gost3410Parameters;
if (other == null)
return false;
return Equals(other);
}
protected bool Equals(
Gost3410Parameters other)
{
return p.Equals(other.p) && q.Equals(other.q) && a.Equals(other.a);
}
public override int GetHashCode()
{
return p.GetHashCode() ^ q.GetHashCode() ^ a.GetHashCode();
}
}
}

View File

@@ -0,0 +1,41 @@
using System;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.CryptoPro;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class Gost3410PrivateKeyParameters
: Gost3410KeyParameters
{
private readonly BigInteger x;
public Gost3410PrivateKeyParameters(
BigInteger x,
Gost3410Parameters parameters)
: base(true, parameters)
{
if (x.SignValue < 1 || x.BitLength > 256 || x.CompareTo(Parameters.Q) >= 0)
throw new ArgumentException("Invalid x for GOST3410 private key", "x");
this.x = x;
}
public Gost3410PrivateKeyParameters(
BigInteger x,
DerObjectIdentifier publicKeyParamSet)
: base(true, publicKeyParamSet)
{
if (x.SignValue < 1 || x.BitLength > 256 || x.CompareTo(Parameters.Q) >= 0)
throw new ArgumentException("Invalid x for GOST3410 private key", "x");
this.x = x;
}
public BigInteger X
{
get { return x; }
}
}
}

View File

@@ -0,0 +1,40 @@
using System;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class Gost3410PublicKeyParameters
: Gost3410KeyParameters
{
private readonly BigInteger y;
public Gost3410PublicKeyParameters(
BigInteger y,
Gost3410Parameters parameters)
: base(false, parameters)
{
if (y.SignValue < 1 || y.CompareTo(Parameters.P) >= 0)
throw new ArgumentException("Invalid y for GOST3410 public key", "y");
this.y = y;
}
public Gost3410PublicKeyParameters(
BigInteger y,
DerObjectIdentifier publicKeyParamSet)
: base(false, publicKeyParamSet)
{
if (y.SignValue < 1 || y.CompareTo(Parameters.P) >= 0)
throw new ArgumentException("Invalid y for GOST3410 public key", "y");
this.y = y;
}
public BigInteger Y
{
get { return y; }
}
}
}

View File

@@ -0,0 +1,51 @@
using System;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class Gost3410ValidationParameters
{
private int x0;
private int c;
private long x0L;
private long cL;
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 int C { get { return c; } }
public int X0 { get { return x0; } }
public long CL { get { return cL; } }
public long X0L { get { return x0L; } }
public override bool Equals(
object obj)
{
Gost3410ValidationParameters other = obj as Gost3410ValidationParameters;
return other != null
&& other.c == this.c
&& other.x0 == this.x0
&& other.cL == this.cL
&& other.x0L == this.x0L;
}
public override int GetHashCode()
{
return c.GetHashCode() ^ x0.GetHashCode() ^ cL.GetHashCode() ^ x0L.GetHashCode();
}
}
}

View File

@@ -0,0 +1,25 @@
using System;
using Org.BouncyCastle.Crypto;
namespace Org.BouncyCastle.Crypto.Parameters
{
/**
* parameters for Key derivation functions for ISO-18033
*/
public class Iso18033KdfParameters
: IDerivationParameters
{
byte[] seed;
public Iso18033KdfParameters(
byte[] seed)
{
this.seed = seed;
}
public byte[] GetSeed()
{
return seed;
}
}
}

View File

@@ -0,0 +1,49 @@
using System;
using Org.BouncyCastle.Crypto;
namespace Org.BouncyCastle.Crypto.Parameters
{
/**
* parameters for using an integrated cipher in stream mode.
*/
public class IesParameters : ICipherParameters
{
private byte[] derivation;
private byte[] encoding;
private int macKeySize;
/**
* @param derivation the derivation parameter for the KDF function.
* @param encoding the encoding parameter for the KDF function.
* @param macKeySize the size of the MAC key (in bits).
*/
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;
}
public int MacKeySize
{
get
{
return macKeySize;
}
}
}
}

View File

@@ -0,0 +1,33 @@
using System;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class IesWithCipherParameters : IesParameters
{
private int cipherKeySize;
/**
* @param derivation the derivation parameter for the KDF function.
* @param encoding the encoding parameter for the KDF function.
* @param macKeySize the size of the MAC key (in bits).
* @param cipherKeySize the size of the associated Cipher key (in bits).
*/
public IesWithCipherParameters(
byte[] derivation,
byte[] encoding,
int macKeySize,
int cipherKeySize) : base(derivation, encoding, macKeySize)
{
this.cipherKeySize = cipherKeySize;
}
public int CipherKeySize
{
get
{
return cipherKeySize;
}
}
}
}

View File

@@ -0,0 +1,33 @@
using System;
using Org.BouncyCastle.Crypto;
namespace Org.BouncyCastle.Crypto.Parameters
{
/**
* parameters for Key derivation functions for IEEE P1363a
*/
public class KdfParameters : IDerivationParameters
{
byte[] iv;
byte[] shared;
public KdfParameters(
byte[] shared,
byte[] iv)
{
this.shared = shared;
this.iv = iv;
}
public byte[] GetSharedSecret()
{
return shared;
}
public byte[] GetIV()
{
return iv;
}
}
}

View File

@@ -0,0 +1,43 @@
using System;
using Org.BouncyCastle.Crypto;
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 || (keyOff + keyLen) > key.Length)
throw new ArgumentOutOfRangeException("keyLen");
this.key = new byte[keyLen];
Array.Copy(key, keyOff, this.key, 0, keyLen);
}
public byte[] GetKey()
{
return (byte[]) key.Clone();
}
}
}

View File

@@ -0,0 +1,31 @@
using System;
namespace Org.BouncyCastle.Crypto.Parameters
{
/// <remarks>Parameters for mask derivation functions.</remarks>
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();
}
}
}

View File

@@ -0,0 +1,101 @@
using System;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Security;
namespace Org.BouncyCastle.Crypto.Parameters
{
/**
* Parameters for NaccacheStern public private key generation. For details on
* this cipher, please see
*
* http://www.gemplus.com/smart/rd/publications/pdf/NS98pkcs.pdf
*/
public class NaccacheSternKeyGenerationParameters : KeyGenerationParameters
{
// private BigInteger publicExponent;
private readonly int certainty;
private readonly int countSmallPrimes;
private bool debug;
/**
* Parameters for generating a NaccacheStern KeyPair.
*
* @param random
* The source of randomness
* @param strength
* The desired strength of the Key in Bits
* @param certainty
* the probability that the generated primes are not really prime
* as integer: 2^(-certainty) is then the probability
* @param countSmallPrimes
* How many small key factors are desired
*/
public NaccacheSternKeyGenerationParameters(
SecureRandom random,
int strength,
int certainty,
int countSmallPrimes)
: this(random, strength, certainty, countSmallPrimes, false)
{
}
/**
* Parameters for a NaccacheStern KeyPair.
*
* @param random
* The source of randomness
* @param strength
* The desired strength of the Key in Bits
* @param certainty
* the probability that the generated primes are not really prime
* as integer: 2^(-certainty) is then the probability
* @param cntSmallPrimes
* How many small key factors are desired
* @param debug
* Turn debugging on or off (reveals secret information, use with
* caution)
*/
public NaccacheSternKeyGenerationParameters(SecureRandom random,
int strength,
int certainty,
int countSmallPrimes,
bool debug)
: 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;
this.debug = debug;
}
/**
* @return Returns the certainty.
*/
public int Certainty
{
get { return certainty; }
}
/**
* @return Returns the countSmallPrimes.
*/
public int CountSmallPrimes
{
get { return countSmallPrimes; }
}
public bool IsDebug
{
get { return debug; }
}
}
}

View File

@@ -0,0 +1,44 @@
using System;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Crypto.Parameters
{
/**
* Public key parameters for NaccacheStern cipher. For details on this cipher,
* please see
*
* http://www.gemplus.com/smart/rd/publications/pdf/NS98pkcs.pdf
*/
public class NaccacheSternKeyParameters : AsymmetricKeyParameter
{
private readonly BigInteger g, n;
private readonly int lowerSigmaBound;
/**
* @param privateKey
*/
public NaccacheSternKeyParameters(bool privateKey, BigInteger g, BigInteger n, int lowerSigmaBound)
: base(privateKey)
{
this.g = g;
this.n = n;
this.lowerSigmaBound = lowerSigmaBound;
}
/**
* @return Returns the g.
*/
public BigInteger G { get { return g; } }
/**
* @return Returns the lowerSigmaBound.
*/
public int LowerSigmaBound { get { return lowerSigmaBound; } }
/**
* @return Returns the n.
*/
public BigInteger Modulus { get { return n; } }
}
}

View File

@@ -0,0 +1,56 @@
using System;
using System.Collections;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Crypto.Parameters
{
/**
* Private key parameters for NaccacheStern cipher. For details on this cipher,
* please see
*
* http://www.gemplus.com/smart/rd/publications/pdf/NS98pkcs.pdf
*/
public class NaccacheSternPrivateKeyParameters : NaccacheSternKeyParameters
{
private readonly BigInteger phiN;
private readonly ArrayList smallPrimes;
/**
* Constructs a NaccacheSternPrivateKey
*
* @param g
* the public enryption parameter g
* @param n
* the public modulus n = p*q
* @param lowerSigmaBound
* the public lower sigma bound up to which data can be encrypted
* @param smallPrimes
* the small primes, of which sigma is constructed in the right
* order
* @param phi_n
* the private modulus phi(n) = (p-1)(q-1)
*/
public NaccacheSternPrivateKeyParameters(
BigInteger g,
BigInteger n,
int lowerSigmaBound,
ArrayList smallPrimes,
BigInteger phiN)
: base(true, g, n, lowerSigmaBound)
{
this.smallPrimes = smallPrimes;
this.phiN = phiN;
}
public BigInteger PhiN
{
get { return phiN; }
}
public ArrayList SmallPrimes
{
get { return smallPrimes; }
}
}
}

View File

@@ -0,0 +1,44 @@
using System;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class ParametersWithIV
: ICipherParameters
{
private readonly ICipherParameters parameters;
private readonly byte[] iv;
public ParametersWithIV(
ICipherParameters parameters,
byte[] iv)
: this(parameters, iv, 0, iv.Length)
{
}
public ParametersWithIV(
ICipherParameters parameters,
byte[] iv,
int ivOff,
int ivLen)
{
if (parameters == null)
throw new ArgumentNullException("parameters");
if (iv == null)
throw new ArgumentNullException("iv");
this.parameters = parameters;
this.iv = new byte[ivLen];
Array.Copy(iv, ivOff, this.iv, 0, ivLen);
}
public byte[] GetIV()
{
return (byte[]) iv.Clone();
}
public ICipherParameters Parameters
{
get { return parameters; }
}
}
}

View File

@@ -0,0 +1,48 @@
using System;
using Org.BouncyCastle.Security;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class ParametersWithRandom
: ICipherParameters
{
private readonly ICipherParameters parameters;
private readonly SecureRandom random;
public ParametersWithRandom(
ICipherParameters parameters,
SecureRandom random)
{
if (parameters == null)
throw new ArgumentNullException("random");
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;
}
public SecureRandom Random
{
get { return random; }
}
public ICipherParameters Parameters
{
get { return parameters; }
}
}
}

View File

@@ -0,0 +1,24 @@
using System;
using Org.BouncyCastle.Crypto;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class ParametersWithSBox : ICipherParameters
{
private ICipherParameters parameters;
private byte[] sBox;
public ParametersWithSBox(
ICipherParameters parameters,
byte[] sBox)
{
this.parameters = parameters;
this.sBox = sBox;
}
public byte[] GetSBox() { return sBox; }
public ICipherParameters Parameters { get { return parameters; } }
}
}

View File

@@ -0,0 +1,39 @@
using System;
using Org.BouncyCastle.Crypto;
namespace Org.BouncyCastle.Crypto.Parameters
{
/// <summary> Cipher parameters with a fixed salt value associated with them.</summary>
public class ParametersWithSalt : ICipherParameters
{
private byte[] salt;
private ICipherParameters 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;
}
public ICipherParameters Parameters
{
get
{
return parameters;
}
}
}
}

View File

@@ -0,0 +1,47 @@
using System;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class RC2Parameters
: KeyParameter
{
private readonly int 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;
}
public int EffectiveKeyBits
{
get { return bits; }
}
}
}

View File

@@ -0,0 +1,27 @@
using System;
using Org.BouncyCastle.Crypto;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class RC5Parameters
: KeyParameter
{
private readonly int 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;
}
public int Rounds
{
get { return rounds; }
}
}
}

View File

@@ -0,0 +1,34 @@
using System;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class RsaBlindingParameters
: ICipherParameters
{
private readonly RsaKeyParameters publicKey;
private readonly BigInteger 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;
}
public RsaKeyParameters PublicKey
{
get { return publicKey; }
}
public BigInteger BlindingFactor
{
get { return blindingFactor; }
}
}
}

View File

@@ -0,0 +1,55 @@
using System;
using Org.BouncyCastle.Crypto;
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 RsaKeyGenerationParameters(
BigInteger publicExponent,
SecureRandom random,
int strength,
int certainty)
: base(random, strength)
{
this.publicExponent = publicExponent;
this.certainty = certainty;
}
public BigInteger PublicExponent
{
get { return publicExponent; }
}
public int Certainty
{
get { return certainty; }
}
public override bool Equals(
object obj)
{
RsaKeyGenerationParameters other = obj as RsaKeyGenerationParameters;
if (other == null)
{
return false;
}
return certainty == other.certainty
&& publicExponent.Equals(other.publicExponent);
}
public override int GetHashCode()
{
return certainty.GetHashCode() ^ publicExponent.GetHashCode();
}
}
}

View File

@@ -0,0 +1,54 @@
using System;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class RsaKeyParameters
: AsymmetricKeyParameter
{
private readonly BigInteger modulus;
private readonly BigInteger exponent;
public RsaKeyParameters(
bool isPrivate,
BigInteger modulus,
BigInteger exponent)
: base(isPrivate)
{
this.modulus = modulus;
this.exponent = exponent;
}
public BigInteger Modulus
{
get { return modulus; }
}
public BigInteger Exponent
{
get { return exponent; }
}
public override bool Equals(
object obj)
{
RsaKeyParameters kp = obj as RsaKeyParameters;
if (kp == null)
{
return false;
}
return kp.IsPrivate == this.IsPrivate
&& kp.Modulus.Equals(this.modulus)
&& kp.Exponent.Equals(this.exponent);
}
public override int GetHashCode()
{
return modulus.GetHashCode() ^ exponent.GetHashCode() ^ IsPrivate.GetHashCode();
}
}
}

View File

@@ -0,0 +1,89 @@
using System;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Crypto.Parameters
{
public class RsaPrivateCrtKeyParameters
: RsaKeyParameters
{
private readonly BigInteger e, p, q, dP, dQ, qInv;
public RsaPrivateCrtKeyParameters(
BigInteger modulus,
BigInteger publicExponent,
BigInteger privateExponent,
BigInteger p,
BigInteger q,
BigInteger dP,
BigInteger dQ,
BigInteger qInv)
: base(true, modulus, privateExponent)
{
this.e = publicExponent;
this.p = p;
this.q = q;
this.dP = dP;
this.dQ = dQ;
this.qInv = qInv;
}
public BigInteger PublicExponent
{
get { return e; }
}
public BigInteger P
{
get { return p; }
}
public BigInteger Q
{
get { return q; }
}
public BigInteger DP
{
get { return dP; }
}
public BigInteger DQ
{
get { return dQ; }
}
public BigInteger QInv
{
get { return qInv; }
}
public override bool Equals(
object obj)
{
if (obj == this)
return true;
RsaPrivateCrtKeyParameters kp = obj as RsaPrivateCrtKeyParameters;
if (kp == null)
return false;
return kp.DP.Equals(dP)
&& kp.DQ.Equals(dQ)
&& kp.Exponent.Equals(this.Exponent)
&& kp.Modulus.Equals(this.Modulus)
&& kp.P.Equals(p)
&& kp.Q.Equals(q)
&& kp.PublicExponent.Equals(e)
&& kp.QInv.Equals(qInv);
}
public override int GetHashCode()
{
return DP.GetHashCode() ^ DQ.GetHashCode() ^ Exponent.GetHashCode() ^ Modulus.GetHashCode()
^ P.GetHashCode() ^ Q.GetHashCode() ^ PublicExponent.GetHashCode() ^ QInv.GetHashCode();
}
}
}