Example #1
0
        private void VectorTest(int Rounds, byte[] Key, byte[] Vector, byte[] Input, byte[] Output)
        {
            byte[] outBytes = new byte[Input.Length];

            using (Salsa20 salsa = new Salsa20(Rounds))
            {
                salsa.Initialize(new KeyParams(Key, Vector));
                salsa.Transform(Input, 0, Input.Length, outBytes, 0);

                if (Compare.AreEqual(outBytes, Output) == false)
                    throw new Exception("Salsa20: Encrypted arrays are not equal! Expected: " + HexConverter.ToString(Output) + " Received: " + HexConverter.ToString(outBytes));

                salsa.Initialize(new KeyParams(Key, Vector));
                salsa.Transform(Output, 0, Output.Length, outBytes, 0);

                if (Compare.AreEqual(outBytes, Input) == false)
                    throw new Exception("Salsa20: Decrypted arrays are not equal! Expected: " + HexConverter.ToString(Input) + " Received: " + HexConverter.ToString(outBytes));
            }
        }
Example #2
0
        private void ParallelTest()
        {
            CSPRng rng = new CSPRng();
            byte[] key = rng.GetBytes(32);
            byte[] iv = rng.GetBytes(8);
            byte[] data = rng.GetBytes(2048);
            byte[] enc = new byte[2048];
            byte[] dec = new byte[2048];
            rng.Dispose();

            using (Salsa20 salsa = new Salsa20(10))
            {
                // encrypt linear
                salsa.Initialize(new KeyParams(key, iv));
                salsa.IsParallel = false;
                salsa.Transform(data, enc);
                // decrypt parallel
                salsa.Initialize(new KeyParams(key, iv));
                salsa.IsParallel = true;
                salsa.ParallelBlockSize = 2048;
                salsa.Transform(enc, dec);
            }

            if (!Compare.AreEqual(data, dec))
                throw new Exception("Salsa20: Decrypted arrays are not equal!");
        }