2016-08-03 14:13:26 +02:00
|
|
|
/*
|
|
|
|
* This file is part of Luma3DS
|
2017-06-05 02:02:04 +02:00
|
|
|
* Copyright (C) 2016-2017 Aurora Wright, TuxSH
|
2016-08-03 14:13:26 +02:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
2017-06-05 02:02:04 +02:00
|
|
|
* Additional Terms 7.b and 7.c of GPLv3 apply 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.
|
|
|
|
* * Prohibiting misrepresentation of the origin of that material,
|
|
|
|
* or requiring that modified versions of such material be marked in
|
|
|
|
* reasonable ways as different from the original version.
|
2016-08-03 14:13:26 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
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
|
|
|
{
|
2017-06-09 17:29:26 +02:00
|
|
|
static 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-11-15 14:08:58 +01:00
|
|
|
clearScreens(false);
|
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
|
|
|
|
2017-05-07 17:58:44 +02:00
|
|
|
drawString(true, 10, 10, COLOR_TITLE, "Enter a new PIN using ABXY and the DPad");
|
|
|
|
drawString(true, 10, 10 + SPACING_Y, COLOR_TITLE, allowSkipping ? "Press START to skip, SELECT to reset" : "Press SELECT to reset");
|
2016-11-14 15:39:26 +01:00
|
|
|
|
2017-05-07 17:58:44 +02:00
|
|
|
drawFormattedString(true, 10, 10 + 3 * SPACING_Y, COLOR_WHITE, "PIN (%u digits): ", length);
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-08-28 13:49:15 +02:00
|
|
|
//Pad to AES block length with zeroes
|
2016-11-12 14:10:44 +01:00
|
|
|
__attribute__((aligned(4))) u8 enteredPassword[AES_BLOCK_SIZE] = {0};
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-11-14 15:39:26 +01:00
|
|
|
bool reset = false;
|
2016-09-12 18:58:10 +02:00
|
|
|
u8 cnt = 0;
|
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-11-14 15:39:26 +01:00
|
|
|
if(reset)
|
|
|
|
{
|
|
|
|
for(u32 i = 0; i < cnt; i++)
|
2017-05-07 17:58:44 +02:00
|
|
|
drawCharacter(true, 10 + (16 + 2 * i) * SPACING_X, 10 + 3 * SPACING_Y, COLOR_BLACK, (char)enteredPassword[i]);
|
2016-11-14 15:39:26 +01:00
|
|
|
|
|
|
|
cnt = 0;
|
|
|
|
reset = false;
|
|
|
|
}
|
|
|
|
|
2016-08-15 03:51:48 +02:00
|
|
|
u32 pressed;
|
2016-08-03 14:13:26 +02:00
|
|
|
do
|
|
|
|
{
|
2016-11-13 22:13:24 +01:00
|
|
|
pressed = waitInput(false);
|
2016-08-03 14:13:26 +02:00
|
|
|
}
|
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-28 13:49:15 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
if(pressed & BUTTON_SELECT)
|
2016-11-14 15:39:26 +01:00
|
|
|
{
|
2016-11-15 19:29:48 +01:00
|
|
|
reset = true;
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-14 15:39:26 +01:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
if(!pressed) continue;
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-11-16 03:41:59 +01:00
|
|
|
//Add character to password
|
|
|
|
enteredPassword[cnt] = (u8)pinKeyToLetter(pressed);
|
2016-11-15 19:29:48 +01:00
|
|
|
|
|
|
|
//Visualize character on screen
|
2017-05-07 17:58:44 +02:00
|
|
|
drawCharacter(true, 10 + (16 + 2 * cnt) * SPACING_X, 10 + 3 * SPACING_Y, COLOR_WHITE, enteredPassword[cnt]);
|
2016-11-15 19:29:48 +01:00
|
|
|
|
|
|
|
cnt++;
|
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-08-03 14:13:26 +02:00
|
|
|
|
2016-11-12 14:10:44 +01:00
|
|
|
__attribute__((aligned(4))) u8 tmp[SHA_256_HASH_SIZE],
|
|
|
|
lengthBlock[AES_BLOCK_SIZE] = {0};
|
2016-09-22 14:48:28 +02:00
|
|
|
lengthBlock[0] = length;
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-09-22 14:48:28 +02:00
|
|
|
computePinHash(tmp, lengthBlock);
|
|
|
|
memcpy(pin.lengthHash, tmp, sizeof(tmp));
|
2016-09-04 00:36:39 +02:00
|
|
|
|
|
|
|
computePinHash(tmp, enteredPassword);
|
|
|
|
memcpy(pin.hash, tmp, sizeof(tmp));
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-10-12 02:28:08 +02:00
|
|
|
if(!fileWrite(&pin, PIN_FILE, sizeof(PinData)))
|
2016-08-30 16:56:19 +02:00
|
|
|
error("Error writing the PIN file");
|
2016-08-03 14:13:26 +02:00
|
|
|
}
|
|
|
|
|
2017-04-15 15:58:07 +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-10-12 02:28:08 +02:00
|
|
|
if(fileRead(&pin, PIN_FILE, sizeof(PinData)) != sizeof(PinData) ||
|
2016-08-28 23:41:41 +02:00
|
|
|
memcmp(pin.magic, "PINF", 4) != 0 ||
|
|
|
|
pin.formatVersionMajor != PIN_VERSIONMAJOR ||
|
2016-09-22 14:48:28 +02:00
|
|
|
pin.formatVersionMinor != PIN_VERSIONMINOR)
|
2016-08-28 23:41:41 +02:00
|
|
|
return false;
|
|
|
|
|
2016-11-12 14:10:44 +01:00
|
|
|
__attribute__((aligned(4))) u8 tmp[SHA_256_HASH_SIZE],
|
|
|
|
lengthBlock[AES_BLOCK_SIZE] = {0};
|
2016-09-22 14:48:28 +02:00
|
|
|
lengthBlock[0] = 4 + 2 * (pinMode - 1);
|
2016-08-28 23:41:41 +02:00
|
|
|
|
2016-09-22 14:48:28 +02:00
|
|
|
computePinHash(tmp, lengthBlock);
|
2016-08-28 23:41:41 +02:00
|
|
|
|
2016-09-22 14:48:28 +02:00
|
|
|
//Test vector verification (check if SD card has been used on another console or PIN length changed)
|
|
|
|
if(memcmp(pin.lengthHash, tmp, sizeof(tmp)) != 0) return false;
|
2016-08-28 23:41:41 +02:00
|
|
|
|
2016-09-12 18:58:10 +02:00
|
|
|
initScreens();
|
|
|
|
|
2018-03-04 00:45:47 +01:00
|
|
|
swapFramebuffers(true);
|
|
|
|
|
2017-05-07 17:58:44 +02:00
|
|
|
drawString(true, 10, 10, COLOR_TITLE, "Enter the PIN using ABXY and the DPad to proceed");
|
|
|
|
drawString(true, 10, 10 + SPACING_Y, COLOR_TITLE, "Press START to shutdown, SELECT to clear");
|
2016-08-29 16:04:17 +02:00
|
|
|
|
2017-05-07 17:58:44 +02:00
|
|
|
drawFormattedString(true, 10, 10 + 3 * SPACING_Y, COLOR_WHITE, "PIN (%u digits): ", lengthBlock[0]);
|
2016-08-28 14:59:33 +02:00
|
|
|
|
2018-03-04 00:45:47 +01:00
|
|
|
bool isBottomSplashValid = getFileSize("splashpin.bin") == SCREEN_BOTTOM_FBSIZE;
|
|
|
|
if(isBottomSplashValid)
|
|
|
|
{
|
|
|
|
isBottomSplashValid = fileRead(fbs[0].bottom, "splashpin.bin", SCREEN_BOTTOM_FBSIZE) == SCREEN_BOTTOM_FBSIZE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
static const char *messageFile = "pinmessage.txt";
|
|
|
|
char message[801];
|
2016-09-13 16:22:26 +02:00
|
|
|
|
2018-03-04 00:45:47 +01:00
|
|
|
u32 messageSize = fileRead(message, messageFile, sizeof(message) - 1);
|
2016-09-22 14:48:28 +02:00
|
|
|
|
2018-03-04 00:45:47 +01:00
|
|
|
if(messageSize != 0)
|
|
|
|
{
|
|
|
|
message[messageSize] = 0;
|
|
|
|
drawString(false, 10, 10, COLOR_WHITE, message);
|
|
|
|
}
|
2016-09-13 16:22:26 +02:00
|
|
|
}
|
2018-03-04 00:45:47 +01:00
|
|
|
swapFramebuffers(false);
|
2016-09-13 16:22:26 +02:00
|
|
|
|
2016-11-14 15:39:26 +01:00
|
|
|
//Pad to AES block length with zeroes
|
|
|
|
__attribute__((aligned(4))) u8 enteredPassword[AES_BLOCK_SIZE] = {0};
|
|
|
|
|
|
|
|
bool unlock = false,
|
|
|
|
reset = false;
|
|
|
|
u8 cnt = 0;
|
|
|
|
|
2016-08-29 16:04:17 +02:00
|
|
|
while(!unlock)
|
|
|
|
{
|
2016-11-14 15:39:26 +01:00
|
|
|
if(reset)
|
|
|
|
{
|
|
|
|
for(u32 i = 0; i < cnt; i++)
|
2017-05-07 17:58:44 +02:00
|
|
|
drawCharacter(true, 10 + (16 + 2 * i) * SPACING_X, 10 + 3 * SPACING_Y, COLOR_BLACK, '*');
|
2016-11-14 15:39:26 +01:00
|
|
|
|
|
|
|
cnt = 0;
|
|
|
|
reset = false;
|
|
|
|
}
|
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-11-13 22:13:24 +01:00
|
|
|
pressed = waitInput(false);
|
2016-08-29 16:04:17 +02:00
|
|
|
}
|
|
|
|
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-11-15 19:29:48 +01:00
|
|
|
if(pressed & BUTTON_SELECT)
|
2016-08-29 16:04:17 +02:00
|
|
|
{
|
2016-11-15 19:29:48 +01:00
|
|
|
reset = true;
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-14 15:39:26 +01:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
if(!pressed) continue;
|
2016-08-03 14:13:26 +02:00
|
|
|
|
2016-11-16 03:41:59 +01:00
|
|
|
//Add character to password
|
|
|
|
enteredPassword[cnt] = (u8)pinKeyToLetter(pressed);
|
2017-04-15 15:58:07 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
//Visualize character on screen
|
2017-05-07 17:58:44 +02:00
|
|
|
drawCharacter(true, 10 + (16 + 2 * cnt) * SPACING_X, 10 + 3 * SPACING_Y, COLOR_WHITE, '*');
|
2016-11-15 19:29:48 +01:00
|
|
|
|
|
|
|
if(++cnt < lengthBlock[0]) continue;
|
|
|
|
|
|
|
|
computePinHash(tmp, enteredPassword);
|
|
|
|
unlock = memcmp(pin.hash, tmp, sizeof(tmp)) == 0;
|
|
|
|
|
|
|
|
if(!unlock)
|
|
|
|
{
|
|
|
|
reset = true;
|
2016-08-15 03:51:48 +02:00
|
|
|
|
2017-05-07 17:58:44 +02:00
|
|
|
drawString(true, 10, 10 + 5 * SPACING_Y, COLOR_RED, "Wrong PIN, try again");
|
2016-08-03 14:13:26 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-28 23:41:41 +02:00
|
|
|
|
|
|
|
return true;
|
2017-05-07 17:58:44 +02:00
|
|
|
}
|