From 7142ddc9bd55069cf25ebdb58de9478e7fe0006f Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sun, 13 Sep 2026 12:55:43 +0200 Subject: [PATCH 1/2] added hexact parser --- CHANGELOG.md | 4 +- client/CMakeLists.txt | 1 + client/Makefile | 1 + client/experimental_lib/CMakeLists.txt | 1 + client/src/cmdhfmf.c | 21 +- client/src/parsers/parsehexact.c | 121 ++++++ client/src/parsers/parsehexact.h | 30 ++ client/src/parsers/parsevigik.c | 532 +++++++++++++------------ client/src/parsers/parsevigik.h | 3 + tools/pm3_tests.sh | 1 + 10 files changed, 467 insertions(+), 248 deletions(-) create mode 100644 client/src/parsers/parsehexact.c create mode 100644 client/src/parsers/parsehexact.h diff --git a/CHANGELOG.md b/CHANGELOG.md index 5393fd056..fed806f56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,8 +3,8 @@ 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] -- Fixed `-f ` - a filename given with its own extension is now used as-is on both load and save, instead of always getting `.bin` / `.json` appended (@iceman1001) -- Fixed `hf mf view` - VIGIK signature verification was a heuristic that counted zero bytes; it now recovers the ISO 9796-1 message and checks it against the card, catching a modified UID that the old check passed (@iceman1001) +- Fixed `-f ` - a filename given with its own extension is now used as-is on both load and save (@iceman1001) +- Fixed `hf mf view` - VIGIK signature verification now recovers the ISO 9796-1 message and checks it against the card (@iceman1001) - Added `hf mf view --selftest` - recovers and checks the signature of a known good expired VIGIK card (@iceman1001) - Changed `hf mf view` / `hf mf dump --ns` - identify VIGIK family systems (@iceman1001) - Added `parsehid.c` - HID PACS decoding for MIFARE Classic dumps diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 1283d6d9b..c17c8c9ac 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -509,6 +509,7 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/parsers/parsehrt.c ${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/hrtparser/hrtparser.c ${PM3_ROOT}/client/src/pla.c ${PM3_ROOT}/client/src/preferences.c diff --git a/client/Makefile b/client/Makefile index 7ffe7ac83..e6eb01104 100644 --- a/client/Makefile +++ b/client/Makefile @@ -907,6 +907,7 @@ SRCS = mifare/aiddesfire.c \ parsers/parsehrt.c \ parsers/parsehid.c \ parsers/parsevigik.c \ + parsers/parsehexact.c \ parsers/hrtparser/hrtparser.c \ pm3.c \ pm3_binlib.c \ diff --git a/client/experimental_lib/CMakeLists.txt b/client/experimental_lib/CMakeLists.txt index 826a0ca69..7dcc6d6fc 100644 --- a/client/experimental_lib/CMakeLists.txt +++ b/client/experimental_lib/CMakeLists.txt @@ -436,6 +436,7 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/parsers/parsehrt.c ${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/hrtparser/hrtparser.c ${PM3_ROOT}/client/src/pla.c ${PM3_ROOT}/client/src/preferences.c diff --git a/client/src/cmdhfmf.c b/client/src/cmdhfmf.c index c050da4c8..8177f0c06 100644 --- a/client/src/cmdhfmf.c +++ b/client/src/cmdhfmf.c @@ -45,6 +45,7 @@ #include "mifare/gen4.h" #include "parsers/parsehid.h" #include "parsers/parsevigik.h" +#include "parsers/parsehexact.h" #include "generator.h" // keygens. #include "fpga.h" #include "mifare/mifarehost.h" @@ -885,6 +886,10 @@ static int mf_view_dump(uint8_t *dump, size_t bytes_read, uint16_t block_cnt, bo (void)vigik_parser_parse(dump, bytes_read); } + if (is_valid_hexact_card(dump, bytes_read)) { + (void)hexact_parser_parse(dump, bytes_read); + } + return PM3_SUCCESS; } @@ -8466,13 +8471,15 @@ static int CmdHF14AMfView(const char *Cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "hf mf view", "Print a MIFARE Classic dump file (bin/eml/json)", - "hf mf view -f hf-mf-01020304-dump.bin" + "hf mf view -f hf-mf-01020304-dump.bin\n" + "hf mf 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(NULL, "sk", "Save extracted keys to binary file"), + arg_lit0(NULL, "selftest", "Run the dump parsers self tests and exit"), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, false); @@ -8481,8 +8488,18 @@ static int CmdHF14AMfView(const char *Cmd) { CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)filename, FILE_PATH_SIZE, &fnlen); bool verbose = arg_get_lit(ctx, 2); bool save_keys = arg_get_lit(ctx, 3); + bool selftest = arg_get_lit(ctx, 4); CLIParserFree(ctx); + if (selftest) { + return vigik_selftest(); + } + + if (fnlen == 0) { + PrintAndLogEx(ERR, "Specify a filename with `-f` or run `--selftest`"); + return PM3_EINVARG; + } + // read dump file uint8_t *dump = NULL; size_t bytes_read = 0; diff --git a/client/src/parsers/parsehexact.c b/client/src/parsers/parsehexact.c new file mode 100644 index 000000000..ef631a6e5 --- /dev/null +++ b/client/src/parsers/parsehexact.c @@ -0,0 +1,121 @@ +//----------------------------------------------------------------------------- +// 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. +//----------------------------------------------------------------------------- +// Hexact / COGELEC / Intratone parser for MIFARE Classic dumps +// +// A VIGIK based residential access system, but one that carries no MAD, so the +// VIGIK structure parser cannot find anything on it. What the layout is, as far +// as it has been worked out against cards: +// +// sector 0 block 2 8 byte per card identifier, then a constant tail +// sector 9 48 bytes, differs completely between cards +// sector 11 32 bytes, differs completely between cards +// sector 15 the system identifier, the same on every card +// sector 16, 17 MIFARE Classic EV1 signature, not application data +// +// The sector 9 and 11 payload is not decoded. It carries no structure in common +// between two cards, so it is enciphered with something not held here, and +// printing a guess at fields would be inventing them. +//----------------------------------------------------------------------------- + +#include "parsehexact.h" + +#include + +#include "commonutil.h" +#include "ui.h" // PrintAndLogEx +#include "util.h" // sprint_hex +#include "mifare/mifaredefault.h" // MFBLOCK_SIZE +#include "mifare/mifare4.h" // mfFirstBlockOfSector + +#define HEXACT_ID_SECTOR 15 +#define HEXACT_DATA_SECTOR_A 9 +#define HEXACT_DATA_SECTOR_B 11 + +// Sector 15 block 0 spells the system out and is identical on every card seen +static const uint8_t hexact_marker[] = { + 'h', 'E', 'x', 'a', 'c', 't', ' ', '-', ' ', 'C', 'O', 'G', 'E', 'L', 'E', 'C' +}; + +// Block 0 of these cards advertises SAK 0x88 while the tag itself answers 0x08. +// A magic card written with that block 0 answers 0x88 in anticollision too, +// which is how the readers spot a clone. +#define HEXACT_BLOCK0_SAK 0x88 + +static const uint8_t *hexact_sector(const uint8_t *dump, size_t dumplen, uint8_t sector, uint8_t block) { + size_t off = (mfFirstBlockOfSector(sector) + block) * MFBLOCK_SIZE; + if (off + MFBLOCK_SIZE > dumplen) { + return NULL; + } + return dump + off; +} + +bool is_valid_hexact_card(const uint8_t *dump, size_t dumplen) { + const uint8_t *p = hexact_sector(dump, dumplen, HEXACT_ID_SECTOR, 0); + if (p == NULL) { + return false; + } + return (memcmp(p, hexact_marker, sizeof(hexact_marker)) == 0); +} + +int hexact_parser_parse(const uint8_t *dump, size_t dumplen) { + + if (is_valid_hexact_card(dump, dumplen) == false) { + return PM3_EINVARG; + } + + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, _CYAN_("Hexact / COGELEC / Intratone") " detected"); + + const uint8_t *id0 = hexact_sector(dump, dumplen, HEXACT_ID_SECTOR, 0); + const uint8_t *id1 = hexact_sector(dump, dumplen, HEXACT_ID_SECTOR, 1); + const uint8_t *id2 = hexact_sector(dump, dumplen, HEXACT_ID_SECTOR, 2); + + PrintAndLogEx(INFO, "System............. %.16s", (const char *)id0); + if (id1) { + PrintAndLogEx(INFO, " %.16s", (const char *)id1); + } + if (id2) { + PrintAndLogEx(INFO, " %.12s", (const char *)(id2 + 4)); + } + + // sector 0 block 2 holds a per card value followed by a tail that is the + // same on every card + const uint8_t *s0b2 = hexact_sector(dump, dumplen, 0, 2); + if (s0b2) { + PrintAndLogEx(INFO, "Card identifier.... %s", sprint_hex_inrow(s0b2, 8)); + } + + if (dumplen > 5 && dump[5] == HEXACT_BLOCK0_SAK) { + PrintAndLogEx(INFO, "Block 0 SAK........ 0x%02X ( " _YELLOW_("anti clone marker") " )", dump[5]); + PrintAndLogEx(INFO, " the tag answers 0x08, a clone written with this block 0 answers 0x88"); + } + + const uint8_t *a = hexact_sector(dump, dumplen, HEXACT_DATA_SECTOR_A, 0); + const uint8_t *b = hexact_sector(dump, dumplen, HEXACT_DATA_SECTOR_B, 0); + if (a == NULL || b == NULL) { + return PM3_SUCCESS; + } + + PrintAndLogEx(INFO, "Payload............ sector %u and %u, " _YELLOW_("not decoded"), + HEXACT_DATA_SECTOR_A, HEXACT_DATA_SECTOR_B); + for (uint8_t blk = 0; blk < 3; blk++) { + PrintAndLogEx(INFO, " sector %2u blk %u.. %s", HEXACT_DATA_SECTOR_A, blk, sprint_hex_inrow(a + (blk * MFBLOCK_SIZE), MFBLOCK_SIZE)); + } + for (uint8_t blk = 0; blk < 3; blk++) { + PrintAndLogEx(INFO, " sector %2u blk %u.. %s", HEXACT_DATA_SECTOR_B, blk, sprint_hex_inrow(b + (blk * MFBLOCK_SIZE), MFBLOCK_SIZE)); + } + return PM3_SUCCESS; +} diff --git a/client/src/parsers/parsehexact.h b/client/src/parsers/parsehexact.h new file mode 100644 index 000000000..5c387d795 --- /dev/null +++ b/client/src/parsers/parsehexact.h @@ -0,0 +1,30 @@ +//----------------------------------------------------------------------------- +// 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. +//----------------------------------------------------------------------------- +// Hexact / COGELEC / Intratone parser for MIFARE Classic dumps +//----------------------------------------------------------------------------- + +#ifndef PARSEHEXACT_H__ +#define PARSEHEXACT_H__ + +#include "common.h" + +// True when sector 15 carries the Hexact system identifier +bool is_valid_hexact_card(const uint8_t *dump, size_t dumplen); + +// Print what is known about the layout of a Hexact card +int hexact_parser_parse(const uint8_t *dump, size_t dumplen); + +#endif diff --git a/client/src/parsers/parsevigik.c b/client/src/parsers/parsevigik.c index a32900dd2..cf7cd4e89 100644 --- a/client/src/parsers/parsevigik.c +++ b/client/src/parsers/parsevigik.c @@ -30,6 +30,11 @@ #include "mifare/mifaredefault.h" // MIFARE_KEY_SIZE #include "mbedtls/bignum.h" +#define VIGIK_SIG_LEN 128 // RSA 1024, the signature on the card +#define VIGIK_MSG_SLOTS 64 // message byte slots in an ISO 9796-1 block +#define VIGIK_MSG_PAD 8 // leading zero bytes of the signed message + + static const vigik_pk_t vigik_rsa_pk[] = { {"La Poste Service Universel", 0x07AA, "AB9953CBFCCD9375B6C028ADBAB7584BED15B9CA037FADED9765996F9EA1AB983F3041C90DA3A198804FF90D5D872A96A4988F91F2243B821E01C5021E3ED4E1BA83B7CFECAB0E766D8563164DE0B2412AE4E6EA63804DF5C19C7AA78DC14F608294D732D7C8C67A88C6F84C0F2E3FAFAE34084349E11AB5953AC68729D07715"}, {"La Poste Service Universel", 0x07AA, "1577D02987C63A95B51AE149430834AEAF3F2E0F4CF8C6887AC6C8D732D79482604FC18DA77A9CC1F54D8063EAE6E42A41B2E04D1663856D760EABECCFB783BAE1D43E1E02C5011E823B24F2918F98A4962A875D0DF94F8098A1A30DC941303F98ABA19E6F996597EDAD7F03CAB915ED4B58B7BAAD28C0B67593CDFCCB5399AB"}, @@ -37,11 +42,11 @@ static const vigik_pk_t vigik_rsa_pk[] = { {"La Poste Autres Services", 0x07AB, "A6D99B8D902893B04F3F8DE56CB6BF24338FEE897C1BCE6DFD4EBD05B7B1A07FD2EB564BB4F7D35DBFE0A42966C2C137AD156E3DAB62904592BCA20C0BC7B8B1E261EF82D53F52D203843566305A49A22062DECC38C2FE3864CAD08E79219487651E2F79F1C9392B48CAFE1BFFAFF4802AE451E7A283E55A4026AD1E82DF1A15"}, {"La Poste Autres Services", 0x07AB, "151adf821ead26405ae583a2e751e42a80f4afff1bfeca482b39c9f1792f1e65879421798ed0ca6438fec238ccde6220a2495a3066358403d2523fd582ef61e2b1b8c70b0ca2bc92459062ab3d6e15ad37c1c26629a4e0bf5dd3f7b44b56ebd27fa0b1b705bd4efd6dce1b7c89ee8f3324bfb66ce58d3f4fb09328908d9bd9a6"}, - {"France Telecom", 0x07AC, "C44DBCD92F9DCF42F4902A87335DBB35D2FF530CDB09814CFA1F4B95A1BD018D099BC6AB69F667B4922AE1ED826E72951AA3E0EAAA7D49A695F04F8CDAAE2D18D10D25BD529CBB05ABF070DC7C041EC35C2BA7F58CC4C349983CC6E11A5CBE828FB8ECBC26F08E1094A6B44C8953C8E1BAFD214DF3E69F430A98CCC75C03669D"}, - {"France Telecom", 0x07AC, "9d66035cc7cc980a439fe6f34d21fdbae1c853894cb4a694108ef026bcecb88f82be5c1ae1c63c9849c3c48cf5a72b5cc31e047cdc70f0ab05bb9c52bd250dd1182daeda8c4ff095a6497daaeae0a31a95726e82ede12a92b467f669abc69b098d01bda1954b1ffa4c8109db0c53ffd235bb5d33872a90f442cf9d2fd9bc4dc4"}, + {"France Telecom", 0x07AC, "B35193DBD2F88A21CDCFFF4BF84F7FC036A991A363DCB3E802407A5E5879DC2127EECFC520779E79E911394882482C87D09A88B0711CBC2973B77FFDAE40EA0001F595072708C558B484AB89D02BCBCB971FF1B80371C0BE30CB13661078078BB68EBCCA524B9DD55EBF7D47D9355AFC95511350CC1103A5DEE847868848B235"}, + {"France Telecom", 0x07AC, "35b248888647e8dea50311cc50135195fc5a35d9477dbf5ed59d4b52cabc8eb68b0778106613cb30bec07103b8f11f97cbcb2bd089ab84b458c508270795f50100ea40aefd7fb77329bc1c71b0889ad0872c4882483911e9799e7720c5cfee2721dc79585e7a4002e8b3dc63a391a936c07f4ff84bffcfcd218af8d2db9351b3"}, - {"EDF-GDF", 0x07AD, "B35193DBD2F88A21CDCFFF4BF84F7FC036A991A363DCB3E802407A5E5879DC2127EECFC520779E79E911394882482C87D09A88B0711CBC2973B77FFDAE40EA0001F595072708C558B484AB89D02BCBCB971FF1B80371C0BE30CB13661078078BB68EBCCA524B9DD55EBF7D47D9355AFC95511350CC1103A5DEE847868848B235"}, - {"EDF-GDF", 0x07AD, "35b248888647e8dea50311cc50135195fc5a35d9477dbf5ed59d4b52cabc8eb68b0778106613cb30bec07103b8f11f97cbcb2bd089ab84b458c508270795f50100ea40aefd7fb77329bc1c71b0889ad0872c4882483911e9799e7720c5cfee2721dc79585e7a4002e8b3dc63a391a936c07f4ff84bffcfcd218af8d2db9351b3"}, + {"EDF-GDF", 0x07AD, "C44DBCD92F9DCF42F4902A87335DBB35D2FF530CDB09814CFA1F4B95A1BD018D099BC6AB69F667B4922AE1ED826E72951AA3E0EAAA7D49A695F04F8CDAAE2D18D10D25BD529CBB05ABF070DC7C041EC35C2BA7F58CC4C349983CC6E11A5CBE828FB8ECBC26F08E1094A6B44C8953C8E1BAFD214DF3E69F430A98CCC75C03669D"}, + {"EDF-GDF", 0x07AD, "9d66035cc7cc980a439fe6f34d21fdbae1c853894cb4a694108ef026bcecb88f82be5c1ae1c63c9849c3c48cf5a72b5cc31e047cdc70f0ab05bb9c52bd250dd1182daeda8c4ff095a6497daaeae0a31a95726e82ede12a92b467f669abc69b098d01bda1954b1ffa4c8109db0c53ffd235bb5d33872a90f442cf9d2fd9bc4dc4"}, {NULL, 0, NULL} }; @@ -55,256 +60,311 @@ const char *vigik_get_service(uint16_t service_code) { } -int vigik_verify(mfc_vigik_t *d) { -#define PUBLIC_VIGIK_KEYLEN 128 +// ISO 9796-1 shadow permutation. Each message byte is stored next to its shadow, +// which is what gives the scheme its redundancy. +static const uint8_t vigik_iso9796_pi[16] = { + 0x0E, 0x03, 0x05, 0x08, 0x09, 0x04, 0x02, 0x0F, + 0x00, 0x0D, 0x0B, 0x06, 0x07, 0x0A, 0x0C, 0x01 +}; - // iso9796 - // Exponent V = 2 - // n = The public modulus n is the product of the secret prime factors p and q. Its length is 1024 bits. +static uint8_t vigik_shadow(uint8_t b) { + return (uint8_t)((vigik_iso9796_pi[b >> 4] << 4) | vigik_iso9796_pi[b & 0x0F]); +} - if (g_debugMode == DEBUG) { - PrintAndLogEx(INFO, "Raw"); - print_hex_noascii_break((uint8_t *)d, sizeof(*d) - sizeof(d->rsa_signature), MFBLOCK_SIZE * 2); +// Recover the message an ISO 9796-1 signature carries, checking the redundancy on +// the way. `sig` is the signature as it sits on the card, `modulus` the public key +// to try. On success msg holds msglen bytes. +// +// Rabin, so v = 2 and squaring the signature gives the block back. Which of the +// four candidates is the block is decided by the format itself: an ISO 9796-1 +// block always ends in the nibble 6. +static int vigik_iso9796_recover(const uint8_t *sig, const char *modulus, uint8_t *msg, size_t *msglen) { - PrintAndLogEx(INFO, "Raw signature"); - print_hex_noascii_break(d->rsa_signature, sizeof(d->rsa_signature), MFBLOCK_SIZE * 2); + uint8_t n[VIGIK_SIG_LEN] = {0}; + int dl = 0; + param_gethex_to_eol(modulus, 0, n, sizeof(n), &dl); + if (dl != VIGIK_SIG_LEN) { + return PM3_EINVARG; } - /* - int dl = 0; + uint8_t rev[VIGIK_SIG_LEN] = {0}; + reverse_array_copy(sig, VIGIK_SIG_LEN, rev); - param_gethex_to_eol("1C07D46DA3849326D24B3468BD76673F4F3C41827DC413E81E4F3C7804FAC727213059B21D047510D6432448643A92EBFC67FBEDDAB468D13D948B172F5EBC79A0E3FEFDFAF4E81FC7108E070F1E3CD0", 0, signature, PUBLIC_VIGIK_KEYLEN, &dl); + mbedtls_mpi N, S, E, J, cand, tmp; + mbedtls_mpi_init(&N); + mbedtls_mpi_init(&S); + mbedtls_mpi_init(&E); + mbedtls_mpi_init(&J); + mbedtls_mpi_init(&cand); + mbedtls_mpi_init(&tmp); - param_gethex_to_eol("1AB86FE0C17FFFFE4379D5E15A4B2FAFFEFCFA0F1F3F7FA03E7DDDF1E3C78FFFB1F0E23F7FFF51584771C5C18307FEA36CA74E60AA6B0409ACA66A9EC155F4E9112345708A2B8457E722608EE1157408", 0, signature, PUBLIC_VIGIK_KEYLEN, &dl); - signature_len = dl; - */ + mbedtls_mpi_read_binary(&N, n, sizeof(n)); + mbedtls_mpi_read_binary(&S, rev, sizeof(rev)); + mbedtls_mpi_add_int(&E, &E, 2); - uint8_t rev_sig[128]; - reverse_array_copy(d->rsa_signature, sizeof(d->rsa_signature), rev_sig); + uint8_t f[VIGIK_SIG_LEN] = {0}; + bool got_block = false; - PrintAndLogEx(INFO, "Raw signature reverse"); - print_hex_noascii_break(rev_sig, sizeof(d->rsa_signature), MFBLOCK_SIZE * 2); + if (mbedtls_mpi_cmp_mpi(&S, &N) < 0) { - // t = 0xBC = Implicitly known - // t = 0xCC = look at byte before to determine hash function - // uint8_t T[] = {0x33, 0xCC}; + mbedtls_mpi_exp_mod(&J, &S, &E, &N, NULL); - // Success decrypt would mean 0x4b BB ... BB BA padding - // padding, message, hash, 8 bits or 16 bits + for (uint8_t c = 0; c < 4 && got_block == false; c++) { - // signature = h( C || M1 || h(M2) ) - // 1024 - 786 - 160 - 16 -1 - // salt C - // message M = 96 bytes, 768 bits - // sha1 hash H = 20 bytes, 160 bits - // padding = 20 bytes, 96 bits + switch (c) { + case 0: + mbedtls_mpi_copy(&cand, &J); + break; + case 1: + mbedtls_mpi_sub_mpi(&cand, &N, &J); + break; + case 2: + mbedtls_mpi_mul_int(&cand, &J, 2); + break; + default: + mbedtls_mpi_sub_mpi(&tmp, &N, &J); + mbedtls_mpi_mul_int(&cand, &tmp, 2); + break; + } - uint8_t i; - bool is_valid = false; + if (mbedtls_mpi_bitlen(&cand) > (VIGIK_SIG_LEN * 8)) { + continue; + } + + uint8_t low = 0; + for (uint8_t b = 0; b < 4; b++) { + low |= (uint8_t)(mbedtls_mpi_get_bit(&cand, b) << b); + } + + if (low != 0x06) { + continue; + } + + if (mbedtls_mpi_write_binary(&cand, f, sizeof(f)) == 0) { + got_block = true; + } + } + } + + mbedtls_mpi_free(&N); + mbedtls_mpi_free(&S); + mbedtls_mpi_free(&E); + mbedtls_mpi_free(&J); + mbedtls_mpi_free(&cand); + mbedtls_mpi_free(&tmp); + + if (got_block == false) { + return PM3_ESOFT; + } + + // Every byte pair has to be a message byte next to its shadow. Exactly three + // are allowed not to be: the leading pair carries the bit that keeps the block + // below n, one marks how long the message is, and the last holds the forced 6. + size_t broken[4] = {0}; + size_t brokencnt = 0; + for (size_t i = 0; i < VIGIK_SIG_LEN; i += 2) { + if (vigik_shadow(f[i + 1]) != f[i]) { + if (brokencnt < ARRAYLEN(broken)) { + broken[brokencnt] = i; + } + brokencnt++; + } + } + + if (brokencnt != 3 || broken[0] != 0 || broken[2] != (VIGIK_SIG_LEN - 2)) { + return PM3_ESOFT; + } + + size_t z = VIGIK_MSG_SLOTS - (broken[1] / 2); + if (z == 0 || z > (VIGIK_MSG_SLOTS - 1)) { + return PM3_ESOFT; + } + + uint8_t stream[VIGIK_MSG_SLOTS] = {0}; + for (size_t i = 0; i < VIGIK_SIG_LEN; i += 2) { + stream[i / 2] = f[i + 1]; + } + + // The message is repeated to fill the block, the first slot belonging to the + // truncated tail of the run before it. Where a second copy fits, it has to + // agree with the first. + if (((1 + (2 * z)) <= VIGIK_MSG_SLOTS) && (memcmp(stream + 1, stream + 1 + z, z) != 0)) { + return PM3_ESOFT; + } + + reverse_array_copy(stream + 1, (int)z, msg); + *msglen = z; + return PM3_SUCCESS; +} + +// The five byte dates are year since 1900, then month, day, hour, minute. +// Print the raw bytes with the date spelled out beside them, and leave the +// decoded form off when the bytes cannot be a date at all. +static void vigik_print_date(const char *label, const uint8_t *v) { + + bool sane = ((v[1] >= 1) && (v[1] <= 12) && + (v[2] >= 1) && (v[2] <= 31) && + (v[3] <= 23) && (v[4] <= 59)); + + if (sane) { + PrintAndLogEx(INFO, "%s %s ( " _YELLOW_("%04u-%02u-%02u %02u:%02u") " )", + label, sprint_hex_inrow(v, 5), + 1900 + v[0], v[1], v[2], v[3], v[4]); + } else { + PrintAndLogEx(INFO, "%s %s", label, sprint_hex_inrow(v, 5)); + } +} + +// What a VIGIK signature covers: the UID the card has to keep, the service it +// belongs to, and the window it is good for. +static size_t vigik_expected_msg(const mfc_vigik_t *d, uint8_t *out) { + + size_t len = 0; + memset(out, 0, VIGIK_MSG_PAD); + len += VIGIK_MSG_PAD; + + memcpy(out + len, d->b0, 4); // UID + len += 4; + + Uint4byteToMemLe(out + len, d->service_code); + len += 4; + + out[len++] = d->key_version; + out[len++] = d->services_counter; + + memcpy(out + len, d->slot_access_date, sizeof(d->slot_access_date)); + len += sizeof(d->slot_access_date); + + Uint2byteToMemLe(out + len, d->slot_dst_duration); + len += 2; + return len; +} + +int vigik_verify(mfc_vigik_t *d) { + + if (d == NULL) { + return PM3_EINVARG; + } + + uint8_t expected[VIGIK_MSG_SLOTS] = {0}; + size_t expectedlen = vigik_expected_msg(d, expected); + + for (uint8_t i = 0; i < ARRAYLEN(vigik_rsa_pk); i++) { - for (i = 0; i < ARRAYLEN(vigik_rsa_pk); i++) { if (vigik_rsa_pk[i].desc == NULL) { break; } - mbedtls_mpi RN, E; - mbedtls_mpi_init(&RN); - - // exponent 2 = even - mbedtls_mpi_init(&E); - mbedtls_mpi_add_int(&E, &E, 2); - - int dl = 0; - uint8_t n[PUBLIC_VIGIK_KEYLEN]; - memset(n, 0, sizeof(n)); - param_gethex_to_eol(vigik_rsa_pk[i].n, 0, n, PUBLIC_VIGIK_KEYLEN, &dl); - - // convert - mbedtls_mpi N, s, sqr, res; - mbedtls_mpi_init(&N); - mbedtls_mpi_init(&s); - mbedtls_mpi_init(&sqr); - mbedtls_mpi_init(&res); - - mbedtls_mpi_read_binary(&N, (const unsigned char *)n, PUBLIC_VIGIK_KEYLEN); - - //mbedtls_mpi_read_binary(&s, (const unsigned char*)signature, signature_len); - mbedtls_mpi_read_binary(&s, (const unsigned char *)rev_sig, sizeof(d->rsa_signature)); - - // check is sign < (N/2) - - mbedtls_mpi n_2; - mbedtls_mpi_init(&n_2); - mbedtls_mpi_copy(&n_2, &N); - mbedtls_mpi_shift_r(&n_2, 1); - bool is_less = (mbedtls_mpi_cmp_mpi(&s, &n_2) > 0) ? false : true; - PrintAndLogEx(DEBUG, "z < (N/2) ..... %s", (is_less) ? _GREEN_("YES") : _RED_("NO")); - mbedtls_mpi_free(&n_2); - - - if (is_less) { - mbedtls_mpi_exp_mod(&sqr, &s, &E, &N, &RN); - } else { + uint8_t msg[VIGIK_MSG_SLOTS] = {0}; + size_t msglen = 0; + if (vigik_iso9796_recover(d->rsa_signature, vigik_rsa_pk[i].n, msg, &msglen) != PM3_SUCCESS) { continue; } - /* - if v is even and - ⎯ if J* mod 8 = 1, then f* = n–J*. - ⎯ if J* mod 8 = 4, then f* = J*, - ⎯ if J* mod 8 = 6, then f* = 2J*, - ⎯ if J* mod 8 = 7, then f* = 2(n–J*), - */ - uint8_t b2 = mbedtls_mpi_get_bit(&sqr, 2); - uint8_t b1 = mbedtls_mpi_get_bit(&sqr, 1); - uint8_t b0 = mbedtls_mpi_get_bit(&sqr, 0); - uint8_t lsb = (b2 << 2) | (b1 << 1) | b0; + PrintAndLogEx(INFO, "--- " _CYAN_("Signature") " ------------------------------"); + PrintAndLogEx(INFO, "Signed by.......... " _YELLOW_("%s"), vigik_rsa_pk[i].desc); + PrintAndLogEx(INFO, "Scheme............. ISO 9796-1, RSA 1024, v=2"); + PrintAndLogEx(INFO, "Recovered.......... %s", sprint_hex_inrow(msg, msglen)); - /* - //1 - mbedtls_mpi_sub_mpi(&res, &N, &sqr); - mbedtls_mpi_write_file( "[=] 1... ", &res, 16, NULL ); - // 4 - mbedtls_mpi_copy(&res, &sqr); - mbedtls_mpi_write_file( "[=] 4... ", &res, 16, NULL ); - // 6 - mbedtls_mpi_mul_int(&res, &sqr, 2); - mbedtls_mpi_write_file( "[=] 6... ", &res, 16, NULL ); - // 7 - mbedtls_mpi foo; - mbedtls_mpi_init(&foo); - mbedtls_mpi_sub_mpi(&foo, &N, &sqr); - mbedtls_mpi_mul_int(&res, &foo, 2); - mbedtls_mpi_free(&foo); - mbedtls_mpi_write_file( "[=] 7... ", &res, 16, NULL ); - */ - - switch (lsb) { - case 1: { - mbedtls_mpi_sub_mpi(&res, &N, &sqr); - break; - } - case 4: { - mbedtls_mpi_copy(&res, &sqr); - break; - } - case 6: { - mbedtls_mpi_mul_int(&res, &sqr, 2); - break; - } - case 7: { - mbedtls_mpi foo2; - mbedtls_mpi_init(&foo2); - mbedtls_mpi_sub_mpi(&foo2, &N, &sqr); - mbedtls_mpi_mul_int(&res, &foo2, 2); - mbedtls_mpi_free(&foo2); - break; - } - default: { - continue; - } + if ((msglen != expectedlen) || (memcmp(msg, expected, msglen) != 0)) { + PrintAndLogEx(INFO, "Card fields........ %s", sprint_hex_inrow(expected, expectedlen)); + PrintAndLogEx(FAILED, "Signature verification: " _RED_("card data does not match the signature")); + return PM3_ESOFT; } - PrintAndLogEx(DEBUG, "LSB............ " _GREEN_("%u"), lsb); - if (g_debugMode == DEBUG) { - mbedtls_mpi_write_file("[=] N.............. ", &N, 16, NULL); - mbedtls_mpi_write_file("[=] signature...... ", &s, 16, NULL); - mbedtls_mpi_write_file("[=] square mod n... ", &sqr, 16, NULL); - mbedtls_mpi_write_file("[=] n-fs........... ", &res, 16, NULL); + PrintAndLogEx(INFO, " UID.............. %s", sprint_hex_inrow(msg + VIGIK_MSG_PAD, 4)); + PrintAndLogEx(INFO, " Service code..... 0x%04X", (uint16_t)d->service_code); + PrintAndLogEx(INFO, " Key version...... %u", d->key_version); + PrintAndLogEx(INFO, " Services counter. %u", d->services_counter); + vigik_print_date(" Access date......", d->slot_access_date); + PrintAndLogEx(INFO, " DST duration..... %u", d->slot_dst_duration); + + // The service code on the card says which service issued it, so it should + // name the key that just verified. Where it does not, the table pairs a + // description with the wrong modulus - say so rather than quietly + // reporting the wrong issuer. + if (vigik_rsa_pk[i].code != (uint16_t)d->service_code) { + PrintAndLogEx(WARNING, "Card says service 0x%04X but the key that verified is listed as 0x%04X ( %s )", + (uint16_t)d->service_code, vigik_rsa_pk[i].code, vigik_rsa_pk[i].desc); } - - uint8_t nfs[128] = {0}; - mbedtls_mpi_write_binary(&res, nfs, sizeof(nfs)); - - // xor 0xDC01 - int count_zero = 0; - for (int x = 0; x < sizeof(nfs); x += 2) { - nfs[x] ^= 0xDC; - nfs[x + 1] ^= 0x01; - - if (nfs[x] == 0x00) - count_zero++; - if (nfs[x + 1] == 0x00) - count_zero++; - } - - if (count_zero > 10) { - PrintAndLogEx(INFO, ""); - PrintAndLogEx(INFO, "Message XORED"); - print_hex_noascii_break(nfs, sizeof(nfs), 32); - PrintAndLogEx(INFO, "\n"); - is_valid = true; - break; - } - - /* - if (bar == 0) { - typedef struct vigik_rsa_s { - uint8_t rsa[127]; - uint8_t hash; - } vigik_rsa_t; - - vigik_rsa_t ts; - memcpy(&ts, nfs, sizeof(ts)); - - if ( ts.hash == 0xCC ) { - PrintAndLogEx(INFO, "Hash byte... 0x%02X", ts.hash); - switch(ts.rsa[126]) { - case 0x11: - PrintAndLogEx(INFO, "Hash algo ( 0x%02X ) - SHA1"); - break; - case 0x22: - PrintAndLogEx(INFO, "Hash algo ( 0x%02X ) - RIPEMD"); - break; - case 0x33: - PrintAndLogEx(INFO, "Hash algo ( 0x%02X ) - SHA1"); - break; - default: - PrintAndLogEx(INFO, "Hash algo ( 0x%02X ) - " _RED_("err")); - break; - } - } else if ( ts.hash == 0xBC) { - PrintAndLogEx(INFO, "Hash byte... 0x%02X - " _GREEN_("implict"), ts.hash); - } else { - PrintAndLogEx(INFO, "Hash byte... 0x%02x - " _RED_("err"), ts.hash); - } - - PrintAndLogEx(INFO, "Message w padding"); - print_hex_noascii_break(ts.rsa, sizeof(ts.rsa) - 20, 32); - } - */ - - mbedtls_mpi_free(&N); - mbedtls_mpi_free(&s); - mbedtls_mpi_free(&res); - mbedtls_mpi_free(&RN); - mbedtls_mpi_free(&E); + PrintAndLogEx(SUCCESS, "Signature verification: " _GREEN_("successful")); + return PM3_SUCCESS; } - PrintAndLogEx(INFO, ""); - PrintAndLogEx(INFO, "--- " _CYAN_("Tag Signature")); - PrintAndLogEx(INFO, "RSA: 1024bit"); + PrintAndLogEx(FAILED, "Signature verification: " _RED_("no public key recovered a valid block")); + return PM3_ESOFT; +} - if (is_valid == false || i == ARRAYLEN(vigik_rsa_pk)) { - PrintAndLogEx(INFO, "Signature:"); - print_hex_noascii_break(d->rsa_signature, sizeof(d->rsa_signature), MFBLOCK_SIZE * 2); - PrintAndLogEx(SUCCESS, "Signature verification: " _RED_("failed")); +// An expired VIGIK service badge, published for study in +// armsrc/Standalone/hf_colin.c. Signed by La Poste Service Universel. +static const uint8_t vigik_selftest_card[] = { + 0x12, 0x1C, 0x7F, 0x73, 0x02, 0x08, 0x04, 0x00, 0x01, 0xFA, 0x33, 0xF5, + 0xCB, 0x2D, 0x02, 0x1D, 0x44, 0x00, 0x10, 0x49, 0x16, 0x49, 0x16, 0x49, + 0x16, 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0xAA, 0x07, 0x00, 0x00, 0x21, 0x02, 0x08, 0x00, + 0x00, 0x74, 0x0C, 0x11, 0x06, 0x00, 0xAF, 0x13, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x74, 0x0C, 0x11, 0x08, 0x22, 0x00, 0x00, + 0x24, 0xE5, 0x72, 0xB9, 0x23, 0xA3, 0xD2, 0x43, 0xB4, 0x02, 0xD6, 0x0C, + 0xAB, 0x57, 0x69, 0x56, 0x21, 0x6D, 0x65, 0x01, 0xFC, 0x86, 0x18, 0xB6, + 0xC4, 0x26, 0x76, 0x25, 0x11, 0xAC, 0x2D, 0xEE, 0x25, 0xBF, 0x4C, 0xEC, + 0x36, 0x18, 0xD0, 0xBA, 0xB3, 0xA6, 0xE9, 0x21, 0x0D, 0x88, 0x77, 0x46, + 0x0F, 0xBC, 0x41, 0xA5, 0xD9, 0x53, 0x98, 0xE7, 0x6A, 0x1B, 0x20, 0x29, + 0xE8, 0xEA, 0x97, 0x35, 0x08, 0x8B, 0xA2, 0xCE, 0x73, 0x26, 0x53, 0xD0, + 0xC1, 0x14, 0x75, 0x96, 0xAF, 0xCF, 0x94, 0xD7, 0x77, 0xB4, 0xD9, 0x1F, + 0x04, 0x42, 0x18, 0x22, 0x73, 0xA2, 0x9D, 0xEA, 0xF7, 0xA2, 0xD0, 0x95, + 0x4C, 0xEE, 0x71, 0x58, 0x66, 0xE5, 0x08, 0xCD, 0xBC, 0x95, 0xC6, 0x40, + 0xEC, 0x9D, 0x1E, 0x58, 0xE8, 0x00, 0x45, 0x7C, 0xF8, 0xB0, 0x79, 0x41, + 0x4E, 0x1B, 0x45, 0xDD, 0x3E, 0x6C, 0x93, 0x17, +}; + +int vigik_selftest(void) { + + PrintAndLogEx(INFO, "Testing VIGIK ISO 9796-1 signature recovery"); + + if (sizeof(vigik_selftest_card) != sizeof(mfc_vigik_t)) { + PrintAndLogEx(FAILED, " vector is %zu bytes, struct is %zu ( " _RED_("fail") " )", + sizeof(vigik_selftest_card), sizeof(mfc_vigik_t)); return PM3_ESOFT; } - PrintAndLogEx(INFO, "Signature public key name: " _YELLOW_("%s"), vigik_rsa_pk[i].desc); - PrintAndLogEx(INFO, "Signature public key value:"); - PrintAndLogEx(INFO, "%.64s", vigik_rsa_pk[i].n); - PrintAndLogEx(INFO, "%.64s", vigik_rsa_pk[i].n + 64); - PrintAndLogEx(INFO, "%.64s", vigik_rsa_pk[i].n + 128); - PrintAndLogEx(INFO, "%.64s", vigik_rsa_pk[i].n + 192); + mfc_vigik_t card; + memcpy(&card, vigik_selftest_card, sizeof(card)); - PrintAndLogEx(INFO, "Signature:"); - print_hex_noascii_break(d->rsa_signature, sizeof(d->rsa_signature), MFBLOCK_SIZE * 2); + uint8_t expected[VIGIK_MSG_SLOTS] = {0}; + size_t expectedlen = vigik_expected_msg(&card, expected); - PrintAndLogEx(SUCCESS, "Signature verification: " _GREEN_("successful")); + uint8_t msg[VIGIK_MSG_SLOTS] = {0}; + size_t msglen = 0; + int res = PM3_ESOFT; + for (uint8_t i = 0; i < ARRAYLEN(vigik_rsa_pk); i++) { + if (vigik_rsa_pk[i].desc == NULL) { + break; + } + if (vigik_iso9796_recover(card.rsa_signature, vigik_rsa_pk[i].n, msg, &msglen) == PM3_SUCCESS) { + PrintAndLogEx(INFO, " recovered against.. %s", vigik_rsa_pk[i].desc); + res = PM3_SUCCESS; + break; + } + } + + if (res != PM3_SUCCESS) { + PrintAndLogEx(FAILED, " no public key recovered a valid block ( " _RED_("fail") " )"); + return PM3_ESOFT; + } + + PrintAndLogEx(INFO, " recovered.......... %s", sprint_hex_inrow(msg, msglen)); + PrintAndLogEx(INFO, " card fields........ %s", sprint_hex_inrow(expected, expectedlen)); + + if ((msglen != expectedlen) || (memcmp(msg, expected, msglen) != 0)) { + PrintAndLogEx(FAILED, " recovered message does not match the card ( " _RED_("fail") " )"); + return PM3_ESOFT; + } + + PrintAndLogEx(SUCCESS, "VIGIK signature selftest ( " _GREEN_("ok") " )"); return PM3_SUCCESS; } @@ -312,20 +372,20 @@ int vigik_annotate(mfc_vigik_t *d) { if (d == NULL) return PM3_EINVARG; - PrintAndLogEx(INFO, "Manufacture......... %s", sprint_hex(d->b0, sizeof(d->b0))); - PrintAndLogEx(INFO, "MAD................. %s", sprint_hex(d->mad, sizeof(d->mad))); + PrintAndLogEx(INFO, "Manufacture......... %s", sprint_hex_inrow(d->b0, sizeof(d->b0))); + PrintAndLogEx(INFO, "MAD................. %s", sprint_hex_inrow(d->mad, sizeof(d->mad))); PrintAndLogEx(INFO, "Counters............ %u", d->counters); - PrintAndLogEx(INFO, "rtf................. %s", sprint_hex(d->rtf, sizeof(d->rtf))); + PrintAndLogEx(INFO, "rtf................. %s", sprint_hex_inrow(d->rtf, sizeof(d->rtf))); PrintAndLogEx(INFO, "Service code........ 0x%08x / %u - " _YELLOW_("%s"), d->service_code, d->service_code, vigik_get_service(d->service_code)); PrintAndLogEx(INFO, "Info flag........... %u -", d->info_flag); // , sprint_bin(d->info_flag, 1)); PrintAndLogEx(INFO, "Key version......... %u", d->key_version); PrintAndLogEx(INFO, "PTR Counter......... %u", d->ptr_counter); PrintAndLogEx(INFO, "Counter num......... %u", d->counter_num); - PrintAndLogEx(INFO, "Slot access date.... %s", sprint_hex(d->slot_access_date, sizeof(d->slot_access_date))); + vigik_print_date("Slot access date....", d->slot_access_date); PrintAndLogEx(INFO, "Slot dst duration... %u", d->slot_dst_duration); - PrintAndLogEx(INFO, "Other Slots......... %s", sprint_hex(d->other_slots, sizeof(d->other_slots))); + PrintAndLogEx(INFO, "Other Slots......... %s", sprint_hex_inrow(d->other_slots, sizeof(d->other_slots))); PrintAndLogEx(INFO, "Services counter.... %u", d->services_counter); - PrintAndLogEx(INFO, "Loading date........ %s", sprint_hex(d->loading_date, sizeof(d->loading_date))); + vigik_print_date("Loading date........", d->loading_date); PrintAndLogEx(INFO, "Reserved null....... %u", d->reserved_null); PrintAndLogEx(INFO, "----------------------------------------------------------------"); PrintAndLogEx(INFO, ""); @@ -339,8 +399,7 @@ int vigik_annotate(mfc_vigik_t *d) { // Key layouts of the VIGIK family building access systems. Every one of these // ships the same keys on every card, so matching the whole set identifies the // system outright - no guessing from the payload. Sourced from the schemas in -// armsrc/Standalone/hf_colin.c, with the Hexact sector 15 key B measured on a -// card here; hf_colin.c leaves that slot empty. +// armsrc/Standalone/hf_colin.c. Hexact is not here; it has its own parser. typedef struct { const char *name; uint64_t key_a[16]; @@ -348,21 +407,6 @@ typedef struct { } vigik_schema_t; static const vigik_schema_t vigik_schemas[] = { - { - "Infineon / Hexact / COGELEC / Intratone", - { - 0x484558414354ULL, 0x484558414354ULL, 0x484558414354ULL, 0x484558414354ULL, - 0x484558414354ULL, 0x484558414354ULL, 0x484558414354ULL, 0x484558414354ULL, - 0x484558414354ULL, 0x484558414354ULL, 0x484558414354ULL, 0x484558414354ULL, - 0x484558414354ULL, 0x484558414354ULL, 0x484558414354ULL, 0x484558414354ULL - }, - { - 0xa22ae129c013ULL, 0x49fae4e3849fULL, 0x38fcf33072e0ULL, 0x8ad5517b4b18ULL, - 0x509359f131b1ULL, 0x6c78928e1317ULL, 0xaa0720018738ULL, 0xa6cac2886412ULL, - 0x62d0c424ed8eULL, 0xe64a986a5d94ULL, 0x8fa1d601d0a2ULL, 0x89347350bd36ULL, - 0x66d2b7dc39efULL, 0x6bc1e1ae547dULL, 0x22729a9bd40fULL, 0x484558414354ULL - } - }, { "Noralsy", { @@ -402,8 +446,8 @@ static const vigik_schema_t vigik_schemas[] = { VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY }, { - 0x010203040506ULL, 0x010203040506ULL, 0x010203040506ULL, 0x010203040506ULL, - 0x010203040506ULL, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, + VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, + VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY } diff --git a/client/src/parsers/parsevigik.h b/client/src/parsers/parsevigik.h index b90208ef0..1d20fae0e 100644 --- a/client/src/parsers/parsevigik.h +++ b/client/src/parsers/parsevigik.h @@ -47,4 +47,7 @@ const char *vigik_get_service(uint16_t service_code); int vigik_verify(mfc_vigik_t *d); int vigik_annotate(mfc_vigik_t *d); +// Recover and check the signature of a known good expired card +int vigik_selftest(void); + #endif diff --git a/tools/pm3_tests.sh b/tools/pm3_tests.sh index c56c66023..793ca444f 100755 --- a/tools/pm3_tests.sh +++ b/tools/pm3_tests.sh @@ -472,6 +472,7 @@ while true; do if ! CheckExecute "mfu pwdgen test" "$CLIENTBIN -c 'hf mfu pwdgen --test'" "Selftest ok"; then break; fi if ! CheckExecute "mfu keygen test" "$CLIENTBIN -c 'hf mfu keygen --uid 11223344556677'" "80 B1 C2 71 D8 A0"; then break; fi if ! CheckExecute "jooki encode test" "$CLIENTBIN -c 'hf jooki encode --test'" "04 28 F4 DA F0 4A 81 \( ok \)"; then break; fi + if ! CheckExecute "vigik signature selftest" "$CLIENTBIN -c 'hf mf view --selftest'" "selftest \\( ok \\)"; then break; fi if ! CheckExecute "analyse regex selftest" "$CLIENTBIN -c 'analyse regex --test'" "Tests \( ok \)"; then break; fi if ! CheckExecute "trace load/list 14a" "$CLIENTBIN -c 'trace load -f traces/hf_14a_mfu.trace; trace list -1 -t 14a;'" "READBLOCK\(8\)"; then break; fi if ! CheckExecute "trace load/list x" "$CLIENTBIN -c 'trace load -f traces/hf_14a_mfu.trace; trace list -x1 -t 14a;'" "0.0101840425"; then break; fi From 66f9a6b51f4c3b60bc8d48bec25213d08db549d5 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sun, 13 Sep 2026 12:58:46 +0200 Subject: [PATCH 2/2] hf mfdes: report a select/auth failure once, not twice DesfireSelectAndAuthenticate*() and its callers both printed the same error at different severities, so every failure came out as two lines. The core helper now owns the message and names the AID and the step; the 23 restatements in cmdhfmfdes.c are gone. Same duplicate pair fixed in cmdhfgallagher.c. Also: DesfireAuthErrorToStr() falls back to DesfireGetErrorString() for the PM3_E* codes the select paths pass through, which used to print an empty reason, and auth error 7 no longer reuses the text of error 1. Co-Authored-By: Claude Opus 5 (1M context) --- CHANGELOG.md | 1 + client/src/cmdhfgallagher.c | 4 ++-- client/src/cmdhfmfdes.c | 28 ----------------------- client/src/mifare/desfirecore.c | 39 ++++++++++++++++++++------------- 4 files changed, 27 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fed806f56..ac65259f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ 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] +- Fixed `hf mfdes` - select / authenticate failures are now reported once, naming the AID and the failing step (@iceman1001) - Fixed `-f ` - a filename given with its own extension is now used as-is on both load and save (@iceman1001) - Fixed `hf mf view` - VIGIK signature verification now recovers the ISO 9796-1 message and checks it against the card (@iceman1001) - Added `hf mf view --selftest` - recovers and checks the signature of a known good expired VIGIK card (@iceman1001) diff --git a/client/src/cmdhfgallagher.c b/client/src/cmdhfgallagher.c index 7a94c6e06..c965d8acc 100644 --- a/client/src/cmdhfgallagher.c +++ b/client/src/cmdhfgallagher.c @@ -161,7 +161,7 @@ static int authenticate(DesfireContext_t *ctx, bool verbose) { int res = DesfireAuthenticate(ctx, DACEV1, false); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "Desfire authenticate " _RED_("error") ". Result: [%d] %s", + PrintAndLogEx(FAILED, "Desfire authentication " _RED_("failed") ". Result [%d] %s", res, DesfireAuthErrorToStr(res) ); @@ -426,7 +426,7 @@ static int hfgal_create_creds_app(DesfireContext_t *ctx, uint8_t *site_key, uint DesfireSetKeyNoClear(ctx, 0, T_AES, blank_key); DesfireSetKdf(ctx, MFDES_KDF_ALGO_NONE, NULL, 0); res = authenticate(ctx, verbose); - PM3_RET_IF_ERR_WITH_MSG(res, "Desfire authenticate error. Result: [%d] %s", res, DesfireAuthErrorToStr(res)); + PM3_RET_IF_ERR(res); // authenticate() already reported the reason // Change key DesfireSetCommMode(ctx, DCMEncryptedPlain); diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 97df7ea5e..95aa11d35 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -2051,7 +2051,6 @@ static int CmdHF14aDesDetect(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, true, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -2121,7 +2120,6 @@ static int CmdHF14aDesDetect(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, true, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } } @@ -2141,7 +2139,6 @@ static int CmdHF14aDesDetect(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, true, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") " after the LRP probe. Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } } @@ -3385,7 +3382,6 @@ static int CmdHF14ADesBruteISOFIDs(const char *Cmd) { res = DesfireSelectAndAuthenticateW(&dctx, securechann, selectway, appid, false, 0, true, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, appid), res, DesfireAuthErrorToStr(res)); return res; } @@ -3723,7 +3719,6 @@ static int CmdHF14ADesAuth(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, false, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -3858,12 +3853,6 @@ static int CmdHF14ADesSetConfiguration(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, false, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication ( %s ) Result [%d] %s %s", - DesfireWayIDStr(selectway, id), - res, - DesfireAuthErrorToStr(res), - _RED_("failed") - ); return res; } @@ -4000,7 +3989,6 @@ static int CmdHF14ADesChangeKey(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, false, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -4742,7 +4730,6 @@ static int CmdHF14ADesGetUID(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, false, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -5124,7 +5111,6 @@ static int CmdHF14ADesGetKeyVersions(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -5434,7 +5420,6 @@ static int CmdHF14ADesGetFileIDs(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -5511,7 +5496,6 @@ static int CmdHF14ADesGetFileISOIDs(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -5593,7 +5577,6 @@ static int CmdHF14ADesGetFileSettings(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -5796,7 +5779,6 @@ static int CmdHF14ADesChFileSettings(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -6331,7 +6313,6 @@ static int CmdHF14ADesCreateTrMACFile(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -6411,7 +6392,6 @@ static int CmdHF14ADesDeleteFile(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -6519,7 +6499,6 @@ static int CmdHF14ADesValueOperations(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication ( %s )" _RED_("failed") " Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -6724,7 +6703,6 @@ static int CmdHF14ADesClearRecordFile(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -7256,7 +7234,6 @@ static int CmdHF14ADesCreateMFCMapping(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, false, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -7684,7 +7661,6 @@ static int CmdHF14ADesReadData(const char *Cmd) { res = DesfireSelectAndAuthenticateW(&dctx, securechann, selectway, id, fileisoidpresent, fileisoid, noauth, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -7887,7 +7863,6 @@ static int CmdHF14ADesWriteData(const char *Cmd) { res = DesfireSelectAndAuthenticateW(&dctx, securechann, selectway, id, fileisoidpresent, fileisoid, noauth, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -8161,7 +8136,6 @@ static int CmdHF14ADesLsFiles(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -8309,7 +8283,6 @@ static int CmdHF14ADesDump(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } @@ -8329,7 +8302,6 @@ static int CmdHF14ADesDump(const char *Cmd) { res = DesfireSelectAndAuthenticateAppW(&dctx, securechann, selectway, id, noauth, verbose); if (res != PM3_SUCCESS) { DropField(); - PrintAndLogEx(FAILED, "Select or authentication %s " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(selectway, id), res, DesfireAuthErrorToStr(res)); return res; } } diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index 14ec978a4..23921a9af 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -258,7 +258,7 @@ const char *DesfireAuthErrorToStr(int error) { case 6: return "mbedtls_aes_setkey_enc failed"; case 7: - return "Sending auth command failed"; + return "Sending auth response (2nd frame) failed"; case 8: return "Authentication failed. Card timeout."; case 9: @@ -302,6 +302,13 @@ const char *DesfireAuthErrorToStr(int error) { default: break; } + + // the select / auth helpers hand PM3_E* codes straight back to the caller, + // so translate those here instead of returning an empty string + if (error < 0) { + return DesfireGetErrorString(error, NULL); + } + return ""; } @@ -327,7 +334,7 @@ char *DesfireWayIDStr(DesfireISOSelectWay way, uint32_t id) { if (way == ISWMF || way == ISWDFName) snprintf(str, sizeof(str), "%s", DesfireSelectWayToStr(way)); else - snprintf(str, sizeof(str), "%s %0*x", DesfireSelectWayToStr(way), (way == ISW6bAID) ? 6 : 4, id); + snprintf(str, sizeof(str), "%s %0*X", DesfireSelectWayToStr(way), (way == ISW6bAID) ? 6 : 4, id); return str; } @@ -1132,7 +1139,7 @@ int DesfireSelectAndAuthenticateEx(DesfireContext_t *dctx, DesfireSecureChannel if (aid == 0x000000) { res = DesfireAnticollision(verbose); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "Desfire anticollision " _RED_("fail")); + PrintAndLogEx(FAILED, "Desfire anticollision " _RED_("failed")); return 200; } @@ -1143,7 +1150,7 @@ int DesfireSelectAndAuthenticateEx(DesfireContext_t *dctx, DesfireSecureChannel } else { res = DesfireSelectAIDHex(dctx, aid, false, 0); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "Desfire select " _RED_("fail")); + PrintAndLogEx(FAILED, "Desfire %s select " _RED_("failed"), DesfireWayIDStr(ISW6bAID, aid)); return 200; } @@ -1160,15 +1167,16 @@ int DesfireSelectAndAuthenticateEx(DesfireContext_t *dctx, DesfireSecureChannel res = DesfireAuthenticate(dctx, secureChannel, verbose); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "Desfire authenticate " _RED_("fail") ". Result: [%d] %s", res, DesfireAuthErrorToStr(res)); + PrintAndLogEx(FAILED, "Desfire %s authentication " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(ISW6bAID, aid), res, DesfireAuthErrorToStr(res)); return res; } if (DesfireIsAuthenticated(dctx)) { if (verbose) { - PrintAndLogEx(INFO, "Desfire " _GREEN_("authenticated")); + PrintAndLogEx(INFO, "Desfire " _GREEN_("authenticated")); } } else { + PrintAndLogEx(FAILED, "Desfire %s authentication " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(ISW6bAID, aid), 201, DesfireAuthErrorToStr(201)); return 201; } } @@ -1195,7 +1203,7 @@ int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel s res = DesfireISOSelect(dctx, ISSDFName, dctx->selectedDFName, dctx->selectedDFNameLen, resp, &resplen); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "Desfire DF name select " _RED_("error")); + PrintAndLogEx(FAILED, "Desfire DF name select " _RED_("failed")); if (res == PM3_ECARDEXCHANGE || res == PM3_ETIMEOUT || res == PM3_ERFTRANS) { return res; } @@ -1216,7 +1224,7 @@ int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel s res = DesfireSelectAIDHex(dctx, id, false, 0); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "Desfire select " _RED_("error")); + PrintAndLogEx(FAILED, "Desfire %s select " _RED_("failed"), DesfireWayIDStr(ISW6bAID, id)); if (res == PM3_ECARDEXCHANGE || res == PM3_ETIMEOUT || res == PM3_ERFTRANS) { return res; } @@ -1232,7 +1240,7 @@ int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel s } else { res = DesfireSelectEx(dctx, false, way, id, NULL); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "Desfire %s select " _RED_("error"), DesfireSelectWayToStr(way)); + PrintAndLogEx(FAILED, "Desfire %s select " _RED_("failed"), DesfireWayIDStr(way, id)); if (res == PM3_ECARDEXCHANGE || res == PM3_ETIMEOUT || res == PM3_ERFTRANS) { return res; } @@ -1247,7 +1255,7 @@ int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel s // Also select by ISO ID if specified res = DesfireSelectEx(dctx, false, way, id, NULL); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "Desfire %s select " _RED_("error"), DesfireSelectWayToStr(way)); + PrintAndLogEx(FAILED, "Desfire %s select " _RED_("failed"), DesfireWayIDStr(way, id)); if (res == PM3_ECARDEXCHANGE || res == PM3_ETIMEOUT || res == PM3_ERFTRANS) { return res; } @@ -1266,7 +1274,7 @@ int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel s res = DesfireSelectAIDHex(dctx, id, false, 0); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "Desfire select " _RED_("error")); + PrintAndLogEx(FAILED, "Desfire %s select " _RED_("failed"), DesfireWayIDStr(ISW6bAID, id)); if (res == PM3_ECARDEXCHANGE || res == PM3_ETIMEOUT || res == PM3_ERFTRANS) { return res; } @@ -1282,7 +1290,7 @@ int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel s } else { res = DesfireSelectEx(dctx, true, way, id, NULL); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "Desfire %s select " _RED_("error"), DesfireSelectWayToStr(way)); + PrintAndLogEx(FAILED, "Desfire %s select " _RED_("failed"), DesfireWayIDStr(way, id)); if (res == PM3_ECARDEXCHANGE || res == PM3_ETIMEOUT || res == PM3_ERFTRANS) { return res; } @@ -1296,7 +1304,7 @@ int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel s if (selectfile) { res = DesfireSelectEx(dctx, false, ISWIsoID, isofileid, NULL); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "Desfire iso file select " _RED_("error")); + PrintAndLogEx(FAILED, "Desfire %s iso file %04x select " _RED_("failed"), DesfireWayIDStr(way, id), isofileid); if (res == PM3_ECARDEXCHANGE || res == PM3_ETIMEOUT || res == PM3_ERFTRANS) { return res; } @@ -1311,15 +1319,16 @@ int DesfireSelectAndAuthenticateW(DesfireContext_t *dctx, DesfireSecureChannel s if (noauth == false) { res = DesfireAuthenticate(dctx, secureChannel, verbose); if (res != PM3_SUCCESS) { - PrintAndLogEx(ERR, "Desfire authenticate " _RED_("error") ". Result: [%d] %s", res, DesfireAuthErrorToStr(res)); + PrintAndLogEx(FAILED, "Desfire %s authentication " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(way, id), res, DesfireAuthErrorToStr(res)); return res; } if (DesfireIsAuthenticated(dctx)) { if (verbose) { - PrintAndLogEx(INFO, "Desfire " _GREEN_("authenticated")); + PrintAndLogEx(INFO, "Desfire " _GREEN_("authenticated")); } } else { + PrintAndLogEx(FAILED, "Desfire %s authentication " _RED_("failed") ". Result [%d] %s", DesfireWayIDStr(way, id), 201, DesfireAuthErrorToStr(201)); return 201; } }