Security Architecture
A chip that holds a secret must keep it from people who own the chip. Three attempts are made on one key. Each takes a different route, and each is stopped by a different wall.
You Will Be Able To
- List every manager that can reach an asset, not only the processor.
- Say which check stops which route, and why the checks are of different kinds.
- Explain why a key has no read path, and why it is locked before other code runs.
- Lesson
- S9.5
- Time
- 30 min
- Runs on
- Your browser
- Check
- 5 questions
Your chip ships with a device key inside. Someone buys a board, loads a program of their own and clips a probe onto the debug pins.
They own the software and the board. Something on the chip must still say no.
Before You Start
You need Access Control on the Bus. It defines privilege and security state, the attribute bits that travel with a transfer, and the filter in front of a register.
You need DMA: a second manager that makes transfers of its own once software has programmed it.
You need The Debug Module on the Bus: the debugger reaches memory and devices through a bus port of its own.
| Question | |
|---|---|
| 1 | Who, besides the processor, can make a transfer to the address of a key? |
| 2 | Which check stops user code, and which check stops a DMA that user code has programmed? |
| 3 | The debugger passes both checks. Why does it still not get the key? |
| 4 | Why does the firmware lock the key before any other code runs? |
| 5 | The firmware forgets the lock. What is the last line of the lab? |
The Idea: Every Route Must Meet a Wall
Start with what must be kept. On this page it is one key of 128 bits. A block that encrypts data needs it, and nobody else may ever see it. A thing like that is called an asset.
Now ask who could reach it. A processor runs code in one of several privilege modes. The first program after reset runs in the most trusted mode. A program that the owner of the board loads runs in the least trusted one. Both can make a transfer to any address.
The processor is not the only manager. The DMA makes transfers too, and any software can program it. The debug module makes transfers for whoever holds the probe. Each of these is a route to the key. A team that guards only the processor has left two routes open.
So each route must meet a wall. And the walls should be of different kinds, so that one mistake does not open them all. This page builds three. The first asks how trusted the code is. The second asks which world the transfer comes from. The third is the store itself: the key can be written and used, and it cannot be read by anybody.
New Words
| Word | Meaning |
|---|---|
| Asset of a chip | Something on the chip that must not leak or be changed, such as a key. This page says asset. |
| Privilege mode | One of the levels of trust in which a processor runs code. The mode decides the privilege attribute of its transfers. |
| Machine mode | The RISC-V name of the most trusted mode. Its transfers are privileged. |
| User mode | The RISC-V name of the least trusted mode. Its transfers are unprivileged. |
| Boot firmware | The first program that runs after reset. It runs in machine mode. This page says firmware. |
| Key vault | A block that stores a key. It is the asset of this page. |
| Crypto engine | The block that encrypts and decrypts data with the key. |
| Write-only word | A register word that can be written. A read of it returns 0. |
| Key lock | A bit of the vault. Once it is 1, the key words refuse every write until the next reset. |
| Defence in depth | Several walls in a row, of different kinds. Each can stop a route without help from the others. |
| HPROT, HNONSEC | The AHB wires for the attributes. Bit 1 of HPROT at 1 is privileged. HNONSEC at 1 is Non-secure. |
One Picture: Three Routes, Three Walls
- Processor
- Peripheral
- Debug
- Bus
- Memory
- User code
A user-mode load carries PPROT[0] = 0. The first wall refuses it with an error.
- The DMA engine
The DMA read is privileged, so it passes the first wall. It is Non-secure, so the second wall refuses it.
- The debugger
The debug read is privileged and Secure. It reaches the vault, which has no read path for key bits and returns zero.
- The only way out
The key leaves on one private wire to the crypto engine, which uses it but never puts it on the bus.
Read the diagram as text
- User code: ap_rv32 in user mode
- DMA engine: borrowed by user code
- JTAG debugger: probe on the board
- Wall 1: privilege: PPROT[0] must be 1
- Wall 2: world: PPROT[1] must be 0
- Wall 3: key storage: no read path, locked
- Crypto engine: private key wire
- User code → Wall 1: privilege: load KEY0
- DMA engine → Wall 1: privilege: DMA read
- JTAG debugger → Wall 1: privilege: bus read
- Wall 1: privilege → Wall 2: world: privileged
- Wall 2: world → Wall 3: key storage: Secure
- Wall 3: key storage → Crypto engine: engine_key
Architecture drawing of the S9.5 lab. The walls, their order and the attributes of each requester are ap_soc design choices.
The Smallest Example, Worked by Hand
No simulator yet. Four requesters each make one read of the first key word. Each transfer carries two attribute bits. The vault checks privilege first, then the security state. Work out where each read ends.
| Who | Privileged | Security state | Wall 1: privilege | Wall 2: security state | Wall 3: the store | Result |
|---|---|---|---|---|---|---|
| User code | No | Secure | Stopped | Error | ||
| DMA, programmed by user code | Yes | Non-secure | Passes | Stopped | Error | |
| Debugger | Yes | Secure | Passes | Passes | No read path | A good reply, and the data is 0 |
| Firmware | Yes | Secure | Passes | Passes | No read path | A good reply, and the data is 0 |
Read the second row. User code could not read the key, so it asked the DMA to copy it. The DMA is a manager in its own right, and its transfers are privileged. Wall 1 lets it through. Wall 2 stops it, because this chip marks every transfer of the DMA as Non-secure.
Read the last two rows. The debugger and the firmware carry the same two bits, so no attribute check can tell them apart. Both reach the store, and both read 0. Even the firmware, which wrote the key, cannot read it back.
The Rules
Rule 1: List every manager that can reach the asset.
The processor in each of its modes, the DMA, the debug module. Each one is a route.
Rule 2: The attributes travel with every transfer, and a wall checks them in front of the asset.
Privilege says how trusted the code is. The security state says which world the transfer comes from.
Rule 3: A manager that software can program acts for that software.
It must carry no more trust than the least trusted code that can program it.
Rule 4: Every route must meet a wall, and the walls differ in kind.
One lost wire must not open the asset.
Rule 5: A key has no read path.
It is written, and it leaves on private wires to the block that uses it. It never appears in read data.
Rule 6: The key is locked before any less trusted code runs.
After the lock, nobody can put a key that they know in its place.
Sources for These Rules
Protection needs a mode bit that user code cannot write, and a controlled way to change mode: Patterson and Hennessy, Computer Organization and Design, ARM edition, section 5.7.
The attribute bits of APB (bit 0 of PPROT privileged, bit 1 Non-secure): Arm AMBA APB Protocol Specification, IHI 0024 issue E, section 3.5.
The attribute wires of AHB (bit 1 of HPROT privileged; HNONSEC): Arm AMBA AHB Protocol Specification, IHI 0033 issue C, sections 3.9.2 and 3.10.
Machine mode and user mode are RISC-V names. The RISC-V privileged specification is not in our reference library, so the names are used unchecked.
Rules 1 and 3 to 6 are design practice. No source in our library states them. The vault, its words, the order of its checks and the attributes of each requester are choices of ap_soc.
Now Run It
The lab at the end of this page is a key vault with an APB port. Test code plays four requesters and gives each its attributes. First the firmware writes four key words, then the lock, and reads the status. Then come the three attempts.
firmware write 000 pprot=001 -> OKAY prdata=00000000 stop=none
firmware write 004 pprot=001 -> OKAY prdata=00000000 stop=none
firmware write 008 pprot=001 -> OKAY prdata=00000000 stop=none
firmware write 00c pprot=001 -> OKAY prdata=00000000 stop=none
firmware write 014 pprot=001 -> OKAY prdata=00000000 stop=none
firmware read 010 pprot=001 -> OKAY prdata=00000003 stop=none
user read 000 pprot=000 -> ERROR prdata=00000000 stop=privilege
dma read 000 pprot=011 -> ERROR prdata=00000000 stop=world
debugger read 000 pprot=001 -> OKAY prdata=00000000 stop=key storage
debugger write 000 pprot=001 -> ERROR prdata=00000000 stop=key storage
engine_key[31:0]=6b3f9a21Rule 2 at work, in the row of the user. Its pprot is 000, so bit 0 says unprivileged. The reply is ERROR, and the wall that stopped it is privilege.
Rule 3, in the row of the DMA. Its pprot is 011: privileged, and Non-secure. It passes the first wall and is stopped at the second.
Rule 5, in the first row of the debugger. The reply is OKAY and the data is 00000000. The read was allowed, and no wire leads from the key to the read data.
Rule 6, in the second row of the debugger. It tries to write a key of its own and gets ERROR, because the firmware set the lock. The status word read 00000003 earlier: loaded, and locked. The last line shows that the crypto engine still holds 6b3f9a21.
Now rule 4. Mark the transfers of the DMA as Secure, as a careless design might. Its row becomes "dma read 000 pprot=001 -> OKAY prdata=00000000 stop=key storage". The second wall is gone, and the third one held.
This lab leaves things out. The vault is a lab block. ap_soc has no key vault in its memory map, so the rows show offsets and no chip address. No DMA and no debug module are in the lab. A debugger that is privileged and Secure is the worst case. A real chip can close the debug module itself, as the next lesson shows.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| A store with no read path | A key register that the attribute checks protect | A requester that passes every check still gets nothing. Software can never check the key by reading it. |
| A lock that only reset opens | A password that opens the key words again | Nothing that runs later can replace the key. A wrong key stays until the next reset. |
| The DMA marked Non-secure | The DMA with the attributes of whoever programmed it | A fixed mark needs no extra logic. The DMA can then never serve Secure code. |
A wall inside the vault protects only the vault. A chip has more to guard: the watchdog, the system control block, the control page of the DMA. So one firewall in front of many pages holds a rule for each. You build it in "Wall off the Key".
The Trap: A Bridge That Forgot One Wire
A chip passed its security review. Then a new bridge was put between the fast bus and the device bus, to save area. On the next chip, a DMA copy of the key page ended with a good reply.
The bridge had no input for the security state. It drove bit 1 of PPROT to 0 for every transfer, so every access looked Secure. The second wall was still there, and it now passed everybody. Our teaching bridge in the Protocols track behaves this way too.
A second cause of the same fault is a name. Bit 1 of HPROT means privileged. Bit 1 of PPROT means Non-secure. A bridge that copies bit 1 to bit 1 turns one meaning into the other.
In both stories the third wall decided whether the key leaked. That is what defence in depth is for.
At Scale
This section looks ahead. Three requesters and one key become many of both. The items are general practice, given as illustration.
Attributes are lost in plumbing
Every bridge, every width converter and every clock crossing must carry both attributes. Check each one, with wait states in the test.
Rules from one table
Firewall rules are generated from one table of regions, as the address decoder is. Rules written by hand drift apart.
Who may change the rules?
The rules of a firewall are registers too. They need a wall and a lock of their own.
Attacks that make no transfer
Some attacks measure power or time while the key is used. Others disturb the clock or the supply to skip a check. No wall on the bus sees them.
Try It
Run the lab. Find the first row in which a request passes both attribute checks, and read what it gets back.
Set DMA to 3'b001. That is a DMA whose transfers are marked Secure. Which wall stops its read now?
Delete the write to LOCK by the firmware and run again. Compare the last two rows with the first run.
Names Printed by the Lab
| Name | Meaning |
|---|---|
| firmware, user, dma, debugger | Who made the transfer. Test code plays all four. |
| 000 to 00c | The offsets of the four key words inside the vault. |
| 010, 014 | The offsets of the status word and of the lock. |
| pprot | The attributes of the transfer, as bit 2, bit 1, bit 0. Bit 0 at 1 is privileged. Bit 1 at 1 is Non-secure. |
| OKAY, ERROR | The reply of the vault. |
| prdata | The data that a read returned. |
| stop | The wall that stopped the transfer: privilege, world (the security state) or key storage. A probe of the lab. |
| engine_key | The low word of the key, as the crypto engine sees it on its private wires. |
firmware write 000 pprot=001 -> OKAY prdata=00000000 stop=none firmware write 004 pprot=001 -> OKAY prdata=00000000 stop=none firmware write 008 pprot=001 -> OKAY prdata=00000000 stop=none firmware write 00c pprot=001 -> OKAY prdata=00000000 stop=none firmware write 014 pprot=001 -> OKAY prdata=00000000 stop=none firmware read 010 pprot=001 -> OKAY prdata=00000003 stop=none user read 000 pprot=000 -> ERROR prdata=00000000 stop=privilege dma read 000 pprot=011 -> ERROR prdata=00000000 stop=world debugger read 000 pprot=001 -> OKAY prdata=00000000 stop=key storage debugger write 000 pprot=001 -> ERROR prdata=00000000 stop=key storage engine_key[31:0]=6b3f9a21Read included file: ap_key_vault.sv
// ap_key_vault: the S9.5 lab key store, an APB completer behind the ap_soc bridge.
// The register layout, the order of the checks and the responses are ap_soc design choices.
// PPROT meanings follow Arm IHI 0024E section 3.5: PPROT[0] = 1 is privileged, PPROT[1] = 1 is
// Non-secure, PPROT[2] = 1 is an instruction access.
//
// Offsets in the 4 KiB page:
// 0x000-0x00c KEY0-KEY3 write only, and only until LOCK is set
// 0x010 STATUS {30'b0, locked, loaded}
// 0x014 LOCK write 1 to lock the key until reset
// The key never travels on PRDATA. It leaves only on engine_key, a private wire to the
// crypto engine. stopped_by is a lab probe that names the wall that stopped an access.
module ap_key_vault (
input logic pclk, presetn,
input logic psel, penable, pwrite,
input logic [11:0] paddr,
input logic [31:0] pwdata,
input logic [2:0] pprot,
output logic pready, pslverr,
output logic [31:0] prdata,
output logic [127:0] engine_key,
output logic [1:0] stopped_by // 0 passed, 1 privilege, 2 world, 3 key storage
);
localparam logic [11:0] STATUS = 12'h010, LOCK = 12'h014;
logic [31:0] key [0:3];
logic loaded, locked;
wire key_word = paddr[11:4] == 8'h00;
wire privileged = pprot[0];
wire secure = !pprot[1];
wire access = psel && penable;
always_comb begin
if (!privileged) stopped_by = 2'd1;
else if (!secure) stopped_by = 2'd2;
else if (key_word && (!pwrite || locked)) stopped_by = 2'd3;
else stopped_by = 2'd0;
end
// Zero wait states. A key read that passed both walls still gets zero: there is no read path.
// A refused write, and an access refused by a wall, ends with PSLVERR.
assign pready = 1'b1;
assign pslverr = access && (stopped_by == 2'd1 || stopped_by == 2'd2 || (stopped_by == 2'd3 && pwrite));
assign prdata = (access && stopped_by == 2'd0 && !pwrite && paddr == STATUS) ? {30'b0, locked, loaded} : 32'b0;
assign engine_key = {key[3], key[2], key[1], key[0]};
always_ff @(posedge pclk or negedge presetn) begin
if (!presetn) begin
key[0] <= 0; key[1] <= 0; key[2] <= 0; key[3] <= 0;
loaded <= 0;
locked <= 0;
end else if (access && pwrite && stopped_by == 2'd0) begin
if (key_word) begin
key[paddr[3:2]] <= pwdata;
loaded <= 1;
end else if (paddr == LOCK && pwdata[0]) begin
locked <= 1;
end
end
end
endmodule
Check Yourself
Common Mistakes
Wrong Guard only the processor.
The DMA and the debug module reach the same addresses with attributes of their own.
Fix List every manager, and name the wall that stops each one.
Wrong Protect a key register with attribute checks alone.
One lost wire in a bridge makes every transfer look trusted.
Fix Give the key no read path at all.
Wrong Load the key and leave it unlocked.
Later code can write a key that it knows.
Fix Set the lock before any less trusted code runs.
Remember
- Every manager that can reach an asset is a route to it.
- Each route must meet a wall, and the walls differ in kind.
- A manager that software can program carries no more trust than that software.
- A key is written, used and locked. It is never read.