private static ECPoint CalculateMqvAgreement(ECDomainParameters parameters, ECPrivateKeyParameters d1U, ECPrivateKeyParameters d2U, ECPublicKeyParameters Q2U, ECPublicKeyParameters Q1V, ECPublicKeyParameters Q2V)
        {
            BigInteger n     = parameters.N;
            int        num   = (n.BitLength + 1) / 2;
            BigInteger m     = BigInteger.One.ShiftLeft(num);
            ECCurve    curve = parameters.Curve;

            ECPoint[] array = new ECPoint[3]
            {
                ECAlgorithms.ImportPoint(curve, (Q2U == null) ? parameters.G.Multiply(d2U.D) : Q2U.Q),
                ECAlgorithms.ImportPoint(curve, Q1V.Q),
                ECAlgorithms.ImportPoint(curve, Q2V.Q)
            };
            curve.NormalizeAll(array);
            ECPoint    eCPoint     = array[0];
            ECPoint    p           = array[1];
            ECPoint    eCPoint2    = array[2];
            BigInteger bigInteger  = eCPoint.AffineXCoord.ToBigInteger();
            BigInteger bigInteger2 = bigInteger.Mod(m);
            BigInteger val         = bigInteger2.SetBit(num);
            BigInteger val2        = d1U.D.Multiply(val).Add(d2U.D).Mod(n);
            BigInteger bigInteger3 = eCPoint2.AffineXCoord.ToBigInteger();
            BigInteger bigInteger4 = bigInteger3.Mod(m);
            BigInteger bigInteger5 = bigInteger4.SetBit(num);
            BigInteger bigInteger6 = parameters.H.Multiply(val2).Mod(n);

            return(ECAlgorithms.SumOfTwoMultiplies(p, bigInteger5.Multiply(bigInteger6).Mod(n), eCPoint2, bigInteger6));
        }
        internal static ECPoint Validate(ECCurve c, ECPoint q)
        {
            if (q == null)
            {
                throw new ArgumentException("Point has null value", "q");
            }

            q = ECAlgorithms.ImportPoint(c, q).Normalize();

            if (q.IsInfinity)
            {
                throw new ArgumentException("Point at infinity", "q");
            }

            if (!q.IsValid())
            {
                throw new ArgumentException("Point not on curve", "q");
            }

            return(q);
        }
        internal static ECPoint ValidatePublicPoint(ECCurve c, ECPoint q)
        {
            if (null == q)
            {
                throw new ArgumentNullException("q", "Point cannot be null");
            }

            q = ECAlgorithms.ImportPoint(c, q).Normalize();

            if (q.IsInfinity)
            {
                throw new ArgumentException("Point at infinity", "q");
            }

            if (!q.IsValid())
            {
                throw new ArgumentException("Point not on curve", "q");
            }

            return(q);
        }
Example #4
0
        // The ECMQV Primitive as described in SEC-1, 3.4
        private static ECPoint CalculateMqvAgreement(
            ECDomainParameters parameters,
            ECPrivateKeyParameters d1U,
            ECPrivateKeyParameters d2U,
            ECPublicKeyParameters Q2U,
            ECPublicKeyParameters Q1V,
            ECPublicKeyParameters Q2V)
        {
            BigInteger n    = parameters.N;
            int        e    = (n.BitLength + 1) / 2;
            BigInteger powE = BigInteger.One.ShiftLeft(e);

            ECCurve curve = parameters.Curve;

            ECPoint[] points = new ECPoint[] {
                // The Q2U public key is optional - but will be calculated for us if it wasn't present
                ECAlgorithms.ImportPoint(curve, Q2U.Q),
                ECAlgorithms.ImportPoint(curve, Q1V.Q),
                ECAlgorithms.ImportPoint(curve, Q2V.Q)
            };

            curve.NormalizeAll(points);

            ECPoint q2u = points[0], q1v = points[1], q2v = points[2];

            BigInteger x      = q2u.AffineXCoord.ToBigInteger();
            BigInteger xBar   = x.Mod(powE);
            BigInteger Q2UBar = xBar.SetBit(e);
            BigInteger s      = d1U.D.Multiply(Q2UBar).Add(d2U.D).Mod(n);

            BigInteger xPrime    = q2v.AffineXCoord.ToBigInteger();
            BigInteger xPrimeBar = xPrime.Mod(powE);
            BigInteger Q2VBar    = xPrimeBar.SetBit(e);

            BigInteger hs = parameters.H.Multiply(s).Mod(n);

            return(ECAlgorithms.SumOfTwoMultiplies(
                       q1v, Q2VBar.Multiply(hs).Mod(n), q2v, hs));
        }