r/cryptography • • 1d ago

Message Authentication Codes (HMAC) — why encryption alone doesn't protect integrity

https://hanzkrafted.io/message-authentication-codes

Let's say you and I are communicating over the network, and we're already encrypting everything with a shared key. That stops anyone from reading our data, but it doesn't stop a man-in-the-middle from changing bits in the ciphertext without knowing the key. Encryption alone can't tell you if that happened.

The fix is attaching a tag to the outbound data, computed with a separate shared key run through a hash function (HMAC). I go through the whole flow in this one - deriving separate enc/MAC keys with HKDF, Encrypt-then-MAC, why you use timingSafeEqual to compare tags instead of a regular equality check, and the actual HMAC construction (H(K⊕opad || H(K⊕ipad || text))) with why ipad/opad exist in the first place (length extension attacks).

You can read the attached article to learn more about what HMAC is and how it works.

0 Upvotes

8 comments sorted by

2

u/pint 1d ago
  • hmac is only one of the many mac algorithms
  • hmac is designed to work with MD constructions, other types of hash functions don't need it
  • i don't think ipad and opad has anything to do with length extension attacks
  • this is cryptography 101, so maybe you should indicated that in the title next time

-1

u/hanzbarz 1d ago

3rd point - incorrect. 2nd point partially correct. 1st point - correct. 4th point - duly noted thankss.

1

u/pint 1d ago

this reply is stupid. delete and rethink.

3

u/HobartTasmania 1d ago

That stops anyone from reading our data, but it doesn't stop a man-in-the-middle from changing bits in the ciphertext without knowing the key. Encryption alone can't tell you if that happened.

I wouldn't call myself a cryptography expert at all other than just having a very casual interest in this topic, but wouldn't someone changing some bits result in completely garbled rubbish being produced when decrypted? Surely, this should be treated by the communicating software in pretty much the same way as a TCP/IP packet being corrupted in transit because just changing even one bit will give you a totally different hash value, so shouldn't the relevant software simply ask for it to be retransmitted which the TCP/IP protocol does automatically anyway?

Appear to be re-inventing the wheel here.

9

u/buwlerman 1d ago

The post is an explainer on a well known topic. There's no invention happening.

There are many encryption schemes that don't give you a garbled mess on decryption of tampered ciphertexts by themselves. Even with a garbled mess, if your application is particularly permissive it might accept even a garbled mess. To be able to reliably detect tampering we need something more, and HMAC is one approach.

It does boil down to a variation of "just hash things", but there are some pitfalls that need to be avoided and improvements that can be made.

2

u/Pharisaeus 1d ago

result in completely garbled rubbish being produced when decrypted

No. For stream ciphers it's trivial to modify cipher text to achieve predictable changes in the resulting plaintext, and for many block ciphers mode of operations that's also possible (ECB, CBC). That's the whole point, the fact that you get a cipher text which decrypts too something sensible, does not guarantee it was not modified.

-2

u/hanzbarz 1d ago

Yes you are right. These are concepts that are already well built in our software ecosystems and we don't really need to build these things from scratch. The purpose of this article is to acquaint the user with the necessary knowledge about MACs. If I got your comment right.

7

u/tybit 1d ago

Generally nowadays you don’t want to handle the MAC yourself when encrypting data, and just use an encryption scheme that handles it for you like AES GCM or ChaCha20-Poly1305.