示例#1
0
        public void SkipTest()
        {
            // arrange
            ByteCircularBuffer target;
            int expected;

            target = new ByteCircularBuffer(10);
            target.Put(1);
            target.Put(2);
            target.Put(3);
            target.Put(4);

            expected = 2;

            // act
            target.Skip(2);

            // assert
            target.Head.Should().
            Be(expected);
        }
 public override void Decrypt(byte[] buf, int length, byte[] outbuf, out int outlength)
 {
     Debug.Assert(_decCircularBuffer != null, "_circularBuffer != null");
     _decCircularBuffer.Put(buf, 0, length);
     if (!_decryptIVReceived)
     {
         if (_decCircularBuffer.Size <= ivLen)
         {
             // we need more data
             outlength = 0;
             return;
         }
         // start decryption
         _decryptIVReceived = true;
         byte[] iv = _decCircularBuffer.Get(ivLen);
         initCipher(iv, false);
     }
     byte[] cipher = _decCircularBuffer.ToArray();
     cipherUpdate(false, cipher.Length, cipher, outbuf);
     // move pointer only
     _decCircularBuffer.Skip(_decCircularBuffer.Size);
     outlength = cipher.Length;
     // done the decryption
 }
        public override void Decrypt(byte[] buf, int length, byte[] outbuf, out int outlength)
        {
            _decCircularBuffer.Put(buf, 0, length);
            if (!_decryptIVReceived)
            {
                if (_decCircularBuffer.Size <= ivLen)
                {
                    // we need more data
                    outlength = 0;
                    return;
                }
                // start decryption
                _decryptIVReceived = true;
                var iv = ivLen == 0 ? new byte[0] : _decCircularBuffer.Get(ivLen); //none rc4
                InitCipher(iv, false);
            }
            var cipher = _decCircularBuffer.ToArray();

            CipherUpdate(false, cipher.Length, cipher, outbuf);
            // move pointer only
            _decCircularBuffer.Skip(_decCircularBuffer.Size);
            outlength = cipher.Length;
            // done the decryption
        }
示例#4
0
        public override void Decrypt(byte[] buf, int length, byte[] outbuf, out int outlength)
        {
            Debug.Assert(_decCircularBuffer != null, "_decCircularBuffer != null");
            int bufSize;

            outlength = 0;
            // drop all into buffer
            _decCircularBuffer.Put(buf, 0, length);

            Logging.Debug("---Start Decryption");
            if (!_decryptSaltReceived)
            {
                bufSize = _decCircularBuffer.Size;
                // check if we get the leading salt
                if (bufSize <= saltLen)
                {
                    // need more
                    return;
                }
                _decryptSaltReceived = true;
                byte[] salt = _decCircularBuffer.Get(saltLen);
                InitCipher(salt, false, false);
                Logging.Debug("get salt len " + saltLen);
            }

            // handle chunks
            while (true)
            {
                bufSize = _decCircularBuffer.Size;
                // check if we have any data
                if (bufSize <= 0)
                {
                    Logging.Debug("No data in _decCircularBuffer");
                    return;
                }

                // first get chunk length
                if (bufSize <= CHUNK_LEN_BYTES + tagLen)
                {
                    // so we only have chunk length and its tag?
                    return;
                }

                #region Chunk Decryption

                byte[] encLenBytes       = _decCircularBuffer.Peek(CHUNK_LEN_BYTES + tagLen);
                uint   decChunkLenLength = 0;
                byte[] decChunkLenBytes  = new byte[CHUNK_LEN_BYTES];
                // try to dec chunk len
                cipherDecrypt(encLenBytes, CHUNK_LEN_BYTES + (uint)tagLen, decChunkLenBytes, ref decChunkLenLength);
                Debug.Assert(decChunkLenLength == CHUNK_LEN_BYTES);
                // finally we get the real chunk len
                ushort chunkLen = (ushort)IPAddress.NetworkToHostOrder((short)BitConverter.ToUInt16(decChunkLenBytes, 0));
                if (chunkLen > CHUNK_LEN_MASK)
                {
                    // we get invalid chunk
                    Logging.Error($"Invalid chunk length: {chunkLen}");
                    throw new CryptoErrorException();
                }
                Logging.Debug("Get the real chunk len:" + chunkLen);
                bufSize = _decCircularBuffer.Size;
                if (bufSize < CHUNK_LEN_BYTES + tagLen /* we haven't remove them */ + chunkLen + tagLen)
                {
                    Logging.Debug("No more data to decrypt one chunk");
                    return;
                }
                IncrementNonce(false);

                // we have enough data to decrypt one chunk
                // drop chunk len and its tag from buffer
                _decCircularBuffer.Skip(CHUNK_LEN_BYTES + tagLen);
                byte[] encChunkBytes = _decCircularBuffer.Get(chunkLen + tagLen);
                byte[] decChunkBytes = new byte[chunkLen];
                uint   decChunkLen   = 0;
                cipherDecrypt(encChunkBytes, chunkLen + (uint)tagLen, decChunkBytes, ref decChunkLen);
                Debug.Assert(decChunkLen == chunkLen);
                IncrementNonce(false);

                #endregion

                // output to outbuf
                Buffer.BlockCopy(decChunkBytes, 0, outbuf, outlength, (int)decChunkLen);
                outlength += (int)decChunkLen;
                Logging.Debug("aead dec outlength " + outlength);
                if (outlength + 100 > TCPHandler.BufferSize)
                {
                    Logging.Debug("dec outbuf almost full, giving up");
                    return;
                }
                bufSize = _decCircularBuffer.Size;
                // check if we already done all of them
                if (bufSize <= 0)
                {
                    Logging.Debug("No data in _decCircularBuffer, already all done");
                    return;
                }
            }
        }