Esempio n. 1
0
        public void TestValid(int length)
        {
            var contract  = SecureRandomGenerator.GetBytes(length);
            var signature = privateIdentity.Sign(contract);

            Assert.IsTrue(publicIdentity.Verify(contract, signature));
        }
Esempio n. 2
0
        public void TestShort()
        {
            var bytes   = SecureRandomGenerator.GetBytes(SymmetricCrypter.NONCE_SIZE + SymmetricCrypter.TAG_SIZE - 1);
            var crypter = new SymmetricCrypter(SecureRandomGenerator.GetBytes(32));

            Assert.IsTrue(crypter.Decrypt(bytes).Error is SymmetricCrypter.MessageTooShortError);
        }
Esempio n. 3
0
        public void TestChangedSignature(int length)
        {
            var contract  = SecureRandomGenerator.GetBytes(length);
            var signature = privateIdentity.Sign(contract);

            signature[0]++;
            Assert.IsFalse(publicIdentity.Verify(contract, signature));
        }
Esempio n. 4
0
        public void TestTampered()
        {
            var bytes     = SecureRandomGenerator.GetBytes(100);
            var encrypted = crypter.Encrypt(bytes);

            encrypted[0]++;
            Assert.IsTrue(crypter.Decrypt(encrypted).Error is SymmetricCrypter.TamperedMessageError);
        }
Esempio n. 5
0
        public async Task TestValid(int length)
        {
            var bytes   = SecureRandomGenerator.GetBytes(length);
            var sent    = a.Send(bytes);
            var message = await b.Receive();

            Assert.IsTrue(await sent);
            Assert.IsTrue(Enumerable.SequenceEqual <byte>(bytes, message.Value));
        }
Esempio n. 6
0
        public void TestSymmetry()
        {
            byte[] key = SecureRandomGenerator.GetBytes(32);
            var    a   = new KDFRatchet(key);
            var    b   = new KDFRatchet(key);

            for (int i = 0; i < 100; i++)
            {
                byte[] input   = SecureRandomGenerator.GetBytes(32);
                var    outputA = a.Turn(input);
                var    outputB = b.Turn(input);
                CollectionAssert.AreEqual(outputA, outputB);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Protects an insecure connection.
        /// </summary>
        /// <param name="initiator">Set to <see langword="true"/> if a client,
        /// <see langword="false"/> if a server.</param>
        public static async Task <ProtectedMessenger> Protect(SocketMessenger socketMessenger, bool initiator)
        {
            // Perform a key exchange.
            var exchanger = new KeyExchanger();
            var sent      = socketMessenger.Send(exchanger.Token);
            var token     = await socketMessenger.Receive().ConfigureAwait(false);

            if (token.IsError)
            {
                throw new Exception(token.Error.GetType().ToString());
            }
            var key = exchanger.KeyFromToken(token.Value);

            var result = await sent;

            if (result.IsError)
            {
                throw new Exception(result.Error.GetType().ToString());
            }

            // Send test data.
            var protectedMessenger = new ProtectedMessenger(socketMessenger, key, initiator);

            byte[] myValues = SecureRandomGenerator.GetBytes(32);
            var    a        = protectedMessenger.Send(myValues);

            // Echo bytes back.
            var integrityChallenge = await protectedMessenger.Receive().ConfigureAwait(false);

            var b = protectedMessenger.Send(integrityChallenge);
            await Task.WhenAll(a, b).ConfigureAwait(false);

            // Check integrity of test data.
            var integrityCheck = await protectedMessenger.Receive();

            if (!Enumerable.SequenceEqual <byte>(myValues, integrityCheck))
            {
                throw new Exception("Could not verify connection!");
            }

            // Return protected messenger, as the connection has been validated.
            return(protectedMessenger);
        }
Esempio n. 8
0
        public void TestInvalid(int length)
        {
            var bytes = SecureRandomGenerator.GetBytes(length);

            Assert.ThrowsExceptionAsync <ArgumentOutOfRangeException>(() => protected1.Send(bytes));
        }
Esempio n. 9
0
        public void TestCycle(int size)
        {
            var bytes = SecureRandomGenerator.GetBytes(size);

            CollectionAssert.AreEqual(bytes, crypter.Decrypt(crypter.Encrypt(bytes)).Value);
        }
Esempio n. 10
0
 public async Task TestInvalid(int length)
 {
     var bytes = SecureRandomGenerator.GetBytes(length);
     await Assert.ThrowsExceptionAsync <ArgumentOutOfRangeException>(() => a.Send(bytes));
 }