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.

Five questions this page answers
Question
1Who, besides the processor, can make a transfer to the address of a key?
2Which check stops user code, and which check stops a DMA that user code has programmed?
3The debugger passes both checks. Why does it still not get the key?
4Why does the firmware lock the key before any other code runs?
5The 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

New words on this page
WordMeaning
Asset of a chipSomething on the chip that must not leak or be changed, such as a key. This page says asset.
Privilege modeOne of the levels of trust in which a processor runs code. The mode decides the privilege attribute of its transfers.
Machine modeThe RISC-V name of the most trusted mode. Its transfers are privileged.
User modeThe RISC-V name of the least trusted mode. Its transfers are unprivileged.
Boot firmwareThe first program that runs after reset. It runs in machine mode. This page says firmware.
Key vaultA block that stores a key. It is the asset of this page.
Crypto engineThe block that encrypts and decrypts data with the key.
Write-only wordA register word that can be written. A read of it returns 0.
Key lockA bit of the vault. Once it is 1, the key words refuse every write until the next reset.
Defence in depthSeveral walls in a row, of different kinds. Each can stop a route without help from the others.
HPROT, HNONSECThe 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

Three Routes, Three WallsStep through each route and see which wall stops it.
  • Processor
  • Peripheral
  • Debug
  • Bus
  • Memory
Three Routes, Three WallsStep through each route and see which wall stops it. component User code ap_rv32 in user mode component DMA engine borrowed by usercode component JTAG debugger probe on the board component Wall 1: privilege PPROT[0] must be 1 component Wall 2: world PPROT[1] must be 0 component Wall 3: keystorage no read path, locked component Crypto engine private key wire load KEY0DMA readbus readprivilegedSecureengine_key
  1. component User codeap_rv32 in user mode
    • load KEY0 → Wall 1: privilege
  2. component Wall 1: privilegePPROT[0] must be 1
    • privileged → Wall 2: world
  3. component DMA engineborrowed by user code
    • DMA read → Wall 1: privilege
  4. component Wall 2: worldPPROT[1] must be 0
    • Secure → Wall 3: key storage
  5. component JTAG debuggerprobe on the board
    • bus read → Wall 1: privilege
  6. component Wall 3: key storageno read path, locked
    • engine_key → Crypto engine
  7. component Crypto engineprivate key wire
  1. User code

    A user-mode load carries PPROT[0] = 0. The first wall refuses it with an error.

  2. The DMA engine

    The DMA read is privileged, so it passes the first wall. It is Non-secure, so the second wall refuses it.

  3. The debugger

    The debug read is privileged and Secure. It reaches the vault, which has no read path for key bits and returns zero.

  4. 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.

Four reads of one key word
WhoPrivilegedSecurity stateWall 1: privilegeWall 2: security stateWall 3: the storeResult
User codeNoSecureStoppedError
DMA, programmed by user codeYesNon-securePassesStoppedError
DebuggerYesSecurePassesPassesNo read pathA good reply, and the data is 0
FirmwareYesSecurePassesPassesNo read pathA 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

  1. 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.

  2. 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.

  3. 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.

  4. Rule 4: Every route must meet a wall, and the walls differ in kind.

    One lost wire must not open the asset.

  5. 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.

  6. 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.

Printed by the lab on Icarus. pprot is written as bit 2, bit 1, bit 0. The numbers after "write" and "read" are offsets inside the vault.
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]=6b3f9a21

Rule 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 choiceThe alternativeWhat the choice buys, and costs
A store with no read pathA key register that the attribute checks protectA requester that passes every check still gets nothing. Software can never check the key by reading it.
A lock that only reset opensA password that opens the key words againNothing that runs later can replace the key. A wrong key stays until the next reset.
The DMA marked Non-secureThe DMA with the attributes of whoever programmed itA 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 page3 routes, 1 asset
A phone chipdozens of managers, many assets

