| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | using Renci.SshNet.Security.Org.BouncyCastle.Math; |
| | | 4 | | using Renci.SshNet.Security.Org.BouncyCastle.Math.EC; |
| | | 5 | | using Renci.SshNet.Security.Org.BouncyCastle.Crypto.Parameters; |
| | | 6 | | |
| | | 7 | | namespace Renci.SshNet.Security.Org.BouncyCastle.Crypto.Agreement |
| | | 8 | | { |
| | | 9 | | internal class ECDHCBasicAgreement |
| | | 10 | | { |
| | | 11 | | private ECPrivateKeyParameters privKey; |
| | | 12 | | |
| | | 13 | | public virtual void Init( |
| | | 14 | | AsymmetricKeyParameter parameters) |
| | 9 | 15 | | { |
| | 9 | 16 | | this.privKey = (ECPrivateKeyParameters)parameters; |
| | 9 | 17 | | } |
| | | 18 | | |
| | | 19 | | public virtual int GetFieldSize() |
| | 0 | 20 | | { |
| | 0 | 21 | | return (privKey.Parameters.Curve.FieldSize + 7) / 8; |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | public virtual BigInteger CalculateAgreement( |
| | | 25 | | ECPublicKeyParameters pubKey) |
| | 9 | 26 | | { |
| | 9 | 27 | | ECPublicKeyParameters pub = pubKey; |
| | 9 | 28 | | ECDomainParameters dp = privKey.Parameters; |
| | 9 | 29 | | if (!dp.Equals(pub.Parameters)) |
| | 0 | 30 | | throw new InvalidOperationException("ECDHC public key has wrong domain parameters"); |
| | | 31 | | |
| | 9 | 32 | | BigInteger hd = dp.H.Multiply(privKey.D).Mod(dp.N); |
| | | 33 | | |
| | | 34 | | // Always perform calculations on the exact curve specified by our private key's parameters |
| | 9 | 35 | | ECPoint pubPoint = ECAlgorithms.CleanPoint(dp.Curve, pub.Q); |
| | 9 | 36 | | if (pubPoint.IsInfinity) |
| | 0 | 37 | | throw new InvalidOperationException("Infinity is not a valid public key for ECDHC"); |
| | | 38 | | |
| | 9 | 39 | | ECPoint P = pubPoint.Multiply(hd).Normalize(); |
| | 9 | 40 | | if (P.IsInfinity) |
| | 0 | 41 | | throw new InvalidOperationException("Infinity is not a valid agreement value for ECDHC"); |
| | | 42 | | |
| | 9 | 43 | | return P.AffineXCoord.ToBigInteger(); |
| | 9 | 44 | | } |
| | | 45 | | } |
| | | 46 | | } |