public string GetSign(byte[] msg)
    {
        //Org.BouncyCastle.Math.BigInteger d = new Org.BouncyCastle.Math.BigInteger(Convert.FromBase64String(privKey));
        byte[] prvB = StringToByteArray(prv);
        byte[] pubB = StringToByteArray(pub);
        //byte[] prvB = StringToByteArray(prvTmp);
        //byte[] pubB = StringToByteArray(pubTmp);
        Org.BouncyCastle.Math.BigInteger sk = new Org.BouncyCastle.Math.BigInteger(+1, prvB);
        Org.BouncyCastle.Math.EC.ECPoint q  = domain.G.Multiply(sk);

        byte[]     pubKeyX = q.Normalize().AffineXCoord.GetEncoded();
        byte[]     pubKeyY = q.Normalize().AffineYCoord.GetEncoded();
        BigInteger k       = GenerateRandom();

        //BigInteger k = new BigInteger(+1,StringToByteArray("015B931D9C7BF3A7A70E57868BF29712377E74355FC59032CD7547C80E179010"));
        //Debug.Log("kv:" + BitConverter.ToString(kv.ToByteArray()).Replace("-", ""));
        Debug.Log("K:" + BitConverter.ToString(k.ToByteArray()).Replace("-", ""));
        ECPoint Q = domain.G.Multiply(k);

        Org.BouncyCastle.Crypto.Digests.Sha256Digest digester = new Org.BouncyCastle.Crypto.Digests.Sha256Digest();
        byte[] h = new byte[digester.GetDigestSize()];

        digester.BlockUpdate(Q.GetEncoded(true), 0, Q.GetEncoded(true).Length);
        digester.BlockUpdate(pubB, 0, pubB.Length);
        digester.BlockUpdate(msg, 0, msg.Length);

        digester.DoFinal(h, 0);

        Org.BouncyCastle.Math.BigInteger r = new Org.BouncyCastle.Math.BigInteger(+1, h);

        BigInteger s = r.Multiply(sk);

        s = k.Subtract(s);
        s = s.Mod(domain.n);
        string rt = BitConverter.ToString(r.ToByteArray()).Replace("-", "");

        if (rt.Length > 32)
        {
            rt = rt.Remove(0, 2);
        }
        string st = BitConverter.ToString(s.ToByteArray()).Replace("-", "");

        return(rt + st);
    }
        public ECPublicKeyParameters(
            ECPoint				q,
            DerObjectIdentifier publicKeyParamSet)
            : base("ECGOST3410", false, publicKeyParamSet)
        {
            if (q == null)
                throw new ArgumentNullException("q");

            this.q = q.Normalize();
        }
        public ECPublicKeyParameters(
            string				algorithm,
            ECPoint				q,
            DerObjectIdentifier publicKeyParamSet)
            : base(algorithm, false, publicKeyParamSet)
        {
            if (q == null)
                throw new ArgumentNullException("q");

            this.q = q.Normalize();
        }
        public ECPublicKeyParameters(
            string				algorithm,
            ECPoint				q,
            ECDomainParameters	parameters)
            : base(algorithm, false, parameters)
        {
            if (q == null)
                throw new ArgumentNullException("q");

            this.q = q.Normalize();
        }
Example #5
0
        public virtual bool Equals(ECPoint other)
        {
            if (this == other)
            {
                return(true);
            }
            if (other == null)
            {
                return(false);
            }
            ECCurve curve       = Curve;
            ECCurve curve2      = other.Curve;
            bool    flag        = null == curve;
            bool    flag2       = null == curve2;
            bool    isInfinity  = IsInfinity;
            bool    isInfinity2 = other.IsInfinity;

            if (isInfinity || isInfinity2)
            {
                return(isInfinity && isInfinity2 && (flag || flag2 || curve.Equals(curve2)));
            }
            ECPoint eCPoint  = this;
            ECPoint eCPoint2 = other;

            if (!flag || !flag2)
            {
                if (flag)
                {
                    eCPoint2 = eCPoint2.Normalize();
                }
                else if (flag2)
                {
                    eCPoint = eCPoint.Normalize();
                }
                else
                {
                    if (!curve.Equals(curve2))
                    {
                        return(false);
                    }
                    ECPoint[] array = new ECPoint[2]
                    {
                        this,
                        curve.ImportPoint(eCPoint2)
                    };
                    curve.NormalizeAll(array);
                    eCPoint  = array[0];
                    eCPoint2 = array[1];
                }
            }
            return(eCPoint.XCoord.Equals(eCPoint2.XCoord) && eCPoint.YCoord.Equals(eCPoint2.YCoord));
        }
