Secure Boot, Life Cycle and Debug Lock

A chip runs whatever program it finds, unless something checks the program first. The check must be one that an attacker cannot change, cannot repeat and cannot go around through the debug port.

You Will Be Able To

  • Explain why the first check lives in ROM, and why it comes before the jump.
  • Show why a plain sum cannot stop a deliberate change, and what a version floor adds.
  • Read a table of life-cycle states, and say what a damaged state must do.
Lesson
S9.6
Time
30 min
Runs on
Your browser
Check
5 questions

A forum post shows your board printing the message of somebody else. Nobody opened the chip. They wrote a new program into its flash, and the chip ran it.

Between reset and the first instruction of that program, the chip had one chance to refuse.

Before You Start

You need The Boot Flow: the Boot ROM at the reset address, the loader, the image, and the handover as the last step.

You need The Debug Module on the Bus: the debug lock, and the debug key that opens it.

You need Security Architecture: a key that is written, used and never read, and the debugger as a route.

Five questions this page answers
Question
1Why must the first check of an image be done by code in ROM?
2Why does a plain sum stored in the image not stop an attacker?
3What does a version floor refuse that a correct tag lets through?
4What does the life-cycle state decide, and what happens when its code is damaged?
5The version floor is 2 in place of 3. What is the last row of the lab?

The Idea: Check First, with Something the Attacker Lacks

The loader in the Boot ROM copies an image into memory and jumps to it. It does that for any image. So whoever can write the flash decides what the chip runs. Secure boot adds one step before the jump: check the image, and refuse it if the check fails.

The check needs a checker that can be trusted. If the checker sat in memory that can be rewritten, the attacker would rewrite the checker first. So it sits in the ROM, which nobody can change after the chip is made. That starting point is the root of trust.

Next, the check itself. The attacker knows how it works, because the method is no secret. So the check must need something that the attacker does not have. And the values it compares against must live outside the image, where the attacker cannot write.

One door is still open: the debug port. A debugger that can halt the processor and write memory needs no image at all. So the chip keeps a life-cycle state. It says where the chip is in its life, from the tester to the customer. That state decides whether the debug lock may open.

New Words

New words on this page
WordMeaning
Secure boot flowA boot flow in which each program is checked before the jump to it. Secure boot for short.
Root of trustThe first checker. Everything later relies on it, so it must be impossible to change. Here it is the code in the Boot ROM.
Header of an imageA few words in front of the program. Here: a mark, the version, the length and the tag.
Image tagA number in the header that depends on the content of the image. The loader works it out again and compares.
Plain sumA tag made by adding up the words. Anybody can work it out.
Signature of an imageA tag that only the holder of a secret key can make. The chip can check it without holding that secret.
One-time memoryMemory in which a bit can be written once and never erased. Engineers also say fuses.
Version floorThe lowest version that the chip still accepts. It is kept in one-time memory and can only go up.
RollbackLoading an old image, correctly made, that has a known fault.
Life-cycle stateA code in one-time memory that says where the chip is in its life.
TEST, DEV, PRODThree life-cycle states: on the tester of the factory, with the developers, and in the product.
RMAReturn merchandise authorization: the state of a part that came back to its maker to be examined.
Fail closedWhen something is damaged or unknown, every door closes.

One Picture: The Chain of Trust

The Chain of Trust in ap_socEach link checks the next one before handing over control.
  • Memory
  • Processor
The Chain of Trust in ap_socEach link checks the next one before handing over control. component Boot ROM fixed in silicon component One-time memory min version, keys component Firmware image in SRAM, checked component Application runs only if checked reference valuescheck tag, versionjump
    • check tag, version → Firmware image
  1. component One-time memorymin version, keys
    • reference values → Boot ROM
  2. component Firmware imagein SRAM, checked
    • jump → Application
  3. component Applicationruns only if checked
  1. Root of trust

    The ROM is the first code to run, and nothing can rewrite it. Every later check rests on it.

  2. Reference values

    One-time memory holds what the ROM compares against: the lowest version allowed and, in a real design, key material.

  3. Check before run

    The ROM checks the image tag and version before it executes a single image instruction.

  4. Hand over

    Only a passing image gets the jump. A failing one never runs.

Read the diagram as text
  • Boot ROM: fixed in silicon
  • One-time memory: min version, keys
  • Firmware image: in SRAM, checked
  • Application: runs only if checked
  • One-time memory → Boot ROM: reference values
  • Boot ROM → Firmware image: check tag, version
  • Firmware image → Application: jump

