Skip to content

Commit 91b1f39

Browse files
committed
[ot] hw/opentitan: ot_flash: Dump keymgr secret in read trace
I would have likely caught the previous bugs if I had properly traced the seed like the TODO suggests. Add the ability to dump the entire seed in the trace to make it easier to catch issues involving flash secrets in the future. Signed-off-by: Alex Jones <[email protected]>
1 parent b4472f9 commit 91b1f39

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

hw/opentitan/ot_flash.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,13 +875,21 @@ struct OtFlashState {
875875
*/
876876
OtFifo32 hw_rd_fifo;
877877

878+
char *hexstr;
879+
880+
/* properties */
878881
char *ot_id;
879882
BlockBackend *blk; /* Flash backend */
880883
OtVMapperState *vmapper; /* to disable execution from flash */
881884
bool no_mem_prot; /* Flag to disable mem protection features */
882885
bool fatal_escalate;
883886
};
884887

888+
#define OT_FLASH_HEXSTR_SIZE (FLASH_SEED_BYTES * 2u + 2u)
889+
890+
#define ot_flash_dump_bigint(_s_, _b_, _l_) \
891+
ot_common_lhexdump(_b_, _l_, true, (_s_)->hexstr, OT_FLASH_HEXSTR_SIZE)
892+
885893
/* Flash memory protection rules */
886894

887895
static const OtFlashPropertyCfg OT_FLASH_CFG_ALLOW_READ = {
@@ -1947,8 +1955,11 @@ static void ot_flash_read_keymgr_seed(OtFlashState *s, unsigned page,
19471955
memcpy(seed->secret, seed_words, FLASH_SEED_BYTES);
19481956
seed->valid = !s->op.failed;
19491957

1950-
/* todo: dump the seed in the trace? */
1951-
trace_ot_flash_read_keymgr_seed(s->ot_id, page, !s->op.failed);
1958+
if (trace_event_get_state(TRACE_OT_FLASH_READ_KEYMGR_SEED)) {
1959+
const char *hexstr =
1960+
ot_flash_dump_bigint(s, seed->secret, FLASH_SEED_BYTES);
1961+
trace_ot_flash_read_keymgr_seed(s->ot_id, page, !s->op.failed, hexstr);
1962+
}
19521963

19531964
if (s->op.failed) {
19541965
ot_flash_set_error(s, R_FAULT_STATUS_SEED_ERR_MASK, s->op.address);
@@ -3369,6 +3380,8 @@ static void ot_flash_init(Object *obj)
33693380
s->lc_broadcast.timer =
33703381
timer_new_ns(OT_VIRTUAL_CLOCK, &ot_flash_lc_broadcast, s);
33713382
s->op_delay = timer_new_ns(OT_VIRTUAL_CLOCK, &ot_flash_init_complete, s);
3383+
3384+
s->hexstr = g_new0(char, OT_FLASH_HEXSTR_SIZE);
33723385
}
33733386

33743387
static void ot_flash_class_init(ObjectClass *klass, void *data)

hw/opentitan/trace-events

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ ot_flash_op_execute(const char* id, const char *op, bool hw) "%s: %s (hw=%u)"
214214
ot_flash_op_prog(const char* id, unsigned op_addr, unsigned count, unsigned remaining, bool failed, bool fifo_empty) "%s: op_addr 0x%06x count %u remaining %u failed %u fifo_empty %u"
215215
ot_flash_op_start(const char* id, const char *op, bool hw) "%s: %s (hw=%u)"
216216
ot_flash_prog_word(const char* id, bool info_part, unsigned word_addr, uint32_t word) "%s: info_part %u word_addr %u word %u"
217-
ot_flash_read_keymgr_seed(const char* id, unsigned page, bool success) "%s: %u: %u"
217+
ot_flash_read_keymgr_seed(const char* id, unsigned page, bool success, const char *seed) "%s: page:%u, success:%u, seed:%s"
218218
ot_flash_reset(const char *id, const char *phase) "%s: %s"
219219
ot_flash_reset_fifo(const char* id, const char *name) "%s: %s"
220220
ot_flash_set_error(const char* id, const char *op, bool hw, uint32_t err_code, uint32_t err_addr) "%s: %s (hw=%u): err=0x%08x at addr=0x%06x"

0 commit comments

Comments
 (0)