protected void TestECB (SymmetricAlgorithmPlus algo, ECBTestHelper helper)
		{
			ICryptoTransform ct = null;
			int counter = 0;
			algo.Mode = CipherMode.ECB;
			String keyText = "";
			try {
				while (helper.ReadNext ()) {
					counter ++;
					if (ct == null || helper.IsUpdateKey) {
						byte[] key = helper.Key;
						keyText = ToString (key);
						if (ct != null) ct.Dispose ();
						ct = algo.CreateEncryptor (key, new byte[algo.BlockSize >> 3]);
					}
					byte[] output = new byte[ct.OutputBlockSize];
					ct.TransformBlock (helper.PlainText, 0, helper.PlainText.Length, output, 0);
					for (int i = 0; i < output.Length; i ++)
						if (output[i] != helper.CryptText[i])
							Assert.Fail (counter.ToString () + " " + ToString(helper.CryptText) + " != " + ToString (output) + " [Key=" + keyText + "]");
				}
			} finally {
				if (ct != null) ct.Dispose ();
			}
		}
        protected void Test_SameBuffer(SymmetricAlgorithmPlus algo)
        {
            byte[] key = RNG.GetBytes(algo.KeySize >> 3);
            byte[] iv  = RNG.GetBytes(algo.BlockSize >> 3);
            algo.Padding = PaddingMode.None;

            foreach (CipherModePlus mode in Enum.GetValues(typeof(CipherModePlus)))
            {
                algo.ModePlus = mode;
                if (mode == CipherModePlus.CTS)
                {
                    continue;
                }

                byte[] buf1 = RNG.GetBytes((algo.BlockSize >> 3) * 4);
                byte[] buf2 = RNG.GetBytes((algo.BlockSize >> 3) * 4);

                using (ICryptoTransform ct = algo.CreateEncryptor(key, iv)) {
                    ct.TransformBlock(buf1, 0, buf1.Length, buf2, 0);
                }
                using (ICryptoTransform ct = algo.CreateEncryptor(key, iv)) {
                    ct.TransformBlock(buf1, 0, buf1.Length, buf1, 0);
                }
                Assert.AreEqual(buf2, buf1, string.Format("Encrypt with {0} mode", mode));
                using (ICryptoTransform ct = algo.CreateDecryptor(key, iv)) {
                    ct.TransformBlock(buf1, 0, buf1.Length, buf2, 0);
                }
                using (ICryptoTransform ct = algo.CreateDecryptor(key, iv)) {
                    ct.TransformBlock(buf1, 0, buf1.Length, buf1, 0);
                }
                Assert.AreEqual(buf2, buf1, string.Format("Decrypt with {0} mode", mode));
            }
        }
        protected void Test_MultiBlock_1(SymmetricAlgorithmPlus algo)
        {
            KeySizes keySizes   = algo.LegalKeySizes[0];
            KeySizes blockSizes = algo.LegalBlockSizes[0];

            int keySize = keySizes.MinSize;

            do
            {
                int blockSize = blockSizes.MinSize;
                do
                {
                    byte[] pt  = new byte[(blockSize >> 3) * 3];
                    byte[] ct  = new byte[pt.Length];
                    byte[] tmp = new byte[pt.Length];
                    byte[] key = new byte[keySize >> 3];
                    byte[] iv  = new byte[blockSize >> 3];
                    algo.BlockSize = blockSize;
                    algo.KeySize   = keySize;
                    foreach (CipherImplementationType type in _types)
                    {
                        if (!algo.HasImplementation(type))
                        {
                            continue;
                        }
                        RNG.GetBytes(pt);
                        RNG.GetBytes(key);
                        RNG.GetBytes(iv);
                        algo.ImplementationType = type;
                        using (ICryptoTransform t = algo.CreateEncryptor(key, iv))
                            for (int i = 0; i < pt.Length; i += blockSize >> 3)
                            {
                                t.TransformBlock(pt, i, blockSize >> 3, ct, i);
                            }
                        using (ICryptoTransform t = algo.CreateDecryptor(key, iv))
                            for (int i = 0; i < ct.Length; i += blockSize >> 3)
                            {
                                t.TransformBlock(ct, i, blockSize >> 3, tmp, i);
                            }
                        for (int i = 0; i < pt.Length; i++)
                        {
                            Assert.AreEqual(pt[i], tmp[i], "Mode:" + type.ToString() + " Key:" + keySize.ToString() + "bits Block:" + blockSize.ToString() + "bits Pos:" + i.ToString());
                        }
                    }
                    if (blockSizes.SkipSize == 0)
                    {
                        break;
                    }
                    blockSize += blockSizes.SkipSize;
                } while (blockSize <= blockSizes.MaxSize);
                if (keySizes.SkipSize == 0)
                {
                    break;
                }
                keySize += keySizes.SkipSize;
            } while (keySize <= keySizes.MaxSize);
        }
