From 82699f3e00b9ab70390773272e15014585ac9769 Mon Sep 17 00:00:00 2001 From: Aurora Date: Sun, 28 Aug 2016 23:41:41 +0200 Subject: [PATCH] Merge readPin and verifyPin --- source/crypto.c | 4 ++-- source/crypto.h | 6 ++---- source/firm.c | 7 +------ source/pin.c | 52 ++++++++++++++++++++++++------------------------- source/pin.h | 3 +-- 5 files changed, 32 insertions(+), 40 deletions(-) diff --git a/source/crypto.c b/source/crypto.c index 7014027..e6a4751 100755 --- a/source/crypto.c +++ b/source/crypto.c @@ -457,13 +457,13 @@ void arm9Loader(u8 *arm9Section) } } -void computePINHash(u8 out[32], u8 *in, u32 blockCount) +void computePinHash(u8 *out, u8 *in, u32 blockCount) { u8 __attribute__((aligned(4))) cid[0x10]; u8 __attribute__((aligned(4))) cipherText[0x10]; sdmmc_get_cid(1, (u32 *)cid); - aes_use_keyslot(4); // console-unique keyslot which keys are set by the Arm9 bootROM + aes_use_keyslot(4); //Console-unique keyslot whose keys are set by the ARM9 bootROM aes(cipherText, in, blockCount, cid, AES_CBC_ENCRYPT_MODE, AES_INPUT_BE | AES_INPUT_NORMAL); sha(out, cipherText, 0x10, SHA_256_MODE); diff --git a/source/crypto.h b/source/crypto.h index 0b30bf9..c7221c8 100755 --- a/source/crypto.h +++ b/source/crypto.h @@ -100,8 +100,7 @@ #define SHA_1_HASH_SIZE (160 / 8) extern u32 emuOffset; -extern bool isN3DS; -extern bool isDevUnit; +extern bool isN3DS, isDevUnit; extern FirmwareSource firmSource; void ctrNandInit(void); @@ -109,5 +108,4 @@ u32 ctrNandRead(u32 sector, u32 sectorCount, u8 *outbuf); void setRSAMod0DerivedKeys(void); void decryptExeFs(u8 *inbuf); void arm9Loader(u8 *arm9Section); - -void computePINHash(u8 out[32], u8 *in, u32 blockCount); \ No newline at end of file +void computePinHash(u8 *out, u8 *in, u32 blockCount); \ No newline at end of file diff --git a/source/firm.c b/source/firm.c index 94da610..ce51dac 100755 --- a/source/firm.c +++ b/source/firm.c @@ -129,12 +129,7 @@ void main(void) //Boot options aren't being forced if(needConfig != DONT_CONFIGURE) { - PINData pin; - - bool pinExists = CONFIG(8) && readPin(&pin); - - //If we get here we should check the PIN (if it exists) in all cases - if(pinExists) verifyPin(&pin); + bool pinExists = CONFIG(8) && verifyPin(); //If no configuration file exists or SELECT is held, load configuration menu bool shouldLoadConfigMenu = needConfig == CREATE_CONFIGURATION || ((pressed & BUTTON_SELECT) && !(pressed & BUTTON_L1)); diff --git a/source/pin.c b/source/pin.c index 030efc4..1ea64e8 100644 --- a/source/pin.c +++ b/source/pin.c @@ -34,23 +34,7 @@ #include "pin.h" #include "crypto.h" -bool readPin(PINData *out) -{ - if(fileRead(out, "/luma/pin.bin") != sizeof(PINData) || - memcmp(out->magic, "PINF", 4) != 0 || - out->formatVersionMajor != PIN_VERSIONMAJOR || - out->formatVersionMinor != PIN_VERSIONMINOR) - return false; - - u8 __attribute__((aligned(4))) zeroes[16] = {0}; - u8 __attribute__((aligned(4))) tmp[32]; - - computePINHash(tmp, zeroes, 1); - - return memcmp(out->testHash, tmp, 32) == 0; //Test vector verification (SD card has, or hasn't been used on another console) -} - -static inline char PINKeyToLetter(u32 pressed) +static char pinKeyToLetter(u32 pressed) { const char keys[] = "AB--------XY"; @@ -89,7 +73,7 @@ void newPin(bool allowSkipping) if(pressed & BUTTON_START) return; if(!pressed) continue; - char key = PINKeyToLetter(pressed); + char key = pinKeyToLetter(pressed); enteredPassword[cnt++] = (u8)key; //Add character to password //Visualize character on screen @@ -105,10 +89,10 @@ void newPin(bool allowSkipping) pin.formatVersionMajor = PIN_VERSIONMAJOR; pin.formatVersionMinor = PIN_VERSIONMINOR; - computePINHash(tmp, zeroes, 1); + computePinHash(tmp, zeroes, 1); memcpy(pin.testHash, tmp, 32); - computePINHash(tmp, enteredPassword, (PIN_LENGTH + 15) / 16); + computePinHash(tmp, enteredPassword, (PIN_LENGTH + 15) / 16); memcpy(pin.hash, tmp, 32); if(!fileWrite(&pin, "/luma/pin.bin", sizeof(PINData))) @@ -119,10 +103,26 @@ void newPin(bool allowSkipping) } } -void verifyPin(PINData *in) +bool verifyPin(void) { initScreens(); + PINData pin; + + if(fileRead(&pin, "/luma/pin.bin") != sizeof(PINData) || + memcmp(pin.magic, "PINF", 4) != 0 || + pin.formatVersionMajor != PIN_VERSIONMAJOR || + pin.formatVersionMinor != PIN_VERSIONMINOR) + return false; + + u8 __attribute__((aligned(4))) zeroes[16] = {0}; + u8 __attribute__((aligned(4))) tmp[32]; + + computePinHash(tmp, zeroes, 1); + + //Test vector verification (SD card has, or hasn't been used on another console) + if(memcmp(pin.testHash, tmp, 32) != 0) return false; + //Pad to AES block length with zeroes u8 __attribute__((aligned(4))) enteredPassword[16 * ((PIN_LENGTH + 15) / 16)] = {0}; @@ -148,7 +148,7 @@ void verifyPin(PINData *in) if(!pressed) continue; - char key = PINKeyToLetter(pressed); + char key = pinKeyToLetter(pressed); enteredPassword[cnt++] = (u8)key; //Add character to password //Visualize character on screen @@ -157,10 +157,8 @@ void verifyPin(PINData *in) if(cnt >= PIN_LENGTH) { - u8 __attribute__((aligned(4))) tmp[32]; - - computePINHash(tmp, enteredPassword, (PIN_LENGTH + 15) / 16); - unlock = memcmp(in->hash, tmp, 32) == 0; + computePinHash(tmp, enteredPassword, (PIN_LENGTH + 15) / 16); + unlock = memcmp(pin.hash, tmp, 32) == 0; if(!unlock) { @@ -173,4 +171,6 @@ void verifyPin(PINData *in) } } } + + return true; } \ No newline at end of file diff --git a/source/pin.h b/source/pin.h index 79b1dd2..b8aa6de 100644 --- a/source/pin.h +++ b/source/pin.h @@ -43,6 +43,5 @@ typedef struct __attribute__((packed)) u8 hash[32]; } PINData; -bool readPin(PINData* out); void newPin(bool allowSkipping); -void verifyPin(PINData *in); \ No newline at end of file +bool verifyPin(void); \ No newline at end of file