예제 #1
0
        /// <summary>
        /// Check that this is correct public key.
        /// </summary>
        /// <remarks>
        /// This method can be used to verify that public and private key are on the curve.
        /// </remarks>
        public static void Validate(GXPublicKey publicKey)
        {
            if (publicKey == null)
            {
                throw new ArgumentNullException("Invalid public key.");
            }
            GXByteBuffer bb = new GXByteBuffer();

            bb.Set(publicKey.RawValue);
            int          size  = SchemeSize(publicKey.Scheme);
            GXBigInteger x     = new GXBigInteger(bb.SubArray(1, size));
            GXBigInteger y     = new GXBigInteger(bb.SubArray(1 + size, size));
            GXCurve      curve = new GXCurve(publicKey.Scheme);

            y.Multiply(y);
            y.Mod(curve.P);

            GXBigInteger tmpX = new GXBigInteger(x);

            tmpX.Multiply(x);
            tmpX.Mod(curve.P);
            tmpX.Add(curve.A);
            tmpX.Multiply(x);
            tmpX.Add(curve.B);
            tmpX.Mod(curve.P);
            if (y.Compare(tmpX) != 0)
            {
                throw new ArgumentException("Public key validate failed. Public key is not valid ECDSA public key.");
            }
        }
예제 #2
0
 public void Inv(GXBigInteger value)
 {
     if (!IsZero)
     {
         GXBigInteger lm  = new GXBigInteger(1);
         GXBigInteger hm  = new GXBigInteger(0);
         GXBigInteger low = new GXBigInteger(this);
         low.Mod(value);
         GXBigInteger high = new GXBigInteger(value);
         while (!(low.IsZero || low.IsOne))
         {
             GXBigInteger r = new GXBigInteger(high);
             r.Div(low);
             GXBigInteger tmp = new GXBigInteger(lm);
             tmp.Multiply(r);
             GXBigInteger nm = new GXBigInteger(hm);
             nm.Sub(tmp);
             tmp = new GXBigInteger(low);
             tmp.Multiply(r);
             high.Sub(tmp);
             // lm, low, hm, high = nm, new, lm, low
             tmp  = low;
             low  = new GXBigInteger(high);
             high = tmp;
             hm   = new GXBigInteger(lm);
             lm   = new GXBigInteger(nm);
         }
         Data     = lm.Data;
         negative = lm.negative;
         Mod(value);
     }
 }
예제 #3
0
        /// <summary>
        /// Multily elliptic curve point and scalar.
        /// </summary>
        /// <remarks>
        /// Y^2 = X^3 + A*X + B (mod p)
        /// </remarks>
        /// <param name="eccSize"></param>
        /// <param name="p">Point to multiply</param>
        /// <param name="n">Scalar to multiply</param>
        /// <param name="N">Elliptic curve order.</param>
        /// <param name="A"></param>
        /// <param name="P">Prime number</param>
        internal static GXEccPoint JacobianMultiply(GXEccPoint p, GXBigInteger n, GXBigInteger N, GXBigInteger A, GXBigInteger P)
        {
            GXBigInteger tmp;

            if (p.y.IsZero || n.IsZero)
            {
                return(new GXEccPoint(0, 0, 1));
            }
            if (n.IsOne)
            {
                return(p);
            }
            if (n.Compare(0) == -1 || n.Compare(N) != -1)
            {
                tmp = new GXBigInteger(n);
                tmp.Mod(N);
                return(JacobianMultiply(p, tmp, N, A, P));
            }
            if (n.IsEven)
            {
                tmp = new GXBigInteger(n);
                tmp.Rshift(1);
                return(JacobianDouble(JacobianMultiply(p, tmp, N, A, P), A, P));
            }
            tmp = new GXBigInteger(n);
            tmp.Rshift(1);
            GXEccPoint p2 = JacobianDouble(JacobianMultiply(p, tmp, N, A, P), A, P);

            JacobianAdd(p2, p, A, P);
            return(p2);
        }
