Example #1
0
		/// <summary>ctor</summary>
		public AesCtrCryptoTransform(byte[] key, ArraySegment<byte> counterBufferSegment, Func<Aes> aesFactory = null)
		{
			if (counterBufferSegment.Count != AesConstants.AES_BLOCK_SIZE)
				throw new ArgumentException("counterBufferSegment.Count must be " + AesConstants.STR_AES_BLOCK_SIZE + ".");

			this.aes = aesFactory == null ? AesFactories.Aes() : aesFactory();
			this.aes.Mode = CipherMode.ECB;
			this.aes.Padding = PaddingMode.None;

			Utils.BlockCopy(counterBufferSegment.Array, counterBufferSegment.Offset, counterBuffer, 0, AesConstants.AES_BLOCK_SIZE);
			this.cryptoTransform = aes.CreateEncryptor(rgbKey: key, rgbIV: null);
		}// ctor
Example #2
0
		/// <summary>ctor</summary>
		public AesCtrCryptoTransform(byte[] key, ArraySegment<byte> counterBufferSegment, Func<Aes> aesFactory = null)
		{
			if (counterBufferSegment.Count != AesConstants.AES_BLOCK_SIZE)
				ThrowNewArgumentException("counterBufferSegment.Count must be " + AesConstants.STR_AES_BLOCK_SIZE + ".");

			var aes = this.aes = aesFactory == null ? AesFactories.Aes() : aesFactory();
			(aes.Mode, aes.Padding) = (CipherMode.ECB, PaddingMode.None);

			(var counterBufferSegmentArray, var counterBufferSegmentOffset) = (counterBufferSegment.Array, counterBufferSegment.Offset);
			System.Diagnostics.Debug.Assert(AesConstants.AES_BLOCK_SIZE == 16);

			(Unsafe.As<byte, ulong>(ref this.counterBuffer_KeyStreamBuffer[0]), this.counterStruct.UlongValue) =
				Unsafe.As<byte, (ulong, ulong)>(ref counterBufferSegmentArray[counterBufferSegmentOffset]);

			if (BitConverter.IsLittleEndian)
				this.counterStruct.UlongValue = Utils.ReverseEndianness(this.counterStruct.UlongValue);

			this.cryptoTransform = aes.CreateEncryptor(rgbKey: key, rgbIV: null);
		}// ctor
		/// <summary>ctor</summary>
		public AesCtrCryptoTransform(byte[] key, ArraySegment<byte> counterBufferSegment, Func<Aes> aesFactory = null)
		{
			if (counterBufferSegment.Count != AesConstants.AES_BLOCK_SIZE)
				throw new ArgumentException("counterBufferSegment.Count must be " + AesConstants.STR_AES_BLOCK_SIZE + ".");

			var aes = aesFactory == null ? AesFactories.Aes() : aesFactory();

			aes.Mode = CipherMode.ECB;
			aes.Padding = PaddingMode.None;

			var counterBufferSegmentArray = counterBufferSegment.Array;
			var counterBufferSegmentOffset = counterBufferSegment.Offset;

			byte[] counterBuffer_KeyStreamBuffer = this.counterBuffer_KeyStreamBuffer; // looks dumb, but local-access is faster than field-access

			System.Diagnostics.Debug.Assert(AesConstants.AES_BLOCK_SIZE == 16);

			//Utils.BlockCopy(counterBufferSegment.Array, counterBufferSegment.Offset, counterBuffer_KeyStreamBuffer, 0, AesConstants.AES_BLOCK_SIZE);
			counterBuffer_KeyStreamBuffer[00] = counterBufferSegmentArray[counterBufferSegmentOffset + 00];
			counterBuffer_KeyStreamBuffer[01] = counterBufferSegmentArray[counterBufferSegmentOffset + 01];
			counterBuffer_KeyStreamBuffer[02] = counterBufferSegmentArray[counterBufferSegmentOffset + 02];
			counterBuffer_KeyStreamBuffer[03] = counterBufferSegmentArray[counterBufferSegmentOffset + 03];
			counterBuffer_KeyStreamBuffer[04] = counterBufferSegmentArray[counterBufferSegmentOffset + 04];
			counterBuffer_KeyStreamBuffer[05] = counterBufferSegmentArray[counterBufferSegmentOffset + 05];
			counterBuffer_KeyStreamBuffer[06] = counterBufferSegmentArray[counterBufferSegmentOffset + 06];
			counterBuffer_KeyStreamBuffer[07] = counterBufferSegmentArray[counterBufferSegmentOffset + 07];

			this.counterStruct = new Utils.LongStruct
			{
				B8 = counterBufferSegmentArray[counterBufferSegmentOffset + 08],
				B7 = counterBufferSegmentArray[counterBufferSegmentOffset + 09],
				B6 = counterBufferSegmentArray[counterBufferSegmentOffset + 10],
				B5 = counterBufferSegmentArray[counterBufferSegmentOffset + 11],
				B4 = counterBufferSegmentArray[counterBufferSegmentOffset + 12],
				B3 = counterBufferSegmentArray[counterBufferSegmentOffset + 13],
				B2 = counterBufferSegmentArray[counterBufferSegmentOffset + 14],
				B1 = counterBufferSegmentArray[counterBufferSegmentOffset + 15]
			};

			this.cryptoTransform = aes.CreateEncryptor(rgbKey: key, rgbIV: null);
			this.aes = aes;
		}// ctor