Posts Tagged ‘eprom source code decryption’
Instruction-Fetch Decryption: Why Real Code Encryption Is Rare
A request to decrypt source code carries an assumption that is worth testing before any work is quoted: that the code is encrypted. On a microcontroller this is usually false, and the reason is architectural rather than commercial.
A microcontroller executes instructions directly from flash. There is no loader, no decompression step, no operating system to decrypt an image before running it. The fetch unit reads an address and receives an instruction word, and it does that millions of times per second. If the flash contents were encrypted, the decryption would have to happen inside the fetch path, on every access, at full speed.
That is a hardware feature. It exists, it is expensive, and most parts do not have it.
What it would take
Consider what fetching an encrypted instruction requires. The ciphertext is at an address. The plaintext depends on the ciphertext, the key, and the position — because otherwise identical instruction words would produce identical ciphertext, which leaks structure.
So the decryption needs a positional input, which means an address-dependent tweak, which means a block cipher mode with a tweak such as XTS, or a stream cipher seeded per address. Either one costs silicon area and power, and both add latency to the fetch path, which limits the maximum clock.
| Requirement | Consequence |
|---|---|
| Decrypt in the fetch path | Dedicated hardware between flash and core |
| Address-dependent key stream | Tweak or counter per fetch |
| No latency penalty | Pipelined decryption, more area |
| Key held on die | OTP or a factory-provisioned key |
That is a substantial feature, and the parts that have it are the ones sold specifically as secure: secure elements, some automotive parts, and a few application processors with an encrypted external memory interface.
The four things people call encryption
In practice, a customer who reports that their code is encrypted is describing one of four quite different arrangements.
Read protection is the first and by far the most common. The array cannot be read, so from the customer’s point of view the code is inaccessible. There is no cipher involved and nothing to decrypt; the code is in plaintext behind a lock.
Application-level encryption is the second. The developer encrypted a data region — a key table, a configuration blob, a licence file — using a key that is embedded in the code. The code itself is plaintext, and the key is findable.
| Arrangement | What is encrypted | Where the key is | Is there anything to decrypt |
|---|---|---|---|
| Read protection | Nothing | None | No |
| Application-level | A data region | In the code | Yes, and the key is in the image |
| External bus encryption | Everything on the bus | On the SoC | Yes, and the key is not readable |
| On-die fetch decryption | The instruction stream | On the die, protected | Yes, and the key is not readable |
Bus encryption is the third. The code sits in an external flash and travels over a bus in encrypted form, and the SoC decrypts it as it arrives. This is common on application processors with external memory and rare on microcontrollers with internal flash.
On-die fetch decryption is the fourth and the only one that is genuinely hard. The array holds ciphertext, the core receives plaintext, and the key never leaves the die.
The test that tells them apart
The distinction is not academic, because it determines whether a read yields anything.
The test is to read the array and look at what came out. Two measurements settle it.
Entropy is the first. Plaintext code has a characteristic histogram: certain byte values dominate because of instruction encoding, and the entropy is well below the maximum. Ciphertext has a flat histogram and entropy close to eight bits per byte. A region that is compressive is not ciphertext.
| Measurement | Plaintext code | Ciphertext |
|---|---|---|
| Byte histogram | Peaked | Flat |
| Entropy per byte | 5 to 7 bits | 7.9 bits or more |
| Compressibility | 40 to 60 percent | Near zero |
| Disassembles to plausible flow | Yes | No |
The fourth row is the decisive one and it is qualitative rather than numeric. Plaintext code disassembles into something with coherent control flow: functions that end in returns, branches that land on instruction boundaries, calls to addresses that contain prologues. Ciphertext disassembles into a mess that contains no coherent flow, no consistent prologues, and branches that land in the middle of instructions.
The test takes an hour and it converts an ambiguous request into a definite one. If the dump disassembles, there is nothing to decrypt and the job is an analysis. If it does not, the code is either encrypted or the dump is wrong — and a wrong dump is more common than encryption.
Why a wrong dump is the usual explanation
Before concluding that code is encrypted, three dump errors have to be excluded, because each produces high-entropy garbage that looks like ciphertext.
A one-byte shift produces a dump that is almost right and disassembles to nonsense. A wrong address width produces a dump of the wrong region. A read taken while the flash was busy produces a dump with corrupted passages.
| Symptom | Cause | Check |
|---|---|---|
| Entropy high everywhere, no structure | Shifted read | Look for a vector table at offset 0 |
| First part readable, rest not | Address width | Compare the dump size to the array size |
| Isolated corrupted passages | Read during a write | Re-read and compare |
| Correct at the start, garbage after a boundary | Two regions, one encrypted | Check for a length field |
The fourth row is the case that is actually encrypted, and it is usually a data region rather than code: a firmware image that contains an encrypted blob, or an application that decrypts its own resources. That is arrangement two, and the key is in the image.
When there is genuinely nothing to decrypt
If the array holds ciphertext and the key is on the die in a protected area, then the read has succeeded and produced nothing. This is the case that ends projects, and it is worth describing precisely so that it is recognised.
The evidence is a dump with flat entropy, no structure, and a device that runs normally. The device running is the proof that decryption is happening: the core is executing code that the array does not contain in plaintext.
| Observation | Conclusion |
|---|---|
| Device runs, dump is high entropy | Fetch-path decryption present |
| Device runs, dump is plaintext | No encryption; read protection only |
| Device does not run, dump is garbage | Read error, not encryption |
| Device runs, part of dump is plaintext | Mixed: code with an encrypted data region |
In the first case, the remaining routes are the key — which is on the die — or a side channel that observes the plaintext after decryption. Neither is a read. The fault-injection route described in glitch attacks as a search problem can sometimes make the device release plaintext through an interface that would normally refuse, and that is the only electrical approach that does not require the key.
What the customer usually needs instead
The request for decryption is almost always a request for something narrower, and identifying the narrower thing changes the job entirely.
If the goal is to understand how an algorithm works, the deliverable is a behavioural specification with the constants extracted, and the code does not need to be decryptable at all — it needs to be observable. That route is the one in extracting coefficient tables from a DSP, and it works on a device whose flash cannot be read.
If the goal is to modify a behaviour, the deliverable is a patch, which requires the code to be in plaintext in memory somewhere. On a device with fetch-path decryption, that somewhere is the instruction cache or the prefetch buffer, and reading it requires either a debug session the device does not offer or a physical probe.
| Goal | Needs plaintext | Route |
|---|---|---|
| Understand an algorithm | No | Observation and modelling |
| Modify a behaviour | Yes, at the point of patch | Plaintext in cache or buffer |
| Reproduce a function | No | Behavioural characterisation |
| Recover a key | Depends | Key location analysis |
The table is the reason the first conversation matters so much. Two customers with the same sentence — “we need the code decrypted” — need two different engagements, and one of them does not need decryption at all.
What the service does
The first deliverable here is a classification rather than a decryption: whether the array holds plaintext, ciphertext, or a mixture, with the evidence for the conclusion. It is produced in a day from the dump and it prevents the most common wasted engagement in this area, which is a decryption project on an image that was never encrypted.
Where the array is plaintext and the customer believed otherwise, the work becomes the analysis in MCU source code reverse engineering. Where a data region is encrypted with a key in the code, the key is found by the search described in cracking MCU firmware. Where the encryption is in the fetch path, the honest answer is that no decryption is possible from the dump, and the alternatives are physical or side-channel.
The distinction between read protection and encryption is worth restating because it appears in almost every first conversation: a part whose code cannot be read is not a part whose code is encrypted, and the difference is the whole difference between a job that can be done and one that cannot. The read protection side of that distinction is set out in three protection layers.