public static NetXteaEncryption[] TestSRPWithRandomness(NetPeer peer)
        {
            int parallelism = (int)Math.Max(1, Environment.ProcessorCount * 0.75);

            var xtea = new NetXteaEncryption[2000 + 1000 * parallelism];

            Console.WriteLine(
                $"Testing SRP helper {xtea.Length} times (degree of parallelism: {parallelism})...");

            Parallel.For(
                0,
                xtea.Length,
                new ParallelOptions {
                MaxDegreeOfParallelism = parallelism
            },
                (i) =>
            {
                var salt = NetSRP.CreateRandomSalt();
                var x    = NetSRP.ComputePrivateKey("user", "password", salt);

                var v = NetSRP.ComputeServerVerifier(x);
                //Console.WriteLine("v = " + NetUtility.ToHexString(v));

                var a = NetSRP.CreateRandomEphemeral(); //  NetUtility.ToByteArray("393ed364924a71ba7258633cc4854d655ca4ec4e8ba833eceaad2511e80db2b5");
                var A = NetSRP.ComputeClientEphemeral(a);
                //Console.WriteLine("A = " + NetUtility.ToHexString(A));

                var b = NetSRP.CreateRandomEphemeral(); // NetUtility.ToByteArray("cc4d87a90db91067d52e2778b802ca6f7d362490c4be294b21b4a57c71cf55a9");
                var B = NetSRP.ComputeServerEphemeral(b, v);
                //Console.WriteLine("B = " + NetUtility.ToHexString(B));

                var u = NetSRP.ComputeU(A, B);
                //Console.WriteLine("u = " + NetUtility.ToHexString(u));

                var Ss = NetSRP.ComputeServerSessionValue(A, v, u, b);
                //Console.WriteLine("Ss = " + NetUtility.ToHexString(Ss));

                var Sc = NetSRP.ComputeClientSessionValue(B, x, u, a);
                //Console.WriteLine("Sc = " + NetUtility.ToHexString(Sc));

                if (Ss != Sc)
                {
                    throw new LidgrenException("SRP non matching session values!");
                }

                xtea[i] = NetSRP.CreateEncryption(peer, Ss);
            });

            return(xtea);
        }
Ejemplo n.º 2
0
        public void TestNetSRP()
        {
            byte[] salt = NetSRP.CreateRandomSalt();
            byte[] x    = NetSRP.ComputePrivateKey("user", "password", salt);

            byte[] v = NetSRP.ComputeServerVerifier(x);
            //Console.WriteLine("v = " + NetUtility.ToHexString(v));

            byte[] a = NetSRP
                       .CreateRandomEphemeral(); //  NetUtility.ToByteArray("393ed364924a71ba7258633cc4854d655ca4ec4e8ba833eceaad2511e80db2b5");

            byte[] A = NetSRP.ComputeClientEphemeral(a);
            //Console.WriteLine("A = " + NetUtility.ToHexString(A));

            byte[] b = NetSRP
                       .CreateRandomEphemeral(); // NetUtility.ToByteArray("cc4d87a90db91067d52e2778b802ca6f7d362490c4be294b21b4a57c71cf55a9");

            byte[] B = NetSRP.ComputeServerEphemeral(b, v);
            //Console.WriteLine("B = " + NetUtility.ToHexString(B));

            byte[] u = NetSRP.ComputeU(A, B);
            //Console.WriteLine("u = " + NetUtility.ToHexString(u));

            byte[] Ss = NetSRP.ComputeServerSessionValue(A, v, u, b);
            //Console.WriteLine("Ss = " + NetUtility.ToHexString(Ss));

            byte[] Sc = NetSRP.ComputeClientSessionValue(B, x, u, a);
            //Console.WriteLine("Sc = " + NetUtility.ToHexString(Sc));

            if (Ss.Length != Sc.Length)
            {
                Assert.Fail("SRP non matching lengths!");
            }

            for (int j = 0; j < Ss.Length; j++)
            {
                if (Ss[j] != Sc[j])
                {
                    Assert.Fail("SRP non matching session values!");
                }
            }

            var test = NetSRP.CreateEncryption(Peer, Ss);
        }