예제 #4
0
        /// <summary>
        /// Verify that signature matches the data.
        /// </summary>
        /// <param name="signature">Generated signature.</param>
        /// <param name="data">Data to valuate.</param>
        /// <returns></returns>
        public bool Verify(byte[] signature, byte[] data)
        {
            GXBigInteger msg;

            if (PublicKey == null)
            {
                if (PrivateKey == null)
                {
                    throw new ArgumentNullException("Invalid private key.");
                }
                PublicKey = PrivateKey.GetPublicKey();
            }
            if (PublicKey.Scheme == Ecc.P256)
            {
                using (SHA256 sha = new SHA256CryptoServiceProvider())
                {
                    msg = new GXBigInteger(sha.ComputeHash(data));
                }
            }
            else
            {
                using (SHA384 sha = new SHA384CryptoServiceProvider())
                {
                    msg = new GXBigInteger(sha.ComputeHash(data));
                }
            }
            GXByteBuffer pk   = new GXByteBuffer(PublicKey.RawValue);
            GXByteBuffer bb   = new GXByteBuffer(signature);
            int          size = SchemeSize(PublicKey.Scheme);
            GXBigInteger sigR = new GXBigInteger(bb.SubArray(0, size));
            GXBigInteger sigS = new GXBigInteger(bb.SubArray(size, size));
            GXBigInteger inv  = sigS;

            inv.Inv(curve.N);
            // Calculate u1 and u2.
            GXEccPoint u1 = new GXEccPoint(curve.G.x, curve.G.y, new GXBigInteger(1));
            GXEccPoint u2 = new GXEccPoint(new GXBigInteger(pk.SubArray(1, size)),
                                           new GXBigInteger(pk.SubArray(1 + size, size)), new GXBigInteger(1));
            GXBigInteger n = msg;

            n.Multiply(inv);
            n.Mod(curve.N);
            Multiply(u1, n, curve.N, curve.A, curve.P);
            n = new GXBigInteger(sigR);
            n.Multiply(inv);
            n.Mod(curve.N);
            Multiply(u2, n, curve.N, curve.A, curve.P);
            u1.z = new GXBigInteger(1);
            u2.z = new GXBigInteger(1);
            JacobianAdd(u1, u2, curve.A, curve.P);
            FromJacobian(u1, curve.P);
            return(sigR.Compare(u1.x) == 0);
        }
예제 #5
0
        /// <summary>
        /// Convert ECC point to Jacobian.
        /// </summary>
        /// <param name="p">ECC point.</param>
        /// <param name="A"></param>
        /// <param name="P">Prime number.</param>
        /// <returns></returns>
        private static GXEccPoint JacobianDouble(GXEccPoint p, GXBigInteger A, GXBigInteger P)
        {
            GXBigInteger ysq = new GXBigInteger(p.y);

            ysq.Multiply(p.y);
            ysq.Mod(P);
            GXBigInteger S = new GXBigInteger(p.x);

            S.Multiply(new GXBigInteger(4));
            S.Multiply(ysq);
            S.Mod(P);
            GXBigInteger M = new GXBigInteger(p.x);

            M.Multiply(p.x);
            M.Multiply(new GXBigInteger(3));
            GXBigInteger tmp = new GXBigInteger(p.z);

            tmp.Multiply(p.z);
            tmp.Multiply(p.z);
            tmp.Multiply(p.z);
            tmp.Multiply(A);
            M.Add(tmp);
            M.Mod(P);
            //nx
            GXBigInteger nx = new GXBigInteger(M);

            nx.Multiply(M);
            tmp = new GXBigInteger(S);
            tmp.Multiply(new GXBigInteger(2));
            nx.Sub(tmp);
            nx.Mod(P);
            //ny
            GXBigInteger ny = new GXBigInteger(S);

            ny.Sub(nx);
            ny.Multiply(M);
            tmp = new GXBigInteger(ysq);
            tmp.Multiply(ysq);
            tmp.Multiply(new GXBigInteger(8));
            ny.Sub(tmp);
            ny.Mod(P);
            //nz
            GXBigInteger nz = new GXBigInteger(p.y);

            nz.Multiply(p.z);
            nz.Multiply(new GXBigInteger(2));
            nz.Mod(P);
            return(new GXEccPoint(nx, ny, nz));
        }
