Exemple #1
0
        public GenericGcmModeCryptoTransform(IGenericBlockCipher cipher, bool forEncryption,
                                             byte[] iv, byte[] additional, int tagLength)
        {
            if (cipher == null)
            {
                throw new ArgumentNullException("cipher");
            }
            if (cipher.BlockSize != BLOCK_SIZE)
            {
                throw new ArgumentException("cipher");
            }
            if (iv == null)
            {
                throw new ArgumentNullException("iv");
            }
            if (additional == null)
            {
                throw new ArgumentNullException("additional");
            }
            if (tagLength < 12 || tagLength > 16)
            {
                throw new ArgumentException("tagLength " + tagLength);
            }

            _cipher     = cipher;
            _encrypting = forEncryption;
            _tagLength  = tagLength;
            _cipher.Encrypt(_H, 0, _H, 0);

            if (iv.Length == 12)
            {
                Buffer.BlockCopy(iv, 0, _J0, 0, 12);
                _J0[_J0.Length - 1] = 0x01;
            }
            else
            {
                // Construct last hashed block from IV length
                byte[] lengthBytes = BitConverter.GetBytes((UInt64)iv.Length * 8);
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(lengthBytes);
                }
                byte[] lastBlock = new byte[BLOCK_SIZE];
                Buffer.BlockCopy(lengthBytes, 0, lastBlock, 8, 8);

                // Hash the IV and length block to get J0
                GHASH(iv, _J0);
                GHASH(_J0, lastBlock, 0, lastBlock.Length, _J0);
            }

            GHASH(additional, _originalS);
            _additionalLength = (UInt64)additional.Length;

            Reset();
        }
		public GenericCbcModeCryptoTransform(IGenericBlockCipher cipher, bool forEncryption, byte[] iv)
		{
			if (cipher == null)
				throw new ArgumentNullException("cipher");
			if (iv == null)
				throw new ArgumentNullException("iv");
			if (iv.Length != cipher.BlockSize)
				throw new ArgumentException("IV length " + iv.Length + ", expected " + cipher.BlockSize);
			
			_cipher = cipher;
			_blockSize = cipher.BlockSize;
			_encrypting = forEncryption;
			
			_originalIV = (byte[])iv.Clone();
			_currentIV = new byte[_blockSize];
			
			Reset();
		}
		public GenericGcmModeCryptoTransform(IGenericBlockCipher cipher, bool forEncryption,
		                                     byte[] iv, byte[] additional, int tagLength)
		{
			if (cipher == null)
				throw new ArgumentNullException("cipher");
			if (cipher.BlockSize != BLOCK_SIZE)
				throw new ArgumentException("cipher");
			if (iv == null)
				throw new ArgumentNullException("iv");
			if (additional == null)
				throw new ArgumentNullException("additional");
			if (tagLength < 12 || tagLength > 16)
				throw new ArgumentException("tagLength " + tagLength);
			
			_cipher = cipher;
			_encrypting = forEncryption;
			_tagLength = tagLength;
			_cipher.Encrypt(_H, 0, _H, 0);
			
			if (iv.Length == 12) {
				Buffer.BlockCopy(iv, 0, _J0, 0, 12);
				_J0[_J0.Length-1] = 0x01;
			} else {
				// Construct last hashed block from IV length
				byte[] lengthBytes = BitConverter.GetBytes((UInt64)iv.Length*8);
				if (BitConverter.IsLittleEndian) {
					Array.Reverse(lengthBytes);
				}
				byte[] lastBlock = new byte[BLOCK_SIZE];
				Buffer.BlockCopy(lengthBytes, 0, lastBlock, 8, 8);
				
				// Hash the IV and length block to get J0
				GHASH(iv, _J0);
				GHASH(_J0, lastBlock, 0, lastBlock.Length, _J0);
			}
			
			GHASH(additional, _originalS);
			_additionalLength = (UInt64)additional.Length;
			
			Reset();
		}
        public GenericCbcModeCryptoTransform(IGenericBlockCipher cipher, bool forEncryption, byte[] iv)
        {
            if (cipher == null)
            {
                throw new ArgumentNullException("cipher");
            }
            if (iv == null)
            {
                throw new ArgumentNullException("iv");
            }
            if (iv.Length != cipher.BlockSize)
            {
                throw new ArgumentException("IV length " + iv.Length + ", expected " + cipher.BlockSize);
            }

            _cipher     = cipher;
            _blockSize  = cipher.BlockSize;
            _encrypting = forEncryption;

            _originalIV = (byte[])iv.Clone();
            _currentIV  = new byte[_blockSize];

            Reset();
        }