From 4094bd91ecd8581585bd9b3ebea5c849cab99512 Mon Sep 17 00:00:00 2001 From: Tim Trippel Date: Mon, 11 Mar 2024 17:26:39 -0700 Subject: [PATCH] [silicon_creator] namespace OTBN driver functions This partially addresses #21937 by renaming the OTBN driver functions so they do not collide with similar cryptolib functions. Signed-off-by: Tim Trippel --- sw/device/silicon_creator/lib/drivers/otbn.c | 67 +++---- sw/device/silicon_creator/lib/drivers/otbn.h | 74 ++++---- .../lib/drivers/otbn_unittest.cc | 170 +++++++++--------- .../silicon_creator/sigverify/mod_exp_otbn.c | 30 ++-- 4 files changed, 174 insertions(+), 167 deletions(-) diff --git a/sw/device/silicon_creator/lib/drivers/otbn.c b/sw/device/silicon_creator/lib/drivers/otbn.c index 74991942773e4..861c98a0b4804 100644 --- a/sw/device/silicon_creator/lib/drivers/otbn.c +++ b/sw/device/silicon_creator/lib/drivers/otbn.c @@ -51,18 +51,18 @@ static rom_error_t check_offset_len(uint32_t offset_bytes, size_t num_words, return kErrorOk; } -rom_error_t otbn_busy_wait_for_done(void) { +rom_error_t sc_otbn_busy_wait_for_done(void) { uint32_t status = launder32(UINT32_MAX); rom_error_t res = launder32(kErrorOk ^ status); do { status = abs_mmio_read32(kBase + OTBN_STATUS_REG_OFFSET); - } while (launder32(status) != kOtbnStatusIdle && - launder32(status) != kOtbnStatusLocked); + } while (launder32(status) != kScOtbnStatusIdle && + launder32(status) != kScOtbnStatusLocked); res ^= ~status; if (launder32(res) == kErrorOk) { HARDENED_CHECK_EQ(res, kErrorOk); HARDENED_CHECK_EQ(abs_mmio_read32(kBase + OTBN_STATUS_REG_OFFSET), - kOtbnStatusIdle); + kScOtbnStatusIdle); return res; } return kErrorOtbnUnavailable; @@ -75,8 +75,8 @@ rom_error_t otbn_busy_wait_for_done(void) { * @param src Source buffer. * @param num_words Number of words to copy. */ -static void otbn_write(uint32_t dest_addr, const uint32_t *src, - size_t num_words) { +static void sc_otbn_write(uint32_t dest_addr, const uint32_t *src, + size_t num_words) { // Start from a random index less than `num_words`. size_t i = ((uint64_t)rnd_uint32() * (uint64_t)num_words) >> 32; enum { kStep = 1 }; @@ -95,23 +95,24 @@ static void otbn_write(uint32_t dest_addr, const uint32_t *src, } OT_WARN_UNUSED_RESULT -static rom_error_t otbn_imem_write(size_t num_words, const uint32_t *src, - otbn_addr_t dest) { +static rom_error_t sc_otbn_imem_write(size_t num_words, const uint32_t *src, + sc_otbn_addr_t dest) { HARDENED_RETURN_IF_ERROR( check_offset_len(dest, num_words, OTBN_IMEM_SIZE_BYTES)); - otbn_write(kBase + OTBN_IMEM_REG_OFFSET + dest, src, num_words); + sc_otbn_write(kBase + OTBN_IMEM_REG_OFFSET + dest, src, num_words); return kErrorOk; } -rom_error_t otbn_dmem_write(size_t num_words, const uint32_t *src, - otbn_addr_t dest) { +rom_error_t sc_otbn_dmem_write(size_t num_words, const uint32_t *src, + sc_otbn_addr_t dest) { HARDENED_RETURN_IF_ERROR( check_offset_len(dest, num_words, OTBN_DMEM_SIZE_BYTES)); - otbn_write(kBase + OTBN_DMEM_REG_OFFSET + dest, src, num_words); + sc_otbn_write(kBase + OTBN_DMEM_REG_OFFSET + dest, src, num_words); return kErrorOk; } -rom_error_t otbn_dmem_read(size_t num_words, otbn_addr_t src, uint32_t *dest) { +rom_error_t sc_otbn_dmem_read(size_t num_words, sc_otbn_addr_t src, + uint32_t *dest) { HARDENED_RETURN_IF_ERROR( check_offset_len(src, num_words, OTBN_DMEM_SIZE_BYTES)); size_t i = 0, r = num_words - 1; @@ -134,7 +135,7 @@ rom_error_t otbn_dmem_read(size_t num_words, otbn_addr_t src, uint32_t *dest) { * @return Result of the operation. */ OT_WARN_UNUSED_RESULT -static rom_error_t otbn_cmd_run(otbn_cmd_t cmd, rom_error_t error) { +static rom_error_t sc_otbn_cmd_run(sc_otbn_cmd_t cmd, rom_error_t error) { enum { kIntrStateDone = (1 << OTBN_INTR_COMMON_DONE_BIT), // Use a bit index that doesn't overlap with error bits. @@ -160,25 +161,25 @@ static rom_error_t otbn_cmd_run(otbn_cmd_t cmd, rom_error_t error) { uint32_t err_bits = abs_mmio_read32(kBase + OTBN_ERR_BITS_REG_OFFSET); res ^= err_bits; - // Status should be kOtbnStatusIdle; OTBN can also issue a done interrupt + // Status should be kScOtbnStatusIdle; OTBN can also issue a done interrupt // when transitioning to the "locked" state, so it is important to check // the status here. uint32_t status = abs_mmio_read32(kBase + OTBN_STATUS_REG_OFFSET); if (launder32(res) == kErrorOk && launder32(err_bits) == 0 && - launder32(status) == kOtbnStatusIdle) { + launder32(status) == kScOtbnStatusIdle) { HARDENED_CHECK_EQ(res, kErrorOk); HARDENED_CHECK_EQ(err_bits, 0); HARDENED_CHECK_EQ(abs_mmio_read32(kBase + OTBN_STATUS_REG_OFFSET), - kOtbnStatusIdle); + kScOtbnStatusIdle); return res; } return error; } -rom_error_t otbn_execute(void) { +rom_error_t sc_otbn_execute(void) { // If OTBN is busy, wait for it to be done. - HARDENED_RETURN_IF_ERROR(otbn_busy_wait_for_done()); + HARDENED_RETURN_IF_ERROR(sc_otbn_busy_wait_for_done()); // Set software errors to fatal before running the program. Note: the CTRL // register has only this one setting, so we have no need to read the @@ -186,19 +187,19 @@ rom_error_t otbn_execute(void) { sec_mmio_write32(kBase + OTBN_CTRL_REG_OFFSET, 1 << OTBN_CTRL_SOFTWARE_ERRS_FATAL_BIT); - return otbn_cmd_run(kOtbnCmdExecute, kErrorOtbnExecutionFailed); + return sc_otbn_cmd_run(kScOtbnCmdExecute, kErrorOtbnExecutionFailed); } -uint32_t otbn_instruction_count_get(void) { +uint32_t sc_otbn_instruction_count_get(void) { return abs_mmio_read32(kBase + OTBN_INSN_CNT_REG_OFFSET); } -rom_error_t otbn_imem_sec_wipe(void) { - return otbn_cmd_run(kOtbnCmdSecWipeImem, kErrorOtbnSecWipeImemFailed); +rom_error_t sc_otbn_imem_sec_wipe(void) { + return sc_otbn_cmd_run(kScOtbnCmdSecWipeImem, kErrorOtbnSecWipeImemFailed); } -rom_error_t otbn_dmem_sec_wipe(void) { - return otbn_cmd_run(kOtbnCmdSecWipeDmem, kErrorOtbnSecWipeDmemFailed); +rom_error_t sc_otbn_dmem_sec_wipe(void) { + return sc_otbn_cmd_run(kScOtbnCmdSecWipeDmem, kErrorOtbnSecWipeDmemFailed); } /** @@ -212,7 +213,7 @@ rom_error_t otbn_dmem_sec_wipe(void) { * @return OK if the addresses are valid, otherwise `kErrorOtbnInvalidArgument`. */ OT_WARN_UNUSED_RESULT -static rom_error_t check_app_address_ranges(const otbn_app_t app) { +static rom_error_t check_app_address_ranges(const sc_otbn_app_t app) { if (app.imem_end > app.imem_start && app.dmem_data_end >= app.dmem_data_start) { HARDENED_CHECK_GT(app.imem_end, app.imem_start); @@ -222,27 +223,27 @@ static rom_error_t check_app_address_ranges(const otbn_app_t app) { return kErrorOtbnInvalidArgument; } -rom_error_t otbn_load_app(const otbn_app_t app) { +rom_error_t sc_otbn_load_app(const sc_otbn_app_t app) { HARDENED_RETURN_IF_ERROR(check_app_address_ranges(app)); // If OTBN is busy, wait for it to be done. - HARDENED_RETURN_IF_ERROR(otbn_busy_wait_for_done()); + HARDENED_RETURN_IF_ERROR(sc_otbn_busy_wait_for_done()); // Wipe memories. - HARDENED_RETURN_IF_ERROR(otbn_dmem_sec_wipe()); - HARDENED_RETURN_IF_ERROR(otbn_imem_sec_wipe()); + HARDENED_RETURN_IF_ERROR(sc_otbn_dmem_sec_wipe()); + HARDENED_RETURN_IF_ERROR(sc_otbn_imem_sec_wipe()); const size_t imem_num_words = (size_t)(app.imem_end - app.imem_start); const size_t data_num_words = (size_t)(app.dmem_data_end - app.dmem_data_start); // IMEM always starts at 0. - otbn_addr_t imem_start_addr = 0; + sc_otbn_addr_t imem_start_addr = 0; HARDENED_RETURN_IF_ERROR( - otbn_imem_write(imem_num_words, app.imem_start, imem_start_addr)); + sc_otbn_imem_write(imem_num_words, app.imem_start, imem_start_addr)); if (data_num_words > 0) { - HARDENED_RETURN_IF_ERROR(otbn_dmem_write( + HARDENED_RETURN_IF_ERROR(sc_otbn_dmem_write( data_num_words, app.dmem_data_start, app.dmem_data_start_addr)); } return kErrorOk; diff --git a/sw/device/silicon_creator/lib/drivers/otbn.h b/sw/device/silicon_creator/lib/drivers/otbn.h index fab051a2a1f4c..29f0c67552edd 100644 --- a/sw/device/silicon_creator/lib/drivers/otbn.h +++ b/sw/device/silicon_creator/lib/drivers/otbn.h @@ -20,9 +20,9 @@ extern "C" { */ enum { /* Length of an OTBN wide word in bits */ - kOtbnWideWordNumBits = 256, + kScOtbnWideWordNumBits = 256, /* Length of an OTBN wide word in words */ - kOtbnWideWordNumWords = kOtbnWideWordNumBits / (sizeof(uint32_t) * 8), + kScOtbnWideWordNumWords = kScOtbnWideWordNumBits / (sizeof(uint32_t) * 8), }; /** @@ -32,12 +32,12 @@ enum { * * Example: * ``` - * otbn_execute(); - * SEC_MMIO_WRITE_INCREMENT(kOtbnSecMmioExecute); + * sc_otbn_execute(); + * SEC_MMIO_WRITE_INCREMENT(kScOtbnSecMmioExecute); * ``` */ enum { - kOtbnSecMmioExecute = 1, + kScOtbnSecMmioExecute = 1, }; /** @@ -45,25 +45,25 @@ enum { * * TODO(#16754): replace these with constants from otbn_regs.h */ -typedef enum otbn_cmd { - kOtbnCmdExecute = 0xd8, - kOtbnCmdSecWipeDmem = 0xc3, - kOtbnCmdSecWipeImem = 0x1e, -} otbn_cmd_t; +typedef enum sc_otbn_cmd { + kScOtbnCmdExecute = 0xd8, + kScOtbnCmdSecWipeDmem = 0xc3, + kScOtbnCmdSecWipeImem = 0x1e, +} sc_otbn_cmd_t; /** * OTBN status * * TODO(#16754): replace these with constants from otbn_regs.h */ -typedef enum otbn_status { - kOtbnStatusIdle = 0x00, - kOtbnStatusBusyExecute = 0x01, - kOtbnStatusBusySecWipeDmem = 0x02, - kOtbnStatusBusySecWipeImem = 0x03, - kOtbnStatusBusySecWipeInt = 0x04, - kOtbnStatusLocked = 0xFF, -} otbn_status_t; +typedef enum sc_otbn_status { + kScOtbnStatusIdle = 0x00, + kScOtbnStatusBusyExecute = 0x01, + kScOtbnStatusBusySecWipeDmem = 0x02, + kScOtbnStatusBusySecWipeImem = 0x03, + kScOtbnStatusBusySecWipeInt = 0x04, + kScOtbnStatusLocked = 0xFF, +} sc_otbn_status_t; /** * The address of an OTBN symbol as seen by OTBN @@ -71,7 +71,7 @@ typedef enum otbn_status { * Use `OTBN_DECLARE_SYMBOL_ADDR()` together with `OTBN_ADDR_T_INIT()` to * initialize this type. */ -typedef uint32_t otbn_addr_t; +typedef uint32_t sc_otbn_addr_t; /** * Information about an embedded OTBN application image. @@ -82,7 +82,7 @@ typedef uint32_t otbn_addr_t; * Use `OTBN_DECLARE_APP_SYMBOLS()` together with `OTBN_APP_T_INIT()` to * initialize this structure. */ -typedef struct otbn_app { +typedef struct sc_otbn_app { /** * Start of OTBN instruction memory. */ @@ -112,8 +112,8 @@ typedef struct otbn_app { * This pointer references OTBN's memory and is used to copy data at app load * time. */ - const otbn_addr_t dmem_data_start_addr; -} otbn_app_t; + const sc_otbn_addr_t dmem_data_start_addr; +} sc_otbn_app_t; /** * Generate the prefix to add to an OTBN symbol name used on the Ibex side @@ -122,8 +122,8 @@ typedef struct otbn_app { * memory for that symbol. * * This is needed by the OTBN driver to support DMEM/IMEM ranges but - * application code shouldn't need to use this. Use the `otbn_addr_t` type and - * supporting macros instead. + * application code shouldn't need to use this. Use the `sc_otbn_addr_t` type + * and supporting macros instead. */ #define OTBN_SYMBOL_PTR(app_name, sym) _otbn_local_app_##app_name##_##sym @@ -189,13 +189,13 @@ typedef struct otbn_app { * * After making all required symbols from the application image available * through `OTBN_DECLARE_APP_SYMBOLS()`, use this macro to initialize an - * `otbn_app_t` struct with those symbols. + * `sc_otbn_app_t` struct with those symbols. * * @param app_name Name of the application to load. * @see OTBN_DECLARE_APP_SYMBOLS() */ #define OTBN_APP_T_INIT(app_name) \ - ((otbn_app_t){ \ + ((sc_otbn_app_t){ \ .imem_start = OTBN_SYMBOL_PTR(app_name, _imem_start), \ .imem_end = OTBN_SYMBOL_PTR(app_name, _imem_end), \ .dmem_data_start = OTBN_SYMBOL_PTR(app_name, _dmem_data_start), \ @@ -204,7 +204,7 @@ typedef struct otbn_app { }) /** - * Initializes an `otbn_addr_t`. + * Initializes an `sc_otbn_addr_t`. */ #define OTBN_ADDR_T_INIT(app_name, symbol_name) \ ((uint32_t)OTBN_SYMBOL_ADDR(app_name, symbol_name)) @@ -219,7 +219,7 @@ typedef struct otbn_app { * @return The result of the operation. */ OT_WARN_UNUSED_RESULT -rom_error_t otbn_load_app(const otbn_app_t app); +rom_error_t sc_otbn_load_app(const sc_otbn_app_t app); /** * Copies data from the CPU memory to OTBN data memory. @@ -230,8 +230,8 @@ rom_error_t otbn_load_app(const otbn_app_t app); * @return The result of the operation. */ OT_WARN_UNUSED_RESULT -rom_error_t otbn_dmem_write(size_t num_words, const uint32_t *src, - otbn_addr_t dest); +rom_error_t sc_otbn_dmem_write(size_t num_words, const uint32_t *src, + sc_otbn_addr_t dest); /** * Copies data from OTBN's data memory to CPU memory. @@ -243,8 +243,8 @@ rom_error_t otbn_dmem_write(size_t num_words, const uint32_t *src, * @return The result of the operation. */ OT_WARN_UNUSED_RESULT -rom_error_t otbn_dmem_read(size_t num_words, const otbn_addr_t src, - uint32_t *dest); +rom_error_t sc_otbn_dmem_read(size_t num_words, const sc_otbn_addr_t src, + uint32_t *dest); /** * Start the execution of the application loaded into OTBN. @@ -254,7 +254,7 @@ rom_error_t otbn_dmem_read(size_t num_words, const otbn_addr_t src, * @return Result of the operation. */ OT_WARN_UNUSED_RESULT -rom_error_t otbn_execute(void); +rom_error_t sc_otbn_execute(void); /** * Blocks until OTBN is idle. @@ -264,7 +264,7 @@ rom_error_t otbn_execute(void); * @return Result of the operation. */ OT_WARN_UNUSED_RESULT -rom_error_t otbn_busy_wait_for_done(void); +rom_error_t sc_otbn_busy_wait_for_done(void); /** * Read OTBN's instruction count register. @@ -281,7 +281,7 @@ rom_error_t otbn_busy_wait_for_done(void); * @return count the value from the instruction count register */ OT_WARN_UNUSED_RESULT -uint32_t otbn_instruction_count_get(void); +uint32_t sc_otbn_instruction_count_get(void); /** * Wipe IMEM securely. @@ -291,7 +291,7 @@ uint32_t otbn_instruction_count_get(void); * @return Result of the operation. */ OT_WARN_UNUSED_RESULT -rom_error_t otbn_imem_sec_wipe(void); +rom_error_t sc_otbn_imem_sec_wipe(void); /** * Wipe DMEM securely. @@ -301,7 +301,7 @@ rom_error_t otbn_imem_sec_wipe(void); * @return Result of the operation. */ OT_WARN_UNUSED_RESULT -rom_error_t otbn_dmem_sec_wipe(void); +rom_error_t sc_otbn_dmem_sec_wipe(void); #ifdef __cplusplus } diff --git a/sw/device/silicon_creator/lib/drivers/otbn_unittest.cc b/sw/device/silicon_creator/lib/drivers/otbn_unittest.cc index c28207b38f0cd..f4eae995c0722 100644 --- a/sw/device/silicon_creator/lib/drivers/otbn_unittest.cc +++ b/sw/device/silicon_creator/lib/drivers/otbn_unittest.cc @@ -30,7 +30,8 @@ class OtbnTest : public rom_test::RomTest { * @param err_bits Error bits to be returned. * @param status Status of OTBN to be returned after the command is done. */ - void ExpectCmdRun(otbn_cmd_t cmd, uint32_t err_bits, otbn_status_t status) { + void ExpectCmdRun(sc_otbn_cmd_t cmd, uint32_t err_bits, + sc_otbn_status_t status) { EXPECT_ABS_WRITE32(base_ + OTBN_INTR_STATE_REG_OFFSET, { {OTBN_INTR_COMMON_DONE_BIT, 1}, @@ -50,7 +51,7 @@ class OtbnTest : public rom_test::RomTest { EXPECT_ABS_READ32(base_ + OTBN_ERR_BITS_REG_OFFSET, err_bits); EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, status); - if (err_bits == err_bits_ok_ && status == kOtbnStatusIdle) { + if (err_bits == err_bits_ok_ && status == kScOtbnStatusIdle) { EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, status); } } @@ -69,103 +70,106 @@ TEST_F(ExecuteTest, ExecuteSuccess) { static_assert(OTBN_IMEM_SIZE_BYTES >= 8, "OTBN IMEM size too small."); // Read twice for hardening. - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusIdle); - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusIdle); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kScOtbnStatusIdle); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kScOtbnStatusIdle); EXPECT_SEC_WRITE32(base_ + OTBN_CTRL_REG_OFFSET, 0x1); - ExpectCmdRun(kOtbnCmdExecute, err_bits_ok_, kOtbnStatusIdle); + ExpectCmdRun(kScOtbnCmdExecute, err_bits_ok_, kScOtbnStatusIdle); - EXPECT_EQ(otbn_execute(), kErrorOk); + EXPECT_EQ(sc_otbn_execute(), kErrorOk); } TEST_F(ExecuteTest, ExecuteError) { // Read twice for hardening. - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusIdle); - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusIdle); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kScOtbnStatusIdle); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kScOtbnStatusIdle); EXPECT_SEC_WRITE32(base_ + OTBN_CTRL_REG_OFFSET, 0x1); // Nonzero error bits. - ExpectCmdRun(kOtbnCmdExecute, 1 << OTBN_ERR_BITS_FATAL_SOFTWARE_BIT, - kOtbnStatusIdle); + ExpectCmdRun(kScOtbnCmdExecute, 1 << OTBN_ERR_BITS_FATAL_SOFTWARE_BIT, + kScOtbnStatusIdle); - EXPECT_EQ(otbn_execute(), kErrorOtbnExecutionFailed); + EXPECT_EQ(sc_otbn_execute(), kErrorOtbnExecutionFailed); } TEST_F(ExecuteTest, ExecuteBusy) { // Read twice for hardening. - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusIdle); - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusIdle); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kScOtbnStatusIdle); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kScOtbnStatusIdle); EXPECT_SEC_WRITE32(base_ + OTBN_CTRL_REG_OFFSET, 0x01); // Return a busy status after the `done` interrupt. - ExpectCmdRun(kOtbnCmdExecute, err_bits_ok_, kOtbnStatusBusyExecute); + ExpectCmdRun(kScOtbnCmdExecute, err_bits_ok_, kScOtbnStatusBusyExecute); - EXPECT_EQ(otbn_execute(), kErrorOtbnExecutionFailed); + EXPECT_EQ(sc_otbn_execute(), kErrorOtbnExecutionFailed); } TEST_F(ExecuteTest, ExecuteBlockUntilIdle) { // Test assumption. static_assert(OTBN_IMEM_SIZE_BYTES >= 8, "OTBN IMEM size too small."); - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusBusySecWipeDmem); - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusBusySecWipeDmem); - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusBusySecWipeDmem); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, + kScOtbnStatusBusySecWipeDmem); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, + kScOtbnStatusBusySecWipeDmem); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, + kScOtbnStatusBusySecWipeDmem); // Read twice for hardening. - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusIdle); - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusIdle); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kScOtbnStatusIdle); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kScOtbnStatusIdle); EXPECT_SEC_WRITE32(base_ + OTBN_CTRL_REG_OFFSET, 0x1); - ExpectCmdRun(kOtbnCmdExecute, err_bits_ok_, kOtbnStatusIdle); + ExpectCmdRun(kScOtbnCmdExecute, err_bits_ok_, kScOtbnStatusIdle); - EXPECT_EQ(otbn_execute(), kErrorOk); + EXPECT_EQ(sc_otbn_execute(), kErrorOk); } class IsBusyTest : public OtbnTest {}; TEST_F(IsBusyTest, Success) { - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusBusyExecute); - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusBusyExecute); - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusBusyExecute); - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusBusyExecute); - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusIdle); - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusIdle); - - EXPECT_EQ(otbn_busy_wait_for_done(), kErrorOk); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kScOtbnStatusBusyExecute); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kScOtbnStatusBusyExecute); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kScOtbnStatusBusyExecute); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kScOtbnStatusBusyExecute); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kScOtbnStatusIdle); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kScOtbnStatusIdle); + + EXPECT_EQ(sc_otbn_busy_wait_for_done(), kErrorOk); } class ImemSecWipeTest : public OtbnTest {}; TEST_F(ImemSecWipeTest, Success) { - ExpectCmdRun(kOtbnCmdSecWipeImem, err_bits_ok_, kOtbnStatusIdle); + ExpectCmdRun(kScOtbnCmdSecWipeImem, err_bits_ok_, kScOtbnStatusIdle); - EXPECT_EQ(otbn_imem_sec_wipe(), kErrorOk); + EXPECT_EQ(sc_otbn_imem_sec_wipe(), kErrorOk); } TEST_F(ImemSecWipeTest, Failure) { - ExpectCmdRun(kOtbnCmdSecWipeImem, 1 << OTBN_ERR_BITS_FATAL_SOFTWARE_BIT, - kOtbnStatusIdle); + ExpectCmdRun(kScOtbnCmdSecWipeImem, 1 << OTBN_ERR_BITS_FATAL_SOFTWARE_BIT, + kScOtbnStatusIdle); - EXPECT_EQ(otbn_imem_sec_wipe(), kErrorOtbnSecWipeImemFailed); + EXPECT_EQ(sc_otbn_imem_sec_wipe(), kErrorOtbnSecWipeImemFailed); } class DmemSecWipeTest : public OtbnTest {}; TEST_F(DmemSecWipeTest, Success) { - ExpectCmdRun(kOtbnCmdSecWipeDmem, err_bits_ok_, kOtbnStatusIdle); + ExpectCmdRun(kScOtbnCmdSecWipeDmem, err_bits_ok_, kScOtbnStatusIdle); - EXPECT_EQ(otbn_dmem_sec_wipe(), kErrorOk); + EXPECT_EQ(sc_otbn_dmem_sec_wipe(), kErrorOk); } TEST_F(DmemSecWipeTest, Failure) { - ExpectCmdRun(kOtbnCmdSecWipeDmem, 1 << OTBN_ERR_BITS_FATAL_SOFTWARE_BIT, - kOtbnStatusIdle); + ExpectCmdRun(kScOtbnCmdSecWipeDmem, 1 << OTBN_ERR_BITS_FATAL_SOFTWARE_BIT, + kScOtbnStatusIdle); - EXPECT_EQ(otbn_dmem_sec_wipe(), kErrorOtbnSecWipeDmemFailed); + EXPECT_EQ(sc_otbn_dmem_sec_wipe(), kErrorOtbnSecWipeDmemFailed); } class DmemWriteTest : public OtbnTest {}; @@ -175,14 +179,14 @@ TEST_F(DmemWriteTest, SuccessWithoutOffset) { static_assert(OTBN_DMEM_SIZE_BYTES >= 8, "OTBN DMEM size too small."); std::array test_data = {0x12345678, 0xabcdef01}; - otbn_addr_t dest_addr = 0; + sc_otbn_addr_t dest_addr = 0; EXPECT_CALL(rnd_, Uint32()).WillOnce(Return(0)); EXPECT_ABS_WRITE32(base_ + OTBN_DMEM_REG_OFFSET + dest_addr, test_data[0]); EXPECT_ABS_WRITE32(base_ + OTBN_DMEM_REG_OFFSET + dest_addr + 4, test_data[1]); - EXPECT_EQ(otbn_dmem_write(2, test_data.data(), dest_addr), kErrorOk); + EXPECT_EQ(sc_otbn_dmem_write(2, test_data.data(), dest_addr), kErrorOk); } TEST_F(DmemWriteTest, SuccessWithOffset) { @@ -190,21 +194,21 @@ TEST_F(DmemWriteTest, SuccessWithOffset) { static_assert(OTBN_DMEM_SIZE_BYTES >= 12, "OTBN DMEM size too small."); std::array test_data = {0x12345678, 0xabcdef01}; - otbn_addr_t dest_addr = 4; + sc_otbn_addr_t dest_addr = 4; EXPECT_CALL(rnd_, Uint32()).WillOnce(Return(0)); EXPECT_ABS_WRITE32(base_ + OTBN_DMEM_REG_OFFSET + dest_addr, test_data[0]); EXPECT_ABS_WRITE32(base_ + OTBN_DMEM_REG_OFFSET + dest_addr + 4, test_data[1]); - EXPECT_EQ(otbn_dmem_write(2, test_data.data(), dest_addr), kErrorOk); + EXPECT_EQ(sc_otbn_dmem_write(2, test_data.data(), dest_addr), kErrorOk); } TEST_F(DmemWriteTest, FailureOutOfRange) { std::array test_data = {0x12345678, 0xabcdef01}; - otbn_addr_t dest_addr = OTBN_DMEM_SIZE_BYTES; + sc_otbn_addr_t dest_addr = OTBN_DMEM_SIZE_BYTES; - EXPECT_EQ(otbn_dmem_write(2, test_data.data(), dest_addr), + EXPECT_EQ(sc_otbn_dmem_write(2, test_data.data(), dest_addr), kErrorOtbnBadOffsetLen); } @@ -212,18 +216,18 @@ TEST_F(DmemWriteTest, FailureOverflowNumWords) { // Try to trigger an integer overflow with `num_words`. size_t num_words = (std::numeric_limits::max() / sizeof(uint32_t)) + 1; - otbn_addr_t dest_addr = 0; + sc_otbn_addr_t dest_addr = 0; - EXPECT_EQ(otbn_dmem_write(num_words, NULL, dest_addr), + EXPECT_EQ(sc_otbn_dmem_write(num_words, NULL, dest_addr), kErrorOtbnBadOffsetLen); } TEST_F(DmemWriteTest, FailureOverflowOffset) { // Try to trigger an integer overflow with `dest_addr`. std::array test_data = {0x12345678, 0xabcdef01}; - otbn_addr_t dest_addr = std::numeric_limits::max(); + sc_otbn_addr_t dest_addr = std::numeric_limits::max(); - EXPECT_EQ(otbn_dmem_write(test_data.size(), test_data.data(), dest_addr), + EXPECT_EQ(sc_otbn_dmem_write(test_data.size(), test_data.data(), dest_addr), kErrorOtbnBadOffsetLen); } @@ -239,8 +243,8 @@ TEST_F(DmemReadTest, SuccessWithoutOffset) { std::array test_data = {0}; - otbn_addr_t src_addr = 0; - EXPECT_EQ(otbn_dmem_read(2, src_addr, test_data.data()), kErrorOk); + sc_otbn_addr_t src_addr = 0; + EXPECT_EQ(sc_otbn_dmem_read(2, src_addr, test_data.data()), kErrorOk); EXPECT_THAT(test_data, ElementsAre(0x12345678, 0xabcdef01)); } @@ -253,8 +257,8 @@ TEST_F(DmemReadTest, SuccessWithOffset) { std::array test_data = {0}; - otbn_addr_t src_addr = 4; - EXPECT_EQ(otbn_dmem_read(2, src_addr, test_data.data()), kErrorOk); + sc_otbn_addr_t src_addr = 4; + EXPECT_EQ(sc_otbn_dmem_read(2, src_addr, test_data.data()), kErrorOk); EXPECT_THAT(test_data, ElementsAre(0x12345678, 0xabcdef01)); } @@ -263,8 +267,8 @@ class OtbnAppTest : public OtbnTest {}; TEST_F(OtbnAppTest, OtbnLoadAppSuccess) { std::array imem_data = {0x01234567, 0x89abcdef}; std::array dmem_data = {0x456789ab, 0xcdef0123}; - otbn_addr_t dmem_data_offset = 0x12; - otbn_app_t app = { + sc_otbn_addr_t dmem_data_offset = 0x12; + sc_otbn_app_t app = { .imem_start = imem_data.data(), .imem_end = imem_data.data() + imem_data.size(), .dmem_data_start = dmem_data.data(), @@ -278,22 +282,24 @@ TEST_F(OtbnAppTest, OtbnLoadAppSuccess) { static_assert(OTBN_IMEM_SIZE_BYTES >= sizeof(uint32_t) * imem_data.size(), "OTBN IMEM size too small"); - // `otbn_busy_wait_for_done` - begin with busy to ensure we wait until idle. - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusBusyExecute); - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusBusySecWipeDmem); + // `sc_otbn_busy_wait_for_done` - begin with busy to ensure we wait until + // idle. + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kScOtbnStatusBusyExecute); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, + kScOtbnStatusBusySecWipeDmem); // Read twice for hardening. - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusIdle); - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusIdle); - // `otbn_dmem_sec_wipe` - ExpectCmdRun(kOtbnCmdSecWipeDmem, err_bits_ok_, kOtbnStatusIdle); - // `otbn_imem_sec_wipe` - ExpectCmdRun(kOtbnCmdSecWipeImem, err_bits_ok_, kOtbnStatusIdle); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kScOtbnStatusIdle); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kScOtbnStatusIdle); + // `sc_otbn_dmem_sec_wipe` + ExpectCmdRun(kScOtbnCmdSecWipeDmem, err_bits_ok_, kScOtbnStatusIdle); + // `sc_otbn_imem_sec_wipe` + ExpectCmdRun(kScOtbnCmdSecWipeImem, err_bits_ok_, kScOtbnStatusIdle); // `otbn_imem_write` EXPECT_CALL(rnd_, Uint32()).WillOnce(Return(0)); EXPECT_ABS_WRITE32(base_ + OTBN_IMEM_REG_OFFSET, imem_data[0]); EXPECT_ABS_WRITE32(base_ + OTBN_IMEM_REG_OFFSET + sizeof(uint32_t), imem_data[1]); - // `otbn_dmem_write` + // `sc_otbn_dmem_write` EXPECT_CALL(rnd_, Uint32()).WillOnce(Return(0)); EXPECT_ABS_WRITE32(base_ + OTBN_DMEM_REG_OFFSET + dmem_data_offset, dmem_data[0]); @@ -301,15 +307,15 @@ TEST_F(OtbnAppTest, OtbnLoadAppSuccess) { base_ + OTBN_DMEM_REG_OFFSET + dmem_data_offset + sizeof(uint32_t), dmem_data[1]); - EXPECT_EQ(otbn_load_app(app), kErrorOk); + EXPECT_EQ(sc_otbn_load_app(app), kErrorOk); } TEST_F(OtbnAppTest, OtbnLoadInvalidAppEmptyImem) { // Create an invalid app with an empty IMEM range. std::array imem_data = {}; std::array dmem_data = {0x456789ab, 0xcdef0123}; - otbn_addr_t dmem_data_offset = 0x12; - otbn_app_t app = { + sc_otbn_addr_t dmem_data_offset = 0x12; + sc_otbn_app_t app = { .imem_start = imem_data.data(), .imem_end = imem_data.data() + imem_data.size(), .dmem_data_start = dmem_data.data(), @@ -323,7 +329,7 @@ TEST_F(OtbnAppTest, OtbnLoadInvalidAppEmptyImem) { static_assert(OTBN_IMEM_SIZE_BYTES >= sizeof(uint32_t) * imem_data.size(), "OTBN IMEM size too small"); - EXPECT_EQ(otbn_load_app(app), kErrorOtbnInvalidArgument); + EXPECT_EQ(sc_otbn_load_app(app), kErrorOtbnInvalidArgument); } TEST_F(OtbnAppTest, OtbnLoadInvalidAppImemOutOfRange) { @@ -331,8 +337,8 @@ TEST_F(OtbnAppTest, OtbnLoadInvalidAppImemOutOfRange) { std::array imem_data = {0}; std::array dmem_data = {0x456789ab, 0xcdef0123}; - otbn_addr_t dmem_data_offset = 0x12; - otbn_app_t app = { + sc_otbn_addr_t dmem_data_offset = 0x12; + sc_otbn_app_t app = { .imem_start = imem_data.data(), .imem_end = imem_data.data() + imem_data.size(), .dmem_data_start = dmem_data.data(), @@ -341,14 +347,14 @@ TEST_F(OtbnAppTest, OtbnLoadInvalidAppImemOutOfRange) { }; // Read twice for hardening. - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusIdle); - EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kOtbnStatusIdle); - // `otbn_dmem_sec_wipe` - ExpectCmdRun(kOtbnCmdSecWipeDmem, err_bits_ok_, kOtbnStatusIdle); - // `otbn_imem_sec_wipe` - ExpectCmdRun(kOtbnCmdSecWipeImem, err_bits_ok_, kOtbnStatusIdle); - - EXPECT_EQ(otbn_load_app(app), kErrorOtbnBadOffsetLen); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kScOtbnStatusIdle); + EXPECT_ABS_READ32(base_ + OTBN_STATUS_REG_OFFSET, kScOtbnStatusIdle); + // `sc_otbn_dmem_sec_wipe` + ExpectCmdRun(kScOtbnCmdSecWipeDmem, err_bits_ok_, kScOtbnStatusIdle); + // `sc_otbn_imem_sec_wipe` + ExpectCmdRun(kScOtbnCmdSecWipeImem, err_bits_ok_, kScOtbnStatusIdle); + + EXPECT_EQ(sc_otbn_load_app(app), kErrorOtbnBadOffsetLen); } class OtbnWriteTest : public OtbnTest {}; @@ -368,7 +374,7 @@ TEST_F(OtbnWriteTest, Success) { base_ + OTBN_DMEM_REG_OFFSET + kDestAddr + sizeof(uint32_t), test_data[1]); - EXPECT_EQ(otbn_dmem_write(2, test_data.data(), kDestAddr), kErrorOk); + EXPECT_EQ(sc_otbn_dmem_write(2, test_data.data(), kDestAddr), kErrorOk); } class OtbnReadTest : public OtbnTest {}; @@ -386,7 +392,7 @@ TEST_F(OtbnReadTest, Success) { EXPECT_ABS_READ32(base_ + OTBN_DMEM_REG_OFFSET + kSrcAddr + sizeof(uint32_t), 0xabcdef01); - EXPECT_EQ(otbn_dmem_read(2, kSrcAddr, test_data.data()), kErrorOk); + EXPECT_EQ(sc_otbn_dmem_read(2, kSrcAddr, test_data.data()), kErrorOk); EXPECT_THAT(test_data, ElementsAre(0x12345678, 0xabcdef01)); } } // namespace diff --git a/sw/lib/sw/device/silicon_creator/sigverify/mod_exp_otbn.c b/sw/lib/sw/device/silicon_creator/sigverify/mod_exp_otbn.c index f0acd18472cd7..d292f8525c88e 100644 --- a/sw/lib/sw/device/silicon_creator/sigverify/mod_exp_otbn.c +++ b/sw/lib/sw/device/silicon_creator/sigverify/mod_exp_otbn.c @@ -22,15 +22,15 @@ OTBN_DECLARE_SYMBOL_ADDR(run_rsa_verify_3072_rr_modexp, OTBN_DECLARE_SYMBOL_ADDR(run_rsa_verify_3072_rr_modexp, m0inv); // The Montgomery constant m0_inv. -static const otbn_app_t kOtbnAppRsa = +static const sc_otbn_app_t kOtbnAppRsa = OTBN_APP_T_INIT(run_rsa_verify_3072_rr_modexp); -static const otbn_addr_t kOtbnVarRsaOutBuf = +static const sc_otbn_addr_t kOtbnVarRsaOutBuf = OTBN_ADDR_T_INIT(run_rsa_verify_3072_rr_modexp, out_buf); -static const otbn_addr_t kOtbnVarRsaInMod = +static const sc_otbn_addr_t kOtbnVarRsaInMod = OTBN_ADDR_T_INIT(run_rsa_verify_3072_rr_modexp, in_mod); -static const otbn_addr_t kOtbnVarRsaInBuf = +static const sc_otbn_addr_t kOtbnVarRsaInBuf = OTBN_ADDR_T_INIT(run_rsa_verify_3072_rr_modexp, in_buf); -static const otbn_addr_t kOtbnVarRsaM0Inv = +static const sc_otbn_addr_t kOtbnVarRsaM0Inv = OTBN_ADDR_T_INIT(run_rsa_verify_3072_rr_modexp, m0inv); /** @@ -42,8 +42,8 @@ static const otbn_addr_t kOtbnVarRsaM0Inv = */ OT_WARN_UNUSED_RESULT static rom_error_t write_rsa_3072_int_to_otbn(const sigverify_rsa_buffer_t *src, - otbn_addr_t dst) { - return otbn_dmem_write(kSigVerifyRsaNumWords, src->data, dst); + sc_otbn_addr_t dst) { + return sc_otbn_dmem_write(kSigVerifyRsaNumWords, src->data, dst); } /** @@ -54,9 +54,9 @@ static rom_error_t write_rsa_3072_int_to_otbn(const sigverify_rsa_buffer_t *src, * @return The result of the operation. */ OT_WARN_UNUSED_RESULT -static rom_error_t read_rsa_3072_int_from_otbn(const otbn_addr_t src, +static rom_error_t read_rsa_3072_int_from_otbn(const sc_otbn_addr_t src, sigverify_rsa_buffer_t *dst) { - return otbn_dmem_read(kSigVerifyRsaNumWords, src, dst->data); + return sc_otbn_dmem_read(kSigVerifyRsaNumWords, src, dst->data); } OT_WARN_UNUSED_RESULT @@ -65,7 +65,7 @@ static rom_error_t run_otbn_rsa_3072_modexp( const sigverify_rsa_buffer_t *signature, sigverify_rsa_buffer_t *recovered_message) { // Load the RSA app. - HARDENED_RETURN_IF_ERROR(otbn_load_app(kOtbnAppRsa)); + HARDENED_RETURN_IF_ERROR(sc_otbn_load_app(kOtbnAppRsa)); // Set the modulus (n). HARDENED_RETURN_IF_ERROR( @@ -76,17 +76,17 @@ static rom_error_t run_otbn_rsa_3072_modexp( write_rsa_3072_int_to_otbn(signature, kOtbnVarRsaInBuf)); // Set the precomputed constant m0_inv. - HARDENED_RETURN_IF_ERROR(otbn_dmem_write( - kOtbnWideWordNumWords, public_key->n0_inv, kOtbnVarRsaM0Inv)); + HARDENED_RETURN_IF_ERROR(sc_otbn_dmem_write( + kScOtbnWideWordNumWords, public_key->n0_inv, kOtbnVarRsaM0Inv)); // Start the OTBN routine. - HARDENED_RETURN_IF_ERROR(otbn_execute()); - SEC_MMIO_WRITE_INCREMENT(kOtbnSecMmioExecute); + HARDENED_RETURN_IF_ERROR(sc_otbn_execute()); + SEC_MMIO_WRITE_INCREMENT(kScOtbnSecMmioExecute); // Check that the instruction count falls within the expected range. If the // instruction count falls outside this range, it indicates that there was a // fault injection attack of some kind during OTBN execution. - uint32_t count = otbn_instruction_count_get(); + uint32_t count = sc_otbn_instruction_count_get(); if (launder32(count) < kModExpOtbnInsnCountMin || launder32(count) > kModExpOtbnInsnCountMax) { return kErrorOtbnBadInsnCount;