TL;DR
A checksum is a small number computed from a block of data. The sender attaches it to the data, and the receiver computes it again. If the two values differ, something was damaged on the way. Checksums are cheap, and they catch the random errors that noisy links and faulty memory produce, but they do not protect against deliberate tampering.
Where you'll see it: the header of every IP packet, the TCP and UDP segments inside it, Ethernet frames, ZIP and PNG files, and the sector labels on storage devices.
Intuition
Think of a checksum as a fingerprint of a message that is much shorter than the message. The sender computes the fingerprint and sends it along. The receiver computes the fingerprint of what arrived. If the message was changed, the new fingerprint will usually differ from the old one, so the receiver knows to reject the data or ask for it again.
The fingerprint is not unique. Many different messages share each possible checksum value, because a short number cannot distinguish every long message. What matters is the chance that a random error produces the same checksum by accident. A good checksum makes that chance small, around one in for a -bit checksum, for typical random damage. It also makes some specific common errors certain to be caught.
Why not just send the data twice? That doubles the traffic. A checksum adds only a few bytes per packet, and the check costs a handful of arithmetic operations. It is the cheapest form of error detection that still catches most accidents in transit.
The math
Let a message be a sequence of words , each an unsigned integer of bits. The simplest checksum is the sum modulo :
Addition here is the modular arithmetic from Modular Arithmetic: the sum wraps around once it reaches , just as a clock wraps after 12. The sender transmits after the data. The receiver recomputes over the data it received and compares.
This catches any single changed word, because the change is nonzero and smaller than in size, so the sum moves by a nonzero amount modulo . It does not catch a reordering of words, since addition does not care about order.
Internet checksum
The checksum used by IP, TCP and UDP is the one's-complement sum of 16-bit words, defined in RFC 1071. One's-complement addition is ordinary addition with a rule: any carry out of the top bit is added back into the lowest bit. This is called the end-around carry. The sum is then the same as addition modulo , with the value and both standing for zero.
The sender computes the sum of all words with the checksum field set to zero, then transmits the bitwise complement . The receiver sums all the words, including the checksum. If nothing changed, the result is all ones, .
CRC
A cyclic redundancy check treats the message as a polynomial over the field with two elements, where coefficients are bits and addition is exclusive or, written . Let the message be and fix a generator polynomial of degree with a nonzero constant term. The sender computes the remainder
and transmits . Since is a multiple of , the receiver checks whether the received polynomial divided by leaves remainder zero.
A burst of errors confined to consecutive bits is a nonzero polynomial of degree less than . Such a polynomial can never be divisible by , which has degree . So every burst of length or less is detected. This is the property that makes CRCs the standard for link-layer frames.
Worked examples
Example 1: a byte sum that catches one error and misses another
Send the two bytes of the ASCII text "Hi". The values are and . Use the modular sum with , so the checksum is the sum modulo 256.
- Sender: . Since , the checksum is . The packet carries "Hi" and 177.
- Receiver gets and , because one bit of the first byte flipped. Recomputed sum: . The stored checksum is 177, so the mismatch is caught.
- Now a different error: the two bytes arrive in swapped order, then . Recomputed sum: , which matches. The error is not detected.
Interpretation: the sum catches an isolated change to a single value, because the total moves. It is blind to order, so a reordering passes silently. A protocol that can reorder packets needs a checksum that depends on position, or it needs sequence numbers.
Example 2: the internet checksum with one's-complement arithmetic
Take two 16-bit words, and , written in hexadecimal.
- Decimal values: and .
- Sum: , which is . No carry out of the top bit, so there is no end-around carry.
- Checksum: the bitwise complement of is . The sender transmits along with the two words.
- Receiver check: . The result is all ones, so the packet is accepted.
- Carry case: suppose the words are and . The plain sum is . The carry out of bit 15 is added back to the lowest bit, giving , and the checksum is .
Interpretation: the receiver never has to know which words were summed. It only checks whether the total is all ones. A single flipped bit always changes the sum, so it is always caught, which is why this simple scheme has survived in IP for decades.
Tradeoffs and failure modes
Checksums are a detection tool, not a correction tool and not a security tool. Choose by what you need.
| Need | Problem with a simple checksum | Better choice |
|---|---|---|
| Detect bursts of errors on a noisy link | Sums can miss a burst that happens to add to the same value | A CRC, which is guaranteed to catch every burst up to its degree |
| Protect against deliberate tampering | Anyone who knows the algorithm can recompute a valid checksum for altered data | A keyed message authentication code or a cryptographic hash |
| Repair damaged data without resending | A checksum only tells you that something is wrong | Forward error correction such as Reed–Solomon or LDPC codes |
| Detect reordering or duplicated packets | Position-blind sums accept reordered data | Sequence numbers, or a position-weighted checksum such as Fletcher's |
| Very high throughput in software | A bit-by-bit CRC is slow | A table-driven CRC, or CRC instructions in hardware |
A common mistake is to assume that a checksum that passes means the data is correct. A checksum only shows that the data matches what the sender computed. If a fault corrupts the data before the sender computes the checksum, the check will pass. Another mistake is to use a checksum that is too short for the data volume. With a -bit checksum, about one damaged packet in 65 thousand will pass by chance under random damage, which can matter on large networks.
History
The checksum idea is old. Early computers used parity bits and sums of words to detect errors in memory and on tape. The Internet checksum was designed for the early TCP/IP protocols, and RFC 1071, published in 1988, gave the one's-complement algorithm and efficient implementation techniques that are still used. Its design favours speed, which is why it is weak against some errors that a CRC would catch.
Cyclic codes, the mathematical basis of the CRC, were described by W. Wesley Peterson in the early 1960s. The CRC-32 polynomial became common for data storage and Ethernet framing because it catches all short bursts and runs quickly in hardware. Fletcher's checksum, developed in the early 1980s, and Adler-32, from the zlib compression library in the 1990s, gave position-sensitive sums that are cheaper than a CRC in software. Exact dates vary between sources, so treat the decades above as approximate.
The reason checksums spread is that they are cheap enough to run on every packet and every block. A small amount of extra data buys a reliable signal that something went wrong, which lets higher layers retransmit or discard the damaged part.
Check yourself
QuestionRecall: what is the internet checksum's end-around carry, and why does it matter?Show answer
When the one's-complement sum overflows the top bit, the carry is added back into the lowest bit instead of being discarded. This keeps the sum equal to addition modulo , which is what makes the receiver's test work: summing the data together with its checksum always gives all ones when nothing has changed.
QuestionComputation: using the modular sum with from the first example, what checksum do you send for the bytes and , and what does the receiver compute if the second byte arrives as ?Show answer
The sender computes , so the checksum is 44. The receiver gets , which mod 256 is 45. Since 45 does not equal 44, the error is detected.
QuestionWhy: why is a CRC guaranteed to catch every burst error of length or less, while a plain sum is not?Show answer
A burst confined to consecutive bits is a nonzero polynomial of degree below . The generator has degree , so it cannot divide such a polynomial, and the receiver's remainder is nonzero. A plain sum only sees the total of the bits, so two different bursts can have the same total and cancel each other out.
QuestionApplied: a storage system must check blocks for accidental corruption from disk faults, and it must also prove to a remote client that a block was not altered by an attacker. Which tool fits each need, and why is one not enough for both?Show answer
A CRC is a good fit for accidental corruption, since it catches bursts from disk faults cheaply. Proof against an attacker needs a keyed authentication code or a cryptographic hash, because an attacker who alters the block can recompute a CRC for the new contents. A CRC gives no guarantee against a deliberate change, so the two needs call for different tools.