---
title: "Message Authentication Code (MAC) — definition | Nuri Glossary"
description: "A tag proving a message came from someone holding a shared secret."
lang: "en"
type: "glossary-term"
canonical: "https://nuri.com/glossary/message-authentication-code"
---

# Message Authentication Code (MAC)

A tag proving a message came from someone holding a shared secret.

## Basically

A message authentication code is a short tag made from a message and a secret key shared by both sides. It shows that the message is genuine and unchanged.

## Key Takeaways

- A message authentication code (MAC) is a short cryptographic tag computed from a message and a shared secret key. It proves that the message has not been tampered with and was produced by someone who holds the key.
- MACs use symmetric keys, with the same secret on both sides, unlike digital signatures, which use asymmetric key pairs. MACs are faster but cannot provide non-repudiation.
- HMAC-SHA512 is the core primitive behind BIP-32 hierarchical deterministic wallet key derivation. The Poly1305 MAC secures every message in Lightning Network transport encryption.

## What Is a Message Authentication Code?

A message authentication code (MAC) is a fixed-length cryptographic value produced by combining a message with a secret key through a specific algorithm. The recipient shares the same key, recomputes the MAC over the received message and compares it with the attached tag. Matching values confirm integrity, meaning the message was not altered in transit, and authenticity, meaning someone with the secret key created it.
MACs occupy a middle ground between simple error-detection mechanisms and full digital signatures. A checksum or CRC can detect accidental corruption, but an attacker can trivially craft a new message with a matching checksum. Digital signatures use asymmetric keys to provide public verifiability and non-repudiation at a higher computational cost, while MACs offer strong tamper protection with faster symmetric cryptography and require both parties to share the same secret key beforehand.
The formal security goal for a MAC is existential unforgeability under chosen-message attack (EUF-CMA). An adversary cannot forge a valid MAC for any new message, even after requesting valid MACs for arbitrary messages of their choosing.

## How It Works

A MAC algorithm takes two inputs: a secret key and a variable-length message. It produces a fixed-length tag through these steps:
A MAC's security rests on the secrecy of the key. An attacker cannot produce a valid tag for a forged message without the key, even after observing many valid message-tag pairs.

## HMAC: Hash-Based MAC

HMAC (Hash-based Message Authentication Code) is the most widely deployed MAC construction, defined in RFC 2104 and standardised by NIST FIPS 198-1. It works with any cryptographic hash function: HMAC-SHA256 produces a 32-byte tag, while HMAC-SHA512 produces a 64-byte tag.
The HMAC formula uses two passes through the hash function. Each pass uses a different key-derived padding value:
HMAC's double-hash construction is not redundant. A naive approach like H(key // message) is vulnerable to length-extension attacks with Merkle-Damgard hash functions such as SHA-256: an attacker who knows H(key // message) can compute H(key // message // padding // extra) without knowing the key. HMAC's inner and outer hash passes eliminate this vulnerability by ensuring the intermediate hash result cannot be exploited.

## Other MAC Algorithms

HMAC dominates in protocol design. Several other MAC constructions serve specific purposes:

## MAC vs Digital Signature vs Checksum

MACs are often confused with two other mechanisms. A comparison shows where they fit:
The non-repudiation distinction is critical. Both parties in a MAC scheme share the same secret key, so either one could have generated any given tag. In a dispute, the sender can plausibly deny authorship by claiming the receiver forged the tag with the same key, while digital signatures solve this because only the private key holder can sign and anyone with the corresponding public key can verify.

## BIP-32 Key Derivation

Bitcoin's BIP-32 standard for hierarchical deterministic wallets relies on HMAC-SHA512 as its fundamental building block. Master key generation from a seed phrase uses the following computation:
Every child key derivation also uses HMAC-SHA512, with the parent chain code as the HMAC key and the parent public key, or private key for hardened derivation, concatenated with the child index as the data. The MAC construction ensures that knowing one child key does not reveal sibling keys or the parent key. The chain code serves as an additional entropy source that prevents key compromise from propagating through the derivation tree.

## Lightning Network Transport Encryption