This section looks ahead. Three requesters and one key become many of both. The items are general practice, given as illustration.

  1. 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.

  2. Rules from one table

    Firewall rules are generated from one table of regions, as the address decoder is. Rules written by hand drift apart.

  3. Who may change the rules?

    The rules of a firewall are registers too. They need a wall and a lock of their own.

  4. 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
NameMeaning
firmware, user, dma, debuggerWho made the transfer. Test code plays all four.
000 to 00cThe offsets of the four key words inside the vault.
010, 014The offsets of the status word and of the lock.
pprotThe 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, ERRORThe reply of the vault.
prdataThe data that a read returned.
stopThe wall that stopped the transfer: privilege, world (the security state) or key storage. A probe of the lab.
engine_keyThe low word of the key, as the crypto engine sees it on its private wires.
RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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]=6b3f9a21
Read 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

Predict the outputIn this run the firmware forgets to write the lock. What is the last line? Its form is: engine_key[31:0]=........
module tb;
  logic pclk = 0, presetn = 0;
  always #5 pclk = !pclk;
  logic psel = 0, penable = 0, pwrite = 0;
  logic [11:0] paddr = 0;
  logic [31:0] pwdata = 0;
  logic [2:0] pprot = 0;
  wire pready, pslverr;
  wire [31:0] prdata;
  wire [127:0] engine_key;
  wire [1:0] stopped_by;
  ap_key_vault vault(.*);

  // PPROT = {instruction, non-secure, privileged}, as the bridge passes it for each manager.
  localparam logic [2:0] FIRMWARE = 3'b001, USER = 3'b000, DMA = 3'b011, DEBUGGER = 3'b001;
  localparam logic [11:0] KEY0 = 12'h000, STATUS = 12'h010, LOCK = 12'h014;

  function automatic string wall(input logic [1:0] code);
    case (code)
      2'd1: return "privilege";
      2'd2: return "world";
      2'd3: return "key storage";
      default: return "none";
    endcase
  endfunction

  // One APB transfer: setup, then access. The vault never adds wait states.
  task automatic apb(input string who, input bit write, input logic [11:0] offset,
                     input logic [31:0] data, input logic [2:0] prot);
    @(negedge pclk);
    psel = 1; pwrite = write; paddr = offset; pwdata = data; pprot = prot;
    @(negedge pclk);
    penable = 1;
    #4;   // just before the completing rising edge
    $display("%s %s %03h pprot=%03b -> %s prdata=%08h stop=%s", who, write ? "write" : "read ",
             offset, prot, pslverr ? "ERROR" : "OKAY ", prdata, wall(stopped_by));
    @(negedge pclk);
    psel = 0; penable = 0;
  endtask

  initial begin
    repeat (2) @(negedge pclk);
    presetn = 1;
    // Boot firmware: machine mode, Secure. It loads the key, then locks it.
    apb("firmware", 1, 12'h000, 32'h6b3f9a21, FIRMWARE);
    apb("firmware", 1, 12'h004, 32'hd04c7e15, FIRMWARE);
    apb("firmware", 1, 12'h008, 32'h93a5f0c8, FIRMWARE);
    apb("firmware", 1, 12'h00c, 32'h2e81b64d, FIRMWARE);
    apb("firmware", 0, STATUS, 0, FIRMWARE);
    // Three routes to the same key word.
    apb("user    ", 0, KEY0, 0, USER);
    apb("dma     ", 0, KEY0, 0, DMA);
    apb("debugger", 0, KEY0, 0, DEBUGGER);
    apb("debugger", 1, KEY0, 32'h0, DEBUGGER);
    $display("engine_key[31:0]=%08h", engine_key[31:0]);
    $finish;
  end
endmodule
Read 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
ConceptWho, besides the processor, can make a transfer to the address of a key?
ConceptWhich check stops the DMA that user code has programmed?
ConceptThe debugger passes both attribute checks. Why does it not get the key?
ConceptWhy does the firmware set the lock before other code runs?

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.

Practice

Report a problem with this page