package org.bouncycastle.crypto.tls;

import java.io.IOException;

import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.StreamCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.util.Arrays;

public class TlsStreamCipher implements TlsCipher
{
    protected TlsContext context;

    protected StreamCipher encryptCipher;
    protected StreamCipher decryptCipher;

    protected TlsMac writeMac;
    protected TlsMac readMac;

    public TlsStreamCipher(TlsContext context, StreamCipher encryptCipher,
        StreamCipher decryptCipher, Digest writeDigest, Digest readDigest, int cipherKeySize)
        throws IOException
    {
        this.context = context;

        this.encryptCipher = encryptCipher;
        this.decryptCipher = decryptCipher;

        int key_block_size = (2 * cipherKeySize) + writeDigest.getDigestSize()
            + readDigest.getDigestSize();

        byte[] key_block = TlsUtils.calculateKeyBlock(context, key_block_size);

        int offset = 0;

        // Init MACs
        writeMac = new TlsMac(context, writeDigest, key_block, offset, writeDigest.getDigestSize());
        offset += writeDigest.getDigestSize();
        readMac = new TlsMac(context, readDigest, key_block, offset, readDigest.getDigestSize());
        offset += readDigest.getDigestSize();

        // Build keys
        KeyParameter encryptKey = new KeyParameter(key_block, offset, cipherKeySize);
        offset += cipherKeySize;
        KeyParameter decryptKey = new KeyParameter(key_block, offset, cipherKeySize);
        offset += cipherKeySize;

        if (offset != key_block_size)
        {
            throw new TlsFatalAlert(AlertDescription.internal_error);
        }

        encryptCipher.init(true, encryptKey);
        decryptCipher.init(false, decryptKey);
    }

    public int getPlaintextLimit(int ciphertextLimit)
    {
        return ciphertextLimit - writeMac.getSize();
    }

    public byte[] encodePlaintext(long seqNo, short type, byte[] plaintext, int offset, int len)
    {
        byte[] mac = writeMac.calculateMac(seqNo, type, plaintext, offset, len);

        byte[] outbuf = new byte[len + mac.length];

        encryptCipher.processBytes(plaintext, offset, len, outbuf, 0);
        encryptCipher.processBytes(mac, 0, mac.length, outbuf, len);

        return outbuf;
    }

    public byte[] decodeCiphertext(long seqNo, short type, byte[] ciphertext, int offset, int len)
        throws IOException
    {
        byte[] deciphered = new byte[len];
        decryptCipher.processBytes(ciphertext, offset, len, deciphered, 0);

        int plaintextSize = deciphered.length - readMac.getSize();
        byte[] plainText = copyData(deciphered, 0, plaintextSize);

        byte[] receivedMac = copyData(deciphered, plaintextSize, readMac.getSize());
        byte[] computedMac = readMac.calculateMac(seqNo, type, plainText, 0, plainText.length);

        if (!Arrays.constantTimeAreEqual(receivedMac, computedMac))
        {
            throw new TlsFatalAlert(AlertDescription.bad_record_mac);
        }

        return plainText;
    }

    protected byte[] copyData(byte[] text, int offset, int len)
    {
        byte[] result = new byte[len];
        System.arraycopy(text, offset, result, 0, len);
        return result;
    }
}

@@ -685,32 +685,82 @@
 ception%0D%0A    %7B%0D%0A
+        boolean isServer = context.isServer();%0D%0A%0D%0A
         this.con
@@ -1135,17 +1135,30 @@
         
-w
+TlsMac clientW
 riteMac 
@@ -1298,20 +1298,34 @@
         
-read
+TlsMac serverWrite
 Mac = ne
@@ -1496,23 +1496,27 @@
 rameter 
-encrypt
+clientWrite
 Key = ne
@@ -1621,23 +1621,27 @@
 rameter 
-decrypt
+serverWrite
 Key = ne
@@ -1853,32 +1853,487 @@
 ;%0D%0A        %7D%0D%0A%0D%0A
+        KeyParameter encryptKey, decryptKey;%0D%0A        if (isServer)%0D%0A        %7B%0D%0A            writeMac = serverWriteMac;%0D%0A            readMac = clientWriteMac;%0D%0A            encryptKey = serverWriteKey;%0D%0A            decryptKey = clientWriteKey;%0D%0A        %7D%0D%0A        else%0D%0A        %7B%0D%0A            writeMac = clientWriteMac;%0D%0A            readMac = serverWriteMac;%0D%0A            encryptKey = clientWriteKey;%0D%0A            decryptKey = serverWriteKey;%0D%0A        %7D%0D%0A%0D%0A
         encryptC
@@ -3093,32 +3093,195 @@
 ception%0D%0A    %7B%0D%0A
+        int macSize = readMac.getSize();%0D%0A        if (len %3C macSize)%0D%0A        %7B%0D%0A            throw new TlsFatalAlert(AlertDescription.decode_error);%0D%0A        %7D%0D%0A%0D%0A
         byte%5B%5D d
@@ -3403,129 +3403,35 @@
 int 
-plaintextSize = deciphered.length - readMac.getSize();%0D%0A        byte%5B%5D plainText = copyData(deciphered, 0, plaintext
+macInputLen = len - mac
 Size
-)
 ;%0D%0A%0D
@@ -3464,61 +3464,55 @@
 c = 
-copyData(deciphered, plaintextSize, readMac.getSize()
+Arrays.copyOfRange(deciphered, macInputLen, len
 );%0D%0A
@@ -3578,38 +3578,34 @@
 pe, 
-plainText, 0, plainText.length
+deciphered, 0, macInputLen
 );%0D%0A
@@ -3789,219 +3789,54 @@
 urn 
-plainText;%0D%0A    %7D%0D%0A%0D%0A    protected byte%5B%5D copyData(byte%5B%5D text, int offset, int len)%0D%0A    %7B%0D%0A        byte%5B%5D result = new byte%5Blen%5D;%0D%0A        System.arraycopy(text, offset, result, 0, len);%0D%0A        return result
+Arrays.copyOfRange(deciphered, 0, macInputLen)
 ;%0D%0A 

Add support for use in server Add check on input length in decodeCipherText Refactoring