Search

Search papers and lessons

Checksum

Level 2 · 9 min read

A checksum is a short number computed from a message and sent with it, so the receiver can recompute it and notice if the data was damaged in transit.

On this page

  • Colour = topic mix
  • Networking
  • Discrete math
  • Sign in to see your progress

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 2b2^{b} for a bb-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 mm words w1,…,wmw_1, \dots, w_m, each an unsigned integer of bb bits. The simplest checksum is the sum modulo 2b2^{b}:

C=(∑i=1mwi) mod 2b.C = \left(\sum_{i=1}^{m} w_i\right) \bmod 2^{b}.

Addition here is the modular arithmetic from Modular Arithmetic: the sum wraps around once it reaches 2b2^{b}, just as a clock wraps after 12. The sender transmits CC after the data. The receiver recomputes CC over the data it received and compares.

This catches any single changed word, because the change dd is nonzero and smaller than 2b2^{b} in size, so the sum moves by a nonzero amount modulo 2b2^{b}. 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 216−12^{16} - 1, with the value 00000000 and FFFFFFFF both standing for zero.

The sender computes the sum SS of all words with the checksum field set to zero, then transmits the bitwise complement S‾\overline{S}. The receiver sums all the words, including the checksum. If nothing changed, the result is all ones, FFFFFFFF.

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 ⊕\oplus. Let the message be M(x)M(x) and fix a generator polynomial G(x)G(x) of degree rr with a nonzero constant term. The sender computes the remainder

R(x)=remainder of xrM(x) divided by G(x),R(x) = \text{remainder of } x^{r} M(x) \text{ divided by } G(x),

and transmits T(x)=xrM(x)+R(x)T(x) = x^{r} M(x) + R(x). Since T(x)T(x) is a multiple of G(x)G(x), the receiver checks whether the received polynomial divided by G(x)G(x) leaves remainder zero.

A burst of errors confined to rr consecutive bits is a nonzero polynomial of degree less than rr. Such a polynomial can never be divisible by G(x)G(x), which has degree rr. So every burst of length rr 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 H=72H = 72 and i=105i = 105. Use the modular sum with b=8b = 8, so the checksum is the sum modulo 256.

Example
Sum modulo 256 on two bytes
  1. Sender: 72+105=17772 + 105 = 177. Since 177<256177 \lt 256, the checksum is C=177C = 177. The packet carries "Hi" and 177.
  2. Receiver gets 7373 and 105105, because one bit of the first byte flipped. Recomputed sum: 73+105=17873 + 105 = 178. The stored checksum is 177, so the mismatch is caught.
  3. Now a different error: the two bytes arrive in swapped order, 105105 then 7272. Recomputed sum: 105+72=177105 + 72 = 177, 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, 0x12340x1234 and 0xABCD0xABCD, written in hexadecimal.

Example
Internet checksum over two words
  1. Decimal values: 0x1234=46600x1234 = 4660 and 0xABCD=439810xABCD = 43981.
  2. Sum: 4660+43981=486414660 + 43981 = 48641, which is 0xBE010xBE01. No carry out of the top bit, so there is no end-around carry.
  3. Checksum: the bitwise complement of 0xBE010xBE01 is 0x41FE0x41FE. The sender transmits 0x41FE0x41FE along with the two words.
  4. Receiver check: 0xBE01+0x41FE=0xFFFF0xBE01 + 0x41FE = 0xFFFF. The result is all ones, so the packet is accepted.
  5. Carry case: suppose the words are 0xFFFF0xFFFF and 0x00010x0001. The plain sum is 0x100000x10000. The carry out of bit 15 is added back to the lowest bit, giving 0x0000+1=0x00010x0000 + 1 = 0x0001, and the checksum is 0x0001‾=0xFFFE\overline{0x0001} = 0xFFFE.

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.

NeedProblem with a simple checksumBetter choice
Detect bursts of errors on a noisy linkSums can miss a burst that happens to add to the same valueA CRC, which is guaranteed to catch every burst up to its degree
Protect against deliberate tamperingAnyone who knows the algorithm can recompute a valid checksum for altered dataA keyed message authentication code or a cryptographic hash
Repair damaged data without resendingA checksum only tells you that something is wrongForward error correction such as Reed–Solomon or LDPC codes
Detect reordering or duplicated packetsPosition-blind sums accept reordered dataSequence numbers, or a position-weighted checksum such as Fletcher's
Very high throughput in softwareA bit-by-bit CRC is slowA 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 1616-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 216−12^{16} - 1, 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 b=8b = 8 from the first example, what checksum do you send for the bytes 200200 and 100100, and what does the receiver compute if the second byte arrives as 101101?Show answer

The sender computes (200+100) mod 256=300−256=44(200 + 100) \bmod 256 = 300 - 256 = 44, so the checksum is 44. The receiver gets 200+101=301200 + 101 = 301, 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 rr or less, while a plain sum is not?Show answer

A burst confined to rr consecutive bits is a nonzero polynomial of degree below rr. The generator has degree rr, 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.

Sources