From a969a7dff27bf6c0856f1b02215970554ca27b2c Mon Sep 17 00:00:00 2001 From: William Vinnicombe Date: Mon, 10 Feb 2025 15:52:38 +0000 Subject: [PATCH] Shrink IV storage size Use bi_ptr_string instead of 4 bi_ptr_int32s --- enc_bootloader/enc_bootloader.c | 14 ++------------ main.cpp | 7 +++---- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/enc_bootloader/enc_bootloader.c b/enc_bootloader/enc_bootloader.c index 7872493..8ea0ea9 100644 --- a/enc_bootloader/enc_bootloader.c +++ b/enc_bootloader/enc_bootloader.c @@ -282,25 +282,15 @@ int main() { bi_decl(bi_ptr_int32(0, 0, data_start_addr, 0x20000000)); bi_decl(bi_ptr_int32(0, 0, data_size, 0x78000)); - bi_decl(bi_ptr_int32(0, 0, iv0, 0)); - bi_decl(bi_ptr_int32(0, 0, iv1, 1)); - bi_decl(bi_ptr_int32(0, 0, iv2, 2)); - bi_decl(bi_ptr_int32(0, 0, iv3, 3)); + bi_decl(bi_ptr_string(0, 0, iv, "0123456789abcdef", 17)) bi_decl(bi_ptr_int32(0, 0, otp_key_page, 30)); - // Initialise IV from binary info words - uint8_t iv[16]; - memcpy(iv, (void*)&iv0, sizeof(iv0)); - memcpy(iv + 4, (void*)&iv1, sizeof(iv1)); - memcpy(iv + 8, (void*)&iv2, sizeof(iv2)); - memcpy(iv + 12, (void*)&iv3, sizeof(iv3)); - // Initialise random state init_rstate(); // Read key directly from OTP - guarded reads will throw a bus fault if there are any errors uint16_t* otp_data = (uint16_t*)OTP_DATA_GUARDED_BASE; - decrypt((uint8_t*)&(otp_data[(OTP_CMD_ROW_BITS & (otp_key_page * 0x40))]), iv, (void*)data_start_addr, data_size/16); + decrypt((uint8_t*)&(otp_data[(OTP_CMD_ROW_BITS & (otp_key_page * 0x40))]), (uint8_t*)iv, (void*)data_start_addr, data_size/16); otp_hw->sw_lock[otp_key_page] = 0xf; // Increase stack limit by 0x100 diff --git a/main.cpp b/main.cpp index a4ac49f..ac889bb 100644 --- a/main.cpp +++ b/main.cpp @@ -5008,10 +5008,9 @@ bool encrypt_command::execute(device_map &devices) { config_guts(program); // iv for (int i=0; i < 4; i++) { - std::stringstream ss; - ss << "iv" << i; - settings.config.key = ss.str(); - settings.config.value = hex_string(*(uint32_t*)(iv_data.data() + i*sizeof(uint32_t))); + string s((char*)iv_data.data(), iv_data.size()); + settings.config.key = "iv"; + settings.config.value = s; config_guts(program); } // otp_key_page