예제 #6
0
        /// <summary>
        /// Sign given data using public and private key.
        /// </summary>
        /// <param name="data">Data to sign.</param>
        /// <returns>Signature</returns>
        public byte[] Sign(byte[] data)
        {
            if (PrivateKey == null)
            {
                throw new ArgumentException("Invalid private key.");
            }
            GXBigInteger msg;

            if (PrivateKey.Scheme == Ecc.P256)
            {
                using (SHA256 sha = new SHA256CryptoServiceProvider())
                {
                    msg = new GXBigInteger(sha.ComputeHash(data));
                }
            }
            else
            {
                using (SHA384 sha = new SHA384CryptoServiceProvider())
                {
                    msg = new GXBigInteger(sha.ComputeHash(data));
                }
            }
            GXBigInteger pk = new GXBigInteger(PrivateKey.RawValue);
            GXEccPoint   p;
            GXBigInteger n;
            GXBigInteger r;
            GXBigInteger s;

            do
            {
                n = GetRandomNumber(PrivateKey.Scheme);
                p = new GXEccPoint(curve.G.x, curve.G.y, new GXBigInteger(1));
                Multiply(p, n, curve.N, curve.A, curve.P);
                r = p.x;
                r.Mod(curve.N);
                n.Inv(curve.N);
                //s
                s = new GXBigInteger(r);
                s.Multiply(pk);
                s.Add(msg);
                s.Multiply(n);
                s.Mod(curve.N);
            } while (r.IsZero || s.IsZero);
            GXByteBuffer signature = new GXByteBuffer();

            signature.Set(r.ToArray());
            signature.Set(s.ToArray());
            return(signature.Array());
        }
예제 #7
0
        /// <summary>
        /// Verify that signature matches the data.
        /// </summary>
        /// <param name="signature">Generated signature.</param>
        /// <param name="data">Data to valuate.</param>
        /// <returns></returns>
        public bool Verify(byte[] signature, byte[] data)
        {
            GXBigInteger msg;

            using (SHA256 sha = new SHA256CryptoServiceProvider())
            {
                msg = new GXBigInteger(sha.ComputeHash(data));
            }
            if (PublicKey == null)
            {
                PublicKey = PrivateKey.GetPublicKey();
            }
            GXByteBuffer pk   = new GXByteBuffer(PublicKey.RawValue);
            GXByteBuffer bb   = new GXByteBuffer(signature);
            GXBigInteger sigR = new GXBigInteger(bb.SubArray(0, 32));
            GXBigInteger sigS = new GXBigInteger(bb.SubArray(32, 32));
            GXBigInteger inv  = sigS;

            inv.Inv(curve.N);
            // Calculate u1 and u2.
            GXEccPoint   u1 = new GXEccPoint(curve.G.x, curve.G.y, new GXBigInteger(1));
            GXEccPoint   u2 = new GXEccPoint(new GXBigInteger(pk.SubArray(1, 32)), new GXBigInteger(pk.SubArray(33, 32)), new GXBigInteger(1));
            GXBigInteger n  = msg;

            n.Multiply(inv);
            n.Mod(curve.N);
            Multiply(u1, n, curve.N, curve.A, curve.P);
            n = new GXBigInteger(sigR);
            n.Multiply(inv);
            n.Mod(curve.N);
            Multiply(u2, n, curve.N, curve.A, curve.P);
            //  add = Math.add(u1, u2, P = curve.P, A = curve.A)
            u1.z = new GXBigInteger(1);
            u2.z = new GXBigInteger(1);
            JacobianAdd(u1, u2, curve.A, curve.P);
            FromJacobian(u1, curve.P);
            return(sigR.Compare(u1.x) == 0);
        }
