Take the 2-minute tour ×
Cryptography Stack Exchange is a question and answer site for software developers, mathematicians and others interested in cryptography. It's 100% free, no registration required.

Most of the time, when some data must be encrypted, it must also be protected with a MAC, because encryption protects only against passive attackers. There are some nifty encryption modes which include a MAC (EAX, GCM...) but let's assume that we are doing old-style crypto, so we have a standalone encryption method (e.g. AES with CBC chaining and PKCS#5 padding) and a standalone MAC (e.g. HMAC with SHA-256). How should we assemble the encryption and the MAC?

  • Compute the MAC on the cleartext, append it to the data, and then encrypt the whole? (That's what TLS does)
  • Compute the MAC on the cleartext, encrypt the cleartext, and then append the MAC at the end of the ciphertext?
  • Encrypt the cleartext, then compute the MAC on the ciphertext, and append it to the ciphertext? (In that case, we do not forget to include the IV and the encryption method identifier into the MACed data.)

The first two options are often called "MAC-then-encrypt" while the third is "encrypt-then-MAC". What are the arguments for or against either?

share|improve this question
4  
I've heard the second method most commonly referred to as encrypt-and-mac. –  Stavros Korokithakis Oct 12 '13 at 14:35
    
I am a bit perplexed by the fact that this question seems highly related to crypto.stackexchange.com/questions/5458/…, but has diametrically opposed answers... –  Clément Mar 24 at 12:41
    
I haven't got time to write it up at the moment, but this forthcoming EuroCrypt paper is well worth a read –  figlesquidge Apr 3 at 9:31
3  
@Clément: the confusion comes from the widespread (but wrong) habit of calling MAC "signatures". In fact MAC and signatures are very different things used in very different contexts. Sign-then-encrypt protocols also use a distinct encryption key for each message, which nullifies all padding oracle attacks; and the signature is meant to serve as proof (e.g. in a trial), so it MUST be applied on the plaintext message. In MAC+encrypt contexts, the same symmetric key is often reused, and there is no "proof" requirement. –  Thomas Pornin Apr 3 at 10:56

6 Answers 6

up vote 86 down vote accepted

I'm assuming you actually know all of this better than I do... anyway, this paper neatly summarises all these approaches and what level of security they do or don't provide. I shall paraphrase it in English, rather than Mathematical notation, as I understand it, here:

  • Encrypt-then-MAC:
    • Provides integrity of Ciphertext. Assuming the MAC shared secret has not been compromised, we ought to be able to deduce whether a given ciphertext is indeed authentic or has been forged; for example, in public key cryptography anyone can send you messages. EtM ensures you only read valid messages.
    • Plaintext integrity.
    • If the cipher scheme is malleable we need not be so concerned, since the MAC code will filter out this invalid ciphertext.
    • The MAC does not provide any information on the plaintext since, assuming the output of the cipher appears random, so does the MAC. In other words, we haven't carried any structure from the plaintext into the MAC code.
  • MAC-then-Encrypt:
    • Does not provide any integrity on the ciphertext, since we have no way of knowing until we decrypt the message whether it was indeed authentic or spoofed.
    • Plaintext integrity.
    • If the cipher scheme is malleable it may be possible to alter the message to appear valid and have a valid MAC code. This is a theoretical point, of course, since practically speaking the MAC secret should provide protection.
    • Here, the MAC cannot provide any information on the plaintext either, since it is encrypted.
  • Encrypt-and-MAC:
    • No integrity on the ciphertext again, since the MAC is taken against the plaintext. This opens the door to some chosen-ciphertext attacks on the cipher, as shown in section 4 of Breaking and provably repairing the SSH authenticated encryption scheme: A case study of the Encode-then-Encrypt-and-MAC paradigm.
    • Integrity of the plaintext can be verified
    • If the cipher scheme is malleable, the contents of the ciphertext could well be altered, but on decryption we ought to find the plaintext is invalid. Of course, any implementation error that can be exploited in the decryption process has been by that point.
    • May reveal information about the plaintext in the MAC. Theoretical, of course, but a less than ideal scenario. This occurs if the plaintext messages are repeated, and the MACed data does not include a counter (it does in the SSH 2 protocol, but only as a 32-bit counter, so you should take care to rekey before it overflows).

In short, Encrypt-then-MAC is the most ideal scenario. Any modifications to the ciphertext that do not also have a valid MAC code can be filtered out before decryption, protecting against any attacks on the implementation. The MAC cannot, also, be used to infer anything about the plaintext. MAC-then-Encrypt and Encrypt-and-MAC both provide different levels of security, but not the complete set provided by Encrypt-then-MAC.

share|improve this answer
    
Also see the unpublished paper at citeseerx.ist.psu.edu/viewdoc/… –  Fixee Sep 1 '11 at 23:00
1  
Please also note the "padding oracle attack" in the answer from Thomas. –  owlstead Nov 29 '11 at 20:55
5  
For personal/future reference: Encrypt-then-MAC = Encrypt the plaintext, MAC the ciphertext + iv then append it to the ciphertext. MAC-then-Encrypt = MAC the plaintext then append the MAC to the plaintext then Encrypt it all. Encrypt-and-MAC = Encrypt and MAC the plaintext then append the MAC onto the ciphertext. –  MD Kieran Jun 25 '13 at 21:23
1  
@clement it is a good point although I don't think you'd use a mac for identity verification... but there are definitely those who disagree that encrypt-then-mac is the best solution and their arguments are very very valid too. –  Ninefingers Mar 30 at 19:21
1  
@Clément I have had a go at explaining the difference via a separate question, see crypto.stackexchange.com/q/15485/46 - feel free to ask for clarification there :) –  Ninefingers Apr 9 at 9:18

