SEC Module · Secure Enclave
main_fsm.sv · 47 states · readable live at offset 0x008

Main control FSM for boot and record operations

The SEC module uses one top-level state machine to coordinate boot, key reconstruction, authenticated encryption, authenticated decryption, flash access, and error handling. The map below groups the 47 encoded states into functional lanes. Select a state to inspect its entry condition, wait condition, actions, and next states, or trace one of the representative flows.

REQ/WAIT state pairs are drawn as one box; error transitions are simplified — the exact set is in each state's detail panel and the table below. Dashed red = paths into the error states.

Functional lanes

boot

Boot

Waits for flash initialization, then reads the ENROLLED flag to choose between initial enrollment and key reconstruction. This path runs automatically after reset and is not started or repeated through APB.

first boot

Enrollment

Generates the key from the PUF response, writes helper data, the key fingerprint, the counter ceiling, and finally the enrollment flag. This path is intended for the first successful boot. Detail: Startup & Enrollment.

every boot

Reconstruction

Restores the nonce counter from its NVM reservation ceiling, reconstructs the key through the fuzzy extractor, and verifies the recovered key against the stored fingerprint. A mismatch enters the fatal-error state.

hub

Ready

The state that accepts new work. Pending requests are consumed in priority order: debug key loading first, then the single outstanding write or read. Sequence errors and premature RX reads are also converted into request errors here.

operation

Write / Encrypt

Validates the page address, erases the sector when required, increments the nonce counter, persists a new counter ceiling when required, writes associated data, streams ASCON ciphertext and tag, and waits for flash completion.

operation

Read / Decrypt

Validates the address, reads and checks the associated data, buffers the ciphertext and tag, and runs ASCON decryption. Plaintext is released to the RX FIFO only after the authentication tag is accepted.

debug

Debug key loading

Loads a firmware-supplied 128-bit key into ASCON (4 words plus GO over APB). This request has priority over pending data requests and marks key_source until reset. Records written with this key are marked in their associated data.

errors

Error classes

Request errors flush the FIFOs, pulse done, and return to ready. Fatal errors, such as FE failure, flash address violation, counter corruption, or provisioning-time ASCON faults, keep the FSM in the fatal-error state until reset.

All 47 states

Exactly as encoded in main_fsm.sv (enum order = the value readable at offset 0x008).

#StateLaneWhat it does / waits for
0M_RESETbootOne-cycle state after reset.
1M_BOOT_WAIT_SPIbootWaits for the QSPI master's flash init (Quad-Enable) to finish.
2M_BOOT_FLAG_REQbootEnqueues the read of the ENROLLED flag (0x000180).
3M_BOOT_FLAG_WAITbootReceives the flag; branches: 0x01… → reconstruct, else → enroll.
4M_BOOT_COUNTER_REQreconEnqueues reads of the 2 counter-ceiling words (0x001000).
5M_BOOT_COUNTER_WAITreconBuffers the 2 words for counter_control.
6M_COUNTER_RESTORE_CMDreconIssues CTR_CMD_RESTORE to counter_control.
7M_COUNTER_RESTORE_WAITreconWaits for decode of the obfuscated ceiling; bad padding → fatal.
8M_FE_ENROLL_STARTenrollPulses fe_start with mode = enrollment.
9M_FE_ENROLL_WAIT_RenrollWaits for the FE to deliver the 128-bit key R (r_out_valid).
10M_KEY_PROVISION_CMDenrollIssues ASCON KEY_VALIDATE with no stored hash → provision mode.
11M_KEY_PROVISION_STREAMenrollStreams the 8 hash words ASCON emits into the QSPI FIFO (→ 0x000160).
12M_KEY_COUNTER_INIT_REQenrollWrites the initial encoded counter ceiling (2 words → 0x001000).
13M_KEY_FLAG_WRITEenrollWrites the ENROLLED flag — deliberately the last word.
14M_KEY_COMMIT_WAITenrollWaits until flash is idle, FE done and ASCON reports the key valid.
15M_FE_RECON_STARTreconPulses fe_start with mode = reconstruction.
16M_FE_RECON_WAIT_RreconWaits for the recovered key R from the FE.
17M_KEY_HASH_READ_REQreconEnqueues reads of the 8 stored hash words.
18M_KEY_HASH_READ_WAITreconBuffers the stored hash for comparison.
19M_KEY_COMPARE_CMDreconIssues ASCON KEY_VALIDATE in compare mode (hash_stored = 1).
20M_KEY_COMPARE_WAITreconHash match + FE done → ready; mismatch → fatal.
21M_READYhubIdle; accepts debug-key > write > read requests, converts protocol errors.
22M_BACKDOOR_CMDdebug keyIssues KEY_VALIDATE with backdoor_i = 1; ASCON pulls 4 key words from the TX FIFO.
23M_BACKDOOR_WAITdebug keyKey accepted → key_source is set until reset, done pulse; failure → request error.
24M_WR_VALIDATEwriteChecks page ≥ 0x002000 and 256-B aligned; decides whether to erase.
25M_WR_ERASE_REQwriteEnqueues the 4 KB sector erase (only on the sector's first page).
26M_WR_ERASE_WAITwriteWaits for erase completion (WIP polling, FIFO drained).
27M_WR_COUNTER_CMDwriteIssues CTR_CMD_INCREMENT — one nonce per encryption.
28M_WR_COUNTER_DECIDEwriteCeiling crossed? → persist new ceiling first; else straight on.
29M_WR_COUNTER_ERASE_REQwriteEnqueues erase of the counter sector.
30M_WR_COUNTER_ERASE_WAITwriteWaits for the counter-sector erase.
31M_WR_COUNTER_STREAMwriteStreams the 2 encoded ceiling words to flash.
32M_WR_RECORD_AD_REQwriteWrites the 2 plaintext AD words {flag, counter, address} into the page.
33M_WR_ENCRYPT_CMDwriteIssues CMD_ENCRYPT (58 message words) to ASCON.
34M_WR_ENCRYPT_STREAMwriteRoutes 58 ct + 4 tag words from ASCON into the QSPI FIFO, word-addressed.
35M_WR_PACKET_WAITwriteWaits until every queued program command physically completed.
36M_WR_DONEwriteoperation_done pulse + optional IRQ; back to ready.
37M_RD_VALIDATEreadSame address rules as write; no erase, no counter.
38M_RD_AD_REQreadEnqueues reads of the 2 AD words.
39M_RD_AD_WAITreadChecks AD address == requested page and key-source flag == current key source; else ERR_AUTH.
40M_RD_PACKET_REQreadEnqueues reads of the 62 ciphertext + tag words.
41M_RD_PACKET_WAITreadBuffers the packet in packet_buf.
42M_RD_DECRYPT_CMDreadIssues CMD_DECRYPT with nonce metadata from the record's AD.
43M_RD_DECRYPT_STREAMreadFeeds ct+tag to ASCON; on auth OK plaintext flows to the RX FIFO, else ERR_AUTH.
44M_RD_DONEreadoperation_done pulse + optional IRQ; back to ready.
45M_REQUEST_ERRORerrorRecoverable: flush FIFOs, done pulse + IRQ, return to ready. Code stays at 0x004.
46M_FATAL_ERRORerrorSticky: flush, IRQ, remain here until reset. Set by FE/QSPI-guard/counter/provision faults.

Observability: the current state value is always readable at offset 0x008; ASCON's own FSM at 0x014, the counter FSM inside 0x01C/0x028. See the register reference.