Posts Tagged ‘pld flash decrypt’

PostHeaderIcon Bus Encryption: Why a Removed Flash Chip Reads as Noise

An external flash device is the easiest target on any board. Eight pins, a standard command set, and a reader that costs less than the coffee machine. Engineers assume that a device with external memory is therefore unprotected, and on a growing number of products that assumption is wrong in a specific and expensive way.

The protection is not a lock on the flash. It is encryption applied in the SoC’s fetch path, so that the flash holds ciphertext and the core sees plaintext.

Where the decryption happens

The decryptor sits between the external memory interface and the instruction fetch unit. Every read from the external bus passes through it, and the output is what the core executes.

The flash device is unaware of any of this. It receives ordinary read commands at ordinary addresses and returns whatever bytes are stored there. If those bytes were written by a programmer that also encrypted them, the flash contains ciphertext, and it contains it faithfully.

Location Sees Holds
Flash array Read commands and addresses Ciphertext
External bus Ciphertext in transit Nothing
SoC fetch path Ciphertext in, plaintext out The key
Core Plaintext Nothing persistent

The third row is the whole point. The key never leaves the SoC, and on most implementations it is not readable from anywhere inside the SoC either.

What the bus actually carries

Two architectures are common, and they differ in how much a bus capture is worth.

The first is address-based tweak encryption. The key is fixed and the address is mixed in, so the same plaintext at two different addresses produces two different ciphertexts, and moving a block to another address makes it undecryptable. The advantage is that the cipher can be a simple block cipher in counter mode with the address as the counter, which is fast enough for execute-in-place.

Architecture Granularity Address bound Cost
Counter mode with address 16 bytes Yes Low, pipelined
Block cipher, whole image 4 KB or larger No Moderate
Stream cipher, sequential Byte No, order bound Lowest
Full image with header Whole array No Requires load step

The second architecture encrypts whole images with a header that carries a nonce and a key index. Here the SoC copies the image into RAM before running it, which rules out execute-in-place and limits the approach to devices with enough internal RAM.

The distinction matters for the attacker because address-bound ciphers cannot be relocated. A captured ciphertext block replayed at a different address decrypts to garbage, which closes the simplest attack on a naive scheme.

The test for encryption

The test costs one measurement and should be the first thing anyone does with a chip-off dump.

Read the image and compute entropy per erase block. A plaintext image built from compiled code has a characteristic entropy — high but not maximal, with visible structure where constant tables and string pools sit. An encrypted image sits flat at the maximum for the block size, block after block, with no structure at all.

Entropy pattern Interpretation
Variable, dips at string pools Plaintext code
Flat maximum across the whole array Encrypted, or compressed
Flat maximum on some regions only Partial encryption
Flat zero or 0xFF Erased, or never programmed

The second row is ambiguous, and the way to resolve it is a known-plaintext check. Every image built for a Cortex-M or a similar core starts with a vector table whose first word is a stack pointer inside RAM. If the first sixteen bytes of the dump do not look like a vector table and the entropy is maximal, the image is encrypted rather than compressed, because compression leaves the first bytes intact — the loader needs them to know where to decompress to.

Check Plaintext Compressed Encrypted
First word is a RAM address Yes Yes No
Entropy flat throughout No Yes Yes
Recognisable strings present Yes Few None
Decompressor code present No Yes No

Why the key is not in the flash

The obvious countermeasure against all of this would be to store the key with the data, and no competent design does it. The key lives in one of three places.

The first is one-time-programmable memory inside the SoC, programmed at the factory or during provisioning. It is readable by the decryptor and by nothing else, and the read path from the CPU is disabled.

The second is derived from a device-unique secret plus a stored identifier, so that the key differs per unit and a single leak compromises one device.

The third is supplied at boot by an external secure element, which is a separate chip with its own protections and its own attack surface.

Key location Readable from CPU Per-unit Consequence for the attacker
OTP in the SoC No Optional SoC must be attacked, not flash
Derived from device secret No Yes One dump is one device
External secure element No Yes Second chip to attack
In the flash, obfuscated Yes, eventually No Recoverable by analysis

The fourth row exists in real products, usually in low-volume designs where the cost of proper key storage was judged too high. It is worth checking for, because a key stored in the flash with any kind of obfuscation is recoverable by static analysis of the image itself, and the whole encryption scheme then collapses.

What a chip-off read does and does not give

It gives a perfect, bit-exact copy of the ciphertext. That is not nothing. The ciphertext supports differential analysis if two versions of the firmware can be obtained, and it supports replay if the device accepts an image from the flash without further authentication.

It does not give code, strings, or a layout. Every technique in five flash layouts depends on structure that encryption removes, and the analysis has nothing to work with until the encryption is dealt with.

Routes that still work

When the array is encrypted, the useful attack surface moves.

Reading through the SoC is the first option, because the SoC decrypts. Any path that lets code run — a debug port, a DMA transfer, a loader uploaded through a boot mode — reads plaintext, and the encryption is bypassed without being broken. This is the route described in three physical routes into flash, and it is the reason encryption raises cost rather than closing the door.

The second option is to attack the decryptor’s control rather than its key. A glitch that suppresses the decrypt step, or that forces the fetch path into a bypass mode used for debugging, yields plaintext from an encrypted array. The search problem is the one in glitch attacks as a search problem, and the target is a single branch rather than a comparison loop.

The third option is to attack the key where it lives, which means the SoC rather than the flash, and which brings in everything in MCU source code decryption.

Route What it bypasses Device survives
Read through the SoC The encryption entirely Yes
Glitch the decrypt control The decrypt step Usually
Attack the key store The key Usually
Static analysis of the image Nothing, unless the key is in the image Yes

The boot header tell

One practical observation shortens the diagnosis. Encrypted-image designs usually leave a small plaintext region at address zero, because the boot ROM has to find the image, check its length, and locate the key slot before it can decrypt anything.

That header is twenty to sixty bytes of structured data followed by a hard transition into maximum entropy. The transition point is the size of the header, and the header fields are a length, a version, a nonce, and sometimes a checksum of the ciphertext.

Field Width Purpose
Magic 4 bytes Identifies a valid image
Length 4 bytes Ciphertext size
Version 2 to 4 bytes Rollback control
Nonce or IV 8 to 16 bytes Per-image uniqueness

Finding that header confirms the architecture in a few minutes and tells the analysis where the plaintext ends.

What the service does

We classify the protection from a chip-off dump and a boot capture, and report which route remains open.

Deliverable Content
Encryption classification None, address-tweaked, whole-image, or partial
Entropy report Per-block plot with the header boundary marked
Key location assessment Which of the four storage options is in use
Viable route Read-through-SoC, glitch, or key attack, with cost

Where the answer is that the array is encrypted and the SoC is the only path in, we say so before any decapsulation is booked, because a perfect image of an encrypted array is one of the few outcomes in this field that is genuinely worthless.