Пример #1
0
        public void GenerateKeyFromPrivateKeyTest()
        {
            var key    = X25519KeyAgreement.GenerateKeyPair();
            var newKey = X25519KeyAgreement.GenerateKeyFromPrivateKey(key.PrivateKey);

            Assert.IsTrue(key.PublicKey.SequenceEqual(newKey.PublicKey));
        }
Пример #2
0
        public void GenerateKeyFromPrivateKeyTest3()
        {
            var priv = Convert.FromBase64String("aF9hmPSeJfKvjPam++gl7MRIQydQQu2Jdee8zOTX+lY=");
            var pub  = Convert.FromBase64String("FTM52WXsEjj5hBY53RTUFmG2qUwzZxPRJdYs9lu/y3M=");
            var key  = X25519KeyAgreement.GenerateKeyFromPrivateKey(priv);

            Assert.IsTrue(key.PublicKey.SequenceEqual(pub));
        }
Пример #3
0
        static void Main(string[] args)
        {
            // the number of tests
            const int tests     = 1000;
            Stopwatch stopwatch = new Stopwatch();

            // simple key agreement
            {
                stopwatch.Start();
                // generate keys
                var k1 = X25519KeyAgreement.GenerateKeyPair();
                var k2 = X25519KeyAgreement.GenerateKeyPair();

                // generate shared secret
                var shared1 = X25519KeyAgreement.Agreement(k1.PrivateKey, k2.PublicKey);
                var shared2 = X25519KeyAgreement.Agreement(k2.PrivateKey, k1.PublicKey);
                stopwatch.Stop();
                // print them to make sure they are identical
                Console.WriteLine(Convert.ToBase64String(shared1));
                Console.WriteLine(Convert.ToBase64String(shared2));
                Console.WriteLine($"Full key agreement done in {stopwatch.Elapsed}");
            }
            stopwatch.Reset();
            // benchmark key generation
            {
                X25519KeyPair key;
                stopwatch.Start();
                for (int i = 0; i < tests; i++)
                {
                    key = X25519KeyAgreement.GenerateKeyPair();
                }
                stopwatch.Stop();
                Console.WriteLine($"Key generation done in {stopwatch.Elapsed}. That is about " +
                                  $"{Math.Round((double)tests/stopwatch.Elapsed.Milliseconds * 1000)} keys/sec");
            }
            stopwatch.Reset();
            // benchmark agreement
            {
                X25519KeyPair[] alice = new X25519KeyPair[tests];
                X25519KeyPair[] bob   = new X25519KeyPair[tests];
                // I want to benchmark key agreement not key generation
                for (int i = 0; i < tests; i++)
                {
                    alice[i] = X25519KeyAgreement.GenerateKeyPair();
                    bob[i]   = X25519KeyAgreement.GenerateKeyPair();
                }
                stopwatch.Start();
                for (int i = 0; i < tests; i++)
                {
                    X25519KeyAgreement.Agreement(alice[i].PrivateKey, bob[i].PublicKey);
                }
                stopwatch.Stop();
                Console.WriteLine($"Key agreements done in {stopwatch.Elapsed}. That is about " +
                                  $"{Math.Round((double)tests/stopwatch.Elapsed.Milliseconds * 1000)} agreement/sec");
                stopwatch.Reset();
            }
        }
Пример #4
0
        public void AgreementTest()
        {
            // generate keys
            var k1 = X25519KeyAgreement.GenerateKeyPair();
            var k2 = X25519KeyAgreement.GenerateKeyPair();

            // calculate secrets
            var shared1 = X25519KeyAgreement.Agreement(k1.PrivateKey, k2.PublicKey);
            var shared2 = X25519KeyAgreement.Agreement(k2.PrivateKey, k1.PublicKey);

            // check if shared1 and 2 are same
            Assert.IsTrue(shared1.SequenceEqual(shared2));
        }
Пример #5
0
        public void GenerateKeyFromPrivateKeyTest2()
        {
            // predefined values
            var          key1   = X25519KeyAgreement.GenerateKeyFromPrivateKey(Convert.FromBase64String("sECe8YYQT/bODurKruM8QpGFBTahurW8GqxFL+AYiW8="));
            var          key2   = X25519KeyAgreement.GenerateKeyFromPrivateKey(Convert.FromBase64String("wAidyKs9iF+KA1cgBxa1rMtPwemOLFHqSIe5nkVRN2o="));
            const string secret = "dbfEcOMjYactMkh33DRhg0h1VCbmhxoWt6AR3rp6000=";
            // do the agreement
            string secret1 = Convert.ToBase64String(X25519KeyAgreement.Agreement(key1.PrivateKey, key2.PublicKey));
            string secret2 = Convert.ToBase64String(X25519KeyAgreement.Agreement(key2.PrivateKey, key1.PublicKey));

            if (secret1 != secret2)
            {
                Assert.Fail("Secrets does not match");
            }
            // check the final secret
            Assert.AreEqual(secret, secret1);
        }
Пример #6
0
        public void HugeAgreementTest()
        {
            const int rounds = 1000;

            for (int i = 0; i < rounds; i++)
            {
                // generate keys
                var k1 = X25519KeyAgreement.GenerateKeyPair();
                var k2 = X25519KeyAgreement.GenerateKeyPair();

                // calculate secrets
                var shared1 = X25519KeyAgreement.Agreement(k1.PrivateKey, k2.PublicKey);
                var shared2 = X25519KeyAgreement.Agreement(k2.PrivateKey, k1.PublicKey);

                // check if shared1 and 2 are same
                if (!shared1.SequenceEqual(shared2))
                {
                    Assert.Fail($"Two shared secrets are not equal:\nk1:{k1.PrivateKey} , {k1.PublicKey}" +
                                $"\nk2: {k2.PrivateKey} , {k2.PublicKey}");
                }
            }
        }
Пример #7
0
 /// <summary>
 /// Generates the ECDH shared secret between two Ed25519 public keys and returns it.
 /// </summary>
 /// <param name="a">An Ed25519 private key.</param>
 /// <param name="b">An Ed25519 public key.</param>
 public static byte[] SharedSecret(ECPrivKeyModel a, ECPubKeyModel b)
 {
     return(X25519KeyAgreement.Agreement(a.key, b.key));
 }
Пример #8
0
 public void GenerateTest()
 {
     X25519KeyAgreement.GenerateKeyPair();
 }
Пример #9
0
        /// <summary>
        /// Generates a new Ed25519 <see cref="GenericECKeyPairModel"/> and returns it.
        /// </summary>
        public static GenericECKeyPairModel GenerateKeyPair()
        {
            X25519KeyPair pair = X25519KeyAgreement.GenerateKeyPair();

            return(new GenericECKeyPairModel(new ECPrivKeyModel(pair.PrivateKey), new ECPubKeyModel(pair.PublicKey)));
        }