Posts Tagged ‘fpga mcu source code crack’

PostHeaderIcon Self-Protecting Code: Three Patterns Worth Recognising

Most protection in an embedded product is passive. A lock bit refuses an operation; a check returns a value; a debug port is disabled. None of it does anything unless someone tries something.

A minority of products add active protection: code that watches the device and reacts. The reaction is usually destructive, and it is triggered by exactly the conditions that an extraction attempt creates. This is why the first rule of working on an unfamiliar image is not to defeat the self-protection but to avoid triggering it.

Pattern one: the watchdog that is also a detector

A watchdog is a timer that resets the device unless the code writes to it periodically. That is a reliability feature. It becomes a protection feature when the feeding is arranged so that only the genuine code path can feed it.

The arrangement is a chain. Function A calls B, B calls C, and C writes the watchdog register. If the code is paused at a breakpoint in A, C never runs and the device resets. If the code is patched to skip B, C never runs and the device resets. The watchdog has become a liveness check on the program’s structure.

Arrangement What it detects Detection difficulty
Single write in the main loop A halt anywhere Easy to keep alive
Write in a deep callee A halt in the caller Moderate
Write dependent on a computed value Skipped or patched code Hard
Write in an interrupt handler A halt with interrupts disabled Moderate

The fourth row is worth noting because it inverts the usual defence. A watchdog fed from an interrupt keeps running when the main loop is halted at a breakpoint, which is exactly what an engineer wants when debugging and exactly what a self-protection scheme does not want. A design that feeds from an interrupt is a reliability design, not a protection design.

Recognising the pattern in a disassembly means finding the write to the watchdog register and following the call chain upward. If the chain is long and conditional, the design is deliberate.

Pattern two: the clock and voltage monitor

A second pattern measures the device’s own operating conditions and reacts to values that suggest tampering.

The measurement is usually indirect: an internal reference is compared against a divided supply, or a ring oscillator’s frequency is compared against a count derived from the main clock. A supply that has been dipped changes the comparison; a clock that has been glitched changes the count.

Measurement Detects Evasion
Supply against an internal reference Voltage glitch Keep the dip inside the window
Ring oscillator counted against the main clock Clock glitch Keep the period change small
Temperature sensor against a limit Cold spray, hot air Stay near ambient
Light sensor under the shield Decapsulation Do not open it

The reaction is what makes this pattern dangerous rather than merely inconvenient. A monitor that resets the device is a nuisance. A monitor that erases a key page, or that writes a poison value into a configuration area, is a device that destroys itself the first time an experiment overshoots.

The disassembly signature is a reading of an analog channel followed by a comparison against a constant and a call into a routine that touches the non-volatile memory. That last part is the tell: a monitor that only resets does not need to write flash.

Pattern three: the periodic integrity check

The third pattern computes a value over a region of memory and compares it against a stored one, on a schedule.

The region is usually the application code, sometimes the configuration, and occasionally a key table. The schedule is a timer, a loop counter, or a call at the top of a large function.

Check Region Reaction
CRC over the application Code Reset, or refuse to run
Compare against a duplicate copy Code Restore from the copy
Hash of a key table Data Clear the table
Signature over the boot image Boot region Refuse to boot

The first row is common and is a nuisance rather than a hazard: a patch that is not accompanied by a recomputed CRC causes a reset, which is recoverable.

The third row is the hazardous one. A hash over a key table, with a clearing reaction, means that reading the table in a way the code notices will destroy it. The notice comes from timing, from a debug access that the code can detect, or from a modification that changes the hash.

The fourth row is the one that changes the whole approach, because a signature over a boot image means the reactive path is in the boot ROM rather than in the application, and the application cannot patch it.

Why recognition precedes action

The three patterns are described here so that they can be identified before anything is attempted, not so that they can be defeated.

The reason is asymmetric risk. Defeating a self-protection has an uncertain payoff; triggering one has a certain cost, usually the loss of the device and sometimes the loss of the only copy of the data. A minute spent looking for the write to the watchdog register, the analog comparison, and the checksum loop is the cheapest insurance in the whole engagement.

Pattern Where to look What to avoid
Watchdog chain Writes to the timer register Long halts in the caller
Environmental monitor Analog read then compare then a flash call Overshooting a glitch
Integrity check A CRC loop with a periodic trigger Modifying without recomputing
Duplicate comparison Two identical regions Patching only one

The fourth row is not one of the three but is common enough to mention. Some designs keep two copies of a region and compare them, restoring one from the other when they differ. A patch applied to one copy is reverted at the next check, and the device appears to reject the patch intermittently.

The cost of a false positive

The failure mode of this analysis is not missing a protection. It is seeing one that is not there.

Almost every firmware image contains a write to the watchdog register and a checksum over some region, because those are ordinary reliability features. An analyst who interprets both as self-protection concludes that the device is booby-trapped, refuses to halt it, refuses to modify it, and the engagement stalls on a device that was never dangerous.

The distinction is behavioural and it is cheap to make. A watchdog that resets the device is a watchdog; one that also touches non-volatile memory is a protection. A checksum that triggers a reset is a reliability check; one that triggers an erase is a protection. The test is whether the reaction is proportional: resetting is recoverable, and anything that writes to memory is not.

Observation Benign interpretation Protection interpretation
Write to the watchdog register Reliability Only if the chain is deep and conditional
Checksum with a reset reaction Reliability Only if the reaction is destructive
Analog comparison at startup Calibration Only if it runs periodically
Read of a protected region Normal use Only if the result gates a destructive call

The fourth row is the one that requires reading the code rather than the pattern. A routine that reads a protected region and branches on the result may be doing anything, and the only way to know is to follow the branch.

What an assessment looks like

The output of this analysis is a risk map: which self-protection mechanisms exist, what triggers each, what the reaction is, and which of the planned operations would trigger it.

That map is produced from the disassembly and, where the code is unavailable, from behaviour: a device that resets when halted, that changes behaviour when the supply is varied, or that clears a region after an unexpected access is telling you which pattern it uses.

Observation Pattern indicated
Resets when halted at a breakpoint Watchdog chain
Resets when the supply is dipped slightly Environmental monitor
Refuses to run after a minor code change Integrity check
Patch reverts after a few seconds Duplicate comparison

Where this fits

This analysis is a prerequisite for the patching work in patching IC code, because a patch that trips a self-protection is a patch that destroys the unit it was meant to modify. It is also a prerequisite for any fault injection campaign, because the environmental monitors described above are precisely the sensors that a glitch has to stay inside, which is the constraint discussed in power glitches.

Where the self-protection is implemented in a boot ROM rather than in the application, the analysis has to move to the boot stage, and the trust chain that makes it work is described in secure boot. Where the reaction destroys a key rather than the code, the consequences are those in where the secret key lives, because a key that has been cleared is not recoverable by any technique on this page.

The service at this layer is the map and the warning. We identify what will happen before it happens, and where the risk is unacceptable we recommend leaving the device alone rather than testing the reaction on the customer’s only unit.