Example #6
0
 public virtual ECPoint ImportPoint(ECPoint p)
 {
     if (this == p.Curve)
     {
         return(p);
     }
     if (p.IsInfinity)
     {
         return(this.Infinity);
     }
     p = p.Normalize();
     return(this.ValidatePoint(p.XCoord.ToBigInteger(), p.YCoord.ToBigInteger(), p.IsCompressed));
 }
Example #7
0
        public virtual bool Equals(ECPoint other)
        {
            if (this == other)
            {
                return(true);
            }
            if (other == null)
            {
                return(false);
            }
            ECCurve curve      = this.Curve;
            ECCurve curve2     = other.Curve;
            bool    flag       = null == curve;
            bool    flag2      = null == curve2;
            bool    isInfinity = this.IsInfinity;
            bool    flag4      = other.IsInfinity;

            if (isInfinity || flag4)
            {
                return((isInfinity && flag4) && ((flag || flag2) || curve.Equals(curve2)));
            }
            ECPoint point = this;
            ECPoint p     = other;

            if (!flag || !flag2)
            {
                if (flag)
                {
                    p = p.Normalize();
                }
                else if (flag2)
                {
                    point = point.Normalize();
                }
                else
                {
                    if (!curve.Equals(curve2))
                    {
                        return(false);
                    }
                    ECPoint[] points = new ECPoint[] { this, curve.ImportPoint(p) };
                    curve.NormalizeAll(points);
                    point = points[0];
                    p     = points[1];
                }
            }
            return(point.XCoord.Equals(p.XCoord) && point.YCoord.Equals(p.YCoord));
        }
Example #8
0
        public virtual ECPoint ImportPoint(ECPoint p)
        {
            if (this == p.Curve)
            {
                return(p);
            }
            if (p.IsInfinity)
            {
                return(Infinity);
            }

            // TODO Default behaviour could be improved if the two curves have the same coordinate system by copying any Z coordinates.
            p = p.Normalize();

            return(ValidatePoint(p.XCoord.ToBigInteger(), p.YCoord.ToBigInteger(), p.IsCompressed));
        }
        public ECDomainParameters(
            ECCurve     curve,
            ECPoint     g,
            BigInteger  n,
            BigInteger  h,
            byte[]      seed)
        {
            if (curve == null)
                throw new ArgumentNullException("curve");
            if (g == null)
                throw new ArgumentNullException("g");
            if (n == null)
                throw new ArgumentNullException("n");
            if (h == null)
                throw new ArgumentNullException("h");

            this.curve = curve;
            this.g = g.Normalize();
            this.n = n;
            this.h = h;
            this.seed = Arrays.Clone(seed);
        }
Example #10
0
 public X9ECPoint(ECPoint p, bool compressed)
 {
     this.p = p.Normalize();
     this.encoding = new DerOctetString(p.GetEncoded(compressed));
 }
        public virtual ECPoint ImportPoint(ECPoint p)
        {
            if (this == p.Curve)
            {
                return p;
            }
            if (p.IsInfinity)
            {
                return Infinity;
            }

            // TODO Default behaviour could be improved if the two curves have the same coordinate system by copying any Z coordinates.
            p = p.Normalize();

            return ValidatePoint(p.XCoord.ToBigInteger(), p.YCoord.ToBigInteger(), p.IsCompressed);
        }
        public X9ECParameters(
            ECCurve		curve,
            ECPoint		g,
            BigInteger	n,
            BigInteger	h,
            byte[]		seed)
        {
            this.curve = curve;
            this.g = g.Normalize();
            this.n = n;
            this.h = h;
            this.seed = seed;

            if (ECAlgorithms.IsFpCurve(curve))
            {
                this.fieldID = new X9FieldID(curve.Field.Characteristic);
            }
            else if (ECAlgorithms.IsF2mCurve(curve))
            {
                IPolynomialExtensionField field = (IPolynomialExtensionField)curve.Field;
                int[] exponents = field.MinimalPolynomial.GetExponentsPresent();
                if (exponents.Length == 3)
                {
                    this.fieldID = new X9FieldID(exponents[2], exponents[1]);
                }
                else if (exponents.Length == 5)
                {
                    this.fieldID = new X9FieldID(exponents[4], exponents[1], exponents[2], exponents[3]);
                }
                else
                {
                    throw new ArgumentException("Only trinomial and pentomial curves are supported");
                }
            }
            else
            {
                throw new ArgumentException("'curve' is of an unsupported type");
            }
        }
Example #13
0
 public X9ECPoint(
     ECPoint p)
 {
     this.p = p.Normalize();
 }