diff --git a/client/src/cmdmad.c b/client/src/cmdmad.c index d1898f830..e25c603fd 100644 --- a/client/src/cmdmad.c +++ b/client/src/cmdmad.c @@ -35,43 +35,38 @@ // --- Card transport adapters --- -static int mfc_read_sector(uint8_t sector_no, uint8_t key_type, - const uint8_t *key, uint8_t *buf, bool verbose) { +static int mfc_read_sector(uint8_t sector_no, uint8_t key_type, const uint8_t *key, uint8_t *buf, bool verbose) { + // function pointer is used to call this fct, hence the need to match fct signature (void)verbose; return mf_read_sector(sector_no, key_type, key, buf); } -static int mfc_write_sector_data(uint8_t sector_no, uint8_t key_type, - const uint8_t *key, const uint8_t *data, bool verbose) { +static int mfc_write_sector_data(uint8_t sector_no, uint8_t key_type, const uint8_t *key, const uint8_t *data, bool verbose) { (void)verbose; uint8_t first = mfFirstBlockOfSector(sector_no); uint8_t ndata = mfNumBlocksPerSector(sector_no) - 1; + for (int i = 0; i < ndata; i++) { int res = mf_write_block(first + i, key_type, key, data + i * MFBLOCK_SIZE); - if (res != PM3_SUCCESS) + if (res != PM3_SUCCESS) { return res; + } } return PM3_SUCCESS; } -static int mfp_read_sector(uint8_t sector_no, uint8_t key_type, - const uint8_t *key, uint8_t *buf, bool verbose) { - return mfpReadSector(sector_no, key_type, (uint8_t *)key, buf, verbose); +static int mfp_read_sector(uint8_t sector_no, uint8_t key_type, const uint8_t *key, uint8_t *buf, bool verbose) { + return mfpReadSector(sector_no, key_type, key, buf, verbose); } -static int mfp_write_sector_data(uint8_t sector_no, uint8_t key_type, - const uint8_t *key, const uint8_t *data, bool verbose) { - return mfpWriteSector(sector_no, key_type, (uint8_t *)key, data, verbose); +static int mfp_write_sector_data(uint8_t sector_no, uint8_t key_type, const uint8_t *key, const uint8_t *data, bool verbose) { + return mfpWriteSector(sector_no, key_type, key, data, verbose); } // --- Card type detection --- -typedef enum { - MAD_CARD_CLASSIC, - MAD_CARD_PLUS, -} mad_card_type_t; - static int mad_detect_card(mad_card_type_t *out) { + uint8_t sector0[MFBLOCK_SIZE * 4] = {0}; if (mf_read_sector(MF_MAD1_SECTOR, MF_KEY_A, g_mifare_mad_key, sector0) == PM3_SUCCESS) { @@ -84,13 +79,12 @@ static int mad_detect_card(mad_card_type_t *out) { return PM3_SUCCESS; } - PrintAndLogEx(ERR, "Could not authenticate to MAD sector with default keys (Classic or Plus)"); + PrintAndLogEx(ERR, "Could not authenticate to MAD sector with default keys"); return PM3_ESOFT; } -static void mad_fill_ops(mad_ops_t *ops, mad_card_type_t card_type, - const uint8_t *mad_key, const uint8_t *app_key, - uint8_t key_type, bool verbose) { +static void mad_fill_ops(mad_ops_t *ops, mad_card_type_t card_type, const uint8_t *mad_key, const uint8_t *app_key, uint8_t key_type, bool verbose) { + if (card_type == MAD_CARD_CLASSIC) { ops->read_sector = mfc_read_sector; ops->write_sector_data = mfc_write_sector_data; @@ -109,18 +103,22 @@ static void mad_fill_ops(mad_ops_t *ops, mad_card_type_t card_type, // Determine card type from key length, or auto-detect static int mad_resolve_card(int keylen, int madkeylen, mad_card_type_t *out, bool force_classic, bool force_plus) { + if (force_classic) { *out = MAD_CARD_CLASSIC; return PM3_SUCCESS; } + if (force_plus) { *out = MAD_CARD_PLUS; return PM3_SUCCESS; } + if (keylen == AES_KEY_LEN || madkeylen == AES_KEY_LEN) { *out = MAD_CARD_PLUS; return PM3_SUCCESS; } + if (keylen == MIFARE_KEY_SIZE || madkeylen == MIFARE_KEY_SIZE) { *out = MAD_CARD_CLASSIC; return PM3_SUCCESS; @@ -134,68 +132,82 @@ static int mad_cmd_read(const char *Cmd, const char *cmd_name, bool force_classi CLIParserContext *ctx; CLIParserInit(&ctx, cmd_name, "Read application data from sectors matching a MAD AID", - "mad read --aid e103 -> read NDEF data (auto-detect card)\n" - "mad read --aid e103 -k ffffffffffff -b -> Classic, key B\n" - "mad read --aid e103 -k d3f7d3f7d3f7d3f7d3f7d3f7d3f7d3f7 -> Plus\n" - "mad read --aid e103 -v -f mydata.bin -> verbose, save to file"); + "mad read --aid e103 -> read NDEF data\n" + "mad read --aid e103 -k ffffffffffff -b -> Classic, key B\n" + "mad read --aid e103 -k d3f7d3f7d3f7d3f7d3f7d3f7d3f7d3f7 -> Plus\n" + "mad read --aid e103 -v -f mydata.bin"); void *argtable[] = { arg_param_begin, - arg_lit0("v", "verbose", "verbose output"), arg_str1(NULL, "aid", "", "application ID (2 hex bytes)"), arg_str0("k", "key", "", "key for data sectors"), - arg_lit0("b", "keyb", "use key B (def: key A)"), - arg_str0(NULL, "mad-key", "", "key for MAD sectors"), + arg_lit0("b", "keyb", "use key B"), + arg_str0(NULL, "madkey", "", "key for MAD sectors"), arg_lit0(NULL, "be", "big-endian AID byte swap"), arg_lit0(NULL, "override", "override failed CRC check"), arg_str0("f", "file", "", "save raw data to file"), + arg_lit0("v", "verbose", "verbose output"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, false); - bool verbose = arg_get_lit(ctx, 1); + uint8_t aid[2] = {0}; int aidlen = 0; - CLIGetHexWithReturn(ctx, 2, aid, &aidlen); + CLIGetHexWithReturn(ctx, 1, aid, &aidlen); + uint8_t userkey[AES_KEY_LEN] = {0}; int keylen = 0; - CLIGetHexWithReturn(ctx, 3, userkey, &keylen); - bool keyB = arg_get_lit(ctx, 4); + CLIGetHexWithReturn(ctx, 2, userkey, &keylen); + + bool keyB = arg_get_lit(ctx, 3); + uint8_t madkey[AES_KEY_LEN] = {0}; int madkeylen = 0; - CLIGetHexWithReturn(ctx, 5, madkey, &madkeylen); - bool swapmad = arg_get_lit(ctx, 6); - bool override = arg_get_lit(ctx, 7); + CLIGetHexWithReturn(ctx, 4, madkey, &madkeylen); + + bool swapmad = arg_get_lit(ctx, 5); + bool override = arg_get_lit(ctx, 6); int fnlen = 0; char filename[FILE_PATH_SIZE] = {0}; - CLIParamStrToBuf(arg_get_str(ctx, 8), (uint8_t *)filename, FILE_PATH_SIZE, &fnlen); + CLIParamStrToBuf(arg_get_str(ctx, 7), (uint8_t *)filename, FILE_PATH_SIZE, &fnlen); + + bool verbose = arg_get_lit(ctx, 8); CLIParserFree(ctx); mad_card_type_t card_type; int res = mad_resolve_card(keylen, madkeylen, &card_type, force_classic, force_plus); - if (res != PM3_SUCCESS) + if (res != PM3_SUCCESS) { return res; + } - if (verbose) - PrintAndLogEx(INFO, "Using %s transport", card_type == MAD_CARD_CLASSIC ? "MIFARE Classic" : "MIFARE Plus"); + if (verbose) { + PrintAndLogEx(INFO, "Using %s transport", (card_type == MAD_CARD_CLASSIC) ? "MIFARE Classic" : "MIFARE Plus"); + } mad_ops_t ops = {0}; - mad_fill_ops(&ops, card_type, - madkeylen > 0 ? madkey : NULL, - keylen > 0 ? userkey : NULL, - keyB ? MF_KEY_B : MF_KEY_A, verbose); + mad_fill_ops(&ops, + card_type, + madkeylen > 0 ? madkey : NULL, + keylen > 0 ? userkey : NULL, + keyB ? MF_KEY_B : MF_KEY_A, + verbose + ); - uint16_t aaid = (aid[0] << 8) | aid[1]; - uint8_t data[MIFARE_4K_MAX_BYTES] = {0}; size_t datalen = 0; - res = mad_app_read(&ops, aaid, swapmad, override, data, sizeof(data), &datalen); - if (res != PM3_SUCCESS) - return res; + uint16_t aaid = MemBeToUint2byte(aid); + uint8_t data[MIFARE_4K_MAX_BYTES] = {0}; - PrintAndLogEx(INFO, "read %zu bytes from AID 0x%04X", datalen, aaid); + res = mad_app_read(&ops, aaid, swapmad, override, data, sizeof(data), &datalen); + if (res != PM3_SUCCESS) { + return res; + } + + PrintAndLogEx(INFO, "read " _YELLOW_("%zu") " bytes from AID 0x" _YELLOW_("%04X"), datalen, aaid); print_buffer_with_offset(data, datalen, 0, true); - if (fnlen > 0) + if (fnlen) { pm3_save_dump(filename, data, datalen, jsfRaw); + } return PM3_SUCCESS; } @@ -210,7 +222,6 @@ static int mad_cmd_write(const char *Cmd, const char *cmd_name, bool force_class void *argtable[] = { arg_param_begin, - arg_lit0("v", "verbose", "verbose output"), arg_str1(NULL, "aid", "", "application ID (2 hex bytes)"), arg_str0("k", "key", "", "key for data sectors"), arg_lit0("b", "keyb", "use key B (def: key A)"), @@ -219,45 +230,55 @@ static int mad_cmd_write(const char *Cmd, const char *cmd_name, bool force_class arg_lit0(NULL, "override", "override failed CRC check"), arg_str0("d", "data", "", "data to write"), arg_str0("f", "file", "", "load data from file"), + arg_lit0("v", "verbose", "verbose output"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, false); - bool verbose = arg_get_lit(ctx, 1); + uint8_t aid[2] = {0}; int aidlen = 0; CLIGetHexWithReturn(ctx, 2, aid, &aidlen); + uint8_t userkey[AES_KEY_LEN] = {0}; int keylen = 0; CLIGetHexWithReturn(ctx, 3, userkey, &keylen); - bool keyB = arg_get_lit(ctx, 4); + + bool keyB = arg_get_lit(ctx, 3); + uint8_t madkey[AES_KEY_LEN] = {0}; int madkeylen = 0; - CLIGetHexWithReturn(ctx, 5, madkey, &madkeylen); - bool swapmad = arg_get_lit(ctx, 6); - bool override = arg_get_lit(ctx, 7); + CLIGetHexWithReturn(ctx, 4, madkey, &madkeylen); + + bool swapmad = arg_get_lit(ctx, 5); + bool override = arg_get_lit(ctx, 6); + uint8_t hexdata[MIFARE_4K_MAX_BYTES] = {0}; int hexdatalen = 0; - CLIGetHexWithReturn(ctx, 8, hexdata, &hexdatalen); + CLIGetHexWithReturn(ctx, 7, hexdata, &hexdatalen); + int fnlen = 0; char filename[FILE_PATH_SIZE] = {0}; - CLIParamStrToBuf(arg_get_str(ctx, 9), (uint8_t *)filename, FILE_PATH_SIZE, &fnlen); + CLIParamStrToBuf(arg_get_str(ctx, 8), (uint8_t *)filename, FILE_PATH_SIZE, &fnlen); + + bool verbose = arg_get_lit(ctx, 9); CLIParserFree(ctx); uint8_t *wdata = hexdata; size_t wdata_len = hexdatalen; uint8_t *dump = NULL; - if (fnlen > 0) { + if (fnlen) { size_t bytes_read = 0; int res = pm3_load_dump(filename, (void **)&dump, &bytes_read, MIFARE_4K_MAX_BYTES); - if (res != PM3_SUCCESS) + if (res != PM3_SUCCESS) { return res; + } wdata = dump; wdata_len = bytes_read; } if (wdata_len == 0) { - PrintAndLogEx(ERR, "no data to write (use -d or -f)"); + PrintAndLogEx(ERR, "No data to write (use `-d` or `-f`)"); free(dump); return PM3_EINVARG; } @@ -269,16 +290,20 @@ static int mad_cmd_write(const char *Cmd, const char *cmd_name, bool force_class return res; } - if (verbose) - PrintAndLogEx(INFO, "Using %s transport", card_type == MAD_CARD_CLASSIC ? "MIFARE Classic" : "MIFARE Plus"); + if (verbose) { + PrintAndLogEx(INFO, "Using " _GREEN_("%s") " transport", (card_type == MAD_CARD_CLASSIC) ? "MIFARE Classic" : "MIFARE Plus"); + } mad_ops_t ops = {0}; - mad_fill_ops(&ops, card_type, - madkeylen > 0 ? madkey : NULL, - keylen > 0 ? userkey : NULL, - keyB ? MF_KEY_B : MF_KEY_A, verbose); + mad_fill_ops(&ops, + card_type, + madkeylen > 0 ? madkey : NULL, + keylen > 0 ? userkey : NULL, + keyB ? MF_KEY_B : MF_KEY_A, + verbose + ); - uint16_t aaid = (aid[0] << 8) | aid[1]; + uint16_t aaid = MemBeToUint2byte(aid); res = mad_app_write(&ops, aaid, swapmad, override, wdata, wdata_len); free(dump); return res; @@ -288,12 +313,11 @@ static int mad_cmd_verify(const char *Cmd, const char *cmd_name, bool force_clas CLIParserContext *ctx; CLIParserInit(&ctx, cmd_name, "Read back and verify application data against expected content", - "mad verify --aid e103 -d 0102030405060708 -> verify data\n" - "mad verify --aid e103 -f mydata.bin -> verify against file"); + "mad verify --aid e103 -d 0102030405060708\n" + "mad verify --aid e103 -f mydata.bin"); void *argtable[] = { arg_param_begin, - arg_lit0("v", "verbose", "verbose output"), arg_str1(NULL, "aid", "", "application ID (2 hex bytes)"), arg_str0("k", "key", "", "key for data sectors"), arg_lit0("b", "keyb", "use key B (def: key A)"), @@ -302,45 +326,56 @@ static int mad_cmd_verify(const char *Cmd, const char *cmd_name, bool force_clas arg_lit0(NULL, "override", "override failed CRC check"), arg_str0("d", "data", "", "expected data"), arg_str0("f", "file", "", "load expected data from file"), + arg_lit0("v", "verbose", "verbose output"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, false); - bool verbose = arg_get_lit(ctx, 1); + uint8_t aid[2] = {0}; int aidlen = 0; - CLIGetHexWithReturn(ctx, 2, aid, &aidlen); + CLIGetHexWithReturn(ctx, 1, aid, &aidlen); + uint8_t userkey[AES_KEY_LEN] = {0}; int keylen = 0; - CLIGetHexWithReturn(ctx, 3, userkey, &keylen); - bool keyB = arg_get_lit(ctx, 4); + CLIGetHexWithReturn(ctx, 2, userkey, &keylen); + + bool keyB = arg_get_lit(ctx, 3); + uint8_t madkey[AES_KEY_LEN] = {0}; int madkeylen = 0; - CLIGetHexWithReturn(ctx, 5, madkey, &madkeylen); - bool swapmad = arg_get_lit(ctx, 6); - bool override = arg_get_lit(ctx, 7); + CLIGetHexWithReturn(ctx, 4, madkey, &madkeylen); + + bool swapmad = arg_get_lit(ctx, 5); + bool override = arg_get_lit(ctx, 6); + uint8_t hexdata[MIFARE_4K_MAX_BYTES] = {0}; int hexdatalen = 0; - CLIGetHexWithReturn(ctx, 8, hexdata, &hexdatalen); + CLIGetHexWithReturn(ctx, 7, hexdata, &hexdatalen); + int fnlen = 0; char filename[FILE_PATH_SIZE] = {0}; - CLIParamStrToBuf(arg_get_str(ctx, 9), (uint8_t *)filename, FILE_PATH_SIZE, &fnlen); + CLIParamStrToBuf(arg_get_str(ctx, 8), (uint8_t *)filename, FILE_PATH_SIZE, &fnlen); + + bool verbose = arg_get_lit(ctx, 9); CLIParserFree(ctx); uint8_t *edata = hexdata; size_t edata_len = hexdatalen; uint8_t *dump = NULL; - if (fnlen > 0) { + if (fnlen) { size_t bytes_read = 0; int res = pm3_load_dump(filename, (void **)&dump, &bytes_read, MIFARE_4K_MAX_BYTES); - if (res != PM3_SUCCESS) + if (res != PM3_SUCCESS) { return res; + } + edata = dump; edata_len = bytes_read; } if (edata_len == 0) { - PrintAndLogEx(ERR, "no expected data (use -d or -f)"); + PrintAndLogEx(ERR, "No expected data (use -d or -f)"); free(dump); return PM3_EINVARG; } @@ -352,16 +387,19 @@ static int mad_cmd_verify(const char *Cmd, const char *cmd_name, bool force_clas return res; } - if (verbose) - PrintAndLogEx(INFO, "Using %s transport", card_type == MAD_CARD_CLASSIC ? "MIFARE Classic" : "MIFARE Plus"); + if (verbose) { + PrintAndLogEx(INFO, "Using " _GREEN_("%s") " transport", (card_type == MAD_CARD_CLASSIC) ? "MIFARE Classic" : "MIFARE Plus"); + } mad_ops_t ops = {0}; mad_fill_ops(&ops, card_type, - madkeylen > 0 ? madkey : NULL, - keylen > 0 ? userkey : NULL, - keyB ? MF_KEY_B : MF_KEY_A, verbose); + madkeylen > 0 ? madkey : NULL, + keylen > 0 ? userkey : NULL, + keyB ? MF_KEY_B : MF_KEY_A, + verbose + ); - uint16_t aaid = (aid[0] << 8) | aid[1]; + uint16_t aaid = MemBeToUint2byte(aid); res = mad_app_verify(&ops, aaid, swapmad, override, edata, edata_len); free(dump); return res; @@ -369,67 +407,92 @@ static int mad_cmd_verify(const char *Cmd, const char *cmd_name, bool force_clas // --- Auto-detect commands (mad read / mad write / mad verify) --- -static int CmdMADRead(const char *Cmd) { return mad_cmd_read(Cmd, "mad read", false, false); } -static int CmdMADWrite(const char *Cmd) { return mad_cmd_write(Cmd, "mad write", false, false); } -static int CmdMADVerify(const char *Cmd) { return mad_cmd_verify(Cmd, "mad verify", false, false); } +static int CmdMADRead(const char *Cmd) { + return mad_cmd_read(Cmd, "mad read", false, false); +} +static int CmdMADWrite(const char *Cmd) { + return mad_cmd_write(Cmd, "mad write", false, false); +} +static int CmdMADVerify(const char *Cmd) { + return mad_cmd_verify(Cmd, "mad verify", false, false); +} // --- Card-specific wrappers (for hf mf / hf mfp aliases) --- -int CmdMADMFRead(const char *Cmd) { return mad_cmd_read(Cmd, "hf mf madread", true, false); } -int CmdMADMFWrite(const char *Cmd) { return mad_cmd_write(Cmd, "hf mf madwrite", true, false); } -int CmdMADMFVerify(const char *Cmd) { return mad_cmd_verify(Cmd, "hf mf madverify", true, false); } -int CmdMADMFPRead(const char *Cmd) { return mad_cmd_read(Cmd, "hf mfp madread", false, true); } -int CmdMADMFPWrite(const char *Cmd) { return mad_cmd_write(Cmd, "hf mfp madwrite", false, true); } -int CmdMADMFPVerify(const char *Cmd) { return mad_cmd_verify(Cmd, "hf mfp madverify", false, true); } +int CmdMADMFRead(const char *Cmd) { + return mad_cmd_read(Cmd, "hf mf madread", true, false); +} +int CmdMADMFWrite(const char *Cmd) { + return mad_cmd_write(Cmd, "hf mf madwrite", true, false); +} +int CmdMADMFVerify(const char *Cmd) { + return mad_cmd_verify(Cmd, "hf mf madverify", true, false); +} +int CmdMADMFPRead(const char *Cmd) { + return mad_cmd_read(Cmd, "hf mfp madread", false, true); +} +int CmdMADMFPWrite(const char *Cmd) { + return mad_cmd_write(Cmd, "hf mfp madwrite", false, true); +} +int CmdMADMFPVerify(const char *Cmd) { + return mad_cmd_verify(Cmd, "hf mfp madverify", false, true); +} // --- Decode (offline) --- static int CmdMADDecode(const char *Cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "mad decode", - "Decode a MAD byte array and print the directory", - "mad decode -d -> decode MAD1\n" - "mad decode -d --mad2 -> decode MAD1 + MAD2\n"); + "Decode a MAD byte array and print the directory. \n" + "Use Sector 0 == MAD 1\n" + " Sector 16 == MAD 2\n", + "mad decode --mad1 \n" + ); void *argtable[] = { arg_param_begin, - arg_lit0("v", "verbose", "verbose output"), - arg_str1("d", "data", "", "MAD1 sector 0 data (64 bytes)"), - arg_str0(NULL, "mad2", "", "MAD2 sector 16 data (64 bytes)"), - arg_lit0(NULL, "be", "big-endian AID byte swap"), + arg_lit0("v", "verbose", "verbose output"), + arg_str1("1", "mad1", "", "MAD 1 Sector 0 data (64 bytes)"), + arg_str0("2", "mad2", "", "MAD 2 Sector 16 data (64 bytes)"), + arg_lit0(NULL, "be", "big-endian AID byte swap"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, false); + bool verbose = arg_get_lit(ctx, 1); - uint8_t data[MFBLOCK_SIZE * 4] = {0}; - int datalen = 0; - int res = CLIParamHexToBuf(arg_get_str(ctx, 2), data, sizeof(data), &datalen); - uint8_t data2[MFBLOCK_SIZE * 4] = {0}; - int data2len = 0; - int res2 = CLIParamHexToBuf(arg_get_str(ctx, 3), data2, sizeof(data2), &data2len); + + uint8_t s0[MFBLOCK_SIZE * 4] = {0}; + int s0n = 0; + int res = CLIParamHexToBuf(arg_get_str(ctx, 2), s0, sizeof(s0), &s0n); + + uint8_t s16[MFBLOCK_SIZE * 4] = {0}; + int s16n = 0; + int res2 = CLIParamHexToBuf(arg_get_str(ctx, 3), s16, sizeof(s16), &s16n); bool swapmad = arg_get_lit(ctx, 4); CLIParserFree(ctx); + if (res) { PrintAndLogEx(FAILED, "Error parsing MAD1 hex data"); return PM3_EINVARG; } - if (data2len > 0 && res2) { + + if (s16n > 0 && res2) { PrintAndLogEx(FAILED, "Error parsing MAD2 hex data"); return PM3_EINVARG; } - if (datalen < (int)sizeof(mad1_sector_t)) { - PrintAndLogEx(ERR, "Need at least %zu bytes for MAD1 (got %d)", sizeof(mad1_sector_t), datalen); + if (s0n < (int)sizeof(mad1_sector_t)) { + PrintAndLogEx(ERR, "Need at least %zu bytes for MAD1 (got %d)", sizeof(mad1_sector_t), s0n); return PM3_EINVARG; } MADPrintHeader(); bool haveMAD2 = false; - MAD1DecodeAndPrint((const mad1_sector_t *)data, swapmad, verbose, &haveMAD2); + MAD1DecodeAndPrint((const mad1_sector_t *)s0, swapmad, verbose, &haveMAD2); - if (data2len >= (int)sizeof(mad2_sector_t)) { - MAD2DecodeAndPrint((const mad2_sector_t *)data2, swapmad, verbose); - } else if (haveMAD2 && data2len == 0) { + if (s16n >= (int)sizeof(mad2_sector_t)) { + MAD2DecodeAndPrint((const mad2_sector_t *)s16, swapmad, verbose); + } else if (haveMAD2 && s16n == 0) { PrintAndLogEx(HINT, "MAD1 indicates v2, use --mad2 to provide sector 16 data"); } @@ -444,34 +507,51 @@ static int parse_sector_ranges(const char *str, uint8_t *sectors, int max_sector const char *p = str; while (*p) { - while (*p == ' ' || *p == '\t') p++; - if (*p == '\0') break; + + while (*p == ' ' || *p == '\t') { + p++; + } + + if (*p == '\0') { + break; + } char *end = NULL; long start = strtol(p, &end, 10); - if (end == p || start < 0 || start > 39) + + if (end == p || start < 0 || start > 39) { return -1; + } long stop = start; + if (*end == '-') { + p = end + 1; + stop = strtol(p, &end, 10); - if (end == p || stop < start || stop > 39) + + if (end == p || stop < start || stop > 39) { return -1; + } } for (long s = start; s <= stop; s++) { if (s == 0 || s == 16) { - PrintAndLogEx(ERR, "Sector %ld is reserved for MAD directory", s); + PrintAndLogEx(ERR, "Sector " _YELLOW_("%ld") " is reserved for MAD directory", s); return -1; } - if (count >= max_sectors) + if (count >= max_sectors) { return -1; + } sectors[count++] = (uint8_t)s; } p = end; - if (*p == ',') p++; + + if (*p == ',') { + p++; + } } return count; } @@ -482,7 +562,7 @@ static int CmdMADEncode(const char *Cmd) { "Encode a MAD byte array from AID-to-sector mappings", "mad encode --aid E103:1-3 --aid 484D:4 -> MAD1 with NDEF + HID\n" "mad encode --aid E103:1-3,5 -> non-contiguous sectors\n" - "mad encode --aid E103:1-3,17-18 --aid 484D:4 -> MAD1 + MAD2\n" + "mad encode --aid E103:1-3,17-18 --aid 484D:4 -> MAD1 + MAD2 + HID\n" "mad encode --aid E103:1-3 -q -> quiet, hex only\n"); void *argtable[] = { @@ -493,10 +573,9 @@ static int CmdMADEncode(const char *Cmd) { arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, false); - - int aid_count = 0; + struct arg_str *aid_arg = arg_get_str(ctx, 1); - aid_count = aid_arg->count; + int aid_count = aid_arg->count; uint16_t sector_aids[40] = {0}; bool have_mad2 = false; @@ -513,7 +592,7 @@ static int CmdMADEncode(const char *Cmd) { int aid_hex_len = colon - val; if (aid_hex_len != 4) { - PrintAndLogEx(ERR, "AID must be 4 hex chars, got %d in '%s'", aid_hex_len, val); + PrintAndLogEx(ERR, "AID must be 4 hex chars, got " _YELLOW_("%d"), aid_hex_len); CLIParserFree(ctx); return PM3_EINVARG; } @@ -525,7 +604,7 @@ static int CmdMADEncode(const char *Cmd) { uint8_t sectors[40] = {0}; int nsectors = parse_sector_ranges(colon + 1, sectors, 40); if (nsectors <= 0) { - PrintAndLogEx(ERR, "Invalid sector range in '%s'", val); + PrintAndLogEx(ERR, "Invalid sector range in " _YELLOW_("'%s'"), val); CLIParserFree(ctx); return PM3_EINVARG; } @@ -533,7 +612,7 @@ static int CmdMADEncode(const char *Cmd) { for (int s = 0; s < nsectors; s++) { uint8_t sno = sectors[s]; if (sector_aids[sno] != 0) { - PrintAndLogEx(ERR, "Sector %d already assigned to AID 0x%04X", sno, sector_aids[sno]); + PrintAndLogEx(ERR, "Sector " _YELLOW_("%d") " already assigned to AID " _YELLOW_("0x%04X"), sno, sector_aids[sno]); CLIParserFree(ctx); return PM3_EINVARG; } @@ -549,38 +628,40 @@ static int CmdMADEncode(const char *Cmd) { // build MAD1 mad1_sector_t s0; memset(&s0, 0, sizeof(s0)); - s0.mad.info = 0x00; + for (int i = 0; i < MAD1_NUM_AIDS; i++) { uint16_t a = sector_aids[i + 1]; s0.mad.aid[i] = swapmad ? BSWAP_16(a) : a; } - s0.mad.crc = CRC8Mad((uint8_t *)&s0.mad.info, sizeof(mad1_t) - 1); + + s0.mad.info = 0x00; + s0.mad.crc = CRC8Mad((uint8_t *)&s0.mad.info, sizeof(mad1_t) - 1); memcpy(s0.trailer.key_a, g_mifare_mad_key, MIFARE_KEY_SIZE); - s0.trailer.access[0] = 0x78; - s0.trailer.access[1] = 0x77; - s0.trailer.access[2] = 0x88; - s0.trailer.gpb = have_mad2 ? 0xC2 : 0xC1; // DA=1, MA=1, version + memcpy(s0.trailer.access, "\x78\x77\x88", 3); + s0.trailer.gpb = (have_mad2) ? 0xC2 : 0xC1; // DA=1, MA=1, version memcpy(s0.trailer.key_b, g_mifare_mad_key_b, MIFARE_KEY_SIZE); // build MAD2 if needed mad2_sector_t s16; memset(&s16, 0, sizeof(s16)); + if (have_mad2) { - s16.mad.info = 0x00; + + for (int i = 0; i < MAD2_NUM_AIDS; i++) { uint16_t a = sector_aids[i + 17]; s16.mad.aid[i] = swapmad ? BSWAP_16(a) : a; } + s16.mad.info = 0x00; s16.mad.crc = CRC8Mad((uint8_t *)&s16.mad.info, sizeof(mad2_t) - 1); + memcpy(s16.trailer.key_a, g_mifare_mad_key, MIFARE_KEY_SIZE); - s16.trailer.access[0] = 0x78; - s16.trailer.access[1] = 0x77; - s16.trailer.access[2] = 0x88; + memcpy(s16.trailer.access, "\x78\x77\x88", 3); s16.trailer.gpb = 0xC2; memcpy(s16.trailer.key_b, g_mifare_mad_key_b, MIFARE_KEY_SIZE); } - if (!quiet) { + if (quiet == false) { MADPrintHeader(); bool dummy = false; MAD1DecodeAndPrint(&s0, swapmad, false, &dummy); @@ -606,9 +687,9 @@ static int CmdMADEncode(const char *Cmd) { static int CmdMADTest(const char *Cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "mad test", - "Run MAD regression tests (offline, no card needed)", + "Run MAD regression tests. Runs offline with no card needed", "mad test\n" - "mad test -v -> verbose output"); + ); void *argtable[] = { arg_param_begin, @@ -625,12 +706,12 @@ static int CmdMADTest(const char *Cmd) { static int CmdHelp(const char *Cmd); static command_t CommandTable[] = { + {"help", CmdHelp, AlwaysAvailable, "This help"}, {"--------", CmdHelp, AlwaysAvailable, "--------------- " _CYAN_("MAD") " -----------------"}, {"read", CmdMADRead, IfPm3Iso14443a, "Read data from MAD AID sectors"}, {"write", CmdMADWrite, IfPm3Iso14443a, "Write data to MAD AID sectors"}, {"verify", CmdMADVerify, IfPm3Iso14443a, "Verify data in MAD AID sectors"}, {"--------", CmdHelp, AlwaysAvailable, "------------- " _CYAN_("General") " ---------------"}, - {"help", CmdHelp, AlwaysAvailable, "This help"}, {"decode", CmdMADDecode, AlwaysAvailable, "Decode MAD byte array"}, {"encode", CmdMADEncode, AlwaysAvailable, "Encode MAD byte array from AID mappings"}, {"test", CmdMADTest, AlwaysAvailable, "Run MAD regression tests"}, diff --git a/client/src/cmdmad.h b/client/src/cmdmad.h index a6202c351..77f65375c 100644 --- a/client/src/cmdmad.h +++ b/client/src/cmdmad.h @@ -21,6 +21,12 @@ #include "common.h" + +typedef enum { + MAD_CARD_CLASSIC, + MAD_CARD_PLUS, +} mad_card_type_t; + int CmdMAD(const char *Cmd); int CmdMADMFRead(const char *Cmd); diff --git a/client/src/crypto/libpcrypto.c b/client/src/crypto/libpcrypto.c index 2bc9147bc..0cce8d54b 100644 --- a/client/src/crypto/libpcrypto.c +++ b/client/src/crypto/libpcrypto.c @@ -844,7 +844,7 @@ void des3_decrypt(void *out, const void *in, const void *key, uint8_t keycount) } // NIST Special Publication 800-38A — Recommendation for block cipher modes of operation: methods and techniques, 2001. -int aes_encode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int length) { +int aes_encode(uint8_t *iv, const uint8_t *key, uint8_t *input, uint8_t *output, int length) { uint8_t iiv[16] = {0}; if (iv) { memcpy(iiv, iv, sizeof(iiv)); @@ -862,7 +862,7 @@ int aes_encode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int l return PM3_SUCCESS; } -int aes_decode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int length) { +int aes_decode(uint8_t *iv, const uint8_t *key, uint8_t *input, uint8_t *output, int length) { uint8_t iiv[16] = {0}; if (iv) { memcpy(iiv, iv, 16); @@ -881,7 +881,7 @@ int aes_decode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int l } // NIST Special Publication 800-38A — Recommendation for block cipher modes of operation: methods and techniques, 2001. -int aes256_encode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int length) { +int aes256_encode(uint8_t *iv, const uint8_t *key, uint8_t *input, uint8_t *output, int length) { uint8_t iiv[16] = {0}; if (iv) { memcpy(iiv, iv, sizeof(iiv)); @@ -900,7 +900,7 @@ int aes256_encode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, in } -int aes256_decode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int length) { +int aes256_decode(uint8_t *iv, const uint8_t *key, uint8_t *input, uint8_t *output, int length) { uint8_t iiv[16] = {0}; if (iv) { memcpy(iiv, iv, 16); @@ -921,14 +921,14 @@ int aes256_decode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, in // NIST Special Publication 800-38B — Recommendation for block cipher modes of operation: The CMAC mode for authentication. // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/AES_CMAC.pdf -int aes_cmac(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *mac, int length) { +int aes_cmac(uint8_t *iv, const uint8_t *key, uint8_t *input, uint8_t *mac, int length) { memset(mac, 0x00, 16); // NIST 800-38B return mbedtls_aes_cmac_prf_128(key, MBEDTLS_AES_BLOCK_SIZE, input, length, mac); } -int aes_cmac8(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *mac, int length) { +int aes_cmac8(uint8_t *iv, const uint8_t *key, uint8_t *input, uint8_t *mac, int length) { uint8_t cmac_tmp[16] = {0}; memset(mac, 0x00, 8); diff --git a/client/src/crypto/libpcrypto.h b/client/src/crypto/libpcrypto.h index 4a98d2f7e..0b10f32ad 100644 --- a/client/src/crypto/libpcrypto.h +++ b/client/src/crypto/libpcrypto.h @@ -49,14 +49,14 @@ void des_decrypt_cbc(void *out, const void *in, const int length, const void *ke void des3_encrypt(void *out, const void *in, const void *key, uint8_t keycount); void des3_decrypt(void *out, const void *in, const void *key, uint8_t keycount); -int aes_encode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int length); -int aes_decode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int length); +int aes_encode(uint8_t *iv, const uint8_t *key, uint8_t *input, uint8_t *output, int length); +int aes_decode(uint8_t *iv, const uint8_t *key, uint8_t *input, uint8_t *output, int length); -int aes256_encode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int length); -int aes256_decode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int length); +int aes256_encode(uint8_t *iv, const uint8_t *key, uint8_t *input, uint8_t *output, int length); +int aes256_decode(uint8_t *iv, const uint8_t *key, uint8_t *input, uint8_t *output, int length); -int aes_cmac(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *mac, int length); -int aes_cmac8(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *mac, int length); +int aes_cmac(uint8_t *iv, const uint8_t *key, uint8_t *input, uint8_t *mac, int length); +int aes_cmac8(uint8_t *iv, const uint8_t *key, uint8_t *input, uint8_t *mac, int length); int sha1hash(uint8_t *input, int length, uint8_t *hash); int sha256hash(uint8_t *input, int length, uint8_t *hash); diff --git a/client/src/frame_progress.c b/client/src/frame_progress.c new file mode 100644 index 000000000..4b8dcd673 --- /dev/null +++ b/client/src/frame_progress.c @@ -0,0 +1,418 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// Flasher progress +//----------------------------------------------------------------------------- + +#include +#include +#include +#include +#include +#include +#include "frame_progress.h" +#include "frame_data.h" +#include "util_posix.h" + +// --- platform: terminal-width detection ----------------------------------/ +#ifdef _WIN32 +# include +static int terminal_width(void) { + CONSOLE_SCREEN_BUFFER_INFO csbi; + if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) { + return csbi.srWindow.Right - csbi.srWindow.Left + 1; + } + const char *c = getenv("COLUMNS"); + if (c) { + int w = atoi(c); + if (w > 0) { + return w; + } + } + return 80; +} +#else +# include +# include +static int terminal_width(void) { + struct winsize ws; + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0 && ws.ws_col > 0) { + return (int)ws.ws_col; + } + const char *c = getenv("COLUMNS"); + if (c) { + int w = atoi(c); + if (w > 0) { + return w; + } + } + return 80; +} +#endif + +// --- wave parameters (unchanged from hadouken_wave.c) ------------------- +#define ORIGIN_X 64.0 +#define ORIGIN_Y 8.0 +#define ASPECT 2.0 +#define K 0.60 +#define OMEGA 5.0 +#define FLOOR 0.45 + +// BAR_OVERHEAD = visible fixed chars in the bar line: " Flashing [" (11) + "] 100%" (6) +#define BAR_OVERHEAD 17 +#define BAR_MIN 10 // minimum inner fill width +#define BAR_MAX 512 // guard against absurdly wide terminals +#define BAR_ROW (FH + 2) // terminal row for the progress bar (1-based) +#define AFTER_ROW (FH + 3) // first free row after the animation+bar + +// --- ANSI colour macros --------------------------------------------------- +#define ANSI_RESET "\x1b[0m" // reset all attributes +#define ANSI_BOLD "\x1b[1m" // bold / bright +#define ANSI_DIM "\x1b[2m" // dim / faint + +// Progress-bar role colours +#define ANSI_BAR_LABEL "\x1b[36m" // label " Flashing " — cyan +#define ANSI_BAR_BRACKET "\x1b[37m" // brackets [ ] — white +#define ANSI_BAR_FILLED "\x1b[36;1m" // filled '=' — green bold +#define ANSI_BAR_EMPTY "\x1b[2;37m" // empty '-' — dim white +#define ANSI_BAR_PCT "\x1b[33;1m" // percentage number — yellow bold + +// --- cursor / screen control macros -------------------------------------- +// Non-colour escape sequences. +#define ESC_CURSOR_HIDE "\x1b[?25l" // hide the cursor +#define ESC_CURSOR_SHOW "\x1b[?25h" // show the cursor +#define ESC_CURSOR_HOME "\x1b[H" // move to row 1, col 1 +#define ESC_CURSOR_POS "\x1b[%d;1H" // move to row %d col 1 (needs int arg) +#define ESC_ERASE_LINE "\x1b[2K" // erase entire current line +#define ESC_ERASE_SCREEN "\x1b[2J" // erase entire screen +#define ESC_SYNC_ON "\x1b[?2026h" // DEC 2026 synchronized output — on +#define ESC_SYNC_OFF "\x1b[?2026l" // DEC 2026 synchronized output — off + +static const char *GLYPHS = " .:;+xX#"; + +// --- shared state -------------------------------------------------------- +static volatile sig_atomic_t g_resize = 0; // set by SIGWINCH +static volatile int g_running = 0; +static volatile int g_progress = 0; // 0..100 +static pthread_t g_thread; + +// animation config – written once before hadouken_start() +static double g_fps = 30.0; +static int g_glyph_mode = 0; +static int g_wave_mode = 0; // 0 = radial, 1 = beam + +void hadouken_on_sigint(int s) { + (void)s; + g_running = 0; +} + +#ifndef _WIN32 +void hadouken_on_sigwinch(int s) { + (void)s; + g_resize = 1; +} +#endif + +/* + * Compute the inner fill width for the progress bar from the current terminal + * width, clamped to [BAR_MIN, BAR_MAX]. + */ +static int bar_inner_width(void) { + int w = terminal_width() - BAR_OVERHEAD; + if (w < BAR_MIN) { + w = BAR_MIN; + } + if (w > BAR_MAX) { + w = BAR_MAX; + } + return w; +} + +// --- colour helpers ------------------------------------------------------ +static inline int clamp8(double v) { + if (v < 0) { + return 0; + } + if (v > 255) { + return 255; + } + return (int)(v + 0.5); +} + +static void fire_color(int x, int y, const Cell *c, double t, int mode, int *r, int *g, int *b) { + double dx = x - ORIGIN_X; + double dy = (y - ORIGIN_Y) * ASPECT; + double d = sqrt(dx * dx + dy * dy); + double phase_var = mode ? (dx > 0 ? dx : 0.0) : d; + double wave = 0.5 + 0.5 * sin(phase_var * K - OMEGA * t); + double atten = 1.0 - d * 0.012; + + if (atten < 0.35) { + atten = 0.35; + } + + if (atten > 1.0) { + atten = 1.0; + } + + double inten = (FLOOR + (1.0 - FLOOR) * wave) * atten; + double crest = wave * wave; + + *r = clamp8(c->r * inten + 70 * crest); + *g = clamp8(c->g * inten + 110 * crest); + *b = clamp8(c->b * inten + 50 * crest); +} + +// --- line renderers ------------------------------------------------------ + +// Render one animation row into dst (same logic as hadouken_wave.c). +static int render_anim_line(char *dst, int y, double t, int glyph_mode, int mode) { + int len = 0; + int last_r = -1, last_g = -1, last_b = -1, last_has = -2; + int pending_blanks = 0; + + for (int x = 0; x < FW; x++) { + + const Cell *c = &FRAME[y][x]; + int ch = c->ch; + int has = c->has; + int r = 0; + int g = 0; + int b = 0; + + if (ch == ' ' && !has) { + pending_blanks++; + continue; + } + + if (c->fire) { + + fire_color(x, y, c, t, mode, &r, &g, &b); + has = 1; + + if (glyph_mode) { + + double lum = (r + g + b) / 765.0; + + int idx = (int)(lum * 8); + if (idx > 7) { + idx = 7; + } + + ch = GLYPHS[idx]; + if (ch == ' ') { + ch = '.'; + } + } + } else if (has) { + r = c->r; + g = c->g; + b = c->b; + } + + if (pending_blanks) { + memset(dst + len, ' ', pending_blanks); + len += pending_blanks; + pending_blanks = 0; + } + + if (has != last_has || r != last_r || g != last_g || b != last_b) { + if (has) { + len += sprintf(dst + len, "\x1b[38;2;%d;%d;%dm", r, g, b); + } else { + len += sprintf(dst + len, ANSI_RESET); + } + last_has = has; + last_r = r; + last_g = g; + last_b = b; + } + dst[len++] = (char)ch; + } + + dst[len] = '\0'; + return len; +} + +/* + * Render the progress bar into dst. bar_width is the inner fill width, + * computed dynamically from the current terminal width each frame. + * + * Flashing [========================================----] 100% + * ^<--------- bar_width chars ----------->^ + */ +static int render_progress_bar(char *dst, int pct, int bar_width) { + int len = 0; + int filled = (bar_width * pct) / 100; + + if (filled > bar_width) { + filled = bar_width; + } + + // label + len += sprintf(dst + len, ANSI_BAR_LABEL " Flashing " ANSI_RESET ANSI_BAR_BRACKET "[" ANSI_RESET); + + // filled portion + if (filled > 0) { + len += sprintf(dst + len, ANSI_BAR_FILLED); + for (int i = 0; i < filled; i++) { + dst[len++] = '='; + } + } + + // empty portion + if (filled < bar_width) { + len += sprintf(dst + len, ANSI_BAR_EMPTY); + for (int i = filled; i < bar_width; i++) { + dst[len++] = '-'; + } + } + + // closing bracket + percentage + len += sprintf(dst + len, ANSI_RESET ANSI_BAR_BRACKET "] " ANSI_RESET ANSI_BAR_PCT "%3d%%" ANSI_RESET, pct); + dst[len] = '\0'; + return len; +} + +static double now_sec(void) { + return msclock() / 1000.0; +} + +// --- animation thread ---------------------------------------------------- +static void *anim_thread(void *arg) { + (void)arg; + + // static keeps these off the thread stack (BSS) + static char cur [FH][FW * 24 + 16]; + static char prev[FH][FW * 24 + 16]; + static char out [FH * (FW * 24 + 32) + 512]; + + // bar buf: BAR_MAX fill chars + ANSI escapes per char + fixed overhead + static char bar [BAR_MAX + 256]; + + int have_prev = 0; + double frame_dt = 1.0 / g_fps; + double start = now_sec(); + + while (g_running) { + double t0 = now_sec(); + double t = t0 - start; + int pct = g_progress; // single-read snapshot + + // On terminal resize: flush the diff cache so all rows redraw cleanly + if (g_resize) { + g_resize = 0; + have_prev = 0; + } + + // dynamic: re-queried every frame + int bw = bar_inner_width(); + + int n = 0; + + // sync on, home + n += sprintf(out + n, ESC_SYNC_ON ESC_CURSOR_HOME); + + // animation rows (with per-line diffing) + for (int y = 0; y < FH; y++) { + + int len = render_anim_line(cur[y], y, t, g_glyph_mode, g_wave_mode); + + if (have_prev && strcmp(cur[y], prev[y]) == 0) { + continue; + } + + n += sprintf(out + n, ESC_CURSOR_POS ESC_ERASE_LINE, y + 1); + memcpy(out + n, cur[y], len); + + n += len; + memcpy(prev[y], cur[y], len + 1); + } + + // progress bar (always redrawn, width adapts to terminal) + int blen = render_progress_bar(bar, pct, bw); + n += sprintf(out + n, ESC_CURSOR_POS ESC_ERASE_LINE, BAR_ROW); + memcpy(out + n, bar, blen); + n += blen; + + // reset, sync off + n += sprintf(out + n, ANSI_RESET ESC_SYNC_OFF); + have_prev = 1; + + fwrite(out, 1, n, stdout); + fflush(stdout); + + // fixed-timestep with render-time compensation + double remain = frame_dt - (now_sec() - t0); + if (remain > 0) { + msleep((uint32_t)(remain * 1000)); + } + } + + return NULL; +} + +/* + * Start the animation thread. + * fps – frames per second (e.g. 30.0) + * glyph_mode – 1 = ASCII glyphs instead of colour blocks + * wave_mode – 0 = radial rings, 1 = forward beam + */ +void hadouken_start(double fps, int glyph_mode, int wave_mode) { + g_fps = (fps > 0) ? fps : 30.0; + g_glyph_mode = glyph_mode; + g_wave_mode = wave_mode; + g_progress = 0; + g_running = 1; + + // hide cursor, clear screen + fputs(ESC_CURSOR_HIDE ESC_ERASE_SCREEN, stdout); + fflush(stdout); + pthread_create(&g_thread, NULL, anim_thread, NULL); +} + +/* + * Update the progress bar. + * Safe to call from any thread. + * pct is clamped to [0, 100]. + */ +void hadouken_set_progress(int pct) { + if (pct < 0) { + pct = 0; + } + + if (pct > 100) { + pct = 100; + } + g_progress = pct; +} + +/* + * Stop the animation thread, then position the cursor on the line below the + * progress bar and restore it so subsequent printf/puts output is visible. + */ +void hadouken_stop(void) { + /* Wait 2 frame durations so the thread renders at least one more frame + * with the final g_progress value (e.g. 100%) before we signal exit. + * Without this, the thread may be mid-sleep and exit before painting 100%. */ + msleep((uint32_t)(2000.0 / g_fps)); + + g_running = 0; + pthread_join(g_thread, NULL); + + // Move below the progress bar, restore cursor visibility + fprintf(stdout, ESC_CURSOR_POS ANSI_RESET ESC_CURSOR_SHOW "\n", AFTER_ROW); + fflush(stdout); +} diff --git a/client/src/frame_progress.h b/client/src/frame_progress.h new file mode 100644 index 000000000..5bd06d094 --- /dev/null +++ b/client/src/frame_progress.h @@ -0,0 +1,33 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// Flashing progress functions +//----------------------------------------------------------------------------- + +#ifndef __FRAME_PROGRESS_H__ +#define __FRAME_PROGRESS_H__ + +#include "common.h" +#include "elf.h" + +void hadouken_start(double fps, int glyph_mode, int wave_mode); +void hadouken_set_progress(int pct); +void hadouken_stop(void); + +void hadouken_on_sigint(int s); +void hadouken_on_sigwinch(int s); + +#endif + diff --git a/client/src/mifare/mad.c b/client/src/mifare/mad.c index fbeffe3a0..8da958b61 100644 --- a/client/src/mifare/mad.c +++ b/client/src/mifare/mad.c @@ -91,8 +91,9 @@ static int close_mad_file(json_t *root) { static const char *mad_json_get_str(json_t *data, const char *name) { json_t *jstr = json_object_get(data, name); - if (jstr == NULL) + if (jstr == NULL) { return NULL; + } if (!json_is_string(jstr)) { PrintAndLogEx(WARNING, _YELLOW_("`%s`") " is not a string", name); @@ -100,8 +101,9 @@ static const char *mad_json_get_str(json_t *data, const char *name) { } const char *cstr = json_string_value(jstr); - if (strlen(cstr) == 0) + if (strlen(cstr) == 0) { return NULL; + } return cstr; } @@ -115,18 +117,24 @@ static json_t *mad_lookup_aid(json_t *root, uint16_t aid) { snprintf(lmad, sizeof(lmad), "0x%04x", aid); for (uint32_t idx = 0; idx < json_array_size(root); idx++) { + json_t *data = json_array_get(root, idx); if (!json_is_object(data)) { PrintAndLogEx(ERR, "data [%u] is not an object", idx); continue; } + const char *fmad = mad_json_get_str(data, "mad"); - if (fmad == NULL) + if (fmad == NULL) { continue; + } + char lfmad[16]; strncpy(lfmad, fmad, sizeof(lfmad) - 1); lfmad[sizeof(lfmad) - 1] = '\0'; + str_lower(lfmad); + if (strcmp(lmad, lfmad) == 0) { return data; } @@ -138,6 +146,7 @@ static const char *mad_aid_description(json_t *elm) { static char result[256]; const char *application = mad_json_get_str(elm, "application"); const char *company = mad_json_get_str(elm, "company"); + if (application && company) { snprintf(result, sizeof(result), " %s [%s]", application, company); } else if (application) { @@ -145,6 +154,7 @@ static const char *mad_aid_description(json_t *elm) { } else { snprintf(result, sizeof(result), " (unknown)"); } + return result; } @@ -159,12 +169,15 @@ static void mad_print_aid_verbose(json_t *elm) { if (application) { PrintAndLogEx(SUCCESS, " Application......... %s", application); } + if (company) { PrintAndLogEx(SUCCESS, " Company............. %s", company); } + if (provider) { PrintAndLogEx(SUCCESS, " Service provider.... %s", provider); } + if (integrator) { PrintAndLogEx(SUCCESS, " System integrator... %s", integrator); } @@ -173,8 +186,9 @@ static void mad_print_aid_verbose(json_t *elm) { static int mad1CRCCheck(const mad1_t *mad1, bool verbose) { uint8_t crc = CRC8Mad((uint8_t *)&mad1->info, sizeof(mad1_t) - 1); if (crc != mad1->crc) { - if (verbose) + if (verbose) { PrintAndLogEx(WARNING, _RED_("Wrong MAD 1 CRC") " calculated: 0x%02x != 0x%02x", crc, mad1->crc); + } return PM3_ESOFT; } return PM3_SUCCESS; @@ -183,15 +197,18 @@ static int mad1CRCCheck(const mad1_t *mad1, bool verbose) { static int mad2CRCCheck(const mad2_t *mad2, bool verbose) { uint8_t crc = CRC8Mad((uint8_t *)&mad2->info, sizeof(mad2_t) - 1); if (crc != mad2->crc) { - if (verbose) + if (verbose) { PrintAndLogEx(WARNING, _RED_("Wrong MAD 2 CRC") " calculated: 0x%02x != 0x%02x", crc, mad2->crc); + } return PM3_ESOFT; } return PM3_SUCCESS; } static int MADInfoByteDecode(uint8_t info, int mad_ver, bool verbose) { + info &= MAD_INFO_MASK; + if (mad_ver == 1 && info >= 0xF) { PrintAndLogEx(WARNING, "Invalid Info byte (MAD1) value " _YELLOW_("0x%02x"), info); if (verbose) { @@ -199,6 +216,7 @@ static int MADInfoByteDecode(uint8_t info, int mad_ver, bool verbose) { } return PM3_ESOFT; } + if (mad_ver == 2 && (info == 0x10 || info >= 0x28)) { PrintAndLogEx(WARNING, "Invalid Info byte (MAD2) value " _YELLOW_("0x%02x"), info); return PM3_ESOFT; @@ -207,8 +225,9 @@ static int MADInfoByteDecode(uint8_t info, int mad_ver, bool verbose) { } int MADCheck(const mad1_sector_t *sector0, const mad2_sector_t *mad2, bool verbose, bool *haveMAD2) { - if (sector0 == NULL) + if (sector0 == NULL) { return PM3_EINVARG; + } uint8_t GPB = sector0->trailer.gpb; if (verbose) { @@ -250,10 +269,11 @@ int MADCheck(const mad1_sector_t *sector0, const mad2_sector_t *mad2, bool verbo } if (verbose) { - if (GPB & MAD_GPB_MA_MASK) + if (GPB & MAD_GPB_MA_MASK) { PrintAndLogEx(SUCCESS, "Multi application card"); - else + } else { PrintAndLogEx(SUCCESS, "Single application card"); + } } return res; } @@ -269,7 +289,7 @@ int MADDecode(const mad1_sector_t *sector0, const mad2_sector_t *mad2, mad_entry } if (override) { - PrintAndLogEx(INFO, "overriding crc check"); + PrintAndLogEx(INFO, "Overriding crc check"); } for (int i = 0; i < MAD1_NUM_AIDS; i++) { @@ -289,19 +309,28 @@ int MADDecode(const mad1_sector_t *sector0, const mad2_sector_t *mad2, mad_entry } int MADCardHolderInfoDecode(const uint8_t *data, size_t datalen, bool verbose) { + size_t idx = 0; + while (idx < datalen) { + uint8_t len = data[idx] & MAD_TLV_LEN_MASK; uint8_t type = data[idx] >> MAD_TLV_TYPE_SHIFT; idx++; - if (len == 0) + + if (len == 0) { break; + } + if (idx + len > datalen) { PrintAndLogEx(WARNING, "Card holder info truncated (need %u bytes, %zu available)", len, datalen - idx); break; } - if (type >= ARRAYLEN(holder_info_type)) + + if (type >= ARRAYLEN(holder_info_type)) { type = ARRAYLEN(holder_info_type) - 1; + } + PrintAndLogEx(INFO, "%14s " _GREEN_("%.*s"), holder_info_type[type], len, &data[idx]); idx += len; } @@ -314,6 +343,7 @@ void MADPrintHeader(void) { } int MAD1DecodeAndPrint(const mad1_sector_t *sector, bool swapmad, bool verbose, bool *haveMAD2) { + if (open_mad_file(&mad_known_aids, verbose) != PM3_SUCCESS) { PrintAndLogEx(WARNING, "Could not load MAD JSON, AID names will not be resolved"); } @@ -335,9 +365,12 @@ int MAD1DecodeAndPrint(const mad1_sector_t *sector, bool swapmad, bool verbose, PrintAndLogEx(INFO, "---------------- " _CYAN_("Listing") " ----------------"); PrintAndLogEx(INFO, " 00 MAD v1"); + uint32_t prev_aid = 0xFFFFFFFF; for (int i = 0; i < MAD1_NUM_AIDS; i++) { + int sector_no = i + 1; + uint16_t aid = madGetAID(sector->mad.aid[i], swapmad); if (aid <= MAD_AID_ADMIN_MAX) { PrintAndLogEx(INFO, @@ -362,6 +395,7 @@ int MAD1DecodeAndPrint(const mad1_sector_t *sector, bool swapmad, bool verbose, aid, desc ); + if (verbose && elm) { mad_print_aid_verbose(elm); } @@ -373,6 +407,7 @@ int MAD1DecodeAndPrint(const mad1_sector_t *sector, bool swapmad, bool verbose, } int MAD2DecodeAndPrint(const mad2_sector_t *sector, bool swapmad, bool verbose) { + if (open_mad_file(&mad_known_aids, false) != PM3_SUCCESS) { PrintAndLogEx(WARNING, "Could not load MAD JSON, AID names will not be resolved"); } @@ -382,10 +417,11 @@ int MAD2DecodeAndPrint(const mad2_sector_t *sector, bool swapmad, bool verbose) int res = mad2CRCCheck(§or->mad, true); if (verbose) { - if (res == PM3_SUCCESS) + if (res == PM3_SUCCESS) { PrintAndLogEx(SUCCESS, "CRC8...... 0x%02X ( " _GREEN_("%s") " )", sector->mad.crc, "ok"); - else + } else { PrintAndLogEx(SUCCESS, "CRC8...... 0x%02X ( " _RED_("%s") " )", sector->mad.crc, "fail"); + } } int ibs = MADInfoByteDecode(sector->mad.info, 2, verbose); @@ -402,8 +438,10 @@ int MAD2DecodeAndPrint(const mad2_sector_t *sector, bool swapmad, bool verbose) uint32_t prev_aid = 0xFFFFFFFF; for (int i = 0; i < MAD2_NUM_AIDS; i++) { + int sector_no = i + 17; uint16_t aid = madGetAID(sector->mad.aid[i], swapmad); + if (aid <= MAD_AID_ADMIN_MAX) { PrintAndLogEx(INFO, (ibs == sector_no) ? _MAGENTA_(" %02d [%04X] %s") : " %02d [" _GREEN_("%04X") "] %s", @@ -445,34 +483,40 @@ int MADDFDecodeAndPrint(uint32_t short_aid, bool verbose) { json_t *elm = mad_lookup_aid(mad_known_aids, short_aid); const char *desc = elm ? mad_aid_description(elm) : " (unknown)"; PrintAndLogEx(INFO, " MAD AID Function 0x%04X... " _YELLOW_("%s"), short_aid, desc); + if (verbose && elm) { mad_print_aid_verbose(elm); } + close_mad_file(mad_known_aids); return PM3_SUCCESS; } bool HasMADKey(const mad1_sector_t *s0) { - if (s0 == NULL) + if (s0 == NULL) { return false; + } return (memcmp(s0->trailer.key_a, g_mifare_mad_key, sizeof(g_mifare_mad_key)) == 0); } int DetectHID(const mad1_sector_t *s0, uint16_t manufacture) { - if (s0 == NULL) + if (s0 == NULL) { return -1; + } + for (int i = 0; i < MAD1_NUM_AIDS; i++) { - if (madGetAID(s0->mad.aid[i], false) == manufacture) + if (madGetAID(s0->mad.aid[i], false) == manufacture) { return i + 1; + } } return -1; } -int convert_mad_to_arr(const mad1_sector_t *s0, const mad2_sector_t *s16, - size_t dump_len, - uint8_t *out, size_t omax, size_t *olen, bool override) { - if (s0 == NULL || out == NULL || olen == NULL || dump_len < sizeof(mad1_sector_t) || omax == 0) +int convert_mad_to_arr(const mad1_sector_t *s0, const mad2_sector_t *s16, size_t dump_len, uint8_t *out, size_t omax, size_t *olen, bool override) { + + if (s0 == NULL || out == NULL || olen == NULL || dump_len < sizeof(mad1_sector_t) || omax == 0) { return PM3_EINVARG; + } *olen = 0; @@ -482,26 +526,30 @@ int convert_mad_to_arr(const mad1_sector_t *s0, const mad2_sector_t *s16, } mad_entry_list_t mad_list = {0}; - if (MADDecode(s0, s16, &mad_list, false, override)) { - PrintAndLogEx(ERR, "can't decode MAD"); + if (MADDecode(s0, s16, &mad_list, false, override) != PM3_SUCCESS) { + PrintAndLogEx(ERR, "Can't decode MAD"); return PM3_ESOFT; } const uint8_t *dump = (const uint8_t *)s0; uint16_t ndef_aid = 0xE103; + for (size_t i = 0; i < mad_list.len; i++) { - if (ndef_aid != mad_list.entries[i].aid) + + if (ndef_aid != mad_list.entries[i].aid) { continue; + } uint8_t sector_no = mad_list.entries[i].sector; size_t offset = mfFirstBlockOfSector(sector_no) * MFBLOCK_SIZE; size_t sector_size = mfNumBlocksPerSector(sector_no) * MFBLOCK_SIZE; size_t data_size = sector_size - MFBLOCK_SIZE; - if (offset + sector_size > dump_len) + if ((offset + sector_size) > dump_len) { break; + } - if (*olen + data_size > omax) { + if ((*olen + data_size) > omax) { PrintAndLogEx(WARNING, "NDEF output buffer full"); return PM3_EOVFLOW; } @@ -512,20 +560,19 @@ int convert_mad_to_arr(const mad1_sector_t *s0, const mad2_sector_t *s16, return PM3_SUCCESS; } -static int mad_read_directory(const mad_ops_t *ops, bool swapmad, bool override, - mad_entry_list_t *mad_list) { +static int mad_read_directory(const mad_ops_t *ops, bool swapmad, bool override, mad_entry_list_t *mad_list) { + mad1_sector_t sector0 = {0}; - int res = ops->read_sector(MF_MAD1_SECTOR, ops->mad_key_type, - ops->mad_key, (uint8_t *)§or0, ops->verbose); + int res = ops->read_sector(MF_MAD1_SECTOR, ops->mad_key_type, ops->mad_key, (uint8_t *)§or0, ops->verbose); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "error reading MAD1 sector 0"); + PrintAndLogEx(ERR, "Error reading MAD1 sector " _YELLOW_("0")); return res; } mad2_sector_t mad2 = {0}; const mad2_sector_t *pmad2 = NULL; - res = ops->read_sector(MF_MAD2_SECTOR, ops->mad_key_type, - ops->mad_key, (uint8_t *)&mad2, ops->verbose); + + res = ops->read_sector(MF_MAD2_SECTOR, ops->mad_key_type, ops->mad_key, (uint8_t *)&mad2, ops->verbose); if (res == PM3_SUCCESS) { pmad2 = &mad2; } else if (ops->verbose) { @@ -535,75 +582,85 @@ static int mad_read_directory(const mad_ops_t *ops, bool swapmad, bool override, return MADDecode(§or0, pmad2, mad_list, swapmad, override); } -int mad_app_read(const mad_ops_t *ops, uint16_t aid, bool swapmad, bool override, - uint8_t *out, size_t max_len, size_t *out_len) { +int mad_app_read(const mad_ops_t *ops, uint16_t aid, bool swapmad, bool override, uint8_t *out, size_t max_len, size_t *out_len) { + *out_len = 0; mad_entry_list_t mad_list = {0}; int res = mad_read_directory(ops, swapmad, override, &mad_list); - if (res != PM3_SUCCESS) + if (res != PM3_SUCCESS) { return res; + } for (size_t i = 0; i < mad_list.len; i++) { - if (mad_list.entries[i].aid != aid) + + if (mad_list.entries[i].aid != aid) { continue; + } uint8_t sno = mad_list.entries[i].sector; uint8_t sector_buf[MFBLOCK_SIZE * 16] = {0}; - res = ops->read_sector(sno, ops->app_key_type, - ops->app_key, sector_buf, ops->verbose); + + res = ops->read_sector(sno, ops->app_key_type, ops->app_key, sector_buf, ops->verbose); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "error reading sector %u", sno); + PrintAndLogEx(ERR, "Error reading sector %u", sno); return res; } uint8_t num_data_blocks = mfNumBlocksPerSector(sno) - 1; size_t nbytes = num_data_blocks * MFBLOCK_SIZE; - if (*out_len + nbytes > max_len) { - PrintAndLogEx(ERR, "output buffer too small"); + if ((*out_len + nbytes) > max_len) { + PrintAndLogEx(ERR, "Output buffer too small"); return PM3_EOVFLOW; } memcpy(out + *out_len, sector_buf, nbytes); *out_len += nbytes; - if (ops->verbose) - PrintAndLogEx(INFO, "read sector %u, %zu data bytes", sno, nbytes); + if (ops->verbose) { + PrintAndLogEx(INFO, "Read sector %u, %zu bytes", sno, nbytes); + } } - if (*out_len == 0 && ops->verbose) - PrintAndLogEx(WARNING, "no sectors found for AID 0x%04X", aid); + if (*out_len == 0 && ops->verbose) { + PrintAndLogEx(WARNING, "No sectors found for AID 0x%04X", aid); + } return PM3_SUCCESS; } -int mad_app_write(const mad_ops_t *ops, uint16_t aid, bool swapmad, bool override, - const uint8_t *data, size_t data_len) { +int mad_app_write(const mad_ops_t *ops, uint16_t aid, bool swapmad, bool override, const uint8_t *data, size_t data_len) { + mad_entry_list_t mad_list = {0}; int res = mad_read_directory(ops, swapmad, override, &mad_list); - if (res != PM3_SUCCESS) + if (res != PM3_SUCCESS) { return res; + } size_t capacity = 0; for (size_t i = 0; i < mad_list.len; i++) { - if (mad_list.entries[i].aid == aid) + if (mad_list.entries[i].aid == aid) { capacity += (mfNumBlocksPerSector(mad_list.entries[i].sector) - 1) * MFBLOCK_SIZE; + } } if (data_len > capacity) { - if (ops->verbose) - PrintAndLogEx(ERR, "data (%zu bytes) exceeds capacity (%zu bytes) for AID 0x%04X", - data_len, capacity, aid); + if (ops->verbose) { + PrintAndLogEx(ERR, "Data (%zu bytes) exceeds capacity (%zu bytes) for AID 0x%04X", data_len, capacity, aid); + } return PM3_EINVARG; } size_t offset = 0; for (size_t i = 0; i < mad_list.len; i++) { - if (mad_list.entries[i].aid != aid) + if (mad_list.entries[i].aid != aid) { continue; - if (offset >= data_len) + } + + if (offset >= data_len) { break; + } uint8_t sno = mad_list.entries[i].sector; uint8_t num_data_blocks = mfNumBlocksPerSector(sno) - 1; @@ -611,45 +668,50 @@ int mad_app_write(const mad_ops_t *ops, uint16_t aid, bool swapmad, bool overrid uint8_t sector_data[MFBLOCK_SIZE * 15] = {0}; size_t to_copy = sector_data_size; - if (data_len - offset < to_copy) + + if (data_len - offset < to_copy) { to_copy = data_len - offset; + } + memcpy(sector_data, data + offset, to_copy); - res = ops->write_sector_data(sno, ops->app_key_type, - ops->app_key, sector_data, ops->verbose); + res = ops->write_sector_data(sno, ops->app_key_type, ops->app_key, sector_data, ops->verbose); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "error writing sector %u", sno); + PrintAndLogEx(ERR, "Error writing sector %u", sno); return res; } - if (ops->verbose) - PrintAndLogEx(INFO, "wrote sector %u, %zu bytes", sno, to_copy); + if (ops->verbose) { + PrintAndLogEx(INFO, "Wrote sector %u, %zu bytes", sno, to_copy); + } offset += sector_data_size; } - PrintAndLogEx(SUCCESS, "wrote %zu bytes to AID 0x%04X", data_len, aid); + PrintAndLogEx(SUCCESS, "Wrote %zu bytes to AID 0x%04X ( %s )", data_len, aid, _GREEN_("ok")); return PM3_SUCCESS; } -int mad_app_verify(const mad_ops_t *ops, uint16_t aid, bool swapmad, bool override, - const uint8_t *expected, size_t expected_len) { +int mad_app_verify(const mad_ops_t *ops, uint16_t aid, bool swapmad, bool override, const uint8_t *expected, size_t expected_len) { + uint8_t readback[MIFARE_4K_MAX_BYTES]; size_t readback_len = 0; - int res = mad_app_read(ops, aid, swapmad, override, - readback, sizeof(readback), &readback_len); - if (res != PM3_SUCCESS) + int res = mad_app_read(ops, aid, swapmad, override, readback, sizeof(readback), &readback_len); + if (res != PM3_SUCCESS) { return res; + } size_t cmp_len = readback_len < expected_len ? readback_len : expected_len; + if (memcmp(expected, readback, cmp_len) != 0) { if (ops->verbose) { - PrintAndLogEx(ERR, "Verify " _RED_("FAILED") ": data mismatch"); + + PrintAndLogEx(ERR, "Data mismatchm, verify ( %s )", _RED_("fail")); + for (size_t j = 0; j < cmp_len; j++) { if (expected[j] != readback[j]) { - PrintAndLogEx(ERR, "first difference at offset %zu: expected %02X, got %02X", - j, expected[j], readback[j]); + PrintAndLogEx(ERR, "first difference at offset %zu: expected %02X, got %02X", j, expected[j], readback[j]); break; } } @@ -658,10 +720,9 @@ int mad_app_verify(const mad_ops_t *ops, uint16_t aid, bool swapmad, bool overri } if (expected_len != readback_len) { - PrintAndLogEx(WARNING, "length mismatch: expected %zu, read %zu (data matches up to shorter)", - expected_len, readback_len); + PrintAndLogEx(WARNING, "length mismatch: expected %zu, read %zu (data matches up to shorter)", expected_len, readback_len); } - PrintAndLogEx(SUCCESS, "Verify " _GREEN_("OK") " (%zu bytes)", cmp_len); + PrintAndLogEx(SUCCESS, "Verified %zu bytes ( %s )", cmp_len, _GREEN_("ok")); return PM3_SUCCESS; } diff --git a/client/src/mifare/mad.h b/client/src/mifare/mad.h index e309abc76..8d018aacb 100644 --- a/client/src/mifare/mad.h +++ b/client/src/mifare/mad.h @@ -78,13 +78,11 @@ typedef struct { typedef struct { // Read a full sector (all blocks including trailer) into buf. // buf must hold mfNumBlocksPerSector(sector_no) * MFBLOCK_SIZE bytes. - int (*read_sector)(uint8_t sector_no, uint8_t key_type, - const uint8_t *key, uint8_t *buf, bool verbose); + int (*read_sector)(uint8_t sector_no, uint8_t key_type, const uint8_t *key, uint8_t *buf, bool verbose); // Write the data blocks (non-trailer) of a sector. // data points to (mfNumBlocksPerSector(sector_no) - 1) * MFBLOCK_SIZE bytes. - int (*write_sector_data)(uint8_t sector_no, uint8_t key_type, - const uint8_t *key, const uint8_t *data, bool verbose); + int (*write_sector_data)(uint8_t sector_no, uint8_t key_type, const uint8_t *key, const uint8_t *data, bool verbose); const uint8_t *mad_key; uint8_t mad_key_type; diff --git a/client/src/mifare/mifare4.c b/client/src/mifare/mifare4.c index 4f20a6602..9c3233bd4 100644 --- a/client/src/mifare/mifare4.c +++ b/client/src/mifare/mifare4.c @@ -221,7 +221,7 @@ int CalculateMAC(mf4Session_t *mf4session, MACType_t mtype, uint8_t blockNum, ui return aes_cmac8(NULL, mf4session->Kmac, macdata, mac, macdatalen); } -int MifareAuth4(mf4Session_t *mf4session, const uint8_t *keyn, uint8_t *key, bool nonfirst, bool activateField, bool leaveSignalON, bool dropFieldIfError, bool verbose, bool silentMode) { +int MifareAuth4(mf4Session_t *mf4session, const uint8_t *keyn, const uint8_t *key, bool nonfirst, bool activateField, bool leaveSignalON, bool dropFieldIfError, bool verbose, bool silentMode) { uint8_t data[257] = {0}; int datalen = 0; @@ -585,7 +585,7 @@ int MFPWriteBlock(mf4Session_t *mf4session, bool plain, bool nomacres, uint8_t b return PM3_SUCCESS; } -int mfpReadSector(uint8_t sectorNo, uint8_t keyType, uint8_t *key, uint8_t *dataout, bool verbose) { +int mfpReadSector(uint8_t sectorNo, uint8_t keyType, const uint8_t *key, uint8_t *dataout, bool verbose) { // 0x4000 is the base for MFP KEY ID's. See datasheet uint16_t uKeyNum = 0x4000 + sectorNo * 2 + (keyType ? 1 : 0); @@ -662,7 +662,7 @@ int mfpReadSector(uint8_t sectorNo, uint8_t keyType, uint8_t *key, uint8_t *data return PM3_SUCCESS; } -int mfpWriteSector(uint8_t sectorNo, uint8_t keyType, uint8_t *key, const uint8_t *datain, bool verbose) { +int mfpWriteSector(uint8_t sectorNo, uint8_t keyType, const uint8_t *key, const uint8_t *datain, bool verbose) { uint16_t uKeyNum = 0x4000 + sectorNo * 2 + (keyType ? 1 : 0); uint8_t keyn[2] = {0}; diff --git a/client/src/mifare/mifare4.h b/client/src/mifare/mifare4.h index 913bd53ef..cc5e8defe 100644 --- a/client/src/mifare/mifare4.h +++ b/client/src/mifare/mifare4.h @@ -60,14 +60,14 @@ void mfpSetVerboseMode(bool verbose); const char *mfpGetErrorDescription(uint8_t errorCode); int CalculateMAC(mf4Session_t *mf4session, MACType_t mtype, uint8_t blockNum, uint8_t blockCount, uint8_t *data, int datalen, uint8_t *mac, bool verbose); -int MifareAuth4(mf4Session_t *mf4session, const uint8_t *keyn, uint8_t *key, bool nonfirst, bool activateField, bool leaveSignalON, bool dropFieldIfError, bool verbose, bool silentMode); +int MifareAuth4(mf4Session_t *mf4session, const uint8_t *keyn, const uint8_t *key, bool nonfirst, bool activateField, bool leaveSignalON, bool dropFieldIfError, bool verbose, bool silentMode); int MFPWritePerso(const uint8_t *keyNum, const uint8_t *key, bool activateField, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen); int MFPCommitPerso(bool activateField, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen); int MFPReadBlock(mf4Session_t *mf4session, bool plain, bool nomaccmd, bool nomacres, uint8_t blockNum, uint8_t blockCount, bool activateField, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen, uint8_t *mac); int MFPWriteBlock(mf4Session_t *mf4session, bool plain, bool nomacres, uint8_t blockNum, uint8_t blockHdr, const uint8_t *data, bool activateField, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen, uint8_t *mac); -int mfpReadSector(uint8_t sectorNo, uint8_t keyType, uint8_t *key, uint8_t *dataout, bool verbose); -int mfpWriteSector(uint8_t sectorNo, uint8_t keyType, uint8_t *key, const uint8_t *datain, bool verbose); +int mfpReadSector(uint8_t sectorNo, uint8_t keyType, const uint8_t *key, uint8_t *dataout, bool verbose); +int mfpWriteSector(uint8_t sectorNo, uint8_t keyType, const uint8_t *key, const uint8_t *datain, bool verbose); int MFPGetSignature(bool activateField, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen); int MFPGetVersion(bool activateField, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen);