Esempio n. 1
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 RHX()))
            {
                mode1.Initialize(true, new KeyParams(Key, iv));

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

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

            index++;

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

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

                    if (Evaluate.AreEqual(outBytes, _output[index, i]) == false)
                    {
                        throw new Exception("CFB Mode: Decrypted arrays are not equal!");
                    }
                }
            }
        }
Esempio n. 2
0
        private void ParallelTest()
        {
            byte[]    data;
            byte[]    dec1;
            byte[]    dec2;
            byte[]    enc1;
            byte[]    enc2;
            int       blockSize;
            KeyParams keyParam = new KeyParams(new byte[32], new byte[16]);

            // CTR mode
            using (CTR cipher = new CTR(new RHX()))
            {
                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;

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

                // linear 1
                cipher.Initialize(true, keyParam);
                cipher.IsParallel = false;
                blockSize         = cipher.BlockSize;
                enc2 = Transform2(cipher, data, blockSize);

                if (Evaluate.AreEqual(enc1, enc2) == false)
                {
                    throw new Exception("Parallel CTR: Encrypted output is not equal!");
                }

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

                if (Evaluate.AreEqual(enc1, enc2) == false)
                {
                    throw new Exception("Parallel CTR: Encrypted output is not equal!");
                }

                // linear 2
                cipher.Initialize(true, keyParam);
                cipher.IsParallel = false;
                blockSize         = cipher.BlockSize;
                enc2 = Transform2(cipher, data, blockSize);

                if (Evaluate.AreEqual(enc1, enc2) == false)
                {
                    throw new Exception("Parallel CTR: Encrypted output is not equal!");
                }

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

                // parallel 2
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = true;
                blockSize         = cipher.ParallelBlockSize;
                dec2 = Transform2(cipher, enc2, blockSize);

                if (Evaluate.AreEqual(dec1, dec2) == false)
                {
                    throw new Exception("Parallel CTR: Decrypted output is not equal!");
                }

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

                if (Evaluate.AreEqual(dec1, dec2) == false)
                {
                    throw new Exception("Parallel CTR: Decrypted output is not equal!");
                }

                // linear 2
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = false;
                blockSize         = cipher.BlockSize;
                dec2 = Transform2(cipher, enc2, blockSize);

                if (Evaluate.AreEqual(dec1, dec2) == false)
                {
                    throw new Exception("Parallel CTR: Decrypted output is not equal!");
                }
            }

            if (Evaluate.AreEqual(data, dec1) == false)
            {
                throw new Exception("Parallel CTR: Decrypted output is not equal!");
            }
            if (Evaluate.AreEqual(data, 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 RHX()))
            {
                // must be divisible by block size, add padding if required
                data = GetBytes(2048);

                // encrypt
                cipher.ParallelBlockSize = 1024;

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

                // t2
                cipher.Initialize(true, keyParam);
                blockSize = cipher.BlockSize;
                enc2      = Transform2(cipher, data, blockSize);

                if (Evaluate.AreEqual(enc1, enc2) == false)
                {
                    throw new Exception("Parallel CBC: Decrypted output is not equal!");
                }

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

                // t1 linear
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = false;
                blockSize         = cipher.BlockSize;
                dec2 = Transform1(cipher, enc2, blockSize);

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

                // t2 parallel
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = true;
                blockSize         = cipher.ParallelBlockSize;
                dec1 = Transform2(cipher, enc2, blockSize);

                // t2 linear
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = false;
                blockSize         = cipher.BlockSize;
                dec2 = Transform2(cipher, enc1, blockSize);

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

            if (Evaluate.AreEqual(dec1, data) == false)
            {
                throw new Exception("Parallel CBC: Decrypted output is not equal!");
            }
            if (Evaluate.AreEqual(dec2, data) == 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 RHX()))
            {
                // must be divisible by block size, add padding if required
                data = GetBytes(2048);

                // encrypt
                cipher.ParallelBlockSize = 1024;

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

                if (Evaluate.AreEqual(enc1, enc2) == false)
                {
                    throw new Exception("Parallel CFB: Decrypted output is not equal!");
                }

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

                // t1 linear
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = false;
                blockSize         = cipher.BlockSize;
                dec2 = Transform1(cipher, enc2, blockSize);

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

                // t2 parallel
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = true;
                blockSize         = cipher.ParallelBlockSize;
                dec1 = Transform2(cipher, enc2, blockSize);

                // t2 linear
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = false;
                blockSize         = cipher.BlockSize;
                dec2 = Transform2(cipher, enc1, blockSize);

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

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

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

            // dispose container
            keyParam.Dispose();
        }
Esempio n. 3
0
        private void CfbModeTest()
        {
            AllocateRandom(ref _iv, 16);
            AllocateRandom(ref _key, 32);

            KeyParams kp      = new KeyParams(_key, _iv);
            RHX       eng     = new RHX();
            CFB       cipher  = new CFB(eng);
            CFB       cipher2 = new CFB(eng);
            ISO7816   padding = new ISO7816();

            cipher.IsParallel = false;
            CipherStream cs = new CipherStream(cipher2, padding);

            for (int i = 0; i < 10; i++)
            {
                int sze      = AllocateRandom(ref _plnText, 0, eng.BlockSize);
                int prlBlock = sze - (sze % (cipher.BlockSize * _processorCount));
                _cmpText = new byte[sze];
                _decText = new byte[sze];
                _encText = new byte[sze];

                cipher.ParallelBlockSize  = prlBlock;
                cipher2.ParallelBlockSize = prlBlock;
                MemoryStream mIn  = new MemoryStream(_plnText);
                MemoryStream mOut = new MemoryStream();
                MemoryStream mRes = new MemoryStream();

                // *** Compare encryption output *** //

                // local processor
                cipher.Initialize(true, kp);
                BlockEncrypt(cipher, padding, _plnText, 0, ref _encText, 0);

                // streamcipher linear mode
                cs.IsParallel = false;
                // memorystream interface
                cs.Initialize(true, kp);
                cs.Write(mIn, mOut);

                if (!Evaluate.AreEqual(mOut.ToArray(), _encText))
                {
                    throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                }

                // byte array interface
                cs.Initialize(true, kp);
                cs.Write(_plnText, 0, ref _cmpText, 0);

                if (!Evaluate.AreEqual(_cmpText, _encText))
                {
                    throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                }

                // ***compare decryption output *** //

                // local processor
                cipher.Initialize(false, kp);
                BlockDecrypt(cipher, padding, _encText, 0, ref _decText, 0);

                if (!Evaluate.AreEqual(_plnText, _decText))
                {
                    throw new Exception("CipherStreamTest: Decrypted arrays are not equal!");
                }

                // decrypt linear mode
                cs.IsParallel = false;
                mOut.Seek(0, SeekOrigin.Begin);
                cs.Initialize(false, kp);
                cs.Write(mOut, mRes);

                if (!Evaluate.AreEqual(mRes.ToArray(), _decText))
                {
                    throw new Exception("CipherStreamTest: Decrypted arrays are not equal!");
                }

                // byte array interface
                cs.Initialize(false, kp);
                cs.Write(_encText, 0, ref _cmpText, 0);

                if (!Evaluate.AreEqual(_cmpText, _decText))
                {
                    throw new Exception("CipherStreamTest: Decrypted arrays are not equal!");
                }

                // decrypt parallel mode
                cs.IsParallel = true;
                mOut.Seek(0, SeekOrigin.Begin);
                mRes.Seek(0, SeekOrigin.Begin);
                cs.Initialize(false, kp);
                cs.Write(mOut, mRes);

                if (!Evaluate.AreEqual(mRes.ToArray(), _decText))
                {
                    throw new Exception("CipherStreamTest: Decrypted arrays are not equal!");
                }

                // byte array interface
                cs.Initialize(false, kp);
                Array.Resize(ref _cmpText, _encText.Length);
                cs.Write(_encText, 0, ref _cmpText, 0);

                if (!Evaluate.AreEqual(_cmpText, _decText))
                {
                    throw new Exception("CipherStreamTest: Decrypted arrays are not equal!");
                }
            }

            eng.Dispose();
        }