diff --git a/armsrc/Standalone/hf_msdsal.c b/armsrc/Standalone/hf_msdsal.c index 38416307c..c79d01aed 100644 --- a/armsrc/Standalone/hf_msdsal.c +++ b/armsrc/Standalone/hf_msdsal.c @@ -371,40 +371,36 @@ void RunMod() { Dbhexdump(len, receivedCmd, false); if (receivedCmd[0] == 0x02 || receivedCmd[0] == 0x03) { //Emulate a Visa MSD(Magnetic stripe data) card - uint8_t ppsea[39] = {0x6F, 0x23, 0x84, 0x0E, 0x32, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31, 0xA5, 0x11, 0xBF, 0x0C, 0x0E, 0x61, 0x0C, 0x4F, 0x07, 0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10, 0x87, 0x01, 0x01, 0x90, 0x00}; - uint8_t processing[10] = {0x80, 0x06, 0x00, 0x80, 0x08, 0x01, 0x01, 0x00, 0x90, 0x00}; - uint8_t visauid[34] = {0x6F, 0x1E, 0x84, 0x07, 0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10, 0xA5, 0x13, 0x50, 0x0B, 0x56, 0x49, 0x53, 0x41, 0x20, 0x43, 0x52, 0x45, 0x44, 0x49, 0x54, 0x9F, 0x38, 0x03, 0x9F, 0x66, 0x02, 0x90, 0x00}; - - uint8_t last[4] = {0x70, 0x15, 0x57, 0x13}; - uint8_t card[25]; - uint8_t statusapdu[2] = {0x90, 0x00}; - - uint8_t finished[2] = {0x6f, 0x00}; - - memcpy(&card[0], last, sizeof(last)); - memcpy(&card[4], token, sizeof(token)); - memcpy(&card[23], statusapdu, sizeof(statusapdu)); - dynamic_response_info.response[0] = receivedCmd[0]; //Depending on card reader commands, the Proxmark will answer to fool the reader if (receivedCmd[2] == 0xA4 && receivedCmd[6] == 0x32 && prevCmd == 0) { //Respond with PPSE + uint8_t ppsea[39] = {0x6F, 0x23, 0x84, 0x0E, 0x32, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31, 0xA5, 0x11, 0xBF, 0x0C, 0x0E, 0x61, 0x0C, 0x4F, 0x07, 0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10, 0x87, 0x01, 0x01, 0x90, 0x00}; memcpy(&dynamic_response_info.response[1], ppsea, sizeof(ppsea)); dynamic_response_info.response_n = sizeof(ppsea) + 1; prevCmd++; } else if (receivedCmd[2] == 0xA4 && receivedCmd[10] == 0x03 && receivedCmd[11] == 0x10 && prevCmd == 1) { //Respond Visa AID - memcpy(&dynamic_response_info.response[1], visauid, sizeof(visauid)); - dynamic_response_info.response_n = sizeof(visauid) + 1; + uint8_t visauid_long[34] = {0x6F, 0x1E, 0x84, 0x07, 0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10, 0xA5, 0x13, 0x50, 0x0B, 0x56, 0x49, 0x53, 0x41, 0x20, 0x43, 0x52, 0x45, 0x44, 0x49, 0x54, 0x9F, 0x38, 0x03, 0x9F, 0x66, 0x02, 0x90, 0x00}; + memcpy(&dynamic_response_info.response[1], visauid_long, sizeof(visauid_long)); + dynamic_response_info.response_n = sizeof(visauid_long) + 1; prevCmd++; } else if (receivedCmd[1] == 0x80 && receivedCmd[2] == 0xA8 && receivedCmd[6] == 0x83 && prevCmd == 2) { //GET PROCESSING - memcpy(&dynamic_response_info.response[1], processing, sizeof(processing)); - dynamic_response_info.response_n = sizeof(processing) + 1; + uint8_t processing_long[10] = {0x80, 0x06, 0x00, 0x80, 0x08, 0x01, 0x01, 0x00, 0x90, 0x00}; + memcpy(&dynamic_response_info.response[1], processing_long, sizeof(processing_long)); + dynamic_response_info.response_n = sizeof(processing_long) + 1; prevCmd++; } else if (receivedCmd[1] == 0x00 && receivedCmd[2] == 0xB2 && prevCmd == 3) { //SFI + uint8_t last[4] = {0x70, 0x15, 0x57, 0x13}; + uint8_t statusapdu[2] = {0x90, 0x00}; + uint8_t card[25]; + memcpy(&card[0], last, sizeof(last)); + memcpy(&card[4], token, sizeof(token)); + memcpy(&card[23], statusapdu, sizeof(statusapdu)); memcpy(&dynamic_response_info.response[1], card, sizeof(card)); dynamic_response_info.response_n = sizeof(card) + 1; prevCmd++; } else { + uint8_t finished[2] = {0x6f, 0x00}; memcpy(&dynamic_response_info.response[1], finished, sizeof(finished)); dynamic_response_info.response_n = sizeof(finished) + 1; if (prevCmd == 5) { diff --git a/armsrc/frozen.c b/armsrc/frozen.c index 41f2179cd..6477e0602 100644 --- a/armsrc/frozen.c +++ b/armsrc/frozen.c @@ -780,8 +780,7 @@ int json_printf_array(struct json_out *out, va_list *ap) { } #ifdef _WIN32 -int cs_win_vsnprintf(char *str, size_t size, const char *format, - va_list ap) WEAK; +int cs_win_vsnprintf(char *str, size_t size, const char *format, va_list ap) WEAK; int cs_win_vsnprintf(char *str, size_t size, const char *format, va_list ap) { int res = _vsnprintf(str, size, format, ap); va_end(ap); @@ -863,14 +862,21 @@ struct json_scanf_info { int json_unescape(const char *src, int slen, char *dst, int dlen) WEAK; int json_unescape(const char *src, int slen, char *dst, int dlen) { - char *send = (char *) src + slen, *dend = dst + dlen, *orig_dst = dst, *p; + if (dst == NULL || dlen == 0) + return JSON_STRING_INVALID; + + char *send = (char *) src + slen; + char *dend = dst + dlen; + char *orig_dst = dst; + char *p; const char *esc1 = "\"\\/bfnrt", *esc2 = "\"\\/\b\f\n\r\t"; while (src < send) { if (*src == '\\') { - if (++src >= send) return JSON_STRING_INCOMPLETE; + if (++src >= send) { return JSON_STRING_INCOMPLETE; } + if (*src == 'u') { - if (send - src < 5) return JSON_STRING_INCOMPLETE; + if (send - src < 5) { return JSON_STRING_INCOMPLETE; } /* Here we go: this is a \u.... escape. Process simple one-byte chars */ if (src[1] == '0' && src[2] == '0') { /* This is \u00xx character from the ASCII range */ @@ -881,12 +887,16 @@ int json_unescape(const char *src, int slen, char *dst, int dlen) { return JSON_STRING_INVALID; } } else if ((p = (char *) strchr(esc1, *src)) != NULL) { - if (dst < dend) *dst = esc2[p - esc1]; + if (dst < dend) { + *dst = esc2[p - esc1]; + } } else { return JSON_STRING_INVALID; } } else { - if (dst < dend) *dst = *src; + if (dst < dend) { + *dst = *src; + } } dst++; src++; diff --git a/armsrc/hitag2crack.c b/armsrc/hitag2crack.c index 34d35ada6..faf530897 100644 --- a/armsrc/hitag2crack.c +++ b/armsrc/hitag2crack.c @@ -32,7 +32,7 @@ bool hitag2_crack(uint8_t *response, uint8_t *nrarhex) { uint8_t uid[32]; uint8_t nrar[64]; uint8_t e_firstcmd[10]; - uint8_t e_page0cmd[10]; +// uint8_t e_page0cmd[10]; uint8_t keybits[42]; uint8_t pagehex[9]; uint8_t temp[20]; @@ -298,7 +298,6 @@ bool hitag2crack_read_page(uint8_t *responsestr, uint8_t pagenum, uint8_t *nrar, uint8_t e_responsestr[9]; uint8_t e_response[32]; uint8_t response[32]; - int i; if (pagenum > 7) { UserMessage("hitag2crack_read_page:\r\n invalid pagenum\r\n"); @@ -354,7 +353,6 @@ bool hitag2crack_send_e_cmd(uint8_t *responsestr, uint8_t *nrar, uint8_t *cmd, i // uint8_t tmp[37]; uint8_t uid[9]; uint8_t e_page3str[9]; - int ret = 0; // get the UID if (!hitag2_get_uid(uid)) { diff --git a/armsrc/lfsampling.h b/armsrc/lfsampling.h index 570dc20fe..bad90d62c 100644 --- a/armsrc/lfsampling.h +++ b/armsrc/lfsampling.h @@ -82,7 +82,7 @@ uint32_t getSampleCounter(); * 0 or 95 ==> 125 kHz * **/ -void LFSetupFPGAForADC(int divisor, bool lf_field); +void LFSetupFPGAForADC(int divisor, bool reader_field); /** * Called from the USB-handler to set the sampling configuration diff --git a/armsrc/mifaredesfire.c b/armsrc/mifaredesfire.c index 1fb77ab0e..ff5babda0 100644 --- a/armsrc/mifaredesfire.c +++ b/armsrc/mifaredesfire.c @@ -270,7 +270,8 @@ void MifareDES_Auth1(uint8_t *datain) { LED_B_OFF(); LED_C_OFF(); - if (payload->key == NULL) { + + if (payload->keylen == 0) { if (payload->algo == MFDES_AUTH_DES) { memcpy(keybytes, PICC_MASTER_KEY8, 8); } else if (payload->algo == MFDES_ALGO_AES || payload->algo == MFDES_ALGO_3DES) { diff --git a/client/Makefile b/client/Makefile index a7bf93383..d3461a302 100644 --- a/client/Makefile +++ b/client/Makefile @@ -83,7 +83,7 @@ else LDLIBS := -L/usr/local/opt/readline/lib $(LDLIBS) LIBS := -I/usr/local/opt/readline/include $(LIBS) # cf brew info qt: qt not symlinked anymore - PKG_CONFIG_PATH := PKG_CONFIG_PATH=/usr/local/opt/qt/lib/pkgconfig + PKG_CONFIG_ENV := PKG_CONFIG_ENV=/usr/local/opt/qt/lib/pkgconfig else LUALIB += -ldl LUAPLATFORM = linux @@ -92,16 +92,16 @@ endif ifneq ($(SKIPQT),1) # Check for correctly configured Qt5 - QTINCLUDES = $(shell $(PKG_CONFIG_PATH) pkg-config --cflags Qt5Core Qt5Widgets 2>/dev/null) - QTLDLIBS = $(shell $(PKG_CONFIG_PATH) pkg-config --libs Qt5Core Qt5Widgets 2>/dev/null) - MOC = $(shell $(PKG_CONFIG_PATH) pkg-config --variable=host_bins Qt5Core)/moc - UIC = $(shell $(PKG_CONFIG_PATH) pkg-config --variable=host_bins Qt5Core)/uic + QTINCLUDES = $(shell $(PKG_CONFIG_ENV) pkg-config --cflags Qt5Core Qt5Widgets 2>/dev/null) + QTLDLIBS = $(shell $(PKG_CONFIG_ENV) pkg-config --libs Qt5Core Qt5Widgets 2>/dev/null) + MOC = $(shell $(PKG_CONFIG_ENV) pkg-config --variable=host_bins Qt5Core)/moc + UIC = $(shell $(PKG_CONFIG_ENV) pkg-config --variable=host_bins Qt5Core)/uic ifeq ($(QTINCLUDES), ) # if Qt5 not found check for correctly configured Qt4 - QTINCLUDES = $(shell $(PKG_CONFIG_PATH) pkg-config --cflags QtCore QtGui 2>/dev/null) - QTLDLIBS = $(shell $(PKG_CONFIG_PATH) pkg-config --libs QtCore QtGui 2>/dev/null) - MOC = $(shell $(PKG_CONFIG_PATH) pkg-config --variable=moc_location QtCore) - UIC = $(shell $(PKG_CONFIG_PATH) pkg-config --variable=uic_location QtCore) + QTINCLUDES = $(shell $(PKG_CONFIG_ENV) pkg-config --cflags QtCore QtGui 2>/dev/null) + QTLDLIBS = $(shell $(PKG_CONFIG_ENV) pkg-config --libs QtCore QtGui 2>/dev/null) + MOC = $(shell $(PKG_CONFIG_ENV) pkg-config --variable=moc_location QtCore) + UIC = $(shell $(PKG_CONFIG_ENV) pkg-config --variable=uic_location QtCore) else # On OSX Qt5 is claiming for a C++11 compiler (gnu++14 works too, but if nothing it fails) PM3CXXFLAGS += -fPIC -std=c++11 diff --git a/client/deps/amiitool/amiitool.c b/client/deps/amiitool/amiitool.c index 2c5c4ddc2..b63222d50 100644 --- a/client/deps/amiitool/amiitool.c +++ b/client/deps/amiitool/amiitool.c @@ -86,7 +86,7 @@ int main(int argc, char **argv) { return 1; } - nfc3d_amiibo_keys amiiboKeys; + nfc3d_amiibo_keys amiiboKeys = {0}; if (! LoadAmiikey(amiiboKeys, keyfile)) return 5; @@ -136,7 +136,7 @@ int main(int argc, char **argv) { return 3; } } - size_t readPages = fread(original, 4, NTAG215_SIZE / 4, f); + readPages = fread(original, 4, NTAG215_SIZE / 4, f); if (readPages < NFC3D_AMIIBO_SIZE / 4) { fprintf(stderr, "Could not read from save\n"); fclose(f); diff --git a/client/deps/jansson/pack_unpack.c b/client/deps/jansson/pack_unpack.c index 651e1c56b..279499ebd 100644 --- a/client/deps/jansson/pack_unpack.c +++ b/client/deps/jansson/pack_unpack.c @@ -185,7 +185,7 @@ static char *read_string(scanner_t *s, va_list *ap, length = va_arg(*ap, size_t); } else { prev_token(s); - length = s->has_error ? 0 : strlen(str); + length = s->has_error == 1 ? 0 : strlen(str); } if (!s->has_error && strbuffer_append_bytes(&strbuff, str, length) == -1) { diff --git a/client/resources/aid_desfire.json b/client/resources/aid_desfire.json index e1f679845..206e003a0 100644 --- a/client/resources/aid_desfire.json +++ b/client/resources/aid_desfire.json @@ -313,5 +313,12 @@ FFFFFF General Issuer Information (FIDs 00: MAD Version; 01: Card Holder; 02: Ca "Description": "Campus Card", "Type": "student" }, -} + { + "AID": "C26001", + "Vendor": "CAR2GO", + "Country": "DE", + "Name": "MemberCard", + "Description": "CAR2GO - Member Card", + "Type": "carsharing" + } ] diff --git a/client/src/cmdhf14a.c b/client/src/cmdhf14a.c index c53bc08b6..262e879f4 100644 --- a/client/src/cmdhf14a.c +++ b/client/src/cmdhf14a.c @@ -1024,7 +1024,7 @@ static int CmdHF14ACmdRaw(const char *Cmd) { active_select = true; break; case 'b': - sscanf(Cmd + i + 2, "%d", &temp); + sscanf(Cmd + i + 2, "%u", &temp); numbits = temp & 0xFFFF; i += 3; while (Cmd[i] != ' ' && Cmd[i] != '\0') { i++; } @@ -1032,7 +1032,7 @@ static int CmdHF14ACmdRaw(const char *Cmd) { break; case 't': bTimeout = true; - sscanf(Cmd + i + 2, "%d", &temp); + sscanf(Cmd + i + 2, "%u", &temp); timeout = temp; i += 3; while (Cmd[i] != ' ' && Cmd[i] != '\0') { i++; } diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 4b091343b..65b02b5f8 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -46,12 +46,12 @@ typedef struct { uint8_t keyno; uint8_t keylen; uint8_t key[24]; -} mfdes_authinput_t; +} PACKED mfdes_authinput_t; typedef struct mfdes_auth_res { uint8_t sessionkeylen; uint8_t sessionkey[24]; -} mfdes_auth_res_t; +} PACKED mfdes_auth_res_t; typedef struct mfdes_data { uint8_t fileno; //01 @@ -1478,7 +1478,7 @@ static int handler_desfire_writedata(mfdes_data_t *data, MFDES_FILE_TYPE_T type) if (data->fileno > 0x1F) return PM3_EINVARG; int datatowrite = le24toh(data->length); int offset = le24toh(data->offset); - int datasize = 0; + int datasize; int pos = 0; int recvlen = 0; int res = PM3_SUCCESS; @@ -1491,8 +1491,10 @@ static int handler_desfire_writedata(mfdes_data_t *data, MFDES_FILE_TYPE_T type) if (type == MFDES_RECORD_FILE) apdu.INS = MFDES_WRITE_RECORD; while (datatowrite > 0) { - if (datatowrite > 52) datasize = 52; - else datasize = datatowrite; + if (datatowrite > 52) + datasize = 52; + else + datasize = datatowrite; tmp[1] = offset & 0xFF; tmp[2] = (offset >> 8) & 0xFF; @@ -1690,7 +1692,7 @@ int getKeySettings(uint8_t *aid) { } // Authentication tests - int res = test_desfire_authenticate(); + res = test_desfire_authenticate(); if (res == PM3_ETIMEOUT) return res; PrintAndLogEx(SUCCESS, " [0x0A] Authenticate : %s", (res == PM3_SUCCESS) ? _YELLOW_("YES") : "NO"); @@ -1838,29 +1840,29 @@ static int CmdHF14ADesCreateApp(const char *Cmd) { swap16(fid); if (aidlength != 3) { - PrintAndLogEx(ERR, "AID must have 3 bytes length."); + PrintAndLogEx(ERR, "AID must have 3 bytes length"); return PM3_EINVARG; } if (fidlength != 2) { - PrintAndLogEx(ERR, "FID must have 2 bytes length."); - return PM3_EINVARG; - } - bool usefid = true; - if (fidlength == 0) usefid = false; - - if (keylen1 != 1) { - PrintAndLogEx(ERR, "Keysetting1 must have 1 byte length."); + PrintAndLogEx(ERR, "FID must have 2 bytes length"); return PM3_EINVARG; } + bool usefid = (fidlength != 0); + if (keylen1 != 1) { - PrintAndLogEx(ERR, "Keysetting2 must have 1 byte length."); + PrintAndLogEx(ERR, "Keysetting1 must have 1 byte length"); + return PM3_EINVARG; + } + + if (keylen2 != 1) { + PrintAndLogEx(ERR, "Keysetting2 must have 1 byte length"); return PM3_EINVARG; } if (namelen > 16) { - PrintAndLogEx(ERR, "Name has a max. of 16 bytes length."); + PrintAndLogEx(ERR, "Name has a max. of 16 bytes length"); return PM3_EINVARG; } bool usename = true; @@ -1874,7 +1876,7 @@ static int CmdHF14ADesCreateApp(const char *Cmd) { uint8_t keysetting2=0xEE;*/ if (memcmp(aid, "\x00\x00\x00", 3) == 0) { - PrintAndLogEx(WARNING, _RED_(" Creating root aid 000000 is forbidden.")); + PrintAndLogEx(WARNING, _RED_(" Creating root aid 000000 is forbidden")); return PM3_ESOFT; } @@ -1882,12 +1884,17 @@ static int CmdHF14ADesCreateApp(const char *Cmd) { memcpy(aidhdr.aid, aid, sizeof(aid)); aidhdr.keysetting1 = keysetting1; aidhdr.keysetting2 = keysetting2; + if (usefid) memcpy(aidhdr.fid, fid, sizeof(fid)); + if (usename) memcpy(aidhdr.name, name, sizeof(name)); uint8_t rootaid[3] = {0x00, 0x00, 0x00}; int res = handler_desfire_select_application(rootaid); - if (res != PM3_SUCCESS) { DropField(); return res; } + if (res != PM3_SUCCESS) { + DropField(); + return res; + } res = handler_desfire_createapp(&aidhdr, usename, usefid); DropField(); @@ -1945,12 +1952,14 @@ static int CmdHF14ADesClearRecordFile(const char *Cmd) { arg_param_end }; CLIExecWithReturn(Cmd, argtable, false); - uint8_t fileno; int aidlength = 0; uint8_t aid[3] = {0}; CLIGetHexWithReturn(1, aid, &aidlength); + int filenolen = 0; - CLIGetHexWithReturn(2, &fileno, &filenolen); + uint8_t fileno[1] = {0}; + CLIGetHexWithReturn(2, fileno, &filenolen); + int fidlength = 0; uint8_t fid[2] = {0}; CLIParamHexToBuf(arg_get_str(3), fid, 2, &fidlength); @@ -1961,7 +1970,7 @@ static int CmdHF14ADesClearRecordFile(const char *Cmd) { return PM3_EINVARG; } - if (fileno > 0x1F) { + if (fileno[0] > 0x1F) { PrintAndLogEx(ERR, "Fileno must be lower 0x1F."); return PM3_EINVARG; } @@ -1979,10 +1988,12 @@ static int CmdHF14ADesClearRecordFile(const char *Cmd) { return res; } - res = handler_desfire_clearrecordfile(fileno); + res = handler_desfire_clearrecordfile(fileno[0]); if (res == PM3_SUCCESS) { PrintAndLogEx(SUCCESS, "Successfully cleared record file."); - } else PrintAndLogEx(ERR, "Error on deleting file : %d", res); + } else { + PrintAndLogEx(ERR, "Error on deleting file : %d", res); + } DropField(); return res; } @@ -2001,12 +2012,14 @@ static int CmdHF14ADesDeleteFile(const char *Cmd) { arg_param_end }; CLIExecWithReturn(Cmd, argtable, false); - uint8_t fileno; int aidlength = 0; uint8_t aid[3] = {0}; CLIGetHexWithReturn(1, aid, &aidlength); + int filenolen = 0; - CLIGetHexWithReturn(2, &fileno, &filenolen); + uint8_t fileno[1] = {0}; + CLIGetHexWithReturn(2, fileno, &filenolen); + int fidlength = 0; uint8_t fid[2] = {0}; CLIParamHexToBuf(arg_get_str(3), fid, 2, &fidlength); @@ -2017,7 +2030,7 @@ static int CmdHF14ADesDeleteFile(const char *Cmd) { return PM3_EINVARG; } - if (fileno > 0x1F) { + if (fileno[0] > 0x1F) { PrintAndLogEx(ERR, "Fileno must be lower 0x1F."); return PM3_EINVARG; } @@ -2035,10 +2048,12 @@ static int CmdHF14ADesDeleteFile(const char *Cmd) { return res; } - res = handler_desfire_deletefile(fileno); + res = handler_desfire_deletefile(fileno[0]); if (res == PM3_SUCCESS) { PrintAndLogEx(SUCCESS, "Successfully deleted file.."); - } else PrintAndLogEx(ERR, "Error on deleting file : %d", res); + } else { + PrintAndLogEx(ERR, "Error on deleting file : %d", res); + } DropField(); return res; } @@ -2062,22 +2077,27 @@ static int CmdHF14ADesCreateFile(const char *Cmd) { arg_param_end }; CLIExecWithReturn(Cmd, argtable, false); - uint8_t fileno; int aidlength = 0; uint8_t aid[3] = {0}; CLIGetHexWithReturn(1, aid, &aidlength); + int filenolen = 0; - CLIGetHexWithReturn(2, &fileno, &filenolen); + uint8_t fileno[1] = {0}; + CLIGetHexWithReturn(2, fileno, &filenolen); + int fidlength = 0; uint8_t fid[2] = {0}; CLIParamHexToBuf(arg_get_str(3), fid, 2, &fidlength); + uint8_t comset = arg_get_int(4); int arlength = 0; uint8_t ar[2] = {0}; CLIGetHexWithReturn(5, ar, &arlength); + int fsizelen = 0; uint8_t filesize[3] = {0}; CLIGetHexWithReturn(6, filesize, &fsizelen); + bool isbackup = arg_get_lit(7); CLIParserFree(); @@ -2090,7 +2110,7 @@ static int CmdHF14ADesCreateFile(const char *Cmd) { return PM3_EINVARG; } - if (fileno > 0x1F) { + if (fileno[0] > 0x1F) { PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)."); return PM3_EINVARG; } @@ -2130,7 +2150,7 @@ static int CmdHF14ADesCreateFile(const char *Cmd) { mfdes_file_t ft; memcpy(ft.fid, fid, 2); memcpy(ft.filesize, filesize, 3); - ft.fileno = fileno; + ft.fileno = fileno[0]; ft.comset = comset; memcpy(ft.access_rights, ar, 2); @@ -2160,21 +2180,23 @@ static int CmdHF14ADesGetValueData(const char *Cmd) { arg_param_end }; CLIExecWithReturn(Cmd, argtable, false); - uint8_t fileno; + int aidlength = 0; uint8_t aid[3] = {0}; CLIGetHexWithReturn(1, aid, &aidlength); + int filenolen = 0; - CLIGetHexWithReturn(2, &fileno, &filenolen); + uint8_t fileno[1] = {0}; + CLIGetHexWithReturn(2, fileno, &filenolen); CLIParserFree(); if (filenolen != 1) { - PrintAndLogEx(ERR, "File number is missing."); + PrintAndLogEx(ERR, "File number is missing"); return PM3_EINVARG; } - if (fileno > 0x1F) { - PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)."); + if (fileno[0] > 0x1F) { + PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)"); return PM3_EINVARG; } @@ -2193,11 +2215,12 @@ static int CmdHF14ADesGetValueData(const char *Cmd) { return res; } mfdes_value_t value; - value.fileno = fileno; + value.fileno = fileno[0]; + int len = 0; res = handler_desfire_getvalue(&value, &len); if (res == PM3_SUCCESS) { - PrintAndLogEx(SUCCESS, "Successfully read value from File %d:", fileno); + PrintAndLogEx(SUCCESS, "Successfully read value from File %u:", fileno[0]); PrintAndLogEx(NORMAL, "\nOffset | Data | Ascii"); PrintAndLogEx(NORMAL, "----------------------------------------------------------------------------"); for (int i = 0; i < len; i += 16) { @@ -2228,38 +2251,42 @@ static int CmdHF14ADesReadData(const char *Cmd) { arg_param_end }; CLIExecWithReturn(Cmd, argtable, false); - uint8_t fileno; int aidlength = 0; uint8_t aid[3] = {0}; CLIGetHexWithReturn(1, aid, &aidlength); + int filenolen = 0; - CLIGetHexWithReturn(2, &fileno, &filenolen); + uint8_t fileno[1] = {0}; + CLIGetHexWithReturn(2, fileno, &filenolen); + int offsetlength = 0; uint8_t offset[3] = {0}; CLIParamHexToBuf(arg_get_str(3), offset, 3, &offsetlength); + int flength = 0; uint8_t filesize[3] = {0}; CLIParamHexToBuf(arg_get_str(4), filesize, 3, &flength); + int type = arg_get_int(5); CLIParserFree(); if (type > 1) { - PrintAndLogEx(ERR, "Invalid file type (0=Standard/Backup, 1=Record)."); + PrintAndLogEx(ERR, "Invalid file type (0=Standard/Backup, 1=Record)"); return PM3_EINVARG; } if (offsetlength != 3 && offsetlength != 0) { - PrintAndLogEx(ERR, "Offset needs 3 hex bytes."); + PrintAndLogEx(ERR, "Offset needs 3 hex bytes"); return PM3_EINVARG; } if (filenolen != 1) { - PrintAndLogEx(ERR, "File number is missing."); + PrintAndLogEx(ERR, "File number is missing"); return PM3_EINVARG; } - if (fileno > 0x1F) { - PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)."); + if (fileno[0] > 0x1F) { + PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)"); return PM3_EINVARG; } @@ -2283,15 +2310,18 @@ static int CmdHF14ADesReadData(const char *Cmd) { mfdes_data_t ft; memcpy(ft.offset, offset, 3); memcpy(ft.length, filesize, 3); - ft.fileno = fileno; + ft.fileno = fileno[0]; + int bytestoread = le24toh(filesize); + if (bytestoread == 0) bytestoread = 0xFFFFFF; - uint8_t *data = (uint8_t *)malloc(bytestoread); + + uint8_t *data = (uint8_t *)calloc(bytestoread, sizeof(uint8_t)); if (data != NULL) { ft.data = data; res = handler_desfire_readdata(&ft, type); if (res == PM3_SUCCESS) { - PrintAndLogEx(SUCCESS, "Successfully read data from File %d:", ft.fileno); + PrintAndLogEx(SUCCESS, "Successfully read data from file %d:", ft.fileno); PrintAndLogEx(NORMAL, "\nOffset | Data | Ascii"); PrintAndLogEx(NORMAL, "----------------------------------------------------------------------------"); int len = le24toh(ft.length); @@ -2322,15 +2352,22 @@ static int CmdHF14ADesChangeValue(const char *Cmd) { arg_int0("mM", "mode", "", "Mode (0=Credit, 1=LimitedCredit, 2=Debit)"), arg_param_end }; + mfdes_value_t value; CLIExecWithReturn(Cmd, argtable, false); + int aidlength = 0; uint8_t aid[3] = {0}; CLIGetHexWithReturn(1, aid, &aidlength); + int filenolen = 0; - CLIGetHexWithReturn(2, &value.fileno, &filenolen); + uint8_t fileno[1] = {0}; + CLIGetHexWithReturn(2, fileno, &filenolen); + value.fileno = fileno[0]; + int vlength = 0x0; CLIParamHexToBuf(arg_get_str(3), value.value, 4, &vlength); + int mode = arg_get_int(4); CLIParserFree(); swap24(aid); @@ -2392,6 +2429,7 @@ static int CmdHF14ADesChangeValue(const char *Cmd) { static int CmdHF14ADesWriteData(const char *Cmd) { + CLIParserInit("hf mfdes writedata", "Write data to File", "Usage:" @@ -2403,56 +2441,66 @@ static int CmdHF14ADesWriteData(const char *Cmd) { arg_strx0("aA", "aid", "", "AID for file (3 hex bytes, big endian)"), arg_strx0("nN", "fileno", "", "File Number (1 hex byte, 0x00 - 0x1F)"), arg_strx0("oO", "offset", "", "File Offset (3 hex bytes, big endian), optional"), - arg_strx0("dD", "data", "", "Data to write (hex bytes, 0xFFFFFF bytes max.)"), + arg_strx0("dD", "data", "", "Data to write (hex bytes, 0xFFFF bytes max.)"), arg_int0("type", "type", "", "File Type (0=Standard/Backup, 1=Record)"), arg_param_end }; + CLIExecWithReturn(Cmd, argtable, false); - uint8_t fileno; + int aidlength = 0; uint8_t aid[3] = {0}; CLIGetHexWithReturn(1, aid, &aidlength); + int filenolen = 0; - CLIGetHexWithReturn(2, &fileno, &filenolen); + uint8_t fileno[1] = {0}; + CLIGetHexWithReturn(2, fileno, &filenolen); + int offsetlength = 0; uint8_t offset[3] = {0}; CLIParamHexToBuf(arg_get_str(3), offset, 3, &offsetlength); - int dlength = 0xFFFFFF; - uint8_t *data = (uint8_t *)malloc(0xFFFFFF); - memset(data, 0x0, 0xFFFFFF); - CLIParamHexToBuf(arg_get_str(4), data, 0xFFFFFF, &dlength); + + int dlength = 0xFFFF; + uint8_t *data = (uint8_t *)calloc(0xFFFF, sizeof(uint8_t)); + if (data == NULL) { + PrintAndLogEx(ERR, "failed to allocate memory"); + return PM3_EMALLOC; + } + CLIParamHexToBuf(arg_get_str(4), data, 0xFFFF, &dlength); + int type = arg_get_int(5); + CLIParserFree(); swap24(aid); swap24(offset); - if (type > 1) { - PrintAndLogEx(ERR, "Unknown type (0=Standard/Backup, 1=Record)."); + if (type < 0 || type > 1) { + PrintAndLogEx(ERR, "Unknown type (0=Standard/Backup, 1=Record)"); if (data) free(data); return PM3_EINVARG; } if (dlength == 0) { - PrintAndLogEx(ERR, "Data needs some hex bytes to write."); + PrintAndLogEx(ERR, "Data needs some hex bytes to write"); if (data) free(data); return PM3_EINVARG; } if (offsetlength != 3 && offsetlength != 0) { - PrintAndLogEx(ERR, "Offset needs 3 hex bytes."); + PrintAndLogEx(ERR, "Offset needs 3 hex bytes"); if (data) free(data); return PM3_EINVARG; } if (filenolen != 1) { - PrintAndLogEx(ERR, "File number is missing."); + PrintAndLogEx(ERR, "File number is missing"); if (data) free(data); return PM3_EINVARG; } - if (fileno > 0x1F) { - PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)."); + if (fileno[0] > 0x1F) { + PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)"); if (data) free(data); return PM3_EINVARG; } @@ -2466,21 +2514,23 @@ static int CmdHF14ADesWriteData(const char *Cmd) { int res = handler_desfire_select_application(aid); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "Couldn't select aid."); + PrintAndLogEx(ERR, "Couldn't select aid"); DropField(); if (data) free(data); return res; } mfdes_data_t ft; + memcpy(ft.offset, offset, 3); htole24(dlength, ft.length); - ft.fileno = fileno; + ft.fileno = fileno[0]; + if (data != NULL) { ft.data = data; res = handler_desfire_writedata(&ft, type); if (res == PM3_SUCCESS) { - PrintAndLogEx(SUCCESS, "Successfully wrote data."); + PrintAndLogEx(SUCCESS, "Successfully wrote data"); } else { PrintAndLogEx(ERR, "Couldn't read data. Error %d", res); } @@ -2511,25 +2561,31 @@ static int CmdHF14ADesCreateRecordFile(const char *Cmd) { arg_param_end }; CLIExecWithReturn(Cmd, argtable, false); - uint8_t fileno; int aidlength = 0; uint8_t aid[3] = {0}; CLIGetHexWithReturn(1, aid, &aidlength); + int filenolen = 0; - CLIGetHexWithReturn(2, &fileno, &filenolen); + uint8_t fileno[1] = {0}; + CLIGetHexWithReturn(2, fileno, &filenolen); + int fidlength = 0; uint8_t fid[2] = {0}; CLIParamHexToBuf(arg_get_str(3), fid, 2, &fidlength); + uint8_t comset = arg_get_int(4); int arlength = 0; uint8_t ar[2] = {0}; CLIGetHexWithReturn(5, ar, &arlength); + int rsizelen = 0; uint8_t recordsize[3] = {0}; CLIGetHexWithReturn(6, recordsize, &rsizelen); + int msizelen = 0; uint8_t maxnumrecords[3] = {0}; CLIGetHexWithReturn(7, maxnumrecords, &msizelen); + bool cyclic = arg_get_lit(8); CLIParserFree(); @@ -2553,7 +2609,7 @@ static int CmdHF14ADesCreateRecordFile(const char *Cmd) { return PM3_EINVARG; } - if (fileno > 0x1F) { + if (fileno[0] > 0x1F) { PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)."); return PM3_EINVARG; } @@ -2592,7 +2648,7 @@ static int CmdHF14ADesCreateRecordFile(const char *Cmd) { } mfdes_linear_t ft; - ft.fileno = fileno; + ft.fileno = fileno[0]; memcpy(ft.fid, fid, 2); ft.comset = comset; memcpy(ft.access_rights, ar, 2); @@ -2630,28 +2686,34 @@ static int CmdHF14ADesCreateValueFile(const char *Cmd) { arg_param_end }; CLIExecWithReturn(Cmd, argtable, false); - uint8_t fileno; int aidlength = 0; uint8_t aid[3] = {0}; CLIGetHexWithReturn(1, aid, &aidlength); + int filenolen = 0; - CLIGetHexWithReturn(2, &fileno, &filenolen); + uint8_t fileno[1] = {0}; + CLIGetHexWithReturn(2, fileno, &filenolen); + uint8_t comset = arg_get_int(3); int arlength = 0; uint8_t ar[2] = {0}; CLIGetHexWithReturn(4, ar, &arlength); + int lllen = 0; uint8_t lowerlimit[4] = {0}; CLIGetHexWithReturn(5, lowerlimit, &lllen); + int ullen = 0; uint8_t upperlimit[4] = {0}; CLIGetHexWithReturn(6, upperlimit, &ullen); + int vllen = 0; uint8_t value[4] = {0}; CLIGetHexWithReturn(7, value, &vllen); + int limitedlen = 0; - uint8_t limited = 0; - CLIGetHexWithReturn(8, &limited, &limitedlen); + uint8_t limited[1] = {0}; + CLIGetHexWithReturn(8, limited, &limitedlen); CLIParserFree(); @@ -2661,37 +2723,37 @@ static int CmdHF14ADesCreateValueFile(const char *Cmd) { swap32(value); if (filenolen != 1) { - PrintAndLogEx(ERR, "File number is missing."); + PrintAndLogEx(ERR, "File number is missing"); return PM3_EINVARG; } - if (fileno > 0x1F) { - PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)."); + if (fileno[0] > 0x1F) { + PrintAndLogEx(ERR, "File number range is invalid (0x00-0x1F)"); return PM3_EINVARG; } if (comset != 0 && comset != 1 && comset != 3) { - PrintAndLogEx(ERR, "Communication setting must be either 0=Plain, 1=Plain+MAC or 3=Encrypt."); + PrintAndLogEx(ERR, "Communication setting must be either 0=Plain, 1=Plain+MAC or 3=Encrypt"); return PM3_EINVARG; } if (arlength != 2) { - PrintAndLogEx(ERR, "Access rights must have 2 hex bytes length."); + PrintAndLogEx(ERR, "Access rights must have 2 hex bytes length"); return PM3_EINVARG; } if (lllen != 4) { - PrintAndLogEx(ERR, "Lower limit must have 4 hex bytes length."); + PrintAndLogEx(ERR, "Lower limit must have 4 hex bytes length"); return PM3_EINVARG; } if (ullen != 4) { - PrintAndLogEx(ERR, "Upper limit must have 4 hex bytes length."); + PrintAndLogEx(ERR, "Upper limit must have 4 hex bytes length"); return PM3_EINVARG; } if (vllen != 4) { - PrintAndLogEx(ERR, "Value must have 4 hex bytes length."); + PrintAndLogEx(ERR, "Value must have 4 hex bytes length"); return PM3_EINVARG; } @@ -2713,13 +2775,13 @@ static int CmdHF14ADesCreateValueFile(const char *Cmd) { } mfdes_value_file_t ft; - ft.fileno = fileno; + ft.fileno = fileno[0]; ft.comset = comset; memcpy(ft.access_rights, ar, 2); memcpy(ft.lowerlimit, lowerlimit, 4); memcpy(ft.upperlimit, upperlimit, 4); memcpy(ft.value, value, 4); - ft.limitedcreditenabled = limited; + ft.limitedcreditenabled = limited[0]; res = handler_desfire_create_value_file(&ft); if (res == PM3_SUCCESS) { @@ -2751,7 +2813,7 @@ static int CmdHF14ADesFormatPICC(const char *Cmd) { CLIParserFree(); if ((keylen < 8) || (keylen > 8)) { - PrintAndLogEx(ERR, "Specified key must have 8 bytes length."); + PrintAndLogEx(ERR, "Specified key must have 8 bytes length"); return PM3_EINVARG; } @@ -2766,6 +2828,7 @@ static int CmdHF14ADesFormatPICC(const char *Cmd) { payload.mode = MFDES_AUTH_PICC; payload.algo = MFDES_ALGO_DES; payload.keyno = 0; + SendCommandNG(CMD_HF_DESFIRE_AUTH1, (uint8_t *)&payload, sizeof(payload)); PacketResponseNG resp; @@ -2781,11 +2844,14 @@ static int CmdHF14ADesFormatPICC(const char *Cmd) { uint8_t flags; uint8_t datalen; uint8_t datain[FRAME_PAYLOAD_SIZE]; - } PACKED payload; - payload.datain[0] = 0xFC; - payload.flags = NONE; - payload.datalen = 1; - SendCommandNG(CMD_HF_DESFIRE_COMMAND, (uint8_t *)&payload, sizeof(payload)); + } PACKED payload_raw; + + payload_raw.datain[0] = 0xFC; + payload_raw.flags = NONE; + payload_raw.datalen = 1; + + SendCommandNG(CMD_HF_DESFIRE_COMMAND, (uint8_t *)&payload_raw, sizeof(payload_raw)); + if (!WaitForResponseTimeout(CMD_HF_DESFIRE_COMMAND, &resp, 3000)) { PrintAndLogEx(WARNING, "Client reset command execute timeout"); DropField(); @@ -2801,7 +2867,7 @@ static int CmdHF14ADesFormatPICC(const char *Cmd) { return PM3_SUCCESS; } } else { - PrintAndLogEx(WARNING, _RED_("Auth command failed.")); + PrintAndLogEx(WARNING, _RED_("Auth command failed")); } DropField(); return PM3_SUCCESS; @@ -3124,8 +3190,10 @@ static int CmdHF14ADesDump(const char *Cmd) { uint8_t filesettings[20] = {0}; int fileset_len = 0; - int res = handler_desfire_filesettings(file_ids[j], filesettings, &fileset_len); + + res = handler_desfire_filesettings(file_ids[j], filesettings, &fileset_len); int maclen = 0; // To be implemented + if (res == PM3_SUCCESS) { //if (DecodeFileSettings(filesettings, fileset_len, maclen) != PM3_SUCCESS) { if (fileset_len == 1 + 1 + 2 + 3 + maclen) { @@ -3143,8 +3211,8 @@ static int CmdHF14ADesDump(const char *Cmd) { PrintAndLogEx(NORMAL, "\nOffset | Data | Ascii"); PrintAndLogEx(NORMAL, "----------------------------------------------------------------------------"); int len = le24toh(fdata.length); - for (int i = 0; i < len; i += 16) { - PrintAndLogEx(NORMAL, "%02d/0x%02X | %s| %s", i, i, sprint_hex(&fdata.data[i], len > 16 ? 16 : len), sprint_ascii(&fdata.data[i], len > 16 ? 16 : len)); + for (int n = 0; n < len; n += 16) { + PrintAndLogEx(NORMAL, "%02d/0x%02X | %s| %s", n, n, sprint_hex(&fdata.data[n], len > 16 ? 16 : len), sprint_ascii(&fdata.data[n], len > 16 ? 16 : len)); } free(data); } else { @@ -3161,8 +3229,8 @@ static int CmdHF14ADesDump(const char *Cmd) { if (res == PM3_SUCCESS) { PrintAndLogEx(NORMAL, "\nOffset | Value | Ascii"); PrintAndLogEx(NORMAL, "----------------------------------------------------------------------------"); - for (int i = 0; i < len; i += 16) { - PrintAndLogEx(NORMAL, "%02d/0x%02X | %s| %s", i, i, sprint_hex(&value.value[i], len > 16 ? 16 : len), sprint_ascii(&value.value[i], len > 16 ? 16 : len)); + for (int n = 0; n < len; n += 16) { + PrintAndLogEx(NORMAL, "%02d/0x%02X | %s| %s", n, n, sprint_hex(&value.value[n], len > 16 ? 16 : len), sprint_ascii(&value.value[n], len > 16 ? 16 : len)); } } else { PrintAndLogEx(ERR, "Couldn't read value. Error %d", res); @@ -3189,8 +3257,8 @@ static int CmdHF14ADesDump(const char *Cmd) { PrintAndLogEx(NORMAL, "\nOffset | Data | Ascii"); PrintAndLogEx(NORMAL, "----------------------------------------------------------------------------"); int len = le24toh(fdata.length); - for (int i = 0; i < len; i += 16) { - PrintAndLogEx(NORMAL, "%02d/0x%02X | %s| %s", i, i, sprint_hex(&fdata.data[i], len > 16 ? 16 : len), sprint_ascii(&fdata.data[i], len > 16 ? 16 : len)); + for (int n = 0; n < len; n += 16) { + PrintAndLogEx(NORMAL, "%02d/0x%02X | %s| %s", n, n, sprint_hex(&fdata.data[n], len > 16 ? 16 : len), sprint_ascii(&fdata.data[n], len > 16 ? 16 : len)); } } else { res = handler_desfire_select_application(aid); @@ -3279,8 +3347,9 @@ static int CmdHF14ADesEnumApplications(const char *Cmd) { uint8_t filesettings[20] = {0}; int fileset_len = 0; - int res = handler_desfire_filesettings(file_ids[j], filesettings, &fileset_len); int maclen = 0; // To be implemented + + res = handler_desfire_filesettings(file_ids[j], filesettings, &fileset_len); if (res == PM3_SUCCESS) { if (DecodeFileSettings(filesettings, fileset_len, maclen) != PM3_SUCCESS) { PrintAndLogEx(INFO, " Settings [%u] %s", fileset_len, sprint_hex(filesettings, fileset_len)); @@ -3600,7 +3669,7 @@ static int AuthCheckDesfire(uint8_t *aid, uint8_t deskeyList[MAX_KEYS_LIST_LEN][ DropField(); res = handler_desfire_select_application(aid); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "AID %X does not exist."); + PrintAndLogEx(ERR, "AID 0x%06X does not exist", curaid); return res; } break; diff --git a/client/src/cmdhfmfu.c b/client/src/cmdhfmfu.c index 9bdd9b814..f70c3887e 100644 --- a/client/src/cmdhfmfu.c +++ b/client/src/cmdhfmfu.c @@ -2705,7 +2705,8 @@ static int CmdHF14AMfuOtpTearoff(const char *Cmd) { return usage_hf_mfu_otp_tearoff(); case 'b': blockNoUint = param_get8(Cmd, cmdp + 1); - if (blockNoUint < 0) { + //iceman, which blocks can be targeted? UID blocks? + if (blockNoUint < 2) { PrintAndLogEx(WARNING, "Wrong block number"); errors = true; } @@ -2713,7 +2714,7 @@ static int CmdHF14AMfuOtpTearoff(const char *Cmd) { break; case 'i': interval = param_get32ex(Cmd, cmdp + 1, interval, 10); - if (interval <= 0) { + if (interval == 0) { PrintAndLogEx(WARNING, "Wrong interval number"); errors = true; } @@ -2758,13 +2759,14 @@ static int CmdHF14AMfuOtpTearoff(const char *Cmd) { if (errors) return usage_hf_mfu_otp_tearoff(); + PrintAndLogEx(INFO, "Starting TearOff test - Selected Block no: %u", blockNoUint); + + uint32_t actualTime = startTime; - printf("\nStarting TearOff test - Selected Block no: %d ...\n", blockNoUint); while (actualTime <= (timeLimit - interval)) { - printf("\nTrying attack at: %d us\n", actualTime); - printf("\n.....\n"); - printf("\nReading block before attack: \n"); + PrintAndLogEx(INFO, "Using tear-off at: %" PRIu32 " us", actualTime); + PrintAndLogEx(INFO, "Reading block BEFORE attack"); clearCommandBuffer(); SendCommandOLD(CMD_HF_MIFAREU_READBL, blockNoUint, 0, 0, NULL, 0); @@ -2780,7 +2782,7 @@ static int CmdHF14AMfuOtpTearoff(const char *Cmd) { } } - printf("\n.....\n"); + PrintAndLogEx(INFO, "....."); clearCommandBuffer(); SendCommandOLD(CMD_HF_MFU_OTP_TEAROFF, blockNoUint, actualTime, 0, teardata, 8); @@ -2789,8 +2791,7 @@ static int CmdHF14AMfuOtpTearoff(const char *Cmd) { return PM3_ESOFT; } - - printf("\nReading block after attack: \n"); + PrintAndLogEx(INFO, "Reading block AFTER attack"); clearCommandBuffer(); SendCommandOLD(CMD_HF_MIFAREU_READBL, blockNoUint, 0, 0, NULL, 0); diff --git a/client/src/crypto/libpcrypto.c b/client/src/crypto/libpcrypto.c index 1141818ab..a00918b91 100644 --- a/client/src/crypto/libpcrypto.c +++ b/client/src/crypto/libpcrypto.c @@ -177,8 +177,9 @@ static int ecdsa_init(mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id curveid, int ecdsa_key_create(mbedtls_ecp_group_id curveid, uint8_t *key_d, uint8_t *key_xy) { int res; mbedtls_ecdsa_context ctx; - ecdsa_init(&ctx, curveid, NULL, NULL); - + res = ecdsa_init(&ctx, curveid, NULL, NULL); + if (res) + goto exit; mbedtls_entropy_context entropy; mbedtls_ctr_drbg_context ctr_drbg; @@ -276,7 +277,10 @@ int ecdsa_signature_create(mbedtls_ecp_group_id curveid, uint8_t *key_d, uint8_t goto exit; mbedtls_ecdsa_context ctx; - ecdsa_init(&ctx, curveid, key_d, key_xy); + res = ecdsa_init(&ctx, curveid, key_d, key_xy); + if (res) + goto exit; + res = mbedtls_ecdsa_write_signature( &ctx, MBEDTLS_MD_SHA256, @@ -308,7 +312,10 @@ static int ecdsa_signature_create_test(mbedtls_ecp_group_id curveid, const char param_gethex_to_eol(random, 0, fixed_rand_value, sizeof(fixed_rand_value), &rndlen); mbedtls_ecdsa_context ctx; - ecdsa_init_str(&ctx, curveid, key_d, key_x, key_y); + res = ecdsa_init_str(&ctx, curveid, key_d, key_x, key_y); + if (res) + return res; + res = mbedtls_ecdsa_write_signature(&ctx, MBEDTLS_MD_SHA256, shahash, sizeof(shahash), signature, signaturelen, fixed_rand, NULL); mbedtls_ecdsa_free(&ctx); @@ -323,7 +330,10 @@ static int ecdsa_signature_verify_keystr(mbedtls_ecp_group_id curveid, const cha return res; mbedtls_ecdsa_context ctx; - ecdsa_init_str(&ctx, curveid, NULL, key_x, key_y); + res = ecdsa_init_str(&ctx, curveid, NULL, key_x, key_y); + if (res) + return res; + res = mbedtls_ecdsa_read_signature( &ctx, hash ? shahash : input, @@ -347,6 +357,9 @@ int ecdsa_signature_verify(mbedtls_ecp_group_id curveid, uint8_t *key_xy, uint8_ mbedtls_ecdsa_context ctx; res = ecdsa_init(&ctx, curveid, NULL, key_xy); + if (res) + return res; + res = mbedtls_ecdsa_read_signature( &ctx, hash ? shahash : input, diff --git a/client/src/fido/fidocore.c b/client/src/fido/fidocore.c index 1a680ed88..32d7c9ff0 100644 --- a/client/src/fido/fidocore.c +++ b/client/src/fido/fidocore.c @@ -359,10 +359,11 @@ bool CheckrpIdHash(json_t *json, uint8_t *hash) { // check ANSI X9.62 format ECDSA signature (on P-256) static int FIDO2CheckSignature(json_t *root, uint8_t *publickey, uint8_t *sign, size_t signLen, uint8_t *authData, size_t authDataLen, bool verbose) { - int res; + uint8_t rval[300] = {0}; uint8_t sval[300] = {0}; - res = ecdsa_asn1_get_signature(sign, signLen, rval, sval); + + int res = ecdsa_asn1_get_signature(sign, signLen, rval, sval); if (!res) { if (verbose) { PrintAndLogEx(NORMAL, " r: %s", sprint_hex(rval, 32)); @@ -383,7 +384,8 @@ static int FIDO2CheckSignature(json_t *root, uint8_t *publickey, uint8_t *sign, authData, authDataLen, // rpIdHash[32] + flags[1] + signCount[4] clientDataHash, 32, // Hash of the serialized client data. "$.ClientDataHash" from json NULL, 0); - //PrintAndLogEx(NORMAL, "--xbuf(%d)[%d]: %s", res, xbuflen, sprint_hex(xbuf, xbuflen)); + PrintAndLogEx(DEBUG, "--xbuf(%d)[%zu]: %s", res, xbuflen, sprint_hex(xbuf, xbuflen)); + res = ecdsa_signature_verify(MBEDTLS_ECP_DP_SECP256R1, publickey, xbuf, xbuflen, sign, signLen, true); if (res) { if (res == MBEDTLS_ERR_ECP_VERIFY_FAILED) { diff --git a/client/src/fileutils.c b/client/src/fileutils.c index b32d8ec88..b0b8d9772 100644 --- a/client/src/fileutils.c +++ b/client/src/fileutils.c @@ -49,6 +49,7 @@ #include "util.h" #ifdef _WIN32 #include "scandir.h" +#include #endif #define PATH_MAX_LENGTH 200 @@ -130,7 +131,87 @@ static bool is_directory(const char *filename) { return S_ISDIR(st.st_mode) != 0; } +/** + * @brief create a new directory. + * @param dirname + * @return + */ +// Not used... +/* +#ifdef _WIN32 +#define make_dir(a) _mkdir(a) +#else +#define make_dir(a) mkdir(a,0755) //note 0755 MUST have leading 0 for octal linux file permissions +#endif +bool create_path(const char *dirname) { + if (dirname == NULL) // nothing to do + return false; + + if ((strlen(dirname) == 1) && (dirname[0] == '/')) + return true; + + if ((strlen(dirname) == 2) && (dirname[1] == ':')) + return true; + + if (fileExists(dirname) == 0) { + + char *bs = strrchr(dirname, '\\'); + char *fs = strrchr(dirname, '/'); + + if ((bs == NULL) && (fs != NULL)) { + *fs = 0x00; + create_path (dirname); + *fs = '/'; + } + + if ((bs != NULL) && (fs == NULL)) { + *bs = 0x00; + create_path (dirname); + *bs = '\\'; + } + + if ((bs != NULL) && (fs != NULL)) { + if (strlen (bs) > strlen (fs)) { + *fs = 0x00; // No slash + create_path (dirname); + *fs = '/'; + } else { + *bs = 0x00; + create_path (dirname); + *bs = '\\'; + } + + } + + if (make_dir(dirname) != 0) { + PrintAndLogEx(ERR, "could not create directory.... "_RED_("%s"),dirname); + return false; + } + } + return true; +} +*/ +/* +bool setDefaultPath (savePaths_t pathIndex,const char *Path) { + + if (pathIndex < spItemCount) { + if ((Path == NULL) && (session.defaultPaths[pathIndex] != NULL)) { + free (session.defaultPaths[pathIndex]); + session.defaultPaths[pathIndex] = NULL; + } + + if (Path != NULL) { + session.defaultPaths[pathIndex] = (char *)realloc(session.defaultPaths[pathIndex], strlen(Path) + 1); + strcpy(session.defaultPaths[pathIndex], Path); + } + } else { + return false; + } + + return true; +} +*/ static char *filenamemcopy(const char *preferredName, const char *suffix) { if (preferredName == NULL) return NULL; if (suffix == NULL) return NULL; @@ -905,10 +986,10 @@ int loadFileJSON(const char *preferredName, void *data, size_t maxdatalen, size_ } *datalen = sptr; } + PrintAndLogEx(SUCCESS, "loaded from JSON file " _YELLOW_("%s"), fileName); if (!strcmp(ctype, "settings")) { preferences_load_callback(root); } - PrintAndLogEx(SUCCESS, "loaded from JSON file " _YELLOW_("%s"), fileName); out: json_decref(root); free(fileName); diff --git a/client/src/fileutils.h b/client/src/fileutils.h index ff43b3f7e..2f55a3c1a 100644 --- a/client/src/fileutils.h +++ b/client/src/fileutils.h @@ -74,6 +74,9 @@ typedef enum { } DumpFileType_t; int fileExists(const char *filename); +//bool create_path(const char *dirname); +//bool setDefaultPath (savePaths_t pathIndex,const char *Path); // set a path in the path list session.defaultPaths + /** * @brief Utility function to save data to a binary file. This method takes a preferred name, but if that diff --git a/client/src/loclass/cipher.c b/client/src/loclass/cipher.c index 13f686b51..af5d4f3a3 100644 --- a/client/src/loclass/cipher.c +++ b/client/src/loclass/cipher.c @@ -68,6 +68,7 @@ typedef struct { * T (x 0 x 1 . . . . . . x 15 ) = x 0 ⊕ x 1 ⊕ x 5 ⊕ x 7 ⊕ x 10 ⊕ x 11 ⊕ x 14 ⊕ x 15 . **/ static bool T(State state) { +/* bool x0 = state.t & 0x8000; bool x1 = state.t & 0x4000; bool x5 = state.t & 0x0400; @@ -77,19 +78,33 @@ static bool T(State state) { bool x14 = state.t & 0x0002; bool x15 = state.t & 0x0001; return x0 ^ x1 ^ x5 ^ x7 ^ x10 ^ x11 ^ x14 ^ x15; +*/ +#define _x0 ((state.t & 0x8000) >> 15 ) +#define _x1 ((state.t & 0x4000) >> 14 ) +#define _x5 ((state.t & 0x0400) >> 10 ) +#define _x7 ((state.t & 0x0100) >> 8 ) +#define _x10 ((state.t & 0x0020) >> 5 ) +#define _x11 ((state.t & 0x0010) >> 4 ) +#define _x14 ((state.t & 0x0002) >> 1 ) +#define _x15 (state.t & 0x0001) + return (_x0) ^ (_x1) ^ (_x5) ^ (_x7) ^ (_x10) ^ (_x11) ^ (_x14) ^ (_x15); } /** * Similarly, the feedback function for the bottom register B : F 8/2 → F 2 is defined as * B(x 0 x 1 . . . x 7 ) = x 1 ⊕ x 2 ⊕ x 3 ⊕ x 7 . **/ -static bool B(State state) { +/*static bool B(State state) { bool x1 = state.b & 0x40; bool x2 = state.b & 0x20; bool x3 = state.b & 0x10; bool x7 = state.b & 0x01; return x1 ^ x2 ^ x3 ^ x7; } +*/ +#define B(x) (((x.b & 0x40) >> 6) ^ ((x.b & 0x20) >> 5) ^ ((x.b & 0x10) >> 4) ^ (x.b & 0x01)) +// 12 3456 +// 0100 0000 /** * Definition 3 (Selection function). The selection function select : F 2 × F 2 × @@ -99,14 +114,28 @@ static bool B(State state) { * z 2 = (r 3 ∧ r 5 ) ⊕ (r 4 ∧ r 6 ) ⊕ r 7 ⊕ x **/ static uint8_t _select(bool x, bool y, uint8_t r) { - bool r0 = r >> 7 & 0x1; - bool r1 = r >> 6 & 0x1; - bool r2 = r >> 5 & 0x1; - bool r3 = r >> 4 & 0x1; - bool r4 = r >> 3 & 0x1; - bool r5 = r >> 2 & 0x1; - bool r6 = r >> 1 & 0x1; - bool r7 = r & 0x1; + #define _r0 ((r >> 7) & 0x01) + #define _r1 ((r >> 6) & 0x01) + #define _r2 ((r >> 5) & 0x01) + #define _r3 ((r >> 4) & 0x01) + #define _r4 ((r >> 3) & 0x01) + #define _r5 ((r >> 2) & 0x01) + #define _r6 ((r >> 1) & 0x01) + #define _r7 (r & 0x01) + + #define _z0 ( (_r0 & _r2) ^ ( _r1 & (!_r3)) ^ (_r2 | _r4) ) + #define _z1 ( (_r0 | _r2) ^ ( _r5 | _r7) ^_r1 ^ _r6 ^ x ^ y ) + #define _z2 ( (_r3 & (!_r5)) ^ (_r4 & _r6) ^ _r7 ^ x ) + +/* + uint8_t r0 = r >> 7 & 0x1; + uint8_t r1 = r >> 6 & 0x1; + uint8_t r2 = r >> 5 & 0x1; + uint8_t r3 = r >> 4 & 0x1; + uint8_t r4 = r >> 3 & 0x1; + uint8_t r5 = r >> 2 & 0x1; + uint8_t r6 = r >> 1 & 0x1; + uint8_t r7 = r & 0x1; bool z0 = (r0 & r2) ^ (r1 & (!r3)) ^ (r2 | r4); bool z1 = (r0 | r2) ^ (r5 | r7) ^ r1 ^ r6 ^ x ^ y; @@ -115,6 +144,11 @@ static uint8_t _select(bool x, bool y, uint8_t r) { // The three bitz z0.. z1 are packed into a uint8_t: // 00000ZZZ //Return value is a uint8_t + return ((z0 << 2) & 4) | ((z1 << 1) & 2) | (z2 & 1); +*/ + return ((_z0 << 2) & 4) | ((_z1 << 1) & 2) | (_z2 & 1); + +/* uint8_t retval = 0; retval |= (z0 << 2) & 4; retval |= (z1 << 1) & 2; @@ -122,6 +156,7 @@ static uint8_t _select(bool x, bool y, uint8_t r) { // Return value 0 <= retval <= 7 return retval; +*/ } /** diff --git a/client/src/mifare/desfire_crypto.c b/client/src/mifare/desfire_crypto.c index e4caaae26..a0e066b44 100644 --- a/client/src/mifare/desfire_crypto.c +++ b/client/src/mifare/desfire_crypto.c @@ -281,6 +281,10 @@ void cmac_generate_subkeys(desfirekey_t key) { void cmac(const desfirekey_t key, uint8_t *ivect, const uint8_t *data, size_t len, uint8_t *cmac) { int kbs = key_block_size(key); + if (kbs == 0) { + return; + } + uint8_t *buffer = malloc(padded_data_length(len, kbs)); memcpy(buffer, data, len); diff --git a/client/src/preferences.c b/client/src/preferences.c index a1de6e4e2..b0534a671 100644 --- a/client/src/preferences.c +++ b/client/src/preferences.c @@ -23,11 +23,9 @@ #include #include "cmdparser.h" #include -// #include +#include #include -//#include "proxgui.h" -//extern void SetWindowsPosition (void); static int CmdHelp(const char *Cmd); static int setCmdHelp(const char *Cmd); @@ -41,42 +39,70 @@ static int setCmdHelp(const char *Cmd); #endif static char *prefGetFilename(void) { - char *Path; + char *path; - if (searchHomeFilePath(&Path, preferencesFilename, false) == PM3_SUCCESS) - return Path; + if (searchHomeFilePath(&path, preferencesFilename, false) == PM3_SUCCESS) + return path; else return preferencesFilename; } int preferences_load(void) { - PrintAndLogEx(INFO, "Looking for preferences..."); - // Set all defaults - session.client_debug_level = OFF; + session.client_debug_level = cdbOFF; + // session.device_debug_level = ddbOFF; session.window_changed = false; - session.window_plot_xpos = 10; - session.window_plot_ypos = 30; - session.window_plot_hsize = 400; - session.window_plot_wsize = 800; - session.window_overlay_xpos = session.window_plot_xpos; - session.window_overlay_ypos = 60 + session.window_plot_ypos + session.window_plot_hsize; - session.window_overlay_hsize = 200; - session.window_overlay_wsize = session.window_plot_wsize; + session.plot.x = 10; + session.plot.y = 30; + session.plot.h = 400; + session.plot.w = 800; + session.overlay.x = session.plot.x; + session.overlay.y = 60 + session.plot.y + session.plot.h; + session.overlay.h = 200; + session.overlay.w = session.plot.w; session.emoji_mode = ALIAS; session.show_hints = false; session.supports_colors = false; +// setDefaultPath (spDefault, ""); +// setDefaultPath (spDump, ""); +// setDefaultPath (spTrace, ""); + + /* + // default save path + if (get_my_user_directory() != NULL) // should return path to .proxmark3 folder + setDefaultPath (spDefault, get_my_user_directory()); + else + setDefaultPath (spDefault, "."); + + // default dump path + if (get_my_user_directory() != NULL) // should return path to .proxmark3 folder + setDefaultPath (spDump, get_my_user_directory()); + else + setDefaultPath (spDump, "."); + + // default dump path + if (get_my_user_directory() != NULL) // should return path to .proxmark3 folder + setDefaultPath (spTrace, get_my_user_directory()); + else + setDefaultPath (spTrace, "."); + */ // loadFileJson wants these, so pass in place holder values, though not used // in settings load; uint8_t dummyData = 0x00; size_t dummyDL = 0x00; - if (loadFileJSON(prefGetFilename(), &dummyData, sizeof(dummyData), &dummyDL) == PM3_SUCCESS) { - session.preferences_loaded = true; + // to better control json cant find file error msg. + char* fn = prefGetFilename(); + if (fileExists(fn)) { + PrintAndLogEx(INFO, "Loading Preferences..."); + if (loadFileJSON(fn, &dummyData, sizeof(dummyData), &dummyDL) == PM3_SUCCESS) { + session.preferences_loaded = true; + } } + free(fn); // Note, if session.settings_loaded == false then the settings_save // will be called in main () to save settings as set in defaults and main() checks. @@ -87,21 +113,34 @@ int preferences_load(void) { int preferences_save(void) { // Note sure if backup has value ? - char backupFilename[FILENAME_MAX + sizeof(preferencesFilename) + 10] = {0}; + PrintAndLogEx(INFO, "Saving Preferences..."); - PrintAndLogEx(INFO, "Saving preferences ..."); - snprintf(backupFilename, sizeof(backupFilename) - 1, "%s.bak", prefGetFilename()); + char* fn = prefGetFilename(); + int fnLen = strlen(fn) + 5; // .bak\0 + + // [FILENAME_MAX+sizeof(preferencesFilename)+10] + char* backupFilename = (char *)calloc(fnLen, sizeof(uint8_t)); + if (backupFilename == NULL) { + PrintAndLogEx(ERR, "failed to allocate memory"); + free(fn); + return PM3_EMALLOC; + } + snprintf(backupFilename, fnLen, "%s.bak", fn); if (fileExists(backupFilename)) { if (remove(backupFilename) != 0) { PrintAndLogEx(FAILED, "Error - could not delete old settings backup file \"%s\"", backupFilename); + free(fn); + free(backupFilename); return PM3_ESOFT; } } - if (fileExists(prefGetFilename())) { - if (rename(prefGetFilename(), backupFilename) != 0) { - PrintAndLogEx(FAILED, "Error - could not backup settings file \"%s\" to \"%s\"", prefGetFilename(), backupFilename); + if (fileExists(fn)) { + if (rename(fn, backupFilename) != 0) { + PrintAndLogEx(FAILED, "Error - could not backup settings file \"%s\" to \"%s\"", fn, backupFilename); + free(fn); + free(backupFilename); return PM3_ESOFT; } } @@ -109,9 +148,11 @@ int preferences_save(void) { uint8_t dummyData = 0x00; size_t dummyDL = 0x00; - if (saveFileJSON(prefGetFilename(), jsfSettings, &dummyData, dummyDL) != PM3_SUCCESS) - PrintAndLogEx(ERR, "Error saving preferences to \"%s\"", prefGetFilename()); + if (saveFileJSON(fn, jsfSettings, &dummyData, dummyDL) != PM3_SUCCESS) + PrintAndLogEx(ERR, "Error saving preferences to \"%s\"", fn); + free(fn); + free(backupFilename); return PM3_SUCCESS; } @@ -119,33 +160,6 @@ void preferences_save_callback(json_t *root) { JsonSaveStr(root, "FileType", "settings"); - // Log level, convert to text - switch (session.client_debug_level) { - case OFF: - JsonSaveStr(root, "client.debug.level", "off"); - break; - case SIMPLE: - JsonSaveStr(root, "client.debug.level", "simple"); - break; - case FULL: - JsonSaveStr(root, "client.debug.level", "full"); - break; - default: - JsonSaveStr(root, "logging.level", "NORMAL"); - } - - // Plot window - JsonSaveInt(root, "window.plot.xpos", session.window_plot_xpos); - JsonSaveInt(root, "window.plot.ypos", session.window_plot_ypos); - JsonSaveInt(root, "window.plot.hsize", session.window_plot_hsize); - JsonSaveInt(root, "window.plot.wsize", session.window_plot_wsize); - - // Overlay/Slider window - JsonSaveInt(root, "window.overlay.xpos", session.window_overlay_xpos); - JsonSaveInt(root, "window.overlay.ypos", session.window_overlay_ypos); - JsonSaveInt(root, "window.overlay.hsize", session.window_overlay_hsize); - JsonSaveInt(root, "window.overlay.wsize", session.window_overlay_wsize); - // Emoji switch (session.emoji_mode) { case ALIAS: @@ -167,6 +181,58 @@ void preferences_save_callback(json_t *root) { JsonSaveBoolean(root, "show.hints", session.show_hints); JsonSaveBoolean(root, "os.supports.colors", session.supports_colors); + +// JsonSaveStr(root, "file.default.savepath", session.defaultPaths[spDefault]); +// JsonSaveStr(root, "file.default.dumppath", session.defaultPaths[spDump]); +// JsonSaveStr(root, "file.default.tracepath", session.defaultPaths[spTrace]); + + // Plot window + JsonSaveInt(root, "window.plot.xpos", session.plot.x); + JsonSaveInt(root, "window.plot.ypos", session.plot.y); + JsonSaveInt(root, "window.plot.hsize", session.plot.h); + JsonSaveInt(root, "window.plot.wsize", session.plot.w); + + // Overlay/Slider window + JsonSaveInt(root, "window.overlay.xpos", session.overlay.x); + JsonSaveInt(root, "window.overlay.ypos", session.overlay.y); + JsonSaveInt(root, "window.overlay.hsize", session.overlay.h); + JsonSaveInt(root, "window.overlay.wsize", session.overlay.w); + + // Log level, convert to text + switch (session.client_debug_level) { + case cdbOFF: + JsonSaveStr(root, "client.debug.level", "off"); + break; + case cdbSIMPLE: + JsonSaveStr(root, "client.debug.level", "simple"); + break; + case cdbFULL: + JsonSaveStr(root, "client.debug.level", "full"); + break; + default: + JsonSaveStr(root, "logging.level", "NORMAL"); + } + /* + switch (session.device_debug_level) { + case ddbOFF: + JsonSaveStr(root, "device.debug.level", "off"); + break; + case ddbERROR: + JsonSaveStr(root, "device.debug.level", "error"); + break; + case ddbINFO: + JsonSaveStr(root, "device.debug.level", "info"); + break; + case ddbDEBUG: + JsonSaveStr(root, "device.debug.level", "debug"); + break; + case ddbEXTENDED: + JsonSaveStr(root, "device.debug.level", "extended"); + break; + default: + JsonSaveStr(root, "logging.level", "NORMAL"); + } + */ } void preferences_load_callback(json_t *root) { @@ -180,30 +246,42 @@ void preferences_load_callback(json_t *root) { if (json_unpack_ex(root, &up_error, 0, "{s:s}", "client.debug.level", &s1) == 0) { strncpy(tempStr, s1, sizeof(tempStr) - 1); str_lower(tempStr); - if (strncmp(tempStr, "off", 3) == 0) session.client_debug_level = OFF; - if (strncmp(tempStr, "simple", 6) == 0) session.client_debug_level = SIMPLE; - if (strncmp(tempStr, "full", 4) == 0) session.client_debug_level = FULL; + if (strncmp(tempStr, "off", 3) == 0) session.client_debug_level = cdbOFF; + if (strncmp(tempStr, "simple", 6) == 0) session.client_debug_level = cdbSIMPLE; + if (strncmp(tempStr, "full", 4) == 0) session.client_debug_level = cdbFULL; } + /* + // default save path + if (json_unpack_ex(root, &up_error, 0, "{s:s}", "file.default.savepath", &s1) == 0) + setDefaultPath (spDefault,s1); + // default dump path + if (json_unpack_ex(root, &up_error, 0, "{s:s}", "file.default.dumppath", &s1) == 0) + setDefaultPath (spDump,s1); + + // default trace path + if (json_unpack_ex(root, &up_error, 0, "{s:s}", "file.default.tracepath", &s1) == 0) + setDefaultPath (spTrace,s1); + */ // window plot if (json_unpack_ex(root, &up_error, 0, "{s:i}", "window.plot.xpos", &i1) == 0) - session.window_plot_xpos = i1; + session.plot.x = i1; if (json_unpack_ex(root, &up_error, 0, "{s:i}", "window.plot.ypos", &i1) == 0) - session.window_plot_ypos = i1; + session.plot.y = i1; if (json_unpack_ex(root, &up_error, 0, "{s:i}", "window.plot.hsize", &i1) == 0) - session.window_plot_hsize = i1; + session.plot.h = i1; if (json_unpack_ex(root, &up_error, 0, "{s:i}", "window.plot.wsize", &i1) == 0) - session.window_plot_wsize = i1; + session.plot.w = i1; // overlay/slider plot if (json_unpack_ex(root, &up_error, 0, "{s:i}", "window.overlay.xpos", &i1) == 0) - session.window_overlay_xpos = i1; + session.overlay.x = i1; if (json_unpack_ex(root, &up_error, 0, "{s:i}", "window.overlay.ypos", &i1) == 0) - session.window_overlay_ypos = i1; + session.overlay.y = i1; if (json_unpack_ex(root, &up_error, 0, "{s:i}", "window.overlay.hsize", &i1) == 0) - session.window_overlay_hsize = i1; + session.overlay.h = i1; if (json_unpack_ex(root, &up_error, 0, "{s:i}", "window.overlay.wsize", &i1) == 0) - session.window_overlay_wsize = i1; + session.overlay.w = i1; // show options if (json_unpack_ex(root, &up_error, 0, "{s:s}", "show.emoji", &s1) == 0) { @@ -220,7 +298,18 @@ void preferences_load_callback(json_t *root) { if (json_unpack_ex(root, &up_error, 0, "{s:b}", "os.supports.colors", &b1) == 0) session.supports_colors = b1; - + /* + // Logging Level + if (json_unpack_ex(root, &up_error, 0, "{s:s}", "device.debug.level", &s1) == 0) { + strncpy(tempStr, s1, sizeof(tempStr) - 1); + str_lower(tempStr); + if (strncmp(tempStr, "off", 3) == 0) session.device_debug_level = ddbOFF; + if (strncmp(tempStr, "error", 5) == 0) session.device_debug_level = ddbERROR; + if (strncmp(tempStr, "info", 4) == 0) session.device_debug_level = ddbINFO; + if (strncmp(tempStr, "debug", 5) == 0) session.device_debug_level = ddbDEBUG; + if (strncmp(tempStr, "extended", 8) == 0) session.device_debug_level = ddbEXTENDED; + } + */ } // Help Functions @@ -233,7 +322,6 @@ static int usage_set_emoji() { PrintAndLogEx(NORMAL, " "_GREEN_("emoji")" - Show amoji"); PrintAndLogEx(NORMAL, " "_GREEN_("alttext")" - Show alt text for emoji"); PrintAndLogEx(NORMAL, " "_GREEN_("erase")" - Dont show emoji or text"); - return PM3_SUCCESS; } @@ -243,40 +331,63 @@ static int usage_set_color() { PrintAndLogEx(NORMAL, " "_GREEN_("help")" - This help"); PrintAndLogEx(NORMAL, " "_GREEN_("off")" - Dont use colors"); PrintAndLogEx(NORMAL, " "_GREEN_("ansi")" - Use ANSI colors"); - return PM3_SUCCESS; } static int usage_set_debug() { - PrintAndLogEx(NORMAL, "Usage: pref set debug "); + PrintAndLogEx(NORMAL, "Usage: pref set clientdebug "); PrintAndLogEx(NORMAL, "Options:"); PrintAndLogEx(NORMAL, " "_GREEN_("help")" - This help"); PrintAndLogEx(NORMAL, " "_GREEN_("off")" - no debug messages"); PrintAndLogEx(NORMAL, " "_GREEN_("simple")" - simple debug messages"); PrintAndLogEx(NORMAL, " "_GREEN_("full")" - full debug messages"); + return PM3_SUCCESS; +} +/* +static int usage_set_devicedebug() { + PrintAndLogEx(NORMAL, "Usage: pref set devicedebug "); + PrintAndLogEx(NORMAL, "Options:"); + PrintAndLogEx(NORMAL, " "_GREEN_("help")" - This help"); + PrintAndLogEx(NORMAL, " "_GREEN_("off")" - no debug messages"); + PrintAndLogEx(NORMAL, " "_GREEN_("error")" - error messages"); + PrintAndLogEx(NORMAL, " "_GREEN_("info")" - info messages"); + PrintAndLogEx(NORMAL, " "_GREEN_("debug")" - debug messages"); + PrintAndLogEx(NORMAL, " "_GREEN_("extended")" - extended debug messages"); return PM3_SUCCESS; } +*/ static int usage_set_hints() { PrintAndLogEx(NORMAL, "Usage: pref set hints "); PrintAndLogEx(NORMAL, "Options:"); PrintAndLogEx(NORMAL, " "_GREEN_("help")" - This help"); PrintAndLogEx(NORMAL, " "_GREEN_("off")" - Dont display hints"); PrintAndLogEx(NORMAL, " "_GREEN_("on")" - Display hints"); + return PM3_SUCCESS; +} +/* +static int usage_set_savePaths() { + PrintAndLogEx(NORMAL, "Usage: pref set savepaths [help] [create] [default ] [dump ] [trace ]"); + PrintAndLogEx(NORMAL, "Options:"); + PrintAndLogEx(NORMAL, " "_GREEN_("help")" - This help"); + PrintAndLogEx(NORMAL, " "_GREEN_("create")" - Create directory if it does not exist"); + PrintAndLogEx(NORMAL, " "_GREEN_("default")" - Deafult path"); + PrintAndLogEx(NORMAL, " "_GREEN_("dump")" - Dump path"); + PrintAndLogEx(NORMAL, " "_GREEN_("trace")" - Trace help"); return PM3_SUCCESS; } - +*/ // Preference Processing Functions -typedef enum preferenceId {prefNONE, prefHELP, prefEMOJI, prefCOLOR, prefPLOT, prefOVERLAY, prefHINTS, prefCLIENTDEBUG} preferenceId_t; -typedef enum prefShowOpt {prefShowNone, prefShowOLD, prefShowNEW} prefShowOpt_t; +// typedef enum preferenceId {prefNONE,prefHELP,prefEMOJI,prefCOLOR,prefPLOT,prefOVERLAY,prefHINTS,prefCLIENTDEBUG} preferenceId_t; +typedef enum prefShowOpt {prefShowNone, prefShowOLD, prefShowNEW} prefShowOpt_t; const char *prefShowMsg(prefShowOpt_t Opt) { switch (Opt) { case prefShowOLD: - return _YELLOW_("[old]"); //strncpy(Msg,"Before ",sizeof(Msg)-1); break; + return _YELLOW_("[old]"); case prefShowNEW: - return _GREEN_("[new]"); // strncpy(Msg,"After ",sizeof(Msg)-1); break; + return _GREEN_("[new]"); case prefShowNone: return ""; } @@ -309,41 +420,87 @@ void showColorState(prefShowOpt_t Opt) { if (session.supports_colors) PrintAndLogEx(NORMAL, " %s color.................. "_GREEN_("ansi"), prefShowMsg(Opt)); else - PrintAndLogEx(NORMAL, " %s color.................. "_GREEN_("off"), prefShowMsg(Opt)); + PrintAndLogEx(NORMAL, " %s color.................. "_WHITE_("off"), prefShowMsg(Opt)); } void showClientDebugState(prefShowOpt_t Opt) { switch (session.client_debug_level) { - case OFF: - PrintAndLogEx(NORMAL, " %s client debug........... "_GREEN_("off"), prefShowMsg(Opt)); + case cdbOFF: + PrintAndLogEx(NORMAL, " %s client debug........... "_WHITE_("off"), prefShowMsg(Opt)); break; - case SIMPLE: + case cdbSIMPLE: PrintAndLogEx(NORMAL, " %s client debug........... "_GREEN_("simple"), prefShowMsg(Opt)); break; - case FULL: + case cdbFULL: PrintAndLogEx(NORMAL, " %s client debug........... "_GREEN_("full"), prefShowMsg(Opt)); break; default: PrintAndLogEx(NORMAL, " %s client debug........... "_RED_("unknown"), prefShowMsg(Opt)); } } +/* +void showDeviceDebugState(prefShowOpt_t Opt) { + switch (session.device_debug_level) { + case ddbOFF: + PrintAndLogEx(NORMAL, " %s device debug........... "_WHITE_("off"), prefShowMsg(Opt)); + break; + case ddbERROR: + PrintAndLogEx(NORMAL, " %s device debug........... "_GREEN_("error"), prefShowMsg(Opt)); + break; + case ddbINFO: + PrintAndLogEx(NORMAL, " %s device debug........... "_GREEN_("info"), prefShowMsg(Opt)); + break; + case ddbDEBUG: + PrintAndLogEx(NORMAL, " %s device debug........... "_GREEN_("debug"), prefShowMsg(Opt)); + break; + case ddbEXTENDED: + PrintAndLogEx(NORMAL, " %s device debug........... "_GREEN_("extended"), prefShowMsg(Opt)); + break; + default: + PrintAndLogEx(NORMAL, " %s device debug........... "_RED_("unknown"), prefShowMsg(Opt)); + } +} +*/ +/* +void showSavePathState(savePaths_t pathIndex, prefShowOpt_t Opt) { + char tempStr[50]; + + switch (pathIndex) { + case spDefault: + strcpy (tempStr,"default save path......"); + break; + case spDump: + strcpy (tempStr,"dump save path........."); + break; + case spTrace: + strcpy (tempStr,"trace save path........"); + break; + default: + strcpy (tempStr,_RED_("unknown")" save path......"); + } + if ((session.defaultPaths[pathIndex] == NULL) || (strcmp(session.defaultPaths[pathIndex],"") == 0)) + PrintAndLogEx(NORMAL, " %s %s "_WHITE_("not set"), prefShowMsg(Opt),tempStr); + else + PrintAndLogEx(NORMAL, " %s %s "_GREEN_("%s"), prefShowMsg(Opt), tempStr, session.defaultPaths[pathIndex]); +} +*/ void showPlotPosState(void) { PrintAndLogEx(NORMAL, " Plot window............ X "_GREEN_("%4d")" Y "_GREEN_("%4d")" H "_GREEN_("%4d")" W "_GREEN_("%4d"), - session.window_plot_xpos, session.window_plot_ypos, session.window_plot_hsize, session.window_plot_wsize); + session.plot.x, session.plot.y, session.plot.h, session.plot.w); } void showOverlayPosState(void) { PrintAndLogEx(NORMAL, " Slider/Overlay window.. X "_GREEN_("%4d")" Y "_GREEN_("%4d")" H "_GREEN_("%4d")" W "_GREEN_("%4d"), - session.window_overlay_xpos, session.window_overlay_ypos, session.window_overlay_hsize, session.window_overlay_wsize); + session.overlay.x, session.overlay.y, session.overlay.h, session.overlay.w); } void showHintsState(prefShowOpt_t Opt) { if (session.show_hints) - PrintAndLogEx(NORMAL, " %s Hints.................. "_GREEN_("on"), prefShowMsg(Opt)); + PrintAndLogEx(NORMAL, " %s hints.................. "_GREEN_("on"), prefShowMsg(Opt)); else - PrintAndLogEx(NORMAL, " %s Hints.................. "_GREEN_("off"), prefShowMsg(Opt)); + PrintAndLogEx(NORMAL, " %s hints.................. "_WHITE_("off"), prefShowMsg(Opt)); } static int setCmdEmoji(const char *Cmd) { @@ -465,15 +622,15 @@ static int setCmdDebug(const char *Cmd) { return usage_set_debug(); if (strncmp(strOpt, "off", 3) == 0) { validValue = true; - newValue = OFF; + newValue = cdbOFF; } if (strncmp(strOpt, "simple", 6) == 0) { validValue = true; - newValue = SIMPLE; + newValue = cdbSIMPLE; } if (strncmp(strOpt, "full", 4) == 0) { validValue = true; - newValue = FULL; + newValue = cdbFULL; } if (validValue) { @@ -496,7 +653,73 @@ static int setCmdDebug(const char *Cmd) { return PM3_SUCCESS; } +/* +static int setCmdDeviceDebug (const char *Cmd) +{ + uint8_t cmdp = 0; + bool errors = false; + bool validValue = false; + char strOpt[50]; + devicedebugLevel_t newValue = session.device_debug_level; + if (param_getchar(Cmd, cmdp) == 0x00) + return usage_set_devicedebug (); + + while ((param_getchar(Cmd, cmdp) != 0x00) && !errors) { + + if (param_getstr(Cmd, cmdp++, strOpt, sizeof(strOpt)) != 0) { + str_lower(strOpt); // convert to lowercase + + if (strncmp (strOpt,"help",4) == 0) + return usage_set_devicedebug(); + if (strncmp (strOpt,"off",3) == 0) { + validValue = true; + newValue = ddbOFF; + } + if (strncmp (strOpt,"error",5) == 0) { + validValue = true; + newValue = ddbERROR; + } + if (strncmp (strOpt,"info",4) == 0) { + validValue = true; + newValue = ddbINFO; + } + if (strncmp (strOpt,"debug",5) == 0) { + validValue = true; + newValue = ddbDEBUG; + } + if (strncmp (strOpt,"extended",8) == 0) { + validValue = true; + newValue = ddbEXTENDED; + } + + if (validValue) { + if (session.device_debug_level != newValue) {// changed + showDeviceDebugState (prefShowOLD); + session.device_debug_level = newValue; + showDeviceDebugState (prefShowNEW); + preferences_save (); + } else { + PrintAndLogEx(INFO,"nothing changed"); + showDeviceDebugState (prefShowNone); + } + if (session.pm3_present) { + PrintAndLogEx (INFO,"setting device debug loglevel"); + SendCommandNG(CMD_SET_DBGMODE, &session.device_debug_level, 1); + PacketResponseNG resp; + if (WaitForResponseTimeout(CMD_SET_DBGMODE, &resp, 2000) == false) + PrintAndLogEx (INFO,"failed to set device debug loglevel"); + } + } else { + PrintAndLogEx(ERR,"invalid option"); + return usage_set_devicedebug(); + } + } + } + + return PM3_SUCCESS; +} +*/ static int setCmdHint(const char *Cmd) { uint8_t cmdp = 0; bool errors = false; @@ -542,76 +765,204 @@ static int setCmdHint(const char *Cmd) { return PM3_SUCCESS; } +/* +static int setCmdSavePaths (const char *Cmd) { + uint8_t cmdp = 0; + bool errors = false; + // bool validValue = false; + char *strOpt = NULL; + int optLen = 0; + char *newValue = NULL; + bool createDir = false; + savePaths_t pathItem = spItemCount; -static command_t setCommandTable[] = { - {"help", setCmdHelp, AlwaysAvailable, "This help"}, - {"emoji", setCmdEmoji, AlwaysAvailable, "Set emoji display"}, - {"color", setCmdColor, AlwaysAvailable, "Set color support"}, - {"debug", setCmdDebug, AlwaysAvailable, "Set client debug level"}, - {"hints", setCmdHint, AlwaysAvailable, "Set hint display"}, + + if (param_getchar(Cmd, cmdp) == 0x00) + return usage_set_savePaths(); + + while ((param_getchar(Cmd, cmdp) != 0x00) && !errors) { + + optLen = param_getlength(Cmd, cmdp)+1; + strOpt = (char *)realloc(strOpt,optLen+1);//, sizeof(uint8_t)); + + if (param_getstr(Cmd, cmdp++, strOpt, optLen) != 0) { + str_lower(strOpt); // convert to lowercase + + if (strncmp(strOpt, "help", 4) == 0) + return usage_set_savePaths(); + + if (strncmp(strOpt, "create", 6) == 0) { + // check if 2 more options. + if (param_getlength(Cmd, cmdp+1) == 0) // should have min 2 more options + return usage_set_savePaths(); + createDir = true; + } else { + if ((strncmp(strOpt, "default", 7) == 0) || + (strncmp(strOpt, "dump", 4) == 0) || + (strncmp(strOpt, "trace", 5) == 0)) { + + // Get Path + optLen = param_getlength(Cmd, cmdp) + 1; + newValue = (char *)realloc(newValue, optLen+1); + if (param_getstr(Cmd, cmdp++, newValue, optLen) == 0) { + PrintAndLogEx(INFO, "missing %s path",strOpt); + return usage_set_savePaths(); + } + // remove trailing slash. + if ((newValue[strlen(newValue)-1] == '/') || (newValue[strlen(newValue)-1] == '\\')) + newValue[strlen(newValue)-1] = 0x00; + + // Check path + if (!fileExists(newValue) && !createDir) { + PrintAndLogEx(ERR,"path does not exist... "_RED_("%s"),newValue); + } else { + // do we need to create it + // if (!fileExists(newValue)) + // create_path (newValue); //mkdir (newValue,0x777); + + pathItem = spItemCount; + if (strncmp(strOpt, "default", 7) == 0) pathItem = spDefault; + if (strncmp(strOpt, "dump", 4) == 0) pathItem = spDump; + if (strncmp(strOpt, "trace", 5) == 0) pathItem = spTrace; + + if (pathItem < spItemCount) { + if (strcmp(newValue, session.defaultPaths[pathItem]) != 0) { + showSavePathState(pathItem, prefShowOLD); + setDefaultPath (pathItem, newValue); + showSavePathState(pathItem, prefShowNEW); + preferences_save(); + } else { + PrintAndLogEx(INFO, "nothing changed"); + showSavePathState(pathItem, prefShowNone); + } + } + } + } else { + return usage_set_savePaths(); + } + } + } + } + + // clean up + if (strOpt != NULL) free (strOpt); + if (newValue != NULL) free (newValue); + + return PM3_SUCCESS; +} +*/ + +int getCmdHelp(const char *Cmd) { + return PM3_SUCCESS; +} + +int getCmdEmoji(const char *Cmd) { + showEmojiState(prefShowNone); + return PM3_SUCCESS; +} + +int getCmdHint(const char *Cmd) { + showHintsState(prefShowNone); + return PM3_SUCCESS; +} + +int getCmdColor(const char *Cmd) { + showColorState(prefShowNone); + return PM3_SUCCESS; +} + +int getCmdDebug(const char *Cmd) { + showClientDebugState(prefShowNone); + return PM3_SUCCESS; +} + +static command_t getCommandTable[] = { +// {"help", getCmdHelp, AlwaysAvailable, "This help"}, + {"emoji", getCmdEmoji, AlwaysAvailable, "Get emoji display preference"}, + {"hints", getCmdHint, AlwaysAvailable, "Get hint display preference"}, + {"color", getCmdColor, AlwaysAvailable, "Get color support preference"}, + // {"defaultsavepaths", getCmdSavePaths, AlwaysAvailable, "... to be adjusted next ... "}, + {"clientdebug", getCmdDebug, AlwaysAvailable, "Get client debug level preference"}, + // {"devicedebug", getCmdDeviceDebug, AlwaysAvailable, "Get device debug level"}, {NULL, NULL, NULL, NULL} }; +static command_t setCommandTable[] = { + {"help", setCmdHelp, AlwaysAvailable, "This help"}, + {"emoji", setCmdEmoji, AlwaysAvailable, "Set emoji display"}, + {"hints", setCmdHint, AlwaysAvailable, "Set hint display"}, + {"color", setCmdColor, AlwaysAvailable, "Set color support"}, + // {"defaultsavepaths", setCmdSavePaths, AlwaysAvailable, "... to be adjusted next ... "}, + {"clientdebug", setCmdDebug, AlwaysAvailable, "Set client debug level"}, + // {"devicedebug", setCmdDeviceDebug, AlwaysAvailable, "Set device debug level"}, + {NULL, NULL, NULL, NULL} +}; static int setCmdHelp(const char *Cmd) { (void)Cmd; // Cmd is not used so far CmdsHelp(setCommandTable); - return PM3_SUCCESS; } +int CmdPrefGet(const char *Cmd) { + clearCommandBuffer(); + return CmdsParse(getCommandTable, Cmd); +} + int CmdPrefSet(const char *Cmd) { clearCommandBuffer(); - return CmdsParse(setCommandTable, Cmd); } static int CmdPrefShow(const char *Cmd) { + char* fn = prefGetFilename(); PrintAndLogEx(NORMAL, ""); - PrintAndLogEx(NORMAL, _BLUE_("Preferences")); + PrintAndLogEx(NORMAL, _CYAN_("Preferences loaded from %s"), fn); - if (!session. preferences_loaded) { - PrintAndLogEx(ERR, "Preferneces not loaded"); + free(fn); + + if (!session.preferences_loaded) { + PrintAndLogEx(ERR, "Preferences not loaded"); return PM3_ESOFT; } - PrintAndLogEx(NORMAL, " preference file........ "_GREEN_("%s"), prefGetFilename()); + // PrintAndLogEx(NORMAL, " preference file........ "_GREEN_("%s"), fn); showEmojiState(prefShowNone); + showHintsState(prefShowNone); showColorState(prefShowNone); // showPlotPosState (); // showOverlayPosState (); + // showSavePathState(spDefault, prefShowNone); + // showSavePathState(spDump, prefShowNone); + // showSavePathState(spTrace, prefShowNone); + showClientDebugState(prefShowNone); - showHintsState(prefShowNone); - +// showDeviceDebugState(prefShowNone); PrintAndLogEx(NORMAL, ""); - return PM3_SUCCESS; } /* static int CmdPrefSave (const char *Cmd) { preferences_save(); - return PM3_SUCCESS; } */ static command_t CommandTable[] = { {"help", CmdHelp, AlwaysAvailable, "This help"}, + {"get", CmdPrefGet, AlwaysAvailable, "Get a preference"}, {"set", CmdPrefSet, AlwaysAvailable, "Set a preference"}, - {"show", CmdPrefShow, AlwaysAvailable, "Show preferences"}, -// {"save", CmdPrefSave, AlwaysAvailable, "Save preferences now"}, + {"show", CmdPrefShow, AlwaysAvailable, "Show all preferences"}, {NULL, NULL, NULL, NULL} }; static int CmdHelp(const char *Cmd) { (void)Cmd; // Cmd is not used so far CmdsHelp(CommandTable); - return PM3_SUCCESS; } int CmdPreferences(const char *Cmd) { clearCommandBuffer(); - return CmdsParse(CommandTable, Cmd); } diff --git a/client/src/preferences.h b/client/src/preferences.h index 38e2272cb..0d7705027 100644 --- a/client/src/preferences.h +++ b/client/src/preferences.h @@ -9,6 +9,7 @@ #define PREFERENCES_H_ #include "fileutils.h" +#include // Current working directory will be prepended. #define preferencesFilename "preferences.json" diff --git a/client/src/proxguiqt.cpp b/client/src/proxguiqt.cpp index 945a1922f..855b41c87 100644 --- a/client/src/proxguiqt.cpp +++ b/client/src/proxguiqt.cpp @@ -139,21 +139,21 @@ ProxGuiQT::~ProxGuiQT(void) { SliderWidget::SliderWidget() { // Set the initail postion and size from settings if (session.preferences_loaded) - setGeometry(session.window_overlay_xpos, session.window_overlay_ypos, session.window_overlay_wsize, session.window_overlay_hsize); + setGeometry(session.overlay.x, session.overlay.y, session.overlay.w, session.overlay.h); else resize(800, 400); } void SliderWidget::resizeEvent(QResizeEvent *event) { - session.window_overlay_hsize = event->size().height(); - session.window_overlay_wsize = event->size().width(); + session.overlay.h = event->size().height(); + session.overlay.w = event->size().width(); session.window_changed = true; } void SliderWidget::moveEvent(QMoveEvent *event) { - session.window_overlay_xpos = event->pos().x(); - session.window_overlay_ypos = event->pos().y(); + session.overlay.x = event->pos().x(); + session.overlay.y = event->pos().y(); session.window_changed = true; } @@ -199,7 +199,7 @@ ProxWidget::ProxWidget(QWidget *parent, ProxGuiQT *master) : QWidget(parent) { this->master = master; // Set the initail postion and size from settings if (session.preferences_loaded) - setGeometry(session.window_plot_xpos, session.window_plot_ypos, session.window_plot_wsize, session.window_plot_hsize); + setGeometry(session.plot.x, session.plot.y, session.plot.w, session.plot.h); else resize(800, 400); @@ -224,7 +224,7 @@ ProxWidget::ProxWidget(QWidget *parent, ProxGuiQT *master) : QWidget(parent) { QObject::connect(opsController->horizontalSlider_dirthr_down, SIGNAL(valueChanged(int)), this, SLOT(vchange_dthr_down(int))); QObject::connect(opsController->horizontalSlider_askedge, SIGNAL(valueChanged(int)), this, SLOT(vchange_askedge(int))); - controlWidget->setGeometry(session.window_overlay_xpos, session.window_overlay_ypos, session.window_overlay_wsize, session.window_overlay_hsize); + controlWidget->setGeometry(session.overlay.x, session.overlay.y, session.overlay.w, session.overlay.h); // Set up the plot widget, which does the actual plotting plot = new Plot(this); @@ -292,13 +292,13 @@ void ProxWidget::showEvent(QShowEvent *event) { plot->show(); } void ProxWidget::moveEvent(QMoveEvent *event) { - session.window_plot_xpos = event->pos().x(); - session.window_plot_ypos = event->pos().y(); + session.plot.x = event->pos().x(); + session.plot.y = event->pos().y(); session.window_changed = true; } void ProxWidget::resizeEvent(QResizeEvent *event) { - session.window_plot_hsize = event->size().height(); - session.window_plot_wsize = event->size().width(); + session.plot.h = event->size().height(); + session.plot.w = event->size().width(); session.window_changed = true; } @@ -349,11 +349,11 @@ void Plot::setMaxAndStart(int *buffer, size_t len, QRect plotRect) { GraphStart = startMax; } if (GraphStart > len) return; - int vMin = INT_MAX, vMax = INT_MIN, v = 0; + int vMin = INT_MAX, vMax = INT_MIN; uint32_t sample_index = GraphStart ; for (; sample_index < len && xCoordOf(sample_index, plotRect) < plotRect.right() ; sample_index++) { - v = buffer[sample_index]; + int v = buffer[sample_index]; if (v < vMin) vMin = v; if (v > vMax) vMax = v; } @@ -381,7 +381,7 @@ void Plot::PlotDemod(uint8_t *buffer, size_t len, QRect plotRect, QRect annotati first_delta_x += BitStart * PlotGridX; if (BitStart > (int)len) return; int delta_x = 0; - int v = 0; +// int v = 0; //printf("first_delta_x %i, grid_delta_x %i, DemodStart %i, BitStart %i\n",first_delta_x,grid_delta_x,DemodStart, BitStart); painter->setPen(getColor(graphNum)); @@ -393,9 +393,9 @@ void Plot::PlotDemod(uint8_t *buffer, size_t len, QRect plotRect, QRect annotati delta_x = 0; int clk = first_delta_x; for (int i = BitStart; i < (int)len && xCoordOf(delta_x + DemodStart, plotRect) < plotRect.right(); i++) { - for (int ii = 0; ii < (clk) && i < (int)len && xCoordOf(DemodStart + delta_x + ii, plotRect) < plotRect.right() ; ii++) { - x = xCoordOf(DemodStart + delta_x + ii, plotRect); - v = buffer[i] * 200 - 100; + for (int j = 0; j < (clk) && i < (int)len && xCoordOf(DemodStart + delta_x + j, plotRect) < plotRect.right() ; j++) { + x = xCoordOf(DemodStart + delta_x + j, plotRect); + int v = buffer[i] * 200 - 100; y = yCoordOf(v, plotRect, absVMax); @@ -405,7 +405,7 @@ void Plot::PlotDemod(uint8_t *buffer, size_t len, QRect plotRect, QRect annotati QRect f(QPoint(x - 3, y - 3), QPoint(x + 3, y + 3)); painter->fillRect(f, QColor(100, 255, 100)); } - if (ii == (int)clk / 2) { + if (j == (int)clk / 2) { //print label sprintf(str, "%u", buffer[i]); painter->drawText(x - 8, y + ((buffer[i] > 0) ? 18 : -6), str); @@ -482,7 +482,7 @@ void Plot::PlotGraph(int *buffer, size_t len, QRect plotRect, QRect annotationRe //Graph annotations painter->drawPath(penPath); char str[200]; - sprintf(str, "max=%d min=%d mean=%d n=%d/%zu CursorAVal=[%d] CursorBVal=[%d]", + sprintf(str, "max=%d min=%d mean=%d n=%u/%zu CursorAVal=[%d] CursorBVal=[%d]", vMax, vMin, vMean, i, len, buffer[CursorAPos], buffer[CursorBPos]); painter->drawText(20, annotationRect.bottom() - 23 - 20 * graphNum, str); @@ -595,7 +595,7 @@ void Plot::paintEvent(QPaintEvent *event) { //Draw annotations char str[200]; - sprintf(str, "@%d dt=%d [%2.2f] zoom=%2.2f CursorAPos=%d CursorBPos=%d GridX=%d GridY=%d (%s) GridXoffset=%d", + sprintf(str, "@%u dt=%u [%2.2f] zoom=%2.2f CursorAPos=%u CursorBPos=%u GridX=%d GridY=%d (%s) GridXoffset=%d", GraphStart, CursorBPos - CursorAPos, ((int32_t)(CursorBPos - CursorAPos)) / CursorScaleFactor, diff --git a/client/src/proxguiqt.h b/client/src/proxguiqt.h index 70a9cc6c1..7f9a82126 100644 --- a/client/src/proxguiqt.h +++ b/client/src/proxguiqt.h @@ -35,8 +35,8 @@ class Plot: public QWidget { double GraphPixelsPerPoint; // How many visual pixels are between each sample point (x axis) uint32_t CursorAPos; uint32_t CursorBPos; - void PlotGraph(int *buffer, size_t len, QRect r, QRect r2, QPainter *painter, int graphNum); - void PlotDemod(uint8_t *buffer, size_t len, QRect r, QRect r2, QPainter *painter, int graphNum, uint32_t plotOffset); + void PlotGraph(int *buffer, size_t len, QRect plotRect, QRect annotationRect, QPainter *painter, int graphNum); + void PlotDemod(uint8_t *buffer, size_t len, QRect plotRect, QRect annotationRect, QPainter *painter, int graphNum, uint32_t plotOffset); void plotGridLines(QPainter *painter, QRect r); int xCoordOf(int i, QRect r); int yCoordOf(int v, QRect r, int maxVal); diff --git a/client/src/proxmark3.c b/client/src/proxmark3.c index 7f3a81299..6010fb981 100644 --- a/client/src/proxmark3.c +++ b/client/src/proxmark3.c @@ -38,7 +38,7 @@ #endif // Used to enable/disable use of preferences json file -// #define USE_PREFERENCE_FILE +#define USE_PREFERENCE_FILE #ifdef _WIN32 @@ -412,21 +412,54 @@ static void set_my_executable_path(void) { } static const char *my_user_directory = NULL; -static char _cwd_Buffer [FILENAME_MAX] = {0}; +// static char _cwd_Buffer [FILENAME_MAX] = {0}; const char *get_my_user_directory(void) { return my_user_directory; } + static void set_my_user_directory(void) { + /* my_user_directory = getenv("HOME"); + + // if not found, default to current directory + if (my_user_directory == NULL) { + my_user_directory = GetCurrentDir(_cwd_Buffer, sizeof(_cwd_Buffer)); + // change all slashs to / (windows should not care... + for (int i = 0; i < strlen(_cwd_Buffer); i++) + if (_cwd_Buffer[i] == '\\') _cwd_Buffer[i] = '/'; + // my_user_directory = "."; + } + */ my_user_directory = getenv("HOME"); // if not found, default to current directory if (my_user_directory == NULL) { - my_user_directory = GetCurrentDir(_cwd_Buffer, sizeof(_cwd_Buffer)); - // change all slashs to / (windows should not care... - for (int i = 0; i < strlen(_cwd_Buffer); i++) - if (_cwd_Buffer[i] == '\\') _cwd_Buffer[i] = '/'; - // my_user_directory = "."; + + char *cwd_Buffer = NULL; + uint16_t pathLen = FILENAME_MAX; // should be a good starting point + bool error = false; + + cwd_Buffer = (char *)calloc(pathLen, sizeof(uint8_t)); + + while (!error && (GetCurrentDir(cwd_Buffer, pathLen) == NULL)) { + if (errno == ERANGE) { // Need bigger buffer + pathLen += 10; // if buffer was too small add 10 characters and try again + cwd_Buffer = realloc(cwd_Buffer, pathLen); + } else { + error = true; + free(cwd_Buffer); + cwd_Buffer = NULL; + } + printf("Len... %d\n", pathLen); + } + + if (!error) { + + for (int i = 0; i < strlen(cwd_Buffer); i++) + if (cwd_Buffer[i] == '\\') cwd_Buffer[i] = '/'; + + my_user_directory = cwd_Buffer; + } } } @@ -668,10 +701,6 @@ int main(int argc, char *argv[]) { // Load Settings and assign // This will allow the command line to override the settings.json values preferences_load(); - // Change height/width (Rows,Cols) - Testing - // printf ("\e[8;50;100t"); - // printf ("\e[3;50;50t"); // x,y - //printf ("Path : %s \n",my_user_directory); // quick patch for debug level g_debugMode = session.client_debug_level; // settings_save (); @@ -877,6 +906,12 @@ int main(int argc, char *argv[]) { session.supports_colors = true; session.emoji_mode = EMOJI; } + } else { + // even if prefs, we disable colors if stdin or stdout is not a TTY + if ((! session.stdinOnTTY) || (! session.stdoutOnTTY)) { + session.supports_colors = false; + session.emoji_mode = ALTTEXT; + } } #else if (session.stdinOnTTY && session.stdoutOnTTY) { @@ -945,9 +980,22 @@ int main(int argc, char *argv[]) { // Doing this here will ensure other checks and updates are saved to over rule default // e.g. Linux color use check if (!session.preferences_loaded) { + PrintAndLogEx(INFO, "Creating initial preferences file"); // json save reports file name, so just info msg here preferences_save(); // Save defaults session.preferences_loaded = true; + } /* else { + // Set device debug level + PrintAndLogEx(INFO,"setting device debug loglevel"); + if (session.pm3_present) { + SendCommandNG(CMD_SET_DBGMODE, &session.device_debug_level, 1); + PacketResponseNG resp; + if (WaitForResponseTimeout(CMD_SET_DBGMODE, &resp, 2000) == false) + PrintAndLogEx (INFO,"failed to set device debug loglevel"); + } + else + PrintAndLogEx(WARNING,"Proxmark3 not ready to set debug level"); } + */ #endif #ifdef HAVE_GUI diff --git a/client/src/uart/uart_posix.c b/client/src/uart/uart_posix.c index 0e05f8389..a2b324b49 100644 --- a/client/src/uart/uart_posix.c +++ b/client/src/uart/uart_posix.c @@ -194,12 +194,12 @@ serial_port uart_open(const char *pcPortName, uint32_t speed) { } if (connect(localsocket, (struct sockaddr *) &remote, len) == -1) { + close(localsocket); free(sp); return INVALID_SERIAL_PORT; } sp->fd = localsocket; - return sp; } diff --git a/client/src/ui.h b/client/src/ui.h index 24c4b7201..e47031d70 100644 --- a/client/src/ui.h +++ b/client/src/ui.h @@ -19,7 +19,10 @@ typedef enum logLevel {NORMAL, SUCCESS, INFO, FAILED, WARNING, ERR, DEBUG, INPLACE, HINT} logLevel_t; typedef enum emojiMode {ALIAS, EMOJI, ALTTEXT, ERASE} emojiMode_t; -typedef enum clientdebugLevel {OFF, SIMPLE, FULL} clientdebugLevel_t; +typedef enum clientdebugLevel {cdbOFF, cdbSIMPLE, cdbFULL} clientdebugLevel_t; +// typedef enum devicedebugLevel {ddbOFF, ddbERROR, ddbINFO, ddbDEBUG, ddbEXTENDED} devicedebugLevel_t; +//typedef enum savePaths {spDefault, spDump, spTrace, spItemCount} savePaths_t; // last item spItemCount used to auto map to number of files +typedef struct {int x; int y; int h; int w;} qtWindow_t; typedef struct { bool preferences_loaded; @@ -31,15 +34,11 @@ typedef struct { bool help_dump_mode; bool show_hints; bool window_changed; // track if plot/overlay pos/size changed to save on exit - int window_plot_xpos; - int window_plot_ypos; - int window_plot_hsize; - int window_plot_wsize; - int window_overlay_xpos; - int window_overlay_ypos; - int window_overlay_hsize; - int window_overlay_wsize; + qtWindow_t plot; + qtWindow_t overlay; +// char *defaultPaths[spItemCount]; // Array should allow loop searching for files clientdebugLevel_t client_debug_level; +// uint8_t device_debug_level; } session_arg_t; extern session_arg_t session; diff --git a/doc/md/Installation_Instructions/Troubleshooting.md b/doc/md/Installation_Instructions/Troubleshooting.md index 3fba71e1c..d2c062dcb 100644 --- a/doc/md/Installation_Instructions/Troubleshooting.md +++ b/doc/md/Installation_Instructions/Troubleshooting.md @@ -19,6 +19,7 @@ Always use the latest repository commits from *master* branch. There are always * [Pixmap / pixbuf warnings](#pixmap--pixbuf-warnings) * [Usb cable](#usb-cable) * [WSL 2 explorer.exe . doesnt work](#WSL-2) + * [Troubles with running the Proxmark3 client](#troubles-with-running-the-proxmark3-client) ## `pm3` or `pm3-flash*` doesn't see my Proxmark @@ -173,3 +174,7 @@ Trying to access the dump files created in WSL, you will need to run ```explore [](www.icedev.se/proxmark3/rdv40/wsl2_p9np.png) +## Troubles with running the Proxmark3 client +Some reports has stated that they needed to execute the Proxmark3 as root on their *nix system. +Try running it with + `sudo ./pm3` diff --git a/pm3test.sh b/pm3test.sh index 202849f60..be9331358 100755 --- a/pm3test.sh +++ b/pm3test.sh @@ -108,9 +108,17 @@ while true; do printf "\n${C_BLUE}Testing data manipulation:${C_NC}\n" if ! CheckExecute "reveng test" "$PM3BIN -c 'reveng -w 8 -s 01020304e3 010204039d'" "CRC-8/SMBUS"; then break; fi if ! CheckExecute "mfu pwdgen test" "$PM3BIN -c 'hf mfu pwdgen t'" "Selftest OK"; then break; fi - + printf "\n${C_BLUE}Testing LF:${C_NC}\n" - if ! CheckExecute "lf em4x05 test" "$PM3BIN -c 'data load traces/em4x05.pm3;lf search'" "FDX-B ID found"; then break; fi + if ! CheckExecute "lf em4x05 test" "$PM3BIN -c 'data load traces/em4x05.pm3;lf search 1'" "FDX-B ID found"; then break; fi + if ! CheckExecute "lf em410x test" "$PM3BIN -c 'data load traces/EM4102-1.pm3;lf search 1'" "EM410x ID found"; then break; fi + if ! CheckExecute "lf visa2000 test" "$PM3BIN -c 'data load traces/visa2000.pm3;lf search 1'" "Visa2000 ID found"; then break; fi + if ! CheckExecute "lf awid test" "$PM3BIN -c 'data load traces/AWID-15-259.pm3;lf search 1'" "AWID ID found"; then break; fi + if ! CheckExecute "lf securakey test" "$PM3BIN -c 'data load traces/securakey-64169.pm3;lf search 1 '" "Securakey ID found"; then break; fi + if ! CheckExecute "lf keri test" "$PM3BIN -c 'data load traces/keri.pm3;lf search 1'" "Pyramid ID found"; then break; fi + if ! CheckExecute "lf HID Prox test" "$PM3BIN -c 'data load traces/hid-proxCardII-05512-11432784-1.pm3;lf search 1'" "HID Prox ID found"; then break; fi + if ! CheckExecute "lf Paradox test" "$PM3BIN -c 'data load traces/Paradox-96_40426-APJN08.pm3;lf search 1'" "Paradox ID found"; then break; fi + if ! CheckExecute "lf IO Prox test" "$PM3BIN -c 'data load traces/ioprox-XSF-01-3B-44725.pm3;lf search 1'" "IO Prox ID found"; then break; fi printf "\n${C_BLUE}Testing HF:${C_NC}\n" if ! CheckExecute "hf mf offline text" "$PM3BIN -c 'hf mf'" "at_enc"; then break; fi diff --git a/tools/hitag2crack/crack2/Makefile b/tools/hitag2crack/crack2/Makefile index 55c6a5bd9..4794d6ec4 100644 --- a/tools/hitag2crack/crack2/Makefile +++ b/tools/hitag2crack/crack2/Makefile @@ -1,4 +1,4 @@ -CFLAGS?=-Wall +CFLAGS?=-Wall -Werror -O3 # Linux libs LIBS=-pthread -D_GNU_SOURCE # Mac libs diff --git a/tools/hitag2crack/crack3/Makefile b/tools/hitag2crack/crack3/Makefile index 80f1ed863..318b5db78 100644 --- a/tools/hitag2crack/crack3/Makefile +++ b/tools/hitag2crack/crack3/Makefile @@ -1,4 +1,4 @@ -CFLAGS?=-Wall +CFLAGS?=-Wall -Werror -O3 LIBS= VPATH=../common INC=-I ../common diff --git a/tools/hitag2crack/crack4/Makefile b/tools/hitag2crack/crack4/Makefile index 418fb9930..b560577bd 100644 --- a/tools/hitag2crack/crack4/Makefile +++ b/tools/hitag2crack/crack4/Makefile @@ -1,4 +1,4 @@ -CFLAGS?=-Wall +CFLAGS?=-Wall -Werror -O3 LIBS=-lpthread VPATH=../common INC=-I ../common diff --git a/tools/hitag2crack/crack5/Makefile b/tools/hitag2crack/crack5/Makefile index 6ddc0150f..9d19b63ee 100644 --- a/tools/hitag2crack/crack5/Makefile +++ b/tools/hitag2crack/crack5/Makefile @@ -1,4 +1,4 @@ -CFLAGS?=-Wall +CFLAGS?=-Wall -Werror -O3 LIBS=-lpthread VPATH=../common INC=-I ../common diff --git a/tools/hitag2crack/crack5/ht2crack5.c b/tools/hitag2crack/crack5/ht2crack5.c index 42afbde88..79557bbfd 100644 --- a/tools/hitag2crack/crack5/ht2crack5.c +++ b/tools/hitag2crack/crack5/ht2crack5.c @@ -99,21 +99,29 @@ uint64_t candidates[(1 << 20)]; bitslice_t initial_bitslices[48]; size_t filter_pos[20] = {4, 7, 9, 13, 16, 18, 22, 24, 27, 30, 32, 35, 45, 47 }; size_t thread_count = 8; -size_t layer_0_found; +uint64_t layer_0_found; void *find_state(void *thread_d); static void try_state(uint64_t s); int main(int argc, char *argv[]) { + + if (argc < 6) { + printf("%s UID {nR1} {aR1} {nR2} {aR2}\n", argv[0]); + exit(1); + } + // set constants memset(bs_ones.bytes, 0xff, VECTOR_SIZE); memset(bs_zeroes.bytes, 0x00, VECTOR_SIZE); uint32_t target = 0; - if (argc < 6) { - printf("%s UID {nR1} {aR1} {nR2} {aR2}\n", argv[0]); - exit(1); - } +#ifndef __WIN32 + thread_count = sysconf(_SC_NPROCESSORS_CONF); + if ( thread_count < 2) + thread_count = 2; +#endif /* _WIN32 */ + if (!strncmp(argv[1], "0x", 2) || !strncmp(argv[1], "0X", 2)) { uid = rev32(hexreversetoulong(argv[1] + 2)); @@ -181,16 +189,20 @@ int main(int argc, char *argv[]) { } void *find_state(void *thread_d) { - size_t thread = (size_t)thread_d; + uint64_t thread = (uint64_t)thread_d; + + for (uint64_t index = thread; index < layer_0_found; index += thread_count) { - for (size_t index = thread; index < layer_0_found; index += thread_count) { if (((index / thread_count) & 0xFF) == 0) - printf("Thread %lu slice %lu/%lu\n", thread, index / thread_count / 256 + 1, layer_0_found / thread_count / 256); + printf("Thread %" PRIu64 " slice %" PRIu64 "/%" PRIu64 "\n", thread, index / thread_count / 256 + 1, layer_0_found / thread_count / 256); + uint64_t state0 = candidates[index]; bitslice(state0 >> 2, &state[0], 46, false); + for (size_t bit = 0; bit < 8; bit++) { state[-2 + filter_pos[bit]] = initial_bitslices[bit]; } + for (uint16_t i1 = 0; i1 < (1 << (bits[1] + 1) >> 8); i1++) { state[-2 + 27].value = ((bool)(i1 & 0x1)) ? bs_ones.value : bs_zeroes.value; state[-2 + 30].value = ((bool)(i1 & 0x2)) ? bs_ones.value : bs_zeroes.value; @@ -208,6 +220,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter1 = f_c_bs(filter1_0, filter1_1, filter1_2, filter1_3, filter1_4); bitslice_t results1; results1.value = filter1 ^ keystream[1].value; + if (results1.bytes64[0] == 0 && results1.bytes64[1] == 0 && results1.bytes64[2] == 0 @@ -226,6 +239,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter10_0 = f_a_bs(state[-2 + 12].value, state[-2 + 13].value, state[-2 + 15].value, state[-2 + 16].value); const bitslice_value_t filter11_0 = f_a_bs(state[-2 + 13].value, state[-2 + 14].value, state[-2 + 16].value, state[-2 + 17].value); const bitslice_value_t filter12_0 = f_a_bs(state[-2 + 14].value, state[-2 + 15].value, state[-2 + 17].value, state[-2 + 18].value); + for (uint16_t i2 = 0; i2 < (1 << (bits[2] + 1)); i2++) { state[-2 + 10].value = ((bool)(i2 & 0x1)) ? bs_ones.value : bs_zeroes.value; state[-2 + 19].value = ((bool)(i2 & 0x2)) ? bs_ones.value : bs_zeroes.value; @@ -239,6 +253,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter2 = f_c_bs(filter2_0, filter2_1, filter2_2, filter2_3, filter2_4); bitslice_t results2; results2.value = results1.value & (filter2 ^ keystream[2].value); + if (results2.bytes64[0] == 0 && results2.bytes64[1] == 0 && results2.bytes64[2] == 0 @@ -257,6 +272,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter10_1 = f_b_bs(state[-2 + 18].value, state[-2 + 22].value, state[-2 + 24].value, state[-2 + 25].value); const bitslice_value_t filter10_2 = f_b_bs(state[-2 + 27].value, state[-2 + 31].value, state[-2 + 33].value, state[-2 + 36].value); const bitslice_value_t filter11_1 = f_b_bs(state[-2 + 19].value, state[-2 + 23].value, state[-2 + 25].value, state[-2 + 26].value); + for (uint8_t i3 = 0; i3 < (1 << bits[3]); i3++) { state[-2 + 11].value = ((bool)(i3 & 0x1)) ? bs_ones.value : bs_zeroes.value; state[-2 + 20].value = ((bool)(i3 & 0x2)) ? bs_ones.value : bs_zeroes.value; @@ -268,6 +284,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter3 = f_c_bs(filter3_0, filter3_1, filter3_2, filter3_3, filter3_4); bitslice_t results3; results3.value = results2.value & (filter3 ^ keystream[3].value); + if (results3.bytes64[0] == 0 && results3.bytes64[1] == 0 && results3.bytes64[2] == 0 @@ -275,6 +292,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 51].value = lfsr_bs(3); state[-2 + 52].value = lfsr_bs(4); state[-2 + 53].value = lfsr_bs(5); @@ -291,6 +309,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter9_4 = f_a_bs(state[-2 + 43].value, state[-2 + 52].value, state[-2 + 53].value, state[-2 + 55].value); const bitslice_value_t filter11_2 = f_b_bs(state[-2 + 28].value, state[-2 + 32].value, state[-2 + 34].value, state[-2 + 37].value); const bitslice_value_t filter12_1 = f_b_bs(state[-2 + 20].value, state[-2 + 24].value, state[-2 + 26].value, state[-2 + 27].value); + for (uint8_t i4 = 0; i4 < (1 << bits[4]); i4++) { state[-2 + 38].value = ((bool)(i4 & 0x1)) ? bs_ones.value : bs_zeroes.value; // 0xff87ffffffff @@ -298,6 +317,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter4 = f_c_bs(filter4_0, filter4_1, filter4_2, filter4_3, filter4_4); bitslice_t results4; results4.value = results3.value & (filter4 ^ keystream[4].value); + if (results4.bytes64[0] == 0 && results4.bytes64[1] == 0 && results4.bytes64[2] == 0 @@ -305,8 +325,10 @@ void *find_state(void *thread_d) { ) { continue; } + const bitslice_value_t filter5_3 = f_b_bs(state[-2 + 33].value, state[-2 + 34].value, state[-2 + 36].value, state[-2 + 38].value); const bitslice_value_t filter12_2 = f_b_bs(state[-2 + 29].value, state[-2 + 33].value, state[-2 + 35].value, state[-2 + 38].value); + for (uint8_t i5 = 0; i5 < (1 << bits[5]); i5++) { state[-2 + 39].value = ((bool)(i5 & 0x1)) ? bs_ones.value : bs_zeroes.value; // 0xffc7ffffffff @@ -314,6 +336,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter5 = f_c_bs(filter5_0, filter5_1, filter5_2, filter5_3, filter5_4); bitslice_t results5; results5.value = results4.value & (filter5 ^ keystream[5].value); + if (results5.bytes64[0] == 0 && results5.bytes64[1] == 0 && results5.bytes64[2] == 0 @@ -321,7 +344,9 @@ void *find_state(void *thread_d) { ) { continue; } + const bitslice_value_t filter6_3 = f_b_bs(state[-2 + 34].value, state[-2 + 35].value, state[-2 + 37].value, state[-2 + 39].value); + for (uint8_t i6 = 0; i6 < (1 << bits[6]); i6++) { state[-2 + 40].value = ((bool)(i6 & 0x1)) ? bs_ones.value : bs_zeroes.value; // 0xffe7ffffffff @@ -329,6 +354,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter6 = f_c_bs(filter6_0, filter6_1, filter6_2, filter6_3, filter6_4); bitslice_t results6; results6.value = results5.value & (filter6 ^ keystream[6].value); + if (results6.bytes64[0] == 0 && results6.bytes64[1] == 0 && results6.bytes64[2] == 0 @@ -336,7 +362,9 @@ void *find_state(void *thread_d) { ) { continue; } + const bitslice_value_t filter7_3 = f_b_bs(state[-2 + 35].value, state[-2 + 36].value, state[-2 + 38].value, state[-2 + 40].value); + for (uint8_t i7 = 0; i7 < (1 << bits[7]); i7++) { state[-2 + 41].value = ((bool)(i7 & 0x1)) ? bs_ones.value : bs_zeroes.value; // 0xfff7ffffffff @@ -351,9 +379,11 @@ void *find_state(void *thread_d) { ) { continue; } + const bitslice_value_t filter8_3 = f_b_bs(state[-2 + 36].value, state[-2 + 37].value, state[-2 + 39].value, state[-2 + 41].value); const bitslice_value_t filter10_3 = f_b_bs(state[-2 + 38].value, state[-2 + 39].value, state[-2 + 41].value, state[-2 + 43].value); const bitslice_value_t filter12_3 = f_b_bs(state[-2 + 40].value, state[-2 + 41].value, state[-2 + 43].value, state[-2 + 45].value); + for (uint8_t i8 = 0; i8 < (1 << bits[8]); i8++) { state[-2 + 42].value = ((bool)(i8 & 0x1)) ? bs_ones.value : bs_zeroes.value; // 0xffffffffffff @@ -363,6 +393,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter8 = f_c_bs(filter8_0, filter8_1, filter8_2, filter8_3, filter8_4); bitslice_t results8; results8.value = results7.value & (filter8 ^ keystream[8].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -370,8 +401,10 @@ void *find_state(void *thread_d) { ) { continue; } + const bitslice_value_t filter9 = f_c_bs(filter9_0, filter9_1, filter9_2, filter9_3, filter9_4); results8.value &= (filter9 ^ keystream[9].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -383,6 +416,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter10_4 = f_a_bs(state[-2 + 44].value, state[-2 + 53].value, state[-2 + 54].value, state[-2 + 56].value); const bitslice_value_t filter10 = f_c_bs(filter10_0, filter10_1, filter10_2, filter10_3, filter10_4); results8.value &= (filter10 ^ keystream[10].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -390,10 +424,12 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 57].value = lfsr_bs(9); const bitslice_value_t filter11_4 = f_a_bs(state[-2 + 45].value, state[-2 + 54].value, state[-2 + 55].value, state[-2 + 57].value); const bitslice_value_t filter11 = f_c_bs(filter11_0, filter11_1, filter11_2, filter11_3, filter11_4); results8.value &= (filter11 ^ keystream[11].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -401,10 +437,12 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 58].value = lfsr_bs(10); const bitslice_value_t filter12_4 = f_a_bs(state[-2 + 46].value, state[-2 + 55].value, state[-2 + 56].value, state[-2 + 58].value); const bitslice_value_t filter12 = f_c_bs(filter12_0, filter12_1, filter12_2, filter12_3, filter12_4); results8.value &= (filter12 ^ keystream[12].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -412,6 +450,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 59].value = lfsr_bs(11); const bitslice_value_t filter13_0 = f_a_bs(state[-2 + 15].value, state[-2 + 16].value, state[-2 + 18].value, state[-2 + 19].value); const bitslice_value_t filter13_1 = f_b_bs(state[-2 + 21].value, state[-2 + 25].value, state[-2 + 27].value, state[-2 + 28].value); @@ -420,6 +459,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter13_4 = f_a_bs(state[-2 + 47].value, state[-2 + 56].value, state[-2 + 57].value, state[-2 + 59].value); const bitslice_value_t filter13 = f_c_bs(filter13_0, filter13_1, filter13_2, filter13_3, filter13_4); results8.value &= (filter13 ^ keystream[13].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -427,6 +467,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 60].value = lfsr_bs(12); const bitslice_value_t filter14_0 = f_a_bs(state[-2 + 16].value, state[-2 + 17].value, state[-2 + 19].value, state[-2 + 20].value); const bitslice_value_t filter14_1 = f_b_bs(state[-2 + 22].value, state[-2 + 26].value, state[-2 + 28].value, state[-2 + 29].value); @@ -435,6 +476,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter14_4 = f_a_bs(state[-2 + 48].value, state[-2 + 57].value, state[-2 + 58].value, state[-2 + 60].value); const bitslice_value_t filter14 = f_c_bs(filter14_0, filter14_1, filter14_2, filter14_3, filter14_4); results8.value &= (filter14 ^ keystream[14].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -442,6 +484,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 61].value = lfsr_bs(13); const bitslice_value_t filter15_0 = f_a_bs(state[-2 + 17].value, state[-2 + 18].value, state[-2 + 20].value, state[-2 + 21].value); const bitslice_value_t filter15_1 = f_b_bs(state[-2 + 23].value, state[-2 + 27].value, state[-2 + 29].value, state[-2 + 30].value); @@ -450,6 +493,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter15_4 = f_a_bs(state[-2 + 49].value, state[-2 + 58].value, state[-2 + 59].value, state[-2 + 61].value); const bitslice_value_t filter15 = f_c_bs(filter15_0, filter15_1, filter15_2, filter15_3, filter15_4); results8.value &= (filter15 ^ keystream[15].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -457,6 +501,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 62].value = lfsr_bs(14); const bitslice_value_t filter16_0 = f_a_bs(state[-2 + 18].value, state[-2 + 19].value, state[-2 + 21].value, state[-2 + 22].value); const bitslice_value_t filter16_1 = f_b_bs(state[-2 + 24].value, state[-2 + 28].value, state[-2 + 30].value, state[-2 + 31].value); @@ -465,6 +510,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter16_4 = f_a_bs(state[-2 + 50].value, state[-2 + 59].value, state[-2 + 60].value, state[-2 + 62].value); const bitslice_value_t filter16 = f_c_bs(filter16_0, filter16_1, filter16_2, filter16_3, filter16_4); results8.value &= (filter16 ^ keystream[16].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -472,6 +518,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 63].value = lfsr_bs(15); const bitslice_value_t filter17_0 = f_a_bs(state[-2 + 19].value, state[-2 + 20].value, state[-2 + 22].value, state[-2 + 23].value); const bitslice_value_t filter17_1 = f_b_bs(state[-2 + 25].value, state[-2 + 29].value, state[-2 + 31].value, state[-2 + 32].value); @@ -480,6 +527,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter17_4 = f_a_bs(state[-2 + 51].value, state[-2 + 60].value, state[-2 + 61].value, state[-2 + 63].value); const bitslice_value_t filter17 = f_c_bs(filter17_0, filter17_1, filter17_2, filter17_3, filter17_4); results8.value &= (filter17 ^ keystream[17].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -487,6 +535,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 64].value = lfsr_bs(16); const bitslice_value_t filter18_0 = f_a_bs(state[-2 + 20].value, state[-2 + 21].value, state[-2 + 23].value, state[-2 + 24].value); const bitslice_value_t filter18_1 = f_b_bs(state[-2 + 26].value, state[-2 + 30].value, state[-2 + 32].value, state[-2 + 33].value); @@ -495,6 +544,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter18_4 = f_a_bs(state[-2 + 52].value, state[-2 + 61].value, state[-2 + 62].value, state[-2 + 64].value); const bitslice_value_t filter18 = f_c_bs(filter18_0, filter18_1, filter18_2, filter18_3, filter18_4); results8.value &= (filter18 ^ keystream[18].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -502,6 +552,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 65].value = lfsr_bs(17); const bitslice_value_t filter19_0 = f_a_bs(state[-2 + 21].value, state[-2 + 22].value, state[-2 + 24].value, state[-2 + 25].value); const bitslice_value_t filter19_1 = f_b_bs(state[-2 + 27].value, state[-2 + 31].value, state[-2 + 33].value, state[-2 + 34].value); @@ -510,6 +561,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter19_4 = f_a_bs(state[-2 + 53].value, state[-2 + 62].value, state[-2 + 63].value, state[-2 + 65].value); const bitslice_value_t filter19 = f_c_bs(filter19_0, filter19_1, filter19_2, filter19_3, filter19_4); results8.value &= (filter19 ^ keystream[19].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -517,6 +569,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 66].value = lfsr_bs(18); const bitslice_value_t filter20_0 = f_a_bs(state[-2 + 22].value, state[-2 + 23].value, state[-2 + 25].value, state[-2 + 26].value); const bitslice_value_t filter20_1 = f_b_bs(state[-2 + 28].value, state[-2 + 32].value, state[-2 + 34].value, state[-2 + 35].value); @@ -525,6 +578,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter20_4 = f_a_bs(state[-2 + 54].value, state[-2 + 63].value, state[-2 + 64].value, state[-2 + 66].value); const bitslice_value_t filter20 = f_c_bs(filter20_0, filter20_1, filter20_2, filter20_3, filter20_4); results8.value &= (filter20 ^ keystream[20].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -532,6 +586,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 67].value = lfsr_bs(19); const bitslice_value_t filter21_0 = f_a_bs(state[-2 + 23].value, state[-2 + 24].value, state[-2 + 26].value, state[-2 + 27].value); const bitslice_value_t filter21_1 = f_b_bs(state[-2 + 29].value, state[-2 + 33].value, state[-2 + 35].value, state[-2 + 36].value); @@ -540,6 +595,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter21_4 = f_a_bs(state[-2 + 55].value, state[-2 + 64].value, state[-2 + 65].value, state[-2 + 67].value); const bitslice_value_t filter21 = f_c_bs(filter21_0, filter21_1, filter21_2, filter21_3, filter21_4); results8.value &= (filter21 ^ keystream[21].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -547,6 +603,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 68].value = lfsr_bs(20); const bitslice_value_t filter22_0 = f_a_bs(state[-2 + 24].value, state[-2 + 25].value, state[-2 + 27].value, state[-2 + 28].value); const bitslice_value_t filter22_1 = f_b_bs(state[-2 + 30].value, state[-2 + 34].value, state[-2 + 36].value, state[-2 + 37].value); @@ -555,6 +612,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter22_4 = f_a_bs(state[-2 + 56].value, state[-2 + 65].value, state[-2 + 66].value, state[-2 + 68].value); const bitslice_value_t filter22 = f_c_bs(filter22_0, filter22_1, filter22_2, filter22_3, filter22_4); results8.value &= (filter22 ^ keystream[22].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -562,6 +620,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 69].value = lfsr_bs(21); const bitslice_value_t filter23_0 = f_a_bs(state[-2 + 25].value, state[-2 + 26].value, state[-2 + 28].value, state[-2 + 29].value); const bitslice_value_t filter23_1 = f_b_bs(state[-2 + 31].value, state[-2 + 35].value, state[-2 + 37].value, state[-2 + 38].value); @@ -600,6 +659,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter25_4 = f_a_bs(state[-2 + 59].value, state[-2 + 68].value, state[-2 + 69].value, state[-2 + 71].value); const bitslice_value_t filter25 = f_c_bs(filter25_0, filter25_1, filter25_2, filter25_3, filter25_4); results8.value &= (filter25 ^ keystream[25].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -607,6 +667,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 72].value = lfsr_bs(24); const bitslice_value_t filter26_0 = f_a_bs(state[-2 + 28].value, state[-2 + 29].value, state[-2 + 31].value, state[-2 + 32].value); const bitslice_value_t filter26_1 = f_b_bs(state[-2 + 34].value, state[-2 + 38].value, state[-2 + 40].value, state[-2 + 41].value); @@ -615,6 +676,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter26_4 = f_a_bs(state[-2 + 60].value, state[-2 + 69].value, state[-2 + 70].value, state[-2 + 72].value); const bitslice_value_t filter26 = f_c_bs(filter26_0, filter26_1, filter26_2, filter26_3, filter26_4); results8.value &= (filter26 ^ keystream[26].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -622,6 +684,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 73].value = lfsr_bs(25); const bitslice_value_t filter27_0 = f_a_bs(state[-2 + 29].value, state[-2 + 30].value, state[-2 + 32].value, state[-2 + 33].value); const bitslice_value_t filter27_1 = f_b_bs(state[-2 + 35].value, state[-2 + 39].value, state[-2 + 41].value, state[-2 + 42].value); @@ -630,6 +693,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter27_4 = f_a_bs(state[-2 + 61].value, state[-2 + 70].value, state[-2 + 71].value, state[-2 + 73].value); const bitslice_value_t filter27 = f_c_bs(filter27_0, filter27_1, filter27_2, filter27_3, filter27_4); results8.value &= (filter27 ^ keystream[27].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -637,6 +701,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 74].value = lfsr_bs(26); const bitslice_value_t filter28_0 = f_a_bs(state[-2 + 30].value, state[-2 + 31].value, state[-2 + 33].value, state[-2 + 34].value); const bitslice_value_t filter28_1 = f_b_bs(state[-2 + 36].value, state[-2 + 40].value, state[-2 + 42].value, state[-2 + 43].value); @@ -645,6 +710,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter28_4 = f_a_bs(state[-2 + 62].value, state[-2 + 71].value, state[-2 + 72].value, state[-2 + 74].value); const bitslice_value_t filter28 = f_c_bs(filter28_0, filter28_1, filter28_2, filter28_3, filter28_4); results8.value &= (filter28 ^ keystream[28].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -652,6 +718,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 75].value = lfsr_bs(27); const bitslice_value_t filter29_0 = f_a_bs(state[-2 + 31].value, state[-2 + 32].value, state[-2 + 34].value, state[-2 + 35].value); const bitslice_value_t filter29_1 = f_b_bs(state[-2 + 37].value, state[-2 + 41].value, state[-2 + 43].value, state[-2 + 44].value); @@ -660,6 +727,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter29_4 = f_a_bs(state[-2 + 63].value, state[-2 + 72].value, state[-2 + 73].value, state[-2 + 75].value); const bitslice_value_t filter29 = f_c_bs(filter29_0, filter29_1, filter29_2, filter29_3, filter29_4); results8.value &= (filter29 ^ keystream[29].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -667,6 +735,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 76].value = lfsr_bs(28); const bitslice_value_t filter30_0 = f_a_bs(state[-2 + 32].value, state[-2 + 33].value, state[-2 + 35].value, state[-2 + 36].value); const bitslice_value_t filter30_1 = f_b_bs(state[-2 + 38].value, state[-2 + 42].value, state[-2 + 44].value, state[-2 + 45].value); @@ -675,6 +744,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter30_4 = f_a_bs(state[-2 + 64].value, state[-2 + 73].value, state[-2 + 74].value, state[-2 + 76].value); const bitslice_value_t filter30 = f_c_bs(filter30_0, filter30_1, filter30_2, filter30_3, filter30_4); results8.value &= (filter30 ^ keystream[30].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 @@ -682,6 +752,7 @@ void *find_state(void *thread_d) { ) { continue; } + state[-2 + 77].value = lfsr_bs(29); const bitslice_value_t filter31_0 = f_a_bs(state[-2 + 33].value, state[-2 + 34].value, state[-2 + 36].value, state[-2 + 37].value); const bitslice_value_t filter31_1 = f_b_bs(state[-2 + 39].value, state[-2 + 43].value, state[-2 + 45].value, state[-2 + 46].value); @@ -690,6 +761,7 @@ void *find_state(void *thread_d) { const bitslice_value_t filter31_4 = f_a_bs(state[-2 + 65].value, state[-2 + 74].value, state[-2 + 75].value, state[-2 + 77].value); const bitslice_value_t filter31 = f_c_bs(filter31_0, filter31_1, filter31_2, filter31_3, filter31_4); results8.value &= (filter31 ^ keystream[31].value); + if (results8.bytes64[0] == 0 && results8.bytes64[1] == 0 && results8.bytes64[2] == 0 diff --git a/tools/hitag2crack/crack5gpu/ht2crack5.c b/tools/hitag2crack/crack5gpu/ht2crack5.c index 37df9df65..adaf1ae82 100644 --- a/tools/hitag2crack/crack5gpu/ht2crack5.c +++ b/tools/hitag2crack/crack5gpu/ht2crack5.c @@ -129,6 +129,11 @@ void runKernel(struct context *ctx, uint32_t cand_base, uint64_t *matches, uint3 // Write our data set into the input array in device memory err = clEnqueueWriteBuffer(ctx->commands, ctx->matches_found, CL_TRUE, 0, sizeof(uint32_t), matches_found, 0, NULL, NULL); + if (err != CL_SUCCESS) { + printf("Error: Failed to enque kernel writebuffer in runKernel! %d\n", err); + exit(1); + } + // Set the arguments to our compute kernel err = clSetKernelArg(ctx->kernel, 0, sizeof(uint32_t), &cand_base); err |= clSetKernelArg(ctx->kernel, 4, sizeof(cl_mem), &ctx->matches_found); @@ -231,7 +236,7 @@ int main(int argc, char *argv[]) { for (size_t i0 = 0; i0 < 1 << 20; i0++) { uint64_t state0 = expand(0x5806b4a2d16c, i0); - if (f(state0) == target >> 31) { + if (f(state0) == target >> 31 ) { // cf kernel, state is now split in 3 shorts >> 2 candidates[(layer_0_found * 3) + 0] = (uint16_t)((state0 >> (32 + 2)) & 0xffff); candidates[(layer_0_found * 3) + 1] = (uint16_t)((state0 >> (16 + 2)) & 0xffff);