warmboot: add code for decrypting/restoring tzram.
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
/* "private" functions. */
|
||||
static bool secmon_should_clear_aes_keyslot(unsigned int keyslot);
|
||||
static void secmon_clear_unused_keyslots(void);
|
||||
static void secmon_decrypt_saved_image(void *dst, const void *src, size_t size);
|
||||
|
||||
void secmon_restore_to_tzram(const uint32_t target_firmware) {
|
||||
/* Newer warmboot binaries clear the untouched keyslots for safety. */
|
||||
@@ -32,10 +33,51 @@ void secmon_restore_to_tzram(const uint32_t target_firmware) {
|
||||
secmon_clear_unused_keyslots();
|
||||
}
|
||||
|
||||
/* Decrypt Secure Monitor from DRAM into TZRAM. */
|
||||
void *tzram_src = (void *)(0x80010000);
|
||||
void *tzram_dst = (void *)(target_firmware >= ATMOSPHERE_TARGET_FIRMWARE_500 ? 0x7C012000 : 0x7C010000);
|
||||
const size_t tzram_size = 0xE000;
|
||||
secmon_decrypt_saved_image(tzram_dst, tzram_src, tzram_size);
|
||||
|
||||
/* Nintendo clears DRAM, but I'm not sure why, given they lock out BPMP access to DRAM. */
|
||||
for (size_t i = 0; i < tzram_size/sizeof(uint32_t); i++) {
|
||||
((volatile uint32_t *)tzram_src)[i] = 0;
|
||||
}
|
||||
|
||||
/* Make security engine require secure busmaster. */
|
||||
se_get_regs()->_0x4 = 0;
|
||||
|
||||
/* TODO: stuff */
|
||||
}
|
||||
|
||||
void secmon_decrypt_saved_image(void *dst, const void *src, size_t size) {
|
||||
/* First, AES-256-CBC decrypt the image into TZRAM. */
|
||||
se_aes_256_cbc_decrypt(KEYSLOT_SWITCH_LP0TZRAMKEY, dst, size, src, size);
|
||||
|
||||
/* Next, calculate CMAC. */
|
||||
uint32_t tzram_cmac[4] = {0, 0, 0, 0};
|
||||
se_compute_aes_256_cmac(KEYSLOT_SWITCH_LP0TZRAMKEY, tzram_cmac, sizeof(tzram_cmac), dst, size);
|
||||
|
||||
/* Validate the MAC against saved one in PMC scratch. */
|
||||
if (tzram_cmac[0] != APBDEV_PMC_SECURE_SCRATCH112_0 ||
|
||||
tzram_cmac[1] != APBDEV_PMC_SECURE_SCRATCH113_0 ||
|
||||
tzram_cmac[2] != APBDEV_PMC_SECURE_SCRATCH114_0 ||
|
||||
tzram_cmac[3] != APBDEV_PMC_SECURE_SCRATCH115_0) {
|
||||
reboot();
|
||||
}
|
||||
|
||||
/* Clear the PMC scratch registers that hold the CMAC. */
|
||||
APBDEV_PMC_SECURE_SCRATCH112_0 = 0;
|
||||
APBDEV_PMC_SECURE_SCRATCH113_0 = 0;
|
||||
APBDEV_PMC_SECURE_SCRATCH114_0 = 0;
|
||||
APBDEV_PMC_SECURE_SCRATCH115_0 = 0;
|
||||
|
||||
/* Clear keyslot now that we're done with it. */
|
||||
clear_aes_keyslot(KEYSLOT_SWITCH_LP0TZRAMKEY);
|
||||
}
|
||||
|
||||
bool secmon_should_clear_aes_keyslot(unsigned int keyslot) {
|
||||
/* We'll just compare keyslot against a hardcoded list of keys. */
|
||||
static const uint8_t saved_keyslots[6] = {
|
||||
KEYSLOT_SWITCH_LP0TZRAMKEY,
|
||||
KEYSLOT_SWITCH_SESSIONKEY,
|
||||
@@ -64,4 +106,4 @@ void secmon_clear_unused_keyslots(void) {
|
||||
for (unsigned int i = 0; i < KEYSLOT_RSA_MAX; i++) {
|
||||
clear_rsa_keyslot(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user