I want to combine encryption and MAC.
For encryption I use AES-256 with CBC and PKCS5Padding. For MAC I use HmacSHA512.
I use the Encrypt-then-Mac approach (calculate MAC over the ciphertext and deliver it with the ciphertext)
The algorithm should be able to process streamed data, and if I calculate the MAC once over the whole ciphertext, there are two possibilities:
Prepend the mac to the ciphertext:
This is a very bad idea, because the encryption part has to encrypt the whole text before it can output the mac. Therefore it's not streamable. On decryption, we have to process the whole ciphertext before knowing if is compromised (also not fully streamable).
Append the mac to the ciphertext:
Better idea, but the decryption part can only check the MAC in the end. Before that, it did output many bytes that could have been changed by an attacker, or it has to wait until it sees the MAC (not streamable again).
So I need a solution which divides the ciphertext into parts and then calculates a MAC for each part. Because the ciphertext shouldn't be much larger than the plaintext, I choose a MAC block size that is greater than the encryption block size (for example 4 kB).
I want to use a pattern like (with $c_X$ = part X of the ciphertext, $m_i$ = part X of the MAC):
$$ m_1 || IV || c_1 || m_2 || c_2 || m_3 || c_3 || \dots $$
The calculation of the first MAC is clear ($k$ being the MAC key):
$$ m_1 = MAC(k, IV || c_1) $$
But then we meet problems. If we just use
$$ m_X = MAC(k, c_X), $$
this wouldn't be secure. An attacker could change the blocks in order, he could duplicate or remove whole blocks, and so on.
So I thought about an idea similar to CBC mode in encryption:
$$ m_X = MAC(k, m_{X-1} || c_X) $$
Is this now secure? And is there a difference to this one?
$$ m_X = MAC(k, c_1 || ... || c_X) $$