Example #1
0
		public void EcryptTestVector512(string strComment, int rounds, string key, string iv, string strOutput0_63, string strOutput192_255, string strOutput256_319, string strOutput448_511)
		{
			const int c_nSize = 512;
			byte[] output = new byte[c_nSize];

			// encrypt by using CryptoStream
			using (SymmetricAlgorithm salsa20 = new Salsa20() { Rounds = rounds })
			using (ICryptoTransform encrypt = salsa20.CreateEncryptor(ToBytes(key), ToBytes(iv)))
			using (MemoryStream streamInput = new MemoryStream(new byte[c_nSize], false))
			using (CryptoStream streamEncrypted = new CryptoStream(streamInput, encrypt, CryptoStreamMode.Read))
			{
				streamEncrypted.Read(output, 0, output.Length);
			}

			CollectionAssert.AreEqual(ToBytes(strOutput0_63), output.Skip(0).Take(64).ToList());
			CollectionAssert.AreEqual(ToBytes(strOutput192_255), output.Skip(192).Take(64).ToList());
			CollectionAssert.AreEqual(ToBytes(strOutput256_319), output.Skip(256).Take(64).ToList());
			CollectionAssert.AreEqual(ToBytes(strOutput448_511), output.Skip(448).Take(64).ToList());
		}
Example #2
0
		public void EcryptTestVector131072(string strComment, int rounds, string key, string iv, string strOutput0_63, string strOutput65472_65535, string strOutput65536_65599, string strOutput131008_131071)
		{
			byte[] output;

			// use the Salsa20 transform directly
			using (SymmetricAlgorithm salsa20 = new Salsa20() { Rounds = rounds })
			using (ICryptoTransform encrypt = salsa20.CreateEncryptor(ToBytes(key), ToBytes(iv)))
			{
				// input is all zeroes
				byte[] input = new byte[131072];
				output = encrypt.TransformFinalBlock(input, 0, input.Length);
			}

			CollectionAssert.AreEqual(ToBytes(strOutput0_63), output.Skip(0).Take(64).ToList());
			CollectionAssert.AreEqual(ToBytes(strOutput65472_65535), output.Skip(65472).Take(64).ToList());
			CollectionAssert.AreEqual(ToBytes(strOutput65536_65599), output.Skip(65536).Take(64).ToList());
			CollectionAssert.AreEqual(ToBytes(strOutput131008_131071), output.Skip(131008).Take(64).ToList());
		}
Example #3
0
		public void EncryptDecrypt()
		{
			// generate data to encrypt
			byte[] input;
			using (MemoryStream stream = new MemoryStream())
			using (BinaryWriter writer = new BinaryWriter(stream))
			{
				for (short value = -1024; value <= 1023; value++)
					writer.Write(value);
				writer.Flush();

				input = stream.ToArray();
			}

			using (SymmetricAlgorithm salsa20 = new Salsa20())
			{
				byte[] encrypted = new byte[input.Length];
				using (ICryptoTransform encrypt = salsa20.CreateEncryptor())
					encrypt.TransformBlock(input, 0, input.Length, encrypted, 0);

				byte[] decrypted = new byte[input.Length];
				using (ICryptoTransform decrypt = salsa20.CreateDecryptor())
					decrypt.TransformBlock(encrypted, 0, encrypted.Length, decrypted, 0);

				CollectionAssert.AreEqual(input, decrypted);
			}
		}
Example #4
0
		public void Dispose()
		{
			using (SymmetricAlgorithm salsa20 = new Salsa20())
			using (ICryptoTransform transform = salsa20.CreateEncryptor(new byte[16], new byte[8]))
			{
				transform.Dispose();
				transform.Dispose();

				Assert.Throws<ObjectDisposedException>(() => transform.TransformBlock(new byte[1], 0, 1, new byte[1], 0));
				Assert.Throws<ObjectDisposedException>(() => transform.TransformFinalBlock(new byte[1], 0, 1));

				((IDisposable) salsa20).Dispose();
				((IDisposable) salsa20).Dispose();
			}
		}
Example #5
0
		public void TransformFinalBlock()
		{
			using (SymmetricAlgorithm salsa20 = new Salsa20())
			using (ICryptoTransform transform = salsa20.CreateEncryptor(new byte[16], new byte[8]))
			{
				byte[] aby0 = new byte[0];
				byte[] aby1 = new byte[1];

				Assert.Throws<ArgumentNullException>(() => transform.TransformFinalBlock(null, 0, 0));
				Assert.Throws<ArgumentOutOfRangeException>(() => transform.TransformFinalBlock(aby0, -1, 0));
				Assert.Throws<ArgumentOutOfRangeException>(() => transform.TransformFinalBlock(aby0, 1, 0));
				Assert.Throws<ArgumentOutOfRangeException>(() => transform.TransformFinalBlock(aby1, 0, -1));
				Assert.Throws<ArgumentOutOfRangeException>(() => transform.TransformFinalBlock(aby1, 0, 2));
				Assert.Throws<ArgumentOutOfRangeException>(() => transform.TransformFinalBlock(aby1, 1, 1));
			}
		}
Example #6
0
		public void CryptoTransform()
		{
			using (SymmetricAlgorithm salsa20 = new Salsa20())
			using (ICryptoTransform transform = salsa20.CreateEncryptor(new byte[16], new byte[8]))
			{
				Assert.AreEqual(salsa20.BlockSize / 8, transform.InputBlockSize);
				Assert.AreEqual(salsa20.BlockSize / 8, transform.OutputBlockSize);

				Assert.IsFalse(transform.CanReuseTransform);
				Assert.IsTrue(transform.CanTransformMultipleBlocks);
			}
		}
Example #7
0
		public void CreateEncryptorInvalid()
		{
			using (SymmetricAlgorithm salsa20 = new Salsa20())
			{
				Assert.Throws<ArgumentNullException>(() => salsa20.CreateEncryptor(null, new byte[8]));
				Assert.Throws<ArgumentNullException>(() => salsa20.CreateEncryptor(new byte[16], null));
				Assert.Throws<CryptographicException>(() => salsa20.CreateEncryptor(new byte[15], new byte[8]));
				Assert.Throws<CryptographicException>(() => salsa20.CreateEncryptor(new byte[16], new byte[7]));
			}
		}