Пример #1
0
        public virtual BigInteger CalculateAgreement(
            ICipherParameters pubKey)
        {
            ECPublicKeyParameters pub = (ECPublicKeyParameters)pubKey;
            ECDomainParameters    dp  = privKey.Parameters;

            if (!dp.Equals(pub.Parameters))
            {
                throw new InvalidOperationException("ECDHC public key has wrong domain parameters");
            }

            BigInteger hd = dp.H.Multiply(privKey.D).Mod(dp.N);

            // Always perform calculations on the exact curve specified by our private key's parameters
            ECPoint pubPoint = ECAlgorithms.CleanPoint(dp.Curve, pub.Q);

            if (pubPoint.IsInfinity)
            {
                throw new InvalidOperationException("Infinity is not a valid public key for ECDHC");
            }

            ECPoint P = pubPoint.Multiply(hd).Normalize();

            if (P.IsInfinity)
            {
                throw new InvalidOperationException("Infinity is not a valid agreement value for ECDHC");
            }

            return(P.AffineXCoord.ToBigInteger());
        }
 protected bool Equals(ECKeyParameters other)
 {
     if (parameters.Equals(other.parameters))
     {
         return(Equals((AsymmetricKeyParameter)other));
     }
     return(false);
 }
Пример #3
0
        private ECPoint CalculatePoint(ECPublicKeyParameters pubKeyParams)
        {
            ECDomainParameters dp = keyParam.Parameters;

            if (!dp.Equals(pubKeyParams.Parameters))
            {
                throw new InvalidOperationException(
                          "ECDH public key has wrong domain parameters"
                          );
            }

            BigInteger d = keyParam.D;

            ECPoint q = dp.Curve.DecodePoint(pubKeyParams.Q.GetEncoded(true));

            if (q.IsInfinity)
            {
                throw new InvalidOperationException(
                          "Infinity is not a valid public key for ECDH"
                          );
            }

            BigInteger h = dp.H;

            if (!h.Equals(BigInteger.One))
            {
                d = dp.H.ModInverse(dp.N).Multiply(d).Mod(dp.N);
                q = ECAlgorithms.ReferenceMultiply(q, h);
            }

            ECPoint p = q.Multiply(d).Normalize();

            if (p.IsInfinity)
            {
                throw new InvalidOperationException(
                          "Infinity is not a valid agreement value for ECDH"
                          );
            }

            return(p);
        }
Пример #4
0
        public virtual BigInteger CalculateAgreement(
            ICipherParameters pubKey)
        {
            ECPublicKeyParameters pub        = (ECPublicKeyParameters)pubKey;
            ECDomainParameters    parameters = pub.Parameters;

            if (!parameters.Equals(privKey.Parameters))
            {
                throw new InvalidOperationException("ECDHC public key has wrong domain parameters");
            }

            BigInteger hd = parameters.H.Multiply(privKey.D).Mod(parameters.N);

            ECPoint P = pub.Q.Multiply(hd).Normalize();

            if (P.IsInfinity)
            {
                throw new InvalidOperationException("Infinity is not a valid agreement value for ECDHC");
            }

            return(P.AffineXCoord.ToBigInteger());
        }
Пример #5
0
        public virtual BigInteger CalculateAgreement(
            ICipherParameters pubKey)
        {
            MqvPublicParameters pubParams = (MqvPublicParameters)pubKey;

            ECPrivateKeyParameters staticPrivateKey = privParams.StaticPrivateKey;
            ECDomainParameters     parameters       = staticPrivateKey.Parameters;

            if (!parameters.Equals(pubParams.StaticPublicKey.Parameters))
            {
                throw new InvalidOperationException("ECMQV public key components have wrong domain parameters");
            }

            ECPoint agreement = CalculateMqvAgreement(parameters, staticPrivateKey,
                                                      privParams.EphemeralPrivateKey, privParams.EphemeralPublicKey,
                                                      pubParams.StaticPublicKey, pubParams.EphemeralPublicKey).Normalize();

            if (agreement.IsInfinity)
            {
                throw new InvalidOperationException("Infinity is not a valid agreement value for MQV");
            }

            return(agreement.AffineXCoord.ToBigInteger());
        }
Пример #6
0
 public static bool AreOnSameCurve(ECDomainParameters a, ECDomainParameters b)
 {
     return(a != null && a.Equals(b));
 }
Пример #7
0
 public static bool AreOnSameCurve(ECDomainParameters a, ECDomainParameters b) =>
 ((a != null) && a.Equals(b));