Ejemplo n.º 3
0
        public static void Run(NetPeer peer)
        {
            //
            // Test encryption
            //
            List <INetEncryption> algos = new List <INetEncryption>();

            algos.Add(new NetXorEncryption("TopSecret"));
            algos.Add(new NetXtea("TopSecret"));
            algos.Add(new NetAESEncryption("TopSecret"));
            algos.Add(new NetRC2Encryption("TopSecret"));
            algos.Add(new NetDESEncryption("TopSecret"));
            algos.Add(new NetTripleDESEncryption("TopSecret"));

            foreach (var algo in algos)
            {
                NetOutgoingMessage om = peer.CreateMessage();
                om.Write("Hallon");
                om.Write(42);
                om.Write(5, 5);
                om.Write(true);
                om.Write("kokos");
                int trueLen = om.LengthBits;
                om.Encrypt(algo);

                // convert to incoming message
                NetIncomingMessage im = Program.CreateIncomingMessage(om.PeekDataBuffer(), om.LengthBits);
                im.Decrypt(algo);

                if (im.LengthBits != trueLen)
                {
                    throw new NetException("Length fail");
                }

                if (im.ReadString() != "Hallon")
                {
                    throw new NetException("fail");
                }
                if (im.ReadInt32() != 42)
                {
                    throw new NetException("fail");
                }
                if (im.ReadInt32(5) != 5)
                {
                    throw new NetException("fail");
                }
                if (im.ReadBoolean() != true)
                {
                    throw new NetException("fail");
                }
                if (im.ReadString() != "kokos")
                {
                    throw new NetException("fail");
                }

                Console.WriteLine(algo.GetType().Name + " encryption verified");
            }

            for (int i = 0; i < 100; i++)
            {
                byte[] salt = NetSRP.CreateRandomSalt();
                byte[] x    = NetSRP.ComputePrivateKey("user", "password", salt);

                byte[] v = NetSRP.ComputeServerVerifier(x);
                //Console.WriteLine("v = " + NetUtility.ToHexString(v));

                byte[] a = NetSRP.CreateRandomEphemeral();                 //  NetUtility.ToByteArray("393ed364924a71ba7258633cc4854d655ca4ec4e8ba833eceaad2511e80db2b5");
                byte[] A = NetSRP.ComputeClientEphemeral(a);
                //Console.WriteLine("A = " + NetUtility.ToHexString(A));

                byte[] b = NetSRP.CreateRandomEphemeral();                 // NetUtility.ToByteArray("cc4d87a90db91067d52e2778b802ca6f7d362490c4be294b21b4a57c71cf55a9");
                byte[] B = NetSRP.ComputeServerEphemeral(b, v);
                //Console.WriteLine("B = " + NetUtility.ToHexString(B));

                byte[] u = NetSRP.ComputeU(A, B);
                //Console.WriteLine("u = " + NetUtility.ToHexString(u));

                byte[] Ss = NetSRP.ComputeServerSessionValue(A, v, u, b);
                //Console.WriteLine("Ss = " + NetUtility.ToHexString(Ss));

                byte[] Sc = NetSRP.ComputeClientSessionValue(B, x, u, a);
                //Console.WriteLine("Sc = " + NetUtility.ToHexString(Sc));

                if (Ss.Length != Sc.Length)
                {
                    throw new NetException("SRP non matching lengths!");
                }

                for (int j = 0; j < Ss.Length; j++)
                {
                    if (Ss[j] != Sc[j])
                    {
                        throw new NetException("SRP non matching session values!");
                    }
                }

                var test = NetSRP.CreateEncryption(Ss);
            }

            Console.WriteLine("Message encryption OK");
        }
Ejemplo n.º 4
0
        public static void Run(NetPeer peer)
        {
            //
            // Test XTEA
            //
            NetXtea xtea = new NetXtea("TopSecret");

            byte[] original = new byte[16];
            NetRandom.Instance.NextBytes(original);

            byte[] encrypted = new byte[original.Length];
            xtea.EncryptBlock(original, 0, encrypted, 0);
            xtea.EncryptBlock(original, 8, encrypted, 8);

            byte[] decrypted = new byte[original.Length];
            xtea.DecryptBlock(encrypted, 0, decrypted, 0);
            xtea.DecryptBlock(encrypted, 8, decrypted, 8);

            // compare!
            for (int i = 0; i < original.Length; i++)
            {
                if (original[i] != decrypted[i])
                {
                    throw new NetException("XTEA fail!");
                }
            }

            Console.WriteLine("XTEA OK");

            NetOutgoingMessage om = peer.CreateMessage();

            om.Write("Hallon");
            om.Write(42);
            om.Write(5, 5);
            om.Write(true);
            om.Write("kokos");
            om.Encrypt(xtea);

            // convert to incoming message
            NetIncomingMessage im = Program.CreateIncomingMessage(om.PeekDataBuffer(), om.LengthBits);

            im.Decrypt(xtea);

            if (im.ReadString() != "Hallon")
            {
                throw new NetException("fail");
            }
            if (im.ReadInt32() != 42)
            {
                throw new NetException("fail");
            }
            if (im.ReadInt32(5) != 5)
            {
                throw new NetException("fail");
            }
            if (im.ReadBoolean() != true)
            {
                throw new NetException("fail");
            }
            if (im.ReadString() != "kokos")
            {
                throw new NetException("fail");
            }

            for (int i = 0; i < 100; i++)
            {
                byte[] salt = NetSRP.CreateRandomSalt();
                byte[] x    = NetSRP.ComputePrivateKey("user", "password", salt);

                byte[] v = NetSRP.ComputeServerVerifier(x);
                //Console.WriteLine("v = " + NetUtility.ToHexString(v));

                byte[] a = NetSRP.CreateRandomEphemeral();                 //  NetUtility.ToByteArray("393ed364924a71ba7258633cc4854d655ca4ec4e8ba833eceaad2511e80db2b5");
                byte[] A = NetSRP.ComputeClientEphemeral(a);
                //Console.WriteLine("A = " + NetUtility.ToHexString(A));

                byte[] b = NetSRP.CreateRandomEphemeral();                 // NetUtility.ToByteArray("cc4d87a90db91067d52e2778b802ca6f7d362490c4be294b21b4a57c71cf55a9");
                byte[] B = NetSRP.ComputeServerEphemeral(b, v);
                //Console.WriteLine("B = " + NetUtility.ToHexString(B));

                byte[] u = NetSRP.ComputeU(A, B);
                //Console.WriteLine("u = " + NetUtility.ToHexString(u));

                byte[] Ss = NetSRP.ComputeServerSessionValue(A, v, u, b);
                //Console.WriteLine("Ss = " + NetUtility.ToHexString(Ss));

                byte[] Sc = NetSRP.ComputeClientSessionValue(B, x, u, a);
                //Console.WriteLine("Sc = " + NetUtility.ToHexString(Sc));

                if (Ss.Length != Sc.Length)
                {
                    throw new NetException("SRP non matching lengths!");
                }

                for (int j = 0; j < Ss.Length; j++)
                {
                    if (Ss[j] != Sc[j])
                    {
                        throw new NetException("SRP non matching session values!");
                    }
                }

                var test = NetSRP.CreateEncryption(Ss);
            }

            Console.WriteLine("Message encryption OK");
        }