Definition
A monotonic counter is a stored number that can only increase: every operation on it either leaves it unchanged or raises it, and no event that the counter is claimed to withstand can make it smaller. The list of such events, for example power loss, software faults or an attacker who reprograms storage, is part of every such claim. Monotonic counters hold version floors for anti-rollback §8.26 and provide freshness, the assurance that a message or a stored state is not an old one presented again.
The idea
A receiver refuses replayed messages by remembering the highest message number it has accepted. The number must survive a restart, so the receiver keeps it in flash memory §6.6, and its code only ever raises it. While that code is the only thing that writes the number, the number never falls. But flash can be written by other means: a debug interface that reprograms the chip, a full chip erase, or someone who saved a copy of the entire flash and later writes it back. After such a restore the receiver holds an old number and accepts again every message it accepted since the copy was made. It cannot notice, because the restored bytes are bytes it once wrote itself.
A car’s odometer is a good picture. It should only count up, and the fraud of winding it back is commonly called odometer rollback. An odometer resists only people who cannot get at its mechanism or its electronics, and a counter in ordinary flash resists only people and programs that cannot write the flash. The picture stops at the hardware counters described below: a fuse that has been blown has no operation that restores it, so no amount of access lets anyone wind it back. Monotonicity is therefore a claim about who and what can write the counter, not a property of the increment instruction.
How it works
A claim that a counter is monotonic must state three things.
Coverage: the events the counter withstands without decreasing. Typical candidates are power loss at any instant, a reset, a fault in other software, reprogramming through a debug interface, restoring a saved copy of storage and replacing a chip.
Increment rule: who may increase the counter and when, and how an interrupted increment is completed or undone.
Exhaustion rule: what happens when the counter reaches the largest value it can hold.
Four kinds of implementation are common.
One-time programmable (OTP) fuses. Each fuse bit can be changed once, from its unprogrammed value to its programmed value, and never back. A counter stored in fuses is the number of programmed bits: with 64 fuses it counts from 0 to 64, and each increment programs one more bit. No reprogramming lowers it. Fuses are few, so such a counter is raised only on rare events, such as a release that repairs a vulnerability.
A separate security chip. A TPM (Trusted Platform Module), a standardized security chip found in many computers, provides counters that can only be incremented, and a secure element §5.38, a tamper-resistant chip that keeps keys and small amounts of data, can provide the same. Reprogramming the main processor’s flash does not touch these counters. The processor must talk to the chip over an authenticated channel, or an attacker on the wires between them can feed it old answers.
A replay-protected storage area. The eMMC (embedded MultiMediaCard) storage chips used in phones and other devices contain a region called the RPMB (Replay Protected Memory Block). Every write to it carries a message authentication code §5.7 (MAC), computed with a key that is programmed into the storage chip once and shared with the processor, over the data and the chip’s current write counter. The chip raises its write counter after each accepted write, so a recorded write request is not accepted a second time. A read request carries a nonce §5.18, a fresh value chosen by the processor, which the chip includes under the MAC of its reply, so an old reply cannot be passed off as new.
A software counter in ordinary flash. Two copies with sequence numbers and checksums §4.32, or records appended to a log, make the counter survive power loss and resets. Nothing makes it survive physical reprogramming or a restored copy of the flash.
In most flash memory, erased bits read as 1, programming can change a bit from 1 to 0 without an erase, and returning a bit to 1 requires erasing a whole sector. Programming one more bit to 0 for each increment therefore counts up without erasing, where the chip allows a word to be programmed more than once between erases; many microcontrollers §6.1 limit or forbid that, so the datasheet decides. The trick saves erase cycles, but it does not make the counter monotonic: software and debug tools can still erase the sector.
An anchor for an ordinary counter. State that changes often, including a frequently raised counter, can live in ordinary flash, checked against a hardware counter, the anchor, that changes rarely. To raise the anchor, the device first writes the anchor’s next value into its stored state, protected by a MAC computed with a key that only the device holds so that nobody else can edit it, and then raises the anchor. At start-up it compares the two. A stored copy equal to the anchor is current. A copy one above the anchor means that a raise was interrupted, and the device completes it. A copy below the anchor means that the stored state was rolled back, and the device refuses it. A restore to any state that records the anchor’s current value goes unnoticed; that is every state written since the last raise began, including the state that announced the raise. So the anchor must be raised at every change that must never be undone. If the anchor sits in the same storage as the state, an attacker restores both together and the check gains nothing.
Reserving ahead. To save writes, a device can store a value some distance ahead, say the current value plus 1000, and hand out the values below it from memory without writing. Before it would reach the stored value, it stores a new one another 1000 further ahead. After a restart it continues from the stored value, which nobody has received yet. Some values are skipped, but none is handed out twice, because the stored value is always above every value already handed out.
At the top of the range. A counter of bits holds the values 0 to . At the top, an increment can do one of three things.
Wrap: return to 0, as unsigned modular arithmetic §4.16 does. The counter then falls, so it is no longer monotonic.
Saturate: stay at the largest value. The counter remains monotonic, but further increments change nothing, so a freshness check §1.26 can no longer tell a new message from an old one.
Refuse: report that the counter is exhausted and stop accepting what depends on it. The counter remains monotonic and the failure is visible.
A monotonic counter used for security must saturate or refuse, never wrap, and whatever relies on a saturated counter must refuse new work.
Worked example
Wrapping breaks monotonicity. A 2-bit counter has four bit patterns, 00, 01, 10 and 11, read as 0, 1, 2 and 3. Incrementing 3 in 2-bit modular arithmetic gives , and , so the result is 00. The counter went from 3 to 0, and because it decreased: wrapping is valid arithmetic but not monotonic. A saturating version stays at 3. A refusing version reports that the counter is exhausted.
A restore lowers a flash counter. A receiver’s flash holds the counter value 9. An attacker saved a copy of the flash earlier, when the value was 7, and now writes it back. The receiver reads 7 and again accepts messages numbered 8 and 9, which it had already accepted once. Every step the receiver’s own code took was an increment; the decrease came from outside that code. Now give the receiver an anchor, a fuse counter, and let it record the anchor’s value in its stored state. When the copy was made, the anchor stood at 2 and the stored state recorded 2. Since then the receiver has raised the anchor to 3, writing 3 into its stored state first. At start-up after the restore, the receiver finds the recorded value 2 below the anchor’s 3, concludes that its storage was rolled back and refuses to continue. Had the copy been made after the anchor reached 3, the restore would have gone unnoticed; had the anchor been kept in the same flash, the attacker’s copy would have set it back as well.
Fuses run out. A fuse counter has 32 fuses and is raised once for each release that repairs a vulnerability. At 4 such releases a year it lasts years. Raised once per message instead, it would be exhausted after 32 messages. Fuse counters therefore hold rarely changing values such as a version floor, not message numbers.
Flash wear §8.17. Assume a flash sector of 4096 bytes that tolerates 10,000 erase cycles. A counter rewritten in place, with one erase per increment, wears the sector out after 10,000 increments, which at one increment per second is hours. Appending each new value as a 4-byte record instead fits records per erase, so the sector lasts increments, which is days at the same rate. Reserving 1000 values ahead with each record makes every record stand for 1000 increments, which stretches this 1000 times further.
Stated precisely
Let be the value read from the counter at time , and let be the set of events the counter is claimed to withstand. The counter is monotonic with respect to if, for any two reads at times between which only increments and events from occurred, It is strictly monotonic if, in addition, every completed increment produces a value larger than every value read before it, so that no value is ever handed out twice.
A counter of bits can stay monotonic only if an increment at leaves the value unchanged or fails. Strict monotonicity therefore ends at : only distinct values exist, and after the last one the counter must refuse.
The claim rests on three assumptions, each required for every event in .
Atomic increment: a power loss during an increment leaves either the old value or the new one, never a smaller value and never an unreadable value that is then read as smaller.
Faithful reads: a read returns the value of the last completed increment.
No backward path: no event in can write a smaller value. For a counter in ordinary flash, this assumption fails as soon as contains physical reprogramming or the restoring of a saved copy.
Saturating, wrapping and monotonic counters compared
Counters that are not used for security choose differently at the top of their range, and the choice follows from how the value is read.
A statistics counter, such as a count of dropped packets, is read as a total. If it wrapped, a large count would later read as a small one, which misleads. A saturating statistics counter instead means “at least this many” once it reaches its top, which stays true.
A cycle counter, a hardware register that counts processor clock cycles, is read twice and the two readings are subtracted. Unsigned subtraction modulo gives the right difference even across a wrap, provided the true interval is shorter than cycles. A saturated cycle counter would make every later difference 0, so cycle counters wrap; the Counter wraparound §6.21 entry treats this arithmetic.
A sequence number in a stream of records or packets only has to say which of two nearby values is newer. Serial number arithmetic §19.3.4, defined in RFC 1982, compares two values around the circle of values: the newer one is reached from the older one by moving forward fewer than steps, half the circle. Such numbers may wrap, because only nearby values are ever compared.
Saturating and monotonic are different properties. Saturating describes what happens at the ends of the range; monotonic describes the direction of change. The 2-bit saturating counters used in branch predictors, which guess whether a processor’s conditional jump will be taken, count up and down and stop at 0 and at 3, so they saturate without being monotonic.
Limits and common mistakes
A software increment is not hardware monotonicity. Code that only ever increments makes the counter monotonic against everything that passes through that code, and against nothing else.
Monotonic does not mean secret or authentic. Anyone may know the value. A message that carries a counter needs a message authentication code over the counter, or an attacker can send a forged message with a very high number and make the receiver refuse every genuine message that follows.
Counter values are unique only within one counter. Two devices, or two keys on one device, each with its own counter, produce the same numbers. Authenticated data must name the device or key that a number belongs to.
A monotonic clock §9.35 is a different thing: a time source that never runs backwards while the system runs. Most monotonic clocks start again from 0 at every start-up, so they give no protection across restarts.
Debug and readout protection §16.5, which block access to flash from outside, make physical reprogramming harder. Many chips still allow a full erase that clears both the protection and the flash, after which any value can be written. Whether a counter survives such an erase depends on where the chip keeps it.
A full lifecycle reset may legitimately clear counters, but only together with everything they protect. Android Verified Boot, for example, requires that unlocking a device erases its user data and also sets its stored rollback indexes to zero. A reset that clears a counter but keeps the protected data or keys reopens every replay the counter used to refuse.
Questions answered
Does a flash variable remain monotonic under physical reprogramming or backup restoration?
No. A counter kept as an ordinary variable in flash memory is monotonic, meaning it never decreases, only because the device’s own code never writes a smaller value, and physical reprogramming and backup restoration both bypass that code. Reprogramming through a debug interface, or after a full chip erase, can write any value. Restoring a saved copy of the flash writes back an older value, and the restored bytes are exactly bytes the device once wrote, so no format check or checksum reveals the change.
Making the counter survive these events needs an anchor in storage that cannot count down: one-time programmable fuses, a separate security chip such as a TPM (Trusted Platform Module) or a secure element, or a replay-protected storage area. Either the counter itself lives in that storage, or the state in flash records the anchor’s current value under a message authentication code that only the device can compute, and the device refuses to continue whenever the recorded value is below the anchor. With the second method, a restore to a copy that records the anchor’s current value, one made since the last raise of the anchor began, goes unnoticed. If the anchor sits in the same flash, a restore resets it too and nothing is gained. A design that keeps a plain flash counter must state that physical reprogramming and backup restoration are outside its threat model §1.2.
Related entries: Version high-water mark (§8.27), Signed versus unsigned comparison (§2.12), Replay attack (§1.26), Counter wraparound (§6.21), Unsigned modular arithmetic (§4.16), Flash wear and endurance (§8.17), Monotonic clock (§9.35), Lifecycle reset and replay across complete reinitialization (§19.3.7). Further reading: [154].