Merge branch 'master' into purring-basilisk

Signed-off-by: JLitewski <hackhalotwo@gmail.com>
This commit is contained in:
JLitewski authored and GitHub committed 2024-04-22 06:42:45 -04:00
commit 6097c531c8
50 files changed
+1544 -1560

No files matched your search

+9 -46
View File
@@ -40,11 +40,7 @@ static bool TestKVV(void) {
bool res = memcmp(KeyKvv, kvv, CIPURSE_KVV_LENGTH) == 0;
if (res)
PrintAndLogEx(INFO, "kvv.............. " _GREEN_("passed"));
else
PrintAndLogEx(ERR, "kvv.............. " _RED_("fail"));
PrintAndLogEx(SUCCESS, "kvv.............. ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -58,11 +54,7 @@ static bool TestISO9797M2(void) {
res = res && (FindISO9797M2PaddingDataLen(data, ddatalen) == 4);
if (res)
PrintAndLogEx(INFO, "ISO9797M2........ " _GREEN_("passed"));
else
PrintAndLogEx(ERR, "ISO9797M2........ " _RED_("fail"));
PrintAndLogEx(SUCCESS, "ISO9797M2........ ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -92,11 +84,7 @@ static bool TestSMI(void) {
res = res && (CipurseCGetSMI(&ctx, false) == 0x88);
res = res && (CipurseCGetSMI(&ctx, true) == 0x89);
if (res)
PrintAndLogEx(INFO, "SMI.............. " _GREEN_("passed"));
else
PrintAndLogEx(ERR, "SMI.............. " _RED_("fail"));
PrintAndLogEx(SUCCESS, "SMI.............. ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -115,15 +103,10 @@ static bool TestMIC(void) {
res = res && (CipurseCCheckMIC(TestData, 6, mic));
if (res)
PrintAndLogEx(INFO, "MIC.............. " _GREEN_("passed"));
else
PrintAndLogEx(ERR, "MIC.............. " _RED_("fail"));
PrintAndLogEx(SUCCESS, "MIC.............. ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
static bool TestAuth(void) {
CipurseContext_t ctx = {0};
CipurseCClearContext(&ctx);
@@ -159,11 +142,7 @@ static bool TestAuth(void) {
uint8_t framekey[] = {0xCF, 0x6F, 0x3A, 0x47, 0xFC, 0xAC, 0x8D, 0x38, 0x25, 0x75, 0x8B, 0xFC, 0x8B, 0x61, 0x68, 0xF3};
res = res && (memcmp(ctx.frameKey, framekey, sizeof(framekey)) == 0);
if (res)
PrintAndLogEx(INFO, "Auth............. " _GREEN_("passed"));
else
PrintAndLogEx(ERR, "Auth............. " _RED_("fail"));
PrintAndLogEx(SUCCESS, "Auth............. ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -211,11 +190,7 @@ static bool TestMAC(void) {
uint8_t framekey4[] = {0xA0, 0x65, 0x1A, 0x62, 0x56, 0x5D, 0xD7, 0xC9, 0x32, 0xAE, 0x1D, 0xE0, 0xCF, 0x8D, 0xC1, 0xB9};
res = res && (memcmp(ctx.frameKey, framekey4, sizeof(framekey4)) == 0);
if (res)
PrintAndLogEx(INFO, "channel MAC...... " _GREEN_("passed"));
else
PrintAndLogEx(ERR, "channel MAC...... " _RED_("fail"));
PrintAndLogEx(SUCCESS, "channel MAC...... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -259,11 +234,7 @@ static bool TestEncDec(void) {
res = res && (dstdatalen == 16);
res = res && (memcmp(dstdata, TestData, 16) == 0);
if (res)
PrintAndLogEx(INFO, "channel EncDec... " _GREEN_("passed"));
else
PrintAndLogEx(ERR, "channel EncDec... " _RED_("fail"));
PrintAndLogEx(SUCCESS, "channel EncDec... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -351,11 +322,7 @@ static bool TestAPDU(void) {
res = res && (memcmp(test6, dstdata, dstdatalen) == 0);
res = res && (sw == 0xccdd);
if (res)
PrintAndLogEx(INFO, "apdu............. " _GREEN_("passed"));
else
PrintAndLogEx(ERR, "apdu............. " _RED_("fail"));
PrintAndLogEx(SUCCESS, "apdu............. ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -374,11 +341,7 @@ bool CIPURSETest(bool verbose) {
res = res && TestAPDU();
PrintAndLogEx(INFO, "---------------------------");
if (res)
PrintAndLogEx(SUCCESS, " Tests [ %s ]", _GREEN_("ok"));
else
PrintAndLogEx(FAILED, " Tests [ %s ]", _RED_("fail"));
PrintAndLogEx(SUCCESS, "Tests ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
PrintAndLogEx(NORMAL, "");
return res;
}
+2 -15
View File
@@ -2769,7 +2769,7 @@ static int CmdAsn1Decoder(const char *Cmd) {
void *argtable[] = {
arg_param_begin,
arg_str0("d", NULL, "<hex>", "ASN1 encoded byte array"),
arg_lit0("t", "test", "perform selftest"),
arg_lit0("t", "test", "perform self test"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, false);
@@ -2798,17 +2798,14 @@ static int CmdDiff(const char *Cmd) {
"data diff -w 4 -a hf-mfu-01020304.bin -b hf-mfu-04030201.bin\n"
"data diff -a fileA -b fileB\n"
"data diff -a fileA --eb\n"
// "data diff -a fileA --cb\n"
"data diff --fa fileA -b fileB\n"
"data diff --fa fileA --fb fileB\n"
// "data diff --ea --cb\n"
);
void *argtable[] = {
arg_param_begin,
arg_str0("a", NULL, "<fn>", "input file name A"),
arg_str0("b", NULL, "<fn>", "input file name B"),
// arg_lit0(NULL, "cb", "magic gen1 <hf mf csave>"),
arg_lit0(NULL, "eb", "emulator memory <hf mf esave>"),
arg_str0(NULL, "fa", "<fn>", "input spiffs file A"),
arg_str0(NULL, "fb", "<fn>", "input spiffs file B"),
@@ -2825,7 +2822,6 @@ static int CmdDiff(const char *Cmd) {
char filenameB[FILE_PATH_SIZE] = {0};
CLIParamStrToBuf(arg_get_str(ctx, 2), (uint8_t *)filenameB, FILE_PATH_SIZE, &fnlenB);
// bool use_c = arg_get_lit(ctx, 3);
bool use_e = arg_get_lit(ctx, 3);
// SPIFFS filename A
@@ -2930,14 +2926,6 @@ static int CmdDiff(const char *Cmd) {
}
}
// dump magic card memory
/*
if (use_c) {
PrintAndLogEx(INFO, " To be implemented, feel free to contribute!");
return PM3_ENOTIMPL;
}
*/
size_t biggest = (datalenA > datalenB) ? datalenA : datalenB;
PrintAndLogEx(DEBUG, "data len: %zu A %zu B %zu", biggest, datalenA, datalenB);
@@ -2949,7 +2937,6 @@ static int CmdDiff(const char *Cmd) {
PrintAndLogEx(INFO, "inB null");
}
char hdr0[400] = {0};
int hdr_sln = (width * 4) + 2;
@@ -3395,7 +3382,7 @@ static int CmdAtrLookup(const char *Cmd) {
void *argtable[] = {
arg_param_begin,
arg_str0("d", NULL, "<hex>", "ASN1 encoded byte array"),
// arg_lit0("t", "test", "perform selftest"),
// arg_lit0("t", "test", "perform self test"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, false);
+13 -3
View File
@@ -2735,11 +2735,14 @@ static int CmdHF15CSetUID(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "hf 15 csetuid",
"Set UID for magic Chinese card (only works with such cards)\n",
"hf 15 csetuid -u E011223344556677");
"hf 15 csetuid -u E011223344556677 -> use gen1 command\n"
"hf 15 csetuid -u E011223344556677 --v2 -> use gen2 command"
);
void *argtable[] = {
arg_param_begin,
arg_str1("u", "uid", "<hex>", "UID, 8 hex bytes"),
arg_lit0("2", "v2", "Use gen2 magic command"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, false);
@@ -2750,6 +2753,7 @@ static int CmdHF15CSetUID(const char *Cmd) {
int uidlen = 0;
CLIGetHexWithReturn(ctx, 1, payload.uid, &uidlen);
bool use_v2 = arg_get_lit(ctx, 2);
CLIParserFree(ctx);
if (uidlen != HF15_UID_LENGTH) {
@@ -2775,8 +2779,14 @@ static int CmdHF15CSetUID(const char *Cmd) {
PrintAndLogEx(INFO, "Writing...");
PacketResponseNG resp;
clearCommandBuffer();
SendCommandNG(CMD_HF_ISO15693_CSETUID, (uint8_t *)&payload, sizeof(payload));
if (WaitForResponseTimeout(CMD_HF_ISO15693_CSETUID, &resp, 2000) == false) {
uint16_t cmd = CMD_HF_ISO15693_CSETUID;
if (use_v2) {
cmd = CMD_HF_ISO15693_CSETUID_V2;
}
SendCommandNG(cmd, (uint8_t *)&payload, sizeof(payload));
if (WaitForResponseTimeout(cmd, &resp, 2000) == false) {
PrintAndLogEx(WARNING, "timeout while waiting for reply");
DropField();
return PM3_ESOFT;
+2
View File
@@ -93,6 +93,8 @@ static int CmdHFCryptoRFSniff(const char *Cmd) {
clearCommandBuffer();
SendCommandNG(CMD_HF_ISO14443B_SNIFF, NULL, 0);
PacketResponseNG resp;
WaitForResponse(CMD_HF_ISO14443B_SNIFF, &resp);
PrintAndLogEx(HINT, "Try `" _YELLOW_("hf cryptorf list") "` to view captured tracelog");
PrintAndLogEx(HINT, "Try `" _YELLOW_("trace save -f hf_cryptorf_mytrace") "` to save tracelog for later analysing");
+8 -6
View File
@@ -2147,8 +2147,9 @@ static int CmdHFiClassDump(const char *Cmd) {
write_dump:
if (have_credit_key && pagemap != 0x01 && aa2_success == false)
if (have_credit_key && pagemap != 0x01 && aa2_success == false) {
PrintAndLogEx(INFO, "Reading AA2 failed. dumping AA1 data to file");
}
// print the dump
printIclassDumpContents(tag_data, 1, (bytes_got / 8), bytes_got, dense_output);
@@ -2852,8 +2853,8 @@ static int CmdHFiClass_loclass(const char *Cmd) {
void *argtable[] = {
arg_param_begin,
arg_str0("f", "file", "<fn>", "filename with nr/mac data from `hf iclass sim -t 2` "),
arg_lit0(NULL, "test", "Perform self-test"),
arg_lit0(NULL, "long", "Perform self-test, including long ones"),
arg_lit0(NULL, "test", "Perform self test"),
arg_lit0(NULL, "long", "Perform self test, including long ones"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, false);
@@ -3776,9 +3777,10 @@ out:
static int CmdHFiClassLookUp(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "hf iclass lookup",
"Lookup keys takes some sniffed trace data and tries to verify what key was used against a dictionary file",
"This command take sniffed trace data and try to recovery a iCLASS Standard or iCLASS Elite key.",
"hf iclass lookup --csn 9655a400f8ff12e0 --epurse f0ffffffffffffff --macs 0000000089cb984b -f iclass_default_keys.dic\n"
"hf iclass lookup --csn 9655a400f8ff12e0 --epurse f0ffffffffffffff --macs 0000000089cb984b -f iclass_default_keys.dic --elite");
"hf iclass lookup --csn 9655a400f8ff12e0 --epurse f0ffffffffffffff --macs 0000000089cb984b -f iclass_default_keys.dic --elite"
);
void *argtable[] = {
arg_param_begin,
@@ -4628,9 +4630,9 @@ static int CmdHFiClassSAM(const char *Cmd) {
}
static command_t CommandTable[] = {
{"-----------", CmdHelp, AlwaysAvailable, "--------------------- " _CYAN_("General") " ---------------------"},
{"help", CmdHelp, AlwaysAvailable, "This help"},
{"list", CmdHFiClassList, AlwaysAvailable, "List iclass history"},
// {"-----------", CmdHelp, AlwaysAvailable, "--------------------- " _CYAN_("General") " ---------------------"},
{"-----------", CmdHelp, IfPm3Iclass, "------------------- " _CYAN_("Operations") " -------------------"},
// {"clone", CmdHFiClassClone, IfPm3Iclass, "Create a HID credential to Picopass / iCLASS tag"},
{"dump", CmdHFiClassDump, IfPm3Iclass, "Dump Picopass / iCLASS tag to file"},
+2 -2
View File
@@ -238,7 +238,7 @@ static void jooki_print(uint8_t *b64, uint8_t *result, bool verbose) {
static int jooki_selftest(void) {
PrintAndLogEx(INFO, "======== " _CYAN_("selftest") " ===========================================");
PrintAndLogEx(INFO, "======== " _CYAN_("self test") " ===========================================");
for (int i = 0; i < ARRAYLEN(jooks); i++) {
if (strlen(jooks[i].b64) == 0)
continue;
@@ -296,7 +296,7 @@ static int CmdHF14AJookiEncode(const char *Cmd) {
arg_param_begin,
arg_str0("u", "uid", "<hex>", "uid bytes"),
arg_lit0("r", NULL, "read uid from tag instead"),
arg_lit0("t", NULL, "selftest"),
arg_lit0("t", NULL, "self test"),
arg_lit0("v", "verbose", "verbose output"),
arg_lit0(NULL, "dragon", "figurine type"),
arg_lit0(NULL, "fox", "figurine type"),
+4 -1
View File
@@ -704,6 +704,9 @@ void annotateIso15693(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize) {
case ISO15693_READ_SIGNATURE:
snprintf(exp, size, "READ_SIGNATURE");
return;
case ISO15693_MAGIC_WRITE:
snprintf(exp, size, "MAGIC_WRITEBLOCK");
return;
default:
break;
}
@@ -2223,7 +2226,7 @@ bool DecodeMifareData(uint8_t *cmd, uint8_t cmdsize, uint8_t *parity, bool isRes
char sat[5] = {0, 0, 0, 0, 0};
mf_get_paritybinstr(sat, AuthData.at_enc, AuthData.at_enc_par);
PrintAndLogEx(NORMAL, "Nested authentication detected. ");
PrintAndLogEx(NORMAL, "Nested authentication detected!");
PrintAndLogEx(NORMAL, "tools/mf_nonce_brute/mf_nonce_brute %x %x %s %x %x %s %x %s %s\n"
, AuthData.uid
, AuthData.nt_enc
+30 -8
View File
@@ -41,15 +41,17 @@ static int sendPing(void) {
SendCommandNG(CMD_PING, NULL, 0);
clearCommandBuffer();
PacketResponseNG resp;
if (!WaitForResponseTimeout(CMD_PING, &resp, 1000))
if (WaitForResponseTimeout(CMD_PING, &resp, 1000) == false) {
return PM3_ETIMEOUT;
}
return PM3_SUCCESS;
}
static int sendTry(uint8_t fmtlen, uint32_t fc, uint32_t cn, uint32_t delay, uint8_t *bits, size_t bs_len, bool verbose) {
if (verbose)
if (verbose) {
PrintAndLogEx(INFO, "Trying FC: " _YELLOW_("%u") " CN: " _YELLOW_("%u"), fc, cn);
}
if (getAWIDBits(fmtlen, fc, cn, bits) != PM3_SUCCESS) {
PrintAndLogEx(ERR, "Error with tag bitstream generation.");
@@ -205,6 +207,11 @@ int demodAWID(bool verbose) {
free(bits);
return PM3_ESOFT;
}
char binstr[68] = {0};
binarray_2_binstr(binstr, (char *)bits, size);
PrintAndLogEx(DEBUG, "no parity... %s", binstr);
// ok valid card found!
// Index map
@@ -230,35 +237,47 @@ int demodAWID(bool verbose) {
uint8_t fmtLen = bytebits_to_byte(bits, 8);
switch (fmtLen) {
case 26:
case 26: {
fc = bytebits_to_byte(bits + 9, 8);
cardnum = bytebits_to_byte(bits + 17, 16);
code1 = bytebits_to_byte(bits + 8, fmtLen);
PrintAndLogEx(SUCCESS, "AWID - len: " _GREEN_("%d") " FC: " _GREEN_("%d") " Card: " _GREEN_("%u") " - Wiegand: " _GREEN_("%x") ", Raw: %08x%08x%08x", fmtLen, fc, cardnum, code1, rawHi2, rawHi, rawLo);
break;
case 34:
}
case 34: {
fc = bytebits_to_byte(bits + 9, 8);
cardnum = bytebits_to_byte(bits + 17, 24);
code1 = bytebits_to_byte(bits + 8, (fmtLen - 32));
code2 = bytebits_to_byte(bits + 8 + (fmtLen - 32), 32);
PrintAndLogEx(SUCCESS, "AWID - len: " _GREEN_("%d") " FC: " _GREEN_("%d") " Card: " _GREEN_("%u") " - Wiegand: " _GREEN_("%x%08x") ", Raw: %08x%08x%08x", fmtLen, fc, cardnum, code1, code2, rawHi2, rawHi, rawLo);
break;
case 37:
}
case 36: {
fc = bytebits_to_byte(bits + 14, 11);
cardnum = bytebits_to_byte(bits + 25, 18);
code1 = bytebits_to_byte(bits + 8, (fmtLen - 32));
code2 = bytebits_to_byte(bits + 8 + (fmtLen - 32), 32);
PrintAndLogEx(SUCCESS, "AWID - len: " _GREEN_("%d") " FC: " _GREEN_("%d") " Card: " _GREEN_("%u") " - Wiegand: " _GREEN_("%x%08x") ", Raw: %08x%08x%08x", fmtLen, fc, cardnum, code1, code2, rawHi2, rawHi, rawLo);
break;
}
case 37: {
fc = bytebits_to_byte(bits + 9, 13);
cardnum = bytebits_to_byte(bits + 22, 18);
code1 = bytebits_to_byte(bits + 8, (fmtLen - 32));
code2 = bytebits_to_byte(bits + 8 + (fmtLen - 32), 32);
PrintAndLogEx(SUCCESS, "AWID - len: " _GREEN_("%d")" FC: " _GREEN_("%d")" Card: " _GREEN_("%u") " - Wiegand: " _GREEN_("%x%08x") ", Raw: %08x%08x%08x", fmtLen, fc, cardnum, code1, code2, rawHi2, rawHi, rawLo);
break;
}
// case 40:
// break;
case 50:
case 50: {
fc = bytebits_to_byte(bits + 9, 16);
cardnum = bytebits_to_byte(bits + 25, 32);
code1 = bytebits_to_byte(bits + 8, (fmtLen - 32));
code2 = bytebits_to_byte(bits + 8 + (fmtLen - 32), 32);
PrintAndLogEx(SUCCESS, "AWID - len: " _GREEN_("%d") " FC: " _GREEN_("%d") " Card: " _GREEN_("%u") " - Wiegand: " _GREEN_("%x%08x") ", Raw: %08x%08x%08x", fmtLen, fc, cardnum, code1, code2, rawHi2, rawHi, rawLo);
break;
}
default:
if (fmtLen > 32) {
cardnum = bytebits_to_byte(bits + 8 + (fmtLen - 17), 16);
@@ -288,7 +307,9 @@ static int CmdAWIDDemod(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "lf awid demod",
"Try to find AWID Prox preamble, if found decode / descramble data",
"lf awid demod"
"lf awid demod\n"
"lf awid demod --raw "
);
void *argtable[] = {
@@ -566,9 +587,10 @@ static int CmdAWIDBrute(const char *Cmd) {
static command_t CommandTable[] = {
{"help", CmdHelp, AlwaysAvailable, "this help"},
{"brute", CmdAWIDBrute, IfPm3Lf, "bruteforce card number against reader"},
{"clone", CmdAWIDClone, IfPm3Lf, "clone AWID tag to T55x7, Q5/T5555 or EM4305/4469"},
{"demod", CmdAWIDDemod, AlwaysAvailable, "demodulate an AWID FSK tag from the GraphBuffer"},
{"reader", CmdAWIDReader, IfPm3Lf, "attempt to read and extract tag data"},
{"clone", CmdAWIDClone, IfPm3Lf, "clone AWID tag to T55x7, Q5/T5555 or EM4305/4469"},
{"sim", CmdAWIDSim, IfPm3Lf, "simulate AWID tag"},
{"brute", CmdAWIDBrute, IfPm3Lf, "bruteforce card number against reader"},
{"watch", CmdAWIDWatch, IfPm3Lf, "continuously watch for cards. Reader mode"},
+3 -3
View File
@@ -1750,9 +1750,9 @@ int CmdEM4x05Chk(const char *Cmd) {
res = loadFileDICTIONARY_safe(filename, (void **) &keyBlock, 4, &keycount);
if (res != PM3_SUCCESS || keycount == 0 || keyBlock == NULL) {
PrintAndLogEx(WARNING, "no keys found in file");
if (keyBlock != NULL)
if (keyBlock != NULL) {
free(keyBlock);
}
return PM3_ESOFT;
}
@@ -2634,7 +2634,7 @@ static command_t CommandTable[] = {
{"-----------", CmdHelp, AlwaysAvailable, "----------------------- " _CYAN_("Operations") " -----------------------"},
{"clonehelp", CmdEM4x05CloneHelp, IfPm3Lf, "Shows the available clone commands"},
{"brute", CmdEM4x05Brute, IfPm3Lf, "Bruteforce password"},
{"chk", CmdEM4x05Chk, IfPm3Lf, "Check passwords from dictionary"},
{"chk", CmdEM4x05Chk, IfPm3Lf, "Check passwords"},
{"config", CmdEM4x05Config, AlwaysAvailable, "Create common configuration words"},
{"demod", CmdEM4x05Demod, AlwaysAvailable, "Demodulate a EM4x05/EM4x69 tag from the GraphBuffer"},
{"dump", CmdEM4x05Dump, IfPm3Lf, "Dump EM4x05/EM4x69 tag"},
+14 -7
View File
@@ -63,10 +63,10 @@ static void em4x50_print_result(const em4x50_word_t *words, int fwr, int lwr) {
s = _YELLOW_("control cfg ( locked )");
break;
case EM4X50_DEVICE_SERIAL:
s = _YELLOW_("device serial number ( RO )");
s = _YELLOW_("serial number ( RO )");
break;
case EM4X50_DEVICE_ID:
s = _YELLOW_("device identification ( RO )");
s = _YELLOW_("device id ( RO )");
break;
default:
s = "user data";
@@ -602,8 +602,11 @@ int read_em4x50_uid(void) {
};
em4x50_word_t words[EM4X50_NO_WORDS];
int res = em4x50_read(&etd, words);
if (res == PM3_SUCCESS)
if (res == PM3_SUCCESS) {
PrintAndLogEx(INFO, " Serial: " _GREEN_("%s"), sprint_hex(words[EM4X50_DEVICE_SERIAL].byte, 4));
} else {
SendCommandNG(CMD_BREAK_LOOP, NULL, 0);
}
return res;
}
@@ -612,7 +615,10 @@ int read_em4x50_uid(void) {
// read protected) -> selective read mode
int em4x50_read(em4x50_data_t *etd, em4x50_word_t *out) {
em4x50_data_t edata = { .pwd_given = false, .addr_given = false };
em4x50_data_t edata = {
.pwd_given = false,
.addr_given = false,
};
if (etd != NULL) {
edata = *etd;
@@ -630,9 +636,10 @@ int em4x50_read(em4x50_data_t *etd, em4x50_word_t *out) {
return PM3_ESOFT;
}
uint8_t *data = resp.data.asBytes;
em4x50_read_data_response_t *o = (em4x50_read_data_response_t *)resp.data.asBytes;
em4x50_word_t words[EM4X50_NO_WORDS] = {0};
em4x50_prepare_result(data, etd->addresses & 0xFF, (etd->addresses >> 8) & 0xFF, words);
em4x50_prepare_result((uint8_t *)o->words, etd->addresses & 0xFF, (etd->addresses >> 8) & 0xFF, words);
if (out != NULL) {
memcpy(out, &words, sizeof(em4x50_word_t) * EM4X50_NO_WORDS);
@@ -1331,7 +1338,7 @@ static command_t CommandTable[] = {
{"help", CmdHelp, AlwaysAvailable, "This help"},
{"-----------", CmdHelp, AlwaysAvailable, "--------------------- " _CYAN_("operations") " ---------------------"},
{"brute", CmdEM4x50Brute, IfPm3EM4x50, "Bruteforce attack to find password"},
{"chk", CmdEM4x50Chk, IfPm3EM4x50, "Check passwords from dictionary"},
{"chk", CmdEM4x50Chk, IfPm3EM4x50, "Check passwords"},
{"dump", CmdEM4x50Dump, IfPm3EM4x50, "Dump EM4x50 tag"},
{"info", CmdEM4x50Info, IfPm3EM4x50, "Tag information"},
{"login", CmdEM4x50Login, IfPm3EM4x50, "Login into EM4x50 tag"},
+313 -10
View File
@@ -66,6 +66,265 @@ static t55xx_conf_block_t config = {
};
static t55xx_memory_item_t cardmem[T55x7_BLOCK_COUNT] = {{0}};
/*
#define DC(x) ((x) + 128)
static bool t55xx_is_valid_block0(uint32_t block, uint8_t rfclk, uint8_t pskcf) {
if (block == 0x00) {
return false;
}
// Master key = 6 or 9
if ((((block >> 28)& 0xF) != 0x0) &&
(((block >> 28)& 0xF) != 0x6) &&
(((block >> 28)& 0xF) != 0x9)) {
return false;
}
// X Mode
if ( ((block >> 17) & 1) && ((((block >> 28) & 0xf) == 0x6) || (((block >> 28) & 0xf) == 0x9)) ) {
// X mode fixed 0 bits
if ((block & 0x0F000000) != 0x00) {
return false;
}
} else {
// / Basic Mode fixed 0 bits
if ((block & 0x0FE00106) != 0x00) {
return false;
}
}
// Modulation
if ( (((block >> 12) & 0x1F) != 0x00) && // Direct
(((block >> 12) & 0x1F) != 0x01) && // PSK1
(((block >> 12) & 0x1F) != 0x02) && // PSK2
(((block >> 12) & 0x1F) != 0x03) && // PSK3
(((block >> 12) & 0x1F) != 0x04) && // FSK1
(((block >> 12) & 0x1F) != 0x05) && // FSK2
(((block >> 12) & 0x1F) != 0x06) && // FSK1a
(((block >> 12) & 0x1F) != 0x07) && // FSK2a
(((block >> 12) & 0x1F) != 0x08) && // Manchester
(((block >> 12) & 0x1F) != 0x10) && // Bi-phase
(((block >> 12) & 0x1F) != 0x18) ) { // Reserved
return false;
}
PrintAndLogEx(DEBUG, "suggested block... %08x", block);
// check pskcf
if ((pskcf <= 3) && (((block >> 10) & 0x3) != pskcf)) {
PrintAndLogEx(DEBUG, "fail 6 - %u %u", pskcf, (block >> 10) & 0x3);
return false;
}
uint8_t testSpeed;
// check rfclk
if ((((block >> 17) & 1) == 1) && ((((block >> 28) & 0xf) == 0x6) || (((block >> 28) & 0xf) == 0x9)) ){ // X mode speedBits
testSpeed = (((block >> 18) & 0x3F) * 2) + 2;
} else {
uint8_t basicSpeeds[] = {8,16,32,40,50,64,100,128};
testSpeed = basicSpeeds[(block >> 18) & 0x7];
}
if (testSpeed != rfclk) {
PrintAndLogEx(DEBUG, "fail 7 - %u %u ", testSpeed , rfclk);
return false;
}
return true;
}
static void t55xx_psk1_demod (int *data, uint8_t rfclk, uint8_t pskcf, uint32_t *block) {
if ((rfclk < 8) || (rfclk > 128)) {
return;
}
switch (pskcf) {
case 0: {
pskcf = 2;
break;
}
case 1: {
pskcf = 4;
break;
}
case 2: {
pskcf = 8;
break;
}
default: {
break;
}
}
int startOffset = 1; // where to start reading data samples
int sampleCount = 0; // Counter for 1 bit of samples
int samples0, samples1; // Number of High even and odd bits in a sample.
int startBitOffset = 1; // which bit to start at e.g. for rf/32 1 33 65 ...
int bitCount = 0;
uint32_t myblock = 0;
int offset;
uint8_t drift = 0;
uint8_t tuneOffset = 0;
drift = (rfclk % pskcf); // 50 2 = 1 50 4 = 2
// locate first "0" - high transisiton for correct start offset
while (DC(data[startOffset]) <= (DC(data[startOffset - 1]) + 5)) {
// sampleToggle ^= 1;
startOffset++;
}
// Start sample may be 1 off due to sample alignment with chip modulation
// so seach for the first lower value, and adjust as needed
if (pskcf == 2) {
tuneOffset = startOffset + 1;
while (DC(data[tuneOffset]) >= (DC(data[tuneOffset - 1]) + 5)) {
tuneOffset++;
}
if ((tuneOffset - startOffset - 1) % 2) {
startOffset++;
}
}
uint8_t pskcfidx = 0;
// Get the offset to the first sample of the data block
offset = (rfclk * startBitOffset) + startOffset;
pskcfidx = (drift / 2);
pskcfidx = pskcfidx % pskcf;
// while data my be in the settle period of sampling
// First 18 - 24 bits not usable for reference only
while (offset < 20) {
offset += (32 * rfclk);
}
// Read 1 block of data
for (bitCount = 0; bitCount < 32; bitCount++) {
samples0 = 0;
samples1 = 0;
// Get 1 bit of data
for (sampleCount = 0; sampleCount < rfclk; sampleCount++){
// Count number of even and odd high bits at center to edge
switch (pskcf) {
case 2: {
// if current sample is high
if (DC(data[offset]) > DC(data[offset + 1])) {
if (pskcfidx == 0) {
samples0++;
} else {
samples1++;
}
}
break;
}
case 4: {
// only check pskcf 2nd bit x 1 x x
if (pskcfidx == 1) {
// if current sample is high
if (DC(data[offset]) > DC(data[offset + 2])) {
samples0++;
} else {
samples1++;
}
}
break;
}
case 8: {
if (pskcfidx == 3) { // x x x 1 x x x x // 00041840 : FFFBE7BF
// if current sample is high
if (DC(data[offset]) > DC(data[offset + 4])) {
samples0++;
} else {
samples1++;
}
}
break;
}
default: {
break;
}
}
// If at bit boundary (after first bit) then adjust phase check for drift
if ((sampleCount > 0) && (sampleCount % rfclk) == 0) {
pskcfidx -= drift;
}
offset++;
pskcfidx++;
pskcfidx = pskcfidx % pskcf;
}
myblock <<= 1;
if (samples1 > samples0) {
myblock++;
}
}
*block = myblock;
}
static void t55xx_psk2_demod (int *data, uint8_t rfclk, uint8_t pskcf, uint32_t *block) {
// decode PSK
t55xx_psk1_demod (data, rfclk, pskcf, block);
uint32_t new_block = 0;
uint8_t prev_phase = 1;
// Convert to PSK2
for (int8_t bit = 31; bit >= 0; bit--) {
new_block <<= 1;
if (((*block >> bit) & 1) != prev_phase) {
new_block++;
}
prev_phase = ((*block >> bit) & 1);
}
*block = new_block;
}
static void t55xx_search_config_psk(int *d, int pskV) {
for (uint8_t pskcf = 0; pskcf < 3; pskcf++) {
for (uint8_t speedBits = 0; speedBits < 64; speedBits++) {
uint8_t rfclk = rfclk = (2 * speedBits) + 2;
uint32_t block = 0;
if (pskV == 1) {
t55xx_psk1_demod (d, rfclk, pskcf, &block);
}
if (pskV == 2) {
t55xx_psk2_demod (d, rfclk, pskcf, &block);
}
if (t55xx_is_valid_block0(block, rfclk, pskcf)) {
PrintAndLogEx(SUCCESS, "Valid config block [%08X] - rfclk [%d] - pskcf [%d]", block, rfclk, pskcf);
}
}
}
}
*/
t55xx_conf_block_t Get_t55xx_Config(void) {
return config;
@@ -1203,6 +1462,8 @@ bool t55xxTryDetectModulationEx(uint8_t downlink_mode, bool print_config, uint32
//undo trim samples
restore_bufferS32(saveState, g_GraphBuffer);
g_GridOffset = saveState.offset;
// t55xx_search_config_psk(g_GraphBuffer, 1);
// t55xx_search_config_psk(g_GraphBuffer, 2);
}
}
if (hits == 1) {
@@ -1452,10 +1713,17 @@ static bool testBitRate(uint8_t readRate, uint8_t clk) {
bool test(uint8_t mode, uint8_t *offset, int *fndBitRate, uint8_t clk, bool *Q5) {
if (g_DemodBufferLen < 64) return false;
if (g_DemodBufferLen < 64) {
return false;
}
for (uint8_t idx = 28; idx < 64; idx++) {
uint8_t si = idx;
if (PackBits(si, 28, g_DemodBuffer) == 0x00) continue;
if (PackBits(si, 28, g_DemodBuffer) == 0x00) {
continue;
}
uint8_t safer = PackBits(si, 4, g_DemodBuffer);
si += 4; //master key
@@ -1463,7 +1731,10 @@ bool test(uint8_t mode, uint8_t *offset, int *fndBitRate, uint8_t clk, bool *Q5)
si += 4; //was 7 & +=7+3 //should be only 4 bits if extended mode
// 2nibble must be zeroed.
// moved test to here, since this gets most faults first.
if (resv > 0x00) continue;
if (resv > 0x00) {
continue;
}
int bitRate = PackBits(si, 6, g_DemodBuffer);
si += 6; //bit rate (includes extended mode part of rate)
@@ -1478,20 +1749,33 @@ bool test(uint8_t mode, uint8_t *offset, int *fndBitRate, uint8_t clk, bool *Q5)
//if extended mode
bool extMode = ((safer == 0x6 || safer == 0x9) && extend) ? true : false;
if (!extMode) {
if (bitRate > 7) continue;
if (!testBitRate(bitRate, clk)) continue;
} else { //extended mode bitrate = same function to calc bitrate as em4x05
if (EM4x05_GET_BITRATE(bitRate) != clk) continue;
if (extMode == false) {
if (bitRate > 7) {
continue;
}
if (testBitRate(bitRate, clk) == false) {
continue;
}
} else { //extended mode bitrate = same function to calc bitrate as em4x05
if (EM4x05_GET_BITRATE(bitRate) != clk) {
continue;
}
}
//test modulation
if (!testModulation(mode, modread)) continue;
if (testModulation(mode, modread) == false) {
continue;
}
*fndBitRate = bitRate;
*offset = idx;
*Q5 = false;
return true;
}
if (testQ5(mode, offset, fndBitRate, clk)) {
*Q5 = true;
return true;
@@ -4409,7 +4693,7 @@ static command_t CommandTable[] = {
{"write", CmdT55xxWriteBlock, IfPm3Lf, "Write T55xx block data"},
{"-----------", CmdHelp, AlwaysAvailable, "--------------------- " _CYAN_("recovery") " ---------------------"},
{"bruteforce", CmdT55xxBruteForce, IfPm3Lf, "Simple bruteforce attack to find password"},
{"chk", CmdT55xxChkPwds, IfPm3Lf, "Check passwords from dictionary/flash"},
{"chk", CmdT55xxChkPwds, IfPm3Lf, "Check passwords"},
{"protect", CmdT55xxProtect, IfPm3Lf, "Password protect tag"},
{"recoverpw", CmdT55xxRecoverPW, IfPm3Lf, "Try to recover from bad password write from a cloner"},
{"sniff", CmdT55xxSniff, AlwaysAvailable, "Attempt to recover T55xx commands from sample buffer"},
@@ -4428,3 +4712,22 @@ int CmdLFT55XX(const char *Cmd) {
clearCommandBuffer();
return CmdsParse(CommandTable, Cmd);
}
/*
one of
// Leading 0
lf t55 write -b 3 --pg1 -d 90000800
// 1 of 4
lf t55 write -b 3 --pg1 -d 90000C00
T55xx clone card lock: block 3 page 1 0x00000020 00000000 00000000 00000000 00100000
(this bit in any combo seems to lock the card)
You can have other data in the block write, but if that single bit is set "1" the entire card locks in its current state; no know way to unlock
*/
+4 -4
View File
@@ -153,10 +153,10 @@ int roca_self_test(void) {
int ret = 0;
if (emv_rocacheck(keyp, 64, false)) {
PrintAndLogEx(SUCCESS, "Weak modulus [ %s ]", _GREEN_("PASS"));
PrintAndLogEx(SUCCESS, "Weak modulus ( %s )", _GREEN_("ok"));
} else {
ret++;
PrintAndLogEx(FAILED, "Weak modulus [ %s ]", _RED_("Fail"));
PrintAndLogEx(FAILED, "Weak modulus ( %s )", _RED_("fail"));
}
// negative
@@ -167,9 +167,9 @@ int roca_self_test(void) {
if (emv_rocacheck(keyn, 64, false)) {
ret++;
PrintAndLogEx(FAILED, "Strong modulus [ %s ]", _RED_("Fail"));
PrintAndLogEx(FAILED, "Strong modulus ( %s )", _RED_("fail"));
} else {
PrintAndLogEx(SUCCESS, "Strong modulus [ %s ]", _GREEN_("PASS"));
PrintAndLogEx(SUCCESS, "Strong modulus ( %s )", _GREEN_("ok"));
}
return ret;
}
+4 -4
View File
@@ -427,16 +427,16 @@ static int cda_test_pk(bool verbose) {
int exec_cda_test(bool verbose) {
int ret = cda_test_raw(verbose);
if (ret) {
PrintAndLogEx(WARNING, "CDA raw test: (%s)", _RED_("failed"));
PrintAndLogEx(WARNING, "CDA raw test ( %s )", _RED_("fail"));
return ret;
}
PrintAndLogEx(INFO, "CDA raw test: (%s)", _GREEN_("passed"));
PrintAndLogEx(INFO, "CDA raw test ( %s )", _GREEN_("ok"));
ret = cda_test_pk(verbose);
if (ret) {
PrintAndLogEx(WARNING, "CDA test pk: (%s)", _RED_("failed"));
PrintAndLogEx(WARNING, "CDA test pk ( %s )", _RED_("fail"));
return ret;
}
PrintAndLogEx(INFO, "CDA test pk: (%s)", _GREEN_("passed"));
PrintAndLogEx(INFO, "CDA test pk ( %s )", _GREEN_("ok"));
return 0;
}
+4 -4
View File
@@ -310,16 +310,16 @@ int exec_crypto_test(bool verbose, bool include_slow_tests) {
unsigned int extra_keylengths[] = {1152, 1408, 1984, 3072, 4096};
int ret = test_pk(verbose);
if (ret) {
PrintAndLogEx(WARNING, "Crypto raw test: (%s)", _RED_("failed"));
PrintAndLogEx(WARNING, "Crypto raw test ( %s )", _RED_("fail"));
return ret;
}
PrintAndLogEx(SUCCESS, "Crypto raw test (%s)", _GREEN_("passed"));
PrintAndLogEx(SUCCESS, "Crypto raw test ( %s )", _GREEN_("ok"));
for (int i = 0; i < ARRAYLEN(keylengths); i++) {
unsigned int kl = keylengths[i];
ret = test_genkey(kl, message, kl / 8, verbose);
if (ret) {
PrintAndLogEx(WARNING, "Crypto generate key[%u] test: (%s)", kl, _RED_("failed"));
PrintAndLogEx(WARNING, "Crypto generate key[ %u ] test ( %s )", kl, _RED_("fail"));
return ret;
}
}
@@ -328,7 +328,7 @@ int exec_crypto_test(bool verbose, bool include_slow_tests) {
unsigned int kl = extra_keylengths[i];
ret = test_genkey(kl, message, kl / 8, verbose);
if (ret) {
PrintAndLogEx(WARNING, "Crypto generate key[%u] test: (%s)", kl, _RED_("failed"));
PrintAndLogEx(WARNING, "Crypto generate key[ %u ] test ( %s )", kl, _RED_("fail"));
return ret;
}
}
+2 -2
View File
@@ -111,9 +111,9 @@ int ExecuteCryptoTests(bool verbose, bool ignore_time, bool include_slow_tests)
PrintAndLogEx(INFO, "--------------------------");
if (TestFail)
PrintAndLogEx(FAILED, "\tTest(s) [ %s ]", _RED_("fail"));
PrintAndLogEx(FAILED, "Tests ( %s )", _RED_("fail"));
else
PrintAndLogEx(SUCCESS, "\tTest(s) [ %s ]", _GREEN_("ok"));
PrintAndLogEx(SUCCESS, "Tests ( %s )", _GREEN_("ok"));
return TestFail;
}
+4 -4
View File
@@ -373,16 +373,16 @@ static int dda_test_pk(bool verbose) {
int exec_dda_test(bool verbose) {
int ret = dda_test_raw(verbose);
if (ret) {
PrintAndLogEx(WARNING, "DDA raw test: %s", _RED_("failed"));
PrintAndLogEx(WARNING, "DDA raw test ( %s )", _RED_("fail"));
return ret;
}
PrintAndLogEx(SUCCESS, "DDA raw test: %s", _GREEN_("passed"));
PrintAndLogEx(SUCCESS, "DDA raw test ( %s )", _GREEN_("ok"));
ret = dda_test_pk(verbose);
if (ret) {
PrintAndLogEx(WARNING, "DDA test pk: %s", _RED_("failed"));
PrintAndLogEx(WARNING, "DDA test pk ( %s )", _RED_("fail"));
return ret;
}
PrintAndLogEx(SUCCESS, "DDA test pk: %s", _GREEN_("passed"));
PrintAndLogEx(SUCCESS, "DDA test pk ( %s )", _GREEN_("ok"));
return 0;
}
+4 -4
View File
@@ -262,16 +262,16 @@ static int sda_test_pk(bool verbose) {
int exec_sda_test(bool verbose) {
int ret = sda_test_raw(verbose);
if (ret) {
PrintAndLogEx(WARNING, "SDA raw test: %s", _RED_("failed"));
PrintAndLogEx(WARNING, "SDA raw test ( %s )", _RED_("fa1l"));
return ret;
}
PrintAndLogEx(SUCCESS, "SDA raw test: %s", _GREEN_("passed"));
PrintAndLogEx(SUCCESS, "SDA raw test ( %s )", _GREEN_("ok"));
ret = sda_test_pk(verbose);
if (ret) {
PrintAndLogEx(WARNING, "SDA test pk: %s", _RED_("failed"));
PrintAndLogEx(WARNING, "SDA test pk ( %s )", _RED_("fail"));
return ret;
}
PrintAndLogEx(SUCCESS, "SDA test pk: %s", _GREEN_("passed"));
PrintAndLogEx(SUCCESS, "SDA test pk ( %s )", _GREEN_("ok"));
return 0;
}
+38 -22
View File
@@ -674,11 +674,11 @@ static int DesfireExchangeISONative(bool activate_field, DesfireContext_t *ctx,
}
if (respcode) {
*respcode = 0xff;
*respcode = 0xFF;
}
uint16_t sw = 0;
uint8_t *buf = calloc(DESFIRE_BUFFER_SIZE, 1);
uint8_t *buf = calloc(DESFIRE_BUFFER_SIZE, 1);
if (buf == NULL) {
return PM3_EMALLOC;
}
@@ -698,10 +698,11 @@ static int DesfireExchangeISONative(bool activate_field, DesfireContext_t *ctx,
// tx chaining
size_t sentdatalen = 0;
while (datalen >= sentdatalen) {
if (datalen - sentdatalen > DESFIRE_TX_FRAME_MAX_LEN)
if (datalen - sentdatalen > DESFIRE_TX_FRAME_MAX_LEN) {
apdu.Lc = DESFIRE_TX_FRAME_MAX_LEN;
else
} else {
apdu.Lc = datalen - sentdatalen;
}
apdu.data = &data[sentdatalen];
@@ -725,8 +726,8 @@ static int DesfireExchangeISONative(bool activate_field, DesfireContext_t *ctx,
}
}
if (respcode != NULL && ((sw & 0xff00) == 0x9100)) {
*respcode = sw & 0xff;
if (respcode != NULL && ((sw & 0xFF00) == 0x9100)) {
*respcode = sw & 0xFF;
}
if (resp) {
@@ -741,7 +742,7 @@ static int DesfireExchangeISONative(bool activate_field, DesfireContext_t *ctx,
pos += buflen;
if (enable_chaining == false) {
if (sw == DESFIRE_GET_ISO_STATUS(MFDES_S_OPERATION_OK) ||
sw == DESFIRE_GET_ISO_STATUS(MFDES_ADDITIONAL_FRAME)) {
sw == DESFIRE_GET_ISO_STATUS(MFDES_ADDITIONAL_FRAME)) {
if (resplen) {
*resplen = pos;
@@ -759,6 +760,8 @@ static int DesfireExchangeISONative(bool activate_field, DesfireContext_t *ctx,
apdu.P2 = 0;
apdu.data = NULL;
buflen = 0;
res = DESFIRESendApdu(false, apdu, buf, DESFIRE_BUFFER_SIZE, &buflen, &sw);
if (res != PM3_SUCCESS) {
PrintAndLogEx(DEBUG, "error DESFIRESendApdu %s", DesfireGetErrorString(res, &sw));
@@ -766,8 +769,8 @@ static int DesfireExchangeISONative(bool activate_field, DesfireContext_t *ctx,
return res;
}
if (respcode != NULL && ((sw & 0xff00) == 0x9100)) {
*respcode = sw & 0xff;
if (respcode != NULL && ((sw & 0xFF00) == 0x9100)) {
*respcode = sw & 0xFF;
}
if (resp != NULL) {
@@ -776,9 +779,10 @@ static int DesfireExchangeISONative(bool activate_field, DesfireContext_t *ctx,
memcpy(&resp[i * splitbysize + 1], buf, buflen);
i += 1;
} else {
memcpy(&resp[pos], buf, buflen);
memcpy(resp + (pos), buf, buflen);
}
}
pos += buflen;
if (sw != DESFIRE_GET_ISO_STATUS(MFDES_ADDITIONAL_FRAME)) {
@@ -786,6 +790,7 @@ static int DesfireExchangeISONative(bool activate_field, DesfireContext_t *ctx,
}
}
if (resplen) {
*resplen = (splitbysize) ? i : pos;
}
@@ -854,7 +859,7 @@ int DesfireExchangeEx(bool activate_field, DesfireContext_t *ctx, uint8_t cmd, u
DesfirePrintContext(ctx);
}
uint8_t *databuf = calloc(DESFIRE_BUFFER_SIZE, 1);
uint8_t *databuf = calloc(DESFIRE_BUFFER_SIZE, 1);
if (databuf == NULL) {
return PM3_EMALLOC;
}
@@ -866,13 +871,14 @@ int DesfireExchangeEx(bool activate_field, DesfireContext_t *ctx, uint8_t cmd, u
case DCCNativeISO:
DesfireSecureChannelEncode(ctx, cmd, data, datalen, databuf, &databuflen);
if (ctx->cmdSet == DCCNative)
if (ctx->cmdSet == DCCNative) {
res = DesfireExchangeNative(activate_field, ctx, cmd, databuf, databuflen, respcode, databuf, &databuflen, enable_chaining, splitbysize);
else
} else {
res = DesfireExchangeISONative(activate_field, ctx, cmd, databuf, databuflen, respcode, databuf, &databuflen, enable_chaining, splitbysize);
}
if (splitbysize) {
uint8_t sdata[250 * 5] = {0};
uint8_t sdata[DESFIRE_BUFFER_SIZE] = {0};
size_t sdatalen = 0;
DesfireJoinBlockToBytes(databuf, databuflen, splitbysize, sdata, &sdatalen);
@@ -1924,13 +1930,16 @@ void DesfirePrintAppList(DesfireContext_t *dctx, PICCInfo_t *PICCInfo, AppListS
}
static int DesfireCommandEx(DesfireContext_t *dctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *resp, size_t *resplen, int checklength, size_t splitbysize) {
if (resplen)
if (resplen) {
*resplen = 0;
}
uint8_t respcode = 0xFF;
uint8_t respcode = 0xff;
uint8_t *xresp = calloc(DESFIRE_BUFFER_SIZE, 1);
if (xresp == NULL)
if (xresp == NULL) {
return PM3_EMALLOC;
}
size_t xresplen = 0;
int res = DesfireExchangeEx(false, dctx, cmd, data, datalen, &respcode, xresp, &xresplen, true, splitbysize);
@@ -1938,19 +1947,24 @@ static int DesfireCommandEx(DesfireContext_t *dctx, uint8_t cmd, uint8_t *data,
free(xresp);
return res;
}
if (respcode != MFDES_S_OPERATION_OK) {
free(xresp);
return PM3_EAPDU_FAIL;
}
if (checklength >= 0 && xresplen != checklength) {
free(xresp);
return PM3_EAPDU_FAIL;
}
if (resplen)
if (resplen) {
*resplen = xresplen;
if (resp)
}
if (resp) {
memcpy(resp, xresp, (splitbysize == 0) ? xresplen : xresplen * splitbysize);
}
free(xresp);
return PM3_SUCCESS;
@@ -1996,13 +2010,16 @@ int DesfireReadSignature(DesfireContext_t *dctx, uint8_t sid, uint8_t *resp, siz
uint8_t respcode = 0xff;
int res = DesfireExchange(dctx, MFDES_READSIG, &sid, 1, &respcode, xresp, &xresplen);
if (res != PM3_SUCCESS)
if (res != PM3_SUCCESS) {
return res;
}
if (respcode != 0x90)
if (respcode != 0x90) {
return PM3_EAPDU_FAIL;
}
memcpy(resp, xresp, xresplen);
*resplen = xresplen;
return PM3_SUCCESS;
@@ -2172,7 +2189,6 @@ int DesfireReadFile(DesfireContext_t *dctx, uint8_t fnum, uint32_t offset, uint3
data[0] = fnum;
Uint3byteToMemLe(&data[1], offset);
Uint3byteToMemLe(&data[4], len);
return DesfireCommand(dctx, (dctx->isoChaining) ? MFDES_READ_DATA2 : MFDES_READ_DATA, data, 7, resp, resplen, -1);
}
+42 -19
View File
@@ -26,9 +26,10 @@
#include "des.h"
#include <mbedtls/cmac.h>
#include "crc.h"
#include "crc16.h" // crc16 ccitt
#include "crc16.h" // crc16 ccitt
#include "crc32.h"
#include "commonutil.h"
#include "desfirecore.h" // DESFIRE_BUFFER_SIZE
void DesfireClearContext(DesfireContext_t *ctx) {
ctx->keyNum = 0;
@@ -148,22 +149,29 @@ size_t DesfireSearchCRCPos(uint8_t *data, size_t datalen, uint8_t respcode, uint
return 0;
}
uint8_t crcdata[1024] = {0};
uint8_t crcdata[DESFIRE_BUFFER_SIZE] = {0};
size_t crcposfound = 0;
// crc may be 00..00 and at the end of file may be padding 0x80. so we search from last zero to crclen + 2 (one for crc=0 and one for padding 0x80)
for (int i = 0; i < crclen + 2; i++) {
if (crcpos - i == 0)
if (crcpos - i == 0) {
break;
if (crcpos - i + crclen > datalen)
}
if (crcpos - i + crclen > datalen) {
continue;
}
memcpy(crcdata, data, crcpos - i);
crcdata[crcpos - i] = respcode;
bool res;
if (crclen == 4)
if (crclen == 4) {
res = desfire_crc32_check(crcdata, crcpos - i + 1, &data[crcpos - i]);
else
} else {
res = iso14443a_crc_check(data, crcpos - i, &data[crcpos - i]);
}
if (res) {
crcposfound = crcpos - i;
}
@@ -252,7 +260,8 @@ static void DesfireCryptoEncDecSingleBlock(uint8_t *key, DesfireCryptoAlgorithm
}
void DesfireCryptoEncDecEx(DesfireContext_t *ctx, DesfireCryptoOpKeyType key_type, uint8_t *srcdata, size_t srcdatalen, uint8_t *dstdata, bool dir_to_send, bool encode, uint8_t *iv) {
uint8_t data[1024] = {0};
uint8_t data[DESFIRE_BUFFER_SIZE] = {0};
uint8_t xiv[DESFIRE_MAX_CRYPTO_BLOCK_SIZE] = {0};
if (ctx->secureChannel == DACd40) {
@@ -287,15 +296,17 @@ void DesfireCryptoEncDecEx(DesfireContext_t *ctx, DesfireCryptoOpKeyType key_typ
else
memcpy(iv, xiv, block_size);
if (dstdata)
if (dstdata) {
memcpy(dstdata, data, srcdatalen);
}
}
void DesfireCryptoEncDec(DesfireContext_t *ctx, DesfireCryptoOpKeyType key_type, uint8_t *srcdata, size_t srcdatalen, uint8_t *dstdata, bool encode) {
bool dir_to_send = encode;
bool xencode = encode;
if (ctx->secureChannel == DACd40)
if (ctx->secureChannel == DACd40) {
xencode = false;
}
DesfireCryptoEncDecEx(ctx, key_type, srcdata, srcdatalen, dstdata, dir_to_send, xencode, NULL);
}
@@ -333,8 +344,9 @@ void DesfireCMACGenerateSubkeys(DesfireContext_t *ctx, DesfireCryptoOpKeyType ke
void DesfireCryptoCMACEx(DesfireContext_t *ctx, DesfireCryptoOpKeyType key_type, uint8_t *data, size_t len, size_t minlen, uint8_t *cmac) {
int kbs = desfire_get_key_block_length(ctx->keyType);
if (kbs == 0)
if (kbs == 0) {
return;
}
uint8_t buffer[padded_data_length(MAX(minlen, len) + 1, kbs)];
memset(buffer, 0, sizeof(buffer));
@@ -357,8 +369,9 @@ void DesfireCryptoCMACEx(DesfireContext_t *ctx, DesfireCryptoOpKeyType key_type,
DesfireCryptoEncDec(ctx, key_type, buffer, len, NULL, true);
if (cmac != NULL)
if (cmac != NULL) {
memcpy(cmac, ctx->IV, kbs);
}
}
void DesfireCryptoCMAC(DesfireContext_t *ctx, uint8_t *data, size_t len, uint8_t *cmac) {
@@ -371,16 +384,18 @@ void MifareKdfAn10922(DesfireContext_t *ctx, DesfireCryptoOpKeyType key_type, co
return;
int kbs = desfire_get_key_block_length(ctx->keyType); // 8 or 16
if (kbs == 0)
if (kbs == 0) {
return;
}
uint8_t cmac[DESFIRE_MAX_CRYPTO_BLOCK_SIZE * 3] = {0};
uint8_t buffer[DESFIRE_MAX_CRYPTO_BLOCK_SIZE * 3] = {0};
if (ctx->keyType == T_AES) {
// AES uses 16 byte IV
if (kbs < CRYPTO_AES_BLOCK_SIZE)
if (kbs < CRYPTO_AES_BLOCK_SIZE) {
kbs = CRYPTO_AES_BLOCK_SIZE;
}
buffer[0] = 0x01;
memcpy(&buffer[1], data, len);
@@ -429,8 +444,9 @@ void DesfireDESKeySetVersion(uint8_t *key, DesfireCryptoAlgorithm keytype, uint8
return;
// clear version
for (int n = 0; n < desfire_get_key_length(keytype); n++)
for (int n = 0; n < desfire_get_key_length(keytype); n++) {
key[n] &= 0xFE;
}
// set version
for (int n = 0; n < 8; n++) {
@@ -451,8 +467,9 @@ void DesfireDESKeySetVersion(uint8_t *key, DesfireCryptoAlgorithm keytype, uint8
uint8_t DesfireDESKeyGetVersion(const uint8_t *key) {
uint8_t version = 0;
for (int n = 0; n < 8; n++)
for (int n = 0; n < 8; n++) {
version |= ((key[n] & 1) << (7 - n));
}
return version;
}
@@ -644,14 +661,16 @@ void DesfireEV2FillIV(DesfireContext_t *ctx, bool ivforcommand, uint8_t *iv) {
}
int DesfireEV2CalcCMAC(DesfireContext_t *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *mac) {
uint8_t mdata[1050] = {0};
uint8_t mdata[DESFIRE_BUFFER_SIZE] = {0};
size_t mdatalen = 0;
mdata[0] = cmd;
Uint2byteToMemLe(&mdata[1], ctx->cmdCntr);
memcpy(&mdata[3], ctx->TI, 4);
if (data != NULL && datalen > 0)
if (data != NULL && datalen > 0) {
memcpy(&mdata[7], data, datalen);
}
mdatalen = 1 + 2 + 4 + datalen;
return aes_cmac8(NULL, ctx->sessionKeyMAC, mdata, mac, mdatalen);
@@ -717,14 +736,18 @@ void DesfireDecodePrevReaderID(DesfireContext_t *ctx, uint8_t *key, uint32_t trC
}
int DesfireLRPCalcCMAC(DesfireContext_t *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *mac) {
uint8_t mdata[1050] = {0};
uint8_t mdata[DESFIRE_BUFFER_SIZE] = {0};
size_t mdatalen = 0;
mdata[0] = cmd;
Uint2byteToMemLe(&mdata[1], ctx->cmdCntr);
memcpy(&mdata[3], ctx->TI, 4);
if (data != NULL && datalen > 0)
if (data != NULL && datalen > 0) {
memcpy(&mdata[7], data, datalen);
}
mdatalen = 1 + 2 + 4 + datalen;
LRPContext_t lctx = {0};
+42 -20
View File
@@ -49,9 +49,11 @@ static const uint8_t CommandsCanUseAnyChannel[] = {
};
static bool CommandCanUseAnyChannel(uint8_t cmd) {
for (int i = 0; i < ARRAYLEN(CommandsCanUseAnyChannel); i++)
if (CommandsCanUseAnyChannel[i] == cmd)
for (int i = 0; i < ARRAYLEN(CommandsCanUseAnyChannel); i++) {
if (CommandsCanUseAnyChannel[i] == cmd) {
return true;
}
}
return false;
}
@@ -207,10 +209,11 @@ static const CmdHeaderLengths_t CmdHeaderLengths[] = {
};
static uint8_t DesfireGetCmdHeaderLen(uint8_t cmd) {
for (int i = 0; i < ARRAYLEN(CmdHeaderLengths); i++)
if (CmdHeaderLengths[i].cmd == cmd)
for (int i = 0; i < ARRAYLEN(CmdHeaderLengths); i++) {
if (CmdHeaderLengths[i].cmd == cmd) {
return CmdHeaderLengths[i].len;
}
}
return 0;
}
@@ -228,12 +231,15 @@ static const uint8_t EV1D40TransmitMAC[] = {
};
static bool DesfireEV1D40TransmitMAC(DesfireContext_t *ctx, uint8_t cmd) {
if (ctx->secureChannel != DACd40 && ctx->secureChannel != DACEV1)
if (ctx->secureChannel != DACd40 && ctx->secureChannel != DACEV1) {
return true;
}
for (int i = 0; i < ARRAYLEN(EV1D40TransmitMAC); i++)
if (EV1D40TransmitMAC[i] == cmd)
for (int i = 0; i < ARRAYLEN(EV1D40TransmitMAC); i++) {
if (EV1D40TransmitMAC[i] == cmd) {
return true;
}
}
return false;
}
@@ -247,12 +253,15 @@ static const uint8_t D40ReceiveMAC[] = {
};
static bool DesfireEV1D40ReceiveMAC(DesfireContext_t *ctx, uint8_t cmd) {
if (ctx->secureChannel != DACd40)
if (ctx->secureChannel != DACd40) {
return true;
}
for (int i = 0; i < ARRAYLEN(D40ReceiveMAC); i++)
if (D40ReceiveMAC[i] == cmd)
for (int i = 0; i < ARRAYLEN(D40ReceiveMAC); i++) {
if (D40ReceiveMAC[i] == cmd) {
return true;
}
}
return false;
}
@@ -269,10 +278,11 @@ static const uint8_t ISOChannelValidCmd[] = {
};
static bool DesfireISOChannelValidCmd(uint8_t cmd) {
for (int i = 0; i < ARRAYLEN(ISOChannelValidCmd); i++)
if (ISOChannelValidCmd[i] == cmd)
for (int i = 0; i < ARRAYLEN(ISOChannelValidCmd); i++) {
if (ISOChannelValidCmd[i] == cmd) {
return true;
}
}
return false;
}
@@ -349,15 +359,17 @@ static void DesfireSecureChannelEncodeD40(DesfireContext_t *ctx, uint8_t cmd, ui
static void DesfireSecureChannelEncodeEV1(DesfireContext_t *ctx, uint8_t cmd, uint8_t *srcdata, size_t srcdatalen, uint8_t *dstdata, size_t *dstdatalen) {
uint8_t *data = calloc(DESFIRE_BUFFER_SIZE, sizeof(uint8_t));
if (data == NULL)
if (data == NULL) {
return;
}
memcpy(dstdata, srcdata, srcdatalen);
*dstdatalen = srcdatalen;
uint8_t hdrlen = DesfireGetCmdHeaderLen(cmd);
if (srcdatalen < hdrlen)
if (srcdatalen < hdrlen) {
hdrlen = srcdatalen;
}
size_t rlen;
@@ -582,12 +594,14 @@ static void DesfireSecureChannelDecodeD40(DesfireContext_t *ctx, uint8_t *srcdat
static void DesfireSecureChannelDecodeEV1(DesfireContext_t *ctx, uint8_t *srcdata, size_t srcdatalen, uint8_t respcode, uint8_t *dstdata, size_t *dstdatalen) {
uint8_t *data = calloc(DESFIRE_BUFFER_SIZE, sizeof(uint8_t));
if (data == NULL)
if (data == NULL) {
return;
}
// if comm mode = plain --> response with MAC
// if request is not zero length --> response MAC
if (ctx->commMode == DCMPlain || ctx->commMode == DCMMACed || (ctx->commMode == DCMEncrypted && !ctx->lastRequestZeroLen)) {
if (srcdatalen < DesfireGetMACLength(ctx)) {
memcpy(dstdata, srcdata, srcdatalen);
*dstdatalen = srcdatalen;
@@ -596,6 +610,7 @@ static void DesfireSecureChannelDecodeEV1(DesfireContext_t *ctx, uint8_t *srcdat
}
memcpy(dstdata, srcdata, srcdatalen - DesfireGetMACLength(ctx));
*dstdatalen = srcdatalen - DesfireGetMACLength(ctx);
memcpy(data, srcdata, *dstdatalen);
@@ -603,15 +618,22 @@ static void DesfireSecureChannelDecodeEV1(DesfireContext_t *ctx, uint8_t *srcdat
uint8_t cmac[DESFIRE_MAX_CRYPTO_BLOCK_SIZE] = {0};
DesfireCryptoCMAC(ctx, data, *dstdatalen + 1, cmac);
if (memcmp(&srcdata[*dstdatalen], cmac, DesfireGetMACLength(ctx)) != 0) {
PrintAndLogEx(WARNING, "Received MAC is not match with calculated");
PrintAndLogEx(INFO, " received MAC: %s", sprint_hex(&srcdata[*dstdatalen], DesfireGetMACLength(ctx)));
PrintAndLogEx(INFO, " calculated MAC: %s", sprint_hex(cmac, DesfireGetMACLength(ctx)));
} else {
if (GetAPDULogging())
if (GetAPDULogging()) {
PrintAndLogEx(INFO, "Received MAC OK");
}
}
} else if (ctx->commMode == DCMEncrypted || ctx->commMode == DCMEncryptedWithPadding) {
} else if (ctx->commMode == DCMEncrypted || ctx->commMode == DCMEncryptedWithPadding) {
if (srcdatalen < desfire_get_key_block_length(ctx->keyType)) {
memcpy(dstdata, srcdata, srcdatalen);
*dstdatalen = srcdatalen;
@@ -620,7 +642,7 @@ static void DesfireSecureChannelDecodeEV1(DesfireContext_t *ctx, uint8_t *srcdat
}
DesfireCryptoEncDec(ctx, DCOSessionKeyEnc, srcdata, srcdatalen, dstdata, false);
//PrintAndLogEx(INFO, "decoded[%d]: %s", srcdatalen, sprint_hex(dstdata, srcdatalen));
// PrintAndLogEx(INFO, "decoded[%d]: %s", srcdatalen, sprint_hex(dstdata, srcdatalen));
size_t puredatalen = DesfireSearchCRCPos(dstdata, srcdatalen, respcode, 4);
if (puredatalen != 0) {
+23 -116
View File
@@ -53,11 +53,7 @@ static bool TestCRC16(void) {
len = DesfireSearchCRCPos(data, 1, 0x00, 2);
res = res && (len == 0);
if (res)
PrintAndLogEx(INFO, "CRC16............. " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "CRC16............. " _RED_("fail"));
PrintAndLogEx(INFO, "CRC16............. ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -80,11 +76,7 @@ static bool TestCRC32(void) {
len = DesfireSearchCRCPos(data, 2, 0x00, 4);
res = res && (len == 0);
if (res)
PrintAndLogEx(INFO, "CRC32............. " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "CRC32............. " _RED_("fail"));
PrintAndLogEx(INFO, "CRC32............. ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -131,11 +123,7 @@ static bool TestCMACSubkeys(void) {
res = res && (memcmp(sk1, sk1_3tdea, sizeof(sk1_3tdea)) == 0);
res = res && (memcmp(sk2, sk2_3tdea, sizeof(sk2_3tdea)) == 0);
if (res)
PrintAndLogEx(INFO, "CMAC subkeys...... " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "CMAC subkeys...... " _RED_("fail"));
PrintAndLogEx(INFO, "CMAC subkeys...... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -155,11 +143,7 @@ static bool TestAn10922KDFAES(void) {
uint8_t dkey[] = {0xA8, 0xDD, 0x63, 0xA3, 0xB8, 0x9D, 0x54, 0xB3, 0x7C, 0xA8, 0x02, 0x47, 0x3F, 0xDA, 0x91, 0x75};
res = res && (memcmp(dctx.key, dkey, sizeof(dkey)) == 0);
if (res)
PrintAndLogEx(INFO, "An10922 AES....... " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "An10922 AES....... " _RED_("fail"));
PrintAndLogEx(INFO, "An10922 AES....... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -177,11 +161,7 @@ static bool TestAn10922KDF2TDEA(void) {
uint8_t dkey[] = {0x16, 0xF8, 0x59, 0x7C, 0x9E, 0x89, 0x10, 0xC8, 0x6B, 0x96, 0x48, 0xD0, 0x06, 0x10, 0x7D, 0xD7};
res = res && (memcmp(dctx.key, dkey, sizeof(dkey)) == 0);
if (res)
PrintAndLogEx(INFO, "An10922 2TDEA..... " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "An10922 2TDEA..... " _RED_("fail"));
PrintAndLogEx(INFO, "An10922 2TDEA..... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -201,11 +181,7 @@ static bool TestAn10922KDF3TDEA(void) {
};
res = res && (memcmp(dctx.key, dkey, sizeof(dkey)) == 0);
if (res)
PrintAndLogEx(INFO, "An10922 3TDEA..... " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "An10922 3TDEA..... " _RED_("fail"));
PrintAndLogEx(INFO, "An10922 3TDEA..... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -245,11 +221,7 @@ static bool TestCMAC3TDEA(void) {
DesfireCryptoCMAC(&dctx, CMACData, 32, cmac);
res = res && (memcmp(cmac, cmac4, sizeof(cmac1)) == 0);
if (res)
PrintAndLogEx(INFO, "CMAC 3TDEA........ " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "CMAC 3TDEA........ " _RED_("fail"));
PrintAndLogEx(INFO, "CMAC 3TDEA........ ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -289,11 +261,7 @@ static bool TestCMAC2TDEA(void) {
DesfireCryptoCMAC(&dctx, CMACData, 32, cmac);
res = res && (memcmp(cmac, cmac4, sizeof(cmac1)) == 0);
if (res)
PrintAndLogEx(INFO, "CMAC 2TDEA........ " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "CMAC 2TDEA........ " _RED_("fail"));
PrintAndLogEx(INFO, "CMAC 2TDEA........ ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -329,11 +297,7 @@ static bool TestCMACDES(void) {
DesfireCryptoCMAC(&dctx, CMACData, 32, cmac);
res = res && (memcmp(cmac, cmac4, sizeof(cmac1)) == 0);
if (res)
PrintAndLogEx(INFO, "CMAC DES.......... " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "CMAC DES.......... " _RED_("fail"));
PrintAndLogEx(INFO, "CMAC DES.......... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -356,11 +320,7 @@ static bool TestEV2SessionKeys(void) {
DesfireGenSessionKeyEV2(key, rnda, rndb, false, sessionkey);
res = res && (memcmp(sessionkey, sessionkeymac, sizeof(sessionkeymac)) == 0);
if (res)
PrintAndLogEx(INFO, "EV2 session keys.. " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "EV2 session keys.. " _RED_("fail"));
PrintAndLogEx(INFO, "EV2 session keys.. ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -392,11 +352,7 @@ static bool TestEV2IVEncode(void) {
DesfireEV2FillIV(&ctx, true, iv);
res = res && (memcmp(iv, ivres2, sizeof(ivres2)) == 0);
if (res)
PrintAndLogEx(INFO, "EV2 IV calc....... " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "EV2 IV calc....... " _RED_("fail"));
PrintAndLogEx(INFO, "EV2 IV calc....... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -452,17 +408,12 @@ static bool TestEV2MAC(void) {
DesfireEV2CalcCMAC(&ctx, rc, cmddata4, sizeof(cmddata4), mac);
res = res && (memcmp(mac, macres4, sizeof(macres4)) == 0);
if (res)
PrintAndLogEx(INFO, "EV2 MAC calc...... " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "EV2 MAC calc...... " _RED_("fail"));
PrintAndLogEx(INFO, "EV2 MAC calc...... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
static bool TestTransSessionKeys(void) {
bool res = true;
uint8_t key[] = {0x66, 0xA8, 0xCB, 0x93, 0x26, 0x9D, 0xC9, 0xBC, 0x28, 0x85, 0xB7, 0xA9, 0x1B, 0x9C, 0x69, 0x7B};
uint8_t uid[] = {0x04, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
uint32_t trCntr = 8;
@@ -476,11 +427,7 @@ static bool TestTransSessionKeys(void) {
uint8_t keyenc[] = {0x11, 0x9B, 0x90, 0x2A, 0x07, 0xB1, 0x8A, 0x86, 0x5B, 0x8E, 0x1B, 0x00, 0x60, 0x59, 0x47, 0x84};
res = res && (memcmp(sessionkey, keyenc, sizeof(keyenc)) == 0);
if (res)
PrintAndLogEx(INFO, "Trans session key. " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "Trans session key. " _RED_("fail"));
PrintAndLogEx(INFO, "Trans session key. ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -505,11 +452,7 @@ static bool TestLRPPlaintexts(void) {
uint8_t pt15[] = {0x71, 0xB4, 0x44, 0xAF, 0x25, 0x7A, 0x93, 0x21, 0x53, 0x11, 0xD7, 0x58, 0xDD, 0x33, 0x32, 0x47};
res = res && (memcmp(ctx.plaintexts[15], pt15, sizeof(pt15)) == 0);
if (res)
PrintAndLogEx(INFO, "LRP plaintexts.... " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "LRP plaintexts.... " _RED_("fail"));
PrintAndLogEx(INFO, "LRP plaintexts.... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -531,11 +474,7 @@ static bool TestLRPUpdatedKeys(void) {
uint8_t key2[] = {0xFE, 0x30, 0xAB, 0x50, 0x46, 0x7E, 0x61, 0x78, 0x3B, 0xFE, 0x6B, 0x5E, 0x05, 0x60, 0x16, 0x0E};
res = res && (memcmp(ctx.updatedKeys[2], key2, sizeof(key2)) == 0);
if (res)
PrintAndLogEx(INFO, "LRP updated keys.. " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "LRP updated keys.. " _RED_("fail"));
PrintAndLogEx(INFO, "LRP updated keys.. ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -587,11 +526,7 @@ static bool TestLRPEval(void) {
uint8_t y5[] = {0xCF, 0x99, 0x13, 0x92, 0xF0, 0x36, 0x93, 0x50, 0xA7, 0xE2, 0x1B, 0xE5, 0x2F, 0x74, 0x88, 0x21};
res = res && (memcmp(y, y5, sizeof(y5)) == 0);
if (res)
PrintAndLogEx(INFO, "LRP eval.......... " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "LRP eval.......... " _RED_("fail"));
PrintAndLogEx(INFO, "LRP eval.......... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -618,11 +553,7 @@ static bool TestLRPIncCounter(void) {
uint8_t ctrr4[] = {0x00};
res = res && (memcmp(ctr4, ctrr4, sizeof(ctrr4)) == 0);
if (res)
PrintAndLogEx(INFO, "LRP inc counter... " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "LRP inc counter... " _RED_("fail"));
PrintAndLogEx(INFO, "LRP inc counter... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -686,11 +617,7 @@ static bool TestLRPEncode(void) {
res = res && (resplen == sizeof(res5));
res = res && (memcmp(resp, res5, sizeof(res5)) == 0);
if (res)
PrintAndLogEx(INFO, "LRP encode........ " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "LRP encode........ " _RED_("fail"));
PrintAndLogEx(INFO, "LRP encode........ ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -753,11 +680,7 @@ static bool TestLRPDecode(void) {
res = res && (resplen == sizeof(res5));
res = res && (memcmp(resp, res5, sizeof(res5)) == 0);
if (res)
PrintAndLogEx(INFO, "LRP decode........ " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "LRP decode........ " _RED_("fail"));
PrintAndLogEx(INFO, "LRP decode........ ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -793,11 +716,7 @@ static bool TestLRPSubkeys(void) {
res = res && (memcmp(sk1, sk1r3, sizeof(sk1r3)) == 0);
res = res && (memcmp(sk2, sk2r3, sizeof(sk2r3)) == 0);
if (res)
PrintAndLogEx(INFO, "LRP subkeys....... " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "LRP subkeys....... " _RED_("fail"));
PrintAndLogEx(INFO, "LRP subkeys....... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -855,11 +774,7 @@ static bool TestLRPCMAC(void) {
uint8_t cmacres6[] = {0x05, 0xF1, 0xCE, 0x30, 0x45, 0x1A, 0x03, 0xA6, 0xE4, 0x68, 0xB3, 0xA5, 0x90, 0x33, 0xA5, 0x54};
res = res && (memcmp(cmac, cmacres6, sizeof(cmacres6)) == 0);
if (res)
PrintAndLogEx(INFO, "LRP CMAC.......... " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "LRP CMAC.......... " _RED_("fail"));
PrintAndLogEx(INFO, "LRP CMAC.......... ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -877,11 +792,7 @@ static bool TestLRPSessionKeys(void) {
DesfireGenSessionKeyLRP(key, rnda, rndb, true, sessionkey);
res = res && (memcmp(sessionkey, sessionkeyres, sizeof(sessionkeyres)) == 0);
if (res)
PrintAndLogEx(INFO, "LRP session keys.. " _GREEN_("ok"));
else
PrintAndLogEx(ERR, "LRP session keys.. " _RED_("fail"));
PrintAndLogEx(INFO, "LRP session keys.. ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
return res;
}
@@ -914,11 +825,7 @@ bool DesfireTest(bool verbose) {
res = res && TestLRPSessionKeys();
PrintAndLogEx(INFO, "---------------------------");
if (res)
PrintAndLogEx(SUCCESS, " Tests [ %s ]", _GREEN_("ok"));
else
PrintAndLogEx(FAILED, " Tests [ %s ]", _RED_("fail"));
PrintAndLogEx(SUCCESS, "Tests ( %s )", (res) ? _GREEN_("ok") : _RED_("fail"));
PrintAndLogEx(NORMAL, "");
return res;
}