Exemplo n.º 1
0
        public void Test_02_Negate()
        {
            WeierstrassCurve C = new NistP256();
            int i;

            for (i = 0; i < 100; i++)
            {
                byte[]       k = C.GenerateSecret();
                PointOnCurve P = C.ScalarMultiplication(k, C.PublicKeyPoint, true);
                PointOnCurve Q = P;
                C.Negate(ref Q);
                C.AddTo(ref P, Q);
                Assert.IsFalse(P.NonZero);
            }
        }
Exemplo n.º 2
0
        public void Test_07_Coordinates_25519()
        {
            MontgomeryCurve C = new Curve25519();

            PointOnCurve UV = C.BasePoint;
            PointOnCurve XY = C.ToXY(UV);

            Assert.AreEqual("15112221349535400772501151409588531511454012693041857206046113283949847762202", XY.X.ToString());
            Assert.AreEqual("46316835694926478169428394003475163141307993866256225615783033603165251855960", XY.Y.ToString());

            PointOnCurve UV2 = C.ToUV(XY);

            Assert.AreEqual(UV.X, UV2.X);
            Assert.AreEqual(UV.Y, UV2.Y);
        }
Exemplo n.º 3
0
        private static byte[] GetSharedKey(Uri uri)
        {
            var curve = new NistP521();

            var coords = new Coordinates()
            {
                X = curve.PublicKey.X.ToString(),
                Y = curve.PublicKey.Y.ToString()
            };

            var json = Actions.WriteJson(coords);

            var response = Encoding.UTF8.GetString(Http.Post(uri, Encoding.UTF8.GetBytes(json)));

            var remoteCoords = Actions.ParseJson((response));
            var remotePoint  = new PointOnCurve(BigInteger.Parse(remoteCoords.X), BigInteger.Parse(remoteCoords.Y));

            return(curve.GetSharedKey(remotePoint, HashFunction.SHA256));
        }
Exemplo n.º 4
0
        private void TestEncoding(EdwardsCurveBase Curve)
        {
            int i;
            int NrErrors = 0;

            for (i = 0; i < 100; i++)
            {
                PointOnCurve P1 = Curve.PublicKeyPoint;

                byte[]       Encoded = EdDSA.Encode(P1, Curve);
                PointOnCurve P2      = EdDSA.Decode(Encoded, Curve);

                if (!P1.Equals(P2))
                {
                    NrErrors++;
                }
            }

            Assert.AreEqual(0, NrErrors);
        }
Exemplo n.º 5
0
        public void Test_08_Coordinates_448()
        {
            MontgomeryCurve C = new Curve448();

            PointOnCurve UV = C.BasePoint;
            PointOnCurve XY = C.ToXY(UV);

            Edwards448   C2  = new Edwards448();
            PointOnCurve XY4 = C2.ScalarMultiplication(4, C2.BasePoint, true);  // 4-isogeny

            Assert.AreEqual(XY4.X, XY.X);
            Assert.AreEqual(XY4.Y, XY.Y);

            PointOnCurve UV2 = C.ToUV(XY);
            PointOnCurve UV4 = C.ScalarMultiplication(4, C.BasePoint, true);

            Assert.AreEqual(UV4.X, UV2.X);

            BigInteger V4 = C.CalcV(UV4.X);

            Assert.IsTrue(V4 == UV2.Y || (C.Prime - V4) == UV2.Y);
        }
Exemplo n.º 6
0
        protected void EdwardsTwinTest <CurveType>(MontgomeryCurve C1, int Isogeny)
            where CurveType : EdwardsCurveBase
        {
            int Ok    = 0;
            int Error = 0;
            int i;

            for (i = 0; i < 100; i++)
            {
                try
                {
                    Console.Out.WriteLine(i);

                    CurveType C2 = C1.Pair as CurveType;

                    Assert.IsNotNull(C2);

                    PointOnCurve P1   = C1.PublicKeyPoint;
                    PointOnCurve P1_2 = C1.ToXY(P1);
                    PointOnCurve P2   = C2.PublicKeyPoint;

                    if (Isogeny != 1)
                    {
                        P2 = C2.ScalarMultiplication(Isogeny, P2, true);
                    }

                    Assert.AreEqual(P1_2.Y, P2.Y);

                    Ok++;
                }
                catch (Exception)
                {
                    Error++;
                }
            }

            Assert.AreEqual(0, Error);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Abstract base class for Elliptic Curve / AES-256 hybrid ciphers.s
 /// </summary>
 /// <param name="X">X-coordinate of remote public key.</param>
 /// <param name="Y">Y-coordinate of remote public key.</param>
 /// <param name="LocalEndpoint">Local security endpoint, if available.</param>
 public EcAes256(byte[] X, byte[] Y, EndpointSecurity LocalEndpoint)
     : base()
 {
     this.publicKey     = new PointOnCurve(FromNetwork(X), FromNetwork(Y));
     this.localEndpoint = LocalEndpoint;
 }