liveBook · Manning

Message authentication code

As explained in Software Security for Developers

A message authentication code is a keyed value that enables verification of a message's integrity and origin.

A message authentication code (MAC) is a keyed value attached to a message so the recipient can check its integrity and origin. Unlike a plain hash, which anyone can calculate from the message, a MAC requires a secret key shared by the sender and recipient. An attacker may read or alter the message but cannot create a valid MAC without that key. section 4.5

Stopping a fraudulent refund

A payment service receives refunds.json from a warehouse service. A plain SHA-256 file hash can reveal accidental corruption, but an attacker could change both the refund file and its hash. With a MAC, the warehouse computes a value from the file and their shared secret; the payment service recomputes it and processes the file only when both values match. section 4.5

A MAC exchange follows this pattern:

  • The sender creates the message, computes its MAC using the secret key, and sends both message and MAC. section 4.5
  • The recipient computes a MAC over the received message and compares it with the supplied value. section 4.5
  • Matching values indicate that the message was not modified and came from a party holding the shared key. section 4.5
  • Do not improvise key mixing by hashing message || secret; use an established MAC construction such as HMAC. section 4.5
Figure 4.8 The MAC is data added to the end of a message that the receiver can use to verify who sent the message and whether it was tampered with.
figure

Visualizes the MAC as data added to a message for checking its sender and detecting tampering. section 4.5

MACs can protect more than a standalone message. A MAC can cover a plaintext header together with an encrypted body, binding metadata to ciphertext and helping prevent header changes or message swapping. AEAD modes combine encryption and authentication in one operation. section 5.4

How Software Security for Developers covers it

How deep it goes
Explains keyed verification, exchange steps, hash contrast, and unsafe key concatenation, but avoids mathematical proofs and protocol internals.
Examples
Uses warehouse refunds, modified files, TLS tampering, and authenticated headers with encrypted bodies as concrete examples.
What it assumes
Assumes only basic message exchange and hashing, while teaching cryptographic primitives from an application-developer starting point.
What you can do after
Diagnose tampering and apply keyed integrity checks when designing service-to-service messages or protected payloads.
How it connects
Connects message authentication codes with cryptographic hash functions, HMAC, symmetric key cryptography, TLS, JOSE, and AEAD.

Also explained in

Compare how 4 books explain message authentication code →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Security for Developers. The section links point to the parts of the book it draws on.