hf mf: add RKF travel card parser
The RKF travel card (Resekortsföreningen i Norden) is the Nordic public
transport card, specified 2001-2002 on MIFARE Classic and deployed by
Västtrafik, SL Stockholm, Länstrafiken and the Danish Rejsekort.
Decodes the card issuer and support layers (CMI, TCCI, TCAS, TCDI) and
every application object the specification pins down: TCPU purse, TCEL
event log, TCDB, TCCP, TCST, and TCTI/TCCO through an identifier chain
walker covering all 18 data element groups. AID owners are named from
RKF-0019.
Three things the specification does not state, determined from 21 cards:
- RKF-0022 7.7.1 calls the block checksum 'standard CRC-8' without
naming the parameters. It is CRC-8/MIFARE-MAD, poly 0x1D init 0xC7,
which the tree already ships as CRC8Mad().
- The MAC trio is the final 24 bits of a MACed object. The RKF-0023
tables list Free (bits) as a trailing summary row, which reads as if
the authenticator follows a gap after the key identifier; it does
not. Measured on the fact that MACAlgorithmIdentifier must be 0:
TCST 42/42 against 15/42, TCCP 20/20 against 13/20.
- TCPU version 4 drops EndDate and moves Value to bit 16.
Cards reporting a version other than 2 are flagged, and a MAC algorithm
the specification does not define is reported as a layout failure rather
than printed as data.
MAC values are not verified. The master key is in RKF-0018, which was
never published. des_mac() is added to libpcrypto for when that changes,
with the FIPS 113 worked example as its test.
Verified against a Danish Rejsekort whose provenance notes give the
purchase timestamp, card number and balance; the parser reproduces all
three. 48 self test checks on 'hf mf view --selftest', no false
positives across 460 local dumps.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
82597a32c5
commit
28d49510e2
10 files changed
+2197
-1
No files matched your search
@@ -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]
|
||||
- Added `hf mf view` - decodes RKF travel cards, the Nordic public transport card (Resekortsföreningen i Norden) on MIFARE Classic (@iceman1001)
|
||||
- Fixed `hf 14a info` now correctly handles NAK when receiving RATS (@iceman1001)
|
||||
- Fixed `hf mfu restore` to support Ultralight AES (@iceman1001)
|
||||
- Fixed `hf mfu info` to print UL-AES config pages again (@iceman1001)
|
||||
|
||||
@@ -514,6 +514,7 @@ set (TARGET_SOURCES
|
||||
${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/parserkf.c
|
||||
${PM3_ROOT}/client/src/parsers/hrtparser/hrtparser.c
|
||||
${PM3_ROOT}/client/src/pla.c
|
||||
${PM3_ROOT}/client/src/preferences.c
|
||||
@@ -521,7 +522,6 @@ set (TARGET_SOURCES
|
||||
${PM3_ROOT}/client/src/pm3_binlib.c
|
||||
${PM3_ROOT}/client/src/pm3_bitlib.c
|
||||
${PM3_ROOT}/client/src/pm3_dsp.c
|
||||
|
||||
${PM3_ROOT}/client/src/pm3_fit.c
|
||||
${PM3_ROOT}/client/src/pm3line.c
|
||||
${PM3_ROOT}/client/src/pm3line_vocabulary.c
|
||||
|
||||
@@ -912,6 +912,7 @@ SRCS = mifare/aiddesfire.c \
|
||||
parsers/parsehexact.c \
|
||||
parsers/parsemykey.c \
|
||||
parsers/parseproac.c \
|
||||
parsers/parserkf.c \
|
||||
parsers/hrtparser/hrtparser.c \
|
||||
pm3.c \
|
||||
pm3_binlib.c \
|
||||
|
||||
@@ -441,6 +441,7 @@ set (TARGET_SOURCES
|
||||
${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/parserkf.c
|
||||
${PM3_ROOT}/client/src/parsers/hrtparser/hrtparser.c
|
||||
${PM3_ROOT}/client/src/pla.c
|
||||
${PM3_ROOT}/client/src/preferences.c
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -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
|
||||
Reference in new issue
Block a user