Attack Microchip Encrypted Code: Fuses, KEELOQ, and a Key at the End of Flash
When a customer says the code on a Microchip device is encrypted, they are describing one of two situations, and the two require completely different work. In the first, nothing is encrypted at all: the code protect fuse prevents reading, and “encrypted” is the word the customer reached for because the read returned nonsense. In the second, there is a genuine cryptographic mechanism, usually a KEELOQ-style rolling code or a challenge-response scheme, and the key material exists somewhere on the device.
Telling these apart is a ten-minute exercise once the image is in hand, and it changes the scope from “recover the firmware” to “recover the firmware and then recover a key from inside it”.
Case one: the fuse, and why it is not encryption
The configuration word on a PIC device contains a code protect bit. Setting it does not transform the stored bits; it changes what the programming interface is allowed to return. The array still holds the original instructions, in the clear, addressed exactly as before. The only thing that changed is the answer to a read request.
| Property | Fuse-based protection | True encryption |
|---|---|---|
| Contents of the array | Plaintext instructions | Ciphertext or plaintext with a key elsewhere |
| Effect of a correct read | Returns the original bytes | Returns bytes that need a key to interpret |
| Effect of erasing the fuse | Contents destroyed too | Key still needed elsewhere |
| Physical readout yields | The program directly | The program, and separately the key |
The distinction has a practical consequence that is easy to miss. If the protection is a fuse, a successful physical readout gives the program and nothing else is needed. If the protection is a fuse plus a KEELOQ implementation, a successful physical readout gives the program and the key schedule, because both are in the same array. There is no separate secure element on these parts; the key has to live somewhere the CPU can reach, and the only non-volatile place the CPU can reach is flash or EEPROM.
Case two: KEELOQ and where the key has to be
KEELOQ is a rolling-code algorithm used mostly in remote keyless entry and access control. The transmitter holds a manufacturer key, a device key and a synchronisation counter. The receiver holds the same device key and counter, or derives them. Both ends are microcontrollers, and both ends must store key material.
That storage is the weakness, and it is structural rather than a design mistake. A device key that the firmware cannot read is a device key the firmware cannot use. So the key is in memory at run time, and in non-volatile memory at boot. The only question is where, and that question has a small set of answers.
| Location | How to recognise it | Difficulty |
|---|---|---|
| Flash, near the end of the used region | Small block of high-entropy constants, no code references it via a table | Easy once the image is read |
| EEPROM | Short record with a counter that increments | Requires a separate EEPROM read |
| Flash, inside the code as immediates | Key bytes appear as literal operands in the crypto routine | Requires reading the routine |
| Derived from a device serial | Seed plus a derivation routine | Requires both the seed and the algorithm |
The first row is the most common and the easiest to confirm: a run of 8 or 16 bytes that is referenced only from one routine, has no ASCII interpretation, and sits in a region the linker would otherwise leave empty. The fourth row is the interesting one, because the key is not stored at all — it is computed from a serial number and a manufacturer algorithm, and recovering it means recovering the algorithm and one serial.
Recognising a crypto routine in a small instruction set
On an 8-bit core, a block cipher or a rolling-code function has a distinctive shape: a loop with a fixed trip count, heavy rotate and xor activity, and a state array in data memory that is read and written in a fixed pattern. There is no multiply, so any mixing is shift-and-xor. There is no barrel shifter, so rotations cost cycles and the loop body is dense.
| Signal | What it suggests |
|---|---|
| Fixed-count loop with rotate and xor only | Cipher round, or CRC |
| Table of 16 or 256 constants referenced inside the loop | S-box or nibble substitution |
| Same routine called from a periodic timer interrupt | Rolling-code increment |
| Counter compared against a received value | Synchronisation check |
CRC is the false positive to watch for. A CRC routine has the same rotate-and-xor shape and a table, and it protects data integrity rather than secrecy. The tell is that a CRC is computed over a buffer whose contents are not secret, and the result is compared against a stored value, whereas a cipher’s output is transmitted.
What the deliverable looks like in each case
In the fuse case, the deliverable is the program image plus the configuration word, and the “encryption” question disappears once the image disassembles cleanly. In the KEELOQ case, the deliverable is the image plus an extracted key record plus a description of the counter handling, because a key without its counter semantics is not usable for interoperability work.
Turning that image into a readable reconstruction is a separate pass, described in attacking Microchip source code, and it is usually worth doing before key extraction on parts where the crypto routine is not obvious from the raw bytes.
There is a third case worth mentioning because it is common in practice and frequently misdiagnosed: the customer’s own firmware encrypts a data blob at rest, using a key that is also in the firmware. This is not protection against extraction at all, since the key travels with the ciphertext. It protects against someone reading the EEPROM with a programmer. Restoring embedded information covers this pattern, where the task is to decrypt a data region using a key recovered from the code region.
Why the counter matters as much as the key
A recovered key without its synchronisation state is not usable. Rolling-code schemes compare a received counter against a stored window, and the width of that window is a design parameter that the reconstruction has to establish. Too narrow and the receiver loses sync on a missed press; too wide and the anti-replay property is weakened. Reading the comparison constants out of the routine is how the window is recovered, and those constants are usually immediates in the same function that uses the key.
There is a second reason to recover the counter handling. On many designs the counter is stored in EEPROM and incremented on every transmission, which means the EEPROM has a write budget tied to the product’s usage pattern. A clone that resets the counter to zero will resynchronise on the first press but will also look like a replay to a receiver that has seen higher values, and the failure is intermittent and confusing.
Where the image comes from
None of the above is possible without the array contents, and on a protected PIC that means a physical route; the interaction between the erase primitive and the protect bit is covered in attacking Microchip MCU firmware. Recovering the program is the prerequisite, and key extraction is a second pass over the same image.
Services at this boundary
Work covers determining which of the two cases applies, image recovery, key and counter record identification, crypto routine documentation, and interoperability testing where a rolling-code scheme has to be reproduced. It does not cover breaking the cipher itself, and it does not cover keys that are held only in a separate secure element that the customer has not supplied.
The useful first question is not “can you decrypt it” but “what makes you think it is encrypted”. The answer almost always identifies the case, and the case determines everything else.