A message authentication code (MAC) is a short cryptographic value computed from a message and a secret key, allowing a recipient who possesses the key to check whether the message has been altered or generated by a party without that key. It is a primitive of symmetric-key cryptography that provides message integrity and a form of data-origin authentication. A MAC does not conceal the message, establish its freshness, or distinguish among multiple parties who possess the same key.
The term refers both to the keyed algorithm and, in common usage, to its output, also called an authentication tag. MACs are used in network protocols, authenticated data storage, financial messaging, and software interfaces.
Basic operation
A conventional MAC scheme has three components:
- Key generation, which produces a secret key .
- Tag generation, which computes a tag for a message .
- Verification, which checks whether a received message and tag are valid under the key.
For a deterministic MAC, tag generation is commonly written as
The sender transmits and . The receiver computes the expected tag using the same key and checks whether it matches the received tag. A more general description uses a verification algorithm:
Some constructions additionally require a nonce: a value that must satisfy the construction’s uniqueness or other specified requirements. Such a scheme can be expressed as
where is the nonce. It need not be secret, but incorrect nonce handling can undermine security.
The message can be any precisely defined sequence of bytes. Its interpretation as a payment instruction, network packet, file, or structured object belongs to the surrounding protocol. A valid tag authenticates the bytes covered by the calculation, not an interpretation that differs from what those bytes actually encode.
Security properties
Resistance to forgery
The central security goal is that an attacker without the key cannot produce an acceptable tag for an unauthorized message, even after obtaining tags for messages of the attacker’s choice.
A standard formalization is existential unforgeability under chosen-message attack. In this model, an attacker can request tags for selected messages and succeeds by producing a valid message–tag pair for a message not previously submitted for authentication. A stronger variant also treats a new valid tag for a previously queried message as a forgery; this distinction matters for schemes that permit multiple valid tags for one message.
This model reflects practical settings in which an attacker may observe many authenticated messages or cause legitimate systems to authenticate selected inputs. Security must therefore depend on more than keeping previous messages and tags secret.
Tag length and guessing
An attacker can always attempt to guess a tag. For an ideal scheme with uniformly distributed -bit tags, one blind attempt succeeds with probability
For independent guesses against such tags, the probability of at least one success is
when is small compared with .
These expressions describe guessing, not a complete security bound for every construction. Actual bounds may also depend on message lengths, total authenticated data, the number of users or keys, and weaknesses in the underlying primitive. Truncating a tag reduces its resistance to guessing.
What a MAC does not establish
A MAC has several important limits:
- No confidentiality. The message remains readable unless protected separately by encryption.
- No inherent freshness. An old, valid message–tag pair can remain valid.
- No unique attribution among key holders. Every party able to generate tags with the same key can impersonate every other such party at the MAC layer.
- No protection after key compromise. An attacker who learns the key can generally generate valid tags.
- No availability guarantee. Authentication does not prevent messages from being blocked, delayed, or discarded.
A successful verification establishes a relationship among the key, message, and tag under the scheme’s security assumptions. It does not establish that the message is factually true or that the requested operation is authorized.
Relationship to other mechanisms
Hashes and checksums
A cryptographic hash function ordinarily maps an input to a fixed-length digest without a secret key. Anyone can recompute that digest after changing the message. Consequently, transmitting a message alongside its unkeyed hash does not by itself protect against deliberate modification.
Checksums and error-correcting codes primarily address accidental corruption rather than adversarial forgery. Their redundancy is generally public and can be recalculated by an attacker.
A MAC incorporates a secret that the attacker lacks. However, simply placing a secret next to a message and hashing the result is not automatically a secure construction. For example, the naive design is susceptible to length-extension attacks when used with certain common hash constructions.
Digital signatures
A digital signature uses a private signing key and a distinct public verification key. A MAC instead normally uses a shared secret for both generation and verification.
This difference affects who can verify and who can forge. A party holding a MAC verification key can normally generate valid tags as well. A party holding only a signature verification key cannot ordinarily create signatures. MACs therefore do not provide transferable proof that one particular key holder, rather than another, generated a message.
Digital signatures can support attribution and non-repudiation mechanisms, although their evidentiary meaning also depends on key management, identity binding, procedures, and the applicable legal framework.
Major constructions
MACs are built from several kinds of cryptographic primitives. Their security requirements and operational limits are not interchangeable.
HMAC
HMAC constructs a MAC from a cryptographic hash function using a nested design:
Here, is the key normalized to the hash function’s block size, denotes exclusive OR, denotes concatenation, and and are distinct fixed padding values.
The nested structure avoids weaknesses of several naive keyed-hash constructions. HMAC’s security properties are not identical to the collision resistance of its underlying hash: finding ordinary hash collisions does not automatically produce an HMAC forgery.
HMAC was developed in the 1990s and standardized for Internet use in RFC 2104 in 1997. HMAC-SHA-256 is a widely deployed instance.
CBC-MAC and CMAC
CBC-MAC constructs a tag by processing message blocks through a block cipher in a chaining arrangement. Its basic form is suitable for fixed-length messages under appropriate conditions, but it is not a secure general-purpose MAC for unrestricted variable-length messages. Authenticating messages of different lengths requires additional structure or a construction designed for that purpose.
CMAC modifies this approach with derived subkeys and different treatment of the final block. It supports variable-length messages without the straightforward extension weaknesses of basic CBC-MAC. CMAC is commonly instantiated using the Advanced Encryption Standard and was standardized by the US National Institute of Standards and Technology in 2005.
Universal-hash-based MACs
Another family combines a keyed universal hash with a secret masking or pseudorandom component. These constructions can be efficient because much of the message processing uses inexpensive arithmetic.
Poly1305 is a prominent example. It evaluates a polynomial over a finite field and combines the result with a secret value to produce a tag. Its authentication key is a one-time key: reuse for distinct messages can permit forgery. In constructions such as ChaCha20-Poly1305, the one-time Poly1305 key is derived from a longer-term key and a nonce.
GMAC, the authentication-only specialization of Galois/Counter Mode, is another important construction. It relies on nonce uniqueness under a given key; reuse can severely compromise authentication.
“One-time key” does not necessarily mean that a system needs an independently distributed long-term secret for every message. A larger construction can derive per-message keys, provided its derivation and nonce requirements are satisfied.
Authentication combined with encryption
Many applications require both confidentiality and authenticity. Combining an encryption algorithm with a MAC is a protocol-design problem, not merely a matter of applying both in an arbitrary order.
In an encrypt-then-MAC arrangement, the sender encrypts the plaintext and then authenticates the ciphertext together with relevant parameters and context. Under suitable assumptions, this composition provides strong security. Other arrangements require careful analysis; historical protocols have suffered from interactions among authentication checks, padding validation, and observable error behavior.
Modern systems often use authenticated encryption with associated data (AEAD). An AEAD scheme protects the confidentiality and integrity of plaintext while also authenticating associated data that remains unencrypted. Examples include AES-GCM and ChaCha20-Poly1305.
Associated data can include packet headers, protocol identifiers, or sequence numbers. Such fields can influence how a message is processed even when they do not require secrecy.
Transport Layer Security illustrates this evolution. Earlier cipher suites used separate encryption and MAC mechanisms, while TLS 1.3 uses AEAD algorithms for record protection.
Protocol and implementation considerations
Replay protection
A replay attack retransmits a previously valid message. Because the original tag is still correct, MAC verification alone does not detect it.
Protocols establish freshness through mechanisms such as authenticated sequence numbers, challenges, nonces, timestamps, or transaction identifiers, together with receiver state and acceptance rules. Merely including a nonce does not prevent replay if the receiver accepts repeated values.
Message encoding and context
The authenticated representation must be unambiguous. If two fields are concatenated without boundaries, different pairs of values can produce identical bytes. For example, the pairs ("ab", "c") and ("a", "bc") both produce abc.
Length prefixes, well-defined encodings, and explicit field boundaries prevent this ambiguity. Protocol identifiers and message-type labels can also prevent a valid tag from one context from being reused in another.
Structured formats such as JSON introduce further questions: whether tags cover transmitted bytes or a canonical representation, how duplicate fields are interpreted, and whether sender and receiver parse the same input identically.
Key handling and verification
A MAC’s security depends on the secrecy and suitability of its key. Passwords do not ordinarily have the same unpredictability as randomly generated cryptographic keys. Systems using a password as the underlying secret require a defined key-derivation mechanism and a threat model that accounts for password guessing.
Using separate keys, or explicitly separated derivations, for different purposes reduces unintended interactions between protocols and algorithms. This principle is commonly called domain separation.
Verification implementations must also account for side-channel attacks. A comparison routine that reveals how much of a tag matched can leak information through timing or other observable behavior. Constant-time comparison is intended to avoid data-dependent behavior in this check, although it does not by itself eliminate every side channel in a system.
Authentication is also distinct from access control. A valid tag can identify an authorized key holder’s message without establishing that the requested operation is permitted.
Applications and limitations
MACs are used to authenticate network traffic, detect unauthorized changes to stored records, protect inter-service messages, and validate requests to an application programming interface. HMAC-based request authentication commonly covers selected request fields together with contextual information such as a timestamp or request identifier.
In storage systems, authenticating only a data block’s contents may leave substitution attacks possible. An attacker could replace one valid block with another valid block. Binding tags to record identifiers, storage positions, or version numbers addresses different forms of substitution, while protection against rollback generally requires trusted state beyond the tag itself.
The choice of MAC depends on the available primitives, performance requirements, tag size, message volume, nonce-management model, and interoperability requirements. Even a secure construction can fail when its key is exposed, its usage limits are exceeded, its authenticated fields omit important context, or its surrounding protocol accepts replayed messages.