Architecture drawing. ap_soc has a two-link chain. S10.15 draws a longer chain for large SoCs.

The Smallest Example, Worked by Hand

No simulator yet. An image has a header and two words of program. The second program word is a flag: 1 would open the debug port. The tag is a plain sum of the version, the length and the program words. The version floor of the chip is 3.

Four images and one loader
ImageVersionLengthProgram wordsTag in the headerSum worked outVerdict
Genuine325 and 0103 + 2 + 5 + 0 = 10Jump
Flag changed to 1325 and 11011Refuse: the tag does not match
Flag changed, and the tag too325 and 11111Jump
An old genuine image225 and 099Refuse: version 2 is below the floor

Read the second row. One word was changed, and the sum no longer matches. So far the check works.

Read the third row. The attacker changed the tag as well, by adding 1. The sum matches again, and the changed image runs. A plain sum finds an accident. It does not stop anybody who can do an addition.

Read the fourth row. Nothing in this image was changed. It is a real release, version 2, which had a fault that version 3 repaired. Its tag is perfect. Only the floor refuses it.

The Rules

  1. Rule 1: The first check is done by code that cannot be changed.

    On ap_soc that is the Boot ROM at the reset address.

  2. Rule 2: Check first, jump last.

    An image that fails never runs one instruction. Each later program checks the next one in the same way.

  3. Rule 3: A check must need something that the attacker does not have.

    A plain sum needs nothing. A signature needs a secret that never leaves its maker.

  4. Rule 4: The values to compare against live outside the image.

    In the ROM or in one-time memory. The image is the thing being checked.

  5. Rule 5: A version floor refuses an old image, even with a correct tag.

    The floor is in one-time memory, and it only goes up.

  6. Rule 6: The life-cycle state decides which doors may open, and any unknown code closes them all.

    The debug lock opens only where the table of states allows it.

The four life-cycle states of ap_soc, and the doors they open
StateCodeJTAG portHalt and bus access by the debuggerTest modeKeys usable
TEST3COpenOpenOpenNo
DEV5AOpenOpenClosedYes
PROD96OpenOnly after the debug lock opensClosedOnly while the debug lock is closed
RMAC3OpenOpenOpenNo
Any other codenoneClosedClosedClosedNo

The codes are bytes in hex. These states, codes and doors are choices of ap_soc.

Read the row of PROD. The JTAG port stays open, because a debugger needs a way in to show its debug key. Halt and bus access open only after that. While they are open, the keys cannot be used.

Read the row of RMA. A part that failed at a customer comes back to be examined. Debug and test are open again, as on the tester. The keys are not usable, so the secrets of the customer stay closed.

Now the codes. Any two of the four differ in at least four bits. One flipped bit can never turn PROD into DEV. It gives a code that is not in the table, and rule 6 closes every door.

Sources for These Rules

The Boot ROM at address 0: the memory map of ap_soc. The loader and the handover: the lesson on the boot flow.

Rules 1 to 5 are general practice. Our reference library holds no standard for secure boot, and no source that states them. Rows of the lab show rules 2, 3 and 5.

Rule 6, the four states, their codes and their doors are choices of ap_soc. The names TEST, DEV, PROD and RMA are in common use. No standard for them is in the library.

That real chips check a signature, and hold only what is needed to check one, is general practice and not checked against a source.

Now Run It

The lab at the end of this page checks four images, as the hand example did. No processor runs here. A task of the test code does the steps that the program in the ROM would do. An array stands for SRAM.

Printed by the lab on Icarus. The numbers of tag and computed are in hex.
genuine  version=3 tag=100000de computed=100000de min=3 -> JUMP 80000010
tampered version=3 tag=100000de computed=100000df min=3 -> REFUSE tag
fixed-up version=3 tag=100000df computed=100000df min=3 -> JUMP 80000010
rollback version=2 tag=100000dd computed=100000dd min=3 -> REFUSE version

Rule 2 at work, in the second row. The flag was changed, and the sum that the loader worked out moved from 100000de to 100000df. The verdict refuses it and names the tag. The changed image never ran.

Rule 3, in the third row. The attacker added 1 to the tag. Both numbers read 100000df, and the image gets the jump.

