Beispiel #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(byte[] Data, byte[] PublicKey, HashFunction 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);

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

                int    c   = Data.Length;
                byte[] Bin = new byte[(ScalarBytes << 1) + c];              // dom2(F, C) = blank string
                Array.Copy(R, 0, Bin, 0, ScalarBytes);
                Array.Copy(PublicKey, 0, Bin, ScalarBytes, ScalarBytes);
                Array.Copy(Data, 0, Bin, ScalarBytes << 1, c);              // PH(M)=M

                byte[] h = HashFunction(Bin);

                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);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Gets a shared key using the Elliptic Curve Diffie-Hellman (ECDH) algorithm.
        /// </summary>
        /// <param name="LocalPrivateKey">Local private key.</param>
        /// <param name="RemotePublicKey">Public key of the remote party.</param>
        /// <param name="HashFunction">A Hash function is applied to the derived key to generate the shared secret.
        /// The derived key, as a byte array of equal size as the order of the prime field, ordered by most significant byte first,
        /// is passed on to the hash function before being returned as the shared key.</param>
        /// <param name="Curve">Elliptic curve used.</param>
        /// <returns>Shared secret.</returns>
        public static byte[] GetSharedKey(byte[] LocalPrivateKey, byte[] RemotePublicKey,
                                          HashFunction HashFunction, EllipticCurve Curve)
        {
            PointOnCurve PublicKey = Curve.Decode(RemotePublicKey);
            PointOnCurve P         = Curve.ScalarMultiplication(LocalPrivateKey, PublicKey, true);

            byte[] B = P.X.ToByteArray();

            if (B.Length != Curve.OrderBytes)
            {
                Array.Resize <byte>(ref B, Curve.OrderBytes);
            }

            Array.Reverse(B);   // Most significant byte first.

            return(HashFunction(B));
        }
Beispiel #3
0
        /// <summary>
        /// Doubles a point on the curve.
        /// </summary>
        /// <param name="P">Point</param>
        public override void Double(ref PointOnCurve P)
        {
            if (!P.IsHomogeneous)
            {
                P.Z = BigInteger.One;
            }

            BigInteger A = this.modP.Add(P.X, P.Y);
            BigInteger B = this.modP.Multiply(A, A);
            BigInteger C = this.modP.Multiply(P.X, P.X);
            BigInteger D = this.modP.Multiply(P.Y, P.Y);
            BigInteger E = this.modP.Add(C, D);
            BigInteger H = this.modP.Multiply(P.Z, P.Z);
            BigInteger J = this.modP.Subtract(E, H << 1);

            P.X = this.modP.Multiply(B - E, J);
            P.Y = this.modP.Multiply(E, C - D);
            P.Z = this.modP.Multiply(E, J);
        }
Beispiel #4
0
        /// <summary>
        /// Encodes a point on the curve.
        /// </summary>
        /// <param name="Point">Normalized point to encode.</param>
        /// <returns>Encoded point.</returns>
        public virtual byte[] Encode(PointOnCurve Point)
        {
            byte[] X = Point.X.ToByteArray();
            byte[] Y = Point.Y.ToByteArray();

            if (X.Length != this.orderBytes)
            {
                Array.Resize <byte>(ref X, this.orderBytes);
            }

            if (Y.Length != this.orderBytes)
            {
                Array.Resize <byte>(ref Y, this.orderBytes);
            }

            byte[] Result = new byte[this.orderBytes << 1];

            Array.Copy(X, 0, Result, 0, this.orderBytes);
            Array.Copy(Y, 0, Result, this.orderBytes, this.orderBytes);

            return(Result);
        }
Beispiel #5
0
        /// <summary>
        /// Performs the scalar multiplication of <paramref name="N"/>*<paramref name="P"/>.
        /// </summary>
        /// <param name="N">Scalar, in binary, little-endian form.</param>
        /// <param name="P">Point</param>
        /// <param name="Normalize">If normalized output is expected.</param>
        /// <returns><paramref name="N"/>*<paramref name="P"/></returns>
        public virtual PointOnCurve ScalarMultiplication(byte[] N, PointOnCurve P, bool Normalize)
        {
            PointOnCurve Result = this.Zero;
            int          i, c = N.Length;
            byte         b, Bit;

            for (i = 0; i < c; i++)
            {
                b = N[i];

                for (Bit = 1; Bit != 0; Bit <<= 1)
                {
                    if ((b & Bit) != 0)
                    {
                        this.AddTo(ref Result, P);
                    }

                    this.Double(ref P);
                }
            }

            return(Result);
        }
