2

It's a well-known best practice to not use one key to both encrypt and MAC data. In my application, there are instances where I MAC a piece of data without having encrypted it first. Do I still need a different key than the one used for encryption? Is it OK to use the same key to MAC one piece of data if I'm going to re-use that key to encrypt some other data under the same key?

D.W.
  • 36,365
  • 13
  • 102
  • 187
pg1989
  • 4,636
  • 23
  • 42
  • 1
    I'm confused by the juxtaposition of "best practice" and your situation. You state best practice is to not use the same key for MAC and encryption, then ask if you need a different key for MACing and encrypting the plaintext, which is answered by your statement of best practice. Are you trying to confirm whether that is indeed best practice? – B-Con Jun 13 '13 at 21:10
  • 1
    @B-Con: The way I interpret this question, he isn't asking whether or not you should encrypt and MAC the same data with the same key. Rather, he's asking is it okay if you MAC some data and then encrypt some other data with the same key. – Reid Jun 13 '13 at 22:01
  • 1
    Reid is correct, sorry if that was ambiguous. – pg1989 Jun 13 '13 at 22:45

2 Answers2

3

It depends on how you encrypt and mac.

If you can still choose then you should use an authenticated encryption with associated data mode (AEAD) like Galois/Counter Mode. If all data you want authenticated in a message has to be transmitted in the clear then just leave the encrypted part empty. These modes have the advantage that they were designed to use just one key and have solid security proofs.

For any other combinations it depends on what algorithms and modes you use for encryption and mac. Mostly this analysis is not worth the trouble, as deriving two keys from one master key is pretty simple (HMAC(masterKey,"Key one"), and HMAC(masterKey,"Key two") for example, or AES(masterKey,0x0…01) and AES(masterKey,0x0…02)). Also any custom crypto (and using the same key twice this way is non-standard) has the disadvantage that code and security reviews get way more complicated and any error is on you.

If you use two algorithms with completely different roots (for example Sha256-HMAC and AES counter mode) you are probably on the safe side, but that is just my feeling with no rigid argumentation behind it.

Perseids
  • 562
  • 3
  • 13
  • 1
    I'm using AES/GCM to encrypt and HmacSHA256 for MACing. – pg1989 Jun 13 '13 at 22:46
  • 2
    Then it's probably okay, but it still remains bad practice. Avoid it if at all possible.

    Also AES/GCM is an AEAD scheme. Why don't you just AES/GCM for MAC as well? Be careful to never use the same combination of key nonce

    – Perseids Jun 14 '13 at 08:40
  • 2
  • never use the same combination of key and nonce though, as this destroys confidentiality of those messages with the same (nonce,key) and integrity protection of all messages with this key.
  • – Perseids Jun 14 '13 at 08:50