Rules 4 and 5, in the fourth row. The tag matches, and the image is still refused, because version 2 is below min=3. The floor was read from one-time memory, not from the image.

The address of the jump is 80000010. This image has a header of four words, so its first instruction is the fifth word of SRAM. The plain image of the boot flow lesson had no header and began at 80000000.

This lab leaves things out. Its tag is a plain sum, to show why a sum is not enough. It holds no key and no signature. The life-cycle states are not in this lab: you build their gate in the practice problem "Life Cycle and the Debug Port".

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
The first checker in ROMA checker in flash, which can be repaired laterNobody can replace it. A fault in it is in every chip for ever.
A floor in one-time memoryA list of bad versions in the imageThe attacker cannot lower it. Each step up uses bits that never come back.
The JTAG port open in PRODThe port closed for goodThe maker can still open debug with the debug key. The lock and the key must be sound.
Codes four bits apartA code of two bitsA disturbed bit gives an unknown code, not another state. The decoder compares eight bits.

The practice problem "Refuse the Tampered Image" goes one step past the plain sum. Its tag mixes in a key from one-time memory. It is still a teaching model. Its mixing is simple enough that one genuine image and its tag let a changed copy be made.

The Trap: One Bit Away from a Development Part

A chip kept its life-cycle state in two bits: 00 for the tester, 01 for developers, 10 for the product. The decoder was tiny, and the review passed it.

Then someone disturbed the supply at the moment the state was read. One bit flipped, and a product part read 00. Every debug and test door opened, on a chip in the hands of a customer.

The fault is in the code book. With two bits, every damaged value is another valid state. With codes that are four bits apart, a damaged value is no state at all, and the chip fails closed.

The second trap is to believe a tag. A correct signature says that the maker made this image. It does not say that the image is the newest one. Only the floor says that.

At Scale

This page1 check, 1 image
A phone chipa chain of 4 or more stages

This section looks ahead. One image and one ROM become several stages and several keys. The items are general practice, given as illustration.

  1. The chain grows

    The ROM checks a first loader, which checks trusted firmware, which checks the operating system. A later lesson draws that chain.

  2. The root cannot be repaired

    A fault in ROM code ships in every part. So that code is kept small, and every way of refusing is tested.

  3. Floors run out

    Each step of the floor uses bits of one-time memory. The number of steps is planned before the chip is made.

  4. One way only

    A part moves from TEST to DEV to PROD, and perhaps to RMA. Each move is written once. A move back would make a product part a development part.

Try It

Run the four images. Find the row in which a changed image still gets the jump.

Raise the version floor to 4 before the first check. It is a variable named otp_min_version. Which rows still jump?

Change the tampered image so that the attacker also adds one to the version word. Which check refuses it first?

Names Printed by the Lab
NameMeaning
genuine, tampered, fixed-up, rollbackThe four images that the lab checks, one after another.
versionThe version word in the header of the image.
tagThe tag word in the header: the sum that the maker of the image wrote there.
computedThe sum that the loader works out from the image itself.
minThe version floor, read from one-time memory.
JUMP, REFUSEThe verdict. JUMP shows the address of the first instruction. REFUSE names the check that failed.
RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: genuine version=3 tag=100000de computed=100000de min=3 -> JUMP 80000010 tampered version=3 tag=100000de computed=100000df min=3 -> REFUSE tag fixed-up version=3 tag=100000df computed=100000df min=3 -> JUMP 80000010 rollback version=2 tag=100000dd computed=100000dd min=3 -> REFUSE version
Read included file: boot_rom.svh
// The S9.6 boot ROM model. ap_rv32 is built later in the track, so this task runs the steps the
// ROM program will run. The image layout and the checks are ap_soc design choices:
//   word 0 magic "APSB"   word 1 version   word 2 payload length in words   word 3 tag
//   words 4 onward: the payload, which starts at IMAGE + 0x10
// The tag here is a plain 32-bit sum of version, length and payload. It stands in for a signature
// and holds no secret. otp_min_version models the anti-rollback counter in one-time memory.
localparam logic [31:0] IMAGE = 32'h8000_0000, MAGIC = 32'h4150_5342;
logic [31:0] sram [0:15];
logic [31:0] otp_min_version = 3;

