From 06abe5a00429deb7a0a3053b806fbd8f7090edd4 Mon Sep 17 00:00:00 2001 From: Aurora Date: Sun, 2 Oct 2016 17:18:49 +0200 Subject: [PATCH] Add support for payload_stage1.bin.sha and payload_stage2.bin.sha to verify stages integrity (if either file is missing, a button sequence must be entered) --- source/installer.c | 61 +++++++++++++++++++++++++++++++--------------- source/utils.c | 15 ++++++++++++ source/utils.h | 1 + 3 files changed, 58 insertions(+), 19 deletions(-) diff --git a/source/installer.c b/source/installer.c index 72b96b5..5b4a8a1 100755 --- a/source/installer.c +++ b/source/installer.c @@ -13,27 +13,27 @@ #include "fatfs/sdmmc/sdmmc.h" #include "../build/bundled.h" -static const u8 sectorHash[0x20] = { +static const u8 sectorHash[SHA_256_HASH_SIZE] = { 0x82, 0xF2, 0x73, 0x0D, 0x2C, 0x2D, 0xA3, 0xF3, 0x01, 0x65, 0xF9, 0x87, 0xFD, 0xCC, 0xAC, 0x5C, 0xBA, 0xB2, 0x4B, 0x4E, 0x5F, 0x65, 0xC9, 0x81, 0xCD, 0x7B, 0xE6, 0xF4, 0x38, 0xE6, 0xD9, 0xD3 }; -static const u8 firm0Hash[0x20] = { +static const u8 firm0Hash[SHA_256_HASH_SIZE] = { 0x6E, 0x4D, 0x14, 0xAD, 0x51, 0x50, 0xA5, 0x9A, 0x87, 0x59, 0x62, 0xB7, 0x09, 0x0A, 0x3C, 0x74, 0x4F, 0x72, 0x4B, 0xBD, 0x97, 0x39, 0x33, 0xF2, 0x11, 0xC9, 0x35, 0x22, 0xC8, 0xBB, 0x1C, 0x7D }; -static const u8 firm0A9lhHash[0x20] = { +static const u8 firm0A9lhHash[SHA_256_HASH_SIZE] = { 0x79, 0x3D, 0x35, 0x7B, 0x8F, 0xF1, 0xFC, 0xF0, 0x8F, 0xB6, 0xDB, 0x51, 0x31, 0xD4, 0xA7, 0x74, 0x8E, 0xF0, 0x4A, 0xB1, 0xA6, 0x7F, 0xCD, 0xAB, 0x0C, 0x0A, 0xC0, 0x69, 0xA7, 0x9D, 0xC5, 0x04 }; -static const u8 firm0100Hash[0x20] = { +static const u8 firm0100Hash[SHA_256_HASH_SIZE] = { 0xD8, 0x2D, 0xB7, 0xB4, 0x38, 0x2B, 0x07, 0x88, 0x99, 0x77, 0x91, 0x0C, 0xC6, 0xEC, 0x6D, 0x87, 0x7D, 0x21, 0x79, 0x23, 0xD7, 0x60, 0xAF, 0x4E, 0x8B, 0x3A, 0xAB, 0xB2, 0x63, 0xE4, 0x21, 0xC6 }; -static const u8 firm1Hash[0x20] = { +static const u8 firm1Hash[SHA_256_HASH_SIZE] = { 0xD2, 0x53, 0xC1, 0xCC, 0x0A, 0x5F, 0xFA, 0xC6, 0xB3, 0x83, 0xDA, 0xC1, 0x82, 0x7C, 0xFB, 0x3B, 0x2D, 0x3D, 0x56, 0x6C, 0x6A, 0x1A, 0x8E, 0x52, 0x54, 0xE3, 0x89, 0xC2, 0x95, 0x06, 0x23, 0xE5 }; @@ -178,20 +178,53 @@ static inline void installer(bool isA9lh, bool isOtpless) if(!isOtpless) { + bool missingStage1Hash, + missingStage2Hash; + u8 stageHash[SHA_256_HASH_SIZE]; + //Inject stage1 memset32((void *)STAGE1_OFFSET, 0, MAX_STAGE1_SIZE); - if(!fileRead((void *)STAGE1_OFFSET, "a9lh/payload_stage1.bin", MAX_STAGE1_SIZE)) + u32 stage1Size = fileRead((void *)STAGE1_OFFSET, "a9lh/payload_stage1.bin", MAX_STAGE1_SIZE); + if(!stage1Size) shutdown(1, "Error: payload_stage1.bin doesn't exist or\nexceeds max size"); const u8 zeroes[688] = {0}; if(memcmp(zeroes, (void *)STAGE1_OFFSET, 688) == 0) shutdown(1, "Error: the payload_stage1.bin you're attempting\nto install is not compatible"); + //Verify stage1 + if(fileRead(stageHash, "a9lh/payload_stage1.bin.sha", sizeof(stageHash)) == sizeof(stageHash)) + { + if(!verifyHash((void *)STAGE1_OFFSET, stage1Size, stageHash)) + shutdown(1, "Error: payload_stage1.bin is invalid\nor corrupted"); + + missingStage1Hash = false; + } + else missingStage1Hash = true; + //Read stage2 memset32((void *)STAGE2_OFFSET, 0, MAX_STAGE2_SIZE); - if(!fileRead((void *)STAGE2_OFFSET, "a9lh/payload_stage2.bin", MAX_STAGE2_SIZE)) + u32 stage2Size = fileRead((void *)STAGE2_OFFSET, "a9lh/payload_stage2.bin", MAX_STAGE2_SIZE); + if(!stage2Size) shutdown(1, "Error: payload_stage2.bin doesn't exist or\nexceeds max size"); + //Verify stage2 + if(fileRead(stageHash, "a9lh/payload_stage2.bin.sha", sizeof(stageHash)) == sizeof(stageHash)) + { + if(!verifyHash((void *)STAGE2_OFFSET, stage2Size, stageHash)) + shutdown(1, "Error: payload_stage2.bin is invalid\nor corrupted"); + + missingStage2Hash = false; + } + else missingStage2Hash = true; + + if(missingStage1Hash || missingStage2Hash) + { + posY = drawString("Couldn't verify stage1 and/or stage2 integrity!", 10, posY + 10, COLOR_RED); + posY = drawString("Continuing might be dangerous!", 10, posY, COLOR_RED); + inputSequence(); + } + posY = drawString("All checks passed, installing...", 10, posY + SPACING_Y, COLOR_WHITE); sdmmc_nand_writesectors(0x5C000, MAX_STAGE2_SIZE / 0x200, (u8 *)STAGE2_OFFSET); @@ -202,7 +235,7 @@ static inline void installer(bool isA9lh, bool isOtpless) if(!isA9lh && isN3DS) { - *(u32 *)0x80FD0FC = 0xEAFFCBBF; //B 0x80F0000 + *(vu32 *)0x80FD0FC = 0xEAFFCBBF; //B 0x80F0000 memcpy((void *)0x80F0000, loader_bin, loader_bin_size); writeFirm((u8 *)FIRM0_100_OFFSET, false, FIRM0100_SIZE); @@ -221,17 +254,7 @@ static inline void uninstaller(void) posY = drawString("You are about to uninstall A9LH!", 10, posY + 10, COLOR_RED); posY = drawString("Doing this will require having 9.0 to reinstall!", 10, posY, COLOR_RED); - posY = drawString("If you would like to continue, press:", 10, posY, COLOR_WHITE); - posY = drawString("Up, Down, Left, Right, B, A, START, SELECT", 10, posY, COLOR_WHITE); - - u32 unlockSequence[] = { BUTTON_UP, BUTTON_DOWN, BUTTON_LEFT, BUTTON_RIGHT, BUTTON_B, BUTTON_A, BUTTON_START, BUTTON_SELECT }, - sequenceSize = sizeof(unlockSequence) / sizeof(u32); - - for(u32 correctPresses = 0; correctPresses < sequenceSize; correctPresses++) - { - if(waitInput() != unlockSequence[correctPresses]) - shutdown(1, "Button sequence not entered correctly"); - } + inputSequence(); //New 3DSes need a key sector with a proper key2, Old 3DSes have a blank key sector if(isN3DS) diff --git a/source/utils.c b/source/utils.c index 526fb14..b9c6292 100755 --- a/source/utils.c +++ b/source/utils.c @@ -46,6 +46,21 @@ void mcuReboot(void) while(true); } +void inputSequence(void) +{ + posY = drawString("If you would like to continue, press:", 10, posY, COLOR_WHITE); + posY = drawString("Up, Down, Left, Right, B, A, START, SELECT", 10, posY, COLOR_WHITE); + + u32 unlockSequence[] = { BUTTON_UP, BUTTON_DOWN, BUTTON_LEFT, BUTTON_RIGHT, BUTTON_B, BUTTON_A, BUTTON_START, BUTTON_SELECT }, + sequenceSize = sizeof(unlockSequence) / sizeof(u32); + + for(u32 correctPresses = 0; correctPresses < sequenceSize; correctPresses++) + { + if(waitInput() != unlockSequence[correctPresses]) + shutdown(1, "Button sequence not entered correctly"); + } +} + void shutdown(u32 mode, const char *message) { if(mode != 0) diff --git a/source/utils.h b/source/utils.h index 468f53d..7eb7ed3 100644 --- a/source/utils.h +++ b/source/utils.h @@ -25,4 +25,5 @@ extern u32 posY; u32 waitInput(void); void mcuReboot(void); +void inputSequence(void); void shutdown(u32 mode, const char *message); \ No newline at end of file