Skip to content

Commit

Permalink
[silicon_creator] namespace OTBN driver functions
Browse files Browse the repository at this point in the history
This partially addresses #21937 by renaming the OTBN driver functions so
they do not collide with similar cryptolib functions.

Signed-off-by: Tim Trippel <[email protected]>
  • Loading branch information
timothytrippel authored and sameo committed May 30, 2024
1 parent cda3707 commit 4094bd9
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 167 deletions.
67 changes: 34 additions & 33 deletions sw/device/silicon_creator/lib/drivers/otbn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 };
Expand All @@ -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;
Expand All @@ -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.
Expand All @@ -160,45 +161,45 @@ 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
// previous value.
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);
}

/**
Expand All @@ -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);
Expand All @@ -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;
Expand Down
74 changes: 37 additions & 37 deletions sw/device/silicon_creator/lib/drivers/otbn.h
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};

/**
Expand All @@ -32,46 +32,46 @@ enum {
*
* Example:
* ```
* otbn_execute();
* SEC_MMIO_WRITE_INCREMENT(kOtbnSecMmioExecute);
* sc_otbn_execute();
* SEC_MMIO_WRITE_INCREMENT(kScOtbnSecMmioExecute);
* ```
*/
enum {
kOtbnSecMmioExecute = 1,
kScOtbnSecMmioExecute = 1,
};

/**
* OTBN commands
*
* 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
*
* 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.
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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), \
Expand All @@ -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))
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
}
Expand Down
Loading

0 comments on commit 4094bd9

Please sign in to comment.