Merge branch 'master' into pm3-iclass-sio-unknown-tags

Signed-off-by: Iceman <iceman@iuse.se>
This commit is contained in:
Iceman authored and GitHub committed 2026-09-22 16:22:25 +07:00
commit f7dd78ebc5
23 files changed
+2895 -42

No files matched your search

+5 -3
View File
@@ -2994,9 +2994,11 @@ int infoHF14A(bool verbose, bool do_nack_test, bool do_aid_search) {
return PM3_ETIMEOUT;
}
memcpy(card.ats, resp.data.asBytes, rlen_2863);
card.ats_len = rlen_2863; // note: ats_len includes CRC Bytes
if (card.ats_len > 3) {
// Only a well formed ATS, TL plus the bytes TL counts plus CRC, means the
// PICC entered ISO14443-4. A 4 bit NAK to RATS is not an ATS.
if ((rlen_2863 > 3) && (resp.data.asBytes[0] == rlen_2863 - 2)) {
memcpy(card.ats, resp.data.asBytes, rlen_2863);
card.ats_len = rlen_2863; // note: ats_len includes CRC Bytes
select_status = 4;
}
}
+25 -10
View File
@@ -47,6 +47,7 @@
#include "parsers/parsevigik.h"
#include "parsers/parsehexact.h"
#include "parsers/parseproac.h"
#include "parsers/parserkf.h"
#include "generator.h" // keygens.
#include "fpga.h"
#include "mifare/mifarehost.h"
@@ -405,9 +406,11 @@ int mf_read_uid(uint8_t *uid, int *uidlen, int *nxptype) {
return PM3_ETIMEOUT;
}
memcpy(card.ats, resp.data.asBytes, rlen_398);
card.ats_len = rlen_398; // note: ats_len includes CRC Bytes
if (card.ats_len > 3) {
// Only a well formed ATS, TL plus the bytes TL counts plus CRC, means the
// PICC entered ISO14443-4. A 4 bit NAK to RATS is not an ATS.
if ((rlen_398 > 3) && (resp.data.asBytes[0] == rlen_398 - 2)) {
memcpy(card.ats, resp.data.asBytes, rlen_398);
card.ats_len = rlen_398; // note: ats_len includes CRC Bytes
select_status = 4;
}
}
@@ -895,6 +898,10 @@ static int mf_view_dump(uint8_t *dump, size_t bytes_read, uint16_t block_cnt, bo
(void)proac_parser_parse(dump, bytes_read);
}
if (is_valid_rkf_card(dump, bytes_read)) {
(void)rkf_parser_parse(dump, bytes_read);
}
return PM3_SUCCESS;
}
@@ -3199,9 +3206,11 @@ static int CmdHF14AMfAutoPWN(const char *Cmd) {
return PM3_ETIMEOUT;
}
memcpy(card.ats, resp.data.asBytes, rlen_3120);
card.ats_len = rlen_3120; // note: ats_len includes CRC Bytes
if (card.ats_len > 3) {
// Only a well formed ATS, TL plus the bytes TL counts plus CRC, means the
// PICC entered ISO14443-4. A 4 bit NAK to RATS is not an ATS.
if ((rlen_3120 > 3) && (resp.data.asBytes[0] == rlen_3120 - 2)) {
memcpy(card.ats, resp.data.asBytes, rlen_3120);
card.ats_len = rlen_3120; // note: ats_len includes CRC Bytes
select_status = 4;
}
}
@@ -8552,7 +8561,11 @@ static int CmdHF14AMfView(const char *Cmd) {
if (res != PM3_SUCCESS) {
return res;
}
return hexact_selftest();
res = hexact_selftest();
if (res != PM3_SUCCESS) {
return res;
}
return rkf_selftest();
}
if (fnlen == 0) {
@@ -11458,9 +11471,11 @@ static int CmdHF14AMfInfo(const char *Cmd) {
return PM3_ETIMEOUT;
}
memcpy(card.ats, resp.data.asBytes, rlen_11266);
card.ats_len = rlen_11266; // note: ats_len includes CRC Bytes
if (card.ats_len > 3) {
// Only a well formed ATS, TL plus the bytes TL counts plus CRC, means the
// PICC entered ISO14443-4. A 4 bit NAK to RATS is not an ATS.
if ((rlen_11266 > 3) && (resp.data.asBytes[0] == rlen_11266 - 2)) {
memcpy(card.ats, resp.data.asBytes, rlen_11266);
card.ats_len = rlen_11266; // note: ats_len includes CRC Bytes
select_status = 4;
}
}
+20 -4
View File
@@ -35,6 +35,7 @@
#include "crypto/duoxcrypto.h"
#include "protocols.h"
#include "parsers/parsehrt.h"
#include "parsers/parsenortic.h"
#include "cmdtrace.h"
#include "cliparser.h"
#include "iso7816/apduinfo.h" // APDU manipulation / errorcodes
@@ -9691,12 +9692,14 @@ static int CmdHF14ADesView(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "hf mfdes view",
"Print a DESFire card dump file (json)",
"hf mfdes view -f hf-mfdes-01020304050607-dump.json");
"hf mfdes view -f hf-mfdes-01020304050607-dump.json\n"
"hf mfdes view --selftest");
void *argtable[] = {
arg_param_begin,
arg_str1("f", "file", "<fn>", "Filename of dump"),
arg_str0("f", "file", "<fn>", "Filename of dump"),
arg_lit0("v", "verbose", "Verbose output"),
arg_lit0(NULL, "selftest", "Run the dump parsers self tests and exit"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, false);
@@ -9705,8 +9708,18 @@ static int CmdHF14ADesView(const char *Cmd) {
char filename[FILE_PATH_SIZE] = {0};
CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)filename, FILE_PATH_SIZE, &fnlen);
bool verbose = arg_get_lit(ctx, 2);
bool selftest = arg_get_lit(ctx, 3);
CLIParserFree(ctx);
if (selftest) {
return nortic_selftest();
}
if (fnlen == 0) {
PrintAndLogEx(ERR, "Specify a filename with `-f` or run `--selftest`");
return PM3_EINVARG;
}
desfire_dump_t *dump = calloc(1, sizeof(desfire_dump_t));
if (dump == NULL) {
PrintAndLogEx(ERR, "Failed to allocate memory");
@@ -9776,6 +9789,10 @@ static int CmdHF14ADesView(const char *Cmd) {
DesfireViewPrintApp(&dump->app[i]);
}
if (is_valid_nortic_card(dump)) {
(void)nortic_parser_parse(dump);
}
if (verbose) {
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(INFO, "Loaded `" _YELLOW_("%s") "`", filename);
@@ -11053,8 +11070,7 @@ static int CmdHF14ADesVerifyCert(const char *Cmd) {
arg_str0(NULL, "dfname", "<hex>", "Certificate application DF name (1-16 bytes)"), // 14
arg_str0(NULL, "fid", "<hex>", "Certificate file ID (1 byte)"), // 15
arg_lit0(NULL, "no-auth", "Read certificate file without authentication"), // 16
arg_strn(NULL, "ca", "<name|cert|pubkey|path|skip>", 0, MFDES_VERIFYCERT_MAX_CAS,
"CA input, or `skip` to skip certificate signature validation. Repeat --ca for multiple entries"), // 17
arg_strn(NULL, "ca", "<name|cert|pubkey|path|skip>", 0, MFDES_VERIFYCERT_MAX_CAS, "CA input, or `skip` to skip validation"), // 17
arg_str0(NULL, "keyaid", "<hex>", "Key application ID (default: cert app)"), // 18
arg_str0(NULL, "keyisoid", "<hex>", "Key application ISO DF ID (2 bytes)"), // 19
arg_str0(NULL, "keydfname", "<hex>", "Key application DF name (default: cert app)"), // 20
+44 -10
View File
@@ -65,6 +65,14 @@
#define MAX_ST25TN512 0x3F
#define MAX_ST25TN01K 0x3F
// UL-AES fixed memory map, MF0AES(H)20
#define MFU_AES_USER_FIRST 0x04
#define MFU_AES_USER_LAST 0x27
#define MFU_AES_LOCK_PAGE 0x28
#define MFU_AES_CFG0_PAGE 0x29
#define MFU_AES_CFG1_PAGE 0x2A
#define MFU_AES_KEY0_PAGE 0x30
#define MIFAREU3P_KEY_SIZE 16
#define MIFAREULC_KEY_INDEX 3
#define MFU_DEFAULT_KEY_DIC "mfulc_default_keys.dic"
@@ -4043,6 +4051,7 @@ static int CmdHF14AMfURestore(const char *Cmd) {
"hf mfu restore -f myfile -s -> special write\n"
"hf mfu restore -f myfile -k AABBCCDD -s -> special write, use key\n"
"hf mfu restore -f myfile -k AABBCCDD -ser -> special write, use key, write dump pwd, ...\n"
"hf mfu restore -f myfile -k 00112233445566778899AABBCCDDEEFF -> UL-C / UL-AES, use key\n"
"\n"
"Note: Restoring a NTAG/UL dump to a UMC will likely result in incorrect PWD and PACK\n"
);
@@ -4050,7 +4059,7 @@ static int CmdHF14AMfURestore(const char *Cmd) {
void *argtable[] = {
arg_param_begin,
arg_str1("f", "file", "<fn>", "Specify a filename for dump file"),
arg_str0("k", "key", "<hex>", "key for authentication (UL-C 16 bytes, EV1/NTAG 4 bytes)"),
arg_str0("k", "key", "<hex>", "key for authentication (UL-C/UL-AES 16 bytes, EV1/NTAG 4 bytes)"),
arg_lit0("l", NULL, "swap entered key's endianness"),
arg_lit0("s", NULL, "enable special write UID -MAGIC TAG ONLY-"),
arg_lit0("e", NULL, "enable special write version/signature -MAGIC NTAG 21* ONLY-"),
@@ -4142,6 +4151,8 @@ static int CmdHF14AMfURestore(const char *Cmd) {
return PM3_ESOFT;
}
bool is_ulaes = ((tagtype & MFU_TT_UL_AES) == MFU_TT_UL_AES);
if ((tagtype & MFU_TT_UL_C) == MFU_TT_UL_C) {
if ((has_key == true) && (ak_len != 16)) {
PrintAndLogEx(ERR, "UL-C key must be 16 bytes");
@@ -4172,12 +4183,6 @@ static int CmdHF14AMfURestore(const char *Cmd) {
}
}
if ((tagtype & MFU_TT_UL_AES) == MFU_TT_UL_AES) {
PrintAndLogEx(ERR, "Sorry, UL-AES not yet supported. Feel free to implement!");
free(dump);
return PM3_ENOTIMPL;
}
PrintAndLogEx(INFO, "Restoring " _YELLOW_("%s")" to card", filename);
mfu_print_dump(mem, pages, 0, dense_output);
@@ -4279,7 +4284,19 @@ static int CmdHF14AMfURestore(const char *Cmd) {
// write all other data
// Skip block 0,1,2,3 (only magic tags can write to them)
// Skip last 5 blocks usually is configuration
for (uint8_t b = 4; b < pages - 5; b++) {
uint8_t first_page = 4;
uint8_t last_page = pages - 5;
if (is_ulaes) {
// UL-AES user memory stops before the lock, config and key pages
first_page = MFU_AES_USER_FIRST;
last_page = MFU_AES_USER_LAST + 1;
if (pages < last_page) {
last_page = pages;
}
}
for (uint8_t b = first_page; b < last_page; b++) {
//Send write Block
memcpy(packetw.data, mem->data + (b * 4), 4);
@@ -4306,8 +4323,19 @@ static int CmdHF14AMfURestore(const char *Cmd) {
// otp, uid, lock, dynlockbits, cfg0, cfg1
uint8_t blocks[] = {3, 0, 1, 2, pages - 5, pages - 4, pages - 3};
#endif
for (uint8_t i = 0; i < ARRAYLEN(blocks); i++) {
uint8_t b = blocks[i];
// otp, uid, lock, cfg0, cfg1.
// RFU pages (0x2B, 0x2C) and the key lock bits (0x2D) are left alone, they are one way
uint8_t ulaes_blocks[] = {3, 0, 1, 2, MFU_AES_LOCK_PAGE, MFU_AES_CFG0_PAGE, MFU_AES_CFG1_PAGE};
uint8_t *pblocks = blocks;
uint8_t blockcnt = ARRAYLEN(blocks);
if (is_ulaes) {
pblocks = ulaes_blocks;
blockcnt = ARRAYLEN(ulaes_blocks);
}
for (uint8_t i = 0; i < blockcnt; i++) {
uint8_t b = pblocks[i];
memcpy(packetw.data, mem->data + (b * 4), 4);
packetw.block_no = b;
clearCommandBuffer();
@@ -4319,6 +4347,12 @@ static int CmdHF14AMfURestore(const char *Cmd) {
DropField();
free(dump);
if (is_ulaes) {
PrintAndLogEx(INFO, "Key pages 0x%02X..0x%02X not restored", MFU_AES_KEY0_PAGE, MAX_UL_AES);
PrintAndLogEx(HINT, "Hint: Try `" _YELLOW_("hf mfu setkey -k <16 hex bytes>") "` to restore the AES keys");
}
PrintAndLogEx(HINT, "Hint: Try `" _YELLOW_("hf mfu dump --ns") "` to verify");
PrintAndLogEx(INFO, "Done!");
return PM3_SUCCESS;
+3 -4
View File
@@ -1940,7 +1940,7 @@ static int CmdLFHitagEload(const char *Cmd) {
arg_lit0("1", "ht1", "Card type Hitag 1"),
arg_lit0("2", "ht2", "Card type Hitag 2"),
arg_lit0("s", "hts", "Card type Hitag S"),
arg_lit0("m", "htm", "Card type Hitag \xce\xbc"), // μ
arg_lit0("m", "htm", "Card type Hitag µ"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, false);
@@ -1990,7 +1990,7 @@ static int CmdLFHitagEload(const char *Cmd) {
} else if (use_htm) {
// a u dump is sized by the tag's ICR, so accept anything that fits
expected = 0;
tname = "Hitag \xce\xbc";
tname = "Hitag µ";
}
if (use_htm) {
@@ -2053,7 +2053,7 @@ static int CmdLFHitagEview(const char *Cmd) {
arg_lit0("1", "ht1", "Card type Hitag 1"),
arg_lit0("2", "ht2", "Card type Hitag 2 (default)"),
arg_lit0("s", "hts", "Card type Hitag S"),
arg_lit0("m", "htm", "Card type Hitag \xce\xbc"),
arg_lit0("m", "htm", "Card type Hitag µ"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
@@ -2933,4 +2933,3 @@ int CmdLFHitag(const char *Cmd) {
return CmdsParse(CommandTable, Cmd);
}
+101
View File
@@ -795,6 +795,44 @@ void des_decrypt_cbc(void *out, const void *in, const int length, const void *ke
mbedtls_des_crypt_cbc(&ctx, MBEDTLS_DES_DECRYPT, length, iv, in, out);
}
// FIPS PUB 113 - Computer Data Authentication.
// https://csrc.nist.gov/files/pubs/fips/113/final/docs/fips113.pdf
int des_mac(const void *key, const void *input, size_t length, uint8_t *mac, size_t mac_len) {
if (key == NULL || mac == NULL || mac_len == 0 || mac_len > 8) {
return PM3_EINVARG;
}
if (length && input == NULL) {
return PM3_EINVARG;
}
// zero pad up to a whole number of blocks, an empty message is one block
size_t n = (length + 7) & ~(size_t)7;
if (n == 0) {
n = 8;
}
uint8_t *buf = calloc(n * 2, sizeof(uint8_t));
if (buf == NULL) {
return PM3_EMALLOC;
}
uint8_t *out = buf + n;
if (length) {
memcpy(buf, input, length);
}
uint8_t iv[8] = {0};
des_encrypt_cbc(out, buf, (int)n, key, iv);
// the MAC is the leading bits of the last cipher block
memcpy(mac, out + n - 8, mac_len);
free(buf);
return PM3_SUCCESS;
}
void des3_encrypt(void *out, const void *in, const void *key, uint8_t keycount) {
switch (keycount) {
case 1:
@@ -1280,6 +1318,69 @@ int ecdsa_signature_r_s_verify(mbedtls_ecp_group_id curveid, uint8_t *key_xy, ui
#define T_R "2B42F576D07F4165FF65D1F3B1500F81E44C316F1F0B3EF57325B69ACA46104F"
#define T_S "DC42C2122D6392CD3E3A993A89502A8198C1886FE69D262C4B329BDB6B63FAF1"
static int des_mac_test_one(const char *name, const uint8_t key[8], const uint8_t *data,
size_t len, const uint8_t *expect, size_t expect_len, bool verbose) {
uint8_t mac[8] = {0};
int res = des_mac(key, data, len, mac, expect_len);
if (res != PM3_SUCCESS) {
return res;
}
if (memcmp(mac, expect, expect_len) != 0) {
if (verbose) {
PrintAndLogEx(FAILED, " %s got %s", name, sprint_hex_inrow(mac, expect_len));
}
return PM3_ESOFT;
}
if (verbose) {
PrintAndLogEx(INFO, " %s %s ( %s )", name, sprint_hex_inrow(mac, expect_len), _GREEN_("ok"));
}
return PM3_SUCCESS;
}
int des_mac_test(bool verbose) {
if (verbose) {
PrintAndLogEx(INFO, "DES-MAC FIPS 113 test");
} else {
PrintAndLogEx(INFO, "DES-MAC FIPS 113 test " NOLF);
}
const uint8_t key[8] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};
// the worked example of FIPS PUB 113, which publishes the 32 bit MAC
const uint8_t msg[] = "7654321 Now is the time for ";
const uint8_t full[8] = {0xF1, 0xD3, 0x0F, 0x68, 0x49, 0x31, 0x2C, 0xA4};
int res = des_mac_test_one("FIPS 113, 64 bit", key, msg, sizeof(msg) - 1, full, 8, verbose);
if (res == PM3_SUCCESS) {
res = des_mac_test_one("FIPS 113, 32 bit", key, msg, sizeof(msg) - 1, full, 4, verbose);
}
if (res == PM3_SUCCESS) {
res = des_mac_test_one("FIPS 113, 16 bit", key, msg, sizeof(msg) - 1, full, 2, verbose);
}
// one block of zero padding only
if (res == PM3_SUCCESS) {
const uint8_t empty[8] = {0xD5, 0xD4, 0x4F, 0xF7, 0x20, 0x68, 0x3D, 0x0D};
res = des_mac_test_one("empty message...", key, NULL, 0, empty, 8, verbose);
}
// exactly one block, so no padding is added
if (res == PM3_SUCCESS) {
const uint8_t one[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
const uint8_t want[8] = {0xCA, 0xDB, 0x67, 0x82, 0xEE, 0x2B, 0x48, 0x23};
res = des_mac_test_one("one whole block.", key, one, sizeof(one), want, 8, verbose);
}
if (verbose == false) {
PrintAndLogEx(NORMAL, (res == PM3_SUCCESS) ? "( " _GREEN_("ok") " )" : "( " _RED_("fail") " )");
}
return res;
}
int ecdsa_nist_test(bool verbose) {
int res;
uint8_t input[] = "Example of ECDSA with P-256";
+9
View File
@@ -49,6 +49,15 @@ 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);
// desMAC according to [FIPS 113]. The data is zero padded to a multiple of the
// DES block size and encrypted in CBC mode with a zero IV; the MAC is the most
// significant mac_len bytes of the final cipher block. key is 8 bytes, mac_len
// is 1..8.
int des_mac(const void *key, const void *input, size_t length, uint8_t *mac, size_t mac_len);
// Check des_mac against the worked example of [FIPS 113]
int des_mac_test(bool verbose);
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);
+6
View File
@@ -84,6 +84,12 @@ int ExecuteCryptoTests(bool verbose, bool ignore_time, bool include_slow_tests)
res = mbedtls_cmac_self_test(verbose);
if (res) TestFail = true;
res = des_mac_test(verbose);
if (res) {
return res;
}
PrintAndLogEx(NORMAL, "");
res = ecdsa_nist_test(verbose);
if (res) TestFail = true;
+422
View File
@@ -0,0 +1,422 @@
//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------
// NORTIC travel card parser for MIFARE DESFire dumps
//-----------------------------------------------------------------------------
#include "parsenortic.h"
#include <stdio.h> // snprintf
#include <string.h>
#include "commonutil.h" // ARRAYLEN
#include "ui.h" // PrintAndLogEx
#include "util.h" // sprint_hex_inrow
#define NORTIC_CI_HEADER_SIZE 16
// Days from 1970-01-01 to the EN 1545 date epoch of 1997-01-01
#define NORTIC_DAYS_TO_1997 9862
#define NORTIC_LABEL "%-22s"
//-----------------------------------------------------------------------------
// Bit access
//
// EN 1545 packs fields most significant bit first, so bit 0 is the top bit of
// byte 0. This is the opposite of the RKF Type CL-1 layout in parserkf.c, where
// RKF-0022 section 7.4.1 numbers bits from the least significant end.
//-----------------------------------------------------------------------------
static uint64_t nortic_bits(const uint8_t *buf, uint16_t off, uint8_t len) {
uint64_t v = 0;
for (uint8_t k = 0; k < len; k++) {
uint16_t b = off + k;
v = (v << 1) | ((buf[b >> 3] >> (7 - (b & 7))) & 1);
}
return v;
}
static void nortic_set_bits(uint8_t *buf, uint16_t off, uint8_t len, uint64_t v) {
for (uint8_t k = 0; k < len; k++) {
uint16_t b = off + k;
uint8_t bit = (v >> (len - 1 - k)) & 1;
if (bit) {
buf[b >> 3] |= (uint8_t)(1 << (7 - (b & 7)));
} else {
buf[b >> 3] &= (uint8_t)~(1 << (7 - (b & 7)));
}
}
}
//-----------------------------------------------------------------------------
// Card issuer header, file 0C of AID 578000, 16 bytes
//-----------------------------------------------------------------------------
#define NORTIC_OFF_COUNTRY 0
#define NORTIC_OFF_FORMAT 10
#define NORTIC_OFF_CHOICE 30
#define NORTIC_OFF_SERIAL 32
#define NORTIC_OFF_VALIDITY 64
#define NORTIC_OFF_OWNER 78
#define NORTIC_OFF_RETAILER 98
#define NORTIC_OFF_KEYVERSION 118
#define NORTIC_OFF_UNUSED 122
typedef struct {
uint16_t country;
uint32_t format;
uint8_t choice;
uint32_t serial;
uint16_t validity_end;
uint32_t owner;
uint32_t retailer;
uint8_t key_version;
uint8_t unused;
} nortic_ci_t;
static void nortic_read_ci(const uint8_t *d, nortic_ci_t *c) {
c->country = (uint16_t)nortic_bits(d, NORTIC_OFF_COUNTRY, 10);
c->format = (uint32_t)nortic_bits(d, NORTIC_OFF_FORMAT, 20);
c->choice = (uint8_t)nortic_bits(d, NORTIC_OFF_CHOICE, 2);
c->serial = (uint32_t)nortic_bits(d, NORTIC_OFF_SERIAL, 32);
c->validity_end = (uint16_t)nortic_bits(d, NORTIC_OFF_VALIDITY, 14);
c->owner = (uint32_t)nortic_bits(d, NORTIC_OFF_OWNER, 20);
c->retailer = (uint32_t)nortic_bits(d, NORTIC_OFF_RETAILER, 20);
c->key_version = (uint8_t)nortic_bits(d, NORTIC_OFF_KEYVERSION, 4);
c->unused = (uint8_t)nortic_bits(d, NORTIC_OFF_UNUSED, 6);
}
//-----------------------------------------------------------------------------
// Value decoding
//-----------------------------------------------------------------------------
static void nortic_civil_from_days(int32_t z, int *year, unsigned *month, unsigned *day) {
z += 719468;
int32_t era = ((z >= 0) ? z : (z - 146096)) / 146097;
uint32_t doe = (uint32_t)(z - (era * 146097));
uint32_t yoe = (doe - (doe / 1460) + (doe / 36524) - (doe / 146096)) / 365;
int32_t y = (int32_t)yoe + (era * 400);
uint32_t doy = doe - ((365 * yoe) + (yoe / 4) - (yoe / 100));
uint32_t mp = ((5 * doy) + 2) / 153;
*day = doy - (((153 * mp) + 2) / 5) + 1;
*month = (mp < 10) ? (mp + 3) : (mp - 9);
*year = y + ((*month <= 2) ? 1 : 0);
}
// EN 1545 date, day 0 is 1 January 1997
static void nortic_date_str(uint16_t v, char *out, size_t outlen) {
int y;
unsigned m, d;
nortic_civil_from_days((int32_t)v + NORTIC_DAYS_TO_1997, &y, &m, &d);
snprintf(out, outlen, "%04d-%02u-%02u", y, m, d);
}
static const char *nortic_country_name(uint16_t code) {
switch (code) {
case 208:
return "Denmark";
case 246:
return "Finland";
case 578:
return "Norway";
case 752:
return "Sweden";
default:
return NULL;
}
}
// The choice bitmap says which optional card identifier is present
static const char *nortic_choice_name(uint8_t choice) {
switch (choice) {
case 0:
return "none";
case 1:
return "RFU";
case 2:
return "cardIDNumber32bits";
default:
return "RFU";
}
}
//-----------------------------------------------------------------------------
// Transport application file map
//-----------------------------------------------------------------------------
static const struct {
uint8_t fid;
const char *name;
const char *note;
} nortic_transport_files[] = {
{0x01, "Product Retailer", "384 bytes, written with key 4"},
{0x02, "Service Provider", "128 bytes, written with key 6"},
{0x03, "Special Event", "288 bytes, written with key 6"},
{0x04, "Stored Value", "value file, written with key 5"},
{0x05, "General Event Log", "cyclic, 36 bytes per record"},
{0x06, "SV Reload Log", "cyclic, 32 bytes per record"},
{0x0A, "Environment", "32 bytes, written with key 2"},
{0x0C, "Card Holder", "32 bytes, written with key 3"},
};
static const char *nortic_transport_file_name(uint8_t fid) {
for (size_t i = 0; i < ARRAYLEN(nortic_transport_files); i++) {
if (nortic_transport_files[i].fid == fid) {
return nortic_transport_files[i].name;
}
}
return NULL;
}
//-----------------------------------------------------------------------------
// Lookup helpers
//-----------------------------------------------------------------------------
static const desfire_dump_app_t *nortic_find_app(const desfire_dump_t *dump, uint32_t aid) {
if (dump == NULL) {
return NULL;
}
for (uint8_t i = 0; i < dump->appcount && i < DESFIRE_MAX_APP_COUNT; i++) {
if (dump->app[i].aid == aid) {
return &dump->app[i];
}
}
return NULL;
}
static const desfire_dump_file_t *nortic_find_file(const desfire_dump_app_t *app, uint8_t fid) {
if (app == NULL) {
return NULL;
}
for (uint8_t i = 0; i < app->filecount && i < DESFIRE_MAX_FILE_COUNT; i++) {
if (app->files[i].num == fid) {
return &app->files[i];
}
}
return NULL;
}
//-----------------------------------------------------------------------------
// Detection
//-----------------------------------------------------------------------------
bool is_valid_nortic_card(const desfire_dump_t *dump) {
return (nortic_find_app(dump, NORTIC_AID_CARD_ISSUER) != NULL);
}
//-----------------------------------------------------------------------------
// Printing
//-----------------------------------------------------------------------------
static void nortic_print_ci(const nortic_ci_t *c) {
char buf[32];
const char *country = nortic_country_name(c->country);
PrintAndLogEx(INFO, "");
PrintAndLogEx(INFO, "--- " _CYAN_("Card issuer header") " --- AID %06X file %02X",
NORTIC_AID_CARD_ISSUER, NORTIC_FID_CI_HEADER);
PrintAndLogEx(INFO, NORTIC_LABEL " %u ( %s )", "Country code", c->country,
country ? country : "unknown");
PrintAndLogEx(INFO, NORTIC_LABEL " %u", "Format", c->format);
PrintAndLogEx(INFO, NORTIC_LABEL " %u ( %s )", "Choice bitmap", c->choice,
nortic_choice_name(c->choice));
PrintAndLogEx(INFO, NORTIC_LABEL " " _YELLOW_("%u") " ( %08X )", "Card serial number",
c->serial, c->serial);
nortic_date_str(c->validity_end, buf, sizeof(buf));
PrintAndLogEx(INFO, NORTIC_LABEL " " _YELLOW_("%s") " ( day %u )", "Valid until", buf,
c->validity_end);
PrintAndLogEx(INFO, NORTIC_LABEL " %u", "App owner company", c->owner);
PrintAndLogEx(INFO, NORTIC_LABEL " %u", "Retailer organisation", c->retailer);
PrintAndLogEx(INFO, NORTIC_LABEL " %u", "Card key version", c->key_version);
if (c->unused != 0) {
PrintAndLogEx(INFO, NORTIC_LABEL " %u ( " _YELLOW_("expected 0") " )", "Unused bits", c->unused);
}
}
static void nortic_print_transport(const desfire_dump_app_t *app) {
PrintAndLogEx(INFO, "");
PrintAndLogEx(INFO, "--- " _CYAN_("Transport application") " --- AID %06X", NORTIC_AID_TRANSPORT);
if (app == NULL) {
PrintAndLogEx(INFO, "Not present on this card");
return;
}
PrintAndLogEx(INFO, " fid | file | read");
PrintAndLogEx(INFO, "-----+---------------------+---------------------------------------");
for (uint8_t i = 0; i < app->filecount && i < DESFIRE_MAX_FILE_COUNT; i++) {
const desfire_dump_file_t *f = &app->files[i];
const char *name = nortic_transport_file_name(f->num);
PrintAndLogEx(INFO, " %02X | %-19s | %s", f->num, name ? name : "unknown",
f->read_ok ? _GREEN_("read") : _YELLOW_("not read, needs key 7"));
}
PrintAndLogEx(INFO, "-----+---------------------+---------------------------------------");
// Contents are only printed as hex. The field layouts of these files are in
// Handbok V821 Del 18, which is not published.
for (uint8_t i = 0; i < app->filecount && i < DESFIRE_MAX_FILE_COUNT; i++) {
const desfire_dump_file_t *f = &app->files[i];
if (f->read_ok == false || f->data == NULL || f->datalen == 0) {
continue;
}
const char *name = nortic_transport_file_name(f->num);
PrintAndLogEx(INFO, "");
PrintAndLogEx(INFO, "file %02X %s %u bytes " _YELLOW_("not decoded"), f->num,
name ? name : "unknown", f->datalen);
for (uint32_t off = 0; off < f->datalen; off += 16) {
uint32_t n = (f->datalen - off < 16) ? (f->datalen - off) : 16;
PrintAndLogEx(INFO, " %04X %s", off, sprint_hex_inrow(f->data + off, n));
}
}
}
//-----------------------------------------------------------------------------
// Top level
//-----------------------------------------------------------------------------
int nortic_parser_parse(const desfire_dump_t *dump) {
const desfire_dump_app_t *ci = nortic_find_app(dump, NORTIC_AID_CARD_ISSUER);
if (ci == NULL) {
return PM3_EINVARG;
}
PrintAndLogEx(INFO, "");
PrintAndLogEx(INFO, "--- " _CYAN_("NORTIC travel card") " ------------------------");
PrintAndLogEx(INFO, "Norwegian Ticketing Interoperable Concept");
const desfire_dump_file_t *f = nortic_find_file(ci, NORTIC_FID_CI_HEADER);
if (f == NULL) {
PrintAndLogEx(INFO, "");
PrintAndLogEx(INFO, "Card issuer header (file %02X) is not in this dump",
NORTIC_FID_CI_HEADER);
} else if (f->read_ok == false || f->data == NULL) {
PrintAndLogEx(INFO, "");
PrintAndLogEx(INFO, "Card issuer header was not read. It needs no key, so retry the dump");
} else if (f->datalen < NORTIC_CI_HEADER_SIZE) {
PrintAndLogEx(INFO, "");
PrintAndLogEx(INFO, "Card issuer header is %u bytes, expected %u", f->datalen,
NORTIC_CI_HEADER_SIZE);
} else {
nortic_ci_t c;
nortic_read_ci(f->data, &c);
nortic_print_ci(&c);
if (c.country != NORTIC_COUNTRY_NORWAY) {
PrintAndLogEx(INFO, "");
PrintAndLogEx(WARNING, "Country code is %u, not %u. The layout below is the",
c.country, NORTIC_COUNTRY_NORWAY);
PrintAndLogEx(WARNING, "Norwegian one and may not hold.");
}
}
nortic_print_transport(nortic_find_app(dump, NORTIC_AID_TRANSPORT));
PrintAndLogEx(INFO, "");
PrintAndLogEx(INFO, "Transport application layouts are in Handbok V821 Del 18, which is");
PrintAndLogEx(INFO, "not published. Its files also need read key 7, which is not public");
return PM3_SUCCESS;
}
//-----------------------------------------------------------------------------
// Self test
//-----------------------------------------------------------------------------
static bool nortic_check(const char *what, uint64_t got, uint64_t want) {
bool ok = (got == want);
PrintAndLogEx(INFO, " %-24s %12llu ( %s )", what, (unsigned long long)got,
ok ? _GREEN_("ok") : _RED_("fail"));
return ok;
}
int nortic_selftest(void) {
PrintAndLogEx(INFO, "");
PrintAndLogEx(INFO, "Testing NORTIC card issuer header decode");
// The published card issuer header, with the serial number filled in where
// the capture redacted it
const uint8_t ci[NORTIC_CI_HEADER_SIZE] = {
0x90, 0x80, 0x00, 0x02, 0xAA, 0xBB, 0xCC, 0xDD,
0x6C, 0x68, 0x00, 0x28, 0x00, 0x02, 0x80, 0x40
};
nortic_ci_t c;
nortic_read_ci(ci, &c);
bool ok = true;
ok &= nortic_check("country code", c.country, NORTIC_COUNTRY_NORWAY);
ok &= nortic_check("format", c.format, 0);
ok &= nortic_check("choice bitmap", c.choice, 2);
ok &= nortic_check("card serial number", c.serial, 0xAABBCCDD);
ok &= nortic_check("validity end day", c.validity_end, 6938);
ok &= nortic_check("app owner company", c.owner, 160);
ok &= nortic_check("retailer organisation", c.retailer, 160);
ok &= nortic_check("card key version", c.key_version, 1);
ok &= nortic_check("unused bits", c.unused, 0);
char buf[32];
nortic_date_str(c.validity_end, buf, sizeof(buf));
bool dok = (strcmp(buf, "2015-12-31") == 0);
PrintAndLogEx(INFO, " %-24s %12s ( %s )", "validity end date", buf,
dok ? _GREEN_("ok") : _RED_("fail"));
ok &= dok;
// EN 1545 is most significant bit first. Reading it the RKF way gives 144,
// so a wrong bit order cannot pass unnoticed.
uint64_t lsb = 0;
for (uint8_t k = 0; k < 10; k++) {
if ((ci[k >> 3] >> (k & 7)) & 1) {
lsb |= (uint64_t)1 << k;
}
}
ok &= nortic_check("lsb first would give", lsb, 144);
// round trip through the encoder
uint8_t enc[NORTIC_CI_HEADER_SIZE] = {0};
nortic_set_bits(enc, NORTIC_OFF_COUNTRY, 10, c.country);
nortic_set_bits(enc, NORTIC_OFF_FORMAT, 20, c.format);
nortic_set_bits(enc, NORTIC_OFF_CHOICE, 2, c.choice);
nortic_set_bits(enc, NORTIC_OFF_SERIAL, 32, c.serial);
nortic_set_bits(enc, NORTIC_OFF_VALIDITY, 14, c.validity_end);
nortic_set_bits(enc, NORTIC_OFF_OWNER, 20, c.owner);
nortic_set_bits(enc, NORTIC_OFF_RETAILER, 20, c.retailer);
nortic_set_bits(enc, NORTIC_OFF_KEYVERSION, 4, c.key_version);
nortic_set_bits(enc, NORTIC_OFF_UNUSED, 6, c.unused);
bool rok = (memcmp(enc, ci, NORTIC_CI_HEADER_SIZE) == 0);
PrintAndLogEx(INFO, " %-24s %12s ( %s )", "re-encodes to the same 16 B",
sprint_hex_inrow(enc, 4), rok ? _GREEN_("ok") : _RED_("fail"));
ok &= rok;
PrintAndLogEx(INFO, "");
PrintAndLogEx(INFO, "NORTIC self test ( %s )", ok ? _GREEN_("ok") : _RED_("fail"));
return ok ? PM3_SUCCESS : PM3_ESOFT;
}
+48
View File
@@ -0,0 +1,48 @@
//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------
// NORTIC travel card parser for MIFARE DESFire dumps
//
// Norwegian Ticketing Interoperable Concept, the national public transport card
// specified by Statens vegvesen and now Jernbanedirektoratet, Handbok V821.
// Fields are EN 1545 coded.
//-----------------------------------------------------------------------------
#ifndef PARSENORTIC_H__
#define PARSENORTIC_H__
#include "common.h"
#include "fileutils.h" // desfire_dump_t
// Card Issuer application. Its header file is free to read, no key needed.
#define NORTIC_AID_CARD_ISSUER 0x578000
#define NORTIC_FID_CI_HEADER 0x0C
// Transport application. Every file needs the read key, number 7.
#define NORTIC_AID_TRANSPORT 0x578001
// ISO 3166 numeric code carried in the first field of the card issuer header
#define NORTIC_COUNTRY_NORWAY 578
// True when the dump carries the NORTIC card issuer application
bool is_valid_nortic_card(const desfire_dump_t *dump);
// Decode the card issuer header and name what the transport application holds
int nortic_parser_parse(const desfire_dump_t *dump);
// Check the decoder against the published card issuer header
int nortic_selftest(void);
#endif
File diff suppressed because it is too large. Load diff
+42
View File
@@ -0,0 +1,42 @@
//-----------------------------------------------------------------------------
// 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.
//-----------------------------------------------------------------------------
// RKF travel card parser for MIFARE Classic dumps
//
// Resekortsforeningen i Norden, "RKF Type CL-1", the Nordic public transport
// travel card on MIFARE Classic 1K. Layout per RKF-0022 1.0 and RKF-0023 2.00.
//-----------------------------------------------------------------------------
#ifndef PARSERKF_H__
#define PARSERKF_H__
#include "common.h"
// TCAS: Travel Card Applications Status, identifier of the first instance,
// always in block 2 of sector 0
#define RKF_TCAS_IDENTIFIER 0xA0
// True when sector 0 carries a travel card applications status block and either
// the card information block or the directory corroborates it
bool is_valid_rkf_card(const uint8_t *dump, size_t dumplen);
// Decode the support layer and walk the directory, printing every application
// object the specification pins down
int rkf_parser_parse(const uint8_t *dump, size_t dumplen);
// Build a card from the encoder and check the decoder reads it back
int rkf_selftest(void);
#endif