@Ninefingers answers the question quite well; I just want to add a few details.

Encrypt-then-MAC is the mode which is recommended by most researchers. Mostly, it makes it easier to prove the security of the encryption part (because thanks to the MAC, a decryption engine cannot be fed with invalid ciphertexts; this yields automatic protection against chosen ciphertext attacks) and also avoids any trouble to confidentiality from the MAC (since the MAC operates on the encrypted text, it cannot reveal anything about the plaintext, regardless of its quality). Note that the padding oracle attacks, which have been applied in the field to ASP.NET, are chosen ciphertext attacks.

Ferguson and Schneier, in their book Practical Cryptography, have argued the opposite: that MAC-then-encrypt (or MAC-and-encrypt) is the "natural" order and that encrypt-then-MAC is overly complex. The sore point of encrypt-then-MAC is that you have to be careful about what you MAC: you must not forget the IV, or (in case the protocol allows algorithm flexibility) the unambiguous identifier for the encryption algorithm; otherwise, the attacker could change either, inducing a plaintext alteration which would be undetected by the MAC. To prove their point, Ferguson and Schneier describe an attack over an instance of IPsec in which the encrypt-then-MAC was not done properly.

So while encrypt-then-MAC is theoretically better, it is also somewhat harder to get right.

share|improve this answer

Hugo Krawczyk has a paper titled The Order of Encryption and Authentication for Protecting Communications (or: How Secure Is SSL?). It identifies 3 types of combining authentication (MAC) with encryption:

  1. Encrypt then Authenticate (EtA) used in IPsec;
  2. Authenticate then Encrypt (AtE) used in SSL;
  3. Encrypt and Authenticate (E&A) used in SSH.

It proves that EtA is the secure way to use, and both AtE and E&A are subject to attacks, unless the encryption method is either in CBC mode or it is a stream cipher.

The abstract says everything; I emphasized important parts by bolding them:

We study the question of how to generically compose symmetric encryption and authentication when building “secure channels” for the protection of communications over insecure networks. We show that any secure channels protocol designed to work with any combination of secure encryption (against chosen plaintext attacks) and secure MAC must use the encrypt-then-authenticate method. We demonstrate this by showing that the other common methods of composing encryption and authentication, including the authenticate-then-encrypt method used in SSL, are not generically secure. We show an example of an encryption function that provides (Shannon’s) perfect secrecy but when combined with any MAC function under the authenticate-then-encrypt method yields a totally insecure protocol (for example, finding passwords or credit card numbers transmitted under the protection of such protocol becomes an easy task for an active attacker). The same applies to the encrypt-and-authenticate method used in SSH.

On the positive side we show that the authenticate-then-encrypt method is secure if the encryption method in use is either CBC mode (with an underlying secure block cipher) or a stream cipher (that xor the data with a random or pseudorandom pad). Thus, while we show the generic security of SSL to be broken, the current practical implementations of the protocol that use the above modes of encryption are safe.

share|improve this answer
    
CBC decrypt before authentication in an online prococol is secure? What about padding oracle attacks? Or do they explicitly specify that you need to verify the MAC in the last block before unpadding? –  owlstead Nov 29 '11 at 21:04

There is no property of a MAC that states that information about the input should not be leaked. As such, you should encrypt the message first, then apply a MAC. This way, even if the MAC leaks information, all that is leaked is ciphertext.

share|improve this answer

Moxie Marlinspike calls it in his article http://www.thoughtcrime.org/blog/the-cryptographic-doom-principle/ the doom principle:

if you have to perform any cryptographic operation before verifying the MAC on a message you’ve received, it will somehow inevitably lead to doom.

He also demonstrates two attacks which are possible because of trying to decrypt before checking the MAC.

To summarize: "Encrypt Then Authenticate" is the way to go.

share|improve this answer
4  
As-is, I have a hard time finding reason to upvote this answer because your answer is close to a link-only answer. Can you elaborate a bit on what you are quoting? For example: can you explain “why” it is a problem to be able to decrypt a message before checking authentication, and why Moxie says it will “inevitably lead to doom” if you MAC-then-encrypt? That would certainly make your answer more valuable… after all, the question clearly asks “What are the arguments for or against either?” I can't really see you're providing arguments. Instead, you merely point to and quote a site. –  e-sushi Apr 3 at 10:49
    
@e-sushi Agreed - it remains that this is one of the best accessible treatment of the subject. –  louism Apr 8 at 0:51

I think Encrypt-then-MAC does not deliver Plaintext integrity, but only ciphertext integrity. If the MAC over the ciphertext is OK but then we use the wrong key to decrypt (for whatever reason), then the recipient receives a plaintext that the sender did not send and did not vouch for. If this can happen, this is a violation of plaintext integrity.

So, Encrypt-then-MAC is only secure if you can somehow be sure that decryption won't use the wrong key, and that any other processing/decoding done to the ciphertext after checking the MAC is completely correct. This is a somewhat fragile aspect of Encrypt-then-MAC, and one reason why Ferguson and Schneier advocate against Encrypt-then-MAC.

share|improve this answer
1  
I edited the answer to more clearly express the point Josef was trying to make. Personally, I think the answer is fine (I upvoted it). –  D.W. Apr 5 at 1:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.