Ejemplo n.º 1
0
        private ECPoint CalculatePoint(ECPublicKeyParameters pubKeyParams)
        {
            var dp = keyParam.Parameters;

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

            var d = keyParam.D;

            ECPoint Q = ECAlgorithms.CleanPoint(dp.Curve, pubKeyParams.Q);

            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.HInv.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);
        }
Ejemplo n.º 2
0
        /**
         * Checks, if the point multiplication algorithm of the given point yields
         * the same result as point multiplication done by the reference
         * implementation given in <code>multiply()</code>. This method chooses a
         * random number by which the given point <code>p</code> is multiplied.
         *
         * @param p
         *            The point to be multiplied.
         * @param numBits
         *            The bitlength of the random number by which <code>p</code>
         *            is multiplied.
         */
        private void ImplTestMultiply(ECPoint p, int numBits)
        {
            BigInteger k    = new BigInteger(numBits, secRand);
            ECPoint    reff = ECAlgorithms.ReferenceMultiply(p, k);
            ECPoint    q    = p.Multiply(k);

            AssertPointsEqual("ECPoint.Multiply is incorrect", reff, q);
        }
Ejemplo n.º 3
0
    protected internal bool SatisfiesCofactor()
    {
        BigInteger cofactor = Curve.Cofactor;

        if (cofactor != null && !cofactor.Equals(BigInteger.One))
        {
            return(!ECAlgorithms.ReferenceMultiply(this, cofactor).IsInfinity);
        }
        return(true);
    }
Ejemplo n.º 4
0
        /**
         * Checks, if the point multiplication algorithm of the given point yields
         * the same result as point multiplication done by the reference
         * implementation given in <code>multiply()</code>. This method tests
         * multiplication of <code>p</code> by every number of bitlength
         * <code>numBits</code> or less.
         *
         * @param p
         *            The point to be multiplied.
         * @param numBits
         *            Try every multiplier up to this bitlength
         */
        private void ImplTestMultiplyAll(ECPoint p, int numBits)
        {
            BigInteger bound = BigInteger.One.ShiftLeft(numBits);
            BigInteger k     = BigInteger.Zero;

            do
            {
                ECPoint reff = ECAlgorithms.ReferenceMultiply(p, k);
                ECPoint q    = p.Multiply(k);
                AssertPointsEqual("ECPoint.Multiply is incorrect", reff, q);
                k = k.Add(BigInteger.One);
            }while (k.CompareTo(bound) < 0);
        }
Ejemplo n.º 5
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);
        }
        public virtual BigInteger CalculateAgreement(
            ICipherParameters pubKey)
        {
            ECPublicKeyParameters pub = (ECPublicKeyParameters)pubKey;
            ECDomainParameters    dp  = privKey.Parameters;

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

            BigInteger d = privKey.D;

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

            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.HInv.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.AffineXCoord.ToBigInteger());
        }
Ejemplo n.º 7
0
        public void TestFixedPointMultiplier()
        {
            FixedPointCombMultiplier M = new FixedPointCombMultiplier();

            ArrayList names = new ArrayList();

            CollectionUtilities.AddRange(names, ECNamedCurveTable.Names);
            CollectionUtilities.AddRange(names, CustomNamedCurves.Names);

            ISet uniqNames = new HashSet(names);

            foreach (string name in uniqNames)
            {
                X9ECParameters x9A = ECNamedCurveTable.GetByName(name);
                X9ECParameters x9B = CustomNamedCurves.GetByName(name);

                X9ECParameters x9 = x9B != null ? x9B : x9A;

                for (int i = 0; i < TestsPerCurve; ++i)
                {
                    BigInteger k    = new BigInteger(x9.N.BitLength, Random);
                    ECPoint    pRef = ECAlgorithms.ReferenceMultiply(x9.G, k);

                    if (x9A != null)
                    {
                        ECPoint pA = M.Multiply(x9A.G, k);
                        AssertPointsEqual("Standard curve fixed-point failure", pRef, pA);
                    }

                    if (x9B != null)
                    {
                        ECPoint pB = M.Multiply(x9B.G, k);
                        AssertPointsEqual("Custom curve fixed-point failure", pRef, pB);
                    }
                }
            }
        }
Ejemplo n.º 8
0
 protected override ECPoint MultiplyPositive(ECPoint p, BigInteger k)
 {
     return(ECAlgorithms.ReferenceMultiply(p, k));
 }