Esempio n. 4
0
		static double[] Run (SymmetricAlgorithmPlus algo, CipherModePlus mode, int keySize, int blockSize, int dataSize, int threads)
		{
			double[] result = SpeedTest.Run (algo, mode, keySize, blockSize, dataSize, threads);
			for (int i = 0; i < 10; i++) {
				double[] temp = SpeedTest.Run (algo, mode, keySize, blockSize, dataSize, threads);
				result[0] = Math.Max (result[0], temp[0]);
				result[1] = Math.Max (result[1], temp[1]);
			}
			return result;
		}
Esempio n. 5
0
 static double[] Run(SymmetricAlgorithmPlus algo, CipherModePlus mode, int keySize, int blockSize, int dataSize, int threads)
 {
     double[] result = SpeedTest.Run(algo, mode, keySize, blockSize, dataSize, threads);
     for (int i = 0; i < 10; i++)
     {
         double[] temp = SpeedTest.Run(algo, mode, keySize, blockSize, dataSize, threads);
         result[0] = Math.Max(result[0], temp[0]);
         result[1] = Math.Max(result[1], temp[1]);
     }
     return(result);
 }
Esempio n. 6
0
        public static double[] Run(SymmetricAlgorithmPlus algo, CipherModePlus mode, int keySize, int blockSize, int dataSize, int threads)
        {
            double[] result;
            int      totalBlocks = dataSize / (blockSize >> 3);

            byte[] key = new byte [keySize >> 3];
            byte[] iv  = new byte [blockSize >> 3];
            algo.NumberOfThreads = threads;
            algo.ModePlus        = mode;
            result = Run(algo, key, iv, totalBlocks);
            for (int i = 0; i < result.Length; i++)
            {
                result[i] = dataSize * 8.0 / result[i] / (1024.0 * 1024.0);
            }
            return(result);
        }
 static void Test(SymmetricAlgorithmPlus algo, String msg, byte[] key, byte[] iv, byte[] src, byte[] enc)
 {
     using (ICryptoTransform ect = algo.CreateEncryptor(key, iv), dct = algo.CreateDecryptor(key, iv)) {
         byte[] temp = new byte [iv.Length], temp2 = new byte [iv.Length];
         for (int i = 0; i < src.Length; i += iv.Length)
         {
             ect.TransformBlock(src, i, iv.Length, temp, 0);
             for (int j = 0; j < temp.Length; j++)
             {
                 Assert.AreEqual(enc[i + j], temp[j], msg + string.Format(" Encryption Error Block:{0}, Pos:{1}", i / iv.Length, j));
             }
             dct.TransformBlock(temp, 0, temp.Length, temp2, 0);
             for (int j = 0; j < temp2.Length; j++)
             {
                 Assert.AreEqual(src[i + j], temp2[j], msg + string.Format(" Decryption Error Block:{0}, Pos:{1}", i / iv.Length, j));
                 temp[j] = temp2[j] = 0;
             }
         }
     }
 }
		protected void Test_MultiBlock_1 (SymmetricAlgorithmPlus algo)
		{
			KeySizes keySizes = algo.LegalKeySizes[0];
			KeySizes blockSizes = algo.LegalBlockSizes[0];

			int keySize = keySizes.MinSize;
			do {
				int blockSize = blockSizes.MinSize;
				do {
					byte[] pt = new byte[(blockSize >> 3) * 3];
					byte[] ct = new byte[pt.Length];
					byte[] tmp = new byte[pt.Length];
					byte[] key = new byte[keySize >> 3];
					byte[] iv = new byte[blockSize >> 3];
					algo.BlockSize = blockSize;
					algo.KeySize = keySize;
					foreach (CipherImplementationType type in _types) {
						if (!algo.HasImplementation (type))
							continue;
						RNG.GetBytes (pt);
						RNG.GetBytes (key);
						RNG.GetBytes (iv);
						algo.ImplementationType = type;
						using (ICryptoTransform t = algo.CreateEncryptor (key, iv))
							for (int i = 0; i < pt.Length; i += blockSize >> 3)
								t.TransformBlock (pt, i, blockSize >> 3, ct, i);
						using (ICryptoTransform t = algo.CreateDecryptor (key, iv))
							for (int i = 0; i < ct.Length; i += blockSize >> 3)
								t.TransformBlock (ct, i, blockSize >> 3, tmp, i);
						for (int i = 0; i < pt.Length; i ++)
							Assert.AreEqual (pt[i], tmp[i], "Mode:" + type.ToString () + " Key:" + keySize.ToString () + "bits Block:" + blockSize.ToString () + "bits Pos:" + i.ToString ());
					}
					if (blockSizes.SkipSize == 0)
						break;
					blockSize += blockSizes.SkipSize;
				} while (blockSize <= blockSizes.MaxSize);
				if (keySizes.SkipSize == 0)
					break;
				keySize += keySizes.SkipSize;
			} while (keySize <= keySizes.MaxSize);
		}
