From e648438306697f7e93feb5c2465a7e6cc301b7b1 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Wed, 16 Sep 2026 10:58:57 +0200 Subject: [PATCH] hf 14b view: decode MyKey / COGES keys on SRIX4K The SRIX4K block scrambler in cmdhf14b.c was a three entry lookup table with its callers commented out, so `hf 14b valid` printed hard coded values and nothing ever decoded. The scrambler is a 4x4 transpose of the block read as sixteen crumbs. A transpose is its own inverse and it reproduces all three entries of the old table, so the stub is replaced by a working parser. client/src/parsers/parsemykey.c reads the application off a dump: key id, production date, operations counter, vendor code, lock id, current and previous credit, and the eight slot transaction ring. Every block carries a checksum in its top byte and the parser reports how many hold up, which catches a wrong UID or a torn write. The credit blocks are XORed with a session key derived from the UID, the vendor code and the count down counter in block 6, so the file name has to carry the UID. `hf 14b valid` is removed. Checking that the maths holds is now `hf 14b view --selftest`, following `hf mf view --selftest`, and it runs against traces/hf-14b-D0021F673CB26556-dump.json, a dump of a real reset key. Co-Authored-By: Claude Opus 5 (1M context) --- CHANGELOG.md | 3 + client/CMakeLists.txt | 1 + client/Makefile | 1 + client/experimental_lib/CMakeLists.txt | 1 + client/src/cmdhf14b.c | 172 ++-------- client/src/parsers/parsemykey.c | 419 +++++++++++++++++++++++ client/src/parsers/parsemykey.h | 32 ++ traces/README.md | 17 + traces/hf-14b-D0021F673CB26556-dump.json | 135 ++++++++ 9 files changed, 630 insertions(+), 151 deletions(-) create mode 100644 client/src/parsers/parsemykey.c create mode 100644 client/src/parsers/parsemykey.h create mode 100644 traces/hf-14b-D0021F673CB26556-dump.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 289682230..1eb276a0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Added `hf 14b view` - MyKey / COGES keys on SRIX4K now decode (@iceman1001) +- Added `hf 14b view --selftest` - runs the MyKey parser self tests (@iceman1001) +- Removed `hf 14b valid` - the SRIX4K scrambler stub it wrapped is finished and lives in the MyKey parser (@iceman1001) - Fixed build too large for 256kb images by guarding WITH_DESFIRE_SIM (@iceman1001) - Fixed `pm3 flash` - the progress bar no longer keeps SIGINT after it finishes, CTRL-C works again afterwards (@iceman1001) - Fixed `CTRL-C` / `CTRL-Z` - the client owns its signals instead of letting readline re-raise them, CTRL-C quits cleanly saving the history and CTRL-Z suspends reliably (@iceman1001) diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index d420dbfe9..fa73ebd35 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -512,6 +512,7 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/parsers/parsehid.c ${PM3_ROOT}/client/src/parsers/parsevigik.c ${PM3_ROOT}/client/src/parsers/parsehexact.c + ${PM3_ROOT}/client/src/parsers/parsemykey.c ${PM3_ROOT}/client/src/parsers/parseproac.c ${PM3_ROOT}/client/src/parsers/hrtparser/hrtparser.c ${PM3_ROOT}/client/src/pla.c diff --git a/client/Makefile b/client/Makefile index d011eb36f..9002ea8c9 100644 --- a/client/Makefile +++ b/client/Makefile @@ -910,6 +910,7 @@ SRCS = mifare/aiddesfire.c \ parsers/parsehid.c \ parsers/parsevigik.c \ parsers/parsehexact.c \ + parsers/parsemykey.c \ parsers/parseproac.c \ parsers/hrtparser/hrtparser.c \ pm3.c \ diff --git a/client/experimental_lib/CMakeLists.txt b/client/experimental_lib/CMakeLists.txt index 4a224c812..2b5f21330 100644 --- a/client/experimental_lib/CMakeLists.txt +++ b/client/experimental_lib/CMakeLists.txt @@ -439,6 +439,7 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/parsers/parsehid.c ${PM3_ROOT}/client/src/parsers/parsevigik.c ${PM3_ROOT}/client/src/parsers/parsehexact.c + ${PM3_ROOT}/client/src/parsers/parsemykey.c ${PM3_ROOT}/client/src/parsers/parseproac.c ${PM3_ROOT}/client/src/parsers/hrtparser/hrtparser.c ${PM3_ROOT}/client/src/pla.c diff --git a/client/src/cmdhf14b.c b/client/src/cmdhf14b.c index 2f4432e6f..140e6a7cf 100644 --- a/client/src/cmdhf14b.c +++ b/client/src/cmdhf14b.c @@ -37,6 +37,7 @@ #include "cmdhf.h" // handle HF plot #include "atrs.h" // atqbToEmulatedAtr #include "pla.h" // ECP parsing +#include "parsers/parsemykey.h" // MyKey / COGES #define MAX_14B_TIMEOUT_MS (4949U) @@ -2619,153 +2620,6 @@ static int CmdHF14BRestore(const char *Cmd) { return PM3_SUCCESS; } -/* - -static uint32_t srix4kEncode(uint32_t value) { - // vv = value - // pp = position - // vv vv vv pp - // 4 bytes : 00 1A 20 01 - // only the lower crumbs. - uint8_t block = (value & 0xFF); - uint8_t i = 0; - uint8_t valuebytes[] = {0, 0, 0}; - Uint3byteToMemBe(valuebytes, value >> 8); - - uint32_t value_x = (value >> 8); - PrintAndLogEx(INFO, "value...... %08x %06x", value, value_x); - PrintAndLogEx(INFO, "3b value... %s", sprint_hex_inrow(valuebytes, sizeof(valuebytes))); - PrintAndLogEx(INFO, "block no... %02x", block); - - // Scrambled part - // Crumb swapping of value. - uint32_t foo = 0; - foo |= CRUMB(value_x, 22) << 28; - foo |= CRUMB(value_x, 14) << 26; - foo |= CRUMB(value_x, 6) << 24; - - foo |= CRUMB(value_x, 20) << 20; - foo |= CRUMB(value_x, 12) << 18; - foo |= CRUMB(value_x, 4) << 16; - - foo |= CRUMB(value_x, 18) << 12; - foo |= CRUMB(value_x, 10) << 10; - foo |= CRUMB(value_x, 2) << 8; - - foo |= CRUMB(value_x, 16) << 4; - foo |= CRUMB(value_x, 8) << 2; - foo |= CRUMB(value_x, 0) << 0; - - PrintAndLogEx(INFO, "hex........ %02x %02x %02x", CRUMB(value_x, 22), CRUMB(value_x, 14), CRUMB(value_x, 6)); - PrintAndLogEx(INFO, "hex........ %02x %02x %02x", CRUMB(value_x, 20), CRUMB(value_x, 12), CRUMB(value_x, 4)); - PrintAndLogEx(INFO, "hex........ %02x %02x %02x", CRUMB(value_x, 18), CRUMB(value_x, 10), CRUMB(value_x, 2)); - PrintAndLogEx(INFO, "hex........ %02x %02x %02x", CRUMB(value_x, 16), CRUMB(value_x, 8), CRUMB(value_x, 0)); - - PrintAndLogEx(INFO, "hex........ %08x", foo); - - // chksum part - uint32_t chksum = 0xFF - block; - - // chksum is reduced by each nibbles of value. - for (i = 0; i < 3; ++i) { - chksum -= NIBBLE_HIGH(valuebytes[i]); - chksum -= NIBBLE_LOW(valuebytes[i]); - } - - // base4 conversion and left shift twice - i = 3; - uint8_t base4[] = {0, 0, 0, 0}; - while (chksum != 0) { - base4[i--] = (chksum % 4 << 2); - chksum /= 4; - } - PrintAndLogEx(INFO, "%s", sprint_hex_inrow(base4, sizeof(base4))); - - // merge scambled and chksum parts - uint32_t encvalue = 0; - - (NIBBLE_LOW(base4[0]) << 28) | - (NIBBLE_HIGH(temp[0]) << 24) | - - (NIBBLE_LOW(base4[1]) << 20) | - (NIBBLE_LOW(temp[0]) << 16) | - - (NIBBLE_LOW(base4[2]) << 12) | - (NIBBLE_HIGH(temp[1]) << 8) | - - (NIBBLE_LOW(base4[3]) << 4) | - NIBBLE_LOW(temp[1]); - - PrintAndLogEx(NORMAL, "ICE encoded | %08X -> %08X", value, encvalue); - return encvalue; -} -*/ - -static uint32_t srix4k_decode_counter(uint32_t num) { - uint32_t value = ~num; - ++value; - return value; -} -/* -static uint32_t srix4kDecode(uint32_t value) { - switch (value) { - case 0xC04F42C5: - return 0x003139; - case 0xC1484807: - return 0x002943; - case 0xC0C60848: - return 0x001A20; - } - return 0; -} -*/ - -static uint32_t srix4k_get_magicbytes(uint64_t uid, uint32_t block6, uint32_t block18, uint32_t block19) { -#define MASK 0xFFFFFFFF; - uint32_t uid32 = uid & MASK; - uint32_t counter = srix4k_decode_counter(block6); - uint32_t decodedBlock18 = 0; - uint32_t decodedBlock19 = 0; -// uint32_t decodedBlock18 = srix4kDecode(block18); -// uint32_t decodedBlock19 = srix4kDecode(block19); - uint32_t doubleBlock = (decodedBlock18 << 16 | decodedBlock19) + 1; - - uint32_t result = (uid32 * doubleBlock * counter) & MASK; - PrintAndLogEx(SUCCESS, "Magic bytes | %08X", result); - return result; -} - -static int CmdSRIX4kValid(const char *Cmd) { - CLIParserContext *ctx; - CLIParserInit(&ctx, "hf 14b valid", - "SRIX checksum test", - "hf 14b valid\n" - ); - - void *argtable[] = { - arg_param_begin, - arg_param_end - }; - CLIExecWithReturn(ctx, Cmd, argtable, false); - CLIParserFree(ctx); - - uint64_t uid = 0xD00202501A4532F9; - uint32_t block6 = 0xFFFFFFFF; - uint32_t block18 = 0xC04F42C5; - uint32_t block19 = 0xC1484807; - uint32_t block21 = 0xD1BCABA4; - - uint32_t test_b18 = 0x001A2001; // 0x00313918; - uint32_t test_b18_enc = 0; - // uint32_t test_b18_enc = srix4kEncode(test_b18); - // uint32_t test_b18_dec = srix4kDecode(test_b18_enc); - PrintAndLogEx(SUCCESS, "ENCODE & CHECKSUM | %08X -> %08X (%s)", test_b18, test_b18_enc, ""); - - uint32_t magic = srix4k_get_magicbytes(uid, block6, block18, block19); - PrintAndLogEx(SUCCESS, "BLOCK 21 | %08X -> %08X (no XOR)", block21, magic ^ block21); - return PM3_SUCCESS; -} - int select_card_14443b_4(bool disconnect, iso14b_card_select_t *card) { if (card) { memset(card, 0, sizeof(iso14b_card_select_t)); @@ -3471,13 +3325,15 @@ static int CmdHF14BView(const char *Cmd) { "note:\n" " - command expects the filename to contain a UID\n" " which is needed to determine card memory type", - "hf 14b view -f hf-14b-01020304-dump.bin" + "hf 14b view -f hf-14b-01020304-dump.bin\n" + "hf 14b view --selftest" ); void *argtable[] = { arg_param_begin, - arg_str1("f", "file", "", "Specify a filename for dump file"), + arg_str0("f", "file", "", "Specify a filename for dump file"), arg_lit0("v", "verbose", "verbose output"), arg_lit0("z", "dense", "dense dump output style"), + arg_lit0(NULL, "selftest", "Run the dump parsers self tests and exit"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, false); @@ -3486,8 +3342,18 @@ static int CmdHF14BView(const char *Cmd) { CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)filename, FILE_PATH_SIZE, &fnlen); bool verbose = arg_get_lit(ctx, 2); bool dense_output = (g_session.dense_output || arg_get_lit(ctx, 3)); + bool selftest = arg_get_lit(ctx, 4); CLIParserFree(ctx); + if (selftest) { + return mykey_selftest(); + } + + if (fnlen == 0) { + PrintAndLogEx(ERR, "Specify a filename with `" _YELLOW_("-f") "` or run `" _YELLOW_("--selftest") "`"); + return PM3_EINVARG; + } + // read dump file uint8_t *dump = NULL; size_t bytes_read = (ST25TB_SR_BLOCK_SIZE * 0x100); @@ -3504,7 +3370,12 @@ static int CmdHF14BView(const char *Cmd) { // figure out a way to identify the different dump files. // STD/SR/CT is difference - print_sr_blocks(dump, bytes_read, get_uid_from_filename(filename), dense_output); + const uint8_t *uid = get_uid_from_filename(filename); + print_sr_blocks(dump, bytes_read, uid, dense_output); + + if (is_valid_mykey_card(dump, bytes_read)) { + (void)mykey_parser_parse(dump, bytes_read, uid); + } //print_std_blocks(dump, bytes_read); //print_ct_blocks(dump, bytes_read); @@ -3739,7 +3610,6 @@ static command_t CommandTable[] = { {"wrbl", CmdHF14BSriWrbl, IfPm3Iso14443b, "Write data to a SRI512/SRIX4 tag"}, {"tearoff", CmdHF14BSriTearoff, IfPm3Iso14443b, "Tear-off attack on ST25TB/SRx counter blocks"}, {"view", CmdHF14BView, AlwaysAvailable, "Display content from tag dump file"}, - {"valid", CmdSRIX4kValid, AlwaysAvailable, "SRIX4 checksum test"}, {"---------", CmdHelp, IfPm3Iso14443b, "------------------ " _CYAN_("ASK CTS / C-ticket") " ------------------"}, {"ctdump", CmdHF14BCtsDump, IfPm3Iso14443b, "Dump ASK CTS/C-ticket"}, {"ctrdbl", CmdHF14BCtRdBl, IfPm3Iso14443b, "Read ASK CTS/C-ticket block"}, diff --git a/client/src/parsers/parsemykey.c b/client/src/parsers/parsemykey.c new file mode 100644 index 000000000..f1c50a8fb --- /dev/null +++ b/client/src/parsers/parsemykey.c @@ -0,0 +1,419 @@ +//----------------------------------------------------------------------------- +// 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. +//----------------------------------------------------------------------------- +// MyKey / COGES parser for SRIX4K - ST25TB04K dumps +//----------------------------------------------------------------------------- + +#include "parsemykey.h" + +#include "commonutil.h" // ARRAYLEN, MemBeToUint4byte, MemLeToUint4byte +#include "ui.h" // PrintAndLogEx + +// Blocks below 0x10 are written by the chip maker and the key issuer +#define MYKEY_BLK_LOCKID 0x05 // lock id marker in the low byte +#define MYKEY_BLK_OTP 0x06 // count down counter, feeds the session key +#define MYKEY_BLK_KEYID 0x07 // key id, low three bytes +#define MYKEY_BLK_PRODUCED 0x08 // production date, BCD + +// The application itself starts at 0x10 and keeps most fields in duplicate +#define MYKEY_BLK_APP_FIRST 0x10 +#define MYKEY_BLK_ELAPSED 0x10 // key id top byte + days since 1995-01-01 +#define MYKEY_BLK_KEYID_A 0x11 // mirror of block 7 +#define MYKEY_BLK_OPS 0x12 // operations counter +#define MYKEY_BLK_KEYID_B 0x15 // mirror of block 7 +#define MYKEY_BLK_VENDOR_HI 0x18 +#define MYKEY_BLK_VENDOR_LO 0x19 +#define MYKEY_BLK_CREDIT_A 0x21 // current credit, XORed with the session key +#define MYKEY_BLK_PREV_A 0x23 // credit before the last transaction +#define MYKEY_BLK_CREDIT_B 0x25 +#define MYKEY_BLK_PREV_B 0x27 +#define MYKEY_BLK_TXN_FIRST 0x34 // eight slot transaction ring +#define MYKEY_BLK_TXN_CNT 8 +#define MYKEY_BLK_TXN_PTR 0x3C + +// Vendor code a key carries when it is not bound to any vendor +#define MYKEY_VENDOR_RESET 0xFEDC0123 +#define MYKEY_LOCKID_MARK 0x7F +#define MYKEY_BLANK 0xFFFFFFFF + +static uint8_t mykey_bcd(uint8_t v) { + return (NIBBLE_HIGH(v) * 10) + NIBBLE_LOW(v); +} + +static uint32_t mykey_scramble(uint32_t block) { + uint32_t res = 0; + for (uint8_t i = 0; i < 16; i++) { + uint8_t dst = ((i % 4) * 4) + (i / 4); + res |= (uint32_t)CRUMB(block, i * 2) << (dst * 2); + } + return res; +} + +static uint8_t mykey_crc(uint32_t block, uint8_t blockno) { + uint8_t crc = 0xFF - blockno; + for (uint8_t i = 0; i < 24; i += 4) { + crc -= NIBBLE_LOW(block >> i); + } + return crc; +} + +static bool mykey_crc_ok(uint32_t block, uint8_t blockno) { + return (mykey_crc(block, blockno) == (block >> 24)); +} + +static uint32_t mykey_block(const uint8_t *dump, uint8_t blockno) { + return MemBeToUint4byte(dump + (blockno * MYKEY_BLOCK_SIZE)); +} + +static uint32_t mykey_vendor(const uint8_t *dump) { + uint32_t hi = mykey_scramble(mykey_block(dump, MYKEY_BLK_VENDOR_HI)); + uint32_t lo = mykey_scramble(mykey_block(dump, MYKEY_BLK_VENDOR_LO)); + return ((hi & 0xFFFF) << 16) | (lo & 0xFFFF); +} + +static uint32_t mykey_session_key(const uint8_t *dump, const uint8_t *uid) { + uint32_t otp = ~BSWAP_32(mykey_block(dump, MYKEY_BLK_OTP)) + 1; + return MemLeToUint4byte(uid) * (mykey_vendor(dump) + 1) * otp; +} + +bool is_valid_mykey_card(const uint8_t *dump, size_t dumplen) { + + if (dump == NULL || dumplen < MYKEY_BYTES) { + return false; + } + + uint32_t keyid = mykey_block(dump, MYKEY_BLK_KEYID); + if (keyid == MYKEY_BLANK || keyid == 0) { + return false; + } + + const uint8_t mirrors[2] = { MYKEY_BLK_KEYID_A, MYKEY_BLK_KEYID_B }; + + for (uint8_t i = 0; i < ARRAYLEN(mirrors); i++) { + + uint32_t b = mykey_block(dump, mirrors[i]); + if ((b & 0x00FFFFFF) != (keyid & 0x00FFFFFF)) { + return false; + } + + if (mykey_crc_ok(b, mirrors[i]) == false) { + return false; + } + } + return true; +} + +static int32_t mykey_credit(const uint8_t *dump, uint8_t blockno, uint32_t sk) { + uint32_t v = mykey_scramble(mykey_block(dump, blockno) ^ sk); + if (mykey_crc_ok(v, blockno) == false) { + return PM3_ECRC; + } + return (int32_t)(v & 0xFFFF); +} + +static uint16_t mykey_check_blocks(const uint8_t *dump, uint32_t sk, uint16_t *total) { + + uint16_t ok = 0; + *total = 0; + + uint32_t keymask = mykey_block(dump, MYKEY_BLK_KEYID) & 0x00FFFFFF; + + for (uint16_t i = MYKEY_BLK_APP_FIRST; i < MYKEY_NUM_BLOCKS; i++) { + + uint32_t b = mykey_block(dump, i); + if (b == MYKEY_BLANK) { + continue; + } + + if ((i >= MYKEY_BLK_TXN_FIRST) && (i < (MYKEY_BLK_TXN_FIRST + MYKEY_BLK_TXN_CNT))) { + continue; + } + + (*total)++; + + if (mykey_crc_ok(b, i) || + mykey_crc_ok(mykey_scramble(b), i) || + mykey_crc_ok(mykey_scramble(b ^ sk), i) || + mykey_crc_ok(mykey_scramble(b ^ keymask), i)) { + ok++; + } + } + return ok; +} + +static void mykey_print_produced(uint32_t blk) { + + uint8_t day = mykey_bcd((blk >> 24) & 0xFF); + uint8_t month = mykey_bcd((blk >> 16) & 0xFF); + uint16_t year = (mykey_bcd(SWAP_NIBBLE(blk & 0xFF)) * 100) + mykey_bcd((blk >> 8) & 0xFF); + + PrintAndLogEx(INFO, "Produced........... " _YELLOW_("%04u-%02u-%02u"), year, month, day); +} + +static void mykey_print_transactions(const uint8_t *dump) { + + uint32_t ptrblk = mykey_block(dump, MYKEY_BLK_TXN_PTR); + int32_t ptr = -1; + + if (ptrblk != MYKEY_BLANK) { + + uint32_t v = mykey_scramble(ptrblk ^ (mykey_block(dump, MYKEY_BLK_KEYID) & 0x00FFFFFF)); + if (mykey_crc_ok(v, MYKEY_BLK_TXN_PTR)) { + ptr = (int32_t)((v >> 16) & 0xFF); + } + + if (ptr > (MYKEY_BLK_TXN_CNT - 1)) { + ptr = -1; + } + } + + uint8_t used = 0; + for (uint8_t i = 0; i < MYKEY_BLK_TXN_CNT; i++) { + if (mykey_block(dump, MYKEY_BLK_TXN_FIRST + i) != MYKEY_BLANK) { + used++; + } + } + + if (used == 0) { + PrintAndLogEx(INFO, "Transactions....... " _YELLOW_("none")); + return; + } + + PrintAndLogEx(INFO, "Transactions....... %u of %u slots used", used, MYKEY_BLK_TXN_CNT); + + for (uint8_t i = 0; i < MYKEY_BLK_TXN_CNT; i++) { + + uint32_t t = mykey_block(dump, MYKEY_BLK_TXN_FIRST + i); + if (t == MYKEY_BLANK) { + continue; + } + + PrintAndLogEx(INFO, " %s slot %u........ %04u-%02u-%02u " _GREEN_("%u.%02u") " EUR", + (i == ptr) ? ">" : " ", + i, + 2000 + ((t >> 16) & 0x7F), + NIBBLE_LOW(t >> 23), + (t >> 27) & 0x1F, + (t & 0xFFFF) / 100, + (t & 0xFFFF) % 100 + ); + } +} + +int mykey_parser_parse(const uint8_t *dump, size_t dumplen, const uint8_t *uid) { + + if (is_valid_mykey_card(dump, dumplen) == false) { + return PM3_EINVARG; + } + + PrintAndLogEx(NORMAL, ""); + PrintAndLogEx(INFO, "--- " _CYAN_("MyKey / COGES") " ------------------------"); + + PrintAndLogEx(INFO, "Key ID............. " _YELLOW_("%06X"), mykey_block(dump, MYKEY_BLK_KEYID) & 0x00FFFFFF); + mykey_print_produced(mykey_block(dump, MYKEY_BLK_PRODUCED)); + + uint32_t elapsed = mykey_block(dump, MYKEY_BLK_ELAPSED) & 0xFFFF; + if (elapsed) { + PrintAndLogEx(INFO, "Days since 1995.... %04X", elapsed); + } + + uint32_t ops = mykey_block(dump, MYKEY_BLK_OPS); + if (mykey_crc_ok(ops, MYKEY_BLK_OPS)) { + PrintAndLogEx(INFO, "Operations......... " _YELLOW_("%u"), ops & 0x00FFFFFF); + } + + uint32_t vendor = mykey_vendor(dump); + if (vendor == MYKEY_VENDOR_RESET) { + PrintAndLogEx(INFO, "Vendor............. " _YELLOW_("reset") ", not bound to a vendor"); + } else { + PrintAndLogEx(INFO, "Vendor............. " _YELLOW_("%08X"), vendor); + } + + bool lockid = ((mykey_block(dump, MYKEY_BLK_LOCKID) & 0xFF) == MYKEY_LOCKID_MARK); + PrintAndLogEx(INFO, "Lock ID............ %s", lockid ? _RED_("yes") : _GREEN_("no")); + + bool have_uid = false; + for (uint8_t i = 0; (uid != NULL) && (i < 8); i++) { + if (uid[i]) { + have_uid = true; + break; + } + } + + if (have_uid == false) { + PrintAndLogEx(INFO, "Session key........ " _YELLOW_("unknown") ", UID needed to decrypt the credit"); + PrintAndLogEx(INFO, "Credit............. %08X " _YELLOW_("still encrypted"), mykey_block(dump, MYKEY_BLK_CREDIT_A)); + mykey_print_transactions(dump); + return PM3_SUCCESS; + } + + uint32_t sk = mykey_session_key(dump, uid); + PrintAndLogEx(INFO, "Session key........ " _YELLOW_("%08X"), sk); + + int32_t credit = mykey_credit(dump, MYKEY_BLK_CREDIT_A, sk); + int32_t copy = mykey_credit(dump, MYKEY_BLK_CREDIT_B, sk); + + if (credit == PM3_ECRC) { + PrintAndLogEx(INFO, "Credit............. " _RED_("checksum failed") ", wrong UID or unknown vendor"); + } else { + PrintAndLogEx(INFO, "Credit............. " _GREEN_("%u.%02u") " EUR", credit / 100, credit % 100); + if (copy != credit) { + PrintAndLogEx(INFO, " block %02X disagrees, a torn write", MYKEY_BLK_CREDIT_B); + } + } + + // The previous credit is scrambled but not XORed with the session key + int32_t prev = mykey_credit(dump, MYKEY_BLK_PREV_A, 0); + int32_t prev_copy = mykey_credit(dump, MYKEY_BLK_PREV_B, 0); + + if (prev != PM3_ECRC) { + PrintAndLogEx(INFO, "Previous credit.... " _GREEN_("%u.%02u") " EUR", prev / 100, prev % 100); + if (prev_copy != prev) { + PrintAndLogEx(INFO, " block %02X disagrees, a torn write", MYKEY_BLK_PREV_B); + } + } + + if (lockid && (credit == PM3_ECRC)) { + PrintAndLogEx(WARNING, "Lock id set and the credit does not check out"); + } + + mykey_print_transactions(dump); + + uint16_t total = 0; + uint16_t ok = mykey_check_blocks(dump, sk, &total); + PrintAndLogEx(INFO, "Block checksums.... %u / %u ( %s )" + , ok + , total + , (ok == total) ? _GREEN_("ok") : _RED_("fail") + ); + + return PM3_SUCCESS; +} + +// A reset key read off an ST25TB04K. Every written block checksums, the vendor +// is the reset code and the credit is zero. +static const uint8_t mykey_selftest_uid[8] = { 0x56, 0x65, 0xB2, 0x3C, 0x67, 0x1F, 0x02, 0xD0 }; + +static const uint8_t mykey_selftest_dump[MYKEY_BYTES] = { + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0x64, 0x95, 0x43, 0x19, 0x27, 0x10, 0x21, 0x02, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xE5, 0x64, 0x00, 0x00, 0xCF, 0x95, 0x43, 0x19, + 0xEC, 0x00, 0x00, 0x01, 0xE4, 0x04, 0x00, 0x13, 0xE1, 0x64, 0x00, 0x00, + 0xCB, 0x95, 0x43, 0x19, 0xE8, 0x00, 0x00, 0x01, 0xE0, 0x04, 0x00, 0x13, + 0x8F, 0xCD, 0x0F, 0x48, 0xC0, 0x82, 0x00, 0x07, 0xC0, 0x80, 0x40, 0x40, + 0xC0, 0x80, 0x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, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +}; + +static const struct { + uint32_t enc; + uint32_t dec; +} mykey_selftest_vectors[] = { + { 0xC04F42C5, 0xD7003139 }, + { 0xC1484807, 0xD4002943 }, + { 0xC0C60848, 0xF1001A20 }, +}; + +int mykey_selftest(void) { + + PrintAndLogEx(INFO, "Testing MyKey block scrambler and session key"); + + for (uint8_t i = 0; i < ARRAYLEN(mykey_selftest_vectors); i++) { + + uint32_t enc = mykey_selftest_vectors[i].enc; + uint32_t dec = mykey_selftest_vectors[i].dec; + + if (mykey_scramble(enc) != dec) { + PrintAndLogEx(FAILED, " %08X descrambled to %08X, expected %08X ( " _RED_("fail") " )" + , enc + , mykey_scramble(enc) + , dec + ); + return PM3_ESOFT; + } + + if (mykey_scramble(dec) != enc) { + PrintAndLogEx(FAILED, " scrambler is not its own inverse on %08X ( " _RED_("fail") " )", dec); + return PM3_ESOFT; + } + } + PrintAndLogEx(INFO, " scrambler.......... %u vectors, self inverse ( " _GREEN_("ok") " )", (unsigned)ARRAYLEN(mykey_selftest_vectors)); + + if (is_valid_mykey_card(mykey_selftest_dump, sizeof(mykey_selftest_dump)) == false) { + PrintAndLogEx(FAILED, " known good dump not recognised ( " _RED_("fail") " )"); + return PM3_ESOFT; + } + + uint32_t vendor = mykey_vendor(mykey_selftest_dump); + if (vendor != MYKEY_VENDOR_RESET) { + PrintAndLogEx(FAILED, " vendor %08X, expected the reset code %08X ( " _RED_("fail") " )", vendor, MYKEY_VENDOR_RESET); + return PM3_ESOFT; + } + + uint32_t sk = mykey_session_key(mykey_selftest_dump, mykey_selftest_uid); + PrintAndLogEx(INFO, " session key........ %08X", sk); + + int32_t credit = mykey_credit(mykey_selftest_dump, MYKEY_BLK_CREDIT_A, sk); + if (credit != 0) { + PrintAndLogEx(FAILED, " credit read back as %d, expected 0 ( " _RED_("fail") " )", credit); + return PM3_ESOFT; + } + + uint16_t total = 0; + uint16_t ok = mykey_check_blocks(mykey_selftest_dump, sk, &total); + if (ok != total || total == 0) { + PrintAndLogEx(FAILED, " %u of %u block checksums ( " _RED_("fail") " )", ok, total); + return PM3_ESOFT; + } + PrintAndLogEx(INFO, " block checksums.... %u / %u ( " _GREEN_("ok") " )", ok, total); + + PrintAndLogEx(SUCCESS, "MyKey parser selftest ( " _GREEN_("ok") " )"); + return PM3_SUCCESS; +} diff --git a/client/src/parsers/parsemykey.h b/client/src/parsers/parsemykey.h new file mode 100644 index 000000000..68c174bb3 --- /dev/null +++ b/client/src/parsers/parsemykey.h @@ -0,0 +1,32 @@ +//----------------------------------------------------------------------------- +// 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. +//----------------------------------------------------------------------------- +// MyKey / COGES parser for SRIX4K - ST25TB04K dumps +//----------------------------------------------------------------------------- + +#ifndef PARSEMYKEY_H__ +#define PARSEMYKEY_H__ + +#include "common.h" + +#define MYKEY_BLOCK_SIZE 4 +#define MYKEY_NUM_BLOCKS 128 +#define MYKEY_BYTES (MYKEY_NUM_BLOCKS * MYKEY_BLOCK_SIZE) + +bool is_valid_mykey_card(const uint8_t *dump, size_t dumplen); +int mykey_parser_parse(const uint8_t *dump, size_t dumplen, const uint8_t *uid); +int mykey_selftest(void); + +#endif diff --git a/traces/README.md b/traces/README.md index effbd4201..80f243fa1 100644 --- a/traces/README.md +++ b/traces/README.md @@ -190,3 +190,20 @@ the key files are for reading or rebuilding the physical card. hf mfdes eload -f traces/mifare/hf-mfdes-ev1-8k-test.json hf mfdes sim ``` + +## ST25TB card images + +`14b v2` dump format, 128 EEPROM blocks of four bytes followed by the system +block. The file name has to carry the UID, `hf 14b view` reads it from there to +work out the chip type and, for MyKey, to derive the session key. + +|filename|description| +|--------|-----------| +|hf-14b-D0021F673CB26556-dump.json |ST25TB04K carrying a COGES v3 MyKey, reset state: no vendor bound, zero credit, empty transaction ring. Blocks 7 and 8 are write protected| + +``` +hf 14b view -f traces/hf-14b-D0021F673CB26556-dump.json +``` + +This is the vector behind `hf 14b view --selftest`, which checks the block +scrambler, the session key and all 48 block checksums against it. diff --git a/traces/hf-14b-D0021F673CB26556-dump.json b/traces/hf-14b-D0021F673CB26556-dump.json new file mode 100644 index 000000000..bfccaef65 --- /dev/null +++ b/traces/hf-14b-D0021F673CB26556-dump.json @@ -0,0 +1,135 @@ +{ + "Created": "proxmark3", + "FileType": "14b v2", + "blocks": { + "0": "FFFFFFFF", + "1": "FFFFFFFF", + "2": "FFFFFFFF", + "3": "FFFFFFFF", + "4": "FFFFFFFF", + "5": "FEFFFFFF", + "6": "FFFFFFFF", + "7": "64954319", + "8": "27102102", + "9": "FFFFFFFF", + "10": "FFFFFFFF", + "11": "FFFFFFFF", + "12": "FFFFFFFF", + "13": "FFFFFFFF", + "14": "FFFFFFFF", + "15": "FFFFFFFF", + "16": "E5640000", + "17": "CF954319", + "18": "EC000001", + "19": "E4040013", + "20": "E1640000", + "21": "CB954319", + "22": "E8000001", + "23": "E0040013", + "24": "8FCD0F48", + "25": "C0820007", + "26": "C0804040", + "27": "C0804000", + "28": "8F8DCF48", + "29": "C042C007", + "30": "C0800040", + "31": "C0800000", + "32": "C040C090", + "33": "65235698", + "34": "C0660113", + "35": "C040C000", + "36": "C0408090", + "37": "65231698", + "38": "C026C113", + "39": "C0408000", + "40": "FFFFFFFF", + "41": "FFFFFFFF", + "42": "FFFFFFFF", + "43": "FFFFFFFF", + "44": "FFFFFFFF", + "45": "FFFFFFFF", + "46": "FFFFFFFF", + "47": "FFFFFFFF", + "48": "FFFFFFFF", + "49": "FFFFFFFF", + "50": "FFFFFFFF", + "51": "FFFFFFFF", + "52": "FFFFFFFF", + "53": "FFFFFFFF", + "54": "FFFFFFFF", + "55": "FFFFFFFF", + "56": "FFFFFFFF", + "57": "FFFFFFFF", + "58": "FFFFFFFF", + "59": "FFFFFFFF", + "60": "FFFFFFFF", + "61": "FFFFFFFF", + "62": "FFFFFFFF", + "63": "B6640000", + "64": "A0954319", + "65": "BD000001", + "66": "B5040013", + "67": "B2640000", + "68": "9C954319", + "69": "B9000001", + "70": "B1040013", + "71": "8F0D0F88", + "72": "80C20047", + "73": "80C04080", + "74": "80C04040", + "75": "4FCDCF88", + "76": "8082C047", + "77": "80C00080", + "78": "80C00040", + "79": "8080C0D0", + "80": "8080C0C0", + "81": "80A60153", + "82": "8080C040", + "83": "808080D0", + "84": "808080C0", + "85": "8066C153", + "86": "80808040", + "87": "FFFFFFFF", + "88": "FFFFFFFF", + "89": "FFFFFFFF", + "90": "FFFFFFFF", + "91": "FFFFFFFF", + "92": "FFFFFFFF", + "93": "FFFFFFFF", + "94": "FFFFFFFF", + "95": "FFFFFFFF", + "96": "FFFFFFFF", + "97": "FFFFFFFF", + "98": "FFFFFFFF", + "99": "FFFFFFFF", + "100": "FFFFFFFF", + "101": "FFFFFFFF", + "102": "FFFFFFFF", + "103": "FFFFFFFF", + "104": "FFFFFFFF", + "105": "FFFFFFFF", + "106": "FFFFFFFF", + "107": "FFFFFFFF", + "108": "FFFFFFFF", + "109": "FFFFFFFF", + "110": "FFFFFFFF", + "111": "FFFFFFFF", + "112": "FFFFFFFF", + "113": "FFFFFFFF", + "114": "FFFFFFFF", + "115": "FFFFFFFF", + "116": "FFFFFFFF", + "117": "FFFFFFFF", + "118": "FFFFFFFF", + "119": "FFFFFFFF", + "120": "FFFFFFFF", + "121": "FFFFFFFF", + "122": "FFFFFFFF", + "123": "FFFFFFFF", + "124": "FFFFFFFF", + "125": "FFFFFFFF", + "126": "FFFFFFFF", + "127": "FFFFFFFF", + "128": "FFFFFFFE" + } +}