Exemplo n.º 1
43
        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 (ChaCha chacha = new ChaCha(10))
            {
                // encrypt linear
                chacha.Initialize(new KeyParams(key, iv));
                chacha.IsParallel = false;
                chacha.Transform(data, enc);
                // decrypt parallel
                chacha.Initialize(new KeyParams(key, iv));
                chacha.IsParallel = true;
                chacha.ParallelBlockSize = 2048;
                chacha.Transform(enc, dec);
            }

            if (!Compare.AreEqual(data, dec))
                throw new Exception("ChaCha: Decrypted arrays are not equal!");
        }
Exemplo n.º 2
0
        private void VectorTest(int Rounds, byte[] Key, byte[] Vector, byte[] Input, byte[] Output)
        {
            byte[] outBytes = new byte[Input.Length];

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

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

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

            if (Compare.AreEqual(outBytes, Input) == false)
                throw new Exception("ChaChaVector: Decrypted arrays are not equal! Expected: " + HexConverter.ToString(Input) + " Received: " + HexConverter.ToString(outBytes));
        }