Esempio n. 9
0
        private void RunTest(SymmetricAlgorithmPlus algo, CipherModePlus mode)
        {
            const int TestDataSize = 1 << 20;

            byte[] data1 = new byte[TestDataSize];
            byte[] data2 = new byte[TestDataSize];
            byte[] data3 = new byte[TestDataSize];
            byte[] iv    = new byte[algo.IV.Length];
            byte[] key   = new byte[algo.Key.Length];

            RNG.GetBytes(data1);
            RNG.GetBytes(key);
            RNG.GetBytes(iv);
            algo.ModePlus = mode;

            algo.NumberOfThreads = 4;
            using (ICryptoTransform ct = algo.CreateEncryptor(key, iv)) {
                ct.TransformBlock(data1, 0, TestDataSize, data2, 0);
            }
            algo.NumberOfThreads = 1;
            using (ICryptoTransform ct = algo.CreateDecryptor(key, iv)) {
                ct.TransformBlock(data2, 0, TestDataSize, data3, 0);
            }
            for (int i = 0; i < TestDataSize; i++)
            {
                Assert.AreEqual(data1[i], data3[i], "Encryption Error");
            }

            algo.NumberOfThreads = 1;
            using (ICryptoTransform ct = algo.CreateEncryptor(key, iv)) {
                ct.TransformBlock(data1, 0, TestDataSize, data2, 0);
            }
            algo.NumberOfThreads = 4;
            using (ICryptoTransform ct = algo.CreateDecryptor(key, iv)) {
                ct.TransformBlock(data2, 0, TestDataSize, data3, 0);
            }
            for (int i = 0; i < TestDataSize; i++)
            {
                Assert.AreEqual(data1[i], data3[i], "Decryption Error");
            }
        }
        protected void TestECB(SymmetricAlgorithmPlus algo, ECBTestHelper helper)
        {
            ICryptoTransform ct = null;
            int counter         = 0;

            algo.Mode = CipherMode.ECB;
            String keyText = "";

            try {
                while (helper.ReadNext())
                {
                    counter++;
                    if (ct == null || helper.IsUpdateKey)
                    {
                        byte[] key = helper.Key;
                        keyText = ToString(key);
                        if (ct != null)
                        {
                            ct.Dispose();
                        }
                        ct = algo.CreateEncryptor(key, new byte[algo.BlockSize >> 3]);
                    }
                    byte[] output = new byte[ct.OutputBlockSize];
                    ct.TransformBlock(helper.PlainText, 0, helper.PlainText.Length, output, 0);
                    for (int i = 0; i < output.Length; i++)
                    {
                        if (output[i] != helper.CryptText[i])
                        {
                            Assert.Fail(counter.ToString() + " " + ToString(helper.CryptText) + " != " + ToString(output) + " [Key=" + keyText + "]");
                        }
                    }
                }
            } finally {
                if (ct != null)
                {
                    ct.Dispose();
                }
            }
        }
		protected void Test_SameBuffer (SymmetricAlgorithmPlus algo)
		{
			byte[] key = RNG.GetBytes (algo.KeySize >> 3);
			byte[] iv = RNG.GetBytes (algo.BlockSize >> 3);
			algo.Padding = PaddingMode.None;

			foreach (CipherModePlus mode in Enum.GetValues (typeof (CipherModePlus))) {
				algo.ModePlus = mode;
				if (mode == CipherModePlus.CTS)
					continue;

				byte[] buf1 = RNG.GetBytes ((algo.BlockSize >> 3) * 4);
				byte[] buf2 = RNG.GetBytes ((algo.BlockSize >> 3) * 4);

				using (ICryptoTransform ct = algo.CreateEncryptor (key, iv)) {
					ct.TransformBlock (buf1, 0, buf1.Length, buf2, 0);
				}
				using (ICryptoTransform ct = algo.CreateEncryptor (key, iv)) {
					ct.TransformBlock (buf1, 0, buf1.Length, buf1, 0);
				}
				Assert.AreEqual (buf2, buf1, string.Format ("Encrypt with {0} mode", mode));
				using (ICryptoTransform ct = algo.CreateDecryptor (key, iv)) {
					ct.TransformBlock (buf1, 0, buf1.Length, buf2, 0);
				}
				using (ICryptoTransform ct = algo.CreateDecryptor (key, iv)) {
					ct.TransformBlock (buf1, 0, buf1.Length, buf1, 0);
				}
				Assert.AreEqual (buf2, buf1, string.Format ("Decrypt with {0} mode", mode));
			}
		}
