Exemple #1
0
        private static void CheckIterated(int count)
        {
            byte[] k = new byte[32]; k[0] = 9;
            byte[] u = new byte[32]; u[0] = 9;
            byte[] r = new byte[32];

            int iterations = 0;

            while (iterations < count)
            {
                X25519.ScalarMult(k, 0, u, 0, r, 0);

                Array.Copy(k, 0, u, 0, 32);
                Array.Copy(r, 0, k, 0, 32);

                switch (++iterations)
                {
                case 1:
                    CheckValue(k, "Iterated @1", "422c8e7a6227d7bca1350b3e2bb7279f7897b87bb6854b783c60e80311ae3079");
                    break;

                case 1000:
                    CheckValue(k, "Iterated @1000", "684cf59ba83309552800ef566f2f4d3c1c3887c49360e3875f2eb94d99532c51");
                    break;

                case 1000000:
                    CheckValue(k, "Iterated @1000000", "7c3911e0ab2586fd864497297e575e6f3bc601c0883c30df5f4dd2d24f665424");
                    break;

                default:
                    break;
                }
            }
        }
Exemple #2
0
        public void TestECDH()
        {
            byte[] kA = new byte[32];
            byte[] kB = new byte[32];
            byte[] qA = new byte[32];
            byte[] qB = new byte[32];
            byte[] sA = new byte[32];
            byte[] sB = new byte[32];

            for (int i = 1; i <= 100; ++i)
            {
                // Each party generates an ephemeral private key, ...
                Random.NextBytes(kA);
                Random.NextBytes(kB);

                // ... publishes their public key, ...
                X25519.ScalarMultBase(kA, 0, qA, 0);
                X25519.ScalarMultBase(kB, 0, qB, 0);

                // ... computes the shared secret, ...
                X25519.ScalarMult(kA, 0, qB, 0, sA, 0);
                X25519.ScalarMult(kB, 0, qA, 0, sB, 0);

                // ... which is the same for both parties.
                //Assert.IsTrue(Arrays.AreEqual(sA, sB), "ECDH #" + i);
                if (!Arrays.AreEqual(sA, sB))
                {
                    Console.WriteLine(" " + i);
                }
            }
        }
Exemple #3
0
 private static void CheckX25519Vector(string sk, string su, string se, string text)
 {
     byte[] k = Hex.Decode(sk);
     byte[] u = Hex.Decode(su);
     byte[] r = new byte[32];
     X25519.ScalarMult(k, 0, u, 0, r, 0);
     CheckValue(r, text, se);
 }
Exemple #4
0
        private static void CheckX25519Vector(string sk, string su, string se, string text)
        {
            byte[] k = Hex.Decode(sk);
            Assert.AreEqual(X25519.ScalarSize, k.Length);

            byte[] u = Hex.Decode(su);
            Assert.AreEqual(X25519.PointSize, u.Length);

            byte[] r = new byte[X25519.PointSize];
            X25519.ScalarMult(k, 0, u, 0, r, 0);
            CheckValue(r, text, se);
        }
Exemple #5
0
        public void TestConsistency()
        {
            byte[] u  = new byte[32];    u[0] = 9;
            byte[] k  = new byte[32];
            byte[] rF = new byte[32];
            byte[] rV = new byte[32];

            for (int i = 1; i <= 100; ++i)
            {
                Random.NextBytes(k);
                X25519.ScalarMultBase(k, 0, rF, 0);
                X25519.ScalarMult(k, 0, u, 0, rV, 0);
                Assert.IsTrue(Arrays.AreEqual(rF, rV), "Consistency #" + i);
            }
        }
Exemple #6
0
        private static void CheckECDHVector(string sA, string sAPub, string sB, string sBPub, string sK, string text)
        {
            byte[] a = Hex.Decode(sA);
            byte[] b = Hex.Decode(sB);

            byte[] aPub = new byte[32];
            X25519.ScalarMultBase(a, 0, aPub, 0);
            CheckValue(aPub, text, sAPub);

            byte[] bPub = new byte[32];
            X25519.ScalarMultBase(b, 0, bPub, 0);
            CheckValue(bPub, text, sBPub);

            byte[] aK = new byte[32];
            X25519.ScalarMult(a, 0, bPub, 0, aK, 0);
            CheckValue(aK, text, sK);

            byte[] bK = new byte[32];
            X25519.ScalarMult(b, 0, aPub, 0, bK, 0);
            CheckValue(bK, text, sK);
        }
Exemple #7
0
        private static void CheckECDHVector(string sA, string sAPub, string sB, string sBPub, string sK, string text)
        {
            byte[] a = Hex.Decode(sA);
            Assert.AreEqual(X25519.ScalarSize, a.Length);

            byte[] b = Hex.Decode(sB);
            Assert.AreEqual(X25519.ScalarSize, b.Length);

            byte[] aPub = new byte[X25519.PointSize];
            X25519.ScalarMultBase(a, 0, aPub, 0);
            CheckValue(aPub, text, sAPub);

            byte[] bPub = new byte[X25519.PointSize];
            X25519.ScalarMultBase(b, 0, bPub, 0);
            CheckValue(bPub, text, sBPub);

            byte[] aK = new byte[X25519.PointSize];
            X25519.ScalarMult(a, 0, bPub, 0, aK, 0);
            CheckValue(aK, text, sK);

            byte[] bK = new byte[X25519.PointSize];
            X25519.ScalarMult(b, 0, aPub, 0, bK, 0);
            CheckValue(bK, text, sK);
        }
Exemple #8
0
 public void GenerateSecret(X25519PublicKeyParameters publicKey, byte[] buf, int off)
 {
     byte[] encoded = new byte[X25519.PointSize];
     publicKey.Encode(encoded, 0);
     X25519.ScalarMult(data, 0, encoded, 0, buf, off);
 }