Exemplo n.º 1
0
        private void ParallelTest()
        {
            byte[] data;
            byte[] dec1;
            byte[] dec2;
            byte[] enc1;
            byte[] enc2;
            int blockSize;
            KeyParams keyParam = new KeyParams(GetBytes(32), GetBytes(16));

            // CTR mode
            using (CTR cipher = new CTR(new RDX()))
            {
                data = GetBytes(1036);

                // how to calculate an ideal block size
                int plen = (data.Length / cipher.ParallelMinimumSize) * cipher.ParallelMinimumSize;
                // you can factor it up or down or use a default
                if (plen > cipher.ParallelMaximumSize)
                    plen = 1024;

                // set parallel block size
                cipher.ParallelBlockSize = plen;

                // encrypt //
                // parallel
                cipher.Initialize(true, keyParam);
                cipher.IsParallel = true;
                blockSize = cipher.ParallelBlockSize;
                enc1 = Transform1(cipher, data, blockSize);

                // normal mode
                cipher.Initialize(true, keyParam);
                cipher.IsParallel = false;
                blockSize = cipher.BlockSize;
                enc2 = Transform1(cipher, data, blockSize);

                // decrypt
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = true;
                blockSize = cipher.ParallelBlockSize;
                dec1 = Transform1(cipher, enc1, blockSize);

                cipher.Initialize(false, keyParam);
                cipher.IsParallel = false;
                blockSize = cipher.BlockSize;
                dec2 = Transform1(cipher, enc1, blockSize);
            }

            if (Compare.AreEqual(enc1, enc2) == false)
                throw new Exception("Parallel CTR: Encrypted output is not equal!");
            if (Compare.AreEqual(dec1, dec2) == false)
                throw new Exception("Parallel CTR: Decrypted output is not equal!");

            OnProgress(new TestEventArgs("Passed Parallel CTR encryption and decryption tests.."));

            // CBC mode
            using (CBC cipher = new CBC(new RDX()))
            {
                // must be divisible by block size, add padding if required
                data = GetBytes(2048);

                // encrypt
                cipher.ParallelBlockSize = 1024;

                // encrypt only in normal mode for cbc
                cipher.Initialize(true, keyParam);
                blockSize = cipher.BlockSize;
                enc1 = Transform1(cipher, data, blockSize);

                // decrypt
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = true;
                blockSize = cipher.ParallelBlockSize;
                dec1 = Transform1(cipher, enc1, blockSize);

                cipher.Initialize(false, keyParam);
                cipher.IsParallel = false;
                blockSize = cipher.BlockSize;
                dec2 = Transform1(cipher, enc1, blockSize);
            }

            if (Compare.AreEqual(dec1, dec2) == false)
                throw new Exception("Parallel CBC: Decrypted output is not equal!");

            OnProgress(new TestEventArgs("Passed Parallel CBC decryption tests.."));

            // CFB mode
            using (CFB cipher = new CFB(new RDX()))
            {
                // must be divisible by block size, add padding if required
                data = GetBytes(2048);

                // encrypt
                cipher.ParallelBlockSize = 1024;

                // encrypt only in normal mode for cfb
                cipher.Initialize(true, keyParam);
                blockSize = cipher.BlockSize;
                enc1 = Transform1(cipher, data, blockSize);

                // decrypt
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = true;
                blockSize = cipher.ParallelBlockSize;
                dec1 = Transform1(cipher, enc1, blockSize);

                cipher.Initialize(false, keyParam);
                cipher.IsParallel = false;
                blockSize = cipher.BlockSize;
                dec2 = Transform1(cipher, enc1, blockSize);
            }

            if (Compare.AreEqual(dec1, dec2) == false)
                throw new Exception("Parallel CFB: Decrypted output is not equal!");
            if (Compare.AreEqual(dec1, data) == false)
                throw new Exception("Parallel CFB: Decrypted output is not equal!");

            OnProgress(new TestEventArgs("Passed Parallel CFB decryption tests.."));

            // dispose container
            keyParam.Dispose();
        }
Exemplo n.º 2
0
        private void CFBTest(byte[] Key, byte[,][] Input, byte[,][] Output)
        {
            byte[] outBytes = new byte[16];
            byte[] iv = _vectors[0];
            int index = 12;

            if (Key.Length == 24)
                index = 14;
            else if (Key.Length == 32)
                index = 16;

            using (CFB mode1 = new CFB(new RDX()))
            {
                mode1.Initialize(true, new KeyParams(Key, iv));

                for (int i = 0; i < 4; i++)
                {
                    mode1.Transform(Input[index, i], 0, outBytes, 0);

                    if (Compare.AreEqual(outBytes, Output[index, i]) == false)
                        throw new Exception("CFB Mode: Encrypted arrays are not equal!");
                }
            }

            index++;

            using (CFB mode2 = new CFB(new RDX()))
            {
                mode2.Initialize(false, new KeyParams(Key, iv));

                for (int i = 0; i < 4; i++)
                {
                    mode2.Transform(Input[index, i], outBytes);

                    if (Compare.AreEqual(outBytes, _output[index, i]) == false)
                        throw new Exception("CFB Mode: Decrypted arrays are not equal!");
                }
            }
        }