Esempio n. 12
0
 public DummyTransform(SymmetricAlgorithmPlus algo, bool encryptMode, byte[] iv)
     : base(algo, encryptMode, iv)
 {
 }
Esempio n. 13
0
		static void Test (SymmetricAlgorithmPlus algo, String msg, byte[] key, byte[] iv, byte[] src, byte[] enc)
		{
			using (ICryptoTransform ect = algo.CreateEncryptor (key, iv), dct = algo.CreateDecryptor (key, iv)) {
				byte[] temp = new byte [iv.Length], temp2 = new byte [iv.Length];
				for (int i = 0; i < src.Length; i += iv.Length) {
					ect.TransformBlock (src, i, iv.Length, temp, 0);
					for (int j = 0; j < temp.Length; j ++)
						Assert.AreEqual (enc[i + j], temp[j], msg + string.Format (" Encryption Error Block:{0}, Pos:{1}", i / iv.Length, j));
					dct.TransformBlock (temp, 0, temp.Length, temp2, 0);
					for (int j = 0; j < temp2.Length; j ++) {
						Assert.AreEqual (src[i + j], temp2[j], msg + string.Format (" Decryption Error Block:{0}, Pos:{1}", i / iv.Length, j));
						temp[j] = temp2[j] = 0;
					}
				}
			}
		}