diff --git a/src/Renci.SshNet/Security/BouncyCastle/asn1/x9/X9Curve.cs b/src/Renci.SshNet/Security/BouncyCastle/asn1/x9/X9Curve.cs deleted file mode 100644 index 1faf777a8..000000000 --- a/src/Renci.SshNet/Security/BouncyCastle/asn1/x9/X9Curve.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; - -using Renci.SshNet.Security.Org.BouncyCastle.Math.EC; -using Renci.SshNet.Security.Org.BouncyCastle.Utilities; - -namespace Renci.SshNet.Security.Org.BouncyCastle.Asn1.X9 -{ - internal class X9Curve - { - private readonly ECCurve curve; - private readonly byte[] seed; - - public X9Curve( - ECCurve curve) - : this(curve, null) - { - } - - public X9Curve( - ECCurve curve, - byte[] seed) - { - if (curve == null) - throw new ArgumentNullException("curve"); - - this.curve = curve; - this.seed = Arrays.Clone(seed); - } - - public ECCurve Curve - { - get { return curve; } - } - - public byte[] GetSeed() - { - return Arrays.Clone(seed); - } - } -} \ No newline at end of file diff --git a/src/Renci.SshNet/Security/BouncyCastle/asn1/x9/X9ECParameters.cs b/src/Renci.SshNet/Security/BouncyCastle/asn1/x9/X9ECParameters.cs index f8234b963..e727a1bf5 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/asn1/x9/X9ECParameters.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/asn1/x9/X9ECParameters.cs @@ -9,50 +9,6 @@ internal class X9ECParameters { private byte[] seed; - public static X9ECParameters GetInstance(Object obj) - { - if (obj is X9ECParameters) - return (X9ECParameters)obj; - - return null; - } - - public X9ECParameters( - ECCurve curve, - ECPoint g, - BigInteger n) - : this(curve, g, n, null, null) - { - } - - public X9ECParameters( - ECCurve curve, - X9ECPoint g, - BigInteger n, - BigInteger h) - : this(curve, g, n, h, null) - { - } - - public X9ECParameters( - ECCurve curve, - ECPoint g, - BigInteger n, - BigInteger h) - : this(curve, g, n, h, null) - { - } - - public X9ECParameters( - ECCurve curve, - ECPoint g, - BigInteger n, - BigInteger h, - byte[] seed) - : this(curve, new X9ECPoint(g), n, h, seed) - { - } - public X9ECParameters( ECCurve curve, X9ECPoint g, @@ -83,11 +39,6 @@ public byte[] GetSeed() return seed; } - public X9Curve CurveEntry - { - get { return new X9Curve(Curve, seed); } - } - public X9ECPoint BaseEntry { get; private set; } } } diff --git a/src/Renci.SshNet/Security/BouncyCastle/asn1/x9/X9ECPoint.cs b/src/Renci.SshNet/Security/BouncyCastle/asn1/x9/X9ECPoint.cs index f4db71063..6808653da 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/asn1/x9/X9ECPoint.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/asn1/x9/X9ECPoint.cs @@ -10,28 +10,12 @@ internal class X9ECPoint private ECCurve c; private ECPoint p; - public X9ECPoint(ECPoint p) - : this(p, false) - { - } - - public X9ECPoint(ECPoint p, bool compressed) - { - this.p = p.Normalize(); - this.encoding = p.GetEncoded(compressed); - } - public X9ECPoint(ECCurve c, byte[] encoding) { this.c = c; this.encoding = Arrays.Clone(encoding); } - public byte[] GetPointEncoding() - { - return Arrays.Clone(encoding); - } - public ECPoint Point { get @@ -44,14 +28,5 @@ public ECPoint Point return p; } } - - public bool IsPointCompressed - { - get - { - byte[] octets = encoding; - return octets != null && octets.Length > 0 && (octets[0] == 2 || octets[0] == 3); - } - } } -} \ No newline at end of file +} diff --git a/src/Renci.SshNet/Security/BouncyCastle/crypto/digests/GeneralDigest.cs b/src/Renci.SshNet/Security/BouncyCastle/crypto/digests/GeneralDigest.cs index 718f34ae8..cb16a1d58 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/crypto/digests/GeneralDigest.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/crypto/digests/GeneralDigest.cs @@ -5,7 +5,7 @@ namespace Renci.SshNet.Security.Org.BouncyCastle.Crypto.Digests { internal abstract class GeneralDigest - : IDigest, IMemoable + : IDigest { private const int BYTE_LENGTH = 64; @@ -123,7 +123,5 @@ public int GetByteLength() public abstract string AlgorithmName { get; } public abstract int GetDigestSize(); public abstract int DoFinal(byte[] output, int outOff); - public abstract IMemoable Copy(); - public abstract void Reset(IMemoable t); } -} \ No newline at end of file +} diff --git a/src/Renci.SshNet/Security/BouncyCastle/crypto/digests/Sha256Digest.cs b/src/Renci.SshNet/Security/BouncyCastle/crypto/digests/Sha256Digest.cs index da700fcf2..4cd336e67 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/crypto/digests/Sha256Digest.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/crypto/digests/Sha256Digest.cs @@ -263,18 +263,5 @@ private static uint Theta1( 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 }; - - public override IMemoable Copy() - { - return new Sha256Digest(this); - } - - public override void Reset(IMemoable other) - { - Sha256Digest d = (Sha256Digest)other; - - CopyIn(d); - } - } -} \ No newline at end of file +} diff --git a/src/Renci.SshNet/Security/BouncyCastle/crypto/generators/ECKeyPairGenerator.cs b/src/Renci.SshNet/Security/BouncyCastle/crypto/generators/ECKeyPairGenerator.cs index 446c279cd..691df633c 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/crypto/generators/ECKeyPairGenerator.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/crypto/generators/ECKeyPairGenerator.cs @@ -78,14 +78,5 @@ protected virtual ECMultiplier CreateBasePointMultiplier() { return new FixedPointCombMultiplier(); } - - internal static ECPublicKeyParameters GetCorrespondingPublicKey( - ECPrivateKeyParameters privKey) - { - ECDomainParameters ec = privKey.Parameters; - ECPoint q = new FixedPointCombMultiplier().Multiply(ec.G, privKey.D); - - return new ECPublicKeyParameters(privKey.AlgorithmName, q, ec); - } } -} \ No newline at end of file +} diff --git a/src/Renci.SshNet/Security/BouncyCastle/crypto/parameters/ECDomainParameters.cs b/src/Renci.SshNet/Security/BouncyCastle/crypto/parameters/ECDomainParameters.cs index 1d1b037f2..44dc05dd9 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/crypto/parameters/ECDomainParameters.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/crypto/parameters/ECDomainParameters.cs @@ -57,21 +57,6 @@ public BigInteger H get { return h; } } - public BigInteger HInv - { - get - { - lock (this) - { - if (hInv == null) - { - hInv = h.ModInverse(n); - } - return hInv; - } - } - } - internal static ECPoint Validate(ECCurve c, ECPoint q) { if (q == null) @@ -88,4 +73,4 @@ internal static ECPoint Validate(ECCurve c, ECPoint q) return q; } } -} \ No newline at end of file +} diff --git a/src/Renci.SshNet/Security/BouncyCastle/crypto/parameters/ECKeyParameters.cs b/src/Renci.SshNet/Security/BouncyCastle/crypto/parameters/ECKeyParameters.cs index 50426c54e..ac13e3a03 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/crypto/parameters/ECKeyParameters.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/crypto/parameters/ECKeyParameters.cs @@ -63,12 +63,6 @@ public override int GetHashCode() return parameters.GetHashCode() ^ base.GetHashCode(); } - internal ECKeyGenerationParameters CreateKeyGenerationParameters( - SecureRandom random) - { - return new ECKeyGenerationParameters(parameters, random); - } - internal static string VerifyAlgorithmName(string algorithm) { if (Array.IndexOf(algorithms, algorithm, 0, algorithms.Length) < 0) @@ -76,4 +70,4 @@ internal static string VerifyAlgorithmName(string algorithm) return algorithm.ToUpper(); } } -} \ No newline at end of file +} diff --git a/src/Renci.SshNet/Security/BouncyCastle/crypto/parameters/ECPrivateKeyParameters.cs b/src/Renci.SshNet/Security/BouncyCastle/crypto/parameters/ECPrivateKeyParameters.cs index a11233c8d..e06f93193 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/crypto/parameters/ECPrivateKeyParameters.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/crypto/parameters/ECPrivateKeyParameters.cs @@ -9,13 +9,6 @@ internal class ECPrivateKeyParameters { private readonly BigInteger d; - public ECPrivateKeyParameters( - BigInteger d, - ECDomainParameters parameters) - : this("EC", d, parameters) - { - } - public ECPrivateKeyParameters( string algorithm, BigInteger d, @@ -58,4 +51,4 @@ public override int GetHashCode() return d.GetHashCode() ^ base.GetHashCode(); } } -} \ No newline at end of file +} diff --git a/src/Renci.SshNet/Security/BouncyCastle/crypto/parameters/ECPublicKeyParameters.cs b/src/Renci.SshNet/Security/BouncyCastle/crypto/parameters/ECPublicKeyParameters.cs index 6b1f3ec5c..4088549b9 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/crypto/parameters/ECPublicKeyParameters.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/crypto/parameters/ECPublicKeyParameters.cs @@ -9,13 +9,6 @@ internal class ECPublicKeyParameters { private readonly ECPoint q; - public ECPublicKeyParameters( - ECPoint q, - ECDomainParameters parameters) - : this("EC", q, parameters) - { - } - public ECPublicKeyParameters( string algorithm, ECPoint q, @@ -57,4 +50,4 @@ public override int GetHashCode() return q.GetHashCode() ^ base.GetHashCode(); } } -} \ No newline at end of file +} diff --git a/src/Renci.SshNet/Security/BouncyCastle/crypto/util/Pack.cs b/src/Renci.SshNet/Security/BouncyCastle/crypto/util/Pack.cs index 594959eb2..345698402 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/crypto/util/Pack.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/crypto/util/Pack.cs @@ -8,47 +8,6 @@ private Pack() { } - internal static void UInt16_To_BE(ushort n, byte[] bs) - { - bs[0] = (byte)(n >> 8); - bs[1] = (byte)(n); - } - - internal static void UInt16_To_BE(ushort n, byte[] bs, int off) - { - bs[off] = (byte)(n >> 8); - bs[off + 1] = (byte)(n); - } - - internal static ushort BE_To_UInt16(byte[] bs) - { - uint n = (uint)bs[0] << 8 - | (uint)bs[1]; - return (ushort)n; - } - - internal static ushort BE_To_UInt16(byte[] bs, int off) - { - uint n = (uint)bs[off] << 8 - | (uint)bs[off + 1]; - return (ushort)n; - } - - internal static byte[] UInt32_To_BE(uint n) - { - byte[] bs = new byte[4]; - UInt32_To_BE(n, bs, 0); - return bs; - } - - internal static void UInt32_To_BE(uint n, byte[] bs) - { - bs[0] = (byte)(n >> 24); - bs[1] = (byte)(n >> 16); - bs[2] = (byte)(n >> 8); - bs[3] = (byte)(n); - } - internal static void UInt32_To_BE(uint n, byte[] bs, int off) { bs[off] = (byte)(n >> 24); @@ -57,30 +16,6 @@ internal static void UInt32_To_BE(uint n, byte[] bs, int off) bs[off + 3] = (byte)(n); } - internal static byte[] UInt32_To_BE(uint[] ns) - { - byte[] bs = new byte[4 * ns.Length]; - UInt32_To_BE(ns, bs, 0); - return bs; - } - - internal static void UInt32_To_BE(uint[] ns, byte[] bs, int off) - { - for (int i = 0; i < ns.Length; ++i) - { - UInt32_To_BE(ns[i], bs, off); - off += 4; - } - } - - internal static uint BE_To_UInt32(byte[] bs) - { - return (uint)bs[0] << 24 - | (uint)bs[1] << 16 - | (uint)bs[2] << 8 - | (uint)bs[3]; - } - internal static uint BE_To_UInt32(byte[] bs, int off) { return (uint)bs[off] << 24 @@ -98,97 +33,6 @@ internal static void BE_To_UInt32(byte[] bs, int off, uint[] ns) } } - internal static byte[] UInt64_To_BE(ulong n) - { - byte[] bs = new byte[8]; - UInt64_To_BE(n, bs, 0); - return bs; - } - - internal static void UInt64_To_BE(ulong n, byte[] bs) - { - UInt32_To_BE((uint)(n >> 32), bs); - UInt32_To_BE((uint)(n), bs, 4); - } - - internal static void UInt64_To_BE(ulong n, byte[] bs, int off) - { - UInt32_To_BE((uint)(n >> 32), bs, off); - UInt32_To_BE((uint)(n), bs, off + 4); - } - - internal static byte[] UInt64_To_BE(ulong[] ns) - { - byte[] bs = new byte[8 * ns.Length]; - UInt64_To_BE(ns, bs, 0); - return bs; - } - - internal static void UInt64_To_BE(ulong[] ns, byte[] bs, int off) - { - for (int i = 0; i < ns.Length; ++i) - { - UInt64_To_BE(ns[i], bs, off); - off += 8; - } - } - - internal static ulong BE_To_UInt64(byte[] bs) - { - uint hi = BE_To_UInt32(bs); - uint lo = BE_To_UInt32(bs, 4); - return ((ulong)hi << 32) | (ulong)lo; - } - - internal static ulong BE_To_UInt64(byte[] bs, int off) - { - uint hi = BE_To_UInt32(bs, off); - uint lo = BE_To_UInt32(bs, off + 4); - return ((ulong)hi << 32) | (ulong)lo; - } - - internal static void BE_To_UInt64(byte[] bs, int off, ulong[] ns) - { - for (int i = 0; i < ns.Length; ++i) - { - ns[i] = BE_To_UInt64(bs, off); - off += 8; - } - } - - internal static void UInt16_To_LE(ushort n, byte[] bs) - { - bs[0] = (byte)(n); - bs[1] = (byte)(n >> 8); - } - - internal static void UInt16_To_LE(ushort n, byte[] bs, int off) - { - bs[off] = (byte)(n); - bs[off + 1] = (byte)(n >> 8); - } - - internal static ushort LE_To_UInt16(byte[] bs) - { - uint n = (uint)bs[0] - | (uint)bs[1] << 8; - return (ushort)n; - } - - internal static ushort LE_To_UInt16(byte[] bs, int off) - { - uint n = (uint)bs[off] - | (uint)bs[off + 1] << 8; - return (ushort)n; - } - - internal static byte[] UInt32_To_LE(uint n) - { - byte[] bs = new byte[4]; - UInt32_To_LE(n, bs, 0); - return bs; - } - internal static void UInt32_To_LE(uint n, byte[] bs) { bs[0] = (byte)(n); @@ -205,141 +49,10 @@ internal static void UInt32_To_LE(uint n, byte[] bs, int off) bs[off + 3] = (byte)(n >> 24); } - internal static byte[] UInt32_To_LE(uint[] ns) - { - byte[] bs = new byte[4 * ns.Length]; - UInt32_To_LE(ns, bs, 0); - return bs; - } - - internal static void UInt32_To_LE(uint[] ns, byte[] bs, int off) - { - for (int i = 0; i < ns.Length; ++i) - { - UInt32_To_LE(ns[i], bs, off); - off += 4; - } - } - - internal static uint LE_To_UInt32(byte[] bs) - { - return (uint)bs[0] - | (uint)bs[1] << 8 - | (uint)bs[2] << 16 - | (uint)bs[3] << 24; - } - - internal static uint LE_To_UInt32(byte[] bs, int off) - { - return (uint)bs[off] - | (uint)bs[off + 1] << 8 - | (uint)bs[off + 2] << 16 - | (uint)bs[off + 3] << 24; - } - - internal static void LE_To_UInt32(byte[] bs, int off, uint[] ns) - { - for (int i = 0; i < ns.Length; ++i) - { - ns[i] = LE_To_UInt32(bs, off); - off += 4; - } - } - - internal static void LE_To_UInt32(byte[] bs, int bOff, uint[] ns, int nOff, int count) - { - for (int i = 0; i < count; ++i) - { - ns[nOff + i] = LE_To_UInt32(bs, bOff); - bOff += 4; - } - } - - internal static uint[] LE_To_UInt32(byte[] bs, int off, int count) - { - uint[] ns = new uint[count]; - for (int i = 0; i < ns.Length; ++i) - { - ns[i] = LE_To_UInt32(bs, off); - off += 4; - } - return ns; - } - - internal static byte[] UInt64_To_LE(ulong n) - { - byte[] bs = new byte[8]; - UInt64_To_LE(n, bs, 0); - return bs; - } - internal static void UInt64_To_LE(ulong n, byte[] bs) { UInt32_To_LE((uint)(n), bs); UInt32_To_LE((uint)(n >> 32), bs, 4); } - - internal static void UInt64_To_LE(ulong n, byte[] bs, int off) - { - UInt32_To_LE((uint)(n), bs, off); - UInt32_To_LE((uint)(n >> 32), bs, off + 4); - } - - internal static byte[] UInt64_To_LE(ulong[] ns) - { - byte[] bs = new byte[8 * ns.Length]; - UInt64_To_LE(ns, bs, 0); - return bs; - } - - internal static void UInt64_To_LE(ulong[] ns, byte[] bs, int off) - { - for (int i = 0; i < ns.Length; ++i) - { - UInt64_To_LE(ns[i], bs, off); - off += 8; - } - } - - internal static void UInt64_To_LE(ulong[] ns, int nsOff, int nsLen, byte[] bs, int bsOff) - { - for (int i = 0; i < nsLen; ++i) - { - UInt64_To_LE(ns[nsOff + i], bs, bsOff); - bsOff += 8; - } - } - - internal static ulong LE_To_UInt64(byte[] bs) - { - uint lo = LE_To_UInt32(bs); - uint hi = LE_To_UInt32(bs, 4); - return ((ulong)hi << 32) | (ulong)lo; - } - - internal static ulong LE_To_UInt64(byte[] bs, int off) - { - uint lo = LE_To_UInt32(bs, off); - uint hi = LE_To_UInt32(bs, off + 4); - return ((ulong)hi << 32) | (ulong)lo; - } - - internal static void LE_To_UInt64(byte[] bs, int off, ulong[] ns) - { - for (int i = 0; i < ns.Length; ++i) - { - ns[i] = LE_To_UInt64(bs, off); - off += 8; - } - } - - internal static void LE_To_UInt64(byte[] bs, int bsOff, ulong[] ns, int nsOff, int nsLen) - { - for (int i = 0; i < nsLen; ++i) - { - ns[nsOff + i] = LE_To_UInt64(bs, bsOff); - bsOff += 8; - } - } } -} \ No newline at end of file +} diff --git a/src/Renci.SshNet/Security/BouncyCastle/math/BigInteger.cs b/src/Renci.SshNet/Security/BouncyCastle/math/BigInteger.cs index 635a25409..dee77545d 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/math/BigInteger.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/math/BigInteger.cs @@ -15,115 +15,6 @@ namespace Renci.SshNet.Security.Org.BouncyCastle.Math #endif internal class BigInteger { - // The first few odd primes - /* - 3 5 7 11 13 17 19 23 29 - 31 37 41 43 47 53 59 61 67 71 - 73 79 83 89 97 101 103 107 109 113 - 127 131 137 139 149 151 157 163 167 173 - 179 181 191 193 197 199 211 223 227 229 - 233 239 241 251 257 263 269 271 277 281 - 283 293 307 311 313 317 331 337 347 349 - 353 359 367 373 379 383 389 397 401 409 - 419 421 431 433 439 443 449 457 461 463 - 467 479 487 491 499 503 509 521 523 541 - 547 557 563 569 571 577 587 593 599 601 - 607 613 617 619 631 641 643 647 653 659 - 661 673 677 683 691 701 709 719 727 733 - 739 743 751 757 761 769 773 787 797 809 - 811 821 823 827 829 839 853 857 859 863 - 877 881 883 887 907 911 919 929 937 941 - 947 953 967 971 977 983 991 997 1009 - 1013 1019 1021 1031 1033 1039 1049 1051 - 1061 1063 1069 1087 1091 1093 1097 1103 - 1109 1117 1123 1129 1151 1153 1163 1171 - 1181 1187 1193 1201 1213 1217 1223 1229 - 1231 1237 1249 1259 1277 1279 1283 1289 - */ - - // Each list has a product < 2^31 - internal static readonly int[][] primeLists = new int[][] - { - new int[]{ 3, 5, 7, 11, 13, 17, 19, 23 }, - new int[]{ 29, 31, 37, 41, 43 }, - new int[]{ 47, 53, 59, 61, 67 }, - new int[]{ 71, 73, 79, 83 }, - new int[]{ 89, 97, 101, 103 }, - - new int[]{ 107, 109, 113, 127 }, - new int[]{ 131, 137, 139, 149 }, - new int[]{ 151, 157, 163, 167 }, - new int[]{ 173, 179, 181, 191 }, - new int[]{ 193, 197, 199, 211 }, - - new int[]{ 223, 227, 229 }, - new int[]{ 233, 239, 241 }, - new int[]{ 251, 257, 263 }, - new int[]{ 269, 271, 277 }, - new int[]{ 281, 283, 293 }, - - new int[]{ 307, 311, 313 }, - new int[]{ 317, 331, 337 }, - new int[]{ 347, 349, 353 }, - new int[]{ 359, 367, 373 }, - new int[]{ 379, 383, 389 }, - - new int[]{ 397, 401, 409 }, - new int[]{ 419, 421, 431 }, - new int[]{ 433, 439, 443 }, - new int[]{ 449, 457, 461 }, - new int[]{ 463, 467, 479 }, - - new int[]{ 487, 491, 499 }, - new int[]{ 503, 509, 521 }, - new int[]{ 523, 541, 547 }, - new int[]{ 557, 563, 569 }, - new int[]{ 571, 577, 587 }, - - new int[]{ 593, 599, 601 }, - new int[]{ 607, 613, 617 }, - new int[]{ 619, 631, 641 }, - new int[]{ 643, 647, 653 }, - new int[]{ 659, 661, 673 }, - - new int[]{ 677, 683, 691 }, - new int[]{ 701, 709, 719 }, - new int[]{ 727, 733, 739 }, - new int[]{ 743, 751, 757 }, - new int[]{ 761, 769, 773 }, - - new int[]{ 787, 797, 809 }, - new int[]{ 811, 821, 823 }, - new int[]{ 827, 829, 839 }, - new int[]{ 853, 857, 859 }, - new int[]{ 863, 877, 881 }, - - new int[]{ 883, 887, 907 }, - new int[]{ 911, 919, 929 }, - new int[]{ 937, 941, 947 }, - new int[]{ 953, 967, 971 }, - new int[]{ 977, 983, 991 }, - - new int[]{ 997, 1009, 1013 }, - new int[]{ 1019, 1021, 1031 }, - new int[]{ 1033, 1039, 1049 }, - new int[]{ 1051, 1061, 1063 }, - new int[]{ 1069, 1087, 1091 }, - - new int[]{ 1093, 1097, 1103 }, - new int[]{ 1109, 1117, 1123 }, - new int[]{ 1129, 1151, 1153 }, - new int[]{ 1163, 1171, 1181 }, - new int[]{ 1187, 1193, 1201 }, - - new int[]{ 1213, 1217, 1223 }, - new int[]{ 1229, 1231, 1237 }, - new int[]{ 1249, 1259, 1277 }, - new int[]{ 1279, 1283, 1289 }, - }; - - internal static readonly int[] primeProducts; - private const long IMASK = 0xFFFFFFFFL; private const ulong UIMASK = 0xFFFFFFFFUL; @@ -137,26 +28,6 @@ 1231 1237 1249 1259 1277 1279 1283 1289 public static readonly BigInteger Three; public static readonly BigInteger Ten; - //private readonly static byte[] BitCountTable = - //{ - // 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, - // 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, - // 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, - // 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, - // 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, - // 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, - // 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, - // 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, - // 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, - // 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, - // 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, - // 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, - // 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, - // 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, - // 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, - // 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 - //}; - private readonly static byte[] BitLengthTable = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, @@ -177,10 +48,6 @@ 1231 1237 1249 1259 1277 1279 1283 1289 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 }; - // TODO Parse radix-2 64 bits at a time and radix-8 63 bits at a time - private const int chunk2 = 1, chunk8 = 1, chunk10 = 19, chunk16 = 16; - private static readonly BigInteger radix2, radix2E, radix8, radix8E, radix10, radix10E, radix16, radix16E; - private static readonly SecureRandom RandomSource = new SecureRandom(); /* @@ -209,31 +76,6 @@ static BigInteger() Two = SMALL_CONSTANTS[2]; Three = SMALL_CONSTANTS[3]; Ten = SMALL_CONSTANTS[10]; - - radix2 = ValueOf(2); - radix2E = radix2.Pow(chunk2); - - radix8 = ValueOf(8); - radix8E = radix8.Pow(chunk8); - - radix10 = ValueOf(10); - radix10E = radix10.Pow(chunk10); - - radix16 = ValueOf(16); - radix16E = radix16.Pow(chunk16); - - primeProducts = new int[primeLists.Length]; - - for (int i = 0; i < primeLists.Length; ++i) - { - int[] primeList = primeLists[i]; - int product = primeList[0]; - for (int j = 1; j < primeList.Length; ++j) - { - product *= primeList[j]; - } - primeProducts[i] = product; - } } private int[] magnitude; // array of ints with [0] being the most significant @@ -294,255 +136,6 @@ private BigInteger( } } - public BigInteger( - string value) - : this(value, 10) - { - } - - public BigInteger( - string str, - int radix) - { - if (str.Length == 0) - throw new FormatException("Zero length BigInteger"); - - NumberStyles style; - int chunk; - BigInteger r; - BigInteger rE; - - switch (radix) - { - case 2: - // Is there anyway to restrict to binary digits? - style = NumberStyles.Integer; - chunk = chunk2; - r = radix2; - rE = radix2E; - break; - case 8: - // Is there anyway to restrict to octal digits? - style = NumberStyles.Integer; - chunk = chunk8; - r = radix8; - rE = radix8E; - break; - case 10: - // This style seems to handle spaces and minus sign already (our processing redundant?) - style = NumberStyles.Integer; - chunk = chunk10; - r = radix10; - rE = radix10E; - break; - case 16: - // TODO Should this be HexNumber? - style = NumberStyles.AllowHexSpecifier; - chunk = chunk16; - r = radix16; - rE = radix16E; - break; - default: - throw new FormatException("Only bases 2, 8, 10, or 16 allowed"); - } - - - int index = 0; - sign = 1; - - if (str[0] == '-') - { - if (str.Length == 1) - throw new FormatException("Zero length BigInteger"); - - sign = -1; - index = 1; - } - - // strip leading zeros from the string str - while (index < str.Length && Int32.Parse(str[index].ToString(), style) == 0) - { - index++; - } - - if (index >= str.Length) - { - // zero value - we're done - sign = 0; - magnitude = ZeroMagnitude; - return; - } - - ////// - // could we work out the max number of ints required to store - // str.Length digits in the given base, then allocate that - // storage in one hit?, then Generate the magnitude in one hit too? - ////// - - BigInteger b = Zero; - - - int next = index + chunk; - - if (next <= str.Length) - { - do - { - string s = str.Substring(index, chunk); - ulong i = ulong.Parse(s, style); - BigInteger bi = CreateUValueOf(i); - - switch (radix) - { - case 2: - // TODO Need this because we are parsing in radix 10 above - if (i >= 2) - throw new FormatException("Bad character in radix 2 string: " + s); - - // TODO Parse 64 bits at a time - b = b.ShiftLeft(1); - break; - case 8: - // TODO Need this because we are parsing in radix 10 above - if (i >= 8) - throw new FormatException("Bad character in radix 8 string: " + s); - - // TODO Parse 63 bits at a time - b = b.ShiftLeft(3); - break; - case 16: - b = b.ShiftLeft(64); - break; - default: - b = b.Multiply(rE); - break; - } - - b = b.Add(bi); - - index = next; - next += chunk; - } - while (next <= str.Length); - } - - if (index < str.Length) - { - string s = str.Substring(index); - ulong i = ulong.Parse(s, style); - BigInteger bi = CreateUValueOf(i); - - if (b.sign > 0) - { - if (radix == 2) - { - // NB: Can't reach here since we are parsing one char at a time - Debug.Assert(false); - - // TODO Parse all bits at once -// b = b.ShiftLeft(s.Length); - } - else if (radix == 8) - { - // NB: Can't reach here since we are parsing one char at a time - Debug.Assert(false); - - // TODO Parse all bits at once -// b = b.ShiftLeft(s.Length * 3); - } - else if (radix == 16) - { - b = b.ShiftLeft(s.Length << 2); - } - else - { - b = b.Multiply(r.Pow(s.Length)); - } - - b = b.Add(bi); - } - else - { - b = bi; - } - } - - // Note: This is the previous (slower) algorithm -// while (index < value.Length) -// { -// char c = value[index]; -// string s = c.ToString(); -// int i = Int32.Parse(s, style); -// -// b = b.Multiply(r).Add(ValueOf(i)); -// index++; -// } - - magnitude = b.magnitude; - } - - public BigInteger( - byte[] bytes) - : this(bytes, 0, bytes.Length) - { - } - - public BigInteger( - byte[] bytes, - int offset, - int length) - { - if (length == 0) - throw new FormatException("Zero length BigInteger"); - - // TODO Move this processing into MakeMagnitude (provide sign argument) - if ((sbyte)bytes[offset] < 0) - { - this.sign = -1; - - int end = offset + length; - - int iBval; - // strip leading sign bytes - for (iBval = offset; iBval < end && ((sbyte)bytes[iBval] == -1); iBval++) - { - } - - if (iBval >= end) - { - this.magnitude = One.magnitude; - } - else - { - int numBytes = end - iBval; - byte[] inverse = new byte[numBytes]; - - int index = 0; - while (index < numBytes) - { - inverse[index++] = (byte)~bytes[iBval++]; - } - - Debug.Assert(iBval == end); - - while (inverse[--index] == byte.MaxValue) - { - inverse[index] = byte.MinValue; - } - - inverse[index]++; - - this.magnitude = MakeMagnitude(inverse, 0, inverse.Length); - } - } - else - { - // strip leading zero bytes and return magnitude bytes - this.magnitude = MakeMagnitude(bytes, offset, length); - this.sign = this.magnitude.Length > 0 ? 1 : 0; - } - } - private static int[] MakeMagnitude( byte[] bytes, int offset, @@ -658,65 +251,6 @@ public BigInteger( this.sign = this.magnitude.Length < 1 ? 0 : 1; } - public BigInteger( - int bitLength, - int certainty, - Random random) - { - if (bitLength < 2) - throw new ArithmeticException("bitLength < 2"); - - this.sign = 1; - this.nBitLength = bitLength; - - if (bitLength == 2) - { - this.magnitude = random.Next(2) == 0 - ? Two.magnitude - : Three.magnitude; - return; - } - - int nBytes = GetByteLength(bitLength); - byte[] b = new byte[nBytes]; - - int xBits = BitsPerByte * nBytes - bitLength; - byte mask = (byte)(255U >> xBits); - byte lead = (byte)(1 << (7 - xBits)); - - for (;;) - { - random.NextBytes(b); - - // strip off any excess bits in the MSB - b[0] &= mask; - - // ensure the leading bit is 1 (to meet the strength requirement) - b[0] |= lead; - - // ensure the trailing bit is 1 (i.e. must be odd) - b[nBytes - 1] |= 1; - - this.magnitude = MakeMagnitude(b, 0, b.Length); - this.nBits = -1; - this.mQuote = 0; - - if (certainty < 1) - break; - - if (CheckProbablePrime(certainty, random, true)) - break; - - for (int j = 1; j < (magnitude.Length - 1); ++j) - { - this.magnitude[j] ^= random.Next(); - - if (CheckProbablePrime(certainty, random, true)) - return; - } - } - } - public BigInteger Abs() { return sign >= 0 ? this : Negate(); @@ -808,69 +342,6 @@ private BigInteger AddToMagnitude( return new BigInteger(this.sign, bigCopy, possibleOverflow); } - public BigInteger And( - BigInteger value) - { - if (this.sign == 0 || value.sign == 0) - { - return Zero; - } - - int[] aMag = this.sign > 0 - ? this.magnitude - : Add(One).magnitude; - - int[] bMag = value.sign > 0 - ? value.magnitude - : value.Add(One).magnitude; - - bool resultNeg = sign < 0 && value.sign < 0; - int resultLength = System.Math.Max(aMag.Length, bMag.Length); - int[] resultMag = new int[resultLength]; - - int aStart = resultMag.Length - aMag.Length; - int bStart = resultMag.Length - bMag.Length; - - for (int i = 0; i < resultMag.Length; ++i) - { - int aWord = i >= aStart ? aMag[i - aStart] : 0; - int bWord = i >= bStart ? bMag[i - bStart] : 0; - - if (this.sign < 0) - { - aWord = ~aWord; - } - - if (value.sign < 0) - { - bWord = ~bWord; - } - - resultMag[i] = aWord & bWord; - - if (resultNeg) - { - resultMag[i] = ~resultMag[i]; - } - } - - BigInteger result = new BigInteger(1, resultMag, true); - - // TODO Optimise this case - if (resultNeg) - { - result = result.Not(); - } - - return result; - } - - public BigInteger AndNot( - BigInteger val) - { - return And(val.Not()); - } - public int BitCount { get @@ -984,12 +455,6 @@ private bool QuickPow2Check() return sign > 0 && nBits == 1; } - public int CompareTo( - object obj) - { - return CompareTo((BigInteger)obj); - } - /** * unsigned comparison on two arrays - note the arrays may * start with leading zeros. @@ -1265,29 +730,6 @@ private bool IsEqualMagnitude(BigInteger x) return true; } - public BigInteger Gcd( - BigInteger value) - { - if (value.sign == 0) - return Abs(); - - if (sign == 0) - return value.Abs(); - - BigInteger r; - BigInteger u = this; - BigInteger v = value; - - while (v.sign != 0) - { - r = u.Mod(v); - u = v; - v = r; - } - - return u; - } - public override int GetHashCode() { int hc = magnitude.Length; @@ -1331,243 +773,6 @@ public int IntValue } } - /** - * return whether or not a BigInteger is probably prime with a - * probability of 1 - (1/2)**certainty. - *