예제 #8
0
        /// <summary>
        /// Sign given data using public and private key.
        /// </summary>
        /// <param name="data">Data to sign.</param>
        /// <returns>Signature</returns>
        public byte[] Sign(byte[] data)
        {
            if (PrivateKey == null)
            {
                throw new ArgumentException("Invalid private key.");
            }
            GXBigInteger msg;

            using (SHA256 sha = new SHA256CryptoServiceProvider())
            {
                msg = new GXBigInteger(sha.ComputeHash(data));
            }
            GXBigInteger pk = new GXBigInteger(PrivateKey.RawValue);
            GXEccPoint   p;
            GXBigInteger n = new GXBigInteger(10);
            GXBigInteger r;
            GXBigInteger s;

            do
            {
                if (CustomRandomNumber != null)
                {
                    n = CustomRandomNumber;
                }
                else
                {
                    n = GetRandomNumber(PrivateKey.Scheme);
                }
                p = new GXEccPoint(curve.G.x, curve.G.y, new GXBigInteger(1));
                Multiply(p, n, curve.N, curve.A, curve.P);
                r = p.x;
                r.Mod(curve.N);
                n.Inv(curve.N);
                //s
                s = new GXBigInteger(r);
                s.Multiply(pk);
                s.Add(msg);
                s.Multiply(n);
                s.Mod(curve.N);
            } while (r.IsZero || s.IsZero);

            byte recoveryId;

            if (p.y.IsOne)
            {
                recoveryId = 1;
            }
            else
            {
                recoveryId = 0;
            }
            if (p.y.Compare(curve.N) == 1)
            {
                recoveryId += 2;
            }
            GXByteBuffer signature = new GXByteBuffer();

            signature.Set(r.ToArray());
            signature.Set(s.ToArray());
            return(signature.Array());
        }
예제 #9
0
 /// <summary>
 /// Y^2 = X^3 + A*X + B (mod p)
 /// </summary>
 /// <param name="p"></param>
 /// <param name="q"></param>
 /// <param name="A"></param>
 /// <param name="P">Prime number</param>
 private static void JacobianAdd(GXEccPoint p, GXEccPoint q, GXBigInteger A, GXBigInteger P)
 {
     if (!(p.y.IsZero || q.y.IsZero))
     {
         GXBigInteger U1 = new GXBigInteger(p.x);
         U1.Multiply(q.z);
         U1.Multiply(q.z);
         U1.Mod(P);
         GXBigInteger U2 = new GXBigInteger(p.z);
         U2.Multiply(p.z);
         U2.Multiply(q.x);
         U2.Mod(P);
         GXBigInteger S1 = new GXBigInteger(p.y);
         S1.Multiply(q.z);
         S1.Multiply(q.z);
         S1.Multiply(q.z);
         S1.Mod(P);
         GXBigInteger S2 = new GXBigInteger(q.y);
         S2.Multiply(p.z);
         S2.Multiply(p.z);
         S2.Multiply(p.z);
         S2.Mod(P);
         if (U1.Compare(U2) == 0)
         {
             if (S1.Compare(S2) != 0)
             {
                 p.x = p.y = new GXBigInteger(0);
                 p.z = new GXBigInteger(1);
             }
             else
             {
                 p.x = A;
                 p.y = P;
             }
         }
         //H
         GXBigInteger H = U2;
         H.Sub(U1);
         //R
         GXBigInteger R = S2;
         R.Sub(S1);
         GXBigInteger H2 = new GXBigInteger(H);
         H2.Multiply(H);
         H2.Mod(P);
         GXBigInteger H3 = new GXBigInteger(H);
         H3.Multiply(H2);
         H3.Mod(P);
         GXBigInteger U1H2 = new GXBigInteger(U1);
         U1H2.Multiply(H2);
         U1H2.Mod(P);
         GXBigInteger tmp = new GXBigInteger(2);
         tmp.Multiply(U1H2);
         //nx
         GXBigInteger nx = new GXBigInteger(R);
         nx.Multiply(R);
         nx.Sub(H3);
         nx.Sub(tmp);
         nx.Mod(P);
         //ny
         GXBigInteger ny = R;
         tmp = new GXBigInteger(U1H2);
         tmp.Sub(nx);
         ny.Multiply(tmp);
         tmp = new GXBigInteger(S1);
         tmp.Multiply(H3);
         ny.Sub(tmp);
         ny.Mod(P);
         //nz
         GXBigInteger nz = H;
         nz.Multiply(p.z);
         nz.Multiply(q.z);
         nz.Mod(P);
         p.x = nx;
         p.y = ny;
         p.z = nz;
     }
 }