diff --git a/fusee/fusee-secondary/src/key_derivation.c b/fusee/fusee-secondary/src/key_derivation.c index bf9688423..e67d3dfbd 100644 --- a/fusee/fusee-secondary/src/key_derivation.c +++ b/fusee/fusee-secondary/src/key_derivation.c @@ -25,11 +25,12 @@ #define AL16 ALIGN(16) static const uint8_t AL16 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. */ - {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. */ - {0x2D, 0x1F, 0x48, 0x80, 0xED, 0xEC, 0xED, 0x3E, 0x3C, 0xF2, 0x48, 0xB5, 0x65, 0x7D, 0xF7, 0xBE}, /* Keyblob seed 03. */ - {0xBB, 0x5A, 0x01, 0xF9, 0x88, 0xAF, 0xF5, 0xFC, 0x6C, 0xFF, 0x07, 0x9E, 0x13, 0x3C, 0x39, 0x80}, /* Keyblob seed 04. */ + {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. */ + {0x33, 0x76, 0x85, 0xEE, 0x88, 0x4A, 0xAE, 0x0A, 0xC2, 0x8A, 0xFD, 0x7D, 0x63, 0xC0, 0x43, 0x3B}, /* Keyblob seed 02. */ + {0x2D, 0x1F, 0x48, 0x80, 0xED, 0xEC, 0xED, 0x3E, 0x3C, 0xF2, 0x48, 0xB5, 0x65, 0x7D, 0xF7, 0xBE}, /* Keyblob seed 03. */ + {0xBB, 0x5A, 0x01, 0xF9, 0x88, 0xAF, 0xF5, 0xFC, 0x6C, 0xFF, 0x07, 0x9E, 0x13, 0x3C, 0x39, 0x80}, /* Keyblob seed 04. */ + {0xD8, 0xCC, 0xE1, 0x26, 0x6A, 0x35, 0x3F, 0xCC, 0x20, 0xF3, 0x2D, 0x3B, 0x51, 0x7D, 0xE9, 0xC0} /* Keyblob seed 05. */ }; static const uint8_t AL16 keyblob_mac_seed[0x10] = { @@ -107,7 +108,7 @@ static int decrypt_keyblob(const nx_keyblob_t *keyblobs, uint32_t revision, uint } int load_package1_key(uint32_t revision) { - if (revision > MASTERKEY_REVISION_500_CURRENT) { + if (revision > MASTERKEY_REVISION_600_CURRENT) { return -1; } @@ -141,7 +142,7 @@ int derive_nx_keydata(uint32_t target_firmware, const nx_keyblob_t *keyblobs, ui clear_aes_keyslot(0xE); /* Get needed data. */ - set_aes_keyslot(0xC, g_dec_keyblobs[MASTERKEY_REVISION_500_CURRENT].keys[0], 0x10); + set_aes_keyslot(0xC, g_dec_keyblobs[MASTERKEY_REVISION_600_CURRENT].keys[0], 0x10); /* Also set the Package1 key for the revision that is stored on the eMMC boot0 partition. */ load_package1_key(available_revision); @@ -161,6 +162,7 @@ int derive_nx_keydata(uint32_t target_firmware, const nx_keyblob_t *keyblobs, ui decrypt_data_into_keyslot(0xC, 0xC, masterkey_seed, 0x10); break; case EXOSPHERE_TARGET_FIRMWARE_500: + case EXOSPHERE_TARGET_FIRMWARE_600: decrypt_data_into_keyslot(0xA, 0xF, devicekey_4x_seed, 0x10); decrypt_data_into_keyslot(0xF, 0xF, devicekey_seed, 0x10); decrypt_data_into_keyslot(0xE, 0xC, masterkey_4x_seed, 0x10); @@ -171,7 +173,7 @@ int derive_nx_keydata(uint32_t target_firmware, const nx_keyblob_t *keyblobs, ui } /* Setup master key revision, derive older master keys for use. */ - return mkey_detect_revision(); + return mkey_detect_revision(fuse_get_retail_type() != 0); } /* Sets final keyslot flags, for handover to TZ/Exosphere. Setting these will prevent the BPMP from using the device key or master key. */ diff --git a/fusee/fusee-secondary/src/key_derivation.h b/fusee/fusee-secondary/src/key_derivation.h index f55b4c66e..5b64f76d1 100644 --- a/fusee/fusee-secondary/src/key_derivation.h +++ b/fusee/fusee-secondary/src/key_derivation.h @@ -20,7 +20,6 @@ #include #include #include -#include "masterkey.h" typedef enum BisPartition { BisPartition_Calibration = 0, @@ -41,11 +40,9 @@ typedef struct nx_keyblob_t { }; } nx_keyblob_t; -/* TSEC fw must be 0x100-byte-aligned. */ int derive_nx_keydata(uint32_t target_firmware, const nx_keyblob_t *keyblobs, uint32_t available_revision, const void *tsec_fw, size_t tsec_fw_size); int load_package1_key(uint32_t revision); void finalize_nx_keydata(uint32_t target_firmware); - void derive_bis_key(void *dst, BisPartition partition_id, uint32_t target_firmware); #endif diff --git a/fusee/fusee-secondary/src/masterkey.c b/fusee/fusee-secondary/src/masterkey.c index 6d738f23d..bf31ec581 100644 --- a/fusee/fusee-secondary/src/masterkey.c +++ b/fusee/fusee-secondary/src/masterkey.c @@ -26,8 +26,21 @@ static unsigned int g_mkey_revision = 0; static bool g_determined_mkey_revision = false; static uint8_t g_old_masterkeys[MASTERKEY_REVISION_MAX][0x10]; +static uint8_t g_old_devicekeys[MASTERKEY_NUM_NEW_DEVICE_KEYS - 1][0x10]; /* TODO: Extend with new vectors, as needed. */ +/* Dev unit keys. */ +static const uint8_t mkey_vectors_dev[MASTERKEY_REVISION_MAX][0x10] = +{ + {0x46, 0x22, 0xB4, 0x51, 0x9A, 0x7E, 0xA7, 0x7F, 0x62, 0xA1, 0x1F, 0x8F, 0xC5, 0x3A, 0xDB, 0xFE}, /* Zeroes encrypted with Master Key 00. */ + {0x39, 0x33, 0xF9, 0x31, 0xBA, 0xE4, 0xA7, 0x21, 0x2C, 0xDD, 0xB7, 0xD8, 0xB4, 0x4E, 0x37, 0x23}, /* Master key 00 encrypted with Master key 01. */ + {0x97, 0x29, 0xB0, 0x32, 0x43, 0x14, 0x8C, 0xA6, 0x85, 0xE9, 0x5A, 0x94, 0x99, 0x39, 0xAC, 0x5D}, /* Master key 01 encrypted with Master key 02. */ + {0x2C, 0xCA, 0x9C, 0x31, 0x1E, 0x07, 0xB0, 0x02, 0x97, 0x0A, 0xD8, 0x03, 0xA2, 0x76, 0x3F, 0xA3}, /* Master key 02 encrypted with Master key 03. */ + {0x9B, 0x84, 0x76, 0x14, 0x72, 0x94, 0x52, 0xCB, 0x54, 0x92, 0x9B, 0xC4, 0x8C, 0x5B, 0x0F, 0xBA}, /* Master key 03 encrypted with Master key 04. */ + {0x78, 0xD5, 0xF1, 0x20, 0x3D, 0x16, 0xE9, 0x30, 0x32, 0x27, 0x34, 0x6F, 0xCF, 0xE0, 0x27, 0xDC}, /* Master key 04 encrypted with Master key 05. */ +}; + +/* Retail unit keys. */ static const uint8_t mkey_vectors[MASTERKEY_REVISION_MAX][0x10] = { {0x0C, 0xF0, 0x59, 0xAC, 0x85, 0xF6, 0x26, 0x65, 0xE1, 0xE9, 0x19, 0x55, 0xE6, 0xF2, 0x67, 0x3D}, /* Zeroes encrypted with Master Key 00. */ @@ -35,16 +48,17 @@ static const uint8_t mkey_vectors[MASTERKEY_REVISION_MAX][0x10] = {0xDE, 0xCF, 0xEB, 0xEB, 0x10, 0xAE, 0x74, 0xD8, 0xAD, 0x7C, 0xF4, 0x9E, 0x62, 0xE0, 0xE8, 0x72}, /* Master key 01 encrypted with Master key 02. */ {0x0A, 0x0D, 0xDF, 0x34, 0x22, 0x06, 0x6C, 0xA4, 0xE6, 0xB1, 0xEC, 0x71, 0x85, 0xCA, 0x4E, 0x07}, /* Master key 02 encrypted with Master key 03. */ {0x6E, 0x7D, 0x2D, 0xC3, 0x0F, 0x59, 0xC8, 0xFA, 0x87, 0xA8, 0x2E, 0xD5, 0x89, 0x5E, 0xF3, 0xE9}, /* Master key 03 encrypted with Master key 04. */ + {0xEB, 0xF5, 0x6F, 0x83, 0x61, 0x9E, 0xF8, 0xFA, 0xE0, 0x87, 0xD7, 0xA1, 0x4E, 0x25, 0x36, 0xEE}, /* Master key 04 encrypted with Master key 05. */ }; -bool check_mkey_revision(unsigned int revision) { +static bool check_mkey_revision(unsigned int revision, bool is_retail) { uint8_t final_vector[0x10]; unsigned int check_keyslot = KEYSLOT_SWITCH_MASTERKEY; if (revision > 0) { /* Generate old master key array. */ for (unsigned int i = revision; i > 0; i--) { - se_aes_ecb_decrypt_block(check_keyslot, g_old_masterkeys[i-1], 0x10, mkey_vectors[i], 0x10); + se_aes_ecb_decrypt_block(check_keyslot, g_old_masterkeys[i-1], 0x10, is_retail ? mkey_vectors[i] : mkey_vectors_dev[i], 0x10); set_aes_keyslot(KEYSLOT_SWITCH_TEMPKEY, g_old_masterkeys[i-1], 0x10); check_keyslot = KEYSLOT_SWITCH_TEMPKEY; } @@ -59,13 +73,13 @@ bool check_mkey_revision(unsigned int revision) { return true; } -int mkey_detect_revision(void) { +int mkey_detect_revision(bool is_retail) { if (g_determined_mkey_revision) { generic_panic(); } for (unsigned int rev = 0; rev < MASTERKEY_REVISION_MAX; rev++) { - if (check_mkey_revision(rev)) { + if (check_mkey_revision(rev, is_retail)) { g_determined_mkey_revision = true; g_mkey_revision = rev; break; @@ -105,3 +119,33 @@ unsigned int mkey_get_keyslot(unsigned int revision) { return KEYSLOT_SWITCH_TEMPKEY; } } + +void set_old_devkey(unsigned int revision, const uint8_t *key) { + if (revision < MASTERKEY_REVISION_400_410 || MASTERKEY_REVISION_600_CURRENT <= revision) { + generic_panic(); + } + + memcpy(g_old_devicekeys[revision - MASTERKEY_REVISION_400_410], key, 0x10); +} + +unsigned int devkey_get_keyslot(unsigned int revision) { + if (!g_determined_mkey_revision || revision >= MASTERKEY_REVISION_MAX) { + generic_panic(); + } + + if (revision > g_mkey_revision) { + generic_panic(); + } + + if (revision >= 1) { + if (revision == MASTERKEY_REVISION_600_CURRENT) { + return KEYSLOT_SWITCH_DEVICEKEY; + } else { + /* Load into a temp keyslot. */ + set_aes_keyslot(KEYSLOT_SWITCH_TEMPKEY, g_old_devicekeys[revision - MASTERKEY_REVISION_400_410], 0x10); + return KEYSLOT_SWITCH_TEMPKEY; + } + } else { + return KEYSLOT_SWITCH_4XOLDDEVICEKEY; + } +} \ No newline at end of file diff --git a/fusee/fusee-secondary/src/masterkey.h b/fusee/fusee-secondary/src/masterkey.h index a491b23ef..531eebc58 100644 --- a/fusee/fusee-secondary/src/masterkey.h +++ b/fusee/fusee-secondary/src/masterkey.h @@ -19,22 +19,24 @@ /* This is glue code to enable master key support across versions. */ -/* TODO: Update to 0x6 on release of new master key. */ -#define MASTERKEY_REVISION_MAX 0x5 +/* TODO: Update to 0x7 on release of new master key. */ +#define MASTERKEY_REVISION_MAX 0x6 #define MASTERKEY_REVISION_100_230 0x00 #define MASTERKEY_REVISION_300 0x01 #define MASTERKEY_REVISION_301_302 0x02 #define MASTERKEY_REVISION_400_410 0x03 -#define MASTERKEY_REVISION_500_CURRENT 0x04 +#define MASTERKEY_REVISION_500_510 0x04 +#define MASTERKEY_REVISION_600_CURRENT 0x05 #define MASTERKEY_NUM_NEW_DEVICE_KEYS (MASTERKEY_REVISION_MAX - MASTERKEY_REVISION_400_410) -/* This should be called during initialization. */ -int mkey_detect_revision(void); +/* This should be called early on in initialization. */ +int mkey_detect_revision(bool is_retail); unsigned int mkey_get_revision(void); - unsigned int mkey_get_keyslot(unsigned int revision); +void set_old_devkey(unsigned int revision, const uint8_t *key); +unsigned int devkey_get_keyslot(unsigned int revision); -#endif +#endif \ No newline at end of file