Where MAC and HMAC are implemented in the Lightning Protocol — and why
Here we will discuss the implementation of MAC and HMAC in Lightning node communications and why they are so important.
Where are MAC and HMAC implemented in the Lightning Protocol?
Let’s start with the basics first.
What are MAC and HMAC?
MAC stands for Message Authentication Code. It is a tag we use to prove that the message we are sending is authentic — not altered — and that it came from the right sender. A MAC is a one-way function, meaning that it is easy to compute the MAC of a message, but it is hard to compute the message from the MAC.
Let’s take an example without and with a MAC.
- Without a MAC
In this context, we have two nodes, Olaniran and John. Since we have an open channel, we have a way to communicate between us. Actually Olaniran can send a message to John, and he can respond back to Olaniran.
Our current configuration looks like:

In this case, we are exposed to rewriting attacks, where an attacker node can intercept the message and rewrite it. This is a problem because we can’t be sure if the message is authentic or not.

- With a MAC
To prevent this, we are going to add a kind of tag to the message. This tag is called a MAC (Message Authentication Code).

But another problem remains: how do we ensure that we pass the message and the key K in the right order so that our partner can verify the authenticity and integrity of our message?
Since MAC(message, key) is different from MAC(key, message), we have to define an order to create the MAC tag.

At this point, we can introduce HMAC, which is a MAC implementation based on hash functions.

HMAC is now here to provide:
- More privacy
- Message integrity and authenticity
- Key integrity and authenticity
Why and where?
From this we can conclude that MAC and HMAC are essentially used to prove the authenticity of communications and messages between nodes.
In Lightning, it covers:
- Message transport
- Onion routing
- Payment channels (channel state)
- Hashed Timelock Contracts (HTLCs)