The Lightning Network's encrypted transport layer (BOLT #8) uses ChaCha20-Poly1305, an AEAD (Authenticated Encryption with Associated Data) construction defined in RFC 8439. ChaCha20 provides the stream cipher for confidentiality, while Poly1305 generates a 16-byte authentication tag for each encrypted message.
The protocol follows the Noise framework pattern Noise_XK_secp256k1_ChaChaPoly_SHA256. A three-act handshake establishes shared symmetric keys via ECDH, after which every subsequent message is encrypted and authenticated. Poly1305 ensures that any modification to a ciphertext, even a single flipped bit, is detected and rejected; every 1,000 encryptions, the keys are rotated using HKDF to provide backward secrecy, so compromising the current key cannot decrypt previously captured traffic.

## TLS and HTTPS

Modern TLS connections (TLS 1.3) exclusively use AEAD cipher suites where the MAC is integrated into the encryption step. The two mandatory cipher suites are AES-128-GCM, using GMAC, and AES-256-GCM. ChaCha20-Poly1305 is also widely supported, especially on devices without hardware AES acceleration, and every HTTPS request you make is authenticated at the record layer by one of these MAC algorithms.

## API Authentication

HMAC is widely used to authenticate API requests, with the client and server sharing a secret API key. The client computes an HMAC over the request parameters, including method, path, timestamp and body, and includes the tag in a request header; the server recomputes the HMAC to verify the request's authenticity and integrity. Cryptocurrency exchanges, payment processors and cloud services use this pattern to secure programmatic access.

## Why It Matters for Bitcoin and Payments

MACs are woven into nearly every layer of Bitcoin's security model, even though they operate quietly beneath higher-level abstractions. Key derivation that transforms a seed into millions of addresses, encrypted peer-to-peer connections between Lightning nodes and authenticated communication channels between wallet software and hardware signing devices all depend on MAC algorithms.
MACs explain certain cryptographic choices in payment infrastructure built on Bitcoin. BIP-32 uses HMAC-SHA512 rather than a simple hash because the MAC construction binds the chain code, acting as a key, to the derivation process, preventing an attacker who obtains a child private key from computing parent or sibling keys. The Lightning Network uses Poly1305 rather than HMAC because its polynomial evaluation is faster for high-throughput message authentication, and the per-message key generation from ChaCha20 naturally satisfies Poly1305's one-time key requirement.
Symmetric MAC algorithms like HMAC remain relatively resilient as post-quantum cryptography becomes increasingly relevant. Grover's algorithm can theoretically halve the effective security of symmetric primitives, but HMAC-SHA256's 256-bit key space still provides 128 bits of post-quantum security, well above practical attack thresholds. Elliptic-curve-based digital signatures contrast with this because Shor's algorithm could break them entirely on a sufficiently large quantum computer.

## Key Management

A MAC is only as secure as its key. A compromised shared secret lets an attacker forge valid tags for arbitrary messages, and the bilateral compromise removes all security guarantees for both sender and receiver simultaneously, unlike digital signatures, where compromise of one party's key does not affect the other. Secure key distribution, storage and rotation are essential.

## No Non-Repudiation

Shared keys mean MACs cannot prove which party generated a particular tag because both parties hold the same key. Many protocols accept this limitation: a Lightning node only needs to verify that its peer sent a message, not prove it to a third party. Scenarios requiring legal accountability or dispute resolution need digital signatures when non-repudiation is required.

## Algorithm-Specific Pitfalls

Different MAC algorithms have different failure modes. Poly1305 keys must never be reused across messages because key reuse allows an attacker to recover the secret through algebraic attacks; GMAC requires unique nonces per message under the same key because a nonce collision leaks the authentication key. HMAC is the most forgiving construction, requiring no nonce and tolerating key reuse across messages, which partly explains its dominance in protocol design.

## Tag Verification Timing

MAC verification must use constant-time comparison. A naive byte-by-byte comparison that returns early on the first mismatch leaks information about how many bytes of the tag matched, enabling a timing side-channel attack; attackers can forge valid tags one byte at a time by measuring response latencies. Cryptographic libraries provide constant-time comparison functions, such as CRYPTO_memcmp or hmac.compare_digest, to prevent this.
The glossary entry is for informational purposes only and does not constitute financial or investment advice. You should always do your own research before using any protocol or technology.
Part of the [Nuri glossary](https://nuri.com/glossary), which defines 1876 Bitcoin, Lightning, wallet, stablecoin and payment terms.
