示例#1
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);
                }
            }
        }
示例#2
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);
            }
        }
示例#3
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);
        }
示例#4
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);
        }
示例#5
0
 public X25519PublicKeyParameters GeneratePublicKey()
 {
     byte[] publicKey = new byte[X25519.PointSize];
     X25519.ScalarMultBase(data, 0, publicKey, 0);
     return(new X25519PublicKeyParameters(publicKey, 0));
 }