[stage2] Refactor key_derivation type and function declarations

This commit is contained in:
TuxSH 2018-05-11 23:01:13 +02:00
parent eb8573093d
commit 3ac3183c85
5 changed files with 76 additions and 76 deletions

View file

@ -4,8 +4,7 @@
#include "exocfg.h" #include "exocfg.h"
#include "fuse.h" #include "fuse.h"
static const u8 keyblob_seeds[MASTERKEY_REVISION_MAX][0x10] = static const uint8_t keyblob_seeds[MASTERKEY_REVISION_MAX][0x10] = {
{
{0xDF, 0x20, 0x6F, 0x59, 0x44, 0x54, 0xEF, 0xDC, 0x70, 0x74, 0x48, 0x3B, 0x0D, 0xED, 0x9F, 0xD3}, /* Keyblob seed 00. */ {0xDF, 0x20, 0x6F, 0x59, 0x44, 0x54, 0xEF, 0xDC, 0x70, 0x74, 0x48, 0x3B, 0x0D, 0xED, 0x9F, 0xD3}, /* Keyblob seed 00. */
{0x0C, 0x25, 0x61, 0x5D, 0x68, 0x4C, 0xEB, 0x42, 0x1C, 0x23, 0x79, 0xEA, 0x82, 0x25, 0x12, 0xAC}, /* Keyblob seed 01. */ {0x0C, 0x25, 0x61, 0x5D, 0x68, 0x4C, 0xEB, 0x42, 0x1C, 0x23, 0x79, 0xEA, 0x82, 0x25, 0x12, 0xAC}, /* Keyblob seed 01. */
{0x33, 0x76, 0x85, 0xEE, 0x88, 0x4A, 0xAE, 0x0A, 0xC2, 0x8A, 0xFD, 0x7D, 0x63, 0xC0, 0x43, 0x3B}, /* Keyblob seed 02. */ {0x33, 0x76, 0x85, 0xEE, 0x88, 0x4A, 0xAE, 0x0A, 0xC2, 0x8A, 0xFD, 0x7D, 0x63, 0xC0, 0x43, 0x3B}, /* Keyblob seed 02. */
@ -13,111 +12,110 @@ static const u8 keyblob_seeds[MASTERKEY_REVISION_MAX][0x10] =
{0xBB, 0x5A, 0x01, 0xF9, 0x88, 0xAF, 0xF5, 0xFC, 0x6C, 0xFF, 0x07, 0x9E, 0x13, 0x3C, 0x39, 0x80}, /* Keyblob seed 04. */ {0xBB, 0x5A, 0x01, 0xF9, 0x88, 0xAF, 0xF5, 0xFC, 0x6C, 0xFF, 0x07, 0x9E, 0x13, 0x3C, 0x39, 0x80}, /* Keyblob seed 04. */
}; };
static const u8 keyblob_mac_seed[0x10] = static const uint8_t keyblob_mac_seed[0x10] = {
{
0x59, 0xC7, 0xFB, 0x6F, 0xBE, 0x9B, 0xBE, 0x87, 0x65, 0x6B, 0x15, 0xC0, 0x53, 0x73, 0x36, 0xA5 0x59, 0xC7, 0xFB, 0x6F, 0xBE, 0x9B, 0xBE, 0x87, 0x65, 0x6B, 0x15, 0xC0, 0x53, 0x73, 0x36, 0xA5
}; };
static const uint8_t masterkey_seed[0x10] = static const uint8_t masterkey_seed[0x10] = {
{
0xD8, 0xA2, 0x41, 0x0A, 0xC6, 0xC5, 0x90, 0x01, 0xC6, 0x1D, 0x6A, 0x26, 0x7C, 0x51, 0x3F, 0x3C 0xD8, 0xA2, 0x41, 0x0A, 0xC6, 0xC5, 0x90, 0x01, 0xC6, 0x1D, 0x6A, 0x26, 0x7C, 0x51, 0x3F, 0x3C
}; };
static const uint8_t devicekey_seed[0x10] = static const uint8_t devicekey_seed[0x10] = {
{
0x4F, 0x02, 0x5F, 0x0E, 0xB6, 0x6D, 0x11, 0x0E, 0xDC, 0x32, 0x7D, 0x41, 0x86, 0xC2, 0xF4, 0x78 0x4F, 0x02, 0x5F, 0x0E, 0xB6, 0x6D, 0x11, 0x0E, 0xDC, 0x32, 0x7D, 0x41, 0x86, 0xC2, 0xF4, 0x78
}; };
static const uint8_t devicekey_4x_seed[0x10] = static const uint8_t devicekey_4x_seed[0x10] = {
{
0x0C, 0x91, 0x09, 0xDB, 0x93, 0x93, 0x07, 0x81, 0x07, 0x3C, 0xC4, 0x16, 0x22, 0x7C, 0x6C, 0x28 0x0C, 0x91, 0x09, 0xDB, 0x93, 0x93, 0x07, 0x81, 0x07, 0x3C, 0xC4, 0x16, 0x22, 0x7C, 0x6C, 0x28
}; };
static const uint8_t masterkey_4x_seed[0x10] = static const uint8_t masterkey_4x_seed[0x10] = {
{
0x2D, 0xC1, 0xF4, 0x8D, 0xF3, 0x5B, 0x69, 0x33, 0x42, 0x10, 0xAC, 0x65, 0xDA, 0x90, 0x46, 0x66 0x2D, 0xC1, 0xF4, 0x8D, 0xF3, 0x5B, 0x69, 0x33, 0x42, 0x10, 0xAC, 0x65, 0xDA, 0x90, 0x46, 0x66
}; };
void get_tsec_key(void *dst) { static void get_tsec_key(void *dst, const void *tsec_fw, size_t tsec_fw_size) {
/* TODO: Implement this method. Attempt to read TSEC fw from NAND, or from SD if that fails. */ /* TODO: Implement this method. Attempt to read TSEC fw from NAND, or from SD if that fails. */
} }
void get_keyblob(void *dst, u32 revision) { void get_keyblob(nx_keyblob_t *dst, uint32_t revision, const nx_keyblob_t *available_keyblobs, uint32_t available_revision) {
if (revision >= 0x20) { if (revision >= 0x20) {
generic_panic(); generic_panic();
} }
/* TODO: Read the appropriate keyblob from eMMC Boot0 partition. */ if (revision <= available_revision) {
*dst = available_keyblobs[revision];
} else {
generic_panic();
/* TODO: what should we do? */
}
} }
bool safe_memcmp(u8 *a, u8 *b, u32 sz) { static bool safe_memcmp(uint8_t *a, uint8_t *b, size_t sz) {
u8 different = 0; uint8_t different = 0;
for (u32 i = 0; i < sz; i++) { for (unsigned int i = 0; i < sz; i++) {
different |= a[i] ^ b[i]; different |= a[i] ^ b[i];
} }
return different != 0; return different != 0;
} }
/* Derive all Switch keys. */ /* Derive all Switch keys. */
void derive_nx_keydata(u32 target_firmware) { void derive_nx_keydata(uint32_t target_firmware, const nx_keyblob_t *available_keyblobs, uint32_t available_revision, const void *tsec_fw, size_t tsec_fw_size) {
u8 work_buffer[0x10]; uint8_t work_buffer[0x10];
nx_keyblob_t keyblob; nx_keyblob_t keyblob;
/* TODO: Set keyslot flags properly in preparation of derivation. */ /* TODO: Set keyslot flags properly in preparation of derivation. */
set_aes_keyslot_flags(0xE, 0x15); set_aes_keyslot_flags(0xE, 0x15);
set_aes_keyslot_flags(0xD, 0x15); set_aes_keyslot_flags(0xD, 0x15);
/* Set TSEC key. */ /* Set TSEC key. */
get_tsec_key(work_buffer); get_tsec_key(work_buffer, tsec_fw, tsec_fw_size);
set_aes_keyslot(0xD, work_buffer, 0x10); set_aes_keyslot(0xD, work_buffer, 0x10);
/* Get keyblob, always try to set up the highest possible master key. */ /* Get keyblob, always try to set up the highest possible master key. */
/* TODO: Should we iterate, trying lower keys on failure? */ /* TODO: Should we iterate, trying lower keys on failure? */
get_keyblob(&keyblob, MASTERKEY_REVISION_500_CURRENT); get_keyblob(&keyblob, MASTERKEY_REVISION_500_CURRENT, available_keyblobs, available_revision);
/* Derive both keyblob key 1, and keyblob key latest. */ /* Derive both keyblob key 1, and keyblob key latest. */
se_aes_ecb_decrypt_block(0xD, work_buffer, 0x10, keyblob_seeds[MASTERKEY_REVISION_100_230], 0x10); se_aes_ecb_decrypt_block(0xD, work_buffer, 0x10, keyblob_seeds[MASTERKEY_REVISION_100_230], 0x10);
decrypt_data_into_keyslot(0xF, 0xE, work_buffer, 0x10); decrypt_data_into_keyslot(0xF, 0xE, work_buffer, 0x10);
se_aes_ecb_decrypt_block(0xD, work_buffer, 0x10, keyblob_seeds[MASTERKEY_REVISION_500_CURRENT], 0x10); se_aes_ecb_decrypt_block(0xD, work_buffer, 0x10, keyblob_seeds[MASTERKEY_REVISION_500_CURRENT], 0x10);
decrypt_data_into_keyslot(0xD, 0xE, work_buffer, 0x10); decrypt_data_into_keyslot(0xD, 0xE, work_buffer, 0x10);
/* Clear the SBK. */ /* Clear the SBK. */
clear_aes_keyslot(0xE); clear_aes_keyslot(0xE);
se_aes_ecb_decrypt_block(0xD, work_buffer, 0x10, keyblob_mac_seed, 0x10); se_aes_ecb_decrypt_block(0xD, work_buffer, 0x10, keyblob_mac_seed, 0x10);
decrypt_data_into_keyslot(0xB, 0xD, keyblob_mac_seed, 0x10); decrypt_data_into_keyslot(0xB, 0xD, keyblob_mac_seed, 0x10);
/* Validate keyblob. */ /* Validate keyblob. */
se_compute_aes_128_cmac(0xB, work_buffer, 0x10, keyblob.mac + sizeof(keyblob.mac), sizeof(keyblob) - sizeof(keyblob.mac)); se_compute_aes_128_cmac(0xB, work_buffer, 0x10, keyblob.mac + sizeof(keyblob.mac), sizeof(keyblob) - sizeof(keyblob.mac));
if (safe_memcmp(keyblob.mac, work_buffer, 0x10)) { if (safe_memcmp(keyblob.mac, work_buffer, 0x10)) {
generic_panic(); generic_panic();
} }
/* Decrypt keyblob. */ /* Decrypt keyblob. */
se_aes_ctr_crypt(0xD, keyblob.data, sizeof(keyblob.data), keyblob.data, sizeof(keyblob.data), keyblob.ctr, sizeof(keyblob.ctr)); se_aes_ctr_crypt(0xD, keyblob.data, sizeof(keyblob.data), keyblob.data, sizeof(keyblob.data), keyblob.ctr, sizeof(keyblob.ctr));
/* Get needed data. */ /* Get needed data. */
set_aes_keyslot(0xC, keyblob.keys[0], 0x10); set_aes_keyslot(0xC, keyblob.keys[0], 0x10);
/* We don't need the Package1 Key, but for reference: set_aes_keyslot(0xB, keyblob.keys[8], 0x10); */ /* We don't need the Package1 Key, but for reference: set_aes_keyslot(0xB, keyblob.keys[8], 0x10); */
/* Clear keyblob. */ /* Clear keyblob. */
memset(keyblob.data, 0, sizeof(keyblob.data)); memset(keyblob.data, 0, sizeof(keyblob.data));
/* Derive keys for Exosphere, lock critical keyslots. */ /* Derive keys for Exosphere, lock critical keyslots. */
switch (target_firmware) { switch (target_firmware) {
case EXOSPHERE_TARGET_FIRMWARE_100: case EXOSPHERE_TARGET_FIRMWARE_100:
case EXOSPHERE_TARGET_FIRMWARE_200: case EXOSPHERE_TARGET_FIRMWARE_200:
case EXOSPHERE_TARGET_FIRMWARE_300: case EXOSPHERE_TARGET_FIRMWARE_300:
decrypt_data_into_keyslot(0xD, 0xF, devicekey_seed, 0x10); decrypt_data_into_keyslot(0xD, 0xF, devicekey_seed, 0x10);
decrypt_data_into_keyslot(0xC, 0xC, masterkey_seed, 0x10); decrypt_data_into_keyslot(0xC, 0xC, masterkey_seed, 0x10);
break; break;
case EXOSPHERE_TARGET_FIRMWARE_400: case EXOSPHERE_TARGET_FIRMWARE_400:
decrypt_data_into_keyslot(0xD, 0xF, devicekey_4x_seed, 0x10); decrypt_data_into_keyslot(0xD, 0xF, devicekey_4x_seed, 0x10);
decrypt_data_into_keyslot(0xF, 0xF, devicekey_seed, 0x10); decrypt_data_into_keyslot(0xF, 0xF, devicekey_seed, 0x10);
decrypt_data_into_keyslot(0xE, 0xC, masterkey_4x_seed, 0x10); decrypt_data_into_keyslot(0xE, 0xC, masterkey_4x_seed, 0x10);
decrypt_data_into_keyslot(0xC, 0xC, masterkey_seed, 0x10); decrypt_data_into_keyslot(0xC, 0xC, masterkey_seed, 0x10);
break; break;
case EXOSPHERE_TARGET_FIRMWARE_500: case EXOSPHERE_TARGET_FIRMWARE_500:
decrypt_data_into_keyslot(0xA, 0xF, devicekey_4x_seed, 0x10); decrypt_data_into_keyslot(0xA, 0xF, devicekey_4x_seed, 0x10);
decrypt_data_into_keyslot(0xF, 0xF, devicekey_seed, 0x10); decrypt_data_into_keyslot(0xF, 0xF, devicekey_seed, 0x10);
decrypt_data_into_keyslot(0xE, 0xC, masterkey_4x_seed, 0x10); decrypt_data_into_keyslot(0xE, 0xC, masterkey_4x_seed, 0x10);
@ -126,46 +124,46 @@ void derive_nx_keydata(u32 target_firmware) {
default: default:
generic_panic(); generic_panic();
} }
/* Setup master key revision, derive older master keys for use. */ /* Setup master key revision, derive older master keys for use. */
mkey_detect_revision(); mkey_detect_revision();
} }
/* Sets final keyslot flags, for handover to TZ/Exosphere. Setting these will prevent the BPMP from using the device key or master key. */ /* Sets final keyslot flags, for handover to TZ/Exosphere. Setting these will prevent the BPMP from using the device key or master key. */
void finalize_nx_keydata(u32 target_firmware) { void finalize_nx_keydata(uint32_t target_firmware) {
set_aes_keyslot_flags(0xC, 0xFF); set_aes_keyslot_flags(0xC, 0xFF);
set_aes_keyslot_flags((target_firmware >= EXOSPHERE_TARGET_FIRMWARE_400) ? (KEYSLOT_SWITCH_4XOLDDEVICEKEY) : (KEYSLOT_SWITCH_DEVICEKEY), 0xFF); set_aes_keyslot_flags((target_firmware >= EXOSPHERE_TARGET_FIRMWARE_400) ? (KEYSLOT_SWITCH_4XOLDDEVICEKEY) : (KEYSLOT_SWITCH_DEVICEKEY), 0xFF);
} }
void fusee_generate_specific_aes_key(void *dst, const void *wrapped_key, bool should_mask, u32 target_firmware) { static void generate_specific_aes_key(void *dst, const void *wrapped_key, bool should_mask, uint32_t target_firmware) {
unsigned int keyslot = (target_firmware >= EXOSPHERE_TARGET_FIRMWARE_400) ? (KEYSLOT_SWITCH_4XOLDDEVICEKEY) : (KEYSLOT_SWITCH_DEVICEKEY); unsigned int keyslot = (target_firmware >= EXOSPHERE_TARGET_FIRMWARE_400) ? (KEYSLOT_SWITCH_4XOLDDEVICEKEY) : (KEYSLOT_SWITCH_DEVICEKEY);
if (fuse_get_bootrom_patch_version() < 0x7F) { if (fuse_get_bootrom_patch_version() < 0x7F) {
/* On dev units, use a fixed "all-zeroes" seed. */ /* On dev units, use a fixed "all-zeroes" seed. */
/* Yes, this data really is all-zero in actual TrustZone .rodata. */ /* Yes, this data really is all-zero in actual TrustZone .rodata. */
static const u8 dev_specific_aes_key_source[0x10] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; static const uint8_t dev_specific_aes_key_source[0x10] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
static const u8 dev_specific_aes_key_ctr[0x10] = {0x3C, 0xD5, 0x92, 0xEC, 0x68, 0x31, 0x4A, 0x06, 0xD4, 0x1B, 0x0C, 0xD9, 0xF6, 0x2E, 0xD9, 0xE9}; static const uint8_t dev_specific_aes_key_ctr[0x10] = {0x3C, 0xD5, 0x92, 0xEC, 0x68, 0x31, 0x4A, 0x06, 0xD4, 0x1B, 0x0C, 0xD9, 0xF6, 0x2E, 0xD9, 0xE9};
static const u8 dev_specific_aes_key_mask[0x10] = {0xAC, 0xCA, 0x9A, 0xCA, 0xFF, 0x2E, 0xB9, 0x22, 0xCC, 0x1F, 0x4F, 0xAD, 0xDD, 0x77, 0x21, 0x1E}; static const uint8_t dev_specific_aes_key_mask[0x10] = {0xAC, 0xCA, 0x9A, 0xCA, 0xFF, 0x2E, 0xB9, 0x22, 0xCC, 0x1F, 0x4F, 0xAD, 0xDD, 0x77, 0x21, 0x1E};
se_aes_ctr_crypt(keyslot, dst, 0x10, dev_specific_aes_key_source, 0x10, dev_specific_aes_key_ctr, 0x10); se_aes_ctr_crypt(keyslot, dst, 0x10, dev_specific_aes_key_source, 0x10, dev_specific_aes_key_ctr, 0x10);
if (should_mask) { if (should_mask) {
for (unsigned int i = 0; i < 0x10; i++) { for (unsigned int i = 0; i < 0x10; i++) {
((u8 *)dst)[i] ^= dev_specific_aes_key_mask[i]; ((uint8_t *)dst)[i] ^= dev_specific_aes_key_mask[i];
} }
} }
} else { } else {
/* On retail, standard kek->key decryption. */ /* On retail, standard kek->key decryption. */
static const u8 retail_specific_aes_key_source[0x10] = {0xE2, 0xD6, 0xB8, 0x7A, 0x11, 0x9C, 0xB8, 0x80, 0xE8, 0x22, 0x88, 0x8A, 0x46, 0xFB, 0xA1, 0x95}; static const uint8_t retail_specific_aes_key_source[0x10] = {0xE2, 0xD6, 0xB8, 0x7A, 0x11, 0x9C, 0xB8, 0x80, 0xE8, 0x22, 0x88, 0x8A, 0x46, 0xFB, 0xA1, 0x95};
decrypt_data_into_keyslot(KEYSLOT_SWITCH_TEMPKEY, keyslot, retail_specific_aes_key_source, 0x10); decrypt_data_into_keyslot(KEYSLOT_SWITCH_TEMPKEY, keyslot, retail_specific_aes_key_source, 0x10);
se_aes_ecb_decrypt_block(KEYSLOT_SWITCH_TEMPKEY, dst, 0x10, wrapped_key, 0x10); se_aes_ecb_decrypt_block(KEYSLOT_SWITCH_TEMPKEY, dst, 0x10, wrapped_key, 0x10);
} }
} }
void fusee_generate_personalized_aes_key_for_bis(void *dst, const void *wrapped_kek, const void *wrapped_key, u32 target_firmware) { static void generate_personalized_aes_key_for_bis(void *dst, const void *wrapped_kek, const void *wrapped_key, uint32_t target_firmware) {
static const u8 kek_source[0x10] = { static const uint8_t kek_source[0x10] = {
0x4D, 0x87, 0x09, 0x86, 0xC4, 0x5D, 0x20, 0x72, 0x2F, 0xBA, 0x10, 0x53, 0xDA, 0x92, 0xE8, 0xA9 0x4D, 0x87, 0x09, 0x86, 0xC4, 0x5D, 0x20, 0x72, 0x2F, 0xBA, 0x10, 0x53, 0xDA, 0x92, 0xE8, 0xA9
}; };
static const u8 key_source[0x10] = { static const uint8_t key_source[0x10] = {
0x89, 0x61, 0x5E, 0xE0, 0x5C, 0x31, 0xB6, 0x80, 0x5F, 0xE5, 0x8F, 0x3D, 0xA2, 0x4F, 0x7A, 0xA8 0x89, 0x61, 0x5E, 0xE0, 0x5C, 0x31, 0xB6, 0x80, 0x5F, 0xE5, 0x8F, 0x3D, 0xA2, 0x4F, 0x7A, 0xA8
}; };
@ -178,8 +176,8 @@ void fusee_generate_personalized_aes_key_for_bis(void *dst, const void *wrapped_
se_aes_ecb_decrypt_block(KEYSLOT_SWITCH_TEMPKEY, dst, 0x10, wrapped_key, 0x10); se_aes_ecb_decrypt_block(KEYSLOT_SWITCH_TEMPKEY, dst, 0x10, wrapped_key, 0x10);
} }
void derive_bis_key(void *dst, BisPartition_t partition_id, u32 target_firmware) { void derive_bis_key(void *dst, BisPartition partition_id, uint32_t target_firmware) {
static const u8 key_source_for_bis[3][2][0x10] = { static const uint8_t key_source_for_bis[3][2][0x10] = {
{ {
{0xF8, 0x3F, 0x38, 0x6E, 0x2C, 0xD2, 0xCA, 0x32, 0xA8, 0x9A, 0xB9, 0xAA, 0x29, 0xBF, 0xC7, 0x48}, {0xF8, 0x3F, 0x38, 0x6E, 0x2C, 0xD2, 0xCA, 0x32, 0xA8, 0x9A, 0xB9, 0xAA, 0x29, 0xBF, 0xC7, 0x48},
{0x7D, 0x92, 0xB0, 0x3A, 0xA8, 0xBF, 0xDE, 0xE1, 0xA7, 0x4C, 0x3B, 0x6E, 0x35, 0xCB, 0x71, 0x06} {0x7D, 0x92, 0xB0, 0x3A, 0xA8, 0xBF, 0xDE, 0xE1, 0xA7, 0x4C, 0x3B, 0x6E, 0x35, 0xCB, 0x71, 0x06}
@ -193,19 +191,19 @@ void derive_bis_key(void *dst, BisPartition_t partition_id, u32 target_firmware)
{0x4D, 0x12, 0xE1, 0x4B, 0x2A, 0x47, 0x4C, 0x1C, 0x09, 0xCB, 0x03, 0x59, 0xF0, 0x15, 0xF4, 0xE4} {0x4D, 0x12, 0xE1, 0x4B, 0x2A, 0x47, 0x4C, 0x1C, 0x09, 0xCB, 0x03, 0x59, 0xF0, 0x15, 0xF4, 0xE4}
} }
}; };
static const u8 bis_kek_source[0x10] = {0x34, 0xC1, 0xA0, 0xC4, 0x82, 0x58, 0xF8, 0xB4, 0xFA, 0x9E, 0x5E, 0x6A, 0xDA, 0xFC, 0x7E, 0x4F}; static const uint8_t bis_kek_source[0x10] = {0x34, 0xC1, 0xA0, 0xC4, 0x82, 0x58, 0xF8, 0xB4, 0xFA, 0x9E, 0x5E, 0x6A, 0xDA, 0xFC, 0x7E, 0x4F};
switch (partition_id) { switch (partition_id) {
case BisPartition_Calibration: case BisPartition_Calibration:
fusee_generate_specific_aes_key(dst, key_source_for_bis[partition_id][0], false, target_firmware); generate_specific_aes_key(dst, key_source_for_bis[partition_id][0], false, target_firmware);
fusee_generate_specific_aes_key(dst + 0x10, key_source_for_bis[partition_id][1], false, target_firmware); generate_specific_aes_key(dst + 0x10, key_source_for_bis[partition_id][1], false, target_firmware);
break; break;
case BisPartition_Safe: case BisPartition_Safe:
case BisPartition_UserSystem: case BisPartition_UserSystem:
fusee_generate_personalized_aes_key_for_bis(dst, bis_kek_source, key_source_for_bis[partition_id][0], target_firmware); generate_personalized_aes_key_for_bis(dst, bis_kek_source, key_source_for_bis[partition_id][0], target_firmware);
fusee_generate_personalized_aes_key_for_bis(dst + 0x10, bis_kek_source, key_source_for_bis[partition_id][1], target_firmware); generate_personalized_aes_key_for_bis(dst + 0x10, bis_kek_source, key_source_for_bis[partition_id][1], target_firmware);
break; break;
default: default:
generic_panic(); generic_panic();
} }
} }

View file

@ -1,7 +1,9 @@
#ifndef FUSEE_KEYDERIVATION_H #ifndef FUSEE_KEYDERIVATION_H
#define FUSEE_KEYDERIVATION_H #define FUSEE_KEYDERIVATION_H
#include "hwinit/tsec.h" #include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
/* TODO: Update to 0x6 on release of new master key. */ /* TODO: Update to 0x6 on release of new master key. */
#define MASTERKEY_REVISION_MAX 0x5 #define MASTERKEY_REVISION_MAX 0x5
@ -14,24 +16,24 @@
#define MASTERKEY_NUM_NEW_DEVICE_KEYS (MASTERKEY_REVISION_MAX - MASTERKEY_REVISION_400_410) #define MASTERKEY_NUM_NEW_DEVICE_KEYS (MASTERKEY_REVISION_MAX - MASTERKEY_REVISION_400_410)
typedef enum { typedef enum BisPartition {
BisPartition_Calibration = 0, BisPartition_Calibration = 0,
BisPartition_Safe = 1, BisPartition_Safe = 1,
BisPartition_UserSystem = 2 BisPartition_UserSystem = 2,
} BisPartition_t; } BisPartition;
typedef struct { typedef struct nx_keyblob_t {
u8 mac[0x10]; uint8_t mac[0x10];
u8 ctr[0x10]; uint8_t ctr[0x10];
union { union {
u8 data[0x90]; uint8_t data[0x90];
u8 keys[9][0x10]; uint8_t keys[9][0x10];
}; };
} nx_keyblob_t; } nx_keyblob_t;
void derive_nx_keydata(u32 target_firmware); void derive_nx_keydata(uint32_t target_firmware, const nx_keyblob_t *available_keyblobs, uint32_t available_revision, const void *tsec_fw, size_t tsec_fw_size);
void finalize_nx_keydata(u32 target_firmware); void finalize_nx_keydata(uint32_t target_firmware);
void derive_bis_key(void *dst, BisPartition_t partition_id, u32 target_firmware); void derive_bis_key(void *dst, BisPartition partition_id, uint32_t target_firmware);
#endif #endif

View file

@ -117,7 +117,7 @@ void nxboot_main(void) {
nxboot_configure_exosphere(); nxboot_configure_exosphere();
/* Derive keydata. */ /* Derive keydata. */
derive_nx_keydata(MAILBOX_EXOSPHERE_CONFIGURATION->target_firmware); //derive_nx_keydata(MAILBOX_EXOSPHERE_CONFIGURATION->target_firmware);
if (loader_ctx->package2_loadfile.load_address == 0) { if (loader_ctx->package2_loadfile.load_address == 0) {
if (init_bcpkg2_device() == -1) { if (init_bcpkg2_device() == -1) {

View file

@ -3,13 +3,13 @@
#include "package1.h" #include "package1.h"
#include "bct.h" #include "bct.h"
int package1_parse_boot0(void **package1, size_t *package1_size, nx_keyblob_t *keyblobs, uint32_t *version, FILE *boot0) { int package1_parse_boot0(void **package1, size_t *package1_size, nx_keyblob_t *keyblobs, uint32_t *revision, FILE *boot0) {
static nvboot_config_table bct = {0}; /* Normal firmware BCT, primary. TODO: check? */ static nvboot_config_table bct = {0}; /* Normal firmware BCT, primary. TODO: check? */
nv_bootloader_info *pk1_info = &bct.bootloader[0]; /* TODO: check? */ nv_bootloader_info *pk1_info = &bct.bootloader[0]; /* TODO: check? */
size_t fpos, pk1_offset; size_t fpos, pk1_offset;
if (package1 == NULL || package1_size != NULL || keyblobs == NULL || version == NULL || boot0 == NULL) { if (package1 == NULL || package1_size != NULL || keyblobs == NULL || revision == NULL || boot0 == NULL) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
@ -25,7 +25,7 @@ int package1_parse_boot0(void **package1, size_t *package1_size, nx_keyblob_t *k
return -1; return -1;
} }
*version = pk1_info->attribute; *revision = pk1_info->attribute;
*package1_size = pk1_info->length; *package1_size = pk1_info->length;
pk1_offset = 0x4000 * pk1_info->start_blk + 0x200 * pk1_info->start_page; pk1_offset = 0x4000 * pk1_info->start_blk + 0x200 * pk1_info->start_page;

View file

@ -4,6 +4,6 @@
#include <stdio.h> #include <stdio.h>
#include "key_derivation.h" #include "key_derivation.h"
int package1_parse_boot0(void **package1, size_t *package1_size, nx_keyblob_t *keyblobs, uint32_t *version, FILE *boot0); int package1_parse_boot0(void **package1, size_t *package1_size, nx_keyblob_t *keyblobs, uint32_t *revision, FILE *boot0);
#endif #endif