task automatic rom_boot_check(input string name);
  logic [31:0] sum;
  string verdict;
  sum = sram[1] + sram[2];
  for (int i = 0; i < sram[2] && i < 12; i++)
    sum = sum + sram[4 + i];
  if (sram[0] != MAGIC)                verdict = "REFUSE magic";
  else if (sum != sram[3])             verdict = "REFUSE tag";
  else if (sram[1] < otp_min_version)  verdict = "REFUSE version";
  else                                 verdict = $sformatf("JUMP %08h", IMAGE + 32'h10);
  $display("%s version=%0d tag=%08h computed=%08h min=%0d -> %s",
           name, sram[1], sram[3], sum, otp_min_version, verdict);
endtask

Check Yourself

Predict the outputIn this run the version floor in one-time memory is 2 in place of 3. What is the last row? Its form is: rollback version=2 tag=100000dd computed=100000dd min=2 -> ....
module tb;
  `include "boot_rom.svh"
  // A genuine version 3 image: header, then four payload words.
  task automatic load_genuine();
    sram[0] = MAGIC;
    sram[1] = 3;              // version
    sram[2] = 4;              // payload words
    sram[4] = 32'h1000_0000;  // the UART0 base the program writes to
    sram[5] = 32'h0000_0068;  // 'h'
    sram[6] = 32'h0000_0000;  // debug flag: 0 keeps the debug port closed
    sram[7] = 32'h0000_006f;  // 'o'
    sram[3] = sram[1] + sram[2] + sram[4] + sram[5] + sram[6] + sram[7];  // the signer's tag
  endtask

  initial begin
    otp_min_version = 2;
    load_genuine();
    rom_boot_check("genuine ");
    sram[6] = 32'h1;          // an attacker flips the debug flag
    rom_boot_check("tampered");
    sram[3] = sram[3] + 1;    // and fixes up the sum
    rom_boot_check("fixed-up");
    load_genuine();
    sram[1] = 2;              // an older image, genuinely signed, with a known bug
    sram[3] = sram[3] - 1;
    rom_boot_check("rollback");
  end
endmodule
Read included file: boot_rom.svh
// The S9.6 boot ROM model. ap_rv32 is built later in the track, so this task runs the steps the
// ROM program will run. The image layout and the checks are ap_soc design choices:
//   word 0 magic "APSB"   word 1 version   word 2 payload length in words   word 3 tag
//   words 4 onward: the payload, which starts at IMAGE + 0x10
// The tag here is a plain 32-bit sum of version, length and payload. It stands in for a signature
// and holds no secret. otp_min_version models the anti-rollback counter in one-time memory.
localparam logic [31:0] IMAGE = 32'h8000_0000, MAGIC = 32'h4150_5342;
logic [31:0] sram [0:15];
logic [31:0] otp_min_version = 3;

task automatic rom_boot_check(input string name);
  logic [31:0] sum;
  string verdict;
  sum = sram[1] + sram[2];
  for (int i = 0; i < sram[2] && i < 12; i++)
    sum = sum + sram[4 + i];
  if (sram[0] != MAGIC)                verdict = "REFUSE magic";
  else if (sum != sram[3])             verdict = "REFUSE tag";
  else if (sram[1] < otp_min_version)  verdict = "REFUSE version";
  else                                 verdict = $sformatf("JUMP %08h", IMAGE + 32'h10);
  $display("%s version=%0d tag=%08h computed=%08h min=%0d -> %s",
           name, sram[1], sram[3], sum, otp_min_version, verdict);
endtask
ConceptWhy must the first check be done by code in ROM?
ConceptWhy does a plain sum in the image not stop an attacker?
ConceptWhich image does only the version floor refuse?
ConceptOne bit of the PROD code flips. What does the chip do?

Common Mistakes

Wrong Trust a plain sum that is stored in the image.

Whoever changes the image can work the sum out again.

Fix Use a check that needs a secret, and keep the reference values outside the image.

Wrong Treat a correct tag as proof that the image is the newest.

An old genuine image has a correct tag too.

Fix Compare its version with a floor in one-time memory.

Wrong Decode the life-cycle state from a few bits.

One flipped bit then turns a product part into a development part.

Fix Compare the whole code, and close every door on any other value.

Remember

  • The first check is done by code that nobody can change, and it comes before the jump.
  • A check must need something that the attacker does not have. A plain sum needs nothing.
  • A version floor in one-time memory refuses old genuine images.
  • The life-cycle state decides which doors may open, and a damaged state closes them all.

Practice

Report a problem with this page