From Knuth Vol 2, pg 395.

- */ - public bool IsProbablePrime(int certainty) - { - return IsProbablePrime(certainty, false); - } - - internal bool IsProbablePrime(int certainty, bool randomlySelected) - { - if (certainty <= 0) - return true; - - BigInteger n = Abs(); - - if (!n.TestBit(0)) - return n.Equals(Two); - - if (n.Equals(One)) - return false; - - return n.CheckProbablePrime(certainty, RandomSource, randomlySelected); - } - - private bool CheckProbablePrime(int certainty, Random random, bool randomlySelected) - { - Debug.Assert(certainty > 0); - Debug.Assert(CompareTo(Two) > 0); - Debug.Assert(TestBit(0)); - - - // Try to reduce the penalty for really small numbers - int numLists = System.Math.Min(BitLength - 1, primeLists.Length); - - for (int i = 0; i < numLists; ++i) - { - int test = Remainder(primeProducts[i]); - - int[] primeList = primeLists[i]; - for (int j = 0; j < primeList.Length; ++j) - { - int prime = primeList[j]; - int qRem = test % prime; - if (qRem == 0) - { - // We may find small numbers in the list - return BitLength < 16 && IntValue == prime; - } - } - } - - - // TODO Special case for < 10^16 (RabinMiller fixed list) -// if (BitLength < 30) -// { -// RabinMiller against 2, 3, 5, 7, 11, 13, 23 is sufficient -// } - - - // TODO Is it worth trying to create a hybrid of these two? - return RabinMillerTest(certainty, random, randomlySelected); -// return SolovayStrassenTest(certainty, random); - -// bool rbTest = RabinMillerTest(certainty, random); -// bool ssTest = SolovayStrassenTest(certainty, random); -// -// Debug.Assert(rbTest == ssTest); -// -// return rbTest; - } - - public bool RabinMillerTest(int certainty, Random random) - { - return RabinMillerTest(certainty, random, false); - } - - internal bool RabinMillerTest(int certainty, Random random, bool randomlySelected) - { - int bits = BitLength; - - Debug.Assert(certainty > 0); - Debug.Assert(bits > 2); - Debug.Assert(TestBit(0)); - - int iterations = ((certainty - 1) / 2) + 1; - if (randomlySelected) - { - int itersFor100Cert = bits >= 1024 ? 4 - : bits >= 512 ? 8 - : bits >= 256 ? 16 - : 50; - - if (certainty < 100) - { - iterations = System.Math.Min(itersFor100Cert, iterations); - } - else - { - iterations -= 50; - iterations += itersFor100Cert; - } - } - - // let n = 1 + d . 2^s - BigInteger n = this; - int s = n.GetLowestSetBitMaskFirst(-1 << 1); - Debug.Assert(s >= 1); - BigInteger r = n.ShiftRight(s); - - // NOTE: Avoid conversion to/from Montgomery form and check for R/-R as result instead - - BigInteger montRadix = One.ShiftLeft(32 * n.magnitude.Length).Remainder(n); - BigInteger minusMontRadix = n.Subtract(montRadix); - - do - { - BigInteger a; - do - { - a = new BigInteger(n.BitLength, random); - } - while (a.sign == 0 || a.CompareTo(n) >= 0 - || a.IsEqualMagnitude(montRadix) || a.IsEqualMagnitude(minusMontRadix)); - - BigInteger y = ModPowMonty(a, r, n, false); - - if (!y.Equals(montRadix)) - { - int j = 0; - while (!y.Equals(minusMontRadix)) - { - if (++j == s) - return false; - - y = ModPowMonty(y, Two, n, false); - - if (y.Equals(montRadix)) - return false; - } - } - } - while (--iterations > 0); - - return true; - } - -// private bool SolovayStrassenTest( -// int certainty, -// Random random) -// { -// Debug.Assert(certainty > 0); -// Debug.Assert(CompareTo(Two) > 0); -// Debug.Assert(TestBit(0)); -// -// BigInteger n = this; -// BigInteger nMinusOne = n.Subtract(One); -// BigInteger e = nMinusOne.ShiftRight(1); -// -// do -// { -// BigInteger a; -// do -// { -// a = new BigInteger(nBitLength, random); -// } -// // NB: Spec says 0 < x < n, but 1 is trivial -// while (a.CompareTo(One) <= 0 || a.CompareTo(n) >= 0); -// -// -// // TODO Check this is redundant given the way Jacobi() works? -//// if (!a.Gcd(n).Equals(One)) -//// return false; -// -// int x = Jacobi(a, n); -// -// if (x == 0) -// return false; -// -// BigInteger check = a.ModPow(e, n); -// -// if (x == 1 && !check.Equals(One)) -// return false; -// -// if (x == -1 && !check.Equals(nMinusOne)) -// return false; -// -// --certainty; -// } -// while (certainty > 0); -// -// return true; -// } -// -// private static int Jacobi( -// BigInteger a, -// BigInteger b) -// { -// Debug.Assert(a.sign >= 0); -// Debug.Assert(b.sign > 0); -// Debug.Assert(b.TestBit(0)); -// Debug.Assert(a.CompareTo(b) < 0); -// -// int totalS = 1; -// for (;;) -// { -// if (a.sign == 0) -// return 0; -// -// if (a.Equals(One)) -// break; -// -// int e = a.GetLowestSetBit(); -// -// int bLsw = b.magnitude[b.magnitude.Length - 1]; -// if ((e & 1) != 0 && ((bLsw & 7) == 3 || (bLsw & 7) == 5)) -// totalS = -totalS; -// -// // TODO Confirm this is faster than later a1.Equals(One) test -// if (a.BitLength == e + 1) -// break; -// BigInteger a1 = a.ShiftRight(e); -//// if (a1.Equals(One)) -//// break; -// -// int a1Lsw = a1.magnitude[a1.magnitude.Length - 1]; -// if ((bLsw & 3) == 3 && (a1Lsw & 3) == 3) -// totalS = -totalS; -// -//// a = b.Mod(a1); -// a = b.Remainder(a1); -// b = a1; -// } -// return totalS; -// } - public long LongValue { get @@ -1587,18 +792,6 @@ public long LongValue } } - public BigInteger Max( - BigInteger value) - { - return CompareTo(value) > 0 ? this : value; - } - - public BigInteger Min( - BigInteger value) - { - return CompareTo(value) < 0 ? this : value; - } - public BigInteger Mod( BigInteger m) { @@ -1616,44 +809,6 @@ public BigInteger ModInverse( if (m.sign < 1) throw new ArithmeticException("Modulus must be positive"); - // TODO Too slow at the moment -// // "Fast Key Exchange with Elliptic Curve Systems" R.Schoeppel -// if (m.TestBit(0)) -// { -// //The Almost Inverse Algorithm -// int k = 0; -// BigInteger B = One, C = Zero, F = this, G = m, tmp; -// -// for (;;) -// { -// // While F is even, do F=F/u, C=C*u, k=k+1. -// int zeroes = F.GetLowestSetBit(); -// if (zeroes > 0) -// { -// F = F.ShiftRight(zeroes); -// C = C.ShiftLeft(zeroes); -// k += zeroes; -// } -// -// // If F = 1, then return B,k. -// if (F.Equals(One)) -// { -// BigInteger half = m.Add(One).ShiftRight(1); -// BigInteger halfK = half.ModPow(BigInteger.ValueOf(k), m); -// return B.Multiply(halfK).Mod(m); -// } -// -// if (F.CompareTo(G) < 0) -// { -// tmp = G; G = F; F = tmp; -// tmp = B; B = C; C = tmp; -// } -// -// F = F.Add(G); -// B = B.Add(C); -// } -// } - if (m.QuickPow2Check()) { return ModInversePow2(m); @@ -1787,12 +942,6 @@ private static BigInteger ExtEuclid(BigInteger a, BigInteger b, out BigInteger u return u3; } - private static void ZeroOut( - int[] x) - { - Array.Clear(x, 0, x.Length); - } - public BigInteger ModPow(BigInteger e, BigInteger m) { if (m.sign < 1) @@ -2506,78 +1655,11 @@ public BigInteger Negate() return new BigInteger(-sign, magnitude, false); } - public BigInteger NextProbablePrime() - { - if (sign < 0) - throw new ArithmeticException("Cannot be called on value < 0"); - - if (CompareTo(Two) < 0) - return Two; - - BigInteger n = Inc().SetBit(0); - - while (!n.CheckProbablePrime(100, RandomSource, false)) - { - n = n.Add(Two); - } - - return n; - } - public BigInteger Not() { return Inc().Negate(); } - public BigInteger Pow(int exp) - { - if (exp <= 0) - { - if (exp < 0) - throw new ArithmeticException("Negative exponent"); - - return One; - } - - if (sign == 0) - { - return this; - } - - if (QuickPow2Check()) - { - long powOf2 = (long)exp * (BitLength - 1); - if (powOf2 > Int32.MaxValue) - { - throw new ArithmeticException("Result too large"); - } - return One.ShiftLeft((int)powOf2); - } - - BigInteger y = One; - BigInteger z = this; - - for (;;) - { - if ((exp & 0x1) == 1) - { - y = y.Multiply(z); - } - exp >>= 1; - if (exp == 0) break; - z = z.Multiply(z); - } - - return y; - } - - public static BigInteger ProbablePrime( - int bitLength, - Random random) - { - return new BigInteger(bitLength, 100, random); - } - private int Remainder( int m) { @@ -2841,19 +1923,6 @@ private static int[] ShiftLeft( return newMag; } - private static int ShiftLeftOneInPlace(int[] x, int carry) - { - Debug.Assert(carry == 0 || carry == 1); - int pos = x.Length; - while (--pos >= 0) - { - uint val = (uint)x[pos]; - x[pos] = (int)(val << 1) | carry; - carry = (int)(val >> 31); - } - return carry; - } - public BigInteger ShiftLeft( int n) { @@ -3556,35 +2625,6 @@ public BigInteger SetBit( return Or(One.ShiftLeft(n)); } - public BigInteger ClearBit( - int n) - { - if (n < 0) - throw new ArithmeticException("Bit address less than zero"); - - if (!TestBit(n)) - return this; - - // TODO Handle negative values - if (sign > 0 && n < (BitLength - 1)) - return FlipExistingBit(n); - - return AndNot(One.ShiftLeft(n)); - } - - public BigInteger FlipBit( - int n) - { - if (n < 0) - throw new ArithmeticException("Bit address less than zero"); - - // TODO Handle negative values and zero - if (sign > 0 && n < (BitLength - 1)) - return FlipExistingBit(n); - - return Xor(One.ShiftLeft(n)); - } - private BigInteger FlipExistingBit( int n) { diff --git a/src/Renci.SshNet/Security/BouncyCastle/math/ec/ECAlgorithms.cs b/src/Renci.SshNet/Security/BouncyCastle/math/ec/ECAlgorithms.cs index 1347a9af2..3dea0d8a6 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/math/ec/ECAlgorithms.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/math/ec/ECAlgorithms.cs @@ -1,6 +1,5 @@ using System; -using Renci.SshNet.Security.Org.BouncyCastle.Math.EC.Endo; using Renci.SshNet.Security.Org.BouncyCastle.Math.EC.Multiplier; using Renci.SshNet.Security.Org.BouncyCastle.Math.Field; @@ -8,17 +7,6 @@ namespace Renci.SshNet.Security.Org.BouncyCastle.Math.EC { internal class ECAlgorithms { - public static bool IsF2mCurve(ECCurve c) - { - return IsF2mField(c.Field); - } - - public static bool IsF2mField(IFiniteField field) - { - return field.Dimension > 1 && field.Characteristic.Equals(BigInteger.Two) - && field is IPolynomialExtensionField; - } - public static bool IsFpCurve(ECCurve c) { return IsFpField(c.Field); @@ -29,91 +17,6 @@ public static bool IsFpField(IFiniteField field) return field.Dimension == 1; } - public static ECPoint SumOfMultiplies(ECPoint[] ps, BigInteger[] ks) - { - if (ps == null || ks == null || ps.Length != ks.Length || ps.Length < 1) - throw new ArgumentException("point and scalar arrays should be non-null, and of equal, non-zero, length"); - - int count = ps.Length; - switch (count) - { - case 1: - return ps[0].Multiply(ks[0]); - case 2: - return SumOfTwoMultiplies(ps[0], ks[0], ps[1], ks[1]); - default: - break; - } - - ECPoint p = ps[0]; - ECCurve c = p.Curve; - - ECPoint[] imported = new ECPoint[count]; - imported[0] = p; - for (int i = 1; i < count; ++i) - { - imported[i] = ImportPoint(c, ps[i]); - } - - GlvEndomorphism glvEndomorphism = c.GetEndomorphism() as GlvEndomorphism; - if (glvEndomorphism != null) - { - return ImplCheckResult(ImplSumOfMultipliesGlv(imported, ks, glvEndomorphism)); - } - - return ImplCheckResult(ImplSumOfMultiplies(imported, ks)); - } - - public static ECPoint SumOfTwoMultiplies(ECPoint P, BigInteger a, ECPoint Q, BigInteger b) - { - ECCurve cp = P.Curve; - Q = ImportPoint(cp, Q); - - // Point multiplication for Koblitz curves (using WTNAF) beats Shamir's trick - { - AbstractF2mCurve f2mCurve = cp as AbstractF2mCurve; - if (f2mCurve != null && f2mCurve.IsKoblitz) - { - return ImplCheckResult(P.Multiply(a).Add(Q.Multiply(b))); - } - } - - GlvEndomorphism glvEndomorphism = cp.GetEndomorphism() as GlvEndomorphism; - if (glvEndomorphism != null) - { - return ImplCheckResult( - ImplSumOfMultipliesGlv(new ECPoint[] { P, Q }, new BigInteger[] { a, b }, glvEndomorphism)); - } - - return ImplCheckResult(ImplShamirsTrickWNaf(P, a, Q, b)); - } - - /* - * "Shamir's Trick", originally due to E. G. Straus - * (Addition chains of vectors. American Mathematical Monthly, - * 71(7):806-808, Aug./Sept. 1964) - * - * Input: The points P, Q, scalar k = (km?, ... , k1, k0) - * and scalar l = (lm?, ... , l1, l0). - * Output: R = k * P + l * Q. - * 1: Z <- P + Q - * 2: R <- O - * 3: for i from m-1 down to 0 do - * 4: R <- R + R {point doubling} - * 5: if (ki = 1) and (li = 0) then R <- R + P end if - * 6: if (ki = 0) and (li = 1) then R <- R + Q end if - * 7: if (ki = 1) and (li = 1) then R <- R + Z end if - * 8: end for - * 9: return R - */ - public static ECPoint ShamirsTrick(ECPoint P, BigInteger k, ECPoint Q, BigInteger l) - { - ECCurve cp = P.Curve; - Q = ImportPoint(cp, Q); - - return ImplCheckResult(ImplShamirsTrickJsf(P, k, Q, l)); - } - public static ECPoint ImportPoint(ECCurve c, ECPoint p) { ECCurve cp = p.Curve; @@ -123,11 +26,6 @@ public static ECPoint ImportPoint(ECCurve c, ECPoint p) return c.ImportPoint(p); } - public static void MontgomeryTrick(ECFieldElement[] zs, int off, int len) - { - MontgomeryTrick(zs, off, len, null); - } - public static void MontgomeryTrick(ECFieldElement[] zs, int off, int len, ECFieldElement scale) { /* @@ -199,14 +97,6 @@ public static ECPoint ReferenceMultiply(ECPoint p, BigInteger k) return k.SignValue < 0 ? q.Negate() : q; } - public static ECPoint ValidatePoint(ECPoint p) - { - if (!p.IsValid()) - throw new InvalidOperationException("Invalid point"); - - return p; - } - public static ECPoint CleanPoint(ECCurve c, ECPoint p) { ECCurve cp = p.Curve; @@ -223,274 +113,5 @@ internal static ECPoint ImplCheckResult(ECPoint p) return p; } - - internal static ECPoint ImplShamirsTrickJsf(ECPoint P, BigInteger k, ECPoint Q, BigInteger l) - { - ECCurve curve = P.Curve; - ECPoint infinity = curve.Infinity; - - // TODO conjugate co-Z addition (ZADDC) can return both of these - ECPoint PaddQ = P.Add(Q); - ECPoint PsubQ = P.Subtract(Q); - - ECPoint[] points = new ECPoint[] { Q, PsubQ, P, PaddQ }; - curve.NormalizeAll(points); - - ECPoint[] table = new ECPoint[] { - points[3].Negate(), points[2].Negate(), points[1].Negate(), - points[0].Negate(), infinity, points[0], - points[1], points[2], points[3] }; - - byte[] jsf = WNafUtilities.GenerateJsf(k, l); - - ECPoint R = infinity; - - int i = jsf.Length; - while (--i >= 0) - { - int jsfi = jsf[i]; - - // NOTE: The shifting ensures the sign is extended correctly - int kDigit = ((jsfi << 24) >> 28), lDigit = ((jsfi << 28) >> 28); - - int index = 4 + (kDigit * 3) + lDigit; - R = R.TwicePlus(table[index]); - } - - return R; - } - - internal static ECPoint ImplShamirsTrickWNaf(ECPoint P, BigInteger k, - ECPoint Q, BigInteger l) - { - bool negK = k.SignValue < 0, negL = l.SignValue < 0; - - k = k.Abs(); - l = l.Abs(); - - int widthP = System.Math.Max(2, System.Math.Min(16, WNafUtilities.GetWindowSize(k.BitLength))); - int widthQ = System.Math.Max(2, System.Math.Min(16, WNafUtilities.GetWindowSize(l.BitLength))); - - WNafPreCompInfo infoP = WNafUtilities.Precompute(P, widthP, true); - WNafPreCompInfo infoQ = WNafUtilities.Precompute(Q, widthQ, true); - - ECPoint[] preCompP = negK ? infoP.PreCompNeg : infoP.PreComp; - ECPoint[] preCompQ = negL ? infoQ.PreCompNeg : infoQ.PreComp; - ECPoint[] preCompNegP = negK ? infoP.PreComp : infoP.PreCompNeg; - ECPoint[] preCompNegQ = negL ? infoQ.PreComp : infoQ.PreCompNeg; - - byte[] wnafP = WNafUtilities.GenerateWindowNaf(widthP, k); - byte[] wnafQ = WNafUtilities.GenerateWindowNaf(widthQ, l); - - return ImplShamirsTrickWNaf(preCompP, preCompNegP, wnafP, preCompQ, preCompNegQ, wnafQ); - } - - internal static ECPoint ImplShamirsTrickWNaf(ECPoint P, BigInteger k, ECPointMap pointMapQ, BigInteger l) - { - bool negK = k.SignValue < 0, negL = l.SignValue < 0; - - k = k.Abs(); - l = l.Abs(); - - int width = System.Math.Max(2, System.Math.Min(16, WNafUtilities.GetWindowSize(System.Math.Max(k.BitLength, l.BitLength)))); - - ECPoint Q = WNafUtilities.MapPointWithPrecomp(P, width, true, pointMapQ); - WNafPreCompInfo infoP = WNafUtilities.GetWNafPreCompInfo(P); - WNafPreCompInfo infoQ = WNafUtilities.GetWNafPreCompInfo(Q); - - ECPoint[] preCompP = negK ? infoP.PreCompNeg : infoP.PreComp; - ECPoint[] preCompQ = negL ? infoQ.PreCompNeg : infoQ.PreComp; - ECPoint[] preCompNegP = negK ? infoP.PreComp : infoP.PreCompNeg; - ECPoint[] preCompNegQ = negL ? infoQ.PreComp : infoQ.PreCompNeg; - - byte[] wnafP = WNafUtilities.GenerateWindowNaf(width, k); - byte[] wnafQ = WNafUtilities.GenerateWindowNaf(width, l); - - return ImplShamirsTrickWNaf(preCompP, preCompNegP, wnafP, preCompQ, preCompNegQ, wnafQ); - } - - private static ECPoint ImplShamirsTrickWNaf(ECPoint[] preCompP, ECPoint[] preCompNegP, byte[] wnafP, - ECPoint[] preCompQ, ECPoint[] preCompNegQ, byte[] wnafQ) - { - int len = System.Math.Max(wnafP.Length, wnafQ.Length); - - ECCurve curve = preCompP[0].Curve; - ECPoint infinity = curve.Infinity; - - ECPoint R = infinity; - int zeroes = 0; - - for (int i = len - 1; i >= 0; --i) - { - int wiP = i < wnafP.Length ? (int)(sbyte)wnafP[i] : 0; - int wiQ = i < wnafQ.Length ? (int)(sbyte)wnafQ[i] : 0; - - if ((wiP | wiQ) == 0) - { - ++zeroes; - continue; - } - - ECPoint r = infinity; - if (wiP != 0) - { - int nP = System.Math.Abs(wiP); - ECPoint[] tableP = wiP < 0 ? preCompNegP : preCompP; - r = r.Add(tableP[nP >> 1]); - } - if (wiQ != 0) - { - int nQ = System.Math.Abs(wiQ); - ECPoint[] tableQ = wiQ < 0 ? preCompNegQ : preCompQ; - r = r.Add(tableQ[nQ >> 1]); - } - - if (zeroes > 0) - { - R = R.TimesPow2(zeroes); - zeroes = 0; - } - - R = R.TwicePlus(r); - } - - if (zeroes > 0) - { - R = R.TimesPow2(zeroes); - } - - return R; - } - - internal static ECPoint ImplSumOfMultiplies(ECPoint[] ps, BigInteger[] ks) - { - int count = ps.Length; - bool[] negs = new bool[count]; - WNafPreCompInfo[] infos = new WNafPreCompInfo[count]; - byte[][] wnafs = new byte[count][]; - - for (int i = 0; i < count; ++i) - { - BigInteger ki = ks[i]; negs[i] = ki.SignValue < 0; ki = ki.Abs(); - - int width = System.Math.Max(2, System.Math.Min(16, WNafUtilities.GetWindowSize(ki.BitLength))); - infos[i] = WNafUtilities.Precompute(ps[i], width, true); - wnafs[i] = WNafUtilities.GenerateWindowNaf(width, ki); - } - - return ImplSumOfMultiplies(negs, infos, wnafs); - } - - internal static ECPoint ImplSumOfMultipliesGlv(ECPoint[] ps, BigInteger[] ks, GlvEndomorphism glvEndomorphism) - { - BigInteger n = ps[0].Curve.Order; - - int len = ps.Length; - - BigInteger[] abs = new BigInteger[len << 1]; - for (int i = 0, j = 0; i < len; ++i) - { - BigInteger[] ab = glvEndomorphism.DecomposeScalar(ks[i].Mod(n)); - abs[j++] = ab[0]; - abs[j++] = ab[1]; - } - - ECPointMap pointMap = glvEndomorphism.PointMap; - if (glvEndomorphism.HasEfficientPointMap) - { - return ECAlgorithms.ImplSumOfMultiplies(ps, pointMap, abs); - } - - ECPoint[] pqs = new ECPoint[len << 1]; - for (int i = 0, j = 0; i < len; ++i) - { - ECPoint p = ps[i], q = pointMap.Map(p); - pqs[j++] = p; - pqs[j++] = q; - } - - return ECAlgorithms.ImplSumOfMultiplies(pqs, abs); - } - - internal static ECPoint ImplSumOfMultiplies(ECPoint[] ps, ECPointMap pointMap, BigInteger[] ks) - { - int halfCount = ps.Length, fullCount = halfCount << 1; - - bool[] negs = new bool[fullCount]; - WNafPreCompInfo[] infos = new WNafPreCompInfo[fullCount]; - byte[][] wnafs = new byte[fullCount][]; - - for (int i = 0; i < halfCount; ++i) - { - int j0 = i << 1, j1 = j0 + 1; - - BigInteger kj0 = ks[j0]; negs[j0] = kj0.SignValue < 0; kj0 = kj0.Abs(); - BigInteger kj1 = ks[j1]; negs[j1] = kj1.SignValue < 0; kj1 = kj1.Abs(); - - int width = System.Math.Max(2, System.Math.Min(16, WNafUtilities.GetWindowSize(System.Math.Max(kj0.BitLength, kj1.BitLength)))); - - ECPoint P = ps[i], Q = WNafUtilities.MapPointWithPrecomp(P, width, true, pointMap); - infos[j0] = WNafUtilities.GetWNafPreCompInfo(P); - infos[j1] = WNafUtilities.GetWNafPreCompInfo(Q); - wnafs[j0] = WNafUtilities.GenerateWindowNaf(width, kj0); - wnafs[j1] = WNafUtilities.GenerateWindowNaf(width, kj1); - } - - return ImplSumOfMultiplies(negs, infos, wnafs); - } - - private static ECPoint ImplSumOfMultiplies(bool[] negs, WNafPreCompInfo[] infos, byte[][] wnafs) - { - int len = 0, count = wnafs.Length; - for (int i = 0; i < count; ++i) - { - len = System.Math.Max(len, wnafs[i].Length); - } - - ECCurve curve = infos[0].PreComp[0].Curve; - ECPoint infinity = curve.Infinity; - - ECPoint R = infinity; - int zeroes = 0; - - for (int i = len - 1; i >= 0; --i) - { - ECPoint r = infinity; - - for (int j = 0; j < count; ++j) - { - byte[] wnaf = wnafs[j]; - int wi = i < wnaf.Length ? (int)(sbyte)wnaf[i] : 0; - if (wi != 0) - { - int n = System.Math.Abs(wi); - WNafPreCompInfo info = infos[j]; - ECPoint[] table = (wi < 0 == negs[j]) ? info.PreComp : info.PreCompNeg; - r = r.Add(table[n >> 1]); - } - } - - if (r == infinity) - { - ++zeroes; - continue; - } - - if (zeroes > 0) - { - R = R.TimesPow2(zeroes); - zeroes = 0; - } - - R = R.TwicePlus(r); - } - - if (zeroes > 0) - { - R = R.TimesPow2(zeroes); - } - - return R; - } } } diff --git a/src/Renci.SshNet/Security/BouncyCastle/math/ec/ECCurve.cs b/src/Renci.SshNet/Security/BouncyCastle/math/ec/ECCurve.cs index 46c69184d..5ddd074a2 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/math/ec/ECCurve.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/math/ec/ECCurve.cs @@ -2,7 +2,6 @@ using System.Collections; using System.Collections.Generic; using Renci.SshNet.Security.Org.BouncyCastle.Math.EC.Abc; -using Renci.SshNet.Security.Org.BouncyCastle.Math.EC.Endo; using Renci.SshNet.Security.Org.BouncyCastle.Math.EC.Multiplier; using Renci.SshNet.Security.Org.BouncyCastle.Math.Field; using Renci.SshNet.Security.Org.BouncyCastle.Math.Raw; @@ -22,72 +21,11 @@ internal abstract class ECCurve public const int COORD_LAMBDA_PROJECTIVE = 6; public const int COORD_SKEWED = 7; - public static int[] GetAllCoordinateSystems() - { - return new int[]{ COORD_AFFINE, COORD_HOMOGENEOUS, COORD_JACOBIAN, COORD_JACOBIAN_CHUDNOVSKY, - COORD_JACOBIAN_MODIFIED, COORD_LAMBDA_AFFINE, COORD_LAMBDA_PROJECTIVE, COORD_SKEWED }; - } - - internal class Config - { - protected ECCurve outer; - protected int coord; - protected ECEndomorphism endomorphism; - protected ECMultiplier multiplier; - - internal Config(ECCurve outer, int coord, ECEndomorphism endomorphism, ECMultiplier multiplier) - { - this.outer = outer; - this.coord = coord; - this.endomorphism = endomorphism; - this.multiplier = multiplier; - } - - public Config SetCoordinateSystem(int coord) - { - this.coord = coord; - return this; - } - - public Config SetEndomorphism(ECEndomorphism endomorphism) - { - this.endomorphism = endomorphism; - return this; - } - - public Config SetMultiplier(ECMultiplier multiplier) - { - this.multiplier = multiplier; - return this; - } - - public ECCurve Create() - { - if (!outer.SupportsCoordinateSystem(coord)) - { - throw new InvalidOperationException("unsupported coordinate system"); - } - - ECCurve c = outer.CloneCurve(); - if (c == outer) - { - throw new InvalidOperationException("implementation returned current curve"); - } - - c.m_coord = coord; - c.m_endomorphism = endomorphism; - c.m_multiplier = multiplier; - - return c; - } - } - protected readonly IFiniteField m_field; protected ECFieldElement m_a, m_b; protected BigInteger m_order, m_cofactor; protected int m_coord = COORD_AFFINE; - protected ECEndomorphism m_endomorphism = null; protected ECMultiplier m_multiplier = null; protected ECCurve(IFiniteField field) @@ -99,11 +37,6 @@ protected ECCurve(IFiniteField field) public abstract ECFieldElement FromBigInteger(BigInteger x); public abstract bool IsValidFieldElement(BigInteger x); - public virtual Config Configure() - { - return new Config(this, this.m_coord, this.m_endomorphism, this.m_multiplier); - } - public virtual ECPoint ValidatePoint(BigInteger x, BigInteger y) { ECPoint p = CreatePoint(x, y); @@ -114,16 +47,6 @@ public virtual ECPoint ValidatePoint(BigInteger x, BigInteger y) return p; } - public virtual ECPoint ValidatePoint(BigInteger x, BigInteger y, bool withCompression) - { - ECPoint p = CreatePoint(x, y, withCompression); - if (!p.IsValid()) - { - throw new ArgumentException("Invalid point coordinates"); - } - return p; - } - public virtual ECPoint CreatePoint(BigInteger x, BigInteger y) { return CreatePoint(x, y, false); @@ -142,12 +65,6 @@ public virtual ECPoint CreatePoint(BigInteger x, BigInteger y, bool withCompress protected virtual ECMultiplier CreateDefaultMultiplier() { - GlvEndomorphism glvEndomorphism = m_endomorphism as GlvEndomorphism; - if (glvEndomorphism != null) - { - return new GlvMultiplier(this, glvEndomorphism); - } - return new WNafL2RMultiplier(); } @@ -156,25 +73,6 @@ public virtual bool SupportsCoordinateSystem(int coord) return coord == COORD_AFFINE; } - public virtual PreCompInfo GetPreCompInfo(ECPoint point, string name) - { - CheckPoint(point); - - IDictionary table; - lock (point) - { - table = point.m_preCompTable; - } - - if (null == table) - return null; - - lock (table) - { - return (PreCompInfo)table[name]; - } - } - /** * Compute a PreCompInfo for a point on this curve, under a given name. Used by * ECMultipliers to save the precomputation for this ECPoint for use @@ -376,11 +274,6 @@ protected virtual void CheckPoint(ECPoint point) throw new ArgumentException("must be non-null and on this curve", "point"); } - protected virtual void CheckPoints(ECPoint[] points) - { - CheckPoints(points, 0, points.Length); - } - protected virtual void CheckPoints(ECPoint[] points, int off, int len) { if (points == null) @@ -421,11 +314,6 @@ public override int GetHashCode() protected abstract ECPoint DecompressPoint(int yTilde, BigInteger X1); - public virtual ECEndomorphism GetEndomorphism() - { - return m_endomorphism; - } - /** * Sets the default ECMultiplier, unless already set. */ @@ -609,11 +497,6 @@ internal class FpCurve protected readonly BigInteger m_q, m_r; protected readonly FpPoint m_infinity; - public FpCurve(BigInteger q, BigInteger a, BigInteger b) - : this(q, a, b, null, null) - { - } - public FpCurve(BigInteger q, BigInteger a, BigInteger b, BigInteger order, BigInteger cofactor) : base(q) { @@ -628,11 +511,6 @@ public FpCurve(BigInteger q, BigInteger a, BigInteger b, BigInteger order, BigIn this.m_coord = FP_DEFAULT_COORDS; } - protected FpCurve(BigInteger q, BigInteger r, ECFieldElement a, ECFieldElement b) - : this(q, r, a, b, null, null) - { - } - protected FpCurve(BigInteger q, BigInteger r, ECFieldElement a, ECFieldElement b, BigInteger order, BigInteger cofactor) : base(q) { @@ -722,11 +600,6 @@ public override ECPoint ImportPoint(ECPoint p) internal abstract class AbstractF2mCurve : ECCurve { - public static BigInteger Inverse(int m, int[] ks, BigInteger x) - { - return new LongArray(x).ModInverse(m, ks).ToBigInteger(); - } - /** * The auxiliary values s0 and * s1 used for partial modular reduction for @@ -963,90 +836,6 @@ internal class F2mCurve */ protected readonly F2mPoint m_infinity; - /** - * Constructor for Trinomial Polynomial Basis (TPB). - * @param m The exponent m of - * F2m. - * @param k The integer k where xm + - * xk + 1 represents the reduction - * polynomial f(z). - * @param a The coefficient a in the Weierstrass equation - * for non-supersingular elliptic curves over - * F2m. - * @param b The coefficient b in the Weierstrass equation - * for non-supersingular elliptic curves over - * F2m. - */ - [Obsolete("Use constructor taking order/cofactor")] - public F2mCurve( - int m, - int k, - BigInteger a, - BigInteger b) - : this(m, k, 0, 0, a, b, null, null) - { - } - - /** - * Constructor for Trinomial Polynomial Basis (TPB). - * @param m The exponent m of - * F2m. - * @param k The integer k where xm + - * xk + 1 represents the reduction - * polynomial f(z). - * @param a The coefficient a in the Weierstrass equation - * for non-supersingular elliptic curves over - * F2m. - * @param b The coefficient b in the Weierstrass equation - * for non-supersingular elliptic curves over - * F2m. - * @param order The order of the main subgroup of the elliptic curve. - * @param cofactor The cofactor of the elliptic curve, i.e. - * #Ea(F2m) = h * n. - */ - public F2mCurve( - int m, - int k, - BigInteger a, - BigInteger b, - BigInteger order, - BigInteger cofactor) - : this(m, k, 0, 0, a, b, order, cofactor) - { - } - - /** - * Constructor for Pentanomial Polynomial Basis (PPB). - * @param m The exponent m of - * F2m. - * @param k1 The integer k1 where xm + - * xk3 + xk2 + xk1 + 1 - * represents the reduction polynomial f(z). - * @param k2 The integer k2 where xm + - * xk3 + xk2 + xk1 + 1 - * represents the reduction polynomial f(z). - * @param k3 The integer k3 where xm + - * xk3 + xk2 + xk1 + 1 - * represents the reduction polynomial f(z). - * @param a The coefficient a in the Weierstrass equation - * for non-supersingular elliptic curves over - * F2m. - * @param b The coefficient b in the Weierstrass equation - * for non-supersingular elliptic curves over - * F2m. - */ - [Obsolete("Use constructor taking order/cofactor")] - public F2mCurve( - int m, - int k1, - int k2, - int k3, - BigInteger a, - BigInteger b) - : this(m, k1, k2, k3, a, b, null, null) - { - } - /** * Constructor for Pentanomial Polynomial Basis (PPB). * @param m The exponent m of diff --git a/src/Renci.SshNet/Security/BouncyCastle/math/ec/ECFieldElement.cs b/src/Renci.SshNet/Security/BouncyCastle/math/ec/ECFieldElement.cs index 3ebb3332c..dec33c793 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/math/ec/ECFieldElement.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/math/ec/ECFieldElement.cs @@ -129,12 +129,6 @@ internal static BigInteger CalculateResidue(BigInteger p) return null; } - [Obsolete("Use ECCurve.FromBigInteger to construct field elements")] - public FpFieldElement(BigInteger q, BigInteger x) - : this(q, CalculateResidue(q), x) - { - } - internal FpFieldElement(BigInteger q, BigInteger r, BigInteger x) { if (x == null || x.SignValue < 0 || x.CompareTo(q) >= 0) @@ -414,15 +408,6 @@ protected virtual BigInteger ModDouble(BigInteger x) return _2x; } - protected virtual BigInteger ModHalf(BigInteger x) - { - if (x.TestBit(0)) - { - x = q.Add(x); - } - return x.ShiftRight(1); - } - protected virtual BigInteger ModHalfAbs(BigInteger x) { if (x.TestBit(0)) @@ -544,23 +529,6 @@ public override int GetHashCode() internal abstract class AbstractF2mFieldElement : ECFieldElement { - public virtual ECFieldElement HalfTrace() - { - int m = FieldSize; - if ((m & 1) == 0) - throw new InvalidOperationException("Half-trace only defined for odd m"); - - ECFieldElement fe = this; - ECFieldElement ht = fe; - for (int i = 2; i < m; i += 2) - { - fe = fe.SquarePow(2); - ht = ht.Add(fe); - } - - return ht; - } - public virtual int Trace() { int m = FieldSize; @@ -670,24 +638,6 @@ public F2mFieldElement( this.x = new LongArray(x); } - /** - * Constructor for Tpb. - * @param m The exponent m of - * F2m. - * @param k The integer k where xm + - * xk + 1 represents the reduction - * polynomial f(z). - * @param x The BigInteger representing the value of the field element. - */ - public F2mFieldElement( - int m, - int k, - BigInteger x) - : this(m, k, 0, 0, x) - { - // Set k1 to k, and set k2 and k3 to 0 - } - internal F2mFieldElement(int m, int[] ks, LongArray x) { this.m = m; diff --git a/src/Renci.SshNet/Security/BouncyCastle/math/ec/ECPoint.cs b/src/Renci.SshNet/Security/BouncyCastle/math/ec/ECPoint.cs index 3a93c3e76..ebe045154 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/math/ec/ECPoint.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/math/ec/ECPoint.cs @@ -81,11 +81,6 @@ protected virtual bool SatisfiesOrder() return n == null || ECAlgorithms.ReferenceMultiply(this, n).IsInfinity; } - public ECPoint GetDetachedPoint() - { - return Normalize().Detach(); - } - public virtual ECCurve Curve { get { return m_curve; } @@ -165,18 +160,6 @@ public virtual ECFieldElement GetZCoord(int index) return (index < 0 || index >= m_zs.Length) ? null : m_zs[index]; } - public virtual ECFieldElement[] GetZCoords() - { - int zsLen = m_zs.Length; - if (zsLen == 0) - { - return m_zs; - } - ECFieldElement[] copy = new ECFieldElement[zsLen]; - Array.Copy(m_zs, 0, copy, 0, zsLen); - return copy; - } - protected internal ECFieldElement RawXCoord { get { return m_x; } diff --git a/src/Renci.SshNet/Security/BouncyCastle/math/ec/LongArray.cs b/src/Renci.SshNet/Security/BouncyCastle/math/ec/LongArray.cs index 6b51b5ff6..4adbb4a9a 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/math/ec/LongArray.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/math/ec/LongArray.cs @@ -49,225 +49,6 @@ internal class LongArray 0x5540, 0x5541, 0x5544, 0x5545, 0x5550, 0x5551, 0x5554, 0x5555 }; - /* - * This expands 7 bit indices into 21 bit contents (high bit 18), by inserting 0s between bits. - */ - private static readonly int[] INTERLEAVE3_TABLE = new int[] - { - 0x00000, 0x00001, 0x00008, 0x00009, 0x00040, 0x00041, 0x00048, 0x00049, - 0x00200, 0x00201, 0x00208, 0x00209, 0x00240, 0x00241, 0x00248, 0x00249, - 0x01000, 0x01001, 0x01008, 0x01009, 0x01040, 0x01041, 0x01048, 0x01049, - 0x01200, 0x01201, 0x01208, 0x01209, 0x01240, 0x01241, 0x01248, 0x01249, - 0x08000, 0x08001, 0x08008, 0x08009, 0x08040, 0x08041, 0x08048, 0x08049, - 0x08200, 0x08201, 0x08208, 0x08209, 0x08240, 0x08241, 0x08248, 0x08249, - 0x09000, 0x09001, 0x09008, 0x09009, 0x09040, 0x09041, 0x09048, 0x09049, - 0x09200, 0x09201, 0x09208, 0x09209, 0x09240, 0x09241, 0x09248, 0x09249, - 0x40000, 0x40001, 0x40008, 0x40009, 0x40040, 0x40041, 0x40048, 0x40049, - 0x40200, 0x40201, 0x40208, 0x40209, 0x40240, 0x40241, 0x40248, 0x40249, - 0x41000, 0x41001, 0x41008, 0x41009, 0x41040, 0x41041, 0x41048, 0x41049, - 0x41200, 0x41201, 0x41208, 0x41209, 0x41240, 0x41241, 0x41248, 0x41249, - 0x48000, 0x48001, 0x48008, 0x48009, 0x48040, 0x48041, 0x48048, 0x48049, - 0x48200, 0x48201, 0x48208, 0x48209, 0x48240, 0x48241, 0x48248, 0x48249, - 0x49000, 0x49001, 0x49008, 0x49009, 0x49040, 0x49041, 0x49048, 0x49049, - 0x49200, 0x49201, 0x49208, 0x49209, 0x49240, 0x49241, 0x49248, 0x49249 - }; - - /* - * This expands 8 bit indices into 32 bit contents (high bit 28), by inserting 0s between bits. - */ - private static readonly int[] INTERLEAVE4_TABLE = new int[] - { - 0x00000000, 0x00000001, 0x00000010, 0x00000011, 0x00000100, 0x00000101, 0x00000110, 0x00000111, - 0x00001000, 0x00001001, 0x00001010, 0x00001011, 0x00001100, 0x00001101, 0x00001110, 0x00001111, - 0x00010000, 0x00010001, 0x00010010, 0x00010011, 0x00010100, 0x00010101, 0x00010110, 0x00010111, - 0x00011000, 0x00011001, 0x00011010, 0x00011011, 0x00011100, 0x00011101, 0x00011110, 0x00011111, - 0x00100000, 0x00100001, 0x00100010, 0x00100011, 0x00100100, 0x00100101, 0x00100110, 0x00100111, - 0x00101000, 0x00101001, 0x00101010, 0x00101011, 0x00101100, 0x00101101, 0x00101110, 0x00101111, - 0x00110000, 0x00110001, 0x00110010, 0x00110011, 0x00110100, 0x00110101, 0x00110110, 0x00110111, - 0x00111000, 0x00111001, 0x00111010, 0x00111011, 0x00111100, 0x00111101, 0x00111110, 0x00111111, - 0x01000000, 0x01000001, 0x01000010, 0x01000011, 0x01000100, 0x01000101, 0x01000110, 0x01000111, - 0x01001000, 0x01001001, 0x01001010, 0x01001011, 0x01001100, 0x01001101, 0x01001110, 0x01001111, - 0x01010000, 0x01010001, 0x01010010, 0x01010011, 0x01010100, 0x01010101, 0x01010110, 0x01010111, - 0x01011000, 0x01011001, 0x01011010, 0x01011011, 0x01011100, 0x01011101, 0x01011110, 0x01011111, - 0x01100000, 0x01100001, 0x01100010, 0x01100011, 0x01100100, 0x01100101, 0x01100110, 0x01100111, - 0x01101000, 0x01101001, 0x01101010, 0x01101011, 0x01101100, 0x01101101, 0x01101110, 0x01101111, - 0x01110000, 0x01110001, 0x01110010, 0x01110011, 0x01110100, 0x01110101, 0x01110110, 0x01110111, - 0x01111000, 0x01111001, 0x01111010, 0x01111011, 0x01111100, 0x01111101, 0x01111110, 0x01111111, - 0x10000000, 0x10000001, 0x10000010, 0x10000011, 0x10000100, 0x10000101, 0x10000110, 0x10000111, - 0x10001000, 0x10001001, 0x10001010, 0x10001011, 0x10001100, 0x10001101, 0x10001110, 0x10001111, - 0x10010000, 0x10010001, 0x10010010, 0x10010011, 0x10010100, 0x10010101, 0x10010110, 0x10010111, - 0x10011000, 0x10011001, 0x10011010, 0x10011011, 0x10011100, 0x10011101, 0x10011110, 0x10011111, - 0x10100000, 0x10100001, 0x10100010, 0x10100011, 0x10100100, 0x10100101, 0x10100110, 0x10100111, - 0x10101000, 0x10101001, 0x10101010, 0x10101011, 0x10101100, 0x10101101, 0x10101110, 0x10101111, - 0x10110000, 0x10110001, 0x10110010, 0x10110011, 0x10110100, 0x10110101, 0x10110110, 0x10110111, - 0x10111000, 0x10111001, 0x10111010, 0x10111011, 0x10111100, 0x10111101, 0x10111110, 0x10111111, - 0x11000000, 0x11000001, 0x11000010, 0x11000011, 0x11000100, 0x11000101, 0x11000110, 0x11000111, - 0x11001000, 0x11001001, 0x11001010, 0x11001011, 0x11001100, 0x11001101, 0x11001110, 0x11001111, - 0x11010000, 0x11010001, 0x11010010, 0x11010011, 0x11010100, 0x11010101, 0x11010110, 0x11010111, - 0x11011000, 0x11011001, 0x11011010, 0x11011011, 0x11011100, 0x11011101, 0x11011110, 0x11011111, - 0x11100000, 0x11100001, 0x11100010, 0x11100011, 0x11100100, 0x11100101, 0x11100110, 0x11100111, - 0x11101000, 0x11101001, 0x11101010, 0x11101011, 0x11101100, 0x11101101, 0x11101110, 0x11101111, - 0x11110000, 0x11110001, 0x11110010, 0x11110011, 0x11110100, 0x11110101, 0x11110110, 0x11110111, - 0x11111000, 0x11111001, 0x11111010, 0x11111011, 0x11111100, 0x11111101, 0x11111110, 0x11111111 - }; - - /* - * This expands 7 bit indices into 35 bit contents (high bit 30), by inserting 0s between bits. - */ - private static readonly int[] INTERLEAVE5_TABLE = new int[] { - 0x00000000, 0x00000001, 0x00000020, 0x00000021, 0x00000400, 0x00000401, 0x00000420, 0x00000421, - 0x00008000, 0x00008001, 0x00008020, 0x00008021, 0x00008400, 0x00008401, 0x00008420, 0x00008421, - 0x00100000, 0x00100001, 0x00100020, 0x00100021, 0x00100400, 0x00100401, 0x00100420, 0x00100421, - 0x00108000, 0x00108001, 0x00108020, 0x00108021, 0x00108400, 0x00108401, 0x00108420, 0x00108421, - 0x02000000, 0x02000001, 0x02000020, 0x02000021, 0x02000400, 0x02000401, 0x02000420, 0x02000421, - 0x02008000, 0x02008001, 0x02008020, 0x02008021, 0x02008400, 0x02008401, 0x02008420, 0x02008421, - 0x02100000, 0x02100001, 0x02100020, 0x02100021, 0x02100400, 0x02100401, 0x02100420, 0x02100421, - 0x02108000, 0x02108001, 0x02108020, 0x02108021, 0x02108400, 0x02108401, 0x02108420, 0x02108421, - 0x40000000, 0x40000001, 0x40000020, 0x40000021, 0x40000400, 0x40000401, 0x40000420, 0x40000421, - 0x40008000, 0x40008001, 0x40008020, 0x40008021, 0x40008400, 0x40008401, 0x40008420, 0x40008421, - 0x40100000, 0x40100001, 0x40100020, 0x40100021, 0x40100400, 0x40100401, 0x40100420, 0x40100421, - 0x40108000, 0x40108001, 0x40108020, 0x40108021, 0x40108400, 0x40108401, 0x40108420, 0x40108421, - 0x42000000, 0x42000001, 0x42000020, 0x42000021, 0x42000400, 0x42000401, 0x42000420, 0x42000421, - 0x42008000, 0x42008001, 0x42008020, 0x42008021, 0x42008400, 0x42008401, 0x42008420, 0x42008421, - 0x42100000, 0x42100001, 0x42100020, 0x42100021, 0x42100400, 0x42100401, 0x42100420, 0x42100421, - 0x42108000, 0x42108001, 0x42108020, 0x42108021, 0x42108400, 0x42108401, 0x42108420, 0x42108421 - }; - - /* - * This expands 9 bit indices into 63 bit (long) contents (high bit 56), by inserting 0s between bits. - */ - private static readonly long[] INTERLEAVE7_TABLE = new long[] - { - 0x0000000000000000L, 0x0000000000000001L, 0x0000000000000080L, 0x0000000000000081L, - 0x0000000000004000L, 0x0000000000004001L, 0x0000000000004080L, 0x0000000000004081L, - 0x0000000000200000L, 0x0000000000200001L, 0x0000000000200080L, 0x0000000000200081L, - 0x0000000000204000L, 0x0000000000204001L, 0x0000000000204080L, 0x0000000000204081L, - 0x0000000010000000L, 0x0000000010000001L, 0x0000000010000080L, 0x0000000010000081L, - 0x0000000010004000L, 0x0000000010004001L, 0x0000000010004080L, 0x0000000010004081L, - 0x0000000010200000L, 0x0000000010200001L, 0x0000000010200080L, 0x0000000010200081L, - 0x0000000010204000L, 0x0000000010204001L, 0x0000000010204080L, 0x0000000010204081L, - 0x0000000800000000L, 0x0000000800000001L, 0x0000000800000080L, 0x0000000800000081L, - 0x0000000800004000L, 0x0000000800004001L, 0x0000000800004080L, 0x0000000800004081L, - 0x0000000800200000L, 0x0000000800200001L, 0x0000000800200080L, 0x0000000800200081L, - 0x0000000800204000L, 0x0000000800204001L, 0x0000000800204080L, 0x0000000800204081L, - 0x0000000810000000L, 0x0000000810000001L, 0x0000000810000080L, 0x0000000810000081L, - 0x0000000810004000L, 0x0000000810004001L, 0x0000000810004080L, 0x0000000810004081L, - 0x0000000810200000L, 0x0000000810200001L, 0x0000000810200080L, 0x0000000810200081L, - 0x0000000810204000L, 0x0000000810204001L, 0x0000000810204080L, 0x0000000810204081L, - 0x0000040000000000L, 0x0000040000000001L, 0x0000040000000080L, 0x0000040000000081L, - 0x0000040000004000L, 0x0000040000004001L, 0x0000040000004080L, 0x0000040000004081L, - 0x0000040000200000L, 0x0000040000200001L, 0x0000040000200080L, 0x0000040000200081L, - 0x0000040000204000L, 0x0000040000204001L, 0x0000040000204080L, 0x0000040000204081L, - 0x0000040010000000L, 0x0000040010000001L, 0x0000040010000080L, 0x0000040010000081L, - 0x0000040010004000L, 0x0000040010004001L, 0x0000040010004080L, 0x0000040010004081L, - 0x0000040010200000L, 0x0000040010200001L, 0x0000040010200080L, 0x0000040010200081L, - 0x0000040010204000L, 0x0000040010204001L, 0x0000040010204080L, 0x0000040010204081L, - 0x0000040800000000L, 0x0000040800000001L, 0x0000040800000080L, 0x0000040800000081L, - 0x0000040800004000L, 0x0000040800004001L, 0x0000040800004080L, 0x0000040800004081L, - 0x0000040800200000L, 0x0000040800200001L, 0x0000040800200080L, 0x0000040800200081L, - 0x0000040800204000L, 0x0000040800204001L, 0x0000040800204080L, 0x0000040800204081L, - 0x0000040810000000L, 0x0000040810000001L, 0x0000040810000080L, 0x0000040810000081L, - 0x0000040810004000L, 0x0000040810004001L, 0x0000040810004080L, 0x0000040810004081L, - 0x0000040810200000L, 0x0000040810200001L, 0x0000040810200080L, 0x0000040810200081L, - 0x0000040810204000L, 0x0000040810204001L, 0x0000040810204080L, 0x0000040810204081L, - 0x0002000000000000L, 0x0002000000000001L, 0x0002000000000080L, 0x0002000000000081L, - 0x0002000000004000L, 0x0002000000004001L, 0x0002000000004080L, 0x0002000000004081L, - 0x0002000000200000L, 0x0002000000200001L, 0x0002000000200080L, 0x0002000000200081L, - 0x0002000000204000L, 0x0002000000204001L, 0x0002000000204080L, 0x0002000000204081L, - 0x0002000010000000L, 0x0002000010000001L, 0x0002000010000080L, 0x0002000010000081L, - 0x0002000010004000L, 0x0002000010004001L, 0x0002000010004080L, 0x0002000010004081L, - 0x0002000010200000L, 0x0002000010200001L, 0x0002000010200080L, 0x0002000010200081L, - 0x0002000010204000L, 0x0002000010204001L, 0x0002000010204080L, 0x0002000010204081L, - 0x0002000800000000L, 0x0002000800000001L, 0x0002000800000080L, 0x0002000800000081L, - 0x0002000800004000L, 0x0002000800004001L, 0x0002000800004080L, 0x0002000800004081L, - 0x0002000800200000L, 0x0002000800200001L, 0x0002000800200080L, 0x0002000800200081L, - 0x0002000800204000L, 0x0002000800204001L, 0x0002000800204080L, 0x0002000800204081L, - 0x0002000810000000L, 0x0002000810000001L, 0x0002000810000080L, 0x0002000810000081L, - 0x0002000810004000L, 0x0002000810004001L, 0x0002000810004080L, 0x0002000810004081L, - 0x0002000810200000L, 0x0002000810200001L, 0x0002000810200080L, 0x0002000810200081L, - 0x0002000810204000L, 0x0002000810204001L, 0x0002000810204080L, 0x0002000810204081L, - 0x0002040000000000L, 0x0002040000000001L, 0x0002040000000080L, 0x0002040000000081L, - 0x0002040000004000L, 0x0002040000004001L, 0x0002040000004080L, 0x0002040000004081L, - 0x0002040000200000L, 0x0002040000200001L, 0x0002040000200080L, 0x0002040000200081L, - 0x0002040000204000L, 0x0002040000204001L, 0x0002040000204080L, 0x0002040000204081L, - 0x0002040010000000L, 0x0002040010000001L, 0x0002040010000080L, 0x0002040010000081L, - 0x0002040010004000L, 0x0002040010004001L, 0x0002040010004080L, 0x0002040010004081L, - 0x0002040010200000L, 0x0002040010200001L, 0x0002040010200080L, 0x0002040010200081L, - 0x0002040010204000L, 0x0002040010204001L, 0x0002040010204080L, 0x0002040010204081L, - 0x0002040800000000L, 0x0002040800000001L, 0x0002040800000080L, 0x0002040800000081L, - 0x0002040800004000L, 0x0002040800004001L, 0x0002040800004080L, 0x0002040800004081L, - 0x0002040800200000L, 0x0002040800200001L, 0x0002040800200080L, 0x0002040800200081L, - 0x0002040800204000L, 0x0002040800204001L, 0x0002040800204080L, 0x0002040800204081L, - 0x0002040810000000L, 0x0002040810000001L, 0x0002040810000080L, 0x0002040810000081L, - 0x0002040810004000L, 0x0002040810004001L, 0x0002040810004080L, 0x0002040810004081L, - 0x0002040810200000L, 0x0002040810200001L, 0x0002040810200080L, 0x0002040810200081L, - 0x0002040810204000L, 0x0002040810204001L, 0x0002040810204080L, 0x0002040810204081L, - 0x0100000000000000L, 0x0100000000000001L, 0x0100000000000080L, 0x0100000000000081L, - 0x0100000000004000L, 0x0100000000004001L, 0x0100000000004080L, 0x0100000000004081L, - 0x0100000000200000L, 0x0100000000200001L, 0x0100000000200080L, 0x0100000000200081L, - 0x0100000000204000L, 0x0100000000204001L, 0x0100000000204080L, 0x0100000000204081L, - 0x0100000010000000L, 0x0100000010000001L, 0x0100000010000080L, 0x0100000010000081L, - 0x0100000010004000L, 0x0100000010004001L, 0x0100000010004080L, 0x0100000010004081L, - 0x0100000010200000L, 0x0100000010200001L, 0x0100000010200080L, 0x0100000010200081L, - 0x0100000010204000L, 0x0100000010204001L, 0x0100000010204080L, 0x0100000010204081L, - 0x0100000800000000L, 0x0100000800000001L, 0x0100000800000080L, 0x0100000800000081L, - 0x0100000800004000L, 0x0100000800004001L, 0x0100000800004080L, 0x0100000800004081L, - 0x0100000800200000L, 0x0100000800200001L, 0x0100000800200080L, 0x0100000800200081L, - 0x0100000800204000L, 0x0100000800204001L, 0x0100000800204080L, 0x0100000800204081L, - 0x0100000810000000L, 0x0100000810000001L, 0x0100000810000080L, 0x0100000810000081L, - 0x0100000810004000L, 0x0100000810004001L, 0x0100000810004080L, 0x0100000810004081L, - 0x0100000810200000L, 0x0100000810200001L, 0x0100000810200080L, 0x0100000810200081L, - 0x0100000810204000L, 0x0100000810204001L, 0x0100000810204080L, 0x0100000810204081L, - 0x0100040000000000L, 0x0100040000000001L, 0x0100040000000080L, 0x0100040000000081L, - 0x0100040000004000L, 0x0100040000004001L, 0x0100040000004080L, 0x0100040000004081L, - 0x0100040000200000L, 0x0100040000200001L, 0x0100040000200080L, 0x0100040000200081L, - 0x0100040000204000L, 0x0100040000204001L, 0x0100040000204080L, 0x0100040000204081L, - 0x0100040010000000L, 0x0100040010000001L, 0x0100040010000080L, 0x0100040010000081L, - 0x0100040010004000L, 0x0100040010004001L, 0x0100040010004080L, 0x0100040010004081L, - 0x0100040010200000L, 0x0100040010200001L, 0x0100040010200080L, 0x0100040010200081L, - 0x0100040010204000L, 0x0100040010204001L, 0x0100040010204080L, 0x0100040010204081L, - 0x0100040800000000L, 0x0100040800000001L, 0x0100040800000080L, 0x0100040800000081L, - 0x0100040800004000L, 0x0100040800004001L, 0x0100040800004080L, 0x0100040800004081L, - 0x0100040800200000L, 0x0100040800200001L, 0x0100040800200080L, 0x0100040800200081L, - 0x0100040800204000L, 0x0100040800204001L, 0x0100040800204080L, 0x0100040800204081L, - 0x0100040810000000L, 0x0100040810000001L, 0x0100040810000080L, 0x0100040810000081L, - 0x0100040810004000L, 0x0100040810004001L, 0x0100040810004080L, 0x0100040810004081L, - 0x0100040810200000L, 0x0100040810200001L, 0x0100040810200080L, 0x0100040810200081L, - 0x0100040810204000L, 0x0100040810204001L, 0x0100040810204080L, 0x0100040810204081L, - 0x0102000000000000L, 0x0102000000000001L, 0x0102000000000080L, 0x0102000000000081L, - 0x0102000000004000L, 0x0102000000004001L, 0x0102000000004080L, 0x0102000000004081L, - 0x0102000000200000L, 0x0102000000200001L, 0x0102000000200080L, 0x0102000000200081L, - 0x0102000000204000L, 0x0102000000204001L, 0x0102000000204080L, 0x0102000000204081L, - 0x0102000010000000L, 0x0102000010000001L, 0x0102000010000080L, 0x0102000010000081L, - 0x0102000010004000L, 0x0102000010004001L, 0x0102000010004080L, 0x0102000010004081L, - 0x0102000010200000L, 0x0102000010200001L, 0x0102000010200080L, 0x0102000010200081L, - 0x0102000010204000L, 0x0102000010204001L, 0x0102000010204080L, 0x0102000010204081L, - 0x0102000800000000L, 0x0102000800000001L, 0x0102000800000080L, 0x0102000800000081L, - 0x0102000800004000L, 0x0102000800004001L, 0x0102000800004080L, 0x0102000800004081L, - 0x0102000800200000L, 0x0102000800200001L, 0x0102000800200080L, 0x0102000800200081L, - 0x0102000800204000L, 0x0102000800204001L, 0x0102000800204080L, 0x0102000800204081L, - 0x0102000810000000L, 0x0102000810000001L, 0x0102000810000080L, 0x0102000810000081L, - 0x0102000810004000L, 0x0102000810004001L, 0x0102000810004080L, 0x0102000810004081L, - 0x0102000810200000L, 0x0102000810200001L, 0x0102000810200080L, 0x0102000810200081L, - 0x0102000810204000L, 0x0102000810204001L, 0x0102000810204080L, 0x0102000810204081L, - 0x0102040000000000L, 0x0102040000000001L, 0x0102040000000080L, 0x0102040000000081L, - 0x0102040000004000L, 0x0102040000004001L, 0x0102040000004080L, 0x0102040000004081L, - 0x0102040000200000L, 0x0102040000200001L, 0x0102040000200080L, 0x0102040000200081L, - 0x0102040000204000L, 0x0102040000204001L, 0x0102040000204080L, 0x0102040000204081L, - 0x0102040010000000L, 0x0102040010000001L, 0x0102040010000080L, 0x0102040010000081L, - 0x0102040010004000L, 0x0102040010004001L, 0x0102040010004080L, 0x0102040010004081L, - 0x0102040010200000L, 0x0102040010200001L, 0x0102040010200080L, 0x0102040010200081L, - 0x0102040010204000L, 0x0102040010204001L, 0x0102040010204080L, 0x0102040010204081L, - 0x0102040800000000L, 0x0102040800000001L, 0x0102040800000080L, 0x0102040800000081L, - 0x0102040800004000L, 0x0102040800004001L, 0x0102040800004080L, 0x0102040800004081L, - 0x0102040800200000L, 0x0102040800200001L, 0x0102040800200080L, 0x0102040800200081L, - 0x0102040800204000L, 0x0102040800204001L, 0x0102040800204080L, 0x0102040800204081L, - 0x0102040810000000L, 0x0102040810000001L, 0x0102040810000080L, 0x0102040810000081L, - 0x0102040810004000L, 0x0102040810004001L, 0x0102040810004080L, 0x0102040810004081L, - 0x0102040810200000L, 0x0102040810200001L, 0x0102040810200080L, 0x0102040810200081L, - 0x0102040810204000L, 0x0102040810204001L, 0x0102040810204080L, 0x0102040810204081L - }; - // For toString(); must have length 64 private const string ZEROES = "0000000000000000000000000000000000000000000000000000000000000000"; @@ -477,30 +258,6 @@ private int DegreeFrom(int limit) return (i << 6) + BitLength(w); } - // private int lowestCoefficient() - // { - // for (int i = 0; i < m_ints.Length; ++i) - // { - // long mi = m_ints[i]; - // if (mi != 0) - // { - // int j = 0; - // while ((mi & 0xFFL) == 0) - // { - // j += 8; - // mi >>>= 8; - // } - // while ((mi & 1L) == 0) - // { - // ++j; - // mi >>>= 1; - // } - // return (i << 6) + j; - // } - // } - // return -1; - // } - private static int BitLength(long w) { int u = (int)((ulong)w >> 32), b; @@ -577,31 +334,6 @@ public BigInteger ToBigInteger() return new BigInteger(1, barr); } - // private static long shiftUp(long[] x, int xOff, int count) - // { - // long prev = 0; - // for (int i = 0; i < count; ++i) - // { - // long next = x[xOff + i]; - // x[xOff + i] = (next << 1) | prev; - // prev = next >>> 63; - // } - // return prev; - // } - - private static long ShiftUp(long[] x, int xOff, int count, int shift) - { - int shiftInv = 64 - shift; - long prev = 0; - for (int i = 0; i < count; ++i) - { - long next = x[xOff + i]; - x[xOff + i] = (next << shift) | prev; - prev = (long)((ulong)next >> shiftInv); - } - return prev; - } - private static long ShiftUp(long[] x, int xOff, long[] z, int zOff, int count, int shift) { int shiftInv = 64 - shift; @@ -628,33 +360,6 @@ public LongArray AddOne() return new LongArray(ints); } - // private void addShiftedByBits(LongArray other, int bits) - // { - // int words = bits >>> 6; - // int shift = bits & 0x3F; - // - // if (shift == 0) - // { - // addShiftedByWords(other, words); - // return; - // } - // - // int otherUsedLen = other.GetUsedLength(); - // if (otherUsedLen == 0) - // { - // return; - // } - // - // int minLen = otherUsedLen + words + 1; - // if (minLen > m_ints.Length) - // { - // m_ints = resizedInts(minLen); - // } - // - // long carry = addShiftedByBits(m_ints, words, other.m_ints, 0, otherUsedLen, shift); - // m_ints[otherUsedLen + words] ^= carry; - // } - private void AddShiftedByBitsSafe(LongArray other, int otherDegree, int bits) { int otherLen = (int)((uint)(otherDegree + 63) >> 6); @@ -743,21 +448,6 @@ private static void AddBoth(long[] x, int xOff, long[] y1, int y1Off, long[] y2, } } - private static void Distribute(long[] x, int src, int dst1, int dst2, int count) - { - for (int i = 0; i < count; ++i) - { - long v = x[src + i]; - x[dst1 + i] ^= v; - x[dst2 + i] ^= v; - } - } - - public int Length - { - get { return m_ints.Length; } - } - private static void FlipWord(long[] buf, int off, int bit, long word) { int n = off + (int)((uint)bit >> 6); @@ -777,22 +467,6 @@ private static void FlipWord(long[] buf, int off, int bit, long word) } } - // private static long getWord(long[] buf, int off, int len, int bit) - // { - // int n = off + (bit >>> 6); - // int shift = bit & 0x3F; - // if (shift == 0) - // { - // return buf[n]; - // } - // long result = buf[n] >>> shift; - // if (++n < len) - // { - // result |= buf[n] << (64 - shift); - // } - // return result; - // } - public bool TestBitZero() { return m_ints.Length > 0 && (m_ints[0] & 1L) != 0; @@ -818,26 +492,6 @@ private static void FlipBit(long[] buf, int off, int n) buf[off + theInt] ^= flipper; } - // private static void SetBit(long[] buf, int off, int n) - // { - // // theInt = n / 64 - // int theInt = n >>> 6; - // // theBit = n % 64 - // int theBit = n & 0x3F; - // long setter = 1L << theBit; - // buf[off + theInt] |= setter; - // } - // - // private static void ClearBit(long[] buf, int off, int n) - // { - // // theInt = n / 64 - // int theInt = n >>> 6; - // // theBit = n % 64 - // int theBit = n & 0x3F; - // long setter = 1L << theBit; - // buf[off + theInt] &= ~setter; - // } - private static void MultiplyWord(long a, long[] b, int bLen, long[] c, int cOff) { if ((a & 1L) != 0L) @@ -859,138 +513,6 @@ private static void MultiplyWord(long a, long[] b, int bLen, long[] c, int cOff) } } - public LongArray ModMultiplyLD(LongArray other, int m, int[] ks) - { - /* - * Find out the degree of each argument and handle the zero cases - */ - int aDeg = Degree(); - if (aDeg == 0) - { - return this; - } - int bDeg = other.Degree(); - if (bDeg == 0) - { - return other; - } - - /* - * Swap if necessary so that A is the smaller argument - */ - LongArray A = this, B = other; - if (aDeg > bDeg) - { - A = other; B = this; - int tmp = aDeg; aDeg = bDeg; bDeg = tmp; - } - - /* - * Establish the word lengths of the arguments and result - */ - int aLen = (int)((uint)(aDeg + 63) >> 6); - int bLen = (int)((uint)(bDeg + 63) >> 6); - int cLen = (int)((uint)(aDeg + bDeg + 62) >> 6); - - if (aLen == 1) - { - long a0 = A.m_ints[0]; - if (a0 == 1L) - { - return B; - } - - /* - * Fast path for small A, with performance dependent only on the number of set bits - */ - long[] c0 = new long[cLen]; - MultiplyWord(a0, B.m_ints, bLen, c0, 0); - - /* - * Reduce the raw answer against the reduction coefficients - */ - return ReduceResult(c0, 0, cLen, m, ks); - } - - /* - * Determine if B will get bigger during shifting - */ - int bMax = (int)((uint)(bDeg + 7 + 63) >> 6); - - /* - * Lookup table for the offset of each B in the tables - */ - int[] ti = new int[16]; - - /* - * Precompute table of all 4-bit products of B - */ - long[] T0 = new long[bMax << 4]; - int tOff = bMax; - ti[1] = tOff; - Array.Copy(B.m_ints, 0, T0, tOff, bLen); - for (int i = 2; i < 16; ++i) - { - ti[i] = (tOff += bMax); - if ((i & 1) == 0) - { - ShiftUp(T0, (int)((uint)tOff >> 1), T0, tOff, bMax, 1); - } - else - { - Add(T0, bMax, T0, tOff - bMax, T0, tOff, bMax); - } - } - - /* - * Second table with all 4-bit products of B shifted 4 bits - */ - long[] T1 = new long[T0.Length]; - ShiftUp(T0, 0, T1, 0, T0.Length, 4); - // shiftUp(T0, bMax, T1, bMax, tOff, 4); - - long[] a = A.m_ints; - long[] c = new long[cLen]; - - int MASK = 0xF; - - /* - * Lopez-Dahab algorithm - */ - - for (int k = 56; k >= 0; k -= 8) - { - for (int j = 1; j < aLen; j += 2) - { - int aVal = (int)((ulong)a[j] >> k); - int u = aVal & MASK; - int v = (int)((uint)aVal >> 4) & MASK; - AddBoth(c, j - 1, T0, ti[u], T1, ti[v], bMax); - } - ShiftUp(c, 0, cLen, 8); - } - - for (int k = 56; k >= 0; k -= 8) - { - for (int j = 0; j < aLen; j += 2) - { - int aVal = (int)((ulong)a[j] >> k); - int u = aVal & MASK; - int v = (int)((uint)aVal >> 4) & MASK; - AddBoth(c, j, T0, ti[u], T1, ti[v], bMax); - } - if (k > 0) - { - ShiftUp(c, 0, cLen, 8); - } - } - - /* - * Finally the raw answer is collected, reduce it against the reduction coefficients - */ - return ReduceResult(c, 0, cLen, m, ks); - } - public LongArray ModMultiply(LongArray other, int m, int[] ks) { /* @@ -1123,230 +645,6 @@ public LongArray ModMultiply(LongArray other, int m, int[] ks) return ReduceResult(c, 0, cLen, m, ks); } - public LongArray ModMultiplyAlt(LongArray other, int m, int[] ks) - { - /* - * Find out the degree of each argument and handle the zero cases - */ - int aDeg = Degree(); - if (aDeg == 0) - { - return this; - } - int bDeg = other.Degree(); - if (bDeg == 0) - { - return other; - } - - /* - * Swap if necessary so that A is the smaller argument - */ - LongArray A = this, B = other; - if (aDeg > bDeg) - { - A = other; B = this; - int tmp = aDeg; aDeg = bDeg; bDeg = tmp; - } - - /* - * Establish the word lengths of the arguments and result - */ - int aLen = (int)((uint)(aDeg + 63) >> 6); - int bLen = (int)((uint)(bDeg + 63) >> 6); - int cLen = (int)((uint)(aDeg + bDeg + 62) >> 6); - - if (aLen == 1) - { - long a0 = A.m_ints[0]; - if (a0 == 1L) - { - return B; - } - - /* - * Fast path for small A, with performance dependent only on the number of set bits - */ - long[] c0 = new long[cLen]; - MultiplyWord(a0, B.m_ints, bLen, c0, 0); - - /* - * Reduce the raw answer against the reduction coefficients - */ - return ReduceResult(c0, 0, cLen, m, ks); - } - - // NOTE: This works, but is slower than width 4 processing - // if (aLen == 2) - // { - // /* - // * Use common-multiplicand optimization to save ~1/4 of the adds - // */ - // long a1 = A.m_ints[0], a2 = A.m_ints[1]; - // long aa = a1 & a2; a1 ^= aa; a2 ^= aa; - // - // long[] b = B.m_ints; - // long[] c = new long[cLen]; - // multiplyWord(aa, b, bLen, c, 1); - // add(c, 0, c, 1, cLen - 1); - // multiplyWord(a1, b, bLen, c, 0); - // multiplyWord(a2, b, bLen, c, 1); - // - // /* - // * Reduce the raw answer against the reduction coefficients - // */ - // return ReduceResult(c, 0, cLen, m, ks); - // } - - /* - * Determine the parameters of the Interleaved window algorithm: the 'width' in bits to - * process together, the number of evaluation 'positions' implied by that width, and the - * 'top' position at which the regular window algorithm stops. - */ - int width, positions, top, banks; - - // NOTE: width 4 is the fastest over the entire range of sizes used in current crypto - // width = 1; positions = 64; top = 64; banks = 4; - // width = 2; positions = 32; top = 64; banks = 4; - // width = 3; positions = 21; top = 63; banks = 3; - width = 4; positions = 16; top = 64; banks = 8; - // width = 5; positions = 13; top = 65; banks = 7; - // width = 7; positions = 9; top = 63; banks = 9; - // width = 8; positions = 8; top = 64; banks = 8; - - /* - * Determine if B will get bigger during shifting - */ - int shifts = top < 64 ? positions : positions - 1; - int bMax = (int)((uint)(bDeg + shifts + 63) >> 6); - - int bTotal = bMax * banks, stride = width * banks; - - /* - * Create a single temporary buffer, with an offset table to find the positions of things in it - */ - int[] ci = new int[1 << width]; - int cTotal = aLen; - { - ci[0] = cTotal; - cTotal += bTotal; - ci[1] = cTotal; - for (int i = 2; i < ci.Length; ++i) - { - cTotal += cLen; - ci[i] = cTotal; - } - cTotal += cLen; - } - // NOTE: Provide a safe dump for "high zeroes" since we are adding 'bMax' and not 'bLen' - ++cTotal; - - long[] c = new long[cTotal]; - - // Prepare A in Interleaved form, according to the chosen width - Interleave(A.m_ints, 0, c, 0, aLen, width); - - // Make a working copy of B, since we will be shifting it - { - int bOff = aLen; - Array.Copy(B.m_ints, 0, c, bOff, bLen); - for (int bank = 1; bank < banks; ++bank) - { - ShiftUp(c, aLen, c, bOff += bMax, bMax, bank); - } - } - - /* - * The main loop analyzes the Interleaved windows in A, and for each non-zero window - * a single word-array XOR is performed to a carefully selected slice of 'c'. The loop is - * breadth-first, checking the lowest window in each word, then looping again for the - * next higher window position. - */ - int MASK = (1 << width) - 1; - - int k = 0; - for (;;) - { - int aPos = 0; - do - { - long aVal = (long)((ulong)c[aPos] >> k); - int bank = 0, bOff = aLen; - for (;;) - { - int index = (int)(aVal) & MASK; - if (index != 0) - { - /* - * Add to a 'c' buffer based on the bit-pattern of 'index'. Since A is in - * Interleaved form, the bits represent the current B shifted by 0, 'positions', - * 'positions' * 2, ..., 'positions' * ('width' - 1) - */ - Add(c, aPos + ci[index], c, bOff, bMax); - } - if (++bank == banks) - { - break; - } - bOff += bMax; - aVal = (long)((ulong)aVal >> width); - } - } - while (++aPos < aLen); - - if ((k += stride) >= top) - { - if (k >= 64) - { - break; - } - - /* - * Adjustment for window setups with top == 63, the final bit (if any) is processed - * as the top-bit of a window - */ - k = 64 - width; - MASK &= MASK << (top - k); - } - - /* - * After each position has been checked for all words of A, B is shifted up 1 place - */ - ShiftUp(c, aLen, bTotal, banks); - } - - int ciPos = ci.Length; - while (--ciPos > 1) - { - if ((ciPos & 1L) == 0L) - { - /* - * For even numbers, shift contents and add to the half-position - */ - AddShiftedUp(c, ci[(uint)ciPos >> 1], c, ci[ciPos], cLen, positions); - } - else - { - /* - * For odd numbers, 'distribute' contents to the result and the next-lowest position - */ - Distribute(c, ci[ciPos], ci[ciPos - 1], ci[1], cLen); - } - } - - /* - * Finally the raw answer is collected, reduce it against the reduction coefficients - */ - return ReduceResult(c, ci[1], cLen, m, ks); - } - - public LongArray ModReduce(int m, int[] ks) - { - long[] buf = Arrays.Clone(m_ints); - int rLen = ReduceInPlace(buf, 0, buf.Length, m, ks); - return new LongArray(buf, 0, rLen); - } - public LongArray Multiply(LongArray other, int m, int[] ks) { /* @@ -1498,33 +796,6 @@ private static LongArray ReduceResult(long[] buf, int off, int len, int m, int[] return new LongArray(buf, off, rLen); } - // private static void deInterleave(long[] x, int xOff, long[] z, int zOff, int count, int rounds) - // { - // for (int i = 0; i < count; ++i) - // { - // z[zOff + i] = deInterleave(x[zOff + i], rounds); - // } - // } - // - // private static long deInterleave(long x, int rounds) - // { - // while (--rounds >= 0) - // { - // x = deInterleave32(x & DEInterleave_MASK) | (deInterleave32((x >>> 1) & DEInterleave_MASK) << 32); - // } - // return x; - // } - // - // private static long deInterleave32(long x) - // { - // x = (x | (x >>> 1)) & 0x3333333333333333L; - // x = (x | (x >>> 2)) & 0x0F0F0F0F0F0F0F0FL; - // x = (x | (x >>> 4)) & 0x00FF00FF00FF00FFL; - // x = (x | (x >>> 8)) & 0x0000FFFF0000FFFFL; - // x = (x | (x >>> 16)) & 0x00000000FFFFFFFFL; - // return x; - // } - private static int ReduceInPlace(long[] buf, int off, int len, int m, int[] ks) { int mLen = (m + 63) >> 6; @@ -1736,171 +1007,6 @@ private static void SquareInPlace(long[] x, int xLen, int m, int[] ks) } } - private static void Interleave(long[] x, int xOff, long[] z, int zOff, int count, int width) - { - switch (width) - { - case 3: - Interleave3(x, xOff, z, zOff, count); - break; - case 5: - Interleave5(x, xOff, z, zOff, count); - break; - case 7: - Interleave7(x, xOff, z, zOff, count); - break; - default: - Interleave2_n(x, xOff, z, zOff, count, BitLengths[width] - 1); - break; - } - } - - private static void Interleave3(long[] x, int xOff, long[] z, int zOff, int count) - { - for (int i = 0; i < count; ++i) - { - z[zOff + i] = Interleave3(x[xOff + i]); - } - } - - private static long Interleave3(long x) - { - long z = x & (1L << 63); - return z - | Interleave3_21to63((int)x & 0x1FFFFF) - | Interleave3_21to63((int)((ulong)x >> 21) & 0x1FFFFF) << 1 - | Interleave3_21to63((int)((ulong)x >> 42) & 0x1FFFFF) << 2; - - // int zPos = 0, wPos = 0, xPos = 0; - // for (;;) - // { - // z |= ((x >>> xPos) & 1L) << zPos; - // if (++zPos == 63) - // { - // String sz2 = Long.toBinaryString(z); - // return z; - // } - // if ((xPos += 21) >= 63) - // { - // xPos = ++wPos; - // } - // } - } - - private static long Interleave3_21to63(int x) - { - int r00 = INTERLEAVE3_TABLE[x & 0x7F]; - int r21 = INTERLEAVE3_TABLE[((uint)x >> 7) & 0x7F]; - int r42 = INTERLEAVE3_TABLE[(uint)x >> 14]; - return (r42 & 0xFFFFFFFFL) << 42 | (r21 & 0xFFFFFFFFL) << 21 | (r00 & 0xFFFFFFFFL); - } - - private static void Interleave5(long[] x, int xOff, long[] z, int zOff, int count) - { - for (int i = 0; i < count; ++i) - { - z[zOff + i] = Interleave5(x[xOff + i]); - } - } - - private static long Interleave5(long x) - { - return Interleave3_13to65((int)x & 0x1FFF) - | Interleave3_13to65((int)((ulong)x >> 13) & 0x1FFF) << 1 - | Interleave3_13to65((int)((ulong)x >> 26) & 0x1FFF) << 2 - | Interleave3_13to65((int)((ulong)x >> 39) & 0x1FFF) << 3 - | Interleave3_13to65((int)((ulong)x >> 52) & 0x1FFF) << 4; - - // long z = 0; - // int zPos = 0, wPos = 0, xPos = 0; - // for (;;) - // { - // z |= ((x >>> xPos) & 1L) << zPos; - // if (++zPos == 64) - // { - // return z; - // } - // if ((xPos += 13) >= 64) - // { - // xPos = ++wPos; - // } - // } - } - - private static long Interleave3_13to65(int x) - { - int r00 = INTERLEAVE5_TABLE[x & 0x7F]; - int r35 = INTERLEAVE5_TABLE[(uint)x >> 7]; - return (r35 & 0xFFFFFFFFL) << 35 | (r00 & 0xFFFFFFFFL); - } - - private static void Interleave7(long[] x, int xOff, long[] z, int zOff, int count) - { - for (int i = 0; i < count; ++i) - { - z[zOff + i] = Interleave7(x[xOff + i]); - } - } - - private static long Interleave7(long x) - { - long z = x & (1L << 63); - return z - | INTERLEAVE7_TABLE[(int)x & 0x1FF] - | INTERLEAVE7_TABLE[(int)((ulong)x >> 9) & 0x1FF] << 1 - | INTERLEAVE7_TABLE[(int)((ulong)x >> 18) & 0x1FF] << 2 - | INTERLEAVE7_TABLE[(int)((ulong)x >> 27) & 0x1FF] << 3 - | INTERLEAVE7_TABLE[(int)((ulong)x >> 36) & 0x1FF] << 4 - | INTERLEAVE7_TABLE[(int)((ulong)x >> 45) & 0x1FF] << 5 - | INTERLEAVE7_TABLE[(int)((ulong)x >> 54) & 0x1FF] << 6; - - // int zPos = 0, wPos = 0, xPos = 0; - // for (;;) - // { - // z |= ((x >>> xPos) & 1L) << zPos; - // if (++zPos == 63) - // { - // return z; - // } - // if ((xPos += 9) >= 63) - // { - // xPos = ++wPos; - // } - // } - } - - private static void Interleave2_n(long[] x, int xOff, long[] z, int zOff, int count, int rounds) - { - for (int i = 0; i < count; ++i) - { - z[zOff + i] = Interleave2_n(x[xOff + i], rounds); - } - } - - private static long Interleave2_n(long x, int rounds) - { - while (rounds > 1) - { - rounds -= 2; - x = Interleave4_16to64((int)x & 0xFFFF) - | Interleave4_16to64((int)((ulong)x >> 16) & 0xFFFF) << 1 - | Interleave4_16to64((int)((ulong)x >> 32) & 0xFFFF) << 2 - | Interleave4_16to64((int)((ulong)x >> 48) & 0xFFFF) << 3; - } - if (rounds > 0) - { - x = Interleave2_32to64((int)x) | Interleave2_32to64((int)((ulong)x >> 32)) << 1; - } - return x; - } - - private static long Interleave4_16to64(int x) - { - int r00 = INTERLEAVE4_TABLE[x & 0xFF]; - int r32 = INTERLEAVE4_TABLE[(uint)x >> 8]; - return (r32 & 0xFFFFFFFFL) << 32 | (r00 & 0xFFFFFFFFL); - } - private static long Interleave2_32to64(int x) { int r00 = INTERLEAVE2_TABLE[x & 0xFF] | INTERLEAVE2_TABLE[((uint)x >> 8) & 0xFF] << 16; @@ -1908,149 +1014,8 @@ private static long Interleave2_32to64(int x) return (r32 & 0xFFFFFFFFL) << 32 | (r00 & 0xFFFFFFFFL); } - // private static LongArray ExpItohTsujii2(LongArray B, int n, int m, int[] ks) - // { - // LongArray t1 = B, t3 = new LongArray(new long[]{ 1L }); - // int scale = 1; - // - // int numTerms = n; - // while (numTerms > 1) - // { - // if ((numTerms & 1) != 0) - // { - // t3 = t3.ModMultiply(t1, m, ks); - // t1 = t1.modSquareN(scale, m, ks); - // } - // - // LongArray t2 = t1.modSquareN(scale, m, ks); - // t1 = t1.ModMultiply(t2, m, ks); - // numTerms >>>= 1; scale <<= 1; - // } - // - // return t3.ModMultiply(t1, m, ks); - // } - // - // private static LongArray ExpItohTsujii23(LongArray B, int n, int m, int[] ks) - // { - // LongArray t1 = B, t3 = new LongArray(new long[]{ 1L }); - // int scale = 1; - // - // int numTerms = n; - // while (numTerms > 1) - // { - // bool m03 = numTerms % 3 == 0; - // bool m14 = !m03 && (numTerms & 1) != 0; - // - // if (m14) - // { - // t3 = t3.ModMultiply(t1, m, ks); - // t1 = t1.modSquareN(scale, m, ks); - // } - // - // LongArray t2 = t1.modSquareN(scale, m, ks); - // t1 = t1.ModMultiply(t2, m, ks); - // - // if (m03) - // { - // t2 = t2.modSquareN(scale, m, ks); - // t1 = t1.ModMultiply(t2, m, ks); - // numTerms /= 3; scale *= 3; - // } - // else - // { - // numTerms >>>= 1; scale <<= 1; - // } - // } - // - // return t3.ModMultiply(t1, m, ks); - // } - // - // private static LongArray ExpItohTsujii235(LongArray B, int n, int m, int[] ks) - // { - // LongArray t1 = B, t4 = new LongArray(new long[]{ 1L }); - // int scale = 1; - // - // int numTerms = n; - // while (numTerms > 1) - // { - // if (numTerms % 5 == 0) - // { - //// t1 = ExpItohTsujii23(t1, 5, m, ks); - // - // LongArray t3 = t1; - // t1 = t1.modSquareN(scale, m, ks); - // - // LongArray t2 = t1.modSquareN(scale, m, ks); - // t1 = t1.ModMultiply(t2, m, ks); - // t2 = t1.modSquareN(scale << 1, m, ks); - // t1 = t1.ModMultiply(t2, m, ks); - // - // t1 = t1.ModMultiply(t3, m, ks); - // - // numTerms /= 5; scale *= 5; - // continue; - // } - // - // bool m03 = numTerms % 3 == 0; - // bool m14 = !m03 && (numTerms & 1) != 0; - // - // if (m14) - // { - // t4 = t4.ModMultiply(t1, m, ks); - // t1 = t1.modSquareN(scale, m, ks); - // } - // - // LongArray t2 = t1.modSquareN(scale, m, ks); - // t1 = t1.ModMultiply(t2, m, ks); - // - // if (m03) - // { - // t2 = t2.modSquareN(scale, m, ks); - // t1 = t1.ModMultiply(t2, m, ks); - // numTerms /= 3; scale *= 3; - // } - // else - // { - // numTerms >>>= 1; scale <<= 1; - // } - // } - // - // return t4.ModMultiply(t1, m, ks); - // } - public LongArray ModInverse(int m, int[] ks) { - /* - * Fermat's Little Theorem - */ - // LongArray A = this; - // LongArray B = A.modSquare(m, ks); - // LongArray R0 = B, R1 = B; - // for (int i = 2; i < m; ++i) - // { - // R1 = R1.modSquare(m, ks); - // R0 = R0.ModMultiply(R1, m, ks); - // } - // - // return R0; - - /* - * Itoh-Tsujii - */ - // LongArray B = modSquare(m, ks); - // switch (m) - // { - // case 409: - // return ExpItohTsujii23(B, m - 1, m, ks); - // case 571: - // return ExpItohTsujii235(B, m - 1, m, ks); - // case 163: - // case 233: - // case 283: - // default: - // return ExpItohTsujii2(B, m - 1, m, ks); - // } - /* * Inversion in F2m using the extended Euclidean algorithm * diff --git a/src/Renci.SshNet/Security/BouncyCastle/math/ec/abc/SimpleBigDecimal.cs b/src/Renci.SshNet/Security/BouncyCastle/math/ec/abc/SimpleBigDecimal.cs index 23604d1ba..97b5c6a6e 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/math/ec/abc/SimpleBigDecimal.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/math/ec/abc/SimpleBigDecimal.cs @@ -16,27 +16,10 @@ namespace Renci.SshNet.Security.Org.BouncyCastle.Math.EC.Abc * SimpleBigDecimal with double scale. */ internal class SimpleBigDecimal - // : Number { - // private static final long serialVersionUID = 1L; - private readonly BigInteger bigInt; private readonly int scale; - /** - * Returns a SimpleBigDecimal representing the same numerical - * value as value. - * @param value The value of the SimpleBigDecimal to be - * created. - * @param scale The scale of the SimpleBigDecimal to be - * created. - * @return The such created SimpleBigDecimal. - */ - public static SimpleBigDecimal GetInstance(BigInteger val, int scale) - { - return new SimpleBigDecimal(val.ShiftLeft(scale), scale); - } - /** * Constructor for SimpleBigDecimal. The value of the * constructed SimpleBigDecimal Equals bigInt / @@ -53,12 +36,6 @@ public SimpleBigDecimal(BigInteger bigInt, int scale) this.scale = scale; } - private SimpleBigDecimal(SimpleBigDecimal limBigDec) - { - bigInt = limBigDec.bigInt; - scale = limBigDec.scale; - } - private void CheckScale(SimpleBigDecimal b) { if (scale != b.scale) @@ -82,11 +59,6 @@ public SimpleBigDecimal Add(SimpleBigDecimal b) return new SimpleBigDecimal(bigInt.Add(b.bigInt), scale); } - public SimpleBigDecimal Add(BigInteger b) - { - return new SimpleBigDecimal(bigInt.Add(b.ShiftLeft(scale)), scale); - } - public SimpleBigDecimal Negate() { return new SimpleBigDecimal(bigInt.Negate(), scale); @@ -102,40 +74,6 @@ public SimpleBigDecimal Subtract(BigInteger b) return new SimpleBigDecimal(bigInt.Subtract(b.ShiftLeft(scale)), scale); } - public SimpleBigDecimal Multiply(SimpleBigDecimal b) - { - CheckScale(b); - return new SimpleBigDecimal(bigInt.Multiply(b.bigInt), scale + scale); - } - - public SimpleBigDecimal Multiply(BigInteger b) - { - return new SimpleBigDecimal(bigInt.Multiply(b), scale); - } - - public SimpleBigDecimal Divide(SimpleBigDecimal b) - { - CheckScale(b); - BigInteger dividend = bigInt.ShiftLeft(scale); - return new SimpleBigDecimal(dividend.Divide(b.bigInt), scale); - } - - public SimpleBigDecimal Divide(BigInteger b) - { - return new SimpleBigDecimal(bigInt.Divide(b), scale); - } - - public SimpleBigDecimal ShiftLeft(int n) - { - return new SimpleBigDecimal(bigInt.ShiftLeft(n), scale); - } - - public int CompareTo(SimpleBigDecimal val) - { - CheckScale(val); - return bigInt.CompareTo(val.bigInt); - } - public int CompareTo(BigInteger val) { return bigInt.CompareTo(val.ShiftLeft(scale)); @@ -152,26 +90,6 @@ public BigInteger Round() return Add(oneHalf.AdjustScale(scale)).Floor(); } - public int IntValue - { - get { return Floor().IntValue; } - } - - public long LongValue - { - get { return Floor().LongValue; } - } - -// public double doubleValue() -// { -// return new Double(ToString()).doubleValue(); -// } -// -// public float floatValue() -// { -// return new Float(ToString()).floatValue(); -// } - public int Scale { get { return scale; } diff --git a/src/Renci.SshNet/Security/BouncyCastle/math/ec/abc/Tnaf.cs b/src/Renci.SshNet/Security/BouncyCastle/math/ec/abc/Tnaf.cs index 86e582f2a..2bfefe6ad 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/math/ec/abc/Tnaf.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/math/ec/abc/Tnaf.cs @@ -14,7 +14,6 @@ internal class Tnaf private static readonly BigInteger MinusOne = BigInteger.One.Negate(); private static readonly BigInteger MinusTwo = BigInteger.Two.Negate(); private static readonly BigInteger MinusThree = BigInteger.Three.Negate(); - private static readonly BigInteger Four = BigInteger.ValueOf(4); /** * The window width of WTNAF. The standard value of 4 is slightly less @@ -114,47 +113,6 @@ public static BigInteger Norm(sbyte mu, ZTauElement lambda) return norm; } - /** - * Computes the norm of an element λ of - * R[τ], where λ = u + vτ - * and u and u are real numbers (elements of - * R). - * @param mu The parameter μ of the elliptic curve. - * @param u The real part of the element λ of - * R[τ]. - * @param v The τ-adic part of the element - * λ of R[τ]. - * @return The norm of λ. - */ - public static SimpleBigDecimal Norm(sbyte mu, SimpleBigDecimal u, SimpleBigDecimal v) - { - SimpleBigDecimal norm; - - // s1 = u^2 - SimpleBigDecimal s1 = u.Multiply(u); - - // s2 = u * v - SimpleBigDecimal s2 = u.Multiply(v); - - // s3 = 2 * v^2 - SimpleBigDecimal s3 = v.Multiply(v).ShiftLeft(1); - - if (mu == 1) - { - norm = s1.Add(s2).Add(s3); - } - else if (mu == -1) - { - norm = s1.Subtract(s2).Add(s3); - } - else - { - throw new ArgumentException("mu must be 1 or -1"); - } - - return norm; - } - /** * Rounds an element λ of R[τ] * to an element of Z[τ], such that their difference @@ -303,131 +261,6 @@ public static SimpleBigDecimal ApproximateDivisionByN(BigInteger k, return new SimpleBigDecimal(ls, c); } - /** - * Computes the τ-adic NAF (non-adjacent form) of an - * element λ of Z[τ]. - * @param mu The parameter μ of the elliptic curve. - * @param lambda The element λ of - * Z[τ]. - * @return The τ-adic NAF of λ. - */ - public static sbyte[] TauAdicNaf(sbyte mu, ZTauElement lambda) - { - if (!((mu == 1) || (mu == -1))) - throw new ArgumentException("mu must be 1 or -1"); - - BigInteger norm = Norm(mu, lambda); - - // Ceiling of log2 of the norm - int log2Norm = norm.BitLength; - - // If length(TNAF) > 30, then length(TNAF) < log2Norm + 3.52 - int maxLength = log2Norm > 30 ? log2Norm + 4 : 34; - - // The array holding the TNAF - sbyte[] u = new sbyte[maxLength]; - int i = 0; - - // The actual length of the TNAF - int length = 0; - - BigInteger r0 = lambda.u; - BigInteger r1 = lambda.v; - - while(!((r0.Equals(BigInteger.Zero)) && (r1.Equals(BigInteger.Zero)))) - { - // If r0 is odd - if (r0.TestBit(0)) - { - u[i] = (sbyte) BigInteger.Two.Subtract((r0.Subtract(r1.ShiftLeft(1))).Mod(Four)).IntValue; - - // r0 = r0 - u[i] - if (u[i] == 1) - { - r0 = r0.ClearBit(0); - } - else - { - // u[i] == -1 - r0 = r0.Add(BigInteger.One); - } - length = i; - } - else - { - u[i] = 0; - } - - BigInteger t = r0; - BigInteger s = r0.ShiftRight(1); - if (mu == 1) - { - r0 = r1.Add(s); - } - else - { - // mu == -1 - r0 = r1.Subtract(s); - } - - r1 = t.ShiftRight(1).Negate(); - i++; - } - - length++; - - // Reduce the TNAF array to its actual length - sbyte[] tnaf = new sbyte[length]; - Array.Copy(u, 0, tnaf, 0, length); - return tnaf; - } - - /** - * Applies the operation τ() to an - * AbstractF2mPoint. - * @param p The AbstractF2mPoint to which τ() is applied. - * @return τ(p) - */ - public static AbstractF2mPoint Tau(AbstractF2mPoint p) - { - return p.Tau(); - } - - /** - * Returns the parameter μ of the elliptic curve. - * @param curve The elliptic curve from which to obtain μ. - * The curve must be a Koblitz curve, i.e. a Equals - * 0 or 1 and b Equals - * 1. - * @return μ of the elliptic curve. - * @throws ArgumentException if the given ECCurve is not a Koblitz - * curve. - */ - public static sbyte GetMu(AbstractF2mCurve curve) - { - BigInteger a = curve.A.ToBigInteger(); - - sbyte mu; - if (a.SignValue == 0) - { - mu = -1; - } - else if (a.Equals(BigInteger.One)) - { - mu = 1; - } - else - { - throw new ArgumentException("No Koblitz curve (ABC), TNAF multiplication not possible"); - } - return mu; - } - - public static sbyte GetMu(ECFieldElement curveA) - { - return (sbyte)(curveA.IsZero ? -1 : 1); - } - public static sbyte GetMu(int curveA) { return (sbyte)(curveA == 0 ? -1 : 1); @@ -560,24 +393,6 @@ public static BigInteger[] GetSi(AbstractF2mCurve curve) return new BigInteger[] { dividend0, dividend1 }; } - public static BigInteger[] GetSi(int fieldSize, int curveA, BigInteger cofactor) - { - sbyte mu = GetMu(curveA); - int shifts = GetShiftsForCofactor(cofactor); - int index = fieldSize + 3 - curveA; - BigInteger[] ui = GetLucas(mu, index, false); - if (mu == 1) - { - ui[0] = ui[0].Negate(); - ui[1] = ui[1].Negate(); - } - - BigInteger dividend0 = BigInteger.One.Add(ui[1]).ShiftRight(shifts); - BigInteger dividend1 = BigInteger.One.Add(ui[0]).ShiftRight(shifts).Negate(); - - return new BigInteger[] { dividend0, dividend1 }; - } - protected static int GetShiftsForCofactor(BigInteger h) { if (h != null && h.BitLength < 4) @@ -640,46 +455,6 @@ public static ZTauElement PartModReduction(BigInteger k, int m, sbyte a, return new ZTauElement(r0, r1); } - /** - * Multiplies a {@link org.bouncycastle.math.ec.AbstractF2mPoint AbstractF2mPoint} - * by a BigInteger using the reduced τ-adic - * NAF (RTNAF) method. - * @param p The AbstractF2mPoint to Multiply. - * @param k The BigInteger by which to Multiply p. - * @return k * p - */ - public static AbstractF2mPoint MultiplyRTnaf(AbstractF2mPoint p, BigInteger k) - { - AbstractF2mCurve curve = (AbstractF2mCurve)p.Curve; - int m = curve.FieldSize; - int a = curve.A.ToBigInteger().IntValue; - sbyte mu = GetMu(a); - BigInteger[] s = curve.GetSi(); - ZTauElement rho = PartModReduction(k, m, (sbyte)a, s, mu, (sbyte)10); - - return MultiplyTnaf(p, rho); - } - - /** - * Multiplies a {@link org.bouncycastle.math.ec.AbstractF2mPoint AbstractF2mPoint} - * by an element λ of Z[τ] - * using the τ-adic NAF (TNAF) method. - * @param p The AbstractF2mPoint to Multiply. - * @param lambda The element λ of - * Z[τ]. - * @return λ * p - */ - public static AbstractF2mPoint MultiplyTnaf(AbstractF2mPoint p, ZTauElement lambda) - { - AbstractF2mCurve curve = (AbstractF2mCurve)p.Curve; - sbyte mu = GetMu(curve.A); - sbyte[] u = TauAdicNaf(mu, lambda); - - AbstractF2mPoint q = MultiplyFromTnaf(p, u); - - return q; - } - /** * Multiplies a {@link org.bouncycastle.math.ec.AbstractF2mPoint AbstractF2mPoint} * by an element λ of Z[τ] diff --git a/src/Renci.SshNet/Security/BouncyCastle/math/ec/endo/ECEndomorphism.cs b/src/Renci.SshNet/Security/BouncyCastle/math/ec/endo/ECEndomorphism.cs deleted file mode 100644 index 8dbbace40..000000000 --- a/src/Renci.SshNet/Security/BouncyCastle/math/ec/endo/ECEndomorphism.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; - -namespace Renci.SshNet.Security.Org.BouncyCastle.Math.EC.Endo -{ - internal interface ECEndomorphism - { - ECPointMap PointMap { get; } - - bool HasEfficientPointMap { get; } - } -} diff --git a/src/Renci.SshNet/Security/BouncyCastle/math/ec/endo/GlvEndomorphism.cs b/src/Renci.SshNet/Security/BouncyCastle/math/ec/endo/GlvEndomorphism.cs deleted file mode 100644 index acfae5264..000000000 --- a/src/Renci.SshNet/Security/BouncyCastle/math/ec/endo/GlvEndomorphism.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; - -namespace Renci.SshNet.Security.Org.BouncyCastle.Math.EC.Endo -{ - internal interface GlvEndomorphism - : ECEndomorphism - { - BigInteger[] DecomposeScalar(BigInteger k); - } -} diff --git a/src/Renci.SshNet/Security/BouncyCastle/math/ec/multiplier/FixedPointUtilities.cs b/src/Renci.SshNet/Security/BouncyCastle/math/ec/multiplier/FixedPointUtilities.cs index 14dea10a7..4b7f4a347 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/math/ec/multiplier/FixedPointUtilities.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/math/ec/multiplier/FixedPointUtilities.cs @@ -12,11 +12,6 @@ public static int GetCombSize(ECCurve c) return order == null ? c.FieldSize + 1 : order.BitLength; } - public static FixedPointPreCompInfo GetFixedPointPreCompInfo(PreCompInfo preCompInfo) - { - return preCompInfo as FixedPointPreCompInfo; - } - public static FixedPointPreCompInfo Precompute(ECPoint p) { return (FixedPointPreCompInfo)p.Curve.Precompute(p, PRECOMP_NAME, new FixedPointCallback(p)); diff --git a/src/Renci.SshNet/Security/BouncyCastle/math/ec/multiplier/GlvMultiplier.cs b/src/Renci.SshNet/Security/BouncyCastle/math/ec/multiplier/GlvMultiplier.cs deleted file mode 100644 index e255b3ee8..000000000 --- a/src/Renci.SshNet/Security/BouncyCastle/math/ec/multiplier/GlvMultiplier.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; - -using Renci.SshNet.Security.Org.BouncyCastle.Math.EC.Endo; - -namespace Renci.SshNet.Security.Org.BouncyCastle.Math.EC.Multiplier -{ - internal class GlvMultiplier - : AbstractECMultiplier - { - protected readonly ECCurve curve; - protected readonly GlvEndomorphism glvEndomorphism; - - public GlvMultiplier(ECCurve curve, GlvEndomorphism glvEndomorphism) - { - if (curve == null || curve.Order == null) - throw new ArgumentException("Need curve with known group order", "curve"); - - this.curve = curve; - this.glvEndomorphism = glvEndomorphism; - } - - protected override ECPoint MultiplyPositive(ECPoint p, BigInteger k) - { - if (!curve.Equals(p.Curve)) - throw new InvalidOperationException(); - - BigInteger n = p.Curve.Order; - BigInteger[] ab = glvEndomorphism.DecomposeScalar(k.Mod(n)); - BigInteger a = ab[0], b = ab[1]; - - ECPointMap pointMap = glvEndomorphism.PointMap; - if (glvEndomorphism.HasEfficientPointMap) - { - return ECAlgorithms.ImplShamirsTrickWNaf(p, a, pointMap, b); - } - - return ECAlgorithms.ImplShamirsTrickWNaf(p, a, pointMap.Map(p), b); - } - } -} diff --git a/src/Renci.SshNet/Security/BouncyCastle/math/ec/multiplier/WNafUtilities.cs b/src/Renci.SshNet/Security/BouncyCastle/math/ec/multiplier/WNafUtilities.cs index 8dbe7ef73..e40ca959c 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/math/ec/multiplier/WNafUtilities.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/math/ec/multiplier/WNafUtilities.cs @@ -111,164 +111,6 @@ public static int[] GenerateCompactWindowNaf(int width, BigInteger k) return wnaf; } - public static byte[] GenerateJsf(BigInteger g, BigInteger h) - { - int digits = System.Math.Max(g.BitLength, h.BitLength) + 1; - byte[] jsf = new byte[digits]; - - BigInteger k0 = g, k1 = h; - int j = 0, d0 = 0, d1 = 0; - - int offset = 0; - while ((d0 | d1) != 0 || k0.BitLength > offset || k1.BitLength > offset) - { - int n0 = ((int)((uint)k0.IntValue >> offset) + d0) & 7; - int n1 = ((int)((uint)k1.IntValue >> offset) + d1) & 7; - - int u0 = n0 & 1; - if (u0 != 0) - { - u0 -= (n0 & 2); - if ((n0 + u0) == 4 && (n1 & 3) == 2) - { - u0 = -u0; - } - } - - int u1 = n1 & 1; - if (u1 != 0) - { - u1 -= (n1 & 2); - if ((n1 + u1) == 4 && (n0 & 3) == 2) - { - u1 = -u1; - } - } - - if ((d0 << 1) == 1 + u0) - { - d0 ^= 1; - } - if ((d1 << 1) == 1 + u1) - { - d1 ^= 1; - } - - if (++offset == 30) - { - offset = 0; - k0 = k0.ShiftRight(30); - k1 = k1.ShiftRight(30); - } - - jsf[j++] = (byte)((u0 << 4) | (u1 & 0xF)); - } - - // Reduce the JSF array to its actual length - if (jsf.Length > j) - { - jsf = Trim(jsf, j); - } - - return jsf; - } - - public static byte[] GenerateNaf(BigInteger k) - { - if (k.SignValue == 0) - return Arrays.EmptyBytes; - - BigInteger _3k = k.ShiftLeft(1).Add(k); - - int digits = _3k.BitLength - 1; - byte[] naf = new byte[digits]; - - BigInteger diff = _3k.Xor(k); - - for (int i = 1; i < digits; ++i) - { - if (diff.TestBit(i)) - { - naf[i - 1] = (byte)(k.TestBit(i) ? -1 : 1); - ++i; - } - } - - naf[digits - 1] = 1; - - return naf; - } - - /** - * Computes the Window NAF (non-adjacent Form) of an integer. - * @param width The width w of the Window NAF. The width is - * defined as the minimal number w, such that for any - * w consecutive digits in the resulting representation, at - * most one is non-zero. - * @param k The integer of which the Window NAF is computed. - * @return The Window NAF of the given width, such that the following holds: - * k = &sum;i=0l-1 ki2i - * , where the ki denote the elements of the - * returned byte[]. - */ - public static byte[] GenerateWindowNaf(int width, BigInteger k) - { - if (width == 2) - { - return GenerateNaf(k); - } - - if (width < 2 || width > 8) - throw new ArgumentException("must be in the range [2, 8]", "width"); - if (k.SignValue == 0) - return Arrays.EmptyBytes; - - byte[] wnaf = new byte[k.BitLength + 1]; - - // 2^width and a mask and sign bit set accordingly - int pow2 = 1 << width; - int mask = pow2 - 1; - int sign = pow2 >> 1; - - bool carry = false; - int length = 0, pos = 0; - - while (pos <= k.BitLength) - { - if (k.TestBit(pos) == carry) - { - ++pos; - continue; - } - - k = k.ShiftRight(pos); - - int digit = k.IntValue & mask; - if (carry) - { - ++digit; - } - - carry = (digit & sign) != 0; - if (carry) - { - digit -= pow2; - } - - length += (length > 0) ? pos - 1 : pos; - wnaf[length++] = (byte)digit; - pos = width; - } - - // Reduce the WNAF array to its actual length - if (wnaf.Length > length) - { - wnaf = Trim(wnaf, length); - } - - return wnaf; - } - public static int GetNafWeight(BigInteger k) { if (k.SignValue == 0) @@ -280,16 +122,6 @@ public static int GetNafWeight(BigInteger k) return diff.BitCount; } - public static WNafPreCompInfo GetWNafPreCompInfo(ECPoint p) - { - return GetWNafPreCompInfo(p.Curve.GetPreCompInfo(p, PRECOMP_NAME)); - } - - public static WNafPreCompInfo GetWNafPreCompInfo(PreCompInfo preCompInfo) - { - return preCompInfo as WNafPreCompInfo; - } - /** * Determine window width to use for a scalar multiplication of the given size. * @@ -321,29 +153,11 @@ public static int GetWindowSize(int bits, int[] windowSizeCutoffs) return w + 2; } - public static ECPoint MapPointWithPrecomp(ECPoint p, int width, bool includeNegated, - ECPointMap pointMap) - { - ECCurve c = p.Curve; - WNafPreCompInfo wnafPreCompP = Precompute(p, width, includeNegated); - - ECPoint q = pointMap.Map(p); - c.Precompute(q, PRECOMP_NAME, new MapPointCallback(wnafPreCompP, includeNegated, pointMap)); - return q; - } - public static WNafPreCompInfo Precompute(ECPoint p, int width, bool includeNegated) { return (WNafPreCompInfo)p.Curve.Precompute(p, PRECOMP_NAME, new WNafCallback(p, width, includeNegated)); } - private static byte[] Trim(byte[] a, int length) - { - byte[] result = new byte[length]; - Array.Copy(a, 0, result, 0, result.Length); - return result; - } - private static int[] Trim(int[] a, int length) { int[] result = new int[length]; diff --git a/src/Renci.SshNet/Security/BouncyCastle/math/raw/Mod.cs b/src/Renci.SshNet/Security/BouncyCastle/math/raw/Mod.cs index 1fa6b6da1..06dc99a5b 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/math/raw/Mod.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/math/raw/Mod.cs @@ -8,8 +8,6 @@ namespace Renci.SshNet.Security.Org.BouncyCastle.Math.Raw { internal abstract class Mod { - private static readonly SecureRandom RandomSource = new SecureRandom(); - public static void Invert(uint[] p, uint[] x, uint[] z) { int len = p.Length; @@ -76,50 +74,6 @@ public static void Invert(uint[] p, uint[] x, uint[] z) } } - public static uint[] Random(uint[] p) - { - int len = p.Length; - uint[] s = Nat.Create(len); - - uint m = p[len - 1]; - m |= m >> 1; - m |= m >> 2; - m |= m >> 4; - m |= m >> 8; - m |= m >> 16; - - do - { - byte[] bytes = new byte[len << 2]; - RandomSource.NextBytes(bytes); - Pack.BE_To_UInt32(bytes, 0, s); - s[len - 1] &= m; - } - while (Nat.Gte(len, s, p)); - - return s; - } - - public static void Add(uint[] p, uint[] x, uint[] y, uint[] z) - { - int len = p.Length; - uint c = Nat.Add(len, x, y, z); - if (c != 0) - { - Nat.SubFrom(len, p, z); - } - } - - public static void Subtract(uint[] p, uint[] x, uint[] y, uint[] z) - { - int len = p.Length; - int c = Nat.Sub(len, x, y, z); - if (c != 0) - { - Nat.AddTo(len, p, z); - } - } - private static void InversionResult(uint[] p, int ac, uint[] a, uint[] z) { if (ac < 0) diff --git a/src/Renci.SshNet/Security/BouncyCastle/math/raw/Nat.cs b/src/Renci.SshNet/Security/BouncyCastle/math/raw/Nat.cs index 710c2497a..d5194c516 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/math/raw/Nat.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/math/raw/Nat.cs @@ -21,122 +21,6 @@ public static uint Add(int len, uint[] x, uint[] y, uint[] z) return (uint)c; } - public static uint Add33At(int len, uint x, uint[] z, int zPos) - { - Debug.Assert(zPos <= (len - 2)); - ulong c = (ulong)z[zPos + 0] + x; - z[zPos + 0] = (uint)c; - c >>= 32; - c += (ulong)z[zPos + 1] + 1; - z[zPos + 1] = (uint)c; - c >>= 32; - return c == 0 ? 0 : IncAt(len, z, zPos + 2); - } - - public static uint Add33At(int len, uint x, uint[] z, int zOff, int zPos) - { - Debug.Assert(zPos <= (len - 2)); - ulong c = (ulong)z[zOff + zPos] + x; - z[zOff + zPos] = (uint)c; - c >>= 32; - c += (ulong)z[zOff + zPos + 1] + 1; - z[zOff + zPos + 1] = (uint)c; - c >>= 32; - return c == 0 ? 0 : IncAt(len, z, zOff, zPos + 2); - } - - public static uint Add33To(int len, uint x, uint[] z) - { - ulong c = (ulong)z[0] + x; - z[0] = (uint)c; - c >>= 32; - c += (ulong)z[1] + 1; - z[1] = (uint)c; - c >>= 32; - return c == 0 ? 0 : IncAt(len, z, 2); - } - - public static uint Add33To(int len, uint x, uint[] z, int zOff) - { - ulong c = (ulong)z[zOff + 0] + x; - z[zOff + 0] = (uint)c; - c >>= 32; - c += (ulong)z[zOff + 1] + 1; - z[zOff + 1] = (uint)c; - c >>= 32; - return c == 0 ? 0 : IncAt(len, z, zOff, 2); - } - - public static uint AddBothTo(int len, uint[] x, uint[] y, uint[] z) - { - ulong c = 0; - for (int i = 0; i < len; ++i) - { - c += (ulong)x[i] + y[i] + z[i]; - z[i] = (uint)c; - c >>= 32; - } - return (uint)c; - } - - public static uint AddBothTo(int len, uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff) - { - ulong c = 0; - for (int i = 0; i < len; ++i) - { - c += (ulong)x[xOff + i] + y[yOff + i] + z[zOff + i]; - z[zOff + i] = (uint)c; - c >>= 32; - } - return (uint)c; - } - - public static uint AddDWordAt(int len, ulong x, uint[] z, int zPos) - { - Debug.Assert(zPos <= (len - 2)); - ulong c = (ulong)z[zPos + 0] + (x & M); - z[zPos + 0] = (uint)c; - c >>= 32; - c += (ulong)z[zPos + 1] + (x >> 32); - z[zPos + 1] = (uint)c; - c >>= 32; - return c == 0 ? 0 : IncAt(len, z, zPos + 2); - } - - public static uint AddDWordAt(int len, ulong x, uint[] z, int zOff, int zPos) - { - Debug.Assert(zPos <= (len - 2)); - ulong c = (ulong)z[zOff + zPos] + (x & M); - z[zOff + zPos] = (uint)c; - c >>= 32; - c += (ulong)z[zOff + zPos + 1] + (x >> 32); - z[zOff + zPos + 1] = (uint)c; - c >>= 32; - return c == 0 ? 0 : IncAt(len, z, zOff, zPos + 2); - } - - public static uint AddDWordTo(int len, ulong x, uint[] z) - { - ulong c = (ulong)z[0] + (x & M); - z[0] = (uint)c; - c >>= 32; - c += (ulong)z[1] + (x >> 32); - z[1] = (uint)c; - c >>= 32; - return c == 0 ? 0 : IncAt(len, z, 2); - } - - public static uint AddDWordTo(int len, ulong x, uint[] z, int zOff) - { - ulong c = (ulong)z[zOff + 0] + (x & M); - z[zOff + 0] = (uint)c; - c >>= 32; - c += (ulong)z[zOff + 1] + (x >> 32); - z[zOff + 1] = (uint)c; - c >>= 32; - return c == 0 ? 0 : IncAt(len, z, zOff, 2); - } - public static uint AddTo(int len, uint[] x, uint[] z) { ulong c = 0; @@ -149,115 +33,6 @@ public static uint AddTo(int len, uint[] x, uint[] z) return (uint)c; } - public static uint AddTo(int len, uint[] x, int xOff, uint[] z, int zOff) - { - ulong c = 0; - for (int i = 0; i < len; ++i) - { - c += (ulong)x[xOff + i] + z[zOff + i]; - z[zOff + i] = (uint)c; - c >>= 32; - } - return (uint)c; - } - - public static uint AddWordAt(int len, uint x, uint[] z, int zPos) - { - Debug.Assert(zPos <= (len - 1)); - ulong c = (ulong)x + z[zPos]; - z[zPos] = (uint)c; - c >>= 32; - return c == 0 ? 0 : IncAt(len, z, zPos + 1); - } - - public static uint AddWordAt(int len, uint x, uint[] z, int zOff, int zPos) - { - Debug.Assert(zPos <= (len - 1)); - ulong c = (ulong)x + z[zOff + zPos]; - z[zOff + zPos] = (uint)c; - c >>= 32; - return c == 0 ? 0 : IncAt(len, z, zOff, zPos + 1); - } - - public static uint AddWordTo(int len, uint x, uint[] z) - { - ulong c = (ulong)x + z[0]; - z[0] = (uint)c; - c >>= 32; - return c == 0 ? 0 : IncAt(len, z, 1); - } - - public static uint AddWordTo(int len, uint x, uint[] z, int zOff) - { - ulong c = (ulong)x + z[zOff]; - z[zOff] = (uint)c; - c >>= 32; - return c == 0 ? 0 : IncAt(len, z, zOff, 1); - } - - public static uint CAdd(int len, int mask, uint[] x, uint[] y, uint[] z) - { - uint MASK = (uint)-(mask & 1); - - ulong c = 0; - for (int i = 0; i < len; ++i) - { - c += (ulong)x[i] + (y[i] & MASK); - z[i] = (uint)c; - c >>= 32; - } - return (uint)c; - } - - public static void CMov(int len, int mask, uint[] x, int xOff, uint[] z, int zOff) - { - uint MASK = (uint)-(mask & 1); - - for (int i = 0; i < len; ++i) - { - uint z_i = z[zOff + i], diff = z_i ^ x[xOff + i]; - z_i ^= (diff & MASK); - z[zOff + i] = z_i; - } - - //uint half = 0x55555555U, rest = half << (-(int)MASK); - - //for (int i = 0; i < len; ++i) - //{ - // uint z_i = z[zOff + i], diff = z_i ^ x[xOff + i]; - // z_i ^= (diff & half); - // z_i ^= (diff & rest); - // z[zOff + i] = z_i; - //} - } - - public static void CMov(int len, int mask, int[] x, int xOff, int[] z, int zOff) - { - mask = -(mask & 1); - - for (int i = 0; i < len; ++i) - { - int z_i = z[zOff + i], diff = z_i ^ x[xOff + i]; - z_i ^= (diff & mask); - z[zOff + i] = z_i; - } - - //int half = 0x55555555, rest = half << (-mask); - - //for (int i = 0; i < len; ++i) - //{ - // int z_i = z[zOff + i], diff = z_i ^ x[xOff + i]; - // z_i ^= (diff & half); - // z_i ^= (diff & rest); - // z[zOff + i] = z_i; - //} - } - - public static void Copy(int len, uint[] x, uint[] z) - { - Array.Copy(x, 0, z, 0, len); - } - public static uint[] Copy(int len, uint[] x) { uint[] z = new uint[len]; @@ -265,92 +40,11 @@ public static uint[] Copy(int len, uint[] x) return z; } - public static void Copy(int len, uint[] x, int xOff, uint[] z, int zOff) - { - Array.Copy(x, xOff, z, zOff, len); - } - public static uint[] Create(int len) { return new uint[len]; } - public static ulong[] Create64(int len) - { - return new ulong[len]; - } - - public static int Dec(int len, uint[] z) - { - for (int i = 0; i < len; ++i) - { - if (--z[i] != uint.MaxValue) - { - return 0; - } - } - return -1; - } - - public static int Dec(int len, uint[] x, uint[] z) - { - int i = 0; - while (i < len) - { - uint c = x[i] - 1; - z[i] = c; - ++i; - if (c != uint.MaxValue) - { - while (i < len) - { - z[i] = x[i]; - ++i; - } - return 0; - } - } - return -1; - } - - public static int DecAt(int len, uint[] z, int zPos) - { - Debug.Assert(zPos <= len); - for (int i = zPos; i < len; ++i) - { - if (--z[i] != uint.MaxValue) - { - return 0; - } - } - return -1; - } - - public static int DecAt(int len, uint[] z, int zOff, int zPos) - { - Debug.Assert(zPos <= len); - for (int i = zPos; i < len; ++i) - { - if (--z[zOff + i] != uint.MaxValue) - { - return 0; - } - } - return -1; - } - - public static bool Eq(int len, uint[] x, uint[] y) - { - for (int i = len - 1; i >= 0; --i) - { - if (x[i] != y[i]) - { - return false; - } - } - return true; - } - public static uint[] FromBigInteger(int bits, BigInteger x) { if (x.SignValue < 0 || x.BitLength > bits) @@ -367,21 +61,6 @@ public static uint[] FromBigInteger(int bits, BigInteger x) return z; } - public static uint GetBit(uint[] x, int bit) - { - if (bit == 0) - { - return x[0] & 1; - } - int w = bit >> 5; - if (w < 0 || w >= x.Length) - { - return 0; - } - int b = bit & 31; - return (x[w] >> b) & 1; - } - public static bool Gte(int len, uint[] x, uint[] y) { for (int i = len - 1; i >= 0; --i) @@ -395,65 +74,6 @@ public static bool Gte(int len, uint[] x, uint[] y) return true; } - public static uint Inc(int len, uint[] z) - { - for (int i = 0; i < len; ++i) - { - if (++z[i] != uint.MinValue) - { - return 0; - } - } - return 1; - } - - public static uint Inc(int len, uint[] x, uint[] z) - { - int i = 0; - while (i < len) - { - uint c = x[i] + 1; - z[i] = c; - ++i; - if (c != 0) - { - while (i < len) - { - z[i] = x[i]; - ++i; - } - return 0; - } - } - return 1; - } - - public static uint IncAt(int len, uint[] z, int zPos) - { - Debug.Assert(zPos <= len); - for (int i = zPos; i < len; ++i) - { - if (++z[i] != uint.MinValue) - { - return 0; - } - } - return 1; - } - - public static uint IncAt(int len, uint[] z, int zOff, int zPos) - { - Debug.Assert(zPos <= len); - for (int i = zPos; i < len; ++i) - { - if (++z[zOff + i] != uint.MinValue) - { - return 0; - } - } - return 1; - } - public static bool IsOne(int len, uint[] x) { if (x[0] != 1) @@ -486,668 +106,67 @@ public static bool IsZero(int len, uint[] x) return true; } - public static void Mul(int len, uint[] x, uint[] y, uint[] zz) - { - zz[len] = MulWord(len, x[0], y, zz); - - for (int i = 1; i < len; ++i) - { - zz[i + len] = MulWordAddTo(len, x[i], y, 0, zz, i); - } - } - - public static void Mul(int len, uint[] x, int xOff, uint[] y, int yOff, uint[] zz, int zzOff) + public static uint ShiftDownBit(int len, uint[] z, uint c) { - zz[zzOff + len] = MulWord(len, x[xOff], y, yOff, zz, zzOff); - - for (int i = 1; i < len; ++i) + int i = len; + while (--i >= 0) { - zz[zzOff + i + len] = MulWordAddTo(len, x[xOff + i], y, yOff, zz, zzOff + i); + uint next = z[i]; + z[i] = (next >> 1) | (c << 31); + c = next; } + return c << 31; } - public static void Mul(uint[] x, int xOff, int xLen, uint[] y, int yOff, int yLen, uint[] zz, int zzOff) + public static uint ShiftDownBits(int len, uint[] z, int bits, uint c) { - zz[zzOff + yLen] = MulWord(yLen, x[xOff], y, yOff, zz, zzOff); - - for (int i = 1; i < xLen; ++i) + Debug.Assert(bits > 0 && bits < 32); + int i = len; + while (--i >= 0) { - zz[zzOff + i + yLen] = MulWordAddTo(yLen, x[xOff + i], y, yOff, zz, zzOff + i); + uint next = z[i]; + z[i] = (next >> bits) | (c << -bits); + c = next; } + return c << -bits; } - public static uint MulAddTo(int len, uint[] x, uint[] y, uint[] zz) + public static uint ShiftDownWord(int len, uint[] z, uint c) { - ulong zc = 0; - for (int i = 0; i < len; ++i) + int i = len; + while (--i >= 0) { - ulong c = MulWordAddTo(len, x[i], y, 0, zz, i) & M; - c += zc + (zz[i + len] & M); - zz[i + len] = (uint)c; - zc = c >> 32; + uint next = z[i]; + z[i] = c; + c = next; } - return (uint)zc; + return c; } - public static uint MulAddTo(int len, uint[] x, int xOff, uint[] y, int yOff, uint[] zz, int zzOff) + public static int SubFrom(int len, uint[] x, uint[] z) { - ulong zc = 0; + long c = 0; for (int i = 0; i < len; ++i) { - ulong c = MulWordAddTo(len, x[xOff + i], y, yOff, zz, zzOff) & M; - c += zc + (zz[zzOff + len] & M); - zz[zzOff + len] = (uint)c; - zc = c >> 32; - ++zzOff; - } - return (uint)zc; - } - - public static uint Mul31BothAdd(int len, uint a, uint[] x, uint b, uint[] y, uint[] z, int zOff) - { - ulong c = 0, aVal = (ulong)a, bVal = (ulong)b; - int i = 0; - do - { - c += aVal * x[i] + bVal * y[i] + z[zOff + i]; - z[zOff + i] = (uint)c; - c >>= 32; - } - while (++i < len); - return (uint)c; - } - - public static uint MulWord(int len, uint x, uint[] y, uint[] z) - { - ulong c = 0, xVal = (ulong)x; - int i = 0; - do - { - c += xVal * y[i]; + c += (long)z[i] - x[i]; z[i] = (uint)c; c >>= 32; } - while (++i < len); - return (uint)c; - } - - public static uint MulWord(int len, uint x, uint[] y, int yOff, uint[] z, int zOff) - { - ulong c = 0, xVal = (ulong)x; - int i = 0; - do - { - c += xVal * y[yOff + i]; - z[zOff + i] = (uint)c; - c >>= 32; - } - while (++i < len); - return (uint)c; + return (int)c; } - public static uint MulWordAddTo(int len, uint x, uint[] y, int yOff, uint[] z, int zOff) + public static BigInteger ToBigInteger(int len, uint[] x) { - ulong c = 0, xVal = (ulong)x; - int i = 0; - do + byte[] bs = new byte[len << 2]; + for (int i = 0; i < len; ++i) { - c += xVal * y[yOff + i] + z[zOff + i]; - z[zOff + i] = (uint)c; - c >>= 32; - } - while (++i < len); - return (uint)c; - } - - public static uint MulWordDwordAddAt(int len, uint x, ulong y, uint[] z, int zPos) - { - Debug.Assert(zPos <= (len - 3)); - ulong c = 0, xVal = (ulong)x; - c += xVal * (uint)y + z[zPos + 0]; - z[zPos + 0] = (uint)c; - c >>= 32; - c += xVal * (y >> 32) + z[zPos + 1]; - z[zPos + 1] = (uint)c; - c >>= 32; - c += (ulong)z[zPos + 2]; - z[zPos + 2] = (uint)c; - c >>= 32; - return c == 0 ? 0 : IncAt(len, z, zPos + 3); - } - - public static uint ShiftDownBit(int len, uint[] z, uint c) - { - int i = len; - while (--i >= 0) - { - uint next = z[i]; - z[i] = (next >> 1) | (c << 31); - c = next; - } - return c << 31; - } - - public static uint ShiftDownBit(int len, uint[] z, int zOff, uint c) - { - int i = len; - while (--i >= 0) - { - uint next = z[zOff + i]; - z[zOff + i] = (next >> 1) | (c << 31); - c = next; - } - return c << 31; - } - - public static uint ShiftDownBit(int len, uint[] x, uint c, uint[] z) - { - int i = len; - while (--i >= 0) - { - uint next = x[i]; - z[i] = (next >> 1) | (c << 31); - c = next; - } - return c << 31; - } - - public static uint ShiftDownBit(int len, uint[] x, int xOff, uint c, uint[] z, int zOff) - { - int i = len; - while (--i >= 0) - { - uint next = x[xOff + i]; - z[zOff + i] = (next >> 1) | (c << 31); - c = next; - } - return c << 31; - } - - public static uint ShiftDownBits(int len, uint[] z, int bits, uint c) - { - Debug.Assert(bits > 0 && bits < 32); - int i = len; - while (--i >= 0) - { - uint next = z[i]; - z[i] = (next >> bits) | (c << -bits); - c = next; - } - return c << -bits; - } - - public static uint ShiftDownBits(int len, uint[] z, int zOff, int bits, uint c) - { - Debug.Assert(bits > 0 && bits < 32); - int i = len; - while (--i >= 0) - { - uint next = z[zOff + i]; - z[zOff + i] = (next >> bits) | (c << -bits); - c = next; - } - return c << -bits; - } - - public static uint ShiftDownBits(int len, uint[] x, int bits, uint c, uint[] z) - { - Debug.Assert(bits > 0 && bits < 32); - int i = len; - while (--i >= 0) - { - uint next = x[i]; - z[i] = (next >> bits) | (c << -bits); - c = next; - } - return c << -bits; - } - - public static uint ShiftDownBits(int len, uint[] x, int xOff, int bits, uint c, uint[] z, int zOff) - { - Debug.Assert(bits > 0 && bits < 32); - int i = len; - while (--i >= 0) - { - uint next = x[xOff + i]; - z[zOff + i] = (next >> bits) | (c << -bits); - c = next; - } - return c << -bits; - } - - public static uint ShiftDownWord(int len, uint[] z, uint c) - { - int i = len; - while (--i >= 0) - { - uint next = z[i]; - z[i] = c; - c = next; - } - return c; - } - - public static uint ShiftUpBit(int len, uint[] z, uint c) - { - for (int i = 0; i < len; ++i) - { - uint next = z[i]; - z[i] = (next << 1) | (c >> 31); - c = next; - } - return c >> 31; - } - - public static uint ShiftUpBit(int len, uint[] z, int zOff, uint c) - { - for (int i = 0; i < len; ++i) - { - uint next = z[zOff + i]; - z[zOff + i] = (next << 1) | (c >> 31); - c = next; - } - return c >> 31; - } - - public static uint ShiftUpBit(int len, uint[] x, uint c, uint[] z) - { - for (int i = 0; i < len; ++i) - { - uint next = x[i]; - z[i] = (next << 1) | (c >> 31); - c = next; - } - return c >> 31; - } - - public static uint ShiftUpBit(int len, uint[] x, int xOff, uint c, uint[] z, int zOff) - { - for (int i = 0; i < len; ++i) - { - uint next = x[xOff + i]; - z[zOff + i] = (next << 1) | (c >> 31); - c = next; - } - return c >> 31; - } - - public static ulong ShiftUpBit64(int len, ulong[] x, int xOff, ulong c, ulong[] z, int zOff) - { - for (int i = 0; i < len; ++i) - { - ulong next = x[xOff + i]; - z[zOff + i] = (next << 1) | (c >> 63); - c = next; - } - return c >> 63; - } - - public static uint ShiftUpBits(int len, uint[] z, int bits, uint c) - { - Debug.Assert(bits > 0 && bits < 32); - for (int i = 0; i < len; ++i) - { - uint next = z[i]; - z[i] = (next << bits) | (c >> -bits); - c = next; - } - return c >> -bits; - } - - public static uint ShiftUpBits(int len, uint[] z, int zOff, int bits, uint c) - { - Debug.Assert(bits > 0 && bits < 32); - for (int i = 0; i < len; ++i) - { - uint next = z[zOff + i]; - z[zOff + i] = (next << bits) | (c >> -bits); - c = next; - } - return c >> -bits; - } - - public static ulong ShiftUpBits64(int len, ulong[] z, int zOff, int bits, ulong c) - { - Debug.Assert(bits > 0 && bits < 64); - for (int i = 0; i < len; ++i) - { - ulong next = z[zOff + i]; - z[zOff + i] = (next << bits) | (c >> -bits); - c = next; - } - return c >> -bits; - } - - public static uint ShiftUpBits(int len, uint[] x, int bits, uint c, uint[] z) - { - Debug.Assert(bits > 0 && bits < 32); - for (int i = 0; i < len; ++i) - { - uint next = x[i]; - z[i] = (next << bits) | (c >> -bits); - c = next; - } - return c >> -bits; - } - - public static uint ShiftUpBits(int len, uint[] x, int xOff, int bits, uint c, uint[] z, int zOff) - { - Debug.Assert(bits > 0 && bits < 32); - for (int i = 0; i < len; ++i) - { - uint next = x[xOff + i]; - z[zOff + i] = (next << bits) | (c >> -bits); - c = next; - } - return c >> -bits; - } - - public static ulong ShiftUpBits64(int len, ulong[] x, int xOff, int bits, ulong c, ulong[] z, int zOff) - { - Debug.Assert(bits > 0 && bits < 64); - for (int i = 0; i < len; ++i) - { - ulong next = x[xOff + i]; - z[zOff + i] = (next << bits) | (c >> -bits); - c = next; - } - return c >> -bits; - } - - public static void Square(int len, uint[] x, uint[] zz) - { - int extLen = len << 1; - uint c = 0; - int j = len, k = extLen; - do - { - ulong xVal = (ulong)x[--j]; - ulong p = xVal * xVal; - zz[--k] = (c << 31) | (uint)(p >> 33); - zz[--k] = (uint)(p >> 1); - c = (uint)p; - } - while (j > 0); - - for (int i = 1; i < len; ++i) - { - c = SquareWordAdd(x, i, zz); - AddWordAt(extLen, c, zz, i << 1); - } - - ShiftUpBit(extLen, zz, x[0] << 31); - } - - public static void Square(int len, uint[] x, int xOff, uint[] zz, int zzOff) - { - int extLen = len << 1; - uint c = 0; - int j = len, k = extLen; - do - { - ulong xVal = (ulong)x[xOff + --j]; - ulong p = xVal * xVal; - zz[zzOff + --k] = (c << 31) | (uint)(p >> 33); - zz[zzOff + --k] = (uint)(p >> 1); - c = (uint)p; - } - while (j > 0); - - for (int i = 1; i < len; ++i) - { - c = SquareWordAdd(x, xOff, i, zz, zzOff); - AddWordAt(extLen, c, zz, zzOff, i << 1); - } - - ShiftUpBit(extLen, zz, zzOff, x[xOff] << 31); - } - - public static uint SquareWordAdd(uint[] x, int xPos, uint[] z) - { - ulong c = 0, xVal = (ulong)x[xPos]; - int i = 0; - do - { - c += xVal * x[i] + z[xPos + i]; - z[xPos + i] = (uint)c; - c >>= 32; - } - while (++i < xPos); - return (uint)c; - } - - public static uint SquareWordAdd(uint[] x, int xOff, int xPos, uint[] z, int zOff) - { - ulong c = 0, xVal = (ulong)x[xOff + xPos]; - int i = 0; - do - { - c += xVal * (x[xOff + i] & M) + (z[xPos + zOff] & M); - z[xPos + zOff] = (uint)c; - c >>= 32; - ++zOff; - } - while (++i < xPos); - return (uint)c; - } - - public static int Sub(int len, uint[] x, uint[] y, uint[] z) - { - long c = 0; - for (int i = 0; i < len; ++i) - { - c += (long)x[i] - y[i]; - z[i] = (uint)c; - c >>= 32; - } - return (int)c; - } - - public static int Sub(int len, uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff) - { - long c = 0; - for (int i = 0; i < len; ++i) - { - c += (long)x[xOff + i] - y[yOff + i]; - z[zOff + i] = (uint)c; - c >>= 32; - } - return (int)c; - } - public static int Sub33At(int len, uint x, uint[] z, int zPos) - { - Debug.Assert(zPos <= (len - 2)); - long c = (long)z[zPos + 0] - x; - z[zPos + 0] = (uint)c; - c >>= 32; - c += (long)z[zPos + 1] - 1; - z[zPos + 1] = (uint)c; - c >>= 32; - return c == 0 ? 0 : DecAt(len, z, zPos + 2); - } - - public static int Sub33At(int len, uint x, uint[] z, int zOff, int zPos) - { - Debug.Assert(zPos <= (len - 2)); - long c = (long)z[zOff + zPos] - x; - z[zOff + zPos] = (uint)c; - c >>= 32; - c += (long)z[zOff + zPos + 1] - 1; - z[zOff + zPos + 1] = (uint)c; - c >>= 32; - return c == 0 ? 0 : DecAt(len, z, zOff, zPos + 2); - } - - public static int Sub33From(int len, uint x, uint[] z) - { - long c = (long)z[0] - x; - z[0] = (uint)c; - c >>= 32; - c += (long)z[1] - 1; - z[1] = (uint)c; - c >>= 32; - return c == 0 ? 0 : DecAt(len, z, 2); - } - - public static int Sub33From(int len, uint x, uint[] z, int zOff) - { - long c = (long)z[zOff + 0] - x; - z[zOff + 0] = (uint)c; - c >>= 32; - c += (long)z[zOff + 1] - 1; - z[zOff + 1] = (uint)c; - c >>= 32; - return c == 0 ? 0 : DecAt(len, z, zOff, 2); - } - - public static int SubBothFrom(int len, uint[] x, uint[] y, uint[] z) - { - long c = 0; - for (int i = 0; i < len; ++i) - { - c += (long)z[i] - x[i] - y[i]; - z[i] = (uint)c; - c >>= 32; - } - return (int)c; - } - - public static int SubBothFrom(int len, uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff) - { - long c = 0; - for (int i = 0; i < len; ++i) - { - c += (long)z[zOff + i] - x[xOff + i] - y[yOff + i]; - z[zOff + i] = (uint)c; - c >>= 32; - } - return (int)c; - } - - public static int SubDWordAt(int len, ulong x, uint[] z, int zPos) - { - Debug.Assert(zPos <= (len - 2)); - long c = (long)z[zPos + 0] - (long)(x & M); - z[zPos + 0] = (uint)c; - c >>= 32; - c += (long)z[zPos + 1] - (long)(x >> 32); - z[zPos + 1] = (uint)c; - c >>= 32; - return c == 0 ? 0 : DecAt(len, z, zPos + 2); - } - - public static int SubDWordAt(int len, ulong x, uint[] z, int zOff, int zPos) - { - Debug.Assert(zPos <= (len - 2)); - long c = (long)z[zOff + zPos] - (long)(x & M); - z[zOff + zPos] = (uint)c; - c >>= 32; - c += (long)z[zOff + zPos + 1] - (long)(x >> 32); - z[zOff + zPos + 1] = (uint)c; - c >>= 32; - return c == 0 ? 0 : DecAt(len, z, zOff, zPos + 2); - } - - public static int SubDWordFrom(int len, ulong x, uint[] z) - { - long c = (long)z[0] - (long)(x & M); - z[0] = (uint)c; - c >>= 32; - c += (long)z[1] - (long)(x >> 32); - z[1] = (uint)c; - c >>= 32; - return c == 0 ? 0 : DecAt(len, z, 2); - } - - public static int SubDWordFrom(int len, ulong x, uint[] z, int zOff) - { - long c = (long)z[zOff + 0] - (long)(x & M); - z[zOff + 0] = (uint)c; - c >>= 32; - c += (long)z[zOff + 1] - (long)(x >> 32); - z[zOff + 1] = (uint)c; - c >>= 32; - return c == 0 ? 0 : DecAt(len, z, zOff, 2); - } - - public static int SubFrom(int len, uint[] x, uint[] z) - { - long c = 0; - for (int i = 0; i < len; ++i) - { - c += (long)z[i] - x[i]; - z[i] = (uint)c; - c >>= 32; - } - return (int)c; - } - - public static int SubFrom(int len, uint[] x, int xOff, uint[] z, int zOff) - { - long c = 0; - for (int i = 0; i < len; ++i) - { - c += (long)z[zOff + i] - x[xOff + i]; - z[zOff + i] = (uint)c; - c >>= 32; - } - return (int)c; - } - - public static int SubWordAt(int len, uint x, uint[] z, int zPos) - { - Debug.Assert(zPos <= (len - 1)); - long c = (long)z[zPos] - x; - z[zPos] = (uint)c; - c >>= 32; - return c == 0 ? 0 : DecAt(len, z, zPos + 1); - } - - public static int SubWordAt(int len, uint x, uint[] z, int zOff, int zPos) - { - Debug.Assert(zPos <= (len - 1)); - long c = (long)z[zOff + zPos] - x; - z[zOff + zPos] = (uint)c; - c >>= 32; - return c == 0 ? 0 : DecAt(len, z, zOff, zPos + 1); - } - - public static int SubWordFrom(int len, uint x, uint[] z) - { - long c = (long)z[0] - x; - z[0] = (uint)c; - c >>= 32; - return c == 0 ? 0 : DecAt(len, z, 1); - } - - public static int SubWordFrom(int len, uint x, uint[] z, int zOff) - { - long c = (long)z[zOff + 0] - x; - z[zOff + 0] = (uint)c; - c >>= 32; - return c == 0 ? 0 : DecAt(len, z, zOff, 1); - } - - public static BigInteger ToBigInteger(int len, uint[] x) - { - byte[] bs = new byte[len << 2]; - for (int i = 0; i < len; ++i) - { - uint x_i = x[i]; - if (x_i != 0) - { - Pack.UInt32_To_BE(x_i, bs, (len - 1 - i) << 2); - } + uint x_i = x[i]; + if (x_i != 0) + { + Pack.UInt32_To_BE(x_i, bs, (len - 1 - i) << 2); + } } return new BigInteger(1, bs); } - - public static void Zero(int len, uint[] z) - { - for (int i = 0; i < len; ++i) - { - z[i] = 0; - } - } } } diff --git a/src/Renci.SshNet/Security/BouncyCastle/security/SecureRandom.cs b/src/Renci.SshNet/Security/BouncyCastle/security/SecureRandom.cs index 377817e01..922270700 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/security/SecureRandom.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/security/SecureRandom.cs @@ -63,21 +63,6 @@ public SecureRandom(IRandomGenerator generator) this.generator = generator; } - public virtual byte[] GenerateSeed(int length) - { - return GetNextBytes(Master, length); - } - - public virtual void SetSeed(byte[] seed) - { - generator.AddSeedMaterial(seed); - } - - public virtual void SetSeed(long seed) - { - generator.AddSeedMaterial(seed); - } - public override int Next() { return NextInt() & int.MaxValue; @@ -142,11 +127,6 @@ public override void NextBytes(byte[] buf) generator.NextBytes(buf); } - public virtual void NextBytes(byte[] buf, int off, int len) - { - generator.NextBytes(buf, off, len); - } - private static readonly double DoubleScale = System.Math.Pow(2.0, 64.0); public override double NextDouble() diff --git a/src/Renci.SshNet/Security/BouncyCastle/security/SecurityUtilityException.cs b/src/Renci.SshNet/Security/BouncyCastle/security/SecurityUtilityException.cs deleted file mode 100644 index 25a442e48..000000000 --- a/src/Renci.SshNet/Security/BouncyCastle/security/SecurityUtilityException.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; - -namespace Renci.SshNet.Security.Org.BouncyCastle.Security -{ -#if FEATURE_BINARY_SERIALIZATION - [Serializable] -#endif - internal class SecurityUtilityException - : Exception - { - /** - * base constructor. - */ - public SecurityUtilityException() - { - } - - /** - * create a SecurityUtilityException with the given message. - * - * @param message the message to be carried with the exception. - */ - public SecurityUtilityException( - string message) - : base(message) - { - } - - public SecurityUtilityException( - string message, - Exception exception) - : base(message, exception) - { - } - } -} diff --git a/src/Renci.SshNet/Security/BouncyCastle/util/Arrays.cs b/src/Renci.SshNet/Security/BouncyCastle/util/Arrays.cs index 3e8947548..30c67ffd5 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/util/Arrays.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/util/Arrays.cs @@ -11,92 +11,6 @@ internal abstract class Arrays public static readonly byte[] EmptyBytes = new byte[0]; public static readonly int[] EmptyInts = new int[0]; - public static bool AreAllZeroes(byte[] buf, int off, int len) - { - uint bits = 0; - for (int i = 0; i < len; ++i) - { - bits |= buf[off + i]; - } - return bits == 0; - } - - public static bool AreEqual( - bool[] a, - bool[] b) - { - if (a == b) - return true; - - if (a == null || b == null) - return false; - - return HaveSameContents(a, b); - } - - public static bool AreEqual( - char[] a, - char[] b) - { - if (a == b) - return true; - - if (a == null || b == null) - return false; - - return HaveSameContents(a, b); - } - - /// - /// Are two arrays equal. - /// - /// Left side. - /// Right side. - /// True if equal. - public static bool AreEqual( - byte[] a, - byte[] b) - { - if (a == b) - return true; - - if (a == null || b == null) - return false; - - return HaveSameContents(a, b); - } - - [Obsolete("Use 'AreEqual' method instead")] - public static bool AreSame( - byte[] a, - byte[] b) - { - return AreEqual(a, b); - } - - /// - /// A constant time equals comparison - does not terminate early if - /// test will fail. - /// - /// first array - /// second array - /// true if arrays equal, false otherwise. - public static bool ConstantTimeAreEqual( - byte[] a, - byte[] b) - { - int i = a.Length; - if (i != b.Length) - return false; - int cmp = 0; - while (i != 0) - { - --i; - cmp |= (a[i] ^ b[i]); - } - return cmp == 0; - } - public static bool AreEqual( int[] a, int[] b) @@ -110,65 +24,6 @@ public static bool AreEqual( return HaveSameContents(a, b); } - public static bool AreEqual(uint[] a, uint[] b) - { - if (a == b) - return true; - - if (a == null || b == null) - return false; - - return HaveSameContents(a, b); - } - - private static bool HaveSameContents( - bool[] a, - bool[] b) - { - int i = a.Length; - if (i != b.Length) - return false; - while (i != 0) - { - --i; - if (a[i] != b[i]) - return false; - } - return true; - } - - private static bool HaveSameContents( - char[] a, - char[] b) - { - int i = a.Length; - if (i != b.Length) - return false; - while (i != 0) - { - --i; - if (a[i] != b[i]) - return false; - } - return true; - } - - private static bool HaveSameContents( - byte[] a, - byte[] b) - { - int i = a.Length; - if (i != b.Length) - return false; - while (i != 0) - { - --i; - if (a[i] != b[i]) - return false; - } - return true; - } - private static bool HaveSameContents( int[] a, int[] b) @@ -185,74 +40,6 @@ private static bool HaveSameContents( return true; } - private static bool HaveSameContents(uint[] a, uint[] b) - { - int i = a.Length; - if (i != b.Length) - return false; - while (i != 0) - { - --i; - if (a[i] != b[i]) - return false; - } - return true; - } - - public static string ToString( - object[] a) - { - StringBuilder sb = new StringBuilder('['); - if (a.Length > 0) - { - sb.Append(a[0]); - for (int index = 1; index < a.Length; ++index) - { - sb.Append(", ").Append(a[index]); - } - } - sb.Append(']'); - return sb.ToString(); - } - - public static int GetHashCode(byte[] data) - { - if (data == null) - { - return 0; - } - - int i = data.Length; - int hc = i + 1; - - while (--i >= 0) - { - hc *= 257; - hc ^= data[i]; - } - - return hc; - } - - public static int GetHashCode(byte[] data, int off, int len) - { - if (data == null) - { - return 0; - } - - int i = len; - int hc = i + 1; - - while (--i >= 0) - { - hc *= 257; - hc ^= data[off + i]; - } - - return hc; - } - public static int GetHashCode(int[] data) { if (data == null) @@ -270,187 +57,23 @@ public static int GetHashCode(int[] data) return hc; } - public static int GetHashCode(int[] data, int off, int len) - { - if (data == null) - return 0; - - int i = len; - int hc = i + 1; - - while (--i >= 0) - { - hc *= 257; - hc ^= data[off + i]; - } - - return hc; - } - - public static int GetHashCode(uint[] data) - { - if (data == null) - return 0; - - int i = data.Length; - int hc = i + 1; - - while (--i >= 0) - { - hc *= 257; - hc ^= (int)data[i]; - } - - return hc; - } - - public static int GetHashCode(uint[] data, int off, int len) - { - if (data == null) - return 0; - - int i = len; - int hc = i + 1; - - while (--i >= 0) - { - hc *= 257; - hc ^= (int)data[off + i]; - } - - return hc; - } - - public static int GetHashCode(ulong[] data) - { - if (data == null) - return 0; - - int i = data.Length; - int hc = i + 1; - - while (--i >= 0) - { - ulong di = data[i]; - hc *= 257; - hc ^= (int)di; - hc *= 257; - hc ^= (int)(di >> 32); - } - - return hc; - } - - public static int GetHashCode(ulong[] data, int off, int len) - { - if (data == null) - return 0; - - int i = len; - int hc = i + 1; - - while (--i >= 0) - { - ulong di = data[off + i]; - hc *= 257; - hc ^= (int)di; - hc *= 257; - hc ^= (int)(di >> 32); - } - - return hc; - } - public static byte[] Clone( byte[] data) { return data == null ? null : (byte[])data.Clone(); } - public static byte[] Clone( - byte[] data, - byte[] existing) - { - if (data == null) - { - return null; - } - if ((existing == null) || (existing.Length != data.Length)) - { - return Clone(data); - } - Array.Copy(data, 0, existing, 0, existing.Length); - return existing; - } - public static int[] Clone( int[] data) { return data == null ? null : (int[])data.Clone(); } - internal static uint[] Clone(uint[] data) - { - return data == null ? null : (uint[])data.Clone(); - } - public static long[] Clone(long[] data) { return data == null ? null : (long[])data.Clone(); } - public static ulong[] Clone( - ulong[] data) - { - return data == null ? null : (ulong[]) data.Clone(); - } - - public static ulong[] Clone( - ulong[] data, - ulong[] existing) - { - if (data == null) - { - return null; - } - if ((existing == null) || (existing.Length != data.Length)) - { - return Clone(data); - } - Array.Copy(data, 0, existing, 0, existing.Length); - return existing; - } - - public static bool Contains(byte[] a, byte n) - { - for (int i = 0; i < a.Length; ++i) - { - if (a[i] == n) - return true; - } - return false; - } - - public static bool Contains(short[] a, short n) - { - for (int i = 0; i < a.Length; ++i) - { - if (a[i] == n) - return true; - } - return false; - } - - public static bool Contains(int[] a, int n) - { - for (int i = 0; i < a.Length; ++i) - { - if (a[i] == n) - return true; - } - return false; - } - public static void Fill( byte[] buf, byte b) @@ -461,258 +84,5 @@ public static void Fill( buf[--i] = b; } } - - public static void Fill(byte[] buf, int from, int to, byte b) - { - for (int i = from; i < to; ++i) - { - buf[i] = b; - } - } - - public static byte[] CopyOf(byte[] data, int newLength) - { - byte[] tmp = new byte[newLength]; - Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length)); - return tmp; - } - - public static char[] CopyOf(char[] data, int newLength) - { - char[] tmp = new char[newLength]; - Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length)); - return tmp; - } - - public static int[] CopyOf(int[] data, int newLength) - { - int[] tmp = new int[newLength]; - Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length)); - return tmp; - } - - public static long[] CopyOf(long[] data, int newLength) - { - long[] tmp = new long[newLength]; - Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length)); - return tmp; - } - - public static BigInteger[] CopyOf(BigInteger[] data, int newLength) - { - BigInteger[] tmp = new BigInteger[newLength]; - Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length)); - return tmp; - } - - /** - * Make a copy of a range of bytes from the passed in data array. The range can - * extend beyond the end of the input array, in which case the return array will - * be padded with zeroes. - * - * @param data the array from which the data is to be copied. - * @param from the start index at which the copying should take place. - * @param to the final index of the range (exclusive). - * - * @return a new byte array containing the range given. - */ - public static byte[] CopyOfRange(byte[] data, int from, int to) - { - int newLength = GetLength(from, to); - byte[] tmp = new byte[newLength]; - Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from)); - return tmp; - } - - public static int[] CopyOfRange(int[] data, int from, int to) - { - int newLength = GetLength(from, to); - int[] tmp = new int[newLength]; - Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from)); - return tmp; - } - - public static long[] CopyOfRange(long[] data, int from, int to) - { - int newLength = GetLength(from, to); - long[] tmp = new long[newLength]; - Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from)); - return tmp; - } - - public static BigInteger[] CopyOfRange(BigInteger[] data, int from, int to) - { - int newLength = GetLength(from, to); - BigInteger[] tmp = new BigInteger[newLength]; - Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from)); - return tmp; - } - - private static int GetLength(int from, int to) - { - int newLength = to - from; - if (newLength < 0) - throw new ArgumentException(from + " > " + to); - return newLength; - } - - public static byte[] Append(byte[] a, byte b) - { - if (a == null) - return new byte[] { b }; - - int length = a.Length; - byte[] result = new byte[length + 1]; - Array.Copy(a, 0, result, 0, length); - result[length] = b; - return result; - } - - public static short[] Append(short[] a, short b) - { - if (a == null) - return new short[] { b }; - - int length = a.Length; - short[] result = new short[length + 1]; - Array.Copy(a, 0, result, 0, length); - result[length] = b; - return result; - } - - public static int[] Append(int[] a, int b) - { - if (a == null) - return new int[] { b }; - - int length = a.Length; - int[] result = new int[length + 1]; - Array.Copy(a, 0, result, 0, length); - result[length] = b; - return result; - } - - public static byte[] Concatenate(byte[] a, byte[] b) - { - if (a == null) - return Clone(b); - if (b == null) - return Clone(a); - - byte[] rv = new byte[a.Length + b.Length]; - Array.Copy(a, 0, rv, 0, a.Length); - Array.Copy(b, 0, rv, a.Length, b.Length); - return rv; - } - - public static byte[] ConcatenateAll(params byte[][] vs) - { - byte[][] nonNull = new byte[vs.Length][]; - int count = 0; - int totalLength = 0; - - for (int i = 0; i < vs.Length; ++i) - { - byte[] v = vs[i]; - if (v != null) - { - nonNull[count++] = v; - totalLength += v.Length; - } - } - - byte[] result = new byte[totalLength]; - int pos = 0; - - for (int j = 0; j < count; ++j) - { - byte[] v = nonNull[j]; - Array.Copy(v, 0, result, pos, v.Length); - pos += v.Length; - } - - return result; - } - - public static int[] Concatenate(int[] a, int[] b) - { - if (a == null) - return Clone(b); - if (b == null) - return Clone(a); - - int[] rv = new int[a.Length + b.Length]; - Array.Copy(a, 0, rv, 0, a.Length); - Array.Copy(b, 0, rv, a.Length, b.Length); - return rv; - } - - public static byte[] Prepend(byte[] a, byte b) - { - if (a == null) - return new byte[] { b }; - - int length = a.Length; - byte[] result = new byte[length + 1]; - Array.Copy(a, 0, result, 1, length); - result[0] = b; - return result; - } - - public static short[] Prepend(short[] a, short b) - { - if (a == null) - return new short[] { b }; - - int length = a.Length; - short[] result = new short[length + 1]; - Array.Copy(a, 0, result, 1, length); - result[0] = b; - return result; - } - - public static int[] Prepend(int[] a, int b) - { - if (a == null) - return new int[] { b }; - - int length = a.Length; - int[] result = new int[length + 1]; - Array.Copy(a, 0, result, 1, length); - result[0] = b; - return result; - } - - public static byte[] Reverse(byte[] a) - { - if (a == null) - return null; - - int p1 = 0, p2 = a.Length; - byte[] result = new byte[p2]; - - while (--p2 >= 0) - { - result[p2] = a[p1++]; - } - - return result; - } - - public static int[] Reverse(int[] a) - { - if (a == null) - return null; - - int p1 = 0, p2 = a.Length; - int[] result = new int[p2]; - - while (--p2 >= 0) - { - result[p2] = a[p1++]; - } - - return result; - } } } diff --git a/src/Renci.SshNet/Security/BouncyCastle/util/BigIntegers.cs b/src/Renci.SshNet/Security/BouncyCastle/util/BigIntegers.cs index 6e1b8c855..380fd5021 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/util/BigIntegers.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/util/BigIntegers.cs @@ -10,20 +10,6 @@ namespace Renci.SshNet.Security.Org.BouncyCastle.Utilities */ internal abstract class BigIntegers { - private const int MaxIterations = 1000; - - /** - * Return the passed in value as an unsigned byte array. - * - * @param value value to be converted. - * @return a byte array without a leading zero byte if present in the signed encoding. - */ - public static byte[] AsUnsignedByteArray( - BigInteger n) - { - return n.ToByteArrayUnsigned(); - } - /** * Return the passed in value as an unsigned byte array of specified length, zero-extended as necessary. * @@ -45,51 +31,5 @@ public static byte[] AsUnsignedByteArray(int length, BigInteger n) Array.Copy(bytes, 0, tmp, tmp.Length - bytes.Length, bytes.Length); return tmp; } - - /** - * Return a random BigInteger not less than 'min' and not greater than 'max' - * - * @param min the least value that may be generated - * @param max the greatest value that may be generated - * @param random the source of randomness - * @return a random BigInteger value in the range [min,max] - */ - public static BigInteger CreateRandomInRange( - BigInteger min, - BigInteger max, - // TODO Should have been just Random class - SecureRandom random) - { - int cmp = min.CompareTo(max); - if (cmp >= 0) - { - if (cmp > 0) - throw new ArgumentException("'min' may not be greater than 'max'"); - - return min; - } - - if (min.BitLength > max.BitLength / 2) - { - return CreateRandomInRange(BigInteger.Zero, max.Subtract(min), random).Add(min); - } - - for (int i = 0; i < MaxIterations; ++i) - { - BigInteger x = new BigInteger(max.BitLength, random); - if (x.CompareTo(min) >= 0 && x.CompareTo(max) <= 0) - { - return x; - } - } - - // fall back to a faster (restricted) method - return new BigInteger(max.Subtract(min).BitLength - 1, random).Add(min); - } - - public static int GetUnsignedByteLength(BigInteger n) - { - return (n.BitLength + 7) / 8; - } } } diff --git a/src/Renci.SshNet/Security/BouncyCastle/util/IMemoable.cs b/src/Renci.SshNet/Security/BouncyCastle/util/IMemoable.cs deleted file mode 100644 index 9f94275f4..000000000 --- a/src/Renci.SshNet/Security/BouncyCastle/util/IMemoable.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; - -namespace Renci.SshNet.Security.Org.BouncyCastle.Utilities -{ - internal interface IMemoable - { - /// - /// Produce a copy of this object with its configuration and in its current state. - /// - /// - /// The returned object may be used simply to store the state, or may be used as a similar object - /// starting from the copied state. - /// - IMemoable Copy(); - - /// - /// Restore a copied object state into this object. - /// - /// - /// Implementations of this method should try to avoid or minimise memory allocation to perform the reset. - /// - /// an object originally {@link #copy() copied} from an object of the same type as this instance. - /// if the provided object is not of the correct type. - /// if the other parameter is in some other way invalid. - void Reset(IMemoable other); - } - -} - diff --git a/src/Renci.SshNet/Security/BouncyCastle/util/Integers.cs b/src/Renci.SshNet/Security/BouncyCastle/util/Integers.cs index 29a858cc5..974ac82dc 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/util/Integers.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/util/Integers.cs @@ -8,20 +8,5 @@ public static int RotateLeft(int i, int distance) { return (i << distance) ^ (int)((uint)i >> -distance); } - - public static uint RotateLeft(uint i, int distance) - { - return (i << distance) ^ (i >> -distance); - } - - public static int RotateRight(int i, int distance) - { - return (int)((uint)i >> distance) ^ (i << -distance); - } - - public static uint RotateRight(uint i, int distance) - { - return (i >> distance) ^ (i << -distance); - } } } diff --git a/src/Renci.SshNet/Security/BouncyCastle/util/MemoableResetException.cs b/src/Renci.SshNet/Security/BouncyCastle/util/MemoableResetException.cs deleted file mode 100644 index 3b5ac38c5..000000000 --- a/src/Renci.SshNet/Security/BouncyCastle/util/MemoableResetException.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; - -namespace Renci.SshNet.Security.Org.BouncyCastle.Utilities -{ - /** - * Exception to be thrown on a failure to reset an object implementing Memoable. - *

- * The exception extends InvalidCastException to enable users to have a single handling case, - * only introducing specific handling of this one if required. - *

- */ - internal class MemoableResetException - : InvalidCastException - { - /** - * Basic Constructor. - * - * @param msg message to be associated with this exception. - */ - public MemoableResetException(string msg) - : base(msg) - { - } - } - -} - diff --git a/src/Renci.SshNet/Security/BouncyCastle/util/encoders/Hex.cs b/src/Renci.SshNet/Security/BouncyCastle/util/encoders/Hex.cs index 658eb6fec..cb0ed20e8 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/util/encoders/Hex.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/util/encoders/Hex.cs @@ -14,90 +14,6 @@ private Hex() { } - public static string ToHexString( - byte[] data) - { - return ToHexString(data, 0, data.Length); - } - - public static string ToHexString( - byte[] data, - int off, - int length) - { - byte[] hex = Encode(data, off, length); - return Encoding.UTF8.GetString(hex, 0, hex.Length); - } - - /** - * encode the input data producing a Hex encoded byte array. - * - * @return a byte array containing the Hex encoded data. - */ - public static byte[] Encode( - byte[] data) - { - return Encode(data, 0, data.Length); - } - - /** - * encode the input data producing a Hex encoded byte array. - * - * @return a byte array containing the Hex encoded data. - */ - public static byte[] Encode( - byte[] data, - int off, - int length) - { - MemoryStream bOut = new MemoryStream(length * 2); - - encoder.Encode(data, off, length, bOut); - - return bOut.ToArray(); - } - - /** - * Hex encode the byte data writing it to the given output stream. - * - * @return the number of bytes produced. - */ - public static int Encode( - byte[] data, - Stream outStream) - { - return encoder.Encode(data, 0, data.Length, outStream); - } - - /** - * Hex encode the byte data writing it to the given output stream. - * - * @return the number of bytes produced. - */ - public static int Encode( - byte[] data, - int off, - int length, - Stream outStream) - { - return encoder.Encode(data, off, length, outStream); - } - - /** - * decode the Hex encoded input data. It is assumed the input data is valid. - * - * @return a byte array representing the decoded data. - */ - public static byte[] Decode( - byte[] data) - { - MemoryStream bOut = new MemoryStream((data.Length + 1) / 2); - - encoder.Decode(data, 0, data.Length, bOut); - - return bOut.ToArray(); - } - /** * decode the Hex encoded string data - whitespace will be ignored. * @@ -112,18 +28,5 @@ public static byte[] Decode( return bOut.ToArray(); } - - /** - * decode the Hex encoded string data writing it to the given output stream, - * whitespace characters will be ignored. - * - * @return the number of bytes produced. - */ - public static int Decode( - string data, - Stream outStream) - { - return encoder.DecodeString(data, outStream); - } } -} \ No newline at end of file +} diff --git a/src/Renci.SshNet/Security/BouncyCastle/util/encoders/HexEncoder.cs b/src/Renci.SshNet/Security/BouncyCastle/util/encoders/HexEncoder.cs index dd4e1ec20..83505f311 100644 --- a/src/Renci.SshNet/Security/BouncyCastle/util/encoders/HexEncoder.cs +++ b/src/Renci.SshNet/Security/BouncyCastle/util/encoders/HexEncoder.cs @@ -37,87 +37,11 @@ public HexEncoder() InitialiseDecodingTable(); } - /** - * encode the input data producing a Hex output stream. - * - * @return the number of bytes produced. - */ - public int Encode( - byte[] data, - int off, - int length, - Stream outStream) - { - for (int i = off; i < (off + length); i++) - { - int v = data[i]; - - outStream.WriteByte(encodingTable[v >> 4]); - outStream.WriteByte(encodingTable[v & 0xf]); - } - - return length * 2; - } - private static bool Ignore(char c) { return c == '\n' || c =='\r' || c == '\t' || c == ' '; } - /** - * decode the Hex encoded byte data writing it to the given output stream, - * whitespace characters will be ignored. - * - * @return the number of bytes produced. - */ - public int Decode( - byte[] data, - int off, - int length, - Stream outStream) - { - byte b1, b2; - int outLen = 0; - int end = off + length; - - while (end > off) - { - if (!Ignore((char)data[end - 1])) - { - break; - } - - end--; - } - - int i = off; - while (i < end) - { - while (i < end && Ignore((char)data[i])) - { - i++; - } - - b1 = decodingTable[data[i++]]; - - while (i < end && Ignore((char)data[i])) - { - i++; - } - - b2 = decodingTable[data[i++]]; - - if ((b1 | b2) >= 0x80) - throw new IOException("invalid characters encountered in Hex data"); - - outStream.WriteByte((byte)((b1 << 4) | b2)); - - outLen++; - } - - return outLen; - } - /** * decode the Hex encoded string data writing it to the given output stream, * whitespace characters will be ignored. @@ -171,4 +95,4 @@ public int DecodeString( return length; } } -} \ No newline at end of file +} diff --git a/src/Renci.SshNet/Security/Chaos.NaCl/CryptoBytes.cs b/src/Renci.SshNet/Security/Chaos.NaCl/CryptoBytes.cs index 032291827..9b1d1936b 100644 --- a/src/Renci.SshNet/Security/Chaos.NaCl/CryptoBytes.cs +++ b/src/Renci.SshNet/Security/Chaos.NaCl/CryptoBytes.cs @@ -5,29 +5,6 @@ namespace Renci.SshNet.Security.Chaos.NaCl { internal static class CryptoBytes { - internal static bool ConstantTimeEquals(byte[] x, byte[] y) - { - if (x == null) - throw new ArgumentNullException("x"); - if (y == null) - throw new ArgumentNullException("y"); - if (x.Length != y.Length) - throw new ArgumentException("x.Length must equal y.Length"); - return InternalConstantTimeEquals(x, 0, y, 0, x.Length) != 0; - } - - internal static bool ConstantTimeEquals(ArraySegment x, ArraySegment y) - { - if (x.Array == null) - throw new ArgumentNullException("x.Array"); - if (y.Array == null) - throw new ArgumentNullException("y.Array"); - if (x.Count != y.Count) - throw new ArgumentException("x.Count must equal y.Count"); - - return InternalConstantTimeEquals(x.Array, x.Offset, y.Array, y.Offset, x.Count) != 0; - } - internal static bool ConstantTimeEquals(byte[] x, int xOffset, byte[] y, int yOffset, int length) { if (x == null) @@ -63,26 +40,6 @@ internal static void Wipe(byte[] data) InternalWipe(data, 0, data.Length); } - internal static void Wipe(byte[] data, int offset, int count) - { - if (data == null) - throw new ArgumentNullException("data"); - if (offset < 0) - throw new ArgumentOutOfRangeException("offset"); - if (count < 0) - throw new ArgumentOutOfRangeException("count", "Requires count >= 0"); - if ((uint)offset + (uint)count > (uint)data.Length) - throw new ArgumentException("Requires offset + count <= data.Length"); - InternalWipe(data, offset, count); - } - - internal static void Wipe(ArraySegment data) - { - if (data.Array == null) - throw new ArgumentNullException("data.Array"); - InternalWipe(data.Array, data.Offset, data.Count); - } - // Secure wiping is hard // * the GC can move around and copy memory // Perhaps this can be avoided by using unmanaged memory or by fixing the position of the array in memory @@ -96,95 +53,5 @@ internal static void InternalWipe(byte[] data, int offset, int count) { Array.Clear(data, offset, count); } - - // shallow wipe of structs - [MethodImpl(MethodImplOptions.NoInlining)] - internal static void InternalWipe(ref T data) - where T : struct - { - data = default(T); - } - - // constant time hex conversion - // see http://stackoverflow.com/a/14333437/445517 - // - // An explanation of the weird bit fiddling: - // - // 1. `bytes[i] >> 4` extracts the high nibble of a byte - // `bytes[i] & 0xF` extracts the low nibble of a byte - // 2. `b - 10` - // is `< 0` for values `b < 10`, which will become a decimal digit - // is `>= 0` for values `b > 10`, which will become a letter from `A` to `F`. - // 3. Using `i >> 31` on a signed 32 bit integer extracts the sign, thanks to sign extension. - // It will be `-1` for `i < 0` and `0` for `i >= 0`. - // 4. Combining 2) and 3), shows that `(b-10)>>31` will be `0` for letters and `-1` for digits. - // 5. Looking at the case for letters, the last summand becomes `0`, and `b` is in the range 10 to 15. We want to map it to `A`(65) to `F`(70), which implies adding 55 (`'A'-10`). - // 6. Looking at the case for digits, we want to adapt the last summand so it maps `b` from the range 0 to 9 to the range `0`(48) to `9`(57). This means it needs to become -7 (`'0' - 55`). - // Now we could just multiply with 7. But since -1 is represented by all bits being 1, we can instead use `& -7` since `(0 & -7) == 0` and `(-1 & -7) == -7`. - // - // Some further considerations: - // - // * I didn't use a second loop variable to index into `c`, since measurement shows that calculating it from `i` is cheaper. - // * Using exactly `i < bytes.Length` as upper bound of the loop allows the JITter to eliminate bounds checks on `bytes[i]`, so I chose that variant. - // * Making `b` an int avoids unnecessary conversions from and to byte. - internal static string ToHexStringUpper(byte[] data) - { - if (data == null) - return null; - char[] c = new char[data.Length * 2]; - int b; - for (int i = 0; i < data.Length; i++) - { - b = data[i] >> 4; - c[i * 2] = (char)(55 + b + (((b - 10) >> 31) & -7)); - b = data[i] & 0xF; - c[i * 2 + 1] = (char)(55 + b + (((b - 10) >> 31) & -7)); - } - return new string(c); - } - - // Explanation is similar to ToHexStringUpper - // constant 55 -> 87 and -7 -> -39 to compensate for the offset 32 between lowercase and uppercase letters - internal static string ToHexStringLower(byte[] data) - { - if (data == null) - return null; - char[] c = new char[data.Length * 2]; - int b; - for (int i = 0; i < data.Length; i++) - { - b = data[i] >> 4; - c[i * 2] = (char)(87 + b + (((b - 10) >> 31) & -39)); - b = data[i] & 0xF; - c[i * 2 + 1] = (char)(87 + b + (((b - 10) >> 31) & -39)); - } - return new string(c); - } - - internal static byte[] FromHexString(string hexString) - { - if (hexString == null) - return null; - if (hexString.Length % 2 != 0) - throw new FormatException("The hex string is invalid because it has an odd length"); - var result = new byte[hexString.Length / 2]; - for (int i = 0; i < result.Length; i++) - result[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); - return result; - } - - internal static string ToBase64String(byte[] data) - { - if (data == null) - return null; - return Convert.ToBase64String(data); - } - - internal static byte[] FromBase64String(string s) - { - if (s == null) - return null; - return Convert.FromBase64String(s); - } } } diff --git a/src/Renci.SshNet/Security/Chaos.NaCl/Ed25519.cs b/src/Renci.SshNet/Security/Chaos.NaCl/Ed25519.cs index 5a085225b..5993a6e9d 100644 --- a/src/Renci.SshNet/Security/Chaos.NaCl/Ed25519.cs +++ b/src/Renci.SshNet/Security/Chaos.NaCl/Ed25519.cs @@ -9,16 +9,6 @@ internal static class Ed25519 public static readonly int SignatureSizeInBytes = 64; public static readonly int ExpandedPrivateKeySizeInBytes = 32 * 2; public static readonly int PrivateKeySeedSizeInBytes = 32; - public static readonly int SharedKeySizeInBytes = 32; - - public static bool Verify(ArraySegment signature, ArraySegment message, ArraySegment publicKey) - { - if (signature.Count != SignatureSizeInBytes) - throw new ArgumentException(string.Format("Signature size must be {0}", SignatureSizeInBytes), "signature.Count"); - if (publicKey.Count != PublicKeySizeInBytes) - throw new ArgumentException(string.Format("Public key size must be {0}", PublicKeySizeInBytes), "publicKey.Count"); - return Ed25519Operations.crypto_sign_verify(signature.Array, signature.Offset, message.Array, message.Offset, message.Count, publicKey.Array, publicKey.Offset); - } public static bool Verify(byte[] signature, byte[] message, byte[] publicKey) { @@ -57,24 +47,6 @@ public static byte[] Sign(byte[] message, byte[] expandedPrivateKey) return signature; } - public static byte[] PublicKeyFromSeed(byte[] privateKeySeed) - { - byte[] privateKey; - byte[] publicKey; - KeyPairFromSeed(out publicKey, out privateKey, privateKeySeed); - CryptoBytes.Wipe(privateKey); - return publicKey; - } - - public static byte[] ExpandedPrivateKeyFromSeed(byte[] privateKeySeed) - { - byte[] privateKey; - byte[] publicKey; - KeyPairFromSeed(out publicKey, out privateKey, privateKeySeed); - CryptoBytes.Wipe(publicKey); - return privateKey; - } - public static void KeyPairFromSeed(out byte[] publicKey, out byte[] expandedPrivateKey, byte[] privateKeySeed) { if (privateKeySeed == null) @@ -87,61 +59,5 @@ public static void KeyPairFromSeed(out byte[] publicKey, out byte[] expandedPriv publicKey = pk; expandedPrivateKey = sk; } - - public static void KeyPairFromSeed(ArraySegment publicKey, ArraySegment expandedPrivateKey, ArraySegment privateKeySeed) - { - if (publicKey.Array == null) - throw new ArgumentNullException("publicKey.Array"); - if (expandedPrivateKey.Array == null) - throw new ArgumentNullException("expandedPrivateKey.Array"); - if (privateKeySeed.Array == null) - throw new ArgumentNullException("privateKeySeed.Array"); - if (publicKey.Count != PublicKeySizeInBytes) - throw new ArgumentException("publicKey.Count"); - if (expandedPrivateKey.Count != ExpandedPrivateKeySizeInBytes) - throw new ArgumentException("expandedPrivateKey.Count"); - if (privateKeySeed.Count != PrivateKeySeedSizeInBytes) - throw new ArgumentException("privateKeySeed.Count"); - Ed25519Operations.crypto_sign_keypair( - publicKey.Array, publicKey.Offset, - expandedPrivateKey.Array, expandedPrivateKey.Offset, - privateKeySeed.Array, privateKeySeed.Offset); - } - - [Obsolete("Needs more testing")] - public static byte[] KeyExchange(byte[] publicKey, byte[] privateKey) - { - var sharedKey = new byte[SharedKeySizeInBytes]; - KeyExchange(new ArraySegment(sharedKey), new ArraySegment(publicKey), new ArraySegment(privateKey)); - return sharedKey; - } - - [Obsolete("Needs more testing")] - public static void KeyExchange(ArraySegment sharedKey, ArraySegment publicKey, ArraySegment privateKey) - { - if (sharedKey.Array == null) - throw new ArgumentNullException("sharedKey.Array"); - if (publicKey.Array == null) - throw new ArgumentNullException("publicKey.Array"); - if (privateKey.Array == null) - throw new ArgumentNullException("privateKey"); - if (sharedKey.Count != 32) - throw new ArgumentException("sharedKey.Count != 32"); - if (publicKey.Count != 32) - throw new ArgumentException("publicKey.Count != 32"); - if (privateKey.Count != 64) - throw new ArgumentException("privateKey.Count != 64"); - - FieldElement montgomeryX, edwardsY, edwardsZ, sharedMontgomeryX; - FieldOperations.fe_frombytes(out edwardsY, publicKey.Array, publicKey.Offset); - FieldOperations.fe_1(out edwardsZ); - MontgomeryCurve25519.EdwardsToMontgomeryX(out montgomeryX, ref edwardsY, ref edwardsZ); - byte[] h = Sha512.Hash(privateKey.Array, privateKey.Offset, 32);//ToDo: Remove alloc - ScalarOperations.sc_clamp(h, 0); - MontgomeryOperations.scalarmult(out sharedMontgomeryX, h, 0, ref montgomeryX); - CryptoBytes.Wipe(h); - FieldOperations.fe_tobytes(sharedKey.Array, sharedKey.Offset, ref sharedMontgomeryX); - MontgomeryCurve25519.KeyExchangeOutputHashNaCl(sharedKey.Array, sharedKey.Offset); - } } -} \ No newline at end of file +} diff --git a/src/Renci.SshNet/Security/Chaos.NaCl/Internal/ByteIntegerConverter.cs b/src/Renci.SshNet/Security/Chaos.NaCl/Internal/ByteIntegerConverter.cs index 72f4e8afb..7bc6a4712 100644 --- a/src/Renci.SshNet/Security/Chaos.NaCl/Internal/ByteIntegerConverter.cs +++ b/src/Renci.SshNet/Security/Chaos.NaCl/Internal/ByteIntegerConverter.cs @@ -8,25 +8,6 @@ namespace Renci.SshNet.Security.Chaos.NaCl.Internal // so this ugly code becomes necessary :( internal static class ByteIntegerConverter { - #region Individual - - internal static UInt32 LoadLittleEndian32(byte[] buf, int offset) - { - return - (UInt32)(buf[offset + 0]) - | (((UInt32)(buf[offset + 1])) << 8) - | (((UInt32)(buf[offset + 2])) << 16) - | (((UInt32)(buf[offset + 3])) << 24); - } - - internal static void StoreLittleEndian32(byte[] buf, int offset, UInt32 value) - { - buf[offset + 0] = unchecked((byte)value); - buf[offset + 1] = unchecked((byte)(value >> 8)); - buf[offset + 2] = unchecked((byte)(value >> 16)); - buf[offset + 3] = unchecked((byte)(value >> 24)); - } - internal static UInt64 LoadBigEndian64(byte[] buf, int offset) { return @@ -52,261 +33,6 @@ internal static void StoreBigEndian64(byte[] buf, int offset, UInt64 value) buf[offset + 0] = unchecked((byte)(value >> 56)); } - /*internal static void XorLittleEndian32(byte[] buf, int offset, UInt32 value) - { - buf[offset + 0] ^= (byte)value; - buf[offset + 1] ^= (byte)(value >> 8); - buf[offset + 2] ^= (byte)(value >> 16); - buf[offset + 3] ^= (byte)(value >> 24); - }*/ - - /*internal static void XorLittleEndian32(byte[] output, int outputOffset, byte[] input, int inputOffset, UInt32 value) - { - output[outputOffset + 0] = (byte)(input[inputOffset + 0] ^ value); - output[outputOffset + 1] = (byte)(input[inputOffset + 1] ^ (value >> 8)); - output[outputOffset + 2] = (byte)(input[inputOffset + 2] ^ (value >> 16)); - output[outputOffset + 3] = (byte)(input[inputOffset + 3] ^ (value >> 24)); - }*/ - - #endregion - - #region Array8 - - internal static void Array8LoadLittleEndian32(out Array8 output, byte[] input, int inputOffset) - { - output.x0 = LoadLittleEndian32(input, inputOffset + 0); - output.x1 = LoadLittleEndian32(input, inputOffset + 4); - output.x2 = LoadLittleEndian32(input, inputOffset + 8); - output.x3 = LoadLittleEndian32(input, inputOffset + 12); - output.x4 = LoadLittleEndian32(input, inputOffset + 16); - output.x5 = LoadLittleEndian32(input, inputOffset + 20); - output.x6 = LoadLittleEndian32(input, inputOffset + 24); - output.x7 = LoadLittleEndian32(input, inputOffset + 28); - } - - /* internal static void Array8LoadLittleEndian32(out Array8 output, byte[] input, int inputOffset, int inputLength) - { - #if DEBUG - if (inputLength <= 0) - throw new ArgumentException(); - #endif - int inputEnd = inputOffset + inputLength; - UInt32 highestInt; - switch (inputLength & 3) - { - case 1: - highestInt = input[inputEnd - 1]; - break; - case 2: - highestInt = (uint)( - (input[inputEnd - 1] << 8) | - (input[inputEnd - 2])); - break; - case 3: - highestInt = (uint)( - (input[inputEnd - 1] << 16) | - (input[inputEnd - 2] << 8) | - (input[inputEnd - 3])); - break; - case 0: - highestInt = (uint)( - (input[inputEnd - 1] << 24) | - (input[inputEnd - 2] << 16) | - (input[inputEnd - 3] << 8) | - (input[inputEnd - 4])); - break; - default: - throw new InvalidOperationException(); - } - switch ((inputLength - 1) >> 2) - { - case 7: - output.x7 = highestInt; - output.x6 = LoadLittleEndian32(input, inputOffset + 6 * 4); - output.x5 = LoadLittleEndian32(input, inputOffset + 5 * 4); - output.x4 = LoadLittleEndian32(input, inputOffset + 4 * 4); - output.x3 = LoadLittleEndian32(input, inputOffset + 3 * 4); - output.x2 = LoadLittleEndian32(input, inputOffset + 2 * 4); - output.x1 = LoadLittleEndian32(input, inputOffset + 1 * 4); - output.x0 = LoadLittleEndian32(input, inputOffset + 0 * 4); - return; - case 6: - output.x7 = 0; - output.x6 = highestInt; - output.x5 = LoadLittleEndian32(input, inputOffset + 5 * 4); - output.x4 = LoadLittleEndian32(input, inputOffset + 4 * 4); - output.x3 = LoadLittleEndian32(input, inputOffset + 3 * 4); - output.x2 = LoadLittleEndian32(input, inputOffset + 2 * 4); - output.x1 = LoadLittleEndian32(input, inputOffset + 1 * 4); - output.x0 = LoadLittleEndian32(input, inputOffset + 0 * 4); - return; - case 5: - output.x7 = 0; - output.x6 = 0; - output.x5 = highestInt; - output.x4 = LoadLittleEndian32(input, inputOffset + 4 * 4); - output.x3 = LoadLittleEndian32(input, inputOffset + 3 * 4); - output.x2 = LoadLittleEndian32(input, inputOffset + 2 * 4); - output.x1 = LoadLittleEndian32(input, inputOffset + 1 * 4); - output.x0 = LoadLittleEndian32(input, inputOffset + 0 * 4); - return; - case 4: - output.x7 = 0; - output.x6 = 0; - output.x5 = 0; - output.x4 = highestInt; - output.x3 = LoadLittleEndian32(input, inputOffset + 3 * 4); - output.x2 = LoadLittleEndian32(input, inputOffset + 2 * 4); - output.x1 = LoadLittleEndian32(input, inputOffset + 1 * 4); - output.x0 = LoadLittleEndian32(input, inputOffset + 0 * 4); - return; - case 3: - output.x7 = 0; - output.x6 = 0; - output.x5 = 0; - output.x4 = 0; - output.x3 = highestInt; - output.x2 = LoadLittleEndian32(input, inputOffset + 2 * 4); - output.x1 = LoadLittleEndian32(input, inputOffset + 1 * 4); - output.x0 = LoadLittleEndian32(input, inputOffset + 0 * 4); - return; - case 2: - output.x7 = 0; - output.x6 = 0; - output.x5 = 0; - output.x4 = 0; - output.x3 = 0; - output.x2 = highestInt; - output.x1 = LoadLittleEndian32(input, inputOffset + 1 * 4); - output.x0 = LoadLittleEndian32(input, inputOffset + 0 * 4); - return; - case 1: - output.x7 = 0; - output.x6 = 0; - output.x5 = 0; - output.x4 = 0; - output.x3 = 0; - output.x2 = 0; - output.x1 = highestInt; - output.x0 = LoadLittleEndian32(input, inputOffset + 0 * 4); - return; - case 0: - output.x7 = 0; - output.x6 = 0; - output.x5 = 0; - output.x4 = 0; - output.x3 = 0; - output.x2 = 0; - output.x1 = 0; - output.x0 = highestInt; - return; - default: - throw new InvalidOperationException(); - } - }*/ - - /*internal static void Array8XorLittleEndian(byte[] output, int outputOffset, byte[] input, int inputOffset, ref Array8 keyStream, int length) - { -#if DEBUG - InternalAssert(length > 0); -#endif - int outputEnd = outputOffset + length; - UInt32 highestInt; - switch ((length - 1) >> 2) - { - case 7: - highestInt = keyStream.x7; - XorLittleEndian32(output, outputOffset + 6 * 4, input, inputOffset + 6 * 4, keyStream.x6); - XorLittleEndian32(output, outputOffset + 5 * 4, input, inputOffset + 6 * 4, keyStream.x5); - XorLittleEndian32(output, outputOffset + 4 * 4, input, inputOffset + 6 * 4, keyStream.x4); - XorLittleEndian32(output, outputOffset + 3 * 4, input, inputOffset + 6 * 4, keyStream.x3); - XorLittleEndian32(output, outputOffset + 2 * 4, input, inputOffset + 6 * 4, keyStream.x2); - XorLittleEndian32(output, outputOffset + 1 * 4, input, inputOffset + 6 * 4, keyStream.x1); - XorLittleEndian32(output, outputOffset + 0 * 4, input, inputOffset + 6 * 4, keyStream.x0); - break; - case 6: - highestInt = keyStream.x6; - XorLittleEndian32(output, outputOffset + 5 * 4, input, inputOffset + 6 * 4, keyStream.x5); - XorLittleEndian32(output, outputOffset + 4 * 4, input, inputOffset + 6 * 4, keyStream.x4); - XorLittleEndian32(output, outputOffset + 3 * 4, input, inputOffset + 6 * 4, keyStream.x3); - XorLittleEndian32(output, outputOffset + 2 * 4, input, inputOffset + 6 * 4, keyStream.x2); - XorLittleEndian32(output, outputOffset + 1 * 4, input, inputOffset + 6 * 4, keyStream.x1); - XorLittleEndian32(output, outputOffset + 0 * 4, input, inputOffset + 6 * 4, keyStream.x0); - break; - case 5: - highestInt = keyStream.x5; - XorLittleEndian32(output, outputOffset + 4 * 4, input, inputOffset + 6 * 4, keyStream.x4); - XorLittleEndian32(output, outputOffset + 3 * 4, input, inputOffset + 6 * 4, keyStream.x3); - XorLittleEndian32(output, outputOffset + 2 * 4, input, inputOffset + 6 * 4, keyStream.x2); - XorLittleEndian32(output, outputOffset + 1 * 4, input, inputOffset + 6 * 4, keyStream.x1); - XorLittleEndian32(output, outputOffset + 0 * 4, input, inputOffset + 6 * 4, keyStream.x0); - break; - case 4: - highestInt = keyStream.x4; - XorLittleEndian32(output, outputOffset + 3 * 4, input, inputOffset + 6 * 4, keyStream.x3); - XorLittleEndian32(output, outputOffset + 2 * 4, input, inputOffset + 6 * 4, keyStream.x2); - XorLittleEndian32(output, outputOffset + 1 * 4, input, inputOffset + 6 * 4, keyStream.x1); - XorLittleEndian32(output, outputOffset + 0 * 4, input, inputOffset + 6 * 4, keyStream.x0); - break; - case 3: - highestInt = keyStream.x3; - XorLittleEndian32(output, outputOffset + 2 * 4, input, inputOffset + 6 * 4, keyStream.x2); - XorLittleEndian32(output, outputOffset + 1 * 4, input, inputOffset + 6 * 4, keyStream.x1); - XorLittleEndian32(output, outputOffset + 0 * 4, input, inputOffset + 6 * 4, keyStream.x0); - break; - case 2: - highestInt = keyStream.x2; - XorLittleEndian32(output, outputOffset + 1 * 4, input, inputOffset + 6 * 4, keyStream.x1); - XorLittleEndian32(output, outputOffset + 0 * 4, input, inputOffset + 6 * 4, keyStream.x0); - break; - case 1: - highestInt = keyStream.x1; - XorLittleEndian32(output, outputOffset + 0 * 4, input, inputOffset + 6 * 4, keyStream.x0); - break; - case 0: - highestInt = keyStream.x0; - break; - default: - throw new InvalidOperationException(); - } - switch (length & 3) - { - case 1: - output[outputEnd - 1] ^= (byte)highestInt; - break; - case 2: - output[outputEnd - 1] ^= (byte)(highestInt >> 8); - output[outputEnd - 2] ^= (byte)highestInt; - break; - case 3: - output[outputEnd - 1] ^= (byte)(highestInt >> 16); - output[outputEnd - 2] ^= (byte)(highestInt >> 8); - output[outputEnd - 3] ^= (byte)highestInt; - break; - case 0: - output[outputEnd - 1] ^= (byte)(highestInt >> 24); - output[outputEnd - 2] ^= (byte)(highestInt >> 16); - output[outputEnd - 3] ^= (byte)(highestInt >> 8); - output[outputEnd - 4] ^= (byte)highestInt; - break; - default: - throw new InvalidOperationException(); - } - }*/ - - /*internal static void Array8StoreLittleEndian32(byte[] output, int outputOffset, ref Array8 input) - { - StoreLittleEndian32(output, outputOffset + 0, input.x0); - StoreLittleEndian32(output, outputOffset + 4, input.x1); - StoreLittleEndian32(output, outputOffset + 8, input.x2); - StoreLittleEndian32(output, outputOffset + 12, input.x3); - StoreLittleEndian32(output, outputOffset + 16, input.x4); - StoreLittleEndian32(output, outputOffset + 20, input.x5); - StoreLittleEndian32(output, outputOffset + 24, input.x6); - StoreLittleEndian32(output, outputOffset + 28, input.x7); - }*/ - #endregion - internal static void Array16LoadBigEndian64(out Array16 output, byte[] input, int inputOffset) { output.x0 = LoadBigEndian64(input, inputOffset + 0); @@ -326,91 +52,5 @@ internal static void Array16LoadBigEndian64(out Array16 output, byte[] i output.x14 = LoadBigEndian64(input, inputOffset + 112); output.x15 = LoadBigEndian64(input, inputOffset + 120); } - - // ToDo: Only used in tests. Remove? - internal static void Array16LoadLittleEndian32(out Array16 output, byte[] input, int inputOffset) - { - output.x0 = LoadLittleEndian32(input, inputOffset + 0); - output.x1 = LoadLittleEndian32(input, inputOffset + 4); - output.x2 = LoadLittleEndian32(input, inputOffset + 8); - output.x3 = LoadLittleEndian32(input, inputOffset + 12); - output.x4 = LoadLittleEndian32(input, inputOffset + 16); - output.x5 = LoadLittleEndian32(input, inputOffset + 20); - output.x6 = LoadLittleEndian32(input, inputOffset + 24); - output.x7 = LoadLittleEndian32(input, inputOffset + 28); - output.x8 = LoadLittleEndian32(input, inputOffset + 32); - output.x9 = LoadLittleEndian32(input, inputOffset + 36); - output.x10 = LoadLittleEndian32(input, inputOffset + 40); - output.x11 = LoadLittleEndian32(input, inputOffset + 44); - output.x12 = LoadLittleEndian32(input, inputOffset + 48); - output.x13 = LoadLittleEndian32(input, inputOffset + 52); - output.x14 = LoadLittleEndian32(input, inputOffset + 56); - output.x15 = LoadLittleEndian32(input, inputOffset + 60); - } - - /*internal static void Array16LoadLittleEndian32(out Array16 output, byte[] input, int inputOffset, int inputLength) - { - Array8 temp; - if (inputLength > 32) - { - output.x0 = LoadLittleEndian32(input, inputOffset + 0); - output.x1 = LoadLittleEndian32(input, inputOffset + 4); - output.x2 = LoadLittleEndian32(input, inputOffset + 8); - output.x3 = LoadLittleEndian32(input, inputOffset + 12); - output.x4 = LoadLittleEndian32(input, inputOffset + 16); - output.x5 = LoadLittleEndian32(input, inputOffset + 20); - output.x6 = LoadLittleEndian32(input, inputOffset + 24); - output.x7 = LoadLittleEndian32(input, inputOffset + 28); - Array8LoadLittleEndian32(out temp, input, inputOffset + 32, inputLength - 32); - output.x8 = temp.x0; - output.x9 = temp.x1; - output.x10 = temp.x2; - output.x11 = temp.x3; - output.x12 = temp.x4; - output.x13 = temp.x5; - output.x14 = temp.x6; - output.x15 = temp.x7; - } - else - { - Array8LoadLittleEndian32(out temp, input, inputOffset, inputLength); - output.x0 = temp.x0; - output.x1 = temp.x1; - output.x2 = temp.x2; - output.x3 = temp.x3; - output.x4 = temp.x4; - output.x5 = temp.x5; - output.x6 = temp.x6; - output.x7 = temp.x7; - output.x8 = 0; - output.x9 = 0; - output.x10 = 0; - output.x11 = 0; - output.x12 = 0; - output.x13 = 0; - output.x14 = 0; - output.x15 = 0; - } - }*/ - - internal static void Array16StoreLittleEndian32(byte[] output, int outputOffset, ref Array16 input) - { - StoreLittleEndian32(output, outputOffset + 0, input.x0); - StoreLittleEndian32(output, outputOffset + 4, input.x1); - StoreLittleEndian32(output, outputOffset + 8, input.x2); - StoreLittleEndian32(output, outputOffset + 12, input.x3); - StoreLittleEndian32(output, outputOffset + 16, input.x4); - StoreLittleEndian32(output, outputOffset + 20, input.x5); - StoreLittleEndian32(output, outputOffset + 24, input.x6); - StoreLittleEndian32(output, outputOffset + 28, input.x7); - StoreLittleEndian32(output, outputOffset + 32, input.x8); - StoreLittleEndian32(output, outputOffset + 36, input.x9); - StoreLittleEndian32(output, outputOffset + 40, input.x10); - StoreLittleEndian32(output, outputOffset + 44, input.x11); - StoreLittleEndian32(output, outputOffset + 48, input.x12); - StoreLittleEndian32(output, outputOffset + 52, input.x13); - StoreLittleEndian32(output, outputOffset + 56, input.x14); - StoreLittleEndian32(output, outputOffset + 60, input.x15); - } } } diff --git a/src/Renci.SshNet/Security/Chaos.NaCl/Internal/Poly1305Donna.cs b/src/Renci.SshNet/Security/Chaos.NaCl/Internal/Poly1305Donna.cs deleted file mode 100644 index 168e69bb0..000000000 --- a/src/Renci.SshNet/Security/Chaos.NaCl/Internal/Poly1305Donna.cs +++ /dev/null @@ -1,154 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Renci.SshNet.Security.Chaos.NaCl.Internal -{ - internal class Poly1305Donna - { - // written by floodyberry (Andrew M.) - // original license: MIT or PUBLIC DOMAIN - // https://github.com/floodyberry/poly1305-donna/blob/master/poly1305-donna-unrolled.c - internal static void poly1305_auth(byte[] output, int outputOffset, byte[] m, int mStart, int mLength, ref Array8 key) - { - UInt32 t0, t1, t2, t3; - UInt32 h0, h1, h2, h3, h4; - UInt32 r0, r1, r2, r3, r4; - UInt32 s1, s2, s3, s4; - UInt32 b, nb; - int j; - UInt64 tt0, tt1, tt2, tt3, tt4; - UInt64 f0, f1, f2, f3; - UInt32 g0, g1, g2, g3, g4; - UInt64 c; - - /* clamp key */ - t0 = key.x0; - t1 = key.x1; - t2 = key.x2; - t3 = key.x3; - - /* precompute multipliers */ - r0 = t0 & 0x3ffffff; t0 >>= 26; t0 |= t1 << 6; - r1 = t0 & 0x3ffff03; t1 >>= 20; t1 |= t2 << 12; - r2 = t1 & 0x3ffc0ff; t2 >>= 14; t2 |= t3 << 18; - r3 = t2 & 0x3f03fff; t3 >>= 8; - r4 = t3 & 0x00fffff; - - s1 = r1 * 5; - s2 = r2 * 5; - s3 = r3 * 5; - s4 = r4 * 5; - - /* init state */ - h0 = 0; - h1 = 0; - h2 = 0; - h3 = 0; - h4 = 0; - - /* full blocks */ - if (mLength < 16) - goto poly1305_donna_atmost15bytes; - - poly1305_donna_16bytes: - mStart += 16; - mLength -= 16; - - t0 = ByteIntegerConverter.LoadLittleEndian32(m, mStart - 16); - t1 = ByteIntegerConverter.LoadLittleEndian32(m, mStart - 12); - t2 = ByteIntegerConverter.LoadLittleEndian32(m, mStart - 8); - t3 = ByteIntegerConverter.LoadLittleEndian32(m, mStart - 4); - - //todo: looks like these can be simplified a bit - h0 += t0 & 0x3ffffff; - h1 += (uint)(((((UInt64)t1 << 32) | t0) >> 26) & 0x3ffffff); - h2 += (uint)(((((UInt64)t2 << 32) | t1) >> 20) & 0x3ffffff); - h3 += (uint)(((((UInt64)t3 << 32) | t2) >> 14) & 0x3ffffff); - h4 += (t3 >> 8) | (1 << 24); - - - poly1305_donna_mul: - tt0 = (ulong)h0 * r0 + (ulong)h1 * s4 + (ulong)h2 * s3 + (ulong)h3 * s2 + (ulong)h4 * s1; - tt1 = (ulong)h0 * r1 + (ulong)h1 * r0 + (ulong)h2 * s4 + (ulong)h3 * s3 + (ulong)h4 * s2; - tt2 = (ulong)h0 * r2 + (ulong)h1 * r1 + (ulong)h2 * r0 + (ulong)h3 * s4 + (ulong)h4 * s3; - tt3 = (ulong)h0 * r3 + (ulong)h1 * r2 + (ulong)h2 * r1 + (ulong)h3 * r0 + (ulong)h4 * s4; - tt4 = (ulong)h0 * r4 + (ulong)h1 * r3 + (ulong)h2 * r2 + (ulong)h3 * r1 + (ulong)h4 * r0; - - unchecked - { - h0 = (UInt32)tt0 & 0x3ffffff; c = (tt0 >> 26); - tt1 += c; h1 = (UInt32)tt1 & 0x3ffffff; b = (UInt32)(tt1 >> 26); - tt2 += b; h2 = (UInt32)tt2 & 0x3ffffff; b = (UInt32)(tt2 >> 26); - tt3 += b; h3 = (UInt32)tt3 & 0x3ffffff; b = (UInt32)(tt3 >> 26); - tt4 += b; h4 = (UInt32)tt4 & 0x3ffffff; b = (UInt32)(tt4 >> 26); - } - h0 += b * 5; - - if (mLength >= 16) - goto poly1305_donna_16bytes; - - /* final bytes */ - poly1305_donna_atmost15bytes: - if (mLength == 0) - goto poly1305_donna_finish; - - byte[] mp = new byte[16];//todo remove allocation - - for (j = 0; j < mLength; j++) - mp[j] = m[mStart + j]; - mp[j++] = 1; - for (; j < 16; j++) - mp[j] = 0; - mLength = 0; - - t0 = ByteIntegerConverter.LoadLittleEndian32(mp, 0); - t1 = ByteIntegerConverter.LoadLittleEndian32(mp, 4); - t2 = ByteIntegerConverter.LoadLittleEndian32(mp, 8); - t3 = ByteIntegerConverter.LoadLittleEndian32(mp, 12); - CryptoBytes.Wipe(mp); - - h0 += t0 & 0x3ffffff; - h1 += (uint)(((((UInt64)t1 << 32) | t0) >> 26) & 0x3ffffff); - h2 += (uint)(((((UInt64)t2 << 32) | t1) >> 20) & 0x3ffffff); - h3 += (uint)(((((UInt64)t3 << 32) | t2) >> 14) & 0x3ffffff); - h4 += t3 >> 8; - - goto poly1305_donna_mul; - - poly1305_donna_finish: - b = h0 >> 26; h0 = h0 & 0x3ffffff; - h1 += b; b = h1 >> 26; h1 = h1 & 0x3ffffff; - h2 += b; b = h2 >> 26; h2 = h2 & 0x3ffffff; - h3 += b; b = h3 >> 26; h3 = h3 & 0x3ffffff; - h4 += b; b = h4 >> 26; h4 = h4 & 0x3ffffff; - h0 += b * 5; - - g0 = h0 + 5; b = g0 >> 26; g0 &= 0x3ffffff; - g1 = h1 + b; b = g1 >> 26; g1 &= 0x3ffffff; - g2 = h2 + b; b = g2 >> 26; g2 &= 0x3ffffff; - g3 = h3 + b; b = g3 >> 26; g3 &= 0x3ffffff; - g4 = unchecked(h4 + b - (1 << 26)); - - b = (g4 >> 31) - 1; - nb = ~b; - h0 = (h0 & nb) | (g0 & b); - h1 = (h1 & nb) | (g1 & b); - h2 = (h2 & nb) | (g2 & b); - h3 = (h3 & nb) | (g3 & b); - h4 = (h4 & nb) | (g4 & b); - - f0 = ((h0) | (h1 << 26)) + (UInt64)key.x4; - f1 = ((h1 >> 6) | (h2 << 20)) + (UInt64)key.x5; - f2 = ((h2 >> 12) | (h3 << 14)) + (UInt64)key.x6; - f3 = ((h3 >> 18) | (h4 << 8)) + (UInt64)key.x7; - - unchecked - { - ByteIntegerConverter.StoreLittleEndian32(output, outputOffset + 0, (uint)f0); f1 += (f0 >> 32); - ByteIntegerConverter.StoreLittleEndian32(output, outputOffset + 4, (uint)f1); f2 += (f1 >> 32); - ByteIntegerConverter.StoreLittleEndian32(output, outputOffset + 8, (uint)f2); f3 += (f2 >> 32); - ByteIntegerConverter.StoreLittleEndian32(output, outputOffset + 12, (uint)f3); - } - } - } -} diff --git a/src/Renci.SshNet/Security/Chaos.NaCl/Internal/Salsa/Salsa20.cs b/src/Renci.SshNet/Security/Chaos.NaCl/Internal/Salsa/Salsa20.cs deleted file mode 100644 index 4b74ad0c5..000000000 --- a/src/Renci.SshNet/Security/Chaos.NaCl/Internal/Salsa/Salsa20.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Renci.SshNet.Security.Chaos.NaCl.Internal.Salsa -{ - internal class Salsa20 - { - public const uint SalsaConst0 = 0x61707865; - public const uint SalsaConst1 = 0x3320646e; - public const uint SalsaConst2 = 0x79622d32; - public const uint SalsaConst3 = 0x6b206574; - - internal static void HSalsa20(byte[] output, int outputOffset, byte[] key, int keyOffset, byte[] nonce, int nonceOffset) - { - Array16 state; - state.x0 = SalsaConst0; - state.x1 = ByteIntegerConverter.LoadLittleEndian32(key, keyOffset + 0); - state.x2 = ByteIntegerConverter.LoadLittleEndian32(key, keyOffset + 4); - state.x3 = ByteIntegerConverter.LoadLittleEndian32(key, keyOffset + 8); - state.x4 = ByteIntegerConverter.LoadLittleEndian32(key, keyOffset + 12); - state.x5 = SalsaConst1; - state.x6 = ByteIntegerConverter.LoadLittleEndian32(nonce, nonceOffset + 0); - state.x7 = ByteIntegerConverter.LoadLittleEndian32(nonce, nonceOffset + 4); - state.x8 = ByteIntegerConverter.LoadLittleEndian32(nonce, nonceOffset + 8); - state.x9 = ByteIntegerConverter.LoadLittleEndian32(nonce, nonceOffset + 12); - state.x10 = SalsaConst2; - state.x11 = ByteIntegerConverter.LoadLittleEndian32(key, keyOffset + 16); - state.x12 = ByteIntegerConverter.LoadLittleEndian32(key, keyOffset + 20); - state.x13 = ByteIntegerConverter.LoadLittleEndian32(key, keyOffset + 24); - state.x14 = ByteIntegerConverter.LoadLittleEndian32(key, keyOffset + 28); - state.x15 = SalsaConst3; - - SalsaCore.HSalsa(out state, ref state, 20); - - ByteIntegerConverter.StoreLittleEndian32(output, outputOffset + 0, state.x0); - ByteIntegerConverter.StoreLittleEndian32(output, outputOffset + 4, state.x5); - ByteIntegerConverter.StoreLittleEndian32(output, outputOffset + 8, state.x10); - ByteIntegerConverter.StoreLittleEndian32(output, outputOffset + 12, state.x15); - ByteIntegerConverter.StoreLittleEndian32(output, outputOffset + 16, state.x6); - ByteIntegerConverter.StoreLittleEndian32(output, outputOffset + 20, state.x7); - ByteIntegerConverter.StoreLittleEndian32(output, outputOffset + 24, state.x8); - ByteIntegerConverter.StoreLittleEndian32(output, outputOffset + 28, state.x9); - } - } -} diff --git a/src/Renci.SshNet/Security/Chaos.NaCl/Internal/Salsa/SalsaCore.cs b/src/Renci.SshNet/Security/Chaos.NaCl/Internal/Salsa/SalsaCore.cs deleted file mode 100644 index e539d668f..000000000 --- a/src/Renci.SshNet/Security/Chaos.NaCl/Internal/Salsa/SalsaCore.cs +++ /dev/null @@ -1,263 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Renci.SshNet.Security.Chaos.NaCl.Internal.Salsa -{ - internal static class SalsaCore - { - internal static void HSalsa(out Array16 output, ref Array16 input, int rounds) - { - InternalAssert.Assert(rounds % 2 == 0, "Number of salsa rounds must be even"); - - int doubleRounds = rounds / 2; - - UInt32 x0 = input.x0; - UInt32 x1 = input.x1; - UInt32 x2 = input.x2; - UInt32 x3 = input.x3; - UInt32 x4 = input.x4; - UInt32 x5 = input.x5; - UInt32 x6 = input.x6; - UInt32 x7 = input.x7; - UInt32 x8 = input.x8; - UInt32 x9 = input.x9; - UInt32 x10 = input.x10; - UInt32 x11 = input.x11; - UInt32 x12 = input.x12; - UInt32 x13 = input.x13; - UInt32 x14 = input.x14; - UInt32 x15 = input.x15; - - unchecked - { - for (int i = 0; i < doubleRounds; i++) - { - UInt32 y; - - // row 0 - y = x0 + x12; - x4 ^= (y << 7) | (y >> (32 - 7)); - y = x4 + x0; - x8 ^= (y << 9) | (y >> (32 - 9)); - y = x8 + x4; - x12 ^= (y << 13) | (y >> (32 - 13)); - y = x12 + x8; - x0 ^= (y << 18) | (y >> (32 - 18)); - - // row 1 - y = x5 + x1; - x9 ^= (y << 7) | (y >> (32 - 7)); - y = x9 + x5; - x13 ^= (y << 9) | (y >> (32 - 9)); - y = x13 + x9; - x1 ^= (y << 13) | (y >> (32 - 13)); - y = x1 + x13; - x5 ^= (y << 18) | (y >> (32 - 18)); - - // row 2 - y = x10 + x6; - x14 ^= (y << 7) | (y >> (32 - 7)); - y = x14 + x10; - x2 ^= (y << 9) | (y >> (32 - 9)); - y = x2 + x14; - x6 ^= (y << 13) | (y >> (32 - 13)); - y = x6 + x2; - x10 ^= (y << 18) | (y >> (32 - 18)); - - // row 3 - y = x15 + x11; - x3 ^= (y << 7) | (y >> (32 - 7)); - y = x3 + x15; - x7 ^= (y << 9) | (y >> (32 - 9)); - y = x7 + x3; - x11 ^= (y << 13) | (y >> (32 - 13)); - y = x11 + x7; - x15 ^= (y << 18) | (y >> (32 - 18)); - - // column 0 - y = x0 + x3; - x1 ^= (y << 7) | (y >> (32 - 7)); - y = x1 + x0; - x2 ^= (y << 9) | (y >> (32 - 9)); - y = x2 + x1; - x3 ^= (y << 13) | (y >> (32 - 13)); - y = x3 + x2; - x0 ^= (y << 18) | (y >> (32 - 18)); - - // column 1 - y = x5 + x4; - x6 ^= (y << 7) | (y >> (32 - 7)); - y = x6 + x5; - x7 ^= (y << 9) | (y >> (32 - 9)); - y = x7 + x6; - x4 ^= (y << 13) | (y >> (32 - 13)); - y = x4 + x7; - x5 ^= (y << 18) | (y >> (32 - 18)); - - // column 2 - y = x10 + x9; - x11 ^= (y << 7) | (y >> (32 - 7)); - y = x11 + x10; - x8 ^= (y << 9) | (y >> (32 - 9)); - y = x8 + x11; - x9 ^= (y << 13) | (y >> (32 - 13)); - y = x9 + x8; - x10 ^= (y << 18) | (y >> (32 - 18)); - - // column 3 - y = x15 + x14; - x12 ^= (y << 7) | (y >> (32 - 7)); - y = x12 + x15; - x13 ^= (y << 9) | (y >> (32 - 9)); - y = x13 + x12; - x14 ^= (y << 13) | (y >> (32 - 13)); - y = x14 + x13; - x15 ^= (y << 18) | (y >> (32 - 18)); - } - } - - output.x0 = x0; - output.x1 = x1; - output.x2 = x2; - output.x3 = x3; - output.x4 = x4; - output.x5 = x5; - output.x6 = x6; - output.x7 = x7; - output.x8 = x8; - output.x9 = x9; - output.x10 = x10; - output.x11 = x11; - output.x12 = x12; - output.x13 = x13; - output.x14 = x14; - output.x15 = x15; - } - - internal static void Salsa(out Array16 output, ref Array16 input, int rounds) - { - Array16 temp; - HSalsa(out temp, ref input, rounds); - unchecked - { - output.x0 = temp.x0 + input.x0; - output.x1 = temp.x1 + input.x1; - output.x2 = temp.x2 + input.x2; - output.x3 = temp.x3 + input.x3; - output.x4 = temp.x4 + input.x4; - output.x5 = temp.x5 + input.x5; - output.x6 = temp.x6 + input.x6; - output.x7 = temp.x7 + input.x7; - output.x8 = temp.x8 + input.x8; - output.x9 = temp.x9 + input.x9; - output.x10 = temp.x10 + input.x10; - output.x11 = temp.x11 + input.x11; - output.x12 = temp.x12 + input.x12; - output.x13 = temp.x13 + input.x13; - output.x14 = temp.x14 + input.x14; - output.x15 = temp.x15 + input.x15; - } - } - - /*internal static void SalsaCore(int[] output, int outputOffset, int[] input, int inputOffset, int rounds) - { - if (rounds % 2 != 0) - throw new ArgumentException("rounds must be even"); - } - - -static void store_littleendian(unsigned char *x,uint32 u) -{ - x[0] = u; u >>= 8; - x[1] = u; u >>= 8; - x[2] = u; u >>= 8; - x[3] = u; -} - - internal static void HSalsaCore(int[] output, int outputOffset, int[] input, int inputOffset, int rounds) - { - if (rounds % 2 != 0) - throw new ArgumentException("rounds must be even"); - static uint32 rotate(uint32 u,int c) -{ - return (u << c) | (u >> (32 - c)); -} - - - -int crypto_core( - unsigned char *out, - const unsigned char *in, - const unsigned char *k, - const unsigned char *c -) -{ - uint32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15; - int i; - - x0 = load_littleendian(c + 0); - x1 = load_littleendian(k + 0); - x2 = load_littleendian(k + 4); - x3 = load_littleendian(k + 8); - x4 = load_littleendian(k + 12); - x5 = load_littleendian(c + 4); - x6 = load_littleendian(in + 0); - x7 = load_littleendian(in + 4); - x8 = load_littleendian(in + 8); - x9 = load_littleendian(in + 12); - x10 = load_littleendian(c + 8); - x11 = load_littleendian(k + 16); - x12 = load_littleendian(k + 20); - x13 = load_littleendian(k + 24); - x14 = load_littleendian(k + 28); - x15 = load_littleendian(c + 12); - - for (i = ROUNDS;i > 0;i -= 2) { - x4 ^= rotate( x0+x12, 7); - x8 ^= rotate( x4+ x0, 9); - x12 ^= rotate( x8+ x4,13); - x0 ^= rotate(x12+ x8,18); - x9 ^= rotate( x5+ x1, 7); - x13 ^= rotate( x9+ x5, 9); - x1 ^= rotate(x13+ x9,13); - x5 ^= rotate( x1+x13,18); - x14 ^= rotate(x10+ x6, 7); - x2 ^= rotate(x14+x10, 9); - x6 ^= rotate( x2+x14,13); - x10 ^= rotate( x6+ x2,18); - x3 ^= rotate(x15+x11, 7); - x7 ^= rotate( x3+x15, 9); - x11 ^= rotate( x7+ x3,13); - x15 ^= rotate(x11+ x7,18); - x1 ^= rotate( x0+ x3, 7); - x2 ^= rotate( x1+ x0, 9); - x3 ^= rotate( x2+ x1,13); - x0 ^= rotate( x3+ x2,18); - x6 ^= rotate( x5+ x4, 7); - x7 ^= rotate( x6+ x5, 9); - x4 ^= rotate( x7+ x6,13); - x5 ^= rotate( x4+ x7,18); - x11 ^= rotate(x10+ x9, 7); - x8 ^= rotate(x11+x10, 9); - x9 ^= rotate( x8+x11,13); - x10 ^= rotate( x9+ x8,18); - x12 ^= rotate(x15+x14, 7); - x13 ^= rotate(x12+x15, 9); - x14 ^= rotate(x13+x12,13); - x15 ^= rotate(x14+x13,18); - } - - store_littleendian(out + 0,x0); - store_littleendian(out + 4,x5); - store_littleendian(out + 8,x10); - store_littleendian(out + 12,x15); - store_littleendian(out + 16,x6); - store_littleendian(out + 20,x7); - store_littleendian(out + 24,x8); - store_littleendian(out + 28,x9); - - return 0; -}*/ - - } -} diff --git a/src/Renci.SshNet/Security/Chaos.NaCl/Internal/Salsa/replace regex.txt b/src/Renci.SshNet/Security/Chaos.NaCl/Internal/Salsa/replace regex.txt deleted file mode 100644 index f4856899e..000000000 --- a/src/Renci.SshNet/Security/Chaos.NaCl/Internal/Salsa/replace regex.txt +++ /dev/null @@ -1,2 +0,0 @@ -x(\d+) ^= rotate\((.+), (\d+)\); -y = \2;\r\nx\1 ^= (y << \3) | (y >> (32 - \3)); \ No newline at end of file diff --git a/src/Renci.SshNet/Security/Chaos.NaCl/MontgomeryCurve25519.cs b/src/Renci.SshNet/Security/Chaos.NaCl/MontgomeryCurve25519.cs index 2b99ce55c..cbf5dd844 100644 --- a/src/Renci.SshNet/Security/Chaos.NaCl/MontgomeryCurve25519.cs +++ b/src/Renci.SshNet/Security/Chaos.NaCl/MontgomeryCurve25519.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using Renci.SshNet.Security.Chaos.NaCl.Internal; using Renci.SshNet.Security.Chaos.NaCl.Internal.Ed25519Ref10; -using Renci.SshNet.Security.Chaos.NaCl.Internal.Salsa; namespace Renci.SshNet.Security.Chaos.NaCl { @@ -12,131 +11,5 @@ internal static class MontgomeryCurve25519 { internal static readonly int PublicKeySizeInBytes = 32; internal static readonly int PrivateKeySizeInBytes = 32; - internal static readonly int SharedKeySizeInBytes = 32; - - internal static byte[] GetPublicKey(byte[] privateKey) - { - if (privateKey == null) - throw new ArgumentNullException("privateKey"); - if (privateKey.Length != PrivateKeySizeInBytes) - throw new ArgumentException("privateKey.Length must be 32"); - var publicKey = new byte[32]; - GetPublicKey(new ArraySegment(publicKey), new ArraySegment(privateKey)); - return publicKey; - } - - static readonly byte[] _basePoint = new byte[32] - { - 9, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0 ,0, 0, 0, 0, 0, - 0, 0, 0 ,0, 0, 0, 0, 0, - 0, 0, 0 ,0, 0, 0, 0, 0 - }; - - internal static void GetPublicKey(ArraySegment publicKey, ArraySegment privateKey) - { - if (publicKey.Array == null) - throw new ArgumentNullException("publicKey.Array"); - if (privateKey.Array == null) - throw new ArgumentNullException("privateKey.Array"); - if (publicKey.Count != PublicKeySizeInBytes) - throw new ArgumentException("privateKey.Count must be 32"); - if (privateKey.Count != PrivateKeySizeInBytes) - throw new ArgumentException("privateKey.Count must be 32"); - - // hack: abusing publicKey as temporary storage - // todo: remove hack - for (int i = 0; i < 32; i++) - { - publicKey.Array[publicKey.Offset + i] = privateKey.Array[privateKey.Offset + i]; - } - ScalarOperations.sc_clamp(publicKey.Array, publicKey.Offset); - - GroupElementP3 A; - GroupOperations.ge_scalarmult_base(out A, publicKey.Array, publicKey.Offset); - FieldElement publicKeyFE; - EdwardsToMontgomeryX(out publicKeyFE, ref A.Y, ref A.Z); - FieldOperations.fe_tobytes(publicKey.Array, publicKey.Offset, ref publicKeyFE); - } - - // hashes like the Curve25519 paper says - internal static void KeyExchangeOutputHashCurve25519Paper(byte[] sharedKey, int offset) - { - //c = Curve25519output - const UInt32 c0 = 'C' | 'u' << 8 | 'r' << 16 | (UInt32)'v' << 24; - const UInt32 c1 = 'e' | '2' << 8 | '5' << 16 | (UInt32)'5' << 24; - const UInt32 c2 = '1' | '9' << 8 | 'o' << 16 | (UInt32)'u' << 24; - const UInt32 c3 = 't' | 'p' << 8 | 'u' << 16 | (UInt32)'t' << 24; - - Array16 salsaState; - salsaState.x0 = c0; - salsaState.x1 = ByteIntegerConverter.LoadLittleEndian32(sharedKey, offset + 0); - salsaState.x2 = 0; - salsaState.x3 = ByteIntegerConverter.LoadLittleEndian32(sharedKey, offset + 4); - salsaState.x4 = ByteIntegerConverter.LoadLittleEndian32(sharedKey, offset + 8); - salsaState.x5 = c1; - salsaState.x6 = ByteIntegerConverter.LoadLittleEndian32(sharedKey, offset + 12); - salsaState.x7 = 0; - salsaState.x8 = 0; - salsaState.x9 = ByteIntegerConverter.LoadLittleEndian32(sharedKey, offset + 16); - salsaState.x10 = c2; - salsaState.x11 = ByteIntegerConverter.LoadLittleEndian32(sharedKey, offset + 20); - salsaState.x12 = ByteIntegerConverter.LoadLittleEndian32(sharedKey, offset + 24); - salsaState.x13 = 0; - salsaState.x14 = ByteIntegerConverter.LoadLittleEndian32(sharedKey, offset + 28); - salsaState.x15 = c3; - SalsaCore.Salsa(out salsaState, ref salsaState, 20); - - ByteIntegerConverter.StoreLittleEndian32(sharedKey, offset + 0, salsaState.x0); - ByteIntegerConverter.StoreLittleEndian32(sharedKey, offset + 4, salsaState.x1); - ByteIntegerConverter.StoreLittleEndian32(sharedKey, offset + 8, salsaState.x2); - ByteIntegerConverter.StoreLittleEndian32(sharedKey, offset + 12, salsaState.x3); - ByteIntegerConverter.StoreLittleEndian32(sharedKey, offset + 16, salsaState.x4); - ByteIntegerConverter.StoreLittleEndian32(sharedKey, offset + 20, salsaState.x5); - ByteIntegerConverter.StoreLittleEndian32(sharedKey, offset + 24, salsaState.x6); - ByteIntegerConverter.StoreLittleEndian32(sharedKey, offset + 28, salsaState.x7); - } - - private static readonly byte[] _zero16 = new byte[16]; - - // hashes like the NaCl paper says instead i.e. HSalsa(x,0) - internal static void KeyExchangeOutputHashNaCl(byte[] sharedKey, int offset) - { - Salsa20.HSalsa20(sharedKey, offset, sharedKey, offset, _zero16, 0); - } - - internal static byte[] KeyExchange(byte[] publicKey, byte[] privateKey) - { - var sharedKey = new byte[SharedKeySizeInBytes]; - KeyExchange(new ArraySegment(sharedKey), new ArraySegment(publicKey), new ArraySegment(privateKey)); - return sharedKey; - } - - internal static void KeyExchange(ArraySegment sharedKey, ArraySegment publicKey, ArraySegment privateKey) - { - if (sharedKey.Array == null) - throw new ArgumentNullException("sharedKey.Array"); - if (publicKey.Array == null) - throw new ArgumentNullException("publicKey.Array"); - if (privateKey.Array == null) - throw new ArgumentNullException("privateKey"); - if (sharedKey.Count != 32) - throw new ArgumentException("sharedKey.Count != 32"); - if (publicKey.Count != 32) - throw new ArgumentException("publicKey.Count != 32"); - if (privateKey.Count != 32) - throw new ArgumentException("privateKey.Count != 32"); - MontgomeryOperations.scalarmult(sharedKey.Array, sharedKey.Offset, privateKey.Array, privateKey.Offset, publicKey.Array, publicKey.Offset); - KeyExchangeOutputHashNaCl(sharedKey.Array, sharedKey.Offset); - } - - internal static void EdwardsToMontgomeryX(out FieldElement montgomeryX, ref FieldElement edwardsY, ref FieldElement edwardsZ) - { - FieldElement tempX, tempZ; - FieldOperations.fe_add(out tempX, ref edwardsZ, ref edwardsY); - FieldOperations.fe_sub(out tempZ, ref edwardsZ, ref edwardsY); - FieldOperations.fe_invert(out tempZ, ref tempZ); - FieldOperations.fe_mul(out montgomeryX, ref tempX, ref tempZ); - } } } diff --git a/src/Renci.SshNet/Security/Chaos.NaCl/Sha512.cs b/src/Renci.SshNet/Security/Chaos.NaCl/Sha512.cs index f68d6ab8b..5044e5259 100644 --- a/src/Renci.SshNet/Security/Chaos.NaCl/Sha512.cs +++ b/src/Renci.SshNet/Security/Chaos.NaCl/Sha512.cs @@ -24,13 +24,6 @@ public void Init() _totalBytes = 0; } - public void Update(ArraySegment data) - { - if (data.Array == null) - throw new ArgumentNullException("data.Array"); - Update(data.Array, data.Offset, data.Count); - } - public void Update(byte[] data, int offset, int count) { if (data == null) @@ -117,11 +110,6 @@ public byte[] Finish() return result; } - internal static byte[] Hash(byte[] data) - { - return Hash(data, 0, data.Length); - } - internal static byte[] Hash(byte[] data, int offset, int count) { var hasher = new Sha512();