2016-08-03 14:13:26 +02:00
|
|
|
/*
|
|
|
|
* This file is part of Luma3DS
|
|
|
|
* Copyright (C) 2016 Aurora Wright, TuxSH
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified
|
|
|
|
* reasonable legal notices or author attributions in that material or in the Appropriate Legal
|
|
|
|
* Notices displayed by works containing it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2016-09-08 23:07:03 +02:00
|
|
|
* Code originally by reworks
|
2016-08-03 14:13:26 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "draw.h"
|
2016-09-04 00:36:39 +02:00
|
|
|
#include "config.h"
|
2016-08-03 14:13:26 +02:00
|
|
|
#include "screen.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "buttons.h"
|
|
|
|
#include "fs.h"
|
|
|
|
#include "pin.h"
|
|
|
|
#include "crypto.h"
|
|
|
|
|
2016-08-28 23:41:41 +02:00
|
|
|
static char pinKeyToLetter(u32 pressed)
|
2016-08-03 14:13:26 +02:00
|
|
|
{
|
2016-09-04 00:36:39 +02:00
|
|
|
const char keys[] = "AB--RLUD--XY";
|
2016-08-03 14:13:26 +02:00
|
|
|
|
|
|
|
u32 i;
|
2016-08-29 18:11:59 +02:00
|
|
|
for(i = 31; pressed > 1; i--) pressed /= 2;
|
2016-08-03 14:13:26 +02:00
|
|
|
|
|
|
|
return keys[31 - i];
|
|
|
|
}
|
|
|
|
|
2016-09-19 14:57:36 +02:00
|
|
|
void newPin(bool allowSkipping, u32 pinMode)
|
2016-08-03 14:13:26 +02:00
|
|
|
{
|
2016-09-13 16:22:26 +02:00
|
|
|
clearScreens(true, true);
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-09-19 14:57:36 +02:00
|
|
|
u8 length = 4 + 2 * (pinMode - 1);
|
2016-09-12 18:58:10 +02:00
|
|
|
|
2016-08-28 15:50:11 +02:00
|
|
|
char *title = allowSkipping ? "Press START to skip or enter a new PIN" : "Enter a new PIN to proceed";
|
2016-09-13 16:22:26 +02:00
|
|
|
drawString(title, true, 10, 10, COLOR_TITLE);
|
|
|
|
drawString("PIN ( digits): ", true, 10, 10 + 2 * SPACING_Y, COLOR_WHITE);
|
|
|
|
drawCharacter('0' + length, true, 10 + 5 * SPACING_X, 10 + 2 * SPACING_Y, COLOR_WHITE);
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-08-28 13:49:15 +02:00
|
|
|
//Pad to AES block length with zeroes
|
2016-09-19 14:57:36 +02:00
|
|
|
u8 __attribute__((aligned(4))) enteredPassword[AES_BLOCK_SIZE] = {0};
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-09-12 18:58:10 +02:00
|
|
|
u8 cnt = 0;
|
2016-09-14 22:31:25 +02:00
|
|
|
u32 charDrawPos = 16 * SPACING_X;
|
2016-09-12 18:47:47 +02:00
|
|
|
|
2016-09-04 00:36:39 +02:00
|
|
|
while(cnt < length)
|
2016-08-03 14:13:26 +02:00
|
|
|
{
|
2016-08-15 03:51:48 +02:00
|
|
|
u32 pressed;
|
2016-08-03 14:13:26 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
pressed = waitInput();
|
|
|
|
}
|
2016-08-28 15:50:11 +02:00
|
|
|
while(!(pressed & PIN_BUTTONS));
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-08-28 15:50:11 +02:00
|
|
|
pressed &= PIN_BUTTONS;
|
|
|
|
if(!allowSkipping) pressed &= ~BUTTON_START;
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-08-28 15:50:11 +02:00
|
|
|
if(pressed & BUTTON_START) return;
|
2016-08-03 14:13:26 +02:00
|
|
|
if(!pressed) continue;
|
2016-08-28 13:49:15 +02:00
|
|
|
|
2016-08-28 23:41:41 +02:00
|
|
|
char key = pinKeyToLetter(pressed);
|
2016-08-28 13:49:15 +02:00
|
|
|
enteredPassword[cnt++] = (u8)key; //Add character to password
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-08-28 13:49:15 +02:00
|
|
|
//Visualize character on screen
|
2016-09-13 16:22:26 +02:00
|
|
|
drawCharacter(key, true, 10 + charDrawPos, 10 + 2 * SPACING_Y, COLOR_WHITE);
|
2016-08-03 14:13:26 +02:00
|
|
|
charDrawPos += 2 * SPACING_X;
|
2016-08-16 22:46:41 +02:00
|
|
|
}
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-09-08 02:12:29 +02:00
|
|
|
PinData pin;
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-08-16 22:46:41 +02:00
|
|
|
memcpy(pin.magic, "PINF", 4);
|
2016-08-27 16:00:15 +02:00
|
|
|
pin.formatVersionMajor = PIN_VERSIONMAJOR;
|
|
|
|
pin.formatVersionMinor = PIN_VERSIONMINOR;
|
2016-09-04 00:36:39 +02:00
|
|
|
pin.length = length;
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-09-19 14:57:36 +02:00
|
|
|
u8 __attribute__((aligned(4))) tmp[SHA_256_HASH_SIZE];
|
|
|
|
u8 __attribute__((aligned(4))) zeroes[AES_BLOCK_SIZE] = {0};
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-09-04 00:36:39 +02:00
|
|
|
computePinHash(tmp, zeroes);
|
|
|
|
memcpy(pin.testHash, tmp, sizeof(tmp));
|
|
|
|
|
|
|
|
computePinHash(tmp, enteredPassword);
|
|
|
|
memcpy(pin.hash, tmp, sizeof(tmp));
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-09-08 02:12:29 +02:00
|
|
|
if(!fileWrite(&pin, PIN_PATH, sizeof(PinData)))
|
2016-08-30 16:56:19 +02:00
|
|
|
error("Error writing the PIN file");
|
2016-08-03 14:13:26 +02:00
|
|
|
}
|
|
|
|
|
2016-09-19 14:57:36 +02:00
|
|
|
bool verifyPin(u32 pinMode)
|
2016-08-03 14:13:26 +02:00
|
|
|
{
|
2016-09-08 02:12:29 +02:00
|
|
|
PinData pin;
|
2016-08-28 23:41:41 +02:00
|
|
|
|
2016-09-13 14:54:14 +02:00
|
|
|
if(fileRead(&pin, PIN_PATH, sizeof(PinData)) != sizeof(PinData) ||
|
2016-08-28 23:41:41 +02:00
|
|
|
memcmp(pin.magic, "PINF", 4) != 0 ||
|
|
|
|
pin.formatVersionMajor != PIN_VERSIONMAJOR ||
|
2016-09-04 00:36:39 +02:00
|
|
|
pin.formatVersionMinor != PIN_VERSIONMINOR ||
|
2016-09-19 14:57:36 +02:00
|
|
|
pin.length != 4 + 2 * (pinMode - 1))
|
2016-08-28 23:41:41 +02:00
|
|
|
return false;
|
|
|
|
|
2016-09-19 14:57:36 +02:00
|
|
|
u8 __attribute__((aligned(4))) zeroes[AES_BLOCK_SIZE] = {0};
|
|
|
|
u8 __attribute__((aligned(4))) tmp[SHA_256_HASH_SIZE];
|
2016-08-28 23:41:41 +02:00
|
|
|
|
2016-09-04 00:36:39 +02:00
|
|
|
computePinHash(tmp, zeroes);
|
2016-08-28 23:41:41 +02:00
|
|
|
|
|
|
|
//Test vector verification (SD card has, or hasn't been used on another console)
|
2016-09-04 00:55:20 +02:00
|
|
|
if(memcmp(pin.testHash, tmp, sizeof(tmp)) != 0) return false;
|
2016-08-28 23:41:41 +02:00
|
|
|
|
2016-09-12 18:58:10 +02:00
|
|
|
initScreens();
|
|
|
|
|
2016-08-29 16:04:17 +02:00
|
|
|
//Pad to AES block length with zeroes
|
2016-09-19 14:57:36 +02:00
|
|
|
u8 __attribute__((aligned(4))) enteredPassword[AES_BLOCK_SIZE] = {0};
|
2016-08-29 16:04:17 +02:00
|
|
|
|
|
|
|
bool unlock = false;
|
2016-09-12 18:58:10 +02:00
|
|
|
u8 cnt = 0;
|
2016-09-14 22:31:25 +02:00
|
|
|
u32 charDrawPos = 16 * SPACING_X;
|
2016-08-28 14:59:33 +02:00
|
|
|
|
2016-09-15 19:53:51 +02:00
|
|
|
const char messagePath[] = "/luma/pinmessage.txt";
|
2016-09-13 16:22:26 +02:00
|
|
|
|
|
|
|
u32 messageSize = getFileSize(messagePath);
|
2016-09-14 22:31:25 +02:00
|
|
|
if(messageSize > 0 && messageSize <= 800)
|
2016-09-13 16:22:26 +02:00
|
|
|
{
|
|
|
|
char message[messageSize + 1];
|
|
|
|
fileRead(message, messagePath, 0);
|
|
|
|
message[messageSize] = 0;
|
|
|
|
drawString(message, false, 10, 10, COLOR_WHITE);
|
|
|
|
}
|
|
|
|
|
2016-08-29 16:04:17 +02:00
|
|
|
while(!unlock)
|
|
|
|
{
|
2016-09-13 16:22:26 +02:00
|
|
|
drawString("Press START to shutdown or enter PIN to proceed", true, 10, 10, COLOR_TITLE);
|
|
|
|
drawString("PIN ( digits): ", true, 10, 10 + 2 * SPACING_Y, COLOR_WHITE);
|
|
|
|
drawCharacter('0' + pin.length, true, 10 + 5 * SPACING_X, 10 + 2 * SPACING_Y, COLOR_WHITE);
|
2016-08-29 15:57:32 +02:00
|
|
|
|
2016-08-29 16:04:17 +02:00
|
|
|
u32 pressed;
|
|
|
|
do
|
2016-08-03 14:13:26 +02:00
|
|
|
{
|
2016-08-29 16:04:17 +02:00
|
|
|
pressed = waitInput();
|
|
|
|
}
|
|
|
|
while(!(pressed & PIN_BUTTONS));
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-08-29 16:04:17 +02:00
|
|
|
if(pressed & BUTTON_START) mcuPowerOff();
|
2016-08-16 22:59:25 +02:00
|
|
|
|
2016-08-29 16:04:17 +02:00
|
|
|
pressed &= PIN_BUTTONS;
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-08-29 16:04:17 +02:00
|
|
|
if(!pressed) continue;
|
2016-08-28 15:58:10 +02:00
|
|
|
|
2016-08-29 16:04:17 +02:00
|
|
|
char key = pinKeyToLetter(pressed);
|
|
|
|
enteredPassword[cnt++] = (u8)key; //Add character to password
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-08-29 16:04:17 +02:00
|
|
|
//Visualize character on screen
|
2016-09-13 16:22:26 +02:00
|
|
|
drawCharacter(key, true, 10 + charDrawPos, 10 + 2 * SPACING_Y, COLOR_WHITE);
|
2016-08-29 16:04:17 +02:00
|
|
|
charDrawPos += 2 * SPACING_X;
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-09-04 00:36:39 +02:00
|
|
|
if(cnt >= pin.length)
|
2016-08-29 16:04:17 +02:00
|
|
|
{
|
2016-09-04 00:36:39 +02:00
|
|
|
computePinHash(tmp, enteredPassword);
|
|
|
|
unlock = memcmp(pin.hash, tmp, sizeof(tmp)) == 0;
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-08-29 16:04:17 +02:00
|
|
|
if(!unlock)
|
2016-08-15 03:51:48 +02:00
|
|
|
{
|
2016-09-12 18:58:10 +02:00
|
|
|
charDrawPos = 16 * SPACING_X;
|
2016-08-29 16:04:17 +02:00
|
|
|
cnt = 0;
|
2016-08-15 03:51:48 +02:00
|
|
|
|
2016-09-13 16:22:26 +02:00
|
|
|
clearScreens(true, false);
|
2016-08-15 03:51:48 +02:00
|
|
|
|
2016-09-13 16:22:26 +02:00
|
|
|
drawString("Wrong PIN, try again", true, 10, 10 + 4 * SPACING_Y, COLOR_RED);
|
2016-08-03 14:13:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-28 23:41:41 +02:00
|
|
|
|
|
|
|
return true;
|
2016-08-16 18:47:27 +02:00
|
|
|
}
|