Merge readPin and verifyPin

This commit is contained in:
Aurora 2016-08-28 23:41:41 +02:00
parent e2d828a4a2
commit 82699f3e00
5 changed files with 32 additions and 40 deletions

View File

@ -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);

View File

@ -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);
void computePinHash(u8 *out, u8 *in, u32 blockCount);

View File

@ -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));

View File

@ -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;
}

View File

@ -43,6 +43,5 @@ typedef struct __attribute__((packed))
u8 hash[32];
} PINData;
bool readPin(PINData* out);
void newPin(bool allowSkipping);
void verifyPin(PINData *in);
bool verifyPin(void);