Ejemplo n.º 1
0
        /// <summary>
        /// Verifies a signature of <paramref name="Data"/> made by the EdDSA algorithm.
        /// </summary>
        /// <param name="Data">Payload to sign.</param>
        /// <param name="PublicKey">Public Key of the entity that generated the signature.</param>
        /// <param name="HashFunction">Hash function to use.</param>
        /// <param name="Curve">Elliptic curve</param>
        /// <param name="Signature">Signature</param>
        /// <returns>If the signature is valid.</returns>
        public static bool Verify(Stream Data, byte[] PublicKey, HashFunctionStream HashFunction,
                                  EdwardsCurveBase Curve, byte[] Signature)
        {
            try
            {
                int ScalarBytes = Signature.Length;
                if ((ScalarBytes & 1) != 0)
                {
                    return(false);
                }

                ScalarBytes >>= 1;

                byte[] R = new byte[ScalarBytes];
                Array.Copy(Signature, 0, R, 0, ScalarBytes);
                PointOnCurve r = Decode(R, Curve);
                byte[]       S = new byte[ScalarBytes];
                Array.Copy(Signature, ScalarBytes, S, 0, ScalarBytes);
                BigInteger s = EllipticCurve.ToInt(S);
                byte[]     h;

                if (s >= Curve.Order)
                {
                    return(false);
                }

                using (TemporaryFile TempFile = new TemporaryFile())        // dom2(F, C) = blank string
                {
                    TempFile.Write(R, 0, ScalarBytes);
                    TempFile.Write(PublicKey, 0, ScalarBytes);

                    Data.Position = 0;
                    Data.CopyTo(TempFile);                                  // PH(M)=M

                    TempFile.Position = 0;
                    h = HashFunction(TempFile);
                }

                BigInteger   k  = BigInteger.Remainder(EllipticCurve.ToInt(h), Curve.Order);
                PointOnCurve P1 = Curve.ScalarMultiplication(s, Curve.BasePoint, false);
                PointOnCurve P2 = Curve.ScalarMultiplication(k, Curve.Decode(PublicKey), false);
                Curve.AddTo(ref P2, r);

                P1.Normalize(Curve);
                P2.Normalize(Curve);

                return(P1.Equals(P2));
            }
            catch (ArgumentException)
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
        private byte[] H_dom4(Stream Data)
        {
            using (TemporaryFile TempFile = new TemporaryFile())
            {
                TempFile.Write(preamble, 0, 8);
                TempFile.WriteByte(0);              // x=phflag=0
                TempFile.WriteByte(0);              // y=context=empty string

                Data.Position = 0;
                Data.CopyTo(TempFile);

                TempFile.Position = 0;
                return(this.shake256_114.ComputeVariable(TempFile));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Signs data using the EdDSA algorithm.
        /// </summary>
        /// <param name="Data">Data to be signed.</param>
        /// <param name="PrivateKey">Private key.</param>
        /// <param name="Prefix">Prefix</param>
        /// <param name="HashFunction">Hash function to use</param>
        /// <param name="Curve">Elliptic curve</param>
        /// <returns>Signature</returns>
        public static byte[] Sign(Stream Data, byte[] PrivateKey, byte[] Prefix,
                                  HashFunctionStream HashFunction, EdwardsCurveBase Curve)
        {
            // 5.1.6 of RFC 8032

            int ScalarBytes = PrivateKey.Length;

            if (Prefix.Length != ScalarBytes)
            {
                throw new ArgumentException("Invalid prefix.", nameof(Prefix));
            }

            BigInteger   a = EllipticCurve.ToInt(PrivateKey);
            PointOnCurve P = Curve.ScalarMultiplication(PrivateKey, Curve.BasePoint, true);

            byte[] A = Encode(P, Curve);
            byte[] h;

            using (TemporaryFile TempFile = new TemporaryFile())    // dom2(F, C) = blank string
            {
                TempFile.Write(Prefix, 0, ScalarBytes);             // prefix

                Data.Position = 0;
                Data.CopyTo(TempFile);                              // PH(M)=M

                TempFile.Position = 0;
                h = HashFunction(TempFile);
            }

            BigInteger   r = BigInteger.Remainder(EllipticCurve.ToInt(h), Curve.Order);
            PointOnCurve R = Curve.ScalarMultiplication(r, Curve.BasePoint, true);

            byte[] Rs = Encode(R, Curve);

            using (TemporaryFile TempFile = new TemporaryFile())    // dom2(F, C) = blank string
            {
                TempFile.Write(Rs, 0, ScalarBytes);
                TempFile.Write(A, 0, ScalarBytes);

                Data.Position = 0;
                Data.CopyTo(TempFile);                              // PH(M)=M

                TempFile.Position = 0;
                h = HashFunction(TempFile);
            }

            BigInteger k = BigInteger.Remainder(EllipticCurve.ToInt(h), Curve.Order);
            BigInteger s = Curve.ModulusN.Add(r, Curve.ModulusN.Multiply(k, a));

            byte[] Bin = s.ToByteArray();
            if (Bin.Length != ScalarBytes)
            {
                Array.Resize <byte>(ref Bin, ScalarBytes);
            }

            byte[] Signature = new byte[ScalarBytes << 1];

            Array.Copy(Rs, 0, Signature, 0, ScalarBytes);
            Array.Copy(Bin, 0, Signature, ScalarBytes, ScalarBytes);

            return(Signature);
        }