From 2d8812ac6e529ebbedd801cadfc03b126adc499c Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sat, 31 Mar 2018 15:36:54 -0600 Subject: [PATCH] Implement BIS Key derivation. --- fusee/src/key_derivation.c | 88 +++++++++++++++++++++++++++++++++++--- fusee/src/key_derivation.h | 9 ++++ fusee/src/se.c | 4 +- fusee/src/utils.h | 1 + 4 files changed, 94 insertions(+), 8 deletions(-) diff --git a/fusee/src/key_derivation.c b/fusee/src/key_derivation.c index 27b7a38ed..1ff61678c 100644 --- a/fusee/src/key_derivation.c +++ b/fusee/src/key_derivation.c @@ -1,6 +1,7 @@ #include "key_derivation.h" #include "se.h" #include "exocfg.h" +#include "fuse.h" static const u8 keyblob_seeds[MASTERKEY_REVISION_MAX][0x10] = { @@ -82,6 +83,7 @@ void derive_nx_keydata(u32 target_firmware) { /* Clear the SBK. */ clear_aes_keyslot(0xE); + se_aes_ecb_decrypt_block(0xD, work_buffer, 0x10, keyblob_mac_seed, 0x10); decrypt_data_into_keyslot(0xB, 0xD, keyblob_mac_seed, 0x10); /* Validate keyblob. */ @@ -107,24 +109,98 @@ void derive_nx_keydata(u32 target_firmware) { case EXOSPHERE_TARGET_FIRMWARE_300: decrypt_data_into_keyslot(0xD, 0xF, devicekey_seed, 0x10); decrypt_data_into_keyslot(0xC, 0xC, masterkey_seed, 0x10); - set_aes_keyslot_flags(0xC, 0xFF); - set_aes_keyslot_flags(0xD, 0xFF); break; case EXOSPHERE_TARGET_FIRMWARE_400: decrypt_data_into_keyslot(0xD, 0xF, devicekey_4x_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(0xC, 0xC, masterkey_seed, 0x10); - set_aes_keyslot_flags(0xC, 0xFF); - set_aes_keyslot_flags(0xF, 0xFF); break; case EXOSPHERE_TARGET_FIRMWARE_500: 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); decrypt_data_into_keyslot(0xC, 0xC, masterkey_seed, 0x10); - set_aes_keyslot_flags(0xC, 0xFF); - set_aes_keyslot_flags(0xF, 0xFF); + break; + default: + generic_panic(); + } +} + +/* 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) { + set_aes_keyslot_flags(0xC, 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) { + unsigned int keyslot = (target_firmware >= EXOSPHERE_TARGET_FIRMWARE_400) ? (KEYSLOT_SWITCH_4XOLDDEVICEKEY) : (KEYSLOT_SWITCH_DEVICEKEY); + if (fuse_get_bootrom_patch_version() < 0x7F) { + /* On dev units, use a fixed "all-zeroes" seed. */ + /* 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 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 u8 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); + + if (should_mask) { + for (unsigned int i = 0; i < 0x10; i++) { + ((u8 *)dst)[i] ^= dev_specific_aes_key_mask[i]; + } + } + } else { + /* 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}; + 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); + } +} + +void fusee_generate_personalized_aes_key_for_bis(void *dst, const void *wrapped_kek, const void *wrapped_key, u32 target_firmware) { + static const u8 kek_source[0x10] = { + 0x4D, 0x87, 0x09, 0x86, 0xC4, 0x5D, 0x20, 0x72, 0x2F, 0xBA, 0x10, 0x53, 0xDA, 0x92, 0xE8, 0xA9 + }; + static const u8 key_source[0x10] = { + 0x89, 0x61, 0x5E, 0xE0, 0x5C, 0x31, 0xB6, 0x80, 0x5F, 0xE5, 0x8F, 0x3D, 0xA2, 0x4F, 0x7A, 0xA8 + }; + + unsigned int keyslot = (target_firmware >= EXOSPHERE_TARGET_FIRMWARE_400) ? (KEYSLOT_SWITCH_4XOLDDEVICEKEY) : (KEYSLOT_SWITCH_DEVICEKEY); + /* Derive kek. */ + decrypt_data_into_keyslot(KEYSLOT_SWITCH_TEMPKEY, keyslot, kek_source, 0x10); + decrypt_data_into_keyslot(KEYSLOT_SWITCH_TEMPKEY, KEYSLOT_SWITCH_TEMPKEY, wrapped_kek, 0x10); + /* Derive key. */ + decrypt_data_into_keyslot(KEYSLOT_SWITCH_TEMPKEY, KEYSLOT_SWITCH_TEMPKEY, key_source, 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) { + static const u8 key_source_for_bis[3][2][0x10] = { + { + {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} + }, + { + {0x41, 0x00, 0x30, 0x49, 0xDD, 0xCC, 0xC0, 0x65, 0x64, 0x7A, 0x7E, 0xB4, 0x1E, 0xED, 0x9C, 0x5F}, + {0x44, 0x42, 0x4E, 0xDA, 0xB4, 0x9D, 0xFC, 0xD9, 0x87, 0x77, 0x24, 0x9A, 0xDC, 0x9F, 0x7C, 0xA4} + }, + { + {0x52, 0xC2, 0xE9, 0xEB, 0x09, 0xE3, 0xEE, 0x29, 0x32, 0xA1, 0x0C, 0x1F, 0xB6, 0xA0, 0x92, 0x6C}, + {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}; + + switch (partition_id) { + case BisPartition_Calibration: + fusee_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); + break; + case BisPartition_User: + case BisPartition_System: + fusee_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); break; default: generic_panic(); diff --git a/fusee/src/key_derivation.h b/fusee/src/key_derivation.h index 0e885371f..d00ee2f32 100644 --- a/fusee/src/key_derivation.h +++ b/fusee/src/key_derivation.h @@ -14,6 +14,12 @@ #define MASTERKEY_NUM_NEW_DEVICE_KEYS (MASTERKEY_REVISION_MAX - MASTERKEY_REVISION_400_410) +typedef enum { + BisPartition_Calibration = 0, + BisPartition_User = 1, + BisPartition_System = 2 +} BisPartition_t; + typedef struct { u8 mac[0x10]; u8 ctr[0x10]; @@ -24,5 +30,8 @@ typedef struct { } nx_keyblob_t; void derive_nx_keydata(u32 target_firmware); +void finalize_nx_keydata(u32 target_firmware); + +void derive_bis_key(void *dst, BisPartition_t partition_id, u32 target_firmware); #endif \ No newline at end of file diff --git a/fusee/src/se.c b/fusee/src/se.c index 24288f0e9..100dd7217 100644 --- a/fusee/src/se.c +++ b/fusee/src/se.c @@ -11,7 +11,7 @@ static unsigned int g_se_modulus_sizes[KEYSLOT_RSA_MAX]; static unsigned int g_se_exp_sizes[KEYSLOT_RSA_MAX]; /* Initialize a SE linked list. */ -void ll_init(volatile se_ll_t *ll, void *buffer, size_t size) { +void NOINLINE ll_init(volatile se_ll_t *ll, void *buffer, size_t size) { ll->num_entries = 0; /* 1 Entry. */ if (buffer != NULL) { @@ -165,7 +165,7 @@ void set_se_ctr(const void *ctr) { } void decrypt_data_into_keyslot(unsigned int keyslot_dst, unsigned int keyslot_src, const void *wrapped_key, size_t wrapped_key_size) { - if (keyslot_dst >= KEYSLOT_AES_MAX || keyslot_src >= KEYSIZE_AES_MAX || wrapped_key_size > KEYSIZE_AES_MAX) { + if (keyslot_dst >= KEYSLOT_AES_MAX || keyslot_src >= KEYSLOT_AES_MAX || wrapped_key_size > KEYSIZE_AES_MAX) { generic_panic(); } diff --git a/fusee/src/utils.h b/fusee/src/utils.h index f2a767cf3..5a288717d 100644 --- a/fusee/src/utils.h +++ b/fusee/src/utils.h @@ -19,6 +19,7 @@ #define PACKED __attribute__((packed)) #define ALINLINE __attribute__((always_inline)) +#define NOINLINE __attribute__((noinline)) #define SET_SYSREG(reg, val) do { temp_reg = (val); __asm__ __volatile__ ("msr " #reg ", %0" :: "r"(temp_reg) : "memory"); } while(false)