proxmark3/client/cmdflashmem.c

596 lines
21 KiB
C
Raw Normal View History

//-----------------------------------------------------------------------------
// Copyright (C) 2018 iceman
//
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
// at your option, any later version. See the LICENSE.txt file for the text of
// the license.
//-----------------------------------------------------------------------------
// Proxmark3 RDV40 Flash memory commands
//-----------------------------------------------------------------------------
#include "cmdflashmem.h"
2018-11-08 00:17:17 +08:00
#include "mbedtls/rsa.h"
#include "mbedtls/sha1.h"
2019-02-25 06:57:04 +08:00
#include "mbedtls/base64.h"
2018-09-06 11:24:50 +08:00
#define MCK 48000000
#define FLASH_MINFAST 24000000 //33000000
#define FLASH_BAUD MCK/2
#define FLASH_FASTBAUD MCK
#define FLASH_MINBAUD FLASH_FASTBAUD
#define FASTFLASH (FLASHMEM_SPIBAUDRATE > FLASH_MINFAST)
static int CmdHelp(const char *Cmd);
2018-09-06 11:24:50 +08:00
2019-04-10 19:06:05 +08:00
static int usage_flashmem_spibaud(void) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "Usage: mem spibaud [h] <baudrate>");
PrintAndLogEx(NORMAL, "Options:");
PrintAndLogEx(NORMAL, " h this help");
PrintAndLogEx(NORMAL, " <baudrate> SPI baudrate in MHz [24|48]");
PrintAndLogEx(NORMAL, " ");
2019-08-06 19:51:10 +08:00
PrintAndLogEx(NORMAL, " If >= 24MHz, FASTREADS instead of READS instruction will be used.");
PrintAndLogEx(NORMAL, " Reading Flash ID will virtually always fail under 48MHz setting");
PrintAndLogEx(NORMAL, " Unless you know what you are doing, please stay at 24MHz");
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "Examples:");
PrintAndLogEx(NORMAL, " mem spibaud 48");
2019-04-19 05:26:12 +08:00
return PM3_SUCCESS;
2018-09-06 11:24:50 +08:00
}
2019-04-10 19:06:05 +08:00
static int usage_flashmem_load(void) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "Loads binary file into flash memory on device");
2019-06-01 05:07:09 +08:00
PrintAndLogEx(NORMAL, "Usage: mem load [o <offset>] f <file name> [m|t|i]");
PrintAndLogEx(NORMAL, "Warning: mem area to be written must have been wiped first");
PrintAndLogEx(NORMAL, "(this is already taken care when loading dictionaries)");
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, " o <offset> : offset in memory");
PrintAndLogEx(NORMAL, " f <filename> : file name");
PrintAndLogEx(NORMAL, " m : upload 6 bytes keys (mifare key dictionary)");
PrintAndLogEx(NORMAL, " i : upload 8 bytes keys (iClass key dictionary)");
PrintAndLogEx(NORMAL, " t : upload 4 bytes keys (pwd dictionary)");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Examples:");
PrintAndLogEx(NORMAL, " mem load f myfile"); // upload file myfile at default offset 0
PrintAndLogEx(NORMAL, " mem load f myfile o 1024"); // upload file myfile at offset 1024
PrintAndLogEx(NORMAL, " mem load f default_keys m");
PrintAndLogEx(NORMAL, " mem load f default_pwd t");
PrintAndLogEx(NORMAL, " mem load f default_iclass_keys i");
2019-04-19 05:26:12 +08:00
return PM3_SUCCESS;
}
static int usage_flashmem_dump(void) {
PrintAndLogEx(NORMAL, "Dumps flash memory on device into a file or in console");
PrintAndLogEx(NORMAL, " Usage: mem dump [o <offset>] [l <length>] [f <file name>] [p]");
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, " o <offset> : offset in memory");
PrintAndLogEx(NORMAL, " l <length> : length");
PrintAndLogEx(NORMAL, " f <filename> : file name");
PrintAndLogEx(NORMAL, " p : print dump in console");
PrintAndLogEx(NORMAL, " You must specify at lease option f or option p, both if you wish");
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Examples:");
PrintAndLogEx(NORMAL, " mem dump f myfile"); // download whole flashmem to file myfile
PrintAndLogEx(NORMAL, " mem dump p o 262015 l 128"); // display 128 bytes from offset 262015 (RSA sig)
PrintAndLogEx(NORMAL, " mem dump p f myfile o 241664 l 58"); // download and display 58 bytes from offset 241664 to file myfile
2019-04-19 05:26:12 +08:00
return PM3_SUCCESS;
}
2019-04-10 19:06:05 +08:00
static int usage_flashmem_wipe(void) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, "[OBS] use with caution.");
PrintAndLogEx(NORMAL, "Wipe flash memory on device, which fills memory with 0xFF\n");
PrintAndLogEx(NORMAL, " Usage: mem wipe p <page>");
PrintAndLogEx(NORMAL, " p <page> : 0,1,2 page memory");
// PrintAndLogEx(NORMAL, " i : inital total wipe");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Examples:");
PrintAndLogEx(NORMAL, " mem wipe p 0"); // wipes first page.
2019-04-19 05:26:12 +08:00
return PM3_SUCCESS;
}
2019-04-10 19:06:05 +08:00
static int usage_flashmem_info(void) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "Collect signature and verify it from flash memory\n");
2019-06-01 05:07:09 +08:00
PrintAndLogEx(NORMAL, " Usage: mem info");
// PrintAndLogEx(NORMAL, " s : create a signature");
// PrintAndLogEx(NORMAL, " w : write signature to flash memory");
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Examples:");
PrintAndLogEx(NORMAL, " mem info");
2019-06-01 05:07:09 +08:00
// PrintAndLogEx(NORMAL, " mem info s");
2019-04-19 05:26:12 +08:00
return PM3_SUCCESS;
}
static int CmdFlashmemSpiBaudrate(const char *Cmd) {
2018-09-06 11:24:50 +08:00
2019-03-10 06:35:06 +08:00
char ctmp = tolower(param_getchar(Cmd, 0));
2019-04-19 05:26:12 +08:00
if (strlen(Cmd) < 1 || ctmp == 'h') {
2019-05-08 00:50:45 +08:00
return usage_flashmem_spibaud();
2019-04-19 05:26:12 +08:00
}
2019-05-08 07:35:51 +08:00
2019-03-10 06:35:06 +08:00
uint32_t baudrate = param_get32ex(Cmd, 0, 0, 10);
baudrate = baudrate * 1000000;
2019-04-19 05:26:12 +08:00
if (baudrate != FLASH_BAUD && baudrate != FLASH_MINBAUD) {
usage_flashmem_spibaud();
return PM3_EINVARG;
}
2019-05-08 00:50:45 +08:00
SendCommandMIX(CMD_FLASHMEM_SET_SPIBAUDRATE, baudrate, 0, 0, NULL, 0);
2019-04-19 05:26:12 +08:00
return PM3_SUCCESS;
2018-09-06 11:24:50 +08:00
}
static int CmdFlashMemLoad(const char *Cmd) {
2019-03-10 06:35:06 +08:00
uint32_t start_index = 0;
char filename[FILE_PATH_SIZE] = {0};
bool errors = false;
uint8_t cmdp = 0;
Dictionary_t d = DICTIONARY_NONE;
while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch (tolower(param_getchar(Cmd, cmdp))) {
2019-03-10 07:00:59 +08:00
case 'h':
2019-05-08 00:50:45 +08:00
return usage_flashmem_load();
2019-03-10 07:00:59 +08:00
case 'f':
if (param_getstr(Cmd, cmdp + 1, filename, FILE_PATH_SIZE) >= FILE_PATH_SIZE) {
PrintAndLogEx(FAILED, "Filename too long");
errors = true;
break;
}
cmdp += 2;
break;
case 'o':
start_index = param_get32ex(Cmd, cmdp + 1, 0, 10);
cmdp += 2;
break;
case 'm':
d = DICTIONARY_MIFARE;
cmdp++;
break;
case 't':
d = DICTIONARY_T55XX;
cmdp++;
break;
case 'i':
d = DICTIONARY_ICLASS;
cmdp++;
break;
default:
PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
2019-03-10 06:35:06 +08:00
errors = true;
break;
}
}
//Validations
2019-04-19 05:26:12 +08:00
if (errors || cmdp == 0) {
usage_flashmem_load();
return PM3_EINVARG;
}
2019-03-10 06:35:06 +08:00
size_t datalen = 0;
uint16_t keycount = 0;
int res = 0;
uint8_t *data = calloc(FLASH_MEM_MAX_SIZE, sizeof(uint8_t));
switch (d) {
case DICTIONARY_MIFARE:
start_index = DEFAULT_MF_KEYS_OFFSET;
res = loadFileDICTIONARY(filename, data + 2, &datalen, 6, &keycount);
2019-03-10 07:00:59 +08:00
if (res || !keycount) {
2019-03-10 06:35:06 +08:00
free(data);
2019-04-19 05:26:12 +08:00
return PM3_EFILE;
2019-03-10 06:35:06 +08:00
}
data[0] = (keycount >> 0) & 0xFF;
data[1] = (keycount >> 8) & 0xFF;
datalen += 2;
break;
case DICTIONARY_T55XX:
start_index = DEFAULT_T55XX_KEYS_OFFSET;
res = loadFileDICTIONARY(filename, data + 2, &datalen, 4, &keycount);
2019-03-10 07:00:59 +08:00
if (res || !keycount) {
2019-03-10 06:35:06 +08:00
free(data);
2019-04-19 05:26:12 +08:00
return PM3_EFILE;
2019-03-10 06:35:06 +08:00
}
data[0] = (keycount >> 0) & 0xFF;
data[1] = (keycount >> 8) & 0xFF;
datalen += 2;
break;
case DICTIONARY_ICLASS:
start_index = DEFAULT_ICLASS_KEYS_OFFSET;
res = loadFileDICTIONARY(filename, data + 2, &datalen, 8, &keycount);
2019-03-10 07:00:59 +08:00
if (res || !keycount) {
2019-03-10 06:35:06 +08:00
free(data);
2019-04-19 05:26:12 +08:00
return PM3_EFILE;
2019-03-10 06:35:06 +08:00
}
data[0] = (keycount >> 0) & 0xFF;
data[1] = (keycount >> 8) & 0xFF;
datalen += 2;
break;
case DICTIONARY_NONE:
res = loadFile(filename, ".bin", data, FLASH_MEM_MAX_SIZE, &datalen);
//int res = loadFileEML( filename, data, &datalen);
2019-03-10 07:00:59 +08:00
if (res) {
2019-03-10 06:35:06 +08:00
free(data);
2019-04-19 05:26:12 +08:00
return PM3_EFILE;
2019-03-10 06:35:06 +08:00
}
if (datalen > FLASH_MEM_MAX_SIZE) {
PrintAndLogDevice(ERR, "error, filesize is larger than available memory");
2019-03-10 06:35:06 +08:00
free(data);
2019-04-19 05:26:12 +08:00
return PM3_EOVFLOW;
2019-03-10 06:35:06 +08:00
}
break;
}
uint8_t *newdata = realloc(data, datalen);
if (newdata == NULL) {
free(data);
2019-04-19 05:26:12 +08:00
return PM3_EMALLOC;
} else {
data = newdata;
2019-03-19 03:35:03 +08:00
}
2019-03-10 06:35:06 +08:00
//Send to device
uint32_t bytes_sent = 0;
uint32_t bytes_remaining = datalen;
2019-05-08 00:50:45 +08:00
// fast push mode
conn.block_after_ACK = true;
2019-05-08 07:35:51 +08:00
2019-03-10 07:00:59 +08:00
while (bytes_remaining > 0) {
2019-03-10 06:35:06 +08:00
uint32_t bytes_in_packet = MIN(FLASH_MEM_BLOCK_SIZE, bytes_remaining);
clearCommandBuffer();
2019-05-08 00:50:45 +08:00
2019-04-19 05:26:12 +08:00
SendCommandOLD(CMD_FLASHMEM_WRITE, start_index + bytes_sent, bytes_in_packet, 0, data + bytes_sent, bytes_in_packet);
2019-03-10 06:35:06 +08:00
bytes_remaining -= bytes_in_packet;
bytes_sent += bytes_in_packet;
2019-04-18 18:43:35 +08:00
PacketResponseNG resp;
2019-03-10 07:00:59 +08:00
if (!WaitForResponseTimeout(CMD_ACK, &resp, 2000)) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, "timeout while waiting for reply.");
2019-04-26 19:53:11 +08:00
conn.block_after_ACK = false;
2019-03-10 06:35:06 +08:00
free(data);
2019-04-19 05:26:12 +08:00
return PM3_ETIMEOUT;
2019-03-10 06:35:06 +08:00
}
2019-04-18 05:44:48 +08:00
uint8_t isok = resp.oldarg[0] & 0xFF;
2019-04-19 05:26:12 +08:00
if (!isok) {
2019-05-08 07:35:51 +08:00
conn.block_after_ACK = false;
2019-03-10 06:35:06 +08:00
PrintAndLogEx(FAILED, "Flash write fail [offset %u]", bytes_sent);
2019-04-19 05:26:12 +08:00
return PM3_EFLASH;
}
2019-03-10 06:35:06 +08:00
}
2019-05-08 00:50:45 +08:00
conn.block_after_ACK = false;
free(data);
PrintAndLogEx(SUCCESS, "Wrote "_GREEN_("%u")"bytes to offset "_GREEN_("%u"), datalen, start_index);
2019-04-19 05:26:12 +08:00
return PM3_SUCCESS;
}
static int CmdFlashMemDump(const char *Cmd) {
2019-03-10 06:35:06 +08:00
char filename[FILE_PATH_SIZE] = {0};
uint8_t cmdp = 0;
bool errors = false;
bool print = false;
2019-03-10 06:35:06 +08:00
uint32_t start_index = 0, len = FLASH_MEM_MAX_SIZE;
while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch (tolower(param_getchar(Cmd, cmdp))) {
2019-03-10 07:00:59 +08:00
case 'h':
return usage_flashmem_dump();
2019-03-10 07:00:59 +08:00
case 'l':
len = param_get32ex(Cmd, cmdp + 1, FLASH_MEM_MAX_SIZE, 10);
cmdp += 2;
break;
case 'o':
start_index = param_get32ex(Cmd, cmdp + 1, 0, 10);
cmdp += 2;
break;
case 'p':
print = true;
cmdp += 1;
break;
2019-03-10 07:00:59 +08:00
case 'f':
//File handling
if (param_getstr(Cmd, cmdp + 1, filename, FILE_PATH_SIZE) >= FILE_PATH_SIZE) {
PrintAndLogEx(FAILED, "Filename too long");
errors = true;
break;
}
cmdp += 2;
break;
default:
PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
2019-03-10 06:35:06 +08:00
errors = true;
break;
}
}
//Validations
2019-04-19 05:26:12 +08:00
if (errors || cmdp == 0) {
usage_flashmem_dump();
2019-04-19 05:26:12 +08:00
return PM3_EINVARG;
}
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
uint8_t *dump = calloc(len, sizeof(uint8_t));
2019-03-10 06:35:06 +08:00
if (!dump) {
PrintAndLogDevice(ERR, "error, cannot allocate memory ");
2019-04-19 05:26:12 +08:00
return PM3_EMALLOC;
2019-03-10 06:35:06 +08:00
}
2019-06-01 05:07:09 +08:00
PrintAndLogEx(INFO, "downloading "_YELLOW_("%u")"bytes from flashmem", len);
2019-07-24 03:33:52 +08:00
if (!GetFromDevice(FLASH_MEM, dump, len, start_index, NULL, 0, NULL, -1, true)) {
PrintAndLogEx(FAILED, "ERROR; downloading from flashmemory");
2019-03-10 06:35:06 +08:00
free(dump);
2019-04-19 05:26:12 +08:00
return PM3_EFLASH;
2019-03-10 06:35:06 +08:00
}
if (print) {
print_hex_break(dump, len, 32);
}
if (filename[0] != '\0') {
saveFile(filename, ".bin", dump, len);
saveFileEML(filename, dump, len, 16);
}
2019-03-10 06:35:06 +08:00
free(dump);
2019-04-19 05:26:12 +08:00
return PM3_SUCCESS;
}
static int CmdFlashMemWipe(const char *Cmd) {
2019-03-10 06:35:06 +08:00
uint8_t cmdp = 0;
bool errors = false;
bool initalwipe = false;
uint8_t page = 0;
while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch (tolower(param_getchar(Cmd, cmdp))) {
2019-03-10 07:00:59 +08:00
case 'h':
2019-05-08 00:50:45 +08:00
return usage_flashmem_wipe();
2019-03-10 07:00:59 +08:00
case 'p':
page = param_get8ex(Cmd, cmdp + 1, 0, 10);
if (page > 2) {
PrintAndLogEx(WARNING, "page must be 0, 1 or 2");
errors = true;
break;
}
cmdp += 2;
break;
case 'i':
initalwipe = true;
cmdp++;
break;
default:
PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
2019-03-10 06:35:06 +08:00
errors = true;
break;
}
}
//Validations
2019-04-19 05:26:12 +08:00
if (errors || cmdp == 0) {
usage_flashmem_wipe();
return PM3_EINVARG;
}
2019-03-10 06:35:06 +08:00
clearCommandBuffer();
2019-05-08 00:50:45 +08:00
SendCommandMIX(CMD_FLASHMEM_WIPE, page, initalwipe, 0, NULL, 0);
2019-04-18 18:43:35 +08:00
PacketResponseNG resp;
2019-03-10 07:00:59 +08:00
if (!WaitForResponseTimeout(CMD_ACK, &resp, 8000)) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, "timeout while waiting for reply.");
2019-04-19 05:26:12 +08:00
return PM3_ETIMEOUT;
2019-03-10 06:35:06 +08:00
}
2019-04-18 05:44:48 +08:00
uint8_t isok = resp.oldarg[0] & 0xFF;
2019-03-10 06:35:06 +08:00
if (isok)
PrintAndLogEx(SUCCESS, "Flash WIPE ok");
2019-04-19 05:26:12 +08:00
else {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(FAILED, "Flash WIPE failed");
2019-04-19 05:26:12 +08:00
return PM3_EFLASH;
}
2019-03-10 06:35:06 +08:00
2019-04-19 05:26:12 +08:00
return PM3_SUCCESS;
}
static int CmdFlashMemInfo(const char *Cmd) {
2019-03-10 06:35:06 +08:00
uint8_t sha_hash[20] = {0};
mbedtls_rsa_context rsa;
uint8_t cmdp = 0;
bool errors = false, shall_write = false, shall_sign = false;
while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch (tolower(param_getchar(Cmd, cmdp))) {
2019-03-10 07:00:59 +08:00
case 'h':
2019-05-08 00:50:45 +08:00
return usage_flashmem_info();
2019-03-10 07:00:59 +08:00
case 's': {
shall_sign = true;
cmdp++;
break;
}
case 'w':
shall_write = true;
cmdp++;
break;
default:
PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
errors = true;
break;
2019-03-10 06:35:06 +08:00
}
}
//Validations
2019-04-19 05:26:12 +08:00
if (errors) {
usage_flashmem_info();
return PM3_EINVARG;
}
2019-03-10 06:35:06 +08:00
clearCommandBuffer();
SendCommandNG(CMD_FLASHMEM_INFO, NULL, 0);
2019-04-18 18:43:35 +08:00
PacketResponseNG resp;
2019-03-10 07:00:59 +08:00
if (!WaitForResponseTimeout(CMD_ACK, &resp, 2500)) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, "timeout while waiting for reply.");
2019-04-19 05:26:12 +08:00
return PM3_ETIMEOUT;
2019-03-10 06:35:06 +08:00
}
2019-04-18 05:44:48 +08:00
uint8_t isok = resp.oldarg[0] & 0xFF;
2019-03-10 06:35:06 +08:00
if (!isok) {
PrintAndLogEx(FAILED, "failed");
2019-04-19 05:26:12 +08:00
return PM3_EFLASH;
2019-03-10 06:35:06 +08:00
}
// validate signature here
rdv40_validation_t mem;
2019-04-18 05:44:48 +08:00
memcpy(&mem, (rdv40_validation_t *)resp.data.asBytes, sizeof(rdv40_validation_t));
2019-03-10 06:35:06 +08:00
// Flash ID hash (sha1)
2019-03-10 07:00:59 +08:00
mbedtls_sha1(mem.flashid, sizeof(mem.flashid), sha_hash);
2019-03-10 06:35:06 +08:00
// print header
PrintAndLogEx(INFO, "\n--- Flash memory Information ---------");
PrintAndLogEx(INFO, "-------------------------------------------------------------");
2019-03-10 07:00:59 +08:00
PrintAndLogEx(INFO, "ID | %s", sprint_hex(mem.flashid, sizeof(mem.flashid)));
2019-03-10 06:35:06 +08:00
PrintAndLogEx(INFO, "SHA1 | %s", sprint_hex(sha_hash, sizeof(sha_hash)));
PrintAndLogEx(INFO, "RSA SIGNATURE |");
2019-03-10 07:00:59 +08:00
print_hex_break(mem.signature, sizeof(mem.signature), 32);
//-------------------------------------------------------------------------------
2019-05-31 17:25:49 +08:00
// RRG Public RSA Key
//
2019-05-31 17:25:49 +08:00
// public key Exponent E
2019-06-08 03:40:33 +08:00
#define RSA_E "010001"
2019-05-31 17:25:49 +08:00
// public key modulus N
2019-06-08 03:40:33 +08:00
#define RSA_N "E28D809BF323171D11D1ACA4C32A5B7E0A8974FD171E75AD120D60E9B76968FF4B0A6364AE50583F9555B8EE1A725F279E949246DF0EFCE4C02B9F3ACDCC623F9337F21C0C066FFB703D8BFCB5067F309E056772096642C2B1A8F50305D5EC33DB7FB5A3C8AC42EB635AE3C148C910750ABAA280CE82DC2F180F49F30A1393B5"
2019-05-31 17:25:49 +08:00
//-------------------------------------------------------------------------------
// Example RSA-1024 keypair, for test purposes (from common/polarssl/rsa.c)
//
// private key Exponent D
#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2019-03-10 07:00:59 +08:00
"66CA472BC44D253102F8B4A9D3BFA750" \
"91386C0077937FE33FA3252D28855837" \
"AE1B484A8A9A45F7EE8C0C634F99E8CD" \
"DF79C5CE07EE72C7F123142198164234" \
"CABB724CF78B8173B9F880FC86322407" \
"AF1FEDFDDE2BEB674CA15F3E81A1521E" \
"071513A1E85B5DFA031F21ECAE91A34D"
// prime P
#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2019-03-10 07:00:59 +08:00
"2C01CAD19EA484A87EA4377637E75500" \
"FCB2005C5C7DD6EC4AC023CDA285D796" \
"C3D9E75E1EFC42488BB4F1D13AC30A57"
// prime Q
#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2019-03-10 07:00:59 +08:00
"E211C2B9E5DB1ED0BF61D0D9899620F4" \
"910E4168387E3C30AA1E00C339A79508" \
"8452DD96A9A5EA5D9DCA68DA636032AF"
#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
2019-03-10 07:00:59 +08:00
"3C94D22288ACD763FD8E5600ED4A702D" \
"F84198A5F06C2E72236AE490C93F07F8" \
"3CC559CD27BC2D1CA488811730BB5725"
#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
2019-03-10 07:00:59 +08:00
"D8AAEA56749EA28623272E4F7D0592AF" \
"7C1F1313CAC9471B5C523BFE592F517B" \
"407A1BD76C164B93DA2D32A383E58357"
#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
2019-03-10 07:00:59 +08:00
"F38D18D2B2F0E2DD275AA977E2BF4411" \
"F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
"A74206CEC169D74BF5A8C50D6F48EA08"
#define KEY_LEN 128
2019-03-10 06:35:06 +08:00
mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0);
rsa.len = KEY_LEN;
2019-03-10 07:00:59 +08:00
mbedtls_mpi_read_string(&rsa.N, 16, RSA_N);
mbedtls_mpi_read_string(&rsa.E, 16, RSA_E);
mbedtls_mpi_read_string(&rsa.D, 16, RSA_D);
mbedtls_mpi_read_string(&rsa.P, 16, RSA_P);
mbedtls_mpi_read_string(&rsa.Q, 16, RSA_Q);
mbedtls_mpi_read_string(&rsa.DP, 16, RSA_DP);
mbedtls_mpi_read_string(&rsa.DQ, 16, RSA_DQ);
mbedtls_mpi_read_string(&rsa.QP, 16, RSA_QP);
2019-03-10 06:35:06 +08:00
PrintAndLogEx(INFO, "KEY length | %d", KEY_LEN);
2019-03-10 07:00:59 +08:00
bool is_keyok = (mbedtls_rsa_check_pubkey(&rsa) == 0 || mbedtls_rsa_check_privkey(&rsa) == 0);
2019-03-10 06:35:06 +08:00
if (is_keyok)
PrintAndLogEx(SUCCESS, "RSA key validation ok");
else
PrintAndLogEx(FAILED, "RSA key validation failed");
//
uint8_t from_device[KEY_LEN];
uint8_t sign[KEY_LEN];
// to be verified
memcpy(from_device, mem.signature, KEY_LEN);
// to be signed (all zeros
memset(sign, 0, KEY_LEN);
// Signing (private key)
if (shall_sign) {
2019-03-10 07:00:59 +08:00
int is_signed = mbedtls_rsa_pkcs1_sign(&rsa, NULL, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 20, sha_hash, sign);
2019-03-10 06:35:06 +08:00
if (is_signed == 0)
PrintAndLogEx(SUCCESS, "RSA Signing ok");
else
PrintAndLogEx(FAILED, "RSA Signing failed");
if (shall_write) {
// save to mem
clearCommandBuffer();
2019-04-19 05:26:12 +08:00
SendCommandOLD(CMD_FLASHMEM_WRITE, FLASH_MEM_SIGNATURE_OFFSET, FLASH_MEM_SIGNATURE_LEN, 0, sign, sizeof(sign));
2019-03-10 07:00:59 +08:00
if (!WaitForResponseTimeout(CMD_ACK, &resp, 2000)) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, "timeout while waiting for reply.");
} else {
2019-04-18 05:44:48 +08:00
if (!resp.oldarg[0])
2019-03-10 06:35:06 +08:00
PrintAndLogEx(FAILED, "Writing signature failed");
else
PrintAndLogEx(SUCCESS, "Writing signature ok [offset: %u]", FLASH_MEM_SIGNATURE_OFFSET);
}
}
PrintAndLogEx(INFO, "Signed | ");
2019-03-10 07:00:59 +08:00
print_hex_break(sign, sizeof(sign), 32);
2019-03-10 06:35:06 +08:00
}
// Verify (public key)
2019-03-10 07:00:59 +08:00
int is_verified = mbedtls_rsa_pkcs1_verify(&rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 20, sha_hash, from_device);
2019-03-10 06:35:06 +08:00
if (is_verified == 0)
PrintAndLogEx(SUCCESS, "RSA Verification ok");
else
PrintAndLogEx(FAILED, "RSA Verification failed");
mbedtls_rsa_free(&rsa);
2019-04-19 05:26:12 +08:00
return PM3_SUCCESS;
}
static command_t CommandTable[] = {
{"help", CmdHelp, AlwaysAvailable, "This help"},
{"spiffs", CmdFlashMemSpiFFS, IfPm3Flash, "High level SPI FileSystem Flash manipulation [rdv40]"},
{"spibaud", CmdFlashmemSpiBaudrate, IfPm3Flash, "Set Flash memory Spi baudrate [rdv40]"},
{"info", CmdFlashMemInfo, IfPm3Flash, "Flash memory information [rdv40]"},
{"load", CmdFlashMemLoad, IfPm3Flash, "Load data into flash memory [rdv40]"},
{"dump", CmdFlashMemDump, IfPm3Flash, "Dump data from flash memory [rdv40]"},
{"wipe", CmdFlashMemWipe, IfPm3Flash, "Wipe data from flash memory [rdv40]"},
{NULL, NULL, NULL, NULL}
};
static int CmdHelp(const char *Cmd) {
(void)Cmd; // Cmd is not used so far
CmdsHelp(CommandTable);
2019-04-19 05:26:12 +08:00
return PM3_SUCCESS;
}
int CmdFlashMem(const char *Cmd) {
clearCommandBuffer();
2019-04-19 06:42:25 +08:00
return CmdsParse(CommandTable, Cmd);
}