Beispiel #6
0
        /// <summary>
        /// Encodes a point on the curve in accordance with §5.1.2 of RFC 8032.
        /// </summary>
        /// <param name="P">Point</param>
        /// <param name="Curve">Edwards curve.</param>
        /// <returns>Encoding</returns>
        public static byte[] Encode(PointOnCurve P, EdwardsCurveBase Curve)
        {
            int ScalarBits  = Curve.CoordinateBits;
            int ScalarBytes = (ScalarBits + 9) >> 3;

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

            byte[] x   = P.X.ToByteArray();
            int    Msb = (ScalarBits + 1) & 7;

            byte Mask = (byte)(0xff >> (8 - Msb));

            y[ScalarBytes - 1] &= Mask;

            if ((x[0] & 1) != 0)
            {
                y[ScalarBytes - 1] |= 0x80;     // Always MSB
            }
            return(y);
        }
Beispiel #7
0
 /// <summary>
 /// Abstract base class for elliptic curves.
 /// </summary>
 /// <param name="BasePoint">Base-point.</param>
 /// <param name="Order">Order of base-point.</param>
 /// <param name="Cofactor">Cofactor of curve.</param>
 public EllipticCurve(PointOnCurve BasePoint, BigInteger Order, int Cofactor)
     : this(BasePoint, Order, Cofactor, null)
 {
 }
Beispiel #8
0
 /// <summary>
 /// Doubles a point on the curve.
 /// </summary>
 /// <param name="P">Point</param>
 public abstract void Double(ref PointOnCurve P);
Beispiel #9
0
 /// <summary>
 /// Adds <paramref name="Q"/> to <paramref name="P"/>.
 /// </summary>
 /// <param name="P">Point 1.</param>
 /// <param name="Q">Point 2.</param>
 /// <returns>P+Q</returns>
 public abstract void AddTo(ref PointOnCurve P, PointOnCurve Q);
Beispiel #10
0
 /// <summary>
 /// Performs the scalar multiplication of <paramref name="N"/>*<paramref name="P"/>.
 /// </summary>
 /// <param name="N">Scalar</param>
 /// <param name="P">Point</param>
 /// <param name="Normalize">If normalized output is expected.</param>
 /// <returns><paramref name="N"/>*<paramref name="P"/></returns>
 public PointOnCurve ScalarMultiplication(BigInteger N, PointOnCurve P, bool Normalize)
 {
     return(this.ScalarMultiplication(N.ToByteArray(), P, Normalize));
 }
Beispiel #11
0
 /// <summary>
 /// Encodes a point on the curve.
 /// </summary>
 /// <param name="Point">Normalized point to encode.</param>
 /// <returns>Encoded point.</returns>
 public override byte[] Encode(PointOnCurve Point)
 {
     return(EdDSA.Encode(Point, this));
 }
Beispiel #12
0
 /// <summary>
 /// Base class of Twisted Edwards curves (-x²+y²=1+dx²y²) over a prime field.
 /// </summary>
 /// <param name="Prime">Prime base of field.</param>
 /// <param name="BasePoint">Base-point in (X,Y) coordinates.</param>
 /// <param name="d">Edwards curve coefficient</param>
 /// <param name="Order">Order of base-point.</param>
 /// <param name="Cofactor">Cofactor of curve.</param>
 public EdwardsCurveBase(BigInteger Prime, PointOnCurve BasePoint,
                         BigInteger d, BigInteger Order, int Cofactor)
     : this(Prime, BasePoint, d, Order, Cofactor, null)
 {
 }
Beispiel #13
0
 /// <summary>
 /// Negates a point on the curve.
 /// </summary>
 /// <param name="P">Point</param>
 public void Negate(ref PointOnCurve P)
 {
     P.X = this.p - P.X;
 }
Beispiel #14
0
 /// <summary>
 /// Base class of Elliptic curves over a prime field.
 /// </summary>
 /// <param name="Prime">Prime base of field.</param>
 /// <param name="BasePoint">Base-point.</param>
 /// <param name="Order">Order of base-point.</param>
 /// <param name="Cofactor">Cofactor of curve.</param>
 public PrimeFieldCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger Order,
                        int Cofactor)
     : this(Prime, BasePoint, Order, Cofactor, null)
 {
 }
Beispiel #15
0
 /// <summary>
 /// Base class of Edwards curves (x²+y²=1+dx²y²) over a prime field.
 /// </summary>
 /// <param name="Prime">Prime base of field.</param>
 /// <param name="BasePoint">Base-point in (X,Y) coordinates.</param>
 /// <param name="d">Coefficient in the curve equation (x²+y²=1+dx²y²)</param>
 /// <param name="Order">Order of base-point.</param>
 /// <param name="Cofactor">Cofactor of curve.</param>
 /// <param name="Secret">Secret.</param>
 public EdwardsCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger d,
                     BigInteger Order, int Cofactor, byte[] Secret)
     : base(Prime, BasePoint, d, Order, Cofactor, Secret)
 {
     this.p34 = (this.p - 3) / 4;
 }